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