]>
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 | ||
15 | static kmem_cache_t *buf_pool_cache; | |
16 | ||
17 | /* add attributes for our block devices in sysfs */ | |
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", | |
3ae1c24e EC |
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 */ | |
1da177e4 LT |
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 | } | |
4613ed27 EC |
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 | } | |
1da177e4 LT |
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 | }; | |
4613ed27 EC |
62 | static struct disk_attribute disk_attr_fwver = { |
63 | .attr = {.name = "firmware-version", .mode = S_IRUGO }, | |
64 | .show = aoedisk_show_fwver | |
65 | }; | |
1da177e4 LT |
66 | |
67 | static void | |
68 | aoedisk_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); | |
4613ed27 | 73 | sysfs_create_file(&d->gd->kobj, &disk_attr_fwver.attr); |
1da177e4 LT |
74 | } |
75 | void | |
76 | aoedisk_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"); | |
4613ed27 | 81 | sysfs_remove_link(&d->gd->kobj, "firmware-version"); |
1da177e4 LT |
82 | } |
83 | ||
84 | static int | |
85 | aoeblk_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 | ||
102 | static int | |
103 | aoeblk_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 | ||
5f7702fd | 112 | if (--d->nopen == 0) { |
1da177e4 LT |
113 | 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 | ||
122 | static int | |
123 | aoeblk_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) { | |
6bb6285f | 135 | iprintk("buf allocation failure\n"); |
1da177e4 LT |
136 | bio_endio(bio, bio->bi_size, -ENOMEM); |
137 | return 0; | |
138 | } | |
139 | memset(buf, 0, sizeof(*buf)); | |
140 | INIT_LIST_HEAD(&buf->bufs); | |
0c6f0e79 | 141 | buf->start_time = jiffies; |
1da177e4 LT |
142 | buf->bio = bio; |
143 | buf->resid = bio->bi_size; | |
144 | buf->sector = bio->bi_sector; | |
145 | buf->bv = buf->bio->bi_io_vec; | |
146 | buf->bv_resid = buf->bv->bv_len; | |
147 | buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset; | |
148 | ||
149 | spin_lock_irqsave(&d->lock, flags); | |
150 | ||
151 | if ((d->flags & DEVFL_UP) == 0) { | |
6bb6285f | 152 | iprintk("device %ld.%ld is not up\n", d->aoemajor, d->aoeminor); |
1da177e4 LT |
153 | spin_unlock_irqrestore(&d->lock, flags); |
154 | mempool_free(buf, d->bufpool); | |
155 | bio_endio(bio, bio->bi_size, -ENXIO); | |
156 | return 0; | |
157 | } | |
158 | ||
159 | list_add_tail(&buf->bufs, &d->bufq); | |
1da177e4 | 160 | |
3ae1c24e | 161 | aoecmd_work(d); |
a4b38364 EC |
162 | sl = d->sendq_hd; |
163 | d->sendq_hd = d->sendq_tl = NULL; | |
1da177e4 LT |
164 | |
165 | spin_unlock_irqrestore(&d->lock, flags); | |
1da177e4 | 166 | aoenet_xmit(sl); |
3ae1c24e | 167 | |
1da177e4 LT |
168 | return 0; |
169 | } | |
170 | ||
1da177e4 | 171 | static int |
a885c8c4 | 172 | aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
1da177e4 | 173 | { |
a885c8c4 | 174 | struct aoedev *d = bdev->bd_disk->private_data; |
1da177e4 | 175 | |
1da177e4 | 176 | if ((d->flags & DEVFL_UP) == 0) { |
6bb6285f | 177 | eprintk("disk not up\n"); |
1da177e4 LT |
178 | return -ENODEV; |
179 | } | |
180 | ||
a885c8c4 CH |
181 | geo->cylinders = d->geo.cylinders; |
182 | geo->heads = d->geo.heads; | |
183 | geo->sectors = d->geo.sectors; | |
184 | return 0; | |
1da177e4 LT |
185 | } |
186 | ||
187 | static struct block_device_operations aoe_bdops = { | |
188 | .open = aoeblk_open, | |
189 | .release = aoeblk_release, | |
a885c8c4 | 190 | .getgeo = aoeblk_getgeo, |
1da177e4 LT |
191 | .owner = THIS_MODULE, |
192 | }; | |
193 | ||
194 | /* alloc_disk and add_disk can sleep */ | |
195 | void | |
196 | aoeblk_gdalloc(void *vp) | |
197 | { | |
198 | struct aoedev *d = vp; | |
199 | struct gendisk *gd; | |
200 | ulong flags; | |
201 | ||
202 | gd = alloc_disk(AOE_PARTITIONS); | |
203 | if (gd == NULL) { | |
6bb6285f EC |
204 | eprintk("cannot allocate disk structure for %ld.%ld\n", |
205 | d->aoemajor, d->aoeminor); | |
1da177e4 | 206 | spin_lock_irqsave(&d->lock, flags); |
3ae1c24e | 207 | d->flags &= ~DEVFL_GDALLOC; |
1da177e4 LT |
208 | spin_unlock_irqrestore(&d->lock, flags); |
209 | return; | |
210 | } | |
211 | ||
93d2341c | 212 | d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache); |
1da177e4 | 213 | if (d->bufpool == NULL) { |
6bb6285f EC |
214 | eprintk("cannot allocate bufpool for %ld.%ld\n", |
215 | d->aoemajor, d->aoeminor); | |
1da177e4 LT |
216 | put_disk(gd); |
217 | spin_lock_irqsave(&d->lock, flags); | |
3ae1c24e | 218 | d->flags &= ~DEVFL_GDALLOC; |
1da177e4 LT |
219 | spin_unlock_irqrestore(&d->lock, flags); |
220 | return; | |
221 | } | |
222 | ||
223 | spin_lock_irqsave(&d->lock, flags); | |
224 | blk_queue_make_request(&d->blkq, aoeblk_make_request); | |
225 | gd->major = AOE_MAJOR; | |
226 | gd->first_minor = d->sysminor * AOE_PARTITIONS; | |
227 | gd->fops = &aoe_bdops; | |
228 | gd->private_data = d; | |
229 | gd->capacity = d->ssize; | |
230 | snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%ld", | |
231 | d->aoemajor, d->aoeminor); | |
232 | ||
233 | gd->queue = &d->blkq; | |
234 | d->gd = gd; | |
3ae1c24e | 235 | d->flags &= ~DEVFL_GDALLOC; |
1da177e4 LT |
236 | d->flags |= DEVFL_UP; |
237 | ||
238 | spin_unlock_irqrestore(&d->lock, flags); | |
239 | ||
240 | add_disk(gd); | |
241 | aoedisk_add_sysfs(d); | |
1da177e4 LT |
242 | } |
243 | ||
244 | void | |
245 | aoeblk_exit(void) | |
246 | { | |
247 | kmem_cache_destroy(buf_pool_cache); | |
248 | } | |
249 | ||
250 | int __init | |
251 | aoeblk_init(void) | |
252 | { | |
253 | buf_pool_cache = kmem_cache_create("aoe_bufs", | |
254 | sizeof(struct buf), | |
255 | 0, 0, NULL, NULL); | |
256 | if (buf_pool_cache == NULL) | |
257 | return -ENOMEM; | |
258 | ||
259 | return 0; | |
260 | } | |
261 |