etherm10g.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  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(...) do{if(debug)print(__VA_ARGS__);}while(0)
  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 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 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 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 = %u\n", off);
  272. if(off < 0x100 || off >= 4*KiB - 1)
  273. return 0;
  274. }
  275. print("pciecap found = %u\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("%u 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 = 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("[%x]", i);
  442. return i;
  443. }
  444. tsleep(&up->sleep, return0, 0, 1);
  445. }
  446. qunlock(&c->cmdl);
  447. iprint("m10g: cmd timeout [%x %x] 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 = 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("[%x]", i);
  479. return i;
  480. }
  481. tsleep(&up->sleep, return0, 0, 1);
  482. }
  483. qunlock(&c->cmdl);
  484. iprint("m10g: maccmd timeout [%x %x] 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 = 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 = 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("[%x %x]", 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 = %x\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 %llx\n", (uint64_t)off);
  661. if((off&3) || off + sizeof *h > c->ramsz){
  662. print("!m10g: bad firmware %llx\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 %x\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 = 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: %u MB/s\n", ((i>>16)*sz*2)/(i&0xffff));
  701. i = dmatestcmd(c, DMAwrite, c->done.busaddr, sz);
  702. print("\t" "write: %u MB/s\n", ((i>>16)*sz*2)/(i&0xffff));
  703. i = dmatestcmd(c, DMAwrite|DMAread, c->done.busaddr, sz);
  704. print("\t" "r/w: %u 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->Netif.maxmtu = Maxmtu;
  710. cmd(c, CSmtu, e->Netif.maxmtu);
  711. dprint("CSmtu %d...\n", e->Netif.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.8lx\n", p->mem[0].bar);
  742. return -1;
  743. }
  744. dprint("%llx <- vmap(mem[0].size = %x)\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->Lock);
  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->Lock);
  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->Lock);
  790. b->next = p->head;
  791. p->head = b;
  792. p->n++;
  793. p->cnt++;
  794. iunlock(&p->Lock);
  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->Lock);
  804. b->next = p->head;
  805. p->head = b;
  806. p->n++;
  807. p->cnt++;
  808. iunlock(&p->Lock);
  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->Netif.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)) != nil)
  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]) != nil){
  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: %u %lu\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->QLock);
  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->QLock);
  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->Netif.link = i;
  1131. if((c->linkstat = i) != 0)
  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 = externup();
  1177. Ctlr *c;
  1178. char name[12];
  1179. dprint("m10gattach\n");
  1180. /* the original code use the anon struct to lock; qlock came before
  1181. * the c = c->ctrl deref. I think what we're doing here is really equivalent,
  1182. * but I wanted you to know.
  1183. */
  1184. c = e->ctlr;
  1185. qlock(&c->QLock);
  1186. if(c->state != Detached){
  1187. qunlock(&c->QLock);
  1188. return;
  1189. }
  1190. if(waserror()){
  1191. c->state = Detached;
  1192. qunlock(&c->QLock);
  1193. nexterror();
  1194. }
  1195. reset(e, c);
  1196. c->state = Attached;
  1197. open0(e, c);
  1198. if(c->kprocs == 0){
  1199. c->kprocs++;
  1200. snprint(name, sizeof name, "#l%drxproc", e->ctlrno);
  1201. kproc(name, m10rx, e);
  1202. snprint(name, sizeof name, "#l%dtxproc", e->ctlrno);
  1203. kproc(name, txproc, e);
  1204. }
  1205. c->state = Runed;
  1206. qunlock(&c->QLock);
  1207. poperror();
  1208. }
  1209. static int
  1210. m10gdetach(Ctlr *c)
  1211. {
  1212. dprint("m10gdetach\n");
  1213. // reset(e->ctlr);
  1214. vunmap(c->ram, c->pcidev->mem[0].size);
  1215. ctlrfree(c);
  1216. return -1;
  1217. }
  1218. static int
  1219. lstcount(Block *b)
  1220. {
  1221. int i;
  1222. i = 0;
  1223. for(; b; b = b->next)
  1224. i++;
  1225. return i;
  1226. }
  1227. static int32_t
  1228. m10gifstat(Ether *e, void *v, int32_t n, uint32_t off)
  1229. {
  1230. int l, lim;
  1231. char *p;
  1232. Ctlr *c;
  1233. Stats s;
  1234. c = e->ctlr;
  1235. lim = 2*READSTR-1;
  1236. p = malloc(lim+1);
  1237. l = 0;
  1238. /* no point in locking this because this is done via dma. */
  1239. memmove(&s, c->stats, sizeof s);
  1240. // l +=
  1241. snprint(p+l, lim,
  1242. "txcnt = %u\n" "linkstat = %u\n" "dlink = %u\n"
  1243. "derror = %u\n" "drunt = %u\n" "doverrun = %u\n"
  1244. "dnosm = %u\n" "dnobg = %u\n" "nrdma = %u\n"
  1245. "txstopped = %u\n" "down = %u\n" "updated = %u\n"
  1246. "valid = %u\n\n"
  1247. "tx pkt = %lu\n" "tx bytes = %lld\n"
  1248. "tx cnt = %u\n" "tx n = %u\n" "tx i = %u\n"
  1249. "sm cnt = %u\n" "sm i = %u\n" "sm n = %u\n"
  1250. "sm lst = %u\n"
  1251. "bg cnt = %u\n" "bg i = %u\n" "bg n = %u\n"
  1252. "bg lst = %u\n"
  1253. "segsz = %u\n" "coal = %d\n",
  1254. gbit32(s.txcnt), gbit32(s.linkstat), gbit32(s.dlink),
  1255. gbit32(s.derror), gbit32(s.drunt), gbit32(s.doverrun),
  1256. gbit32(s.dnosm), gbit32(s.dnobg), gbit32(s.nrdma),
  1257. s.txstopped, s.down, s.updated, s.valid,
  1258. c->tx.npkt, c->tx.nbytes,
  1259. c->tx.cnt, c->tx.n, c->tx.i,
  1260. c->sm.cnt, c->sm.i, c->sm.pool->n, lstcount(c->sm.pool->head),
  1261. c->bg.cnt, c->bg.i, c->bg.pool->n, lstcount(c->bg.pool->head),
  1262. c->tx.segsz, gbit32((uint8_t*)c->coal));
  1263. n = readstr(off, v, n, p);
  1264. free(p);
  1265. return n;
  1266. }
  1267. //static void
  1268. //summary(Ether *e)
  1269. //{
  1270. // char *buf;
  1271. // int n, i, j;
  1272. //
  1273. // if(e == 0)
  1274. // return;
  1275. // buf = malloc(n=250);
  1276. // if(buf == 0)
  1277. // return;
  1278. //
  1279. // snprint(buf, n, "oq\n");
  1280. // qsummary(e->oq, buf+3, n-3-1);
  1281. // iprint("%s", buf);
  1282. //
  1283. // if(e->f) for(i = 0; e->f[i]; i++){
  1284. // j = snprint(buf, n, "f%d %d\n", i, e->f[i]->type);
  1285. // qsummary(e->f[i]->in, buf+j, n-j-1);
  1286. // print("%s", buf);
  1287. // }
  1288. //
  1289. // free(buf);
  1290. //}
  1291. static void
  1292. rxring(Ctlr *c)
  1293. {
  1294. Done *d;
  1295. Slot *s;
  1296. Slotparts *sp;
  1297. int i;
  1298. d = &c->done;
  1299. s = d->entry;
  1300. for(i = 0; i < d->n; i++) {
  1301. sp = (Slotparts *)(s + i);
  1302. if(sp->len)
  1303. iprint("s[%d] = %d\n", i, sp->len);
  1304. }
  1305. }
  1306. enum {
  1307. CMdebug,
  1308. CMcoal,
  1309. CMwakeup,
  1310. CMtxwakeup,
  1311. CMqsummary,
  1312. CMrxring,
  1313. };
  1314. static Cmdtab ctab[] = {
  1315. {CMdebug, "debug", 2},
  1316. {CMcoal, "coal", 2},
  1317. {CMwakeup, "wakeup", 1},
  1318. {CMtxwakeup, "txwakeup", 1},
  1319. // {CMqsummary, "q", 1},
  1320. {CMrxring, "rxring", 1},
  1321. };
  1322. static int32_t
  1323. m10gctl(Ether *e, void *v, int32_t n)
  1324. {
  1325. Proc *up = externup();
  1326. int i;
  1327. Cmdbuf *c;
  1328. Cmdtab *t;
  1329. dprint("m10gctl\n");
  1330. if(e->ctlr == nil)
  1331. error(Enonexist);
  1332. c = parsecmd(v, n);
  1333. if(waserror()){
  1334. free(c);
  1335. nexterror();
  1336. }
  1337. t = lookupcmd(c, ctab, nelem(ctab));
  1338. switch(t->index){
  1339. case CMdebug:
  1340. debug = (strcmp(c->f[1], "on") == 0);
  1341. break;
  1342. case CMcoal:
  1343. i = atoi(c->f[1]);
  1344. if(i < 0 || i > 1000)
  1345. error(Ebadarg);
  1346. *((Ctlr*)e->ctlr)->coal = pbit32(i);
  1347. break;
  1348. case CMwakeup:
  1349. wakeup(&((Ctlr*)e->ctlr)->rxrendez); /* you're kidding, right? */
  1350. break;
  1351. case CMtxwakeup:
  1352. wakeup(&((Ctlr*)e->ctlr)->txrendez); /* you're kidding, right? */
  1353. break;
  1354. // case CMqsummary:
  1355. // summary(e);
  1356. // break;
  1357. case CMrxring:
  1358. rxring(e->ctlr);
  1359. break;
  1360. default:
  1361. error(Ebadarg);
  1362. }
  1363. free(c);
  1364. poperror();
  1365. return n;
  1366. }
  1367. static void
  1368. m10gshutdown(Ether *e)
  1369. {
  1370. dprint("m10gshutdown\n");
  1371. m10gdetach(e->ctlr);
  1372. }
  1373. static void
  1374. m10gpromiscuous(void *v, int on)
  1375. {
  1376. Ether *e;
  1377. int i;
  1378. dprint("m10gpromiscuous\n");
  1379. e = v;
  1380. if(on)
  1381. i = Cpromisc;
  1382. else
  1383. i = Cnopromisc;
  1384. cmd(e->ctlr, i, 0);
  1385. }
  1386. static int mcctab[] = { CSleavemc, CSjoinmc };
  1387. static char *mcntab[] = { "leave", "join" };
  1388. static void
  1389. m10gmulticast(void *v, uint8_t *ea, int on)
  1390. {
  1391. Ether *e;
  1392. int i;
  1393. dprint("m10gmulticast\n");
  1394. e = v;
  1395. if((i = maccmd(e->ctlr, mcctab[on], ea)) != 0)
  1396. print("m10g: can't %s %E: %d\n", mcntab[on], ea, i);
  1397. }
  1398. static void
  1399. m10gpci(void)
  1400. {
  1401. Pcidev *p;
  1402. Ctlr *t, *c;
  1403. t = nil;
  1404. for(p = nil; (p = pcimatch(p, 0x14c1, 0x0008)) != nil;){
  1405. c = malloc(sizeof *c);
  1406. if(c == nil)
  1407. continue;
  1408. memset(c, 0, sizeof *c);
  1409. c->pcidev = p;
  1410. c->id = p->did<<16 | p->vid;
  1411. c->boot = pcicap(p, PciCapVND);
  1412. // kickthebaby(p, c);
  1413. pcisetbme(p);
  1414. if(setmem(p, c) == -1){
  1415. print("m10g failed\n");
  1416. free(c);
  1417. /* cleanup */
  1418. continue;
  1419. }
  1420. if(t)
  1421. t->next = c;
  1422. else
  1423. ctlrs = c;
  1424. t = c;
  1425. }
  1426. }
  1427. static int
  1428. m10gpnp(Ether *e)
  1429. {
  1430. Ctlr *c;
  1431. if(ctlrs == nil)
  1432. m10gpci();
  1433. for(c = ctlrs; c != nil; c = c->next)
  1434. if(c->active)
  1435. continue;
  1436. else if(e->ISAConf.port == 0 || e->ISAConf.port == c->port)
  1437. break;
  1438. if(c == nil)
  1439. return -1;
  1440. c->active = 1;
  1441. e->ctlr = c;
  1442. e->ISAConf.port = c->port;
  1443. e->ISAConf.irq = c->pcidev->intl;
  1444. e->tbdf = c->pcidev->tbdf;
  1445. e->Netif.mbps = 10000;
  1446. memmove(e->ea, c->ra, Eaddrlen);
  1447. e->attach = m10gattach;
  1448. e->detach = m10gshutdown;
  1449. e->transmit = m10gtransmit;
  1450. e->interrupt = m10ginterrupt;
  1451. e->ifstat = m10gifstat;
  1452. e->ctl = m10gctl;
  1453. // e->power = m10gpower;
  1454. e->shutdown = m10gshutdown;
  1455. e->Netif.arg = e;
  1456. e->Netif.promiscuous = m10gpromiscuous;
  1457. e->Netif.multicast = m10gmulticast;
  1458. return 0;
  1459. }
  1460. void
  1461. etherm10glink(void)
  1462. {
  1463. addethercard("m10g", m10gpnp);
  1464. }