blob: 1747dd272cd476bd1c14b03be0ee0c8f5faf17dd [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
7#include <linux/hdreg.h>
8#include <linux/blkdev.h>
Andrew Morton43cbe2c2007-12-10 15:49:13 -08009#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/fs.h>
11#include <linux/ioctl.h>
12#include <linux/genhd.h>
13#include <linux/netdevice.h>
14#include "aoe.h"
15
Christoph Lametere18b8902006-12-06 20:33:20 -080016static struct kmem_cache *buf_pool_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Kay Sieversedfaa7c2007-05-21 22:08:01 +020018static ssize_t aoedisk_show_state(struct device *dev,
19 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020021 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 struct aoedev *d = disk->private_data;
23
24 return snprintf(page, PAGE_SIZE,
25 "%s%s\n",
26 (d->flags & DEVFL_UP) ? "up" : "down",
Ed L. Cashin68e0d422008-02-08 04:20:00 -080027 (d->flags & DEVFL_KICKME) ? ",kickme" :
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050028 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
29 /* I'd rather see nopen exported so we can ditch closewait */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030}
Kay Sieversedfaa7c2007-05-21 22:08:01 +020031static ssize_t aoedisk_show_mac(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;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080036 struct aoetgt *t = d->targets[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Ed L. Cashin68e0d422008-02-08 04:20:00 -080038 if (t == NULL)
39 return snprintf(page, PAGE_SIZE, "none\n");
Ed L. Cashin1eb0da42008-02-08 04:20:01 -080040 return snprintf(page, PAGE_SIZE, "%012llx\n", mac_addr(t->addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
Kay Sieversedfaa7c2007-05-21 22:08:01 +020042static ssize_t aoedisk_show_netif(struct device *dev,
43 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020045 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct aoedev *d = disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080047 struct net_device *nds[8], **nd, **nnd, **ne;
48 struct aoetgt **t, **te;
49 struct aoeif *ifp, *e;
50 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Ed L. Cashin68e0d422008-02-08 04:20:00 -080052 memset(nds, 0, sizeof nds);
53 nd = nds;
54 ne = nd + ARRAY_SIZE(nds);
55 t = d->targets;
56 te = t + NTARGETS;
57 for (; t < te && *t; t++) {
58 ifp = (*t)->ifs;
59 e = ifp + NAOEIFS;
60 for (; ifp < e && ifp->nd; ifp++) {
61 for (nnd = nds; nnd < nd; nnd++)
62 if (*nnd == ifp->nd)
63 break;
64 if (nnd == nd && nd != ne)
65 *nd++ = ifp->nd;
66 }
67 }
68
69 ne = nd;
70 nd = nds;
71 if (*nd == NULL)
72 return snprintf(page, PAGE_SIZE, "none\n");
73 for (p = page; nd < ne; nd++)
74 p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
75 p == page ? "" : ",", (*nd)->name);
76 p += snprintf(p, PAGE_SIZE - (p-page), "\n");
77 return p-page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
Ed L Cashin4613ed22005-04-29 10:24:25 -040079/* firmware version */
Kay Sieversedfaa7c2007-05-21 22:08:01 +020080static ssize_t aoedisk_show_fwver(struct device *dev,
81 struct device_attribute *attr, char *page)
Ed L Cashin4613ed22005-04-29 10:24:25 -040082{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020083 struct gendisk *disk = dev_to_disk(dev);
Ed L Cashin4613ed22005-04-29 10:24:25 -040084 struct aoedev *d = disk->private_data;
85
86 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
87}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Kay Sieversedfaa7c2007-05-21 22:08:01 +020089static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
90static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
91static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
92static struct device_attribute dev_attr_firmware_version = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -070093 .attr = { .name = "firmware-version", .mode = S_IRUGO },
Kay Sieversedfaa7c2007-05-21 22:08:01 +020094 .show = aoedisk_show_fwver,
Ed L Cashin4613ed22005-04-29 10:24:25 -040095};
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -070097static struct attribute *aoe_attrs[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +020098 &dev_attr_state.attr,
99 &dev_attr_mac.attr,
100 &dev_attr_netif.attr,
101 &dev_attr_firmware_version.attr,
102 NULL,
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -0700103};
104
105static const struct attribute_group attr_group = {
106 .attrs = aoe_attrs,
107};
108
109static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110aoedisk_add_sysfs(struct aoedev *d)
111{
Tejun Heoed9e1982008-08-25 19:56:05 +0900112 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114void
115aoedisk_rm_sysfs(struct aoedev *d)
116{
Tejun Heoed9e1982008-08-25 19:56:05 +0900117 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120static int
Al Viro94562c12008-03-02 09:23:18 -0500121aoeblk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Al Viro94562c12008-03-02 09:23:18 -0500123 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 ulong flags;
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 spin_lock_irqsave(&d->lock, flags);
127 if (d->flags & DEVFL_UP) {
128 d->nopen++;
129 spin_unlock_irqrestore(&d->lock, flags);
130 return 0;
131 }
132 spin_unlock_irqrestore(&d->lock, flags);
133 return -ENODEV;
134}
135
136static int
Al Viro94562c12008-03-02 09:23:18 -0500137aoeblk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Al Viro94562c12008-03-02 09:23:18 -0500139 struct aoedev *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 ulong flags;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 spin_lock_irqsave(&d->lock, flags);
143
Ed L. Cashin5f7702f2006-01-19 13:46:27 -0500144 if (--d->nopen == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 spin_unlock_irqrestore(&d->lock, flags);
146 aoecmd_cfg(d->aoemajor, d->aoeminor);
147 return 0;
148 }
149 spin_unlock_irqrestore(&d->lock, flags);
150
151 return 0;
152}
153
154static int
Jens Axboe165125e2007-07-24 09:28:11 +0200155aoeblk_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700157 struct sk_buff_head queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 struct aoedev *d;
159 struct buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 ulong flags;
161
162 blk_queue_bounce(q, &bio);
163
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800164 if (bio == NULL) {
165 printk(KERN_ERR "aoe: bio is NULL\n");
166 BUG();
167 return 0;
168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 d = bio->bi_bdev->bd_disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800170 if (d == NULL) {
171 printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n");
172 BUG();
173 bio_endio(bio, -ENXIO);
174 return 0;
175 } else if (bio->bi_io_vec == NULL) {
176 printk(KERN_ERR "aoe: bi_io_vec is NULL\n");
177 BUG();
178 bio_endio(bio, -ENXIO);
179 return 0;
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 buf = mempool_alloc(d->bufpool, GFP_NOIO);
182 if (buf == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400183 printk(KERN_INFO "aoe: buf allocation failure\n");
NeilBrown6712ecf2007-09-27 12:47:43 +0200184 bio_endio(bio, -ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return 0;
186 }
187 memset(buf, 0, sizeof(*buf));
188 INIT_LIST_HEAD(&buf->bufs);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800189 buf->stime = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 buf->bio = bio;
191 buf->resid = bio->bi_size;
192 buf->sector = bio->bi_sector;
Ed L. Cashin392e4842006-09-20 14:36:50 -0400193 buf->bv = &bio->bi_io_vec[bio->bi_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 buf->bv_resid = buf->bv->bv_len;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800195 WARN_ON(buf->bv_resid == 0);
196 buf->bv_off = buf->bv->bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 spin_lock_irqsave(&d->lock, flags);
199
200 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashin1d759812008-02-08 04:20:08 -0800201 printk(KERN_INFO "aoe: device %ld.%d is not up\n",
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400202 d->aoemajor, d->aoeminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 spin_unlock_irqrestore(&d->lock, flags);
204 mempool_free(buf, d->bufpool);
NeilBrown6712ecf2007-09-27 12:47:43 +0200205 bio_endio(bio, -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 0;
207 }
208
209 list_add_tail(&buf->bufs, &d->bufq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500211 aoecmd_work(d);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700212 __skb_queue_head_init(&queue);
213 skb_queue_splice_init(&d->sendq, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 spin_unlock_irqrestore(&d->lock, flags);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700216 aoenet_xmit(&queue);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800222aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800224 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400227 printk(KERN_ERR "aoe: disk not up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return -ENODEV;
229 }
230
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800231 geo->cylinders = d->geo.cylinders;
232 geo->heads = d->geo.heads;
233 geo->sectors = d->geo.sectors;
234 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237static struct block_device_operations aoe_bdops = {
Al Viro94562c12008-03-02 09:23:18 -0500238 .open = aoeblk_open,
239 .release = aoeblk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800240 .getgeo = aoeblk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .owner = THIS_MODULE,
242};
243
244/* alloc_disk and add_disk can sleep */
245void
246aoeblk_gdalloc(void *vp)
247{
248 struct aoedev *d = vp;
249 struct gendisk *gd;
250 ulong flags;
251
252 gd = alloc_disk(AOE_PARTITIONS);
253 if (gd == NULL) {
Ed L. Cashin1d759812008-02-08 04:20:08 -0800254 printk(KERN_ERR
255 "aoe: cannot allocate disk structure for %ld.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400256 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800257 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
Matthew Dobson93d23412006-03-26 01:37:50 -0800260 d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (d->bufpool == NULL) {
Ed L. Cashin1d759812008-02-08 04:20:08 -0800262 printk(KERN_ERR "aoe: cannot allocate bufpool 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_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 blk_queue_make_request(&d->blkq, aoeblk_make_request);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800268 if (bdi_init(&d->blkq.backing_dev_info))
269 goto err_mempool;
270 spin_lock_irqsave(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 gd->major = AOE_MAJOR;
272 gd->first_minor = d->sysminor * AOE_PARTITIONS;
273 gd->fops = &aoe_bdops;
274 gd->private_data = d;
Tejun Heo80795ae2008-08-25 19:56:07 +0900275 set_capacity(gd, d->ssize);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800276 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 d->aoemajor, d->aoeminor);
278
279 gd->queue = &d->blkq;
280 d->gd = gd;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500281 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 d->flags |= DEVFL_UP;
283
284 spin_unlock_irqrestore(&d->lock, flags);
285
286 add_disk(gd);
287 aoedisk_add_sysfs(d);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800288 return;
289
290err_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