1 /* Copyright (c) 2006 Coraid, Inc. See COPYING for GPL terms. */
4 * Filesystem request handling methods
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
9 #include <linux/skbuff.h>
10 #include <linux/netdevice.h>
11 #include <linux/genhd.h>
12 #include <asm/unaligned.h>
15 #define TIMERTICK (HZ / 10)
16 #define MINTIMER (2 * TIMERTICK)
17 #define MAXTIMER (HZ << 1)
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.");
28 skb = alloc_skb(len, GFP_ATOMIC);
30 skb->nh.raw = skb->mac.raw = skb->data;
31 skb->protocol = __constant_htons(ETH_P_AOE);
34 memset(skb->head, 0, len);
35 skb->next = skb->prev = NULL;
37 /* tell the network layer not to perform IP checksums
38 * or to get the NIC to do it
40 skb->ip_summed = CHECKSUM_NONE;
46 getframe(struct aoedev *d, int tag)
59 * Leave the top bit clear so we have tagspace for userland.
60 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
61 * This driver reserves tag -1 to mean "unused frame."
64 newtag(struct aoedev *d)
69 return n |= (++d->lasttag & 0x7fff) << 16;
73 aoehdr_atainit(struct aoedev *d, struct aoe_hdr *h)
75 u32 host_tag = newtag(d);
77 memcpy(h->src, d->ifp->dev_addr, sizeof h->src);
78 memcpy(h->dst, d->addr, sizeof h->dst);
79 h->type = __constant_cpu_to_be16(ETH_P_AOE);
81 h->major = cpu_to_be16(d->aoemajor);
82 h->minor = d->aoeminor;
84 h->tag = cpu_to_be32(host_tag);
90 put_lba(struct aoe_atahdr *ah, sector_t lba)
101 aoecmd_ata_rw(struct aoedev *d, struct frame *f)
104 struct aoe_atahdr *ah;
108 register sector_t sector;
109 char writebit, extbit;
116 sector = buf->sector;
117 bcnt = buf->bv_resid;
118 if (bcnt > d->maxbcnt)
121 /* initialize the headers & frame */
123 h = (struct aoe_hdr *) skb->mac.raw;
124 ah = (struct aoe_atahdr *) (h+1);
125 skb->len = sizeof *h + sizeof *ah;
126 memset(h, 0, ETH_ZLEN);
127 f->tag = aoehdr_atainit(d, h);
130 f->bufaddr = buf->bufaddr;
134 /* set up ata header */
135 ah->scnt = bcnt >> 9;
137 if (d->flags & DEVFL_EXT) {
138 ah->aflags |= AOEAFL_EXT;
142 ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
145 if (bio_data_dir(buf->bio) == WRITE) {
146 skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr),
147 offset_in_page(f->bufaddr), bcnt);
148 ah->aflags |= AOEAFL_WRITE;
150 skb->data_len = bcnt;
156 ah->cmdstat = WIN_READ | writebit | extbit;
158 /* mark all tracking fields and load out */
159 buf->nframesout += 1;
160 buf->bufaddr += bcnt;
161 buf->bv_resid -= bcnt;
162 /* printk(KERN_DEBUG "aoe: bv_resid=%ld\n", buf->bv_resid); */
164 buf->sector += bcnt >> 9;
165 if (buf->resid == 0) {
167 } else if (buf->bv_resid == 0) {
169 WARN_ON(buf->bv->bv_len == 0);
170 buf->bv_resid = buf->bv->bv_len;
171 buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset;
175 skb = skb_clone(skb, GFP_ATOMIC);
179 d->sendq_tl->next = skb;
185 /* some callers cannot sleep, and they can call this function,
186 * transmitting the packets later, when interrupts are on
188 static struct sk_buff *
189 aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff **tail)
192 struct aoe_cfghdr *ch;
193 struct sk_buff *skb, *sl, *sl_tail;
194 struct net_device *ifp;
198 read_lock(&dev_base_lock);
199 for (ifp = dev_base; ifp; dev_put(ifp), ifp = ifp->next) {
201 if (!is_aoe_netif(ifp))
204 skb = new_skb(sizeof *h + sizeof *ch);
206 printk(KERN_INFO "aoe: skb alloc failure\n");
212 h = (struct aoe_hdr *) skb->mac.raw;
213 memset(h, 0, sizeof *h + sizeof *ch);
215 memset(h->dst, 0xff, sizeof h->dst);
216 memcpy(h->src, ifp->dev_addr, sizeof h->src);
217 h->type = __constant_cpu_to_be16(ETH_P_AOE);
219 h->major = cpu_to_be16(aoemajor);
226 read_unlock(&dev_base_lock);
233 static struct frame *
234 freeframe(struct aoedev *d)
242 if (f->tag != FREETAG)
244 if (atomic_read(&skb_shinfo(f->skb)->dataref) == 1) {
245 skb_shinfo(f->skb)->nr_frags = f->skb->data_len = 0;
250 if (n == d->nframes) /* wait for network layer */
251 d->flags |= DEVFL_KICKME;
256 /* enters with d->lock held */
258 aoecmd_work(struct aoedev *d)
263 if (d->flags & DEVFL_PAUSE) {
264 if (!aoedev_isbusy(d))
265 d->sendq_hd = aoecmd_cfg_pkts(d->aoemajor,
266 d->aoeminor, &d->sendq_tl);
274 if (d->inprocess == NULL) {
275 if (list_empty(&d->bufq))
277 buf = container_of(d->bufq.next, struct buf, bufs);
278 list_del(d->bufq.next);
279 /*printk(KERN_DEBUG "aoe: bi_size=%ld\n", buf->bio->bi_size); */
287 rexmit(struct aoedev *d, struct frame *f)
291 struct aoe_atahdr *ah;
297 snprintf(buf, sizeof buf,
298 "%15s e%ld.%ld oldtag=%08x@%08lx newtag=%08x\n",
300 d->aoemajor, d->aoeminor, f->tag, jiffies, n);
304 h = (struct aoe_hdr *) skb->mac.raw;
305 ah = (struct aoe_atahdr *) (h+1);
307 h->tag = cpu_to_be32(n);
308 memcpy(h->dst, d->addr, sizeof h->dst);
309 memcpy(h->src, d->ifp->dev_addr, sizeof h->src);
311 n = DEFAULTBCNT / 512;
314 if (ah->aflags & AOEAFL_WRITE) {
315 skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr),
316 offset_in_page(f->bufaddr), DEFAULTBCNT);
317 skb->len = sizeof *h + sizeof *ah + DEFAULTBCNT;
318 skb->data_len = DEFAULTBCNT;
320 if (++d->lostjumbo > (d->nframes << 1))
321 if (d->maxbcnt != DEFAULTBCNT) {
322 printk(KERN_INFO "aoe: e%ld.%ld: too many lost jumbo on %s - using 1KB frames.\n",
323 d->aoemajor, d->aoeminor, d->ifp->name);
324 d->maxbcnt = DEFAULTBCNT;
325 d->flags |= DEVFL_MAXBCNT;
330 skb = skb_clone(skb, GFP_ATOMIC);
334 d->sendq_tl->next = skb;
345 n = jiffies & 0xffff;
353 rexmit_timer(ulong vp)
358 register long timeout;
361 d = (struct aoedev *) vp;
364 /* timeout is always ~150% of the moving average */
366 timeout += timeout >> 1;
368 spin_lock_irqsave(&d->lock, flags);
370 if (d->flags & DEVFL_TKILL) {
371 spin_unlock_irqrestore(&d->lock, flags);
377 if (f->tag != FREETAG && tsince(f->tag) >= timeout) {
378 n = f->waited += timeout;
380 if (n > aoe_deadsecs) { /* waited too long for response */
387 if (d->flags & DEVFL_KICKME) {
388 d->flags &= ~DEVFL_KICKME;
393 d->sendq_hd = d->sendq_tl = NULL;
397 d->rttavg = MAXTIMER;
400 d->timer.expires = jiffies + TIMERTICK;
401 add_timer(&d->timer);
403 spin_unlock_irqrestore(&d->lock, flags);
408 /* this function performs work that has been deferred until sleeping is OK
411 aoecmd_sleepwork(void *vp)
413 struct aoedev *d = (struct aoedev *) vp;
415 if (d->flags & DEVFL_GDALLOC)
418 if (d->flags & DEVFL_NEWSIZE) {
419 struct block_device *bd;
423 ssize = d->gd->capacity;
424 bd = bdget_disk(d->gd, 0);
427 mutex_lock(&bd->bd_inode->i_mutex);
428 i_size_write(bd->bd_inode, (loff_t)ssize<<9);
429 mutex_unlock(&bd->bd_inode->i_mutex);
432 spin_lock_irqsave(&d->lock, flags);
433 d->flags |= DEVFL_UP;
434 d->flags &= ~DEVFL_NEWSIZE;
435 spin_unlock_irqrestore(&d->lock, flags);
440 ataid_complete(struct aoedev *d, unsigned char *id)
445 /* word 83: command set supported */
446 n = le16_to_cpu(get_unaligned((__le16 *) &id[83<<1]));
448 /* word 86: command set/feature enabled */
449 n |= le16_to_cpu(get_unaligned((__le16 *) &id[86<<1]));
451 if (n & (1<<10)) { /* bit 10: LBA 48 */
452 d->flags |= DEVFL_EXT;
454 /* word 100: number lba48 sectors */
455 ssize = le64_to_cpu(get_unaligned((__le64 *) &id[100<<1]));
457 /* set as in ide-disk.c:init_idedisk_capacity */
458 d->geo.cylinders = ssize;
459 d->geo.cylinders /= (255 * 63);
463 d->flags &= ~DEVFL_EXT;
465 /* number lba28 sectors */
466 ssize = le32_to_cpu(get_unaligned((__le32 *) &id[60<<1]));
468 /* NOTE: obsolete in ATA 6 */
469 d->geo.cylinders = le16_to_cpu(get_unaligned((__le16 *) &id[54<<1]));
470 d->geo.heads = le16_to_cpu(get_unaligned((__le16 *) &id[55<<1]));
471 d->geo.sectors = le16_to_cpu(get_unaligned((__le16 *) &id[56<<1]));
474 if (d->ssize != ssize)
475 printk(KERN_INFO "aoe: %012llx e%lu.%lu v%04x has %llu sectors\n",
476 (unsigned long long)mac_addr(d->addr),
477 d->aoemajor, d->aoeminor,
478 d->fw_ver, (long long)ssize);
482 d->gd->capacity = ssize;
483 d->flags |= DEVFL_NEWSIZE;
485 if (d->flags & DEVFL_GDALLOC) {
486 printk(KERN_ERR "aoe: can't schedule work for e%lu.%lu, %s\n",
487 d->aoemajor, d->aoeminor,
488 "it's already on! This shouldn't happen.\n");
491 d->flags |= DEVFL_GDALLOC;
493 schedule_work(&d->work);
497 calc_rttavg(struct aoedev *d, int rtt)
506 else if (n > MAXTIMER)
508 d->mintimer += (n - d->mintimer) >> 1;
509 } else if (n < d->mintimer)
511 else if (n > MAXTIMER)
514 /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
520 aoecmd_ata_rsp(struct sk_buff *skb)
523 struct aoe_hdr *hin, *hout;
524 struct aoe_atahdr *ahin, *ahout;
533 hin = (struct aoe_hdr *) skb->mac.raw;
534 aoemajor = be16_to_cpu(hin->major);
535 d = aoedev_by_aoeaddr(aoemajor, hin->minor);
537 snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
538 "for unknown device %d.%d\n",
539 aoemajor, hin->minor);
544 spin_lock_irqsave(&d->lock, flags);
546 n = be32_to_cpu(hin->tag);
549 calc_rttavg(d, -tsince(n));
550 spin_unlock_irqrestore(&d->lock, flags);
551 snprintf(ebuf, sizeof ebuf,
552 "%15s e%d.%d tag=%08x@%08lx\n",
554 be16_to_cpu(hin->major),
556 be32_to_cpu(hin->tag),
562 calc_rttavg(d, tsince(f->tag));
564 ahin = (struct aoe_atahdr *) (hin+1);
565 hout = (struct aoe_hdr *) f->skb->mac.raw;
566 ahout = (struct aoe_atahdr *) (hout+1);
569 if (ahout->cmdstat == WIN_IDENTIFY)
570 d->flags &= ~DEVFL_PAUSE;
571 if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
573 "aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%ld\n",
574 ahout->cmdstat, ahin->cmdstat,
575 d->aoemajor, d->aoeminor);
577 buf->flags |= BUFFL_FAIL;
579 n = ahout->scnt << 9;
580 switch (ahout->cmdstat) {
583 if (skb->len - sizeof *hin - sizeof *ahin < n) {
585 "aoe: runt data size in read. skb->len=%d\n",
587 /* fail frame f? just returning will rexmit. */
588 spin_unlock_irqrestore(&d->lock, flags);
591 memcpy(f->bufaddr, ahin+1, n);
597 put_lba(ahout, f->lba += ahout->scnt);
601 ahout->scnt = n >> 9;
602 if (ahout->aflags & AOEAFL_WRITE) {
603 skb_fill_page_desc(skb, 0,
604 virt_to_page(f->bufaddr),
605 offset_in_page(f->bufaddr), n);
606 skb->len = sizeof *hout + sizeof *ahout + n;
610 hout->tag = cpu_to_be32(f->tag);
612 skb = skb_clone(skb, GFP_ATOMIC);
613 spin_unlock_irqrestore(&d->lock, flags);
622 if (skb->len - sizeof *hin - sizeof *ahin < 512) {
624 "aoe: runt data size in ataid. skb->len=%d\n",
626 spin_unlock_irqrestore(&d->lock, flags);
629 ataid_complete(d, (char *) (ahin+1));
633 "aoe: unrecognized ata command %2.2Xh for %d.%d\n",
635 be16_to_cpu(hin->major),
641 buf->nframesout -= 1;
642 if (buf->nframesout == 0 && buf->resid == 0) {
643 unsigned long duration = jiffies - buf->start_time;
644 unsigned long n_sect = buf->bio->bi_size >> 9;
645 struct gendisk *disk = d->gd;
646 const int rw = bio_data_dir(buf->bio);
648 disk_stat_inc(disk, ios[rw]);
649 disk_stat_add(disk, ticks[rw], duration);
650 disk_stat_add(disk, sectors[rw], n_sect);
651 disk_stat_add(disk, io_ticks, duration);
652 n = (buf->flags & BUFFL_FAIL) ? -EIO : 0;
653 bio_endio(buf->bio, buf->bio->bi_size, n);
654 mempool_free(buf, d->bufpool);
663 d->sendq_hd = d->sendq_tl = NULL;
665 spin_unlock_irqrestore(&d->lock, flags);
670 aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
674 sl = aoecmd_cfg_pkts(aoemajor, aoeminor, NULL);
680 * Since we only call this in one place (and it only prepares one frame)
681 * we just return the skb. Usually we'd chain it up to the aoedev sendq.
683 static struct sk_buff *
684 aoecmd_ata_id(struct aoedev *d)
687 struct aoe_atahdr *ah;
693 printk(KERN_ERR "aoe: can't get a frame. This shouldn't happen.\n");
697 /* initialize the headers & frame */
699 h = (struct aoe_hdr *) skb->mac.raw;
700 ah = (struct aoe_atahdr *) (h+1);
702 memset(h, 0, ETH_ZLEN);
703 f->tag = aoehdr_atainit(d, h);
706 /* set up ata header */
708 ah->cmdstat = WIN_IDENTIFY;
713 d->rttavg = MAXTIMER;
714 d->timer.function = rexmit_timer;
716 return skb_clone(skb, GFP_ATOMIC);
720 aoecmd_cfg_rsp(struct sk_buff *skb)
724 struct aoe_cfghdr *ch;
725 ulong flags, sysminor, aoemajor;
727 enum { MAXFRAMES = 16 };
730 h = (struct aoe_hdr *) skb->mac.raw;
731 ch = (struct aoe_cfghdr *) (h+1);
734 * Enough people have their dip switches set backwards to
735 * warrant a loud message for this special case.
737 aoemajor = be16_to_cpu(h->major);
738 if (aoemajor == 0xfff) {
739 printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
740 "Check shelf dip switches.\n");
744 sysminor = SYSMINOR(aoemajor, h->minor);
745 if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
746 printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n",
747 aoemajor, (int) h->minor);
751 n = be16_to_cpu(ch->bufcnt);
752 if (n > MAXFRAMES) /* keep it reasonable */
755 d = aoedev_by_sysminor_m(sysminor, n);
757 printk(KERN_INFO "aoe: device sysminor_m failure\n");
761 spin_lock_irqsave(&d->lock, flags);
763 /* permit device to migrate mac and network interface */
765 memcpy(d->addr, h->src, sizeof d->addr);
766 if (!(d->flags & DEVFL_MAXBCNT)) {
768 n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr);
772 n = n ? n * 512 : DEFAULTBCNT;
773 if (n != d->maxbcnt) {
775 "aoe: e%ld.%ld: setting %d byte data frames on %s\n",
776 d->aoemajor, d->aoeminor, n, d->ifp->name);
781 /* don't change users' perspective */
782 if (d->nopen && !(d->flags & DEVFL_PAUSE)) {
783 spin_unlock_irqrestore(&d->lock, flags);
786 d->flags |= DEVFL_PAUSE; /* force pause */
787 d->mintimer = MINTIMER;
788 d->fw_ver = be16_to_cpu(ch->fwver);
790 /* check for already outstanding ataid */
791 sl = aoedev_isbusy(d) == 0 ? aoecmd_ata_id(d) : NULL;
793 spin_unlock_irqrestore(&d->lock, flags);