xref: /openbmc/linux/drivers/block/aoe/aoecmd.c (revision 5bd8e16d)
1 /* Copyright (c) 2013 Coraid, Inc.  See COPYING for GPL terms. */
2 /*
3  * aoecmd.c
4  * Filesystem request handling methods
5  */
6 
7 #include <linux/ata.h>
8 #include <linux/slab.h>
9 #include <linux/hdreg.h>
10 #include <linux/blkdev.h>
11 #include <linux/skbuff.h>
12 #include <linux/netdevice.h>
13 #include <linux/genhd.h>
14 #include <linux/moduleparam.h>
15 #include <linux/workqueue.h>
16 #include <linux/kthread.h>
17 #include <net/net_namespace.h>
18 #include <asm/unaligned.h>
19 #include <linux/uio.h>
20 #include "aoe.h"
21 
22 #define MAXIOC (8192)	/* default meant to avoid most soft lockups */
23 
24 static void ktcomplete(struct frame *, struct sk_buff *);
25 static int count_targets(struct aoedev *d, int *untainted);
26 
27 static struct buf *nextbuf(struct aoedev *);
28 
29 static int aoe_deadsecs = 60 * 3;
30 module_param(aoe_deadsecs, int, 0644);
31 MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
32 
33 static int aoe_maxout = 64;
34 module_param(aoe_maxout, int, 0644);
35 MODULE_PARM_DESC(aoe_maxout,
36 	"Only aoe_maxout outstanding packets for every MAC on eX.Y.");
37 
38 /* The number of online cpus during module initialization gives us a
39  * convenient heuristic cap on the parallelism used for ktio threads
40  * doing I/O completion.  It is not important that the cap equal the
41  * actual number of running CPUs at any given time, but because of CPU
42  * hotplug, we take care to use ncpus instead of using
43  * num_online_cpus() after module initialization.
44  */
45 static int ncpus;
46 
47 /* mutex lock used for synchronization while thread spawning */
48 static DEFINE_MUTEX(ktio_spawn_lock);
49 
50 static wait_queue_head_t *ktiowq;
51 static struct ktstate *kts;
52 
53 /* io completion queue */
54 struct iocq_ktio {
55 	struct list_head head;
56 	spinlock_t lock;
57 };
58 static struct iocq_ktio *iocq;
59 
60 static struct page *empty_page;
61 
62 static struct sk_buff *
63 new_skb(ulong len)
64 {
65 	struct sk_buff *skb;
66 
67 	skb = alloc_skb(len + MAX_HEADER, GFP_ATOMIC);
68 	if (skb) {
69 		skb_reserve(skb, MAX_HEADER);
70 		skb_reset_mac_header(skb);
71 		skb_reset_network_header(skb);
72 		skb->protocol = __constant_htons(ETH_P_AOE);
73 		skb_checksum_none_assert(skb);
74 	}
75 	return skb;
76 }
77 
78 static struct frame *
79 getframe_deferred(struct aoedev *d, u32 tag)
80 {
81 	struct list_head *head, *pos, *nx;
82 	struct frame *f;
83 
84 	head = &d->rexmitq;
85 	list_for_each_safe(pos, nx, head) {
86 		f = list_entry(pos, struct frame, head);
87 		if (f->tag == tag) {
88 			list_del(pos);
89 			return f;
90 		}
91 	}
92 	return NULL;
93 }
94 
95 static struct frame *
96 getframe(struct aoedev *d, u32 tag)
97 {
98 	struct frame *f;
99 	struct list_head *head, *pos, *nx;
100 	u32 n;
101 
102 	n = tag % NFACTIVE;
103 	head = &d->factive[n];
104 	list_for_each_safe(pos, nx, head) {
105 		f = list_entry(pos, struct frame, head);
106 		if (f->tag == tag) {
107 			list_del(pos);
108 			return f;
109 		}
110 	}
111 	return NULL;
112 }
113 
114 /*
115  * Leave the top bit clear so we have tagspace for userland.
116  * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
117  * This driver reserves tag -1 to mean "unused frame."
118  */
119 static int
120 newtag(struct aoedev *d)
121 {
122 	register ulong n;
123 
124 	n = jiffies & 0xffff;
125 	return n |= (++d->lasttag & 0x7fff) << 16;
126 }
127 
128 static u32
129 aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
130 {
131 	u32 host_tag = newtag(d);
132 
133 	memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
134 	memcpy(h->dst, t->addr, sizeof h->dst);
135 	h->type = __constant_cpu_to_be16(ETH_P_AOE);
136 	h->verfl = AOE_HVER;
137 	h->major = cpu_to_be16(d->aoemajor);
138 	h->minor = d->aoeminor;
139 	h->cmd = AOECMD_ATA;
140 	h->tag = cpu_to_be32(host_tag);
141 
142 	return host_tag;
143 }
144 
145 static inline void
146 put_lba(struct aoe_atahdr *ah, sector_t lba)
147 {
148 	ah->lba0 = lba;
149 	ah->lba1 = lba >>= 8;
150 	ah->lba2 = lba >>= 8;
151 	ah->lba3 = lba >>= 8;
152 	ah->lba4 = lba >>= 8;
153 	ah->lba5 = lba >>= 8;
154 }
155 
156 static struct aoeif *
157 ifrotate(struct aoetgt *t)
158 {
159 	struct aoeif *ifp;
160 
161 	ifp = t->ifp;
162 	ifp++;
163 	if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL)
164 		ifp = t->ifs;
165 	if (ifp->nd == NULL)
166 		return NULL;
167 	return t->ifp = ifp;
168 }
169 
170 static void
171 skb_pool_put(struct aoedev *d, struct sk_buff *skb)
172 {
173 	__skb_queue_tail(&d->skbpool, skb);
174 }
175 
176 static struct sk_buff *
177 skb_pool_get(struct aoedev *d)
178 {
179 	struct sk_buff *skb = skb_peek(&d->skbpool);
180 
181 	if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
182 		__skb_unlink(skb, &d->skbpool);
183 		return skb;
184 	}
185 	if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
186 	    (skb = new_skb(ETH_ZLEN)))
187 		return skb;
188 
189 	return NULL;
190 }
191 
192 void
193 aoe_freetframe(struct frame *f)
194 {
195 	struct aoetgt *t;
196 
197 	t = f->t;
198 	f->buf = NULL;
199 	f->lba = 0;
200 	f->bv = NULL;
201 	f->r_skb = NULL;
202 	f->flags = 0;
203 	list_add(&f->head, &t->ffree);
204 }
205 
206 static struct frame *
207 newtframe(struct aoedev *d, struct aoetgt *t)
208 {
209 	struct frame *f;
210 	struct sk_buff *skb;
211 	struct list_head *pos;
212 
213 	if (list_empty(&t->ffree)) {
214 		if (t->falloc >= NSKBPOOLMAX*2)
215 			return NULL;
216 		f = kcalloc(1, sizeof(*f), GFP_ATOMIC);
217 		if (f == NULL)
218 			return NULL;
219 		t->falloc++;
220 		f->t = t;
221 	} else {
222 		pos = t->ffree.next;
223 		list_del(pos);
224 		f = list_entry(pos, struct frame, head);
225 	}
226 
227 	skb = f->skb;
228 	if (skb == NULL) {
229 		f->skb = skb = new_skb(ETH_ZLEN);
230 		if (!skb) {
231 bail:			aoe_freetframe(f);
232 			return NULL;
233 		}
234 	}
235 
236 	if (atomic_read(&skb_shinfo(skb)->dataref) != 1) {
237 		skb = skb_pool_get(d);
238 		if (skb == NULL)
239 			goto bail;
240 		skb_pool_put(d, f->skb);
241 		f->skb = skb;
242 	}
243 
244 	skb->truesize -= skb->data_len;
245 	skb_shinfo(skb)->nr_frags = skb->data_len = 0;
246 	skb_trim(skb, 0);
247 	return f;
248 }
249 
250 static struct frame *
251 newframe(struct aoedev *d)
252 {
253 	struct frame *f;
254 	struct aoetgt *t, **tt;
255 	int totout = 0;
256 	int use_tainted;
257 	int has_untainted;
258 
259 	if (!d->targets || !d->targets[0]) {
260 		printk(KERN_ERR "aoe: NULL TARGETS!\n");
261 		return NULL;
262 	}
263 	tt = d->tgt;	/* last used target */
264 	for (use_tainted = 0, has_untainted = 0;;) {
265 		tt++;
266 		if (tt >= &d->targets[d->ntargets] || !*tt)
267 			tt = d->targets;
268 		t = *tt;
269 		if (!t->taint) {
270 			has_untainted = 1;
271 			totout += t->nout;
272 		}
273 		if (t->nout < t->maxout
274 		&& (use_tainted || !t->taint)
275 		&& t->ifp->nd) {
276 			f = newtframe(d, t);
277 			if (f) {
278 				ifrotate(t);
279 				d->tgt = tt;
280 				return f;
281 			}
282 		}
283 		if (tt == d->tgt) {	/* we've looped and found nada */
284 			if (!use_tainted && !has_untainted)
285 				use_tainted = 1;
286 			else
287 				break;
288 		}
289 	}
290 	if (totout == 0) {
291 		d->kicked++;
292 		d->flags |= DEVFL_KICKME;
293 	}
294 	return NULL;
295 }
296 
297 static void
298 skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt)
299 {
300 	int frag = 0;
301 	ulong fcnt;
302 loop:
303 	fcnt = bv->bv_len - (off - bv->bv_offset);
304 	if (fcnt > cnt)
305 		fcnt = cnt;
306 	skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt);
307 	cnt -= fcnt;
308 	if (cnt <= 0)
309 		return;
310 	bv++;
311 	off = bv->bv_offset;
312 	goto loop;
313 }
314 
315 static void
316 fhash(struct frame *f)
317 {
318 	struct aoedev *d = f->t->d;
319 	u32 n;
320 
321 	n = f->tag % NFACTIVE;
322 	list_add_tail(&f->head, &d->factive[n]);
323 }
324 
325 static void
326 ata_rw_frameinit(struct frame *f)
327 {
328 	struct aoetgt *t;
329 	struct aoe_hdr *h;
330 	struct aoe_atahdr *ah;
331 	struct sk_buff *skb;
332 	char writebit, extbit;
333 
334 	skb = f->skb;
335 	h = (struct aoe_hdr *) skb_mac_header(skb);
336 	ah = (struct aoe_atahdr *) (h + 1);
337 	skb_put(skb, sizeof(*h) + sizeof(*ah));
338 	memset(h, 0, skb->len);
339 
340 	writebit = 0x10;
341 	extbit = 0x4;
342 
343 	t = f->t;
344 	f->tag = aoehdr_atainit(t->d, t, h);
345 	fhash(f);
346 	t->nout++;
347 	f->waited = 0;
348 	f->waited_total = 0;
349 	if (f->buf)
350 		f->lba = f->buf->sector;
351 
352 	/* set up ata header */
353 	ah->scnt = f->bcnt >> 9;
354 	put_lba(ah, f->lba);
355 	if (t->d->flags & DEVFL_EXT) {
356 		ah->aflags |= AOEAFL_EXT;
357 	} else {
358 		extbit = 0;
359 		ah->lba3 &= 0x0f;
360 		ah->lba3 |= 0xe0;	/* LBA bit + obsolete 0xa0 */
361 	}
362 	if (f->buf && bio_data_dir(f->buf->bio) == WRITE) {
363 		skb_fillup(skb, f->bv, f->bv_off, f->bcnt);
364 		ah->aflags |= AOEAFL_WRITE;
365 		skb->len += f->bcnt;
366 		skb->data_len = f->bcnt;
367 		skb->truesize += f->bcnt;
368 		t->wpkts++;
369 	} else {
370 		t->rpkts++;
371 		writebit = 0;
372 	}
373 
374 	ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
375 	skb->dev = t->ifp->nd;
376 }
377 
378 static int
379 aoecmd_ata_rw(struct aoedev *d)
380 {
381 	struct frame *f;
382 	struct buf *buf;
383 	struct sk_buff *skb;
384 	struct sk_buff_head queue;
385 	ulong bcnt, fbcnt;
386 
387 	buf = nextbuf(d);
388 	if (buf == NULL)
389 		return 0;
390 	f = newframe(d);
391 	if (f == NULL)
392 		return 0;
393 	bcnt = d->maxbcnt;
394 	if (bcnt == 0)
395 		bcnt = DEFAULTBCNT;
396 	if (bcnt > buf->resid)
397 		bcnt = buf->resid;
398 	fbcnt = bcnt;
399 	f->bv = buf->bv;
400 	f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
401 	do {
402 		if (fbcnt < buf->bv_resid) {
403 			buf->bv_resid -= fbcnt;
404 			buf->resid -= fbcnt;
405 			break;
406 		}
407 		fbcnt -= buf->bv_resid;
408 		buf->resid -= buf->bv_resid;
409 		if (buf->resid == 0) {
410 			d->ip.buf = NULL;
411 			break;
412 		}
413 		buf->bv++;
414 		buf->bv_resid = buf->bv->bv_len;
415 		WARN_ON(buf->bv_resid == 0);
416 	} while (fbcnt);
417 
418 	/* initialize the headers & frame */
419 	f->buf = buf;
420 	f->bcnt = bcnt;
421 	ata_rw_frameinit(f);
422 
423 	/* mark all tracking fields and load out */
424 	buf->nframesout += 1;
425 	buf->sector += bcnt >> 9;
426 
427 	skb = skb_clone(f->skb, GFP_ATOMIC);
428 	if (skb) {
429 		do_gettimeofday(&f->sent);
430 		f->sent_jiffs = (u32) jiffies;
431 		__skb_queue_head_init(&queue);
432 		__skb_queue_tail(&queue, skb);
433 		aoenet_xmit(&queue);
434 	}
435 	return 1;
436 }
437 
438 /* some callers cannot sleep, and they can call this function,
439  * transmitting the packets later, when interrupts are on
440  */
441 static void
442 aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
443 {
444 	struct aoe_hdr *h;
445 	struct aoe_cfghdr *ch;
446 	struct sk_buff *skb;
447 	struct net_device *ifp;
448 
449 	rcu_read_lock();
450 	for_each_netdev_rcu(&init_net, ifp) {
451 		dev_hold(ifp);
452 		if (!is_aoe_netif(ifp))
453 			goto cont;
454 
455 		skb = new_skb(sizeof *h + sizeof *ch);
456 		if (skb == NULL) {
457 			printk(KERN_INFO "aoe: skb alloc failure\n");
458 			goto cont;
459 		}
460 		skb_put(skb, sizeof *h + sizeof *ch);
461 		skb->dev = ifp;
462 		__skb_queue_tail(queue, skb);
463 		h = (struct aoe_hdr *) skb_mac_header(skb);
464 		memset(h, 0, sizeof *h + sizeof *ch);
465 
466 		memset(h->dst, 0xff, sizeof h->dst);
467 		memcpy(h->src, ifp->dev_addr, sizeof h->src);
468 		h->type = __constant_cpu_to_be16(ETH_P_AOE);
469 		h->verfl = AOE_HVER;
470 		h->major = cpu_to_be16(aoemajor);
471 		h->minor = aoeminor;
472 		h->cmd = AOECMD_CFG;
473 
474 cont:
475 		dev_put(ifp);
476 	}
477 	rcu_read_unlock();
478 }
479 
480 static void
481 resend(struct aoedev *d, struct frame *f)
482 {
483 	struct sk_buff *skb;
484 	struct sk_buff_head queue;
485 	struct aoe_hdr *h;
486 	struct aoetgt *t;
487 	char buf[128];
488 	u32 n;
489 
490 	t = f->t;
491 	n = newtag(d);
492 	skb = f->skb;
493 	if (ifrotate(t) == NULL) {
494 		/* probably can't happen, but set it up to fail anyway */
495 		pr_info("aoe: resend: no interfaces to rotate to.\n");
496 		ktcomplete(f, NULL);
497 		return;
498 	}
499 	h = (struct aoe_hdr *) skb_mac_header(skb);
500 
501 	if (!(f->flags & FFL_PROBE)) {
502 		snprintf(buf, sizeof(buf),
503 			"%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
504 			"retransmit", d->aoemajor, d->aoeminor,
505 			f->tag, jiffies, n,
506 			h->src, h->dst, t->nout);
507 		aoechr_error(buf);
508 	}
509 
510 	f->tag = n;
511 	fhash(f);
512 	h->tag = cpu_to_be32(n);
513 	memcpy(h->dst, t->addr, sizeof h->dst);
514 	memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
515 
516 	skb->dev = t->ifp->nd;
517 	skb = skb_clone(skb, GFP_ATOMIC);
518 	if (skb == NULL)
519 		return;
520 	do_gettimeofday(&f->sent);
521 	f->sent_jiffs = (u32) jiffies;
522 	__skb_queue_head_init(&queue);
523 	__skb_queue_tail(&queue, skb);
524 	aoenet_xmit(&queue);
525 }
526 
527 static int
528 tsince_hr(struct frame *f)
529 {
530 	struct timeval now;
531 	int n;
532 
533 	do_gettimeofday(&now);
534 	n = now.tv_usec - f->sent.tv_usec;
535 	n += (now.tv_sec - f->sent.tv_sec) * USEC_PER_SEC;
536 
537 	if (n < 0)
538 		n = -n;
539 
540 	/* For relatively long periods, use jiffies to avoid
541 	 * discrepancies caused by updates to the system time.
542 	 *
543 	 * On system with HZ of 1000, 32-bits is over 49 days
544 	 * worth of jiffies, or over 71 minutes worth of usecs.
545 	 *
546 	 * Jiffies overflow is handled by subtraction of unsigned ints:
547 	 * (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe
548 	 * $3 = 4
549 	 * (gdb)
550 	 */
551 	if (n > USEC_PER_SEC / 4) {
552 		n = ((u32) jiffies) - f->sent_jiffs;
553 		n *= USEC_PER_SEC / HZ;
554 	}
555 
556 	return n;
557 }
558 
559 static int
560 tsince(u32 tag)
561 {
562 	int n;
563 
564 	n = jiffies & 0xffff;
565 	n -= tag & 0xffff;
566 	if (n < 0)
567 		n += 1<<16;
568 	return jiffies_to_usecs(n + 1);
569 }
570 
571 static struct aoeif *
572 getif(struct aoetgt *t, struct net_device *nd)
573 {
574 	struct aoeif *p, *e;
575 
576 	p = t->ifs;
577 	e = p + NAOEIFS;
578 	for (; p < e; p++)
579 		if (p->nd == nd)
580 			return p;
581 	return NULL;
582 }
583 
584 static void
585 ejectif(struct aoetgt *t, struct aoeif *ifp)
586 {
587 	struct aoeif *e;
588 	struct net_device *nd;
589 	ulong n;
590 
591 	nd = ifp->nd;
592 	e = t->ifs + NAOEIFS - 1;
593 	n = (e - ifp) * sizeof *ifp;
594 	memmove(ifp, ifp+1, n);
595 	e->nd = NULL;
596 	dev_put(nd);
597 }
598 
599 static struct frame *
600 reassign_frame(struct frame *f)
601 {
602 	struct frame *nf;
603 	struct sk_buff *skb;
604 
605 	nf = newframe(f->t->d);
606 	if (!nf)
607 		return NULL;
608 	if (nf->t == f->t) {
609 		aoe_freetframe(nf);
610 		return NULL;
611 	}
612 
613 	skb = nf->skb;
614 	nf->skb = f->skb;
615 	nf->buf = f->buf;
616 	nf->bcnt = f->bcnt;
617 	nf->lba = f->lba;
618 	nf->bv = f->bv;
619 	nf->bv_off = f->bv_off;
620 	nf->waited = 0;
621 	nf->waited_total = f->waited_total;
622 	nf->sent = f->sent;
623 	nf->sent_jiffs = f->sent_jiffs;
624 	f->skb = skb;
625 
626 	return nf;
627 }
628 
629 static void
630 probe(struct aoetgt *t)
631 {
632 	struct aoedev *d;
633 	struct frame *f;
634 	struct sk_buff *skb;
635 	struct sk_buff_head queue;
636 	size_t n, m;
637 	int frag;
638 
639 	d = t->d;
640 	f = newtframe(d, t);
641 	if (!f) {
642 		pr_err("%s %pm for e%ld.%d: %s\n",
643 			"aoe: cannot probe remote address",
644 			t->addr,
645 			(long) d->aoemajor, d->aoeminor,
646 			"no frame available");
647 		return;
648 	}
649 	f->flags |= FFL_PROBE;
650 	ifrotate(t);
651 	f->bcnt = t->d->maxbcnt ? t->d->maxbcnt : DEFAULTBCNT;
652 	ata_rw_frameinit(f);
653 	skb = f->skb;
654 	for (frag = 0, n = f->bcnt; n > 0; ++frag, n -= m) {
655 		if (n < PAGE_SIZE)
656 			m = n;
657 		else
658 			m = PAGE_SIZE;
659 		skb_fill_page_desc(skb, frag, empty_page, 0, m);
660 	}
661 	skb->len += f->bcnt;
662 	skb->data_len = f->bcnt;
663 	skb->truesize += f->bcnt;
664 
665 	skb = skb_clone(f->skb, GFP_ATOMIC);
666 	if (skb) {
667 		do_gettimeofday(&f->sent);
668 		f->sent_jiffs = (u32) jiffies;
669 		__skb_queue_head_init(&queue);
670 		__skb_queue_tail(&queue, skb);
671 		aoenet_xmit(&queue);
672 	}
673 }
674 
675 static long
676 rto(struct aoedev *d)
677 {
678 	long t;
679 
680 	t = 2 * d->rttavg >> RTTSCALE;
681 	t += 8 * d->rttdev >> RTTDSCALE;
682 	if (t == 0)
683 		t = 1;
684 
685 	return t;
686 }
687 
688 static void
689 rexmit_deferred(struct aoedev *d)
690 {
691 	struct aoetgt *t;
692 	struct frame *f;
693 	struct frame *nf;
694 	struct list_head *pos, *nx, *head;
695 	int since;
696 	int untainted;
697 
698 	count_targets(d, &untainted);
699 
700 	head = &d->rexmitq;
701 	list_for_each_safe(pos, nx, head) {
702 		f = list_entry(pos, struct frame, head);
703 		t = f->t;
704 		if (t->taint) {
705 			if (!(f->flags & FFL_PROBE)) {
706 				nf = reassign_frame(f);
707 				if (nf) {
708 					if (t->nout_probes == 0
709 					&& untainted > 0) {
710 						probe(t);
711 						t->nout_probes++;
712 					}
713 					list_replace(&f->head, &nf->head);
714 					pos = &nf->head;
715 					aoe_freetframe(f);
716 					f = nf;
717 					t = f->t;
718 				}
719 			} else if (untainted < 1) {
720 				/* don't probe w/o other untainted aoetgts */
721 				goto stop_probe;
722 			} else if (tsince_hr(f) < t->taint * rto(d)) {
723 				/* reprobe slowly when taint is high */
724 				continue;
725 			}
726 		} else if (f->flags & FFL_PROBE) {
727 stop_probe:		/* don't probe untainted aoetgts */
728 			list_del(pos);
729 			aoe_freetframe(f);
730 			/* leaving d->kicked, because this is routine */
731 			f->t->d->flags |= DEVFL_KICKME;
732 			continue;
733 		}
734 		if (t->nout >= t->maxout)
735 			continue;
736 		list_del(pos);
737 		t->nout++;
738 		if (f->flags & FFL_PROBE)
739 			t->nout_probes++;
740 		since = tsince_hr(f);
741 		f->waited += since;
742 		f->waited_total += since;
743 		resend(d, f);
744 	}
745 }
746 
747 /* An aoetgt accumulates demerits quickly, and successful
748  * probing redeems the aoetgt slowly.
749  */
750 static void
751 scorn(struct aoetgt *t)
752 {
753 	int n;
754 
755 	n = t->taint++;
756 	t->taint += t->taint * 2;
757 	if (n > t->taint)
758 		t->taint = n;
759 	if (t->taint > MAX_TAINT)
760 		t->taint = MAX_TAINT;
761 }
762 
763 static int
764 count_targets(struct aoedev *d, int *untainted)
765 {
766 	int i, good;
767 
768 	for (i = good = 0; i < d->ntargets && d->targets[i]; ++i)
769 		if (d->targets[i]->taint == 0)
770 			good++;
771 
772 	if (untainted)
773 		*untainted = good;
774 	return i;
775 }
776 
777 static void
778 rexmit_timer(ulong vp)
779 {
780 	struct aoedev *d;
781 	struct aoetgt *t;
782 	struct aoeif *ifp;
783 	struct frame *f;
784 	struct list_head *head, *pos, *nx;
785 	LIST_HEAD(flist);
786 	register long timeout;
787 	ulong flags, n;
788 	int i;
789 	int utgts;	/* number of aoetgt descriptors (not slots) */
790 	int since;
791 
792 	d = (struct aoedev *) vp;
793 
794 	spin_lock_irqsave(&d->lock, flags);
795 
796 	/* timeout based on observed timings and variations */
797 	timeout = rto(d);
798 
799 	utgts = count_targets(d, NULL);
800 
801 	if (d->flags & DEVFL_TKILL) {
802 		spin_unlock_irqrestore(&d->lock, flags);
803 		return;
804 	}
805 
806 	/* collect all frames to rexmit into flist */
807 	for (i = 0; i < NFACTIVE; i++) {
808 		head = &d->factive[i];
809 		list_for_each_safe(pos, nx, head) {
810 			f = list_entry(pos, struct frame, head);
811 			if (tsince_hr(f) < timeout)
812 				break;	/* end of expired frames */
813 			/* move to flist for later processing */
814 			list_move_tail(pos, &flist);
815 		}
816 	}
817 
818 	/* process expired frames */
819 	while (!list_empty(&flist)) {
820 		pos = flist.next;
821 		f = list_entry(pos, struct frame, head);
822 		since = tsince_hr(f);
823 		n = f->waited_total + since;
824 		n /= USEC_PER_SEC;
825 		if (aoe_deadsecs
826 		&& n > aoe_deadsecs
827 		&& !(f->flags & FFL_PROBE)) {
828 			/* Waited too long.  Device failure.
829 			 * Hang all frames on first hash bucket for downdev
830 			 * to clean up.
831 			 */
832 			list_splice(&flist, &d->factive[0]);
833 			aoedev_downdev(d);
834 			goto out;
835 		}
836 
837 		t = f->t;
838 		n = f->waited + since;
839 		n /= USEC_PER_SEC;
840 		if (aoe_deadsecs && utgts > 0
841 		&& (n > aoe_deadsecs / utgts || n > HARD_SCORN_SECS))
842 			scorn(t); /* avoid this target */
843 
844 		if (t->maxout != 1) {
845 			t->ssthresh = t->maxout / 2;
846 			t->maxout = 1;
847 		}
848 
849 		if (f->flags & FFL_PROBE) {
850 			t->nout_probes--;
851 		} else {
852 			ifp = getif(t, f->skb->dev);
853 			if (ifp && ++ifp->lost > (t->nframes << 1)
854 			&& (ifp != t->ifs || t->ifs[1].nd)) {
855 				ejectif(t, ifp);
856 				ifp = NULL;
857 			}
858 		}
859 		list_move_tail(pos, &d->rexmitq);
860 		t->nout--;
861 	}
862 	rexmit_deferred(d);
863 
864 out:
865 	if ((d->flags & DEVFL_KICKME) && d->blkq) {
866 		d->flags &= ~DEVFL_KICKME;
867 		d->blkq->request_fn(d->blkq);
868 	}
869 
870 	d->timer.expires = jiffies + TIMERTICK;
871 	add_timer(&d->timer);
872 
873 	spin_unlock_irqrestore(&d->lock, flags);
874 }
875 
876 static unsigned long
877 rqbiocnt(struct request *r)
878 {
879 	struct bio *bio;
880 	unsigned long n = 0;
881 
882 	__rq_for_each_bio(bio, r)
883 		n++;
884 	return n;
885 }
886 
887 /* This can be removed if we are certain that no users of the block
888  * layer will ever use zero-count pages in bios.  Otherwise we have to
889  * protect against the put_page sometimes done by the network layer.
890  *
891  * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
892  * discussion.
893  *
894  * We cannot use get_page in the workaround, because it insists on a
895  * positive page count as a precondition.  So we use _count directly.
896  */
897 static void
898 bio_pageinc(struct bio *bio)
899 {
900 	struct bio_vec *bv;
901 	struct page *page;
902 	int i;
903 
904 	bio_for_each_segment(bv, bio, i) {
905 		/* Non-zero page count for non-head members of
906 		 * compound pages is no longer allowed by the kernel.
907 		 */
908 		page = compound_trans_head(bv->bv_page);
909 		atomic_inc(&page->_count);
910 	}
911 }
912 
913 static void
914 bio_pagedec(struct bio *bio)
915 {
916 	struct bio_vec *bv;
917 	struct page *page;
918 	int i;
919 
920 	bio_for_each_segment(bv, bio, i) {
921 		page = compound_trans_head(bv->bv_page);
922 		atomic_dec(&page->_count);
923 	}
924 }
925 
926 static void
927 bufinit(struct buf *buf, struct request *rq, struct bio *bio)
928 {
929 	memset(buf, 0, sizeof(*buf));
930 	buf->rq = rq;
931 	buf->bio = bio;
932 	buf->resid = bio->bi_size;
933 	buf->sector = bio->bi_sector;
934 	bio_pageinc(bio);
935 	buf->bv = bio_iovec(bio);
936 	buf->bv_resid = buf->bv->bv_len;
937 	WARN_ON(buf->bv_resid == 0);
938 }
939 
940 static struct buf *
941 nextbuf(struct aoedev *d)
942 {
943 	struct request *rq;
944 	struct request_queue *q;
945 	struct buf *buf;
946 	struct bio *bio;
947 
948 	q = d->blkq;
949 	if (q == NULL)
950 		return NULL;	/* initializing */
951 	if (d->ip.buf)
952 		return d->ip.buf;
953 	rq = d->ip.rq;
954 	if (rq == NULL) {
955 		rq = blk_peek_request(q);
956 		if (rq == NULL)
957 			return NULL;
958 		blk_start_request(rq);
959 		d->ip.rq = rq;
960 		d->ip.nxbio = rq->bio;
961 		rq->special = (void *) rqbiocnt(rq);
962 	}
963 	buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
964 	if (buf == NULL) {
965 		pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
966 		return NULL;
967 	}
968 	bio = d->ip.nxbio;
969 	bufinit(buf, rq, bio);
970 	bio = bio->bi_next;
971 	d->ip.nxbio = bio;
972 	if (bio == NULL)
973 		d->ip.rq = NULL;
974 	return d->ip.buf = buf;
975 }
976 
977 /* enters with d->lock held */
978 void
979 aoecmd_work(struct aoedev *d)
980 {
981 	rexmit_deferred(d);
982 	while (aoecmd_ata_rw(d))
983 		;
984 }
985 
986 /* this function performs work that has been deferred until sleeping is OK
987  */
988 void
989 aoecmd_sleepwork(struct work_struct *work)
990 {
991 	struct aoedev *d = container_of(work, struct aoedev, work);
992 	struct block_device *bd;
993 	u64 ssize;
994 
995 	if (d->flags & DEVFL_GDALLOC)
996 		aoeblk_gdalloc(d);
997 
998 	if (d->flags & DEVFL_NEWSIZE) {
999 		ssize = get_capacity(d->gd);
1000 		bd = bdget_disk(d->gd, 0);
1001 		if (bd) {
1002 			mutex_lock(&bd->bd_inode->i_mutex);
1003 			i_size_write(bd->bd_inode, (loff_t)ssize<<9);
1004 			mutex_unlock(&bd->bd_inode->i_mutex);
1005 			bdput(bd);
1006 		}
1007 		spin_lock_irq(&d->lock);
1008 		d->flags |= DEVFL_UP;
1009 		d->flags &= ~DEVFL_NEWSIZE;
1010 		spin_unlock_irq(&d->lock);
1011 	}
1012 }
1013 
1014 static void
1015 ata_ident_fixstring(u16 *id, int ns)
1016 {
1017 	u16 s;
1018 
1019 	while (ns-- > 0) {
1020 		s = *id;
1021 		*id++ = s >> 8 | s << 8;
1022 	}
1023 }
1024 
1025 static void
1026 ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
1027 {
1028 	u64 ssize;
1029 	u16 n;
1030 
1031 	/* word 83: command set supported */
1032 	n = get_unaligned_le16(&id[83 << 1]);
1033 
1034 	/* word 86: command set/feature enabled */
1035 	n |= get_unaligned_le16(&id[86 << 1]);
1036 
1037 	if (n & (1<<10)) {	/* bit 10: LBA 48 */
1038 		d->flags |= DEVFL_EXT;
1039 
1040 		/* word 100: number lba48 sectors */
1041 		ssize = get_unaligned_le64(&id[100 << 1]);
1042 
1043 		/* set as in ide-disk.c:init_idedisk_capacity */
1044 		d->geo.cylinders = ssize;
1045 		d->geo.cylinders /= (255 * 63);
1046 		d->geo.heads = 255;
1047 		d->geo.sectors = 63;
1048 	} else {
1049 		d->flags &= ~DEVFL_EXT;
1050 
1051 		/* number lba28 sectors */
1052 		ssize = get_unaligned_le32(&id[60 << 1]);
1053 
1054 		/* NOTE: obsolete in ATA 6 */
1055 		d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
1056 		d->geo.heads = get_unaligned_le16(&id[55 << 1]);
1057 		d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
1058 	}
1059 
1060 	ata_ident_fixstring((u16 *) &id[10<<1], 10);	/* serial */
1061 	ata_ident_fixstring((u16 *) &id[23<<1], 4);	/* firmware */
1062 	ata_ident_fixstring((u16 *) &id[27<<1], 20);	/* model */
1063 	memcpy(d->ident, id, sizeof(d->ident));
1064 
1065 	if (d->ssize != ssize)
1066 		printk(KERN_INFO
1067 			"aoe: %pm e%ld.%d v%04x has %llu sectors\n",
1068 			t->addr,
1069 			d->aoemajor, d->aoeminor,
1070 			d->fw_ver, (long long)ssize);
1071 	d->ssize = ssize;
1072 	d->geo.start = 0;
1073 	if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
1074 		return;
1075 	if (d->gd != NULL) {
1076 		set_capacity(d->gd, ssize);
1077 		d->flags |= DEVFL_NEWSIZE;
1078 	} else
1079 		d->flags |= DEVFL_GDALLOC;
1080 	schedule_work(&d->work);
1081 }
1082 
1083 static void
1084 calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt)
1085 {
1086 	register long n;
1087 
1088 	n = rtt;
1089 
1090 	/* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */
1091 	n -= d->rttavg >> RTTSCALE;
1092 	d->rttavg += n;
1093 	if (n < 0)
1094 		n = -n;
1095 	n -= d->rttdev >> RTTDSCALE;
1096 	d->rttdev += n;
1097 
1098 	if (!t || t->maxout >= t->nframes)
1099 		return;
1100 	if (t->maxout < t->ssthresh)
1101 		t->maxout += 1;
1102 	else if (t->nout == t->maxout && t->next_cwnd-- == 0) {
1103 		t->maxout += 1;
1104 		t->next_cwnd = t->maxout;
1105 	}
1106 }
1107 
1108 static struct aoetgt *
1109 gettgt(struct aoedev *d, char *addr)
1110 {
1111 	struct aoetgt **t, **e;
1112 
1113 	t = d->targets;
1114 	e = t + d->ntargets;
1115 	for (; t < e && *t; t++)
1116 		if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
1117 			return *t;
1118 	return NULL;
1119 }
1120 
1121 static void
1122 bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
1123 {
1124 	ulong fcnt;
1125 	char *p;
1126 	int soff = 0;
1127 loop:
1128 	fcnt = bv->bv_len - (off - bv->bv_offset);
1129 	if (fcnt > cnt)
1130 		fcnt = cnt;
1131 	p = page_address(bv->bv_page) + off;
1132 	skb_copy_bits(skb, soff, p, fcnt);
1133 	soff += fcnt;
1134 	cnt -= fcnt;
1135 	if (cnt <= 0)
1136 		return;
1137 	bv++;
1138 	off = bv->bv_offset;
1139 	goto loop;
1140 }
1141 
1142 void
1143 aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
1144 {
1145 	struct bio *bio;
1146 	int bok;
1147 	struct request_queue *q;
1148 
1149 	q = d->blkq;
1150 	if (rq == d->ip.rq)
1151 		d->ip.rq = NULL;
1152 	do {
1153 		bio = rq->bio;
1154 		bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
1155 	} while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
1156 
1157 	/* cf. http://lkml.org/lkml/2006/10/31/28 */
1158 	if (!fastfail)
1159 		__blk_run_queue(q);
1160 }
1161 
1162 static void
1163 aoe_end_buf(struct aoedev *d, struct buf *buf)
1164 {
1165 	struct request *rq;
1166 	unsigned long n;
1167 
1168 	if (buf == d->ip.buf)
1169 		d->ip.buf = NULL;
1170 	rq = buf->rq;
1171 	bio_pagedec(buf->bio);
1172 	mempool_free(buf, d->bufpool);
1173 	n = (unsigned long) rq->special;
1174 	rq->special = (void *) --n;
1175 	if (n == 0)
1176 		aoe_end_request(d, rq, 0);
1177 }
1178 
1179 static void
1180 ktiocomplete(struct frame *f)
1181 {
1182 	struct aoe_hdr *hin, *hout;
1183 	struct aoe_atahdr *ahin, *ahout;
1184 	struct buf *buf;
1185 	struct sk_buff *skb;
1186 	struct aoetgt *t;
1187 	struct aoeif *ifp;
1188 	struct aoedev *d;
1189 	long n;
1190 	int untainted;
1191 
1192 	if (f == NULL)
1193 		return;
1194 
1195 	t = f->t;
1196 	d = t->d;
1197 	skb = f->r_skb;
1198 	buf = f->buf;
1199 	if (f->flags & FFL_PROBE)
1200 		goto out;
1201 	if (!skb)		/* just fail the buf. */
1202 		goto noskb;
1203 
1204 	hout = (struct aoe_hdr *) skb_mac_header(f->skb);
1205 	ahout = (struct aoe_atahdr *) (hout+1);
1206 
1207 	hin = (struct aoe_hdr *) skb->data;
1208 	skb_pull(skb, sizeof(*hin));
1209 	ahin = (struct aoe_atahdr *) skb->data;
1210 	skb_pull(skb, sizeof(*ahin));
1211 	if (ahin->cmdstat & 0xa9) {	/* these bits cleared on success */
1212 		pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
1213 			ahout->cmdstat, ahin->cmdstat,
1214 			d->aoemajor, d->aoeminor);
1215 noskb:		if (buf)
1216 			clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1217 		goto out;
1218 	}
1219 
1220 	n = ahout->scnt << 9;
1221 	switch (ahout->cmdstat) {
1222 	case ATA_CMD_PIO_READ:
1223 	case ATA_CMD_PIO_READ_EXT:
1224 		if (skb->len < n) {
1225 			pr_err("%s e%ld.%d.  skb->len=%d need=%ld\n",
1226 				"aoe: runt data size in read from",
1227 				(long) d->aoemajor, d->aoeminor,
1228 			       skb->len, n);
1229 			clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1230 			break;
1231 		}
1232 		bvcpy(f->bv, f->bv_off, skb, n);
1233 	case ATA_CMD_PIO_WRITE:
1234 	case ATA_CMD_PIO_WRITE_EXT:
1235 		spin_lock_irq(&d->lock);
1236 		ifp = getif(t, skb->dev);
1237 		if (ifp)
1238 			ifp->lost = 0;
1239 		spin_unlock_irq(&d->lock);
1240 		break;
1241 	case ATA_CMD_ID_ATA:
1242 		if (skb->len < 512) {
1243 			pr_info("%s e%ld.%d.  skb->len=%d need=512\n",
1244 				"aoe: runt data size in ataid from",
1245 				(long) d->aoemajor, d->aoeminor,
1246 				skb->len);
1247 			break;
1248 		}
1249 		if (skb_linearize(skb))
1250 			break;
1251 		spin_lock_irq(&d->lock);
1252 		ataid_complete(d, t, skb->data);
1253 		spin_unlock_irq(&d->lock);
1254 		break;
1255 	default:
1256 		pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
1257 			ahout->cmdstat,
1258 			be16_to_cpu(get_unaligned(&hin->major)),
1259 			hin->minor);
1260 	}
1261 out:
1262 	spin_lock_irq(&d->lock);
1263 	if (t->taint > 0
1264 	&& --t->taint > 0
1265 	&& t->nout_probes == 0) {
1266 		count_targets(d, &untainted);
1267 		if (untainted > 0) {
1268 			probe(t);
1269 			t->nout_probes++;
1270 		}
1271 	}
1272 
1273 	aoe_freetframe(f);
1274 
1275 	if (buf && --buf->nframesout == 0 && buf->resid == 0)
1276 		aoe_end_buf(d, buf);
1277 
1278 	spin_unlock_irq(&d->lock);
1279 	aoedev_put(d);
1280 	dev_kfree_skb(skb);
1281 }
1282 
1283 /* Enters with iocq.lock held.
1284  * Returns true iff responses needing processing remain.
1285  */
1286 static int
1287 ktio(int id)
1288 {
1289 	struct frame *f;
1290 	struct list_head *pos;
1291 	int i;
1292 	int actual_id;
1293 
1294 	for (i = 0; ; ++i) {
1295 		if (i == MAXIOC)
1296 			return 1;
1297 		if (list_empty(&iocq[id].head))
1298 			return 0;
1299 		pos = iocq[id].head.next;
1300 		list_del(pos);
1301 		f = list_entry(pos, struct frame, head);
1302 		spin_unlock_irq(&iocq[id].lock);
1303 		ktiocomplete(f);
1304 
1305 		/* Figure out if extra threads are required. */
1306 		actual_id = f->t->d->aoeminor % ncpus;
1307 
1308 		if (!kts[actual_id].active) {
1309 			BUG_ON(id != 0);
1310 			mutex_lock(&ktio_spawn_lock);
1311 			if (!kts[actual_id].active
1312 				&& aoe_ktstart(&kts[actual_id]) == 0)
1313 				kts[actual_id].active = 1;
1314 			mutex_unlock(&ktio_spawn_lock);
1315 		}
1316 		spin_lock_irq(&iocq[id].lock);
1317 	}
1318 }
1319 
1320 static int
1321 kthread(void *vp)
1322 {
1323 	struct ktstate *k;
1324 	DECLARE_WAITQUEUE(wait, current);
1325 	int more;
1326 
1327 	k = vp;
1328 	current->flags |= PF_NOFREEZE;
1329 	set_user_nice(current, -10);
1330 	complete(&k->rendez);	/* tell spawner we're running */
1331 	do {
1332 		spin_lock_irq(k->lock);
1333 		more = k->fn(k->id);
1334 		if (!more) {
1335 			add_wait_queue(k->waitq, &wait);
1336 			__set_current_state(TASK_INTERRUPTIBLE);
1337 		}
1338 		spin_unlock_irq(k->lock);
1339 		if (!more) {
1340 			schedule();
1341 			remove_wait_queue(k->waitq, &wait);
1342 		} else
1343 			cond_resched();
1344 	} while (!kthread_should_stop());
1345 	complete(&k->rendez);	/* tell spawner we're stopping */
1346 	return 0;
1347 }
1348 
1349 void
1350 aoe_ktstop(struct ktstate *k)
1351 {
1352 	kthread_stop(k->task);
1353 	wait_for_completion(&k->rendez);
1354 }
1355 
1356 int
1357 aoe_ktstart(struct ktstate *k)
1358 {
1359 	struct task_struct *task;
1360 
1361 	init_completion(&k->rendez);
1362 	task = kthread_run(kthread, k, "%s", k->name);
1363 	if (task == NULL || IS_ERR(task))
1364 		return -ENOMEM;
1365 	k->task = task;
1366 	wait_for_completion(&k->rendez); /* allow kthread to start */
1367 	init_completion(&k->rendez);	/* for waiting for exit later */
1368 	return 0;
1369 }
1370 
1371 /* pass it off to kthreads for processing */
1372 static void
1373 ktcomplete(struct frame *f, struct sk_buff *skb)
1374 {
1375 	int id;
1376 	ulong flags;
1377 
1378 	f->r_skb = skb;
1379 	id = f->t->d->aoeminor % ncpus;
1380 	spin_lock_irqsave(&iocq[id].lock, flags);
1381 	if (!kts[id].active) {
1382 		spin_unlock_irqrestore(&iocq[id].lock, flags);
1383 		/* The thread with id has not been spawned yet,
1384 		 * so delegate the work to the main thread and
1385 		 * try spawning a new thread.
1386 		 */
1387 		id = 0;
1388 		spin_lock_irqsave(&iocq[id].lock, flags);
1389 	}
1390 	list_add_tail(&f->head, &iocq[id].head);
1391 	spin_unlock_irqrestore(&iocq[id].lock, flags);
1392 	wake_up(&ktiowq[id]);
1393 }
1394 
1395 struct sk_buff *
1396 aoecmd_ata_rsp(struct sk_buff *skb)
1397 {
1398 	struct aoedev *d;
1399 	struct aoe_hdr *h;
1400 	struct frame *f;
1401 	u32 n;
1402 	ulong flags;
1403 	char ebuf[128];
1404 	u16 aoemajor;
1405 
1406 	h = (struct aoe_hdr *) skb->data;
1407 	aoemajor = be16_to_cpu(get_unaligned(&h->major));
1408 	d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
1409 	if (d == NULL) {
1410 		snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
1411 			"for unknown device %d.%d\n",
1412 			aoemajor, h->minor);
1413 		aoechr_error(ebuf);
1414 		return skb;
1415 	}
1416 
1417 	spin_lock_irqsave(&d->lock, flags);
1418 
1419 	n = be32_to_cpu(get_unaligned(&h->tag));
1420 	f = getframe(d, n);
1421 	if (f) {
1422 		calc_rttavg(d, f->t, tsince_hr(f));
1423 		f->t->nout--;
1424 		if (f->flags & FFL_PROBE)
1425 			f->t->nout_probes--;
1426 	} else {
1427 		f = getframe_deferred(d, n);
1428 		if (f) {
1429 			calc_rttavg(d, NULL, tsince_hr(f));
1430 		} else {
1431 			calc_rttavg(d, NULL, tsince(n));
1432 			spin_unlock_irqrestore(&d->lock, flags);
1433 			aoedev_put(d);
1434 			snprintf(ebuf, sizeof(ebuf),
1435 				 "%15s e%d.%d    tag=%08x@%08lx s=%pm d=%pm\n",
1436 				 "unexpected rsp",
1437 				 get_unaligned_be16(&h->major),
1438 				 h->minor,
1439 				 get_unaligned_be32(&h->tag),
1440 				 jiffies,
1441 				 h->src,
1442 				 h->dst);
1443 			aoechr_error(ebuf);
1444 			return skb;
1445 		}
1446 	}
1447 	aoecmd_work(d);
1448 
1449 	spin_unlock_irqrestore(&d->lock, flags);
1450 
1451 	ktcomplete(f, skb);
1452 
1453 	/*
1454 	 * Note here that we do not perform an aoedev_put, as we are
1455 	 * leaving this reference for the ktio to release.
1456 	 */
1457 	return NULL;
1458 }
1459 
1460 void
1461 aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
1462 {
1463 	struct sk_buff_head queue;
1464 
1465 	__skb_queue_head_init(&queue);
1466 	aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
1467 	aoenet_xmit(&queue);
1468 }
1469 
1470 struct sk_buff *
1471 aoecmd_ata_id(struct aoedev *d)
1472 {
1473 	struct aoe_hdr *h;
1474 	struct aoe_atahdr *ah;
1475 	struct frame *f;
1476 	struct sk_buff *skb;
1477 	struct aoetgt *t;
1478 
1479 	f = newframe(d);
1480 	if (f == NULL)
1481 		return NULL;
1482 
1483 	t = *d->tgt;
1484 
1485 	/* initialize the headers & frame */
1486 	skb = f->skb;
1487 	h = (struct aoe_hdr *) skb_mac_header(skb);
1488 	ah = (struct aoe_atahdr *) (h+1);
1489 	skb_put(skb, sizeof *h + sizeof *ah);
1490 	memset(h, 0, skb->len);
1491 	f->tag = aoehdr_atainit(d, t, h);
1492 	fhash(f);
1493 	t->nout++;
1494 	f->waited = 0;
1495 	f->waited_total = 0;
1496 
1497 	/* set up ata header */
1498 	ah->scnt = 1;
1499 	ah->cmdstat = ATA_CMD_ID_ATA;
1500 	ah->lba3 = 0xa0;
1501 
1502 	skb->dev = t->ifp->nd;
1503 
1504 	d->rttavg = RTTAVG_INIT;
1505 	d->rttdev = RTTDEV_INIT;
1506 	d->timer.function = rexmit_timer;
1507 
1508 	skb = skb_clone(skb, GFP_ATOMIC);
1509 	if (skb) {
1510 		do_gettimeofday(&f->sent);
1511 		f->sent_jiffs = (u32) jiffies;
1512 	}
1513 
1514 	return skb;
1515 }
1516 
1517 static struct aoetgt **
1518 grow_targets(struct aoedev *d)
1519 {
1520 	ulong oldn, newn;
1521 	struct aoetgt **tt;
1522 
1523 	oldn = d->ntargets;
1524 	newn = oldn * 2;
1525 	tt = kcalloc(newn, sizeof(*d->targets), GFP_ATOMIC);
1526 	if (!tt)
1527 		return NULL;
1528 	memmove(tt, d->targets, sizeof(*d->targets) * oldn);
1529 	d->tgt = tt + (d->tgt - d->targets);
1530 	kfree(d->targets);
1531 	d->targets = tt;
1532 	d->ntargets = newn;
1533 
1534 	return &d->targets[oldn];
1535 }
1536 
1537 static struct aoetgt *
1538 addtgt(struct aoedev *d, char *addr, ulong nframes)
1539 {
1540 	struct aoetgt *t, **tt, **te;
1541 
1542 	tt = d->targets;
1543 	te = tt + d->ntargets;
1544 	for (; tt < te && *tt; tt++)
1545 		;
1546 
1547 	if (tt == te) {
1548 		tt = grow_targets(d);
1549 		if (!tt)
1550 			goto nomem;
1551 	}
1552 	t = kzalloc(sizeof(*t), GFP_ATOMIC);
1553 	if (!t)
1554 		goto nomem;
1555 	t->nframes = nframes;
1556 	t->d = d;
1557 	memcpy(t->addr, addr, sizeof t->addr);
1558 	t->ifp = t->ifs;
1559 	aoecmd_wreset(t);
1560 	t->maxout = t->nframes / 2;
1561 	INIT_LIST_HEAD(&t->ffree);
1562 	return *tt = t;
1563 
1564  nomem:
1565 	pr_info("aoe: cannot allocate memory to add target\n");
1566 	return NULL;
1567 }
1568 
1569 static void
1570 setdbcnt(struct aoedev *d)
1571 {
1572 	struct aoetgt **t, **e;
1573 	int bcnt = 0;
1574 
1575 	t = d->targets;
1576 	e = t + d->ntargets;
1577 	for (; t < e && *t; t++)
1578 		if (bcnt == 0 || bcnt > (*t)->minbcnt)
1579 			bcnt = (*t)->minbcnt;
1580 	if (bcnt != d->maxbcnt) {
1581 		d->maxbcnt = bcnt;
1582 		pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
1583 			d->aoemajor, d->aoeminor, bcnt);
1584 	}
1585 }
1586 
1587 static void
1588 setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
1589 {
1590 	struct aoedev *d;
1591 	struct aoeif *p, *e;
1592 	int minbcnt;
1593 
1594 	d = t->d;
1595 	minbcnt = bcnt;
1596 	p = t->ifs;
1597 	e = p + NAOEIFS;
1598 	for (; p < e; p++) {
1599 		if (p->nd == NULL)
1600 			break;		/* end of the valid interfaces */
1601 		if (p->nd == nd) {
1602 			p->bcnt = bcnt;	/* we're updating */
1603 			nd = NULL;
1604 		} else if (minbcnt > p->bcnt)
1605 			minbcnt = p->bcnt; /* find the min interface */
1606 	}
1607 	if (nd) {
1608 		if (p == e) {
1609 			pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
1610 			return;
1611 		}
1612 		dev_hold(nd);
1613 		p->nd = nd;
1614 		p->bcnt = bcnt;
1615 	}
1616 	t->minbcnt = minbcnt;
1617 	setdbcnt(d);
1618 }
1619 
1620 void
1621 aoecmd_cfg_rsp(struct sk_buff *skb)
1622 {
1623 	struct aoedev *d;
1624 	struct aoe_hdr *h;
1625 	struct aoe_cfghdr *ch;
1626 	struct aoetgt *t;
1627 	ulong flags, aoemajor;
1628 	struct sk_buff *sl;
1629 	struct sk_buff_head queue;
1630 	u16 n;
1631 
1632 	sl = NULL;
1633 	h = (struct aoe_hdr *) skb_mac_header(skb);
1634 	ch = (struct aoe_cfghdr *) (h+1);
1635 
1636 	/*
1637 	 * Enough people have their dip switches set backwards to
1638 	 * warrant a loud message for this special case.
1639 	 */
1640 	aoemajor = get_unaligned_be16(&h->major);
1641 	if (aoemajor == 0xfff) {
1642 		printk(KERN_ERR "aoe: Warning: shelf address is all ones.  "
1643 			"Check shelf dip switches.\n");
1644 		return;
1645 	}
1646 	if (aoemajor == 0xffff) {
1647 		pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
1648 			aoemajor, (int) h->minor);
1649 		return;
1650 	}
1651 	if (h->minor == 0xff) {
1652 		pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
1653 			aoemajor, (int) h->minor);
1654 		return;
1655 	}
1656 
1657 	n = be16_to_cpu(ch->bufcnt);
1658 	if (n > aoe_maxout)	/* keep it reasonable */
1659 		n = aoe_maxout;
1660 
1661 	d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
1662 	if (d == NULL) {
1663 		pr_info("aoe: device allocation failure\n");
1664 		return;
1665 	}
1666 
1667 	spin_lock_irqsave(&d->lock, flags);
1668 
1669 	t = gettgt(d, h->src);
1670 	if (t) {
1671 		t->nframes = n;
1672 		if (n < t->maxout)
1673 			aoecmd_wreset(t);
1674 	} else {
1675 		t = addtgt(d, h->src, n);
1676 		if (!t)
1677 			goto bail;
1678 	}
1679 	n = skb->dev->mtu;
1680 	n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
1681 	n /= 512;
1682 	if (n > ch->scnt)
1683 		n = ch->scnt;
1684 	n = n ? n * 512 : DEFAULTBCNT;
1685 	setifbcnt(t, skb->dev, n);
1686 
1687 	/* don't change users' perspective */
1688 	if (d->nopen == 0) {
1689 		d->fw_ver = be16_to_cpu(ch->fwver);
1690 		sl = aoecmd_ata_id(d);
1691 	}
1692 bail:
1693 	spin_unlock_irqrestore(&d->lock, flags);
1694 	aoedev_put(d);
1695 	if (sl) {
1696 		__skb_queue_head_init(&queue);
1697 		__skb_queue_tail(&queue, sl);
1698 		aoenet_xmit(&queue);
1699 	}
1700 }
1701 
1702 void
1703 aoecmd_wreset(struct aoetgt *t)
1704 {
1705 	t->maxout = 1;
1706 	t->ssthresh = t->nframes / 2;
1707 	t->next_cwnd = t->nframes;
1708 }
1709 
1710 void
1711 aoecmd_cleanslate(struct aoedev *d)
1712 {
1713 	struct aoetgt **t, **te;
1714 
1715 	d->rttavg = RTTAVG_INIT;
1716 	d->rttdev = RTTDEV_INIT;
1717 	d->maxbcnt = 0;
1718 
1719 	t = d->targets;
1720 	te = t + d->ntargets;
1721 	for (; t < te && *t; t++)
1722 		aoecmd_wreset(*t);
1723 }
1724 
1725 void
1726 aoe_failbuf(struct aoedev *d, struct buf *buf)
1727 {
1728 	if (buf == NULL)
1729 		return;
1730 	buf->resid = 0;
1731 	clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1732 	if (buf->nframesout == 0)
1733 		aoe_end_buf(d, buf);
1734 }
1735 
1736 void
1737 aoe_flush_iocq(void)
1738 {
1739 	int i;
1740 
1741 	for (i = 0; i < ncpus; i++) {
1742 		if (kts[i].active)
1743 			aoe_flush_iocq_by_index(i);
1744 	}
1745 }
1746 
1747 void
1748 aoe_flush_iocq_by_index(int id)
1749 {
1750 	struct frame *f;
1751 	struct aoedev *d;
1752 	LIST_HEAD(flist);
1753 	struct list_head *pos;
1754 	struct sk_buff *skb;
1755 	ulong flags;
1756 
1757 	spin_lock_irqsave(&iocq[id].lock, flags);
1758 	list_splice_init(&iocq[id].head, &flist);
1759 	spin_unlock_irqrestore(&iocq[id].lock, flags);
1760 	while (!list_empty(&flist)) {
1761 		pos = flist.next;
1762 		list_del(pos);
1763 		f = list_entry(pos, struct frame, head);
1764 		d = f->t->d;
1765 		skb = f->r_skb;
1766 		spin_lock_irqsave(&d->lock, flags);
1767 		if (f->buf) {
1768 			f->buf->nframesout--;
1769 			aoe_failbuf(d, f->buf);
1770 		}
1771 		aoe_freetframe(f);
1772 		spin_unlock_irqrestore(&d->lock, flags);
1773 		dev_kfree_skb(skb);
1774 		aoedev_put(d);
1775 	}
1776 }
1777 
1778 int __init
1779 aoecmd_init(void)
1780 {
1781 	void *p;
1782 	int i;
1783 	int ret;
1784 
1785 	/* get_zeroed_page returns page with ref count 1 */
1786 	p = (void *) get_zeroed_page(GFP_KERNEL | __GFP_REPEAT);
1787 	if (!p)
1788 		return -ENOMEM;
1789 	empty_page = virt_to_page(p);
1790 
1791 	ncpus = num_online_cpus();
1792 
1793 	iocq = kcalloc(ncpus, sizeof(struct iocq_ktio), GFP_KERNEL);
1794 	if (!iocq)
1795 		return -ENOMEM;
1796 
1797 	kts = kcalloc(ncpus, sizeof(struct ktstate), GFP_KERNEL);
1798 	if (!kts) {
1799 		ret = -ENOMEM;
1800 		goto kts_fail;
1801 	}
1802 
1803 	ktiowq = kcalloc(ncpus, sizeof(wait_queue_head_t), GFP_KERNEL);
1804 	if (!ktiowq) {
1805 		ret = -ENOMEM;
1806 		goto ktiowq_fail;
1807 	}
1808 
1809 	mutex_init(&ktio_spawn_lock);
1810 
1811 	for (i = 0; i < ncpus; i++) {
1812 		INIT_LIST_HEAD(&iocq[i].head);
1813 		spin_lock_init(&iocq[i].lock);
1814 		init_waitqueue_head(&ktiowq[i]);
1815 		snprintf(kts[i].name, sizeof(kts[i].name), "aoe_ktio%d", i);
1816 		kts[i].fn = ktio;
1817 		kts[i].waitq = &ktiowq[i];
1818 		kts[i].lock = &iocq[i].lock;
1819 		kts[i].id = i;
1820 		kts[i].active = 0;
1821 	}
1822 	kts[0].active = 1;
1823 	if (aoe_ktstart(&kts[0])) {
1824 		ret = -ENOMEM;
1825 		goto ktstart_fail;
1826 	}
1827 	return 0;
1828 
1829 ktstart_fail:
1830 	kfree(ktiowq);
1831 ktiowq_fail:
1832 	kfree(kts);
1833 kts_fail:
1834 	kfree(iocq);
1835 
1836 	return ret;
1837 }
1838 
1839 void
1840 aoecmd_exit(void)
1841 {
1842 	int i;
1843 
1844 	for (i = 0; i < ncpus; i++)
1845 		if (kts[i].active)
1846 			aoe_ktstop(&kts[i]);
1847 
1848 	aoe_flush_iocq();
1849 
1850 	/* Free up the iocq and thread speicific configuration
1851 	* allocated during startup.
1852 	*/
1853 	kfree(iocq);
1854 	kfree(kts);
1855 	kfree(ktiowq);
1856 
1857 	free_page((unsigned long) page_address(empty_page));
1858 	empty_page = NULL;
1859 }
1860