xref: /openbmc/linux/drivers/block/aoe/aoecmd.c (revision 3fc9b032489d365957ce531d32e225b2d0b3ed11)
1fea05a26SEd Cashin /* Copyright (c) 2012 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>
15896831f5SEd Cashin #include <linux/workqueue.h>
16896831f5SEd Cashin #include <linux/kthread.h>
17881d966bSEric W. Biederman #include <net/net_namespace.h>
18475172fbSEd L. Cashin #include <asm/unaligned.h>
19896831f5SEd Cashin #include <linux/uio.h>
201da177e4SLinus Torvalds #include "aoe.h"
211da177e4SLinus Torvalds 
22896831f5SEd Cashin #define MAXIOC (8192)	/* default meant to avoid most soft lockups */
23896831f5SEd Cashin 
24896831f5SEd Cashin static void ktcomplete(struct frame *, struct sk_buff *);
25896831f5SEd Cashin 
2669cf2d85SEd Cashin static struct buf *nextbuf(struct aoedev *);
2769cf2d85SEd Cashin 
28b751e8b6SEd L. Cashin static int aoe_deadsecs = 60 * 3;
29b751e8b6SEd L. Cashin module_param(aoe_deadsecs, int, 0644);
30b751e8b6SEd L. Cashin MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
311da177e4SLinus Torvalds 
327df620d8SEd L. Cashin static int aoe_maxout = 16;
337df620d8SEd L. Cashin module_param(aoe_maxout, int, 0644);
347df620d8SEd L. Cashin MODULE_PARM_DESC(aoe_maxout,
357df620d8SEd L. Cashin 	"Only aoe_maxout outstanding packets for every MAC on eX.Y.");
367df620d8SEd L. Cashin 
37896831f5SEd Cashin static wait_queue_head_t ktiowq;
38896831f5SEd Cashin static struct ktstate kts;
39896831f5SEd Cashin 
40896831f5SEd Cashin /* io completion queue */
41896831f5SEd Cashin static struct {
42896831f5SEd Cashin 	struct list_head head;
43896831f5SEd Cashin 	spinlock_t lock;
44896831f5SEd Cashin } iocq;
45896831f5SEd Cashin 
4668e0d42fSEd L. Cashin static struct sk_buff *
47e407a7f6SEd L. Cashin new_skb(ulong len)
481da177e4SLinus Torvalds {
491da177e4SLinus Torvalds 	struct sk_buff *skb;
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 	skb = alloc_skb(len, GFP_ATOMIC);
521da177e4SLinus Torvalds 	if (skb) {
53459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
54c1d2bbe1SArnaldo Carvalho de Melo 		skb_reset_network_header(skb);
551da177e4SLinus Torvalds 		skb->protocol = __constant_htons(ETH_P_AOE);
568babe8ccSEd Cashin 		skb_checksum_none_assert(skb);
571da177e4SLinus Torvalds 	}
581da177e4SLinus Torvalds 	return skb;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds static struct frame *
623a0c40d2SEd Cashin getframe_deferred(struct aoedev *d, u32 tag)
633a0c40d2SEd Cashin {
643a0c40d2SEd Cashin 	struct list_head *head, *pos, *nx;
653a0c40d2SEd Cashin 	struct frame *f;
663a0c40d2SEd Cashin 
673a0c40d2SEd Cashin 	head = &d->rexmitq;
683a0c40d2SEd Cashin 	list_for_each_safe(pos, nx, head) {
693a0c40d2SEd Cashin 		f = list_entry(pos, struct frame, head);
703a0c40d2SEd Cashin 		if (f->tag == tag) {
713a0c40d2SEd Cashin 			list_del(pos);
723a0c40d2SEd Cashin 			return f;
733a0c40d2SEd Cashin 		}
743a0c40d2SEd Cashin 	}
753a0c40d2SEd Cashin 	return NULL;
763a0c40d2SEd Cashin }
773a0c40d2SEd Cashin 
783a0c40d2SEd Cashin static struct frame *
7964a80f5aSEd Cashin getframe(struct aoedev *d, u32 tag)
801da177e4SLinus Torvalds {
81896831f5SEd Cashin 	struct frame *f;
82896831f5SEd Cashin 	struct list_head *head, *pos, *nx;
83896831f5SEd Cashin 	u32 n;
841da177e4SLinus Torvalds 
85896831f5SEd Cashin 	n = tag % NFACTIVE;
8664a80f5aSEd Cashin 	head = &d->factive[n];
87896831f5SEd Cashin 	list_for_each_safe(pos, nx, head) {
88896831f5SEd Cashin 		f = list_entry(pos, struct frame, head);
89896831f5SEd Cashin 		if (f->tag == tag) {
90896831f5SEd Cashin 			list_del(pos);
911da177e4SLinus Torvalds 			return f;
92896831f5SEd Cashin 		}
93896831f5SEd Cashin 	}
941da177e4SLinus Torvalds 	return NULL;
951da177e4SLinus Torvalds }
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds /*
981da177e4SLinus Torvalds  * Leave the top bit clear so we have tagspace for userland.
991da177e4SLinus Torvalds  * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
1001da177e4SLinus Torvalds  * This driver reserves tag -1 to mean "unused frame."
1011da177e4SLinus Torvalds  */
1021da177e4SLinus Torvalds static int
10364a80f5aSEd Cashin newtag(struct aoedev *d)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	register ulong n;
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	n = jiffies & 0xffff;
10864a80f5aSEd Cashin 	return n |= (++d->lasttag & 0x7fff) << 16;
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
111896831f5SEd Cashin static u32
11268e0d42fSEd L. Cashin aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
1131da177e4SLinus Torvalds {
11464a80f5aSEd Cashin 	u32 host_tag = newtag(d);
1151da177e4SLinus Torvalds 
11668e0d42fSEd L. Cashin 	memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
11768e0d42fSEd L. Cashin 	memcpy(h->dst, t->addr, sizeof h->dst);
11863e9cc5dSecashin@coraid.com 	h->type = __constant_cpu_to_be16(ETH_P_AOE);
1191da177e4SLinus Torvalds 	h->verfl = AOE_HVER;
12063e9cc5dSecashin@coraid.com 	h->major = cpu_to_be16(d->aoemajor);
1211da177e4SLinus Torvalds 	h->minor = d->aoeminor;
1221da177e4SLinus Torvalds 	h->cmd = AOECMD_ATA;
12363e9cc5dSecashin@coraid.com 	h->tag = cpu_to_be32(host_tag);
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	return host_tag;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
12819bf2635SEd L. Cashin static inline void
12919bf2635SEd L. Cashin put_lba(struct aoe_atahdr *ah, sector_t lba)
13019bf2635SEd L. Cashin {
13119bf2635SEd L. Cashin 	ah->lba0 = lba;
13219bf2635SEd L. Cashin 	ah->lba1 = lba >>= 8;
13319bf2635SEd L. Cashin 	ah->lba2 = lba >>= 8;
13419bf2635SEd L. Cashin 	ah->lba3 = lba >>= 8;
13519bf2635SEd L. Cashin 	ah->lba4 = lba >>= 8;
13619bf2635SEd L. Cashin 	ah->lba5 = lba >>= 8;
13719bf2635SEd L. Cashin }
13819bf2635SEd L. Cashin 
1393f0f0133SEd Cashin static struct aoeif *
14068e0d42fSEd L. Cashin ifrotate(struct aoetgt *t)
1411da177e4SLinus Torvalds {
1423f0f0133SEd Cashin 	struct aoeif *ifp;
1433f0f0133SEd Cashin 
1443f0f0133SEd Cashin 	ifp = t->ifp;
1453f0f0133SEd Cashin 	ifp++;
1463f0f0133SEd Cashin 	if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL)
1473f0f0133SEd Cashin 		ifp = t->ifs;
1483f0f0133SEd Cashin 	if (ifp->nd == NULL)
1493f0f0133SEd Cashin 		return NULL;
1503f0f0133SEd Cashin 	return t->ifp = ifp;
15168e0d42fSEd L. Cashin }
15268e0d42fSEd L. Cashin 
1539bb237b6SEd L. Cashin static void
1549bb237b6SEd L. Cashin skb_pool_put(struct aoedev *d, struct sk_buff *skb)
1559bb237b6SEd L. Cashin {
156e9bb8fb0SDavid S. Miller 	__skb_queue_tail(&d->skbpool, skb);
1579bb237b6SEd L. Cashin }
1589bb237b6SEd L. Cashin 
1599bb237b6SEd L. Cashin static struct sk_buff *
1609bb237b6SEd L. Cashin skb_pool_get(struct aoedev *d)
1619bb237b6SEd L. Cashin {
162e9bb8fb0SDavid S. Miller 	struct sk_buff *skb = skb_peek(&d->skbpool);
1639bb237b6SEd L. Cashin 
1649bb237b6SEd L. Cashin 	if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
165e9bb8fb0SDavid S. Miller 		__skb_unlink(skb, &d->skbpool);
1669bb237b6SEd L. Cashin 		return skb;
1679bb237b6SEd L. Cashin 	}
168e9bb8fb0SDavid S. Miller 	if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
169e9bb8fb0SDavid S. Miller 	    (skb = new_skb(ETH_ZLEN)))
1709bb237b6SEd L. Cashin 		return skb;
171e9bb8fb0SDavid S. Miller 
1729bb237b6SEd L. Cashin 	return NULL;
1739bb237b6SEd L. Cashin }
1749bb237b6SEd L. Cashin 
175896831f5SEd Cashin void
176896831f5SEd Cashin aoe_freetframe(struct frame *f)
17768e0d42fSEd L. Cashin {
178896831f5SEd Cashin 	struct aoetgt *t;
179896831f5SEd Cashin 
180896831f5SEd Cashin 	t = f->t;
181896831f5SEd Cashin 	f->buf = NULL;
182896831f5SEd Cashin 	f->bv = NULL;
183896831f5SEd Cashin 	f->r_skb = NULL;
184896831f5SEd Cashin 	list_add(&f->head, &t->ffree);
185896831f5SEd Cashin }
186896831f5SEd Cashin 
187896831f5SEd Cashin static struct frame *
188896831f5SEd Cashin newtframe(struct aoedev *d, struct aoetgt *t)
189896831f5SEd Cashin {
190896831f5SEd Cashin 	struct frame *f;
1919bb237b6SEd L. Cashin 	struct sk_buff *skb;
192896831f5SEd Cashin 	struct list_head *pos;
193896831f5SEd Cashin 
194896831f5SEd Cashin 	if (list_empty(&t->ffree)) {
195896831f5SEd Cashin 		if (t->falloc >= NSKBPOOLMAX*2)
196896831f5SEd Cashin 			return NULL;
197896831f5SEd Cashin 		f = kcalloc(1, sizeof(*f), GFP_ATOMIC);
198896831f5SEd Cashin 		if (f == NULL)
199896831f5SEd Cashin 			return NULL;
200896831f5SEd Cashin 		t->falloc++;
201896831f5SEd Cashin 		f->t = t;
202896831f5SEd Cashin 	} else {
203896831f5SEd Cashin 		pos = t->ffree.next;
204896831f5SEd Cashin 		list_del(pos);
205896831f5SEd Cashin 		f = list_entry(pos, struct frame, head);
206896831f5SEd Cashin 	}
207896831f5SEd Cashin 
208896831f5SEd Cashin 	skb = f->skb;
209896831f5SEd Cashin 	if (skb == NULL) {
210896831f5SEd Cashin 		f->skb = skb = new_skb(ETH_ZLEN);
211896831f5SEd Cashin 		if (!skb) {
212896831f5SEd Cashin bail:			aoe_freetframe(f);
213896831f5SEd Cashin 			return NULL;
214896831f5SEd Cashin 		}
215896831f5SEd Cashin 	}
216896831f5SEd Cashin 
217896831f5SEd Cashin 	if (atomic_read(&skb_shinfo(skb)->dataref) != 1) {
218896831f5SEd Cashin 		skb = skb_pool_get(d);
219896831f5SEd Cashin 		if (skb == NULL)
220896831f5SEd Cashin 			goto bail;
221896831f5SEd Cashin 		skb_pool_put(d, f->skb);
222896831f5SEd Cashin 		f->skb = skb;
223896831f5SEd Cashin 	}
224896831f5SEd Cashin 
225896831f5SEd Cashin 	skb->truesize -= skb->data_len;
226896831f5SEd Cashin 	skb_shinfo(skb)->nr_frags = skb->data_len = 0;
227896831f5SEd Cashin 	skb_trim(skb, 0);
228896831f5SEd Cashin 	return f;
229896831f5SEd Cashin }
230896831f5SEd Cashin 
231896831f5SEd Cashin static struct frame *
232896831f5SEd Cashin newframe(struct aoedev *d)
233896831f5SEd Cashin {
234896831f5SEd Cashin 	struct frame *f;
235896831f5SEd Cashin 	struct aoetgt *t, **tt;
236896831f5SEd Cashin 	int totout = 0;
23768e0d42fSEd L. Cashin 
23868e0d42fSEd L. Cashin 	if (d->targets[0] == NULL) {	/* shouldn't happen, but I'm paranoid */
23968e0d42fSEd L. Cashin 		printk(KERN_ERR "aoe: NULL TARGETS!\n");
24068e0d42fSEd L. Cashin 		return NULL;
24168e0d42fSEd L. Cashin 	}
242896831f5SEd Cashin 	tt = d->tgt;	/* last used target */
2439bb237b6SEd L. Cashin 	for (;;) {
244896831f5SEd Cashin 		tt++;
245896831f5SEd Cashin 		if (tt >= &d->targets[NTARGETS] || !*tt)
246896831f5SEd Cashin 			tt = d->targets;
247896831f5SEd Cashin 		t = *tt;
248896831f5SEd Cashin 		totout += t->nout;
249896831f5SEd Cashin 		if (t->nout < t->maxout
2509bb237b6SEd L. Cashin 		&& t != d->htgt
251896831f5SEd Cashin 		&& t->ifp->nd) {
252896831f5SEd Cashin 			f = newtframe(d, t);
253896831f5SEd Cashin 			if (f) {
254896831f5SEd Cashin 				ifrotate(t);
2553f0f0133SEd Cashin 				d->tgt = tt;
25668e0d42fSEd L. Cashin 				return f;
25768e0d42fSEd L. Cashin 			}
2589bb237b6SEd L. Cashin 		}
259896831f5SEd Cashin 		if (tt == d->tgt)	/* we've looped and found nada */
2609bb237b6SEd L. Cashin 			break;
261896831f5SEd Cashin 	}
262896831f5SEd Cashin 	if (totout == 0) {
263896831f5SEd Cashin 		d->kicked++;
264896831f5SEd Cashin 		d->flags |= DEVFL_KICKME;
2659bb237b6SEd L. Cashin 	}
26668e0d42fSEd L. Cashin 	return NULL;
26768e0d42fSEd L. Cashin }
26868e0d42fSEd L. Cashin 
2693d5b0605SEd Cashin static void
2703d5b0605SEd Cashin skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt)
2713d5b0605SEd Cashin {
2723d5b0605SEd Cashin 	int frag = 0;
2733d5b0605SEd Cashin 	ulong fcnt;
2743d5b0605SEd Cashin loop:
2753d5b0605SEd Cashin 	fcnt = bv->bv_len - (off - bv->bv_offset);
2763d5b0605SEd Cashin 	if (fcnt > cnt)
2773d5b0605SEd Cashin 		fcnt = cnt;
2783d5b0605SEd Cashin 	skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt);
2793d5b0605SEd Cashin 	cnt -= fcnt;
2803d5b0605SEd Cashin 	if (cnt <= 0)
2813d5b0605SEd Cashin 		return;
2823d5b0605SEd Cashin 	bv++;
2833d5b0605SEd Cashin 	off = bv->bv_offset;
2843d5b0605SEd Cashin 	goto loop;
2853d5b0605SEd Cashin }
2863d5b0605SEd Cashin 
287896831f5SEd Cashin static void
288896831f5SEd Cashin fhash(struct frame *f)
289896831f5SEd Cashin {
29064a80f5aSEd Cashin 	struct aoedev *d = f->t->d;
291896831f5SEd Cashin 	u32 n;
292896831f5SEd Cashin 
293896831f5SEd Cashin 	n = f->tag % NFACTIVE;
29464a80f5aSEd Cashin 	list_add_tail(&f->head, &d->factive[n]);
295896831f5SEd Cashin }
296896831f5SEd Cashin 
29768e0d42fSEd L. Cashin static int
29868e0d42fSEd L. Cashin aoecmd_ata_rw(struct aoedev *d)
29968e0d42fSEd L. Cashin {
30068e0d42fSEd L. Cashin 	struct frame *f;
3011da177e4SLinus Torvalds 	struct aoe_hdr *h;
3021da177e4SLinus Torvalds 	struct aoe_atahdr *ah;
3031da177e4SLinus Torvalds 	struct buf *buf;
30468e0d42fSEd L. Cashin 	struct aoetgt *t;
3051da177e4SLinus Torvalds 	struct sk_buff *skb;
30669cf2d85SEd Cashin 	struct sk_buff_head queue;
3073d5b0605SEd Cashin 	ulong bcnt, fbcnt;
3081da177e4SLinus Torvalds 	char writebit, extbit;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	writebit = 0x10;
3111da177e4SLinus Torvalds 	extbit = 0x4;
3121da177e4SLinus Torvalds 
31369cf2d85SEd Cashin 	buf = nextbuf(d);
31469cf2d85SEd Cashin 	if (buf == NULL)
31569cf2d85SEd Cashin 		return 0;
316896831f5SEd Cashin 	f = newframe(d);
31768e0d42fSEd L. Cashin 	if (f == NULL)
31868e0d42fSEd L. Cashin 		return 0;
31968e0d42fSEd L. Cashin 	t = *d->tgt;
3203f0f0133SEd Cashin 	bcnt = d->maxbcnt;
32168e0d42fSEd L. Cashin 	if (bcnt == 0)
32268e0d42fSEd L. Cashin 		bcnt = DEFAULTBCNT;
3233d5b0605SEd Cashin 	if (bcnt > buf->resid)
3243d5b0605SEd Cashin 		bcnt = buf->resid;
3253d5b0605SEd Cashin 	fbcnt = bcnt;
3263d5b0605SEd Cashin 	f->bv = buf->bv;
3273d5b0605SEd Cashin 	f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
3283d5b0605SEd Cashin 	do {
3293d5b0605SEd Cashin 		if (fbcnt < buf->bv_resid) {
3303d5b0605SEd Cashin 			buf->bv_resid -= fbcnt;
3313d5b0605SEd Cashin 			buf->resid -= fbcnt;
3323d5b0605SEd Cashin 			break;
3333d5b0605SEd Cashin 		}
3343d5b0605SEd Cashin 		fbcnt -= buf->bv_resid;
3353d5b0605SEd Cashin 		buf->resid -= buf->bv_resid;
3363d5b0605SEd Cashin 		if (buf->resid == 0) {
33769cf2d85SEd Cashin 			d->ip.buf = NULL;
3383d5b0605SEd Cashin 			break;
3393d5b0605SEd Cashin 		}
3403d5b0605SEd Cashin 		buf->bv++;
3413d5b0605SEd Cashin 		buf->bv_resid = buf->bv->bv_len;
3423d5b0605SEd Cashin 		WARN_ON(buf->bv_resid == 0);
3433d5b0605SEd Cashin 	} while (fbcnt);
3443d5b0605SEd Cashin 
3451da177e4SLinus Torvalds 	/* initialize the headers & frame */
346e407a7f6SEd L. Cashin 	skb = f->skb;
347abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
3481da177e4SLinus Torvalds 	ah = (struct aoe_atahdr *) (h+1);
34919900cdeSEd L. Cashin 	skb_put(skb, sizeof *h + sizeof *ah);
35019900cdeSEd L. Cashin 	memset(h, 0, skb->len);
35168e0d42fSEd L. Cashin 	f->tag = aoehdr_atainit(d, t, h);
352896831f5SEd Cashin 	fhash(f);
35368e0d42fSEd L. Cashin 	t->nout++;
3541da177e4SLinus Torvalds 	f->waited = 0;
355*3fc9b032SEd Cashin 	f->waited_total = 0;
3561da177e4SLinus Torvalds 	f->buf = buf;
35719bf2635SEd L. Cashin 	f->bcnt = bcnt;
35868e0d42fSEd L. Cashin 	f->lba = buf->sector;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	/* set up ata header */
3611da177e4SLinus Torvalds 	ah->scnt = bcnt >> 9;
36268e0d42fSEd L. Cashin 	put_lba(ah, buf->sector);
3631da177e4SLinus Torvalds 	if (d->flags & DEVFL_EXT) {
3641da177e4SLinus Torvalds 		ah->aflags |= AOEAFL_EXT;
3651da177e4SLinus Torvalds 	} else {
3661da177e4SLinus Torvalds 		extbit = 0;
3671da177e4SLinus Torvalds 		ah->lba3 &= 0x0f;
3681da177e4SLinus Torvalds 		ah->lba3 |= 0xe0;	/* LBA bit + obsolete 0xa0 */
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds 	if (bio_data_dir(buf->bio) == WRITE) {
3713d5b0605SEd Cashin 		skb_fillup(skb, f->bv, f->bv_off, bcnt);
3721da177e4SLinus Torvalds 		ah->aflags |= AOEAFL_WRITE;
3734f51dc5eSEd L. Cashin 		skb->len += bcnt;
3744f51dc5eSEd L. Cashin 		skb->data_len = bcnt;
3753d5b0605SEd Cashin 		skb->truesize += bcnt;
37668e0d42fSEd L. Cashin 		t->wpkts++;
3771da177e4SLinus Torvalds 	} else {
37868e0d42fSEd L. Cashin 		t->rpkts++;
3791da177e4SLinus Torvalds 		writebit = 0;
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 
38204b3ab52SBartlomiej Zolnierkiewicz 	ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	/* mark all tracking fields and load out */
3851da177e4SLinus Torvalds 	buf->nframesout += 1;
3861da177e4SLinus Torvalds 	buf->sector += bcnt >> 9;
3871da177e4SLinus Torvalds 
38868e0d42fSEd L. Cashin 	skb->dev = t->ifp->nd;
3894f51dc5eSEd L. Cashin 	skb = skb_clone(skb, GFP_ATOMIC);
39069cf2d85SEd Cashin 	if (skb) {
3915f0c9c48SEd Cashin 		do_gettimeofday(&f->sent);
3925f0c9c48SEd Cashin 		f->sent_jiffs = (u32) jiffies;
39369cf2d85SEd Cashin 		__skb_queue_head_init(&queue);
39469cf2d85SEd Cashin 		__skb_queue_tail(&queue, skb);
39569cf2d85SEd Cashin 		aoenet_xmit(&queue);
39669cf2d85SEd Cashin 	}
39768e0d42fSEd L. Cashin 	return 1;
39868e0d42fSEd L. Cashin }
3991da177e4SLinus Torvalds 
4003ae1c24eSEd L. Cashin /* some callers cannot sleep, and they can call this function,
4013ae1c24eSEd L. Cashin  * transmitting the packets later, when interrupts are on
4023ae1c24eSEd L. Cashin  */
403e9bb8fb0SDavid S. Miller static void
404e9bb8fb0SDavid S. Miller aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
4053ae1c24eSEd L. Cashin {
4063ae1c24eSEd L. Cashin 	struct aoe_hdr *h;
4073ae1c24eSEd L. Cashin 	struct aoe_cfghdr *ch;
408e9bb8fb0SDavid S. Miller 	struct sk_buff *skb;
4093ae1c24eSEd L. Cashin 	struct net_device *ifp;
4103ae1c24eSEd L. Cashin 
411840a185dSEric Dumazet 	rcu_read_lock();
412840a185dSEric Dumazet 	for_each_netdev_rcu(&init_net, ifp) {
4133ae1c24eSEd L. Cashin 		dev_hold(ifp);
4143ae1c24eSEd L. Cashin 		if (!is_aoe_netif(ifp))
4157562f876SPavel Emelianov 			goto cont;
4163ae1c24eSEd L. Cashin 
417e407a7f6SEd L. Cashin 		skb = new_skb(sizeof *h + sizeof *ch);
4183ae1c24eSEd L. Cashin 		if (skb == NULL) {
419a12c93f0SEd L. Cashin 			printk(KERN_INFO "aoe: skb alloc failure\n");
4207562f876SPavel Emelianov 			goto cont;
4213ae1c24eSEd L. Cashin 		}
42219900cdeSEd L. Cashin 		skb_put(skb, sizeof *h + sizeof *ch);
423e407a7f6SEd L. Cashin 		skb->dev = ifp;
424e9bb8fb0SDavid S. Miller 		__skb_queue_tail(queue, skb);
425abdbf94dSEd L. Cashin 		h = (struct aoe_hdr *) skb_mac_header(skb);
4263ae1c24eSEd L. Cashin 		memset(h, 0, sizeof *h + sizeof *ch);
4273ae1c24eSEd L. Cashin 
4283ae1c24eSEd L. Cashin 		memset(h->dst, 0xff, sizeof h->dst);
4293ae1c24eSEd L. Cashin 		memcpy(h->src, ifp->dev_addr, sizeof h->src);
4303ae1c24eSEd L. Cashin 		h->type = __constant_cpu_to_be16(ETH_P_AOE);
4313ae1c24eSEd L. Cashin 		h->verfl = AOE_HVER;
4323ae1c24eSEd L. Cashin 		h->major = cpu_to_be16(aoemajor);
4333ae1c24eSEd L. Cashin 		h->minor = aoeminor;
4343ae1c24eSEd L. Cashin 		h->cmd = AOECMD_CFG;
4353ae1c24eSEd L. Cashin 
4367562f876SPavel Emelianov cont:
4377562f876SPavel Emelianov 		dev_put(ifp);
4383ae1c24eSEd L. Cashin 	}
439840a185dSEric Dumazet 	rcu_read_unlock();
4403ae1c24eSEd L. Cashin }
4413ae1c24eSEd L. Cashin 
4421da177e4SLinus Torvalds static void
443896831f5SEd Cashin resend(struct aoedev *d, struct frame *f)
4441da177e4SLinus Torvalds {
4451da177e4SLinus Torvalds 	struct sk_buff *skb;
44669cf2d85SEd Cashin 	struct sk_buff_head queue;
4471da177e4SLinus Torvalds 	struct aoe_hdr *h;
44819bf2635SEd L. Cashin 	struct aoe_atahdr *ah;
449896831f5SEd Cashin 	struct aoetgt *t;
4501da177e4SLinus Torvalds 	char buf[128];
4511da177e4SLinus Torvalds 	u32 n;
4521da177e4SLinus Torvalds 
453896831f5SEd Cashin 	t = f->t;
45464a80f5aSEd Cashin 	n = newtag(d);
455e407a7f6SEd L. Cashin 	skb = f->skb;
4563f0f0133SEd Cashin 	if (ifrotate(t) == NULL) {
4573f0f0133SEd Cashin 		/* probably can't happen, but set it up to fail anyway */
4583f0f0133SEd Cashin 		pr_info("aoe: resend: no interfaces to rotate to.\n");
4593f0f0133SEd Cashin 		ktcomplete(f, NULL);
4603f0f0133SEd Cashin 		return;
4613f0f0133SEd Cashin 	}
462abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
46319bf2635SEd L. Cashin 	ah = (struct aoe_atahdr *) (h+1);
46468e0d42fSEd L. Cashin 
46568e0d42fSEd L. Cashin 	snprintf(buf, sizeof buf,
466411c41eeSHarvey Harrison 		"%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
46768e0d42fSEd L. Cashin 		"retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
468411c41eeSHarvey Harrison 		h->src, h->dst, t->nout);
46968e0d42fSEd L. Cashin 	aoechr_error(buf);
47068e0d42fSEd L. Cashin 
4711da177e4SLinus Torvalds 	f->tag = n;
472896831f5SEd Cashin 	fhash(f);
47363e9cc5dSecashin@coraid.com 	h->tag = cpu_to_be32(n);
47468e0d42fSEd L. Cashin 	memcpy(h->dst, t->addr, sizeof h->dst);
47568e0d42fSEd L. Cashin 	memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
4761da177e4SLinus Torvalds 
47768e0d42fSEd L. Cashin 	skb->dev = t->ifp->nd;
4784f51dc5eSEd L. Cashin 	skb = skb_clone(skb, GFP_ATOMIC);
4794f51dc5eSEd L. Cashin 	if (skb == NULL)
4804f51dc5eSEd L. Cashin 		return;
4815f0c9c48SEd Cashin 	do_gettimeofday(&f->sent);
4825f0c9c48SEd Cashin 	f->sent_jiffs = (u32) jiffies;
48369cf2d85SEd Cashin 	__skb_queue_head_init(&queue);
48469cf2d85SEd Cashin 	__skb_queue_tail(&queue, skb);
48569cf2d85SEd Cashin 	aoenet_xmit(&queue);
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds static int
4895f0c9c48SEd Cashin tsince_hr(struct frame *f)
4905f0c9c48SEd Cashin {
4915f0c9c48SEd Cashin 	struct timeval now;
4925f0c9c48SEd Cashin 	int n;
4935f0c9c48SEd Cashin 
4945f0c9c48SEd Cashin 	do_gettimeofday(&now);
4955f0c9c48SEd Cashin 	n = now.tv_usec - f->sent.tv_usec;
4965f0c9c48SEd Cashin 	n += (now.tv_sec - f->sent.tv_sec) * USEC_PER_SEC;
4975f0c9c48SEd Cashin 
4985f0c9c48SEd Cashin 	if (n < 0)
4995f0c9c48SEd Cashin 		n = -n;
5005f0c9c48SEd Cashin 
5015f0c9c48SEd Cashin 	/* For relatively long periods, use jiffies to avoid
5025f0c9c48SEd Cashin 	 * discrepancies caused by updates to the system time.
5035f0c9c48SEd Cashin 	 *
5045f0c9c48SEd Cashin 	 * On system with HZ of 1000, 32-bits is over 49 days
5055f0c9c48SEd Cashin 	 * worth of jiffies, or over 71 minutes worth of usecs.
5065f0c9c48SEd Cashin 	 *
5075f0c9c48SEd Cashin 	 * Jiffies overflow is handled by subtraction of unsigned ints:
5085f0c9c48SEd Cashin 	 * (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe
5095f0c9c48SEd Cashin 	 * $3 = 4
5105f0c9c48SEd Cashin 	 * (gdb)
5115f0c9c48SEd Cashin 	 */
5125f0c9c48SEd Cashin 	if (n > USEC_PER_SEC / 4) {
5135f0c9c48SEd Cashin 		n = ((u32) jiffies) - f->sent_jiffs;
5145f0c9c48SEd Cashin 		n *= USEC_PER_SEC / HZ;
5155f0c9c48SEd Cashin 	}
5165f0c9c48SEd Cashin 
5175f0c9c48SEd Cashin 	return n;
5185f0c9c48SEd Cashin }
5195f0c9c48SEd Cashin 
5205f0c9c48SEd Cashin static int
521896831f5SEd Cashin tsince(u32 tag)
5221da177e4SLinus Torvalds {
5231da177e4SLinus Torvalds 	int n;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	n = jiffies & 0xffff;
5261da177e4SLinus Torvalds 	n -= tag & 0xffff;
5271da177e4SLinus Torvalds 	if (n < 0)
5281da177e4SLinus Torvalds 		n += 1<<16;
5295f0c9c48SEd Cashin 	return jiffies_to_usecs(n + 1);
5301da177e4SLinus Torvalds }
5311da177e4SLinus Torvalds 
53268e0d42fSEd L. Cashin static struct aoeif *
53368e0d42fSEd L. Cashin getif(struct aoetgt *t, struct net_device *nd)
53468e0d42fSEd L. Cashin {
53568e0d42fSEd L. Cashin 	struct aoeif *p, *e;
53668e0d42fSEd L. Cashin 
53768e0d42fSEd L. Cashin 	p = t->ifs;
53868e0d42fSEd L. Cashin 	e = p + NAOEIFS;
53968e0d42fSEd L. Cashin 	for (; p < e; p++)
54068e0d42fSEd L. Cashin 		if (p->nd == nd)
54168e0d42fSEd L. Cashin 			return p;
54268e0d42fSEd L. Cashin 	return NULL;
54368e0d42fSEd L. Cashin }
54468e0d42fSEd L. Cashin 
54568e0d42fSEd L. Cashin static void
54668e0d42fSEd L. Cashin ejectif(struct aoetgt *t, struct aoeif *ifp)
54768e0d42fSEd L. Cashin {
54868e0d42fSEd L. Cashin 	struct aoeif *e;
5491b86fda9SEd Cashin 	struct net_device *nd;
55068e0d42fSEd L. Cashin 	ulong n;
55168e0d42fSEd L. Cashin 
5521b86fda9SEd Cashin 	nd = ifp->nd;
55368e0d42fSEd L. Cashin 	e = t->ifs + NAOEIFS - 1;
55468e0d42fSEd L. Cashin 	n = (e - ifp) * sizeof *ifp;
55568e0d42fSEd L. Cashin 	memmove(ifp, ifp+1, n);
55668e0d42fSEd L. Cashin 	e->nd = NULL;
5571b86fda9SEd Cashin 	dev_put(nd);
55868e0d42fSEd L. Cashin }
55968e0d42fSEd L. Cashin 
560*3fc9b032SEd Cashin static struct frame *
561*3fc9b032SEd Cashin reassign_frame(struct list_head *pos)
56268e0d42fSEd L. Cashin {
563*3fc9b032SEd Cashin 	struct frame *f;
564*3fc9b032SEd Cashin 	struct frame *nf;
56568e0d42fSEd L. Cashin 	struct sk_buff *skb;
56668e0d42fSEd L. Cashin 
567896831f5SEd Cashin 	f = list_entry(pos, struct frame, head);
568*3fc9b032SEd Cashin 	nf = newframe(f->t->d);
56968e0d42fSEd L. Cashin 	if (!nf)
570*3fc9b032SEd Cashin 		return NULL;
571896831f5SEd Cashin 
572896831f5SEd Cashin 	list_del(pos);
573896831f5SEd Cashin 
57468e0d42fSEd L. Cashin 	skb = nf->skb;
575896831f5SEd Cashin 	nf->skb = f->skb;
576896831f5SEd Cashin 	nf->buf = f->buf;
577896831f5SEd Cashin 	nf->bcnt = f->bcnt;
578896831f5SEd Cashin 	nf->lba = f->lba;
579896831f5SEd Cashin 	nf->bv = f->bv;
580896831f5SEd Cashin 	nf->bv_off = f->bv_off;
58168e0d42fSEd L. Cashin 	nf->waited = 0;
582*3fc9b032SEd Cashin 	nf->waited_total = f->waited_total;
583*3fc9b032SEd Cashin 	nf->sent = f->sent;
584896831f5SEd Cashin 	f->skb = skb;
585896831f5SEd Cashin 	aoe_freetframe(f);
586*3fc9b032SEd Cashin 	f->t->nout--;
587896831f5SEd Cashin 	nf->t->nout++;
588*3fc9b032SEd Cashin 
589*3fc9b032SEd Cashin 	return nf;
590*3fc9b032SEd Cashin }
591*3fc9b032SEd Cashin 
592*3fc9b032SEd Cashin static int
593*3fc9b032SEd Cashin sthtith(struct aoedev *d)
594*3fc9b032SEd Cashin {
595*3fc9b032SEd Cashin 	struct frame *f, *nf;
596*3fc9b032SEd Cashin 	struct list_head *nx, *pos, *head;
597*3fc9b032SEd Cashin 	struct aoetgt *ht = d->htgt;
598*3fc9b032SEd Cashin 	int i;
599*3fc9b032SEd Cashin 
600*3fc9b032SEd Cashin 	/* look through the active and pending retransmit frames */
601*3fc9b032SEd Cashin 	for (i = 0; i < NFACTIVE; i++) {
602*3fc9b032SEd Cashin 		head = &d->factive[i];
603*3fc9b032SEd Cashin 		list_for_each_safe(pos, nx, head) {
604*3fc9b032SEd Cashin 			f = list_entry(pos, struct frame, head);
605*3fc9b032SEd Cashin 			if (f->t != ht)
606*3fc9b032SEd Cashin 				continue;
607*3fc9b032SEd Cashin 			nf = reassign_frame(pos);
608*3fc9b032SEd Cashin 			if (!nf)
609*3fc9b032SEd Cashin 				return 0;
610896831f5SEd Cashin 			resend(d, nf);
611896831f5SEd Cashin 		}
61268e0d42fSEd L. Cashin 	}
613*3fc9b032SEd Cashin 	head = &d->rexmitq;
614*3fc9b032SEd Cashin 	list_for_each_safe(pos, nx, head) {
615*3fc9b032SEd Cashin 		f = list_entry(pos, struct frame, head);
616*3fc9b032SEd Cashin 		if (f->t != ht)
617*3fc9b032SEd Cashin 			continue;
618*3fc9b032SEd Cashin 		nf = reassign_frame(pos);
619*3fc9b032SEd Cashin 		if (!nf)
620*3fc9b032SEd Cashin 			return 0;
621*3fc9b032SEd Cashin 		resend(d, nf);
622*3fc9b032SEd Cashin 	}
6233f0f0133SEd Cashin 	/* We've cleaned up the outstanding so take away his
6243f0f0133SEd Cashin 	 * interfaces so he won't be used.  We should remove him from
6253f0f0133SEd Cashin 	 * the target array here, but cleaning up a target is
6263f0f0133SEd Cashin 	 * involved.  PUNT!
6273f0f0133SEd Cashin 	 */
62868e0d42fSEd L. Cashin 	memset(ht->ifs, 0, sizeof ht->ifs);
62968e0d42fSEd L. Cashin 	d->htgt = NULL;
63068e0d42fSEd L. Cashin 	return 1;
63168e0d42fSEd L. Cashin }
63268e0d42fSEd L. Cashin 
6331da177e4SLinus Torvalds static void
6343a0c40d2SEd Cashin rexmit_deferred(struct aoedev *d)
6353a0c40d2SEd Cashin {
6363a0c40d2SEd Cashin 	struct aoetgt *t;
6373a0c40d2SEd Cashin 	struct frame *f;
6383a0c40d2SEd Cashin 	struct list_head *pos, *nx, *head;
639*3fc9b032SEd Cashin 	int since;
6403a0c40d2SEd Cashin 
6413a0c40d2SEd Cashin 	head = &d->rexmitq;
6423a0c40d2SEd Cashin 	list_for_each_safe(pos, nx, head) {
6433a0c40d2SEd Cashin 		f = list_entry(pos, struct frame, head);
6443a0c40d2SEd Cashin 		t = f->t;
6453a0c40d2SEd Cashin 		if (t->nout >= t->maxout)
6463a0c40d2SEd Cashin 			continue;
6473a0c40d2SEd Cashin 		list_del(pos);
6483a0c40d2SEd Cashin 		t->nout++;
649*3fc9b032SEd Cashin 		since = tsince_hr(f);
650*3fc9b032SEd Cashin 		f->waited += since;
651*3fc9b032SEd Cashin 		f->waited_total += since;
6523a0c40d2SEd Cashin 		resend(d, f);
6533a0c40d2SEd Cashin 	}
6543a0c40d2SEd Cashin }
6553a0c40d2SEd Cashin 
6563a0c40d2SEd Cashin static void
6571da177e4SLinus Torvalds rexmit_timer(ulong vp)
6581da177e4SLinus Torvalds {
6591da177e4SLinus Torvalds 	struct aoedev *d;
6603a0c40d2SEd Cashin 	struct aoetgt *t;
66168e0d42fSEd L. Cashin 	struct aoeif *ifp;
662896831f5SEd Cashin 	struct frame *f;
663896831f5SEd Cashin 	struct list_head *head, *pos, *nx;
664896831f5SEd Cashin 	LIST_HEAD(flist);
6651da177e4SLinus Torvalds 	register long timeout;
6661da177e4SLinus Torvalds 	ulong flags, n;
667896831f5SEd Cashin 	int i;
668*3fc9b032SEd Cashin 	int since;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	d = (struct aoedev *) vp;
6711da177e4SLinus Torvalds 
6720d555ecfSEd Cashin 	spin_lock_irqsave(&d->lock, flags);
6730d555ecfSEd Cashin 
6743a0c40d2SEd Cashin 	/* timeout based on observed timings and variations */
6753a0c40d2SEd Cashin 	timeout = 2 * d->rttavg >> RTTSCALE;
6763a0c40d2SEd Cashin 	timeout += 8 * d->rttdev >> RTTDSCALE;
6773a0c40d2SEd Cashin 	if (timeout == 0)
6783a0c40d2SEd Cashin 		timeout = 1;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	if (d->flags & DEVFL_TKILL) {
6811c6f3fcaSEd L. Cashin 		spin_unlock_irqrestore(&d->lock, flags);
6821da177e4SLinus Torvalds 		return;
6831da177e4SLinus Torvalds 	}
684896831f5SEd Cashin 
685896831f5SEd Cashin 	/* collect all frames to rexmit into flist */
686896831f5SEd Cashin 	for (i = 0; i < NFACTIVE; i++) {
68764a80f5aSEd Cashin 		head = &d->factive[i];
688896831f5SEd Cashin 		list_for_each_safe(pos, nx, head) {
689896831f5SEd Cashin 			f = list_entry(pos, struct frame, head);
6905f0c9c48SEd Cashin 			if (tsince_hr(f) < timeout)
69164a80f5aSEd Cashin 				break;	/* end of expired frames */
692896831f5SEd Cashin 			/* move to flist for later processing */
693896831f5SEd Cashin 			list_move_tail(pos, &flist);
694896831f5SEd Cashin 		}
695896831f5SEd Cashin 	}
69669cf2d85SEd Cashin 
697896831f5SEd Cashin 	/* process expired frames */
698896831f5SEd Cashin 	while (!list_empty(&flist)) {
699896831f5SEd Cashin 		pos = flist.next;
700896831f5SEd Cashin 		f = list_entry(pos, struct frame, head);
701*3fc9b032SEd Cashin 		since = tsince_hr(f);
702*3fc9b032SEd Cashin 		n = f->waited_total + since;
7035f0c9c48SEd Cashin 		n /= USEC_PER_SEC;
70468e0d42fSEd L. Cashin 		if (n > aoe_deadsecs) {
705896831f5SEd Cashin 			/* Waited too long.  Device failure.
706896831f5SEd Cashin 			 * Hang all frames on first hash bucket for downdev
707896831f5SEd Cashin 			 * to clean up.
708896831f5SEd Cashin 			 */
70964a80f5aSEd Cashin 			list_splice(&flist, &d->factive[0]);
7101da177e4SLinus Torvalds 			aoedev_downdev(d);
7113a0c40d2SEd Cashin 			goto out;
7121da177e4SLinus Torvalds 		}
71368e0d42fSEd L. Cashin 
714896831f5SEd Cashin 		t = f->t;
715d54d35acSEd Cashin 		if (n > aoe_deadsecs/2)
716d54d35acSEd Cashin 			d->htgt = t; /* see if another target can help */
717d54d35acSEd Cashin 
7183a0c40d2SEd Cashin 		if (t->maxout != 1) {
7193a0c40d2SEd Cashin 			t->ssthresh = t->maxout / 2;
7203a0c40d2SEd Cashin 			t->maxout = 1;
72168e0d42fSEd L. Cashin 		}
72268e0d42fSEd L. Cashin 
72368e0d42fSEd L. Cashin 		ifp = getif(t, f->skb->dev);
72468e0d42fSEd L. Cashin 		if (ifp && ++ifp->lost > (t->nframes << 1)
72568e0d42fSEd L. Cashin 		&& (ifp != t->ifs || t->ifs[1].nd)) {
72668e0d42fSEd L. Cashin 			ejectif(t, ifp);
72768e0d42fSEd L. Cashin 			ifp = NULL;
72868e0d42fSEd L. Cashin 		}
7293a0c40d2SEd Cashin 		list_move_tail(pos, &d->rexmitq);
7303a0c40d2SEd Cashin 		t->nout--;
7311da177e4SLinus Torvalds 	}
7323a0c40d2SEd Cashin 	rexmit_deferred(d);
73368e0d42fSEd L. Cashin 
7343a0c40d2SEd Cashin out:
73569cf2d85SEd Cashin 	if ((d->flags & DEVFL_KICKME || d->htgt) && d->blkq) {
7364f51dc5eSEd L. Cashin 		d->flags &= ~DEVFL_KICKME;
73769cf2d85SEd Cashin 		d->blkq->request_fn(d->blkq);
7384f51dc5eSEd L. Cashin 	}
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	d->timer.expires = jiffies + TIMERTICK;
7411da177e4SLinus Torvalds 	add_timer(&d->timer);
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 	spin_unlock_irqrestore(&d->lock, flags);
74469cf2d85SEd Cashin }
7451da177e4SLinus Torvalds 
74669cf2d85SEd Cashin static unsigned long
74769cf2d85SEd Cashin rqbiocnt(struct request *r)
74869cf2d85SEd Cashin {
74969cf2d85SEd Cashin 	struct bio *bio;
75069cf2d85SEd Cashin 	unsigned long n = 0;
75169cf2d85SEd Cashin 
75269cf2d85SEd Cashin 	__rq_for_each_bio(bio, r)
75369cf2d85SEd Cashin 		n++;
75469cf2d85SEd Cashin 	return n;
75569cf2d85SEd Cashin }
75669cf2d85SEd Cashin 
75769cf2d85SEd Cashin /* This can be removed if we are certain that no users of the block
75869cf2d85SEd Cashin  * layer will ever use zero-count pages in bios.  Otherwise we have to
75969cf2d85SEd Cashin  * protect against the put_page sometimes done by the network layer.
76069cf2d85SEd Cashin  *
76169cf2d85SEd Cashin  * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
76269cf2d85SEd Cashin  * discussion.
76369cf2d85SEd Cashin  *
76469cf2d85SEd Cashin  * We cannot use get_page in the workaround, because it insists on a
76569cf2d85SEd Cashin  * positive page count as a precondition.  So we use _count directly.
76669cf2d85SEd Cashin  */
76769cf2d85SEd Cashin static void
76869cf2d85SEd Cashin bio_pageinc(struct bio *bio)
76969cf2d85SEd Cashin {
77069cf2d85SEd Cashin 	struct bio_vec *bv;
77169cf2d85SEd Cashin 	struct page *page;
77269cf2d85SEd Cashin 	int i;
77369cf2d85SEd Cashin 
77469cf2d85SEd Cashin 	bio_for_each_segment(bv, bio, i) {
77569cf2d85SEd Cashin 		page = bv->bv_page;
77669cf2d85SEd Cashin 		/* Non-zero page count for non-head members of
77769cf2d85SEd Cashin 		 * compound pages is no longer allowed by the kernel,
77869cf2d85SEd Cashin 		 * but this has never been seen here.
77969cf2d85SEd Cashin 		 */
78069cf2d85SEd Cashin 		if (unlikely(PageCompound(page)))
78169cf2d85SEd Cashin 			if (compound_trans_head(page) != page) {
78269cf2d85SEd Cashin 				pr_crit("page tail used for block I/O\n");
78369cf2d85SEd Cashin 				BUG();
78469cf2d85SEd Cashin 			}
78569cf2d85SEd Cashin 		atomic_inc(&page->_count);
78669cf2d85SEd Cashin 	}
78769cf2d85SEd Cashin }
78869cf2d85SEd Cashin 
78969cf2d85SEd Cashin static void
79069cf2d85SEd Cashin bio_pagedec(struct bio *bio)
79169cf2d85SEd Cashin {
79269cf2d85SEd Cashin 	struct bio_vec *bv;
79369cf2d85SEd Cashin 	int i;
79469cf2d85SEd Cashin 
79569cf2d85SEd Cashin 	bio_for_each_segment(bv, bio, i)
79669cf2d85SEd Cashin 		atomic_dec(&bv->bv_page->_count);
79769cf2d85SEd Cashin }
79869cf2d85SEd Cashin 
79969cf2d85SEd Cashin static void
80069cf2d85SEd Cashin bufinit(struct buf *buf, struct request *rq, struct bio *bio)
80169cf2d85SEd Cashin {
80269cf2d85SEd Cashin 	struct bio_vec *bv;
80369cf2d85SEd Cashin 
80469cf2d85SEd Cashin 	memset(buf, 0, sizeof(*buf));
80569cf2d85SEd Cashin 	buf->rq = rq;
80669cf2d85SEd Cashin 	buf->bio = bio;
80769cf2d85SEd Cashin 	buf->resid = bio->bi_size;
80869cf2d85SEd Cashin 	buf->sector = bio->bi_sector;
80969cf2d85SEd Cashin 	bio_pageinc(bio);
81069cf2d85SEd Cashin 	buf->bv = bv = &bio->bi_io_vec[bio->bi_idx];
81169cf2d85SEd Cashin 	buf->bv_resid = bv->bv_len;
81269cf2d85SEd Cashin 	WARN_ON(buf->bv_resid == 0);
81369cf2d85SEd Cashin }
81469cf2d85SEd Cashin 
81569cf2d85SEd Cashin static struct buf *
81669cf2d85SEd Cashin nextbuf(struct aoedev *d)
81769cf2d85SEd Cashin {
81869cf2d85SEd Cashin 	struct request *rq;
81969cf2d85SEd Cashin 	struct request_queue *q;
82069cf2d85SEd Cashin 	struct buf *buf;
82169cf2d85SEd Cashin 	struct bio *bio;
82269cf2d85SEd Cashin 
82369cf2d85SEd Cashin 	q = d->blkq;
82469cf2d85SEd Cashin 	if (q == NULL)
82569cf2d85SEd Cashin 		return NULL;	/* initializing */
82669cf2d85SEd Cashin 	if (d->ip.buf)
82769cf2d85SEd Cashin 		return d->ip.buf;
82869cf2d85SEd Cashin 	rq = d->ip.rq;
82969cf2d85SEd Cashin 	if (rq == NULL) {
83069cf2d85SEd Cashin 		rq = blk_peek_request(q);
83169cf2d85SEd Cashin 		if (rq == NULL)
83269cf2d85SEd Cashin 			return NULL;
83369cf2d85SEd Cashin 		blk_start_request(rq);
83469cf2d85SEd Cashin 		d->ip.rq = rq;
83569cf2d85SEd Cashin 		d->ip.nxbio = rq->bio;
83669cf2d85SEd Cashin 		rq->special = (void *) rqbiocnt(rq);
83769cf2d85SEd Cashin 	}
83869cf2d85SEd Cashin 	buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
83969cf2d85SEd Cashin 	if (buf == NULL) {
84069cf2d85SEd Cashin 		pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
84169cf2d85SEd Cashin 		return NULL;
84269cf2d85SEd Cashin 	}
84369cf2d85SEd Cashin 	bio = d->ip.nxbio;
84469cf2d85SEd Cashin 	bufinit(buf, rq, bio);
84569cf2d85SEd Cashin 	bio = bio->bi_next;
84669cf2d85SEd Cashin 	d->ip.nxbio = bio;
84769cf2d85SEd Cashin 	if (bio == NULL)
84869cf2d85SEd Cashin 		d->ip.rq = NULL;
84969cf2d85SEd Cashin 	return d->ip.buf = buf;
8501da177e4SLinus Torvalds }
8511da177e4SLinus Torvalds 
85268e0d42fSEd L. Cashin /* enters with d->lock held */
85368e0d42fSEd L. Cashin void
85468e0d42fSEd L. Cashin aoecmd_work(struct aoedev *d)
85568e0d42fSEd L. Cashin {
85668e0d42fSEd L. Cashin 	if (d->htgt && !sthtith(d))
85768e0d42fSEd L. Cashin 		return;
8583a0c40d2SEd Cashin 	rexmit_deferred(d);
85969cf2d85SEd Cashin 	while (aoecmd_ata_rw(d))
86069cf2d85SEd Cashin 		;
86168e0d42fSEd L. Cashin }
86268e0d42fSEd L. Cashin 
8633ae1c24eSEd L. Cashin /* this function performs work that has been deferred until sleeping is OK
8643ae1c24eSEd L. Cashin  */
8653ae1c24eSEd L. Cashin void
866c4028958SDavid Howells aoecmd_sleepwork(struct work_struct *work)
8673ae1c24eSEd L. Cashin {
868c4028958SDavid Howells 	struct aoedev *d = container_of(work, struct aoedev, work);
869b21faa25SEd Cashin 	struct block_device *bd;
870b21faa25SEd Cashin 	u64 ssize;
8713ae1c24eSEd L. Cashin 
8723ae1c24eSEd L. Cashin 	if (d->flags & DEVFL_GDALLOC)
8733ae1c24eSEd L. Cashin 		aoeblk_gdalloc(d);
8743ae1c24eSEd L. Cashin 
8753ae1c24eSEd L. Cashin 	if (d->flags & DEVFL_NEWSIZE) {
87680795aefSTejun Heo 		ssize = get_capacity(d->gd);
8773ae1c24eSEd L. Cashin 		bd = bdget_disk(d->gd, 0);
8783ae1c24eSEd L. Cashin 		if (bd) {
8793ae1c24eSEd L. Cashin 			mutex_lock(&bd->bd_inode->i_mutex);
8803ae1c24eSEd L. Cashin 			i_size_write(bd->bd_inode, (loff_t)ssize<<9);
8813ae1c24eSEd L. Cashin 			mutex_unlock(&bd->bd_inode->i_mutex);
8823ae1c24eSEd L. Cashin 			bdput(bd);
8833ae1c24eSEd L. Cashin 		}
884b21faa25SEd Cashin 		spin_lock_irq(&d->lock);
8853ae1c24eSEd L. Cashin 		d->flags |= DEVFL_UP;
8863ae1c24eSEd L. Cashin 		d->flags &= ~DEVFL_NEWSIZE;
887b21faa25SEd Cashin 		spin_unlock_irq(&d->lock);
8883ae1c24eSEd L. Cashin 	}
8893ae1c24eSEd L. Cashin }
8903ae1c24eSEd L. Cashin 
8911da177e4SLinus Torvalds static void
892667be1e7SEd Cashin ata_ident_fixstring(u16 *id, int ns)
893667be1e7SEd Cashin {
894667be1e7SEd Cashin 	u16 s;
895667be1e7SEd Cashin 
896667be1e7SEd Cashin 	while (ns-- > 0) {
897667be1e7SEd Cashin 		s = *id;
898667be1e7SEd Cashin 		*id++ = s >> 8 | s << 8;
899667be1e7SEd Cashin 	}
900667be1e7SEd Cashin }
901667be1e7SEd Cashin 
902667be1e7SEd Cashin static void
90368e0d42fSEd L. Cashin ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
9041da177e4SLinus Torvalds {
9051da177e4SLinus Torvalds 	u64 ssize;
9061da177e4SLinus Torvalds 	u16 n;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	/* word 83: command set supported */
909f885f8d1SHarvey Harrison 	n = get_unaligned_le16(&id[83 << 1]);
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	/* word 86: command set/feature enabled */
912f885f8d1SHarvey Harrison 	n |= get_unaligned_le16(&id[86 << 1]);
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	if (n & (1<<10)) {	/* bit 10: LBA 48 */
9151da177e4SLinus Torvalds 		d->flags |= DEVFL_EXT;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 		/* word 100: number lba48 sectors */
918f885f8d1SHarvey Harrison 		ssize = get_unaligned_le64(&id[100 << 1]);
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 		/* set as in ide-disk.c:init_idedisk_capacity */
9211da177e4SLinus Torvalds 		d->geo.cylinders = ssize;
9221da177e4SLinus Torvalds 		d->geo.cylinders /= (255 * 63);
9231da177e4SLinus Torvalds 		d->geo.heads = 255;
9241da177e4SLinus Torvalds 		d->geo.sectors = 63;
9251da177e4SLinus Torvalds 	} else {
9261da177e4SLinus Torvalds 		d->flags &= ~DEVFL_EXT;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 		/* number lba28 sectors */
929f885f8d1SHarvey Harrison 		ssize = get_unaligned_le32(&id[60 << 1]);
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 		/* NOTE: obsolete in ATA 6 */
932f885f8d1SHarvey Harrison 		d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
933f885f8d1SHarvey Harrison 		d->geo.heads = get_unaligned_le16(&id[55 << 1]);
934f885f8d1SHarvey Harrison 		d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
9351da177e4SLinus Torvalds 	}
9363ae1c24eSEd L. Cashin 
937667be1e7SEd Cashin 	ata_ident_fixstring((u16 *) &id[10<<1], 10);	/* serial */
938667be1e7SEd Cashin 	ata_ident_fixstring((u16 *) &id[23<<1], 4);	/* firmware */
939667be1e7SEd Cashin 	ata_ident_fixstring((u16 *) &id[27<<1], 20);	/* model */
940667be1e7SEd Cashin 	memcpy(d->ident, id, sizeof(d->ident));
941667be1e7SEd Cashin 
9423ae1c24eSEd L. Cashin 	if (d->ssize != ssize)
9431d75981aSEd L. Cashin 		printk(KERN_INFO
944411c41eeSHarvey Harrison 			"aoe: %pm e%ld.%d v%04x has %llu sectors\n",
945411c41eeSHarvey Harrison 			t->addr,
9463ae1c24eSEd L. Cashin 			d->aoemajor, d->aoeminor,
9473ae1c24eSEd L. Cashin 			d->fw_ver, (long long)ssize);
9481da177e4SLinus Torvalds 	d->ssize = ssize;
9491da177e4SLinus Torvalds 	d->geo.start = 0;
9506b9699bbSEd L. Cashin 	if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
9516b9699bbSEd L. Cashin 		return;
9521da177e4SLinus Torvalds 	if (d->gd != NULL) {
95380795aefSTejun Heo 		set_capacity(d->gd, ssize);
9543ae1c24eSEd L. Cashin 		d->flags |= DEVFL_NEWSIZE;
95568e0d42fSEd L. Cashin 	} else
9563ae1c24eSEd L. Cashin 		d->flags |= DEVFL_GDALLOC;
9571da177e4SLinus Torvalds 	schedule_work(&d->work);
9581da177e4SLinus Torvalds }
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds static void
9613a0c40d2SEd Cashin calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt)
9621da177e4SLinus Torvalds {
9631da177e4SLinus Torvalds 	register long n;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	n = rtt;
9661da177e4SLinus Torvalds 
9673a0c40d2SEd Cashin 	/* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */
9683a0c40d2SEd Cashin 	n -= d->rttavg >> RTTSCALE;
9693a0c40d2SEd Cashin 	d->rttavg += n;
9703a0c40d2SEd Cashin 	if (n < 0)
9713a0c40d2SEd Cashin 		n = -n;
9723a0c40d2SEd Cashin 	n -= d->rttdev >> RTTDSCALE;
9733a0c40d2SEd Cashin 	d->rttdev += n;
9743a0c40d2SEd Cashin 
9753a0c40d2SEd Cashin 	if (!t || t->maxout >= t->nframes)
9763a0c40d2SEd Cashin 		return;
9773a0c40d2SEd Cashin 	if (t->maxout < t->ssthresh)
9783a0c40d2SEd Cashin 		t->maxout += 1;
9793a0c40d2SEd Cashin 	else if (t->nout == t->maxout && t->next_cwnd-- == 0) {
9803a0c40d2SEd Cashin 		t->maxout += 1;
9813a0c40d2SEd Cashin 		t->next_cwnd = t->maxout;
9823a0c40d2SEd Cashin 	}
9831da177e4SLinus Torvalds }
9841da177e4SLinus Torvalds 
98568e0d42fSEd L. Cashin static struct aoetgt *
98668e0d42fSEd L. Cashin gettgt(struct aoedev *d, char *addr)
98768e0d42fSEd L. Cashin {
98868e0d42fSEd L. Cashin 	struct aoetgt **t, **e;
98968e0d42fSEd L. Cashin 
99068e0d42fSEd L. Cashin 	t = d->targets;
99168e0d42fSEd L. Cashin 	e = t + NTARGETS;
99268e0d42fSEd L. Cashin 	for (; t < e && *t; t++)
99368e0d42fSEd L. Cashin 		if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
99468e0d42fSEd L. Cashin 			return *t;
99568e0d42fSEd L. Cashin 	return NULL;
99668e0d42fSEd L. Cashin }
99768e0d42fSEd L. Cashin 
9983d5b0605SEd Cashin static void
999896831f5SEd Cashin bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
10003d5b0605SEd Cashin {
10013d5b0605SEd Cashin 	ulong fcnt;
10023d5b0605SEd Cashin 	char *p;
10033d5b0605SEd Cashin 	int soff = 0;
10043d5b0605SEd Cashin loop:
10053d5b0605SEd Cashin 	fcnt = bv->bv_len - (off - bv->bv_offset);
10063d5b0605SEd Cashin 	if (fcnt > cnt)
10073d5b0605SEd Cashin 		fcnt = cnt;
10083d5b0605SEd Cashin 	p = page_address(bv->bv_page) + off;
10093d5b0605SEd Cashin 	skb_copy_bits(skb, soff, p, fcnt);
10103d5b0605SEd Cashin 	soff += fcnt;
10113d5b0605SEd Cashin 	cnt -= fcnt;
10123d5b0605SEd Cashin 	if (cnt <= 0)
10133d5b0605SEd Cashin 		return;
10143d5b0605SEd Cashin 	bv++;
10153d5b0605SEd Cashin 	off = bv->bv_offset;
10163d5b0605SEd Cashin 	goto loop;
10173d5b0605SEd Cashin }
10183d5b0605SEd Cashin 
101969cf2d85SEd Cashin void
102069cf2d85SEd Cashin aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
102169cf2d85SEd Cashin {
102269cf2d85SEd Cashin 	struct bio *bio;
102369cf2d85SEd Cashin 	int bok;
102469cf2d85SEd Cashin 	struct request_queue *q;
102569cf2d85SEd Cashin 
102669cf2d85SEd Cashin 	q = d->blkq;
102769cf2d85SEd Cashin 	if (rq == d->ip.rq)
102869cf2d85SEd Cashin 		d->ip.rq = NULL;
102969cf2d85SEd Cashin 	do {
103069cf2d85SEd Cashin 		bio = rq->bio;
103169cf2d85SEd Cashin 		bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
103269cf2d85SEd Cashin 	} while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
103369cf2d85SEd Cashin 
103469cf2d85SEd Cashin 	/* cf. http://lkml.org/lkml/2006/10/31/28 */
103569cf2d85SEd Cashin 	if (!fastfail)
103611cfb6ffSEd Cashin 		__blk_run_queue(q);
103769cf2d85SEd Cashin }
103869cf2d85SEd Cashin 
103969cf2d85SEd Cashin static void
104069cf2d85SEd Cashin aoe_end_buf(struct aoedev *d, struct buf *buf)
104169cf2d85SEd Cashin {
104269cf2d85SEd Cashin 	struct request *rq;
104369cf2d85SEd Cashin 	unsigned long n;
104469cf2d85SEd Cashin 
104569cf2d85SEd Cashin 	if (buf == d->ip.buf)
104669cf2d85SEd Cashin 		d->ip.buf = NULL;
104769cf2d85SEd Cashin 	rq = buf->rq;
104869cf2d85SEd Cashin 	bio_pagedec(buf->bio);
104969cf2d85SEd Cashin 	mempool_free(buf, d->bufpool);
105069cf2d85SEd Cashin 	n = (unsigned long) rq->special;
105169cf2d85SEd Cashin 	rq->special = (void *) --n;
105269cf2d85SEd Cashin 	if (n == 0)
105369cf2d85SEd Cashin 		aoe_end_request(d, rq, 0);
105469cf2d85SEd Cashin }
105569cf2d85SEd Cashin 
10563d5b0605SEd Cashin static void
1057896831f5SEd Cashin ktiocomplete(struct frame *f)
10583d5b0605SEd Cashin {
1059ddec63e8SEd L. Cashin 	struct aoe_hdr *hin, *hout;
10601da177e4SLinus Torvalds 	struct aoe_atahdr *ahin, *ahout;
10611da177e4SLinus Torvalds 	struct buf *buf;
1062896831f5SEd Cashin 	struct sk_buff *skb;
106368e0d42fSEd L. Cashin 	struct aoetgt *t;
106468e0d42fSEd L. Cashin 	struct aoeif *ifp;
1065896831f5SEd Cashin 	struct aoedev *d;
1066896831f5SEd Cashin 	long n;
1067896831f5SEd Cashin 
1068896831f5SEd Cashin 	if (f == NULL)
1069896831f5SEd Cashin 		return;
1070896831f5SEd Cashin 
1071896831f5SEd Cashin 	t = f->t;
1072896831f5SEd Cashin 	d = t->d;
1073896831f5SEd Cashin 
1074896831f5SEd Cashin 	hout = (struct aoe_hdr *) skb_mac_header(f->skb);
1075896831f5SEd Cashin 	ahout = (struct aoe_atahdr *) (hout+1);
1076896831f5SEd Cashin 	buf = f->buf;
1077896831f5SEd Cashin 	skb = f->r_skb;
1078896831f5SEd Cashin 	if (skb == NULL)
1079896831f5SEd Cashin 		goto noskb;	/* just fail the buf. */
1080896831f5SEd Cashin 
1081896831f5SEd Cashin 	hin = (struct aoe_hdr *) skb->data;
1082896831f5SEd Cashin 	skb_pull(skb, sizeof(*hin));
1083896831f5SEd Cashin 	ahin = (struct aoe_atahdr *) skb->data;
1084896831f5SEd Cashin 	skb_pull(skb, sizeof(*ahin));
1085896831f5SEd Cashin 	if (ahin->cmdstat & 0xa9) {	/* these bits cleared on success */
1086896831f5SEd Cashin 		pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
1087896831f5SEd Cashin 			ahout->cmdstat, ahin->cmdstat,
1088896831f5SEd Cashin 			d->aoemajor, d->aoeminor);
1089896831f5SEd Cashin noskb:		if (buf)
109069cf2d85SEd Cashin 			clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1091896831f5SEd Cashin 		goto badrsp;
1092896831f5SEd Cashin 	}
1093896831f5SEd Cashin 
1094896831f5SEd Cashin 	n = ahout->scnt << 9;
1095896831f5SEd Cashin 	switch (ahout->cmdstat) {
1096896831f5SEd Cashin 	case ATA_CMD_PIO_READ:
1097896831f5SEd Cashin 	case ATA_CMD_PIO_READ_EXT:
1098896831f5SEd Cashin 		if (skb->len < n) {
1099896831f5SEd Cashin 			pr_err("aoe: runt data size in read.  skb->len=%d need=%ld\n",
1100896831f5SEd Cashin 				skb->len, n);
110169cf2d85SEd Cashin 			clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1102896831f5SEd Cashin 			break;
1103896831f5SEd Cashin 		}
1104896831f5SEd Cashin 		bvcpy(f->bv, f->bv_off, skb, n);
1105896831f5SEd Cashin 	case ATA_CMD_PIO_WRITE:
1106896831f5SEd Cashin 	case ATA_CMD_PIO_WRITE_EXT:
1107896831f5SEd Cashin 		spin_lock_irq(&d->lock);
1108896831f5SEd Cashin 		ifp = getif(t, skb->dev);
11093f0f0133SEd Cashin 		if (ifp)
1110896831f5SEd Cashin 			ifp->lost = 0;
1111896831f5SEd Cashin 		if (d->htgt == t) /* I'll help myself, thank you. */
1112896831f5SEd Cashin 			d->htgt = NULL;
1113896831f5SEd Cashin 		spin_unlock_irq(&d->lock);
1114896831f5SEd Cashin 		break;
1115896831f5SEd Cashin 	case ATA_CMD_ID_ATA:
1116896831f5SEd Cashin 		if (skb->len < 512) {
1117896831f5SEd Cashin 			pr_info("aoe: runt data size in ataid.  skb->len=%d\n",
1118896831f5SEd Cashin 				skb->len);
1119896831f5SEd Cashin 			break;
1120896831f5SEd Cashin 		}
1121896831f5SEd Cashin 		if (skb_linearize(skb))
1122896831f5SEd Cashin 			break;
1123896831f5SEd Cashin 		spin_lock_irq(&d->lock);
1124896831f5SEd Cashin 		ataid_complete(d, t, skb->data);
1125896831f5SEd Cashin 		spin_unlock_irq(&d->lock);
1126896831f5SEd Cashin 		break;
1127896831f5SEd Cashin 	default:
1128896831f5SEd Cashin 		pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
1129896831f5SEd Cashin 			ahout->cmdstat,
1130896831f5SEd Cashin 			be16_to_cpu(get_unaligned(&hin->major)),
1131896831f5SEd Cashin 			hin->minor);
1132896831f5SEd Cashin 	}
1133896831f5SEd Cashin badrsp:
1134896831f5SEd Cashin 	spin_lock_irq(&d->lock);
1135896831f5SEd Cashin 
1136896831f5SEd Cashin 	aoe_freetframe(f);
1137896831f5SEd Cashin 
113869cf2d85SEd Cashin 	if (buf && --buf->nframesout == 0 && buf->resid == 0)
113969cf2d85SEd Cashin 		aoe_end_buf(d, buf);
1140896831f5SEd Cashin 
114169cf2d85SEd Cashin 	aoecmd_work(d);
114269cf2d85SEd Cashin 
1143896831f5SEd Cashin 	spin_unlock_irq(&d->lock);
114469cf2d85SEd Cashin 	aoedev_put(d);
1145896831f5SEd Cashin 	dev_kfree_skb(skb);
1146896831f5SEd Cashin }
1147896831f5SEd Cashin 
1148896831f5SEd Cashin /* Enters with iocq.lock held.
1149896831f5SEd Cashin  * Returns true iff responses needing processing remain.
1150896831f5SEd Cashin  */
1151896831f5SEd Cashin static int
1152896831f5SEd Cashin ktio(void)
1153896831f5SEd Cashin {
1154896831f5SEd Cashin 	struct frame *f;
1155896831f5SEd Cashin 	struct list_head *pos;
1156896831f5SEd Cashin 	int i;
1157896831f5SEd Cashin 
1158896831f5SEd Cashin 	for (i = 0; ; ++i) {
1159896831f5SEd Cashin 		if (i == MAXIOC)
1160896831f5SEd Cashin 			return 1;
1161896831f5SEd Cashin 		if (list_empty(&iocq.head))
1162896831f5SEd Cashin 			return 0;
1163896831f5SEd Cashin 		pos = iocq.head.next;
1164896831f5SEd Cashin 		list_del(pos);
1165896831f5SEd Cashin 		spin_unlock_irq(&iocq.lock);
1166896831f5SEd Cashin 		f = list_entry(pos, struct frame, head);
1167896831f5SEd Cashin 		ktiocomplete(f);
1168896831f5SEd Cashin 		spin_lock_irq(&iocq.lock);
1169896831f5SEd Cashin 	}
1170896831f5SEd Cashin }
1171896831f5SEd Cashin 
1172896831f5SEd Cashin static int
1173896831f5SEd Cashin kthread(void *vp)
1174896831f5SEd Cashin {
1175896831f5SEd Cashin 	struct ktstate *k;
1176896831f5SEd Cashin 	DECLARE_WAITQUEUE(wait, current);
1177896831f5SEd Cashin 	int more;
1178896831f5SEd Cashin 
1179896831f5SEd Cashin 	k = vp;
1180896831f5SEd Cashin 	current->flags |= PF_NOFREEZE;
1181896831f5SEd Cashin 	set_user_nice(current, -10);
1182896831f5SEd Cashin 	complete(&k->rendez);	/* tell spawner we're running */
1183896831f5SEd Cashin 	do {
1184896831f5SEd Cashin 		spin_lock_irq(k->lock);
1185896831f5SEd Cashin 		more = k->fn();
1186896831f5SEd Cashin 		if (!more) {
1187896831f5SEd Cashin 			add_wait_queue(k->waitq, &wait);
1188896831f5SEd Cashin 			__set_current_state(TASK_INTERRUPTIBLE);
1189896831f5SEd Cashin 		}
1190896831f5SEd Cashin 		spin_unlock_irq(k->lock);
1191896831f5SEd Cashin 		if (!more) {
1192896831f5SEd Cashin 			schedule();
1193896831f5SEd Cashin 			remove_wait_queue(k->waitq, &wait);
1194896831f5SEd Cashin 		} else
1195896831f5SEd Cashin 			cond_resched();
1196896831f5SEd Cashin 	} while (!kthread_should_stop());
1197896831f5SEd Cashin 	complete(&k->rendez);	/* tell spawner we're stopping */
1198896831f5SEd Cashin 	return 0;
1199896831f5SEd Cashin }
1200896831f5SEd Cashin 
1201eb086ec5SEd Cashin void
1202896831f5SEd Cashin aoe_ktstop(struct ktstate *k)
1203896831f5SEd Cashin {
1204896831f5SEd Cashin 	kthread_stop(k->task);
1205896831f5SEd Cashin 	wait_for_completion(&k->rendez);
1206896831f5SEd Cashin }
1207896831f5SEd Cashin 
1208eb086ec5SEd Cashin int
1209896831f5SEd Cashin aoe_ktstart(struct ktstate *k)
1210896831f5SEd Cashin {
1211896831f5SEd Cashin 	struct task_struct *task;
1212896831f5SEd Cashin 
1213896831f5SEd Cashin 	init_completion(&k->rendez);
1214896831f5SEd Cashin 	task = kthread_run(kthread, k, k->name);
1215896831f5SEd Cashin 	if (task == NULL || IS_ERR(task))
1216896831f5SEd Cashin 		return -ENOMEM;
1217896831f5SEd Cashin 	k->task = task;
1218896831f5SEd Cashin 	wait_for_completion(&k->rendez); /* allow kthread to start */
1219896831f5SEd Cashin 	init_completion(&k->rendez);	/* for waiting for exit later */
1220896831f5SEd Cashin 	return 0;
1221896831f5SEd Cashin }
1222896831f5SEd Cashin 
1223896831f5SEd Cashin /* pass it off to kthreads for processing */
1224896831f5SEd Cashin static void
1225896831f5SEd Cashin ktcomplete(struct frame *f, struct sk_buff *skb)
1226896831f5SEd Cashin {
1227896831f5SEd Cashin 	ulong flags;
1228896831f5SEd Cashin 
1229896831f5SEd Cashin 	f->r_skb = skb;
1230896831f5SEd Cashin 	spin_lock_irqsave(&iocq.lock, flags);
1231896831f5SEd Cashin 	list_add_tail(&f->head, &iocq.head);
1232896831f5SEd Cashin 	spin_unlock_irqrestore(&iocq.lock, flags);
1233896831f5SEd Cashin 	wake_up(&ktiowq);
1234896831f5SEd Cashin }
1235896831f5SEd Cashin 
1236896831f5SEd Cashin struct sk_buff *
1237896831f5SEd Cashin aoecmd_ata_rsp(struct sk_buff *skb)
1238896831f5SEd Cashin {
1239896831f5SEd Cashin 	struct aoedev *d;
1240896831f5SEd Cashin 	struct aoe_hdr *h;
1241896831f5SEd Cashin 	struct frame *f;
1242896831f5SEd Cashin 	u32 n;
12431da177e4SLinus Torvalds 	ulong flags;
12441da177e4SLinus Torvalds 	char ebuf[128];
124532465c65Secashin@coraid.com 	u16 aoemajor;
12461da177e4SLinus Torvalds 
1247896831f5SEd Cashin 	h = (struct aoe_hdr *) skb->data;
1248896831f5SEd Cashin 	aoemajor = be16_to_cpu(get_unaligned(&h->major));
12490c966214SEd Cashin 	d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
12501da177e4SLinus Torvalds 	if (d == NULL) {
12511da177e4SLinus Torvalds 		snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
12521da177e4SLinus Torvalds 			"for unknown device %d.%d\n",
1253896831f5SEd Cashin 			aoemajor, h->minor);
12541da177e4SLinus Torvalds 		aoechr_error(ebuf);
1255896831f5SEd Cashin 		return skb;
12561da177e4SLinus Torvalds 	}
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	spin_lock_irqsave(&d->lock, flags);
12591da177e4SLinus Torvalds 
1260896831f5SEd Cashin 	n = be32_to_cpu(get_unaligned(&h->tag));
126164a80f5aSEd Cashin 	f = getframe(d, n);
12623a0c40d2SEd Cashin 	if (f) {
12635f0c9c48SEd Cashin 		calc_rttavg(d, f->t, tsince_hr(f));
12643a0c40d2SEd Cashin 		f->t->nout--;
12653a0c40d2SEd Cashin 	} else {
12663a0c40d2SEd Cashin 		f = getframe_deferred(d, n);
12673a0c40d2SEd Cashin 		if (f) {
12685f0c9c48SEd Cashin 			calc_rttavg(d, NULL, tsince_hr(f));
12693a0c40d2SEd Cashin 		} else {
12703a0c40d2SEd Cashin 			calc_rttavg(d, NULL, tsince(n));
12711da177e4SLinus Torvalds 			spin_unlock_irqrestore(&d->lock, flags);
127269cf2d85SEd Cashin 			aoedev_put(d);
12733a0c40d2SEd Cashin 			snprintf(ebuf, sizeof(ebuf),
12742292a7e1SEd Cashin 				 "%15s e%d.%d    tag=%08x@%08lx s=%pm d=%pm\n",
12751da177e4SLinus Torvalds 				 "unexpected rsp",
1276896831f5SEd Cashin 				 get_unaligned_be16(&h->major),
1277896831f5SEd Cashin 				 h->minor,
1278896831f5SEd Cashin 				 get_unaligned_be32(&h->tag),
12792292a7e1SEd Cashin 				 jiffies,
12802292a7e1SEd Cashin 				 h->src,
12812292a7e1SEd Cashin 				 h->dst);
12821da177e4SLinus Torvalds 			aoechr_error(ebuf);
1283896831f5SEd Cashin 			return skb;
12841da177e4SLinus Torvalds 		}
12853a0c40d2SEd Cashin 	}
12861da177e4SLinus Torvalds 	aoecmd_work(d);
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	spin_unlock_irqrestore(&d->lock, flags);
1289896831f5SEd Cashin 
1290896831f5SEd Cashin 	ktcomplete(f, skb);
1291896831f5SEd Cashin 
1292896831f5SEd Cashin 	/*
1293896831f5SEd Cashin 	 * Note here that we do not perform an aoedev_put, as we are
1294896831f5SEd Cashin 	 * leaving this reference for the ktio to release.
1295896831f5SEd Cashin 	 */
1296896831f5SEd Cashin 	return NULL;
12971da177e4SLinus Torvalds }
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds void
13001da177e4SLinus Torvalds aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
13011da177e4SLinus Torvalds {
1302e9bb8fb0SDavid S. Miller 	struct sk_buff_head queue;
13031da177e4SLinus Torvalds 
1304e9bb8fb0SDavid S. Miller 	__skb_queue_head_init(&queue);
1305e9bb8fb0SDavid S. Miller 	aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
1306e9bb8fb0SDavid S. Miller 	aoenet_xmit(&queue);
13071da177e4SLinus Torvalds }
13081da177e4SLinus Torvalds 
130968e0d42fSEd L. Cashin struct sk_buff *
13101da177e4SLinus Torvalds aoecmd_ata_id(struct aoedev *d)
13111da177e4SLinus Torvalds {
13121da177e4SLinus Torvalds 	struct aoe_hdr *h;
13131da177e4SLinus Torvalds 	struct aoe_atahdr *ah;
13141da177e4SLinus Torvalds 	struct frame *f;
13151da177e4SLinus Torvalds 	struct sk_buff *skb;
131668e0d42fSEd L. Cashin 	struct aoetgt *t;
13171da177e4SLinus Torvalds 
1318896831f5SEd Cashin 	f = newframe(d);
131968e0d42fSEd L. Cashin 	if (f == NULL)
13201da177e4SLinus Torvalds 		return NULL;
132168e0d42fSEd L. Cashin 
132268e0d42fSEd L. Cashin 	t = *d->tgt;
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	/* initialize the headers & frame */
1325e407a7f6SEd L. Cashin 	skb = f->skb;
1326abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
13271da177e4SLinus Torvalds 	ah = (struct aoe_atahdr *) (h+1);
132819900cdeSEd L. Cashin 	skb_put(skb, sizeof *h + sizeof *ah);
132919900cdeSEd L. Cashin 	memset(h, 0, skb->len);
133068e0d42fSEd L. Cashin 	f->tag = aoehdr_atainit(d, t, h);
1331896831f5SEd Cashin 	fhash(f);
133268e0d42fSEd L. Cashin 	t->nout++;
13331da177e4SLinus Torvalds 	f->waited = 0;
1334*3fc9b032SEd Cashin 	f->waited_total = 0;
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 	/* set up ata header */
13371da177e4SLinus Torvalds 	ah->scnt = 1;
133804b3ab52SBartlomiej Zolnierkiewicz 	ah->cmdstat = ATA_CMD_ID_ATA;
13391da177e4SLinus Torvalds 	ah->lba3 = 0xa0;
13401da177e4SLinus Torvalds 
134168e0d42fSEd L. Cashin 	skb->dev = t->ifp->nd;
13421da177e4SLinus Torvalds 
13433a0c40d2SEd Cashin 	d->rttavg = RTTAVG_INIT;
13443a0c40d2SEd Cashin 	d->rttdev = RTTDEV_INIT;
13451da177e4SLinus Torvalds 	d->timer.function = rexmit_timer;
13461da177e4SLinus Torvalds 
13475f0c9c48SEd Cashin 	skb = skb_clone(skb, GFP_ATOMIC);
13485f0c9c48SEd Cashin 	if (skb) {
13495f0c9c48SEd Cashin 		do_gettimeofday(&f->sent);
13505f0c9c48SEd Cashin 		f->sent_jiffs = (u32) jiffies;
13515f0c9c48SEd Cashin 	}
13525f0c9c48SEd Cashin 
13535f0c9c48SEd Cashin 	return skb;
13541da177e4SLinus Torvalds }
13551da177e4SLinus Torvalds 
135668e0d42fSEd L. Cashin static struct aoetgt *
135768e0d42fSEd L. Cashin addtgt(struct aoedev *d, char *addr, ulong nframes)
135868e0d42fSEd L. Cashin {
135968e0d42fSEd L. Cashin 	struct aoetgt *t, **tt, **te;
136068e0d42fSEd L. Cashin 
136168e0d42fSEd L. Cashin 	tt = d->targets;
136268e0d42fSEd L. Cashin 	te = tt + NTARGETS;
136368e0d42fSEd L. Cashin 	for (; tt < te && *tt; tt++)
136468e0d42fSEd L. Cashin 		;
136568e0d42fSEd L. Cashin 
1366578c4aa0SEd L. Cashin 	if (tt == te) {
1367578c4aa0SEd L. Cashin 		printk(KERN_INFO
1368578c4aa0SEd L. Cashin 			"aoe: device addtgt failure; too many targets\n");
136968e0d42fSEd L. Cashin 		return NULL;
1370578c4aa0SEd L. Cashin 	}
1371896831f5SEd Cashin 	t = kzalloc(sizeof(*t), GFP_ATOMIC);
1372896831f5SEd Cashin 	if (!t) {
1373578c4aa0SEd L. Cashin 		printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
13749bb237b6SEd L. Cashin 		return NULL;
13759bb237b6SEd L. Cashin 	}
13769bb237b6SEd L. Cashin 
1377896831f5SEd Cashin 	d->ntargets++;
137868e0d42fSEd L. Cashin 	t->nframes = nframes;
1379896831f5SEd Cashin 	t->d = d;
138068e0d42fSEd L. Cashin 	memcpy(t->addr, addr, sizeof t->addr);
138168e0d42fSEd L. Cashin 	t->ifp = t->ifs;
13823a0c40d2SEd Cashin 	aoecmd_wreset(t);
1383896831f5SEd Cashin 	INIT_LIST_HEAD(&t->ffree);
138468e0d42fSEd L. Cashin 	return *tt = t;
138568e0d42fSEd L. Cashin }
138668e0d42fSEd L. Cashin 
13873f0f0133SEd Cashin static void
13883f0f0133SEd Cashin setdbcnt(struct aoedev *d)
13893f0f0133SEd Cashin {
13903f0f0133SEd Cashin 	struct aoetgt **t, **e;
13913f0f0133SEd Cashin 	int bcnt = 0;
13923f0f0133SEd Cashin 
13933f0f0133SEd Cashin 	t = d->targets;
13943f0f0133SEd Cashin 	e = t + NTARGETS;
13953f0f0133SEd Cashin 	for (; t < e && *t; t++)
13963f0f0133SEd Cashin 		if (bcnt == 0 || bcnt > (*t)->minbcnt)
13973f0f0133SEd Cashin 			bcnt = (*t)->minbcnt;
13983f0f0133SEd Cashin 	if (bcnt != d->maxbcnt) {
13993f0f0133SEd Cashin 		d->maxbcnt = bcnt;
14003f0f0133SEd Cashin 		pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
14013f0f0133SEd Cashin 			d->aoemajor, d->aoeminor, bcnt);
14023f0f0133SEd Cashin 	}
14033f0f0133SEd Cashin }
14043f0f0133SEd Cashin 
14053f0f0133SEd Cashin static void
14063f0f0133SEd Cashin setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
14073f0f0133SEd Cashin {
14083f0f0133SEd Cashin 	struct aoedev *d;
14093f0f0133SEd Cashin 	struct aoeif *p, *e;
14103f0f0133SEd Cashin 	int minbcnt;
14113f0f0133SEd Cashin 
14123f0f0133SEd Cashin 	d = t->d;
14133f0f0133SEd Cashin 	minbcnt = bcnt;
14143f0f0133SEd Cashin 	p = t->ifs;
14153f0f0133SEd Cashin 	e = p + NAOEIFS;
14163f0f0133SEd Cashin 	for (; p < e; p++) {
14173f0f0133SEd Cashin 		if (p->nd == NULL)
14183f0f0133SEd Cashin 			break;		/* end of the valid interfaces */
14193f0f0133SEd Cashin 		if (p->nd == nd) {
14203f0f0133SEd Cashin 			p->bcnt = bcnt;	/* we're updating */
14213f0f0133SEd Cashin 			nd = NULL;
14223f0f0133SEd Cashin 		} else if (minbcnt > p->bcnt)
14233f0f0133SEd Cashin 			minbcnt = p->bcnt; /* find the min interface */
14243f0f0133SEd Cashin 	}
14253f0f0133SEd Cashin 	if (nd) {
14263f0f0133SEd Cashin 		if (p == e) {
14273f0f0133SEd Cashin 			pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
14283f0f0133SEd Cashin 			return;
14293f0f0133SEd Cashin 		}
14301b86fda9SEd Cashin 		dev_hold(nd);
14313f0f0133SEd Cashin 		p->nd = nd;
14323f0f0133SEd Cashin 		p->bcnt = bcnt;
14333f0f0133SEd Cashin 	}
14343f0f0133SEd Cashin 	t->minbcnt = minbcnt;
14353f0f0133SEd Cashin 	setdbcnt(d);
14363f0f0133SEd Cashin }
14373f0f0133SEd Cashin 
14381da177e4SLinus Torvalds void
14391da177e4SLinus Torvalds aoecmd_cfg_rsp(struct sk_buff *skb)
14401da177e4SLinus Torvalds {
14411da177e4SLinus Torvalds 	struct aoedev *d;
14421da177e4SLinus Torvalds 	struct aoe_hdr *h;
14431da177e4SLinus Torvalds 	struct aoe_cfghdr *ch;
144468e0d42fSEd L. Cashin 	struct aoetgt *t;
14450c966214SEd Cashin 	ulong flags, aoemajor;
14461da177e4SLinus Torvalds 	struct sk_buff *sl;
144769cf2d85SEd Cashin 	struct sk_buff_head queue;
144819bf2635SEd L. Cashin 	u16 n;
14491da177e4SLinus Torvalds 
145069cf2d85SEd Cashin 	sl = NULL;
1451abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
14521da177e4SLinus Torvalds 	ch = (struct aoe_cfghdr *) (h+1);
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	/*
14551da177e4SLinus Torvalds 	 * Enough people have their dip switches set backwards to
14561da177e4SLinus Torvalds 	 * warrant a loud message for this special case.
14571da177e4SLinus Torvalds 	 */
1458823ed72eSHarvey Harrison 	aoemajor = get_unaligned_be16(&h->major);
14591da177e4SLinus Torvalds 	if (aoemajor == 0xfff) {
1460a12c93f0SEd L. Cashin 		printk(KERN_ERR "aoe: Warning: shelf address is all ones.  "
14616bb6285fSEd L. Cashin 			"Check shelf dip switches.\n");
14621da177e4SLinus Torvalds 		return;
14631da177e4SLinus Torvalds 	}
14647159e969SEd Cashin 	if (aoemajor == 0xffff) {
14657159e969SEd Cashin 		pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
14660c966214SEd Cashin 			aoemajor, (int) h->minor);
14676583303cSEd Cashin 		return;
14686583303cSEd Cashin 	}
14697159e969SEd Cashin 	if (h->minor == 0xff) {
14707159e969SEd Cashin 		pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
14717159e969SEd Cashin 			aoemajor, (int) h->minor);
14721da177e4SLinus Torvalds 		return;
14731da177e4SLinus Torvalds 	}
14741da177e4SLinus Torvalds 
147519bf2635SEd L. Cashin 	n = be16_to_cpu(ch->bufcnt);
14767df620d8SEd L. Cashin 	if (n > aoe_maxout)	/* keep it reasonable */
14777df620d8SEd L. Cashin 		n = aoe_maxout;
14781da177e4SLinus Torvalds 
14797159e969SEd Cashin 	d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
14807159e969SEd Cashin 	if (d == NULL) {
14817159e969SEd Cashin 		pr_info("aoe: device allocation failure\n");
14827159e969SEd Cashin 		return;
14837159e969SEd Cashin 	}
14847159e969SEd Cashin 
14851da177e4SLinus Torvalds 	spin_lock_irqsave(&d->lock, flags);
14861da177e4SLinus Torvalds 
148768e0d42fSEd L. Cashin 	t = gettgt(d, h->src);
14881b8a1636SEd Cashin 	if (t) {
14891b8a1636SEd Cashin 		t->nframes = n;
14901b8a1636SEd Cashin 		if (n < t->maxout)
14913a0c40d2SEd Cashin 			aoecmd_wreset(t);
14921b8a1636SEd Cashin 	} else {
149368e0d42fSEd L. Cashin 		t = addtgt(d, h->src, n);
149469cf2d85SEd Cashin 		if (!t)
149569cf2d85SEd Cashin 			goto bail;
149668e0d42fSEd L. Cashin 	}
14973f0f0133SEd Cashin 	n = skb->dev->mtu;
149819bf2635SEd L. Cashin 	n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
149919bf2635SEd L. Cashin 	n /= 512;
150019bf2635SEd L. Cashin 	if (n > ch->scnt)
150119bf2635SEd L. Cashin 		n = ch->scnt;
15024f51dc5eSEd L. Cashin 	n = n ? n * 512 : DEFAULTBCNT;
15033f0f0133SEd Cashin 	setifbcnt(t, skb->dev, n);
15043ae1c24eSEd L. Cashin 
15053ae1c24eSEd L. Cashin 	/* don't change users' perspective */
150669cf2d85SEd Cashin 	if (d->nopen == 0) {
150763e9cc5dSecashin@coraid.com 		d->fw_ver = be16_to_cpu(ch->fwver);
150868e0d42fSEd L. Cashin 		sl = aoecmd_ata_id(d);
150969cf2d85SEd Cashin 	}
151069cf2d85SEd Cashin bail:
15111da177e4SLinus Torvalds 	spin_unlock_irqrestore(&d->lock, flags);
151269cf2d85SEd Cashin 	aoedev_put(d);
1513e9bb8fb0SDavid S. Miller 	if (sl) {
1514e9bb8fb0SDavid S. Miller 		__skb_queue_head_init(&queue);
1515e9bb8fb0SDavid S. Miller 		__skb_queue_tail(&queue, sl);
1516e9bb8fb0SDavid S. Miller 		aoenet_xmit(&queue);
1517e9bb8fb0SDavid S. Miller 	}
15181da177e4SLinus Torvalds }
15191da177e4SLinus Torvalds 
152068e0d42fSEd L. Cashin void
15213a0c40d2SEd Cashin aoecmd_wreset(struct aoetgt *t)
15223a0c40d2SEd Cashin {
15233a0c40d2SEd Cashin 	t->maxout = 1;
15243a0c40d2SEd Cashin 	t->ssthresh = t->nframes / 2;
15253a0c40d2SEd Cashin 	t->next_cwnd = t->nframes;
15263a0c40d2SEd Cashin }
15273a0c40d2SEd Cashin 
15283a0c40d2SEd Cashin void
152968e0d42fSEd L. Cashin aoecmd_cleanslate(struct aoedev *d)
153068e0d42fSEd L. Cashin {
153168e0d42fSEd L. Cashin 	struct aoetgt **t, **te;
153268e0d42fSEd L. Cashin 
15333a0c40d2SEd Cashin 	d->rttavg = RTTAVG_INIT;
15343a0c40d2SEd Cashin 	d->rttdev = RTTDEV_INIT;
15353f0f0133SEd Cashin 	d->maxbcnt = 0;
153668e0d42fSEd L. Cashin 
153768e0d42fSEd L. Cashin 	t = d->targets;
153868e0d42fSEd L. Cashin 	te = t + NTARGETS;
15393f0f0133SEd Cashin 	for (; t < te && *t; t++)
15403a0c40d2SEd Cashin 		aoecmd_wreset(*t);
154168e0d42fSEd L. Cashin }
1542896831f5SEd Cashin 
154369cf2d85SEd Cashin void
154469cf2d85SEd Cashin aoe_failbuf(struct aoedev *d, struct buf *buf)
154569cf2d85SEd Cashin {
154669cf2d85SEd Cashin 	if (buf == NULL)
154769cf2d85SEd Cashin 		return;
154869cf2d85SEd Cashin 	buf->resid = 0;
154969cf2d85SEd Cashin 	clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
155069cf2d85SEd Cashin 	if (buf->nframesout == 0)
155169cf2d85SEd Cashin 		aoe_end_buf(d, buf);
155269cf2d85SEd Cashin }
155369cf2d85SEd Cashin 
155469cf2d85SEd Cashin void
155569cf2d85SEd Cashin aoe_flush_iocq(void)
1556896831f5SEd Cashin {
1557896831f5SEd Cashin 	struct frame *f;
1558896831f5SEd Cashin 	struct aoedev *d;
1559896831f5SEd Cashin 	LIST_HEAD(flist);
1560896831f5SEd Cashin 	struct list_head *pos;
1561896831f5SEd Cashin 	struct sk_buff *skb;
1562896831f5SEd Cashin 	ulong flags;
1563896831f5SEd Cashin 
1564896831f5SEd Cashin 	spin_lock_irqsave(&iocq.lock, flags);
1565896831f5SEd Cashin 	list_splice_init(&iocq.head, &flist);
1566896831f5SEd Cashin 	spin_unlock_irqrestore(&iocq.lock, flags);
1567896831f5SEd Cashin 	while (!list_empty(&flist)) {
1568896831f5SEd Cashin 		pos = flist.next;
1569896831f5SEd Cashin 		list_del(pos);
1570896831f5SEd Cashin 		f = list_entry(pos, struct frame, head);
1571896831f5SEd Cashin 		d = f->t->d;
1572896831f5SEd Cashin 		skb = f->r_skb;
1573896831f5SEd Cashin 		spin_lock_irqsave(&d->lock, flags);
1574896831f5SEd Cashin 		if (f->buf) {
1575896831f5SEd Cashin 			f->buf->nframesout--;
1576896831f5SEd Cashin 			aoe_failbuf(d, f->buf);
1577896831f5SEd Cashin 		}
1578896831f5SEd Cashin 		aoe_freetframe(f);
1579896831f5SEd Cashin 		spin_unlock_irqrestore(&d->lock, flags);
1580896831f5SEd Cashin 		dev_kfree_skb(skb);
158169cf2d85SEd Cashin 		aoedev_put(d);
1582896831f5SEd Cashin 	}
1583896831f5SEd Cashin }
1584896831f5SEd Cashin 
1585896831f5SEd Cashin int __init
1586896831f5SEd Cashin aoecmd_init(void)
1587896831f5SEd Cashin {
1588896831f5SEd Cashin 	INIT_LIST_HEAD(&iocq.head);
1589896831f5SEd Cashin 	spin_lock_init(&iocq.lock);
1590896831f5SEd Cashin 	init_waitqueue_head(&ktiowq);
1591896831f5SEd Cashin 	kts.name = "aoe_ktio";
1592896831f5SEd Cashin 	kts.fn = ktio;
1593896831f5SEd Cashin 	kts.waitq = &ktiowq;
1594896831f5SEd Cashin 	kts.lock = &iocq.lock;
1595896831f5SEd Cashin 	return aoe_ktstart(&kts);
1596896831f5SEd Cashin }
1597896831f5SEd Cashin 
1598896831f5SEd Cashin void
1599896831f5SEd Cashin aoecmd_exit(void)
1600896831f5SEd Cashin {
1601896831f5SEd Cashin 	aoe_ktstop(&kts);
160269cf2d85SEd Cashin 	aoe_flush_iocq();
1603896831f5SEd Cashin }
1604