1052 lines · 17.3 KB · C
RawDownload
/* *	myricom 10 gbit ethernet *	© 2010 erik quanstrom, coraid, inc. */ #include "u.h"#include "lib.h"#include "mem.h"#include "dat.h"#include "fns.h"#include "io.h" #include "etherif.h" #define		qlock(i)		#define		qunlock(i)	#define		wakeup(i)	while(0) #define		K	* 1024#define		MB	* 1024 K #define	dprint(...)	if(0) print(__VA_ARGS__); else {}#define malign(n)	xspanalloc(n, 4 K, 0)#define if64(...)		(sizeof(uintptr) == 8? (__VA_ARGS__): 0)#define pbit32h(x)	if64(pbit32((uvlong)x >> 32)) enum {	Epromsz	= 256,	Maxslots	= 128,			/* 1024? */	Rbalign	= BY2PG,	Noconf	= 0xffffffff,	Fwoffset	= 1 MB,	Hdroff	= 0x00003c,	Cmdoff	= 0xf80000,		/* offset of command port */	Fwsubmt	= 0xfc0000,		/* offset of firmware submission command port */	Rdmaoff	= 0xfc01c0,		/* offset of rdma command port */	Cmderr	= 0x01000001,}; enum {	CZero,	Creset,	Cversion, 	CSintrqdma,		/* issue these before Cetherup */	CSbigsz,			/* in bytes bigsize = 2^n */	CSsmallsz, 	CGsendoff,	CGsmallrxoff,	CGbigrxoff,	CGirqackoff,	CGirqdeassoff,	CGsendrgsz,	CGrxrgsz, 	CSintrqsz,		/* 2^n */	Cetherup,		/* above paramters + mtu/mac addr must be set first */	Cetherdn, 	CSmtu,			/* below may be issued live */	CGcoaloff,		/* in µs */	CSstatsrate,		/* in µs */	CSstatsdma, 	Cpromisc,	Cnopromisc,	CSmac, 	Cenablefc,	Cdisablefc, 	Cdmatest, 	Cenableallmc,	Cdisableallmc, 	CSjoinmc,	CSleavemc,	Cleaveallmc, 	CSstatsdma2,	Cdmatestu,	Custatus,		/* unaligned status */}; typedef union {	uint	i[2];	uchar	c[8];} Cmd; typedef struct {	ushort	cksum;	ushort	len;} Slot; enum {	SFsmall	= 1,	SFfirst	= 2,	SFalign	= 4,	SFnotso	= 16,}; typedef struct {	u32int	high;	u32int	low;	u16int	hdroff;	u16int	len;	union{		struct {			uchar	pad;			uchar	nrdma;			uchar	chkoff;			uchar	flags;		};		u32int	fword;	/* ha! */	};} Send; typedef struct {	Send	*lanai;		/* tx ring (cksum + len in lanai memory) */	Send	*host;		/* tx ring (data in our memory). */	Block	**bring;	int	size;		/* how big are the buffers in the z8's memory */	uint	segsz;	uint	n;		/* txslots */	uint	m;		/* mask */	uint	i;		/* number of segments (not frames) queued */	uint	cnt;		/* number of segments sent by the card */	uint	starve;	uint	starvei;		/* starve pt */	uint	submit; 	uint	npkt;	vlong	nbytes;} Tx; enum {	Pstarve	= 1<<0,}; typedef struct {	Lock;	uint	size;		/* buffer size of each block */	uint	n;		/* n free buffers. */	uint	cnt;//	uint	flags;} Bpool; typedef struct {	Bpool	*pool;		/* free buffers */	uint	*lanai;		/* rx ring; we have no perminant host shadow. */	Block	**host;		/* called "info" in myricom driver */	uint	m;	uint	n;		/* rxslots */	uint	i;	uint	cnt;		/* number of buffers allocated (lifetime). */} Rx; /* dma mapped.  unix network byte order. */typedef struct {	uchar	unused[4];	uchar	dpause[4];	uchar	dufilt[4];	uchar	dcrc32[4];	uchar	dphy[4];	uchar	dmcast[4];	uchar	txcnt[4];	uchar	linkstat[4];	uchar	dlinkef[4];	uchar	derror[4];	uchar	drunt[4];	uchar	doverrun[4];	uchar	dnosm[4];	uchar	dnobg[4];	uchar	nrdma[4];	uchar	txstopped;	uchar	down;	uchar	updated;	uchar	valid;} Stats; enum {	Detached,	Attached,	Runed,}; typedef struct {	uint	*entry;	uintptr	busaddr;	uint	m;	uint	n;	uint	i;} Done; typedef struct Ctlr Ctlr;typedef struct Ctlr {	int	state;	uintptr	port;	Pcidev*	pcidev;	Ctlr*	next;	int	active; 	uchar	ra[Eaddrlen]; 	int	ramsz;	uchar	*ram; 	u32int	*irqack;	u32int	*irqdeass;	u32int	*coal; 	char	eprom[Epromsz];	uint	serial;			/* unit serial number */ 	void	*cmdl;	Cmd	*cmd;			/* address of command return */	uintptr	cprt;			/* bus address of command */ 	Done	done;	Tx	tx;	Rx	sm;	Rx	bg;	Stats	*stats;	uintptr	statsprt;	uint	speed[2]; //	Rendez	txrendez;	int	txrendez; 	int	msi;	uint	linkstat;	uint	nrdma;} Ctlr; static	Bpool	smpool 	= {.size	= 2048, };static	Bpool	bgpool	= {.size = 2048,};static	Ctlr 	*ctlrs; static intparseeprom(Ctlr *c){	int i, j, k, l, bits;	char *s; 	dprint("m10g eprom:\n");	s = c->eprom;	bits = 3;	for(i = 0; s[i] && i < Epromsz; i++){		l = strlen(s + i);		dprint("\t%s\n", s + i);		if(strncmp(s + i, "MAC=", 4) == 0 && l == 21){			bits ^= 1;			j = i + 4;			for(k = 0; k < 6; k++)				c->ra[k] = strtoul(s + j + 3*k, 0, 16);		}else if(strncmp(s + i, "SN=", 3) == 0){			bits ^= 2;			c->serial = atoi(s + i + 3);		}		i += l;	}	if(bits)		return -1;	return 0;} static ushortpbit16(u16int i){	u16int j;	uchar *p; 	p = (uchar*)&j;	p[1] = i;	p[0] = i>>8;	return j;} static u16intgbit16(uchar i[2]){	u16int j; 	j = i[1];	j |= i[0]<<8;	return j;} static u32intpbit32(u32int i){	uint j;	uchar *p; 	p = (uchar*)&j;	p[3] = i;	p[2] = i>>8;	p[1] = i>>16;	p[0] = i>>24;	return j;} static u32intgbit32(uchar i[4]){	u32int j; 	j = i[3];	j |= i[2]<<8;	j |= i[1]<<16;	j |= i[0]<<24;	return j;} static voidprepcmd(u32int *cmd, int i){	while(i-- > 0)		cmd[i] = pbit32(cmd[i]);} /* * the command looks like this (int 32bit integers) * cmd type * data0 (or, addr low; endian backwards) * data1 (addr high) * data2 * response (high) * response (low) * 40 byte = 5 int pad. */ static uintcmd(Ctlr *c, int type, int sz, uvlong data){	u32int buf[16], i;	Cmd *cmd; 	qlock(&c->cmdl);	cmd = c->cmd;	cmd->i[1] = Noconf;	memset(buf, 0, sizeof buf);	buf[0] = type;	buf[1] = data;	buf[2] = data>>32;	buf[3] = sz;	buf[4] = (uvlong)c->cprt>>32;	buf[5] = c->cprt;	prepcmd(buf, 6);	coherence();	memmove(c->ram + Cmdoff, buf, sizeof buf); 	for(i = 0; i < 15; i++){		if(cmd->i[1] != Noconf){			i = gbit32(cmd->c);			qunlock(&c->cmdl);			if(cmd->i[1] != 0)				dprint("[%ux]", i);			return i;		}		delay(1);	}	qunlock(&c->cmdl);	print("m10g: cmd timeout [%ux %ux] cmd=%d\n", cmd->i[0], cmd->i[1], type);	return Cmderr;	/* hopefully a bogus value! */} static uintmaccmd(Ctlr *c, int type, uchar *m){	u32int buf[16], i;	Cmd * cmd; 	qlock(&c->cmdl);	cmd = c->cmd;	cmd->i[1] = Noconf;	memset(buf, 0, sizeof buf);	buf[0] = type;	buf[1] = m[0]<<24 | m[1]<<16 | m[2]<<8 | m[3];	buf[2] = m[4]<<8 | m[5];	buf[4] = (uvlong)c->cprt>>32;	buf[5] = c->cprt;	prepcmd(buf, 6);	coherence();	memmove(c->ram + Cmdoff, buf, sizeof buf); 	for(i = 0; i < 15; i++){		if(cmd->i[1] != Noconf){			i = gbit32(cmd->c);			qunlock(&c->cmdl);			if(cmd->i[1] != 0)				dprint("[%ux]", i);			return i;		}		delay(1);	}	qunlock(&c->cmdl);	print("m10g: maccmd timeout\n");	return ~0;} static uintrdmacmd(Ctlr *c, int on){	u32int buf[16], i; 	memset(buf, 0, sizeof buf);	c->cmd->i[0] = 0;	coherence();	buf[0] = (uvlong)c->cprt>>32;	buf[1] = c->cprt;	buf[2] = Noconf;	buf[3] = (uvlong)c->cprt>>32;	buf[4] = c->cprt;	buf[5] = on;	prepcmd(buf, 6);	memmove(c->ram + Rdmaoff, buf, sizeof buf); 	for(i = 0; i < 20; i++){		if(c->cmd->i[0] == Noconf)			return 0;		delay(1);	}	print("m10g: rdmacmd timeout\n");	return Cmderr;} typedef struct{	uchar	len[4];	uchar	type[4];	char	version[128];	uchar	globals[4];	uchar	ramsz[4];	uchar	specs[4];	uchar	specssz[4];	uchar	idx;	uchar	norabbit;	uchar	unaligntlp;	uchar	pcilinkalg;	uchar	cntaddr[4];	uchar	cbinfo[4];	uchar	handoid[2];	uchar	handocap[2];	uchar	msixtab[4];	uchar	bss[4];	uchar	features[4];	uchar	eehdr[4];} Fwhdr; enum{	Tmx	= 0x4d582020,	Tpcie	= 0x70636965,	Teth	= 0x45544820,	Tmcp0	= 0x4d435030,}; static intchkfw(Ctlr *c){	uint off, type;	Fwhdr *h; 	off = gbit32(c->ram + Hdroff);	dprint("m10g: firmware %ux\n", off);	if(off == 0 || off&3 || off + sizeof *h >= c->ramsz){		print("m10g: bad firmware %#ux\n", off);		return -1;	}	h = (Fwhdr*)(c->ram + off);	type = gbit32(h->type);	dprint("\t" "type	%.8ux\n", type);	dprint("\t" "vers	%s\n", h->version);	dprint("\t" "ramsz	%ux\n", gbit32(h->ramsz));	if(type != Teth){		print("m10g: bad card type %.8ux\n", type);		return -1;	}	return rdmacmd(c, 0);} static intreset(Ether*, Ctlr *c){	if(chkfw(c) == -1){err:		print("m10g: reset error\n");		return -1;	}	if(cmd(c, Creset, 0, 0) == Cmderr){		print("reset fails\n");		goto err;	}	if(cmd(c, CSintrqsz, 0, c->done.n*sizeof *c->done.entry) == Cmderr)		goto err;	if(cmd(c, CSintrqdma, 0, c->done.busaddr) == Cmderr)		goto err;	c->irqack = (u32int*)(c->ram + cmd(c, CGirqackoff, 0, 0));	c->irqdeass = (u32int*)(c->ram + cmd(c, CGirqdeassoff, 0, 0));

Lines 1–500 of 1052.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t