xref: /openbmc/linux/drivers/block/aoe/aoecmd.c (revision 3a0c40d2d29e476ece583540e4f11276e0f36d5f)
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 *
62*3a0c40d2SEd Cashin getframe_deferred(struct aoedev *d, u32 tag)
63*3a0c40d2SEd Cashin {
64*3a0c40d2SEd Cashin 	struct list_head *head, *pos, *nx;
65*3a0c40d2SEd Cashin 	struct frame *f;
66*3a0c40d2SEd Cashin 
67*3a0c40d2SEd Cashin 	head = &d->rexmitq;
68*3a0c40d2SEd Cashin 	list_for_each_safe(pos, nx, head) {
69*3a0c40d2SEd Cashin 		f = list_entry(pos, struct frame, head);
70*3a0c40d2SEd Cashin 		if (f->tag == tag) {
71*3a0c40d2SEd Cashin 			list_del(pos);
72*3a0c40d2SEd Cashin 			return f;
73*3a0c40d2SEd Cashin 		}
74*3a0c40d2SEd Cashin 	}
75*3a0c40d2SEd Cashin 	return NULL;
76*3a0c40d2SEd Cashin }
77*3a0c40d2SEd Cashin 
78*3a0c40d2SEd 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;
3551da177e4SLinus Torvalds 	f->buf = buf;
35619bf2635SEd L. Cashin 	f->bcnt = bcnt;
35768e0d42fSEd L. Cashin 	f->lba = buf->sector;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	/* set up ata header */
3601da177e4SLinus Torvalds 	ah->scnt = bcnt >> 9;
36168e0d42fSEd L. Cashin 	put_lba(ah, buf->sector);
3621da177e4SLinus Torvalds 	if (d->flags & DEVFL_EXT) {
3631da177e4SLinus Torvalds 		ah->aflags |= AOEAFL_EXT;
3641da177e4SLinus Torvalds 	} else {
3651da177e4SLinus Torvalds 		extbit = 0;
3661da177e4SLinus Torvalds 		ah->lba3 &= 0x0f;
3671da177e4SLinus Torvalds 		ah->lba3 |= 0xe0;	/* LBA bit + obsolete 0xa0 */
3681da177e4SLinus Torvalds 	}
3691da177e4SLinus Torvalds 	if (bio_data_dir(buf->bio) == WRITE) {
3703d5b0605SEd Cashin 		skb_fillup(skb, f->bv, f->bv_off, bcnt);
3711da177e4SLinus Torvalds 		ah->aflags |= AOEAFL_WRITE;
3724f51dc5eSEd L. Cashin 		skb->len += bcnt;
3734f51dc5eSEd L. Cashin 		skb->data_len = bcnt;
3743d5b0605SEd Cashin 		skb->truesize += bcnt;
37568e0d42fSEd L. Cashin 		t->wpkts++;
3761da177e4SLinus Torvalds 	} else {
37768e0d42fSEd L. Cashin 		t->rpkts++;
3781da177e4SLinus Torvalds 		writebit = 0;
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds 
38104b3ab52SBartlomiej Zolnierkiewicz 	ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	/* mark all tracking fields and load out */
3841da177e4SLinus Torvalds 	buf->nframesout += 1;
3851da177e4SLinus Torvalds 	buf->sector += bcnt >> 9;
3861da177e4SLinus Torvalds 
38768e0d42fSEd L. Cashin 	skb->dev = t->ifp->nd;
3884f51dc5eSEd L. Cashin 	skb = skb_clone(skb, GFP_ATOMIC);
38969cf2d85SEd Cashin 	if (skb) {
39069cf2d85SEd Cashin 		__skb_queue_head_init(&queue);
39169cf2d85SEd Cashin 		__skb_queue_tail(&queue, skb);
39269cf2d85SEd Cashin 		aoenet_xmit(&queue);
39369cf2d85SEd Cashin 	}
39468e0d42fSEd L. Cashin 	return 1;
39568e0d42fSEd L. Cashin }
3961da177e4SLinus Torvalds 
3973ae1c24eSEd L. Cashin /* some callers cannot sleep, and they can call this function,
3983ae1c24eSEd L. Cashin  * transmitting the packets later, when interrupts are on
3993ae1c24eSEd L. Cashin  */
400e9bb8fb0SDavid S. Miller static void
401e9bb8fb0SDavid S. Miller aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
4023ae1c24eSEd L. Cashin {
4033ae1c24eSEd L. Cashin 	struct aoe_hdr *h;
4043ae1c24eSEd L. Cashin 	struct aoe_cfghdr *ch;
405e9bb8fb0SDavid S. Miller 	struct sk_buff *skb;
4063ae1c24eSEd L. Cashin 	struct net_device *ifp;
4073ae1c24eSEd L. Cashin 
408840a185dSEric Dumazet 	rcu_read_lock();
409840a185dSEric Dumazet 	for_each_netdev_rcu(&init_net, ifp) {
4103ae1c24eSEd L. Cashin 		dev_hold(ifp);
4113ae1c24eSEd L. Cashin 		if (!is_aoe_netif(ifp))
4127562f876SPavel Emelianov 			goto cont;
4133ae1c24eSEd L. Cashin 
414e407a7f6SEd L. Cashin 		skb = new_skb(sizeof *h + sizeof *ch);
4153ae1c24eSEd L. Cashin 		if (skb == NULL) {
416a12c93f0SEd L. Cashin 			printk(KERN_INFO "aoe: skb alloc failure\n");
4177562f876SPavel Emelianov 			goto cont;
4183ae1c24eSEd L. Cashin 		}
41919900cdeSEd L. Cashin 		skb_put(skb, sizeof *h + sizeof *ch);
420e407a7f6SEd L. Cashin 		skb->dev = ifp;
421e9bb8fb0SDavid S. Miller 		__skb_queue_tail(queue, skb);
422abdbf94dSEd L. Cashin 		h = (struct aoe_hdr *) skb_mac_header(skb);
4233ae1c24eSEd L. Cashin 		memset(h, 0, sizeof *h + sizeof *ch);
4243ae1c24eSEd L. Cashin 
4253ae1c24eSEd L. Cashin 		memset(h->dst, 0xff, sizeof h->dst);
4263ae1c24eSEd L. Cashin 		memcpy(h->src, ifp->dev_addr, sizeof h->src);
4273ae1c24eSEd L. Cashin 		h->type = __constant_cpu_to_be16(ETH_P_AOE);
4283ae1c24eSEd L. Cashin 		h->verfl = AOE_HVER;
4293ae1c24eSEd L. Cashin 		h->major = cpu_to_be16(aoemajor);
4303ae1c24eSEd L. Cashin 		h->minor = aoeminor;
4313ae1c24eSEd L. Cashin 		h->cmd = AOECMD_CFG;
4323ae1c24eSEd L. Cashin 
4337562f876SPavel Emelianov cont:
4347562f876SPavel Emelianov 		dev_put(ifp);
4353ae1c24eSEd L. Cashin 	}
436840a185dSEric Dumazet 	rcu_read_unlock();
4373ae1c24eSEd L. Cashin }
4383ae1c24eSEd L. Cashin 
4391da177e4SLinus Torvalds static void
440896831f5SEd Cashin resend(struct aoedev *d, struct frame *f)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds 	struct sk_buff *skb;
44369cf2d85SEd Cashin 	struct sk_buff_head queue;
4441da177e4SLinus Torvalds 	struct aoe_hdr *h;
44519bf2635SEd L. Cashin 	struct aoe_atahdr *ah;
446896831f5SEd Cashin 	struct aoetgt *t;
4471da177e4SLinus Torvalds 	char buf[128];
4481da177e4SLinus Torvalds 	u32 n;
4491da177e4SLinus Torvalds 
450896831f5SEd Cashin 	t = f->t;
45164a80f5aSEd Cashin 	n = newtag(d);
452e407a7f6SEd L. Cashin 	skb = f->skb;
4533f0f0133SEd Cashin 	if (ifrotate(t) == NULL) {
4543f0f0133SEd Cashin 		/* probably can't happen, but set it up to fail anyway */
4553f0f0133SEd Cashin 		pr_info("aoe: resend: no interfaces to rotate to.\n");
4563f0f0133SEd Cashin 		ktcomplete(f, NULL);
4573f0f0133SEd Cashin 		return;
4583f0f0133SEd Cashin 	}
459abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
46019bf2635SEd L. Cashin 	ah = (struct aoe_atahdr *) (h+1);
46168e0d42fSEd L. Cashin 
46268e0d42fSEd L. Cashin 	snprintf(buf, sizeof buf,
463411c41eeSHarvey Harrison 		"%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
46468e0d42fSEd L. Cashin 		"retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
465411c41eeSHarvey Harrison 		h->src, h->dst, t->nout);
46668e0d42fSEd L. Cashin 	aoechr_error(buf);
46768e0d42fSEd L. Cashin 
4681da177e4SLinus Torvalds 	f->tag = n;
469896831f5SEd Cashin 	fhash(f);
47063e9cc5dSecashin@coraid.com 	h->tag = cpu_to_be32(n);
47168e0d42fSEd L. Cashin 	memcpy(h->dst, t->addr, sizeof h->dst);
47268e0d42fSEd L. Cashin 	memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
4731da177e4SLinus Torvalds 
47468e0d42fSEd L. Cashin 	skb->dev = t->ifp->nd;
4754f51dc5eSEd L. Cashin 	skb = skb_clone(skb, GFP_ATOMIC);
4764f51dc5eSEd L. Cashin 	if (skb == NULL)
4774f51dc5eSEd L. Cashin 		return;
47869cf2d85SEd Cashin 	__skb_queue_head_init(&queue);
47969cf2d85SEd Cashin 	__skb_queue_tail(&queue, skb);
48069cf2d85SEd Cashin 	aoenet_xmit(&queue);
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds static int
484896831f5SEd Cashin tsince(u32 tag)
4851da177e4SLinus Torvalds {
4861da177e4SLinus Torvalds 	int n;
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	n = jiffies & 0xffff;
4891da177e4SLinus Torvalds 	n -= tag & 0xffff;
4901da177e4SLinus Torvalds 	if (n < 0)
4911da177e4SLinus Torvalds 		n += 1<<16;
4921da177e4SLinus Torvalds 	return n;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
49568e0d42fSEd L. Cashin static struct aoeif *
49668e0d42fSEd L. Cashin getif(struct aoetgt *t, struct net_device *nd)
49768e0d42fSEd L. Cashin {
49868e0d42fSEd L. Cashin 	struct aoeif *p, *e;
49968e0d42fSEd L. Cashin 
50068e0d42fSEd L. Cashin 	p = t->ifs;
50168e0d42fSEd L. Cashin 	e = p + NAOEIFS;
50268e0d42fSEd L. Cashin 	for (; p < e; p++)
50368e0d42fSEd L. Cashin 		if (p->nd == nd)
50468e0d42fSEd L. Cashin 			return p;
50568e0d42fSEd L. Cashin 	return NULL;
50668e0d42fSEd L. Cashin }
50768e0d42fSEd L. Cashin 
50868e0d42fSEd L. Cashin static void
50968e0d42fSEd L. Cashin ejectif(struct aoetgt *t, struct aoeif *ifp)
51068e0d42fSEd L. Cashin {
51168e0d42fSEd L. Cashin 	struct aoeif *e;
5121b86fda9SEd Cashin 	struct net_device *nd;
51368e0d42fSEd L. Cashin 	ulong n;
51468e0d42fSEd L. Cashin 
5151b86fda9SEd Cashin 	nd = ifp->nd;
51668e0d42fSEd L. Cashin 	e = t->ifs + NAOEIFS - 1;
51768e0d42fSEd L. Cashin 	n = (e - ifp) * sizeof *ifp;
51868e0d42fSEd L. Cashin 	memmove(ifp, ifp+1, n);
51968e0d42fSEd L. Cashin 	e->nd = NULL;
5201b86fda9SEd Cashin 	dev_put(nd);
52168e0d42fSEd L. Cashin }
52268e0d42fSEd L. Cashin 
52368e0d42fSEd L. Cashin static int
52468e0d42fSEd L. Cashin sthtith(struct aoedev *d)
52568e0d42fSEd L. Cashin {
526896831f5SEd Cashin 	struct frame *f, *nf;
527896831f5SEd Cashin 	struct list_head *nx, *pos, *head;
52868e0d42fSEd L. Cashin 	struct sk_buff *skb;
529896831f5SEd Cashin 	struct aoetgt *ht = d->htgt;
530896831f5SEd Cashin 	int i;
53168e0d42fSEd L. Cashin 
532896831f5SEd Cashin 	for (i = 0; i < NFACTIVE; i++) {
53364a80f5aSEd Cashin 		head = &d->factive[i];
534896831f5SEd Cashin 		list_for_each_safe(pos, nx, head) {
535896831f5SEd Cashin 			f = list_entry(pos, struct frame, head);
53664a80f5aSEd Cashin 			if (f->t != ht)
53764a80f5aSEd Cashin 				continue;
53864a80f5aSEd Cashin 
539896831f5SEd Cashin 			nf = newframe(d);
54068e0d42fSEd L. Cashin 			if (!nf)
54168e0d42fSEd L. Cashin 				return 0;
542896831f5SEd Cashin 
543896831f5SEd Cashin 			/* remove frame from active list */
544896831f5SEd Cashin 			list_del(pos);
545896831f5SEd Cashin 
546896831f5SEd Cashin 			/* reassign all pertinent bits to new outbound frame */
54768e0d42fSEd L. Cashin 			skb = nf->skb;
548896831f5SEd Cashin 			nf->skb = f->skb;
549896831f5SEd Cashin 			nf->buf = f->buf;
550896831f5SEd Cashin 			nf->bcnt = f->bcnt;
551896831f5SEd Cashin 			nf->lba = f->lba;
552896831f5SEd Cashin 			nf->bv = f->bv;
553896831f5SEd Cashin 			nf->bv_off = f->bv_off;
55468e0d42fSEd L. Cashin 			nf->waited = 0;
555896831f5SEd Cashin 			f->skb = skb;
556896831f5SEd Cashin 			aoe_freetframe(f);
55768e0d42fSEd L. Cashin 			ht->nout--;
558896831f5SEd Cashin 			nf->t->nout++;
559896831f5SEd Cashin 			resend(d, nf);
560896831f5SEd Cashin 		}
56168e0d42fSEd L. Cashin 	}
5623f0f0133SEd Cashin 	/* We've cleaned up the outstanding so take away his
5633f0f0133SEd Cashin 	 * interfaces so he won't be used.  We should remove him from
5643f0f0133SEd Cashin 	 * the target array here, but cleaning up a target is
5653f0f0133SEd Cashin 	 * involved.  PUNT!
5663f0f0133SEd Cashin 	 */
56768e0d42fSEd L. Cashin 	memset(ht->ifs, 0, sizeof ht->ifs);
56868e0d42fSEd L. Cashin 	d->htgt = NULL;
56968e0d42fSEd L. Cashin 	return 1;
57068e0d42fSEd L. Cashin }
57168e0d42fSEd L. Cashin 
5721da177e4SLinus Torvalds static void
573*3a0c40d2SEd Cashin rexmit_deferred(struct aoedev *d)
574*3a0c40d2SEd Cashin {
575*3a0c40d2SEd Cashin 	struct aoetgt *t;
576*3a0c40d2SEd Cashin 	struct frame *f;
577*3a0c40d2SEd Cashin 	struct list_head *pos, *nx, *head;
578*3a0c40d2SEd Cashin 
579*3a0c40d2SEd Cashin 	head = &d->rexmitq;
580*3a0c40d2SEd Cashin 	list_for_each_safe(pos, nx, head) {
581*3a0c40d2SEd Cashin 		f = list_entry(pos, struct frame, head);
582*3a0c40d2SEd Cashin 		t = f->t;
583*3a0c40d2SEd Cashin 		if (t->nout >= t->maxout)
584*3a0c40d2SEd Cashin 			continue;
585*3a0c40d2SEd Cashin 		list_del(pos);
586*3a0c40d2SEd Cashin 		t->nout++;
587*3a0c40d2SEd Cashin 		resend(d, f);
588*3a0c40d2SEd Cashin 	}
589*3a0c40d2SEd Cashin }
590*3a0c40d2SEd Cashin 
591*3a0c40d2SEd Cashin static void
5921da177e4SLinus Torvalds rexmit_timer(ulong vp)
5931da177e4SLinus Torvalds {
5941da177e4SLinus Torvalds 	struct aoedev *d;
595*3a0c40d2SEd Cashin 	struct aoetgt *t;
59668e0d42fSEd L. Cashin 	struct aoeif *ifp;
597896831f5SEd Cashin 	struct frame *f;
598896831f5SEd Cashin 	struct list_head *head, *pos, *nx;
599896831f5SEd Cashin 	LIST_HEAD(flist);
6001da177e4SLinus Torvalds 	register long timeout;
6011da177e4SLinus Torvalds 	ulong flags, n;
602896831f5SEd Cashin 	int i;
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	d = (struct aoedev *) vp;
6051da177e4SLinus Torvalds 
606*3a0c40d2SEd Cashin 	/* timeout based on observed timings and variations */
607*3a0c40d2SEd Cashin 	timeout = 2 * d->rttavg >> RTTSCALE;
608*3a0c40d2SEd Cashin 	timeout += 8 * d->rttdev >> RTTDSCALE;
609*3a0c40d2SEd Cashin 	if (timeout == 0)
610*3a0c40d2SEd Cashin 		timeout = 1;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	spin_lock_irqsave(&d->lock, flags);
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	if (d->flags & DEVFL_TKILL) {
6151c6f3fcaSEd L. Cashin 		spin_unlock_irqrestore(&d->lock, flags);
6161da177e4SLinus Torvalds 		return;
6171da177e4SLinus Torvalds 	}
618896831f5SEd Cashin 
619896831f5SEd Cashin 	/* collect all frames to rexmit into flist */
620896831f5SEd Cashin 	for (i = 0; i < NFACTIVE; i++) {
62164a80f5aSEd Cashin 		head = &d->factive[i];
622896831f5SEd Cashin 		list_for_each_safe(pos, nx, head) {
623896831f5SEd Cashin 			f = list_entry(pos, struct frame, head);
624896831f5SEd Cashin 			if (tsince(f->tag) < timeout)
62564a80f5aSEd Cashin 				break;	/* end of expired frames */
626896831f5SEd Cashin 			/* move to flist for later processing */
627896831f5SEd Cashin 			list_move_tail(pos, &flist);
628896831f5SEd Cashin 		}
629896831f5SEd Cashin 	}
63069cf2d85SEd Cashin 
631896831f5SEd Cashin 	/* process expired frames */
632896831f5SEd Cashin 	while (!list_empty(&flist)) {
633896831f5SEd Cashin 		pos = flist.next;
634896831f5SEd Cashin 		f = list_entry(pos, struct frame, head);
635*3a0c40d2SEd Cashin 		n = f->waited += tsince(f->tag);
6361da177e4SLinus Torvalds 		n /= HZ;
63768e0d42fSEd L. Cashin 		if (n > aoe_deadsecs) {
638896831f5SEd Cashin 			/* Waited too long.  Device failure.
639896831f5SEd Cashin 			 * Hang all frames on first hash bucket for downdev
640896831f5SEd Cashin 			 * to clean up.
641896831f5SEd Cashin 			 */
64264a80f5aSEd Cashin 			list_splice(&flist, &d->factive[0]);
6431da177e4SLinus Torvalds 			aoedev_downdev(d);
644*3a0c40d2SEd Cashin 			goto out;
6451da177e4SLinus Torvalds 		}
64668e0d42fSEd L. Cashin 
647896831f5SEd Cashin 		t = f->t;
648d54d35acSEd Cashin 		if (n > aoe_deadsecs/2)
649d54d35acSEd Cashin 			d->htgt = t; /* see if another target can help */
650d54d35acSEd Cashin 
651*3a0c40d2SEd Cashin 		if (t->maxout != 1) {
652*3a0c40d2SEd Cashin 			t->ssthresh = t->maxout / 2;
653*3a0c40d2SEd Cashin 			t->maxout = 1;
65468e0d42fSEd L. Cashin 		}
65568e0d42fSEd L. Cashin 
65668e0d42fSEd L. Cashin 		ifp = getif(t, f->skb->dev);
65768e0d42fSEd L. Cashin 		if (ifp && ++ifp->lost > (t->nframes << 1)
65868e0d42fSEd L. Cashin 		&& (ifp != t->ifs || t->ifs[1].nd)) {
65968e0d42fSEd L. Cashin 			ejectif(t, ifp);
66068e0d42fSEd L. Cashin 			ifp = NULL;
66168e0d42fSEd L. Cashin 		}
662*3a0c40d2SEd Cashin 		list_move_tail(pos, &d->rexmitq);
663*3a0c40d2SEd Cashin 		t->nout--;
6641da177e4SLinus Torvalds 	}
665*3a0c40d2SEd Cashin 	rexmit_deferred(d);
66668e0d42fSEd L. Cashin 
667*3a0c40d2SEd Cashin out:
66869cf2d85SEd Cashin 	if ((d->flags & DEVFL_KICKME || d->htgt) && d->blkq) {
6694f51dc5eSEd L. Cashin 		d->flags &= ~DEVFL_KICKME;
67069cf2d85SEd Cashin 		d->blkq->request_fn(d->blkq);
6714f51dc5eSEd L. Cashin 	}
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	d->timer.expires = jiffies + TIMERTICK;
6741da177e4SLinus Torvalds 	add_timer(&d->timer);
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	spin_unlock_irqrestore(&d->lock, flags);
67769cf2d85SEd Cashin }
6781da177e4SLinus Torvalds 
67969cf2d85SEd Cashin static unsigned long
68069cf2d85SEd Cashin rqbiocnt(struct request *r)
68169cf2d85SEd Cashin {
68269cf2d85SEd Cashin 	struct bio *bio;
68369cf2d85SEd Cashin 	unsigned long n = 0;
68469cf2d85SEd Cashin 
68569cf2d85SEd Cashin 	__rq_for_each_bio(bio, r)
68669cf2d85SEd Cashin 		n++;
68769cf2d85SEd Cashin 	return n;
68869cf2d85SEd Cashin }
68969cf2d85SEd Cashin 
69069cf2d85SEd Cashin /* This can be removed if we are certain that no users of the block
69169cf2d85SEd Cashin  * layer will ever use zero-count pages in bios.  Otherwise we have to
69269cf2d85SEd Cashin  * protect against the put_page sometimes done by the network layer.
69369cf2d85SEd Cashin  *
69469cf2d85SEd Cashin  * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
69569cf2d85SEd Cashin  * discussion.
69669cf2d85SEd Cashin  *
69769cf2d85SEd Cashin  * We cannot use get_page in the workaround, because it insists on a
69869cf2d85SEd Cashin  * positive page count as a precondition.  So we use _count directly.
69969cf2d85SEd Cashin  */
70069cf2d85SEd Cashin static void
70169cf2d85SEd Cashin bio_pageinc(struct bio *bio)
70269cf2d85SEd Cashin {
70369cf2d85SEd Cashin 	struct bio_vec *bv;
70469cf2d85SEd Cashin 	struct page *page;
70569cf2d85SEd Cashin 	int i;
70669cf2d85SEd Cashin 
70769cf2d85SEd Cashin 	bio_for_each_segment(bv, bio, i) {
70869cf2d85SEd Cashin 		page = bv->bv_page;
70969cf2d85SEd Cashin 		/* Non-zero page count for non-head members of
71069cf2d85SEd Cashin 		 * compound pages is no longer allowed by the kernel,
71169cf2d85SEd Cashin 		 * but this has never been seen here.
71269cf2d85SEd Cashin 		 */
71369cf2d85SEd Cashin 		if (unlikely(PageCompound(page)))
71469cf2d85SEd Cashin 			if (compound_trans_head(page) != page) {
71569cf2d85SEd Cashin 				pr_crit("page tail used for block I/O\n");
71669cf2d85SEd Cashin 				BUG();
71769cf2d85SEd Cashin 			}
71869cf2d85SEd Cashin 		atomic_inc(&page->_count);
71969cf2d85SEd Cashin 	}
72069cf2d85SEd Cashin }
72169cf2d85SEd Cashin 
72269cf2d85SEd Cashin static void
72369cf2d85SEd Cashin bio_pagedec(struct bio *bio)
72469cf2d85SEd Cashin {
72569cf2d85SEd Cashin 	struct bio_vec *bv;
72669cf2d85SEd Cashin 	int i;
72769cf2d85SEd Cashin 
72869cf2d85SEd Cashin 	bio_for_each_segment(bv, bio, i)
72969cf2d85SEd Cashin 		atomic_dec(&bv->bv_page->_count);
73069cf2d85SEd Cashin }
73169cf2d85SEd Cashin 
73269cf2d85SEd Cashin static void
73369cf2d85SEd Cashin bufinit(struct buf *buf, struct request *rq, struct bio *bio)
73469cf2d85SEd Cashin {
73569cf2d85SEd Cashin 	struct bio_vec *bv;
73669cf2d85SEd Cashin 
73769cf2d85SEd Cashin 	memset(buf, 0, sizeof(*buf));
73869cf2d85SEd Cashin 	buf->rq = rq;
73969cf2d85SEd Cashin 	buf->bio = bio;
74069cf2d85SEd Cashin 	buf->resid = bio->bi_size;
74169cf2d85SEd Cashin 	buf->sector = bio->bi_sector;
74269cf2d85SEd Cashin 	bio_pageinc(bio);
74369cf2d85SEd Cashin 	buf->bv = bv = &bio->bi_io_vec[bio->bi_idx];
74469cf2d85SEd Cashin 	buf->bv_resid = bv->bv_len;
74569cf2d85SEd Cashin 	WARN_ON(buf->bv_resid == 0);
74669cf2d85SEd Cashin }
74769cf2d85SEd Cashin 
74869cf2d85SEd Cashin static struct buf *
74969cf2d85SEd Cashin nextbuf(struct aoedev *d)
75069cf2d85SEd Cashin {
75169cf2d85SEd Cashin 	struct request *rq;
75269cf2d85SEd Cashin 	struct request_queue *q;
75369cf2d85SEd Cashin 	struct buf *buf;
75469cf2d85SEd Cashin 	struct bio *bio;
75569cf2d85SEd Cashin 
75669cf2d85SEd Cashin 	q = d->blkq;
75769cf2d85SEd Cashin 	if (q == NULL)
75869cf2d85SEd Cashin 		return NULL;	/* initializing */
75969cf2d85SEd Cashin 	if (d->ip.buf)
76069cf2d85SEd Cashin 		return d->ip.buf;
76169cf2d85SEd Cashin 	rq = d->ip.rq;
76269cf2d85SEd Cashin 	if (rq == NULL) {
76369cf2d85SEd Cashin 		rq = blk_peek_request(q);
76469cf2d85SEd Cashin 		if (rq == NULL)
76569cf2d85SEd Cashin 			return NULL;
76669cf2d85SEd Cashin 		blk_start_request(rq);
76769cf2d85SEd Cashin 		d->ip.rq = rq;
76869cf2d85SEd Cashin 		d->ip.nxbio = rq->bio;
76969cf2d85SEd Cashin 		rq->special = (void *) rqbiocnt(rq);
77069cf2d85SEd Cashin 	}
77169cf2d85SEd Cashin 	buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
77269cf2d85SEd Cashin 	if (buf == NULL) {
77369cf2d85SEd Cashin 		pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
77469cf2d85SEd Cashin 		return NULL;
77569cf2d85SEd Cashin 	}
77669cf2d85SEd Cashin 	bio = d->ip.nxbio;
77769cf2d85SEd Cashin 	bufinit(buf, rq, bio);
77869cf2d85SEd Cashin 	bio = bio->bi_next;
77969cf2d85SEd Cashin 	d->ip.nxbio = bio;
78069cf2d85SEd Cashin 	if (bio == NULL)
78169cf2d85SEd Cashin 		d->ip.rq = NULL;
78269cf2d85SEd Cashin 	return d->ip.buf = buf;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
78568e0d42fSEd L. Cashin /* enters with d->lock held */
78668e0d42fSEd L. Cashin void
78768e0d42fSEd L. Cashin aoecmd_work(struct aoedev *d)
78868e0d42fSEd L. Cashin {
78968e0d42fSEd L. Cashin 	if (d->htgt && !sthtith(d))
79068e0d42fSEd L. Cashin 		return;
791*3a0c40d2SEd Cashin 	rexmit_deferred(d);
79269cf2d85SEd Cashin 	while (aoecmd_ata_rw(d))
79369cf2d85SEd Cashin 		;
79468e0d42fSEd L. Cashin }
79568e0d42fSEd L. Cashin 
7963ae1c24eSEd L. Cashin /* this function performs work that has been deferred until sleeping is OK
7973ae1c24eSEd L. Cashin  */
7983ae1c24eSEd L. Cashin void
799c4028958SDavid Howells aoecmd_sleepwork(struct work_struct *work)
8003ae1c24eSEd L. Cashin {
801c4028958SDavid Howells 	struct aoedev *d = container_of(work, struct aoedev, work);
802b21faa25SEd Cashin 	struct block_device *bd;
803b21faa25SEd Cashin 	u64 ssize;
8043ae1c24eSEd L. Cashin 
8053ae1c24eSEd L. Cashin 	if (d->flags & DEVFL_GDALLOC)
8063ae1c24eSEd L. Cashin 		aoeblk_gdalloc(d);
8073ae1c24eSEd L. Cashin 
8083ae1c24eSEd L. Cashin 	if (d->flags & DEVFL_NEWSIZE) {
80980795aefSTejun Heo 		ssize = get_capacity(d->gd);
8103ae1c24eSEd L. Cashin 		bd = bdget_disk(d->gd, 0);
8113ae1c24eSEd L. Cashin 		if (bd) {
8123ae1c24eSEd L. Cashin 			mutex_lock(&bd->bd_inode->i_mutex);
8133ae1c24eSEd L. Cashin 			i_size_write(bd->bd_inode, (loff_t)ssize<<9);
8143ae1c24eSEd L. Cashin 			mutex_unlock(&bd->bd_inode->i_mutex);
8153ae1c24eSEd L. Cashin 			bdput(bd);
8163ae1c24eSEd L. Cashin 		}
817b21faa25SEd Cashin 		spin_lock_irq(&d->lock);
8183ae1c24eSEd L. Cashin 		d->flags |= DEVFL_UP;
8193ae1c24eSEd L. Cashin 		d->flags &= ~DEVFL_NEWSIZE;
820b21faa25SEd Cashin 		spin_unlock_irq(&d->lock);
8213ae1c24eSEd L. Cashin 	}
8223ae1c24eSEd L. Cashin }
8233ae1c24eSEd L. Cashin 
8241da177e4SLinus Torvalds static void
825667be1e7SEd Cashin ata_ident_fixstring(u16 *id, int ns)
826667be1e7SEd Cashin {
827667be1e7SEd Cashin 	u16 s;
828667be1e7SEd Cashin 
829667be1e7SEd Cashin 	while (ns-- > 0) {
830667be1e7SEd Cashin 		s = *id;
831667be1e7SEd Cashin 		*id++ = s >> 8 | s << 8;
832667be1e7SEd Cashin 	}
833667be1e7SEd Cashin }
834667be1e7SEd Cashin 
835667be1e7SEd Cashin static void
83668e0d42fSEd L. Cashin ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
8371da177e4SLinus Torvalds {
8381da177e4SLinus Torvalds 	u64 ssize;
8391da177e4SLinus Torvalds 	u16 n;
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	/* word 83: command set supported */
842f885f8d1SHarvey Harrison 	n = get_unaligned_le16(&id[83 << 1]);
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	/* word 86: command set/feature enabled */
845f885f8d1SHarvey Harrison 	n |= get_unaligned_le16(&id[86 << 1]);
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	if (n & (1<<10)) {	/* bit 10: LBA 48 */
8481da177e4SLinus Torvalds 		d->flags |= DEVFL_EXT;
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 		/* word 100: number lba48 sectors */
851f885f8d1SHarvey Harrison 		ssize = get_unaligned_le64(&id[100 << 1]);
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 		/* set as in ide-disk.c:init_idedisk_capacity */
8541da177e4SLinus Torvalds 		d->geo.cylinders = ssize;
8551da177e4SLinus Torvalds 		d->geo.cylinders /= (255 * 63);
8561da177e4SLinus Torvalds 		d->geo.heads = 255;
8571da177e4SLinus Torvalds 		d->geo.sectors = 63;
8581da177e4SLinus Torvalds 	} else {
8591da177e4SLinus Torvalds 		d->flags &= ~DEVFL_EXT;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 		/* number lba28 sectors */
862f885f8d1SHarvey Harrison 		ssize = get_unaligned_le32(&id[60 << 1]);
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 		/* NOTE: obsolete in ATA 6 */
865f885f8d1SHarvey Harrison 		d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
866f885f8d1SHarvey Harrison 		d->geo.heads = get_unaligned_le16(&id[55 << 1]);
867f885f8d1SHarvey Harrison 		d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
8681da177e4SLinus Torvalds 	}
8693ae1c24eSEd L. Cashin 
870667be1e7SEd Cashin 	ata_ident_fixstring((u16 *) &id[10<<1], 10);	/* serial */
871667be1e7SEd Cashin 	ata_ident_fixstring((u16 *) &id[23<<1], 4);	/* firmware */
872667be1e7SEd Cashin 	ata_ident_fixstring((u16 *) &id[27<<1], 20);	/* model */
873667be1e7SEd Cashin 	memcpy(d->ident, id, sizeof(d->ident));
874667be1e7SEd Cashin 
8753ae1c24eSEd L. Cashin 	if (d->ssize != ssize)
8761d75981aSEd L. Cashin 		printk(KERN_INFO
877411c41eeSHarvey Harrison 			"aoe: %pm e%ld.%d v%04x has %llu sectors\n",
878411c41eeSHarvey Harrison 			t->addr,
8793ae1c24eSEd L. Cashin 			d->aoemajor, d->aoeminor,
8803ae1c24eSEd L. Cashin 			d->fw_ver, (long long)ssize);
8811da177e4SLinus Torvalds 	d->ssize = ssize;
8821da177e4SLinus Torvalds 	d->geo.start = 0;
8836b9699bbSEd L. Cashin 	if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
8846b9699bbSEd L. Cashin 		return;
8851da177e4SLinus Torvalds 	if (d->gd != NULL) {
88680795aefSTejun Heo 		set_capacity(d->gd, ssize);
8873ae1c24eSEd L. Cashin 		d->flags |= DEVFL_NEWSIZE;
88868e0d42fSEd L. Cashin 	} else
8893ae1c24eSEd L. Cashin 		d->flags |= DEVFL_GDALLOC;
8901da177e4SLinus Torvalds 	schedule_work(&d->work);
8911da177e4SLinus Torvalds }
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds static void
894*3a0c40d2SEd Cashin calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt)
8951da177e4SLinus Torvalds {
8961da177e4SLinus Torvalds 	register long n;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	n = rtt;
8991da177e4SLinus Torvalds 
900*3a0c40d2SEd Cashin 	/* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */
901*3a0c40d2SEd Cashin 	n -= d->rttavg >> RTTSCALE;
902*3a0c40d2SEd Cashin 	d->rttavg += n;
903*3a0c40d2SEd Cashin 	if (n < 0)
904*3a0c40d2SEd Cashin 		n = -n;
905*3a0c40d2SEd Cashin 	n -= d->rttdev >> RTTDSCALE;
906*3a0c40d2SEd Cashin 	d->rttdev += n;
907*3a0c40d2SEd Cashin 
908*3a0c40d2SEd Cashin 	if (!t || t->maxout >= t->nframes)
909*3a0c40d2SEd Cashin 		return;
910*3a0c40d2SEd Cashin 	if (t->maxout < t->ssthresh)
911*3a0c40d2SEd Cashin 		t->maxout += 1;
912*3a0c40d2SEd Cashin 	else if (t->nout == t->maxout && t->next_cwnd-- == 0) {
913*3a0c40d2SEd Cashin 		t->maxout += 1;
914*3a0c40d2SEd Cashin 		t->next_cwnd = t->maxout;
915*3a0c40d2SEd Cashin 	}
9161da177e4SLinus Torvalds }
9171da177e4SLinus Torvalds 
91868e0d42fSEd L. Cashin static struct aoetgt *
91968e0d42fSEd L. Cashin gettgt(struct aoedev *d, char *addr)
92068e0d42fSEd L. Cashin {
92168e0d42fSEd L. Cashin 	struct aoetgt **t, **e;
92268e0d42fSEd L. Cashin 
92368e0d42fSEd L. Cashin 	t = d->targets;
92468e0d42fSEd L. Cashin 	e = t + NTARGETS;
92568e0d42fSEd L. Cashin 	for (; t < e && *t; t++)
92668e0d42fSEd L. Cashin 		if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
92768e0d42fSEd L. Cashin 			return *t;
92868e0d42fSEd L. Cashin 	return NULL;
92968e0d42fSEd L. Cashin }
93068e0d42fSEd L. Cashin 
9313d5b0605SEd Cashin static void
932896831f5SEd Cashin bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
9333d5b0605SEd Cashin {
9343d5b0605SEd Cashin 	ulong fcnt;
9353d5b0605SEd Cashin 	char *p;
9363d5b0605SEd Cashin 	int soff = 0;
9373d5b0605SEd Cashin loop:
9383d5b0605SEd Cashin 	fcnt = bv->bv_len - (off - bv->bv_offset);
9393d5b0605SEd Cashin 	if (fcnt > cnt)
9403d5b0605SEd Cashin 		fcnt = cnt;
9413d5b0605SEd Cashin 	p = page_address(bv->bv_page) + off;
9423d5b0605SEd Cashin 	skb_copy_bits(skb, soff, p, fcnt);
9433d5b0605SEd Cashin 	soff += fcnt;
9443d5b0605SEd Cashin 	cnt -= fcnt;
9453d5b0605SEd Cashin 	if (cnt <= 0)
9463d5b0605SEd Cashin 		return;
9473d5b0605SEd Cashin 	bv++;
9483d5b0605SEd Cashin 	off = bv->bv_offset;
9493d5b0605SEd Cashin 	goto loop;
9503d5b0605SEd Cashin }
9513d5b0605SEd Cashin 
95269cf2d85SEd Cashin void
95369cf2d85SEd Cashin aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
95469cf2d85SEd Cashin {
95569cf2d85SEd Cashin 	struct bio *bio;
95669cf2d85SEd Cashin 	int bok;
95769cf2d85SEd Cashin 	struct request_queue *q;
95869cf2d85SEd Cashin 
95969cf2d85SEd Cashin 	q = d->blkq;
96069cf2d85SEd Cashin 	if (rq == d->ip.rq)
96169cf2d85SEd Cashin 		d->ip.rq = NULL;
96269cf2d85SEd Cashin 	do {
96369cf2d85SEd Cashin 		bio = rq->bio;
96469cf2d85SEd Cashin 		bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
96569cf2d85SEd Cashin 	} while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
96669cf2d85SEd Cashin 
96769cf2d85SEd Cashin 	/* cf. http://lkml.org/lkml/2006/10/31/28 */
96869cf2d85SEd Cashin 	if (!fastfail)
96911cfb6ffSEd Cashin 		__blk_run_queue(q);
97069cf2d85SEd Cashin }
97169cf2d85SEd Cashin 
97269cf2d85SEd Cashin static void
97369cf2d85SEd Cashin aoe_end_buf(struct aoedev *d, struct buf *buf)
97469cf2d85SEd Cashin {
97569cf2d85SEd Cashin 	struct request *rq;
97669cf2d85SEd Cashin 	unsigned long n;
97769cf2d85SEd Cashin 
97869cf2d85SEd Cashin 	if (buf == d->ip.buf)
97969cf2d85SEd Cashin 		d->ip.buf = NULL;
98069cf2d85SEd Cashin 	rq = buf->rq;
98169cf2d85SEd Cashin 	bio_pagedec(buf->bio);
98269cf2d85SEd Cashin 	mempool_free(buf, d->bufpool);
98369cf2d85SEd Cashin 	n = (unsigned long) rq->special;
98469cf2d85SEd Cashin 	rq->special = (void *) --n;
98569cf2d85SEd Cashin 	if (n == 0)
98669cf2d85SEd Cashin 		aoe_end_request(d, rq, 0);
98769cf2d85SEd Cashin }
98869cf2d85SEd Cashin 
9893d5b0605SEd Cashin static void
990896831f5SEd Cashin ktiocomplete(struct frame *f)
9913d5b0605SEd Cashin {
992ddec63e8SEd L. Cashin 	struct aoe_hdr *hin, *hout;
9931da177e4SLinus Torvalds 	struct aoe_atahdr *ahin, *ahout;
9941da177e4SLinus Torvalds 	struct buf *buf;
995896831f5SEd Cashin 	struct sk_buff *skb;
99668e0d42fSEd L. Cashin 	struct aoetgt *t;
99768e0d42fSEd L. Cashin 	struct aoeif *ifp;
998896831f5SEd Cashin 	struct aoedev *d;
999896831f5SEd Cashin 	long n;
1000896831f5SEd Cashin 
1001896831f5SEd Cashin 	if (f == NULL)
1002896831f5SEd Cashin 		return;
1003896831f5SEd Cashin 
1004896831f5SEd Cashin 	t = f->t;
1005896831f5SEd Cashin 	d = t->d;
1006896831f5SEd Cashin 
1007896831f5SEd Cashin 	hout = (struct aoe_hdr *) skb_mac_header(f->skb);
1008896831f5SEd Cashin 	ahout = (struct aoe_atahdr *) (hout+1);
1009896831f5SEd Cashin 	buf = f->buf;
1010896831f5SEd Cashin 	skb = f->r_skb;
1011896831f5SEd Cashin 	if (skb == NULL)
1012896831f5SEd Cashin 		goto noskb;	/* just fail the buf. */
1013896831f5SEd Cashin 
1014896831f5SEd Cashin 	hin = (struct aoe_hdr *) skb->data;
1015896831f5SEd Cashin 	skb_pull(skb, sizeof(*hin));
1016896831f5SEd Cashin 	ahin = (struct aoe_atahdr *) skb->data;
1017896831f5SEd Cashin 	skb_pull(skb, sizeof(*ahin));
1018896831f5SEd Cashin 	if (ahin->cmdstat & 0xa9) {	/* these bits cleared on success */
1019896831f5SEd Cashin 		pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
1020896831f5SEd Cashin 			ahout->cmdstat, ahin->cmdstat,
1021896831f5SEd Cashin 			d->aoemajor, d->aoeminor);
1022896831f5SEd Cashin noskb:		if (buf)
102369cf2d85SEd Cashin 			clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1024896831f5SEd Cashin 		goto badrsp;
1025896831f5SEd Cashin 	}
1026896831f5SEd Cashin 
1027896831f5SEd Cashin 	n = ahout->scnt << 9;
1028896831f5SEd Cashin 	switch (ahout->cmdstat) {
1029896831f5SEd Cashin 	case ATA_CMD_PIO_READ:
1030896831f5SEd Cashin 	case ATA_CMD_PIO_READ_EXT:
1031896831f5SEd Cashin 		if (skb->len < n) {
1032896831f5SEd Cashin 			pr_err("aoe: runt data size in read.  skb->len=%d need=%ld\n",
1033896831f5SEd Cashin 				skb->len, n);
103469cf2d85SEd Cashin 			clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1035896831f5SEd Cashin 			break;
1036896831f5SEd Cashin 		}
1037896831f5SEd Cashin 		bvcpy(f->bv, f->bv_off, skb, n);
1038896831f5SEd Cashin 	case ATA_CMD_PIO_WRITE:
1039896831f5SEd Cashin 	case ATA_CMD_PIO_WRITE_EXT:
1040896831f5SEd Cashin 		spin_lock_irq(&d->lock);
1041896831f5SEd Cashin 		ifp = getif(t, skb->dev);
10423f0f0133SEd Cashin 		if (ifp)
1043896831f5SEd Cashin 			ifp->lost = 0;
1044896831f5SEd Cashin 		if (d->htgt == t) /* I'll help myself, thank you. */
1045896831f5SEd Cashin 			d->htgt = NULL;
1046896831f5SEd Cashin 		spin_unlock_irq(&d->lock);
1047896831f5SEd Cashin 		break;
1048896831f5SEd Cashin 	case ATA_CMD_ID_ATA:
1049896831f5SEd Cashin 		if (skb->len < 512) {
1050896831f5SEd Cashin 			pr_info("aoe: runt data size in ataid.  skb->len=%d\n",
1051896831f5SEd Cashin 				skb->len);
1052896831f5SEd Cashin 			break;
1053896831f5SEd Cashin 		}
1054896831f5SEd Cashin 		if (skb_linearize(skb))
1055896831f5SEd Cashin 			break;
1056896831f5SEd Cashin 		spin_lock_irq(&d->lock);
1057896831f5SEd Cashin 		ataid_complete(d, t, skb->data);
1058896831f5SEd Cashin 		spin_unlock_irq(&d->lock);
1059896831f5SEd Cashin 		break;
1060896831f5SEd Cashin 	default:
1061896831f5SEd Cashin 		pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
1062896831f5SEd Cashin 			ahout->cmdstat,
1063896831f5SEd Cashin 			be16_to_cpu(get_unaligned(&hin->major)),
1064896831f5SEd Cashin 			hin->minor);
1065896831f5SEd Cashin 	}
1066896831f5SEd Cashin badrsp:
1067896831f5SEd Cashin 	spin_lock_irq(&d->lock);
1068896831f5SEd Cashin 
1069896831f5SEd Cashin 	aoe_freetframe(f);
1070896831f5SEd Cashin 
107169cf2d85SEd Cashin 	if (buf && --buf->nframesout == 0 && buf->resid == 0)
107269cf2d85SEd Cashin 		aoe_end_buf(d, buf);
1073896831f5SEd Cashin 
107469cf2d85SEd Cashin 	aoecmd_work(d);
107569cf2d85SEd Cashin 
1076896831f5SEd Cashin 	spin_unlock_irq(&d->lock);
107769cf2d85SEd Cashin 	aoedev_put(d);
1078896831f5SEd Cashin 	dev_kfree_skb(skb);
1079896831f5SEd Cashin }
1080896831f5SEd Cashin 
1081896831f5SEd Cashin /* Enters with iocq.lock held.
1082896831f5SEd Cashin  * Returns true iff responses needing processing remain.
1083896831f5SEd Cashin  */
1084896831f5SEd Cashin static int
1085896831f5SEd Cashin ktio(void)
1086896831f5SEd Cashin {
1087896831f5SEd Cashin 	struct frame *f;
1088896831f5SEd Cashin 	struct list_head *pos;
1089896831f5SEd Cashin 	int i;
1090896831f5SEd Cashin 
1091896831f5SEd Cashin 	for (i = 0; ; ++i) {
1092896831f5SEd Cashin 		if (i == MAXIOC)
1093896831f5SEd Cashin 			return 1;
1094896831f5SEd Cashin 		if (list_empty(&iocq.head))
1095896831f5SEd Cashin 			return 0;
1096896831f5SEd Cashin 		pos = iocq.head.next;
1097896831f5SEd Cashin 		list_del(pos);
1098896831f5SEd Cashin 		spin_unlock_irq(&iocq.lock);
1099896831f5SEd Cashin 		f = list_entry(pos, struct frame, head);
1100896831f5SEd Cashin 		ktiocomplete(f);
1101896831f5SEd Cashin 		spin_lock_irq(&iocq.lock);
1102896831f5SEd Cashin 	}
1103896831f5SEd Cashin }
1104896831f5SEd Cashin 
1105896831f5SEd Cashin static int
1106896831f5SEd Cashin kthread(void *vp)
1107896831f5SEd Cashin {
1108896831f5SEd Cashin 	struct ktstate *k;
1109896831f5SEd Cashin 	DECLARE_WAITQUEUE(wait, current);
1110896831f5SEd Cashin 	int more;
1111896831f5SEd Cashin 
1112896831f5SEd Cashin 	k = vp;
1113896831f5SEd Cashin 	current->flags |= PF_NOFREEZE;
1114896831f5SEd Cashin 	set_user_nice(current, -10);
1115896831f5SEd Cashin 	complete(&k->rendez);	/* tell spawner we're running */
1116896831f5SEd Cashin 	do {
1117896831f5SEd Cashin 		spin_lock_irq(k->lock);
1118896831f5SEd Cashin 		more = k->fn();
1119896831f5SEd Cashin 		if (!more) {
1120896831f5SEd Cashin 			add_wait_queue(k->waitq, &wait);
1121896831f5SEd Cashin 			__set_current_state(TASK_INTERRUPTIBLE);
1122896831f5SEd Cashin 		}
1123896831f5SEd Cashin 		spin_unlock_irq(k->lock);
1124896831f5SEd Cashin 		if (!more) {
1125896831f5SEd Cashin 			schedule();
1126896831f5SEd Cashin 			remove_wait_queue(k->waitq, &wait);
1127896831f5SEd Cashin 		} else
1128896831f5SEd Cashin 			cond_resched();
1129896831f5SEd Cashin 	} while (!kthread_should_stop());
1130896831f5SEd Cashin 	complete(&k->rendez);	/* tell spawner we're stopping */
1131896831f5SEd Cashin 	return 0;
1132896831f5SEd Cashin }
1133896831f5SEd Cashin 
1134eb086ec5SEd Cashin void
1135896831f5SEd Cashin aoe_ktstop(struct ktstate *k)
1136896831f5SEd Cashin {
1137896831f5SEd Cashin 	kthread_stop(k->task);
1138896831f5SEd Cashin 	wait_for_completion(&k->rendez);
1139896831f5SEd Cashin }
1140896831f5SEd Cashin 
1141eb086ec5SEd Cashin int
1142896831f5SEd Cashin aoe_ktstart(struct ktstate *k)
1143896831f5SEd Cashin {
1144896831f5SEd Cashin 	struct task_struct *task;
1145896831f5SEd Cashin 
1146896831f5SEd Cashin 	init_completion(&k->rendez);
1147896831f5SEd Cashin 	task = kthread_run(kthread, k, k->name);
1148896831f5SEd Cashin 	if (task == NULL || IS_ERR(task))
1149896831f5SEd Cashin 		return -ENOMEM;
1150896831f5SEd Cashin 	k->task = task;
1151896831f5SEd Cashin 	wait_for_completion(&k->rendez); /* allow kthread to start */
1152896831f5SEd Cashin 	init_completion(&k->rendez);	/* for waiting for exit later */
1153896831f5SEd Cashin 	return 0;
1154896831f5SEd Cashin }
1155896831f5SEd Cashin 
1156896831f5SEd Cashin /* pass it off to kthreads for processing */
1157896831f5SEd Cashin static void
1158896831f5SEd Cashin ktcomplete(struct frame *f, struct sk_buff *skb)
1159896831f5SEd Cashin {
1160896831f5SEd Cashin 	ulong flags;
1161896831f5SEd Cashin 
1162896831f5SEd Cashin 	f->r_skb = skb;
1163896831f5SEd Cashin 	spin_lock_irqsave(&iocq.lock, flags);
1164896831f5SEd Cashin 	list_add_tail(&f->head, &iocq.head);
1165896831f5SEd Cashin 	spin_unlock_irqrestore(&iocq.lock, flags);
1166896831f5SEd Cashin 	wake_up(&ktiowq);
1167896831f5SEd Cashin }
1168896831f5SEd Cashin 
1169896831f5SEd Cashin struct sk_buff *
1170896831f5SEd Cashin aoecmd_ata_rsp(struct sk_buff *skb)
1171896831f5SEd Cashin {
1172896831f5SEd Cashin 	struct aoedev *d;
1173896831f5SEd Cashin 	struct aoe_hdr *h;
1174896831f5SEd Cashin 	struct frame *f;
1175896831f5SEd Cashin 	u32 n;
11761da177e4SLinus Torvalds 	ulong flags;
11771da177e4SLinus Torvalds 	char ebuf[128];
117832465c65Secashin@coraid.com 	u16 aoemajor;
11791da177e4SLinus Torvalds 
1180896831f5SEd Cashin 	h = (struct aoe_hdr *) skb->data;
1181896831f5SEd Cashin 	aoemajor = be16_to_cpu(get_unaligned(&h->major));
11820c966214SEd Cashin 	d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
11831da177e4SLinus Torvalds 	if (d == NULL) {
11841da177e4SLinus Torvalds 		snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
11851da177e4SLinus Torvalds 			"for unknown device %d.%d\n",
1186896831f5SEd Cashin 			aoemajor, h->minor);
11871da177e4SLinus Torvalds 		aoechr_error(ebuf);
1188896831f5SEd Cashin 		return skb;
11891da177e4SLinus Torvalds 	}
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 	spin_lock_irqsave(&d->lock, flags);
11921da177e4SLinus Torvalds 
1193896831f5SEd Cashin 	n = be32_to_cpu(get_unaligned(&h->tag));
119464a80f5aSEd Cashin 	f = getframe(d, n);
1195*3a0c40d2SEd Cashin 	if (f) {
1196*3a0c40d2SEd Cashin 		calc_rttavg(d, f->t, tsince(n));
1197*3a0c40d2SEd Cashin 		f->t->nout--;
1198*3a0c40d2SEd Cashin 	} else {
1199*3a0c40d2SEd Cashin 		f = getframe_deferred(d, n);
1200*3a0c40d2SEd Cashin 		if (f) {
1201*3a0c40d2SEd Cashin 			calc_rttavg(d, NULL, tsince(n));
1202*3a0c40d2SEd Cashin 		} else {
1203*3a0c40d2SEd Cashin 			calc_rttavg(d, NULL, tsince(n));
12041da177e4SLinus Torvalds 			spin_unlock_irqrestore(&d->lock, flags);
120569cf2d85SEd Cashin 			aoedev_put(d);
1206*3a0c40d2SEd Cashin 			snprintf(ebuf, sizeof(ebuf),
12071da177e4SLinus Torvalds 				 "%15s e%d.%d    tag=%08x@%08lx\n",
12081da177e4SLinus Torvalds 				 "unexpected rsp",
1209896831f5SEd Cashin 				 get_unaligned_be16(&h->major),
1210896831f5SEd Cashin 				 h->minor,
1211896831f5SEd Cashin 				 get_unaligned_be32(&h->tag),
12121da177e4SLinus Torvalds 				 jiffies);
12131da177e4SLinus Torvalds 			aoechr_error(ebuf);
1214896831f5SEd Cashin 			return skb;
12151da177e4SLinus Torvalds 		}
1216*3a0c40d2SEd Cashin 	}
12171da177e4SLinus Torvalds 	aoecmd_work(d);
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 	spin_unlock_irqrestore(&d->lock, flags);
1220896831f5SEd Cashin 
1221896831f5SEd Cashin 	ktcomplete(f, skb);
1222896831f5SEd Cashin 
1223896831f5SEd Cashin 	/*
1224896831f5SEd Cashin 	 * Note here that we do not perform an aoedev_put, as we are
1225896831f5SEd Cashin 	 * leaving this reference for the ktio to release.
1226896831f5SEd Cashin 	 */
1227896831f5SEd Cashin 	return NULL;
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds void
12311da177e4SLinus Torvalds aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
12321da177e4SLinus Torvalds {
1233e9bb8fb0SDavid S. Miller 	struct sk_buff_head queue;
12341da177e4SLinus Torvalds 
1235e9bb8fb0SDavid S. Miller 	__skb_queue_head_init(&queue);
1236e9bb8fb0SDavid S. Miller 	aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
1237e9bb8fb0SDavid S. Miller 	aoenet_xmit(&queue);
12381da177e4SLinus Torvalds }
12391da177e4SLinus Torvalds 
124068e0d42fSEd L. Cashin struct sk_buff *
12411da177e4SLinus Torvalds aoecmd_ata_id(struct aoedev *d)
12421da177e4SLinus Torvalds {
12431da177e4SLinus Torvalds 	struct aoe_hdr *h;
12441da177e4SLinus Torvalds 	struct aoe_atahdr *ah;
12451da177e4SLinus Torvalds 	struct frame *f;
12461da177e4SLinus Torvalds 	struct sk_buff *skb;
124768e0d42fSEd L. Cashin 	struct aoetgt *t;
12481da177e4SLinus Torvalds 
1249896831f5SEd Cashin 	f = newframe(d);
125068e0d42fSEd L. Cashin 	if (f == NULL)
12511da177e4SLinus Torvalds 		return NULL;
125268e0d42fSEd L. Cashin 
125368e0d42fSEd L. Cashin 	t = *d->tgt;
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 	/* initialize the headers & frame */
1256e407a7f6SEd L. Cashin 	skb = f->skb;
1257abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
12581da177e4SLinus Torvalds 	ah = (struct aoe_atahdr *) (h+1);
125919900cdeSEd L. Cashin 	skb_put(skb, sizeof *h + sizeof *ah);
126019900cdeSEd L. Cashin 	memset(h, 0, skb->len);
126168e0d42fSEd L. Cashin 	f->tag = aoehdr_atainit(d, t, h);
1262896831f5SEd Cashin 	fhash(f);
126368e0d42fSEd L. Cashin 	t->nout++;
12641da177e4SLinus Torvalds 	f->waited = 0;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	/* set up ata header */
12671da177e4SLinus Torvalds 	ah->scnt = 1;
126804b3ab52SBartlomiej Zolnierkiewicz 	ah->cmdstat = ATA_CMD_ID_ATA;
12691da177e4SLinus Torvalds 	ah->lba3 = 0xa0;
12701da177e4SLinus Torvalds 
127168e0d42fSEd L. Cashin 	skb->dev = t->ifp->nd;
12721da177e4SLinus Torvalds 
1273*3a0c40d2SEd Cashin 	d->rttavg = RTTAVG_INIT;
1274*3a0c40d2SEd Cashin 	d->rttdev = RTTDEV_INIT;
12751da177e4SLinus Torvalds 	d->timer.function = rexmit_timer;
12761da177e4SLinus Torvalds 
12774f51dc5eSEd L. Cashin 	return skb_clone(skb, GFP_ATOMIC);
12781da177e4SLinus Torvalds }
12791da177e4SLinus Torvalds 
128068e0d42fSEd L. Cashin static struct aoetgt *
128168e0d42fSEd L. Cashin addtgt(struct aoedev *d, char *addr, ulong nframes)
128268e0d42fSEd L. Cashin {
128368e0d42fSEd L. Cashin 	struct aoetgt *t, **tt, **te;
128468e0d42fSEd L. Cashin 
128568e0d42fSEd L. Cashin 	tt = d->targets;
128668e0d42fSEd L. Cashin 	te = tt + NTARGETS;
128768e0d42fSEd L. Cashin 	for (; tt < te && *tt; tt++)
128868e0d42fSEd L. Cashin 		;
128968e0d42fSEd L. Cashin 
1290578c4aa0SEd L. Cashin 	if (tt == te) {
1291578c4aa0SEd L. Cashin 		printk(KERN_INFO
1292578c4aa0SEd L. Cashin 			"aoe: device addtgt failure; too many targets\n");
129368e0d42fSEd L. Cashin 		return NULL;
1294578c4aa0SEd L. Cashin 	}
1295896831f5SEd Cashin 	t = kzalloc(sizeof(*t), GFP_ATOMIC);
1296896831f5SEd Cashin 	if (!t) {
1297578c4aa0SEd L. Cashin 		printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
12989bb237b6SEd L. Cashin 		return NULL;
12999bb237b6SEd L. Cashin 	}
13009bb237b6SEd L. Cashin 
1301896831f5SEd Cashin 	d->ntargets++;
130268e0d42fSEd L. Cashin 	t->nframes = nframes;
1303896831f5SEd Cashin 	t->d = d;
130468e0d42fSEd L. Cashin 	memcpy(t->addr, addr, sizeof t->addr);
130568e0d42fSEd L. Cashin 	t->ifp = t->ifs;
1306*3a0c40d2SEd Cashin 	aoecmd_wreset(t);
1307896831f5SEd Cashin 	INIT_LIST_HEAD(&t->ffree);
130868e0d42fSEd L. Cashin 	return *tt = t;
130968e0d42fSEd L. Cashin }
131068e0d42fSEd L. Cashin 
13113f0f0133SEd Cashin static void
13123f0f0133SEd Cashin setdbcnt(struct aoedev *d)
13133f0f0133SEd Cashin {
13143f0f0133SEd Cashin 	struct aoetgt **t, **e;
13153f0f0133SEd Cashin 	int bcnt = 0;
13163f0f0133SEd Cashin 
13173f0f0133SEd Cashin 	t = d->targets;
13183f0f0133SEd Cashin 	e = t + NTARGETS;
13193f0f0133SEd Cashin 	for (; t < e && *t; t++)
13203f0f0133SEd Cashin 		if (bcnt == 0 || bcnt > (*t)->minbcnt)
13213f0f0133SEd Cashin 			bcnt = (*t)->minbcnt;
13223f0f0133SEd Cashin 	if (bcnt != d->maxbcnt) {
13233f0f0133SEd Cashin 		d->maxbcnt = bcnt;
13243f0f0133SEd Cashin 		pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
13253f0f0133SEd Cashin 			d->aoemajor, d->aoeminor, bcnt);
13263f0f0133SEd Cashin 	}
13273f0f0133SEd Cashin }
13283f0f0133SEd Cashin 
13293f0f0133SEd Cashin static void
13303f0f0133SEd Cashin setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
13313f0f0133SEd Cashin {
13323f0f0133SEd Cashin 	struct aoedev *d;
13333f0f0133SEd Cashin 	struct aoeif *p, *e;
13343f0f0133SEd Cashin 	int minbcnt;
13353f0f0133SEd Cashin 
13363f0f0133SEd Cashin 	d = t->d;
13373f0f0133SEd Cashin 	minbcnt = bcnt;
13383f0f0133SEd Cashin 	p = t->ifs;
13393f0f0133SEd Cashin 	e = p + NAOEIFS;
13403f0f0133SEd Cashin 	for (; p < e; p++) {
13413f0f0133SEd Cashin 		if (p->nd == NULL)
13423f0f0133SEd Cashin 			break;		/* end of the valid interfaces */
13433f0f0133SEd Cashin 		if (p->nd == nd) {
13443f0f0133SEd Cashin 			p->bcnt = bcnt;	/* we're updating */
13453f0f0133SEd Cashin 			nd = NULL;
13463f0f0133SEd Cashin 		} else if (minbcnt > p->bcnt)
13473f0f0133SEd Cashin 			minbcnt = p->bcnt; /* find the min interface */
13483f0f0133SEd Cashin 	}
13493f0f0133SEd Cashin 	if (nd) {
13503f0f0133SEd Cashin 		if (p == e) {
13513f0f0133SEd Cashin 			pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
13523f0f0133SEd Cashin 			return;
13533f0f0133SEd Cashin 		}
13541b86fda9SEd Cashin 		dev_hold(nd);
13553f0f0133SEd Cashin 		p->nd = nd;
13563f0f0133SEd Cashin 		p->bcnt = bcnt;
13573f0f0133SEd Cashin 	}
13583f0f0133SEd Cashin 	t->minbcnt = minbcnt;
13593f0f0133SEd Cashin 	setdbcnt(d);
13603f0f0133SEd Cashin }
13613f0f0133SEd Cashin 
13621da177e4SLinus Torvalds void
13631da177e4SLinus Torvalds aoecmd_cfg_rsp(struct sk_buff *skb)
13641da177e4SLinus Torvalds {
13651da177e4SLinus Torvalds 	struct aoedev *d;
13661da177e4SLinus Torvalds 	struct aoe_hdr *h;
13671da177e4SLinus Torvalds 	struct aoe_cfghdr *ch;
136868e0d42fSEd L. Cashin 	struct aoetgt *t;
13690c966214SEd Cashin 	ulong flags, aoemajor;
13701da177e4SLinus Torvalds 	struct sk_buff *sl;
137169cf2d85SEd Cashin 	struct sk_buff_head queue;
137219bf2635SEd L. Cashin 	u16 n;
13731da177e4SLinus Torvalds 
137469cf2d85SEd Cashin 	sl = NULL;
1375abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
13761da177e4SLinus Torvalds 	ch = (struct aoe_cfghdr *) (h+1);
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	/*
13791da177e4SLinus Torvalds 	 * Enough people have their dip switches set backwards to
13801da177e4SLinus Torvalds 	 * warrant a loud message for this special case.
13811da177e4SLinus Torvalds 	 */
1382823ed72eSHarvey Harrison 	aoemajor = get_unaligned_be16(&h->major);
13831da177e4SLinus Torvalds 	if (aoemajor == 0xfff) {
1384a12c93f0SEd L. Cashin 		printk(KERN_ERR "aoe: Warning: shelf address is all ones.  "
13856bb6285fSEd L. Cashin 			"Check shelf dip switches.\n");
13861da177e4SLinus Torvalds 		return;
13871da177e4SLinus Torvalds 	}
13887159e969SEd Cashin 	if (aoemajor == 0xffff) {
13897159e969SEd Cashin 		pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
13900c966214SEd Cashin 			aoemajor, (int) h->minor);
13916583303cSEd Cashin 		return;
13926583303cSEd Cashin 	}
13937159e969SEd Cashin 	if (h->minor == 0xff) {
13947159e969SEd Cashin 		pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
13957159e969SEd Cashin 			aoemajor, (int) h->minor);
13961da177e4SLinus Torvalds 		return;
13971da177e4SLinus Torvalds 	}
13981da177e4SLinus Torvalds 
139919bf2635SEd L. Cashin 	n = be16_to_cpu(ch->bufcnt);
14007df620d8SEd L. Cashin 	if (n > aoe_maxout)	/* keep it reasonable */
14017df620d8SEd L. Cashin 		n = aoe_maxout;
14021da177e4SLinus Torvalds 
14037159e969SEd Cashin 	d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
14047159e969SEd Cashin 	if (d == NULL) {
14057159e969SEd Cashin 		pr_info("aoe: device allocation failure\n");
14067159e969SEd Cashin 		return;
14077159e969SEd Cashin 	}
14087159e969SEd Cashin 
14091da177e4SLinus Torvalds 	spin_lock_irqsave(&d->lock, flags);
14101da177e4SLinus Torvalds 
141168e0d42fSEd L. Cashin 	t = gettgt(d, h->src);
14121b8a1636SEd Cashin 	if (t) {
14131b8a1636SEd Cashin 		t->nframes = n;
14141b8a1636SEd Cashin 		if (n < t->maxout)
1415*3a0c40d2SEd Cashin 			aoecmd_wreset(t);
14161b8a1636SEd Cashin 	} else {
141768e0d42fSEd L. Cashin 		t = addtgt(d, h->src, n);
141869cf2d85SEd Cashin 		if (!t)
141969cf2d85SEd Cashin 			goto bail;
142068e0d42fSEd L. Cashin 	}
14213f0f0133SEd Cashin 	n = skb->dev->mtu;
142219bf2635SEd L. Cashin 	n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
142319bf2635SEd L. Cashin 	n /= 512;
142419bf2635SEd L. Cashin 	if (n > ch->scnt)
142519bf2635SEd L. Cashin 		n = ch->scnt;
14264f51dc5eSEd L. Cashin 	n = n ? n * 512 : DEFAULTBCNT;
14273f0f0133SEd Cashin 	setifbcnt(t, skb->dev, n);
14283ae1c24eSEd L. Cashin 
14293ae1c24eSEd L. Cashin 	/* don't change users' perspective */
143069cf2d85SEd Cashin 	if (d->nopen == 0) {
143163e9cc5dSecashin@coraid.com 		d->fw_ver = be16_to_cpu(ch->fwver);
143268e0d42fSEd L. Cashin 		sl = aoecmd_ata_id(d);
143369cf2d85SEd Cashin 	}
143469cf2d85SEd Cashin bail:
14351da177e4SLinus Torvalds 	spin_unlock_irqrestore(&d->lock, flags);
143669cf2d85SEd Cashin 	aoedev_put(d);
1437e9bb8fb0SDavid S. Miller 	if (sl) {
1438e9bb8fb0SDavid S. Miller 		__skb_queue_head_init(&queue);
1439e9bb8fb0SDavid S. Miller 		__skb_queue_tail(&queue, sl);
1440e9bb8fb0SDavid S. Miller 		aoenet_xmit(&queue);
1441e9bb8fb0SDavid S. Miller 	}
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
144468e0d42fSEd L. Cashin void
1445*3a0c40d2SEd Cashin aoecmd_wreset(struct aoetgt *t)
1446*3a0c40d2SEd Cashin {
1447*3a0c40d2SEd Cashin 	t->maxout = 1;
1448*3a0c40d2SEd Cashin 	t->ssthresh = t->nframes / 2;
1449*3a0c40d2SEd Cashin 	t->next_cwnd = t->nframes;
1450*3a0c40d2SEd Cashin }
1451*3a0c40d2SEd Cashin 
1452*3a0c40d2SEd Cashin void
145368e0d42fSEd L. Cashin aoecmd_cleanslate(struct aoedev *d)
145468e0d42fSEd L. Cashin {
145568e0d42fSEd L. Cashin 	struct aoetgt **t, **te;
145668e0d42fSEd L. Cashin 
1457*3a0c40d2SEd Cashin 	d->rttavg = RTTAVG_INIT;
1458*3a0c40d2SEd Cashin 	d->rttdev = RTTDEV_INIT;
14593f0f0133SEd Cashin 	d->maxbcnt = 0;
146068e0d42fSEd L. Cashin 
146168e0d42fSEd L. Cashin 	t = d->targets;
146268e0d42fSEd L. Cashin 	te = t + NTARGETS;
14633f0f0133SEd Cashin 	for (; t < te && *t; t++)
1464*3a0c40d2SEd Cashin 		aoecmd_wreset(*t);
146568e0d42fSEd L. Cashin }
1466896831f5SEd Cashin 
146769cf2d85SEd Cashin void
146869cf2d85SEd Cashin aoe_failbuf(struct aoedev *d, struct buf *buf)
146969cf2d85SEd Cashin {
147069cf2d85SEd Cashin 	if (buf == NULL)
147169cf2d85SEd Cashin 		return;
147269cf2d85SEd Cashin 	buf->resid = 0;
147369cf2d85SEd Cashin 	clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
147469cf2d85SEd Cashin 	if (buf->nframesout == 0)
147569cf2d85SEd Cashin 		aoe_end_buf(d, buf);
147669cf2d85SEd Cashin }
147769cf2d85SEd Cashin 
147869cf2d85SEd Cashin void
147969cf2d85SEd Cashin aoe_flush_iocq(void)
1480896831f5SEd Cashin {
1481896831f5SEd Cashin 	struct frame *f;
1482896831f5SEd Cashin 	struct aoedev *d;
1483896831f5SEd Cashin 	LIST_HEAD(flist);
1484896831f5SEd Cashin 	struct list_head *pos;
1485896831f5SEd Cashin 	struct sk_buff *skb;
1486896831f5SEd Cashin 	ulong flags;
1487896831f5SEd Cashin 
1488896831f5SEd Cashin 	spin_lock_irqsave(&iocq.lock, flags);
1489896831f5SEd Cashin 	list_splice_init(&iocq.head, &flist);
1490896831f5SEd Cashin 	spin_unlock_irqrestore(&iocq.lock, flags);
1491896831f5SEd Cashin 	while (!list_empty(&flist)) {
1492896831f5SEd Cashin 		pos = flist.next;
1493896831f5SEd Cashin 		list_del(pos);
1494896831f5SEd Cashin 		f = list_entry(pos, struct frame, head);
1495896831f5SEd Cashin 		d = f->t->d;
1496896831f5SEd Cashin 		skb = f->r_skb;
1497896831f5SEd Cashin 		spin_lock_irqsave(&d->lock, flags);
1498896831f5SEd Cashin 		if (f->buf) {
1499896831f5SEd Cashin 			f->buf->nframesout--;
1500896831f5SEd Cashin 			aoe_failbuf(d, f->buf);
1501896831f5SEd Cashin 		}
1502896831f5SEd Cashin 		aoe_freetframe(f);
1503896831f5SEd Cashin 		spin_unlock_irqrestore(&d->lock, flags);
1504896831f5SEd Cashin 		dev_kfree_skb(skb);
150569cf2d85SEd Cashin 		aoedev_put(d);
1506896831f5SEd Cashin 	}
1507896831f5SEd Cashin }
1508896831f5SEd Cashin 
1509896831f5SEd Cashin int __init
1510896831f5SEd Cashin aoecmd_init(void)
1511896831f5SEd Cashin {
1512896831f5SEd Cashin 	INIT_LIST_HEAD(&iocq.head);
1513896831f5SEd Cashin 	spin_lock_init(&iocq.lock);
1514896831f5SEd Cashin 	init_waitqueue_head(&ktiowq);
1515896831f5SEd Cashin 	kts.name = "aoe_ktio";
1516896831f5SEd Cashin 	kts.fn = ktio;
1517896831f5SEd Cashin 	kts.waitq = &ktiowq;
1518896831f5SEd Cashin 	kts.lock = &iocq.lock;
1519896831f5SEd Cashin 	return aoe_ktstart(&kts);
1520896831f5SEd Cashin }
1521896831f5SEd Cashin 
1522896831f5SEd Cashin void
1523896831f5SEd Cashin aoecmd_exit(void)
1524896831f5SEd Cashin {
1525896831f5SEd Cashin 	aoe_ktstop(&kts);
152669cf2d85SEd Cashin 	aoe_flush_iocq();
1527896831f5SEd Cashin }
1528