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>
9 #include <linux/backing-dev.h>
11 #include <linux/ioctl.h>
12 #include <linux/genhd.h>
13 #include <linux/netdevice.h>
16 static struct kmem_cache *buf_pool_cache;
18 static ssize_t aoedisk_show_state(struct device *dev,
19 struct device_attribute *attr, char *page)
21 struct gendisk *disk = dev_to_disk(dev);
22 struct aoedev *d = disk->private_data;
24 return snprintf(page, PAGE_SIZE,
26 (d->flags & DEVFL_UP) ? "up" : "down",
27 (d->flags & DEVFL_PAUSE) ? ",paused" :
28 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
29 /* I'd rather see nopen exported so we can ditch closewait */
31 static ssize_t aoedisk_show_mac(struct device *dev,
32 struct device_attribute *attr, char *page)
34 struct gendisk *disk = dev_to_disk(dev);
35 struct aoedev *d = disk->private_data;
37 return snprintf(page, PAGE_SIZE, "%012llx\n",
38 (unsigned long long)mac_addr(d->addr));
40 static ssize_t aoedisk_show_netif(struct device *dev,
41 struct device_attribute *attr, char *page)
43 struct gendisk *disk = dev_to_disk(dev);
44 struct aoedev *d = disk->private_data;
46 return snprintf(page, PAGE_SIZE, "%s\n", d->ifp->name);
48 /* firmware version */
49 static ssize_t aoedisk_show_fwver(struct device *dev,
50 struct device_attribute *attr, char *page)
52 struct gendisk *disk = dev_to_disk(dev);
53 struct aoedev *d = disk->private_data;
55 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
58 static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
59 static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
60 static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
61 static struct device_attribute dev_attr_firmware_version = {
62 .attr = { .name = "firmware-version", .mode = S_IRUGO, .owner = THIS_MODULE },
63 .show = aoedisk_show_fwver,
66 static struct attribute *aoe_attrs[] = {
70 &dev_attr_firmware_version.attr,
74 static const struct attribute_group attr_group = {
79 aoedisk_add_sysfs(struct aoedev *d)
81 return sysfs_create_group(&d->gd->dev.kobj, &attr_group);
84 aoedisk_rm_sysfs(struct aoedev *d)
86 sysfs_remove_group(&d->gd->dev.kobj, &attr_group);
90 aoeblk_open(struct inode *inode, struct file *filp)
95 d = inode->i_bdev->bd_disk->private_data;
97 spin_lock_irqsave(&d->lock, flags);
98 if (d->flags & DEVFL_UP) {
100 spin_unlock_irqrestore(&d->lock, flags);
103 spin_unlock_irqrestore(&d->lock, flags);
108 aoeblk_release(struct inode *inode, struct file *filp)
113 d = inode->i_bdev->bd_disk->private_data;
115 spin_lock_irqsave(&d->lock, flags);
117 if (--d->nopen == 0) {
118 spin_unlock_irqrestore(&d->lock, flags);
119 aoecmd_cfg(d->aoemajor, d->aoeminor);
122 spin_unlock_irqrestore(&d->lock, flags);
128 aoeblk_make_request(struct request_queue *q, struct bio *bio)
135 blk_queue_bounce(q, &bio);
137 d = bio->bi_bdev->bd_disk->private_data;
138 buf = mempool_alloc(d->bufpool, GFP_NOIO);
140 printk(KERN_INFO "aoe: buf allocation failure\n");
141 bio_endio(bio, -ENOMEM);
144 memset(buf, 0, sizeof(*buf));
145 INIT_LIST_HEAD(&buf->bufs);
146 buf->start_time = jiffies;
148 buf->resid = bio->bi_size;
149 buf->sector = bio->bi_sector;
150 buf->bv = &bio->bi_io_vec[bio->bi_idx];
151 WARN_ON(buf->bv->bv_len == 0);
152 buf->bv_resid = buf->bv->bv_len;
153 buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset;
155 spin_lock_irqsave(&d->lock, flags);
157 if ((d->flags & DEVFL_UP) == 0) {
158 printk(KERN_INFO "aoe: device %ld.%ld is not up\n",
159 d->aoemajor, d->aoeminor);
160 spin_unlock_irqrestore(&d->lock, flags);
161 mempool_free(buf, d->bufpool);
162 bio_endio(bio, -ENXIO);
166 list_add_tail(&buf->bufs, &d->bufq);
170 d->sendq_hd = d->sendq_tl = NULL;
172 spin_unlock_irqrestore(&d->lock, flags);
179 aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
181 struct aoedev *d = bdev->bd_disk->private_data;
183 if ((d->flags & DEVFL_UP) == 0) {
184 printk(KERN_ERR "aoe: disk not up\n");
188 geo->cylinders = d->geo.cylinders;
189 geo->heads = d->geo.heads;
190 geo->sectors = d->geo.sectors;
194 static struct block_device_operations aoe_bdops = {
196 .release = aoeblk_release,
197 .getgeo = aoeblk_getgeo,
198 .owner = THIS_MODULE,
201 /* alloc_disk and add_disk can sleep */
203 aoeblk_gdalloc(void *vp)
205 struct aoedev *d = vp;
209 gd = alloc_disk(AOE_PARTITIONS);
211 printk(KERN_ERR "aoe: cannot allocate disk structure for %ld.%ld\n",
212 d->aoemajor, d->aoeminor);
216 d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
217 if (d->bufpool == NULL) {
218 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%ld\n",
219 d->aoemajor, d->aoeminor);
223 blk_queue_make_request(&d->blkq, aoeblk_make_request);
224 if (bdi_init(&d->blkq.backing_dev_info))
226 spin_lock_irqsave(&d->lock, flags);
227 gd->major = AOE_MAJOR;
228 gd->first_minor = d->sysminor * AOE_PARTITIONS;
229 gd->fops = &aoe_bdops;
230 gd->private_data = d;
231 gd->capacity = d->ssize;
232 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%ld",
233 d->aoemajor, d->aoeminor);
235 gd->queue = &d->blkq;
237 d->flags &= ~DEVFL_GDALLOC;
238 d->flags |= DEVFL_UP;
240 spin_unlock_irqrestore(&d->lock, flags);
243 aoedisk_add_sysfs(d);
247 mempool_destroy(d->bufpool);
251 spin_lock_irqsave(&d->lock, flags);
252 d->flags &= ~DEVFL_GDALLOC;
253 spin_unlock_irqrestore(&d->lock, flags);
259 kmem_cache_destroy(buf_pool_cache);
265 buf_pool_cache = kmem_cache_create("aoe_bufs",
268 if (buf_pool_cache == NULL)