1 /* Copyright (c) 2006 Coraid, Inc. See COPYING for GPL terms. */
4 * block device routines
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
10 #include <linux/ioctl.h>
11 #include <linux/genhd.h>
12 #include <linux/netdevice.h>
15 static kmem_cache_t *buf_pool_cache;
17 /* add attributes for our block devices in sysfs */
18 static ssize_t aoedisk_show_state(struct gendisk * disk, char *page)
20 struct aoedev *d = disk->private_data;
22 return snprintf(page, PAGE_SIZE,
24 (d->flags & DEVFL_UP) ? "up" : "down",
25 (d->flags & DEVFL_PAUSE) ? ",paused" :
26 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
27 /* I'd rather see nopen exported so we can ditch closewait */
29 static ssize_t aoedisk_show_mac(struct gendisk * disk, char *page)
31 struct aoedev *d = disk->private_data;
33 return snprintf(page, PAGE_SIZE, "%012llx\n",
34 (unsigned long long)mac_addr(d->addr));
36 static ssize_t aoedisk_show_netif(struct gendisk * disk, char *page)
38 struct aoedev *d = disk->private_data;
40 return snprintf(page, PAGE_SIZE, "%s\n", d->ifp->name);
42 /* firmware version */
43 static ssize_t aoedisk_show_fwver(struct gendisk * disk, char *page)
45 struct aoedev *d = disk->private_data;
47 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
50 static struct disk_attribute disk_attr_state = {
51 .attr = {.name = "state", .mode = S_IRUGO },
52 .show = aoedisk_show_state
54 static struct disk_attribute disk_attr_mac = {
55 .attr = {.name = "mac", .mode = S_IRUGO },
56 .show = aoedisk_show_mac
58 static struct disk_attribute disk_attr_netif = {
59 .attr = {.name = "netif", .mode = S_IRUGO },
60 .show = aoedisk_show_netif
62 static struct disk_attribute disk_attr_fwver = {
63 .attr = {.name = "firmware-version", .mode = S_IRUGO },
64 .show = aoedisk_show_fwver
68 aoedisk_add_sysfs(struct aoedev *d)
70 sysfs_create_file(&d->gd->kobj, &disk_attr_state.attr);
71 sysfs_create_file(&d->gd->kobj, &disk_attr_mac.attr);
72 sysfs_create_file(&d->gd->kobj, &disk_attr_netif.attr);
73 sysfs_create_file(&d->gd->kobj, &disk_attr_fwver.attr);
76 aoedisk_rm_sysfs(struct aoedev *d)
78 sysfs_remove_link(&d->gd->kobj, "state");
79 sysfs_remove_link(&d->gd->kobj, "mac");
80 sysfs_remove_link(&d->gd->kobj, "netif");
81 sysfs_remove_link(&d->gd->kobj, "firmware-version");
85 aoeblk_open(struct inode *inode, struct file *filp)
90 d = inode->i_bdev->bd_disk->private_data;
92 spin_lock_irqsave(&d->lock, flags);
93 if (d->flags & DEVFL_UP) {
95 spin_unlock_irqrestore(&d->lock, flags);
98 spin_unlock_irqrestore(&d->lock, flags);
103 aoeblk_release(struct inode *inode, struct file *filp)
108 d = inode->i_bdev->bd_disk->private_data;
110 spin_lock_irqsave(&d->lock, flags);
112 if (--d->nopen == 0) {
113 spin_unlock_irqrestore(&d->lock, flags);
114 aoecmd_cfg(d->aoemajor, d->aoeminor);
117 spin_unlock_irqrestore(&d->lock, flags);
123 aoeblk_make_request(request_queue_t *q, struct bio *bio)
130 blk_queue_bounce(q, &bio);
132 d = bio->bi_bdev->bd_disk->private_data;
133 buf = mempool_alloc(d->bufpool, GFP_NOIO);
135 iprintk("buf allocation failure\n");
136 bio_endio(bio, bio->bi_size, -ENOMEM);
139 memset(buf, 0, sizeof(*buf));
140 INIT_LIST_HEAD(&buf->bufs);
141 buf->start_time = jiffies;
143 buf->resid = bio->bi_size;
144 buf->sector = bio->bi_sector;
145 buf->bv = &bio->bi_io_vec[bio->bi_idx];
146 WARN_ON(buf->bv->bv_len == 0);
147 buf->bv_resid = buf->bv->bv_len;
148 buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset;
150 spin_lock_irqsave(&d->lock, flags);
152 if ((d->flags & DEVFL_UP) == 0) {
153 iprintk("device %ld.%ld is not up\n", d->aoemajor, d->aoeminor);
154 spin_unlock_irqrestore(&d->lock, flags);
155 mempool_free(buf, d->bufpool);
156 bio_endio(bio, bio->bi_size, -ENXIO);
160 list_add_tail(&buf->bufs, &d->bufq);
164 d->sendq_hd = d->sendq_tl = NULL;
166 spin_unlock_irqrestore(&d->lock, flags);
173 aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
175 struct aoedev *d = bdev->bd_disk->private_data;
177 if ((d->flags & DEVFL_UP) == 0) {
178 eprintk("disk not up\n");
182 geo->cylinders = d->geo.cylinders;
183 geo->heads = d->geo.heads;
184 geo->sectors = d->geo.sectors;
188 static struct block_device_operations aoe_bdops = {
190 .release = aoeblk_release,
191 .getgeo = aoeblk_getgeo,
192 .owner = THIS_MODULE,
195 /* alloc_disk and add_disk can sleep */
197 aoeblk_gdalloc(void *vp)
199 struct aoedev *d = vp;
203 gd = alloc_disk(AOE_PARTITIONS);
205 eprintk("cannot allocate disk structure for %ld.%ld\n",
206 d->aoemajor, d->aoeminor);
207 spin_lock_irqsave(&d->lock, flags);
208 d->flags &= ~DEVFL_GDALLOC;
209 spin_unlock_irqrestore(&d->lock, flags);
213 d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
214 if (d->bufpool == NULL) {
215 eprintk("cannot allocate bufpool for %ld.%ld\n",
216 d->aoemajor, d->aoeminor);
218 spin_lock_irqsave(&d->lock, flags);
219 d->flags &= ~DEVFL_GDALLOC;
220 spin_unlock_irqrestore(&d->lock, flags);
224 spin_lock_irqsave(&d->lock, flags);
225 blk_queue_make_request(&d->blkq, aoeblk_make_request);
226 gd->major = AOE_MAJOR;
227 gd->first_minor = d->sysminor * AOE_PARTITIONS;
228 gd->fops = &aoe_bdops;
229 gd->private_data = d;
230 gd->capacity = d->ssize;
231 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%ld",
232 d->aoemajor, d->aoeminor);
234 gd->queue = &d->blkq;
236 d->flags &= ~DEVFL_GDALLOC;
237 d->flags |= DEVFL_UP;
239 spin_unlock_irqrestore(&d->lock, flags);
242 aoedisk_add_sysfs(d);
248 kmem_cache_destroy(buf_pool_cache);
254 buf_pool_cache = kmem_cache_create("aoe_bufs",
257 if (buf_pool_cache == NULL)