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