e1000e.c 64 KB

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