ether82598.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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. Ether *edev;
  234. u32int *reg;
  235. u32int *reg3;
  236. uchar flag;
  237. int nrd;
  238. int ntd;
  239. int nrb;
  240. int rbsz;
  241. QLock slock;
  242. QLock alock;
  243. QLock tlock;
  244. Rendez lrendez;
  245. Rendez trendez;
  246. Rendez rrendez;
  247. uint im;
  248. uint lim;
  249. uint rim;
  250. uint tim;
  251. Lock imlock;
  252. char *alloc;
  253. Rd *rdba;
  254. Block **rb;
  255. uint rdt;
  256. uint rdfree;
  257. Td *tdba;
  258. uint tdh;
  259. uint tdt;
  260. Block **tb;
  261. uchar ra[Eaddrlen];
  262. uchar mta[128];
  263. ulong stats[nelem(stattab)];
  264. uint speeds[3];
  265. } Ctlr;
  266. /* tweakable paramaters */
  267. enum {
  268. Rbsz = 12*1024,
  269. Nrd = 256,
  270. Ntd = 256,
  271. Nrb = 256,
  272. };
  273. static Ctlr *ctlrtab[4];
  274. static int nctlr;
  275. static Lock rblock;
  276. static Block *rbpool;
  277. static void
  278. readstats(Ctlr *c)
  279. {
  280. int i;
  281. qlock(&c->slock);
  282. for(i = 0; i < nelem(c->stats); i++)
  283. c->stats[i] += c->reg[stattab[i].reg >> 2];
  284. qunlock(&c->slock);
  285. }
  286. static int speedtab[] = {
  287. 0,
  288. 1000,
  289. 10000,
  290. };
  291. static long
  292. ifstat(Ether *e, void *a, long n, ulong offset)
  293. {
  294. uint i, *t;
  295. char *s, *p, *q;
  296. Ctlr *c;
  297. c = e->ctlr;
  298. p = s = malloc(READSTR);
  299. q = p + READSTR;
  300. readstats(c);
  301. for(i = 0; i < nelem(stattab); i++)
  302. if(c->stats[i] > 0)
  303. p = seprint(p, q, "%.10s %uld\n", stattab[i].name, c->stats[i]);
  304. t = c->speeds;
  305. p = seprint(p, q, "speeds: 0:%d 1000:%d 10000:%d\n", t[0], t[1], t[2]);
  306. seprint(p, q, "rdfree %d rdh %d rdt %d\n", c->rdfree, c->reg[Rdt],
  307. c->reg[Rdh]);
  308. n = readstr(offset, a, n, s);
  309. free(s);
  310. return n;
  311. }
  312. static void
  313. im(Ctlr *c, int i)
  314. {
  315. ilock(&c->imlock);
  316. c->im |= i;
  317. c->reg[Ims] = c->im;
  318. iunlock(&c->imlock);
  319. }
  320. static int
  321. lim(void *v)
  322. {
  323. return ((Ctlr*)v)->lim != 0;
  324. }
  325. static void
  326. lproc(void *v)
  327. {
  328. int r, i;
  329. Ctlr *c;
  330. Ether *e;
  331. e = v;
  332. c = e->ctlr;
  333. for (;;) {
  334. r = c->reg[Links];
  335. e->link = (r & Lnkup) != 0;
  336. i = 0;
  337. if(e->link)
  338. i = 1 + ((r & Lnkspd) != 0);
  339. c->speeds[i]++;
  340. e->mbps = speedtab[i];
  341. c->lim = 0;
  342. im(c, Lsc);
  343. sleep(&c->lrendez, lim, c);
  344. c->lim = 0;
  345. }
  346. }
  347. static long
  348. ctl(Ether *, void *, long)
  349. {
  350. error(Ebadarg);
  351. return -1;
  352. }
  353. static Block*
  354. rballoc(void)
  355. {
  356. Block *b;
  357. ilock(&rblock);
  358. if(b = rbpool){
  359. rbpool = b->next;
  360. b->next = 0;
  361. }
  362. iunlock(&rblock);
  363. return b;
  364. }
  365. void
  366. rbfree(Block *b)
  367. {
  368. b->rp = b->wp = (uchar*)PGROUND((uintptr)b->base);
  369. ilock(&rblock);
  370. b->next = rbpool;
  371. rbpool = b;
  372. iunlock(&rblock);
  373. }
  374. #define Next(x, m) (((x)+1) & (m))
  375. static int
  376. cleanup(Ctlr *c, int tdh)
  377. {
  378. Block *b;
  379. uint m, n;
  380. m = c->ntd - 1;
  381. while(c->tdba[n = Next(tdh, m)].status & Tdd){
  382. tdh = n;
  383. b = c->tb[tdh];
  384. c->tb[tdh] = 0;
  385. freeb(b);
  386. c->tdba[tdh].status = 0;
  387. }
  388. return tdh;
  389. }
  390. void
  391. transmit(Ether *e)
  392. {
  393. uint i, m, tdt, tdh;
  394. Ctlr *c;
  395. Block *b;
  396. Td *t;
  397. c = e->ctlr;
  398. // qlock(&c->tlock);
  399. if(!canqlock(&c->tlock)){
  400. im(c, Itx0);
  401. return;
  402. }
  403. tdh = c->tdh = cleanup(c, c->tdh);
  404. tdt = c->tdt;
  405. m = c->ntd - 1;
  406. for(i = 0; i < 8; i++){
  407. if(Next(tdt, m) == tdh){
  408. im(c, Itx0);
  409. break;
  410. }
  411. if(!(b = qget(e->oq)))
  412. break;
  413. t = c->tdba + tdt;
  414. t->addr[0] = PCIWADDR(b->rp);
  415. t->length = BLEN(b);
  416. t->cmd = Rs | Ifcs | Teop;
  417. c->tb[tdt] = b;
  418. tdt = Next(tdt, m);
  419. }
  420. if(i){
  421. c->tdt = tdt;
  422. c->reg[Tdt] = tdt;
  423. }
  424. qunlock(&c->tlock);
  425. }
  426. static int
  427. tim(void *c)
  428. {
  429. return ((Ctlr*)c)->tim != 0;
  430. }
  431. static void
  432. tproc(void *v)
  433. {
  434. Ctlr *c;
  435. Ether *e;
  436. e = v;
  437. c = e->ctlr;
  438. for (;;) {
  439. sleep(&c->trendez, tim, c); /* transmit kicks us */
  440. c->tim = 0;
  441. transmit(e);
  442. }
  443. }
  444. static void
  445. rxinit(Ctlr *c)
  446. {
  447. int i;
  448. Block *b;
  449. c->reg[Rxctl] &= ~Rxen;
  450. for(i = 0; i < c->nrd; i++){
  451. b = c->rb[i];
  452. c->rb[i] = 0;
  453. if(b)
  454. freeb(b);
  455. }
  456. c->rdfree = 0;
  457. c->reg[Fctrl] |= Bam;
  458. c->reg[Rxcsum] |= Ipcs;
  459. c->reg[Srrctl] = (c->rbsz + 1023)/1024;
  460. c->reg[Mhadd] = c->rbsz << 16;
  461. c->reg[Hlreg0] |= Jumboen;
  462. c->reg[Rbal] = PCIWADDR(c->rdba);
  463. c->reg[Rbah] = 0;
  464. c->reg[Rdlen] = c->nrd*sizeof(Rd);
  465. c->reg[Rdh] = 0;
  466. c->reg[Rdt] = c->rdt = 0;
  467. c->reg[Rdrxctl] = Rdmt¼;
  468. c->reg[Rxdctl] = 8<<Wthresh | 8<<Pthresh | 4<<Hthresh | Renable;
  469. c->reg[Rxctl] |= Rxen | Dmbyps;
  470. }
  471. static void
  472. replenish(Ctlr *c, uint rdh)
  473. {
  474. int rdt, m, i;
  475. Block *b;
  476. Rd *r;
  477. m = c->nrd - 1;
  478. i = 0;
  479. for(rdt = c->rdt; Next(rdt, m) != rdh; rdt = Next(rdt, m)){
  480. r = c->rdba + rdt;
  481. if(!(b = rballoc())){
  482. print("82598: no buffers\n");
  483. break;
  484. }
  485. c->rb[rdt] = b;
  486. r->addr[0] = PCIWADDR(b->rp);
  487. r->status = 0;
  488. c->rdfree++;
  489. i++;
  490. }
  491. if(i)
  492. c->reg[Rdt] = c->rdt = rdt;
  493. }
  494. static int
  495. rim(void *v)
  496. {
  497. return ((Ctlr*)v)->rim != 0;
  498. }
  499. static uchar zeroea[Eaddrlen];
  500. void
  501. rproc(void *v)
  502. {
  503. uint m, rdh;
  504. Block *b;
  505. Ctlr *c;
  506. Ether *e;
  507. Rd *r;
  508. e = v;
  509. c = e->ctlr;
  510. m = c->nrd - 1;
  511. rdh = 0;
  512. loop:
  513. replenish(c, rdh);
  514. im(c, Irx0);
  515. sleep(&c->rrendez, rim, c);
  516. loop1:
  517. c->rim = 0;
  518. if(c->nrd - c->rdfree >= 16)
  519. replenish(c, rdh);
  520. r = c->rdba + rdh;
  521. if(!(r->status & Rdd))
  522. goto loop; /* UGH */
  523. b = c->rb[rdh];
  524. c->rb[rdh] = 0;
  525. b->wp += r->length;
  526. b->lim = b->wp; /* lie like a dog */
  527. if(!(r->status & Ixsm)){
  528. if(r->status & Ipcs)
  529. b->flag |= Bipck;
  530. if(r->status & Tcpcs)
  531. b->flag |= Btcpck | Budpck;
  532. b->checksum = r->cksum;
  533. }
  534. // r->status = 0;
  535. etheriq(e, b, 1);
  536. c->rdfree--;
  537. rdh = Next(rdh, m);
  538. goto loop1; /* UGH */
  539. }
  540. static void
  541. promiscuous(void *a, int on)
  542. {
  543. Ctlr *c;
  544. Ether *e;
  545. e = a;
  546. c = e->ctlr;
  547. if(on)
  548. c->reg[Fctrl] |= Upe | Mpe;
  549. else
  550. c->reg[Fctrl] &= ~(Upe | Mpe);
  551. }
  552. static void
  553. multicast(void *a, uchar *ea, int on)
  554. {
  555. int b, i;
  556. Ctlr *c;
  557. Ether *e;
  558. e = a;
  559. c = e->ctlr;
  560. /*
  561. * multiple ether addresses can hash to the same filter bit,
  562. * so it's never safe to clear a filter bit.
  563. * if we want to clear filter bits, we need to keep track of
  564. * all the multicast addresses in use, clear all the filter bits,
  565. * then set the ones corresponding to in-use addresses.
  566. */
  567. i = ea[5] >> 1;
  568. b = (ea[5]&1)<<4 | ea[4]>>4;
  569. b = 1 << b;
  570. if(on)
  571. c->mta[i] |= b;
  572. // else
  573. // c->mta[i] &= ~b;
  574. c->reg[Mta+i] = c->mta[i];
  575. }
  576. static int
  577. detach(Ctlr *c)
  578. {
  579. int i;
  580. c->reg[Imc] = ~0;
  581. c->reg[Ctrl] |= Rst;
  582. for(i = 0; i < 100; i++){
  583. delay(1);
  584. if((c->reg[Ctrl] & Rst) == 0)
  585. break;
  586. }
  587. if (i >= 100)
  588. return -1;
  589. /* errata */
  590. delay(50);
  591. c->reg[Ecc] &= ~(1<<21 | 1<<18 | 1<<9 | 1<<6);
  592. /* not cleared by reset; kill it manually. */
  593. for(i = 1; i < 16; i++)
  594. c->reg[Rah] &= ~(1 << 31);
  595. for(i = 0; i < 128; i++)
  596. c->reg[Mta + i] = 0;
  597. for(i = 1; i < 640; i++)
  598. c->reg[Vfta + i] = 0;
  599. return 0;
  600. }
  601. static void
  602. shutdown(Ether *e)
  603. {
  604. detach(e->ctlr);
  605. }
  606. /* ≤ 20ms */
  607. static ushort
  608. eeread(Ctlr *c, int i)
  609. {
  610. c->reg[Eerd] = EEstart | i<<2;
  611. while((c->reg[Eerd] & EEdone) == 0)
  612. ;
  613. return c->reg[Eerd] >> 16;
  614. }
  615. static int
  616. eeload(Ctlr *c)
  617. {
  618. ushort u, v, p, l, i, j;
  619. if((eeread(c, 0) & 0xc0) != 0x40)
  620. return -1;
  621. u = 0;
  622. for(i = 0; i < 0x40; i++)
  623. u += eeread(c, i);
  624. for(i = 3; i < 0xf; i++){
  625. p = eeread(c, i);
  626. l = eeread(c, p++);
  627. if((int)p + l + 1 > 0xffff)
  628. continue;
  629. for(j = p; j < p + l; j++)
  630. u += eeread(c, j);
  631. }
  632. if(u != 0xbaba)
  633. return -1;
  634. if(c->reg[Status] & (1<<3))
  635. u = eeread(c, 10);
  636. else
  637. u = eeread(c, 9);
  638. u++;
  639. for(i = 0; i < Eaddrlen;){
  640. v = eeread(c, u + i/2);
  641. c->ra[i++] = v;
  642. c->ra[i++] = v>>8;
  643. }
  644. c->ra[5] += (c->reg[Status] & 0xc) >> 2;
  645. return 0;
  646. }
  647. static int
  648. reset(Ctlr *c)
  649. {
  650. int i;
  651. uchar *p;
  652. if(detach(c)){
  653. print("82598: reset timeout\n");
  654. return -1;
  655. }
  656. if(eeload(c)){
  657. print("82598: eeprom failure\n");
  658. return -1;
  659. }
  660. p = c->ra;
  661. c->reg[Ral] = p[3]<<24 | p[2]<<16 | p[1]<<8 | p[0];
  662. c->reg[Rah] = p[5]<<8 | p[4] | 1<<31;
  663. readstats(c);
  664. for(i = 0; i<nelem(c->stats); i++)
  665. c->stats[i] = 0;
  666. c->reg[Ctrlext] |= 1 << 16;
  667. /* make some guesses for flow control */
  668. c->reg[Fcrtl] = 0x10000 | 1<<31;
  669. c->reg[Fcrth] = 0x40000 | 1<<31;
  670. c->reg[Rcrtv] = 0x6000;
  671. /* configure interrupt mapping (don't ask) */
  672. c->reg[Ivar+0] = 0 | 1<<7;
  673. c->reg[Ivar+64/4] = 1 | 1<<7;
  674. // c->reg[Ivar+97/4] = (2 | 1<<7) << (8*(97%4));
  675. /* interrupt throttling goes here. */
  676. for(i = Itr; i < Itr + 20; i++)
  677. c->reg[i] = 128; /* ¼µs intervals */
  678. c->reg[Itr + Itx0] = 256;
  679. return 0;
  680. }
  681. static void
  682. txinit(Ctlr *c)
  683. {
  684. Block *b;
  685. int i;
  686. c->reg[Txdctl] = 16<<Wthresh | 16<<Pthresh;
  687. for(i = 0; i < c->ntd; i++){
  688. b = c->tb[i];
  689. c->tb[i] = 0;
  690. if(b)
  691. freeb(b);
  692. }
  693. memset(c->tdba, 0, c->ntd * sizeof(Td));
  694. c->reg[Tdbal] = PCIWADDR(c->tdba);
  695. c->reg[Tdbah] = 0;
  696. c->reg[Tdlen] = c->ntd*sizeof(Td);
  697. c->reg[Tdh] = 0;
  698. c->reg[Tdt] = 0;
  699. c->tdh = c->ntd - 1;
  700. c->tdt = 0;
  701. c->reg[Txdctl] |= Ten;
  702. }
  703. static void
  704. attach(Ether *e)
  705. {
  706. Block *b;
  707. Ctlr *c;
  708. int t;
  709. char buf[KNAMELEN];
  710. c = e->ctlr;
  711. c->edev = e; /* point back to Ether* */
  712. qlock(&c->alock);
  713. if(c->alloc){
  714. qunlock(&c->alock);
  715. return;
  716. }
  717. c->nrd = Nrd;
  718. c->ntd = Ntd;
  719. t = c->nrd * sizeof *c->rdba + 255;
  720. t += c->ntd * sizeof *c->tdba + 255;
  721. t += (c->ntd + c->nrd) * sizeof(Block*);
  722. c->alloc = malloc(t);
  723. qunlock(&c->alock);
  724. if(c->alloc == nil)
  725. error(Enomem);
  726. c->rdba = (Rd*)ROUNDUP((uintptr)c->alloc, 256);
  727. c->tdba = (Td*)ROUNDUP((uintptr)(c->rdba + c->nrd), 256);
  728. c->rb = (Block**)(c->tdba + c->ntd);
  729. c->tb = (Block**)(c->rb + c->nrd);
  730. if(waserror()){
  731. while(b = rballoc()){
  732. b->free = 0;
  733. freeb(b);
  734. }
  735. free(c->alloc);
  736. c->alloc = nil;
  737. nexterror();
  738. }
  739. for(c->nrb = 0; c->nrb < 2*Nrb; c->nrb++){
  740. if(!(b = allocb(c->rbsz+BY2PG)))
  741. error(Enomem);
  742. b->free = rbfree;
  743. freeb(b);
  744. }
  745. poperror();
  746. rxinit(c);
  747. txinit(c);
  748. sprint(buf, "#l%dl", e->ctlrno);
  749. kproc(buf, lproc, e);
  750. sprint(buf, "#l%dr", e->ctlrno);
  751. kproc(buf, rproc, e);
  752. sprint(buf, "#l%dt", e->ctlrno);
  753. kproc(buf, tproc, e);
  754. }
  755. static void
  756. interrupt(Ureg*, void *v)
  757. {
  758. int icr, im;
  759. Ctlr *c;
  760. Ether *e;
  761. e = v;
  762. c = e->ctlr;
  763. ilock(&c->imlock);
  764. c->reg[Imc] = ~0;
  765. im = c->im;
  766. while((icr = c->reg[Icr] & c->im) != 0){
  767. if(icr & Lsc){
  768. im &= ~Lsc;
  769. c->lim = icr & Lsc;
  770. wakeup(&c->lrendez);
  771. }
  772. if(icr & Irx0){
  773. im &= ~Irx0;
  774. c->rim = icr & Irx0;
  775. wakeup(&c->rrendez);
  776. }
  777. if(icr & Itx0){
  778. im &= ~Itx0;
  779. c->tim = icr & Itx0;
  780. wakeup(&c->trendez);
  781. }
  782. }
  783. c->reg[Ims] = c->im = im;
  784. iunlock(&c->imlock);
  785. }
  786. static void
  787. scan(void)
  788. {
  789. ulong io, io3;
  790. void *mem, *mem3;
  791. Ctlr *c;
  792. Pcidev *p;
  793. p = 0;
  794. while(p = pcimatch(p, 0x8086, 0)){
  795. switch(p->did){
  796. case 0x10c6: /* 82598 af dual port */
  797. case 0x10c7: /* 82598 af single port */
  798. case 0x10b6: /* 82598 backplane */
  799. case 0x10dd: /* 82598 at cx4 */
  800. break;
  801. default:
  802. continue;
  803. }
  804. if(nctlr == nelem(ctlrtab)){
  805. print("i82598: too many controllers\n");
  806. return;
  807. }
  808. io = p->mem[0].bar & ~0xf;
  809. mem = vmap(io, p->mem[0].size);
  810. if(mem == nil){
  811. print("i82598: can't map %#p\n", p->mem[0].bar);
  812. continue;
  813. }
  814. io3 = p->mem[3].bar & ~0xf;
  815. mem3 = vmap(io3, p->mem[3].size);
  816. if(mem3 == nil){
  817. print("i82598: can't map %#p\n", p->mem[3].bar);
  818. vunmap(mem, p->mem[0].size);
  819. continue;
  820. }
  821. c = malloc(sizeof *c);
  822. c->p = p;
  823. c->reg = (u32int*)mem;
  824. c->reg3 = (u32int*)mem3;
  825. c->rbsz = Rbsz;
  826. if(reset(c)){
  827. print("i82598: can't reset\n");
  828. free(c);
  829. vunmap(mem, p->mem[0].size);
  830. vunmap(mem3, p->mem[3].size);
  831. continue;
  832. }
  833. pcisetbme(p);
  834. ctlrtab[nctlr++] = c;
  835. }
  836. }
  837. static int
  838. pnp(Ether *e)
  839. {
  840. int i;
  841. Ctlr *c = nil;
  842. if(nctlr == 0)
  843. scan();
  844. for(i = 0; i < nctlr; i++){
  845. c = ctlrtab[i];
  846. if(c == nil || c->flag & Factive)
  847. continue;
  848. if(e->port == 0 || e->port == (ulong)c->reg)
  849. break;
  850. }
  851. if (i >= nctlr)
  852. return -1;
  853. c->flag |= Factive;
  854. e->ctlr = c;
  855. e->port = (uintptr)c->reg;
  856. e->irq = c->p->intl;
  857. e->tbdf = c->p->tbdf;
  858. e->mbps = 10000;
  859. e->maxmtu = c->rbsz;
  860. memmove(e->ea, c->ra, Eaddrlen);
  861. e->arg = e;
  862. e->attach = attach;
  863. e->ctl = ctl;
  864. e->ifstat = ifstat;
  865. e->interrupt = interrupt;
  866. e->multicast = multicast;
  867. e->promiscuous = promiscuous;
  868. e->shutdown = shutdown;
  869. e->transmit = transmit;
  870. return 0;
  871. }
  872. void
  873. ether82598link(void)
  874. {
  875. addethercard("i82598", pnp);
  876. }