ether82598.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. /*
  2. * intel pci-express 10Gb ethernet driver for 8259[89]
  3. * copyright © 2007, coraid, inc.
  4. * depessimised and made to work on the 82599 at bell labs, 2013.
  5. *
  6. * 82599 requests should ideally not cross a 4KB (page) boundary.
  7. */
  8. #include "u.h"
  9. #include "../port/lib.h"
  10. #include "mem.h"
  11. #include "dat.h"
  12. #include "fns.h"
  13. #include "io.h"
  14. #include "../port/error.h"
  15. #include "../port/netif.h"
  16. #include "etherif.h"
  17. #define NEXTPOW2(x, m) (((x)+1) & (m))
  18. enum {
  19. Rbsz = ETHERMAXTU+32, /* +slop is for vlan headers, crcs, etc. */
  20. Descalign= 128, /* 599 manual needs 128-byte alignment */
  21. /* tunable parameters */
  22. Goslow = 0, /* flag: go slow by throttling intrs, etc. */
  23. /* were 256, 1024 & 64, but 30, 47 and 1 are ample. */
  24. Nrd = 64, /* multiple of 8, power of 2 for NEXTPOW2 */
  25. Nrb = 128,
  26. Ntd = 32, /* multiple of 8, power of 2 for NEXTPOW2 */
  27. };
  28. enum {
  29. /* general */
  30. Ctrl = 0x00000/4, /* Device Control */
  31. Status = 0x00008/4, /* Device Status */
  32. Ctrlext = 0x00018/4, /* Extended Device Control */
  33. Esdp = 0x00020/4, /* extended sdp control */
  34. Esodp = 0x00028/4, /* extended od sdp control (i2cctl on 599) */
  35. Ledctl = 0x00200/4, /* led control */
  36. Tcptimer = 0x0004c/4, /* tcp timer */
  37. Ecc = 0x110b0/4, /* errata ecc control magic (pcie intr cause on 599) */
  38. /* nvm */
  39. Eec = 0x10010/4, /* eeprom/flash control */
  40. Eerd = 0x10014/4, /* eeprom read */
  41. Fla = 0x1001c/4, /* flash access */
  42. Flop = 0x1013c/4, /* flash opcode */
  43. Grc = 0x10200/4, /* general rx control */
  44. /* interrupt */
  45. Icr = 0x00800/4, /* interrupt cause read */
  46. Ics = 0x00808/4, /* " set */
  47. Ims = 0x00880/4, /* " mask read/set (actually enable) */
  48. Imc = 0x00888/4, /* " mask clear */
  49. Iac = 0x00810/4, /* " auto clear */
  50. Iam = 0x00890/4, /* " auto mask enable */
  51. Itr = 0x00820/4, /* " throttling rate regs (0-19) */
  52. Ivar = 0x00900/4, /* " vector allocation regs. */
  53. /* msi interrupt */
  54. Msixt = 0x0000/4, /* msix table (bar3) */
  55. Msipba = 0x2000/4, /* msix pending bit array (bar3) */
  56. Pbacl = 0x11068/4, /* pba clear */
  57. Gpie = 0x00898/4, /* general purpose int enable */
  58. /* flow control */
  59. Pfctop = 0x03008/4, /* priority flow ctl type opcode */
  60. Fcttv = 0x03200/4, /* " transmit timer value (0-3) */
  61. Fcrtl = 0x03220/4, /* " rx threshold low (0-7) +8n */
  62. Fcrth = 0x03260/4, /* " rx threshold high (0-7) +8n */
  63. Rcrtv = 0x032a0/4, /* " refresh value threshold */
  64. Tfcs = 0x0ce00/4, /* " tx status */
  65. /* rx dma */
  66. Rbal = 0x01000/4, /* rx desc base low (0-63) +0x40n */
  67. Rbah = 0x01004/4, /* " high */
  68. Rdlen = 0x01008/4, /* " length */
  69. Rdh = 0x01010/4, /* " head */
  70. Rdt = 0x01018/4, /* " tail */
  71. Rxdctl = 0x01028/4, /* " control */
  72. Srrctl = 0x02100/4, /* split & replication rx ctl. array */
  73. Dcarxctl = 0x02200/4, /* rx dca control */
  74. Rdrxctl = 0x02f00/4, /* rx dma control */
  75. Rxpbsize = 0x03c00/4, /* rx packet buffer size */
  76. Rxctl = 0x03000/4, /* rx control */
  77. Dropen = 0x03d04/4, /* drop enable control (598 only) */
  78. /* rx */
  79. Rxcsum = 0x05000/4, /* rx checksum control */
  80. Rfctl = 0x05008/4, /* rx filter control */
  81. Mta = 0x05200/4, /* multicast table array (0-127) */
  82. Ral98 = 0x05400/4, /* rx address low (598) */
  83. Rah98 = 0x05404/4,
  84. Ral99 = 0x0a200/4, /* rx address low array (599) */
  85. Rah99 = 0x0a204/4,
  86. Psrtype = 0x05480/4, /* packet split rx type. */
  87. Vfta = 0x0a000/4, /* vlan filter table array. */
  88. Fctrl = 0x05080/4, /* filter control */
  89. Vlnctrl = 0x05088/4, /* vlan control */
  90. Msctctrl = 0x05090/4, /* multicast control */
  91. Mrqc = 0x05818/4, /* multiple rx queues cmd */
  92. Vmdctl = 0x0581c/4, /* vmdq control (598 only) */
  93. Imir = 0x05a80/4, /* immediate irq rx (0-7) (598 only) */
  94. Imirext = 0x05aa0/4, /* immediate irq rx ext (598 only) */
  95. Imirvp = 0x05ac0/4, /* immediate irq vlan priority (598 only) */
  96. Reta = 0x05c00/4, /* redirection table */
  97. Rssrk = 0x05c80/4, /* rss random key */
  98. /* tx */
  99. Tdbal = 0x06000/4, /* tx desc base low +0x40n array */
  100. Tdbah = 0x06004/4, /* " high */
  101. Tdlen = 0x06008/4, /* " len */
  102. Tdh = 0x06010/4, /* " head */
  103. Tdt = 0x06018/4, /* " tail */
  104. Txdctl = 0x06028/4, /* " control */
  105. Tdwbal = 0x06038/4, /* " write-back address low */
  106. Tdwbah = 0x0603c/4,
  107. Dtxctl98 = 0x07e00/4, /* tx dma control (598 only) */
  108. Dtxctl99 = 0x04a80/4, /* tx dma control (599 only) */
  109. Tdcatxctrl98 = 0x07200/4, /* tx dca register (0-15) (598 only) */
  110. Tdcatxctrl99 = 0x0600c/4, /* tx dca register (0-127) (599 only) */
  111. Tipg = 0x0cb00/4, /* tx inter-packet gap (598 only) */
  112. Txpbsize = 0x0cc00/4, /* tx packet-buffer size (0-15) */
  113. /* mac */
  114. Hlreg0 = 0x04240/4, /* highlander control reg 0 */
  115. Hlreg1 = 0x04244/4, /* highlander control reg 1 (ro) */
  116. Msca = 0x0425c/4, /* mdi signal cmd & addr */
  117. Msrwd = 0x04260/4, /* mdi single rw data */
  118. Mhadd = 0x04268/4, /* mac addr high & max frame */
  119. Pcss1 = 0x04288/4, /* xgxs status 1 */
  120. Pcss2 = 0x0428c/4,
  121. Xpcss = 0x04290/4, /* 10gb-x pcs status */
  122. Serdesc = 0x04298/4, /* serdes control */
  123. Macs = 0x0429c/4, /* fifo control & report */
  124. Autoc = 0x042a0/4, /* autodetect control & status */
  125. Links = 0x042a4/4, /* link status */
  126. Links2 = 0x04324/4, /* 599 only */
  127. Autoc2 = 0x042a8/4,
  128. };
  129. enum {
  130. Factive = 1<<0,
  131. Enable = 1<<31,
  132. /* Ctrl */
  133. Rst = 1<<26, /* full nic reset */
  134. /* Txdctl */
  135. Ten = 1<<25,
  136. /* Dtxctl99 */
  137. Te = 1<<0, /* dma tx enable */
  138. /* Fctrl */
  139. Bam = 1<<10, /* broadcast accept mode */
  140. Upe = 1<<9, /* unicast promiscuous */
  141. Mpe = 1<<8, /* multicast promiscuous */
  142. /* Rxdctl */
  143. Pthresh = 0, /* prefresh threshold shift in bits */
  144. Hthresh = 8, /* host buffer minimum threshold " */
  145. Wthresh = 16, /* writeback threshold */
  146. Renable = 1<<25,
  147. /* Rxctl */
  148. Rxen = 1<<0,
  149. Dmbyps = 1<<1, /* descr. monitor bypass (598 only) */
  150. /* Rdrxctl */
  151. Rdmt½ = 0, /* 598 */
  152. Rdmt¼ = 1, /* 598 */
  153. Rdmt⅛ = 2, /* 598 */
  154. Crcstrip = 1<<1, /* 599 */
  155. Rscfrstsize = 037<<17, /* 599; should be zero */
  156. /* Rxcsum */
  157. Ippcse = 1<<12, /* ip payload checksum enable */
  158. /* Eerd */
  159. EEstart = 1<<0, /* Start Read */
  160. EEdone = 1<<1, /* Read done */
  161. /* interrupts */
  162. Irx0 = 1<<0, /* driver defined */
  163. Itx0 = 1<<1, /* driver defined */
  164. Lsc = 1<<20, /* link status change */
  165. /* Links */
  166. Lnkup = 1<<30,
  167. Lnkspd = 1<<29,
  168. /* Hlreg0 */
  169. Txcrcen = 1<<0, /* add crc during xmit */
  170. Rxcrcstrip = 1<<1, /* strip crc during recv */
  171. Jumboen = 1<<2,
  172. Txpaden = 1<<10, /* pad short frames during xmit */
  173. /* Autoc */
  174. Flu = 1<<0, /* force link up */
  175. Lmsshift = 13, /* link mode select shift */
  176. Lmsmask = 7,
  177. };
  178. typedef struct Ctlr Ctlr;
  179. typedef struct Rd Rd;
  180. typedef struct Td Td;
  181. typedef struct {
  182. uint reg;
  183. char *name;
  184. } Stat;
  185. Stat stattab[] = {
  186. 0x4000, "crc error",
  187. 0x4004, "illegal byte",
  188. 0x4008, "short packet",
  189. 0x3fa0, "missed pkt0",
  190. 0x4034, "mac local flt",
  191. 0x4038, "mac rmt flt",
  192. 0x4040, "rx length err",
  193. 0x3f60, "xon tx",
  194. 0xcf60, "xon rx",
  195. 0x3f68, "xoff tx",
  196. 0xcf68, "xoff rx",
  197. 0x405c, "rx 040",
  198. 0x4060, "rx 07f",
  199. 0x4064, "rx 100",
  200. 0x4068, "rx 200",
  201. 0x406c, "rx 3ff",
  202. 0x4070, "rx big",
  203. 0x4074, "rx ok",
  204. 0x4078, "rx bcast",
  205. 0x3fc0, "rx no buf0",
  206. 0x40a4, "rx runt",
  207. 0x40a8, "rx frag",
  208. 0x40ac, "rx ovrsz",
  209. 0x40b0, "rx jab",
  210. 0x40d0, "rx pkt",
  211. 0x40d4, "tx pkt",
  212. 0x40d8, "tx 040",
  213. 0x40dc, "tx 07f",
  214. 0x40e0, "tx 100",
  215. 0x40e4, "tx 200",
  216. 0x40e8, "tx 3ff",
  217. 0x40ec, "tx big",
  218. 0x40f4, "tx bcast",
  219. 0x4120, "xsum err",
  220. };
  221. /* status */
  222. enum {
  223. Pif = 1<<7, /* past exact filter (sic) */
  224. Ipcs = 1<<6, /* ip checksum calculated */
  225. L4cs = 1<<5, /* layer 2 */
  226. Tcpcs = 1<<4, /* tcp checksum calculated */
  227. Vp = 1<<3, /* 802.1q packet matched vet */
  228. Ixsm = 1<<2, /* ignore checksum */
  229. Reop = 1<<1, /* end of packet */
  230. Rdd = 1<<0, /* descriptor done */
  231. };
  232. struct Rd { /* Receive Descriptor */
  233. u32int addr[2];
  234. ushort length;
  235. ushort cksum;
  236. uchar status;
  237. uchar errors;
  238. ushort vlan;
  239. };
  240. enum {
  241. /* Td cmd */
  242. Rs = 1<<3, /* report status */
  243. Ic = 1<<2, /* insert checksum */
  244. Ifcs = 1<<1, /* insert FCS (ethernet crc) */
  245. Teop = 1<<0, /* end of packet */
  246. /* Td status */
  247. Tdd = 1<<0, /* descriptor done */
  248. };
  249. struct Td { /* Transmit Descriptor */
  250. u32int addr[2];
  251. ushort length;
  252. uchar cso;
  253. uchar cmd;
  254. uchar status;
  255. uchar css;
  256. ushort vlan;
  257. };
  258. struct Ctlr {
  259. Pcidev *p;
  260. Ether *edev;
  261. int type;
  262. /* virtual */
  263. u32int *reg;
  264. u32int *msix; /* unused */
  265. /* physical */
  266. u32int *physreg;
  267. u32int *physmsix; /* unused */
  268. uchar flag;
  269. int nrd;
  270. int ntd;
  271. int nrb; /* # bufs this Ctlr has in the pool */
  272. uint rbsz;
  273. int procsrunning;
  274. int attached;
  275. Watermark wmrb;
  276. Watermark wmrd;
  277. Watermark wmtd;
  278. QLock slock;
  279. QLock alock; /* attach lock */
  280. QLock tlock;
  281. Rendez lrendez;
  282. Rendez trendez;
  283. Rendez rrendez;
  284. uint im; /* interrupt mask */
  285. uint lim;
  286. uint rim;
  287. uint tim;
  288. Lock imlock;
  289. Rd* rdba; /* receive descriptor base address */
  290. Block** rb; /* receive buffers */
  291. int rdt; /* receive descriptor tail */
  292. int rdfree; /* rx descriptors awaiting packets */
  293. Td* tdba; /* transmit descriptor base address */
  294. int tdh; /* transmit descriptor head */
  295. int tdt; /* transmit descriptor tail */
  296. Block** tb; /* transmit buffers */
  297. uchar ra[Eaddrlen]; /* receive address */
  298. uchar mta[128]; /* multicast table array */
  299. ulong stats[nelem(stattab)];
  300. uint speeds[3];
  301. };
  302. enum {
  303. I82598 = 1,
  304. I82599,
  305. };
  306. static Ctlr *ctlrtab[4];
  307. static int nctlr;
  308. static Lock rblock;
  309. static Block *rbpool;
  310. static int nrbfull; /* # of rcv Blocks with data awaiting processing */
  311. static void
  312. readstats(Ctlr *ctlr)
  313. {
  314. int i;
  315. qlock(&ctlr->slock);
  316. for(i = 0; i < nelem(ctlr->stats); i++)
  317. ctlr->stats[i] += ctlr->reg[stattab[i].reg >> 2];
  318. qunlock(&ctlr->slock);
  319. }
  320. static int speedtab[] = {
  321. 0,
  322. 1000,
  323. 10000,
  324. };
  325. static long
  326. ifstat(Ether *edev, void *a, long n, ulong offset)
  327. {
  328. uint i, *t;
  329. char *s, *p, *e;
  330. Ctlr *ctlr;
  331. ctlr = edev->ctlr;
  332. p = s = malloc(READSTR);
  333. if(p == nil)
  334. error(Enomem);
  335. e = p + READSTR;
  336. readstats(ctlr);
  337. for(i = 0; i < nelem(stattab); i++)
  338. if(ctlr->stats[i] > 0)
  339. p = seprint(p, e, "%.10s %uld\n", stattab[i].name,
  340. ctlr->stats[i]);
  341. t = ctlr->speeds;
  342. p = seprint(p, e, "speeds: 0:%d 1000:%d 10000:%d\n", t[0], t[1], t[2]);
  343. p = seprint(p, e, "mtu: min:%d max:%d\n", edev->minmtu, edev->maxmtu);
  344. p = seprint(p, e, "rdfree %d rdh %d rdt %d\n", ctlr->rdfree, ctlr->reg[Rdt],
  345. ctlr->reg[Rdh]);
  346. p = seprintmark(p, e, &ctlr->wmrb);
  347. p = seprintmark(p, e, &ctlr->wmrd);
  348. p = seprintmark(p, e, &ctlr->wmtd);
  349. USED(p);
  350. n = readstr(offset, a, n, s);
  351. free(s);
  352. return n;
  353. }
  354. static void
  355. ienable(Ctlr *ctlr, int i)
  356. {
  357. ilock(&ctlr->imlock);
  358. ctlr->im |= i;
  359. ctlr->reg[Ims] = ctlr->im;
  360. iunlock(&ctlr->imlock);
  361. }
  362. static int
  363. lim(void *v)
  364. {
  365. return ((Ctlr*)v)->lim != 0;
  366. }
  367. static void
  368. lproc(void *v)
  369. {
  370. int r, i;
  371. Ctlr *ctlr;
  372. Ether *e;
  373. e = v;
  374. ctlr = e->ctlr;
  375. for (;;) {
  376. r = ctlr->reg[Links];
  377. e->link = (r & Lnkup) != 0;
  378. i = 0;
  379. if(e->link)
  380. i = 1 + ((r & Lnkspd) != 0);
  381. ctlr->speeds[i]++;
  382. e->mbps = speedtab[i];
  383. ctlr->lim = 0;
  384. ienable(ctlr, Lsc);
  385. sleep(&ctlr->lrendez, lim, ctlr);
  386. ctlr->lim = 0;
  387. }
  388. }
  389. static long
  390. ctl(Ether *, void *, long)
  391. {
  392. error(Ebadarg);
  393. return -1;
  394. }
  395. static Block*
  396. rballoc(void)
  397. {
  398. Block *bp;
  399. ilock(&rblock);
  400. if((bp = rbpool) != nil){
  401. rbpool = bp->next;
  402. bp->next = 0;
  403. _xinc(&bp->ref); /* prevent bp from being freed */
  404. }
  405. iunlock(&rblock);
  406. return bp;
  407. }
  408. void
  409. rbfree(Block *b)
  410. {
  411. b->rp = b->wp = (uchar*)PGROUND((uintptr)b->base);
  412. b->flag &= ~(Bipck | Budpck | Btcpck | Bpktck);
  413. ilock(&rblock);
  414. b->next = rbpool;
  415. rbpool = b;
  416. nrbfull--;
  417. iunlock(&rblock);
  418. }
  419. static int
  420. cleanup(Ctlr *ctlr, int tdh)
  421. {
  422. Block *b;
  423. uint m, n;
  424. m = ctlr->ntd - 1;
  425. while(ctlr->tdba[n = NEXTPOW2(tdh, m)].status & Tdd){
  426. tdh = n;
  427. b = ctlr->tb[tdh];
  428. ctlr->tb[tdh] = 0;
  429. if (b)
  430. freeb(b);
  431. ctlr->tdba[tdh].status = 0;
  432. }
  433. return tdh;
  434. }
  435. void
  436. transmit(Ether *e)
  437. {
  438. uint i, m, tdt, tdh;
  439. Ctlr *ctlr;
  440. Block *b;
  441. Td *t;
  442. ctlr = e->ctlr;
  443. if(!canqlock(&ctlr->tlock)){
  444. ienable(ctlr, Itx0);
  445. return;
  446. }
  447. tdh = ctlr->tdh = cleanup(ctlr, ctlr->tdh);
  448. tdt = ctlr->tdt;
  449. m = ctlr->ntd - 1;
  450. for(i = 0; ; i++){
  451. if(NEXTPOW2(tdt, m) == tdh){ /* ring full? */
  452. ienable(ctlr, Itx0);
  453. break;
  454. }
  455. if((b = qget(e->oq)) == nil)
  456. break;
  457. assert(ctlr->tdba != nil);
  458. t = ctlr->tdba + tdt;
  459. t->addr[0] = PCIWADDR(b->rp);
  460. t->length = BLEN(b);
  461. t->cmd = Ifcs | Teop;
  462. if (!Goslow)
  463. t->cmd |= Rs;
  464. ctlr->tb[tdt] = b;
  465. /* note size of queue of tds awaiting transmission */
  466. notemark(&ctlr->wmtd, (tdt + Ntd - tdh) % Ntd);
  467. tdt = NEXTPOW2(tdt, m);
  468. }
  469. if(i) {
  470. coherence();
  471. ctlr->reg[Tdt] = ctlr->tdt = tdt; /* make new Tds active */
  472. coherence();
  473. ienable(ctlr, Itx0);
  474. }
  475. qunlock(&ctlr->tlock);
  476. }
  477. static int
  478. tim(void *c)
  479. {
  480. return ((Ctlr*)c)->tim != 0;
  481. }
  482. static void
  483. tproc(void *v)
  484. {
  485. Ctlr *ctlr;
  486. Ether *e;
  487. e = v;
  488. ctlr = e->ctlr;
  489. for (;;) {
  490. sleep(&ctlr->trendez, tim, ctlr); /* xmit interrupt kicks us */
  491. ctlr->tim = 0;
  492. transmit(e);
  493. }
  494. }
  495. static void
  496. rxinit(Ctlr *ctlr)
  497. {
  498. int i, is598, autoc;
  499. ulong until;
  500. Block *b;
  501. ctlr->reg[Rxctl] &= ~Rxen;
  502. ctlr->reg[Rxdctl] = 0;
  503. for(i = 0; i < ctlr->nrd; i++){
  504. b = ctlr->rb[i];
  505. ctlr->rb[i] = 0;
  506. if(b)
  507. freeb(b);
  508. }
  509. ctlr->rdfree = 0;
  510. coherence();
  511. ctlr->reg[Fctrl] |= Bam;
  512. ctlr->reg[Fctrl] &= ~(Upe | Mpe);
  513. /* intel gets some csums wrong (e.g., errata 44) */
  514. ctlr->reg[Rxcsum] &= ~Ippcse;
  515. ctlr->reg[Hlreg0] &= ~Jumboen; /* jumbos are a bad idea */
  516. ctlr->reg[Hlreg0] |= Txcrcen | Rxcrcstrip | Txpaden;
  517. ctlr->reg[Srrctl] = (ctlr->rbsz + 1024 - 1) / 1024;
  518. ctlr->reg[Mhadd] = ctlr->rbsz << 16;
  519. ctlr->reg[Rbal] = PCIWADDR(ctlr->rdba);
  520. ctlr->reg[Rbah] = 0;
  521. ctlr->reg[Rdlen] = ctlr->nrd*sizeof(Rd); /* must be multiple of 128 */
  522. ctlr->reg[Rdh] = 0;
  523. ctlr->reg[Rdt] = ctlr->rdt = 0;
  524. coherence();
  525. is598 = (ctlr->type == I82598);
  526. if (is598)
  527. ctlr->reg[Rdrxctl] = Rdmt¼;
  528. else {
  529. ctlr->reg[Rdrxctl] |= Crcstrip;
  530. ctlr->reg[Rdrxctl] &= ~Rscfrstsize;
  531. }
  532. if (Goslow && is598)
  533. ctlr->reg[Rxdctl] = 8<<Wthresh | 8<<Pthresh | 4<<Hthresh | Renable;
  534. else
  535. ctlr->reg[Rxdctl] = Renable;
  536. coherence();
  537. /*
  538. * don't wait forever like an idiot (and hang the system),
  539. * maybe it's disconnected.
  540. */
  541. until = TK2MS(MACHP(0)->ticks) + 250;
  542. while (!(ctlr->reg[Rxdctl] & Renable) && TK2MS(MACHP(0)->ticks) < until)
  543. ;
  544. if(!(ctlr->reg[Rxdctl] & Renable))
  545. print("#l%d: Renable didn't come on, might be disconnected\n",
  546. ctlr->edev->ctlrno);
  547. ctlr->reg[Rxctl] |= Rxen | (is598? Dmbyps: 0);
  548. if (is598){
  549. autoc = ctlr->reg[Autoc];
  550. /* what is this rubbish and why do we care? */
  551. print("#l%d: autoc %#ux; lms %d (3 is 10g sfp)\n",
  552. ctlr->edev->ctlrno, autoc, (autoc>>Lmsshift) & Lmsmask);
  553. ctlr->reg[Autoc] |= Flu;
  554. coherence();
  555. delay(50);
  556. }
  557. }
  558. static void
  559. replenish(Ctlr *ctlr, uint rdh)
  560. {
  561. int rdt, m, i;
  562. Block *b;
  563. Rd *r;
  564. m = ctlr->nrd - 1;
  565. i = 0;
  566. for(rdt = ctlr->rdt; NEXTPOW2(rdt, m) != rdh; rdt = NEXTPOW2(rdt, m)){
  567. r = ctlr->rdba + rdt;
  568. if((b = rballoc()) == nil){
  569. print("#l%d: no buffers\n", ctlr->edev->ctlrno);
  570. break;
  571. }
  572. ctlr->rb[rdt] = b;
  573. r->addr[0] = PCIWADDR(b->rp);
  574. r->status = 0;
  575. ctlr->rdfree++;
  576. i++;
  577. }
  578. if(i) {
  579. coherence();
  580. ctlr->reg[Rdt] = ctlr->rdt = rdt; /* hand back recycled rdescs */
  581. coherence();
  582. }
  583. }
  584. static int
  585. rim(void *v)
  586. {
  587. return ((Ctlr*)v)->rim != 0;
  588. }
  589. void
  590. rproc(void *v)
  591. {
  592. int passed;
  593. uint m, rdh;
  594. Block *bp;
  595. Ctlr *ctlr;
  596. Ether *e;
  597. Rd *r;
  598. e = v;
  599. ctlr = e->ctlr;
  600. m = ctlr->nrd - 1;
  601. for (rdh = 0; ; ) {
  602. replenish(ctlr, rdh);
  603. ienable(ctlr, Irx0);
  604. sleep(&ctlr->rrendez, rim, ctlr);
  605. passed = 0;
  606. for (;;) {
  607. ctlr->rim = 0;
  608. r = ctlr->rdba + rdh;
  609. if(!(r->status & Rdd))
  610. break; /* wait for pkts to arrive */
  611. bp = ctlr->rb[rdh];
  612. ctlr->rb[rdh] = 0;
  613. if (r->length > ETHERMAXTU)
  614. print("#l%d: got jumbo of %d bytes\n",
  615. e->ctlrno, r->length);
  616. bp->wp += r->length;
  617. bp->lim = bp->wp; /* lie like a dog */
  618. // r->status = 0;
  619. ilock(&rblock);
  620. nrbfull++;
  621. iunlock(&rblock);
  622. notemark(&ctlr->wmrb, nrbfull);
  623. etheriq(e, bp, 1);
  624. passed++;
  625. ctlr->rdfree--;
  626. rdh = NEXTPOW2(rdh, m);
  627. if (ctlr->rdfree <= ctlr->nrd - 16)
  628. replenish(ctlr, rdh);
  629. }
  630. /* note how many rds had full buffers */
  631. notemark(&ctlr->wmrd, passed);
  632. }
  633. }
  634. static void
  635. promiscuous(void *a, int on)
  636. {
  637. Ctlr *ctlr;
  638. Ether *e;
  639. e = a;
  640. ctlr = e->ctlr;
  641. if(on)
  642. ctlr->reg[Fctrl] |= Upe | Mpe;
  643. else
  644. ctlr->reg[Fctrl] &= ~(Upe | Mpe);
  645. }
  646. static void
  647. multicast(void *a, uchar *ea, int on)
  648. {
  649. int b, i;
  650. Ctlr *ctlr;
  651. Ether *e;
  652. e = a;
  653. ctlr = e->ctlr;
  654. /*
  655. * multiple ether addresses can hash to the same filter bit,
  656. * so it's never safe to clear a filter bit.
  657. * if we want to clear filter bits, we need to keep track of
  658. * all the multicast addresses in use, clear all the filter bits,
  659. * then set the ones corresponding to in-use addresses.
  660. */
  661. i = ea[5] >> 1;
  662. b = (ea[5]&1)<<4 | ea[4]>>4;
  663. b = 1 << b;
  664. if(on)
  665. ctlr->mta[i] |= b;
  666. // else
  667. // ctlr->mta[i] &= ~b;
  668. ctlr->reg[Mta+i] = ctlr->mta[i];
  669. }
  670. static void
  671. freemem(Ctlr *ctlr)
  672. {
  673. Block *b;
  674. while(b = rballoc()){
  675. b->free = 0;
  676. freeb(b);
  677. }
  678. free(ctlr->rdba);
  679. ctlr->rdba = nil;
  680. free(ctlr->tdba);
  681. ctlr->tdba = nil;
  682. free(ctlr->rb);
  683. ctlr->rb = nil;
  684. free(ctlr->tb);
  685. ctlr->tb = nil;
  686. }
  687. static int
  688. detach(Ctlr *ctlr)
  689. {
  690. int i, is598;
  691. ctlr->reg[Imc] = ~0;
  692. ctlr->reg[Ctrl] |= Rst;
  693. for(i = 0; i < 100; i++){
  694. delay(1);
  695. if((ctlr->reg[Ctrl] & Rst) == 0)
  696. break;
  697. }
  698. if (i >= 100)
  699. return -1;
  700. is598 = (ctlr->type == I82598);
  701. if (is598) { /* errata */
  702. delay(50);
  703. ctlr->reg[Ecc] &= ~(1<<21 | 1<<18 | 1<<9 | 1<<6);
  704. }
  705. /* not cleared by reset; kill it manually. */
  706. for(i = 1; i < 16; i++)
  707. ctlr->reg[is598? Rah98: Rah99] &= ~Enable;
  708. for(i = 0; i < 128; i++)
  709. ctlr->reg[Mta + i] = 0;
  710. for(i = 1; i < (is598? 640: 128); i++)
  711. ctlr->reg[Vfta + i] = 0;
  712. // freemem(ctlr); // TODO
  713. ctlr->attached = 0;
  714. return 0;
  715. }
  716. static void
  717. shutdown(Ether *e)
  718. {
  719. detach(e->ctlr);
  720. // freemem(e->ctlr);
  721. }
  722. /* ≤ 20ms */
  723. static ushort
  724. eeread(Ctlr *ctlr, int i)
  725. {
  726. ctlr->reg[Eerd] = EEstart | i<<2;
  727. while((ctlr->reg[Eerd] & EEdone) == 0)
  728. ;
  729. return ctlr->reg[Eerd] >> 16;
  730. }
  731. static int
  732. eeload(Ctlr *ctlr)
  733. {
  734. ushort u, v, p, l, i, j;
  735. if((eeread(ctlr, 0) & 0xc0) != 0x40)
  736. return -1;
  737. u = 0;
  738. for(i = 0; i < 0x40; i++)
  739. u += eeread(ctlr, i);
  740. for(i = 3; i < 0xf; i++){
  741. p = eeread(ctlr, i);
  742. l = eeread(ctlr, p++);
  743. if((int)p + l + 1 > 0xffff)
  744. continue;
  745. for(j = p; j < p + l; j++)
  746. u += eeread(ctlr, j);
  747. }
  748. if(u != 0xbaba)
  749. return -1;
  750. if(ctlr->reg[Status] & (1<<3))
  751. u = eeread(ctlr, 10);
  752. else
  753. u = eeread(ctlr, 9);
  754. u++;
  755. for(i = 0; i < Eaddrlen;){
  756. v = eeread(ctlr, u + i/2);
  757. ctlr->ra[i++] = v;
  758. ctlr->ra[i++] = v>>8;
  759. }
  760. ctlr->ra[5] += (ctlr->reg[Status] & 0xc) >> 2;
  761. return 0;
  762. }
  763. static int
  764. reset(Ctlr *ctlr)
  765. {
  766. int i, is598;
  767. uchar *p;
  768. if(detach(ctlr)){
  769. print("82598: reset timeout\n");
  770. return -1;
  771. }
  772. if(eeload(ctlr)){
  773. print("82598: eeprom failure\n");
  774. return -1;
  775. }
  776. p = ctlr->ra;
  777. is598 = (ctlr->type == I82598);
  778. ctlr->reg[is598? Ral98: Ral99] = p[3]<<24 | p[2]<<16 | p[1]<<8 | p[0];
  779. ctlr->reg[is598? Rah98: Rah99] = p[5]<<8 | p[4] | Enable;
  780. readstats(ctlr);
  781. for(i = 0; i<nelem(ctlr->stats); i++)
  782. ctlr->stats[i] = 0;
  783. ctlr->reg[Ctrlext] |= 1 << 16; /* required by errata (spec change 4) */
  784. if (Goslow) {
  785. /* make some guesses for flow control */
  786. ctlr->reg[Fcrtl] = 0x10000 | Enable;
  787. ctlr->reg[Fcrth] = 0x40000 | Enable;
  788. ctlr->reg[Rcrtv] = 0x6000;
  789. } else
  790. ctlr->reg[Fcrtl] = ctlr->reg[Fcrth] = ctlr->reg[Rcrtv] = 0;
  791. /* configure interrupt mapping (don't ask) */
  792. ctlr->reg[Ivar+0] = 0 | 1<<7;
  793. ctlr->reg[Ivar+64/4] = 1 | 1<<7;
  794. // ctlr->reg[Ivar+97/4] = (2 | 1<<7) << (8*(97%4));
  795. if (Goslow) {
  796. /* interrupt throttling goes here. */
  797. for(i = Itr; i < Itr + 20; i++)
  798. ctlr->reg[i] = 128; /* ¼µs intervals */
  799. ctlr->reg[Itr + Itx0] = 256;
  800. } else { /* don't throttle */
  801. for(i = Itr; i < Itr + 20; i++)
  802. ctlr->reg[i] = 0; /* ¼µs intervals */
  803. ctlr->reg[Itr + Itx0] = 0;
  804. }
  805. return 0;
  806. }
  807. static void
  808. txinit(Ctlr *ctlr)
  809. {
  810. Block *b;
  811. int i;
  812. if (Goslow)
  813. ctlr->reg[Txdctl] = 16<<Wthresh | 16<<Pthresh;
  814. else
  815. ctlr->reg[Txdctl] = 0;
  816. if (ctlr->type == I82599)
  817. ctlr->reg[Dtxctl99] = 0;
  818. coherence();
  819. for(i = 0; i < ctlr->ntd; i++){
  820. b = ctlr->tb[i];
  821. ctlr->tb[i] = 0;
  822. if(b)
  823. freeb(b);
  824. }
  825. assert(ctlr->tdba != nil);
  826. memset(ctlr->tdba, 0, ctlr->ntd * sizeof(Td));
  827. ctlr->reg[Tdbal] = PCIWADDR(ctlr->tdba);
  828. ctlr->reg[Tdbah] = 0;
  829. ctlr->reg[Tdlen] = ctlr->ntd*sizeof(Td); /* must be multiple of 128 */
  830. ctlr->reg[Tdh] = 0;
  831. ctlr->tdh = ctlr->ntd - 1;
  832. ctlr->reg[Tdt] = ctlr->tdt = 0;
  833. coherence();
  834. if (ctlr->type == I82599)
  835. ctlr->reg[Dtxctl99] |= Te;
  836. coherence();
  837. ctlr->reg[Txdctl] |= Ten;
  838. coherence();
  839. while (!(ctlr->reg[Txdctl] & Ten))
  840. ;
  841. }
  842. static void
  843. attach(Ether *e)
  844. {
  845. Block *b;
  846. Ctlr *ctlr;
  847. char buf[KNAMELEN];
  848. ctlr = e->ctlr;
  849. ctlr->edev = e; /* point back to Ether* */
  850. qlock(&ctlr->alock);
  851. if(waserror()){
  852. reset(ctlr);
  853. freemem(ctlr);
  854. qunlock(&ctlr->alock);
  855. nexterror();
  856. }
  857. if(ctlr->rdba == nil) {
  858. ctlr->nrd = Nrd;
  859. ctlr->ntd = Ntd;
  860. ctlr->rdba = mallocalign(ctlr->nrd * sizeof *ctlr->rdba,
  861. Descalign, 0, 0);
  862. ctlr->tdba = mallocalign(ctlr->ntd * sizeof *ctlr->tdba,
  863. Descalign, 0, 0);
  864. ctlr->rb = malloc(ctlr->nrd * sizeof(Block *));
  865. ctlr->tb = malloc(ctlr->ntd * sizeof(Block *));
  866. if (ctlr->rdba == nil || ctlr->tdba == nil ||
  867. ctlr->rb == nil || ctlr->tb == nil)
  868. error(Enomem);
  869. for(ctlr->nrb = 0; ctlr->nrb < 2*Nrb; ctlr->nrb++){
  870. b = allocb(ctlr->rbsz + BY2PG); /* see rbfree() */
  871. if(b == nil)
  872. error(Enomem);
  873. b->free = rbfree;
  874. freeb(b);
  875. }
  876. }
  877. if (!ctlr->attached) {
  878. rxinit(ctlr);
  879. txinit(ctlr);
  880. nrbfull = 0;
  881. if (!ctlr->procsrunning) {
  882. snprint(buf, sizeof buf, "#l%dl", e->ctlrno);
  883. kproc(buf, lproc, e);
  884. snprint(buf, sizeof buf, "#l%dr", e->ctlrno);
  885. kproc(buf, rproc, e);
  886. snprint(buf, sizeof buf, "#l%dt", e->ctlrno);
  887. kproc(buf, tproc, e);
  888. ctlr->procsrunning = 1;
  889. }
  890. initmark(&ctlr->wmrb, Nrb, "rcv bufs unprocessed");
  891. initmark(&ctlr->wmrd, Nrd-1, "rcv descrs processed at once");
  892. initmark(&ctlr->wmtd, Ntd-1, "xmit descr queue len");
  893. ctlr->attached = 1;
  894. }
  895. qunlock(&ctlr->alock);
  896. poperror();
  897. }
  898. static void
  899. interrupt(Ureg*, void *v)
  900. {
  901. int icr, im;
  902. Ctlr *ctlr;
  903. Ether *e;
  904. e = v;
  905. ctlr = e->ctlr;
  906. ilock(&ctlr->imlock);
  907. ctlr->reg[Imc] = ~0; /* disable all intrs */
  908. im = ctlr->im;
  909. while((icr = ctlr->reg[Icr] & ctlr->im) != 0){
  910. if(icr & Irx0){
  911. im &= ~Irx0;
  912. ctlr->rim = Irx0;
  913. wakeup(&ctlr->rrendez);
  914. }
  915. if(icr & Itx0){
  916. im &= ~Itx0;
  917. ctlr->tim = Itx0;
  918. wakeup(&ctlr->trendez);
  919. }
  920. if(icr & Lsc){
  921. im &= ~Lsc;
  922. ctlr->lim = Lsc;
  923. wakeup(&ctlr->lrendez);
  924. }
  925. }
  926. ctlr->reg[Ims] = ctlr->im = im; /* enable only intrs we didn't service */
  927. iunlock(&ctlr->imlock);
  928. }
  929. static void
  930. scan(void)
  931. {
  932. int pciregs, pcimsix, type;
  933. ulong io, iomsi;
  934. void *mem, *memmsi;
  935. Ctlr *ctlr;
  936. Pcidev *p;
  937. p = 0;
  938. while(p = pcimatch(p, Vintel, 0)){
  939. switch(p->did){
  940. case 0x10b6: /* 82598 backplane */
  941. case 0x10c6: /* 82598 af dual port */
  942. case 0x10c7: /* 82598 af single port */
  943. case 0x10dd: /* 82598 at cx4 */
  944. case 0x10ec: /* 82598 at cx4 dual port */
  945. pcimsix = 3;
  946. type = I82598;
  947. break;
  948. case 0x10f7: /* 82599 kx/kx4 */
  949. case 0x10f8: /* 82599 kx/kx4/kx */
  950. case 0x10f9: /* 82599 cx4 */
  951. case 0x10fb: /* 82599 sfi/sfp+ */
  952. case 0x10fc: /* 82599 xaui/bx4 */
  953. case 0x1557: /* 82599 single-port sfi */
  954. pcimsix = 4;
  955. type = I82599;
  956. break;
  957. default:
  958. continue;
  959. }
  960. pciregs = 0;
  961. if(nctlr >= nelem(ctlrtab)){
  962. print("i82598: too many controllers\n");
  963. return;
  964. }
  965. io = p->mem[pciregs].bar & ~0xf;
  966. mem = vmap(io, p->mem[pciregs].size);
  967. if(mem == nil){
  968. print("i82598: can't map regs %#p\n",
  969. p->mem[pciregs].bar);
  970. continue;
  971. }
  972. iomsi = p->mem[pcimsix].bar & ~0xf;
  973. memmsi = vmap(iomsi, p->mem[pcimsix].size);
  974. if(memmsi == nil){
  975. print("i82598: can't map msi-x regs %#p\n",
  976. p->mem[pcimsix].bar);
  977. vunmap(mem, p->mem[pciregs].size);
  978. continue;
  979. }
  980. ctlr = malloc(sizeof *ctlr);
  981. if(ctlr == nil) {
  982. vunmap(mem, p->mem[pciregs].size);
  983. vunmap(memmsi, p->mem[pcimsix].size);
  984. error(Enomem);
  985. }
  986. ctlr->p = p;
  987. ctlr->type = type;
  988. ctlr->physreg = (u32int*)io;
  989. ctlr->physmsix = (u32int*)iomsi;
  990. ctlr->reg = (u32int*)mem;
  991. ctlr->msix = (u32int*)memmsi; /* unused */
  992. ctlr->rbsz = Rbsz;
  993. if(reset(ctlr)){
  994. print("i82598: can't reset\n");
  995. free(ctlr);
  996. vunmap(mem, p->mem[pciregs].size);
  997. vunmap(memmsi, p->mem[pcimsix].size);
  998. continue;
  999. }
  1000. pcisetbme(p);
  1001. ctlrtab[nctlr++] = ctlr;
  1002. }
  1003. }
  1004. static int
  1005. pnp(Ether *e)
  1006. {
  1007. int i;
  1008. Ctlr *ctlr;
  1009. if(nctlr == 0)
  1010. scan();
  1011. ctlr = nil;
  1012. for(i = 0; i < nctlr; i++){
  1013. ctlr = ctlrtab[i];
  1014. if(ctlr == nil || ctlr->flag & Factive)
  1015. continue;
  1016. if(e->port == 0 || e->port == (ulong)ctlr->reg)
  1017. break;
  1018. }
  1019. if (i >= nctlr)
  1020. return -1;
  1021. ctlr->flag |= Factive;
  1022. e->ctlr = ctlr;
  1023. e->port = (uintptr)ctlr->physreg;
  1024. e->irq = ctlr->p->intl;
  1025. e->tbdf = ctlr->p->tbdf;
  1026. e->mbps = 10000;
  1027. e->maxmtu = ETHERMAXTU;
  1028. memmove(e->ea, ctlr->ra, Eaddrlen);
  1029. e->arg = e;
  1030. e->attach = attach;
  1031. e->detach = shutdown;
  1032. e->transmit = transmit;
  1033. e->interrupt = interrupt;
  1034. e->ifstat = ifstat;
  1035. e->shutdown = shutdown;
  1036. e->ctl = ctl;
  1037. e->multicast = multicast;
  1038. e->promiscuous = promiscuous;
  1039. return 0;
  1040. }
  1041. void
  1042. ether82598link(void)
  1043. {
  1044. addethercard("i82598", pnp);
  1045. addethercard("i10gbe", pnp);
  1046. }