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