etherm10g.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. /*
  2. * myricom 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("pciecap offset = %ud\n", off);
  260. if(off < 0x100 || off >= 4*KiB - 1)
  261. return 0;
  262. }
  263. print("pciecap found = %ud\n", 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("%d offset\n", off);
  294. cap = 0;
  295. if(off != 0){
  296. off += AercCCR;
  297. cap = pcicfgr32(p, off);
  298. print("%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. return 4*KiB;
  313. }
  314. if(ecrc & 10){
  315. print("fw = 4096 [ecrc set]\n");
  316. return 4*KiB;
  317. }
  318. print("fw = 4096 [default]\n");
  319. return 4*KiB;
  320. }
  321. static int
  322. parseeprom(Ctlr *c)
  323. {
  324. int i, j, k, l, bits;
  325. char *s;
  326. dprint("m10g eprom:\n");
  327. s = c->eprom;
  328. bits = 3;
  329. for(i = 0; s[i] && i < Epromsz; i++){
  330. l = strlen(s+i);
  331. dprint("\t%s\n", s+i);
  332. if(strncmp(s+i, "MAC=", 4) == 0 && l == 4+12+5){
  333. bits ^= 1;
  334. j = i + 4;
  335. for(k = 0; k < 6; k++)
  336. c->ra[k] = strtoul(s+j+3*k, 0, 16);
  337. }else if(strncmp(s+i, "SN=", 3) == 0){
  338. bits ^= 2;
  339. c->serial = atoi(s+i+3);
  340. }
  341. i += l;
  342. }
  343. if(bits)
  344. return -1;
  345. return 0;
  346. }
  347. static ushort
  348. pbit16(ushort i)
  349. {
  350. ushort j;
  351. uchar *p;
  352. p = (uchar*)&j;
  353. p[1] = i;
  354. p[0] = i>>8;
  355. return j;
  356. }
  357. static ushort
  358. gbit16(uchar i[2])
  359. {
  360. ushort j;
  361. j = i[1];
  362. j |= i[0]<<8;
  363. return j;
  364. }
  365. static ulong
  366. pbit32(ulong i)
  367. {
  368. ulong j;
  369. uchar *p;
  370. p = (uchar*)&j;
  371. p[3] = i;
  372. p[2] = i>>8;
  373. p[1] = i>>16;
  374. p[0] = i>>24;
  375. return j;
  376. }
  377. static ulong
  378. gbit32(uchar i[4])
  379. {
  380. ulong j;
  381. j = i[3];
  382. j |= i[2]<<8;
  383. j |= i[1]<<16;
  384. j |= i[0]<<24;
  385. return j;
  386. }
  387. static void
  388. prepcmd(ulong *cmd, int i)
  389. {
  390. while(i-- > 0)
  391. cmd[i] = pbit32(cmd[i]);
  392. }
  393. /*
  394. * the command looks like this (int 32bit integers)
  395. * cmd type
  396. * addr (low)
  397. * addr (high)
  398. * pad (used for dma testing)
  399. * response (high)
  400. * response (low)
  401. * 40 byte = 5 int pad.
  402. */
  403. ulong
  404. cmd(Ctlr *c, int type, uvlong data)
  405. {
  406. ulong buf[16], i;
  407. Cmd *cmd;
  408. qlock(&c->cmdl);
  409. cmd = c->cmd;
  410. cmd->i[1] = Noconf;
  411. memset(buf, 0, sizeof buf);
  412. buf[0] = type;
  413. buf[1] = data;
  414. buf[2] = data >> 32;
  415. buf[4] = c->cprt >> 32;
  416. buf[5] = c->cprt;
  417. prepcmd(buf, 6);
  418. coherence();
  419. memmove(c->ram + Cmdoff, buf, sizeof buf);
  420. if(waserror())
  421. nexterror();
  422. for(i = 0; i < 15; i++){
  423. if(cmd->i[1] != Noconf){
  424. poperror();
  425. i = gbit32(cmd->c);
  426. qunlock(&c->cmdl);
  427. if(cmd->i[1] != 0)
  428. dprint("[%lux]", i);
  429. return i;
  430. }
  431. tsleep(&up->sleep, return0, 0, 1);
  432. }
  433. qunlock(&c->cmdl);
  434. iprint("m10g: cmd timeout [%ux %ux] cmd=%d\n",
  435. cmd->i[0], cmd->i[1], type);
  436. error(Etimeout);
  437. return ~0; /* silence! */
  438. }
  439. ulong
  440. maccmd(Ctlr *c, int type, uchar *m)
  441. {
  442. ulong buf[16], i;
  443. Cmd *cmd;
  444. qlock(&c->cmdl);
  445. cmd = c->cmd;
  446. cmd->i[1] = Noconf;
  447. memset(buf, 0, sizeof buf);
  448. buf[0] = type;
  449. buf[1] = m[0]<<24 | m[1]<<16 | m[2]<<8 | m[3];
  450. buf[2] = m[4]<< 8 | m[5];
  451. buf[4] = c->cprt >> 32;
  452. buf[5] = c->cprt;
  453. prepcmd(buf, 6);
  454. coherence();
  455. memmove(c->ram + Cmdoff, buf, sizeof buf);
  456. if(waserror())
  457. nexterror();
  458. for(i = 0; i < 15; i++){
  459. if(cmd->i[1] != Noconf){
  460. poperror();
  461. i = gbit32(cmd->c);
  462. qunlock(&c->cmdl);
  463. if(cmd->i[1] != 0)
  464. dprint("[%lux]", i);
  465. return i;
  466. }
  467. tsleep(&up->sleep, return0, 0, 1);
  468. }
  469. qunlock(&c->cmdl);
  470. iprint("m10g: maccmd timeout [%ux %ux] cmd=%d\n",
  471. cmd->i[0], cmd->i[1], type);
  472. error(Etimeout);
  473. return ~0; /* silence! */
  474. }
  475. /* remove this garbage after testing */
  476. enum {
  477. DMAread = 0x10000,
  478. DMAwrite= 0x1,
  479. };
  480. ulong
  481. dmatestcmd(Ctlr *c, int type, uvlong addr, int len)
  482. {
  483. ulong buf[16], i;
  484. memset(buf, 0, sizeof buf);
  485. memset(c->cmd, Noconf, sizeof *c->cmd);
  486. buf[0] = Cdmatest;
  487. buf[1] = addr;
  488. buf[2] = addr >> 32;
  489. buf[3] = len * type;
  490. buf[4] = c->cprt >> 32;
  491. buf[5] = c->cprt;
  492. prepcmd(buf, 6);
  493. coherence();
  494. memmove(c->ram + Cmdoff, buf, sizeof buf);
  495. if(waserror())
  496. nexterror();
  497. for(i = 0; i < 15; i++){
  498. if(c->cmd->i[1] != Noconf){
  499. i = gbit32(c->cmd->c);
  500. if(i == 0)
  501. error(Eio);
  502. poperror();
  503. return i;
  504. }
  505. tsleep(&up->sleep, return0, 0, 5);
  506. }
  507. error(Etimeout);
  508. return ~0; /* silence! */
  509. }
  510. ulong
  511. rdmacmd(Ctlr *c, int on)
  512. {
  513. ulong buf[16], i;
  514. memset(buf, 0, sizeof buf);
  515. c->cmd->i[0] = 0;
  516. coherence();
  517. buf[0] = c->cprt >> 32;
  518. buf[1] = c->cprt;
  519. buf[2] = Noconf;
  520. buf[3] = c->cprt >> 32;
  521. buf[4] = c->cprt;
  522. buf[5] = on;
  523. prepcmd(buf, 6);
  524. memmove(c->ram + Rdmaoff, buf, sizeof buf);
  525. if(waserror())
  526. nexterror();
  527. for(i = 0; i < 20; i++){
  528. if(c->cmd->i[0] == Noconf){
  529. poperror();
  530. return gbit32(c->cmd->c);
  531. }
  532. tsleep(&up->sleep, return0, 0, 1);
  533. }
  534. error(Etimeout);
  535. iprint("m10g: rdmacmd timeout\n");
  536. return ~0; /* silence! */
  537. }
  538. static int
  539. loadfw(Ctlr *c, int *align)
  540. {
  541. ulong *f, *s, sz;
  542. int i;
  543. if((*align = whichfw(c->pcidev)) == 4*KiB){
  544. f = (ulong*)fw4k;
  545. sz = sizeof fw4k;
  546. }else{
  547. f = (ulong*)fw2k;
  548. sz = sizeof fw2k;
  549. }
  550. s = (ulong*)(c->ram + Fwoffset);
  551. for(i = 0; i < sz / 4; i++)
  552. s[i] = f[i];
  553. return sz & ~3;
  554. }
  555. static int
  556. bootfw(Ctlr *c)
  557. {
  558. int i, sz, align;
  559. ulong buf[16];
  560. Cmd* cmd;
  561. if((sz = loadfw(c, &align)) == 0)
  562. return 0;
  563. dprint("bootfw %d bytes ... ", sz);
  564. cmd = c->cmd;
  565. memset(buf, 0, sizeof buf);
  566. c->cmd->i[0] = 0;
  567. coherence();
  568. buf[0] = c->cprt >> 32; /* upper dma target address */
  569. buf[1] = c->cprt; /* lower */
  570. buf[2] = Noconf; /* writeback */
  571. buf[3] = Fwoffset + 8,
  572. buf[4] = sz - 8;
  573. buf[5] = 8;
  574. buf[6] = 0;
  575. prepcmd(buf, 7);
  576. coherence();
  577. memmove(c->ram + Fwsubmt, buf, sizeof buf);
  578. for(i = 0; i < 20; i++){
  579. if(cmd->i[0] == Noconf)
  580. break;
  581. delay(1);
  582. }
  583. dprint("[%lux %lux]", gbit32(cmd->c), gbit32(cmd->c+4));
  584. if(i == 20){
  585. print("m10g: cannot load fw\n");
  586. return -1;
  587. }
  588. dprint("\n");
  589. c->tx.segsz = align;
  590. return 0;
  591. }
  592. static int
  593. kickthebaby(Pcidev *p, Ctlr *c)
  594. {
  595. /* don't kick the baby! */
  596. ulong code;
  597. pcicfgw8(p, 0x10 + c->boot, 0x3);
  598. pcicfgw32(p, 0x18 + c->boot, 0xfffffff0);
  599. code = pcicfgr32(p, 0x14 + c->boot);
  600. dprint("reboot status = %lux\n", code);
  601. if(code != 0xfffffff0)
  602. return -1;
  603. return 0;
  604. }
  605. typedef struct {
  606. uchar len[4];
  607. uchar type[4];
  608. char version[128];
  609. uchar globals[4];
  610. uchar ramsz[4];
  611. uchar specs[4];
  612. uchar specssz[4];
  613. } Fwhdr;
  614. enum {
  615. Tmx = 0x4d582020,
  616. Tpcie = 0x70636965,
  617. Teth = 0x45544820,
  618. Tmcp0 = 0x4d435030,
  619. };
  620. static char *
  621. fwtype(ulong type)
  622. {
  623. switch(type){
  624. case Tmx:
  625. return "mx";
  626. case Tpcie:
  627. return "PCIe";
  628. case Teth:
  629. return "eth";
  630. case Tmcp0:
  631. return "mcp0";
  632. }
  633. return "*GOK*";
  634. }
  635. static int
  636. chkfw(Ctlr *c)
  637. {
  638. uintptr off;
  639. Fwhdr *h;
  640. ulong type;
  641. off = gbit32(c->ram+0x3c);
  642. dprint("firmware %llux\n", (uvlong)off);
  643. if((off&3) || off + sizeof *h > c->ramsz){
  644. print("!m10g: bad firmware %llux\n", (uvlong)off);
  645. return -1;
  646. }
  647. h = (Fwhdr*)(c->ram + off);
  648. type = gbit32(h->type);
  649. dprint("\t" "type %s\n", fwtype(type));
  650. dprint("\t" "vers %s\n", h->version);
  651. dprint("\t" "ramsz %lux\n", gbit32(h->ramsz));
  652. if(type != Teth){
  653. print("!m10g: bad card type %s\n", fwtype(type));
  654. return -1;
  655. }
  656. return bootfw(c) || rdmacmd(c, 0);
  657. }
  658. static int
  659. reset(Ether *e, Ctlr *c)
  660. {
  661. ulong i, sz;
  662. if(waserror()){
  663. print("m10g: reset error\n");
  664. nexterror();
  665. return -1;
  666. }
  667. chkfw(c);
  668. cmd(c, Creset, 0);
  669. cmd(c, CSintrqsz, c->done.n * sizeof *c->done.entry);
  670. cmd(c, CSintrqdma, c->done.busaddr);
  671. c->irqack = (ulong*)(c->ram + cmd(c, CGirqackoff, 0));
  672. /* required only if we're not doing msi? */
  673. c->irqdeass = (ulong*)(c->ram + cmd(c, CGirqdeassoff, 0));
  674. /* this is the driver default, why fiddle with this? */
  675. c->coal = (ulong*)(c->ram + cmd(c, CGcoaloff, 0));
  676. *c->coal = pbit32(25);
  677. dprint("dma stats:\n");
  678. rdmacmd(c, 1);
  679. sz = c->tx.segsz;
  680. i = dmatestcmd(c, DMAread, c->done.busaddr, sz);
  681. print("\t" "read: %lud MB/s\n", ((i>>16)*sz*2) / (i&0xffff));
  682. i = dmatestcmd(c, DMAwrite, c->done.busaddr, sz);
  683. print("\t" "write: %lud MB/s\n", ((i>>16)*sz*2) / (i&0xffff));
  684. i = dmatestcmd(c, DMAwrite|DMAread, c->done.busaddr, sz);
  685. print("\t" "r/w: %lud MB/s\n", ((i>>16)*sz*2*2) / (i&0xffff));
  686. memset(c->done.entry, 0, c->done.n * sizeof *c->done.entry);
  687. maccmd(c, CSmac, c->ra);
  688. // cmd(c, Cnopromisc, 0);
  689. cmd(c, Cenablefc, 0);
  690. e->maxmtu = Maxmtu;
  691. cmd(c, CSmtu, e->maxmtu);
  692. dprint("CSmtu %d...\n", e->maxmtu);
  693. poperror();
  694. return 0;
  695. }
  696. static void
  697. ctlrfree(Ctlr *c)
  698. {
  699. /* free up all the Block*s, too */
  700. free(c->tx.host);
  701. free(c->sm.host);
  702. free(c->bg.host);
  703. free(c->cmd);
  704. free(c->done.entry);
  705. free(c->stats);
  706. free(c);
  707. }
  708. static int
  709. setmem(Pcidev *p, Ctlr *c)
  710. {
  711. ulong i;
  712. uvlong raddr;
  713. Done *d;
  714. void *mem;
  715. c->tx.segsz = 2048;
  716. c->ramsz = 2*MiB - (2*48*KiB + 32*KiB) - 0x100;
  717. if(c->ramsz > p->mem[0].size)
  718. return -1;
  719. raddr = p->mem[0].bar & ~0x0F;
  720. mem = vmap(raddr, p->mem[0].size);
  721. if(mem == nil){
  722. print("m10g: can't map %8.8lux\n", p->mem[0].bar);
  723. return -1;
  724. }
  725. dprint("%llux <- vmap(mem[0].size = %ux)\n", raddr, p->mem[0].size);
  726. c->port = raddr;
  727. c->ram = mem;
  728. c->cmd = malign(sizeof *c->cmd);
  729. c->cprt = PCIWADDR(c->cmd);
  730. d = &c->done;
  731. d->n = Maxslots;
  732. d->m = d->n - 1;
  733. i = d->n * sizeof *d->entry;
  734. d->entry = malign(i);
  735. memset(d->entry, 0, i);
  736. d->busaddr = PCIWADDR(d->entry);
  737. c->stats = malign(sizeof *c->stats);
  738. memset(c->stats, 0, sizeof *c->stats);
  739. c->statsprt = PCIWADDR(c->stats);
  740. memmove(c->eprom, c->ram + c->ramsz - Epromsz, Epromsz-2);
  741. return setpcie(p) || parseeprom(c);
  742. }
  743. static Rx*
  744. whichrx(Ctlr *c, int sz)
  745. {
  746. if(sz <= smpool.size)
  747. return &c->sm;
  748. return &c->bg;
  749. }
  750. static Block*
  751. balloc(Rx* rx)
  752. {
  753. Block *b;
  754. ilock(rx->pool);
  755. if((b = rx->pool->head) != nil){
  756. rx->pool->head = b->next;
  757. b->next = nil;
  758. rx->pool->n--;
  759. }
  760. iunlock(rx->pool);
  761. return b;
  762. }
  763. static void
  764. smbfree(Block *b)
  765. {
  766. Bpool *p;
  767. b->rp = b->wp = (uchar*)PGROUND((uintptr)b->base);
  768. p = &smpool;
  769. ilock(p);
  770. b->next = p->head;
  771. p->head = b;
  772. p->n++;
  773. p->cnt++;
  774. iunlock(p);
  775. }
  776. static void
  777. bgbfree(Block *b)
  778. {
  779. Bpool *p;
  780. b->rp = b->wp = (uchar*)PGROUND((uintptr)b->base);
  781. p = &bgpool;
  782. ilock(p);
  783. b->next = p->head;
  784. p->head = b;
  785. p->n++;
  786. p->cnt++;
  787. iunlock(p);
  788. }
  789. static void
  790. replenish(Rx *rx)
  791. {
  792. ulong buf[16], i, idx, e;
  793. Bpool *p;
  794. Block *b;
  795. p = rx->pool;
  796. if(p->n < 8)
  797. return;
  798. memset(buf, 0, sizeof buf);
  799. e = (rx->i - rx->cnt) & ~7;
  800. e += rx->n;
  801. while(p->n >= 8 && e){
  802. idx = rx->cnt & rx->m;
  803. for(i = 0; i < 8; i++){
  804. b = balloc(rx);
  805. buf[i*2] = pbit32((uvlong)PCIWADDR(b->wp) >> 32);
  806. buf[i*2+1] = pbit32(PCIWADDR(b->wp));
  807. rx->host[idx+i] = b;
  808. assert(b);
  809. }
  810. memmove(rx->lanai + 2*idx, buf, sizeof buf);
  811. coherence();
  812. rx->cnt += 8;
  813. e -= 8;
  814. }
  815. if(e && p->n > 7+1)
  816. print("should panic? pool->n = %d\n", p->n);
  817. }
  818. /*
  819. * future:
  820. * if (c->mtrr >= 0) {
  821. * c->tx.wcfifo = c->ram+0x200000;
  822. * c->sm.wcfifo = c->ram+0x300000;
  823. * c->bg.wcfifo = c->ram+0x340000;
  824. * }
  825. */
  826. static int
  827. nextpow(int j)
  828. {
  829. int i;
  830. for(i = 0; j > (1 << i); i++)
  831. ;
  832. return 1 << i;
  833. }
  834. static void*
  835. emalign(int sz)
  836. {
  837. void *v;
  838. v = malign(sz);
  839. if(v == nil)
  840. error(Enomem);
  841. memset(v, 0, sz);
  842. return v;
  843. }
  844. static void
  845. open0(Ether *e, Ctlr *c)
  846. {
  847. Block *b;
  848. int i, sz, entries;
  849. entries = cmd(c, CGsendrgsz, 0) / sizeof *c->tx.lanai;
  850. c->tx.lanai = (Send*)(c->ram + cmd(c, CGsendoff, 0));
  851. c->tx.host = emalign(entries * sizeof *c->tx.host);
  852. c->tx.bring = emalign(entries * sizeof *c->tx.bring);
  853. c->tx.n = entries;
  854. c->tx.m = entries-1;
  855. entries = cmd(c, CGrxrgsz, 0)/8;
  856. c->sm.pool = &smpool;
  857. cmd(c, CSsmallsz, c->sm.pool->size);
  858. c->sm.lanai = (ulong*)(c->ram + cmd(c, CGsmallrxoff, 0));
  859. c->sm.n = entries;
  860. c->sm.m = entries-1;
  861. c->sm.host = emalign(entries * sizeof *c->sm.host);
  862. c->bg.pool = &bgpool;
  863. c->bg.pool->size = nextpow(2 + e->maxmtu); /* 2-byte alignment pad */
  864. cmd(c, CSbigsz, c->bg.pool->size);
  865. c->bg.lanai = (ulong*)(c->ram + cmd(c, CGbigrxoff, 0));
  866. c->bg.n = entries;
  867. c->bg.m = entries-1;
  868. c->bg.host = emalign(entries * sizeof *c->bg.host);
  869. sz = c->sm.pool->size + BY2PG;
  870. for(i = 0; i < c->sm.n; i++){
  871. if((b = allocb(sz)) == 0)
  872. break;
  873. b->free = smbfree;
  874. freeb(b);
  875. }
  876. sz = c->bg.pool->size + BY2PG;
  877. for(i = 0; i < c->bg.n; i++){
  878. if((b = allocb(sz)) == 0)
  879. break;
  880. b->free = bgbfree;
  881. freeb(b);
  882. }
  883. cmd(c, CSstatsdma, c->statsprt);
  884. c->linkstat = ~0;
  885. c->nrdma = 15;
  886. cmd(c, Cetherup, 0);
  887. }
  888. static Block*
  889. nextblock(Ctlr *c)
  890. {
  891. uint i;
  892. ushort l, k;
  893. Block *b;
  894. Done *d;
  895. Rx *rx;
  896. Slot *s;
  897. Slotparts *sp;
  898. d = &c->done;
  899. s = d->entry;
  900. i = d->i & d->m;
  901. sp = (Slotparts *)(s + i);
  902. l = sp->len;
  903. if(l == 0)
  904. return 0;
  905. k = sp->cksum;
  906. s[i] = 0;
  907. d->i++;
  908. l = gbit16((uchar*)&l);
  909. //dprint("nextb: i=%d l=%d\n", d->i, l);
  910. rx = whichrx(c, l);
  911. if(rx->i >= rx->cnt){
  912. iprint("m10g: overrun\n");
  913. return 0;
  914. }
  915. i = rx->i & rx->m;
  916. b = rx->host[i];
  917. rx->host[i] = 0;
  918. if(b == 0){
  919. iprint("m10g: error rx to no block. memory is hosed.\n");
  920. return 0;
  921. }
  922. rx->i++;
  923. b->flag |= Bipck|Btcpck|Budpck;
  924. b->checksum = k;
  925. b->rp += 2;
  926. b->wp += 2+l;
  927. b->lim = b->wp; /* lie like a dog. */
  928. return b;
  929. }
  930. static int
  931. rxcansleep(void *v)
  932. {
  933. Ctlr *c;
  934. Slot *s;
  935. Slotparts *sp;
  936. Done *d;
  937. c = v;
  938. d = &c->done;
  939. s = c->done.entry;
  940. sp = (Slotparts *)(s + (d->i & d->m));
  941. if(sp->len != 0)
  942. return -1;
  943. c->irqack[0] = pbit32(3);
  944. return 0;
  945. }
  946. static void
  947. m10rx(void *v)
  948. {
  949. Ether *e;
  950. Ctlr *c;
  951. Block *b;
  952. e = v;
  953. c = e->ctlr;
  954. for(;;){
  955. replenish(&c->sm);
  956. replenish(&c->bg);
  957. sleep(&c->rxrendez, rxcansleep, c);
  958. while(b = nextblock(c))
  959. etheriq(e, b, 1);
  960. }
  961. }
  962. static void
  963. txcleanup(Tx *tx, ulong n)
  964. {
  965. Block *b;
  966. uint j, l, m;
  967. if(tx->npkt == n)
  968. return;
  969. l = 0;
  970. m = tx->m;
  971. /*
  972. * if tx->cnt == tx->i, yet tx->npkt == n-1, we just
  973. * caught ourselves and myricom card updating.
  974. */
  975. for(;; tx->cnt++){
  976. j = tx->cnt & tx->m;
  977. if(b = tx->bring[j]){
  978. tx->bring[j] = 0;
  979. tx->nbytes += BLEN(b);
  980. freeb(b);
  981. if(++tx->npkt == n)
  982. return;
  983. }
  984. if(tx->cnt == tx->i)
  985. return;
  986. if(l++ == m){
  987. iprint("tx ovrun: %lud %lud\n", n, tx->npkt);
  988. return;
  989. }
  990. }
  991. }
  992. static int
  993. txcansleep(void *v)
  994. {
  995. Ctlr *c;
  996. c = v;
  997. if(c->tx.cnt != c->tx.i && c->tx.npkt != gbit32(c->stats->txcnt))
  998. return -1;
  999. return 0;
  1000. }
  1001. static void
  1002. txproc(void *v)
  1003. {
  1004. Ether *e;
  1005. Ctlr *c;
  1006. Tx *tx;
  1007. e = v;
  1008. c = e->ctlr;
  1009. tx = &c->tx;
  1010. for(;;){
  1011. sleep(&c->txrendez, txcansleep, c);
  1012. txcleanup(tx, gbit32(c->stats->txcnt));
  1013. }
  1014. }
  1015. static void
  1016. submittx(Tx *tx, int n)
  1017. {
  1018. Send *l, *h;
  1019. int i0, i, m;
  1020. m = tx->m;
  1021. i0 = tx->i & m;
  1022. l = tx->lanai;
  1023. h = tx->host;
  1024. for(i = n-1; i >= 0; i--)
  1025. memmove(l+(i + i0 & m), h+(i + i0 & m), sizeof *h);
  1026. tx->i += n;
  1027. // coherence();
  1028. }
  1029. static int
  1030. nsegments(Block *b, int segsz)
  1031. {
  1032. uintptr bus, end, slen, len;
  1033. int i;
  1034. bus = PCIWADDR(b->rp);
  1035. i = 0;
  1036. for(len = BLEN(b); len; len -= slen){
  1037. end = bus + segsz & ~(segsz-1);
  1038. slen = end - bus;
  1039. if(slen > len)
  1040. slen = len;
  1041. bus += slen;
  1042. i++;
  1043. }
  1044. return i;
  1045. }
  1046. static void
  1047. m10gtransmit(Ether *e)
  1048. {
  1049. ushort slen;
  1050. ulong i, cnt, rdma, nseg, count, end, bus, len, segsz;
  1051. uchar flags;
  1052. Block *b;
  1053. Ctlr *c;
  1054. Send *s, *s0, *s0m8;
  1055. Tx *tx;
  1056. c = e->ctlr;
  1057. tx = &c->tx;
  1058. segsz = tx->segsz;
  1059. qlock(tx);
  1060. count = 0;
  1061. s = tx->host + (tx->i & tx->m);
  1062. cnt = tx->cnt;
  1063. s0 = tx->host + (cnt & tx->m);
  1064. s0m8 = tx->host + ((cnt - 8) & tx->m);
  1065. i = tx->i;
  1066. for(; s >= s0 || s < s0m8; i += nseg){
  1067. if((b = qget(e->oq)) == nil)
  1068. break;
  1069. flags = SFfirst|SFnotso;
  1070. if((len = BLEN(b)) < 1520)
  1071. flags |= SFsmall;
  1072. rdma = nseg = nsegments(b, segsz);
  1073. bus = PCIWADDR(b->rp);
  1074. for(; len; len -= slen){
  1075. end = bus + segsz & ~(segsz-1);
  1076. slen = end - bus;
  1077. if(slen > len)
  1078. slen = len;
  1079. s->low = pbit32(bus);
  1080. s->len = pbit16(slen);
  1081. s->nrdma = rdma;
  1082. s->flags = flags;
  1083. bus += slen;
  1084. if(++s == tx->host + tx->n)
  1085. s = tx->host;
  1086. count++;
  1087. flags &= ~SFfirst;
  1088. rdma = 1;
  1089. }
  1090. tx->bring[i + nseg - 1 & tx->m] = b;
  1091. if(1 || count > 0){
  1092. submittx(tx, count);
  1093. count = 0;
  1094. cnt = tx->cnt;
  1095. s0 = tx->host + (cnt & tx->m);
  1096. s0m8 = tx->host + ((cnt - 8) & tx->m);
  1097. }
  1098. }
  1099. qunlock(tx);
  1100. }
  1101. static void
  1102. checkstats(Ether *e, Ctlr *c, Stats *s)
  1103. {
  1104. ulong i;
  1105. if(s->updated == 0)
  1106. return;
  1107. i = gbit32(s->linkstat);
  1108. if(c->linkstat != i){
  1109. e->link = i;
  1110. if(c->linkstat = i)
  1111. dprint("m10g: link up\n");
  1112. else
  1113. dprint("m10g: link down\n");
  1114. }
  1115. i = gbit32(s->nrdma);
  1116. if(i != c->nrdma){
  1117. dprint("m10g: rdma timeout %ld\n", i);
  1118. c->nrdma = i;
  1119. }
  1120. }
  1121. static void
  1122. waitintx(Ctlr *c)
  1123. {
  1124. int i;
  1125. for(i = 0; i < 1024*1024; i++){
  1126. if(c->stats->valid == 0)
  1127. break;
  1128. coherence();
  1129. }
  1130. }
  1131. static void
  1132. m10ginterrupt(Ureg *, void *v)
  1133. {
  1134. Ether *e;
  1135. Ctlr *c;
  1136. e = v;
  1137. c = e->ctlr;
  1138. if(c->state != Runed || c->stats->valid == 0) /* not ready for us? */
  1139. return;
  1140. if(c->stats->valid & 1)
  1141. wakeup(&c->rxrendez);
  1142. if(gbit32(c->stats->txcnt) != c->tx.npkt)
  1143. wakeup(&c->txrendez);
  1144. if(c->msi == 0)
  1145. *c->irqdeass = 0;
  1146. else
  1147. c->stats->valid = 0;
  1148. waitintx(c);
  1149. checkstats(e, c, c->stats);
  1150. c->irqack[1] = pbit32(3);
  1151. }
  1152. static void
  1153. m10gattach(Ether *e)
  1154. {
  1155. Ctlr *c;
  1156. char name[12];
  1157. dprint("m10gattach\n");
  1158. qlock(e->ctlr);
  1159. c = e->ctlr;
  1160. if(c->state != Detached){
  1161. qunlock(c);
  1162. return;
  1163. }
  1164. if(waserror()){
  1165. c->state = Detached;
  1166. qunlock(c);
  1167. nexterror();
  1168. }
  1169. reset(e, c);
  1170. c->state = Attached;
  1171. open0(e, c);
  1172. if(c->kprocs == 0){
  1173. c->kprocs++;
  1174. snprint(name, sizeof name, "#l%drxproc", e->ctlrno);
  1175. kproc(name, m10rx, e);
  1176. snprint(name, sizeof name, "#l%dtxproc", e->ctlrno);
  1177. kproc(name, txproc, e);
  1178. }
  1179. c->state = Runed;
  1180. qunlock(c);
  1181. poperror();
  1182. }
  1183. static int
  1184. m10gdetach(Ctlr *c)
  1185. {
  1186. dprint("m10gdetach\n");
  1187. // reset(e->ctlr);
  1188. vunmap(c->ram, c->pcidev->mem[0].size);
  1189. ctlrfree(c);
  1190. return -1;
  1191. }
  1192. static int
  1193. lstcount(Block *b)
  1194. {
  1195. int i;
  1196. i = 0;
  1197. for(; b; b = b->next)
  1198. i++;
  1199. return i;
  1200. }
  1201. static long
  1202. m10gifstat(Ether *e, void *v, long n, ulong off)
  1203. {
  1204. int l, lim;
  1205. char *p;
  1206. Ctlr *c;
  1207. Stats s;
  1208. c = e->ctlr;
  1209. lim = 2*READSTR-1;
  1210. p = malloc(lim+1);
  1211. l = 0;
  1212. /* no point in locking this because this is done via dma. */
  1213. memmove(&s, c->stats, sizeof s);
  1214. // l +=
  1215. snprint(p+l, lim,
  1216. "txcnt = %lud\n" "linkstat = %lud\n" "dlink = %lud\n"
  1217. "derror = %lud\n" "drunt = %lud\n" "doverrun = %lud\n"
  1218. "dnosm = %lud\n" "dnobg = %lud\n" "nrdma = %lud\n"
  1219. "txstopped = %ud\n" "down = %ud\n" "updated = %ud\n"
  1220. "valid = %ud\n\n"
  1221. "tx pkt = %lud\n" "tx bytes = %lld\n"
  1222. "tx cnt = %ud\n" "tx n = %ud\n" "tx i = %ud\n"
  1223. "sm cnt = %ud\n" "sm i = %ud\n" "sm n = %ud\n"
  1224. "sm lst = %ud\n"
  1225. "bg cnt = %ud\n" "bg i = %ud\n" "bg n = %ud\n"
  1226. "bg lst = %ud\n"
  1227. "segsz = %lud\n" "coal = %lud\n",
  1228. gbit32(s.txcnt), gbit32(s.linkstat), gbit32(s.dlink),
  1229. gbit32(s.derror), gbit32(s.drunt), gbit32(s.doverrun),
  1230. gbit32(s.dnosm), gbit32(s.dnobg), gbit32(s.nrdma),
  1231. s.txstopped, s.down, s.updated, s.valid,
  1232. c->tx.npkt, c->tx.nbytes,
  1233. c->tx.cnt, c->tx.n, c->tx.i,
  1234. c->sm.cnt, c->sm.i, c->sm.pool->n, lstcount(c->sm.pool->head),
  1235. c->bg.cnt, c->bg.i, c->bg.pool->n, lstcount(c->bg.pool->head),
  1236. c->tx.segsz, gbit32((uchar*)c->coal));
  1237. n = readstr(off, v, n, p);
  1238. free(p);
  1239. return n;
  1240. }
  1241. //static void
  1242. //summary(Ether *e)
  1243. //{
  1244. // char *buf;
  1245. // int n, i, j;
  1246. //
  1247. // if(e == 0)
  1248. // return;
  1249. // buf = malloc(n=250);
  1250. // if(buf == 0)
  1251. // return;
  1252. //
  1253. // snprint(buf, n, "oq\n");
  1254. // qsummary(e->oq, buf+3, n-3-1);
  1255. // iprint("%s", buf);
  1256. //
  1257. // if(e->f) for(i = 0; e->f[i]; i++){
  1258. // j = snprint(buf, n, "f%d %d\n", i, e->f[i]->type);
  1259. // qsummary(e->f[i]->in, buf+j, n-j-1);
  1260. // print("%s", buf);
  1261. // }
  1262. //
  1263. // free(buf);
  1264. //}
  1265. static void
  1266. rxring(Ctlr *c)
  1267. {
  1268. Done *d;
  1269. Slot *s;
  1270. Slotparts *sp;
  1271. int i;
  1272. d = &c->done;
  1273. s = d->entry;
  1274. for(i = 0; i < d->n; i++) {
  1275. sp = (Slotparts *)(s + i);
  1276. if(sp->len)
  1277. iprint("s[%d] = %d\n", i, sp->len);
  1278. }
  1279. }
  1280. enum {
  1281. CMdebug,
  1282. CMcoal,
  1283. CMwakeup,
  1284. CMtxwakeup,
  1285. CMqsummary,
  1286. CMrxring,
  1287. };
  1288. static Cmdtab ctab[] = {
  1289. CMdebug, "debug", 2,
  1290. CMcoal, "coal", 2,
  1291. CMwakeup, "wakeup", 1,
  1292. CMtxwakeup, "txwakeup", 1,
  1293. // CMqsummary, "q", 1,
  1294. CMrxring, "rxring", 1,
  1295. };
  1296. static long
  1297. m10gctl(Ether *e, void *v, long n)
  1298. {
  1299. int i;
  1300. Cmdbuf *c;
  1301. Cmdtab *t;
  1302. dprint("m10gctl\n");
  1303. if(e->ctlr == nil)
  1304. error(Enonexist);
  1305. c = parsecmd(v, n);
  1306. if(waserror()){
  1307. free(c);
  1308. nexterror();
  1309. }
  1310. t = lookupcmd(c, ctab, nelem(ctab));
  1311. switch(t->index){
  1312. case CMdebug:
  1313. debug = (strcmp(c->f[1], "on") == 0);
  1314. break;
  1315. case CMcoal:
  1316. i = atoi(c->f[1]);
  1317. if(i < 0 || i > 1000)
  1318. error(Ebadarg);
  1319. *((Ctlr*)e->ctlr)->coal = pbit32(i);
  1320. break;
  1321. case CMwakeup:
  1322. wakeup(&((Ctlr*)e->ctlr)->rxrendez); /* you're kidding, right? */
  1323. break;
  1324. case CMtxwakeup:
  1325. wakeup(&((Ctlr*)e->ctlr)->txrendez); /* you're kidding, right? */
  1326. break;
  1327. // case CMqsummary:
  1328. // summary(e);
  1329. // break;
  1330. case CMrxring:
  1331. rxring(e->ctlr);
  1332. break;
  1333. default:
  1334. error(Ebadarg);
  1335. }
  1336. free(c);
  1337. poperror();
  1338. return n;
  1339. }
  1340. static void
  1341. m10gshutdown(Ether *e)
  1342. {
  1343. dprint("m10gshutdown\n");
  1344. m10gdetach(e->ctlr);
  1345. }
  1346. static void
  1347. m10gpromiscuous(void *v, int on)
  1348. {
  1349. Ether *e;
  1350. int i;
  1351. dprint("m10gpromiscuous\n");
  1352. e = v;
  1353. if(on)
  1354. i = Cpromisc;
  1355. else
  1356. i = Cnopromisc;
  1357. cmd(e->ctlr, i, 0);
  1358. }
  1359. static int mcctab[] = { CSleavemc, CSjoinmc };
  1360. static char *mcntab[] = { "leave", "join" };
  1361. static void
  1362. m10gmulticast(void *v, uchar *ea, int on)
  1363. {
  1364. Ether *e;
  1365. int i;
  1366. dprint("m10gmulticast\n");
  1367. e = v;
  1368. if((i = maccmd(e->ctlr, mcctab[on], ea)) != 0)
  1369. print("m10g: can't %s %E: %d\n", mcntab[on], ea, i);
  1370. }
  1371. static void
  1372. m10gpci(void)
  1373. {
  1374. Pcidev *p;
  1375. Ctlr *t, *c;
  1376. t = 0;
  1377. for(p = 0; p = pcimatch(p, 0x14c1, 0x0008); ){
  1378. c = malloc(sizeof *c);
  1379. if(c == nil)
  1380. continue;
  1381. memset(c, 0, sizeof *c);
  1382. c->pcidev = p;
  1383. c->id = p->did<<16 | p->vid;
  1384. c->boot = pcicap(p, PciCapVND);
  1385. // kickthebaby(p, c);
  1386. pcisetbme(p);
  1387. if(setmem(p, c) == -1){
  1388. print("m10g failed\n");
  1389. free(c);
  1390. /* cleanup */
  1391. continue;
  1392. }
  1393. if(t)
  1394. t->next = c;
  1395. else
  1396. ctlrs = c;
  1397. t = c;
  1398. }
  1399. }
  1400. static int
  1401. m10gpnp(Ether *e)
  1402. {
  1403. Ctlr *c;
  1404. if(ctlrs == nil)
  1405. m10gpci();
  1406. for(c = ctlrs; c != nil; c = c->next)
  1407. if(c->active)
  1408. continue;
  1409. else if(e->port == 0 || e->port == c->port)
  1410. break;
  1411. if(c == nil)
  1412. return -1;
  1413. c->active = 1;
  1414. e->ctlr = c;
  1415. e->port = c->port;
  1416. e->irq = c->pcidev->intl;
  1417. e->tbdf = c->pcidev->tbdf;
  1418. e->mbps = 10000;
  1419. memmove(e->ea, c->ra, Eaddrlen);
  1420. e->attach = m10gattach;
  1421. e->detach = m10gshutdown;
  1422. e->transmit = m10gtransmit;
  1423. e->interrupt = m10ginterrupt;
  1424. e->ifstat = m10gifstat;
  1425. e->ctl = m10gctl;
  1426. // e->power = m10gpower;
  1427. e->shutdown = m10gshutdown;
  1428. e->arg = e;
  1429. e->promiscuous = m10gpromiscuous;
  1430. e->multicast = m10gmulticast;
  1431. return 0;
  1432. }
  1433. void
  1434. etherm10glink(void)
  1435. {
  1436. addethercard("m10g", m10gpnp);
  1437. }