e1000e.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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. if (probe_bar0(bus, dev, func,
  357. &bar_base, &bar_size) != 0)
  358. continue;
  359. pci_bus = bus;
  360. pci_dev = dev;
  361. pci_func = func;
  362. device_id = (idf >> 16) & 0xFFFFu;
  363. detected_chip = chip_name(device_id);
  364. pch_lan = is_pch_lan(device_id);
  365. return 0;
  366. }
  367. }
  368. }
  369. return -1;
  370. }
  371. /* ---- MAC address ---- */
  372. /* Classic EERD word read; used only when RAL/RAH came back invalid. */
  373. static int eeprom_read16(unsigned int addr, unsigned short *out)
  374. {
  375. unsigned int t0, v;
  376. w32(REG_EERD, (addr << EERD_ADDR_SHIFT) | EERD_START);
  377. t0 = get_ticks_ms();
  378. for (;;) {
  379. v = r32(REG_EERD);
  380. if (v & EERD_DONE) {
  381. *out = (unsigned short)(v >> EERD_DATA_SHIFT);
  382. return 0;
  383. }
  384. if (timed_out(t0, 5))
  385. return -1;
  386. }
  387. }
  388. static int mac_valid(const unsigned char *m)
  389. {
  390. unsigned int i;
  391. int zero = 1, ones = 1;
  392. for (i = 0; i < 6; i++) {
  393. if (m[i] != 0x00)
  394. zero = 0;
  395. if (m[i] != 0xFF)
  396. ones = 0;
  397. }
  398. return !zero && !ones;
  399. }
  400. static void read_mac(void)
  401. {
  402. unsigned int ral = r32(REG_RAL0);
  403. unsigned int rah = r32(REG_RAH0);
  404. mac[0] = (unsigned char)(ral & 0xFF);
  405. mac[1] = (unsigned char)((ral >> 8) & 0xFF);
  406. mac[2] = (unsigned char)((ral >> 16) & 0xFF);
  407. mac[3] = (unsigned char)((ral >> 24) & 0xFF);
  408. mac[4] = (unsigned char)(rah & 0xFF);
  409. mac[5] = (unsigned char)((rah >> 8) & 0xFF);
  410. if (!mac_valid(mac)) {
  411. unsigned short w0 = 0, w1 = 0, w2 = 0;
  412. if (eeprom_read16(0, &w0) == 0 &&
  413. eeprom_read16(1, &w1) == 0 &&
  414. eeprom_read16(2, &w2) == 0) {
  415. mac[0] = (unsigned char)(w0 & 0xFF);
  416. mac[1] = (unsigned char)(w0 >> 8);
  417. mac[2] = (unsigned char)(w1 & 0xFF);
  418. mac[3] = (unsigned char)(w1 >> 8);
  419. mac[4] = (unsigned char)(w2 & 0xFF);
  420. mac[5] = (unsigned char)(w2 >> 8);
  421. }
  422. }
  423. /* Program the unicast filter (RAH first, then RAL on these parts).
  424. * Promiscuous mode is on as well for bring-up, but a valid RAL/RAH
  425. * keeps the hardware filter sane if UPE is ever turned off. */
  426. w32(REG_RAH0, ((unsigned int)mac[5] << 8) | mac[4]);
  427. w32(REG_RAL0, (unsigned int)mac[0] | ((unsigned int)mac[1] << 8) |
  428. ((unsigned int)mac[2] << 16) | ((unsigned int)mac[3] << 24));
  429. }
  430. /* ---- PHY access via MDIC ---- */
  431. static int mdic_read(unsigned int phy, unsigned int reg)
  432. {
  433. unsigned int t0, v;
  434. w32(REG_MDIC, (reg << MDIC_REG_SHIFT) | (phy << MDIC_PHY_SHIFT) |
  435. MDIC_OP_READ);
  436. t0 = get_ticks_ms();
  437. for (;;) {
  438. v = r32(REG_MDIC);
  439. if (v & MDIC_READY)
  440. return (v & MDIC_ERROR) ? -1 : (int)(v & MDIC_DATA_MASK);
  441. if (timed_out(t0, 10))
  442. return -1;
  443. }
  444. }
  445. static int mdic_write(unsigned int phy, unsigned int reg, unsigned int data)
  446. {
  447. unsigned int t0, v;
  448. w32(REG_MDIC, (data & MDIC_DATA_MASK) | (reg << MDIC_REG_SHIFT) |
  449. (phy << MDIC_PHY_SHIFT) | MDIC_OP_WRITE);
  450. t0 = get_ticks_ms();
  451. for (;;) {
  452. v = r32(REG_MDIC);
  453. if (v & MDIC_READY)
  454. return (v & MDIC_ERROR) ? -1 : 0;
  455. if (timed_out(t0, 10))
  456. return -1;
  457. }
  458. }
  459. static void phy_probe(void)
  460. {
  461. unsigned int phy;
  462. phy_addr = -1;
  463. phy_id1 = phy_id2 = -1;
  464. for (phy = 0; phy < 4; phy++) {
  465. int id1 = mdic_read(phy, MII_PHYID1);
  466. int id2 = mdic_read(phy, MII_PHYID2);
  467. if (id1 < 0 || id2 < 0)
  468. continue;
  469. if ((id1 & 0xFFFF) == 0 && (id2 & 0xFFFF) == 0)
  470. continue;
  471. if ((id1 & 0xFFFF) == 0xFFFF)
  472. continue;
  473. phy_addr = (int)phy;
  474. phy_id1 = id1;
  475. phy_id2 = id2;
  476. break;
  477. }
  478. }
  479. static void phy_autoneg(void)
  480. {
  481. if (phy_addr < 0)
  482. return;
  483. mdic_write(phy_addr, MII_ANAR, 0x01E1); /* 10/100 half+full */
  484. mdic_write(phy_addr, MII_GBSR, 0x0300); /* 1000 half+full */
  485. mdic_write(phy_addr, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
  486. }
  487. /* ---- hardware bring-up ---- */
  488. /* Quiesce the MAC without touching the PHY: mask interrupts, stop RX/TX and
  489. * let any in-flight DMA finish. This is the "reset" path on PCH LAN parts. */
  490. static void hw_stop(void)
  491. {
  492. w32(REG_IMC, 0xFFFFFFFFu);
  493. (void)r32(REG_ICR);
  494. w32(REG_RCTL, 0);
  495. w32(REG_TCTL, TCTL_PSP);
  496. (void)r32(REG_STATUS); /* flush posted writes */
  497. delay_ms(10);
  498. }
  499. /* PCH LAN with AMT: the firmware (ME) must be told that the OS driver owns
  500. * the network interface, otherwise it can keep the LAN - notably the TX
  501. * path - to itself. e1000e's e1000e_get_hw_control() sets
  502. * CTRL_EXT.DRV_LOAD for parts with FLAG_HAS_CTRLEXT_ON_LOAD (the pch_info
  503. * used by the 82577LM/82578/82579). Do it right after the MAC reset, before
  504. * the rings are programmed, exactly where e1000e calls it. */
  505. static void pch_claim(void)
  506. {
  507. unsigned int ctrl_ext;
  508. if (!pch_lan)
  509. return;
  510. ctrl_ext = r32(REG_CTRL_EXT);
  511. if (!(ctrl_ext & 0x10000000u)) {
  512. w32(REG_CTRL_EXT, ctrl_ext | 0x10000000u);
  513. dbg("AMT handshake: CTRL_EXT.DRV_LOAD set");
  514. }
  515. /* e1000e_reset(): clear wake-up control and enable VLAN EtherType.
  516. * PBA is only read/reported: writing the generic PCH default of 26
  517. * would set the TX half to zero on split-PBA parts (verified on
  518. * QEMU's 82540: 0x00100030 -> 0x0000001A). */
  519. w32(REG_WUC, 0);
  520. w32(REG_VET, 0x8100);
  521. serial_puts(SERIAL_COM1, "e1000e: pba ");
  522. serial_puthex(SERIAL_COM1, r32(REG_PBA));
  523. serial_puts(SERIAL_COM1, "\n");
  524. }
  525. static int hw_reset(void)
  526. {
  527. unsigned int t0, ctrl;
  528. hw_stop();
  529. /* Block new DMA requests and wait for the last TLP to finish before
  530. * hitting the MAC with a global reset (same order as e1000e). */
  531. ctrl = r32(REG_CTRL);
  532. w32(REG_CTRL, ctrl | CTRL_GIO_MASTER_DISABLE);
  533. t0 = get_ticks_ms();
  534. while ((r32(REG_STATUS) & STATUS_GIO_MASTER_ENABLE) &&
  535. !timed_out(t0, 1000))
  536. ;
  537. w32(REG_CTRL, r32(REG_CTRL) | CTRL_RST);
  538. t0 = get_ticks_ms();
  539. while (r32(REG_CTRL) & CTRL_RST) {
  540. if (timed_out(t0, 1000))
  541. return -1;
  542. }
  543. delay_ms(20); /* NVM auto-read after reset */
  544. /* Wait for the NVM auto-read to finish and clear the done bit. */
  545. t0 = get_ticks_ms();
  546. while (!(r32(REG_STATUS) & STATUS_LAN_INIT_DONE) &&
  547. !timed_out(t0, 200))
  548. ;
  549. w32(REG_STATUS, r32(REG_STATUS) & ~STATUS_LAN_INIT_DONE);
  550. reset_count++;
  551. return 0;
  552. }
  553. static void setup_rings(void)
  554. {
  555. unsigned int i;
  556. for (i = 0; i < E1000E_NRX; i++) {
  557. rx_ring[i].addr_lo = (unsigned int)(rx_bufs + i * E1000E_BUF);
  558. rx_ring[i].addr_hi = 0;
  559. rx_ring[i].length = 0;
  560. rx_ring[i].csum = 0;
  561. rx_ring[i].status = 0;
  562. rx_ring[i].errors = 0;
  563. rx_ring[i].special = 0;
  564. }
  565. for (i = 0; i < E1000E_NTX; i++) {
  566. tx_ring[i].addr_lo = (unsigned int)(tx_bufs + i * E1000E_BUF);
  567. tx_ring[i].addr_hi = 0;
  568. tx_ring[i].lower = 0;
  569. tx_ring[i].upper = 0;
  570. }
  571. __sync_synchronize();
  572. w32(REG_RDBAL, (unsigned int)rx_ring);
  573. w32(REG_RDBAH, 0);
  574. w32(REG_RDLEN, E1000E_NRX * (unsigned int)sizeof(struct e1000e_rx_desc));
  575. w32(REG_RDH, 0);
  576. w32(REG_RDT, E1000E_NRX - 1);
  577. w32(REG_TDBAL, (unsigned int)tx_ring);
  578. w32(REG_TDBAH, 0);
  579. w32(REG_TDLEN, E1000E_NTX * (unsigned int)sizeof(struct e1000e_tx_desc));
  580. w32(REG_TDH, 0);
  581. w32(REG_TDT, 0);
  582. rx_cur = 0;
  583. tx_cur = 0;
  584. tx_pending = 0;
  585. tx_fail_run = 0;
  586. tx_broken = 0;
  587. }
  588. /* PCH LAN: program the TX descriptor write-back policy and arbitration bits
  589. * that e1000e's ich8lan init sets. Without WTHRESH/COUNT_DESC the 82577 was
  590. * observed to never report TX descriptor completion (tx_ok stayed 0), while
  591. * RX worked. */
  592. static void tx_desc_config(void)
  593. {
  594. unsigned int v, gcr;
  595. /* Enable snooping for TX/RX DMA (see GCR_NO_SNOOP). */
  596. gcr = r32(REG_GCR);
  597. w32(REG_GCR, gcr & ~GCR_NO_SNOOP);
  598. serial_puts(SERIAL_COM1, "e1000e: gcr ");
  599. serial_puthex(SERIAL_COM1, gcr);
  600. serial_puts(SERIAL_COM1, " -> ");
  601. serial_puthex(SERIAL_COM1, gcr & ~GCR_NO_SNOOP);
  602. serial_puts(SERIAL_COM1, "\n");
  603. /* CTRL_EXT bits e1000e's ich8 init sets: relaxed-ordering disable,
  604. * the always-set bit 22, and PHY power-down in D3. */
  605. v = r32(REG_CTRL_EXT);
  606. v |= 0x00020000u | 0x00400000u | 0x00100000u;
  607. w32(REG_CTRL_EXT, v);
  608. v = r32(REG_TXDCTL0);
  609. v |= TXDCTL_COUNT_DESC;
  610. v = (v & ~TXDCTL_WTHRESH) | TXDCTL_WB_ONE;
  611. v = (v & ~TXDCTL_PTHRESH) | TXDCTL_PREFETCH;
  612. v |= TXDCTL_HTHRESH;
  613. w32(REG_TXDCTL0, v);
  614. v = r32(REG_TXDCTL1);
  615. v |= TXDCTL_COUNT_DESC;
  616. v = (v & ~TXDCTL_WTHRESH) | TXDCTL_WB_ONE;
  617. v = (v & ~TXDCTL_PTHRESH) | TXDCTL_PREFETCH;
  618. v |= TXDCTL_HTHRESH;
  619. w32(REG_TXDCTL1, v);
  620. /* TARC queue arbitration workarounds from the PCH init. */
  621. w32(REG_TARC0, r32(REG_TARC0) | 0x0D800000u);
  622. w32(REG_TARC1, r32(REG_TARC1) | 0x51000000u);
  623. }
  624. static void hw_start(void)
  625. {
  626. setup_rings();
  627. w32(REG_RCTL, RCTL_EN | RCTL_UPE | RCTL_MPE | RCTL_BAM | RCTL_SECRC);
  628. if (pch_lan)
  629. tx_desc_config();
  630. w32(REG_TCTL, TCTL_EN | TCTL_PSP | TCTL_RTLC |
  631. (TCTL_CT << TCTL_CT_SHIFT) |
  632. (TCTL_COLD << TCTL_COLD_SHIFT));
  633. w32(REG_TIPG, 10u | (8u << 10) | (6u << 20));
  634. __sync_synchronize();
  635. }
  636. /* ---- netdev interface ---- */
  637. static int e1000e_init(void)
  638. {
  639. char buf[64];
  640. unsigned int i;
  641. if (inited)
  642. return present ? 0 : -1;
  643. inited = 1;
  644. if (probe_pci() != 0) {
  645. dbg("no supported Intel NIC");
  646. return -1;
  647. }
  648. process_map_mmio_all(bar_base, bar_size);
  649. mmio = (unsigned char *)(unsigned long)bar_base;
  650. if (r32(REG_STATUS) == 0xFFFFFFFFu) {
  651. dbg("MMIO read failed");
  652. mmio = 0;
  653. return -1;
  654. }
  655. i = 0;
  656. /* "PCI 00:19.0 f2500000 128K" */
  657. {
  658. static const char hx[] = "0123456789ABCDEF";
  659. unsigned int n = 0;
  660. buf[i++] = 'P'; buf[i++] = 'C'; buf[i++] = 'I'; buf[i++] = ' ';
  661. for (n = 0; n < 2; n++)
  662. buf[i++] = hx[(pci_bus >> (4 * (1 - n))) & 0xF];
  663. buf[i++] = ':';
  664. for (n = 0; n < 2; n++)
  665. buf[i++] = hx[(pci_dev >> (4 * (1 - n))) & 0xF];
  666. buf[i++] = '.';
  667. buf[i++] = (char)('0' + pci_func);
  668. buf[i++] = ' ';
  669. for (n = 0; n < 8; n++)
  670. buf[i++] = hx[(bar_base >> (4 * (7 - n))) & 0xF];
  671. buf[i++] = ' ';
  672. buf[i] = '\0';
  673. }
  674. dbg(buf);
  675. /* MAC-only reset: CTRL.PHY_RST is deliberately never set, so the
  676. * firmware's PHY/Kumeran setup (and the link) survives, while the MAC
  677. * registers - including whatever TX queue state the BIOS UNDI left
  678. * behind - go back to their defaults. e1000e does the same on parts
  679. * where the ME owns the PHY. */
  680. if (hw_reset() != 0) {
  681. dbg("MAC reset timeout");
  682. mmio = 0;
  683. return -1;
  684. }
  685. dbg("reset ok");
  686. pch_claim();
  687. read_mac();
  688. /* All ring and buffer memory in one allocation: the page allocator is
  689. * IRQ-safe but not cross-CPU locked, so two separate calls can (and
  690. * once did) hand out the same page under boot-time concurrency. A
  691. * single contiguous run cannot alias itself. */
  692. {
  693. unsigned int pages = 2 +
  694. (E1000E_NRX * E1000E_BUF) / PAGE_SIZE +
  695. (E1000E_NTX * E1000E_BUF) / PAGE_SIZE;
  696. unsigned int mem = mm_alloc_pages(pages);
  697. if (mem) {
  698. rx_ring = (struct e1000e_rx_desc *)mem;
  699. tx_ring = (struct e1000e_tx_desc *)(mem + PAGE_SIZE);
  700. rx_bufs = (unsigned char *)(mem + 2 * PAGE_SIZE);
  701. tx_bufs = rx_bufs + E1000E_NRX * E1000E_BUF;
  702. }
  703. }
  704. serial_puts(SERIAL_COM1, "e1000e: ring mem rx=");
  705. serial_puthex(SERIAL_COM1, (unsigned int)rx_ring);
  706. serial_puts(SERIAL_COM1, " tx=");
  707. serial_puthex(SERIAL_COM1, (unsigned int)tx_ring);
  708. serial_puts(SERIAL_COM1, " rxb=");
  709. serial_puthex(SERIAL_COM1, (unsigned int)rx_bufs);
  710. serial_puts(SERIAL_COM1, " txb=");
  711. serial_puthex(SERIAL_COM1, (unsigned int)tx_bufs);
  712. serial_puts(SERIAL_COM1, "\n");
  713. if (!rx_ring || !tx_ring || !rx_bufs || !tx_bufs) {
  714. dbg("no memory for rings");
  715. mmio = 0;
  716. return -1;
  717. }
  718. phy_probe();
  719. /* The 82577 PHY is at MDIC address 1 even when the probe could not
  720. * read its ID (the PCH needs a slow-MDIO mode we do not program); keep
  721. * the address so a link-down autoneg attempt can still go out. */
  722. if (pch_lan && phy_addr < 0)
  723. phy_addr = 1;
  724. hw_start();
  725. {
  726. unsigned int st = r32(REG_STATUS);
  727. dbg(st & STATUS_LU ? "link up" : "link started");
  728. }
  729. present = 1;
  730. /* "<chip> <mac>" */
  731. i = 0;
  732. for (const char *p = detected_chip; *p && i + 1 < sizeof(buf); p++)
  733. buf[i++] = *p;
  734. buf[i++] = ' ';
  735. for (unsigned int j = 0; j < 6 && i + 4 < sizeof(buf); j++) {
  736. static const char hx[] = "0123456789ABCDEF";
  737. buf[i++] = hx[(mac[j] >> 4) & 0xF];
  738. buf[i++] = hx[mac[j] & 0xF];
  739. if (j != 5)
  740. buf[i++] = ':';
  741. }
  742. buf[i] = '\0';
  743. dbg(buf);
  744. if (!(r32(REG_STATUS) & STATUS_LU))
  745. phy_autoneg();
  746. tx_ensure_enabled();
  747. return 0;
  748. }
  749. static void e1000e_get_mac(unsigned char out[6])
  750. {
  751. unsigned int i;
  752. for (i = 0; i < 6; i++)
  753. out[i] = mac[i];
  754. }
  755. static int e1000e_rx(unsigned char *buf, unsigned int maxlen)
  756. {
  757. unsigned int guard = 0;
  758. if (!present || !mmio)
  759. return -1;
  760. while (guard++ < 8) {
  761. unsigned int entry = rx_cur;
  762. struct e1000e_rx_desc *d = &rx_ring[entry];
  763. unsigned int status = d->status;
  764. unsigned int len;
  765. if (!(status & RX_DD))
  766. return -1; /* nothing pending */
  767. __sync_synchronize();
  768. last_rx_status = status;
  769. last_rx_errors = d->errors;
  770. if ((status & RX_EOP) && d->errors == 0) {
  771. len = d->length;
  772. if (len >= 14 && len <= maxlen) {
  773. unsigned char *src = rx_bufs +
  774. entry * E1000E_BUF;
  775. unsigned int i;
  776. for (i = 0; i < len; i++)
  777. buf[i] = src[i];
  778. rx_ok++;
  779. d->status = 0;
  780. __sync_synchronize();
  781. w32(REG_RDT, entry);
  782. rx_cur = (entry + 1) % E1000E_NRX;
  783. return (int)len;
  784. }
  785. rx_drop++;
  786. } else {
  787. rx_err++;
  788. }
  789. /* Drop this descriptor and give it back to the hardware. */
  790. d->status = 0;
  791. __sync_synchronize();
  792. w32(REG_RDT, entry);
  793. rx_cur = (entry + 1) % E1000E_NRX;
  794. }
  795. return -1;
  796. }
  797. static int tx_timeout(void)
  798. {
  799. tx_err++;
  800. if (++tx_fail_run >= E1000E_TX_FAIL_MAX)
  801. tx_broken = 1;
  802. return -1;
  803. }
  804. /* The TX engine is not moving (TDH stuck): toggle TCTL.EN and re-arm the
  805. * ring. Any descriptor the hardware never retired is simply dropped; the
  806. * network stack retries the frame. This also makes the next TDT write a
  807. * real 0 -> 1 transition again, in case the first doorbell was lost while
  808. * the queue was disabled. */
  809. static void tx_queue_restart(void)
  810. {
  811. unsigned int tctl = r32(REG_TCTL);
  812. w32(REG_TCTL, tctl & ~TCTL_EN);
  813. delay_ms(1);
  814. w32(REG_TDBAL, (unsigned int)tx_ring);
  815. w32(REG_TDBAH, 0);
  816. w32(REG_TDLEN, (unsigned int)(E1000E_NTX *
  817. sizeof(struct e1000e_tx_desc)));
  818. w32(REG_TDH, 0);
  819. w32(REG_TDT, 0);
  820. w32(REG_TCTL, tctl | TCTL_EN);
  821. __sync_synchronize();
  822. tx_cur = 0;
  823. tx_pending = 0;
  824. }
  825. /* The MAC may clear TCTL.EN on a link transition; make sure it is set before
  826. * a send. Returns 1 when it had to be re-enabled. */
  827. static int tx_ensure_enabled(void)
  828. {
  829. unsigned int tctl = r32(REG_TCTL);
  830. if (tctl & TCTL_EN)
  831. return 0;
  832. w32(REG_TCTL, tctl | TCTL_EN);
  833. tctl_fixes++;
  834. return 1;
  835. }
  836. static int e1000e_tx(const unsigned char *buf, unsigned int len)
  837. {
  838. static unsigned char pad[60];
  839. struct e1000e_tx_desc *d;
  840. unsigned int entry, t0;
  841. const unsigned char *p = buf;
  842. unsigned int txlen = len;
  843. if (!present || !mmio)
  844. return -1;
  845. /* Once TX has failed repeatedly, fail instantly: the netlog flush and
  846. * the DHCP/ping loops must not hold the BKL for 100 ms per frame on a
  847. * NIC whose TX does not complete. `netreset` clears this. */
  848. if (tx_broken)
  849. return -1;
  850. if (len < 14 || len > 1514)
  851. return -1;
  852. tx_ensure_enabled();
  853. if (len < 60) {
  854. unsigned int i;
  855. for (i = 0; i < len; i++)
  856. pad[i] = buf[i];
  857. for (i = len; i < 60; i++)
  858. pad[i] = 0;
  859. p = pad;
  860. txlen = 60;
  861. }
  862. entry = tx_cur;
  863. d = &tx_ring[entry];
  864. /* A previous command on this slot that timed out may still be in
  865. * flight; wait for it before reuse. */
  866. if (tx_pending & (1u << entry)) {
  867. t0 = get_ticks_ms();
  868. while (!(d->upper & TX_DD)) {
  869. if (timed_out(t0, E1000E_TX_TIMEOUT_MS)) {
  870. /* The hardware never retired this
  871. * descriptor: restart the TX queue and
  872. * drop the slot. */
  873. tx_queue_restart();
  874. return tx_timeout();
  875. }
  876. }
  877. tx_pending &= ~(1u << entry);
  878. }
  879. {
  880. unsigned char *dst = tx_bufs + entry * E1000E_BUF;
  881. unsigned int i;
  882. for (i = 0; i < txlen; i++)
  883. dst[i] = p[i];
  884. }
  885. d->addr_lo = (unsigned int)(tx_bufs + entry * E1000E_BUF);
  886. d->addr_hi = 0;
  887. d->lower = txlen | (TX_CMD << 24);
  888. d->upper = 0;
  889. __sync_synchronize();
  890. tx_pending |= 1u << entry;
  891. w32(REG_TDT, (entry + 1) % E1000E_NTX);
  892. t0 = get_ticks_ms();
  893. while (!(d->upper & TX_DD)) {
  894. if (timed_out(t0, E1000E_TX_TIMEOUT_MS)) {
  895. /* Leave the pending bit set: the next TX on this slot
  896. * waits for it before reuse. */
  897. return tx_timeout();
  898. }
  899. }
  900. last_tx_status = d->upper;
  901. tx_pending &= ~(1u << entry);
  902. tx_fail_run = 0;
  903. tx_ok++;
  904. tx_cur = (entry + 1) % E1000E_NTX;
  905. return 0;
  906. }
  907. static void e1000e_print_info(void)
  908. {
  909. unsigned int st, bmcr = 0, bmsr = 0;
  910. if (!present || !mmio) {
  911. io_putstr("e1000e: no NIC\n");
  912. return;
  913. }
  914. st = r32(REG_STATUS);
  915. io_putstr("chip: ");
  916. io_putstr(detected_chip);
  917. io_putstr("\npci ");
  918. put_dec(pci_bus); io_putchar(':');
  919. put_dec(pci_dev); io_putchar('.');
  920. put_dec(pci_func);
  921. io_putstr(" bar0=0x");
  922. {
  923. static const char hx[] = "0123456789ABCDEF";
  924. unsigned int i;
  925. for (i = 0; i < 8; i++)
  926. io_putchar(hx[(bar_base >> (4 * (7 - i))) & 0xF]);
  927. }
  928. io_putstr(" mac ");
  929. put_mac(mac);
  930. io_putstr("\nlink ");
  931. if (st & STATUS_LU) {
  932. io_putstr((st & STATUS_FD) ? "up full" : "up half");
  933. io_putstr(" ");
  934. switch (st & STATUS_SPEED) {
  935. case 0x00: io_putstr("10M"); break;
  936. case 0x40: io_putstr("100M"); break;
  937. case 0x80: io_putstr("1000M"); break;
  938. default: io_putstr("?M"); break;
  939. }
  940. } else {
  941. io_putstr("down");
  942. }
  943. io_putstr(" status=0x");
  944. io_puthex(st);
  945. io_putstr("\nphy ");
  946. if (phy_addr >= 0) {
  947. put_dec((unsigned int)phy_addr);
  948. io_putstr(" id ");
  949. put_hex2((unsigned int)(phy_id1 >> 8) & 0xFF);
  950. put_hex2((unsigned int)phy_id1 & 0xFF);
  951. io_putchar(':');
  952. put_hex2((unsigned int)(phy_id2 >> 8) & 0xFF);
  953. put_hex2((unsigned int)phy_id2 & 0xFF);
  954. bmcr = (unsigned int)mdic_read((unsigned int)phy_addr, MII_BMCR);
  955. bmsr = (unsigned int)mdic_read((unsigned int)phy_addr, MII_BMSR);
  956. io_putstr(" bmcr=0x");
  957. io_puthex(bmcr);
  958. io_putstr(" bmsr=0x");
  959. io_puthex(bmsr);
  960. } else {
  961. io_putstr("none");
  962. }
  963. io_putstr("txdctl=0x");
  964. io_puthex(r32(REG_TXDCTL0));
  965. io_putstr(" tarc=0x");
  966. io_puthex(r32(REG_TARC0));
  967. io_putstr(" gcr=0x");
  968. io_puthex(r32(REG_GCR));
  969. io_putstr(" pba=0x");
  970. io_puthex(r32(REG_PBA));
  971. io_putstr(" tdh=0x");
  972. io_puthex(r32(REG_TDH));
  973. io_putstr(" tdt=0x");
  974. io_puthex(r32(REG_TDT));
  975. io_putchar('\n');
  976. io_putstr("rx ok ");
  977. put_dec(rx_ok);
  978. io_putstr(" err ");
  979. put_dec(rx_err);
  980. io_putstr(" drop ");
  981. put_dec(rx_drop);
  982. io_putstr(" tx ok ");
  983. put_dec(tx_ok);
  984. io_putstr(" err ");
  985. put_dec(tx_err);
  986. io_putstr(" broken ");
  987. put_dec((unsigned int)tx_broken);
  988. io_putstr(" tctl_fix ");
  989. put_dec(tctl_fixes);
  990. io_putstr(" resets ");
  991. put_dec(reset_count);
  992. io_putchar('\n');
  993. }
  994. static void e1000e_dump_regs(void)
  995. {
  996. static const struct {
  997. const char *name;
  998. unsigned int reg;
  999. } regs[] = {
  1000. { "CTRL", REG_CTRL }, { "STATUS", REG_STATUS },
  1001. { "CTRL_EXT", REG_CTRL_EXT }, { "EERD", REG_EERD },
  1002. { "MDIC", REG_MDIC }, { "ICR", REG_ICR },
  1003. { "PBA", REG_PBA }, { "WUC", REG_WUC },
  1004. { "RCTL", REG_RCTL }, { "TCTL", REG_TCTL },
  1005. { "TIPG", REG_TIPG }, { "RAL0", REG_RAL0 },
  1006. { "RAH0", REG_RAH0 }, { "RDBAL", REG_RDBAL },
  1007. { "RDLEN", REG_RDLEN }, { "RDH", REG_RDH },
  1008. { "RDT", REG_RDT }, { "TDBAL", REG_TDBAL },
  1009. { "TDLEN", REG_TDLEN }, { "TDH", REG_TDH },
  1010. { "TDT", REG_TDT }, { "TXDCTL0", REG_TXDCTL0 },
  1011. { "TXDCTL1", REG_TXDCTL1 }, { "TARC0", REG_TARC0 },
  1012. { "TARC1", REG_TARC1 }, { "GCR", REG_GCR },
  1013. };
  1014. unsigned int i;
  1015. if (!present || !mmio) {
  1016. io_putstr("e1000e: no NIC\n");
  1017. return;
  1018. }
  1019. for (i = 0; i < sizeof(regs) / sizeof(regs[0]); i++) {
  1020. io_putstr(regs[i].name);
  1021. io_putchar('=');
  1022. io_puthex(r32(regs[i].reg));
  1023. if ((i & 1) == 1)
  1024. io_putchar('\n');
  1025. else
  1026. io_putstr(" ");
  1027. }
  1028. if (i & 1)
  1029. io_putchar('\n');
  1030. io_putstr("rings rx=0x");
  1031. io_puthex((unsigned int)rx_ring);
  1032. io_putstr(" tx=0x");
  1033. io_puthex((unsigned int)tx_ring);
  1034. io_putstr(" rx_cur=");
  1035. put_dec(rx_cur);
  1036. io_putstr(" tx_cur=");
  1037. put_dec(tx_cur);
  1038. io_putstr(" tx_pending=0x");
  1039. io_puthex(tx_pending);
  1040. io_putchar('\n');
  1041. io_putstr("state rx_ok=");
  1042. put_dec(rx_ok);
  1043. io_putstr(" rx_err=");
  1044. put_dec(rx_err);
  1045. io_putstr(" rx_drop=");
  1046. put_dec(rx_drop);
  1047. io_putstr(" tx_ok=");
  1048. put_dec(tx_ok);
  1049. io_putstr(" tx_err=");
  1050. put_dec(tx_err);
  1051. io_putstr(" tx_broken=");
  1052. put_dec((unsigned int)tx_broken);
  1053. io_putstr(" tx_fail_run=");
  1054. put_dec(tx_fail_run);
  1055. io_putstr(" tctl_fix=");
  1056. put_dec(tctl_fixes);
  1057. io_putstr(" reset=");
  1058. put_dec(reset_count);
  1059. io_putchar('\n');
  1060. io_putstr("last rx status=0x");
  1061. io_puthex(last_rx_status);
  1062. io_putstr(" errors=0x");
  1063. io_puthex(last_rx_errors);
  1064. io_putstr(" tx status=0x");
  1065. io_puthex(last_tx_status);
  1066. io_putchar('\n');
  1067. if (tx_ring) {
  1068. io_putstr("tx0 addr=0x");
  1069. io_puthex(tx_ring[0].addr_lo);
  1070. io_putstr(" lower=0x");
  1071. io_puthex(tx_ring[0].lower);
  1072. io_putstr(" upper=0x");
  1073. io_puthex(tx_ring[0].upper);
  1074. io_putchar('\n');
  1075. }
  1076. }
  1077. static void e1000e_reset(void)
  1078. {
  1079. if (!present || !mmio || !rx_ring) {
  1080. io_putstr("e1000e: reset: not initialized\n");
  1081. return;
  1082. }
  1083. if (hw_reset() != 0) {
  1084. io_putstr("e1000e: reset timeout\n");
  1085. return;
  1086. }
  1087. pch_claim();
  1088. read_mac();
  1089. hw_start();
  1090. if (!(r32(REG_STATUS) & STATUS_LU))
  1091. phy_autoneg();
  1092. tx_ensure_enabled();
  1093. io_putstr("e1000e: netreset done\n");
  1094. }
  1095. const struct netdev e1000e_netdev = {
  1096. "e1000e", "Intel e1000e",
  1097. e1000e_init, e1000e_get_mac, e1000e_rx, e1000e_tx, e1000e_print_info,
  1098. e1000e_dump_regs, 0, e1000e_reset,
  1099. };