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