blob: 00dfc5008ad4bddd8a8efa036eb1bbbc8cc8ec23 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "aoe.h"
20
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020021static DEFINE_MUTEX(aoeblk_mutex);
Christoph Lametere18b8902006-12-06 20:33:20 -080022static struct kmem_cache *buf_pool_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Kay Sieversedfaa7c2007-05-21 22:08:01 +020024static ssize_t aoedisk_show_state(struct device *dev,
25 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020027 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 struct aoedev *d = disk->private_data;
29
30 return snprintf(page, PAGE_SIZE,
31 "%s%s\n",
32 (d->flags & DEVFL_UP) ? "up" : "down",
Ed L. Cashin68e0d422008-02-08 04:20:00 -080033 (d->flags & DEVFL_KICKME) ? ",kickme" :
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050034 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
35 /* I'd rather see nopen exported so we can ditch closewait */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
Kay Sieversedfaa7c2007-05-21 22:08:01 +020037static ssize_t aoedisk_show_mac(struct device *dev,
38 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020040 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 struct aoedev *d = disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080042 struct aoetgt *t = d->targets[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Ed L. Cashin68e0d422008-02-08 04:20:00 -080044 if (t == NULL)
45 return snprintf(page, PAGE_SIZE, "none\n");
Harvey Harrison411c41e2008-11-25 00:40:37 -080046 return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
Kay Sieversedfaa7c2007-05-21 22:08:01 +020048static ssize_t aoedisk_show_netif(struct device *dev,
49 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020051 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct aoedev *d = disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080053 struct net_device *nds[8], **nd, **nnd, **ne;
54 struct aoetgt **t, **te;
55 struct aoeif *ifp, *e;
56 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Ed L. Cashin68e0d422008-02-08 04:20:00 -080058 memset(nds, 0, sizeof nds);
59 nd = nds;
60 ne = nd + ARRAY_SIZE(nds);
61 t = d->targets;
62 te = t + NTARGETS;
63 for (; t < te && *t; t++) {
64 ifp = (*t)->ifs;
65 e = ifp + NAOEIFS;
66 for (; ifp < e && ifp->nd; ifp++) {
67 for (nnd = nds; nnd < nd; nnd++)
68 if (*nnd == ifp->nd)
69 break;
70 if (nnd == nd && nd != ne)
71 *nd++ = ifp->nd;
72 }
73 }
74
75 ne = nd;
76 nd = nds;
77 if (*nd == NULL)
78 return snprintf(page, PAGE_SIZE, "none\n");
79 for (p = page; nd < ne; nd++)
80 p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
81 p == page ? "" : ",", (*nd)->name);
82 p += snprintf(p, PAGE_SIZE - (p-page), "\n");
83 return p-page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
Ed L Cashin4613ed22005-04-29 10:24:25 -040085/* firmware version */
Kay Sieversedfaa7c2007-05-21 22:08:01 +020086static ssize_t aoedisk_show_fwver(struct device *dev,
87 struct device_attribute *attr, char *page)
Ed L Cashin4613ed22005-04-29 10:24:25 -040088{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020089 struct gendisk *disk = dev_to_disk(dev);
Ed L Cashin4613ed22005-04-29 10:24:25 -040090 struct aoedev *d = disk->private_data;
91
92 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
93}
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Kay Sieversedfaa7c2007-05-21 22:08:01 +020095static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
96static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
97static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
98static struct device_attribute dev_attr_firmware_version = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -070099 .attr = { .name = "firmware-version", .mode = S_IRUGO },
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200100 .show = aoedisk_show_fwver,
Ed L Cashin4613ed22005-04-29 10:24:25 -0400101};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -0700103static struct attribute *aoe_attrs[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200104 &dev_attr_state.attr,
105 &dev_attr_mac.attr,
106 &dev_attr_netif.attr,
107 &dev_attr_firmware_version.attr,
108 NULL,
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -0700109};
110
111static const struct attribute_group attr_group = {
112 .attrs = aoe_attrs,
113};
114
115static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116aoedisk_add_sysfs(struct aoedev *d)
117{
Tejun Heoed9e1982008-08-25 19:56:05 +0900118 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120void
121aoedisk_rm_sysfs(struct aoedev *d)
122{
Tejun Heoed9e1982008-08-25 19:56:05 +0900123 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126static int
Al Viro94562c12008-03-02 09:23:18 -0500127aoeblk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Al Viro94562c12008-03-02 09:23:18 -0500129 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 ulong flags;
131
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200132 mutex_lock(&aoeblk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 spin_lock_irqsave(&d->lock, flags);
134 if (d->flags & DEVFL_UP) {
135 d->nopen++;
136 spin_unlock_irqrestore(&d->lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200137 mutex_unlock(&aoeblk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 0;
139 }
140 spin_unlock_irqrestore(&d->lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200141 mutex_unlock(&aoeblk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return -ENODEV;
143}
144
145static int
Al Viro94562c12008-03-02 09:23:18 -0500146aoeblk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Al Viro94562c12008-03-02 09:23:18 -0500148 struct aoedev *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 ulong flags;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 spin_lock_irqsave(&d->lock, flags);
152
Ed L. Cashin5f7702f2006-01-19 13:46:27 -0500153 if (--d->nopen == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 spin_unlock_irqrestore(&d->lock, flags);
155 aoecmd_cfg(d->aoemajor, d->aoeminor);
156 return 0;
157 }
158 spin_unlock_irqrestore(&d->lock, flags);
159
160 return 0;
161}
162
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200163static void
Ed Cashin69cf2d852012-10-04 17:16:23 -0700164aoeblk_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 struct aoedev *d;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700167 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Ed Cashin69cf2d852012-10-04 17:16:23 -0700169 d = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if ((d->flags & DEVFL_UP) == 0) {
Andrew Morton027b1802010-10-28 06:15:26 -0600171 pr_info_ratelimited("aoe: device %ld.%d is not up\n",
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400172 d->aoemajor, d->aoeminor);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700173 while ((rq = blk_peek_request(q))) {
174 blk_start_request(rq);
175 aoe_end_request(d, rq, 1);
176 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200177 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500179 aoecmd_work(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800183aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800185 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400188 printk(KERN_ERR "aoe: disk not up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -ENODEV;
190 }
191
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800192 geo->cylinders = d->geo.cylinders;
193 geo->heads = d->geo.heads;
194 geo->sectors = d->geo.sectors;
195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700198static const struct block_device_operations aoe_bdops = {
Al Viro94562c12008-03-02 09:23:18 -0500199 .open = aoeblk_open,
200 .release = aoeblk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800201 .getgeo = aoeblk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 .owner = THIS_MODULE,
203};
204
205/* alloc_disk and add_disk can sleep */
206void
207aoeblk_gdalloc(void *vp)
208{
209 struct aoedev *d = vp;
210 struct gendisk *gd;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700211 mempool_t *mp;
212 struct request_queue *q;
213 enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 ulong flags;
215
216 gd = alloc_disk(AOE_PARTITIONS);
217 if (gd == NULL) {
Ed Cashin69cf2d852012-10-04 17:16:23 -0700218 pr_err("aoe: cannot allocate disk structure for %ld.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400219 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800220 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222
Ed Cashin69cf2d852012-10-04 17:16:23 -0700223 mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab,
224 buf_pool_cache);
225 if (mp == NULL) {
Ed L. Cashin1d759812008-02-08 04:20:08 -0800226 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400227 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800228 goto err_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Ed Cashin69cf2d852012-10-04 17:16:23 -0700230 q = blk_init_queue(aoeblk_request, &d->lock);
231 if (q == NULL) {
232 pr_err("aoe: cannot allocate block queue for %ld.%d\n",
233 d->aoemajor, d->aoeminor);
234 mempool_destroy(mp);
235 goto err_disk;
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Ed Cashin7135a71b2009-09-09 14:10:18 +0200238 d->blkq = blk_alloc_queue(GFP_KERNEL);
239 if (!d->blkq)
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800240 goto err_mempool;
Jens Axboed9938312009-06-12 14:45:52 +0200241 d->blkq->backing_dev_info.name = "aoe";
Ed Cashin7135a71b2009-09-09 14:10:18 +0200242 if (bdi_init(&d->blkq->backing_dev_info))
243 goto err_blkq;
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800244 spin_lock_irqsave(&d->lock, flags);
Ed Cashin3d5b0602012-10-04 17:16:20 -0700245 blk_queue_max_hw_sectors(d->blkq, BLK_DEF_MAX_SECTORS);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700246 q->backing_dev_info.ra_pages = READ_AHEAD / PAGE_CACHE_SIZE;
247 d->bufpool = mp;
248 d->blkq = gd->queue = q;
249 q->queuedata = d;
250 d->gd = gd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 gd->major = AOE_MAJOR;
Ed Cashin0c966212012-10-04 17:16:40 -0700252 gd->first_minor = d->sysminor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 gd->fops = &aoe_bdops;
254 gd->private_data = d;
Tejun Heo80795ae2008-08-25 19:56:07 +0900255 set_capacity(gd, d->ssize);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800256 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 d->aoemajor, d->aoeminor);
258
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500259 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 d->flags |= DEVFL_UP;
261
262 spin_unlock_irqrestore(&d->lock, flags);
263
264 add_disk(gd);
265 aoedisk_add_sysfs(d);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800266 return;
267
Ed Cashin7135a71b2009-09-09 14:10:18 +0200268err_blkq:
269 blk_cleanup_queue(d->blkq);
270 d->blkq = NULL;
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800271err_mempool:
272 mempool_destroy(d->bufpool);
273err_disk:
274 put_disk(gd);
275err:
276 spin_lock_irqsave(&d->lock, flags);
277 d->flags &= ~DEVFL_GDALLOC;
278 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281void
282aoeblk_exit(void)
283{
284 kmem_cache_destroy(buf_pool_cache);
285}
286
287int __init
288aoeblk_init(void)
289{
Paul Mundt20c2df82007-07-20 10:11:58 +0900290 buf_pool_cache = kmem_cache_create("aoe_bufs",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 sizeof(struct buf),
Paul Mundt20c2df82007-07-20 10:11:58 +0900292 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (buf_pool_cache == NULL)
294 return -ENOMEM;
295
296 return 0;
297}
298