1 /* Copyright (c) 2013 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/blk-mq.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
*);
25 static int count_targets(struct aoedev
*d
, int *untainted
);
27 static struct buf
*nextbuf(struct aoedev
*);
29 static int aoe_deadsecs
= 60 * 3;
30 module_param(aoe_deadsecs
, int, 0644);
31 MODULE_PARM_DESC(aoe_deadsecs
, "After aoe_deadsecs seconds, give up and fail dev.");
33 static int aoe_maxout
= 64;
34 module_param(aoe_maxout
, int, 0644);
35 MODULE_PARM_DESC(aoe_maxout
,
36 "Only aoe_maxout outstanding packets for every MAC on eX.Y.");
38 /* The number of online cpus during module initialization gives us a
39 * convenient heuristic cap on the parallelism used for ktio threads
40 * doing I/O completion. It is not important that the cap equal the
41 * actual number of running CPUs at any given time, but because of CPU
42 * hotplug, we take care to use ncpus instead of using
43 * num_online_cpus() after module initialization.
47 /* mutex lock used for synchronization while thread spawning */
48 static DEFINE_MUTEX(ktio_spawn_lock
);
50 static wait_queue_head_t
*ktiowq
;
51 static struct ktstate
*kts
;
53 /* io completion queue */
55 struct list_head head
;
58 static struct iocq_ktio
*iocq
;
60 static struct page
*empty_page
;
62 static struct sk_buff
*
67 skb
= alloc_skb(len
+ MAX_HEADER
, GFP_ATOMIC
);
69 skb_reserve(skb
, MAX_HEADER
);
70 skb_reset_mac_header(skb
);
71 skb_reset_network_header(skb
);
72 skb
->protocol
= __constant_htons(ETH_P_AOE
);
73 skb_checksum_none_assert(skb
);
79 getframe_deferred(struct aoedev
*d
, u32 tag
)
81 struct list_head
*head
, *pos
, *nx
;
85 list_for_each_safe(pos
, nx
, head
) {
86 f
= list_entry(pos
, struct frame
, head
);
96 getframe(struct aoedev
*d
, u32 tag
)
99 struct list_head
*head
, *pos
, *nx
;
103 head
= &d
->factive
[n
];
104 list_for_each_safe(pos
, nx
, head
) {
105 f
= list_entry(pos
, struct frame
, head
);
115 * Leave the top bit clear so we have tagspace for userland.
116 * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
117 * This driver reserves tag -1 to mean "unused frame."
120 newtag(struct aoedev
*d
)
124 n
= jiffies
& 0xffff;
125 return n
|= (++d
->lasttag
& 0x7fff) << 16;
129 aoehdr_atainit(struct aoedev
*d
, struct aoetgt
*t
, struct aoe_hdr
*h
)
131 u32 host_tag
= newtag(d
);
133 memcpy(h
->src
, t
->ifp
->nd
->dev_addr
, sizeof h
->src
);
134 memcpy(h
->dst
, t
->addr
, sizeof h
->dst
);
135 h
->type
= __constant_cpu_to_be16(ETH_P_AOE
);
137 h
->major
= cpu_to_be16(d
->aoemajor
);
138 h
->minor
= d
->aoeminor
;
140 h
->tag
= cpu_to_be32(host_tag
);
146 put_lba(struct aoe_atahdr
*ah
, sector_t lba
)
149 ah
->lba1
= lba
>>= 8;
150 ah
->lba2
= lba
>>= 8;
151 ah
->lba3
= lba
>>= 8;
152 ah
->lba4
= lba
>>= 8;
153 ah
->lba5
= lba
>>= 8;
156 static struct aoeif
*
157 ifrotate(struct aoetgt
*t
)
163 if (ifp
>= &t
->ifs
[NAOEIFS
] || ifp
->nd
== NULL
)
171 skb_pool_put(struct aoedev
*d
, struct sk_buff
*skb
)
173 __skb_queue_tail(&d
->skbpool
, skb
);
176 static struct sk_buff
*
177 skb_pool_get(struct aoedev
*d
)
179 struct sk_buff
*skb
= skb_peek(&d
->skbpool
);
181 if (skb
&& atomic_read(&skb_shinfo(skb
)->dataref
) == 1) {
182 __skb_unlink(skb
, &d
->skbpool
);
185 if (skb_queue_len(&d
->skbpool
) < NSKBPOOLMAX
&&
186 (skb
= new_skb(ETH_ZLEN
)))
193 aoe_freetframe(struct frame
*f
)
199 memset(&f
->iter
, 0, sizeof(f
->iter
));
202 list_add(&f
->head
, &t
->ffree
);
205 static struct frame
*
206 newtframe(struct aoedev
*d
, struct aoetgt
*t
)
210 struct list_head
*pos
;
212 if (list_empty(&t
->ffree
)) {
213 if (t
->falloc
>= NSKBPOOLMAX
*2)
215 f
= kcalloc(1, sizeof(*f
), GFP_ATOMIC
);
223 f
= list_entry(pos
, struct frame
, head
);
228 f
->skb
= skb
= new_skb(ETH_ZLEN
);
230 bail
: aoe_freetframe(f
);
235 if (atomic_read(&skb_shinfo(skb
)->dataref
) != 1) {
236 skb
= skb_pool_get(d
);
239 skb_pool_put(d
, f
->skb
);
243 skb
->truesize
-= skb
->data_len
;
244 skb_shinfo(skb
)->nr_frags
= skb
->data_len
= 0;
249 static struct frame
*
250 newframe(struct aoedev
*d
)
253 struct aoetgt
*t
, **tt
;
258 if (!d
->targets
|| !d
->targets
[0]) {
259 printk(KERN_ERR
"aoe: NULL TARGETS!\n");
262 tt
= d
->tgt
; /* last used target */
263 for (use_tainted
= 0, has_untainted
= 0;;) {
265 if (tt
>= &d
->targets
[d
->ntargets
] || !*tt
)
272 if (t
->nout
< t
->maxout
273 && (use_tainted
|| !t
->taint
)
282 if (tt
== d
->tgt
) { /* we've looped and found nada */
283 if (!use_tainted
&& !has_untainted
)
291 d
->flags
|= DEVFL_KICKME
;
297 skb_fillup(struct sk_buff
*skb
, struct bio
*bio
, struct bvec_iter iter
)
302 __bio_for_each_segment(bv
, bio
, iter
, iter
)
303 skb_fill_page_desc(skb
, frag
++, bv
.bv_page
,
304 bv
.bv_offset
, bv
.bv_len
);
308 fhash(struct frame
*f
)
310 struct aoedev
*d
= f
->t
->d
;
313 n
= f
->tag
% NFACTIVE
;
314 list_add_tail(&f
->head
, &d
->factive
[n
]);
318 ata_rw_frameinit(struct frame
*f
)
322 struct aoe_atahdr
*ah
;
324 char writebit
, extbit
;
327 h
= (struct aoe_hdr
*) skb_mac_header(skb
);
328 ah
= (struct aoe_atahdr
*) (h
+ 1);
329 skb_put(skb
, sizeof(*h
) + sizeof(*ah
));
330 memset(h
, 0, skb
->len
);
336 f
->tag
= aoehdr_atainit(t
->d
, t
, h
);
342 /* set up ata header */
343 ah
->scnt
= f
->iter
.bi_size
>> 9;
344 put_lba(ah
, f
->iter
.bi_sector
);
345 if (t
->d
->flags
& DEVFL_EXT
) {
346 ah
->aflags
|= AOEAFL_EXT
;
350 ah
->lba3
|= 0xe0; /* LBA bit + obsolete 0xa0 */
352 if (f
->buf
&& bio_data_dir(f
->buf
->bio
) == WRITE
) {
353 skb_fillup(skb
, f
->buf
->bio
, f
->iter
);
354 ah
->aflags
|= AOEAFL_WRITE
;
355 skb
->len
+= f
->iter
.bi_size
;
356 skb
->data_len
= f
->iter
.bi_size
;
357 skb
->truesize
+= f
->iter
.bi_size
;
364 ah
->cmdstat
= ATA_CMD_PIO_READ
| writebit
| extbit
;
365 skb
->dev
= t
->ifp
->nd
;
369 aoecmd_ata_rw(struct aoedev
*d
)
374 struct sk_buff_head queue
;
383 /* initialize the headers & frame */
386 f
->iter
.bi_size
= min_t(unsigned long,
387 d
->maxbcnt
?: DEFAULTBCNT
,
389 bio_advance_iter(buf
->bio
, &buf
->iter
, f
->iter
.bi_size
);
391 if (!buf
->iter
.bi_size
)
394 /* mark all tracking fields and load out */
395 buf
->nframesout
+= 1;
399 skb
= skb_clone(f
->skb
, GFP_ATOMIC
);
401 f
->sent
= ktime_get();
402 __skb_queue_head_init(&queue
);
403 __skb_queue_tail(&queue
, skb
);
409 /* some callers cannot sleep, and they can call this function,
410 * transmitting the packets later, when interrupts are on
413 aoecmd_cfg_pkts(ushort aoemajor
, unsigned char aoeminor
, struct sk_buff_head
*queue
)
416 struct aoe_cfghdr
*ch
;
418 struct net_device
*ifp
;
421 for_each_netdev_rcu(&init_net
, ifp
) {
423 if (!is_aoe_netif(ifp
))
426 skb
= new_skb(sizeof *h
+ sizeof *ch
);
428 printk(KERN_INFO
"aoe: skb alloc failure\n");
431 skb_put(skb
, sizeof *h
+ sizeof *ch
);
433 __skb_queue_tail(queue
, skb
);
434 h
= (struct aoe_hdr
*) skb_mac_header(skb
);
435 memset(h
, 0, sizeof *h
+ sizeof *ch
);
437 memset(h
->dst
, 0xff, sizeof h
->dst
);
438 memcpy(h
->src
, ifp
->dev_addr
, sizeof h
->src
);
439 h
->type
= __constant_cpu_to_be16(ETH_P_AOE
);
441 h
->major
= cpu_to_be16(aoemajor
);
452 resend(struct aoedev
*d
, struct frame
*f
)
455 struct sk_buff_head queue
;
464 if (ifrotate(t
) == NULL
) {
465 /* probably can't happen, but set it up to fail anyway */
466 pr_info("aoe: resend: no interfaces to rotate to.\n");
470 h
= (struct aoe_hdr
*) skb_mac_header(skb
);
472 if (!(f
->flags
& FFL_PROBE
)) {
473 snprintf(buf
, sizeof(buf
),
474 "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
475 "retransmit", d
->aoemajor
, d
->aoeminor
,
477 h
->src
, h
->dst
, t
->nout
);
483 h
->tag
= cpu_to_be32(n
);
484 memcpy(h
->dst
, t
->addr
, sizeof h
->dst
);
485 memcpy(h
->src
, t
->ifp
->nd
->dev_addr
, sizeof h
->src
);
487 skb
->dev
= t
->ifp
->nd
;
488 skb
= skb_clone(skb
, GFP_ATOMIC
);
491 f
->sent
= ktime_get();
492 __skb_queue_head_init(&queue
);
493 __skb_queue_tail(&queue
, skb
);
498 tsince_hr(struct frame
*f
)
500 u64 delta
= ktime_to_ns(ktime_sub(ktime_get(), f
->sent
));
502 /* delta is normally under 4.2 seconds, avoid 64-bit division */
503 if (likely(delta
<= UINT_MAX
))
504 return (u32
)delta
/ NSEC_PER_USEC
;
506 /* avoid overflow after 71 minutes */
507 if (delta
> ((u64
)INT_MAX
* NSEC_PER_USEC
))
510 return div_u64(delta
, NSEC_PER_USEC
);
518 n
= jiffies
& 0xffff;
522 return jiffies_to_usecs(n
+ 1);
525 static struct aoeif
*
526 getif(struct aoetgt
*t
, struct net_device
*nd
)
539 ejectif(struct aoetgt
*t
, struct aoeif
*ifp
)
542 struct net_device
*nd
;
546 e
= t
->ifs
+ NAOEIFS
- 1;
547 n
= (e
- ifp
) * sizeof *ifp
;
548 memmove(ifp
, ifp
+1, n
);
553 static struct frame
*
554 reassign_frame(struct frame
*f
)
559 nf
= newframe(f
->t
->d
);
572 nf
->waited_total
= f
->waited_total
;
580 probe(struct aoetgt
*t
)
585 struct sk_buff_head queue
;
592 pr_err("%s %pm for e%ld.%d: %s\n",
593 "aoe: cannot probe remote address",
595 (long) d
->aoemajor
, d
->aoeminor
,
596 "no frame available");
599 f
->flags
|= FFL_PROBE
;
601 f
->iter
.bi_size
= t
->d
->maxbcnt
? t
->d
->maxbcnt
: DEFAULTBCNT
;
604 for (frag
= 0, n
= f
->iter
.bi_size
; n
> 0; ++frag
, n
-= m
) {
609 skb_fill_page_desc(skb
, frag
, empty_page
, 0, m
);
611 skb
->len
+= f
->iter
.bi_size
;
612 skb
->data_len
= f
->iter
.bi_size
;
613 skb
->truesize
+= f
->iter
.bi_size
;
615 skb
= skb_clone(f
->skb
, GFP_ATOMIC
);
617 f
->sent
= ktime_get();
618 __skb_queue_head_init(&queue
);
619 __skb_queue_tail(&queue
, skb
);
625 rto(struct aoedev
*d
)
629 t
= 2 * d
->rttavg
>> RTTSCALE
;
630 t
+= 8 * d
->rttdev
>> RTTDSCALE
;
638 rexmit_deferred(struct aoedev
*d
)
643 struct list_head
*pos
, *nx
, *head
;
647 count_targets(d
, &untainted
);
650 list_for_each_safe(pos
, nx
, head
) {
651 f
= list_entry(pos
, struct frame
, head
);
654 if (!(f
->flags
& FFL_PROBE
)) {
655 nf
= reassign_frame(f
);
657 if (t
->nout_probes
== 0
662 list_replace(&f
->head
, &nf
->head
);
668 } else if (untainted
< 1) {
669 /* don't probe w/o other untainted aoetgts */
671 } else if (tsince_hr(f
) < t
->taint
* rto(d
)) {
672 /* reprobe slowly when taint is high */
675 } else if (f
->flags
& FFL_PROBE
) {
676 stop_probe
: /* don't probe untainted aoetgts */
679 /* leaving d->kicked, because this is routine */
680 f
->t
->d
->flags
|= DEVFL_KICKME
;
683 if (t
->nout
>= t
->maxout
)
687 if (f
->flags
& FFL_PROBE
)
689 since
= tsince_hr(f
);
691 f
->waited_total
+= since
;
696 /* An aoetgt accumulates demerits quickly, and successful
697 * probing redeems the aoetgt slowly.
700 scorn(struct aoetgt
*t
)
705 t
->taint
+= t
->taint
* 2;
708 if (t
->taint
> MAX_TAINT
)
709 t
->taint
= MAX_TAINT
;
713 count_targets(struct aoedev
*d
, int *untainted
)
717 for (i
= good
= 0; i
< d
->ntargets
&& d
->targets
[i
]; ++i
)
718 if (d
->targets
[i
]->taint
== 0)
727 rexmit_timer(struct timer_list
*timer
)
733 struct list_head
*head
, *pos
, *nx
;
735 register long timeout
;
738 int utgts
; /* number of aoetgt descriptors (not slots) */
741 d
= from_timer(d
, timer
, timer
);
743 spin_lock_irqsave(&d
->lock
, flags
);
745 /* timeout based on observed timings and variations */
748 utgts
= count_targets(d
, NULL
);
750 if (d
->flags
& DEVFL_TKILL
) {
751 spin_unlock_irqrestore(&d
->lock
, flags
);
755 /* collect all frames to rexmit into flist */
756 for (i
= 0; i
< NFACTIVE
; i
++) {
757 head
= &d
->factive
[i
];
758 list_for_each_safe(pos
, nx
, head
) {
759 f
= list_entry(pos
, struct frame
, head
);
760 if (tsince_hr(f
) < timeout
)
761 break; /* end of expired frames */
762 /* move to flist for later processing */
763 list_move_tail(pos
, &flist
);
767 /* process expired frames */
768 while (!list_empty(&flist
)) {
770 f
= list_entry(pos
, struct frame
, head
);
771 since
= tsince_hr(f
);
772 n
= f
->waited_total
+ since
;
776 && !(f
->flags
& FFL_PROBE
)) {
777 /* Waited too long. Device failure.
778 * Hang all frames on first hash bucket for downdev
781 list_splice(&flist
, &d
->factive
[0]);
787 n
= f
->waited
+ since
;
789 if (aoe_deadsecs
&& utgts
> 0
790 && (n
> aoe_deadsecs
/ utgts
|| n
> HARD_SCORN_SECS
))
791 scorn(t
); /* avoid this target */
793 if (t
->maxout
!= 1) {
794 t
->ssthresh
= t
->maxout
/ 2;
798 if (f
->flags
& FFL_PROBE
) {
801 ifp
= getif(t
, f
->skb
->dev
);
802 if (ifp
&& ++ifp
->lost
> (t
->nframes
<< 1)
803 && (ifp
!= t
->ifs
|| t
->ifs
[1].nd
)) {
808 list_move_tail(pos
, &d
->rexmitq
);
814 if ((d
->flags
& DEVFL_KICKME
) && d
->blkq
) {
815 d
->flags
&= ~DEVFL_KICKME
;
816 blk_mq_run_hw_queues(d
->blkq
, true);
819 d
->timer
.expires
= jiffies
+ TIMERTICK
;
820 add_timer(&d
->timer
);
822 spin_unlock_irqrestore(&d
->lock
, flags
);
826 bufinit(struct buf
*buf
, struct request
*rq
, struct bio
*bio
)
828 memset(buf
, 0, sizeof(*buf
));
831 buf
->iter
= bio
->bi_iter
;
835 nextbuf(struct aoedev
*d
)
838 struct request_queue
*q
;
845 return NULL
; /* initializing */
850 rq
= list_first_entry_or_null(&d
->rq_list
, struct request
,
854 list_del_init(&rq
->queuelist
);
855 blk_mq_start_request(rq
);
857 d
->ip
.nxbio
= rq
->bio
;
859 req
= blk_mq_rq_to_pdu(rq
);
861 __rq_for_each_bio(bio
, rq
)
864 buf
= mempool_alloc(d
->bufpool
, GFP_ATOMIC
);
866 pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
870 bufinit(buf
, rq
, bio
);
875 return d
->ip
.buf
= buf
;
878 /* enters with d->lock held */
880 aoecmd_work(struct aoedev
*d
)
883 while (aoecmd_ata_rw(d
))
887 /* this function performs work that has been deferred until sleeping is OK
890 aoecmd_sleepwork(struct work_struct
*work
)
892 struct aoedev
*d
= container_of(work
, struct aoedev
, work
);
893 struct block_device
*bd
;
896 if (d
->flags
& DEVFL_GDALLOC
)
899 if (d
->flags
& DEVFL_NEWSIZE
) {
900 ssize
= get_capacity(d
->gd
);
901 bd
= bdget_disk(d
->gd
, 0);
903 inode_lock(bd
->bd_inode
);
904 i_size_write(bd
->bd_inode
, (loff_t
)ssize
<<9);
905 inode_unlock(bd
->bd_inode
);
908 spin_lock_irq(&d
->lock
);
909 d
->flags
|= DEVFL_UP
;
910 d
->flags
&= ~DEVFL_NEWSIZE
;
911 spin_unlock_irq(&d
->lock
);
916 ata_ident_fixstring(u16
*id
, int ns
)
922 *id
++ = s
>> 8 | s
<< 8;
927 ataid_complete(struct aoedev
*d
, struct aoetgt
*t
, unsigned char *id
)
932 /* word 83: command set supported */
933 n
= get_unaligned_le16(&id
[83 << 1]);
935 /* word 86: command set/feature enabled */
936 n
|= get_unaligned_le16(&id
[86 << 1]);
938 if (n
& (1<<10)) { /* bit 10: LBA 48 */
939 d
->flags
|= DEVFL_EXT
;
941 /* word 100: number lba48 sectors */
942 ssize
= get_unaligned_le64(&id
[100 << 1]);
944 /* set as in ide-disk.c:init_idedisk_capacity */
945 d
->geo
.cylinders
= ssize
;
946 d
->geo
.cylinders
/= (255 * 63);
950 d
->flags
&= ~DEVFL_EXT
;
952 /* number lba28 sectors */
953 ssize
= get_unaligned_le32(&id
[60 << 1]);
955 /* NOTE: obsolete in ATA 6 */
956 d
->geo
.cylinders
= get_unaligned_le16(&id
[54 << 1]);
957 d
->geo
.heads
= get_unaligned_le16(&id
[55 << 1]);
958 d
->geo
.sectors
= get_unaligned_le16(&id
[56 << 1]);
961 ata_ident_fixstring((u16
*) &id
[10<<1], 10); /* serial */
962 ata_ident_fixstring((u16
*) &id
[23<<1], 4); /* firmware */
963 ata_ident_fixstring((u16
*) &id
[27<<1], 20); /* model */
964 memcpy(d
->ident
, id
, sizeof(d
->ident
));
966 if (d
->ssize
!= ssize
)
968 "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
970 d
->aoemajor
, d
->aoeminor
,
971 d
->fw_ver
, (long long)ssize
);
974 if (d
->flags
& (DEVFL_GDALLOC
|DEVFL_NEWSIZE
))
977 set_capacity(d
->gd
, ssize
);
978 d
->flags
|= DEVFL_NEWSIZE
;
980 d
->flags
|= DEVFL_GDALLOC
;
981 schedule_work(&d
->work
);
985 calc_rttavg(struct aoedev
*d
, struct aoetgt
*t
, int rtt
)
991 /* cf. Congestion Avoidance and Control, Jacobson & Karels, 1988 */
992 n
-= d
->rttavg
>> RTTSCALE
;
996 n
-= d
->rttdev
>> RTTDSCALE
;
999 if (!t
|| t
->maxout
>= t
->nframes
)
1001 if (t
->maxout
< t
->ssthresh
)
1003 else if (t
->nout
== t
->maxout
&& t
->next_cwnd
-- == 0) {
1005 t
->next_cwnd
= t
->maxout
;
1009 static struct aoetgt
*
1010 gettgt(struct aoedev
*d
, char *addr
)
1012 struct aoetgt
**t
, **e
;
1015 e
= t
+ d
->ntargets
;
1016 for (; t
< e
&& *t
; t
++)
1017 if (memcmp((*t
)->addr
, addr
, sizeof((*t
)->addr
)) == 0)
1023 bvcpy(struct sk_buff
*skb
, struct bio
*bio
, struct bvec_iter iter
, long cnt
)
1030 __bio_for_each_segment(bv
, bio
, iter
, iter
) {
1031 char *p
= kmap_atomic(bv
.bv_page
) + bv
.bv_offset
;
1032 skb_copy_bits(skb
, soff
, p
, bv
.bv_len
);
1039 aoe_end_request(struct aoedev
*d
, struct request
*rq
, int fastfail
)
1043 struct request_queue
*q
;
1044 blk_status_t err
= BLK_STS_OK
;
1051 bok
= !fastfail
&& !bio
->bi_status
;
1053 err
= BLK_STS_IOERR
;
1054 } while (blk_update_request(rq
, bok
? BLK_STS_OK
: BLK_STS_IOERR
, bio
->bi_iter
.bi_size
));
1056 __blk_mq_end_request(rq
, err
);
1058 /* cf. http://lkml.org/lkml/2006/10/31/28 */
1060 blk_mq_run_hw_queues(q
, true);
1064 aoe_end_buf(struct aoedev
*d
, struct buf
*buf
)
1066 struct request
*rq
= buf
->rq
;
1067 struct aoe_req
*req
= blk_mq_rq_to_pdu(rq
);
1069 if (buf
== d
->ip
.buf
)
1071 mempool_free(buf
, d
->bufpool
);
1072 if (--req
->nr_bios
== 0)
1073 aoe_end_request(d
, rq
, 0);
1077 ktiocomplete(struct frame
*f
)
1079 struct aoe_hdr
*hin
, *hout
;
1080 struct aoe_atahdr
*ahin
, *ahout
;
1082 struct sk_buff
*skb
;
1096 if (f
->flags
& FFL_PROBE
)
1098 if (!skb
) /* just fail the buf. */
1101 hout
= (struct aoe_hdr
*) skb_mac_header(f
->skb
);
1102 ahout
= (struct aoe_atahdr
*) (hout
+1);
1104 hin
= (struct aoe_hdr
*) skb
->data
;
1105 skb_pull(skb
, sizeof(*hin
));
1106 ahin
= (struct aoe_atahdr
*) skb
->data
;
1107 skb_pull(skb
, sizeof(*ahin
));
1108 if (ahin
->cmdstat
& 0xa9) { /* these bits cleared on success */
1109 pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
1110 ahout
->cmdstat
, ahin
->cmdstat
,
1111 d
->aoemajor
, d
->aoeminor
);
1113 buf
->bio
->bi_status
= BLK_STS_IOERR
;
1117 n
= ahout
->scnt
<< 9;
1118 switch (ahout
->cmdstat
) {
1119 case ATA_CMD_PIO_READ
:
1120 case ATA_CMD_PIO_READ_EXT
:
1122 pr_err("%s e%ld.%d. skb->len=%d need=%ld\n",
1123 "aoe: runt data size in read from",
1124 (long) d
->aoemajor
, d
->aoeminor
,
1126 buf
->bio
->bi_status
= BLK_STS_IOERR
;
1129 if (n
> f
->iter
.bi_size
) {
1130 pr_err_ratelimited("%s e%ld.%d. bytes=%ld need=%u\n",
1131 "aoe: too-large data size in read from",
1132 (long) d
->aoemajor
, d
->aoeminor
,
1133 n
, f
->iter
.bi_size
);
1134 buf
->bio
->bi_status
= BLK_STS_IOERR
;
1137 bvcpy(skb
, f
->buf
->bio
, f
->iter
, n
);
1139 case ATA_CMD_PIO_WRITE
:
1140 case ATA_CMD_PIO_WRITE_EXT
:
1141 spin_lock_irq(&d
->lock
);
1142 ifp
= getif(t
, skb
->dev
);
1145 spin_unlock_irq(&d
->lock
);
1147 case ATA_CMD_ID_ATA
:
1148 if (skb
->len
< 512) {
1149 pr_info("%s e%ld.%d. skb->len=%d need=512\n",
1150 "aoe: runt data size in ataid from",
1151 (long) d
->aoemajor
, d
->aoeminor
,
1155 if (skb_linearize(skb
))
1157 spin_lock_irq(&d
->lock
);
1158 ataid_complete(d
, t
, skb
->data
);
1159 spin_unlock_irq(&d
->lock
);
1162 pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
1164 be16_to_cpu(get_unaligned(&hin
->major
)),
1168 spin_lock_irq(&d
->lock
);
1171 && t
->nout_probes
== 0) {
1172 count_targets(d
, &untainted
);
1173 if (untainted
> 0) {
1181 if (buf
&& --buf
->nframesout
== 0 && buf
->iter
.bi_size
== 0)
1182 aoe_end_buf(d
, buf
);
1184 spin_unlock_irq(&d
->lock
);
1189 /* Enters with iocq.lock held.
1190 * Returns true iff responses needing processing remain.
1196 struct list_head
*pos
;
1200 for (i
= 0; ; ++i
) {
1203 if (list_empty(&iocq
[id
].head
))
1205 pos
= iocq
[id
].head
.next
;
1207 f
= list_entry(pos
, struct frame
, head
);
1208 spin_unlock_irq(&iocq
[id
].lock
);
1211 /* Figure out if extra threads are required. */
1212 actual_id
= f
->t
->d
->aoeminor
% ncpus
;
1214 if (!kts
[actual_id
].active
) {
1216 mutex_lock(&ktio_spawn_lock
);
1217 if (!kts
[actual_id
].active
1218 && aoe_ktstart(&kts
[actual_id
]) == 0)
1219 kts
[actual_id
].active
= 1;
1220 mutex_unlock(&ktio_spawn_lock
);
1222 spin_lock_irq(&iocq
[id
].lock
);
1230 DECLARE_WAITQUEUE(wait
, current
);
1234 current
->flags
|= PF_NOFREEZE
;
1235 set_user_nice(current
, -10);
1236 complete(&k
->rendez
); /* tell spawner we're running */
1238 spin_lock_irq(k
->lock
);
1239 more
= k
->fn(k
->id
);
1241 add_wait_queue(k
->waitq
, &wait
);
1242 __set_current_state(TASK_INTERRUPTIBLE
);
1244 spin_unlock_irq(k
->lock
);
1247 remove_wait_queue(k
->waitq
, &wait
);
1250 } while (!kthread_should_stop());
1251 complete(&k
->rendez
); /* tell spawner we're stopping */
1256 aoe_ktstop(struct ktstate
*k
)
1258 kthread_stop(k
->task
);
1259 wait_for_completion(&k
->rendez
);
1263 aoe_ktstart(struct ktstate
*k
)
1265 struct task_struct
*task
;
1267 init_completion(&k
->rendez
);
1268 task
= kthread_run(kthread
, k
, "%s", k
->name
);
1269 if (task
== NULL
|| IS_ERR(task
))
1272 wait_for_completion(&k
->rendez
); /* allow kthread to start */
1273 init_completion(&k
->rendez
); /* for waiting for exit later */
1277 /* pass it off to kthreads for processing */
1279 ktcomplete(struct frame
*f
, struct sk_buff
*skb
)
1285 id
= f
->t
->d
->aoeminor
% ncpus
;
1286 spin_lock_irqsave(&iocq
[id
].lock
, flags
);
1287 if (!kts
[id
].active
) {
1288 spin_unlock_irqrestore(&iocq
[id
].lock
, flags
);
1289 /* The thread with id has not been spawned yet,
1290 * so delegate the work to the main thread and
1291 * try spawning a new thread.
1294 spin_lock_irqsave(&iocq
[id
].lock
, flags
);
1296 list_add_tail(&f
->head
, &iocq
[id
].head
);
1297 spin_unlock_irqrestore(&iocq
[id
].lock
, flags
);
1298 wake_up(&ktiowq
[id
]);
1302 aoecmd_ata_rsp(struct sk_buff
*skb
)
1312 h
= (struct aoe_hdr
*) skb
->data
;
1313 aoemajor
= be16_to_cpu(get_unaligned(&h
->major
));
1314 d
= aoedev_by_aoeaddr(aoemajor
, h
->minor
, 0);
1316 snprintf(ebuf
, sizeof ebuf
, "aoecmd_ata_rsp: ata response "
1317 "for unknown device %d.%d\n",
1318 aoemajor
, h
->minor
);
1323 spin_lock_irqsave(&d
->lock
, flags
);
1325 n
= be32_to_cpu(get_unaligned(&h
->tag
));
1328 calc_rttavg(d
, f
->t
, tsince_hr(f
));
1330 if (f
->flags
& FFL_PROBE
)
1331 f
->t
->nout_probes
--;
1333 f
= getframe_deferred(d
, n
);
1335 calc_rttavg(d
, NULL
, tsince_hr(f
));
1337 calc_rttavg(d
, NULL
, tsince(n
));
1338 spin_unlock_irqrestore(&d
->lock
, flags
);
1340 snprintf(ebuf
, sizeof(ebuf
),
1341 "%15s e%d.%d tag=%08x@%08lx s=%pm d=%pm\n",
1343 get_unaligned_be16(&h
->major
),
1345 get_unaligned_be32(&h
->tag
),
1355 spin_unlock_irqrestore(&d
->lock
, flags
);
1360 * Note here that we do not perform an aoedev_put, as we are
1361 * leaving this reference for the ktio to release.
1367 aoecmd_cfg(ushort aoemajor
, unsigned char aoeminor
)
1369 struct sk_buff_head queue
;
1371 __skb_queue_head_init(&queue
);
1372 aoecmd_cfg_pkts(aoemajor
, aoeminor
, &queue
);
1373 aoenet_xmit(&queue
);
1377 aoecmd_ata_id(struct aoedev
*d
)
1380 struct aoe_atahdr
*ah
;
1382 struct sk_buff
*skb
;
1391 /* initialize the headers & frame */
1393 h
= (struct aoe_hdr
*) skb_mac_header(skb
);
1394 ah
= (struct aoe_atahdr
*) (h
+1);
1395 skb_put(skb
, sizeof *h
+ sizeof *ah
);
1396 memset(h
, 0, skb
->len
);
1397 f
->tag
= aoehdr_atainit(d
, t
, h
);
1401 f
->waited_total
= 0;
1403 /* set up ata header */
1405 ah
->cmdstat
= ATA_CMD_ID_ATA
;
1408 skb
->dev
= t
->ifp
->nd
;
1410 d
->rttavg
= RTTAVG_INIT
;
1411 d
->rttdev
= RTTDEV_INIT
;
1412 d
->timer
.function
= rexmit_timer
;
1414 skb
= skb_clone(skb
, GFP_ATOMIC
);
1416 f
->sent
= ktime_get();
1421 static struct aoetgt
**
1422 grow_targets(struct aoedev
*d
)
1429 tt
= kcalloc(newn
, sizeof(*d
->targets
), GFP_ATOMIC
);
1432 memmove(tt
, d
->targets
, sizeof(*d
->targets
) * oldn
);
1433 d
->tgt
= tt
+ (d
->tgt
- d
->targets
);
1438 return &d
->targets
[oldn
];
1441 static struct aoetgt
*
1442 addtgt(struct aoedev
*d
, char *addr
, ulong nframes
)
1444 struct aoetgt
*t
, **tt
, **te
;
1447 te
= tt
+ d
->ntargets
;
1448 for (; tt
< te
&& *tt
; tt
++)
1452 tt
= grow_targets(d
);
1456 t
= kzalloc(sizeof(*t
), GFP_ATOMIC
);
1459 t
->nframes
= nframes
;
1461 memcpy(t
->addr
, addr
, sizeof t
->addr
);
1464 t
->maxout
= t
->nframes
/ 2;
1465 INIT_LIST_HEAD(&t
->ffree
);
1469 pr_info("aoe: cannot allocate memory to add target\n");
1474 setdbcnt(struct aoedev
*d
)
1476 struct aoetgt
**t
, **e
;
1480 e
= t
+ d
->ntargets
;
1481 for (; t
< e
&& *t
; t
++)
1482 if (bcnt
== 0 || bcnt
> (*t
)->minbcnt
)
1483 bcnt
= (*t
)->minbcnt
;
1484 if (bcnt
!= d
->maxbcnt
) {
1486 pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
1487 d
->aoemajor
, d
->aoeminor
, bcnt
);
1492 setifbcnt(struct aoetgt
*t
, struct net_device
*nd
, int bcnt
)
1495 struct aoeif
*p
, *e
;
1502 for (; p
< e
; p
++) {
1504 break; /* end of the valid interfaces */
1506 p
->bcnt
= bcnt
; /* we're updating */
1508 } else if (minbcnt
> p
->bcnt
)
1509 minbcnt
= p
->bcnt
; /* find the min interface */
1513 pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
1520 t
->minbcnt
= minbcnt
;
1525 aoecmd_cfg_rsp(struct sk_buff
*skb
)
1529 struct aoe_cfghdr
*ch
;
1531 ulong flags
, aoemajor
;
1533 struct sk_buff_head queue
;
1537 h
= (struct aoe_hdr
*) skb_mac_header(skb
);
1538 ch
= (struct aoe_cfghdr
*) (h
+1);
1541 * Enough people have their dip switches set backwards to
1542 * warrant a loud message for this special case.
1544 aoemajor
= get_unaligned_be16(&h
->major
);
1545 if (aoemajor
== 0xfff) {
1546 printk(KERN_ERR
"aoe: Warning: shelf address is all ones. "
1547 "Check shelf dip switches.\n");
1550 if (aoemajor
== 0xffff) {
1551 pr_info("aoe: e%ld.%d: broadcast shelf number invalid\n",
1552 aoemajor
, (int) h
->minor
);
1555 if (h
->minor
== 0xff) {
1556 pr_info("aoe: e%ld.%d: broadcast slot number invalid\n",
1557 aoemajor
, (int) h
->minor
);
1561 n
= be16_to_cpu(ch
->bufcnt
);
1562 if (n
> aoe_maxout
) /* keep it reasonable */
1565 d
= aoedev_by_aoeaddr(aoemajor
, h
->minor
, 1);
1567 pr_info("aoe: device allocation failure\n");
1571 spin_lock_irqsave(&d
->lock
, flags
);
1573 t
= gettgt(d
, h
->src
);
1579 t
= addtgt(d
, h
->src
, n
);
1584 n
-= sizeof(struct aoe_hdr
) + sizeof(struct aoe_atahdr
);
1588 n
= n
? n
* 512 : DEFAULTBCNT
;
1589 setifbcnt(t
, skb
->dev
, n
);
1591 /* don't change users' perspective */
1592 if (d
->nopen
== 0) {
1593 d
->fw_ver
= be16_to_cpu(ch
->fwver
);
1594 sl
= aoecmd_ata_id(d
);
1597 spin_unlock_irqrestore(&d
->lock
, flags
);
1600 __skb_queue_head_init(&queue
);
1601 __skb_queue_tail(&queue
, sl
);
1602 aoenet_xmit(&queue
);
1607 aoecmd_wreset(struct aoetgt
*t
)
1610 t
->ssthresh
= t
->nframes
/ 2;
1611 t
->next_cwnd
= t
->nframes
;
1615 aoecmd_cleanslate(struct aoedev
*d
)
1617 struct aoetgt
**t
, **te
;
1619 d
->rttavg
= RTTAVG_INIT
;
1620 d
->rttdev
= RTTDEV_INIT
;
1624 te
= t
+ d
->ntargets
;
1625 for (; t
< te
&& *t
; t
++)
1630 aoe_failbuf(struct aoedev
*d
, struct buf
*buf
)
1634 buf
->iter
.bi_size
= 0;
1635 buf
->bio
->bi_status
= BLK_STS_IOERR
;
1636 if (buf
->nframesout
== 0)
1637 aoe_end_buf(d
, buf
);
1641 aoe_flush_iocq(void)
1645 for (i
= 0; i
< ncpus
; i
++) {
1647 aoe_flush_iocq_by_index(i
);
1652 aoe_flush_iocq_by_index(int id
)
1657 struct list_head
*pos
;
1658 struct sk_buff
*skb
;
1661 spin_lock_irqsave(&iocq
[id
].lock
, flags
);
1662 list_splice_init(&iocq
[id
].head
, &flist
);
1663 spin_unlock_irqrestore(&iocq
[id
].lock
, flags
);
1664 while (!list_empty(&flist
)) {
1667 f
= list_entry(pos
, struct frame
, head
);
1670 spin_lock_irqsave(&d
->lock
, flags
);
1672 f
->buf
->nframesout
--;
1673 aoe_failbuf(d
, f
->buf
);
1676 spin_unlock_irqrestore(&d
->lock
, flags
);
1689 /* get_zeroed_page returns page with ref count 1 */
1690 p
= (void *) get_zeroed_page(GFP_KERNEL
);
1693 empty_page
= virt_to_page(p
);
1695 ncpus
= num_online_cpus();
1697 iocq
= kcalloc(ncpus
, sizeof(struct iocq_ktio
), GFP_KERNEL
);
1701 kts
= kcalloc(ncpus
, sizeof(struct ktstate
), GFP_KERNEL
);
1707 ktiowq
= kcalloc(ncpus
, sizeof(wait_queue_head_t
), GFP_KERNEL
);
1713 mutex_init(&ktio_spawn_lock
);
1715 for (i
= 0; i
< ncpus
; i
++) {
1716 INIT_LIST_HEAD(&iocq
[i
].head
);
1717 spin_lock_init(&iocq
[i
].lock
);
1718 init_waitqueue_head(&ktiowq
[i
]);
1719 snprintf(kts
[i
].name
, sizeof(kts
[i
].name
), "aoe_ktio%d", i
);
1721 kts
[i
].waitq
= &ktiowq
[i
];
1722 kts
[i
].lock
= &iocq
[i
].lock
;
1727 if (aoe_ktstart(&kts
[0])) {
1748 for (i
= 0; i
< ncpus
; i
++)
1750 aoe_ktstop(&kts
[i
]);
1754 /* Free up the iocq and thread speicific configuration
1755 * allocated during startup.
1761 free_page((unsigned long) page_address(empty_page
));