2006-09-20 14:36:48 -04:00
|
|
|
/* Copyright (c) 2006 Coraid, Inc. See COPYING for GPL terms. */
|
2005-04-16 18:20:36 -04:00
|
|
|
/*
|
|
|
|
* aoedev.c
|
|
|
|
* AoE device utility functions; maintains device list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/hdreg.h>
|
|
|
|
#include <linux/blkdev.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include "aoe.h"
|
|
|
|
|
|
|
|
static struct aoedev *devlist;
|
|
|
|
static spinlock_t devlist_lock;
|
|
|
|
|
2006-01-19 13:46:19 -05:00
|
|
|
int
|
|
|
|
aoedev_isbusy(struct aoedev *d)
|
|
|
|
{
|
|
|
|
struct frame *f, *e;
|
|
|
|
|
|
|
|
f = d->frames;
|
|
|
|
e = f + d->nframes;
|
|
|
|
do {
|
2006-09-20 14:34:41 -04:00
|
|
|
if (f->tag != FREETAG)
|
2006-01-19 13:46:19 -05:00
|
|
|
return 1;
|
|
|
|
} while (++f < e);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
struct aoedev *
|
2005-04-19 01:00:18 -04:00
|
|
|
aoedev_by_aoeaddr(int maj, int min)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
|
|
|
struct aoedev *d;
|
|
|
|
ulong flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&devlist_lock, flags);
|
|
|
|
|
|
|
|
for (d=devlist; d; d=d->next)
|
2005-04-19 01:00:18 -04:00
|
|
|
if (d->aoemajor == maj && d->aoeminor == min)
|
2005-04-16 18:20:36 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&devlist_lock, flags);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2006-01-19 13:46:19 -05:00
|
|
|
static void
|
|
|
|
dummy_timer(ulong vp)
|
|
|
|
{
|
|
|
|
struct aoedev *d;
|
|
|
|
|
|
|
|
d = (struct aoedev *)vp;
|
|
|
|
if (d->flags & DEVFL_TKILL)
|
|
|
|
return;
|
|
|
|
d->timer.expires = jiffies + HZ;
|
|
|
|
add_timer(&d->timer);
|
|
|
|
}
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
/* called with devlist lock held */
|
|
|
|
static struct aoedev *
|
|
|
|
aoedev_newdev(ulong nframes)
|
|
|
|
{
|
|
|
|
struct aoedev *d;
|
|
|
|
struct frame *f, *e;
|
|
|
|
|
2005-09-06 18:18:35 -04:00
|
|
|
d = kzalloc(sizeof *d, GFP_ATOMIC);
|
2005-04-16 18:20:36 -04:00
|
|
|
f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
|
2006-09-20 14:36:49 -04:00
|
|
|
switch (!d || !f) {
|
|
|
|
case 0:
|
|
|
|
d->nframes = nframes;
|
|
|
|
d->frames = f;
|
|
|
|
e = f + nframes;
|
|
|
|
for (; f<e; f++) {
|
|
|
|
f->tag = FREETAG;
|
|
|
|
f->skb = new_skb(ETH_ZLEN);
|
|
|
|
if (!f->skb)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (f == e)
|
|
|
|
break;
|
|
|
|
while (f > d->frames) {
|
|
|
|
f--;
|
|
|
|
dev_kfree_skb(f->skb);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if (f)
|
|
|
|
kfree(f);
|
|
|
|
if (d)
|
|
|
|
kfree(d);
|
2005-04-16 18:20:36 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-11-22 09:57:56 -05:00
|
|
|
INIT_WORK(&d->work, aoecmd_sleepwork);
|
2005-04-16 18:20:36 -04:00
|
|
|
spin_lock_init(&d->lock);
|
|
|
|
init_timer(&d->timer);
|
2006-01-19 13:46:19 -05:00
|
|
|
d->timer.data = (ulong) d;
|
|
|
|
d->timer.function = dummy_timer;
|
|
|
|
d->timer.expires = jiffies + HZ;
|
|
|
|
add_timer(&d->timer);
|
2005-04-16 18:20:36 -04:00
|
|
|
d->bufpool = NULL; /* defer to aoeblk_gdalloc */
|
|
|
|
INIT_LIST_HEAD(&d->bufq);
|
|
|
|
d->next = devlist;
|
|
|
|
devlist = d;
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
aoedev_downdev(struct aoedev *d)
|
|
|
|
{
|
|
|
|
struct frame *f, *e;
|
|
|
|
struct buf *buf;
|
|
|
|
struct bio *bio;
|
|
|
|
|
|
|
|
f = d->frames;
|
|
|
|
e = f + d->nframes;
|
|
|
|
for (; f<e; f->tag = FREETAG, f->buf = NULL, f++) {
|
|
|
|
if (f->tag == FREETAG || f->buf == NULL)
|
|
|
|
continue;
|
|
|
|
buf = f->buf;
|
|
|
|
bio = buf->bio;
|
|
|
|
if (--buf->nframesout == 0) {
|
|
|
|
mempool_free(buf, d->bufpool);
|
2007-09-27 06:47:43 -04:00
|
|
|
bio_endio(bio, -EIO);
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
2006-09-20 14:36:49 -04:00
|
|
|
skb_shinfo(f->skb)->nr_frags = f->skb->data_len = 0;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
d->inprocess = NULL;
|
|
|
|
|
|
|
|
while (!list_empty(&d->bufq)) {
|
|
|
|
buf = container_of(d->bufq.next, struct buf, bufs);
|
|
|
|
list_del(d->bufq.next);
|
|
|
|
bio = buf->bio;
|
|
|
|
mempool_free(buf, d->bufpool);
|
2007-09-27 06:47:43 -04:00
|
|
|
bio_endio(bio, -EIO);
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (d->gd)
|
|
|
|
d->gd->capacity = 0;
|
|
|
|
|
2006-01-19 13:46:19 -05:00
|
|
|
d->flags &= ~(DEVFL_UP | DEVFL_PAUSE);
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
2006-01-19 13:46:19 -05:00
|
|
|
/* find it or malloc it */
|
2005-04-16 18:20:36 -04:00
|
|
|
struct aoedev *
|
2006-01-19 13:46:19 -05:00
|
|
|
aoedev_by_sysminor_m(ulong sysminor, ulong bufcnt)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
|
|
|
struct aoedev *d;
|
|
|
|
ulong flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&devlist_lock, flags);
|
|
|
|
|
|
|
|
for (d=devlist; d; d=d->next)
|
2005-04-29 10:24:22 -04:00
|
|
|
if (d->sysminor == sysminor)
|
2005-04-16 18:20:36 -04:00
|
|
|
break;
|
|
|
|
|
2006-01-19 13:46:19 -05:00
|
|
|
if (d == NULL) {
|
|
|
|
d = aoedev_newdev(bufcnt);
|
|
|
|
if (d == NULL) {
|
|
|
|
spin_unlock_irqrestore(&devlist_lock, flags);
|
2006-09-20 14:36:51 -04:00
|
|
|
printk(KERN_INFO "aoe: aoedev_newdev failure.\n");
|
2006-01-19 13:46:19 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-04-16 18:20:36 -04:00
|
|
|
d->sysminor = sysminor;
|
|
|
|
d->aoemajor = AOEMAJOR(sysminor);
|
|
|
|
d->aoeminor = AOEMINOR(sysminor);
|
|
|
|
}
|
|
|
|
|
2006-01-19 13:46:19 -05:00
|
|
|
spin_unlock_irqrestore(&devlist_lock, flags);
|
2005-04-16 18:20:36 -04:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
aoedev_freedev(struct aoedev *d)
|
|
|
|
{
|
2006-09-20 14:36:49 -04:00
|
|
|
struct frame *f, *e;
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
if (d->gd) {
|
|
|
|
aoedisk_rm_sysfs(d);
|
|
|
|
del_gendisk(d->gd);
|
|
|
|
put_disk(d->gd);
|
|
|
|
}
|
2006-09-20 14:36:49 -04:00
|
|
|
f = d->frames;
|
|
|
|
e = f + d->nframes;
|
|
|
|
for (; f<e; f++) {
|
|
|
|
skb_shinfo(f->skb)->nr_frags = 0;
|
|
|
|
dev_kfree_skb(f->skb);
|
|
|
|
}
|
2005-04-16 18:20:36 -04:00
|
|
|
kfree(d->frames);
|
2005-04-19 01:00:19 -04:00
|
|
|
if (d->bufpool)
|
|
|
|
mempool_destroy(d->bufpool);
|
2005-04-16 18:20:36 -04:00
|
|
|
kfree(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
aoedev_exit(void)
|
|
|
|
{
|
|
|
|
struct aoedev *d;
|
|
|
|
ulong flags;
|
|
|
|
|
|
|
|
flush_scheduled_work();
|
|
|
|
|
|
|
|
while ((d = devlist)) {
|
|
|
|
devlist = d->next;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&d->lock, flags);
|
|
|
|
aoedev_downdev(d);
|
2006-01-19 13:46:19 -05:00
|
|
|
d->flags |= DEVFL_TKILL;
|
2005-04-16 18:20:36 -04:00
|
|
|
spin_unlock_irqrestore(&d->lock, flags);
|
|
|
|
|
|
|
|
del_timer_sync(&d->timer);
|
|
|
|
aoedev_freedev(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int __init
|
|
|
|
aoedev_init(void)
|
|
|
|
{
|
|
|
|
spin_lock_init(&devlist_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|