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)
384 struct sk_buff_head queue;
396 if (bcnt > buf->resid)
400 f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
402 if (fbcnt < buf->bv_resid) {
403 buf->bv_resid -= fbcnt;
407 fbcnt -= buf->bv_resid;
408 buf->resid -= buf->bv_resid;
409 if (buf->resid == 0) {
414 buf->bv_resid = buf->bv->bv_len;
415 WARN_ON(buf->bv_resid == 0);
418 /* initialize the headers & frame */
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);
429 do_gettimeofday(&f->sent);
430 f->sent_jiffs = (u32) jiffies;
431 __skb_queue_head_init(&queue);
432 __skb_queue_tail(&queue, skb);
438 /* some callers cannot sleep, and they can call this function,
439 * transmitting the packets later, when interrupts are on
442 aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
445 struct aoe_cfghdr *ch;
447 struct net_device *ifp;
450 for_each_netdev_rcu(&init_net, ifp) {
452 if (!is_aoe_netif(ifp))
455 skb = new_skb(sizeof *h + sizeof *ch);
457 printk(KERN_INFO "aoe: skb alloc failure\n");
460 skb_put(skb, sizeof *h + sizeof *ch);
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);
470 h->major = cpu_to_be16(aoemajor);
481 resend(struct aoedev *d, struct frame *f)
484 struct sk_buff_head queue;
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");
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,
506 h->src, h->dst, t->nout);
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);
520 do_gettimeofday(&f->sent);
521 f->sent_jiffs = (u32) jiffies;
522 __skb_queue_head_init(&queue);
523 __skb_queue_tail(&queue, skb);
528 tsince_hr(struct frame *f)
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;
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
551 if (n > USEC_PER_SEC / 4) {
552 n = ((u32) jiffies) - f->sent_jiffs;
553 n *= USEC_PER_SEC / HZ;
564 n = jiffies & 0xffff;
568 return jiffies_to_usecs(n + 1);
571 static struct aoeif *
572 getif(struct aoetgt *t, struct net_device *nd)
585 ejectif(struct aoetgt *t, struct aoeif *ifp)
588 struct net_device *nd;
592 e = t->ifs + NAOEIFS - 1;
593 n = (e - ifp) * sizeof *ifp;
594 memmove(ifp, ifp+1, n);
599 static struct frame *
600 reassign_frame(struct frame *f)
605 nf = newframe(f->t->d);
619 nf->bv_off = f->bv_off;
621 nf->waited_total = f->waited_total;
623 nf->sent_jiffs = f->sent_jiffs;
630 probe(struct aoetgt *t)
635 struct sk_buff_head queue;
642 pr_err("%s %pm for e%ld.%d: %s\n",
643 "aoe: cannot probe remote address",
645 (long) d->aoemajor, d->aoeminor,
646 "no frame available");
649 f->flags |= FFL_PROBE;
651 f->bcnt = t->d->maxbcnt ? t->d->maxbcnt : DEFAULTBCNT;
654 for (frag = 0, n = f->bcnt; n > 0; ++frag, n -= m) {
659 skb_fill_page_desc(skb, frag, empty_page, 0, m);
662 skb->data_len = f->bcnt;
663 skb->truesize += f->bcnt;
665 skb = skb_clone(f->skb, GFP_ATOMIC);
667 do_gettimeofday(&f->sent);
668 f->sent_jiffs = (u32) jiffies;
669 __skb_queue_head_init(&queue);
670 __skb_queue_tail(&queue, skb);
676 rto(struct aoedev *d)
680 t = 2 * d->rttavg >> RTTSCALE;
681 t += 8 * d->rttdev >> RTTDSCALE;
689 rexmit_deferred(struct aoedev *d)
694 struct list_head *pos, *nx, *head;
698 count_targets(d, &untainted);
701 list_for_each_safe(pos, nx, head) {
702 f = list_entry(pos, struct frame, head);
705 if (!(f->flags & FFL_PROBE)) {
706 nf = reassign_frame(f);
708 if (t->nout_probes == 0
713 list_replace(&f->head, &nf->head);
719 } else if (untainted < 1) {
720 /* don't probe w/o other untainted aoetgts */
722 } else if (tsince_hr(f) < t->taint * rto(d)) {
723 /* reprobe slowly when taint is high */
726 } else if (f->flags & FFL_PROBE) {
727 stop_probe: /* don't probe untainted aoetgts */
730 /* leaving d->kicked, because this is routine */
731 f->t->d->flags |= DEVFL_KICKME;
734 if (t->nout >= t->maxout)
738 if (f->flags & FFL_PROBE)
740 since = tsince_hr(f);
742 f->waited_total += since;
747 /* An aoetgt accumulates demerits quickly, and successful
748 * probing redeems the aoetgt slowly.
751 scorn(struct aoetgt *t)
756 t->taint += t->taint * 2;
759 if (t->taint > MAX_TAINT)
760 t->taint = MAX_TAINT;
764 count_targets(struct aoedev *d, int *untainted)
768 for (i = good = 0; i < d->ntargets && d->targets[i]; ++i)
769 if (d->targets[i]->taint == 0)
778 rexmit_timer(ulong vp)
784 struct list_head *head, *pos, *nx;
786 register long timeout;
789 int utgts; /* number of aoetgt descriptors (not slots) */
792 d = (struct aoedev *) vp;
794 spin_lock_irqsave(&d->lock, flags);
796 /* timeout based on observed timings and variations */
799 utgts = count_targets(d, NULL);
801 if (d->flags & DEVFL_TKILL) {
802 spin_unlock_irqrestore(&d->lock, flags);
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)) {
821 f = list_entry(pos, struct frame, head);
822 since = tsince_hr(f);
823 n = f->waited_total + since;
827 && !(f->flags & FFL_PROBE)) {
828 /* Waited too long. Device failure.
829 * Hang all frames on first hash bucket for downdev
832 list_splice(&flist, &d->factive[0]);
838 n = f->waited + since;
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;
849 if (f->flags & FFL_PROBE) {
852 ifp = getif(t, f->skb->dev);
853 if (ifp && ++ifp->lost > (t->nframes << 1)
854 && (ifp != t->ifs || t->ifs[1].nd)) {
859 list_move_tail(pos, &d->rexmitq);
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);
877 rqbiocnt(struct request *r)
882 __rq_for_each_bio(bio, r)
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
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.
898 bio_pageinc(struct bio *bio)
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_trans_head(bv->bv_page);
909 atomic_inc(&page->_count);
914 bio_pagedec(struct bio *bio)
920 bio_for_each_segment(bv, bio, i) {
921 page = compound_trans_head(bv->bv_page);
922 atomic_dec(&page->_count);
927 bufinit(struct buf *buf, struct request *rq, struct bio *bio)
929 memset(buf, 0, sizeof(*buf));
932 buf->resid = bio->bi_size;
933 buf->sector = bio->bi_sector;
935 buf->bv = bio_iovec(bio);
936 buf->bv_resid = buf->bv->bv_len;
937 WARN_ON(buf->bv_resid == 0);
941 nextbuf(struct aoedev *d)
944 struct request_queue *q;
950 return NULL; /* initializing */
955 rq = blk_peek_request(q);
958 blk_start_request(rq);
960 d->ip.nxbio = rq->bio;
961 rq->special = (void *) rqbiocnt(rq);
963 buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
965 pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
969 bufinit(buf, rq, bio);
974 return d->ip.buf = buf;
977 /* enters with d->lock held */
979 aoecmd_work(struct aoedev *d)
982 while (aoecmd_ata_rw(d))
986 /* this function performs work that has been deferred until sleeping is OK
989 aoecmd_sleepwork(struct work_struct *work)
991 struct aoedev *d = container_of(work, struct aoedev, work);
992 struct block_device *bd;
995 if (d->flags & DEVFL_GDALLOC)
998 if (d->flags & DEVFL_NEWSIZE) {
999 ssize = get_capacity(d->gd);
1000 bd = bdget_disk(d->gd, 0);
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);
1007 spin_lock_irq(&d->lock);
1008 d->flags |= DEVFL_UP;
1009 d->flags &= ~DEVFL_NEWSIZE;
1010 spin_unlock_irq(&d->lock);
1015 ata_ident_fixstring(u16 *id, int ns)
1021 *id++ = s >> 8 | s << 8;
1026 ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
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);
1047 d->geo.sectors = 63;
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)
1067 "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
1069 d->aoemajor, d->aoeminor,
1070 d->fw_ver, (long long)ssize);
1073 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
1075 if (d->gd != NULL) {
1076 set_capacity(d->gd, ssize);
1077 d->flags |= DEVFL_NEWSIZE;
1079 d->flags |= DEVFL_GDALLOC;
1080 schedule_work(&d->work);
1084 calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt)
1090 /* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */
1091 n -= d->rttavg >> RTTSCALE;
1095 n -= d->rttdev >> RTTDSCALE;
1098 if (!t || t->maxout >= t->nframes)
1100 if (t->maxout < t->ssthresh)
1102 else if (t->nout == t->maxout && t->next_cwnd-- == 0) {
1104 t->next_cwnd = t->maxout;
1108 static struct aoetgt *
1109 gettgt(struct aoedev *d, char *addr)
1111 struct aoetgt **t, **e;
1114 e = t + d->ntargets;
1115 for (; t < e && *t; t++)
1116 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
1122 bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
1128 fcnt = bv->bv_len - (off - bv->bv_offset);
1131 p = page_address(bv->bv_page) + off;
1132 skb_copy_bits(skb, soff, p, fcnt);
1138 off = bv->bv_offset;
1143 aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
1147 struct request_queue *q;
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 */
1163 aoe_end_buf(struct aoedev *d, struct buf *buf)
1168 if (buf == d->ip.buf)
1171 bio_pagedec(buf->bio);
1172 mempool_free(buf, d->bufpool);
1173 n = (unsigned long) rq->special;
1174 rq->special = (void *) --n;
1176 aoe_end_request(d, rq, 0);
1180 ktiocomplete(struct frame *f)
1182 struct aoe_hdr *hin, *hout;
1183 struct aoe_atahdr *ahin, *ahout;
1185 struct sk_buff *skb;
1199 if (f->flags & FFL_PROBE)
1201 if (!skb) /* just fail the buf. */
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);
1216 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1220 n = ahout->scnt << 9;
1221 switch (ahout->cmdstat) {
1222 case ATA_CMD_PIO_READ:
1223 case ATA_CMD_PIO_READ_EXT:
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,
1229 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
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);
1239 spin_unlock_irq(&d->lock);
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,
1249 if (skb_linearize(skb))
1251 spin_lock_irq(&d->lock);
1252 ataid_complete(d, t, skb->data);
1253 spin_unlock_irq(&d->lock);
1256 pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
1258 be16_to_cpu(get_unaligned(&hin->major)),
1262 spin_lock_irq(&d->lock);
1265 && t->nout_probes == 0) {
1266 count_targets(d, &untainted);
1267 if (untainted > 0) {
1275 if (buf && --buf->nframesout == 0 && buf->resid == 0)
1276 aoe_end_buf(d, buf);
1278 spin_unlock_irq(&d->lock);
1283 /* Enters with iocq.lock held.
1284 * Returns true iff responses needing processing remain.
1290 struct list_head *pos;
1294 for (i = 0; ; ++i) {
1297 if (list_empty(&iocq[id].head))
1299 pos = iocq[id].head.next;
1301 f = list_entry(pos, struct frame, head);
1302 spin_unlock_irq(&iocq[id].lock);
1305 /* Figure out if extra threads are required. */
1306 actual_id = f->t->d->aoeminor % ncpus;
1308 if (!kts[actual_id].active) {
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);
1324 DECLARE_WAITQUEUE(wait, current);
1328 current->flags |= PF_NOFREEZE;
1329 set_user_nice(current, -10);
1330 complete(&k->rendez); /* tell spawner we're running */
1332 spin_lock_irq(k->lock);
1333 more = k->fn(k->id);
1335 add_wait_queue(k->waitq, &wait);
1336 __set_current_state(TASK_INTERRUPTIBLE);
1338 spin_unlock_irq(k->lock);
1341 remove_wait_queue(k->waitq, &wait);
1344 } while (!kthread_should_stop());
1345 complete(&k->rendez); /* tell spawner we're stopping */
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))
1366 wait_for_completion(&k->rendez); /* allow kthread to start */
1367 init_completion(&k->rendez); /* for waiting for exit later */
1371 /* pass it off to kthreads for processing */
1373 ktcomplete(struct frame *f, struct sk_buff *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.
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]);
1396 aoecmd_ata_rsp(struct sk_buff *skb)
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);
1410 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
1411 "for unknown device %d.%d\n",
1412 aoemajor, h->minor);
1417 spin_lock_irqsave(&d->lock, flags);
1419 n = be32_to_cpu(get_unaligned(&h->tag));
1422 calc_rttavg(d, f->t, tsince_hr(f));
1424 if (f->flags & FFL_PROBE)
1425 f->t->nout_probes--;
1427 f = getframe_deferred(d, n);
1429 calc_rttavg(d, NULL, tsince_hr(f));
1431 calc_rttavg(d, NULL, tsince(n));
1432 spin_unlock_irqrestore(&d->lock, flags);
1434 snprintf(ebuf, sizeof(ebuf),
1435 "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n",
1437 get_unaligned_be16(&h->major),
1439 get_unaligned_be32(&h->tag),
1449 spin_unlock_irqrestore(&d->lock, flags);
1454 * Note here that we do not perform an aoedev_put, as we are
1455 * leaving this reference for the ktio to release.
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);
1471 aoecmd_ata_id(struct aoedev *d)
1474 struct aoe_atahdr *ah;
1476 struct sk_buff *skb;
1485 /* initialize the headers & frame */
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);
1495 f->waited_total = 0;
1497 /* set up ata header */
1499 ah->cmdstat = ATA_CMD_ID_ATA;
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);
1510 do_gettimeofday(&f->sent);
1511 f->sent_jiffs = (u32) jiffies;
1517 static struct aoetgt **
1518 grow_targets(struct aoedev *d)
1525 tt = kcalloc(newn, sizeof(*d->targets), GFP_ATOMIC);
1528 memmove(tt, d->targets, sizeof(*d->targets) * oldn);
1529 d->tgt = tt + (d->tgt - d->targets);
1534 return &d->targets[oldn];
1537 static struct aoetgt *
1538 addtgt(struct aoedev *d, char *addr, ulong nframes)
1540 struct aoetgt *t, **tt, **te;
1543 te = tt + d->ntargets;
1544 for (; tt < te && *tt; tt++)
1548 tt = grow_targets(d);
1552 t = kzalloc(sizeof(*t), GFP_ATOMIC);
1555 t->nframes = nframes;
1557 memcpy(t->addr, addr, sizeof t->addr);
1560 t->maxout = t->nframes / 2;
1561 INIT_LIST_HEAD(&t->ffree);
1565 pr_info("aoe: cannot allocate memory to add target\n");
1570 setdbcnt(struct aoedev *d)
1572 struct aoetgt **t, **e;
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) {
1582 pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
1583 d->aoemajor, d->aoeminor, bcnt);
1588 setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
1591 struct aoeif *p, *e;
1598 for (; p < e; p++) {
1600 break; /* end of the valid interfaces */
1602 p->bcnt = bcnt; /* we're updating */
1604 } else if (minbcnt > p->bcnt)
1605 minbcnt = p->bcnt; /* find the min interface */
1609 pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
1616 t->minbcnt = minbcnt;
1621 aoecmd_cfg_rsp(struct sk_buff *skb)
1625 struct aoe_cfghdr *ch;
1627 ulong flags, aoemajor;
1629 struct sk_buff_head queue;
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");
1646 if (aoemajor == 0xffff) {
1647 pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
1648 aoemajor, (int) h->minor);
1651 if (h->minor == 0xff) {
1652 pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
1653 aoemajor, (int) h->minor);
1657 n = be16_to_cpu(ch->bufcnt);
1658 if (n > aoe_maxout) /* keep it reasonable */
1661 d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
1663 pr_info("aoe: device allocation failure\n");
1667 spin_lock_irqsave(&d->lock, flags);
1669 t = gettgt(d, h->src);
1675 t = addtgt(d, h->src, n);
1680 n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
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);
1693 spin_unlock_irqrestore(&d->lock, flags);
1696 __skb_queue_head_init(&queue);
1697 __skb_queue_tail(&queue, sl);
1698 aoenet_xmit(&queue);
1703 aoecmd_wreset(struct aoetgt *t)
1706 t->ssthresh = t->nframes / 2;
1707 t->next_cwnd = t->nframes;
1711 aoecmd_cleanslate(struct aoedev *d)
1713 struct aoetgt **t, **te;
1715 d->rttavg = RTTAVG_INIT;
1716 d->rttdev = RTTDEV_INIT;
1720 te = t + d->ntargets;
1721 for (; t < te && *t; t++)
1726 aoe_failbuf(struct aoedev *d, struct buf *buf)
1731 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1732 if (buf->nframesout == 0)
1733 aoe_end_buf(d, buf);
1737 aoe_flush_iocq(void)
1741 for (i = 0; i < ncpus; i++) {
1743 aoe_flush_iocq_by_index(i);
1748 aoe_flush_iocq_by_index(int id)
1753 struct list_head *pos;
1754 struct sk_buff *skb;
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)) {
1763 f = list_entry(pos, struct frame, head);
1766 spin_lock_irqsave(&d->lock, flags);
1768 f->buf->nframesout--;
1769 aoe_failbuf(d, f->buf);
1772 spin_unlock_irqrestore(&d->lock, flags);
1785 /* get_zeroed_page returns page with ref count 1 */
1786 p = (void *) get_zeroed_page(GFP_KERNEL | __GFP_REPEAT);
1789 empty_page = virt_to_page(p);
1791 ncpus = num_online_cpus();
1793 iocq = kcalloc(ncpus, sizeof(struct iocq_ktio), GFP_KERNEL);
1797 kts = kcalloc(ncpus, sizeof(struct ktstate), GFP_KERNEL);
1803 ktiowq = kcalloc(ncpus, sizeof(wait_queue_head_t), GFP_KERNEL);
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);
1817 kts[i].waitq = &ktiowq[i];
1818 kts[i].lock = &iocq[i].lock;
1823 if (aoe_ktstart(&kts[0])) {
1844 for (i = 0; i < ncpus; i++)
1846 aoe_ktstop(&kts[i]);
1850 /* Free up the iocq and thread speicific configuration
1851 * allocated during startup.
1857 free_page((unsigned long) page_address(empty_page));