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)
18 #define MAXWAIT (60 * 3) /* After MAXWAIT seconds, give up and fail dev */
25 skb
= alloc_skb(len
, GFP_ATOMIC
);
27 skb
->nh
.raw
= skb
->mac
.raw
= skb
->data
;
28 skb
->protocol
= __constant_htons(ETH_P_AOE
);
31 memset(skb
->head
, 0, len
);
32 skb
->next
= skb
->prev
= NULL
;
34 /* tell the network layer not to perform IP checksums
35 * or to get the NIC to do it
37 skb
->ip_summed
= CHECKSUM_NONE
;
43 getframe(struct aoedev
*d
, int tag
)
56 * Leave the top bit clear so we have tagspace for userland.
57 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
58 * This driver reserves tag -1 to mean "unused frame."
61 newtag(struct aoedev
*d
)
66 return n
|= (++d
->lasttag
& 0x7fff) << 16;
70 aoehdr_atainit(struct aoedev
*d
, struct aoe_hdr
*h
)
72 u32 host_tag
= newtag(d
);
74 memcpy(h
->src
, d
->ifp
->dev_addr
, sizeof h
->src
);
75 memcpy(h
->dst
, d
->addr
, sizeof h
->dst
);
76 h
->type
= __constant_cpu_to_be16(ETH_P_AOE
);
78 h
->major
= cpu_to_be16(d
->aoemajor
);
79 h
->minor
= d
->aoeminor
;
81 h
->tag
= cpu_to_be32(host_tag
);
87 put_lba(struct aoe_atahdr
*ah
, sector_t lba
)
98 aoecmd_ata_rw(struct aoedev
*d
, struct frame
*f
)
101 struct aoe_atahdr
*ah
;
105 register sector_t sector
;
106 char writebit
, extbit
;
113 sector
= buf
->sector
;
114 bcnt
= buf
->bv_resid
;
115 if (bcnt
> d
->maxbcnt
)
118 /* initialize the headers & frame */
120 h
= (struct aoe_hdr
*) skb
->mac
.raw
;
121 ah
= (struct aoe_atahdr
*) (h
+1);
122 skb
->len
= sizeof *h
+ sizeof *ah
;
123 memset(h
, 0, skb
->len
);
124 f
->tag
= aoehdr_atainit(d
, h
);
127 f
->bufaddr
= buf
->bufaddr
;
131 /* set up ata header */
132 ah
->scnt
= bcnt
>> 9;
134 if (d
->flags
& DEVFL_EXT
) {
135 ah
->aflags
|= AOEAFL_EXT
;
139 ah
->lba3
|= 0xe0; /* LBA bit + obsolete 0xa0 */
142 if (bio_data_dir(buf
->bio
) == WRITE
) {
143 skb_fill_page_desc(skb
, 0, virt_to_page(f
->bufaddr
),
144 offset_in_page(f
->bufaddr
), bcnt
);
145 ah
->aflags
|= AOEAFL_WRITE
;
147 skb_shinfo(skb
)->nr_frags
= 0;
152 ah
->cmdstat
= WIN_READ
| writebit
| extbit
;
154 /* mark all tracking fields and load out */
155 buf
->nframesout
+= 1;
156 buf
->bufaddr
+= bcnt
;
157 buf
->bv_resid
-= bcnt
;
158 /* printk(KERN_INFO "aoe: bv_resid=%ld\n", buf->bv_resid); */
160 buf
->sector
+= bcnt
>> 9;
161 if (buf
->resid
== 0) {
163 } else if (buf
->bv_resid
== 0) {
165 buf
->bv_resid
= buf
->bv
->bv_len
;
166 buf
->bufaddr
= page_address(buf
->bv
->bv_page
) + buf
->bv
->bv_offset
;
173 d
->sendq_tl
->next
= skb
;
179 /* some callers cannot sleep, and they can call this function,
180 * transmitting the packets later, when interrupts are on
182 static struct sk_buff
*
183 aoecmd_cfg_pkts(ushort aoemajor
, unsigned char aoeminor
, struct sk_buff
**tail
)
186 struct aoe_cfghdr
*ch
;
187 struct sk_buff
*skb
, *sl
, *sl_tail
;
188 struct net_device
*ifp
;
192 read_lock(&dev_base_lock
);
193 for (ifp
= dev_base
; ifp
; dev_put(ifp
), ifp
= ifp
->next
) {
195 if (!is_aoe_netif(ifp
))
198 skb
= new_skb(sizeof *h
+ sizeof *ch
);
200 printk(KERN_INFO
"aoe: aoecmd_cfg: skb alloc failure\n");
206 h
= (struct aoe_hdr
*) skb
->mac
.raw
;
207 memset(h
, 0, sizeof *h
+ sizeof *ch
);
209 memset(h
->dst
, 0xff, sizeof h
->dst
);
210 memcpy(h
->src
, ifp
->dev_addr
, sizeof h
->src
);
211 h
->type
= __constant_cpu_to_be16(ETH_P_AOE
);
213 h
->major
= cpu_to_be16(aoemajor
);
220 read_unlock(&dev_base_lock
);
227 /* enters with d->lock held */
229 aoecmd_work(struct aoedev
*d
)
234 if (d
->flags
& DEVFL_PAUSE
) {
235 if (!aoedev_isbusy(d
))
236 d
->sendq_hd
= aoecmd_cfg_pkts(d
->aoemajor
,
237 d
->aoeminor
, &d
->sendq_tl
);
242 f
= getframe(d
, FREETAG
);
245 if (d
->inprocess
== NULL
) {
246 if (list_empty(&d
->bufq
))
248 buf
= container_of(d
->bufq
.next
, struct buf
, bufs
);
249 list_del(d
->bufq
.next
);
250 /*printk(KERN_INFO "aoecmd_work: bi_size=%ld\n", buf->bio->bi_size); */
258 rexmit(struct aoedev
*d
, struct frame
*f
)
262 struct aoe_atahdr
*ah
;
268 snprintf(buf
, sizeof buf
,
269 "%15s e%ld.%ld oldtag=%08x@%08lx newtag=%08x\n",
271 d
->aoemajor
, d
->aoeminor
, f
->tag
, jiffies
, n
);
275 h
= (struct aoe_hdr
*) skb
->mac
.raw
;
276 ah
= (struct aoe_atahdr
*) (h
+1);
278 h
->tag
= cpu_to_be32(n
);
279 memcpy(h
->dst
, d
->addr
, sizeof h
->dst
);
280 memcpy(h
->src
, d
->ifp
->dev_addr
, sizeof h
->src
);
282 n
= DEFAULTBCNT
/ 512;
285 if (ah
->aflags
& AOEAFL_WRITE
)
286 skb_fill_page_desc(skb
, 0, virt_to_page(f
->bufaddr
),
287 offset_in_page(f
->bufaddr
), DEFAULTBCNT
);
288 if (++d
->lostjumbo
> (d
->nframes
<< 1))
289 if (d
->maxbcnt
!= DEFAULTBCNT
) {
290 printk(KERN_INFO
"aoe: rexmit: too many lost jumbo. "
291 "dropping back to 1KB frames.\n");
292 d
->maxbcnt
= DEFAULTBCNT
;
293 d
->flags
|= DEVFL_MAXBCNT
;
301 d
->sendq_tl
->next
= skb
;
312 n
= jiffies
& 0xffff;
320 rexmit_timer(ulong vp
)
325 register long timeout
;
328 d
= (struct aoedev
*) vp
;
331 /* timeout is always ~150% of the moving average */
333 timeout
+= timeout
>> 1;
335 spin_lock_irqsave(&d
->lock
, flags
);
337 if (d
->flags
& DEVFL_TKILL
) {
338 spin_unlock_irqrestore(&d
->lock
, flags
);
344 if (f
->tag
!= FREETAG
&& tsince(f
->tag
) >= timeout
) {
345 n
= f
->waited
+= timeout
;
347 if (n
> MAXWAIT
) { /* waited too long. device failure. */
356 d
->sendq_hd
= d
->sendq_tl
= NULL
;
360 d
->rttavg
= MAXTIMER
;
363 d
->timer
.expires
= jiffies
+ TIMERTICK
;
364 add_timer(&d
->timer
);
366 spin_unlock_irqrestore(&d
->lock
, flags
);
371 /* this function performs work that has been deferred until sleeping is OK
374 aoecmd_sleepwork(void *vp
)
376 struct aoedev
*d
= (struct aoedev
*) vp
;
378 if (d
->flags
& DEVFL_GDALLOC
)
381 if (d
->flags
& DEVFL_NEWSIZE
) {
382 struct block_device
*bd
;
386 ssize
= d
->gd
->capacity
;
387 bd
= bdget_disk(d
->gd
, 0);
390 mutex_lock(&bd
->bd_inode
->i_mutex
);
391 i_size_write(bd
->bd_inode
, (loff_t
)ssize
<<9);
392 mutex_unlock(&bd
->bd_inode
->i_mutex
);
395 spin_lock_irqsave(&d
->lock
, flags
);
396 d
->flags
|= DEVFL_UP
;
397 d
->flags
&= ~DEVFL_NEWSIZE
;
398 spin_unlock_irqrestore(&d
->lock
, flags
);
403 ataid_complete(struct aoedev
*d
, unsigned char *id
)
408 /* word 83: command set supported */
409 n
= le16_to_cpu(get_unaligned((__le16
*) &id
[83<<1]));
411 /* word 86: command set/feature enabled */
412 n
|= le16_to_cpu(get_unaligned((__le16
*) &id
[86<<1]));
414 if (n
& (1<<10)) { /* bit 10: LBA 48 */
415 d
->flags
|= DEVFL_EXT
;
417 /* word 100: number lba48 sectors */
418 ssize
= le64_to_cpu(get_unaligned((__le64
*) &id
[100<<1]));
420 /* set as in ide-disk.c:init_idedisk_capacity */
421 d
->geo
.cylinders
= ssize
;
422 d
->geo
.cylinders
/= (255 * 63);
426 d
->flags
&= ~DEVFL_EXT
;
428 /* number lba28 sectors */
429 ssize
= le32_to_cpu(get_unaligned((__le32
*) &id
[60<<1]));
431 /* NOTE: obsolete in ATA 6 */
432 d
->geo
.cylinders
= le16_to_cpu(get_unaligned((__le16
*) &id
[54<<1]));
433 d
->geo
.heads
= le16_to_cpu(get_unaligned((__le16
*) &id
[55<<1]));
434 d
->geo
.sectors
= le16_to_cpu(get_unaligned((__le16
*) &id
[56<<1]));
437 if (d
->ssize
!= ssize
)
438 printk(KERN_INFO
"aoe: %012llx e%lu.%lu v%04x has %llu "
439 "sectors\n", (unsigned long long)mac_addr(d
->addr
),
440 d
->aoemajor
, d
->aoeminor
,
441 d
->fw_ver
, (long long)ssize
);
445 d
->gd
->capacity
= ssize
;
446 d
->flags
|= DEVFL_NEWSIZE
;
448 if (d
->flags
& DEVFL_GDALLOC
) {
449 printk(KERN_INFO
"aoe: %s: %s e%lu.%lu, %s\n",
451 "can't schedule work for",
452 d
->aoemajor
, d
->aoeminor
,
453 "it's already on! (This really shouldn't happen).\n");
456 d
->flags
|= DEVFL_GDALLOC
;
458 schedule_work(&d
->work
);
462 calc_rttavg(struct aoedev
*d
, int rtt
)
469 else if (n
> MAXTIMER
)
472 /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
478 aoecmd_ata_rsp(struct sk_buff
*skb
)
482 struct aoe_atahdr
*ahin
, *ahout
;
491 hin
= (struct aoe_hdr
*) skb
->mac
.raw
;
492 aoemajor
= be16_to_cpu(hin
->major
);
493 d
= aoedev_by_aoeaddr(aoemajor
, hin
->minor
);
495 snprintf(ebuf
, sizeof ebuf
, "aoecmd_ata_rsp: ata response "
496 "for unknown device %d.%d\n",
497 aoemajor
, hin
->minor
);
502 spin_lock_irqsave(&d
->lock
, flags
);
504 f
= getframe(d
, be32_to_cpu(hin
->tag
));
506 spin_unlock_irqrestore(&d
->lock
, flags
);
507 snprintf(ebuf
, sizeof ebuf
,
508 "%15s e%d.%d tag=%08x@%08lx\n",
510 be16_to_cpu(hin
->major
),
512 be32_to_cpu(hin
->tag
),
518 calc_rttavg(d
, tsince(f
->tag
));
520 ahin
= (struct aoe_atahdr
*) (hin
+1);
521 ahout
= (struct aoe_atahdr
*) (f
->skb
->mac
.raw
+ sizeof(struct aoe_hdr
));
524 if (ahout
->cmdstat
== WIN_IDENTIFY
)
525 d
->flags
&= ~DEVFL_PAUSE
;
526 if (ahin
->cmdstat
& 0xa9) { /* these bits cleared on success */
527 printk(KERN_CRIT
"aoe: aoecmd_ata_rsp: ata error cmd=%2.2Xh "
528 "stat=%2.2Xh from e%ld.%ld\n",
529 ahout
->cmdstat
, ahin
->cmdstat
,
530 d
->aoemajor
, d
->aoeminor
);
532 buf
->flags
|= BUFFL_FAIL
;
534 n
= ahout
->scnt
<< 9;
535 switch (ahout
->cmdstat
) {
538 if (skb
->len
- sizeof *hin
- sizeof *ahin
< n
) {
539 printk(KERN_CRIT
"aoe: aoecmd_ata_rsp: runt "
540 "ata data size in read. skb->len=%d\n",
542 /* fail frame f? just returning will rexmit. */
543 spin_unlock_irqrestore(&d
->lock
, flags
);
546 memcpy(f
->bufaddr
, ahin
+1, n
);
551 put_lba(ahout
, f
->lba
+= ahout
->scnt
);
552 n
= f
->bcnt
> DEFAULTBCNT
? DEFAULTBCNT
: f
->bcnt
;
553 ahout
->scnt
= n
>> 9;
554 if (ahout
->aflags
& AOEAFL_WRITE
)
555 skb_fill_page_desc(f
->skb
, 0, virt_to_page(f
->bufaddr
),
556 offset_in_page(f
->bufaddr
), n
);
559 spin_unlock_irqrestore(&d
->lock
, flags
);
567 if (skb
->len
- sizeof *hin
- sizeof *ahin
< 512) {
568 printk(KERN_INFO
"aoe: aoecmd_ata_rsp: runt data size "
569 "in ataid. skb->len=%d\n", skb
->len
);
570 spin_unlock_irqrestore(&d
->lock
, flags
);
573 ataid_complete(d
, (char *) (ahin
+1));
576 printk(KERN_INFO
"aoe: aoecmd_ata_rsp: unrecognized "
577 "outbound ata command %2.2Xh for %d.%d\n",
579 be16_to_cpu(hin
->major
),
585 buf
->nframesout
-= 1;
586 if (buf
->nframesout
== 0 && buf
->resid
== 0) {
587 unsigned long duration
= jiffies
- buf
->start_time
;
588 unsigned long n_sect
= buf
->bio
->bi_size
>> 9;
589 struct gendisk
*disk
= d
->gd
;
590 const int rw
= bio_data_dir(buf
->bio
);
592 disk_stat_inc(disk
, ios
[rw
]);
593 disk_stat_add(disk
, ticks
[rw
], duration
);
594 disk_stat_add(disk
, sectors
[rw
], n_sect
);
595 disk_stat_add(disk
, io_ticks
, duration
);
596 n
= (buf
->flags
& BUFFL_FAIL
) ? -EIO
: 0;
597 bio_endio(buf
->bio
, buf
->bio
->bi_size
, n
);
598 mempool_free(buf
, d
->bufpool
);
607 d
->sendq_hd
= d
->sendq_tl
= NULL
;
609 spin_unlock_irqrestore(&d
->lock
, flags
);
614 aoecmd_cfg(ushort aoemajor
, unsigned char aoeminor
)
618 sl
= aoecmd_cfg_pkts(aoemajor
, aoeminor
, NULL
);
624 * Since we only call this in one place (and it only prepares one frame)
625 * we just return the skb. Usually we'd chain it up to the aoedev sendq.
627 static struct sk_buff
*
628 aoecmd_ata_id(struct aoedev
*d
)
631 struct aoe_atahdr
*ah
;
635 f
= getframe(d
, FREETAG
);
637 printk(KERN_CRIT
"aoe: aoecmd_ata_id: can't get a frame. "
638 "This shouldn't happen.\n");
642 /* initialize the headers & frame */
644 h
= (struct aoe_hdr
*) skb
->mac
.raw
;
645 ah
= (struct aoe_atahdr
*) (h
+1);
646 skb
->len
= sizeof *h
+ sizeof *ah
;
647 memset(h
, 0, skb
->len
);
648 f
->tag
= aoehdr_atainit(d
, h
);
651 /* set up ata header */
653 ah
->cmdstat
= WIN_IDENTIFY
;
659 d
->rttavg
= MAXTIMER
;
660 d
->timer
.function
= rexmit_timer
;
666 aoecmd_cfg_rsp(struct sk_buff
*skb
)
670 struct aoe_cfghdr
*ch
;
671 ulong flags
, sysminor
, aoemajor
;
673 enum { MAXFRAMES
= 16 };
676 h
= (struct aoe_hdr
*) skb
->mac
.raw
;
677 ch
= (struct aoe_cfghdr
*) (h
+1);
680 * Enough people have their dip switches set backwards to
681 * warrant a loud message for this special case.
683 aoemajor
= be16_to_cpu(h
->major
);
684 if (aoemajor
== 0xfff) {
685 printk(KERN_CRIT
"aoe: aoecmd_cfg_rsp: Warning: shelf "
686 "address is all ones. Check shelf dip switches\n");
690 sysminor
= SYSMINOR(aoemajor
, h
->minor
);
691 if (sysminor
* AOE_PARTITIONS
+ AOE_PARTITIONS
> MINORMASK
) {
693 "aoe: e%ld.%d: minor number too large\n",
694 aoemajor
, (int) h
->minor
);
698 n
= be16_to_cpu(ch
->bufcnt
);
699 if (n
> MAXFRAMES
) /* keep it reasonable */
702 d
= aoedev_by_sysminor_m(sysminor
, n
);
704 printk(KERN_INFO
"aoe: aoecmd_cfg_rsp: device sysminor_m failure\n");
708 spin_lock_irqsave(&d
->lock
, flags
);
710 /* permit device to migrate mac and network interface */
712 memcpy(d
->addr
, h
->src
, sizeof d
->addr
);
713 if (!(d
->flags
& DEVFL_MAXBCNT
)) {
715 n
-= sizeof (struct aoe_hdr
) + sizeof (struct aoe_atahdr
);
719 d
->maxbcnt
= n
? n
* 512 : DEFAULTBCNT
;
722 /* don't change users' perspective */
723 if (d
->nopen
&& !(d
->flags
& DEVFL_PAUSE
)) {
724 spin_unlock_irqrestore(&d
->lock
, flags
);
727 d
->flags
|= DEVFL_PAUSE
; /* force pause */
728 d
->fw_ver
= be16_to_cpu(ch
->fwver
);
730 /* check for already outstanding ataid */
731 sl
= aoedev_isbusy(d
) == 0 ? aoecmd_ata_id(d
) : NULL
;
733 spin_unlock_irqrestore(&d
->lock
, flags
);