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