r8168.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. /* Read + acknowledge latched events; recover from an RX stall. */
  311. static void isr_poll(void)
  312. {
  313. unsigned int st = r16(REG_IntrStatus);
  314. if (st == 0)
  315. return;
  316. w16(REG_IntrStatus, st); /* write back to clear */
  317. isr_last = st;
  318. if (st & ISR_ROVW) {
  319. isr_rovw++;
  320. rx_restart();
  321. }
  322. if (st & ISR_FOV) {
  323. isr_fov++;
  324. rx_restart();
  325. }
  326. if (st & ISR_TDU) {
  327. isr_tdu++;
  328. w8(REG_TxPoll, TxPoll_NPQ); /* nudge the TX queue */
  329. }
  330. }
  331. /* ---- PCI probe ---- */
  332. static int bar_is_mmio(unsigned int val)
  333. {
  334. return !(val & 1u) && (val & 0xFFFFFFF0u);
  335. }
  336. static int probe_pci(void)
  337. {
  338. unsigned int bus, dev, func, bar;
  339. for (bus = 0; bus < 256; bus++) {
  340. for (dev = 0; dev < 32; dev++) {
  341. unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
  342. unsigned int htype, nfunc;
  343. if (id == 0xFFFFFFFFu || id == 0)
  344. continue;
  345. /* Multifunction: scan every function. */
  346. htype = (pci_cfg_read32(bus, dev, 0, 0x0C) >> 16) & 0xFFu;
  347. nfunc = (htype & 0x80u) ? 8u : 1u;
  348. for (func = 0; func < nfunc; func++) {
  349. unsigned int idf = pci_cfg_read32(bus, dev, func, 0x00);
  350. unsigned int cmd;
  351. if (idf == 0xFFFFFFFFu || idf == 0)
  352. continue;
  353. if ((idf & 0xFFFFu) != R8168_VENDOR)
  354. continue;
  355. if (((idf >> 16) & 0xFFFFu) != R8168_DEVICE)
  356. continue;
  357. /* Enable memory space + bus master. */
  358. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  359. pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0006u);
  360. /* First memory BAR (BAR2 on this chip). */
  361. for (bar = 0; bar < 6; bar++) {
  362. unsigned int val = pci_cfg_read32(bus, dev, func,
  363. 0x10 + 4 * bar);
  364. unsigned int hi = 0, mask;
  365. if (!bar_is_mmio(val))
  366. continue;
  367. if (val & 4u) /* 64-bit BAR: high half */
  368. hi = pci_cfg_read32(bus, dev, func,
  369. 0x10 + 4 * bar + 4);
  370. /* Size probe: all-ones, read mask, restore. */
  371. pci_cfg_write32(bus, dev, func, 0x10 + 4 * bar,
  372. 0xFFFFFFFFu);
  373. if (val & 4u)
  374. pci_cfg_write32(bus, dev, func,
  375. 0x10 + 4 * bar + 4,
  376. 0xFFFFFFFFu);
  377. mask = pci_cfg_read32(bus, dev, func,
  378. 0x10 + 4 * bar) & 0xFFFFFFF0u;
  379. pci_cfg_write32(bus, dev, func, 0x10 + 4 * bar, val);
  380. if (val & 4u)
  381. pci_cfg_write32(bus, dev, func,
  382. 0x10 + 4 * bar + 4, hi);
  383. if (mask == 0)
  384. continue;
  385. bar_base = val & 0xFFFFFFF0u;
  386. bar_size = (~mask + 1u);
  387. if (bar_size < 0x1000)
  388. bar_size = 0x1000;
  389. pci_bus = bus;
  390. pci_dev = dev;
  391. pci_func = func;
  392. return 0;
  393. }
  394. }
  395. }
  396. }
  397. return -1;
  398. }
  399. /* ---- bring-up steps ---- */
  400. static int hw_reset(void)
  401. {
  402. unsigned int t0;
  403. w8(REG_ChipCmd, ChipCmd_Reset);
  404. t0 = get_ticks_ms();
  405. while (r8(REG_ChipCmd) & ChipCmd_Reset) {
  406. if (timed_out(t0, 100))
  407. return -1;
  408. }
  409. return 0;
  410. }
  411. static void read_mac(void)
  412. {
  413. unsigned int v, i;
  414. w8(REG_Cfg9346, Cfg9346_Unlock);
  415. v = r32(REG_MAC0);
  416. for (i = 0; i < 4; i++)
  417. mac[i] = (unsigned char)((v >> (8 * i)) & 0xFF);
  418. v = r16(REG_MAC0 + 4);
  419. mac[4] = (unsigned char)(v & 0xFF);
  420. mac[5] = (unsigned char)((v >> 8) & 0xFF);
  421. /* Program the address back into the chip. The MAC filter registers
  422. * (IDR/RAR) must hold our address or unicast frames are dropped, and a
  423. * soft reset may have cleared them; every driver does this after a
  424. * reset. Order matters: MAC4 (high word) first, then MAC0. */
  425. w16(REG_MAC0 + 4, (unsigned short)(mac[4] | (mac[5] << 8)));
  426. w32(REG_MAC0, (unsigned int)mac[0] | ((unsigned int)mac[1] << 8) |
  427. ((unsigned int)mac[2] << 16) | ((unsigned int)mac[3] << 24));
  428. w8(REG_Cfg9346, 0x00);
  429. }
  430. static void setup_rings(void)
  431. {
  432. unsigned int i;
  433. w32(REG_TxDescHigh, 0);
  434. w32(REG_TxDescLow, (unsigned int)tx_ring);
  435. w32(REG_RxDescHigh, 0);
  436. w32(REG_RxDescLow, (unsigned int)rx_ring);
  437. if ((xid & 0x7c8) == 0x3c0 || (xid & 0x7c8) == 0x3c8)
  438. w32(REG_RDSAR1, 0); /* 8168c takes the RX ring high half too */
  439. for (i = 0; i < R8168_NTX; i++) {
  440. tx_ring[i].opts1 = 0;
  441. tx_ring[i].opts2 = 0;
  442. tx_ring[i].addr_lo = (unsigned int)(tx_bufs + i * R8168_BUF_SIZE);
  443. tx_ring[i].addr_hi = 0;
  444. }
  445. for (i = 0; i < R8168_NRX; i++) {
  446. rx_ring[i].opts2 = 0;
  447. rx_ring[i].addr_lo = (unsigned int)(rx_bufs + i * R8168_BUF_SIZE);
  448. rx_ring[i].addr_hi = 0;
  449. rx_ring[i].opts1 = DescOwn | R8168_RX_ARM;
  450. }
  451. rx_ring[R8168_NRX - 1].opts1 |= DescRingEnd;
  452. dma_barrier();
  453. tx_cur = 0;
  454. rx_cur = 0;
  455. }
  456. static void set_rx_mode(void)
  457. {
  458. unsigned int cfg = r32(REG_RxConfig);
  459. /* Promiscuous for bring-up (same choice as the ne2000 driver): keeps
  460. * NIC and switch filtering out of the picture while we validate the
  461. * DMA path; netrecv is a diagnosis tool and wants to see everything. */
  462. cfg = (cfg & ~RX_ACCEPT_MASK) |
  463. RX_ACCEPT_BROADCAST | RX_ACCEPT_MULTICAST | RX_ACCEPT_MYPHYS |
  464. RX_ACCEPT_ALLPHYS;
  465. w32(REG_RxConfig, cfg);
  466. }
  467. /* Bounded poll of PHYstatus.LINK. Only used to recover a link that was
  468. * established before the probe reset; a missing cable must not delay boot. */
  469. static int wait_link(unsigned int timeout_ms)
  470. {
  471. unsigned int t0 = get_ticks_ms();
  472. while (!(r8(REG_PHYstatus) & PHYST_LINK)) {
  473. if (timed_out(t0, timeout_ms))
  474. return 0;
  475. __asm__ volatile ("pause");
  476. }
  477. return 1;
  478. }
  479. static void phy_autoneg(void)
  480. {
  481. int id1 = mdio_read(MII_PHYSID1);
  482. int id2 = mdio_read(MII_PHYSID2);
  483. serial_puts(SERIAL_COM1, "r8168: phy id ");
  484. serial_puthex(SERIAL_COM1, (unsigned int)(id1 < 0 ? 0xFFFF : id1));
  485. serial_puts(SERIAL_COM1, ":");
  486. serial_puthex(SERIAL_COM1, (unsigned int)(id2 < 0 ? 0xFFFF : id2));
  487. serial_puts(SERIAL_COM1, "\n");
  488. mdio_write(MII_ANAR, 0x01E1); /* advertise 10/100 half+full */
  489. mdio_write(MII_GBSR, 0x0300); /* advertise 1000 half+full */
  490. mdio_write(MII_BMCR, BMCR_AUTONEG | BMCR_RESTART_AN);
  491. }
  492. /* ---- netdev interface ---- */
  493. /* Rings + configuration registers + PHY + RX/TX enable. Used by both the
  494. * first probe and `netreset`; the latter forces a fresh auto-negotiation. */
  495. static void hw_start(int restart_phy)
  496. {
  497. w8(REG_Cfg9346, Cfg9346_Unlock);
  498. setup_rings();
  499. w32(REG_RxConfig, RX128_INT_EN | RX_DMA_BURST);
  500. set_rx_mode();
  501. w32(REG_TxConfig, TXCONFIG_VALUE);
  502. w16(REG_RxMaxSize, R8168_BUF_SIZE);
  503. w8(REG_MaxTxPacket, 0x3f);
  504. w16(REG_IntrMitigate, 0x0000);
  505. w16(REG_IntrMask, 0x0000);
  506. w16(REG_IntrStatus, 0xFFFF);
  507. w8(REG_Cfg9346, 0x00);
  508. dbg("regs ok");
  509. /* Keep a link the firmware already established: the boot network
  510. * policy samples PHYstatus to choose between wired and WiFi, and
  511. * restarting auto-negotiation would make a plugged cable look down. */
  512. if (restart_phy || !(r8(REG_PHYstatus) & PHYST_LINK))
  513. phy_autoneg();
  514. dbg("phy up");
  515. w8(REG_ChipCmd, ChipCmd_RxEnb | ChipCmd_TxEnb);
  516. /* Auto-negotiation runs in the background. Do not wait for link here:
  517. * that used to add up to 5 s to boot (and to every netreset), while
  518. * DHCP already retries and netinfo reports the live link state. */
  519. dbg(r8(REG_PHYstatus) & PHYST_LINK ? "link up" : "link started");
  520. }
  521. int r8168_init(void)
  522. {
  523. char buf[40];
  524. unsigned int i;
  525. int had_link;
  526. if (inited)
  527. return present ? 0 : -1;
  528. inited = 1;
  529. if (probe_pci() != 0) {
  530. dbg("no 10ec:8168 found");
  531. return -1;
  532. }
  533. /* "PCI 04:00.0 f8fff000 4K" */
  534. i = 0;
  535. i = app(buf, i, "PCI ");
  536. i = apph(buf, i, pci_bus, 2);
  537. buf[i++] = ':';
  538. i = apph(buf, i, pci_dev, 2);
  539. buf[i++] = '.';
  540. buf[i++] = (char)('0' + pci_func);
  541. buf[i++] = ' ';
  542. i = apph(buf, i, bar_base, 8);
  543. buf[i++] = ' ';
  544. i = appd(buf, i, bar_size / 1024);
  545. buf[i++] = 'K';
  546. buf[i] = '\0';
  547. dbg(buf);
  548. mmio = ioremap(bar_base, bar_size, IOREMAP_MMIO);
  549. if (!mmio)
  550. return -1;
  551. /* Master abort / unmapped window reads back all-ones. */
  552. if (r32(REG_TxConfig) == 0xFFFFFFFFu) {
  553. dbg("mmio read failed");
  554. mmio = 0;
  555. return -1;
  556. }
  557. xid = (r32(REG_TxConfig) >> 20) & 0xfcf;
  558. /* Sample before the reset: a cable that was already linked keeps
  559. * the boot DHCP on the wired path instead of falling back to WiFi. */
  560. had_link = (r8(REG_PHYstatus) & PHYST_LINK) != 0;
  561. if (hw_reset() != 0) {
  562. dbg("reset timeout");
  563. mmio = 0;
  564. return -1;
  565. }
  566. dbg("reset ok");
  567. read_mac();
  568. /* Ring memory: one page each (256-byte aligned); buffers in pages. */
  569. tx_ring = (struct r8168_desc *)mm_alloc_page();
  570. rx_ring = (struct r8168_desc *)mm_alloc_page();
  571. tx_bufs = (unsigned char *)mm_alloc_pages(R8168_NTX * R8168_BUF_SIZE / PAGE_SIZE);
  572. rx_bufs = (unsigned char *)mm_alloc_pages(R8168_NRX * R8168_BUF_SIZE / PAGE_SIZE);
  573. if (!tx_ring || !rx_ring || !tx_bufs || !rx_bufs) {
  574. dbg("no memory for rings");
  575. mmio = 0;
  576. return -1;
  577. }
  578. dbg("ring memory ok");
  579. hw_start(0);
  580. if (had_link && !(r8(REG_PHYstatus) & PHYST_LINK))
  581. wait_link(3000);
  582. /* "<chip> XID <xxx> <mac>" */
  583. i = 0;
  584. i = app(buf, i, chip_name());
  585. buf[i++] = ' ';
  586. i = app(buf, i, "XID ");
  587. i = apph(buf, i, xid, 3);
  588. buf[i++] = ' ';
  589. for (unsigned int j = 0; j < 6; j++) {
  590. i = apph(buf, i, mac[j], 2);
  591. if (j != 5)
  592. buf[i++] = ':';
  593. }
  594. buf[i] = '\0';
  595. dbg(buf);
  596. present = 1;
  597. return 0;
  598. }
  599. /* `netreset`: full re-init of an already-probed NIC (used to recover from an
  600. * RX stall and as a bring-up experiment). */
  601. void r8168_reset(void)
  602. {
  603. if (!present || !mmio || !rx_ring) {
  604. dbg("reset: not initialized");
  605. return;
  606. }
  607. if (hw_reset() != 0) {
  608. dbg("reset timeout");
  609. return;
  610. }
  611. read_mac();
  612. hw_start(1);
  613. dbg("netreset done");
  614. }
  615. void r8168_get_mac(unsigned char out[6])
  616. {
  617. unsigned int i;
  618. for (i = 0; i < 6; i++)
  619. out[i] = mac[i];
  620. }
  621. int r8168_tx(const unsigned char *buf, unsigned int len)
  622. {
  623. static unsigned char pad[60];
  624. struct r8168_desc *d;
  625. unsigned int entry, opts1, t0;
  626. const unsigned char *p = buf;
  627. unsigned int txlen = len;
  628. if (!present || !mmio)
  629. return -1;
  630. if (len < 14 || len > 1514)
  631. return -1;
  632. isr_poll();
  633. if (len < 60) {
  634. unsigned int i;
  635. for (i = 0; i < len; i++)
  636. pad[i] = buf[i];
  637. for (i = len; i < 60; i++)
  638. pad[i] = 0;
  639. p = pad;
  640. txlen = 60;
  641. }
  642. entry = tx_cur;
  643. d = &tx_ring[entry];
  644. /* Wait for a previous use of this slot to complete. */
  645. t0 = get_ticks_ms();
  646. while (d->opts1 & DescOwn) {
  647. if (timed_out(t0, 100)) {
  648. tx_err++;
  649. return -1;
  650. }
  651. }
  652. {
  653. unsigned char *dst = tx_bufs + entry * R8168_BUF_SIZE;
  654. unsigned int i;
  655. for (i = 0; i < txlen; i++)
  656. dst[i] = p[i];
  657. }
  658. opts1 = txlen | DescFirstFrag | DescLastFrag;
  659. if (entry == R8168_NTX - 1)
  660. opts1 |= DescRingEnd;
  661. d->addr_lo = (unsigned int)(tx_bufs + entry * R8168_BUF_SIZE);
  662. d->addr_hi = 0;
  663. d->opts2 = 0;
  664. dma_barrier();
  665. d->opts1 = opts1 | DescOwn;
  666. dma_barrier();
  667. w8(REG_TxPoll, TxPoll_NPQ);
  668. t0 = get_ticks_ms();
  669. while (d->opts1 & DescOwn) {
  670. if (timed_out(t0, 100)) {
  671. tx_err++;
  672. return -1;
  673. }
  674. }
  675. tx_cur = (entry + 1) % R8168_NTX;
  676. last_tx_status = d->opts1;
  677. tx_ok++;
  678. return 0;
  679. }
  680. /* Re-arm one RX descriptor (clears owner for the NIC). */
  681. static void rx_arm(unsigned int entry)
  682. {
  683. rx_ring[entry].opts2 = 0;
  684. dma_barrier();
  685. rx_ring[entry].opts1 = DescOwn | R8168_RX_ARM |
  686. (entry == R8168_NRX - 1 ? DescRingEnd : 0);
  687. dma_barrier();
  688. }
  689. int r8168_rx(unsigned char *buf, unsigned int maxlen)
  690. {
  691. unsigned int guard = 0;
  692. if (!present || !mmio)
  693. return -1;
  694. isr_poll();
  695. while (guard++ < 32) {
  696. unsigned int entry = rx_cur;
  697. struct r8168_desc *d = &rx_ring[entry];
  698. unsigned int status = d->opts1;
  699. unsigned int len;
  700. if (status & DescOwn)
  701. return -1; /* nothing pending */
  702. dma_barrier();
  703. if (status & RxRES) {
  704. rx_err++;
  705. } else if ((status & (DescFirstFrag | DescLastFrag)) !=
  706. (DescFirstFrag | DescLastFrag)) {
  707. rx_drop++; /* fragmented frame: too big */
  708. } else {
  709. len = status & RxPktLenMask;
  710. if (len >= 4)
  711. len -= 4; /* drop the trailing FCS */
  712. if (len < 14 || len > maxlen) {
  713. rx_drop++;
  714. } else {
  715. unsigned char *src = rx_bufs + entry * R8168_BUF_SIZE;
  716. unsigned int i;
  717. for (i = 0; i < len; i++)
  718. buf[i] = src[i];
  719. rx_ok++;
  720. rx_arm(entry);
  721. rx_cur = (rx_cur + 1) % R8168_NRX;
  722. return (int)len;
  723. }
  724. }
  725. rx_arm(entry);
  726. rx_cur = (rx_cur + 1) % R8168_NRX;
  727. }
  728. return -1;
  729. }
  730. /* PHY loopback for a self-test (driver op): transmitted frames are looped
  731. * back into the receiver by the PHY, so TX and RX can be validated with no
  732. * cable, switch or peer. Forces 100M full duplex while enabled. */
  733. static int saved_bmcr = -1;
  734. int r8168_set_loopback(int on)
  735. {
  736. if (!present || !mmio)
  737. return -1;
  738. if (on) {
  739. saved_bmcr = mdio_read(MII_BMCR);
  740. mdio_write(MII_BMCR, 0x2000 | 0x0100 | 0x4000); /* 100M FD + LB */
  741. } else {
  742. int bmcr = saved_bmcr >= 0 ? (saved_bmcr & 0x7fff) : 0;
  743. unsigned int t0;
  744. mdio_write(MII_BMCR, bmcr | BMCR_AUTONEG | BMCR_RESTART_AN);
  745. t0 = get_ticks_ms();
  746. while (get_ticks_ms() - t0 < 2000) {
  747. if (r8(REG_PHYstatus) & PHYST_LINK)
  748. break;
  749. }
  750. }
  751. return 0;
  752. }
  753. void r8168_print_info(void)
  754. {
  755. char sp[6];
  756. unsigned int st, i;
  757. if (!present) {
  758. io_putstr("r8168: no RTL8111/8168 found\n");
  759. return;
  760. }
  761. io_putstr(chip_name());
  762. io_putstr(" XID ");
  763. put_hex4(xid);
  764. io_putstr(" at ");
  765. put_hex2(pci_bus);
  766. io_putchar(':');
  767. put_hex2(pci_dev);
  768. io_putchar('.');
  769. io_putchar((char)('0' + pci_func));
  770. io_putchar('\n');
  771. io_putstr(" bar ");
  772. put_hex4(bar_base >> 16);
  773. put_hex4(bar_base & 0xFFFF);
  774. io_putstr(" (");
  775. put_dec(bar_size / 1024);
  776. io_putstr("K) mac=");
  777. for (i = 0; i < 6; i++) {
  778. if (i)
  779. io_putchar(':');
  780. put_hex2(mac[i]);
  781. }
  782. io_putchar('\n');
  783. st = r8(REG_PHYstatus);
  784. speed_string(st, sp);
  785. io_putstr(" link ");
  786. io_putstr((st & PHYST_LINK) ? "up " : "down ");
  787. io_putstr(sp);
  788. io_putstr((st & PHYST_FULLDUP) ? "F" : "H");
  789. io_putstr(" tx ");
  790. put_dec(tx_ok);
  791. io_putchar('/');
  792. put_dec(tx_err);
  793. io_putstr(" rx ");
  794. put_dec(rx_ok);
  795. io_putchar('/');
  796. put_dec(rx_err);
  797. io_putchar('\n');
  798. }
  799. /* Physical link state: kernel_main and `wifi autoconnect` prefer a wired
  800. * link over the saved WiFi network. */
  801. static int r8168_link_up(void)
  802. {
  803. if (!present || !mmio)
  804. return 0;
  805. return (r8(REG_PHYstatus) & PHYST_LINK) ? 1 : 0;
  806. }
  807. void r8168_dump_regs(void)
  808. {
  809. static const struct {
  810. const char *name;
  811. unsigned int reg;
  812. } regs[] = {
  813. { "MAC0", REG_MAC0 }, { "TxDescLo", REG_TxDescLow },
  814. { "TxDescHi", REG_TxDescHigh }, { "ChipCmd", REG_ChipCmd },
  815. { "IntrMask", REG_IntrMask }, { "IntrStat", REG_IntrStatus },
  816. { "TxConfig", REG_TxConfig }, { "RxConfig", REG_RxConfig },
  817. { "PHYAR", REG_PHYAR }, { "PHYstat", REG_PHYstatus },
  818. { "RxMaxSize", REG_RxMaxSize }, { "CPlusCmd", REG_CPlusCmd },
  819. { "RxDescLo", REG_RxDescLow }, { "RxDescHi", REG_RxDescHigh },
  820. };
  821. unsigned int i;
  822. if (!present || !mmio) {
  823. io_putstr("r8168: no NIC\n");
  824. return;
  825. }
  826. for (i = 0; i < sizeof(regs) / sizeof(regs[0]); i++) {
  827. io_putstr(regs[i].name);
  828. io_putstr("=");
  829. io_puthex(r32(regs[i].reg));
  830. if (i & 1)
  831. io_putchar('\n');
  832. else
  833. io_putstr(" ");
  834. }
  835. if (i & 1)
  836. io_putchar('\n');
  837. io_putstr("state tx_ok=");
  838. put_dec(tx_ok);
  839. io_putstr(" tx_err=");
  840. put_dec(tx_err);
  841. io_putstr(" rx_ok=");
  842. put_dec(rx_ok);
  843. io_putstr(" rx_err=");
  844. put_dec(rx_err);
  845. io_putstr(" rx_drop=");
  846. put_dec(rx_drop);
  847. io_putstr(" rx_restart=");
  848. put_dec(rx_restarts);
  849. io_putchar('\n');
  850. io_putstr("isr ");
  851. if (isr_last == 0) {
  852. io_putstr("(none)");
  853. } else {
  854. if (isr_last & ISR_RxOK) io_putstr("RxOK ");
  855. if (isr_last & ISR_RxErr) io_putstr("RxErr ");
  856. if (isr_last & ISR_TxOK) io_putstr("TxOK ");
  857. if (isr_last & ISR_TxErr) io_putstr("TxErr ");
  858. if (isr_last & ISR_ROVW) io_putstr("ROVW ");
  859. if (isr_last & ISR_LinkChg) io_putstr("LinkChg ");
  860. if (isr_last & ISR_FOV) io_putstr("FOV ");
  861. if (isr_last & ISR_TDU) io_putstr("TDU ");
  862. if (isr_last & ISR_SWInt) io_putstr("SWInt ");
  863. }
  864. io_putstr(" evt rovw=");
  865. put_dec(isr_rovw);
  866. io_putstr(" fov=");
  867. put_dec(isr_fov);
  868. io_putstr(" tdu=");
  869. put_dec(isr_tdu);
  870. io_putchar('\n');
  871. io_putstr("tx status ");
  872. io_puthex(last_tx_status);
  873. io_putstr(" ring tx ");
  874. io_puthex((unsigned int)tx_ring);
  875. io_putstr(" rx ");
  876. io_puthex((unsigned int)rx_ring);
  877. io_putchar('\n');
  878. }
  879. const struct netdev r8168_netdev = {
  880. "r8168", "RTL8111/8168",
  881. r8168_init, r8168_get_mac, r8168_rx, r8168_tx, r8168_print_info,
  882. r8168_dump_regs, r8168_set_loopback, r8168_reset,
  883. 0, 0, 0, 0, 0, 0,
  884. r8168_link_up,
  885. };