etherm10g.c 29 KB

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