blob: 321de7b6c44228e5b7c5cfc0a410be19a54759fe [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
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
Jens Axboe165125e2007-07-24 09:28:11 +0200164aoeblk_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700166 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct aoedev *d;
168 struct buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 ulong flags;
170
171 blk_queue_bounce(q, &bio);
172
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800173 if (bio == NULL) {
174 printk(KERN_ERR "aoe: bio is NULL\n");
175 BUG();
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200176 return;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 d = bio->bi_bdev->bd_disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800179 if (d == NULL) {
180 printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n");
181 BUG();
182 bio_endio(bio, -ENXIO);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200183 return;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800184 } else if (bio->bi_io_vec == NULL) {
185 printk(KERN_ERR "aoe: bi_io_vec is NULL\n");
186 BUG();
187 bio_endio(bio, -ENXIO);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200188 return;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 buf = mempool_alloc(d->bufpool, GFP_NOIO);
191 if (buf == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400192 printk(KERN_INFO "aoe: buf allocation failure\n");
NeilBrown6712ecf2007-09-27 12:47:43 +0200193 bio_endio(bio, -ENOMEM);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200194 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 memset(buf, 0, sizeof(*buf));
197 INIT_LIST_HEAD(&buf->bufs);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800198 buf->stime = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 buf->bio = bio;
200 buf->resid = bio->bi_size;
201 buf->sector = bio->bi_sector;
Ed L. Cashin392e4842006-09-20 14:36:50 -0400202 buf->bv = &bio->bi_io_vec[bio->bi_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 buf->bv_resid = buf->bv->bv_len;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800204 WARN_ON(buf->bv_resid == 0);
205 buf->bv_off = buf->bv->bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 spin_lock_irqsave(&d->lock, flags);
208
209 if ((d->flags & DEVFL_UP) == 0) {
Andrew Morton027b1802010-10-28 06:15:26 -0600210 pr_info_ratelimited("aoe: device %ld.%d is not up\n",
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400211 d->aoemajor, d->aoeminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 spin_unlock_irqrestore(&d->lock, flags);
213 mempool_free(buf, d->bufpool);
NeilBrown6712ecf2007-09-27 12:47:43 +0200214 bio_endio(bio, -ENXIO);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200215 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
218 list_add_tail(&buf->bufs, &d->bufq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500220 aoecmd_work(d);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700221 __skb_queue_head_init(&queue);
222 skb_queue_splice_init(&d->sendq, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 spin_unlock_irqrestore(&d->lock, flags);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700225 aoenet_xmit(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800229aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800231 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400234 printk(KERN_ERR "aoe: disk not up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -ENODEV;
236 }
237
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800238 geo->cylinders = d->geo.cylinders;
239 geo->heads = d->geo.heads;
240 geo->sectors = d->geo.sectors;
241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700244static const struct block_device_operations aoe_bdops = {
Al Viro94562c12008-03-02 09:23:18 -0500245 .open = aoeblk_open,
246 .release = aoeblk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800247 .getgeo = aoeblk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .owner = THIS_MODULE,
249};
250
251/* alloc_disk and add_disk can sleep */
252void
253aoeblk_gdalloc(void *vp)
254{
255 struct aoedev *d = vp;
256 struct gendisk *gd;
257 ulong flags;
258
259 gd = alloc_disk(AOE_PARTITIONS);
260 if (gd == NULL) {
Ed L. Cashin1d759812008-02-08 04:20:08 -0800261 printk(KERN_ERR
262 "aoe: cannot allocate disk structure for %ld.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400263 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800264 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
Matthew Dobson93d23412006-03-26 01:37:50 -0800267 d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (d->bufpool == NULL) {
Ed L. Cashin1d759812008-02-08 04:20:08 -0800269 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400270 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800271 goto err_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
Ed Cashin7135a71b2009-09-09 14:10:18 +0200274 d->blkq = blk_alloc_queue(GFP_KERNEL);
275 if (!d->blkq)
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800276 goto err_mempool;
Ed Cashin7135a71b2009-09-09 14:10:18 +0200277 blk_queue_make_request(d->blkq, aoeblk_make_request);
Jens Axboed9938312009-06-12 14:45:52 +0200278 d->blkq->backing_dev_info.name = "aoe";
Ed Cashin7135a71b2009-09-09 14:10:18 +0200279 if (bdi_init(&d->blkq->backing_dev_info))
280 goto err_blkq;
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800281 spin_lock_irqsave(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 gd->major = AOE_MAJOR;
283 gd->first_minor = d->sysminor * AOE_PARTITIONS;
284 gd->fops = &aoe_bdops;
285 gd->private_data = d;
Tejun Heo80795ae2008-08-25 19:56:07 +0900286 set_capacity(gd, d->ssize);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800287 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 d->aoemajor, d->aoeminor);
289
Ed Cashin7135a71b2009-09-09 14:10:18 +0200290 gd->queue = d->blkq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 d->gd = gd;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500292 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 d->flags |= DEVFL_UP;
294
295 spin_unlock_irqrestore(&d->lock, flags);
296
297 add_disk(gd);
298 aoedisk_add_sysfs(d);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800299 return;
300
Ed Cashin7135a71b2009-09-09 14:10:18 +0200301err_blkq:
302 blk_cleanup_queue(d->blkq);
303 d->blkq = NULL;
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800304err_mempool:
305 mempool_destroy(d->bufpool);
306err_disk:
307 put_disk(gd);
308err:
309 spin_lock_irqsave(&d->lock, flags);
310 d->flags &= ~DEVFL_GDALLOC;
311 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314void
315aoeblk_exit(void)
316{
317 kmem_cache_destroy(buf_pool_cache);
318}
319
320int __init
321aoeblk_init(void)
322{
Paul Mundt20c2df82007-07-20 10:11:58 +0900323 buf_pool_cache = kmem_cache_create("aoe_bufs",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 sizeof(struct buf),
Paul Mundt20c2df82007-07-20 10:11:58 +0900325 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (buf_pool_cache == NULL)
327 return -ENOMEM;
328
329 return 0;
330}
331