e1000e.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  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_PBS 0x01008
  33. #define REG_WUC 0x05800
  34. #define REG_VET 0x00038
  35. #define REG_ICR 0x000C0
  36. #define REG_IMC 0x000D8
  37. #define REG_RCTL 0x00100
  38. #define REG_TCTL 0x00400
  39. #define REG_TIPG 0x00410
  40. #define REG_RDBAL 0x02800
  41. #define REG_RDBAH 0x02804
  42. #define REG_RDLEN 0x02808
  43. #define REG_RDH 0x02810
  44. #define REG_RDT 0x02818
  45. #define REG_RDTR 0x02820
  46. #define REG_RADV 0x0282C
  47. #define REG_TDBAL 0x03800
  48. #define REG_TDBAH 0x03804
  49. #define REG_TDLEN 0x03808
  50. #define REG_TDH 0x03810
  51. #define REG_TDT 0x03818
  52. #define REG_TXDCTL0 0x03828
  53. #define REG_TXDCTL1 0x03928
  54. #define REG_TARC0 0x03840
  55. #define REG_TARC1 0x03940
  56. #define REG_RAL0 0x05400
  57. #define REG_RAH0 0x05404
  58. #define REG_EXTCNF_CTRL 0x00F00
  59. #define REG_KMRNCTRLSTA 0x00034
  60. #define REG_KABGTXD 0x03004
  61. #define REG_RXCSUM 0x05000
  62. #define REG_RFCTL 0x05008
  63. #define REG_RXDCTL0 0x02828
  64. #define REG_RXDCTL1 0x02928
  65. #define REG_FWSM 0x05B54
  66. #define REG_CRC_OFFSET 0x05F50
  67. #define REG_GCR 0x05B00
  68. /* Hardware statistics counters (read-clear). These are the ones that tell
  69. * whether the PHY/link is dropping frames: CRC/alignment errors, missed
  70. * packets (RX FIFO full) and the broadcast counters DHCP/ARP depend on. */
  71. #define REG_CRCERRS 0x04000
  72. #define REG_RXERRC 0x0400C
  73. #define REG_MPC 0x04010
  74. #define REG_GPRC 0x04064
  75. #define REG_BPRC 0x04068
  76. #define REG_MPRC 0x0406C
  77. #define REG_GPTC 0x04070
  78. #define REG_RUC 0x04088
  79. #define REG_BPTC 0x040F4
  80. /* GCR no-snoop enables: 1 = the NIC bypasses the CPU cache for that DMA
  81. * type. A firmware that leaves the TX bits set makes the NIC read freshly
  82. * written TX descriptors from stale DRAM, so TX never completes (RX still
  83. * works because its descriptor lines were evicted long before the traffic).
  84. * Linux clears these on every ich8/pch part. */
  85. #define GCR_NO_SNOOP 0x0000003Fu
  86. /* ---- CTRL ---- */
  87. #define CTRL_SLU 0x00000040u
  88. #define CTRL_RST 0x04000000u
  89. #define CTRL_GIO_MASTER_DISABLE 0x00000004u
  90. #define CTRL_FRCSPD 0x00000800u
  91. #define CTRL_FRCDPX 0x00001000u
  92. /* ---- STATUS ---- */
  93. #define STATUS_FD 0x00000001u
  94. #define STATUS_LU 0x00000002u
  95. #define STATUS_SPEED 0x000000C0u
  96. #define STATUS_LAN_INIT_DONE 0x00000200u
  97. #define STATUS_PHYRA 0x00000400u
  98. #define STATUS_GIO_MASTER_ENABLE 0x00080000u
  99. /* ---- RCTL ---- */
  100. #define RCTL_EN 0x00000002u
  101. #define RCTL_UPE 0x00000008u
  102. #define RCTL_MPE 0x00000010u
  103. #define RCTL_LPE 0x00000020u
  104. #define RCTL_BAM 0x00008000u
  105. #define RCTL_SECRC 0x04000000u
  106. #define RCTL_RDMTS_HALF 0x00000100u
  107. /* ---- RXCSUM ---- */
  108. #define RXCSUM_TUOFL 0x00000200u
  109. #define RXCSUM_IPPCSE 0x00001000u
  110. /* ---- RFCTL ---- */
  111. #define RFCTL_NFSW_DIS 0x00000040u
  112. #define RFCTL_NFSR_DIS 0x00000080u
  113. #define RFCTL_EXTEN 0x00008000u
  114. #define RFCTL_IPV6_EX_DIS 0x00010000u
  115. #define RFCTL_NEW_IPV6_EXT_DIS 0x00020000u
  116. /* ---- TCTL ---- */
  117. #define TCTL_EN 0x00000002u
  118. #define TCTL_PSP 0x00000008u
  119. #define TCTL_RTLC 0x01000000u
  120. #define TCTL_CT_SHIFT 4
  121. #define TCTL_COLD_SHIFT 12
  122. #define TCTL_CT 0x0Fu
  123. #define TCTL_COLD 0x3Fu
  124. #define TCTL_CT_MASK 0x00000FF0u
  125. #define TCTL_COLD_MASK 0x003FF000u
  126. /* ---- TXDCTL (PCH LAN: descriptor write-back policy) ---- */
  127. #define TXDCTL_PTHRESH 0x0000003Fu
  128. #define TXDCTL_WTHRESH 0x003F0000u
  129. #define TXDCTL_COUNT_DESC 0x00400000u
  130. #define TXDCTL_GRAN 0x01000000u
  131. #define TXDCTL_WB_ONE 0x01010000u /* GRAN | WTHRESH=1 */
  132. #define TXDCTL_PREFETCH 0x0100001Fu /* GRAN | PTHRESH=31 */
  133. #define TXDCTL_HTHRESH 0x00000100u /* host threshold = 1 */
  134. /* ---- RXDCTL (PCH LAN: descriptor write-back policy) ---- */
  135. #define RXDCTL_PTHRESH 0x0000003Fu
  136. #define RXDCTL_HTHRESH 0x00003F00u
  137. #define RXDCTL_WTHRESH 0x003F0000u
  138. #define RXDCTL_GRAN 0x01000000u
  139. /* ---- MDIC ---- */
  140. #define MDIC_TIMEOUT_MS 100 /* Linux polls up to ~96 ms; slow MDIO needs it */
  141. #define MDIC_DATA_MASK 0x0000FFFFu
  142. #define MDIC_REG_SHIFT 16
  143. #define MDIC_PHY_SHIFT 21
  144. #define MDIC_OP_WRITE 0x04000000u
  145. #define MDIC_OP_READ 0x08000000u
  146. #define MDIC_READY 0x10000000u
  147. #define MDIC_ERROR 0x40000000u
  148. /* ---- EERD ---- */
  149. #define EERD_START 0x00000001u
  150. #define EERD_DONE 0x00000010u
  151. #define EERD_ADDR_SHIFT 8
  152. #define EERD_DATA_SHIFT 16
  153. /* ---- descriptor status ---- */
  154. #define RX_DD 0x01
  155. #define RX_EOP 0x02
  156. #define TX_DD 0x01
  157. #define TX_CMD (0x01u | 0x02u | 0x08u) /* EOP | IFCS | RS */
  158. /* ---- rings ---- */
  159. #define E1000E_NRX 64
  160. #define E1000E_NTX 16
  161. #define E1000E_BUF 2048
  162. #define E1000E_TX_TIMEOUT_MS 20
  163. #define E1000E_TX_FAIL_MAX 32
  164. /* ---- PHY / MII ---- */
  165. #define MII_BMCR 0
  166. #define MII_BMSR 1
  167. #define MII_PHYID1 2
  168. #define MII_PHYID2 3
  169. #define MII_ANAR 4
  170. #define MII_GBSR 9
  171. #define BMCR_ANENABLE 0x1000
  172. #define BMCR_ANRESTART 0x0200
  173. /* ---- PCI ---- */
  174. #define PCI_CMD_INT_DISABLE 0x0400u /* command bit 10: INTx disabled */
  175. #define E1000E_VENDOR 0x8086u
  176. /* Extended RX descriptor (16 bytes), the layout the e1000e/PCH parts use
  177. * when RFCTL.EXTEN is set: the hardware overwrites the buffer address with
  178. * MRQ/RSS data and writes status/error, length and VLAN tag back at offsets
  179. * 8, 12 and 14. The legacy 16-byte layout puts status at 12 instead. */
  180. struct e1000e_rx_desc {
  181. volatile unsigned int addr_lo;
  182. volatile unsigned int addr_hi;
  183. volatile unsigned int status_error;
  184. volatile unsigned short length;
  185. volatile unsigned short vlan;
  186. } __attribute__((packed));
  187. struct e1000e_tx_desc {
  188. volatile unsigned int addr_lo;
  189. volatile unsigned int addr_hi;
  190. volatile unsigned int lower;
  191. volatile unsigned int upper;
  192. } __attribute__((packed));
  193. static unsigned char *mmio;
  194. static int present;
  195. static int inited;
  196. static unsigned char mac[6];
  197. static unsigned int bar_base, bar_size;
  198. static unsigned int pci_bus, pci_dev, pci_func;
  199. static unsigned int device_id;
  200. static const char *detected_chip = "Intel e1000e";
  201. static struct e1000e_rx_desc *rx_ring;
  202. static struct e1000e_tx_desc *tx_ring;
  203. static unsigned char *rx_bufs;
  204. static unsigned char *tx_bufs;
  205. static unsigned int rx_cur;
  206. static unsigned int tx_cur;
  207. static unsigned int tx_pending; /* bitmask: submitted, DD not seen yet */
  208. static unsigned int tx_fail_run; /* consecutive TX timeouts */
  209. static int tx_broken; /* stop stalling the system on a dead TX */
  210. static unsigned int tctl_fixes; /* TCTL.EN re-asserts */
  211. static int last_link; /* last STATUS.LU sampled, PCH only */
  212. static unsigned int link_poll_ms;
  213. static int tx_timeout_logged;
  214. static void tx_queue_restart(void);
  215. static int tx_ensure_enabled(void);
  216. static void phy_autoneg(void);
  217. static int pch_swflag_acquire(void);
  218. static void pch_swflag_release(void);
  219. static int phy_addr = -1;
  220. static int phy_id1 = -1, phy_id2 = -1;
  221. static int pch_lan; /* 82577/82578/82579: PCH LAN/Kumeran PHY */
  222. static int force_speed; /* 0 = auto, 100 = advertise 100M only */
  223. /* Counters (also reported by `netregs`). */
  224. static unsigned int tx_ok, tx_err, rx_ok, rx_err, rx_drop;
  225. static unsigned int rx_ucast, rx_bcast, rx_mcast;
  226. static unsigned char last_rx_dst[6];
  227. static unsigned int last_rx_len;
  228. static unsigned int last_tx_status, last_rx_status, last_rx_errors;
  229. static unsigned int reset_count;
  230. static int timed_out(unsigned int start_ms, unsigned int ms)
  231. {
  232. return get_ticks_ms() - start_ms > ms;
  233. }
  234. static void delay_ms(unsigned int ms)
  235. {
  236. unsigned int t0 = get_ticks_ms();
  237. while (get_ticks_ms() - t0 < ms)
  238. ;
  239. }
  240. /* Some network syscalls enter with interrupts off; get_ticks_ms() would be
  241. * frozen there, so the link workarounds (which use delay_ms and MDIC
  242. * timeouts) must only run in a context where the PIT is ticking. */
  243. static int irqs_on(void)
  244. {
  245. unsigned int flags;
  246. __asm__ volatile ("pushfl; popl %0" : "=r"(flags));
  247. return (flags & 0x200u) != 0;
  248. }
  249. /* ---- serial debug trail (the netlog mirror carries it to `make netmon`) ---- */
  250. static void dbg(const char *s)
  251. {
  252. serial_puts(SERIAL_COM1, "e1000e: ");
  253. serial_puts(SERIAL_COM1, s);
  254. serial_puts(SERIAL_COM1, "\n");
  255. /* Also show the trail on the boot console (NOWM diagnostics): if the
  256. * probe wedges, the last line says which step. No-op once the WM has
  257. * taken over and disabled the console. */
  258. bootcon_puts("e1000e: ");
  259. bootcon_puts(s);
  260. bootcon_puts("\n");
  261. }
  262. /* "name=0x12345678" through dbg(), so the value is visible on the boot
  263. * console too (make nowm) and not only on serial/netlog. */
  264. static void dbg_hex(const char *name, unsigned int v)
  265. {
  266. static const char hx[] = "0123456789ABCDEF";
  267. char buf[48];
  268. unsigned int i = 0;
  269. int shift;
  270. while (*name && i < 24)
  271. buf[i++] = *name++;
  272. buf[i++] = '=';
  273. buf[i++] = '0';
  274. buf[i++] = 'x';
  275. for (shift = 28; shift >= 0; shift -= 4)
  276. buf[i++] = hx[(v >> shift) & 0xF];
  277. buf[i] = '\0';
  278. dbg(buf);
  279. }
  280. static void put_dec(unsigned int v)
  281. {
  282. char b[12];
  283. int i = 0;
  284. if (v == 0) {
  285. io_putchar('0');
  286. return;
  287. }
  288. while (v) {
  289. b[i++] = (char)('0' + v % 10);
  290. v /= 10;
  291. }
  292. while (i)
  293. io_putchar(b[--i]);
  294. }
  295. static void put_hex2(unsigned int v)
  296. {
  297. static const char hx[] = "0123456789ABCDEF";
  298. io_putchar(hx[(v >> 4) & 0xF]);
  299. io_putchar(hx[v & 0xF]);
  300. }
  301. static void put_mac(const unsigned char *m)
  302. {
  303. unsigned int i;
  304. for (i = 0; i < 6; i++) {
  305. put_hex2(m[i]);
  306. if (i != 5)
  307. io_putchar(':');
  308. }
  309. }
  310. /* ---- MMIO accessors (uncacheable mapping) ---- */
  311. static inline unsigned int r32(unsigned int reg)
  312. {
  313. return *(volatile unsigned int *)(mmio + reg);
  314. }
  315. static inline void w32(unsigned int reg, unsigned int v)
  316. {
  317. *(volatile unsigned int *)(mmio + reg) = v;
  318. }
  319. /* Snapshot the registers firmware/ME could have changed. This is the first
  320. * thing to look at on a board where DHCP fails: RFCTL.EXTEN in particular
  321. * changes the RX descriptor layout. */
  322. static void log_state(const char *tag)
  323. {
  324. dbg(tag);
  325. dbg_hex(" ctrl", r32(REG_CTRL));
  326. dbg_hex(" ctrl_ext", r32(REG_CTRL_EXT));
  327. dbg_hex(" status", r32(REG_STATUS));
  328. dbg_hex(" rfctl", r32(REG_RFCTL));
  329. dbg_hex(" rxcsum", r32(REG_RXCSUM));
  330. dbg_hex(" rxdctl", r32(REG_RXDCTL0));
  331. dbg_hex(" tctl", r32(REG_TCTL));
  332. dbg_hex(" txdctl", r32(REG_TXDCTL0));
  333. dbg_hex(" pba", r32(REG_PBA));
  334. dbg_hex(" pbs", r32(REG_PBS));
  335. dbg_hex(" extcnf", r32(REG_EXTCNF_CTRL));
  336. dbg_hex(" fwsm", r32(REG_FWSM));
  337. }
  338. /* ---- PCI discovery ---- */
  339. static int supported_device(unsigned int dev)
  340. {
  341. switch (dev) {
  342. case 0x100E: /* 82540EM: QEMU's e1000 */
  343. case 0x10D3: /* 82574L: QEMU's e1000e */
  344. case 0x10EA: /* 82577LM: ThinkPad X201 */
  345. case 0x10EB: /* 82577LC */
  346. case 0x10EF: /* 82578DM */
  347. case 0x10F0: /* 82578DC */
  348. case 0x1502: /* 82579LM */
  349. case 0x1503: /* 82579V */
  350. return 1;
  351. default:
  352. return 0;
  353. }
  354. }
  355. static const char *chip_name(unsigned int dev)
  356. {
  357. switch (dev) {
  358. case 0x100E: return "Intel 82540EM (QEMU e1000)";
  359. case 0x10D3: return "Intel 82574L";
  360. case 0x10EA: return "Intel 82577LM";
  361. case 0x10EB: return "Intel 82577LC";
  362. case 0x10EF: return "Intel 82578DM";
  363. case 0x10F0: return "Intel 82578DC";
  364. case 0x1502: return "Intel 82579LM";
  365. case 0x1503: return "Intel 82579V";
  366. default: return "Intel e1000e";
  367. }
  368. }
  369. /* PCH-integrated LAN: the PHY sits behind the Kumeran interface and needs a
  370. * large set of PHY/Kumeran workarounds after a full-chip reset. Booting the
  371. * firmware has already configured it, so the driver deliberately does a warm
  372. * init on these parts (stop our RX/TX, leave CTRL.PHY_RST alone) and only
  373. * restarts autoneg if the link is down. */
  374. static int is_pch_lan(unsigned int dev)
  375. {
  376. switch (dev) {
  377. case 0x10EA: case 0x10EB: case 0x10EF: case 0x10F0:
  378. case 0x1502: case 0x1503:
  379. return 1;
  380. default:
  381. return 0;
  382. }
  383. }
  384. /* The PHY workarounds below are the pchlan (82577/82578) ones. The 82579
  385. * is pch2lan and has a different K1/LV sequence; leave its firmware setup
  386. * alone rather than writing 82577-specific registers to it. */
  387. static int pch_phy_82577(void)
  388. {
  389. return device_id == 0x10EA || device_id == 0x10EB ||
  390. (phy_id1 == 0x0154 && phy_id2 >= 0 &&
  391. (phy_id2 & 0xFFF0) == 0x0050);
  392. }
  393. static int pch_phy_82578(void)
  394. {
  395. return device_id == 0x10EF || device_id == 0x10F0 ||
  396. phy_id1 == 0x004D;
  397. }
  398. /* BAR0 is the 128 KB register window on every supported part. Handle a
  399. * 64-bit BAR and probe its size with the usual all-ones trick. */
  400. static int probe_bar0(unsigned int bus, unsigned int dev, unsigned int func,
  401. unsigned int *base, unsigned int *size)
  402. {
  403. unsigned int val = pci_cfg_read32(bus, dev, func, 0x10);
  404. if (!val || val == 0xFFFFFFFFu || (val & 1u))
  405. return -1;
  406. /* Do NOT size the BAR by writing 0xFFFFFFFF: on PCH LAN parts the
  407. * ME/AMT can leave the BAR restore incomplete, after which every MMIO
  408. * access goes to an undecoded address and stalls. All supported
  409. * e1000/e1000e BARs are 128 KB, so trust the firmware-assigned base. */
  410. if (val & 4u)
  411. return -1; /* 64-bit BAR: not used by these parts */
  412. *base = val & 0xFFFFFFF0u;
  413. *size = 0x20000;
  414. return 0;
  415. }
  416. /* Put the function in D0 before the first MMIO access. The PCH LAN is
  417. * often left in D3hot by the BIOS/ME; MMIO while suspended can return
  418. * 0xFFFFFFFF or stall the PCIe root complex. Walk the capability list for
  419. * the PM capability (ID 1) and clear PMCSR.PS. */
  420. static void pci_set_d0(unsigned int bus, unsigned int dev, unsigned int func)
  421. {
  422. unsigned int status = pci_cfg_read32(bus, dev, func, 0x04) >> 16;
  423. unsigned int cap, guard = 0;
  424. if (!(status & 0x10u))
  425. return; /* no capability list */
  426. cap = pci_cfg_read32(bus, dev, func, 0x34) & 0xFCu;
  427. while (cap && guard++ < 48) {
  428. unsigned int hdr = pci_cfg_read32(bus, dev, func, cap);
  429. if ((hdr & 0xFFu) == 0x01u) {
  430. unsigned int pmcsr =
  431. pci_cfg_read32(bus, dev, func, cap + 4);
  432. if (pmcsr & 3u) {
  433. pci_cfg_write32(bus, dev, func, cap + 4,
  434. pmcsr & ~3u);
  435. delay_ms(10);
  436. dbg("PM state -> D0");
  437. }
  438. return;
  439. }
  440. cap = (hdr >> 8) & 0xFCu;
  441. }
  442. }
  443. /* The e1000e PCH LAN is sensitive to PCIe ASPM L1/L0s: it causes long
  444. * wake-up latency and packet loss on ThinkPads (the 82573/82577 family).
  445. * Linux's e1000e_disable_aspm() clears the ASPM enables in the Link
  446. * Control register for the affected parts. On bus 0 there is no upstream
  447. * bridge device, so clearing the function's own enables is the same thing
  448. * the Linux driver does. */
  449. static void pci_disable_aspm(unsigned int bus, unsigned int dev,
  450. unsigned int func)
  451. {
  452. unsigned int status = pci_cfg_read32(bus, dev, func, 0x04) >> 16;
  453. unsigned int cap, guard = 0;
  454. if (!(status & 0x10u))
  455. return; /* no capability list */
  456. cap = pci_cfg_read32(bus, dev, func, 0x34) & 0xFCu;
  457. while (cap && guard++ < 48) {
  458. unsigned int hdr = pci_cfg_read32(bus, dev, func, cap);
  459. if ((hdr & 0xFFu) == 0x10u) { /* PCI Express capability */
  460. unsigned int lc =
  461. pci_cfg_read32(bus, dev, func,
  462. cap + 0x10) & 0xFFFFu;
  463. if (lc & 0x3u) {
  464. dbg_hex(" PCIe linkctl before", lc);
  465. pci_cfg_write16(bus, dev, func, cap + 0x10,
  466. (unsigned short)(lc & ~0x3u));
  467. dbg_hex(" PCIe linkctl after",
  468. pci_cfg_read32(bus, dev, func,
  469. cap + 0x10) & 0xFFFFu);
  470. dbg("PCIe ASPM disabled");
  471. }
  472. return;
  473. }
  474. cap = (hdr >> 8) & 0xFCu;
  475. }
  476. }
  477. static int probe_pci(void)
  478. {
  479. unsigned int bus, dev, func;
  480. for (bus = 0; bus < 256; bus++) {
  481. for (dev = 0; dev < 32; dev++) {
  482. unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
  483. unsigned int htype, nfunc;
  484. if (id == 0xFFFFFFFFu || id == 0)
  485. continue;
  486. htype = (pci_cfg_read32(bus, dev, 0, 0x0C) >> 16) & 0xFFu;
  487. nfunc = (htype & 0x80u) ? 8u : 1u;
  488. for (func = 0; func < nfunc; func++) {
  489. unsigned int idf, cmd;
  490. idf = pci_cfg_read32(bus, dev, func, 0x00);
  491. if (idf == 0xFFFFFFFFu || idf == 0)
  492. continue;
  493. if ((idf & 0xFFFFu) != E1000E_VENDOR)
  494. continue;
  495. if (!supported_device((idf >> 16) & 0xFFFFu))
  496. continue;
  497. /* Memory space + bus master. */
  498. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  499. /* Polled driver: disable INTx before enabling memory
  500. * and bus master. A link-change interrupt from
  501. * the PHY can otherwise fire before hw_stop()
  502. * masks the device, and the kernel has no NIC IRQ
  503. * handler (the X201 stalls with the cable in). */
  504. pci_cfg_write32(bus, dev, func, 0x04,
  505. cmd | 0x0006u |
  506. PCI_CMD_INT_DISABLE);
  507. pci_set_d0(bus, dev, func);
  508. pci_disable_aspm(bus, dev, func);
  509. /* Let the function settle after D0/memory enable;
  510. * the ME can still be finishing its link work when
  511. * the cable is present. */
  512. delay_ms(10);
  513. if (probe_bar0(bus, dev, func,
  514. &bar_base, &bar_size) != 0)
  515. continue;
  516. pci_bus = bus;
  517. pci_dev = dev;
  518. pci_func = func;
  519. device_id = (idf >> 16) & 0xFFFFu;
  520. detected_chip = chip_name(device_id);
  521. pch_lan = is_pch_lan(device_id);
  522. return 0;
  523. }
  524. }
  525. }
  526. return -1;
  527. }
  528. /* ---- MAC address ---- */
  529. /* Classic EERD word read; used only when RAL/RAH came back invalid. */
  530. static int eeprom_read16(unsigned int addr, unsigned short *out)
  531. {
  532. unsigned int t0, v;
  533. w32(REG_EERD, (addr << EERD_ADDR_SHIFT) | EERD_START);
  534. t0 = get_ticks_ms();
  535. for (;;) {
  536. v = r32(REG_EERD);
  537. if (v & EERD_DONE) {
  538. *out = (unsigned short)(v >> EERD_DATA_SHIFT);
  539. return 0;
  540. }
  541. if (timed_out(t0, 5))
  542. return -1;
  543. }
  544. }
  545. static int mac_valid(const unsigned char *m)
  546. {
  547. unsigned int i;
  548. int zero = 1, ones = 1;
  549. for (i = 0; i < 6; i++) {
  550. if (m[i] != 0x00)
  551. zero = 0;
  552. if (m[i] != 0xFF)
  553. ones = 0;
  554. }
  555. return !zero && !ones;
  556. }
  557. /* Program the unicast filter the way Linux's e1000e_rar_set_generic()
  558. * does: RAL first, then RAH with AV last. The previous RAH-then-RAL
  559. * order could leave the exact-match filter pointing at a stale low half
  560. * while AV was already set; broadcast/multicast still worked, but unicast
  561. * to the board's own MAC was dropped. */
  562. static void write_rar(void)
  563. {
  564. w32(REG_RAL0, (unsigned int)mac[0] | ((unsigned int)mac[1] << 8) |
  565. ((unsigned int)mac[2] << 16) | ((unsigned int)mac[3] << 24));
  566. __sync_synchronize();
  567. w32(REG_RAH0, 0x80000000u | ((unsigned int)mac[5] << 8) | mac[4]);
  568. __sync_synchronize();
  569. }
  570. static void read_mac(void)
  571. {
  572. unsigned int ral = r32(REG_RAL0);
  573. unsigned int rah = r32(REG_RAH0);
  574. mac[0] = (unsigned char)(ral & 0xFF);
  575. mac[1] = (unsigned char)((ral >> 8) & 0xFF);
  576. mac[2] = (unsigned char)((ral >> 16) & 0xFF);
  577. mac[3] = (unsigned char)((ral >> 24) & 0xFF);
  578. mac[4] = (unsigned char)(rah & 0xFF);
  579. mac[5] = (unsigned char)((rah >> 8) & 0xFF);
  580. if (!mac_valid(mac)) {
  581. unsigned short w0 = 0, w1 = 0, w2 = 0;
  582. if (eeprom_read16(0, &w0) == 0 &&
  583. eeprom_read16(1, &w1) == 0 &&
  584. eeprom_read16(2, &w2) == 0) {
  585. mac[0] = (unsigned char)(w0 & 0xFF);
  586. mac[1] = (unsigned char)(w0 >> 8);
  587. mac[2] = (unsigned char)(w1 & 0xFF);
  588. mac[3] = (unsigned char)(w1 >> 8);
  589. mac[4] = (unsigned char)(w2 & 0xFF);
  590. mac[5] = (unsigned char)(w2 >> 8);
  591. }
  592. }
  593. write_rar();
  594. }
  595. /* Runtime MAC override (netmac diagnostic). The TX source, ARP and DHCP
  596. * client all use the `mac` array, so changing it and reprogramming RAL/RAH
  597. * is enough to move the whole interface to a different address. */
  598. static void e1000e_set_mac(const unsigned char m[6])
  599. {
  600. unsigned int i;
  601. for (i = 0; i < 6; i++)
  602. mac[i] = m[i];
  603. write_rar();
  604. }
  605. /* ---- PHY access via MDIC ---- */
  606. static int mdic_read(unsigned int phy, unsigned int reg)
  607. {
  608. unsigned int t0, v;
  609. w32(REG_MDIC, (reg << MDIC_REG_SHIFT) | (phy << MDIC_PHY_SHIFT) |
  610. MDIC_OP_READ);
  611. t0 = get_ticks_ms();
  612. for (;;) {
  613. v = r32(REG_MDIC);
  614. if (v & MDIC_READY)
  615. return (v & MDIC_ERROR) ? -1 : (int)(v & MDIC_DATA_MASK);
  616. if (timed_out(t0, pch_lan ? MDIC_TIMEOUT_MS : 10))
  617. return -1;
  618. }
  619. }
  620. static int mdic_write(unsigned int phy, unsigned int reg, unsigned int data)
  621. {
  622. unsigned int t0, v;
  623. w32(REG_MDIC, (data & MDIC_DATA_MASK) | (reg << MDIC_REG_SHIFT) |
  624. (phy << MDIC_PHY_SHIFT) | MDIC_OP_WRITE);
  625. t0 = get_ticks_ms();
  626. for (;;) {
  627. v = r32(REG_MDIC);
  628. if (v & MDIC_READY)
  629. return (v & MDIC_ERROR) ? -1 : 0;
  630. if (timed_out(t0, pch_lan ? MDIC_TIMEOUT_MS : 10))
  631. return -1;
  632. }
  633. }
  634. /* ---- PCH (82577/82578/82579) PHY access ---- */
  635. /* The HV registers are reached through the IGP page-select register (31):
  636. * the page number is shifted left by 5, exactly like PHY_REG(page, reg). */
  637. /* The 82577 uses two MDIC addresses: page 0 / standard MII registers at
  638. * address 2, and the HV pages (768+) at address 1. Linux's
  639. * e1000_get_phy_addr_for_hv_page() encodes exactly this. */
  640. #define HV_PHY_ADDR 1
  641. #define HV_PAGE_START 768
  642. static unsigned int phy_page_addr(unsigned int page)
  643. {
  644. if (page >= HV_PAGE_START)
  645. return HV_PHY_ADDR;
  646. return (phy_addr >= 0) ? (unsigned int)phy_addr : 0xFFFFFFFFu;
  647. }
  648. static void phy_set_page(unsigned int page)
  649. {
  650. unsigned int addr = phy_page_addr(page);
  651. if (addr == 0xFFFFFFFFu)
  652. return;
  653. mdic_write(addr, 31, page << 5);
  654. }
  655. static int phy_read_page(unsigned int page, unsigned int reg)
  656. {
  657. unsigned int addr = phy_page_addr(page);
  658. if (addr == 0xFFFFFFFFu)
  659. return -1;
  660. mdic_write(addr, 31, page << 5);
  661. return mdic_read(addr, reg);
  662. }
  663. static int phy_write_page(unsigned int page, unsigned int reg, unsigned int data)
  664. {
  665. unsigned int addr = phy_page_addr(page);
  666. if (addr == 0xFFFFFFFFu)
  667. return -1;
  668. mdic_write(addr, 31, page << 5);
  669. return mdic_write(addr, reg, data);
  670. }
  671. /* e1000_set_mdio_slow_mode_hv(): the 82577 PHY needs slow MDIO timing, and
  672. * Linux sets it before the first reliable PHY ID read. Without it the
  673. * 82577 often answers with 0/0xffff (which is why phy_probe() used to fail). */
  674. static void pch_slow_mdio(void)
  675. {
  676. int v;
  677. int lock;
  678. if (phy_addr < 0)
  679. return;
  680. lock = pch_swflag_acquire() == 0;
  681. v = phy_read_page(769, 16);
  682. if (v < 0) {
  683. /* The first read can fail before the PHY is in slow mode; try
  684. * once more, then set the bit without a read-modify-write. */
  685. delay_ms(1);
  686. v = phy_read_page(769, 16);
  687. }
  688. if (v >= 0)
  689. phy_write_page(769, 16, (unsigned int)v | 0x0400u);
  690. else
  691. phy_write_page(769, 16, 0x0400u);
  692. phy_set_page(0);
  693. if (lock)
  694. pch_swflag_release();
  695. }
  696. /* e1000_hv_phy_workarounds_ich8lan() and e1000_copper_link_setup_82577().
  697. * The firmware normally leaves the PHY in a usable state, but these are the
  698. * parts that make the link reliable at gigabit speed; they are idempotent. */
  699. static void pch_phy_workarounds(void)
  700. {
  701. int v;
  702. int is77 = pch_phy_82577();
  703. int is78 = pch_phy_82578();
  704. int lock;
  705. if (!pch_lan || phy_addr < 0)
  706. return;
  707. if (!is77 && !is78)
  708. return;
  709. lock = pch_swflag_acquire() == 0;
  710. /* BM_PORT_GEN_CFG: clear the host wakeup bits in the low byte. */
  711. v = phy_read_page(769, 17);
  712. if (v >= 0)
  713. phy_write_page(769, 17, (unsigned int)v & 0x00FFu);
  714. /* Back to page 0 for the standard registers and the EMI window. */
  715. phy_set_page(0);
  716. /* 82577 MSE threshold: keep the link up in noise (EMI 0x0887). */
  717. mdic_write((unsigned int)phy_addr, 16, 0x0887);
  718. mdic_write((unsigned int)phy_addr, 17, 0x0034);
  719. if (is77) {
  720. /* Assert CRS on TX and enable downshift. */
  721. v = mdic_read((unsigned int)phy_addr, 22);
  722. if (v >= 0)
  723. mdic_write((unsigned int)phy_addr, 22,
  724. (unsigned int)v | 0x8000u | 0x0C00u);
  725. /* Auto MDI/MDIX. */
  726. v = mdic_read((unsigned int)phy_addr, 18);
  727. if (v >= 0) {
  728. unsigned int mdix = (unsigned int)v;
  729. mdix &= ~0x0600u;
  730. mdix |= 0x0400u;
  731. mdic_write((unsigned int)phy_addr, 18, mdix);
  732. }
  733. /* Auto master/slave resolution. */
  734. v = mdic_read((unsigned int)phy_addr, MII_GBSR);
  735. if (v >= 0)
  736. mdic_write((unsigned int)phy_addr, MII_GBSR,
  737. (unsigned int)v & ~0x1000u);
  738. }
  739. /* 82577 rev 1/2: disable early preamble generation and tune the
  740. * Kumeran FIFO preamble for SSC. */
  741. if (is77 && phy_id1 == 0x0154 && phy_id2 >= 0 &&
  742. (phy_id2 & 0xF) >= 1 && (phy_id2 & 0xF) <= 2) {
  743. phy_write_page(769, 25, 0x4431);
  744. phy_write_page(770, 16, 0xA204);
  745. }
  746. phy_set_page(0);
  747. if (lock)
  748. pch_swflag_release();
  749. }
  750. /* Kumeran control/status register (MAC CSR 0x34). offset 7 bit 1 is the K1
  751. * enable; offset 4/9 are the timeout and in-band parameters. */
  752. static unsigned int kmrn_read(unsigned int off)
  753. {
  754. w32(REG_KMRNCTRLSTA, (off << 16) | 0x00200000u);
  755. delay_ms(1);
  756. return r32(REG_KMRNCTRLSTA) & 0xFFFFu;
  757. }
  758. static void kmrn_write(unsigned int off, unsigned int data)
  759. {
  760. w32(REG_KMRNCTRLSTA, (off << 16) | (data & 0xFFFFu));
  761. delay_ms(1);
  762. }
  763. /* e1000_configure_k1_ich8lan(): change the K1 enable bit, then toggle the
  764. * forced-speed/bypass bits so the MAC samples the new setting. */
  765. static void pch_k1_config(int enable)
  766. {
  767. unsigned int v = kmrn_read(7);
  768. unsigned int ctrl, ctrl_ext, forced;
  769. if (enable)
  770. v |= 0x0002u;
  771. else
  772. v &= ~0x0002u;
  773. kmrn_write(7, v);
  774. ctrl = r32(REG_CTRL);
  775. ctrl_ext = r32(REG_CTRL_EXT);
  776. forced = ctrl & ~(0x00000100u | 0x00000200u);
  777. forced |= 0x00000800u; /* CTRL_FRCSPD */
  778. w32(REG_CTRL, forced);
  779. w32(REG_CTRL_EXT, ctrl_ext | 0x00008000u); /* SPD_BYPS */
  780. delay_ms(1);
  781. w32(REG_CTRL, ctrl);
  782. w32(REG_CTRL_EXT, ctrl_ext);
  783. delay_ms(1);
  784. }
  785. /* e1000_k1_gig_workaround_hv(): at 1 Gbps K1 must be off or the MAC can
  786. * stall when it transitions from a lower speed. The preamble workaround
  787. * below is e1000_check_for_copper_link_ich8lan()'s half-duplex fix. */
  788. static void pch_link_up_workarounds(void)
  789. {
  790. unsigned int st = r32(REG_STATUS);
  791. int ms, v;
  792. int is77 = pch_phy_82577();
  793. int is78 = pch_phy_82578();
  794. int lock;
  795. int speed1000 = (st & STATUS_SPEED) == 0x80;
  796. if (!pch_lan || phy_addr < 0)
  797. return;
  798. if (!is77 && !is78)
  799. return;
  800. lock = pch_swflag_acquire() == 0;
  801. phy_set_page(0);
  802. if (is78) {
  803. /* BM_CS_STATUS: link up + resolved + 1000 Mbps. */
  804. ms = mdic_read((unsigned int)phy_addr, 17);
  805. if (ms >= 0 &&
  806. (ms & 0x0C00) == 0x0C00 &&
  807. (ms & 0xC000) == 0x8000)
  808. v = 0;
  809. else if (speed1000)
  810. v = 0; /* STATUS says 1G: never leave K1 on */
  811. else
  812. v = 1;
  813. } else {
  814. ms = mdic_read((unsigned int)phy_addr, 26);
  815. if (ms >= 0 &&
  816. (ms & 0x0040) && (ms & 0x1000) &&
  817. (ms & 0x0300) == 0x0200)
  818. v = 0;
  819. else if (speed1000)
  820. v = 0; /* PHY read failed; trust STATUS */
  821. else
  822. v = 1;
  823. }
  824. dbg(v ? "K1 enabled" : "K1 disabled (1G)");
  825. /* Link-stall fix, link-up value, then the K1 setting. */
  826. phy_write_page(770, 19, 0x0100);
  827. pch_k1_config(v);
  828. {
  829. int pre = phy_read_page(770, 16);
  830. if (pre >= 0) {
  831. unsigned int p = (unsigned int)pre & ~0x7000u;
  832. if (!(st & STATUS_FD))
  833. p |= 0x1000;
  834. phy_write_page(770, 16, p);
  835. }
  836. }
  837. phy_set_page(0);
  838. if (lock)
  839. pch_swflag_release();
  840. }
  841. static void pch_link_down_workarounds(void)
  842. {
  843. int lock;
  844. if (!pch_lan || phy_addr < 0)
  845. return;
  846. if (!pch_phy_82577() && !pch_phy_82578())
  847. return;
  848. lock = pch_swflag_acquire() == 0;
  849. phy_write_page(770, 19, 0x4100);
  850. pch_k1_config(1);
  851. phy_set_page(0);
  852. if (lock)
  853. pch_swflag_release();
  854. }
  855. /* Poll the PHY link while commands are running. Linux's watchdog does the
  856. * same every couple of seconds: re-apply the K1/preamble workarounds when
  857. * the link comes up and re-enable RCTL/TCTL. Without this a cable plugged
  858. * in after boot never gets the link-up workarounds. */
  859. static void link_check(void)
  860. {
  861. unsigned int now, st;
  862. if (!present || !mmio || !pch_lan)
  863. return;
  864. if (!irqs_on())
  865. return;
  866. now = get_ticks_ms();
  867. if (now - link_poll_ms < 250)
  868. return;
  869. link_poll_ms = now;
  870. st = r32(REG_STATUS);
  871. if (!!(st & STATUS_LU) == last_link)
  872. return;
  873. last_link = !!(st & STATUS_LU);
  874. if (last_link) {
  875. dbg("link up (poll)");
  876. pch_link_up_workarounds();
  877. w32(REG_CTRL, r32(REG_CTRL) | CTRL_SLU);
  878. w32(REG_RCTL, r32(REG_RCTL) | RCTL_EN);
  879. w32(REG_TCTL, r32(REG_TCTL) | TCTL_EN);
  880. } else {
  881. dbg("link down (poll)");
  882. pch_link_down_workarounds();
  883. phy_autoneg();
  884. }
  885. }
  886. static void phy_probe(void)
  887. {
  888. unsigned int phy;
  889. int lock;
  890. phy_addr = -1;
  891. phy_id1 = phy_id2 = -1;
  892. lock = pch_swflag_acquire() == 0;
  893. for (phy = 0; phy < 4; phy++) {
  894. int id1, id2;
  895. /* Make sure a previous page-select is not in effect. */
  896. mdic_write(phy, 31, 0);
  897. id1 = mdic_read(phy, MII_PHYID1);
  898. id2 = mdic_read(phy, MII_PHYID2);
  899. if (id1 < 0 || id2 < 0)
  900. continue;
  901. if ((id1 & 0xFFFF) == 0 && (id2 & 0xFFFF) == 0)
  902. continue;
  903. if ((id1 & 0xFFFF) == 0xFFFF)
  904. continue;
  905. phy_addr = (int)phy;
  906. phy_id1 = id1;
  907. phy_id2 = id2;
  908. break;
  909. }
  910. if (lock)
  911. pch_swflag_release();
  912. }
  913. /* Poll STATUS.LU for a link; used after a full-chip reset and by net100.
  914. * Returns 1 when the link is up, 0 on timeout. */
  915. static int wait_link(unsigned int timeout_ms)
  916. {
  917. unsigned int t0 = get_ticks_ms();
  918. while (!(r32(REG_STATUS) & STATUS_LU)) {
  919. if (timed_out(t0, timeout_ms))
  920. return 0;
  921. delay_ms(10);
  922. }
  923. return 1;
  924. }
  925. static void phy_autoneg(void)
  926. {
  927. int lock;
  928. if (phy_addr < 0)
  929. return;
  930. lock = pch_swflag_acquire() == 0;
  931. phy_set_page(0);
  932. mdic_write(phy_addr, MII_ANAR, 0x01E1); /* 10/100 half+full */
  933. /* force_speed is the net100 fallback: with 1000BASE-T advertisement
  934. * cleared the PHY can only resolve 10/100, which bypasses a marginal
  935. * 1000M pair/jack while still giving a fully usable network. */
  936. mdic_write(phy_addr, MII_GBSR, force_speed == 100 ? 0x0000 : 0x0300);
  937. mdic_write(phy_addr, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
  938. if (lock)
  939. pch_swflag_release();
  940. }
  941. /* net100 fallback: 0 = auto/1G advertisement, 100 = 100M only.
  942. *
  943. * The link is deliberately dropped and renegotiated. The caller (the
  944. * net100 command) waits for the result; the force_speed flag is also
  945. * honoured by phy_autoneg() so a link-down poll cannot silently restore
  946. * 1000M. */
  947. static int e1000e_set_speed(unsigned int mbps)
  948. {
  949. unsigned int t0;
  950. if (!present || !mmio || phy_addr < 0)
  951. return -1;
  952. if (mbps != 0 && mbps != 100)
  953. return -1;
  954. force_speed = (mbps == 100) ? 100 : 0;
  955. phy_autoneg(); /* honours force_speed and restarts autoneg */
  956. /* First let the old link drop (the PHY restarts autoneg), then wait
  957. * for the new one. A switch can take a couple of seconds. */
  958. t0 = get_ticks_ms();
  959. while ((r32(REG_STATUS) & STATUS_LU) && !timed_out(t0, 1000))
  960. delay_ms(10);
  961. if (!wait_link(5000)) {
  962. last_link = 0;
  963. return -3;
  964. }
  965. if (pch_lan)
  966. pch_link_up_workarounds();
  967. /* The MAC may clear RCTL/TCTL on the link transition; re-assert them
  968. * here because last_link is set to 1 and link_check() will not. */
  969. w32(REG_CTRL, r32(REG_CTRL) | CTRL_SLU);
  970. w32(REG_RCTL, r32(REG_RCTL) | RCTL_EN);
  971. w32(REG_TCTL, r32(REG_TCTL) | TCTL_EN);
  972. last_link = 1;
  973. return 0;
  974. }
  975. /* ---- hardware bring-up ---- */
  976. /* Quiesce the MAC without touching the PHY: mask interrupts, stop RX/TX and
  977. * let any in-flight DMA finish. This is the "reset" path on PCH LAN parts. */
  978. static void hw_stop(void)
  979. {
  980. unsigned int tctl;
  981. /* PCH LAN: let the ME finish the current link transaction before the
  982. * first MAC write. */
  983. delay_ms(5);
  984. w32(REG_IMC, 0xFFFFFFFFu);
  985. (void)r32(REG_ICR);
  986. w32(REG_RCTL, 0);
  987. /* Stop TX without clobbering the 82577's reserved TCTL bits (bit 28
  988. * and RRTHRESH are reset to 1/01 and must stay that way). */
  989. tctl = r32(REG_TCTL);
  990. w32(REG_TCTL, (tctl & ~TCTL_EN) | TCTL_PSP);
  991. (void)r32(REG_STATUS); /* flush posted writes */
  992. delay_ms(10);
  993. }
  994. static void e1000e_stop(void)
  995. {
  996. unsigned int t0;
  997. if (!present || !mmio)
  998. return;
  999. hw_stop();
  1000. w32(REG_CTRL, r32(REG_CTRL) | CTRL_GIO_MASTER_DISABLE);
  1001. t0 = get_ticks_ms();
  1002. while ((r32(REG_STATUS) & STATUS_GIO_MASTER_ENABLE) &&
  1003. !timed_out(t0, 100))
  1004. delay_ms(1);
  1005. dbg("stopped for kexec");
  1006. }
  1007. /* The 82577 PBA register has a 5-bit RX allocation (RXA) and a read-only
  1008. * TX allocation (TXA = PBS - RXA). Linux uses 26 KB RX on PCH parts,
  1009. * leaving the rest of the packet buffer for TX. If firmware/ME left RXA at
  1010. * PBS, TXA is zero and the transmit engine has no FIFO space, so the queue
  1011. * never moves. */
  1012. static void pch_pba_config(void)
  1013. {
  1014. unsigned int pbs, pba;
  1015. if (!pch_lan)
  1016. return;
  1017. pbs = r32(REG_PBS);
  1018. pba = r32(REG_PBA);
  1019. if (pbs > 26 && (pba & 0x1Fu) != 26) {
  1020. w32(REG_PBA, 26);
  1021. dbg("PBA set to 26K RX");
  1022. }
  1023. serial_puts(SERIAL_COM1, "e1000e: pbs ");
  1024. serial_puthex(SERIAL_COM1, pbs);
  1025. serial_puts(SERIAL_COM1, " pba ");
  1026. serial_puthex(SERIAL_COM1, r32(REG_PBA));
  1027. serial_puts(SERIAL_COM1, "\n");
  1028. }
  1029. /* PCH LAN with AMT: the firmware (ME) must be told that the OS driver owns
  1030. * the network interface, otherwise it can keep the LAN - notably the TX
  1031. * path - to itself. e1000e's e1000e_get_hw_control() sets
  1032. * CTRL_EXT.DRV_LOAD for parts with FLAG_HAS_CTRLEXT_ON_LOAD (the pch_info
  1033. * used by the 82577LM/82578/82579). e1000e_open() takes ownership before
  1034. * the reset on AMT parts; do the same, then call this again after the reset
  1035. * to restore WUC/VET. */
  1036. static void pch_claim(void)
  1037. {
  1038. unsigned int ctrl_ext;
  1039. if (!pch_lan)
  1040. return;
  1041. ctrl_ext = r32(REG_CTRL_EXT);
  1042. if (!(ctrl_ext & 0x10000000u)) {
  1043. w32(REG_CTRL_EXT, ctrl_ext | 0x10000000u);
  1044. dbg("AMT handshake: CTRL_EXT.DRV_LOAD set");
  1045. }
  1046. /* e1000e_reset(): clear wake-up control and enable VLAN EtherType.
  1047. * PBA needs the special PCH encoding (RXA only); pch_pba_config()
  1048. * handles it and leaves non-PCH split-PBA parts alone. */
  1049. w32(REG_WUC, 0);
  1050. w32(REG_VET, 0x8100);
  1051. pch_pba_config();
  1052. /* These are normally applied in the post-reset path; do them here as
  1053. * well so a warm boot (MAC reset skipped while the link is up) gets the
  1054. * same values. CRC_OFFSET is pchlan-only (0x5F50 is the receive-address
  1055. * CRC register on the 82579); KABGTXD applies to the whole ich8lan
  1056. * family. */
  1057. if (device_id == 0x10EA || device_id == 0x10EB ||
  1058. device_id == 0x10EF || device_id == 0x10F0)
  1059. w32(REG_CRC_OFFSET, 0x65656565u);
  1060. if (pch_lan) {
  1061. unsigned int st = r32(REG_STATUS);
  1062. w32(REG_KABGTXD, r32(REG_KABGTXD) | 0x00050000u);
  1063. /* Linux clears this sticky "PHY reset asserted" bit after a
  1064. * reset; the firmware leaves it set on the warm path. */
  1065. if (st & STATUS_PHYRA)
  1066. w32(REG_STATUS, st & ~STATUS_PHYRA);
  1067. }
  1068. }
  1069. static void pch_swflag_release(void)
  1070. {
  1071. if (pch_lan)
  1072. w32(REG_EXTCNF_CTRL, r32(REG_EXTCNF_CTRL) & ~0x20u);
  1073. }
  1074. /* e1000_acquire_swflag_ich8lan(): coordinate PHY/MAC access with the ME
  1075. * before a global reset. Without this the X201 could wedge on CTRL_RST
  1076. * while the ME was holding the shared resource. */
  1077. static int pch_swflag_acquire(void)
  1078. {
  1079. unsigned int t0;
  1080. if (!pch_lan)
  1081. return -1;
  1082. t0 = get_ticks_ms();
  1083. while ((r32(REG_EXTCNF_CTRL) & 0x20u) && !timed_out(t0, 100))
  1084. delay_ms(1);
  1085. if (r32(REG_EXTCNF_CTRL) & 0x20u) {
  1086. dbg("SWFLAG busy");
  1087. return -1;
  1088. }
  1089. w32(REG_EXTCNF_CTRL, r32(REG_EXTCNF_CTRL) | 0x20u);
  1090. t0 = get_ticks_ms();
  1091. while (!(r32(REG_EXTCNF_CTRL) & 0x20u)) {
  1092. if (timed_out(t0, 200)) {
  1093. dbg("SWFLAG acquire timeout");
  1094. w32(REG_EXTCNF_CTRL,
  1095. r32(REG_EXTCNF_CTRL) & ~0x20u);
  1096. return -1;
  1097. }
  1098. delay_ms(1);
  1099. }
  1100. return 0;
  1101. }
  1102. static int hw_reset(void)
  1103. {
  1104. unsigned int t0, ctrl;
  1105. /* Sample the link before hw_stop(): the PHY link is independent of
  1106. * the MAC RX/TX state. */
  1107. int link_up = pch_lan && (r32(REG_STATUS) & STATUS_LU);
  1108. int swflag = 0;
  1109. if (link_up) {
  1110. /* Warm path at probe and netreset: stop our queues but leave
  1111. * the firmware-initialized PHY/MAC alone. */
  1112. hw_stop();
  1113. dbg("MAC reset skipped (link up)");
  1114. return 0;
  1115. }
  1116. /* Block new DMA requests before clearing RCTL. Linux disables the
  1117. * PCIe master first; clearing RCTL while DMA is active can wedge the
  1118. * receive packet buffer on these parts. */
  1119. ctrl = r32(REG_CTRL);
  1120. w32(REG_CTRL, ctrl | CTRL_GIO_MASTER_DISABLE);
  1121. t0 = get_ticks_ms();
  1122. while ((r32(REG_STATUS) & STATUS_GIO_MASTER_ENABLE) &&
  1123. !timed_out(t0, 1000))
  1124. ;
  1125. delay_ms(5);
  1126. hw_stop();
  1127. delay_ms(5);
  1128. /* Coordinate with the ME before the global reset (Linux takes the
  1129. * SW flag after quiescing the queues, just before CTRL_RST). */
  1130. if (pch_lan)
  1131. swflag = pch_swflag_acquire() == 0;
  1132. /* MAC reset (only when the link was down). */
  1133. ctrl = r32(REG_CTRL);
  1134. dbg("full reset: MAC only");
  1135. w32(REG_CTRL, ctrl | CTRL_RST);
  1136. t0 = get_ticks_ms();
  1137. while (r32(REG_CTRL) & CTRL_RST) {
  1138. if (timed_out(t0, 1000)) {
  1139. if (swflag)
  1140. pch_swflag_release();
  1141. return -1;
  1142. }
  1143. }
  1144. delay_ms(20); /* NVM auto-read after reset */
  1145. /* Linux deliberately does not write EXTCNF_CTRL.SWFLAG after a global
  1146. * reset: the reset clears it, and clearing it again can steal the
  1147. * flag from the ME. Only the timeout/error paths release it. */
  1148. /* Wait for the NVM auto-read to finish and clear the done bit. */
  1149. t0 = get_ticks_ms();
  1150. while (!(r32(REG_STATUS) & STATUS_LAN_INIT_DONE) &&
  1151. !timed_out(t0, 200))
  1152. ;
  1153. w32(REG_STATUS, r32(REG_STATUS) & ~STATUS_LAN_INIT_DONE);
  1154. if (device_id == 0x10EA || device_id == 0x10EB ||
  1155. device_id == 0x10EF || device_id == 0x10F0) {
  1156. /* e1000e_reset_hw_ich8lan(): with this value noise on the
  1157. * Kumeran interface is detected as a CRC error and dropped
  1158. * before it reaches the DMA engine. This is the pchlan
  1159. * (82577/82578) register; on the 82579 the same offset is
  1160. * the receive-address CRC calculation register. */
  1161. w32(REG_CRC_OFFSET, 0x65656565u);
  1162. }
  1163. if (pch_lan)
  1164. w32(REG_KABGTXD, r32(REG_KABGTXD) | 0x00050000u);
  1165. reset_count++;
  1166. return 0;
  1167. }
  1168. static void setup_rings(void)
  1169. {
  1170. unsigned int i;
  1171. for (i = 0; i < E1000E_NRX; i++) {
  1172. rx_ring[i].addr_lo = (unsigned int)(rx_bufs + i * E1000E_BUF);
  1173. rx_ring[i].addr_hi = 0;
  1174. rx_ring[i].status_error = 0;
  1175. rx_ring[i].length = 0;
  1176. rx_ring[i].vlan = 0;
  1177. }
  1178. for (i = 0; i < E1000E_NTX; i++) {
  1179. tx_ring[i].addr_lo = (unsigned int)(tx_bufs + i * E1000E_BUF);
  1180. tx_ring[i].addr_hi = 0;
  1181. tx_ring[i].lower = 0;
  1182. tx_ring[i].upper = 0;
  1183. }
  1184. __sync_synchronize();
  1185. w32(REG_RDBAL, (unsigned int)rx_ring);
  1186. w32(REG_RDBAH, 0);
  1187. w32(REG_RDLEN, E1000E_NRX * (unsigned int)sizeof(struct e1000e_rx_desc));
  1188. w32(REG_RDH, 0);
  1189. w32(REG_RDT, E1000E_NRX - 1);
  1190. w32(REG_TDBAL, (unsigned int)tx_ring);
  1191. w32(REG_TDBAH, 0);
  1192. w32(REG_TDLEN, E1000E_NTX * (unsigned int)sizeof(struct e1000e_tx_desc));
  1193. w32(REG_TDH, 0);
  1194. w32(REG_TDT, 0);
  1195. rx_cur = 0;
  1196. tx_cur = 0;
  1197. tx_pending = 0;
  1198. tx_fail_run = 0;
  1199. tx_broken = 0;
  1200. tx_timeout_logged = 0;
  1201. rx_ok = rx_err = rx_drop = 0;
  1202. rx_ucast = rx_bcast = rx_mcast = 0;
  1203. last_rx_len = 0;
  1204. {
  1205. unsigned int i;
  1206. for (i = 0; i < 6; i++)
  1207. last_rx_dst[i] = 0;
  1208. }
  1209. tx_ok = tx_err = 0;
  1210. last_tx_status = 0;
  1211. last_rx_status = 0;
  1212. last_rx_errors = 0;
  1213. tctl_fixes = 0;
  1214. }
  1215. /* PCH LAN: program the TX descriptor write-back policy and arbitration bits
  1216. * that e1000e's ich8lan init sets. Without WTHRESH/COUNT_DESC the 82577 was
  1217. * observed to never report TX descriptor completion (tx_ok stayed 0), while
  1218. * RX worked. */
  1219. static void tx_desc_config(void)
  1220. {
  1221. unsigned int v, gcr;
  1222. /* Enable snooping for TX/RX DMA (see GCR_NO_SNOOP). */
  1223. gcr = r32(REG_GCR);
  1224. w32(REG_GCR, gcr & ~GCR_NO_SNOOP);
  1225. serial_puts(SERIAL_COM1, "e1000e: gcr ");
  1226. serial_puthex(SERIAL_COM1, gcr);
  1227. serial_puts(SERIAL_COM1, " -> ");
  1228. serial_puthex(SERIAL_COM1, gcr & ~GCR_NO_SNOOP);
  1229. serial_puts(SERIAL_COM1, "\n");
  1230. /* CTRL_EXT bits e1000e's ich8 init sets: relaxed-ordering disable,
  1231. * the always-set bit 22, and PHY power-down in D3. */
  1232. v = r32(REG_CTRL_EXT);
  1233. v |= 0x00020000u | 0x00400000u | 0x00100000u;
  1234. w32(REG_CTRL_EXT, v);
  1235. v = r32(REG_TXDCTL0);
  1236. v |= TXDCTL_COUNT_DESC;
  1237. v &= ~(TXDCTL_PTHRESH | TXDCTL_HTHRESH | TXDCTL_WTHRESH);
  1238. v |= TXDCTL_WB_ONE | 0x1Fu; /* GRAN, WTHRESH=1, PTHRESH=31 */
  1239. w32(REG_TXDCTL0, v);
  1240. v = r32(REG_TXDCTL1);
  1241. v |= TXDCTL_COUNT_DESC;
  1242. v &= ~(TXDCTL_PTHRESH | TXDCTL_HTHRESH | TXDCTL_WTHRESH);
  1243. v |= TXDCTL_WB_ONE | 0x1Fu;
  1244. w32(REG_TXDCTL1, v);
  1245. /* TARC queue arbitration workarounds from the PCH init. Linux sets
  1246. * TARC(0) bits 23/24/26/27 and TARC(1) bits 24/26/30 plus bit 28
  1247. * when TCTL.MULR is clear (which it is here). */
  1248. w32(REG_TARC0, r32(REG_TARC0) | 0x0D800000u);
  1249. w32(REG_TARC1, r32(REG_TARC1) | 0x55000000u);
  1250. }
  1251. /* Pin the state of the receive path. The firmware/ME can leave RFCTL,
  1252. * RXCSUM and RXDCTL in whatever its driver used. e1000e/PCH parts use the
  1253. * extended 16-byte RX descriptor write-back, so enable RFCTL.EXTEN and parse
  1254. * status/error at offset 8 and length at 12 (see struct e1000e_rx_desc). */
  1255. static void rx_desc_config(void)
  1256. {
  1257. unsigned int rfctl = r32(REG_RFCTL);
  1258. unsigned int rxcsum = r32(REG_RXCSUM);
  1259. unsigned int v;
  1260. rfctl |= RFCTL_EXTEN;
  1261. rfctl |= RFCTL_NFSW_DIS | RFCTL_NFSR_DIS |
  1262. RFCTL_IPV6_EX_DIS | RFCTL_NEW_IPV6_EXT_DIS;
  1263. w32(REG_RFCTL, rfctl);
  1264. /* No checksum offload: the stack verifies checksums, and the driver
  1265. * rejects any descriptor whose error byte is non-zero. With RXCSUM
  1266. * enabled a UDP checksum of zero (common in DHCP replies) can be
  1267. * reported as TCPE and the reply would be dropped. */
  1268. w32(REG_RXCSUM, 0);
  1269. /* The firmware can leave the receive delay timers programmed; the
  1270. * driver polls descriptors and wants them written back immediately. */
  1271. w32(REG_RDTR, 0);
  1272. w32(REG_RADV, 0);
  1273. /* Match Linux's PCH setting: clear the prefetch/host/write-back
  1274. * thresholds and keep only GRAN. Prefetching descriptors ahead of
  1275. * the tail is unsafe with extended write-back, because the hardware
  1276. * overwrites the buffer-address field with MRQ/RSS; a prefetched
  1277. * descriptor can then carry a garbage address. */
  1278. v = r32(REG_RXDCTL0);
  1279. v &= ~(RXDCTL_PTHRESH | RXDCTL_HTHRESH | RXDCTL_WTHRESH);
  1280. v |= RXDCTL_GRAN;
  1281. w32(REG_RXDCTL0, v);
  1282. v = r32(REG_RXDCTL1);
  1283. v &= ~(RXDCTL_PTHRESH | RXDCTL_HTHRESH | RXDCTL_WTHRESH);
  1284. v |= RXDCTL_GRAN;
  1285. w32(REG_RXDCTL1, v);
  1286. serial_puts(SERIAL_COM1, "e1000e: rfctl ");
  1287. serial_puthex(SERIAL_COM1, rfctl);
  1288. serial_puts(SERIAL_COM1, " rxcsum ");
  1289. serial_puthex(SERIAL_COM1, rxcsum);
  1290. serial_puts(SERIAL_COM1, " rxdctl ");
  1291. serial_puthex(SERIAL_COM1, r32(REG_RXDCTL0));
  1292. serial_puts(SERIAL_COM1, "\n");
  1293. }
  1294. static void hw_start(void)
  1295. {
  1296. unsigned int ctrl, tctl;
  1297. setup_rings();
  1298. if (pch_lan)
  1299. tx_desc_config();
  1300. /* Make the MAC link state explicit, like e1000's
  1301. * e1000_setup_copper_link_ich8lan(): the PHY controls speed/duplex,
  1302. * but SLU must be set and the forced speed/duplex bits cleared. */
  1303. ctrl = r32(REG_CTRL);
  1304. ctrl |= CTRL_SLU;
  1305. ctrl &= ~(CTRL_FRCSPD | CTRL_FRCDPX);
  1306. ctrl &= ~CTRL_GIO_MASTER_DISABLE;
  1307. ctrl &= ~0x40000000u; /* CTRL.VME: no VLAN insert/strip */
  1308. w32(REG_CTRL, ctrl);
  1309. rx_desc_config();
  1310. w32(REG_RCTL, RCTL_EN | RCTL_BAM | RCTL_SECRC);
  1311. /* Preserve the reserved/RRTHRESH bits (82577 reset: bit28=1 and
  1312. * RRTHRESH=01). Writing TCTL from scratch cleared them and left the
  1313. * transmitter unable to fetch descriptors (TDH stuck at 0). */
  1314. tctl = r32(REG_TCTL);
  1315. tctl &= ~(TCTL_CT_MASK | TCTL_COLD_MASK);
  1316. tctl |= TCTL_EN | TCTL_PSP | TCTL_RTLC |
  1317. (TCTL_CT << TCTL_CT_SHIFT) |
  1318. (TCTL_COLD << TCTL_COLD_SHIFT);
  1319. /* The 82577/82578 TCTL reset value has bit 28 = 1 and RRTHRESH
  1320. * (bits 30:29) = 01. A warm boot may inherit a firmware value that
  1321. * cleared them; restore the documented reset state explicitly. */
  1322. if (device_id == 0x10EA || device_id == 0x10EB ||
  1323. device_id == 0x10EF || device_id == 0x10F0)
  1324. tctl |= 0x30000000u;
  1325. w32(REG_TCTL, tctl);
  1326. w32(REG_TIPG, 10u | (8u << 10) | (6u << 20));
  1327. __sync_synchronize();
  1328. }
  1329. /* ---- netdev interface ---- */
  1330. static int e1000e_init(void)
  1331. {
  1332. char buf[64];
  1333. unsigned int i;
  1334. if (inited)
  1335. return present ? 0 : -1;
  1336. inited = 1;
  1337. if (probe_pci() != 0) {
  1338. dbg("no supported Intel NIC");
  1339. return -1;
  1340. }
  1341. process_map_mmio_all(bar_base, bar_size);
  1342. mmio = (unsigned char *)(unsigned long)bar_base;
  1343. if (r32(REG_STATUS) == 0xFFFFFFFFu) {
  1344. dbg("MMIO read failed");
  1345. mmio = 0;
  1346. return -1;
  1347. }
  1348. i = 0;
  1349. /* "PCI 00:19.0 f2500000 128K" */
  1350. {
  1351. static const char hx[] = "0123456789ABCDEF";
  1352. unsigned int n = 0;
  1353. buf[i++] = 'P'; buf[i++] = 'C'; buf[i++] = 'I'; buf[i++] = ' ';
  1354. for (n = 0; n < 2; n++)
  1355. buf[i++] = hx[(pci_bus >> (4 * (1 - n))) & 0xF];
  1356. buf[i++] = ':';
  1357. for (n = 0; n < 2; n++)
  1358. buf[i++] = hx[(pci_dev >> (4 * (1 - n))) & 0xF];
  1359. buf[i++] = '.';
  1360. buf[i++] = (char)('0' + pci_func);
  1361. buf[i++] = ' ';
  1362. for (n = 0; n < 8; n++)
  1363. buf[i++] = hx[(bar_base >> (4 * (7 - n))) & 0xF];
  1364. buf[i++] = ' ';
  1365. buf[i] = '\0';
  1366. }
  1367. dbg(buf);
  1368. /* The PCH LAN/ME needs a moment after the config-space setup before
  1369. * the first MAC register access; without it the probe can wedge when
  1370. * the link is up. */
  1371. delay_ms(10);
  1372. /* AMT parts: Linux sets CTRL_EXT.DRV_LOAD before the reset that runs
  1373. * at e1000e_open() time, so the ME knows the driver owns the LAN
  1374. * while the reset happens. */
  1375. pch_claim();
  1376. log_state("state before reset");
  1377. /* Warm init: leave the firmware-configured PCH PHY alone. The RX
  1378. * prefetch fix is in rx_desc_config(); a full MAC/PHY reset is not
  1379. * needed and wedges this PCH. */
  1380. if (hw_reset() != 0) {
  1381. dbg("MAC reset timeout");
  1382. mmio = 0;
  1383. return -1;
  1384. }
  1385. dbg("reset ok");
  1386. pch_claim();
  1387. log_state("state after reset");
  1388. read_mac();
  1389. /* All ring and buffer memory in one allocation: the page allocator is
  1390. * IRQ-safe but not cross-CPU locked, so two separate calls can (and
  1391. * once did) hand out the same page under boot-time concurrency. A
  1392. * single contiguous run cannot alias itself. */
  1393. {
  1394. unsigned int pages = 2 +
  1395. (E1000E_NRX * E1000E_BUF) / PAGE_SIZE +
  1396. (E1000E_NTX * E1000E_BUF) / PAGE_SIZE;
  1397. unsigned int mem = mm_alloc_pages(pages);
  1398. if (mem) {
  1399. rx_ring = (struct e1000e_rx_desc *)mem;
  1400. tx_ring = (struct e1000e_tx_desc *)(mem + PAGE_SIZE);
  1401. rx_bufs = (unsigned char *)(mem + 2 * PAGE_SIZE);
  1402. tx_bufs = rx_bufs + E1000E_NRX * E1000E_BUF;
  1403. }
  1404. }
  1405. serial_puts(SERIAL_COM1, "e1000e: ring mem rx=");
  1406. serial_puthex(SERIAL_COM1, (unsigned int)rx_ring);
  1407. serial_puts(SERIAL_COM1, " tx=");
  1408. serial_puthex(SERIAL_COM1, (unsigned int)tx_ring);
  1409. serial_puts(SERIAL_COM1, " rxb=");
  1410. serial_puthex(SERIAL_COM1, (unsigned int)rx_bufs);
  1411. serial_puts(SERIAL_COM1, " txb=");
  1412. serial_puthex(SERIAL_COM1, (unsigned int)tx_bufs);
  1413. serial_puts(SERIAL_COM1, "\n");
  1414. if (!rx_ring || !tx_ring || !rx_bufs || !tx_bufs) {
  1415. dbg("no memory for rings");
  1416. mmio = 0;
  1417. return -1;
  1418. }
  1419. /* The 82577 PHY is at MDIC address 1. Set slow MDIO before reading
  1420. * the ID, exactly like e1000_init_phy_params_pchlan(); without it the
  1421. * first ID read often returns 0/0xffff. */
  1422. if (pch_lan) {
  1423. phy_addr = 1;
  1424. pch_slow_mdio();
  1425. }
  1426. phy_probe();
  1427. if (pch_lan) {
  1428. dbg_hex("phy id1", (unsigned int)phy_id1);
  1429. dbg_hex("phy id2", (unsigned int)phy_id2);
  1430. if (phy_addr < 0)
  1431. phy_addr = 1;
  1432. /* phy_probe() may find the PHY at address 2 (X201); set slow
  1433. * MDIO at the address it actually answered on. */
  1434. pch_slow_mdio();
  1435. pch_phy_workarounds();
  1436. }
  1437. hw_start();
  1438. delay_ms(10);
  1439. /* The full-chip reset drops the link and the PHY has to renegotiate.
  1440. * Wait for it before net_init runs DHCP, otherwise the first DISCOVER
  1441. * is transmitted into a dead link. */
  1442. if (pch_lan && !(r32(REG_STATUS) & STATUS_LU)) {
  1443. if (!wait_link(5000))
  1444. dbg("link did not come up after reset");
  1445. }
  1446. {
  1447. unsigned int st = r32(REG_STATUS);
  1448. if (pch_lan && (st & STATUS_LU)) {
  1449. pch_link_up_workarounds();
  1450. /* The PHY reset/link transition can clear these. */
  1451. w32(REG_CTRL, r32(REG_CTRL) | CTRL_SLU);
  1452. w32(REG_RCTL, r32(REG_RCTL) | RCTL_EN);
  1453. w32(REG_TCTL, r32(REG_TCTL) | TCTL_EN);
  1454. }
  1455. dbg(st & STATUS_LU ? "link up" : "link down after reset");
  1456. last_link = !!(st & STATUS_LU);
  1457. }
  1458. log_state("state after start");
  1459. present = 1;
  1460. /* "<chip> <mac>" */
  1461. i = 0;
  1462. for (const char *p = detected_chip; *p && i + 1 < sizeof(buf); p++)
  1463. buf[i++] = *p;
  1464. buf[i++] = ' ';
  1465. for (unsigned int j = 0; j < 6 && i + 4 < sizeof(buf); j++) {
  1466. static const char hx[] = "0123456789ABCDEF";
  1467. buf[i++] = hx[(mac[j] >> 4) & 0xF];
  1468. buf[i++] = hx[mac[j] & 0xF];
  1469. if (j != 5)
  1470. buf[i++] = ':';
  1471. }
  1472. buf[i] = '\0';
  1473. dbg(buf);
  1474. if (!(r32(REG_STATUS) & STATUS_LU))
  1475. phy_autoneg();
  1476. tx_ensure_enabled();
  1477. return 0;
  1478. }
  1479. static void e1000e_get_mac(unsigned char out[6])
  1480. {
  1481. unsigned int i;
  1482. for (i = 0; i < 6; i++)
  1483. out[i] = mac[i];
  1484. }
  1485. /* Give a descriptor back to the hardware. The buffer address is rewritten
  1486. * every time: the extended descriptor write-back overwrites the first 8
  1487. * bytes with RSS/checksum data, so restoring it makes the ring survive even
  1488. * if firmware ever leaves RFCTL.EXTEN set. */
  1489. static void rx_rearm(unsigned int entry)
  1490. {
  1491. struct e1000e_rx_desc *d = &rx_ring[entry];
  1492. unsigned char *buf = rx_bufs + entry * E1000E_BUF;
  1493. d->addr_lo = (unsigned int)buf;
  1494. d->addr_hi = 0;
  1495. d->status_error = 0;
  1496. d->length = 0;
  1497. d->vlan = 0;
  1498. __sync_synchronize();
  1499. w32(REG_RDT, entry);
  1500. }
  1501. static int e1000e_rx(unsigned char *buf, unsigned int maxlen)
  1502. {
  1503. unsigned int guard = 0;
  1504. if (!present || !mmio)
  1505. return -1;
  1506. link_check();
  1507. while (guard++ < 8) {
  1508. unsigned int entry = rx_cur;
  1509. struct e1000e_rx_desc *d = &rx_ring[entry];
  1510. unsigned int se = d->status_error;
  1511. unsigned int status = se & 0xFFu;
  1512. unsigned int errors = (se >> 24) & 0xFFu;
  1513. unsigned int len;
  1514. if (!(status & RX_DD))
  1515. return -1; /* nothing pending */
  1516. __sync_synchronize();
  1517. last_rx_status = status;
  1518. last_rx_errors = errors;
  1519. if ((status & RX_EOP) && errors == 0) {
  1520. len = d->length;
  1521. if (len >= 14 && len <= maxlen) {
  1522. unsigned char *src = rx_bufs +
  1523. entry * E1000E_BUF;
  1524. unsigned int i;
  1525. for (i = 0; i < len; i++)
  1526. buf[i] = src[i];
  1527. for (i = 0; i < 6; i++)
  1528. last_rx_dst[i] = buf[i];
  1529. last_rx_len = len;
  1530. if (buf[0] == 0xFF && buf[1] == 0xFF &&
  1531. buf[2] == 0xFF && buf[3] == 0xFF &&
  1532. buf[4] == 0xFF && buf[5] == 0xFF)
  1533. rx_bcast++;
  1534. else if (buf[0] & 0x01)
  1535. rx_mcast++;
  1536. else
  1537. rx_ucast++;
  1538. rx_ok++;
  1539. rx_rearm(entry);
  1540. rx_cur = (entry + 1) % E1000E_NRX;
  1541. return (int)len;
  1542. }
  1543. rx_drop++;
  1544. } else {
  1545. rx_err++;
  1546. }
  1547. /* Drop this descriptor and give it back to the hardware. */
  1548. rx_rearm(entry);
  1549. rx_cur = (entry + 1) % E1000E_NRX;
  1550. }
  1551. return -1;
  1552. }
  1553. /* One-shot snapshot of the transmit state on the first timeout. On the X201
  1554. * the queue never moved at all, so the interesting values are TDH/TDT,
  1555. * TCTL, TXDCTL and the first descriptor's command word. */
  1556. static void tx_log_timeout(void)
  1557. {
  1558. if (tx_timeout_logged)
  1559. return;
  1560. tx_timeout_logged = 1;
  1561. dbg("TX timeout");
  1562. dbg_hex(" tctl", r32(REG_TCTL));
  1563. dbg_hex(" tdh", r32(REG_TDH));
  1564. dbg_hex(" tdt", r32(REG_TDT));
  1565. dbg_hex(" txdctl", r32(REG_TXDCTL0));
  1566. dbg_hex(" tdbal", r32(REG_TDBAL));
  1567. dbg_hex(" tdlen", r32(REG_TDLEN));
  1568. dbg_hex(" tarc0", r32(REG_TARC0));
  1569. dbg_hex(" tarc1", r32(REG_TARC1));
  1570. dbg_hex(" tx_cur", tx_cur);
  1571. dbg_hex(" desc_lo", tx_ring[tx_cur].lower);
  1572. dbg_hex(" desc_hi", tx_ring[tx_cur].upper);
  1573. }
  1574. static int tx_timeout(void)
  1575. {
  1576. tx_log_timeout();
  1577. tx_err++;
  1578. if (++tx_fail_run >= E1000E_TX_FAIL_MAX)
  1579. tx_broken = 1;
  1580. return -1;
  1581. }
  1582. /* The TX engine is not moving (TDH stuck): toggle TCTL.EN and re-arm the
  1583. * ring. Any descriptor the hardware never retired is simply dropped; the
  1584. * network stack retries the frame. This also makes the next TDT write a
  1585. * real 0 -> 1 transition again, in case the first doorbell was lost while
  1586. * the queue was disabled. */
  1587. static void tx_queue_restart(void)
  1588. {
  1589. unsigned int tctl = r32(REG_TCTL);
  1590. w32(REG_TCTL, tctl & ~TCTL_EN);
  1591. delay_ms(1);
  1592. w32(REG_TDBAL, (unsigned int)tx_ring);
  1593. w32(REG_TDBAH, 0);
  1594. w32(REG_TDLEN, (unsigned int)(E1000E_NTX *
  1595. sizeof(struct e1000e_tx_desc)));
  1596. w32(REG_TDH, 0);
  1597. w32(REG_TDT, 0);
  1598. w32(REG_TCTL, tctl | TCTL_EN);
  1599. __sync_synchronize();
  1600. tx_cur = 0;
  1601. tx_pending = 0;
  1602. }
  1603. /* The MAC may clear TCTL.EN on a link transition; make sure it is set before
  1604. * a send. Returns 1 when it had to be re-enabled. */
  1605. static int tx_ensure_enabled(void)
  1606. {
  1607. unsigned int tctl = r32(REG_TCTL);
  1608. if (tctl & TCTL_EN)
  1609. return 0;
  1610. w32(REG_TCTL, tctl | TCTL_EN);
  1611. tctl_fixes++;
  1612. return 1;
  1613. }
  1614. static int e1000e_tx(const unsigned char *buf, unsigned int len)
  1615. {
  1616. static unsigned char pad[60];
  1617. struct e1000e_tx_desc *d;
  1618. unsigned int entry, t0;
  1619. const unsigned char *p = buf;
  1620. unsigned int txlen = len;
  1621. if (!present || !mmio)
  1622. return -1;
  1623. link_check();
  1624. /* Once TX has failed repeatedly, fail instantly: the netlog flush and
  1625. * the DHCP/ping loops must not hold the BKL for 100 ms per frame on a
  1626. * NIC whose TX does not complete. `netreset` clears this. */
  1627. if (tx_broken)
  1628. return -1;
  1629. if (len < 14 || len > 1514)
  1630. return -1;
  1631. tx_ensure_enabled();
  1632. if (len < 60) {
  1633. unsigned int i;
  1634. for (i = 0; i < len; i++)
  1635. pad[i] = buf[i];
  1636. for (i = len; i < 60; i++)
  1637. pad[i] = 0;
  1638. p = pad;
  1639. txlen = 60;
  1640. }
  1641. entry = tx_cur;
  1642. d = &tx_ring[entry];
  1643. /* A previous command on this slot that timed out may still be in
  1644. * flight; wait for it before reuse. */
  1645. if (tx_pending & (1u << entry)) {
  1646. t0 = get_ticks_ms();
  1647. while (!(d->upper & TX_DD)) {
  1648. if (timed_out(t0, E1000E_TX_TIMEOUT_MS)) {
  1649. /* The hardware never retired this
  1650. * descriptor: restart the TX queue and
  1651. * drop the slot. */
  1652. tx_queue_restart();
  1653. return tx_timeout();
  1654. }
  1655. }
  1656. tx_pending &= ~(1u << entry);
  1657. }
  1658. {
  1659. unsigned char *dst = tx_bufs + entry * E1000E_BUF;
  1660. unsigned int i;
  1661. for (i = 0; i < txlen; i++)
  1662. dst[i] = p[i];
  1663. }
  1664. d->addr_lo = (unsigned int)(tx_bufs + entry * E1000E_BUF);
  1665. d->addr_hi = 0;
  1666. d->lower = txlen | (TX_CMD << 24);
  1667. d->upper = 0;
  1668. __sync_synchronize();
  1669. tx_pending |= 1u << entry;
  1670. w32(REG_TDT, (entry + 1) % E1000E_NTX);
  1671. t0 = get_ticks_ms();
  1672. while (!(d->upper & TX_DD)) {
  1673. if (timed_out(t0, E1000E_TX_TIMEOUT_MS)) {
  1674. /* Leave the pending bit set: the next TX on this slot
  1675. * waits for it before reuse. */
  1676. return tx_timeout();
  1677. }
  1678. }
  1679. last_tx_status = d->upper;
  1680. tx_pending &= ~(1u << entry);
  1681. tx_fail_run = 0;
  1682. tx_ok++;
  1683. tx_cur = (entry + 1) % E1000E_NTX;
  1684. return 0;
  1685. }
  1686. static void e1000e_print_info(void)
  1687. {
  1688. unsigned int st, bmcr = 0, bmsr = 0;
  1689. if (!present || !mmio) {
  1690. io_putstr("e1000e: no NIC\n");
  1691. return;
  1692. }
  1693. st = r32(REG_STATUS);
  1694. io_putstr("chip: ");
  1695. io_putstr(detected_chip);
  1696. io_putstr("\npci ");
  1697. put_dec(pci_bus); io_putchar(':');
  1698. put_dec(pci_dev); io_putchar('.');
  1699. put_dec(pci_func);
  1700. io_putstr(" bar0=0x");
  1701. {
  1702. static const char hx[] = "0123456789ABCDEF";
  1703. unsigned int i;
  1704. for (i = 0; i < 8; i++)
  1705. io_putchar(hx[(bar_base >> (4 * (7 - i))) & 0xF]);
  1706. }
  1707. io_putstr(" mac ");
  1708. put_mac(mac);
  1709. io_putstr("\nlink ");
  1710. if (st & STATUS_LU) {
  1711. io_putstr((st & STATUS_FD) ? "up full" : "up half");
  1712. io_putstr(" ");
  1713. switch (st & STATUS_SPEED) {
  1714. case 0x00: io_putstr("10M"); break;
  1715. case 0x40: io_putstr("100M"); break;
  1716. case 0x80: io_putstr("1000M"); break;
  1717. default: io_putstr("?M"); break;
  1718. }
  1719. } else {
  1720. io_putstr("down");
  1721. }
  1722. io_putstr(" status=0x");
  1723. io_puthex(st);
  1724. io_putstr("\nphy ");
  1725. if (phy_addr >= 0) {
  1726. int lock = pch_swflag_acquire() == 0;
  1727. phy_set_page(0);
  1728. put_dec((unsigned int)phy_addr);
  1729. io_putstr(" id ");
  1730. put_hex2((unsigned int)(phy_id1 >> 8) & 0xFF);
  1731. put_hex2((unsigned int)phy_id1 & 0xFF);
  1732. io_putchar(':');
  1733. put_hex2((unsigned int)(phy_id2 >> 8) & 0xFF);
  1734. put_hex2((unsigned int)phy_id2 & 0xFF);
  1735. bmcr = (unsigned int)mdic_read((unsigned int)phy_addr, MII_BMCR);
  1736. bmsr = (unsigned int)mdic_read((unsigned int)phy_addr, MII_BMSR);
  1737. io_putstr(" bmcr=0x");
  1738. io_puthex(bmcr);
  1739. io_putstr(" bmsr=0x");
  1740. io_puthex(bmsr);
  1741. if (lock)
  1742. pch_swflag_release();
  1743. } else {
  1744. io_putstr("none");
  1745. }
  1746. io_putstr("txdctl=0x");
  1747. io_puthex(r32(REG_TXDCTL0));
  1748. io_putstr(" tarc=0x");
  1749. io_puthex(r32(REG_TARC0));
  1750. io_putstr(" gcr=0x");
  1751. io_puthex(r32(REG_GCR));
  1752. io_putstr(" rfctl=0x");
  1753. io_puthex(r32(REG_RFCTL));
  1754. io_putstr(" rxcsum=0x");
  1755. io_puthex(r32(REG_RXCSUM));
  1756. io_putstr(" rxdctl=0x");
  1757. io_puthex(r32(REG_RXDCTL0));
  1758. io_putstr("\nextcnf=0x");
  1759. io_puthex(r32(REG_EXTCNF_CTRL));
  1760. io_putstr(" fwsm=0x");
  1761. io_puthex(r32(REG_FWSM));
  1762. io_putstr(" pba=0x");
  1763. io_puthex(r32(REG_PBA));
  1764. io_putstr(" pbs=0x");
  1765. io_puthex(r32(REG_PBS));
  1766. io_putstr(" tdh=0x");
  1767. io_puthex(r32(REG_TDH));
  1768. io_putstr(" tdt=0x");
  1769. io_puthex(r32(REG_TDT));
  1770. io_putchar('\n');
  1771. io_putstr("rx ok ");
  1772. put_dec(rx_ok);
  1773. io_putstr(" uc ");
  1774. put_dec(rx_ucast);
  1775. io_putstr(" bc ");
  1776. put_dec(rx_bcast);
  1777. io_putstr(" mc ");
  1778. put_dec(rx_mcast);
  1779. io_putstr(" err ");
  1780. put_dec(rx_err);
  1781. io_putstr(" drop ");
  1782. put_dec(rx_drop);
  1783. io_putstr(" tx ok ");
  1784. put_dec(tx_ok);
  1785. io_putstr(" err ");
  1786. put_dec(tx_err);
  1787. io_putstr(" broken ");
  1788. put_dec((unsigned int)tx_broken);
  1789. io_putstr(" tctl_fix ");
  1790. put_dec(tctl_fixes);
  1791. io_putstr(" resets ");
  1792. put_dec(reset_count);
  1793. io_putchar('\n');
  1794. }
  1795. static void e1000e_dump_rx(void)
  1796. {
  1797. io_putstr("[e1000e rx] ok=");
  1798. put_dec(rx_ok);
  1799. io_putstr(" uc=");
  1800. put_dec(rx_ucast);
  1801. io_putstr(" bc=");
  1802. put_dec(rx_bcast);
  1803. io_putstr(" mc=");
  1804. put_dec(rx_mcast);
  1805. io_putstr(" err=");
  1806. put_dec(rx_err);
  1807. io_putstr(" drop=");
  1808. put_dec(rx_drop);
  1809. io_putstr(" tx=");
  1810. put_dec(tx_ok);
  1811. io_putchar('\n');
  1812. io_putstr(" last status=0x");
  1813. io_puthex(last_rx_status);
  1814. io_putstr(" err=0x");
  1815. io_puthex(last_rx_errors);
  1816. io_putstr(" len=");
  1817. put_dec(last_rx_len);
  1818. io_putstr(" dst=");
  1819. put_mac(last_rx_dst);
  1820. io_putchar('\n');
  1821. }
  1822. static void e1000e_dump_regs(void)
  1823. {
  1824. static const struct {
  1825. const char *name;
  1826. unsigned int reg;
  1827. } regs[] = {
  1828. { "CTRL", REG_CTRL }, { "STATUS", REG_STATUS },
  1829. { "CTRL_EXT", REG_CTRL_EXT }, { "EERD", REG_EERD },
  1830. { "MDIC", REG_MDIC }, { "ICR", REG_ICR },
  1831. { "PBA", REG_PBA }, { "WUC", REG_WUC },
  1832. { "PBS", REG_PBS }, { "RCTL", REG_RCTL },
  1833. { "TCTL", REG_TCTL },
  1834. { "TIPG", REG_TIPG }, { "RAL0", REG_RAL0 },
  1835. { "RAH0", REG_RAH0 }, { "RDBAL", REG_RDBAL },
  1836. { "RDLEN", REG_RDLEN }, { "RDH", REG_RDH },
  1837. { "RDT", REG_RDT }, { "TDBAL", REG_TDBAL },
  1838. { "TDLEN", REG_TDLEN }, { "TDH", REG_TDH },
  1839. { "TDT", REG_TDT }, { "TXDCTL0", REG_TXDCTL0 },
  1840. { "TXDCTL1", REG_TXDCTL1 }, { "TARC0", REG_TARC0 },
  1841. { "TARC1", REG_TARC1 }, { "GCR", REG_GCR },
  1842. { "RXCSUM", REG_RXCSUM }, { "RFCTL", REG_RFCTL },
  1843. { "RXDCTL0", REG_RXDCTL0 }, { "RXDCTL1", REG_RXDCTL1 },
  1844. { "EXTCNF", REG_EXTCNF_CTRL }, { "FWSM", REG_FWSM },
  1845. { "KABGTXD", REG_KABGTXD }, { "CRC_OFF", REG_CRC_OFFSET },
  1846. };
  1847. unsigned int i;
  1848. if (!present || !mmio) {
  1849. io_putstr("e1000e: no NIC\n");
  1850. return;
  1851. }
  1852. for (i = 0; i < sizeof(regs) / sizeof(regs[0]); i++) {
  1853. io_putstr(regs[i].name);
  1854. io_putchar('=');
  1855. io_puthex(r32(regs[i].reg));
  1856. if ((i & 1) == 1)
  1857. io_putchar('\n');
  1858. else
  1859. io_putstr(" ");
  1860. }
  1861. if (i & 1)
  1862. io_putchar('\n');
  1863. io_putstr("rings rx=0x");
  1864. io_puthex((unsigned int)rx_ring);
  1865. io_putstr(" tx=0x");
  1866. io_puthex((unsigned int)tx_ring);
  1867. io_putstr(" rx_cur=");
  1868. put_dec(rx_cur);
  1869. io_putstr(" tx_cur=");
  1870. put_dec(tx_cur);
  1871. io_putstr(" tx_pending=0x");
  1872. io_puthex(tx_pending);
  1873. io_putchar('\n');
  1874. io_putstr("state rx_ok=");
  1875. put_dec(rx_ok);
  1876. io_putstr(" rx_err=");
  1877. put_dec(rx_err);
  1878. io_putstr(" rx_drop=");
  1879. put_dec(rx_drop);
  1880. io_putstr(" tx_ok=");
  1881. put_dec(tx_ok);
  1882. io_putstr(" tx_err=");
  1883. put_dec(tx_err);
  1884. io_putstr(" tx_broken=");
  1885. put_dec((unsigned int)tx_broken);
  1886. io_putstr(" tx_fail_run=");
  1887. put_dec(tx_fail_run);
  1888. io_putstr(" tctl_fix=");
  1889. put_dec(tctl_fixes);
  1890. io_putstr(" reset=");
  1891. put_dec(reset_count);
  1892. io_putchar('\n');
  1893. /* Read-clear hardware counters. CRCERRS/RXERRC/MPC reveal a dirty
  1894. * 1000M link even when the descriptor error byte is zero; BPRC/BPTC
  1895. * show whether DHCP/ARP broadcasts are actually moving. */
  1896. {
  1897. unsigned int crcerrs = r32(REG_CRCERRS);
  1898. unsigned int rxerrc = r32(REG_RXERRC);
  1899. unsigned int mpc = r32(REG_MPC);
  1900. unsigned int gprc = r32(REG_GPRC);
  1901. unsigned int bprc = r32(REG_BPRC);
  1902. unsigned int mprc = r32(REG_MPRC);
  1903. unsigned int ruc = r32(REG_RUC);
  1904. unsigned int gptc = r32(REG_GPTC);
  1905. unsigned int bptc = r32(REG_BPTC);
  1906. io_putstr("hw crcerr=");
  1907. put_dec(crcerrs);
  1908. io_putstr(" rxerr=");
  1909. put_dec(rxerrc);
  1910. io_putstr(" mpc=");
  1911. put_dec(mpc);
  1912. io_putstr(" gprc=");
  1913. put_dec(gprc);
  1914. io_putstr(" bprc=");
  1915. put_dec(bprc);
  1916. io_putstr(" mprc=");
  1917. put_dec(mprc);
  1918. io_putstr(" ruc=");
  1919. put_dec(ruc);
  1920. io_putstr(" gptc=");
  1921. put_dec(gptc);
  1922. io_putstr(" bptc=");
  1923. put_dec(bptc);
  1924. io_putchar('\n');
  1925. }
  1926. io_putstr("last rx status=0x");
  1927. io_puthex(last_rx_status);
  1928. io_putstr(" errors=0x");
  1929. io_puthex(last_rx_errors);
  1930. io_putstr(" tx status=0x");
  1931. io_puthex(last_tx_status);
  1932. io_putchar('\n');
  1933. if (tx_ring) {
  1934. io_putstr("tx0 addr=0x");
  1935. io_puthex(tx_ring[0].addr_lo);
  1936. io_putstr(" lower=0x");
  1937. io_puthex(tx_ring[0].lower);
  1938. io_putstr(" upper=0x");
  1939. io_puthex(tx_ring[0].upper);
  1940. io_putchar('\n');
  1941. }
  1942. if (rx_ring) {
  1943. io_putstr("rx0 addr=0x");
  1944. io_puthex(rx_ring[0].addr_lo);
  1945. io_putstr(" len=0x");
  1946. io_puthex(rx_ring[0].length);
  1947. io_putstr(" status_error=0x");
  1948. io_puthex(rx_ring[0].status_error);
  1949. io_putstr(" vlan=0x");
  1950. io_puthex(rx_ring[0].vlan);
  1951. io_putchar('\n');
  1952. }
  1953. }
  1954. static void e1000e_reset(void)
  1955. {
  1956. if (!present || !mmio || !rx_ring) {
  1957. io_putstr("e1000e: reset: not initialized\n");
  1958. return;
  1959. }
  1960. pch_claim();
  1961. if (hw_reset() != 0) {
  1962. io_putstr("e1000e: reset timeout\n");
  1963. return;
  1964. }
  1965. pch_claim();
  1966. read_mac();
  1967. hw_start();
  1968. {
  1969. unsigned int st = r32(REG_STATUS);
  1970. if (pch_lan && (st & STATUS_LU)) {
  1971. pch_link_up_workarounds();
  1972. w32(REG_CTRL, r32(REG_CTRL) | CTRL_SLU);
  1973. w32(REG_RCTL, r32(REG_RCTL) | RCTL_EN);
  1974. w32(REG_TCTL, r32(REG_TCTL) | TCTL_EN);
  1975. }
  1976. last_link = !!(st & STATUS_LU);
  1977. if (!(st & STATUS_LU))
  1978. phy_autoneg();
  1979. }
  1980. tx_ensure_enabled();
  1981. io_putstr("e1000e: netreset done\n");
  1982. }
  1983. static const unsigned char e1000e_default_ip[4] = { 192, 168, 178, 240 };
  1984. static const unsigned char e1000e_default_mask[4] = { 0, 0, 0, 0 };
  1985. const struct netdev e1000e_netdev = {
  1986. "e1000e", "Intel e1000e",
  1987. e1000e_init, e1000e_get_mac, e1000e_rx, e1000e_tx, e1000e_print_info,
  1988. e1000e_dump_regs, 0, e1000e_reset, e1000e_set_speed, e1000e_set_mac,
  1989. e1000e_default_ip, e1000e_default_mask, e1000e_dump_rx,
  1990. e1000e_stop,
  1991. };