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