r8168.c 26 KB

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