blob: a946929735a5818e525c14e97b9011913f9e54f9 [file] [log] [blame]
Ed L. Cashin52e112b2008-02-08 04:20:09 -08001/* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * aoeblk.c
4 * block device routines
5 */
6
7#include <linux/hdreg.h>
8#include <linux/blkdev.h>
Andrew Morton43cbe2c2007-12-10 15:49:13 -08009#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/fs.h>
11#include <linux/ioctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/genhd.h>
14#include <linux/netdevice.h>
Arnd Bergmann6e9624b2010-08-07 18:25:34 +020015#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "aoe.h"
17
Christoph Lametere18b8902006-12-06 20:33:20 -080018static struct kmem_cache *buf_pool_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Kay Sieversedfaa7c2007-05-21 22:08:01 +020020static ssize_t aoedisk_show_state(struct device *dev,
21 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020023 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 struct aoedev *d = disk->private_data;
25
26 return snprintf(page, PAGE_SIZE,
27 "%s%s\n",
28 (d->flags & DEVFL_UP) ? "up" : "down",
Ed L. Cashin68e0d422008-02-08 04:20:00 -080029 (d->flags & DEVFL_KICKME) ? ",kickme" :
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050030 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
31 /* I'd rather see nopen exported so we can ditch closewait */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
Kay Sieversedfaa7c2007-05-21 22:08:01 +020033static ssize_t aoedisk_show_mac(struct device *dev,
34 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020036 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 struct aoedev *d = disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080038 struct aoetgt *t = d->targets[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Ed L. Cashin68e0d422008-02-08 04:20:00 -080040 if (t == NULL)
41 return snprintf(page, PAGE_SIZE, "none\n");
Harvey Harrison411c41e2008-11-25 00:40:37 -080042 return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
Kay Sieversedfaa7c2007-05-21 22:08:01 +020044static ssize_t aoedisk_show_netif(struct device *dev,
45 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020047 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct aoedev *d = disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080049 struct net_device *nds[8], **nd, **nnd, **ne;
50 struct aoetgt **t, **te;
51 struct aoeif *ifp, *e;
52 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Ed L. Cashin68e0d422008-02-08 04:20:00 -080054 memset(nds, 0, sizeof nds);
55 nd = nds;
56 ne = nd + ARRAY_SIZE(nds);
57 t = d->targets;
58 te = t + NTARGETS;
59 for (; t < te && *t; t++) {
60 ifp = (*t)->ifs;
61 e = ifp + NAOEIFS;
62 for (; ifp < e && ifp->nd; ifp++) {
63 for (nnd = nds; nnd < nd; nnd++)
64 if (*nnd == ifp->nd)
65 break;
66 if (nnd == nd && nd != ne)
67 *nd++ = ifp->nd;
68 }
69 }
70
71 ne = nd;
72 nd = nds;
73 if (*nd == NULL)
74 return snprintf(page, PAGE_SIZE, "none\n");
75 for (p = page; nd < ne; nd++)
76 p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
77 p == page ? "" : ",", (*nd)->name);
78 p += snprintf(p, PAGE_SIZE - (p-page), "\n");
79 return p-page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
Ed L Cashin4613ed22005-04-29 10:24:25 -040081/* firmware version */
Kay Sieversedfaa7c2007-05-21 22:08:01 +020082static ssize_t aoedisk_show_fwver(struct device *dev,
83 struct device_attribute *attr, char *page)
Ed L Cashin4613ed22005-04-29 10:24:25 -040084{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020085 struct gendisk *disk = dev_to_disk(dev);
Ed L Cashin4613ed22005-04-29 10:24:25 -040086 struct aoedev *d = disk->private_data;
87
88 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
89}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Kay Sieversedfaa7c2007-05-21 22:08:01 +020091static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
92static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
93static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
94static struct device_attribute dev_attr_firmware_version = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -070095 .attr = { .name = "firmware-version", .mode = S_IRUGO },
Kay Sieversedfaa7c2007-05-21 22:08:01 +020096 .show = aoedisk_show_fwver,
Ed L Cashin4613ed22005-04-29 10:24:25 -040097};
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -070099static struct attribute *aoe_attrs[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200100 &dev_attr_state.attr,
101 &dev_attr_mac.attr,
102 &dev_attr_netif.attr,
103 &dev_attr_firmware_version.attr,
104 NULL,
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -0700105};
106
107static const struct attribute_group attr_group = {
108 .attrs = aoe_attrs,
109};
110
111static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112aoedisk_add_sysfs(struct aoedev *d)
113{
Tejun Heoed9e1982008-08-25 19:56:05 +0900114 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116void
117aoedisk_rm_sysfs(struct aoedev *d)
118{
Tejun Heoed9e1982008-08-25 19:56:05 +0900119 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122static int
Al Viro94562c12008-03-02 09:23:18 -0500123aoeblk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Al Viro94562c12008-03-02 09:23:18 -0500125 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 ulong flags;
127
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200128 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 spin_lock_irqsave(&d->lock, flags);
130 if (d->flags & DEVFL_UP) {
131 d->nopen++;
132 spin_unlock_irqrestore(&d->lock, flags);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200133 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return 0;
135 }
136 spin_unlock_irqrestore(&d->lock, flags);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200137 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return -ENODEV;
139}
140
141static int
Al Viro94562c12008-03-02 09:23:18 -0500142aoeblk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Al Viro94562c12008-03-02 09:23:18 -0500144 struct aoedev *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 ulong flags;
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 spin_lock_irqsave(&d->lock, flags);
148
Ed L. Cashin5f7702f2006-01-19 13:46:27 -0500149 if (--d->nopen == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 spin_unlock_irqrestore(&d->lock, flags);
151 aoecmd_cfg(d->aoemajor, d->aoeminor);
152 return 0;
153 }
154 spin_unlock_irqrestore(&d->lock, flags);
155
156 return 0;
157}
158
159static int
Jens Axboe165125e2007-07-24 09:28:11 +0200160aoeblk_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700162 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct aoedev *d;
164 struct buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 ulong flags;
166
167 blk_queue_bounce(q, &bio);
168
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800169 if (bio == NULL) {
170 printk(KERN_ERR "aoe: bio is NULL\n");
171 BUG();
172 return 0;
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 d = bio->bi_bdev->bd_disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800175 if (d == NULL) {
176 printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n");
177 BUG();
178 bio_endio(bio, -ENXIO);
179 return 0;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200180 } else if (bio->bi_rw & REQ_HARDBARRIER) {
Ed Cashin18d82172009-09-10 22:30:47 +0200181 bio_endio(bio, -EOPNOTSUPP);
182 return 0;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800183 } else if (bio->bi_io_vec == NULL) {
184 printk(KERN_ERR "aoe: bi_io_vec is NULL\n");
185 BUG();
186 bio_endio(bio, -ENXIO);
187 return 0;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 buf = mempool_alloc(d->bufpool, GFP_NOIO);
190 if (buf == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400191 printk(KERN_INFO "aoe: buf allocation failure\n");
NeilBrown6712ecf2007-09-27 12:47:43 +0200192 bio_endio(bio, -ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return 0;
194 }
195 memset(buf, 0, sizeof(*buf));
196 INIT_LIST_HEAD(&buf->bufs);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800197 buf->stime = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 buf->bio = bio;
199 buf->resid = bio->bi_size;
200 buf->sector = bio->bi_sector;
Ed L. Cashin392e4842006-09-20 14:36:50 -0400201 buf->bv = &bio->bi_io_vec[bio->bi_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 buf->bv_resid = buf->bv->bv_len;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800203 WARN_ON(buf->bv_resid == 0);
204 buf->bv_off = buf->bv->bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 spin_lock_irqsave(&d->lock, flags);
207
208 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashin1d759812008-02-08 04:20:08 -0800209 printk(KERN_INFO "aoe: device %ld.%d is not up\n",
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400210 d->aoemajor, d->aoeminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 spin_unlock_irqrestore(&d->lock, flags);
212 mempool_free(buf, d->bufpool);
NeilBrown6712ecf2007-09-27 12:47:43 +0200213 bio_endio(bio, -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return 0;
215 }
216
217 list_add_tail(&buf->bufs, &d->bufq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500219 aoecmd_work(d);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700220 __skb_queue_head_init(&queue);
221 skb_queue_splice_init(&d->sendq, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 spin_unlock_irqrestore(&d->lock, flags);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700224 aoenet_xmit(&queue);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return 0;
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800230aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800232 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400235 printk(KERN_ERR "aoe: disk not up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return -ENODEV;
237 }
238
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800239 geo->cylinders = d->geo.cylinders;
240 geo->heads = d->geo.heads;
241 geo->sectors = d->geo.sectors;
242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700245static const struct block_device_operations aoe_bdops = {
Al Viro94562c12008-03-02 09:23:18 -0500246 .open = aoeblk_open,
247 .release = aoeblk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800248 .getgeo = aoeblk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .owner = THIS_MODULE,
250};
251
252/* alloc_disk and add_disk can sleep */
253void
254aoeblk_gdalloc(void *vp)
255{
256 struct aoedev *d = vp;
257 struct gendisk *gd;
258 ulong flags;
259
260 gd = alloc_disk(AOE_PARTITIONS);
261 if (gd == NULL) {
Ed L. Cashin1d759812008-02-08 04:20:08 -0800262 printk(KERN_ERR
263 "aoe: cannot allocate disk structure for %ld.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400264 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800265 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
Matthew Dobson93d23412006-03-26 01:37:50 -0800268 d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (d->bufpool == NULL) {
Ed L. Cashin1d759812008-02-08 04:20:08 -0800270 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400271 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800272 goto err_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
Ed Cashin7135a71b2009-09-09 14:10:18 +0200275 d->blkq = blk_alloc_queue(GFP_KERNEL);
276 if (!d->blkq)
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800277 goto err_mempool;
Ed Cashin7135a71b2009-09-09 14:10:18 +0200278 blk_queue_make_request(d->blkq, aoeblk_make_request);
Jens Axboed9938312009-06-12 14:45:52 +0200279 d->blkq->backing_dev_info.name = "aoe";
Ed Cashin7135a71b2009-09-09 14:10:18 +0200280 if (bdi_init(&d->blkq->backing_dev_info))
281 goto err_blkq;
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800282 spin_lock_irqsave(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 gd->major = AOE_MAJOR;
284 gd->first_minor = d->sysminor * AOE_PARTITIONS;
285 gd->fops = &aoe_bdops;
286 gd->private_data = d;
Tejun Heo80795ae2008-08-25 19:56:07 +0900287 set_capacity(gd, d->ssize);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800288 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 d->aoemajor, d->aoeminor);
290
Ed Cashin7135a71b2009-09-09 14:10:18 +0200291 gd->queue = d->blkq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 d->gd = gd;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500293 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 d->flags |= DEVFL_UP;
295
296 spin_unlock_irqrestore(&d->lock, flags);
297
298 add_disk(gd);
299 aoedisk_add_sysfs(d);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800300 return;
301
Ed Cashin7135a71b2009-09-09 14:10:18 +0200302err_blkq:
303 blk_cleanup_queue(d->blkq);
304 d->blkq = NULL;
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800305err_mempool:
306 mempool_destroy(d->bufpool);
307err_disk:
308 put_disk(gd);
309err:
310 spin_lock_irqsave(&d->lock, flags);
311 d->flags &= ~DEVFL_GDALLOC;
312 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315void
316aoeblk_exit(void)
317{
318 kmem_cache_destroy(buf_pool_cache);
319}
320
321int __init
322aoeblk_init(void)
323{
Paul Mundt20c2df82007-07-20 10:11:58 +0900324 buf_pool_cache = kmem_cache_create("aoe_bufs",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 sizeof(struct buf),
Paul Mundt20c2df82007-07-20 10:11:58 +0900326 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (buf_pool_cache == NULL)
328 return -ENOMEM;
329
330 return 0;
331}
332