Commit | Line | Data |
---|---|---|
3bf2bd20 SL |
1 | /* |
2 | * Add configfs and memory store: Kyungchan Koh <kkc6196@fb.com> and | |
3 | * Shaohua Li <shli@fb.com> | |
4 | */ | |
f2298c04 | 5 | #include <linux/module.h> |
fc1bc354 | 6 | |
f2298c04 JA |
7 | #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> | |
b2b7e001 | 15 | #include <linux/lightnvm.h> |
3bf2bd20 | 16 | #include <linux/configfs.h> |
f2298c04 | 17 | |
5bcd0e0c SL |
18 | #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 | ||
f2298c04 JA |
26 | struct 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; | |
3c395a96 | 34 | struct hrtimer timer; |
5bcd0e0c | 35 | blk_status_t error; |
f2298c04 JA |
36 | }; |
37 | ||
38 | struct nullb_queue { | |
39 | unsigned long *tag_map; | |
40 | wait_queue_head_t wait; | |
41 | unsigned int queue_depth; | |
2984c868 | 42 | struct nullb_device *dev; |
f2298c04 JA |
43 | |
44 | struct nullb_cmd *cmds; | |
45 | }; | |
46 | ||
3bf2bd20 SL |
47 | /* |
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 | */ | |
53 | enum nullb_device_flags { | |
54 | NULLB_DEV_FL_CONFIGURED = 0, | |
55 | NULLB_DEV_FL_UP = 1, | |
56 | }; | |
57 | ||
5bcd0e0c SL |
58 | /* |
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 | */ | |
66 | struct nullb_page { | |
67 | struct page *page; | |
68 | unsigned long bitmap; | |
69 | }; | |
70 | ||
2984c868 SL |
71 | struct nullb_device { |
72 | struct nullb *nullb; | |
3bf2bd20 | 73 | struct config_item item; |
5bcd0e0c | 74 | struct radix_tree_root data; /* data stored in the disk */ |
3bf2bd20 | 75 | unsigned long flags; /* device flags */ |
2984c868 SL |
76 | |
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 */ | |
cedcafad | 85 | unsigned int index; /* index of the disk, only valid with a disk */ |
2984c868 SL |
86 | 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 */ | |
cedcafad | 89 | bool power; /* power on/off the device */ |
5bcd0e0c | 90 | bool memory_backed; /* if data is stored in memory */ |
2984c868 SL |
91 | }; |
92 | ||
f2298c04 | 93 | struct nullb { |
2984c868 | 94 | struct nullb_device *dev; |
f2298c04 JA |
95 | struct list_head list; |
96 | unsigned int index; | |
97 | struct request_queue *q; | |
98 | struct gendisk *disk; | |
b0b4e09c | 99 | struct nvm_dev *ndev; |
82f402fe JA |
100 | struct blk_mq_tag_set *tag_set; |
101 | struct blk_mq_tag_set __tag_set; | |
f2298c04 JA |
102 | struct hrtimer timer; |
103 | unsigned int queue_depth; | |
104 | spinlock_t lock; | |
105 | ||
106 | struct nullb_queue *queues; | |
107 | unsigned int nr_queues; | |
b2b7e001 | 108 | char disk_name[DISK_NAME_LEN]; |
f2298c04 JA |
109 | }; |
110 | ||
111 | static LIST_HEAD(nullb_list); | |
112 | static struct mutex lock; | |
113 | static int null_major; | |
94bc02e3 | 114 | static DEFINE_IDA(nullb_indexes); |
6bb9535b | 115 | static struct kmem_cache *ppa_cache; |
82f402fe | 116 | static struct blk_mq_tag_set tag_set; |
f2298c04 | 117 | |
f2298c04 JA |
118 | enum { |
119 | NULL_IRQ_NONE = 0, | |
120 | NULL_IRQ_SOFTIRQ = 1, | |
121 | NULL_IRQ_TIMER = 2, | |
ce2c350b | 122 | }; |
f2298c04 | 123 | |
ce2c350b | 124 | enum { |
f2298c04 JA |
125 | NULL_Q_BIO = 0, |
126 | NULL_Q_RQ = 1, | |
127 | NULL_Q_MQ = 2, | |
128 | }; | |
129 | ||
2984c868 SL |
130 | static int g_submit_queues = 1; |
131 | module_param_named(submit_queues, g_submit_queues, int, S_IRUGO); | |
f2298c04 JA |
132 | MODULE_PARM_DESC(submit_queues, "Number of submission queues"); |
133 | ||
2984c868 SL |
134 | static int g_home_node = NUMA_NO_NODE; |
135 | module_param_named(home_node, g_home_node, int, S_IRUGO); | |
f2298c04 JA |
136 | MODULE_PARM_DESC(home_node, "Home node for the device"); |
137 | ||
2984c868 | 138 | static int g_queue_mode = NULL_Q_MQ; |
709c8667 MB |
139 | |
140 | static 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 | ||
155 | static int null_set_queue_mode(const char *str, const struct kernel_param *kp) | |
156 | { | |
2984c868 | 157 | return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ); |
709c8667 MB |
158 | } |
159 | ||
9c27847d | 160 | static const struct kernel_param_ops null_queue_mode_param_ops = { |
709c8667 MB |
161 | .set = null_set_queue_mode, |
162 | .get = param_get_int, | |
163 | }; | |
164 | ||
2984c868 | 165 | device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, S_IRUGO); |
54ae81cd | 166 | MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)"); |
f2298c04 | 167 | |
2984c868 SL |
168 | static int g_gb = 250; |
169 | module_param_named(gb, g_gb, int, S_IRUGO); | |
f2298c04 JA |
170 | MODULE_PARM_DESC(gb, "Size in GB"); |
171 | ||
2984c868 SL |
172 | static int g_bs = 512; |
173 | module_param_named(bs, g_bs, int, S_IRUGO); | |
f2298c04 JA |
174 | MODULE_PARM_DESC(bs, "Block size (in bytes)"); |
175 | ||
82f402fe | 176 | static int nr_devices = 1; |
f2298c04 JA |
177 | module_param(nr_devices, int, S_IRUGO); |
178 | MODULE_PARM_DESC(nr_devices, "Number of devices to register"); | |
179 | ||
2984c868 SL |
180 | static bool g_use_lightnvm; |
181 | module_param_named(use_lightnvm, g_use_lightnvm, bool, S_IRUGO); | |
b2b7e001 MB |
182 | MODULE_PARM_DESC(use_lightnvm, "Register as a LightNVM device"); |
183 | ||
2984c868 SL |
184 | static bool g_blocking; |
185 | module_param_named(blocking, g_blocking, bool, S_IRUGO); | |
db5bcf87 JA |
186 | MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device"); |
187 | ||
82f402fe JA |
188 | static bool shared_tags; |
189 | module_param(shared_tags, bool, S_IRUGO); | |
190 | MODULE_PARM_DESC(shared_tags, "Share tag set between devices for blk-mq"); | |
191 | ||
2984c868 | 192 | static int g_irqmode = NULL_IRQ_SOFTIRQ; |
709c8667 MB |
193 | |
194 | static int null_set_irqmode(const char *str, const struct kernel_param *kp) | |
195 | { | |
2984c868 | 196 | return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE, |
709c8667 MB |
197 | NULL_IRQ_TIMER); |
198 | } | |
199 | ||
9c27847d | 200 | static const struct kernel_param_ops null_irqmode_param_ops = { |
709c8667 MB |
201 | .set = null_set_irqmode, |
202 | .get = param_get_int, | |
203 | }; | |
204 | ||
2984c868 | 205 | device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, S_IRUGO); |
f2298c04 JA |
206 | MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer"); |
207 | ||
2984c868 SL |
208 | static unsigned long g_completion_nsec = 10000; |
209 | module_param_named(completion_nsec, g_completion_nsec, ulong, S_IRUGO); | |
f2298c04 JA |
210 | MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns"); |
211 | ||
2984c868 SL |
212 | static int g_hw_queue_depth = 64; |
213 | module_param_named(hw_queue_depth, g_hw_queue_depth, int, S_IRUGO); | |
f2298c04 JA |
214 | MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64"); |
215 | ||
2984c868 SL |
216 | static bool g_use_per_node_hctx; |
217 | module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, S_IRUGO); | |
20005244 | 218 | MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false"); |
f2298c04 | 219 | |
3bf2bd20 SL |
220 | static struct nullb_device *null_alloc_dev(void); |
221 | static void null_free_dev(struct nullb_device *dev); | |
cedcafad SL |
222 | static void null_del_dev(struct nullb *nullb); |
223 | static int null_add_dev(struct nullb_device *dev); | |
5bcd0e0c | 224 | static void null_free_device_storage(struct nullb_device *dev); |
3bf2bd20 SL |
225 | |
226 | static 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 | ||
231 | static 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 | ||
236 | static 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 | ||
242 | static inline ssize_t nullb_device_bool_attr_show(bool val, char *page) | |
243 | { | |
244 | return snprintf(page, PAGE_SIZE, "%u\n", val); | |
245 | } | |
246 | ||
247 | static 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 | ||
261 | static 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 | ||
275 | static 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) \ | |
291 | static ssize_t \ | |
292 | nullb_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 | } \ | |
297 | static ssize_t \ | |
298 | nullb_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 | } \ | |
306 | CONFIGFS_ATTR(nullb_device_, NAME); | |
307 | ||
308 | NULLB_DEVICE_ATTR(size, ulong); | |
309 | NULLB_DEVICE_ATTR(completion_nsec, ulong); | |
310 | NULLB_DEVICE_ATTR(submit_queues, uint); | |
311 | NULLB_DEVICE_ATTR(home_node, uint); | |
312 | NULLB_DEVICE_ATTR(queue_mode, uint); | |
313 | NULLB_DEVICE_ATTR(blocksize, uint); | |
314 | NULLB_DEVICE_ATTR(irqmode, uint); | |
315 | NULLB_DEVICE_ATTR(hw_queue_depth, uint); | |
cedcafad | 316 | NULLB_DEVICE_ATTR(index, uint); |
3bf2bd20 SL |
317 | NULLB_DEVICE_ATTR(use_lightnvm, bool); |
318 | NULLB_DEVICE_ATTR(blocking, bool); | |
319 | NULLB_DEVICE_ATTR(use_per_node_hctx, bool); | |
5bcd0e0c | 320 | NULLB_DEVICE_ATTR(memory_backed, bool); |
3bf2bd20 | 321 | |
cedcafad SL |
322 | static 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 | ||
327 | static 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 | ||
359 | CONFIGFS_ATTR(nullb_device_, power); | |
360 | ||
3bf2bd20 SL |
361 | static 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, | |
cedcafad | 370 | &nullb_device_attr_index, |
3bf2bd20 SL |
371 | &nullb_device_attr_use_lightnvm, |
372 | &nullb_device_attr_blocking, | |
373 | &nullb_device_attr_use_per_node_hctx, | |
cedcafad | 374 | &nullb_device_attr_power, |
5bcd0e0c | 375 | &nullb_device_attr_memory_backed, |
3bf2bd20 SL |
376 | NULL, |
377 | }; | |
378 | ||
379 | static void nullb_device_release(struct config_item *item) | |
380 | { | |
5bcd0e0c SL |
381 | struct nullb_device *dev = to_nullb_device(item); |
382 | ||
383 | null_free_device_storage(dev); | |
384 | null_free_dev(dev); | |
3bf2bd20 SL |
385 | } |
386 | ||
387 | static struct configfs_item_operations nullb_device_ops = { | |
388 | .release = nullb_device_release, | |
389 | }; | |
390 | ||
391 | static 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 | ||
397 | static struct | |
398 | config_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 | ||
411 | static void | |
412 | nullb_group_drop_item(struct config_group *group, struct config_item *item) | |
413 | { | |
cedcafad SL |
414 | 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 | ||
3bf2bd20 SL |
423 | config_item_put(item); |
424 | } | |
425 | ||
426 | static ssize_t memb_group_features_show(struct config_item *item, char *page) | |
427 | { | |
5bcd0e0c | 428 | return snprintf(page, PAGE_SIZE, "memory_backed\n"); |
3bf2bd20 SL |
429 | } |
430 | ||
431 | CONFIGFS_ATTR_RO(memb_group_, features); | |
432 | ||
433 | static struct configfs_attribute *nullb_group_attrs[] = { | |
434 | &memb_group_attr_features, | |
435 | NULL, | |
436 | }; | |
437 | ||
438 | static struct configfs_group_operations nullb_group_ops = { | |
439 | .make_item = nullb_group_make_item, | |
440 | .drop_item = nullb_group_drop_item, | |
441 | }; | |
442 | ||
443 | static 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 | ||
449 | static 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 | ||
2984c868 SL |
458 | static 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; | |
5bcd0e0c | 465 | INIT_RADIX_TREE(&dev->data, GFP_ATOMIC); |
2984c868 SL |
466 | 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 | ||
480 | static void null_free_dev(struct nullb_device *dev) | |
481 | { | |
482 | kfree(dev); | |
483 | } | |
484 | ||
f2298c04 JA |
485 | static 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 | ||
493 | static 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 | ||
506 | static void free_cmd(struct nullb_cmd *cmd) | |
507 | { | |
508 | put_tag(cmd->nq, cmd->tag); | |
509 | } | |
510 | ||
3c395a96 PV |
511 | static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer); |
512 | ||
f2298c04 JA |
513 | static 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; | |
2984c868 | 523 | if (nq->dev->irqmode == NULL_IRQ_TIMER) { |
3c395a96 PV |
524 | hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, |
525 | HRTIMER_MODE_REL); | |
526 | cmd->timer.function = null_cmd_timer_expired; | |
527 | } | |
f2298c04 JA |
528 | return cmd; |
529 | } | |
530 | ||
531 | return NULL; | |
532 | } | |
533 | ||
534 | static 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 | ||
556 | static void end_cmd(struct nullb_cmd *cmd) | |
557 | { | |
cf8ecc5a | 558 | struct request_queue *q = NULL; |
2984c868 | 559 | int queue_mode = cmd->nq->dev->queue_mode; |
cf8ecc5a | 560 | |
e8271201 MK |
561 | if (cmd->rq) |
562 | q = cmd->rq->q; | |
563 | ||
ce2c350b CH |
564 | switch (queue_mode) { |
565 | case NULL_Q_MQ: | |
5bcd0e0c | 566 | blk_mq_end_request(cmd->rq, cmd->error); |
ce2c350b CH |
567 | return; |
568 | case NULL_Q_RQ: | |
569 | INIT_LIST_HEAD(&cmd->rq->queuelist); | |
5bcd0e0c | 570 | blk_end_request_all(cmd->rq, cmd->error); |
ce2c350b CH |
571 | break; |
572 | case NULL_Q_BIO: | |
5bcd0e0c | 573 | cmd->bio->bi_status = cmd->error; |
4246a0b6 | 574 | bio_endio(cmd->bio); |
48cc661e | 575 | break; |
ce2c350b | 576 | } |
f2298c04 | 577 | |
48cc661e JA |
578 | free_cmd(cmd); |
579 | ||
cf8ecc5a | 580 | /* Restart queue if needed, as we are freeing a tag */ |
48cc661e | 581 | if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) { |
cf8ecc5a AA |
582 | unsigned long flags; |
583 | ||
584 | spin_lock_irqsave(q->queue_lock, flags); | |
48cc661e | 585 | blk_start_queue_async(q); |
cf8ecc5a | 586 | spin_unlock_irqrestore(q->queue_lock, flags); |
f2298c04 | 587 | } |
cf8ecc5a AA |
588 | } |
589 | ||
590 | static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer) | |
591 | { | |
592 | end_cmd(container_of(timer, struct nullb_cmd, timer)); | |
f2298c04 JA |
593 | |
594 | return HRTIMER_NORESTART; | |
595 | } | |
596 | ||
597 | static void null_cmd_end_timer(struct nullb_cmd *cmd) | |
598 | { | |
2984c868 | 599 | ktime_t kt = cmd->nq->dev->completion_nsec; |
f2298c04 | 600 | |
3c395a96 | 601 | hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL); |
f2298c04 JA |
602 | } |
603 | ||
604 | static void null_softirq_done_fn(struct request *rq) | |
605 | { | |
2984c868 SL |
606 | struct nullb *nullb = rq->q->queuedata; |
607 | ||
608 | if (nullb->dev->queue_mode == NULL_Q_MQ) | |
d891fa70 JA |
609 | end_cmd(blk_mq_rq_to_pdu(rq)); |
610 | else | |
611 | end_cmd(rq->special); | |
f2298c04 JA |
612 | } |
613 | ||
5bcd0e0c SL |
614 | static struct nullb_page *null_alloc_page(gfp_t gfp_flags) |
615 | { | |
616 | 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; | |
628 | out_freepage: | |
629 | kfree(t_page); | |
630 | out: | |
631 | return NULL; | |
632 | } | |
633 | ||
634 | static void null_free_page(struct nullb_page *t_page) | |
635 | { | |
636 | __free_page(t_page->page); | |
637 | kfree(t_page); | |
638 | } | |
639 | ||
640 | static 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 | ||
663 | static 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 | ||
679 | static 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 | ||
705 | static 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 | ||
724 | static 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; | |
750 | out_freepage: | |
751 | null_free_page(t_page); | |
752 | out_lock: | |
753 | spin_lock_irq(&nullb->lock); | |
754 | return null_lookup_page(nullb, sector, true); | |
755 | } | |
756 | ||
757 | static 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 | ||
787 | static 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); | |
809 | next: | |
810 | kunmap_atomic(dst); | |
811 | ||
812 | count += temp; | |
813 | sector += temp >> SECTOR_SHIFT; | |
814 | } | |
815 | return 0; | |
816 | } | |
817 | ||
818 | static int null_transfer(struct nullb *nullb, struct page *page, | |
819 | unsigned int len, unsigned int off, bool is_write, sector_t sector) | |
f2298c04 | 820 | { |
5bcd0e0c SL |
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 | ||
834 | static 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 | ||
862 | static 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 | ||
889 | static 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); | |
f2298c04 | 901 | /* Complete IO by inline, softirq or timer */ |
5bcd0e0c | 902 | switch (dev->irqmode) { |
f2298c04 | 903 | case NULL_IRQ_SOFTIRQ: |
5bcd0e0c | 904 | switch (dev->queue_mode) { |
ce2c350b | 905 | case NULL_Q_MQ: |
08e0029a | 906 | blk_mq_complete_request(cmd->rq); |
ce2c350b CH |
907 | 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; | |
919 | case NULL_IRQ_NONE: | |
f2298c04 | 920 | end_cmd(cmd); |
f2298c04 JA |
921 | break; |
922 | case NULL_IRQ_TIMER: | |
923 | null_cmd_end_timer(cmd); | |
924 | break; | |
925 | } | |
5bcd0e0c | 926 | return BLK_STS_OK; |
f2298c04 JA |
927 | } |
928 | ||
929 | static 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 | ||
dece1635 | 939 | static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio) |
f2298c04 JA |
940 | { |
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); | |
dece1635 | 949 | return BLK_QC_T_NONE; |
f2298c04 JA |
950 | } |
951 | ||
952 | static 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 | } | |
8b70f45e | 964 | blk_stop_queue(q); |
f2298c04 JA |
965 | |
966 | return BLKPREP_DEFER; | |
967 | } | |
968 | ||
969 | static 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 | ||
fc17b653 | 982 | static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx, |
74c45052 | 983 | const struct blk_mq_queue_data *bd) |
f2298c04 | 984 | { |
74c45052 | 985 | struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq); |
2984c868 | 986 | struct nullb_queue *nq = hctx->driver_data; |
f2298c04 | 987 | |
db5bcf87 JA |
988 | might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING); |
989 | ||
2984c868 | 990 | if (nq->dev->irqmode == NULL_IRQ_TIMER) { |
3c395a96 PV |
991 | hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
992 | cmd->timer.function = null_cmd_timer_expired; | |
993 | } | |
74c45052 | 994 | cmd->rq = bd->rq; |
2984c868 | 995 | cmd->nq = nq; |
f2298c04 | 996 | |
74c45052 | 997 | blk_mq_start_request(bd->rq); |
e2490073 | 998 | |
5bcd0e0c | 999 | return null_handle_cmd(cmd); |
f2298c04 JA |
1000 | } |
1001 | ||
f363b089 | 1002 | static const struct blk_mq_ops null_mq_ops = { |
f2298c04 | 1003 | .queue_rq = null_queue_rq, |
ce2c350b | 1004 | .complete = null_softirq_done_fn, |
f2298c04 JA |
1005 | }; |
1006 | ||
de65d2d2 MB |
1007 | static void cleanup_queue(struct nullb_queue *nq) |
1008 | { | |
1009 | kfree(nq->tag_map); | |
1010 | kfree(nq->cmds); | |
1011 | } | |
1012 | ||
1013 | static 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 | ||
b2b7e001 MB |
1023 | #ifdef CONFIG_NVM |
1024 | ||
2a842aca | 1025 | static void null_lnvm_end_io(struct request *rq, blk_status_t status) |
b2b7e001 MB |
1026 | { |
1027 | struct nvm_rq *rqd = rq->end_io_data; | |
b2b7e001 | 1028 | |
2a842aca CH |
1029 | /* XXX: lighnvm core seems to expect NVM_RSP_* values here.. */ |
1030 | rqd->error = status ? -EIO : 0; | |
06894efe | 1031 | nvm_end_io(rqd); |
b2b7e001 MB |
1032 | |
1033 | blk_put_request(rq); | |
1034 | } | |
1035 | ||
16f26c3a | 1036 | static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd) |
b2b7e001 | 1037 | { |
16f26c3a | 1038 | struct request_queue *q = dev->q; |
b2b7e001 MB |
1039 | struct request *rq; |
1040 | struct bio *bio = rqd->bio; | |
1041 | ||
aebf526b CH |
1042 | rq = blk_mq_alloc_request(q, |
1043 | op_is_write(bio_op(bio)) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0); | |
b2b7e001 MB |
1044 | if (IS_ERR(rq)) |
1045 | return -ENOMEM; | |
1046 | ||
2644a3cc | 1047 | blk_init_request_from_bio(rq, bio); |
b2b7e001 MB |
1048 | |
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 | ||
16f26c3a | 1056 | static int null_lnvm_id(struct nvm_dev *dev, struct nvm_id *id) |
b2b7e001 | 1057 | { |
2984c868 SL |
1058 | struct nullb *nullb = dev->q->queuedata; |
1059 | sector_t size = (sector_t)nullb->dev->size * 1024 * 1024ULL; | |
5b40db99 | 1060 | sector_t blksize; |
b2b7e001 MB |
1061 | struct nvm_id_group *grp; |
1062 | ||
1063 | id->ver_id = 0x1; | |
1064 | id->vmnt = 0; | |
bf643185 | 1065 | id->cap = 0x2; |
b2b7e001 | 1066 | id->dom = 0x1; |
5b40db99 MB |
1067 | |
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; | |
b2b7e001 | 1080 | |
2984c868 | 1081 | sector_div(size, nullb->dev->blocksize); /* convert size to pages */ |
e93d12ae | 1082 | size >>= 8; /* concert size to pgs pr blk */ |
19bd6fe7 | 1083 | grp = &id->grp; |
b2b7e001 | 1084 | grp->mtype = 0; |
5b40db99 | 1085 | grp->fmtype = 0; |
b2b7e001 | 1086 | grp->num_ch = 1; |
b2b7e001 | 1087 | grp->num_pg = 256; |
5b40db99 | 1088 | blksize = size; |
e93d12ae | 1089 | size >>= 16; |
5b40db99 | 1090 | grp->num_lun = size + 1; |
e93d12ae | 1091 | sector_div(blksize, grp->num_lun); |
5b40db99 MB |
1092 | grp->num_blk = blksize; |
1093 | grp->num_pln = 1; | |
1094 | ||
2984c868 SL |
1095 | grp->fpg_sz = nullb->dev->blocksize; |
1096 | grp->csecs = nullb->dev->blocksize; | |
b2b7e001 MB |
1097 | 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 */ | |
2984c868 | 1104 | grp->cpar = nullb->dev->hw_queue_depth; |
b2b7e001 MB |
1105 | |
1106 | return 0; | |
1107 | } | |
1108 | ||
16f26c3a | 1109 | static void *null_lnvm_create_dma_pool(struct nvm_dev *dev, char *name) |
b2b7e001 MB |
1110 | { |
1111 | mempool_t *virtmem_pool; | |
1112 | ||
6bb9535b | 1113 | virtmem_pool = mempool_create_slab_pool(64, ppa_cache); |
b2b7e001 MB |
1114 | 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 | ||
1122 | static void null_lnvm_destroy_dma_pool(void *pool) | |
1123 | { | |
1124 | mempool_destroy(pool); | |
1125 | } | |
1126 | ||
16f26c3a | 1127 | static void *null_lnvm_dev_dma_alloc(struct nvm_dev *dev, void *pool, |
b2b7e001 MB |
1128 | gfp_t mem_flags, dma_addr_t *dma_handler) |
1129 | { | |
1130 | return mempool_alloc(pool, mem_flags); | |
1131 | } | |
1132 | ||
1133 | static void null_lnvm_dev_dma_free(void *pool, void *entry, | |
1134 | dma_addr_t dma_handler) | |
1135 | { | |
1136 | mempool_free(entry, pool); | |
1137 | } | |
1138 | ||
1139 | static 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 | }; | |
9ae2d0aa MB |
1151 | |
1152 | static int null_nvm_register(struct nullb *nullb) | |
1153 | { | |
b0b4e09c MB |
1154 | 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; | |
9ae2d0aa MB |
1172 | } |
1173 | ||
1174 | static void null_nvm_unregister(struct nullb *nullb) | |
1175 | { | |
b0b4e09c | 1176 | nvm_unregister(nullb->ndev); |
9ae2d0aa | 1177 | } |
b2b7e001 | 1178 | #else |
9ae2d0aa MB |
1179 | static int null_nvm_register(struct nullb *nullb) |
1180 | { | |
92153d30 | 1181 | pr_err("null_blk: CONFIG_NVM needs to be enabled for LightNVM\n"); |
9ae2d0aa MB |
1182 | return -EINVAL; |
1183 | } | |
1184 | static void null_nvm_unregister(struct nullb *nullb) {} | |
b2b7e001 MB |
1185 | #endif /* CONFIG_NVM */ |
1186 | ||
9ae2d0aa MB |
1187 | static void null_del_dev(struct nullb *nullb) |
1188 | { | |
2984c868 SL |
1189 | struct nullb_device *dev = nullb->dev; |
1190 | ||
94bc02e3 SL |
1191 | ida_simple_remove(&nullb_indexes, nullb->index); |
1192 | ||
9ae2d0aa MB |
1193 | list_del_init(&nullb->list); |
1194 | ||
2984c868 | 1195 | if (dev->use_lightnvm) |
9ae2d0aa MB |
1196 | null_nvm_unregister(nullb); |
1197 | else | |
1198 | del_gendisk(nullb->disk); | |
1199 | blk_cleanup_queue(nullb->q); | |
2984c868 SL |
1200 | if (dev->queue_mode == NULL_Q_MQ && |
1201 | nullb->tag_set == &nullb->__tag_set) | |
82f402fe | 1202 | blk_mq_free_tag_set(nullb->tag_set); |
2984c868 | 1203 | if (!dev->use_lightnvm) |
9ae2d0aa MB |
1204 | put_disk(nullb->disk); |
1205 | cleanup_queues(nullb); | |
1206 | kfree(nullb); | |
2984c868 | 1207 | dev->nullb = NULL; |
9ae2d0aa MB |
1208 | } |
1209 | ||
f2298c04 JA |
1210 | static int null_open(struct block_device *bdev, fmode_t mode) |
1211 | { | |
1212 | return 0; | |
1213 | } | |
1214 | ||
1215 | static void null_release(struct gendisk *disk, fmode_t mode) | |
1216 | { | |
1217 | } | |
1218 | ||
1219 | static const struct block_device_operations null_fops = { | |
1220 | .owner = THIS_MODULE, | |
1221 | .open = null_open, | |
1222 | .release = null_release, | |
1223 | }; | |
1224 | ||
82f402fe JA |
1225 | static 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; | |
2984c868 | 1232 | nq->dev = nullb->dev; |
82f402fe JA |
1233 | } |
1234 | ||
1235 | static 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 | ||
f2298c04 JA |
1252 | static 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) | |
2d263a78 | 1259 | return -ENOMEM; |
f2298c04 JA |
1260 | |
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); | |
2d263a78 | 1265 | return -ENOMEM; |
f2298c04 JA |
1266 | } |
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 | ||
f2298c04 JA |
1278 | static int setup_queues(struct nullb *nullb) |
1279 | { | |
2984c868 SL |
1280 | nullb->queues = kzalloc(nullb->dev->submit_queues * |
1281 | sizeof(struct nullb_queue), GFP_KERNEL); | |
f2298c04 | 1282 | if (!nullb->queues) |
2d263a78 | 1283 | return -ENOMEM; |
f2298c04 JA |
1284 | |
1285 | nullb->nr_queues = 0; | |
2984c868 | 1286 | nullb->queue_depth = nullb->dev->hw_queue_depth; |
f2298c04 | 1287 | |
2d263a78 MB |
1288 | return 0; |
1289 | } | |
1290 | ||
1291 | static int init_driver_queues(struct nullb *nullb) | |
1292 | { | |
1293 | struct nullb_queue *nq; | |
1294 | int i, ret = 0; | |
f2298c04 | 1295 | |
2984c868 | 1296 | for (i = 0; i < nullb->dev->submit_queues; i++) { |
f2298c04 | 1297 | nq = &nullb->queues[i]; |
2d263a78 MB |
1298 | |
1299 | null_init_queue(nullb, nq); | |
1300 | ||
1301 | ret = setup_commands(nq); | |
1302 | if (ret) | |
31f9690e | 1303 | return ret; |
f2298c04 JA |
1304 | nullb->nr_queues++; |
1305 | } | |
2d263a78 | 1306 | return 0; |
f2298c04 JA |
1307 | } |
1308 | ||
9ae2d0aa | 1309 | static int null_gendisk_register(struct nullb *nullb) |
f2298c04 JA |
1310 | { |
1311 | struct gendisk *disk; | |
f2298c04 | 1312 | sector_t size; |
9ae2d0aa | 1313 | |
2984c868 | 1314 | disk = nullb->disk = alloc_disk_node(1, nullb->dev->home_node); |
9ae2d0aa MB |
1315 | if (!disk) |
1316 | return -ENOMEM; | |
2984c868 | 1317 | size = (sector_t)nullb->dev->size * 1024 * 1024ULL; |
9ae2d0aa MB |
1318 | 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 | ||
2984c868 | 1332 | static int null_init_tag_set(struct nullb *nullb, struct blk_mq_tag_set *set) |
82f402fe JA |
1333 | { |
1334 | set->ops = &null_mq_ops; | |
2984c868 SL |
1335 | 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; | |
82f402fe JA |
1340 | set->cmd_size = sizeof(struct nullb_cmd); |
1341 | set->flags = BLK_MQ_F_SHOULD_MERGE; | |
1342 | set->driver_data = NULL; | |
1343 | ||
2984c868 | 1344 | if (nullb->dev->blocking) |
82f402fe JA |
1345 | set->flags |= BLK_MQ_F_BLOCKING; |
1346 | ||
1347 | return blk_mq_alloc_tag_set(set); | |
1348 | } | |
1349 | ||
cedcafad SL |
1350 | static 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); | |
5bcd0e0c SL |
1370 | |
1371 | /* Do memory allocation, so set blocking */ | |
1372 | if (dev->memory_backed) | |
1373 | dev->blocking = true; | |
cedcafad SL |
1374 | } |
1375 | ||
2984c868 | 1376 | static int null_add_dev(struct nullb_device *dev) |
9ae2d0aa MB |
1377 | { |
1378 | struct nullb *nullb; | |
dc501dc0 | 1379 | int rv; |
f2298c04 | 1380 | |
cedcafad SL |
1381 | null_validate_conf(dev); |
1382 | ||
2984c868 | 1383 | nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, dev->home_node); |
dc501dc0 RE |
1384 | if (!nullb) { |
1385 | rv = -ENOMEM; | |
24d2f903 | 1386 | goto out; |
dc501dc0 | 1387 | } |
2984c868 SL |
1388 | nullb->dev = dev; |
1389 | dev->nullb = nullb; | |
f2298c04 JA |
1390 | |
1391 | spin_lock_init(&nullb->lock); | |
1392 | ||
dc501dc0 RE |
1393 | rv = setup_queues(nullb); |
1394 | if (rv) | |
24d2f903 | 1395 | goto out_free_nullb; |
f2298c04 | 1396 | |
2984c868 | 1397 | if (dev->queue_mode == NULL_Q_MQ) { |
82f402fe JA |
1398 | if (shared_tags) { |
1399 | nullb->tag_set = &tag_set; | |
1400 | rv = 0; | |
1401 | } else { | |
1402 | nullb->tag_set = &nullb->__tag_set; | |
2984c868 | 1403 | rv = null_init_tag_set(nullb, nullb->tag_set); |
82f402fe JA |
1404 | } |
1405 | ||
dc501dc0 | 1406 | if (rv) |
24d2f903 CH |
1407 | goto out_cleanup_queues; |
1408 | ||
82f402fe | 1409 | nullb->q = blk_mq_init_queue(nullb->tag_set); |
35b489d3 | 1410 | if (IS_ERR(nullb->q)) { |
dc501dc0 | 1411 | rv = -ENOMEM; |
24d2f903 | 1412 | goto out_cleanup_tags; |
dc501dc0 | 1413 | } |
82f402fe | 1414 | null_init_queues(nullb); |
2984c868 SL |
1415 | } else if (dev->queue_mode == NULL_Q_BIO) { |
1416 | nullb->q = blk_alloc_queue_node(GFP_KERNEL, dev->home_node); | |
dc501dc0 RE |
1417 | if (!nullb->q) { |
1418 | rv = -ENOMEM; | |
24d2f903 | 1419 | goto out_cleanup_queues; |
dc501dc0 | 1420 | } |
f2298c04 | 1421 | blk_queue_make_request(nullb->q, null_queue_bio); |
31f9690e JK |
1422 | rv = init_driver_queues(nullb); |
1423 | if (rv) | |
1424 | goto out_cleanup_blk_queue; | |
f2298c04 | 1425 | } else { |
2984c868 SL |
1426 | nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock, |
1427 | dev->home_node); | |
dc501dc0 RE |
1428 | if (!nullb->q) { |
1429 | rv = -ENOMEM; | |
24d2f903 | 1430 | goto out_cleanup_queues; |
dc501dc0 | 1431 | } |
f2298c04 | 1432 | blk_queue_prep_rq(nullb->q, null_rq_prep_fn); |
24d2f903 | 1433 | blk_queue_softirq_done(nullb->q, null_softirq_done_fn); |
31f9690e JK |
1434 | rv = init_driver_queues(nullb); |
1435 | if (rv) | |
1436 | goto out_cleanup_blk_queue; | |
f2298c04 JA |
1437 | } |
1438 | ||
f2298c04 JA |
1439 | nullb->q->queuedata = nullb; |
1440 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q); | |
b277da0a | 1441 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, nullb->q); |
f2298c04 | 1442 | |
f2298c04 | 1443 | mutex_lock(&lock); |
94bc02e3 | 1444 | nullb->index = ida_simple_get(&nullb_indexes, 0, 0, GFP_KERNEL); |
cedcafad | 1445 | dev->index = nullb->index; |
f2298c04 JA |
1446 | mutex_unlock(&lock); |
1447 | ||
2984c868 SL |
1448 | blk_queue_logical_block_size(nullb->q, dev->blocksize); |
1449 | blk_queue_physical_block_size(nullb->q, dev->blocksize); | |
f2298c04 | 1450 | |
b2b7e001 MB |
1451 | sprintf(nullb->disk_name, "nullb%d", nullb->index); |
1452 | ||
2984c868 | 1453 | if (dev->use_lightnvm) |
9ae2d0aa MB |
1454 | rv = null_nvm_register(nullb); |
1455 | else | |
1456 | rv = null_gendisk_register(nullb); | |
b2b7e001 | 1457 | |
9ae2d0aa MB |
1458 | if (rv) |
1459 | goto out_cleanup_blk_queue; | |
a514379b MB |
1460 | |
1461 | mutex_lock(&lock); | |
1462 | list_add_tail(&nullb->list, &nullb_list); | |
1463 | mutex_unlock(&lock); | |
3681c85d | 1464 | |
f2298c04 | 1465 | return 0; |
24d2f903 CH |
1466 | out_cleanup_blk_queue: |
1467 | blk_cleanup_queue(nullb->q); | |
1468 | out_cleanup_tags: | |
2984c868 | 1469 | if (dev->queue_mode == NULL_Q_MQ && nullb->tag_set == &nullb->__tag_set) |
82f402fe | 1470 | blk_mq_free_tag_set(nullb->tag_set); |
24d2f903 CH |
1471 | out_cleanup_queues: |
1472 | cleanup_queues(nullb); | |
1473 | out_free_nullb: | |
1474 | kfree(nullb); | |
1475 | out: | |
2984c868 | 1476 | null_free_dev(dev); |
dc501dc0 | 1477 | return rv; |
f2298c04 JA |
1478 | } |
1479 | ||
1480 | static int __init null_init(void) | |
1481 | { | |
af096e22 | 1482 | int ret = 0; |
f2298c04 | 1483 | unsigned int i; |
af096e22 | 1484 | struct nullb *nullb; |
2984c868 | 1485 | struct nullb_device *dev; |
f2298c04 | 1486 | |
2984c868 | 1487 | if (g_bs > PAGE_SIZE) { |
9967d8ac R |
1488 | pr_warn("null_blk: invalid block size\n"); |
1489 | pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE); | |
2984c868 | 1490 | g_bs = PAGE_SIZE; |
9967d8ac | 1491 | } |
f2298c04 | 1492 | |
2984c868 | 1493 | if (g_use_lightnvm && g_bs != 4096) { |
6bb9535b MB |
1494 | pr_warn("null_blk: LightNVM only supports 4k block size\n"); |
1495 | pr_warn("null_blk: defaults block size to 4k\n"); | |
2984c868 | 1496 | g_bs = 4096; |
6bb9535b MB |
1497 | } |
1498 | ||
2984c868 | 1499 | if (g_use_lightnvm && g_queue_mode != NULL_Q_MQ) { |
b2b7e001 MB |
1500 | pr_warn("null_blk: LightNVM only supported for blk-mq\n"); |
1501 | pr_warn("null_blk: defaults queue mode to blk-mq\n"); | |
2984c868 | 1502 | g_queue_mode = NULL_Q_MQ; |
b2b7e001 MB |
1503 | } |
1504 | ||
2984c868 SL |
1505 | if (g_queue_mode == NULL_Q_MQ && g_use_per_node_hctx) { |
1506 | if (g_submit_queues != nr_online_nodes) { | |
558ab300 | 1507 | pr_warn("null_blk: submit_queues param is set to %u.\n", |
d15ee6b1 | 1508 | nr_online_nodes); |
2984c868 | 1509 | g_submit_queues = nr_online_nodes; |
fc1bc354 | 1510 | } |
2984c868 SL |
1511 | } 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; | |
f2298c04 | 1515 | |
2984c868 SL |
1516 | if (g_queue_mode == NULL_Q_MQ && shared_tags) { |
1517 | ret = null_init_tag_set(NULL, &tag_set); | |
db2d153d MG |
1518 | if (ret) |
1519 | return ret; | |
1520 | } | |
1521 | ||
3bf2bd20 SL |
1522 | 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 | ||
f2298c04 JA |
1529 | mutex_init(&lock); |
1530 | ||
f2298c04 | 1531 | null_major = register_blkdev(0, "nullb"); |
db2d153d MG |
1532 | if (null_major < 0) { |
1533 | ret = null_major; | |
3bf2bd20 | 1534 | goto err_conf; |
db2d153d | 1535 | } |
f2298c04 | 1536 | |
2984c868 | 1537 | if (g_use_lightnvm) { |
6bb9535b MB |
1538 | 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"); | |
af096e22 MH |
1542 | ret = -ENOMEM; |
1543 | goto err_ppa; | |
6bb9535b MB |
1544 | } |
1545 | } | |
1546 | ||
f2298c04 | 1547 | for (i = 0; i < nr_devices; i++) { |
2984c868 SL |
1548 | dev = null_alloc_dev(); |
1549 | if (!dev) | |
1550 | goto err_dev; | |
1551 | ret = null_add_dev(dev); | |
1552 | if (ret) { | |
1553 | null_free_dev(dev); | |
af096e22 | 1554 | goto err_dev; |
2984c868 | 1555 | } |
f2298c04 JA |
1556 | } |
1557 | ||
1558 | pr_info("null: module loaded\n"); | |
1559 | return 0; | |
af096e22 MH |
1560 | |
1561 | err_dev: | |
1562 | while (!list_empty(&nullb_list)) { | |
1563 | nullb = list_entry(nullb_list.next, struct nullb, list); | |
2984c868 | 1564 | dev = nullb->dev; |
af096e22 | 1565 | null_del_dev(nullb); |
2984c868 | 1566 | null_free_dev(dev); |
af096e22 | 1567 | } |
6bb9535b | 1568 | kmem_cache_destroy(ppa_cache); |
af096e22 MH |
1569 | err_ppa: |
1570 | unregister_blkdev(null_major, "nullb"); | |
3bf2bd20 SL |
1571 | err_conf: |
1572 | configfs_unregister_subsystem(&nullb_subsys); | |
db2d153d | 1573 | err_tagset: |
2984c868 | 1574 | if (g_queue_mode == NULL_Q_MQ && shared_tags) |
db2d153d | 1575 | blk_mq_free_tag_set(&tag_set); |
af096e22 | 1576 | return ret; |
f2298c04 JA |
1577 | } |
1578 | ||
1579 | static void __exit null_exit(void) | |
1580 | { | |
1581 | struct nullb *nullb; | |
1582 | ||
3bf2bd20 SL |
1583 | configfs_unregister_subsystem(&nullb_subsys); |
1584 | ||
f2298c04 JA |
1585 | unregister_blkdev(null_major, "nullb"); |
1586 | ||
1587 | mutex_lock(&lock); | |
1588 | while (!list_empty(&nullb_list)) { | |
2984c868 SL |
1589 | struct nullb_device *dev; |
1590 | ||
f2298c04 | 1591 | nullb = list_entry(nullb_list.next, struct nullb, list); |
2984c868 | 1592 | dev = nullb->dev; |
f2298c04 | 1593 | null_del_dev(nullb); |
2984c868 | 1594 | null_free_dev(dev); |
f2298c04 JA |
1595 | } |
1596 | mutex_unlock(&lock); | |
6bb9535b | 1597 | |
2984c868 | 1598 | if (g_queue_mode == NULL_Q_MQ && shared_tags) |
82f402fe JA |
1599 | blk_mq_free_tag_set(&tag_set); |
1600 | ||
6bb9535b | 1601 | kmem_cache_destroy(ppa_cache); |
f2298c04 JA |
1602 | } |
1603 | ||
1604 | module_init(null_init); | |
1605 | module_exit(null_exit); | |
1606 | ||
1607 | MODULE_AUTHOR("Jens Axboe <jaxboe@fusionio.com>"); | |
1608 | MODULE_LICENSE("GPL"); |