xref: /openbmc/linux/drivers/block/aoe/aoecmd.c (revision 3d5b06051cd5fa82c9a4285f7ce8650a0f0845ff)
152e112b3SEd L. Cashin /* Copyright (c) 2007 Coraid, Inc.  See COPYING for GPL terms. */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * aoecmd.c
41da177e4SLinus Torvalds  * Filesystem request handling methods
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
704b3ab52SBartlomiej Zolnierkiewicz #include <linux/ata.h>
85a0e3ad6STejun Heo #include <linux/slab.h>
91da177e4SLinus Torvalds #include <linux/hdreg.h>
101da177e4SLinus Torvalds #include <linux/blkdev.h>
111da177e4SLinus Torvalds #include <linux/skbuff.h>
121da177e4SLinus Torvalds #include <linux/netdevice.h>
133ae1c24eSEd L. Cashin #include <linux/genhd.h>
1468e0d42fSEd L. Cashin #include <linux/moduleparam.h>
15881d966bSEric W. Biederman #include <net/net_namespace.h>
16475172fbSEd L. Cashin #include <asm/unaligned.h>
171da177e4SLinus Torvalds #include "aoe.h"
181da177e4SLinus Torvalds 
19b751e8b6SEd L. Cashin static int aoe_deadsecs = 60 * 3;
20b751e8b6SEd L. Cashin module_param(aoe_deadsecs, int, 0644);
21b751e8b6SEd L. Cashin MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
221da177e4SLinus Torvalds 
237df620d8SEd L. Cashin static int aoe_maxout = 16;
247df620d8SEd L. Cashin module_param(aoe_maxout, int, 0644);
257df620d8SEd L. Cashin MODULE_PARM_DESC(aoe_maxout,
267df620d8SEd L. Cashin 	"Only aoe_maxout outstanding packets for every MAC on eX.Y.");
277df620d8SEd L. Cashin 
2868e0d42fSEd L. Cashin static struct sk_buff *
29e407a7f6SEd L. Cashin new_skb(ulong len)
301da177e4SLinus Torvalds {
311da177e4SLinus Torvalds 	struct sk_buff *skb;
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds 	skb = alloc_skb(len, GFP_ATOMIC);
341da177e4SLinus Torvalds 	if (skb) {
35459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
36c1d2bbe1SArnaldo Carvalho de Melo 		skb_reset_network_header(skb);
371da177e4SLinus Torvalds 		skb->protocol = __constant_htons(ETH_P_AOE);
388babe8ccSEd Cashin 		skb_checksum_none_assert(skb);
391da177e4SLinus Torvalds 	}
401da177e4SLinus Torvalds 	return skb;
411da177e4SLinus Torvalds }
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds static struct frame *
4468e0d42fSEd L. Cashin getframe(struct aoetgt *t, int tag)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	struct frame *f, *e;
471da177e4SLinus Torvalds 
4868e0d42fSEd L. Cashin 	f = t->frames;
4968e0d42fSEd L. Cashin 	e = f + t->nframes;
501da177e4SLinus Torvalds 	for (; f<e; f++)
511da177e4SLinus Torvalds 		if (f->tag == tag)
521da177e4SLinus Torvalds 			return f;
531da177e4SLinus Torvalds 	return NULL;
541da177e4SLinus Torvalds }
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /*
571da177e4SLinus Torvalds  * Leave the top bit clear so we have tagspace for userland.
581da177e4SLinus Torvalds  * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
591da177e4SLinus Torvalds  * This driver reserves tag -1 to mean "unused frame."
601da177e4SLinus Torvalds  */
611da177e4SLinus Torvalds static int
6268e0d42fSEd L. Cashin newtag(struct aoetgt *t)
631da177e4SLinus Torvalds {
641da177e4SLinus Torvalds 	register ulong n;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	n = jiffies & 0xffff;
6768e0d42fSEd L. Cashin 	return n |= (++t->lasttag & 0x7fff) << 16;
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds static int
7168e0d42fSEd L. Cashin aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
721da177e4SLinus Torvalds {
7368e0d42fSEd L. Cashin 	u32 host_tag = newtag(t);
741da177e4SLinus Torvalds 
7568e0d42fSEd L. Cashin 	memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
7668e0d42fSEd L. Cashin 	memcpy(h->dst, t->addr, sizeof h->dst);
7763e9cc5dSecashin@coraid.com 	h->type = __constant_cpu_to_be16(ETH_P_AOE);
781da177e4SLinus Torvalds 	h->verfl = AOE_HVER;
7963e9cc5dSecashin@coraid.com 	h->major = cpu_to_be16(d->aoemajor);
801da177e4SLinus Torvalds 	h->minor = d->aoeminor;
811da177e4SLinus Torvalds 	h->cmd = AOECMD_ATA;
8263e9cc5dSecashin@coraid.com 	h->tag = cpu_to_be32(host_tag);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	return host_tag;
851da177e4SLinus Torvalds }
861da177e4SLinus Torvalds 
8719bf2635SEd L. Cashin static inline void
8819bf2635SEd L. Cashin put_lba(struct aoe_atahdr *ah, sector_t lba)
8919bf2635SEd L. Cashin {
9019bf2635SEd L. Cashin 	ah->lba0 = lba;
9119bf2635SEd L. Cashin 	ah->lba1 = lba >>= 8;
9219bf2635SEd L. Cashin 	ah->lba2 = lba >>= 8;
9319bf2635SEd L. Cashin 	ah->lba3 = lba >>= 8;
9419bf2635SEd L. Cashin 	ah->lba4 = lba >>= 8;
9519bf2635SEd L. Cashin 	ah->lba5 = lba >>= 8;
9619bf2635SEd L. Cashin }
9719bf2635SEd L. Cashin 
981da177e4SLinus Torvalds static void
9968e0d42fSEd L. Cashin ifrotate(struct aoetgt *t)
1001da177e4SLinus Torvalds {
10168e0d42fSEd L. Cashin 	t->ifp++;
10268e0d42fSEd L. Cashin 	if (t->ifp >= &t->ifs[NAOEIFS] || t->ifp->nd == NULL)
10368e0d42fSEd L. Cashin 		t->ifp = t->ifs;
10468e0d42fSEd L. Cashin 	if (t->ifp->nd == NULL) {
10568e0d42fSEd L. Cashin 		printk(KERN_INFO "aoe: no interface to rotate to\n");
10668e0d42fSEd L. Cashin 		BUG();
10768e0d42fSEd L. Cashin 	}
10868e0d42fSEd L. Cashin }
10968e0d42fSEd L. Cashin 
1109bb237b6SEd L. Cashin static void
1119bb237b6SEd L. Cashin skb_pool_put(struct aoedev *d, struct sk_buff *skb)
1129bb237b6SEd L. Cashin {
113e9bb8fb0SDavid S. Miller 	__skb_queue_tail(&d->skbpool, skb);
1149bb237b6SEd L. Cashin }
1159bb237b6SEd L. Cashin 
1169bb237b6SEd L. Cashin static struct sk_buff *
1179bb237b6SEd L. Cashin skb_pool_get(struct aoedev *d)
1189bb237b6SEd L. Cashin {
119e9bb8fb0SDavid S. Miller 	struct sk_buff *skb = skb_peek(&d->skbpool);
1209bb237b6SEd L. Cashin 
1219bb237b6SEd L. Cashin 	if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
122e9bb8fb0SDavid S. Miller 		__skb_unlink(skb, &d->skbpool);
1239bb237b6SEd L. Cashin 		return skb;
1249bb237b6SEd L. Cashin 	}
125e9bb8fb0SDavid S. Miller 	if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
126e9bb8fb0SDavid S. Miller 	    (skb = new_skb(ETH_ZLEN)))
1279bb237b6SEd L. Cashin 		return skb;
128e9bb8fb0SDavid S. Miller 
1299bb237b6SEd L. Cashin 	return NULL;
1309bb237b6SEd L. Cashin }
1319bb237b6SEd L. Cashin 
1329bb237b6SEd L. Cashin /* freeframe is where we do our load balancing so it's a little hairy. */
13368e0d42fSEd L. Cashin static struct frame *
13468e0d42fSEd L. Cashin freeframe(struct aoedev *d)
13568e0d42fSEd L. Cashin {
1369bb237b6SEd L. Cashin 	struct frame *f, *e, *rf;
13768e0d42fSEd L. Cashin 	struct aoetgt **t;
1389bb237b6SEd L. Cashin 	struct sk_buff *skb;
13968e0d42fSEd L. Cashin 
14068e0d42fSEd L. Cashin 	if (d->targets[0] == NULL) {	/* shouldn't happen, but I'm paranoid */
14168e0d42fSEd L. Cashin 		printk(KERN_ERR "aoe: NULL TARGETS!\n");
14268e0d42fSEd L. Cashin 		return NULL;
14368e0d42fSEd L. Cashin 	}
1449bb237b6SEd L. Cashin 	t = d->tgt;
1459bb237b6SEd L. Cashin 	t++;
1469bb237b6SEd L. Cashin 	if (t >= &d->targets[NTARGETS] || !*t)
14768e0d42fSEd L. Cashin 		t = d->targets;
1489bb237b6SEd L. Cashin 	for (;;) {
1499bb237b6SEd L. Cashin 		if ((*t)->nout < (*t)->maxout
1509bb237b6SEd L. Cashin 		&& t != d->htgt
1519bb237b6SEd L. Cashin 		&& (*t)->ifp->nd) {
1529bb237b6SEd L. Cashin 			rf = NULL;
15368e0d42fSEd L. Cashin 			f = (*t)->frames;
1549bb237b6SEd L. Cashin 			e = f + (*t)->nframes;
15568e0d42fSEd L. Cashin 			for (; f < e; f++) {
15668e0d42fSEd L. Cashin 				if (f->tag != FREETAG)
15768e0d42fSEd L. Cashin 					continue;
1589bb237b6SEd L. Cashin 				skb = f->skb;
1599bb237b6SEd L. Cashin 				if (!skb
1609bb237b6SEd L. Cashin 				&& !(f->skb = skb = new_skb(ETH_ZLEN)))
1619bb237b6SEd L. Cashin 					continue;
1629bb237b6SEd L. Cashin 				if (atomic_read(&skb_shinfo(skb)->dataref)
16368e0d42fSEd L. Cashin 					!= 1) {
1649bb237b6SEd L. Cashin 					if (!rf)
1659bb237b6SEd L. Cashin 						rf = f;
16668e0d42fSEd L. Cashin 					continue;
16768e0d42fSEd L. Cashin 				}
168*3d5b0605SEd Cashin gotone:				skb->truesize -= skb->data_len;
169*3d5b0605SEd Cashin 				skb_shinfo(skb)->nr_frags = skb->data_len = 0;
1709bb237b6SEd L. Cashin 				skb_trim(skb, 0);
17168e0d42fSEd L. Cashin 				d->tgt = t;
17268e0d42fSEd L. Cashin 				ifrotate(*t);
17368e0d42fSEd L. Cashin 				return f;
17468e0d42fSEd L. Cashin 			}
1759bb237b6SEd L. Cashin 			/* Work can be done, but the network layer is
1769bb237b6SEd L. Cashin 			   holding our precious packets.  Try to grab
1779bb237b6SEd L. Cashin 			   one from the pool. */
1789bb237b6SEd L. Cashin 			f = rf;
1799bb237b6SEd L. Cashin 			if (f == NULL) {	/* more paranoia */
1809bb237b6SEd L. Cashin 				printk(KERN_ERR
1819bb237b6SEd L. Cashin 					"aoe: freeframe: %s.\n",
1829bb237b6SEd L. Cashin 					"unexpected null rf");
1839bb237b6SEd L. Cashin 				d->flags |= DEVFL_KICKME;
1849bb237b6SEd L. Cashin 				return NULL;
1859bb237b6SEd L. Cashin 			}
1869bb237b6SEd L. Cashin 			skb = skb_pool_get(d);
1879bb237b6SEd L. Cashin 			if (skb) {
1889bb237b6SEd L. Cashin 				skb_pool_put(d, f->skb);
1899bb237b6SEd L. Cashin 				f->skb = skb;
1909bb237b6SEd L. Cashin 				goto gotone;
1919bb237b6SEd L. Cashin 			}
1929bb237b6SEd L. Cashin 			(*t)->dataref++;
1939bb237b6SEd L. Cashin 			if ((*t)->nout == 0)
19468e0d42fSEd L. Cashin 				d->flags |= DEVFL_KICKME;
19568e0d42fSEd L. Cashin 		}
1969bb237b6SEd L. Cashin 		if (t == d->tgt)	/* we've looped and found nada */
1979bb237b6SEd L. Cashin 			break;
19868e0d42fSEd L. Cashin 		t++;
1999bb237b6SEd L. Cashin 		if (t >= &d->targets[NTARGETS] || !*t)
2009bb237b6SEd L. Cashin 			t = d->targets;
2019bb237b6SEd L. Cashin 	}
20268e0d42fSEd L. Cashin 	return NULL;
20368e0d42fSEd L. Cashin }
20468e0d42fSEd L. Cashin 
205*3d5b0605SEd Cashin static void
206*3d5b0605SEd Cashin skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt)
207*3d5b0605SEd Cashin {
208*3d5b0605SEd Cashin 	int frag = 0;
209*3d5b0605SEd Cashin 	ulong fcnt;
210*3d5b0605SEd Cashin loop:
211*3d5b0605SEd Cashin 	fcnt = bv->bv_len - (off - bv->bv_offset);
212*3d5b0605SEd Cashin 	if (fcnt > cnt)
213*3d5b0605SEd Cashin 		fcnt = cnt;
214*3d5b0605SEd Cashin 	skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt);
215*3d5b0605SEd Cashin 	cnt -= fcnt;
216*3d5b0605SEd Cashin 	if (cnt <= 0)
217*3d5b0605SEd Cashin 		return;
218*3d5b0605SEd Cashin 	bv++;
219*3d5b0605SEd Cashin 	off = bv->bv_offset;
220*3d5b0605SEd Cashin 	goto loop;
221*3d5b0605SEd Cashin }
222*3d5b0605SEd Cashin 
22368e0d42fSEd L. Cashin static int
22468e0d42fSEd L. Cashin aoecmd_ata_rw(struct aoedev *d)
22568e0d42fSEd L. Cashin {
22668e0d42fSEd L. Cashin 	struct frame *f;
2271da177e4SLinus Torvalds 	struct aoe_hdr *h;
2281da177e4SLinus Torvalds 	struct aoe_atahdr *ah;
2291da177e4SLinus Torvalds 	struct buf *buf;
23068e0d42fSEd L. Cashin 	struct bio_vec *bv;
23168e0d42fSEd L. Cashin 	struct aoetgt *t;
2321da177e4SLinus Torvalds 	struct sk_buff *skb;
233*3d5b0605SEd Cashin 	ulong bcnt, fbcnt;
2341da177e4SLinus Torvalds 	char writebit, extbit;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	writebit = 0x10;
2371da177e4SLinus Torvalds 	extbit = 0x4;
2381da177e4SLinus Torvalds 
23968e0d42fSEd L. Cashin 	f = freeframe(d);
24068e0d42fSEd L. Cashin 	if (f == NULL)
24168e0d42fSEd L. Cashin 		return 0;
24268e0d42fSEd L. Cashin 	t = *d->tgt;
2431da177e4SLinus Torvalds 	buf = d->inprocess;
24468e0d42fSEd L. Cashin 	bv = buf->bv;
24568e0d42fSEd L. Cashin 	bcnt = t->ifp->maxbcnt;
24668e0d42fSEd L. Cashin 	if (bcnt == 0)
24768e0d42fSEd L. Cashin 		bcnt = DEFAULTBCNT;
248*3d5b0605SEd Cashin 	if (bcnt > buf->resid)
249*3d5b0605SEd Cashin 		bcnt = buf->resid;
250*3d5b0605SEd Cashin 	fbcnt = bcnt;
251*3d5b0605SEd Cashin 	f->bv = buf->bv;
252*3d5b0605SEd Cashin 	f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
253*3d5b0605SEd Cashin 	do {
254*3d5b0605SEd Cashin 		if (fbcnt < buf->bv_resid) {
255*3d5b0605SEd Cashin 			buf->bv_resid -= fbcnt;
256*3d5b0605SEd Cashin 			buf->resid -= fbcnt;
257*3d5b0605SEd Cashin 			break;
258*3d5b0605SEd Cashin 		}
259*3d5b0605SEd Cashin 		fbcnt -= buf->bv_resid;
260*3d5b0605SEd Cashin 		buf->resid -= buf->bv_resid;
261*3d5b0605SEd Cashin 		if (buf->resid == 0) {
262*3d5b0605SEd Cashin 			d->inprocess = NULL;
263*3d5b0605SEd Cashin 			break;
264*3d5b0605SEd Cashin 		}
265*3d5b0605SEd Cashin 		buf->bv++;
266*3d5b0605SEd Cashin 		buf->bv_resid = buf->bv->bv_len;
267*3d5b0605SEd Cashin 		WARN_ON(buf->bv_resid == 0);
268*3d5b0605SEd Cashin 	} while (fbcnt);
269*3d5b0605SEd Cashin 
2701da177e4SLinus Torvalds 	/* initialize the headers & frame */
271e407a7f6SEd L. Cashin 	skb = f->skb;
272abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
2731da177e4SLinus Torvalds 	ah = (struct aoe_atahdr *) (h+1);
27419900cdeSEd L. Cashin 	skb_put(skb, sizeof *h + sizeof *ah);
27519900cdeSEd L. Cashin 	memset(h, 0, skb->len);
27668e0d42fSEd L. Cashin 	f->tag = aoehdr_atainit(d, t, h);
27768e0d42fSEd L. Cashin 	t->nout++;
2781da177e4SLinus Torvalds 	f->waited = 0;
2791da177e4SLinus Torvalds 	f->buf = buf;
28019bf2635SEd L. Cashin 	f->bcnt = bcnt;
28168e0d42fSEd L. Cashin 	f->lba = buf->sector;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	/* set up ata header */
2841da177e4SLinus Torvalds 	ah->scnt = bcnt >> 9;
28568e0d42fSEd L. Cashin 	put_lba(ah, buf->sector);
2861da177e4SLinus Torvalds 	if (d->flags & DEVFL_EXT) {
2871da177e4SLinus Torvalds 		ah->aflags |= AOEAFL_EXT;
2881da177e4SLinus Torvalds 	} else {
2891da177e4SLinus Torvalds 		extbit = 0;
2901da177e4SLinus Torvalds 		ah->lba3 &= 0x0f;
2911da177e4SLinus Torvalds 		ah->lba3 |= 0xe0;	/* LBA bit + obsolete 0xa0 */
2921da177e4SLinus Torvalds 	}
2931da177e4SLinus Torvalds 	if (bio_data_dir(buf->bio) == WRITE) {
294*3d5b0605SEd Cashin 		skb_fillup(skb, f->bv, f->bv_off, bcnt);
2951da177e4SLinus Torvalds 		ah->aflags |= AOEAFL_WRITE;
2964f51dc5eSEd L. Cashin 		skb->len += bcnt;
2974f51dc5eSEd L. Cashin 		skb->data_len = bcnt;
298*3d5b0605SEd Cashin 		skb->truesize += bcnt;
29968e0d42fSEd L. Cashin 		t->wpkts++;
3001da177e4SLinus Torvalds 	} else {
30168e0d42fSEd L. Cashin 		t->rpkts++;
3021da177e4SLinus Torvalds 		writebit = 0;
3031da177e4SLinus Torvalds 	}
3041da177e4SLinus Torvalds 
30504b3ab52SBartlomiej Zolnierkiewicz 	ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	/* mark all tracking fields and load out */
3081da177e4SLinus Torvalds 	buf->nframesout += 1;
3091da177e4SLinus Torvalds 	buf->sector += bcnt >> 9;
3101da177e4SLinus Torvalds 
31168e0d42fSEd L. Cashin 	skb->dev = t->ifp->nd;
3124f51dc5eSEd L. Cashin 	skb = skb_clone(skb, GFP_ATOMIC);
313e9bb8fb0SDavid S. Miller 	if (skb)
314e9bb8fb0SDavid S. Miller 		__skb_queue_tail(&d->sendq, skb);
31568e0d42fSEd L. Cashin 	return 1;
31668e0d42fSEd L. Cashin }
3171da177e4SLinus Torvalds 
3183ae1c24eSEd L. Cashin /* some callers cannot sleep, and they can call this function,
3193ae1c24eSEd L. Cashin  * transmitting the packets later, when interrupts are on
3203ae1c24eSEd L. Cashin  */
321e9bb8fb0SDavid S. Miller static void
322e9bb8fb0SDavid S. Miller aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
3233ae1c24eSEd L. Cashin {
3243ae1c24eSEd L. Cashin 	struct aoe_hdr *h;
3253ae1c24eSEd L. Cashin 	struct aoe_cfghdr *ch;
326e9bb8fb0SDavid S. Miller 	struct sk_buff *skb;
3273ae1c24eSEd L. Cashin 	struct net_device *ifp;
3283ae1c24eSEd L. Cashin 
329840a185dSEric Dumazet 	rcu_read_lock();
330840a185dSEric Dumazet 	for_each_netdev_rcu(&init_net, ifp) {
3313ae1c24eSEd L. Cashin 		dev_hold(ifp);
3323ae1c24eSEd L. Cashin 		if (!is_aoe_netif(ifp))
3337562f876SPavel Emelianov 			goto cont;
3343ae1c24eSEd L. Cashin 
335e407a7f6SEd L. Cashin 		skb = new_skb(sizeof *h + sizeof *ch);
3363ae1c24eSEd L. Cashin 		if (skb == NULL) {
337a12c93f0SEd L. Cashin 			printk(KERN_INFO "aoe: skb alloc failure\n");
3387562f876SPavel Emelianov 			goto cont;
3393ae1c24eSEd L. Cashin 		}
34019900cdeSEd L. Cashin 		skb_put(skb, sizeof *h + sizeof *ch);
341e407a7f6SEd L. Cashin 		skb->dev = ifp;
342e9bb8fb0SDavid S. Miller 		__skb_queue_tail(queue, skb);
343abdbf94dSEd L. Cashin 		h = (struct aoe_hdr *) skb_mac_header(skb);
3443ae1c24eSEd L. Cashin 		memset(h, 0, sizeof *h + sizeof *ch);
3453ae1c24eSEd L. Cashin 
3463ae1c24eSEd L. Cashin 		memset(h->dst, 0xff, sizeof h->dst);
3473ae1c24eSEd L. Cashin 		memcpy(h->src, ifp->dev_addr, sizeof h->src);
3483ae1c24eSEd L. Cashin 		h->type = __constant_cpu_to_be16(ETH_P_AOE);
3493ae1c24eSEd L. Cashin 		h->verfl = AOE_HVER;
3503ae1c24eSEd L. Cashin 		h->major = cpu_to_be16(aoemajor);
3513ae1c24eSEd L. Cashin 		h->minor = aoeminor;
3523ae1c24eSEd L. Cashin 		h->cmd = AOECMD_CFG;
3533ae1c24eSEd L. Cashin 
3547562f876SPavel Emelianov cont:
3557562f876SPavel Emelianov 		dev_put(ifp);
3563ae1c24eSEd L. Cashin 	}
357840a185dSEric Dumazet 	rcu_read_unlock();
3583ae1c24eSEd L. Cashin }
3593ae1c24eSEd L. Cashin 
3601da177e4SLinus Torvalds static void
36168e0d42fSEd L. Cashin resend(struct aoedev *d, struct aoetgt *t, struct frame *f)
3621da177e4SLinus Torvalds {
3631da177e4SLinus Torvalds 	struct sk_buff *skb;
3641da177e4SLinus Torvalds 	struct aoe_hdr *h;
36519bf2635SEd L. Cashin 	struct aoe_atahdr *ah;
3661da177e4SLinus Torvalds 	char buf[128];
3671da177e4SLinus Torvalds 	u32 n;
3681da177e4SLinus Torvalds 
36968e0d42fSEd L. Cashin 	ifrotate(t);
37068e0d42fSEd L. Cashin 	n = newtag(t);
371e407a7f6SEd L. Cashin 	skb = f->skb;
372abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
37319bf2635SEd L. Cashin 	ah = (struct aoe_atahdr *) (h+1);
37468e0d42fSEd L. Cashin 
37568e0d42fSEd L. Cashin 	snprintf(buf, sizeof buf,
376411c41eeSHarvey Harrison 		"%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
37768e0d42fSEd L. Cashin 		"retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
378411c41eeSHarvey Harrison 		h->src, h->dst, t->nout);
37968e0d42fSEd L. Cashin 	aoechr_error(buf);
38068e0d42fSEd L. Cashin 
3811da177e4SLinus Torvalds 	f->tag = n;
38263e9cc5dSecashin@coraid.com 	h->tag = cpu_to_be32(n);
38368e0d42fSEd L. Cashin 	memcpy(h->dst, t->addr, sizeof h->dst);
38468e0d42fSEd L. Cashin 	memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
3851da177e4SLinus Torvalds 
38668e0d42fSEd L. Cashin 	switch (ah->cmdstat) {
38768e0d42fSEd L. Cashin 	default:
38868e0d42fSEd L. Cashin 		break;
38904b3ab52SBartlomiej Zolnierkiewicz 	case ATA_CMD_PIO_READ:
39004b3ab52SBartlomiej Zolnierkiewicz 	case ATA_CMD_PIO_READ_EXT:
39104b3ab52SBartlomiej Zolnierkiewicz 	case ATA_CMD_PIO_WRITE:
39204b3ab52SBartlomiej Zolnierkiewicz 	case ATA_CMD_PIO_WRITE_EXT:
39368e0d42fSEd L. Cashin 		put_lba(ah, f->lba);
39468e0d42fSEd L. Cashin 
39568e0d42fSEd L. Cashin 		n = f->bcnt;
39668e0d42fSEd L. Cashin 		ah->scnt = n >> 9;
3974f51dc5eSEd L. Cashin 		if (ah->aflags & AOEAFL_WRITE) {
398*3d5b0605SEd Cashin 			skb_fillup(skb, f->bv, f->bv_off, n);
39968e0d42fSEd L. Cashin 			skb->len = sizeof *h + sizeof *ah + n;
40068e0d42fSEd L. Cashin 			skb->data_len = n;
401*3d5b0605SEd Cashin 			skb->truesize += n;
40219bf2635SEd L. Cashin 		}
40319bf2635SEd L. Cashin 	}
40468e0d42fSEd L. Cashin 	skb->dev = t->ifp->nd;
4054f51dc5eSEd L. Cashin 	skb = skb_clone(skb, GFP_ATOMIC);
4064f51dc5eSEd L. Cashin 	if (skb == NULL)
4074f51dc5eSEd L. Cashin 		return;
408e9bb8fb0SDavid S. Miller 	__skb_queue_tail(&d->sendq, skb);
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds static int
4121da177e4SLinus Torvalds tsince(int tag)
4131da177e4SLinus Torvalds {
4141da177e4SLinus Torvalds 	int n;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	n = jiffies & 0xffff;
4171da177e4SLinus Torvalds 	n -= tag & 0xffff;
4181da177e4SLinus Torvalds 	if (n < 0)
4191da177e4SLinus Torvalds 		n += 1<<16;
4201da177e4SLinus Torvalds 	return n;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
42368e0d42fSEd L. Cashin static struct aoeif *
42468e0d42fSEd L. Cashin getif(struct aoetgt *t, struct net_device *nd)
42568e0d42fSEd L. Cashin {
42668e0d42fSEd L. Cashin 	struct aoeif *p, *e;
42768e0d42fSEd L. Cashin 
42868e0d42fSEd L. Cashin 	p = t->ifs;
42968e0d42fSEd L. Cashin 	e = p + NAOEIFS;
43068e0d42fSEd L. Cashin 	for (; p < e; p++)
43168e0d42fSEd L. Cashin 		if (p->nd == nd)
43268e0d42fSEd L. Cashin 			return p;
43368e0d42fSEd L. Cashin 	return NULL;
43468e0d42fSEd L. Cashin }
43568e0d42fSEd L. Cashin 
43668e0d42fSEd L. Cashin static struct aoeif *
43768e0d42fSEd L. Cashin addif(struct aoetgt *t, struct net_device *nd)
43868e0d42fSEd L. Cashin {
43968e0d42fSEd L. Cashin 	struct aoeif *p;
44068e0d42fSEd L. Cashin 
44168e0d42fSEd L. Cashin 	p = getif(t, NULL);
44268e0d42fSEd L. Cashin 	if (!p)
44368e0d42fSEd L. Cashin 		return NULL;
44468e0d42fSEd L. Cashin 	p->nd = nd;
44568e0d42fSEd L. Cashin 	p->maxbcnt = DEFAULTBCNT;
44668e0d42fSEd L. Cashin 	p->lost = 0;
44768e0d42fSEd L. Cashin 	p->lostjumbo = 0;
44868e0d42fSEd L. Cashin 	return p;
44968e0d42fSEd L. Cashin }
45068e0d42fSEd L. Cashin 
45168e0d42fSEd L. Cashin static void
45268e0d42fSEd L. Cashin ejectif(struct aoetgt *t, struct aoeif *ifp)
45368e0d42fSEd L. Cashin {
45468e0d42fSEd L. Cashin 	struct aoeif *e;
45568e0d42fSEd L. Cashin 	ulong n;
45668e0d42fSEd L. Cashin 
45768e0d42fSEd L. Cashin 	e = t->ifs + NAOEIFS - 1;
45868e0d42fSEd L. Cashin 	n = (e - ifp) * sizeof *ifp;
45968e0d42fSEd L. Cashin 	memmove(ifp, ifp+1, n);
46068e0d42fSEd L. Cashin 	e->nd = NULL;
46168e0d42fSEd L. Cashin }
46268e0d42fSEd L. Cashin 
46368e0d42fSEd L. Cashin static int
46468e0d42fSEd L. Cashin sthtith(struct aoedev *d)
46568e0d42fSEd L. Cashin {
46668e0d42fSEd L. Cashin 	struct frame *f, *e, *nf;
46768e0d42fSEd L. Cashin 	struct sk_buff *skb;
46868e0d42fSEd L. Cashin 	struct aoetgt *ht = *d->htgt;
46968e0d42fSEd L. Cashin 
47068e0d42fSEd L. Cashin 	f = ht->frames;
47168e0d42fSEd L. Cashin 	e = f + ht->nframes;
47268e0d42fSEd L. Cashin 	for (; f < e; f++) {
47368e0d42fSEd L. Cashin 		if (f->tag == FREETAG)
47468e0d42fSEd L. Cashin 			continue;
47568e0d42fSEd L. Cashin 		nf = freeframe(d);
47668e0d42fSEd L. Cashin 		if (!nf)
47768e0d42fSEd L. Cashin 			return 0;
47868e0d42fSEd L. Cashin 		skb = nf->skb;
47968e0d42fSEd L. Cashin 		*nf = *f;
48068e0d42fSEd L. Cashin 		f->skb = skb;
48168e0d42fSEd L. Cashin 		f->tag = FREETAG;
48268e0d42fSEd L. Cashin 		nf->waited = 0;
48368e0d42fSEd L. Cashin 		ht->nout--;
48468e0d42fSEd L. Cashin 		(*d->tgt)->nout++;
48568e0d42fSEd L. Cashin 		resend(d, *d->tgt, nf);
48668e0d42fSEd L. Cashin 	}
48768e0d42fSEd L. Cashin 	/* he's clean, he's useless.  take away his interfaces */
48868e0d42fSEd L. Cashin 	memset(ht->ifs, 0, sizeof ht->ifs);
48968e0d42fSEd L. Cashin 	d->htgt = NULL;
49068e0d42fSEd L. Cashin 	return 1;
49168e0d42fSEd L. Cashin }
49268e0d42fSEd L. Cashin 
49368e0d42fSEd L. Cashin static inline unsigned char
49468e0d42fSEd L. Cashin ata_scnt(unsigned char *packet) {
49568e0d42fSEd L. Cashin 	struct aoe_hdr *h;
49668e0d42fSEd L. Cashin 	struct aoe_atahdr *ah;
49768e0d42fSEd L. Cashin 
49868e0d42fSEd L. Cashin 	h = (struct aoe_hdr *) packet;
49968e0d42fSEd L. Cashin 	ah = (struct aoe_atahdr *) (h+1);
50068e0d42fSEd L. Cashin 	return ah->scnt;
50168e0d42fSEd L. Cashin }
50268e0d42fSEd L. Cashin 
5031da177e4SLinus Torvalds static void
5041da177e4SLinus Torvalds rexmit_timer(ulong vp)
5051da177e4SLinus Torvalds {
506e9bb8fb0SDavid S. Miller 	struct sk_buff_head queue;
5071da177e4SLinus Torvalds 	struct aoedev *d;
50868e0d42fSEd L. Cashin 	struct aoetgt *t, **tt, **te;
50968e0d42fSEd L. Cashin 	struct aoeif *ifp;
5101da177e4SLinus Torvalds 	struct frame *f, *e;
5111da177e4SLinus Torvalds 	register long timeout;
5121da177e4SLinus Torvalds 	ulong flags, n;
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	d = (struct aoedev *) vp;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	/* timeout is always ~150% of the moving average */
5171da177e4SLinus Torvalds 	timeout = d->rttavg;
5181da177e4SLinus Torvalds 	timeout += timeout >> 1;
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	spin_lock_irqsave(&d->lock, flags);
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	if (d->flags & DEVFL_TKILL) {
5231c6f3fcaSEd L. Cashin 		spin_unlock_irqrestore(&d->lock, flags);
5241da177e4SLinus Torvalds 		return;
5251da177e4SLinus Torvalds 	}
52668e0d42fSEd L. Cashin 	tt = d->targets;
52768e0d42fSEd L. Cashin 	te = tt + NTARGETS;
52868e0d42fSEd L. Cashin 	for (; tt < te && *tt; tt++) {
52968e0d42fSEd L. Cashin 		t = *tt;
53068e0d42fSEd L. Cashin 		f = t->frames;
53168e0d42fSEd L. Cashin 		e = f + t->nframes;
5321da177e4SLinus Torvalds 		for (; f < e; f++) {
53368e0d42fSEd L. Cashin 			if (f->tag == FREETAG
53468e0d42fSEd L. Cashin 			|| tsince(f->tag) < timeout)
53568e0d42fSEd L. Cashin 				continue;
5361da177e4SLinus Torvalds 			n = f->waited += timeout;
5371da177e4SLinus Torvalds 			n /= HZ;
53868e0d42fSEd L. Cashin 			if (n > aoe_deadsecs) {
53968e0d42fSEd L. Cashin 				/* waited too long.  device failure. */
5401da177e4SLinus Torvalds 				aoedev_downdev(d);
5411c6f3fcaSEd L. Cashin 				break;
5421da177e4SLinus Torvalds 			}
54368e0d42fSEd L. Cashin 
54468e0d42fSEd L. Cashin 			if (n > HELPWAIT /* see if another target can help */
54568e0d42fSEd L. Cashin 			&& (tt != d->targets || d->targets[1]))
54668e0d42fSEd L. Cashin 				d->htgt = tt;
54768e0d42fSEd L. Cashin 
54868e0d42fSEd L. Cashin 			if (t->nout == t->maxout) {
54968e0d42fSEd L. Cashin 				if (t->maxout > 1)
55068e0d42fSEd L. Cashin 					t->maxout--;
55168e0d42fSEd L. Cashin 				t->lastwadj = jiffies;
55268e0d42fSEd L. Cashin 			}
55368e0d42fSEd L. Cashin 
55468e0d42fSEd L. Cashin 			ifp = getif(t, f->skb->dev);
55568e0d42fSEd L. Cashin 			if (ifp && ++ifp->lost > (t->nframes << 1)
55668e0d42fSEd L. Cashin 			&& (ifp != t->ifs || t->ifs[1].nd)) {
55768e0d42fSEd L. Cashin 				ejectif(t, ifp);
55868e0d42fSEd L. Cashin 				ifp = NULL;
55968e0d42fSEd L. Cashin 			}
56068e0d42fSEd L. Cashin 			resend(d, t, f);
56168e0d42fSEd L. Cashin 		}
56268e0d42fSEd L. Cashin 
56368e0d42fSEd L. Cashin 		/* window check */
56468e0d42fSEd L. Cashin 		if (t->nout == t->maxout
56568e0d42fSEd L. Cashin 		&& t->maxout < t->nframes
56668e0d42fSEd L. Cashin 		&& (jiffies - t->lastwadj)/HZ > 10) {
56768e0d42fSEd L. Cashin 			t->maxout++;
56868e0d42fSEd L. Cashin 			t->lastwadj = jiffies;
5691da177e4SLinus Torvalds 		}
5701da177e4SLinus Torvalds 	}
57168e0d42fSEd L. Cashin 
572e9bb8fb0SDavid S. Miller 	if (!skb_queue_empty(&d->sendq)) {
57368e0d42fSEd L. Cashin 		n = d->rttavg <<= 1;
57468e0d42fSEd L. Cashin 		if (n > MAXTIMER)
57568e0d42fSEd L. Cashin 			d->rttavg = MAXTIMER;
57668e0d42fSEd L. Cashin 	}
57768e0d42fSEd L. Cashin 
57868e0d42fSEd L. Cashin 	if (d->flags & DEVFL_KICKME || d->htgt) {
5794f51dc5eSEd L. Cashin 		d->flags &= ~DEVFL_KICKME;
5804f51dc5eSEd L. Cashin 		aoecmd_work(d);
5814f51dc5eSEd L. Cashin 	}
5821da177e4SLinus Torvalds 
583e9bb8fb0SDavid S. Miller 	__skb_queue_head_init(&queue);
584e9bb8fb0SDavid S. Miller 	skb_queue_splice_init(&d->sendq, &queue);
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	d->timer.expires = jiffies + TIMERTICK;
5871da177e4SLinus Torvalds 	add_timer(&d->timer);
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	spin_unlock_irqrestore(&d->lock, flags);
5901da177e4SLinus Torvalds 
591e9bb8fb0SDavid S. Miller 	aoenet_xmit(&queue);
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
59468e0d42fSEd L. Cashin /* enters with d->lock held */
59568e0d42fSEd L. Cashin void
59668e0d42fSEd L. Cashin aoecmd_work(struct aoedev *d)
59768e0d42fSEd L. Cashin {
59868e0d42fSEd L. Cashin 	struct buf *buf;
59968e0d42fSEd L. Cashin loop:
60068e0d42fSEd L. Cashin 	if (d->htgt && !sthtith(d))
60168e0d42fSEd L. Cashin 		return;
60268e0d42fSEd L. Cashin 	if (d->inprocess == NULL) {
60368e0d42fSEd L. Cashin 		if (list_empty(&d->bufq))
60468e0d42fSEd L. Cashin 			return;
60568e0d42fSEd L. Cashin 		buf = container_of(d->bufq.next, struct buf, bufs);
60668e0d42fSEd L. Cashin 		list_del(d->bufq.next);
60768e0d42fSEd L. Cashin 		d->inprocess = buf;
60868e0d42fSEd L. Cashin 	}
60968e0d42fSEd L. Cashin 	if (aoecmd_ata_rw(d))
61068e0d42fSEd L. Cashin 		goto loop;
61168e0d42fSEd L. Cashin }
61268e0d42fSEd L. Cashin 
6133ae1c24eSEd L. Cashin /* this function performs work that has been deferred until sleeping is OK
6143ae1c24eSEd L. Cashin  */
6153ae1c24eSEd L. Cashin void
616c4028958SDavid Howells aoecmd_sleepwork(struct work_struct *work)
6173ae1c24eSEd L. Cashin {
618c4028958SDavid Howells 	struct aoedev *d = container_of(work, struct aoedev, work);
6193ae1c24eSEd L. Cashin 
6203ae1c24eSEd L. Cashin 	if (d->flags & DEVFL_GDALLOC)
6213ae1c24eSEd L. Cashin 		aoeblk_gdalloc(d);
6223ae1c24eSEd L. Cashin 
6233ae1c24eSEd L. Cashin 	if (d->flags & DEVFL_NEWSIZE) {
6243ae1c24eSEd L. Cashin 		struct block_device *bd;
6253ae1c24eSEd L. Cashin 		unsigned long flags;
6263ae1c24eSEd L. Cashin 		u64 ssize;
6273ae1c24eSEd L. Cashin 
62880795aefSTejun Heo 		ssize = get_capacity(d->gd);
6293ae1c24eSEd L. Cashin 		bd = bdget_disk(d->gd, 0);
6303ae1c24eSEd L. Cashin 
6313ae1c24eSEd L. Cashin 		if (bd) {
6323ae1c24eSEd L. Cashin 			mutex_lock(&bd->bd_inode->i_mutex);
6333ae1c24eSEd L. Cashin 			i_size_write(bd->bd_inode, (loff_t)ssize<<9);
6343ae1c24eSEd L. Cashin 			mutex_unlock(&bd->bd_inode->i_mutex);
6353ae1c24eSEd L. Cashin 			bdput(bd);
6363ae1c24eSEd L. Cashin 		}
6373ae1c24eSEd L. Cashin 		spin_lock_irqsave(&d->lock, flags);
6383ae1c24eSEd L. Cashin 		d->flags |= DEVFL_UP;
6393ae1c24eSEd L. Cashin 		d->flags &= ~DEVFL_NEWSIZE;
6403ae1c24eSEd L. Cashin 		spin_unlock_irqrestore(&d->lock, flags);
6413ae1c24eSEd L. Cashin 	}
6423ae1c24eSEd L. Cashin }
6433ae1c24eSEd L. Cashin 
6441da177e4SLinus Torvalds static void
64568e0d42fSEd L. Cashin ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
6461da177e4SLinus Torvalds {
6471da177e4SLinus Torvalds 	u64 ssize;
6481da177e4SLinus Torvalds 	u16 n;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	/* word 83: command set supported */
651f885f8d1SHarvey Harrison 	n = get_unaligned_le16(&id[83 << 1]);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	/* word 86: command set/feature enabled */
654f885f8d1SHarvey Harrison 	n |= get_unaligned_le16(&id[86 << 1]);
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	if (n & (1<<10)) {	/* bit 10: LBA 48 */
6571da177e4SLinus Torvalds 		d->flags |= DEVFL_EXT;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 		/* word 100: number lba48 sectors */
660f885f8d1SHarvey Harrison 		ssize = get_unaligned_le64(&id[100 << 1]);
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 		/* set as in ide-disk.c:init_idedisk_capacity */
6631da177e4SLinus Torvalds 		d->geo.cylinders = ssize;
6641da177e4SLinus Torvalds 		d->geo.cylinders /= (255 * 63);
6651da177e4SLinus Torvalds 		d->geo.heads = 255;
6661da177e4SLinus Torvalds 		d->geo.sectors = 63;
6671da177e4SLinus Torvalds 	} else {
6681da177e4SLinus Torvalds 		d->flags &= ~DEVFL_EXT;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 		/* number lba28 sectors */
671f885f8d1SHarvey Harrison 		ssize = get_unaligned_le32(&id[60 << 1]);
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 		/* NOTE: obsolete in ATA 6 */
674f885f8d1SHarvey Harrison 		d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
675f885f8d1SHarvey Harrison 		d->geo.heads = get_unaligned_le16(&id[55 << 1]);
676f885f8d1SHarvey Harrison 		d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
6771da177e4SLinus Torvalds 	}
6783ae1c24eSEd L. Cashin 
6793ae1c24eSEd L. Cashin 	if (d->ssize != ssize)
6801d75981aSEd L. Cashin 		printk(KERN_INFO
681411c41eeSHarvey Harrison 			"aoe: %pm e%ld.%d v%04x has %llu sectors\n",
682411c41eeSHarvey Harrison 			t->addr,
6833ae1c24eSEd L. Cashin 			d->aoemajor, d->aoeminor,
6843ae1c24eSEd L. Cashin 			d->fw_ver, (long long)ssize);
6851da177e4SLinus Torvalds 	d->ssize = ssize;
6861da177e4SLinus Torvalds 	d->geo.start = 0;
6876b9699bbSEd L. Cashin 	if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
6886b9699bbSEd L. Cashin 		return;
6891da177e4SLinus Torvalds 	if (d->gd != NULL) {
69080795aefSTejun Heo 		set_capacity(d->gd, ssize);
6913ae1c24eSEd L. Cashin 		d->flags |= DEVFL_NEWSIZE;
69268e0d42fSEd L. Cashin 	} else
6933ae1c24eSEd L. Cashin 		d->flags |= DEVFL_GDALLOC;
6941da177e4SLinus Torvalds 	schedule_work(&d->work);
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds static void
6981da177e4SLinus Torvalds calc_rttavg(struct aoedev *d, int rtt)
6991da177e4SLinus Torvalds {
7001da177e4SLinus Torvalds 	register long n;
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	n = rtt;
703dced3a05SEd L. Cashin 	if (n < 0) {
704dced3a05SEd L. Cashin 		n = -rtt;
7051da177e4SLinus Torvalds 		if (n < MINTIMER)
7061da177e4SLinus Torvalds 			n = MINTIMER;
7071da177e4SLinus Torvalds 		else if (n > MAXTIMER)
7081da177e4SLinus Torvalds 			n = MAXTIMER;
709dced3a05SEd L. Cashin 		d->mintimer += (n - d->mintimer) >> 1;
710dced3a05SEd L. Cashin 	} else if (n < d->mintimer)
711dced3a05SEd L. Cashin 		n = d->mintimer;
712dced3a05SEd L. Cashin 	else if (n > MAXTIMER)
713dced3a05SEd L. Cashin 		n = MAXTIMER;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	/* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
7161da177e4SLinus Torvalds 	n -= d->rttavg;
7171da177e4SLinus Torvalds 	d->rttavg += n >> 2;
7181da177e4SLinus Torvalds }
7191da177e4SLinus Torvalds 
72068e0d42fSEd L. Cashin static struct aoetgt *
72168e0d42fSEd L. Cashin gettgt(struct aoedev *d, char *addr)
72268e0d42fSEd L. Cashin {
72368e0d42fSEd L. Cashin 	struct aoetgt **t, **e;
72468e0d42fSEd L. Cashin 
72568e0d42fSEd L. Cashin 	t = d->targets;
72668e0d42fSEd L. Cashin 	e = t + NTARGETS;
72768e0d42fSEd L. Cashin 	for (; t < e && *t; t++)
72868e0d42fSEd L. Cashin 		if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
72968e0d42fSEd L. Cashin 			return *t;
73068e0d42fSEd L. Cashin 	return NULL;
73168e0d42fSEd L. Cashin }
73268e0d42fSEd L. Cashin 
73368e0d42fSEd L. Cashin static inline void
73403054de1SLinus Torvalds diskstats(struct gendisk *disk, struct bio *bio, ulong duration, sector_t sector)
73568e0d42fSEd L. Cashin {
73668e0d42fSEd L. Cashin 	unsigned long n_sect = bio->bi_size >> 9;
73768e0d42fSEd L. Cashin 	const int rw = bio_data_dir(bio);
73828f13702SJens Axboe 	struct hd_struct *part;
739c9959059STejun Heo 	int cpu;
74068e0d42fSEd L. Cashin 
741074a7acaSTejun Heo 	cpu = part_stat_lock();
742e71bf0d0STejun Heo 	part = disk_map_sector_rcu(disk, sector);
743e71bf0d0STejun Heo 
744074a7acaSTejun Heo 	part_stat_inc(cpu, part, ios[rw]);
745074a7acaSTejun Heo 	part_stat_add(cpu, part, ticks[rw], duration);
746074a7acaSTejun Heo 	part_stat_add(cpu, part, sectors[rw], n_sect);
747074a7acaSTejun Heo 	part_stat_add(cpu, part, io_ticks, duration);
748c9959059STejun Heo 
749074a7acaSTejun Heo 	part_stat_unlock();
75068e0d42fSEd L. Cashin }
75168e0d42fSEd L. Cashin 
752*3d5b0605SEd Cashin static void
753*3d5b0605SEd Cashin bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, ulong cnt)
754*3d5b0605SEd Cashin {
755*3d5b0605SEd Cashin 	ulong fcnt;
756*3d5b0605SEd Cashin 	char *p;
757*3d5b0605SEd Cashin 	int soff = 0;
758*3d5b0605SEd Cashin loop:
759*3d5b0605SEd Cashin 	fcnt = bv->bv_len - (off - bv->bv_offset);
760*3d5b0605SEd Cashin 	if (fcnt > cnt)
761*3d5b0605SEd Cashin 		fcnt = cnt;
762*3d5b0605SEd Cashin 	p = page_address(bv->bv_page) + off;
763*3d5b0605SEd Cashin 	skb_copy_bits(skb, soff, p, fcnt);
764*3d5b0605SEd Cashin 	soff += fcnt;
765*3d5b0605SEd Cashin 	cnt -= fcnt;
766*3d5b0605SEd Cashin 	if (cnt <= 0)
767*3d5b0605SEd Cashin 		return;
768*3d5b0605SEd Cashin 	bv++;
769*3d5b0605SEd Cashin 	off = bv->bv_offset;
770*3d5b0605SEd Cashin 	goto loop;
771*3d5b0605SEd Cashin }
772*3d5b0605SEd Cashin 
773*3d5b0605SEd Cashin static void
774*3d5b0605SEd Cashin fadvance(struct frame *f, ulong cnt)
775*3d5b0605SEd Cashin {
776*3d5b0605SEd Cashin 	ulong fcnt;
777*3d5b0605SEd Cashin 
778*3d5b0605SEd Cashin 	f->lba += cnt >> 9;
779*3d5b0605SEd Cashin loop:
780*3d5b0605SEd Cashin 	fcnt = f->bv->bv_len - (f->bv_off - f->bv->bv_offset);
781*3d5b0605SEd Cashin 	if (fcnt > cnt) {
782*3d5b0605SEd Cashin 		f->bv_off += cnt;
783*3d5b0605SEd Cashin 		return;
784*3d5b0605SEd Cashin 	}
785*3d5b0605SEd Cashin 	cnt -= fcnt;
786*3d5b0605SEd Cashin 	f->bv++;
787*3d5b0605SEd Cashin 	f->bv_off = f->bv->bv_offset;
788*3d5b0605SEd Cashin 	goto loop;
789*3d5b0605SEd Cashin }
790*3d5b0605SEd Cashin 
7911da177e4SLinus Torvalds void
7921da177e4SLinus Torvalds aoecmd_ata_rsp(struct sk_buff *skb)
7931da177e4SLinus Torvalds {
794e9bb8fb0SDavid S. Miller 	struct sk_buff_head queue;
7951da177e4SLinus Torvalds 	struct aoedev *d;
796ddec63e8SEd L. Cashin 	struct aoe_hdr *hin, *hout;
7971da177e4SLinus Torvalds 	struct aoe_atahdr *ahin, *ahout;
7981da177e4SLinus Torvalds 	struct frame *f;
7991da177e4SLinus Torvalds 	struct buf *buf;
80068e0d42fSEd L. Cashin 	struct aoetgt *t;
80168e0d42fSEd L. Cashin 	struct aoeif *ifp;
8021da177e4SLinus Torvalds 	register long n;
8031da177e4SLinus Torvalds 	ulong flags;
8041da177e4SLinus Torvalds 	char ebuf[128];
80532465c65Secashin@coraid.com 	u16 aoemajor;
8061da177e4SLinus Torvalds 
807abdbf94dSEd L. Cashin 	hin = (struct aoe_hdr *) skb_mac_header(skb);
808*3d5b0605SEd Cashin 	skb_pull(skb, sizeof(*hin));
809f885f8d1SHarvey Harrison 	aoemajor = get_unaligned_be16(&hin->major);
81032465c65Secashin@coraid.com 	d = aoedev_by_aoeaddr(aoemajor, hin->minor);
8111da177e4SLinus Torvalds 	if (d == NULL) {
8121da177e4SLinus Torvalds 		snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
8131da177e4SLinus Torvalds 			"for unknown device %d.%d\n",
81432465c65Secashin@coraid.com 			 aoemajor, hin->minor);
8151da177e4SLinus Torvalds 		aoechr_error(ebuf);
8161da177e4SLinus Torvalds 		return;
8171da177e4SLinus Torvalds 	}
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	spin_lock_irqsave(&d->lock, flags);
8201da177e4SLinus Torvalds 
821f885f8d1SHarvey Harrison 	n = get_unaligned_be32(&hin->tag);
82268e0d42fSEd L. Cashin 	t = gettgt(d, hin->src);
82368e0d42fSEd L. Cashin 	if (t == NULL) {
824411c41eeSHarvey Harrison 		printk(KERN_INFO "aoe: can't find target e%ld.%d:%pm\n",
825411c41eeSHarvey Harrison 			d->aoemajor, d->aoeminor, hin->src);
82668e0d42fSEd L. Cashin 		spin_unlock_irqrestore(&d->lock, flags);
82768e0d42fSEd L. Cashin 		return;
82868e0d42fSEd L. Cashin 	}
82968e0d42fSEd L. Cashin 	f = getframe(t, n);
8301da177e4SLinus Torvalds 	if (f == NULL) {
831dced3a05SEd L. Cashin 		calc_rttavg(d, -tsince(n));
8321da177e4SLinus Torvalds 		spin_unlock_irqrestore(&d->lock, flags);
8331da177e4SLinus Torvalds 		snprintf(ebuf, sizeof ebuf,
8341da177e4SLinus Torvalds 			"%15s e%d.%d    tag=%08x@%08lx\n",
8351da177e4SLinus Torvalds 			"unexpected rsp",
836f885f8d1SHarvey Harrison 			get_unaligned_be16(&hin->major),
8371da177e4SLinus Torvalds 			hin->minor,
838f885f8d1SHarvey Harrison 			get_unaligned_be32(&hin->tag),
8391da177e4SLinus Torvalds 			jiffies);
8401da177e4SLinus Torvalds 		aoechr_error(ebuf);
8411da177e4SLinus Torvalds 		return;
8421da177e4SLinus Torvalds 	}
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	calc_rttavg(d, tsince(f->tag));
8451da177e4SLinus Torvalds 
846*3d5b0605SEd Cashin 	ahin = (struct aoe_atahdr *) skb->data;
847*3d5b0605SEd Cashin 	skb_pull(skb, sizeof(*ahin));
848abdbf94dSEd L. Cashin 	hout = (struct aoe_hdr *) skb_mac_header(f->skb);
849ddec63e8SEd L. Cashin 	ahout = (struct aoe_atahdr *) (hout+1);
8501da177e4SLinus Torvalds 	buf = f->buf;
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	if (ahin->cmdstat & 0xa9) {	/* these bits cleared on success */
853a12c93f0SEd L. Cashin 		printk(KERN_ERR
8541d75981aSEd L. Cashin 			"aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
8551da177e4SLinus Torvalds 			ahout->cmdstat, ahin->cmdstat,
8561da177e4SLinus Torvalds 			d->aoemajor, d->aoeminor);
8571da177e4SLinus Torvalds 		if (buf)
8581da177e4SLinus Torvalds 			buf->flags |= BUFFL_FAIL;
8591da177e4SLinus Torvalds 	} else {
86068e0d42fSEd L. Cashin 		if (d->htgt && t == *d->htgt) /* I'll help myself, thank you. */
86168e0d42fSEd L. Cashin 			d->htgt = NULL;
86219bf2635SEd L. Cashin 		n = ahout->scnt << 9;
8631da177e4SLinus Torvalds 		switch (ahout->cmdstat) {
86404b3ab52SBartlomiej Zolnierkiewicz 		case ATA_CMD_PIO_READ:
86504b3ab52SBartlomiej Zolnierkiewicz 		case ATA_CMD_PIO_READ_EXT:
866*3d5b0605SEd Cashin 			if (skb->len < n) {
867a12c93f0SEd L. Cashin 				printk(KERN_ERR
86868e0d42fSEd L. Cashin 					"aoe: %s.  skb->len=%d need=%ld\n",
86968e0d42fSEd L. Cashin 					"runt data size in read", skb->len, n);
8701da177e4SLinus Torvalds 				/* fail frame f?  just returning will rexmit. */
8711da177e4SLinus Torvalds 				spin_unlock_irqrestore(&d->lock, flags);
8721da177e4SLinus Torvalds 				return;
8731da177e4SLinus Torvalds 			}
874*3d5b0605SEd Cashin 			bvcpy(f->bv, f->bv_off, skb, n);
87504b3ab52SBartlomiej Zolnierkiewicz 		case ATA_CMD_PIO_WRITE:
87604b3ab52SBartlomiej Zolnierkiewicz 		case ATA_CMD_PIO_WRITE_EXT:
87768e0d42fSEd L. Cashin 			ifp = getif(t, skb->dev);
87868e0d42fSEd L. Cashin 			if (ifp) {
87968e0d42fSEd L. Cashin 				ifp->lost = 0;
88068e0d42fSEd L. Cashin 				if (n > DEFAULTBCNT)
88168e0d42fSEd L. Cashin 					ifp->lostjumbo = 0;
88268e0d42fSEd L. Cashin 			}
88319bf2635SEd L. Cashin 			if (f->bcnt -= n) {
884*3d5b0605SEd Cashin 				fadvance(f, n);
88568e0d42fSEd L. Cashin 				resend(d, t, f);
88668e0d42fSEd L. Cashin 				goto xmit;
8874f51dc5eSEd L. Cashin 			}
8881da177e4SLinus Torvalds 			break;
88904b3ab52SBartlomiej Zolnierkiewicz 		case ATA_CMD_ID_ATA:
890*3d5b0605SEd Cashin 			if (skb->len < 512) {
891a12c93f0SEd L. Cashin 				printk(KERN_INFO
892a12c93f0SEd L. Cashin 					"aoe: runt data size in ataid.  skb->len=%d\n",
8936bb6285fSEd L. Cashin 					skb->len);
8941da177e4SLinus Torvalds 				spin_unlock_irqrestore(&d->lock, flags);
8951da177e4SLinus Torvalds 				return;
8961da177e4SLinus Torvalds 			}
897*3d5b0605SEd Cashin 			if (skb_linearize(skb))
898*3d5b0605SEd Cashin 				break;
899*3d5b0605SEd Cashin 			ataid_complete(d, t, skb->data);
9001da177e4SLinus Torvalds 			break;
9011da177e4SLinus Torvalds 		default:
902a12c93f0SEd L. Cashin 			printk(KERN_INFO
903a12c93f0SEd L. Cashin 				"aoe: unrecognized ata command %2.2Xh for %d.%d\n",
9041da177e4SLinus Torvalds 				ahout->cmdstat,
905f885f8d1SHarvey Harrison 				get_unaligned_be16(&hin->major),
9061da177e4SLinus Torvalds 				hin->minor);
9071da177e4SLinus Torvalds 		}
9081da177e4SLinus Torvalds 	}
9091da177e4SLinus Torvalds 
91068e0d42fSEd L. Cashin 	if (buf && --buf->nframesout == 0 && buf->resid == 0) {
91103054de1SLinus Torvalds 		diskstats(d->gd, buf->bio, jiffies - buf->stime, buf->sector);
9120a1f127aSPeter Horton 		if (buf->flags & BUFFL_FAIL)
9130a1f127aSPeter Horton 			bio_endio(buf->bio, -EIO);
9140a1f127aSPeter Horton 		else {
9156ec1480dSAndrew Morton 			bio_flush_dcache_pages(buf->bio);
9160a1f127aSPeter Horton 			bio_endio(buf->bio, 0);
9170a1f127aSPeter Horton 		}
9181da177e4SLinus Torvalds 		mempool_free(buf, d->bufpool);
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 	f->buf = NULL;
9221da177e4SLinus Torvalds 	f->tag = FREETAG;
92368e0d42fSEd L. Cashin 	t->nout--;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	aoecmd_work(d);
92668e0d42fSEd L. Cashin xmit:
927e9bb8fb0SDavid S. Miller 	__skb_queue_head_init(&queue);
928e9bb8fb0SDavid S. Miller 	skb_queue_splice_init(&d->sendq, &queue);
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	spin_unlock_irqrestore(&d->lock, flags);
931e9bb8fb0SDavid S. Miller 	aoenet_xmit(&queue);
9321da177e4SLinus Torvalds }
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds void
9351da177e4SLinus Torvalds aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
9361da177e4SLinus Torvalds {
937e9bb8fb0SDavid S. Miller 	struct sk_buff_head queue;
9381da177e4SLinus Torvalds 
939e9bb8fb0SDavid S. Miller 	__skb_queue_head_init(&queue);
940e9bb8fb0SDavid S. Miller 	aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
941e9bb8fb0SDavid S. Miller 	aoenet_xmit(&queue);
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds 
94468e0d42fSEd L. Cashin struct sk_buff *
9451da177e4SLinus Torvalds aoecmd_ata_id(struct aoedev *d)
9461da177e4SLinus Torvalds {
9471da177e4SLinus Torvalds 	struct aoe_hdr *h;
9481da177e4SLinus Torvalds 	struct aoe_atahdr *ah;
9491da177e4SLinus Torvalds 	struct frame *f;
9501da177e4SLinus Torvalds 	struct sk_buff *skb;
95168e0d42fSEd L. Cashin 	struct aoetgt *t;
9521da177e4SLinus Torvalds 
9534f51dc5eSEd L. Cashin 	f = freeframe(d);
95468e0d42fSEd L. Cashin 	if (f == NULL)
9551da177e4SLinus Torvalds 		return NULL;
95668e0d42fSEd L. Cashin 
95768e0d42fSEd L. Cashin 	t = *d->tgt;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	/* initialize the headers & frame */
960e407a7f6SEd L. Cashin 	skb = f->skb;
961abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
9621da177e4SLinus Torvalds 	ah = (struct aoe_atahdr *) (h+1);
96319900cdeSEd L. Cashin 	skb_put(skb, sizeof *h + sizeof *ah);
96419900cdeSEd L. Cashin 	memset(h, 0, skb->len);
96568e0d42fSEd L. Cashin 	f->tag = aoehdr_atainit(d, t, h);
96668e0d42fSEd L. Cashin 	t->nout++;
9671da177e4SLinus Torvalds 	f->waited = 0;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	/* set up ata header */
9701da177e4SLinus Torvalds 	ah->scnt = 1;
97104b3ab52SBartlomiej Zolnierkiewicz 	ah->cmdstat = ATA_CMD_ID_ATA;
9721da177e4SLinus Torvalds 	ah->lba3 = 0xa0;
9731da177e4SLinus Torvalds 
97468e0d42fSEd L. Cashin 	skb->dev = t->ifp->nd;
9751da177e4SLinus Torvalds 
9763ae1c24eSEd L. Cashin 	d->rttavg = MAXTIMER;
9771da177e4SLinus Torvalds 	d->timer.function = rexmit_timer;
9781da177e4SLinus Torvalds 
9794f51dc5eSEd L. Cashin 	return skb_clone(skb, GFP_ATOMIC);
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
98268e0d42fSEd L. Cashin static struct aoetgt *
98368e0d42fSEd L. Cashin addtgt(struct aoedev *d, char *addr, ulong nframes)
98468e0d42fSEd L. Cashin {
98568e0d42fSEd L. Cashin 	struct aoetgt *t, **tt, **te;
98668e0d42fSEd L. Cashin 	struct frame *f, *e;
98768e0d42fSEd L. Cashin 
98868e0d42fSEd L. Cashin 	tt = d->targets;
98968e0d42fSEd L. Cashin 	te = tt + NTARGETS;
99068e0d42fSEd L. Cashin 	for (; tt < te && *tt; tt++)
99168e0d42fSEd L. Cashin 		;
99268e0d42fSEd L. Cashin 
993578c4aa0SEd L. Cashin 	if (tt == te) {
994578c4aa0SEd L. Cashin 		printk(KERN_INFO
995578c4aa0SEd L. Cashin 			"aoe: device addtgt failure; too many targets\n");
99668e0d42fSEd L. Cashin 		return NULL;
997578c4aa0SEd L. Cashin 	}
99868e0d42fSEd L. Cashin 	t = kcalloc(1, sizeof *t, GFP_ATOMIC);
99968e0d42fSEd L. Cashin 	f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
1000578c4aa0SEd L. Cashin 	if (!t || !f) {
1001578c4aa0SEd L. Cashin 		kfree(f);
10029bb237b6SEd L. Cashin 		kfree(t);
1003578c4aa0SEd L. Cashin 		printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
10049bb237b6SEd L. Cashin 		return NULL;
10059bb237b6SEd L. Cashin 	}
10069bb237b6SEd L. Cashin 
100768e0d42fSEd L. Cashin 	t->nframes = nframes;
100868e0d42fSEd L. Cashin 	t->frames = f;
100968e0d42fSEd L. Cashin 	e = f + nframes;
10109bb237b6SEd L. Cashin 	for (; f < e; f++)
101168e0d42fSEd L. Cashin 		f->tag = FREETAG;
101268e0d42fSEd L. Cashin 	memcpy(t->addr, addr, sizeof t->addr);
101368e0d42fSEd L. Cashin 	t->ifp = t->ifs;
101468e0d42fSEd L. Cashin 	t->maxout = t->nframes;
101568e0d42fSEd L. Cashin 	return *tt = t;
101668e0d42fSEd L. Cashin }
101768e0d42fSEd L. Cashin 
10181da177e4SLinus Torvalds void
10191da177e4SLinus Torvalds aoecmd_cfg_rsp(struct sk_buff *skb)
10201da177e4SLinus Torvalds {
10211da177e4SLinus Torvalds 	struct aoedev *d;
10221da177e4SLinus Torvalds 	struct aoe_hdr *h;
10231da177e4SLinus Torvalds 	struct aoe_cfghdr *ch;
102468e0d42fSEd L. Cashin 	struct aoetgt *t;
102568e0d42fSEd L. Cashin 	struct aoeif *ifp;
102663e9cc5dSecashin@coraid.com 	ulong flags, sysminor, aoemajor;
10271da177e4SLinus Torvalds 	struct sk_buff *sl;
102819bf2635SEd L. Cashin 	u16 n;
10291da177e4SLinus Torvalds 
1030abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
10311da177e4SLinus Torvalds 	ch = (struct aoe_cfghdr *) (h+1);
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	/*
10341da177e4SLinus Torvalds 	 * Enough people have their dip switches set backwards to
10351da177e4SLinus Torvalds 	 * warrant a loud message for this special case.
10361da177e4SLinus Torvalds 	 */
1037823ed72eSHarvey Harrison 	aoemajor = get_unaligned_be16(&h->major);
10381da177e4SLinus Torvalds 	if (aoemajor == 0xfff) {
1039a12c93f0SEd L. Cashin 		printk(KERN_ERR "aoe: Warning: shelf address is all ones.  "
10406bb6285fSEd L. Cashin 			"Check shelf dip switches.\n");
10411da177e4SLinus Torvalds 		return;
10421da177e4SLinus Torvalds 	}
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	sysminor = SYSMINOR(aoemajor, h->minor);
1045fc458dcdSecashin@coraid.com 	if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
1046a12c93f0SEd L. Cashin 		printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n",
1047fc458dcdSecashin@coraid.com 			aoemajor, (int) h->minor);
10481da177e4SLinus Torvalds 		return;
10491da177e4SLinus Torvalds 	}
10501da177e4SLinus Torvalds 
105119bf2635SEd L. Cashin 	n = be16_to_cpu(ch->bufcnt);
10527df620d8SEd L. Cashin 	if (n > aoe_maxout)	/* keep it reasonable */
10537df620d8SEd L. Cashin 		n = aoe_maxout;
10541da177e4SLinus Torvalds 
105568e0d42fSEd L. Cashin 	d = aoedev_by_sysminor_m(sysminor);
10561da177e4SLinus Torvalds 	if (d == NULL) {
1057a12c93f0SEd L. Cashin 		printk(KERN_INFO "aoe: device sysminor_m failure\n");
10581da177e4SLinus Torvalds 		return;
10591da177e4SLinus Torvalds 	}
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 	spin_lock_irqsave(&d->lock, flags);
10621da177e4SLinus Torvalds 
106368e0d42fSEd L. Cashin 	t = gettgt(d, h->src);
106468e0d42fSEd L. Cashin 	if (!t) {
106568e0d42fSEd L. Cashin 		t = addtgt(d, h->src, n);
106668e0d42fSEd L. Cashin 		if (!t) {
106768e0d42fSEd L. Cashin 			spin_unlock_irqrestore(&d->lock, flags);
106868e0d42fSEd L. Cashin 			return;
106968e0d42fSEd L. Cashin 		}
107068e0d42fSEd L. Cashin 	}
107168e0d42fSEd L. Cashin 	ifp = getif(t, skb->dev);
107268e0d42fSEd L. Cashin 	if (!ifp) {
107368e0d42fSEd L. Cashin 		ifp = addif(t, skb->dev);
107468e0d42fSEd L. Cashin 		if (!ifp) {
107568e0d42fSEd L. Cashin 			printk(KERN_INFO
107668e0d42fSEd L. Cashin 				"aoe: device addif failure; "
107768e0d42fSEd L. Cashin 				"too many interfaces?\n");
107868e0d42fSEd L. Cashin 			spin_unlock_irqrestore(&d->lock, flags);
107968e0d42fSEd L. Cashin 			return;
108068e0d42fSEd L. Cashin 		}
108168e0d42fSEd L. Cashin 	}
108268e0d42fSEd L. Cashin 	if (ifp->maxbcnt) {
108368e0d42fSEd L. Cashin 		n = ifp->nd->mtu;
108419bf2635SEd L. Cashin 		n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr);
108519bf2635SEd L. Cashin 		n /= 512;
108619bf2635SEd L. Cashin 		if (n > ch->scnt)
108719bf2635SEd L. Cashin 			n = ch->scnt;
10884f51dc5eSEd L. Cashin 		n = n ? n * 512 : DEFAULTBCNT;
108968e0d42fSEd L. Cashin 		if (n != ifp->maxbcnt) {
1090a12c93f0SEd L. Cashin 			printk(KERN_INFO
1091411c41eeSHarvey Harrison 				"aoe: e%ld.%d: setting %d%s%s:%pm\n",
109268e0d42fSEd L. Cashin 				d->aoemajor, d->aoeminor, n,
109368e0d42fSEd L. Cashin 				" byte data frames on ", ifp->nd->name,
1094411c41eeSHarvey Harrison 				t->addr);
109568e0d42fSEd L. Cashin 			ifp->maxbcnt = n;
10964f51dc5eSEd L. Cashin 		}
109719bf2635SEd L. Cashin 	}
10983ae1c24eSEd L. Cashin 
10993ae1c24eSEd L. Cashin 	/* don't change users' perspective */
110068e0d42fSEd L. Cashin 	if (d->nopen) {
11011da177e4SLinus Torvalds 		spin_unlock_irqrestore(&d->lock, flags);
11021da177e4SLinus Torvalds 		return;
11031da177e4SLinus Torvalds 	}
110463e9cc5dSecashin@coraid.com 	d->fw_ver = be16_to_cpu(ch->fwver);
11051da177e4SLinus Torvalds 
110668e0d42fSEd L. Cashin 	sl = aoecmd_ata_id(d);
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 	spin_unlock_irqrestore(&d->lock, flags);
11091da177e4SLinus Torvalds 
1110e9bb8fb0SDavid S. Miller 	if (sl) {
1111e9bb8fb0SDavid S. Miller 		struct sk_buff_head queue;
1112e9bb8fb0SDavid S. Miller 		__skb_queue_head_init(&queue);
1113e9bb8fb0SDavid S. Miller 		__skb_queue_tail(&queue, sl);
1114e9bb8fb0SDavid S. Miller 		aoenet_xmit(&queue);
1115e9bb8fb0SDavid S. Miller 	}
11161da177e4SLinus Torvalds }
11171da177e4SLinus Torvalds 
111868e0d42fSEd L. Cashin void
111968e0d42fSEd L. Cashin aoecmd_cleanslate(struct aoedev *d)
112068e0d42fSEd L. Cashin {
112168e0d42fSEd L. Cashin 	struct aoetgt **t, **te;
112268e0d42fSEd L. Cashin 	struct aoeif *p, *e;
112368e0d42fSEd L. Cashin 
112468e0d42fSEd L. Cashin 	d->mintimer = MINTIMER;
112568e0d42fSEd L. Cashin 
112668e0d42fSEd L. Cashin 	t = d->targets;
112768e0d42fSEd L. Cashin 	te = t + NTARGETS;
112868e0d42fSEd L. Cashin 	for (; t < te && *t; t++) {
112968e0d42fSEd L. Cashin 		(*t)->maxout = (*t)->nframes;
113068e0d42fSEd L. Cashin 		p = (*t)->ifs;
113168e0d42fSEd L. Cashin 		e = p + NAOEIFS;
113268e0d42fSEd L. Cashin 		for (; p < e; p++) {
113368e0d42fSEd L. Cashin 			p->lostjumbo = 0;
113468e0d42fSEd L. Cashin 			p->lost = 0;
113568e0d42fSEd L. Cashin 			p->maxbcnt = DEFAULTBCNT;
113668e0d42fSEd L. Cashin 		}
113768e0d42fSEd L. Cashin 	}
113868e0d42fSEd L. Cashin }
1139