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