e1000e.c 61 KB

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