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