e1000e.c 65 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. * Mapped BAR0 (128 KB on the 82577LM) is above the 128 MB identity map, so
  11. * process_map_mmio_all() installs it in every process page directory: kernel
  12. * code runs with the current process's CR3 and the network syscalls follow
  13. * the user process.
  14. */
  15. #include "e1000e.h"
  16. #include "bootcon.h"
  17. #include "io.h"
  18. #include "mm.h"
  19. #include "netdev.h"
  20. #include "paging.h"
  21. #include "pci.h"
  22. #include "process.h"
  23. #include "serial.h"
  24. #include "timer.h"
  25. /* ---- register offsets (BAR0) ---- */
  26. #define REG_CTRL 0x00000
  27. #define REG_STATUS 0x00008
  28. #define REG_EERD 0x00014
  29. #define REG_CTRL_EXT 0x00018
  30. #define REG_MDIC 0x00020
  31. #define REG_PBA 0x01000
  32. #define REG_PBS 0x01008
  33. #define REG_WUC 0x05800
  34. #define REG_VET 0x00038
  35. #define REG_ICR 0x000C0
  36. #define REG_IMC 0x000D8
  37. #define REG_RCTL 0x00100
  38. #define REG_TCTL 0x00400
  39. #define REG_TIPG 0x00410
  40. #define REG_RDBAL 0x02800
  41. #define REG_RDBAH 0x02804
  42. #define REG_RDLEN 0x02808
  43. #define REG_RDH 0x02810
  44. #define REG_RDT 0x02818
  45. #define REG_RDTR 0x02820
  46. #define REG_RADV 0x0282C
  47. #define REG_TDBAL 0x03800
  48. #define REG_TDBAH 0x03804
  49. #define REG_TDLEN 0x03808
  50. #define REG_TDH 0x03810
  51. #define REG_TDT 0x03818
  52. #define REG_TXDCTL0 0x03828
  53. #define REG_TXDCTL1 0x03928
  54. #define REG_TARC0 0x03840
  55. #define REG_TARC1 0x03940
  56. #define REG_RAL0 0x05400
  57. #define REG_RAH0 0x05404
  58. #define REG_EXTCNF_CTRL 0x00F00
  59. #define REG_KMRNCTRLSTA 0x00034
  60. #define REG_KABGTXD 0x03004
  61. #define REG_RXCSUM 0x05000
  62. #define REG_RFCTL 0x05008
  63. #define REG_RXDCTL0 0x02828
  64. #define REG_RXDCTL1 0x02928
  65. #define REG_FWSM 0x05B54
  66. #define REG_CRC_OFFSET 0x05F50
  67. #define REG_GCR 0x05B00
  68. /* Hardware statistics counters (read-clear). These are the ones that tell
  69. * whether the PHY/link is dropping frames: CRC/alignment errors, missed
  70. * packets (RX FIFO full) and the broadcast counters DHCP/ARP depend on. */
  71. #define REG_CRCERRS 0x04000
  72. #define REG_RXERRC 0x0400C
  73. #define REG_MPC 0x04010
  74. #define REG_GPRC 0x04064
  75. #define REG_BPRC 0x04068
  76. #define REG_MPRC 0x0406C
  77. #define REG_GPTC 0x04070
  78. #define REG_RUC 0x04088
  79. #define REG_BPTC 0x040F4
  80. /* GCR no-snoop enables: 1 = the NIC bypasses the CPU cache for that DMA
  81. * type. A firmware that leaves the TX bits set makes the NIC read freshly
  82. * written TX descriptors from stale DRAM, so TX never completes (RX still
  83. * works because its descriptor lines were evicted long before the traffic).
  84. * Linux clears these on every ich8/pch part. */
  85. #define GCR_NO_SNOOP 0x0000003Fu
  86. /* ---- CTRL ---- */
  87. #define CTRL_SLU 0x00000040u
  88. #define CTRL_RST 0x04000000u
  89. #define CTRL_GIO_MASTER_DISABLE 0x00000004u
  90. #define CTRL_FRCSPD 0x00000800u
  91. #define CTRL_FRCDPX 0x00001000u
  92. /* ---- STATUS ---- */
  93. #define STATUS_FD 0x00000001u
  94. #define STATUS_LU 0x00000002u
  95. #define STATUS_SPEED 0x000000C0u
  96. #define STATUS_LAN_INIT_DONE 0x00000200u
  97. #define STATUS_PHYRA 0x00000400u
  98. #define STATUS_GIO_MASTER_ENABLE 0x00080000u
  99. /* ---- RCTL ---- */
  100. #define RCTL_EN 0x00000002u
  101. #define RCTL_UPE 0x00000008u
  102. #define RCTL_MPE 0x00000010u
  103. #define RCTL_LPE 0x00000020u
  104. #define RCTL_BAM 0x00008000u
  105. #define RCTL_SECRC 0x04000000u
  106. #define RCTL_RDMTS_HALF 0x00000100u
  107. /* ---- RXCSUM ---- */
  108. #define RXCSUM_TUOFL 0x00000200u
  109. #define RXCSUM_IPPCSE 0x00001000u
  110. /* ---- RFCTL ---- */
  111. #define RFCTL_NFSW_DIS 0x00000040u
  112. #define RFCTL_NFSR_DIS 0x00000080u
  113. #define RFCTL_EXTEN 0x00008000u
  114. #define RFCTL_IPV6_EX_DIS 0x00010000u
  115. #define RFCTL_NEW_IPV6_EXT_DIS 0x00020000u
  116. /* ---- TCTL ---- */
  117. #define TCTL_EN 0x00000002u
  118. #define TCTL_PSP 0x00000008u
  119. #define TCTL_RTLC 0x01000000u
  120. #define TCTL_CT_SHIFT 4
  121. #define TCTL_COLD_SHIFT 12
  122. #define TCTL_CT 0x0Fu
  123. #define TCTL_COLD 0x3Fu
  124. #define TCTL_CT_MASK 0x00000FF0u
  125. #define TCTL_COLD_MASK 0x003FF000u
  126. /* ---- TXDCTL (PCH LAN: descriptor write-back policy) ---- */
  127. #define TXDCTL_PTHRESH 0x0000003Fu
  128. #define TXDCTL_WTHRESH 0x003F0000u
  129. #define TXDCTL_COUNT_DESC 0x00400000u
  130. #define TXDCTL_GRAN 0x01000000u
  131. #define TXDCTL_WB_ONE 0x01010000u /* GRAN | WTHRESH=1 */
  132. #define TXDCTL_PREFETCH 0x0100001Fu /* GRAN | PTHRESH=31 */
  133. #define TXDCTL_HTHRESH 0x00000100u /* host threshold = 1 */
  134. /* ---- RXDCTL (PCH LAN: descriptor write-back policy) ---- */
  135. #define RXDCTL_PTHRESH 0x0000003Fu
  136. #define RXDCTL_HTHRESH 0x00003F00u
  137. #define RXDCTL_WTHRESH 0x003F0000u
  138. #define RXDCTL_GRAN 0x01000000u
  139. /* ---- MDIC ---- */
  140. #define MDIC_TIMEOUT_MS 100 /* Linux polls up to ~96 ms; slow MDIO needs it */
  141. #define MDIC_DATA_MASK 0x0000FFFFu
  142. #define MDIC_REG_SHIFT 16
  143. #define MDIC_PHY_SHIFT 21
  144. #define MDIC_OP_WRITE 0x04000000u
  145. #define MDIC_OP_READ 0x08000000u
  146. #define MDIC_READY 0x10000000u
  147. #define MDIC_ERROR 0x40000000u
  148. /* ---- EERD ---- */
  149. #define EERD_START 0x00000001u
  150. #define EERD_DONE 0x00000010u
  151. #define EERD_ADDR_SHIFT 8
  152. #define EERD_DATA_SHIFT 16
  153. /* ---- descriptor status ---- */
  154. #define RX_DD 0x01
  155. #define RX_EOP 0x02
  156. #define TX_DD 0x01
  157. #define TX_CMD (0x01u | 0x02u | 0x08u) /* EOP | IFCS | RS */
  158. /* ---- rings ---- */
  159. #define E1000E_NRX 64
  160. #define E1000E_NTX 16
  161. #define E1000E_BUF 2048
  162. #define E1000E_TX_TIMEOUT_MS 20
  163. #define E1000E_TX_FAIL_MAX 32
  164. /* ---- PHY / MII ---- */
  165. #define MII_BMCR 0
  166. #define MII_BMSR 1
  167. #define MII_PHYID1 2
  168. #define MII_PHYID2 3
  169. #define MII_ANAR 4
  170. #define MII_GBSR 9
  171. #define BMCR_ANENABLE 0x1000
  172. #define BMCR_ANRESTART 0x0200
  173. /* ---- PCI ---- */
  174. #define PCI_CMD_INT_DISABLE 0x0400u /* command bit 10: INTx disabled */
  175. #define E1000E_VENDOR 0x8086u
  176. /* Extended RX descriptor (16 bytes), the layout the e1000e/PCH parts use
  177. * when RFCTL.EXTEN is set: the hardware overwrites the buffer address with
  178. * MRQ/RSS data and writes status/error, length and VLAN tag back at offsets
  179. * 8, 12 and 14. The legacy 16-byte layout puts length at 8, checksum at
  180. * 10, status at 12 and errors at 13 instead; `rx_ext` selects the decode. */
  181. struct e1000e_rx_desc {
  182. volatile unsigned int addr_lo;
  183. volatile unsigned int addr_hi;
  184. volatile unsigned int status_error;
  185. volatile unsigned short length;
  186. volatile unsigned short vlan;
  187. } __attribute__((packed));
  188. struct e1000e_tx_desc {
  189. volatile unsigned int addr_lo;
  190. volatile unsigned int addr_hi;
  191. volatile unsigned int lower;
  192. volatile unsigned int upper;
  193. } __attribute__((packed));
  194. static unsigned char *mmio;
  195. static int present;
  196. static int inited;
  197. static unsigned char mac[6];
  198. static unsigned int bar_base, bar_size;
  199. static unsigned int pci_bus, pci_dev, pci_func;
  200. static unsigned int device_id;
  201. static const char *detected_chip = "Intel e1000e";
  202. static struct e1000e_rx_desc *rx_ring;
  203. static struct e1000e_tx_desc *tx_ring;
  204. static unsigned char *rx_bufs;
  205. static unsigned char *tx_bufs;
  206. static unsigned int rx_cur;
  207. /* 1 when RFCTL.EXTEN stuck (PCH parts): status/error at offset 8, length
  208. * at 12. 0 for MACs without RFCTL (the 82540EM, whatever RFCTL reads):
  209. * legacy write-back, length at 8, status at 12, errors at 13. */
  210. static int rx_ext;
  211. static unsigned int tx_cur;
  212. static unsigned int tx_pending; /* bitmask: submitted, DD not seen yet */
  213. static unsigned int tx_fail_run; /* consecutive TX timeouts */
  214. static int tx_broken; /* stop stalling the system on a dead TX */
  215. static unsigned int tctl_fixes; /* TCTL.EN re-asserts */
  216. static int last_link; /* last STATUS.LU sampled, PCH only */
  217. static unsigned int link_poll_ms;
  218. static int tx_timeout_logged;
  219. static void tx_queue_restart(void);
  220. static int tx_ensure_enabled(void);
  221. static void phy_autoneg(void);
  222. static int pch_swflag_acquire(void);
  223. static void pch_swflag_release(void);
  224. static int phy_addr = -1;
  225. static int phy_id1 = -1, phy_id2 = -1;
  226. static int pch_lan; /* 82577/82578/82579: PCH LAN/Kumeran PHY */
  227. static int force_speed; /* 0 = auto, 100 = advertise 100M only */
  228. /* Counters (also reported by `netregs`). */
  229. static unsigned int tx_ok, tx_err, rx_ok, rx_err, rx_drop;
  230. static unsigned int rx_ucast, rx_bcast, rx_mcast;
  231. static unsigned char last_rx_dst[6];
  232. static unsigned int last_rx_len;
  233. static unsigned int last_tx_status, last_rx_status, last_rx_errors;
  234. static unsigned int reset_count;
  235. static int timed_out(unsigned int start_ms, unsigned int ms)
  236. {
  237. return get_ticks_ms() - start_ms > ms;
  238. }
  239. static void delay_ms(unsigned int ms)
  240. {
  241. unsigned int t0 = get_ticks_ms();
  242. while (get_ticks_ms() - t0 < ms)
  243. ;
  244. }
  245. /* Some network syscalls enter with interrupts off; get_ticks_ms() would be
  246. * frozen there, so the link workarounds (which use delay_ms and MDIC
  247. * timeouts) must only run in a context where the PIT is ticking. */
  248. static int irqs_on(void)
  249. {
  250. unsigned int flags;
  251. __asm__ volatile ("pushfl; popl %0" : "=r"(flags));
  252. return (flags & 0x200u) != 0;
  253. }
  254. /* ---- serial debug trail (the netlog mirror carries it to `make netmon`) ---- */
  255. static void dbg(const char *s)
  256. {
  257. serial_puts(SERIAL_COM1, "e1000e: ");
  258. serial_puts(SERIAL_COM1, s);
  259. serial_puts(SERIAL_COM1, "\n");
  260. /* Also show the trail on the boot console (NOWM diagnostics): if the
  261. * probe wedges, the last line says which step. No-op once the WM has
  262. * taken over and disabled the console. */
  263. bootcon_puts("e1000e: ");
  264. bootcon_puts(s);
  265. bootcon_puts("\n");
  266. }
  267. /* "name=0x12345678" through dbg(), so the value is visible on the boot
  268. * console too (make nowm) and not only on serial/netlog. */
  269. static void dbg_hex(const char *name, unsigned int v)
  270. {
  271. static const char hx[] = "0123456789ABCDEF";
  272. char buf[48];
  273. unsigned int i = 0;
  274. int shift;
  275. while (*name && i < 24)
  276. buf[i++] = *name++;
  277. buf[i++] = '=';
  278. buf[i++] = '0';
  279. buf[i++] = 'x';
  280. for (shift = 28; shift >= 0; shift -= 4)
  281. buf[i++] = hx[(v >> shift) & 0xF];
  282. buf[i] = '\0';
  283. dbg(buf);
  284. }
  285. static void put_dec(unsigned int v)
  286. {
  287. char b[12];
  288. int i = 0;
  289. if (v == 0) {
  290. io_putchar('0');
  291. return;
  292. }
  293. while (v) {
  294. b[i++] = (char)('0' + v % 10);
  295. v /= 10;
  296. }
  297. while (i)
  298. io_putchar(b[--i]);
  299. }
  300. static void put_hex2(unsigned int v)
  301. {
  302. static const char hx[] = "0123456789ABCDEF";
  303. io_putchar(hx[(v >> 4) & 0xF]);
  304. io_putchar(hx[v & 0xF]);
  305. }
  306. static void put_mac(const unsigned char *m)
  307. {
  308. unsigned int i;
  309. for (i = 0; i < 6; i++) {
  310. put_hex2(m[i]);
  311. if (i != 5)
  312. io_putchar(':');
  313. }
  314. }
  315. /* ---- MMIO accessors (uncacheable mapping) ---- */
  316. static inline unsigned int r32(unsigned int reg)
  317. {
  318. return *(volatile unsigned int *)(mmio + reg);
  319. }
  320. static inline void w32(unsigned int reg, unsigned int v)
  321. {
  322. *(volatile unsigned int *)(mmio + reg) = v;
  323. }
  324. /* Snapshot the registers firmware/ME could have changed. This is the first
  325. * thing to look at on a board where DHCP fails: RFCTL.EXTEN in particular
  326. * changes the RX descriptor layout. */
  327. static void log_state(const char *tag)
  328. {
  329. dbg(tag);
  330. dbg_hex(" ctrl", r32(REG_CTRL));
  331. dbg_hex(" ctrl_ext", r32(REG_CTRL_EXT));
  332. dbg_hex(" status", r32(REG_STATUS));
  333. dbg_hex(" rfctl", r32(REG_RFCTL));
  334. dbg_hex(" rxcsum", r32(REG_RXCSUM));
  335. dbg_hex(" rxdctl", r32(REG_RXDCTL0));
  336. dbg_hex(" tctl", r32(REG_TCTL));
  337. dbg_hex(" txdctl", r32(REG_TXDCTL0));
  338. dbg_hex(" pba", r32(REG_PBA));
  339. dbg_hex(" pbs", r32(REG_PBS));
  340. dbg_hex(" extcnf", r32(REG_EXTCNF_CTRL));
  341. dbg_hex(" fwsm", r32(REG_FWSM));
  342. }
  343. /* ---- PCI discovery ---- */
  344. static int supported_device(unsigned int dev)
  345. {
  346. switch (dev) {
  347. case 0x100E: /* 82540EM: QEMU's e1000 */
  348. case 0x10D3: /* 82574L: QEMU's e1000e */
  349. case 0x10EA: /* 82577LM: ThinkPad X201 */
  350. case 0x10EB: /* 82577LC */
  351. case 0x10EF: /* 82578DM */
  352. case 0x10F0: /* 82578DC */
  353. case 0x1502: /* 82579LM */
  354. case 0x1503: /* 82579V */
  355. return 1;
  356. default:
  357. return 0;
  358. }
  359. }
  360. static const char *chip_name(unsigned int dev)
  361. {
  362. switch (dev) {
  363. case 0x100E: return "Intel 82540EM (QEMU e1000)";
  364. case 0x10D3: return "Intel 82574L";
  365. case 0x10EA: return "Intel 82577LM";
  366. case 0x10EB: return "Intel 82577LC";
  367. case 0x10EF: return "Intel 82578DM";
  368. case 0x10F0: return "Intel 82578DC";
  369. case 0x1502: return "Intel 82579LM";
  370. case 0x1503: return "Intel 82579V";
  371. default: return "Intel e1000e";
  372. }
  373. }
  374. /* PCH-integrated LAN: the PHY sits behind the Kumeran interface and needs a
  375. * large set of PHY/Kumeran workarounds after a full-chip reset. Booting the
  376. * firmware has already configured it, so the driver deliberately does a warm
  377. * init on these parts (stop our RX/TX, leave CTRL.PHY_RST alone) and only
  378. * restarts autoneg if the link is down. */
  379. static int is_pch_lan(unsigned int dev)
  380. {
  381. switch (dev) {
  382. case 0x10EA: case 0x10EB: case 0x10EF: case 0x10F0:
  383. case 0x1502: case 0x1503:
  384. return 1;
  385. default:
  386. return 0;
  387. }
  388. }
  389. /* The PHY workarounds below are the pchlan (82577/82578) ones. The 82579
  390. * is pch2lan and has a different K1/LV sequence; leave its firmware setup
  391. * alone rather than writing 82577-specific registers to it. */
  392. static int pch_phy_82577(void)
  393. {
  394. return device_id == 0x10EA || device_id == 0x10EB ||
  395. (phy_id1 == 0x0154 && phy_id2 >= 0 &&
  396. (phy_id2 & 0xFFF0) == 0x0050);
  397. }
  398. static int pch_phy_82578(void)
  399. {
  400. return device_id == 0x10EF || device_id == 0x10F0 ||
  401. phy_id1 == 0x004D;
  402. }
  403. /* BAR0 is the 128 KB register window on every supported part. Handle a
  404. * 64-bit BAR and probe its size with the usual all-ones trick. */
  405. static int probe_bar0(unsigned int bus, unsigned int dev, unsigned int func,
  406. unsigned int *base, unsigned int *size)
  407. {
  408. unsigned int val = pci_cfg_read32(bus, dev, func, 0x10);
  409. if (!val || val == 0xFFFFFFFFu || (val & 1u))
  410. return -1;
  411. /* Do NOT size the BAR by writing 0xFFFFFFFF: on PCH LAN parts the
  412. * ME/AMT can leave the BAR restore incomplete, after which every MMIO
  413. * access goes to an undecoded address and stalls. All supported
  414. * e1000/e1000e BARs are 128 KB, so trust the firmware-assigned base. */
  415. if (val & 4u)
  416. return -1; /* 64-bit BAR: not used by these parts */
  417. *base = val & 0xFFFFFFF0u;
  418. *size = 0x20000;
  419. return 0;
  420. }
  421. /* Put the function in D0 before the first MMIO access. The PCH LAN is
  422. * often left in D3hot by the BIOS/ME; MMIO while suspended can return
  423. * 0xFFFFFFFF or stall the PCIe root complex. Walk the capability list for
  424. * the PM capability (ID 1) and clear PMCSR.PS. */
  425. static void pci_set_d0(unsigned int bus, unsigned int dev, unsigned int func)
  426. {
  427. unsigned int status = pci_cfg_read32(bus, dev, func, 0x04) >> 16;
  428. unsigned int cap, guard = 0;
  429. if (!(status & 0x10u))
  430. return; /* no capability list */
  431. cap = pci_cfg_read32(bus, dev, func, 0x34) & 0xFCu;
  432. while (cap && guard++ < 48) {
  433. unsigned int hdr = pci_cfg_read32(bus, dev, func, cap);
  434. if ((hdr & 0xFFu) == 0x01u) {
  435. unsigned int pmcsr =
  436. pci_cfg_read32(bus, dev, func, cap + 4);
  437. if (pmcsr & 3u) {
  438. pci_cfg_write32(bus, dev, func, cap + 4,
  439. pmcsr & ~3u);
  440. delay_ms(10);
  441. dbg("PM state -> D0");
  442. }
  443. return;
  444. }
  445. cap = (hdr >> 8) & 0xFCu;
  446. }
  447. }
  448. /* The e1000e PCH LAN is sensitive to PCIe ASPM L1/L0s: it causes long
  449. * wake-up latency and packet loss on ThinkPads (the 82573/82577 family).
  450. * Linux's e1000e_disable_aspm() clears the ASPM enables in the Link
  451. * Control register for the affected parts. On bus 0 there is no upstream
  452. * bridge device, so clearing the function's own enables is the same thing
  453. * the Linux driver does. */
  454. static void pci_disable_aspm(unsigned int bus, unsigned int dev,
  455. unsigned int func)
  456. {
  457. unsigned int status = pci_cfg_read32(bus, dev, func, 0x04) >> 16;
  458. unsigned int cap, guard = 0;
  459. if (!(status & 0x10u))
  460. return; /* no capability list */
  461. cap = pci_cfg_read32(bus, dev, func, 0x34) & 0xFCu;
  462. while (cap && guard++ < 48) {
  463. unsigned int hdr = pci_cfg_read32(bus, dev, func, cap);
  464. if ((hdr & 0xFFu) == 0x10u) { /* PCI Express capability */
  465. unsigned int lc =
  466. pci_cfg_read32(bus, dev, func,
  467. cap + 0x10) & 0xFFFFu;
  468. if (lc & 0x3u) {
  469. dbg_hex(" PCIe linkctl before", lc);
  470. pci_cfg_write16(bus, dev, func, cap + 0x10,
  471. (unsigned short)(lc & ~0x3u));
  472. dbg_hex(" PCIe linkctl after",
  473. pci_cfg_read32(bus, dev, func,
  474. cap + 0x10) & 0xFFFFu);
  475. dbg("PCIe ASPM disabled");
  476. }
  477. return;
  478. }
  479. cap = (hdr >> 8) & 0xFCu;
  480. }
  481. }
  482. static void pci_af_flr(unsigned int bus, unsigned int dev, unsigned int func)
  483. {
  484. unsigned int status = pci_cfg_read32(bus, dev, func, 0x04) >> 16;
  485. unsigned int bar0 = pci_cfg_read32(bus, dev, func, 0x10);
  486. unsigned int cmd = pci_cfg_read32(bus, dev, func, 0x04);
  487. unsigned int cap, guard = 0;
  488. if (!(status & 0x10u))
  489. return; /* no capability list */
  490. cap = pci_cfg_read32(bus, dev, func, 0x34) & 0xFCu;
  491. while (cap && guard++ < 48) {
  492. unsigned int hdr = pci_cfg_read32(bus, dev, func, cap);
  493. if ((hdr & 0xFFu) == 0x13u) { /* PCI Advanced Features */
  494. unsigned int t0;
  495. /* AF capability byte is at cap+3 (TP|FLR). */
  496. if ((pci_cfg_read32(bus, dev, func, cap) >> 24) & 0x3u) {
  497. /* Wait for any pending transaction to drain. */
  498. t0 = get_ticks_ms();
  499. while (((pci_cfg_read32(bus, dev, func, cap + 4) >>
  500. 8) & 1u) && !timed_out(t0, 100))
  501. delay_ms(1);
  502. /* AF Control: bit 0 triggers FLR. */
  503. pci_cfg_write8(bus, dev, func, cap + 4, 1);
  504. delay_ms(100);
  505. /* Wait for the function to respond again. */
  506. t0 = get_ticks_ms();
  507. while (pci_cfg_read32(bus, dev, func, 0) == 0xFFFFFFFFu &&
  508. !timed_out(t0, 1000))
  509. delay_ms(1);
  510. /* FLR may clear BAR0/command; restore them. */
  511. if (bar0 && bar0 != 0xFFFFFFFFu)
  512. pci_cfg_write32(bus, dev, func, 0x10,
  513. bar0 & 0xFFFFFFF0u);
  514. pci_cfg_write32(bus, dev, func, 0x04,
  515. cmd | 0x0006u | PCI_CMD_INT_DISABLE);
  516. dbg("AF FLR done");
  517. }
  518. return;
  519. }
  520. cap = (hdr >> 8) & 0xFCu;
  521. }
  522. }
  523. static int probe_pci(void)
  524. {
  525. unsigned int bus, dev, func;
  526. for (bus = 0; bus < 256; bus++) {
  527. for (dev = 0; dev < 32; dev++) {
  528. unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
  529. unsigned int htype, nfunc;
  530. if (id == 0xFFFFFFFFu || id == 0)
  531. continue;
  532. htype = (pci_cfg_read32(bus, dev, 0, 0x0C) >> 16) & 0xFFu;
  533. nfunc = (htype & 0x80u) ? 8u : 1u;
  534. for (func = 0; func < nfunc; func++) {
  535. unsigned int idf, cmd;
  536. idf = pci_cfg_read32(bus, dev, func, 0x00);
  537. if (idf == 0xFFFFFFFFu || idf == 0)
  538. continue;
  539. if ((idf & 0xFFFFu) != E1000E_VENDOR)
  540. continue;
  541. if (!supported_device((idf >> 16) & 0xFFFFu))
  542. continue;
  543. /* Function Level Reset clears the RX descriptor
  544. * prefetch cache left by the previous kernel
  545. * (kexec) or firmware. Linux uses the same AF
  546. * FLR; the PCH advertises TP+FLR. */
  547. pci_af_flr(bus, dev, func);
  548. /* Memory space + bus master. */
  549. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  550. /* Polled driver: disable INTx before enabling memory
  551. * and bus master. A link-change interrupt from
  552. * the PHY can otherwise fire before hw_stop()
  553. * masks the device, and the kernel has no NIC IRQ
  554. * handler (the X201 stalls with the cable in). */
  555. pci_cfg_write32(bus, dev, func, 0x04,
  556. cmd | 0x0006u |
  557. PCI_CMD_INT_DISABLE);
  558. pci_set_d0(bus, dev, func);
  559. pci_disable_aspm(bus, dev, func);
  560. /* Let the function settle after D0/memory enable;
  561. * the ME can still be finishing its link work when
  562. * the cable is present. */
  563. delay_ms(10);
  564. if (probe_bar0(bus, dev, func,
  565. &bar_base, &bar_size) != 0)
  566. continue;
  567. pci_bus = bus;
  568. pci_dev = dev;
  569. pci_func = func;
  570. device_id = (idf >> 16) & 0xFFFFu;
  571. detected_chip = chip_name(device_id);
  572. pch_lan = is_pch_lan(device_id);
  573. return 0;
  574. }
  575. }
  576. }
  577. return -1;
  578. }
  579. /* ---- MAC address ---- */
  580. /* Classic EERD word read; used only when RAL/RAH came back invalid. */
  581. static int eeprom_read16(unsigned int addr, unsigned short *out)
  582. {
  583. unsigned int t0, v;
  584. w32(REG_EERD, (addr << EERD_ADDR_SHIFT) | EERD_START);
  585. t0 = get_ticks_ms();
  586. for (;;) {
  587. v = r32(REG_EERD);
  588. if (v & EERD_DONE) {
  589. *out = (unsigned short)(v >> EERD_DATA_SHIFT);
  590. return 0;
  591. }
  592. if (timed_out(t0, 5))
  593. return -1;
  594. }
  595. }
  596. static int mac_valid(const unsigned char *m)
  597. {
  598. unsigned int i;
  599. int zero = 1, ones = 1;
  600. for (i = 0; i < 6; i++) {
  601. if (m[i] != 0x00)
  602. zero = 0;
  603. if (m[i] != 0xFF)
  604. ones = 0;
  605. }
  606. return !zero && !ones;
  607. }
  608. /* Program the unicast filter the way Linux's e1000e_rar_set_generic()
  609. * does: RAL first, then RAH with AV last. The previous RAH-then-RAL
  610. * order could leave the exact-match filter pointing at a stale low half
  611. * while AV was already set; broadcast/multicast still worked, but unicast
  612. * to the board's own MAC was dropped. */
  613. static void write_rar(void)
  614. {
  615. w32(REG_RAL0, (unsigned int)mac[0] | ((unsigned int)mac[1] << 8) |
  616. ((unsigned int)mac[2] << 16) | ((unsigned int)mac[3] << 24));
  617. __sync_synchronize();
  618. w32(REG_RAH0, 0x80000000u | ((unsigned int)mac[5] << 8) | mac[4]);
  619. __sync_synchronize();
  620. }
  621. static void read_mac(void)
  622. {
  623. unsigned int ral = r32(REG_RAL0);
  624. unsigned int rah = r32(REG_RAH0);
  625. mac[0] = (unsigned char)(ral & 0xFF);
  626. mac[1] = (unsigned char)((ral >> 8) & 0xFF);
  627. mac[2] = (unsigned char)((ral >> 16) & 0xFF);
  628. mac[3] = (unsigned char)((ral >> 24) & 0xFF);
  629. mac[4] = (unsigned char)(rah & 0xFF);
  630. mac[5] = (unsigned char)((rah >> 8) & 0xFF);
  631. if (!mac_valid(mac)) {
  632. unsigned short w0 = 0, w1 = 0, w2 = 0;
  633. if (eeprom_read16(0, &w0) == 0 &&
  634. eeprom_read16(1, &w1) == 0 &&
  635. eeprom_read16(2, &w2) == 0) {
  636. mac[0] = (unsigned char)(w0 & 0xFF);
  637. mac[1] = (unsigned char)(w0 >> 8);
  638. mac[2] = (unsigned char)(w1 & 0xFF);
  639. mac[3] = (unsigned char)(w1 >> 8);
  640. mac[4] = (unsigned char)(w2 & 0xFF);
  641. mac[5] = (unsigned char)(w2 >> 8);
  642. }
  643. }
  644. write_rar();
  645. }
  646. /* Runtime MAC override (netmac diagnostic). The TX source, ARP and DHCP
  647. * client all use the `mac` array, so changing it and reprogramming RAL/RAH
  648. * is enough to move the whole interface to a different address. */
  649. static void e1000e_set_mac(const unsigned char m[6])
  650. {
  651. unsigned int i;
  652. for (i = 0; i < 6; i++)
  653. mac[i] = m[i];
  654. write_rar();
  655. }
  656. /* ---- PHY access via MDIC ---- */
  657. static int mdic_read(unsigned int phy, unsigned int reg)
  658. {
  659. unsigned int t0, v;
  660. w32(REG_MDIC, (reg << MDIC_REG_SHIFT) | (phy << MDIC_PHY_SHIFT) |
  661. MDIC_OP_READ);
  662. t0 = get_ticks_ms();
  663. for (;;) {
  664. v = r32(REG_MDIC);
  665. if (v & MDIC_READY)
  666. return (v & MDIC_ERROR) ? -1 : (int)(v & MDIC_DATA_MASK);
  667. if (timed_out(t0, pch_lan ? MDIC_TIMEOUT_MS : 10))
  668. return -1;
  669. }
  670. }
  671. static int mdic_write(unsigned int phy, unsigned int reg, unsigned int data)
  672. {
  673. unsigned int t0, v;
  674. w32(REG_MDIC, (data & MDIC_DATA_MASK) | (reg << MDIC_REG_SHIFT) |
  675. (phy << MDIC_PHY_SHIFT) | MDIC_OP_WRITE);
  676. t0 = get_ticks_ms();
  677. for (;;) {
  678. v = r32(REG_MDIC);
  679. if (v & MDIC_READY)
  680. return (v & MDIC_ERROR) ? -1 : 0;
  681. if (timed_out(t0, pch_lan ? MDIC_TIMEOUT_MS : 10))
  682. return -1;
  683. }
  684. }
  685. /* ---- PCH (82577/82578/82579) PHY access ---- */
  686. /* The HV registers are reached through the IGP page-select register (31):
  687. * the page number is shifted left by 5, exactly like PHY_REG(page, reg). */
  688. /* The 82577 uses two MDIC addresses: page 0 / standard MII registers at
  689. * address 2, and the HV pages (768+) at address 1. Linux's
  690. * e1000_get_phy_addr_for_hv_page() encodes exactly this. */
  691. #define HV_PHY_ADDR 1
  692. #define HV_PAGE_START 768
  693. static unsigned int phy_page_addr(unsigned int page)
  694. {
  695. if (page >= HV_PAGE_START)
  696. return HV_PHY_ADDR;
  697. return (phy_addr >= 0) ? (unsigned int)phy_addr : 0xFFFFFFFFu;
  698. }
  699. static void phy_set_page(unsigned int page)
  700. {
  701. unsigned int addr = phy_page_addr(page);
  702. if (addr == 0xFFFFFFFFu)
  703. return;
  704. mdic_write(addr, 31, page << 5);
  705. }
  706. static int phy_read_page(unsigned int page, unsigned int reg)
  707. {
  708. unsigned int addr = phy_page_addr(page);
  709. if (addr == 0xFFFFFFFFu)
  710. return -1;
  711. mdic_write(addr, 31, page << 5);
  712. return mdic_read(addr, reg);
  713. }
  714. static int phy_write_page(unsigned int page, unsigned int reg, unsigned int data)
  715. {
  716. unsigned int addr = phy_page_addr(page);
  717. if (addr == 0xFFFFFFFFu)
  718. return -1;
  719. mdic_write(addr, 31, page << 5);
  720. return mdic_write(addr, reg, data);
  721. }
  722. /* e1000_set_mdio_slow_mode_hv(): the 82577 PHY needs slow MDIO timing, and
  723. * Linux sets it before the first reliable PHY ID read. Without it the
  724. * 82577 often answers with 0/0xffff (which is why phy_probe() used to fail). */
  725. static void pch_slow_mdio(void)
  726. {
  727. int v;
  728. int lock;
  729. if (phy_addr < 0)
  730. return;
  731. lock = pch_swflag_acquire() == 0;
  732. v = phy_read_page(769, 16);
  733. if (v < 0) {
  734. /* The first read can fail before the PHY is in slow mode; try
  735. * once more, then set the bit without a read-modify-write. */
  736. delay_ms(1);
  737. v = phy_read_page(769, 16);
  738. }
  739. if (v >= 0)
  740. phy_write_page(769, 16, (unsigned int)v | 0x0400u);
  741. else
  742. phy_write_page(769, 16, 0x0400u);
  743. phy_set_page(0);
  744. if (lock)
  745. pch_swflag_release();
  746. }
  747. /* e1000_hv_phy_workarounds_ich8lan() and e1000_copper_link_setup_82577().
  748. * The firmware normally leaves the PHY in a usable state, but these are the
  749. * parts that make the link reliable at gigabit speed; they are idempotent. */
  750. static void pch_phy_workarounds(void)
  751. {
  752. int v;
  753. int is77 = pch_phy_82577();
  754. int is78 = pch_phy_82578();
  755. int lock;
  756. if (!pch_lan || phy_addr < 0)
  757. return;
  758. if (!is77 && !is78)
  759. return;
  760. lock = pch_swflag_acquire() == 0;
  761. /* BM_PORT_GEN_CFG: clear the host wakeup bits in the low byte. */
  762. v = phy_read_page(769, 17);
  763. if (v >= 0)
  764. phy_write_page(769, 17, (unsigned int)v & 0x00FFu);
  765. /* Back to page 0 for the standard registers and the EMI window. */
  766. phy_set_page(0);
  767. /* 82577 MSE threshold: keep the link up in noise (EMI 0x0887). */
  768. mdic_write((unsigned int)phy_addr, 16, 0x0887);
  769. mdic_write((unsigned int)phy_addr, 17, 0x0034);
  770. if (is77) {
  771. /* Assert CRS on TX and enable downshift. */
  772. v = mdic_read((unsigned int)phy_addr, 22);
  773. if (v >= 0)
  774. mdic_write((unsigned int)phy_addr, 22,
  775. (unsigned int)v | 0x8000u | 0x0C00u);
  776. /* Auto MDI/MDIX. */
  777. v = mdic_read((unsigned int)phy_addr, 18);
  778. if (v >= 0) {
  779. unsigned int mdix = (unsigned int)v;
  780. mdix &= ~0x0600u;
  781. mdix |= 0x0400u;
  782. mdic_write((unsigned int)phy_addr, 18, mdix);
  783. }
  784. /* Auto master/slave resolution. */
  785. v = mdic_read((unsigned int)phy_addr, MII_GBSR);
  786. if (v >= 0)
  787. mdic_write((unsigned int)phy_addr, MII_GBSR,
  788. (unsigned int)v & ~0x1000u);
  789. }
  790. /* 82577 rev 1/2: disable early preamble generation and tune the
  791. * Kumeran FIFO preamble for SSC. */
  792. if (is77 && phy_id1 == 0x0154 && phy_id2 >= 0 &&
  793. (phy_id2 & 0xF) >= 1 && (phy_id2 & 0xF) <= 2) {
  794. phy_write_page(769, 25, 0x4431);
  795. phy_write_page(770, 16, 0xA204);
  796. }
  797. phy_set_page(0);
  798. if (lock)
  799. pch_swflag_release();
  800. }
  801. /* Kumeran control/status register (MAC CSR 0x34). offset 7 bit 1 is the K1
  802. * enable; offset 4/9 are the timeout and in-band parameters. */
  803. static unsigned int kmrn_read(unsigned int off)
  804. {
  805. w32(REG_KMRNCTRLSTA, (off << 16) | 0x00200000u);
  806. delay_ms(1);
  807. return r32(REG_KMRNCTRLSTA) & 0xFFFFu;
  808. }
  809. static void kmrn_write(unsigned int off, unsigned int data)
  810. {
  811. w32(REG_KMRNCTRLSTA, (off << 16) | (data & 0xFFFFu));
  812. delay_ms(1);
  813. }
  814. /* e1000_configure_k1_ich8lan(): change the K1 enable bit, then toggle the
  815. * forced-speed/bypass bits so the MAC samples the new setting. */
  816. static void pch_k1_config(int enable)
  817. {
  818. unsigned int v = kmrn_read(7);
  819. unsigned int ctrl, ctrl_ext, forced;
  820. if (enable)
  821. v |= 0x0002u;
  822. else
  823. v &= ~0x0002u;
  824. kmrn_write(7, v);
  825. ctrl = r32(REG_CTRL);
  826. ctrl_ext = r32(REG_CTRL_EXT);
  827. forced = ctrl & ~(0x00000100u | 0x00000200u);
  828. forced |= 0x00000800u; /* CTRL_FRCSPD */
  829. w32(REG_CTRL, forced);
  830. w32(REG_CTRL_EXT, ctrl_ext | 0x00008000u); /* SPD_BYPS */
  831. delay_ms(1);
  832. w32(REG_CTRL, ctrl);
  833. w32(REG_CTRL_EXT, ctrl_ext);
  834. delay_ms(1);
  835. }
  836. /* e1000_k1_gig_workaround_hv(): at 1 Gbps K1 must be off or the MAC can
  837. * stall when it transitions from a lower speed. The preamble workaround
  838. * below is e1000_check_for_copper_link_ich8lan()'s half-duplex fix. */
  839. static void pch_link_up_workarounds(void)
  840. {
  841. unsigned int st = r32(REG_STATUS);
  842. int ms, v;
  843. int is77 = pch_phy_82577();
  844. int is78 = pch_phy_82578();
  845. int lock;
  846. int speed1000 = (st & STATUS_SPEED) == 0x80;
  847. if (!pch_lan || phy_addr < 0)
  848. return;
  849. if (!is77 && !is78)
  850. return;
  851. lock = pch_swflag_acquire() == 0;
  852. phy_set_page(0);
  853. if (is78) {
  854. /* BM_CS_STATUS: link up + resolved + 1000 Mbps. */
  855. ms = mdic_read((unsigned int)phy_addr, 17);
  856. if (ms >= 0 &&
  857. (ms & 0x0C00) == 0x0C00 &&
  858. (ms & 0xC000) == 0x8000)
  859. v = 0;
  860. else if (speed1000)
  861. v = 0; /* STATUS says 1G: never leave K1 on */
  862. else
  863. v = 1;
  864. } else {
  865. ms = mdic_read((unsigned int)phy_addr, 26);
  866. if (ms >= 0 &&
  867. (ms & 0x0040) && (ms & 0x1000) &&
  868. (ms & 0x0300) == 0x0200)
  869. v = 0;
  870. else if (speed1000)
  871. v = 0; /* PHY read failed; trust STATUS */
  872. else
  873. v = 1;
  874. }
  875. dbg(v ? "K1 enabled" : "K1 disabled (1G)");
  876. /* Link-stall fix, link-up value, then the K1 setting. */
  877. phy_write_page(770, 19, 0x0100);
  878. pch_k1_config(v);
  879. {
  880. int pre = phy_read_page(770, 16);
  881. if (pre >= 0) {
  882. unsigned int p = (unsigned int)pre & ~0x7000u;
  883. if (!(st & STATUS_FD))
  884. p |= 0x1000;
  885. phy_write_page(770, 16, p);
  886. }
  887. }
  888. phy_set_page(0);
  889. if (lock)
  890. pch_swflag_release();
  891. }
  892. static void pch_link_down_workarounds(void)
  893. {
  894. int lock;
  895. if (!pch_lan || phy_addr < 0)
  896. return;
  897. if (!pch_phy_82577() && !pch_phy_82578())
  898. return;
  899. lock = pch_swflag_acquire() == 0;
  900. phy_write_page(770, 19, 0x4100);
  901. pch_k1_config(1);
  902. phy_set_page(0);
  903. if (lock)
  904. pch_swflag_release();
  905. }
  906. /* Poll the PHY link while commands are running. Linux's watchdog does the
  907. * same every couple of seconds: re-apply the K1/preamble workarounds when
  908. * the link comes up and re-enable RCTL/TCTL. Without this a cable plugged
  909. * in after boot never gets the link-up workarounds. */
  910. static void link_check(void)
  911. {
  912. unsigned int now, st;
  913. if (!present || !mmio || !pch_lan)
  914. return;
  915. if (!irqs_on())
  916. return;
  917. now = get_ticks_ms();
  918. if (now - link_poll_ms < 250)
  919. return;
  920. link_poll_ms = now;
  921. st = r32(REG_STATUS);
  922. if (!!(st & STATUS_LU) == last_link)
  923. return;
  924. last_link = !!(st & STATUS_LU);
  925. if (last_link) {
  926. dbg("link up (poll)");
  927. pch_link_up_workarounds();
  928. w32(REG_CTRL, r32(REG_CTRL) | CTRL_SLU);
  929. w32(REG_RCTL, r32(REG_RCTL) | RCTL_EN);
  930. w32(REG_TCTL, r32(REG_TCTL) | TCTL_EN);
  931. } else {
  932. dbg("link down (poll)");
  933. pch_link_down_workarounds();
  934. phy_autoneg();
  935. }
  936. }
  937. static void phy_probe(void)
  938. {
  939. unsigned int phy;
  940. int lock;
  941. phy_addr = -1;
  942. phy_id1 = phy_id2 = -1;
  943. lock = pch_swflag_acquire() == 0;
  944. for (phy = 0; phy < 4; phy++) {
  945. int id1, id2;
  946. /* Make sure a previous page-select is not in effect. */
  947. mdic_write(phy, 31, 0);
  948. id1 = mdic_read(phy, MII_PHYID1);
  949. id2 = mdic_read(phy, MII_PHYID2);
  950. if (id1 < 0 || id2 < 0)
  951. continue;
  952. if ((id1 & 0xFFFF) == 0 && (id2 & 0xFFFF) == 0)
  953. continue;
  954. if ((id1 & 0xFFFF) == 0xFFFF)
  955. continue;
  956. phy_addr = (int)phy;
  957. phy_id1 = id1;
  958. phy_id2 = id2;
  959. break;
  960. }
  961. if (lock)
  962. pch_swflag_release();
  963. }
  964. /* Poll STATUS.LU for a link; used after a full-chip reset and by net100.
  965. * Returns 1 when the link is up, 0 on timeout. */
  966. static int wait_link(unsigned int timeout_ms)
  967. {
  968. unsigned int t0 = get_ticks_ms();
  969. while (!(r32(REG_STATUS) & STATUS_LU)) {
  970. if (timed_out(t0, timeout_ms))
  971. return 0;
  972. delay_ms(10);
  973. }
  974. return 1;
  975. }
  976. static void phy_autoneg(void)
  977. {
  978. int lock;
  979. if (phy_addr < 0)
  980. return;
  981. lock = pch_swflag_acquire() == 0;
  982. phy_set_page(0);
  983. mdic_write(phy_addr, MII_ANAR, 0x01E1); /* 10/100 half+full */
  984. /* force_speed is the net100 fallback: with 1000BASE-T advertisement
  985. * cleared the PHY can only resolve 10/100, which bypasses a marginal
  986. * 1000M pair/jack while still giving a fully usable network. */
  987. mdic_write(phy_addr, MII_GBSR, force_speed == 100 ? 0x0000 : 0x0300);
  988. mdic_write(phy_addr, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
  989. if (lock)
  990. pch_swflag_release();
  991. }
  992. /* net100 fallback: 0 = auto/1G advertisement, 100 = 100M only.
  993. *
  994. * The link is deliberately dropped and renegotiated. The caller (the
  995. * net100 command) waits for the result; the force_speed flag is also
  996. * honoured by phy_autoneg() so a link-down poll cannot silently restore
  997. * 1000M. */
  998. static int e1000e_set_speed(unsigned int mbps)
  999. {
  1000. unsigned int t0;
  1001. if (!present || !mmio || phy_addr < 0)
  1002. return -1;
  1003. if (mbps != 0 && mbps != 100)
  1004. return -1;
  1005. force_speed = (mbps == 100) ? 100 : 0;
  1006. phy_autoneg(); /* honours force_speed and restarts autoneg */
  1007. /* First let the old link drop (the PHY restarts autoneg), then wait
  1008. * for the new one. A switch can take a couple of seconds. */
  1009. t0 = get_ticks_ms();
  1010. while ((r32(REG_STATUS) & STATUS_LU) && !timed_out(t0, 1000))
  1011. delay_ms(10);
  1012. if (!wait_link(5000)) {
  1013. last_link = 0;
  1014. return -3;
  1015. }
  1016. if (pch_lan)
  1017. pch_link_up_workarounds();
  1018. /* The MAC may clear RCTL/TCTL on the link transition; re-assert them
  1019. * here because last_link is set to 1 and link_check() will not. */
  1020. w32(REG_CTRL, r32(REG_CTRL) | CTRL_SLU);
  1021. w32(REG_RCTL, r32(REG_RCTL) | RCTL_EN);
  1022. w32(REG_TCTL, r32(REG_TCTL) | TCTL_EN);
  1023. last_link = 1;
  1024. return 0;
  1025. }
  1026. /* ---- hardware bring-up ---- */
  1027. /* Quiesce the MAC without touching the PHY: mask interrupts, stop RX/TX and
  1028. * let any in-flight DMA finish. This is the "reset" path on PCH LAN parts. */
  1029. static void hw_stop(void)
  1030. {
  1031. unsigned int tctl;
  1032. /* PCH LAN: let the ME finish the current link transaction before the
  1033. * first MAC write. */
  1034. delay_ms(5);
  1035. w32(REG_IMC, 0xFFFFFFFFu);
  1036. (void)r32(REG_ICR);
  1037. w32(REG_RCTL, 0);
  1038. /* Stop TX without clobbering the 82577's reserved TCTL bits (bit 28
  1039. * and RRTHRESH are reset to 1/01 and must stay that way). */
  1040. tctl = r32(REG_TCTL);
  1041. w32(REG_TCTL, (tctl & ~TCTL_EN) | TCTL_PSP);
  1042. (void)r32(REG_STATUS); /* flush posted writes */
  1043. delay_ms(10);
  1044. }
  1045. static void e1000e_stop(void)
  1046. {
  1047. unsigned int t0;
  1048. if (!present || !mmio)
  1049. return;
  1050. hw_stop();
  1051. w32(REG_CTRL, r32(REG_CTRL) | CTRL_GIO_MASTER_DISABLE);
  1052. t0 = get_ticks_ms();
  1053. while ((r32(REG_STATUS) & STATUS_GIO_MASTER_ENABLE) &&
  1054. !timed_out(t0, 100))
  1055. delay_ms(1);
  1056. dbg("stopped for kexec");
  1057. }
  1058. /* The 82577 PBA register has a 5-bit RX allocation (RXA) and a read-only
  1059. * TX allocation (TXA = PBS - RXA). Linux uses 26 KB RX on PCH parts,
  1060. * leaving the rest of the packet buffer for TX. If firmware/ME left RXA at
  1061. * PBS, TXA is zero and the transmit engine has no FIFO space, so the queue
  1062. * never moves. */
  1063. static void pch_pba_config(void)
  1064. {
  1065. unsigned int pbs, pba;
  1066. if (!pch_lan)
  1067. return;
  1068. pbs = r32(REG_PBS);
  1069. pba = r32(REG_PBA);
  1070. if (pbs > 26 && (pba & 0x1Fu) != 26) {
  1071. w32(REG_PBA, 26);
  1072. dbg("PBA set to 26K RX");
  1073. }
  1074. serial_puts(SERIAL_COM1, "e1000e: pbs ");
  1075. serial_puthex(SERIAL_COM1, pbs);
  1076. serial_puts(SERIAL_COM1, " pba ");
  1077. serial_puthex(SERIAL_COM1, r32(REG_PBA));
  1078. serial_puts(SERIAL_COM1, "\n");
  1079. }
  1080. /* PCH LAN with AMT: the firmware (ME) must be told that the OS driver owns
  1081. * the network interface, otherwise it can keep the LAN - notably the TX
  1082. * path - to itself. e1000e's e1000e_get_hw_control() sets
  1083. * CTRL_EXT.DRV_LOAD for parts with FLAG_HAS_CTRLEXT_ON_LOAD (the pch_info
  1084. * used by the 82577LM/82578/82579). e1000e_open() takes ownership before
  1085. * the reset on AMT parts; do the same, then call this again after the reset
  1086. * to restore WUC/VET. */
  1087. static void pch_claim(void)
  1088. {
  1089. unsigned int ctrl_ext;
  1090. if (!pch_lan)
  1091. return;
  1092. ctrl_ext = r32(REG_CTRL_EXT);
  1093. if (!(ctrl_ext & 0x10000000u)) {
  1094. w32(REG_CTRL_EXT, ctrl_ext | 0x10000000u);
  1095. dbg("AMT handshake: CTRL_EXT.DRV_LOAD set");
  1096. }
  1097. /* e1000e_reset(): clear wake-up control and enable VLAN EtherType.
  1098. * PBA needs the special PCH encoding (RXA only); pch_pba_config()
  1099. * handles it and leaves non-PCH split-PBA parts alone. */
  1100. w32(REG_WUC, 0);
  1101. w32(REG_VET, 0x8100);
  1102. pch_pba_config();
  1103. /* These are normally applied in the post-reset path; do them here as
  1104. * well so a warm boot (MAC reset skipped while the link is up) gets the
  1105. * same values. CRC_OFFSET is pchlan-only (0x5F50 is the receive-address
  1106. * CRC register on the 82579); KABGTXD applies to the whole ich8lan
  1107. * family. */
  1108. if (device_id == 0x10EA || device_id == 0x10EB ||
  1109. device_id == 0x10EF || device_id == 0x10F0)
  1110. w32(REG_CRC_OFFSET, 0x65656565u);
  1111. if (pch_lan) {
  1112. unsigned int st = r32(REG_STATUS);
  1113. w32(REG_KABGTXD, r32(REG_KABGTXD) | 0x00050000u);
  1114. /* Linux clears this sticky "PHY reset asserted" bit after a
  1115. * reset; the firmware leaves it set on the warm path. */
  1116. if (st & STATUS_PHYRA)
  1117. w32(REG_STATUS, st & ~STATUS_PHYRA);
  1118. }
  1119. }
  1120. static void pch_swflag_release(void)
  1121. {
  1122. if (pch_lan)
  1123. w32(REG_EXTCNF_CTRL, r32(REG_EXTCNF_CTRL) & ~0x20u);
  1124. }
  1125. /* e1000_acquire_swflag_ich8lan(): coordinate PHY/MAC access with the ME
  1126. * before a global reset. Without this the X201 could wedge on CTRL_RST
  1127. * while the ME was holding the shared resource. */
  1128. static int pch_swflag_acquire(void)
  1129. {
  1130. unsigned int t0;
  1131. if (!pch_lan)
  1132. return -1;
  1133. t0 = get_ticks_ms();
  1134. while ((r32(REG_EXTCNF_CTRL) & 0x20u) && !timed_out(t0, 100))
  1135. delay_ms(1);
  1136. if (r32(REG_EXTCNF_CTRL) & 0x20u) {
  1137. dbg("SWFLAG busy");
  1138. return -1;
  1139. }
  1140. w32(REG_EXTCNF_CTRL, r32(REG_EXTCNF_CTRL) | 0x20u);
  1141. t0 = get_ticks_ms();
  1142. while (!(r32(REG_EXTCNF_CTRL) & 0x20u)) {
  1143. if (timed_out(t0, 200)) {
  1144. dbg("SWFLAG acquire timeout");
  1145. w32(REG_EXTCNF_CTRL,
  1146. r32(REG_EXTCNF_CTRL) & ~0x20u);
  1147. return -1;
  1148. }
  1149. delay_ms(1);
  1150. }
  1151. return 0;
  1152. }
  1153. static int hw_reset(void)
  1154. {
  1155. unsigned int t0, ctrl;
  1156. /* Sample the link before hw_stop(): the PHY link is independent of
  1157. * the MAC RX/TX state. */
  1158. int link_up = pch_lan && (r32(REG_STATUS) & STATUS_LU);
  1159. int swflag = 0;
  1160. if (link_up) {
  1161. /* Warm path at probe and netreset: stop our queues but leave
  1162. * the firmware-initialized PHY/MAC alone. */
  1163. hw_stop();
  1164. dbg("MAC reset skipped (link up)");
  1165. return 0;
  1166. }
  1167. /* Block new DMA requests before clearing RCTL. Linux disables the
  1168. * PCIe master first; clearing RCTL while DMA is active can wedge the
  1169. * receive packet buffer on these parts. */
  1170. ctrl = r32(REG_CTRL);
  1171. /* The GIO master handshake only exists on PCIe parts. The 82540EM
  1172. * is conventional PCI: QEMU keeps STATUS bit 19 set forever, so the
  1173. * wait used to burn its whole 1 s timeout on every boot. */
  1174. if (device_id != 0x100E) {
  1175. w32(REG_CTRL, ctrl | CTRL_GIO_MASTER_DISABLE);
  1176. t0 = get_ticks_ms();
  1177. while ((r32(REG_STATUS) & STATUS_GIO_MASTER_ENABLE) &&
  1178. !timed_out(t0, 1000))
  1179. ;
  1180. delay_ms(5);
  1181. }
  1182. hw_stop();
  1183. delay_ms(5);
  1184. /* Coordinate with the ME before the global reset (Linux takes the
  1185. * SW flag after quiescing the queues, just before CTRL_RST). */
  1186. if (pch_lan)
  1187. swflag = pch_swflag_acquire() == 0;
  1188. /* MAC reset (only when the link was down). */
  1189. ctrl = r32(REG_CTRL);
  1190. dbg("full reset: MAC only");
  1191. w32(REG_CTRL, ctrl | CTRL_RST);
  1192. t0 = get_ticks_ms();
  1193. while (r32(REG_CTRL) & CTRL_RST) {
  1194. if (timed_out(t0, 1000)) {
  1195. if (swflag)
  1196. pch_swflag_release();
  1197. return -1;
  1198. }
  1199. }
  1200. delay_ms(20); /* NVM auto-read after reset */
  1201. /* Linux deliberately does not write EXTCNF_CTRL.SWFLAG after a global
  1202. * reset: the reset clears it, and clearing it again can steal the
  1203. * flag from the ME. Only the timeout/error paths release it. */
  1204. /* Wait for the NVM auto-read to finish and clear the done bit. */
  1205. t0 = get_ticks_ms();
  1206. while (!(r32(REG_STATUS) & STATUS_LAN_INIT_DONE) &&
  1207. !timed_out(t0, 200))
  1208. ;
  1209. w32(REG_STATUS, r32(REG_STATUS) & ~STATUS_LAN_INIT_DONE);
  1210. if (device_id == 0x10EA || device_id == 0x10EB ||
  1211. device_id == 0x10EF || device_id == 0x10F0) {
  1212. /* e1000e_reset_hw_ich8lan(): with this value noise on the
  1213. * Kumeran interface is detected as a CRC error and dropped
  1214. * before it reaches the DMA engine. This is the pchlan
  1215. * (82577/82578) register; on the 82579 the same offset is
  1216. * the receive-address CRC calculation register. */
  1217. w32(REG_CRC_OFFSET, 0x65656565u);
  1218. }
  1219. if (pch_lan)
  1220. w32(REG_KABGTXD, r32(REG_KABGTXD) | 0x00050000u);
  1221. reset_count++;
  1222. return 0;
  1223. }
  1224. static void setup_rings(void)
  1225. {
  1226. unsigned int i;
  1227. for (i = 0; i < E1000E_NRX; i++) {
  1228. rx_ring[i].addr_lo = (unsigned int)(rx_bufs + i * E1000E_BUF);
  1229. rx_ring[i].addr_hi = 0;
  1230. rx_ring[i].status_error = 0;
  1231. rx_ring[i].length = 0;
  1232. rx_ring[i].vlan = 0;
  1233. }
  1234. for (i = 0; i < E1000E_NTX; i++) {
  1235. tx_ring[i].addr_lo = (unsigned int)(tx_bufs + i * E1000E_BUF);
  1236. tx_ring[i].addr_hi = 0;
  1237. tx_ring[i].lower = 0;
  1238. tx_ring[i].upper = 0;
  1239. }
  1240. __sync_synchronize();
  1241. w32(REG_RDBAL, (unsigned int)rx_ring);
  1242. w32(REG_RDBAH, 0);
  1243. w32(REG_RDLEN, E1000E_NRX * (unsigned int)sizeof(struct e1000e_rx_desc));
  1244. w32(REG_RDH, 0);
  1245. w32(REG_RDT, E1000E_NRX - 1);
  1246. w32(REG_TDBAL, (unsigned int)tx_ring);
  1247. w32(REG_TDBAH, 0);
  1248. w32(REG_TDLEN, E1000E_NTX * (unsigned int)sizeof(struct e1000e_tx_desc));
  1249. w32(REG_TDH, 0);
  1250. w32(REG_TDT, 0);
  1251. rx_cur = 0;
  1252. tx_cur = 0;
  1253. tx_pending = 0;
  1254. tx_fail_run = 0;
  1255. tx_broken = 0;
  1256. tx_timeout_logged = 0;
  1257. rx_ok = rx_err = rx_drop = 0;
  1258. rx_ucast = rx_bcast = rx_mcast = 0;
  1259. last_rx_len = 0;
  1260. {
  1261. unsigned int i;
  1262. for (i = 0; i < 6; i++)
  1263. last_rx_dst[i] = 0;
  1264. }
  1265. tx_ok = tx_err = 0;
  1266. last_tx_status = 0;
  1267. last_rx_status = 0;
  1268. last_rx_errors = 0;
  1269. tctl_fixes = 0;
  1270. }
  1271. /* PCH LAN: program the TX descriptor write-back policy and arbitration bits
  1272. * that e1000e's ich8lan init sets. Without WTHRESH/COUNT_DESC the 82577 was
  1273. * observed to never report TX descriptor completion (tx_ok stayed 0), while
  1274. * RX worked. */
  1275. static void tx_desc_config(void)
  1276. {
  1277. unsigned int v, gcr;
  1278. /* Enable snooping for TX/RX DMA (see GCR_NO_SNOOP). */
  1279. gcr = r32(REG_GCR);
  1280. w32(REG_GCR, gcr & ~GCR_NO_SNOOP);
  1281. serial_puts(SERIAL_COM1, "e1000e: gcr ");
  1282. serial_puthex(SERIAL_COM1, gcr);
  1283. serial_puts(SERIAL_COM1, " -> ");
  1284. serial_puthex(SERIAL_COM1, gcr & ~GCR_NO_SNOOP);
  1285. serial_puts(SERIAL_COM1, "\n");
  1286. /* CTRL_EXT bits e1000e's ich8 init sets: relaxed-ordering disable,
  1287. * the always-set bit 22, and PHY power-down in D3. */
  1288. v = r32(REG_CTRL_EXT);
  1289. v |= 0x00020000u | 0x00400000u | 0x00100000u;
  1290. w32(REG_CTRL_EXT, v);
  1291. v = r32(REG_TXDCTL0);
  1292. v |= TXDCTL_COUNT_DESC;
  1293. v &= ~(TXDCTL_PTHRESH | TXDCTL_HTHRESH | TXDCTL_WTHRESH);
  1294. v |= TXDCTL_WB_ONE | 0x1Fu; /* GRAN, WTHRESH=1, PTHRESH=31 */
  1295. w32(REG_TXDCTL0, v);
  1296. v = r32(REG_TXDCTL1);
  1297. v |= TXDCTL_COUNT_DESC;
  1298. v &= ~(TXDCTL_PTHRESH | TXDCTL_HTHRESH | TXDCTL_WTHRESH);
  1299. v |= TXDCTL_WB_ONE | 0x1Fu;
  1300. w32(REG_TXDCTL1, v);
  1301. /* TARC queue arbitration workarounds from the PCH init. Linux sets
  1302. * TARC(0) bits 23/24/26/27 and TARC(1) bits 24/26/30 plus bit 28
  1303. * when TCTL.MULR is clear (which it is here). */
  1304. w32(REG_TARC0, r32(REG_TARC0) | 0x0D800000u);
  1305. w32(REG_TARC1, r32(REG_TARC1) | 0x55000000u);
  1306. }
  1307. /* Pin the state of the receive path. The firmware/ME can leave RFCTL,
  1308. * RXCSUM and RXDCTL in whatever its driver used. e1000e/PCH parts use the
  1309. * extended 16-byte RX descriptor write-back, so enable RFCTL.EXTEN and parse
  1310. * status/error at offset 8 and length at 12 (see struct e1000e_rx_desc). */
  1311. static void rx_desc_config(void)
  1312. {
  1313. unsigned int rfctl = r32(REG_RFCTL);
  1314. unsigned int rxcsum = r32(REG_RXCSUM);
  1315. unsigned int v;
  1316. rfctl |= RFCTL_EXTEN;
  1317. rfctl |= RFCTL_NFSW_DIS | RFCTL_NFSR_DIS |
  1318. RFCTL_IPV6_EX_DIS | RFCTL_NEW_IPV6_EXT_DIS;
  1319. w32(REG_RFCTL, rfctl);
  1320. /* The 82540EM (QEMU's e1000) has no RFCTL: the write is ignored and
  1321. * the MAC keeps writing legacy descriptors. Decoding those with the
  1322. * extended layout reads the frame length as status, so DD looks clear
  1323. * for every even-length frame and the ring stalls full. */
  1324. rx_ext = (r32(REG_RFCTL) & RFCTL_EXTEN) ? 1 : 0;
  1325. /* QEMU's e1000 stores RFCTL like any register, so EXTEN reads back
  1326. * set, yet the emulated 82540EM still writes legacy descriptors.
  1327. * The 82540EM never has extended RX descriptors. */
  1328. if (device_id == 0x100E)
  1329. rx_ext = 0;
  1330. /* No checksum offload: the stack verifies checksums, and the driver
  1331. * rejects any descriptor whose error byte is non-zero. With RXCSUM
  1332. * enabled a UDP checksum of zero (common in DHCP replies) can be
  1333. * reported as TCPE and the reply would be dropped. */
  1334. w32(REG_RXCSUM, 0);
  1335. /* The firmware can leave the receive delay timers programmed; the
  1336. * driver polls descriptors and wants them written back immediately. */
  1337. w32(REG_RDTR, 0);
  1338. w32(REG_RADV, 0);
  1339. /* Match Linux's PCH setting: clear the prefetch/host/write-back
  1340. * thresholds and keep only GRAN. Prefetching descriptors ahead of
  1341. * the tail is unsafe with extended write-back, because the hardware
  1342. * overwrites the buffer-address field with MRQ/RSS; a prefetched
  1343. * descriptor can then carry a garbage address. */
  1344. v = r32(REG_RXDCTL0);
  1345. v &= ~(RXDCTL_PTHRESH | RXDCTL_HTHRESH | RXDCTL_WTHRESH);
  1346. v |= RXDCTL_GRAN;
  1347. w32(REG_RXDCTL0, v);
  1348. v = r32(REG_RXDCTL1);
  1349. v &= ~(RXDCTL_PTHRESH | RXDCTL_HTHRESH | RXDCTL_WTHRESH);
  1350. v |= RXDCTL_GRAN;
  1351. w32(REG_RXDCTL1, v);
  1352. serial_puts(SERIAL_COM1, "e1000e: rfctl ");
  1353. serial_puthex(SERIAL_COM1, rfctl);
  1354. serial_puts(SERIAL_COM1, " rxcsum ");
  1355. serial_puthex(SERIAL_COM1, rxcsum);
  1356. serial_puts(SERIAL_COM1, " rxdctl ");
  1357. serial_puthex(SERIAL_COM1, r32(REG_RXDCTL0));
  1358. serial_puts(SERIAL_COM1, "\n");
  1359. }
  1360. static void hw_start(void)
  1361. {
  1362. unsigned int ctrl, tctl;
  1363. setup_rings();
  1364. if (pch_lan)
  1365. tx_desc_config();
  1366. /* Make the MAC link state explicit, like e1000's
  1367. * e1000_setup_copper_link_ich8lan(): the PHY controls speed/duplex,
  1368. * but SLU must be set and the forced speed/duplex bits cleared. */
  1369. ctrl = r32(REG_CTRL);
  1370. ctrl |= CTRL_SLU;
  1371. ctrl &= ~(CTRL_FRCSPD | CTRL_FRCDPX);
  1372. ctrl &= ~CTRL_GIO_MASTER_DISABLE;
  1373. ctrl &= ~0x40000000u; /* CTRL.VME: no VLAN insert/strip */
  1374. w32(REG_CTRL, ctrl);
  1375. rx_desc_config();
  1376. w32(REG_RCTL, RCTL_EN | RCTL_BAM | RCTL_SECRC);
  1377. /* Preserve the reserved/RRTHRESH bits (82577 reset: bit28=1 and
  1378. * RRTHRESH=01). Writing TCTL from scratch cleared them and left the
  1379. * transmitter unable to fetch descriptors (TDH stuck at 0). */
  1380. tctl = r32(REG_TCTL);
  1381. tctl &= ~(TCTL_CT_MASK | TCTL_COLD_MASK);
  1382. tctl |= TCTL_EN | TCTL_PSP | TCTL_RTLC |
  1383. (TCTL_CT << TCTL_CT_SHIFT) |
  1384. (TCTL_COLD << TCTL_COLD_SHIFT);
  1385. /* The 82577/82578 TCTL reset value has bit 28 = 1 and RRTHRESH
  1386. * (bits 30:29) = 01. A warm boot may inherit a firmware value that
  1387. * cleared them; restore the documented reset state explicitly. */
  1388. if (device_id == 0x10EA || device_id == 0x10EB ||
  1389. device_id == 0x10EF || device_id == 0x10F0)
  1390. tctl |= 0x30000000u;
  1391. w32(REG_TCTL, tctl);
  1392. w32(REG_TIPG, 10u | (8u << 10) | (6u << 20));
  1393. __sync_synchronize();
  1394. }
  1395. /* ---- netdev interface ---- */
  1396. static int e1000e_init(void)
  1397. {
  1398. char buf[64];
  1399. unsigned int i;
  1400. if (inited)
  1401. return present ? 0 : -1;
  1402. inited = 1;
  1403. if (probe_pci() != 0) {
  1404. dbg("no supported Intel NIC");
  1405. return -1;
  1406. }
  1407. process_map_mmio_all(bar_base, bar_size);
  1408. mmio = (unsigned char *)(unsigned long)bar_base;
  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. };