xref: /openbmc/linux/drivers/block/aoe/aoecmd.c (revision 8030d34397e066deecb5ee9d17387fa767b12de2)
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 *);
25bbb44e30SEd Cashin static int count_targets(struct aoedev *d, int *untainted);
26896831f5SEd Cashin 
2769cf2d85SEd Cashin static struct buf *nextbuf(struct aoedev *);
2869cf2d85SEd Cashin 
29b751e8b6SEd L. Cashin static int aoe_deadsecs = 60 * 3;
30b751e8b6SEd L. Cashin module_param(aoe_deadsecs, int, 0644);
31b751e8b6SEd L. Cashin MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
321da177e4SLinus Torvalds 
337b6ccc5fSEd Cashin static int aoe_maxout = 64;
347df620d8SEd L. Cashin module_param(aoe_maxout, int, 0644);
357df620d8SEd L. Cashin MODULE_PARM_DESC(aoe_maxout,
367df620d8SEd L. Cashin 	"Only aoe_maxout outstanding packets for every MAC on eX.Y.");
377df620d8SEd L. Cashin 
38*8030d343SEd Cashin /* The number of online cpus during module initialization gives us a
39*8030d343SEd Cashin  * convenient heuristic cap on the parallelism used for ktio threads
40*8030d343SEd Cashin  * doing I/O completion.  It is not important that the cap equal the
41*8030d343SEd Cashin  * actual number of running CPUs at any given time, but because of CPU
42*8030d343SEd Cashin  * hotplug, we take care to use ncpus instead of using
43*8030d343SEd Cashin  * num_online_cpus() after module initialization.
44*8030d343SEd Cashin  */
45*8030d343SEd Cashin static int ncpus;
46*8030d343SEd Cashin 
47*8030d343SEd Cashin /* mutex lock used for synchronization while thread spawning */
48*8030d343SEd Cashin static DEFINE_MUTEX(ktio_spawn_lock);
49*8030d343SEd Cashin 
50*8030d343SEd Cashin static wait_queue_head_t *ktiowq;
51*8030d343SEd Cashin static struct ktstate *kts;
52896831f5SEd Cashin 
53896831f5SEd Cashin /* io completion queue */
54*8030d343SEd Cashin struct iocq_ktio {
55896831f5SEd Cashin 	struct list_head head;
56896831f5SEd Cashin 	spinlock_t lock;
57*8030d343SEd Cashin };
58*8030d343SEd Cashin static struct iocq_ktio *iocq;
59896831f5SEd Cashin 
60bbb44e30SEd Cashin static struct page *empty_page;
61bbb44e30SEd Cashin 
6268e0d42fSEd L. Cashin static struct sk_buff *
63e407a7f6SEd L. Cashin new_skb(ulong len)
641da177e4SLinus Torvalds {
651da177e4SLinus Torvalds 	struct sk_buff *skb;
661da177e4SLinus Torvalds 
6791c57464SEric Dumazet 	skb = alloc_skb(len + MAX_HEADER, GFP_ATOMIC);
681da177e4SLinus Torvalds 	if (skb) {
6991c57464SEric Dumazet 		skb_reserve(skb, MAX_HEADER);
70459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
71c1d2bbe1SArnaldo Carvalho de Melo 		skb_reset_network_header(skb);
721da177e4SLinus Torvalds 		skb->protocol = __constant_htons(ETH_P_AOE);
738babe8ccSEd Cashin 		skb_checksum_none_assert(skb);
741da177e4SLinus Torvalds 	}
751da177e4SLinus Torvalds 	return skb;
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds static struct frame *
793a0c40d2SEd Cashin getframe_deferred(struct aoedev *d, u32 tag)
803a0c40d2SEd Cashin {
813a0c40d2SEd Cashin 	struct list_head *head, *pos, *nx;
823a0c40d2SEd Cashin 	struct frame *f;
833a0c40d2SEd Cashin 
843a0c40d2SEd Cashin 	head = &d->rexmitq;
853a0c40d2SEd Cashin 	list_for_each_safe(pos, nx, head) {
863a0c40d2SEd Cashin 		f = list_entry(pos, struct frame, head);
873a0c40d2SEd Cashin 		if (f->tag == tag) {
883a0c40d2SEd Cashin 			list_del(pos);
893a0c40d2SEd Cashin 			return f;
903a0c40d2SEd Cashin 		}
913a0c40d2SEd Cashin 	}
923a0c40d2SEd Cashin 	return NULL;
933a0c40d2SEd Cashin }
943a0c40d2SEd Cashin 
953a0c40d2SEd Cashin static struct frame *
9664a80f5aSEd Cashin getframe(struct aoedev *d, u32 tag)
971da177e4SLinus Torvalds {
98896831f5SEd Cashin 	struct frame *f;
99896831f5SEd Cashin 	struct list_head *head, *pos, *nx;
100896831f5SEd Cashin 	u32 n;
1011da177e4SLinus Torvalds 
102896831f5SEd Cashin 	n = tag % NFACTIVE;
10364a80f5aSEd Cashin 	head = &d->factive[n];
104896831f5SEd Cashin 	list_for_each_safe(pos, nx, head) {
105896831f5SEd Cashin 		f = list_entry(pos, struct frame, head);
106896831f5SEd Cashin 		if (f->tag == tag) {
107896831f5SEd Cashin 			list_del(pos);
1081da177e4SLinus Torvalds 			return f;
109896831f5SEd Cashin 		}
110896831f5SEd Cashin 	}
1111da177e4SLinus Torvalds 	return NULL;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds /*
1151da177e4SLinus Torvalds  * Leave the top bit clear so we have tagspace for userland.
1161da177e4SLinus Torvalds  * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
1171da177e4SLinus Torvalds  * This driver reserves tag -1 to mean "unused frame."
1181da177e4SLinus Torvalds  */
1191da177e4SLinus Torvalds static int
12064a80f5aSEd Cashin newtag(struct aoedev *d)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	register ulong n;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	n = jiffies & 0xffff;
12564a80f5aSEd Cashin 	return n |= (++d->lasttag & 0x7fff) << 16;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
128896831f5SEd Cashin static u32
12968e0d42fSEd L. Cashin aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
1301da177e4SLinus Torvalds {
13164a80f5aSEd Cashin 	u32 host_tag = newtag(d);
1321da177e4SLinus Torvalds 
13368e0d42fSEd L. Cashin 	memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
13468e0d42fSEd L. Cashin 	memcpy(h->dst, t->addr, sizeof h->dst);
13563e9cc5dSecashin@coraid.com 	h->type = __constant_cpu_to_be16(ETH_P_AOE);
1361da177e4SLinus Torvalds 	h->verfl = AOE_HVER;
13763e9cc5dSecashin@coraid.com 	h->major = cpu_to_be16(d->aoemajor);
1381da177e4SLinus Torvalds 	h->minor = d->aoeminor;
1391da177e4SLinus Torvalds 	h->cmd = AOECMD_ATA;
14063e9cc5dSecashin@coraid.com 	h->tag = cpu_to_be32(host_tag);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	return host_tag;
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
14519bf2635SEd L. Cashin static inline void
14619bf2635SEd L. Cashin put_lba(struct aoe_atahdr *ah, sector_t lba)
14719bf2635SEd L. Cashin {
14819bf2635SEd L. Cashin 	ah->lba0 = lba;
14919bf2635SEd L. Cashin 	ah->lba1 = lba >>= 8;
15019bf2635SEd L. Cashin 	ah->lba2 = lba >>= 8;
15119bf2635SEd L. Cashin 	ah->lba3 = lba >>= 8;
15219bf2635SEd L. Cashin 	ah->lba4 = lba >>= 8;
15319bf2635SEd L. Cashin 	ah->lba5 = lba >>= 8;
15419bf2635SEd L. Cashin }
15519bf2635SEd L. Cashin 
1563f0f0133SEd Cashin static struct aoeif *
15768e0d42fSEd L. Cashin ifrotate(struct aoetgt *t)
1581da177e4SLinus Torvalds {
1593f0f0133SEd Cashin 	struct aoeif *ifp;
1603f0f0133SEd Cashin 
1613f0f0133SEd Cashin 	ifp = t->ifp;
1623f0f0133SEd Cashin 	ifp++;
1633f0f0133SEd Cashin 	if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL)
1643f0f0133SEd Cashin 		ifp = t->ifs;
1653f0f0133SEd Cashin 	if (ifp->nd == NULL)
1663f0f0133SEd Cashin 		return NULL;
1673f0f0133SEd Cashin 	return t->ifp = ifp;
16868e0d42fSEd L. Cashin }
16968e0d42fSEd L. Cashin 
1709bb237b6SEd L. Cashin static void
1719bb237b6SEd L. Cashin skb_pool_put(struct aoedev *d, struct sk_buff *skb)
1729bb237b6SEd L. Cashin {
173e9bb8fb0SDavid S. Miller 	__skb_queue_tail(&d->skbpool, skb);
1749bb237b6SEd L. Cashin }
1759bb237b6SEd L. Cashin 
1769bb237b6SEd L. Cashin static struct sk_buff *
1779bb237b6SEd L. Cashin skb_pool_get(struct aoedev *d)
1789bb237b6SEd L. Cashin {
179e9bb8fb0SDavid S. Miller 	struct sk_buff *skb = skb_peek(&d->skbpool);
1809bb237b6SEd L. Cashin 
1819bb237b6SEd L. Cashin 	if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
182e9bb8fb0SDavid S. Miller 		__skb_unlink(skb, &d->skbpool);
1839bb237b6SEd L. Cashin 		return skb;
1849bb237b6SEd L. Cashin 	}
185e9bb8fb0SDavid S. Miller 	if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
186e9bb8fb0SDavid S. Miller 	    (skb = new_skb(ETH_ZLEN)))
1879bb237b6SEd L. Cashin 		return skb;
188e9bb8fb0SDavid S. Miller 
1899bb237b6SEd L. Cashin 	return NULL;
1909bb237b6SEd L. Cashin }
1919bb237b6SEd L. Cashin 
192896831f5SEd Cashin void
193896831f5SEd Cashin aoe_freetframe(struct frame *f)
19468e0d42fSEd L. Cashin {
195896831f5SEd Cashin 	struct aoetgt *t;
196896831f5SEd Cashin 
197896831f5SEd Cashin 	t = f->t;
198896831f5SEd Cashin 	f->buf = NULL;
199bbb44e30SEd Cashin 	f->lba = 0;
200896831f5SEd Cashin 	f->bv = NULL;
201896831f5SEd Cashin 	f->r_skb = NULL;
202bbb44e30SEd Cashin 	f->flags = 0;
203896831f5SEd Cashin 	list_add(&f->head, &t->ffree);
204896831f5SEd Cashin }
205896831f5SEd Cashin 
206896831f5SEd Cashin static struct frame *
207896831f5SEd Cashin newtframe(struct aoedev *d, struct aoetgt *t)
208896831f5SEd Cashin {
209896831f5SEd Cashin 	struct frame *f;
2109bb237b6SEd L. Cashin 	struct sk_buff *skb;
211896831f5SEd Cashin 	struct list_head *pos;
212896831f5SEd Cashin 
213896831f5SEd Cashin 	if (list_empty(&t->ffree)) {
214896831f5SEd Cashin 		if (t->falloc >= NSKBPOOLMAX*2)
215896831f5SEd Cashin 			return NULL;
216896831f5SEd Cashin 		f = kcalloc(1, sizeof(*f), GFP_ATOMIC);
217896831f5SEd Cashin 		if (f == NULL)
218896831f5SEd Cashin 			return NULL;
219896831f5SEd Cashin 		t->falloc++;
220896831f5SEd Cashin 		f->t = t;
221896831f5SEd Cashin 	} else {
222896831f5SEd Cashin 		pos = t->ffree.next;
223896831f5SEd Cashin 		list_del(pos);
224896831f5SEd Cashin 		f = list_entry(pos, struct frame, head);
225896831f5SEd Cashin 	}
226896831f5SEd Cashin 
227896831f5SEd Cashin 	skb = f->skb;
228896831f5SEd Cashin 	if (skb == NULL) {
229896831f5SEd Cashin 		f->skb = skb = new_skb(ETH_ZLEN);
230896831f5SEd Cashin 		if (!skb) {
231896831f5SEd Cashin bail:			aoe_freetframe(f);
232896831f5SEd Cashin 			return NULL;
233896831f5SEd Cashin 		}
234896831f5SEd Cashin 	}
235896831f5SEd Cashin 
236896831f5SEd Cashin 	if (atomic_read(&skb_shinfo(skb)->dataref) != 1) {
237896831f5SEd Cashin 		skb = skb_pool_get(d);
238896831f5SEd Cashin 		if (skb == NULL)
239896831f5SEd Cashin 			goto bail;
240896831f5SEd Cashin 		skb_pool_put(d, f->skb);
241896831f5SEd Cashin 		f->skb = skb;
242896831f5SEd Cashin 	}
243896831f5SEd Cashin 
244896831f5SEd Cashin 	skb->truesize -= skb->data_len;
245896831f5SEd Cashin 	skb_shinfo(skb)->nr_frags = skb->data_len = 0;
246896831f5SEd Cashin 	skb_trim(skb, 0);
247896831f5SEd Cashin 	return f;
248896831f5SEd Cashin }
249896831f5SEd Cashin 
250896831f5SEd Cashin static struct frame *
251896831f5SEd Cashin newframe(struct aoedev *d)
252896831f5SEd Cashin {
253896831f5SEd Cashin 	struct frame *f;
254896831f5SEd Cashin 	struct aoetgt *t, **tt;
255896831f5SEd Cashin 	int totout = 0;
256bbb44e30SEd Cashin 	int use_tainted;
257bbb44e30SEd Cashin 	int has_untainted;
25868e0d42fSEd L. Cashin 
25971114ec4SEd Cashin 	if (!d->targets || !d->targets[0]) {
26068e0d42fSEd L. Cashin 		printk(KERN_ERR "aoe: NULL TARGETS!\n");
26168e0d42fSEd L. Cashin 		return NULL;
26268e0d42fSEd L. Cashin 	}
263896831f5SEd Cashin 	tt = d->tgt;	/* last used target */
264bbb44e30SEd Cashin 	for (use_tainted = 0, has_untainted = 0;;) {
265896831f5SEd Cashin 		tt++;
26671114ec4SEd Cashin 		if (tt >= &d->targets[d->ntargets] || !*tt)
267896831f5SEd Cashin 			tt = d->targets;
268896831f5SEd Cashin 		t = *tt;
269bbb44e30SEd Cashin 		if (!t->taint) {
270bbb44e30SEd Cashin 			has_untainted = 1;
271896831f5SEd Cashin 			totout += t->nout;
272bbb44e30SEd Cashin 		}
273896831f5SEd Cashin 		if (t->nout < t->maxout
274bbb44e30SEd Cashin 		&& (use_tainted || !t->taint)
275896831f5SEd Cashin 		&& t->ifp->nd) {
276896831f5SEd Cashin 			f = newtframe(d, t);
277896831f5SEd Cashin 			if (f) {
278896831f5SEd Cashin 				ifrotate(t);
2793f0f0133SEd Cashin 				d->tgt = tt;
28068e0d42fSEd L. Cashin 				return f;
28168e0d42fSEd L. Cashin 			}
2829bb237b6SEd L. Cashin 		}
283bbb44e30SEd Cashin 		if (tt == d->tgt) {	/* we've looped and found nada */
284bbb44e30SEd Cashin 			if (!use_tainted && !has_untainted)
285bbb44e30SEd Cashin 				use_tainted = 1;
286bbb44e30SEd Cashin 			else
2879bb237b6SEd L. Cashin 				break;
288896831f5SEd Cashin 		}
289bbb44e30SEd Cashin 	}
290896831f5SEd Cashin 	if (totout == 0) {
291896831f5SEd Cashin 		d->kicked++;
292896831f5SEd Cashin 		d->flags |= DEVFL_KICKME;
2939bb237b6SEd L. Cashin 	}
29468e0d42fSEd L. Cashin 	return NULL;
29568e0d42fSEd L. Cashin }
29668e0d42fSEd L. Cashin 
2973d5b0605SEd Cashin static void
2983d5b0605SEd Cashin skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt)
2993d5b0605SEd Cashin {
3003d5b0605SEd Cashin 	int frag = 0;
3013d5b0605SEd Cashin 	ulong fcnt;
3023d5b0605SEd Cashin loop:
3033d5b0605SEd Cashin 	fcnt = bv->bv_len - (off - bv->bv_offset);
3043d5b0605SEd Cashin 	if (fcnt > cnt)
3053d5b0605SEd Cashin 		fcnt = cnt;
3063d5b0605SEd Cashin 	skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt);
3073d5b0605SEd Cashin 	cnt -= fcnt;
3083d5b0605SEd Cashin 	if (cnt <= 0)
3093d5b0605SEd Cashin 		return;
3103d5b0605SEd Cashin 	bv++;
3113d5b0605SEd Cashin 	off = bv->bv_offset;
3123d5b0605SEd Cashin 	goto loop;
3133d5b0605SEd Cashin }
3143d5b0605SEd Cashin 
315896831f5SEd Cashin static void
316896831f5SEd Cashin fhash(struct frame *f)
317896831f5SEd Cashin {
31864a80f5aSEd Cashin 	struct aoedev *d = f->t->d;
319896831f5SEd Cashin 	u32 n;
320896831f5SEd Cashin 
321896831f5SEd Cashin 	n = f->tag % NFACTIVE;
32264a80f5aSEd Cashin 	list_add_tail(&f->head, &d->factive[n]);
323896831f5SEd Cashin }
324896831f5SEd Cashin 
325bbb44e30SEd Cashin static void
326bbb44e30SEd Cashin ata_rw_frameinit(struct frame *f)
327bbb44e30SEd Cashin {
328bbb44e30SEd Cashin 	struct aoetgt *t;
329bbb44e30SEd Cashin 	struct aoe_hdr *h;
330bbb44e30SEd Cashin 	struct aoe_atahdr *ah;
331bbb44e30SEd Cashin 	struct sk_buff *skb;
332bbb44e30SEd Cashin 	char writebit, extbit;
333bbb44e30SEd Cashin 
334bbb44e30SEd Cashin 	skb = f->skb;
335bbb44e30SEd Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
336bbb44e30SEd Cashin 	ah = (struct aoe_atahdr *) (h + 1);
337bbb44e30SEd Cashin 	skb_put(skb, sizeof(*h) + sizeof(*ah));
338bbb44e30SEd Cashin 	memset(h, 0, skb->len);
339bbb44e30SEd Cashin 
340bbb44e30SEd Cashin 	writebit = 0x10;
341bbb44e30SEd Cashin 	extbit = 0x4;
342bbb44e30SEd Cashin 
343bbb44e30SEd Cashin 	t = f->t;
344bbb44e30SEd Cashin 	f->tag = aoehdr_atainit(t->d, t, h);
345bbb44e30SEd Cashin 	fhash(f);
346bbb44e30SEd Cashin 	t->nout++;
347bbb44e30SEd Cashin 	f->waited = 0;
348bbb44e30SEd Cashin 	f->waited_total = 0;
349bbb44e30SEd Cashin 	if (f->buf)
350bbb44e30SEd Cashin 		f->lba = f->buf->sector;
351bbb44e30SEd Cashin 
352bbb44e30SEd Cashin 	/* set up ata header */
353bbb44e30SEd Cashin 	ah->scnt = f->bcnt >> 9;
354bbb44e30SEd Cashin 	put_lba(ah, f->lba);
355bbb44e30SEd Cashin 	if (t->d->flags & DEVFL_EXT) {
356bbb44e30SEd Cashin 		ah->aflags |= AOEAFL_EXT;
357bbb44e30SEd Cashin 	} else {
358bbb44e30SEd Cashin 		extbit = 0;
359bbb44e30SEd Cashin 		ah->lba3 &= 0x0f;
360bbb44e30SEd Cashin 		ah->lba3 |= 0xe0;	/* LBA bit + obsolete 0xa0 */
361bbb44e30SEd Cashin 	}
362bbb44e30SEd Cashin 	if (f->buf && bio_data_dir(f->buf->bio) == WRITE) {
363bbb44e30SEd Cashin 		skb_fillup(skb, f->bv, f->bv_off, f->bcnt);
364bbb44e30SEd Cashin 		ah->aflags |= AOEAFL_WRITE;
365bbb44e30SEd Cashin 		skb->len += f->bcnt;
366bbb44e30SEd Cashin 		skb->data_len = f->bcnt;
367bbb44e30SEd Cashin 		skb->truesize += f->bcnt;
368bbb44e30SEd Cashin 		t->wpkts++;
369bbb44e30SEd Cashin 	} else {
370bbb44e30SEd Cashin 		t->rpkts++;
371bbb44e30SEd Cashin 		writebit = 0;
372bbb44e30SEd Cashin 	}
373bbb44e30SEd Cashin 
374bbb44e30SEd Cashin 	ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
375bbb44e30SEd Cashin 	skb->dev = t->ifp->nd;
376bbb44e30SEd Cashin }
377bbb44e30SEd Cashin 
37868e0d42fSEd L. Cashin static int
37968e0d42fSEd L. Cashin aoecmd_ata_rw(struct aoedev *d)
38068e0d42fSEd L. Cashin {
38168e0d42fSEd L. Cashin 	struct frame *f;
3821da177e4SLinus Torvalds 	struct buf *buf;
38368e0d42fSEd L. Cashin 	struct aoetgt *t;
3841da177e4SLinus Torvalds 	struct sk_buff *skb;
38569cf2d85SEd Cashin 	struct sk_buff_head queue;
3863d5b0605SEd Cashin 	ulong bcnt, fbcnt;
3871da177e4SLinus Torvalds 
38869cf2d85SEd Cashin 	buf = nextbuf(d);
38969cf2d85SEd Cashin 	if (buf == NULL)
39069cf2d85SEd Cashin 		return 0;
391896831f5SEd Cashin 	f = newframe(d);
39268e0d42fSEd L. Cashin 	if (f == NULL)
39368e0d42fSEd L. Cashin 		return 0;
39468e0d42fSEd L. Cashin 	t = *d->tgt;
3953f0f0133SEd Cashin 	bcnt = d->maxbcnt;
39668e0d42fSEd L. Cashin 	if (bcnt == 0)
39768e0d42fSEd L. Cashin 		bcnt = DEFAULTBCNT;
3983d5b0605SEd Cashin 	if (bcnt > buf->resid)
3993d5b0605SEd Cashin 		bcnt = buf->resid;
4003d5b0605SEd Cashin 	fbcnt = bcnt;
4013d5b0605SEd Cashin 	f->bv = buf->bv;
4023d5b0605SEd Cashin 	f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
4033d5b0605SEd Cashin 	do {
4043d5b0605SEd Cashin 		if (fbcnt < buf->bv_resid) {
4053d5b0605SEd Cashin 			buf->bv_resid -= fbcnt;
4063d5b0605SEd Cashin 			buf->resid -= fbcnt;
4073d5b0605SEd Cashin 			break;
4083d5b0605SEd Cashin 		}
4093d5b0605SEd Cashin 		fbcnt -= buf->bv_resid;
4103d5b0605SEd Cashin 		buf->resid -= buf->bv_resid;
4113d5b0605SEd Cashin 		if (buf->resid == 0) {
41269cf2d85SEd Cashin 			d->ip.buf = NULL;
4133d5b0605SEd Cashin 			break;
4143d5b0605SEd Cashin 		}
4153d5b0605SEd Cashin 		buf->bv++;
4163d5b0605SEd Cashin 		buf->bv_resid = buf->bv->bv_len;
4173d5b0605SEd Cashin 		WARN_ON(buf->bv_resid == 0);
4183d5b0605SEd Cashin 	} while (fbcnt);
4193d5b0605SEd Cashin 
4201da177e4SLinus Torvalds 	/* initialize the headers & frame */
4211da177e4SLinus Torvalds 	f->buf = buf;
42219bf2635SEd L. Cashin 	f->bcnt = bcnt;
423bbb44e30SEd Cashin 	ata_rw_frameinit(f);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	/* mark all tracking fields and load out */
4261da177e4SLinus Torvalds 	buf->nframesout += 1;
4271da177e4SLinus Torvalds 	buf->sector += bcnt >> 9;
4281da177e4SLinus Torvalds 
429bbb44e30SEd Cashin 	skb = skb_clone(f->skb, GFP_ATOMIC);
43069cf2d85SEd Cashin 	if (skb) {
4315f0c9c48SEd Cashin 		do_gettimeofday(&f->sent);
4325f0c9c48SEd Cashin 		f->sent_jiffs = (u32) jiffies;
43369cf2d85SEd Cashin 		__skb_queue_head_init(&queue);
43469cf2d85SEd Cashin 		__skb_queue_tail(&queue, skb);
43569cf2d85SEd Cashin 		aoenet_xmit(&queue);
43669cf2d85SEd Cashin 	}
43768e0d42fSEd L. Cashin 	return 1;
43868e0d42fSEd L. Cashin }
4391da177e4SLinus Torvalds 
4403ae1c24eSEd L. Cashin /* some callers cannot sleep, and they can call this function,
4413ae1c24eSEd L. Cashin  * transmitting the packets later, when interrupts are on
4423ae1c24eSEd L. Cashin  */
443e9bb8fb0SDavid S. Miller static void
444e9bb8fb0SDavid S. Miller aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
4453ae1c24eSEd L. Cashin {
4463ae1c24eSEd L. Cashin 	struct aoe_hdr *h;
4473ae1c24eSEd L. Cashin 	struct aoe_cfghdr *ch;
448e9bb8fb0SDavid S. Miller 	struct sk_buff *skb;
4493ae1c24eSEd L. Cashin 	struct net_device *ifp;
4503ae1c24eSEd L. Cashin 
451840a185dSEric Dumazet 	rcu_read_lock();
452840a185dSEric Dumazet 	for_each_netdev_rcu(&init_net, ifp) {
4533ae1c24eSEd L. Cashin 		dev_hold(ifp);
4543ae1c24eSEd L. Cashin 		if (!is_aoe_netif(ifp))
4557562f876SPavel Emelianov 			goto cont;
4563ae1c24eSEd L. Cashin 
457e407a7f6SEd L. Cashin 		skb = new_skb(sizeof *h + sizeof *ch);
4583ae1c24eSEd L. Cashin 		if (skb == NULL) {
459a12c93f0SEd L. Cashin 			printk(KERN_INFO "aoe: skb alloc failure\n");
4607562f876SPavel Emelianov 			goto cont;
4613ae1c24eSEd L. Cashin 		}
46219900cdeSEd L. Cashin 		skb_put(skb, sizeof *h + sizeof *ch);
463e407a7f6SEd L. Cashin 		skb->dev = ifp;
464e9bb8fb0SDavid S. Miller 		__skb_queue_tail(queue, skb);
465abdbf94dSEd L. Cashin 		h = (struct aoe_hdr *) skb_mac_header(skb);
4663ae1c24eSEd L. Cashin 		memset(h, 0, sizeof *h + sizeof *ch);
4673ae1c24eSEd L. Cashin 
4683ae1c24eSEd L. Cashin 		memset(h->dst, 0xff, sizeof h->dst);
4693ae1c24eSEd L. Cashin 		memcpy(h->src, ifp->dev_addr, sizeof h->src);
4703ae1c24eSEd L. Cashin 		h->type = __constant_cpu_to_be16(ETH_P_AOE);
4713ae1c24eSEd L. Cashin 		h->verfl = AOE_HVER;
4723ae1c24eSEd L. Cashin 		h->major = cpu_to_be16(aoemajor);
4733ae1c24eSEd L. Cashin 		h->minor = aoeminor;
4743ae1c24eSEd L. Cashin 		h->cmd = AOECMD_CFG;
4753ae1c24eSEd L. Cashin 
4767562f876SPavel Emelianov cont:
4777562f876SPavel Emelianov 		dev_put(ifp);
4783ae1c24eSEd L. Cashin 	}
479840a185dSEric Dumazet 	rcu_read_unlock();
4803ae1c24eSEd L. Cashin }
4813ae1c24eSEd L. Cashin 
4821da177e4SLinus Torvalds static void
483896831f5SEd Cashin resend(struct aoedev *d, struct frame *f)
4841da177e4SLinus Torvalds {
4851da177e4SLinus Torvalds 	struct sk_buff *skb;
48669cf2d85SEd Cashin 	struct sk_buff_head queue;
4871da177e4SLinus Torvalds 	struct aoe_hdr *h;
48819bf2635SEd L. Cashin 	struct aoe_atahdr *ah;
489896831f5SEd Cashin 	struct aoetgt *t;
4901da177e4SLinus Torvalds 	char buf[128];
4911da177e4SLinus Torvalds 	u32 n;
4921da177e4SLinus Torvalds 
493896831f5SEd Cashin 	t = f->t;
49464a80f5aSEd Cashin 	n = newtag(d);
495e407a7f6SEd L. Cashin 	skb = f->skb;
4963f0f0133SEd Cashin 	if (ifrotate(t) == NULL) {
4973f0f0133SEd Cashin 		/* probably can't happen, but set it up to fail anyway */
4983f0f0133SEd Cashin 		pr_info("aoe: resend: no interfaces to rotate to.\n");
4993f0f0133SEd Cashin 		ktcomplete(f, NULL);
5003f0f0133SEd Cashin 		return;
5013f0f0133SEd Cashin 	}
502abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
50319bf2635SEd L. Cashin 	ah = (struct aoe_atahdr *) (h+1);
50468e0d42fSEd L. Cashin 
505bbb44e30SEd Cashin 	if (!(f->flags & FFL_PROBE)) {
506bbb44e30SEd Cashin 		snprintf(buf, sizeof(buf),
507411c41eeSHarvey Harrison 			"%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
508bbb44e30SEd Cashin 			"retransmit", d->aoemajor, d->aoeminor,
509bbb44e30SEd Cashin 			f->tag, jiffies, n,
510411c41eeSHarvey Harrison 			h->src, h->dst, t->nout);
51168e0d42fSEd L. Cashin 		aoechr_error(buf);
512bbb44e30SEd Cashin 	}
51368e0d42fSEd L. Cashin 
5141da177e4SLinus Torvalds 	f->tag = n;
515896831f5SEd Cashin 	fhash(f);
51663e9cc5dSecashin@coraid.com 	h->tag = cpu_to_be32(n);
51768e0d42fSEd L. Cashin 	memcpy(h->dst, t->addr, sizeof h->dst);
51868e0d42fSEd L. Cashin 	memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
5191da177e4SLinus Torvalds 
52068e0d42fSEd L. Cashin 	skb->dev = t->ifp->nd;
5214f51dc5eSEd L. Cashin 	skb = skb_clone(skb, GFP_ATOMIC);
5224f51dc5eSEd L. Cashin 	if (skb == NULL)
5234f51dc5eSEd L. Cashin 		return;
5245f0c9c48SEd Cashin 	do_gettimeofday(&f->sent);
5255f0c9c48SEd Cashin 	f->sent_jiffs = (u32) jiffies;
52669cf2d85SEd Cashin 	__skb_queue_head_init(&queue);
52769cf2d85SEd Cashin 	__skb_queue_tail(&queue, skb);
52869cf2d85SEd Cashin 	aoenet_xmit(&queue);
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds static int
5325f0c9c48SEd Cashin tsince_hr(struct frame *f)
5335f0c9c48SEd Cashin {
5345f0c9c48SEd Cashin 	struct timeval now;
5355f0c9c48SEd Cashin 	int n;
5365f0c9c48SEd Cashin 
5375f0c9c48SEd Cashin 	do_gettimeofday(&now);
5385f0c9c48SEd Cashin 	n = now.tv_usec - f->sent.tv_usec;
5395f0c9c48SEd Cashin 	n += (now.tv_sec - f->sent.tv_sec) * USEC_PER_SEC;
5405f0c9c48SEd Cashin 
5415f0c9c48SEd Cashin 	if (n < 0)
5425f0c9c48SEd Cashin 		n = -n;
5435f0c9c48SEd Cashin 
5445f0c9c48SEd Cashin 	/* For relatively long periods, use jiffies to avoid
5455f0c9c48SEd Cashin 	 * discrepancies caused by updates to the system time.
5465f0c9c48SEd Cashin 	 *
5475f0c9c48SEd Cashin 	 * On system with HZ of 1000, 32-bits is over 49 days
5485f0c9c48SEd Cashin 	 * worth of jiffies, or over 71 minutes worth of usecs.
5495f0c9c48SEd Cashin 	 *
5505f0c9c48SEd Cashin 	 * Jiffies overflow is handled by subtraction of unsigned ints:
5515f0c9c48SEd Cashin 	 * (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe
5525f0c9c48SEd Cashin 	 * $3 = 4
5535f0c9c48SEd Cashin 	 * (gdb)
5545f0c9c48SEd Cashin 	 */
5555f0c9c48SEd Cashin 	if (n > USEC_PER_SEC / 4) {
5565f0c9c48SEd Cashin 		n = ((u32) jiffies) - f->sent_jiffs;
5575f0c9c48SEd Cashin 		n *= USEC_PER_SEC / HZ;
5585f0c9c48SEd Cashin 	}
5595f0c9c48SEd Cashin 
5605f0c9c48SEd Cashin 	return n;
5615f0c9c48SEd Cashin }
5625f0c9c48SEd Cashin 
5635f0c9c48SEd Cashin static int
564896831f5SEd Cashin tsince(u32 tag)
5651da177e4SLinus Torvalds {
5661da177e4SLinus Torvalds 	int n;
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	n = jiffies & 0xffff;
5691da177e4SLinus Torvalds 	n -= tag & 0xffff;
5701da177e4SLinus Torvalds 	if (n < 0)
5711da177e4SLinus Torvalds 		n += 1<<16;
5725f0c9c48SEd Cashin 	return jiffies_to_usecs(n + 1);
5731da177e4SLinus Torvalds }
5741da177e4SLinus Torvalds 
57568e0d42fSEd L. Cashin static struct aoeif *
57668e0d42fSEd L. Cashin getif(struct aoetgt *t, struct net_device *nd)
57768e0d42fSEd L. Cashin {
57868e0d42fSEd L. Cashin 	struct aoeif *p, *e;
57968e0d42fSEd L. Cashin 
58068e0d42fSEd L. Cashin 	p = t->ifs;
58168e0d42fSEd L. Cashin 	e = p + NAOEIFS;
58268e0d42fSEd L. Cashin 	for (; p < e; p++)
58368e0d42fSEd L. Cashin 		if (p->nd == nd)
58468e0d42fSEd L. Cashin 			return p;
58568e0d42fSEd L. Cashin 	return NULL;
58668e0d42fSEd L. Cashin }
58768e0d42fSEd L. Cashin 
58868e0d42fSEd L. Cashin static void
58968e0d42fSEd L. Cashin ejectif(struct aoetgt *t, struct aoeif *ifp)
59068e0d42fSEd L. Cashin {
59168e0d42fSEd L. Cashin 	struct aoeif *e;
5921b86fda9SEd Cashin 	struct net_device *nd;
59368e0d42fSEd L. Cashin 	ulong n;
59468e0d42fSEd L. Cashin 
5951b86fda9SEd Cashin 	nd = ifp->nd;
59668e0d42fSEd L. Cashin 	e = t->ifs + NAOEIFS - 1;
59768e0d42fSEd L. Cashin 	n = (e - ifp) * sizeof *ifp;
59868e0d42fSEd L. Cashin 	memmove(ifp, ifp+1, n);
59968e0d42fSEd L. Cashin 	e->nd = NULL;
6001b86fda9SEd Cashin 	dev_put(nd);
60168e0d42fSEd L. Cashin }
60268e0d42fSEd L. Cashin 
6033fc9b032SEd Cashin static struct frame *
604bbb44e30SEd Cashin reassign_frame(struct frame *f)
60568e0d42fSEd L. Cashin {
6063fc9b032SEd Cashin 	struct frame *nf;
60768e0d42fSEd L. Cashin 	struct sk_buff *skb;
60868e0d42fSEd L. Cashin 
6093fc9b032SEd Cashin 	nf = newframe(f->t->d);
61068e0d42fSEd L. Cashin 	if (!nf)
6113fc9b032SEd Cashin 		return NULL;
612bbb44e30SEd Cashin 	if (nf->t == f->t) {
613bbb44e30SEd Cashin 		aoe_freetframe(nf);
614bbb44e30SEd Cashin 		return NULL;
615bbb44e30SEd Cashin 	}
616896831f5SEd Cashin 
61768e0d42fSEd L. Cashin 	skb = nf->skb;
618896831f5SEd Cashin 	nf->skb = f->skb;
619896831f5SEd Cashin 	nf->buf = f->buf;
620896831f5SEd Cashin 	nf->bcnt = f->bcnt;
621896831f5SEd Cashin 	nf->lba = f->lba;
622896831f5SEd Cashin 	nf->bv = f->bv;
623896831f5SEd Cashin 	nf->bv_off = f->bv_off;
62468e0d42fSEd L. Cashin 	nf->waited = 0;
6253fc9b032SEd Cashin 	nf->waited_total = f->waited_total;
6263fc9b032SEd Cashin 	nf->sent = f->sent;
627fe7252bfSEd Cashin 	nf->sent_jiffs = f->sent_jiffs;
628896831f5SEd Cashin 	f->skb = skb;
6293fc9b032SEd Cashin 
6303fc9b032SEd Cashin 	return nf;
6313fc9b032SEd Cashin }
6323fc9b032SEd Cashin 
633bbb44e30SEd Cashin static void
634bbb44e30SEd Cashin probe(struct aoetgt *t)
6353fc9b032SEd Cashin {
636bbb44e30SEd Cashin 	struct aoedev *d;
637bbb44e30SEd Cashin 	struct frame *f;
638bbb44e30SEd Cashin 	struct sk_buff *skb;
639bbb44e30SEd Cashin 	struct sk_buff_head queue;
640bbb44e30SEd Cashin 	size_t n, m;
641bbb44e30SEd Cashin 	int frag;
6423fc9b032SEd Cashin 
643bbb44e30SEd Cashin 	d = t->d;
644bbb44e30SEd Cashin 	f = newtframe(d, t);
645bbb44e30SEd Cashin 	if (!f) {
646bbb44e30SEd Cashin 		pr_err("%s %pm for e%ld.%d: %s\n",
647bbb44e30SEd Cashin 			"aoe: cannot probe remote address",
648bbb44e30SEd Cashin 			t->addr,
649bbb44e30SEd Cashin 			(long) d->aoemajor, d->aoeminor,
650bbb44e30SEd Cashin 			"no frame available");
651bbb44e30SEd Cashin 		return;
652bbb44e30SEd Cashin 	}
653bbb44e30SEd Cashin 	f->flags |= FFL_PROBE;
654bbb44e30SEd Cashin 	ifrotate(t);
655bbb44e30SEd Cashin 	f->bcnt = t->d->maxbcnt ? t->d->maxbcnt : DEFAULTBCNT;
656bbb44e30SEd Cashin 	ata_rw_frameinit(f);
657bbb44e30SEd Cashin 	skb = f->skb;
658bbb44e30SEd Cashin 	for (frag = 0, n = f->bcnt; n > 0; ++frag, n -= m) {
659bbb44e30SEd Cashin 		if (n < PAGE_SIZE)
660bbb44e30SEd Cashin 			m = n;
661bbb44e30SEd Cashin 		else
662bbb44e30SEd Cashin 			m = PAGE_SIZE;
663bbb44e30SEd Cashin 		skb_fill_page_desc(skb, frag, empty_page, 0, m);
664bbb44e30SEd Cashin 	}
665bbb44e30SEd Cashin 	skb->len += f->bcnt;
666bbb44e30SEd Cashin 	skb->data_len = f->bcnt;
667bbb44e30SEd Cashin 	skb->truesize += f->bcnt;
668bbb44e30SEd Cashin 
669bbb44e30SEd Cashin 	skb = skb_clone(f->skb, GFP_ATOMIC);
670bbb44e30SEd Cashin 	if (skb) {
671bbb44e30SEd Cashin 		do_gettimeofday(&f->sent);
672bbb44e30SEd Cashin 		f->sent_jiffs = (u32) jiffies;
673bbb44e30SEd Cashin 		__skb_queue_head_init(&queue);
674bbb44e30SEd Cashin 		__skb_queue_tail(&queue, skb);
675bbb44e30SEd Cashin 		aoenet_xmit(&queue);
676896831f5SEd Cashin 	}
67768e0d42fSEd L. Cashin }
678bbb44e30SEd Cashin 
679bbb44e30SEd Cashin static long
680bbb44e30SEd Cashin rto(struct aoedev *d)
681bbb44e30SEd Cashin {
682bbb44e30SEd Cashin 	long t;
683bbb44e30SEd Cashin 
684bbb44e30SEd Cashin 	t = 2 * d->rttavg >> RTTSCALE;
685bbb44e30SEd Cashin 	t += 8 * d->rttdev >> RTTDSCALE;
686bbb44e30SEd Cashin 	if (t == 0)
687bbb44e30SEd Cashin 		t = 1;
688bbb44e30SEd Cashin 
689bbb44e30SEd Cashin 	return t;
69068e0d42fSEd L. Cashin }
69168e0d42fSEd L. Cashin 
6921da177e4SLinus Torvalds static void
6933a0c40d2SEd Cashin rexmit_deferred(struct aoedev *d)
6943a0c40d2SEd Cashin {
6953a0c40d2SEd Cashin 	struct aoetgt *t;
6963a0c40d2SEd Cashin 	struct frame *f;
697bbb44e30SEd Cashin 	struct frame *nf;
6983a0c40d2SEd Cashin 	struct list_head *pos, *nx, *head;
6993fc9b032SEd Cashin 	int since;
700bbb44e30SEd Cashin 	int untainted;
701bbb44e30SEd Cashin 
702bbb44e30SEd Cashin 	count_targets(d, &untainted);
7033a0c40d2SEd Cashin 
7043a0c40d2SEd Cashin 	head = &d->rexmitq;
7053a0c40d2SEd Cashin 	list_for_each_safe(pos, nx, head) {
7063a0c40d2SEd Cashin 		f = list_entry(pos, struct frame, head);
7073a0c40d2SEd Cashin 		t = f->t;
708bbb44e30SEd Cashin 		if (t->taint) {
709bbb44e30SEd Cashin 			if (!(f->flags & FFL_PROBE)) {
710bbb44e30SEd Cashin 				nf = reassign_frame(f);
711bbb44e30SEd Cashin 				if (nf) {
712bbb44e30SEd Cashin 					if (t->nout_probes == 0
713bbb44e30SEd Cashin 					&& untainted > 0) {
714bbb44e30SEd Cashin 						probe(t);
715bbb44e30SEd Cashin 						t->nout_probes++;
716bbb44e30SEd Cashin 					}
717bbb44e30SEd Cashin 					list_replace(&f->head, &nf->head);
718bbb44e30SEd Cashin 					pos = &nf->head;
719bbb44e30SEd Cashin 					aoe_freetframe(f);
720bbb44e30SEd Cashin 					f = nf;
721bbb44e30SEd Cashin 					t = f->t;
722bbb44e30SEd Cashin 				}
723bbb44e30SEd Cashin 			} else if (untainted < 1) {
724bbb44e30SEd Cashin 				/* don't probe w/o other untainted aoetgts */
725bbb44e30SEd Cashin 				goto stop_probe;
726bbb44e30SEd Cashin 			} else if (tsince_hr(f) < t->taint * rto(d)) {
727bbb44e30SEd Cashin 				/* reprobe slowly when taint is high */
728bbb44e30SEd Cashin 				continue;
729bbb44e30SEd Cashin 			}
730bbb44e30SEd Cashin 		} else if (f->flags & FFL_PROBE) {
731bbb44e30SEd Cashin stop_probe:		/* don't probe untainted aoetgts */
732bbb44e30SEd Cashin 			list_del(pos);
733bbb44e30SEd Cashin 			aoe_freetframe(f);
734bbb44e30SEd Cashin 			/* leaving d->kicked, because this is routine */
735bbb44e30SEd Cashin 			f->t->d->flags |= DEVFL_KICKME;
736bbb44e30SEd Cashin 			continue;
737bbb44e30SEd Cashin 		}
7383a0c40d2SEd Cashin 		if (t->nout >= t->maxout)
7393a0c40d2SEd Cashin 			continue;
7403a0c40d2SEd Cashin 		list_del(pos);
7413a0c40d2SEd Cashin 		t->nout++;
742bbb44e30SEd Cashin 		if (f->flags & FFL_PROBE)
743bbb44e30SEd Cashin 			t->nout_probes++;
7443fc9b032SEd Cashin 		since = tsince_hr(f);
7453fc9b032SEd Cashin 		f->waited += since;
7463fc9b032SEd Cashin 		f->waited_total += since;
7473a0c40d2SEd Cashin 		resend(d, f);
7483a0c40d2SEd Cashin 	}
7493a0c40d2SEd Cashin }
7503a0c40d2SEd Cashin 
751bbb44e30SEd Cashin /* An aoetgt accumulates demerits quickly, and successful
752bbb44e30SEd Cashin  * probing redeems the aoetgt slowly.
753bbb44e30SEd Cashin  */
754bbb44e30SEd Cashin static void
755bbb44e30SEd Cashin scorn(struct aoetgt *t)
756bbb44e30SEd Cashin {
757bbb44e30SEd Cashin 	int n;
758bbb44e30SEd Cashin 
759bbb44e30SEd Cashin 	n = t->taint++;
760bbb44e30SEd Cashin 	t->taint += t->taint * 2;
761bbb44e30SEd Cashin 	if (n > t->taint)
762bbb44e30SEd Cashin 		t->taint = n;
763bbb44e30SEd Cashin 	if (t->taint > MAX_TAINT)
764bbb44e30SEd Cashin 		t->taint = MAX_TAINT;
765bbb44e30SEd Cashin }
766bbb44e30SEd Cashin 
767bbb44e30SEd Cashin static int
768bbb44e30SEd Cashin count_targets(struct aoedev *d, int *untainted)
769bbb44e30SEd Cashin {
770bbb44e30SEd Cashin 	int i, good;
771bbb44e30SEd Cashin 
772bbb44e30SEd Cashin 	for (i = good = 0; i < d->ntargets && d->targets[i]; ++i)
773bbb44e30SEd Cashin 		if (d->targets[i]->taint == 0)
774bbb44e30SEd Cashin 			good++;
775bbb44e30SEd Cashin 
776bbb44e30SEd Cashin 	if (untainted)
777bbb44e30SEd Cashin 		*untainted = good;
778bbb44e30SEd Cashin 	return i;
779bbb44e30SEd Cashin }
780bbb44e30SEd Cashin 
7813a0c40d2SEd Cashin static void
7821da177e4SLinus Torvalds rexmit_timer(ulong vp)
7831da177e4SLinus Torvalds {
7841da177e4SLinus Torvalds 	struct aoedev *d;
7853a0c40d2SEd Cashin 	struct aoetgt *t;
78668e0d42fSEd L. Cashin 	struct aoeif *ifp;
787896831f5SEd Cashin 	struct frame *f;
788896831f5SEd Cashin 	struct list_head *head, *pos, *nx;
789896831f5SEd Cashin 	LIST_HEAD(flist);
7901da177e4SLinus Torvalds 	register long timeout;
7911da177e4SLinus Torvalds 	ulong flags, n;
792896831f5SEd Cashin 	int i;
793bbb44e30SEd Cashin 	int utgts;	/* number of aoetgt descriptors (not slots) */
7943fc9b032SEd Cashin 	int since;
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	d = (struct aoedev *) vp;
7971da177e4SLinus Torvalds 
7980d555ecfSEd Cashin 	spin_lock_irqsave(&d->lock, flags);
7990d555ecfSEd Cashin 
8003a0c40d2SEd Cashin 	/* timeout based on observed timings and variations */
801bbb44e30SEd Cashin 	timeout = rto(d);
802bbb44e30SEd Cashin 
803bbb44e30SEd Cashin 	utgts = count_targets(d, NULL);
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	if (d->flags & DEVFL_TKILL) {
8061c6f3fcaSEd L. Cashin 		spin_unlock_irqrestore(&d->lock, flags);
8071da177e4SLinus Torvalds 		return;
8081da177e4SLinus Torvalds 	}
809896831f5SEd Cashin 
810896831f5SEd Cashin 	/* collect all frames to rexmit into flist */
811896831f5SEd Cashin 	for (i = 0; i < NFACTIVE; i++) {
81264a80f5aSEd Cashin 		head = &d->factive[i];
813896831f5SEd Cashin 		list_for_each_safe(pos, nx, head) {
814896831f5SEd Cashin 			f = list_entry(pos, struct frame, head);
8155f0c9c48SEd Cashin 			if (tsince_hr(f) < timeout)
81664a80f5aSEd Cashin 				break;	/* end of expired frames */
817896831f5SEd Cashin 			/* move to flist for later processing */
818896831f5SEd Cashin 			list_move_tail(pos, &flist);
819896831f5SEd Cashin 		}
820896831f5SEd Cashin 	}
82169cf2d85SEd Cashin 
822896831f5SEd Cashin 	/* process expired frames */
823896831f5SEd Cashin 	while (!list_empty(&flist)) {
824896831f5SEd Cashin 		pos = flist.next;
825896831f5SEd Cashin 		f = list_entry(pos, struct frame, head);
8263fc9b032SEd Cashin 		since = tsince_hr(f);
8273fc9b032SEd Cashin 		n = f->waited_total + since;
8285f0c9c48SEd Cashin 		n /= USEC_PER_SEC;
829c450ba0fSEd Cashin 		if (aoe_deadsecs
830c450ba0fSEd Cashin 		&& n > aoe_deadsecs
831c450ba0fSEd Cashin 		&& !(f->flags & FFL_PROBE)) {
832896831f5SEd Cashin 			/* Waited too long.  Device failure.
833896831f5SEd Cashin 			 * Hang all frames on first hash bucket for downdev
834896831f5SEd Cashin 			 * to clean up.
835896831f5SEd Cashin 			 */
83664a80f5aSEd Cashin 			list_splice(&flist, &d->factive[0]);
8371da177e4SLinus Torvalds 			aoedev_downdev(d);
8383a0c40d2SEd Cashin 			goto out;
8391da177e4SLinus Torvalds 		}
84068e0d42fSEd L. Cashin 
841896831f5SEd Cashin 		t = f->t;
842bbb44e30SEd Cashin 		n = f->waited + since;
843bbb44e30SEd Cashin 		n /= USEC_PER_SEC;
844bbb44e30SEd Cashin 		if (aoe_deadsecs && utgts > 0
845bbb44e30SEd Cashin 		&& (n > aoe_deadsecs / utgts || n > HARD_SCORN_SECS))
846bbb44e30SEd Cashin 			scorn(t); /* avoid this target */
847d54d35acSEd Cashin 
8483a0c40d2SEd Cashin 		if (t->maxout != 1) {
8493a0c40d2SEd Cashin 			t->ssthresh = t->maxout / 2;
8503a0c40d2SEd Cashin 			t->maxout = 1;
85168e0d42fSEd L. Cashin 		}
85268e0d42fSEd L. Cashin 
853bbb44e30SEd Cashin 		if (f->flags & FFL_PROBE) {
854bbb44e30SEd Cashin 			t->nout_probes--;
855bbb44e30SEd Cashin 		} else {
85668e0d42fSEd L. Cashin 			ifp = getif(t, f->skb->dev);
85768e0d42fSEd L. Cashin 			if (ifp && ++ifp->lost > (t->nframes << 1)
85868e0d42fSEd L. Cashin 			&& (ifp != t->ifs || t->ifs[1].nd)) {
85968e0d42fSEd L. Cashin 				ejectif(t, ifp);
86068e0d42fSEd L. Cashin 				ifp = NULL;
86168e0d42fSEd L. Cashin 			}
862bbb44e30SEd Cashin 		}
8633a0c40d2SEd Cashin 		list_move_tail(pos, &d->rexmitq);
8643a0c40d2SEd Cashin 		t->nout--;
8651da177e4SLinus Torvalds 	}
8663a0c40d2SEd Cashin 	rexmit_deferred(d);
86768e0d42fSEd L. Cashin 
8683a0c40d2SEd Cashin out:
869bbb44e30SEd Cashin 	if ((d->flags & DEVFL_KICKME) && d->blkq) {
8704f51dc5eSEd L. Cashin 		d->flags &= ~DEVFL_KICKME;
87169cf2d85SEd Cashin 		d->blkq->request_fn(d->blkq);
8724f51dc5eSEd L. Cashin 	}
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	d->timer.expires = jiffies + TIMERTICK;
8751da177e4SLinus Torvalds 	add_timer(&d->timer);
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	spin_unlock_irqrestore(&d->lock, flags);
87869cf2d85SEd Cashin }
8791da177e4SLinus Torvalds 
88069cf2d85SEd Cashin static unsigned long
88169cf2d85SEd Cashin rqbiocnt(struct request *r)
88269cf2d85SEd Cashin {
88369cf2d85SEd Cashin 	struct bio *bio;
88469cf2d85SEd Cashin 	unsigned long n = 0;
88569cf2d85SEd Cashin 
88669cf2d85SEd Cashin 	__rq_for_each_bio(bio, r)
88769cf2d85SEd Cashin 		n++;
88869cf2d85SEd Cashin 	return n;
88969cf2d85SEd Cashin }
89069cf2d85SEd Cashin 
89169cf2d85SEd Cashin /* This can be removed if we are certain that no users of the block
89269cf2d85SEd Cashin  * layer will ever use zero-count pages in bios.  Otherwise we have to
89369cf2d85SEd Cashin  * protect against the put_page sometimes done by the network layer.
89469cf2d85SEd Cashin  *
89569cf2d85SEd Cashin  * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
89669cf2d85SEd Cashin  * discussion.
89769cf2d85SEd Cashin  *
89869cf2d85SEd Cashin  * We cannot use get_page in the workaround, because it insists on a
89969cf2d85SEd Cashin  * positive page count as a precondition.  So we use _count directly.
90069cf2d85SEd Cashin  */
90169cf2d85SEd Cashin static void
90269cf2d85SEd Cashin bio_pageinc(struct bio *bio)
90369cf2d85SEd Cashin {
90469cf2d85SEd Cashin 	struct bio_vec *bv;
90569cf2d85SEd Cashin 	struct page *page;
90669cf2d85SEd Cashin 	int i;
90769cf2d85SEd Cashin 
90869cf2d85SEd Cashin 	bio_for_each_segment(bv, bio, i) {
90969cf2d85SEd Cashin 		page = bv->bv_page;
91069cf2d85SEd Cashin 		/* Non-zero page count for non-head members of
91169cf2d85SEd Cashin 		 * compound pages is no longer allowed by the kernel,
91269cf2d85SEd Cashin 		 * but this has never been seen here.
91369cf2d85SEd Cashin 		 */
91469cf2d85SEd Cashin 		if (unlikely(PageCompound(page)))
91569cf2d85SEd Cashin 			if (compound_trans_head(page) != page) {
91669cf2d85SEd Cashin 				pr_crit("page tail used for block I/O\n");
91769cf2d85SEd Cashin 				BUG();
91869cf2d85SEd Cashin 			}
91969cf2d85SEd Cashin 		atomic_inc(&page->_count);
92069cf2d85SEd Cashin 	}
92169cf2d85SEd Cashin }
92269cf2d85SEd Cashin 
92369cf2d85SEd Cashin static void
92469cf2d85SEd Cashin bio_pagedec(struct bio *bio)
92569cf2d85SEd Cashin {
92669cf2d85SEd Cashin 	struct bio_vec *bv;
92769cf2d85SEd Cashin 	int i;
92869cf2d85SEd Cashin 
92969cf2d85SEd Cashin 	bio_for_each_segment(bv, bio, i)
93069cf2d85SEd Cashin 		atomic_dec(&bv->bv_page->_count);
93169cf2d85SEd Cashin }
93269cf2d85SEd Cashin 
93369cf2d85SEd Cashin static void
93469cf2d85SEd Cashin bufinit(struct buf *buf, struct request *rq, struct bio *bio)
93569cf2d85SEd Cashin {
93669cf2d85SEd Cashin 	memset(buf, 0, sizeof(*buf));
93769cf2d85SEd Cashin 	buf->rq = rq;
93869cf2d85SEd Cashin 	buf->bio = bio;
93969cf2d85SEd Cashin 	buf->resid = bio->bi_size;
94069cf2d85SEd Cashin 	buf->sector = bio->bi_sector;
94169cf2d85SEd Cashin 	bio_pageinc(bio);
942ebb37277SLinus Torvalds 	buf->bv = bio_iovec(bio);
9432124469eSJens Axboe 	buf->bv_resid = buf->bv->bv_len;
94469cf2d85SEd Cashin 	WARN_ON(buf->bv_resid == 0);
94569cf2d85SEd Cashin }
94669cf2d85SEd Cashin 
94769cf2d85SEd Cashin static struct buf *
94869cf2d85SEd Cashin nextbuf(struct aoedev *d)
94969cf2d85SEd Cashin {
95069cf2d85SEd Cashin 	struct request *rq;
95169cf2d85SEd Cashin 	struct request_queue *q;
95269cf2d85SEd Cashin 	struct buf *buf;
95369cf2d85SEd Cashin 	struct bio *bio;
95469cf2d85SEd Cashin 
95569cf2d85SEd Cashin 	q = d->blkq;
95669cf2d85SEd Cashin 	if (q == NULL)
95769cf2d85SEd Cashin 		return NULL;	/* initializing */
95869cf2d85SEd Cashin 	if (d->ip.buf)
95969cf2d85SEd Cashin 		return d->ip.buf;
96069cf2d85SEd Cashin 	rq = d->ip.rq;
96169cf2d85SEd Cashin 	if (rq == NULL) {
96269cf2d85SEd Cashin 		rq = blk_peek_request(q);
96369cf2d85SEd Cashin 		if (rq == NULL)
96469cf2d85SEd Cashin 			return NULL;
96569cf2d85SEd Cashin 		blk_start_request(rq);
96669cf2d85SEd Cashin 		d->ip.rq = rq;
96769cf2d85SEd Cashin 		d->ip.nxbio = rq->bio;
96869cf2d85SEd Cashin 		rq->special = (void *) rqbiocnt(rq);
96969cf2d85SEd Cashin 	}
97069cf2d85SEd Cashin 	buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
97169cf2d85SEd Cashin 	if (buf == NULL) {
97269cf2d85SEd Cashin 		pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
97369cf2d85SEd Cashin 		return NULL;
97469cf2d85SEd Cashin 	}
97569cf2d85SEd Cashin 	bio = d->ip.nxbio;
97669cf2d85SEd Cashin 	bufinit(buf, rq, bio);
97769cf2d85SEd Cashin 	bio = bio->bi_next;
97869cf2d85SEd Cashin 	d->ip.nxbio = bio;
97969cf2d85SEd Cashin 	if (bio == NULL)
98069cf2d85SEd Cashin 		d->ip.rq = NULL;
98169cf2d85SEd Cashin 	return d->ip.buf = buf;
9821da177e4SLinus Torvalds }
9831da177e4SLinus Torvalds 
98468e0d42fSEd L. Cashin /* enters with d->lock held */
98568e0d42fSEd L. Cashin void
98668e0d42fSEd L. Cashin aoecmd_work(struct aoedev *d)
98768e0d42fSEd L. Cashin {
9883a0c40d2SEd Cashin 	rexmit_deferred(d);
98969cf2d85SEd Cashin 	while (aoecmd_ata_rw(d))
99069cf2d85SEd Cashin 		;
99168e0d42fSEd L. Cashin }
99268e0d42fSEd L. Cashin 
9933ae1c24eSEd L. Cashin /* this function performs work that has been deferred until sleeping is OK
9943ae1c24eSEd L. Cashin  */
9953ae1c24eSEd L. Cashin void
996c4028958SDavid Howells aoecmd_sleepwork(struct work_struct *work)
9973ae1c24eSEd L. Cashin {
998c4028958SDavid Howells 	struct aoedev *d = container_of(work, struct aoedev, work);
999b21faa25SEd Cashin 	struct block_device *bd;
1000b21faa25SEd Cashin 	u64 ssize;
10013ae1c24eSEd L. Cashin 
10023ae1c24eSEd L. Cashin 	if (d->flags & DEVFL_GDALLOC)
10033ae1c24eSEd L. Cashin 		aoeblk_gdalloc(d);
10043ae1c24eSEd L. Cashin 
10053ae1c24eSEd L. Cashin 	if (d->flags & DEVFL_NEWSIZE) {
100680795aefSTejun Heo 		ssize = get_capacity(d->gd);
10073ae1c24eSEd L. Cashin 		bd = bdget_disk(d->gd, 0);
10083ae1c24eSEd L. Cashin 		if (bd) {
10093ae1c24eSEd L. Cashin 			mutex_lock(&bd->bd_inode->i_mutex);
10103ae1c24eSEd L. Cashin 			i_size_write(bd->bd_inode, (loff_t)ssize<<9);
10113ae1c24eSEd L. Cashin 			mutex_unlock(&bd->bd_inode->i_mutex);
10123ae1c24eSEd L. Cashin 			bdput(bd);
10133ae1c24eSEd L. Cashin 		}
1014b21faa25SEd Cashin 		spin_lock_irq(&d->lock);
10153ae1c24eSEd L. Cashin 		d->flags |= DEVFL_UP;
10163ae1c24eSEd L. Cashin 		d->flags &= ~DEVFL_NEWSIZE;
1017b21faa25SEd Cashin 		spin_unlock_irq(&d->lock);
10183ae1c24eSEd L. Cashin 	}
10193ae1c24eSEd L. Cashin }
10203ae1c24eSEd L. Cashin 
10211da177e4SLinus Torvalds static void
1022667be1e7SEd Cashin ata_ident_fixstring(u16 *id, int ns)
1023667be1e7SEd Cashin {
1024667be1e7SEd Cashin 	u16 s;
1025667be1e7SEd Cashin 
1026667be1e7SEd Cashin 	while (ns-- > 0) {
1027667be1e7SEd Cashin 		s = *id;
1028667be1e7SEd Cashin 		*id++ = s >> 8 | s << 8;
1029667be1e7SEd Cashin 	}
1030667be1e7SEd Cashin }
1031667be1e7SEd Cashin 
1032667be1e7SEd Cashin static void
103368e0d42fSEd L. Cashin ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
10341da177e4SLinus Torvalds {
10351da177e4SLinus Torvalds 	u64 ssize;
10361da177e4SLinus Torvalds 	u16 n;
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	/* word 83: command set supported */
1039f885f8d1SHarvey Harrison 	n = get_unaligned_le16(&id[83 << 1]);
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 	/* word 86: command set/feature enabled */
1042f885f8d1SHarvey Harrison 	n |= get_unaligned_le16(&id[86 << 1]);
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	if (n & (1<<10)) {	/* bit 10: LBA 48 */
10451da177e4SLinus Torvalds 		d->flags |= DEVFL_EXT;
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 		/* word 100: number lba48 sectors */
1048f885f8d1SHarvey Harrison 		ssize = get_unaligned_le64(&id[100 << 1]);
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 		/* set as in ide-disk.c:init_idedisk_capacity */
10511da177e4SLinus Torvalds 		d->geo.cylinders = ssize;
10521da177e4SLinus Torvalds 		d->geo.cylinders /= (255 * 63);
10531da177e4SLinus Torvalds 		d->geo.heads = 255;
10541da177e4SLinus Torvalds 		d->geo.sectors = 63;
10551da177e4SLinus Torvalds 	} else {
10561da177e4SLinus Torvalds 		d->flags &= ~DEVFL_EXT;
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 		/* number lba28 sectors */
1059f885f8d1SHarvey Harrison 		ssize = get_unaligned_le32(&id[60 << 1]);
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 		/* NOTE: obsolete in ATA 6 */
1062f885f8d1SHarvey Harrison 		d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
1063f885f8d1SHarvey Harrison 		d->geo.heads = get_unaligned_le16(&id[55 << 1]);
1064f885f8d1SHarvey Harrison 		d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
10651da177e4SLinus Torvalds 	}
10663ae1c24eSEd L. Cashin 
1067667be1e7SEd Cashin 	ata_ident_fixstring((u16 *) &id[10<<1], 10);	/* serial */
1068667be1e7SEd Cashin 	ata_ident_fixstring((u16 *) &id[23<<1], 4);	/* firmware */
1069667be1e7SEd Cashin 	ata_ident_fixstring((u16 *) &id[27<<1], 20);	/* model */
1070667be1e7SEd Cashin 	memcpy(d->ident, id, sizeof(d->ident));
1071667be1e7SEd Cashin 
10723ae1c24eSEd L. Cashin 	if (d->ssize != ssize)
10731d75981aSEd L. Cashin 		printk(KERN_INFO
1074411c41eeSHarvey Harrison 			"aoe: %pm e%ld.%d v%04x has %llu sectors\n",
1075411c41eeSHarvey Harrison 			t->addr,
10763ae1c24eSEd L. Cashin 			d->aoemajor, d->aoeminor,
10773ae1c24eSEd L. Cashin 			d->fw_ver, (long long)ssize);
10781da177e4SLinus Torvalds 	d->ssize = ssize;
10791da177e4SLinus Torvalds 	d->geo.start = 0;
10806b9699bbSEd L. Cashin 	if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
10816b9699bbSEd L. Cashin 		return;
10821da177e4SLinus Torvalds 	if (d->gd != NULL) {
108380795aefSTejun Heo 		set_capacity(d->gd, ssize);
10843ae1c24eSEd L. Cashin 		d->flags |= DEVFL_NEWSIZE;
108568e0d42fSEd L. Cashin 	} else
10863ae1c24eSEd L. Cashin 		d->flags |= DEVFL_GDALLOC;
10871da177e4SLinus Torvalds 	schedule_work(&d->work);
10881da177e4SLinus Torvalds }
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds static void
10913a0c40d2SEd Cashin calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt)
10921da177e4SLinus Torvalds {
10931da177e4SLinus Torvalds 	register long n;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	n = rtt;
10961da177e4SLinus Torvalds 
10973a0c40d2SEd Cashin 	/* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */
10983a0c40d2SEd Cashin 	n -= d->rttavg >> RTTSCALE;
10993a0c40d2SEd Cashin 	d->rttavg += n;
11003a0c40d2SEd Cashin 	if (n < 0)
11013a0c40d2SEd Cashin 		n = -n;
11023a0c40d2SEd Cashin 	n -= d->rttdev >> RTTDSCALE;
11033a0c40d2SEd Cashin 	d->rttdev += n;
11043a0c40d2SEd Cashin 
11053a0c40d2SEd Cashin 	if (!t || t->maxout >= t->nframes)
11063a0c40d2SEd Cashin 		return;
11073a0c40d2SEd Cashin 	if (t->maxout < t->ssthresh)
11083a0c40d2SEd Cashin 		t->maxout += 1;
11093a0c40d2SEd Cashin 	else if (t->nout == t->maxout && t->next_cwnd-- == 0) {
11103a0c40d2SEd Cashin 		t->maxout += 1;
11113a0c40d2SEd Cashin 		t->next_cwnd = t->maxout;
11123a0c40d2SEd Cashin 	}
11131da177e4SLinus Torvalds }
11141da177e4SLinus Torvalds 
111568e0d42fSEd L. Cashin static struct aoetgt *
111668e0d42fSEd L. Cashin gettgt(struct aoedev *d, char *addr)
111768e0d42fSEd L. Cashin {
111868e0d42fSEd L. Cashin 	struct aoetgt **t, **e;
111968e0d42fSEd L. Cashin 
112068e0d42fSEd L. Cashin 	t = d->targets;
112171114ec4SEd Cashin 	e = t + d->ntargets;
112268e0d42fSEd L. Cashin 	for (; t < e && *t; t++)
112368e0d42fSEd L. Cashin 		if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
112468e0d42fSEd L. Cashin 			return *t;
112568e0d42fSEd L. Cashin 	return NULL;
112668e0d42fSEd L. Cashin }
112768e0d42fSEd L. Cashin 
11283d5b0605SEd Cashin static void
1129896831f5SEd Cashin bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
11303d5b0605SEd Cashin {
11313d5b0605SEd Cashin 	ulong fcnt;
11323d5b0605SEd Cashin 	char *p;
11333d5b0605SEd Cashin 	int soff = 0;
11343d5b0605SEd Cashin loop:
11353d5b0605SEd Cashin 	fcnt = bv->bv_len - (off - bv->bv_offset);
11363d5b0605SEd Cashin 	if (fcnt > cnt)
11373d5b0605SEd Cashin 		fcnt = cnt;
11383d5b0605SEd Cashin 	p = page_address(bv->bv_page) + off;
11393d5b0605SEd Cashin 	skb_copy_bits(skb, soff, p, fcnt);
11403d5b0605SEd Cashin 	soff += fcnt;
11413d5b0605SEd Cashin 	cnt -= fcnt;
11423d5b0605SEd Cashin 	if (cnt <= 0)
11433d5b0605SEd Cashin 		return;
11443d5b0605SEd Cashin 	bv++;
11453d5b0605SEd Cashin 	off = bv->bv_offset;
11463d5b0605SEd Cashin 	goto loop;
11473d5b0605SEd Cashin }
11483d5b0605SEd Cashin 
114969cf2d85SEd Cashin void
115069cf2d85SEd Cashin aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
115169cf2d85SEd Cashin {
115269cf2d85SEd Cashin 	struct bio *bio;
115369cf2d85SEd Cashin 	int bok;
115469cf2d85SEd Cashin 	struct request_queue *q;
115569cf2d85SEd Cashin 
115669cf2d85SEd Cashin 	q = d->blkq;
115769cf2d85SEd Cashin 	if (rq == d->ip.rq)
115869cf2d85SEd Cashin 		d->ip.rq = NULL;
115969cf2d85SEd Cashin 	do {
116069cf2d85SEd Cashin 		bio = rq->bio;
116169cf2d85SEd Cashin 		bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
116269cf2d85SEd Cashin 	} while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
116369cf2d85SEd Cashin 
116469cf2d85SEd Cashin 	/* cf. http://lkml.org/lkml/2006/10/31/28 */
116569cf2d85SEd Cashin 	if (!fastfail)
116611cfb6ffSEd Cashin 		__blk_run_queue(q);
116769cf2d85SEd Cashin }
116869cf2d85SEd Cashin 
116969cf2d85SEd Cashin static void
117069cf2d85SEd Cashin aoe_end_buf(struct aoedev *d, struct buf *buf)
117169cf2d85SEd Cashin {
117269cf2d85SEd Cashin 	struct request *rq;
117369cf2d85SEd Cashin 	unsigned long n;
117469cf2d85SEd Cashin 
117569cf2d85SEd Cashin 	if (buf == d->ip.buf)
117669cf2d85SEd Cashin 		d->ip.buf = NULL;
117769cf2d85SEd Cashin 	rq = buf->rq;
117869cf2d85SEd Cashin 	bio_pagedec(buf->bio);
117969cf2d85SEd Cashin 	mempool_free(buf, d->bufpool);
118069cf2d85SEd Cashin 	n = (unsigned long) rq->special;
118169cf2d85SEd Cashin 	rq->special = (void *) --n;
118269cf2d85SEd Cashin 	if (n == 0)
118369cf2d85SEd Cashin 		aoe_end_request(d, rq, 0);
118469cf2d85SEd Cashin }
118569cf2d85SEd Cashin 
11863d5b0605SEd Cashin static void
1187896831f5SEd Cashin ktiocomplete(struct frame *f)
11883d5b0605SEd Cashin {
1189ddec63e8SEd L. Cashin 	struct aoe_hdr *hin, *hout;
11901da177e4SLinus Torvalds 	struct aoe_atahdr *ahin, *ahout;
11911da177e4SLinus Torvalds 	struct buf *buf;
1192896831f5SEd Cashin 	struct sk_buff *skb;
119368e0d42fSEd L. Cashin 	struct aoetgt *t;
119468e0d42fSEd L. Cashin 	struct aoeif *ifp;
1195896831f5SEd Cashin 	struct aoedev *d;
1196896831f5SEd Cashin 	long n;
1197bbb44e30SEd Cashin 	int untainted;
1198896831f5SEd Cashin 
1199896831f5SEd Cashin 	if (f == NULL)
1200896831f5SEd Cashin 		return;
1201896831f5SEd Cashin 
1202896831f5SEd Cashin 	t = f->t;
1203896831f5SEd Cashin 	d = t->d;
1204bbb44e30SEd Cashin 	skb = f->r_skb;
1205bbb44e30SEd Cashin 	buf = f->buf;
1206bbb44e30SEd Cashin 	if (f->flags & FFL_PROBE)
1207bbb44e30SEd Cashin 		goto out;
1208bbb44e30SEd Cashin 	if (!skb)		/* just fail the buf. */
1209bbb44e30SEd Cashin 		goto noskb;
1210896831f5SEd Cashin 
1211896831f5SEd Cashin 	hout = (struct aoe_hdr *) skb_mac_header(f->skb);
1212896831f5SEd Cashin 	ahout = (struct aoe_atahdr *) (hout+1);
1213896831f5SEd Cashin 
1214896831f5SEd Cashin 	hin = (struct aoe_hdr *) skb->data;
1215896831f5SEd Cashin 	skb_pull(skb, sizeof(*hin));
1216896831f5SEd Cashin 	ahin = (struct aoe_atahdr *) skb->data;
1217896831f5SEd Cashin 	skb_pull(skb, sizeof(*ahin));
1218896831f5SEd Cashin 	if (ahin->cmdstat & 0xa9) {	/* these bits cleared on success */
1219896831f5SEd Cashin 		pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
1220896831f5SEd Cashin 			ahout->cmdstat, ahin->cmdstat,
1221896831f5SEd Cashin 			d->aoemajor, d->aoeminor);
1222896831f5SEd Cashin noskb:		if (buf)
122369cf2d85SEd Cashin 			clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1224bbb44e30SEd Cashin 		goto out;
1225896831f5SEd Cashin 	}
1226896831f5SEd Cashin 
1227896831f5SEd Cashin 	n = ahout->scnt << 9;
1228896831f5SEd Cashin 	switch (ahout->cmdstat) {
1229896831f5SEd Cashin 	case ATA_CMD_PIO_READ:
1230896831f5SEd Cashin 	case ATA_CMD_PIO_READ_EXT:
1231896831f5SEd Cashin 		if (skb->len < n) {
1232bf29754aSEd Cashin 			pr_err("%s e%ld.%d.  skb->len=%d need=%ld\n",
1233bf29754aSEd Cashin 				"aoe: runt data size in read from",
1234bf29754aSEd Cashin 				(long) d->aoemajor, d->aoeminor,
1235896831f5SEd Cashin 			       skb->len, n);
123669cf2d85SEd Cashin 			clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1237896831f5SEd Cashin 			break;
1238896831f5SEd Cashin 		}
1239896831f5SEd Cashin 		bvcpy(f->bv, f->bv_off, skb, n);
1240896831f5SEd Cashin 	case ATA_CMD_PIO_WRITE:
1241896831f5SEd Cashin 	case ATA_CMD_PIO_WRITE_EXT:
1242896831f5SEd Cashin 		spin_lock_irq(&d->lock);
1243896831f5SEd Cashin 		ifp = getif(t, skb->dev);
12443f0f0133SEd Cashin 		if (ifp)
1245896831f5SEd Cashin 			ifp->lost = 0;
1246896831f5SEd Cashin 		spin_unlock_irq(&d->lock);
1247896831f5SEd Cashin 		break;
1248896831f5SEd Cashin 	case ATA_CMD_ID_ATA:
1249896831f5SEd Cashin 		if (skb->len < 512) {
1250bf29754aSEd Cashin 			pr_info("%s e%ld.%d.  skb->len=%d need=512\n",
1251bf29754aSEd Cashin 				"aoe: runt data size in ataid from",
1252bf29754aSEd Cashin 				(long) d->aoemajor, d->aoeminor,
1253896831f5SEd Cashin 				skb->len);
1254896831f5SEd Cashin 			break;
1255896831f5SEd Cashin 		}
1256896831f5SEd Cashin 		if (skb_linearize(skb))
1257896831f5SEd Cashin 			break;
1258896831f5SEd Cashin 		spin_lock_irq(&d->lock);
1259896831f5SEd Cashin 		ataid_complete(d, t, skb->data);
1260896831f5SEd Cashin 		spin_unlock_irq(&d->lock);
1261896831f5SEd Cashin 		break;
1262896831f5SEd Cashin 	default:
1263896831f5SEd Cashin 		pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
1264896831f5SEd Cashin 			ahout->cmdstat,
1265896831f5SEd Cashin 			be16_to_cpu(get_unaligned(&hin->major)),
1266896831f5SEd Cashin 			hin->minor);
1267896831f5SEd Cashin 	}
1268bbb44e30SEd Cashin out:
1269896831f5SEd Cashin 	spin_lock_irq(&d->lock);
1270bbb44e30SEd Cashin 	if (t->taint > 0
1271bbb44e30SEd Cashin 	&& --t->taint > 0
1272bbb44e30SEd Cashin 	&& t->nout_probes == 0) {
1273bbb44e30SEd Cashin 		count_targets(d, &untainted);
1274bbb44e30SEd Cashin 		if (untainted > 0) {
1275bbb44e30SEd Cashin 			probe(t);
1276bbb44e30SEd Cashin 			t->nout_probes++;
1277bbb44e30SEd Cashin 		}
1278bbb44e30SEd Cashin 	}
1279896831f5SEd Cashin 
1280896831f5SEd Cashin 	aoe_freetframe(f);
1281896831f5SEd Cashin 
128269cf2d85SEd Cashin 	if (buf && --buf->nframesout == 0 && buf->resid == 0)
128369cf2d85SEd Cashin 		aoe_end_buf(d, buf);
1284896831f5SEd Cashin 
1285896831f5SEd Cashin 	spin_unlock_irq(&d->lock);
128669cf2d85SEd Cashin 	aoedev_put(d);
1287896831f5SEd Cashin 	dev_kfree_skb(skb);
1288896831f5SEd Cashin }
1289896831f5SEd Cashin 
1290896831f5SEd Cashin /* Enters with iocq.lock held.
1291896831f5SEd Cashin  * Returns true iff responses needing processing remain.
1292896831f5SEd Cashin  */
1293896831f5SEd Cashin static int
1294*8030d343SEd Cashin ktio(int id)
1295896831f5SEd Cashin {
1296896831f5SEd Cashin 	struct frame *f;
1297896831f5SEd Cashin 	struct list_head *pos;
1298896831f5SEd Cashin 	int i;
1299*8030d343SEd Cashin 	int actual_id;
1300896831f5SEd Cashin 
1301896831f5SEd Cashin 	for (i = 0; ; ++i) {
1302896831f5SEd Cashin 		if (i == MAXIOC)
1303896831f5SEd Cashin 			return 1;
1304*8030d343SEd Cashin 		if (list_empty(&iocq[id].head))
1305896831f5SEd Cashin 			return 0;
1306*8030d343SEd Cashin 		pos = iocq[id].head.next;
1307896831f5SEd Cashin 		list_del(pos);
1308896831f5SEd Cashin 		f = list_entry(pos, struct frame, head);
1309*8030d343SEd Cashin 		spin_unlock_irq(&iocq[id].lock);
1310896831f5SEd Cashin 		ktiocomplete(f);
1311*8030d343SEd Cashin 
1312*8030d343SEd Cashin 		/* Figure out if extra threads are required. */
1313*8030d343SEd Cashin 		actual_id = f->t->d->aoeminor % ncpus;
1314*8030d343SEd Cashin 
1315*8030d343SEd Cashin 		if (!kts[actual_id].active) {
1316*8030d343SEd Cashin 			BUG_ON(id != 0);
1317*8030d343SEd Cashin 			mutex_lock(&ktio_spawn_lock);
1318*8030d343SEd Cashin 			if (!kts[actual_id].active
1319*8030d343SEd Cashin 				&& aoe_ktstart(&kts[actual_id]) == 0)
1320*8030d343SEd Cashin 				kts[actual_id].active = 1;
1321*8030d343SEd Cashin 			mutex_unlock(&ktio_spawn_lock);
1322*8030d343SEd Cashin 		}
1323*8030d343SEd Cashin 		spin_lock_irq(&iocq[id].lock);
1324896831f5SEd Cashin 	}
1325896831f5SEd Cashin }
1326896831f5SEd Cashin 
1327896831f5SEd Cashin static int
1328896831f5SEd Cashin kthread(void *vp)
1329896831f5SEd Cashin {
1330896831f5SEd Cashin 	struct ktstate *k;
1331896831f5SEd Cashin 	DECLARE_WAITQUEUE(wait, current);
1332896831f5SEd Cashin 	int more;
1333896831f5SEd Cashin 
1334896831f5SEd Cashin 	k = vp;
1335896831f5SEd Cashin 	current->flags |= PF_NOFREEZE;
1336896831f5SEd Cashin 	set_user_nice(current, -10);
1337896831f5SEd Cashin 	complete(&k->rendez);	/* tell spawner we're running */
1338896831f5SEd Cashin 	do {
1339896831f5SEd Cashin 		spin_lock_irq(k->lock);
1340*8030d343SEd Cashin 		more = k->fn(k->id);
1341896831f5SEd Cashin 		if (!more) {
1342896831f5SEd Cashin 			add_wait_queue(k->waitq, &wait);
1343896831f5SEd Cashin 			__set_current_state(TASK_INTERRUPTIBLE);
1344896831f5SEd Cashin 		}
1345896831f5SEd Cashin 		spin_unlock_irq(k->lock);
1346896831f5SEd Cashin 		if (!more) {
1347896831f5SEd Cashin 			schedule();
1348896831f5SEd Cashin 			remove_wait_queue(k->waitq, &wait);
1349896831f5SEd Cashin 		} else
1350896831f5SEd Cashin 			cond_resched();
1351896831f5SEd Cashin 	} while (!kthread_should_stop());
1352896831f5SEd Cashin 	complete(&k->rendez);	/* tell spawner we're stopping */
1353896831f5SEd Cashin 	return 0;
1354896831f5SEd Cashin }
1355896831f5SEd Cashin 
1356eb086ec5SEd Cashin void
1357896831f5SEd Cashin aoe_ktstop(struct ktstate *k)
1358896831f5SEd Cashin {
1359896831f5SEd Cashin 	kthread_stop(k->task);
1360896831f5SEd Cashin 	wait_for_completion(&k->rendez);
1361896831f5SEd Cashin }
1362896831f5SEd Cashin 
1363eb086ec5SEd Cashin int
1364896831f5SEd Cashin aoe_ktstart(struct ktstate *k)
1365896831f5SEd Cashin {
1366896831f5SEd Cashin 	struct task_struct *task;
1367896831f5SEd Cashin 
1368896831f5SEd Cashin 	init_completion(&k->rendez);
1369f170168bSKees Cook 	task = kthread_run(kthread, k, "%s", k->name);
1370896831f5SEd Cashin 	if (task == NULL || IS_ERR(task))
1371896831f5SEd Cashin 		return -ENOMEM;
1372896831f5SEd Cashin 	k->task = task;
1373896831f5SEd Cashin 	wait_for_completion(&k->rendez); /* allow kthread to start */
1374896831f5SEd Cashin 	init_completion(&k->rendez);	/* for waiting for exit later */
1375896831f5SEd Cashin 	return 0;
1376896831f5SEd Cashin }
1377896831f5SEd Cashin 
1378896831f5SEd Cashin /* pass it off to kthreads for processing */
1379896831f5SEd Cashin static void
1380896831f5SEd Cashin ktcomplete(struct frame *f, struct sk_buff *skb)
1381896831f5SEd Cashin {
1382*8030d343SEd Cashin 	int id;
1383896831f5SEd Cashin 	ulong flags;
1384896831f5SEd Cashin 
1385896831f5SEd Cashin 	f->r_skb = skb;
1386*8030d343SEd Cashin 	id = f->t->d->aoeminor % ncpus;
1387*8030d343SEd Cashin 	spin_lock_irqsave(&iocq[id].lock, flags);
1388*8030d343SEd Cashin 	if (!kts[id].active) {
1389*8030d343SEd Cashin 		spin_unlock_irqrestore(&iocq[id].lock, flags);
1390*8030d343SEd Cashin 		/* The thread with id has not been spawned yet,
1391*8030d343SEd Cashin 		 * so delegate the work to the main thread and
1392*8030d343SEd Cashin 		 * try spawning a new thread.
1393*8030d343SEd Cashin 		 */
1394*8030d343SEd Cashin 		id = 0;
1395*8030d343SEd Cashin 		spin_lock_irqsave(&iocq[id].lock, flags);
1396*8030d343SEd Cashin 	}
1397*8030d343SEd Cashin 	list_add_tail(&f->head, &iocq[id].head);
1398*8030d343SEd Cashin 	spin_unlock_irqrestore(&iocq[id].lock, flags);
1399*8030d343SEd Cashin 	wake_up(&ktiowq[id]);
1400896831f5SEd Cashin }
1401896831f5SEd Cashin 
1402896831f5SEd Cashin struct sk_buff *
1403896831f5SEd Cashin aoecmd_ata_rsp(struct sk_buff *skb)
1404896831f5SEd Cashin {
1405896831f5SEd Cashin 	struct aoedev *d;
1406896831f5SEd Cashin 	struct aoe_hdr *h;
1407896831f5SEd Cashin 	struct frame *f;
1408896831f5SEd Cashin 	u32 n;
14091da177e4SLinus Torvalds 	ulong flags;
14101da177e4SLinus Torvalds 	char ebuf[128];
141132465c65Secashin@coraid.com 	u16 aoemajor;
14121da177e4SLinus Torvalds 
1413896831f5SEd Cashin 	h = (struct aoe_hdr *) skb->data;
1414896831f5SEd Cashin 	aoemajor = be16_to_cpu(get_unaligned(&h->major));
14150c966214SEd Cashin 	d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
14161da177e4SLinus Torvalds 	if (d == NULL) {
14171da177e4SLinus Torvalds 		snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
14181da177e4SLinus Torvalds 			"for unknown device %d.%d\n",
1419896831f5SEd Cashin 			aoemajor, h->minor);
14201da177e4SLinus Torvalds 		aoechr_error(ebuf);
1421896831f5SEd Cashin 		return skb;
14221da177e4SLinus Torvalds 	}
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds 	spin_lock_irqsave(&d->lock, flags);
14251da177e4SLinus Torvalds 
1426896831f5SEd Cashin 	n = be32_to_cpu(get_unaligned(&h->tag));
142764a80f5aSEd Cashin 	f = getframe(d, n);
14283a0c40d2SEd Cashin 	if (f) {
14295f0c9c48SEd Cashin 		calc_rttavg(d, f->t, tsince_hr(f));
14303a0c40d2SEd Cashin 		f->t->nout--;
1431bbb44e30SEd Cashin 		if (f->flags & FFL_PROBE)
1432bbb44e30SEd Cashin 			f->t->nout_probes--;
14333a0c40d2SEd Cashin 	} else {
14343a0c40d2SEd Cashin 		f = getframe_deferred(d, n);
14353a0c40d2SEd Cashin 		if (f) {
14365f0c9c48SEd Cashin 			calc_rttavg(d, NULL, tsince_hr(f));
14373a0c40d2SEd Cashin 		} else {
14383a0c40d2SEd Cashin 			calc_rttavg(d, NULL, tsince(n));
14391da177e4SLinus Torvalds 			spin_unlock_irqrestore(&d->lock, flags);
144069cf2d85SEd Cashin 			aoedev_put(d);
14413a0c40d2SEd Cashin 			snprintf(ebuf, sizeof(ebuf),
14422292a7e1SEd Cashin 				 "%15s e%d.%d    tag=%08x@%08lx s=%pm d=%pm\n",
14431da177e4SLinus Torvalds 				 "unexpected rsp",
1444896831f5SEd Cashin 				 get_unaligned_be16(&h->major),
1445896831f5SEd Cashin 				 h->minor,
1446896831f5SEd Cashin 				 get_unaligned_be32(&h->tag),
14472292a7e1SEd Cashin 				 jiffies,
14482292a7e1SEd Cashin 				 h->src,
14492292a7e1SEd Cashin 				 h->dst);
14501da177e4SLinus Torvalds 			aoechr_error(ebuf);
1451896831f5SEd Cashin 			return skb;
14521da177e4SLinus Torvalds 		}
14533a0c40d2SEd Cashin 	}
14541da177e4SLinus Torvalds 	aoecmd_work(d);
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds 	spin_unlock_irqrestore(&d->lock, flags);
1457896831f5SEd Cashin 
1458896831f5SEd Cashin 	ktcomplete(f, skb);
1459896831f5SEd Cashin 
1460896831f5SEd Cashin 	/*
1461896831f5SEd Cashin 	 * Note here that we do not perform an aoedev_put, as we are
1462896831f5SEd Cashin 	 * leaving this reference for the ktio to release.
1463896831f5SEd Cashin 	 */
1464896831f5SEd Cashin 	return NULL;
14651da177e4SLinus Torvalds }
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds void
14681da177e4SLinus Torvalds aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
14691da177e4SLinus Torvalds {
1470e9bb8fb0SDavid S. Miller 	struct sk_buff_head queue;
14711da177e4SLinus Torvalds 
1472e9bb8fb0SDavid S. Miller 	__skb_queue_head_init(&queue);
1473e9bb8fb0SDavid S. Miller 	aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
1474e9bb8fb0SDavid S. Miller 	aoenet_xmit(&queue);
14751da177e4SLinus Torvalds }
14761da177e4SLinus Torvalds 
147768e0d42fSEd L. Cashin struct sk_buff *
14781da177e4SLinus Torvalds aoecmd_ata_id(struct aoedev *d)
14791da177e4SLinus Torvalds {
14801da177e4SLinus Torvalds 	struct aoe_hdr *h;
14811da177e4SLinus Torvalds 	struct aoe_atahdr *ah;
14821da177e4SLinus Torvalds 	struct frame *f;
14831da177e4SLinus Torvalds 	struct sk_buff *skb;
148468e0d42fSEd L. Cashin 	struct aoetgt *t;
14851da177e4SLinus Torvalds 
1486896831f5SEd Cashin 	f = newframe(d);
148768e0d42fSEd L. Cashin 	if (f == NULL)
14881da177e4SLinus Torvalds 		return NULL;
148968e0d42fSEd L. Cashin 
149068e0d42fSEd L. Cashin 	t = *d->tgt;
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	/* initialize the headers & frame */
1493e407a7f6SEd L. Cashin 	skb = f->skb;
1494abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
14951da177e4SLinus Torvalds 	ah = (struct aoe_atahdr *) (h+1);
149619900cdeSEd L. Cashin 	skb_put(skb, sizeof *h + sizeof *ah);
149719900cdeSEd L. Cashin 	memset(h, 0, skb->len);
149868e0d42fSEd L. Cashin 	f->tag = aoehdr_atainit(d, t, h);
1499896831f5SEd Cashin 	fhash(f);
150068e0d42fSEd L. Cashin 	t->nout++;
15011da177e4SLinus Torvalds 	f->waited = 0;
15023fc9b032SEd Cashin 	f->waited_total = 0;
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds 	/* set up ata header */
15051da177e4SLinus Torvalds 	ah->scnt = 1;
150604b3ab52SBartlomiej Zolnierkiewicz 	ah->cmdstat = ATA_CMD_ID_ATA;
15071da177e4SLinus Torvalds 	ah->lba3 = 0xa0;
15081da177e4SLinus Torvalds 
150968e0d42fSEd L. Cashin 	skb->dev = t->ifp->nd;
15101da177e4SLinus Torvalds 
15113a0c40d2SEd Cashin 	d->rttavg = RTTAVG_INIT;
15123a0c40d2SEd Cashin 	d->rttdev = RTTDEV_INIT;
15131da177e4SLinus Torvalds 	d->timer.function = rexmit_timer;
15141da177e4SLinus Torvalds 
15155f0c9c48SEd Cashin 	skb = skb_clone(skb, GFP_ATOMIC);
15165f0c9c48SEd Cashin 	if (skb) {
15175f0c9c48SEd Cashin 		do_gettimeofday(&f->sent);
15185f0c9c48SEd Cashin 		f->sent_jiffs = (u32) jiffies;
15195f0c9c48SEd Cashin 	}
15205f0c9c48SEd Cashin 
15215f0c9c48SEd Cashin 	return skb;
15221da177e4SLinus Torvalds }
15231da177e4SLinus Torvalds 
152471114ec4SEd Cashin static struct aoetgt **
152571114ec4SEd Cashin grow_targets(struct aoedev *d)
152671114ec4SEd Cashin {
152771114ec4SEd Cashin 	ulong oldn, newn;
152871114ec4SEd Cashin 	struct aoetgt **tt;
152971114ec4SEd Cashin 
153071114ec4SEd Cashin 	oldn = d->ntargets;
153171114ec4SEd Cashin 	newn = oldn * 2;
153271114ec4SEd Cashin 	tt = kcalloc(newn, sizeof(*d->targets), GFP_ATOMIC);
153371114ec4SEd Cashin 	if (!tt)
153471114ec4SEd Cashin 		return NULL;
153571114ec4SEd Cashin 	memmove(tt, d->targets, sizeof(*d->targets) * oldn);
153671114ec4SEd Cashin 	d->tgt = tt + (d->tgt - d->targets);
153771114ec4SEd Cashin 	kfree(d->targets);
153871114ec4SEd Cashin 	d->targets = tt;
153971114ec4SEd Cashin 	d->ntargets = newn;
154071114ec4SEd Cashin 
154171114ec4SEd Cashin 	return &d->targets[oldn];
154271114ec4SEd Cashin }
154371114ec4SEd Cashin 
154468e0d42fSEd L. Cashin static struct aoetgt *
154568e0d42fSEd L. Cashin addtgt(struct aoedev *d, char *addr, ulong nframes)
154668e0d42fSEd L. Cashin {
154768e0d42fSEd L. Cashin 	struct aoetgt *t, **tt, **te;
154868e0d42fSEd L. Cashin 
154968e0d42fSEd L. Cashin 	tt = d->targets;
155071114ec4SEd Cashin 	te = tt + d->ntargets;
155168e0d42fSEd L. Cashin 	for (; tt < te && *tt; tt++)
155268e0d42fSEd L. Cashin 		;
155368e0d42fSEd L. Cashin 
1554578c4aa0SEd L. Cashin 	if (tt == te) {
155571114ec4SEd Cashin 		tt = grow_targets(d);
155671114ec4SEd Cashin 		if (!tt)
155771114ec4SEd Cashin 			goto nomem;
1558578c4aa0SEd L. Cashin 	}
1559896831f5SEd Cashin 	t = kzalloc(sizeof(*t), GFP_ATOMIC);
156071114ec4SEd Cashin 	if (!t)
156171114ec4SEd Cashin 		goto nomem;
156268e0d42fSEd L. Cashin 	t->nframes = nframes;
1563896831f5SEd Cashin 	t->d = d;
156468e0d42fSEd L. Cashin 	memcpy(t->addr, addr, sizeof t->addr);
156568e0d42fSEd L. Cashin 	t->ifp = t->ifs;
15663a0c40d2SEd Cashin 	aoecmd_wreset(t);
1567bbb44e30SEd Cashin 	t->maxout = t->nframes / 2;
1568896831f5SEd Cashin 	INIT_LIST_HEAD(&t->ffree);
156968e0d42fSEd L. Cashin 	return *tt = t;
157071114ec4SEd Cashin 
157171114ec4SEd Cashin  nomem:
157271114ec4SEd Cashin 	pr_info("aoe: cannot allocate memory to add target\n");
157371114ec4SEd Cashin 	return NULL;
157468e0d42fSEd L. Cashin }
157568e0d42fSEd L. Cashin 
15763f0f0133SEd Cashin static void
15773f0f0133SEd Cashin setdbcnt(struct aoedev *d)
15783f0f0133SEd Cashin {
15793f0f0133SEd Cashin 	struct aoetgt **t, **e;
15803f0f0133SEd Cashin 	int bcnt = 0;
15813f0f0133SEd Cashin 
15823f0f0133SEd Cashin 	t = d->targets;
158371114ec4SEd Cashin 	e = t + d->ntargets;
15843f0f0133SEd Cashin 	for (; t < e && *t; t++)
15853f0f0133SEd Cashin 		if (bcnt == 0 || bcnt > (*t)->minbcnt)
15863f0f0133SEd Cashin 			bcnt = (*t)->minbcnt;
15873f0f0133SEd Cashin 	if (bcnt != d->maxbcnt) {
15883f0f0133SEd Cashin 		d->maxbcnt = bcnt;
15893f0f0133SEd Cashin 		pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
15903f0f0133SEd Cashin 			d->aoemajor, d->aoeminor, bcnt);
15913f0f0133SEd Cashin 	}
15923f0f0133SEd Cashin }
15933f0f0133SEd Cashin 
15943f0f0133SEd Cashin static void
15953f0f0133SEd Cashin setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
15963f0f0133SEd Cashin {
15973f0f0133SEd Cashin 	struct aoedev *d;
15983f0f0133SEd Cashin 	struct aoeif *p, *e;
15993f0f0133SEd Cashin 	int minbcnt;
16003f0f0133SEd Cashin 
16013f0f0133SEd Cashin 	d = t->d;
16023f0f0133SEd Cashin 	minbcnt = bcnt;
16033f0f0133SEd Cashin 	p = t->ifs;
16043f0f0133SEd Cashin 	e = p + NAOEIFS;
16053f0f0133SEd Cashin 	for (; p < e; p++) {
16063f0f0133SEd Cashin 		if (p->nd == NULL)
16073f0f0133SEd Cashin 			break;		/* end of the valid interfaces */
16083f0f0133SEd Cashin 		if (p->nd == nd) {
16093f0f0133SEd Cashin 			p->bcnt = bcnt;	/* we're updating */
16103f0f0133SEd Cashin 			nd = NULL;
16113f0f0133SEd Cashin 		} else if (minbcnt > p->bcnt)
16123f0f0133SEd Cashin 			minbcnt = p->bcnt; /* find the min interface */
16133f0f0133SEd Cashin 	}
16143f0f0133SEd Cashin 	if (nd) {
16153f0f0133SEd Cashin 		if (p == e) {
16163f0f0133SEd Cashin 			pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
16173f0f0133SEd Cashin 			return;
16183f0f0133SEd Cashin 		}
16191b86fda9SEd Cashin 		dev_hold(nd);
16203f0f0133SEd Cashin 		p->nd = nd;
16213f0f0133SEd Cashin 		p->bcnt = bcnt;
16223f0f0133SEd Cashin 	}
16233f0f0133SEd Cashin 	t->minbcnt = minbcnt;
16243f0f0133SEd Cashin 	setdbcnt(d);
16253f0f0133SEd Cashin }
16263f0f0133SEd Cashin 
16271da177e4SLinus Torvalds void
16281da177e4SLinus Torvalds aoecmd_cfg_rsp(struct sk_buff *skb)
16291da177e4SLinus Torvalds {
16301da177e4SLinus Torvalds 	struct aoedev *d;
16311da177e4SLinus Torvalds 	struct aoe_hdr *h;
16321da177e4SLinus Torvalds 	struct aoe_cfghdr *ch;
163368e0d42fSEd L. Cashin 	struct aoetgt *t;
16340c966214SEd Cashin 	ulong flags, aoemajor;
16351da177e4SLinus Torvalds 	struct sk_buff *sl;
163669cf2d85SEd Cashin 	struct sk_buff_head queue;
163719bf2635SEd L. Cashin 	u16 n;
16381da177e4SLinus Torvalds 
163969cf2d85SEd Cashin 	sl = NULL;
1640abdbf94dSEd L. Cashin 	h = (struct aoe_hdr *) skb_mac_header(skb);
16411da177e4SLinus Torvalds 	ch = (struct aoe_cfghdr *) (h+1);
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 	/*
16441da177e4SLinus Torvalds 	 * Enough people have their dip switches set backwards to
16451da177e4SLinus Torvalds 	 * warrant a loud message for this special case.
16461da177e4SLinus Torvalds 	 */
1647823ed72eSHarvey Harrison 	aoemajor = get_unaligned_be16(&h->major);
16481da177e4SLinus Torvalds 	if (aoemajor == 0xfff) {
1649a12c93f0SEd L. Cashin 		printk(KERN_ERR "aoe: Warning: shelf address is all ones.  "
16506bb6285fSEd L. Cashin 			"Check shelf dip switches.\n");
16511da177e4SLinus Torvalds 		return;
16521da177e4SLinus Torvalds 	}
16537159e969SEd Cashin 	if (aoemajor == 0xffff) {
16547159e969SEd Cashin 		pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
16550c966214SEd Cashin 			aoemajor, (int) h->minor);
16566583303cSEd Cashin 		return;
16576583303cSEd Cashin 	}
16587159e969SEd Cashin 	if (h->minor == 0xff) {
16597159e969SEd Cashin 		pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
16607159e969SEd Cashin 			aoemajor, (int) h->minor);
16611da177e4SLinus Torvalds 		return;
16621da177e4SLinus Torvalds 	}
16631da177e4SLinus Torvalds 
166419bf2635SEd L. Cashin 	n = be16_to_cpu(ch->bufcnt);
16657df620d8SEd L. Cashin 	if (n > aoe_maxout)	/* keep it reasonable */
16667df620d8SEd L. Cashin 		n = aoe_maxout;
16671da177e4SLinus Torvalds 
16687159e969SEd Cashin 	d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
16697159e969SEd Cashin 	if (d == NULL) {
16707159e969SEd Cashin 		pr_info("aoe: device allocation failure\n");
16717159e969SEd Cashin 		return;
16727159e969SEd Cashin 	}
16737159e969SEd Cashin 
16741da177e4SLinus Torvalds 	spin_lock_irqsave(&d->lock, flags);
16751da177e4SLinus Torvalds 
167668e0d42fSEd L. Cashin 	t = gettgt(d, h->src);
16771b8a1636SEd Cashin 	if (t) {
16781b8a1636SEd Cashin 		t->nframes = n;
16791b8a1636SEd Cashin 		if (n < t->maxout)
16803a0c40d2SEd Cashin 			aoecmd_wreset(t);
16811b8a1636SEd Cashin 	} else {
168268e0d42fSEd L. Cashin 		t = addtgt(d, h->src, n);
168369cf2d85SEd Cashin 		if (!t)
168469cf2d85SEd Cashin 			goto bail;
168568e0d42fSEd L. Cashin 	}
16863f0f0133SEd Cashin 	n = skb->dev->mtu;
168719bf2635SEd L. Cashin 	n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
168819bf2635SEd L. Cashin 	n /= 512;
168919bf2635SEd L. Cashin 	if (n > ch->scnt)
169019bf2635SEd L. Cashin 		n = ch->scnt;
16914f51dc5eSEd L. Cashin 	n = n ? n * 512 : DEFAULTBCNT;
16923f0f0133SEd Cashin 	setifbcnt(t, skb->dev, n);
16933ae1c24eSEd L. Cashin 
16943ae1c24eSEd L. Cashin 	/* don't change users' perspective */
169569cf2d85SEd Cashin 	if (d->nopen == 0) {
169663e9cc5dSecashin@coraid.com 		d->fw_ver = be16_to_cpu(ch->fwver);
169768e0d42fSEd L. Cashin 		sl = aoecmd_ata_id(d);
169869cf2d85SEd Cashin 	}
169969cf2d85SEd Cashin bail:
17001da177e4SLinus Torvalds 	spin_unlock_irqrestore(&d->lock, flags);
170169cf2d85SEd Cashin 	aoedev_put(d);
1702e9bb8fb0SDavid S. Miller 	if (sl) {
1703e9bb8fb0SDavid S. Miller 		__skb_queue_head_init(&queue);
1704e9bb8fb0SDavid S. Miller 		__skb_queue_tail(&queue, sl);
1705e9bb8fb0SDavid S. Miller 		aoenet_xmit(&queue);
1706e9bb8fb0SDavid S. Miller 	}
17071da177e4SLinus Torvalds }
17081da177e4SLinus Torvalds 
170968e0d42fSEd L. Cashin void
17103a0c40d2SEd Cashin aoecmd_wreset(struct aoetgt *t)
17113a0c40d2SEd Cashin {
17123a0c40d2SEd Cashin 	t->maxout = 1;
17133a0c40d2SEd Cashin 	t->ssthresh = t->nframes / 2;
17143a0c40d2SEd Cashin 	t->next_cwnd = t->nframes;
17153a0c40d2SEd Cashin }
17163a0c40d2SEd Cashin 
17173a0c40d2SEd Cashin void
171868e0d42fSEd L. Cashin aoecmd_cleanslate(struct aoedev *d)
171968e0d42fSEd L. Cashin {
172068e0d42fSEd L. Cashin 	struct aoetgt **t, **te;
172168e0d42fSEd L. Cashin 
17223a0c40d2SEd Cashin 	d->rttavg = RTTAVG_INIT;
17233a0c40d2SEd Cashin 	d->rttdev = RTTDEV_INIT;
17243f0f0133SEd Cashin 	d->maxbcnt = 0;
172568e0d42fSEd L. Cashin 
172668e0d42fSEd L. Cashin 	t = d->targets;
172771114ec4SEd Cashin 	te = t + d->ntargets;
17283f0f0133SEd Cashin 	for (; t < te && *t; t++)
17293a0c40d2SEd Cashin 		aoecmd_wreset(*t);
173068e0d42fSEd L. Cashin }
1731896831f5SEd Cashin 
173269cf2d85SEd Cashin void
173369cf2d85SEd Cashin aoe_failbuf(struct aoedev *d, struct buf *buf)
173469cf2d85SEd Cashin {
173569cf2d85SEd Cashin 	if (buf == NULL)
173669cf2d85SEd Cashin 		return;
173769cf2d85SEd Cashin 	buf->resid = 0;
173869cf2d85SEd Cashin 	clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
173969cf2d85SEd Cashin 	if (buf->nframesout == 0)
174069cf2d85SEd Cashin 		aoe_end_buf(d, buf);
174169cf2d85SEd Cashin }
174269cf2d85SEd Cashin 
174369cf2d85SEd Cashin void
174469cf2d85SEd Cashin aoe_flush_iocq(void)
1745896831f5SEd Cashin {
1746*8030d343SEd Cashin 	int i;
1747*8030d343SEd Cashin 
1748*8030d343SEd Cashin 	for (i = 0; i < ncpus; i++) {
1749*8030d343SEd Cashin 		if (kts[i].active)
1750*8030d343SEd Cashin 			aoe_flush_iocq_by_index(i);
1751*8030d343SEd Cashin 	}
1752*8030d343SEd Cashin }
1753*8030d343SEd Cashin 
1754*8030d343SEd Cashin void
1755*8030d343SEd Cashin aoe_flush_iocq_by_index(int id)
1756*8030d343SEd Cashin {
1757896831f5SEd Cashin 	struct frame *f;
1758896831f5SEd Cashin 	struct aoedev *d;
1759896831f5SEd Cashin 	LIST_HEAD(flist);
1760896831f5SEd Cashin 	struct list_head *pos;
1761896831f5SEd Cashin 	struct sk_buff *skb;
1762896831f5SEd Cashin 	ulong flags;
1763896831f5SEd Cashin 
1764*8030d343SEd Cashin 	spin_lock_irqsave(&iocq[id].lock, flags);
1765*8030d343SEd Cashin 	list_splice_init(&iocq[id].head, &flist);
1766*8030d343SEd Cashin 	spin_unlock_irqrestore(&iocq[id].lock, flags);
1767896831f5SEd Cashin 	while (!list_empty(&flist)) {
1768896831f5SEd Cashin 		pos = flist.next;
1769896831f5SEd Cashin 		list_del(pos);
1770896831f5SEd Cashin 		f = list_entry(pos, struct frame, head);
1771896831f5SEd Cashin 		d = f->t->d;
1772896831f5SEd Cashin 		skb = f->r_skb;
1773896831f5SEd Cashin 		spin_lock_irqsave(&d->lock, flags);
1774896831f5SEd Cashin 		if (f->buf) {
1775896831f5SEd Cashin 			f->buf->nframesout--;
1776896831f5SEd Cashin 			aoe_failbuf(d, f->buf);
1777896831f5SEd Cashin 		}
1778896831f5SEd Cashin 		aoe_freetframe(f);
1779896831f5SEd Cashin 		spin_unlock_irqrestore(&d->lock, flags);
1780896831f5SEd Cashin 		dev_kfree_skb(skb);
178169cf2d85SEd Cashin 		aoedev_put(d);
1782896831f5SEd Cashin 	}
1783896831f5SEd Cashin }
1784896831f5SEd Cashin 
1785896831f5SEd Cashin int __init
1786896831f5SEd Cashin aoecmd_init(void)
1787896831f5SEd Cashin {
1788bbb44e30SEd Cashin 	void *p;
1789*8030d343SEd Cashin 	int i;
1790*8030d343SEd Cashin 	int ret;
1791bbb44e30SEd Cashin 
1792bbb44e30SEd Cashin 	/* get_zeroed_page returns page with ref count 1 */
1793bbb44e30SEd Cashin 	p = (void *) get_zeroed_page(GFP_KERNEL | __GFP_REPEAT);
1794bbb44e30SEd Cashin 	if (!p)
1795bbb44e30SEd Cashin 		return -ENOMEM;
1796bbb44e30SEd Cashin 	empty_page = virt_to_page(p);
1797bbb44e30SEd Cashin 
1798*8030d343SEd Cashin 	ncpus = num_online_cpus();
1799*8030d343SEd Cashin 
1800*8030d343SEd Cashin 	iocq = kcalloc(ncpus, sizeof(struct iocq_ktio), GFP_KERNEL);
1801*8030d343SEd Cashin 	if (!iocq)
1802*8030d343SEd Cashin 		return -ENOMEM;
1803*8030d343SEd Cashin 
1804*8030d343SEd Cashin 	kts = kcalloc(ncpus, sizeof(struct ktstate), GFP_KERNEL);
1805*8030d343SEd Cashin 	if (!kts) {
1806*8030d343SEd Cashin 		ret = -ENOMEM;
1807*8030d343SEd Cashin 		goto kts_fail;
1808*8030d343SEd Cashin 	}
1809*8030d343SEd Cashin 
1810*8030d343SEd Cashin 	ktiowq = kcalloc(ncpus, sizeof(wait_queue_head_t), GFP_KERNEL);
1811*8030d343SEd Cashin 	if (!ktiowq) {
1812*8030d343SEd Cashin 		ret = -ENOMEM;
1813*8030d343SEd Cashin 		goto ktiowq_fail;
1814*8030d343SEd Cashin 	}
1815*8030d343SEd Cashin 
1816*8030d343SEd Cashin 	mutex_init(&ktio_spawn_lock);
1817*8030d343SEd Cashin 
1818*8030d343SEd Cashin 	for (i = 0; i < ncpus; i++) {
1819*8030d343SEd Cashin 		INIT_LIST_HEAD(&iocq[i].head);
1820*8030d343SEd Cashin 		spin_lock_init(&iocq[i].lock);
1821*8030d343SEd Cashin 		init_waitqueue_head(&ktiowq[i]);
1822*8030d343SEd Cashin 		snprintf(kts[i].name, sizeof(kts[i].name), "aoe_ktio%d", i);
1823*8030d343SEd Cashin 		kts[i].fn = ktio;
1824*8030d343SEd Cashin 		kts[i].waitq = &ktiowq[i];
1825*8030d343SEd Cashin 		kts[i].lock = &iocq[i].lock;
1826*8030d343SEd Cashin 		kts[i].id = i;
1827*8030d343SEd Cashin 		kts[i].active = 0;
1828*8030d343SEd Cashin 	}
1829*8030d343SEd Cashin 	kts[0].active = 1;
1830*8030d343SEd Cashin 	if (aoe_ktstart(&kts[0])) {
1831*8030d343SEd Cashin 		ret = -ENOMEM;
1832*8030d343SEd Cashin 		goto ktstart_fail;
1833*8030d343SEd Cashin 	}
1834*8030d343SEd Cashin 	return 0;
1835*8030d343SEd Cashin 
1836*8030d343SEd Cashin ktstart_fail:
1837*8030d343SEd Cashin 	kfree(ktiowq);
1838*8030d343SEd Cashin ktiowq_fail:
1839*8030d343SEd Cashin 	kfree(kts);
1840*8030d343SEd Cashin kts_fail:
1841*8030d343SEd Cashin 	kfree(iocq);
1842*8030d343SEd Cashin 
1843*8030d343SEd Cashin 	return ret;
1844896831f5SEd Cashin }
1845896831f5SEd Cashin 
1846896831f5SEd Cashin void
1847896831f5SEd Cashin aoecmd_exit(void)
1848896831f5SEd Cashin {
1849*8030d343SEd Cashin 	int i;
1850*8030d343SEd Cashin 
1851*8030d343SEd Cashin 	for (i = 0; i < ncpus; i++)
1852*8030d343SEd Cashin 		if (kts[i].active)
1853*8030d343SEd Cashin 			aoe_ktstop(&kts[i]);
1854*8030d343SEd Cashin 
185569cf2d85SEd Cashin 	aoe_flush_iocq();
1856bbb44e30SEd Cashin 
1857*8030d343SEd Cashin 	/* Free up the iocq and thread speicific configuration
1858*8030d343SEd Cashin 	* allocated during startup.
1859*8030d343SEd Cashin 	*/
1860*8030d343SEd Cashin 	kfree(iocq);
1861*8030d343SEd Cashin 	kfree(kts);
1862*8030d343SEd Cashin 	kfree(ktiowq);
1863*8030d343SEd Cashin 
1864bbb44e30SEd Cashin 	free_page((unsigned long) page_address(empty_page));
1865bbb44e30SEd Cashin 	empty_page = NULL;
1866896831f5SEd Cashin }
1867