1 /* Copyright (c) 2007 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(struct aoetgt *t, u32 tag)
65 struct list_head *head, *pos, *nx;
69 head = &t->factive[n];
70 list_for_each_safe(pos, nx, head) {
71 f = list_entry(pos, struct frame, head);
81 * Leave the top bit clear so we have tagspace for userland.
82 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
83 * This driver reserves tag -1 to mean "unused frame."
86 newtag(struct aoetgt *t)
91 return n |= (++t->lasttag & 0x7fff) << 16;
95 aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
97 u32 host_tag = newtag(t);
99 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
100 memcpy(h->dst, t->addr, sizeof h->dst);
101 h->type = __constant_cpu_to_be16(ETH_P_AOE);
103 h->major = cpu_to_be16(d->aoemajor);
104 h->minor = d->aoeminor;
106 h->tag = cpu_to_be32(host_tag);
112 put_lba(struct aoe_atahdr *ah, sector_t lba)
115 ah->lba1 = lba >>= 8;
116 ah->lba2 = lba >>= 8;
117 ah->lba3 = lba >>= 8;
118 ah->lba4 = lba >>= 8;
119 ah->lba5 = lba >>= 8;
122 static struct aoeif *
123 ifrotate(struct aoetgt *t)
129 if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL)
137 skb_pool_put(struct aoedev *d, struct sk_buff *skb)
139 __skb_queue_tail(&d->skbpool, skb);
142 static struct sk_buff *
143 skb_pool_get(struct aoedev *d)
145 struct sk_buff *skb = skb_peek(&d->skbpool);
147 if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
148 __skb_unlink(skb, &d->skbpool);
151 if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
152 (skb = new_skb(ETH_ZLEN)))
159 aoe_freetframe(struct frame *f)
167 list_add(&f->head, &t->ffree);
170 static struct frame *
171 newtframe(struct aoedev *d, struct aoetgt *t)
175 struct list_head *pos;
177 if (list_empty(&t->ffree)) {
178 if (t->falloc >= NSKBPOOLMAX*2)
180 f = kcalloc(1, sizeof(*f), GFP_ATOMIC);
188 f = list_entry(pos, struct frame, head);
193 f->skb = skb = new_skb(ETH_ZLEN);
195 bail: aoe_freetframe(f);
200 if (atomic_read(&skb_shinfo(skb)->dataref) != 1) {
201 skb = skb_pool_get(d);
204 skb_pool_put(d, f->skb);
208 skb->truesize -= skb->data_len;
209 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
214 static struct frame *
215 newframe(struct aoedev *d)
218 struct aoetgt *t, **tt;
221 if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
222 printk(KERN_ERR "aoe: NULL TARGETS!\n");
225 tt = d->tgt; /* last used target */
228 if (tt >= &d->targets[NTARGETS] || !*tt)
232 if (t->nout < t->maxout
242 if (tt == d->tgt) /* we've looped and found nada */
247 d->flags |= DEVFL_KICKME;
253 skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt)
258 fcnt = bv->bv_len - (off - bv->bv_offset);
261 skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt);
271 fhash(struct frame *f)
273 struct aoetgt *t = f->t;
276 n = f->tag % NFACTIVE;
277 list_add_tail(&f->head, &t->factive[n]);
281 aoecmd_ata_rw(struct aoedev *d)
285 struct aoe_atahdr *ah;
290 struct sk_buff_head queue;
292 char writebit, extbit;
308 if (bcnt > buf->resid)
312 f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
314 if (fbcnt < buf->bv_resid) {
315 buf->bv_resid -= fbcnt;
319 fbcnt -= buf->bv_resid;
320 buf->resid -= buf->bv_resid;
321 if (buf->resid == 0) {
326 buf->bv_resid = buf->bv->bv_len;
327 WARN_ON(buf->bv_resid == 0);
330 /* initialize the headers & frame */
332 h = (struct aoe_hdr *) skb_mac_header(skb);
333 ah = (struct aoe_atahdr *) (h+1);
334 skb_put(skb, sizeof *h + sizeof *ah);
335 memset(h, 0, skb->len);
336 f->tag = aoehdr_atainit(d, t, h);
342 f->lba = buf->sector;
344 /* set up ata header */
345 ah->scnt = bcnt >> 9;
346 put_lba(ah, buf->sector);
347 if (d->flags & DEVFL_EXT) {
348 ah->aflags |= AOEAFL_EXT;
352 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
354 if (bio_data_dir(buf->bio) == WRITE) {
355 skb_fillup(skb, f->bv, f->bv_off, bcnt);
356 ah->aflags |= AOEAFL_WRITE;
358 skb->data_len = bcnt;
359 skb->truesize += bcnt;
366 ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
368 /* mark all tracking fields and load out */
369 buf->nframesout += 1;
370 buf->sector += bcnt >> 9;
372 skb->dev = t->ifp->nd;
373 skb = skb_clone(skb, GFP_ATOMIC);
375 __skb_queue_head_init(&queue);
376 __skb_queue_tail(&queue, skb);
382 /* some callers cannot sleep, and they can call this function,
383 * transmitting the packets later, when interrupts are on
386 aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
389 struct aoe_cfghdr *ch;
391 struct net_device *ifp;
394 for_each_netdev_rcu(&init_net, ifp) {
396 if (!is_aoe_netif(ifp))
399 skb = new_skb(sizeof *h + sizeof *ch);
401 printk(KERN_INFO "aoe: skb alloc failure\n");
404 skb_put(skb, sizeof *h + sizeof *ch);
406 __skb_queue_tail(queue, skb);
407 h = (struct aoe_hdr *) skb_mac_header(skb);
408 memset(h, 0, sizeof *h + sizeof *ch);
410 memset(h->dst, 0xff, sizeof h->dst);
411 memcpy(h->src, ifp->dev_addr, sizeof h->src);
412 h->type = __constant_cpu_to_be16(ETH_P_AOE);
414 h->major = cpu_to_be16(aoemajor);
425 resend(struct aoedev *d, struct frame *f)
428 struct sk_buff_head queue;
430 struct aoe_atahdr *ah;
438 if (ifrotate(t) == NULL) {
439 /* probably can't happen, but set it up to fail anyway */
440 pr_info("aoe: resend: no interfaces to rotate to.\n");
444 h = (struct aoe_hdr *) skb_mac_header(skb);
445 ah = (struct aoe_atahdr *) (h+1);
447 snprintf(buf, sizeof buf,
448 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
449 "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
450 h->src, h->dst, t->nout);
455 h->tag = cpu_to_be32(n);
456 memcpy(h->dst, t->addr, sizeof h->dst);
457 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
459 skb->dev = t->ifp->nd;
460 skb = skb_clone(skb, GFP_ATOMIC);
463 __skb_queue_head_init(&queue);
464 __skb_queue_tail(&queue, skb);
473 n = jiffies & 0xffff;
480 static struct aoeif *
481 getif(struct aoetgt *t, struct net_device *nd)
494 ejectif(struct aoetgt *t, struct aoeif *ifp)
499 e = t->ifs + NAOEIFS - 1;
500 n = (e - ifp) * sizeof *ifp;
501 memmove(ifp, ifp+1, n);
506 sthtith(struct aoedev *d)
508 struct frame *f, *nf;
509 struct list_head *nx, *pos, *head;
511 struct aoetgt *ht = d->htgt;
514 for (i = 0; i < NFACTIVE; i++) {
515 head = &ht->factive[i];
516 list_for_each_safe(pos, nx, head) {
517 f = list_entry(pos, struct frame, head);
522 /* remove frame from active list */
525 /* reassign all pertinent bits to new outbound frame */
532 nf->bv_off = f->bv_off;
541 /* We've cleaned up the outstanding so take away his
542 * interfaces so he won't be used. We should remove him from
543 * the target array here, but cleaning up a target is
546 memset(ht->ifs, 0, sizeof ht->ifs);
551 static inline unsigned char
552 ata_scnt(unsigned char *packet) {
554 struct aoe_atahdr *ah;
556 h = (struct aoe_hdr *) packet;
557 ah = (struct aoe_atahdr *) (h+1);
562 rexmit_timer(ulong vp)
565 struct aoetgt *t, **tt, **te;
568 struct list_head *head, *pos, *nx;
570 register long timeout;
574 d = (struct aoedev *) vp;
576 /* timeout is always ~150% of the moving average */
578 timeout += timeout >> 1;
580 spin_lock_irqsave(&d->lock, flags);
582 if (d->flags & DEVFL_TKILL) {
583 spin_unlock_irqrestore(&d->lock, flags);
587 /* collect all frames to rexmit into flist */
590 for (; tt < te && *tt; tt++) {
592 for (i = 0; i < NFACTIVE; i++) {
593 head = &t->factive[i];
594 list_for_each_safe(pos, nx, head) {
595 f = list_entry(pos, struct frame, head);
596 if (tsince(f->tag) < timeout)
598 /* move to flist for later processing */
599 list_move_tail(pos, &flist);
604 if (t->nout == t->maxout
605 && t->maxout < t->nframes
606 && (jiffies - t->lastwadj)/HZ > 10) {
608 t->lastwadj = jiffies;
612 if (!list_empty(&flist)) { /* retransmissions necessary */
615 d->rttavg = MAXTIMER;
618 /* process expired frames */
619 while (!list_empty(&flist)) {
621 f = list_entry(pos, struct frame, head);
622 n = f->waited += timeout;
624 if (n > aoe_deadsecs) {
625 /* Waited too long. Device failure.
626 * Hang all frames on first hash bucket for downdev
629 list_splice(&flist, &f->t->factive[0]);
637 /* see if another target can help */
641 if (t->nout == t->maxout) {
644 t->lastwadj = jiffies;
647 ifp = getif(t, f->skb->dev);
648 if (ifp && ++ifp->lost > (t->nframes << 1)
649 && (ifp != t->ifs || t->ifs[1].nd)) {
656 if ((d->flags & DEVFL_KICKME || d->htgt) && d->blkq) {
657 d->flags &= ~DEVFL_KICKME;
658 d->blkq->request_fn(d->blkq);
661 d->timer.expires = jiffies + TIMERTICK;
662 add_timer(&d->timer);
664 spin_unlock_irqrestore(&d->lock, flags);
668 rqbiocnt(struct request *r)
673 __rq_for_each_bio(bio, r)
678 /* This can be removed if we are certain that no users of the block
679 * layer will ever use zero-count pages in bios. Otherwise we have to
680 * protect against the put_page sometimes done by the network layer.
682 * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
685 * We cannot use get_page in the workaround, because it insists on a
686 * positive page count as a precondition. So we use _count directly.
689 bio_pageinc(struct bio *bio)
695 bio_for_each_segment(bv, bio, i) {
697 /* Non-zero page count for non-head members of
698 * compound pages is no longer allowed by the kernel,
699 * but this has never been seen here.
701 if (unlikely(PageCompound(page)))
702 if (compound_trans_head(page) != page) {
703 pr_crit("page tail used for block I/O\n");
706 atomic_inc(&page->_count);
711 bio_pagedec(struct bio *bio)
716 bio_for_each_segment(bv, bio, i)
717 atomic_dec(&bv->bv_page->_count);
721 bufinit(struct buf *buf, struct request *rq, struct bio *bio)
725 memset(buf, 0, sizeof(*buf));
728 buf->resid = bio->bi_size;
729 buf->sector = bio->bi_sector;
731 buf->bv = bv = &bio->bi_io_vec[bio->bi_idx];
732 buf->bv_resid = bv->bv_len;
733 WARN_ON(buf->bv_resid == 0);
737 nextbuf(struct aoedev *d)
740 struct request_queue *q;
746 return NULL; /* initializing */
751 rq = blk_peek_request(q);
754 blk_start_request(rq);
756 d->ip.nxbio = rq->bio;
757 rq->special = (void *) rqbiocnt(rq);
759 buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
761 pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
765 bufinit(buf, rq, bio);
770 return d->ip.buf = buf;
773 /* enters with d->lock held */
775 aoecmd_work(struct aoedev *d)
777 if (d->htgt && !sthtith(d))
779 while (aoecmd_ata_rw(d))
783 /* this function performs work that has been deferred until sleeping is OK
786 aoecmd_sleepwork(struct work_struct *work)
788 struct aoedev *d = container_of(work, struct aoedev, work);
790 if (d->flags & DEVFL_GDALLOC)
793 if (d->flags & DEVFL_NEWSIZE) {
794 struct block_device *bd;
798 ssize = get_capacity(d->gd);
799 bd = bdget_disk(d->gd, 0);
802 mutex_lock(&bd->bd_inode->i_mutex);
803 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
804 mutex_unlock(&bd->bd_inode->i_mutex);
807 spin_lock_irqsave(&d->lock, flags);
808 d->flags |= DEVFL_UP;
809 d->flags &= ~DEVFL_NEWSIZE;
810 spin_unlock_irqrestore(&d->lock, flags);
815 ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
820 /* word 83: command set supported */
821 n = get_unaligned_le16(&id[83 << 1]);
823 /* word 86: command set/feature enabled */
824 n |= get_unaligned_le16(&id[86 << 1]);
826 if (n & (1<<10)) { /* bit 10: LBA 48 */
827 d->flags |= DEVFL_EXT;
829 /* word 100: number lba48 sectors */
830 ssize = get_unaligned_le64(&id[100 << 1]);
832 /* set as in ide-disk.c:init_idedisk_capacity */
833 d->geo.cylinders = ssize;
834 d->geo.cylinders /= (255 * 63);
838 d->flags &= ~DEVFL_EXT;
840 /* number lba28 sectors */
841 ssize = get_unaligned_le32(&id[60 << 1]);
843 /* NOTE: obsolete in ATA 6 */
844 d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
845 d->geo.heads = get_unaligned_le16(&id[55 << 1]);
846 d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
849 if (d->ssize != ssize)
851 "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
853 d->aoemajor, d->aoeminor,
854 d->fw_ver, (long long)ssize);
857 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
860 set_capacity(d->gd, ssize);
861 d->flags |= DEVFL_NEWSIZE;
863 d->flags |= DEVFL_GDALLOC;
864 schedule_work(&d->work);
868 calc_rttavg(struct aoedev *d, int rtt)
877 else if (n > MAXTIMER)
879 d->mintimer += (n - d->mintimer) >> 1;
880 } else if (n < d->mintimer)
882 else if (n > MAXTIMER)
885 /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
890 static struct aoetgt *
891 gettgt(struct aoedev *d, char *addr)
893 struct aoetgt **t, **e;
897 for (; t < e && *t; t++)
898 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
904 bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
910 fcnt = bv->bv_len - (off - bv->bv_offset);
913 p = page_address(bv->bv_page) + off;
914 skb_copy_bits(skb, soff, p, fcnt);
925 aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
929 struct request_queue *q;
936 bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
937 } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
939 /* cf. http://lkml.org/lkml/2006/10/31/28 */
945 aoe_end_buf(struct aoedev *d, struct buf *buf)
950 if (buf == d->ip.buf)
953 bio_pagedec(buf->bio);
954 mempool_free(buf, d->bufpool);
955 n = (unsigned long) rq->special;
956 rq->special = (void *) --n;
958 aoe_end_request(d, rq, 0);
962 ktiocomplete(struct frame *f)
964 struct aoe_hdr *hin, *hout;
965 struct aoe_atahdr *ahin, *ahout;
979 hout = (struct aoe_hdr *) skb_mac_header(f->skb);
980 ahout = (struct aoe_atahdr *) (hout+1);
984 goto noskb; /* just fail the buf. */
986 hin = (struct aoe_hdr *) skb->data;
987 skb_pull(skb, sizeof(*hin));
988 ahin = (struct aoe_atahdr *) skb->data;
989 skb_pull(skb, sizeof(*ahin));
990 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
991 pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
992 ahout->cmdstat, ahin->cmdstat,
993 d->aoemajor, d->aoeminor);
995 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
999 n = ahout->scnt << 9;
1000 switch (ahout->cmdstat) {
1001 case ATA_CMD_PIO_READ:
1002 case ATA_CMD_PIO_READ_EXT:
1004 pr_err("aoe: runt data size in read. skb->len=%d need=%ld\n",
1006 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1009 bvcpy(f->bv, f->bv_off, skb, n);
1010 case ATA_CMD_PIO_WRITE:
1011 case ATA_CMD_PIO_WRITE_EXT:
1012 spin_lock_irq(&d->lock);
1013 ifp = getif(t, skb->dev);
1016 if (d->htgt == t) /* I'll help myself, thank you. */
1018 spin_unlock_irq(&d->lock);
1020 case ATA_CMD_ID_ATA:
1021 if (skb->len < 512) {
1022 pr_info("aoe: runt data size in ataid. skb->len=%d\n",
1026 if (skb_linearize(skb))
1028 spin_lock_irq(&d->lock);
1029 ataid_complete(d, t, skb->data);
1030 spin_unlock_irq(&d->lock);
1033 pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
1035 be16_to_cpu(get_unaligned(&hin->major)),
1039 spin_lock_irq(&d->lock);
1043 if (buf && --buf->nframesout == 0 && buf->resid == 0)
1044 aoe_end_buf(d, buf);
1048 spin_unlock_irq(&d->lock);
1053 /* Enters with iocq.lock held.
1054 * Returns true iff responses needing processing remain.
1060 struct list_head *pos;
1063 for (i = 0; ; ++i) {
1066 if (list_empty(&iocq.head))
1068 pos = iocq.head.next;
1070 spin_unlock_irq(&iocq.lock);
1071 f = list_entry(pos, struct frame, head);
1073 spin_lock_irq(&iocq.lock);
1081 DECLARE_WAITQUEUE(wait, current);
1085 current->flags |= PF_NOFREEZE;
1086 set_user_nice(current, -10);
1087 complete(&k->rendez); /* tell spawner we're running */
1089 spin_lock_irq(k->lock);
1092 add_wait_queue(k->waitq, &wait);
1093 __set_current_state(TASK_INTERRUPTIBLE);
1095 spin_unlock_irq(k->lock);
1098 remove_wait_queue(k->waitq, &wait);
1101 } while (!kthread_should_stop());
1102 complete(&k->rendez); /* tell spawner we're stopping */
1107 aoe_ktstop(struct ktstate *k)
1109 kthread_stop(k->task);
1110 wait_for_completion(&k->rendez);
1114 aoe_ktstart(struct ktstate *k)
1116 struct task_struct *task;
1118 init_completion(&k->rendez);
1119 task = kthread_run(kthread, k, k->name);
1120 if (task == NULL || IS_ERR(task))
1123 wait_for_completion(&k->rendez); /* allow kthread to start */
1124 init_completion(&k->rendez); /* for waiting for exit later */
1128 /* pass it off to kthreads for processing */
1130 ktcomplete(struct frame *f, struct sk_buff *skb)
1135 spin_lock_irqsave(&iocq.lock, flags);
1136 list_add_tail(&f->head, &iocq.head);
1137 spin_unlock_irqrestore(&iocq.lock, flags);
1142 aoecmd_ata_rsp(struct sk_buff *skb)
1153 h = (struct aoe_hdr *) skb->data;
1154 aoemajor = be16_to_cpu(get_unaligned(&h->major));
1155 d = aoedev_by_aoeaddr(aoemajor, h->minor);
1157 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
1158 "for unknown device %d.%d\n",
1159 aoemajor, h->minor);
1164 spin_lock_irqsave(&d->lock, flags);
1166 n = be32_to_cpu(get_unaligned(&h->tag));
1167 t = gettgt(d, h->src);
1169 printk(KERN_INFO "aoe: can't find target e%ld.%d:%pm\n",
1170 d->aoemajor, d->aoeminor, h->src);
1171 spin_unlock_irqrestore(&d->lock, flags);
1177 calc_rttavg(d, -tsince(n));
1178 spin_unlock_irqrestore(&d->lock, flags);
1180 snprintf(ebuf, sizeof ebuf,
1181 "%15s e%d.%d tag=%08x@%08lx\n",
1183 get_unaligned_be16(&h->major),
1185 get_unaligned_be32(&h->tag),
1190 calc_rttavg(d, tsince(f->tag));
1194 spin_unlock_irqrestore(&d->lock, flags);
1199 * Note here that we do not perform an aoedev_put, as we are
1200 * leaving this reference for the ktio to release.
1206 aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
1208 struct sk_buff_head queue;
1210 __skb_queue_head_init(&queue);
1211 aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
1212 aoenet_xmit(&queue);
1216 aoecmd_ata_id(struct aoedev *d)
1219 struct aoe_atahdr *ah;
1221 struct sk_buff *skb;
1230 /* initialize the headers & frame */
1232 h = (struct aoe_hdr *) skb_mac_header(skb);
1233 ah = (struct aoe_atahdr *) (h+1);
1234 skb_put(skb, sizeof *h + sizeof *ah);
1235 memset(h, 0, skb->len);
1236 f->tag = aoehdr_atainit(d, t, h);
1241 /* set up ata header */
1243 ah->cmdstat = ATA_CMD_ID_ATA;
1246 skb->dev = t->ifp->nd;
1248 d->rttavg = MAXTIMER;
1249 d->timer.function = rexmit_timer;
1251 return skb_clone(skb, GFP_ATOMIC);
1254 static struct aoetgt *
1255 addtgt(struct aoedev *d, char *addr, ulong nframes)
1257 struct aoetgt *t, **tt, **te;
1262 for (; tt < te && *tt; tt++)
1267 "aoe: device addtgt failure; too many targets\n");
1270 t = kzalloc(sizeof(*t), GFP_ATOMIC);
1272 printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
1277 t->nframes = nframes;
1279 memcpy(t->addr, addr, sizeof t->addr);
1281 t->maxout = t->nframes;
1282 INIT_LIST_HEAD(&t->ffree);
1283 for (i = 0; i < NFACTIVE; ++i)
1284 INIT_LIST_HEAD(&t->factive[i]);
1289 setdbcnt(struct aoedev *d)
1291 struct aoetgt **t, **e;
1296 for (; t < e && *t; t++)
1297 if (bcnt == 0 || bcnt > (*t)->minbcnt)
1298 bcnt = (*t)->minbcnt;
1299 if (bcnt != d->maxbcnt) {
1301 pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
1302 d->aoemajor, d->aoeminor, bcnt);
1307 setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
1310 struct aoeif *p, *e;
1317 for (; p < e; p++) {
1319 break; /* end of the valid interfaces */
1321 p->bcnt = bcnt; /* we're updating */
1323 } else if (minbcnt > p->bcnt)
1324 minbcnt = p->bcnt; /* find the min interface */
1328 pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
1334 t->minbcnt = minbcnt;
1339 aoecmd_cfg_rsp(struct sk_buff *skb)
1343 struct aoe_cfghdr *ch;
1345 ulong flags, sysminor, aoemajor;
1347 struct sk_buff_head queue;
1351 h = (struct aoe_hdr *) skb_mac_header(skb);
1352 ch = (struct aoe_cfghdr *) (h+1);
1355 * Enough people have their dip switches set backwards to
1356 * warrant a loud message for this special case.
1358 aoemajor = get_unaligned_be16(&h->major);
1359 if (aoemajor == 0xfff) {
1360 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
1361 "Check shelf dip switches.\n");
1365 sysminor = SYSMINOR(aoemajor, h->minor);
1366 if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
1367 printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n",
1368 aoemajor, (int) h->minor);
1372 n = be16_to_cpu(ch->bufcnt);
1373 if (n > aoe_maxout) /* keep it reasonable */
1376 d = aoedev_by_sysminor_m(sysminor);
1378 printk(KERN_INFO "aoe: device sysminor_m failure\n");
1382 spin_lock_irqsave(&d->lock, flags);
1384 t = gettgt(d, h->src);
1386 t = addtgt(d, h->src, n);
1391 n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
1395 n = n ? n * 512 : DEFAULTBCNT;
1396 setifbcnt(t, skb->dev, n);
1398 /* don't change users' perspective */
1399 if (d->nopen == 0) {
1400 d->fw_ver = be16_to_cpu(ch->fwver);
1401 sl = aoecmd_ata_id(d);
1404 spin_unlock_irqrestore(&d->lock, flags);
1407 __skb_queue_head_init(&queue);
1408 __skb_queue_tail(&queue, sl);
1409 aoenet_xmit(&queue);
1414 aoecmd_cleanslate(struct aoedev *d)
1416 struct aoetgt **t, **te;
1418 d->mintimer = MINTIMER;
1423 for (; t < te && *t; t++)
1424 (*t)->maxout = (*t)->nframes;
1428 aoe_failbuf(struct aoedev *d, struct buf *buf)
1433 clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
1434 if (buf->nframesout == 0)
1435 aoe_end_buf(d, buf);
1439 aoe_flush_iocq(void)
1444 struct list_head *pos;
1445 struct sk_buff *skb;
1448 spin_lock_irqsave(&iocq.lock, flags);
1449 list_splice_init(&iocq.head, &flist);
1450 spin_unlock_irqrestore(&iocq.lock, flags);
1451 while (!list_empty(&flist)) {
1454 f = list_entry(pos, struct frame, head);
1457 spin_lock_irqsave(&d->lock, flags);
1459 f->buf->nframesout--;
1460 aoe_failbuf(d, f->buf);
1463 spin_unlock_irqrestore(&d->lock, flags);
1472 INIT_LIST_HEAD(&iocq.head);
1473 spin_lock_init(&iocq.lock);
1474 init_waitqueue_head(&ktiowq);
1475 kts.name = "aoe_ktio";
1477 kts.waitq = &ktiowq;
1478 kts.lock = &iocq.lock;
1479 return aoe_ktstart(&kts);