x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / block / aoe / aoecmd.c
blob8fb295350efbbda8ecc23fc49707bd2152747c90
1 /* Copyright (c) 2013 Coraid, Inc. See COPYING for GPL terms. */
2 /*
3 * aoecmd.c
4 * Filesystem request handling methods
5 */
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"
22 #define MAXIOC (8192) /* default meant to avoid most soft lockups */
24 static void ktcomplete(struct frame *, struct sk_buff *);
25 static int count_targets(struct aoedev *d, int *untainted);
27 static struct buf *nextbuf(struct aoedev *);
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.");
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.");
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.
45 static int ncpus;
47 /* mutex lock used for synchronization while thread spawning */
48 static DEFINE_MUTEX(ktio_spawn_lock);
50 static wait_queue_head_t *ktiowq;
51 static struct ktstate *kts;
53 /* io completion queue */
54 struct iocq_ktio {
55 struct list_head head;
56 spinlock_t lock;
58 static struct iocq_ktio *iocq;
60 static struct page *empty_page;
62 static struct sk_buff *
63 new_skb(ulong len)
65 struct sk_buff *skb;
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);
75 return skb;
78 static struct frame *
79 getframe_deferred(struct aoedev *d, u32 tag)
81 struct list_head *head, *pos, *nx;
82 struct frame *f;
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;
92 return NULL;
95 static struct frame *
96 getframe(struct aoedev *d, u32 tag)
98 struct frame *f;
99 struct list_head *head, *pos, *nx;
100 u32 n;
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;
111 return NULL;
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."
119 static int
120 newtag(struct aoedev *d)
122 register ulong n;
124 n = jiffies & 0xffff;
125 return n |= (++d->lasttag & 0x7fff) << 16;
128 static u32
129 aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
131 u32 host_tag = newtag(d);
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);
142 return host_tag;
145 static inline void
146 put_lba(struct aoe_atahdr *ah, sector_t lba)
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;
156 static struct aoeif *
157 ifrotate(struct aoetgt *t)
159 struct aoeif *ifp;
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;
170 static void
171 skb_pool_put(struct aoedev *d, struct sk_buff *skb)
173 __skb_queue_tail(&d->skbpool, skb);
176 static struct sk_buff *
177 skb_pool_get(struct aoedev *d)
179 struct sk_buff *skb = skb_peek(&d->skbpool);
181 if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
182 __skb_unlink(skb, &d->skbpool);
183 return skb;
185 if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
186 (skb = new_skb(ETH_ZLEN)))
187 return skb;
189 return NULL;
192 void
193 aoe_freetframe(struct frame *f)
195 struct aoetgt *t;
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);
206 static struct frame *
207 newtframe(struct aoedev *d, struct aoetgt *t)
209 struct frame *f;
210 struct sk_buff *skb;
211 struct list_head *pos;
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);
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;
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;
244 skb->truesize -= skb->data_len;
245 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
246 skb_trim(skb, 0);
247 return f;
250 static struct frame *
251 newframe(struct aoedev *d)
253 struct frame *f;
254 struct aoetgt *t, **tt;
255 int totout = 0;
256 int use_tainted;
257 int has_untainted;
259 if (!d->targets || !d->targets[0]) {
260 printk(KERN_ERR "aoe: NULL TARGETS!\n");
261 return NULL;
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;
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;
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;
290 if (totout == 0) {
291 d->kicked++;
292 d->flags |= DEVFL_KICKME;
294 return NULL;
297 static void
298 skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt)
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;
315 static void
316 fhash(struct frame *f)
318 struct aoedev *d = f->t->d;
319 u32 n;
321 n = f->tag % NFACTIVE;
322 list_add_tail(&f->head, &d->factive[n]);
325 static void
326 ata_rw_frameinit(struct frame *f)
328 struct aoetgt *t;
329 struct aoe_hdr *h;
330 struct aoe_atahdr *ah;
331 struct sk_buff *skb;
332 char writebit, extbit;
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);
340 writebit = 0x10;
341 extbit = 0x4;
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;
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 */
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;
374 ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
375 skb->dev = t->ifp->nd;
378 static int
379 aoecmd_ata_rw(struct aoedev *d)
381 struct frame *f;
382 struct buf *buf;
383 struct sk_buff *skb;
384 struct sk_buff_head queue;
385 ulong bcnt, fbcnt;
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;
407 fbcnt -= buf->bv_resid;
408 buf->resid -= buf->bv_resid;
409 if (buf->resid == 0) {
410 d->ip.buf = NULL;
411 break;
413 buf->bv++;
414 buf->bv_resid = buf->bv->bv_len;
415 WARN_ON(buf->bv_resid == 0);
416 } while (fbcnt);
418 /* initialize the headers & frame */
419 f->buf = buf;
420 f->bcnt = bcnt;
421 ata_rw_frameinit(f);
423 /* mark all tracking fields and load out */
424 buf->nframesout += 1;
425 buf->sector += bcnt >> 9;
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);
435 return 1;
438 /* some callers cannot sleep, and they can call this function,
439 * transmitting the packets later, when interrupts are on
441 static void
442 aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
444 struct aoe_hdr *h;
445 struct aoe_cfghdr *ch;
446 struct sk_buff *skb;
447 struct net_device *ifp;
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;
455 skb = new_skb(sizeof *h + sizeof *ch);
456 if (skb == NULL) {
457 printk(KERN_INFO "aoe: skb alloc failure\n");
458 goto cont;
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);
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;
474 cont:
475 dev_put(ifp);
477 rcu_read_unlock();
480 static void
481 resend(struct aoedev *d, struct frame *f)
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;
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;
499 h = (struct aoe_hdr *) skb_mac_header(skb);
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);
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);
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);
527 static int
528 tsince_hr(struct frame *f)
530 struct timeval now;
531 int n;
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;
537 if (n < 0)
538 n = -n;
540 /* For relatively long periods, use jiffies to avoid
541 * discrepancies caused by updates to the system time.
543 * On system with HZ of 1000, 32-bits is over 49 days
544 * worth of jiffies, or over 71 minutes worth of usecs.
546 * Jiffies overflow is handled by subtraction of unsigned ints:
547 * (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe
548 * $3 = 4
549 * (gdb)
551 if (n > USEC_PER_SEC / 4) {
552 n = ((u32) jiffies) - f->sent_jiffs;
553 n *= USEC_PER_SEC / HZ;
556 return n;
559 static int
560 tsince(u32 tag)
562 int n;
564 n = jiffies & 0xffff;
565 n -= tag & 0xffff;
566 if (n < 0)
567 n += 1<<16;
568 return jiffies_to_usecs(n + 1);
571 static struct aoeif *
572 getif(struct aoetgt *t, struct net_device *nd)
574 struct aoeif *p, *e;
576 p = t->ifs;
577 e = p + NAOEIFS;
578 for (; p < e; p++)
579 if (p->nd == nd)
580 return p;
581 return NULL;
584 static void
585 ejectif(struct aoetgt *t, struct aoeif *ifp)
587 struct aoeif *e;
588 struct net_device *nd;
589 ulong n;
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);
599 static struct frame *
600 reassign_frame(struct frame *f)
602 struct frame *nf;
603 struct sk_buff *skb;
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;
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;
626 return nf;
629 static void
630 probe(struct aoetgt *t)
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;
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;
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);
661 skb->len += f->bcnt;
662 skb->data_len = f->bcnt;
663 skb->truesize += f->bcnt;
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);
675 static long
676 rto(struct aoedev *d)
678 long t;
680 t = 2 * d->rttavg >> RTTSCALE;
681 t += 8 * d->rttdev >> RTTDSCALE;
682 if (t == 0)
683 t = 1;
685 return t;
688 static void
689 rexmit_deferred(struct aoedev *d)
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;
698 count_targets(d, &untainted);
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++;
713 list_replace(&f->head, &nf->head);
714 pos = &nf->head;
715 aoe_freetframe(f);
716 f = nf;
717 t = f->t;
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;
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;
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);
747 /* An aoetgt accumulates demerits quickly, and successful
748 * probing redeems the aoetgt slowly.
750 static void
751 scorn(struct aoetgt *t)
753 int n;
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;
763 static int
764 count_targets(struct aoedev *d, int *untainted)
766 int i, good;
768 for (i = good = 0; i < d->ntargets && d->targets[i]; ++i)
769 if (d->targets[i]->taint == 0)
770 good++;
772 if (untainted)
773 *untainted = good;
774 return i;
777 static void
778 rexmit_timer(ulong vp)
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;
792 d = (struct aoedev *) vp;
794 spin_lock_irqsave(&d->lock, flags);
796 /* timeout based on observed timings and variations */
797 timeout = rto(d);
799 utgts = count_targets(d, NULL);
801 if (d->flags & DEVFL_TKILL) {
802 spin_unlock_irqrestore(&d->lock, flags);
803 return;
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);
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.
832 list_splice(&flist, &d->factive[0]);
833 aoedev_downdev(d);
834 goto out;
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 */
844 if (t->maxout != 1) {
845 t->ssthresh = t->maxout / 2;
846 t->maxout = 1;
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;
859 list_move_tail(pos, &d->rexmitq);
860 t->nout--;
862 rexmit_deferred(d);
864 out:
865 if ((d->flags & DEVFL_KICKME) && d->blkq) {
866 d->flags &= ~DEVFL_KICKME;
867 d->blkq->request_fn(d->blkq);
870 d->timer.expires = jiffies + TIMERTICK;
871 add_timer(&d->timer);
873 spin_unlock_irqrestore(&d->lock, flags);
876 static unsigned long
877 rqbiocnt(struct request *r)
879 struct bio *bio;
880 unsigned long n = 0;
882 __rq_for_each_bio(bio, r)
883 n++;
884 return n;
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.
891 * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
892 * discussion.
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.
897 static void
898 bio_pageinc(struct bio *bio)
900 struct bio_vec *bv;
901 struct page *page;
902 int i;
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.
908 page = compound_head(bv->bv_page);
909 atomic_inc(&page->_count);
913 static void
914 bio_pagedec(struct bio *bio)
916 struct bio_vec *bv;
917 struct page *page;
918 int i;
920 bio_for_each_segment(bv, bio, i) {
921 page = compound_head(bv->bv_page);
922 atomic_dec(&page->_count);
926 static void
927 bufinit(struct buf *buf, struct request *rq, struct bio *bio)
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);
940 static struct buf *
941 nextbuf(struct aoedev *d)
943 struct request *rq;
944 struct request_queue *q;
945 struct buf *buf;
946 struct bio *bio;
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);
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;
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;
977 /* enters with d->lock held */
978 void
979 aoecmd_work(struct aoedev *d)
981 rexmit_deferred(d);
982 while (aoecmd_ata_rw(d))
986 /* this function performs work that has been deferred until sleeping is OK
988 void
989 aoecmd_sleepwork(struct work_struct *work)
991 struct aoedev *d = container_of(work, struct aoedev, work);
992 struct block_device *bd;
993 u64 ssize;
995 if (d->flags & DEVFL_GDALLOC)
996 aoeblk_gdalloc(d);
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);
1007 spin_lock_irq(&d->lock);
1008 d->flags |= DEVFL_UP;
1009 d->flags &= ~DEVFL_NEWSIZE;
1010 spin_unlock_irq(&d->lock);
1014 static void
1015 ata_ident_fixstring(u16 *id, int ns)
1017 u16 s;
1019 while (ns-- > 0) {
1020 s = *id;
1021 *id++ = s >> 8 | s << 8;
1025 static void
1026 ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
1028 u64 ssize;
1029 u16 n;
1031 /* word 83: command set supported */
1032 n = get_unaligned_le16(&id[83 << 1]);
1034 /* word 86: command set/feature enabled */
1035 n |= get_unaligned_le16(&id[86 << 1]);
1037 if (n & (1<<10)) { /* bit 10: LBA 48 */
1038 d->flags |= DEVFL_EXT;
1040 /* word 100: number lba48 sectors */
1041 ssize = get_unaligned_le64(&id[100 << 1]);
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;
1051 /* number lba28 sectors */
1052 ssize = get_unaligned_le32(&id[60 << 1]);
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]);
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));
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);
1083 static void
1084 calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt)
1086 register long n;
1088 n = rtt;
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;
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;
1108 static struct aoetgt *
1109 gettgt(struct aoedev *d, char *addr)
1111 struct aoetgt **t, **e;
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;
1121 static void
1122 bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
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;
1142 void
1143 aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
1145 struct bio *bio;
1146 int bok;
1147 struct request_queue *q;
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));
1157 /* cf. http://lkml.org/lkml/2006/10/31/28 */
1158 if (!fastfail)
1159 __blk_run_queue(q);
1162 static void
1163 aoe_end_buf(struct aoedev *d, struct buf *buf)
1165 struct request *rq;
1166 unsigned long n;
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);
1179 static void
1180 ktiocomplete(struct frame *f)
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;
1192 if (f == NULL)
1193 return;
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;
1204 hout = (struct aoe_hdr *) skb_mac_header(f->skb);
1205 ahout = (struct aoe_atahdr *) (hout+1);
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;
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;
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;
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);
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++;
1273 aoe_freetframe(f);
1275 if (buf && --buf->nframesout == 0 && buf->resid == 0)
1276 aoe_end_buf(d, buf);
1278 spin_unlock_irq(&d->lock);
1279 aoedev_put(d);
1280 dev_kfree_skb(skb);
1283 /* Enters with iocq.lock held.
1284 * Returns true iff responses needing processing remain.
1286 static int
1287 ktio(int id)
1289 struct frame *f;
1290 struct list_head *pos;
1291 int i;
1292 int actual_id;
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);
1305 /* Figure out if extra threads are required. */
1306 actual_id = f->t->d->aoeminor % ncpus;
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);
1316 spin_lock_irq(&iocq[id].lock);
1320 static int
1321 kthread(void *vp)
1323 struct ktstate *k;
1324 DECLARE_WAITQUEUE(wait, current);
1325 int more;
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);
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;
1349 void
1350 aoe_ktstop(struct ktstate *k)
1352 kthread_stop(k->task);
1353 wait_for_completion(&k->rendez);
1357 aoe_ktstart(struct ktstate *k)
1359 struct task_struct *task;
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;
1371 /* pass it off to kthreads for processing */
1372 static void
1373 ktcomplete(struct frame *f, struct sk_buff *skb)
1375 int id;
1376 ulong flags;
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.
1387 id = 0;
1388 spin_lock_irqsave(&iocq[id].lock, flags);
1390 list_add_tail(&f->head, &iocq[id].head);
1391 spin_unlock_irqrestore(&iocq[id].lock, flags);
1392 wake_up(&ktiowq[id]);
1395 struct sk_buff *
1396 aoecmd_ata_rsp(struct sk_buff *skb)
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;
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;
1417 spin_lock_irqsave(&d->lock, flags);
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;
1447 aoecmd_work(d);
1449 spin_unlock_irqrestore(&d->lock, flags);
1451 ktcomplete(f, skb);
1454 * Note here that we do not perform an aoedev_put, as we are
1455 * leaving this reference for the ktio to release.
1457 return NULL;
1460 void
1461 aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
1463 struct sk_buff_head queue;
1465 __skb_queue_head_init(&queue);
1466 aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
1467 aoenet_xmit(&queue);
1470 struct sk_buff *
1471 aoecmd_ata_id(struct aoedev *d)
1473 struct aoe_hdr *h;
1474 struct aoe_atahdr *ah;
1475 struct frame *f;
1476 struct sk_buff *skb;
1477 struct aoetgt *t;
1479 f = newframe(d);
1480 if (f == NULL)
1481 return NULL;
1483 t = *d->tgt;
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;
1497 /* set up ata header */
1498 ah->scnt = 1;
1499 ah->cmdstat = ATA_CMD_ID_ATA;
1500 ah->lba3 = 0xa0;
1502 skb->dev = t->ifp->nd;
1504 d->rttavg = RTTAVG_INIT;
1505 d->rttdev = RTTDEV_INIT;
1506 d->timer.function = rexmit_timer;
1508 skb = skb_clone(skb, GFP_ATOMIC);
1509 if (skb) {
1510 do_gettimeofday(&f->sent);
1511 f->sent_jiffs = (u32) jiffies;
1514 return skb;
1517 static struct aoetgt **
1518 grow_targets(struct aoedev *d)
1520 ulong oldn, newn;
1521 struct aoetgt **tt;
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;
1534 return &d->targets[oldn];
1537 static struct aoetgt *
1538 addtgt(struct aoedev *d, char *addr, ulong nframes)
1540 struct aoetgt *t, **tt, **te;
1542 tt = d->targets;
1543 te = tt + d->ntargets;
1544 for (; tt < te && *tt; tt++)
1547 if (tt == te) {
1548 tt = grow_targets(d);
1549 if (!tt)
1550 goto nomem;
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;
1564 nomem:
1565 pr_info("aoe: cannot allocate memory to add target\n");
1566 return NULL;
1569 static void
1570 setdbcnt(struct aoedev *d)
1572 struct aoetgt **t, **e;
1573 int bcnt = 0;
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);
1587 static void
1588 setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
1590 struct aoedev *d;
1591 struct aoeif *p, *e;
1592 int minbcnt;
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 */
1607 if (nd) {
1608 if (p == e) {
1609 pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
1610 return;
1612 dev_hold(nd);
1613 p->nd = nd;
1614 p->bcnt = bcnt;
1616 t->minbcnt = minbcnt;
1617 setdbcnt(d);
1620 void
1621 aoecmd_cfg_rsp(struct sk_buff *skb)
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;
1632 sl = NULL;
1633 h = (struct aoe_hdr *) skb_mac_header(skb);
1634 ch = (struct aoe_cfghdr *) (h+1);
1637 * Enough people have their dip switches set backwards to
1638 * warrant a loud message for this special case.
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;
1646 if (aoemajor == 0xffff) {
1647 pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
1648 aoemajor, (int) h->minor);
1649 return;
1651 if (h->minor == 0xff) {
1652 pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
1653 aoemajor, (int) h->minor);
1654 return;
1657 n = be16_to_cpu(ch->bufcnt);
1658 if (n > aoe_maxout) /* keep it reasonable */
1659 n = aoe_maxout;
1661 d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
1662 if (d == NULL) {
1663 pr_info("aoe: device allocation failure\n");
1664 return;
1667 spin_lock_irqsave(&d->lock, flags);
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;
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);
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);
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);
1702 void
1703 aoecmd_wreset(struct aoetgt *t)
1705 t->maxout = 1;
1706 t->ssthresh = t->nframes / 2;
1707 t->next_cwnd = t->nframes;
1710 void
1711 aoecmd_cleanslate(struct aoedev *d)
1713 struct aoetgt **t, **te;
1715 d->rttavg = RTTAVG_INIT;
1716 d->rttdev = RTTDEV_INIT;
1717 d->maxbcnt = 0;
1719 t = d->targets;
1720 te = t + d->ntargets;
1721 for (; t < te && *t; t++)
1722 aoecmd_wreset(*t);
1725 void
1726 aoe_failbuf(struct aoedev *d, struct buf *buf)
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);
1736 void
1737 aoe_flush_iocq(void)
1739 int i;
1741 for (i = 0; i < ncpus; i++) {
1742 if (kts[i].active)
1743 aoe_flush_iocq_by_index(i);
1747 void
1748 aoe_flush_iocq_by_index(int id)
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;
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);
1771 aoe_freetframe(f);
1772 spin_unlock_irqrestore(&d->lock, flags);
1773 dev_kfree_skb(skb);
1774 aoedev_put(d);
1778 int __init
1779 aoecmd_init(void)
1781 void *p;
1782 int i;
1783 int ret;
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);
1791 ncpus = num_online_cpus();
1793 iocq = kcalloc(ncpus, sizeof(struct iocq_ktio), GFP_KERNEL);
1794 if (!iocq)
1795 return -ENOMEM;
1797 kts = kcalloc(ncpus, sizeof(struct ktstate), GFP_KERNEL);
1798 if (!kts) {
1799 ret = -ENOMEM;
1800 goto kts_fail;
1803 ktiowq = kcalloc(ncpus, sizeof(wait_queue_head_t), GFP_KERNEL);
1804 if (!ktiowq) {
1805 ret = -ENOMEM;
1806 goto ktiowq_fail;
1809 mutex_init(&ktio_spawn_lock);
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;
1822 kts[0].active = 1;
1823 if (aoe_ktstart(&kts[0])) {
1824 ret = -ENOMEM;
1825 goto ktstart_fail;
1827 return 0;
1829 ktstart_fail:
1830 kfree(ktiowq);
1831 ktiowq_fail:
1832 kfree(kts);
1833 kts_fail:
1834 kfree(iocq);
1836 return ret;
1839 void
1840 aoecmd_exit(void)
1842 int i;
1844 for (i = 0; i < ncpus; i++)
1845 if (kts[i].active)
1846 aoe_ktstop(&kts[i]);
1848 aoe_flush_iocq();
1850 /* Free up the iocq and thread speicific configuration
1851 * allocated during startup.
1853 kfree(iocq);
1854 kfree(kts);
1855 kfree(ktiowq);
1857 free_page((unsigned long) page_address(empty_page));
1858 empty_page = NULL;