1 /* Copyright (c) 2004 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_bymac(unsigned char *macaddr
)
21 spin_lock_irqsave(&devlist_lock
, flags
);
23 for (d
=devlist
; d
; d
=d
->next
)
24 if (!memcmp(d
->addr
, macaddr
, 6))
27 spin_unlock_irqrestore(&devlist_lock
, flags
);
31 /* called with devlist lock held */
32 static struct aoedev
*
33 aoedev_newdev(ulong nframes
)
38 d
= kcalloc(1, sizeof *d
, GFP_ATOMIC
);
41 f
= kcalloc(nframes
, sizeof *f
, GFP_ATOMIC
);
53 spin_lock_init(&d
->lock
);
54 init_timer(&d
->timer
);
55 d
->bufpool
= NULL
; /* defer to aoeblk_gdalloc */
56 INIT_LIST_HEAD(&d
->bufq
);
64 aoedev_downdev(struct aoedev
*d
)
70 d
->flags
|= DEVFL_TKILL
;
75 for (; f
<e
; f
->tag
= FREETAG
, f
->buf
= NULL
, f
++) {
76 if (f
->tag
== FREETAG
|| f
->buf
== NULL
)
80 if (--buf
->nframesout
== 0) {
81 mempool_free(buf
, d
->bufpool
);
82 bio_endio(bio
, bio
->bi_size
, -EIO
);
87 while (!list_empty(&d
->bufq
)) {
88 buf
= container_of(d
->bufq
.next
, struct buf
, bufs
);
89 list_del(d
->bufq
.next
);
91 mempool_free(buf
, d
->bufpool
);
92 bio_endio(bio
, bio
->bi_size
, -EIO
);
96 d
->flags
|= DEVFL_CLOSEWAIT
;
100 d
->flags
&= ~DEVFL_UP
;
104 aoedev_set(ulong sysminor
, unsigned char *addr
, struct net_device
*ifp
, ulong bufcnt
)
109 spin_lock_irqsave(&devlist_lock
, flags
);
111 for (d
=devlist
; d
; d
=d
->next
)
112 if (d
->sysminor
== sysminor
113 || memcmp(d
->addr
, addr
, sizeof d
->addr
) == 0)
116 if (d
== NULL
&& (d
= aoedev_newdev(bufcnt
)) == NULL
) {
117 spin_unlock_irqrestore(&devlist_lock
, flags
);
118 printk(KERN_INFO
"aoe: aoedev_set: aoedev_newdev failure.\n");
122 spin_unlock_irqrestore(&devlist_lock
, flags
);
123 spin_lock_irqsave(&d
->lock
, flags
);
127 if (d
->sysminor
!= sysminor
128 || memcmp(d
->addr
, addr
, sizeof d
->addr
)
129 || (d
->flags
& DEVFL_UP
) == 0) {
130 aoedev_downdev(d
); /* flushes outstanding frames */
131 memcpy(d
->addr
, addr
, sizeof d
->addr
);
132 d
->sysminor
= sysminor
;
133 d
->aoemajor
= AOEMAJOR(sysminor
);
134 d
->aoeminor
= AOEMINOR(sysminor
);
137 spin_unlock_irqrestore(&d
->lock
, flags
);
142 aoedev_freedev(struct aoedev
*d
)
150 mempool_destroy(d
->bufpool
);
160 flush_scheduled_work();
162 while ((d
= devlist
)) {
165 spin_lock_irqsave(&d
->lock
, flags
);
167 spin_unlock_irqrestore(&d
->lock
, flags
);
169 del_timer_sync(&d
->timer
);
177 spin_lock_init(&devlist_lock
);