blob: a129f8c8073db35469f4c0f8232ed3eb44f51d05 [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>
Ed Cashin667be1e2012-12-17 16:03:42 -080020#include <scsi/sg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "aoe.h"
22
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020023static DEFINE_MUTEX(aoeblk_mutex);
Christoph Lametere18b8902006-12-06 20:33:20 -080024static struct kmem_cache *buf_pool_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Ed Cashinaa304fd2012-12-17 16:03:32 -080026/* GPFS needs a larger value than the default. */
27static int aoe_maxsectors;
28module_param(aoe_maxsectors, int, 0644);
29MODULE_PARM_DESC(aoe_maxsectors,
30 "When nonzero, set the maximum number of sectors per I/O request");
31
Kay Sieversedfaa7c2007-05-21 22:08:01 +020032static ssize_t aoedisk_show_state(struct device *dev,
33 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020035 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct aoedev *d = disk->private_data;
37
38 return snprintf(page, PAGE_SIZE,
39 "%s%s\n",
40 (d->flags & DEVFL_UP) ? "up" : "down",
Ed L. Cashin68e0d422008-02-08 04:20:00 -080041 (d->flags & DEVFL_KICKME) ? ",kickme" :
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050042 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
43 /* I'd rather see nopen exported so we can ditch closewait */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
Kay Sieversedfaa7c2007-05-21 22:08:01 +020045static ssize_t aoedisk_show_mac(struct device *dev,
46 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020048 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct aoedev *d = disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080050 struct aoetgt *t = d->targets[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Ed L. Cashin68e0d422008-02-08 04:20:00 -080052 if (t == NULL)
53 return snprintf(page, PAGE_SIZE, "none\n");
Harvey Harrison411c41e2008-11-25 00:40:37 -080054 return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
Kay Sieversedfaa7c2007-05-21 22:08:01 +020056static ssize_t aoedisk_show_netif(struct device *dev,
57 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020059 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct aoedev *d = disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080061 struct net_device *nds[8], **nd, **nnd, **ne;
62 struct aoetgt **t, **te;
63 struct aoeif *ifp, *e;
64 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Ed L. Cashin68e0d422008-02-08 04:20:00 -080066 memset(nds, 0, sizeof nds);
67 nd = nds;
68 ne = nd + ARRAY_SIZE(nds);
69 t = d->targets;
Ed Cashin71114ec2012-12-17 16:04:11 -080070 te = t + d->ntargets;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080071 for (; t < te && *t; t++) {
72 ifp = (*t)->ifs;
73 e = ifp + NAOEIFS;
74 for (; ifp < e && ifp->nd; ifp++) {
75 for (nnd = nds; nnd < nd; nnd++)
76 if (*nnd == ifp->nd)
77 break;
78 if (nnd == nd && nd != ne)
79 *nd++ = ifp->nd;
80 }
81 }
82
83 ne = nd;
84 nd = nds;
85 if (*nd == NULL)
86 return snprintf(page, PAGE_SIZE, "none\n");
87 for (p = page; nd < ne; nd++)
88 p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
89 p == page ? "" : ",", (*nd)->name);
90 p += snprintf(p, PAGE_SIZE - (p-page), "\n");
91 return p-page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
Ed L Cashin4613ed22005-04-29 10:24:25 -040093/* firmware version */
Kay Sieversedfaa7c2007-05-21 22:08:01 +020094static ssize_t aoedisk_show_fwver(struct device *dev,
95 struct device_attribute *attr, char *page)
Ed L Cashin4613ed22005-04-29 10:24:25 -040096{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020097 struct gendisk *disk = dev_to_disk(dev);
Ed L Cashin4613ed22005-04-29 10:24:25 -040098 struct aoedev *d = disk->private_data;
99
100 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
101}
Ed Cashin90a25082012-12-17 16:03:34 -0800102static ssize_t aoedisk_show_payload(struct device *dev,
103 struct device_attribute *attr, char *page)
104{
105 struct gendisk *disk = dev_to_disk(dev);
106 struct aoedev *d = disk->private_data;
107
108 return snprintf(page, PAGE_SIZE, "%lu\n", d->maxbcnt);
109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200111static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
112static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
113static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
114static struct device_attribute dev_attr_firmware_version = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700115 .attr = { .name = "firmware-version", .mode = S_IRUGO },
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200116 .show = aoedisk_show_fwver,
Ed L Cashin4613ed22005-04-29 10:24:25 -0400117};
Ed Cashin90a25082012-12-17 16:03:34 -0800118static DEVICE_ATTR(payload, S_IRUGO, aoedisk_show_payload, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -0700120static struct attribute *aoe_attrs[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200121 &dev_attr_state.attr,
122 &dev_attr_mac.attr,
123 &dev_attr_netif.attr,
124 &dev_attr_firmware_version.attr,
Ed Cashin90a25082012-12-17 16:03:34 -0800125 &dev_attr_payload.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200126 NULL,
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -0700127};
128
129static const struct attribute_group attr_group = {
130 .attrs = aoe_attrs,
131};
132
133static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134aoedisk_add_sysfs(struct aoedev *d)
135{
Tejun Heoed9e1982008-08-25 19:56:05 +0900136 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138void
139aoedisk_rm_sysfs(struct aoedev *d)
140{
Tejun Heoed9e1982008-08-25 19:56:05 +0900141 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144static int
Al Viro94562c12008-03-02 09:23:18 -0500145aoeblk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Al Viro94562c12008-03-02 09:23:18 -0500147 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 ulong flags;
149
Ed Cashine52a2932012-12-17 16:04:09 -0800150 if (!virt_addr_valid(d)) {
151 pr_crit("aoe: invalid device pointer in %s\n",
152 __func__);
153 WARN_ON(1);
154 return -ENODEV;
155 }
156 if (!(d->flags & DEVFL_UP) || d->flags & DEVFL_TKILL)
157 return -ENODEV;
158
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200159 mutex_lock(&aoeblk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 spin_lock_irqsave(&d->lock, flags);
Ed Cashine52a2932012-12-17 16:04:09 -0800161 if (d->flags & DEVFL_UP && !(d->flags & DEVFL_TKILL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 d->nopen++;
163 spin_unlock_irqrestore(&d->lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200164 mutex_unlock(&aoeblk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return 0;
166 }
167 spin_unlock_irqrestore(&d->lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200168 mutex_unlock(&aoeblk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return -ENODEV;
170}
171
172static int
Al Viro94562c12008-03-02 09:23:18 -0500173aoeblk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Al Viro94562c12008-03-02 09:23:18 -0500175 struct aoedev *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 ulong flags;
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 spin_lock_irqsave(&d->lock, flags);
179
Ed L. Cashin5f7702f2006-01-19 13:46:27 -0500180 if (--d->nopen == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 spin_unlock_irqrestore(&d->lock, flags);
182 aoecmd_cfg(d->aoemajor, d->aoeminor);
183 return 0;
184 }
185 spin_unlock_irqrestore(&d->lock, flags);
186
187 return 0;
188}
189
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200190static void
Ed Cashin69cf2d852012-10-04 17:16:23 -0700191aoeblk_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 struct aoedev *d;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700194 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Ed Cashin69cf2d852012-10-04 17:16:23 -0700196 d = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if ((d->flags & DEVFL_UP) == 0) {
Andrew Morton027b1802010-10-28 06:15:26 -0600198 pr_info_ratelimited("aoe: device %ld.%d is not up\n",
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400199 d->aoemajor, d->aoeminor);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700200 while ((rq = blk_peek_request(q))) {
201 blk_start_request(rq);
202 aoe_end_request(d, rq, 1);
203 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200204 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500206 aoecmd_work(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800210aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800212 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400215 printk(KERN_ERR "aoe: disk not up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return -ENODEV;
217 }
218
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800219 geo->cylinders = d->geo.cylinders;
220 geo->heads = d->geo.heads;
221 geo->sectors = d->geo.sectors;
222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Ed Cashin667be1e2012-12-17 16:03:42 -0800225static int
226aoeblk_ioctl(struct block_device *bdev, fmode_t mode, uint cmd, ulong arg)
227{
228 struct aoedev *d;
229
230 if (!arg)
231 return -EINVAL;
232
233 d = bdev->bd_disk->private_data;
234 if ((d->flags & DEVFL_UP) == 0) {
235 pr_err("aoe: disk not up\n");
236 return -ENODEV;
237 }
238
239 if (cmd == HDIO_GET_IDENTITY) {
240 if (!copy_to_user((void __user *) arg, &d->ident,
241 sizeof(d->ident)))
242 return 0;
243 return -EFAULT;
244 }
245
246 /* udev calls scsi_id, which uses SG_IO, resulting in noise */
247 if (cmd != SG_IO)
248 pr_info("aoe: unknown ioctl 0x%x\n", cmd);
249
250 return -ENOTTY;
251}
252
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700253static const struct block_device_operations aoe_bdops = {
Al Viro94562c12008-03-02 09:23:18 -0500254 .open = aoeblk_open,
255 .release = aoeblk_release,
Ed Cashin667be1e2012-12-17 16:03:42 -0800256 .ioctl = aoeblk_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800257 .getgeo = aoeblk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 .owner = THIS_MODULE,
259};
260
261/* alloc_disk and add_disk can sleep */
262void
263aoeblk_gdalloc(void *vp)
264{
265 struct aoedev *d = vp;
266 struct gendisk *gd;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700267 mempool_t *mp;
268 struct request_queue *q;
269 enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 ulong flags;
Ed Cashine52a2932012-12-17 16:04:09 -0800271 int late = 0;
272
273 spin_lock_irqsave(&d->lock, flags);
274 if (d->flags & DEVFL_GDALLOC
275 && !(d->flags & DEVFL_TKILL)
276 && !(d->flags & DEVFL_GD_NOW))
277 d->flags |= DEVFL_GD_NOW;
278 else
279 late = 1;
280 spin_unlock_irqrestore(&d->lock, flags);
281 if (late)
282 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 gd = alloc_disk(AOE_PARTITIONS);
285 if (gd == NULL) {
Ed Cashin69cf2d852012-10-04 17:16:23 -0700286 pr_err("aoe: cannot allocate disk structure for %ld.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400287 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800288 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
Ed Cashin69cf2d852012-10-04 17:16:23 -0700291 mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab,
292 buf_pool_cache);
293 if (mp == NULL) {
Ed L. Cashin1d759812008-02-08 04:20:08 -0800294 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400295 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800296 goto err_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Ed Cashin69cf2d852012-10-04 17:16:23 -0700298 q = blk_init_queue(aoeblk_request, &d->lock);
299 if (q == NULL) {
300 pr_err("aoe: cannot allocate block queue for %ld.%d\n",
301 d->aoemajor, d->aoeminor);
Ed Cashin0a414092012-12-17 16:03:58 -0800302 goto err_mempool;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800305 spin_lock_irqsave(&d->lock, flags);
Ed Cashine52a2932012-12-17 16:04:09 -0800306 WARN_ON(!(d->flags & DEVFL_GD_NOW));
307 WARN_ON(!(d->flags & DEVFL_GDALLOC));
308 WARN_ON(d->flags & DEVFL_TKILL);
309 WARN_ON(d->gd);
310 WARN_ON(d->flags & DEVFL_UP);
Ed Cashin0a414092012-12-17 16:03:58 -0800311 blk_queue_max_hw_sectors(q, BLK_DEF_MAX_SECTORS);
312 q->backing_dev_info.name = "aoe";
Ed Cashin69cf2d852012-10-04 17:16:23 -0700313 q->backing_dev_info.ra_pages = READ_AHEAD / PAGE_CACHE_SIZE;
314 d->bufpool = mp;
315 d->blkq = gd->queue = q;
316 q->queuedata = d;
317 d->gd = gd;
Ed Cashinaa304fd2012-12-17 16:03:32 -0800318 if (aoe_maxsectors)
319 blk_queue_max_hw_sectors(q, aoe_maxsectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 gd->major = AOE_MAJOR;
Ed Cashin0c966212012-10-04 17:16:40 -0700321 gd->first_minor = d->sysminor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 gd->fops = &aoe_bdops;
323 gd->private_data = d;
Tejun Heo80795ae2008-08-25 19:56:07 +0900324 set_capacity(gd, d->ssize);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800325 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 d->aoemajor, d->aoeminor);
327
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500328 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 d->flags |= DEVFL_UP;
330
331 spin_unlock_irqrestore(&d->lock, flags);
332
333 add_disk(gd);
334 aoedisk_add_sysfs(d);
Ed Cashine52a2932012-12-17 16:04:09 -0800335
336 spin_lock_irqsave(&d->lock, flags);
337 WARN_ON(!(d->flags & DEVFL_GD_NOW));
338 d->flags &= ~DEVFL_GD_NOW;
339 spin_unlock_irqrestore(&d->lock, flags);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800340 return;
341
342err_mempool:
Ed Cashin0a414092012-12-17 16:03:58 -0800343 mempool_destroy(mp);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800344err_disk:
345 put_disk(gd);
346err:
347 spin_lock_irqsave(&d->lock, flags);
Ed Cashine52a2932012-12-17 16:04:09 -0800348 d->flags &= ~DEVFL_GD_NOW;
349 schedule_work(&d->work);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800350 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353void
354aoeblk_exit(void)
355{
356 kmem_cache_destroy(buf_pool_cache);
357}
358
359int __init
360aoeblk_init(void)
361{
Paul Mundt20c2df82007-07-20 10:11:58 +0900362 buf_pool_cache = kmem_cache_create("aoe_bufs",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 sizeof(struct buf),
Paul Mundt20c2df82007-07-20 10:11:58 +0900364 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (buf_pool_cache == NULL)
366 return -ENOMEM;
367
368 return 0;
369}
370