r8168.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /* Realtek RTL8111/8168 Gigabit Ethernet driver (C+ mode, 8168c/d/e class).
  2. *
  3. * Target chip: the RTL8112L on the ASUS AT3IONT-I -- PCI 10ec:8168 rev 03,
  4. * register window in BAR2 (64-bit, 4 KB, prefetchable). Polled: no IRQ.
  5. * TX/RX use 16-byte descriptors the NIC DMAs to/from memory; register and
  6. * descriptor values mirror the r8169 sequence from Linux.
  7. *
  8. * Every wait is time-bounded and each bring-up step goes to serial, because
  9. * a wedged driver must still be diagnosable from the log.
  10. */
  11. #include "io.h"
  12. #include "mm.h"
  13. #include "netdev.h"
  14. #include "paging.h"
  15. #include "pci.h"
  16. #include "process.h"
  17. #include "serial.h"
  18. #include "timer.h"
  19. #include "r8168.h"
  20. /* ---- chip registers (offsets into the MMIO window) ---- */
  21. #define REG_MAC0 0x00
  22. #define REG_TxDescLow 0x20
  23. #define REG_TxDescHigh 0x24
  24. #define REG_ChipCmd 0x37
  25. #define REG_TxPoll 0x38
  26. #define REG_IntrMask 0x3c
  27. #define REG_IntrStatus 0x3e
  28. #define REG_TxConfig 0x40 /* also carries the chip version (XID) */
  29. #define REG_RxConfig 0x44
  30. #define REG_Cfg9346 0x50
  31. #define REG_PHYAR 0x60
  32. #define REG_PHYstatus 0x6c
  33. #define REG_RDSAR1 0xd0 /* RX ring high 32 bits (8168c only) */
  34. #define REG_RxMaxSize 0xda
  35. #define REG_CPlusCmd 0xe0
  36. #define REG_IntrMitigate 0xe2
  37. #define REG_RxDescLow 0xe4
  38. #define REG_RxDescHigh 0xe8
  39. #define REG_MaxTxPacket 0xec
  40. /* ChipCmd bits. */
  41. #define ChipCmd_Reset 0x10
  42. #define ChipCmd_RxEnb 0x08
  43. #define ChipCmd_TxEnb 0x04
  44. #define Cfg9346_Unlock 0xC0
  45. /* TxPoll: kick the normal priority queue. */
  46. #define TxPoll_NPQ 0x40
  47. /* TxConfig: max PCI burst + shortest inter-frame gap. */
  48. #define TXCONFIG_VALUE ((7 << 8) | (3 << 24))
  49. /* RxConfig. */
  50. #define RX_DMA_BURST 0x0700
  51. #define RX128_INT_EN 0x8000
  52. #define RX_ACCEPT_MASK 0x3f
  53. #define RX_ACCEPT_BROADCAST 0x08
  54. #define RX_ACCEPT_MULTICAST 0x04
  55. #define RX_ACCEPT_MYPHYS 0x02
  56. #define RX_ACCEPT_ALLPHYS 0x01 /* promiscuous (bring-up: see set_rx_mode) */
  57. /* PHYstatus (0x6c). */
  58. #define PHYST_LINK 0x02
  59. #define PHYST_FULLDUP 0x01
  60. #define PHYST_1000 0x10
  61. #define PHYST_100 0x08
  62. #define PHYST_10 0x04
  63. /* PHYAR: bit 31 = busy/valid, reg in bits 20:16, data in 15:0. */
  64. #define PHYAR_FLAG 0x80000000u
  65. /* Interrupt/event status bits (IntrStatus, 0x3e). The register latches
  66. * events; each bit is cleared by writing it back. With IntrMask = 0 we only
  67. * use these as a diagnostic and to detect an RX stall. */
  68. #define ISR_RxOK 0x0001
  69. #define ISR_RxErr 0x0002
  70. #define ISR_TxOK 0x0004
  71. #define ISR_TxErr 0x0008
  72. #define ISR_ROVW 0x0010 /* RX FIFO overflow */
  73. #define ISR_LinkChg 0x0020
  74. #define ISR_FOV 0x0040 /* RX FIFO overflow (8168) */
  75. #define ISR_TDU 0x0080 /* TX descriptor unavailable */
  76. #define ISR_SWInt 0x0100
  77. /* PHY registers. */
  78. #define MII_BMCR 0
  79. #define MII_BMSR 1
  80. #define MII_PHYSID1 2
  81. #define MII_PHYSID2 3
  82. #define MII_ANAR 4
  83. #define MII_GBSR 9
  84. #define BMCR_AUTONEG 0x1000
  85. #define BMCR_RESTART_AN 0x0200
  86. #define BMSR_LINK 0x0004
  87. /* Descriptor bits (C+ mode). */
  88. #define DescOwn 0x80000000u
  89. #define DescRingEnd 0x40000000u
  90. #define DescFirstFrag 0x20000000u
  91. #define DescLastFrag 0x10000000u
  92. #define RxRES (1u << 21) /* RX error summary */
  93. #define RxPktLenMask 0x3fffu /* length field, includes the FCS */
  94. /* Rings. */
  95. #define R8168_NTX 16
  96. #define R8168_NRX 64
  97. #define R8168_BUF_SIZE 2048
  98. #define R8168_RX_ARM (R8168_BUF_SIZE - 1)
  99. /* PCI ids. */
  100. #define R8168_VENDOR 0x10ECu
  101. #define R8168_DEVICE 0x8168u
  102. struct r8168_desc {
  103. volatile unsigned int opts1;
  104. volatile unsigned int opts2;
  105. volatile unsigned int addr_lo;
  106. volatile unsigned int addr_hi;
  107. };
  108. static unsigned char *mmio;
  109. static int present;
  110. static int inited;
  111. static unsigned char mac[6];
  112. static unsigned int xid; /* (TxConfig >> 20) & 0xfcf */
  113. static unsigned int bar_base, bar_size;
  114. static unsigned int pci_bus, pci_dev, pci_func;
  115. static struct r8168_desc *tx_ring;
  116. static struct r8168_desc *rx_ring;
  117. static unsigned char *tx_bufs;
  118. static unsigned char *rx_bufs;
  119. static unsigned int tx_cur;
  120. static unsigned int rx_cur;
  121. /* Counters (also reported by `netregs`). */
  122. static unsigned int tx_ok, tx_err, rx_ok, rx_err, rx_drop;
  123. static unsigned int last_tx_status; /* raw descriptor status after TX */
  124. static unsigned int isr_last; /* last IntrStatus value we acked */
  125. static unsigned int isr_rovw, isr_fov, isr_tdu;
  126. static unsigned int rx_restarts;
  127. static int timed_out(unsigned int start_ms, unsigned int ms)
  128. {
  129. return get_ticks_ms() - start_ms > ms;
  130. }
  131. /* ---- MMIO accessors (uncacheable mapping) ---- */
  132. static inline unsigned int r32(unsigned int reg)
  133. {
  134. return *(volatile unsigned int *)(mmio + reg);
  135. }
  136. static inline void w32(unsigned int reg, unsigned int v)
  137. {
  138. *(volatile unsigned int *)(mmio + reg) = v;
  139. }
  140. static inline unsigned short r16(unsigned int reg)
  141. {
  142. return *(volatile unsigned short *)(mmio + reg);
  143. }
  144. static inline void w16(unsigned int reg, unsigned short v)
  145. {
  146. *(volatile unsigned short *)(mmio + reg) = v;
  147. }
  148. static inline unsigned char r8(unsigned int reg)
  149. {
  150. return *(volatile unsigned char *)(mmio + reg);
  151. }
  152. static inline void w8(unsigned int reg, unsigned char v)
  153. {
  154. *(volatile unsigned char *)(mmio + reg) = v;
  155. }
  156. /* Keep the compiler from reordering descriptor/MMIO stores. x86 caches are
  157. * coherent with PCI DMA, so no instruction barrier is needed. */
  158. static inline void dma_barrier(void)
  159. {
  160. __asm__ volatile ("" ::: "memory");
  161. }
  162. /* ---- small string/decimal/hex buffer helpers ---- */
  163. static unsigned int app(char *b, unsigned int i, const char *s)
  164. {
  165. while (*s)
  166. b[i++] = *s++;
  167. return i;
  168. }
  169. static unsigned int appd(char *b, unsigned int i, unsigned int v)
  170. {
  171. char t[11];
  172. unsigned int n = 0;
  173. if (v > 9999)
  174. v = 9999; /* keep rows short */
  175. if (v == 0) {
  176. b[i++] = '0';
  177. return i;
  178. }
  179. while (v) {
  180. t[n++] = (char)('0' + v % 10);
  181. v /= 10;
  182. }
  183. while (n)
  184. b[i++] = t[--n];
  185. return i;
  186. }
  187. static unsigned int apph(char *b, unsigned int i, unsigned int v, unsigned int digits)
  188. {
  189. static const char hex[] = "0123456789ABCDEF";
  190. unsigned int d;
  191. for (d = 0; d < digits; d++)
  192. b[i++] = hex[(v >> (4 * (digits - 1 - d))) & 0xF];
  193. return i;
  194. }
  195. /* ---- console printing helpers ---- */
  196. static void put_hex1(unsigned int v)
  197. {
  198. static const char hex[] = "0123456789ABCDEF";
  199. io_putchar(hex[v & 0xF]);
  200. }
  201. static void put_hex2(unsigned int v)
  202. {
  203. put_hex1((v >> 4) & 0xF);
  204. put_hex1(v & 0xF);
  205. }
  206. static void put_hex4(unsigned int v)
  207. {
  208. put_hex2((v >> 8) & 0xFF);
  209. put_hex2(v & 0xFF);
  210. }
  211. static void put_dec(unsigned int v)
  212. {
  213. char b[12];
  214. int i = 0;
  215. if (v == 0) {
  216. io_putchar('0');
  217. return;
  218. }
  219. while (v) {
  220. b[i++] = (char)('0' + (v % 10));
  221. v /= 10;
  222. }
  223. while (i)
  224. io_putchar(b[--i]);
  225. }
  226. /* ---- debug trail (serial) ---- */
  227. static void dbg(const char *s)
  228. {
  229. serial_puts(SERIAL_COM1, "r8168: ");
  230. serial_puts(SERIAL_COM1, s);
  231. serial_puts(SERIAL_COM1, "\n");
  232. }
  233. static const char *chip_name(void)
  234. {
  235. switch (xid & 0x7c8) {
  236. case 0x280: return "RTL8168d/8111d"; /* RTL8112L class */
  237. case 0x3c0: return "RTL8168c/8111c";
  238. case 0x3c8: return "RTL8168cp/8111cp";
  239. case 0x380: return "RTL8168b/8111b";
  240. case 0x2c0: return "RTL8168e/8111e";
  241. default: return "RTL8168/8111 (unknown rev)";
  242. }
  243. }
  244. /* ---- PHY access via PHYAR ---- */
  245. static int phy_busy(void)
  246. {
  247. unsigned int t0 = get_ticks_ms();
  248. while (r32(REG_PHYAR) & PHYAR_FLAG) {
  249. if (timed_out(t0, 100))
  250. return -1;
  251. }
  252. return 0;
  253. }
  254. static int mdio_write(unsigned int reg, unsigned int val)
  255. {
  256. if (phy_busy() != 0)
  257. return -1;
  258. w32(REG_PHYAR, PHYAR_FLAG | ((reg & 0x1f) << 16) | (val & 0xffff));
  259. return 0;
  260. }
  261. static int mdio_read(unsigned int reg)
  262. {
  263. unsigned int t0 = get_ticks_ms();
  264. if (phy_busy() != 0)
  265. return -1;
  266. w32(REG_PHYAR, (reg & 0x1f) << 16);
  267. while (!(r32(REG_PHYAR) & PHYAR_FLAG)) {
  268. if (timed_out(t0, 100))
  269. return -1;
  270. }
  271. return (int)(r32(REG_PHYAR) & 0xffff);
  272. }
  273. /* ---- status ---- */
  274. /* "1000M"/"100M"/"10M"/"?" + duplex letter, into out[6]. */
  275. static void speed_string(unsigned int st, char *out)
  276. {
  277. if (st & PHYST_1000) {
  278. out[0] = '1'; out[1] = '0'; out[2] = '0'; out[3] = '0'; out[4] = 'M';
  279. } else if (st & PHYST_100) {
  280. out[0] = '1'; out[1] = '0'; out[2] = '0'; out[3] = 'M'; out[4] = ' ';
  281. } else if (st & PHYST_10) {
  282. out[0] = '1'; out[1] = '0'; out[2] = 'M'; out[3] = ' '; out[4] = ' ';
  283. } else {
  284. out[0] = '?'; out[1] = out[2] = out[3] = out[4] = ' ';
  285. }
  286. out[5] = '\0';
  287. }
  288. /* Re-arm every RX descriptor and restart the RX engine. The chip can halt
  289. * its RX DMA after a FIFO overflow or when it ran out of descriptors (which
  290. * happens when nothing polls for a while, e.g. while a command is printing);
  291. * re-asserting CmdRxEnb resumes it. */
  292. static void rx_restart(void)
  293. {
  294. unsigned int i;
  295. w8(REG_ChipCmd, ChipCmd_TxEnb); /* stop RX */
  296. for (i = 0; i < 1000; i++) /* let the engine settle */
  297. (void)r8(REG_ChipCmd);
  298. for (i = 0; i < R8168_NRX; i++) {
  299. rx_ring[i].opts2 = 0;
  300. rx_ring[i].addr_lo = (unsigned int)(rx_bufs + i * R8168_BUF_SIZE);
  301. rx_ring[i].addr_hi = 0;
  302. rx_ring[i].opts1 = DescOwn | R8168_RX_ARM |
  303. (i == R8168_NRX - 1 ? DescRingEnd : 0);
  304. }
  305. dma_barrier();
  306. rx_cur = 0;
  307. w8(REG_ChipCmd, ChipCmd_RxEnb | ChipCmd_TxEnb);
  308. rx_restarts++;
  309. }
  310. /* Set by an RX overflow / descriptors-unavailable event: restart the RX
  311. * engine, but only once the frames already in the ring were handed out.
  312. * Restarting at once re-armed all 64 descriptors and threw away up to a
  313. * ring of good frames per overflow, so parallel TCP downloads lost bursts,
  314. * waited for the peers' retransmission timeouts and stalled. */
  315. static int rx_restart_pending;
  316. /* Read + acknowledge latched events; recover from an RX stall. */
  317. static void isr_poll(void)
  318. {
  319. unsigned int st = r16(REG_IntrStatus);
  320. if (st == 0)
  321. return;
  322. w16(REG_IntrStatus, st); /* write back to clear */
  323. isr_last = st;
  324. if (st & ISR_ROVW) {
  325. isr_rovw++;
  326. rx_restart_pending = 1;
  327. }
  328. if (st & ISR_FOV) {
  329. isr_fov++;
  330. rx_restart_pending = 1;
  331. }
  332. if (st & ISR_TDU) {
  333. isr_tdu++;
  334. w8(REG_TxPoll, TxPoll_NPQ); /* nudge the TX queue */
  335. }
  336. }
  337. /* ---- PCI probe ---- */
  338. static int bar_is_mmio(unsigned int val)
  339. {
  340. return !(val & 1u) && (val & 0xFFFFFFF0u);
  341. }
  342. static int probe_pci(void)
  343. {
  344. unsigned int bus, dev, func, bar;
  345. for (bus = 0; bus < 256; bus++) {
  346. for (dev = 0; dev < 32; dev++) {
  347. unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
  348. unsigned int htype, nfunc;
  349. if (id == 0xFFFFFFFFu || id == 0)
  350. continue;
  351. /* Multifunction: scan every function. */
  352. htype = (pci_cfg_read32(bus, dev, 0, 0x0C) >> 16) & 0xFFu;
  353. nfunc = (htype & 0x80u) ? 8u : 1u;
  354. for (func = 0; func < nfunc; func++) {
  355. unsigned int idf = pci_cfg_read32(bus, dev, func, 0x00);
  356. unsigned int cmd;
  357. if (idf == 0xFFFFFFFFu || idf == 0)
  358. continue;
  359. if ((idf & 0xFFFFu) != R8168_VENDOR)
  360. continue;
  361. if (((idf >> 16) & 0xFFFFu) != R8168_DEVICE)
  362. continue;
  363. /* Enable memory space + bus master. */
  364. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  365. pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0006u);
  366. /* First memory BAR (BAR2 on this chip). */
  367. for (bar = 0; bar < 6; bar++) {
  368. unsigned int val = pci_cfg_read32(bus, dev, func,
  369. 0x10 + 4 * bar);
  370. unsigned int hi = 0, mask;
  371. if (!bar_is_mmio(val))
  372. continue;
  373. if (val & 4u) /* 64-bit BAR: high half */
  374. hi = pci_cfg_read32(bus, dev, func,
  375. 0x10 + 4 * bar + 4);
  376. /* Size probe: all-ones, read mask, restore. */
  377. pci_cfg_write32(bus, dev, func, 0x10 + 4 * bar,
  378. 0xFFFFFFFFu);
  379. if (val & 4u)
  380. pci_cfg_write32(bus, dev, func,
  381. 0x10 + 4 * bar + 4,
  382. 0xFFFFFFFFu);
  383. mask = pci_cfg_read32(bus, dev, func,
  384. 0x10 + 4 * bar) & 0xFFFFFFF0u;
  385. pci_cfg_write32(bus, dev, func, 0x10 + 4 * bar, val);
  386. if (val & 4u)
  387. pci_cfg_write32(bus, dev, func,
  388. 0x10 + 4 * bar + 4, hi);
  389. if (mask == 0)
  390. continue;
  391. bar_base = val & 0xFFFFFFF0u;
  392. bar_size = (~mask + 1u);
  393. if (bar_size < 0x1000)
  394. bar_size = 0x1000;
  395. pci_bus = bus;
  396. pci_dev = dev;
  397. pci_func = func;
  398. return 0;
  399. }
  400. }
  401. }
  402. }
  403. return -1;
  404. }
  405. /* ---- bring-up steps ---- */
  406. static int hw_reset(void)
  407. {
  408. unsigned int t0;
  409. w8(REG_ChipCmd, ChipCmd_Reset);
  410. t0 = get_ticks_ms();
  411. while (r8(REG_ChipCmd) & ChipCmd_Reset) {
  412. if (timed_out(t0, 100))
  413. return -1;
  414. }
  415. return 0;
  416. }
  417. static void read_mac(void)
  418. {
  419. unsigned int v, i;
  420. w8(REG_Cfg9346, Cfg9346_Unlock);
  421. v = r32(REG_MAC0);
  422. for (i = 0; i < 4; i++)
  423. mac[i] = (unsigned char)((v >> (8 * i)) & 0xFF);
  424. v = r16(REG_MAC0 + 4);
  425. mac[4] = (unsigned char)(v & 0xFF);
  426. mac[5] = (unsigned char)((v >> 8) & 0xFF);
  427. /* Program the address back into the chip. The MAC filter registers
  428. * (IDR/RAR) must hold our address or unicast frames are dropped, and a
  429. * soft reset may have cleared them; every driver does this after a
  430. * reset. Order matters: MAC4 (high word) first, then MAC0. */
  431. w16(REG_MAC0 + 4, (unsigned short)(mac[4] | (mac[5] << 8)));
  432. w32(REG_MAC0, (unsigned int)mac[0] | ((unsigned int)mac[1] << 8) |
  433. ((unsigned int)mac[2] << 16) | ((unsigned int)mac[3] << 24));
  434. w8(REG_Cfg9346, 0x00);
  435. }
  436. static void setup_rings(void)
  437. {
  438. unsigned int i;
  439. w32(REG_TxDescHigh, 0);
  440. w32(REG_TxDescLow, (unsigned int)tx_ring);
  441. w32(REG_RxDescHigh, 0);
  442. w32(REG_RxDescLow, (unsigned int)rx_ring);
  443. if ((xid & 0x7c8) == 0x3c0 || (xid & 0x7c8) == 0x3c8)
  444. w32(REG_RDSAR1, 0); /* 8168c takes the RX ring high half too */
  445. for (i = 0; i < R8168_NTX; i++) {
  446. tx_ring[i].opts1 = 0;
  447. tx_ring[i].opts2 = 0;
  448. tx_ring[i].addr_lo = (unsigned int)(tx_bufs + i * R8168_BUF_SIZE);
  449. tx_ring[i].addr_hi = 0;
  450. }
  451. for (i = 0; i < R8168_NRX; i++) {
  452. rx_ring[i].opts2 = 0;
  453. rx_ring[i].addr_lo = (unsigned int)(rx_bufs + i * R8168_BUF_SIZE);
  454. rx_ring[i].addr_hi = 0;
  455. rx_ring[i].opts1 = DescOwn | R8168_RX_ARM;
  456. }
  457. rx_ring[R8168_NRX - 1].opts1 |= DescRingEnd;
  458. dma_barrier();
  459. tx_cur = 0;
  460. rx_cur = 0;
  461. }
  462. static void set_rx_mode(void)
  463. {
  464. unsigned int cfg = r32(REG_RxConfig);
  465. /* Promiscuous for bring-up (same choice as the ne2000 driver): keeps
  466. * NIC and switch filtering out of the picture while we validate the
  467. * DMA path; netrecv is a diagnosis tool and wants to see everything. */
  468. cfg = (cfg & ~RX_ACCEPT_MASK) |
  469. RX_ACCEPT_BROADCAST | RX_ACCEPT_MULTICAST | RX_ACCEPT_MYPHYS |
  470. RX_ACCEPT_ALLPHYS;
  471. w32(REG_RxConfig, cfg);
  472. }
  473. /* Bounded poll of PHYstatus.LINK. Only used to recover a link that was
  474. * established before the probe reset; a missing cable must not delay boot. */
  475. static int wait_link(unsigned int timeout_ms)
  476. {
  477. unsigned int t0 = get_ticks_ms();
  478. while (!(r8(REG_PHYstatus) & PHYST_LINK)) {
  479. if (timed_out(t0, timeout_ms))
  480. return 0;
  481. __asm__ volatile ("pause");
  482. }
  483. return 1;
  484. }
  485. static void phy_autoneg(void)
  486. {
  487. int id1 = mdio_read(MII_PHYSID1);
  488. int id2 = mdio_read(MII_PHYSID2);
  489. serial_puts(SERIAL_COM1, "r8168: phy id ");
  490. serial_puthex(SERIAL_COM1, (unsigned int)(id1 < 0 ? 0xFFFF : id1));
  491. serial_puts(SERIAL_COM1, ":");
  492. serial_puthex(SERIAL_COM1, (unsigned int)(id2 < 0 ? 0xFFFF : id2));
  493. serial_puts(SERIAL_COM1, "\n");
  494. mdio_write(MII_ANAR, 0x01E1); /* advertise 10/100 half+full */
  495. mdio_write(MII_GBSR, 0x0300); /* advertise 1000 half+full */
  496. mdio_write(MII_BMCR, BMCR_AUTONEG | BMCR_RESTART_AN);
  497. }
  498. /* ---- netdev interface ---- */
  499. /* Rings + configuration registers + PHY + RX/TX enable. Used by both the
  500. * first probe and `netreset`; the latter forces a fresh auto-negotiation. */
  501. static void hw_start(int restart_phy)
  502. {
  503. w8(REG_Cfg9346, Cfg9346_Unlock);
  504. setup_rings();
  505. w32(REG_RxConfig, RX128_INT_EN | RX_DMA_BURST);
  506. set_rx_mode();
  507. w32(REG_TxConfig, TXCONFIG_VALUE);
  508. w16(REG_RxMaxSize, R8168_BUF_SIZE);
  509. w8(REG_MaxTxPacket, 0x3f);
  510. w16(REG_IntrMitigate, 0x0000);
  511. w16(REG_IntrMask, 0x0000);
  512. w16(REG_IntrStatus, 0xFFFF);
  513. w8(REG_Cfg9346, 0x00);
  514. dbg("regs ok");
  515. /* Keep a link the firmware already established: the boot network
  516. * policy samples PHYstatus to choose between wired and WiFi, and
  517. * restarting auto-negotiation would make a plugged cable look down. */
  518. if (restart_phy || !(r8(REG_PHYstatus) & PHYST_LINK))
  519. phy_autoneg();
  520. dbg("phy up");
  521. w8(REG_ChipCmd, ChipCmd_RxEnb | ChipCmd_TxEnb);
  522. /* Auto-negotiation runs in the background. Do not wait for link here:
  523. * that used to add up to 5 s to boot (and to every netreset), while
  524. * DHCP already retries and netinfo reports the live link state. */
  525. dbg(r8(REG_PHYstatus) & PHYST_LINK ? "link up" : "link started");
  526. }
  527. int r8168_init(void)
  528. {
  529. char buf[40];
  530. unsigned int i;
  531. int had_link;
  532. if (inited)
  533. return present ? 0 : -1;
  534. inited = 1;
  535. if (probe_pci() != 0) {
  536. dbg("no 10ec:8168 found");
  537. return -1;
  538. }
  539. /* "PCI 04:00.0 f8fff000 4K" */
  540. i = 0;
  541. i = app(buf, i, "PCI ");
  542. i = apph(buf, i, pci_bus, 2);
  543. buf[i++] = ':';
  544. i = apph(buf, i, pci_dev, 2);
  545. buf[i++] = '.';
  546. buf[i++] = (char)('0' + pci_func);
  547. buf[i++] = ' ';
  548. i = apph(buf, i, bar_base, 8);
  549. buf[i++] = ' ';
  550. i = appd(buf, i, bar_size / 1024);
  551. buf[i++] = 'K';
  552. buf[i] = '\0';
  553. dbg(buf);
  554. mmio = ioremap(bar_base, bar_size, IOREMAP_MMIO);
  555. if (!mmio)
  556. return -1;
  557. /* Master abort / unmapped window reads back all-ones. */
  558. if (r32(REG_TxConfig) == 0xFFFFFFFFu) {
  559. dbg("mmio read failed");
  560. mmio = 0;
  561. return -1;
  562. }
  563. xid = (r32(REG_TxConfig) >> 20) & 0xfcf;
  564. /* Sample before the reset: a cable that was already linked keeps
  565. * the boot DHCP on the wired path instead of falling back to WiFi. */
  566. had_link = (r8(REG_PHYstatus) & PHYST_LINK) != 0;
  567. if (hw_reset() != 0) {
  568. dbg("reset timeout");
  569. mmio = 0;
  570. return -1;
  571. }
  572. dbg("reset ok");
  573. read_mac();
  574. /* Ring memory: one page each (256-byte aligned); buffers in pages. */
  575. tx_ring = (struct r8168_desc *)mm_alloc_page();
  576. rx_ring = (struct r8168_desc *)mm_alloc_page();
  577. tx_bufs = (unsigned char *)mm_alloc_pages(R8168_NTX * R8168_BUF_SIZE / PAGE_SIZE);
  578. rx_bufs = (unsigned char *)mm_alloc_pages(R8168_NRX * R8168_BUF_SIZE / PAGE_SIZE);
  579. if (!tx_ring || !rx_ring || !tx_bufs || !rx_bufs) {
  580. dbg("no memory for rings");
  581. mmio = 0;
  582. return -1;
  583. }
  584. dbg("ring memory ok");
  585. hw_start(0);
  586. if (had_link && !(r8(REG_PHYstatus) & PHYST_LINK))
  587. wait_link(3000);
  588. /* "<chip> XID <xxx> <mac>" */
  589. i = 0;
  590. i = app(buf, i, chip_name());
  591. buf[i++] = ' ';
  592. i = app(buf, i, "XID ");
  593. i = apph(buf, i, xid, 3);
  594. buf[i++] = ' ';
  595. for (unsigned int j = 0; j < 6; j++) {
  596. i = apph(buf, i, mac[j], 2);
  597. if (j != 5)
  598. buf[i++] = ':';
  599. }
  600. buf[i] = '\0';
  601. dbg(buf);
  602. present = 1;
  603. return 0;
  604. }
  605. /* `netreset`: full re-init of an already-probed NIC (used to recover from an
  606. * RX stall and as a bring-up experiment). */
  607. void r8168_reset(void)
  608. {
  609. if (!present || !mmio || !rx_ring) {
  610. dbg("reset: not initialized");
  611. return;
  612. }
  613. if (hw_reset() != 0) {
  614. dbg("reset timeout");
  615. return;
  616. }
  617. read_mac();
  618. hw_start(1);
  619. dbg("netreset done");
  620. }
  621. void r8168_get_mac(unsigned char out[6])
  622. {
  623. unsigned int i;
  624. for (i = 0; i < 6; i++)
  625. out[i] = mac[i];
  626. }
  627. int r8168_tx(const unsigned char *buf, unsigned int len)
  628. {
  629. static unsigned char pad[60];
  630. struct r8168_desc *d;
  631. unsigned int entry, opts1, t0;
  632. const unsigned char *p = buf;
  633. unsigned int txlen = len;
  634. if (!present || !mmio)
  635. return -1;
  636. if (len < 14 || len > 1514)
  637. return -1;
  638. isr_poll();
  639. if (len < 60) {
  640. unsigned int i;
  641. for (i = 0; i < len; i++)
  642. pad[i] = buf[i];
  643. for (i = len; i < 60; i++)
  644. pad[i] = 0;
  645. p = pad;
  646. txlen = 60;
  647. }
  648. entry = tx_cur;
  649. d = &tx_ring[entry];
  650. /* Wait for a previous use of this slot to complete. */
  651. t0 = get_ticks_ms();
  652. while (d->opts1 & DescOwn) {
  653. if (timed_out(t0, 100)) {
  654. tx_err++;
  655. return -1;
  656. }
  657. }
  658. {
  659. unsigned char *dst = tx_bufs + entry * R8168_BUF_SIZE;
  660. unsigned int i;
  661. for (i = 0; i < txlen; i++)
  662. dst[i] = p[i];
  663. }
  664. opts1 = txlen | DescFirstFrag | DescLastFrag;
  665. if (entry == R8168_NTX - 1)
  666. opts1 |= DescRingEnd;
  667. d->addr_lo = (unsigned int)(tx_bufs + entry * R8168_BUF_SIZE);
  668. d->addr_hi = 0;
  669. d->opts2 = 0;
  670. dma_barrier();
  671. d->opts1 = opts1 | DescOwn;
  672. dma_barrier();
  673. w8(REG_TxPoll, TxPoll_NPQ);
  674. t0 = get_ticks_ms();
  675. while (d->opts1 & DescOwn) {
  676. if (timed_out(t0, 100)) {
  677. tx_err++;
  678. return -1;
  679. }
  680. }
  681. tx_cur = (entry + 1) % R8168_NTX;
  682. last_tx_status = d->opts1;
  683. tx_ok++;
  684. return 0;
  685. }
  686. /* Re-arm one RX descriptor (clears owner for the NIC). */
  687. static void rx_arm(unsigned int entry)
  688. {
  689. rx_ring[entry].opts2 = 0;
  690. dma_barrier();
  691. rx_ring[entry].opts1 = DescOwn | R8168_RX_ARM |
  692. (entry == R8168_NRX - 1 ? DescRingEnd : 0);
  693. dma_barrier();
  694. }
  695. int r8168_rx(unsigned char *buf, unsigned int maxlen)
  696. {
  697. unsigned int guard = 0;
  698. if (!present || !mmio)
  699. return -1;
  700. isr_poll();
  701. while (guard++ < 32) {
  702. unsigned int entry = rx_cur;
  703. struct r8168_desc *d = &rx_ring[entry];
  704. unsigned int status = d->opts1;
  705. unsigned int len;
  706. if (status & DescOwn) {
  707. /* Drained: a pending restart loses nothing now. */
  708. if (rx_restart_pending) {
  709. rx_restart_pending = 0;
  710. rx_restart();
  711. }
  712. return -1; /* nothing pending */
  713. }
  714. dma_barrier();
  715. if (status & RxRES) {
  716. rx_err++;
  717. } else if ((status & (DescFirstFrag | DescLastFrag)) !=
  718. (DescFirstFrag | DescLastFrag)) {
  719. rx_drop++; /* fragmented frame: too big */
  720. } else {
  721. len = status & RxPktLenMask;
  722. if (len >= 4)
  723. len -= 4; /* drop the trailing FCS */
  724. if (len < 14 || len > maxlen) {
  725. rx_drop++;
  726. } else {
  727. unsigned char *src = rx_bufs + entry * R8168_BUF_SIZE;
  728. unsigned int i;
  729. for (i = 0; i < len; i++)
  730. buf[i] = src[i];
  731. rx_ok++;
  732. rx_arm(entry);
  733. rx_cur = (rx_cur + 1) % R8168_NRX;
  734. return (int)len;
  735. }
  736. }
  737. rx_arm(entry);
  738. rx_cur = (rx_cur + 1) % R8168_NRX;
  739. }
  740. return -1;
  741. }
  742. /* PHY loopback for a self-test (driver op): transmitted frames are looped
  743. * back into the receiver by the PHY, so TX and RX can be validated with no
  744. * cable, switch or peer. Forces 100M full duplex while enabled. */
  745. static int saved_bmcr = -1;
  746. int r8168_set_loopback(int on)
  747. {
  748. if (!present || !mmio)
  749. return -1;
  750. if (on) {
  751. saved_bmcr = mdio_read(MII_BMCR);
  752. mdio_write(MII_BMCR, 0x2000 | 0x0100 | 0x4000); /* 100M FD + LB */
  753. } else {
  754. int bmcr = saved_bmcr >= 0 ? (saved_bmcr & 0x7fff) : 0;
  755. unsigned int t0;
  756. mdio_write(MII_BMCR, bmcr | BMCR_AUTONEG | BMCR_RESTART_AN);
  757. t0 = get_ticks_ms();
  758. while (get_ticks_ms() - t0 < 2000) {
  759. if (r8(REG_PHYstatus) & PHYST_LINK)
  760. break;
  761. }
  762. }
  763. return 0;
  764. }
  765. void r8168_print_info(void)
  766. {
  767. char sp[6];
  768. unsigned int st, i;
  769. if (!present) {
  770. io_putstr("r8168: no RTL8111/8168 found\n");
  771. return;
  772. }
  773. io_putstr(chip_name());
  774. io_putstr(" XID ");
  775. put_hex4(xid);
  776. io_putstr(" at ");
  777. put_hex2(pci_bus);
  778. io_putchar(':');
  779. put_hex2(pci_dev);
  780. io_putchar('.');
  781. io_putchar((char)('0' + pci_func));
  782. io_putchar('\n');
  783. io_putstr(" bar ");
  784. put_hex4(bar_base >> 16);
  785. put_hex4(bar_base & 0xFFFF);
  786. io_putstr(" (");
  787. put_dec(bar_size / 1024);
  788. io_putstr("K) mac=");
  789. for (i = 0; i < 6; i++) {
  790. if (i)
  791. io_putchar(':');
  792. put_hex2(mac[i]);
  793. }
  794. io_putchar('\n');
  795. st = r8(REG_PHYstatus);
  796. speed_string(st, sp);
  797. io_putstr(" link ");
  798. io_putstr((st & PHYST_LINK) ? "up " : "down ");
  799. io_putstr(sp);
  800. io_putstr((st & PHYST_FULLDUP) ? "F" : "H");
  801. io_putstr(" tx ");
  802. put_dec(tx_ok);
  803. io_putchar('/');
  804. put_dec(tx_err);
  805. io_putstr(" rx ");
  806. put_dec(rx_ok);
  807. io_putchar('/');
  808. put_dec(rx_err);
  809. io_putchar('\n');
  810. }
  811. /* Physical link state: kernel_main and `wifi autoconnect` prefer a wired
  812. * link over the saved WiFi network. */
  813. static int r8168_link_up(void)
  814. {
  815. if (!present || !mmio)
  816. return 0;
  817. return (r8(REG_PHYstatus) & PHYST_LINK) ? 1 : 0;
  818. }
  819. void r8168_dump_regs(void)
  820. {
  821. static const struct {
  822. const char *name;
  823. unsigned int reg;
  824. } regs[] = {
  825. { "MAC0", REG_MAC0 }, { "TxDescLo", REG_TxDescLow },
  826. { "TxDescHi", REG_TxDescHigh }, { "ChipCmd", REG_ChipCmd },
  827. { "IntrMask", REG_IntrMask }, { "IntrStat", REG_IntrStatus },
  828. { "TxConfig", REG_TxConfig }, { "RxConfig", REG_RxConfig },
  829. { "PHYAR", REG_PHYAR }, { "PHYstat", REG_PHYstatus },
  830. { "RxMaxSize", REG_RxMaxSize }, { "CPlusCmd", REG_CPlusCmd },
  831. { "RxDescLo", REG_RxDescLow }, { "RxDescHi", REG_RxDescHigh },
  832. };
  833. unsigned int i;
  834. if (!present || !mmio) {
  835. io_putstr("r8168: no NIC\n");
  836. return;
  837. }
  838. for (i = 0; i < sizeof(regs) / sizeof(regs[0]); i++) {
  839. io_putstr(regs[i].name);
  840. io_putstr("=");
  841. io_puthex(r32(regs[i].reg));
  842. if (i & 1)
  843. io_putchar('\n');
  844. else
  845. io_putstr(" ");
  846. }
  847. if (i & 1)
  848. io_putchar('\n');
  849. io_putstr("state tx_ok=");
  850. put_dec(tx_ok);
  851. io_putstr(" tx_err=");
  852. put_dec(tx_err);
  853. io_putstr(" rx_ok=");
  854. put_dec(rx_ok);
  855. io_putstr(" rx_err=");
  856. put_dec(rx_err);
  857. io_putstr(" rx_drop=");
  858. put_dec(rx_drop);
  859. io_putstr(" rx_restart=");
  860. put_dec(rx_restarts);
  861. io_putchar('\n');
  862. io_putstr("isr ");
  863. if (isr_last == 0) {
  864. io_putstr("(none)");
  865. } else {
  866. if (isr_last & ISR_RxOK) io_putstr("RxOK ");
  867. if (isr_last & ISR_RxErr) io_putstr("RxErr ");
  868. if (isr_last & ISR_TxOK) io_putstr("TxOK ");
  869. if (isr_last & ISR_TxErr) io_putstr("TxErr ");
  870. if (isr_last & ISR_ROVW) io_putstr("ROVW ");
  871. if (isr_last & ISR_LinkChg) io_putstr("LinkChg ");
  872. if (isr_last & ISR_FOV) io_putstr("FOV ");
  873. if (isr_last & ISR_TDU) io_putstr("TDU ");
  874. if (isr_last & ISR_SWInt) io_putstr("SWInt ");
  875. }
  876. io_putstr(" evt rovw=");
  877. put_dec(isr_rovw);
  878. io_putstr(" fov=");
  879. put_dec(isr_fov);
  880. io_putstr(" tdu=");
  881. put_dec(isr_tdu);
  882. io_putchar('\n');
  883. io_putstr("tx status ");
  884. io_puthex(last_tx_status);
  885. io_putstr(" ring tx ");
  886. io_puthex((unsigned int)tx_ring);
  887. io_putstr(" rx ");
  888. io_puthex((unsigned int)rx_ring);
  889. io_putchar('\n');
  890. }
  891. const struct netdev r8168_netdev = {
  892. "r8168", "RTL8111/8168",
  893. r8168_init, r8168_get_mac, r8168_rx, r8168_tx, r8168_print_info,
  894. r8168_dump_regs, r8168_set_loopback, r8168_reset,
  895. 0, 0, 0, 0, 0, 0,
  896. r8168_link_up,
  897. };