ether82598.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /*
  2. * intel 10gbe pci-express driver
  3. * copyright © 2007, coraid, inc.
  4. */
  5. #include "u.h"
  6. #include "../port/lib.h"
  7. #include "mem.h"
  8. #include "dat.h"
  9. #include "fns.h"
  10. #include "io.h"
  11. #include "../port/error.h"
  12. #include "../port/netif.h"
  13. #include "etherif.h"
  14. /*
  15. * // comments note conflicts with 82563-style drivers,
  16. * and the registers are all different.
  17. */
  18. enum {
  19. /* general */
  20. Ctrl = 0x00000/4, /* Device Control */
  21. Status = 0x00008/4, /* Device Status */
  22. Ctrlext = 0x00018/4, /* Extended Device Control */
  23. Esdp = 0x00020/4, /* extended sdp control */
  24. Esodp = 0x00028/4, /* extended od sdp control */
  25. Ledctl = 0x00200/4, /* led control */
  26. Tcptimer = 0x0004c/4, /* tcp timer */
  27. Ecc = 0x110b0/4, /* errata ecc control magic */
  28. /* nvm */
  29. Eec = 0x10010/4, /* eeprom/flash control */
  30. Eerd = 0x10014/4, /* eeprom read */
  31. Fla = 0x1001c/4, /* flash access */
  32. Flop = 0x1013c/4, /* flash opcode */
  33. Grc = 0x10200/4, /* general rx control */
  34. /* interrupt */
  35. Icr = 0x00800/4, /* interrupt cause read */
  36. Ics = 0x00808/4, /* " set */
  37. Ims = 0x00880/4, /* " mask read/set */
  38. Imc = 0x00888/4, /* " mask clear */
  39. Iac = 0x00810/4, /* " ayto clear */
  40. Iam = 0x00890/4, /* " auto mask enable */
  41. Itr = 0x00820/4, /* " throttling rate (0-19) */
  42. Ivar = 0x00900/4, /* " vector allocation regs. */
  43. /* msi interrupt */
  44. Msixt = 0x0000/4, /* msix table (bar3) */
  45. Msipba = 0x2000/4, /* msix pending bit array (bar3) */
  46. Pbacl = 0x11068/4, /* pba clear */
  47. Gpie = 0x00898/4, /* general purpose int enable */
  48. /* flow control */
  49. Pfctop = 0x03008/4, /* priority flow ctl type opcode */
  50. Fcttv = 0x03200/4, /* " transmit timer value (0-3) */
  51. Fcrtl = 0x03220/4, /* " rx threshold low (0-7) +8n */
  52. Fcrth = 0x03260/4, /* " rx threshold high (0-7) +8n */
  53. Rcrtv = 0x032a0/4, /* " refresh value threshold */
  54. Tfcs = 0x0ce00/4, /* " tx status */
  55. /* rx dma */
  56. Rbal = 0x01000/4, /* rx desc base low (0-63) +0x40n */
  57. Rbah = 0x01004/4, /* " high */
  58. Rdlen = 0x01008/4, /* " length */
  59. Rdh = 0x01010/4, /* " head */
  60. Rdt = 0x01018/4, /* " tail */
  61. Rxdctl = 0x01028/4, /* " control */
  62. Srrctl = 0x02100/4, /* split and replication rx ctl. */
  63. Dcarxctl = 0x02200/4, /* rx dca control */
  64. Rdrxctl = 0x02f00/4, /* rx dma control */
  65. Rxpbsize = 0x03c00/4, /* rx packet buffer size */
  66. Rxctl = 0x03000/4, /* rx control */
  67. Dropen = 0x03d04/4, /* drop enable control */
  68. /* rx */
  69. Rxcsum = 0x05000/4, /* rx checksum control */
  70. Rfctl = 0x04008/4, /* rx filter control */
  71. Mta = 0x05200/4, /* multicast table array (0-127) */
  72. Ral = 0x05400/4, /* rx address low */
  73. Rah = 0x05404/4,
  74. Psrtype = 0x05480/4, /* packet split rx type. */
  75. Vfta = 0x0a000/4, /* vlan filter table array. */
  76. Fctrl = 0x05080/4, /* filter control */
  77. Vlnctrl = 0x05088/4, /* vlan control */
  78. Msctctrl = 0x05090/4, /* multicast control */
  79. Mrqc = 0x05818/4, /* multiple rx queues cmd */
  80. Vmdctl = 0x0581c/4, /* vmdq control */
  81. Imir = 0x05a80/4, /* immediate irq rx (0-7) */
  82. Imirext = 0x05aa0/4, /* immediate irq rx ext */
  83. Imirvp = 0x05ac0/4, /* immediate irq vlan priority */
  84. Reta = 0x05c00/4, /* redirection table */
  85. Rssrk = 0x05c80/4, /* rss random key */
  86. /* tx */
  87. Tdbal = 0x06000/4, /* tx desc base low +0x40n */
  88. Tdbah = 0x06004/4, /* " high */
  89. Tdlen = 0x06008/4, /* " len */
  90. Tdh = 0x06010/4, /* " head */
  91. Tdt = 0x06018/4, /* " tail */
  92. Txdctl = 0x06028/4, /* " control */
  93. Tdwbal = 0x06038/4, /* " write-back address low */
  94. Tdwbah = 0x0603c/4,
  95. Dtxctl = 0x07e00/4, /* tx dma control */
  96. Tdcatxctrl = 0x07200/4, /* tx dca register (0-15) */
  97. Tipg = 0x0cb00/4, /* tx inter-packet gap */
  98. Txpbsize = 0x0cc00/4, /* tx packet-buffer size (0-15) */
  99. /* mac */
  100. Hlreg0 = 0x04240/4, /* highlander control reg 0 */
  101. Hlreg1 = 0x04244/4, /* highlander control reg 1 (ro) */
  102. Msca = 0x0425c/4, /* mdi signal cmd & addr */
  103. Msrwd = 0x04260/4, /* mdi single rw data */
  104. Mhadd = 0x04268/4, /* mac addr high & max frame */
  105. Pcss1 = 0x04288/4, /* xgxs status 1 */
  106. Pcss2 = 0x0428c/4,
  107. Xpcss = 0x04290/4, /* 10gb-x pcs status */
  108. Serdesc = 0x04298/4, /* serdes control */
  109. Macs = 0x0429c/4, /* fifo control & report */
  110. Autoc = 0x042a0/4, /* autodetect control & status */
  111. Links = 0x042a4/4, /* link status */
  112. Autoc2 = 0x042a8/4,
  113. };
  114. enum {
  115. /* Ctrl */
  116. Rst = 1<<26, /* full nic reset */
  117. /* Txdctl */
  118. Ten = 1<<25,
  119. /* Fctrl */
  120. Bam = 1<<10, /* broadcast accept mode */
  121. Upe = 1<<9, /* unicast promiscuous */
  122. Mpe = 1<<8, /* multicast promiscuous */
  123. /* Rxdctl */
  124. Pthresh = 0, /* prefresh threshold shift in bits */
  125. Hthresh = 8, /* host buffer minimum threshold " */
  126. Wthresh = 16, /* writeback threshold */
  127. Renable = 1<<25,
  128. /* Rxctl */
  129. Rxen = 1<<0,
  130. Dmbyps = 1<<1,
  131. /* Rdrxctl */
  132. Rdmt½ = 0,
  133. Rdmt¼ = 1,
  134. Rdmt⅛ = 2,
  135. /* Rxcsum */
  136. Ippcse = 1<<12, /* ip payload checksum enable */
  137. /* Eerd */
  138. EEstart = 1<<0, /* Start Read */
  139. EEdone = 1<<1, /* Read done */
  140. /* interrupts */
  141. Irx0 = 1<<0, /* driver defined */
  142. Itx0 = 1<<1, /* driver defined */
  143. Lsc = 1<<20, /* link status change */
  144. /* Links */
  145. Lnkup = 1<<30,
  146. Lnkspd = 1<<29,
  147. /* Hlreg0 */
  148. Jumboen = 1<<2,
  149. };
  150. typedef struct {
  151. uint reg;
  152. char *name;
  153. } Stat;
  154. Stat stattab[] = {
  155. 0x4000, "crc error",
  156. 0x4004, "illegal byte",
  157. 0x4008, "short packet",
  158. 0x3fa0, "missed pkt0",
  159. 0x4034, "mac local flt",
  160. 0x4038, "mac rmt flt",
  161. 0x4040, "rx length err",
  162. 0x3f60, "xon tx",
  163. 0xcf60, "xon rx",
  164. 0x3f68, "xoff tx",
  165. 0xcf68, "xoff rx",
  166. 0x405c, "rx 040",
  167. 0x4060, "rx 07f",
  168. 0x4064, "rx 100",
  169. 0x4068, "rx 200",
  170. 0x406c, "rx 3ff",
  171. 0x4070, "rx big",
  172. 0x4074, "rx ok",
  173. 0x4078, "rx bcast",
  174. 0x3fc0, "rx no buf0",
  175. 0x40a4, "rx runt",
  176. 0x40a8, "rx frag",
  177. 0x40ac, "rx ovrsz",
  178. 0x40b0, "rx jab",
  179. 0x40d0, "rx pkt",
  180. 0x40d4, "tx pkt",
  181. 0x40d8, "tx 040",
  182. 0x40dc, "tx 07f",
  183. 0x40e0, "tx 100",
  184. 0x40e4, "tx 200",
  185. 0x40e8, "tx 3ff",
  186. 0x40ec, "tx big",
  187. 0x40f4, "tx bcast",
  188. 0x4120, "xsum err",
  189. };
  190. /* status */
  191. enum {
  192. Pif = 1<<7, /* past exact filter (sic) */
  193. Ipcs = 1<<6, /* ip checksum calcuated */
  194. L4cs = 1<<5, /* layer 2 */
  195. Tcpcs = 1<<4, /* tcp checksum calcuated */
  196. Vp = 1<<3, /* 802.1q packet matched vet */
  197. Ixsm = 1<<2, /* ignore checksum */
  198. Reop = 1<<1, /* end of packet */
  199. Rdd = 1<<0, /* descriptor done */
  200. };
  201. typedef struct {
  202. u32int addr[2];
  203. ushort length;
  204. ushort cksum;
  205. uchar status;
  206. uchar errors;
  207. ushort vlan;
  208. } Rd;
  209. enum {
  210. /* Td cmd */
  211. Rs = 1<<3,
  212. Ic = 1<<2,
  213. Ifcs = 1<<1,
  214. Teop = 1<<0,
  215. /* Td status */
  216. Tdd = 1<<0,
  217. };
  218. typedef struct {
  219. u32int addr[2];
  220. ushort length;
  221. uchar cso;
  222. uchar cmd;
  223. uchar status;
  224. uchar css;
  225. ushort vlan;
  226. } Td;
  227. enum {
  228. Factive = 1<<0,
  229. Fstarted = 1<<1,
  230. };
  231. typedef struct {
  232. Pcidev *p;
  233. u32int *reg;
  234. u32int *reg3;
  235. uchar flag;
  236. int nrd;
  237. int ntd;
  238. int nrb;
  239. int rbsz;
  240. QLock slock;
  241. QLock alock;
  242. QLock tlock;
  243. Rendez lrendez;
  244. Rendez trendez;
  245. Rendez rrendez;
  246. uint im;
  247. uint lim;
  248. uint rim;
  249. uint tim;
  250. Lock imlock;
  251. char *alloc;
  252. Rd *rdba;
  253. Block **rb;
  254. uint rdt;
  255. uint rdfree;
  256. Td *tdba;
  257. uint tdh;
  258. uint tdt;
  259. Block **tb;
  260. uchar ra[Eaddrlen];
  261. uchar mta[128];
  262. ulong stats[nelem(stattab)];
  263. uint speeds[3];
  264. } Ctlr;
  265. /* tweakable paramaters */
  266. enum {
  267. Rbsz = 12*1024,
  268. Nrd = 256,
  269. Ntd = 256,
  270. Nrb = 256,
  271. };
  272. static Ctlr *ctlrtab[4];
  273. static int nctlr;
  274. static Lock rblock;
  275. static Block *rbpool;
  276. static void
  277. readstats(Ctlr *c)
  278. {
  279. int i;
  280. qlock(&c->slock);
  281. for(i = 0; i < nelem(c->stats); i++)
  282. c->stats[i] += c->reg[stattab[i].reg >> 2];
  283. qunlock(&c->slock);
  284. }
  285. static int speedtab[] = {
  286. 0,
  287. 1000,
  288. 10000,
  289. };
  290. static long
  291. ifstat(Ether *e, void *a, long n, ulong offset)
  292. {
  293. uint i, *t;
  294. char *s, *p, *q;
  295. Ctlr *c;
  296. c = e->ctlr;
  297. p = s = malloc(2*READSTR);
  298. q = p + 2*READSTR;
  299. readstats(c);
  300. for(i = 0; i < nelem(stattab); i++)
  301. if(c->stats[i] > 0)
  302. p = seprint(p, q, "%.10s %uld\n", stattab[i].name, c->stats[i]);
  303. t = c->speeds;
  304. p = seprint(p, q, "speeds: 0:%d 1000:%d 10000:%d\n", t[0], t[1], t[2]);
  305. seprint(p, q, "rdfree %d rdh %d rdt %d\n", c->rdfree, c->reg[Rdt],
  306. c->reg[Rdh]);
  307. n = readstr(offset, a, n, s);
  308. free(s);
  309. return n;
  310. }
  311. static void
  312. im(Ctlr *c, int i)
  313. {
  314. ilock(&c->imlock);
  315. c->im |= i;
  316. c->reg[Ims] = c->im;
  317. iunlock(&c->imlock);
  318. }
  319. static int
  320. lim(void *v)
  321. {
  322. return ((Ctlr*)v)->lim != 0;
  323. }
  324. static void
  325. lproc(void *v)
  326. {
  327. int r, i;
  328. Ctlr *c;
  329. Ether *e;
  330. e = v;
  331. c = e->ctlr;
  332. for (;;) {
  333. r = c->reg[Links];
  334. e->link = (r & Lnkup) != 0;
  335. i = 0;
  336. if(e->link)
  337. i = 1 + ((r & Lnkspd) != 0);
  338. c->speeds[i]++;
  339. e->mbps = speedtab[i];
  340. c->lim = 0;
  341. im(c, Lsc);
  342. sleep(&c->lrendez, lim, c);
  343. c->lim = 0;
  344. }
  345. }
  346. static long
  347. ctl(Ether *, void *, long)
  348. {
  349. error(Ebadarg);
  350. return -1;
  351. }
  352. static Block*
  353. rballoc(void)
  354. {
  355. Block *b;
  356. ilock(&rblock);
  357. if(b = rbpool){
  358. rbpool = b->next;
  359. b->next = 0;
  360. }
  361. iunlock(&rblock);
  362. return b;
  363. }
  364. void
  365. rbfree(Block *b)
  366. {
  367. b->rp = b->wp = (uchar*)PGROUND((uintptr)b->base);
  368. ilock(&rblock);
  369. b->next = rbpool;
  370. rbpool = b;
  371. iunlock(&rblock);
  372. }
  373. #define Next(x, m) (((x)+1) & (m))
  374. static int
  375. cleanup(Ctlr *c, int tdh)
  376. {
  377. Block *b;
  378. uint m, n;
  379. m = c->ntd - 1;
  380. while(c->tdba[n = Next(tdh, m)].status & Tdd){
  381. tdh = n;
  382. b = c->tb[tdh];
  383. c->tb[tdh] = 0;
  384. freeb(b);
  385. c->tdba[tdh].status = 0;
  386. }
  387. return tdh;
  388. }
  389. void
  390. transmit(Ether *e)
  391. {
  392. uint i, m, tdt, tdh;
  393. Ctlr *c;
  394. Block *b;
  395. Td *t;
  396. c = e->ctlr;
  397. // qlock(&c->tlock);
  398. if(!canqlock(&c->tlock)){
  399. im(c, Itx0);
  400. return;
  401. }
  402. tdh = c->tdh = cleanup(c, c->tdh);
  403. tdt = c->tdt;
  404. m = c->ntd - 1;
  405. for(i = 0; i < 8; i++){
  406. if(Next(tdt, m) == tdh){
  407. im(c, Itx0);
  408. break;
  409. }
  410. if(!(b = qget(e->oq)))
  411. break;
  412. t = c->tdba + tdt;
  413. t->addr[0] = PCIWADDR(b->rp);
  414. t->length = BLEN(b);
  415. t->cmd = Rs | Ifcs | Teop;
  416. c->tb[tdt] = b;
  417. tdt = Next(tdt, m);
  418. }
  419. if(i){
  420. c->tdt = tdt;
  421. c->reg[Tdt] = tdt;
  422. }
  423. qunlock(&c->tlock);
  424. }
  425. static int
  426. tim(void *c)
  427. {
  428. return ((Ctlr*)c)->tim != 0;
  429. }
  430. static void
  431. tproc(void *v)
  432. {
  433. Ctlr *c;
  434. Ether *e;
  435. e = v;
  436. c = e->ctlr;
  437. for (;;) {
  438. sleep(&c->trendez, tim, c); /* transmit kicks us */
  439. c->tim = 0;
  440. transmit(e);
  441. }
  442. }
  443. static void
  444. rxinit(Ctlr *c)
  445. {
  446. int i;
  447. Block *b;
  448. c->reg[Rxctl] &= ~Rxen;
  449. for(i = 0; i < c->nrd; i++){
  450. b = c->rb[i];
  451. c->rb[i] = 0;
  452. if(b)
  453. freeb(b);
  454. }
  455. c->rdfree = 0;
  456. c->reg[Fctrl] |= Bam;
  457. c->reg[Rxcsum] |= Ipcs;
  458. c->reg[Srrctl] = (c->rbsz + 1023)/1024;
  459. c->reg[Mhadd] = c->rbsz << 16;
  460. c->reg[Hlreg0] |= Jumboen;
  461. c->reg[Rbal] = PCIWADDR(c->rdba);
  462. c->reg[Rbah] = 0;
  463. c->reg[Rdlen] = c->nrd*sizeof(Rd);
  464. c->reg[Rdh] = 0;
  465. c->reg[Rdt] = c->rdt = 0;
  466. c->reg[Rdrxctl] = Rdmt¼;
  467. c->reg[Rxdctl] = 8<<Wthresh | 8<<Pthresh | 4<<Hthresh | Renable;
  468. c->reg[Rxctl] |= Rxen | Dmbyps;
  469. }
  470. static void
  471. replenish(Ctlr *c, uint rdh)
  472. {
  473. int rdt, m, i;
  474. Block *b;
  475. Rd *r;
  476. m = c->nrd - 1;
  477. i = 0;
  478. for(rdt = c->rdt; Next(rdt, m) != rdh; rdt = Next(rdt, m)){
  479. r = c->rdba + rdt;
  480. if(!(b = rballoc())){
  481. print("82598: no buffers\n");
  482. break;
  483. }
  484. c->rb[rdt] = b;
  485. r->addr[0] = PCIWADDR(b->rp);
  486. r->status = 0;
  487. c->rdfree++;
  488. i++;
  489. }
  490. if(i)
  491. c->reg[Rdt] = c->rdt = rdt;
  492. }
  493. static int
  494. rim(void *v)
  495. {
  496. return ((Ctlr*)v)->rim != 0;
  497. }
  498. static uchar zeroea[Eaddrlen];
  499. void
  500. rproc(void *v)
  501. {
  502. uint m, rdh;
  503. Block *b;
  504. Ctlr *c;
  505. Ether *e;
  506. Rd *r;
  507. e = v;
  508. c = e->ctlr;
  509. m = c->nrd - 1;
  510. rdh = 0;
  511. loop:
  512. replenish(c, rdh);
  513. im(c, Irx0);
  514. sleep(&c->rrendez, rim, c);
  515. loop1:
  516. c->rim = 0;
  517. if(c->nrd - c->rdfree >= 16)
  518. replenish(c, rdh);
  519. r = c->rdba + rdh;
  520. if(!(r->status & Rdd))
  521. goto loop; /* UGH */
  522. b = c->rb[rdh];
  523. c->rb[rdh] = 0;
  524. b->wp += r->length;
  525. b->lim = b->wp; /* lie like a dog */
  526. if(!(r->status & Ixsm)){
  527. if(r->status & Ipcs)
  528. b->flag |= Bipck;
  529. if(r->status & Tcpcs)
  530. b->flag |= Btcpck | Budpck;
  531. b->checksum = r->cksum;
  532. }
  533. // r->status = 0;
  534. etheriq(e, b, 1);
  535. c->rdfree--;
  536. rdh = Next(rdh, m);
  537. goto loop1; /* UGH */
  538. }
  539. static void
  540. promiscuous(void *a, int on)
  541. {
  542. Ctlr *c;
  543. Ether *e;
  544. e = a;
  545. c = e->ctlr;
  546. if(on)
  547. c->reg[Fctrl] |= Upe | Mpe;
  548. else
  549. c->reg[Fctrl] &= ~(Upe | Mpe);
  550. }
  551. static void
  552. multicast(void *a, uchar *ea, int on)
  553. {
  554. int b, i;
  555. Ctlr *c;
  556. Ether *e;
  557. e = a;
  558. c = e->ctlr;
  559. /*
  560. * multiple ether addresses can hash to the same filter bit,
  561. * so it's never safe to clear a filter bit.
  562. * if we want to clear filter bits, we need to keep track of
  563. * all the multicast addresses in use, clear all the filter bits,
  564. * then set the ones corresponding to in-use addresses.
  565. */
  566. i = ea[5] >> 1;
  567. b = (ea[5]&1)<<4 | ea[4]>>4;
  568. b = 1 << b;
  569. if(on)
  570. c->mta[i] |= b;
  571. // else
  572. // c->mta[i] &= ~b;
  573. c->reg[Mta+i] = c->mta[i];
  574. }
  575. static int
  576. detach(Ctlr *c)
  577. {
  578. int i;
  579. c->reg[Imc] = ~0;
  580. c->reg[Ctrl] |= Rst;
  581. for(i = 0; i < 100; i++){
  582. delay(1);
  583. if((c->reg[Ctrl] & Rst) == 0)
  584. break;
  585. }
  586. if (i >= 100)
  587. return -1;
  588. /* errata */
  589. delay(50);
  590. c->reg[Ecc] &= ~(1<<21 | 1<<18 | 1<<9 | 1<<6);
  591. /* not cleared by reset; kill it manually. */
  592. for(i = 1; i < 16; i++)
  593. c->reg[Rah] &= ~(1 << 31);
  594. for(i = 0; i < 128; i++)
  595. c->reg[Mta + i] = 0;
  596. for(i = 1; i < 640; i++)
  597. c->reg[Vfta + i] = 0;
  598. return 0;
  599. }
  600. static void
  601. shutdown(Ether *e)
  602. {
  603. detach(e->ctlr);
  604. }
  605. /* ≤ 20ms */
  606. static ushort
  607. eeread(Ctlr *c, int i)
  608. {
  609. c->reg[Eerd] = EEstart | i<<2;
  610. while((c->reg[Eerd] & EEdone) == 0)
  611. ;
  612. return c->reg[Eerd] >> 16;
  613. }
  614. static int
  615. eeload(Ctlr *c)
  616. {
  617. ushort u, v, p, l, i, j;
  618. if((eeread(c, 0) & 0xc0) != 0x40)
  619. return -1;
  620. u = 0;
  621. for(i = 0; i < 0x40; i++)
  622. u += eeread(c, i);
  623. for(i = 3; i < 0xf; i++){
  624. p = eeread(c, i);
  625. l = eeread(c, p++);
  626. if((int)p + l + 1 > 0xffff)
  627. continue;
  628. for(j = p; j < p + l; j++)
  629. u += eeread(c, j);
  630. }
  631. if(u != 0xbaba)
  632. return -1;
  633. if(c->reg[Status] & (1<<3))
  634. u = eeread(c, 10);
  635. else
  636. u = eeread(c, 9);
  637. u++;
  638. for(i = 0; i < Eaddrlen;){
  639. v = eeread(c, u + i/2);
  640. c->ra[i++] = v;
  641. c->ra[i++] = v>>8;
  642. }
  643. c->ra[5] += (c->reg[Status] & 0xc) >> 2;
  644. return 0;
  645. }
  646. static int
  647. reset(Ctlr *c)
  648. {
  649. int i;
  650. uchar *p;
  651. if(detach(c)){
  652. print("82598: reset timeout\n");
  653. return -1;
  654. }
  655. if(eeload(c)){
  656. print("82598: eeprom failure\n");
  657. return -1;
  658. }
  659. p = c->ra;
  660. c->reg[Ral] = p[3]<<24 | p[2]<<16 | p[1]<<8 | p[0];
  661. c->reg[Rah] = p[5]<<8 | p[4] | 1<<31;
  662. readstats(c);
  663. for(i = 0; i<nelem(c->stats); i++)
  664. c->stats[i] = 0;
  665. c->reg[Ctrlext] |= 1 << 16;
  666. /* make some guesses for flow control */
  667. c->reg[Fcrtl] = 0x10000 | 1<<31;
  668. c->reg[Fcrth] = 0x40000 | 1<<31;
  669. c->reg[Rcrtv] = 0x6000;
  670. /* configure interrupt mapping (don't ask) */
  671. c->reg[Ivar+0] = 0 | 1<<7;
  672. c->reg[Ivar+64/4] = 1 | 1<<7;
  673. // c->reg[Ivar+97/4] = (2 | 1<<7) << (8*(97%4));
  674. /* interrupt throttling goes here. */
  675. for(i = Itr; i < Itr + 20; i++)
  676. c->reg[i] = 128; /* ¼µs intervals */
  677. c->reg[Itr + Itx0] = 256;
  678. return 0;
  679. }
  680. static void
  681. txinit(Ctlr *c)
  682. {
  683. Block *b;
  684. int i;
  685. c->reg[Txdctl] = 16<<Wthresh | 16<<Pthresh;
  686. for(i = 0; i < c->ntd; i++){
  687. b = c->tb[i];
  688. c->tb[i] = 0;
  689. if(b)
  690. freeb(b);
  691. }
  692. memset(c->tdba, 0, c->ntd * sizeof(Td));
  693. c->reg[Tdbal] = PCIWADDR(c->tdba);
  694. c->reg[Tdbah] = 0;
  695. c->reg[Tdlen] = c->ntd*sizeof(Td);
  696. c->reg[Tdh] = 0;
  697. c->reg[Tdt] = 0;
  698. c->tdh = c->ntd - 1;
  699. c->tdt = 0;
  700. c->reg[Txdctl] |= Ten;
  701. }
  702. static void
  703. attach(Ether *e)
  704. {
  705. Block *b;
  706. Ctlr *c;
  707. int t;
  708. char buf[KNAMELEN];
  709. c = e->ctlr;
  710. qlock(&c->alock);
  711. if(c->alloc){
  712. qunlock(&c->alock);
  713. return;
  714. }
  715. c->nrd = Nrd;
  716. c->ntd = Ntd;
  717. t = c->nrd * sizeof *c->rdba + 255;
  718. t += c->ntd * sizeof *c->tdba + 255;
  719. t += (c->ntd + c->nrd) * sizeof(Block*);
  720. c->alloc = malloc(t);
  721. qunlock(&c->alock);
  722. if(c->alloc == nil)
  723. error(Enomem);
  724. c->rdba = (Rd*)ROUNDUP((uintptr)c->alloc, 256);
  725. c->tdba = (Td*)ROUNDUP((uintptr)(c->rdba + c->nrd), 256);
  726. c->rb = (Block**)(c->tdba + c->ntd);
  727. c->tb = (Block**)(c->rb + c->nrd);
  728. if(waserror()){
  729. while(b = rballoc()){
  730. b->free = 0;
  731. freeb(b);
  732. }
  733. free(c->alloc);
  734. c->alloc = nil;
  735. nexterror();
  736. }
  737. for(c->nrb = 0; c->nrb < 2*Nrb; c->nrb++){
  738. if(!(b = allocb(c->rbsz+BY2PG)))
  739. error(Enomem);
  740. b->free = rbfree;
  741. freeb(b);
  742. }
  743. poperror();
  744. rxinit(c);
  745. txinit(c);
  746. sprint(buf, "#l%dl", e->ctlrno);
  747. kproc(buf, lproc, e);
  748. sprint(buf, "#l%dr", e->ctlrno);
  749. kproc(buf, rproc, e);
  750. sprint(buf, "#l%dt", e->ctlrno);
  751. kproc(buf, tproc, e);
  752. }
  753. static void
  754. interrupt(Ureg*, void *v)
  755. {
  756. int icr, im;
  757. Ctlr *c;
  758. Ether *e;
  759. e = v;
  760. c = e->ctlr;
  761. ilock(&c->imlock);
  762. c->reg[Imc] = ~0;
  763. im = c->im;
  764. while((icr = c->reg[Icr] & c->im) != 0){
  765. if(icr & Lsc){
  766. im &= ~Lsc;
  767. c->lim = icr & Lsc;
  768. wakeup(&c->lrendez);
  769. }
  770. if(icr & Irx0){
  771. im &= ~Irx0;
  772. c->rim = icr & Irx0;
  773. wakeup(&c->rrendez);
  774. }
  775. if(icr & Itx0){
  776. im &= ~Itx0;
  777. c->tim = icr & Itx0;
  778. wakeup(&c->trendez);
  779. }
  780. }
  781. c->reg[Ims] = c->im = im;
  782. iunlock(&c->imlock);
  783. }
  784. static void
  785. scan(void)
  786. {
  787. ulong io, io3;
  788. void *mem, *mem3;
  789. Ctlr *c;
  790. Pcidev *p;
  791. p = 0;
  792. while(p = pcimatch(p, 0x8086, 0)){
  793. switch(p->did){
  794. case 0x10c6: /* 82598 af dual port */
  795. case 0x10c7: /* 82598 af single port */
  796. case 0x10b6: /* 82598 backplane */
  797. case 0x10dd: /* 82598 at cx4 */
  798. break;
  799. default:
  800. continue;
  801. }
  802. if(nctlr == nelem(ctlrtab)){
  803. print("i82598: too many controllers\n");
  804. return;
  805. }
  806. io = p->mem[0].bar & ~0xf;
  807. mem = vmap(io, p->mem[0].size);
  808. if(mem == nil){
  809. print("i82598: can't map %#p\n", p->mem[0].bar);
  810. continue;
  811. }
  812. io3 = p->mem[3].bar & ~0xf;
  813. mem3 = vmap(io3, p->mem[3].size);
  814. if(mem3 == nil){
  815. print("i82598: can't map %#p\n", p->mem[3].bar);
  816. vunmap(mem, p->mem[0].size);
  817. continue;
  818. }
  819. c = malloc(sizeof *c);
  820. c->p = p;
  821. c->reg = (u32int*)mem;
  822. c->reg3 = (u32int*)mem3;
  823. c->rbsz = Rbsz;
  824. if(reset(c)){
  825. print("i82598: can't reset\n");
  826. free(c);
  827. vunmap(mem, p->mem[0].size);
  828. vunmap(mem3, p->mem[3].size);
  829. continue;
  830. }
  831. pcisetbme(p);
  832. ctlrtab[nctlr++] = c;
  833. }
  834. }
  835. static int
  836. pnp(Ether *e)
  837. {
  838. int i;
  839. Ctlr *c = nil;
  840. if(nctlr == 0)
  841. scan();
  842. for(i = 0; i < nctlr; i++){
  843. c = ctlrtab[i];
  844. if(c == nil || c->flag & Factive)
  845. continue;
  846. if(e->port == 0 || e->port == (ulong)c->reg)
  847. break;
  848. }
  849. if (i >= nctlr)
  850. return -1;
  851. c->flag |= Factive;
  852. e->ctlr = c;
  853. e->port = (uintptr)c->reg;
  854. e->irq = c->p->intl;
  855. e->tbdf = c->p->tbdf;
  856. e->mbps = 10000;
  857. e->maxmtu = c->rbsz;
  858. memmove(e->ea, c->ra, Eaddrlen);
  859. e->arg = e;
  860. e->attach = attach;
  861. e->ctl = ctl;
  862. e->ifstat = ifstat;
  863. e->interrupt = interrupt;
  864. e->multicast = multicast;
  865. e->promiscuous = promiscuous;
  866. e->shutdown = shutdown;
  867. e->transmit = transmit;
  868. return 0;
  869. }
  870. void
  871. ether82598link(void)
  872. {
  873. addethercard("i82598", pnp);
  874. }