blob: d433f27e0ce2529c1617a6584e42abe55be62d36 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017static ssize_t aoedisk_show_state(struct gendisk * disk, char *page)
18{
19 struct aoedev *d = disk->private_data;
20
21 return snprintf(page, PAGE_SIZE,
22 "%s%s\n",
23 (d->flags & DEVFL_UP) ? "up" : "down",
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050024 (d->flags & DEVFL_PAUSE) ? ",paused" :
25 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
26 /* I'd rather see nopen exported so we can ditch closewait */
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}
28static ssize_t aoedisk_show_mac(struct gendisk * disk, char *page)
29{
30 struct aoedev *d = disk->private_data;
31
32 return snprintf(page, PAGE_SIZE, "%012llx\n",
33 (unsigned long long)mac_addr(d->addr));
34}
35static ssize_t aoedisk_show_netif(struct gendisk * disk, char *page)
36{
37 struct aoedev *d = disk->private_data;
38
39 return snprintf(page, PAGE_SIZE, "%s\n", d->ifp->name);
40}
Ed L Cashin4613ed22005-04-29 10:24:25 -040041/* firmware version */
42static ssize_t aoedisk_show_fwver(struct gendisk * disk, char *page)
43{
44 struct aoedev *d = disk->private_data;
45
46 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
47}
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49static struct disk_attribute disk_attr_state = {
50 .attr = {.name = "state", .mode = S_IRUGO },
51 .show = aoedisk_show_state
52};
53static struct disk_attribute disk_attr_mac = {
54 .attr = {.name = "mac", .mode = S_IRUGO },
55 .show = aoedisk_show_mac
56};
57static struct disk_attribute disk_attr_netif = {
58 .attr = {.name = "netif", .mode = S_IRUGO },
59 .show = aoedisk_show_netif
60};
Ed L Cashin4613ed22005-04-29 10:24:25 -040061static struct disk_attribute disk_attr_fwver = {
62 .attr = {.name = "firmware-version", .mode = S_IRUGO },
63 .show = aoedisk_show_fwver
64};
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -070066static struct attribute *aoe_attrs[] = {
67 &disk_attr_state.attr,
68 &disk_attr_mac.attr,
69 &disk_attr_netif.attr,
70 &disk_attr_fwver.attr,
71};
72
73static const struct attribute_group attr_group = {
74 .attrs = aoe_attrs,
75};
76
77static int
Linus Torvalds1da177e2005-04-16 15:20:36 -070078aoedisk_add_sysfs(struct aoedev *d)
79{
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -070080 return sysfs_create_group(&d->gd->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82void
83aoedisk_rm_sysfs(struct aoedev *d)
84{
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -070085 sysfs_remove_group(&d->gd->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88static int
89aoeblk_open(struct inode *inode, struct file *filp)
90{
91 struct aoedev *d;
92 ulong flags;
93
94 d = inode->i_bdev->bd_disk->private_data;
95
96 spin_lock_irqsave(&d->lock, flags);
97 if (d->flags & DEVFL_UP) {
98 d->nopen++;
99 spin_unlock_irqrestore(&d->lock, flags);
100 return 0;
101 }
102 spin_unlock_irqrestore(&d->lock, flags);
103 return -ENODEV;
104}
105
106static int
107aoeblk_release(struct inode *inode, struct file *filp)
108{
109 struct aoedev *d;
110 ulong flags;
111
112 d = inode->i_bdev->bd_disk->private_data;
113
114 spin_lock_irqsave(&d->lock, flags);
115
Ed L. Cashin5f7702f2006-01-19 13:46:27 -0500116 if (--d->nopen == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 spin_unlock_irqrestore(&d->lock, flags);
118 aoecmd_cfg(d->aoemajor, d->aoeminor);
119 return 0;
120 }
121 spin_unlock_irqrestore(&d->lock, flags);
122
123 return 0;
124}
125
126static int
127aoeblk_make_request(request_queue_t *q, struct bio *bio)
128{
129 struct aoedev *d;
130 struct buf *buf;
131 struct sk_buff *sl;
132 ulong flags;
133
134 blk_queue_bounce(q, &bio);
135
136 d = bio->bi_bdev->bd_disk->private_data;
137 buf = mempool_alloc(d->bufpool, GFP_NOIO);
138 if (buf == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400139 printk(KERN_INFO "aoe: buf allocation failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 bio_endio(bio, bio->bi_size, -ENOMEM);
141 return 0;
142 }
143 memset(buf, 0, sizeof(*buf));
144 INIT_LIST_HEAD(&buf->bufs);
ecashin@coraid.com0c6f0e72005-04-18 22:00:22 -0700145 buf->start_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 buf->bio = bio;
147 buf->resid = bio->bi_size;
148 buf->sector = bio->bi_sector;
Ed L. Cashin392e4842006-09-20 14:36:50 -0400149 buf->bv = &bio->bi_io_vec[bio->bi_idx];
150 WARN_ON(buf->bv->bv_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 buf->bv_resid = buf->bv->bv_len;
152 buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset;
153
154 spin_lock_irqsave(&d->lock, flags);
155
156 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400157 printk(KERN_INFO "aoe: device %ld.%ld is not up\n",
158 d->aoemajor, d->aoeminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 spin_unlock_irqrestore(&d->lock, flags);
160 mempool_free(buf, d->bufpool);
161 bio_endio(bio, bio->bi_size, -ENXIO);
162 return 0;
163 }
164
165 list_add_tail(&buf->bufs, &d->bufq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500167 aoecmd_work(d);
ecashin@coraid.coma4b38362005-04-18 22:00:22 -0700168 sl = d->sendq_hd;
169 d->sendq_hd = d->sendq_tl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 aoenet_xmit(sl);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 0;
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800178aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800180 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400183 printk(KERN_ERR "aoe: disk not up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return -ENODEV;
185 }
186
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800187 geo->cylinders = d->geo.cylinders;
188 geo->heads = d->geo.heads;
189 geo->sectors = d->geo.sectors;
190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193static struct block_device_operations aoe_bdops = {
194 .open = aoeblk_open,
195 .release = aoeblk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800196 .getgeo = aoeblk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 .owner = THIS_MODULE,
198};
199
200/* alloc_disk and add_disk can sleep */
201void
202aoeblk_gdalloc(void *vp)
203{
204 struct aoedev *d = vp;
205 struct gendisk *gd;
206 ulong flags;
207
208 gd = alloc_disk(AOE_PARTITIONS);
209 if (gd == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400210 printk(KERN_ERR "aoe: cannot allocate disk structure for %ld.%ld\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400211 d->aoemajor, d->aoeminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 spin_lock_irqsave(&d->lock, flags);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500213 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 spin_unlock_irqrestore(&d->lock, flags);
215 return;
216 }
217
Matthew Dobson93d23412006-03-26 01:37:50 -0800218 d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (d->bufpool == NULL) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400220 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%ld\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400221 d->aoemajor, d->aoeminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 put_disk(gd);
223 spin_lock_irqsave(&d->lock, flags);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500224 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 spin_unlock_irqrestore(&d->lock, flags);
226 return;
227 }
228
229 spin_lock_irqsave(&d->lock, flags);
230 blk_queue_make_request(&d->blkq, aoeblk_make_request);
231 gd->major = AOE_MAJOR;
232 gd->first_minor = d->sysminor * AOE_PARTITIONS;
233 gd->fops = &aoe_bdops;
234 gd->private_data = d;
235 gd->capacity = d->ssize;
236 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%ld",
237 d->aoemajor, d->aoeminor);
238
239 gd->queue = &d->blkq;
240 d->gd = gd;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500241 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 d->flags |= DEVFL_UP;
243
244 spin_unlock_irqrestore(&d->lock, flags);
245
246 add_disk(gd);
247 aoedisk_add_sysfs(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250void
251aoeblk_exit(void)
252{
253 kmem_cache_destroy(buf_pool_cache);
254}
255
256int __init
257aoeblk_init(void)
258{
259 buf_pool_cache = kmem_cache_create("aoe_bufs",
260 sizeof(struct buf),
261 0, 0, NULL, NULL);
262 if (buf_pool_cache == NULL)
263 return -ENOMEM;
264
265 return 0;
266}
267