ether8139.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * Realtek 8139 (but not the 8129).
  11. * Error recovery for the various over/under -flow conditions
  12. * may need work.
  13. */
  14. #include "u.h"
  15. #include "../port/lib.h"
  16. #include "mem.h"
  17. #include "dat.h"
  18. #include "fns.h"
  19. #include "io.h"
  20. #include "../port/error.h"
  21. #include "../port/netif.h"
  22. #include "etherif.h"
  23. enum { /* registers */
  24. Idr0 = 0x0000, /* MAC address */
  25. Mar0 = 0x0008, /* Multicast address */
  26. Tsd0 = 0x0010, /* Transmit Status Descriptor0 */
  27. Tsad0 = 0x0020, /* Transmit Start Address Descriptor0 */
  28. Rbstart = 0x0030, /* Receive Buffer Start Address */
  29. Erbcr = 0x0034, /* Early Receive Byte Count */
  30. Ersr = 0x0036, /* Early Receive Status */
  31. Cr = 0x0037, /* Command Register */
  32. Capr = 0x0038, /* Current Address of Packet Read */
  33. Cbr = 0x003A, /* Current Buffer Address */
  34. Imr = 0x003C, /* Interrupt Mask */
  35. Isr = 0x003E, /* Interrupt Status */
  36. Tcr = 0x0040, /* Transmit Configuration */
  37. Rcr = 0x0044, /* Receive Configuration */
  38. Tctr = 0x0048, /* Timer Count */
  39. Mpc = 0x004C, /* Missed Packet Counter */
  40. Cr9346 = 0x0050, /* 9346 Command Register */
  41. Config0 = 0x0051, /* Configuration Register 0 */
  42. Config1 = 0x0052, /* Configuration Register 1 */
  43. TimerInt = 0x0054, /* Timer Interrupt */
  44. Msr = 0x0058, /* Media Status */
  45. Config3 = 0x0059, /* Configuration Register 3 */
  46. Config4 = 0x005A, /* Configuration Register 4 */
  47. Mulint = 0x005C, /* Multiple Interrupt Select */
  48. RerID = 0x005E, /* PCI Revision ID */
  49. Tsad = 0x0060, /* Transmit Status of all Descriptors */
  50. Bmcr = 0x0062, /* Basic Mode Control */
  51. Bmsr = 0x0064, /* Basic Mode Status */
  52. Anar = 0x0066, /* Auto-Negotiation Advertisment */
  53. Anlpar = 0x0068, /* Auto-Negotiation Link Partner */
  54. Aner = 0x006A, /* Auto-Negotiation Expansion */
  55. Dis = 0x006C, /* Disconnect Counter */
  56. Fcsc = 0x006E, /* False Carrier Sense Counter */
  57. Nwaytr = 0x0070, /* N-way Test */
  58. Rec = 0x0072, /* RX_ER Counter */
  59. Cscr = 0x0074, /* CS Configuration */
  60. Phy1parm = 0x0078, /* PHY Parameter 1 */
  61. Twparm = 0x007C, /* Twister Parameter */
  62. Phy2parm = 0x0080, /* PHY Parameter 2 */
  63. };
  64. enum { /* Cr */
  65. Bufe = 0x01, /* Rx Buffer Empty */
  66. Te = 0x04, /* Transmitter Enable */
  67. Re = 0x08, /* Receiver Enable */
  68. Rst = 0x10, /* Software Reset */
  69. };
  70. enum { /* Imr/Isr */
  71. Rok = 0x0001, /* Receive OK */
  72. Rer = 0x0002, /* Receive Error */
  73. Tok = 0x0004, /* Transmit OK */
  74. Ter = 0x0008, /* Transmit Error */
  75. Rxovw = 0x0010, /* Receive Buffer Overflow */
  76. PunLc = 0x0020, /* Packet Underrun or Link Change */
  77. Fovw = 0x0040, /* Receive FIFO Overflow */
  78. Clc = 0x2000, /* Cable Length Change */
  79. Timerbit = 0x4000, /* Timer */
  80. Serr = 0x8000, /* System Error */
  81. };
  82. enum { /* Tcr */
  83. Clrabt = 0x00000001, /* Clear Abort */
  84. TxrrSHIFT = 4, /* Transmit Retry Count */
  85. TxrrMASK = 0x000000F0,
  86. MtxdmaSHIFT = 8, /* Max. DMA Burst Size */
  87. MtxdmaMASK = 0x00000700,
  88. Mtxdma2048 = 0x00000700,
  89. Acrc = 0x00010000, /* Append CRC (not) */
  90. LbkSHIFT = 17, /* Loopback Test */
  91. LbkMASK = 0x00060000,
  92. Rtl8139ArevG = 0x00800000, /* RTL8139A Rev. G ID */
  93. IfgSHIFT = 24, /* Interframe Gap */
  94. IfgMASK = 0x03000000,
  95. HwveridSHIFT = 26, /* Hardware Version ID */
  96. HwveridMASK = 0x7C000000,
  97. };
  98. enum { /* Rcr */
  99. Aap = 0x00000001, /* Accept All Packets */
  100. Apm = 0x00000002, /* Accept Physical Match */
  101. Am = 0x00000004, /* Accept Multicast */
  102. Ab = 0x00000008, /* Accept Broadcast */
  103. Ar = 0x00000010, /* Accept Runt */
  104. Aer = 0x00000020, /* Accept Error */
  105. Sel9356 = 0x00000040, /* 9356 EEPROM used */
  106. Wrap = 0x00000080, /* Rx Buffer Wrap Control */
  107. MrxdmaSHIFT = 8, /* Max. DMA Burst Size */
  108. MrxdmaMASK = 0x00000700,
  109. Mrxdmaunlimited = 0x00000700,
  110. RblenSHIFT = 11, /* Receive Buffer Length */
  111. RblenMASK = 0x00001800,
  112. Rblen8K = 0x00000000, /* 8KB+16 */
  113. Rblen16K = 0x00000800, /* 16KB+16 */
  114. Rblen32K = 0x00001000, /* 32KB+16 */
  115. Rblen64K = 0x00001800, /* 64KB+16 */
  116. RxfthSHIFT = 13, /* Receive Buffer Length */
  117. RxfthMASK = 0x0000E000,
  118. Rxfth256 = 0x00008000,
  119. Rxfthnone = 0x0000E000,
  120. Rer8 = 0x00010000, /* Accept Error Packets > 8 bytes */
  121. MulERINT = 0x00020000, /* Multiple Early Interrupt Select */
  122. ErxthSHIFT = 24, /* Early Rx Threshold */
  123. ErxthMASK = 0x0F000000,
  124. Erxthnone = 0x00000000,
  125. };
  126. enum { /* Received Packet Status */
  127. Rcok = 0x0001, /* Receive Completed OK */
  128. Fae = 0x0002, /* Frame Alignment Error */
  129. Crc = 0x0004, /* CRC Error */
  130. Long = 0x0008, /* Long Packet */
  131. Runt = 0x0010, /* Runt Packet Received */
  132. Ise = 0x0020, /* Invalid Symbol Error */
  133. Bar = 0x2000, /* Broadcast Address Received */
  134. Pam = 0x4000, /* Physical Address Matched */
  135. Mar = 0x8000, /* Multicast Address Received */
  136. };
  137. enum { /* Media Status Register */
  138. Rxpf = 0x01, /* Pause Flag */
  139. Txpf = 0x02, /* Pause Flag */
  140. Linkb = 0x04, /* Inverse of Link Status */
  141. Speed10 = 0x08, /* 10Mbps */
  142. Auxstatus = 0x10, /* Aux. Power Present Status */
  143. Rxfce = 0x40, /* Receive Flow Control Enable */
  144. Txfce = 0x80, /* Transmit Flow Control Enable */
  145. };
  146. typedef struct Td Td;
  147. struct Td { /* Soft Transmit Descriptor */
  148. int tsd;
  149. int tsad;
  150. unsigned char *data;
  151. Block *bp;
  152. };
  153. enum { /* Tsd0 */
  154. SizeSHIFT = 0, /* Descriptor Size */
  155. SizeMASK = 0x00001FFF,
  156. Own = 0x00002000,
  157. Tun = 0x00004000, /* Transmit FIFO Underrun */
  158. Tcok = 0x00008000, /* Transmit COmpleted OK */
  159. EtxthSHIFT = 16, /* Early Tx Threshold */
  160. EtxthMASK = 0x001F0000,
  161. NccSHIFT = 24, /* Number of Collisions Count */
  162. NccMASK = 0x0F000000,
  163. Cdh = 0x10000000, /* CD Heartbeat */
  164. Owc = 0x20000000, /* Out of Window Collision */
  165. Tabt = 0x40000000, /* Transmit Abort */
  166. Crs = 0x80000000, /* Carrier Sense Lost */
  167. };
  168. enum {
  169. Rblen = Rblen64K, /* Receive Buffer Length */
  170. Ntd = 4, /* Number of Transmit Descriptors */
  171. Tdbsz = ROUNDUP(sizeof(Etherpkt), 4),
  172. };
  173. typedef struct Ctlr Ctlr;
  174. typedef struct Ctlr {
  175. int port;
  176. Pcidev *pcidev;
  177. Ctlr *next;
  178. int active;
  179. int id;
  180. QLock alock; /* attach */
  181. Lock ilock; /* init */
  182. void *alloc; /* base of per-Ctlr allocated data */
  183. int pcie; /* flag: pci-express device? */
  184. u64 mchash; /* multicast hash */
  185. int rcr; /* receive configuration register */
  186. unsigned char *rbstart; /* receive buffer */
  187. int rblen; /* receive buffer length */
  188. int ierrs; /* receive errors */
  189. Lock tlock; /* transmit */
  190. Td td[Ntd];
  191. int ntd; /* descriptors active */
  192. int tdh; /* host index into td */
  193. int tdi; /* interface index into td */
  194. int etxth; /* early transmit threshold */
  195. int taligned; /* packet required no alignment */
  196. int tunaligned; /* packet required alignment */
  197. int dis; /* disconnect counter */
  198. int fcsc; /* false carrier sense counter */
  199. int rec; /* RX_ER counter */
  200. u32 mcast;
  201. } Ctlr;
  202. static Ctlr *ctlrhead;
  203. static Ctlr *ctlrtail;
  204. #define csr8r(c, r) (inb((c)->port + (r)))
  205. #define csr16r(c, r) (ins((c)->port + (r)))
  206. #define csr32r(c, r) (inl((c)->port + (r)))
  207. #define csr8w(c, r, b) (outb((c)->port + (r), (int)(b)))
  208. #define csr16w(c, r, w) (outs((c)->port + (r), (u16)(w)))
  209. #define csr32w(c, r, l) (outl((c)->port + (r), (u32)(l)))
  210. static void
  211. rtl8139promiscuous(void *arg, int on)
  212. {
  213. Ether *edev;
  214. Ctlr *ctlr;
  215. edev = arg;
  216. ctlr = edev->ctlr;
  217. ilock(&ctlr->ilock);
  218. if(on)
  219. ctlr->rcr |= Aap;
  220. else
  221. ctlr->rcr &= ~Aap;
  222. csr32w(ctlr, Rcr, ctlr->rcr);
  223. iunlock(&ctlr->ilock);
  224. }
  225. enum {
  226. /* everyone else uses 0x04c11db7, but they both produce the same crc */
  227. Etherpolybe = 0x04c11db6,
  228. Bytemask = (1 << 8) - 1,
  229. };
  230. static u32
  231. ethercrcbe(unsigned char *addr, i32 len)
  232. {
  233. int i, j;
  234. u64 c, crc, carry;
  235. crc = ~0UL;
  236. for(i = 0; i < len; i++){
  237. c = addr[i];
  238. for(j = 0; j < 8; j++){
  239. carry = ((crc & (1UL << 31)) ? 1 : 0) ^ (c & 1);
  240. crc <<= 1;
  241. c >>= 1;
  242. if(carry)
  243. crc = (crc ^ Etherpolybe) | carry;
  244. }
  245. }
  246. return crc;
  247. }
  248. static u32
  249. swabl(u32 l)
  250. {
  251. return (l >> 24) | ((l >> 8) & (Bytemask << 8)) |
  252. ((l << 8) & (Bytemask << 16)) | (l << 24);
  253. }
  254. static void
  255. rtl8139multicast(void *ether, unsigned char *eaddr, int add)
  256. {
  257. Ether *edev;
  258. Ctlr *ctlr;
  259. if(!add)
  260. return; /* ok to keep receiving on old mcast addrs */
  261. edev = ether;
  262. ctlr = edev->ctlr;
  263. ilock(&ctlr->ilock);
  264. ctlr->mchash |= 1ULL << (ethercrcbe(eaddr, Eaddrlen) >> 26);
  265. ctlr->rcr |= Am;
  266. csr32w(ctlr, Rcr, ctlr->rcr);
  267. /* pci-e variants reverse the order of the hash byte registers */
  268. if(0 && ctlr->pcie){
  269. csr32w(ctlr, Mar0, swabl(ctlr->mchash >> 32));
  270. csr32w(ctlr, Mar0 + 4, swabl(ctlr->mchash));
  271. } else {
  272. csr32w(ctlr, Mar0, ctlr->mchash);
  273. csr32w(ctlr, Mar0 + 4, ctlr->mchash >> 32);
  274. }
  275. iunlock(&ctlr->ilock);
  276. }
  277. static i32
  278. rtl8139ifstat(Ether *edev, void *a, i32 n, u32 offset)
  279. {
  280. int l;
  281. char *p;
  282. Ctlr *ctlr;
  283. ctlr = edev->ctlr;
  284. p = malloc(READSTR);
  285. if(p == nil)
  286. error(Enomem);
  287. l = snprint(p, READSTR, "rcr %#8.8x\n", ctlr->rcr);
  288. l += snprint(p + l, READSTR - l, "multicast %u\n", ctlr->mcast);
  289. l += snprint(p + l, READSTR - l, "ierrs %d\n", ctlr->ierrs);
  290. l += snprint(p + l, READSTR - l, "etxth %d\n", ctlr->etxth);
  291. l += snprint(p + l, READSTR - l, "taligned %d\n", ctlr->taligned);
  292. l += snprint(p + l, READSTR - l, "tunaligned %d\n", ctlr->tunaligned);
  293. ctlr->dis += csr16r(ctlr, Dis);
  294. l += snprint(p + l, READSTR - l, "dis %d\n", ctlr->dis);
  295. ctlr->fcsc += csr16r(ctlr, Fcsc);
  296. l += snprint(p + l, READSTR - l, "fcscnt %d\n", ctlr->fcsc);
  297. ctlr->rec += csr16r(ctlr, Rec);
  298. l += snprint(p + l, READSTR - l, "rec %d\n", ctlr->rec);
  299. l += snprint(p + l, READSTR - l, "Tcr %#8.8lx\n", csr32r(ctlr, Tcr));
  300. l += snprint(p + l, READSTR - l, "Config0 %#2.2x\n", csr8r(ctlr, Config0));
  301. l += snprint(p + l, READSTR - l, "Config1 %#2.2x\n", csr8r(ctlr, Config1));
  302. l += snprint(p + l, READSTR - l, "Msr %#2.2x\n", csr8r(ctlr, Msr));
  303. l += snprint(p + l, READSTR - l, "Config3 %#2.2x\n", csr8r(ctlr, Config3));
  304. l += snprint(p + l, READSTR - l, "Config4 %#2.2x\n", csr8r(ctlr, Config4));
  305. l += snprint(p + l, READSTR - l, "Bmcr %#4.4x\n", csr16r(ctlr, Bmcr));
  306. l += snprint(p + l, READSTR - l, "Bmsr %#4.4x\n", csr16r(ctlr, Bmsr));
  307. l += snprint(p + l, READSTR - l, "Anar %#4.4x\n", csr16r(ctlr, Anar));
  308. l += snprint(p + l, READSTR - l, "Anlpar %#4.4x\n", csr16r(ctlr, Anlpar));
  309. l += snprint(p + l, READSTR - l, "Aner %#4.4x\n", csr16r(ctlr, Aner));
  310. l += snprint(p + l, READSTR - l, "Nwaytr %#4.4x\n", csr16r(ctlr, Nwaytr));
  311. snprint(p + l, READSTR - l, "Cscr %#4.4x\n", csr16r(ctlr, Cscr));
  312. n = readstr(offset, a, n, p);
  313. free(p);
  314. return n;
  315. }
  316. static int
  317. rtl8139reset(Ctlr *ctlr)
  318. {
  319. int timeo;
  320. /* stop interrupts */
  321. csr16w(ctlr, Imr, 0);
  322. csr16w(ctlr, Isr, ~0);
  323. csr32w(ctlr, TimerInt, 0);
  324. /*
  325. * Soft reset the controller.
  326. */
  327. csr8w(ctlr, Cr, Rst);
  328. for(timeo = 0; timeo < 1000; timeo++){
  329. if(!(csr8r(ctlr, Cr) & Rst))
  330. return 0;
  331. delay(1);
  332. }
  333. return -1;
  334. }
  335. static void
  336. rtl8139halt(Ctlr *ctlr)
  337. {
  338. int i;
  339. csr8w(ctlr, Cr, 0);
  340. csr16w(ctlr, Imr, 0);
  341. csr16w(ctlr, Isr, ~0);
  342. csr32w(ctlr, TimerInt, 0);
  343. for(i = 0; i < Ntd; i++){
  344. if(ctlr->td[i].bp == nil)
  345. continue;
  346. freeb(ctlr->td[i].bp);
  347. ctlr->td[i].bp = nil;
  348. }
  349. }
  350. static void
  351. rtl8139shutdown(Ether *edev)
  352. {
  353. Ctlr *ctlr;
  354. ctlr = edev->ctlr;
  355. ilock(&ctlr->ilock);
  356. rtl8139halt(ctlr);
  357. rtl8139reset(ctlr);
  358. iunlock(&ctlr->ilock);
  359. }
  360. static void
  361. rtl8139init(Ether *edev)
  362. {
  363. int i;
  364. u32 r;
  365. Ctlr *ctlr;
  366. unsigned char *alloc;
  367. ctlr = edev->ctlr;
  368. ilock(&ctlr->ilock);
  369. rtl8139halt(ctlr);
  370. /*
  371. * MAC Address.
  372. */
  373. r = (edev->ea[3] << 24) | (edev->ea[2] << 16) | (edev->ea[1] << 8) | edev->ea[0];
  374. csr32w(ctlr, Idr0, r);
  375. r = (edev->ea[5] << 8) | edev->ea[4];
  376. csr32w(ctlr, Idr0 + 4, r);
  377. /*
  378. * Receiver
  379. */
  380. alloc = (unsigned char *)ROUNDUP((u64)ctlr->alloc, 32);
  381. ctlr->rbstart = alloc;
  382. alloc += ctlr->rblen + 16;
  383. memset(ctlr->rbstart, 0, ctlr->rblen + 16);
  384. csr32w(ctlr, Rbstart, PADDR(ctlr->rbstart));
  385. ctlr->rcr = Rxfth256 | Rblen | Mrxdmaunlimited | Ab | Am | Apm;
  386. /*
  387. * Transmitter.
  388. */
  389. for(i = 0; i < Ntd; i++){
  390. ctlr->td[i].tsd = Tsd0 + i * 4;
  391. ctlr->td[i].tsad = Tsad0 + i * 4;
  392. ctlr->td[i].data = alloc;
  393. alloc += Tdbsz;
  394. ctlr->td[i].bp = nil;
  395. }
  396. ctlr->ntd = ctlr->tdh = ctlr->tdi = 0;
  397. ctlr->etxth = 128 / 32;
  398. /*
  399. * Enable receiver/transmitter.
  400. * Need to enable before writing the Rcr or it won't take.
  401. */
  402. csr8w(ctlr, Cr, Te | Re);
  403. csr32w(ctlr, Tcr, Mtxdma2048);
  404. csr32w(ctlr, Rcr, ctlr->rcr);
  405. csr32w(ctlr, Mar0, 0);
  406. csr32w(ctlr, Mar0 + 4, 0);
  407. ctlr->mchash = 0;
  408. /*
  409. * Interrupts.
  410. */
  411. csr32w(ctlr, TimerInt, 0);
  412. csr16w(ctlr, Imr, Serr | Timerbit | Fovw | PunLc | Rxovw | Ter | Tok | Rer | Rok);
  413. csr32w(ctlr, Mpc, 0);
  414. iunlock(&ctlr->ilock);
  415. }
  416. static void
  417. rtl8139attach(Ether *edev)
  418. {
  419. Ctlr *ctlr;
  420. if(edev == nil){
  421. print("rtl8139attach: nil edev\n");
  422. return;
  423. }
  424. ctlr = edev->ctlr;
  425. if(ctlr == nil){
  426. print("rtl8139attach: nil ctlr for Ether %#p\n", edev);
  427. return;
  428. }
  429. qlock(&ctlr->alock);
  430. if(ctlr->alloc == nil){
  431. ctlr->rblen = 1 << ((Rblen >> RblenSHIFT) + 13);
  432. ctlr->alloc = malloc(ctlr->rblen + 16 + Ntd * Tdbsz + 32);
  433. if(ctlr->alloc == nil){
  434. qunlock(&ctlr->alock);
  435. error(Enomem);
  436. }
  437. rtl8139init(edev);
  438. }
  439. qunlock(&ctlr->alock);
  440. }
  441. static void
  442. rtl8139txstart(Ether *edev)
  443. {
  444. Td *td;
  445. int size;
  446. Block *bp;
  447. Ctlr *ctlr;
  448. ctlr = edev->ctlr;
  449. while(ctlr->ntd < Ntd){
  450. bp = qget(edev->oq);
  451. if(bp == nil)
  452. break;
  453. size = BLEN(bp);
  454. td = &ctlr->td[ctlr->tdh];
  455. if(((i64)bp->rp) & 0x03){
  456. memmove(td->data, bp->rp, size);
  457. freeb(bp);
  458. csr32w(ctlr, td->tsad, PADDR(td->data));
  459. ctlr->tunaligned++;
  460. } else {
  461. td->bp = bp;
  462. csr32w(ctlr, td->tsad, PADDR(bp->rp));
  463. ctlr->taligned++;
  464. }
  465. csr32w(ctlr, td->tsd, (ctlr->etxth << EtxthSHIFT) | size);
  466. ctlr->ntd++;
  467. ctlr->tdh = NEXT(ctlr->tdh, Ntd);
  468. }
  469. }
  470. static void
  471. rtl8139transmit(Ether *edev)
  472. {
  473. Ctlr *ctlr;
  474. ctlr = edev->ctlr;
  475. ilock(&ctlr->tlock);
  476. rtl8139txstart(edev);
  477. iunlock(&ctlr->tlock);
  478. }
  479. static void
  480. rtl8139receive(Ether *edev)
  481. {
  482. Block *bp;
  483. Ctlr *ctlr;
  484. u16 capr;
  485. unsigned char cr, *p;
  486. int l, length, status;
  487. ctlr = edev->ctlr;
  488. /*
  489. * Capr is where the host is reading from,
  490. * Cbr is where the NIC is currently writing.
  491. */
  492. if(ctlr->rblen == 0)
  493. return; /* not attached yet (shouldn't happen) */
  494. capr = (csr16r(ctlr, Capr) + 16) % ctlr->rblen;
  495. while(!(csr8r(ctlr, Cr) & Bufe)){
  496. p = ctlr->rbstart + capr;
  497. /*
  498. * Apparently the packet length may be 0xFFF0 if
  499. * the NIC is still copying the packet into memory.
  500. */
  501. length = (*(p + 3) << 8) | *(p + 2);
  502. if(length == 0xFFF0)
  503. break;
  504. status = (*(p + 1) << 8) | *p;
  505. if(!(status & Rcok)){
  506. if(status & (Ise | Fae))
  507. edev->Netif.frames++;
  508. if(status & Crc)
  509. edev->Netif.crcs++;
  510. if(status & (Runt | Long))
  511. edev->Netif.buffs++;
  512. /*
  513. * Reset the receiver.
  514. * Also may have to restore the multicast list
  515. * here too if it ever gets used.
  516. */
  517. cr = csr8r(ctlr, Cr);
  518. csr8w(ctlr, Cr, cr & ~Re);
  519. csr32w(ctlr, Rbstart, PADDR(ctlr->rbstart));
  520. csr8w(ctlr, Cr, cr);
  521. csr32w(ctlr, Rcr, ctlr->rcr);
  522. continue;
  523. }
  524. /*
  525. * Receive Completed OK.
  526. * Very simplistic; there are ways this could be done
  527. * without copying, but the juice probably isn't worth
  528. * the squeeze.
  529. * The packet length includes a 4 byte CRC on the end.
  530. */
  531. capr = (capr + 4) % ctlr->rblen;
  532. p = ctlr->rbstart + capr;
  533. capr = (capr + length) % ctlr->rblen;
  534. if(status & Mar)
  535. ctlr->mcast++;
  536. if((bp = iallocb(length)) != nil){
  537. if(p + length >= ctlr->rbstart + ctlr->rblen){
  538. l = ctlr->rbstart + ctlr->rblen - p;
  539. memmove(bp->wp, p, l);
  540. bp->wp += l;
  541. length -= l;
  542. p = ctlr->rbstart;
  543. }
  544. if(length > 0){
  545. memmove(bp->wp, p, length);
  546. bp->wp += length;
  547. }
  548. bp->wp -= 4;
  549. etheriq(edev, bp, 1);
  550. }
  551. capr = ROUNDUP(capr, 4);
  552. csr16w(ctlr, Capr, capr - 16);
  553. }
  554. }
  555. static void
  556. rtl8139interrupt(Ureg *ureg, void *arg)
  557. {
  558. Td *td;
  559. Ctlr *ctlr;
  560. Ether *edev;
  561. int isr, msr, tsd;
  562. edev = arg;
  563. ctlr = edev->ctlr;
  564. if(ctlr == nil) { /* not attached yet? (shouldn't happen) */
  565. print("rtl8139interrupt: interrupt for unattached Ether %#p\n",
  566. edev);
  567. return;
  568. }
  569. while((isr = csr16r(ctlr, Isr)) != 0){
  570. csr16w(ctlr, Isr, isr);
  571. if(ctlr->alloc == nil){
  572. print("rtl8139interrupt: interrupt for unattached Ctlr "
  573. "%#p port %#p\n",
  574. ctlr, (void *)(i64)ctlr->port);
  575. return; /* not attached yet (shouldn't happen) */
  576. }
  577. if(isr & (Fovw | PunLc | Rxovw | Rer | Rok)){
  578. rtl8139receive(edev);
  579. if(!(isr & Rok))
  580. ctlr->ierrs++;
  581. isr &= ~(Fovw | Rxovw | Rer | Rok);
  582. }
  583. if(isr & (Ter | Tok)){
  584. ilock(&ctlr->tlock);
  585. while(ctlr->ntd){
  586. td = &ctlr->td[ctlr->tdi];
  587. tsd = csr32r(ctlr, td->tsd);
  588. if(!(tsd & (Tabt | Tun | Tcok)))
  589. break;
  590. if(!(tsd & Tcok)){
  591. if(tsd & Tun){
  592. if(ctlr->etxth < ETHERMAXTU / 32)
  593. ctlr->etxth++;
  594. }
  595. edev->Netif.oerrs++;
  596. }
  597. if(td->bp != nil){
  598. freeb(td->bp);
  599. td->bp = nil;
  600. }
  601. ctlr->ntd--;
  602. ctlr->tdi = NEXT(ctlr->tdi, Ntd);
  603. }
  604. rtl8139txstart(edev);
  605. iunlock(&ctlr->tlock);
  606. isr &= ~(Ter | Tok);
  607. }
  608. if(isr & PunLc){
  609. /*
  610. * Maybe the link changed - do we care very much?
  611. */
  612. msr = csr8r(ctlr, Msr);
  613. if(!(msr & Linkb)){
  614. if(!(msr & Speed10) && edev->Netif.mbps != 100){
  615. edev->Netif.mbps = 100;
  616. qsetlimit(edev->oq, 256 * 1024);
  617. } else if((msr & Speed10) && edev->Netif.mbps != 10){
  618. edev->Netif.mbps = 10;
  619. qsetlimit(edev->oq, 65 * 1024);
  620. }
  621. }
  622. isr &= ~(Clc | PunLc);
  623. }
  624. /*
  625. * Only Serr|Timerbit should be left by now.
  626. * Should anything be done to tidy up? TimerInt isn't
  627. * used so that can be cleared. A PCI bus error is indicated
  628. * by Serr, that's pretty serious; is there anyhing to do
  629. * other than try to reinitialise the chip?
  630. */
  631. if((isr & (Serr | Timerbit)) != 0){
  632. iprint("rtl8139interrupt: imr %#4.4x isr %#4.4x\n",
  633. csr16r(ctlr, Imr), isr);
  634. if(isr & Timerbit)
  635. csr32w(ctlr, TimerInt, 0);
  636. if(isr & Serr)
  637. rtl8139init(edev);
  638. }
  639. }
  640. }
  641. static Ctlr *
  642. rtl8139match(Ether *edev, int id)
  643. {
  644. Pcidev *p;
  645. Ctlr *ctlr;
  646. int i, port;
  647. /*
  648. * Any adapter matches if no edev->port is supplied,
  649. * otherwise the ports must match.
  650. */
  651. for(ctlr = ctlrhead; ctlr != nil; ctlr = ctlr->next){
  652. if(ctlr->active)
  653. continue;
  654. p = ctlr->pcidev;
  655. if(((p->did << 16) | p->vid) != id)
  656. continue;
  657. port = p->mem[0].bar & ~0x01;
  658. if(edev->ISAConf.port != 0 && edev->ISAConf.port != port)
  659. continue;
  660. if(ioalloc(port, p->mem[0].size, 0, "rtl8139") < 0){
  661. print("rtl8139: port %#x in use\n", port);
  662. continue;
  663. }
  664. if(pcigetpms(p) > 0){
  665. pcisetpms(p, 0);
  666. for(i = 0; i < 6; i++)
  667. pcicfgw32(p, PciBAR0 + i * 4, p->mem[i].bar);
  668. pcicfgw8(p, PciINTL, p->intl);
  669. pcicfgw8(p, PciLTR, p->ltr);
  670. pcicfgw8(p, PciCLS, p->cls);
  671. pcicfgw16(p, PciPCR, p->pcr);
  672. }
  673. ctlr->port = port;
  674. if(rtl8139reset(ctlr)){
  675. iofree(port);
  676. continue;
  677. }
  678. pcisetbme(p);
  679. ctlr->active = 1;
  680. return ctlr;
  681. }
  682. return nil;
  683. }
  684. static struct {
  685. char *name;
  686. int id;
  687. } rtl8139pci[] = {
  688. {
  689. "rtl8139",
  690. (0x8139 << 16) | 0x10EC,
  691. }, /* generic */
  692. {
  693. "smc1211",
  694. (0x1211 << 16) | 0x1113,
  695. }, /* SMC EZ-Card */
  696. {
  697. "dfe-538tx",
  698. (0x1300 << 16) | 0x1186,
  699. }, /* D-Link DFE-538TX */
  700. {
  701. "dfe-560txd",
  702. (0x1340 << 16) | 0x1186,
  703. }, /* D-Link DFE-560TXD */
  704. {nil},
  705. };
  706. static int
  707. rtl8139pnp(Ether *edev)
  708. {
  709. int i, id;
  710. Pcidev *p;
  711. Ctlr *ctlr;
  712. unsigned char ea[Eaddrlen];
  713. /*
  714. * Make a list of all ethernet controllers
  715. * if not already done.
  716. */
  717. if(ctlrhead == nil){
  718. p = nil;
  719. while((p = pcimatch(p, 0, 0)) != nil){
  720. if(p->ccrb != 0x02 || p->ccru != 0)
  721. continue;
  722. ctlr = malloc(sizeof(Ctlr));
  723. if(ctlr == nil)
  724. error(Enomem);
  725. ctlr->pcidev = p;
  726. ctlr->id = (p->did << 16) | p->vid;
  727. if(ctlrhead != nil)
  728. ctlrtail->next = ctlr;
  729. else
  730. ctlrhead = ctlr;
  731. ctlrtail = ctlr;
  732. }
  733. }
  734. /*
  735. * Is it an RTL8139 under a different name?
  736. * Normally a search is made through all the found controllers
  737. * for one which matches any of the known vid+did pairs.
  738. * If a vid+did pair is specified a search is made for that
  739. * specific controller only.
  740. */
  741. id = 0;
  742. for(i = 0; i < edev->ISAConf.nopt; i++){
  743. if(cistrncmp(edev->ISAConf.opt[i], "id=", 3) == 0)
  744. id = strtol(&edev->ISAConf.opt[i][3], nil, 0);
  745. }
  746. ctlr = nil;
  747. if(id != 0)
  748. ctlr = rtl8139match(edev, id);
  749. else
  750. for(i = 0; rtl8139pci[i].name; i++){
  751. if((ctlr = rtl8139match(edev, rtl8139pci[i].id)) != nil)
  752. break;
  753. }
  754. if(ctlr == nil)
  755. return -1;
  756. edev->ctlr = ctlr;
  757. edev->ISAConf.port = ctlr->port;
  758. edev->ISAConf.irq = ctlr->pcidev->intl;
  759. edev->tbdf = ctlr->pcidev->tbdf;
  760. /*
  761. * Check if the adapter's station address is to be overridden.
  762. * If not, read it from the device and set in edev->ea.
  763. */
  764. memset(ea, 0, Eaddrlen);
  765. if(memcmp(ea, edev->ea, Eaddrlen) == 0){
  766. i = csr32r(ctlr, Idr0);
  767. edev->ea[0] = i;
  768. edev->ea[1] = i >> 8;
  769. edev->ea[2] = i >> 16;
  770. edev->ea[3] = i >> 24;
  771. i = csr32r(ctlr, Idr0 + 4);
  772. edev->ea[4] = i;
  773. edev->ea[5] = i >> 8;
  774. }
  775. edev->Netif.arg = edev;
  776. edev->attach = rtl8139attach;
  777. edev->transmit = rtl8139transmit;
  778. edev->interrupt = rtl8139interrupt;
  779. edev->ifstat = rtl8139ifstat;
  780. edev->Netif.promiscuous = rtl8139promiscuous;
  781. edev->Netif.multicast = rtl8139multicast;
  782. edev->shutdown = rtl8139shutdown;
  783. /*
  784. * This should be much more dynamic but will do for now.
  785. */
  786. if((csr8r(ctlr, Msr) & (Speed10 | Linkb)) == 0)
  787. edev->Netif.mbps = 100;
  788. return 0;
  789. }
  790. void
  791. ether8139link(void)
  792. {
  793. addethercard("rtl8139", rtl8139pnp);
  794. }