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