blob: 196ae7a37643f594c42df78ef1954705f7d69edc [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>
9#include <linux/fs.h>
10#include <linux/ioctl.h>
11#include <linux/genhd.h>
12#include <linux/netdevice.h>
13#include "aoe.h"
14
15static kmem_cache_t *buf_pool_cache;
16
17/* add attributes for our block devices in sysfs */
18static 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
67static void
68aoedisk_add_sysfs(struct aoedev *d)
69{
70 sysfs_create_file(&d->gd->kobj, &disk_attr_state.attr);
71 sysfs_create_file(&d->gd->kobj, &disk_attr_mac.attr);
72 sysfs_create_file(&d->gd->kobj, &disk_attr_netif.attr);
Ed L Cashin4613ed22005-04-29 10:24:25 -040073 sysfs_create_file(&d->gd->kobj, &disk_attr_fwver.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75void
76aoedisk_rm_sysfs(struct aoedev *d)
77{
78 sysfs_remove_link(&d->gd->kobj, "state");
79 sysfs_remove_link(&d->gd->kobj, "mac");
80 sysfs_remove_link(&d->gd->kobj, "netif");
Ed L Cashin4613ed22005-04-29 10:24:25 -040081 sysfs_remove_link(&d->gd->kobj, "firmware-version");
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84static int
85aoeblk_open(struct inode *inode, struct file *filp)
86{
87 struct aoedev *d;
88 ulong flags;
89
90 d = inode->i_bdev->bd_disk->private_data;
91
92 spin_lock_irqsave(&d->lock, flags);
93 if (d->flags & DEVFL_UP) {
94 d->nopen++;
95 spin_unlock_irqrestore(&d->lock, flags);
96 return 0;
97 }
98 spin_unlock_irqrestore(&d->lock, flags);
99 return -ENODEV;
100}
101
102static int
103aoeblk_release(struct inode *inode, struct file *filp)
104{
105 struct aoedev *d;
106 ulong flags;
107
108 d = inode->i_bdev->bd_disk->private_data;
109
110 spin_lock_irqsave(&d->lock, flags);
111
Ed L. Cashin5f7702f2006-01-19 13:46:27 -0500112 if (--d->nopen == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 spin_unlock_irqrestore(&d->lock, flags);
114 aoecmd_cfg(d->aoemajor, d->aoeminor);
115 return 0;
116 }
117 spin_unlock_irqrestore(&d->lock, flags);
118
119 return 0;
120}
121
122static int
123aoeblk_make_request(request_queue_t *q, struct bio *bio)
124{
125 struct aoedev *d;
126 struct buf *buf;
127 struct sk_buff *sl;
128 ulong flags;
129
130 blk_queue_bounce(q, &bio);
131
132 d = bio->bi_bdev->bd_disk->private_data;
133 buf = mempool_alloc(d->bufpool, GFP_NOIO);
134 if (buf == NULL) {
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400135 iprintk("buf allocation failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 bio_endio(bio, bio->bi_size, -ENOMEM);
137 return 0;
138 }
139 memset(buf, 0, sizeof(*buf));
140 INIT_LIST_HEAD(&buf->bufs);
ecashin@coraid.com0c6f0e72005-04-18 22:00:22 -0700141 buf->start_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 buf->bio = bio;
143 buf->resid = bio->bi_size;
144 buf->sector = bio->bi_sector;
Ed L. Cashin392e4842006-09-20 14:36:50 -0400145 buf->bv = &bio->bi_io_vec[bio->bi_idx];
146 WARN_ON(buf->bv->bv_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 buf->bv_resid = buf->bv->bv_len;
148 buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset;
149
150 spin_lock_irqsave(&d->lock, flags);
151
152 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400153 iprintk("device %ld.%ld is not up\n", d->aoemajor, d->aoeminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 spin_unlock_irqrestore(&d->lock, flags);
155 mempool_free(buf, d->bufpool);
156 bio_endio(bio, bio->bi_size, -ENXIO);
157 return 0;
158 }
159
160 list_add_tail(&buf->bufs, &d->bufq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500162 aoecmd_work(d);
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700163 sl = d->sendq_hd;
164 d->sendq_hd = d->sendq_tl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 aoenet_xmit(sl);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return 0;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800173aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800175 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400178 eprintk("disk not up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return -ENODEV;
180 }
181
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800182 geo->cylinders = d->geo.cylinders;
183 geo->heads = d->geo.heads;
184 geo->sectors = d->geo.sectors;
185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188static struct block_device_operations aoe_bdops = {
189 .open = aoeblk_open,
190 .release = aoeblk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800191 .getgeo = aoeblk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .owner = THIS_MODULE,
193};
194
195/* alloc_disk and add_disk can sleep */
196void
197aoeblk_gdalloc(void *vp)
198{
199 struct aoedev *d = vp;
200 struct gendisk *gd;
201 ulong flags;
202
203 gd = alloc_disk(AOE_PARTITIONS);
204 if (gd == NULL) {
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400205 eprintk("cannot allocate disk structure for %ld.%ld\n",
206 d->aoemajor, d->aoeminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 spin_lock_irqsave(&d->lock, flags);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500208 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 spin_unlock_irqrestore(&d->lock, flags);
210 return;
211 }
212
Matthew Dobson93d23412006-03-26 01:37:50 -0800213 d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (d->bufpool == NULL) {
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400215 eprintk("cannot allocate bufpool for %ld.%ld\n",
216 d->aoemajor, d->aoeminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 put_disk(gd);
218 spin_lock_irqsave(&d->lock, flags);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500219 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 spin_unlock_irqrestore(&d->lock, flags);
221 return;
222 }
223
224 spin_lock_irqsave(&d->lock, flags);
225 blk_queue_make_request(&d->blkq, aoeblk_make_request);
226 gd->major = AOE_MAJOR;
227 gd->first_minor = d->sysminor * AOE_PARTITIONS;
228 gd->fops = &aoe_bdops;
229 gd->private_data = d;
230 gd->capacity = d->ssize;
231 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%ld",
232 d->aoemajor, d->aoeminor);
233
234 gd->queue = &d->blkq;
235 d->gd = gd;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500236 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 d->flags |= DEVFL_UP;
238
239 spin_unlock_irqrestore(&d->lock, flags);
240
241 add_disk(gd);
242 aoedisk_add_sysfs(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245void
246aoeblk_exit(void)
247{
248 kmem_cache_destroy(buf_pool_cache);
249}
250
251int __init
252aoeblk_init(void)
253{
254 buf_pool_cache = kmem_cache_create("aoe_bufs",
255 sizeof(struct buf),
256 0, 0, NULL, NULL);
257 if (buf_pool_cache == NULL)
258 return -ENOMEM;
259
260 return 0;
261}
262