Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Add configfs and memory store: Kyungchan Koh <kkc6196@fb.com> and |
| 3 | * Shaohua Li <shli@fb.com> |
| 4 | */ |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 5 | #include <linux/module.h> |
Matias Bjørling | fc1bc35 | 2013-12-21 00:11:01 +0100 | [diff] [blame] | 6 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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> |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 15 | #include <linux/lightnvm.h> |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 16 | #include <linux/configfs.h> |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 17 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 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 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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; |
Paolo Valente | 3c395a9 | 2015-12-01 11:48:17 +0100 | [diff] [blame] | 34 | struct hrtimer timer; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 35 | blk_status_t error; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | struct nullb_queue { |
| 39 | unsigned long *tag_map; |
| 40 | wait_queue_head_t wait; |
| 41 | unsigned int queue_depth; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 42 | struct nullb_device *dev; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 43 | |
| 44 | struct nullb_cmd *cmds; |
| 45 | }; |
| 46 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 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 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 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 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 71 | struct nullb_device { |
| 72 | struct nullb *nullb; |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 73 | struct config_item item; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 74 | struct radix_tree_root data; /* data stored in the disk */ |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 75 | unsigned long flags; /* device flags */ |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 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 */ |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 85 | unsigned int index; /* index of the disk, only valid with a disk */ |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 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 */ |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 89 | bool power; /* power on/off the device */ |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 90 | bool memory_backed; /* if data is stored in memory */ |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 93 | struct nullb { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 94 | struct nullb_device *dev; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 95 | struct list_head list; |
| 96 | unsigned int index; |
| 97 | struct request_queue *q; |
| 98 | struct gendisk *disk; |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 99 | struct nvm_dev *ndev; |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 100 | struct blk_mq_tag_set *tag_set; |
| 101 | struct blk_mq_tag_set __tag_set; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 102 | 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ørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 108 | char disk_name[DISK_NAME_LEN]; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | static LIST_HEAD(nullb_list); |
| 112 | static struct mutex lock; |
| 113 | static int null_major; |
Shaohua Li | 94bc02e | 2017-08-14 15:04:55 -0700 | [diff] [blame] | 114 | static DEFINE_IDA(nullb_indexes); |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 115 | static struct kmem_cache *ppa_cache; |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 116 | static struct blk_mq_tag_set tag_set; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 117 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 118 | enum { |
| 119 | NULL_IRQ_NONE = 0, |
| 120 | NULL_IRQ_SOFTIRQ = 1, |
| 121 | NULL_IRQ_TIMER = 2, |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 122 | }; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 123 | |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 124 | enum { |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 125 | NULL_Q_BIO = 0, |
| 126 | NULL_Q_RQ = 1, |
| 127 | NULL_Q_MQ = 2, |
| 128 | }; |
| 129 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 130 | static int g_submit_queues = 1; |
| 131 | module_param_named(submit_queues, g_submit_queues, int, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 132 | MODULE_PARM_DESC(submit_queues, "Number of submission queues"); |
| 133 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 134 | static int g_home_node = NUMA_NO_NODE; |
| 135 | module_param_named(home_node, g_home_node, int, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 136 | MODULE_PARM_DESC(home_node, "Home node for the device"); |
| 137 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 138 | static int g_queue_mode = NULL_Q_MQ; |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 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 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 157 | return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ); |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 160 | static const struct kernel_param_ops null_queue_mode_param_ops = { |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 161 | .set = null_set_queue_mode, |
| 162 | .get = param_get_int, |
| 163 | }; |
| 164 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 165 | device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, S_IRUGO); |
Mike Snitzer | 54ae81c | 2014-06-11 17:13:50 -0400 | [diff] [blame] | 166 | MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)"); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 167 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 168 | static int g_gb = 250; |
| 169 | module_param_named(gb, g_gb, int, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 170 | MODULE_PARM_DESC(gb, "Size in GB"); |
| 171 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 172 | static int g_bs = 512; |
| 173 | module_param_named(bs, g_bs, int, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 174 | MODULE_PARM_DESC(bs, "Block size (in bytes)"); |
| 175 | |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 176 | static int nr_devices = 1; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 177 | module_param(nr_devices, int, S_IRUGO); |
| 178 | MODULE_PARM_DESC(nr_devices, "Number of devices to register"); |
| 179 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 180 | static bool g_use_lightnvm; |
| 181 | module_param_named(use_lightnvm, g_use_lightnvm, bool, S_IRUGO); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 182 | MODULE_PARM_DESC(use_lightnvm, "Register as a LightNVM device"); |
| 183 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 184 | static bool g_blocking; |
| 185 | module_param_named(blocking, g_blocking, bool, S_IRUGO); |
Jens Axboe | db5bcf8 | 2017-03-30 13:44:26 -0600 | [diff] [blame] | 186 | MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device"); |
| 187 | |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 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 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 192 | static int g_irqmode = NULL_IRQ_SOFTIRQ; |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 193 | |
| 194 | static int null_set_irqmode(const char *str, const struct kernel_param *kp) |
| 195 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 196 | return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE, |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 197 | NULL_IRQ_TIMER); |
| 198 | } |
| 199 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 200 | static const struct kernel_param_ops null_irqmode_param_ops = { |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 201 | .set = null_set_irqmode, |
| 202 | .get = param_get_int, |
| 203 | }; |
| 204 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 205 | device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 206 | MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer"); |
| 207 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 208 | static unsigned long g_completion_nsec = 10000; |
| 209 | module_param_named(completion_nsec, g_completion_nsec, ulong, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 210 | MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns"); |
| 211 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 212 | static int g_hw_queue_depth = 64; |
| 213 | module_param_named(hw_queue_depth, g_hw_queue_depth, int, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 214 | MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64"); |
| 215 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 216 | static bool g_use_per_node_hctx; |
| 217 | module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, S_IRUGO); |
Matias Bjørling | 2000524 | 2013-12-21 00:11:00 +0100 | [diff] [blame] | 218 | MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false"); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 219 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 220 | static struct nullb_device *null_alloc_dev(void); |
| 221 | static void null_free_dev(struct nullb_device *dev); |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 222 | static void null_del_dev(struct nullb *nullb); |
| 223 | static int null_add_dev(struct nullb_device *dev); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 224 | static void null_free_device_storage(struct nullb_device *dev); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 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); |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 316 | NULLB_DEVICE_ATTR(index, uint); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 317 | NULLB_DEVICE_ATTR(use_lightnvm, bool); |
| 318 | NULLB_DEVICE_ATTR(blocking, bool); |
| 319 | NULLB_DEVICE_ATTR(use_per_node_hctx, bool); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 320 | NULLB_DEVICE_ATTR(memory_backed, bool); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 321 | |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 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 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 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, |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 370 | &nullb_device_attr_index, |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 371 | &nullb_device_attr_use_lightnvm, |
| 372 | &nullb_device_attr_blocking, |
| 373 | &nullb_device_attr_use_per_node_hctx, |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 374 | &nullb_device_attr_power, |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 375 | &nullb_device_attr_memory_backed, |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 376 | NULL, |
| 377 | }; |
| 378 | |
| 379 | static void nullb_device_release(struct config_item *item) |
| 380 | { |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 381 | struct nullb_device *dev = to_nullb_device(item); |
| 382 | |
| 383 | null_free_device_storage(dev); |
| 384 | null_free_dev(dev); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 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 | { |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 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 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 423 | config_item_put(item); |
| 424 | } |
| 425 | |
| 426 | static ssize_t memb_group_features_show(struct config_item *item, char *page) |
| 427 | { |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 428 | return snprintf(page, PAGE_SIZE, "memory_backed\n"); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 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 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 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; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 465 | INIT_RADIX_TREE(&dev->data, GFP_ATOMIC); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 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 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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 | |
Paolo Valente | 3c395a9 | 2015-12-01 11:48:17 +0100 | [diff] [blame] | 511 | static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer); |
| 512 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 523 | if (nq->dev->irqmode == NULL_IRQ_TIMER) { |
Paolo Valente | 3c395a9 | 2015-12-01 11:48:17 +0100 | [diff] [blame] | 524 | hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, |
| 525 | HRTIMER_MODE_REL); |
| 526 | cmd->timer.function = null_cmd_timer_expired; |
| 527 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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 | { |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 558 | struct request_queue *q = NULL; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 559 | int queue_mode = cmd->nq->dev->queue_mode; |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 560 | |
Mike Krinkin | e827120 | 2015-12-15 12:56:40 +0300 | [diff] [blame] | 561 | if (cmd->rq) |
| 562 | q = cmd->rq->q; |
| 563 | |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 564 | switch (queue_mode) { |
| 565 | case NULL_Q_MQ: |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 566 | blk_mq_end_request(cmd->rq, cmd->error); |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 567 | return; |
| 568 | case NULL_Q_RQ: |
| 569 | INIT_LIST_HEAD(&cmd->rq->queuelist); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 570 | blk_end_request_all(cmd->rq, cmd->error); |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 571 | break; |
| 572 | case NULL_Q_BIO: |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 573 | cmd->bio->bi_status = cmd->error; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 574 | bio_endio(cmd->bio); |
Jens Axboe | 48cc661 | 2015-12-28 13:02:47 -0700 | [diff] [blame] | 575 | break; |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 576 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 577 | |
Jens Axboe | 48cc661 | 2015-12-28 13:02:47 -0700 | [diff] [blame] | 578 | free_cmd(cmd); |
| 579 | |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 580 | /* Restart queue if needed, as we are freeing a tag */ |
Jens Axboe | 48cc661 | 2015-12-28 13:02:47 -0700 | [diff] [blame] | 581 | if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) { |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 582 | unsigned long flags; |
| 583 | |
| 584 | spin_lock_irqsave(q->queue_lock, flags); |
Jens Axboe | 48cc661 | 2015-12-28 13:02:47 -0700 | [diff] [blame] | 585 | blk_start_queue_async(q); |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 586 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 587 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer) |
| 591 | { |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 592 | end_cmd(container_of(timer, struct nullb_cmd, timer)); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 593 | |
| 594 | return HRTIMER_NORESTART; |
| 595 | } |
| 596 | |
| 597 | static void null_cmd_end_timer(struct nullb_cmd *cmd) |
| 598 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 599 | ktime_t kt = cmd->nq->dev->completion_nsec; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 600 | |
Paolo Valente | 3c395a9 | 2015-12-01 11:48:17 +0100 | [diff] [blame] | 601 | hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | static void null_softirq_done_fn(struct request *rq) |
| 605 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 606 | struct nullb *nullb = rq->q->queuedata; |
| 607 | |
| 608 | if (nullb->dev->queue_mode == NULL_Q_MQ) |
Jens Axboe | d891fa7 | 2014-06-16 11:40:25 -0600 | [diff] [blame] | 609 | end_cmd(blk_mq_rq_to_pdu(rq)); |
| 610 | else |
| 611 | end_cmd(rq->special); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 612 | } |
| 613 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 614 | static struct nullb_page *null_alloc_page(gfp_t gfp_flags) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 615 | { |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 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) |
| 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 | |
| 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); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 901 | /* Complete IO by inline, softirq or timer */ |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 902 | switch (dev->irqmode) { |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 903 | case NULL_IRQ_SOFTIRQ: |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 904 | switch (dev->queue_mode) { |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 905 | case NULL_Q_MQ: |
Christoph Hellwig | 08e0029 | 2017-04-20 16:03:09 +0200 | [diff] [blame] | 906 | blk_mq_complete_request(cmd->rq); |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 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; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 919 | case NULL_IRQ_NONE: |
| 920 | end_cmd(cmd); |
| 921 | break; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 922 | case NULL_IRQ_TIMER: |
| 923 | null_cmd_end_timer(cmd); |
| 924 | break; |
| 925 | } |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 926 | return BLK_STS_OK; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 939 | static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 949 | return BLK_QC_T_NONE; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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 | } |
Akinobu Mita | 8b70f45 | 2015-06-02 08:35:10 +0900 | [diff] [blame] | 964 | blk_stop_queue(q); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 982 | static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx, |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 983 | const struct blk_mq_queue_data *bd) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 984 | { |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 985 | struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 986 | struct nullb_queue *nq = hctx->driver_data; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 987 | |
Jens Axboe | db5bcf8 | 2017-03-30 13:44:26 -0600 | [diff] [blame] | 988 | might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING); |
| 989 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 990 | if (nq->dev->irqmode == NULL_IRQ_TIMER) { |
Paolo Valente | 3c395a9 | 2015-12-01 11:48:17 +0100 | [diff] [blame] | 991 | hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 992 | cmd->timer.function = null_cmd_timer_expired; |
| 993 | } |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 994 | cmd->rq = bd->rq; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 995 | cmd->nq = nq; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 996 | |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 997 | blk_mq_start_request(bd->rq); |
Christoph Hellwig | e249007 | 2014-09-13 16:40:09 -0700 | [diff] [blame] | 998 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 999 | return null_handle_cmd(cmd); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1000 | } |
| 1001 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 1002 | static const struct blk_mq_ops null_mq_ops = { |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1003 | .queue_rq = null_queue_rq, |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 1004 | .complete = null_softirq_done_fn, |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1005 | }; |
| 1006 | |
Matias Bjørling | de65d2d | 2015-08-31 14:17:18 +0200 | [diff] [blame] | 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 | |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1023 | #ifdef CONFIG_NVM |
| 1024 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1025 | static void null_lnvm_end_io(struct request *rq, blk_status_t status) |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1026 | { |
| 1027 | struct nvm_rq *rqd = rq->end_io_data; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1028 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1029 | /* XXX: lighnvm core seems to expect NVM_RSP_* values here.. */ |
| 1030 | rqd->error = status ? -EIO : 0; |
Matias Bjørling | 06894ef | 2017-01-31 13:17:17 +0100 | [diff] [blame] | 1031 | nvm_end_io(rqd); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1032 | |
| 1033 | blk_put_request(rq); |
| 1034 | } |
| 1035 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1036 | static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd) |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1037 | { |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1038 | struct request_queue *q = dev->q; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1039 | struct request *rq; |
| 1040 | struct bio *bio = rqd->bio; |
| 1041 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 1042 | rq = blk_mq_alloc_request(q, |
| 1043 | op_is_write(bio_op(bio)) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1044 | if (IS_ERR(rq)) |
| 1045 | return -ENOMEM; |
| 1046 | |
Bart Van Assche | 2644a3c | 2017-04-19 14:01:25 -0700 | [diff] [blame] | 1047 | blk_init_request_from_bio(rq, bio); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 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 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1056 | static int null_lnvm_id(struct nvm_dev *dev, struct nvm_id *id) |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1057 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1058 | struct nullb *nullb = dev->q->queuedata; |
| 1059 | sector_t size = (sector_t)nullb->dev->size * 1024 * 1024ULL; |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1060 | sector_t blksize; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1061 | struct nvm_id_group *grp; |
| 1062 | |
| 1063 | id->ver_id = 0x1; |
| 1064 | id->vmnt = 0; |
Matias Bjørling | bf64318 | 2016-02-04 15:13:27 +0100 | [diff] [blame] | 1065 | id->cap = 0x2; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1066 | id->dom = 0x1; |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 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; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1080 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1081 | sector_div(size, nullb->dev->blocksize); /* convert size to pages */ |
Arnd Bergmann | e93d12a | 2016-01-13 23:04:08 +0100 | [diff] [blame] | 1082 | size >>= 8; /* concert size to pgs pr blk */ |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 1083 | grp = &id->grp; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1084 | grp->mtype = 0; |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1085 | grp->fmtype = 0; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1086 | grp->num_ch = 1; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1087 | grp->num_pg = 256; |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1088 | blksize = size; |
Arnd Bergmann | e93d12a | 2016-01-13 23:04:08 +0100 | [diff] [blame] | 1089 | size >>= 16; |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1090 | grp->num_lun = size + 1; |
Arnd Bergmann | e93d12a | 2016-01-13 23:04:08 +0100 | [diff] [blame] | 1091 | sector_div(blksize, grp->num_lun); |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1092 | grp->num_blk = blksize; |
| 1093 | grp->num_pln = 1; |
| 1094 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1095 | grp->fpg_sz = nullb->dev->blocksize; |
| 1096 | grp->csecs = nullb->dev->blocksize; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 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 */ |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1104 | grp->cpar = nullb->dev->hw_queue_depth; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1105 | |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1109 | static void *null_lnvm_create_dma_pool(struct nvm_dev *dev, char *name) |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1110 | { |
| 1111 | mempool_t *virtmem_pool; |
| 1112 | |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1113 | virtmem_pool = mempool_create_slab_pool(64, ppa_cache); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 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 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1127 | static void *null_lnvm_dev_dma_alloc(struct nvm_dev *dev, void *pool, |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 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 | }; |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1151 | |
| 1152 | static int null_nvm_register(struct nullb *nullb) |
| 1153 | { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 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; |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | static void null_nvm_unregister(struct nullb *nullb) |
| 1175 | { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 1176 | nvm_unregister(nullb->ndev); |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1177 | } |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1178 | #else |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1179 | static int null_nvm_register(struct nullb *nullb) |
| 1180 | { |
Yasuaki Ishimatsu | 92153d3 | 2016-11-16 08:26:11 -0700 | [diff] [blame] | 1181 | pr_err("null_blk: CONFIG_NVM needs to be enabled for LightNVM\n"); |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1182 | return -EINVAL; |
| 1183 | } |
| 1184 | static void null_nvm_unregister(struct nullb *nullb) {} |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1185 | #endif /* CONFIG_NVM */ |
| 1186 | |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1187 | static void null_del_dev(struct nullb *nullb) |
| 1188 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1189 | struct nullb_device *dev = nullb->dev; |
| 1190 | |
Shaohua Li | 94bc02e | 2017-08-14 15:04:55 -0700 | [diff] [blame] | 1191 | ida_simple_remove(&nullb_indexes, nullb->index); |
| 1192 | |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1193 | list_del_init(&nullb->list); |
| 1194 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1195 | if (dev->use_lightnvm) |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1196 | null_nvm_unregister(nullb); |
| 1197 | else |
| 1198 | del_gendisk(nullb->disk); |
| 1199 | blk_cleanup_queue(nullb->q); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1200 | if (dev->queue_mode == NULL_Q_MQ && |
| 1201 | nullb->tag_set == &nullb->__tag_set) |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1202 | blk_mq_free_tag_set(nullb->tag_set); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1203 | if (!dev->use_lightnvm) |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1204 | put_disk(nullb->disk); |
| 1205 | cleanup_queues(nullb); |
| 1206 | kfree(nullb); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1207 | dev->nullb = NULL; |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1208 | } |
| 1209 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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 | |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 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; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1232 | nq->dev = nullb->dev; |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 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 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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) |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1259 | return -ENOMEM; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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); |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1265 | return -ENOMEM; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1278 | static int setup_queues(struct nullb *nullb) |
| 1279 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1280 | nullb->queues = kzalloc(nullb->dev->submit_queues * |
| 1281 | sizeof(struct nullb_queue), GFP_KERNEL); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1282 | if (!nullb->queues) |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1283 | return -ENOMEM; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1284 | |
| 1285 | nullb->nr_queues = 0; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1286 | nullb->queue_depth = nullb->dev->hw_queue_depth; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1287 | |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 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; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1295 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1296 | for (i = 0; i < nullb->dev->submit_queues; i++) { |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1297 | nq = &nullb->queues[i]; |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1298 | |
| 1299 | null_init_queue(nullb, nq); |
| 1300 | |
| 1301 | ret = setup_commands(nq); |
| 1302 | if (ret) |
Jan Kara | 31f9690 | 2014-10-22 15:34:21 +0200 | [diff] [blame] | 1303 | return ret; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1304 | nullb->nr_queues++; |
| 1305 | } |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1306 | return 0; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1307 | } |
| 1308 | |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1309 | static int null_gendisk_register(struct nullb *nullb) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1310 | { |
| 1311 | struct gendisk *disk; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1312 | sector_t size; |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1313 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1314 | disk = nullb->disk = alloc_disk_node(1, nullb->dev->home_node); |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1315 | if (!disk) |
| 1316 | return -ENOMEM; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1317 | size = (sector_t)nullb->dev->size * 1024 * 1024ULL; |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 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 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1332 | static int null_init_tag_set(struct nullb *nullb, struct blk_mq_tag_set *set) |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1333 | { |
| 1334 | set->ops = &null_mq_ops; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 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; |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1340 | set->cmd_size = sizeof(struct nullb_cmd); |
| 1341 | set->flags = BLK_MQ_F_SHOULD_MERGE; |
| 1342 | set->driver_data = NULL; |
| 1343 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1344 | if (nullb->dev->blocking) |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1345 | set->flags |= BLK_MQ_F_BLOCKING; |
| 1346 | |
| 1347 | return blk_mq_alloc_tag_set(set); |
| 1348 | } |
| 1349 | |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 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); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame^] | 1370 | |
| 1371 | /* Do memory allocation, so set blocking */ |
| 1372 | if (dev->memory_backed) |
| 1373 | dev->blocking = true; |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 1374 | } |
| 1375 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1376 | static int null_add_dev(struct nullb_device *dev) |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1377 | { |
| 1378 | struct nullb *nullb; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1379 | int rv; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1380 | |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 1381 | null_validate_conf(dev); |
| 1382 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1383 | nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, dev->home_node); |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1384 | if (!nullb) { |
| 1385 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1386 | goto out; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1387 | } |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1388 | nullb->dev = dev; |
| 1389 | dev->nullb = nullb; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1390 | |
| 1391 | spin_lock_init(&nullb->lock); |
| 1392 | |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1393 | rv = setup_queues(nullb); |
| 1394 | if (rv) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1395 | goto out_free_nullb; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1396 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1397 | if (dev->queue_mode == NULL_Q_MQ) { |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1398 | if (shared_tags) { |
| 1399 | nullb->tag_set = &tag_set; |
| 1400 | rv = 0; |
| 1401 | } else { |
| 1402 | nullb->tag_set = &nullb->__tag_set; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1403 | rv = null_init_tag_set(nullb, nullb->tag_set); |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1404 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1405 | |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1406 | if (rv) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1407 | goto out_cleanup_queues; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1408 | |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1409 | nullb->q = blk_mq_init_queue(nullb->tag_set); |
Ming Lei | 35b489d | 2015-01-02 14:25:27 +0000 | [diff] [blame] | 1410 | if (IS_ERR(nullb->q)) { |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1411 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1412 | goto out_cleanup_tags; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1413 | } |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1414 | null_init_queues(nullb); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1415 | } else if (dev->queue_mode == NULL_Q_BIO) { |
| 1416 | nullb->q = blk_alloc_queue_node(GFP_KERNEL, dev->home_node); |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1417 | if (!nullb->q) { |
| 1418 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1419 | goto out_cleanup_queues; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1420 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1421 | blk_queue_make_request(nullb->q, null_queue_bio); |
Jan Kara | 31f9690 | 2014-10-22 15:34:21 +0200 | [diff] [blame] | 1422 | rv = init_driver_queues(nullb); |
| 1423 | if (rv) |
| 1424 | goto out_cleanup_blk_queue; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1425 | } else { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1426 | nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock, |
| 1427 | dev->home_node); |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1428 | if (!nullb->q) { |
| 1429 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1430 | goto out_cleanup_queues; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1431 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1432 | blk_queue_prep_rq(nullb->q, null_rq_prep_fn); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1433 | blk_queue_softirq_done(nullb->q, null_softirq_done_fn); |
Jan Kara | 31f9690 | 2014-10-22 15:34:21 +0200 | [diff] [blame] | 1434 | rv = init_driver_queues(nullb); |
| 1435 | if (rv) |
| 1436 | goto out_cleanup_blk_queue; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1437 | } |
| 1438 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1439 | nullb->q->queuedata = nullb; |
| 1440 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q); |
Mike Snitzer | b277da0 | 2014-10-04 10:55:32 -0600 | [diff] [blame] | 1441 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, nullb->q); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1442 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1443 | mutex_lock(&lock); |
Shaohua Li | 94bc02e | 2017-08-14 15:04:55 -0700 | [diff] [blame] | 1444 | nullb->index = ida_simple_get(&nullb_indexes, 0, 0, GFP_KERNEL); |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 1445 | dev->index = nullb->index; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1446 | mutex_unlock(&lock); |
| 1447 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1448 | blk_queue_logical_block_size(nullb->q, dev->blocksize); |
| 1449 | blk_queue_physical_block_size(nullb->q, dev->blocksize); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1450 | |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1451 | sprintf(nullb->disk_name, "nullb%d", nullb->index); |
| 1452 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1453 | if (dev->use_lightnvm) |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1454 | rv = null_nvm_register(nullb); |
| 1455 | else |
| 1456 | rv = null_gendisk_register(nullb); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1457 | |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1458 | if (rv) |
| 1459 | goto out_cleanup_blk_queue; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1460 | |
Matias Bjørling | a514379 | 2016-02-11 14:49:13 +0100 | [diff] [blame] | 1461 | mutex_lock(&lock); |
| 1462 | list_add_tail(&nullb->list, &nullb_list); |
| 1463 | mutex_unlock(&lock); |
Wenwei Tao | 3681c85 | 2016-03-05 00:27:04 +0800 | [diff] [blame] | 1464 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1465 | return 0; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1466 | out_cleanup_blk_queue: |
| 1467 | blk_cleanup_queue(nullb->q); |
| 1468 | out_cleanup_tags: |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1469 | if (dev->queue_mode == NULL_Q_MQ && nullb->tag_set == &nullb->__tag_set) |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1470 | blk_mq_free_tag_set(nullb->tag_set); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1471 | out_cleanup_queues: |
| 1472 | cleanup_queues(nullb); |
| 1473 | out_free_nullb: |
| 1474 | kfree(nullb); |
| 1475 | out: |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1476 | null_free_dev(dev); |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1477 | return rv; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | static int __init null_init(void) |
| 1481 | { |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1482 | int ret = 0; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1483 | unsigned int i; |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1484 | struct nullb *nullb; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1485 | struct nullb_device *dev; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1486 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1487 | if (g_bs > PAGE_SIZE) { |
Raghavendra K T | 9967d8a | 2014-01-21 16:59:59 +0530 | [diff] [blame] | 1488 | pr_warn("null_blk: invalid block size\n"); |
| 1489 | pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1490 | g_bs = PAGE_SIZE; |
Raghavendra K T | 9967d8a | 2014-01-21 16:59:59 +0530 | [diff] [blame] | 1491 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1492 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1493 | if (g_use_lightnvm && g_bs != 4096) { |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1494 | pr_warn("null_blk: LightNVM only supports 4k block size\n"); |
| 1495 | pr_warn("null_blk: defaults block size to 4k\n"); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1496 | g_bs = 4096; |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1497 | } |
| 1498 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1499 | if (g_use_lightnvm && g_queue_mode != NULL_Q_MQ) { |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1500 | pr_warn("null_blk: LightNVM only supported for blk-mq\n"); |
| 1501 | pr_warn("null_blk: defaults queue mode to blk-mq\n"); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1502 | g_queue_mode = NULL_Q_MQ; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1503 | } |
| 1504 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1505 | if (g_queue_mode == NULL_Q_MQ && g_use_per_node_hctx) { |
| 1506 | if (g_submit_queues != nr_online_nodes) { |
weiping zhang | 558ab300 | 2017-08-03 00:26:39 +0800 | [diff] [blame] | 1507 | pr_warn("null_blk: submit_queues param is set to %u.\n", |
Matias Bjorling | d15ee6b | 2013-12-18 13:41:44 +0100 | [diff] [blame] | 1508 | nr_online_nodes); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1509 | g_submit_queues = nr_online_nodes; |
Matias Bjørling | fc1bc35 | 2013-12-21 00:11:01 +0100 | [diff] [blame] | 1510 | } |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 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; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1515 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1516 | if (g_queue_mode == NULL_Q_MQ && shared_tags) { |
| 1517 | ret = null_init_tag_set(NULL, &tag_set); |
Max Gurtovoy | db2d153 | 2017-07-06 18:00:07 +0300 | [diff] [blame] | 1518 | if (ret) |
| 1519 | return ret; |
| 1520 | } |
| 1521 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 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 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1529 | mutex_init(&lock); |
| 1530 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1531 | null_major = register_blkdev(0, "nullb"); |
Max Gurtovoy | db2d153 | 2017-07-06 18:00:07 +0300 | [diff] [blame] | 1532 | if (null_major < 0) { |
| 1533 | ret = null_major; |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 1534 | goto err_conf; |
Max Gurtovoy | db2d153 | 2017-07-06 18:00:07 +0300 | [diff] [blame] | 1535 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1536 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1537 | if (g_use_lightnvm) { |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 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"); |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1542 | ret = -ENOMEM; |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1543 | goto err_ppa; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1544 | } |
| 1545 | } |
| 1546 | |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1547 | for (i = 0; i < nr_devices; i++) { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1548 | dev = null_alloc_dev(); |
| 1549 | if (!dev) |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1550 | goto err_dev; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1551 | ret = null_add_dev(dev); |
| 1552 | if (ret) { |
| 1553 | null_free_dev(dev); |
| 1554 | goto err_dev; |
| 1555 | } |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1558 | pr_info("null: module loaded\n"); |
| 1559 | return 0; |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1560 | |
| 1561 | err_dev: |
| 1562 | while (!list_empty(&nullb_list)) { |
| 1563 | nullb = list_entry(nullb_list.next, struct nullb, list); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1564 | dev = nullb->dev; |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1565 | null_del_dev(nullb); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1566 | null_free_dev(dev); |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1567 | } |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1568 | kmem_cache_destroy(ppa_cache); |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1569 | err_ppa: |
| 1570 | unregister_blkdev(null_major, "nullb"); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 1571 | err_conf: |
| 1572 | configfs_unregister_subsystem(&nullb_subsys); |
Max Gurtovoy | db2d153 | 2017-07-06 18:00:07 +0300 | [diff] [blame] | 1573 | err_tagset: |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1574 | if (g_queue_mode == NULL_Q_MQ && shared_tags) |
Max Gurtovoy | db2d153 | 2017-07-06 18:00:07 +0300 | [diff] [blame] | 1575 | blk_mq_free_tag_set(&tag_set); |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1576 | return ret; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | static void __exit null_exit(void) |
| 1580 | { |
| 1581 | struct nullb *nullb; |
| 1582 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 1583 | configfs_unregister_subsystem(&nullb_subsys); |
| 1584 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1585 | unregister_blkdev(null_major, "nullb"); |
| 1586 | |
| 1587 | mutex_lock(&lock); |
| 1588 | while (!list_empty(&nullb_list)) { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1589 | struct nullb_device *dev; |
| 1590 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1591 | nullb = list_entry(nullb_list.next, struct nullb, list); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1592 | dev = nullb->dev; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1593 | null_del_dev(nullb); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1594 | null_free_dev(dev); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1595 | } |
| 1596 | mutex_unlock(&lock); |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1597 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1598 | if (g_queue_mode == NULL_Q_MQ && shared_tags) |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1599 | blk_mq_free_tag_set(&tag_set); |
| 1600 | |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1601 | kmem_cache_destroy(ppa_cache); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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"); |