blob: 541e18879965d01e74081701b50f3e70c78fb1b5 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "aoe.h"
19
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020020static DEFINE_MUTEX(aoeblk_mutex);
Christoph Lametere18b8902006-12-06 20:33:20 -080021static struct kmem_cache *buf_pool_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Kay Sieversedfaa7c2007-05-21 22:08:01 +020023static ssize_t aoedisk_show_state(struct device *dev,
24 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020026 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 struct aoedev *d = disk->private_data;
28
29 return snprintf(page, PAGE_SIZE,
30 "%s%s\n",
31 (d->flags & DEVFL_UP) ? "up" : "down",
Ed L. Cashin68e0d422008-02-08 04:20:00 -080032 (d->flags & DEVFL_KICKME) ? ",kickme" :
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050033 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
34 /* I'd rather see nopen exported so we can ditch closewait */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
Kay Sieversedfaa7c2007-05-21 22:08:01 +020036static ssize_t aoedisk_show_mac(struct device *dev,
37 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020039 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct aoedev *d = disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080041 struct aoetgt *t = d->targets[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Ed L. Cashin68e0d422008-02-08 04:20:00 -080043 if (t == NULL)
44 return snprintf(page, PAGE_SIZE, "none\n");
Harvey Harrison411c41e2008-11-25 00:40:37 -080045 return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
Kay Sieversedfaa7c2007-05-21 22:08:01 +020047static ssize_t aoedisk_show_netif(struct device *dev,
48 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020050 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 struct aoedev *d = disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080052 struct net_device *nds[8], **nd, **nnd, **ne;
53 struct aoetgt **t, **te;
54 struct aoeif *ifp, *e;
55 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Ed L. Cashin68e0d422008-02-08 04:20:00 -080057 memset(nds, 0, sizeof nds);
58 nd = nds;
59 ne = nd + ARRAY_SIZE(nds);
60 t = d->targets;
61 te = t + NTARGETS;
62 for (; t < te && *t; t++) {
63 ifp = (*t)->ifs;
64 e = ifp + NAOEIFS;
65 for (; ifp < e && ifp->nd; ifp++) {
66 for (nnd = nds; nnd < nd; nnd++)
67 if (*nnd == ifp->nd)
68 break;
69 if (nnd == nd && nd != ne)
70 *nd++ = ifp->nd;
71 }
72 }
73
74 ne = nd;
75 nd = nds;
76 if (*nd == NULL)
77 return snprintf(page, PAGE_SIZE, "none\n");
78 for (p = page; nd < ne; nd++)
79 p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
80 p == page ? "" : ",", (*nd)->name);
81 p += snprintf(p, PAGE_SIZE - (p-page), "\n");
82 return p-page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
Ed L Cashin4613ed22005-04-29 10:24:25 -040084/* firmware version */
Kay Sieversedfaa7c2007-05-21 22:08:01 +020085static ssize_t aoedisk_show_fwver(struct device *dev,
86 struct device_attribute *attr, char *page)
Ed L Cashin4613ed22005-04-29 10:24:25 -040087{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020088 struct gendisk *disk = dev_to_disk(dev);
Ed L Cashin4613ed22005-04-29 10:24:25 -040089 struct aoedev *d = disk->private_data;
90
91 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
92}
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Kay Sieversedfaa7c2007-05-21 22:08:01 +020094static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
95static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
96static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
97static struct device_attribute dev_attr_firmware_version = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -070098 .attr = { .name = "firmware-version", .mode = S_IRUGO },
Kay Sieversedfaa7c2007-05-21 22:08:01 +020099 .show = aoedisk_show_fwver,
Ed L Cashin4613ed22005-04-29 10:24:25 -0400100};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -0700102static struct attribute *aoe_attrs[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200103 &dev_attr_state.attr,
104 &dev_attr_mac.attr,
105 &dev_attr_netif.attr,
106 &dev_attr_firmware_version.attr,
107 NULL,
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -0700108};
109
110static const struct attribute_group attr_group = {
111 .attrs = aoe_attrs,
112};
113
114static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115aoedisk_add_sysfs(struct aoedev *d)
116{
Tejun Heoed9e1982008-08-25 19:56:05 +0900117 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119void
120aoedisk_rm_sysfs(struct aoedev *d)
121{
Tejun Heoed9e1982008-08-25 19:56:05 +0900122 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125static int
Al Viro94562c12008-03-02 09:23:18 -0500126aoeblk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Al Viro94562c12008-03-02 09:23:18 -0500128 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 ulong flags;
130
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200131 mutex_lock(&aoeblk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 spin_lock_irqsave(&d->lock, flags);
133 if (d->flags & DEVFL_UP) {
134 d->nopen++;
135 spin_unlock_irqrestore(&d->lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200136 mutex_unlock(&aoeblk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return 0;
138 }
139 spin_unlock_irqrestore(&d->lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200140 mutex_unlock(&aoeblk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return -ENODEV;
142}
143
144static int
Al Viro94562c12008-03-02 09:23:18 -0500145aoeblk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Al Viro94562c12008-03-02 09:23:18 -0500147 struct aoedev *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 ulong flags;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 spin_lock_irqsave(&d->lock, flags);
151
Ed L. Cashin5f7702f2006-01-19 13:46:27 -0500152 if (--d->nopen == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 spin_unlock_irqrestore(&d->lock, flags);
154 aoecmd_cfg(d->aoemajor, d->aoeminor);
155 return 0;
156 }
157 spin_unlock_irqrestore(&d->lock, flags);
158
159 return 0;
160}
161
162static int
Jens Axboe165125e2007-07-24 09:28:11 +0200163aoeblk_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
David S. Millere9bb8fb02008-09-21 22:36:49 -0700165 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct aoedev *d;
167 struct buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 ulong flags;
169
170 blk_queue_bounce(q, &bio);
171
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800172 if (bio == NULL) {
173 printk(KERN_ERR "aoe: bio is NULL\n");
174 BUG();
175 return 0;
176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 d = bio->bi_bdev->bd_disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800178 if (d == NULL) {
179 printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n");
180 BUG();
181 bio_endio(bio, -ENXIO);
182 return 0;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200183 } else if (bio->bi_rw & REQ_HARDBARRIER) {
Ed Cashin18d82172009-09-10 22:30:47 +0200184 bio_endio(bio, -EOPNOTSUPP);
185 return 0;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800186 } else if (bio->bi_io_vec == NULL) {
187 printk(KERN_ERR "aoe: bi_io_vec is NULL\n");
188 BUG();
189 bio_endio(bio, -ENXIO);
190 return 0;
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 buf = mempool_alloc(d->bufpool, GFP_NOIO);
193 if (buf == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400194 printk(KERN_INFO "aoe: buf allocation failure\n");
NeilBrown6712ecf2007-09-27 12:47:43 +0200195 bio_endio(bio, -ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return 0;
197 }
198 memset(buf, 0, sizeof(*buf));
199 INIT_LIST_HEAD(&buf->bufs);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800200 buf->stime = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 buf->bio = bio;
202 buf->resid = bio->bi_size;
203 buf->sector = bio->bi_sector;
Ed L. Cashin392e4842006-09-20 14:36:50 -0400204 buf->bv = &bio->bi_io_vec[bio->bi_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 buf->bv_resid = buf->bv->bv_len;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800206 WARN_ON(buf->bv_resid == 0);
207 buf->bv_off = buf->bv->bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 spin_lock_irqsave(&d->lock, flags);
210
211 if ((d->flags & DEVFL_UP) == 0) {
Andrew Morton027b1802010-10-28 06:15:26 -0600212 pr_info_ratelimited("aoe: device %ld.%d is not up\n",
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400213 d->aoemajor, d->aoeminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 spin_unlock_irqrestore(&d->lock, flags);
215 mempool_free(buf, d->bufpool);
NeilBrown6712ecf2007-09-27 12:47:43 +0200216 bio_endio(bio, -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218 }
219
220 list_add_tail(&buf->bufs, &d->bufq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500222 aoecmd_work(d);
David S. Millere9bb8fb02008-09-21 22:36:49 -0700223 __skb_queue_head_init(&queue);
224 skb_queue_splice_init(&d->sendq, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 spin_unlock_irqrestore(&d->lock, flags);
David S. Millere9bb8fb02008-09-21 22:36:49 -0700227 aoenet_xmit(&queue);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 0;
230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800233aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800235 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400238 printk(KERN_ERR "aoe: disk not up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return -ENODEV;
240 }
241
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800242 geo->cylinders = d->geo.cylinders;
243 geo->heads = d->geo.heads;
244 geo->sectors = d->geo.sectors;
245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700248static const struct block_device_operations aoe_bdops = {
Al Viro94562c12008-03-02 09:23:18 -0500249 .open = aoeblk_open,
250 .release = aoeblk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800251 .getgeo = aoeblk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 .owner = THIS_MODULE,
253};
254
255/* alloc_disk and add_disk can sleep */
256void
257aoeblk_gdalloc(void *vp)
258{
259 struct aoedev *d = vp;
260 struct gendisk *gd;
261 ulong flags;
262
263 gd = alloc_disk(AOE_PARTITIONS);
264 if (gd == NULL) {
Ed L. Cashin1d759812008-02-08 04:20:08 -0800265 printk(KERN_ERR
266 "aoe: cannot allocate disk structure for %ld.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400267 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800268 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
Matthew Dobson93d23412006-03-26 01:37:50 -0800271 d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (d->bufpool == NULL) {
Ed L. Cashin1d759812008-02-08 04:20:08 -0800273 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400274 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800275 goto err_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277
Ed Cashin7135a71b2009-09-09 14:10:18 +0200278 d->blkq = blk_alloc_queue(GFP_KERNEL);
279 if (!d->blkq)
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800280 goto err_mempool;
Ed Cashin7135a71b2009-09-09 14:10:18 +0200281 blk_queue_make_request(d->blkq, aoeblk_make_request);
Jens Axboed9938312009-06-12 14:45:52 +0200282 d->blkq->backing_dev_info.name = "aoe";
Ed Cashin7135a71b2009-09-09 14:10:18 +0200283 if (bdi_init(&d->blkq->backing_dev_info))
284 goto err_blkq;
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800285 spin_lock_irqsave(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 gd->major = AOE_MAJOR;
287 gd->first_minor = d->sysminor * AOE_PARTITIONS;
288 gd->fops = &aoe_bdops;
289 gd->private_data = d;
Tejun Heo80795ae2008-08-25 19:56:07 +0900290 set_capacity(gd, d->ssize);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800291 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 d->aoemajor, d->aoeminor);
293
Ed Cashin7135a71b2009-09-09 14:10:18 +0200294 gd->queue = d->blkq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 d->gd = gd;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500296 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 d->flags |= DEVFL_UP;
298
299 spin_unlock_irqrestore(&d->lock, flags);
300
301 add_disk(gd);
302 aoedisk_add_sysfs(d);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800303 return;
304
Ed Cashin7135a71b2009-09-09 14:10:18 +0200305err_blkq:
306 blk_cleanup_queue(d->blkq);
307 d->blkq = NULL;
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800308err_mempool:
309 mempool_destroy(d->bufpool);
310err_disk:
311 put_disk(gd);
312err:
313 spin_lock_irqsave(&d->lock, flags);
314 d->flags &= ~DEVFL_GDALLOC;
315 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
318void
319aoeblk_exit(void)
320{
321 kmem_cache_destroy(buf_pool_cache);
322}
323
324int __init
325aoeblk_init(void)
326{
Paul Mundt20c2df82007-07-20 10:11:58 +0900327 buf_pool_cache = kmem_cache_create("aoe_bufs",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 sizeof(struct buf),
Paul Mundt20c2df82007-07-20 10:11:58 +0900329 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (buf_pool_cache == NULL)
331 return -ENOMEM;
332
333 return 0;
334}
335