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)
199 memset(&f->iter, 0, sizeof(f->iter));
202 list_add(&f->head, &t->ffree);
205 static struct frame *
206 newtframe(struct aoedev *d, struct aoetgt *t)
210 struct list_head *pos;
212 if (list_empty(&t->ffree)) {
213 if (t->falloc >= NSKBPOOLMAX*2)
215 f = kcalloc(1, sizeof(*f), GFP_ATOMIC);
223 f = list_entry(pos, struct frame, head);
228 f->skb = skb = new_skb(ETH_ZLEN);
230 bail: aoe_freetframe(f);
235 if (atomic_read(&skb_shinfo(skb)->dataref) != 1) {
236 skb = skb_pool_get(d);
239 skb_pool_put(d, f->skb);
243 skb->truesize -= skb->data_len;
244 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
249 static struct frame *
250 newframe(struct aoedev *d)
253 struct aoetgt *t, **tt;
258 if (!d->targets || !d->targets[0]) {
259 printk(KERN_ERR "aoe: NULL TARGETS!\n");
262 tt = d->tgt; /* last used target */
263 for (use_tainted = 0, has_untainted = 0;;) {
265 if (tt >= &d->targets[d->ntargets] || !*tt)
272 if (t->nout < t->maxout
273 && (use_tainted || !t->taint)
282 if (tt == d->tgt) { /* we've looped and found nada */
283 if (!use_tainted && !has_untainted)
291 d->flags |= DEVFL_KICKME;
297 skb_fillup(struct sk_buff *skb, struct bio *bio, struct bvec_iter iter)
302 __bio_for_each_segment(bv, bio, iter, iter)
303 skb_fill_page_desc(skb, frag++, bv.bv_page,
304 bv.bv_offset, bv.bv_len);
308 fhash(struct frame *f)
310 struct aoedev *d = f->t->d;
313 n = f->tag % NFACTIVE;
314 list_add_tail(&f->head, &d->factive[n]);
318 ata_rw_frameinit(struct frame *f)
322 struct aoe_atahdr *ah;
324 char writebit, extbit;
327 h = (struct aoe_hdr *) skb_mac_header(skb);
328 ah = (struct aoe_atahdr *) (h + 1);
329 skb_put(skb, sizeof(*h) + sizeof(*ah));
330 memset(h, 0, skb->len);
336 f->tag = aoehdr_atainit(t->d, t, h);
342 /* set up ata header */
343 ah->scnt = f->iter.bi_size >> 9;
344 put_lba(ah, f->iter.bi_sector);
345 if (t->d->flags & DEVFL_EXT) {
346 ah->aflags |= AOEAFL_EXT;
350 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
352 if (f->buf && bio_data_dir(f->buf->bio) == WRITE) {
353 skb_fillup(skb, f->buf->bio, f->iter);
354 ah->aflags |= AOEAFL_WRITE;
355 skb->len += f->iter.bi_size;
356 skb->data_len = f->iter.bi_size;
357 skb->truesize += f->iter.bi_size;
364 ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
365 skb->dev = t->ifp->nd;
369 aoecmd_ata_rw(struct aoedev *d)
374 struct sk_buff_head queue;
383 /* initialize the headers & frame */
386 f->iter.bi_size = min_t(unsigned long,
387 d->maxbcnt ?: DEFAULTBCNT,
389 bio_advance_iter(buf->bio, &buf->iter, f->iter.bi_size);
391 if (!buf->iter.bi_size)
394 /* mark all tracking fields and load out */
395 buf->nframesout += 1;
399 skb = skb_clone(f->skb, GFP_ATOMIC);
401 do_gettimeofday(&f->sent);
402 f->sent_jiffs = (u32) jiffies;
403 __skb_queue_head_init(&queue);
404 __skb_queue_tail(&queue, skb);
410 /* some callers cannot sleep, and they can call this function,
411 * transmitting the packets later, when interrupts are on
414 aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
417 struct aoe_cfghdr *ch;
419 struct net_device *ifp;
422 for_each_netdev_rcu(&init_net, ifp) {
424 if (!is_aoe_netif(ifp))
427 skb = new_skb(sizeof *h + sizeof *ch);
429 printk(KERN_INFO "aoe: skb alloc failure\n");
432 skb_put(skb, sizeof *h + sizeof *ch);
434 __skb_queue_tail(queue, skb);
435 h = (struct aoe_hdr *) skb_mac_header(skb);
436 memset(h, 0, sizeof *h + sizeof *ch);
438 memset(h->dst, 0xff, sizeof h->dst);
439 memcpy(h->src, ifp->dev_addr, sizeof h->src);
440 h->type = __constant_cpu_to_be16(ETH_P_AOE);
442 h->major = cpu_to_be16(aoemajor);
453 resend(struct aoedev *d, struct frame *f)
456 struct sk_buff_head queue;
465 if (ifrotate(t) == NULL) {
466 /* probably can't happen, but set it up to fail anyway */
467 pr_info("aoe: resend: no interfaces to rotate to.\n");
471 h = (struct aoe_hdr *) skb_mac_header(skb);
473 if (!(f->flags & FFL_PROBE)) {
474 snprintf(buf, sizeof(buf),
475 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
476 "retransmit", d->aoemajor, d->aoeminor,
478 h->src, h->dst, t->nout);
484 h->tag = cpu_to_be32(n);
485 memcpy(h->dst, t->addr, sizeof h->dst);
486 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
488 skb->dev = t->ifp->nd;
489 skb = skb_clone(skb, GFP_ATOMIC);
492 do_gettimeofday(&f->sent);
493 f->sent_jiffs = (u32) jiffies;
494 __skb_queue_head_init(&queue);
495 __skb_queue_tail(&queue, skb);
500 tsince_hr(struct frame *f)
505 do_gettimeofday(&now);
506 n = now.tv_usec - f->sent.tv_usec;
507 n += (now.tv_sec - f->sent.tv_sec) * USEC_PER_SEC;
512 /* For relatively long periods, use jiffies to avoid
513 * discrepancies caused by updates to the system time.
515 * On system with HZ of 1000, 32-bits is over 49 days
516 * worth of jiffies, or over 71 minutes worth of usecs.
518 * Jiffies overflow is handled by subtraction of unsigned ints:
519 * (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe
523 if (n > USEC_PER_SEC / 4) {
524 n = ((u32) jiffies) - f->sent_jiffs;
525 n *= USEC_PER_SEC / HZ;
536 n = jiffies & 0xffff;
540 return jiffies_to_usecs(n + 1);
543 static struct aoeif *
544 getif(struct aoetgt *t, struct net_device *nd)
557 ejectif(struct aoetgt *t, struct aoeif *ifp)
560 struct net_device *nd;
564 e = t->ifs + NAOEIFS - 1;
565 n = (e - ifp) * sizeof *ifp;
566 memmove(ifp, ifp+1, n);
571 static struct frame *
572 reassign_frame(struct frame *f)
577 nf = newframe(f->t->d);
590 nf->waited_total = f->waited_total;
592 nf->sent_jiffs = f->sent_jiffs;
599 probe(struct aoetgt *t)
604 struct sk_buff_head queue;
611 pr_err("%s %pm for e%ld.%d: %s\n",
612 "aoe: cannot probe remote address",
614 (long) d->aoemajor, d->aoeminor,
615 "no frame available");
618 f->flags |= FFL_PROBE;
620 f->iter.bi_size = t->d->maxbcnt ? t->d->maxbcnt : DEFAULTBCNT;
623 for (frag = 0, n = f->iter.bi_size; n > 0; ++frag, n -= m) {
628 skb_fill_page_desc(skb, frag, empty_page, 0, m);
630 skb->len += f->iter.bi_size;
631 skb->data_len = f->iter.bi_size;
632 skb->truesize += f->iter.bi_size;
634 skb = skb_clone(f->skb, GFP_ATOMIC);
636 do_gettimeofday(&f->sent);
637 f->sent_jiffs = (u32) jiffies;
638 __skb_queue_head_init(&queue);
639 __skb_queue_tail(&queue, skb);
645 rto(struct aoedev *d)
649 t = 2 * d->rttavg >> RTTSCALE;
650 t += 8 * d->rttdev >> RTTDSCALE;
658 rexmit_deferred(struct aoedev *d)
663 struct list_head *pos, *nx, *head;
667 count_targets(d, &untainted);
670 list_for_each_safe(pos, nx, head) {
671 f = list_entry(pos, struct frame, head);
674 if (!(f->flags & FFL_PROBE)) {
675 nf = reassign_frame(f);
677 if (t->nout_probes == 0
682 list_replace(&f->head, &nf->head);
688 } else if (untainted < 1) {
689 /* don't probe w/o other untainted aoetgts */
691 } else if (tsince_hr(f) < t->taint * rto(d)) {
692 /* reprobe slowly when taint is high */
695 } else if (f->flags & FFL_PROBE) {
696 stop_probe: /* don't probe untainted aoetgts */
699 /* leaving d->kicked, because this is routine */
700 f->t->d->flags |= DEVFL_KICKME;
703 if (t->nout >= t->maxout)
707 if (f->flags & FFL_PROBE)
709 since = tsince_hr(f);
711 f->waited_total += since;
716 /* An aoetgt accumulates demerits quickly, and successful
717 * probing redeems the aoetgt slowly.
720 scorn(struct aoetgt *t)
725 t->taint += t->taint * 2;
728 if (t->taint > MAX_TAINT)
729 t->taint = MAX_TAINT;
733 count_targets(struct aoedev *d, int *untainted)
737 for (i = good = 0; i < d->ntargets && d->targets[i]; ++i)
738 if (d->targets[i]->taint == 0)
747 rexmit_timer(ulong vp)
753 struct list_head *head, *pos, *nx;
755 register long timeout;
758 int utgts; /* number of aoetgt descriptors (not slots) */
761 d = (struct aoedev *) vp;
763 spin_lock_irqsave(&d->lock, flags);
765 /* timeout based on observed timings and variations */
768 utgts = count_targets(d, NULL);
770 if (d->flags & DEVFL_TKILL) {
771 spin_unlock_irqrestore(&d->lock, flags);
775 /* collect all frames to rexmit into flist */
776 for (i = 0; i < NFACTIVE; i++) {
777 head = &d->factive[i];
778 list_for_each_safe(pos, nx, head) {
779 f = list_entry(pos, struct frame, head);
780 if (tsince_hr(f) < timeout)
781 break; /* end of expired frames */
782 /* move to flist for later processing */
783 list_move_tail(pos, &flist);
787 /* process expired frames */
788 while (!list_empty(&flist)) {
790 f = list_entry(pos, struct frame, head);
791 since = tsince_hr(f);
792 n = f->waited_total + since;
796 && !(f->flags & FFL_PROBE)) {
797 /* Waited too long. Device failure.
798 * Hang all frames on first hash bucket for downdev
801 list_splice(&flist, &d->factive[0]);
807 n = f->waited + since;
809 if (aoe_deadsecs && utgts > 0
810 && (n > aoe_deadsecs / utgts || n > HARD_SCORN_SECS))
811 scorn(t); /* avoid this target */
813 if (t->maxout != 1) {
814 t->ssthresh = t->maxout / 2;
818 if (f->flags & FFL_PROBE) {
821 ifp = getif(t, f->skb->dev);
822 if (ifp && ++ifp->lost > (t->nframes << 1)
823 && (ifp != t->ifs || t->ifs[1].nd)) {
828 list_move_tail(pos, &d->rexmitq);
834 if ((d->flags & DEVFL_KICKME) && d->blkq) {
835 d->flags &= ~DEVFL_KICKME;
836 d->blkq->request_fn(d->blkq);
839 d->timer.expires = jiffies + TIMERTICK;
840 add_timer(&d->timer);
842 spin_unlock_irqrestore(&d->lock, flags);
846 rqbiocnt(struct request *r)
851 __rq_for_each_bio(bio, r)
856 /* This can be removed if we are certain that no users of the block
857 * layer will ever use zero-count pages in bios. Otherwise we have to
858 * protect against the put_page sometimes done by the network layer.
860 * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
863 * We cannot use get_page in the workaround, because it insists on a
864 * positive page count as a precondition. So we use _count directly.
867 bio_pageinc(struct bio *bio)
871 struct bvec_iter iter;
873 bio_for_each_segment(bv, bio, iter) {
874 /* Non-zero page count for non-head members of
875 * compound pages is no longer allowed by the kernel.
877 page = compound_head(bv.bv_page);
878 atomic_inc(&page->_count);
883 bio_pagedec(struct bio *bio)
887 struct bvec_iter iter;
889 bio_for_each_segment(bv, bio, iter) {
890 page = compound_head(bv.bv_page);
891 atomic_dec(&page->_count);
896 bufinit(struct buf *buf, struct request *rq, struct bio *bio)
898 memset(buf, 0, sizeof(*buf));
901 buf->iter = bio->bi_iter;
906 nextbuf(struct aoedev *d)
909 struct request_queue *q;
915 return NULL; /* initializing */
920 rq = blk_peek_request(q);
923 blk_start_request(rq);
925 d->ip.nxbio = rq->bio;
926 rq->special = (void *) rqbiocnt(rq);
928 buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
930 pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
934 bufinit(buf, rq, bio);
939 return d->ip.buf = buf;
942 /* enters with d->lock held */
944 aoecmd_work(struct aoedev *d)
947 while (aoecmd_ata_rw(d))
951 /* this function performs work that has been deferred until sleeping is OK
954 aoecmd_sleepwork(struct work_struct *work)
956 struct aoedev *d = container_of(work, struct aoedev, work);
957 struct block_device *bd;
960 if (d->flags & DEVFL_GDALLOC)
963 if (d->flags & DEVFL_NEWSIZE) {
964 ssize = get_capacity(d->gd);
965 bd = bdget_disk(d->gd, 0);
967 mutex_lock(&bd->bd_inode->i_mutex);
968 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
969 mutex_unlock(&bd->bd_inode->i_mutex);
972 spin_lock_irq(&d->lock);
973 d->flags |= DEVFL_UP;
974 d->flags &= ~DEVFL_NEWSIZE;
975 spin_unlock_irq(&d->lock);
980 ata_ident_fixstring(u16 *id, int ns)
986 *id++ = s >> 8 | s << 8;
991 ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
996 /* word 83: command set supported */
997 n = get_unaligned_le16(&id[83 << 1]);
999 /* word 86: command set/feature enabled */
1000 n |= get_unaligned_le16(&id[86 << 1]);
1002 if (n & (1<<10)) { /* bit 10: LBA 48 */
1003 d->flags |= DEVFL_EXT;
1005 /* word 100: number lba48 sectors */
1006 ssize = get_unaligned_le64(&id[100 << 1]);
1008 /* set as in ide-disk.c:init_idedisk_capacity */
1009 d->geo.cylinders = ssize;
1010 d->geo.cylinders /= (255 * 63);
1012 d->geo.sectors = 63;
1014 d->flags &= ~DEVFL_EXT;
1016 /* number lba28 sectors */
1017 ssize = get_unaligned_le32(&id[60 << 1]);
1019 /* NOTE: obsolete in ATA 6 */
1020 d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
1021 d->geo.heads = get_unaligned_le16(&id[55 << 1]);
1022 d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
1025 ata_ident_fixstring((u16 *) &id[10<<1], 10); /* serial */
1026 ata_ident_fixstring((u16 *) &id[23<<1], 4); /* firmware */
1027 ata_ident_fixstring((u16 *) &id[27<<1], 20); /* model */
1028 memcpy(d->ident, id, sizeof(d->ident));
1030 if (d->ssize != ssize)
1032 "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
1034 d->aoemajor, d->aoeminor,
1035 d->fw_ver, (long long)ssize);
1038 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
1040 if (d->gd != NULL) {
1041 set_capacity(d->gd, ssize);
1042 d->flags |= DEVFL_NEWSIZE;
1044 d->flags |= DEVFL_GDALLOC;
1045 schedule_work(&d->work);
1049 calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt)
1055 /* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */
1056 n -= d->rttavg >> RTTSCALE;
1060 n -= d->rttdev >> RTTDSCALE;
1063 if (!t || t->maxout >= t->nframes)
1065 if (t->maxout < t->ssthresh)
1067 else if (t->nout == t->maxout && t->next_cwnd-- == 0) {
1069 t->next_cwnd = t->maxout;
1073 static struct aoetgt *
1074 gettgt(struct aoedev *d, char *addr)
1076 struct aoetgt **t, **e;
1079 e = t + d->ntargets;
1080 for (; t < e && *t; t++)
1081 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
1087 bvcpy(struct sk_buff *skb, struct bio *bio, struct bvec_iter iter, long cnt)
1094 __bio_for_each_segment(bv, bio, iter, iter) {
1095 char *p = page_address(bv.bv_page) + bv.bv_offset;
1096 skb_copy_bits(skb, soff, p, bv.bv_len);
1102 aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
1106 struct request_queue *q;
1113 bok = !fastfail && !bio->bi_error;
1114 } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_iter.bi_size));
1116 /* cf. http://lkml.org/lkml/2006/10/31/28 */
1122 aoe_end_buf(struct aoedev *d, struct buf *buf)
1127 if (buf == d->ip.buf)
1130 bio_pagedec(buf->bio);
1131 mempool_free(buf, d->bufpool);
1132 n = (unsigned long) rq->special;
1133 rq->special = (void *) --n;
1135 aoe_end_request(d, rq, 0);
1139 ktiocomplete(struct frame *f)
1141 struct aoe_hdr *hin, *hout;
1142 struct aoe_atahdr *ahin, *ahout;
1144 struct sk_buff *skb;
1158 if (f->flags & FFL_PROBE)
1160 if (!skb) /* just fail the buf. */
1163 hout = (struct aoe_hdr *) skb_mac_header(f->skb);
1164 ahout = (struct aoe_atahdr *) (hout+1);
1166 hin = (struct aoe_hdr *) skb->data;
1167 skb_pull(skb, sizeof(*hin));
1168 ahin = (struct aoe_atahdr *) skb->data;
1169 skb_pull(skb, sizeof(*ahin));
1170 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
1171 pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
1172 ahout->cmdstat, ahin->cmdstat,
1173 d->aoemajor, d->aoeminor);
1175 buf->bio->bi_error = -EIO;
1179 n = ahout->scnt << 9;
1180 switch (ahout->cmdstat) {
1181 case ATA_CMD_PIO_READ:
1182 case ATA_CMD_PIO_READ_EXT:
1184 pr_err("%s e%ld.%d. skb->len=%d need=%ld\n",
1185 "aoe: runt data size in read from",
1186 (long) d->aoemajor, d->aoeminor,
1188 buf->bio->bi_error = -EIO;
1191 if (n > f->iter.bi_size) {
1192 pr_err_ratelimited("%s e%ld.%d. bytes=%ld need=%u\n",
1193 "aoe: too-large data size in read from",
1194 (long) d->aoemajor, d->aoeminor,
1195 n, f->iter.bi_size);
1196 buf->bio->bi_error = -EIO;
1199 bvcpy(skb, f->buf->bio, f->iter, n);
1200 case ATA_CMD_PIO_WRITE:
1201 case ATA_CMD_PIO_WRITE_EXT:
1202 spin_lock_irq(&d->lock);
1203 ifp = getif(t, skb->dev);
1206 spin_unlock_irq(&d->lock);
1208 case ATA_CMD_ID_ATA:
1209 if (skb->len < 512) {
1210 pr_info("%s e%ld.%d. skb->len=%d need=512\n",
1211 "aoe: runt data size in ataid from",
1212 (long) d->aoemajor, d->aoeminor,
1216 if (skb_linearize(skb))
1218 spin_lock_irq(&d->lock);
1219 ataid_complete(d, t, skb->data);
1220 spin_unlock_irq(&d->lock);
1223 pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
1225 be16_to_cpu(get_unaligned(&hin->major)),
1229 spin_lock_irq(&d->lock);
1232 && t->nout_probes == 0) {
1233 count_targets(d, &untainted);
1234 if (untainted > 0) {
1242 if (buf && --buf->nframesout == 0 && buf->iter.bi_size == 0)
1243 aoe_end_buf(d, buf);
1245 spin_unlock_irq(&d->lock);
1250 /* Enters with iocq.lock held.
1251 * Returns true iff responses needing processing remain.
1257 struct list_head *pos;
1261 for (i = 0; ; ++i) {
1264 if (list_empty(&iocq[id].head))
1266 pos = iocq[id].head.next;
1268 f = list_entry(pos, struct frame, head);
1269 spin_unlock_irq(&iocq[id].lock);
1272 /* Figure out if extra threads are required. */
1273 actual_id = f->t->d->aoeminor % ncpus;
1275 if (!kts[actual_id].active) {
1277 mutex_lock(&ktio_spawn_lock);
1278 if (!kts[actual_id].active
1279 && aoe_ktstart(&kts[actual_id]) == 0)
1280 kts[actual_id].active = 1;
1281 mutex_unlock(&ktio_spawn_lock);
1283 spin_lock_irq(&iocq[id].lock);
1291 DECLARE_WAITQUEUE(wait, current);
1295 current->flags |= PF_NOFREEZE;
1296 set_user_nice(current, -10);
1297 complete(&k->rendez); /* tell spawner we're running */
1299 spin_lock_irq(k->lock);
1300 more = k->fn(k->id);
1302 add_wait_queue(k->waitq, &wait);
1303 __set_current_state(TASK_INTERRUPTIBLE);
1305 spin_unlock_irq(k->lock);
1308 remove_wait_queue(k->waitq, &wait);
1311 } while (!kthread_should_stop());
1312 complete(&k->rendez); /* tell spawner we're stopping */
1317 aoe_ktstop(struct ktstate *k)
1319 kthread_stop(k->task);
1320 wait_for_completion(&k->rendez);
1324 aoe_ktstart(struct ktstate *k)
1326 struct task_struct *task;
1328 init_completion(&k->rendez);
1329 task = kthread_run(kthread, k, "%s", k->name);
1330 if (task == NULL || IS_ERR(task))
1333 wait_for_completion(&k->rendez); /* allow kthread to start */
1334 init_completion(&k->rendez); /* for waiting for exit later */
1338 /* pass it off to kthreads for processing */
1340 ktcomplete(struct frame *f, struct sk_buff *skb)
1346 id = f->t->d->aoeminor % ncpus;
1347 spin_lock_irqsave(&iocq[id].lock, flags);
1348 if (!kts[id].active) {
1349 spin_unlock_irqrestore(&iocq[id].lock, flags);
1350 /* The thread with id has not been spawned yet,
1351 * so delegate the work to the main thread and
1352 * try spawning a new thread.
1355 spin_lock_irqsave(&iocq[id].lock, flags);
1357 list_add_tail(&f->head, &iocq[id].head);
1358 spin_unlock_irqrestore(&iocq[id].lock, flags);
1359 wake_up(&ktiowq[id]);
1363 aoecmd_ata_rsp(struct sk_buff *skb)
1373 h = (struct aoe_hdr *) skb->data;
1374 aoemajor = be16_to_cpu(get_unaligned(&h->major));
1375 d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
1377 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
1378 "for unknown device %d.%d\n",
1379 aoemajor, h->minor);
1384 spin_lock_irqsave(&d->lock, flags);
1386 n = be32_to_cpu(get_unaligned(&h->tag));
1389 calc_rttavg(d, f->t, tsince_hr(f));
1391 if (f->flags & FFL_PROBE)
1392 f->t->nout_probes--;
1394 f = getframe_deferred(d, n);
1396 calc_rttavg(d, NULL, tsince_hr(f));
1398 calc_rttavg(d, NULL, tsince(n));
1399 spin_unlock_irqrestore(&d->lock, flags);
1401 snprintf(ebuf, sizeof(ebuf),
1402 "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n",
1404 get_unaligned_be16(&h->major),
1406 get_unaligned_be32(&h->tag),
1416 spin_unlock_irqrestore(&d->lock, flags);
1421 * Note here that we do not perform an aoedev_put, as we are
1422 * leaving this reference for the ktio to release.
1428 aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
1430 struct sk_buff_head queue;
1432 __skb_queue_head_init(&queue);
1433 aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
1434 aoenet_xmit(&queue);
1438 aoecmd_ata_id(struct aoedev *d)
1441 struct aoe_atahdr *ah;
1443 struct sk_buff *skb;
1452 /* initialize the headers & frame */
1454 h = (struct aoe_hdr *) skb_mac_header(skb);
1455 ah = (struct aoe_atahdr *) (h+1);
1456 skb_put(skb, sizeof *h + sizeof *ah);
1457 memset(h, 0, skb->len);
1458 f->tag = aoehdr_atainit(d, t, h);
1462 f->waited_total = 0;
1464 /* set up ata header */
1466 ah->cmdstat = ATA_CMD_ID_ATA;
1469 skb->dev = t->ifp->nd;
1471 d->rttavg = RTTAVG_INIT;
1472 d->rttdev = RTTDEV_INIT;
1473 d->timer.function = rexmit_timer;
1475 skb = skb_clone(skb, GFP_ATOMIC);
1477 do_gettimeofday(&f->sent);
1478 f->sent_jiffs = (u32) jiffies;
1484 static struct aoetgt **
1485 grow_targets(struct aoedev *d)
1492 tt = kcalloc(newn, sizeof(*d->targets), GFP_ATOMIC);
1495 memmove(tt, d->targets, sizeof(*d->targets) * oldn);
1496 d->tgt = tt + (d->tgt - d->targets);
1501 return &d->targets[oldn];
1504 static struct aoetgt *
1505 addtgt(struct aoedev *d, char *addr, ulong nframes)
1507 struct aoetgt *t, **tt, **te;
1510 te = tt + d->ntargets;
1511 for (; tt < te && *tt; tt++)
1515 tt = grow_targets(d);
1519 t = kzalloc(sizeof(*t), GFP_ATOMIC);
1522 t->nframes = nframes;
1524 memcpy(t->addr, addr, sizeof t->addr);
1527 t->maxout = t->nframes / 2;
1528 INIT_LIST_HEAD(&t->ffree);
1532 pr_info("aoe: cannot allocate memory to add target\n");
1537 setdbcnt(struct aoedev *d)
1539 struct aoetgt **t, **e;
1543 e = t + d->ntargets;
1544 for (; t < e && *t; t++)
1545 if (bcnt == 0 || bcnt > (*t)->minbcnt)
1546 bcnt = (*t)->minbcnt;
1547 if (bcnt != d->maxbcnt) {
1549 pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
1550 d->aoemajor, d->aoeminor, bcnt);
1555 setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
1558 struct aoeif *p, *e;
1565 for (; p < e; p++) {
1567 break; /* end of the valid interfaces */
1569 p->bcnt = bcnt; /* we're updating */
1571 } else if (minbcnt > p->bcnt)
1572 minbcnt = p->bcnt; /* find the min interface */
1576 pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
1583 t->minbcnt = minbcnt;
1588 aoecmd_cfg_rsp(struct sk_buff *skb)
1592 struct aoe_cfghdr *ch;
1594 ulong flags, aoemajor;
1596 struct sk_buff_head queue;
1600 h = (struct aoe_hdr *) skb_mac_header(skb);
1601 ch = (struct aoe_cfghdr *) (h+1);
1604 * Enough people have their dip switches set backwards to
1605 * warrant a loud message for this special case.
1607 aoemajor = get_unaligned_be16(&h->major);
1608 if (aoemajor == 0xfff) {
1609 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
1610 "Check shelf dip switches.\n");
1613 if (aoemajor == 0xffff) {
1614 pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
1615 aoemajor, (int) h->minor);
1618 if (h->minor == 0xff) {
1619 pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
1620 aoemajor, (int) h->minor);
1624 n = be16_to_cpu(ch->bufcnt);
1625 if (n > aoe_maxout) /* keep it reasonable */
1628 d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
1630 pr_info("aoe: device allocation failure\n");
1634 spin_lock_irqsave(&d->lock, flags);
1636 t = gettgt(d, h->src);
1642 t = addtgt(d, h->src, n);
1647 n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
1651 n = n ? n * 512 : DEFAULTBCNT;
1652 setifbcnt(t, skb->dev, n);
1654 /* don't change users' perspective */
1655 if (d->nopen == 0) {
1656 d->fw_ver = be16_to_cpu(ch->fwver);
1657 sl = aoecmd_ata_id(d);
1660 spin_unlock_irqrestore(&d->lock, flags);
1663 __skb_queue_head_init(&queue);
1664 __skb_queue_tail(&queue, sl);
1665 aoenet_xmit(&queue);
1670 aoecmd_wreset(struct aoetgt *t)
1673 t->ssthresh = t->nframes / 2;
1674 t->next_cwnd = t->nframes;
1678 aoecmd_cleanslate(struct aoedev *d)
1680 struct aoetgt **t, **te;
1682 d->rttavg = RTTAVG_INIT;
1683 d->rttdev = RTTDEV_INIT;
1687 te = t + d->ntargets;
1688 for (; t < te && *t; t++)
1693 aoe_failbuf(struct aoedev *d, struct buf *buf)
1697 buf->iter.bi_size = 0;
1698 buf->bio->bi_error = -EIO;
1699 if (buf->nframesout == 0)
1700 aoe_end_buf(d, buf);
1704 aoe_flush_iocq(void)
1708 for (i = 0; i < ncpus; i++) {
1710 aoe_flush_iocq_by_index(i);
1715 aoe_flush_iocq_by_index(int id)
1720 struct list_head *pos;
1721 struct sk_buff *skb;
1724 spin_lock_irqsave(&iocq[id].lock, flags);
1725 list_splice_init(&iocq[id].head, &flist);
1726 spin_unlock_irqrestore(&iocq[id].lock, flags);
1727 while (!list_empty(&flist)) {
1730 f = list_entry(pos, struct frame, head);
1733 spin_lock_irqsave(&d->lock, flags);
1735 f->buf->nframesout--;
1736 aoe_failbuf(d, f->buf);
1739 spin_unlock_irqrestore(&d->lock, flags);
1752 /* get_zeroed_page returns page with ref count 1 */
1753 p = (void *) get_zeroed_page(GFP_KERNEL | __GFP_REPEAT);
1756 empty_page = virt_to_page(p);
1758 ncpus = num_online_cpus();
1760 iocq = kcalloc(ncpus, sizeof(struct iocq_ktio), GFP_KERNEL);
1764 kts = kcalloc(ncpus, sizeof(struct ktstate), GFP_KERNEL);
1770 ktiowq = kcalloc(ncpus, sizeof(wait_queue_head_t), GFP_KERNEL);
1776 mutex_init(&ktio_spawn_lock);
1778 for (i = 0; i < ncpus; i++) {
1779 INIT_LIST_HEAD(&iocq[i].head);
1780 spin_lock_init(&iocq[i].lock);
1781 init_waitqueue_head(&ktiowq[i]);
1782 snprintf(kts[i].name, sizeof(kts[i].name), "aoe_ktio%d", i);
1784 kts[i].waitq = &ktiowq[i];
1785 kts[i].lock = &iocq[i].lock;
1790 if (aoe_ktstart(&kts[0])) {
1811 for (i = 0; i < ncpus; i++)
1813 aoe_ktstop(&kts[i]);
1817 /* Free up the iocq and thread speicific configuration
1818 * allocated during startup.
1824 free_page((unsigned long) page_address(empty_page));