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 <asm/unaligned.h> |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 31 | |
| 32 | #include "nvme.h" |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 33 | #include "fabrics.h" |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 34 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 35 | #define NVME_MINORS (1U << MINORBITS) |
| 36 | |
Marc Olson | 8ae4e44 | 2017-09-06 17:23:56 -0700 | [diff] [blame] | 37 | unsigned int admin_timeout = 60; |
| 38 | module_param(admin_timeout, uint, 0644); |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 39 | MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands"); |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 40 | EXPORT_SYMBOL_GPL(admin_timeout); |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 41 | |
Marc Olson | 8ae4e44 | 2017-09-06 17:23:56 -0700 | [diff] [blame] | 42 | unsigned int nvme_io_timeout = 30; |
| 43 | module_param_named(io_timeout, nvme_io_timeout, uint, 0644); |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 44 | MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O"); |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 45 | EXPORT_SYMBOL_GPL(nvme_io_timeout); |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 46 | |
Christoph Hellwig | b3b1b0b | 2017-06-12 18:30:51 +0200 | [diff] [blame] | 47 | static unsigned char shutdown_timeout = 5; |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 48 | module_param(shutdown_timeout, byte, 0644); |
| 49 | MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown"); |
| 50 | |
Christoph Hellwig | 44e44b2 | 2017-04-05 19:18:11 +0200 | [diff] [blame] | 51 | static u8 nvme_max_retries = 5; |
| 52 | module_param_named(max_retries, nvme_max_retries, byte, 0644); |
Keith Busch | f80ec96 | 2016-07-12 16:20:31 -0700 | [diff] [blame] | 53 | 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] | 54 | |
Kai-Heng Feng | 9947d6a | 2017-06-07 15:25:43 +0800 | [diff] [blame] | 55 | static unsigned long default_ps_max_latency_us = 100000; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 56 | module_param(default_ps_max_latency_us, ulong, 0644); |
| 57 | MODULE_PARM_DESC(default_ps_max_latency_us, |
| 58 | "max power saving latency for new devices; use PM QOS to change per device"); |
| 59 | |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 60 | static bool force_apst; |
| 61 | module_param(force_apst, bool, 0644); |
| 62 | MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off"); |
| 63 | |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 64 | static bool streams; |
| 65 | module_param(streams, bool, 0644); |
| 66 | MODULE_PARM_DESC(streams, "turn on support for Streams write directives"); |
| 67 | |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 68 | struct workqueue_struct *nvme_wq; |
| 69 | EXPORT_SYMBOL_GPL(nvme_wq); |
| 70 | |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 71 | static DEFINE_IDA(nvme_subsystems_ida); |
| 72 | static LIST_HEAD(nvme_subsystems); |
| 73 | static DEFINE_MUTEX(nvme_subsystems_lock); |
| 74 | |
Christoph Hellwig | 9843f68 | 2017-10-18 13:10:01 +0200 | [diff] [blame] | 75 | static DEFINE_IDA(nvme_instance_ida); |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 76 | static dev_t nvme_chr_devt; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 77 | static struct class *nvme_class; |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 78 | static struct class *nvme_subsys_class; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 79 | |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 80 | static void nvme_ns_remove(struct nvme_ns *ns); |
| 81 | static int nvme_revalidate_disk(struct gendisk *disk); |
| 82 | |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 83 | static __le32 nvme_get_log_dw10(u8 lid, size_t size) |
| 84 | { |
| 85 | return cpu_to_le32((((size / 4) - 1) << 16) | lid); |
| 86 | } |
| 87 | |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 88 | int nvme_reset_ctrl(struct nvme_ctrl *ctrl) |
| 89 | { |
| 90 | if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING)) |
| 91 | return -EBUSY; |
| 92 | if (!queue_work(nvme_wq, &ctrl->reset_work)) |
| 93 | return -EBUSY; |
| 94 | return 0; |
| 95 | } |
| 96 | EXPORT_SYMBOL_GPL(nvme_reset_ctrl); |
| 97 | |
| 98 | static int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl) |
| 99 | { |
| 100 | int ret; |
| 101 | |
| 102 | ret = nvme_reset_ctrl(ctrl); |
| 103 | if (!ret) |
| 104 | flush_work(&ctrl->reset_work); |
| 105 | return ret; |
| 106 | } |
| 107 | |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 108 | static void nvme_delete_ctrl_work(struct work_struct *work) |
| 109 | { |
| 110 | struct nvme_ctrl *ctrl = |
| 111 | container_of(work, struct nvme_ctrl, delete_work); |
| 112 | |
Sagi Grimberg | 4054637 | 2017-10-29 14:21:02 +0200 | [diff] [blame] | 113 | flush_work(&ctrl->reset_work); |
Christoph Hellwig | 6cd53d1 | 2017-10-29 10:44:31 +0200 | [diff] [blame] | 114 | nvme_stop_ctrl(ctrl); |
| 115 | nvme_remove_namespaces(ctrl); |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 116 | ctrl->ops->delete_ctrl(ctrl); |
Christoph Hellwig | 6cd53d1 | 2017-10-29 10:44:31 +0200 | [diff] [blame] | 117 | nvme_uninit_ctrl(ctrl); |
| 118 | nvme_put_ctrl(ctrl); |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | int nvme_delete_ctrl(struct nvme_ctrl *ctrl) |
| 122 | { |
| 123 | if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING)) |
| 124 | return -EBUSY; |
| 125 | if (!queue_work(nvme_wq, &ctrl->delete_work)) |
| 126 | return -EBUSY; |
| 127 | return 0; |
| 128 | } |
| 129 | EXPORT_SYMBOL_GPL(nvme_delete_ctrl); |
| 130 | |
| 131 | int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl) |
| 132 | { |
| 133 | int ret = 0; |
| 134 | |
| 135 | /* |
| 136 | * Keep a reference until the work is flushed since ->delete_ctrl |
| 137 | * can free the controller. |
| 138 | */ |
| 139 | nvme_get_ctrl(ctrl); |
| 140 | ret = nvme_delete_ctrl(ctrl); |
| 141 | if (!ret) |
| 142 | flush_work(&ctrl->delete_work); |
| 143 | nvme_put_ctrl(ctrl); |
| 144 | return ret; |
| 145 | } |
| 146 | EXPORT_SYMBOL_GPL(nvme_delete_ctrl_sync); |
| 147 | |
Christoph Hellwig | 715ea9e | 2017-11-07 17:27:34 +0100 | [diff] [blame] | 148 | static inline bool nvme_ns_has_pi(struct nvme_ns *ns) |
| 149 | { |
| 150 | return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple); |
| 151 | } |
| 152 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 153 | static blk_status_t nvme_error_status(struct request *req) |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 154 | { |
| 155 | switch (nvme_req(req)->status & 0x7ff) { |
| 156 | case NVME_SC_SUCCESS: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 157 | return BLK_STS_OK; |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 158 | case NVME_SC_CAP_EXCEEDED: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 159 | return BLK_STS_NOSPC; |
Junxiong Guan | e02ab02 | 2017-04-21 12:59:07 +0200 | [diff] [blame] | 160 | case NVME_SC_ONCS_NOT_SUPPORTED: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 161 | return BLK_STS_NOTSUPP; |
Junxiong Guan | e02ab02 | 2017-04-21 12:59:07 +0200 | [diff] [blame] | 162 | case NVME_SC_WRITE_FAULT: |
| 163 | case NVME_SC_READ_ERROR: |
| 164 | case NVME_SC_UNWRITTEN_BLOCK: |
Christoph Hellwig | a751da3 | 2017-08-22 10:17:03 +0200 | [diff] [blame] | 165 | case NVME_SC_ACCESS_DENIED: |
| 166 | case NVME_SC_READ_ONLY: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 167 | return BLK_STS_MEDIUM; |
Christoph Hellwig | a751da3 | 2017-08-22 10:17:03 +0200 | [diff] [blame] | 168 | case NVME_SC_GUARD_CHECK: |
| 169 | case NVME_SC_APPTAG_CHECK: |
| 170 | case NVME_SC_REFTAG_CHECK: |
| 171 | case NVME_SC_INVALID_PI: |
| 172 | return BLK_STS_PROTECTION; |
| 173 | case NVME_SC_RESERVATION_CONFLICT: |
| 174 | return BLK_STS_NEXUS; |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 175 | default: |
| 176 | return BLK_STS_IOERR; |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 177 | } |
| 178 | } |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 179 | |
Christoph Hellwig | f6324b1 | 2017-04-05 19:18:09 +0200 | [diff] [blame] | 180 | static inline bool nvme_req_needs_retry(struct request *req) |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 181 | { |
Christoph Hellwig | f6324b1 | 2017-04-05 19:18:09 +0200 | [diff] [blame] | 182 | if (blk_noretry_request(req)) |
| 183 | return false; |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 184 | if (nvme_req(req)->status & NVME_SC_DNR) |
Christoph Hellwig | f6324b1 | 2017-04-05 19:18:09 +0200 | [diff] [blame] | 185 | return false; |
Christoph Hellwig | 44e44b2 | 2017-04-05 19:18:11 +0200 | [diff] [blame] | 186 | if (nvme_req(req)->retries >= nvme_max_retries) |
Christoph Hellwig | f6324b1 | 2017-04-05 19:18:09 +0200 | [diff] [blame] | 187 | return false; |
Christoph Hellwig | e54b064 | 2017-11-02 21:28:51 +0300 | [diff] [blame] | 188 | if (blk_queue_dying(req->q)) |
| 189 | return false; |
Christoph Hellwig | f6324b1 | 2017-04-05 19:18:09 +0200 | [diff] [blame] | 190 | return true; |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void nvme_complete_rq(struct request *req) |
| 194 | { |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 195 | if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) { |
| 196 | nvme_req(req)->retries++; |
Sagi Grimberg | 8d7b8fa | 2017-07-04 18:16:58 +0300 | [diff] [blame] | 197 | blk_mq_requeue_request(req, true); |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 198 | return; |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 201 | blk_mq_end_request(req, nvme_error_status(req)); |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 202 | } |
| 203 | EXPORT_SYMBOL_GPL(nvme_complete_rq); |
| 204 | |
Ming Lin | c55a2fd | 2016-05-18 14:05:02 -0700 | [diff] [blame] | 205 | void nvme_cancel_request(struct request *req, void *data, bool reserved) |
| 206 | { |
Ming Lin | c55a2fd | 2016-05-18 14:05:02 -0700 | [diff] [blame] | 207 | if (!blk_mq_request_started(req)) |
| 208 | return; |
| 209 | |
| 210 | dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device, |
| 211 | "Cancelling I/O %d", req->tag); |
| 212 | |
Christoph Hellwig | e54b064 | 2017-11-02 21:28:51 +0300 | [diff] [blame] | 213 | nvme_req(req)->status = NVME_SC_ABORT_REQ; |
Christoph Hellwig | 08e0029 | 2017-04-20 16:03:09 +0200 | [diff] [blame] | 214 | blk_mq_complete_request(req); |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 215 | |
Ming Lin | c55a2fd | 2016-05-18 14:05:02 -0700 | [diff] [blame] | 216 | } |
| 217 | EXPORT_SYMBOL_GPL(nvme_cancel_request); |
| 218 | |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 219 | bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl, |
| 220 | enum nvme_ctrl_state new_state) |
| 221 | { |
Gabriel Krisman Bertazi | f6b6a28 | 2016-07-29 16:15:18 -0300 | [diff] [blame] | 222 | enum nvme_ctrl_state old_state; |
Christoph Hellwig | 0a72bbb | 2017-08-22 11:42:24 +0200 | [diff] [blame] | 223 | unsigned long flags; |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 224 | bool changed = false; |
| 225 | |
Christoph Hellwig | 0a72bbb | 2017-08-22 11:42:24 +0200 | [diff] [blame] | 226 | spin_lock_irqsave(&ctrl->lock, flags); |
Gabriel Krisman Bertazi | f6b6a28 | 2016-07-29 16:15:18 -0300 | [diff] [blame] | 227 | |
| 228 | old_state = ctrl->state; |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 229 | switch (new_state) { |
| 230 | case NVME_CTRL_LIVE: |
| 231 | switch (old_state) { |
Christoph Hellwig | 7d2e800 | 2016-06-13 16:45:22 +0200 | [diff] [blame] | 232 | case NVME_CTRL_NEW: |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 233 | case NVME_CTRL_RESETTING: |
Christoph Hellwig | def61ec | 2016-07-06 21:55:49 +0900 | [diff] [blame] | 234 | case NVME_CTRL_RECONNECTING: |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 235 | changed = true; |
| 236 | /* FALLTHRU */ |
| 237 | default: |
| 238 | break; |
| 239 | } |
| 240 | break; |
| 241 | case NVME_CTRL_RESETTING: |
| 242 | switch (old_state) { |
| 243 | case NVME_CTRL_NEW: |
| 244 | case NVME_CTRL_LIVE: |
Christoph Hellwig | def61ec | 2016-07-06 21:55:49 +0900 | [diff] [blame] | 245 | changed = true; |
| 246 | /* FALLTHRU */ |
| 247 | default: |
| 248 | break; |
| 249 | } |
| 250 | break; |
| 251 | case NVME_CTRL_RECONNECTING: |
| 252 | switch (old_state) { |
| 253 | case NVME_CTRL_LIVE: |
James Smart | 3cec7f9 | 2017-10-25 16:43:13 -0700 | [diff] [blame] | 254 | case NVME_CTRL_RESETTING: |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 255 | changed = true; |
| 256 | /* FALLTHRU */ |
| 257 | default: |
| 258 | break; |
| 259 | } |
| 260 | break; |
| 261 | case NVME_CTRL_DELETING: |
| 262 | switch (old_state) { |
| 263 | case NVME_CTRL_LIVE: |
| 264 | case NVME_CTRL_RESETTING: |
Christoph Hellwig | def61ec | 2016-07-06 21:55:49 +0900 | [diff] [blame] | 265 | case NVME_CTRL_RECONNECTING: |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 266 | changed = true; |
| 267 | /* FALLTHRU */ |
| 268 | default: |
| 269 | break; |
| 270 | } |
| 271 | break; |
Keith Busch | 0ff9d4e | 2016-05-12 08:37:14 -0600 | [diff] [blame] | 272 | case NVME_CTRL_DEAD: |
| 273 | switch (old_state) { |
| 274 | case NVME_CTRL_DELETING: |
| 275 | changed = true; |
| 276 | /* FALLTHRU */ |
| 277 | default: |
| 278 | break; |
| 279 | } |
| 280 | break; |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 281 | default: |
| 282 | break; |
| 283 | } |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 284 | |
| 285 | if (changed) |
| 286 | ctrl->state = new_state; |
| 287 | |
Christoph Hellwig | 0a72bbb | 2017-08-22 11:42:24 +0200 | [diff] [blame] | 288 | spin_unlock_irqrestore(&ctrl->lock, flags); |
Gabriel Krisman Bertazi | f6b6a28 | 2016-07-29 16:15:18 -0300 | [diff] [blame] | 289 | |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 290 | return changed; |
| 291 | } |
| 292 | EXPORT_SYMBOL_GPL(nvme_change_ctrl_state); |
| 293 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 294 | static void nvme_free_ns_head(struct kref *ref) |
| 295 | { |
| 296 | struct nvme_ns_head *head = |
| 297 | container_of(ref, struct nvme_ns_head, ref); |
| 298 | |
| 299 | ida_simple_remove(&head->subsys->ns_ida, head->instance); |
| 300 | list_del_init(&head->entry); |
| 301 | cleanup_srcu_struct(&head->srcu); |
| 302 | kfree(head); |
| 303 | } |
| 304 | |
| 305 | static void nvme_put_ns_head(struct nvme_ns_head *head) |
| 306 | { |
| 307 | kref_put(&head->ref, nvme_free_ns_head); |
| 308 | } |
| 309 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 310 | static void nvme_free_ns(struct kref *kref) |
| 311 | { |
| 312 | struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref); |
| 313 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 314 | if (ns->ndev) |
| 315 | nvme_nvm_unregister(ns); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 316 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 317 | put_disk(ns->disk); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 318 | nvme_put_ns_head(ns->head); |
Keith Busch | 075790e | 2016-02-24 09:15:53 -0700 | [diff] [blame] | 319 | nvme_put_ctrl(ns->ctrl); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 320 | kfree(ns); |
| 321 | } |
| 322 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 323 | static void nvme_put_ns(struct nvme_ns *ns) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 324 | { |
| 325 | kref_put(&ns->kref, nvme_free_ns); |
| 326 | } |
| 327 | |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 328 | struct request *nvme_alloc_request(struct request_queue *q, |
Bart Van Assche | 9a95e4e | 2017-11-09 10:49:59 -0800 | [diff] [blame] | 329 | struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid) |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 330 | { |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 331 | 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] | 332 | struct request *req; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 333 | |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 334 | if (qid == NVME_QID_ANY) { |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 335 | req = blk_mq_alloc_request(q, op, flags); |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 336 | } else { |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 337 | req = blk_mq_alloc_request_hctx(q, op, flags, |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 338 | qid ? qid - 1 : 0); |
| 339 | } |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 340 | if (IS_ERR(req)) |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 341 | return req; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 342 | |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 343 | req->cmd_flags |= REQ_FAILFAST_DRIVER; |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 344 | nvme_req(req)->cmd = cmd; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 345 | |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 346 | return req; |
| 347 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 348 | EXPORT_SYMBOL_GPL(nvme_alloc_request); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 349 | |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 350 | static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable) |
| 351 | { |
| 352 | struct nvme_command c; |
| 353 | |
| 354 | memset(&c, 0, sizeof(c)); |
| 355 | |
| 356 | c.directive.opcode = nvme_admin_directive_send; |
Arnav Dawn | 62346ea | 2017-07-12 16:11:53 +0530 | [diff] [blame] | 357 | c.directive.nsid = cpu_to_le32(NVME_NSID_ALL); |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 358 | c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE; |
| 359 | c.directive.dtype = NVME_DIR_IDENTIFY; |
| 360 | c.directive.tdtype = NVME_DIR_STREAMS; |
| 361 | c.directive.endir = enable ? NVME_DIR_ENDIR : 0; |
| 362 | |
| 363 | return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0); |
| 364 | } |
| 365 | |
| 366 | static int nvme_disable_streams(struct nvme_ctrl *ctrl) |
| 367 | { |
| 368 | return nvme_toggle_streams(ctrl, false); |
| 369 | } |
| 370 | |
| 371 | static int nvme_enable_streams(struct nvme_ctrl *ctrl) |
| 372 | { |
| 373 | return nvme_toggle_streams(ctrl, true); |
| 374 | } |
| 375 | |
| 376 | static int nvme_get_stream_params(struct nvme_ctrl *ctrl, |
| 377 | struct streams_directive_params *s, u32 nsid) |
| 378 | { |
| 379 | struct nvme_command c; |
| 380 | |
| 381 | memset(&c, 0, sizeof(c)); |
| 382 | memset(s, 0, sizeof(*s)); |
| 383 | |
| 384 | c.directive.opcode = nvme_admin_directive_recv; |
| 385 | c.directive.nsid = cpu_to_le32(nsid); |
Kwan (Hingkwan) Huen-SSI | a082b42 | 2017-08-09 11:26:29 -0700 | [diff] [blame] | 386 | c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1); |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 387 | c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM; |
| 388 | c.directive.dtype = NVME_DIR_STREAMS; |
| 389 | |
| 390 | return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s)); |
| 391 | } |
| 392 | |
| 393 | static int nvme_configure_directives(struct nvme_ctrl *ctrl) |
| 394 | { |
| 395 | struct streams_directive_params s; |
| 396 | int ret; |
| 397 | |
| 398 | if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES)) |
| 399 | return 0; |
| 400 | if (!streams) |
| 401 | return 0; |
| 402 | |
| 403 | ret = nvme_enable_streams(ctrl); |
| 404 | if (ret) |
| 405 | return ret; |
| 406 | |
Arnav Dawn | 62346ea | 2017-07-12 16:11:53 +0530 | [diff] [blame] | 407 | ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL); |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 408 | if (ret) |
| 409 | return ret; |
| 410 | |
| 411 | ctrl->nssa = le16_to_cpu(s.nssa); |
| 412 | if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) { |
| 413 | dev_info(ctrl->device, "too few streams (%u) available\n", |
| 414 | ctrl->nssa); |
| 415 | nvme_disable_streams(ctrl); |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1); |
| 420 | dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams); |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Check if 'req' has a write hint associated with it. If it does, assign |
| 426 | * a valid namespace stream to the write. |
| 427 | */ |
| 428 | static void nvme_assign_write_stream(struct nvme_ctrl *ctrl, |
| 429 | struct request *req, u16 *control, |
| 430 | u32 *dsmgmt) |
| 431 | { |
| 432 | enum rw_hint streamid = req->write_hint; |
| 433 | |
| 434 | if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE) |
| 435 | streamid = 0; |
| 436 | else { |
| 437 | streamid--; |
| 438 | if (WARN_ON_ONCE(streamid > ctrl->nr_streams)) |
| 439 | return; |
| 440 | |
| 441 | *control |= NVME_RW_DTYPE_STREAMS; |
| 442 | *dsmgmt |= streamid << 16; |
| 443 | } |
| 444 | |
| 445 | if (streamid < ARRAY_SIZE(req->q->write_hints)) |
| 446 | req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9; |
| 447 | } |
| 448 | |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 449 | static inline void nvme_setup_flush(struct nvme_ns *ns, |
| 450 | struct nvme_command *cmnd) |
| 451 | { |
| 452 | memset(cmnd, 0, sizeof(*cmnd)); |
| 453 | cmnd->common.opcode = nvme_cmd_flush; |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 454 | cmnd->common.nsid = cpu_to_le32(ns->head->ns_id); |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 455 | } |
| 456 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 457 | static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req, |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 458 | struct nvme_command *cmnd) |
| 459 | { |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 460 | unsigned short segments = blk_rq_nr_discard_segments(req), n = 0; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 461 | struct nvme_dsm_range *range; |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 462 | struct bio *bio; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 463 | |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 464 | range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC); |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 465 | if (!range) |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 466 | return BLK_STS_RESOURCE; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 467 | |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 468 | __rq_for_each_bio(bio, req) { |
| 469 | u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector); |
| 470 | u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift; |
| 471 | |
| 472 | range[n].cattr = cpu_to_le32(0); |
| 473 | range[n].nlb = cpu_to_le32(nlb); |
| 474 | range[n].slba = cpu_to_le64(slba); |
| 475 | n++; |
| 476 | } |
| 477 | |
| 478 | if (WARN_ON_ONCE(n != segments)) { |
| 479 | kfree(range); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 480 | return BLK_STS_IOERR; |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 481 | } |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 482 | |
| 483 | memset(cmnd, 0, sizeof(*cmnd)); |
| 484 | cmnd->dsm.opcode = nvme_cmd_dsm; |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 485 | cmnd->dsm.nsid = cpu_to_le32(ns->head->ns_id); |
Christoph Hellwig | f1dd03a | 2017-03-31 17:00:05 +0200 | [diff] [blame] | 486 | cmnd->dsm.nr = cpu_to_le32(segments - 1); |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 487 | cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD); |
| 488 | |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 489 | req->special_vec.bv_page = virt_to_page(range); |
| 490 | req->special_vec.bv_offset = offset_in_page(range); |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 491 | req->special_vec.bv_len = sizeof(*range) * segments; |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 492 | req->rq_flags |= RQF_SPECIAL_PAYLOAD; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 493 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 494 | return BLK_STS_OK; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 495 | } |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 496 | |
Christoph Hellwig | ebe6d87 | 2017-06-12 18:36:32 +0200 | [diff] [blame] | 497 | static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns, |
| 498 | struct request *req, struct nvme_command *cmnd) |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 499 | { |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 500 | struct nvme_ctrl *ctrl = ns->ctrl; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 501 | u16 control = 0; |
| 502 | u32 dsmgmt = 0; |
| 503 | |
| 504 | if (req->cmd_flags & REQ_FUA) |
| 505 | control |= NVME_RW_FUA; |
| 506 | if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD)) |
| 507 | control |= NVME_RW_LR; |
| 508 | |
| 509 | if (req->cmd_flags & REQ_RAHEAD) |
| 510 | dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; |
| 511 | |
| 512 | memset(cmnd, 0, sizeof(*cmnd)); |
| 513 | cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 514 | cmnd->rw.nsid = cpu_to_le32(ns->head->ns_id); |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 515 | cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req))); |
| 516 | cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1); |
| 517 | |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 518 | if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams) |
| 519 | nvme_assign_write_stream(ctrl, req, &control, &dsmgmt); |
| 520 | |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 521 | if (ns->ms) { |
Christoph Hellwig | 715ea9e | 2017-11-07 17:27:34 +0100 | [diff] [blame] | 522 | /* |
| 523 | * If formated with metadata, the block layer always provides a |
| 524 | * metadata buffer if CONFIG_BLK_DEV_INTEGRITY is enabled. Else |
| 525 | * we enable the PRACT bit for protection information or set the |
| 526 | * namespace capacity to zero to prevent any I/O. |
| 527 | */ |
| 528 | if (!blk_integrity_rq(req)) { |
| 529 | if (WARN_ON_ONCE(!nvme_ns_has_pi(ns))) |
| 530 | return BLK_STS_NOTSUPP; |
| 531 | control |= NVME_RW_PRINFO_PRACT; |
| 532 | } |
| 533 | |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 534 | switch (ns->pi_type) { |
| 535 | case NVME_NS_DPS_PI_TYPE3: |
| 536 | control |= NVME_RW_PRINFO_PRCHK_GUARD; |
| 537 | break; |
| 538 | case NVME_NS_DPS_PI_TYPE1: |
| 539 | case NVME_NS_DPS_PI_TYPE2: |
| 540 | control |= NVME_RW_PRINFO_PRCHK_GUARD | |
| 541 | NVME_RW_PRINFO_PRCHK_REF; |
| 542 | cmnd->rw.reftag = cpu_to_le32( |
| 543 | nvme_block_nr(ns, blk_rq_pos(req))); |
| 544 | break; |
| 545 | } |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | cmnd->rw.control = cpu_to_le16(control); |
| 549 | cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); |
Christoph Hellwig | ebe6d87 | 2017-06-12 18:36:32 +0200 | [diff] [blame] | 550 | return 0; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 551 | } |
| 552 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 553 | blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req, |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 554 | struct nvme_command *cmd) |
| 555 | { |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 556 | blk_status_t ret = BLK_STS_OK; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 557 | |
Christoph Hellwig | 987f699 | 2017-04-05 19:18:08 +0200 | [diff] [blame] | 558 | if (!(req->rq_flags & RQF_DONTPREP)) { |
Christoph Hellwig | 44e44b2 | 2017-04-05 19:18:11 +0200 | [diff] [blame] | 559 | nvme_req(req)->retries = 0; |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 560 | nvme_req(req)->flags = 0; |
Christoph Hellwig | 987f699 | 2017-04-05 19:18:08 +0200 | [diff] [blame] | 561 | req->rq_flags |= RQF_DONTPREP; |
| 562 | } |
| 563 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 564 | switch (req_op(req)) { |
| 565 | case REQ_OP_DRV_IN: |
| 566 | case REQ_OP_DRV_OUT: |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 567 | memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd)); |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 568 | break; |
| 569 | case REQ_OP_FLUSH: |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 570 | nvme_setup_flush(ns, cmd); |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 571 | break; |
Christoph Hellwig | e850fd1 | 2017-04-05 19:21:13 +0200 | [diff] [blame] | 572 | case REQ_OP_WRITE_ZEROES: |
| 573 | /* currently only aliased to deallocate for a few ctrls: */ |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 574 | case REQ_OP_DISCARD: |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 575 | ret = nvme_setup_discard(ns, req, cmd); |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 576 | break; |
| 577 | case REQ_OP_READ: |
| 578 | case REQ_OP_WRITE: |
Christoph Hellwig | ebe6d87 | 2017-06-12 18:36:32 +0200 | [diff] [blame] | 579 | ret = nvme_setup_rw(ns, req, cmd); |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 580 | break; |
| 581 | default: |
| 582 | WARN_ON_ONCE(1); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 583 | return BLK_STS_IOERR; |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 584 | } |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 585 | |
James Smart | 721b391 | 2016-10-21 23:33:34 +0300 | [diff] [blame] | 586 | cmd->common.command_id = req->tag; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 587 | return ret; |
| 588 | } |
| 589 | EXPORT_SYMBOL_GPL(nvme_setup_cmd); |
| 590 | |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 591 | /* |
| 592 | * Returns 0 on success. If the result is negative, it's a Linux error code; |
| 593 | * if the result is positive, it's an NVM Express status code |
| 594 | */ |
| 595 | 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] | 596 | union nvme_result *result, void *buffer, unsigned bufflen, |
Bart Van Assche | 9a95e4e | 2017-11-09 10:49:59 -0800 | [diff] [blame] | 597 | unsigned timeout, int qid, int at_head, |
| 598 | blk_mq_req_flags_t flags) |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 599 | { |
| 600 | struct request *req; |
| 601 | int ret; |
| 602 | |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 603 | req = nvme_alloc_request(q, cmd, flags, qid); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 604 | if (IS_ERR(req)) |
| 605 | return PTR_ERR(req); |
| 606 | |
| 607 | req->timeout = timeout ? timeout : ADMIN_TIMEOUT; |
| 608 | |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 609 | if (buffer && bufflen) { |
| 610 | ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL); |
| 611 | if (ret) |
| 612 | goto out; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 613 | } |
| 614 | |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 615 | blk_execute_rq(req->q, NULL, req, at_head); |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 616 | if (result) |
| 617 | *result = nvme_req(req)->result; |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 618 | if (nvme_req(req)->flags & NVME_REQ_CANCELLED) |
| 619 | ret = -EINTR; |
| 620 | else |
| 621 | ret = nvme_req(req)->status; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 622 | out: |
| 623 | blk_mq_free_request(req); |
| 624 | return ret; |
| 625 | } |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 626 | EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 627 | |
| 628 | int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, |
| 629 | void *buffer, unsigned bufflen) |
| 630 | { |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 631 | return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0, |
| 632 | NVME_QID_ANY, 0, 0); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 633 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 634 | EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 635 | |
Christoph Hellwig | 1cad656 | 2017-08-29 17:46:01 -0400 | [diff] [blame] | 636 | static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf, |
| 637 | unsigned len, u32 seed, bool write) |
| 638 | { |
| 639 | struct bio_integrity_payload *bip; |
| 640 | int ret = -ENOMEM; |
| 641 | void *buf; |
| 642 | |
| 643 | buf = kmalloc(len, GFP_KERNEL); |
| 644 | if (!buf) |
| 645 | goto out; |
| 646 | |
| 647 | ret = -EFAULT; |
| 648 | if (write && copy_from_user(buf, ubuf, len)) |
| 649 | goto out_free_meta; |
| 650 | |
| 651 | bip = bio_integrity_alloc(bio, GFP_KERNEL, 1); |
| 652 | if (IS_ERR(bip)) { |
| 653 | ret = PTR_ERR(bip); |
| 654 | goto out_free_meta; |
| 655 | } |
| 656 | |
| 657 | bip->bip_iter.bi_size = len; |
| 658 | bip->bip_iter.bi_sector = seed; |
| 659 | ret = bio_integrity_add_page(bio, virt_to_page(buf), len, |
| 660 | offset_in_page(buf)); |
| 661 | if (ret == len) |
| 662 | return buf; |
| 663 | ret = -ENOMEM; |
| 664 | out_free_meta: |
| 665 | kfree(buf); |
| 666 | out: |
| 667 | return ERR_PTR(ret); |
| 668 | } |
| 669 | |
Keith Busch | 63263d6 | 2017-08-29 17:46:04 -0400 | [diff] [blame] | 670 | static int nvme_submit_user_cmd(struct request_queue *q, |
Keith Busch | 485783c | 2017-08-29 17:46:03 -0400 | [diff] [blame] | 671 | struct nvme_command *cmd, void __user *ubuffer, |
| 672 | unsigned bufflen, void __user *meta_buffer, unsigned meta_len, |
| 673 | u32 meta_seed, u32 *result, unsigned timeout) |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 674 | { |
Christoph Hellwig | 7a5abb4 | 2016-06-06 23:20:49 +0200 | [diff] [blame] | 675 | bool write = nvme_is_write(cmd); |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 676 | struct nvme_ns *ns = q->queuedata; |
| 677 | struct gendisk *disk = ns ? ns->disk : NULL; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 678 | struct request *req; |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 679 | struct bio *bio = NULL; |
| 680 | void *meta = NULL; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 681 | int ret; |
| 682 | |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 683 | req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 684 | if (IS_ERR(req)) |
| 685 | return PTR_ERR(req); |
| 686 | |
| 687 | req->timeout = timeout ? timeout : ADMIN_TIMEOUT; |
| 688 | |
| 689 | if (ubuffer && bufflen) { |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 690 | ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen, |
| 691 | GFP_KERNEL); |
| 692 | if (ret) |
| 693 | goto out; |
| 694 | bio = req->bio; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 695 | bio->bi_disk = disk; |
Christoph Hellwig | 1cad656 | 2017-08-29 17:46:01 -0400 | [diff] [blame] | 696 | if (disk && meta_buffer && meta_len) { |
| 697 | meta = nvme_add_user_metadata(bio, meta_buffer, meta_len, |
| 698 | meta_seed, write); |
| 699 | if (IS_ERR(meta)) { |
| 700 | ret = PTR_ERR(meta); |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 701 | goto out_unmap; |
| 702 | } |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 703 | } |
| 704 | } |
Christoph Hellwig | 1cad656 | 2017-08-29 17:46:01 -0400 | [diff] [blame] | 705 | |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 706 | blk_execute_rq(req->q, disk, req, 0); |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 707 | if (nvme_req(req)->flags & NVME_REQ_CANCELLED) |
| 708 | ret = -EINTR; |
| 709 | else |
| 710 | ret = nvme_req(req)->status; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 711 | if (result) |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 712 | *result = le32_to_cpu(nvme_req(req)->result.u32); |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 713 | if (meta && !ret && !write) { |
| 714 | if (copy_to_user(meta_buffer, meta, meta_len)) |
| 715 | ret = -EFAULT; |
| 716 | } |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 717 | kfree(meta); |
| 718 | out_unmap: |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 719 | if (bio) |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 720 | blk_rq_unmap_user(bio); |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 721 | out: |
| 722 | blk_mq_free_request(req); |
| 723 | return ret; |
| 724 | } |
| 725 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 726 | 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] | 727 | { |
| 728 | struct nvme_ctrl *ctrl = rq->end_io_data; |
| 729 | |
| 730 | blk_mq_free_request(rq); |
| 731 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 732 | if (status) { |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 733 | dev_err(ctrl->device, |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 734 | "failed nvme_keep_alive_end_io error=%d\n", |
| 735 | status); |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 736 | return; |
| 737 | } |
| 738 | |
| 739 | schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ); |
| 740 | } |
| 741 | |
| 742 | static int nvme_keep_alive(struct nvme_ctrl *ctrl) |
| 743 | { |
| 744 | struct nvme_command c; |
| 745 | struct request *rq; |
| 746 | |
| 747 | memset(&c, 0, sizeof(c)); |
| 748 | c.common.opcode = nvme_admin_keep_alive; |
| 749 | |
| 750 | rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED, |
| 751 | NVME_QID_ANY); |
| 752 | if (IS_ERR(rq)) |
| 753 | return PTR_ERR(rq); |
| 754 | |
| 755 | rq->timeout = ctrl->kato * HZ; |
| 756 | rq->end_io_data = ctrl; |
| 757 | |
| 758 | blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io); |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | static void nvme_keep_alive_work(struct work_struct *work) |
| 764 | { |
| 765 | struct nvme_ctrl *ctrl = container_of(to_delayed_work(work), |
| 766 | struct nvme_ctrl, ka_work); |
| 767 | |
| 768 | if (nvme_keep_alive(ctrl)) { |
| 769 | /* allocation failure, reset the controller */ |
| 770 | dev_err(ctrl->device, "keep-alive failed\n"); |
Christoph Hellwig | 39bdc59 | 2017-06-12 18:21:19 +0200 | [diff] [blame] | 771 | nvme_reset_ctrl(ctrl); |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 772 | return; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | void nvme_start_keep_alive(struct nvme_ctrl *ctrl) |
| 777 | { |
| 778 | if (unlikely(ctrl->kato == 0)) |
| 779 | return; |
| 780 | |
| 781 | INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work); |
| 782 | schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ); |
| 783 | } |
| 784 | EXPORT_SYMBOL_GPL(nvme_start_keep_alive); |
| 785 | |
| 786 | void nvme_stop_keep_alive(struct nvme_ctrl *ctrl) |
| 787 | { |
| 788 | if (unlikely(ctrl->kato == 0)) |
| 789 | return; |
| 790 | |
| 791 | cancel_delayed_work_sync(&ctrl->ka_work); |
| 792 | } |
| 793 | EXPORT_SYMBOL_GPL(nvme_stop_keep_alive); |
| 794 | |
Keith Busch | 3f7f25a9 | 2017-06-20 15:09:56 -0400 | [diff] [blame] | 795 | static 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] | 796 | { |
| 797 | struct nvme_command c = { }; |
| 798 | int error; |
| 799 | |
| 800 | /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ |
| 801 | c.identify.opcode = nvme_admin_identify; |
Parav Pandit | 986994a | 2017-01-26 17:17:28 +0200 | [diff] [blame] | 802 | c.identify.cns = NVME_ID_CNS_CTRL; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 803 | |
| 804 | *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL); |
| 805 | if (!*id) |
| 806 | return -ENOMEM; |
| 807 | |
| 808 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, |
| 809 | sizeof(struct nvme_id_ctrl)); |
| 810 | if (error) |
| 811 | kfree(*id); |
| 812 | return error; |
| 813 | } |
| 814 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 815 | static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid, |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 816 | struct nvme_ns_ids *ids) |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 817 | { |
| 818 | struct nvme_command c = { }; |
| 819 | int status; |
| 820 | void *data; |
| 821 | int pos; |
| 822 | int len; |
| 823 | |
| 824 | c.identify.opcode = nvme_admin_identify; |
| 825 | c.identify.nsid = cpu_to_le32(nsid); |
| 826 | c.identify.cns = NVME_ID_CNS_NS_DESC_LIST; |
| 827 | |
| 828 | data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL); |
| 829 | if (!data) |
| 830 | return -ENOMEM; |
| 831 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 832 | status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data, |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 833 | NVME_IDENTIFY_DATA_SIZE); |
| 834 | if (status) |
| 835 | goto free_data; |
| 836 | |
| 837 | for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) { |
| 838 | struct nvme_ns_id_desc *cur = data + pos; |
| 839 | |
| 840 | if (cur->nidl == 0) |
| 841 | break; |
| 842 | |
| 843 | switch (cur->nidt) { |
| 844 | case NVME_NIDT_EUI64: |
| 845 | if (cur->nidl != NVME_NIDT_EUI64_LEN) { |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 846 | dev_warn(ctrl->device, |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 847 | "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n", |
| 848 | cur->nidl); |
| 849 | goto free_data; |
| 850 | } |
| 851 | len = NVME_NIDT_EUI64_LEN; |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 852 | memcpy(ids->eui64, data + pos + sizeof(*cur), len); |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 853 | break; |
| 854 | case NVME_NIDT_NGUID: |
| 855 | if (cur->nidl != NVME_NIDT_NGUID_LEN) { |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 856 | dev_warn(ctrl->device, |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 857 | "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n", |
| 858 | cur->nidl); |
| 859 | goto free_data; |
| 860 | } |
| 861 | len = NVME_NIDT_NGUID_LEN; |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 862 | memcpy(ids->nguid, data + pos + sizeof(*cur), len); |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 863 | break; |
| 864 | case NVME_NIDT_UUID: |
| 865 | if (cur->nidl != NVME_NIDT_UUID_LEN) { |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 866 | dev_warn(ctrl->device, |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 867 | "ctrl returned bogus length: %d for NVME_NIDT_UUID\n", |
| 868 | cur->nidl); |
| 869 | goto free_data; |
| 870 | } |
| 871 | len = NVME_NIDT_UUID_LEN; |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 872 | uuid_copy(&ids->uuid, data + pos + sizeof(*cur)); |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 873 | break; |
| 874 | default: |
| 875 | /* Skip unnkown types */ |
| 876 | len = cur->nidl; |
| 877 | break; |
| 878 | } |
| 879 | |
| 880 | len += sizeof(*cur); |
| 881 | } |
| 882 | free_data: |
| 883 | kfree(data); |
| 884 | return status; |
| 885 | } |
| 886 | |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 887 | static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list) |
| 888 | { |
| 889 | struct nvme_command c = { }; |
| 890 | |
| 891 | c.identify.opcode = nvme_admin_identify; |
Parav Pandit | 986994a | 2017-01-26 17:17:28 +0200 | [diff] [blame] | 892 | c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST; |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 893 | c.identify.nsid = cpu_to_le32(nsid); |
| 894 | return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000); |
| 895 | } |
| 896 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 897 | static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl, |
| 898 | unsigned nsid) |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 899 | { |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 900 | struct nvme_id_ns *id; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 901 | struct nvme_command c = { }; |
| 902 | int error; |
| 903 | |
| 904 | /* 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] | 905 | c.identify.opcode = nvme_admin_identify; |
| 906 | c.identify.nsid = cpu_to_le32(nsid); |
Parav Pandit | 986994a | 2017-01-26 17:17:28 +0200 | [diff] [blame] | 907 | c.identify.cns = NVME_ID_CNS_NS; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 908 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 909 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
| 910 | if (!id) |
| 911 | return NULL; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 912 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 913 | error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id)); |
| 914 | if (error) { |
| 915 | dev_warn(ctrl->device, "Identify namespace failed\n"); |
| 916 | kfree(id); |
| 917 | return NULL; |
| 918 | } |
| 919 | |
| 920 | return id; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 921 | } |
| 922 | |
Keith Busch | 3f7f25a9 | 2017-06-20 15:09:56 -0400 | [diff] [blame] | 923 | static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11, |
Andy Lutomirski | 1a6fe74 | 2016-09-16 11:16:10 -0700 | [diff] [blame] | 924 | void *buffer, size_t buflen, u32 *result) |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 925 | { |
| 926 | struct nvme_command c; |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 927 | union nvme_result res; |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame] | 928 | int ret; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 929 | |
| 930 | memset(&c, 0, sizeof(c)); |
| 931 | c.features.opcode = nvme_admin_set_features; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 932 | c.features.fid = cpu_to_le32(fid); |
| 933 | c.features.dword11 = cpu_to_le32(dword11); |
| 934 | |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 935 | ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, |
Andy Lutomirski | 1a6fe74 | 2016-09-16 11:16:10 -0700 | [diff] [blame] | 936 | buffer, buflen, 0, NVME_QID_ANY, 0, 0); |
Andy Lutomirski | 9b47f77a | 2016-08-24 03:52:12 -0700 | [diff] [blame] | 937 | if (ret >= 0 && result) |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 938 | *result = le32_to_cpu(res.u32); |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame] | 939 | return ret; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 940 | } |
| 941 | |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 942 | int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count) |
| 943 | { |
| 944 | u32 q_count = (*count - 1) | ((*count - 1) << 16); |
| 945 | u32 result; |
| 946 | int status, nr_io_queues; |
| 947 | |
Andy Lutomirski | 1a6fe74 | 2016-09-16 11:16:10 -0700 | [diff] [blame] | 948 | 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] | 949 | &result); |
Christoph Hellwig | f5fa90d | 2016-06-06 23:20:50 +0200 | [diff] [blame] | 950 | if (status < 0) |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 951 | return status; |
| 952 | |
Christoph Hellwig | f5fa90d | 2016-06-06 23:20:50 +0200 | [diff] [blame] | 953 | /* |
| 954 | * Degraded controllers might return an error when setting the queue |
| 955 | * count. We still want to be able to bring them online and offer |
| 956 | * access to the admin queue, as that might be only way to fix them up. |
| 957 | */ |
| 958 | if (status > 0) { |
Johannes Thumshirn | f0425db | 2017-06-09 16:17:21 +0200 | [diff] [blame] | 959 | dev_err(ctrl->device, "Could not set queue count (%d)\n", status); |
Christoph Hellwig | f5fa90d | 2016-06-06 23:20:50 +0200 | [diff] [blame] | 960 | *count = 0; |
| 961 | } else { |
| 962 | nr_io_queues = min(result & 0xffff, result >> 16) + 1; |
| 963 | *count = min(*count, nr_io_queues); |
| 964 | } |
| 965 | |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 966 | return 0; |
| 967 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 968 | EXPORT_SYMBOL_GPL(nvme_set_queue_count); |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 969 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 970 | static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) |
| 971 | { |
| 972 | struct nvme_user_io io; |
| 973 | struct nvme_command c; |
| 974 | unsigned length, meta_len; |
| 975 | void __user *metadata; |
| 976 | |
| 977 | if (copy_from_user(&io, uio, sizeof(io))) |
| 978 | return -EFAULT; |
Keith Busch | 63088ec | 2016-02-24 09:15:57 -0700 | [diff] [blame] | 979 | if (io.flags) |
| 980 | return -EINVAL; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 981 | |
| 982 | switch (io.opcode) { |
| 983 | case nvme_cmd_write: |
| 984 | case nvme_cmd_read: |
| 985 | case nvme_cmd_compare: |
| 986 | break; |
| 987 | default: |
| 988 | return -EINVAL; |
| 989 | } |
| 990 | |
| 991 | length = (io.nblocks + 1) << ns->lba_shift; |
| 992 | meta_len = (io.nblocks + 1) * ns->ms; |
| 993 | metadata = (void __user *)(uintptr_t)io.metadata; |
| 994 | |
| 995 | if (ns->ext) { |
| 996 | length += meta_len; |
| 997 | meta_len = 0; |
| 998 | } else if (meta_len) { |
| 999 | if ((io.metadata & 3) || !io.metadata) |
| 1000 | return -EINVAL; |
| 1001 | } |
| 1002 | |
| 1003 | memset(&c, 0, sizeof(c)); |
| 1004 | c.rw.opcode = io.opcode; |
| 1005 | c.rw.flags = io.flags; |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 1006 | c.rw.nsid = cpu_to_le32(ns->head->ns_id); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1007 | c.rw.slba = cpu_to_le64(io.slba); |
| 1008 | c.rw.length = cpu_to_le16(io.nblocks); |
| 1009 | c.rw.control = cpu_to_le16(io.control); |
| 1010 | c.rw.dsmgmt = cpu_to_le32(io.dsmgmt); |
| 1011 | c.rw.reftag = cpu_to_le32(io.reftag); |
| 1012 | c.rw.apptag = cpu_to_le16(io.apptag); |
| 1013 | c.rw.appmask = cpu_to_le16(io.appmask); |
| 1014 | |
Keith Busch | 63263d6 | 2017-08-29 17:46:04 -0400 | [diff] [blame] | 1015 | return nvme_submit_user_cmd(ns->queue, &c, |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1016 | (void __user *)(uintptr_t)io.addr, length, |
| 1017 | metadata, meta_len, io.slba, NULL, 0); |
| 1018 | } |
| 1019 | |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 1020 | static u32 nvme_known_admin_effects(u8 opcode) |
| 1021 | { |
| 1022 | switch (opcode) { |
| 1023 | case nvme_admin_format_nvm: |
| 1024 | return NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC | |
| 1025 | NVME_CMD_EFFECTS_CSE_MASK; |
| 1026 | case nvme_admin_sanitize_nvm: |
| 1027 | return NVME_CMD_EFFECTS_CSE_MASK; |
| 1028 | default: |
| 1029 | break; |
| 1030 | } |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
| 1034 | static u32 nvme_passthru_start(struct nvme_ctrl *ctrl, struct nvme_ns *ns, |
| 1035 | u8 opcode) |
| 1036 | { |
| 1037 | u32 effects = 0; |
| 1038 | |
| 1039 | if (ns) { |
| 1040 | if (ctrl->effects) |
| 1041 | effects = le32_to_cpu(ctrl->effects->iocs[opcode]); |
| 1042 | if (effects & ~NVME_CMD_EFFECTS_CSUPP) |
| 1043 | dev_warn(ctrl->device, |
| 1044 | "IO command:%02x has unhandled effects:%08x\n", |
| 1045 | opcode, effects); |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | if (ctrl->effects) |
| 1050 | effects = le32_to_cpu(ctrl->effects->iocs[opcode]); |
| 1051 | else |
| 1052 | effects = nvme_known_admin_effects(opcode); |
| 1053 | |
| 1054 | /* |
| 1055 | * For simplicity, IO to all namespaces is quiesced even if the command |
| 1056 | * effects say only one namespace is affected. |
| 1057 | */ |
| 1058 | if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) { |
| 1059 | nvme_start_freeze(ctrl); |
| 1060 | nvme_wait_freeze(ctrl); |
| 1061 | } |
| 1062 | return effects; |
| 1063 | } |
| 1064 | |
| 1065 | static void nvme_update_formats(struct nvme_ctrl *ctrl) |
| 1066 | { |
| 1067 | struct nvme_ns *ns; |
| 1068 | |
| 1069 | mutex_lock(&ctrl->namespaces_mutex); |
| 1070 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
| 1071 | if (ns->disk && nvme_revalidate_disk(ns->disk)) |
| 1072 | nvme_ns_remove(ns); |
| 1073 | } |
| 1074 | mutex_unlock(&ctrl->namespaces_mutex); |
| 1075 | } |
| 1076 | |
| 1077 | static void nvme_passthru_end(struct nvme_ctrl *ctrl, u32 effects) |
| 1078 | { |
| 1079 | /* |
| 1080 | * Revalidate LBA changes prior to unfreezing. This is necessary to |
| 1081 | * prevent memory corruption if a logical block size was changed by |
| 1082 | * this command. |
| 1083 | */ |
| 1084 | if (effects & NVME_CMD_EFFECTS_LBCC) |
| 1085 | nvme_update_formats(ctrl); |
| 1086 | if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) |
| 1087 | nvme_unfreeze(ctrl); |
| 1088 | if (effects & NVME_CMD_EFFECTS_CCC) |
| 1089 | nvme_init_identify(ctrl); |
| 1090 | if (effects & (NVME_CMD_EFFECTS_NIC | NVME_CMD_EFFECTS_NCC)) |
| 1091 | nvme_queue_scan(ctrl); |
| 1092 | } |
| 1093 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1094 | 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] | 1095 | struct nvme_passthru_cmd __user *ucmd) |
| 1096 | { |
| 1097 | struct nvme_passthru_cmd cmd; |
| 1098 | struct nvme_command c; |
| 1099 | unsigned timeout = 0; |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 1100 | u32 effects; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1101 | int status; |
| 1102 | |
| 1103 | if (!capable(CAP_SYS_ADMIN)) |
| 1104 | return -EACCES; |
| 1105 | if (copy_from_user(&cmd, ucmd, sizeof(cmd))) |
| 1106 | return -EFAULT; |
Keith Busch | 63088ec | 2016-02-24 09:15:57 -0700 | [diff] [blame] | 1107 | if (cmd.flags) |
| 1108 | return -EINVAL; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1109 | |
| 1110 | memset(&c, 0, sizeof(c)); |
| 1111 | c.common.opcode = cmd.opcode; |
| 1112 | c.common.flags = cmd.flags; |
| 1113 | c.common.nsid = cpu_to_le32(cmd.nsid); |
| 1114 | c.common.cdw2[0] = cpu_to_le32(cmd.cdw2); |
| 1115 | c.common.cdw2[1] = cpu_to_le32(cmd.cdw3); |
| 1116 | c.common.cdw10[0] = cpu_to_le32(cmd.cdw10); |
| 1117 | c.common.cdw10[1] = cpu_to_le32(cmd.cdw11); |
| 1118 | c.common.cdw10[2] = cpu_to_le32(cmd.cdw12); |
| 1119 | c.common.cdw10[3] = cpu_to_le32(cmd.cdw13); |
| 1120 | c.common.cdw10[4] = cpu_to_le32(cmd.cdw14); |
| 1121 | c.common.cdw10[5] = cpu_to_le32(cmd.cdw15); |
| 1122 | |
| 1123 | if (cmd.timeout_ms) |
| 1124 | timeout = msecs_to_jiffies(cmd.timeout_ms); |
| 1125 | |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 1126 | effects = nvme_passthru_start(ctrl, ns, cmd.opcode); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1127 | status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c, |
Arnd Bergmann | d1ea7be | 2015-12-08 16:22:17 +0100 | [diff] [blame] | 1128 | (void __user *)(uintptr_t)cmd.addr, cmd.data_len, |
Keith Busch | 63263d6 | 2017-08-29 17:46:04 -0400 | [diff] [blame] | 1129 | (void __user *)(uintptr_t)cmd.metadata, cmd.metadata, |
| 1130 | 0, &cmd.result, timeout); |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 1131 | nvme_passthru_end(ctrl, effects); |
| 1132 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1133 | if (status >= 0) { |
| 1134 | if (put_user(cmd.result, &ucmd->result)) |
| 1135 | return -EFAULT; |
| 1136 | } |
| 1137 | |
| 1138 | return status; |
| 1139 | } |
| 1140 | |
| 1141 | static int nvme_ioctl(struct block_device *bdev, fmode_t mode, |
| 1142 | unsigned int cmd, unsigned long arg) |
| 1143 | { |
| 1144 | struct nvme_ns *ns = bdev->bd_disk->private_data; |
| 1145 | |
| 1146 | switch (cmd) { |
| 1147 | case NVME_IOCTL_ID: |
| 1148 | force_successful_syscall_return(); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 1149 | return ns->head->ns_id; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1150 | case NVME_IOCTL_ADMIN_CMD: |
| 1151 | return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg); |
| 1152 | case NVME_IOCTL_IO_CMD: |
| 1153 | return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg); |
| 1154 | case NVME_IOCTL_SUBMIT_IO: |
| 1155 | return nvme_submit_io(ns, (void __user *)arg); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1156 | default: |
Matias Bjørling | 84d4add | 2017-01-31 13:17:16 +0100 | [diff] [blame] | 1157 | #ifdef CONFIG_NVM |
| 1158 | if (ns->ndev) |
| 1159 | return nvme_nvm_ioctl(ns, cmd, arg); |
| 1160 | #endif |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1161 | if (is_sed_ioctl(cmd)) |
Christoph Hellwig | 4f1244c | 2017-02-17 13:59:39 +0100 | [diff] [blame] | 1162 | return sed_ioctl(ns->ctrl->opal_dev, cmd, |
Scott Bauer | e225c20 | 2017-02-14 17:29:36 -0700 | [diff] [blame] | 1163 | (void __user *) arg); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1164 | return -ENOTTY; |
| 1165 | } |
| 1166 | } |
| 1167 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1168 | static int nvme_open(struct block_device *bdev, fmode_t mode) |
| 1169 | { |
Christoph Hellwig | c6424a9 | 2017-10-18 13:22:00 +0200 | [diff] [blame] | 1170 | struct nvme_ns *ns = bdev->bd_disk->private_data; |
| 1171 | |
| 1172 | if (!kref_get_unless_zero(&ns->kref)) |
| 1173 | return -ENXIO; |
Christoph Hellwig | c6424a9 | 2017-10-18 13:22:00 +0200 | [diff] [blame] | 1174 | return 0; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | static void nvme_release(struct gendisk *disk, fmode_t mode) |
| 1178 | { |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 1179 | nvme_put_ns(disk->private_data); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 1183 | { |
| 1184 | /* some standard values */ |
| 1185 | geo->heads = 1 << 6; |
| 1186 | geo->sectors = 1 << 5; |
| 1187 | geo->cylinders = get_capacity(bdev->bd_disk) >> 11; |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
| 1191 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
Christoph Hellwig | 39b7baa | 2017-11-02 21:28:53 +0300 | [diff] [blame] | 1192 | static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1193 | { |
| 1194 | struct blk_integrity integrity; |
| 1195 | |
Jay Freyensee | fa9a89f | 2016-07-20 21:26:16 -0600 | [diff] [blame] | 1196 | memset(&integrity, 0, sizeof(integrity)); |
Christoph Hellwig | 39b7baa | 2017-11-02 21:28:53 +0300 | [diff] [blame] | 1197 | switch (pi_type) { |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1198 | case NVME_NS_DPS_PI_TYPE3: |
| 1199 | integrity.profile = &t10_pi_type3_crc; |
Nicholas Bellinger | ba36c21 | 2016-04-09 03:04:42 +0000 | [diff] [blame] | 1200 | integrity.tag_size = sizeof(u16) + sizeof(u32); |
| 1201 | integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1202 | break; |
| 1203 | case NVME_NS_DPS_PI_TYPE1: |
| 1204 | case NVME_NS_DPS_PI_TYPE2: |
| 1205 | integrity.profile = &t10_pi_type1_crc; |
Nicholas Bellinger | ba36c21 | 2016-04-09 03:04:42 +0000 | [diff] [blame] | 1206 | integrity.tag_size = sizeof(u16); |
| 1207 | integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1208 | break; |
| 1209 | default: |
| 1210 | integrity.profile = NULL; |
| 1211 | break; |
| 1212 | } |
Christoph Hellwig | 39b7baa | 2017-11-02 21:28:53 +0300 | [diff] [blame] | 1213 | integrity.tuple_size = ms; |
| 1214 | blk_integrity_register(disk, &integrity); |
| 1215 | blk_queue_max_integrity_segments(disk->queue, 1); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1216 | } |
| 1217 | #else |
Christoph Hellwig | 39b7baa | 2017-11-02 21:28:53 +0300 | [diff] [blame] | 1218 | static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1219 | { |
| 1220 | } |
| 1221 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ |
| 1222 | |
Scott Bauer | 6b8190d | 2017-06-15 10:44:30 -0600 | [diff] [blame] | 1223 | static void nvme_set_chunk_size(struct nvme_ns *ns) |
| 1224 | { |
| 1225 | u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9)); |
| 1226 | blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size)); |
| 1227 | } |
| 1228 | |
Christoph Hellwig | 30e5e92 | 2017-11-02 21:28:54 +0300 | [diff] [blame] | 1229 | static void nvme_config_discard(struct nvme_ctrl *ctrl, |
| 1230 | unsigned stream_alignment, struct request_queue *queue) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1231 | { |
Christoph Hellwig | 30e5e92 | 2017-11-02 21:28:54 +0300 | [diff] [blame] | 1232 | u32 size = queue_logical_block_size(queue); |
| 1233 | |
| 1234 | if (stream_alignment) |
| 1235 | size *= stream_alignment; |
Keith Busch | 08095e7 | 2016-03-04 13:15:17 -0700 | [diff] [blame] | 1236 | |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 1237 | BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) < |
| 1238 | NVME_DSM_MAX_RANGES); |
| 1239 | |
Christoph Hellwig | 30e5e92 | 2017-11-02 21:28:54 +0300 | [diff] [blame] | 1240 | queue->limits.discard_alignment = size; |
| 1241 | queue->limits.discard_granularity = size; |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 1242 | |
Christoph Hellwig | 30e5e92 | 2017-11-02 21:28:54 +0300 | [diff] [blame] | 1243 | blk_queue_max_discard_sectors(queue, UINT_MAX); |
| 1244 | blk_queue_max_discard_segments(queue, NVME_DSM_MAX_RANGES); |
| 1245 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, queue); |
Christoph Hellwig | e850fd1 | 2017-04-05 19:21:13 +0200 | [diff] [blame] | 1246 | |
| 1247 | if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES) |
Christoph Hellwig | 30e5e92 | 2017-11-02 21:28:54 +0300 | [diff] [blame] | 1248 | blk_queue_max_write_zeroes_sectors(queue, UINT_MAX); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1249 | } |
| 1250 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1251 | static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid, |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 1252 | struct nvme_id_ns *id, struct nvme_ns_ids *ids) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1253 | { |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 1254 | memset(ids, 0, sizeof(*ids)); |
| 1255 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1256 | if (ctrl->vs >= NVME_VS(1, 1, 0)) |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 1257 | memcpy(ids->eui64, id->eui64, sizeof(id->eui64)); |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1258 | if (ctrl->vs >= NVME_VS(1, 2, 0)) |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 1259 | memcpy(ids->nguid, id->nguid, sizeof(id->nguid)); |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1260 | if (ctrl->vs >= NVME_VS(1, 3, 0)) { |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 1261 | /* Don't treat error as fatal we potentially |
| 1262 | * already have a NGUID or EUI-64 |
| 1263 | */ |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 1264 | if (nvme_identify_ns_descs(ctrl, nsid, ids)) |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1265 | dev_warn(ctrl->device, |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 1266 | "%s: Identify Descriptors failed\n", __func__); |
| 1267 | } |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1268 | } |
| 1269 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 1270 | static bool nvme_ns_ids_valid(struct nvme_ns_ids *ids) |
| 1271 | { |
| 1272 | return !uuid_is_null(&ids->uuid) || |
| 1273 | memchr_inv(ids->nguid, 0, sizeof(ids->nguid)) || |
| 1274 | memchr_inv(ids->eui64, 0, sizeof(ids->eui64)); |
| 1275 | } |
| 1276 | |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 1277 | static bool nvme_ns_ids_equal(struct nvme_ns_ids *a, struct nvme_ns_ids *b) |
| 1278 | { |
| 1279 | return uuid_equal(&a->uuid, &b->uuid) && |
| 1280 | memcmp(&a->nguid, &b->nguid, sizeof(a->nguid)) == 0 && |
| 1281 | memcmp(&a->eui64, &b->eui64, sizeof(a->eui64)) == 0; |
| 1282 | } |
| 1283 | |
Christoph Hellwig | 24b0b58 | 2017-11-02 21:28:56 +0300 | [diff] [blame] | 1284 | static void nvme_update_disk_info(struct gendisk *disk, |
| 1285 | struct nvme_ns *ns, struct nvme_id_ns *id) |
| 1286 | { |
| 1287 | sector_t capacity = le64_to_cpup(&id->nsze) << (ns->lba_shift - 9); |
| 1288 | unsigned stream_alignment = 0; |
| 1289 | |
| 1290 | if (ns->ctrl->nr_streams && ns->sws && ns->sgs) |
| 1291 | stream_alignment = ns->sws * ns->sgs; |
| 1292 | |
| 1293 | blk_mq_freeze_queue(disk->queue); |
| 1294 | blk_integrity_unregister(disk); |
| 1295 | |
| 1296 | blk_queue_logical_block_size(disk->queue, 1 << ns->lba_shift); |
| 1297 | if (ns->ms && !ns->ext && |
| 1298 | (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED)) |
| 1299 | nvme_init_integrity(disk, ns->ms, ns->pi_type); |
Christoph Hellwig | 715ea9e | 2017-11-07 17:27:34 +0100 | [diff] [blame] | 1300 | if (ns->ms && !nvme_ns_has_pi(ns) && !blk_get_integrity(disk)) |
Christoph Hellwig | 24b0b58 | 2017-11-02 21:28:56 +0300 | [diff] [blame] | 1301 | capacity = 0; |
| 1302 | set_capacity(disk, capacity); |
| 1303 | |
| 1304 | if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM) |
| 1305 | nvme_config_discard(ns->ctrl, stream_alignment, disk->queue); |
| 1306 | blk_mq_unfreeze_queue(disk->queue); |
| 1307 | } |
| 1308 | |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1309 | static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id) |
| 1310 | { |
| 1311 | struct nvme_ns *ns = disk->private_data; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1312 | |
| 1313 | /* |
| 1314 | * If identify namespace failed, use default 512 byte block size so |
| 1315 | * block layer can use before failing read/write for 0 capacity. |
| 1316 | */ |
Christoph Hellwig | c81bfba | 2017-05-20 15:14:45 +0200 | [diff] [blame] | 1317 | 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] | 1318 | if (ns->lba_shift == 0) |
| 1319 | ns->lba_shift = 9; |
Scott Bauer | 6b8190d | 2017-06-15 10:44:30 -0600 | [diff] [blame] | 1320 | ns->noiob = le16_to_cpu(id->noiob); |
Christoph Hellwig | b5be3b3 | 2017-11-02 21:28:52 +0300 | [diff] [blame] | 1321 | ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT); |
| 1322 | ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms); |
| 1323 | /* the PI implementation requires metadata equal t10 pi tuple size */ |
| 1324 | if (ns->ms == sizeof(struct t10_pi_tuple)) |
| 1325 | ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK; |
| 1326 | else |
| 1327 | ns->pi_type = 0; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1328 | |
Christoph Hellwig | 6e78f21 | 2017-11-02 21:28:55 +0300 | [diff] [blame] | 1329 | if (ns->noiob) |
| 1330 | nvme_set_chunk_size(ns); |
Christoph Hellwig | 24b0b58 | 2017-11-02 21:28:56 +0300 | [diff] [blame] | 1331 | nvme_update_disk_info(disk, ns, id); |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1332 | } |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1333 | |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1334 | static int nvme_revalidate_disk(struct gendisk *disk) |
| 1335 | { |
| 1336 | struct nvme_ns *ns = disk->private_data; |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1337 | struct nvme_ctrl *ctrl = ns->ctrl; |
| 1338 | struct nvme_id_ns *id; |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 1339 | struct nvme_ns_ids ids; |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1340 | int ret = 0; |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1341 | |
| 1342 | if (test_bit(NVME_NS_DEAD, &ns->flags)) { |
| 1343 | set_capacity(disk, 0); |
| 1344 | return -ENODEV; |
| 1345 | } |
| 1346 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 1347 | id = nvme_identify_ns(ctrl, ns->head->ns_id); |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1348 | if (!id) |
| 1349 | return -ENODEV; |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1350 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1351 | if (id->ncap == 0) { |
| 1352 | ret = -ENODEV; |
| 1353 | goto out; |
| 1354 | } |
| 1355 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 1356 | nvme_report_ns_ids(ctrl, ns->head->ns_id, id, &ids); |
| 1357 | if (!nvme_ns_ids_equal(&ns->head->ids, &ids)) { |
Christoph Hellwig | 1d5df6a | 2017-08-17 14:10:00 +0200 | [diff] [blame] | 1358 | dev_err(ctrl->device, |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 1359 | "identifiers changed for nsid %d\n", ns->head->ns_id); |
Christoph Hellwig | 1d5df6a | 2017-08-17 14:10:00 +0200 | [diff] [blame] | 1360 | ret = -ENODEV; |
| 1361 | } |
| 1362 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1363 | out: |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1364 | kfree(id); |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1365 | return ret; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | static char nvme_pr_type(enum pr_type type) |
| 1369 | { |
| 1370 | switch (type) { |
| 1371 | case PR_WRITE_EXCLUSIVE: |
| 1372 | return 1; |
| 1373 | case PR_EXCLUSIVE_ACCESS: |
| 1374 | return 2; |
| 1375 | case PR_WRITE_EXCLUSIVE_REG_ONLY: |
| 1376 | return 3; |
| 1377 | case PR_EXCLUSIVE_ACCESS_REG_ONLY: |
| 1378 | return 4; |
| 1379 | case PR_WRITE_EXCLUSIVE_ALL_REGS: |
| 1380 | return 5; |
| 1381 | case PR_EXCLUSIVE_ACCESS_ALL_REGS: |
| 1382 | return 6; |
| 1383 | default: |
| 1384 | return 0; |
| 1385 | } |
| 1386 | }; |
| 1387 | |
| 1388 | static int nvme_pr_command(struct block_device *bdev, u32 cdw10, |
| 1389 | u64 key, u64 sa_key, u8 op) |
| 1390 | { |
| 1391 | struct nvme_ns *ns = bdev->bd_disk->private_data; |
| 1392 | struct nvme_command c; |
| 1393 | u8 data[16] = { 0, }; |
| 1394 | |
| 1395 | put_unaligned_le64(key, &data[0]); |
| 1396 | put_unaligned_le64(sa_key, &data[8]); |
| 1397 | |
| 1398 | memset(&c, 0, sizeof(c)); |
| 1399 | c.common.opcode = op; |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 1400 | c.common.nsid = cpu_to_le32(ns->head->ns_id); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1401 | c.common.cdw10[0] = cpu_to_le32(cdw10); |
| 1402 | |
| 1403 | return nvme_submit_sync_cmd(ns->queue, &c, data, 16); |
| 1404 | } |
| 1405 | |
| 1406 | static int nvme_pr_register(struct block_device *bdev, u64 old, |
| 1407 | u64 new, unsigned flags) |
| 1408 | { |
| 1409 | u32 cdw10; |
| 1410 | |
| 1411 | if (flags & ~PR_FL_IGNORE_KEY) |
| 1412 | return -EOPNOTSUPP; |
| 1413 | |
| 1414 | cdw10 = old ? 2 : 0; |
| 1415 | cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0; |
| 1416 | cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */ |
| 1417 | return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register); |
| 1418 | } |
| 1419 | |
| 1420 | static int nvme_pr_reserve(struct block_device *bdev, u64 key, |
| 1421 | enum pr_type type, unsigned flags) |
| 1422 | { |
| 1423 | u32 cdw10; |
| 1424 | |
| 1425 | if (flags & ~PR_FL_IGNORE_KEY) |
| 1426 | return -EOPNOTSUPP; |
| 1427 | |
| 1428 | cdw10 = nvme_pr_type(type) << 8; |
| 1429 | cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0); |
| 1430 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire); |
| 1431 | } |
| 1432 | |
| 1433 | static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new, |
| 1434 | enum pr_type type, bool abort) |
| 1435 | { |
| 1436 | u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1; |
| 1437 | return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire); |
| 1438 | } |
| 1439 | |
| 1440 | static int nvme_pr_clear(struct block_device *bdev, u64 key) |
| 1441 | { |
Dan Carpenter | 8c0b391 | 2015-12-09 13:24:06 +0300 | [diff] [blame] | 1442 | u32 cdw10 = 1 | (key ? 1 << 3 : 0); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1443 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register); |
| 1444 | } |
| 1445 | |
| 1446 | static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type) |
| 1447 | { |
| 1448 | u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0; |
| 1449 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release); |
| 1450 | } |
| 1451 | |
| 1452 | static const struct pr_ops nvme_pr_ops = { |
| 1453 | .pr_register = nvme_pr_register, |
| 1454 | .pr_reserve = nvme_pr_reserve, |
| 1455 | .pr_release = nvme_pr_release, |
| 1456 | .pr_preempt = nvme_pr_preempt, |
| 1457 | .pr_clear = nvme_pr_clear, |
| 1458 | }; |
| 1459 | |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1460 | #ifdef CONFIG_BLK_SED_OPAL |
Christoph Hellwig | 4f1244c | 2017-02-17 13:59:39 +0100 | [diff] [blame] | 1461 | int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len, |
| 1462 | bool send) |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1463 | { |
Christoph Hellwig | 4f1244c | 2017-02-17 13:59:39 +0100 | [diff] [blame] | 1464 | struct nvme_ctrl *ctrl = data; |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1465 | struct nvme_command cmd; |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1466 | |
| 1467 | memset(&cmd, 0, sizeof(cmd)); |
| 1468 | if (send) |
| 1469 | cmd.common.opcode = nvme_admin_security_send; |
| 1470 | else |
| 1471 | cmd.common.opcode = nvme_admin_security_recv; |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1472 | cmd.common.nsid = 0; |
| 1473 | cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8); |
| 1474 | cmd.common.cdw10[1] = cpu_to_le32(len); |
| 1475 | |
| 1476 | return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len, |
| 1477 | ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0); |
| 1478 | } |
| 1479 | EXPORT_SYMBOL_GPL(nvme_sec_submit); |
| 1480 | #endif /* CONFIG_BLK_SED_OPAL */ |
| 1481 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1482 | static const struct block_device_operations nvme_fops = { |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1483 | .owner = THIS_MODULE, |
| 1484 | .ioctl = nvme_ioctl, |
Christoph Hellwig | 761f2e1 | 2017-10-05 18:46:46 +0200 | [diff] [blame] | 1485 | .compat_ioctl = nvme_ioctl, |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1486 | .open = nvme_open, |
| 1487 | .release = nvme_release, |
| 1488 | .getgeo = nvme_getgeo, |
| 1489 | .revalidate_disk= nvme_revalidate_disk, |
| 1490 | .pr_ops = &nvme_pr_ops, |
| 1491 | }; |
| 1492 | |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1493 | static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled) |
| 1494 | { |
| 1495 | unsigned long timeout = |
| 1496 | ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies; |
| 1497 | u32 csts, bit = enabled ? NVME_CSTS_RDY : 0; |
| 1498 | int ret; |
| 1499 | |
| 1500 | while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { |
Keith Busch | 0df1e4f | 2016-10-11 13:31:58 -0400 | [diff] [blame] | 1501 | if (csts == ~0) |
| 1502 | return -ENODEV; |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1503 | if ((csts & NVME_CSTS_RDY) == bit) |
| 1504 | break; |
| 1505 | |
| 1506 | msleep(100); |
| 1507 | if (fatal_signal_pending(current)) |
| 1508 | return -EINTR; |
| 1509 | if (time_after(jiffies, timeout)) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1510 | dev_err(ctrl->device, |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1511 | "Device not ready; aborting %s\n", enabled ? |
| 1512 | "initialisation" : "reset"); |
| 1513 | return -ENODEV; |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | return ret; |
| 1518 | } |
| 1519 | |
| 1520 | /* |
| 1521 | * If the device has been passed off to us in an enabled state, just clear |
| 1522 | * the enabled bit. The spec says we should set the 'shutdown notification |
| 1523 | * bits', but doing so may cause the device to complete commands to the |
| 1524 | * admin queue ... and we don't know what memory that might be pointing at! |
| 1525 | */ |
| 1526 | int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap) |
| 1527 | { |
| 1528 | int ret; |
| 1529 | |
| 1530 | ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; |
| 1531 | ctrl->ctrl_config &= ~NVME_CC_ENABLE; |
| 1532 | |
| 1533 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); |
| 1534 | if (ret) |
| 1535 | return ret; |
Guilherme G. Piccoli | 54adc01 | 2016-06-14 18:22:41 -0300 | [diff] [blame] | 1536 | |
Guilherme G. Piccoli | b5a10c5 | 2016-12-28 22:13:15 -0200 | [diff] [blame] | 1537 | if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY) |
Guilherme G. Piccoli | 54adc01 | 2016-06-14 18:22:41 -0300 | [diff] [blame] | 1538 | msleep(NVME_QUIRK_DELAY_AMOUNT); |
| 1539 | |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1540 | return nvme_wait_ready(ctrl, cap, false); |
| 1541 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1542 | EXPORT_SYMBOL_GPL(nvme_disable_ctrl); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1543 | |
| 1544 | int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap) |
| 1545 | { |
| 1546 | /* |
| 1547 | * Default to a 4K page size, with the intention to update this |
| 1548 | * path in the future to accomodate architectures with differing |
| 1549 | * kernel and IO page sizes. |
| 1550 | */ |
| 1551 | unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12; |
| 1552 | int ret; |
| 1553 | |
| 1554 | if (page_shift < dev_page_min) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1555 | dev_err(ctrl->device, |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1556 | "Minimum device page size %u too large for host (%u)\n", |
| 1557 | 1 << dev_page_min, 1 << page_shift); |
| 1558 | return -ENODEV; |
| 1559 | } |
| 1560 | |
| 1561 | ctrl->page_size = 1 << page_shift; |
| 1562 | |
| 1563 | ctrl->ctrl_config = NVME_CC_CSS_NVM; |
| 1564 | ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT; |
Max Gurtovoy | 60b43f6 | 2017-08-13 19:21:07 +0300 | [diff] [blame] | 1565 | ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE; |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1566 | ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; |
| 1567 | ctrl->ctrl_config |= NVME_CC_ENABLE; |
| 1568 | |
| 1569 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); |
| 1570 | if (ret) |
| 1571 | return ret; |
| 1572 | return nvme_wait_ready(ctrl, cap, true); |
| 1573 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1574 | EXPORT_SYMBOL_GPL(nvme_enable_ctrl); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1575 | |
| 1576 | int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl) |
| 1577 | { |
Martin K. Petersen | 07fbd32 | 2017-08-25 19:14:50 -0400 | [diff] [blame] | 1578 | unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1579 | u32 csts; |
| 1580 | int ret; |
| 1581 | |
| 1582 | ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; |
| 1583 | ctrl->ctrl_config |= NVME_CC_SHN_NORMAL; |
| 1584 | |
| 1585 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); |
| 1586 | if (ret) |
| 1587 | return ret; |
| 1588 | |
| 1589 | while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { |
| 1590 | if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT) |
| 1591 | break; |
| 1592 | |
| 1593 | msleep(100); |
| 1594 | if (fatal_signal_pending(current)) |
| 1595 | return -EINTR; |
| 1596 | if (time_after(jiffies, timeout)) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1597 | dev_err(ctrl->device, |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1598 | "Device shutdown incomplete; abort shutdown\n"); |
| 1599 | return -ENODEV; |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | return ret; |
| 1604 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1605 | EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1606 | |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1607 | static void nvme_set_queue_limits(struct nvme_ctrl *ctrl, |
| 1608 | struct request_queue *q) |
| 1609 | { |
Jens Axboe | 7c88cb0 | 2016-04-12 15:43:09 -0600 | [diff] [blame] | 1610 | bool vwc = false; |
| 1611 | |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1612 | if (ctrl->max_hw_sectors) { |
Christoph Hellwig | 45686b6 | 2016-03-02 18:07:12 +0100 | [diff] [blame] | 1613 | u32 max_segments = |
| 1614 | (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1; |
| 1615 | |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1616 | blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors); |
Christoph Hellwig | 45686b6 | 2016-03-02 18:07:12 +0100 | [diff] [blame] | 1617 | blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX)); |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1618 | } |
Keith Busch | e6282ae | 2016-12-19 11:37:50 -0500 | [diff] [blame] | 1619 | if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) |
| 1620 | blk_queue_chunk_sectors(q, ctrl->max_hw_sectors); |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1621 | blk_queue_virt_boundary(q, ctrl->page_size - 1); |
Jens Axboe | 7c88cb0 | 2016-04-12 15:43:09 -0600 | [diff] [blame] | 1622 | if (ctrl->vwc & NVME_CTRL_VWC_PRESENT) |
| 1623 | vwc = true; |
| 1624 | blk_queue_write_cache(q, vwc, vwc); |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1625 | } |
| 1626 | |
Jon Derrick | dbf86b3 | 2017-08-16 09:51:29 +0200 | [diff] [blame] | 1627 | static int nvme_configure_timestamp(struct nvme_ctrl *ctrl) |
| 1628 | { |
| 1629 | __le64 ts; |
| 1630 | int ret; |
| 1631 | |
| 1632 | if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP)) |
| 1633 | return 0; |
| 1634 | |
| 1635 | ts = cpu_to_le64(ktime_to_ms(ktime_get_real())); |
| 1636 | ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts), |
| 1637 | NULL); |
| 1638 | if (ret) |
| 1639 | dev_warn_once(ctrl->device, |
| 1640 | "could not set timestamp (%d)\n", ret); |
| 1641 | return ret; |
| 1642 | } |
| 1643 | |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 1644 | static int nvme_configure_apst(struct nvme_ctrl *ctrl) |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1645 | { |
| 1646 | /* |
| 1647 | * APST (Autonomous Power State Transition) lets us program a |
| 1648 | * table of power state transitions that the controller will |
| 1649 | * perform automatically. We configure it with a simple |
| 1650 | * heuristic: we are willing to spend at most 2% of the time |
| 1651 | * transitioning between power states. Therefore, when running |
| 1652 | * in any given state, we will enter the next lower-power |
Andy Lutomirski | 76e4ad0 | 2017-04-21 16:19:22 -0700 | [diff] [blame] | 1653 | * non-operational state after waiting 50 * (enlat + exlat) |
Kai-Heng Feng | da87591 | 2017-06-07 15:25:42 +0800 | [diff] [blame] | 1654 | * microseconds, as long as that state's exit latency is under |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1655 | * the requested maximum latency. |
| 1656 | * |
| 1657 | * We will not autonomously enter any non-operational state for |
| 1658 | * which the total latency exceeds ps_max_latency_us. Users |
| 1659 | * can set ps_max_latency_us to zero to turn off APST. |
| 1660 | */ |
| 1661 | |
| 1662 | unsigned apste; |
| 1663 | struct nvme_feat_auto_pst *table; |
Andy Lutomirski | fb0dc39 | 2017-04-21 16:19:23 -0700 | [diff] [blame] | 1664 | u64 max_lat_us = 0; |
| 1665 | int max_ps = -1; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1666 | int ret; |
| 1667 | |
| 1668 | /* |
| 1669 | * If APST isn't supported or if we haven't been initialized yet, |
| 1670 | * then don't do anything. |
| 1671 | */ |
| 1672 | if (!ctrl->apsta) |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 1673 | return 0; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1674 | |
| 1675 | if (ctrl->npss > 31) { |
| 1676 | dev_warn(ctrl->device, "NPSS is invalid; not using APST\n"); |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 1677 | return 0; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
| 1681 | if (!table) |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 1682 | return 0; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1683 | |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 1684 | if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) { |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1685 | /* Turn off APST. */ |
| 1686 | apste = 0; |
Andy Lutomirski | fb0dc39 | 2017-04-21 16:19:23 -0700 | [diff] [blame] | 1687 | dev_dbg(ctrl->device, "APST disabled\n"); |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1688 | } else { |
| 1689 | __le64 target = cpu_to_le64(0); |
| 1690 | int state; |
| 1691 | |
| 1692 | /* |
| 1693 | * Walk through all states from lowest- to highest-power. |
| 1694 | * According to the spec, lower-numbered states use more |
| 1695 | * power. NPSS, despite the name, is the index of the |
| 1696 | * lowest-power state, not the number of states. |
| 1697 | */ |
| 1698 | for (state = (int)ctrl->npss; state >= 0; state--) { |
Kai-Heng Feng | da87591 | 2017-06-07 15:25:42 +0800 | [diff] [blame] | 1699 | u64 total_latency_us, exit_latency_us, transition_ms; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1700 | |
| 1701 | if (target) |
| 1702 | table->entries[state] = target; |
| 1703 | |
| 1704 | /* |
Andy Lutomirski | ff5350a | 2017-04-20 13:37:55 -0700 | [diff] [blame] | 1705 | * Don't allow transitions to the deepest state |
| 1706 | * if it's quirked off. |
| 1707 | */ |
| 1708 | if (state == ctrl->npss && |
| 1709 | (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) |
| 1710 | continue; |
| 1711 | |
| 1712 | /* |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1713 | * Is this state a useful non-operational state for |
| 1714 | * higher-power states to autonomously transition to? |
| 1715 | */ |
| 1716 | if (!(ctrl->psd[state].flags & |
| 1717 | NVME_PS_FLAGS_NON_OP_STATE)) |
| 1718 | continue; |
| 1719 | |
Kai-Heng Feng | da87591 | 2017-06-07 15:25:42 +0800 | [diff] [blame] | 1720 | exit_latency_us = |
| 1721 | (u64)le32_to_cpu(ctrl->psd[state].exit_lat); |
| 1722 | if (exit_latency_us > ctrl->ps_max_latency_us) |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1723 | continue; |
| 1724 | |
Kai-Heng Feng | da87591 | 2017-06-07 15:25:42 +0800 | [diff] [blame] | 1725 | total_latency_us = |
| 1726 | exit_latency_us + |
| 1727 | le32_to_cpu(ctrl->psd[state].entry_lat); |
| 1728 | |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1729 | /* |
| 1730 | * This state is good. Use it as the APST idle |
| 1731 | * target for higher power states. |
| 1732 | */ |
| 1733 | transition_ms = total_latency_us + 19; |
| 1734 | do_div(transition_ms, 20); |
| 1735 | if (transition_ms > (1 << 24) - 1) |
| 1736 | transition_ms = (1 << 24) - 1; |
| 1737 | |
| 1738 | target = cpu_to_le64((state << 3) | |
| 1739 | (transition_ms << 8)); |
Andy Lutomirski | fb0dc39 | 2017-04-21 16:19:23 -0700 | [diff] [blame] | 1740 | |
| 1741 | if (max_ps == -1) |
| 1742 | max_ps = state; |
| 1743 | |
| 1744 | if (total_latency_us > max_lat_us) |
| 1745 | max_lat_us = total_latency_us; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | apste = 1; |
Andy Lutomirski | fb0dc39 | 2017-04-21 16:19:23 -0700 | [diff] [blame] | 1749 | |
| 1750 | if (max_ps == -1) { |
| 1751 | dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n"); |
| 1752 | } else { |
| 1753 | dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n", |
| 1754 | max_ps, max_lat_us, (int)sizeof(*table), table); |
| 1755 | } |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste, |
| 1759 | table, sizeof(*table), NULL); |
| 1760 | if (ret) |
| 1761 | dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret); |
| 1762 | |
| 1763 | kfree(table); |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 1764 | return ret; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | static void nvme_set_latency_tolerance(struct device *dev, s32 val) |
| 1768 | { |
| 1769 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 1770 | u64 latency; |
| 1771 | |
| 1772 | switch (val) { |
| 1773 | case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT: |
| 1774 | case PM_QOS_LATENCY_ANY: |
| 1775 | latency = U64_MAX; |
| 1776 | break; |
| 1777 | |
| 1778 | default: |
| 1779 | latency = val; |
| 1780 | } |
| 1781 | |
| 1782 | if (ctrl->ps_max_latency_us != latency) { |
| 1783 | ctrl->ps_max_latency_us = latency; |
| 1784 | nvme_configure_apst(ctrl); |
| 1785 | } |
| 1786 | } |
| 1787 | |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 1788 | struct nvme_core_quirk_entry { |
| 1789 | /* |
| 1790 | * NVMe model and firmware strings are padded with spaces. For |
| 1791 | * simplicity, strings in the quirk table are padded with NULLs |
| 1792 | * instead. |
| 1793 | */ |
| 1794 | u16 vid; |
| 1795 | const char *mn; |
| 1796 | const char *fr; |
| 1797 | unsigned long quirks; |
| 1798 | }; |
| 1799 | |
| 1800 | static const struct nvme_core_quirk_entry core_quirks[] = { |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1801 | { |
Andy Lutomirski | be56945 | 2017-04-20 13:37:56 -0700 | [diff] [blame] | 1802 | /* |
| 1803 | * This Toshiba device seems to die using any APST states. See: |
| 1804 | * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11 |
| 1805 | */ |
| 1806 | .vid = 0x1179, |
| 1807 | .mn = "THNSF5256GPUK TOSHIBA", |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1808 | .quirks = NVME_QUIRK_NO_APST, |
Andy Lutomirski | be56945 | 2017-04-20 13:37:56 -0700 | [diff] [blame] | 1809 | } |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 1810 | }; |
| 1811 | |
| 1812 | /* match is null-terminated but idstr is space-padded. */ |
| 1813 | static bool string_matches(const char *idstr, const char *match, size_t len) |
| 1814 | { |
| 1815 | size_t matchlen; |
| 1816 | |
| 1817 | if (!match) |
| 1818 | return true; |
| 1819 | |
| 1820 | matchlen = strlen(match); |
| 1821 | WARN_ON_ONCE(matchlen > len); |
| 1822 | |
| 1823 | if (memcmp(idstr, match, matchlen)) |
| 1824 | return false; |
| 1825 | |
| 1826 | for (; matchlen < len; matchlen++) |
| 1827 | if (idstr[matchlen] != ' ') |
| 1828 | return false; |
| 1829 | |
| 1830 | return true; |
| 1831 | } |
| 1832 | |
| 1833 | static bool quirk_matches(const struct nvme_id_ctrl *id, |
| 1834 | const struct nvme_core_quirk_entry *q) |
| 1835 | { |
| 1836 | return q->vid == le16_to_cpu(id->vid) && |
| 1837 | string_matches(id->mn, q->mn, sizeof(id->mn)) && |
| 1838 | string_matches(id->fr, q->fr, sizeof(id->fr)); |
| 1839 | } |
| 1840 | |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 1841 | static void nvme_init_subnqn(struct nvme_subsystem *subsys, struct nvme_ctrl *ctrl, |
| 1842 | struct nvme_id_ctrl *id) |
Christoph Hellwig | 180de007 | 2017-06-26 12:39:02 +0200 | [diff] [blame] | 1843 | { |
| 1844 | size_t nqnlen; |
| 1845 | int off; |
| 1846 | |
| 1847 | nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE); |
| 1848 | if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) { |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 1849 | strncpy(subsys->subnqn, id->subnqn, NVMF_NQN_SIZE); |
Christoph Hellwig | 180de007 | 2017-06-26 12:39:02 +0200 | [diff] [blame] | 1850 | return; |
| 1851 | } |
| 1852 | |
| 1853 | if (ctrl->vs >= NVME_VS(1, 2, 1)) |
| 1854 | dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n"); |
| 1855 | |
| 1856 | /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */ |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 1857 | off = snprintf(subsys->subnqn, NVMF_NQN_SIZE, |
Christoph Hellwig | 180de007 | 2017-06-26 12:39:02 +0200 | [diff] [blame] | 1858 | "nqn.2014.08.org.nvmexpress:%4x%4x", |
| 1859 | le16_to_cpu(id->vid), le16_to_cpu(id->ssvid)); |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 1860 | memcpy(subsys->subnqn + off, id->sn, sizeof(id->sn)); |
Christoph Hellwig | 180de007 | 2017-06-26 12:39:02 +0200 | [diff] [blame] | 1861 | off += sizeof(id->sn); |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 1862 | memcpy(subsys->subnqn + off, id->mn, sizeof(id->mn)); |
Christoph Hellwig | 180de007 | 2017-06-26 12:39:02 +0200 | [diff] [blame] | 1863 | off += sizeof(id->mn); |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 1864 | memset(subsys->subnqn + off, 0, sizeof(subsys->subnqn) - off); |
| 1865 | } |
| 1866 | |
| 1867 | static void __nvme_release_subsystem(struct nvme_subsystem *subsys) |
| 1868 | { |
| 1869 | ida_simple_remove(&nvme_subsystems_ida, subsys->instance); |
| 1870 | kfree(subsys); |
| 1871 | } |
| 1872 | |
| 1873 | static void nvme_release_subsystem(struct device *dev) |
| 1874 | { |
| 1875 | __nvme_release_subsystem(container_of(dev, struct nvme_subsystem, dev)); |
| 1876 | } |
| 1877 | |
| 1878 | static void nvme_destroy_subsystem(struct kref *ref) |
| 1879 | { |
| 1880 | struct nvme_subsystem *subsys = |
| 1881 | container_of(ref, struct nvme_subsystem, ref); |
| 1882 | |
| 1883 | mutex_lock(&nvme_subsystems_lock); |
| 1884 | list_del(&subsys->entry); |
| 1885 | mutex_unlock(&nvme_subsystems_lock); |
| 1886 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 1887 | ida_destroy(&subsys->ns_ida); |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 1888 | device_del(&subsys->dev); |
| 1889 | put_device(&subsys->dev); |
| 1890 | } |
| 1891 | |
| 1892 | static void nvme_put_subsystem(struct nvme_subsystem *subsys) |
| 1893 | { |
| 1894 | kref_put(&subsys->ref, nvme_destroy_subsystem); |
| 1895 | } |
| 1896 | |
| 1897 | static struct nvme_subsystem *__nvme_find_get_subsystem(const char *subsysnqn) |
| 1898 | { |
| 1899 | struct nvme_subsystem *subsys; |
| 1900 | |
| 1901 | lockdep_assert_held(&nvme_subsystems_lock); |
| 1902 | |
| 1903 | list_for_each_entry(subsys, &nvme_subsystems, entry) { |
| 1904 | if (strcmp(subsys->subnqn, subsysnqn)) |
| 1905 | continue; |
| 1906 | if (!kref_get_unless_zero(&subsys->ref)) |
| 1907 | continue; |
| 1908 | return subsys; |
| 1909 | } |
| 1910 | |
| 1911 | return NULL; |
| 1912 | } |
| 1913 | |
| 1914 | static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) |
| 1915 | { |
| 1916 | struct nvme_subsystem *subsys, *found; |
| 1917 | int ret; |
| 1918 | |
| 1919 | subsys = kzalloc(sizeof(*subsys), GFP_KERNEL); |
| 1920 | if (!subsys) |
| 1921 | return -ENOMEM; |
| 1922 | ret = ida_simple_get(&nvme_subsystems_ida, 0, 0, GFP_KERNEL); |
| 1923 | if (ret < 0) { |
| 1924 | kfree(subsys); |
| 1925 | return ret; |
| 1926 | } |
| 1927 | subsys->instance = ret; |
| 1928 | mutex_init(&subsys->lock); |
| 1929 | kref_init(&subsys->ref); |
| 1930 | INIT_LIST_HEAD(&subsys->ctrls); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 1931 | INIT_LIST_HEAD(&subsys->nsheads); |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 1932 | nvme_init_subnqn(subsys, ctrl, id); |
| 1933 | memcpy(subsys->serial, id->sn, sizeof(subsys->serial)); |
| 1934 | memcpy(subsys->model, id->mn, sizeof(subsys->model)); |
| 1935 | memcpy(subsys->firmware_rev, id->fr, sizeof(subsys->firmware_rev)); |
| 1936 | subsys->vendor_id = le16_to_cpu(id->vid); |
| 1937 | subsys->cmic = id->cmic; |
| 1938 | |
| 1939 | subsys->dev.class = nvme_subsys_class; |
| 1940 | subsys->dev.release = nvme_release_subsystem; |
| 1941 | dev_set_name(&subsys->dev, "nvme-subsys%d", subsys->instance); |
| 1942 | device_initialize(&subsys->dev); |
| 1943 | |
| 1944 | mutex_lock(&nvme_subsystems_lock); |
| 1945 | found = __nvme_find_get_subsystem(subsys->subnqn); |
| 1946 | if (found) { |
| 1947 | /* |
| 1948 | * Verify that the subsystem actually supports multiple |
| 1949 | * controllers, else bail out. |
| 1950 | */ |
| 1951 | if (!(id->cmic & (1 << 1))) { |
| 1952 | dev_err(ctrl->device, |
| 1953 | "ignoring ctrl due to duplicate subnqn (%s).\n", |
| 1954 | found->subnqn); |
| 1955 | nvme_put_subsystem(found); |
| 1956 | ret = -EINVAL; |
| 1957 | goto out_unlock; |
| 1958 | } |
| 1959 | |
| 1960 | __nvme_release_subsystem(subsys); |
| 1961 | subsys = found; |
| 1962 | } else { |
| 1963 | ret = device_add(&subsys->dev); |
| 1964 | if (ret) { |
| 1965 | dev_err(ctrl->device, |
| 1966 | "failed to register subsystem device.\n"); |
| 1967 | goto out_unlock; |
| 1968 | } |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 1969 | ida_init(&subsys->ns_ida); |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 1970 | list_add_tail(&subsys->entry, &nvme_subsystems); |
| 1971 | } |
| 1972 | |
| 1973 | ctrl->subsys = subsys; |
| 1974 | mutex_unlock(&nvme_subsystems_lock); |
| 1975 | |
| 1976 | if (sysfs_create_link(&subsys->dev.kobj, &ctrl->device->kobj, |
| 1977 | dev_name(ctrl->device))) { |
| 1978 | dev_err(ctrl->device, |
| 1979 | "failed to create sysfs link from subsystem.\n"); |
| 1980 | /* the transport driver will eventually put the subsystem */ |
| 1981 | return -EINVAL; |
| 1982 | } |
| 1983 | |
| 1984 | mutex_lock(&subsys->lock); |
| 1985 | list_add_tail(&ctrl->subsys_entry, &subsys->ctrls); |
| 1986 | mutex_unlock(&subsys->lock); |
| 1987 | |
| 1988 | return 0; |
| 1989 | |
| 1990 | out_unlock: |
| 1991 | mutex_unlock(&nvme_subsystems_lock); |
| 1992 | put_device(&subsys->dev); |
| 1993 | return ret; |
Christoph Hellwig | 180de007 | 2017-06-26 12:39:02 +0200 | [diff] [blame] | 1994 | } |
| 1995 | |
Keith Busch | c627c48 | 2017-11-07 10:28:31 -0700 | [diff] [blame] | 1996 | static int nvme_get_log(struct nvme_ctrl *ctrl, u8 log_page, void *log, |
| 1997 | size_t size) |
| 1998 | { |
| 1999 | struct nvme_command c = { }; |
| 2000 | |
| 2001 | c.common.opcode = nvme_admin_get_log_page; |
| 2002 | c.common.nsid = cpu_to_le32(NVME_NSID_ALL); |
| 2003 | c.common.cdw10[0] = nvme_get_log_dw10(log_page, size); |
| 2004 | |
| 2005 | return nvme_submit_sync_cmd(ctrl->admin_q, &c, log, size); |
| 2006 | } |
| 2007 | |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 2008 | static int nvme_get_effects_log(struct nvme_ctrl *ctrl) |
| 2009 | { |
| 2010 | int ret; |
| 2011 | |
| 2012 | if (!ctrl->effects) |
| 2013 | ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL); |
| 2014 | |
| 2015 | if (!ctrl->effects) |
| 2016 | return 0; |
| 2017 | |
| 2018 | ret = nvme_get_log(ctrl, NVME_LOG_CMD_EFFECTS, ctrl->effects, |
| 2019 | sizeof(*ctrl->effects)); |
| 2020 | if (ret) { |
| 2021 | kfree(ctrl->effects); |
| 2022 | ctrl->effects = NULL; |
| 2023 | } |
| 2024 | return ret; |
| 2025 | } |
| 2026 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2027 | /* |
| 2028 | * Initialize the cached copies of the Identify data and various controller |
| 2029 | * register in our nvme_ctrl structure. This should be called as soon as |
| 2030 | * the admin queue is fully up and running. |
| 2031 | */ |
| 2032 | int nvme_init_identify(struct nvme_ctrl *ctrl) |
| 2033 | { |
| 2034 | struct nvme_id_ctrl *id; |
| 2035 | u64 cap; |
| 2036 | int ret, page_shift; |
Christoph Hellwig | a229dbf | 2016-06-06 23:20:48 +0200 | [diff] [blame] | 2037 | u32 max_hw_sectors; |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 2038 | bool prev_apst_enabled; |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2039 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2040 | ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs); |
| 2041 | if (ret) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 2042 | dev_err(ctrl->device, "Reading VS failed (%d)\n", ret); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2043 | return ret; |
| 2044 | } |
| 2045 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2046 | ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap); |
| 2047 | if (ret) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 2048 | dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2049 | return ret; |
| 2050 | } |
| 2051 | page_shift = NVME_CAP_MPSMIN(cap) + 12; |
| 2052 | |
Gabriel Krisman Bertazi | 8ef2074 | 2016-10-19 09:51:05 -0600 | [diff] [blame] | 2053 | if (ctrl->vs >= NVME_VS(1, 1, 0)) |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2054 | ctrl->subsystem = NVME_CAP_NSSRC(cap); |
| 2055 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2056 | ret = nvme_identify_ctrl(ctrl, &id); |
| 2057 | if (ret) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 2058 | dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2059 | return -EIO; |
| 2060 | } |
| 2061 | |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 2062 | if (id->lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG) { |
| 2063 | ret = nvme_get_effects_log(ctrl); |
| 2064 | if (ret < 0) |
| 2065 | return ret; |
| 2066 | } |
| 2067 | |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 2068 | if (!ctrl->identified) { |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2069 | int i; |
| 2070 | |
| 2071 | ret = nvme_init_subsystem(ctrl, id); |
| 2072 | if (ret) |
| 2073 | goto out_free; |
| 2074 | |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 2075 | /* |
| 2076 | * Check for quirks. Quirk can depend on firmware version, |
| 2077 | * so, in principle, the set of quirks present can change |
| 2078 | * across a reset. As a possible future enhancement, we |
| 2079 | * could re-scan for quirks every time we reinitialize |
| 2080 | * the device, but we'd have to make sure that the driver |
| 2081 | * behaves intelligently if the quirks change. |
| 2082 | */ |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 2083 | for (i = 0; i < ARRAY_SIZE(core_quirks); i++) { |
| 2084 | if (quirk_matches(id, &core_quirks[i])) |
| 2085 | ctrl->quirks |= core_quirks[i].quirks; |
| 2086 | } |
| 2087 | } |
| 2088 | |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 2089 | if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) { |
Johannes Thumshirn | f0425db | 2017-06-09 16:17:21 +0200 | [diff] [blame] | 2090 | dev_warn(ctrl->device, "forcibly allowing all power states due to nvme_core.force_apst -- use at your own risk\n"); |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 2091 | ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS; |
| 2092 | } |
| 2093 | |
Scott Bauer | 8a9ae52 | 2017-02-17 13:59:40 +0100 | [diff] [blame] | 2094 | ctrl->oacs = le16_to_cpu(id->oacs); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2095 | ctrl->oncs = le16_to_cpup(&id->oncs); |
Christoph Hellwig | 6bf25d1 | 2015-11-20 09:36:44 +0100 | [diff] [blame] | 2096 | atomic_set(&ctrl->abort_limit, id->acl + 1); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2097 | ctrl->vwc = id->vwc; |
Ming Lin | 931e1c2 | 2016-02-26 13:24:19 -0800 | [diff] [blame] | 2098 | ctrl->cntlid = le16_to_cpup(&id->cntlid); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2099 | if (id->mdts) |
Christoph Hellwig | a229dbf | 2016-06-06 23:20:48 +0200 | [diff] [blame] | 2100 | max_hw_sectors = 1 << (id->mdts + page_shift - 9); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2101 | else |
Christoph Hellwig | a229dbf | 2016-06-06 23:20:48 +0200 | [diff] [blame] | 2102 | max_hw_sectors = UINT_MAX; |
| 2103 | ctrl->max_hw_sectors = |
| 2104 | min_not_zero(ctrl->max_hw_sectors, max_hw_sectors); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2105 | |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 2106 | nvme_set_queue_limits(ctrl, ctrl->admin_q); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 2107 | ctrl->sgls = le32_to_cpu(id->sgls); |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 2108 | ctrl->kas = le16_to_cpu(id->kas); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 2109 | |
Martin K. Petersen | 07fbd32 | 2017-08-25 19:14:50 -0400 | [diff] [blame] | 2110 | if (id->rtd3e) { |
| 2111 | /* us -> s */ |
| 2112 | u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000; |
| 2113 | |
| 2114 | ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time, |
| 2115 | shutdown_timeout, 60); |
| 2116 | |
| 2117 | if (ctrl->shutdown_timeout != shutdown_timeout) |
| 2118 | dev_warn(ctrl->device, |
| 2119 | "Shutdown timeout set to %u seconds\n", |
| 2120 | ctrl->shutdown_timeout); |
| 2121 | } else |
| 2122 | ctrl->shutdown_timeout = shutdown_timeout; |
| 2123 | |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 2124 | ctrl->npss = id->npss; |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 2125 | ctrl->apsta = id->apsta; |
| 2126 | prev_apst_enabled = ctrl->apst_enabled; |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 2127 | if (ctrl->quirks & NVME_QUIRK_NO_APST) { |
| 2128 | if (force_apst && id->apsta) { |
Johannes Thumshirn | f0425db | 2017-06-09 16:17:21 +0200 | [diff] [blame] | 2129 | dev_warn(ctrl->device, "forcibly allowing APST due to nvme_core.force_apst -- use at your own risk\n"); |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 2130 | ctrl->apst_enabled = true; |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 2131 | } else { |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 2132 | ctrl->apst_enabled = false; |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 2133 | } |
| 2134 | } else { |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 2135 | ctrl->apst_enabled = id->apsta; |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 2136 | } |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 2137 | memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd)); |
| 2138 | |
Christoph Hellwig | d3d5b87 | 2017-05-20 15:14:44 +0200 | [diff] [blame] | 2139 | if (ctrl->ops->flags & NVME_F_FABRICS) { |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 2140 | ctrl->icdoff = le16_to_cpu(id->icdoff); |
| 2141 | ctrl->ioccsz = le32_to_cpu(id->ioccsz); |
| 2142 | ctrl->iorcsz = le32_to_cpu(id->iorcsz); |
| 2143 | ctrl->maxcmd = le16_to_cpu(id->maxcmd); |
| 2144 | |
| 2145 | /* |
| 2146 | * In fabrics we need to verify the cntlid matches the |
| 2147 | * admin connect |
| 2148 | */ |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 2149 | if (ctrl->cntlid != le16_to_cpu(id->cntlid)) { |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 2150 | ret = -EINVAL; |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 2151 | goto out_free; |
| 2152 | } |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 2153 | |
| 2154 | if (!ctrl->opts->discovery_nqn && !ctrl->kas) { |
Johannes Thumshirn | f0425db | 2017-06-09 16:17:21 +0200 | [diff] [blame] | 2155 | dev_err(ctrl->device, |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 2156 | "keep-alive support is mandatory for fabrics\n"); |
| 2157 | ret = -EINVAL; |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 2158 | goto out_free; |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 2159 | } |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 2160 | } else { |
| 2161 | ctrl->cntlid = le16_to_cpu(id->cntlid); |
Christoph Hellwig | fe6d53c | 2017-05-12 17:16:10 +0200 | [diff] [blame] | 2162 | ctrl->hmpre = le32_to_cpu(id->hmpre); |
| 2163 | ctrl->hmmin = le32_to_cpu(id->hmmin); |
Christoph Hellwig | 044a9df | 2017-09-11 12:09:28 -0400 | [diff] [blame] | 2164 | ctrl->hmminds = le32_to_cpu(id->hmminds); |
| 2165 | ctrl->hmmaxd = le16_to_cpu(id->hmmaxd); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 2166 | } |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 2167 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2168 | kfree(id); |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 2169 | |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 2170 | if (ctrl->apst_enabled && !prev_apst_enabled) |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 2171 | dev_pm_qos_expose_latency_tolerance(ctrl->device); |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 2172 | else if (!ctrl->apst_enabled && prev_apst_enabled) |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 2173 | dev_pm_qos_hide_latency_tolerance(ctrl->device); |
| 2174 | |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 2175 | ret = nvme_configure_apst(ctrl); |
| 2176 | if (ret < 0) |
| 2177 | return ret; |
Jon Derrick | dbf86b3 | 2017-08-16 09:51:29 +0200 | [diff] [blame] | 2178 | |
| 2179 | ret = nvme_configure_timestamp(ctrl); |
| 2180 | if (ret < 0) |
| 2181 | return ret; |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 2182 | |
| 2183 | ret = nvme_configure_directives(ctrl); |
| 2184 | if (ret < 0) |
| 2185 | return ret; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 2186 | |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 2187 | ctrl->identified = true; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 2188 | |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 2189 | return 0; |
| 2190 | |
| 2191 | out_free: |
| 2192 | kfree(id); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 2193 | return ret; |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2194 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 2195 | EXPORT_SYMBOL_GPL(nvme_init_identify); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2196 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2197 | static int nvme_dev_open(struct inode *inode, struct file *file) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 2198 | { |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 2199 | struct nvme_ctrl *ctrl = |
| 2200 | container_of(inode->i_cdev, struct nvme_ctrl, cdev); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 2201 | |
Christoph Hellwig | 999ada2 | 2017-10-18 17:09:31 +0200 | [diff] [blame] | 2202 | if (ctrl->state != NVME_CTRL_LIVE) |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 2203 | return -EWOULDBLOCK; |
| 2204 | file->private_data = ctrl; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2205 | return 0; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 2206 | } |
| 2207 | |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 2208 | static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp) |
| 2209 | { |
| 2210 | struct nvme_ns *ns; |
| 2211 | int ret; |
| 2212 | |
| 2213 | mutex_lock(&ctrl->namespaces_mutex); |
| 2214 | if (list_empty(&ctrl->namespaces)) { |
| 2215 | ret = -ENOTTY; |
| 2216 | goto out_unlock; |
| 2217 | } |
| 2218 | |
| 2219 | ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list); |
| 2220 | if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 2221 | dev_warn(ctrl->device, |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 2222 | "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n"); |
| 2223 | ret = -EINVAL; |
| 2224 | goto out_unlock; |
| 2225 | } |
| 2226 | |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 2227 | dev_warn(ctrl->device, |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 2228 | "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n"); |
| 2229 | kref_get(&ns->kref); |
| 2230 | mutex_unlock(&ctrl->namespaces_mutex); |
| 2231 | |
| 2232 | ret = nvme_user_cmd(ctrl, ns, argp); |
| 2233 | nvme_put_ns(ns); |
| 2234 | return ret; |
| 2235 | |
| 2236 | out_unlock: |
| 2237 | mutex_unlock(&ctrl->namespaces_mutex); |
| 2238 | return ret; |
| 2239 | } |
| 2240 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2241 | static long nvme_dev_ioctl(struct file *file, unsigned int cmd, |
| 2242 | unsigned long arg) |
| 2243 | { |
| 2244 | struct nvme_ctrl *ctrl = file->private_data; |
| 2245 | void __user *argp = (void __user *)arg; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2246 | |
| 2247 | switch (cmd) { |
| 2248 | case NVME_IOCTL_ADMIN_CMD: |
| 2249 | return nvme_user_cmd(ctrl, NULL, argp); |
| 2250 | case NVME_IOCTL_IO_CMD: |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 2251 | return nvme_dev_user_cmd(ctrl, argp); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2252 | case NVME_IOCTL_RESET: |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 2253 | dev_warn(ctrl->device, "resetting controller\n"); |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 2254 | return nvme_reset_ctrl_sync(ctrl); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2255 | case NVME_IOCTL_SUBSYS_RESET: |
| 2256 | return nvme_reset_subsystem(ctrl); |
Keith Busch | 9ec3bb2 | 2016-04-29 15:45:18 -0600 | [diff] [blame] | 2257 | case NVME_IOCTL_RESCAN: |
| 2258 | nvme_queue_scan(ctrl); |
| 2259 | return 0; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2260 | default: |
| 2261 | return -ENOTTY; |
| 2262 | } |
| 2263 | } |
| 2264 | |
| 2265 | static const struct file_operations nvme_dev_fops = { |
| 2266 | .owner = THIS_MODULE, |
| 2267 | .open = nvme_dev_open, |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2268 | .unlocked_ioctl = nvme_dev_ioctl, |
| 2269 | .compat_ioctl = nvme_dev_ioctl, |
| 2270 | }; |
| 2271 | |
| 2272 | static ssize_t nvme_sysfs_reset(struct device *dev, |
| 2273 | struct device_attribute *attr, const char *buf, |
| 2274 | size_t count) |
| 2275 | { |
| 2276 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2277 | int ret; |
| 2278 | |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 2279 | ret = nvme_reset_ctrl_sync(ctrl); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2280 | if (ret < 0) |
| 2281 | return ret; |
| 2282 | return count; |
| 2283 | } |
| 2284 | static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset); |
| 2285 | |
Keith Busch | 9ec3bb2 | 2016-04-29 15:45:18 -0600 | [diff] [blame] | 2286 | static ssize_t nvme_sysfs_rescan(struct device *dev, |
| 2287 | struct device_attribute *attr, const char *buf, |
| 2288 | size_t count) |
| 2289 | { |
| 2290 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2291 | |
| 2292 | nvme_queue_scan(ctrl); |
| 2293 | return count; |
| 2294 | } |
| 2295 | static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan); |
| 2296 | |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2297 | static ssize_t wwid_show(struct device *dev, struct device_attribute *attr, |
| 2298 | char *buf) |
| 2299 | { |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 2300 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2301 | struct nvme_ns_ids *ids = &ns->head->ids; |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2302 | struct nvme_subsystem *subsys = ns->ctrl->subsys; |
| 2303 | int serial_len = sizeof(subsys->serial); |
| 2304 | int model_len = sizeof(subsys->model); |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2305 | |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2306 | if (!uuid_is_null(&ids->uuid)) |
| 2307 | return sprintf(buf, "uuid.%pU\n", &ids->uuid); |
Johannes Thumshirn | 6484f5d | 2017-07-12 15:38:56 +0200 | [diff] [blame] | 2308 | |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2309 | if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid))) |
| 2310 | return sprintf(buf, "eui.%16phN\n", ids->nguid); |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2311 | |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2312 | if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64))) |
| 2313 | return sprintf(buf, "eui.%8phN\n", ids->eui64); |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2314 | |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2315 | while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' || |
| 2316 | subsys->serial[serial_len - 1] == '\0')) |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2317 | serial_len--; |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2318 | while (model_len > 0 && (subsys->model[model_len - 1] == ' ' || |
| 2319 | subsys->model[model_len - 1] == '\0')) |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2320 | model_len--; |
| 2321 | |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2322 | return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id, |
| 2323 | serial_len, subsys->serial, model_len, subsys->model, |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2324 | ns->head->ns_id); |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2325 | } |
| 2326 | static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL); |
| 2327 | |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2328 | static ssize_t nguid_show(struct device *dev, struct device_attribute *attr, |
| 2329 | char *buf) |
| 2330 | { |
| 2331 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2332 | return sprintf(buf, "%pU\n", ns->head->ids.nguid); |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2333 | } |
| 2334 | static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL); |
| 2335 | |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2336 | static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, |
| 2337 | char *buf) |
| 2338 | { |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 2339 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2340 | struct nvme_ns_ids *ids = &ns->head->ids; |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2341 | |
| 2342 | /* For backward compatibility expose the NGUID to userspace if |
| 2343 | * we have no UUID set |
| 2344 | */ |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2345 | if (uuid_is_null(&ids->uuid)) { |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2346 | printk_ratelimited(KERN_WARNING |
| 2347 | "No UUID available providing old NGUID\n"); |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2348 | return sprintf(buf, "%pU\n", ids->nguid); |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2349 | } |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2350 | return sprintf(buf, "%pU\n", &ids->uuid); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2351 | } |
| 2352 | static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL); |
| 2353 | |
| 2354 | static ssize_t eui_show(struct device *dev, struct device_attribute *attr, |
| 2355 | char *buf) |
| 2356 | { |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 2357 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2358 | return sprintf(buf, "%8ph\n", ns->head->ids.eui64); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2359 | } |
| 2360 | static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL); |
| 2361 | |
| 2362 | static ssize_t nsid_show(struct device *dev, struct device_attribute *attr, |
| 2363 | char *buf) |
| 2364 | { |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 2365 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2366 | return sprintf(buf, "%d\n", ns->head->ns_id); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2367 | } |
| 2368 | static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL); |
| 2369 | |
| 2370 | static struct attribute *nvme_ns_attrs[] = { |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2371 | &dev_attr_wwid.attr, |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2372 | &dev_attr_uuid.attr, |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2373 | &dev_attr_nguid.attr, |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2374 | &dev_attr_eui.attr, |
| 2375 | &dev_attr_nsid.attr, |
| 2376 | NULL, |
| 2377 | }; |
| 2378 | |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2379 | static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj, |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2380 | struct attribute *a, int n) |
| 2381 | { |
| 2382 | struct device *dev = container_of(kobj, struct device, kobj); |
Simon A. F. Lund | 40267ef | 2016-09-16 14:25:08 +0200 | [diff] [blame] | 2383 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2384 | struct nvme_ns_ids *ids = &ns->head->ids; |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2385 | |
| 2386 | if (a == &dev_attr_uuid.attr) { |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2387 | if (uuid_is_null(&ids->uuid) || |
| 2388 | !memchr_inv(ids->nguid, 0, sizeof(ids->nguid))) |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2389 | return 0; |
| 2390 | } |
| 2391 | if (a == &dev_attr_nguid.attr) { |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2392 | if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid))) |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2393 | return 0; |
| 2394 | } |
| 2395 | if (a == &dev_attr_eui.attr) { |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2396 | if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64))) |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2397 | return 0; |
| 2398 | } |
| 2399 | return a->mode; |
| 2400 | } |
| 2401 | |
| 2402 | static const struct attribute_group nvme_ns_attr_group = { |
| 2403 | .attrs = nvme_ns_attrs, |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2404 | .is_visible = nvme_ns_attrs_are_visible, |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2405 | }; |
| 2406 | |
Ming Lin | 931e1c2 | 2016-02-26 13:24:19 -0800 | [diff] [blame] | 2407 | #define nvme_show_str_function(field) \ |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2408 | static ssize_t field##_show(struct device *dev, \ |
| 2409 | struct device_attribute *attr, char *buf) \ |
| 2410 | { \ |
| 2411 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2412 | return sprintf(buf, "%.*s\n", \ |
| 2413 | (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \ |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2414 | } \ |
| 2415 | static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); |
| 2416 | |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2417 | nvme_show_str_function(model); |
| 2418 | nvme_show_str_function(serial); |
| 2419 | nvme_show_str_function(firmware_rev); |
| 2420 | |
Ming Lin | 931e1c2 | 2016-02-26 13:24:19 -0800 | [diff] [blame] | 2421 | #define nvme_show_int_function(field) \ |
| 2422 | static ssize_t field##_show(struct device *dev, \ |
| 2423 | struct device_attribute *attr, char *buf) \ |
| 2424 | { \ |
| 2425 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ |
| 2426 | return sprintf(buf, "%d\n", ctrl->field); \ |
| 2427 | } \ |
| 2428 | static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); |
| 2429 | |
Ming Lin | 931e1c2 | 2016-02-26 13:24:19 -0800 | [diff] [blame] | 2430 | nvme_show_int_function(cntlid); |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2431 | |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2432 | static ssize_t nvme_sysfs_delete(struct device *dev, |
| 2433 | struct device_attribute *attr, const char *buf, |
| 2434 | size_t count) |
| 2435 | { |
| 2436 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2437 | |
| 2438 | if (device_remove_file_self(dev, attr)) |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 2439 | nvme_delete_ctrl_sync(ctrl); |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2440 | return count; |
| 2441 | } |
| 2442 | static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete); |
| 2443 | |
| 2444 | static ssize_t nvme_sysfs_show_transport(struct device *dev, |
| 2445 | struct device_attribute *attr, |
| 2446 | char *buf) |
| 2447 | { |
| 2448 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2449 | |
| 2450 | return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name); |
| 2451 | } |
| 2452 | static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL); |
| 2453 | |
Sagi Grimberg | 8432bdb2 | 2016-11-28 01:47:40 +0200 | [diff] [blame] | 2454 | static ssize_t nvme_sysfs_show_state(struct device *dev, |
| 2455 | struct device_attribute *attr, |
| 2456 | char *buf) |
| 2457 | { |
| 2458 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2459 | static const char *const state_name[] = { |
| 2460 | [NVME_CTRL_NEW] = "new", |
| 2461 | [NVME_CTRL_LIVE] = "live", |
| 2462 | [NVME_CTRL_RESETTING] = "resetting", |
| 2463 | [NVME_CTRL_RECONNECTING]= "reconnecting", |
| 2464 | [NVME_CTRL_DELETING] = "deleting", |
| 2465 | [NVME_CTRL_DEAD] = "dead", |
| 2466 | }; |
| 2467 | |
| 2468 | if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) && |
| 2469 | state_name[ctrl->state]) |
| 2470 | return sprintf(buf, "%s\n", state_name[ctrl->state]); |
| 2471 | |
| 2472 | return sprintf(buf, "unknown state\n"); |
| 2473 | } |
| 2474 | |
| 2475 | static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL); |
| 2476 | |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2477 | static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev, |
| 2478 | struct device_attribute *attr, |
| 2479 | char *buf) |
| 2480 | { |
| 2481 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2482 | |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2483 | return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subsys->subnqn); |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2484 | } |
| 2485 | static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL); |
| 2486 | |
| 2487 | static ssize_t nvme_sysfs_show_address(struct device *dev, |
| 2488 | struct device_attribute *attr, |
| 2489 | char *buf) |
| 2490 | { |
| 2491 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2492 | |
| 2493 | return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE); |
| 2494 | } |
| 2495 | static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL); |
| 2496 | |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2497 | static struct attribute *nvme_dev_attrs[] = { |
| 2498 | &dev_attr_reset_controller.attr, |
Keith Busch | 9ec3bb2 | 2016-04-29 15:45:18 -0600 | [diff] [blame] | 2499 | &dev_attr_rescan_controller.attr, |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2500 | &dev_attr_model.attr, |
| 2501 | &dev_attr_serial.attr, |
| 2502 | &dev_attr_firmware_rev.attr, |
Ming Lin | 931e1c2 | 2016-02-26 13:24:19 -0800 | [diff] [blame] | 2503 | &dev_attr_cntlid.attr, |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2504 | &dev_attr_delete_controller.attr, |
| 2505 | &dev_attr_transport.attr, |
| 2506 | &dev_attr_subsysnqn.attr, |
| 2507 | &dev_attr_address.attr, |
Sagi Grimberg | 8432bdb2 | 2016-11-28 01:47:40 +0200 | [diff] [blame] | 2508 | &dev_attr_state.attr, |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2509 | NULL |
| 2510 | }; |
| 2511 | |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2512 | static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj, |
| 2513 | struct attribute *a, int n) |
| 2514 | { |
| 2515 | struct device *dev = container_of(kobj, struct device, kobj); |
| 2516 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2517 | |
Christoph Hellwig | 49d3d50 | 2017-06-26 12:39:03 +0200 | [diff] [blame] | 2518 | if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl) |
| 2519 | return 0; |
| 2520 | if (a == &dev_attr_address.attr && !ctrl->ops->get_address) |
| 2521 | return 0; |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2522 | |
| 2523 | return a->mode; |
| 2524 | } |
| 2525 | |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2526 | static struct attribute_group nvme_dev_attrs_group = { |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2527 | .attrs = nvme_dev_attrs, |
| 2528 | .is_visible = nvme_dev_attrs_are_visible, |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2529 | }; |
| 2530 | |
| 2531 | static const struct attribute_group *nvme_dev_attr_groups[] = { |
| 2532 | &nvme_dev_attrs_group, |
| 2533 | NULL, |
| 2534 | }; |
| 2535 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2536 | static struct nvme_ns_head *__nvme_find_ns_head(struct nvme_subsystem *subsys, |
| 2537 | unsigned nsid) |
| 2538 | { |
| 2539 | struct nvme_ns_head *h; |
| 2540 | |
| 2541 | lockdep_assert_held(&subsys->lock); |
| 2542 | |
| 2543 | list_for_each_entry(h, &subsys->nsheads, entry) { |
| 2544 | if (h->ns_id == nsid && kref_get_unless_zero(&h->ref)) |
| 2545 | return h; |
| 2546 | } |
| 2547 | |
| 2548 | return NULL; |
| 2549 | } |
| 2550 | |
| 2551 | static int __nvme_check_ids(struct nvme_subsystem *subsys, |
| 2552 | struct nvme_ns_head *new) |
| 2553 | { |
| 2554 | struct nvme_ns_head *h; |
| 2555 | |
| 2556 | lockdep_assert_held(&subsys->lock); |
| 2557 | |
| 2558 | list_for_each_entry(h, &subsys->nsheads, entry) { |
| 2559 | if (nvme_ns_ids_valid(&new->ids) && |
| 2560 | nvme_ns_ids_equal(&new->ids, &h->ids)) |
| 2561 | return -EINVAL; |
| 2562 | } |
| 2563 | |
| 2564 | return 0; |
| 2565 | } |
| 2566 | |
| 2567 | static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl, |
| 2568 | unsigned nsid, struct nvme_id_ns *id) |
| 2569 | { |
| 2570 | struct nvme_ns_head *head; |
| 2571 | int ret = -ENOMEM; |
| 2572 | |
| 2573 | head = kzalloc(sizeof(*head), GFP_KERNEL); |
| 2574 | if (!head) |
| 2575 | goto out; |
| 2576 | ret = ida_simple_get(&ctrl->subsys->ns_ida, 1, 0, GFP_KERNEL); |
| 2577 | if (ret < 0) |
| 2578 | goto out_free_head; |
| 2579 | head->instance = ret; |
| 2580 | INIT_LIST_HEAD(&head->list); |
| 2581 | init_srcu_struct(&head->srcu); |
| 2582 | head->subsys = ctrl->subsys; |
| 2583 | head->ns_id = nsid; |
| 2584 | kref_init(&head->ref); |
| 2585 | |
| 2586 | nvme_report_ns_ids(ctrl, nsid, id, &head->ids); |
| 2587 | |
| 2588 | ret = __nvme_check_ids(ctrl->subsys, head); |
| 2589 | if (ret) { |
| 2590 | dev_err(ctrl->device, |
| 2591 | "duplicate IDs for nsid %d\n", nsid); |
| 2592 | goto out_cleanup_srcu; |
| 2593 | } |
| 2594 | |
| 2595 | list_add_tail(&head->entry, &ctrl->subsys->nsheads); |
| 2596 | return head; |
| 2597 | out_cleanup_srcu: |
| 2598 | cleanup_srcu_struct(&head->srcu); |
| 2599 | ida_simple_remove(&ctrl->subsys->ns_ida, head->instance); |
| 2600 | out_free_head: |
| 2601 | kfree(head); |
| 2602 | out: |
| 2603 | return ERR_PTR(ret); |
| 2604 | } |
| 2605 | |
| 2606 | static int nvme_init_ns_head(struct nvme_ns *ns, unsigned nsid, |
| 2607 | struct nvme_id_ns *id, bool *new) |
| 2608 | { |
| 2609 | struct nvme_ctrl *ctrl = ns->ctrl; |
| 2610 | bool is_shared = id->nmic & (1 << 0); |
| 2611 | struct nvme_ns_head *head = NULL; |
| 2612 | int ret = 0; |
| 2613 | |
| 2614 | mutex_lock(&ctrl->subsys->lock); |
| 2615 | if (is_shared) |
| 2616 | head = __nvme_find_ns_head(ctrl->subsys, nsid); |
| 2617 | if (!head) { |
| 2618 | head = nvme_alloc_ns_head(ctrl, nsid, id); |
| 2619 | if (IS_ERR(head)) { |
| 2620 | ret = PTR_ERR(head); |
| 2621 | goto out_unlock; |
| 2622 | } |
| 2623 | |
| 2624 | *new = true; |
| 2625 | } else { |
| 2626 | struct nvme_ns_ids ids; |
| 2627 | |
| 2628 | nvme_report_ns_ids(ctrl, nsid, id, &ids); |
| 2629 | if (!nvme_ns_ids_equal(&head->ids, &ids)) { |
| 2630 | dev_err(ctrl->device, |
| 2631 | "IDs don't match for shared namespace %d\n", |
| 2632 | nsid); |
| 2633 | ret = -EINVAL; |
| 2634 | goto out_unlock; |
| 2635 | } |
| 2636 | |
| 2637 | *new = false; |
| 2638 | } |
| 2639 | |
| 2640 | list_add_tail(&ns->siblings, &head->list); |
| 2641 | ns->head = head; |
| 2642 | |
| 2643 | out_unlock: |
| 2644 | mutex_unlock(&ctrl->subsys->lock); |
| 2645 | return ret; |
| 2646 | } |
| 2647 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2648 | static int ns_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 2649 | { |
| 2650 | struct nvme_ns *nsa = container_of(a, struct nvme_ns, list); |
| 2651 | struct nvme_ns *nsb = container_of(b, struct nvme_ns, list); |
| 2652 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2653 | return nsa->head->ns_id - nsb->head->ns_id; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2654 | } |
| 2655 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2656 | 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] | 2657 | { |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2658 | struct nvme_ns *ns, *ret = NULL; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2659 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2660 | mutex_lock(&ctrl->namespaces_mutex); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2661 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2662 | if (ns->head->ns_id == nsid) { |
Christoph Hellwig | 2dd4122 | 2017-10-18 13:20:01 +0200 | [diff] [blame] | 2663 | if (!kref_get_unless_zero(&ns->kref)) |
| 2664 | continue; |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2665 | ret = ns; |
| 2666 | break; |
| 2667 | } |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2668 | if (ns->head->ns_id > nsid) |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2669 | break; |
| 2670 | } |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2671 | mutex_unlock(&ctrl->namespaces_mutex); |
| 2672 | return ret; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2673 | } |
| 2674 | |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 2675 | static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns) |
| 2676 | { |
| 2677 | struct streams_directive_params s; |
| 2678 | int ret; |
| 2679 | |
| 2680 | if (!ctrl->nr_streams) |
| 2681 | return 0; |
| 2682 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2683 | ret = nvme_get_stream_params(ctrl, &s, ns->head->ns_id); |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 2684 | if (ret) |
| 2685 | return ret; |
| 2686 | |
| 2687 | ns->sws = le32_to_cpu(s.sws); |
| 2688 | ns->sgs = le16_to_cpu(s.sgs); |
| 2689 | |
| 2690 | if (ns->sws) { |
| 2691 | unsigned int bs = 1 << ns->lba_shift; |
| 2692 | |
| 2693 | blk_queue_io_min(ns->queue, bs * ns->sws); |
| 2694 | if (ns->sgs) |
| 2695 | blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs); |
| 2696 | } |
| 2697 | |
| 2698 | return 0; |
| 2699 | } |
| 2700 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2701 | static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid) |
| 2702 | { |
| 2703 | struct nvme_ns *ns; |
| 2704 | struct gendisk *disk; |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 2705 | struct nvme_id_ns *id; |
| 2706 | char disk_name[DISK_NAME_LEN]; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2707 | int node = dev_to_node(ctrl->dev); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2708 | bool new = true; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2709 | |
| 2710 | ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node); |
| 2711 | if (!ns) |
| 2712 | return; |
| 2713 | |
| 2714 | ns->queue = blk_mq_init_queue(ctrl->tagset); |
| 2715 | if (IS_ERR(ns->queue)) |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2716 | goto out_free_ns; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2717 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue); |
| 2718 | ns->queue->queuedata = ns; |
| 2719 | ns->ctrl = ctrl; |
| 2720 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2721 | kref_init(&ns->kref); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2722 | 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] | 2723 | |
| 2724 | blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift); |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 2725 | nvme_set_queue_limits(ctrl, ns->queue); |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 2726 | nvme_setup_streams_ns(ctrl, ns); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2727 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 2728 | id = nvme_identify_ns(ctrl, nsid); |
| 2729 | if (!id) |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 2730 | goto out_free_queue; |
| 2731 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 2732 | if (id->ncap == 0) |
| 2733 | goto out_free_id; |
| 2734 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2735 | if (nvme_init_ns_head(ns, nsid, id, &new)) |
| 2736 | goto out_free_id; |
| 2737 | |
| 2738 | sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance); |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 2739 | |
Christoph Hellwig | 608cc4b | 2017-09-06 11:45:24 +0200 | [diff] [blame] | 2740 | if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) { |
| 2741 | if (nvme_nvm_register(ns, disk_name, node)) { |
| 2742 | dev_warn(ctrl->device, "LightNVM init failure\n"); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2743 | goto out_unlink_ns; |
Christoph Hellwig | 608cc4b | 2017-09-06 11:45:24 +0200 | [diff] [blame] | 2744 | } |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 2745 | } |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2746 | |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 2747 | disk = alloc_disk_node(0, node); |
| 2748 | if (!disk) |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2749 | goto out_unlink_ns; |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 2750 | |
| 2751 | disk->fops = &nvme_fops; |
| 2752 | disk->private_data = ns; |
| 2753 | disk->queue = ns->queue; |
| 2754 | disk->flags = GENHD_FL_EXT_DEVT; |
| 2755 | memcpy(disk->disk_name, disk_name, DISK_NAME_LEN); |
| 2756 | ns->disk = disk; |
| 2757 | |
| 2758 | __nvme_revalidate_disk(disk, id); |
| 2759 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2760 | mutex_lock(&ctrl->namespaces_mutex); |
| 2761 | list_add_tail(&ns->list, &ctrl->namespaces); |
| 2762 | mutex_unlock(&ctrl->namespaces_mutex); |
| 2763 | |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 2764 | nvme_get_ctrl(ctrl); |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 2765 | |
| 2766 | kfree(id); |
| 2767 | |
Dan Williams | 0d52c756 | 2016-06-15 19:44:20 -0700 | [diff] [blame] | 2768 | device_add_disk(ctrl->device, ns->disk); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2769 | if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj, |
| 2770 | &nvme_ns_attr_group)) |
| 2771 | pr_warn("%s: failed to create sysfs group for identification\n", |
| 2772 | ns->disk->disk_name); |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 2773 | if (ns->ndev && nvme_nvm_register_sysfs(ns)) |
| 2774 | pr_warn("%s: failed to register lightnvm sysfs group for identification\n", |
| 2775 | ns->disk->disk_name); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2776 | return; |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2777 | out_unlink_ns: |
| 2778 | mutex_lock(&ctrl->subsys->lock); |
| 2779 | list_del_rcu(&ns->siblings); |
| 2780 | mutex_unlock(&ctrl->subsys->lock); |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 2781 | out_free_id: |
| 2782 | kfree(id); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2783 | out_free_queue: |
| 2784 | blk_cleanup_queue(ns->queue); |
| 2785 | out_free_ns: |
| 2786 | kfree(ns); |
| 2787 | } |
| 2788 | |
| 2789 | static void nvme_ns_remove(struct nvme_ns *ns) |
| 2790 | { |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2791 | struct nvme_ns_head *head = ns->head; |
| 2792 | |
Keith Busch | 646017a | 2016-02-24 09:15:54 -0700 | [diff] [blame] | 2793 | if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags)) |
| 2794 | return; |
| 2795 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 2796 | if (ns->disk && ns->disk->flags & GENHD_FL_UP) { |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2797 | if (blk_get_integrity(ns->disk)) |
| 2798 | blk_integrity_unregister(ns->disk); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2799 | sysfs_remove_group(&disk_to_dev(ns->disk)->kobj, |
| 2800 | &nvme_ns_attr_group); |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 2801 | if (ns->ndev) |
| 2802 | nvme_nvm_unregister_sysfs(ns); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2803 | del_gendisk(ns->disk); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2804 | blk_cleanup_queue(ns->queue); |
| 2805 | } |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2806 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2807 | mutex_lock(&ns->ctrl->subsys->lock); |
| 2808 | if (head) |
| 2809 | list_del_rcu(&ns->siblings); |
| 2810 | mutex_unlock(&ns->ctrl->subsys->lock); |
| 2811 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2812 | mutex_lock(&ns->ctrl->namespaces_mutex); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2813 | list_del_init(&ns->list); |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2814 | mutex_unlock(&ns->ctrl->namespaces_mutex); |
| 2815 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2816 | synchronize_srcu(&head->srcu); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2817 | nvme_put_ns(ns); |
| 2818 | } |
| 2819 | |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2820 | static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid) |
| 2821 | { |
| 2822 | struct nvme_ns *ns; |
| 2823 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2824 | ns = nvme_find_get_ns(ctrl, nsid); |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2825 | if (ns) { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 2826 | if (ns->disk && revalidate_disk(ns->disk)) |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2827 | nvme_ns_remove(ns); |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2828 | nvme_put_ns(ns); |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2829 | } else |
| 2830 | nvme_alloc_ns(ctrl, nsid); |
| 2831 | } |
| 2832 | |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 2833 | static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl, |
| 2834 | unsigned nsid) |
| 2835 | { |
| 2836 | struct nvme_ns *ns, *next; |
| 2837 | |
| 2838 | list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) { |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame^] | 2839 | if (ns->head->ns_id > nsid) |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 2840 | nvme_ns_remove(ns); |
| 2841 | } |
| 2842 | } |
| 2843 | |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2844 | static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn) |
| 2845 | { |
| 2846 | struct nvme_ns *ns; |
| 2847 | __le32 *ns_list; |
| 2848 | unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024); |
| 2849 | int ret = 0; |
| 2850 | |
| 2851 | ns_list = kzalloc(0x1000, GFP_KERNEL); |
| 2852 | if (!ns_list) |
| 2853 | return -ENOMEM; |
| 2854 | |
| 2855 | for (i = 0; i < num_lists; i++) { |
| 2856 | ret = nvme_identify_ns_list(ctrl, prev, ns_list); |
| 2857 | if (ret) |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 2858 | goto free; |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2859 | |
| 2860 | for (j = 0; j < min(nn, 1024U); j++) { |
| 2861 | nsid = le32_to_cpu(ns_list[j]); |
| 2862 | if (!nsid) |
| 2863 | goto out; |
| 2864 | |
| 2865 | nvme_validate_ns(ctrl, nsid); |
| 2866 | |
| 2867 | while (++prev < nsid) { |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2868 | ns = nvme_find_get_ns(ctrl, prev); |
| 2869 | if (ns) { |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2870 | nvme_ns_remove(ns); |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2871 | nvme_put_ns(ns); |
| 2872 | } |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2873 | } |
| 2874 | } |
| 2875 | nn -= j; |
| 2876 | } |
| 2877 | out: |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 2878 | nvme_remove_invalid_namespaces(ctrl, prev); |
| 2879 | free: |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2880 | kfree(ns_list); |
| 2881 | return ret; |
| 2882 | } |
| 2883 | |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 2884 | static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn) |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2885 | { |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2886 | unsigned i; |
| 2887 | |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2888 | for (i = 1; i <= nn; i++) |
| 2889 | nvme_validate_ns(ctrl, i); |
| 2890 | |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 2891 | nvme_remove_invalid_namespaces(ctrl, nn); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2892 | } |
| 2893 | |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 2894 | static void nvme_scan_work(struct work_struct *work) |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2895 | { |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 2896 | struct nvme_ctrl *ctrl = |
| 2897 | container_of(work, struct nvme_ctrl, scan_work); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2898 | struct nvme_id_ctrl *id; |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2899 | unsigned nn; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2900 | |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 2901 | if (ctrl->state != NVME_CTRL_LIVE) |
| 2902 | return; |
| 2903 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2904 | if (nvme_identify_ctrl(ctrl, &id)) |
| 2905 | return; |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2906 | |
| 2907 | nn = le32_to_cpu(id->nn); |
Gabriel Krisman Bertazi | 8ef2074 | 2016-10-19 09:51:05 -0600 | [diff] [blame] | 2908 | if (ctrl->vs >= NVME_VS(1, 1, 0) && |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2909 | !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) { |
| 2910 | if (!nvme_scan_ns_list(ctrl, nn)) |
| 2911 | goto done; |
| 2912 | } |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 2913 | nvme_scan_ns_sequential(ctrl, nn); |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2914 | done: |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2915 | mutex_lock(&ctrl->namespaces_mutex); |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 2916 | list_sort(NULL, &ctrl->namespaces, ns_cmp); |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 2917 | mutex_unlock(&ctrl->namespaces_mutex); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2918 | kfree(id); |
| 2919 | } |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 2920 | |
| 2921 | void nvme_queue_scan(struct nvme_ctrl *ctrl) |
| 2922 | { |
| 2923 | /* |
| 2924 | * Do not queue new scan work when a controller is reset during |
| 2925 | * removal. |
| 2926 | */ |
| 2927 | if (ctrl->state == NVME_CTRL_LIVE) |
Sagi Grimberg | c669ccd | 2017-05-04 13:33:14 +0300 | [diff] [blame] | 2928 | queue_work(nvme_wq, &ctrl->scan_work); |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 2929 | } |
| 2930 | EXPORT_SYMBOL_GPL(nvme_queue_scan); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2931 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2932 | /* |
| 2933 | * This function iterates the namespace list unlocked to allow recovery from |
| 2934 | * controller failure. It is up to the caller to ensure the namespace list is |
| 2935 | * not modified by scan work while this function is executing. |
| 2936 | */ |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2937 | void nvme_remove_namespaces(struct nvme_ctrl *ctrl) |
| 2938 | { |
| 2939 | struct nvme_ns *ns, *next; |
| 2940 | |
Keith Busch | 0ff9d4e | 2016-05-12 08:37:14 -0600 | [diff] [blame] | 2941 | /* |
| 2942 | * The dead states indicates the controller was not gracefully |
| 2943 | * disconnected. In that case, we won't be able to flush any data while |
| 2944 | * removing the namespaces' disks; fail all the queues now to avoid |
| 2945 | * potentially having to clean up the failed sync later. |
| 2946 | */ |
| 2947 | if (ctrl->state == NVME_CTRL_DEAD) |
| 2948 | nvme_kill_queues(ctrl); |
| 2949 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2950 | list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) |
| 2951 | nvme_ns_remove(ns); |
| 2952 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 2953 | EXPORT_SYMBOL_GPL(nvme_remove_namespaces); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2954 | |
Keith Busch | e3d7874 | 2017-11-07 15:13:14 -0700 | [diff] [blame] | 2955 | static void nvme_aen_uevent(struct nvme_ctrl *ctrl) |
| 2956 | { |
| 2957 | char *envp[2] = { NULL, NULL }; |
| 2958 | u32 aen_result = ctrl->aen_result; |
| 2959 | |
| 2960 | ctrl->aen_result = 0; |
| 2961 | if (!aen_result) |
| 2962 | return; |
| 2963 | |
| 2964 | envp[0] = kasprintf(GFP_KERNEL, "NVME_AEN=%#08x", aen_result); |
| 2965 | if (!envp[0]) |
| 2966 | return; |
| 2967 | kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp); |
| 2968 | kfree(envp[0]); |
| 2969 | } |
| 2970 | |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 2971 | static void nvme_async_event_work(struct work_struct *work) |
| 2972 | { |
| 2973 | struct nvme_ctrl *ctrl = |
| 2974 | container_of(work, struct nvme_ctrl, async_event_work); |
| 2975 | |
Keith Busch | e3d7874 | 2017-11-07 15:13:14 -0700 | [diff] [blame] | 2976 | nvme_aen_uevent(ctrl); |
Keith Busch | ad22c35 | 2017-11-07 15:13:12 -0700 | [diff] [blame] | 2977 | ctrl->ops->submit_async_event(ctrl); |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 2978 | } |
| 2979 | |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 2980 | static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl) |
| 2981 | { |
| 2982 | |
| 2983 | u32 csts; |
| 2984 | |
| 2985 | if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) |
| 2986 | return false; |
| 2987 | |
| 2988 | if (csts == ~0) |
| 2989 | return false; |
| 2990 | |
| 2991 | return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP)); |
| 2992 | } |
| 2993 | |
| 2994 | static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl) |
| 2995 | { |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 2996 | struct nvme_fw_slot_info_log *log; |
| 2997 | |
| 2998 | log = kmalloc(sizeof(*log), GFP_KERNEL); |
| 2999 | if (!log) |
| 3000 | return; |
| 3001 | |
Keith Busch | c627c48 | 2017-11-07 10:28:31 -0700 | [diff] [blame] | 3002 | if (nvme_get_log(ctrl, NVME_LOG_FW_SLOT, log, sizeof(*log))) |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 3003 | dev_warn(ctrl->device, |
| 3004 | "Get FW SLOT INFO log error\n"); |
| 3005 | kfree(log); |
| 3006 | } |
| 3007 | |
| 3008 | static void nvme_fw_act_work(struct work_struct *work) |
| 3009 | { |
| 3010 | struct nvme_ctrl *ctrl = container_of(work, |
| 3011 | struct nvme_ctrl, fw_act_work); |
| 3012 | unsigned long fw_act_timeout; |
| 3013 | |
| 3014 | if (ctrl->mtfa) |
| 3015 | fw_act_timeout = jiffies + |
| 3016 | msecs_to_jiffies(ctrl->mtfa * 100); |
| 3017 | else |
| 3018 | fw_act_timeout = jiffies + |
| 3019 | msecs_to_jiffies(admin_timeout * 1000); |
| 3020 | |
| 3021 | nvme_stop_queues(ctrl); |
| 3022 | while (nvme_ctrl_pp_status(ctrl)) { |
| 3023 | if (time_after(jiffies, fw_act_timeout)) { |
| 3024 | dev_warn(ctrl->device, |
| 3025 | "Fw activation timeout, reset controller\n"); |
| 3026 | nvme_reset_ctrl(ctrl); |
| 3027 | break; |
| 3028 | } |
| 3029 | msleep(100); |
| 3030 | } |
| 3031 | |
| 3032 | if (ctrl->state != NVME_CTRL_LIVE) |
| 3033 | return; |
| 3034 | |
| 3035 | nvme_start_queues(ctrl); |
Minwoo Im | a806c6c | 2017-11-02 18:07:44 +0900 | [diff] [blame] | 3036 | /* read FW slot information to clear the AER */ |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 3037 | nvme_get_fw_slot_info(ctrl); |
| 3038 | } |
| 3039 | |
Christoph Hellwig | 7bf5853 | 2016-11-10 07:32:34 -0800 | [diff] [blame] | 3040 | void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status, |
| 3041 | union nvme_result *res) |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3042 | { |
Christoph Hellwig | 7bf5853 | 2016-11-10 07:32:34 -0800 | [diff] [blame] | 3043 | u32 result = le32_to_cpu(res->u32); |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3044 | |
Keith Busch | ad22c35 | 2017-11-07 15:13:12 -0700 | [diff] [blame] | 3045 | if (le16_to_cpu(status) >> 1 != NVME_SC_SUCCESS) |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3046 | return; |
| 3047 | |
Keith Busch | e3d7874 | 2017-11-07 15:13:14 -0700 | [diff] [blame] | 3048 | switch (result & 0x7) { |
| 3049 | case NVME_AER_ERROR: |
| 3050 | case NVME_AER_SMART: |
| 3051 | case NVME_AER_CSS: |
| 3052 | case NVME_AER_VS: |
| 3053 | ctrl->aen_result = result; |
| 3054 | break; |
| 3055 | default: |
| 3056 | break; |
| 3057 | } |
| 3058 | |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3059 | switch (result & 0xff07) { |
| 3060 | case NVME_AER_NOTICE_NS_CHANGED: |
| 3061 | dev_info(ctrl->device, "rescanning\n"); |
| 3062 | nvme_queue_scan(ctrl); |
| 3063 | break; |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 3064 | case NVME_AER_NOTICE_FW_ACT_STARTING: |
Sagi Grimberg | 1a40d97 | 2017-09-21 17:01:36 +0300 | [diff] [blame] | 3065 | queue_work(nvme_wq, &ctrl->fw_act_work); |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 3066 | break; |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3067 | default: |
| 3068 | dev_warn(ctrl->device, "async event result %08x\n", result); |
| 3069 | } |
Keith Busch | ad22c35 | 2017-11-07 15:13:12 -0700 | [diff] [blame] | 3070 | queue_work(nvme_wq, &ctrl->async_event_work); |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3071 | } |
| 3072 | EXPORT_SYMBOL_GPL(nvme_complete_async_event); |
| 3073 | |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 3074 | void nvme_stop_ctrl(struct nvme_ctrl *ctrl) |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 3075 | { |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 3076 | nvme_stop_keep_alive(ctrl); |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3077 | flush_work(&ctrl->async_event_work); |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 3078 | flush_work(&ctrl->scan_work); |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 3079 | cancel_work_sync(&ctrl->fw_act_work); |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 3080 | } |
| 3081 | EXPORT_SYMBOL_GPL(nvme_stop_ctrl); |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 3082 | |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 3083 | void nvme_start_ctrl(struct nvme_ctrl *ctrl) |
| 3084 | { |
| 3085 | if (ctrl->kato) |
| 3086 | nvme_start_keep_alive(ctrl); |
| 3087 | |
| 3088 | if (ctrl->queue_count > 1) { |
| 3089 | nvme_queue_scan(ctrl); |
Keith Busch | d99ca60 | 2017-11-07 15:13:13 -0700 | [diff] [blame] | 3090 | queue_work(nvme_wq, &ctrl->async_event_work); |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 3091 | nvme_start_queues(ctrl); |
| 3092 | } |
| 3093 | } |
| 3094 | EXPORT_SYMBOL_GPL(nvme_start_ctrl); |
| 3095 | |
| 3096 | void nvme_uninit_ctrl(struct nvme_ctrl *ctrl) |
| 3097 | { |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 3098 | cdev_device_del(&ctrl->cdev, ctrl->device); |
Keith Busch | 53029b0 | 2015-11-28 15:41:02 +0100 | [diff] [blame] | 3099 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 3100 | EXPORT_SYMBOL_GPL(nvme_uninit_ctrl); |
Keith Busch | 53029b0 | 2015-11-28 15:41:02 +0100 | [diff] [blame] | 3101 | |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 3102 | static void nvme_free_ctrl(struct device *dev) |
Keith Busch | 53029b0 | 2015-11-28 15:41:02 +0100 | [diff] [blame] | 3103 | { |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 3104 | struct nvme_ctrl *ctrl = |
| 3105 | container_of(dev, struct nvme_ctrl, ctrl_device); |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 3106 | struct nvme_subsystem *subsys = ctrl->subsys; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3107 | |
Christoph Hellwig | 9843f68 | 2017-10-18 13:10:01 +0200 | [diff] [blame] | 3108 | ida_simple_remove(&nvme_instance_ida, ctrl->instance); |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 3109 | kfree(ctrl->effects); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3110 | |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 3111 | if (subsys) { |
| 3112 | mutex_lock(&subsys->lock); |
| 3113 | list_del(&ctrl->subsys_entry); |
| 3114 | mutex_unlock(&subsys->lock); |
| 3115 | sysfs_remove_link(&subsys->dev.kobj, dev_name(ctrl->device)); |
| 3116 | } |
| 3117 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3118 | ctrl->ops->free_ctrl(ctrl); |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 3119 | |
| 3120 | if (subsys) |
| 3121 | nvme_put_subsystem(subsys); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3122 | } |
| 3123 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3124 | /* |
| 3125 | * Initialize a NVMe controller structures. This needs to be called during |
| 3126 | * earliest initialization so that we have the initialized structured around |
| 3127 | * during probing. |
| 3128 | */ |
| 3129 | int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev, |
| 3130 | const struct nvme_ctrl_ops *ops, unsigned long quirks) |
| 3131 | { |
| 3132 | int ret; |
| 3133 | |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 3134 | ctrl->state = NVME_CTRL_NEW; |
| 3135 | spin_lock_init(&ctrl->lock); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3136 | INIT_LIST_HEAD(&ctrl->namespaces); |
Christoph Hellwig | 69d3b8a | 2015-12-24 15:27:00 +0100 | [diff] [blame] | 3137 | mutex_init(&ctrl->namespaces_mutex); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3138 | ctrl->dev = dev; |
| 3139 | ctrl->ops = ops; |
| 3140 | ctrl->quirks = quirks; |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 3141 | INIT_WORK(&ctrl->scan_work, nvme_scan_work); |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3142 | INIT_WORK(&ctrl->async_event_work, nvme_async_event_work); |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 3143 | INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work); |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 3144 | INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3145 | |
Christoph Hellwig | 9843f68 | 2017-10-18 13:10:01 +0200 | [diff] [blame] | 3146 | ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL); |
| 3147 | if (ret < 0) |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3148 | goto out; |
Christoph Hellwig | 9843f68 | 2017-10-18 13:10:01 +0200 | [diff] [blame] | 3149 | ctrl->instance = ret; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3150 | |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 3151 | device_initialize(&ctrl->ctrl_device); |
| 3152 | ctrl->device = &ctrl->ctrl_device; |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 3153 | ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance); |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 3154 | ctrl->device->class = nvme_class; |
| 3155 | ctrl->device->parent = ctrl->dev; |
| 3156 | ctrl->device->groups = nvme_dev_attr_groups; |
| 3157 | ctrl->device->release = nvme_free_ctrl; |
| 3158 | dev_set_drvdata(ctrl->device, ctrl); |
| 3159 | ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance); |
| 3160 | if (ret) |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3161 | goto out_release_instance; |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 3162 | |
| 3163 | cdev_init(&ctrl->cdev, &nvme_dev_fops); |
| 3164 | ctrl->cdev.owner = ops->module; |
| 3165 | ret = cdev_device_add(&ctrl->cdev, ctrl->device); |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 3166 | if (ret) |
| 3167 | goto out_free_name; |
| 3168 | |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 3169 | /* |
| 3170 | * Initialize latency tolerance controls. The sysfs files won't |
| 3171 | * be visible to userspace unless the device actually supports APST. |
| 3172 | */ |
| 3173 | ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance; |
| 3174 | dev_pm_qos_update_user_latency_tolerance(ctrl->device, |
| 3175 | min(default_ps_max_latency_us, (unsigned long)S32_MAX)); |
| 3176 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3177 | return 0; |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 3178 | out_free_name: |
| 3179 | kfree_const(dev->kobj.name); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3180 | out_release_instance: |
Christoph Hellwig | 9843f68 | 2017-10-18 13:10:01 +0200 | [diff] [blame] | 3181 | ida_simple_remove(&nvme_instance_ida, ctrl->instance); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3182 | out: |
| 3183 | return ret; |
| 3184 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 3185 | EXPORT_SYMBOL_GPL(nvme_init_ctrl); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3186 | |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 3187 | /** |
| 3188 | * nvme_kill_queues(): Ends all namespace queues |
| 3189 | * @ctrl: the dead controller that needs to end |
| 3190 | * |
| 3191 | * Call this function when the driver determines it is unable to get the |
| 3192 | * controller in a state capable of servicing IO. |
| 3193 | */ |
| 3194 | void nvme_kill_queues(struct nvme_ctrl *ctrl) |
| 3195 | { |
| 3196 | struct nvme_ns *ns; |
| 3197 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3198 | mutex_lock(&ctrl->namespaces_mutex); |
Ming Lei | 82654b6 | 2017-06-02 16:32:08 +0800 | [diff] [blame] | 3199 | |
Ming Lei | 443bd90 | 2017-06-19 10:21:08 +0800 | [diff] [blame] | 3200 | /* Forcibly unquiesce queues to avoid blocking dispatch */ |
Scott Bauer | 7dd1ab1 | 2017-07-25 10:27:06 -0600 | [diff] [blame] | 3201 | if (ctrl->admin_q) |
| 3202 | blk_mq_unquiesce_queue(ctrl->admin_q); |
Ming Lei | 443bd90 | 2017-06-19 10:21:08 +0800 | [diff] [blame] | 3203 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3204 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 3205 | /* |
| 3206 | * Revalidating a dead namespace sets capacity to 0. This will |
| 3207 | * end buffered writers dirtying pages that can't be synced. |
| 3208 | */ |
Keith Busch | f33447b | 2017-02-10 18:15:51 -0500 | [diff] [blame] | 3209 | if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags)) |
| 3210 | continue; |
| 3211 | revalidate_disk(ns->disk); |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 3212 | blk_set_queue_dying(ns->queue); |
Ming Lei | 806f026 | 2017-05-22 23:05:03 +0800 | [diff] [blame] | 3213 | |
Ming Lei | 443bd90 | 2017-06-19 10:21:08 +0800 | [diff] [blame] | 3214 | /* Forcibly unquiesce queues to avoid blocking dispatch */ |
| 3215 | blk_mq_unquiesce_queue(ns->queue); |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 3216 | } |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3217 | mutex_unlock(&ctrl->namespaces_mutex); |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 3218 | } |
Linus Torvalds | 237045f | 2016-03-18 17:13:31 -0700 | [diff] [blame] | 3219 | EXPORT_SYMBOL_GPL(nvme_kill_queues); |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 3220 | |
Keith Busch | 302ad8c | 2017-03-01 14:22:12 -0500 | [diff] [blame] | 3221 | void nvme_unfreeze(struct nvme_ctrl *ctrl) |
| 3222 | { |
| 3223 | struct nvme_ns *ns; |
| 3224 | |
| 3225 | mutex_lock(&ctrl->namespaces_mutex); |
| 3226 | list_for_each_entry(ns, &ctrl->namespaces, list) |
| 3227 | blk_mq_unfreeze_queue(ns->queue); |
| 3228 | mutex_unlock(&ctrl->namespaces_mutex); |
| 3229 | } |
| 3230 | EXPORT_SYMBOL_GPL(nvme_unfreeze); |
| 3231 | |
| 3232 | void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout) |
| 3233 | { |
| 3234 | struct nvme_ns *ns; |
| 3235 | |
| 3236 | mutex_lock(&ctrl->namespaces_mutex); |
| 3237 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
| 3238 | timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout); |
| 3239 | if (timeout <= 0) |
| 3240 | break; |
| 3241 | } |
| 3242 | mutex_unlock(&ctrl->namespaces_mutex); |
| 3243 | } |
| 3244 | EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout); |
| 3245 | |
| 3246 | void nvme_wait_freeze(struct nvme_ctrl *ctrl) |
| 3247 | { |
| 3248 | struct nvme_ns *ns; |
| 3249 | |
| 3250 | mutex_lock(&ctrl->namespaces_mutex); |
| 3251 | list_for_each_entry(ns, &ctrl->namespaces, list) |
| 3252 | blk_mq_freeze_queue_wait(ns->queue); |
| 3253 | mutex_unlock(&ctrl->namespaces_mutex); |
| 3254 | } |
| 3255 | EXPORT_SYMBOL_GPL(nvme_wait_freeze); |
| 3256 | |
| 3257 | void nvme_start_freeze(struct nvme_ctrl *ctrl) |
| 3258 | { |
| 3259 | struct nvme_ns *ns; |
| 3260 | |
| 3261 | mutex_lock(&ctrl->namespaces_mutex); |
| 3262 | list_for_each_entry(ns, &ctrl->namespaces, list) |
Ming Lei | 1671d52 | 2017-03-27 20:06:57 +0800 | [diff] [blame] | 3263 | blk_freeze_queue_start(ns->queue); |
Keith Busch | 302ad8c | 2017-03-01 14:22:12 -0500 | [diff] [blame] | 3264 | mutex_unlock(&ctrl->namespaces_mutex); |
| 3265 | } |
| 3266 | EXPORT_SYMBOL_GPL(nvme_start_freeze); |
| 3267 | |
Keith Busch | 2564626 | 2016-01-04 09:10:57 -0700 | [diff] [blame] | 3268 | void nvme_stop_queues(struct nvme_ctrl *ctrl) |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 3269 | { |
| 3270 | struct nvme_ns *ns; |
| 3271 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3272 | mutex_lock(&ctrl->namespaces_mutex); |
Bart Van Assche | a6eaa88 | 2016-10-28 17:23:40 -0700 | [diff] [blame] | 3273 | list_for_each_entry(ns, &ctrl->namespaces, list) |
Bart Van Assche | 3174dd3 | 2016-10-28 17:23:19 -0700 | [diff] [blame] | 3274 | blk_mq_quiesce_queue(ns->queue); |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3275 | mutex_unlock(&ctrl->namespaces_mutex); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 3276 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 3277 | EXPORT_SYMBOL_GPL(nvme_stop_queues); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 3278 | |
Keith Busch | 2564626 | 2016-01-04 09:10:57 -0700 | [diff] [blame] | 3279 | void nvme_start_queues(struct nvme_ctrl *ctrl) |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 3280 | { |
| 3281 | struct nvme_ns *ns; |
| 3282 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3283 | mutex_lock(&ctrl->namespaces_mutex); |
Sagi Grimberg | 8d7b8fa | 2017-07-04 18:16:58 +0300 | [diff] [blame] | 3284 | list_for_each_entry(ns, &ctrl->namespaces, list) |
Ming Lei | f660174 | 2017-06-06 23:22:04 +0800 | [diff] [blame] | 3285 | blk_mq_unquiesce_queue(ns->queue); |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3286 | mutex_unlock(&ctrl->namespaces_mutex); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 3287 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 3288 | EXPORT_SYMBOL_GPL(nvme_start_queues); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 3289 | |
Sagi Grimberg | 31b8446 | 2017-10-11 12:53:07 +0300 | [diff] [blame] | 3290 | int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set) |
| 3291 | { |
| 3292 | if (!ctrl->ops->reinit_request) |
| 3293 | return 0; |
| 3294 | |
| 3295 | return blk_mq_tagset_iter(set, set->driver_data, |
| 3296 | ctrl->ops->reinit_request); |
| 3297 | } |
| 3298 | EXPORT_SYMBOL_GPL(nvme_reinit_tagset); |
| 3299 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3300 | int __init nvme_core_init(void) |
| 3301 | { |
| 3302 | int result; |
| 3303 | |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 3304 | nvme_wq = alloc_workqueue("nvme-wq", |
| 3305 | WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0); |
| 3306 | if (!nvme_wq) |
| 3307 | return -ENOMEM; |
| 3308 | |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 3309 | result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme"); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3310 | if (result < 0) |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 3311 | goto destroy_wq; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3312 | |
| 3313 | nvme_class = class_create(THIS_MODULE, "nvme"); |
| 3314 | if (IS_ERR(nvme_class)) { |
| 3315 | result = PTR_ERR(nvme_class); |
| 3316 | goto unregister_chrdev; |
| 3317 | } |
| 3318 | |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 3319 | nvme_subsys_class = class_create(THIS_MODULE, "nvme-subsystem"); |
| 3320 | if (IS_ERR(nvme_subsys_class)) { |
| 3321 | result = PTR_ERR(nvme_subsys_class); |
| 3322 | goto destroy_class; |
| 3323 | } |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3324 | return 0; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3325 | |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 3326 | destroy_class: |
| 3327 | class_destroy(nvme_class); |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 3328 | unregister_chrdev: |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 3329 | unregister_chrdev_region(nvme_chr_devt, NVME_MINORS); |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 3330 | destroy_wq: |
| 3331 | destroy_workqueue(nvme_wq); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3332 | return result; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | void nvme_core_exit(void) |
| 3336 | { |
Christoph Hellwig | ab9e00c | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 3337 | ida_destroy(&nvme_subsystems_ida); |
| 3338 | class_destroy(nvme_subsys_class); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3339 | class_destroy(nvme_class); |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 3340 | unregister_chrdev_region(nvme_chr_devt, NVME_MINORS); |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 3341 | destroy_workqueue(nvme_wq); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3342 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 3343 | |
| 3344 | MODULE_LICENSE("GPL"); |
| 3345 | MODULE_VERSION("1.0"); |
| 3346 | module_init(nvme_core_init); |
| 3347 | module_exit(nvme_core_exit); |