]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/block/aoe/aoeblk.c
aoe: update copyright date
[karo-tx-linux.git] / drivers / block / aoe / aoeblk.c
1 /* Copyright (c) 2013 Coraid, Inc.  See COPYING for GPL terms. */
2 /*
3  * aoeblk.c
4  * block device routines
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/hdreg.h>
9 #include <linux/blkdev.h>
10 #include <linux/backing-dev.h>
11 #include <linux/fs.h>
12 #include <linux/ioctl.h>
13 #include <linux/slab.h>
14 #include <linux/ratelimit.h>
15 #include <linux/genhd.h>
16 #include <linux/netdevice.h>
17 #include <linux/mutex.h>
18 #include <linux/export.h>
19 #include <linux/moduleparam.h>
20 #include <linux/debugfs.h>
21 #include <scsi/sg.h>
22 #include "aoe.h"
23
24 static DEFINE_MUTEX(aoeblk_mutex);
25 static struct kmem_cache *buf_pool_cache;
26 static struct dentry *aoe_debugfs_dir;
27
28 /* GPFS needs a larger value than the default. */
29 static int aoe_maxsectors;
30 module_param(aoe_maxsectors, int, 0644);
31 MODULE_PARM_DESC(aoe_maxsectors,
32         "When nonzero, set the maximum number of sectors per I/O request");
33
34 static ssize_t aoedisk_show_state(struct device *dev,
35                                   struct device_attribute *attr, char *page)
36 {
37         struct gendisk *disk = dev_to_disk(dev);
38         struct aoedev *d = disk->private_data;
39
40         return snprintf(page, PAGE_SIZE,
41                         "%s%s\n",
42                         (d->flags & DEVFL_UP) ? "up" : "down",
43                         (d->flags & DEVFL_KICKME) ? ",kickme" :
44                         (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
45         /* I'd rather see nopen exported so we can ditch closewait */
46 }
47 static ssize_t aoedisk_show_mac(struct device *dev,
48                                 struct device_attribute *attr, char *page)
49 {
50         struct gendisk *disk = dev_to_disk(dev);
51         struct aoedev *d = disk->private_data;
52         struct aoetgt *t = d->targets[0];
53
54         if (t == NULL)
55                 return snprintf(page, PAGE_SIZE, "none\n");
56         return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
57 }
58 static ssize_t aoedisk_show_netif(struct device *dev,
59                                   struct device_attribute *attr, char *page)
60 {
61         struct gendisk *disk = dev_to_disk(dev);
62         struct aoedev *d = disk->private_data;
63         struct net_device *nds[8], **nd, **nnd, **ne;
64         struct aoetgt **t, **te;
65         struct aoeif *ifp, *e;
66         char *p;
67
68         memset(nds, 0, sizeof nds);
69         nd = nds;
70         ne = nd + ARRAY_SIZE(nds);
71         t = d->targets;
72         te = t + d->ntargets;
73         for (; t < te && *t; t++) {
74                 ifp = (*t)->ifs;
75                 e = ifp + NAOEIFS;
76                 for (; ifp < e && ifp->nd; ifp++) {
77                         for (nnd = nds; nnd < nd; nnd++)
78                                 if (*nnd == ifp->nd)
79                                         break;
80                         if (nnd == nd && nd != ne)
81                                 *nd++ = ifp->nd;
82                 }
83         }
84
85         ne = nd;
86         nd = nds;
87         if (*nd == NULL)
88                 return snprintf(page, PAGE_SIZE, "none\n");
89         for (p = page; nd < ne; nd++)
90                 p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
91                         p == page ? "" : ",", (*nd)->name);
92         p += snprintf(p, PAGE_SIZE - (p-page), "\n");
93         return p-page;
94 }
95 /* firmware version */
96 static ssize_t aoedisk_show_fwver(struct device *dev,
97                                   struct device_attribute *attr, char *page)
98 {
99         struct gendisk *disk = dev_to_disk(dev);
100         struct aoedev *d = disk->private_data;
101
102         return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
103 }
104 static ssize_t aoedisk_show_payload(struct device *dev,
105                                     struct device_attribute *attr, char *page)
106 {
107         struct gendisk *disk = dev_to_disk(dev);
108         struct aoedev *d = disk->private_data;
109
110         return snprintf(page, PAGE_SIZE, "%lu\n", d->maxbcnt);
111 }
112
113 static int aoedisk_debugfs_show(struct seq_file *s, void *ignored)
114 {
115         struct aoedev *d;
116         struct aoetgt **t, **te;
117         struct aoeif *ifp, *ife;
118         unsigned long flags;
119         char c;
120
121         d = s->private;
122         seq_printf(s, "rttavg: %d rttdev: %d\n",
123                 d->rttavg >> RTTSCALE,
124                 d->rttdev >> RTTDSCALE);
125         seq_printf(s, "nskbpool: %d\n", skb_queue_len(&d->skbpool));
126         seq_printf(s, "kicked: %ld\n", d->kicked);
127         seq_printf(s, "maxbcnt: %ld\n", d->maxbcnt);
128         seq_printf(s, "ref: %ld\n", d->ref);
129
130         spin_lock_irqsave(&d->lock, flags);
131         t = d->targets;
132         te = t + d->ntargets;
133         for (; t < te && *t; t++) {
134                 c = '\t';
135                 seq_printf(s, "falloc: %ld\n", (*t)->falloc);
136                 seq_printf(s, "ffree: %p\n",
137                         list_empty(&(*t)->ffree) ? NULL : (*t)->ffree.next);
138                 seq_printf(s, "%pm:%d:%d:%d\n", (*t)->addr, (*t)->nout,
139                         (*t)->maxout, (*t)->nframes);
140                 seq_printf(s, "\tssthresh:%d\n", (*t)->ssthresh);
141                 seq_printf(s, "\ttaint:%d\n", (*t)->taint);
142                 seq_printf(s, "\tr:%d\n", (*t)->rpkts);
143                 seq_printf(s, "\tw:%d\n", (*t)->wpkts);
144                 ifp = (*t)->ifs;
145                 ife = ifp + ARRAY_SIZE((*t)->ifs);
146                 for (; ifp->nd && ifp < ife; ifp++) {
147                         seq_printf(s, "%c%s", c, ifp->nd->name);
148                         c = ',';
149                 }
150                 seq_puts(s, "\n");
151         }
152         spin_unlock_irqrestore(&d->lock, flags);
153
154         return 0;
155 }
156
157 static int aoe_debugfs_open(struct inode *inode, struct file *file)
158 {
159         return single_open(file, aoedisk_debugfs_show, inode->i_private);
160 }
161
162 static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
163 static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
164 static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
165 static struct device_attribute dev_attr_firmware_version = {
166         .attr = { .name = "firmware-version", .mode = S_IRUGO },
167         .show = aoedisk_show_fwver,
168 };
169 static DEVICE_ATTR(payload, S_IRUGO, aoedisk_show_payload, NULL);
170
171 static struct attribute *aoe_attrs[] = {
172         &dev_attr_state.attr,
173         &dev_attr_mac.attr,
174         &dev_attr_netif.attr,
175         &dev_attr_firmware_version.attr,
176         &dev_attr_payload.attr,
177         NULL,
178 };
179
180 static const struct attribute_group attr_group = {
181         .attrs = aoe_attrs,
182 };
183
184 static const struct file_operations aoe_debugfs_fops = {
185         .open = aoe_debugfs_open,
186         .read = seq_read,
187         .llseek = seq_lseek,
188         .release = single_release,
189 };
190
191 static void
192 aoedisk_add_debugfs(struct aoedev *d)
193 {
194         struct dentry *entry;
195         char *p;
196
197         if (aoe_debugfs_dir == NULL)
198                 return;
199         p = strchr(d->gd->disk_name, '/');
200         if (p == NULL)
201                 p = d->gd->disk_name;
202         else
203                 p++;
204         BUG_ON(*p == '\0');
205         entry = debugfs_create_file(p, 0444, aoe_debugfs_dir, d,
206                                     &aoe_debugfs_fops);
207         if (IS_ERR_OR_NULL(entry)) {
208                 pr_info("aoe: cannot create debugfs file for %s\n",
209                         d->gd->disk_name);
210                 return;
211         }
212         BUG_ON(d->debugfs);
213         d->debugfs = entry;
214 }
215 void
216 aoedisk_rm_debugfs(struct aoedev *d)
217 {
218         BUG_ON(d->debugfs == NULL);
219         debugfs_remove(d->debugfs);
220         d->debugfs = NULL;
221 }
222
223 static int
224 aoedisk_add_sysfs(struct aoedev *d)
225 {
226         return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
227 }
228 void
229 aoedisk_rm_sysfs(struct aoedev *d)
230 {
231         sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
232 }
233
234 static int
235 aoeblk_open(struct block_device *bdev, fmode_t mode)
236 {
237         struct aoedev *d = bdev->bd_disk->private_data;
238         ulong flags;
239
240         if (!virt_addr_valid(d)) {
241                 pr_crit("aoe: invalid device pointer in %s\n",
242                         __func__);
243                 WARN_ON(1);
244                 return -ENODEV;
245         }
246         if (!(d->flags & DEVFL_UP) || d->flags & DEVFL_TKILL)
247                 return -ENODEV;
248
249         mutex_lock(&aoeblk_mutex);
250         spin_lock_irqsave(&d->lock, flags);
251         if (d->flags & DEVFL_UP && !(d->flags & DEVFL_TKILL)) {
252                 d->nopen++;
253                 spin_unlock_irqrestore(&d->lock, flags);
254                 mutex_unlock(&aoeblk_mutex);
255                 return 0;
256         }
257         spin_unlock_irqrestore(&d->lock, flags);
258         mutex_unlock(&aoeblk_mutex);
259         return -ENODEV;
260 }
261
262 static void
263 aoeblk_release(struct gendisk *disk, fmode_t mode)
264 {
265         struct aoedev *d = disk->private_data;
266         ulong flags;
267
268         spin_lock_irqsave(&d->lock, flags);
269
270         if (--d->nopen == 0) {
271                 spin_unlock_irqrestore(&d->lock, flags);
272                 aoecmd_cfg(d->aoemajor, d->aoeminor);
273                 return;
274         }
275         spin_unlock_irqrestore(&d->lock, flags);
276 }
277
278 static void
279 aoeblk_request(struct request_queue *q)
280 {
281         struct aoedev *d;
282         struct request *rq;
283
284         d = q->queuedata;
285         if ((d->flags & DEVFL_UP) == 0) {
286                 pr_info_ratelimited("aoe: device %ld.%d is not up\n",
287                         d->aoemajor, d->aoeminor);
288                 while ((rq = blk_peek_request(q))) {
289                         blk_start_request(rq);
290                         aoe_end_request(d, rq, 1);
291                 }
292                 return;
293         }
294         aoecmd_work(d);
295 }
296
297 static int
298 aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
299 {
300         struct aoedev *d = bdev->bd_disk->private_data;
301
302         if ((d->flags & DEVFL_UP) == 0) {
303                 printk(KERN_ERR "aoe: disk not up\n");
304                 return -ENODEV;
305         }
306
307         geo->cylinders = d->geo.cylinders;
308         geo->heads = d->geo.heads;
309         geo->sectors = d->geo.sectors;
310         return 0;
311 }
312
313 static int
314 aoeblk_ioctl(struct block_device *bdev, fmode_t mode, uint cmd, ulong arg)
315 {
316         struct aoedev *d;
317
318         if (!arg)
319                 return -EINVAL;
320
321         d = bdev->bd_disk->private_data;
322         if ((d->flags & DEVFL_UP) == 0) {
323                 pr_err("aoe: disk not up\n");
324                 return -ENODEV;
325         }
326
327         if (cmd == HDIO_GET_IDENTITY) {
328                 if (!copy_to_user((void __user *) arg, &d->ident,
329                         sizeof(d->ident)))
330                         return 0;
331                 return -EFAULT;
332         }
333
334         /* udev calls scsi_id, which uses SG_IO, resulting in noise */
335         if (cmd != SG_IO)
336                 pr_info("aoe: unknown ioctl 0x%x\n", cmd);
337
338         return -ENOTTY;
339 }
340
341 static const struct block_device_operations aoe_bdops = {
342         .open = aoeblk_open,
343         .release = aoeblk_release,
344         .ioctl = aoeblk_ioctl,
345         .getgeo = aoeblk_getgeo,
346         .owner = THIS_MODULE,
347 };
348
349 /* alloc_disk and add_disk can sleep */
350 void
351 aoeblk_gdalloc(void *vp)
352 {
353         struct aoedev *d = vp;
354         struct gendisk *gd;
355         mempool_t *mp;
356         struct request_queue *q;
357         enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
358         ulong flags;
359         int late = 0;
360
361         spin_lock_irqsave(&d->lock, flags);
362         if (d->flags & DEVFL_GDALLOC
363         && !(d->flags & DEVFL_TKILL)
364         && !(d->flags & DEVFL_GD_NOW))
365                 d->flags |= DEVFL_GD_NOW;
366         else
367                 late = 1;
368         spin_unlock_irqrestore(&d->lock, flags);
369         if (late)
370                 return;
371
372         gd = alloc_disk(AOE_PARTITIONS);
373         if (gd == NULL) {
374                 pr_err("aoe: cannot allocate disk structure for %ld.%d\n",
375                         d->aoemajor, d->aoeminor);
376                 goto err;
377         }
378
379         mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab,
380                 buf_pool_cache);
381         if (mp == NULL) {
382                 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
383                         d->aoemajor, d->aoeminor);
384                 goto err_disk;
385         }
386         q = blk_init_queue(aoeblk_request, &d->lock);
387         if (q == NULL) {
388                 pr_err("aoe: cannot allocate block queue for %ld.%d\n",
389                         d->aoemajor, d->aoeminor);
390                 goto err_mempool;
391         }
392
393         spin_lock_irqsave(&d->lock, flags);
394         WARN_ON(!(d->flags & DEVFL_GD_NOW));
395         WARN_ON(!(d->flags & DEVFL_GDALLOC));
396         WARN_ON(d->flags & DEVFL_TKILL);
397         WARN_ON(d->gd);
398         WARN_ON(d->flags & DEVFL_UP);
399         blk_queue_max_hw_sectors(q, BLK_DEF_MAX_SECTORS);
400         q->backing_dev_info.name = "aoe";
401         q->backing_dev_info.ra_pages = READ_AHEAD / PAGE_CACHE_SIZE;
402         d->bufpool = mp;
403         d->blkq = gd->queue = q;
404         q->queuedata = d;
405         d->gd = gd;
406         if (aoe_maxsectors)
407                 blk_queue_max_hw_sectors(q, aoe_maxsectors);
408         gd->major = AOE_MAJOR;
409         gd->first_minor = d->sysminor;
410         gd->fops = &aoe_bdops;
411         gd->private_data = d;
412         set_capacity(gd, d->ssize);
413         snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
414                 d->aoemajor, d->aoeminor);
415
416         d->flags &= ~DEVFL_GDALLOC;
417         d->flags |= DEVFL_UP;
418
419         spin_unlock_irqrestore(&d->lock, flags);
420
421         add_disk(gd);
422         aoedisk_add_sysfs(d);
423         aoedisk_add_debugfs(d);
424
425         spin_lock_irqsave(&d->lock, flags);
426         WARN_ON(!(d->flags & DEVFL_GD_NOW));
427         d->flags &= ~DEVFL_GD_NOW;
428         spin_unlock_irqrestore(&d->lock, flags);
429         return;
430
431 err_mempool:
432         mempool_destroy(mp);
433 err_disk:
434         put_disk(gd);
435 err:
436         spin_lock_irqsave(&d->lock, flags);
437         d->flags &= ~DEVFL_GD_NOW;
438         schedule_work(&d->work);
439         spin_unlock_irqrestore(&d->lock, flags);
440 }
441
442 void
443 aoeblk_exit(void)
444 {
445         debugfs_remove_recursive(aoe_debugfs_dir);
446         aoe_debugfs_dir = NULL;
447         kmem_cache_destroy(buf_pool_cache);
448 }
449
450 int __init
451 aoeblk_init(void)
452 {
453         buf_pool_cache = kmem_cache_create("aoe_bufs",
454                                            sizeof(struct buf),
455                                            0, 0, NULL);
456         if (buf_pool_cache == NULL)
457                 return -ENOMEM;
458         aoe_debugfs_dir = debugfs_create_dir("aoe", NULL);
459         if (IS_ERR_OR_NULL(aoe_debugfs_dir)) {
460                 pr_info("aoe: cannot create debugfs directory\n");
461                 aoe_debugfs_dir = NULL;
462         }
463         return 0;
464 }
465