1 /* Copyright (c) 2013 Coraid, Inc. See COPYING for GPL terms. */
4 * Filesystem request handling methods
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>
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.
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 */
55 struct list_head head;
58 static struct iocq_ktio *iocq;
60 static struct page *empty_page;
62 static struct sk_buff *
67 skb = alloc_skb(len + MAX_HEADER, GFP_ATOMIC);
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);
79 getframe_deferred(struct aoedev *d, u32 tag)
81 struct list_head *head, *pos, *nx;
85 list_for_each_safe(pos, nx, head) {
86 f = list_entry(pos, struct frame, head);
96 getframe(struct aoedev *d, u32 tag)
99 struct list_head *head, *pos, *nx;
103 head = &d->factive[n];
104 list_for_each_safe(pos, nx, head) {
105 f = list_entry(pos, struct frame, head);
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."
120 newtag(struct aoedev *d)
124 n = jiffies & 0xffff;
125 return n |= (++d->lasttag & 0x7fff) << 16;
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);
137 h->major = cpu_to_be16(d->aoemajor);
138 h->minor = d->aoeminor;
140 h->tag = cpu_to_be32(host_tag);
146 put_lba(struct aoe_atahdr *ah, sector_t 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)
163 if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL)
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);
185 if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
186 (skb = new_skb(ETH_ZLEN)))
193 aoe_freetframe(struct frame *f)
203 list_add(&f->head, &t->ffree);
206 static struct frame *
207 newtframe(struct aoedev *d, struct aoetgt *t)
211 struct list_head *pos;
213 if (list_empty(&t->ffree)) {
214 if (t->falloc >= NSKBPOOLMAX*2)
216 f = kcalloc(1, sizeof(*f), GFP_ATOMIC);
224 f = list_entry(pos, struct frame, head);
229 f->skb = skb = new_skb(ETH_ZLEN);
231 bail: aoe_freetframe(f);
236 if (atomic_read(&skb_shinfo(skb)->dataref) != 1) {
237 skb = skb_pool_get(d);
240 skb_pool_put(d, f->skb);
244 skb->truesize -= skb->data_len;
245 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
250 static struct frame *
251 newframe(struct aoedev *d)
254 struct aoetgt *t, **tt;
259 if (!d->targets || !d->targets[0]) {
260 printk(KERN_ERR "aoe: NULL TARGETS!\n");
263 tt = d->tgt; /* last used target */
264 for (use_tainted = 0, has_untainted = 0;;) {
266 if (tt >= &d->targets[d->ntargets] || !*tt)
273 if (t->nout < t->maxout
274 && (use_tainted || !t->taint)
283 if (tt == d->tgt) { /* we've looped and found nada */
284 if (!use_tainted && !has_untainted)
292 d->flags |= DEVFL_KICKME;
298 skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt)
303 fcnt = bv->bv_len - (off - bv->bv_offset);
306 skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt);
316 fhash(struct frame *f)
318 struct aoedev *d = f->t->d;
321 n = f->tag % NFACTIVE;
322 list_add_tail(&f->head, &d->factive[n]);
326 ata_rw_frameinit(struct frame *f)
330 struct aoe_atahdr *ah;
332 char writebit, extbit;
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);
344 f->tag = aoehdr_atainit(t->d, t, h);
350 f->lba = f->buf->sector;
352 /* set up ata header */
353 ah->scnt = f->bcnt >> 9;
355 if (t->d->flags & DEVFL_EXT) {
356 ah->aflags |= AOEAFL_EXT;
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;
366 skb->data_len = f->bcnt;
367 skb->truesize += f->bcnt;
374 ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
375 skb->dev = t->ifp->nd;
379 aoecmd_ata_rw(struct aoedev *d)
385 struct sk_buff_head queue;
398 if (bcnt > buf->resid)
402 f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
404 if (fbcnt < buf->bv_resid) {
405 buf->bv_resid -= fbcnt;
409 fbcnt -= buf->bv_resid;
410 buf->resid -= buf->bv_resid;
411 if (buf->resid == 0) {
416 buf->bv_resid = buf->bv->bv_len;
417 WARN_ON(buf->bv_resid == 0);
420 /* initialize the headers & frame */
425 /* mark all tracking fields and load out */
426 buf->nframesout += 1;
427 buf->sector += bcnt >> 9;
429 skb = skb_clone(f->skb, GFP_ATOMIC);
431 do_gettimeofday(&f->sent);
432 f->sent_jiffs = (u32) jiffies;
433 __skb_queue_head_init(&queue);
434 __skb_queue_tail(&queue, skb);
440 /* some callers cannot sleep, and they can call this function,
441 * transmitting the packets later, when interrupts are on
444 aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
447 struct aoe_cfghdr *ch;
449 struct net_device *ifp;
452 for_each_netdev_rcu(&init_net, ifp) {
454 if (!is_aoe_netif(ifp))
457 skb = new_skb(sizeof *h + sizeof *ch);
459 printk(KERN_INFO "aoe: skb alloc failure\n");
462 skb_put(skb, sizeof *h + sizeof *ch);
464 __skb_queue_tail(queue, skb);
465 h = (struct aoe_hdr *) skb_mac_header(skb);
466 memset(h, 0, sizeof *h + sizeof *ch);
468 memset(h->dst, 0xff, sizeof h->dst);
469 memcpy(h->src, ifp->dev_addr, sizeof h->src);
470 h->type = __constant_cpu_to_be16(ETH_P_AOE);
472 h->major = cpu_to_be16(aoemajor);
483 resend(struct aoedev *d, struct frame *f)
486 struct sk_buff_head queue;
488 struct aoe_atahdr *ah;
496 if (ifrotate(t) == NULL) {
497 /* probably can't happen, but set it up to fail anyway */
498 pr_info("aoe: resend: no interfaces to rotate to.\n");
502 h = (struct aoe_hdr *) skb_mac_header(skb);
503 ah = (struct aoe_atahdr *) (h+1);
505 if (!(f->flags & FFL_PROBE)) {
506 snprintf(buf, sizeof(buf),
507 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
508 "retransmit", d->aoemajor, d->aoeminor,
510 h->src, h->dst, t->nout);
516 h->tag = cpu_to_be32(n);
517 memcpy(h->dst, t->addr, sizeof h->dst);
518 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
520 skb->dev = t->ifp->nd;
521 skb = skb_clone(skb, GFP_ATOMIC);
524 do_gettimeofday(&f->sent);
525 f->sent_jiffs = (u32) jiffies;
526 __skb_queue_head_init(&queue);
527 __skb_queue_tail(&queue, skb);
532 tsince_hr(struct frame *f)
537 do_gettimeofday(&now);
538 n = now.tv_usec - f->sent.tv_usec;
539 n += (now.tv_sec - f->sent.tv_sec) * USEC_PER_SEC;
544 /* For relatively long periods, use jiffies to avoid
545 * discrepancies caused by updates to the system time.
547 * On system with HZ of 1000, 32-bits is over 49 days
548 * worth of jiffies, or over 71 minutes worth of usecs.
550 * Jiffies overflow is handled by subtraction of unsigned ints:
551 * (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe
555 if (n > USEC_PER_SEC / 4) {
556 n = ((u32) jiffies) - f->sent_jiffs;
557 n *= USEC_PER_SEC / HZ;
568 n = jiffies & 0xffff;
572 return jiffies_to_usecs(n + 1);
575 static struct aoeif *
576 getif(struct aoetgt *t, struct net_device *nd)
589 ejectif(struct aoetgt *t, struct aoeif *ifp)
592 struct net_device *nd;
596 e = t->ifs + NAOEIFS - 1;
597 n = (e - ifp) * sizeof *ifp;
598 memmove(ifp, ifp+1, n);
603 static struct frame *
604 reassign_frame(struct frame *f)
609 nf = newframe(f->t->d);
623 nf->bv_off = f->bv_off;
625 nf->waited_total = f->waited_total;
627 nf->sent_jiffs = f->sent_jiffs;
634 probe(struct aoetgt *t)
639 struct sk_buff_head queue;
646 pr_err("%s %pm for e%ld.%d: %s\n",
647 "aoe: cannot probe remote address",
649 (long) d->aoemajor, d->aoeminor,
650 "no frame available");
653 f->flags |= FFL_PROBE;
655 f->bcnt = t->d->maxbcnt ? t->d->maxbcnt : DEFAULTBCNT;
658 for (frag = 0, n = f->bcnt; n > 0; ++frag, n -= m) {
663 skb_fill_page_desc(skb, frag, empty_page, 0, m);
666 skb->data_len = f->bcnt;
667 skb->truesize += f->bcnt;
669 skb = skb_clone(f->skb, GFP_ATOMIC);
671 do_gettimeofday(&f->sent);
672 f->sent_jiffs = (u32) jiffies;
673 __skb_queue_head_init(&queue);
674 __skb_queue_tail(&queue, skb);
680 rto(struct aoedev *d)
684 t = 2 * d->rttavg >> RTTSCALE;
685 t += 8 * d->rttdev >> RTTDSCALE;
693 rexmit_deferred(struct aoedev *d)
698 struct list_head *pos, *nx, *head;
702 count_targets(d, &untainted);
705 list_for_each_safe(pos, nx, head) {
706 f = list_entry(pos, struct frame, head);
709 if (!(f->flags & FFL_PROBE)) {
710 nf = reassign_frame(f);
712 if (t->nout_probes == 0
717 list_replace(&f->head, &nf->head);
723 } else if (untainted < 1) {
724 /* don't probe w/o other untainted aoetgts */
726 } else if (tsince_hr(f) < t->taint * rto(d)) {
727 /* reprobe slowly when taint is high */
730 } else if (f->flags & FFL_PROBE) {
731 stop_probe: /* don't probe untainted aoetgts */
734 /* leaving d->kicked, because this is routine */
735 f->t->d->flags |= DEVFL_KICKME;
738 if (t->nout >= t->maxout)
742 if (f->flags & FFL_PROBE)
744 since = tsince_hr(f);
746 f->waited_total += since;
751 /* An aoetgt accumulates demerits quickly, and successful
752 * probing redeems the aoetgt slowly.
755 scorn(struct aoetgt *t)
760 t->taint += t->taint * 2;
763 if (t->taint > MAX_TAINT)
764 t->taint = MAX_TAINT;
768 count_targets(struct aoedev *d, int *untainted)
772 for (i = good = 0; i < d->ntargets && d->targets[i]; ++i)
773 if (d->targets[i]->taint == 0)
782 rexmit_timer(ulong vp)
788 struct list_head *head, *pos, *nx;
790 register long timeout;
793 int utgts; /* number of aoetgt descriptors (not slots) */
796 d = (struct aoedev *) vp;
798 spin_lock_irqsave(&d->lock, flags);
800 /* timeout based on observed timings and variations */
803 utgts = count_targets(d, NULL);
805 if (d->flags & DEVFL_TKILL) {
806 spin_unlock_irqrestore(&d->lock, flags);
810 /* collect all frames to rexmit into flist */
811 for (i = 0; i < NFACTIVE; i++) {
812 head = &d->factive[i];
813 list_for_each_safe(pos, nx, head) {
814 f = list_entry(pos, struct frame, head);
815 if (tsince_hr(f) < timeout)
816 break; /* end of expired frames */
817 /* move to flist for later processing */
818 list_move_tail(pos, &flist);
822 /* process expired frames */
823 while (!list_empty(&flist)) {
825 f = list_entry(pos, struct frame, head);
826 since = tsince_hr(f);
827 n = f->waited_total + since;
831 && !(f->flags & FFL_PROBE)) {
832 /* Waited too long. Device failure.
833 * Hang all frames on first hash bucket for downdev
836 list_splice(&flist, &d->factive[0]);
842 n = f->waited + since;
844 if (aoe_deadsecs && utgts > 0
845 && (n > aoe_deadsecs / utgts || n > HARD_SCORN_SECS))
846 scorn(t); /* avoid this target */
848 if (t->maxout != 1) {
849 t->ssthresh = t->maxout / 2;
853 if (f->flags & FFL_PROBE) {
856 ifp = getif(t, f->skb->dev);
857 if (ifp && ++ifp->lost > (t->nframes << 1)
858 && (ifp != t->ifs || t->ifs[1].nd)) {
863 list_move_tail(pos, &d->rexmitq);
869 if ((d->flags & DEVFL_KICKME) && d->blkq) {
870 d->flags &= ~DEVFL_KICKME;
871 d->blkq->request_fn(d->blkq);
874 d->timer.expires = jiffies + TIMERTICK;
875 add_timer(&d->timer);
877 spin_unlock_irqrestore(&d->lock, flags);
881 rqbiocnt(struct request *r)
886 __rq_for_each_bio(bio, r)
891 /* This can be removed if we are certain that no users of the block
892 * layer will ever use zero-count pages in bios. Otherwise we have to
893 * protect against the put_page sometimes done by the network layer.
895 * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
898 * We cannot use get_page in the workaround, because it insists on a
899 * positive page count as a precondition. So we use _count directly.
902 bio_pageinc(struct bio *bio)
908 bio_for_each_segment(bv, bio, i) {
909 /* Non-zero page count for non-head members of
910 * compound pages is no longer allowed by the kernel.
912 page = compound_trans_head(bv->bv_page);
913 atomic_inc(&page->_count);
918 bio_pagedec(struct bio *bio)
924 bio_for_each_segment(bv, bio, i) {
925 page = compound_trans_head(bv->bv_page);
926 atomic_dec(&page->_count);
931 bufinit(struct buf *buf, struct request *rq, struct bio *bio)
933 memset(buf, 0, sizeof(*buf));
936 buf->resid = bio->bi_size;
937 buf->sector = bio->bi_sector;
939 buf->bv = bio_iovec(bio);
940 buf->bv_resid = buf->bv->bv_len;
941 WARN_ON(buf->bv_resid == 0);
945 nextbuf(struct aoedev *d)
948 struct request_queue *q;
954 return NULL; /* initializing */
959 rq = blk_peek_request(q);
962 blk_start_request(rq);
964 d->ip.nxbio = rq->bio;
965 rq->special = (void *) rqbiocnt(rq);
967 buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
969 pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
973 bufinit(buf, rq, bio);
978 return d->ip.buf = buf;
981 /* enters with d->lock held */
983 aoecmd_work(struct aoedev *d)
986 while (aoecmd_ata_rw(d))
990 /* this function performs work that has been deferred until sleeping is OK
993 aoecmd_sleepwork(struct work_struct *work)
995 struct aoedev *d = container_of(work, struct aoedev, work);
996 struct block_device *bd;
999 if (d->flags & DEVFL_GDALLOC)
1002 if (d->flags & DEVFL_NEWSIZE) {
1003 ssize = get_capacity(d->gd);
1004 bd = bdget_disk(d->gd, 0);
1006 mutex_lock(&bd->bd_inode->i_mutex);
1007 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
1008 mutex_unlock(&bd->bd_inode->i_mutex);
1011 spin_lock_irq(&d->lock);
1012 d->flags |= DEVFL_UP;
1013 d->flags &= ~DEVFL_NEWSIZE;
1014 spin_unlock_irq(&d->lock);
1019 ata_ident_fixstring(u16 *id, int ns)
1025 *id++ = s >> 8 | s << 8;
1030 ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
1035 /* word 83: command set supported */
1036 n = get_unaligned_le16(&id[83 << 1]);
1038 /* word 86: command set/feature enabled */
1039 n |= get_unaligned_le16(&id[86 << 1]);
1041 if (n & (1<<10)) { /* bit 10: LBA 48 */
1042 d->flags |= DEVFL_EXT;
1044 /* word 100: number lba48 sectors */
1045 ssize = get_unaligned_le64(&id[100 << 1]);
1047 /* set as in ide-disk.c:init_idedisk_capacity */
1048 d->geo.cylinders = ssize;
1049 d->geo.cylinders /= (255 * 63);
1051 d->geo.sectors = 63;
1053 d->flags &= ~DEVFL_EXT;
1055 /* number lba28 sectors */
1056 ssize = get_unaligned_le32(&id[60 << 1]);
1058 /* NOTE: obsolete in ATA 6 */
1059 d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
1060 d->geo.heads = get_unaligned_le16(&id[55 << 1]);
1061 d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
1064 ata_ident_fixstring((u16 *) &id[10<<1], 10); /* serial */
1065 ata_ident_fixstring((u16 *) &id[23<<1], 4); /* firmware */
1066 ata_ident_fixstring((u16 *) &id[27<<1], 20); /* model */
1067 memcpy(d->ident, id, sizeof(d->ident));
1069 if (d->ssize != ssize)
1071 "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
1073 d->aoemajor, d->aoeminor,
1074 d->fw_ver, (long long)ssize);
1077 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
1079 if (d->gd != NULL) {
1080 set_capacity(d->gd, ssize);
1081 d->flags |= DEVFL_NEWSIZE;
1083 d->flags |= DEVFL_GDALLOC;
1084 schedule_work(&d->work);
1088 calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt)
1094 /* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */
1095 n -= d->rttavg >> RTTSCALE;
1099 n -= d->rttdev >> RTTDSCALE;
1102 if (!t || t->maxout >= t->nframes)
1104 if (t->maxout < t->ssthresh)
1106 else if (t->nout == t->maxout && t->next_cwnd-- == 0) {
1108 t->next_cwnd = t->maxout;
1112 static struct aoetgt *
1113 gettgt(struct aoedev *d, char *addr)
1115 struct aoetgt **t, **e;
1118 e = t + d->ntargets;
1119 for (; t < e && *t; t++)
1120 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
1126 bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
1132 fcnt = bv->bv_len - (off - bv->bv_offset);
1135 p = page_address(bv->bv_page) + off;
1136 skb_copy_bits(skb, soff, p, fcnt);
1142 off = bv->bv_offset;
1147 aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
1151 struct request_queue *q;
1158 bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
1159 } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
1161 /* cf. http://lkml.org/lkml/2006/10/31/28 */
1167 aoe_end_buf(struct aoedev *d, struct buf *buf)
1172 if (buf == d->ip.buf)
1175 bio_pagedec(buf->bio);
1176 mempool_free(buf, d->bufpool);
1177 n = (unsigned long) rq->special;
1178 rq->special = (void *) --n;
1180 aoe_end_request(d, rq, 0);
1184 ktiocomplete(struct frame *f)
1186 struct aoe_hdr *hin, *hout;
1187 struct aoe_atahdr *ahin, *ahout;
1189 struct sk_buff *skb;
1203 if (f->flags & FFL_PROBE)
1205 if (!skb) /* just fail the buf. */
1208 hout = (struct aoe_hdr *) skb_mac_header(f->skb);
1209 ahout = (struct aoe_atahdr *) (hout+1);
1211 hin = (struct aoe_hdr *) skb->data;
1212 skb_pull(skb, sizeof(*hin));
1213 ahin = (struct aoe_atahdr *) skb->data;
1214 skb_pull(skb, sizeof(*ahin));
1215 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
1216 pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
1217 ahout->cmdstat, ahin->cmdstat,
1218 d->aoemajor, d->aoeminor);
1220 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1224 n = ahout->scnt << 9;
1225 switch (ahout->cmdstat) {
1226 case ATA_CMD_PIO_READ:
1227 case ATA_CMD_PIO_READ_EXT:
1229 pr_err("%s e%ld.%d. skb->len=%d need=%ld\n",
1230 "aoe: runt data size in read from",
1231 (long) d->aoemajor, d->aoeminor,
1233 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1236 bvcpy(f->bv, f->bv_off, skb, n);
1237 case ATA_CMD_PIO_WRITE:
1238 case ATA_CMD_PIO_WRITE_EXT:
1239 spin_lock_irq(&d->lock);
1240 ifp = getif(t, skb->dev);
1243 spin_unlock_irq(&d->lock);
1245 case ATA_CMD_ID_ATA:
1246 if (skb->len < 512) {
1247 pr_info("%s e%ld.%d. skb->len=%d need=512\n",
1248 "aoe: runt data size in ataid from",
1249 (long) d->aoemajor, d->aoeminor,
1253 if (skb_linearize(skb))
1255 spin_lock_irq(&d->lock);
1256 ataid_complete(d, t, skb->data);
1257 spin_unlock_irq(&d->lock);
1260 pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
1262 be16_to_cpu(get_unaligned(&hin->major)),
1266 spin_lock_irq(&d->lock);
1269 && t->nout_probes == 0) {
1270 count_targets(d, &untainted);
1271 if (untainted > 0) {
1279 if (buf && --buf->nframesout == 0 && buf->resid == 0)
1280 aoe_end_buf(d, buf);
1282 spin_unlock_irq(&d->lock);
1287 /* Enters with iocq.lock held.
1288 * Returns true iff responses needing processing remain.
1294 struct list_head *pos;
1298 for (i = 0; ; ++i) {
1301 if (list_empty(&iocq[id].head))
1303 pos = iocq[id].head.next;
1305 f = list_entry(pos, struct frame, head);
1306 spin_unlock_irq(&iocq[id].lock);
1309 /* Figure out if extra threads are required. */
1310 actual_id = f->t->d->aoeminor % ncpus;
1312 if (!kts[actual_id].active) {
1314 mutex_lock(&ktio_spawn_lock);
1315 if (!kts[actual_id].active
1316 && aoe_ktstart(&kts[actual_id]) == 0)
1317 kts[actual_id].active = 1;
1318 mutex_unlock(&ktio_spawn_lock);
1320 spin_lock_irq(&iocq[id].lock);
1328 DECLARE_WAITQUEUE(wait, current);
1332 current->flags |= PF_NOFREEZE;
1333 set_user_nice(current, -10);
1334 complete(&k->rendez); /* tell spawner we're running */
1336 spin_lock_irq(k->lock);
1337 more = k->fn(k->id);
1339 add_wait_queue(k->waitq, &wait);
1340 __set_current_state(TASK_INTERRUPTIBLE);
1342 spin_unlock_irq(k->lock);
1345 remove_wait_queue(k->waitq, &wait);
1348 } while (!kthread_should_stop());
1349 complete(&k->rendez); /* tell spawner we're stopping */
1354 aoe_ktstop(struct ktstate *k)
1356 kthread_stop(k->task);
1357 wait_for_completion(&k->rendez);
1361 aoe_ktstart(struct ktstate *k)
1363 struct task_struct *task;
1365 init_completion(&k->rendez);
1366 task = kthread_run(kthread, k, "%s", k->name);
1367 if (task == NULL || IS_ERR(task))
1370 wait_for_completion(&k->rendez); /* allow kthread to start */
1371 init_completion(&k->rendez); /* for waiting for exit later */
1375 /* pass it off to kthreads for processing */
1377 ktcomplete(struct frame *f, struct sk_buff *skb)
1383 id = f->t->d->aoeminor % ncpus;
1384 spin_lock_irqsave(&iocq[id].lock, flags);
1385 if (!kts[id].active) {
1386 spin_unlock_irqrestore(&iocq[id].lock, flags);
1387 /* The thread with id has not been spawned yet,
1388 * so delegate the work to the main thread and
1389 * try spawning a new thread.
1392 spin_lock_irqsave(&iocq[id].lock, flags);
1394 list_add_tail(&f->head, &iocq[id].head);
1395 spin_unlock_irqrestore(&iocq[id].lock, flags);
1396 wake_up(&ktiowq[id]);
1400 aoecmd_ata_rsp(struct sk_buff *skb)
1410 h = (struct aoe_hdr *) skb->data;
1411 aoemajor = be16_to_cpu(get_unaligned(&h->major));
1412 d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
1414 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
1415 "for unknown device %d.%d\n",
1416 aoemajor, h->minor);
1421 spin_lock_irqsave(&d->lock, flags);
1423 n = be32_to_cpu(get_unaligned(&h->tag));
1426 calc_rttavg(d, f->t, tsince_hr(f));
1428 if (f->flags & FFL_PROBE)
1429 f->t->nout_probes--;
1431 f = getframe_deferred(d, n);
1433 calc_rttavg(d, NULL, tsince_hr(f));
1435 calc_rttavg(d, NULL, tsince(n));
1436 spin_unlock_irqrestore(&d->lock, flags);
1438 snprintf(ebuf, sizeof(ebuf),
1439 "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n",
1441 get_unaligned_be16(&h->major),
1443 get_unaligned_be32(&h->tag),
1453 spin_unlock_irqrestore(&d->lock, flags);
1458 * Note here that we do not perform an aoedev_put, as we are
1459 * leaving this reference for the ktio to release.
1465 aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
1467 struct sk_buff_head queue;
1469 __skb_queue_head_init(&queue);
1470 aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
1471 aoenet_xmit(&queue);
1475 aoecmd_ata_id(struct aoedev *d)
1478 struct aoe_atahdr *ah;
1480 struct sk_buff *skb;
1489 /* initialize the headers & frame */
1491 h = (struct aoe_hdr *) skb_mac_header(skb);
1492 ah = (struct aoe_atahdr *) (h+1);
1493 skb_put(skb, sizeof *h + sizeof *ah);
1494 memset(h, 0, skb->len);
1495 f->tag = aoehdr_atainit(d, t, h);
1499 f->waited_total = 0;
1501 /* set up ata header */
1503 ah->cmdstat = ATA_CMD_ID_ATA;
1506 skb->dev = t->ifp->nd;
1508 d->rttavg = RTTAVG_INIT;
1509 d->rttdev = RTTDEV_INIT;
1510 d->timer.function = rexmit_timer;
1512 skb = skb_clone(skb, GFP_ATOMIC);
1514 do_gettimeofday(&f->sent);
1515 f->sent_jiffs = (u32) jiffies;
1521 static struct aoetgt **
1522 grow_targets(struct aoedev *d)
1529 tt = kcalloc(newn, sizeof(*d->targets), GFP_ATOMIC);
1532 memmove(tt, d->targets, sizeof(*d->targets) * oldn);
1533 d->tgt = tt + (d->tgt - d->targets);
1538 return &d->targets[oldn];
1541 static struct aoetgt *
1542 addtgt(struct aoedev *d, char *addr, ulong nframes)
1544 struct aoetgt *t, **tt, **te;
1547 te = tt + d->ntargets;
1548 for (; tt < te && *tt; tt++)
1552 tt = grow_targets(d);
1556 t = kzalloc(sizeof(*t), GFP_ATOMIC);
1559 t->nframes = nframes;
1561 memcpy(t->addr, addr, sizeof t->addr);
1564 t->maxout = t->nframes / 2;
1565 INIT_LIST_HEAD(&t->ffree);
1569 pr_info("aoe: cannot allocate memory to add target\n");
1574 setdbcnt(struct aoedev *d)
1576 struct aoetgt **t, **e;
1580 e = t + d->ntargets;
1581 for (; t < e && *t; t++)
1582 if (bcnt == 0 || bcnt > (*t)->minbcnt)
1583 bcnt = (*t)->minbcnt;
1584 if (bcnt != d->maxbcnt) {
1586 pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
1587 d->aoemajor, d->aoeminor, bcnt);
1592 setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
1595 struct aoeif *p, *e;
1602 for (; p < e; p++) {
1604 break; /* end of the valid interfaces */
1606 p->bcnt = bcnt; /* we're updating */
1608 } else if (minbcnt > p->bcnt)
1609 minbcnt = p->bcnt; /* find the min interface */
1613 pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
1620 t->minbcnt = minbcnt;
1625 aoecmd_cfg_rsp(struct sk_buff *skb)
1629 struct aoe_cfghdr *ch;
1631 ulong flags, aoemajor;
1633 struct sk_buff_head queue;
1637 h = (struct aoe_hdr *) skb_mac_header(skb);
1638 ch = (struct aoe_cfghdr *) (h+1);
1641 * Enough people have their dip switches set backwards to
1642 * warrant a loud message for this special case.
1644 aoemajor = get_unaligned_be16(&h->major);
1645 if (aoemajor == 0xfff) {
1646 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
1647 "Check shelf dip switches.\n");
1650 if (aoemajor == 0xffff) {
1651 pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
1652 aoemajor, (int) h->minor);
1655 if (h->minor == 0xff) {
1656 pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
1657 aoemajor, (int) h->minor);
1661 n = be16_to_cpu(ch->bufcnt);
1662 if (n > aoe_maxout) /* keep it reasonable */
1665 d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
1667 pr_info("aoe: device allocation failure\n");
1671 spin_lock_irqsave(&d->lock, flags);
1673 t = gettgt(d, h->src);
1679 t = addtgt(d, h->src, n);
1684 n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
1688 n = n ? n * 512 : DEFAULTBCNT;
1689 setifbcnt(t, skb->dev, n);
1691 /* don't change users' perspective */
1692 if (d->nopen == 0) {
1693 d->fw_ver = be16_to_cpu(ch->fwver);
1694 sl = aoecmd_ata_id(d);
1697 spin_unlock_irqrestore(&d->lock, flags);
1700 __skb_queue_head_init(&queue);
1701 __skb_queue_tail(&queue, sl);
1702 aoenet_xmit(&queue);
1707 aoecmd_wreset(struct aoetgt *t)
1710 t->ssthresh = t->nframes / 2;
1711 t->next_cwnd = t->nframes;
1715 aoecmd_cleanslate(struct aoedev *d)
1717 struct aoetgt **t, **te;
1719 d->rttavg = RTTAVG_INIT;
1720 d->rttdev = RTTDEV_INIT;
1724 te = t + d->ntargets;
1725 for (; t < te && *t; t++)
1730 aoe_failbuf(struct aoedev *d, struct buf *buf)
1735 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1736 if (buf->nframesout == 0)
1737 aoe_end_buf(d, buf);
1741 aoe_flush_iocq(void)
1745 for (i = 0; i < ncpus; i++) {
1747 aoe_flush_iocq_by_index(i);
1752 aoe_flush_iocq_by_index(int id)
1757 struct list_head *pos;
1758 struct sk_buff *skb;
1761 spin_lock_irqsave(&iocq[id].lock, flags);
1762 list_splice_init(&iocq[id].head, &flist);
1763 spin_unlock_irqrestore(&iocq[id].lock, flags);
1764 while (!list_empty(&flist)) {
1767 f = list_entry(pos, struct frame, head);
1770 spin_lock_irqsave(&d->lock, flags);
1772 f->buf->nframesout--;
1773 aoe_failbuf(d, f->buf);
1776 spin_unlock_irqrestore(&d->lock, flags);
1789 /* get_zeroed_page returns page with ref count 1 */
1790 p = (void *) get_zeroed_page(GFP_KERNEL | __GFP_REPEAT);
1793 empty_page = virt_to_page(p);
1795 ncpus = num_online_cpus();
1797 iocq = kcalloc(ncpus, sizeof(struct iocq_ktio), GFP_KERNEL);
1801 kts = kcalloc(ncpus, sizeof(struct ktstate), GFP_KERNEL);
1807 ktiowq = kcalloc(ncpus, sizeof(wait_queue_head_t), GFP_KERNEL);
1813 mutex_init(&ktio_spawn_lock);
1815 for (i = 0; i < ncpus; i++) {
1816 INIT_LIST_HEAD(&iocq[i].head);
1817 spin_lock_init(&iocq[i].lock);
1818 init_waitqueue_head(&ktiowq[i]);
1819 snprintf(kts[i].name, sizeof(kts[i].name), "aoe_ktio%d", i);
1821 kts[i].waitq = &ktiowq[i];
1822 kts[i].lock = &iocq[i].lock;
1827 if (aoe_ktstart(&kts[0])) {
1848 for (i = 0; i < ncpus; i++)
1850 aoe_ktstop(&kts[i]);
1854 /* Free up the iocq and thread speicific configuration
1855 * allocated during startup.
1861 free_page((unsigned long) page_address(empty_page));