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> |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 29 | #include <linux/pm_qos.h> |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 30 | #include <scsi/sg.h> |
| 31 | #include <asm/unaligned.h> |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 32 | |
| 33 | #include "nvme.h" |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 34 | #include "fabrics.h" |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 35 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 36 | #define NVME_MINORS (1U << MINORBITS) |
| 37 | |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 38 | unsigned char admin_timeout = 60; |
| 39 | module_param(admin_timeout, byte, 0644); |
| 40 | MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands"); |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 41 | EXPORT_SYMBOL_GPL(admin_timeout); |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 42 | |
| 43 | unsigned char nvme_io_timeout = 30; |
| 44 | module_param_named(io_timeout, nvme_io_timeout, byte, 0644); |
| 45 | MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O"); |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 46 | EXPORT_SYMBOL_GPL(nvme_io_timeout); |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 47 | |
| 48 | unsigned char shutdown_timeout = 5; |
| 49 | module_param(shutdown_timeout, byte, 0644); |
| 50 | MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown"); |
| 51 | |
Christoph Hellwig | 44e44b2 | 2017-04-05 19:18:11 +0200 | [diff] [blame] | 52 | static u8 nvme_max_retries = 5; |
| 53 | module_param_named(max_retries, nvme_max_retries, byte, 0644); |
Keith Busch | f80ec96 | 2016-07-12 16:20:31 -0700 | [diff] [blame] | 54 | MODULE_PARM_DESC(max_retries, "max number of retries a command may have"); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 55 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 56 | static int nvme_char_major; |
| 57 | module_param(nvme_char_major, int, 0); |
| 58 | |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 59 | static unsigned long default_ps_max_latency_us = 25000; |
| 60 | module_param(default_ps_max_latency_us, ulong, 0644); |
| 61 | MODULE_PARM_DESC(default_ps_max_latency_us, |
| 62 | "max power saving latency for new devices; use PM QOS to change per device"); |
| 63 | |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 64 | static bool force_apst; |
| 65 | module_param(force_apst, bool, 0644); |
| 66 | MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off"); |
| 67 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 68 | static LIST_HEAD(nvme_ctrl_list); |
Ming Lin | 9f2482b | 2016-02-10 10:03:31 -0800 | [diff] [blame] | 69 | static DEFINE_SPINLOCK(dev_list_lock); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 70 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 71 | static struct class *nvme_class; |
| 72 | |
Christoph Hellwig | 65ba6b5 | 2017-04-20 16:02:58 +0200 | [diff] [blame] | 73 | static int nvme_error_status(struct request *req) |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 74 | { |
| 75 | switch (nvme_req(req)->status & 0x7ff) { |
| 76 | case NVME_SC_SUCCESS: |
| 77 | return 0; |
| 78 | case NVME_SC_CAP_EXCEEDED: |
| 79 | return -ENOSPC; |
| 80 | default: |
| 81 | return -EIO; |
Junxiong Guan | e02ab02 | 2017-04-21 12:59:07 +0200 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * XXX: these errors are a nasty side-band protocol to |
| 85 | * drivers/md/dm-mpath.c:noretry_error() that aren't documented |
| 86 | * anywhere.. |
| 87 | */ |
| 88 | case NVME_SC_CMD_SEQ_ERROR: |
| 89 | return -EILSEQ; |
| 90 | case NVME_SC_ONCS_NOT_SUPPORTED: |
| 91 | return -EOPNOTSUPP; |
| 92 | case NVME_SC_WRITE_FAULT: |
| 93 | case NVME_SC_READ_ERROR: |
| 94 | case NVME_SC_UNWRITTEN_BLOCK: |
| 95 | return -ENODATA; |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 96 | } |
| 97 | } |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 98 | |
Christoph Hellwig | f6324b1 | 2017-04-05 19:18:09 +0200 | [diff] [blame] | 99 | static inline bool nvme_req_needs_retry(struct request *req) |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 100 | { |
Christoph Hellwig | f6324b1 | 2017-04-05 19:18:09 +0200 | [diff] [blame] | 101 | if (blk_noretry_request(req)) |
| 102 | return false; |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 103 | if (nvme_req(req)->status & NVME_SC_DNR) |
Christoph Hellwig | f6324b1 | 2017-04-05 19:18:09 +0200 | [diff] [blame] | 104 | return false; |
| 105 | if (jiffies - req->start_time >= req->timeout) |
| 106 | return false; |
Christoph Hellwig | 44e44b2 | 2017-04-05 19:18:11 +0200 | [diff] [blame] | 107 | if (nvme_req(req)->retries >= nvme_max_retries) |
Christoph Hellwig | f6324b1 | 2017-04-05 19:18:09 +0200 | [diff] [blame] | 108 | return false; |
| 109 | return true; |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void nvme_complete_rq(struct request *req) |
| 113 | { |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 114 | if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) { |
| 115 | nvme_req(req)->retries++; |
| 116 | blk_mq_requeue_request(req, !blk_mq_queue_stopped(req->q)); |
| 117 | return; |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 120 | blk_mq_end_request(req, nvme_error_status(req)); |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 121 | } |
| 122 | EXPORT_SYMBOL_GPL(nvme_complete_rq); |
| 123 | |
Ming Lin | c55a2fd | 2016-05-18 14:05:02 -0700 | [diff] [blame] | 124 | void nvme_cancel_request(struct request *req, void *data, bool reserved) |
| 125 | { |
| 126 | int status; |
| 127 | |
| 128 | if (!blk_mq_request_started(req)) |
| 129 | return; |
| 130 | |
| 131 | dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device, |
| 132 | "Cancelling I/O %d", req->tag); |
| 133 | |
| 134 | status = NVME_SC_ABORT_REQ; |
| 135 | if (blk_queue_dying(req->q)) |
| 136 | status |= NVME_SC_DNR; |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 137 | nvme_req(req)->status = status; |
Christoph Hellwig | 08e0029 | 2017-04-20 16:03:09 +0200 | [diff] [blame] | 138 | blk_mq_complete_request(req); |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 139 | |
Ming Lin | c55a2fd | 2016-05-18 14:05:02 -0700 | [diff] [blame] | 140 | } |
| 141 | EXPORT_SYMBOL_GPL(nvme_cancel_request); |
| 142 | |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 143 | bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl, |
| 144 | enum nvme_ctrl_state new_state) |
| 145 | { |
Gabriel Krisman Bertazi | f6b6a28 | 2016-07-29 16:15:18 -0300 | [diff] [blame] | 146 | enum nvme_ctrl_state old_state; |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 147 | bool changed = false; |
| 148 | |
| 149 | spin_lock_irq(&ctrl->lock); |
Gabriel Krisman Bertazi | f6b6a28 | 2016-07-29 16:15:18 -0300 | [diff] [blame] | 150 | |
| 151 | old_state = ctrl->state; |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 152 | switch (new_state) { |
| 153 | case NVME_CTRL_LIVE: |
| 154 | switch (old_state) { |
Christoph Hellwig | 7d2e800 | 2016-06-13 16:45:22 +0200 | [diff] [blame] | 155 | case NVME_CTRL_NEW: |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 156 | case NVME_CTRL_RESETTING: |
Christoph Hellwig | def61ec | 2016-07-06 21:55:49 +0900 | [diff] [blame] | 157 | case NVME_CTRL_RECONNECTING: |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 158 | changed = true; |
| 159 | /* FALLTHRU */ |
| 160 | default: |
| 161 | break; |
| 162 | } |
| 163 | break; |
| 164 | case NVME_CTRL_RESETTING: |
| 165 | switch (old_state) { |
| 166 | case NVME_CTRL_NEW: |
| 167 | case NVME_CTRL_LIVE: |
Christoph Hellwig | def61ec | 2016-07-06 21:55:49 +0900 | [diff] [blame] | 168 | case NVME_CTRL_RECONNECTING: |
| 169 | changed = true; |
| 170 | /* FALLTHRU */ |
| 171 | default: |
| 172 | break; |
| 173 | } |
| 174 | break; |
| 175 | case NVME_CTRL_RECONNECTING: |
| 176 | switch (old_state) { |
| 177 | case NVME_CTRL_LIVE: |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 178 | changed = true; |
| 179 | /* FALLTHRU */ |
| 180 | default: |
| 181 | break; |
| 182 | } |
| 183 | break; |
| 184 | case NVME_CTRL_DELETING: |
| 185 | switch (old_state) { |
| 186 | case NVME_CTRL_LIVE: |
| 187 | case NVME_CTRL_RESETTING: |
Christoph Hellwig | def61ec | 2016-07-06 21:55:49 +0900 | [diff] [blame] | 188 | case NVME_CTRL_RECONNECTING: |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 189 | changed = true; |
| 190 | /* FALLTHRU */ |
| 191 | default: |
| 192 | break; |
| 193 | } |
| 194 | break; |
Keith Busch | 0ff9d4e | 2016-05-12 08:37:14 -0600 | [diff] [blame] | 195 | case NVME_CTRL_DEAD: |
| 196 | switch (old_state) { |
| 197 | case NVME_CTRL_DELETING: |
| 198 | changed = true; |
| 199 | /* FALLTHRU */ |
| 200 | default: |
| 201 | break; |
| 202 | } |
| 203 | break; |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 204 | default: |
| 205 | break; |
| 206 | } |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 207 | |
| 208 | if (changed) |
| 209 | ctrl->state = new_state; |
| 210 | |
Gabriel Krisman Bertazi | f6b6a28 | 2016-07-29 16:15:18 -0300 | [diff] [blame] | 211 | spin_unlock_irq(&ctrl->lock); |
| 212 | |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 213 | return changed; |
| 214 | } |
| 215 | EXPORT_SYMBOL_GPL(nvme_change_ctrl_state); |
| 216 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 217 | static void nvme_free_ns(struct kref *kref) |
| 218 | { |
| 219 | struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref); |
| 220 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 221 | if (ns->ndev) |
| 222 | nvme_nvm_unregister(ns); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 223 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 224 | if (ns->disk) { |
| 225 | spin_lock(&dev_list_lock); |
| 226 | ns->disk->private_data = NULL; |
| 227 | spin_unlock(&dev_list_lock); |
| 228 | } |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 229 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 230 | put_disk(ns->disk); |
Keith Busch | 075790e | 2016-02-24 09:15:53 -0700 | [diff] [blame] | 231 | ida_simple_remove(&ns->ctrl->ns_ida, ns->instance); |
| 232 | nvme_put_ctrl(ns->ctrl); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 233 | kfree(ns); |
| 234 | } |
| 235 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 236 | static void nvme_put_ns(struct nvme_ns *ns) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 237 | { |
| 238 | kref_put(&ns->kref, nvme_free_ns); |
| 239 | } |
| 240 | |
| 241 | static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk) |
| 242 | { |
| 243 | struct nvme_ns *ns; |
| 244 | |
| 245 | spin_lock(&dev_list_lock); |
| 246 | ns = disk->private_data; |
Sagi Grimberg | e439bb1 | 2016-02-10 10:03:29 -0800 | [diff] [blame] | 247 | if (ns) { |
| 248 | if (!kref_get_unless_zero(&ns->kref)) |
| 249 | goto fail; |
| 250 | if (!try_module_get(ns->ctrl->ops->module)) |
| 251 | goto fail_put_ns; |
| 252 | } |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 253 | spin_unlock(&dev_list_lock); |
| 254 | |
| 255 | return ns; |
Sagi Grimberg | e439bb1 | 2016-02-10 10:03:29 -0800 | [diff] [blame] | 256 | |
| 257 | fail_put_ns: |
| 258 | kref_put(&ns->kref, nvme_free_ns); |
| 259 | fail: |
| 260 | spin_unlock(&dev_list_lock); |
| 261 | return NULL; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 264 | struct request *nvme_alloc_request(struct request_queue *q, |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 265 | struct nvme_command *cmd, unsigned int flags, int qid) |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 266 | { |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 267 | unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 268 | struct request *req; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 269 | |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 270 | if (qid == NVME_QID_ANY) { |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 271 | req = blk_mq_alloc_request(q, op, flags); |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 272 | } else { |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 273 | req = blk_mq_alloc_request_hctx(q, op, flags, |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 274 | qid ? qid - 1 : 0); |
| 275 | } |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 276 | if (IS_ERR(req)) |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 277 | return req; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 278 | |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 279 | req->cmd_flags |= REQ_FAILFAST_DRIVER; |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 280 | nvme_req(req)->cmd = cmd; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 281 | |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 282 | return req; |
| 283 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 284 | EXPORT_SYMBOL_GPL(nvme_alloc_request); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 285 | |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 286 | static inline void nvme_setup_flush(struct nvme_ns *ns, |
| 287 | struct nvme_command *cmnd) |
| 288 | { |
| 289 | memset(cmnd, 0, sizeof(*cmnd)); |
| 290 | cmnd->common.opcode = nvme_cmd_flush; |
| 291 | cmnd->common.nsid = cpu_to_le32(ns->ns_id); |
| 292 | } |
| 293 | |
| 294 | static inline int nvme_setup_discard(struct nvme_ns *ns, struct request *req, |
| 295 | struct nvme_command *cmnd) |
| 296 | { |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 297 | unsigned short segments = blk_rq_nr_discard_segments(req), n = 0; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 298 | struct nvme_dsm_range *range; |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 299 | struct bio *bio; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 300 | |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 301 | range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC); |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 302 | if (!range) |
| 303 | return BLK_MQ_RQ_QUEUE_BUSY; |
| 304 | |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 305 | __rq_for_each_bio(bio, req) { |
| 306 | u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector); |
| 307 | u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift; |
| 308 | |
| 309 | range[n].cattr = cpu_to_le32(0); |
| 310 | range[n].nlb = cpu_to_le32(nlb); |
| 311 | range[n].slba = cpu_to_le64(slba); |
| 312 | n++; |
| 313 | } |
| 314 | |
| 315 | if (WARN_ON_ONCE(n != segments)) { |
| 316 | kfree(range); |
| 317 | return BLK_MQ_RQ_QUEUE_ERROR; |
| 318 | } |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 319 | |
| 320 | memset(cmnd, 0, sizeof(*cmnd)); |
| 321 | cmnd->dsm.opcode = nvme_cmd_dsm; |
| 322 | cmnd->dsm.nsid = cpu_to_le32(ns->ns_id); |
Christoph Hellwig | f1dd03a | 2017-03-31 17:00:05 +0200 | [diff] [blame] | 323 | cmnd->dsm.nr = cpu_to_le32(segments - 1); |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 324 | cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD); |
| 325 | |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 326 | req->special_vec.bv_page = virt_to_page(range); |
| 327 | req->special_vec.bv_offset = offset_in_page(range); |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 328 | req->special_vec.bv_len = sizeof(*range) * segments; |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 329 | req->rq_flags |= RQF_SPECIAL_PAYLOAD; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 330 | |
Omar Sandoval | bac0000 | 2016-11-15 11:11:58 -0800 | [diff] [blame] | 331 | return BLK_MQ_RQ_QUEUE_OK; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 332 | } |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 333 | |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 334 | static inline void nvme_setup_rw(struct nvme_ns *ns, struct request *req, |
| 335 | struct nvme_command *cmnd) |
| 336 | { |
| 337 | u16 control = 0; |
| 338 | u32 dsmgmt = 0; |
| 339 | |
| 340 | if (req->cmd_flags & REQ_FUA) |
| 341 | control |= NVME_RW_FUA; |
| 342 | if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD)) |
| 343 | control |= NVME_RW_LR; |
| 344 | |
| 345 | if (req->cmd_flags & REQ_RAHEAD) |
| 346 | dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; |
| 347 | |
| 348 | memset(cmnd, 0, sizeof(*cmnd)); |
| 349 | cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read); |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 350 | cmnd->rw.nsid = cpu_to_le32(ns->ns_id); |
| 351 | cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req))); |
| 352 | cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1); |
| 353 | |
| 354 | if (ns->ms) { |
| 355 | switch (ns->pi_type) { |
| 356 | case NVME_NS_DPS_PI_TYPE3: |
| 357 | control |= NVME_RW_PRINFO_PRCHK_GUARD; |
| 358 | break; |
| 359 | case NVME_NS_DPS_PI_TYPE1: |
| 360 | case NVME_NS_DPS_PI_TYPE2: |
| 361 | control |= NVME_RW_PRINFO_PRCHK_GUARD | |
| 362 | NVME_RW_PRINFO_PRCHK_REF; |
| 363 | cmnd->rw.reftag = cpu_to_le32( |
| 364 | nvme_block_nr(ns, blk_rq_pos(req))); |
| 365 | break; |
| 366 | } |
| 367 | if (!blk_integrity_rq(req)) |
| 368 | control |= NVME_RW_PRINFO_PRACT; |
| 369 | } |
| 370 | |
| 371 | cmnd->rw.control = cpu_to_le16(control); |
| 372 | cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); |
| 373 | } |
| 374 | |
| 375 | int nvme_setup_cmd(struct nvme_ns *ns, struct request *req, |
| 376 | struct nvme_command *cmd) |
| 377 | { |
Omar Sandoval | bac0000 | 2016-11-15 11:11:58 -0800 | [diff] [blame] | 378 | int ret = BLK_MQ_RQ_QUEUE_OK; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 379 | |
Christoph Hellwig | 987f699 | 2017-04-05 19:18:08 +0200 | [diff] [blame] | 380 | if (!(req->rq_flags & RQF_DONTPREP)) { |
Christoph Hellwig | 44e44b2 | 2017-04-05 19:18:11 +0200 | [diff] [blame] | 381 | nvme_req(req)->retries = 0; |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 382 | nvme_req(req)->flags = 0; |
Christoph Hellwig | 987f699 | 2017-04-05 19:18:08 +0200 | [diff] [blame] | 383 | req->rq_flags |= RQF_DONTPREP; |
| 384 | } |
| 385 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 386 | switch (req_op(req)) { |
| 387 | case REQ_OP_DRV_IN: |
| 388 | case REQ_OP_DRV_OUT: |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 389 | memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd)); |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 390 | break; |
| 391 | case REQ_OP_FLUSH: |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 392 | nvme_setup_flush(ns, cmd); |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 393 | break; |
Christoph Hellwig | e850fd1 | 2017-04-05 19:21:13 +0200 | [diff] [blame] | 394 | case REQ_OP_WRITE_ZEROES: |
| 395 | /* currently only aliased to deallocate for a few ctrls: */ |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 396 | case REQ_OP_DISCARD: |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 397 | ret = nvme_setup_discard(ns, req, cmd); |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 398 | break; |
| 399 | case REQ_OP_READ: |
| 400 | case REQ_OP_WRITE: |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 401 | nvme_setup_rw(ns, req, cmd); |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 402 | break; |
| 403 | default: |
| 404 | WARN_ON_ONCE(1); |
| 405 | return BLK_MQ_RQ_QUEUE_ERROR; |
| 406 | } |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 407 | |
James Smart | 721b391 | 2016-10-21 23:33:34 +0300 | [diff] [blame] | 408 | cmd->common.command_id = req->tag; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 409 | return ret; |
| 410 | } |
| 411 | EXPORT_SYMBOL_GPL(nvme_setup_cmd); |
| 412 | |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 413 | /* |
| 414 | * Returns 0 on success. If the result is negative, it's a Linux error code; |
| 415 | * if the result is positive, it's an NVM Express status code |
| 416 | */ |
| 417 | int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 418 | union nvme_result *result, void *buffer, unsigned bufflen, |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 419 | unsigned timeout, int qid, int at_head, int flags) |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 420 | { |
| 421 | struct request *req; |
| 422 | int ret; |
| 423 | |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 424 | req = nvme_alloc_request(q, cmd, flags, qid); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 425 | if (IS_ERR(req)) |
| 426 | return PTR_ERR(req); |
| 427 | |
| 428 | req->timeout = timeout ? timeout : ADMIN_TIMEOUT; |
| 429 | |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 430 | if (buffer && bufflen) { |
| 431 | ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL); |
| 432 | if (ret) |
| 433 | goto out; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 434 | } |
| 435 | |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 436 | blk_execute_rq(req->q, NULL, req, at_head); |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 437 | if (result) |
| 438 | *result = nvme_req(req)->result; |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 439 | if (nvme_req(req)->flags & NVME_REQ_CANCELLED) |
| 440 | ret = -EINTR; |
| 441 | else |
| 442 | ret = nvme_req(req)->status; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 443 | out: |
| 444 | blk_mq_free_request(req); |
| 445 | return ret; |
| 446 | } |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 447 | EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 448 | |
| 449 | int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, |
| 450 | void *buffer, unsigned bufflen) |
| 451 | { |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 452 | return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0, |
| 453 | NVME_QID_ANY, 0, 0); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 454 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 455 | EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 456 | |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 457 | int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd, |
| 458 | void __user *ubuffer, unsigned bufflen, |
| 459 | void __user *meta_buffer, unsigned meta_len, u32 meta_seed, |
| 460 | u32 *result, unsigned timeout) |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 461 | { |
Christoph Hellwig | 7a5abb4 | 2016-06-06 23:20:49 +0200 | [diff] [blame] | 462 | bool write = nvme_is_write(cmd); |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 463 | struct nvme_ns *ns = q->queuedata; |
| 464 | struct gendisk *disk = ns ? ns->disk : NULL; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 465 | struct request *req; |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 466 | struct bio *bio = NULL; |
| 467 | void *meta = NULL; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 468 | int ret; |
| 469 | |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 470 | req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 471 | if (IS_ERR(req)) |
| 472 | return PTR_ERR(req); |
| 473 | |
| 474 | req->timeout = timeout ? timeout : ADMIN_TIMEOUT; |
| 475 | |
| 476 | if (ubuffer && bufflen) { |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 477 | ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen, |
| 478 | GFP_KERNEL); |
| 479 | if (ret) |
| 480 | goto out; |
| 481 | bio = req->bio; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 482 | |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 483 | if (!disk) |
| 484 | goto submit; |
| 485 | bio->bi_bdev = bdget_disk(disk, 0); |
| 486 | if (!bio->bi_bdev) { |
| 487 | ret = -ENODEV; |
| 488 | goto out_unmap; |
| 489 | } |
| 490 | |
Keith Busch | e9fc63d | 2016-02-24 09:15:58 -0700 | [diff] [blame] | 491 | if (meta_buffer && meta_len) { |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 492 | struct bio_integrity_payload *bip; |
| 493 | |
| 494 | meta = kmalloc(meta_len, GFP_KERNEL); |
| 495 | if (!meta) { |
| 496 | ret = -ENOMEM; |
| 497 | goto out_unmap; |
| 498 | } |
| 499 | |
| 500 | if (write) { |
| 501 | if (copy_from_user(meta, meta_buffer, |
| 502 | meta_len)) { |
| 503 | ret = -EFAULT; |
| 504 | goto out_free_meta; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | bip = bio_integrity_alloc(bio, GFP_KERNEL, 1); |
Keith Busch | 06c1e39 | 2015-12-03 09:32:21 -0700 | [diff] [blame] | 509 | if (IS_ERR(bip)) { |
| 510 | ret = PTR_ERR(bip); |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 511 | goto out_free_meta; |
| 512 | } |
| 513 | |
| 514 | bip->bip_iter.bi_size = meta_len; |
| 515 | bip->bip_iter.bi_sector = meta_seed; |
| 516 | |
| 517 | ret = bio_integrity_add_page(bio, virt_to_page(meta), |
| 518 | meta_len, offset_in_page(meta)); |
| 519 | if (ret != meta_len) { |
| 520 | ret = -ENOMEM; |
| 521 | goto out_free_meta; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | submit: |
| 526 | blk_execute_rq(req->q, disk, req, 0); |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 527 | if (nvme_req(req)->flags & NVME_REQ_CANCELLED) |
| 528 | ret = -EINTR; |
| 529 | else |
| 530 | ret = nvme_req(req)->status; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 531 | if (result) |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 532 | *result = le32_to_cpu(nvme_req(req)->result.u32); |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 533 | if (meta && !ret && !write) { |
| 534 | if (copy_to_user(meta_buffer, meta, meta_len)) |
| 535 | ret = -EFAULT; |
| 536 | } |
| 537 | out_free_meta: |
| 538 | kfree(meta); |
| 539 | out_unmap: |
| 540 | if (bio) { |
| 541 | if (disk && bio->bi_bdev) |
| 542 | bdput(bio->bi_bdev); |
| 543 | blk_rq_unmap_user(bio); |
| 544 | } |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 545 | out: |
| 546 | blk_mq_free_request(req); |
| 547 | return ret; |
| 548 | } |
| 549 | |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 550 | int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd, |
| 551 | void __user *ubuffer, unsigned bufflen, u32 *result, |
| 552 | unsigned timeout) |
| 553 | { |
| 554 | return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0, |
| 555 | result, timeout); |
| 556 | } |
| 557 | |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 558 | static void nvme_keep_alive_end_io(struct request *rq, int error) |
| 559 | { |
| 560 | struct nvme_ctrl *ctrl = rq->end_io_data; |
| 561 | |
| 562 | blk_mq_free_request(rq); |
| 563 | |
| 564 | if (error) { |
| 565 | dev_err(ctrl->device, |
| 566 | "failed nvme_keep_alive_end_io error=%d\n", error); |
| 567 | return; |
| 568 | } |
| 569 | |
| 570 | schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ); |
| 571 | } |
| 572 | |
| 573 | static int nvme_keep_alive(struct nvme_ctrl *ctrl) |
| 574 | { |
| 575 | struct nvme_command c; |
| 576 | struct request *rq; |
| 577 | |
| 578 | memset(&c, 0, sizeof(c)); |
| 579 | c.common.opcode = nvme_admin_keep_alive; |
| 580 | |
| 581 | rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED, |
| 582 | NVME_QID_ANY); |
| 583 | if (IS_ERR(rq)) |
| 584 | return PTR_ERR(rq); |
| 585 | |
| 586 | rq->timeout = ctrl->kato * HZ; |
| 587 | rq->end_io_data = ctrl; |
| 588 | |
| 589 | blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io); |
| 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static void nvme_keep_alive_work(struct work_struct *work) |
| 595 | { |
| 596 | struct nvme_ctrl *ctrl = container_of(to_delayed_work(work), |
| 597 | struct nvme_ctrl, ka_work); |
| 598 | |
| 599 | if (nvme_keep_alive(ctrl)) { |
| 600 | /* allocation failure, reset the controller */ |
| 601 | dev_err(ctrl->device, "keep-alive failed\n"); |
| 602 | ctrl->ops->reset_ctrl(ctrl); |
| 603 | return; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | void nvme_start_keep_alive(struct nvme_ctrl *ctrl) |
| 608 | { |
| 609 | if (unlikely(ctrl->kato == 0)) |
| 610 | return; |
| 611 | |
| 612 | INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work); |
| 613 | schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ); |
| 614 | } |
| 615 | EXPORT_SYMBOL_GPL(nvme_start_keep_alive); |
| 616 | |
| 617 | void nvme_stop_keep_alive(struct nvme_ctrl *ctrl) |
| 618 | { |
| 619 | if (unlikely(ctrl->kato == 0)) |
| 620 | return; |
| 621 | |
| 622 | cancel_delayed_work_sync(&ctrl->ka_work); |
| 623 | } |
| 624 | EXPORT_SYMBOL_GPL(nvme_stop_keep_alive); |
| 625 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 626 | 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] | 627 | { |
| 628 | struct nvme_command c = { }; |
| 629 | int error; |
| 630 | |
| 631 | /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ |
| 632 | c.identify.opcode = nvme_admin_identify; |
Parav Pandit | 986994a | 2017-01-26 17:17:28 +0200 | [diff] [blame] | 633 | c.identify.cns = NVME_ID_CNS_CTRL; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 634 | |
| 635 | *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL); |
| 636 | if (!*id) |
| 637 | return -ENOMEM; |
| 638 | |
| 639 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, |
| 640 | sizeof(struct nvme_id_ctrl)); |
| 641 | if (error) |
| 642 | kfree(*id); |
| 643 | return error; |
| 644 | } |
| 645 | |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 646 | static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list) |
| 647 | { |
| 648 | struct nvme_command c = { }; |
| 649 | |
| 650 | c.identify.opcode = nvme_admin_identify; |
Parav Pandit | 986994a | 2017-01-26 17:17:28 +0200 | [diff] [blame] | 651 | c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST; |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 652 | c.identify.nsid = cpu_to_le32(nsid); |
| 653 | return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000); |
| 654 | } |
| 655 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 656 | int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid, |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 657 | struct nvme_id_ns **id) |
| 658 | { |
| 659 | struct nvme_command c = { }; |
| 660 | int error; |
| 661 | |
| 662 | /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ |
Max Gurtovoy | 778f067 | 2017-01-26 17:17:27 +0200 | [diff] [blame] | 663 | c.identify.opcode = nvme_admin_identify; |
| 664 | c.identify.nsid = cpu_to_le32(nsid); |
Parav Pandit | 986994a | 2017-01-26 17:17:28 +0200 | [diff] [blame] | 665 | c.identify.cns = NVME_ID_CNS_NS; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 666 | |
| 667 | *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL); |
| 668 | if (!*id) |
| 669 | return -ENOMEM; |
| 670 | |
| 671 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, |
| 672 | sizeof(struct nvme_id_ns)); |
| 673 | if (error) |
| 674 | kfree(*id); |
| 675 | return error; |
| 676 | } |
| 677 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 678 | int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid, |
Andy Lutomirski | 1a6fe74 | 2016-09-16 11:16:10 -0700 | [diff] [blame] | 679 | void *buffer, size_t buflen, u32 *result) |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 680 | { |
| 681 | struct nvme_command c; |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 682 | union nvme_result res; |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame] | 683 | int ret; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 684 | |
| 685 | memset(&c, 0, sizeof(c)); |
| 686 | c.features.opcode = nvme_admin_get_features; |
| 687 | c.features.nsid = cpu_to_le32(nsid); |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 688 | c.features.fid = cpu_to_le32(fid); |
| 689 | |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 690 | ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, buffer, buflen, 0, |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 691 | NVME_QID_ANY, 0, 0); |
Andy Lutomirski | 9b47f77a | 2016-08-24 03:52:12 -0700 | [diff] [blame] | 692 | if (ret >= 0 && result) |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 693 | *result = le32_to_cpu(res.u32); |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame] | 694 | return ret; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 695 | } |
| 696 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 697 | int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11, |
Andy Lutomirski | 1a6fe74 | 2016-09-16 11:16:10 -0700 | [diff] [blame] | 698 | void *buffer, size_t buflen, u32 *result) |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 699 | { |
| 700 | struct nvme_command c; |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 701 | union nvme_result res; |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame] | 702 | int ret; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 703 | |
| 704 | memset(&c, 0, sizeof(c)); |
| 705 | c.features.opcode = nvme_admin_set_features; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 706 | c.features.fid = cpu_to_le32(fid); |
| 707 | c.features.dword11 = cpu_to_le32(dword11); |
| 708 | |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 709 | ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, |
Andy Lutomirski | 1a6fe74 | 2016-09-16 11:16:10 -0700 | [diff] [blame] | 710 | buffer, buflen, 0, NVME_QID_ANY, 0, 0); |
Andy Lutomirski | 9b47f77a | 2016-08-24 03:52:12 -0700 | [diff] [blame] | 711 | if (ret >= 0 && result) |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 712 | *result = le32_to_cpu(res.u32); |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame] | 713 | return ret; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 714 | } |
| 715 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 716 | 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] | 717 | { |
| 718 | struct nvme_command c = { }; |
| 719 | int error; |
| 720 | |
| 721 | c.common.opcode = nvme_admin_get_log_page, |
| 722 | c.common.nsid = cpu_to_le32(0xFFFFFFFF), |
| 723 | c.common.cdw10[0] = cpu_to_le32( |
| 724 | (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) | |
| 725 | NVME_LOG_SMART), |
| 726 | |
| 727 | *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL); |
| 728 | if (!*log) |
| 729 | return -ENOMEM; |
| 730 | |
| 731 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *log, |
| 732 | sizeof(struct nvme_smart_log)); |
| 733 | if (error) |
| 734 | kfree(*log); |
| 735 | return error; |
| 736 | } |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 737 | |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 738 | int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count) |
| 739 | { |
| 740 | u32 q_count = (*count - 1) | ((*count - 1) << 16); |
| 741 | u32 result; |
| 742 | int status, nr_io_queues; |
| 743 | |
Andy Lutomirski | 1a6fe74 | 2016-09-16 11:16:10 -0700 | [diff] [blame] | 744 | status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0, |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 745 | &result); |
Christoph Hellwig | f5fa90d | 2016-06-06 23:20:50 +0200 | [diff] [blame] | 746 | if (status < 0) |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 747 | return status; |
| 748 | |
Christoph Hellwig | f5fa90d | 2016-06-06 23:20:50 +0200 | [diff] [blame] | 749 | /* |
| 750 | * Degraded controllers might return an error when setting the queue |
| 751 | * count. We still want to be able to bring them online and offer |
| 752 | * access to the admin queue, as that might be only way to fix them up. |
| 753 | */ |
| 754 | if (status > 0) { |
| 755 | dev_err(ctrl->dev, "Could not set queue count (%d)\n", status); |
| 756 | *count = 0; |
| 757 | } else { |
| 758 | nr_io_queues = min(result & 0xffff, result >> 16) + 1; |
| 759 | *count = min(*count, nr_io_queues); |
| 760 | } |
| 761 | |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 762 | return 0; |
| 763 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 764 | EXPORT_SYMBOL_GPL(nvme_set_queue_count); |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 765 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 766 | static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) |
| 767 | { |
| 768 | struct nvme_user_io io; |
| 769 | struct nvme_command c; |
| 770 | unsigned length, meta_len; |
| 771 | void __user *metadata; |
| 772 | |
| 773 | if (copy_from_user(&io, uio, sizeof(io))) |
| 774 | return -EFAULT; |
Keith Busch | 63088ec | 2016-02-24 09:15:57 -0700 | [diff] [blame] | 775 | if (io.flags) |
| 776 | return -EINVAL; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 777 | |
| 778 | switch (io.opcode) { |
| 779 | case nvme_cmd_write: |
| 780 | case nvme_cmd_read: |
| 781 | case nvme_cmd_compare: |
| 782 | break; |
| 783 | default: |
| 784 | return -EINVAL; |
| 785 | } |
| 786 | |
| 787 | length = (io.nblocks + 1) << ns->lba_shift; |
| 788 | meta_len = (io.nblocks + 1) * ns->ms; |
| 789 | metadata = (void __user *)(uintptr_t)io.metadata; |
| 790 | |
| 791 | if (ns->ext) { |
| 792 | length += meta_len; |
| 793 | meta_len = 0; |
| 794 | } else if (meta_len) { |
| 795 | if ((io.metadata & 3) || !io.metadata) |
| 796 | return -EINVAL; |
| 797 | } |
| 798 | |
| 799 | memset(&c, 0, sizeof(c)); |
| 800 | c.rw.opcode = io.opcode; |
| 801 | c.rw.flags = io.flags; |
| 802 | c.rw.nsid = cpu_to_le32(ns->ns_id); |
| 803 | c.rw.slba = cpu_to_le64(io.slba); |
| 804 | c.rw.length = cpu_to_le16(io.nblocks); |
| 805 | c.rw.control = cpu_to_le16(io.control); |
| 806 | c.rw.dsmgmt = cpu_to_le32(io.dsmgmt); |
| 807 | c.rw.reftag = cpu_to_le32(io.reftag); |
| 808 | c.rw.apptag = cpu_to_le16(io.apptag); |
| 809 | c.rw.appmask = cpu_to_le16(io.appmask); |
| 810 | |
| 811 | return __nvme_submit_user_cmd(ns->queue, &c, |
| 812 | (void __user *)(uintptr_t)io.addr, length, |
| 813 | metadata, meta_len, io.slba, NULL, 0); |
| 814 | } |
| 815 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 816 | 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] | 817 | struct nvme_passthru_cmd __user *ucmd) |
| 818 | { |
| 819 | struct nvme_passthru_cmd cmd; |
| 820 | struct nvme_command c; |
| 821 | unsigned timeout = 0; |
| 822 | int status; |
| 823 | |
| 824 | if (!capable(CAP_SYS_ADMIN)) |
| 825 | return -EACCES; |
| 826 | if (copy_from_user(&cmd, ucmd, sizeof(cmd))) |
| 827 | return -EFAULT; |
Keith Busch | 63088ec | 2016-02-24 09:15:57 -0700 | [diff] [blame] | 828 | if (cmd.flags) |
| 829 | return -EINVAL; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 830 | |
| 831 | memset(&c, 0, sizeof(c)); |
| 832 | c.common.opcode = cmd.opcode; |
| 833 | c.common.flags = cmd.flags; |
| 834 | c.common.nsid = cpu_to_le32(cmd.nsid); |
| 835 | c.common.cdw2[0] = cpu_to_le32(cmd.cdw2); |
| 836 | c.common.cdw2[1] = cpu_to_le32(cmd.cdw3); |
| 837 | c.common.cdw10[0] = cpu_to_le32(cmd.cdw10); |
| 838 | c.common.cdw10[1] = cpu_to_le32(cmd.cdw11); |
| 839 | c.common.cdw10[2] = cpu_to_le32(cmd.cdw12); |
| 840 | c.common.cdw10[3] = cpu_to_le32(cmd.cdw13); |
| 841 | c.common.cdw10[4] = cpu_to_le32(cmd.cdw14); |
| 842 | c.common.cdw10[5] = cpu_to_le32(cmd.cdw15); |
| 843 | |
| 844 | if (cmd.timeout_ms) |
| 845 | timeout = msecs_to_jiffies(cmd.timeout_ms); |
| 846 | |
| 847 | status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c, |
Arnd Bergmann | d1ea7be | 2015-12-08 16:22:17 +0100 | [diff] [blame] | 848 | (void __user *)(uintptr_t)cmd.addr, cmd.data_len, |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 849 | &cmd.result, timeout); |
| 850 | if (status >= 0) { |
| 851 | if (put_user(cmd.result, &ucmd->result)) |
| 852 | return -EFAULT; |
| 853 | } |
| 854 | |
| 855 | return status; |
| 856 | } |
| 857 | |
| 858 | static int nvme_ioctl(struct block_device *bdev, fmode_t mode, |
| 859 | unsigned int cmd, unsigned long arg) |
| 860 | { |
| 861 | struct nvme_ns *ns = bdev->bd_disk->private_data; |
| 862 | |
| 863 | switch (cmd) { |
| 864 | case NVME_IOCTL_ID: |
| 865 | force_successful_syscall_return(); |
| 866 | return ns->ns_id; |
| 867 | case NVME_IOCTL_ADMIN_CMD: |
| 868 | return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg); |
| 869 | case NVME_IOCTL_IO_CMD: |
| 870 | return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg); |
| 871 | case NVME_IOCTL_SUBMIT_IO: |
| 872 | return nvme_submit_io(ns, (void __user *)arg); |
Christoph Hellwig | 4490733 | 2015-12-24 15:27:02 +0100 | [diff] [blame] | 873 | #ifdef CONFIG_BLK_DEV_NVME_SCSI |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 874 | case SG_GET_VERSION_NUM: |
| 875 | return nvme_sg_get_version_num((void __user *)arg); |
| 876 | case SG_IO: |
| 877 | return nvme_sg_io(ns, (void __user *)arg); |
Christoph Hellwig | 4490733 | 2015-12-24 15:27:02 +0100 | [diff] [blame] | 878 | #endif |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 879 | default: |
Matias Bjørling | 84d4add | 2017-01-31 13:17:16 +0100 | [diff] [blame] | 880 | #ifdef CONFIG_NVM |
| 881 | if (ns->ndev) |
| 882 | return nvme_nvm_ioctl(ns, cmd, arg); |
| 883 | #endif |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 884 | if (is_sed_ioctl(cmd)) |
Christoph Hellwig | 4f1244c | 2017-02-17 13:59:39 +0100 | [diff] [blame] | 885 | return sed_ioctl(ns->ctrl->opal_dev, cmd, |
Scott Bauer | e225c20 | 2017-02-14 17:29:36 -0700 | [diff] [blame] | 886 | (void __user *) arg); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 887 | return -ENOTTY; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | #ifdef CONFIG_COMPAT |
| 892 | static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 893 | unsigned int cmd, unsigned long arg) |
| 894 | { |
| 895 | switch (cmd) { |
| 896 | case SG_IO: |
| 897 | return -ENOIOCTLCMD; |
| 898 | } |
| 899 | return nvme_ioctl(bdev, mode, cmd, arg); |
| 900 | } |
| 901 | #else |
| 902 | #define nvme_compat_ioctl NULL |
| 903 | #endif |
| 904 | |
| 905 | static int nvme_open(struct block_device *bdev, fmode_t mode) |
| 906 | { |
| 907 | return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO; |
| 908 | } |
| 909 | |
| 910 | static void nvme_release(struct gendisk *disk, fmode_t mode) |
| 911 | { |
Sagi Grimberg | e439bb1 | 2016-02-10 10:03:29 -0800 | [diff] [blame] | 912 | struct nvme_ns *ns = disk->private_data; |
| 913 | |
| 914 | module_put(ns->ctrl->ops->module); |
| 915 | nvme_put_ns(ns); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 919 | { |
| 920 | /* some standard values */ |
| 921 | geo->heads = 1 << 6; |
| 922 | geo->sectors = 1 << 5; |
| 923 | geo->cylinders = get_capacity(bdev->bd_disk) >> 11; |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
Christoph Hellwig | c81bfba | 2017-05-20 15:14:45 +0200 | [diff] [blame] | 928 | static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id, |
| 929 | u16 bs) |
| 930 | { |
| 931 | struct nvme_ns *ns = disk->private_data; |
| 932 | u16 old_ms = ns->ms; |
| 933 | u8 pi_type = 0; |
| 934 | |
| 935 | ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms); |
| 936 | ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT); |
| 937 | |
| 938 | /* PI implementation requires metadata equal t10 pi tuple size */ |
| 939 | if (ns->ms == sizeof(struct t10_pi_tuple)) |
| 940 | pi_type = id->dps & NVME_NS_DPS_PI_MASK; |
| 941 | |
| 942 | if (blk_get_integrity(disk) && |
| 943 | (ns->pi_type != pi_type || ns->ms != old_ms || |
| 944 | bs != queue_logical_block_size(disk->queue) || |
| 945 | (ns->ms && ns->ext))) |
| 946 | blk_integrity_unregister(disk); |
| 947 | |
| 948 | ns->pi_type = pi_type; |
| 949 | } |
| 950 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 951 | static void nvme_init_integrity(struct nvme_ns *ns) |
| 952 | { |
| 953 | struct blk_integrity integrity; |
| 954 | |
Jay Freyensee | fa9a89f | 2016-07-20 21:26:16 -0600 | [diff] [blame] | 955 | memset(&integrity, 0, sizeof(integrity)); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 956 | switch (ns->pi_type) { |
| 957 | case NVME_NS_DPS_PI_TYPE3: |
| 958 | integrity.profile = &t10_pi_type3_crc; |
Nicholas Bellinger | ba36c21 | 2016-04-09 03:04:42 +0000 | [diff] [blame] | 959 | integrity.tag_size = sizeof(u16) + sizeof(u32); |
| 960 | integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 961 | break; |
| 962 | case NVME_NS_DPS_PI_TYPE1: |
| 963 | case NVME_NS_DPS_PI_TYPE2: |
| 964 | integrity.profile = &t10_pi_type1_crc; |
Nicholas Bellinger | ba36c21 | 2016-04-09 03:04:42 +0000 | [diff] [blame] | 965 | integrity.tag_size = sizeof(u16); |
| 966 | integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 967 | break; |
| 968 | default: |
| 969 | integrity.profile = NULL; |
| 970 | break; |
| 971 | } |
| 972 | integrity.tuple_size = ns->ms; |
| 973 | blk_integrity_register(ns->disk, &integrity); |
| 974 | blk_queue_max_integrity_segments(ns->queue, 1); |
| 975 | } |
| 976 | #else |
Christoph Hellwig | c81bfba | 2017-05-20 15:14:45 +0200 | [diff] [blame] | 977 | static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id, |
| 978 | u16 bs) |
| 979 | { |
| 980 | } |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 981 | static void nvme_init_integrity(struct nvme_ns *ns) |
| 982 | { |
| 983 | } |
| 984 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ |
| 985 | |
| 986 | static void nvme_config_discard(struct nvme_ns *ns) |
| 987 | { |
Keith Busch | 08095e7 | 2016-03-04 13:15:17 -0700 | [diff] [blame] | 988 | struct nvme_ctrl *ctrl = ns->ctrl; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 989 | u32 logical_block_size = queue_logical_block_size(ns->queue); |
Keith Busch | 08095e7 | 2016-03-04 13:15:17 -0700 | [diff] [blame] | 990 | |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 991 | BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) < |
| 992 | NVME_DSM_MAX_RANGES); |
| 993 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 994 | ns->queue->limits.discard_alignment = logical_block_size; |
| 995 | ns->queue->limits.discard_granularity = logical_block_size; |
Minfei Huang | bd0fc28 | 2016-05-17 15:58:41 +0800 | [diff] [blame] | 996 | blk_queue_max_discard_sectors(ns->queue, UINT_MAX); |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 997 | blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 998 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue); |
Christoph Hellwig | e850fd1 | 2017-04-05 19:21:13 +0200 | [diff] [blame] | 999 | |
| 1000 | if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES) |
| 1001 | blk_queue_max_write_zeroes_sectors(ns->queue, UINT_MAX); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1002 | } |
| 1003 | |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1004 | static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1005 | { |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1006 | if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 1007 | dev_warn(ns->ctrl->dev, "%s: Identify failure\n", __func__); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1008 | return -ENODEV; |
| 1009 | } |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1010 | |
| 1011 | if ((*id)->ncap == 0) { |
| 1012 | kfree(*id); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1013 | return -ENODEV; |
| 1014 | } |
| 1015 | |
Gabriel Krisman Bertazi | 8ef2074 | 2016-10-19 09:51:05 -0600 | [diff] [blame] | 1016 | if (ns->ctrl->vs >= NVME_VS(1, 1, 0)) |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1017 | memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui)); |
Gabriel Krisman Bertazi | 8ef2074 | 2016-10-19 09:51:05 -0600 | [diff] [blame] | 1018 | if (ns->ctrl->vs >= NVME_VS(1, 2, 0)) |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1019 | memcpy(ns->uuid, (*id)->nguid, sizeof(ns->uuid)); |
| 1020 | |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id) |
| 1025 | { |
| 1026 | struct nvme_ns *ns = disk->private_data; |
Christoph Hellwig | c81bfba | 2017-05-20 15:14:45 +0200 | [diff] [blame] | 1027 | u16 bs; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1028 | |
| 1029 | /* |
| 1030 | * If identify namespace failed, use default 512 byte block size so |
| 1031 | * block layer can use before failing read/write for 0 capacity. |
| 1032 | */ |
Christoph Hellwig | c81bfba | 2017-05-20 15:14:45 +0200 | [diff] [blame] | 1033 | ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1034 | if (ns->lba_shift == 0) |
| 1035 | ns->lba_shift = 9; |
| 1036 | bs = 1 << ns->lba_shift; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1037 | |
| 1038 | blk_mq_freeze_queue(disk->queue); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1039 | |
Christoph Hellwig | c81bfba | 2017-05-20 15:14:45 +0200 | [diff] [blame] | 1040 | if (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED) |
| 1041 | nvme_prep_integrity(disk, id, bs); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1042 | blk_queue_logical_block_size(ns->queue, bs); |
Keith Busch | 4b9d5b1 | 2015-11-20 09:13:30 +0100 | [diff] [blame] | 1043 | if (ns->ms && !blk_get_integrity(disk) && !ns->ext) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1044 | nvme_init_integrity(ns); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1045 | if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk)) |
| 1046 | set_capacity(disk, 0); |
| 1047 | else |
| 1048 | set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); |
| 1049 | |
| 1050 | if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM) |
| 1051 | nvme_config_discard(ns); |
| 1052 | blk_mq_unfreeze_queue(disk->queue); |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1053 | } |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1054 | |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1055 | static int nvme_revalidate_disk(struct gendisk *disk) |
| 1056 | { |
| 1057 | struct nvme_ns *ns = disk->private_data; |
| 1058 | struct nvme_id_ns *id = NULL; |
| 1059 | int ret; |
| 1060 | |
| 1061 | if (test_bit(NVME_NS_DEAD, &ns->flags)) { |
| 1062 | set_capacity(disk, 0); |
| 1063 | return -ENODEV; |
| 1064 | } |
| 1065 | |
| 1066 | ret = nvme_revalidate_ns(ns, &id); |
| 1067 | if (ret) |
| 1068 | return ret; |
| 1069 | |
| 1070 | __nvme_revalidate_disk(disk, id); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1071 | kfree(id); |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1072 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | static char nvme_pr_type(enum pr_type type) |
| 1077 | { |
| 1078 | switch (type) { |
| 1079 | case PR_WRITE_EXCLUSIVE: |
| 1080 | return 1; |
| 1081 | case PR_EXCLUSIVE_ACCESS: |
| 1082 | return 2; |
| 1083 | case PR_WRITE_EXCLUSIVE_REG_ONLY: |
| 1084 | return 3; |
| 1085 | case PR_EXCLUSIVE_ACCESS_REG_ONLY: |
| 1086 | return 4; |
| 1087 | case PR_WRITE_EXCLUSIVE_ALL_REGS: |
| 1088 | return 5; |
| 1089 | case PR_EXCLUSIVE_ACCESS_ALL_REGS: |
| 1090 | return 6; |
| 1091 | default: |
| 1092 | return 0; |
| 1093 | } |
| 1094 | }; |
| 1095 | |
| 1096 | static int nvme_pr_command(struct block_device *bdev, u32 cdw10, |
| 1097 | u64 key, u64 sa_key, u8 op) |
| 1098 | { |
| 1099 | struct nvme_ns *ns = bdev->bd_disk->private_data; |
| 1100 | struct nvme_command c; |
| 1101 | u8 data[16] = { 0, }; |
| 1102 | |
| 1103 | put_unaligned_le64(key, &data[0]); |
| 1104 | put_unaligned_le64(sa_key, &data[8]); |
| 1105 | |
| 1106 | memset(&c, 0, sizeof(c)); |
| 1107 | c.common.opcode = op; |
| 1108 | c.common.nsid = cpu_to_le32(ns->ns_id); |
| 1109 | c.common.cdw10[0] = cpu_to_le32(cdw10); |
| 1110 | |
| 1111 | return nvme_submit_sync_cmd(ns->queue, &c, data, 16); |
| 1112 | } |
| 1113 | |
| 1114 | static int nvme_pr_register(struct block_device *bdev, u64 old, |
| 1115 | u64 new, unsigned flags) |
| 1116 | { |
| 1117 | u32 cdw10; |
| 1118 | |
| 1119 | if (flags & ~PR_FL_IGNORE_KEY) |
| 1120 | return -EOPNOTSUPP; |
| 1121 | |
| 1122 | cdw10 = old ? 2 : 0; |
| 1123 | cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0; |
| 1124 | cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */ |
| 1125 | return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register); |
| 1126 | } |
| 1127 | |
| 1128 | static int nvme_pr_reserve(struct block_device *bdev, u64 key, |
| 1129 | enum pr_type type, unsigned flags) |
| 1130 | { |
| 1131 | u32 cdw10; |
| 1132 | |
| 1133 | if (flags & ~PR_FL_IGNORE_KEY) |
| 1134 | return -EOPNOTSUPP; |
| 1135 | |
| 1136 | cdw10 = nvme_pr_type(type) << 8; |
| 1137 | cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0); |
| 1138 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire); |
| 1139 | } |
| 1140 | |
| 1141 | static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new, |
| 1142 | enum pr_type type, bool abort) |
| 1143 | { |
| 1144 | u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1; |
| 1145 | return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire); |
| 1146 | } |
| 1147 | |
| 1148 | static int nvme_pr_clear(struct block_device *bdev, u64 key) |
| 1149 | { |
Dan Carpenter | 8c0b391 | 2015-12-09 13:24:06 +0300 | [diff] [blame] | 1150 | u32 cdw10 = 1 | (key ? 1 << 3 : 0); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1151 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register); |
| 1152 | } |
| 1153 | |
| 1154 | static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type) |
| 1155 | { |
| 1156 | u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0; |
| 1157 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release); |
| 1158 | } |
| 1159 | |
| 1160 | static const struct pr_ops nvme_pr_ops = { |
| 1161 | .pr_register = nvme_pr_register, |
| 1162 | .pr_reserve = nvme_pr_reserve, |
| 1163 | .pr_release = nvme_pr_release, |
| 1164 | .pr_preempt = nvme_pr_preempt, |
| 1165 | .pr_clear = nvme_pr_clear, |
| 1166 | }; |
| 1167 | |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1168 | #ifdef CONFIG_BLK_SED_OPAL |
Christoph Hellwig | 4f1244c | 2017-02-17 13:59:39 +0100 | [diff] [blame] | 1169 | int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len, |
| 1170 | bool send) |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1171 | { |
Christoph Hellwig | 4f1244c | 2017-02-17 13:59:39 +0100 | [diff] [blame] | 1172 | struct nvme_ctrl *ctrl = data; |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1173 | struct nvme_command cmd; |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1174 | |
| 1175 | memset(&cmd, 0, sizeof(cmd)); |
| 1176 | if (send) |
| 1177 | cmd.common.opcode = nvme_admin_security_send; |
| 1178 | else |
| 1179 | cmd.common.opcode = nvme_admin_security_recv; |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1180 | cmd.common.nsid = 0; |
| 1181 | cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8); |
| 1182 | cmd.common.cdw10[1] = cpu_to_le32(len); |
| 1183 | |
| 1184 | return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len, |
| 1185 | ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0); |
| 1186 | } |
| 1187 | EXPORT_SYMBOL_GPL(nvme_sec_submit); |
| 1188 | #endif /* CONFIG_BLK_SED_OPAL */ |
| 1189 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1190 | static const struct block_device_operations nvme_fops = { |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1191 | .owner = THIS_MODULE, |
| 1192 | .ioctl = nvme_ioctl, |
| 1193 | .compat_ioctl = nvme_compat_ioctl, |
| 1194 | .open = nvme_open, |
| 1195 | .release = nvme_release, |
| 1196 | .getgeo = nvme_getgeo, |
| 1197 | .revalidate_disk= nvme_revalidate_disk, |
| 1198 | .pr_ops = &nvme_pr_ops, |
| 1199 | }; |
| 1200 | |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1201 | static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled) |
| 1202 | { |
| 1203 | unsigned long timeout = |
| 1204 | ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies; |
| 1205 | u32 csts, bit = enabled ? NVME_CSTS_RDY : 0; |
| 1206 | int ret; |
| 1207 | |
| 1208 | while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { |
Keith Busch | 0df1e4f | 2016-10-11 13:31:58 -0400 | [diff] [blame] | 1209 | if (csts == ~0) |
| 1210 | return -ENODEV; |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1211 | if ((csts & NVME_CSTS_RDY) == bit) |
| 1212 | break; |
| 1213 | |
| 1214 | msleep(100); |
| 1215 | if (fatal_signal_pending(current)) |
| 1216 | return -EINTR; |
| 1217 | if (time_after(jiffies, timeout)) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1218 | dev_err(ctrl->device, |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1219 | "Device not ready; aborting %s\n", enabled ? |
| 1220 | "initialisation" : "reset"); |
| 1221 | return -ENODEV; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | return ret; |
| 1226 | } |
| 1227 | |
| 1228 | /* |
| 1229 | * If the device has been passed off to us in an enabled state, just clear |
| 1230 | * the enabled bit. The spec says we should set the 'shutdown notification |
| 1231 | * bits', but doing so may cause the device to complete commands to the |
| 1232 | * admin queue ... and we don't know what memory that might be pointing at! |
| 1233 | */ |
| 1234 | int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap) |
| 1235 | { |
| 1236 | int ret; |
| 1237 | |
| 1238 | ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; |
| 1239 | ctrl->ctrl_config &= ~NVME_CC_ENABLE; |
| 1240 | |
| 1241 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); |
| 1242 | if (ret) |
| 1243 | return ret; |
Guilherme G. Piccoli | 54adc01 | 2016-06-14 18:22:41 -0300 | [diff] [blame] | 1244 | |
Guilherme G. Piccoli | b5a10c5 | 2016-12-28 22:13:15 -0200 | [diff] [blame] | 1245 | if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY) |
Guilherme G. Piccoli | 54adc01 | 2016-06-14 18:22:41 -0300 | [diff] [blame] | 1246 | msleep(NVME_QUIRK_DELAY_AMOUNT); |
| 1247 | |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1248 | return nvme_wait_ready(ctrl, cap, false); |
| 1249 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1250 | EXPORT_SYMBOL_GPL(nvme_disable_ctrl); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1251 | |
| 1252 | int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap) |
| 1253 | { |
| 1254 | /* |
| 1255 | * Default to a 4K page size, with the intention to update this |
| 1256 | * path in the future to accomodate architectures with differing |
| 1257 | * kernel and IO page sizes. |
| 1258 | */ |
| 1259 | unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12; |
| 1260 | int ret; |
| 1261 | |
| 1262 | if (page_shift < dev_page_min) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1263 | dev_err(ctrl->device, |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1264 | "Minimum device page size %u too large for host (%u)\n", |
| 1265 | 1 << dev_page_min, 1 << page_shift); |
| 1266 | return -ENODEV; |
| 1267 | } |
| 1268 | |
| 1269 | ctrl->page_size = 1 << page_shift; |
| 1270 | |
| 1271 | ctrl->ctrl_config = NVME_CC_CSS_NVM; |
| 1272 | ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT; |
| 1273 | ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE; |
| 1274 | ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; |
| 1275 | ctrl->ctrl_config |= NVME_CC_ENABLE; |
| 1276 | |
| 1277 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); |
| 1278 | if (ret) |
| 1279 | return ret; |
| 1280 | return nvme_wait_ready(ctrl, cap, true); |
| 1281 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1282 | EXPORT_SYMBOL_GPL(nvme_enable_ctrl); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1283 | |
| 1284 | int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl) |
| 1285 | { |
| 1286 | unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies; |
| 1287 | u32 csts; |
| 1288 | int ret; |
| 1289 | |
| 1290 | ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; |
| 1291 | ctrl->ctrl_config |= NVME_CC_SHN_NORMAL; |
| 1292 | |
| 1293 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); |
| 1294 | if (ret) |
| 1295 | return ret; |
| 1296 | |
| 1297 | while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { |
| 1298 | if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT) |
| 1299 | break; |
| 1300 | |
| 1301 | msleep(100); |
| 1302 | if (fatal_signal_pending(current)) |
| 1303 | return -EINTR; |
| 1304 | if (time_after(jiffies, timeout)) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1305 | dev_err(ctrl->device, |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1306 | "Device shutdown incomplete; abort shutdown\n"); |
| 1307 | return -ENODEV; |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | return ret; |
| 1312 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1313 | EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1314 | |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1315 | static void nvme_set_queue_limits(struct nvme_ctrl *ctrl, |
| 1316 | struct request_queue *q) |
| 1317 | { |
Jens Axboe | 7c88cb0 | 2016-04-12 15:43:09 -0600 | [diff] [blame] | 1318 | bool vwc = false; |
| 1319 | |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1320 | if (ctrl->max_hw_sectors) { |
Christoph Hellwig | 45686b6 | 2016-03-02 18:07:12 +0100 | [diff] [blame] | 1321 | u32 max_segments = |
| 1322 | (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1; |
| 1323 | |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1324 | blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors); |
Christoph Hellwig | 45686b6 | 2016-03-02 18:07:12 +0100 | [diff] [blame] | 1325 | blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX)); |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1326 | } |
Keith Busch | e6282ae | 2016-12-19 11:37:50 -0500 | [diff] [blame] | 1327 | if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) |
| 1328 | blk_queue_chunk_sectors(q, ctrl->max_hw_sectors); |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1329 | blk_queue_virt_boundary(q, ctrl->page_size - 1); |
Jens Axboe | 7c88cb0 | 2016-04-12 15:43:09 -0600 | [diff] [blame] | 1330 | if (ctrl->vwc & NVME_CTRL_VWC_PRESENT) |
| 1331 | vwc = true; |
| 1332 | blk_queue_write_cache(q, vwc, vwc); |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1333 | } |
| 1334 | |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1335 | static void nvme_configure_apst(struct nvme_ctrl *ctrl) |
| 1336 | { |
| 1337 | /* |
| 1338 | * APST (Autonomous Power State Transition) lets us program a |
| 1339 | * table of power state transitions that the controller will |
| 1340 | * perform automatically. We configure it with a simple |
| 1341 | * heuristic: we are willing to spend at most 2% of the time |
| 1342 | * transitioning between power states. Therefore, when running |
| 1343 | * in any given state, we will enter the next lower-power |
Andy Lutomirski | 76e4ad0 | 2017-04-21 16:19:22 -0700 | [diff] [blame] | 1344 | * non-operational state after waiting 50 * (enlat + exlat) |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1345 | * microseconds, as long as that state's total latency is under |
| 1346 | * the requested maximum latency. |
| 1347 | * |
| 1348 | * We will not autonomously enter any non-operational state for |
| 1349 | * which the total latency exceeds ps_max_latency_us. Users |
| 1350 | * can set ps_max_latency_us to zero to turn off APST. |
| 1351 | */ |
| 1352 | |
| 1353 | unsigned apste; |
| 1354 | struct nvme_feat_auto_pst *table; |
Andy Lutomirski | fb0dc39 | 2017-04-21 16:19:23 -0700 | [diff] [blame] | 1355 | u64 max_lat_us = 0; |
| 1356 | int max_ps = -1; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1357 | int ret; |
| 1358 | |
| 1359 | /* |
| 1360 | * If APST isn't supported or if we haven't been initialized yet, |
| 1361 | * then don't do anything. |
| 1362 | */ |
| 1363 | if (!ctrl->apsta) |
| 1364 | return; |
| 1365 | |
| 1366 | if (ctrl->npss > 31) { |
| 1367 | dev_warn(ctrl->device, "NPSS is invalid; not using APST\n"); |
| 1368 | return; |
| 1369 | } |
| 1370 | |
| 1371 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
| 1372 | if (!table) |
| 1373 | return; |
| 1374 | |
| 1375 | if (ctrl->ps_max_latency_us == 0) { |
| 1376 | /* Turn off APST. */ |
| 1377 | apste = 0; |
Andy Lutomirski | fb0dc39 | 2017-04-21 16:19:23 -0700 | [diff] [blame] | 1378 | dev_dbg(ctrl->device, "APST disabled\n"); |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1379 | } else { |
| 1380 | __le64 target = cpu_to_le64(0); |
| 1381 | int state; |
| 1382 | |
| 1383 | /* |
| 1384 | * Walk through all states from lowest- to highest-power. |
| 1385 | * According to the spec, lower-numbered states use more |
| 1386 | * power. NPSS, despite the name, is the index of the |
| 1387 | * lowest-power state, not the number of states. |
| 1388 | */ |
| 1389 | for (state = (int)ctrl->npss; state >= 0; state--) { |
| 1390 | u64 total_latency_us, transition_ms; |
| 1391 | |
| 1392 | if (target) |
| 1393 | table->entries[state] = target; |
| 1394 | |
| 1395 | /* |
Andy Lutomirski | ff5350a | 2017-04-20 13:37:55 -0700 | [diff] [blame] | 1396 | * Don't allow transitions to the deepest state |
| 1397 | * if it's quirked off. |
| 1398 | */ |
| 1399 | if (state == ctrl->npss && |
| 1400 | (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) |
| 1401 | continue; |
| 1402 | |
| 1403 | /* |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1404 | * Is this state a useful non-operational state for |
| 1405 | * higher-power states to autonomously transition to? |
| 1406 | */ |
| 1407 | if (!(ctrl->psd[state].flags & |
| 1408 | NVME_PS_FLAGS_NON_OP_STATE)) |
| 1409 | continue; |
| 1410 | |
| 1411 | total_latency_us = |
| 1412 | (u64)le32_to_cpu(ctrl->psd[state].entry_lat) + |
| 1413 | + le32_to_cpu(ctrl->psd[state].exit_lat); |
| 1414 | if (total_latency_us > ctrl->ps_max_latency_us) |
| 1415 | continue; |
| 1416 | |
| 1417 | /* |
| 1418 | * This state is good. Use it as the APST idle |
| 1419 | * target for higher power states. |
| 1420 | */ |
| 1421 | transition_ms = total_latency_us + 19; |
| 1422 | do_div(transition_ms, 20); |
| 1423 | if (transition_ms > (1 << 24) - 1) |
| 1424 | transition_ms = (1 << 24) - 1; |
| 1425 | |
| 1426 | target = cpu_to_le64((state << 3) | |
| 1427 | (transition_ms << 8)); |
Andy Lutomirski | fb0dc39 | 2017-04-21 16:19:23 -0700 | [diff] [blame] | 1428 | |
| 1429 | if (max_ps == -1) |
| 1430 | max_ps = state; |
| 1431 | |
| 1432 | if (total_latency_us > max_lat_us) |
| 1433 | max_lat_us = total_latency_us; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | apste = 1; |
Andy Lutomirski | fb0dc39 | 2017-04-21 16:19:23 -0700 | [diff] [blame] | 1437 | |
| 1438 | if (max_ps == -1) { |
| 1439 | dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n"); |
| 1440 | } else { |
| 1441 | dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n", |
| 1442 | max_ps, max_lat_us, (int)sizeof(*table), table); |
| 1443 | } |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste, |
| 1447 | table, sizeof(*table), NULL); |
| 1448 | if (ret) |
| 1449 | dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret); |
| 1450 | |
| 1451 | kfree(table); |
| 1452 | } |
| 1453 | |
| 1454 | static void nvme_set_latency_tolerance(struct device *dev, s32 val) |
| 1455 | { |
| 1456 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 1457 | u64 latency; |
| 1458 | |
| 1459 | switch (val) { |
| 1460 | case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT: |
| 1461 | case PM_QOS_LATENCY_ANY: |
| 1462 | latency = U64_MAX; |
| 1463 | break; |
| 1464 | |
| 1465 | default: |
| 1466 | latency = val; |
| 1467 | } |
| 1468 | |
| 1469 | if (ctrl->ps_max_latency_us != latency) { |
| 1470 | ctrl->ps_max_latency_us = latency; |
| 1471 | nvme_configure_apst(ctrl); |
| 1472 | } |
| 1473 | } |
| 1474 | |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 1475 | struct nvme_core_quirk_entry { |
| 1476 | /* |
| 1477 | * NVMe model and firmware strings are padded with spaces. For |
| 1478 | * simplicity, strings in the quirk table are padded with NULLs |
| 1479 | * instead. |
| 1480 | */ |
| 1481 | u16 vid; |
| 1482 | const char *mn; |
| 1483 | const char *fr; |
| 1484 | unsigned long quirks; |
| 1485 | }; |
| 1486 | |
| 1487 | static const struct nvme_core_quirk_entry core_quirks[] = { |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1488 | { |
Andy Lutomirski | be56945 | 2017-04-20 13:37:56 -0700 | [diff] [blame] | 1489 | /* |
| 1490 | * This Toshiba device seems to die using any APST states. See: |
| 1491 | * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11 |
| 1492 | */ |
| 1493 | .vid = 0x1179, |
| 1494 | .mn = "THNSF5256GPUK TOSHIBA", |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1495 | .quirks = NVME_QUIRK_NO_APST, |
Andy Lutomirski | be56945 | 2017-04-20 13:37:56 -0700 | [diff] [blame] | 1496 | } |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 1497 | }; |
| 1498 | |
| 1499 | /* match is null-terminated but idstr is space-padded. */ |
| 1500 | static bool string_matches(const char *idstr, const char *match, size_t len) |
| 1501 | { |
| 1502 | size_t matchlen; |
| 1503 | |
| 1504 | if (!match) |
| 1505 | return true; |
| 1506 | |
| 1507 | matchlen = strlen(match); |
| 1508 | WARN_ON_ONCE(matchlen > len); |
| 1509 | |
| 1510 | if (memcmp(idstr, match, matchlen)) |
| 1511 | return false; |
| 1512 | |
| 1513 | for (; matchlen < len; matchlen++) |
| 1514 | if (idstr[matchlen] != ' ') |
| 1515 | return false; |
| 1516 | |
| 1517 | return true; |
| 1518 | } |
| 1519 | |
| 1520 | static bool quirk_matches(const struct nvme_id_ctrl *id, |
| 1521 | const struct nvme_core_quirk_entry *q) |
| 1522 | { |
| 1523 | return q->vid == le16_to_cpu(id->vid) && |
| 1524 | string_matches(id->mn, q->mn, sizeof(id->mn)) && |
| 1525 | string_matches(id->fr, q->fr, sizeof(id->fr)); |
| 1526 | } |
| 1527 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 1528 | /* |
| 1529 | * Initialize the cached copies of the Identify data and various controller |
| 1530 | * register in our nvme_ctrl structure. This should be called as soon as |
| 1531 | * the admin queue is fully up and running. |
| 1532 | */ |
| 1533 | int nvme_init_identify(struct nvme_ctrl *ctrl) |
| 1534 | { |
| 1535 | struct nvme_id_ctrl *id; |
| 1536 | u64 cap; |
| 1537 | int ret, page_shift; |
Christoph Hellwig | a229dbf | 2016-06-06 23:20:48 +0200 | [diff] [blame] | 1538 | u32 max_hw_sectors; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1539 | u8 prev_apsta; |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 1540 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1541 | ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs); |
| 1542 | if (ret) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1543 | dev_err(ctrl->device, "Reading VS failed (%d)\n", ret); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1544 | return ret; |
| 1545 | } |
| 1546 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 1547 | ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap); |
| 1548 | if (ret) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1549 | dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 1550 | return ret; |
| 1551 | } |
| 1552 | page_shift = NVME_CAP_MPSMIN(cap) + 12; |
| 1553 | |
Gabriel Krisman Bertazi | 8ef2074 | 2016-10-19 09:51:05 -0600 | [diff] [blame] | 1554 | if (ctrl->vs >= NVME_VS(1, 1, 0)) |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1555 | ctrl->subsystem = NVME_CAP_NSSRC(cap); |
| 1556 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 1557 | ret = nvme_identify_ctrl(ctrl, &id); |
| 1558 | if (ret) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1559 | dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 1560 | return -EIO; |
| 1561 | } |
| 1562 | |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 1563 | if (!ctrl->identified) { |
| 1564 | /* |
| 1565 | * Check for quirks. Quirk can depend on firmware version, |
| 1566 | * so, in principle, the set of quirks present can change |
| 1567 | * across a reset. As a possible future enhancement, we |
| 1568 | * could re-scan for quirks every time we reinitialize |
| 1569 | * the device, but we'd have to make sure that the driver |
| 1570 | * behaves intelligently if the quirks change. |
| 1571 | */ |
| 1572 | |
| 1573 | int i; |
| 1574 | |
| 1575 | for (i = 0; i < ARRAY_SIZE(core_quirks); i++) { |
| 1576 | if (quirk_matches(id, &core_quirks[i])) |
| 1577 | ctrl->quirks |= core_quirks[i].quirks; |
| 1578 | } |
| 1579 | } |
| 1580 | |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 1581 | if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) { |
| 1582 | dev_warn(ctrl->dev, "forcibly allowing all power states due to nvme_core.force_apst -- use at your own risk\n"); |
| 1583 | ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS; |
| 1584 | } |
| 1585 | |
Scott Bauer | 8a9ae52 | 2017-02-17 13:59:40 +0100 | [diff] [blame] | 1586 | ctrl->oacs = le16_to_cpu(id->oacs); |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 1587 | ctrl->vid = le16_to_cpu(id->vid); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 1588 | ctrl->oncs = le16_to_cpup(&id->oncs); |
Christoph Hellwig | 6bf25d1 | 2015-11-20 09:36:44 +0100 | [diff] [blame] | 1589 | atomic_set(&ctrl->abort_limit, id->acl + 1); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 1590 | ctrl->vwc = id->vwc; |
Ming Lin | 931e1c2 | 2016-02-26 13:24:19 -0800 | [diff] [blame] | 1591 | ctrl->cntlid = le16_to_cpup(&id->cntlid); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 1592 | memcpy(ctrl->serial, id->sn, sizeof(id->sn)); |
| 1593 | memcpy(ctrl->model, id->mn, sizeof(id->mn)); |
| 1594 | memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr)); |
| 1595 | if (id->mdts) |
Christoph Hellwig | a229dbf | 2016-06-06 23:20:48 +0200 | [diff] [blame] | 1596 | max_hw_sectors = 1 << (id->mdts + page_shift - 9); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 1597 | else |
Christoph Hellwig | a229dbf | 2016-06-06 23:20:48 +0200 | [diff] [blame] | 1598 | max_hw_sectors = UINT_MAX; |
| 1599 | ctrl->max_hw_sectors = |
| 1600 | min_not_zero(ctrl->max_hw_sectors, max_hw_sectors); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 1601 | |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1602 | nvme_set_queue_limits(ctrl, ctrl->admin_q); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1603 | ctrl->sgls = le32_to_cpu(id->sgls); |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 1604 | ctrl->kas = le16_to_cpu(id->kas); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1605 | |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1606 | ctrl->npss = id->npss; |
| 1607 | prev_apsta = ctrl->apsta; |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 1608 | if (ctrl->quirks & NVME_QUIRK_NO_APST) { |
| 1609 | if (force_apst && id->apsta) { |
| 1610 | dev_warn(ctrl->dev, "forcibly allowing APST due to nvme_core.force_apst -- use at your own risk\n"); |
| 1611 | ctrl->apsta = 1; |
| 1612 | } else { |
| 1613 | ctrl->apsta = 0; |
| 1614 | } |
| 1615 | } else { |
| 1616 | ctrl->apsta = id->apsta; |
| 1617 | } |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1618 | memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd)); |
| 1619 | |
Christoph Hellwig | d3d5b87 | 2017-05-20 15:14:44 +0200 | [diff] [blame] | 1620 | if (ctrl->ops->flags & NVME_F_FABRICS) { |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1621 | ctrl->icdoff = le16_to_cpu(id->icdoff); |
| 1622 | ctrl->ioccsz = le32_to_cpu(id->ioccsz); |
| 1623 | ctrl->iorcsz = le32_to_cpu(id->iorcsz); |
| 1624 | ctrl->maxcmd = le16_to_cpu(id->maxcmd); |
| 1625 | |
| 1626 | /* |
| 1627 | * In fabrics we need to verify the cntlid matches the |
| 1628 | * admin connect |
| 1629 | */ |
| 1630 | if (ctrl->cntlid != le16_to_cpu(id->cntlid)) |
| 1631 | ret = -EINVAL; |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 1632 | |
| 1633 | if (!ctrl->opts->discovery_nqn && !ctrl->kas) { |
| 1634 | dev_err(ctrl->dev, |
| 1635 | "keep-alive support is mandatory for fabrics\n"); |
| 1636 | ret = -EINVAL; |
| 1637 | } |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1638 | } else { |
| 1639 | ctrl->cntlid = le16_to_cpu(id->cntlid); |
| 1640 | } |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1641 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 1642 | kfree(id); |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 1643 | |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1644 | if (ctrl->apsta && !prev_apsta) |
| 1645 | dev_pm_qos_expose_latency_tolerance(ctrl->device); |
| 1646 | else if (!ctrl->apsta && prev_apsta) |
| 1647 | dev_pm_qos_hide_latency_tolerance(ctrl->device); |
| 1648 | |
| 1649 | nvme_configure_apst(ctrl); |
| 1650 | |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 1651 | ctrl->identified = true; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1652 | |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 1653 | return ret; |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 1654 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1655 | EXPORT_SYMBOL_GPL(nvme_init_identify); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 1656 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1657 | static int nvme_dev_open(struct inode *inode, struct file *file) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1658 | { |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1659 | struct nvme_ctrl *ctrl; |
| 1660 | int instance = iminor(inode); |
| 1661 | int ret = -ENODEV; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1662 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1663 | spin_lock(&dev_list_lock); |
| 1664 | list_for_each_entry(ctrl, &nvme_ctrl_list, node) { |
| 1665 | if (ctrl->instance != instance) |
| 1666 | continue; |
| 1667 | |
| 1668 | if (!ctrl->admin_q) { |
| 1669 | ret = -EWOULDBLOCK; |
| 1670 | break; |
| 1671 | } |
| 1672 | if (!kref_get_unless_zero(&ctrl->kref)) |
| 1673 | break; |
| 1674 | file->private_data = ctrl; |
| 1675 | ret = 0; |
| 1676 | break; |
| 1677 | } |
| 1678 | spin_unlock(&dev_list_lock); |
| 1679 | |
| 1680 | return ret; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1681 | } |
| 1682 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1683 | static int nvme_dev_release(struct inode *inode, struct file *file) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1684 | { |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1685 | nvme_put_ctrl(file->private_data); |
| 1686 | return 0; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1687 | } |
| 1688 | |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 1689 | static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp) |
| 1690 | { |
| 1691 | struct nvme_ns *ns; |
| 1692 | int ret; |
| 1693 | |
| 1694 | mutex_lock(&ctrl->namespaces_mutex); |
| 1695 | if (list_empty(&ctrl->namespaces)) { |
| 1696 | ret = -ENOTTY; |
| 1697 | goto out_unlock; |
| 1698 | } |
| 1699 | |
| 1700 | ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list); |
| 1701 | if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1702 | dev_warn(ctrl->device, |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 1703 | "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n"); |
| 1704 | ret = -EINVAL; |
| 1705 | goto out_unlock; |
| 1706 | } |
| 1707 | |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1708 | dev_warn(ctrl->device, |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 1709 | "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n"); |
| 1710 | kref_get(&ns->kref); |
| 1711 | mutex_unlock(&ctrl->namespaces_mutex); |
| 1712 | |
| 1713 | ret = nvme_user_cmd(ctrl, ns, argp); |
| 1714 | nvme_put_ns(ns); |
| 1715 | return ret; |
| 1716 | |
| 1717 | out_unlock: |
| 1718 | mutex_unlock(&ctrl->namespaces_mutex); |
| 1719 | return ret; |
| 1720 | } |
| 1721 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1722 | static long nvme_dev_ioctl(struct file *file, unsigned int cmd, |
| 1723 | unsigned long arg) |
| 1724 | { |
| 1725 | struct nvme_ctrl *ctrl = file->private_data; |
| 1726 | void __user *argp = (void __user *)arg; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1727 | |
| 1728 | switch (cmd) { |
| 1729 | case NVME_IOCTL_ADMIN_CMD: |
| 1730 | return nvme_user_cmd(ctrl, NULL, argp); |
| 1731 | case NVME_IOCTL_IO_CMD: |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 1732 | return nvme_dev_user_cmd(ctrl, argp); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1733 | case NVME_IOCTL_RESET: |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1734 | dev_warn(ctrl->device, "resetting controller\n"); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1735 | return ctrl->ops->reset_ctrl(ctrl); |
| 1736 | case NVME_IOCTL_SUBSYS_RESET: |
| 1737 | return nvme_reset_subsystem(ctrl); |
Keith Busch | 9ec3bb2 | 2016-04-29 15:45:18 -0600 | [diff] [blame] | 1738 | case NVME_IOCTL_RESCAN: |
| 1739 | nvme_queue_scan(ctrl); |
| 1740 | return 0; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1741 | default: |
| 1742 | return -ENOTTY; |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | static const struct file_operations nvme_dev_fops = { |
| 1747 | .owner = THIS_MODULE, |
| 1748 | .open = nvme_dev_open, |
| 1749 | .release = nvme_dev_release, |
| 1750 | .unlocked_ioctl = nvme_dev_ioctl, |
| 1751 | .compat_ioctl = nvme_dev_ioctl, |
| 1752 | }; |
| 1753 | |
| 1754 | static ssize_t nvme_sysfs_reset(struct device *dev, |
| 1755 | struct device_attribute *attr, const char *buf, |
| 1756 | size_t count) |
| 1757 | { |
| 1758 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 1759 | int ret; |
| 1760 | |
| 1761 | ret = ctrl->ops->reset_ctrl(ctrl); |
| 1762 | if (ret < 0) |
| 1763 | return ret; |
| 1764 | return count; |
| 1765 | } |
| 1766 | static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset); |
| 1767 | |
Keith Busch | 9ec3bb2 | 2016-04-29 15:45:18 -0600 | [diff] [blame] | 1768 | static ssize_t nvme_sysfs_rescan(struct device *dev, |
| 1769 | struct device_attribute *attr, const char *buf, |
| 1770 | size_t count) |
| 1771 | { |
| 1772 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 1773 | |
| 1774 | nvme_queue_scan(ctrl); |
| 1775 | return count; |
| 1776 | } |
| 1777 | static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan); |
| 1778 | |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 1779 | static ssize_t wwid_show(struct device *dev, struct device_attribute *attr, |
| 1780 | char *buf) |
| 1781 | { |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 1782 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 1783 | struct nvme_ctrl *ctrl = ns->ctrl; |
| 1784 | int serial_len = sizeof(ctrl->serial); |
| 1785 | int model_len = sizeof(ctrl->model); |
| 1786 | |
| 1787 | if (memchr_inv(ns->uuid, 0, sizeof(ns->uuid))) |
| 1788 | return sprintf(buf, "eui.%16phN\n", ns->uuid); |
| 1789 | |
| 1790 | if (memchr_inv(ns->eui, 0, sizeof(ns->eui))) |
| 1791 | return sprintf(buf, "eui.%8phN\n", ns->eui); |
| 1792 | |
| 1793 | while (ctrl->serial[serial_len - 1] == ' ') |
| 1794 | serial_len--; |
| 1795 | while (ctrl->model[model_len - 1] == ' ') |
| 1796 | model_len--; |
| 1797 | |
| 1798 | return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid, |
| 1799 | serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id); |
| 1800 | } |
| 1801 | static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL); |
| 1802 | |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 1803 | static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, |
| 1804 | char *buf) |
| 1805 | { |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 1806 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 1807 | return sprintf(buf, "%pU\n", ns->uuid); |
| 1808 | } |
| 1809 | static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL); |
| 1810 | |
| 1811 | static ssize_t eui_show(struct device *dev, struct device_attribute *attr, |
| 1812 | char *buf) |
| 1813 | { |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 1814 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 1815 | return sprintf(buf, "%8phd\n", ns->eui); |
| 1816 | } |
| 1817 | static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL); |
| 1818 | |
| 1819 | static ssize_t nsid_show(struct device *dev, struct device_attribute *attr, |
| 1820 | char *buf) |
| 1821 | { |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 1822 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 1823 | return sprintf(buf, "%d\n", ns->ns_id); |
| 1824 | } |
| 1825 | static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL); |
| 1826 | |
| 1827 | static struct attribute *nvme_ns_attrs[] = { |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 1828 | &dev_attr_wwid.attr, |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 1829 | &dev_attr_uuid.attr, |
| 1830 | &dev_attr_eui.attr, |
| 1831 | &dev_attr_nsid.attr, |
| 1832 | NULL, |
| 1833 | }; |
| 1834 | |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 1835 | static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj, |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 1836 | struct attribute *a, int n) |
| 1837 | { |
| 1838 | struct device *dev = container_of(kobj, struct device, kobj); |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 1839 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 1840 | |
| 1841 | if (a == &dev_attr_uuid.attr) { |
| 1842 | if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid))) |
| 1843 | return 0; |
| 1844 | } |
| 1845 | if (a == &dev_attr_eui.attr) { |
| 1846 | if (!memchr_inv(ns->eui, 0, sizeof(ns->eui))) |
| 1847 | return 0; |
| 1848 | } |
| 1849 | return a->mode; |
| 1850 | } |
| 1851 | |
| 1852 | static const struct attribute_group nvme_ns_attr_group = { |
| 1853 | .attrs = nvme_ns_attrs, |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 1854 | .is_visible = nvme_ns_attrs_are_visible, |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 1855 | }; |
| 1856 | |
Ming Lin | 931e1c2 | 2016-02-26 13:24:19 -0800 | [diff] [blame] | 1857 | #define nvme_show_str_function(field) \ |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 1858 | static ssize_t field##_show(struct device *dev, \ |
| 1859 | struct device_attribute *attr, char *buf) \ |
| 1860 | { \ |
| 1861 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ |
| 1862 | return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \ |
| 1863 | } \ |
| 1864 | static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); |
| 1865 | |
Ming Lin | 931e1c2 | 2016-02-26 13:24:19 -0800 | [diff] [blame] | 1866 | #define nvme_show_int_function(field) \ |
| 1867 | static ssize_t field##_show(struct device *dev, \ |
| 1868 | struct device_attribute *attr, char *buf) \ |
| 1869 | { \ |
| 1870 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ |
| 1871 | return sprintf(buf, "%d\n", ctrl->field); \ |
| 1872 | } \ |
| 1873 | static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); |
| 1874 | |
| 1875 | nvme_show_str_function(model); |
| 1876 | nvme_show_str_function(serial); |
| 1877 | nvme_show_str_function(firmware_rev); |
| 1878 | nvme_show_int_function(cntlid); |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 1879 | |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 1880 | static ssize_t nvme_sysfs_delete(struct device *dev, |
| 1881 | struct device_attribute *attr, const char *buf, |
| 1882 | size_t count) |
| 1883 | { |
| 1884 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 1885 | |
| 1886 | if (device_remove_file_self(dev, attr)) |
| 1887 | ctrl->ops->delete_ctrl(ctrl); |
| 1888 | return count; |
| 1889 | } |
| 1890 | static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete); |
| 1891 | |
| 1892 | static ssize_t nvme_sysfs_show_transport(struct device *dev, |
| 1893 | struct device_attribute *attr, |
| 1894 | char *buf) |
| 1895 | { |
| 1896 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 1897 | |
| 1898 | return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name); |
| 1899 | } |
| 1900 | static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL); |
| 1901 | |
Sagi Grimberg | 8432bdb2 | 2016-11-28 01:47:40 +0200 | [diff] [blame] | 1902 | static ssize_t nvme_sysfs_show_state(struct device *dev, |
| 1903 | struct device_attribute *attr, |
| 1904 | char *buf) |
| 1905 | { |
| 1906 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 1907 | static const char *const state_name[] = { |
| 1908 | [NVME_CTRL_NEW] = "new", |
| 1909 | [NVME_CTRL_LIVE] = "live", |
| 1910 | [NVME_CTRL_RESETTING] = "resetting", |
| 1911 | [NVME_CTRL_RECONNECTING]= "reconnecting", |
| 1912 | [NVME_CTRL_DELETING] = "deleting", |
| 1913 | [NVME_CTRL_DEAD] = "dead", |
| 1914 | }; |
| 1915 | |
| 1916 | if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) && |
| 1917 | state_name[ctrl->state]) |
| 1918 | return sprintf(buf, "%s\n", state_name[ctrl->state]); |
| 1919 | |
| 1920 | return sprintf(buf, "unknown state\n"); |
| 1921 | } |
| 1922 | |
| 1923 | static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL); |
| 1924 | |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 1925 | static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev, |
| 1926 | struct device_attribute *attr, |
| 1927 | char *buf) |
| 1928 | { |
| 1929 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 1930 | |
| 1931 | return snprintf(buf, PAGE_SIZE, "%s\n", |
| 1932 | ctrl->ops->get_subsysnqn(ctrl)); |
| 1933 | } |
| 1934 | static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL); |
| 1935 | |
| 1936 | static ssize_t nvme_sysfs_show_address(struct device *dev, |
| 1937 | struct device_attribute *attr, |
| 1938 | char *buf) |
| 1939 | { |
| 1940 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 1941 | |
| 1942 | return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE); |
| 1943 | } |
| 1944 | static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL); |
| 1945 | |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 1946 | static struct attribute *nvme_dev_attrs[] = { |
| 1947 | &dev_attr_reset_controller.attr, |
Keith Busch | 9ec3bb2 | 2016-04-29 15:45:18 -0600 | [diff] [blame] | 1948 | &dev_attr_rescan_controller.attr, |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 1949 | &dev_attr_model.attr, |
| 1950 | &dev_attr_serial.attr, |
| 1951 | &dev_attr_firmware_rev.attr, |
Ming Lin | 931e1c2 | 2016-02-26 13:24:19 -0800 | [diff] [blame] | 1952 | &dev_attr_cntlid.attr, |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 1953 | &dev_attr_delete_controller.attr, |
| 1954 | &dev_attr_transport.attr, |
| 1955 | &dev_attr_subsysnqn.attr, |
| 1956 | &dev_attr_address.attr, |
Sagi Grimberg | 8432bdb2 | 2016-11-28 01:47:40 +0200 | [diff] [blame] | 1957 | &dev_attr_state.attr, |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 1958 | NULL |
| 1959 | }; |
| 1960 | |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 1961 | #define CHECK_ATTR(ctrl, a, name) \ |
| 1962 | if ((a) == &dev_attr_##name.attr && \ |
| 1963 | !(ctrl)->ops->get_##name) \ |
| 1964 | return 0 |
| 1965 | |
| 1966 | static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj, |
| 1967 | struct attribute *a, int n) |
| 1968 | { |
| 1969 | struct device *dev = container_of(kobj, struct device, kobj); |
| 1970 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 1971 | |
| 1972 | if (a == &dev_attr_delete_controller.attr) { |
| 1973 | if (!ctrl->ops->delete_ctrl) |
| 1974 | return 0; |
| 1975 | } |
| 1976 | |
| 1977 | CHECK_ATTR(ctrl, a, subsysnqn); |
| 1978 | CHECK_ATTR(ctrl, a, address); |
| 1979 | |
| 1980 | return a->mode; |
| 1981 | } |
| 1982 | |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 1983 | static struct attribute_group nvme_dev_attrs_group = { |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 1984 | .attrs = nvme_dev_attrs, |
| 1985 | .is_visible = nvme_dev_attrs_are_visible, |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 1986 | }; |
| 1987 | |
| 1988 | static const struct attribute_group *nvme_dev_attr_groups[] = { |
| 1989 | &nvme_dev_attrs_group, |
| 1990 | NULL, |
| 1991 | }; |
| 1992 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1993 | static int ns_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1994 | { |
| 1995 | struct nvme_ns *nsa = container_of(a, struct nvme_ns, list); |
| 1996 | struct nvme_ns *nsb = container_of(b, struct nvme_ns, list); |
| 1997 | |
| 1998 | return nsa->ns_id - nsb->ns_id; |
| 1999 | } |
| 2000 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2001 | static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid) |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2002 | { |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2003 | struct nvme_ns *ns, *ret = NULL; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2004 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2005 | mutex_lock(&ctrl->namespaces_mutex); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2006 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2007 | if (ns->ns_id == nsid) { |
| 2008 | kref_get(&ns->kref); |
| 2009 | ret = ns; |
| 2010 | break; |
| 2011 | } |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2012 | if (ns->ns_id > nsid) |
| 2013 | break; |
| 2014 | } |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2015 | mutex_unlock(&ctrl->namespaces_mutex); |
| 2016 | return ret; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid) |
| 2020 | { |
| 2021 | struct nvme_ns *ns; |
| 2022 | struct gendisk *disk; |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 2023 | struct nvme_id_ns *id; |
| 2024 | char disk_name[DISK_NAME_LEN]; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2025 | int node = dev_to_node(ctrl->dev); |
| 2026 | |
| 2027 | ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node); |
| 2028 | if (!ns) |
| 2029 | return; |
| 2030 | |
Keith Busch | 075790e | 2016-02-24 09:15:53 -0700 | [diff] [blame] | 2031 | ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL); |
| 2032 | if (ns->instance < 0) |
| 2033 | goto out_free_ns; |
| 2034 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2035 | ns->queue = blk_mq_init_queue(ctrl->tagset); |
| 2036 | if (IS_ERR(ns->queue)) |
Keith Busch | 075790e | 2016-02-24 09:15:53 -0700 | [diff] [blame] | 2037 | goto out_release_instance; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2038 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue); |
| 2039 | ns->queue->queuedata = ns; |
| 2040 | ns->ctrl = ctrl; |
| 2041 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2042 | kref_init(&ns->kref); |
| 2043 | ns->ns_id = nsid; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2044 | 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] | 2045 | |
| 2046 | blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift); |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 2047 | nvme_set_queue_limits(ctrl, ns->queue); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2048 | |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 2049 | sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2050 | |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 2051 | if (nvme_revalidate_ns(ns, &id)) |
| 2052 | goto out_free_queue; |
| 2053 | |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 2054 | if (nvme_nvm_ns_supported(ns, id) && |
| 2055 | nvme_nvm_register(ns, disk_name, node)) { |
| 2056 | dev_warn(ctrl->dev, "%s: LightNVM init failure\n", __func__); |
| 2057 | goto out_free_id; |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 2058 | } |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2059 | |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 2060 | disk = alloc_disk_node(0, node); |
| 2061 | if (!disk) |
| 2062 | goto out_free_id; |
| 2063 | |
| 2064 | disk->fops = &nvme_fops; |
| 2065 | disk->private_data = ns; |
| 2066 | disk->queue = ns->queue; |
| 2067 | disk->flags = GENHD_FL_EXT_DEVT; |
| 2068 | memcpy(disk->disk_name, disk_name, DISK_NAME_LEN); |
| 2069 | ns->disk = disk; |
| 2070 | |
| 2071 | __nvme_revalidate_disk(disk, id); |
| 2072 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2073 | mutex_lock(&ctrl->namespaces_mutex); |
| 2074 | list_add_tail(&ns->list, &ctrl->namespaces); |
| 2075 | mutex_unlock(&ctrl->namespaces_mutex); |
| 2076 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2077 | kref_get(&ctrl->kref); |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 2078 | |
| 2079 | kfree(id); |
| 2080 | |
Dan Williams | 0d52c756 | 2016-06-15 19:44:20 -0700 | [diff] [blame] | 2081 | device_add_disk(ctrl->device, ns->disk); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2082 | if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj, |
| 2083 | &nvme_ns_attr_group)) |
| 2084 | pr_warn("%s: failed to create sysfs group for identification\n", |
| 2085 | ns->disk->disk_name); |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 2086 | if (ns->ndev && nvme_nvm_register_sysfs(ns)) |
| 2087 | pr_warn("%s: failed to register lightnvm sysfs group for identification\n", |
| 2088 | ns->disk->disk_name); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2089 | return; |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 2090 | out_free_id: |
| 2091 | kfree(id); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2092 | out_free_queue: |
| 2093 | blk_cleanup_queue(ns->queue); |
Keith Busch | 075790e | 2016-02-24 09:15:53 -0700 | [diff] [blame] | 2094 | out_release_instance: |
| 2095 | ida_simple_remove(&ctrl->ns_ida, ns->instance); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2096 | out_free_ns: |
| 2097 | kfree(ns); |
| 2098 | } |
| 2099 | |
| 2100 | static void nvme_ns_remove(struct nvme_ns *ns) |
| 2101 | { |
Keith Busch | 646017a | 2016-02-24 09:15:54 -0700 | [diff] [blame] | 2102 | if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags)) |
| 2103 | return; |
| 2104 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 2105 | if (ns->disk && ns->disk->flags & GENHD_FL_UP) { |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2106 | if (blk_get_integrity(ns->disk)) |
| 2107 | blk_integrity_unregister(ns->disk); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2108 | sysfs_remove_group(&disk_to_dev(ns->disk)->kobj, |
| 2109 | &nvme_ns_attr_group); |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 2110 | if (ns->ndev) |
| 2111 | nvme_nvm_unregister_sysfs(ns); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2112 | del_gendisk(ns->disk); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2113 | blk_cleanup_queue(ns->queue); |
| 2114 | } |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2115 | |
| 2116 | mutex_lock(&ns->ctrl->namespaces_mutex); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2117 | list_del_init(&ns->list); |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2118 | mutex_unlock(&ns->ctrl->namespaces_mutex); |
| 2119 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2120 | nvme_put_ns(ns); |
| 2121 | } |
| 2122 | |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2123 | static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid) |
| 2124 | { |
| 2125 | struct nvme_ns *ns; |
| 2126 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2127 | ns = nvme_find_get_ns(ctrl, nsid); |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2128 | if (ns) { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 2129 | if (ns->disk && revalidate_disk(ns->disk)) |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2130 | nvme_ns_remove(ns); |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2131 | nvme_put_ns(ns); |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2132 | } else |
| 2133 | nvme_alloc_ns(ctrl, nsid); |
| 2134 | } |
| 2135 | |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 2136 | static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl, |
| 2137 | unsigned nsid) |
| 2138 | { |
| 2139 | struct nvme_ns *ns, *next; |
| 2140 | |
| 2141 | list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) { |
| 2142 | if (ns->ns_id > nsid) |
| 2143 | nvme_ns_remove(ns); |
| 2144 | } |
| 2145 | } |
| 2146 | |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2147 | static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn) |
| 2148 | { |
| 2149 | struct nvme_ns *ns; |
| 2150 | __le32 *ns_list; |
| 2151 | unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024); |
| 2152 | int ret = 0; |
| 2153 | |
| 2154 | ns_list = kzalloc(0x1000, GFP_KERNEL); |
| 2155 | if (!ns_list) |
| 2156 | return -ENOMEM; |
| 2157 | |
| 2158 | for (i = 0; i < num_lists; i++) { |
| 2159 | ret = nvme_identify_ns_list(ctrl, prev, ns_list); |
| 2160 | if (ret) |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 2161 | goto free; |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2162 | |
| 2163 | for (j = 0; j < min(nn, 1024U); j++) { |
| 2164 | nsid = le32_to_cpu(ns_list[j]); |
| 2165 | if (!nsid) |
| 2166 | goto out; |
| 2167 | |
| 2168 | nvme_validate_ns(ctrl, nsid); |
| 2169 | |
| 2170 | while (++prev < nsid) { |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2171 | ns = nvme_find_get_ns(ctrl, prev); |
| 2172 | if (ns) { |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2173 | nvme_ns_remove(ns); |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2174 | nvme_put_ns(ns); |
| 2175 | } |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2176 | } |
| 2177 | } |
| 2178 | nn -= j; |
| 2179 | } |
| 2180 | out: |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 2181 | nvme_remove_invalid_namespaces(ctrl, prev); |
| 2182 | free: |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2183 | kfree(ns_list); |
| 2184 | return ret; |
| 2185 | } |
| 2186 | |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 2187 | static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn) |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2188 | { |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2189 | unsigned i; |
| 2190 | |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2191 | for (i = 1; i <= nn; i++) |
| 2192 | nvme_validate_ns(ctrl, i); |
| 2193 | |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 2194 | nvme_remove_invalid_namespaces(ctrl, nn); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2195 | } |
| 2196 | |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 2197 | static void nvme_scan_work(struct work_struct *work) |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2198 | { |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 2199 | struct nvme_ctrl *ctrl = |
| 2200 | container_of(work, struct nvme_ctrl, scan_work); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2201 | struct nvme_id_ctrl *id; |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2202 | unsigned nn; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2203 | |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 2204 | if (ctrl->state != NVME_CTRL_LIVE) |
| 2205 | return; |
| 2206 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2207 | if (nvme_identify_ctrl(ctrl, &id)) |
| 2208 | return; |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2209 | |
| 2210 | nn = le32_to_cpu(id->nn); |
Gabriel Krisman Bertazi | 8ef2074 | 2016-10-19 09:51:05 -0600 | [diff] [blame] | 2211 | if (ctrl->vs >= NVME_VS(1, 1, 0) && |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2212 | !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) { |
| 2213 | if (!nvme_scan_ns_list(ctrl, nn)) |
| 2214 | goto done; |
| 2215 | } |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 2216 | nvme_scan_ns_sequential(ctrl, nn); |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2217 | done: |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2218 | mutex_lock(&ctrl->namespaces_mutex); |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2219 | list_sort(NULL, &ctrl->namespaces, ns_cmp); |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 2220 | mutex_unlock(&ctrl->namespaces_mutex); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2221 | kfree(id); |
| 2222 | } |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 2223 | |
| 2224 | void nvme_queue_scan(struct nvme_ctrl *ctrl) |
| 2225 | { |
| 2226 | /* |
| 2227 | * Do not queue new scan work when a controller is reset during |
| 2228 | * removal. |
| 2229 | */ |
| 2230 | if (ctrl->state == NVME_CTRL_LIVE) |
| 2231 | schedule_work(&ctrl->scan_work); |
| 2232 | } |
| 2233 | EXPORT_SYMBOL_GPL(nvme_queue_scan); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2234 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2235 | /* |
| 2236 | * This function iterates the namespace list unlocked to allow recovery from |
| 2237 | * controller failure. It is up to the caller to ensure the namespace list is |
| 2238 | * not modified by scan work while this function is executing. |
| 2239 | */ |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2240 | void nvme_remove_namespaces(struct nvme_ctrl *ctrl) |
| 2241 | { |
| 2242 | struct nvme_ns *ns, *next; |
| 2243 | |
Keith Busch | 0ff9d4e | 2016-05-12 08:37:14 -0600 | [diff] [blame] | 2244 | /* |
| 2245 | * The dead states indicates the controller was not gracefully |
| 2246 | * disconnected. In that case, we won't be able to flush any data while |
| 2247 | * removing the namespaces' disks; fail all the queues now to avoid |
| 2248 | * potentially having to clean up the failed sync later. |
| 2249 | */ |
| 2250 | if (ctrl->state == NVME_CTRL_DEAD) |
| 2251 | nvme_kill_queues(ctrl); |
| 2252 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2253 | list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) |
| 2254 | nvme_ns_remove(ns); |
| 2255 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 2256 | EXPORT_SYMBOL_GPL(nvme_remove_namespaces); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2257 | |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 2258 | static void nvme_async_event_work(struct work_struct *work) |
| 2259 | { |
| 2260 | struct nvme_ctrl *ctrl = |
| 2261 | container_of(work, struct nvme_ctrl, async_event_work); |
| 2262 | |
| 2263 | spin_lock_irq(&ctrl->lock); |
| 2264 | while (ctrl->event_limit > 0) { |
| 2265 | int aer_idx = --ctrl->event_limit; |
| 2266 | |
| 2267 | spin_unlock_irq(&ctrl->lock); |
| 2268 | ctrl->ops->submit_async_event(ctrl, aer_idx); |
| 2269 | spin_lock_irq(&ctrl->lock); |
| 2270 | } |
| 2271 | spin_unlock_irq(&ctrl->lock); |
| 2272 | } |
| 2273 | |
Christoph Hellwig | 7bf5853 | 2016-11-10 07:32:34 -0800 | [diff] [blame] | 2274 | void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status, |
| 2275 | union nvme_result *res) |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 2276 | { |
Christoph Hellwig | 7bf5853 | 2016-11-10 07:32:34 -0800 | [diff] [blame] | 2277 | u32 result = le32_to_cpu(res->u32); |
| 2278 | bool done = true; |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 2279 | |
Christoph Hellwig | 7bf5853 | 2016-11-10 07:32:34 -0800 | [diff] [blame] | 2280 | switch (le16_to_cpu(status) >> 1) { |
| 2281 | case NVME_SC_SUCCESS: |
| 2282 | done = false; |
| 2283 | /*FALLTHRU*/ |
| 2284 | case NVME_SC_ABORT_REQ: |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 2285 | ++ctrl->event_limit; |
| 2286 | schedule_work(&ctrl->async_event_work); |
Christoph Hellwig | 7bf5853 | 2016-11-10 07:32:34 -0800 | [diff] [blame] | 2287 | break; |
| 2288 | default: |
| 2289 | break; |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 2290 | } |
| 2291 | |
Christoph Hellwig | 7bf5853 | 2016-11-10 07:32:34 -0800 | [diff] [blame] | 2292 | if (done) |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 2293 | return; |
| 2294 | |
| 2295 | switch (result & 0xff07) { |
| 2296 | case NVME_AER_NOTICE_NS_CHANGED: |
| 2297 | dev_info(ctrl->device, "rescanning\n"); |
| 2298 | nvme_queue_scan(ctrl); |
| 2299 | break; |
| 2300 | default: |
| 2301 | dev_warn(ctrl->device, "async event result %08x\n", result); |
| 2302 | } |
| 2303 | } |
| 2304 | EXPORT_SYMBOL_GPL(nvme_complete_async_event); |
| 2305 | |
| 2306 | void nvme_queue_async_events(struct nvme_ctrl *ctrl) |
| 2307 | { |
| 2308 | ctrl->event_limit = NVME_NR_AERS; |
| 2309 | schedule_work(&ctrl->async_event_work); |
| 2310 | } |
| 2311 | EXPORT_SYMBOL_GPL(nvme_queue_async_events); |
| 2312 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2313 | static DEFINE_IDA(nvme_instance_ida); |
| 2314 | |
| 2315 | static int nvme_set_instance(struct nvme_ctrl *ctrl) |
| 2316 | { |
| 2317 | int instance, error; |
| 2318 | |
| 2319 | do { |
| 2320 | if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL)) |
| 2321 | return -ENODEV; |
| 2322 | |
| 2323 | spin_lock(&dev_list_lock); |
| 2324 | error = ida_get_new(&nvme_instance_ida, &instance); |
| 2325 | spin_unlock(&dev_list_lock); |
| 2326 | } while (error == -EAGAIN); |
| 2327 | |
| 2328 | if (error) |
| 2329 | return -ENODEV; |
| 2330 | |
| 2331 | ctrl->instance = instance; |
| 2332 | return 0; |
| 2333 | } |
| 2334 | |
| 2335 | static void nvme_release_instance(struct nvme_ctrl *ctrl) |
| 2336 | { |
| 2337 | spin_lock(&dev_list_lock); |
| 2338 | ida_remove(&nvme_instance_ida, ctrl->instance); |
| 2339 | spin_unlock(&dev_list_lock); |
| 2340 | } |
| 2341 | |
Keith Busch | 53029b0 | 2015-11-28 15:41:02 +0100 | [diff] [blame] | 2342 | void nvme_uninit_ctrl(struct nvme_ctrl *ctrl) |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 2343 | { |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 2344 | flush_work(&ctrl->async_event_work); |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 2345 | flush_work(&ctrl->scan_work); |
| 2346 | nvme_remove_namespaces(ctrl); |
| 2347 | |
Keith Busch | 53029b0 | 2015-11-28 15:41:02 +0100 | [diff] [blame] | 2348 | device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance)); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2349 | |
| 2350 | spin_lock(&dev_list_lock); |
| 2351 | list_del(&ctrl->node); |
| 2352 | spin_unlock(&dev_list_lock); |
Keith Busch | 53029b0 | 2015-11-28 15:41:02 +0100 | [diff] [blame] | 2353 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 2354 | EXPORT_SYMBOL_GPL(nvme_uninit_ctrl); |
Keith Busch | 53029b0 | 2015-11-28 15:41:02 +0100 | [diff] [blame] | 2355 | |
| 2356 | static void nvme_free_ctrl(struct kref *kref) |
| 2357 | { |
| 2358 | struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2359 | |
| 2360 | put_device(ctrl->device); |
| 2361 | nvme_release_instance(ctrl); |
Keith Busch | 075790e | 2016-02-24 09:15:53 -0700 | [diff] [blame] | 2362 | ida_destroy(&ctrl->ns_ida); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2363 | |
| 2364 | ctrl->ops->free_ctrl(ctrl); |
| 2365 | } |
| 2366 | |
| 2367 | void nvme_put_ctrl(struct nvme_ctrl *ctrl) |
| 2368 | { |
| 2369 | kref_put(&ctrl->kref, nvme_free_ctrl); |
| 2370 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 2371 | EXPORT_SYMBOL_GPL(nvme_put_ctrl); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2372 | |
| 2373 | /* |
| 2374 | * Initialize a NVMe controller structures. This needs to be called during |
| 2375 | * earliest initialization so that we have the initialized structured around |
| 2376 | * during probing. |
| 2377 | */ |
| 2378 | int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev, |
| 2379 | const struct nvme_ctrl_ops *ops, unsigned long quirks) |
| 2380 | { |
| 2381 | int ret; |
| 2382 | |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 2383 | ctrl->state = NVME_CTRL_NEW; |
| 2384 | spin_lock_init(&ctrl->lock); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2385 | INIT_LIST_HEAD(&ctrl->namespaces); |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 2386 | mutex_init(&ctrl->namespaces_mutex); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2387 | kref_init(&ctrl->kref); |
| 2388 | ctrl->dev = dev; |
| 2389 | ctrl->ops = ops; |
| 2390 | ctrl->quirks = quirks; |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 2391 | INIT_WORK(&ctrl->scan_work, nvme_scan_work); |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 2392 | INIT_WORK(&ctrl->async_event_work, nvme_async_event_work); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2393 | |
| 2394 | ret = nvme_set_instance(ctrl); |
| 2395 | if (ret) |
| 2396 | goto out; |
| 2397 | |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2398 | ctrl->device = device_create_with_groups(nvme_class, ctrl->dev, |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2399 | MKDEV(nvme_char_major, ctrl->instance), |
Christoph Hellwig | f4f0f63 | 2016-02-09 12:44:03 -0700 | [diff] [blame] | 2400 | ctrl, nvme_dev_attr_groups, |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2401 | "nvme%d", ctrl->instance); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2402 | if (IS_ERR(ctrl->device)) { |
| 2403 | ret = PTR_ERR(ctrl->device); |
| 2404 | goto out_release_instance; |
| 2405 | } |
| 2406 | get_device(ctrl->device); |
Keith Busch | 075790e | 2016-02-24 09:15:53 -0700 | [diff] [blame] | 2407 | ida_init(&ctrl->ns_ida); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2408 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2409 | spin_lock(&dev_list_lock); |
| 2410 | list_add_tail(&ctrl->node, &nvme_ctrl_list); |
| 2411 | spin_unlock(&dev_list_lock); |
| 2412 | |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 2413 | /* |
| 2414 | * Initialize latency tolerance controls. The sysfs files won't |
| 2415 | * be visible to userspace unless the device actually supports APST. |
| 2416 | */ |
| 2417 | ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance; |
| 2418 | dev_pm_qos_update_user_latency_tolerance(ctrl->device, |
| 2419 | min(default_ps_max_latency_us, (unsigned long)S32_MAX)); |
| 2420 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2421 | return 0; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2422 | out_release_instance: |
| 2423 | nvme_release_instance(ctrl); |
| 2424 | out: |
| 2425 | return ret; |
| 2426 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 2427 | EXPORT_SYMBOL_GPL(nvme_init_ctrl); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2428 | |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 2429 | /** |
| 2430 | * nvme_kill_queues(): Ends all namespace queues |
| 2431 | * @ctrl: the dead controller that needs to end |
| 2432 | * |
| 2433 | * Call this function when the driver determines it is unable to get the |
| 2434 | * controller in a state capable of servicing IO. |
| 2435 | */ |
| 2436 | void nvme_kill_queues(struct nvme_ctrl *ctrl) |
| 2437 | { |
| 2438 | struct nvme_ns *ns; |
| 2439 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2440 | mutex_lock(&ctrl->namespaces_mutex); |
| 2441 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 2442 | /* |
| 2443 | * Revalidating a dead namespace sets capacity to 0. This will |
| 2444 | * end buffered writers dirtying pages that can't be synced. |
| 2445 | */ |
Keith Busch | f33447b | 2017-02-10 18:15:51 -0500 | [diff] [blame] | 2446 | if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags)) |
| 2447 | continue; |
| 2448 | revalidate_disk(ns->disk); |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 2449 | blk_set_queue_dying(ns->queue); |
Ming Lei | 806f026 | 2017-05-22 23:05:03 +0800 | [diff] [blame] | 2450 | |
| 2451 | /* |
| 2452 | * Forcibly start all queues to avoid having stuck requests. |
| 2453 | * Note that we must ensure the queues are not stopped |
| 2454 | * when the final removal happens. |
| 2455 | */ |
| 2456 | blk_mq_start_hw_queues(ns->queue); |
Ming Lei | 986f75c | 2017-05-22 23:05:04 +0800 | [diff] [blame] | 2457 | |
| 2458 | /* draining requests in requeue list */ |
| 2459 | blk_mq_kick_requeue_list(ns->queue); |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 2460 | } |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2461 | mutex_unlock(&ctrl->namespaces_mutex); |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 2462 | } |
Linus Torvalds | 237045f | 2016-03-18 17:13:31 -0700 | [diff] [blame] | 2463 | EXPORT_SYMBOL_GPL(nvme_kill_queues); |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 2464 | |
Keith Busch | 302ad8c | 2017-03-01 14:22:12 -0500 | [diff] [blame] | 2465 | void nvme_unfreeze(struct nvme_ctrl *ctrl) |
| 2466 | { |
| 2467 | struct nvme_ns *ns; |
| 2468 | |
| 2469 | mutex_lock(&ctrl->namespaces_mutex); |
| 2470 | list_for_each_entry(ns, &ctrl->namespaces, list) |
| 2471 | blk_mq_unfreeze_queue(ns->queue); |
| 2472 | mutex_unlock(&ctrl->namespaces_mutex); |
| 2473 | } |
| 2474 | EXPORT_SYMBOL_GPL(nvme_unfreeze); |
| 2475 | |
| 2476 | void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout) |
| 2477 | { |
| 2478 | struct nvme_ns *ns; |
| 2479 | |
| 2480 | mutex_lock(&ctrl->namespaces_mutex); |
| 2481 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
| 2482 | timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout); |
| 2483 | if (timeout <= 0) |
| 2484 | break; |
| 2485 | } |
| 2486 | mutex_unlock(&ctrl->namespaces_mutex); |
| 2487 | } |
| 2488 | EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout); |
| 2489 | |
| 2490 | void nvme_wait_freeze(struct nvme_ctrl *ctrl) |
| 2491 | { |
| 2492 | struct nvme_ns *ns; |
| 2493 | |
| 2494 | mutex_lock(&ctrl->namespaces_mutex); |
| 2495 | list_for_each_entry(ns, &ctrl->namespaces, list) |
| 2496 | blk_mq_freeze_queue_wait(ns->queue); |
| 2497 | mutex_unlock(&ctrl->namespaces_mutex); |
| 2498 | } |
| 2499 | EXPORT_SYMBOL_GPL(nvme_wait_freeze); |
| 2500 | |
| 2501 | void nvme_start_freeze(struct nvme_ctrl *ctrl) |
| 2502 | { |
| 2503 | struct nvme_ns *ns; |
| 2504 | |
| 2505 | mutex_lock(&ctrl->namespaces_mutex); |
| 2506 | list_for_each_entry(ns, &ctrl->namespaces, list) |
Ming Lei | 1671d52 | 2017-03-27 20:06:57 +0800 | [diff] [blame] | 2507 | blk_freeze_queue_start(ns->queue); |
Keith Busch | 302ad8c | 2017-03-01 14:22:12 -0500 | [diff] [blame] | 2508 | mutex_unlock(&ctrl->namespaces_mutex); |
| 2509 | } |
| 2510 | EXPORT_SYMBOL_GPL(nvme_start_freeze); |
| 2511 | |
Keith Busch | 2564626 | 2016-01-04 09:10:57 -0700 | [diff] [blame] | 2512 | void nvme_stop_queues(struct nvme_ctrl *ctrl) |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 2513 | { |
| 2514 | struct nvme_ns *ns; |
| 2515 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2516 | mutex_lock(&ctrl->namespaces_mutex); |
Bart Van Assche | a6eaa88 | 2016-10-28 17:23:40 -0700 | [diff] [blame] | 2517 | list_for_each_entry(ns, &ctrl->namespaces, list) |
Bart Van Assche | 3174dd3 | 2016-10-28 17:23:19 -0700 | [diff] [blame] | 2518 | blk_mq_quiesce_queue(ns->queue); |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2519 | mutex_unlock(&ctrl->namespaces_mutex); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 2520 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 2521 | EXPORT_SYMBOL_GPL(nvme_stop_queues); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 2522 | |
Keith Busch | 2564626 | 2016-01-04 09:10:57 -0700 | [diff] [blame] | 2523 | void nvme_start_queues(struct nvme_ctrl *ctrl) |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 2524 | { |
| 2525 | struct nvme_ns *ns; |
| 2526 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2527 | mutex_lock(&ctrl->namespaces_mutex); |
| 2528 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 2529 | blk_mq_start_stopped_hw_queues(ns->queue, true); |
| 2530 | blk_mq_kick_requeue_list(ns->queue); |
| 2531 | } |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2532 | mutex_unlock(&ctrl->namespaces_mutex); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 2533 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 2534 | EXPORT_SYMBOL_GPL(nvme_start_queues); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 2535 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2536 | int __init nvme_core_init(void) |
| 2537 | { |
| 2538 | int result; |
| 2539 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2540 | result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme", |
| 2541 | &nvme_dev_fops); |
| 2542 | if (result < 0) |
NeilBrown | b09dcf5 | 2016-07-13 11:03:58 -0700 | [diff] [blame] | 2543 | return result; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2544 | else if (result > 0) |
| 2545 | nvme_char_major = result; |
| 2546 | |
| 2547 | nvme_class = class_create(THIS_MODULE, "nvme"); |
| 2548 | if (IS_ERR(nvme_class)) { |
| 2549 | result = PTR_ERR(nvme_class); |
| 2550 | goto unregister_chrdev; |
| 2551 | } |
| 2552 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2553 | return 0; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2554 | |
| 2555 | unregister_chrdev: |
| 2556 | __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme"); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2557 | return result; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | void nvme_core_exit(void) |
| 2561 | { |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2562 | class_destroy(nvme_class); |
| 2563 | __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme"); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2564 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 2565 | |
| 2566 | MODULE_LICENSE("GPL"); |
| 2567 | MODULE_VERSION("1.0"); |
| 2568 | module_init(nvme_core_init); |
| 2569 | module_exit(nvme_core_exit); |