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