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_KICKME) ? ",kickme" :
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;
36 struct aoetgt *t = d->targets[0];
39 return snprintf(page, PAGE_SIZE, "none\n");
40 return snprintf(page, PAGE_SIZE, "%012llx\n",
41 (unsigned long long)mac_addr(t->addr));
43 static ssize_t aoedisk_show_netif(struct device *dev,
44 struct device_attribute *attr, char *page)
46 struct gendisk *disk = dev_to_disk(dev);
47 struct aoedev *d = disk->private_data;
48 struct net_device *nds[8], **nd, **nnd, **ne;
49 struct aoetgt **t, **te;
50 struct aoeif *ifp, *e;
53 memset(nds, 0, sizeof nds);
55 ne = nd + ARRAY_SIZE(nds);
58 for (; t < te && *t; t++) {
61 for (; ifp < e && ifp->nd; ifp++) {
62 for (nnd = nds; nnd < nd; nnd++)
65 if (nnd == nd && nd != ne)
73 return snprintf(page, PAGE_SIZE, "none\n");
74 for (p = page; nd < ne; nd++)
75 p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
76 p == page ? "" : ",", (*nd)->name);
77 p += snprintf(p, PAGE_SIZE - (p-page), "\n");
80 /* firmware version */
81 static ssize_t aoedisk_show_fwver(struct device *dev,
82 struct device_attribute *attr, char *page)
84 struct gendisk *disk = dev_to_disk(dev);
85 struct aoedev *d = disk->private_data;
87 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
90 static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
91 static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
92 static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
93 static struct device_attribute dev_attr_firmware_version = {
94 .attr = { .name = "firmware-version", .mode = S_IRUGO, .owner = THIS_MODULE },
95 .show = aoedisk_show_fwver,
98 static struct attribute *aoe_attrs[] = {
101 &dev_attr_netif.attr,
102 &dev_attr_firmware_version.attr,
106 static const struct attribute_group attr_group = {
111 aoedisk_add_sysfs(struct aoedev *d)
113 return sysfs_create_group(&d->gd->dev.kobj, &attr_group);
116 aoedisk_rm_sysfs(struct aoedev *d)
118 sysfs_remove_group(&d->gd->dev.kobj, &attr_group);
122 aoeblk_open(struct inode *inode, struct file *filp)
127 d = inode->i_bdev->bd_disk->private_data;
129 spin_lock_irqsave(&d->lock, flags);
130 if (d->flags & DEVFL_UP) {
132 spin_unlock_irqrestore(&d->lock, flags);
135 spin_unlock_irqrestore(&d->lock, flags);
140 aoeblk_release(struct inode *inode, struct file *filp)
145 d = inode->i_bdev->bd_disk->private_data;
147 spin_lock_irqsave(&d->lock, flags);
149 if (--d->nopen == 0) {
150 spin_unlock_irqrestore(&d->lock, flags);
151 aoecmd_cfg(d->aoemajor, d->aoeminor);
154 spin_unlock_irqrestore(&d->lock, flags);
160 aoeblk_make_request(struct request_queue *q, struct bio *bio)
167 blk_queue_bounce(q, &bio);
170 printk(KERN_ERR "aoe: bio is NULL\n");
174 d = bio->bi_bdev->bd_disk->private_data;
176 printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n");
178 bio_endio(bio, -ENXIO);
180 } else if (bio->bi_io_vec == NULL) {
181 printk(KERN_ERR "aoe: bi_io_vec is NULL\n");
183 bio_endio(bio, -ENXIO);
186 buf = mempool_alloc(d->bufpool, GFP_NOIO);
188 printk(KERN_INFO "aoe: buf allocation failure\n");
189 bio_endio(bio, -ENOMEM);
192 memset(buf, 0, sizeof(*buf));
193 INIT_LIST_HEAD(&buf->bufs);
194 buf->stime = jiffies;
196 buf->resid = bio->bi_size;
197 buf->sector = bio->bi_sector;
198 buf->bv = &bio->bi_io_vec[bio->bi_idx];
199 buf->bv_resid = buf->bv->bv_len;
200 WARN_ON(buf->bv_resid == 0);
201 buf->bv_off = buf->bv->bv_offset;
203 spin_lock_irqsave(&d->lock, flags);
205 if ((d->flags & DEVFL_UP) == 0) {
206 printk(KERN_INFO "aoe: device %ld.%ld is not up\n",
207 d->aoemajor, d->aoeminor);
208 spin_unlock_irqrestore(&d->lock, flags);
209 mempool_free(buf, d->bufpool);
210 bio_endio(bio, -ENXIO);
214 list_add_tail(&buf->bufs, &d->bufq);
218 d->sendq_hd = d->sendq_tl = NULL;
220 spin_unlock_irqrestore(&d->lock, flags);
227 aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
229 struct aoedev *d = bdev->bd_disk->private_data;
231 if ((d->flags & DEVFL_UP) == 0) {
232 printk(KERN_ERR "aoe: disk not up\n");
236 geo->cylinders = d->geo.cylinders;
237 geo->heads = d->geo.heads;
238 geo->sectors = d->geo.sectors;
242 static struct block_device_operations aoe_bdops = {
244 .release = aoeblk_release,
245 .getgeo = aoeblk_getgeo,
246 .owner = THIS_MODULE,
249 /* alloc_disk and add_disk can sleep */
251 aoeblk_gdalloc(void *vp)
253 struct aoedev *d = vp;
257 gd = alloc_disk(AOE_PARTITIONS);
259 printk(KERN_ERR "aoe: cannot allocate disk structure for %ld.%ld\n",
260 d->aoemajor, d->aoeminor);
264 d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
265 if (d->bufpool == NULL) {
266 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%ld\n",
267 d->aoemajor, d->aoeminor);
271 blk_queue_make_request(&d->blkq, aoeblk_make_request);
272 if (bdi_init(&d->blkq.backing_dev_info))
274 spin_lock_irqsave(&d->lock, flags);
275 gd->major = AOE_MAJOR;
276 gd->first_minor = d->sysminor * AOE_PARTITIONS;
277 gd->fops = &aoe_bdops;
278 gd->private_data = d;
279 gd->capacity = d->ssize;
280 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
281 d->aoemajor, d->aoeminor);
283 gd->queue = &d->blkq;
285 d->flags &= ~DEVFL_GDALLOC;
286 d->flags |= DEVFL_UP;
288 spin_unlock_irqrestore(&d->lock, flags);
291 aoedisk_add_sysfs(d);
295 mempool_destroy(d->bufpool);
299 spin_lock_irqsave(&d->lock, flags);
300 d->flags &= ~DEVFL_GDALLOC;
301 spin_unlock_irqrestore(&d->lock, flags);
307 kmem_cache_destroy(buf_pool_cache);
313 buf_pool_cache = kmem_cache_create("aoe_bufs",
316 if (buf_pool_cache == NULL)