blob: 45e0b565f448dbdfdffdefe856aa161d367c7bbc [file] [log] [blame]
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001/*
2 * Add configfs and memory store: Kyungchan Koh <kkc6196@fb.com> and
3 * Shaohua Li <shli@fb.com>
4 */
Jens Axboef2298c02013-10-25 11:52:25 +01005#include <linux/module.h>
Matias Bjørlingfc1bc352013-12-21 00:11:01 +01006
Jens Axboef2298c02013-10-25 11:52:25 +01007#include <linux/moduleparam.h>
8#include <linux/sched.h>
9#include <linux/fs.h>
10#include <linux/blkdev.h>
11#include <linux/init.h>
12#include <linux/slab.h>
13#include <linux/blk-mq.h>
14#include <linux/hrtimer.h>
Matias Bjørlingb2b7e002015-11-12 20:25:10 +010015#include <linux/lightnvm.h>
Shaohua Li3bf2bd22017-08-14 15:04:53 -070016#include <linux/configfs.h>
Jens Axboef2298c02013-10-25 11:52:25 +010017
Shaohua Li5bcd0e02017-08-14 15:04:56 -070018#define SECTOR_SHIFT 9
19#define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
20#define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT)
21#define SECTOR_SIZE (1 << SECTOR_SHIFT)
22#define SECTOR_MASK (PAGE_SECTORS - 1)
23
24#define FREE_BATCH 16
25
Jens Axboef2298c02013-10-25 11:52:25 +010026struct nullb_cmd {
27 struct list_head list;
28 struct llist_node ll_list;
29 struct call_single_data csd;
30 struct request *rq;
31 struct bio *bio;
32 unsigned int tag;
33 struct nullb_queue *nq;
Paolo Valente3c395a92015-12-01 11:48:17 +010034 struct hrtimer timer;
Shaohua Li5bcd0e02017-08-14 15:04:56 -070035 blk_status_t error;
Jens Axboef2298c02013-10-25 11:52:25 +010036};
37
38struct nullb_queue {
39 unsigned long *tag_map;
40 wait_queue_head_t wait;
41 unsigned int queue_depth;
Shaohua Li2984c862017-08-14 15:04:52 -070042 struct nullb_device *dev;
Jens Axboef2298c02013-10-25 11:52:25 +010043
44 struct nullb_cmd *cmds;
45};
46
Shaohua Li3bf2bd22017-08-14 15:04:53 -070047/*
48 * Status flags for nullb_device.
49 *
50 * CONFIGURED: Device has been configured and turned on. Cannot reconfigure.
51 * UP: Device is currently on and visible in userspace.
52 */
53enum nullb_device_flags {
54 NULLB_DEV_FL_CONFIGURED = 0,
55 NULLB_DEV_FL_UP = 1,
56};
57
Shaohua Li5bcd0e02017-08-14 15:04:56 -070058/*
59 * nullb_page is a page in memory for nullb devices.
60 *
61 * @page: The page holding the data.
62 * @bitmap: The bitmap represents which sector in the page has data.
63 * Each bit represents one block size. For example, sector 8
64 * will use the 7th bit
65 */
66struct nullb_page {
67 struct page *page;
68 unsigned long bitmap;
69};
70
Shaohua Li2984c862017-08-14 15:04:52 -070071struct nullb_device {
72 struct nullb *nullb;
Shaohua Li3bf2bd22017-08-14 15:04:53 -070073 struct config_item item;
Shaohua Li5bcd0e02017-08-14 15:04:56 -070074 struct radix_tree_root data; /* data stored in the disk */
Shaohua Li3bf2bd22017-08-14 15:04:53 -070075 unsigned long flags; /* device flags */
Shaohua Li2984c862017-08-14 15:04:52 -070076
77 unsigned long size; /* device size in MB */
78 unsigned long completion_nsec; /* time in ns to complete a request */
79 unsigned int submit_queues; /* number of submission queues */
80 unsigned int home_node; /* home node for the device */
81 unsigned int queue_mode; /* block interface */
82 unsigned int blocksize; /* block size */
83 unsigned int irqmode; /* IRQ completion handler */
84 unsigned int hw_queue_depth; /* queue depth */
Shaohua Licedcafa2017-08-14 15:04:54 -070085 unsigned int index; /* index of the disk, only valid with a disk */
Shaohua Li2984c862017-08-14 15:04:52 -070086 bool use_lightnvm; /* register as a LightNVM device */
87 bool blocking; /* blocking blk-mq device */
88 bool use_per_node_hctx; /* use per-node allocation for hardware context */
Shaohua Licedcafa2017-08-14 15:04:54 -070089 bool power; /* power on/off the device */
Shaohua Li5bcd0e02017-08-14 15:04:56 -070090 bool memory_backed; /* if data is stored in memory */
Shaohua Li2984c862017-08-14 15:04:52 -070091};
92
Jens Axboef2298c02013-10-25 11:52:25 +010093struct nullb {
Shaohua Li2984c862017-08-14 15:04:52 -070094 struct nullb_device *dev;
Jens Axboef2298c02013-10-25 11:52:25 +010095 struct list_head list;
96 unsigned int index;
97 struct request_queue *q;
98 struct gendisk *disk;
Matias Bjørlingb0b4e092016-09-16 14:25:07 +020099 struct nvm_dev *ndev;
Jens Axboe82f402f2017-06-20 14:22:01 -0600100 struct blk_mq_tag_set *tag_set;
101 struct blk_mq_tag_set __tag_set;
Jens Axboef2298c02013-10-25 11:52:25 +0100102 struct hrtimer timer;
103 unsigned int queue_depth;
104 spinlock_t lock;
105
106 struct nullb_queue *queues;
107 unsigned int nr_queues;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +0100108 char disk_name[DISK_NAME_LEN];
Jens Axboef2298c02013-10-25 11:52:25 +0100109};
110
111static LIST_HEAD(nullb_list);
112static struct mutex lock;
113static int null_major;
Shaohua Li94bc02e2017-08-14 15:04:55 -0700114static DEFINE_IDA(nullb_indexes);
Matias Bjørling6bb95352015-11-19 12:50:08 +0100115static struct kmem_cache *ppa_cache;
Jens Axboe82f402f2017-06-20 14:22:01 -0600116static struct blk_mq_tag_set tag_set;
Jens Axboef2298c02013-10-25 11:52:25 +0100117
Jens Axboef2298c02013-10-25 11:52:25 +0100118enum {
119 NULL_IRQ_NONE = 0,
120 NULL_IRQ_SOFTIRQ = 1,
121 NULL_IRQ_TIMER = 2,
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800122};
Jens Axboef2298c02013-10-25 11:52:25 +0100123
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800124enum {
Jens Axboef2298c02013-10-25 11:52:25 +0100125 NULL_Q_BIO = 0,
126 NULL_Q_RQ = 1,
127 NULL_Q_MQ = 2,
128};
129
Shaohua Li2984c862017-08-14 15:04:52 -0700130static int g_submit_queues = 1;
131module_param_named(submit_queues, g_submit_queues, int, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100132MODULE_PARM_DESC(submit_queues, "Number of submission queues");
133
Shaohua Li2984c862017-08-14 15:04:52 -0700134static int g_home_node = NUMA_NO_NODE;
135module_param_named(home_node, g_home_node, int, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100136MODULE_PARM_DESC(home_node, "Home node for the device");
137
Shaohua Li2984c862017-08-14 15:04:52 -0700138static int g_queue_mode = NULL_Q_MQ;
Matias Bjorling709c8662014-11-26 14:45:48 -0700139
140static int null_param_store_val(const char *str, int *val, int min, int max)
141{
142 int ret, new_val;
143
144 ret = kstrtoint(str, 10, &new_val);
145 if (ret)
146 return -EINVAL;
147
148 if (new_val < min || new_val > max)
149 return -EINVAL;
150
151 *val = new_val;
152 return 0;
153}
154
155static int null_set_queue_mode(const char *str, const struct kernel_param *kp)
156{
Shaohua Li2984c862017-08-14 15:04:52 -0700157 return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ);
Matias Bjorling709c8662014-11-26 14:45:48 -0700158}
159
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930160static const struct kernel_param_ops null_queue_mode_param_ops = {
Matias Bjorling709c8662014-11-26 14:45:48 -0700161 .set = null_set_queue_mode,
162 .get = param_get_int,
163};
164
Shaohua Li2984c862017-08-14 15:04:52 -0700165device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, S_IRUGO);
Mike Snitzer54ae81c2014-06-11 17:13:50 -0400166MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
Jens Axboef2298c02013-10-25 11:52:25 +0100167
Shaohua Li2984c862017-08-14 15:04:52 -0700168static int g_gb = 250;
169module_param_named(gb, g_gb, int, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100170MODULE_PARM_DESC(gb, "Size in GB");
171
Shaohua Li2984c862017-08-14 15:04:52 -0700172static int g_bs = 512;
173module_param_named(bs, g_bs, int, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100174MODULE_PARM_DESC(bs, "Block size (in bytes)");
175
Jens Axboe82f402f2017-06-20 14:22:01 -0600176static int nr_devices = 1;
Jens Axboef2298c02013-10-25 11:52:25 +0100177module_param(nr_devices, int, S_IRUGO);
178MODULE_PARM_DESC(nr_devices, "Number of devices to register");
179
Shaohua Li2984c862017-08-14 15:04:52 -0700180static bool g_use_lightnvm;
181module_param_named(use_lightnvm, g_use_lightnvm, bool, S_IRUGO);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +0100182MODULE_PARM_DESC(use_lightnvm, "Register as a LightNVM device");
183
Shaohua Li2984c862017-08-14 15:04:52 -0700184static bool g_blocking;
185module_param_named(blocking, g_blocking, bool, S_IRUGO);
Jens Axboedb5bcf82017-03-30 13:44:26 -0600186MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device");
187
Jens Axboe82f402f2017-06-20 14:22:01 -0600188static bool shared_tags;
189module_param(shared_tags, bool, S_IRUGO);
190MODULE_PARM_DESC(shared_tags, "Share tag set between devices for blk-mq");
191
Shaohua Li2984c862017-08-14 15:04:52 -0700192static int g_irqmode = NULL_IRQ_SOFTIRQ;
Matias Bjorling709c8662014-11-26 14:45:48 -0700193
194static int null_set_irqmode(const char *str, const struct kernel_param *kp)
195{
Shaohua Li2984c862017-08-14 15:04:52 -0700196 return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE,
Matias Bjorling709c8662014-11-26 14:45:48 -0700197 NULL_IRQ_TIMER);
198}
199
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930200static const struct kernel_param_ops null_irqmode_param_ops = {
Matias Bjorling709c8662014-11-26 14:45:48 -0700201 .set = null_set_irqmode,
202 .get = param_get_int,
203};
204
Shaohua Li2984c862017-08-14 15:04:52 -0700205device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100206MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
207
Shaohua Li2984c862017-08-14 15:04:52 -0700208static unsigned long g_completion_nsec = 10000;
209module_param_named(completion_nsec, g_completion_nsec, ulong, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100210MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");
211
Shaohua Li2984c862017-08-14 15:04:52 -0700212static int g_hw_queue_depth = 64;
213module_param_named(hw_queue_depth, g_hw_queue_depth, int, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100214MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");
215
Shaohua Li2984c862017-08-14 15:04:52 -0700216static bool g_use_per_node_hctx;
217module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, S_IRUGO);
Matias Bjørling20005242013-12-21 00:11:00 +0100218MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
Jens Axboef2298c02013-10-25 11:52:25 +0100219
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700220static struct nullb_device *null_alloc_dev(void);
221static void null_free_dev(struct nullb_device *dev);
Shaohua Licedcafa2017-08-14 15:04:54 -0700222static void null_del_dev(struct nullb *nullb);
223static int null_add_dev(struct nullb_device *dev);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700224static void null_free_device_storage(struct nullb_device *dev);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700225
226static inline struct nullb_device *to_nullb_device(struct config_item *item)
227{
228 return item ? container_of(item, struct nullb_device, item) : NULL;
229}
230
231static inline ssize_t nullb_device_uint_attr_show(unsigned int val, char *page)
232{
233 return snprintf(page, PAGE_SIZE, "%u\n", val);
234}
235
236static inline ssize_t nullb_device_ulong_attr_show(unsigned long val,
237 char *page)
238{
239 return snprintf(page, PAGE_SIZE, "%lu\n", val);
240}
241
242static inline ssize_t nullb_device_bool_attr_show(bool val, char *page)
243{
244 return snprintf(page, PAGE_SIZE, "%u\n", val);
245}
246
247static ssize_t nullb_device_uint_attr_store(unsigned int *val,
248 const char *page, size_t count)
249{
250 unsigned int tmp;
251 int result;
252
253 result = kstrtouint(page, 0, &tmp);
254 if (result)
255 return result;
256
257 *val = tmp;
258 return count;
259}
260
261static ssize_t nullb_device_ulong_attr_store(unsigned long *val,
262 const char *page, size_t count)
263{
264 int result;
265 unsigned long tmp;
266
267 result = kstrtoul(page, 0, &tmp);
268 if (result)
269 return result;
270
271 *val = tmp;
272 return count;
273}
274
275static ssize_t nullb_device_bool_attr_store(bool *val, const char *page,
276 size_t count)
277{
278 bool tmp;
279 int result;
280
281 result = kstrtobool(page, &tmp);
282 if (result)
283 return result;
284
285 *val = tmp;
286 return count;
287}
288
289/* The following macro should only be used with TYPE = {uint, ulong, bool}. */
290#define NULLB_DEVICE_ATTR(NAME, TYPE) \
291static ssize_t \
292nullb_device_##NAME##_show(struct config_item *item, char *page) \
293{ \
294 return nullb_device_##TYPE##_attr_show( \
295 to_nullb_device(item)->NAME, page); \
296} \
297static ssize_t \
298nullb_device_##NAME##_store(struct config_item *item, const char *page, \
299 size_t count) \
300{ \
301 if (test_bit(NULLB_DEV_FL_CONFIGURED, &to_nullb_device(item)->flags)) \
302 return -EBUSY; \
303 return nullb_device_##TYPE##_attr_store( \
304 &to_nullb_device(item)->NAME, page, count); \
305} \
306CONFIGFS_ATTR(nullb_device_, NAME);
307
308NULLB_DEVICE_ATTR(size, ulong);
309NULLB_DEVICE_ATTR(completion_nsec, ulong);
310NULLB_DEVICE_ATTR(submit_queues, uint);
311NULLB_DEVICE_ATTR(home_node, uint);
312NULLB_DEVICE_ATTR(queue_mode, uint);
313NULLB_DEVICE_ATTR(blocksize, uint);
314NULLB_DEVICE_ATTR(irqmode, uint);
315NULLB_DEVICE_ATTR(hw_queue_depth, uint);
Shaohua Licedcafa2017-08-14 15:04:54 -0700316NULLB_DEVICE_ATTR(index, uint);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700317NULLB_DEVICE_ATTR(use_lightnvm, bool);
318NULLB_DEVICE_ATTR(blocking, bool);
319NULLB_DEVICE_ATTR(use_per_node_hctx, bool);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700320NULLB_DEVICE_ATTR(memory_backed, bool);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700321
Shaohua Licedcafa2017-08-14 15:04:54 -0700322static ssize_t nullb_device_power_show(struct config_item *item, char *page)
323{
324 return nullb_device_bool_attr_show(to_nullb_device(item)->power, page);
325}
326
327static ssize_t nullb_device_power_store(struct config_item *item,
328 const char *page, size_t count)
329{
330 struct nullb_device *dev = to_nullb_device(item);
331 bool newp = false;
332 ssize_t ret;
333
334 ret = nullb_device_bool_attr_store(&newp, page, count);
335 if (ret < 0)
336 return ret;
337
338 if (!dev->power && newp) {
339 if (test_and_set_bit(NULLB_DEV_FL_UP, &dev->flags))
340 return count;
341 if (null_add_dev(dev)) {
342 clear_bit(NULLB_DEV_FL_UP, &dev->flags);
343 return -ENOMEM;
344 }
345
346 set_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
347 dev->power = newp;
348 } else if (to_nullb_device(item)->power && !newp) {
349 mutex_lock(&lock);
350 dev->power = newp;
351 null_del_dev(dev->nullb);
352 mutex_unlock(&lock);
353 clear_bit(NULLB_DEV_FL_UP, &dev->flags);
354 }
355
356 return count;
357}
358
359CONFIGFS_ATTR(nullb_device_, power);
360
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700361static struct configfs_attribute *nullb_device_attrs[] = {
362 &nullb_device_attr_size,
363 &nullb_device_attr_completion_nsec,
364 &nullb_device_attr_submit_queues,
365 &nullb_device_attr_home_node,
366 &nullb_device_attr_queue_mode,
367 &nullb_device_attr_blocksize,
368 &nullb_device_attr_irqmode,
369 &nullb_device_attr_hw_queue_depth,
Shaohua Licedcafa2017-08-14 15:04:54 -0700370 &nullb_device_attr_index,
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700371 &nullb_device_attr_use_lightnvm,
372 &nullb_device_attr_blocking,
373 &nullb_device_attr_use_per_node_hctx,
Shaohua Licedcafa2017-08-14 15:04:54 -0700374 &nullb_device_attr_power,
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700375 &nullb_device_attr_memory_backed,
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700376 NULL,
377};
378
379static void nullb_device_release(struct config_item *item)
380{
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700381 struct nullb_device *dev = to_nullb_device(item);
382
383 null_free_device_storage(dev);
384 null_free_dev(dev);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700385}
386
387static struct configfs_item_operations nullb_device_ops = {
388 .release = nullb_device_release,
389};
390
391static struct config_item_type nullb_device_type = {
392 .ct_item_ops = &nullb_device_ops,
393 .ct_attrs = nullb_device_attrs,
394 .ct_owner = THIS_MODULE,
395};
396
397static struct
398config_item *nullb_group_make_item(struct config_group *group, const char *name)
399{
400 struct nullb_device *dev;
401
402 dev = null_alloc_dev();
403 if (!dev)
404 return ERR_PTR(-ENOMEM);
405
406 config_item_init_type_name(&dev->item, name, &nullb_device_type);
407
408 return &dev->item;
409}
410
411static void
412nullb_group_drop_item(struct config_group *group, struct config_item *item)
413{
Shaohua Licedcafa2017-08-14 15:04:54 -0700414 struct nullb_device *dev = to_nullb_device(item);
415
416 if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
417 mutex_lock(&lock);
418 dev->power = false;
419 null_del_dev(dev->nullb);
420 mutex_unlock(&lock);
421 }
422
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700423 config_item_put(item);
424}
425
426static ssize_t memb_group_features_show(struct config_item *item, char *page)
427{
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700428 return snprintf(page, PAGE_SIZE, "memory_backed\n");
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700429}
430
431CONFIGFS_ATTR_RO(memb_group_, features);
432
433static struct configfs_attribute *nullb_group_attrs[] = {
434 &memb_group_attr_features,
435 NULL,
436};
437
438static struct configfs_group_operations nullb_group_ops = {
439 .make_item = nullb_group_make_item,
440 .drop_item = nullb_group_drop_item,
441};
442
443static struct config_item_type nullb_group_type = {
444 .ct_group_ops = &nullb_group_ops,
445 .ct_attrs = nullb_group_attrs,
446 .ct_owner = THIS_MODULE,
447};
448
449static struct configfs_subsystem nullb_subsys = {
450 .su_group = {
451 .cg_item = {
452 .ci_namebuf = "nullb",
453 .ci_type = &nullb_group_type,
454 },
455 },
456};
457
Shaohua Li2984c862017-08-14 15:04:52 -0700458static struct nullb_device *null_alloc_dev(void)
459{
460 struct nullb_device *dev;
461
462 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
463 if (!dev)
464 return NULL;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700465 INIT_RADIX_TREE(&dev->data, GFP_ATOMIC);
Shaohua Li2984c862017-08-14 15:04:52 -0700466 dev->size = g_gb * 1024;
467 dev->completion_nsec = g_completion_nsec;
468 dev->submit_queues = g_submit_queues;
469 dev->home_node = g_home_node;
470 dev->queue_mode = g_queue_mode;
471 dev->blocksize = g_bs;
472 dev->irqmode = g_irqmode;
473 dev->hw_queue_depth = g_hw_queue_depth;
474 dev->use_lightnvm = g_use_lightnvm;
475 dev->blocking = g_blocking;
476 dev->use_per_node_hctx = g_use_per_node_hctx;
477 return dev;
478}
479
480static void null_free_dev(struct nullb_device *dev)
481{
482 kfree(dev);
483}
484
Jens Axboef2298c02013-10-25 11:52:25 +0100485static void put_tag(struct nullb_queue *nq, unsigned int tag)
486{
487 clear_bit_unlock(tag, nq->tag_map);
488
489 if (waitqueue_active(&nq->wait))
490 wake_up(&nq->wait);
491}
492
493static unsigned int get_tag(struct nullb_queue *nq)
494{
495 unsigned int tag;
496
497 do {
498 tag = find_first_zero_bit(nq->tag_map, nq->queue_depth);
499 if (tag >= nq->queue_depth)
500 return -1U;
501 } while (test_and_set_bit_lock(tag, nq->tag_map));
502
503 return tag;
504}
505
506static void free_cmd(struct nullb_cmd *cmd)
507{
508 put_tag(cmd->nq, cmd->tag);
509}
510
Paolo Valente3c395a92015-12-01 11:48:17 +0100511static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer);
512
Jens Axboef2298c02013-10-25 11:52:25 +0100513static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq)
514{
515 struct nullb_cmd *cmd;
516 unsigned int tag;
517
518 tag = get_tag(nq);
519 if (tag != -1U) {
520 cmd = &nq->cmds[tag];
521 cmd->tag = tag;
522 cmd->nq = nq;
Shaohua Li2984c862017-08-14 15:04:52 -0700523 if (nq->dev->irqmode == NULL_IRQ_TIMER) {
Paolo Valente3c395a92015-12-01 11:48:17 +0100524 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC,
525 HRTIMER_MODE_REL);
526 cmd->timer.function = null_cmd_timer_expired;
527 }
Jens Axboef2298c02013-10-25 11:52:25 +0100528 return cmd;
529 }
530
531 return NULL;
532}
533
534static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait)
535{
536 struct nullb_cmd *cmd;
537 DEFINE_WAIT(wait);
538
539 cmd = __alloc_cmd(nq);
540 if (cmd || !can_wait)
541 return cmd;
542
543 do {
544 prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE);
545 cmd = __alloc_cmd(nq);
546 if (cmd)
547 break;
548
549 io_schedule();
550 } while (1);
551
552 finish_wait(&nq->wait, &wait);
553 return cmd;
554}
555
556static void end_cmd(struct nullb_cmd *cmd)
557{
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100558 struct request_queue *q = NULL;
Shaohua Li2984c862017-08-14 15:04:52 -0700559 int queue_mode = cmd->nq->dev->queue_mode;
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100560
Mike Krinkine8271202015-12-15 12:56:40 +0300561 if (cmd->rq)
562 q = cmd->rq->q;
563
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800564 switch (queue_mode) {
565 case NULL_Q_MQ:
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700566 blk_mq_end_request(cmd->rq, cmd->error);
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800567 return;
568 case NULL_Q_RQ:
569 INIT_LIST_HEAD(&cmd->rq->queuelist);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700570 blk_end_request_all(cmd->rq, cmd->error);
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800571 break;
572 case NULL_Q_BIO:
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700573 cmd->bio->bi_status = cmd->error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200574 bio_endio(cmd->bio);
Jens Axboe48cc6612015-12-28 13:02:47 -0700575 break;
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800576 }
Jens Axboef2298c02013-10-25 11:52:25 +0100577
Jens Axboe48cc6612015-12-28 13:02:47 -0700578 free_cmd(cmd);
579
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100580 /* Restart queue if needed, as we are freeing a tag */
Jens Axboe48cc6612015-12-28 13:02:47 -0700581 if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) {
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100582 unsigned long flags;
583
584 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe48cc6612015-12-28 13:02:47 -0700585 blk_start_queue_async(q);
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100586 spin_unlock_irqrestore(q->queue_lock, flags);
587 }
Jens Axboef2298c02013-10-25 11:52:25 +0100588}
589
590static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
591{
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100592 end_cmd(container_of(timer, struct nullb_cmd, timer));
Jens Axboef2298c02013-10-25 11:52:25 +0100593
594 return HRTIMER_NORESTART;
595}
596
597static void null_cmd_end_timer(struct nullb_cmd *cmd)
598{
Shaohua Li2984c862017-08-14 15:04:52 -0700599 ktime_t kt = cmd->nq->dev->completion_nsec;
Jens Axboef2298c02013-10-25 11:52:25 +0100600
Paolo Valente3c395a92015-12-01 11:48:17 +0100601 hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
Jens Axboef2298c02013-10-25 11:52:25 +0100602}
603
604static void null_softirq_done_fn(struct request *rq)
605{
Shaohua Li2984c862017-08-14 15:04:52 -0700606 struct nullb *nullb = rq->q->queuedata;
607
608 if (nullb->dev->queue_mode == NULL_Q_MQ)
Jens Axboed891fa72014-06-16 11:40:25 -0600609 end_cmd(blk_mq_rq_to_pdu(rq));
610 else
611 end_cmd(rq->special);
Jens Axboef2298c02013-10-25 11:52:25 +0100612}
613
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700614static struct nullb_page *null_alloc_page(gfp_t gfp_flags)
Jens Axboef2298c02013-10-25 11:52:25 +0100615{
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700616 struct nullb_page *t_page;
617
618 t_page = kmalloc(sizeof(struct nullb_page), gfp_flags);
619 if (!t_page)
620 goto out;
621
622 t_page->page = alloc_pages(gfp_flags, 0);
623 if (!t_page->page)
624 goto out_freepage;
625
626 t_page->bitmap = 0;
627 return t_page;
628out_freepage:
629 kfree(t_page);
630out:
631 return NULL;
632}
633
634static void null_free_page(struct nullb_page *t_page)
635{
636 __free_page(t_page->page);
637 kfree(t_page);
638}
639
640static void null_free_sector(struct nullb *nullb, sector_t sector)
641{
642 unsigned int sector_bit;
643 u64 idx;
644 struct nullb_page *t_page, *ret;
645 struct radix_tree_root *root;
646
647 root = &nullb->dev->data;
648 idx = sector >> PAGE_SECTORS_SHIFT;
649 sector_bit = (sector & SECTOR_MASK);
650
651 t_page = radix_tree_lookup(root, idx);
652 if (t_page) {
653 __clear_bit(sector_bit, &t_page->bitmap);
654
655 if (!t_page->bitmap) {
656 ret = radix_tree_delete_item(root, idx, t_page);
657 WARN_ON(ret != t_page);
658 null_free_page(ret);
659 }
660 }
661}
662
663static struct nullb_page *null_radix_tree_insert(struct nullb *nullb, u64 idx,
664 struct nullb_page *t_page)
665{
666 struct radix_tree_root *root;
667
668 root = &nullb->dev->data;
669
670 if (radix_tree_insert(root, idx, t_page)) {
671 null_free_page(t_page);
672 t_page = radix_tree_lookup(root, idx);
673 WARN_ON(!t_page || t_page->page->index != idx);
674 }
675
676 return t_page;
677}
678
679static void null_free_device_storage(struct nullb_device *dev)
680{
681 unsigned long pos = 0;
682 int nr_pages;
683 struct nullb_page *ret, *t_pages[FREE_BATCH];
684 struct radix_tree_root *root;
685
686 root = &dev->data;
687
688 do {
689 int i;
690
691 nr_pages = radix_tree_gang_lookup(root,
692 (void **)t_pages, pos, FREE_BATCH);
693
694 for (i = 0; i < nr_pages; i++) {
695 pos = t_pages[i]->page->index;
696 ret = radix_tree_delete_item(root, pos, t_pages[i]);
697 WARN_ON(ret != t_pages[i]);
698 null_free_page(ret);
699 }
700
701 pos++;
702 } while (nr_pages == FREE_BATCH);
703}
704
705static struct nullb_page *null_lookup_page(struct nullb *nullb,
706 sector_t sector, bool for_write)
707{
708 unsigned int sector_bit;
709 u64 idx;
710 struct nullb_page *t_page;
711
712 idx = sector >> PAGE_SECTORS_SHIFT;
713 sector_bit = (sector & SECTOR_MASK);
714
715 t_page = radix_tree_lookup(&nullb->dev->data, idx);
716 WARN_ON(t_page && t_page->page->index != idx);
717
718 if (t_page && (for_write || test_bit(sector_bit, &t_page->bitmap)))
719 return t_page;
720
721 return NULL;
722}
723
724static struct nullb_page *null_insert_page(struct nullb *nullb,
725 sector_t sector)
726{
727 u64 idx;
728 struct nullb_page *t_page;
729
730 t_page = null_lookup_page(nullb, sector, true);
731 if (t_page)
732 return t_page;
733
734 spin_unlock_irq(&nullb->lock);
735
736 t_page = null_alloc_page(GFP_NOIO);
737 if (!t_page)
738 goto out_lock;
739
740 if (radix_tree_preload(GFP_NOIO))
741 goto out_freepage;
742
743 spin_lock_irq(&nullb->lock);
744 idx = sector >> PAGE_SECTORS_SHIFT;
745 t_page->page->index = idx;
746 t_page = null_radix_tree_insert(nullb, idx, t_page);
747 radix_tree_preload_end();
748
749 return t_page;
750out_freepage:
751 null_free_page(t_page);
752out_lock:
753 spin_lock_irq(&nullb->lock);
754 return null_lookup_page(nullb, sector, true);
755}
756
757static int copy_to_nullb(struct nullb *nullb, struct page *source,
758 unsigned int off, sector_t sector, size_t n)
759{
760 size_t temp, count = 0;
761 unsigned int offset;
762 struct nullb_page *t_page;
763 void *dst, *src;
764
765 while (count < n) {
766 temp = min_t(size_t, nullb->dev->blocksize, n - count);
767
768 offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
769 t_page = null_insert_page(nullb, sector);
770 if (!t_page)
771 return -ENOSPC;
772
773 src = kmap_atomic(source);
774 dst = kmap_atomic(t_page->page);
775 memcpy(dst + offset, src + off + count, temp);
776 kunmap_atomic(dst);
777 kunmap_atomic(src);
778
779 __set_bit(sector & SECTOR_MASK, &t_page->bitmap);
780
781 count += temp;
782 sector += temp >> SECTOR_SHIFT;
783 }
784 return 0;
785}
786
787static int copy_from_nullb(struct nullb *nullb, struct page *dest,
788 unsigned int off, sector_t sector, size_t n)
789{
790 size_t temp, count = 0;
791 unsigned int offset;
792 struct nullb_page *t_page;
793 void *dst, *src;
794
795 while (count < n) {
796 temp = min_t(size_t, nullb->dev->blocksize, n - count);
797
798 offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
799 t_page = null_lookup_page(nullb, sector, false);
800
801 dst = kmap_atomic(dest);
802 if (!t_page) {
803 memset(dst + off + count, 0, temp);
804 goto next;
805 }
806 src = kmap_atomic(t_page->page);
807 memcpy(dst + off + count, src + offset, temp);
808 kunmap_atomic(src);
809next:
810 kunmap_atomic(dst);
811
812 count += temp;
813 sector += temp >> SECTOR_SHIFT;
814 }
815 return 0;
816}
817
818static int null_transfer(struct nullb *nullb, struct page *page,
819 unsigned int len, unsigned int off, bool is_write, sector_t sector)
820{
821 int err = 0;
822
823 if (!is_write) {
824 err = copy_from_nullb(nullb, page, off, sector, len);
825 flush_dcache_page(page);
826 } else {
827 flush_dcache_page(page);
828 err = copy_to_nullb(nullb, page, off, sector, len);
829 }
830
831 return err;
832}
833
834static int null_handle_rq(struct nullb_cmd *cmd)
835{
836 struct request *rq = cmd->rq;
837 struct nullb *nullb = cmd->nq->dev->nullb;
838 int err;
839 unsigned int len;
840 sector_t sector;
841 struct req_iterator iter;
842 struct bio_vec bvec;
843
844 sector = blk_rq_pos(rq);
845
846 spin_lock_irq(&nullb->lock);
847 rq_for_each_segment(bvec, rq, iter) {
848 len = bvec.bv_len;
849 err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
850 op_is_write(req_op(rq)), sector);
851 if (err) {
852 spin_unlock_irq(&nullb->lock);
853 return err;
854 }
855 sector += len >> SECTOR_SHIFT;
856 }
857 spin_unlock_irq(&nullb->lock);
858
859 return 0;
860}
861
862static int null_handle_bio(struct nullb_cmd *cmd)
863{
864 struct bio *bio = cmd->bio;
865 struct nullb *nullb = cmd->nq->dev->nullb;
866 int err;
867 unsigned int len;
868 sector_t sector;
869 struct bio_vec bvec;
870 struct bvec_iter iter;
871
872 sector = bio->bi_iter.bi_sector;
873
874 spin_lock_irq(&nullb->lock);
875 bio_for_each_segment(bvec, bio, iter) {
876 len = bvec.bv_len;
877 err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
878 op_is_write(bio_op(bio)), sector);
879 if (err) {
880 spin_unlock_irq(&nullb->lock);
881 return err;
882 }
883 sector += len >> SECTOR_SHIFT;
884 }
885 spin_unlock_irq(&nullb->lock);
886 return 0;
887}
888
889static blk_status_t null_handle_cmd(struct nullb_cmd *cmd)
890{
891 struct nullb_device *dev = cmd->nq->dev;
892 int err = 0;
893
894 if (dev->memory_backed) {
895 if (dev->queue_mode == NULL_Q_BIO)
896 err = null_handle_bio(cmd);
897 else
898 err = null_handle_rq(cmd);
899 }
900 cmd->error = errno_to_blk_status(err);
Jens Axboef2298c02013-10-25 11:52:25 +0100901 /* Complete IO by inline, softirq or timer */
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700902 switch (dev->irqmode) {
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800903 case NULL_IRQ_SOFTIRQ:
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700904 switch (dev->queue_mode) {
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800905 case NULL_Q_MQ:
Christoph Hellwig08e00292017-04-20 16:03:09 +0200906 blk_mq_complete_request(cmd->rq);
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800907 break;
908 case NULL_Q_RQ:
909 blk_complete_request(cmd->rq);
910 break;
911 case NULL_Q_BIO:
912 /*
913 * XXX: no proper submitting cpu information available.
914 */
915 end_cmd(cmd);
916 break;
917 }
918 break;
Jens Axboef2298c02013-10-25 11:52:25 +0100919 case NULL_IRQ_NONE:
920 end_cmd(cmd);
921 break;
Jens Axboef2298c02013-10-25 11:52:25 +0100922 case NULL_IRQ_TIMER:
923 null_cmd_end_timer(cmd);
924 break;
925 }
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700926 return BLK_STS_OK;
Jens Axboef2298c02013-10-25 11:52:25 +0100927}
928
929static struct nullb_queue *nullb_to_queue(struct nullb *nullb)
930{
931 int index = 0;
932
933 if (nullb->nr_queues != 1)
934 index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues);
935
936 return &nullb->queues[index];
937}
938
Jens Axboedece1632015-11-05 10:41:16 -0700939static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio)
Jens Axboef2298c02013-10-25 11:52:25 +0100940{
941 struct nullb *nullb = q->queuedata;
942 struct nullb_queue *nq = nullb_to_queue(nullb);
943 struct nullb_cmd *cmd;
944
945 cmd = alloc_cmd(nq, 1);
946 cmd->bio = bio;
947
948 null_handle_cmd(cmd);
Jens Axboedece1632015-11-05 10:41:16 -0700949 return BLK_QC_T_NONE;
Jens Axboef2298c02013-10-25 11:52:25 +0100950}
951
952static int null_rq_prep_fn(struct request_queue *q, struct request *req)
953{
954 struct nullb *nullb = q->queuedata;
955 struct nullb_queue *nq = nullb_to_queue(nullb);
956 struct nullb_cmd *cmd;
957
958 cmd = alloc_cmd(nq, 0);
959 if (cmd) {
960 cmd->rq = req;
961 req->special = cmd;
962 return BLKPREP_OK;
963 }
Akinobu Mita8b70f452015-06-02 08:35:10 +0900964 blk_stop_queue(q);
Jens Axboef2298c02013-10-25 11:52:25 +0100965
966 return BLKPREP_DEFER;
967}
968
969static void null_request_fn(struct request_queue *q)
970{
971 struct request *rq;
972
973 while ((rq = blk_fetch_request(q)) != NULL) {
974 struct nullb_cmd *cmd = rq->special;
975
976 spin_unlock_irq(q->queue_lock);
977 null_handle_cmd(cmd);
978 spin_lock_irq(q->queue_lock);
979 }
980}
981
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200982static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx,
Jens Axboe74c45052014-10-29 11:14:52 -0600983 const struct blk_mq_queue_data *bd)
Jens Axboef2298c02013-10-25 11:52:25 +0100984{
Jens Axboe74c45052014-10-29 11:14:52 -0600985 struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Shaohua Li2984c862017-08-14 15:04:52 -0700986 struct nullb_queue *nq = hctx->driver_data;
Jens Axboef2298c02013-10-25 11:52:25 +0100987
Jens Axboedb5bcf82017-03-30 13:44:26 -0600988 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
989
Shaohua Li2984c862017-08-14 15:04:52 -0700990 if (nq->dev->irqmode == NULL_IRQ_TIMER) {
Paolo Valente3c395a92015-12-01 11:48:17 +0100991 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
992 cmd->timer.function = null_cmd_timer_expired;
993 }
Jens Axboe74c45052014-10-29 11:14:52 -0600994 cmd->rq = bd->rq;
Shaohua Li2984c862017-08-14 15:04:52 -0700995 cmd->nq = nq;
Jens Axboef2298c02013-10-25 11:52:25 +0100996
Jens Axboe74c45052014-10-29 11:14:52 -0600997 blk_mq_start_request(bd->rq);
Christoph Hellwige2490072014-09-13 16:40:09 -0700998
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700999 return null_handle_cmd(cmd);
Jens Axboef2298c02013-10-25 11:52:25 +01001000}
1001
Eric Biggersf363b082017-03-30 13:39:16 -07001002static const struct blk_mq_ops null_mq_ops = {
Jens Axboef2298c02013-10-25 11:52:25 +01001003 .queue_rq = null_queue_rq,
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001004 .complete = null_softirq_done_fn,
Jens Axboef2298c02013-10-25 11:52:25 +01001005};
1006
Matias Bjørlingde65d2d2015-08-31 14:17:18 +02001007static void cleanup_queue(struct nullb_queue *nq)
1008{
1009 kfree(nq->tag_map);
1010 kfree(nq->cmds);
1011}
1012
1013static void cleanup_queues(struct nullb *nullb)
1014{
1015 int i;
1016
1017 for (i = 0; i < nullb->nr_queues; i++)
1018 cleanup_queue(&nullb->queues[i]);
1019
1020 kfree(nullb->queues);
1021}
1022
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001023#ifdef CONFIG_NVM
1024
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001025static void null_lnvm_end_io(struct request *rq, blk_status_t status)
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001026{
1027 struct nvm_rq *rqd = rq->end_io_data;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001028
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001029 /* XXX: lighnvm core seems to expect NVM_RSP_* values here.. */
1030 rqd->error = status ? -EIO : 0;
Matias Bjørling06894ef2017-01-31 13:17:17 +01001031 nvm_end_io(rqd);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001032
1033 blk_put_request(rq);
1034}
1035
Matias Bjørling16f26c32015-12-06 11:25:48 +01001036static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001037{
Matias Bjørling16f26c32015-12-06 11:25:48 +01001038 struct request_queue *q = dev->q;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001039 struct request *rq;
1040 struct bio *bio = rqd->bio;
1041
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001042 rq = blk_mq_alloc_request(q,
1043 op_is_write(bio_op(bio)) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001044 if (IS_ERR(rq))
1045 return -ENOMEM;
1046
Bart Van Assche2644a3c2017-04-19 14:01:25 -07001047 blk_init_request_from_bio(rq, bio);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001048
1049 rq->end_io_data = rqd;
1050
1051 blk_execute_rq_nowait(q, NULL, rq, 0, null_lnvm_end_io);
1052
1053 return 0;
1054}
1055
Matias Bjørling16f26c32015-12-06 11:25:48 +01001056static int null_lnvm_id(struct nvm_dev *dev, struct nvm_id *id)
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001057{
Shaohua Li2984c862017-08-14 15:04:52 -07001058 struct nullb *nullb = dev->q->queuedata;
1059 sector_t size = (sector_t)nullb->dev->size * 1024 * 1024ULL;
Matias Bjørling5b40db92015-11-19 12:50:09 +01001060 sector_t blksize;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001061 struct nvm_id_group *grp;
1062
1063 id->ver_id = 0x1;
1064 id->vmnt = 0;
Matias Bjørlingbf643182016-02-04 15:13:27 +01001065 id->cap = 0x2;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001066 id->dom = 0x1;
Matias Bjørling5b40db92015-11-19 12:50:09 +01001067
1068 id->ppaf.blk_offset = 0;
1069 id->ppaf.blk_len = 16;
1070 id->ppaf.pg_offset = 16;
1071 id->ppaf.pg_len = 16;
1072 id->ppaf.sect_offset = 32;
1073 id->ppaf.sect_len = 8;
1074 id->ppaf.pln_offset = 40;
1075 id->ppaf.pln_len = 8;
1076 id->ppaf.lun_offset = 48;
1077 id->ppaf.lun_len = 8;
1078 id->ppaf.ch_offset = 56;
1079 id->ppaf.ch_len = 8;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001080
Shaohua Li2984c862017-08-14 15:04:52 -07001081 sector_div(size, nullb->dev->blocksize); /* convert size to pages */
Arnd Bergmanne93d12a2016-01-13 23:04:08 +01001082 size >>= 8; /* concert size to pgs pr blk */
Matias Bjørling19bd6fe2017-01-31 13:17:15 +01001083 grp = &id->grp;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001084 grp->mtype = 0;
Matias Bjørling5b40db92015-11-19 12:50:09 +01001085 grp->fmtype = 0;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001086 grp->num_ch = 1;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001087 grp->num_pg = 256;
Matias Bjørling5b40db92015-11-19 12:50:09 +01001088 blksize = size;
Arnd Bergmanne93d12a2016-01-13 23:04:08 +01001089 size >>= 16;
Matias Bjørling5b40db92015-11-19 12:50:09 +01001090 grp->num_lun = size + 1;
Arnd Bergmanne93d12a2016-01-13 23:04:08 +01001091 sector_div(blksize, grp->num_lun);
Matias Bjørling5b40db92015-11-19 12:50:09 +01001092 grp->num_blk = blksize;
1093 grp->num_pln = 1;
1094
Shaohua Li2984c862017-08-14 15:04:52 -07001095 grp->fpg_sz = nullb->dev->blocksize;
1096 grp->csecs = nullb->dev->blocksize;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001097 grp->trdt = 25000;
1098 grp->trdm = 25000;
1099 grp->tprt = 500000;
1100 grp->tprm = 500000;
1101 grp->tbet = 1500000;
1102 grp->tbem = 1500000;
1103 grp->mpos = 0x010101; /* single plane rwe */
Shaohua Li2984c862017-08-14 15:04:52 -07001104 grp->cpar = nullb->dev->hw_queue_depth;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001105
1106 return 0;
1107}
1108
Matias Bjørling16f26c32015-12-06 11:25:48 +01001109static void *null_lnvm_create_dma_pool(struct nvm_dev *dev, char *name)
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001110{
1111 mempool_t *virtmem_pool;
1112
Matias Bjørling6bb95352015-11-19 12:50:08 +01001113 virtmem_pool = mempool_create_slab_pool(64, ppa_cache);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001114 if (!virtmem_pool) {
1115 pr_err("null_blk: Unable to create virtual memory pool\n");
1116 return NULL;
1117 }
1118
1119 return virtmem_pool;
1120}
1121
1122static void null_lnvm_destroy_dma_pool(void *pool)
1123{
1124 mempool_destroy(pool);
1125}
1126
Matias Bjørling16f26c32015-12-06 11:25:48 +01001127static void *null_lnvm_dev_dma_alloc(struct nvm_dev *dev, void *pool,
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001128 gfp_t mem_flags, dma_addr_t *dma_handler)
1129{
1130 return mempool_alloc(pool, mem_flags);
1131}
1132
1133static void null_lnvm_dev_dma_free(void *pool, void *entry,
1134 dma_addr_t dma_handler)
1135{
1136 mempool_free(entry, pool);
1137}
1138
1139static struct nvm_dev_ops null_lnvm_dev_ops = {
1140 .identity = null_lnvm_id,
1141 .submit_io = null_lnvm_submit_io,
1142
1143 .create_dma_pool = null_lnvm_create_dma_pool,
1144 .destroy_dma_pool = null_lnvm_destroy_dma_pool,
1145 .dev_dma_alloc = null_lnvm_dev_dma_alloc,
1146 .dev_dma_free = null_lnvm_dev_dma_free,
1147
1148 /* Simulate nvme protocol restriction */
1149 .max_phys_sect = 64,
1150};
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001151
1152static int null_nvm_register(struct nullb *nullb)
1153{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001154 struct nvm_dev *dev;
1155 int rv;
1156
1157 dev = nvm_alloc_dev(0);
1158 if (!dev)
1159 return -ENOMEM;
1160
1161 dev->q = nullb->q;
1162 memcpy(dev->name, nullb->disk_name, DISK_NAME_LEN);
1163 dev->ops = &null_lnvm_dev_ops;
1164
1165 rv = nvm_register(dev);
1166 if (rv) {
1167 kfree(dev);
1168 return rv;
1169 }
1170 nullb->ndev = dev;
1171 return 0;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001172}
1173
1174static void null_nvm_unregister(struct nullb *nullb)
1175{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001176 nvm_unregister(nullb->ndev);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001177}
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001178#else
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001179static int null_nvm_register(struct nullb *nullb)
1180{
Yasuaki Ishimatsu92153d32016-11-16 08:26:11 -07001181 pr_err("null_blk: CONFIG_NVM needs to be enabled for LightNVM\n");
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001182 return -EINVAL;
1183}
1184static void null_nvm_unregister(struct nullb *nullb) {}
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001185#endif /* CONFIG_NVM */
1186
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001187static void null_del_dev(struct nullb *nullb)
1188{
Shaohua Li2984c862017-08-14 15:04:52 -07001189 struct nullb_device *dev = nullb->dev;
1190
Shaohua Li94bc02e2017-08-14 15:04:55 -07001191 ida_simple_remove(&nullb_indexes, nullb->index);
1192
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001193 list_del_init(&nullb->list);
1194
Shaohua Li2984c862017-08-14 15:04:52 -07001195 if (dev->use_lightnvm)
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001196 null_nvm_unregister(nullb);
1197 else
1198 del_gendisk(nullb->disk);
1199 blk_cleanup_queue(nullb->q);
Shaohua Li2984c862017-08-14 15:04:52 -07001200 if (dev->queue_mode == NULL_Q_MQ &&
1201 nullb->tag_set == &nullb->__tag_set)
Jens Axboe82f402f2017-06-20 14:22:01 -06001202 blk_mq_free_tag_set(nullb->tag_set);
Shaohua Li2984c862017-08-14 15:04:52 -07001203 if (!dev->use_lightnvm)
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001204 put_disk(nullb->disk);
1205 cleanup_queues(nullb);
1206 kfree(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001207 dev->nullb = NULL;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001208}
1209
Jens Axboef2298c02013-10-25 11:52:25 +01001210static int null_open(struct block_device *bdev, fmode_t mode)
1211{
1212 return 0;
1213}
1214
1215static void null_release(struct gendisk *disk, fmode_t mode)
1216{
1217}
1218
1219static const struct block_device_operations null_fops = {
1220 .owner = THIS_MODULE,
1221 .open = null_open,
1222 .release = null_release,
1223};
1224
Jens Axboe82f402f2017-06-20 14:22:01 -06001225static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
1226{
1227 BUG_ON(!nullb);
1228 BUG_ON(!nq);
1229
1230 init_waitqueue_head(&nq->wait);
1231 nq->queue_depth = nullb->queue_depth;
Shaohua Li2984c862017-08-14 15:04:52 -07001232 nq->dev = nullb->dev;
Jens Axboe82f402f2017-06-20 14:22:01 -06001233}
1234
1235static void null_init_queues(struct nullb *nullb)
1236{
1237 struct request_queue *q = nullb->q;
1238 struct blk_mq_hw_ctx *hctx;
1239 struct nullb_queue *nq;
1240 int i;
1241
1242 queue_for_each_hw_ctx(q, hctx, i) {
1243 if (!hctx->nr_ctx || !hctx->tags)
1244 continue;
1245 nq = &nullb->queues[i];
1246 hctx->driver_data = nq;
1247 null_init_queue(nullb, nq);
1248 nullb->nr_queues++;
1249 }
1250}
1251
Jens Axboef2298c02013-10-25 11:52:25 +01001252static int setup_commands(struct nullb_queue *nq)
1253{
1254 struct nullb_cmd *cmd;
1255 int i, tag_size;
1256
1257 nq->cmds = kzalloc(nq->queue_depth * sizeof(*cmd), GFP_KERNEL);
1258 if (!nq->cmds)
Matias Bjorling2d263a782013-12-18 13:41:43 +01001259 return -ENOMEM;
Jens Axboef2298c02013-10-25 11:52:25 +01001260
1261 tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
1262 nq->tag_map = kzalloc(tag_size * sizeof(unsigned long), GFP_KERNEL);
1263 if (!nq->tag_map) {
1264 kfree(nq->cmds);
Matias Bjorling2d263a782013-12-18 13:41:43 +01001265 return -ENOMEM;
Jens Axboef2298c02013-10-25 11:52:25 +01001266 }
1267
1268 for (i = 0; i < nq->queue_depth; i++) {
1269 cmd = &nq->cmds[i];
1270 INIT_LIST_HEAD(&cmd->list);
1271 cmd->ll_list.next = NULL;
1272 cmd->tag = -1U;
1273 }
1274
1275 return 0;
1276}
1277
Jens Axboef2298c02013-10-25 11:52:25 +01001278static int setup_queues(struct nullb *nullb)
1279{
Shaohua Li2984c862017-08-14 15:04:52 -07001280 nullb->queues = kzalloc(nullb->dev->submit_queues *
1281 sizeof(struct nullb_queue), GFP_KERNEL);
Jens Axboef2298c02013-10-25 11:52:25 +01001282 if (!nullb->queues)
Matias Bjorling2d263a782013-12-18 13:41:43 +01001283 return -ENOMEM;
Jens Axboef2298c02013-10-25 11:52:25 +01001284
1285 nullb->nr_queues = 0;
Shaohua Li2984c862017-08-14 15:04:52 -07001286 nullb->queue_depth = nullb->dev->hw_queue_depth;
Jens Axboef2298c02013-10-25 11:52:25 +01001287
Matias Bjorling2d263a782013-12-18 13:41:43 +01001288 return 0;
1289}
1290
1291static int init_driver_queues(struct nullb *nullb)
1292{
1293 struct nullb_queue *nq;
1294 int i, ret = 0;
Jens Axboef2298c02013-10-25 11:52:25 +01001295
Shaohua Li2984c862017-08-14 15:04:52 -07001296 for (i = 0; i < nullb->dev->submit_queues; i++) {
Jens Axboef2298c02013-10-25 11:52:25 +01001297 nq = &nullb->queues[i];
Matias Bjorling2d263a782013-12-18 13:41:43 +01001298
1299 null_init_queue(nullb, nq);
1300
1301 ret = setup_commands(nq);
1302 if (ret)
Jan Kara31f96902014-10-22 15:34:21 +02001303 return ret;
Jens Axboef2298c02013-10-25 11:52:25 +01001304 nullb->nr_queues++;
1305 }
Matias Bjorling2d263a782013-12-18 13:41:43 +01001306 return 0;
Jens Axboef2298c02013-10-25 11:52:25 +01001307}
1308
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001309static int null_gendisk_register(struct nullb *nullb)
Jens Axboef2298c02013-10-25 11:52:25 +01001310{
1311 struct gendisk *disk;
Jens Axboef2298c02013-10-25 11:52:25 +01001312 sector_t size;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001313
Shaohua Li2984c862017-08-14 15:04:52 -07001314 disk = nullb->disk = alloc_disk_node(1, nullb->dev->home_node);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001315 if (!disk)
1316 return -ENOMEM;
Shaohua Li2984c862017-08-14 15:04:52 -07001317 size = (sector_t)nullb->dev->size * 1024 * 1024ULL;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001318 set_capacity(disk, size >> 9);
1319
1320 disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO;
1321 disk->major = null_major;
1322 disk->first_minor = nullb->index;
1323 disk->fops = &null_fops;
1324 disk->private_data = nullb;
1325 disk->queue = nullb->q;
1326 strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN);
1327
1328 add_disk(disk);
1329 return 0;
1330}
1331
Shaohua Li2984c862017-08-14 15:04:52 -07001332static int null_init_tag_set(struct nullb *nullb, struct blk_mq_tag_set *set)
Jens Axboe82f402f2017-06-20 14:22:01 -06001333{
1334 set->ops = &null_mq_ops;
Shaohua Li2984c862017-08-14 15:04:52 -07001335 set->nr_hw_queues = nullb ? nullb->dev->submit_queues :
1336 g_submit_queues;
1337 set->queue_depth = nullb ? nullb->dev->hw_queue_depth :
1338 g_hw_queue_depth;
1339 set->numa_node = nullb ? nullb->dev->home_node : g_home_node;
Jens Axboe82f402f2017-06-20 14:22:01 -06001340 set->cmd_size = sizeof(struct nullb_cmd);
1341 set->flags = BLK_MQ_F_SHOULD_MERGE;
1342 set->driver_data = NULL;
1343
Shaohua Li2984c862017-08-14 15:04:52 -07001344 if (nullb->dev->blocking)
Jens Axboe82f402f2017-06-20 14:22:01 -06001345 set->flags |= BLK_MQ_F_BLOCKING;
1346
1347 return blk_mq_alloc_tag_set(set);
1348}
1349
Shaohua Licedcafa2017-08-14 15:04:54 -07001350static void null_validate_conf(struct nullb_device *dev)
1351{
1352 dev->blocksize = round_down(dev->blocksize, 512);
1353 dev->blocksize = clamp_t(unsigned int, dev->blocksize, 512, 4096);
1354 if (dev->use_lightnvm && dev->blocksize != 4096)
1355 dev->blocksize = 4096;
1356
1357 if (dev->use_lightnvm && dev->queue_mode != NULL_Q_MQ)
1358 dev->queue_mode = NULL_Q_MQ;
1359
1360 if (dev->queue_mode == NULL_Q_MQ && dev->use_per_node_hctx) {
1361 if (dev->submit_queues != nr_online_nodes)
1362 dev->submit_queues = nr_online_nodes;
1363 } else if (dev->submit_queues > nr_cpu_ids)
1364 dev->submit_queues = nr_cpu_ids;
1365 else if (dev->submit_queues == 0)
1366 dev->submit_queues = 1;
1367
1368 dev->queue_mode = min_t(unsigned int, dev->queue_mode, NULL_Q_MQ);
1369 dev->irqmode = min_t(unsigned int, dev->irqmode, NULL_IRQ_TIMER);
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001370
1371 /* Do memory allocation, so set blocking */
1372 if (dev->memory_backed)
1373 dev->blocking = true;
Shaohua Licedcafa2017-08-14 15:04:54 -07001374}
1375
Shaohua Li2984c862017-08-14 15:04:52 -07001376static int null_add_dev(struct nullb_device *dev)
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001377{
1378 struct nullb *nullb;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001379 int rv;
Jens Axboef2298c02013-10-25 11:52:25 +01001380
Shaohua Licedcafa2017-08-14 15:04:54 -07001381 null_validate_conf(dev);
1382
Shaohua Li2984c862017-08-14 15:04:52 -07001383 nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, dev->home_node);
Robert Elliottdc501dc2014-09-02 11:38:49 -05001384 if (!nullb) {
1385 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001386 goto out;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001387 }
Shaohua Li2984c862017-08-14 15:04:52 -07001388 nullb->dev = dev;
1389 dev->nullb = nullb;
Jens Axboef2298c02013-10-25 11:52:25 +01001390
1391 spin_lock_init(&nullb->lock);
1392
Robert Elliottdc501dc2014-09-02 11:38:49 -05001393 rv = setup_queues(nullb);
1394 if (rv)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001395 goto out_free_nullb;
Jens Axboef2298c02013-10-25 11:52:25 +01001396
Shaohua Li2984c862017-08-14 15:04:52 -07001397 if (dev->queue_mode == NULL_Q_MQ) {
Jens Axboe82f402f2017-06-20 14:22:01 -06001398 if (shared_tags) {
1399 nullb->tag_set = &tag_set;
1400 rv = 0;
1401 } else {
1402 nullb->tag_set = &nullb->__tag_set;
Shaohua Li2984c862017-08-14 15:04:52 -07001403 rv = null_init_tag_set(nullb, nullb->tag_set);
Jens Axboe82f402f2017-06-20 14:22:01 -06001404 }
Jens Axboef2298c02013-10-25 11:52:25 +01001405
Robert Elliottdc501dc2014-09-02 11:38:49 -05001406 if (rv)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001407 goto out_cleanup_queues;
Jens Axboef2298c02013-10-25 11:52:25 +01001408
Jens Axboe82f402f2017-06-20 14:22:01 -06001409 nullb->q = blk_mq_init_queue(nullb->tag_set);
Ming Lei35b489d2015-01-02 14:25:27 +00001410 if (IS_ERR(nullb->q)) {
Robert Elliottdc501dc2014-09-02 11:38:49 -05001411 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001412 goto out_cleanup_tags;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001413 }
Jens Axboe82f402f2017-06-20 14:22:01 -06001414 null_init_queues(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001415 } else if (dev->queue_mode == NULL_Q_BIO) {
1416 nullb->q = blk_alloc_queue_node(GFP_KERNEL, dev->home_node);
Robert Elliottdc501dc2014-09-02 11:38:49 -05001417 if (!nullb->q) {
1418 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001419 goto out_cleanup_queues;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001420 }
Jens Axboef2298c02013-10-25 11:52:25 +01001421 blk_queue_make_request(nullb->q, null_queue_bio);
Jan Kara31f96902014-10-22 15:34:21 +02001422 rv = init_driver_queues(nullb);
1423 if (rv)
1424 goto out_cleanup_blk_queue;
Jens Axboef2298c02013-10-25 11:52:25 +01001425 } else {
Shaohua Li2984c862017-08-14 15:04:52 -07001426 nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock,
1427 dev->home_node);
Robert Elliottdc501dc2014-09-02 11:38:49 -05001428 if (!nullb->q) {
1429 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001430 goto out_cleanup_queues;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001431 }
Jens Axboef2298c02013-10-25 11:52:25 +01001432 blk_queue_prep_rq(nullb->q, null_rq_prep_fn);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001433 blk_queue_softirq_done(nullb->q, null_softirq_done_fn);
Jan Kara31f96902014-10-22 15:34:21 +02001434 rv = init_driver_queues(nullb);
1435 if (rv)
1436 goto out_cleanup_blk_queue;
Jens Axboef2298c02013-10-25 11:52:25 +01001437 }
1438
Jens Axboef2298c02013-10-25 11:52:25 +01001439 nullb->q->queuedata = nullb;
1440 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q);
Mike Snitzerb277da02014-10-04 10:55:32 -06001441 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, nullb->q);
Jens Axboef2298c02013-10-25 11:52:25 +01001442
Jens Axboef2298c02013-10-25 11:52:25 +01001443 mutex_lock(&lock);
Shaohua Li94bc02e2017-08-14 15:04:55 -07001444 nullb->index = ida_simple_get(&nullb_indexes, 0, 0, GFP_KERNEL);
Shaohua Licedcafa2017-08-14 15:04:54 -07001445 dev->index = nullb->index;
Jens Axboef2298c02013-10-25 11:52:25 +01001446 mutex_unlock(&lock);
1447
Shaohua Li2984c862017-08-14 15:04:52 -07001448 blk_queue_logical_block_size(nullb->q, dev->blocksize);
1449 blk_queue_physical_block_size(nullb->q, dev->blocksize);
Jens Axboef2298c02013-10-25 11:52:25 +01001450
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001451 sprintf(nullb->disk_name, "nullb%d", nullb->index);
1452
Shaohua Li2984c862017-08-14 15:04:52 -07001453 if (dev->use_lightnvm)
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001454 rv = null_nvm_register(nullb);
1455 else
1456 rv = null_gendisk_register(nullb);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001457
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001458 if (rv)
1459 goto out_cleanup_blk_queue;
Jens Axboef2298c02013-10-25 11:52:25 +01001460
Matias Bjørlinga5143792016-02-11 14:49:13 +01001461 mutex_lock(&lock);
1462 list_add_tail(&nullb->list, &nullb_list);
1463 mutex_unlock(&lock);
Wenwei Tao3681c852016-03-05 00:27:04 +08001464
Jens Axboef2298c02013-10-25 11:52:25 +01001465 return 0;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001466out_cleanup_blk_queue:
1467 blk_cleanup_queue(nullb->q);
1468out_cleanup_tags:
Shaohua Li2984c862017-08-14 15:04:52 -07001469 if (dev->queue_mode == NULL_Q_MQ && nullb->tag_set == &nullb->__tag_set)
Jens Axboe82f402f2017-06-20 14:22:01 -06001470 blk_mq_free_tag_set(nullb->tag_set);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001471out_cleanup_queues:
1472 cleanup_queues(nullb);
1473out_free_nullb:
1474 kfree(nullb);
1475out:
Shaohua Li2984c862017-08-14 15:04:52 -07001476 null_free_dev(dev);
Robert Elliottdc501dc2014-09-02 11:38:49 -05001477 return rv;
Jens Axboef2298c02013-10-25 11:52:25 +01001478}
1479
1480static int __init null_init(void)
1481{
Minfei Huangaf096e22015-12-08 13:47:34 -07001482 int ret = 0;
Jens Axboef2298c02013-10-25 11:52:25 +01001483 unsigned int i;
Minfei Huangaf096e22015-12-08 13:47:34 -07001484 struct nullb *nullb;
Shaohua Li2984c862017-08-14 15:04:52 -07001485 struct nullb_device *dev;
Jens Axboef2298c02013-10-25 11:52:25 +01001486
Shaohua Li2984c862017-08-14 15:04:52 -07001487 if (g_bs > PAGE_SIZE) {
Raghavendra K T9967d8a2014-01-21 16:59:59 +05301488 pr_warn("null_blk: invalid block size\n");
1489 pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);
Shaohua Li2984c862017-08-14 15:04:52 -07001490 g_bs = PAGE_SIZE;
Raghavendra K T9967d8a2014-01-21 16:59:59 +05301491 }
Jens Axboef2298c02013-10-25 11:52:25 +01001492
Shaohua Li2984c862017-08-14 15:04:52 -07001493 if (g_use_lightnvm && g_bs != 4096) {
Matias Bjørling6bb95352015-11-19 12:50:08 +01001494 pr_warn("null_blk: LightNVM only supports 4k block size\n");
1495 pr_warn("null_blk: defaults block size to 4k\n");
Shaohua Li2984c862017-08-14 15:04:52 -07001496 g_bs = 4096;
Matias Bjørling6bb95352015-11-19 12:50:08 +01001497 }
1498
Shaohua Li2984c862017-08-14 15:04:52 -07001499 if (g_use_lightnvm && g_queue_mode != NULL_Q_MQ) {
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001500 pr_warn("null_blk: LightNVM only supported for blk-mq\n");
1501 pr_warn("null_blk: defaults queue mode to blk-mq\n");
Shaohua Li2984c862017-08-14 15:04:52 -07001502 g_queue_mode = NULL_Q_MQ;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001503 }
1504
Shaohua Li2984c862017-08-14 15:04:52 -07001505 if (g_queue_mode == NULL_Q_MQ && g_use_per_node_hctx) {
1506 if (g_submit_queues != nr_online_nodes) {
weiping zhang558ab3002017-08-03 00:26:39 +08001507 pr_warn("null_blk: submit_queues param is set to %u.\n",
Matias Bjorlingd15ee6b2013-12-18 13:41:44 +01001508 nr_online_nodes);
Shaohua Li2984c862017-08-14 15:04:52 -07001509 g_submit_queues = nr_online_nodes;
Matias Bjørlingfc1bc352013-12-21 00:11:01 +01001510 }
Shaohua Li2984c862017-08-14 15:04:52 -07001511 } else if (g_submit_queues > nr_cpu_ids)
1512 g_submit_queues = nr_cpu_ids;
1513 else if (g_submit_queues <= 0)
1514 g_submit_queues = 1;
Jens Axboef2298c02013-10-25 11:52:25 +01001515
Shaohua Li2984c862017-08-14 15:04:52 -07001516 if (g_queue_mode == NULL_Q_MQ && shared_tags) {
1517 ret = null_init_tag_set(NULL, &tag_set);
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001518 if (ret)
1519 return ret;
1520 }
1521
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001522 config_group_init(&nullb_subsys.su_group);
1523 mutex_init(&nullb_subsys.su_mutex);
1524
1525 ret = configfs_register_subsystem(&nullb_subsys);
1526 if (ret)
1527 goto err_tagset;
1528
Jens Axboef2298c02013-10-25 11:52:25 +01001529 mutex_init(&lock);
1530
Jens Axboef2298c02013-10-25 11:52:25 +01001531 null_major = register_blkdev(0, "nullb");
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001532 if (null_major < 0) {
1533 ret = null_major;
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001534 goto err_conf;
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001535 }
Jens Axboef2298c02013-10-25 11:52:25 +01001536
Shaohua Li2984c862017-08-14 15:04:52 -07001537 if (g_use_lightnvm) {
Matias Bjørling6bb95352015-11-19 12:50:08 +01001538 ppa_cache = kmem_cache_create("ppa_cache", 64 * sizeof(u64),
1539 0, 0, NULL);
1540 if (!ppa_cache) {
1541 pr_err("null_blk: unable to create ppa cache\n");
Minfei Huangaf096e22015-12-08 13:47:34 -07001542 ret = -ENOMEM;
Matias Bjørling6bb95352015-11-19 12:50:08 +01001543 goto err_ppa;
Jens Axboef2298c02013-10-25 11:52:25 +01001544 }
1545 }
1546
Minfei Huangaf096e22015-12-08 13:47:34 -07001547 for (i = 0; i < nr_devices; i++) {
Shaohua Li2984c862017-08-14 15:04:52 -07001548 dev = null_alloc_dev();
1549 if (!dev)
Minfei Huangaf096e22015-12-08 13:47:34 -07001550 goto err_dev;
Shaohua Li2984c862017-08-14 15:04:52 -07001551 ret = null_add_dev(dev);
1552 if (ret) {
1553 null_free_dev(dev);
1554 goto err_dev;
1555 }
Minfei Huangaf096e22015-12-08 13:47:34 -07001556 }
1557
Jens Axboef2298c02013-10-25 11:52:25 +01001558 pr_info("null: module loaded\n");
1559 return 0;
Minfei Huangaf096e22015-12-08 13:47:34 -07001560
1561err_dev:
1562 while (!list_empty(&nullb_list)) {
1563 nullb = list_entry(nullb_list.next, struct nullb, list);
Shaohua Li2984c862017-08-14 15:04:52 -07001564 dev = nullb->dev;
Minfei Huangaf096e22015-12-08 13:47:34 -07001565 null_del_dev(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001566 null_free_dev(dev);
Minfei Huangaf096e22015-12-08 13:47:34 -07001567 }
Matias Bjørling6bb95352015-11-19 12:50:08 +01001568 kmem_cache_destroy(ppa_cache);
Minfei Huangaf096e22015-12-08 13:47:34 -07001569err_ppa:
1570 unregister_blkdev(null_major, "nullb");
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001571err_conf:
1572 configfs_unregister_subsystem(&nullb_subsys);
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001573err_tagset:
Shaohua Li2984c862017-08-14 15:04:52 -07001574 if (g_queue_mode == NULL_Q_MQ && shared_tags)
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001575 blk_mq_free_tag_set(&tag_set);
Minfei Huangaf096e22015-12-08 13:47:34 -07001576 return ret;
Jens Axboef2298c02013-10-25 11:52:25 +01001577}
1578
1579static void __exit null_exit(void)
1580{
1581 struct nullb *nullb;
1582
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001583 configfs_unregister_subsystem(&nullb_subsys);
1584
Jens Axboef2298c02013-10-25 11:52:25 +01001585 unregister_blkdev(null_major, "nullb");
1586
1587 mutex_lock(&lock);
1588 while (!list_empty(&nullb_list)) {
Shaohua Li2984c862017-08-14 15:04:52 -07001589 struct nullb_device *dev;
1590
Jens Axboef2298c02013-10-25 11:52:25 +01001591 nullb = list_entry(nullb_list.next, struct nullb, list);
Shaohua Li2984c862017-08-14 15:04:52 -07001592 dev = nullb->dev;
Jens Axboef2298c02013-10-25 11:52:25 +01001593 null_del_dev(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001594 null_free_dev(dev);
Jens Axboef2298c02013-10-25 11:52:25 +01001595 }
1596 mutex_unlock(&lock);
Matias Bjørling6bb95352015-11-19 12:50:08 +01001597
Shaohua Li2984c862017-08-14 15:04:52 -07001598 if (g_queue_mode == NULL_Q_MQ && shared_tags)
Jens Axboe82f402f2017-06-20 14:22:01 -06001599 blk_mq_free_tag_set(&tag_set);
1600
Matias Bjørling6bb95352015-11-19 12:50:08 +01001601 kmem_cache_destroy(ppa_cache);
Jens Axboef2298c02013-10-25 11:52:25 +01001602}
1603
1604module_init(null_init);
1605module_exit(null_exit);
1606
1607MODULE_AUTHOR("Jens Axboe <jaxboe@fusionio.com>");
1608MODULE_LICENSE("GPL");