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 <net/net_namespace.h>
16 #include <asm/unaligned.h>
19 static int aoe_deadsecs = 60 * 3;
20 module_param(aoe_deadsecs, int, 0644);
21 MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
23 static int aoe_maxout = 16;
24 module_param(aoe_maxout, int, 0644);
25 MODULE_PARM_DESC(aoe_maxout,
26 "Only aoe_maxout outstanding packets for every MAC on eX.Y.");
28 static struct sk_buff *
33 skb = alloc_skb(len, GFP_ATOMIC);
35 skb_reset_mac_header(skb);
36 skb_reset_network_header(skb);
37 skb->protocol = __constant_htons(ETH_P_AOE);
38 skb_checksum_none_assert(skb);
44 getframe(struct aoetgt *t, int tag)
57 * Leave the top bit clear so we have tagspace for userland.
58 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
59 * This driver reserves tag -1 to mean "unused frame."
62 newtag(struct aoetgt *t)
67 return n |= (++t->lasttag & 0x7fff) << 16;
71 aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
73 u32 host_tag = newtag(t);
75 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
76 memcpy(h->dst, t->addr, sizeof h->dst);
77 h->type = __constant_cpu_to_be16(ETH_P_AOE);
79 h->major = cpu_to_be16(d->aoemajor);
80 h->minor = d->aoeminor;
82 h->tag = cpu_to_be32(host_tag);
88 put_lba(struct aoe_atahdr *ah, sector_t lba)
99 ifrotate(struct aoetgt *t)
102 if (t->ifp >= &t->ifs[NAOEIFS] || t->ifp->nd == NULL)
104 if (t->ifp->nd == NULL) {
105 printk(KERN_INFO "aoe: no interface to rotate to\n");
111 skb_pool_put(struct aoedev *d, struct sk_buff *skb)
113 __skb_queue_tail(&d->skbpool, skb);
116 static struct sk_buff *
117 skb_pool_get(struct aoedev *d)
119 struct sk_buff *skb = skb_peek(&d->skbpool);
121 if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
122 __skb_unlink(skb, &d->skbpool);
125 if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
126 (skb = new_skb(ETH_ZLEN)))
132 /* freeframe is where we do our load balancing so it's a little hairy. */
133 static struct frame *
134 freeframe(struct aoedev *d)
136 struct frame *f, *e, *rf;
140 if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
141 printk(KERN_ERR "aoe: NULL TARGETS!\n");
146 if (t >= &d->targets[NTARGETS] || !*t)
149 if ((*t)->nout < (*t)->maxout
154 e = f + (*t)->nframes;
156 if (f->tag != FREETAG)
160 && !(f->skb = skb = new_skb(ETH_ZLEN)))
162 if (atomic_read(&skb_shinfo(skb)->dataref)
168 gotone: skb_shinfo(skb)->nr_frags = skb->data_len = 0;
174 /* Work can be done, but the network layer is
175 holding our precious packets. Try to grab
176 one from the pool. */
178 if (f == NULL) { /* more paranoia */
180 "aoe: freeframe: %s.\n",
181 "unexpected null rf");
182 d->flags |= DEVFL_KICKME;
185 skb = skb_pool_get(d);
187 skb_pool_put(d, f->skb);
193 d->flags |= DEVFL_KICKME;
195 if (t == d->tgt) /* we've looped and found nada */
198 if (t >= &d->targets[NTARGETS] || !*t)
205 aoecmd_ata_rw(struct aoedev *d)
209 struct aoe_atahdr *ah;
215 char writebit, extbit;
226 bcnt = t->ifp->maxbcnt;
229 if (bcnt > buf->bv_resid)
230 bcnt = buf->bv_resid;
231 /* initialize the headers & frame */
233 h = (struct aoe_hdr *) skb_mac_header(skb);
234 ah = (struct aoe_atahdr *) (h+1);
235 skb_put(skb, sizeof *h + sizeof *ah);
236 memset(h, 0, skb->len);
237 f->tag = aoehdr_atainit(d, t, h);
241 f->bufaddr = page_address(bv->bv_page) + buf->bv_off;
243 f->lba = buf->sector;
245 /* set up ata header */
246 ah->scnt = bcnt >> 9;
247 put_lba(ah, buf->sector);
248 if (d->flags & DEVFL_EXT) {
249 ah->aflags |= AOEAFL_EXT;
253 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
255 if (bio_data_dir(buf->bio) == WRITE) {
256 skb_fill_page_desc(skb, 0, bv->bv_page, buf->bv_off, bcnt);
257 ah->aflags |= AOEAFL_WRITE;
259 skb->data_len = bcnt;
266 ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
268 /* mark all tracking fields and load out */
269 buf->nframesout += 1;
271 buf->bv_resid -= bcnt;
273 buf->sector += bcnt >> 9;
274 if (buf->resid == 0) {
276 } else if (buf->bv_resid == 0) {
278 buf->bv_resid = bv->bv_len;
279 WARN_ON(buf->bv_resid == 0);
280 buf->bv_off = bv->bv_offset;
283 skb->dev = t->ifp->nd;
284 skb = skb_clone(skb, GFP_ATOMIC);
286 __skb_queue_tail(&d->sendq, skb);
290 /* some callers cannot sleep, and they can call this function,
291 * transmitting the packets later, when interrupts are on
294 aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
297 struct aoe_cfghdr *ch;
299 struct net_device *ifp;
302 for_each_netdev_rcu(&init_net, ifp) {
304 if (!is_aoe_netif(ifp))
307 skb = new_skb(sizeof *h + sizeof *ch);
309 printk(KERN_INFO "aoe: skb alloc failure\n");
312 skb_put(skb, sizeof *h + sizeof *ch);
314 __skb_queue_tail(queue, skb);
315 h = (struct aoe_hdr *) skb_mac_header(skb);
316 memset(h, 0, sizeof *h + sizeof *ch);
318 memset(h->dst, 0xff, sizeof h->dst);
319 memcpy(h->src, ifp->dev_addr, sizeof h->src);
320 h->type = __constant_cpu_to_be16(ETH_P_AOE);
322 h->major = cpu_to_be16(aoemajor);
333 resend(struct aoedev *d, struct aoetgt *t, struct frame *f)
337 struct aoe_atahdr *ah;
344 h = (struct aoe_hdr *) skb_mac_header(skb);
345 ah = (struct aoe_atahdr *) (h+1);
347 snprintf(buf, sizeof buf,
348 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
349 "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
350 h->src, h->dst, t->nout);
354 h->tag = cpu_to_be32(n);
355 memcpy(h->dst, t->addr, sizeof h->dst);
356 memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
358 switch (ah->cmdstat) {
361 case ATA_CMD_PIO_READ:
362 case ATA_CMD_PIO_READ_EXT:
363 case ATA_CMD_PIO_WRITE:
364 case ATA_CMD_PIO_WRITE_EXT:
371 if (ah->aflags & AOEAFL_WRITE) {
372 skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr),
373 offset_in_page(f->bufaddr), n);
374 skb->len = sizeof *h + sizeof *ah + n;
378 skb->dev = t->ifp->nd;
379 skb = skb_clone(skb, GFP_ATOMIC);
382 __skb_queue_tail(&d->sendq, skb);
390 n = jiffies & 0xffff;
397 static struct aoeif *
398 getif(struct aoetgt *t, struct net_device *nd)
410 static struct aoeif *
411 addif(struct aoetgt *t, struct net_device *nd)
419 p->maxbcnt = DEFAULTBCNT;
426 ejectif(struct aoetgt *t, struct aoeif *ifp)
431 e = t->ifs + NAOEIFS - 1;
432 n = (e - ifp) * sizeof *ifp;
433 memmove(ifp, ifp+1, n);
438 sthtith(struct aoedev *d)
440 struct frame *f, *e, *nf;
442 struct aoetgt *ht = *d->htgt;
447 if (f->tag == FREETAG)
459 resend(d, *d->tgt, nf);
461 /* he's clean, he's useless. take away his interfaces */
462 memset(ht->ifs, 0, sizeof ht->ifs);
467 static inline unsigned char
468 ata_scnt(unsigned char *packet) {
470 struct aoe_atahdr *ah;
472 h = (struct aoe_hdr *) packet;
473 ah = (struct aoe_atahdr *) (h+1);
478 rexmit_timer(ulong vp)
480 struct sk_buff_head queue;
482 struct aoetgt *t, **tt, **te;
485 register long timeout;
488 d = (struct aoedev *) vp;
490 /* timeout is always ~150% of the moving average */
492 timeout += timeout >> 1;
494 spin_lock_irqsave(&d->lock, flags);
496 if (d->flags & DEVFL_TKILL) {
497 spin_unlock_irqrestore(&d->lock, flags);
502 for (; tt < te && *tt; tt++) {
507 if (f->tag == FREETAG
508 || tsince(f->tag) < timeout)
510 n = f->waited += timeout;
512 if (n > aoe_deadsecs) {
513 /* waited too long. device failure. */
518 if (n > HELPWAIT /* see if another target can help */
519 && (tt != d->targets || d->targets[1]))
522 if (t->nout == t->maxout) {
525 t->lastwadj = jiffies;
528 ifp = getif(t, f->skb->dev);
529 if (ifp && ++ifp->lost > (t->nframes << 1)
530 && (ifp != t->ifs || t->ifs[1].nd)) {
535 if (ata_scnt(skb_mac_header(f->skb)) > DEFAULTBCNT / 512
536 && ifp && ++ifp->lostjumbo > (t->nframes << 1)
537 && ifp->maxbcnt != DEFAULTBCNT) {
540 "too many lost jumbo on "
542 "falling back to %d frames.\n",
543 d->aoemajor, d->aoeminor,
544 ifp->nd->name, t->addr,
552 if (t->nout == t->maxout
553 && t->maxout < t->nframes
554 && (jiffies - t->lastwadj)/HZ > 10) {
556 t->lastwadj = jiffies;
560 if (!skb_queue_empty(&d->sendq)) {
563 d->rttavg = MAXTIMER;
566 if (d->flags & DEVFL_KICKME || d->htgt) {
567 d->flags &= ~DEVFL_KICKME;
571 __skb_queue_head_init(&queue);
572 skb_queue_splice_init(&d->sendq, &queue);
574 d->timer.expires = jiffies + TIMERTICK;
575 add_timer(&d->timer);
577 spin_unlock_irqrestore(&d->lock, flags);
582 /* enters with d->lock held */
584 aoecmd_work(struct aoedev *d)
588 if (d->htgt && !sthtith(d))
590 if (d->inprocess == NULL) {
591 if (list_empty(&d->bufq))
593 buf = container_of(d->bufq.next, struct buf, bufs);
594 list_del(d->bufq.next);
597 if (aoecmd_ata_rw(d))
601 /* this function performs work that has been deferred until sleeping is OK
604 aoecmd_sleepwork(struct work_struct *work)
606 struct aoedev *d = container_of(work, struct aoedev, work);
608 if (d->flags & DEVFL_GDALLOC)
611 if (d->flags & DEVFL_NEWSIZE) {
612 struct block_device *bd;
616 ssize = get_capacity(d->gd);
617 bd = bdget_disk(d->gd, 0);
620 mutex_lock(&bd->bd_inode->i_mutex);
621 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
622 mutex_unlock(&bd->bd_inode->i_mutex);
625 spin_lock_irqsave(&d->lock, flags);
626 d->flags |= DEVFL_UP;
627 d->flags &= ~DEVFL_NEWSIZE;
628 spin_unlock_irqrestore(&d->lock, flags);
633 ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
638 /* word 83: command set supported */
639 n = get_unaligned_le16(&id[83 << 1]);
641 /* word 86: command set/feature enabled */
642 n |= get_unaligned_le16(&id[86 << 1]);
644 if (n & (1<<10)) { /* bit 10: LBA 48 */
645 d->flags |= DEVFL_EXT;
647 /* word 100: number lba48 sectors */
648 ssize = get_unaligned_le64(&id[100 << 1]);
650 /* set as in ide-disk.c:init_idedisk_capacity */
651 d->geo.cylinders = ssize;
652 d->geo.cylinders /= (255 * 63);
656 d->flags &= ~DEVFL_EXT;
658 /* number lba28 sectors */
659 ssize = get_unaligned_le32(&id[60 << 1]);
661 /* NOTE: obsolete in ATA 6 */
662 d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
663 d->geo.heads = get_unaligned_le16(&id[55 << 1]);
664 d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
667 if (d->ssize != ssize)
669 "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
671 d->aoemajor, d->aoeminor,
672 d->fw_ver, (long long)ssize);
675 if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
678 set_capacity(d->gd, ssize);
679 d->flags |= DEVFL_NEWSIZE;
681 d->flags |= DEVFL_GDALLOC;
682 schedule_work(&d->work);
686 calc_rttavg(struct aoedev *d, int rtt)
695 else if (n > MAXTIMER)
697 d->mintimer += (n - d->mintimer) >> 1;
698 } else if (n < d->mintimer)
700 else if (n > MAXTIMER)
703 /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
708 static struct aoetgt *
709 gettgt(struct aoedev *d, char *addr)
711 struct aoetgt **t, **e;
715 for (; t < e && *t; t++)
716 if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
722 diskstats(struct gendisk *disk, struct bio *bio, ulong duration, sector_t sector)
724 unsigned long n_sect = bio->bi_size >> 9;
725 const int rw = bio_data_dir(bio);
726 struct hd_struct *part;
729 cpu = part_stat_lock();
730 part = disk_map_sector_rcu(disk, sector);
732 part_stat_inc(cpu, part, ios[rw]);
733 part_stat_add(cpu, part, ticks[rw], duration);
734 part_stat_add(cpu, part, sectors[rw], n_sect);
735 part_stat_add(cpu, part, io_ticks, duration);
741 aoecmd_ata_rsp(struct sk_buff *skb)
743 struct sk_buff_head queue;
745 struct aoe_hdr *hin, *hout;
746 struct aoe_atahdr *ahin, *ahout;
756 hin = (struct aoe_hdr *) skb_mac_header(skb);
757 aoemajor = get_unaligned_be16(&hin->major);
758 d = aoedev_by_aoeaddr(aoemajor, hin->minor);
760 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
761 "for unknown device %d.%d\n",
762 aoemajor, hin->minor);
767 spin_lock_irqsave(&d->lock, flags);
769 n = get_unaligned_be32(&hin->tag);
770 t = gettgt(d, hin->src);
772 printk(KERN_INFO "aoe: can't find target e%ld.%d:%pm\n",
773 d->aoemajor, d->aoeminor, hin->src);
774 spin_unlock_irqrestore(&d->lock, flags);
779 calc_rttavg(d, -tsince(n));
780 spin_unlock_irqrestore(&d->lock, flags);
781 snprintf(ebuf, sizeof ebuf,
782 "%15s e%d.%d tag=%08x@%08lx\n",
784 get_unaligned_be16(&hin->major),
786 get_unaligned_be32(&hin->tag),
792 calc_rttavg(d, tsince(f->tag));
794 ahin = (struct aoe_atahdr *) (hin+1);
795 hout = (struct aoe_hdr *) skb_mac_header(f->skb);
796 ahout = (struct aoe_atahdr *) (hout+1);
799 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
801 "aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
802 ahout->cmdstat, ahin->cmdstat,
803 d->aoemajor, d->aoeminor);
805 buf->flags |= BUFFL_FAIL;
807 if (d->htgt && t == *d->htgt) /* I'll help myself, thank you. */
809 n = ahout->scnt << 9;
810 switch (ahout->cmdstat) {
811 case ATA_CMD_PIO_READ:
812 case ATA_CMD_PIO_READ_EXT:
813 if (skb->len - sizeof *hin - sizeof *ahin < n) {
815 "aoe: %s. skb->len=%d need=%ld\n",
816 "runt data size in read", skb->len, n);
817 /* fail frame f? just returning will rexmit. */
818 spin_unlock_irqrestore(&d->lock, flags);
821 memcpy(f->bufaddr, ahin+1, n);
822 case ATA_CMD_PIO_WRITE:
823 case ATA_CMD_PIO_WRITE_EXT:
824 ifp = getif(t, skb->dev);
838 if (skb->len - sizeof *hin - sizeof *ahin < 512) {
840 "aoe: runt data size in ataid. skb->len=%d\n",
842 spin_unlock_irqrestore(&d->lock, flags);
845 ataid_complete(d, t, (char *) (ahin+1));
849 "aoe: unrecognized ata command %2.2Xh for %d.%d\n",
851 get_unaligned_be16(&hin->major),
856 if (buf && --buf->nframesout == 0 && buf->resid == 0) {
857 diskstats(d->gd, buf->bio, jiffies - buf->stime, buf->sector);
858 if (buf->flags & BUFFL_FAIL)
859 bio_endio(buf->bio, -EIO);
861 bio_flush_dcache_pages(buf->bio);
862 bio_endio(buf->bio, 0);
864 mempool_free(buf, d->bufpool);
873 __skb_queue_head_init(&queue);
874 skb_queue_splice_init(&d->sendq, &queue);
876 spin_unlock_irqrestore(&d->lock, flags);
881 aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
883 struct sk_buff_head queue;
885 __skb_queue_head_init(&queue);
886 aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
891 aoecmd_ata_id(struct aoedev *d)
894 struct aoe_atahdr *ah;
905 /* initialize the headers & frame */
907 h = (struct aoe_hdr *) skb_mac_header(skb);
908 ah = (struct aoe_atahdr *) (h+1);
909 skb_put(skb, sizeof *h + sizeof *ah);
910 memset(h, 0, skb->len);
911 f->tag = aoehdr_atainit(d, t, h);
915 /* set up ata header */
917 ah->cmdstat = ATA_CMD_ID_ATA;
920 skb->dev = t->ifp->nd;
922 d->rttavg = MAXTIMER;
923 d->timer.function = rexmit_timer;
925 return skb_clone(skb, GFP_ATOMIC);
928 static struct aoetgt *
929 addtgt(struct aoedev *d, char *addr, ulong nframes)
931 struct aoetgt *t, **tt, **te;
936 for (; tt < te && *tt; tt++)
941 "aoe: device addtgt failure; too many targets\n");
944 t = kcalloc(1, sizeof *t, GFP_ATOMIC);
945 f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
949 printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
953 t->nframes = nframes;
958 memcpy(t->addr, addr, sizeof t->addr);
960 t->maxout = t->nframes;
965 aoecmd_cfg_rsp(struct sk_buff *skb)
969 struct aoe_cfghdr *ch;
972 ulong flags, sysminor, aoemajor;
976 h = (struct aoe_hdr *) skb_mac_header(skb);
977 ch = (struct aoe_cfghdr *) (h+1);
980 * Enough people have their dip switches set backwards to
981 * warrant a loud message for this special case.
983 aoemajor = get_unaligned_be16(&h->major);
984 if (aoemajor == 0xfff) {
985 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
986 "Check shelf dip switches.\n");
990 sysminor = SYSMINOR(aoemajor, h->minor);
991 if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
992 printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n",
993 aoemajor, (int) h->minor);
997 n = be16_to_cpu(ch->bufcnt);
998 if (n > aoe_maxout) /* keep it reasonable */
1001 d = aoedev_by_sysminor_m(sysminor);
1003 printk(KERN_INFO "aoe: device sysminor_m failure\n");
1007 spin_lock_irqsave(&d->lock, flags);
1009 t = gettgt(d, h->src);
1011 t = addtgt(d, h->src, n);
1013 spin_unlock_irqrestore(&d->lock, flags);
1017 ifp = getif(t, skb->dev);
1019 ifp = addif(t, skb->dev);
1022 "aoe: device addif failure; "
1023 "too many interfaces?\n");
1024 spin_unlock_irqrestore(&d->lock, flags);
1030 n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr);
1034 n = n ? n * 512 : DEFAULTBCNT;
1035 if (n != ifp->maxbcnt) {
1037 "aoe: e%ld.%d: setting %d%s%s:%pm\n",
1038 d->aoemajor, d->aoeminor, n,
1039 " byte data frames on ", ifp->nd->name,
1045 /* don't change users' perspective */
1047 spin_unlock_irqrestore(&d->lock, flags);
1050 d->fw_ver = be16_to_cpu(ch->fwver);
1052 sl = aoecmd_ata_id(d);
1054 spin_unlock_irqrestore(&d->lock, flags);
1057 struct sk_buff_head queue;
1058 __skb_queue_head_init(&queue);
1059 __skb_queue_tail(&queue, sl);
1060 aoenet_xmit(&queue);
1065 aoecmd_cleanslate(struct aoedev *d)
1067 struct aoetgt **t, **te;
1068 struct aoeif *p, *e;
1070 d->mintimer = MINTIMER;
1074 for (; t < te && *t; t++) {
1075 (*t)->maxout = (*t)->nframes;
1078 for (; p < e; p++) {
1081 p->maxbcnt = DEFAULTBCNT;