Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * NVM Express device driver |
| 3 | * Copyright (c) 2011-2014, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/blkdev.h> |
| 16 | #include <linux/blk-mq.h> |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 17 | #include <linux/delay.h> |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 18 | #include <linux/errno.h> |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 19 | #include <linux/hdreg.h> |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/list_sort.h> |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 23 | #include <linux/slab.h> |
| 24 | #include <linux/types.h> |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 25 | #include <linux/pr.h> |
| 26 | #include <linux/ptrace.h> |
| 27 | #include <linux/nvme_ioctl.h> |
| 28 | #include <linux/t10-pi.h> |
| 29 | #include <scsi/sg.h> |
| 30 | #include <asm/unaligned.h> |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 31 | |
| 32 | #include "nvme.h" |
| 33 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 34 | #define NVME_MINORS (1U << MINORBITS) |
| 35 | |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 36 | unsigned char admin_timeout = 60; |
| 37 | module_param(admin_timeout, byte, 0644); |
| 38 | MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands"); |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 39 | EXPORT_SYMBOL_GPL(admin_timeout); |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 40 | |
| 41 | unsigned char nvme_io_timeout = 30; |
| 42 | module_param_named(io_timeout, nvme_io_timeout, byte, 0644); |
| 43 | MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O"); |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 44 | EXPORT_SYMBOL_GPL(nvme_io_timeout); |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 45 | |
| 46 | unsigned char shutdown_timeout = 5; |
| 47 | module_param(shutdown_timeout, byte, 0644); |
| 48 | MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown"); |
| 49 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 50 | static int nvme_major; |
| 51 | module_param(nvme_major, int, 0); |
| 52 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 53 | static int nvme_char_major; |
| 54 | module_param(nvme_char_major, int, 0); |
| 55 | |
| 56 | static LIST_HEAD(nvme_ctrl_list); |
Ming Lin | 9f2482b | 2016-02-10 10:03:31 -0800 | [diff] [blame] | 57 | static DEFINE_SPINLOCK(dev_list_lock); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 58 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 59 | static struct class *nvme_class; |
| 60 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 61 | static void nvme_free_ns(struct kref *kref) |
| 62 | { |
| 63 | struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref); |
| 64 | |
| 65 | if (ns->type == NVME_NS_LIGHTNVM) |
| 66 | nvme_nvm_unregister(ns->queue, ns->disk->disk_name); |
| 67 | |
| 68 | spin_lock(&dev_list_lock); |
| 69 | ns->disk->private_data = NULL; |
| 70 | spin_unlock(&dev_list_lock); |
| 71 | |
| 72 | nvme_put_ctrl(ns->ctrl); |
| 73 | put_disk(ns->disk); |
| 74 | kfree(ns); |
| 75 | } |
| 76 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 77 | static void nvme_put_ns(struct nvme_ns *ns) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 78 | { |
| 79 | kref_put(&ns->kref, nvme_free_ns); |
| 80 | } |
| 81 | |
| 82 | static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk) |
| 83 | { |
| 84 | struct nvme_ns *ns; |
| 85 | |
| 86 | spin_lock(&dev_list_lock); |
| 87 | ns = disk->private_data; |
Sagi Grimberg | e439bb1 | 2016-02-10 10:03:29 -0800 | [diff] [blame] | 88 | if (ns) { |
| 89 | if (!kref_get_unless_zero(&ns->kref)) |
| 90 | goto fail; |
| 91 | if (!try_module_get(ns->ctrl->ops->module)) |
| 92 | goto fail_put_ns; |
| 93 | } |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 94 | spin_unlock(&dev_list_lock); |
| 95 | |
| 96 | return ns; |
Sagi Grimberg | e439bb1 | 2016-02-10 10:03:29 -0800 | [diff] [blame] | 97 | |
| 98 | fail_put_ns: |
| 99 | kref_put(&ns->kref, nvme_free_ns); |
| 100 | fail: |
| 101 | spin_unlock(&dev_list_lock); |
| 102 | return NULL; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Christoph Hellwig | 7688faa | 2015-11-28 15:41:58 +0100 | [diff] [blame] | 105 | void nvme_requeue_req(struct request *req) |
| 106 | { |
| 107 | unsigned long flags; |
| 108 | |
| 109 | blk_mq_requeue_request(req); |
| 110 | spin_lock_irqsave(req->q->queue_lock, flags); |
| 111 | if (!blk_queue_stopped(req->q)) |
| 112 | blk_mq_kick_requeue_list(req->q); |
| 113 | spin_unlock_irqrestore(req->q->queue_lock, flags); |
| 114 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 115 | EXPORT_SYMBOL_GPL(nvme_requeue_req); |
Christoph Hellwig | 7688faa | 2015-11-28 15:41:58 +0100 | [diff] [blame] | 116 | |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 117 | struct request *nvme_alloc_request(struct request_queue *q, |
| 118 | struct nvme_command *cmd, unsigned int flags) |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 119 | { |
| 120 | bool write = cmd->common.opcode & 1; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 121 | struct request *req; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 122 | |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 123 | req = blk_mq_alloc_request(q, write, flags); |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 124 | if (IS_ERR(req)) |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 125 | return req; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 126 | |
| 127 | req->cmd_type = REQ_TYPE_DRV_PRIV; |
| 128 | req->cmd_flags |= REQ_FAILFAST_DRIVER; |
| 129 | req->__data_len = 0; |
| 130 | req->__sector = (sector_t) -1; |
| 131 | req->bio = req->biotail = NULL; |
| 132 | |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 133 | req->cmd = (unsigned char *)cmd; |
| 134 | req->cmd_len = sizeof(struct nvme_command); |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 135 | |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 136 | return req; |
| 137 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 138 | EXPORT_SYMBOL_GPL(nvme_alloc_request); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Returns 0 on success. If the result is negative, it's a Linux error code; |
| 142 | * if the result is positive, it's an NVM Express status code |
| 143 | */ |
| 144 | int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame^] | 145 | struct nvme_completion *cqe, void *buffer, unsigned bufflen, |
| 146 | unsigned timeout) |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 147 | { |
| 148 | struct request *req; |
| 149 | int ret; |
| 150 | |
| 151 | req = nvme_alloc_request(q, cmd, 0); |
| 152 | if (IS_ERR(req)) |
| 153 | return PTR_ERR(req); |
| 154 | |
| 155 | req->timeout = timeout ? timeout : ADMIN_TIMEOUT; |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame^] | 156 | req->special = cqe; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 157 | |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 158 | if (buffer && bufflen) { |
| 159 | ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL); |
| 160 | if (ret) |
| 161 | goto out; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | blk_execute_rq(req->q, NULL, req, 0); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 165 | ret = req->errors; |
| 166 | out: |
| 167 | blk_mq_free_request(req); |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, |
| 172 | void *buffer, unsigned bufflen) |
| 173 | { |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame^] | 174 | return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 175 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 176 | EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 177 | |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 178 | int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd, |
| 179 | void __user *ubuffer, unsigned bufflen, |
| 180 | void __user *meta_buffer, unsigned meta_len, u32 meta_seed, |
| 181 | u32 *result, unsigned timeout) |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 182 | { |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 183 | bool write = cmd->common.opcode & 1; |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame^] | 184 | struct nvme_completion cqe; |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 185 | struct nvme_ns *ns = q->queuedata; |
| 186 | struct gendisk *disk = ns ? ns->disk : NULL; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 187 | struct request *req; |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 188 | struct bio *bio = NULL; |
| 189 | void *meta = NULL; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 190 | int ret; |
| 191 | |
| 192 | req = nvme_alloc_request(q, cmd, 0); |
| 193 | if (IS_ERR(req)) |
| 194 | return PTR_ERR(req); |
| 195 | |
| 196 | req->timeout = timeout ? timeout : ADMIN_TIMEOUT; |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame^] | 197 | req->special = &cqe; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 198 | |
| 199 | if (ubuffer && bufflen) { |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 200 | ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen, |
| 201 | GFP_KERNEL); |
| 202 | if (ret) |
| 203 | goto out; |
| 204 | bio = req->bio; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 205 | |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 206 | if (!disk) |
| 207 | goto submit; |
| 208 | bio->bi_bdev = bdget_disk(disk, 0); |
| 209 | if (!bio->bi_bdev) { |
| 210 | ret = -ENODEV; |
| 211 | goto out_unmap; |
| 212 | } |
| 213 | |
| 214 | if (meta_buffer) { |
| 215 | struct bio_integrity_payload *bip; |
| 216 | |
| 217 | meta = kmalloc(meta_len, GFP_KERNEL); |
| 218 | if (!meta) { |
| 219 | ret = -ENOMEM; |
| 220 | goto out_unmap; |
| 221 | } |
| 222 | |
| 223 | if (write) { |
| 224 | if (copy_from_user(meta, meta_buffer, |
| 225 | meta_len)) { |
| 226 | ret = -EFAULT; |
| 227 | goto out_free_meta; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | bip = bio_integrity_alloc(bio, GFP_KERNEL, 1); |
Keith Busch | 06c1e39 | 2015-12-03 09:32:21 -0700 | [diff] [blame] | 232 | if (IS_ERR(bip)) { |
| 233 | ret = PTR_ERR(bip); |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 234 | goto out_free_meta; |
| 235 | } |
| 236 | |
| 237 | bip->bip_iter.bi_size = meta_len; |
| 238 | bip->bip_iter.bi_sector = meta_seed; |
| 239 | |
| 240 | ret = bio_integrity_add_page(bio, virt_to_page(meta), |
| 241 | meta_len, offset_in_page(meta)); |
| 242 | if (ret != meta_len) { |
| 243 | ret = -ENOMEM; |
| 244 | goto out_free_meta; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | submit: |
| 249 | blk_execute_rq(req->q, disk, req, 0); |
| 250 | ret = req->errors; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 251 | if (result) |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame^] | 252 | *result = le32_to_cpu(cqe.result); |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 253 | if (meta && !ret && !write) { |
| 254 | if (copy_to_user(meta_buffer, meta, meta_len)) |
| 255 | ret = -EFAULT; |
| 256 | } |
| 257 | out_free_meta: |
| 258 | kfree(meta); |
| 259 | out_unmap: |
| 260 | if (bio) { |
| 261 | if (disk && bio->bi_bdev) |
| 262 | bdput(bio->bi_bdev); |
| 263 | blk_rq_unmap_user(bio); |
| 264 | } |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 265 | out: |
| 266 | blk_mq_free_request(req); |
| 267 | return ret; |
| 268 | } |
| 269 | |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 270 | int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd, |
| 271 | void __user *ubuffer, unsigned bufflen, u32 *result, |
| 272 | unsigned timeout) |
| 273 | { |
| 274 | return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0, |
| 275 | result, timeout); |
| 276 | } |
| 277 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 278 | int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id) |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 279 | { |
| 280 | struct nvme_command c = { }; |
| 281 | int error; |
| 282 | |
| 283 | /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ |
| 284 | c.identify.opcode = nvme_admin_identify; |
| 285 | c.identify.cns = cpu_to_le32(1); |
| 286 | |
| 287 | *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL); |
| 288 | if (!*id) |
| 289 | return -ENOMEM; |
| 290 | |
| 291 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, |
| 292 | sizeof(struct nvme_id_ctrl)); |
| 293 | if (error) |
| 294 | kfree(*id); |
| 295 | return error; |
| 296 | } |
| 297 | |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 298 | static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list) |
| 299 | { |
| 300 | struct nvme_command c = { }; |
| 301 | |
| 302 | c.identify.opcode = nvme_admin_identify; |
| 303 | c.identify.cns = cpu_to_le32(2); |
| 304 | c.identify.nsid = cpu_to_le32(nsid); |
| 305 | return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000); |
| 306 | } |
| 307 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 308 | int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid, |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 309 | struct nvme_id_ns **id) |
| 310 | { |
| 311 | struct nvme_command c = { }; |
| 312 | int error; |
| 313 | |
| 314 | /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ |
| 315 | c.identify.opcode = nvme_admin_identify, |
| 316 | c.identify.nsid = cpu_to_le32(nsid), |
| 317 | |
| 318 | *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL); |
| 319 | if (!*id) |
| 320 | return -ENOMEM; |
| 321 | |
| 322 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, |
| 323 | sizeof(struct nvme_id_ns)); |
| 324 | if (error) |
| 325 | kfree(*id); |
| 326 | return error; |
| 327 | } |
| 328 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 329 | int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid, |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 330 | dma_addr_t dma_addr, u32 *result) |
| 331 | { |
| 332 | struct nvme_command c; |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame^] | 333 | struct nvme_completion cqe; |
| 334 | int ret; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 335 | |
| 336 | memset(&c, 0, sizeof(c)); |
| 337 | c.features.opcode = nvme_admin_get_features; |
| 338 | c.features.nsid = cpu_to_le32(nsid); |
| 339 | c.features.prp1 = cpu_to_le64(dma_addr); |
| 340 | c.features.fid = cpu_to_le32(fid); |
| 341 | |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame^] | 342 | ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &cqe, NULL, 0, 0); |
| 343 | if (ret >= 0) |
| 344 | *result = le32_to_cpu(cqe.result); |
| 345 | return ret; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 346 | } |
| 347 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 348 | int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11, |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 349 | dma_addr_t dma_addr, u32 *result) |
| 350 | { |
| 351 | struct nvme_command c; |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame^] | 352 | struct nvme_completion cqe; |
| 353 | int ret; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 354 | |
| 355 | memset(&c, 0, sizeof(c)); |
| 356 | c.features.opcode = nvme_admin_set_features; |
| 357 | c.features.prp1 = cpu_to_le64(dma_addr); |
| 358 | c.features.fid = cpu_to_le32(fid); |
| 359 | c.features.dword11 = cpu_to_le32(dword11); |
| 360 | |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame^] | 361 | ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &cqe, NULL, 0, 0); |
| 362 | if (ret >= 0) |
| 363 | *result = le32_to_cpu(cqe.result); |
| 364 | return ret; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 367 | int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log) |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 368 | { |
| 369 | struct nvme_command c = { }; |
| 370 | int error; |
| 371 | |
| 372 | c.common.opcode = nvme_admin_get_log_page, |
| 373 | c.common.nsid = cpu_to_le32(0xFFFFFFFF), |
| 374 | c.common.cdw10[0] = cpu_to_le32( |
| 375 | (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) | |
| 376 | NVME_LOG_SMART), |
| 377 | |
| 378 | *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL); |
| 379 | if (!*log) |
| 380 | return -ENOMEM; |
| 381 | |
| 382 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *log, |
| 383 | sizeof(struct nvme_smart_log)); |
| 384 | if (error) |
| 385 | kfree(*log); |
| 386 | return error; |
| 387 | } |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 388 | |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 389 | int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count) |
| 390 | { |
| 391 | u32 q_count = (*count - 1) | ((*count - 1) << 16); |
| 392 | u32 result; |
| 393 | int status, nr_io_queues; |
| 394 | |
| 395 | status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, 0, |
| 396 | &result); |
| 397 | if (status) |
| 398 | return status; |
| 399 | |
| 400 | nr_io_queues = min(result & 0xffff, result >> 16) + 1; |
| 401 | *count = min(*count, nr_io_queues); |
| 402 | return 0; |
| 403 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 404 | EXPORT_SYMBOL_GPL(nvme_set_queue_count); |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 405 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 406 | static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) |
| 407 | { |
| 408 | struct nvme_user_io io; |
| 409 | struct nvme_command c; |
| 410 | unsigned length, meta_len; |
| 411 | void __user *metadata; |
| 412 | |
| 413 | if (copy_from_user(&io, uio, sizeof(io))) |
| 414 | return -EFAULT; |
| 415 | |
| 416 | switch (io.opcode) { |
| 417 | case nvme_cmd_write: |
| 418 | case nvme_cmd_read: |
| 419 | case nvme_cmd_compare: |
| 420 | break; |
| 421 | default: |
| 422 | return -EINVAL; |
| 423 | } |
| 424 | |
| 425 | length = (io.nblocks + 1) << ns->lba_shift; |
| 426 | meta_len = (io.nblocks + 1) * ns->ms; |
| 427 | metadata = (void __user *)(uintptr_t)io.metadata; |
| 428 | |
| 429 | if (ns->ext) { |
| 430 | length += meta_len; |
| 431 | meta_len = 0; |
| 432 | } else if (meta_len) { |
| 433 | if ((io.metadata & 3) || !io.metadata) |
| 434 | return -EINVAL; |
| 435 | } |
| 436 | |
| 437 | memset(&c, 0, sizeof(c)); |
| 438 | c.rw.opcode = io.opcode; |
| 439 | c.rw.flags = io.flags; |
| 440 | c.rw.nsid = cpu_to_le32(ns->ns_id); |
| 441 | c.rw.slba = cpu_to_le64(io.slba); |
| 442 | c.rw.length = cpu_to_le16(io.nblocks); |
| 443 | c.rw.control = cpu_to_le16(io.control); |
| 444 | c.rw.dsmgmt = cpu_to_le32(io.dsmgmt); |
| 445 | c.rw.reftag = cpu_to_le32(io.reftag); |
| 446 | c.rw.apptag = cpu_to_le16(io.apptag); |
| 447 | c.rw.appmask = cpu_to_le16(io.appmask); |
| 448 | |
| 449 | return __nvme_submit_user_cmd(ns->queue, &c, |
| 450 | (void __user *)(uintptr_t)io.addr, length, |
| 451 | metadata, meta_len, io.slba, NULL, 0); |
| 452 | } |
| 453 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 454 | static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns, |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 455 | struct nvme_passthru_cmd __user *ucmd) |
| 456 | { |
| 457 | struct nvme_passthru_cmd cmd; |
| 458 | struct nvme_command c; |
| 459 | unsigned timeout = 0; |
| 460 | int status; |
| 461 | |
| 462 | if (!capable(CAP_SYS_ADMIN)) |
| 463 | return -EACCES; |
| 464 | if (copy_from_user(&cmd, ucmd, sizeof(cmd))) |
| 465 | return -EFAULT; |
| 466 | |
| 467 | memset(&c, 0, sizeof(c)); |
| 468 | c.common.opcode = cmd.opcode; |
| 469 | c.common.flags = cmd.flags; |
| 470 | c.common.nsid = cpu_to_le32(cmd.nsid); |
| 471 | c.common.cdw2[0] = cpu_to_le32(cmd.cdw2); |
| 472 | c.common.cdw2[1] = cpu_to_le32(cmd.cdw3); |
| 473 | c.common.cdw10[0] = cpu_to_le32(cmd.cdw10); |
| 474 | c.common.cdw10[1] = cpu_to_le32(cmd.cdw11); |
| 475 | c.common.cdw10[2] = cpu_to_le32(cmd.cdw12); |
| 476 | c.common.cdw10[3] = cpu_to_le32(cmd.cdw13); |
| 477 | c.common.cdw10[4] = cpu_to_le32(cmd.cdw14); |
| 478 | c.common.cdw10[5] = cpu_to_le32(cmd.cdw15); |
| 479 | |
| 480 | if (cmd.timeout_ms) |
| 481 | timeout = msecs_to_jiffies(cmd.timeout_ms); |
| 482 | |
| 483 | status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c, |
Arnd Bergmann | d1ea7be | 2015-12-08 16:22:17 +0100 | [diff] [blame] | 484 | (void __user *)(uintptr_t)cmd.addr, cmd.data_len, |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 485 | &cmd.result, timeout); |
| 486 | if (status >= 0) { |
| 487 | if (put_user(cmd.result, &ucmd->result)) |
| 488 | return -EFAULT; |
| 489 | } |
| 490 | |
| 491 | return status; |
| 492 | } |
| 493 | |
| 494 | static int nvme_ioctl(struct block_device *bdev, fmode_t mode, |
| 495 | unsigned int cmd, unsigned long arg) |
| 496 | { |
| 497 | struct nvme_ns *ns = bdev->bd_disk->private_data; |
| 498 | |
| 499 | switch (cmd) { |
| 500 | case NVME_IOCTL_ID: |
| 501 | force_successful_syscall_return(); |
| 502 | return ns->ns_id; |
| 503 | case NVME_IOCTL_ADMIN_CMD: |
| 504 | return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg); |
| 505 | case NVME_IOCTL_IO_CMD: |
| 506 | return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg); |
| 507 | case NVME_IOCTL_SUBMIT_IO: |
| 508 | return nvme_submit_io(ns, (void __user *)arg); |
Christoph Hellwig | 4490733 | 2015-12-24 15:27:02 +0100 | [diff] [blame] | 509 | #ifdef CONFIG_BLK_DEV_NVME_SCSI |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 510 | case SG_GET_VERSION_NUM: |
| 511 | return nvme_sg_get_version_num((void __user *)arg); |
| 512 | case SG_IO: |
| 513 | return nvme_sg_io(ns, (void __user *)arg); |
Christoph Hellwig | 4490733 | 2015-12-24 15:27:02 +0100 | [diff] [blame] | 514 | #endif |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 515 | default: |
| 516 | return -ENOTTY; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | #ifdef CONFIG_COMPAT |
| 521 | static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 522 | unsigned int cmd, unsigned long arg) |
| 523 | { |
| 524 | switch (cmd) { |
| 525 | case SG_IO: |
| 526 | return -ENOIOCTLCMD; |
| 527 | } |
| 528 | return nvme_ioctl(bdev, mode, cmd, arg); |
| 529 | } |
| 530 | #else |
| 531 | #define nvme_compat_ioctl NULL |
| 532 | #endif |
| 533 | |
| 534 | static int nvme_open(struct block_device *bdev, fmode_t mode) |
| 535 | { |
| 536 | return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO; |
| 537 | } |
| 538 | |
| 539 | static void nvme_release(struct gendisk *disk, fmode_t mode) |
| 540 | { |
Sagi Grimberg | e439bb1 | 2016-02-10 10:03:29 -0800 | [diff] [blame] | 541 | struct nvme_ns *ns = disk->private_data; |
| 542 | |
| 543 | module_put(ns->ctrl->ops->module); |
| 544 | nvme_put_ns(ns); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 548 | { |
| 549 | /* some standard values */ |
| 550 | geo->heads = 1 << 6; |
| 551 | geo->sectors = 1 << 5; |
| 552 | geo->cylinders = get_capacity(bdev->bd_disk) >> 11; |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
| 557 | static void nvme_init_integrity(struct nvme_ns *ns) |
| 558 | { |
| 559 | struct blk_integrity integrity; |
| 560 | |
| 561 | switch (ns->pi_type) { |
| 562 | case NVME_NS_DPS_PI_TYPE3: |
| 563 | integrity.profile = &t10_pi_type3_crc; |
| 564 | break; |
| 565 | case NVME_NS_DPS_PI_TYPE1: |
| 566 | case NVME_NS_DPS_PI_TYPE2: |
| 567 | integrity.profile = &t10_pi_type1_crc; |
| 568 | break; |
| 569 | default: |
| 570 | integrity.profile = NULL; |
| 571 | break; |
| 572 | } |
| 573 | integrity.tuple_size = ns->ms; |
| 574 | blk_integrity_register(ns->disk, &integrity); |
| 575 | blk_queue_max_integrity_segments(ns->queue, 1); |
| 576 | } |
| 577 | #else |
| 578 | static void nvme_init_integrity(struct nvme_ns *ns) |
| 579 | { |
| 580 | } |
| 581 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ |
| 582 | |
| 583 | static void nvme_config_discard(struct nvme_ns *ns) |
| 584 | { |
| 585 | u32 logical_block_size = queue_logical_block_size(ns->queue); |
| 586 | ns->queue->limits.discard_zeroes_data = 0; |
| 587 | ns->queue->limits.discard_alignment = logical_block_size; |
| 588 | ns->queue->limits.discard_granularity = logical_block_size; |
| 589 | blk_queue_max_discard_sectors(ns->queue, 0xffffffff); |
| 590 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue); |
| 591 | } |
| 592 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 593 | static int nvme_revalidate_disk(struct gendisk *disk) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 594 | { |
| 595 | struct nvme_ns *ns = disk->private_data; |
| 596 | struct nvme_id_ns *id; |
| 597 | u8 lbaf, pi_type; |
| 598 | u16 old_ms; |
| 599 | unsigned short bs; |
| 600 | |
| 601 | if (nvme_identify_ns(ns->ctrl, ns->ns_id, &id)) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 602 | dev_warn(disk_to_dev(ns->disk), "%s: Identify failure\n", |
| 603 | __func__); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 604 | return -ENODEV; |
| 605 | } |
| 606 | if (id->ncap == 0) { |
| 607 | kfree(id); |
| 608 | return -ENODEV; |
| 609 | } |
| 610 | |
| 611 | if (nvme_nvm_ns_supported(ns, id) && ns->type != NVME_NS_LIGHTNVM) { |
| 612 | if (nvme_nvm_register(ns->queue, disk->disk_name)) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 613 | dev_warn(disk_to_dev(ns->disk), |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 614 | "%s: LightNVM init failure\n", __func__); |
| 615 | kfree(id); |
| 616 | return -ENODEV; |
| 617 | } |
| 618 | ns->type = NVME_NS_LIGHTNVM; |
| 619 | } |
| 620 | |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 621 | if (ns->ctrl->vs >= NVME_VS(1, 1)) |
| 622 | memcpy(ns->eui, id->eui64, sizeof(ns->eui)); |
| 623 | if (ns->ctrl->vs >= NVME_VS(1, 2)) |
| 624 | memcpy(ns->uuid, id->nguid, sizeof(ns->uuid)); |
| 625 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 626 | old_ms = ns->ms; |
| 627 | lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK; |
| 628 | ns->lba_shift = id->lbaf[lbaf].ds; |
| 629 | ns->ms = le16_to_cpu(id->lbaf[lbaf].ms); |
| 630 | ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT); |
| 631 | |
| 632 | /* |
| 633 | * If identify namespace failed, use default 512 byte block size so |
| 634 | * block layer can use before failing read/write for 0 capacity. |
| 635 | */ |
| 636 | if (ns->lba_shift == 0) |
| 637 | ns->lba_shift = 9; |
| 638 | bs = 1 << ns->lba_shift; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 639 | /* XXX: PI implementation requires metadata equal t10 pi tuple size */ |
| 640 | pi_type = ns->ms == sizeof(struct t10_pi_tuple) ? |
| 641 | id->dps & NVME_NS_DPS_PI_MASK : 0; |
| 642 | |
| 643 | blk_mq_freeze_queue(disk->queue); |
| 644 | if (blk_get_integrity(disk) && (ns->pi_type != pi_type || |
| 645 | ns->ms != old_ms || |
| 646 | bs != queue_logical_block_size(disk->queue) || |
| 647 | (ns->ms && ns->ext))) |
| 648 | blk_integrity_unregister(disk); |
| 649 | |
| 650 | ns->pi_type = pi_type; |
| 651 | blk_queue_logical_block_size(ns->queue, bs); |
| 652 | |
Keith Busch | 4b9d5b1 | 2015-11-20 09:13:30 +0100 | [diff] [blame] | 653 | if (ns->ms && !blk_get_integrity(disk) && !ns->ext) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 654 | nvme_init_integrity(ns); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 655 | if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk)) |
| 656 | set_capacity(disk, 0); |
| 657 | else |
| 658 | set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); |
| 659 | |
| 660 | if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM) |
| 661 | nvme_config_discard(ns); |
| 662 | blk_mq_unfreeze_queue(disk->queue); |
| 663 | |
| 664 | kfree(id); |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | static char nvme_pr_type(enum pr_type type) |
| 669 | { |
| 670 | switch (type) { |
| 671 | case PR_WRITE_EXCLUSIVE: |
| 672 | return 1; |
| 673 | case PR_EXCLUSIVE_ACCESS: |
| 674 | return 2; |
| 675 | case PR_WRITE_EXCLUSIVE_REG_ONLY: |
| 676 | return 3; |
| 677 | case PR_EXCLUSIVE_ACCESS_REG_ONLY: |
| 678 | return 4; |
| 679 | case PR_WRITE_EXCLUSIVE_ALL_REGS: |
| 680 | return 5; |
| 681 | case PR_EXCLUSIVE_ACCESS_ALL_REGS: |
| 682 | return 6; |
| 683 | default: |
| 684 | return 0; |
| 685 | } |
| 686 | }; |
| 687 | |
| 688 | static int nvme_pr_command(struct block_device *bdev, u32 cdw10, |
| 689 | u64 key, u64 sa_key, u8 op) |
| 690 | { |
| 691 | struct nvme_ns *ns = bdev->bd_disk->private_data; |
| 692 | struct nvme_command c; |
| 693 | u8 data[16] = { 0, }; |
| 694 | |
| 695 | put_unaligned_le64(key, &data[0]); |
| 696 | put_unaligned_le64(sa_key, &data[8]); |
| 697 | |
| 698 | memset(&c, 0, sizeof(c)); |
| 699 | c.common.opcode = op; |
| 700 | c.common.nsid = cpu_to_le32(ns->ns_id); |
| 701 | c.common.cdw10[0] = cpu_to_le32(cdw10); |
| 702 | |
| 703 | return nvme_submit_sync_cmd(ns->queue, &c, data, 16); |
| 704 | } |
| 705 | |
| 706 | static int nvme_pr_register(struct block_device *bdev, u64 old, |
| 707 | u64 new, unsigned flags) |
| 708 | { |
| 709 | u32 cdw10; |
| 710 | |
| 711 | if (flags & ~PR_FL_IGNORE_KEY) |
| 712 | return -EOPNOTSUPP; |
| 713 | |
| 714 | cdw10 = old ? 2 : 0; |
| 715 | cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0; |
| 716 | cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */ |
| 717 | return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register); |
| 718 | } |
| 719 | |
| 720 | static int nvme_pr_reserve(struct block_device *bdev, u64 key, |
| 721 | enum pr_type type, unsigned flags) |
| 722 | { |
| 723 | u32 cdw10; |
| 724 | |
| 725 | if (flags & ~PR_FL_IGNORE_KEY) |
| 726 | return -EOPNOTSUPP; |
| 727 | |
| 728 | cdw10 = nvme_pr_type(type) << 8; |
| 729 | cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0); |
| 730 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire); |
| 731 | } |
| 732 | |
| 733 | static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new, |
| 734 | enum pr_type type, bool abort) |
| 735 | { |
| 736 | u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1; |
| 737 | return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire); |
| 738 | } |
| 739 | |
| 740 | static int nvme_pr_clear(struct block_device *bdev, u64 key) |
| 741 | { |
Dan Carpenter | 8c0b391 | 2015-12-09 13:24:06 +0300 | [diff] [blame] | 742 | u32 cdw10 = 1 | (key ? 1 << 3 : 0); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 743 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register); |
| 744 | } |
| 745 | |
| 746 | static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type) |
| 747 | { |
| 748 | u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0; |
| 749 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release); |
| 750 | } |
| 751 | |
| 752 | static const struct pr_ops nvme_pr_ops = { |
| 753 | .pr_register = nvme_pr_register, |
| 754 | .pr_reserve = nvme_pr_reserve, |
| 755 | .pr_release = nvme_pr_release, |
| 756 | .pr_preempt = nvme_pr_preempt, |
| 757 | .pr_clear = nvme_pr_clear, |
| 758 | }; |
| 759 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 760 | static const struct block_device_operations nvme_fops = { |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 761 | .owner = THIS_MODULE, |
| 762 | .ioctl = nvme_ioctl, |
| 763 | .compat_ioctl = nvme_compat_ioctl, |
| 764 | .open = nvme_open, |
| 765 | .release = nvme_release, |
| 766 | .getgeo = nvme_getgeo, |
| 767 | .revalidate_disk= nvme_revalidate_disk, |
| 768 | .pr_ops = &nvme_pr_ops, |
| 769 | }; |
| 770 | |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 771 | static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled) |
| 772 | { |
| 773 | unsigned long timeout = |
| 774 | ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies; |
| 775 | u32 csts, bit = enabled ? NVME_CSTS_RDY : 0; |
| 776 | int ret; |
| 777 | |
| 778 | while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { |
| 779 | if ((csts & NVME_CSTS_RDY) == bit) |
| 780 | break; |
| 781 | |
| 782 | msleep(100); |
| 783 | if (fatal_signal_pending(current)) |
| 784 | return -EINTR; |
| 785 | if (time_after(jiffies, timeout)) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 786 | dev_err(ctrl->device, |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 787 | "Device not ready; aborting %s\n", enabled ? |
| 788 | "initialisation" : "reset"); |
| 789 | return -ENODEV; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | return ret; |
| 794 | } |
| 795 | |
| 796 | /* |
| 797 | * If the device has been passed off to us in an enabled state, just clear |
| 798 | * the enabled bit. The spec says we should set the 'shutdown notification |
| 799 | * bits', but doing so may cause the device to complete commands to the |
| 800 | * admin queue ... and we don't know what memory that might be pointing at! |
| 801 | */ |
| 802 | int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap) |
| 803 | { |
| 804 | int ret; |
| 805 | |
| 806 | ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; |
| 807 | ctrl->ctrl_config &= ~NVME_CC_ENABLE; |
| 808 | |
| 809 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); |
| 810 | if (ret) |
| 811 | return ret; |
| 812 | return nvme_wait_ready(ctrl, cap, false); |
| 813 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 814 | EXPORT_SYMBOL_GPL(nvme_disable_ctrl); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 815 | |
| 816 | int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap) |
| 817 | { |
| 818 | /* |
| 819 | * Default to a 4K page size, with the intention to update this |
| 820 | * path in the future to accomodate architectures with differing |
| 821 | * kernel and IO page sizes. |
| 822 | */ |
| 823 | unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12; |
| 824 | int ret; |
| 825 | |
| 826 | if (page_shift < dev_page_min) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 827 | dev_err(ctrl->device, |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 828 | "Minimum device page size %u too large for host (%u)\n", |
| 829 | 1 << dev_page_min, 1 << page_shift); |
| 830 | return -ENODEV; |
| 831 | } |
| 832 | |
| 833 | ctrl->page_size = 1 << page_shift; |
| 834 | |
| 835 | ctrl->ctrl_config = NVME_CC_CSS_NVM; |
| 836 | ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT; |
| 837 | ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE; |
| 838 | ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; |
| 839 | ctrl->ctrl_config |= NVME_CC_ENABLE; |
| 840 | |
| 841 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); |
| 842 | if (ret) |
| 843 | return ret; |
| 844 | return nvme_wait_ready(ctrl, cap, true); |
| 845 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 846 | EXPORT_SYMBOL_GPL(nvme_enable_ctrl); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 847 | |
| 848 | int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl) |
| 849 | { |
| 850 | unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies; |
| 851 | u32 csts; |
| 852 | int ret; |
| 853 | |
| 854 | ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; |
| 855 | ctrl->ctrl_config |= NVME_CC_SHN_NORMAL; |
| 856 | |
| 857 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); |
| 858 | if (ret) |
| 859 | return ret; |
| 860 | |
| 861 | while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { |
| 862 | if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT) |
| 863 | break; |
| 864 | |
| 865 | msleep(100); |
| 866 | if (fatal_signal_pending(current)) |
| 867 | return -EINTR; |
| 868 | if (time_after(jiffies, timeout)) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 869 | dev_err(ctrl->device, |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 870 | "Device shutdown incomplete; abort shutdown\n"); |
| 871 | return -ENODEV; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | return ret; |
| 876 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 877 | EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 878 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 879 | /* |
| 880 | * Initialize the cached copies of the Identify data and various controller |
| 881 | * register in our nvme_ctrl structure. This should be called as soon as |
| 882 | * the admin queue is fully up and running. |
| 883 | */ |
| 884 | int nvme_init_identify(struct nvme_ctrl *ctrl) |
| 885 | { |
| 886 | struct nvme_id_ctrl *id; |
| 887 | u64 cap; |
| 888 | int ret, page_shift; |
| 889 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 890 | ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs); |
| 891 | if (ret) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 892 | dev_err(ctrl->device, "Reading VS failed (%d)\n", ret); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 893 | return ret; |
| 894 | } |
| 895 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 896 | ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap); |
| 897 | if (ret) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 898 | dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 899 | return ret; |
| 900 | } |
| 901 | page_shift = NVME_CAP_MPSMIN(cap) + 12; |
| 902 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 903 | if (ctrl->vs >= NVME_VS(1, 1)) |
| 904 | ctrl->subsystem = NVME_CAP_NSSRC(cap); |
| 905 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 906 | ret = nvme_identify_ctrl(ctrl, &id); |
| 907 | if (ret) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 908 | dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 909 | return -EIO; |
| 910 | } |
| 911 | |
| 912 | ctrl->oncs = le16_to_cpup(&id->oncs); |
Christoph Hellwig | 6bf25d1 | 2015-11-20 09:36:44 +0100 | [diff] [blame] | 913 | atomic_set(&ctrl->abort_limit, id->acl + 1); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 914 | ctrl->vwc = id->vwc; |
| 915 | memcpy(ctrl->serial, id->sn, sizeof(id->sn)); |
| 916 | memcpy(ctrl->model, id->mn, sizeof(id->mn)); |
| 917 | memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr)); |
| 918 | if (id->mdts) |
| 919 | ctrl->max_hw_sectors = 1 << (id->mdts + page_shift - 9); |
| 920 | else |
| 921 | ctrl->max_hw_sectors = UINT_MAX; |
| 922 | |
| 923 | if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) && id->vs[3]) { |
| 924 | unsigned int max_hw_sectors; |
| 925 | |
| 926 | ctrl->stripe_size = 1 << (id->vs[3] + page_shift); |
| 927 | max_hw_sectors = ctrl->stripe_size >> (page_shift - 9); |
| 928 | if (ctrl->max_hw_sectors) { |
| 929 | ctrl->max_hw_sectors = min(max_hw_sectors, |
| 930 | ctrl->max_hw_sectors); |
| 931 | } else { |
| 932 | ctrl->max_hw_sectors = max_hw_sectors; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | kfree(id); |
| 937 | return 0; |
| 938 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 939 | EXPORT_SYMBOL_GPL(nvme_init_identify); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 940 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 941 | static int nvme_dev_open(struct inode *inode, struct file *file) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 942 | { |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 943 | struct nvme_ctrl *ctrl; |
| 944 | int instance = iminor(inode); |
| 945 | int ret = -ENODEV; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 946 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 947 | spin_lock(&dev_list_lock); |
| 948 | list_for_each_entry(ctrl, &nvme_ctrl_list, node) { |
| 949 | if (ctrl->instance != instance) |
| 950 | continue; |
| 951 | |
| 952 | if (!ctrl->admin_q) { |
| 953 | ret = -EWOULDBLOCK; |
| 954 | break; |
| 955 | } |
| 956 | if (!kref_get_unless_zero(&ctrl->kref)) |
| 957 | break; |
| 958 | file->private_data = ctrl; |
| 959 | ret = 0; |
| 960 | break; |
| 961 | } |
| 962 | spin_unlock(&dev_list_lock); |
| 963 | |
| 964 | return ret; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 965 | } |
| 966 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 967 | static int nvme_dev_release(struct inode *inode, struct file *file) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 968 | { |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 969 | nvme_put_ctrl(file->private_data); |
| 970 | return 0; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 971 | } |
| 972 | |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 973 | static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp) |
| 974 | { |
| 975 | struct nvme_ns *ns; |
| 976 | int ret; |
| 977 | |
| 978 | mutex_lock(&ctrl->namespaces_mutex); |
| 979 | if (list_empty(&ctrl->namespaces)) { |
| 980 | ret = -ENOTTY; |
| 981 | goto out_unlock; |
| 982 | } |
| 983 | |
| 984 | ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list); |
| 985 | if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 986 | dev_warn(ctrl->device, |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 987 | "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n"); |
| 988 | ret = -EINVAL; |
| 989 | goto out_unlock; |
| 990 | } |
| 991 | |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 992 | dev_warn(ctrl->device, |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 993 | "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n"); |
| 994 | kref_get(&ns->kref); |
| 995 | mutex_unlock(&ctrl->namespaces_mutex); |
| 996 | |
| 997 | ret = nvme_user_cmd(ctrl, ns, argp); |
| 998 | nvme_put_ns(ns); |
| 999 | return ret; |
| 1000 | |
| 1001 | out_unlock: |
| 1002 | mutex_unlock(&ctrl->namespaces_mutex); |
| 1003 | return ret; |
| 1004 | } |
| 1005 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1006 | static long nvme_dev_ioctl(struct file *file, unsigned int cmd, |
| 1007 | unsigned long arg) |
| 1008 | { |
| 1009 | struct nvme_ctrl *ctrl = file->private_data; |
| 1010 | void __user *argp = (void __user *)arg; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1011 | |
| 1012 | switch (cmd) { |
| 1013 | case NVME_IOCTL_ADMIN_CMD: |
| 1014 | return nvme_user_cmd(ctrl, NULL, argp); |
| 1015 | case NVME_IOCTL_IO_CMD: |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 1016 | return nvme_dev_user_cmd(ctrl, argp); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1017 | case NVME_IOCTL_RESET: |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1018 | dev_warn(ctrl->device, "resetting controller\n"); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1019 | return ctrl->ops->reset_ctrl(ctrl); |
| 1020 | case NVME_IOCTL_SUBSYS_RESET: |
| 1021 | return nvme_reset_subsystem(ctrl); |
| 1022 | default: |
| 1023 | return -ENOTTY; |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | static const struct file_operations nvme_dev_fops = { |
| 1028 | .owner = THIS_MODULE, |
| 1029 | .open = nvme_dev_open, |
| 1030 | .release = nvme_dev_release, |
| 1031 | .unlocked_ioctl = nvme_dev_ioctl, |
| 1032 | .compat_ioctl = nvme_dev_ioctl, |
| 1033 | }; |
| 1034 | |
| 1035 | static ssize_t nvme_sysfs_reset(struct device *dev, |
| 1036 | struct device_attribute *attr, const char *buf, |
| 1037 | size_t count) |
| 1038 | { |
| 1039 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 1040 | int ret; |
| 1041 | |
| 1042 | ret = ctrl->ops->reset_ctrl(ctrl); |
| 1043 | if (ret < 0) |
| 1044 | return ret; |
| 1045 | return count; |
| 1046 | } |
| 1047 | static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset); |
| 1048 | |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 1049 | static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, |
| 1050 | char *buf) |
| 1051 | { |
| 1052 | struct nvme_ns *ns = dev_to_disk(dev)->private_data; |
| 1053 | return sprintf(buf, "%pU\n", ns->uuid); |
| 1054 | } |
| 1055 | static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL); |
| 1056 | |
| 1057 | static ssize_t eui_show(struct device *dev, struct device_attribute *attr, |
| 1058 | char *buf) |
| 1059 | { |
| 1060 | struct nvme_ns *ns = dev_to_disk(dev)->private_data; |
| 1061 | return sprintf(buf, "%8phd\n", ns->eui); |
| 1062 | } |
| 1063 | static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL); |
| 1064 | |
| 1065 | static ssize_t nsid_show(struct device *dev, struct device_attribute *attr, |
| 1066 | char *buf) |
| 1067 | { |
| 1068 | struct nvme_ns *ns = dev_to_disk(dev)->private_data; |
| 1069 | return sprintf(buf, "%d\n", ns->ns_id); |
| 1070 | } |
| 1071 | static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL); |
| 1072 | |
| 1073 | static struct attribute *nvme_ns_attrs[] = { |
| 1074 | &dev_attr_uuid.attr, |
| 1075 | &dev_attr_eui.attr, |
| 1076 | &dev_attr_nsid.attr, |
| 1077 | NULL, |
| 1078 | }; |
| 1079 | |
| 1080 | static umode_t nvme_attrs_are_visible(struct kobject *kobj, |
| 1081 | struct attribute *a, int n) |
| 1082 | { |
| 1083 | struct device *dev = container_of(kobj, struct device, kobj); |
| 1084 | struct nvme_ns *ns = dev_to_disk(dev)->private_data; |
| 1085 | |
| 1086 | if (a == &dev_attr_uuid.attr) { |
| 1087 | if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid))) |
| 1088 | return 0; |
| 1089 | } |
| 1090 | if (a == &dev_attr_eui.attr) { |
| 1091 | if (!memchr_inv(ns->eui, 0, sizeof(ns->eui))) |
| 1092 | return 0; |
| 1093 | } |
| 1094 | return a->mode; |
| 1095 | } |
| 1096 | |
| 1097 | static const struct attribute_group nvme_ns_attr_group = { |
| 1098 | .attrs = nvme_ns_attrs, |
| 1099 | .is_visible = nvme_attrs_are_visible, |
| 1100 | }; |
| 1101 | |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 1102 | #define nvme_show_function(field) \ |
| 1103 | static ssize_t field##_show(struct device *dev, \ |
| 1104 | struct device_attribute *attr, char *buf) \ |
| 1105 | { \ |
| 1106 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ |
| 1107 | return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \ |
| 1108 | } \ |
| 1109 | static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); |
| 1110 | |
| 1111 | nvme_show_function(model); |
| 1112 | nvme_show_function(serial); |
| 1113 | nvme_show_function(firmware_rev); |
| 1114 | |
| 1115 | static struct attribute *nvme_dev_attrs[] = { |
| 1116 | &dev_attr_reset_controller.attr, |
| 1117 | &dev_attr_model.attr, |
| 1118 | &dev_attr_serial.attr, |
| 1119 | &dev_attr_firmware_rev.attr, |
| 1120 | NULL |
| 1121 | }; |
| 1122 | |
| 1123 | static struct attribute_group nvme_dev_attrs_group = { |
| 1124 | .attrs = nvme_dev_attrs, |
| 1125 | }; |
| 1126 | |
| 1127 | static const struct attribute_group *nvme_dev_attr_groups[] = { |
| 1128 | &nvme_dev_attrs_group, |
| 1129 | NULL, |
| 1130 | }; |
| 1131 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1132 | static int ns_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1133 | { |
| 1134 | struct nvme_ns *nsa = container_of(a, struct nvme_ns, list); |
| 1135 | struct nvme_ns *nsb = container_of(b, struct nvme_ns, list); |
| 1136 | |
| 1137 | return nsa->ns_id - nsb->ns_id; |
| 1138 | } |
| 1139 | |
| 1140 | static struct nvme_ns *nvme_find_ns(struct nvme_ctrl *ctrl, unsigned nsid) |
| 1141 | { |
| 1142 | struct nvme_ns *ns; |
| 1143 | |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 1144 | lockdep_assert_held(&ctrl->namespaces_mutex); |
| 1145 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1146 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
| 1147 | if (ns->ns_id == nsid) |
| 1148 | return ns; |
| 1149 | if (ns->ns_id > nsid) |
| 1150 | break; |
| 1151 | } |
| 1152 | return NULL; |
| 1153 | } |
| 1154 | |
| 1155 | static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid) |
| 1156 | { |
| 1157 | struct nvme_ns *ns; |
| 1158 | struct gendisk *disk; |
| 1159 | int node = dev_to_node(ctrl->dev); |
| 1160 | |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 1161 | lockdep_assert_held(&ctrl->namespaces_mutex); |
| 1162 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1163 | ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node); |
| 1164 | if (!ns) |
| 1165 | return; |
| 1166 | |
| 1167 | ns->queue = blk_mq_init_queue(ctrl->tagset); |
| 1168 | if (IS_ERR(ns->queue)) |
| 1169 | goto out_free_ns; |
| 1170 | queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue); |
| 1171 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue); |
| 1172 | ns->queue->queuedata = ns; |
| 1173 | ns->ctrl = ctrl; |
| 1174 | |
| 1175 | disk = alloc_disk_node(0, node); |
| 1176 | if (!disk) |
| 1177 | goto out_free_queue; |
| 1178 | |
| 1179 | kref_init(&ns->kref); |
| 1180 | ns->ns_id = nsid; |
| 1181 | ns->disk = disk; |
| 1182 | ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */ |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1183 | |
| 1184 | blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift); |
| 1185 | if (ctrl->max_hw_sectors) { |
| 1186 | blk_queue_max_hw_sectors(ns->queue, ctrl->max_hw_sectors); |
| 1187 | blk_queue_max_segments(ns->queue, |
| 1188 | (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1); |
| 1189 | } |
| 1190 | if (ctrl->stripe_size) |
| 1191 | blk_queue_chunk_sectors(ns->queue, ctrl->stripe_size >> 9); |
| 1192 | if (ctrl->vwc & NVME_CTRL_VWC_PRESENT) |
| 1193 | blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA); |
| 1194 | blk_queue_virt_boundary(ns->queue, ctrl->page_size - 1); |
| 1195 | |
| 1196 | disk->major = nvme_major; |
| 1197 | disk->first_minor = 0; |
| 1198 | disk->fops = &nvme_fops; |
| 1199 | disk->private_data = ns; |
| 1200 | disk->queue = ns->queue; |
| 1201 | disk->driverfs_dev = ctrl->device; |
| 1202 | disk->flags = GENHD_FL_EXT_DEVT; |
| 1203 | sprintf(disk->disk_name, "nvme%dn%d", ctrl->instance, nsid); |
| 1204 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1205 | if (nvme_revalidate_disk(ns->disk)) |
| 1206 | goto out_free_disk; |
| 1207 | |
Keith Busch | 4b9d5b1 | 2015-11-20 09:13:30 +0100 | [diff] [blame] | 1208 | list_add_tail(&ns->list, &ctrl->namespaces); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1209 | kref_get(&ctrl->kref); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 1210 | if (ns->type == NVME_NS_LIGHTNVM) |
| 1211 | return; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1212 | |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 1213 | add_disk(ns->disk); |
| 1214 | if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj, |
| 1215 | &nvme_ns_attr_group)) |
| 1216 | pr_warn("%s: failed to create sysfs group for identification\n", |
| 1217 | ns->disk->disk_name); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1218 | return; |
| 1219 | out_free_disk: |
| 1220 | kfree(disk); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1221 | out_free_queue: |
| 1222 | blk_cleanup_queue(ns->queue); |
| 1223 | out_free_ns: |
| 1224 | kfree(ns); |
| 1225 | } |
| 1226 | |
| 1227 | static void nvme_ns_remove(struct nvme_ns *ns) |
| 1228 | { |
| 1229 | bool kill = nvme_io_incapable(ns->ctrl) && |
| 1230 | !blk_queue_dying(ns->queue); |
| 1231 | |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 1232 | lockdep_assert_held(&ns->ctrl->namespaces_mutex); |
| 1233 | |
Linus Torvalds | 3e1e21c | 2016-01-21 19:58:02 -0800 | [diff] [blame] | 1234 | if (kill) { |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1235 | blk_set_queue_dying(ns->queue); |
Linus Torvalds | 3e1e21c | 2016-01-21 19:58:02 -0800 | [diff] [blame] | 1236 | |
| 1237 | /* |
| 1238 | * The controller was shutdown first if we got here through |
| 1239 | * device removal. The shutdown may requeue outstanding |
| 1240 | * requests. These need to be aborted immediately so |
| 1241 | * del_gendisk doesn't block indefinitely for their completion. |
| 1242 | */ |
| 1243 | blk_mq_abort_requeue_list(ns->queue); |
| 1244 | } |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1245 | if (ns->disk->flags & GENHD_FL_UP) { |
| 1246 | if (blk_get_integrity(ns->disk)) |
| 1247 | blk_integrity_unregister(ns->disk); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 1248 | sysfs_remove_group(&disk_to_dev(ns->disk)->kobj, |
| 1249 | &nvme_ns_attr_group); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1250 | del_gendisk(ns->disk); |
| 1251 | } |
| 1252 | if (kill || !blk_queue_dying(ns->queue)) { |
| 1253 | blk_mq_abort_requeue_list(ns->queue); |
| 1254 | blk_cleanup_queue(ns->queue); |
| 1255 | } |
| 1256 | list_del_init(&ns->list); |
| 1257 | nvme_put_ns(ns); |
| 1258 | } |
| 1259 | |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 1260 | static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid) |
| 1261 | { |
| 1262 | struct nvme_ns *ns; |
| 1263 | |
| 1264 | ns = nvme_find_ns(ctrl, nsid); |
| 1265 | if (ns) { |
| 1266 | if (revalidate_disk(ns->disk)) |
| 1267 | nvme_ns_remove(ns); |
| 1268 | } else |
| 1269 | nvme_alloc_ns(ctrl, nsid); |
| 1270 | } |
| 1271 | |
| 1272 | static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn) |
| 1273 | { |
| 1274 | struct nvme_ns *ns; |
| 1275 | __le32 *ns_list; |
| 1276 | unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024); |
| 1277 | int ret = 0; |
| 1278 | |
| 1279 | ns_list = kzalloc(0x1000, GFP_KERNEL); |
| 1280 | if (!ns_list) |
| 1281 | return -ENOMEM; |
| 1282 | |
| 1283 | for (i = 0; i < num_lists; i++) { |
| 1284 | ret = nvme_identify_ns_list(ctrl, prev, ns_list); |
| 1285 | if (ret) |
| 1286 | goto out; |
| 1287 | |
| 1288 | for (j = 0; j < min(nn, 1024U); j++) { |
| 1289 | nsid = le32_to_cpu(ns_list[j]); |
| 1290 | if (!nsid) |
| 1291 | goto out; |
| 1292 | |
| 1293 | nvme_validate_ns(ctrl, nsid); |
| 1294 | |
| 1295 | while (++prev < nsid) { |
| 1296 | ns = nvme_find_ns(ctrl, prev); |
| 1297 | if (ns) |
| 1298 | nvme_ns_remove(ns); |
| 1299 | } |
| 1300 | } |
| 1301 | nn -= j; |
| 1302 | } |
| 1303 | out: |
| 1304 | kfree(ns_list); |
| 1305 | return ret; |
| 1306 | } |
| 1307 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1308 | static void __nvme_scan_namespaces(struct nvme_ctrl *ctrl, unsigned nn) |
| 1309 | { |
| 1310 | struct nvme_ns *ns, *next; |
| 1311 | unsigned i; |
| 1312 | |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 1313 | lockdep_assert_held(&ctrl->namespaces_mutex); |
| 1314 | |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 1315 | for (i = 1; i <= nn; i++) |
| 1316 | nvme_validate_ns(ctrl, i); |
| 1317 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1318 | list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) { |
| 1319 | if (ns->ns_id > nn) |
| 1320 | nvme_ns_remove(ns); |
| 1321 | } |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | void nvme_scan_namespaces(struct nvme_ctrl *ctrl) |
| 1325 | { |
| 1326 | struct nvme_id_ctrl *id; |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 1327 | unsigned nn; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1328 | |
| 1329 | if (nvme_identify_ctrl(ctrl, &id)) |
| 1330 | return; |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 1331 | |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 1332 | mutex_lock(&ctrl->namespaces_mutex); |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 1333 | nn = le32_to_cpu(id->nn); |
| 1334 | if (ctrl->vs >= NVME_VS(1, 1) && |
| 1335 | !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) { |
| 1336 | if (!nvme_scan_ns_list(ctrl, nn)) |
| 1337 | goto done; |
| 1338 | } |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1339 | __nvme_scan_namespaces(ctrl, le32_to_cpup(&id->nn)); |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 1340 | done: |
| 1341 | list_sort(NULL, &ctrl->namespaces, ns_cmp); |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 1342 | mutex_unlock(&ctrl->namespaces_mutex); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1343 | kfree(id); |
| 1344 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1345 | EXPORT_SYMBOL_GPL(nvme_scan_namespaces); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1346 | |
| 1347 | void nvme_remove_namespaces(struct nvme_ctrl *ctrl) |
| 1348 | { |
| 1349 | struct nvme_ns *ns, *next; |
| 1350 | |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 1351 | mutex_lock(&ctrl->namespaces_mutex); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1352 | list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) |
| 1353 | nvme_ns_remove(ns); |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 1354 | mutex_unlock(&ctrl->namespaces_mutex); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1355 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1356 | EXPORT_SYMBOL_GPL(nvme_remove_namespaces); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1357 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1358 | static DEFINE_IDA(nvme_instance_ida); |
| 1359 | |
| 1360 | static int nvme_set_instance(struct nvme_ctrl *ctrl) |
| 1361 | { |
| 1362 | int instance, error; |
| 1363 | |
| 1364 | do { |
| 1365 | if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL)) |
| 1366 | return -ENODEV; |
| 1367 | |
| 1368 | spin_lock(&dev_list_lock); |
| 1369 | error = ida_get_new(&nvme_instance_ida, &instance); |
| 1370 | spin_unlock(&dev_list_lock); |
| 1371 | } while (error == -EAGAIN); |
| 1372 | |
| 1373 | if (error) |
| 1374 | return -ENODEV; |
| 1375 | |
| 1376 | ctrl->instance = instance; |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
| 1380 | static void nvme_release_instance(struct nvme_ctrl *ctrl) |
| 1381 | { |
| 1382 | spin_lock(&dev_list_lock); |
| 1383 | ida_remove(&nvme_instance_ida, ctrl->instance); |
| 1384 | spin_unlock(&dev_list_lock); |
| 1385 | } |
| 1386 | |
Keith Busch | 53029b0 | 2015-11-28 15:41:02 +0100 | [diff] [blame] | 1387 | void nvme_uninit_ctrl(struct nvme_ctrl *ctrl) |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1388 | { |
Keith Busch | 53029b0 | 2015-11-28 15:41:02 +0100 | [diff] [blame] | 1389 | device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance)); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1390 | |
| 1391 | spin_lock(&dev_list_lock); |
| 1392 | list_del(&ctrl->node); |
| 1393 | spin_unlock(&dev_list_lock); |
Keith Busch | 53029b0 | 2015-11-28 15:41:02 +0100 | [diff] [blame] | 1394 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1395 | EXPORT_SYMBOL_GPL(nvme_uninit_ctrl); |
Keith Busch | 53029b0 | 2015-11-28 15:41:02 +0100 | [diff] [blame] | 1396 | |
| 1397 | static void nvme_free_ctrl(struct kref *kref) |
| 1398 | { |
| 1399 | struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1400 | |
| 1401 | put_device(ctrl->device); |
| 1402 | nvme_release_instance(ctrl); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1403 | |
| 1404 | ctrl->ops->free_ctrl(ctrl); |
| 1405 | } |
| 1406 | |
| 1407 | void nvme_put_ctrl(struct nvme_ctrl *ctrl) |
| 1408 | { |
| 1409 | kref_put(&ctrl->kref, nvme_free_ctrl); |
| 1410 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1411 | EXPORT_SYMBOL_GPL(nvme_put_ctrl); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1412 | |
| 1413 | /* |
| 1414 | * Initialize a NVMe controller structures. This needs to be called during |
| 1415 | * earliest initialization so that we have the initialized structured around |
| 1416 | * during probing. |
| 1417 | */ |
| 1418 | int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev, |
| 1419 | const struct nvme_ctrl_ops *ops, unsigned long quirks) |
| 1420 | { |
| 1421 | int ret; |
| 1422 | |
| 1423 | INIT_LIST_HEAD(&ctrl->namespaces); |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 1424 | mutex_init(&ctrl->namespaces_mutex); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1425 | kref_init(&ctrl->kref); |
| 1426 | ctrl->dev = dev; |
| 1427 | ctrl->ops = ops; |
| 1428 | ctrl->quirks = quirks; |
| 1429 | |
| 1430 | ret = nvme_set_instance(ctrl); |
| 1431 | if (ret) |
| 1432 | goto out; |
| 1433 | |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 1434 | ctrl->device = device_create_with_groups(nvme_class, ctrl->dev, |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1435 | MKDEV(nvme_char_major, ctrl->instance), |
Christoph Hellwig | f4f0f63 | 2016-02-09 12:44:03 -0700 | [diff] [blame] | 1436 | ctrl, nvme_dev_attr_groups, |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 1437 | "nvme%d", ctrl->instance); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1438 | if (IS_ERR(ctrl->device)) { |
| 1439 | ret = PTR_ERR(ctrl->device); |
| 1440 | goto out_release_instance; |
| 1441 | } |
| 1442 | get_device(ctrl->device); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1443 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1444 | spin_lock(&dev_list_lock); |
| 1445 | list_add_tail(&ctrl->node, &nvme_ctrl_list); |
| 1446 | spin_unlock(&dev_list_lock); |
| 1447 | |
| 1448 | return 0; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1449 | out_release_instance: |
| 1450 | nvme_release_instance(ctrl); |
| 1451 | out: |
| 1452 | return ret; |
| 1453 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1454 | EXPORT_SYMBOL_GPL(nvme_init_ctrl); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1455 | |
Keith Busch | 2564626 | 2016-01-04 09:10:57 -0700 | [diff] [blame] | 1456 | void nvme_stop_queues(struct nvme_ctrl *ctrl) |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 1457 | { |
| 1458 | struct nvme_ns *ns; |
| 1459 | |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 1460 | mutex_lock(&ctrl->namespaces_mutex); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 1461 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 1462 | spin_lock_irq(ns->queue->queue_lock); |
| 1463 | queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue); |
| 1464 | spin_unlock_irq(ns->queue->queue_lock); |
| 1465 | |
| 1466 | blk_mq_cancel_requeue_work(ns->queue); |
| 1467 | blk_mq_stop_hw_queues(ns->queue); |
| 1468 | } |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 1469 | mutex_unlock(&ctrl->namespaces_mutex); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 1470 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1471 | EXPORT_SYMBOL_GPL(nvme_stop_queues); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 1472 | |
Keith Busch | 2564626 | 2016-01-04 09:10:57 -0700 | [diff] [blame] | 1473 | void nvme_start_queues(struct nvme_ctrl *ctrl) |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 1474 | { |
| 1475 | struct nvme_ns *ns; |
| 1476 | |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 1477 | mutex_lock(&ctrl->namespaces_mutex); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 1478 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
| 1479 | queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 1480 | blk_mq_start_stopped_hw_queues(ns->queue, true); |
| 1481 | blk_mq_kick_requeue_list(ns->queue); |
| 1482 | } |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 1483 | mutex_unlock(&ctrl->namespaces_mutex); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 1484 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1485 | EXPORT_SYMBOL_GPL(nvme_start_queues); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 1486 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1487 | int __init nvme_core_init(void) |
| 1488 | { |
| 1489 | int result; |
| 1490 | |
| 1491 | result = register_blkdev(nvme_major, "nvme"); |
| 1492 | if (result < 0) |
| 1493 | return result; |
| 1494 | else if (result > 0) |
| 1495 | nvme_major = result; |
| 1496 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1497 | result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme", |
| 1498 | &nvme_dev_fops); |
| 1499 | if (result < 0) |
| 1500 | goto unregister_blkdev; |
| 1501 | else if (result > 0) |
| 1502 | nvme_char_major = result; |
| 1503 | |
| 1504 | nvme_class = class_create(THIS_MODULE, "nvme"); |
| 1505 | if (IS_ERR(nvme_class)) { |
| 1506 | result = PTR_ERR(nvme_class); |
| 1507 | goto unregister_chrdev; |
| 1508 | } |
| 1509 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1510 | return 0; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1511 | |
| 1512 | unregister_chrdev: |
| 1513 | __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme"); |
| 1514 | unregister_blkdev: |
| 1515 | unregister_blkdev(nvme_major, "nvme"); |
| 1516 | return result; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | void nvme_core_exit(void) |
| 1520 | { |
| 1521 | unregister_blkdev(nvme_major, "nvme"); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1522 | class_destroy(nvme_class); |
| 1523 | __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme"); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1524 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1525 | |
| 1526 | MODULE_LICENSE("GPL"); |
| 1527 | MODULE_VERSION("1.0"); |
| 1528 | module_init(nvme_core_init); |
| 1529 | module_exit(nvme_core_exit); |