Commit | Line | Data |
---|---|---|
2611464d | 1 | /* Copyright (c) 2006 Coraid, Inc. See COPYING for GPL terms. */ |
1da177e4 LT |
2 | /* |
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 | ||
e18b890b | 15 | static struct kmem_cache *buf_pool_cache; |
1da177e4 | 16 | |
1da177e4 LT |
17 | static 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", | |
3ae1c24e EC |
24 | (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 */ | |
1da177e4 LT |
27 | } |
28 | static 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 | } | |
35 | static 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 | } | |
4613ed27 EC |
41 | /* firmware version */ |
42 | static 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 | } | |
1da177e4 LT |
48 | |
49 | static struct disk_attribute disk_attr_state = { | |
50 | .attr = {.name = "state", .mode = S_IRUGO }, | |
51 | .show = aoedisk_show_state | |
52 | }; | |
53 | static struct disk_attribute disk_attr_mac = { | |
54 | .attr = {.name = "mac", .mode = S_IRUGO }, | |
55 | .show = aoedisk_show_mac | |
56 | }; | |
57 | static struct disk_attribute disk_attr_netif = { | |
58 | .attr = {.name = "netif", .mode = S_IRUGO }, | |
59 | .show = aoedisk_show_netif | |
60 | }; | |
4613ed27 EC |
61 | static struct disk_attribute disk_attr_fwver = { |
62 | .attr = {.name = "firmware-version", .mode = S_IRUGO }, | |
63 | .show = aoedisk_show_fwver | |
64 | }; | |
1da177e4 | 65 | |
4ca5224f GKH |
66 | static struct attribute *aoe_attrs[] = { |
67 | &disk_attr_state.attr, | |
68 | &disk_attr_mac.attr, | |
69 | &disk_attr_netif.attr, | |
70 | &disk_attr_fwver.attr, | |
d355c3c2 | 71 | NULL |
4ca5224f GKH |
72 | }; |
73 | ||
74 | static const struct attribute_group attr_group = { | |
75 | .attrs = aoe_attrs, | |
76 | }; | |
77 | ||
78 | static int | |
1da177e4 LT |
79 | aoedisk_add_sysfs(struct aoedev *d) |
80 | { | |
4ca5224f | 81 | return sysfs_create_group(&d->gd->kobj, &attr_group); |
1da177e4 LT |
82 | } |
83 | void | |
84 | aoedisk_rm_sysfs(struct aoedev *d) | |
85 | { | |
4ca5224f | 86 | sysfs_remove_group(&d->gd->kobj, &attr_group); |
1da177e4 LT |
87 | } |
88 | ||
89 | static int | |
90 | aoeblk_open(struct inode *inode, struct file *filp) | |
91 | { | |
92 | struct aoedev *d; | |
93 | ulong flags; | |
94 | ||
95 | d = inode->i_bdev->bd_disk->private_data; | |
96 | ||
97 | spin_lock_irqsave(&d->lock, flags); | |
98 | if (d->flags & DEVFL_UP) { | |
99 | d->nopen++; | |
100 | spin_unlock_irqrestore(&d->lock, flags); | |
101 | return 0; | |
102 | } | |
103 | spin_unlock_irqrestore(&d->lock, flags); | |
104 | return -ENODEV; | |
105 | } | |
106 | ||
107 | static int | |
108 | aoeblk_release(struct inode *inode, struct file *filp) | |
109 | { | |
110 | struct aoedev *d; | |
111 | ulong flags; | |
112 | ||
113 | d = inode->i_bdev->bd_disk->private_data; | |
114 | ||
115 | spin_lock_irqsave(&d->lock, flags); | |
116 | ||
5f7702fd | 117 | if (--d->nopen == 0) { |
1da177e4 LT |
118 | spin_unlock_irqrestore(&d->lock, flags); |
119 | aoecmd_cfg(d->aoemajor, d->aoeminor); | |
120 | return 0; | |
121 | } | |
122 | spin_unlock_irqrestore(&d->lock, flags); | |
123 | ||
124 | return 0; | |
125 | } | |
126 | ||
127 | static int | |
165125e1 | 128 | aoeblk_make_request(struct request_queue *q, struct bio *bio) |
1da177e4 LT |
129 | { |
130 | struct aoedev *d; | |
131 | struct buf *buf; | |
132 | struct sk_buff *sl; | |
133 | ulong flags; | |
134 | ||
135 | blk_queue_bounce(q, &bio); | |
136 | ||
137 | d = bio->bi_bdev->bd_disk->private_data; | |
138 | buf = mempool_alloc(d->bufpool, GFP_NOIO); | |
139 | if (buf == NULL) { | |
a12c93f0 | 140 | printk(KERN_INFO "aoe: buf allocation failure\n"); |
6712ecf8 | 141 | bio_endio(bio, -ENOMEM); |
1da177e4 LT |
142 | return 0; |
143 | } | |
144 | memset(buf, 0, sizeof(*buf)); | |
145 | INIT_LIST_HEAD(&buf->bufs); | |
0c6f0e79 | 146 | buf->start_time = jiffies; |
1da177e4 LT |
147 | buf->bio = bio; |
148 | buf->resid = bio->bi_size; | |
149 | buf->sector = bio->bi_sector; | |
392e4845 EC |
150 | buf->bv = &bio->bi_io_vec[bio->bi_idx]; |
151 | WARN_ON(buf->bv->bv_len == 0); | |
1da177e4 LT |
152 | buf->bv_resid = buf->bv->bv_len; |
153 | buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset; | |
154 | ||
155 | spin_lock_irqsave(&d->lock, flags); | |
156 | ||
157 | if ((d->flags & DEVFL_UP) == 0) { | |
a12c93f0 EC |
158 | printk(KERN_INFO "aoe: device %ld.%ld is not up\n", |
159 | d->aoemajor, d->aoeminor); | |
1da177e4 LT |
160 | spin_unlock_irqrestore(&d->lock, flags); |
161 | mempool_free(buf, d->bufpool); | |
6712ecf8 | 162 | bio_endio(bio, -ENXIO); |
1da177e4 LT |
163 | return 0; |
164 | } | |
165 | ||
166 | list_add_tail(&buf->bufs, &d->bufq); | |
1da177e4 | 167 | |
3ae1c24e | 168 | aoecmd_work(d); |
a4b38364 | 169 | sl = d->sendq_hd; |
170 | d->sendq_hd = d->sendq_tl = NULL; | |
1da177e4 LT |
171 | |
172 | spin_unlock_irqrestore(&d->lock, flags); | |
1da177e4 | 173 | aoenet_xmit(sl); |
3ae1c24e | 174 | |
1da177e4 LT |
175 | return 0; |
176 | } | |
177 | ||
1da177e4 | 178 | static int |
a885c8c4 | 179 | aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
1da177e4 | 180 | { |
a885c8c4 | 181 | struct aoedev *d = bdev->bd_disk->private_data; |
1da177e4 | 182 | |
1da177e4 | 183 | if ((d->flags & DEVFL_UP) == 0) { |
a12c93f0 | 184 | printk(KERN_ERR "aoe: disk not up\n"); |
1da177e4 LT |
185 | return -ENODEV; |
186 | } | |
187 | ||
a885c8c4 CH |
188 | geo->cylinders = d->geo.cylinders; |
189 | geo->heads = d->geo.heads; | |
190 | geo->sectors = d->geo.sectors; | |
191 | return 0; | |
1da177e4 LT |
192 | } |
193 | ||
194 | static struct block_device_operations aoe_bdops = { | |
195 | .open = aoeblk_open, | |
196 | .release = aoeblk_release, | |
a885c8c4 | 197 | .getgeo = aoeblk_getgeo, |
1da177e4 LT |
198 | .owner = THIS_MODULE, |
199 | }; | |
200 | ||
201 | /* alloc_disk and add_disk can sleep */ | |
202 | void | |
203 | aoeblk_gdalloc(void *vp) | |
204 | { | |
205 | struct aoedev *d = vp; | |
206 | struct gendisk *gd; | |
207 | ulong flags; | |
208 | ||
209 | gd = alloc_disk(AOE_PARTITIONS); | |
210 | if (gd == NULL) { | |
a12c93f0 | 211 | printk(KERN_ERR "aoe: cannot allocate disk structure for %ld.%ld\n", |
6bb6285f | 212 | d->aoemajor, d->aoeminor); |
1da177e4 | 213 | spin_lock_irqsave(&d->lock, flags); |
3ae1c24e | 214 | d->flags &= ~DEVFL_GDALLOC; |
1da177e4 LT |
215 | spin_unlock_irqrestore(&d->lock, flags); |
216 | return; | |
217 | } | |
218 | ||
93d2341c | 219 | d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache); |
1da177e4 | 220 | if (d->bufpool == NULL) { |
a12c93f0 | 221 | printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%ld\n", |
6bb6285f | 222 | d->aoemajor, d->aoeminor); |
1da177e4 LT |
223 | put_disk(gd); |
224 | spin_lock_irqsave(&d->lock, flags); | |
3ae1c24e | 225 | d->flags &= ~DEVFL_GDALLOC; |
1da177e4 LT |
226 | spin_unlock_irqrestore(&d->lock, flags); |
227 | return; | |
228 | } | |
229 | ||
230 | spin_lock_irqsave(&d->lock, flags); | |
231 | blk_queue_make_request(&d->blkq, aoeblk_make_request); | |
232 | gd->major = AOE_MAJOR; | |
233 | gd->first_minor = d->sysminor * AOE_PARTITIONS; | |
234 | gd->fops = &aoe_bdops; | |
235 | gd->private_data = d; | |
236 | gd->capacity = d->ssize; | |
237 | snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%ld", | |
238 | d->aoemajor, d->aoeminor); | |
239 | ||
240 | gd->queue = &d->blkq; | |
241 | d->gd = gd; | |
3ae1c24e | 242 | d->flags &= ~DEVFL_GDALLOC; |
1da177e4 LT |
243 | d->flags |= DEVFL_UP; |
244 | ||
245 | spin_unlock_irqrestore(&d->lock, flags); | |
246 | ||
247 | add_disk(gd); | |
248 | aoedisk_add_sysfs(d); | |
1da177e4 LT |
249 | } |
250 | ||
251 | void | |
252 | aoeblk_exit(void) | |
253 | { | |
254 | kmem_cache_destroy(buf_pool_cache); | |
255 | } | |
256 | ||
257 | int __init | |
258 | aoeblk_init(void) | |
259 | { | |
20c2df83 | 260 | buf_pool_cache = kmem_cache_create("aoe_bufs", |
1da177e4 | 261 | sizeof(struct buf), |
20c2df83 | 262 | 0, 0, NULL); |
1da177e4 LT |
263 | if (buf_pool_cache == NULL) |
264 | return -ENOMEM; | |
265 | ||
266 | return 0; | |
267 | } | |
268 |