blob: ad00b3d94711d09b6344ea36cd73ce21418932a1 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018static ssize_t aoedisk_show_state(struct gendisk * disk, char *page)
19{
20 struct aoedev *d = disk->private_data;
21
22 return snprintf(page, PAGE_SIZE,
23 "%s%s\n",
24 (d->flags & DEVFL_UP) ? "up" : "down",
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050025 (d->flags & DEVFL_PAUSE) ? ",paused" :
26 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
27 /* I'd rather see nopen exported so we can ditch closewait */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028}
29static ssize_t aoedisk_show_mac(struct gendisk * disk, char *page)
30{
31 struct aoedev *d = disk->private_data;
32
33 return snprintf(page, PAGE_SIZE, "%012llx\n",
34 (unsigned long long)mac_addr(d->addr));
35}
36static ssize_t aoedisk_show_netif(struct gendisk * disk, char *page)
37{
38 struct aoedev *d = disk->private_data;
39
40 return snprintf(page, PAGE_SIZE, "%s\n", d->ifp->name);
41}
Ed L Cashin4613ed22005-04-29 10:24:25 -040042/* firmware version */
43static ssize_t aoedisk_show_fwver(struct gendisk * disk, char *page)
44{
45 struct aoedev *d = disk->private_data;
46
47 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
48}
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static struct disk_attribute disk_attr_state = {
51 .attr = {.name = "state", .mode = S_IRUGO },
52 .show = aoedisk_show_state
53};
54static struct disk_attribute disk_attr_mac = {
55 .attr = {.name = "mac", .mode = S_IRUGO },
56 .show = aoedisk_show_mac
57};
58static struct disk_attribute disk_attr_netif = {
59 .attr = {.name = "netif", .mode = S_IRUGO },
60 .show = aoedisk_show_netif
61};
Ed L Cashin4613ed22005-04-29 10:24:25 -040062static struct disk_attribute disk_attr_fwver = {
63 .attr = {.name = "firmware-version", .mode = S_IRUGO },
64 .show = aoedisk_show_fwver
65};
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -070067static struct attribute *aoe_attrs[] = {
68 &disk_attr_state.attr,
69 &disk_attr_mac.attr,
70 &disk_attr_netif.attr,
71 &disk_attr_fwver.attr,
Dennis Stosbergd355c3c2006-11-13 09:15:20 +010072 NULL
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -070073};
74
75static const struct attribute_group attr_group = {
76 .attrs = aoe_attrs,
77};
78
79static int
Linus Torvalds1da177e2005-04-16 15:20:36 -070080aoedisk_add_sysfs(struct aoedev *d)
81{
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -070082 return sysfs_create_group(&d->gd->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84void
85aoedisk_rm_sysfs(struct aoedev *d)
86{
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -070087 sysfs_remove_group(&d->gd->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90static int
91aoeblk_open(struct inode *inode, struct file *filp)
92{
93 struct aoedev *d;
94 ulong flags;
95
96 d = inode->i_bdev->bd_disk->private_data;
97
98 spin_lock_irqsave(&d->lock, flags);
99 if (d->flags & DEVFL_UP) {
100 d->nopen++;
101 spin_unlock_irqrestore(&d->lock, flags);
102 return 0;
103 }
104 spin_unlock_irqrestore(&d->lock, flags);
105 return -ENODEV;
106}
107
108static int
109aoeblk_release(struct inode *inode, struct file *filp)
110{
111 struct aoedev *d;
112 ulong flags;
113
114 d = inode->i_bdev->bd_disk->private_data;
115
116 spin_lock_irqsave(&d->lock, flags);
117
Ed L. Cashin5f7702f2006-01-19 13:46:27 -0500118 if (--d->nopen == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 spin_unlock_irqrestore(&d->lock, flags);
120 aoecmd_cfg(d->aoemajor, d->aoeminor);
121 return 0;
122 }
123 spin_unlock_irqrestore(&d->lock, flags);
124
125 return 0;
126}
127
128static int
Jens Axboe165125e2007-07-24 09:28:11 +0200129aoeblk_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct aoedev *d;
132 struct buf *buf;
133 struct sk_buff *sl;
134 ulong flags;
135
136 blk_queue_bounce(q, &bio);
137
138 d = bio->bi_bdev->bd_disk->private_data;
139 buf = mempool_alloc(d->bufpool, GFP_NOIO);
140 if (buf == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400141 printk(KERN_INFO "aoe: buf allocation failure\n");
NeilBrown6712ecf2007-09-27 12:47:43 +0200142 bio_endio(bio, -ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return 0;
144 }
145 memset(buf, 0, sizeof(*buf));
146 INIT_LIST_HEAD(&buf->bufs);
ecashin@coraid.com0c6f0e72005-04-18 22:00:22 -0700147 buf->start_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 buf->bio = bio;
149 buf->resid = bio->bi_size;
150 buf->sector = bio->bi_sector;
Ed L. Cashin392e4842006-09-20 14:36:50 -0400151 buf->bv = &bio->bi_io_vec[bio->bi_idx];
152 WARN_ON(buf->bv->bv_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 buf->bv_resid = buf->bv->bv_len;
154 buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset;
155
156 spin_lock_irqsave(&d->lock, flags);
157
158 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400159 printk(KERN_INFO "aoe: device %ld.%ld is not up\n",
160 d->aoemajor, d->aoeminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 spin_unlock_irqrestore(&d->lock, flags);
162 mempool_free(buf, d->bufpool);
NeilBrown6712ecf2007-09-27 12:47:43 +0200163 bio_endio(bio, -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return 0;
165 }
166
167 list_add_tail(&buf->bufs, &d->bufq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500169 aoecmd_work(d);
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700170 sl = d->sendq_hd;
171 d->sendq_hd = d->sendq_tl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 aoenet_xmit(sl);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return 0;
177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800180aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800182 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400185 printk(KERN_ERR "aoe: disk not up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return -ENODEV;
187 }
188
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800189 geo->cylinders = d->geo.cylinders;
190 geo->heads = d->geo.heads;
191 geo->sectors = d->geo.sectors;
192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195static struct block_device_operations aoe_bdops = {
196 .open = aoeblk_open,
197 .release = aoeblk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800198 .getgeo = aoeblk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .owner = THIS_MODULE,
200};
201
202/* alloc_disk and add_disk can sleep */
203void
204aoeblk_gdalloc(void *vp)
205{
206 struct aoedev *d = vp;
207 struct gendisk *gd;
208 ulong flags;
209
210 gd = alloc_disk(AOE_PARTITIONS);
211 if (gd == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400212 printk(KERN_ERR "aoe: cannot allocate disk structure for %ld.%ld\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400213 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800214 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
Matthew Dobson93d23412006-03-26 01:37:50 -0800217 d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (d->bufpool == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400219 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%ld\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400220 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800221 goto err_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 blk_queue_make_request(&d->blkq, aoeblk_make_request);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800225 if (bdi_init(&d->blkq.backing_dev_info))
226 goto err_mempool;
227 spin_lock_irqsave(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 gd->major = AOE_MAJOR;
229 gd->first_minor = d->sysminor * AOE_PARTITIONS;
230 gd->fops = &aoe_bdops;
231 gd->private_data = d;
232 gd->capacity = d->ssize;
233 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%ld",
234 d->aoemajor, d->aoeminor);
235
236 gd->queue = &d->blkq;
237 d->gd = gd;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500238 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 d->flags |= DEVFL_UP;
240
241 spin_unlock_irqrestore(&d->lock, flags);
242
243 add_disk(gd);
244 aoedisk_add_sysfs(d);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800245 return;
246
247err_mempool:
248 mempool_destroy(d->bufpool);
249err_disk:
250 put_disk(gd);
251err:
252 spin_lock_irqsave(&d->lock, flags);
253 d->flags &= ~DEVFL_GDALLOC;
254 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257void
258aoeblk_exit(void)
259{
260 kmem_cache_destroy(buf_pool_cache);
261}
262
263int __init
264aoeblk_init(void)
265{
Paul Mundt20c2df82007-07-20 10:11:58 +0900266 buf_pool_cache = kmem_cache_create("aoe_bufs",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 sizeof(struct buf),
Paul Mundt20c2df82007-07-20 10:11:58 +0900268 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (buf_pool_cache == NULL)
270 return -ENOMEM;
271
272 return 0;
273}
274