etherm10g.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  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. if(p == nil)
  1204. error(Enomem);
  1205. l = 0;
  1206. /* no point in locking this because this is done via dma. */
  1207. memmove(&s, c->stats, sizeof s);
  1208. // l +=
  1209. snprint(p+l, READSTR,
  1210. "txcnt = %lud\n" "linkstat = %lud\n" "dlink = %lud\n"
  1211. "derror = %lud\n" "drunt = %lud\n" "doverrun = %lud\n"
  1212. "dnosm = %lud\n" "dnobg = %lud\n" "nrdma = %lud\n"
  1213. "txstopped = %ud\n" "down = %ud\n" "updated = %ud\n"
  1214. "valid = %ud\n\n"
  1215. "tx pkt = %lud\n" "tx bytes = %lld\n"
  1216. "tx cnt = %ud\n" "tx n = %ud\n" "tx i = %ud\n"
  1217. "sm cnt = %ud\n" "sm i = %ud\n" "sm n = %ud\n"
  1218. "sm lst = %ud\n"
  1219. "bg cnt = %ud\n" "bg i = %ud\n" "bg n = %ud\n"
  1220. "bg lst = %ud\n"
  1221. "segsz = %lud\n" "coal = %lud\n",
  1222. gbit32(s.txcnt), gbit32(s.linkstat), gbit32(s.dlink),
  1223. gbit32(s.derror), gbit32(s.drunt), gbit32(s.doverrun),
  1224. gbit32(s.dnosm), gbit32(s.dnobg), gbit32(s.nrdma),
  1225. s.txstopped, s.down, s.updated, s.valid,
  1226. c->tx.npkt, c->tx.nbytes,
  1227. c->tx.cnt, c->tx.n, c->tx.i,
  1228. c->sm.cnt, c->sm.i, c->sm.pool->n, lstcount(c->sm.pool->head),
  1229. c->bg.cnt, c->bg.i, c->bg.pool->n, lstcount(c->bg.pool->head),
  1230. c->tx.segsz, gbit32((uchar*)c->coal));
  1231. n = readstr(off, v, n, p);
  1232. free(p);
  1233. return n;
  1234. }
  1235. //static void
  1236. //summary(Ether *e)
  1237. //{
  1238. // char *buf;
  1239. // int n, i, j;
  1240. //
  1241. // if(e == 0)
  1242. // return;
  1243. // buf = malloc(n=250);
  1244. // if(buf == 0)
  1245. // return;
  1246. //
  1247. // snprint(buf, n, "oq\n");
  1248. // qsummary(e->oq, buf+3, n-3-1);
  1249. // iprint("%s", buf);
  1250. //
  1251. // if(e->f) for(i = 0; e->f[i]; i++){
  1252. // j = snprint(buf, n, "f%d %d\n", i, e->f[i]->type);
  1253. // qsummary(e->f[i]->in, buf+j, n-j-1);
  1254. // print("%s", buf);
  1255. // }
  1256. //
  1257. // free(buf);
  1258. //}
  1259. static void
  1260. rxring(Ctlr *c)
  1261. {
  1262. Done *d;
  1263. Slot *s;
  1264. Slotparts *sp;
  1265. int i;
  1266. d = &c->done;
  1267. s = d->entry;
  1268. for(i = 0; i < d->n; i++) {
  1269. sp = (Slotparts *)(s + i);
  1270. if(sp->len)
  1271. iprint("s[%d] = %d\n", i, sp->len);
  1272. }
  1273. }
  1274. enum {
  1275. CMdebug,
  1276. CMcoal,
  1277. CMwakeup,
  1278. CMtxwakeup,
  1279. CMqsummary,
  1280. CMrxring,
  1281. };
  1282. static Cmdtab ctab[] = {
  1283. CMdebug, "debug", 2,
  1284. CMcoal, "coal", 2,
  1285. CMwakeup, "wakeup", 1,
  1286. CMtxwakeup, "txwakeup", 1,
  1287. // CMqsummary, "q", 1,
  1288. CMrxring, "rxring", 1,
  1289. };
  1290. static long
  1291. m10gctl(Ether *e, void *v, long n)
  1292. {
  1293. int i;
  1294. Cmdbuf *c;
  1295. Cmdtab *t;
  1296. dprint("m10gctl\n");
  1297. if(e->ctlr == nil)
  1298. error(Enonexist);
  1299. c = parsecmd(v, n);
  1300. if(waserror()){
  1301. free(c);
  1302. nexterror();
  1303. }
  1304. t = lookupcmd(c, ctab, nelem(ctab));
  1305. switch(t->index){
  1306. case CMdebug:
  1307. debug = (strcmp(c->f[1], "on") == 0);
  1308. break;
  1309. case CMcoal:
  1310. i = atoi(c->f[1]);
  1311. if(i < 0 || i > 1000)
  1312. error(Ebadarg);
  1313. *((Ctlr*)e->ctlr)->coal = pbit32(i);
  1314. break;
  1315. case CMwakeup:
  1316. wakeup(&((Ctlr*)e->ctlr)->rxrendez); /* you're kidding, right? */
  1317. break;
  1318. case CMtxwakeup:
  1319. wakeup(&((Ctlr*)e->ctlr)->txrendez); /* you're kidding, right? */
  1320. break;
  1321. // case CMqsummary:
  1322. // summary(e);
  1323. // break;
  1324. case CMrxring:
  1325. rxring(e->ctlr);
  1326. break;
  1327. default:
  1328. error(Ebadarg);
  1329. }
  1330. free(c);
  1331. poperror();
  1332. return n;
  1333. }
  1334. static void
  1335. m10gshutdown(Ether *e)
  1336. {
  1337. dprint("m10gshutdown\n");
  1338. m10gdetach(e->ctlr);
  1339. }
  1340. static void
  1341. m10gpromiscuous(void *v, int on)
  1342. {
  1343. Ether *e;
  1344. int i;
  1345. dprint("m10gpromiscuous\n");
  1346. e = v;
  1347. if(on)
  1348. i = Cpromisc;
  1349. else
  1350. i = Cnopromisc;
  1351. cmd(e->ctlr, i, 0);
  1352. }
  1353. static int mcctab[] = { CSleavemc, CSjoinmc };
  1354. static char *mcntab[] = { "leave", "join" };
  1355. static void
  1356. m10gmulticast(void *v, uchar *ea, int on)
  1357. {
  1358. Ether *e;
  1359. int i;
  1360. dprint("m10gmulticast\n");
  1361. e = v;
  1362. if((i = maccmd(e->ctlr, mcctab[on], ea)) != 0)
  1363. print("m10g: can't %s %E: %d\n", mcntab[on], ea, i);
  1364. }
  1365. static void
  1366. m10gpci(void)
  1367. {
  1368. Pcidev *p;
  1369. Ctlr *t, *c;
  1370. t = 0;
  1371. for(p = 0; p = pcimatch(p, Vmyricom, 0); ){
  1372. switch(p->did){
  1373. case 0x8: /* 8a */
  1374. break;
  1375. case 0x9: /* 8a with msi-x fw */
  1376. case 0xa: /* 8b */
  1377. case 0xb: /* 8b2 */
  1378. case 0xc: /* 2-8b2 */
  1379. /* untested */
  1380. break;
  1381. default:
  1382. print("etherm10g: unknown myricom did %#ux\n", p->did);
  1383. continue;
  1384. }
  1385. c = malloc(sizeof *c);
  1386. if(c == nil)
  1387. continue;
  1388. c->pcidev = p;
  1389. c->id = p->did<<16 | p->vid;
  1390. c->boot = pcicap(p, PciCapVND);
  1391. // kickthebaby(p, c);
  1392. pcisetbme(p);
  1393. if(setmem(p, c) == -1){
  1394. print("m10g: setmem failed\n");
  1395. free(c);
  1396. /* cleanup */
  1397. continue;
  1398. }
  1399. if(t)
  1400. t->next = c;
  1401. else
  1402. ctlrs = c;
  1403. t = c;
  1404. }
  1405. }
  1406. static int
  1407. m10gpnp(Ether *e)
  1408. {
  1409. Ctlr *c;
  1410. if(ctlrs == nil)
  1411. m10gpci();
  1412. for(c = ctlrs; c != nil; c = c->next)
  1413. if(c->active)
  1414. continue;
  1415. else if(e->port == 0 || e->port == c->port)
  1416. break;
  1417. if(c == nil)
  1418. return -1;
  1419. c->active = 1;
  1420. e->ctlr = c;
  1421. e->port = c->port;
  1422. e->irq = c->pcidev->intl;
  1423. e->tbdf = c->pcidev->tbdf;
  1424. e->mbps = 10000;
  1425. memmove(e->ea, c->ra, Eaddrlen);
  1426. e->attach = m10gattach;
  1427. e->detach = m10gshutdown;
  1428. e->transmit = m10gtransmit;
  1429. e->interrupt = m10ginterrupt;
  1430. e->ifstat = m10gifstat;
  1431. e->ctl = m10gctl;
  1432. // e->power = m10gpower;
  1433. e->shutdown = m10gshutdown;
  1434. e->arg = e;
  1435. e->promiscuous = m10gpromiscuous;
  1436. e->multicast = m10gmulticast;
  1437. return 0;
  1438. }
  1439. void
  1440. etherm10glink(void)
  1441. {
  1442. addethercard("m10g", m10gpnp);
  1443. }