e1000e.c 64 KB

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