1 /* Copyright (c) 2006 Coraid, Inc. See COPYING for GPL terms. */
4 * AoE device utility functions; maintains device list.
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
9 #include <linux/netdevice.h>
12 static struct aoedev
*devlist
;
13 static spinlock_t devlist_lock
;
16 aoedev_isbusy(struct aoedev
*d
)
23 if (f
->tag
!= FREETAG
)
31 aoedev_by_aoeaddr(int maj
, int min
)
36 spin_lock_irqsave(&devlist_lock
, flags
);
38 for (d
=devlist
; d
; d
=d
->next
)
39 if (d
->aoemajor
== maj
&& d
->aoeminor
== min
)
42 spin_unlock_irqrestore(&devlist_lock
, flags
);
51 d
= (struct aoedev
*)vp
;
52 if (d
->flags
& DEVFL_TKILL
)
54 d
->timer
.expires
= jiffies
+ HZ
;
58 /* called with devlist lock held */
59 static struct aoedev
*
60 aoedev_newdev(ulong nframes
)
65 d
= kzalloc(sizeof *d
, GFP_ATOMIC
);
66 f
= kcalloc(nframes
, sizeof *f
, GFP_ATOMIC
);
74 f
->skb
= new_skb(ETH_ZLEN
);
80 while (f
> d
->frames
) {
82 dev_kfree_skb(f
->skb
);
91 INIT_WORK(&d
->work
, aoecmd_sleepwork
);
92 spin_lock_init(&d
->lock
);
93 init_timer(&d
->timer
);
94 d
->timer
.data
= (ulong
) d
;
95 d
->timer
.function
= dummy_timer
;
96 d
->timer
.expires
= jiffies
+ HZ
;
98 d
->bufpool
= NULL
; /* defer to aoeblk_gdalloc */
99 INIT_LIST_HEAD(&d
->bufq
);
107 aoedev_downdev(struct aoedev
*d
)
115 for (; f
<e
; f
->tag
= FREETAG
, f
->buf
= NULL
, f
++) {
116 if (f
->tag
== FREETAG
|| f
->buf
== NULL
)
120 if (--buf
->nframesout
== 0) {
121 mempool_free(buf
, d
->bufpool
);
122 bio_endio(bio
, -EIO
);
124 skb_shinfo(f
->skb
)->nr_frags
= f
->skb
->data_len
= 0;
128 while (!list_empty(&d
->bufq
)) {
129 buf
= container_of(d
->bufq
.next
, struct buf
, bufs
);
130 list_del(d
->bufq
.next
);
132 mempool_free(buf
, d
->bufpool
);
133 bio_endio(bio
, -EIO
);
139 d
->flags
&= ~(DEVFL_UP
| DEVFL_PAUSE
);
142 /* find it or malloc it */
144 aoedev_by_sysminor_m(ulong sysminor
, ulong bufcnt
)
149 spin_lock_irqsave(&devlist_lock
, flags
);
151 for (d
=devlist
; d
; d
=d
->next
)
152 if (d
->sysminor
== sysminor
)
156 d
= aoedev_newdev(bufcnt
);
158 spin_unlock_irqrestore(&devlist_lock
, flags
);
159 printk(KERN_INFO
"aoe: aoedev_newdev failure.\n");
162 d
->sysminor
= sysminor
;
163 d
->aoemajor
= AOEMAJOR(sysminor
);
164 d
->aoeminor
= AOEMINOR(sysminor
);
167 spin_unlock_irqrestore(&devlist_lock
, flags
);
172 aoedev_freedev(struct aoedev
*d
)
184 skb_shinfo(f
->skb
)->nr_frags
= 0;
185 dev_kfree_skb(f
->skb
);
189 mempool_destroy(d
->bufpool
);
199 flush_scheduled_work();
201 while ((d
= devlist
)) {
204 spin_lock_irqsave(&d
->lock
, flags
);
206 d
->flags
|= DEVFL_TKILL
;
207 spin_unlock_irqrestore(&d
->lock
, flags
);
209 del_timer_sync(&d
->timer
);
217 spin_lock_init(&devlist_lock
);