e1000e.c 63 KB

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