r8168.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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. process_map_mmio_all(bar_base, bar_size);
  549. mmio = (unsigned char *)bar_base;
  550. /* Master abort / unmapped window reads back all-ones. */
  551. if (r32(REG_TxConfig) == 0xFFFFFFFFu) {
  552. dbg("mmio read failed");
  553. mmio = 0;
  554. return -1;
  555. }
  556. xid = (r32(REG_TxConfig) >> 20) & 0xfcf;
  557. /* Sample before the reset: a cable that was already linked keeps
  558. * the boot DHCP on the wired path instead of falling back to WiFi. */
  559. had_link = (r8(REG_PHYstatus) & PHYST_LINK) != 0;
  560. if (hw_reset() != 0) {
  561. dbg("reset timeout");
  562. mmio = 0;
  563. return -1;
  564. }
  565. dbg("reset ok");
  566. read_mac();
  567. /* Ring memory: one page each (256-byte aligned); buffers in pages. */
  568. tx_ring = (struct r8168_desc *)mm_alloc_page();
  569. rx_ring = (struct r8168_desc *)mm_alloc_page();
  570. tx_bufs = (unsigned char *)mm_alloc_pages(R8168_NTX * R8168_BUF_SIZE / PAGE_SIZE);
  571. rx_bufs = (unsigned char *)mm_alloc_pages(R8168_NRX * R8168_BUF_SIZE / PAGE_SIZE);
  572. if (!tx_ring || !rx_ring || !tx_bufs || !rx_bufs) {
  573. dbg("no memory for rings");
  574. mmio = 0;
  575. return -1;
  576. }
  577. dbg("ring memory ok");
  578. hw_start(0);
  579. if (had_link && !(r8(REG_PHYstatus) & PHYST_LINK))
  580. wait_link(3000);
  581. /* "<chip> XID <xxx> <mac>" */
  582. i = 0;
  583. i = app(buf, i, chip_name());
  584. buf[i++] = ' ';
  585. i = app(buf, i, "XID ");
  586. i = apph(buf, i, xid, 3);
  587. buf[i++] = ' ';
  588. for (unsigned int j = 0; j < 6; j++) {
  589. i = apph(buf, i, mac[j], 2);
  590. if (j != 5)
  591. buf[i++] = ':';
  592. }
  593. buf[i] = '\0';
  594. dbg(buf);
  595. present = 1;
  596. return 0;
  597. }
  598. /* `netreset`: full re-init of an already-probed NIC (used to recover from an
  599. * RX stall and as a bring-up experiment). */
  600. void r8168_reset(void)
  601. {
  602. if (!present || !mmio || !rx_ring) {
  603. dbg("reset: not initialized");
  604. return;
  605. }
  606. if (hw_reset() != 0) {
  607. dbg("reset timeout");
  608. return;
  609. }
  610. read_mac();
  611. hw_start(1);
  612. dbg("netreset done");
  613. }
  614. void r8168_get_mac(unsigned char out[6])
  615. {
  616. unsigned int i;
  617. for (i = 0; i < 6; i++)
  618. out[i] = mac[i];
  619. }
  620. int r8168_tx(const unsigned char *buf, unsigned int len)
  621. {
  622. static unsigned char pad[60];
  623. struct r8168_desc *d;
  624. unsigned int entry, opts1, t0;
  625. const unsigned char *p = buf;
  626. unsigned int txlen = len;
  627. if (!present || !mmio)
  628. return -1;
  629. if (len < 14 || len > 1514)
  630. return -1;
  631. isr_poll();
  632. if (len < 60) {
  633. unsigned int i;
  634. for (i = 0; i < len; i++)
  635. pad[i] = buf[i];
  636. for (i = len; i < 60; i++)
  637. pad[i] = 0;
  638. p = pad;
  639. txlen = 60;
  640. }
  641. entry = tx_cur;
  642. d = &tx_ring[entry];
  643. /* Wait for a previous use of this slot to complete. */
  644. t0 = get_ticks_ms();
  645. while (d->opts1 & DescOwn) {
  646. if (timed_out(t0, 100)) {
  647. tx_err++;
  648. return -1;
  649. }
  650. }
  651. {
  652. unsigned char *dst = tx_bufs + entry * R8168_BUF_SIZE;
  653. unsigned int i;
  654. for (i = 0; i < txlen; i++)
  655. dst[i] = p[i];
  656. }
  657. opts1 = txlen | DescFirstFrag | DescLastFrag;
  658. if (entry == R8168_NTX - 1)
  659. opts1 |= DescRingEnd;
  660. d->addr_lo = (unsigned int)(tx_bufs + entry * R8168_BUF_SIZE);
  661. d->addr_hi = 0;
  662. d->opts2 = 0;
  663. dma_barrier();
  664. d->opts1 = opts1 | DescOwn;
  665. dma_barrier();
  666. w8(REG_TxPoll, TxPoll_NPQ);
  667. t0 = get_ticks_ms();
  668. while (d->opts1 & DescOwn) {
  669. if (timed_out(t0, 100)) {
  670. tx_err++;
  671. return -1;
  672. }
  673. }
  674. tx_cur = (entry + 1) % R8168_NTX;
  675. last_tx_status = d->opts1;
  676. tx_ok++;
  677. return 0;
  678. }
  679. /* Re-arm one RX descriptor (clears owner for the NIC). */
  680. static void rx_arm(unsigned int entry)
  681. {
  682. rx_ring[entry].opts2 = 0;
  683. dma_barrier();
  684. rx_ring[entry].opts1 = DescOwn | R8168_RX_ARM |
  685. (entry == R8168_NRX - 1 ? DescRingEnd : 0);
  686. dma_barrier();
  687. }
  688. int r8168_rx(unsigned char *buf, unsigned int maxlen)
  689. {
  690. unsigned int guard = 0;
  691. if (!present || !mmio)
  692. return -1;
  693. isr_poll();
  694. while (guard++ < 32) {
  695. unsigned int entry = rx_cur;
  696. struct r8168_desc *d = &rx_ring[entry];
  697. unsigned int status = d->opts1;
  698. unsigned int len;
  699. if (status & DescOwn)
  700. return -1; /* nothing pending */
  701. dma_barrier();
  702. if (status & RxRES) {
  703. rx_err++;
  704. } else if ((status & (DescFirstFrag | DescLastFrag)) !=
  705. (DescFirstFrag | DescLastFrag)) {
  706. rx_drop++; /* fragmented frame: too big */
  707. } else {
  708. len = status & RxPktLenMask;
  709. if (len >= 4)
  710. len -= 4; /* drop the trailing FCS */
  711. if (len < 14 || len > maxlen) {
  712. rx_drop++;
  713. } else {
  714. unsigned char *src = rx_bufs + entry * R8168_BUF_SIZE;
  715. unsigned int i;
  716. for (i = 0; i < len; i++)
  717. buf[i] = src[i];
  718. rx_ok++;
  719. rx_arm(entry);
  720. rx_cur = (rx_cur + 1) % R8168_NRX;
  721. return (int)len;
  722. }
  723. }
  724. rx_arm(entry);
  725. rx_cur = (rx_cur + 1) % R8168_NRX;
  726. }
  727. return -1;
  728. }
  729. /* PHY loopback for a self-test (driver op): transmitted frames are looped
  730. * back into the receiver by the PHY, so TX and RX can be validated with no
  731. * cable, switch or peer. Forces 100M full duplex while enabled. */
  732. static int saved_bmcr = -1;
  733. int r8168_set_loopback(int on)
  734. {
  735. if (!present || !mmio)
  736. return -1;
  737. if (on) {
  738. saved_bmcr = mdio_read(MII_BMCR);
  739. mdio_write(MII_BMCR, 0x2000 | 0x0100 | 0x4000); /* 100M FD + LB */
  740. } else {
  741. int bmcr = saved_bmcr >= 0 ? (saved_bmcr & 0x7fff) : 0;
  742. unsigned int t0;
  743. mdio_write(MII_BMCR, bmcr | BMCR_AUTONEG | BMCR_RESTART_AN);
  744. t0 = get_ticks_ms();
  745. while (get_ticks_ms() - t0 < 2000) {
  746. if (r8(REG_PHYstatus) & PHYST_LINK)
  747. break;
  748. }
  749. }
  750. return 0;
  751. }
  752. void r8168_print_info(void)
  753. {
  754. char sp[6];
  755. unsigned int st, i;
  756. if (!present) {
  757. io_putstr("r8168: no RTL8111/8168 found\n");
  758. return;
  759. }
  760. io_putstr(chip_name());
  761. io_putstr(" XID ");
  762. put_hex4(xid);
  763. io_putstr(" at ");
  764. put_hex2(pci_bus);
  765. io_putchar(':');
  766. put_hex2(pci_dev);
  767. io_putchar('.');
  768. io_putchar((char)('0' + pci_func));
  769. io_putchar('\n');
  770. io_putstr(" bar ");
  771. put_hex4(bar_base >> 16);
  772. put_hex4(bar_base & 0xFFFF);
  773. io_putstr(" (");
  774. put_dec(bar_size / 1024);
  775. io_putstr("K) mac=");
  776. for (i = 0; i < 6; i++) {
  777. if (i)
  778. io_putchar(':');
  779. put_hex2(mac[i]);
  780. }
  781. io_putchar('\n');
  782. st = r8(REG_PHYstatus);
  783. speed_string(st, sp);
  784. io_putstr(" link ");
  785. io_putstr((st & PHYST_LINK) ? "up " : "down ");
  786. io_putstr(sp);
  787. io_putstr((st & PHYST_FULLDUP) ? "F" : "H");
  788. io_putstr(" tx ");
  789. put_dec(tx_ok);
  790. io_putchar('/');
  791. put_dec(tx_err);
  792. io_putstr(" rx ");
  793. put_dec(rx_ok);
  794. io_putchar('/');
  795. put_dec(rx_err);
  796. io_putchar('\n');
  797. }
  798. /* Physical link state: kernel_main and `wifi autoconnect` prefer a wired
  799. * link over the saved WiFi network. */
  800. static int r8168_link_up(void)
  801. {
  802. if (!present || !mmio)
  803. return 0;
  804. return (r8(REG_PHYstatus) & PHYST_LINK) ? 1 : 0;
  805. }
  806. void r8168_dump_regs(void)
  807. {
  808. static const struct {
  809. const char *name;
  810. unsigned int reg;
  811. } regs[] = {
  812. { "MAC0", REG_MAC0 }, { "TxDescLo", REG_TxDescLow },
  813. { "TxDescHi", REG_TxDescHigh }, { "ChipCmd", REG_ChipCmd },
  814. { "IntrMask", REG_IntrMask }, { "IntrStat", REG_IntrStatus },
  815. { "TxConfig", REG_TxConfig }, { "RxConfig", REG_RxConfig },
  816. { "PHYAR", REG_PHYAR }, { "PHYstat", REG_PHYstatus },
  817. { "RxMaxSize", REG_RxMaxSize }, { "CPlusCmd", REG_CPlusCmd },
  818. { "RxDescLo", REG_RxDescLow }, { "RxDescHi", REG_RxDescHigh },
  819. };
  820. unsigned int i;
  821. if (!present || !mmio) {
  822. io_putstr("r8168: no NIC\n");
  823. return;
  824. }
  825. for (i = 0; i < sizeof(regs) / sizeof(regs[0]); i++) {
  826. io_putstr(regs[i].name);
  827. io_putstr("=");
  828. io_puthex(r32(regs[i].reg));
  829. if (i & 1)
  830. io_putchar('\n');
  831. else
  832. io_putstr(" ");
  833. }
  834. if (i & 1)
  835. io_putchar('\n');
  836. io_putstr("state tx_ok=");
  837. put_dec(tx_ok);
  838. io_putstr(" tx_err=");
  839. put_dec(tx_err);
  840. io_putstr(" rx_ok=");
  841. put_dec(rx_ok);
  842. io_putstr(" rx_err=");
  843. put_dec(rx_err);
  844. io_putstr(" rx_drop=");
  845. put_dec(rx_drop);
  846. io_putstr(" rx_restart=");
  847. put_dec(rx_restarts);
  848. io_putchar('\n');
  849. io_putstr("isr ");
  850. if (isr_last == 0) {
  851. io_putstr("(none)");
  852. } else {
  853. if (isr_last & ISR_RxOK) io_putstr("RxOK ");
  854. if (isr_last & ISR_RxErr) io_putstr("RxErr ");
  855. if (isr_last & ISR_TxOK) io_putstr("TxOK ");
  856. if (isr_last & ISR_TxErr) io_putstr("TxErr ");
  857. if (isr_last & ISR_ROVW) io_putstr("ROVW ");
  858. if (isr_last & ISR_LinkChg) io_putstr("LinkChg ");
  859. if (isr_last & ISR_FOV) io_putstr("FOV ");
  860. if (isr_last & ISR_TDU) io_putstr("TDU ");
  861. if (isr_last & ISR_SWInt) io_putstr("SWInt ");
  862. }
  863. io_putstr(" evt rovw=");
  864. put_dec(isr_rovw);
  865. io_putstr(" fov=");
  866. put_dec(isr_fov);
  867. io_putstr(" tdu=");
  868. put_dec(isr_tdu);
  869. io_putchar('\n');
  870. io_putstr("tx status ");
  871. io_puthex(last_tx_status);
  872. io_putstr(" ring tx ");
  873. io_puthex((unsigned int)tx_ring);
  874. io_putstr(" rx ");
  875. io_puthex((unsigned int)rx_ring);
  876. io_putchar('\n');
  877. }
  878. const struct netdev r8168_netdev = {
  879. "r8168", "RTL8111/8168",
  880. r8168_init, r8168_get_mac, r8168_rx, r8168_tx, r8168_print_info,
  881. r8168_dump_regs, r8168_set_loopback, r8168_reset,
  882. 0, 0, 0, 0, 0, 0,
  883. r8168_link_up,
  884. };