r8168.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. static void phy_autoneg(void)
  468. {
  469. int id1 = mdio_read(MII_PHYSID1);
  470. int id2 = mdio_read(MII_PHYSID2);
  471. serial_puts(SERIAL_COM1, "r8168: phy id ");
  472. serial_puthex(SERIAL_COM1, (unsigned int)(id1 < 0 ? 0xFFFF : id1));
  473. serial_puts(SERIAL_COM1, ":");
  474. serial_puthex(SERIAL_COM1, (unsigned int)(id2 < 0 ? 0xFFFF : id2));
  475. serial_puts(SERIAL_COM1, "\n");
  476. mdio_write(MII_ANAR, 0x01E1); /* advertise 10/100 half+full */
  477. mdio_write(MII_GBSR, 0x0300); /* advertise 1000 half+full */
  478. mdio_write(MII_BMCR, BMCR_AUTONEG | BMCR_RESTART_AN);
  479. }
  480. /* ---- netdev interface ---- */
  481. /* Rings + configuration registers + PHY + RX/TX enable. Used by both the
  482. * first probe and `netreset`. */
  483. static void hw_start(void)
  484. {
  485. w8(REG_Cfg9346, Cfg9346_Unlock);
  486. setup_rings();
  487. w32(REG_RxConfig, RX128_INT_EN | RX_DMA_BURST);
  488. set_rx_mode();
  489. w32(REG_TxConfig, TXCONFIG_VALUE);
  490. w16(REG_RxMaxSize, R8168_BUF_SIZE);
  491. w8(REG_MaxTxPacket, 0x3f);
  492. w16(REG_IntrMitigate, 0x0000);
  493. w16(REG_IntrMask, 0x0000);
  494. w16(REG_IntrStatus, 0xFFFF);
  495. w8(REG_Cfg9346, 0x00);
  496. dbg("regs ok");
  497. phy_autoneg();
  498. dbg("phy up");
  499. w8(REG_ChipCmd, ChipCmd_RxEnb | ChipCmd_TxEnb);
  500. /* Auto-negotiation runs in the background. Do not wait for link here:
  501. * that used to add up to 5 s to boot (and to every netreset), while
  502. * DHCP already retries and netinfo reports the live link state. */
  503. dbg(r8(REG_PHYstatus) & PHYST_LINK ? "link up" : "link started");
  504. }
  505. int r8168_init(void)
  506. {
  507. char buf[40];
  508. unsigned int i;
  509. if (inited)
  510. return present ? 0 : -1;
  511. inited = 1;
  512. if (probe_pci() != 0) {
  513. dbg("no 10ec:8168 found");
  514. return -1;
  515. }
  516. /* "PCI 04:00.0 f8fff000 4K" */
  517. i = 0;
  518. i = app(buf, i, "PCI ");
  519. i = apph(buf, i, pci_bus, 2);
  520. buf[i++] = ':';
  521. i = apph(buf, i, pci_dev, 2);
  522. buf[i++] = '.';
  523. buf[i++] = (char)('0' + pci_func);
  524. buf[i++] = ' ';
  525. i = apph(buf, i, bar_base, 8);
  526. buf[i++] = ' ';
  527. i = appd(buf, i, bar_size / 1024);
  528. buf[i++] = 'K';
  529. buf[i] = '\0';
  530. dbg(buf);
  531. process_map_mmio_all(bar_base, bar_size);
  532. mmio = (unsigned char *)bar_base;
  533. /* Master abort / unmapped window reads back all-ones. */
  534. if (r32(REG_TxConfig) == 0xFFFFFFFFu) {
  535. dbg("mmio read failed");
  536. mmio = 0;
  537. return -1;
  538. }
  539. xid = (r32(REG_TxConfig) >> 20) & 0xfcf;
  540. if (hw_reset() != 0) {
  541. dbg("reset timeout");
  542. mmio = 0;
  543. return -1;
  544. }
  545. dbg("reset ok");
  546. read_mac();
  547. /* Ring memory: one page each (256-byte aligned); buffers in pages. */
  548. tx_ring = (struct r8168_desc *)mm_alloc_page();
  549. rx_ring = (struct r8168_desc *)mm_alloc_page();
  550. tx_bufs = (unsigned char *)mm_alloc_pages(R8168_NTX * R8168_BUF_SIZE / PAGE_SIZE);
  551. rx_bufs = (unsigned char *)mm_alloc_pages(R8168_NRX * R8168_BUF_SIZE / PAGE_SIZE);
  552. if (!tx_ring || !rx_ring || !tx_bufs || !rx_bufs) {
  553. dbg("no memory for rings");
  554. mmio = 0;
  555. return -1;
  556. }
  557. dbg("ring memory ok");
  558. hw_start();
  559. /* "<chip> XID <xxx> <mac>" */
  560. i = 0;
  561. i = app(buf, i, chip_name());
  562. buf[i++] = ' ';
  563. i = app(buf, i, "XID ");
  564. i = apph(buf, i, xid, 3);
  565. buf[i++] = ' ';
  566. for (unsigned int j = 0; j < 6; j++) {
  567. i = apph(buf, i, mac[j], 2);
  568. if (j != 5)
  569. buf[i++] = ':';
  570. }
  571. buf[i] = '\0';
  572. dbg(buf);
  573. present = 1;
  574. return 0;
  575. }
  576. /* `netreset`: full re-init of an already-probed NIC (used to recover from an
  577. * RX stall and as a bring-up experiment). */
  578. void r8168_reset(void)
  579. {
  580. if (!present || !mmio || !rx_ring) {
  581. dbg("reset: not initialized");
  582. return;
  583. }
  584. if (hw_reset() != 0) {
  585. dbg("reset timeout");
  586. return;
  587. }
  588. read_mac();
  589. hw_start();
  590. dbg("netreset done");
  591. }
  592. void r8168_get_mac(unsigned char out[6])
  593. {
  594. unsigned int i;
  595. for (i = 0; i < 6; i++)
  596. out[i] = mac[i];
  597. }
  598. int r8168_tx(const unsigned char *buf, unsigned int len)
  599. {
  600. static unsigned char pad[60];
  601. struct r8168_desc *d;
  602. unsigned int entry, opts1, t0;
  603. const unsigned char *p = buf;
  604. unsigned int txlen = len;
  605. if (!present || !mmio)
  606. return -1;
  607. if (len < 14 || len > 1514)
  608. return -1;
  609. isr_poll();
  610. if (len < 60) {
  611. unsigned int i;
  612. for (i = 0; i < len; i++)
  613. pad[i] = buf[i];
  614. for (i = len; i < 60; i++)
  615. pad[i] = 0;
  616. p = pad;
  617. txlen = 60;
  618. }
  619. entry = tx_cur;
  620. d = &tx_ring[entry];
  621. /* Wait for a previous use of this slot to complete. */
  622. t0 = get_ticks_ms();
  623. while (d->opts1 & DescOwn) {
  624. if (timed_out(t0, 100)) {
  625. tx_err++;
  626. return -1;
  627. }
  628. }
  629. {
  630. unsigned char *dst = tx_bufs + entry * R8168_BUF_SIZE;
  631. unsigned int i;
  632. for (i = 0; i < txlen; i++)
  633. dst[i] = p[i];
  634. }
  635. opts1 = txlen | DescFirstFrag | DescLastFrag;
  636. if (entry == R8168_NTX - 1)
  637. opts1 |= DescRingEnd;
  638. d->addr_lo = (unsigned int)(tx_bufs + entry * R8168_BUF_SIZE);
  639. d->addr_hi = 0;
  640. d->opts2 = 0;
  641. dma_barrier();
  642. d->opts1 = opts1 | DescOwn;
  643. dma_barrier();
  644. w8(REG_TxPoll, TxPoll_NPQ);
  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. tx_cur = (entry + 1) % R8168_NTX;
  653. last_tx_status = d->opts1;
  654. tx_ok++;
  655. return 0;
  656. }
  657. /* Re-arm one RX descriptor (clears owner for the NIC). */
  658. static void rx_arm(unsigned int entry)
  659. {
  660. rx_ring[entry].opts2 = 0;
  661. dma_barrier();
  662. rx_ring[entry].opts1 = DescOwn | R8168_RX_ARM |
  663. (entry == R8168_NRX - 1 ? DescRingEnd : 0);
  664. dma_barrier();
  665. }
  666. int r8168_rx(unsigned char *buf, unsigned int maxlen)
  667. {
  668. unsigned int guard = 0;
  669. if (!present || !mmio)
  670. return -1;
  671. isr_poll();
  672. while (guard++ < 32) {
  673. unsigned int entry = rx_cur;
  674. struct r8168_desc *d = &rx_ring[entry];
  675. unsigned int status = d->opts1;
  676. unsigned int len;
  677. if (status & DescOwn)
  678. return -1; /* nothing pending */
  679. dma_barrier();
  680. if (status & RxRES) {
  681. rx_err++;
  682. } else if ((status & (DescFirstFrag | DescLastFrag)) !=
  683. (DescFirstFrag | DescLastFrag)) {
  684. rx_drop++; /* fragmented frame: too big */
  685. } else {
  686. len = status & RxPktLenMask;
  687. if (len >= 4)
  688. len -= 4; /* drop the trailing FCS */
  689. if (len < 14 || len > maxlen) {
  690. rx_drop++;
  691. } else {
  692. unsigned char *src = rx_bufs + entry * R8168_BUF_SIZE;
  693. unsigned int i;
  694. for (i = 0; i < len; i++)
  695. buf[i] = src[i];
  696. rx_ok++;
  697. rx_arm(entry);
  698. rx_cur = (rx_cur + 1) % R8168_NRX;
  699. return (int)len;
  700. }
  701. }
  702. rx_arm(entry);
  703. rx_cur = (rx_cur + 1) % R8168_NRX;
  704. }
  705. return -1;
  706. }
  707. /* PHY loopback for a self-test (driver op): transmitted frames are looped
  708. * back into the receiver by the PHY, so TX and RX can be validated with no
  709. * cable, switch or peer. Forces 100M full duplex while enabled. */
  710. static int saved_bmcr = -1;
  711. int r8168_set_loopback(int on)
  712. {
  713. if (!present || !mmio)
  714. return -1;
  715. if (on) {
  716. saved_bmcr = mdio_read(MII_BMCR);
  717. mdio_write(MII_BMCR, 0x2000 | 0x0100 | 0x4000); /* 100M FD + LB */
  718. } else {
  719. int bmcr = saved_bmcr >= 0 ? (saved_bmcr & 0x7fff) : 0;
  720. unsigned int t0;
  721. mdio_write(MII_BMCR, bmcr | BMCR_AUTONEG | BMCR_RESTART_AN);
  722. t0 = get_ticks_ms();
  723. while (get_ticks_ms() - t0 < 2000) {
  724. if (r8(REG_PHYstatus) & PHYST_LINK)
  725. break;
  726. }
  727. }
  728. return 0;
  729. }
  730. void r8168_print_info(void)
  731. {
  732. char sp[6];
  733. unsigned int st, i;
  734. if (!present) {
  735. io_putstr("r8168: no RTL8111/8168 found\n");
  736. return;
  737. }
  738. io_putstr(chip_name());
  739. io_putstr(" XID ");
  740. put_hex4(xid);
  741. io_putstr(" at ");
  742. put_hex2(pci_bus);
  743. io_putchar(':');
  744. put_hex2(pci_dev);
  745. io_putchar('.');
  746. io_putchar((char)('0' + pci_func));
  747. io_putchar('\n');
  748. io_putstr(" bar ");
  749. put_hex4(bar_base >> 16);
  750. put_hex4(bar_base & 0xFFFF);
  751. io_putstr(" (");
  752. put_dec(bar_size / 1024);
  753. io_putstr("K) mac=");
  754. for (i = 0; i < 6; i++) {
  755. if (i)
  756. io_putchar(':');
  757. put_hex2(mac[i]);
  758. }
  759. io_putchar('\n');
  760. st = r8(REG_PHYstatus);
  761. speed_string(st, sp);
  762. io_putstr(" link ");
  763. io_putstr((st & PHYST_LINK) ? "up " : "down ");
  764. io_putstr(sp);
  765. io_putstr((st & PHYST_FULLDUP) ? "F" : "H");
  766. io_putstr(" tx ");
  767. put_dec(tx_ok);
  768. io_putchar('/');
  769. put_dec(tx_err);
  770. io_putstr(" rx ");
  771. put_dec(rx_ok);
  772. io_putchar('/');
  773. put_dec(rx_err);
  774. io_putchar('\n');
  775. }
  776. void r8168_dump_regs(void)
  777. {
  778. static const struct {
  779. const char *name;
  780. unsigned int reg;
  781. } regs[] = {
  782. { "MAC0", REG_MAC0 }, { "TxDescLo", REG_TxDescLow },
  783. { "TxDescHi", REG_TxDescHigh }, { "ChipCmd", REG_ChipCmd },
  784. { "IntrMask", REG_IntrMask }, { "IntrStat", REG_IntrStatus },
  785. { "TxConfig", REG_TxConfig }, { "RxConfig", REG_RxConfig },
  786. { "PHYAR", REG_PHYAR }, { "PHYstat", REG_PHYstatus },
  787. { "RxMaxSize", REG_RxMaxSize }, { "CPlusCmd", REG_CPlusCmd },
  788. { "RxDescLo", REG_RxDescLow }, { "RxDescHi", REG_RxDescHigh },
  789. };
  790. unsigned int i;
  791. if (!present || !mmio) {
  792. io_putstr("r8168: no NIC\n");
  793. return;
  794. }
  795. for (i = 0; i < sizeof(regs) / sizeof(regs[0]); i++) {
  796. io_putstr(regs[i].name);
  797. io_putstr("=");
  798. io_puthex(r32(regs[i].reg));
  799. if (i & 1)
  800. io_putchar('\n');
  801. else
  802. io_putstr(" ");
  803. }
  804. if (i & 1)
  805. io_putchar('\n');
  806. io_putstr("state tx_ok=");
  807. put_dec(tx_ok);
  808. io_putstr(" tx_err=");
  809. put_dec(tx_err);
  810. io_putstr(" rx_ok=");
  811. put_dec(rx_ok);
  812. io_putstr(" rx_err=");
  813. put_dec(rx_err);
  814. io_putstr(" rx_drop=");
  815. put_dec(rx_drop);
  816. io_putstr(" rx_restart=");
  817. put_dec(rx_restarts);
  818. io_putchar('\n');
  819. io_putstr("isr ");
  820. if (isr_last == 0) {
  821. io_putstr("(none)");
  822. } else {
  823. if (isr_last & ISR_RxOK) io_putstr("RxOK ");
  824. if (isr_last & ISR_RxErr) io_putstr("RxErr ");
  825. if (isr_last & ISR_TxOK) io_putstr("TxOK ");
  826. if (isr_last & ISR_TxErr) io_putstr("TxErr ");
  827. if (isr_last & ISR_ROVW) io_putstr("ROVW ");
  828. if (isr_last & ISR_LinkChg) io_putstr("LinkChg ");
  829. if (isr_last & ISR_FOV) io_putstr("FOV ");
  830. if (isr_last & ISR_TDU) io_putstr("TDU ");
  831. if (isr_last & ISR_SWInt) io_putstr("SWInt ");
  832. }
  833. io_putstr(" evt rovw=");
  834. put_dec(isr_rovw);
  835. io_putstr(" fov=");
  836. put_dec(isr_fov);
  837. io_putstr(" tdu=");
  838. put_dec(isr_tdu);
  839. io_putchar('\n');
  840. io_putstr("tx status ");
  841. io_puthex(last_tx_status);
  842. io_putstr(" ring tx ");
  843. io_puthex((unsigned int)tx_ring);
  844. io_putstr(" rx ");
  845. io_puthex((unsigned int)rx_ring);
  846. io_putchar('\n');
  847. }
  848. const struct netdev r8168_netdev = {
  849. "r8168", "RTL8111/8168",
  850. r8168_init, r8168_get_mac, r8168_rx, r8168_tx, r8168_print_info,
  851. r8168_dump_regs, r8168_set_loopback, r8168_reset,
  852. 0, 0, 0, 0, 0,
  853. };