1 /* Copyright (c) 2012 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 *);
26 static struct buf *nextbuf(struct aoedev *);
28 static int aoe_deadsecs = 60 * 3;
29 module_param(aoe_deadsecs, int, 0644);
30 MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
32 static int aoe_maxout = 16;
33 module_param(aoe_maxout, int, 0644);
34 MODULE_PARM_DESC(aoe_maxout,
35 "Only aoe_maxout outstanding packets for every MAC on eX.Y.");
37 static wait_queue_head_t ktiowq;
38 static struct ktstate kts;
40 /* io completion queue */
42 struct list_head head;
46 static struct sk_buff *
51 skb = alloc_skb(len, GFP_ATOMIC);
53 skb_reset_mac_header(skb);
54 skb_reset_network_header(skb);
55 skb->protocol = __constant_htons(ETH_P_AOE);
56 skb_checksum_none_assert(skb);
62 getframe_deferred(struct aoedev *d, u32 tag)
64 struct list_head *head, *pos, *nx;
68 list_for_each_safe(pos, nx, head) {
69 f = list_entry(pos, struct frame, head);
79 getframe(struct aoedev *d, u32 tag)
82 struct list_head *head, *pos, *nx;
86 head = &d->factive[n];
87 list_for_each_safe(pos, nx, head) {
88 f = list_entry(pos, struct frame, head);
98 * Leave the top bit clear so we have tagspace for userland.
99 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
100 * This driver reserves tag -1 to mean "unused frame."
103 newtag(struct aoedev *d)
107 n = jiffies & 0xffff;
108 return n |= (++d->lasttag & 0x7fff) << 16;
112 aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
114 u32 host_tag = newtag(d);
116 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
117 memcpy(h->dst, t->addr, sizeof h->dst);
118 h->type = __constant_cpu_to_be16(ETH_P_AOE);
120 h->major = cpu_to_be16(d->aoemajor);
121 h->minor = d->aoeminor;
123 h->tag = cpu_to_be32(host_tag);
129 put_lba(struct aoe_atahdr *ah, sector_t lba)
132 ah->lba1 = lba >>= 8;
133 ah->lba2 = lba >>= 8;
134 ah->lba3 = lba >>= 8;
135 ah->lba4 = lba >>= 8;
136 ah->lba5 = lba >>= 8;
139 static struct aoeif *
140 ifrotate(struct aoetgt *t)
146 if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL)
154 skb_pool_put(struct aoedev *d, struct sk_buff *skb)
156 __skb_queue_tail(&d->skbpool, skb);
159 static struct sk_buff *
160 skb_pool_get(struct aoedev *d)
162 struct sk_buff *skb = skb_peek(&d->skbpool);
164 if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
165 __skb_unlink(skb, &d->skbpool);
168 if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
169 (skb = new_skb(ETH_ZLEN)))
176 aoe_freetframe(struct frame *f)
184 list_add(&f->head, &t->ffree);
187 static struct frame *
188 newtframe(struct aoedev *d, struct aoetgt *t)
192 struct list_head *pos;
194 if (list_empty(&t->ffree)) {
195 if (t->falloc >= NSKBPOOLMAX*2)
197 f = kcalloc(1, sizeof(*f), GFP_ATOMIC);
205 f = list_entry(pos, struct frame, head);
210 f->skb = skb = new_skb(ETH_ZLEN);
212 bail: aoe_freetframe(f);
217 if (atomic_read(&skb_shinfo(skb)->dataref) != 1) {
218 skb = skb_pool_get(d);
221 skb_pool_put(d, f->skb);
225 skb->truesize -= skb->data_len;
226 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
231 static struct frame *
232 newframe(struct aoedev *d)
235 struct aoetgt *t, **tt;
238 if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
239 printk(KERN_ERR "aoe: NULL TARGETS!\n");
242 tt = d->tgt; /* last used target */
245 if (tt >= &d->targets[NTARGETS] || !*tt)
249 if (t->nout < t->maxout
259 if (tt == d->tgt) /* we've looped and found nada */
264 d->flags |= DEVFL_KICKME;
270 skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt)
275 fcnt = bv->bv_len - (off - bv->bv_offset);
278 skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt);
288 fhash(struct frame *f)
290 struct aoedev *d = f->t->d;
293 n = f->tag % NFACTIVE;
294 list_add_tail(&f->head, &d->factive[n]);
298 aoecmd_ata_rw(struct aoedev *d)
302 struct aoe_atahdr *ah;
306 struct sk_buff_head queue;
308 char writebit, extbit;
323 if (bcnt > buf->resid)
327 f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
329 if (fbcnt < buf->bv_resid) {
330 buf->bv_resid -= fbcnt;
334 fbcnt -= buf->bv_resid;
335 buf->resid -= buf->bv_resid;
336 if (buf->resid == 0) {
341 buf->bv_resid = buf->bv->bv_len;
342 WARN_ON(buf->bv_resid == 0);
345 /* initialize the headers & frame */
347 h = (struct aoe_hdr *) skb_mac_header(skb);
348 ah = (struct aoe_atahdr *) (h+1);
349 skb_put(skb, sizeof *h + sizeof *ah);
350 memset(h, 0, skb->len);
351 f->tag = aoehdr_atainit(d, t, h);
358 f->lba = buf->sector;
360 /* set up ata header */
361 ah->scnt = bcnt >> 9;
362 put_lba(ah, buf->sector);
363 if (d->flags & DEVFL_EXT) {
364 ah->aflags |= AOEAFL_EXT;
368 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
370 if (bio_data_dir(buf->bio) == WRITE) {
371 skb_fillup(skb, f->bv, f->bv_off, bcnt);
372 ah->aflags |= AOEAFL_WRITE;
374 skb->data_len = bcnt;
375 skb->truesize += bcnt;
382 ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
384 /* mark all tracking fields and load out */
385 buf->nframesout += 1;
386 buf->sector += bcnt >> 9;
388 skb->dev = t->ifp->nd;
389 skb = skb_clone(skb, GFP_ATOMIC);
391 do_gettimeofday(&f->sent);
392 f->sent_jiffs = (u32) jiffies;
393 __skb_queue_head_init(&queue);
394 __skb_queue_tail(&queue, skb);
400 /* some callers cannot sleep, and they can call this function,
401 * transmitting the packets later, when interrupts are on
404 aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
407 struct aoe_cfghdr *ch;
409 struct net_device *ifp;
412 for_each_netdev_rcu(&init_net, ifp) {
414 if (!is_aoe_netif(ifp))
417 skb = new_skb(sizeof *h + sizeof *ch);
419 printk(KERN_INFO "aoe: skb alloc failure\n");
422 skb_put(skb, sizeof *h + sizeof *ch);
424 __skb_queue_tail(queue, skb);
425 h = (struct aoe_hdr *) skb_mac_header(skb);
426 memset(h, 0, sizeof *h + sizeof *ch);
428 memset(h->dst, 0xff, sizeof h->dst);
429 memcpy(h->src, ifp->dev_addr, sizeof h->src);
430 h->type = __constant_cpu_to_be16(ETH_P_AOE);
432 h->major = cpu_to_be16(aoemajor);
443 resend(struct aoedev *d, struct frame *f)
446 struct sk_buff_head queue;
448 struct aoe_atahdr *ah;
456 if (ifrotate(t) == NULL) {
457 /* probably can't happen, but set it up to fail anyway */
458 pr_info("aoe: resend: no interfaces to rotate to.\n");
462 h = (struct aoe_hdr *) skb_mac_header(skb);
463 ah = (struct aoe_atahdr *) (h+1);
465 snprintf(buf, sizeof buf,
466 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
467 "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
468 h->src, h->dst, t->nout);
473 h->tag = cpu_to_be32(n);
474 memcpy(h->dst, t->addr, sizeof h->dst);
475 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
477 skb->dev = t->ifp->nd;
478 skb = skb_clone(skb, GFP_ATOMIC);
481 do_gettimeofday(&f->sent);
482 f->sent_jiffs = (u32) jiffies;
483 __skb_queue_head_init(&queue);
484 __skb_queue_tail(&queue, skb);
489 tsince_hr(struct frame *f)
494 do_gettimeofday(&now);
495 n = now.tv_usec - f->sent.tv_usec;
496 n += (now.tv_sec - f->sent.tv_sec) * USEC_PER_SEC;
501 /* For relatively long periods, use jiffies to avoid
502 * discrepancies caused by updates to the system time.
504 * On system with HZ of 1000, 32-bits is over 49 days
505 * worth of jiffies, or over 71 minutes worth of usecs.
507 * Jiffies overflow is handled by subtraction of unsigned ints:
508 * (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe
512 if (n > USEC_PER_SEC / 4) {
513 n = ((u32) jiffies) - f->sent_jiffs;
514 n *= USEC_PER_SEC / HZ;
525 n = jiffies & 0xffff;
529 return jiffies_to_usecs(n + 1);
532 static struct aoeif *
533 getif(struct aoetgt *t, struct net_device *nd)
546 ejectif(struct aoetgt *t, struct aoeif *ifp)
549 struct net_device *nd;
553 e = t->ifs + NAOEIFS - 1;
554 n = (e - ifp) * sizeof *ifp;
555 memmove(ifp, ifp+1, n);
560 static struct frame *
561 reassign_frame(struct list_head *pos)
567 f = list_entry(pos, struct frame, head);
568 nf = newframe(f->t->d);
580 nf->bv_off = f->bv_off;
582 nf->waited_total = f->waited_total;
593 sthtith(struct aoedev *d)
595 struct frame *f, *nf;
596 struct list_head *nx, *pos, *head;
597 struct aoetgt *ht = d->htgt;
600 /* look through the active and pending retransmit frames */
601 for (i = 0; i < NFACTIVE; i++) {
602 head = &d->factive[i];
603 list_for_each_safe(pos, nx, head) {
604 f = list_entry(pos, struct frame, head);
607 nf = reassign_frame(pos);
614 list_for_each_safe(pos, nx, head) {
615 f = list_entry(pos, struct frame, head);
618 nf = reassign_frame(pos);
623 /* We've cleaned up the outstanding so take away his
624 * interfaces so he won't be used. We should remove him from
625 * the target array here, but cleaning up a target is
628 memset(ht->ifs, 0, sizeof ht->ifs);
634 rexmit_deferred(struct aoedev *d)
638 struct list_head *pos, *nx, *head;
642 list_for_each_safe(pos, nx, head) {
643 f = list_entry(pos, struct frame, head);
645 if (t->nout >= t->maxout)
649 since = tsince_hr(f);
651 f->waited_total += since;
657 rexmit_timer(ulong vp)
663 struct list_head *head, *pos, *nx;
665 register long timeout;
670 d = (struct aoedev *) vp;
672 spin_lock_irqsave(&d->lock, flags);
674 /* timeout based on observed timings and variations */
675 timeout = 2 * d->rttavg >> RTTSCALE;
676 timeout += 8 * d->rttdev >> RTTDSCALE;
680 if (d->flags & DEVFL_TKILL) {
681 spin_unlock_irqrestore(&d->lock, flags);
685 /* collect all frames to rexmit into flist */
686 for (i = 0; i < NFACTIVE; i++) {
687 head = &d->factive[i];
688 list_for_each_safe(pos, nx, head) {
689 f = list_entry(pos, struct frame, head);
690 if (tsince_hr(f) < timeout)
691 break; /* end of expired frames */
692 /* move to flist for later processing */
693 list_move_tail(pos, &flist);
697 /* process expired frames */
698 while (!list_empty(&flist)) {
700 f = list_entry(pos, struct frame, head);
701 since = tsince_hr(f);
702 n = f->waited_total + since;
704 if (n > aoe_deadsecs) {
705 /* Waited too long. Device failure.
706 * Hang all frames on first hash bucket for downdev
709 list_splice(&flist, &d->factive[0]);
715 if (n > aoe_deadsecs/2)
716 d->htgt = t; /* see if another target can help */
718 if (t->maxout != 1) {
719 t->ssthresh = t->maxout / 2;
723 ifp = getif(t, f->skb->dev);
724 if (ifp && ++ifp->lost > (t->nframes << 1)
725 && (ifp != t->ifs || t->ifs[1].nd)) {
729 list_move_tail(pos, &d->rexmitq);
735 if ((d->flags & DEVFL_KICKME || d->htgt) && d->blkq) {
736 d->flags &= ~DEVFL_KICKME;
737 d->blkq->request_fn(d->blkq);
740 d->timer.expires = jiffies + TIMERTICK;
741 add_timer(&d->timer);
743 spin_unlock_irqrestore(&d->lock, flags);
747 rqbiocnt(struct request *r)
752 __rq_for_each_bio(bio, r)
757 /* This can be removed if we are certain that no users of the block
758 * layer will ever use zero-count pages in bios. Otherwise we have to
759 * protect against the put_page sometimes done by the network layer.
761 * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
764 * We cannot use get_page in the workaround, because it insists on a
765 * positive page count as a precondition. So we use _count directly.
768 bio_pageinc(struct bio *bio)
774 bio_for_each_segment(bv, bio, i) {
776 /* Non-zero page count for non-head members of
777 * compound pages is no longer allowed by the kernel,
778 * but this has never been seen here.
780 if (unlikely(PageCompound(page)))
781 if (compound_trans_head(page) != page) {
782 pr_crit("page tail used for block I/O\n");
785 atomic_inc(&page->_count);
790 bio_pagedec(struct bio *bio)
795 bio_for_each_segment(bv, bio, i)
796 atomic_dec(&bv->bv_page->_count);
800 bufinit(struct buf *buf, struct request *rq, struct bio *bio)
804 memset(buf, 0, sizeof(*buf));
807 buf->resid = bio->bi_size;
808 buf->sector = bio->bi_sector;
810 buf->bv = bv = &bio->bi_io_vec[bio->bi_idx];
811 buf->bv_resid = bv->bv_len;
812 WARN_ON(buf->bv_resid == 0);
816 nextbuf(struct aoedev *d)
819 struct request_queue *q;
825 return NULL; /* initializing */
830 rq = blk_peek_request(q);
833 blk_start_request(rq);
835 d->ip.nxbio = rq->bio;
836 rq->special = (void *) rqbiocnt(rq);
838 buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
840 pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
844 bufinit(buf, rq, bio);
849 return d->ip.buf = buf;
852 /* enters with d->lock held */
854 aoecmd_work(struct aoedev *d)
856 if (d->htgt && !sthtith(d))
859 while (aoecmd_ata_rw(d))
863 /* this function performs work that has been deferred until sleeping is OK
866 aoecmd_sleepwork(struct work_struct *work)
868 struct aoedev *d = container_of(work, struct aoedev, work);
869 struct block_device *bd;
872 if (d->flags & DEVFL_GDALLOC)
875 if (d->flags & DEVFL_NEWSIZE) {
876 ssize = get_capacity(d->gd);
877 bd = bdget_disk(d->gd, 0);
879 mutex_lock(&bd->bd_inode->i_mutex);
880 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
881 mutex_unlock(&bd->bd_inode->i_mutex);
884 spin_lock_irq(&d->lock);
885 d->flags |= DEVFL_UP;
886 d->flags &= ~DEVFL_NEWSIZE;
887 spin_unlock_irq(&d->lock);
892 ata_ident_fixstring(u16 *id, int ns)
898 *id++ = s >> 8 | s << 8;
903 ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
908 /* word 83: command set supported */
909 n = get_unaligned_le16(&id[83 << 1]);
911 /* word 86: command set/feature enabled */
912 n |= get_unaligned_le16(&id[86 << 1]);
914 if (n & (1<<10)) { /* bit 10: LBA 48 */
915 d->flags |= DEVFL_EXT;
917 /* word 100: number lba48 sectors */
918 ssize = get_unaligned_le64(&id[100 << 1]);
920 /* set as in ide-disk.c:init_idedisk_capacity */
921 d->geo.cylinders = ssize;
922 d->geo.cylinders /= (255 * 63);
926 d->flags &= ~DEVFL_EXT;
928 /* number lba28 sectors */
929 ssize = get_unaligned_le32(&id[60 << 1]);
931 /* NOTE: obsolete in ATA 6 */
932 d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
933 d->geo.heads = get_unaligned_le16(&id[55 << 1]);
934 d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
937 ata_ident_fixstring((u16 *) &id[10<<1], 10); /* serial */
938 ata_ident_fixstring((u16 *) &id[23<<1], 4); /* firmware */
939 ata_ident_fixstring((u16 *) &id[27<<1], 20); /* model */
940 memcpy(d->ident, id, sizeof(d->ident));
942 if (d->ssize != ssize)
944 "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
946 d->aoemajor, d->aoeminor,
947 d->fw_ver, (long long)ssize);
950 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
953 set_capacity(d->gd, ssize);
954 d->flags |= DEVFL_NEWSIZE;
956 d->flags |= DEVFL_GDALLOC;
957 schedule_work(&d->work);
961 calc_rttavg(struct aoedev *d, struct aoetgt *t, int rtt)
967 /* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */
968 n -= d->rttavg >> RTTSCALE;
972 n -= d->rttdev >> RTTDSCALE;
975 if (!t || t->maxout >= t->nframes)
977 if (t->maxout < t->ssthresh)
979 else if (t->nout == t->maxout && t->next_cwnd-- == 0) {
981 t->next_cwnd = t->maxout;
985 static struct aoetgt *
986 gettgt(struct aoedev *d, char *addr)
988 struct aoetgt **t, **e;
992 for (; t < e && *t; t++)
993 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
999 bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
1005 fcnt = bv->bv_len - (off - bv->bv_offset);
1008 p = page_address(bv->bv_page) + off;
1009 skb_copy_bits(skb, soff, p, fcnt);
1015 off = bv->bv_offset;
1020 aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
1024 struct request_queue *q;
1031 bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
1032 } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
1034 /* cf. http://lkml.org/lkml/2006/10/31/28 */
1040 aoe_end_buf(struct aoedev *d, struct buf *buf)
1045 if (buf == d->ip.buf)
1048 bio_pagedec(buf->bio);
1049 mempool_free(buf, d->bufpool);
1050 n = (unsigned long) rq->special;
1051 rq->special = (void *) --n;
1053 aoe_end_request(d, rq, 0);
1057 ktiocomplete(struct frame *f)
1059 struct aoe_hdr *hin, *hout;
1060 struct aoe_atahdr *ahin, *ahout;
1062 struct sk_buff *skb;
1074 hout = (struct aoe_hdr *) skb_mac_header(f->skb);
1075 ahout = (struct aoe_atahdr *) (hout+1);
1079 goto noskb; /* just fail the buf. */
1081 hin = (struct aoe_hdr *) skb->data;
1082 skb_pull(skb, sizeof(*hin));
1083 ahin = (struct aoe_atahdr *) skb->data;
1084 skb_pull(skb, sizeof(*ahin));
1085 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
1086 pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
1087 ahout->cmdstat, ahin->cmdstat,
1088 d->aoemajor, d->aoeminor);
1090 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1094 n = ahout->scnt << 9;
1095 switch (ahout->cmdstat) {
1096 case ATA_CMD_PIO_READ:
1097 case ATA_CMD_PIO_READ_EXT:
1099 pr_err("aoe: runt data size in read. skb->len=%d need=%ld\n",
1101 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1104 bvcpy(f->bv, f->bv_off, skb, n);
1105 case ATA_CMD_PIO_WRITE:
1106 case ATA_CMD_PIO_WRITE_EXT:
1107 spin_lock_irq(&d->lock);
1108 ifp = getif(t, skb->dev);
1111 if (d->htgt == t) /* I'll help myself, thank you. */
1113 spin_unlock_irq(&d->lock);
1115 case ATA_CMD_ID_ATA:
1116 if (skb->len < 512) {
1117 pr_info("aoe: runt data size in ataid. skb->len=%d\n",
1121 if (skb_linearize(skb))
1123 spin_lock_irq(&d->lock);
1124 ataid_complete(d, t, skb->data);
1125 spin_unlock_irq(&d->lock);
1128 pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
1130 be16_to_cpu(get_unaligned(&hin->major)),
1134 spin_lock_irq(&d->lock);
1138 if (buf && --buf->nframesout == 0 && buf->resid == 0)
1139 aoe_end_buf(d, buf);
1143 spin_unlock_irq(&d->lock);
1148 /* Enters with iocq.lock held.
1149 * Returns true iff responses needing processing remain.
1155 struct list_head *pos;
1158 for (i = 0; ; ++i) {
1161 if (list_empty(&iocq.head))
1163 pos = iocq.head.next;
1165 spin_unlock_irq(&iocq.lock);
1166 f = list_entry(pos, struct frame, head);
1168 spin_lock_irq(&iocq.lock);
1176 DECLARE_WAITQUEUE(wait, current);
1180 current->flags |= PF_NOFREEZE;
1181 set_user_nice(current, -10);
1182 complete(&k->rendez); /* tell spawner we're running */
1184 spin_lock_irq(k->lock);
1187 add_wait_queue(k->waitq, &wait);
1188 __set_current_state(TASK_INTERRUPTIBLE);
1190 spin_unlock_irq(k->lock);
1193 remove_wait_queue(k->waitq, &wait);
1196 } while (!kthread_should_stop());
1197 complete(&k->rendez); /* tell spawner we're stopping */
1202 aoe_ktstop(struct ktstate *k)
1204 kthread_stop(k->task);
1205 wait_for_completion(&k->rendez);
1209 aoe_ktstart(struct ktstate *k)
1211 struct task_struct *task;
1213 init_completion(&k->rendez);
1214 task = kthread_run(kthread, k, k->name);
1215 if (task == NULL || IS_ERR(task))
1218 wait_for_completion(&k->rendez); /* allow kthread to start */
1219 init_completion(&k->rendez); /* for waiting for exit later */
1223 /* pass it off to kthreads for processing */
1225 ktcomplete(struct frame *f, struct sk_buff *skb)
1230 spin_lock_irqsave(&iocq.lock, flags);
1231 list_add_tail(&f->head, &iocq.head);
1232 spin_unlock_irqrestore(&iocq.lock, flags);
1237 aoecmd_ata_rsp(struct sk_buff *skb)
1247 h = (struct aoe_hdr *) skb->data;
1248 aoemajor = be16_to_cpu(get_unaligned(&h->major));
1249 d = aoedev_by_aoeaddr(aoemajor, h->minor, 0);
1251 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
1252 "for unknown device %d.%d\n",
1253 aoemajor, h->minor);
1258 spin_lock_irqsave(&d->lock, flags);
1260 n = be32_to_cpu(get_unaligned(&h->tag));
1263 calc_rttavg(d, f->t, tsince_hr(f));
1266 f = getframe_deferred(d, n);
1268 calc_rttavg(d, NULL, tsince_hr(f));
1270 calc_rttavg(d, NULL, tsince(n));
1271 spin_unlock_irqrestore(&d->lock, flags);
1273 snprintf(ebuf, sizeof(ebuf),
1274 "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n",
1276 get_unaligned_be16(&h->major),
1278 get_unaligned_be32(&h->tag),
1288 spin_unlock_irqrestore(&d->lock, flags);
1293 * Note here that we do not perform an aoedev_put, as we are
1294 * leaving this reference for the ktio to release.
1300 aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
1302 struct sk_buff_head queue;
1304 __skb_queue_head_init(&queue);
1305 aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
1306 aoenet_xmit(&queue);
1310 aoecmd_ata_id(struct aoedev *d)
1313 struct aoe_atahdr *ah;
1315 struct sk_buff *skb;
1324 /* initialize the headers & frame */
1326 h = (struct aoe_hdr *) skb_mac_header(skb);
1327 ah = (struct aoe_atahdr *) (h+1);
1328 skb_put(skb, sizeof *h + sizeof *ah);
1329 memset(h, 0, skb->len);
1330 f->tag = aoehdr_atainit(d, t, h);
1334 f->waited_total = 0;
1336 /* set up ata header */
1338 ah->cmdstat = ATA_CMD_ID_ATA;
1341 skb->dev = t->ifp->nd;
1343 d->rttavg = RTTAVG_INIT;
1344 d->rttdev = RTTDEV_INIT;
1345 d->timer.function = rexmit_timer;
1347 skb = skb_clone(skb, GFP_ATOMIC);
1349 do_gettimeofday(&f->sent);
1350 f->sent_jiffs = (u32) jiffies;
1356 static struct aoetgt *
1357 addtgt(struct aoedev *d, char *addr, ulong nframes)
1359 struct aoetgt *t, **tt, **te;
1363 for (; tt < te && *tt; tt++)
1368 "aoe: device addtgt failure; too many targets\n");
1371 t = kzalloc(sizeof(*t), GFP_ATOMIC);
1373 printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
1378 t->nframes = nframes;
1380 memcpy(t->addr, addr, sizeof t->addr);
1383 INIT_LIST_HEAD(&t->ffree);
1388 setdbcnt(struct aoedev *d)
1390 struct aoetgt **t, **e;
1395 for (; t < e && *t; t++)
1396 if (bcnt == 0 || bcnt > (*t)->minbcnt)
1397 bcnt = (*t)->minbcnt;
1398 if (bcnt != d->maxbcnt) {
1400 pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
1401 d->aoemajor, d->aoeminor, bcnt);
1406 setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
1409 struct aoeif *p, *e;
1416 for (; p < e; p++) {
1418 break; /* end of the valid interfaces */
1420 p->bcnt = bcnt; /* we're updating */
1422 } else if (minbcnt > p->bcnt)
1423 minbcnt = p->bcnt; /* find the min interface */
1427 pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
1434 t->minbcnt = minbcnt;
1439 aoecmd_cfg_rsp(struct sk_buff *skb)
1443 struct aoe_cfghdr *ch;
1445 ulong flags, aoemajor;
1447 struct sk_buff_head queue;
1451 h = (struct aoe_hdr *) skb_mac_header(skb);
1452 ch = (struct aoe_cfghdr *) (h+1);
1455 * Enough people have their dip switches set backwards to
1456 * warrant a loud message for this special case.
1458 aoemajor = get_unaligned_be16(&h->major);
1459 if (aoemajor == 0xfff) {
1460 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
1461 "Check shelf dip switches.\n");
1464 if (aoemajor == 0xffff) {
1465 pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
1466 aoemajor, (int) h->minor);
1469 if (h->minor == 0xff) {
1470 pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
1471 aoemajor, (int) h->minor);
1475 n = be16_to_cpu(ch->bufcnt);
1476 if (n > aoe_maxout) /* keep it reasonable */
1479 d = aoedev_by_aoeaddr(aoemajor, h->minor, 1);
1481 pr_info("aoe: device allocation failure\n");
1485 spin_lock_irqsave(&d->lock, flags);
1487 t = gettgt(d, h->src);
1493 t = addtgt(d, h->src, n);
1498 n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
1502 n = n ? n * 512 : DEFAULTBCNT;
1503 setifbcnt(t, skb->dev, n);
1505 /* don't change users' perspective */
1506 if (d->nopen == 0) {
1507 d->fw_ver = be16_to_cpu(ch->fwver);
1508 sl = aoecmd_ata_id(d);
1511 spin_unlock_irqrestore(&d->lock, flags);
1514 __skb_queue_head_init(&queue);
1515 __skb_queue_tail(&queue, sl);
1516 aoenet_xmit(&queue);
1521 aoecmd_wreset(struct aoetgt *t)
1524 t->ssthresh = t->nframes / 2;
1525 t->next_cwnd = t->nframes;
1529 aoecmd_cleanslate(struct aoedev *d)
1531 struct aoetgt **t, **te;
1533 d->rttavg = RTTAVG_INIT;
1534 d->rttdev = RTTDEV_INIT;
1539 for (; t < te && *t; t++)
1544 aoe_failbuf(struct aoedev *d, struct buf *buf)
1549 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1550 if (buf->nframesout == 0)
1551 aoe_end_buf(d, buf);
1555 aoe_flush_iocq(void)
1560 struct list_head *pos;
1561 struct sk_buff *skb;
1564 spin_lock_irqsave(&iocq.lock, flags);
1565 list_splice_init(&iocq.head, &flist);
1566 spin_unlock_irqrestore(&iocq.lock, flags);
1567 while (!list_empty(&flist)) {
1570 f = list_entry(pos, struct frame, head);
1573 spin_lock_irqsave(&d->lock, flags);
1575 f->buf->nframesout--;
1576 aoe_failbuf(d, f->buf);
1579 spin_unlock_irqrestore(&d->lock, flags);
1588 INIT_LIST_HEAD(&iocq.head);
1589 spin_lock_init(&iocq.lock);
1590 init_waitqueue_head(&ktiowq);
1591 kts.name = "aoe_ktio";
1593 kts.waitq = &ktiowq;
1594 kts.lock = &iocq.lock;
1595 return aoe_ktstart(&kts);