blob: c2649c954278e01a888332e2b4e1fdf22142ab24 [file] [log] [blame]
Ed L. Cashin26114642006-09-20 14:36:48 -04001/* Copyright (c) 2006 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 return snprintf(page, PAGE_SIZE, "%012llx\n",
Ed L. Cashin68e0d422008-02-08 04:20:00 -080041 (unsigned long long)mac_addr(t->addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
Kay Sieversedfaa7c2007-05-21 22:08:01 +020043static ssize_t aoedisk_show_netif(struct device *dev,
44 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020046 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct aoedev *d = disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080048 struct net_device *nds[8], **nd, **nnd, **ne;
49 struct aoetgt **t, **te;
50 struct aoeif *ifp, *e;
51 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Ed L. Cashin68e0d422008-02-08 04:20:00 -080053 memset(nds, 0, sizeof nds);
54 nd = nds;
55 ne = nd + ARRAY_SIZE(nds);
56 t = d->targets;
57 te = t + NTARGETS;
58 for (; t < te && *t; t++) {
59 ifp = (*t)->ifs;
60 e = ifp + NAOEIFS;
61 for (; ifp < e && ifp->nd; ifp++) {
62 for (nnd = nds; nnd < nd; nnd++)
63 if (*nnd == ifp->nd)
64 break;
65 if (nnd == nd && nd != ne)
66 *nd++ = ifp->nd;
67 }
68 }
69
70 ne = nd;
71 nd = nds;
72 if (*nd == NULL)
73 return snprintf(page, PAGE_SIZE, "none\n");
74 for (p = page; nd < ne; nd++)
75 p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
76 p == page ? "" : ",", (*nd)->name);
77 p += snprintf(p, PAGE_SIZE - (p-page), "\n");
78 return p-page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
Ed L Cashin4613ed22005-04-29 10:24:25 -040080/* firmware version */
Kay Sieversedfaa7c2007-05-21 22:08:01 +020081static ssize_t aoedisk_show_fwver(struct device *dev,
82 struct device_attribute *attr, char *page)
Ed L Cashin4613ed22005-04-29 10:24:25 -040083{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020084 struct gendisk *disk = dev_to_disk(dev);
Ed L Cashin4613ed22005-04-29 10:24:25 -040085 struct aoedev *d = disk->private_data;
86
87 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
88}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Kay Sieversedfaa7c2007-05-21 22:08:01 +020090static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
91static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
92static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
93static struct device_attribute dev_attr_firmware_version = {
94 .attr = { .name = "firmware-version", .mode = S_IRUGO, .owner = THIS_MODULE },
95 .show = aoedisk_show_fwver,
Ed L Cashin4613ed22005-04-29 10:24:25 -040096};
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -070098static struct attribute *aoe_attrs[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +020099 &dev_attr_state.attr,
100 &dev_attr_mac.attr,
101 &dev_attr_netif.attr,
102 &dev_attr_firmware_version.attr,
103 NULL,
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -0700104};
105
106static const struct attribute_group attr_group = {
107 .attrs = aoe_attrs,
108};
109
110static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111aoedisk_add_sysfs(struct aoedev *d)
112{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200113 return sysfs_create_group(&d->gd->dev.kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115void
116aoedisk_rm_sysfs(struct aoedev *d)
117{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200118 sysfs_remove_group(&d->gd->dev.kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121static int
122aoeblk_open(struct inode *inode, struct file *filp)
123{
124 struct aoedev *d;
125 ulong flags;
126
127 d = inode->i_bdev->bd_disk->private_data;
128
129 spin_lock_irqsave(&d->lock, flags);
130 if (d->flags & DEVFL_UP) {
131 d->nopen++;
132 spin_unlock_irqrestore(&d->lock, flags);
133 return 0;
134 }
135 spin_unlock_irqrestore(&d->lock, flags);
136 return -ENODEV;
137}
138
139static int
140aoeblk_release(struct inode *inode, struct file *filp)
141{
142 struct aoedev *d;
143 ulong flags;
144
145 d = inode->i_bdev->bd_disk->private_data;
146
147 spin_lock_irqsave(&d->lock, flags);
148
Ed L. Cashin5f7702f2006-01-19 13:46:27 -0500149 if (--d->nopen == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 spin_unlock_irqrestore(&d->lock, flags);
151 aoecmd_cfg(d->aoemajor, d->aoeminor);
152 return 0;
153 }
154 spin_unlock_irqrestore(&d->lock, flags);
155
156 return 0;
157}
158
159static int
Jens Axboe165125e2007-07-24 09:28:11 +0200160aoeblk_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct aoedev *d;
163 struct buf *buf;
164 struct sk_buff *sl;
165 ulong flags;
166
167 blk_queue_bounce(q, &bio);
168
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800169 if (bio == NULL) {
170 printk(KERN_ERR "aoe: bio is NULL\n");
171 BUG();
172 return 0;
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 d = bio->bi_bdev->bd_disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800175 if (d == NULL) {
176 printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n");
177 BUG();
178 bio_endio(bio, -ENXIO);
179 return 0;
180 } else if (bio->bi_io_vec == NULL) {
181 printk(KERN_ERR "aoe: bi_io_vec is NULL\n");
182 BUG();
183 bio_endio(bio, -ENXIO);
184 return 0;
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 buf = mempool_alloc(d->bufpool, GFP_NOIO);
187 if (buf == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400188 printk(KERN_INFO "aoe: buf allocation failure\n");
NeilBrown6712ecf2007-09-27 12:47:43 +0200189 bio_endio(bio, -ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return 0;
191 }
192 memset(buf, 0, sizeof(*buf));
193 INIT_LIST_HEAD(&buf->bufs);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800194 buf->stime = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 buf->bio = bio;
196 buf->resid = bio->bi_size;
197 buf->sector = bio->bi_sector;
Ed L. Cashin392e4842006-09-20 14:36:50 -0400198 buf->bv = &bio->bi_io_vec[bio->bi_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 buf->bv_resid = buf->bv->bv_len;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800200 WARN_ON(buf->bv_resid == 0);
201 buf->bv_off = buf->bv->bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 spin_lock_irqsave(&d->lock, flags);
204
205 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400206 printk(KERN_INFO "aoe: device %ld.%ld is not up\n",
207 d->aoemajor, d->aoeminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 spin_unlock_irqrestore(&d->lock, flags);
209 mempool_free(buf, d->bufpool);
NeilBrown6712ecf2007-09-27 12:47:43 +0200210 bio_endio(bio, -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 0;
212 }
213
214 list_add_tail(&buf->bufs, &d->bufq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500216 aoecmd_work(d);
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700217 sl = d->sendq_hd;
218 d->sendq_hd = d->sendq_tl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 aoenet_xmit(sl);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return 0;
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800227aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800229 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400232 printk(KERN_ERR "aoe: disk not up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -ENODEV;
234 }
235
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800236 geo->cylinders = d->geo.cylinders;
237 geo->heads = d->geo.heads;
238 geo->sectors = d->geo.sectors;
239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242static struct block_device_operations aoe_bdops = {
243 .open = aoeblk_open,
244 .release = aoeblk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800245 .getgeo = aoeblk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 .owner = THIS_MODULE,
247};
248
249/* alloc_disk and add_disk can sleep */
250void
251aoeblk_gdalloc(void *vp)
252{
253 struct aoedev *d = vp;
254 struct gendisk *gd;
255 ulong flags;
256
257 gd = alloc_disk(AOE_PARTITIONS);
258 if (gd == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400259 printk(KERN_ERR "aoe: cannot allocate disk structure for %ld.%ld\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400260 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800261 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
Matthew Dobson93d23412006-03-26 01:37:50 -0800264 d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (d->bufpool == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400266 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%ld\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400267 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800268 goto err_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 blk_queue_make_request(&d->blkq, aoeblk_make_request);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800272 if (bdi_init(&d->blkq.backing_dev_info))
273 goto err_mempool;
274 spin_lock_irqsave(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 gd->major = AOE_MAJOR;
276 gd->first_minor = d->sysminor * AOE_PARTITIONS;
277 gd->fops = &aoe_bdops;
278 gd->private_data = d;
279 gd->capacity = d->ssize;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800280 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 d->aoemajor, d->aoeminor);
282
283 gd->queue = &d->blkq;
284 d->gd = gd;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500285 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 d->flags |= DEVFL_UP;
287
288 spin_unlock_irqrestore(&d->lock, flags);
289
290 add_disk(gd);
291 aoedisk_add_sysfs(d);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800292 return;
293
294err_mempool:
295 mempool_destroy(d->bufpool);
296err_disk:
297 put_disk(gd);
298err:
299 spin_lock_irqsave(&d->lock, flags);
300 d->flags &= ~DEVFL_GDALLOC;
301 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304void
305aoeblk_exit(void)
306{
307 kmem_cache_destroy(buf_pool_cache);
308}
309
310int __init
311aoeblk_init(void)
312{
Paul Mundt20c2df82007-07-20 10:11:58 +0900313 buf_pool_cache = kmem_cache_create("aoe_bufs",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 sizeof(struct buf),
Paul Mundt20c2df82007-07-20 10:11:58 +0900315 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (buf_pool_cache == NULL)
317 return -ENOMEM;
318
319 return 0;
320}
321