etherrhine.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. Via Rhine driver, written for VT6102.
  3. Uses the ethermii to control PHY.
  4. Currently always copies on both, tx and rx.
  5. rx side could be copy-free, and tx-side might be made
  6. (almost) copy-free by using (possibly) two descriptors (if it allows
  7. arbitrary tx lengths, which it should..): first for alignment and
  8. second for rest of the frame. Rx-part should be worth doing.
  9. */
  10. #include "u.h"
  11. #include "../port/lib.h"
  12. #include "mem.h"
  13. #include "dat.h"
  14. #include "fns.h"
  15. #include "io.h"
  16. #include "../port/error.h"
  17. #include "../port/netif.h"
  18. #include "etherif.h"
  19. #include "ethermii.h"
  20. typedef struct Desc Desc;
  21. typedef struct Ctlr Ctlr;
  22. enum {
  23. Ntxd = 16,
  24. Nrxd = 64,
  25. Nwait = 50,
  26. Ntxstats = 9,
  27. Nrxstats = 8,
  28. BIGSTR = 8192,
  29. };
  30. struct Desc {
  31. ulong stat;
  32. ulong size;
  33. ulong addr;
  34. ulong next;
  35. char *buf;
  36. ulong pad[3];
  37. };
  38. struct Ctlr {
  39. Pcidev *pci;
  40. int attached;
  41. int txused;
  42. int txhead;
  43. int txtail;
  44. int rxtail;
  45. ulong port;
  46. Mii mii;
  47. ulong txstats[Ntxstats];
  48. ulong rxstats[Nrxstats];
  49. Desc *txd; /* wants to be aligned on 16-byte boundary */
  50. Desc *rxd;
  51. QLock attachlck;
  52. Lock lock;
  53. };
  54. #define ior8(c, r) (inb((c)->port+(r)))
  55. #define ior16(c, r) (ins((c)->port+(r)))
  56. #define ior32(c, r) (inl((c)->port+(r)))
  57. #define iow8(c, r, b) (outb((c)->port+(r), (int)(b)))
  58. #define iow16(c, r, w) (outs((c)->port+(r), (ushort)(w)))
  59. #define iow32(c, r, l) (outl((c)->port+(r), (ulong)(l)))
  60. enum Regs {
  61. Eaddr = 0x0,
  62. Rcr = 0x6,
  63. Tcr = 0x7,
  64. Cr = 0x8,
  65. Isr = 0xc,
  66. Imr = 0xe,
  67. McastAddr = 0x10,
  68. RxdAddr = 0x18,
  69. TxdAddr = 0x1C,
  70. Bcr = 0x6e,
  71. RhineMiiPhy = 0x6C,
  72. RhineMiiSr = 0x6D,
  73. RhineMiiCr = 0x70,
  74. RhineMiiAddr = 0x71,
  75. RhineMiiData = 0x72,
  76. Eecsr = 0x74,
  77. ConfigB = 0x79,
  78. ConfigD = 0x7B,
  79. MiscCr = 0x80,
  80. HwSticky = 0x83,
  81. MiscIsr = 0x84,
  82. MiscImr = 0x86,
  83. WolCrSet = 0xA0,
  84. WolCfgSet = 0xA1,
  85. WolCgSet = 0xA3,
  86. WolCrClr = 0xA4,
  87. PwrCfgClr = 0xA5,
  88. WolCgClr = 0xA7,
  89. };
  90. enum Rcrbits {
  91. RxErrX = 1<<0,
  92. RxSmall = 1<<1,
  93. RxMcast = 1<<2,
  94. RxBcast = 1<<3,
  95. RxProm = 1<<4,
  96. RxFifo64 = 0<<5, RxFifo32 = 1<<5, RxFifo128 = 2<<5, RxFifo256 = 3<<5,
  97. RxFifo512 = 4<<5, RxFifo768 = 5<<5, RxFifo1024 = 6<<5,
  98. RxFifoStoreForward = 7<<5,
  99. };
  100. enum Tcrbits {
  101. TxLoopback0 = 1<<1,
  102. TxLoopback1 = 1<<2,
  103. TxBackoff = 1<<3,
  104. TxFifo128 = 0<<5, TxFifo256 = 1<<5, TxFifo512 = 2<<5, TxFifo1024 = 3<<5,
  105. TxFifoStoreForward = 7<<5,
  106. };
  107. enum Crbits {
  108. Init = 1<<0,
  109. Start = 1<<1,
  110. Stop = 1<<2,
  111. RxOn = 1<<3,
  112. TxOn = 1<<4,
  113. Tdmd = 1<<5,
  114. Rdmd = 1<<6,
  115. EarlyRx = 1<<8,
  116. Reserved0 = 1<<9,
  117. FullDuplex = 1<<10,
  118. NoAutoPoll = 1<<11,
  119. Reserved1 = 1<<12,
  120. Tdmd1 = 1<<13,
  121. Rdmd1 = 1<<14,
  122. Reset = 1<<15,
  123. };
  124. enum Isrbits {
  125. RxOk = 1<<0,
  126. TxOk = 1<<1,
  127. RxErr = 1<<2,
  128. TxErr = 1<<3,
  129. TxBufUdf = 1<<4,
  130. RxBufLinkErr = 1<<5,
  131. BusErr = 1<<6,
  132. CrcOvf = 1<<7,
  133. EarlyRxInt = 1<<8,
  134. TxFifoUdf = 1<<9,
  135. RxFifoOvf = 1<<10,
  136. TxPktRace = 1<<11,
  137. NoRxbuf = 1<<12,
  138. TxCollision = 1<<13,
  139. PortCh = 1<<14,
  140. GPInt = 1<<15
  141. };
  142. enum Bcrbits {
  143. Dma32 = 0<<0, Dma64 = 1<<0, Dma128 = 2<<0,
  144. Dma256 = 3<<0, Dma512 = 4<<0, Dma1024 = 5<<0,
  145. DmaStoreForward = 7<<0,
  146. DupRxFifo0 = 1<<3, DupRxFifo1 = 1<<4, DupRxFifo2 = 1<<5,
  147. ExtraLed = 1<<6,
  148. MediumSelect = 1<<7,
  149. PollTimer0 = 1<<8, PollTimer1 = 1<<9, PollTimer2 = 1<<10,
  150. DupTxFifo0 = 1<<11, DupTxFifo1 = 1<<12, DupTxFifo2 = 1<<13,
  151. };
  152. enum Eecsrbits {
  153. EeAutoLoad = 1<<5,
  154. };
  155. enum MiscCrbits {
  156. Timer0Enable= 1<<0,
  157. Timer0Suspend = 1<<1,
  158. HalfDuplexFlowControl = 1<<2,
  159. FullDuplexFlowControl = 1<<3,
  160. Timer1Enable = 1<<8,
  161. ForceSoftReset = 1<<14,
  162. };
  163. enum HwStickybits {
  164. StickyDS0 = 1<<0,
  165. StickyDS1 = 1<<1,
  166. WOLEna = 1<<2,
  167. WOLStat = 1<<3,
  168. };
  169. enum WolCgbits {
  170. PmeOvr = 1<<7,
  171. };
  172. enum Descbits {
  173. OwnNic = 1<<31, /* stat */
  174. TxAbort = 1<<8, /* stat */
  175. TxError = 1<<15, /* stat */
  176. RxChainbuf = 1<<10, /* stat */
  177. RxChainStart = 1<<9, /* stat */
  178. RxChainEnd = 1<<8, /* stat */
  179. Chainbuf = 1<<15, /* size rx & tx*/
  180. TxDisableCrc = 1<<16, /* size */
  181. TxChainStart = 1<<21, /* size */
  182. TxChainEnd = 1<<22, /* size */
  183. TxInt = 1<<23, /* size */
  184. };
  185. enum ConfigDbits {
  186. BackoffOptional = 1<<0,
  187. BackoffAMD = 1<<1,
  188. BackoffDEC = 1<<2,
  189. BackoffRandom = 1<<3,
  190. PmccTestMode = 1<<4,
  191. PciReadlineCap = 1<<5,
  192. DiagMode = 1<<6,
  193. MmioEnable = 1<<7,
  194. };
  195. enum ConfigBbits {
  196. LatencyTimer = 1<<0,
  197. WriteWaitState = 1<<1,
  198. ReadWaitState = 1<<2,
  199. RxArbit = 1<<3,
  200. TxArbit = 1<<4,
  201. NoMemReadline = 1<<5,
  202. NoParity = 1<<6,
  203. NoTxQueuing = 1<<7,
  204. };
  205. enum RhineMiiCrbits {
  206. Mdc = 1<<0,
  207. Mdi = 1<<1,
  208. Mdo = 1<<2,
  209. Mdout = 1<<3,
  210. Mdpm = 1<<4,
  211. Wcmd = 1<<5,
  212. Rcmd = 1<<6,
  213. Mauto = 1<<7,
  214. };
  215. enum RhineMiiSrbits {
  216. Speed10M = 1<<0,
  217. LinkFail = 1<<1,
  218. PhyError = 1<<3,
  219. DefaultPhy = 1<<4,
  220. ResetPhy = 1<<7,
  221. };
  222. enum RhineMiiAddrbits {
  223. Mdone = 1<<5,
  224. Msrcen = 1<<6,
  225. Midle = 1<<7,
  226. };
  227. static char *
  228. txstatnames[Ntxstats] = {
  229. "aborts (excess collisions)",
  230. "out of window collisions",
  231. "carrier sense losses",
  232. "fifo underflows",
  233. "invalid descriptor format or underflows",
  234. "system errors",
  235. "reserved",
  236. "transmit errors",
  237. "collisions",
  238. };
  239. static char *
  240. rxstatnames[Nrxstats] = {
  241. "receiver errors",
  242. "crc errors",
  243. "frame alignment errors",
  244. "fifo overflows",
  245. "long packets",
  246. "run packets",
  247. "system errors",
  248. "buffer underflows",
  249. };
  250. static void
  251. attach(Ether *edev)
  252. {
  253. Ctlr *ctlr;
  254. Desc *txd, *rxd, *td, *rd;
  255. Mii *mi;
  256. MiiPhy *phy;
  257. int i, s;
  258. ctlr = edev->ctlr;
  259. qlock(&ctlr->attachlck);
  260. if (ctlr->attached == 0) {
  261. txd = ctlr->txd;
  262. rxd = ctlr->rxd;
  263. for (i = 0; i < Ntxd; ++i) {
  264. td = &txd[i];
  265. td->next = PCIWADDR(&txd[(i+1) % Ntxd]);
  266. td->buf = xspanalloc(sizeof(Etherpkt)+4, 4, 0);
  267. td->addr = PCIWADDR(td->buf);
  268. td->size = 0;
  269. coherence();
  270. td->stat = 0;
  271. }
  272. for (i = 0; i < Nrxd; ++i) {
  273. rd = &rxd[i];
  274. rd->next = PCIWADDR(&rxd[(i+1) % Nrxd]);
  275. rd->buf = xspanalloc(sizeof(Etherpkt)+4, 4, 0);
  276. rd->addr = PCIWADDR(rd->buf);
  277. rd->size = sizeof(Etherpkt)+4;
  278. coherence();
  279. rd->stat = OwnNic;
  280. }
  281. ctlr->txhead = ctlr->txtail = ctlr->rxtail = 0;
  282. mi = &ctlr->mii;
  283. miistatus(mi);
  284. phy = mi->curphy;
  285. s = splhi();
  286. iow32(ctlr, TxdAddr, PCIWADDR(&txd[0]));
  287. iow32(ctlr, RxdAddr, PCIWADDR(&rxd[0]));
  288. iow16(ctlr, Cr, (phy->fd ? FullDuplex : 0) | NoAutoPoll | TxOn | RxOn | Start | Rdmd);
  289. iow16(ctlr, Isr, 0xFFFF);
  290. iow16(ctlr, Imr, 0xFFFF);
  291. iow8(ctlr, MiscIsr, 0xFF);
  292. iow8(ctlr, MiscImr, ~(3<<5));
  293. splx(s);
  294. }
  295. ctlr->attached++;
  296. qunlock(&ctlr->attachlck);
  297. }
  298. static void
  299. txstart(Ether *edev)
  300. {
  301. Ctlr *ctlr;
  302. Desc *txd, *td;
  303. Block *b;
  304. int i, txused, n;
  305. ulong size;
  306. ctlr = edev->ctlr;
  307. txd = ctlr->txd;
  308. i = ctlr->txhead;
  309. txused = ctlr->txused;
  310. n = 0;
  311. while (txused < Ntxd) {
  312. if ((b = qget(edev->oq)) == nil)
  313. break;
  314. td = &txd[i];
  315. size = BLEN(b);
  316. memmove(td->buf, b->rp, size);
  317. freeb(b);
  318. td->size = size | TxChainStart | TxChainEnd | TxInt; /* could reduce number of ints here */
  319. coherence();
  320. td->stat = OwnNic;
  321. i = (i + 1) % Ntxd;
  322. txused++;
  323. n++;
  324. }
  325. if (n)
  326. iow16(ctlr, Cr, ior16(ctlr, Cr) | Tdmd);
  327. ctlr->txhead = i;
  328. ctlr->txused = txused;
  329. }
  330. static void
  331. transmit(Ether *edev)
  332. {
  333. Ctlr *ctlr;
  334. ctlr = edev->ctlr;
  335. ilock(&ctlr->lock);
  336. txstart(edev);
  337. iunlock(&ctlr->lock);
  338. }
  339. static void
  340. txcomplete(Ether *edev)
  341. {
  342. Ctlr *ctlr;
  343. Desc *txd, *td;
  344. int i, txused, j;
  345. ulong stat;
  346. ctlr = edev->ctlr;
  347. txd = ctlr->txd;
  348. txused = ctlr->txused;
  349. i = ctlr->txtail;
  350. while (txused > 0) {
  351. td = &txd[i];
  352. stat = td->stat;
  353. if (stat & OwnNic)
  354. break;
  355. ctlr->txstats[Ntxstats-1] += stat & 0xF;
  356. for (j = 0; j < Ntxstats-1; ++j)
  357. if (stat & (1<<(j+8)))
  358. ctlr->txstats[j]++;
  359. i = (i + 1) % Ntxd;
  360. txused--;
  361. }
  362. ctlr->txused = txused;
  363. ctlr->txtail = i;
  364. if (txused <= Ntxd/2)
  365. txstart(edev);
  366. }
  367. static void
  368. interrupt(Ureg *, void *arg)
  369. {
  370. Ether *edev;
  371. Ctlr *ctlr;
  372. ushort isr, misr;
  373. ulong stat;
  374. Desc *rxd, *rd;
  375. int i, n, j;
  376. edev = (Ether*)arg;
  377. ctlr = edev->ctlr;
  378. iow16(ctlr, Imr, 0);
  379. isr = ior16(ctlr, Isr);
  380. iow16(ctlr, Isr, 0xFFFF);
  381. misr = ior16(ctlr, MiscIsr) & ~(3<<5); /* don't care about used defined ints */
  382. if (isr & RxOk) {
  383. Block *b;
  384. int size;
  385. rxd = ctlr->rxd;
  386. i = ctlr->rxtail;
  387. n = 0;
  388. while ((rxd[i].stat & OwnNic) == 0) {
  389. rd = &rxd[i];
  390. stat = rd->stat;
  391. for (j = 0; j < Nrxstats; ++j)
  392. if (stat & (1<<j))
  393. ctlr->rxstats[j]++;
  394. if (stat & 0xFF)
  395. iprint("rx: %lux\n", stat & 0xFF);
  396. size = ((rd->stat>>16) & 2047) - 4;
  397. b = iallocb(sizeof(Etherpkt));
  398. memmove(b->wp, rd->buf, size);
  399. b->wp += size;
  400. etheriq(edev, b, 1);
  401. rd->size = sizeof(Etherpkt)+4;
  402. coherence();
  403. rd->stat = OwnNic;
  404. i = (i + 1) % Nrxd;
  405. n++;
  406. }
  407. if (n)
  408. iow16(ctlr, Cr, ior16(ctlr, Cr) | Rdmd);
  409. ctlr->rxtail = i;
  410. isr &= ~RxOk;
  411. }
  412. if (isr & TxOk) {
  413. txcomplete(edev);
  414. isr &= ~TxOk;
  415. }
  416. if (isr | misr)
  417. iprint("etherrhine: unhandled irq(s). isr:%x misr:%x\n", isr, misr);
  418. iow16(ctlr, Imr, 0xFFFF);
  419. }
  420. static void
  421. promiscuous(void *arg, int enable)
  422. {
  423. Ether *edev;
  424. Ctlr *ctlr;
  425. edev = arg;
  426. ctlr = edev->ctlr;
  427. ilock(&ctlr->lock);
  428. iow8(ctlr, Rcr, (ior8(ctlr, Rcr) & ~(RxProm|RxBcast)) |
  429. (enable ? RxProm : RxBcast));
  430. iunlock(&ctlr->lock);
  431. }
  432. static int
  433. miiread(Mii *mii, int phy, int reg)
  434. {
  435. Ctlr *ctlr;
  436. int n;
  437. ctlr = mii->ctlr;
  438. n = Nwait;
  439. while (n-- && ior8(ctlr, RhineMiiCr) & (Rcmd | Wcmd))
  440. microdelay(1);
  441. if (n == Nwait)
  442. iprint("etherrhine: miiread: timeout\n");
  443. iow8(ctlr, RhineMiiCr, 0);
  444. iow8(ctlr, RhineMiiPhy, phy);
  445. iow8(ctlr, RhineMiiAddr, reg);
  446. iow8(ctlr, RhineMiiCr, Rcmd);
  447. n = Nwait;
  448. while (n-- && ior8(ctlr, RhineMiiCr) & Rcmd)
  449. microdelay(1);
  450. if (n == Nwait)
  451. iprint("etherrhine: miiread: timeout\n");
  452. n = ior16(ctlr, RhineMiiData);
  453. return n;
  454. }
  455. static int
  456. miiwrite(Mii *mii, int phy, int reg, int data)
  457. {
  458. int n;
  459. Ctlr *ctlr;
  460. ctlr = mii->ctlr;
  461. n = Nwait;
  462. while (n-- && ior8(ctlr, RhineMiiCr) & (Rcmd | Wcmd))
  463. microdelay(1);
  464. if (n == Nwait)
  465. iprint("etherrhine: miiwrite: timeout\n");
  466. iow8(ctlr, RhineMiiCr, 0);
  467. iow8(ctlr, RhineMiiPhy, phy);
  468. iow8(ctlr, RhineMiiAddr, reg);
  469. iow16(ctlr, RhineMiiData, data);
  470. iow8(ctlr, RhineMiiCr, Wcmd);
  471. n = Nwait;
  472. while (n-- && ior8(ctlr, RhineMiiCr) & Wcmd)
  473. microdelay(1);
  474. if (n == Nwait)
  475. iprint("etherrhine: miiwrite: timeout\n");
  476. return 0;
  477. }
  478. /* multicast already on, don't need to do anything */
  479. static void
  480. multicast(void*, uchar*, int)
  481. {
  482. }
  483. static void
  484. shutdown(Ether *edev)
  485. {
  486. int i;
  487. Ctlr *ctlr = edev->ctlr;
  488. ilock(&ctlr->lock);
  489. pcisetbme(ctlr->pci);
  490. iow16(ctlr, Cr, ior16(ctlr, Cr) | Stop);
  491. iow16(ctlr, Cr, ior16(ctlr, Cr) | Reset);
  492. for (i = 0; i < Nwait; ++i) {
  493. if ((ior16(ctlr, Cr) & Reset) == 0)
  494. break;
  495. delay(5);
  496. }
  497. if (i == Nwait)
  498. iprint("etherrhine: reset timeout\n");
  499. iunlock(&ctlr->lock);
  500. }
  501. static void
  502. init(Ether *edev)
  503. {
  504. Ctlr *ctlr;
  505. MiiPhy *phy;
  506. int i;
  507. shutdown(edev);
  508. ctlr = edev->ctlr;
  509. ilock(&ctlr->lock);
  510. iow8(ctlr, Eecsr, ior8(ctlr, Eecsr) | EeAutoLoad);
  511. for (i = 0; i < Nwait; ++i) {
  512. if ((ior8(ctlr, Eecsr) & EeAutoLoad) == 0)
  513. break;
  514. delay(5);
  515. }
  516. if (i == Nwait)
  517. iprint("etherrhine: eeprom autoload timeout\n");
  518. for (i = 0; i < Eaddrlen; ++i)
  519. edev->ea[i] = ior8(ctlr, Eaddr + i);
  520. ctlr->mii.mir = miiread;
  521. ctlr->mii.miw = miiwrite;
  522. ctlr->mii.ctlr = ctlr;
  523. if(mii(&ctlr->mii, ~0) == 0 || ctlr->mii.curphy == nil){
  524. iprint("etherrhine: init mii failure\n");
  525. return;
  526. }
  527. for (i = 0; i < NMiiPhy; ++i)
  528. if (ctlr->mii.phy[i])
  529. if (ctlr->mii.phy[i]->oui != 0xFFFFF)
  530. ctlr->mii.curphy = ctlr->mii.phy[i];
  531. miistatus(&ctlr->mii);
  532. phy = ctlr->mii.curphy;
  533. edev->mbps = phy->speed;
  534. iow16(ctlr, Imr, 0);
  535. iow16(ctlr, Cr, ior16(ctlr, Cr) | Stop);
  536. iow8(ctlr, Rcr, ior8(ctlr, Rcr) | RxMcast);
  537. iunlock(&ctlr->lock);
  538. }
  539. static Pcidev *
  540. rhinematch(ulong)
  541. {
  542. static int nrhines = 0;
  543. int nfound = 0;
  544. Pcidev *p = nil;
  545. while (p = pcimatch(p, 0x1106, 0))
  546. if (p->did == 0x3065)
  547. if (++nfound > nrhines) {
  548. nrhines++;
  549. break;
  550. }
  551. return p;
  552. }
  553. static long
  554. ifstat(Ether* edev, void* a, long n, ulong offset)
  555. {
  556. int l = 0, i;
  557. char *p;
  558. Ctlr *ctlr;
  559. ctlr = edev->ctlr;
  560. p = malloc(BIGSTR);
  561. for (i = 0; i < Ntxstats; ++i)
  562. if (txstatnames[i])
  563. l += snprint(p+l, BIGSTR - l, "tx: %s: %lud\n", txstatnames[i], ctlr->txstats[i]);
  564. for (i = 0; i < Nrxstats; ++i)
  565. if (rxstatnames[i])
  566. l += snprint(p+l, BIGSTR - l, "rx: %s: %lud\n", rxstatnames[i], ctlr->rxstats[i]);
  567. /*
  568. for (i = 0; i < NMiiPhyr; ++i) {
  569. if ((i % 8) == 0)
  570. l += snprint(p + l, BIGSTR - l, "\nmii 0x%02x:", i);
  571. reg=miimir(&ctlr->mii, i);
  572. reg=miimir(&ctlr->mii, i);
  573. l += snprint(p + l, BIGSTR - l, " %4ux", reg);
  574. }
  575. for (i = 0; i < 0x100; i+=1) {
  576. if ((i % 16) == 0)
  577. l += snprint(p + l, BIGSTR - l, "\nreg 0x%02x:", i);
  578. else if ((i % 2) == 0)
  579. l += snprint(p + l, BIGSTR - l, " ");
  580. reg=ior8(ctlr, i);
  581. l += snprint(p + l, BIGSTR - l, "%02x", reg);
  582. }
  583. l += snprint(p + l, BIGSTR - l, " \n");
  584. */
  585. n = readstr(offset, a, n, p);
  586. free(p);
  587. return n;
  588. }
  589. static int
  590. pnp(Ether *edev)
  591. {
  592. Pcidev *p;
  593. Ctlr *ctlr;
  594. ulong port;
  595. ulong size;
  596. p = rhinematch(edev->port);
  597. if (p == nil)
  598. return -1;
  599. port = p->mem[0].bar & ~1;
  600. size = p->mem[0].size;
  601. if (ioalloc(port, size, 0, "rhine") < 0) {
  602. print("etherrhine: couldn't allocate port %lud\n", port);
  603. return -1;
  604. }
  605. if ((ctlr = malloc(sizeof(Ctlr))) == nil) {
  606. print("etherrhine: couldn't allocate memory for ctlr\n");
  607. return -1;
  608. }
  609. memset(ctlr, 0, sizeof(Ctlr));
  610. ctlr->txd = xspanalloc(sizeof(Desc) * Ntxd, 16, 0);
  611. ctlr->rxd = xspanalloc(sizeof(Desc) * Nrxd, 16, 0);
  612. ctlr->pci = p;
  613. ctlr->port = port;
  614. edev->ctlr = ctlr;
  615. edev->port = ctlr->port;
  616. edev->irq = p->intl;
  617. edev->tbdf = p->tbdf;
  618. init(edev);
  619. edev->interrupt = interrupt;
  620. edev->arg = edev;
  621. edev->attach = attach;
  622. edev->transmit = transmit;
  623. edev->ifstat = ifstat;
  624. edev->promiscuous = promiscuous;
  625. edev->multicast = multicast;
  626. edev->shutdown = shutdown;
  627. return 0;
  628. }
  629. void
  630. etherrhinelink(void)
  631. {
  632. addethercard("rhine", pnp);
  633. }