e1000e.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. /* Intel e1000/e1000e network driver.
  2. *
  3. * Targets the PCH LAN controllers used in Arrandale/Clarkdale laptops
  4. * (82577LM/82577LC, plus the 82578/82579 relations) and the plain e1000
  5. * (82540EM) that QEMU emulates. The MAC/DMA path is the classic e1000
  6. * register set with legacy 16-byte descriptors; the PHY is reached through
  7. * MDIC. Everything is polled: interrupts are masked and the ring status
  8. * bits are checked directly, like the other nebos NIC drivers.
  9. *
  10. * Mapped BAR0 (128 KB on the 82577LM) is above the 128 MB identity map, so
  11. * process_map_mmio_all() installs it in every process page directory: kernel
  12. * code runs with the current process's CR3 and the network syscalls follow
  13. * the user process.
  14. */
  15. #include "e1000e.h"
  16. #include "bootcon.h"
  17. #include "io.h"
  18. #include "mm.h"
  19. #include "netdev.h"
  20. #include "paging.h"
  21. #include "pci.h"
  22. #include "process.h"
  23. #include "serial.h"
  24. #include "timer.h"
  25. /* ---- register offsets (BAR0) ---- */
  26. #define REG_CTRL 0x00000
  27. #define REG_STATUS 0x00008
  28. #define REG_EERD 0x00014
  29. #define REG_CTRL_EXT 0x00018
  30. #define REG_MDIC 0x00020
  31. #define REG_PBA 0x01000
  32. #define REG_WUC 0x05800
  33. #define REG_VET 0x00038
  34. #define REG_ICR 0x000C0
  35. #define REG_IMC 0x000D8
  36. #define REG_RCTL 0x00100
  37. #define REG_TCTL 0x00400
  38. #define REG_TIPG 0x00410
  39. #define REG_RDBAL 0x02800
  40. #define REG_RDBAH 0x02804
  41. #define REG_RDLEN 0x02808
  42. #define REG_RDH 0x02810
  43. #define REG_RDT 0x02818
  44. #define REG_TDBAL 0x03800
  45. #define REG_TDBAH 0x03804
  46. #define REG_TDLEN 0x03808
  47. #define REG_TDH 0x03810
  48. #define REG_TDT 0x03818
  49. #define REG_TXDCTL0 0x03828
  50. #define REG_TXDCTL1 0x03928
  51. #define REG_TARC0 0x03840
  52. #define REG_TARC1 0x03940
  53. #define REG_RAL0 0x05400
  54. #define REG_RAH0 0x05404
  55. #define REG_GCR 0x05B00
  56. /* GCR no-snoop enables: 1 = the NIC bypasses the CPU cache for that DMA
  57. * type. A firmware that leaves the TX bits set makes the NIC read freshly
  58. * written TX descriptors from stale DRAM, so TX never completes (RX still
  59. * works because its descriptor lines were evicted long before the traffic).
  60. * Linux clears these on every ich8/pch part. */
  61. #define GCR_NO_SNOOP 0x0000003Fu
  62. /* ---- CTRL ---- */
  63. #define CTRL_RST 0x04000000u
  64. #define CTRL_GIO_MASTER_DISABLE 0x00000004u
  65. /* ---- STATUS ---- */
  66. #define STATUS_FD 0x00000001u
  67. #define STATUS_LU 0x00000002u
  68. #define STATUS_SPEED 0x000000C0u
  69. #define STATUS_LAN_INIT_DONE 0x00000200u
  70. #define STATUS_GIO_MASTER_ENABLE 0x00080000u
  71. /* ---- RCTL ---- */
  72. #define RCTL_EN 0x00000002u
  73. #define RCTL_UPE 0x00000008u
  74. #define RCTL_MPE 0x00000010u
  75. #define RCTL_LPE 0x00000020u
  76. #define RCTL_BAM 0x00008000u
  77. #define RCTL_SECRC 0x04000000u
  78. /* ---- TCTL ---- */
  79. #define TCTL_EN 0x00000002u
  80. #define TCTL_PSP 0x00000008u
  81. #define TCTL_RTLC 0x01000000u
  82. #define TCTL_CT_SHIFT 4
  83. #define TCTL_COLD_SHIFT 8
  84. #define TCTL_CT 0x10u
  85. #define TCTL_COLD 0x40u
  86. /* ---- TXDCTL (PCH LAN: descriptor write-back policy) ---- */
  87. #define TXDCTL_PTHRESH 0x0000003Fu
  88. #define TXDCTL_WTHRESH 0x003F0000u
  89. #define TXDCTL_COUNT_DESC 0x00400000u
  90. #define TXDCTL_GRAN 0x01000000u
  91. #define TXDCTL_WB_ONE 0x01010000u /* GRAN | WTHRESH=1 */
  92. #define TXDCTL_PREFETCH 0x0100001Fu /* GRAN | PTHRESH=31 */
  93. #define TXDCTL_HTHRESH 0x00000100u /* host threshold = 1 */
  94. /* ---- MDIC ---- */
  95. #define MDIC_DATA_MASK 0x0000FFFFu
  96. #define MDIC_REG_SHIFT 16
  97. #define MDIC_PHY_SHIFT 21
  98. #define MDIC_OP_WRITE 0x04000000u
  99. #define MDIC_OP_READ 0x08000000u
  100. #define MDIC_READY 0x10000000u
  101. #define MDIC_ERROR 0x40000000u
  102. /* ---- EERD ---- */
  103. #define EERD_START 0x00000001u
  104. #define EERD_DONE 0x00000010u
  105. #define EERD_ADDR_SHIFT 8
  106. #define EERD_DATA_SHIFT 16
  107. /* ---- descriptor status ---- */
  108. #define RX_DD 0x01
  109. #define RX_EOP 0x02
  110. #define TX_DD 0x01
  111. #define TX_CMD (0x01u | 0x02u | 0x08u) /* EOP | IFCS | RS */
  112. /* ---- rings ---- */
  113. #define E1000E_NRX 64
  114. #define E1000E_NTX 16
  115. #define E1000E_BUF 2048
  116. #define E1000E_TX_TIMEOUT_MS 20
  117. #define E1000E_TX_FAIL_MAX 32
  118. /* ---- PHY / MII ---- */
  119. #define MII_BMCR 0
  120. #define MII_BMSR 1
  121. #define MII_PHYID1 2
  122. #define MII_PHYID2 3
  123. #define MII_ANAR 4
  124. #define MII_GBSR 9
  125. #define BMCR_ANENABLE 0x1000
  126. #define BMCR_ANRESTART 0x0200
  127. /* ---- PCI ---- */
  128. #define PCI_CMD_INT_DISABLE 0x0400u /* command bit 10: INTx disabled */
  129. #define E1000E_VENDOR 0x8086u
  130. struct e1000e_rx_desc {
  131. volatile unsigned int addr_lo;
  132. volatile unsigned int addr_hi;
  133. volatile unsigned short length;
  134. volatile unsigned short csum;
  135. volatile unsigned char status;
  136. volatile unsigned char errors;
  137. volatile unsigned short special;
  138. } __attribute__((packed));
  139. struct e1000e_tx_desc {
  140. volatile unsigned int addr_lo;
  141. volatile unsigned int addr_hi;
  142. volatile unsigned int lower;
  143. volatile unsigned int upper;
  144. } __attribute__((packed));
  145. static unsigned char *mmio;
  146. static int present;
  147. static int inited;
  148. static unsigned char mac[6];
  149. static unsigned int bar_base, bar_size;
  150. static unsigned int pci_bus, pci_dev, pci_func;
  151. static unsigned int device_id;
  152. static const char *detected_chip = "Intel e1000e";
  153. static struct e1000e_rx_desc *rx_ring;
  154. static struct e1000e_tx_desc *tx_ring;
  155. static unsigned char *rx_bufs;
  156. static unsigned char *tx_bufs;
  157. static unsigned int rx_cur;
  158. static unsigned int tx_cur;
  159. static unsigned int tx_pending; /* bitmask: submitted, DD not seen yet */
  160. static unsigned int tx_fail_run; /* consecutive TX timeouts */
  161. static int tx_broken; /* stop stalling the system on a dead TX */
  162. static unsigned int tctl_fixes; /* TCTL.EN re-asserts */
  163. static void tx_queue_restart(void);
  164. static int tx_ensure_enabled(void);
  165. static int phy_addr = -1;
  166. static int phy_id1 = -1, phy_id2 = -1;
  167. static int pch_lan; /* 82577/82578/82579: keep the PHY as-is */
  168. /* Counters (also reported by `netregs`). */
  169. static unsigned int tx_ok, tx_err, rx_ok, rx_err, rx_drop;
  170. static unsigned int last_tx_status, last_rx_status, last_rx_errors;
  171. static unsigned int reset_count;
  172. static int timed_out(unsigned int start_ms, unsigned int ms)
  173. {
  174. return get_ticks_ms() - start_ms > ms;
  175. }
  176. static void delay_ms(unsigned int ms)
  177. {
  178. unsigned int t0 = get_ticks_ms();
  179. while (get_ticks_ms() - t0 < ms)
  180. ;
  181. }
  182. /* ---- serial debug trail (the netlog mirror carries it to `make netmon`) ---- */
  183. static void dbg(const char *s)
  184. {
  185. serial_puts(SERIAL_COM1, "e1000e: ");
  186. serial_puts(SERIAL_COM1, s);
  187. serial_puts(SERIAL_COM1, "\n");
  188. /* Also show the trail on the boot console (NOWM diagnostics): if the
  189. * probe wedges, the last line says which step. No-op once the WM has
  190. * taken over and disabled the console. */
  191. bootcon_puts("e1000e: ");
  192. bootcon_puts(s);
  193. bootcon_puts("\n");
  194. }
  195. static void put_dec(unsigned int v)
  196. {
  197. char b[12];
  198. int i = 0;
  199. if (v == 0) {
  200. io_putchar('0');
  201. return;
  202. }
  203. while (v) {
  204. b[i++] = (char)('0' + v % 10);
  205. v /= 10;
  206. }
  207. while (i)
  208. io_putchar(b[--i]);
  209. }
  210. static void put_hex2(unsigned int v)
  211. {
  212. static const char hx[] = "0123456789ABCDEF";
  213. io_putchar(hx[(v >> 4) & 0xF]);
  214. io_putchar(hx[v & 0xF]);
  215. }
  216. static void put_mac(const unsigned char *m)
  217. {
  218. unsigned int i;
  219. for (i = 0; i < 6; i++) {
  220. put_hex2(m[i]);
  221. if (i != 5)
  222. io_putchar(':');
  223. }
  224. }
  225. /* ---- MMIO accessors (uncacheable mapping) ---- */
  226. static inline unsigned int r32(unsigned int reg)
  227. {
  228. return *(volatile unsigned int *)(mmio + reg);
  229. }
  230. static inline void w32(unsigned int reg, unsigned int v)
  231. {
  232. *(volatile unsigned int *)(mmio + reg) = v;
  233. }
  234. /* ---- PCI discovery ---- */
  235. static int supported_device(unsigned int dev)
  236. {
  237. switch (dev) {
  238. case 0x100E: /* 82540EM: QEMU's e1000 */
  239. case 0x10D3: /* 82574L: QEMU's e1000e */
  240. case 0x10EA: /* 82577LM: ThinkPad X201 */
  241. case 0x10EB: /* 82577LC */
  242. case 0x10EF: /* 82578DM */
  243. case 0x10F0: /* 82578DC */
  244. case 0x1502: /* 82579LM */
  245. case 0x1503: /* 82579V */
  246. return 1;
  247. default:
  248. return 0;
  249. }
  250. }
  251. static const char *chip_name(unsigned int dev)
  252. {
  253. switch (dev) {
  254. case 0x100E: return "Intel 82540EM (QEMU e1000)";
  255. case 0x10D3: return "Intel 82574L";
  256. case 0x10EA: return "Intel 82577LM";
  257. case 0x10EB: return "Intel 82577LC";
  258. case 0x10EF: return "Intel 82578DM";
  259. case 0x10F0: return "Intel 82578DC";
  260. case 0x1502: return "Intel 82579LM";
  261. case 0x1503: return "Intel 82579V";
  262. default: return "Intel e1000e";
  263. }
  264. }
  265. /* PCH-integrated LAN: the PHY sits behind the Kumeran interface and needs a
  266. * large set of PHY/Kumeran workarounds after a full-chip reset. Booting the
  267. * firmware has already configured it, so the driver deliberately does a warm
  268. * init on these parts (stop our RX/TX, leave CTRL.PHY_RST alone) and only
  269. * restarts autoneg if the link is down. */
  270. static int is_pch_lan(unsigned int dev)
  271. {
  272. switch (dev) {
  273. case 0x10EA: case 0x10EB: case 0x10EF: case 0x10F0:
  274. case 0x1502: case 0x1503:
  275. return 1;
  276. default:
  277. return 0;
  278. }
  279. }
  280. /* BAR0 is the 128 KB register window on every supported part. Handle a
  281. * 64-bit BAR and probe its size with the usual all-ones trick. */
  282. static int probe_bar0(unsigned int bus, unsigned int dev, unsigned int func,
  283. unsigned int *base, unsigned int *size)
  284. {
  285. unsigned int val = pci_cfg_read32(bus, dev, func, 0x10);
  286. if (!val || val == 0xFFFFFFFFu || (val & 1u))
  287. return -1;
  288. /* Do NOT size the BAR by writing 0xFFFFFFFF: on PCH LAN parts the
  289. * ME/AMT can leave the BAR restore incomplete, after which every MMIO
  290. * access goes to an undecoded address and stalls. All supported
  291. * e1000/e1000e BARs are 128 KB, so trust the firmware-assigned base. */
  292. if (val & 4u)
  293. return -1; /* 64-bit BAR: not used by these parts */
  294. *base = val & 0xFFFFFFF0u;
  295. *size = 0x20000;
  296. return 0;
  297. }
  298. /* Put the function in D0 before the first MMIO access. The PCH LAN is
  299. * often left in D3hot by the BIOS/ME; MMIO while suspended can return
  300. * 0xFFFFFFFF or stall the PCIe root complex. Walk the capability list for
  301. * the PM capability (ID 1) and clear PMCSR.PS. */
  302. static void pci_set_d0(unsigned int bus, unsigned int dev, unsigned int func)
  303. {
  304. unsigned int status = pci_cfg_read32(bus, dev, func, 0x04) >> 16;
  305. unsigned int cap, guard = 0;
  306. if (!(status & 0x10u))
  307. return; /* no capability list */
  308. cap = pci_cfg_read32(bus, dev, func, 0x34) & 0xFCu;
  309. while (cap && guard++ < 48) {
  310. unsigned int hdr = pci_cfg_read32(bus, dev, func, cap);
  311. if ((hdr & 0xFFu) == 0x01u) {
  312. unsigned int pmcsr =
  313. pci_cfg_read32(bus, dev, func, cap + 4);
  314. if (pmcsr & 3u) {
  315. pci_cfg_write32(bus, dev, func, cap + 4,
  316. pmcsr & ~3u);
  317. delay_ms(10);
  318. dbg("PM state -> D0");
  319. }
  320. return;
  321. }
  322. cap = (hdr >> 8) & 0xFCu;
  323. }
  324. }
  325. static int probe_pci(void)
  326. {
  327. unsigned int bus, dev, func;
  328. for (bus = 0; bus < 256; bus++) {
  329. for (dev = 0; dev < 32; dev++) {
  330. unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
  331. unsigned int htype, nfunc;
  332. if (id == 0xFFFFFFFFu || id == 0)
  333. continue;
  334. htype = (pci_cfg_read32(bus, dev, 0, 0x0C) >> 16) & 0xFFu;
  335. nfunc = (htype & 0x80u) ? 8u : 1u;
  336. for (func = 0; func < nfunc; func++) {
  337. unsigned int idf, cmd;
  338. idf = pci_cfg_read32(bus, dev, func, 0x00);
  339. if (idf == 0xFFFFFFFFu || idf == 0)
  340. continue;
  341. if ((idf & 0xFFFFu) != E1000E_VENDOR)
  342. continue;
  343. if (!supported_device((idf >> 16) & 0xFFFFu))
  344. continue;
  345. /* Memory space + bus master. */
  346. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  347. /* Polled driver: disable INTx before enabling memory
  348. * and bus master. A link-change interrupt from
  349. * the PHY can otherwise fire before hw_stop()
  350. * masks the device, and the kernel has no NIC IRQ
  351. * handler (the X201 stalls with the cable in). */
  352. pci_cfg_write32(bus, dev, func, 0x04,
  353. cmd | 0x0006u |
  354. PCI_CMD_INT_DISABLE);
  355. pci_set_d0(bus, dev, func);
  356. /* Let the function settle after D0/memory enable;
  357. * the ME can still be finishing its link work when
  358. * the cable is present. */
  359. delay_ms(10);
  360. if (probe_bar0(bus, dev, func,
  361. &bar_base, &bar_size) != 0)
  362. continue;
  363. pci_bus = bus;
  364. pci_dev = dev;
  365. pci_func = func;
  366. device_id = (idf >> 16) & 0xFFFFu;
  367. detected_chip = chip_name(device_id);
  368. pch_lan = is_pch_lan(device_id);
  369. return 0;
  370. }
  371. }
  372. }
  373. return -1;
  374. }
  375. /* ---- MAC address ---- */
  376. /* Classic EERD word read; used only when RAL/RAH came back invalid. */
  377. static int eeprom_read16(unsigned int addr, unsigned short *out)
  378. {
  379. unsigned int t0, v;
  380. w32(REG_EERD, (addr << EERD_ADDR_SHIFT) | EERD_START);
  381. t0 = get_ticks_ms();
  382. for (;;) {
  383. v = r32(REG_EERD);
  384. if (v & EERD_DONE) {
  385. *out = (unsigned short)(v >> EERD_DATA_SHIFT);
  386. return 0;
  387. }
  388. if (timed_out(t0, 5))
  389. return -1;
  390. }
  391. }
  392. static int mac_valid(const unsigned char *m)
  393. {
  394. unsigned int i;
  395. int zero = 1, ones = 1;
  396. for (i = 0; i < 6; i++) {
  397. if (m[i] != 0x00)
  398. zero = 0;
  399. if (m[i] != 0xFF)
  400. ones = 0;
  401. }
  402. return !zero && !ones;
  403. }
  404. static void read_mac(void)
  405. {
  406. unsigned int ral = r32(REG_RAL0);
  407. unsigned int rah = r32(REG_RAH0);
  408. mac[0] = (unsigned char)(ral & 0xFF);
  409. mac[1] = (unsigned char)((ral >> 8) & 0xFF);
  410. mac[2] = (unsigned char)((ral >> 16) & 0xFF);
  411. mac[3] = (unsigned char)((ral >> 24) & 0xFF);
  412. mac[4] = (unsigned char)(rah & 0xFF);
  413. mac[5] = (unsigned char)((rah >> 8) & 0xFF);
  414. if (!mac_valid(mac)) {
  415. unsigned short w0 = 0, w1 = 0, w2 = 0;
  416. if (eeprom_read16(0, &w0) == 0 &&
  417. eeprom_read16(1, &w1) == 0 &&
  418. eeprom_read16(2, &w2) == 0) {
  419. mac[0] = (unsigned char)(w0 & 0xFF);
  420. mac[1] = (unsigned char)(w0 >> 8);
  421. mac[2] = (unsigned char)(w1 & 0xFF);
  422. mac[3] = (unsigned char)(w1 >> 8);
  423. mac[4] = (unsigned char)(w2 & 0xFF);
  424. mac[5] = (unsigned char)(w2 >> 8);
  425. }
  426. }
  427. /* Program the unicast filter (RAH first, then RAL on these parts).
  428. * Promiscuous mode is on as well for bring-up, but a valid RAL/RAH
  429. * keeps the hardware filter sane if UPE is ever turned off. */
  430. w32(REG_RAH0, ((unsigned int)mac[5] << 8) | mac[4]);
  431. w32(REG_RAL0, (unsigned int)mac[0] | ((unsigned int)mac[1] << 8) |
  432. ((unsigned int)mac[2] << 16) | ((unsigned int)mac[3] << 24));
  433. }
  434. /* ---- PHY access via MDIC ---- */
  435. static int mdic_read(unsigned int phy, unsigned int reg)
  436. {
  437. unsigned int t0, v;
  438. w32(REG_MDIC, (reg << MDIC_REG_SHIFT) | (phy << MDIC_PHY_SHIFT) |
  439. MDIC_OP_READ);
  440. t0 = get_ticks_ms();
  441. for (;;) {
  442. v = r32(REG_MDIC);
  443. if (v & MDIC_READY)
  444. return (v & MDIC_ERROR) ? -1 : (int)(v & MDIC_DATA_MASK);
  445. if (timed_out(t0, 10))
  446. return -1;
  447. }
  448. }
  449. static int mdic_write(unsigned int phy, unsigned int reg, unsigned int data)
  450. {
  451. unsigned int t0, v;
  452. w32(REG_MDIC, (data & MDIC_DATA_MASK) | (reg << MDIC_REG_SHIFT) |
  453. (phy << MDIC_PHY_SHIFT) | MDIC_OP_WRITE);
  454. t0 = get_ticks_ms();
  455. for (;;) {
  456. v = r32(REG_MDIC);
  457. if (v & MDIC_READY)
  458. return (v & MDIC_ERROR) ? -1 : 0;
  459. if (timed_out(t0, 10))
  460. return -1;
  461. }
  462. }
  463. static void phy_probe(void)
  464. {
  465. unsigned int phy;
  466. phy_addr = -1;
  467. phy_id1 = phy_id2 = -1;
  468. for (phy = 0; phy < 4; phy++) {
  469. int id1 = mdic_read(phy, MII_PHYID1);
  470. int id2 = mdic_read(phy, MII_PHYID2);
  471. if (id1 < 0 || id2 < 0)
  472. continue;
  473. if ((id1 & 0xFFFF) == 0 && (id2 & 0xFFFF) == 0)
  474. continue;
  475. if ((id1 & 0xFFFF) == 0xFFFF)
  476. continue;
  477. phy_addr = (int)phy;
  478. phy_id1 = id1;
  479. phy_id2 = id2;
  480. break;
  481. }
  482. }
  483. static void phy_autoneg(void)
  484. {
  485. if (phy_addr < 0)
  486. return;
  487. mdic_write(phy_addr, MII_ANAR, 0x01E1); /* 10/100 half+full */
  488. mdic_write(phy_addr, MII_GBSR, 0x0300); /* 1000 half+full */
  489. mdic_write(phy_addr, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
  490. }
  491. /* ---- hardware bring-up ---- */
  492. /* Quiesce the MAC without touching the PHY: mask interrupts, stop RX/TX and
  493. * let any in-flight DMA finish. This is the "reset" path on PCH LAN parts. */
  494. static void hw_stop(void)
  495. {
  496. /* PCH LAN: let the ME finish the current link transaction before the
  497. * first MAC write. */
  498. delay_ms(5);
  499. w32(REG_IMC, 0xFFFFFFFFu);
  500. (void)r32(REG_ICR);
  501. w32(REG_RCTL, 0);
  502. w32(REG_TCTL, TCTL_PSP);
  503. (void)r32(REG_STATUS); /* flush posted writes */
  504. delay_ms(10);
  505. }
  506. /* PCH LAN with AMT: the firmware (ME) must be told that the OS driver owns
  507. * the network interface, otherwise it can keep the LAN - notably the TX
  508. * path - to itself. e1000e's e1000e_get_hw_control() sets
  509. * CTRL_EXT.DRV_LOAD for parts with FLAG_HAS_CTRLEXT_ON_LOAD (the pch_info
  510. * used by the 82577LM/82578/82579). Do it after the reset/quiesce path and
  511. * before the rings are programmed, exactly where e1000e calls it. */
  512. static void pch_claim(void)
  513. {
  514. unsigned int ctrl_ext;
  515. if (!pch_lan)
  516. return;
  517. ctrl_ext = r32(REG_CTRL_EXT);
  518. if (!(ctrl_ext & 0x10000000u)) {
  519. w32(REG_CTRL_EXT, ctrl_ext | 0x10000000u);
  520. dbg("AMT handshake: CTRL_EXT.DRV_LOAD set");
  521. }
  522. /* e1000e_reset(): clear wake-up control and enable VLAN EtherType.
  523. * PBA is only read/reported: writing the generic PCH default of 26
  524. * would set the TX half to zero on split-PBA parts (verified on
  525. * QEMU's 82540: 0x00100030 -> 0x0000001A). */
  526. w32(REG_WUC, 0);
  527. w32(REG_VET, 0x8100);
  528. serial_puts(SERIAL_COM1, "e1000e: pba ");
  529. serial_puthex(SERIAL_COM1, r32(REG_PBA));
  530. serial_puts(SERIAL_COM1, "\n");
  531. }
  532. static int hw_reset(void)
  533. {
  534. unsigned int t0, ctrl;
  535. /* Sample the link before hw_stop(): the PHY link is independent of
  536. * the MAC RX/TX state. */
  537. int link_up = pch_lan && (r32(REG_STATUS) & STATUS_LU);
  538. hw_stop();
  539. if (link_up) {
  540. /* Do not issue CTRL_RST while the ME has the link up: on the
  541. * X201 the CTRL access after GIO_MASTER_DISABLE wedges. The
  542. * firmware already brought the MAC/PHY up; hw_stop() stopped
  543. * RX/TX and hw_start() reprograms the queues below. */
  544. dbg("MAC reset skipped (link up)");
  545. return 0;
  546. }
  547. delay_ms(5);
  548. /* Block new DMA requests and wait for the last TLP to finish before
  549. * hitting the MAC with a global reset (same order as e1000e). */
  550. ctrl = r32(REG_CTRL);
  551. w32(REG_CTRL, ctrl | CTRL_GIO_MASTER_DISABLE);
  552. t0 = get_ticks_ms();
  553. while ((r32(REG_STATUS) & STATUS_GIO_MASTER_ENABLE) &&
  554. !timed_out(t0, 1000))
  555. ;
  556. delay_ms(5);
  557. w32(REG_CTRL, r32(REG_CTRL) | CTRL_RST);
  558. t0 = get_ticks_ms();
  559. while (r32(REG_CTRL) & CTRL_RST) {
  560. if (timed_out(t0, 1000))
  561. return -1;
  562. }
  563. delay_ms(20); /* NVM auto-read after reset */
  564. /* Wait for the NVM auto-read to finish and clear the done bit. */
  565. t0 = get_ticks_ms();
  566. while (!(r32(REG_STATUS) & STATUS_LAN_INIT_DONE) &&
  567. !timed_out(t0, 200))
  568. ;
  569. w32(REG_STATUS, r32(REG_STATUS) & ~STATUS_LAN_INIT_DONE);
  570. reset_count++;
  571. return 0;
  572. }
  573. static void setup_rings(void)
  574. {
  575. unsigned int i;
  576. for (i = 0; i < E1000E_NRX; i++) {
  577. rx_ring[i].addr_lo = (unsigned int)(rx_bufs + i * E1000E_BUF);
  578. rx_ring[i].addr_hi = 0;
  579. rx_ring[i].length = 0;
  580. rx_ring[i].csum = 0;
  581. rx_ring[i].status = 0;
  582. rx_ring[i].errors = 0;
  583. rx_ring[i].special = 0;
  584. }
  585. for (i = 0; i < E1000E_NTX; i++) {
  586. tx_ring[i].addr_lo = (unsigned int)(tx_bufs + i * E1000E_BUF);
  587. tx_ring[i].addr_hi = 0;
  588. tx_ring[i].lower = 0;
  589. tx_ring[i].upper = 0;
  590. }
  591. __sync_synchronize();
  592. w32(REG_RDBAL, (unsigned int)rx_ring);
  593. w32(REG_RDBAH, 0);
  594. w32(REG_RDLEN, E1000E_NRX * (unsigned int)sizeof(struct e1000e_rx_desc));
  595. w32(REG_RDH, 0);
  596. w32(REG_RDT, E1000E_NRX - 1);
  597. w32(REG_TDBAL, (unsigned int)tx_ring);
  598. w32(REG_TDBAH, 0);
  599. w32(REG_TDLEN, E1000E_NTX * (unsigned int)sizeof(struct e1000e_tx_desc));
  600. w32(REG_TDH, 0);
  601. w32(REG_TDT, 0);
  602. rx_cur = 0;
  603. tx_cur = 0;
  604. tx_pending = 0;
  605. tx_fail_run = 0;
  606. tx_broken = 0;
  607. }
  608. /* PCH LAN: program the TX descriptor write-back policy and arbitration bits
  609. * that e1000e's ich8lan init sets. Without WTHRESH/COUNT_DESC the 82577 was
  610. * observed to never report TX descriptor completion (tx_ok stayed 0), while
  611. * RX worked. */
  612. static void tx_desc_config(void)
  613. {
  614. unsigned int v, gcr;
  615. /* Enable snooping for TX/RX DMA (see GCR_NO_SNOOP). */
  616. gcr = r32(REG_GCR);
  617. w32(REG_GCR, gcr & ~GCR_NO_SNOOP);
  618. serial_puts(SERIAL_COM1, "e1000e: gcr ");
  619. serial_puthex(SERIAL_COM1, gcr);
  620. serial_puts(SERIAL_COM1, " -> ");
  621. serial_puthex(SERIAL_COM1, gcr & ~GCR_NO_SNOOP);
  622. serial_puts(SERIAL_COM1, "\n");
  623. /* CTRL_EXT bits e1000e's ich8 init sets: relaxed-ordering disable,
  624. * the always-set bit 22, and PHY power-down in D3. */
  625. v = r32(REG_CTRL_EXT);
  626. v |= 0x00020000u | 0x00400000u | 0x00100000u;
  627. w32(REG_CTRL_EXT, v);
  628. v = r32(REG_TXDCTL0);
  629. v |= TXDCTL_COUNT_DESC;
  630. v = (v & ~TXDCTL_WTHRESH) | TXDCTL_WB_ONE;
  631. v = (v & ~TXDCTL_PTHRESH) | TXDCTL_PREFETCH;
  632. v |= TXDCTL_HTHRESH;
  633. w32(REG_TXDCTL0, v);
  634. v = r32(REG_TXDCTL1);
  635. v |= TXDCTL_COUNT_DESC;
  636. v = (v & ~TXDCTL_WTHRESH) | TXDCTL_WB_ONE;
  637. v = (v & ~TXDCTL_PTHRESH) | TXDCTL_PREFETCH;
  638. v |= TXDCTL_HTHRESH;
  639. w32(REG_TXDCTL1, v);
  640. /* TARC queue arbitration workarounds from the PCH init. */
  641. w32(REG_TARC0, r32(REG_TARC0) | 0x0D800000u);
  642. w32(REG_TARC1, r32(REG_TARC1) | 0x51000000u);
  643. }
  644. static void hw_start(void)
  645. {
  646. setup_rings();
  647. w32(REG_RCTL, RCTL_EN | RCTL_UPE | RCTL_MPE | RCTL_BAM | RCTL_SECRC);
  648. if (pch_lan)
  649. tx_desc_config();
  650. w32(REG_TCTL, TCTL_EN | TCTL_PSP | TCTL_RTLC |
  651. (TCTL_CT << TCTL_CT_SHIFT) |
  652. (TCTL_COLD << TCTL_COLD_SHIFT));
  653. w32(REG_TIPG, 10u | (8u << 10) | (6u << 20));
  654. __sync_synchronize();
  655. }
  656. /* ---- netdev interface ---- */
  657. static int e1000e_init(void)
  658. {
  659. char buf[64];
  660. unsigned int i;
  661. if (inited)
  662. return present ? 0 : -1;
  663. inited = 1;
  664. if (probe_pci() != 0) {
  665. dbg("no supported Intel NIC");
  666. return -1;
  667. }
  668. process_map_mmio_all(bar_base, bar_size);
  669. mmio = (unsigned char *)(unsigned long)bar_base;
  670. if (r32(REG_STATUS) == 0xFFFFFFFFu) {
  671. dbg("MMIO read failed");
  672. mmio = 0;
  673. return -1;
  674. }
  675. i = 0;
  676. /* "PCI 00:19.0 f2500000 128K" */
  677. {
  678. static const char hx[] = "0123456789ABCDEF";
  679. unsigned int n = 0;
  680. buf[i++] = 'P'; buf[i++] = 'C'; buf[i++] = 'I'; buf[i++] = ' ';
  681. for (n = 0; n < 2; n++)
  682. buf[i++] = hx[(pci_bus >> (4 * (1 - n))) & 0xF];
  683. buf[i++] = ':';
  684. for (n = 0; n < 2; n++)
  685. buf[i++] = hx[(pci_dev >> (4 * (1 - n))) & 0xF];
  686. buf[i++] = '.';
  687. buf[i++] = (char)('0' + pci_func);
  688. buf[i++] = ' ';
  689. for (n = 0; n < 8; n++)
  690. buf[i++] = hx[(bar_base >> (4 * (7 - n))) & 0xF];
  691. buf[i++] = ' ';
  692. buf[i] = '\0';
  693. }
  694. dbg(buf);
  695. /* The PCH LAN/ME needs a moment after the config-space setup before
  696. * the first MAC register access; without it the probe can wedge when
  697. * the link is up. */
  698. delay_ms(10);
  699. /* MAC-only reset: CTRL.PHY_RST is deliberately never set, so the
  700. * firmware's PHY/Kumeran setup (and the link) survives, while the MAC
  701. * registers - including whatever TX queue state the BIOS UNDI left
  702. * behind - go back to their defaults. e1000e does the same on parts
  703. * where the ME owns the PHY. */
  704. if (hw_reset() != 0) {
  705. dbg("MAC reset timeout");
  706. mmio = 0;
  707. return -1;
  708. }
  709. dbg("reset ok");
  710. pch_claim();
  711. read_mac();
  712. /* All ring and buffer memory in one allocation: the page allocator is
  713. * IRQ-safe but not cross-CPU locked, so two separate calls can (and
  714. * once did) hand out the same page under boot-time concurrency. A
  715. * single contiguous run cannot alias itself. */
  716. {
  717. unsigned int pages = 2 +
  718. (E1000E_NRX * E1000E_BUF) / PAGE_SIZE +
  719. (E1000E_NTX * E1000E_BUF) / PAGE_SIZE;
  720. unsigned int mem = mm_alloc_pages(pages);
  721. if (mem) {
  722. rx_ring = (struct e1000e_rx_desc *)mem;
  723. tx_ring = (struct e1000e_tx_desc *)(mem + PAGE_SIZE);
  724. rx_bufs = (unsigned char *)(mem + 2 * PAGE_SIZE);
  725. tx_bufs = rx_bufs + E1000E_NRX * E1000E_BUF;
  726. }
  727. }
  728. serial_puts(SERIAL_COM1, "e1000e: ring mem rx=");
  729. serial_puthex(SERIAL_COM1, (unsigned int)rx_ring);
  730. serial_puts(SERIAL_COM1, " tx=");
  731. serial_puthex(SERIAL_COM1, (unsigned int)tx_ring);
  732. serial_puts(SERIAL_COM1, " rxb=");
  733. serial_puthex(SERIAL_COM1, (unsigned int)rx_bufs);
  734. serial_puts(SERIAL_COM1, " txb=");
  735. serial_puthex(SERIAL_COM1, (unsigned int)tx_bufs);
  736. serial_puts(SERIAL_COM1, "\n");
  737. if (!rx_ring || !tx_ring || !rx_bufs || !tx_bufs) {
  738. dbg("no memory for rings");
  739. mmio = 0;
  740. return -1;
  741. }
  742. phy_probe();
  743. /* The 82577 PHY is at MDIC address 1 even when the probe could not
  744. * read its ID (the PCH needs a slow-MDIO mode we do not program); keep
  745. * the address so a link-down autoneg attempt can still go out. */
  746. if (pch_lan && phy_addr < 0)
  747. phy_addr = 1;
  748. hw_start();
  749. delay_ms(10);
  750. {
  751. unsigned int st = r32(REG_STATUS);
  752. dbg(st & STATUS_LU ? "link up" : "link started");
  753. }
  754. present = 1;
  755. /* "<chip> <mac>" */
  756. i = 0;
  757. for (const char *p = detected_chip; *p && i + 1 < sizeof(buf); p++)
  758. buf[i++] = *p;
  759. buf[i++] = ' ';
  760. for (unsigned int j = 0; j < 6 && i + 4 < sizeof(buf); j++) {
  761. static const char hx[] = "0123456789ABCDEF";
  762. buf[i++] = hx[(mac[j] >> 4) & 0xF];
  763. buf[i++] = hx[mac[j] & 0xF];
  764. if (j != 5)
  765. buf[i++] = ':';
  766. }
  767. buf[i] = '\0';
  768. dbg(buf);
  769. if (!(r32(REG_STATUS) & STATUS_LU))
  770. phy_autoneg();
  771. tx_ensure_enabled();
  772. return 0;
  773. }
  774. static void e1000e_get_mac(unsigned char out[6])
  775. {
  776. unsigned int i;
  777. for (i = 0; i < 6; i++)
  778. out[i] = mac[i];
  779. }
  780. static int e1000e_rx(unsigned char *buf, unsigned int maxlen)
  781. {
  782. unsigned int guard = 0;
  783. if (!present || !mmio)
  784. return -1;
  785. while (guard++ < 8) {
  786. unsigned int entry = rx_cur;
  787. struct e1000e_rx_desc *d = &rx_ring[entry];
  788. unsigned int status = d->status;
  789. unsigned int len;
  790. if (!(status & RX_DD))
  791. return -1; /* nothing pending */
  792. __sync_synchronize();
  793. last_rx_status = status;
  794. last_rx_errors = d->errors;
  795. if ((status & RX_EOP) && d->errors == 0) {
  796. len = d->length;
  797. if (len >= 14 && len <= maxlen) {
  798. unsigned char *src = rx_bufs +
  799. entry * E1000E_BUF;
  800. unsigned int i;
  801. for (i = 0; i < len; i++)
  802. buf[i] = src[i];
  803. rx_ok++;
  804. d->status = 0;
  805. __sync_synchronize();
  806. w32(REG_RDT, entry);
  807. rx_cur = (entry + 1) % E1000E_NRX;
  808. return (int)len;
  809. }
  810. rx_drop++;
  811. } else {
  812. rx_err++;
  813. }
  814. /* Drop this descriptor and give it back to the hardware. */
  815. d->status = 0;
  816. __sync_synchronize();
  817. w32(REG_RDT, entry);
  818. rx_cur = (entry + 1) % E1000E_NRX;
  819. }
  820. return -1;
  821. }
  822. static int tx_timeout(void)
  823. {
  824. tx_err++;
  825. if (++tx_fail_run >= E1000E_TX_FAIL_MAX)
  826. tx_broken = 1;
  827. return -1;
  828. }
  829. /* The TX engine is not moving (TDH stuck): toggle TCTL.EN and re-arm the
  830. * ring. Any descriptor the hardware never retired is simply dropped; the
  831. * network stack retries the frame. This also makes the next TDT write a
  832. * real 0 -> 1 transition again, in case the first doorbell was lost while
  833. * the queue was disabled. */
  834. static void tx_queue_restart(void)
  835. {
  836. unsigned int tctl = r32(REG_TCTL);
  837. w32(REG_TCTL, tctl & ~TCTL_EN);
  838. delay_ms(1);
  839. w32(REG_TDBAL, (unsigned int)tx_ring);
  840. w32(REG_TDBAH, 0);
  841. w32(REG_TDLEN, (unsigned int)(E1000E_NTX *
  842. sizeof(struct e1000e_tx_desc)));
  843. w32(REG_TDH, 0);
  844. w32(REG_TDT, 0);
  845. w32(REG_TCTL, tctl | TCTL_EN);
  846. __sync_synchronize();
  847. tx_cur = 0;
  848. tx_pending = 0;
  849. }
  850. /* The MAC may clear TCTL.EN on a link transition; make sure it is set before
  851. * a send. Returns 1 when it had to be re-enabled. */
  852. static int tx_ensure_enabled(void)
  853. {
  854. unsigned int tctl = r32(REG_TCTL);
  855. if (tctl & TCTL_EN)
  856. return 0;
  857. w32(REG_TCTL, tctl | TCTL_EN);
  858. tctl_fixes++;
  859. return 1;
  860. }
  861. static int e1000e_tx(const unsigned char *buf, unsigned int len)
  862. {
  863. static unsigned char pad[60];
  864. struct e1000e_tx_desc *d;
  865. unsigned int entry, t0;
  866. const unsigned char *p = buf;
  867. unsigned int txlen = len;
  868. if (!present || !mmio)
  869. return -1;
  870. /* Once TX has failed repeatedly, fail instantly: the netlog flush and
  871. * the DHCP/ping loops must not hold the BKL for 100 ms per frame on a
  872. * NIC whose TX does not complete. `netreset` clears this. */
  873. if (tx_broken)
  874. return -1;
  875. if (len < 14 || len > 1514)
  876. return -1;
  877. tx_ensure_enabled();
  878. if (len < 60) {
  879. unsigned int i;
  880. for (i = 0; i < len; i++)
  881. pad[i] = buf[i];
  882. for (i = len; i < 60; i++)
  883. pad[i] = 0;
  884. p = pad;
  885. txlen = 60;
  886. }
  887. entry = tx_cur;
  888. d = &tx_ring[entry];
  889. /* A previous command on this slot that timed out may still be in
  890. * flight; wait for it before reuse. */
  891. if (tx_pending & (1u << entry)) {
  892. t0 = get_ticks_ms();
  893. while (!(d->upper & TX_DD)) {
  894. if (timed_out(t0, E1000E_TX_TIMEOUT_MS)) {
  895. /* The hardware never retired this
  896. * descriptor: restart the TX queue and
  897. * drop the slot. */
  898. tx_queue_restart();
  899. return tx_timeout();
  900. }
  901. }
  902. tx_pending &= ~(1u << entry);
  903. }
  904. {
  905. unsigned char *dst = tx_bufs + entry * E1000E_BUF;
  906. unsigned int i;
  907. for (i = 0; i < txlen; i++)
  908. dst[i] = p[i];
  909. }
  910. d->addr_lo = (unsigned int)(tx_bufs + entry * E1000E_BUF);
  911. d->addr_hi = 0;
  912. d->lower = txlen | (TX_CMD << 24);
  913. d->upper = 0;
  914. __sync_synchronize();
  915. tx_pending |= 1u << entry;
  916. w32(REG_TDT, (entry + 1) % E1000E_NTX);
  917. t0 = get_ticks_ms();
  918. while (!(d->upper & TX_DD)) {
  919. if (timed_out(t0, E1000E_TX_TIMEOUT_MS)) {
  920. /* Leave the pending bit set: the next TX on this slot
  921. * waits for it before reuse. */
  922. return tx_timeout();
  923. }
  924. }
  925. last_tx_status = d->upper;
  926. tx_pending &= ~(1u << entry);
  927. tx_fail_run = 0;
  928. tx_ok++;
  929. tx_cur = (entry + 1) % E1000E_NTX;
  930. return 0;
  931. }
  932. static void e1000e_print_info(void)
  933. {
  934. unsigned int st, bmcr = 0, bmsr = 0;
  935. if (!present || !mmio) {
  936. io_putstr("e1000e: no NIC\n");
  937. return;
  938. }
  939. st = r32(REG_STATUS);
  940. io_putstr("chip: ");
  941. io_putstr(detected_chip);
  942. io_putstr("\npci ");
  943. put_dec(pci_bus); io_putchar(':');
  944. put_dec(pci_dev); io_putchar('.');
  945. put_dec(pci_func);
  946. io_putstr(" bar0=0x");
  947. {
  948. static const char hx[] = "0123456789ABCDEF";
  949. unsigned int i;
  950. for (i = 0; i < 8; i++)
  951. io_putchar(hx[(bar_base >> (4 * (7 - i))) & 0xF]);
  952. }
  953. io_putstr(" mac ");
  954. put_mac(mac);
  955. io_putstr("\nlink ");
  956. if (st & STATUS_LU) {
  957. io_putstr((st & STATUS_FD) ? "up full" : "up half");
  958. io_putstr(" ");
  959. switch (st & STATUS_SPEED) {
  960. case 0x00: io_putstr("10M"); break;
  961. case 0x40: io_putstr("100M"); break;
  962. case 0x80: io_putstr("1000M"); break;
  963. default: io_putstr("?M"); break;
  964. }
  965. } else {
  966. io_putstr("down");
  967. }
  968. io_putstr(" status=0x");
  969. io_puthex(st);
  970. io_putstr("\nphy ");
  971. if (phy_addr >= 0) {
  972. put_dec((unsigned int)phy_addr);
  973. io_putstr(" id ");
  974. put_hex2((unsigned int)(phy_id1 >> 8) & 0xFF);
  975. put_hex2((unsigned int)phy_id1 & 0xFF);
  976. io_putchar(':');
  977. put_hex2((unsigned int)(phy_id2 >> 8) & 0xFF);
  978. put_hex2((unsigned int)phy_id2 & 0xFF);
  979. bmcr = (unsigned int)mdic_read((unsigned int)phy_addr, MII_BMCR);
  980. bmsr = (unsigned int)mdic_read((unsigned int)phy_addr, MII_BMSR);
  981. io_putstr(" bmcr=0x");
  982. io_puthex(bmcr);
  983. io_putstr(" bmsr=0x");
  984. io_puthex(bmsr);
  985. } else {
  986. io_putstr("none");
  987. }
  988. io_putstr("txdctl=0x");
  989. io_puthex(r32(REG_TXDCTL0));
  990. io_putstr(" tarc=0x");
  991. io_puthex(r32(REG_TARC0));
  992. io_putstr(" gcr=0x");
  993. io_puthex(r32(REG_GCR));
  994. io_putstr(" pba=0x");
  995. io_puthex(r32(REG_PBA));
  996. io_putstr(" tdh=0x");
  997. io_puthex(r32(REG_TDH));
  998. io_putstr(" tdt=0x");
  999. io_puthex(r32(REG_TDT));
  1000. io_putchar('\n');
  1001. io_putstr("rx ok ");
  1002. put_dec(rx_ok);
  1003. io_putstr(" err ");
  1004. put_dec(rx_err);
  1005. io_putstr(" drop ");
  1006. put_dec(rx_drop);
  1007. io_putstr(" tx ok ");
  1008. put_dec(tx_ok);
  1009. io_putstr(" err ");
  1010. put_dec(tx_err);
  1011. io_putstr(" broken ");
  1012. put_dec((unsigned int)tx_broken);
  1013. io_putstr(" tctl_fix ");
  1014. put_dec(tctl_fixes);
  1015. io_putstr(" resets ");
  1016. put_dec(reset_count);
  1017. io_putchar('\n');
  1018. }
  1019. static void e1000e_dump_regs(void)
  1020. {
  1021. static const struct {
  1022. const char *name;
  1023. unsigned int reg;
  1024. } regs[] = {
  1025. { "CTRL", REG_CTRL }, { "STATUS", REG_STATUS },
  1026. { "CTRL_EXT", REG_CTRL_EXT }, { "EERD", REG_EERD },
  1027. { "MDIC", REG_MDIC }, { "ICR", REG_ICR },
  1028. { "PBA", REG_PBA }, { "WUC", REG_WUC },
  1029. { "RCTL", REG_RCTL }, { "TCTL", REG_TCTL },
  1030. { "TIPG", REG_TIPG }, { "RAL0", REG_RAL0 },
  1031. { "RAH0", REG_RAH0 }, { "RDBAL", REG_RDBAL },
  1032. { "RDLEN", REG_RDLEN }, { "RDH", REG_RDH },
  1033. { "RDT", REG_RDT }, { "TDBAL", REG_TDBAL },
  1034. { "TDLEN", REG_TDLEN }, { "TDH", REG_TDH },
  1035. { "TDT", REG_TDT }, { "TXDCTL0", REG_TXDCTL0 },
  1036. { "TXDCTL1", REG_TXDCTL1 }, { "TARC0", REG_TARC0 },
  1037. { "TARC1", REG_TARC1 }, { "GCR", REG_GCR },
  1038. };
  1039. unsigned int i;
  1040. if (!present || !mmio) {
  1041. io_putstr("e1000e: no NIC\n");
  1042. return;
  1043. }
  1044. for (i = 0; i < sizeof(regs) / sizeof(regs[0]); i++) {
  1045. io_putstr(regs[i].name);
  1046. io_putchar('=');
  1047. io_puthex(r32(regs[i].reg));
  1048. if ((i & 1) == 1)
  1049. io_putchar('\n');
  1050. else
  1051. io_putstr(" ");
  1052. }
  1053. if (i & 1)
  1054. io_putchar('\n');
  1055. io_putstr("rings rx=0x");
  1056. io_puthex((unsigned int)rx_ring);
  1057. io_putstr(" tx=0x");
  1058. io_puthex((unsigned int)tx_ring);
  1059. io_putstr(" rx_cur=");
  1060. put_dec(rx_cur);
  1061. io_putstr(" tx_cur=");
  1062. put_dec(tx_cur);
  1063. io_putstr(" tx_pending=0x");
  1064. io_puthex(tx_pending);
  1065. io_putchar('\n');
  1066. io_putstr("state rx_ok=");
  1067. put_dec(rx_ok);
  1068. io_putstr(" rx_err=");
  1069. put_dec(rx_err);
  1070. io_putstr(" rx_drop=");
  1071. put_dec(rx_drop);
  1072. io_putstr(" tx_ok=");
  1073. put_dec(tx_ok);
  1074. io_putstr(" tx_err=");
  1075. put_dec(tx_err);
  1076. io_putstr(" tx_broken=");
  1077. put_dec((unsigned int)tx_broken);
  1078. io_putstr(" tx_fail_run=");
  1079. put_dec(tx_fail_run);
  1080. io_putstr(" tctl_fix=");
  1081. put_dec(tctl_fixes);
  1082. io_putstr(" reset=");
  1083. put_dec(reset_count);
  1084. io_putchar('\n');
  1085. io_putstr("last rx status=0x");
  1086. io_puthex(last_rx_status);
  1087. io_putstr(" errors=0x");
  1088. io_puthex(last_rx_errors);
  1089. io_putstr(" tx status=0x");
  1090. io_puthex(last_tx_status);
  1091. io_putchar('\n');
  1092. if (tx_ring) {
  1093. io_putstr("tx0 addr=0x");
  1094. io_puthex(tx_ring[0].addr_lo);
  1095. io_putstr(" lower=0x");
  1096. io_puthex(tx_ring[0].lower);
  1097. io_putstr(" upper=0x");
  1098. io_puthex(tx_ring[0].upper);
  1099. io_putchar('\n');
  1100. }
  1101. }
  1102. static void e1000e_reset(void)
  1103. {
  1104. if (!present || !mmio || !rx_ring) {
  1105. io_putstr("e1000e: reset: not initialized\n");
  1106. return;
  1107. }
  1108. if (hw_reset() != 0) {
  1109. io_putstr("e1000e: reset timeout\n");
  1110. return;
  1111. }
  1112. pch_claim();
  1113. read_mac();
  1114. hw_start();
  1115. if (!(r32(REG_STATUS) & STATUS_LU))
  1116. phy_autoneg();
  1117. tx_ensure_enabled();
  1118. io_putstr("e1000e: netreset done\n");
  1119. }
  1120. const struct netdev e1000e_netdev = {
  1121. "e1000e", "Intel e1000e",
  1122. e1000e_init, e1000e_get_mac, e1000e_rx, e1000e_tx, e1000e_print_info,
  1123. e1000e_dump_regs, 0, e1000e_reset,
  1124. };