blob: 56736cd5f3fef359fcd3f8a5b7d731c3e2b13d5a [file] [log] [blame]
Ed Cashinfea05a22012-10-04 17:16:38 -07001/* Copyright (c) 2012 Coraid, Inc. See COPYING for GPL terms. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * aoeblk.c
4 * block device routines
5 */
6
Andrew Morton027b1802010-10-28 06:15:26 -06007#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/hdreg.h>
9#include <linux/blkdev.h>
Andrew Morton43cbe2c2007-12-10 15:49:13 -080010#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/fs.h>
12#include <linux/ioctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Andrew Morton027b1802010-10-28 06:15:26 -060014#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/genhd.h>
16#include <linux/netdevice.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020017#include <linux/mutex.h>
Paul Gortmakerd5decd32011-05-26 16:00:52 -040018#include <linux/export.h>
Ed Cashinaa304fd2012-12-17 16:03:32 -080019#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "aoe.h"
21
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020022static DEFINE_MUTEX(aoeblk_mutex);
Christoph Lametere18b8902006-12-06 20:33:20 -080023static struct kmem_cache *buf_pool_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Ed Cashinaa304fd2012-12-17 16:03:32 -080025/* GPFS needs a larger value than the default. */
26static int aoe_maxsectors;
27module_param(aoe_maxsectors, int, 0644);
28MODULE_PARM_DESC(aoe_maxsectors,
29 "When nonzero, set the maximum number of sectors per I/O request");
30
Kay Sieversedfaa7c2007-05-21 22:08:01 +020031static ssize_t aoedisk_show_state(struct device *dev,
32 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020034 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 struct aoedev *d = disk->private_data;
36
37 return snprintf(page, PAGE_SIZE,
38 "%s%s\n",
39 (d->flags & DEVFL_UP) ? "up" : "down",
Ed L. Cashin68e0d422008-02-08 04:20:00 -080040 (d->flags & DEVFL_KICKME) ? ",kickme" :
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050041 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
42 /* I'd rather see nopen exported so we can ditch closewait */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
Kay Sieversedfaa7c2007-05-21 22:08:01 +020044static ssize_t aoedisk_show_mac(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 aoetgt *t = d->targets[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Ed L. Cashin68e0d422008-02-08 04:20:00 -080051 if (t == NULL)
52 return snprintf(page, PAGE_SIZE, "none\n");
Harvey Harrison411c41e2008-11-25 00:40:37 -080053 return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
Kay Sieversedfaa7c2007-05-21 22:08:01 +020055static ssize_t aoedisk_show_netif(struct device *dev,
56 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020058 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 struct aoedev *d = disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080060 struct net_device *nds[8], **nd, **nnd, **ne;
61 struct aoetgt **t, **te;
62 struct aoeif *ifp, *e;
63 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Ed L. Cashin68e0d422008-02-08 04:20:00 -080065 memset(nds, 0, sizeof nds);
66 nd = nds;
67 ne = nd + ARRAY_SIZE(nds);
68 t = d->targets;
69 te = t + NTARGETS;
70 for (; t < te && *t; t++) {
71 ifp = (*t)->ifs;
72 e = ifp + NAOEIFS;
73 for (; ifp < e && ifp->nd; ifp++) {
74 for (nnd = nds; nnd < nd; nnd++)
75 if (*nnd == ifp->nd)
76 break;
77 if (nnd == nd && nd != ne)
78 *nd++ = ifp->nd;
79 }
80 }
81
82 ne = nd;
83 nd = nds;
84 if (*nd == NULL)
85 return snprintf(page, PAGE_SIZE, "none\n");
86 for (p = page; nd < ne; nd++)
87 p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
88 p == page ? "" : ",", (*nd)->name);
89 p += snprintf(p, PAGE_SIZE - (p-page), "\n");
90 return p-page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
Ed L Cashin4613ed22005-04-29 10:24:25 -040092/* firmware version */
Kay Sieversedfaa7c2007-05-21 22:08:01 +020093static ssize_t aoedisk_show_fwver(struct device *dev,
94 struct device_attribute *attr, char *page)
Ed L Cashin4613ed22005-04-29 10:24:25 -040095{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020096 struct gendisk *disk = dev_to_disk(dev);
Ed L Cashin4613ed22005-04-29 10:24:25 -040097 struct aoedev *d = disk->private_data;
98
99 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
100}
Ed Cashin90a25082012-12-17 16:03:34 -0800101static ssize_t aoedisk_show_payload(struct device *dev,
102 struct device_attribute *attr, char *page)
103{
104 struct gendisk *disk = dev_to_disk(dev);
105 struct aoedev *d = disk->private_data;
106
107 return snprintf(page, PAGE_SIZE, "%lu\n", d->maxbcnt);
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200110static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
111static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
112static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
113static struct device_attribute dev_attr_firmware_version = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700114 .attr = { .name = "firmware-version", .mode = S_IRUGO },
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200115 .show = aoedisk_show_fwver,
Ed L Cashin4613ed22005-04-29 10:24:25 -0400116};
Ed Cashin90a25082012-12-17 16:03:34 -0800117static DEVICE_ATTR(payload, S_IRUGO, aoedisk_show_payload, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -0700119static struct attribute *aoe_attrs[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200120 &dev_attr_state.attr,
121 &dev_attr_mac.attr,
122 &dev_attr_netif.attr,
123 &dev_attr_firmware_version.attr,
Ed Cashin90a25082012-12-17 16:03:34 -0800124 &dev_attr_payload.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200125 NULL,
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -0700126};
127
128static const struct attribute_group attr_group = {
129 .attrs = aoe_attrs,
130};
131
132static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133aoedisk_add_sysfs(struct aoedev *d)
134{
Tejun Heoed9e1982008-08-25 19:56:05 +0900135 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137void
138aoedisk_rm_sysfs(struct aoedev *d)
139{
Tejun Heoed9e1982008-08-25 19:56:05 +0900140 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143static int
Al Viro94562c12008-03-02 09:23:18 -0500144aoeblk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Al Viro94562c12008-03-02 09:23:18 -0500146 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 ulong flags;
148
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200149 mutex_lock(&aoeblk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 spin_lock_irqsave(&d->lock, flags);
151 if (d->flags & DEVFL_UP) {
152 d->nopen++;
153 spin_unlock_irqrestore(&d->lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200154 mutex_unlock(&aoeblk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return 0;
156 }
157 spin_unlock_irqrestore(&d->lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200158 mutex_unlock(&aoeblk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return -ENODEV;
160}
161
162static int
Al Viro94562c12008-03-02 09:23:18 -0500163aoeblk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Al Viro94562c12008-03-02 09:23:18 -0500165 struct aoedev *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 ulong flags;
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 spin_lock_irqsave(&d->lock, flags);
169
Ed L. Cashin5f7702f2006-01-19 13:46:27 -0500170 if (--d->nopen == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 spin_unlock_irqrestore(&d->lock, flags);
172 aoecmd_cfg(d->aoemajor, d->aoeminor);
173 return 0;
174 }
175 spin_unlock_irqrestore(&d->lock, flags);
176
177 return 0;
178}
179
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200180static void
Ed Cashin69cf2d852012-10-04 17:16:23 -0700181aoeblk_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 struct aoedev *d;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700184 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Ed Cashin69cf2d852012-10-04 17:16:23 -0700186 d = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if ((d->flags & DEVFL_UP) == 0) {
Andrew Morton027b1802010-10-28 06:15:26 -0600188 pr_info_ratelimited("aoe: device %ld.%d is not up\n",
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400189 d->aoemajor, d->aoeminor);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700190 while ((rq = blk_peek_request(q))) {
191 blk_start_request(rq);
192 aoe_end_request(d, rq, 1);
193 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200194 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500196 aoecmd_work(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800200aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800202 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400205 printk(KERN_ERR "aoe: disk not up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return -ENODEV;
207 }
208
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800209 geo->cylinders = d->geo.cylinders;
210 geo->heads = d->geo.heads;
211 geo->sectors = d->geo.sectors;
212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700215static const struct block_device_operations aoe_bdops = {
Al Viro94562c12008-03-02 09:23:18 -0500216 .open = aoeblk_open,
217 .release = aoeblk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800218 .getgeo = aoeblk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 .owner = THIS_MODULE,
220};
221
222/* alloc_disk and add_disk can sleep */
223void
224aoeblk_gdalloc(void *vp)
225{
226 struct aoedev *d = vp;
227 struct gendisk *gd;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700228 mempool_t *mp;
229 struct request_queue *q;
230 enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 ulong flags;
232
233 gd = alloc_disk(AOE_PARTITIONS);
234 if (gd == NULL) {
Ed Cashin69cf2d852012-10-04 17:16:23 -0700235 pr_err("aoe: cannot allocate disk structure for %ld.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400236 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800237 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239
Ed Cashin69cf2d852012-10-04 17:16:23 -0700240 mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab,
241 buf_pool_cache);
242 if (mp == NULL) {
Ed L. Cashin1d759812008-02-08 04:20:08 -0800243 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400244 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800245 goto err_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
Ed Cashin69cf2d852012-10-04 17:16:23 -0700247 q = blk_init_queue(aoeblk_request, &d->lock);
248 if (q == NULL) {
249 pr_err("aoe: cannot allocate block queue for %ld.%d\n",
250 d->aoemajor, d->aoeminor);
251 mempool_destroy(mp);
252 goto err_disk;
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Ed Cashin7135a71b2009-09-09 14:10:18 +0200255 d->blkq = blk_alloc_queue(GFP_KERNEL);
256 if (!d->blkq)
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800257 goto err_mempool;
Jens Axboed9938312009-06-12 14:45:52 +0200258 d->blkq->backing_dev_info.name = "aoe";
Ed Cashin7135a71b2009-09-09 14:10:18 +0200259 if (bdi_init(&d->blkq->backing_dev_info))
260 goto err_blkq;
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800261 spin_lock_irqsave(&d->lock, flags);
Ed Cashin3d5b0602012-10-04 17:16:20 -0700262 blk_queue_max_hw_sectors(d->blkq, BLK_DEF_MAX_SECTORS);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700263 q->backing_dev_info.ra_pages = READ_AHEAD / PAGE_CACHE_SIZE;
264 d->bufpool = mp;
265 d->blkq = gd->queue = q;
266 q->queuedata = d;
267 d->gd = gd;
Ed Cashinaa304fd2012-12-17 16:03:32 -0800268 if (aoe_maxsectors)
269 blk_queue_max_hw_sectors(q, aoe_maxsectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 gd->major = AOE_MAJOR;
Ed Cashin0c966212012-10-04 17:16:40 -0700271 gd->first_minor = d->sysminor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 gd->fops = &aoe_bdops;
273 gd->private_data = d;
Tejun Heo80795ae2008-08-25 19:56:07 +0900274 set_capacity(gd, d->ssize);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800275 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 d->aoemajor, d->aoeminor);
277
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500278 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 d->flags |= DEVFL_UP;
280
281 spin_unlock_irqrestore(&d->lock, flags);
282
283 add_disk(gd);
284 aoedisk_add_sysfs(d);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800285 return;
286
Ed Cashin7135a71b2009-09-09 14:10:18 +0200287err_blkq:
288 blk_cleanup_queue(d->blkq);
289 d->blkq = NULL;
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800290err_mempool:
291 mempool_destroy(d->bufpool);
292err_disk:
293 put_disk(gd);
294err:
295 spin_lock_irqsave(&d->lock, flags);
296 d->flags &= ~DEVFL_GDALLOC;
297 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300void
301aoeblk_exit(void)
302{
303 kmem_cache_destroy(buf_pool_cache);
304}
305
306int __init
307aoeblk_init(void)
308{
Paul Mundt20c2df82007-07-20 10:11:58 +0900309 buf_pool_cache = kmem_cache_create("aoe_bufs",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 sizeof(struct buf),
Paul Mundt20c2df82007-07-20 10:11:58 +0900311 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (buf_pool_cache == NULL)
313 return -ENOMEM;
314
315 return 0;
316}
317