etherm10g.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  1. /*
  2. * myricom 10g-pcie-8a 10 Gb ethernet driver
  3. * © 2007 erik quanstrom, coraid
  4. *
  5. * the card is big endian.
  6. * we use uvlong rather than uintptr to hold addresses so that
  7. * we don't get "warning: stupid shift" on 32-bit architectures.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "io.h"
  15. #include "../port/error.h"
  16. #include "../port/netif.h"
  17. #include "../pc/etherif.h"
  18. #ifndef KiB
  19. #define KiB 1024u /* Kibi 0x0000000000000400 */
  20. #define MiB 1048576u /* Mebi 0x0000000000100000 */
  21. #endif /* KiB */
  22. #define dprint(...) if(debug) print(__VA_ARGS__)
  23. #define pcicapdbg(...)
  24. #define malign(n) mallocalign((n), 4*KiB, 0, 0)
  25. #include "etherm10g2k.i"
  26. #include "etherm10g4k.i"
  27. static int debug = 0;
  28. static char Etimeout[] = "timeout";
  29. enum {
  30. Epromsz = 256,
  31. Maxslots= 1024,
  32. Align = 4096,
  33. Maxmtu = 9000,
  34. Noconf = 0xffffffff,
  35. Fwoffset= 1*MiB,
  36. Cmdoff = 0xf80000, /* command port offset */
  37. Fwsubmt = 0xfc0000, /* firmware submission command port offset */
  38. Rdmaoff = 0xfc01c0, /* rdma command port offset */
  39. };
  40. enum {
  41. CZero,
  42. Creset,
  43. Cversion,
  44. CSintrqdma, /* issue these before Cetherup */
  45. CSbigsz, /* in bytes bigsize = 2^n */
  46. CSsmallsz,
  47. CGsendoff,
  48. CGsmallrxoff,
  49. CGbigrxoff,
  50. CGirqackoff,
  51. CGirqdeassoff,
  52. CGsendrgsz,
  53. CGrxrgsz,
  54. CSintrqsz, /* 2^n */
  55. Cetherup, /* above parameters + mtu/mac addr must be set first. */
  56. Cetherdn,
  57. CSmtu, /* below may be issued live */
  58. CGcoaloff, /* in µs */
  59. CSstatsrate, /* in µs */
  60. CSstatsdma,
  61. Cpromisc,
  62. Cnopromisc,
  63. CSmac,
  64. Cenablefc,
  65. Cdisablefc,
  66. Cdmatest, /* address in d[0-1], d[2]=length */
  67. Cenableallmc,
  68. Cdisableallmc,
  69. CSjoinmc,
  70. CSleavemc,
  71. Cleaveallmc,
  72. CSstatsdma2, /* adds (unused) multicast stats */
  73. };
  74. typedef union {
  75. uint i[2];
  76. uchar c[8];
  77. } Cmd;
  78. typedef ulong Slot;
  79. typedef struct {
  80. ushort cksum;
  81. ushort len;
  82. } Slotparts;
  83. enum {
  84. SFsmall = 1,
  85. SFfirst = 2,
  86. SFalign = 4,
  87. SFnotso = 16,
  88. };
  89. typedef struct {
  90. ulong high;
  91. ulong low;
  92. ushort hdroff;
  93. ushort len;
  94. uchar pad;
  95. uchar nrdma;
  96. uchar chkoff;
  97. uchar flags;
  98. } Send;
  99. typedef struct {
  100. QLock;
  101. Send *lanai; /* tx ring (cksum+len in lanai memory) */
  102. Send *host; /* tx ring (data in our memory) */
  103. Block **bring;
  104. // uchar *wcfifo; /* what the heck is a w/c fifo? */
  105. int size; /* of buffers in the z8's memory */
  106. ulong segsz;
  107. uint n; /* rxslots */
  108. uint m; /* mask; rxslots must be a power of two */
  109. uint i; /* number of segments (not frames) queued */
  110. uint cnt; /* number of segments sent by the card */
  111. ulong npkt;
  112. vlong nbytes;
  113. } Tx;
  114. typedef struct {
  115. Lock;
  116. Block *head;
  117. uint size; /* buffer size of each block */
  118. uint n; /* n free buffers */
  119. uint cnt;
  120. } Bpool;
  121. static Bpool smpool = { .size = 128, };
  122. static Bpool bgpool = { .size = Maxmtu, };
  123. typedef struct {
  124. Bpool *pool; /* free buffers */
  125. ulong *lanai; /* rx ring; we have no permanent host shadow */
  126. Block **host; /* called "info" in myricom driver */
  127. // uchar *wcfifo; /* cmd submission fifo */
  128. uint m;
  129. uint n; /* rxslots */
  130. uint i;
  131. uint cnt; /* number of buffers allocated (lifetime) */
  132. uint allocfail;
  133. } Rx;
  134. /* dma mapped. unix network byte order. */
  135. typedef struct {
  136. uchar txcnt[4];
  137. uchar linkstat[4];
  138. uchar dlink[4];
  139. uchar derror[4];
  140. uchar drunt[4];
  141. uchar doverrun[4];
  142. uchar dnosm[4];
  143. uchar dnobg[4];
  144. uchar nrdma[4];
  145. uchar txstopped;
  146. uchar down;
  147. uchar updated;
  148. uchar valid;
  149. } Stats;
  150. enum {
  151. Detached,
  152. Attached,
  153. Runed,
  154. };
  155. typedef struct {
  156. Slot *entry;
  157. uvlong busaddr;
  158. uint m;
  159. uint n;
  160. uint i;
  161. } Done;
  162. typedef struct Ctlr Ctlr;
  163. typedef struct Ctlr {
  164. QLock;
  165. int state;
  166. int kprocs;
  167. uvlong port;
  168. Pcidev* pcidev;
  169. Ctlr* next;
  170. int active;
  171. int id; /* do we need this? */
  172. uchar ra[Eaddrlen];
  173. int ramsz;
  174. uchar *ram;
  175. ulong *irqack;
  176. ulong *irqdeass;
  177. ulong *coal;
  178. char eprom[Epromsz];
  179. ulong serial; /* unit serial number */
  180. QLock cmdl;
  181. Cmd *cmd; /* address of command return */
  182. uvlong cprt; /* bus address of command */
  183. uvlong boot; /* boot address */
  184. Done done;
  185. Tx tx;
  186. Rx sm;
  187. Rx bg;
  188. Stats *stats;
  189. uvlong statsprt;
  190. Rendez rxrendez;
  191. Rendez txrendez;
  192. int msi;
  193. ulong linkstat;
  194. ulong nrdma;
  195. } Ctlr;
  196. static Ctlr *ctlrs;
  197. enum {
  198. PciCapPMG = 0x01, /* power management */
  199. PciCapAGP = 0x02,
  200. PciCapVPD = 0x03, /* vital product data */
  201. PciCapSID = 0x04, /* slot id */
  202. PciCapMSI = 0x05,
  203. PciCapCHS = 0x06, /* compact pci hot swap */
  204. PciCapPCIX = 0x07,
  205. PciCapHTC = 0x08, /* hypertransport irq conf */
  206. PciCapVND = 0x09, /* vendor specific information */
  207. PciCapHSW = 0x0C, /* hot swap */
  208. PciCapPCIe = 0x10,
  209. PciCapMSIX = 0x11,
  210. };
  211. enum {
  212. PcieAERC = 1,
  213. PcieVC,
  214. PcieSNC,
  215. PciePBC,
  216. };
  217. enum {
  218. AercCCR = 0x18, /* control register */
  219. };
  220. enum {
  221. PcieCTL = 8,
  222. PcieLCR = 12,
  223. PcieMRD = 0x7000, /* maximum read size */
  224. };
  225. static int
  226. pcicap(Pcidev *p, int cap)
  227. {
  228. int i, c, off;
  229. pcicapdbg("pcicap: %x:%d\n", p->vid, p->did);
  230. off = 0x34; /* 0x14 for cardbus */
  231. for(i = 48; i--; ){
  232. pcicapdbg("\t" "loop %x\n", off);
  233. off = pcicfgr8(p, off);
  234. pcicapdbg("\t" "pcicfgr8 %x\n", off);
  235. if(off < 0x40)
  236. break;
  237. off &= ~3;
  238. c = pcicfgr8(p, off);
  239. pcicapdbg("\t" "pcicfgr8 %x\n", c);
  240. if(c == 0xff)
  241. break;
  242. if(c == cap)
  243. return off;
  244. off++;
  245. }
  246. return 0;
  247. }
  248. /*
  249. * this function doesn't work because pcicgr32 doesn't have access
  250. * to the pcie extended configuration space.
  251. */
  252. static int
  253. pciecap(Pcidev *p, int cap)
  254. {
  255. uint off, i;
  256. off = 0x100;
  257. while(((i = pcicfgr32(p, off)) & 0xffff) != cap){
  258. off = i >> 20;
  259. print("m10g: pciecap offset = %ud", off);
  260. if(off < 0x100 || off >= 4*KiB - 1)
  261. return 0;
  262. }
  263. print("m10g: pciecap found = %ud", off);
  264. return off;
  265. }
  266. static int
  267. setpcie(Pcidev *p)
  268. {
  269. int off;
  270. /* set 4k writes */
  271. off = pcicap(p, PciCapPCIe);
  272. if(off < 64)
  273. return -1;
  274. off += PcieCTL;
  275. pcicfgw16(p, off, (pcicfgr16(p, off) & ~PcieMRD) | 5<<12);
  276. return 0;
  277. }
  278. static int
  279. whichfw(Pcidev *p)
  280. {
  281. char *s;
  282. int i, off, lanes, ecrc;
  283. ulong cap;
  284. /* check the number of configured lanes. */
  285. off = pcicap(p, PciCapPCIe);
  286. if(off < 64)
  287. return -1;
  288. off += PcieLCR;
  289. cap = pcicfgr16(p, off);
  290. lanes = (cap>>4) & 0x3f;
  291. /* check AERC register. we need it on. */
  292. off = pciecap(p, PcieAERC);
  293. print("; offset %d returned\n", off);
  294. cap = 0;
  295. if(off != 0){
  296. off += AercCCR;
  297. cap = pcicfgr32(p, off);
  298. print("m10g: %lud cap\n", cap);
  299. }
  300. ecrc = (cap>>4) & 0xf;
  301. /* if we don't like the aerc, kick it here. */
  302. print("m10g: %d lanes; ecrc=%d; ", lanes, ecrc);
  303. if(s = getconf("myriforce")){
  304. i = atoi(s);
  305. if(i != 4*KiB || i != 2*KiB)
  306. i = 2*KiB;
  307. print("fw = %d [forced]\n", i);
  308. return i;
  309. }
  310. if(lanes <= 4)
  311. print("fw = 4096 [lanes]\n");
  312. else if(ecrc & 10)
  313. print("fw = 4096 [ecrc set]\n");
  314. else
  315. print("fw = 4096 [default]\n");
  316. return 4*KiB;
  317. }
  318. static int
  319. parseeprom(Ctlr *c)
  320. {
  321. int i, j, k, l, bits;
  322. char *s;
  323. dprint("m10g eprom:\n");
  324. s = c->eprom;
  325. bits = 3;
  326. for(i = 0; s[i] && i < Epromsz; i++){
  327. l = strlen(s+i);
  328. dprint("\t%s\n", s+i);
  329. if(strncmp(s+i, "MAC=", 4) == 0 && l == 4+12+5){
  330. bits ^= 1;
  331. j = i + 4;
  332. for(k = 0; k < 6; k++)
  333. c->ra[k] = strtoul(s+j+3*k, 0, 16);
  334. }else if(strncmp(s+i, "SN=", 3) == 0){
  335. bits ^= 2;
  336. c->serial = atoi(s+i+3);
  337. }
  338. i += l;
  339. }
  340. if(bits)
  341. return -1;
  342. return 0;
  343. }
  344. static ushort
  345. pbit16(ushort i)
  346. {
  347. ushort j;
  348. uchar *p;
  349. p = (uchar*)&j;
  350. p[1] = i;
  351. p[0] = i>>8;
  352. return j;
  353. }
  354. static ushort
  355. gbit16(uchar i[2])
  356. {
  357. ushort j;
  358. j = i[1];
  359. j |= i[0]<<8;
  360. return j;
  361. }
  362. static ulong
  363. pbit32(ulong i)
  364. {
  365. ulong j;
  366. uchar *p;
  367. p = (uchar*)&j;
  368. p[3] = i;
  369. p[2] = i>>8;
  370. p[1] = i>>16;
  371. p[0] = i>>24;
  372. return j;
  373. }
  374. static ulong
  375. gbit32(uchar i[4])
  376. {
  377. ulong j;
  378. j = i[3];
  379. j |= i[2]<<8;
  380. j |= i[1]<<16;
  381. j |= i[0]<<24;
  382. return j;
  383. }
  384. static void
  385. prepcmd(ulong *cmd, int i)
  386. {
  387. while(i-- > 0)
  388. cmd[i] = pbit32(cmd[i]);
  389. }
  390. /*
  391. * the command looks like this (int 32bit integers)
  392. * cmd type
  393. * addr (low)
  394. * addr (high)
  395. * pad (used for dma testing)
  396. * response (high)
  397. * response (low)
  398. * 40 byte = 5 int pad.
  399. */
  400. ulong
  401. cmd(Ctlr *c, int type, uvlong data)
  402. {
  403. ulong buf[16], i;
  404. Cmd *cmd;
  405. qlock(&c->cmdl);
  406. cmd = c->cmd;
  407. cmd->i[1] = Noconf;
  408. memset(buf, 0, sizeof buf);
  409. buf[0] = type;
  410. buf[1] = data;
  411. buf[2] = data >> 32;
  412. buf[4] = c->cprt >> 32;
  413. buf[5] = c->cprt;
  414. prepcmd(buf, 6);
  415. coherence();
  416. memmove(c->ram + Cmdoff, buf, sizeof buf);
  417. if(waserror())
  418. nexterror();
  419. for(i = 0; i < 15; i++){
  420. if(cmd->i[1] != Noconf){
  421. poperror();
  422. i = gbit32(cmd->c);
  423. qunlock(&c->cmdl);
  424. if(cmd->i[1] != 0)
  425. dprint("[%lux]", i);
  426. return i;
  427. }
  428. tsleep(&up->sleep, return0, 0, 1);
  429. }
  430. qunlock(&c->cmdl);
  431. iprint("m10g: cmd timeout [%ux %ux] cmd=%d\n",
  432. cmd->i[0], cmd->i[1], type);
  433. error(Etimeout);
  434. return ~0; /* silence! */
  435. }
  436. ulong
  437. maccmd(Ctlr *c, int type, uchar *m)
  438. {
  439. ulong buf[16], i;
  440. Cmd *cmd;
  441. qlock(&c->cmdl);
  442. cmd = c->cmd;
  443. cmd->i[1] = Noconf;
  444. memset(buf, 0, sizeof buf);
  445. buf[0] = type;
  446. buf[1] = m[0]<<24 | m[1]<<16 | m[2]<<8 | m[3];
  447. buf[2] = m[4]<< 8 | m[5];
  448. buf[4] = c->cprt >> 32;
  449. buf[5] = c->cprt;
  450. prepcmd(buf, 6);
  451. coherence();
  452. memmove(c->ram + Cmdoff, buf, sizeof buf);
  453. if(waserror())
  454. nexterror();
  455. for(i = 0; i < 15; i++){
  456. if(cmd->i[1] != Noconf){
  457. poperror();
  458. i = gbit32(cmd->c);
  459. qunlock(&c->cmdl);
  460. if(cmd->i[1] != 0)
  461. dprint("[%lux]", i);
  462. return i;
  463. }
  464. tsleep(&up->sleep, return0, 0, 1);
  465. }
  466. qunlock(&c->cmdl);
  467. iprint("m10g: maccmd timeout [%ux %ux] cmd=%d\n",
  468. cmd->i[0], cmd->i[1], type);
  469. error(Etimeout);
  470. return ~0; /* silence! */
  471. }
  472. /* remove this garbage after testing */
  473. enum {
  474. DMAread = 0x10000,
  475. DMAwrite= 0x1,
  476. };
  477. ulong
  478. dmatestcmd(Ctlr *c, int type, uvlong addr, int len)
  479. {
  480. ulong buf[16], i;
  481. memset(buf, 0, sizeof buf);
  482. memset(c->cmd, Noconf, sizeof *c->cmd);
  483. buf[0] = Cdmatest;
  484. buf[1] = addr;
  485. buf[2] = addr >> 32;
  486. buf[3] = len * type;
  487. buf[4] = c->cprt >> 32;
  488. buf[5] = c->cprt;
  489. prepcmd(buf, 6);
  490. coherence();
  491. memmove(c->ram + Cmdoff, buf, sizeof buf);
  492. if(waserror())
  493. nexterror();
  494. for(i = 0; i < 15; i++){
  495. if(c->cmd->i[1] != Noconf){
  496. i = gbit32(c->cmd->c);
  497. if(i == 0)
  498. error(Eio);
  499. poperror();
  500. return i;
  501. }
  502. tsleep(&up->sleep, return0, 0, 5);
  503. }
  504. error(Etimeout);
  505. return ~0; /* silence! */
  506. }
  507. ulong
  508. rdmacmd(Ctlr *c, int on)
  509. {
  510. ulong buf[16], i;
  511. memset(buf, 0, sizeof buf);
  512. c->cmd->i[0] = 0;
  513. coherence();
  514. buf[0] = c->cprt >> 32;
  515. buf[1] = c->cprt;
  516. buf[2] = Noconf;
  517. buf[3] = c->cprt >> 32;
  518. buf[4] = c->cprt;
  519. buf[5] = on;
  520. prepcmd(buf, 6);
  521. memmove(c->ram + Rdmaoff, buf, sizeof buf);
  522. if(waserror())
  523. nexterror();
  524. for(i = 0; i < 20; i++){
  525. if(c->cmd->i[0] == Noconf){
  526. poperror();
  527. return gbit32(c->cmd->c);
  528. }
  529. tsleep(&up->sleep, return0, 0, 1);
  530. }
  531. error(Etimeout);
  532. iprint("m10g: rdmacmd timeout\n");
  533. return ~0; /* silence! */
  534. }
  535. static int
  536. loadfw(Ctlr *c, int *align)
  537. {
  538. ulong *f, *s, sz;
  539. int i;
  540. if((*align = whichfw(c->pcidev)) == 4*KiB){
  541. f = (ulong*)fw4k;
  542. sz = sizeof fw4k;
  543. }else{
  544. f = (ulong*)fw2k;
  545. sz = sizeof fw2k;
  546. }
  547. s = (ulong*)(c->ram + Fwoffset);
  548. for(i = 0; i < sz / 4; i++)
  549. s[i] = f[i];
  550. return sz & ~3;
  551. }
  552. static int
  553. bootfw(Ctlr *c)
  554. {
  555. int i, sz, align;
  556. ulong buf[16];
  557. Cmd* cmd;
  558. if((sz = loadfw(c, &align)) == 0)
  559. return 0;
  560. dprint("bootfw %d bytes ... ", sz);
  561. cmd = c->cmd;
  562. memset(buf, 0, sizeof buf);
  563. c->cmd->i[0] = 0;
  564. coherence();
  565. buf[0] = c->cprt >> 32; /* upper dma target address */
  566. buf[1] = c->cprt; /* lower */
  567. buf[2] = Noconf; /* writeback */
  568. buf[3] = Fwoffset + 8,
  569. buf[4] = sz - 8;
  570. buf[5] = 8;
  571. buf[6] = 0;
  572. prepcmd(buf, 7);
  573. coherence();
  574. memmove(c->ram + Fwsubmt, buf, sizeof buf);
  575. for(i = 0; i < 20; i++){
  576. if(cmd->i[0] == Noconf)
  577. break;
  578. delay(1);
  579. }
  580. dprint("[%lux %lux]", gbit32(cmd->c), gbit32(cmd->c+4));
  581. if(i == 20){
  582. print("m10g: cannot load fw\n");
  583. return -1;
  584. }
  585. dprint("\n");
  586. c->tx.segsz = align;
  587. return 0;
  588. }
  589. static int
  590. kickthebaby(Pcidev *p, Ctlr *c)
  591. {
  592. /* don't kick the baby! */
  593. ulong code;
  594. pcicfgw8(p, 0x10 + c->boot, 0x3);
  595. pcicfgw32(p, 0x18 + c->boot, 0xfffffff0);
  596. code = pcicfgr32(p, 0x14 + c->boot);
  597. dprint("reboot status = %lux\n", code);
  598. if(code != 0xfffffff0)
  599. return -1;
  600. return 0;
  601. }
  602. typedef struct {
  603. uchar len[4];
  604. uchar type[4];
  605. char version[128];
  606. uchar globals[4];
  607. uchar ramsz[4];
  608. uchar specs[4];
  609. uchar specssz[4];
  610. } Fwhdr;
  611. enum {
  612. Tmx = 0x4d582020,
  613. Tpcie = 0x70636965,
  614. Teth = 0x45544820,
  615. Tmcp0 = 0x4d435030,
  616. };
  617. static char *
  618. fwtype(ulong type)
  619. {
  620. switch(type){
  621. case Tmx:
  622. return "mx";
  623. case Tpcie:
  624. return "PCIe";
  625. case Teth:
  626. return "eth";
  627. case Tmcp0:
  628. return "mcp0";
  629. }
  630. return "*GOK*";
  631. }
  632. static int
  633. chkfw(Ctlr *c)
  634. {
  635. ulong off, type;
  636. Fwhdr *h;
  637. off = gbit32(c->ram+0x3c);
  638. dprint("firmware %lux\n", off);
  639. if((off&3) || off + sizeof *h > c->ramsz){
  640. print("!m10g: bad firmware %lux\n", off);
  641. return -1;
  642. }
  643. h = (Fwhdr*)(c->ram + off);
  644. type = gbit32(h->type);
  645. dprint("\t" "type %s\n", fwtype(type));
  646. dprint("\t" "vers %s\n", h->version);
  647. dprint("\t" "ramsz %lux\n", gbit32(h->ramsz));
  648. if(type != Teth){
  649. print("!m10g: bad card type %s\n", fwtype(type));
  650. return -1;
  651. }
  652. return bootfw(c) || rdmacmd(c, 0);
  653. }
  654. static int
  655. reset(Ether *e, Ctlr *c)
  656. {
  657. ulong i, sz;
  658. if(waserror()){
  659. print("m10g: reset error\n");
  660. nexterror();
  661. return -1;
  662. }
  663. chkfw(c);
  664. cmd(c, Creset, 0);
  665. cmd(c, CSintrqsz, c->done.n * sizeof *c->done.entry);
  666. cmd(c, CSintrqdma, c->done.busaddr);
  667. c->irqack = (ulong*)(c->ram + cmd(c, CGirqackoff, 0));
  668. /* required only if we're not doing msi? */
  669. c->irqdeass = (ulong*)(c->ram + cmd(c, CGirqdeassoff, 0));
  670. /* this is the driver default, why fiddle with this? */
  671. c->coal = (ulong*)(c->ram + cmd(c, CGcoaloff, 0));
  672. *c->coal = pbit32(25);
  673. dprint("dma stats:\n");
  674. rdmacmd(c, 1);
  675. sz = c->tx.segsz;
  676. i = dmatestcmd(c, DMAread, c->done.busaddr, sz);
  677. print("m10g: read %lud MB/s;", ((i>>16)*sz*2) / (i&0xffff));
  678. i = dmatestcmd(c, DMAwrite, c->done.busaddr, sz);
  679. print(" write %lud MB/s;", ((i>>16)*sz*2) / (i&0xffff));
  680. i = dmatestcmd(c, DMAwrite|DMAread, c->done.busaddr, sz);
  681. print(" r/w %lud MB/s\n", ((i>>16)*sz*2*2) / (i&0xffff));
  682. memset(c->done.entry, 0, c->done.n * sizeof *c->done.entry);
  683. maccmd(c, CSmac, c->ra);
  684. // cmd(c, Cnopromisc, 0);
  685. cmd(c, Cenablefc, 0);
  686. e->maxmtu = Maxmtu;
  687. cmd(c, CSmtu, e->maxmtu);
  688. dprint("CSmtu %d...\n", e->maxmtu);
  689. poperror();
  690. return 0;
  691. }
  692. static void
  693. ctlrfree(Ctlr *c)
  694. {
  695. /* free up all the Block*s, too */
  696. free(c->tx.host);
  697. free(c->sm.host);
  698. free(c->bg.host);
  699. free(c->cmd);
  700. free(c->done.entry);
  701. free(c->stats);
  702. free(c);
  703. }
  704. static int
  705. setmem(Pcidev *p, Ctlr *c)
  706. {
  707. ulong i;
  708. uvlong raddr;
  709. Done *d;
  710. void *mem;
  711. c->tx.segsz = 2048;
  712. c->ramsz = 2*MiB - (2*48*KiB + 32*KiB) - 0x100;
  713. if(c->ramsz > p->mem[0].size)
  714. return -1;
  715. raddr = p->mem[0].bar & ~0x0F;
  716. mem = vmap(raddr, p->mem[0].size);
  717. if(mem == nil){
  718. print("m10g: can't map %8.8lux\n", p->mem[0].bar);
  719. return -1;
  720. }
  721. dprint("%llux <- vmap(mem[0].size = %ux)\n", raddr, p->mem[0].size);
  722. c->port = raddr;
  723. c->ram = mem;
  724. c->cmd = malign(sizeof *c->cmd);
  725. c->cprt = PCIWADDR(c->cmd);
  726. d = &c->done;
  727. d->n = Maxslots;
  728. d->m = d->n - 1;
  729. i = d->n * sizeof *d->entry;
  730. d->entry = malign(i);
  731. memset(d->entry, 0, i);
  732. d->busaddr = PCIWADDR(d->entry);
  733. c->stats = malign(sizeof *c->stats);
  734. memset(c->stats, 0, sizeof *c->stats);
  735. c->statsprt = PCIWADDR(c->stats);
  736. memmove(c->eprom, c->ram + c->ramsz - Epromsz, Epromsz-2);
  737. return setpcie(p) || parseeprom(c);
  738. }
  739. static Rx*
  740. whichrx(Ctlr *c, int sz)
  741. {
  742. if(sz <= smpool.size)
  743. return &c->sm;
  744. return &c->bg;
  745. }
  746. static Block*
  747. balloc(Rx* rx)
  748. {
  749. Block *bp;
  750. ilock(rx->pool);
  751. if((bp = rx->pool->head) != nil){
  752. rx->pool->head = bp->next;
  753. bp->next = nil;
  754. _xinc(&bp->ref); /* prevent bp from being freed */
  755. rx->pool->n--;
  756. }
  757. iunlock(rx->pool);
  758. return bp;
  759. }
  760. static void
  761. rbfree(Block *b, Bpool *p)
  762. {
  763. b->rp = b->wp = (uchar*)PGROUND((uintptr)b->base);
  764. b->flag &= ~(Bipck | Budpck | Btcpck | Bpktck);
  765. ilock(p);
  766. b->next = p->head;
  767. p->head = b;
  768. p->n++;
  769. p->cnt++;
  770. iunlock(p);
  771. }
  772. static void
  773. smbfree(Block *b)
  774. {
  775. rbfree(b, &smpool);
  776. }
  777. static void
  778. bgbfree(Block *b)
  779. {
  780. rbfree(b, &bgpool);
  781. }
  782. static void
  783. replenish(Rx *rx)
  784. {
  785. ulong buf[16], i, idx, e;
  786. Bpool *p;
  787. Block *b;
  788. p = rx->pool;
  789. if(p->n < 8)
  790. return;
  791. memset(buf, 0, sizeof buf);
  792. e = (rx->i - rx->cnt) & ~7;
  793. e += rx->n;
  794. while(p->n >= 8 && e){
  795. idx = rx->cnt & rx->m;
  796. for(i = 0; i < 8; i++){
  797. b = balloc(rx);
  798. buf[i*2] = pbit32((uvlong)PCIWADDR(b->wp) >> 32);
  799. buf[i*2+1] = pbit32(PCIWADDR(b->wp));
  800. rx->host[idx+i] = b;
  801. assert(b);
  802. }
  803. memmove(rx->lanai + 2*idx, buf, sizeof buf);
  804. coherence();
  805. rx->cnt += 8;
  806. e -= 8;
  807. }
  808. if(e && p->n > 7+1)
  809. print("m10g: should panic? pool->n = %d\n", p->n);
  810. }
  811. /*
  812. * future:
  813. * if (c->mtrr >= 0) {
  814. * c->tx.wcfifo = c->ram+0x200000;
  815. * c->sm.wcfifo = c->ram+0x300000;
  816. * c->bg.wcfifo = c->ram+0x340000;
  817. * }
  818. */
  819. static int
  820. nextpow(int j)
  821. {
  822. int i;
  823. for(i = 0; j > (1 << i); i++)
  824. ;
  825. return 1 << i;
  826. }
  827. static void*
  828. emalign(int sz)
  829. {
  830. void *v;
  831. v = malign(sz);
  832. if(v == nil)
  833. error(Enomem);
  834. memset(v, 0, sz);
  835. return v;
  836. }
  837. static void
  838. open0(Ether *e, Ctlr *c)
  839. {
  840. Block *b;
  841. int i, sz, entries;
  842. entries = cmd(c, CGsendrgsz, 0) / sizeof *c->tx.lanai;
  843. c->tx.lanai = (Send*)(c->ram + cmd(c, CGsendoff, 0));
  844. c->tx.host = emalign(entries * sizeof *c->tx.host);
  845. c->tx.bring = emalign(entries * sizeof *c->tx.bring);
  846. c->tx.n = entries;
  847. c->tx.m = entries-1;
  848. entries = cmd(c, CGrxrgsz, 0)/8;
  849. c->sm.pool = &smpool;
  850. cmd(c, CSsmallsz, c->sm.pool->size);
  851. c->sm.lanai = (ulong*)(c->ram + cmd(c, CGsmallrxoff, 0));
  852. c->sm.n = entries;
  853. c->sm.m = entries-1;
  854. c->sm.host = emalign(entries * sizeof *c->sm.host);
  855. c->bg.pool = &bgpool;
  856. c->bg.pool->size = nextpow(2 + e->maxmtu); /* 2-byte alignment pad */
  857. cmd(c, CSbigsz, c->bg.pool->size);
  858. c->bg.lanai = (ulong*)(c->ram + cmd(c, CGbigrxoff, 0));
  859. c->bg.n = entries;
  860. c->bg.m = entries-1;
  861. c->bg.host = emalign(entries * sizeof *c->bg.host);
  862. sz = c->sm.pool->size + BY2PG;
  863. for(i = 0; i < c->sm.n; i++){
  864. if((b = allocb(sz)) == 0)
  865. break;
  866. b->free = smbfree;
  867. freeb(b);
  868. }
  869. sz = c->bg.pool->size + BY2PG;
  870. for(i = 0; i < c->bg.n; i++){
  871. if((b = allocb(sz)) == 0)
  872. break;
  873. b->free = bgbfree;
  874. freeb(b);
  875. }
  876. cmd(c, CSstatsdma, c->statsprt);
  877. c->linkstat = ~0;
  878. c->nrdma = 15;
  879. cmd(c, Cetherup, 0);
  880. }
  881. static Block*
  882. nextblock(Ctlr *c)
  883. {
  884. uint i;
  885. ushort l, k;
  886. Block *b;
  887. Done *d;
  888. Rx *rx;
  889. Slot *s;
  890. Slotparts *sp;
  891. d = &c->done;
  892. s = d->entry;
  893. i = d->i & d->m;
  894. sp = (Slotparts *)(s + i);
  895. l = sp->len;
  896. if(l == 0)
  897. return 0;
  898. k = sp->cksum;
  899. s[i] = 0;
  900. d->i++;
  901. l = gbit16((uchar*)&l);
  902. //dprint("nextb: i=%d l=%d\n", d->i, l);
  903. rx = whichrx(c, l);
  904. if(rx->i >= rx->cnt){
  905. iprint("m10g: overrun\n");
  906. return 0;
  907. }
  908. i = rx->i & rx->m;
  909. b = rx->host[i];
  910. rx->host[i] = 0;
  911. if(b == 0){
  912. iprint("m10g: error rx to no block. memory is hosed.\n");
  913. return 0;
  914. }
  915. rx->i++;
  916. b->flag |= Bipck|Btcpck|Budpck;
  917. b->checksum = k;
  918. b->rp += 2;
  919. b->wp += 2+l;
  920. b->lim = b->wp; /* lie like a dog. */
  921. return b;
  922. }
  923. static int
  924. rxcansleep(void *v)
  925. {
  926. Ctlr *c;
  927. Slot *s;
  928. Slotparts *sp;
  929. Done *d;
  930. c = v;
  931. d = &c->done;
  932. s = c->done.entry;
  933. sp = (Slotparts *)(s + (d->i & d->m));
  934. if(sp->len != 0)
  935. return -1;
  936. c->irqack[0] = pbit32(3);
  937. return 0;
  938. }
  939. static void
  940. m10rx(void *v)
  941. {
  942. Ether *e;
  943. Ctlr *c;
  944. Block *b;
  945. e = v;
  946. c = e->ctlr;
  947. for(;;){
  948. replenish(&c->sm);
  949. replenish(&c->bg);
  950. sleep(&c->rxrendez, rxcansleep, c);
  951. while(b = nextblock(c))
  952. etheriq(e, b, 1);
  953. }
  954. }
  955. static void
  956. txcleanup(Tx *tx, ulong n)
  957. {
  958. Block *b;
  959. uint j, l, m;
  960. if(tx->npkt == n)
  961. return;
  962. l = 0;
  963. m = tx->m;
  964. /*
  965. * if tx->cnt == tx->i, yet tx->npkt == n-1, we just
  966. * caught ourselves and myricom card updating.
  967. */
  968. for(;; tx->cnt++){
  969. j = tx->cnt & tx->m;
  970. if(b = tx->bring[j]){
  971. tx->bring[j] = 0;
  972. tx->nbytes += BLEN(b);
  973. freeb(b);
  974. if(++tx->npkt == n)
  975. return;
  976. }
  977. if(tx->cnt == tx->i)
  978. return;
  979. if(l++ == m){
  980. iprint("m10g: tx ovrun: %lud %lud\n", n, tx->npkt);
  981. return;
  982. }
  983. }
  984. }
  985. static int
  986. txcansleep(void *v)
  987. {
  988. Ctlr *c;
  989. c = v;
  990. if(c->tx.cnt != c->tx.i && c->tx.npkt != gbit32(c->stats->txcnt))
  991. return -1;
  992. return 0;
  993. }
  994. static void
  995. txproc(void *v)
  996. {
  997. Ether *e;
  998. Ctlr *c;
  999. Tx *tx;
  1000. e = v;
  1001. c = e->ctlr;
  1002. tx = &c->tx;
  1003. for(;;){
  1004. sleep(&c->txrendez, txcansleep, c);
  1005. txcleanup(tx, gbit32(c->stats->txcnt));
  1006. }
  1007. }
  1008. static void
  1009. submittx(Tx *tx, int n)
  1010. {
  1011. Send *l, *h;
  1012. int i0, i, m;
  1013. m = tx->m;
  1014. i0 = tx->i & m;
  1015. l = tx->lanai;
  1016. h = tx->host;
  1017. for(i = n-1; i >= 0; i--)
  1018. memmove(l+(i + i0 & m), h+(i + i0 & m), sizeof *h);
  1019. tx->i += n;
  1020. // coherence();
  1021. }
  1022. static int
  1023. nsegments(Block *b, int segsz)
  1024. {
  1025. uintptr bus, end, slen, len;
  1026. int i;
  1027. bus = PCIWADDR(b->rp);
  1028. i = 0;
  1029. for(len = BLEN(b); len; len -= slen){
  1030. end = bus + segsz & ~(segsz-1);
  1031. slen = end - bus;
  1032. if(slen > len)
  1033. slen = len;
  1034. bus += slen;
  1035. i++;
  1036. }
  1037. return i;
  1038. }
  1039. static void
  1040. m10gtransmit(Ether *e)
  1041. {
  1042. ushort slen;
  1043. ulong i, cnt, rdma, nseg, count, end, bus, len, segsz;
  1044. uchar flags;
  1045. Block *b;
  1046. Ctlr *c;
  1047. Send *s, *s0, *s0m8;
  1048. Tx *tx;
  1049. c = e->ctlr;
  1050. tx = &c->tx;
  1051. segsz = tx->segsz;
  1052. qlock(tx);
  1053. count = 0;
  1054. s = tx->host + (tx->i & tx->m);
  1055. cnt = tx->cnt;
  1056. s0 = tx->host + (cnt & tx->m);
  1057. s0m8 = tx->host + ((cnt - 8) & tx->m);
  1058. i = tx->i;
  1059. for(; s >= s0 || s < s0m8; i += nseg){
  1060. if((b = qget(e->oq)) == nil)
  1061. break;
  1062. flags = SFfirst|SFnotso;
  1063. if((len = BLEN(b)) < 1520)
  1064. flags |= SFsmall;
  1065. rdma = nseg = nsegments(b, segsz);
  1066. bus = PCIWADDR(b->rp);
  1067. for(; len; len -= slen){
  1068. end = (bus + segsz) & ~(segsz-1);
  1069. slen = end - bus;
  1070. if(slen > len)
  1071. slen = len;
  1072. s->low = pbit32(bus);
  1073. s->len = pbit16(slen);
  1074. s->nrdma = rdma;
  1075. s->flags = flags;
  1076. bus += slen;
  1077. if(++s == tx->host + tx->n)
  1078. s = tx->host;
  1079. count++;
  1080. flags &= ~SFfirst;
  1081. rdma = 1;
  1082. }
  1083. tx->bring[(i + nseg - 1) & tx->m] = b;
  1084. if(1 || count > 0){
  1085. submittx(tx, count);
  1086. count = 0;
  1087. cnt = tx->cnt;
  1088. s0 = tx->host + (cnt & tx->m);
  1089. s0m8 = tx->host + ((cnt - 8) & tx->m);
  1090. }
  1091. }
  1092. qunlock(tx);
  1093. }
  1094. static void
  1095. checkstats(Ether *e, Ctlr *c, Stats *s)
  1096. {
  1097. ulong i;
  1098. if(s->updated == 0)
  1099. return;
  1100. i = gbit32(s->linkstat);
  1101. if(c->linkstat != i){
  1102. e->link = i;
  1103. if(c->linkstat = i)
  1104. dprint("m10g: link up\n");
  1105. else
  1106. dprint("m10g: link down\n");
  1107. }
  1108. i = gbit32(s->nrdma);
  1109. if(i != c->nrdma){
  1110. dprint("m10g: rdma timeout %ld\n", i);
  1111. c->nrdma = i;
  1112. }
  1113. }
  1114. static void
  1115. waitintx(Ctlr *c)
  1116. {
  1117. int i;
  1118. for(i = 0; i < 1024*1024; i++){
  1119. if(c->stats->valid == 0)
  1120. break;
  1121. coherence();
  1122. }
  1123. }
  1124. static void
  1125. m10ginterrupt(Ureg *, void *v)
  1126. {
  1127. Ether *e;
  1128. Ctlr *c;
  1129. e = v;
  1130. c = e->ctlr;
  1131. if(c->state != Runed || c->stats->valid == 0) /* not ready for us? */
  1132. return;
  1133. if(c->stats->valid & 1)
  1134. wakeup(&c->rxrendez);
  1135. if(gbit32(c->stats->txcnt) != c->tx.npkt)
  1136. wakeup(&c->txrendez);
  1137. if(c->msi == 0)
  1138. *c->irqdeass = 0;
  1139. else
  1140. c->stats->valid = 0;
  1141. waitintx(c);
  1142. checkstats(e, c, c->stats);
  1143. c->irqack[1] = pbit32(3);
  1144. }
  1145. static void
  1146. m10gattach(Ether *e)
  1147. {
  1148. Ctlr *c;
  1149. char name[12];
  1150. dprint("m10gattach\n");
  1151. qlock(e->ctlr);
  1152. c = e->ctlr;
  1153. if(c->state != Detached){
  1154. qunlock(c);
  1155. return;
  1156. }
  1157. if(waserror()){
  1158. c->state = Detached;
  1159. qunlock(c);
  1160. nexterror();
  1161. }
  1162. reset(e, c);
  1163. c->state = Attached;
  1164. open0(e, c);
  1165. if(c->kprocs == 0){
  1166. c->kprocs++;
  1167. snprint(name, sizeof name, "#l%drxproc", e->ctlrno);
  1168. kproc(name, m10rx, e);
  1169. snprint(name, sizeof name, "#l%dtxproc", e->ctlrno);
  1170. kproc(name, txproc, e);
  1171. }
  1172. c->state = Runed;
  1173. qunlock(c);
  1174. poperror();
  1175. }
  1176. static int
  1177. m10gdetach(Ctlr *c)
  1178. {
  1179. dprint("m10gdetach\n");
  1180. // reset(e->ctlr);
  1181. vunmap(c->ram, c->pcidev->mem[0].size);
  1182. ctlrfree(c);
  1183. return -1;
  1184. }
  1185. static int
  1186. lstcount(Block *b)
  1187. {
  1188. int i;
  1189. i = 0;
  1190. for(; b; b = b->next)
  1191. i++;
  1192. return i;
  1193. }
  1194. static long
  1195. m10gifstat(Ether *e, void *v, long n, ulong off)
  1196. {
  1197. int l;
  1198. char *p;
  1199. Ctlr *c;
  1200. Stats s;
  1201. c = e->ctlr;
  1202. p = malloc(READSTR+1);
  1203. l = 0;
  1204. /* no point in locking this because this is done via dma. */
  1205. memmove(&s, c->stats, sizeof s);
  1206. // l +=
  1207. snprint(p+l, READSTR,
  1208. "txcnt = %lud\n" "linkstat = %lud\n" "dlink = %lud\n"
  1209. "derror = %lud\n" "drunt = %lud\n" "doverrun = %lud\n"
  1210. "dnosm = %lud\n" "dnobg = %lud\n" "nrdma = %lud\n"
  1211. "txstopped = %ud\n" "down = %ud\n" "updated = %ud\n"
  1212. "valid = %ud\n\n"
  1213. "tx pkt = %lud\n" "tx bytes = %lld\n"
  1214. "tx cnt = %ud\n" "tx n = %ud\n" "tx i = %ud\n"
  1215. "sm cnt = %ud\n" "sm i = %ud\n" "sm n = %ud\n"
  1216. "sm lst = %ud\n"
  1217. "bg cnt = %ud\n" "bg i = %ud\n" "bg n = %ud\n"
  1218. "bg lst = %ud\n"
  1219. "segsz = %lud\n" "coal = %lud\n",
  1220. gbit32(s.txcnt), gbit32(s.linkstat), gbit32(s.dlink),
  1221. gbit32(s.derror), gbit32(s.drunt), gbit32(s.doverrun),
  1222. gbit32(s.dnosm), gbit32(s.dnobg), gbit32(s.nrdma),
  1223. s.txstopped, s.down, s.updated, s.valid,
  1224. c->tx.npkt, c->tx.nbytes,
  1225. c->tx.cnt, c->tx.n, c->tx.i,
  1226. c->sm.cnt, c->sm.i, c->sm.pool->n, lstcount(c->sm.pool->head),
  1227. c->bg.cnt, c->bg.i, c->bg.pool->n, lstcount(c->bg.pool->head),
  1228. c->tx.segsz, gbit32((uchar*)c->coal));
  1229. n = readstr(off, v, n, p);
  1230. free(p);
  1231. return n;
  1232. }
  1233. //static void
  1234. //summary(Ether *e)
  1235. //{
  1236. // char *buf;
  1237. // int n, i, j;
  1238. //
  1239. // if(e == 0)
  1240. // return;
  1241. // buf = malloc(n=250);
  1242. // if(buf == 0)
  1243. // return;
  1244. //
  1245. // snprint(buf, n, "oq\n");
  1246. // qsummary(e->oq, buf+3, n-3-1);
  1247. // iprint("%s", buf);
  1248. //
  1249. // if(e->f) for(i = 0; e->f[i]; i++){
  1250. // j = snprint(buf, n, "f%d %d\n", i, e->f[i]->type);
  1251. // qsummary(e->f[i]->in, buf+j, n-j-1);
  1252. // print("%s", buf);
  1253. // }
  1254. //
  1255. // free(buf);
  1256. //}
  1257. static void
  1258. rxring(Ctlr *c)
  1259. {
  1260. Done *d;
  1261. Slot *s;
  1262. Slotparts *sp;
  1263. int i;
  1264. d = &c->done;
  1265. s = d->entry;
  1266. for(i = 0; i < d->n; i++) {
  1267. sp = (Slotparts *)(s + i);
  1268. if(sp->len)
  1269. iprint("s[%d] = %d\n", i, sp->len);
  1270. }
  1271. }
  1272. enum {
  1273. CMdebug,
  1274. CMcoal,
  1275. CMwakeup,
  1276. CMtxwakeup,
  1277. CMqsummary,
  1278. CMrxring,
  1279. };
  1280. static Cmdtab ctab[] = {
  1281. CMdebug, "debug", 2,
  1282. CMcoal, "coal", 2,
  1283. CMwakeup, "wakeup", 1,
  1284. CMtxwakeup, "txwakeup", 1,
  1285. // CMqsummary, "q", 1,
  1286. CMrxring, "rxring", 1,
  1287. };
  1288. static long
  1289. m10gctl(Ether *e, void *v, long n)
  1290. {
  1291. int i;
  1292. Cmdbuf *c;
  1293. Cmdtab *t;
  1294. dprint("m10gctl\n");
  1295. if(e->ctlr == nil)
  1296. error(Enonexist);
  1297. c = parsecmd(v, n);
  1298. if(waserror()){
  1299. free(c);
  1300. nexterror();
  1301. }
  1302. t = lookupcmd(c, ctab, nelem(ctab));
  1303. switch(t->index){
  1304. case CMdebug:
  1305. debug = (strcmp(c->f[1], "on") == 0);
  1306. break;
  1307. case CMcoal:
  1308. i = atoi(c->f[1]);
  1309. if(i < 0 || i > 1000)
  1310. error(Ebadarg);
  1311. *((Ctlr*)e->ctlr)->coal = pbit32(i);
  1312. break;
  1313. case CMwakeup:
  1314. wakeup(&((Ctlr*)e->ctlr)->rxrendez); /* you're kidding, right? */
  1315. break;
  1316. case CMtxwakeup:
  1317. wakeup(&((Ctlr*)e->ctlr)->txrendez); /* you're kidding, right? */
  1318. break;
  1319. // case CMqsummary:
  1320. // summary(e);
  1321. // break;
  1322. case CMrxring:
  1323. rxring(e->ctlr);
  1324. break;
  1325. default:
  1326. error(Ebadarg);
  1327. }
  1328. free(c);
  1329. poperror();
  1330. return n;
  1331. }
  1332. static void
  1333. m10gshutdown(Ether *e)
  1334. {
  1335. dprint("m10gshutdown\n");
  1336. m10gdetach(e->ctlr);
  1337. }
  1338. static void
  1339. m10gpromiscuous(void *v, int on)
  1340. {
  1341. Ether *e;
  1342. int i;
  1343. dprint("m10gpromiscuous\n");
  1344. e = v;
  1345. if(on)
  1346. i = Cpromisc;
  1347. else
  1348. i = Cnopromisc;
  1349. cmd(e->ctlr, i, 0);
  1350. }
  1351. static int mcctab[] = { CSleavemc, CSjoinmc };
  1352. static char *mcntab[] = { "leave", "join" };
  1353. static void
  1354. m10gmulticast(void *v, uchar *ea, int on)
  1355. {
  1356. Ether *e;
  1357. int i;
  1358. dprint("m10gmulticast\n");
  1359. e = v;
  1360. if((i = maccmd(e->ctlr, mcctab[on], ea)) != 0)
  1361. print("m10g: can't %s %E: %d\n", mcntab[on], ea, i);
  1362. }
  1363. static void
  1364. m10gpci(void)
  1365. {
  1366. Pcidev *p;
  1367. Ctlr *t, *c;
  1368. t = 0;
  1369. for(p = 0; p = pcimatch(p, Vmyricom, 0); ){
  1370. switch(p->did){
  1371. case 0x8: /* 8a */
  1372. break;
  1373. case 0x9: /* 8a with msi-x fw */
  1374. case 0xa: /* 8b */
  1375. case 0xb: /* 8b2 */
  1376. case 0xc: /* 2-8b2 */
  1377. /* untested */
  1378. break;
  1379. default:
  1380. print("etherm10g: unknown myricom did %#ux\n", p->did);
  1381. continue;
  1382. }
  1383. c = malloc(sizeof *c);
  1384. if(c == nil)
  1385. continue;
  1386. c->pcidev = p;
  1387. c->id = p->did<<16 | p->vid;
  1388. c->boot = pcicap(p, PciCapVND);
  1389. // kickthebaby(p, c);
  1390. pcisetbme(p);
  1391. if(setmem(p, c) == -1){
  1392. print("m10g: setmem failed\n");
  1393. free(c);
  1394. /* cleanup */
  1395. continue;
  1396. }
  1397. if(t)
  1398. t->next = c;
  1399. else
  1400. ctlrs = c;
  1401. t = c;
  1402. }
  1403. }
  1404. static int
  1405. m10gpnp(Ether *e)
  1406. {
  1407. Ctlr *c;
  1408. if(ctlrs == nil)
  1409. m10gpci();
  1410. for(c = ctlrs; c != nil; c = c->next)
  1411. if(c->active)
  1412. continue;
  1413. else if(e->port == 0 || e->port == c->port)
  1414. break;
  1415. if(c == nil)
  1416. return -1;
  1417. c->active = 1;
  1418. e->ctlr = c;
  1419. e->port = c->port;
  1420. e->irq = c->pcidev->intl;
  1421. e->tbdf = c->pcidev->tbdf;
  1422. e->mbps = 10000;
  1423. memmove(e->ea, c->ra, Eaddrlen);
  1424. e->attach = m10gattach;
  1425. e->detach = m10gshutdown;
  1426. e->transmit = m10gtransmit;
  1427. e->interrupt = m10ginterrupt;
  1428. e->ifstat = m10gifstat;
  1429. e->ctl = m10gctl;
  1430. // e->power = m10gpower;
  1431. e->shutdown = m10gshutdown;
  1432. e->arg = e;
  1433. e->promiscuous = m10gpromiscuous;
  1434. e->multicast = m10gmulticast;
  1435. return 0;
  1436. }
  1437. void
  1438. etherm10glink(void)
  1439. {
  1440. addethercard("m10g", m10gpnp);
  1441. }