]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/block/aoe/aoeblk.c
ad00b3d94711d09b6344ea36cd73ce21418932a1
[linux-2.6-omap-h63xx.git] / drivers / block / aoe / aoeblk.c
1 /* Copyright (c) 2006 Coraid, Inc.  See COPYING for GPL terms. */
2 /*
3  * aoeblk.c
4  * block device routines
5  */
6
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
9 #include <linux/backing-dev.h>
10 #include <linux/fs.h>
11 #include <linux/ioctl.h>
12 #include <linux/genhd.h>
13 #include <linux/netdevice.h>
14 #include "aoe.h"
15
16 static struct kmem_cache *buf_pool_cache;
17
18 static 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",
25                         (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 */
28 }
29 static 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 }
36 static 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 }
42 /* firmware version */
43 static 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 }
49
50 static struct disk_attribute disk_attr_state = {
51         .attr = {.name = "state", .mode = S_IRUGO },
52         .show = aoedisk_show_state
53 };
54 static struct disk_attribute disk_attr_mac = {
55         .attr = {.name = "mac", .mode = S_IRUGO },
56         .show = aoedisk_show_mac
57 };
58 static struct disk_attribute disk_attr_netif = {
59         .attr = {.name = "netif", .mode = S_IRUGO },
60         .show = aoedisk_show_netif
61 };
62 static struct disk_attribute disk_attr_fwver = {
63         .attr = {.name = "firmware-version", .mode = S_IRUGO },
64         .show = aoedisk_show_fwver
65 };
66
67 static struct attribute *aoe_attrs[] = {
68         &disk_attr_state.attr,
69         &disk_attr_mac.attr,
70         &disk_attr_netif.attr,
71         &disk_attr_fwver.attr,
72         NULL
73 };
74
75 static const struct attribute_group attr_group = {
76         .attrs = aoe_attrs,
77 };
78
79 static int
80 aoedisk_add_sysfs(struct aoedev *d)
81 {
82         return sysfs_create_group(&d->gd->kobj, &attr_group);
83 }
84 void
85 aoedisk_rm_sysfs(struct aoedev *d)
86 {
87         sysfs_remove_group(&d->gd->kobj, &attr_group);
88 }
89
90 static int
91 aoeblk_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
108 static int
109 aoeblk_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
118         if (--d->nopen == 0) {
119                 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
128 static int
129 aoeblk_make_request(struct request_queue *q, struct bio *bio)
130 {
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) {
141                 printk(KERN_INFO "aoe: buf allocation failure\n");
142                 bio_endio(bio, -ENOMEM);
143                 return 0;
144         }
145         memset(buf, 0, sizeof(*buf));
146         INIT_LIST_HEAD(&buf->bufs);
147         buf->start_time = jiffies;
148         buf->bio = bio;
149         buf->resid = bio->bi_size;
150         buf->sector = bio->bi_sector;
151         buf->bv = &bio->bi_io_vec[bio->bi_idx];
152         WARN_ON(buf->bv->bv_len == 0);
153         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) {
159                 printk(KERN_INFO "aoe: device %ld.%ld is not up\n",
160                         d->aoemajor, d->aoeminor);
161                 spin_unlock_irqrestore(&d->lock, flags);
162                 mempool_free(buf, d->bufpool);
163                 bio_endio(bio, -ENXIO);
164                 return 0;
165         }
166
167         list_add_tail(&buf->bufs, &d->bufq);
168
169         aoecmd_work(d);
170         sl = d->sendq_hd;
171         d->sendq_hd = d->sendq_tl = NULL;
172
173         spin_unlock_irqrestore(&d->lock, flags);
174         aoenet_xmit(sl);
175
176         return 0;
177 }
178
179 static int
180 aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
181 {
182         struct aoedev *d = bdev->bd_disk->private_data;
183
184         if ((d->flags & DEVFL_UP) == 0) {
185                 printk(KERN_ERR "aoe: disk not up\n");
186                 return -ENODEV;
187         }
188
189         geo->cylinders = d->geo.cylinders;
190         geo->heads = d->geo.heads;
191         geo->sectors = d->geo.sectors;
192         return 0;
193 }
194
195 static struct block_device_operations aoe_bdops = {
196         .open = aoeblk_open,
197         .release = aoeblk_release,
198         .getgeo = aoeblk_getgeo,
199         .owner = THIS_MODULE,
200 };
201
202 /* alloc_disk and add_disk can sleep */
203 void
204 aoeblk_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) {
212                 printk(KERN_ERR "aoe: cannot allocate disk structure for %ld.%ld\n",
213                         d->aoemajor, d->aoeminor);
214                 goto err;
215         }
216
217         d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
218         if (d->bufpool == NULL) {
219                 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%ld\n",
220                         d->aoemajor, d->aoeminor);
221                 goto err_disk;
222         }
223
224         blk_queue_make_request(&d->blkq, aoeblk_make_request);
225         if (bdi_init(&d->blkq.backing_dev_info))
226                 goto err_mempool;
227         spin_lock_irqsave(&d->lock, flags);
228         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;
238         d->flags &= ~DEVFL_GDALLOC;
239         d->flags |= DEVFL_UP;
240
241         spin_unlock_irqrestore(&d->lock, flags);
242
243         add_disk(gd);
244         aoedisk_add_sysfs(d);
245         return;
246
247 err_mempool:
248         mempool_destroy(d->bufpool);
249 err_disk:
250         put_disk(gd);
251 err:
252         spin_lock_irqsave(&d->lock, flags);
253         d->flags &= ~DEVFL_GDALLOC;
254         spin_unlock_irqrestore(&d->lock, flags);
255 }
256
257 void
258 aoeblk_exit(void)
259 {
260         kmem_cache_destroy(buf_pool_cache);
261 }
262
263 int __init
264 aoeblk_init(void)
265 {
266         buf_pool_cache = kmem_cache_create("aoe_bufs",
267                                            sizeof(struct buf),
268                                            0, 0, NULL);
269         if (buf_pool_cache == NULL)
270                 return -ENOMEM;
271
272         return 0;
273 }
274