blob: ec9d8610b25f772eefe20917cc8921db17c29016 [file] [log] [blame]
Ed Cashinec345122013-09-11 14:25:43 -07001/* Copyright (c) 2013 Coraid, Inc. See COPYING for GPL terms. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * aoeblk.c
4 * block device routines
5 */
6
Andrew Morton027b1802010-10-28 06:15:26 -06007#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/hdreg.h>
9#include <linux/blkdev.h>
Andrew Morton43cbe2c2007-12-10 15:49:13 -080010#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/fs.h>
12#include <linux/ioctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Andrew Morton027b1802010-10-28 06:15:26 -060014#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/genhd.h>
16#include <linux/netdevice.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020017#include <linux/mutex.h>
Paul Gortmakerd5decd32011-05-26 16:00:52 -040018#include <linux/export.h>
Ed Cashinaa304fd2012-12-17 16:03:32 -080019#include <linux/moduleparam.h>
Ed Cashin190519c2013-09-11 14:25:39 -070020#include <linux/debugfs.h>
Ed Cashin667be1e2012-12-17 16:03:42 -080021#include <scsi/sg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "aoe.h"
23
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020024static DEFINE_MUTEX(aoeblk_mutex);
Christoph Lametere18b8902006-12-06 20:33:20 -080025static struct kmem_cache *buf_pool_cache;
Ed Cashin190519c2013-09-11 14:25:39 -070026static struct dentry *aoe_debugfs_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Ed Cashinaa304fd2012-12-17 16:03:32 -080028/* GPFS needs a larger value than the default. */
29static int aoe_maxsectors;
30module_param(aoe_maxsectors, int, 0644);
31MODULE_PARM_DESC(aoe_maxsectors,
32 "When nonzero, set the maximum number of sectors per I/O request");
33
Kay Sieversedfaa7c2007-05-21 22:08:01 +020034static ssize_t aoedisk_show_state(struct device *dev,
35 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020037 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct aoedev *d = disk->private_data;
39
40 return snprintf(page, PAGE_SIZE,
41 "%s%s\n",
42 (d->flags & DEVFL_UP) ? "up" : "down",
Ed L. Cashin68e0d422008-02-08 04:20:00 -080043 (d->flags & DEVFL_KICKME) ? ",kickme" :
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050044 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
45 /* I'd rather see nopen exported so we can ditch closewait */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
Kay Sieversedfaa7c2007-05-21 22:08:01 +020047static ssize_t aoedisk_show_mac(struct device *dev,
48 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020050 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 struct aoedev *d = disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080052 struct aoetgt *t = d->targets[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Ed L. Cashin68e0d422008-02-08 04:20:00 -080054 if (t == NULL)
55 return snprintf(page, PAGE_SIZE, "none\n");
Harvey Harrison411c41e2008-11-25 00:40:37 -080056 return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
Kay Sieversedfaa7c2007-05-21 22:08:01 +020058static ssize_t aoedisk_show_netif(struct device *dev,
59 struct device_attribute *attr, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020061 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct aoedev *d = disk->private_data;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080063 struct net_device *nds[8], **nd, **nnd, **ne;
64 struct aoetgt **t, **te;
65 struct aoeif *ifp, *e;
66 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Ed L. Cashin68e0d422008-02-08 04:20:00 -080068 memset(nds, 0, sizeof nds);
69 nd = nds;
70 ne = nd + ARRAY_SIZE(nds);
71 t = d->targets;
Ed Cashin71114ec2012-12-17 16:04:11 -080072 te = t + d->ntargets;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080073 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
Ed L Cashin4613ed22005-04-29 10:24:25 -040095/* firmware version */
Kay Sieversedfaa7c2007-05-21 22:08:01 +020096static ssize_t aoedisk_show_fwver(struct device *dev,
97 struct device_attribute *attr, char *page)
Ed L Cashin4613ed22005-04-29 10:24:25 -040098{
Kay Sieversedfaa7c2007-05-21 22:08:01 +020099 struct gendisk *disk = dev_to_disk(dev);
Ed L Cashin4613ed22005-04-29 10:24:25 -0400100 struct aoedev *d = disk->private_data;
101
102 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
103}
Ed Cashin90a25082012-12-17 16:03:34 -0800104static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Ed Cashin1cf94792013-09-11 14:25:41 -0700113static int aoedisk_debugfs_show(struct seq_file *s, void *ignored)
114{
115 struct aoedev *d;
Ed Cashin2256c1c2013-09-11 14:25:42 -0700116 struct aoetgt **t, **te;
117 struct aoeif *ifp, *ife;
Ed Cashin1cf94792013-09-11 14:25:41 -0700118 unsigned long flags;
Ed Cashin2256c1c2013-09-11 14:25:42 -0700119 char c;
Ed Cashin1cf94792013-09-11 14:25:41 -0700120
121 d = s->private;
Ed Cashin2256c1c2013-09-11 14:25:42 -0700122 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
Ed Cashin1cf94792013-09-11 14:25:41 -0700130 spin_lock_irqsave(&d->lock, flags);
Ed Cashin2256c1c2013-09-11 14:25:42 -0700131 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 }
Ed Cashin1cf94792013-09-11 14:25:41 -0700152 spin_unlock_irqrestore(&d->lock, flags);
153
154 return 0;
155}
156
157static int aoe_debugfs_open(struct inode *inode, struct file *file)
158{
159 return single_open(file, aoedisk_debugfs_show, inode->i_private);
160}
161
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200162static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
163static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
164static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
165static struct device_attribute dev_attr_firmware_version = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700166 .attr = { .name = "firmware-version", .mode = S_IRUGO },
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200167 .show = aoedisk_show_fwver,
Ed L Cashin4613ed22005-04-29 10:24:25 -0400168};
Ed Cashin90a25082012-12-17 16:03:34 -0800169static DEVICE_ATTR(payload, S_IRUGO, aoedisk_show_payload, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -0700171static struct attribute *aoe_attrs[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200172 &dev_attr_state.attr,
173 &dev_attr_mac.attr,
174 &dev_attr_netif.attr,
175 &dev_attr_firmware_version.attr,
Ed Cashin90a25082012-12-17 16:03:34 -0800176 &dev_attr_payload.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200177 NULL,
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -0700178};
179
180static const struct attribute_group attr_group = {
181 .attrs = aoe_attrs,
182};
183
Ed Cashin1cf94792013-09-11 14:25:41 -0700184static 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};
Ed Cashine8866cf2013-09-11 14:25:40 -0700190
191static void
192aoedisk_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}
215void
216aoedisk_rm_debugfs(struct aoedev *d)
217{
Ed Cashine8866cf2013-09-11 14:25:40 -0700218 debugfs_remove(d->debugfs);
219 d->debugfs = NULL;
220}
221
Greg Kroah-Hartman4ca52242002-04-09 12:14:34 -0700222static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223aoedisk_add_sysfs(struct aoedev *d)
224{
Tejun Heoed9e1982008-08-25 19:56:05 +0900225 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227void
228aoedisk_rm_sysfs(struct aoedev *d)
229{
Tejun Heoed9e1982008-08-25 19:56:05 +0900230 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233static int
Al Viro94562c12008-03-02 09:23:18 -0500234aoeblk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Al Viro94562c12008-03-02 09:23:18 -0500236 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 ulong flags;
238
Ed Cashine52a2932012-12-17 16:04:09 -0800239 if (!virt_addr_valid(d)) {
240 pr_crit("aoe: invalid device pointer in %s\n",
241 __func__);
242 WARN_ON(1);
243 return -ENODEV;
244 }
245 if (!(d->flags & DEVFL_UP) || d->flags & DEVFL_TKILL)
246 return -ENODEV;
247
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200248 mutex_lock(&aoeblk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 spin_lock_irqsave(&d->lock, flags);
Ed Cashine52a2932012-12-17 16:04:09 -0800250 if (d->flags & DEVFL_UP && !(d->flags & DEVFL_TKILL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 d->nopen++;
252 spin_unlock_irqrestore(&d->lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200253 mutex_unlock(&aoeblk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return 0;
255 }
256 spin_unlock_irqrestore(&d->lock, flags);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200257 mutex_unlock(&aoeblk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return -ENODEV;
259}
260
Al Virodb2a1442013-05-05 21:52:57 -0400261static void
Al Viro94562c12008-03-02 09:23:18 -0500262aoeblk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Al Viro94562c12008-03-02 09:23:18 -0500264 struct aoedev *d = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 ulong flags;
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 spin_lock_irqsave(&d->lock, flags);
268
Ed L. Cashin5f7702f2006-01-19 13:46:27 -0500269 if (--d->nopen == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 spin_unlock_irqrestore(&d->lock, flags);
271 aoecmd_cfg(d->aoemajor, d->aoeminor);
Al Virodb2a1442013-05-05 21:52:57 -0400272 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200277static void
Ed Cashin69cf2d852012-10-04 17:16:23 -0700278aoeblk_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 struct aoedev *d;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700281 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Ed Cashin69cf2d852012-10-04 17:16:23 -0700283 d = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if ((d->flags & DEVFL_UP) == 0) {
Andrew Morton027b1802010-10-28 06:15:26 -0600285 pr_info_ratelimited("aoe: device %ld.%d is not up\n",
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400286 d->aoemajor, d->aoeminor);
Ed Cashin69cf2d852012-10-04 17:16:23 -0700287 while ((rq = blk_peek_request(q))) {
288 blk_start_request(rq);
289 aoe_end_request(d, rq, 1);
290 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200291 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500293 aoecmd_work(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800297aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800299 struct aoedev *d = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if ((d->flags & DEVFL_UP) == 0) {
Ed L. Cashina12c93f2006-09-20 14:36:51 -0400302 printk(KERN_ERR "aoe: disk not up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return -ENODEV;
304 }
305
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800306 geo->cylinders = d->geo.cylinders;
307 geo->heads = d->geo.heads;
308 geo->sectors = d->geo.sectors;
309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Ed Cashin667be1e2012-12-17 16:03:42 -0800312static int
313aoeblk_ioctl(struct block_device *bdev, fmode_t mode, uint cmd, ulong arg)
314{
315 struct aoedev *d;
316
317 if (!arg)
318 return -EINVAL;
319
320 d = bdev->bd_disk->private_data;
321 if ((d->flags & DEVFL_UP) == 0) {
322 pr_err("aoe: disk not up\n");
323 return -ENODEV;
324 }
325
326 if (cmd == HDIO_GET_IDENTITY) {
327 if (!copy_to_user((void __user *) arg, &d->ident,
328 sizeof(d->ident)))
329 return 0;
330 return -EFAULT;
331 }
332
333 /* udev calls scsi_id, which uses SG_IO, resulting in noise */
334 if (cmd != SG_IO)
335 pr_info("aoe: unknown ioctl 0x%x\n", cmd);
336
337 return -ENOTTY;
338}
339
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700340static const struct block_device_operations aoe_bdops = {
Al Viro94562c12008-03-02 09:23:18 -0500341 .open = aoeblk_open,
342 .release = aoeblk_release,
Ed Cashin667be1e2012-12-17 16:03:42 -0800343 .ioctl = aoeblk_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800344 .getgeo = aoeblk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 .owner = THIS_MODULE,
346};
347
348/* alloc_disk and add_disk can sleep */
349void
350aoeblk_gdalloc(void *vp)
351{
352 struct aoedev *d = vp;
353 struct gendisk *gd;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700354 mempool_t *mp;
355 struct request_queue *q;
356 enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 ulong flags;
Ed Cashine52a2932012-12-17 16:04:09 -0800358 int late = 0;
359
360 spin_lock_irqsave(&d->lock, flags);
361 if (d->flags & DEVFL_GDALLOC
362 && !(d->flags & DEVFL_TKILL)
363 && !(d->flags & DEVFL_GD_NOW))
364 d->flags |= DEVFL_GD_NOW;
365 else
366 late = 1;
367 spin_unlock_irqrestore(&d->lock, flags);
368 if (late)
369 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 gd = alloc_disk(AOE_PARTITIONS);
372 if (gd == NULL) {
Ed Cashin69cf2d852012-10-04 17:16:23 -0700373 pr_err("aoe: cannot allocate disk structure for %ld.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400374 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800375 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
Ed Cashin69cf2d852012-10-04 17:16:23 -0700378 mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab,
379 buf_pool_cache);
380 if (mp == NULL) {
Ed L. Cashin1d759812008-02-08 04:20:08 -0800381 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
Ed L. Cashin6bb6285f2006-09-20 14:36:49 -0400382 d->aoemajor, d->aoeminor);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800383 goto err_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Ed Cashin69cf2d852012-10-04 17:16:23 -0700385 q = blk_init_queue(aoeblk_request, &d->lock);
386 if (q == NULL) {
387 pr_err("aoe: cannot allocate block queue for %ld.%d\n",
388 d->aoemajor, d->aoeminor);
Ed Cashin0a414092012-12-17 16:03:58 -0800389 goto err_mempool;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800392 spin_lock_irqsave(&d->lock, flags);
Ed Cashine52a2932012-12-17 16:04:09 -0800393 WARN_ON(!(d->flags & DEVFL_GD_NOW));
394 WARN_ON(!(d->flags & DEVFL_GDALLOC));
395 WARN_ON(d->flags & DEVFL_TKILL);
396 WARN_ON(d->gd);
397 WARN_ON(d->flags & DEVFL_UP);
Jeff Moyer30e2bc02015-08-13 14:57:56 -0400398 blk_queue_max_hw_sectors(q, BLK_DEF_MAX_SECTORS);
Ed Cashin0a414092012-12-17 16:03:58 -0800399 q->backing_dev_info.name = "aoe";
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300400 q->backing_dev_info.ra_pages = READ_AHEAD / PAGE_SIZE;
Ed Cashin69cf2d852012-10-04 17:16:23 -0700401 d->bufpool = mp;
402 d->blkq = gd->queue = q;
403 q->queuedata = d;
404 d->gd = gd;
Ed Cashinaa304fd2012-12-17 16:03:32 -0800405 if (aoe_maxsectors)
406 blk_queue_max_hw_sectors(q, aoe_maxsectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 gd->major = AOE_MAJOR;
Ed Cashin0c966212012-10-04 17:16:40 -0700408 gd->first_minor = d->sysminor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 gd->fops = &aoe_bdops;
410 gd->private_data = d;
Tejun Heo80795ae2008-08-25 19:56:07 +0900411 set_capacity(gd, d->ssize);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800412 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 d->aoemajor, d->aoeminor);
414
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500415 d->flags &= ~DEVFL_GDALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 d->flags |= DEVFL_UP;
417
418 spin_unlock_irqrestore(&d->lock, flags);
419
420 add_disk(gd);
421 aoedisk_add_sysfs(d);
Ed Cashine8866cf2013-09-11 14:25:40 -0700422 aoedisk_add_debugfs(d);
Ed Cashine52a2932012-12-17 16:04:09 -0800423
424 spin_lock_irqsave(&d->lock, flags);
425 WARN_ON(!(d->flags & DEVFL_GD_NOW));
426 d->flags &= ~DEVFL_GD_NOW;
427 spin_unlock_irqrestore(&d->lock, flags);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800428 return;
429
430err_mempool:
Ed Cashin0a414092012-12-17 16:03:58 -0800431 mempool_destroy(mp);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800432err_disk:
433 put_disk(gd);
434err:
435 spin_lock_irqsave(&d->lock, flags);
Ed Cashine52a2932012-12-17 16:04:09 -0800436 d->flags &= ~DEVFL_GD_NOW;
437 schedule_work(&d->work);
Andrew Morton43cbe2c2007-12-10 15:49:13 -0800438 spin_unlock_irqrestore(&d->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441void
442aoeblk_exit(void)
443{
Ed Cashin190519c2013-09-11 14:25:39 -0700444 debugfs_remove_recursive(aoe_debugfs_dir);
445 aoe_debugfs_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 kmem_cache_destroy(buf_pool_cache);
447}
448
449int __init
450aoeblk_init(void)
451{
Paul Mundt20c2df82007-07-20 10:11:58 +0900452 buf_pool_cache = kmem_cache_create("aoe_bufs",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 sizeof(struct buf),
Paul Mundt20c2df82007-07-20 10:11:58 +0900454 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (buf_pool_cache == NULL)
456 return -ENOMEM;
Ed Cashin190519c2013-09-11 14:25:39 -0700457 aoe_debugfs_dir = debugfs_create_dir("aoe", NULL);
458 if (IS_ERR_OR_NULL(aoe_debugfs_dir)) {
459 pr_info("aoe: cannot create debugfs directory\n");
460 aoe_debugfs_dir = NULL;
461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return 0;
463}
464