Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * NVM Express device driver |
| 3 | * Copyright (c) 2011-2014, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/blkdev.h> |
| 16 | #include <linux/blk-mq.h> |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 17 | #include <linux/delay.h> |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 18 | #include <linux/errno.h> |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 19 | #include <linux/hdreg.h> |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/list_sort.h> |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 23 | #include <linux/slab.h> |
| 24 | #include <linux/types.h> |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 25 | #include <linux/pr.h> |
| 26 | #include <linux/ptrace.h> |
| 27 | #include <linux/nvme_ioctl.h> |
| 28 | #include <linux/t10-pi.h> |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 29 | #include <linux/pm_qos.h> |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 30 | #include <asm/unaligned.h> |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 31 | |
Johannes Thumshirn | 3d030e4 | 2018-01-26 11:21:37 +0100 | [diff] [blame] | 32 | #define CREATE_TRACE_POINTS |
| 33 | #include "trace.h" |
| 34 | |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 35 | #include "nvme.h" |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 36 | #include "fabrics.h" |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 37 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 38 | #define NVME_MINORS (1U << MINORBITS) |
| 39 | |
Marc Olson | 8ae4e44 | 2017-09-06 17:23:56 -0700 | [diff] [blame] | 40 | unsigned int admin_timeout = 60; |
| 41 | module_param(admin_timeout, uint, 0644); |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 42 | MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands"); |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 43 | EXPORT_SYMBOL_GPL(admin_timeout); |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 44 | |
Marc Olson | 8ae4e44 | 2017-09-06 17:23:56 -0700 | [diff] [blame] | 45 | unsigned int nvme_io_timeout = 30; |
| 46 | module_param_named(io_timeout, nvme_io_timeout, uint, 0644); |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 47 | MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O"); |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 48 | EXPORT_SYMBOL_GPL(nvme_io_timeout); |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 49 | |
Christoph Hellwig | b3b1b0b | 2017-06-12 18:30:51 +0200 | [diff] [blame] | 50 | static unsigned char shutdown_timeout = 5; |
Ming Lin | ba0ba7d | 2016-02-10 10:03:30 -0800 | [diff] [blame] | 51 | module_param(shutdown_timeout, byte, 0644); |
| 52 | MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown"); |
| 53 | |
Christoph Hellwig | 44e44b2 | 2017-04-05 19:18:11 +0200 | [diff] [blame] | 54 | static u8 nvme_max_retries = 5; |
| 55 | module_param_named(max_retries, nvme_max_retries, byte, 0644); |
Keith Busch | f80ec96 | 2016-07-12 16:20:31 -0700 | [diff] [blame] | 56 | 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] | 57 | |
Kai-Heng Feng | 9947d6a | 2017-06-07 15:25:43 +0800 | [diff] [blame] | 58 | static unsigned long default_ps_max_latency_us = 100000; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 59 | module_param(default_ps_max_latency_us, ulong, 0644); |
| 60 | MODULE_PARM_DESC(default_ps_max_latency_us, |
| 61 | "max power saving latency for new devices; use PM QOS to change per device"); |
| 62 | |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 63 | static bool force_apst; |
| 64 | module_param(force_apst, bool, 0644); |
| 65 | MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off"); |
| 66 | |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 67 | static bool streams; |
| 68 | module_param(streams, bool, 0644); |
| 69 | MODULE_PARM_DESC(streams, "turn on support for Streams write directives"); |
| 70 | |
Roy Shterman | b227c59 | 2018-01-14 12:39:02 +0200 | [diff] [blame] | 71 | /* |
| 72 | * nvme_wq - hosts nvme related works that are not reset or delete |
| 73 | * nvme_reset_wq - hosts nvme reset works |
| 74 | * nvme_delete_wq - hosts nvme delete works |
| 75 | * |
| 76 | * nvme_wq will host works such are scan, aen handling, fw activation, |
| 77 | * keep-alive error recovery, periodic reconnects etc. nvme_reset_wq |
| 78 | * runs reset works which also flush works hosted on nvme_wq for |
| 79 | * serialization purposes. nvme_delete_wq host controller deletion |
| 80 | * works which flush reset works for serialization. |
| 81 | */ |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 82 | struct workqueue_struct *nvme_wq; |
| 83 | EXPORT_SYMBOL_GPL(nvme_wq); |
| 84 | |
Roy Shterman | b227c59 | 2018-01-14 12:39:02 +0200 | [diff] [blame] | 85 | struct workqueue_struct *nvme_reset_wq; |
| 86 | EXPORT_SYMBOL_GPL(nvme_reset_wq); |
| 87 | |
| 88 | struct workqueue_struct *nvme_delete_wq; |
| 89 | EXPORT_SYMBOL_GPL(nvme_delete_wq); |
| 90 | |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 91 | static DEFINE_IDA(nvme_subsystems_ida); |
| 92 | static LIST_HEAD(nvme_subsystems); |
| 93 | static DEFINE_MUTEX(nvme_subsystems_lock); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 94 | |
Christoph Hellwig | 9843f68 | 2017-10-18 13:10:01 +0200 | [diff] [blame] | 95 | static DEFINE_IDA(nvme_instance_ida); |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 96 | static dev_t nvme_chr_devt; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 97 | static struct class *nvme_class; |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 98 | static struct class *nvme_subsys_class; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 99 | |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 100 | static void nvme_ns_remove(struct nvme_ns *ns); |
| 101 | static int nvme_revalidate_disk(struct gendisk *disk); |
Jianchao Wang | 12d9f07 | 2018-05-04 16:01:57 +0800 | [diff] [blame] | 102 | static void nvme_put_subsystem(struct nvme_subsystem *subsys); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 103 | |
Christoph Hellwig | 50e8d8e | 2018-05-25 18:15:47 +0200 | [diff] [blame] | 104 | static void nvme_queue_scan(struct nvme_ctrl *ctrl) |
| 105 | { |
| 106 | /* |
| 107 | * Only new queue scan work when admin and IO queues are both alive |
| 108 | */ |
| 109 | if (ctrl->state == NVME_CTRL_LIVE) |
| 110 | queue_work(nvme_wq, &ctrl->scan_work); |
| 111 | } |
| 112 | |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 113 | int nvme_reset_ctrl(struct nvme_ctrl *ctrl) |
| 114 | { |
| 115 | if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING)) |
| 116 | return -EBUSY; |
Roy Shterman | b227c59 | 2018-01-14 12:39:02 +0200 | [diff] [blame] | 117 | if (!queue_work(nvme_reset_wq, &ctrl->reset_work)) |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 118 | return -EBUSY; |
| 119 | return 0; |
| 120 | } |
| 121 | EXPORT_SYMBOL_GPL(nvme_reset_ctrl); |
| 122 | |
Sagi Grimberg | 79c48cc | 2018-01-14 12:39:00 +0200 | [diff] [blame] | 123 | int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl) |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 124 | { |
| 125 | int ret; |
| 126 | |
| 127 | ret = nvme_reset_ctrl(ctrl); |
Nitzan Carmi | 8000d1f | 2018-01-17 11:01:14 +0000 | [diff] [blame] | 128 | if (!ret) { |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 129 | flush_work(&ctrl->reset_work); |
Charles Machalow | 4e50d9e | 2018-05-10 16:01:38 -0700 | [diff] [blame] | 130 | if (ctrl->state != NVME_CTRL_LIVE && |
| 131 | ctrl->state != NVME_CTRL_ADMIN_ONLY) |
Nitzan Carmi | 8000d1f | 2018-01-17 11:01:14 +0000 | [diff] [blame] | 132 | ret = -ENETRESET; |
| 133 | } |
| 134 | |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 135 | return ret; |
| 136 | } |
Sagi Grimberg | 79c48cc | 2018-01-14 12:39:00 +0200 | [diff] [blame] | 137 | EXPORT_SYMBOL_GPL(nvme_reset_ctrl_sync); |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 138 | |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 139 | static void nvme_delete_ctrl_work(struct work_struct *work) |
| 140 | { |
| 141 | struct nvme_ctrl *ctrl = |
| 142 | container_of(work, struct nvme_ctrl, delete_work); |
| 143 | |
Max Gurtovoy | 77d0612 | 2018-03-11 17:46:06 +0200 | [diff] [blame] | 144 | dev_info(ctrl->device, |
| 145 | "Removing ctrl: NQN \"%s\"\n", ctrl->opts->subsysnqn); |
| 146 | |
Sagi Grimberg | 4054637 | 2017-10-29 14:21:02 +0200 | [diff] [blame] | 147 | flush_work(&ctrl->reset_work); |
Christoph Hellwig | 6cd53d1 | 2017-10-29 10:44:31 +0200 | [diff] [blame] | 148 | nvme_stop_ctrl(ctrl); |
| 149 | nvme_remove_namespaces(ctrl); |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 150 | ctrl->ops->delete_ctrl(ctrl); |
Christoph Hellwig | 6cd53d1 | 2017-10-29 10:44:31 +0200 | [diff] [blame] | 151 | nvme_uninit_ctrl(ctrl); |
| 152 | nvme_put_ctrl(ctrl); |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | int nvme_delete_ctrl(struct nvme_ctrl *ctrl) |
| 156 | { |
| 157 | if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING)) |
| 158 | return -EBUSY; |
Roy Shterman | b227c59 | 2018-01-14 12:39:02 +0200 | [diff] [blame] | 159 | if (!queue_work(nvme_delete_wq, &ctrl->delete_work)) |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 160 | return -EBUSY; |
| 161 | return 0; |
| 162 | } |
| 163 | EXPORT_SYMBOL_GPL(nvme_delete_ctrl); |
| 164 | |
| 165 | int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl) |
| 166 | { |
| 167 | int ret = 0; |
| 168 | |
| 169 | /* |
| 170 | * Keep a reference until the work is flushed since ->delete_ctrl |
| 171 | * can free the controller. |
| 172 | */ |
| 173 | nvme_get_ctrl(ctrl); |
| 174 | ret = nvme_delete_ctrl(ctrl); |
| 175 | if (!ret) |
| 176 | flush_work(&ctrl->delete_work); |
| 177 | nvme_put_ctrl(ctrl); |
| 178 | return ret; |
| 179 | } |
| 180 | EXPORT_SYMBOL_GPL(nvme_delete_ctrl_sync); |
| 181 | |
Christoph Hellwig | 715ea9e | 2017-11-07 17:27:34 +0100 | [diff] [blame] | 182 | static inline bool nvme_ns_has_pi(struct nvme_ns *ns) |
| 183 | { |
| 184 | return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple); |
| 185 | } |
| 186 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 187 | static blk_status_t nvme_error_status(struct request *req) |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 188 | { |
| 189 | switch (nvme_req(req)->status & 0x7ff) { |
| 190 | case NVME_SC_SUCCESS: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 191 | return BLK_STS_OK; |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 192 | case NVME_SC_CAP_EXCEEDED: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 193 | return BLK_STS_NOSPC; |
Keith Busch | e96fef2 | 2018-01-09 12:04:14 -0700 | [diff] [blame] | 194 | case NVME_SC_LBA_RANGE: |
| 195 | return BLK_STS_TARGET; |
| 196 | case NVME_SC_BAD_ATTRIBUTES: |
Junxiong Guan | e02ab02 | 2017-04-21 12:59:07 +0200 | [diff] [blame] | 197 | case NVME_SC_ONCS_NOT_SUPPORTED: |
Keith Busch | e96fef2 | 2018-01-09 12:04:14 -0700 | [diff] [blame] | 198 | case NVME_SC_INVALID_OPCODE: |
| 199 | case NVME_SC_INVALID_FIELD: |
| 200 | case NVME_SC_INVALID_NS: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 201 | return BLK_STS_NOTSUPP; |
Junxiong Guan | e02ab02 | 2017-04-21 12:59:07 +0200 | [diff] [blame] | 202 | case NVME_SC_WRITE_FAULT: |
| 203 | case NVME_SC_READ_ERROR: |
| 204 | case NVME_SC_UNWRITTEN_BLOCK: |
Christoph Hellwig | a751da3 | 2017-08-22 10:17:03 +0200 | [diff] [blame] | 205 | case NVME_SC_ACCESS_DENIED: |
| 206 | case NVME_SC_READ_ONLY: |
Keith Busch | e96fef2 | 2018-01-09 12:04:14 -0700 | [diff] [blame] | 207 | case NVME_SC_COMPARE_FAILED: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 208 | return BLK_STS_MEDIUM; |
Christoph Hellwig | a751da3 | 2017-08-22 10:17:03 +0200 | [diff] [blame] | 209 | case NVME_SC_GUARD_CHECK: |
| 210 | case NVME_SC_APPTAG_CHECK: |
| 211 | case NVME_SC_REFTAG_CHECK: |
| 212 | case NVME_SC_INVALID_PI: |
| 213 | return BLK_STS_PROTECTION; |
| 214 | case NVME_SC_RESERVATION_CONFLICT: |
| 215 | return BLK_STS_NEXUS; |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 216 | default: |
| 217 | return BLK_STS_IOERR; |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 218 | } |
| 219 | } |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 220 | |
Christoph Hellwig | f6324b1 | 2017-04-05 19:18:09 +0200 | [diff] [blame] | 221 | static inline bool nvme_req_needs_retry(struct request *req) |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 222 | { |
Christoph Hellwig | f6324b1 | 2017-04-05 19:18:09 +0200 | [diff] [blame] | 223 | if (blk_noretry_request(req)) |
| 224 | return false; |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 225 | if (nvme_req(req)->status & NVME_SC_DNR) |
Christoph Hellwig | f6324b1 | 2017-04-05 19:18:09 +0200 | [diff] [blame] | 226 | return false; |
Christoph Hellwig | 44e44b2 | 2017-04-05 19:18:11 +0200 | [diff] [blame] | 227 | if (nvme_req(req)->retries >= nvme_max_retries) |
Christoph Hellwig | f6324b1 | 2017-04-05 19:18:09 +0200 | [diff] [blame] | 228 | return false; |
| 229 | return true; |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void nvme_complete_rq(struct request *req) |
| 233 | { |
Keith Busch | 908e456 | 2018-01-09 12:04:15 -0700 | [diff] [blame] | 234 | blk_status_t status = nvme_error_status(req); |
| 235 | |
Johannes Thumshirn | ca5554a | 2018-01-26 11:21:38 +0100 | [diff] [blame] | 236 | trace_nvme_complete_rq(req); |
| 237 | |
Keith Busch | 908e456 | 2018-01-09 12:04:15 -0700 | [diff] [blame] | 238 | if (unlikely(status != BLK_STS_OK && nvme_req_needs_retry(req))) { |
| 239 | if (nvme_req_needs_failover(req, status)) { |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 240 | nvme_failover_req(req); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | if (!blk_queue_dying(req->q)) { |
| 245 | nvme_req(req)->retries++; |
| 246 | blk_mq_requeue_request(req, true); |
| 247 | return; |
| 248 | } |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 249 | } |
Keith Busch | 908e456 | 2018-01-09 12:04:15 -0700 | [diff] [blame] | 250 | blk_mq_end_request(req, status); |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 251 | } |
| 252 | EXPORT_SYMBOL_GPL(nvme_complete_rq); |
| 253 | |
Ming Lin | c55a2fd | 2016-05-18 14:05:02 -0700 | [diff] [blame] | 254 | void nvme_cancel_request(struct request *req, void *data, bool reserved) |
| 255 | { |
Ming Lin | c55a2fd | 2016-05-18 14:05:02 -0700 | [diff] [blame] | 256 | dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device, |
| 257 | "Cancelling I/O %d", req->tag); |
| 258 | |
Christoph Hellwig | e54b064 | 2017-11-02 21:28:51 +0300 | [diff] [blame] | 259 | nvme_req(req)->status = NVME_SC_ABORT_REQ; |
Christoph Hellwig | 08e0029 | 2017-04-20 16:03:09 +0200 | [diff] [blame] | 260 | blk_mq_complete_request(req); |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 261 | |
Ming Lin | c55a2fd | 2016-05-18 14:05:02 -0700 | [diff] [blame] | 262 | } |
| 263 | EXPORT_SYMBOL_GPL(nvme_cancel_request); |
| 264 | |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 265 | bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl, |
| 266 | enum nvme_ctrl_state new_state) |
| 267 | { |
Gabriel Krisman Bertazi | f6b6a28 | 2016-07-29 16:15:18 -0300 | [diff] [blame] | 268 | enum nvme_ctrl_state old_state; |
Christoph Hellwig | 0a72bbb | 2017-08-22 11:42:24 +0200 | [diff] [blame] | 269 | unsigned long flags; |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 270 | bool changed = false; |
| 271 | |
Christoph Hellwig | 0a72bbb | 2017-08-22 11:42:24 +0200 | [diff] [blame] | 272 | spin_lock_irqsave(&ctrl->lock, flags); |
Gabriel Krisman Bertazi | f6b6a28 | 2016-07-29 16:15:18 -0300 | [diff] [blame] | 273 | |
| 274 | old_state = ctrl->state; |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 275 | switch (new_state) { |
Jianchao Wang | 2b1b7e7 | 2018-01-06 08:01:58 +0800 | [diff] [blame] | 276 | case NVME_CTRL_ADMIN_ONLY: |
| 277 | switch (old_state) { |
Max Gurtovoy | ad6a0a5 | 2018-01-31 18:31:24 +0200 | [diff] [blame] | 278 | case NVME_CTRL_CONNECTING: |
Jianchao Wang | 2b1b7e7 | 2018-01-06 08:01:58 +0800 | [diff] [blame] | 279 | changed = true; |
| 280 | /* FALLTHRU */ |
| 281 | default: |
| 282 | break; |
| 283 | } |
| 284 | break; |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 285 | case NVME_CTRL_LIVE: |
| 286 | switch (old_state) { |
Christoph Hellwig | 7d2e800 | 2016-06-13 16:45:22 +0200 | [diff] [blame] | 287 | case NVME_CTRL_NEW: |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 288 | case NVME_CTRL_RESETTING: |
Max Gurtovoy | ad6a0a5 | 2018-01-31 18:31:24 +0200 | [diff] [blame] | 289 | case NVME_CTRL_CONNECTING: |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 290 | changed = true; |
| 291 | /* FALLTHRU */ |
| 292 | default: |
| 293 | break; |
| 294 | } |
| 295 | break; |
| 296 | case NVME_CTRL_RESETTING: |
| 297 | switch (old_state) { |
| 298 | case NVME_CTRL_NEW: |
| 299 | case NVME_CTRL_LIVE: |
Jianchao Wang | 2b1b7e7 | 2018-01-06 08:01:58 +0800 | [diff] [blame] | 300 | case NVME_CTRL_ADMIN_ONLY: |
Christoph Hellwig | def61ec | 2016-07-06 21:55:49 +0900 | [diff] [blame] | 301 | changed = true; |
| 302 | /* FALLTHRU */ |
| 303 | default: |
| 304 | break; |
| 305 | } |
| 306 | break; |
Max Gurtovoy | ad6a0a5 | 2018-01-31 18:31:24 +0200 | [diff] [blame] | 307 | case NVME_CTRL_CONNECTING: |
Christoph Hellwig | def61ec | 2016-07-06 21:55:49 +0900 | [diff] [blame] | 308 | switch (old_state) { |
Max Gurtovoy | b754a32 | 2018-01-31 18:31:25 +0200 | [diff] [blame] | 309 | case NVME_CTRL_NEW: |
James Smart | 3cec7f9 | 2017-10-25 16:43:13 -0700 | [diff] [blame] | 310 | case NVME_CTRL_RESETTING: |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 311 | changed = true; |
| 312 | /* FALLTHRU */ |
| 313 | default: |
| 314 | break; |
| 315 | } |
| 316 | break; |
| 317 | case NVME_CTRL_DELETING: |
| 318 | switch (old_state) { |
| 319 | case NVME_CTRL_LIVE: |
Jianchao Wang | 2b1b7e7 | 2018-01-06 08:01:58 +0800 | [diff] [blame] | 320 | case NVME_CTRL_ADMIN_ONLY: |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 321 | case NVME_CTRL_RESETTING: |
Max Gurtovoy | ad6a0a5 | 2018-01-31 18:31:24 +0200 | [diff] [blame] | 322 | case NVME_CTRL_CONNECTING: |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 323 | changed = true; |
| 324 | /* FALLTHRU */ |
| 325 | default: |
| 326 | break; |
| 327 | } |
| 328 | break; |
Keith Busch | 0ff9d4e | 2016-05-12 08:37:14 -0600 | [diff] [blame] | 329 | case NVME_CTRL_DEAD: |
| 330 | switch (old_state) { |
| 331 | case NVME_CTRL_DELETING: |
| 332 | changed = true; |
| 333 | /* FALLTHRU */ |
| 334 | default: |
| 335 | break; |
| 336 | } |
| 337 | break; |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 338 | default: |
| 339 | break; |
| 340 | } |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 341 | |
| 342 | if (changed) |
| 343 | ctrl->state = new_state; |
| 344 | |
Christoph Hellwig | 0a72bbb | 2017-08-22 11:42:24 +0200 | [diff] [blame] | 345 | spin_unlock_irqrestore(&ctrl->lock, flags); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 346 | if (changed && ctrl->state == NVME_CTRL_LIVE) |
| 347 | nvme_kick_requeue_lists(ctrl); |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 348 | return changed; |
| 349 | } |
| 350 | EXPORT_SYMBOL_GPL(nvme_change_ctrl_state); |
| 351 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 352 | static void nvme_free_ns_head(struct kref *ref) |
| 353 | { |
| 354 | struct nvme_ns_head *head = |
| 355 | container_of(ref, struct nvme_ns_head, ref); |
| 356 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 357 | nvme_mpath_remove_disk(head); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 358 | ida_simple_remove(&head->subsys->ns_ida, head->instance); |
| 359 | list_del_init(&head->entry); |
Nitzan Carmi | 4317228 | 2018-04-09 17:50:26 +0300 | [diff] [blame] | 360 | cleanup_srcu_struct_quiesced(&head->srcu); |
Jianchao Wang | 12d9f07 | 2018-05-04 16:01:57 +0800 | [diff] [blame] | 361 | nvme_put_subsystem(head->subsys); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 362 | kfree(head); |
| 363 | } |
| 364 | |
| 365 | static void nvme_put_ns_head(struct nvme_ns_head *head) |
| 366 | { |
| 367 | kref_put(&head->ref, nvme_free_ns_head); |
| 368 | } |
| 369 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 370 | static void nvme_free_ns(struct kref *kref) |
| 371 | { |
| 372 | struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref); |
| 373 | |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 374 | if (ns->ndev) |
| 375 | nvme_nvm_unregister(ns); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 376 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 377 | put_disk(ns->disk); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 378 | nvme_put_ns_head(ns->head); |
Keith Busch | 075790e | 2016-02-24 09:15:53 -0700 | [diff] [blame] | 379 | nvme_put_ctrl(ns->ctrl); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 380 | kfree(ns); |
| 381 | } |
| 382 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 383 | static void nvme_put_ns(struct nvme_ns *ns) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 384 | { |
| 385 | kref_put(&ns->kref, nvme_free_ns); |
| 386 | } |
| 387 | |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 388 | static inline void nvme_clear_nvme_request(struct request *req) |
| 389 | { |
| 390 | if (!(req->rq_flags & RQF_DONTPREP)) { |
| 391 | nvme_req(req)->retries = 0; |
| 392 | nvme_req(req)->flags = 0; |
| 393 | req->rq_flags |= RQF_DONTPREP; |
| 394 | } |
| 395 | } |
| 396 | |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 397 | struct request *nvme_alloc_request(struct request_queue *q, |
Bart Van Assche | 9a95e4e | 2017-11-09 10:49:59 -0800 | [diff] [blame] | 398 | struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid) |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 399 | { |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 400 | 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] | 401 | struct request *req; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 402 | |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 403 | if (qid == NVME_QID_ANY) { |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 404 | req = blk_mq_alloc_request(q, op, flags); |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 405 | } else { |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 406 | req = blk_mq_alloc_request_hctx(q, op, flags, |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 407 | qid ? qid - 1 : 0); |
| 408 | } |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 409 | if (IS_ERR(req)) |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 410 | return req; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 411 | |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 412 | req->cmd_flags |= REQ_FAILFAST_DRIVER; |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 413 | nvme_clear_nvme_request(req); |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 414 | nvme_req(req)->cmd = cmd; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 415 | |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 416 | return req; |
| 417 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 418 | EXPORT_SYMBOL_GPL(nvme_alloc_request); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 419 | |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 420 | static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable) |
| 421 | { |
| 422 | struct nvme_command c; |
| 423 | |
| 424 | memset(&c, 0, sizeof(c)); |
| 425 | |
| 426 | c.directive.opcode = nvme_admin_directive_send; |
Arnav Dawn | 62346ea | 2017-07-12 16:11:53 +0530 | [diff] [blame] | 427 | c.directive.nsid = cpu_to_le32(NVME_NSID_ALL); |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 428 | c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE; |
| 429 | c.directive.dtype = NVME_DIR_IDENTIFY; |
| 430 | c.directive.tdtype = NVME_DIR_STREAMS; |
| 431 | c.directive.endir = enable ? NVME_DIR_ENDIR : 0; |
| 432 | |
| 433 | return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0); |
| 434 | } |
| 435 | |
| 436 | static int nvme_disable_streams(struct nvme_ctrl *ctrl) |
| 437 | { |
| 438 | return nvme_toggle_streams(ctrl, false); |
| 439 | } |
| 440 | |
| 441 | static int nvme_enable_streams(struct nvme_ctrl *ctrl) |
| 442 | { |
| 443 | return nvme_toggle_streams(ctrl, true); |
| 444 | } |
| 445 | |
| 446 | static int nvme_get_stream_params(struct nvme_ctrl *ctrl, |
| 447 | struct streams_directive_params *s, u32 nsid) |
| 448 | { |
| 449 | struct nvme_command c; |
| 450 | |
| 451 | memset(&c, 0, sizeof(c)); |
| 452 | memset(s, 0, sizeof(*s)); |
| 453 | |
| 454 | c.directive.opcode = nvme_admin_directive_recv; |
| 455 | c.directive.nsid = cpu_to_le32(nsid); |
Kwan (Hingkwan) Huen-SSI | a082b42 | 2017-08-09 11:26:29 -0700 | [diff] [blame] | 456 | c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1); |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 457 | c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM; |
| 458 | c.directive.dtype = NVME_DIR_STREAMS; |
| 459 | |
| 460 | return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s)); |
| 461 | } |
| 462 | |
| 463 | static int nvme_configure_directives(struct nvme_ctrl *ctrl) |
| 464 | { |
| 465 | struct streams_directive_params s; |
| 466 | int ret; |
| 467 | |
| 468 | if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES)) |
| 469 | return 0; |
| 470 | if (!streams) |
| 471 | return 0; |
| 472 | |
| 473 | ret = nvme_enable_streams(ctrl); |
| 474 | if (ret) |
| 475 | return ret; |
| 476 | |
Arnav Dawn | 62346ea | 2017-07-12 16:11:53 +0530 | [diff] [blame] | 477 | ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL); |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 478 | if (ret) |
| 479 | return ret; |
| 480 | |
| 481 | ctrl->nssa = le16_to_cpu(s.nssa); |
| 482 | if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) { |
| 483 | dev_info(ctrl->device, "too few streams (%u) available\n", |
| 484 | ctrl->nssa); |
| 485 | nvme_disable_streams(ctrl); |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1); |
| 490 | dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams); |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Check if 'req' has a write hint associated with it. If it does, assign |
| 496 | * a valid namespace stream to the write. |
| 497 | */ |
| 498 | static void nvme_assign_write_stream(struct nvme_ctrl *ctrl, |
| 499 | struct request *req, u16 *control, |
| 500 | u32 *dsmgmt) |
| 501 | { |
| 502 | enum rw_hint streamid = req->write_hint; |
| 503 | |
| 504 | if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE) |
| 505 | streamid = 0; |
| 506 | else { |
| 507 | streamid--; |
| 508 | if (WARN_ON_ONCE(streamid > ctrl->nr_streams)) |
| 509 | return; |
| 510 | |
| 511 | *control |= NVME_RW_DTYPE_STREAMS; |
| 512 | *dsmgmt |= streamid << 16; |
| 513 | } |
| 514 | |
| 515 | if (streamid < ARRAY_SIZE(req->q->write_hints)) |
| 516 | req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9; |
| 517 | } |
| 518 | |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 519 | static inline void nvme_setup_flush(struct nvme_ns *ns, |
| 520 | struct nvme_command *cmnd) |
| 521 | { |
| 522 | memset(cmnd, 0, sizeof(*cmnd)); |
| 523 | cmnd->common.opcode = nvme_cmd_flush; |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 524 | cmnd->common.nsid = cpu_to_le32(ns->head->ns_id); |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 525 | } |
| 526 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 527 | 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] | 528 | struct nvme_command *cmnd) |
| 529 | { |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 530 | unsigned short segments = blk_rq_nr_discard_segments(req), n = 0; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 531 | struct nvme_dsm_range *range; |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 532 | struct bio *bio; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 533 | |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 534 | range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC); |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 535 | if (!range) |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 536 | return BLK_STS_RESOURCE; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 537 | |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 538 | __rq_for_each_bio(bio, req) { |
| 539 | u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector); |
| 540 | u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift; |
| 541 | |
Keith Busch | 8cb6af7 | 2018-01-31 17:01:58 -0700 | [diff] [blame] | 542 | if (n < segments) { |
| 543 | range[n].cattr = cpu_to_le32(0); |
| 544 | range[n].nlb = cpu_to_le32(nlb); |
| 545 | range[n].slba = cpu_to_le64(slba); |
| 546 | } |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 547 | n++; |
| 548 | } |
| 549 | |
| 550 | if (WARN_ON_ONCE(n != segments)) { |
| 551 | kfree(range); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 552 | return BLK_STS_IOERR; |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 553 | } |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 554 | |
| 555 | memset(cmnd, 0, sizeof(*cmnd)); |
| 556 | cmnd->dsm.opcode = nvme_cmd_dsm; |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 557 | cmnd->dsm.nsid = cpu_to_le32(ns->head->ns_id); |
Christoph Hellwig | f1dd03a | 2017-03-31 17:00:05 +0200 | [diff] [blame] | 558 | cmnd->dsm.nr = cpu_to_le32(segments - 1); |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 559 | cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD); |
| 560 | |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 561 | req->special_vec.bv_page = virt_to_page(range); |
| 562 | req->special_vec.bv_offset = offset_in_page(range); |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 563 | req->special_vec.bv_len = sizeof(*range) * segments; |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 564 | req->rq_flags |= RQF_SPECIAL_PAYLOAD; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 565 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 566 | return BLK_STS_OK; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 567 | } |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 568 | |
Christoph Hellwig | ebe6d87 | 2017-06-12 18:36:32 +0200 | [diff] [blame] | 569 | static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns, |
| 570 | struct request *req, struct nvme_command *cmnd) |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 571 | { |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 572 | struct nvme_ctrl *ctrl = ns->ctrl; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 573 | u16 control = 0; |
| 574 | u32 dsmgmt = 0; |
| 575 | |
| 576 | if (req->cmd_flags & REQ_FUA) |
| 577 | control |= NVME_RW_FUA; |
| 578 | if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD)) |
| 579 | control |= NVME_RW_LR; |
| 580 | |
| 581 | if (req->cmd_flags & REQ_RAHEAD) |
| 582 | dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; |
| 583 | |
| 584 | memset(cmnd, 0, sizeof(*cmnd)); |
| 585 | cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 586 | cmnd->rw.nsid = cpu_to_le32(ns->head->ns_id); |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 587 | cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req))); |
| 588 | cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1); |
| 589 | |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 590 | if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams) |
| 591 | nvme_assign_write_stream(ctrl, req, &control, &dsmgmt); |
| 592 | |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 593 | if (ns->ms) { |
Christoph Hellwig | 715ea9e | 2017-11-07 17:27:34 +0100 | [diff] [blame] | 594 | /* |
| 595 | * If formated with metadata, the block layer always provides a |
| 596 | * metadata buffer if CONFIG_BLK_DEV_INTEGRITY is enabled. Else |
| 597 | * we enable the PRACT bit for protection information or set the |
| 598 | * namespace capacity to zero to prevent any I/O. |
| 599 | */ |
| 600 | if (!blk_integrity_rq(req)) { |
| 601 | if (WARN_ON_ONCE(!nvme_ns_has_pi(ns))) |
| 602 | return BLK_STS_NOTSUPP; |
| 603 | control |= NVME_RW_PRINFO_PRACT; |
| 604 | } |
| 605 | |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 606 | switch (ns->pi_type) { |
| 607 | case NVME_NS_DPS_PI_TYPE3: |
| 608 | control |= NVME_RW_PRINFO_PRCHK_GUARD; |
| 609 | break; |
| 610 | case NVME_NS_DPS_PI_TYPE1: |
| 611 | case NVME_NS_DPS_PI_TYPE2: |
| 612 | control |= NVME_RW_PRINFO_PRCHK_GUARD | |
| 613 | NVME_RW_PRINFO_PRCHK_REF; |
| 614 | cmnd->rw.reftag = cpu_to_le32( |
| 615 | nvme_block_nr(ns, blk_rq_pos(req))); |
| 616 | break; |
| 617 | } |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | cmnd->rw.control = cpu_to_le16(control); |
| 621 | cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); |
Christoph Hellwig | ebe6d87 | 2017-06-12 18:36:32 +0200 | [diff] [blame] | 622 | return 0; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 623 | } |
| 624 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 625 | 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] | 626 | struct nvme_command *cmd) |
| 627 | { |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 628 | blk_status_t ret = BLK_STS_OK; |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 629 | |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 630 | nvme_clear_nvme_request(req); |
Christoph Hellwig | 987f699 | 2017-04-05 19:18:08 +0200 | [diff] [blame] | 631 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 632 | switch (req_op(req)) { |
| 633 | case REQ_OP_DRV_IN: |
| 634 | case REQ_OP_DRV_OUT: |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 635 | memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd)); |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 636 | break; |
| 637 | case REQ_OP_FLUSH: |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 638 | nvme_setup_flush(ns, cmd); |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 639 | break; |
Christoph Hellwig | e850fd1 | 2017-04-05 19:21:13 +0200 | [diff] [blame] | 640 | case REQ_OP_WRITE_ZEROES: |
| 641 | /* currently only aliased to deallocate for a few ctrls: */ |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 642 | case REQ_OP_DISCARD: |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 643 | ret = nvme_setup_discard(ns, req, cmd); |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 644 | break; |
| 645 | case REQ_OP_READ: |
| 646 | case REQ_OP_WRITE: |
Christoph Hellwig | ebe6d87 | 2017-06-12 18:36:32 +0200 | [diff] [blame] | 647 | ret = nvme_setup_rw(ns, req, cmd); |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 648 | break; |
| 649 | default: |
| 650 | WARN_ON_ONCE(1); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 651 | return BLK_STS_IOERR; |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 652 | } |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 653 | |
James Smart | 721b391 | 2016-10-21 23:33:34 +0300 | [diff] [blame] | 654 | cmd->common.command_id = req->tag; |
Johannes Thumshirn | 3d030e4 | 2018-01-26 11:21:37 +0100 | [diff] [blame] | 655 | if (ns) |
| 656 | trace_nvme_setup_nvm_cmd(req->q->id, cmd); |
| 657 | else |
| 658 | trace_nvme_setup_admin_cmd(cmd); |
Ming Lin | 8093f7c | 2016-04-12 13:10:14 -0600 | [diff] [blame] | 659 | return ret; |
| 660 | } |
| 661 | EXPORT_SYMBOL_GPL(nvme_setup_cmd); |
| 662 | |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 663 | /* |
| 664 | * Returns 0 on success. If the result is negative, it's a Linux error code; |
| 665 | * if the result is positive, it's an NVM Express status code |
| 666 | */ |
| 667 | 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] | 668 | union nvme_result *result, void *buffer, unsigned bufflen, |
Bart Van Assche | 9a95e4e | 2017-11-09 10:49:59 -0800 | [diff] [blame] | 669 | unsigned timeout, int qid, int at_head, |
| 670 | blk_mq_req_flags_t flags) |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 671 | { |
| 672 | struct request *req; |
| 673 | int ret; |
| 674 | |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 675 | req = nvme_alloc_request(q, cmd, flags, qid); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 676 | if (IS_ERR(req)) |
| 677 | return PTR_ERR(req); |
| 678 | |
| 679 | req->timeout = timeout ? timeout : ADMIN_TIMEOUT; |
| 680 | |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 681 | if (buffer && bufflen) { |
| 682 | ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL); |
| 683 | if (ret) |
| 684 | goto out; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 685 | } |
| 686 | |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 687 | blk_execute_rq(req->q, NULL, req, at_head); |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 688 | if (result) |
| 689 | *result = nvme_req(req)->result; |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 690 | if (nvme_req(req)->flags & NVME_REQ_CANCELLED) |
| 691 | ret = -EINTR; |
| 692 | else |
| 693 | ret = nvme_req(req)->status; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 694 | out: |
| 695 | blk_mq_free_request(req); |
| 696 | return ret; |
| 697 | } |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 698 | EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 699 | |
| 700 | int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, |
| 701 | void *buffer, unsigned bufflen) |
| 702 | { |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 703 | return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0, |
| 704 | NVME_QID_ANY, 0, 0); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 705 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 706 | EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 707 | |
Christoph Hellwig | 1cad656 | 2017-08-29 17:46:01 -0400 | [diff] [blame] | 708 | static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf, |
| 709 | unsigned len, u32 seed, bool write) |
| 710 | { |
| 711 | struct bio_integrity_payload *bip; |
| 712 | int ret = -ENOMEM; |
| 713 | void *buf; |
| 714 | |
| 715 | buf = kmalloc(len, GFP_KERNEL); |
| 716 | if (!buf) |
| 717 | goto out; |
| 718 | |
| 719 | ret = -EFAULT; |
| 720 | if (write && copy_from_user(buf, ubuf, len)) |
| 721 | goto out_free_meta; |
| 722 | |
| 723 | bip = bio_integrity_alloc(bio, GFP_KERNEL, 1); |
| 724 | if (IS_ERR(bip)) { |
| 725 | ret = PTR_ERR(bip); |
| 726 | goto out_free_meta; |
| 727 | } |
| 728 | |
| 729 | bip->bip_iter.bi_size = len; |
| 730 | bip->bip_iter.bi_sector = seed; |
| 731 | ret = bio_integrity_add_page(bio, virt_to_page(buf), len, |
| 732 | offset_in_page(buf)); |
| 733 | if (ret == len) |
| 734 | return buf; |
| 735 | ret = -ENOMEM; |
| 736 | out_free_meta: |
| 737 | kfree(buf); |
| 738 | out: |
| 739 | return ERR_PTR(ret); |
| 740 | } |
| 741 | |
Keith Busch | 63263d6 | 2017-08-29 17:46:04 -0400 | [diff] [blame] | 742 | static int nvme_submit_user_cmd(struct request_queue *q, |
Keith Busch | 485783c | 2017-08-29 17:46:03 -0400 | [diff] [blame] | 743 | struct nvme_command *cmd, void __user *ubuffer, |
| 744 | unsigned bufflen, void __user *meta_buffer, unsigned meta_len, |
| 745 | u32 meta_seed, u32 *result, unsigned timeout) |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 746 | { |
Christoph Hellwig | 7a5abb4 | 2016-06-06 23:20:49 +0200 | [diff] [blame] | 747 | bool write = nvme_is_write(cmd); |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 748 | struct nvme_ns *ns = q->queuedata; |
| 749 | struct gendisk *disk = ns ? ns->disk : NULL; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 750 | struct request *req; |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 751 | struct bio *bio = NULL; |
| 752 | void *meta = NULL; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 753 | int ret; |
| 754 | |
Christoph Hellwig | eb71f43 | 2016-06-13 16:45:23 +0200 | [diff] [blame] | 755 | req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY); |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 756 | if (IS_ERR(req)) |
| 757 | return PTR_ERR(req); |
| 758 | |
| 759 | req->timeout = timeout ? timeout : ADMIN_TIMEOUT; |
James Smart | bb06ec31 | 2018-04-12 09:16:15 -0600 | [diff] [blame] | 760 | nvme_req(req)->flags |= NVME_REQ_USERCMD; |
Christoph Hellwig | 4160982 | 2015-11-20 09:00:02 +0100 | [diff] [blame] | 761 | |
| 762 | if (ubuffer && bufflen) { |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 763 | ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen, |
| 764 | GFP_KERNEL); |
| 765 | if (ret) |
| 766 | goto out; |
| 767 | bio = req->bio; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 768 | bio->bi_disk = disk; |
Christoph Hellwig | 1cad656 | 2017-08-29 17:46:01 -0400 | [diff] [blame] | 769 | if (disk && meta_buffer && meta_len) { |
| 770 | meta = nvme_add_user_metadata(bio, meta_buffer, meta_len, |
| 771 | meta_seed, write); |
| 772 | if (IS_ERR(meta)) { |
| 773 | ret = PTR_ERR(meta); |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 774 | goto out_unmap; |
| 775 | } |
Keith Busch | f31a211 | 2018-04-17 14:42:44 -0600 | [diff] [blame] | 776 | req->cmd_flags |= REQ_INTEGRITY; |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 777 | } |
| 778 | } |
Christoph Hellwig | 1cad656 | 2017-08-29 17:46:01 -0400 | [diff] [blame] | 779 | |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 780 | blk_execute_rq(req->q, disk, req, 0); |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 781 | if (nvme_req(req)->flags & NVME_REQ_CANCELLED) |
| 782 | ret = -EINTR; |
| 783 | else |
| 784 | ret = nvme_req(req)->status; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 785 | if (result) |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 786 | *result = le32_to_cpu(nvme_req(req)->result.u32); |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 787 | if (meta && !ret && !write) { |
| 788 | if (copy_to_user(meta_buffer, meta, meta_len)) |
| 789 | ret = -EFAULT; |
| 790 | } |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 791 | kfree(meta); |
| 792 | out_unmap: |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 793 | if (bio) |
Keith Busch | 0b7f1f2 | 2015-10-23 09:47:28 -0600 | [diff] [blame] | 794 | blk_rq_unmap_user(bio); |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 795 | out: |
| 796 | blk_mq_free_request(req); |
| 797 | return ret; |
| 798 | } |
| 799 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 800 | 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] | 801 | { |
| 802 | struct nvme_ctrl *ctrl = rq->end_io_data; |
| 803 | |
| 804 | blk_mq_free_request(rq); |
| 805 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 806 | if (status) { |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 807 | dev_err(ctrl->device, |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 808 | "failed nvme_keep_alive_end_io error=%d\n", |
| 809 | status); |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 810 | return; |
| 811 | } |
| 812 | |
| 813 | schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ); |
| 814 | } |
| 815 | |
| 816 | static int nvme_keep_alive(struct nvme_ctrl *ctrl) |
| 817 | { |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 818 | struct request *rq; |
| 819 | |
Roland Dreier | 0a34e46 | 2018-01-11 13:38:15 -0800 | [diff] [blame] | 820 | rq = nvme_alloc_request(ctrl->admin_q, &ctrl->ka_cmd, BLK_MQ_REQ_RESERVED, |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 821 | NVME_QID_ANY); |
| 822 | if (IS_ERR(rq)) |
| 823 | return PTR_ERR(rq); |
| 824 | |
| 825 | rq->timeout = ctrl->kato * HZ; |
| 826 | rq->end_io_data = ctrl; |
| 827 | |
| 828 | blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io); |
| 829 | |
| 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | static void nvme_keep_alive_work(struct work_struct *work) |
| 834 | { |
| 835 | struct nvme_ctrl *ctrl = container_of(to_delayed_work(work), |
| 836 | struct nvme_ctrl, ka_work); |
| 837 | |
| 838 | if (nvme_keep_alive(ctrl)) { |
| 839 | /* allocation failure, reset the controller */ |
| 840 | dev_err(ctrl->device, "keep-alive failed\n"); |
Christoph Hellwig | 39bdc59 | 2017-06-12 18:21:19 +0200 | [diff] [blame] | 841 | nvme_reset_ctrl(ctrl); |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 842 | return; |
| 843 | } |
| 844 | } |
| 845 | |
Johannes Thumshirn | 00b683d | 2018-04-12 09:16:05 -0600 | [diff] [blame] | 846 | static void nvme_start_keep_alive(struct nvme_ctrl *ctrl) |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 847 | { |
| 848 | if (unlikely(ctrl->kato == 0)) |
| 849 | return; |
| 850 | |
| 851 | INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work); |
Roland Dreier | 0a34e46 | 2018-01-11 13:38:15 -0800 | [diff] [blame] | 852 | memset(&ctrl->ka_cmd, 0, sizeof(ctrl->ka_cmd)); |
| 853 | ctrl->ka_cmd.common.opcode = nvme_admin_keep_alive; |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 854 | schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ); |
| 855 | } |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 856 | |
| 857 | void nvme_stop_keep_alive(struct nvme_ctrl *ctrl) |
| 858 | { |
| 859 | if (unlikely(ctrl->kato == 0)) |
| 860 | return; |
| 861 | |
| 862 | cancel_delayed_work_sync(&ctrl->ka_work); |
| 863 | } |
| 864 | EXPORT_SYMBOL_GPL(nvme_stop_keep_alive); |
| 865 | |
Keith Busch | 3f7f25a9 | 2017-06-20 15:09:56 -0400 | [diff] [blame] | 866 | static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id) |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 867 | { |
| 868 | struct nvme_command c = { }; |
| 869 | int error; |
| 870 | |
| 871 | /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ |
| 872 | c.identify.opcode = nvme_admin_identify; |
Parav Pandit | 986994a | 2017-01-26 17:17:28 +0200 | [diff] [blame] | 873 | c.identify.cns = NVME_ID_CNS_CTRL; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 874 | |
| 875 | *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL); |
| 876 | if (!*id) |
| 877 | return -ENOMEM; |
| 878 | |
| 879 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, |
| 880 | sizeof(struct nvme_id_ctrl)); |
| 881 | if (error) |
| 882 | kfree(*id); |
| 883 | return error; |
| 884 | } |
| 885 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 886 | static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid, |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 887 | struct nvme_ns_ids *ids) |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 888 | { |
| 889 | struct nvme_command c = { }; |
| 890 | int status; |
| 891 | void *data; |
| 892 | int pos; |
| 893 | int len; |
| 894 | |
| 895 | c.identify.opcode = nvme_admin_identify; |
| 896 | c.identify.nsid = cpu_to_le32(nsid); |
| 897 | c.identify.cns = NVME_ID_CNS_NS_DESC_LIST; |
| 898 | |
| 899 | data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL); |
| 900 | if (!data) |
| 901 | return -ENOMEM; |
| 902 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 903 | status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data, |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 904 | NVME_IDENTIFY_DATA_SIZE); |
| 905 | if (status) |
| 906 | goto free_data; |
| 907 | |
| 908 | for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) { |
| 909 | struct nvme_ns_id_desc *cur = data + pos; |
| 910 | |
| 911 | if (cur->nidl == 0) |
| 912 | break; |
| 913 | |
| 914 | switch (cur->nidt) { |
| 915 | case NVME_NIDT_EUI64: |
| 916 | if (cur->nidl != NVME_NIDT_EUI64_LEN) { |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 917 | dev_warn(ctrl->device, |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 918 | "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n", |
| 919 | cur->nidl); |
| 920 | goto free_data; |
| 921 | } |
| 922 | len = NVME_NIDT_EUI64_LEN; |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 923 | memcpy(ids->eui64, data + pos + sizeof(*cur), len); |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 924 | break; |
| 925 | case NVME_NIDT_NGUID: |
| 926 | if (cur->nidl != NVME_NIDT_NGUID_LEN) { |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 927 | dev_warn(ctrl->device, |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 928 | "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n", |
| 929 | cur->nidl); |
| 930 | goto free_data; |
| 931 | } |
| 932 | len = NVME_NIDT_NGUID_LEN; |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 933 | memcpy(ids->nguid, data + pos + sizeof(*cur), len); |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 934 | break; |
| 935 | case NVME_NIDT_UUID: |
| 936 | if (cur->nidl != NVME_NIDT_UUID_LEN) { |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 937 | dev_warn(ctrl->device, |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 938 | "ctrl returned bogus length: %d for NVME_NIDT_UUID\n", |
| 939 | cur->nidl); |
| 940 | goto free_data; |
| 941 | } |
| 942 | len = NVME_NIDT_UUID_LEN; |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 943 | uuid_copy(&ids->uuid, data + pos + sizeof(*cur)); |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 944 | break; |
| 945 | default: |
| 946 | /* Skip unnkown types */ |
| 947 | len = cur->nidl; |
| 948 | break; |
| 949 | } |
| 950 | |
| 951 | len += sizeof(*cur); |
| 952 | } |
| 953 | free_data: |
| 954 | kfree(data); |
| 955 | return status; |
| 956 | } |
| 957 | |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 958 | static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list) |
| 959 | { |
| 960 | struct nvme_command c = { }; |
| 961 | |
| 962 | c.identify.opcode = nvme_admin_identify; |
Parav Pandit | 986994a | 2017-01-26 17:17:28 +0200 | [diff] [blame] | 963 | c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST; |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 964 | c.identify.nsid = cpu_to_le32(nsid); |
Minwoo Im | 42595eb | 2018-02-08 22:56:31 +0900 | [diff] [blame] | 965 | return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, |
| 966 | NVME_IDENTIFY_DATA_SIZE); |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 967 | } |
| 968 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 969 | static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl, |
| 970 | unsigned nsid) |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 971 | { |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 972 | struct nvme_id_ns *id; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 973 | struct nvme_command c = { }; |
| 974 | int error; |
| 975 | |
| 976 | /* 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] | 977 | c.identify.opcode = nvme_admin_identify; |
| 978 | c.identify.nsid = cpu_to_le32(nsid); |
Parav Pandit | 986994a | 2017-01-26 17:17:28 +0200 | [diff] [blame] | 979 | c.identify.cns = NVME_ID_CNS_NS; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 980 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 981 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
| 982 | if (!id) |
| 983 | return NULL; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 984 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 985 | error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id)); |
| 986 | if (error) { |
| 987 | dev_warn(ctrl->device, "Identify namespace failed\n"); |
| 988 | kfree(id); |
| 989 | return NULL; |
| 990 | } |
| 991 | |
| 992 | return id; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 993 | } |
| 994 | |
Keith Busch | 3f7f25a9 | 2017-06-20 15:09:56 -0400 | [diff] [blame] | 995 | static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11, |
Andy Lutomirski | 1a6fe74 | 2016-09-16 11:16:10 -0700 | [diff] [blame] | 996 | void *buffer, size_t buflen, u32 *result) |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 997 | { |
| 998 | struct nvme_command c; |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 999 | union nvme_result res; |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame] | 1000 | int ret; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 1001 | |
| 1002 | memset(&c, 0, sizeof(c)); |
| 1003 | c.features.opcode = nvme_admin_set_features; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 1004 | c.features.fid = cpu_to_le32(fid); |
| 1005 | c.features.dword11 = cpu_to_le32(dword11); |
| 1006 | |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 1007 | ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, |
Andy Lutomirski | 1a6fe74 | 2016-09-16 11:16:10 -0700 | [diff] [blame] | 1008 | buffer, buflen, 0, NVME_QID_ANY, 0, 0); |
Andy Lutomirski | 9b47f77a | 2016-08-24 03:52:12 -0700 | [diff] [blame] | 1009 | if (ret >= 0 && result) |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 1010 | *result = le32_to_cpu(res.u32); |
Christoph Hellwig | 1cb3cce | 2016-02-29 15:59:47 +0100 | [diff] [blame] | 1011 | return ret; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 1012 | } |
| 1013 | |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 1014 | int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count) |
| 1015 | { |
| 1016 | u32 q_count = (*count - 1) | ((*count - 1) << 16); |
| 1017 | u32 result; |
| 1018 | int status, nr_io_queues; |
| 1019 | |
Andy Lutomirski | 1a6fe74 | 2016-09-16 11:16:10 -0700 | [diff] [blame] | 1020 | 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] | 1021 | &result); |
Christoph Hellwig | f5fa90d | 2016-06-06 23:20:50 +0200 | [diff] [blame] | 1022 | if (status < 0) |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 1023 | return status; |
| 1024 | |
Christoph Hellwig | f5fa90d | 2016-06-06 23:20:50 +0200 | [diff] [blame] | 1025 | /* |
| 1026 | * Degraded controllers might return an error when setting the queue |
| 1027 | * count. We still want to be able to bring them online and offer |
| 1028 | * access to the admin queue, as that might be only way to fix them up. |
| 1029 | */ |
| 1030 | if (status > 0) { |
Johannes Thumshirn | f0425db | 2017-06-09 16:17:21 +0200 | [diff] [blame] | 1031 | dev_err(ctrl->device, "Could not set queue count (%d)\n", status); |
Christoph Hellwig | f5fa90d | 2016-06-06 23:20:50 +0200 | [diff] [blame] | 1032 | *count = 0; |
| 1033 | } else { |
| 1034 | nr_io_queues = min(result & 0xffff, result >> 16) + 1; |
| 1035 | *count = min(*count, nr_io_queues); |
| 1036 | } |
| 1037 | |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 1038 | return 0; |
| 1039 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1040 | EXPORT_SYMBOL_GPL(nvme_set_queue_count); |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 1041 | |
Hannes Reinecke | c0561f8 | 2018-05-22 11:09:55 +0200 | [diff] [blame] | 1042 | #define NVME_AEN_SUPPORTED \ |
| 1043 | (NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_FW_ACT) |
| 1044 | |
| 1045 | static void nvme_enable_aen(struct nvme_ctrl *ctrl) |
| 1046 | { |
| 1047 | u32 result; |
| 1048 | int status; |
| 1049 | |
| 1050 | status = nvme_set_features(ctrl, NVME_FEAT_ASYNC_EVENT, |
| 1051 | ctrl->oaes & NVME_AEN_SUPPORTED, NULL, 0, &result); |
| 1052 | if (status) |
| 1053 | dev_warn(ctrl->device, "Failed to configure AEN (cfg %x)\n", |
| 1054 | ctrl->oaes & NVME_AEN_SUPPORTED); |
| 1055 | } |
| 1056 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1057 | static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) |
| 1058 | { |
| 1059 | struct nvme_user_io io; |
| 1060 | struct nvme_command c; |
| 1061 | unsigned length, meta_len; |
| 1062 | void __user *metadata; |
| 1063 | |
| 1064 | if (copy_from_user(&io, uio, sizeof(io))) |
| 1065 | return -EFAULT; |
Keith Busch | 63088ec | 2016-02-24 09:15:57 -0700 | [diff] [blame] | 1066 | if (io.flags) |
| 1067 | return -EINVAL; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1068 | |
| 1069 | switch (io.opcode) { |
| 1070 | case nvme_cmd_write: |
| 1071 | case nvme_cmd_read: |
| 1072 | case nvme_cmd_compare: |
| 1073 | break; |
| 1074 | default: |
| 1075 | return -EINVAL; |
| 1076 | } |
| 1077 | |
| 1078 | length = (io.nblocks + 1) << ns->lba_shift; |
| 1079 | meta_len = (io.nblocks + 1) * ns->ms; |
| 1080 | metadata = (void __user *)(uintptr_t)io.metadata; |
| 1081 | |
| 1082 | if (ns->ext) { |
| 1083 | length += meta_len; |
| 1084 | meta_len = 0; |
| 1085 | } else if (meta_len) { |
| 1086 | if ((io.metadata & 3) || !io.metadata) |
| 1087 | return -EINVAL; |
| 1088 | } |
| 1089 | |
| 1090 | memset(&c, 0, sizeof(c)); |
| 1091 | c.rw.opcode = io.opcode; |
| 1092 | c.rw.flags = io.flags; |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 1093 | c.rw.nsid = cpu_to_le32(ns->head->ns_id); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1094 | c.rw.slba = cpu_to_le64(io.slba); |
| 1095 | c.rw.length = cpu_to_le16(io.nblocks); |
| 1096 | c.rw.control = cpu_to_le16(io.control); |
| 1097 | c.rw.dsmgmt = cpu_to_le32(io.dsmgmt); |
| 1098 | c.rw.reftag = cpu_to_le32(io.reftag); |
| 1099 | c.rw.apptag = cpu_to_le16(io.apptag); |
| 1100 | c.rw.appmask = cpu_to_le16(io.appmask); |
| 1101 | |
Keith Busch | 63263d6 | 2017-08-29 17:46:04 -0400 | [diff] [blame] | 1102 | return nvme_submit_user_cmd(ns->queue, &c, |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1103 | (void __user *)(uintptr_t)io.addr, length, |
| 1104 | metadata, meta_len, io.slba, NULL, 0); |
| 1105 | } |
| 1106 | |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 1107 | static u32 nvme_known_admin_effects(u8 opcode) |
| 1108 | { |
| 1109 | switch (opcode) { |
| 1110 | case nvme_admin_format_nvm: |
| 1111 | return NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC | |
| 1112 | NVME_CMD_EFFECTS_CSE_MASK; |
| 1113 | case nvme_admin_sanitize_nvm: |
| 1114 | return NVME_CMD_EFFECTS_CSE_MASK; |
| 1115 | default: |
| 1116 | break; |
| 1117 | } |
| 1118 | return 0; |
| 1119 | } |
| 1120 | |
| 1121 | static u32 nvme_passthru_start(struct nvme_ctrl *ctrl, struct nvme_ns *ns, |
| 1122 | u8 opcode) |
| 1123 | { |
| 1124 | u32 effects = 0; |
| 1125 | |
| 1126 | if (ns) { |
| 1127 | if (ctrl->effects) |
| 1128 | effects = le32_to_cpu(ctrl->effects->iocs[opcode]); |
| 1129 | if (effects & ~NVME_CMD_EFFECTS_CSUPP) |
| 1130 | dev_warn(ctrl->device, |
| 1131 | "IO command:%02x has unhandled effects:%08x\n", |
| 1132 | opcode, effects); |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
| 1136 | if (ctrl->effects) |
Keith Busch | 62843c2 | 2018-04-12 09:16:14 -0600 | [diff] [blame] | 1137 | effects = le32_to_cpu(ctrl->effects->acs[opcode]); |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 1138 | else |
| 1139 | effects = nvme_known_admin_effects(opcode); |
| 1140 | |
| 1141 | /* |
| 1142 | * For simplicity, IO to all namespaces is quiesced even if the command |
| 1143 | * effects say only one namespace is affected. |
| 1144 | */ |
| 1145 | if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) { |
| 1146 | nvme_start_freeze(ctrl); |
| 1147 | nvme_wait_freeze(ctrl); |
| 1148 | } |
| 1149 | return effects; |
| 1150 | } |
| 1151 | |
| 1152 | static void nvme_update_formats(struct nvme_ctrl *ctrl) |
| 1153 | { |
Jianchao Wang | 3fd176b | 2018-02-12 20:54:45 +0800 | [diff] [blame] | 1154 | struct nvme_ns *ns, *next; |
| 1155 | LIST_HEAD(rm_list); |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 1156 | |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 1157 | down_write(&ctrl->namespaces_rwsem); |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 1158 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
Jianchao Wang | 3fd176b | 2018-02-12 20:54:45 +0800 | [diff] [blame] | 1159 | if (ns->disk && nvme_revalidate_disk(ns->disk)) { |
| 1160 | list_move_tail(&ns->list, &rm_list); |
| 1161 | } |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 1162 | } |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 1163 | up_write(&ctrl->namespaces_rwsem); |
Jianchao Wang | 3fd176b | 2018-02-12 20:54:45 +0800 | [diff] [blame] | 1164 | |
| 1165 | list_for_each_entry_safe(ns, next, &rm_list, list) |
| 1166 | nvme_ns_remove(ns); |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | static void nvme_passthru_end(struct nvme_ctrl *ctrl, u32 effects) |
| 1170 | { |
| 1171 | /* |
| 1172 | * Revalidate LBA changes prior to unfreezing. This is necessary to |
| 1173 | * prevent memory corruption if a logical block size was changed by |
| 1174 | * this command. |
| 1175 | */ |
| 1176 | if (effects & NVME_CMD_EFFECTS_LBCC) |
| 1177 | nvme_update_formats(ctrl); |
| 1178 | if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) |
| 1179 | nvme_unfreeze(ctrl); |
| 1180 | if (effects & NVME_CMD_EFFECTS_CCC) |
| 1181 | nvme_init_identify(ctrl); |
| 1182 | if (effects & (NVME_CMD_EFFECTS_NIC | NVME_CMD_EFFECTS_NCC)) |
| 1183 | nvme_queue_scan(ctrl); |
| 1184 | } |
| 1185 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 1186 | 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] | 1187 | struct nvme_passthru_cmd __user *ucmd) |
| 1188 | { |
| 1189 | struct nvme_passthru_cmd cmd; |
| 1190 | struct nvme_command c; |
| 1191 | unsigned timeout = 0; |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 1192 | u32 effects; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1193 | int status; |
| 1194 | |
| 1195 | if (!capable(CAP_SYS_ADMIN)) |
| 1196 | return -EACCES; |
| 1197 | if (copy_from_user(&cmd, ucmd, sizeof(cmd))) |
| 1198 | return -EFAULT; |
Keith Busch | 63088ec | 2016-02-24 09:15:57 -0700 | [diff] [blame] | 1199 | if (cmd.flags) |
| 1200 | return -EINVAL; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1201 | |
| 1202 | memset(&c, 0, sizeof(c)); |
| 1203 | c.common.opcode = cmd.opcode; |
| 1204 | c.common.flags = cmd.flags; |
| 1205 | c.common.nsid = cpu_to_le32(cmd.nsid); |
| 1206 | c.common.cdw2[0] = cpu_to_le32(cmd.cdw2); |
| 1207 | c.common.cdw2[1] = cpu_to_le32(cmd.cdw3); |
| 1208 | c.common.cdw10[0] = cpu_to_le32(cmd.cdw10); |
| 1209 | c.common.cdw10[1] = cpu_to_le32(cmd.cdw11); |
| 1210 | c.common.cdw10[2] = cpu_to_le32(cmd.cdw12); |
| 1211 | c.common.cdw10[3] = cpu_to_le32(cmd.cdw13); |
| 1212 | c.common.cdw10[4] = cpu_to_le32(cmd.cdw14); |
| 1213 | c.common.cdw10[5] = cpu_to_le32(cmd.cdw15); |
| 1214 | |
| 1215 | if (cmd.timeout_ms) |
| 1216 | timeout = msecs_to_jiffies(cmd.timeout_ms); |
| 1217 | |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 1218 | effects = nvme_passthru_start(ctrl, ns, cmd.opcode); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1219 | status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c, |
Arnd Bergmann | d1ea7be | 2015-12-08 16:22:17 +0100 | [diff] [blame] | 1220 | (void __user *)(uintptr_t)cmd.addr, cmd.data_len, |
Keith Busch | 63263d6 | 2017-08-29 17:46:04 -0400 | [diff] [blame] | 1221 | (void __user *)(uintptr_t)cmd.metadata, cmd.metadata, |
| 1222 | 0, &cmd.result, timeout); |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 1223 | nvme_passthru_end(ctrl, effects); |
| 1224 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1225 | if (status >= 0) { |
| 1226 | if (put_user(cmd.result, &ucmd->result)) |
| 1227 | return -EFAULT; |
| 1228 | } |
| 1229 | |
| 1230 | return status; |
| 1231 | } |
| 1232 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 1233 | /* |
| 1234 | * Issue ioctl requests on the first available path. Note that unlike normal |
| 1235 | * block layer requests we will not retry failed request on another controller. |
| 1236 | */ |
| 1237 | static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk, |
| 1238 | struct nvme_ns_head **head, int *srcu_idx) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1239 | { |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 1240 | #ifdef CONFIG_NVME_MULTIPATH |
| 1241 | if (disk->fops == &nvme_ns_head_ops) { |
| 1242 | *head = disk->private_data; |
| 1243 | *srcu_idx = srcu_read_lock(&(*head)->srcu); |
| 1244 | return nvme_find_path(*head); |
| 1245 | } |
| 1246 | #endif |
| 1247 | *head = NULL; |
| 1248 | *srcu_idx = -1; |
| 1249 | return disk->private_data; |
| 1250 | } |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1251 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 1252 | static void nvme_put_ns_from_disk(struct nvme_ns_head *head, int idx) |
| 1253 | { |
| 1254 | if (head) |
| 1255 | srcu_read_unlock(&head->srcu, idx); |
| 1256 | } |
| 1257 | |
| 1258 | static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned cmd, unsigned long arg) |
| 1259 | { |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1260 | switch (cmd) { |
| 1261 | case NVME_IOCTL_ID: |
| 1262 | force_successful_syscall_return(); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 1263 | return ns->head->ns_id; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1264 | case NVME_IOCTL_ADMIN_CMD: |
| 1265 | return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg); |
| 1266 | case NVME_IOCTL_IO_CMD: |
| 1267 | return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg); |
| 1268 | case NVME_IOCTL_SUBMIT_IO: |
| 1269 | return nvme_submit_io(ns, (void __user *)arg); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1270 | default: |
Matias Bjørling | 84d4add | 2017-01-31 13:17:16 +0100 | [diff] [blame] | 1271 | #ifdef CONFIG_NVM |
| 1272 | if (ns->ndev) |
| 1273 | return nvme_nvm_ioctl(ns, cmd, arg); |
| 1274 | #endif |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1275 | if (is_sed_ioctl(cmd)) |
Christoph Hellwig | 4f1244c | 2017-02-17 13:59:39 +0100 | [diff] [blame] | 1276 | return sed_ioctl(ns->ctrl->opal_dev, cmd, |
Scott Bauer | e225c20 | 2017-02-14 17:29:36 -0700 | [diff] [blame] | 1277 | (void __user *) arg); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1278 | return -ENOTTY; |
| 1279 | } |
| 1280 | } |
| 1281 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 1282 | static int nvme_ioctl(struct block_device *bdev, fmode_t mode, |
| 1283 | unsigned int cmd, unsigned long arg) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1284 | { |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 1285 | struct nvme_ns_head *head = NULL; |
| 1286 | struct nvme_ns *ns; |
| 1287 | int srcu_idx, ret; |
| 1288 | |
| 1289 | ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx); |
| 1290 | if (unlikely(!ns)) |
| 1291 | ret = -EWOULDBLOCK; |
| 1292 | else |
| 1293 | ret = nvme_ns_ioctl(ns, cmd, arg); |
| 1294 | nvme_put_ns_from_disk(head, srcu_idx); |
| 1295 | return ret; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1296 | } |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1297 | |
| 1298 | static int nvme_open(struct block_device *bdev, fmode_t mode) |
| 1299 | { |
Christoph Hellwig | c6424a9 | 2017-10-18 13:22:00 +0200 | [diff] [blame] | 1300 | struct nvme_ns *ns = bdev->bd_disk->private_data; |
| 1301 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 1302 | #ifdef CONFIG_NVME_MULTIPATH |
| 1303 | /* should never be called due to GENHD_FL_HIDDEN */ |
| 1304 | if (WARN_ON_ONCE(ns->head->disk)) |
Nitzan Carmi | 85088c4 | 2018-01-04 17:56:13 +0200 | [diff] [blame] | 1305 | goto fail; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 1306 | #endif |
Christoph Hellwig | c6424a9 | 2017-10-18 13:22:00 +0200 | [diff] [blame] | 1307 | if (!kref_get_unless_zero(&ns->kref)) |
Nitzan Carmi | 85088c4 | 2018-01-04 17:56:13 +0200 | [diff] [blame] | 1308 | goto fail; |
| 1309 | if (!try_module_get(ns->ctrl->ops->module)) |
| 1310 | goto fail_put_ns; |
| 1311 | |
Christoph Hellwig | c6424a9 | 2017-10-18 13:22:00 +0200 | [diff] [blame] | 1312 | return 0; |
Nitzan Carmi | 85088c4 | 2018-01-04 17:56:13 +0200 | [diff] [blame] | 1313 | |
| 1314 | fail_put_ns: |
| 1315 | nvme_put_ns(ns); |
| 1316 | fail: |
| 1317 | return -ENXIO; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | static void nvme_release(struct gendisk *disk, fmode_t mode) |
| 1321 | { |
Nitzan Carmi | 85088c4 | 2018-01-04 17:56:13 +0200 | [diff] [blame] | 1322 | struct nvme_ns *ns = disk->private_data; |
| 1323 | |
| 1324 | module_put(ns->ctrl->ops->module); |
| 1325 | nvme_put_ns(ns); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 1329 | { |
| 1330 | /* some standard values */ |
| 1331 | geo->heads = 1 << 6; |
| 1332 | geo->sectors = 1 << 5; |
| 1333 | geo->cylinders = get_capacity(bdev->bd_disk) >> 11; |
| 1334 | return 0; |
| 1335 | } |
| 1336 | |
| 1337 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
Christoph Hellwig | 39b7baa | 2017-11-02 21:28:53 +0300 | [diff] [blame] | 1338 | static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1339 | { |
| 1340 | struct blk_integrity integrity; |
| 1341 | |
Jay Freyensee | fa9a89f | 2016-07-20 21:26:16 -0600 | [diff] [blame] | 1342 | memset(&integrity, 0, sizeof(integrity)); |
Christoph Hellwig | 39b7baa | 2017-11-02 21:28:53 +0300 | [diff] [blame] | 1343 | switch (pi_type) { |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1344 | case NVME_NS_DPS_PI_TYPE3: |
| 1345 | integrity.profile = &t10_pi_type3_crc; |
Nicholas Bellinger | ba36c21 | 2016-04-09 03:04:42 +0000 | [diff] [blame] | 1346 | integrity.tag_size = sizeof(u16) + sizeof(u32); |
| 1347 | integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1348 | break; |
| 1349 | case NVME_NS_DPS_PI_TYPE1: |
| 1350 | case NVME_NS_DPS_PI_TYPE2: |
| 1351 | integrity.profile = &t10_pi_type1_crc; |
Nicholas Bellinger | ba36c21 | 2016-04-09 03:04:42 +0000 | [diff] [blame] | 1352 | integrity.tag_size = sizeof(u16); |
| 1353 | integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1354 | break; |
| 1355 | default: |
| 1356 | integrity.profile = NULL; |
| 1357 | break; |
| 1358 | } |
Christoph Hellwig | 39b7baa | 2017-11-02 21:28:53 +0300 | [diff] [blame] | 1359 | integrity.tuple_size = ms; |
| 1360 | blk_integrity_register(disk, &integrity); |
| 1361 | blk_queue_max_integrity_segments(disk->queue, 1); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1362 | } |
| 1363 | #else |
Christoph Hellwig | 39b7baa | 2017-11-02 21:28:53 +0300 | [diff] [blame] | 1364 | static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1365 | { |
| 1366 | } |
| 1367 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ |
| 1368 | |
Scott Bauer | 6b8190d | 2017-06-15 10:44:30 -0600 | [diff] [blame] | 1369 | static void nvme_set_chunk_size(struct nvme_ns *ns) |
| 1370 | { |
| 1371 | u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9)); |
| 1372 | blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size)); |
| 1373 | } |
| 1374 | |
Jens Axboe | 3831761 | 2018-05-02 11:06:54 -0600 | [diff] [blame] | 1375 | static void nvme_config_discard(struct nvme_ns *ns) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1376 | { |
Jens Axboe | 3831761 | 2018-05-02 11:06:54 -0600 | [diff] [blame] | 1377 | struct nvme_ctrl *ctrl = ns->ctrl; |
| 1378 | struct request_queue *queue = ns->queue; |
Christoph Hellwig | 30e5e92 | 2017-11-02 21:28:54 +0300 | [diff] [blame] | 1379 | u32 size = queue_logical_block_size(queue); |
| 1380 | |
Jens Axboe | 3831761 | 2018-05-02 11:06:54 -0600 | [diff] [blame] | 1381 | if (!(ctrl->oncs & NVME_CTRL_ONCS_DSM)) { |
| 1382 | blk_queue_flag_clear(QUEUE_FLAG_DISCARD, queue); |
| 1383 | return; |
| 1384 | } |
| 1385 | |
| 1386 | if (ctrl->nr_streams && ns->sws && ns->sgs) |
| 1387 | size *= ns->sws * ns->sgs; |
Keith Busch | 08095e7 | 2016-03-04 13:15:17 -0700 | [diff] [blame] | 1388 | |
Christoph Hellwig | b35ba01 | 2017-02-08 14:46:50 +0100 | [diff] [blame] | 1389 | BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) < |
| 1390 | NVME_DSM_MAX_RANGES); |
| 1391 | |
David Disseldorp | b224f61 | 2017-11-24 16:30:53 +0100 | [diff] [blame] | 1392 | queue->limits.discard_alignment = 0; |
Christoph Hellwig | 30e5e92 | 2017-11-02 21:28:54 +0300 | [diff] [blame] | 1393 | queue->limits.discard_granularity = size; |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 1394 | |
Jens Axboe | 3831761 | 2018-05-02 11:06:54 -0600 | [diff] [blame] | 1395 | /* If discard is already enabled, don't reset queue limits */ |
| 1396 | if (blk_queue_flag_test_and_set(QUEUE_FLAG_DISCARD, queue)) |
| 1397 | return; |
| 1398 | |
Christoph Hellwig | 30e5e92 | 2017-11-02 21:28:54 +0300 | [diff] [blame] | 1399 | blk_queue_max_discard_sectors(queue, UINT_MAX); |
| 1400 | blk_queue_max_discard_segments(queue, NVME_DSM_MAX_RANGES); |
Christoph Hellwig | e850fd1 | 2017-04-05 19:21:13 +0200 | [diff] [blame] | 1401 | |
| 1402 | if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES) |
Christoph Hellwig | 30e5e92 | 2017-11-02 21:28:54 +0300 | [diff] [blame] | 1403 | blk_queue_max_write_zeroes_sectors(queue, UINT_MAX); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1404 | } |
| 1405 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1406 | static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid, |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 1407 | struct nvme_id_ns *id, struct nvme_ns_ids *ids) |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1408 | { |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 1409 | memset(ids, 0, sizeof(*ids)); |
| 1410 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1411 | if (ctrl->vs >= NVME_VS(1, 1, 0)) |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 1412 | memcpy(ids->eui64, id->eui64, sizeof(id->eui64)); |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1413 | if (ctrl->vs >= NVME_VS(1, 2, 0)) |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 1414 | memcpy(ids->nguid, id->nguid, sizeof(id->nguid)); |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1415 | if (ctrl->vs >= NVME_VS(1, 3, 0)) { |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 1416 | /* Don't treat error as fatal we potentially |
| 1417 | * already have a NGUID or EUI-64 |
| 1418 | */ |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 1419 | if (nvme_identify_ns_descs(ctrl, nsid, ids)) |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1420 | dev_warn(ctrl->device, |
Johannes Thumshirn | 3b22ba2 | 2017-06-07 11:45:34 +0200 | [diff] [blame] | 1421 | "%s: Identify Descriptors failed\n", __func__); |
| 1422 | } |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1423 | } |
| 1424 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 1425 | static bool nvme_ns_ids_valid(struct nvme_ns_ids *ids) |
| 1426 | { |
| 1427 | return !uuid_is_null(&ids->uuid) || |
| 1428 | memchr_inv(ids->nguid, 0, sizeof(ids->nguid)) || |
| 1429 | memchr_inv(ids->eui64, 0, sizeof(ids->eui64)); |
| 1430 | } |
| 1431 | |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 1432 | static bool nvme_ns_ids_equal(struct nvme_ns_ids *a, struct nvme_ns_ids *b) |
| 1433 | { |
| 1434 | return uuid_equal(&a->uuid, &b->uuid) && |
| 1435 | memcmp(&a->nguid, &b->nguid, sizeof(a->nguid)) == 0 && |
| 1436 | memcmp(&a->eui64, &b->eui64, sizeof(a->eui64)) == 0; |
| 1437 | } |
| 1438 | |
Christoph Hellwig | 24b0b58 | 2017-11-02 21:28:56 +0300 | [diff] [blame] | 1439 | static void nvme_update_disk_info(struct gendisk *disk, |
| 1440 | struct nvme_ns *ns, struct nvme_id_ns *id) |
| 1441 | { |
| 1442 | sector_t capacity = le64_to_cpup(&id->nsze) << (ns->lba_shift - 9); |
Jeff Lien | cee160f | 2017-12-19 13:24:15 -0600 | [diff] [blame] | 1443 | unsigned short bs = 1 << ns->lba_shift; |
Christoph Hellwig | 24b0b58 | 2017-11-02 21:28:56 +0300 | [diff] [blame] | 1444 | |
| 1445 | blk_mq_freeze_queue(disk->queue); |
| 1446 | blk_integrity_unregister(disk); |
| 1447 | |
Jeff Lien | cee160f | 2017-12-19 13:24:15 -0600 | [diff] [blame] | 1448 | blk_queue_logical_block_size(disk->queue, bs); |
| 1449 | blk_queue_physical_block_size(disk->queue, bs); |
| 1450 | blk_queue_io_min(disk->queue, bs); |
| 1451 | |
Christoph Hellwig | 24b0b58 | 2017-11-02 21:28:56 +0300 | [diff] [blame] | 1452 | if (ns->ms && !ns->ext && |
| 1453 | (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED)) |
| 1454 | nvme_init_integrity(disk, ns->ms, ns->pi_type); |
Christoph Hellwig | 715ea9e | 2017-11-07 17:27:34 +0100 | [diff] [blame] | 1455 | if (ns->ms && !nvme_ns_has_pi(ns) && !blk_get_integrity(disk)) |
Christoph Hellwig | 24b0b58 | 2017-11-02 21:28:56 +0300 | [diff] [blame] | 1456 | capacity = 0; |
Christoph Hellwig | 24b0b58 | 2017-11-02 21:28:56 +0300 | [diff] [blame] | 1457 | |
Jens Axboe | 3831761 | 2018-05-02 11:06:54 -0600 | [diff] [blame] | 1458 | set_capacity(disk, capacity); |
| 1459 | nvme_config_discard(ns); |
Christoph Hellwig | 24b0b58 | 2017-11-02 21:28:56 +0300 | [diff] [blame] | 1460 | blk_mq_unfreeze_queue(disk->queue); |
| 1461 | } |
| 1462 | |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1463 | static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id) |
| 1464 | { |
| 1465 | struct nvme_ns *ns = disk->private_data; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1466 | |
| 1467 | /* |
| 1468 | * If identify namespace failed, use default 512 byte block size so |
| 1469 | * block layer can use before failing read/write for 0 capacity. |
| 1470 | */ |
Christoph Hellwig | c81bfba | 2017-05-20 15:14:45 +0200 | [diff] [blame] | 1471 | 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] | 1472 | if (ns->lba_shift == 0) |
| 1473 | ns->lba_shift = 9; |
Scott Bauer | 6b8190d | 2017-06-15 10:44:30 -0600 | [diff] [blame] | 1474 | ns->noiob = le16_to_cpu(id->noiob); |
Christoph Hellwig | b5be3b3 | 2017-11-02 21:28:52 +0300 | [diff] [blame] | 1475 | ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms); |
Max Gurtovoy | c97f414 | 2018-05-27 18:50:10 +0300 | [diff] [blame] | 1476 | ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT); |
Christoph Hellwig | b5be3b3 | 2017-11-02 21:28:52 +0300 | [diff] [blame] | 1477 | /* the PI implementation requires metadata equal t10 pi tuple size */ |
| 1478 | if (ns->ms == sizeof(struct t10_pi_tuple)) |
| 1479 | ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK; |
| 1480 | else |
| 1481 | ns->pi_type = 0; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1482 | |
Scott Bauer | 6b8190d | 2017-06-15 10:44:30 -0600 | [diff] [blame] | 1483 | if (ns->noiob) |
| 1484 | nvme_set_chunk_size(ns); |
Christoph Hellwig | 24b0b58 | 2017-11-02 21:28:56 +0300 | [diff] [blame] | 1485 | nvme_update_disk_info(disk, ns, id); |
Matias Bjørling | 96257a8 | 2018-03-30 00:05:05 +0200 | [diff] [blame] | 1486 | if (ns->ndev) |
| 1487 | nvme_nvm_update_nvm_info(ns); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 1488 | #ifdef CONFIG_NVME_MULTIPATH |
| 1489 | if (ns->head->disk) |
| 1490 | nvme_update_disk_info(ns->head->disk, ns, id); |
| 1491 | #endif |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1492 | } |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1493 | |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1494 | static int nvme_revalidate_disk(struct gendisk *disk) |
| 1495 | { |
| 1496 | struct nvme_ns *ns = disk->private_data; |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1497 | struct nvme_ctrl *ctrl = ns->ctrl; |
| 1498 | struct nvme_id_ns *id; |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 1499 | struct nvme_ns_ids ids; |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1500 | int ret = 0; |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1501 | |
| 1502 | if (test_bit(NVME_NS_DEAD, &ns->flags)) { |
| 1503 | set_capacity(disk, 0); |
| 1504 | return -ENODEV; |
| 1505 | } |
| 1506 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 1507 | id = nvme_identify_ns(ctrl, ns->head->ns_id); |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1508 | if (!id) |
| 1509 | return -ENODEV; |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 1510 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1511 | if (id->ncap == 0) { |
| 1512 | ret = -ENODEV; |
| 1513 | goto out; |
| 1514 | } |
| 1515 | |
Keith Busch | 5e0fab5 | 2017-10-27 13:51:22 -0600 | [diff] [blame] | 1516 | __nvme_revalidate_disk(disk, id); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 1517 | nvme_report_ns_ids(ctrl, ns->head->ns_id, id, &ids); |
| 1518 | if (!nvme_ns_ids_equal(&ns->head->ids, &ids)) { |
Christoph Hellwig | 1d5df6a | 2017-08-17 14:10:00 +0200 | [diff] [blame] | 1519 | dev_err(ctrl->device, |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 1520 | "identifiers changed for nsid %d\n", ns->head->ns_id); |
Christoph Hellwig | 1d5df6a | 2017-08-17 14:10:00 +0200 | [diff] [blame] | 1521 | ret = -ENODEV; |
| 1522 | } |
| 1523 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1524 | out: |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1525 | kfree(id); |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 1526 | return ret; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | static char nvme_pr_type(enum pr_type type) |
| 1530 | { |
| 1531 | switch (type) { |
| 1532 | case PR_WRITE_EXCLUSIVE: |
| 1533 | return 1; |
| 1534 | case PR_EXCLUSIVE_ACCESS: |
| 1535 | return 2; |
| 1536 | case PR_WRITE_EXCLUSIVE_REG_ONLY: |
| 1537 | return 3; |
| 1538 | case PR_EXCLUSIVE_ACCESS_REG_ONLY: |
| 1539 | return 4; |
| 1540 | case PR_WRITE_EXCLUSIVE_ALL_REGS: |
| 1541 | return 5; |
| 1542 | case PR_EXCLUSIVE_ACCESS_ALL_REGS: |
| 1543 | return 6; |
| 1544 | default: |
| 1545 | return 0; |
| 1546 | } |
| 1547 | }; |
| 1548 | |
| 1549 | static int nvme_pr_command(struct block_device *bdev, u32 cdw10, |
| 1550 | u64 key, u64 sa_key, u8 op) |
| 1551 | { |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 1552 | struct nvme_ns_head *head = NULL; |
| 1553 | struct nvme_ns *ns; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1554 | struct nvme_command c; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 1555 | int srcu_idx, ret; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1556 | u8 data[16] = { 0, }; |
| 1557 | |
Keith Busch | b0d61d5 | 2017-11-16 13:36:49 -0700 | [diff] [blame] | 1558 | ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx); |
| 1559 | if (unlikely(!ns)) |
| 1560 | return -EWOULDBLOCK; |
| 1561 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1562 | put_unaligned_le64(key, &data[0]); |
| 1563 | put_unaligned_le64(sa_key, &data[8]); |
| 1564 | |
| 1565 | memset(&c, 0, sizeof(c)); |
| 1566 | c.common.opcode = op; |
Keith Busch | b0d61d5 | 2017-11-16 13:36:49 -0700 | [diff] [blame] | 1567 | c.common.nsid = cpu_to_le32(ns->head->ns_id); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1568 | c.common.cdw10[0] = cpu_to_le32(cdw10); |
| 1569 | |
Keith Busch | b0d61d5 | 2017-11-16 13:36:49 -0700 | [diff] [blame] | 1570 | ret = nvme_submit_sync_cmd(ns->queue, &c, data, 16); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 1571 | nvme_put_ns_from_disk(head, srcu_idx); |
| 1572 | return ret; |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | static int nvme_pr_register(struct block_device *bdev, u64 old, |
| 1576 | u64 new, unsigned flags) |
| 1577 | { |
| 1578 | u32 cdw10; |
| 1579 | |
| 1580 | if (flags & ~PR_FL_IGNORE_KEY) |
| 1581 | return -EOPNOTSUPP; |
| 1582 | |
| 1583 | cdw10 = old ? 2 : 0; |
| 1584 | cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0; |
| 1585 | cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */ |
| 1586 | return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register); |
| 1587 | } |
| 1588 | |
| 1589 | static int nvme_pr_reserve(struct block_device *bdev, u64 key, |
| 1590 | enum pr_type type, unsigned flags) |
| 1591 | { |
| 1592 | u32 cdw10; |
| 1593 | |
| 1594 | if (flags & ~PR_FL_IGNORE_KEY) |
| 1595 | return -EOPNOTSUPP; |
| 1596 | |
| 1597 | cdw10 = nvme_pr_type(type) << 8; |
| 1598 | cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0); |
| 1599 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire); |
| 1600 | } |
| 1601 | |
| 1602 | static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new, |
| 1603 | enum pr_type type, bool abort) |
| 1604 | { |
Ivan Bornyakov | e9a9853 | 2018-05-23 17:56:11 +0300 | [diff] [blame] | 1605 | u32 cdw10 = nvme_pr_type(type) << 8 | (abort ? 2 : 1); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1606 | return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire); |
| 1607 | } |
| 1608 | |
| 1609 | static int nvme_pr_clear(struct block_device *bdev, u64 key) |
| 1610 | { |
Dan Carpenter | 8c0b391 | 2015-12-09 13:24:06 +0300 | [diff] [blame] | 1611 | u32 cdw10 = 1 | (key ? 1 << 3 : 0); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1612 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register); |
| 1613 | } |
| 1614 | |
| 1615 | static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type) |
| 1616 | { |
Ivan Bornyakov | e9a9853 | 2018-05-23 17:56:11 +0300 | [diff] [blame] | 1617 | u32 cdw10 = nvme_pr_type(type) << 8 | (key ? 1 << 3 : 0); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1618 | return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release); |
| 1619 | } |
| 1620 | |
| 1621 | static const struct pr_ops nvme_pr_ops = { |
| 1622 | .pr_register = nvme_pr_register, |
| 1623 | .pr_reserve = nvme_pr_reserve, |
| 1624 | .pr_release = nvme_pr_release, |
| 1625 | .pr_preempt = nvme_pr_preempt, |
| 1626 | .pr_clear = nvme_pr_clear, |
| 1627 | }; |
| 1628 | |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1629 | #ifdef CONFIG_BLK_SED_OPAL |
Christoph Hellwig | 4f1244c | 2017-02-17 13:59:39 +0100 | [diff] [blame] | 1630 | int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len, |
| 1631 | bool send) |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1632 | { |
Christoph Hellwig | 4f1244c | 2017-02-17 13:59:39 +0100 | [diff] [blame] | 1633 | struct nvme_ctrl *ctrl = data; |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1634 | struct nvme_command cmd; |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1635 | |
| 1636 | memset(&cmd, 0, sizeof(cmd)); |
| 1637 | if (send) |
| 1638 | cmd.common.opcode = nvme_admin_security_send; |
| 1639 | else |
| 1640 | cmd.common.opcode = nvme_admin_security_recv; |
Scott Bauer | a98e58e5 | 2017-02-03 12:50:32 -0700 | [diff] [blame] | 1641 | cmd.common.nsid = 0; |
| 1642 | cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8); |
| 1643 | cmd.common.cdw10[1] = cpu_to_le32(len); |
| 1644 | |
| 1645 | return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len, |
| 1646 | ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0); |
| 1647 | } |
| 1648 | EXPORT_SYMBOL_GPL(nvme_sec_submit); |
| 1649 | #endif /* CONFIG_BLK_SED_OPAL */ |
| 1650 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1651 | static const struct block_device_operations nvme_fops = { |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1652 | .owner = THIS_MODULE, |
| 1653 | .ioctl = nvme_ioctl, |
Christoph Hellwig | 761f2e1 | 2017-10-05 18:46:46 +0200 | [diff] [blame] | 1654 | .compat_ioctl = nvme_ioctl, |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 1655 | .open = nvme_open, |
| 1656 | .release = nvme_release, |
| 1657 | .getgeo = nvme_getgeo, |
| 1658 | .revalidate_disk= nvme_revalidate_disk, |
| 1659 | .pr_ops = &nvme_pr_ops, |
| 1660 | }; |
| 1661 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 1662 | #ifdef CONFIG_NVME_MULTIPATH |
| 1663 | static int nvme_ns_head_open(struct block_device *bdev, fmode_t mode) |
| 1664 | { |
| 1665 | struct nvme_ns_head *head = bdev->bd_disk->private_data; |
| 1666 | |
| 1667 | if (!kref_get_unless_zero(&head->ref)) |
| 1668 | return -ENXIO; |
| 1669 | return 0; |
| 1670 | } |
| 1671 | |
| 1672 | static void nvme_ns_head_release(struct gendisk *disk, fmode_t mode) |
| 1673 | { |
| 1674 | nvme_put_ns_head(disk->private_data); |
| 1675 | } |
| 1676 | |
| 1677 | const struct block_device_operations nvme_ns_head_ops = { |
| 1678 | .owner = THIS_MODULE, |
| 1679 | .open = nvme_ns_head_open, |
| 1680 | .release = nvme_ns_head_release, |
| 1681 | .ioctl = nvme_ioctl, |
| 1682 | .compat_ioctl = nvme_ioctl, |
| 1683 | .getgeo = nvme_getgeo, |
| 1684 | .pr_ops = &nvme_pr_ops, |
| 1685 | }; |
| 1686 | #endif /* CONFIG_NVME_MULTIPATH */ |
| 1687 | |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1688 | static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled) |
| 1689 | { |
| 1690 | unsigned long timeout = |
| 1691 | ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies; |
| 1692 | u32 csts, bit = enabled ? NVME_CSTS_RDY : 0; |
| 1693 | int ret; |
| 1694 | |
| 1695 | while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { |
Keith Busch | 0df1e4f | 2016-10-11 13:31:58 -0400 | [diff] [blame] | 1696 | if (csts == ~0) |
| 1697 | return -ENODEV; |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1698 | if ((csts & NVME_CSTS_RDY) == bit) |
| 1699 | break; |
| 1700 | |
| 1701 | msleep(100); |
| 1702 | if (fatal_signal_pending(current)) |
| 1703 | return -EINTR; |
| 1704 | if (time_after(jiffies, timeout)) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1705 | dev_err(ctrl->device, |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1706 | "Device not ready; aborting %s\n", enabled ? |
| 1707 | "initialisation" : "reset"); |
| 1708 | return -ENODEV; |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | return ret; |
| 1713 | } |
| 1714 | |
| 1715 | /* |
| 1716 | * If the device has been passed off to us in an enabled state, just clear |
| 1717 | * the enabled bit. The spec says we should set the 'shutdown notification |
| 1718 | * bits', but doing so may cause the device to complete commands to the |
| 1719 | * admin queue ... and we don't know what memory that might be pointing at! |
| 1720 | */ |
| 1721 | int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap) |
| 1722 | { |
| 1723 | int ret; |
| 1724 | |
| 1725 | ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; |
| 1726 | ctrl->ctrl_config &= ~NVME_CC_ENABLE; |
| 1727 | |
| 1728 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); |
| 1729 | if (ret) |
| 1730 | return ret; |
Guilherme G. Piccoli | 54adc01 | 2016-06-14 18:22:41 -0300 | [diff] [blame] | 1731 | |
Guilherme G. Piccoli | b5a10c5 | 2016-12-28 22:13:15 -0200 | [diff] [blame] | 1732 | if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY) |
Guilherme G. Piccoli | 54adc01 | 2016-06-14 18:22:41 -0300 | [diff] [blame] | 1733 | msleep(NVME_QUIRK_DELAY_AMOUNT); |
| 1734 | |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1735 | return nvme_wait_ready(ctrl, cap, false); |
| 1736 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1737 | EXPORT_SYMBOL_GPL(nvme_disable_ctrl); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1738 | |
| 1739 | int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap) |
| 1740 | { |
| 1741 | /* |
| 1742 | * Default to a 4K page size, with the intention to update this |
| 1743 | * path in the future to accomodate architectures with differing |
| 1744 | * kernel and IO page sizes. |
| 1745 | */ |
| 1746 | unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12; |
| 1747 | int ret; |
| 1748 | |
| 1749 | if (page_shift < dev_page_min) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1750 | dev_err(ctrl->device, |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1751 | "Minimum device page size %u too large for host (%u)\n", |
| 1752 | 1 << dev_page_min, 1 << page_shift); |
| 1753 | return -ENODEV; |
| 1754 | } |
| 1755 | |
| 1756 | ctrl->page_size = 1 << page_shift; |
| 1757 | |
| 1758 | ctrl->ctrl_config = NVME_CC_CSS_NVM; |
| 1759 | ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT; |
Max Gurtovoy | 60b43f6 | 2017-08-13 19:21:07 +0300 | [diff] [blame] | 1760 | ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE; |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1761 | ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; |
| 1762 | ctrl->ctrl_config |= NVME_CC_ENABLE; |
| 1763 | |
| 1764 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); |
| 1765 | if (ret) |
| 1766 | return ret; |
| 1767 | return nvme_wait_ready(ctrl, cap, true); |
| 1768 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1769 | EXPORT_SYMBOL_GPL(nvme_enable_ctrl); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1770 | |
| 1771 | int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl) |
| 1772 | { |
Martin K. Petersen | 07fbd32 | 2017-08-25 19:14:50 -0400 | [diff] [blame] | 1773 | unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1774 | u32 csts; |
| 1775 | int ret; |
| 1776 | |
| 1777 | ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; |
| 1778 | ctrl->ctrl_config |= NVME_CC_SHN_NORMAL; |
| 1779 | |
| 1780 | ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); |
| 1781 | if (ret) |
| 1782 | return ret; |
| 1783 | |
| 1784 | while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { |
| 1785 | if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT) |
| 1786 | break; |
| 1787 | |
| 1788 | msleep(100); |
| 1789 | if (fatal_signal_pending(current)) |
| 1790 | return -EINTR; |
| 1791 | if (time_after(jiffies, timeout)) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 1792 | dev_err(ctrl->device, |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1793 | "Device shutdown incomplete; abort shutdown\n"); |
| 1794 | return -ENODEV; |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | return ret; |
| 1799 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 1800 | EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1801 | |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1802 | static void nvme_set_queue_limits(struct nvme_ctrl *ctrl, |
| 1803 | struct request_queue *q) |
| 1804 | { |
Jens Axboe | 7c88cb0 | 2016-04-12 15:43:09 -0600 | [diff] [blame] | 1805 | bool vwc = false; |
| 1806 | |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1807 | if (ctrl->max_hw_sectors) { |
Christoph Hellwig | 45686b6 | 2016-03-02 18:07:12 +0100 | [diff] [blame] | 1808 | u32 max_segments = |
| 1809 | (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1; |
| 1810 | |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1811 | blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors); |
Christoph Hellwig | 45686b6 | 2016-03-02 18:07:12 +0100 | [diff] [blame] | 1812 | blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX)); |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1813 | } |
Keith Busch | 249159c | 2017-12-14 11:20:14 -0700 | [diff] [blame] | 1814 | if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) && |
| 1815 | is_power_of_2(ctrl->max_hw_sectors)) |
Keith Busch | e6282ae | 2016-12-19 11:37:50 -0500 | [diff] [blame] | 1816 | blk_queue_chunk_sectors(q, ctrl->max_hw_sectors); |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1817 | blk_queue_virt_boundary(q, ctrl->page_size - 1); |
Jens Axboe | 7c88cb0 | 2016-04-12 15:43:09 -0600 | [diff] [blame] | 1818 | if (ctrl->vwc & NVME_CTRL_VWC_PRESENT) |
| 1819 | vwc = true; |
| 1820 | blk_queue_write_cache(q, vwc, vwc); |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 1821 | } |
| 1822 | |
Jon Derrick | dbf86b3 | 2017-08-16 09:51:29 +0200 | [diff] [blame] | 1823 | static int nvme_configure_timestamp(struct nvme_ctrl *ctrl) |
| 1824 | { |
| 1825 | __le64 ts; |
| 1826 | int ret; |
| 1827 | |
| 1828 | if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP)) |
| 1829 | return 0; |
| 1830 | |
| 1831 | ts = cpu_to_le64(ktime_to_ms(ktime_get_real())); |
| 1832 | ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts), |
| 1833 | NULL); |
| 1834 | if (ret) |
| 1835 | dev_warn_once(ctrl->device, |
| 1836 | "could not set timestamp (%d)\n", ret); |
| 1837 | return ret; |
| 1838 | } |
| 1839 | |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 1840 | static int nvme_configure_apst(struct nvme_ctrl *ctrl) |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1841 | { |
| 1842 | /* |
| 1843 | * APST (Autonomous Power State Transition) lets us program a |
| 1844 | * table of power state transitions that the controller will |
| 1845 | * perform automatically. We configure it with a simple |
| 1846 | * heuristic: we are willing to spend at most 2% of the time |
| 1847 | * transitioning between power states. Therefore, when running |
| 1848 | * in any given state, we will enter the next lower-power |
Andy Lutomirski | 76e4ad0 | 2017-04-21 16:19:22 -0700 | [diff] [blame] | 1849 | * non-operational state after waiting 50 * (enlat + exlat) |
Kai-Heng Feng | da87591 | 2017-06-07 15:25:42 +0800 | [diff] [blame] | 1850 | * microseconds, as long as that state's exit latency is under |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1851 | * the requested maximum latency. |
| 1852 | * |
| 1853 | * We will not autonomously enter any non-operational state for |
| 1854 | * which the total latency exceeds ps_max_latency_us. Users |
| 1855 | * can set ps_max_latency_us to zero to turn off APST. |
| 1856 | */ |
| 1857 | |
| 1858 | unsigned apste; |
| 1859 | struct nvme_feat_auto_pst *table; |
Andy Lutomirski | fb0dc39 | 2017-04-21 16:19:23 -0700 | [diff] [blame] | 1860 | u64 max_lat_us = 0; |
| 1861 | int max_ps = -1; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1862 | int ret; |
| 1863 | |
| 1864 | /* |
| 1865 | * If APST isn't supported or if we haven't been initialized yet, |
| 1866 | * then don't do anything. |
| 1867 | */ |
| 1868 | if (!ctrl->apsta) |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 1869 | return 0; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1870 | |
| 1871 | if (ctrl->npss > 31) { |
| 1872 | dev_warn(ctrl->device, "NPSS is invalid; not using APST\n"); |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 1873 | return 0; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
| 1877 | if (!table) |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 1878 | return 0; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1879 | |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 1880 | if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) { |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1881 | /* Turn off APST. */ |
| 1882 | apste = 0; |
Andy Lutomirski | fb0dc39 | 2017-04-21 16:19:23 -0700 | [diff] [blame] | 1883 | dev_dbg(ctrl->device, "APST disabled\n"); |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1884 | } else { |
| 1885 | __le64 target = cpu_to_le64(0); |
| 1886 | int state; |
| 1887 | |
| 1888 | /* |
| 1889 | * Walk through all states from lowest- to highest-power. |
| 1890 | * According to the spec, lower-numbered states use more |
| 1891 | * power. NPSS, despite the name, is the index of the |
| 1892 | * lowest-power state, not the number of states. |
| 1893 | */ |
| 1894 | for (state = (int)ctrl->npss; state >= 0; state--) { |
Kai-Heng Feng | da87591 | 2017-06-07 15:25:42 +0800 | [diff] [blame] | 1895 | u64 total_latency_us, exit_latency_us, transition_ms; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1896 | |
| 1897 | if (target) |
| 1898 | table->entries[state] = target; |
| 1899 | |
| 1900 | /* |
Andy Lutomirski | ff5350a | 2017-04-20 13:37:55 -0700 | [diff] [blame] | 1901 | * Don't allow transitions to the deepest state |
| 1902 | * if it's quirked off. |
| 1903 | */ |
| 1904 | if (state == ctrl->npss && |
| 1905 | (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) |
| 1906 | continue; |
| 1907 | |
| 1908 | /* |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1909 | * Is this state a useful non-operational state for |
| 1910 | * higher-power states to autonomously transition to? |
| 1911 | */ |
| 1912 | if (!(ctrl->psd[state].flags & |
| 1913 | NVME_PS_FLAGS_NON_OP_STATE)) |
| 1914 | continue; |
| 1915 | |
Kai-Heng Feng | da87591 | 2017-06-07 15:25:42 +0800 | [diff] [blame] | 1916 | exit_latency_us = |
| 1917 | (u64)le32_to_cpu(ctrl->psd[state].exit_lat); |
| 1918 | if (exit_latency_us > ctrl->ps_max_latency_us) |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1919 | continue; |
| 1920 | |
Kai-Heng Feng | da87591 | 2017-06-07 15:25:42 +0800 | [diff] [blame] | 1921 | total_latency_us = |
| 1922 | exit_latency_us + |
| 1923 | le32_to_cpu(ctrl->psd[state].entry_lat); |
| 1924 | |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1925 | /* |
| 1926 | * This state is good. Use it as the APST idle |
| 1927 | * target for higher power states. |
| 1928 | */ |
| 1929 | transition_ms = total_latency_us + 19; |
| 1930 | do_div(transition_ms, 20); |
| 1931 | if (transition_ms > (1 << 24) - 1) |
| 1932 | transition_ms = (1 << 24) - 1; |
| 1933 | |
| 1934 | target = cpu_to_le64((state << 3) | |
| 1935 | (transition_ms << 8)); |
Andy Lutomirski | fb0dc39 | 2017-04-21 16:19:23 -0700 | [diff] [blame] | 1936 | |
| 1937 | if (max_ps == -1) |
| 1938 | max_ps = state; |
| 1939 | |
| 1940 | if (total_latency_us > max_lat_us) |
| 1941 | max_lat_us = total_latency_us; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | apste = 1; |
Andy Lutomirski | fb0dc39 | 2017-04-21 16:19:23 -0700 | [diff] [blame] | 1945 | |
| 1946 | if (max_ps == -1) { |
| 1947 | dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n"); |
| 1948 | } else { |
| 1949 | dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n", |
| 1950 | max_ps, max_lat_us, (int)sizeof(*table), table); |
| 1951 | } |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste, |
| 1955 | table, sizeof(*table), NULL); |
| 1956 | if (ret) |
| 1957 | dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret); |
| 1958 | |
| 1959 | kfree(table); |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 1960 | return ret; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | static void nvme_set_latency_tolerance(struct device *dev, s32 val) |
| 1964 | { |
| 1965 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 1966 | u64 latency; |
| 1967 | |
| 1968 | switch (val) { |
| 1969 | case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT: |
| 1970 | case PM_QOS_LATENCY_ANY: |
| 1971 | latency = U64_MAX; |
| 1972 | break; |
| 1973 | |
| 1974 | default: |
| 1975 | latency = val; |
| 1976 | } |
| 1977 | |
| 1978 | if (ctrl->ps_max_latency_us != latency) { |
| 1979 | ctrl->ps_max_latency_us = latency; |
| 1980 | nvme_configure_apst(ctrl); |
| 1981 | } |
| 1982 | } |
| 1983 | |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 1984 | struct nvme_core_quirk_entry { |
| 1985 | /* |
| 1986 | * NVMe model and firmware strings are padded with spaces. For |
| 1987 | * simplicity, strings in the quirk table are padded with NULLs |
| 1988 | * instead. |
| 1989 | */ |
| 1990 | u16 vid; |
| 1991 | const char *mn; |
| 1992 | const char *fr; |
| 1993 | unsigned long quirks; |
| 1994 | }; |
| 1995 | |
| 1996 | static const struct nvme_core_quirk_entry core_quirks[] = { |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 1997 | { |
Andy Lutomirski | be56945 | 2017-04-20 13:37:56 -0700 | [diff] [blame] | 1998 | /* |
| 1999 | * This Toshiba device seems to die using any APST states. See: |
| 2000 | * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11 |
| 2001 | */ |
| 2002 | .vid = 0x1179, |
| 2003 | .mn = "THNSF5256GPUK TOSHIBA", |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 2004 | .quirks = NVME_QUIRK_NO_APST, |
Andy Lutomirski | be56945 | 2017-04-20 13:37:56 -0700 | [diff] [blame] | 2005 | } |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 2006 | }; |
| 2007 | |
| 2008 | /* match is null-terminated but idstr is space-padded. */ |
| 2009 | static bool string_matches(const char *idstr, const char *match, size_t len) |
| 2010 | { |
| 2011 | size_t matchlen; |
| 2012 | |
| 2013 | if (!match) |
| 2014 | return true; |
| 2015 | |
| 2016 | matchlen = strlen(match); |
| 2017 | WARN_ON_ONCE(matchlen > len); |
| 2018 | |
| 2019 | if (memcmp(idstr, match, matchlen)) |
| 2020 | return false; |
| 2021 | |
| 2022 | for (; matchlen < len; matchlen++) |
| 2023 | if (idstr[matchlen] != ' ') |
| 2024 | return false; |
| 2025 | |
| 2026 | return true; |
| 2027 | } |
| 2028 | |
| 2029 | static bool quirk_matches(const struct nvme_id_ctrl *id, |
| 2030 | const struct nvme_core_quirk_entry *q) |
| 2031 | { |
| 2032 | return q->vid == le16_to_cpu(id->vid) && |
| 2033 | string_matches(id->mn, q->mn, sizeof(id->mn)) && |
| 2034 | string_matches(id->fr, q->fr, sizeof(id->fr)); |
| 2035 | } |
| 2036 | |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2037 | static void nvme_init_subnqn(struct nvme_subsystem *subsys, struct nvme_ctrl *ctrl, |
| 2038 | struct nvme_id_ctrl *id) |
Christoph Hellwig | 180de007 | 2017-06-26 12:39:02 +0200 | [diff] [blame] | 2039 | { |
| 2040 | size_t nqnlen; |
| 2041 | int off; |
| 2042 | |
| 2043 | nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE); |
| 2044 | if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) { |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2045 | strncpy(subsys->subnqn, id->subnqn, NVMF_NQN_SIZE); |
Christoph Hellwig | 180de007 | 2017-06-26 12:39:02 +0200 | [diff] [blame] | 2046 | return; |
| 2047 | } |
| 2048 | |
| 2049 | if (ctrl->vs >= NVME_VS(1, 2, 1)) |
| 2050 | dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n"); |
| 2051 | |
| 2052 | /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */ |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2053 | off = snprintf(subsys->subnqn, NVMF_NQN_SIZE, |
Christoph Hellwig | 180de007 | 2017-06-26 12:39:02 +0200 | [diff] [blame] | 2054 | "nqn.2014.08.org.nvmexpress:%4x%4x", |
| 2055 | le16_to_cpu(id->vid), le16_to_cpu(id->ssvid)); |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2056 | memcpy(subsys->subnqn + off, id->sn, sizeof(id->sn)); |
Christoph Hellwig | 180de007 | 2017-06-26 12:39:02 +0200 | [diff] [blame] | 2057 | off += sizeof(id->sn); |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2058 | memcpy(subsys->subnqn + off, id->mn, sizeof(id->mn)); |
Christoph Hellwig | 180de007 | 2017-06-26 12:39:02 +0200 | [diff] [blame] | 2059 | off += sizeof(id->mn); |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2060 | memset(subsys->subnqn + off, 0, sizeof(subsys->subnqn) - off); |
| 2061 | } |
| 2062 | |
| 2063 | static void __nvme_release_subsystem(struct nvme_subsystem *subsys) |
| 2064 | { |
| 2065 | ida_simple_remove(&nvme_subsystems_ida, subsys->instance); |
| 2066 | kfree(subsys); |
| 2067 | } |
| 2068 | |
| 2069 | static void nvme_release_subsystem(struct device *dev) |
| 2070 | { |
| 2071 | __nvme_release_subsystem(container_of(dev, struct nvme_subsystem, dev)); |
| 2072 | } |
| 2073 | |
| 2074 | static void nvme_destroy_subsystem(struct kref *ref) |
| 2075 | { |
| 2076 | struct nvme_subsystem *subsys = |
| 2077 | container_of(ref, struct nvme_subsystem, ref); |
| 2078 | |
| 2079 | mutex_lock(&nvme_subsystems_lock); |
| 2080 | list_del(&subsys->entry); |
| 2081 | mutex_unlock(&nvme_subsystems_lock); |
| 2082 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2083 | ida_destroy(&subsys->ns_ida); |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2084 | device_del(&subsys->dev); |
| 2085 | put_device(&subsys->dev); |
| 2086 | } |
| 2087 | |
| 2088 | static void nvme_put_subsystem(struct nvme_subsystem *subsys) |
| 2089 | { |
| 2090 | kref_put(&subsys->ref, nvme_destroy_subsystem); |
| 2091 | } |
| 2092 | |
| 2093 | static struct nvme_subsystem *__nvme_find_get_subsystem(const char *subsysnqn) |
| 2094 | { |
| 2095 | struct nvme_subsystem *subsys; |
| 2096 | |
| 2097 | lockdep_assert_held(&nvme_subsystems_lock); |
| 2098 | |
| 2099 | list_for_each_entry(subsys, &nvme_subsystems, entry) { |
| 2100 | if (strcmp(subsys->subnqn, subsysnqn)) |
| 2101 | continue; |
| 2102 | if (!kref_get_unless_zero(&subsys->ref)) |
| 2103 | continue; |
| 2104 | return subsys; |
| 2105 | } |
| 2106 | |
| 2107 | return NULL; |
| 2108 | } |
| 2109 | |
Hannes Reinecke | 1e49693 | 2017-11-10 10:58:23 +0100 | [diff] [blame] | 2110 | #define SUBSYS_ATTR_RO(_name, _mode, _show) \ |
| 2111 | struct device_attribute subsys_attr_##_name = \ |
| 2112 | __ATTR(_name, _mode, _show, NULL) |
| 2113 | |
| 2114 | static ssize_t nvme_subsys_show_nqn(struct device *dev, |
| 2115 | struct device_attribute *attr, |
| 2116 | char *buf) |
| 2117 | { |
| 2118 | struct nvme_subsystem *subsys = |
| 2119 | container_of(dev, struct nvme_subsystem, dev); |
| 2120 | |
| 2121 | return snprintf(buf, PAGE_SIZE, "%s\n", subsys->subnqn); |
| 2122 | } |
| 2123 | static SUBSYS_ATTR_RO(subsysnqn, S_IRUGO, nvme_subsys_show_nqn); |
| 2124 | |
| 2125 | #define nvme_subsys_show_str_function(field) \ |
| 2126 | static ssize_t subsys_##field##_show(struct device *dev, \ |
| 2127 | struct device_attribute *attr, char *buf) \ |
| 2128 | { \ |
| 2129 | struct nvme_subsystem *subsys = \ |
| 2130 | container_of(dev, struct nvme_subsystem, dev); \ |
| 2131 | return sprintf(buf, "%.*s\n", \ |
| 2132 | (int)sizeof(subsys->field), subsys->field); \ |
| 2133 | } \ |
| 2134 | static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show); |
| 2135 | |
| 2136 | nvme_subsys_show_str_function(model); |
| 2137 | nvme_subsys_show_str_function(serial); |
| 2138 | nvme_subsys_show_str_function(firmware_rev); |
| 2139 | |
| 2140 | static struct attribute *nvme_subsys_attrs[] = { |
| 2141 | &subsys_attr_model.attr, |
| 2142 | &subsys_attr_serial.attr, |
| 2143 | &subsys_attr_firmware_rev.attr, |
| 2144 | &subsys_attr_subsysnqn.attr, |
| 2145 | NULL, |
| 2146 | }; |
| 2147 | |
| 2148 | static struct attribute_group nvme_subsys_attrs_group = { |
| 2149 | .attrs = nvme_subsys_attrs, |
| 2150 | }; |
| 2151 | |
| 2152 | static const struct attribute_group *nvme_subsys_attrs_groups[] = { |
| 2153 | &nvme_subsys_attrs_group, |
| 2154 | NULL, |
| 2155 | }; |
| 2156 | |
Israel Rukshin | b837b28 | 2018-01-04 17:56:14 +0200 | [diff] [blame] | 2157 | static int nvme_active_ctrls(struct nvme_subsystem *subsys) |
| 2158 | { |
| 2159 | int count = 0; |
| 2160 | struct nvme_ctrl *ctrl; |
| 2161 | |
| 2162 | mutex_lock(&subsys->lock); |
| 2163 | list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) { |
| 2164 | if (ctrl->state != NVME_CTRL_DELETING && |
| 2165 | ctrl->state != NVME_CTRL_DEAD) |
| 2166 | count++; |
| 2167 | } |
| 2168 | mutex_unlock(&subsys->lock); |
| 2169 | |
| 2170 | return count; |
| 2171 | } |
| 2172 | |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2173 | static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) |
| 2174 | { |
| 2175 | struct nvme_subsystem *subsys, *found; |
| 2176 | int ret; |
| 2177 | |
| 2178 | subsys = kzalloc(sizeof(*subsys), GFP_KERNEL); |
| 2179 | if (!subsys) |
| 2180 | return -ENOMEM; |
| 2181 | ret = ida_simple_get(&nvme_subsystems_ida, 0, 0, GFP_KERNEL); |
| 2182 | if (ret < 0) { |
| 2183 | kfree(subsys); |
| 2184 | return ret; |
| 2185 | } |
| 2186 | subsys->instance = ret; |
| 2187 | mutex_init(&subsys->lock); |
| 2188 | kref_init(&subsys->ref); |
| 2189 | INIT_LIST_HEAD(&subsys->ctrls); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2190 | INIT_LIST_HEAD(&subsys->nsheads); |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2191 | nvme_init_subnqn(subsys, ctrl, id); |
| 2192 | memcpy(subsys->serial, id->sn, sizeof(subsys->serial)); |
| 2193 | memcpy(subsys->model, id->mn, sizeof(subsys->model)); |
| 2194 | memcpy(subsys->firmware_rev, id->fr, sizeof(subsys->firmware_rev)); |
| 2195 | subsys->vendor_id = le16_to_cpu(id->vid); |
| 2196 | subsys->cmic = id->cmic; |
| 2197 | |
| 2198 | subsys->dev.class = nvme_subsys_class; |
| 2199 | subsys->dev.release = nvme_release_subsystem; |
Hannes Reinecke | 1e49693 | 2017-11-10 10:58:23 +0100 | [diff] [blame] | 2200 | subsys->dev.groups = nvme_subsys_attrs_groups; |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2201 | dev_set_name(&subsys->dev, "nvme-subsys%d", subsys->instance); |
| 2202 | device_initialize(&subsys->dev); |
| 2203 | |
| 2204 | mutex_lock(&nvme_subsystems_lock); |
| 2205 | found = __nvme_find_get_subsystem(subsys->subnqn); |
| 2206 | if (found) { |
| 2207 | /* |
| 2208 | * Verify that the subsystem actually supports multiple |
| 2209 | * controllers, else bail out. |
| 2210 | */ |
Israel Rukshin | 16001c1 | 2018-06-10 10:31:10 +0000 | [diff] [blame] | 2211 | if (!(ctrl->opts && ctrl->opts->discovery_nqn) && |
Hannes Reinecke | 181303d | 2018-05-24 16:18:17 +0200 | [diff] [blame] | 2212 | nvme_active_ctrls(found) && !(id->cmic & (1 << 1))) { |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2213 | dev_err(ctrl->device, |
| 2214 | "ignoring ctrl due to duplicate subnqn (%s).\n", |
| 2215 | found->subnqn); |
| 2216 | nvme_put_subsystem(found); |
| 2217 | ret = -EINVAL; |
| 2218 | goto out_unlock; |
| 2219 | } |
| 2220 | |
| 2221 | __nvme_release_subsystem(subsys); |
| 2222 | subsys = found; |
| 2223 | } else { |
| 2224 | ret = device_add(&subsys->dev); |
| 2225 | if (ret) { |
| 2226 | dev_err(ctrl->device, |
| 2227 | "failed to register subsystem device.\n"); |
| 2228 | goto out_unlock; |
| 2229 | } |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2230 | ida_init(&subsys->ns_ida); |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2231 | list_add_tail(&subsys->entry, &nvme_subsystems); |
| 2232 | } |
| 2233 | |
| 2234 | ctrl->subsys = subsys; |
| 2235 | mutex_unlock(&nvme_subsystems_lock); |
| 2236 | |
| 2237 | if (sysfs_create_link(&subsys->dev.kobj, &ctrl->device->kobj, |
| 2238 | dev_name(ctrl->device))) { |
| 2239 | dev_err(ctrl->device, |
| 2240 | "failed to create sysfs link from subsystem.\n"); |
| 2241 | /* the transport driver will eventually put the subsystem */ |
| 2242 | return -EINVAL; |
| 2243 | } |
| 2244 | |
| 2245 | mutex_lock(&subsys->lock); |
| 2246 | list_add_tail(&ctrl->subsys_entry, &subsys->ctrls); |
| 2247 | mutex_unlock(&subsys->lock); |
| 2248 | |
| 2249 | return 0; |
| 2250 | |
| 2251 | out_unlock: |
| 2252 | mutex_unlock(&nvme_subsystems_lock); |
| 2253 | put_device(&subsys->dev); |
| 2254 | return ret; |
Christoph Hellwig | 180de007 | 2017-06-26 12:39:02 +0200 | [diff] [blame] | 2255 | } |
| 2256 | |
Matias Bjørling | d558fb5 | 2018-03-21 20:27:07 +0100 | [diff] [blame] | 2257 | int nvme_get_log_ext(struct nvme_ctrl *ctrl, struct nvme_ns *ns, |
Javier González | a294c19 | 2018-03-30 00:05:17 +0200 | [diff] [blame] | 2258 | u8 log_page, void *log, |
Matias Bjørling | 7ec6074 | 2018-04-12 09:16:03 -0600 | [diff] [blame] | 2259 | size_t size, u64 offset) |
Matias Bjørling | 70da609 | 2018-02-26 13:55:40 +0100 | [diff] [blame] | 2260 | { |
| 2261 | struct nvme_command c = { }; |
| 2262 | unsigned long dwlen = size / 4 - 1; |
| 2263 | |
| 2264 | c.get_log_page.opcode = nvme_admin_get_log_page; |
| 2265 | |
| 2266 | if (ns) |
| 2267 | c.get_log_page.nsid = cpu_to_le32(ns->head->ns_id); |
| 2268 | else |
| 2269 | c.get_log_page.nsid = cpu_to_le32(NVME_NSID_ALL); |
| 2270 | |
| 2271 | c.get_log_page.lid = log_page; |
| 2272 | c.get_log_page.numdl = cpu_to_le16(dwlen & ((1 << 16) - 1)); |
| 2273 | c.get_log_page.numdu = cpu_to_le16(dwlen >> 16); |
Matias Bjørling | 7ec6074 | 2018-04-12 09:16:03 -0600 | [diff] [blame] | 2274 | c.get_log_page.lpol = cpu_to_le32(lower_32_bits(offset)); |
| 2275 | c.get_log_page.lpou = cpu_to_le32(upper_32_bits(offset)); |
Matias Bjørling | 70da609 | 2018-02-26 13:55:40 +0100 | [diff] [blame] | 2276 | |
| 2277 | return nvme_submit_sync_cmd(ctrl->admin_q, &c, log, size); |
| 2278 | } |
| 2279 | |
Keith Busch | c627c48 | 2017-11-07 10:28:31 -0700 | [diff] [blame] | 2280 | static int nvme_get_log(struct nvme_ctrl *ctrl, u8 log_page, void *log, |
| 2281 | size_t size) |
| 2282 | { |
Matias Bjørling | 70da609 | 2018-02-26 13:55:40 +0100 | [diff] [blame] | 2283 | return nvme_get_log_ext(ctrl, NULL, log_page, log, size, 0); |
Keith Busch | c627c48 | 2017-11-07 10:28:31 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 2286 | static int nvme_get_effects_log(struct nvme_ctrl *ctrl) |
| 2287 | { |
| 2288 | int ret; |
| 2289 | |
| 2290 | if (!ctrl->effects) |
| 2291 | ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL); |
| 2292 | |
| 2293 | if (!ctrl->effects) |
| 2294 | return 0; |
| 2295 | |
| 2296 | ret = nvme_get_log(ctrl, NVME_LOG_CMD_EFFECTS, ctrl->effects, |
| 2297 | sizeof(*ctrl->effects)); |
| 2298 | if (ret) { |
| 2299 | kfree(ctrl->effects); |
| 2300 | ctrl->effects = NULL; |
| 2301 | } |
| 2302 | return ret; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 2303 | } |
| 2304 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2305 | /* |
| 2306 | * Initialize the cached copies of the Identify data and various controller |
| 2307 | * register in our nvme_ctrl structure. This should be called as soon as |
| 2308 | * the admin queue is fully up and running. |
| 2309 | */ |
| 2310 | int nvme_init_identify(struct nvme_ctrl *ctrl) |
| 2311 | { |
| 2312 | struct nvme_id_ctrl *id; |
| 2313 | u64 cap; |
| 2314 | int ret, page_shift; |
Christoph Hellwig | a229dbf | 2016-06-06 23:20:48 +0200 | [diff] [blame] | 2315 | u32 max_hw_sectors; |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 2316 | bool prev_apst_enabled; |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2317 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2318 | ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs); |
| 2319 | if (ret) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 2320 | dev_err(ctrl->device, "Reading VS failed (%d)\n", ret); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2321 | return ret; |
| 2322 | } |
| 2323 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2324 | ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap); |
| 2325 | if (ret) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 2326 | dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2327 | return ret; |
| 2328 | } |
| 2329 | page_shift = NVME_CAP_MPSMIN(cap) + 12; |
| 2330 | |
Gabriel Krisman Bertazi | 8ef2074 | 2016-10-19 09:51:05 -0600 | [diff] [blame] | 2331 | if (ctrl->vs >= NVME_VS(1, 1, 0)) |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2332 | ctrl->subsystem = NVME_CAP_NSSRC(cap); |
| 2333 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2334 | ret = nvme_identify_ctrl(ctrl, &id); |
| 2335 | if (ret) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 2336 | dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2337 | return -EIO; |
| 2338 | } |
| 2339 | |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 2340 | if (id->lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG) { |
| 2341 | ret = nvme_get_effects_log(ctrl); |
| 2342 | if (ret < 0) |
Hannes Reinecke | 75c8b19 | 2018-05-25 11:06:27 +0200 | [diff] [blame] | 2343 | goto out_free; |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 2344 | } |
Christoph Hellwig | 180de007 | 2017-06-26 12:39:02 +0200 | [diff] [blame] | 2345 | |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 2346 | if (!ctrl->identified) { |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2347 | int i; |
| 2348 | |
| 2349 | ret = nvme_init_subsystem(ctrl, id); |
| 2350 | if (ret) |
| 2351 | goto out_free; |
| 2352 | |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 2353 | /* |
| 2354 | * Check for quirks. Quirk can depend on firmware version, |
| 2355 | * so, in principle, the set of quirks present can change |
| 2356 | * across a reset. As a possible future enhancement, we |
| 2357 | * could re-scan for quirks every time we reinitialize |
| 2358 | * the device, but we'd have to make sure that the driver |
| 2359 | * behaves intelligently if the quirks change. |
| 2360 | */ |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 2361 | for (i = 0; i < ARRAY_SIZE(core_quirks); i++) { |
| 2362 | if (quirk_matches(id, &core_quirks[i])) |
| 2363 | ctrl->quirks |= core_quirks[i].quirks; |
| 2364 | } |
| 2365 | } |
| 2366 | |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 2367 | if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) { |
Johannes Thumshirn | f0425db | 2017-06-09 16:17:21 +0200 | [diff] [blame] | 2368 | 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] | 2369 | ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS; |
| 2370 | } |
| 2371 | |
Scott Bauer | 8a9ae52 | 2017-02-17 13:59:40 +0100 | [diff] [blame] | 2372 | ctrl->oacs = le16_to_cpu(id->oacs); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2373 | ctrl->oncs = le16_to_cpup(&id->oncs); |
Hannes Reinecke | c0561f8 | 2018-05-22 11:09:55 +0200 | [diff] [blame] | 2374 | ctrl->oaes = le32_to_cpu(id->oaes); |
Christoph Hellwig | 6bf25d1 | 2015-11-20 09:36:44 +0100 | [diff] [blame] | 2375 | atomic_set(&ctrl->abort_limit, id->acl + 1); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2376 | ctrl->vwc = id->vwc; |
Ming Lin | 931e1c2 | 2016-02-26 13:24:19 -0800 | [diff] [blame] | 2377 | ctrl->cntlid = le16_to_cpup(&id->cntlid); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2378 | if (id->mdts) |
Christoph Hellwig | a229dbf | 2016-06-06 23:20:48 +0200 | [diff] [blame] | 2379 | max_hw_sectors = 1 << (id->mdts + page_shift - 9); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2380 | else |
Christoph Hellwig | a229dbf | 2016-06-06 23:20:48 +0200 | [diff] [blame] | 2381 | max_hw_sectors = UINT_MAX; |
| 2382 | ctrl->max_hw_sectors = |
| 2383 | min_not_zero(ctrl->max_hw_sectors, max_hw_sectors); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2384 | |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 2385 | nvme_set_queue_limits(ctrl, ctrl->admin_q); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 2386 | ctrl->sgls = le32_to_cpu(id->sgls); |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 2387 | ctrl->kas = le16_to_cpu(id->kas); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 2388 | |
Martin K. Petersen | 07fbd32 | 2017-08-25 19:14:50 -0400 | [diff] [blame] | 2389 | if (id->rtd3e) { |
| 2390 | /* us -> s */ |
| 2391 | u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000; |
| 2392 | |
| 2393 | ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time, |
| 2394 | shutdown_timeout, 60); |
| 2395 | |
| 2396 | if (ctrl->shutdown_timeout != shutdown_timeout) |
Max Gurtovoy | 1a3838d | 2017-12-31 15:33:27 +0200 | [diff] [blame] | 2397 | dev_info(ctrl->device, |
Martin K. Petersen | 07fbd32 | 2017-08-25 19:14:50 -0400 | [diff] [blame] | 2398 | "Shutdown timeout set to %u seconds\n", |
| 2399 | ctrl->shutdown_timeout); |
| 2400 | } else |
| 2401 | ctrl->shutdown_timeout = shutdown_timeout; |
| 2402 | |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 2403 | ctrl->npss = id->npss; |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 2404 | ctrl->apsta = id->apsta; |
| 2405 | prev_apst_enabled = ctrl->apst_enabled; |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 2406 | if (ctrl->quirks & NVME_QUIRK_NO_APST) { |
| 2407 | if (force_apst && id->apsta) { |
Johannes Thumshirn | f0425db | 2017-06-09 16:17:21 +0200 | [diff] [blame] | 2408 | dev_warn(ctrl->device, "forcibly allowing APST due to nvme_core.force_apst -- use at your own risk\n"); |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 2409 | ctrl->apst_enabled = true; |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 2410 | } else { |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 2411 | ctrl->apst_enabled = false; |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 2412 | } |
| 2413 | } else { |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 2414 | ctrl->apst_enabled = id->apsta; |
Andy Lutomirski | c35e30b | 2017-04-21 16:19:24 -0700 | [diff] [blame] | 2415 | } |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 2416 | memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd)); |
| 2417 | |
Christoph Hellwig | d3d5b87 | 2017-05-20 15:14:44 +0200 | [diff] [blame] | 2418 | if (ctrl->ops->flags & NVME_F_FABRICS) { |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 2419 | ctrl->icdoff = le16_to_cpu(id->icdoff); |
| 2420 | ctrl->ioccsz = le32_to_cpu(id->ioccsz); |
| 2421 | ctrl->iorcsz = le32_to_cpu(id->iorcsz); |
| 2422 | ctrl->maxcmd = le16_to_cpu(id->maxcmd); |
| 2423 | |
| 2424 | /* |
| 2425 | * In fabrics we need to verify the cntlid matches the |
| 2426 | * admin connect |
| 2427 | */ |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 2428 | if (ctrl->cntlid != le16_to_cpu(id->cntlid)) { |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 2429 | ret = -EINVAL; |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 2430 | goto out_free; |
| 2431 | } |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 2432 | |
| 2433 | if (!ctrl->opts->discovery_nqn && !ctrl->kas) { |
Johannes Thumshirn | f0425db | 2017-06-09 16:17:21 +0200 | [diff] [blame] | 2434 | dev_err(ctrl->device, |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 2435 | "keep-alive support is mandatory for fabrics\n"); |
| 2436 | ret = -EINVAL; |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 2437 | goto out_free; |
Sagi Grimberg | 038bd4c | 2016-06-13 16:45:28 +0200 | [diff] [blame] | 2438 | } |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 2439 | } else { |
| 2440 | ctrl->cntlid = le16_to_cpu(id->cntlid); |
Christoph Hellwig | fe6d53c | 2017-05-12 17:16:10 +0200 | [diff] [blame] | 2441 | ctrl->hmpre = le32_to_cpu(id->hmpre); |
| 2442 | ctrl->hmmin = le32_to_cpu(id->hmmin); |
Christoph Hellwig | 044a9df | 2017-09-11 12:09:28 -0400 | [diff] [blame] | 2443 | ctrl->hmminds = le32_to_cpu(id->hmminds); |
| 2444 | ctrl->hmmaxd = le16_to_cpu(id->hmmaxd); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 2445 | } |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 2446 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2447 | kfree(id); |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 2448 | |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 2449 | if (ctrl->apst_enabled && !prev_apst_enabled) |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 2450 | dev_pm_qos_expose_latency_tolerance(ctrl->device); |
Kai-Heng Feng | 76a5af8 | 2017-06-26 16:39:54 -0400 | [diff] [blame] | 2451 | else if (!ctrl->apst_enabled && prev_apst_enabled) |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 2452 | dev_pm_qos_hide_latency_tolerance(ctrl->device); |
| 2453 | |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 2454 | ret = nvme_configure_apst(ctrl); |
| 2455 | if (ret < 0) |
| 2456 | return ret; |
Jon Derrick | dbf86b3 | 2017-08-16 09:51:29 +0200 | [diff] [blame] | 2457 | |
| 2458 | ret = nvme_configure_timestamp(ctrl); |
| 2459 | if (ret < 0) |
| 2460 | return ret; |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 2461 | |
| 2462 | ret = nvme_configure_directives(ctrl); |
| 2463 | if (ret < 0) |
| 2464 | return ret; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 2465 | |
Andy Lutomirski | bd4da3a | 2017-02-22 13:32:36 -0700 | [diff] [blame] | 2466 | ctrl->identified = true; |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 2467 | |
Keith Busch | 634b832 | 2017-08-10 11:23:31 +0200 | [diff] [blame] | 2468 | return 0; |
| 2469 | |
| 2470 | out_free: |
| 2471 | kfree(id); |
Christoph Hellwig | 07bfcd0 | 2016-06-13 16:45:26 +0200 | [diff] [blame] | 2472 | return ret; |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2473 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 2474 | EXPORT_SYMBOL_GPL(nvme_init_identify); |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2475 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2476 | static int nvme_dev_open(struct inode *inode, struct file *file) |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 2477 | { |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 2478 | struct nvme_ctrl *ctrl = |
| 2479 | container_of(inode->i_cdev, struct nvme_ctrl, cdev); |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 2480 | |
Jianchao Wang | 2b1b7e7 | 2018-01-06 08:01:58 +0800 | [diff] [blame] | 2481 | switch (ctrl->state) { |
| 2482 | case NVME_CTRL_LIVE: |
| 2483 | case NVME_CTRL_ADMIN_ONLY: |
| 2484 | break; |
| 2485 | default: |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 2486 | return -EWOULDBLOCK; |
Jianchao Wang | 2b1b7e7 | 2018-01-06 08:01:58 +0800 | [diff] [blame] | 2487 | } |
| 2488 | |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 2489 | file->private_data = ctrl; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2490 | return 0; |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 2491 | } |
| 2492 | |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 2493 | static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp) |
| 2494 | { |
| 2495 | struct nvme_ns *ns; |
| 2496 | int ret; |
| 2497 | |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 2498 | down_read(&ctrl->namespaces_rwsem); |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 2499 | if (list_empty(&ctrl->namespaces)) { |
| 2500 | ret = -ENOTTY; |
| 2501 | goto out_unlock; |
| 2502 | } |
| 2503 | |
| 2504 | ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list); |
| 2505 | if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) { |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 2506 | dev_warn(ctrl->device, |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 2507 | "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n"); |
| 2508 | ret = -EINVAL; |
| 2509 | goto out_unlock; |
| 2510 | } |
| 2511 | |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 2512 | dev_warn(ctrl->device, |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 2513 | "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n"); |
| 2514 | kref_get(&ns->kref); |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 2515 | up_read(&ctrl->namespaces_rwsem); |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 2516 | |
| 2517 | ret = nvme_user_cmd(ctrl, ns, argp); |
| 2518 | nvme_put_ns(ns); |
| 2519 | return ret; |
| 2520 | |
| 2521 | out_unlock: |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 2522 | up_read(&ctrl->namespaces_rwsem); |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 2523 | return ret; |
| 2524 | } |
| 2525 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2526 | static long nvme_dev_ioctl(struct file *file, unsigned int cmd, |
| 2527 | unsigned long arg) |
| 2528 | { |
| 2529 | struct nvme_ctrl *ctrl = file->private_data; |
| 2530 | void __user *argp = (void __user *)arg; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2531 | |
| 2532 | switch (cmd) { |
| 2533 | case NVME_IOCTL_ADMIN_CMD: |
| 2534 | return nvme_user_cmd(ctrl, NULL, argp); |
| 2535 | case NVME_IOCTL_IO_CMD: |
Christoph Hellwig | bfd8947 | 2015-12-24 15:27:01 +0100 | [diff] [blame] | 2536 | return nvme_dev_user_cmd(ctrl, argp); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2537 | case NVME_IOCTL_RESET: |
Sagi Grimberg | 1b3c47c | 2016-02-10 08:51:15 -0700 | [diff] [blame] | 2538 | dev_warn(ctrl->device, "resetting controller\n"); |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 2539 | return nvme_reset_ctrl_sync(ctrl); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2540 | case NVME_IOCTL_SUBSYS_RESET: |
| 2541 | return nvme_reset_subsystem(ctrl); |
Keith Busch | 9ec3bb2 | 2016-04-29 15:45:18 -0600 | [diff] [blame] | 2542 | case NVME_IOCTL_RESCAN: |
| 2543 | nvme_queue_scan(ctrl); |
| 2544 | return 0; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2545 | default: |
| 2546 | return -ENOTTY; |
| 2547 | } |
| 2548 | } |
| 2549 | |
| 2550 | static const struct file_operations nvme_dev_fops = { |
| 2551 | .owner = THIS_MODULE, |
| 2552 | .open = nvme_dev_open, |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2553 | .unlocked_ioctl = nvme_dev_ioctl, |
| 2554 | .compat_ioctl = nvme_dev_ioctl, |
| 2555 | }; |
| 2556 | |
| 2557 | static ssize_t nvme_sysfs_reset(struct device *dev, |
| 2558 | struct device_attribute *attr, const char *buf, |
| 2559 | size_t count) |
| 2560 | { |
| 2561 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2562 | int ret; |
| 2563 | |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 2564 | ret = nvme_reset_ctrl_sync(ctrl); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2565 | if (ret < 0) |
| 2566 | return ret; |
| 2567 | return count; |
| 2568 | } |
| 2569 | static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset); |
| 2570 | |
Keith Busch | 9ec3bb2 | 2016-04-29 15:45:18 -0600 | [diff] [blame] | 2571 | static ssize_t nvme_sysfs_rescan(struct device *dev, |
| 2572 | struct device_attribute *attr, const char *buf, |
| 2573 | size_t count) |
| 2574 | { |
| 2575 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2576 | |
| 2577 | nvme_queue_scan(ctrl); |
| 2578 | return count; |
| 2579 | } |
| 2580 | static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan); |
| 2581 | |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2582 | static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev) |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2583 | { |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2584 | struct gendisk *disk = dev_to_disk(dev); |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2585 | |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2586 | if (disk->fops == &nvme_fops) |
| 2587 | return nvme_get_ns_from_dev(dev)->head; |
| 2588 | else |
| 2589 | return disk->private_data; |
| 2590 | } |
Johannes Thumshirn | 6484f5d | 2017-07-12 15:38:56 +0200 | [diff] [blame] | 2591 | |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2592 | static ssize_t wwid_show(struct device *dev, struct device_attribute *attr, |
| 2593 | char *buf) |
| 2594 | { |
| 2595 | struct nvme_ns_head *head = dev_to_ns_head(dev); |
| 2596 | struct nvme_ns_ids *ids = &head->ids; |
| 2597 | struct nvme_subsystem *subsys = head->subsys; |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2598 | int serial_len = sizeof(subsys->serial); |
| 2599 | int model_len = sizeof(subsys->model); |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2600 | |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2601 | if (!uuid_is_null(&ids->uuid)) |
| 2602 | return sprintf(buf, "uuid.%pU\n", &ids->uuid); |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2603 | |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2604 | if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid))) |
| 2605 | return sprintf(buf, "eui.%16phN\n", ids->nguid); |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2606 | |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2607 | if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64))) |
| 2608 | return sprintf(buf, "eui.%8phN\n", ids->eui64); |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2609 | |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2610 | while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' || |
| 2611 | subsys->serial[serial_len - 1] == '\0')) |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2612 | serial_len--; |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2613 | while (model_len > 0 && (subsys->model[model_len - 1] == ' ' || |
| 2614 | subsys->model[model_len - 1] == '\0')) |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2615 | model_len--; |
| 2616 | |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2617 | return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id, |
| 2618 | serial_len, subsys->serial, model_len, subsys->model, |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2619 | head->ns_id); |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2620 | } |
Joe Perches | c828a89 | 2017-12-19 10:15:08 -0800 | [diff] [blame] | 2621 | static DEVICE_ATTR_RO(wwid); |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2622 | |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2623 | static ssize_t nguid_show(struct device *dev, struct device_attribute *attr, |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2624 | char *buf) |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2625 | { |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2626 | return sprintf(buf, "%pU\n", dev_to_ns_head(dev)->ids.nguid); |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2627 | } |
Joe Perches | c828a89 | 2017-12-19 10:15:08 -0800 | [diff] [blame] | 2628 | static DEVICE_ATTR_RO(nguid); |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2629 | |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2630 | static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2631 | char *buf) |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2632 | { |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2633 | struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids; |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2634 | |
| 2635 | /* For backward compatibility expose the NGUID to userspace if |
| 2636 | * we have no UUID set |
| 2637 | */ |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2638 | if (uuid_is_null(&ids->uuid)) { |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2639 | printk_ratelimited(KERN_WARNING |
| 2640 | "No UUID available providing old NGUID\n"); |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2641 | return sprintf(buf, "%pU\n", ids->nguid); |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2642 | } |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2643 | return sprintf(buf, "%pU\n", &ids->uuid); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2644 | } |
Joe Perches | c828a89 | 2017-12-19 10:15:08 -0800 | [diff] [blame] | 2645 | static DEVICE_ATTR_RO(uuid); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2646 | |
| 2647 | static ssize_t eui_show(struct device *dev, struct device_attribute *attr, |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2648 | char *buf) |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2649 | { |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2650 | return sprintf(buf, "%8ph\n", dev_to_ns_head(dev)->ids.eui64); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2651 | } |
Joe Perches | c828a89 | 2017-12-19 10:15:08 -0800 | [diff] [blame] | 2652 | static DEVICE_ATTR_RO(eui); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2653 | |
| 2654 | static ssize_t nsid_show(struct device *dev, struct device_attribute *attr, |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2655 | char *buf) |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2656 | { |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2657 | return sprintf(buf, "%d\n", dev_to_ns_head(dev)->ns_id); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2658 | } |
Joe Perches | c828a89 | 2017-12-19 10:15:08 -0800 | [diff] [blame] | 2659 | static DEVICE_ATTR_RO(nsid); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2660 | |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2661 | static struct attribute *nvme_ns_id_attrs[] = { |
Keith Busch | 118472a | 2016-02-18 09:57:48 -0700 | [diff] [blame] | 2662 | &dev_attr_wwid.attr, |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2663 | &dev_attr_uuid.attr, |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2664 | &dev_attr_nguid.attr, |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2665 | &dev_attr_eui.attr, |
| 2666 | &dev_attr_nsid.attr, |
| 2667 | NULL, |
| 2668 | }; |
| 2669 | |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2670 | static umode_t nvme_ns_id_attrs_are_visible(struct kobject *kobj, |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2671 | struct attribute *a, int n) |
| 2672 | { |
| 2673 | struct device *dev = container_of(kobj, struct device, kobj); |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2674 | struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids; |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2675 | |
| 2676 | if (a == &dev_attr_uuid.attr) { |
Martin Wilck | a04b5de | 2017-09-28 21:33:23 +0200 | [diff] [blame] | 2677 | if (uuid_is_null(&ids->uuid) && |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2678 | !memchr_inv(ids->nguid, 0, sizeof(ids->nguid))) |
Johannes Thumshirn | d934f98 | 2017-06-07 11:45:35 +0200 | [diff] [blame] | 2679 | return 0; |
| 2680 | } |
| 2681 | if (a == &dev_attr_nguid.attr) { |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2682 | if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid))) |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2683 | return 0; |
| 2684 | } |
| 2685 | if (a == &dev_attr_eui.attr) { |
Christoph Hellwig | 002fab0 | 2017-11-09 13:50:16 +0100 | [diff] [blame] | 2686 | if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64))) |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2687 | return 0; |
| 2688 | } |
| 2689 | return a->mode; |
| 2690 | } |
| 2691 | |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 2692 | const struct attribute_group nvme_ns_id_attr_group = { |
| 2693 | .attrs = nvme_ns_id_attrs, |
| 2694 | .is_visible = nvme_ns_id_attrs_are_visible, |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 2695 | }; |
| 2696 | |
Ming Lin | 931e1c2 | 2016-02-26 13:24:19 -0800 | [diff] [blame] | 2697 | #define nvme_show_str_function(field) \ |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2698 | static ssize_t field##_show(struct device *dev, \ |
| 2699 | struct device_attribute *attr, char *buf) \ |
| 2700 | { \ |
| 2701 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2702 | return sprintf(buf, "%.*s\n", \ |
| 2703 | (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \ |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2704 | } \ |
| 2705 | static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); |
| 2706 | |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2707 | nvme_show_str_function(model); |
| 2708 | nvme_show_str_function(serial); |
| 2709 | nvme_show_str_function(firmware_rev); |
| 2710 | |
Ming Lin | 931e1c2 | 2016-02-26 13:24:19 -0800 | [diff] [blame] | 2711 | #define nvme_show_int_function(field) \ |
| 2712 | static ssize_t field##_show(struct device *dev, \ |
| 2713 | struct device_attribute *attr, char *buf) \ |
| 2714 | { \ |
| 2715 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ |
| 2716 | return sprintf(buf, "%d\n", ctrl->field); \ |
| 2717 | } \ |
| 2718 | static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); |
| 2719 | |
Ming Lin | 931e1c2 | 2016-02-26 13:24:19 -0800 | [diff] [blame] | 2720 | nvme_show_int_function(cntlid); |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2721 | |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2722 | static ssize_t nvme_sysfs_delete(struct device *dev, |
| 2723 | struct device_attribute *attr, const char *buf, |
| 2724 | size_t count) |
| 2725 | { |
| 2726 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2727 | |
| 2728 | if (device_remove_file_self(dev, attr)) |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 2729 | nvme_delete_ctrl_sync(ctrl); |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2730 | return count; |
| 2731 | } |
| 2732 | static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete); |
| 2733 | |
| 2734 | static ssize_t nvme_sysfs_show_transport(struct device *dev, |
| 2735 | struct device_attribute *attr, |
| 2736 | char *buf) |
| 2737 | { |
| 2738 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2739 | |
| 2740 | return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name); |
| 2741 | } |
| 2742 | static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL); |
| 2743 | |
Sagi Grimberg | 8432bdb2 | 2016-11-28 01:47:40 +0200 | [diff] [blame] | 2744 | static ssize_t nvme_sysfs_show_state(struct device *dev, |
| 2745 | struct device_attribute *attr, |
| 2746 | char *buf) |
| 2747 | { |
| 2748 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2749 | static const char *const state_name[] = { |
| 2750 | [NVME_CTRL_NEW] = "new", |
| 2751 | [NVME_CTRL_LIVE] = "live", |
Jianchao Wang | 2b1b7e7 | 2018-01-06 08:01:58 +0800 | [diff] [blame] | 2752 | [NVME_CTRL_ADMIN_ONLY] = "only-admin", |
Sagi Grimberg | 8432bdb2 | 2016-11-28 01:47:40 +0200 | [diff] [blame] | 2753 | [NVME_CTRL_RESETTING] = "resetting", |
Max Gurtovoy | ad6a0a5 | 2018-01-31 18:31:24 +0200 | [diff] [blame] | 2754 | [NVME_CTRL_CONNECTING] = "connecting", |
Sagi Grimberg | 8432bdb2 | 2016-11-28 01:47:40 +0200 | [diff] [blame] | 2755 | [NVME_CTRL_DELETING] = "deleting", |
| 2756 | [NVME_CTRL_DEAD] = "dead", |
| 2757 | }; |
| 2758 | |
| 2759 | if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) && |
| 2760 | state_name[ctrl->state]) |
| 2761 | return sprintf(buf, "%s\n", state_name[ctrl->state]); |
| 2762 | |
| 2763 | return sprintf(buf, "unknown state\n"); |
| 2764 | } |
| 2765 | |
| 2766 | static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL); |
| 2767 | |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2768 | static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev, |
| 2769 | struct device_attribute *attr, |
| 2770 | char *buf) |
| 2771 | { |
| 2772 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2773 | |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 2774 | return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subsys->subnqn); |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2775 | } |
| 2776 | static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL); |
| 2777 | |
| 2778 | static ssize_t nvme_sysfs_show_address(struct device *dev, |
| 2779 | struct device_attribute *attr, |
| 2780 | char *buf) |
| 2781 | { |
| 2782 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2783 | |
| 2784 | return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE); |
| 2785 | } |
| 2786 | static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL); |
| 2787 | |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2788 | static struct attribute *nvme_dev_attrs[] = { |
| 2789 | &dev_attr_reset_controller.attr, |
Keith Busch | 9ec3bb2 | 2016-04-29 15:45:18 -0600 | [diff] [blame] | 2790 | &dev_attr_rescan_controller.attr, |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2791 | &dev_attr_model.attr, |
| 2792 | &dev_attr_serial.attr, |
| 2793 | &dev_attr_firmware_rev.attr, |
Ming Lin | 931e1c2 | 2016-02-26 13:24:19 -0800 | [diff] [blame] | 2794 | &dev_attr_cntlid.attr, |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2795 | &dev_attr_delete_controller.attr, |
| 2796 | &dev_attr_transport.attr, |
| 2797 | &dev_attr_subsysnqn.attr, |
| 2798 | &dev_attr_address.attr, |
Sagi Grimberg | 8432bdb2 | 2016-11-28 01:47:40 +0200 | [diff] [blame] | 2799 | &dev_attr_state.attr, |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2800 | NULL |
| 2801 | }; |
| 2802 | |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2803 | static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj, |
| 2804 | struct attribute *a, int n) |
| 2805 | { |
| 2806 | struct device *dev = container_of(kobj, struct device, kobj); |
| 2807 | struct nvme_ctrl *ctrl = dev_get_drvdata(dev); |
| 2808 | |
Christoph Hellwig | 49d3d50 | 2017-06-26 12:39:03 +0200 | [diff] [blame] | 2809 | if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl) |
| 2810 | return 0; |
| 2811 | if (a == &dev_attr_address.attr && !ctrl->ops->get_address) |
| 2812 | return 0; |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2813 | |
| 2814 | return a->mode; |
| 2815 | } |
| 2816 | |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2817 | static struct attribute_group nvme_dev_attrs_group = { |
Ming Lin | 1a353d8 | 2016-06-13 16:45:24 +0200 | [diff] [blame] | 2818 | .attrs = nvme_dev_attrs, |
| 2819 | .is_visible = nvme_dev_attrs_are_visible, |
Keith Busch | 779ff756 | 2016-01-12 15:09:31 -0700 | [diff] [blame] | 2820 | }; |
| 2821 | |
| 2822 | static const struct attribute_group *nvme_dev_attr_groups[] = { |
| 2823 | &nvme_dev_attrs_group, |
| 2824 | NULL, |
| 2825 | }; |
| 2826 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2827 | static struct nvme_ns_head *__nvme_find_ns_head(struct nvme_subsystem *subsys, |
| 2828 | unsigned nsid) |
| 2829 | { |
| 2830 | struct nvme_ns_head *h; |
| 2831 | |
| 2832 | lockdep_assert_held(&subsys->lock); |
| 2833 | |
| 2834 | list_for_each_entry(h, &subsys->nsheads, entry) { |
| 2835 | if (h->ns_id == nsid && kref_get_unless_zero(&h->ref)) |
| 2836 | return h; |
| 2837 | } |
| 2838 | |
| 2839 | return NULL; |
| 2840 | } |
| 2841 | |
| 2842 | static int __nvme_check_ids(struct nvme_subsystem *subsys, |
| 2843 | struct nvme_ns_head *new) |
| 2844 | { |
| 2845 | struct nvme_ns_head *h; |
| 2846 | |
| 2847 | lockdep_assert_held(&subsys->lock); |
| 2848 | |
| 2849 | list_for_each_entry(h, &subsys->nsheads, entry) { |
| 2850 | if (nvme_ns_ids_valid(&new->ids) && |
Keith Busch | 2079699 | 2018-03-19 10:53:50 -0600 | [diff] [blame] | 2851 | !list_empty(&h->list) && |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2852 | nvme_ns_ids_equal(&new->ids, &h->ids)) |
| 2853 | return -EINVAL; |
| 2854 | } |
| 2855 | |
| 2856 | return 0; |
| 2857 | } |
| 2858 | |
| 2859 | static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl, |
| 2860 | unsigned nsid, struct nvme_id_ns *id) |
| 2861 | { |
| 2862 | struct nvme_ns_head *head; |
| 2863 | int ret = -ENOMEM; |
| 2864 | |
| 2865 | head = kzalloc(sizeof(*head), GFP_KERNEL); |
| 2866 | if (!head) |
| 2867 | goto out; |
| 2868 | ret = ida_simple_get(&ctrl->subsys->ns_ida, 1, 0, GFP_KERNEL); |
| 2869 | if (ret < 0) |
| 2870 | goto out_free_head; |
| 2871 | head->instance = ret; |
| 2872 | INIT_LIST_HEAD(&head->list); |
Max Gurtovoy | fd92c77 | 2018-04-12 09:16:12 -0600 | [diff] [blame] | 2873 | ret = init_srcu_struct(&head->srcu); |
| 2874 | if (ret) |
| 2875 | goto out_ida_remove; |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2876 | head->subsys = ctrl->subsys; |
| 2877 | head->ns_id = nsid; |
| 2878 | kref_init(&head->ref); |
| 2879 | |
| 2880 | nvme_report_ns_ids(ctrl, nsid, id, &head->ids); |
| 2881 | |
| 2882 | ret = __nvme_check_ids(ctrl->subsys, head); |
| 2883 | if (ret) { |
| 2884 | dev_err(ctrl->device, |
| 2885 | "duplicate IDs for nsid %d\n", nsid); |
| 2886 | goto out_cleanup_srcu; |
| 2887 | } |
| 2888 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 2889 | ret = nvme_mpath_alloc_disk(ctrl, head); |
| 2890 | if (ret) |
| 2891 | goto out_cleanup_srcu; |
| 2892 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2893 | list_add_tail(&head->entry, &ctrl->subsys->nsheads); |
Jianchao Wang | 12d9f07 | 2018-05-04 16:01:57 +0800 | [diff] [blame] | 2894 | |
| 2895 | kref_get(&ctrl->subsys->ref); |
| 2896 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2897 | return head; |
| 2898 | out_cleanup_srcu: |
| 2899 | cleanup_srcu_struct(&head->srcu); |
Max Gurtovoy | fd92c77 | 2018-04-12 09:16:12 -0600 | [diff] [blame] | 2900 | out_ida_remove: |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2901 | ida_simple_remove(&ctrl->subsys->ns_ida, head->instance); |
| 2902 | out_free_head: |
| 2903 | kfree(head); |
| 2904 | out: |
| 2905 | return ERR_PTR(ret); |
| 2906 | } |
| 2907 | |
| 2908 | static int nvme_init_ns_head(struct nvme_ns *ns, unsigned nsid, |
Baegjae Sung | 9bd82b1 | 2018-02-28 16:06:04 +0900 | [diff] [blame] | 2909 | struct nvme_id_ns *id) |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2910 | { |
| 2911 | struct nvme_ctrl *ctrl = ns->ctrl; |
| 2912 | bool is_shared = id->nmic & (1 << 0); |
| 2913 | struct nvme_ns_head *head = NULL; |
| 2914 | int ret = 0; |
| 2915 | |
| 2916 | mutex_lock(&ctrl->subsys->lock); |
| 2917 | if (is_shared) |
| 2918 | head = __nvme_find_ns_head(ctrl->subsys, nsid); |
| 2919 | if (!head) { |
| 2920 | head = nvme_alloc_ns_head(ctrl, nsid, id); |
| 2921 | if (IS_ERR(head)) { |
| 2922 | ret = PTR_ERR(head); |
| 2923 | goto out_unlock; |
| 2924 | } |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2925 | } else { |
| 2926 | struct nvme_ns_ids ids; |
| 2927 | |
| 2928 | nvme_report_ns_ids(ctrl, nsid, id, &ids); |
| 2929 | if (!nvme_ns_ids_equal(&head->ids, &ids)) { |
| 2930 | dev_err(ctrl->device, |
| 2931 | "IDs don't match for shared namespace %d\n", |
| 2932 | nsid); |
| 2933 | ret = -EINVAL; |
| 2934 | goto out_unlock; |
| 2935 | } |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | list_add_tail(&ns->siblings, &head->list); |
| 2939 | ns->head = head; |
| 2940 | |
| 2941 | out_unlock: |
| 2942 | mutex_unlock(&ctrl->subsys->lock); |
| 2943 | return ret; |
| 2944 | } |
| 2945 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2946 | static int ns_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 2947 | { |
| 2948 | struct nvme_ns *nsa = container_of(a, struct nvme_ns, list); |
| 2949 | struct nvme_ns *nsb = container_of(b, struct nvme_ns, list); |
| 2950 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2951 | return nsa->head->ns_id - nsb->head->ns_id; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2952 | } |
| 2953 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2954 | 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] | 2955 | { |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2956 | struct nvme_ns *ns, *ret = NULL; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2957 | |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 2958 | down_read(&ctrl->namespaces_rwsem); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2959 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2960 | if (ns->head->ns_id == nsid) { |
Christoph Hellwig | 2dd4122 | 2017-10-18 13:20:01 +0200 | [diff] [blame] | 2961 | if (!kref_get_unless_zero(&ns->kref)) |
| 2962 | continue; |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2963 | ret = ns; |
| 2964 | break; |
| 2965 | } |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2966 | if (ns->head->ns_id > nsid) |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2967 | break; |
| 2968 | } |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 2969 | up_read(&ctrl->namespaces_rwsem); |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 2970 | return ret; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2971 | } |
| 2972 | |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 2973 | static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns) |
| 2974 | { |
| 2975 | struct streams_directive_params s; |
| 2976 | int ret; |
| 2977 | |
| 2978 | if (!ctrl->nr_streams) |
| 2979 | return 0; |
| 2980 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 2981 | ret = nvme_get_stream_params(ctrl, &s, ns->head->ns_id); |
Jens Axboe | f5d1184 | 2017-06-27 12:03:06 -0600 | [diff] [blame] | 2982 | if (ret) |
| 2983 | return ret; |
| 2984 | |
| 2985 | ns->sws = le32_to_cpu(s.sws); |
| 2986 | ns->sgs = le16_to_cpu(s.sgs); |
| 2987 | |
| 2988 | if (ns->sws) { |
| 2989 | unsigned int bs = 1 << ns->lba_shift; |
| 2990 | |
| 2991 | blk_queue_io_min(ns->queue, bs * ns->sws); |
| 2992 | if (ns->sgs) |
| 2993 | blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs); |
| 2994 | } |
| 2995 | |
| 2996 | return 0; |
| 2997 | } |
| 2998 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2999 | static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid) |
| 3000 | { |
| 3001 | struct nvme_ns *ns; |
| 3002 | struct gendisk *disk; |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 3003 | struct nvme_id_ns *id; |
| 3004 | char disk_name[DISK_NAME_LEN]; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 3005 | int node = dev_to_node(ctrl->dev), flags = GENHD_FL_EXT_DEVT; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3006 | |
| 3007 | ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node); |
| 3008 | if (!ns) |
| 3009 | return; |
| 3010 | |
| 3011 | ns->queue = blk_mq_init_queue(ctrl->tagset); |
| 3012 | if (IS_ERR(ns->queue)) |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 3013 | goto out_free_ns; |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 3014 | blk_queue_flag_set(QUEUE_FLAG_NONROT, ns->queue); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3015 | ns->queue->queuedata = ns; |
| 3016 | ns->ctrl = ctrl; |
| 3017 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3018 | kref_init(&ns->kref); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3019 | 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] | 3020 | |
| 3021 | blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift); |
Christoph Hellwig | da35825 | 2016-03-02 18:07:11 +0100 | [diff] [blame] | 3022 | nvme_set_queue_limits(ctrl, ns->queue); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3023 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 3024 | id = nvme_identify_ns(ctrl, nsid); |
| 3025 | if (!id) |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 3026 | goto out_free_queue; |
| 3027 | |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 3028 | if (id->ncap == 0) |
| 3029 | goto out_free_id; |
| 3030 | |
Baegjae Sung | 9bd82b1 | 2018-02-28 16:06:04 +0900 | [diff] [blame] | 3031 | if (nvme_init_ns_head(ns, nsid, id)) |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 3032 | goto out_free_id; |
Keith Busch | 654b4a4 | 2017-12-14 11:20:32 -0700 | [diff] [blame] | 3033 | nvme_setup_streams_ns(ctrl, ns); |
Keith Busch | a785dbc | 2018-04-26 14:22:41 -0600 | [diff] [blame] | 3034 | nvme_set_disk_name(disk_name, ns, ctrl, &flags); |
Christoph Hellwig | cdbff4f | 2017-08-16 16:14:47 +0200 | [diff] [blame] | 3035 | |
Christoph Hellwig | 608cc4b | 2017-09-06 11:45:24 +0200 | [diff] [blame] | 3036 | if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) { |
| 3037 | if (nvme_nvm_register(ns, disk_name, node)) { |
| 3038 | dev_warn(ctrl->device, "LightNVM init failure\n"); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 3039 | goto out_unlink_ns; |
Christoph Hellwig | 608cc4b | 2017-09-06 11:45:24 +0200 | [diff] [blame] | 3040 | } |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 3041 | } |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3042 | |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 3043 | disk = alloc_disk_node(0, node); |
| 3044 | if (!disk) |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 3045 | goto out_unlink_ns; |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 3046 | |
| 3047 | disk->fops = &nvme_fops; |
| 3048 | disk->private_data = ns; |
| 3049 | disk->queue = ns->queue; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 3050 | disk->flags = flags; |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 3051 | memcpy(disk->disk_name, disk_name, DISK_NAME_LEN); |
| 3052 | ns->disk = disk; |
| 3053 | |
| 3054 | __nvme_revalidate_disk(disk, id); |
| 3055 | |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3056 | down_write(&ctrl->namespaces_rwsem); |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3057 | list_add_tail(&ns->list, &ctrl->namespaces); |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3058 | up_write(&ctrl->namespaces_rwsem); |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3059 | |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 3060 | nvme_get_ctrl(ctrl); |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 3061 | |
| 3062 | kfree(id); |
| 3063 | |
Dan Williams | 0d52c756 | 2016-06-15 19:44:20 -0700 | [diff] [blame] | 3064 | device_add_disk(ctrl->device, ns->disk); |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 3065 | if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj, |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 3066 | &nvme_ns_id_attr_group)) |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 3067 | pr_warn("%s: failed to create sysfs group for identification\n", |
| 3068 | ns->disk->disk_name); |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 3069 | if (ns->ndev && nvme_nvm_register_sysfs(ns)) |
| 3070 | pr_warn("%s: failed to register lightnvm sysfs group for identification\n", |
| 3071 | ns->disk->disk_name); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 3072 | |
Baegjae Sung | 9bd82b1 | 2018-02-28 16:06:04 +0900 | [diff] [blame] | 3073 | nvme_mpath_add_disk(ns->head); |
Thomas Tai | b9e0385 | 2018-02-08 13:38:29 -0500 | [diff] [blame] | 3074 | nvme_fault_inject_init(ns); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3075 | return; |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 3076 | out_unlink_ns: |
| 3077 | mutex_lock(&ctrl->subsys->lock); |
| 3078 | list_del_rcu(&ns->siblings); |
| 3079 | mutex_unlock(&ctrl->subsys->lock); |
Matias Bjørling | ac81bfa9 | 2016-09-16 14:25:04 +0200 | [diff] [blame] | 3080 | out_free_id: |
| 3081 | kfree(id); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3082 | out_free_queue: |
| 3083 | blk_cleanup_queue(ns->queue); |
| 3084 | out_free_ns: |
| 3085 | kfree(ns); |
| 3086 | } |
| 3087 | |
| 3088 | static void nvme_ns_remove(struct nvme_ns *ns) |
| 3089 | { |
Keith Busch | 646017a | 2016-02-24 09:15:54 -0700 | [diff] [blame] | 3090 | if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags)) |
| 3091 | return; |
| 3092 | |
Thomas Tai | b9e0385 | 2018-02-08 13:38:29 -0500 | [diff] [blame] | 3093 | nvme_fault_inject_fini(ns); |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 3094 | if (ns->disk && ns->disk->flags & GENHD_FL_UP) { |
Keith Busch | 2b9b6e8 | 2015-12-22 10:10:45 -0700 | [diff] [blame] | 3095 | sysfs_remove_group(&disk_to_dev(ns->disk)->kobj, |
Christoph Hellwig | 5b85b82 | 2017-11-09 13:51:03 +0100 | [diff] [blame] | 3096 | &nvme_ns_id_attr_group); |
Matias Bjørling | 3dc87dd | 2016-11-28 22:38:53 +0100 | [diff] [blame] | 3097 | if (ns->ndev) |
| 3098 | nvme_nvm_unregister_sysfs(ns); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3099 | del_gendisk(ns->disk); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3100 | blk_cleanup_queue(ns->queue); |
Ming Lei | bd9f5d6 | 2017-12-06 18:30:09 +0800 | [diff] [blame] | 3101 | if (blk_get_integrity(ns->disk)) |
| 3102 | blk_integrity_unregister(ns->disk); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3103 | } |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3104 | |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 3105 | mutex_lock(&ns->ctrl->subsys->lock); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 3106 | nvme_mpath_clear_current_path(ns); |
Keith Busch | 9941a86 | 2017-11-16 13:36:50 -0700 | [diff] [blame] | 3107 | list_del_rcu(&ns->siblings); |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 3108 | mutex_unlock(&ns->ctrl->subsys->lock); |
| 3109 | |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3110 | down_write(&ns->ctrl->namespaces_rwsem); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3111 | list_del_init(&ns->list); |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3112 | up_write(&ns->ctrl->namespaces_rwsem); |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3113 | |
Keith Busch | 9941a86 | 2017-11-16 13:36:50 -0700 | [diff] [blame] | 3114 | synchronize_srcu(&ns->head->srcu); |
Sagi Grimberg | 479a322 | 2017-12-21 15:07:27 +0200 | [diff] [blame] | 3115 | nvme_mpath_check_last_path(ns); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3116 | nvme_put_ns(ns); |
| 3117 | } |
| 3118 | |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3119 | static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid) |
| 3120 | { |
| 3121 | struct nvme_ns *ns; |
| 3122 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3123 | ns = nvme_find_get_ns(ctrl, nsid); |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3124 | if (ns) { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 3125 | if (ns->disk && revalidate_disk(ns->disk)) |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3126 | nvme_ns_remove(ns); |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3127 | nvme_put_ns(ns); |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3128 | } else |
| 3129 | nvme_alloc_ns(ctrl, nsid); |
| 3130 | } |
| 3131 | |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 3132 | static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl, |
| 3133 | unsigned nsid) |
| 3134 | { |
| 3135 | struct nvme_ns *ns, *next; |
Jianchao Wang | 6f8e0d7 | 2018-02-12 20:54:44 +0800 | [diff] [blame] | 3136 | LIST_HEAD(rm_list); |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 3137 | |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3138 | down_write(&ctrl->namespaces_rwsem); |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 3139 | list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) { |
Christoph Hellwig | ed754e5 | 2017-11-09 13:50:43 +0100 | [diff] [blame] | 3140 | if (ns->head->ns_id > nsid) |
Jianchao Wang | 6f8e0d7 | 2018-02-12 20:54:44 +0800 | [diff] [blame] | 3141 | list_move_tail(&ns->list, &rm_list); |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 3142 | } |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3143 | up_write(&ctrl->namespaces_rwsem); |
Jianchao Wang | 6f8e0d7 | 2018-02-12 20:54:44 +0800 | [diff] [blame] | 3144 | |
| 3145 | list_for_each_entry_safe(ns, next, &rm_list, list) |
| 3146 | nvme_ns_remove(ns); |
| 3147 | |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 3148 | } |
| 3149 | |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3150 | static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn) |
| 3151 | { |
| 3152 | struct nvme_ns *ns; |
| 3153 | __le32 *ns_list; |
| 3154 | unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024); |
| 3155 | int ret = 0; |
| 3156 | |
Minwoo Im | 42595eb | 2018-02-08 22:56:31 +0900 | [diff] [blame] | 3157 | ns_list = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL); |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3158 | if (!ns_list) |
| 3159 | return -ENOMEM; |
| 3160 | |
| 3161 | for (i = 0; i < num_lists; i++) { |
| 3162 | ret = nvme_identify_ns_list(ctrl, prev, ns_list); |
| 3163 | if (ret) |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 3164 | goto free; |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3165 | |
| 3166 | for (j = 0; j < min(nn, 1024U); j++) { |
| 3167 | nsid = le32_to_cpu(ns_list[j]); |
| 3168 | if (!nsid) |
| 3169 | goto out; |
| 3170 | |
| 3171 | nvme_validate_ns(ctrl, nsid); |
| 3172 | |
| 3173 | while (++prev < nsid) { |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3174 | ns = nvme_find_get_ns(ctrl, prev); |
| 3175 | if (ns) { |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3176 | nvme_ns_remove(ns); |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3177 | nvme_put_ns(ns); |
| 3178 | } |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3179 | } |
| 3180 | } |
| 3181 | nn -= j; |
| 3182 | } |
| 3183 | out: |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 3184 | nvme_remove_invalid_namespaces(ctrl, prev); |
| 3185 | free: |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3186 | kfree(ns_list); |
| 3187 | return ret; |
| 3188 | } |
| 3189 | |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 3190 | static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn) |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3191 | { |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3192 | unsigned i; |
| 3193 | |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3194 | for (i = 1; i <= nn; i++) |
| 3195 | nvme_validate_ns(ctrl, i); |
| 3196 | |
Sunad Bhandary | 47b0e50 | 2016-05-27 15:59:43 +0530 | [diff] [blame] | 3197 | nvme_remove_invalid_namespaces(ctrl, nn); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3198 | } |
| 3199 | |
Christoph Hellwig | f493af3 | 2018-06-07 13:47:33 +0200 | [diff] [blame] | 3200 | static void nvme_clear_changed_ns_log(struct nvme_ctrl *ctrl) |
Christoph Hellwig | 30d9096 | 2018-05-25 18:17:41 +0200 | [diff] [blame] | 3201 | { |
| 3202 | size_t log_size = NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32); |
| 3203 | __le32 *log; |
Christoph Hellwig | f493af3 | 2018-06-07 13:47:33 +0200 | [diff] [blame] | 3204 | int error; |
Christoph Hellwig | 30d9096 | 2018-05-25 18:17:41 +0200 | [diff] [blame] | 3205 | |
| 3206 | log = kzalloc(log_size, GFP_KERNEL); |
| 3207 | if (!log) |
Christoph Hellwig | f493af3 | 2018-06-07 13:47:33 +0200 | [diff] [blame] | 3208 | return; |
Christoph Hellwig | 30d9096 | 2018-05-25 18:17:41 +0200 | [diff] [blame] | 3209 | |
Christoph Hellwig | f493af3 | 2018-06-07 13:47:33 +0200 | [diff] [blame] | 3210 | /* |
| 3211 | * We need to read the log to clear the AEN, but we don't want to rely |
| 3212 | * on it for the changed namespace information as userspace could have |
| 3213 | * raced with us in reading the log page, which could cause us to miss |
| 3214 | * updates. |
| 3215 | */ |
Christoph Hellwig | 30d9096 | 2018-05-25 18:17:41 +0200 | [diff] [blame] | 3216 | error = nvme_get_log(ctrl, NVME_LOG_CHANGED_NS, log, log_size); |
Christoph Hellwig | f493af3 | 2018-06-07 13:47:33 +0200 | [diff] [blame] | 3217 | if (error) |
Christoph Hellwig | 30d9096 | 2018-05-25 18:17:41 +0200 | [diff] [blame] | 3218 | dev_warn(ctrl->device, |
| 3219 | "reading changed ns log failed: %d\n", error); |
Christoph Hellwig | 30d9096 | 2018-05-25 18:17:41 +0200 | [diff] [blame] | 3220 | |
Christoph Hellwig | 30d9096 | 2018-05-25 18:17:41 +0200 | [diff] [blame] | 3221 | kfree(log); |
Christoph Hellwig | 30d9096 | 2018-05-25 18:17:41 +0200 | [diff] [blame] | 3222 | } |
| 3223 | |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 3224 | static void nvme_scan_work(struct work_struct *work) |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3225 | { |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 3226 | struct nvme_ctrl *ctrl = |
| 3227 | container_of(work, struct nvme_ctrl, scan_work); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3228 | struct nvme_id_ctrl *id; |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3229 | unsigned nn; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3230 | |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 3231 | if (ctrl->state != NVME_CTRL_LIVE) |
| 3232 | return; |
| 3233 | |
Jianchao Wang | 2b1b7e7 | 2018-01-06 08:01:58 +0800 | [diff] [blame] | 3234 | WARN_ON_ONCE(!ctrl->tagset); |
| 3235 | |
Dan Carpenter | 7701619 | 2018-06-07 11:27:41 +0300 | [diff] [blame] | 3236 | if (test_and_clear_bit(NVME_AER_NOTICE_NS_CHANGED, &ctrl->events)) { |
Christoph Hellwig | 30d9096 | 2018-05-25 18:17:41 +0200 | [diff] [blame] | 3237 | dev_info(ctrl->device, "rescanning namespaces.\n"); |
Christoph Hellwig | f493af3 | 2018-06-07 13:47:33 +0200 | [diff] [blame] | 3238 | nvme_clear_changed_ns_log(ctrl); |
Christoph Hellwig | 30d9096 | 2018-05-25 18:17:41 +0200 | [diff] [blame] | 3239 | } |
| 3240 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3241 | if (nvme_identify_ctrl(ctrl, &id)) |
| 3242 | return; |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3243 | |
| 3244 | nn = le32_to_cpu(id->nn); |
Gabriel Krisman Bertazi | 8ef2074 | 2016-10-19 09:51:05 -0600 | [diff] [blame] | 3245 | if (ctrl->vs >= NVME_VS(1, 1, 0) && |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3246 | !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) { |
| 3247 | if (!nvme_scan_ns_list(ctrl, nn)) |
Christoph Hellwig | 30d9096 | 2018-05-25 18:17:41 +0200 | [diff] [blame] | 3248 | goto out_free_id; |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3249 | } |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 3250 | nvme_scan_ns_sequential(ctrl, nn); |
Christoph Hellwig | 30d9096 | 2018-05-25 18:17:41 +0200 | [diff] [blame] | 3251 | out_free_id: |
| 3252 | kfree(id); |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3253 | down_write(&ctrl->namespaces_rwsem); |
Keith Busch | 540c801 | 2015-10-22 15:45:06 -0600 | [diff] [blame] | 3254 | list_sort(NULL, &ctrl->namespaces, ns_cmp); |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3255 | up_write(&ctrl->namespaces_rwsem); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3256 | } |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 3257 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3258 | /* |
| 3259 | * This function iterates the namespace list unlocked to allow recovery from |
| 3260 | * controller failure. It is up to the caller to ensure the namespace list is |
| 3261 | * not modified by scan work while this function is executing. |
| 3262 | */ |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3263 | void nvme_remove_namespaces(struct nvme_ctrl *ctrl) |
| 3264 | { |
| 3265 | struct nvme_ns *ns, *next; |
Jianchao Wang | 6f8e0d7 | 2018-02-12 20:54:44 +0800 | [diff] [blame] | 3266 | LIST_HEAD(ns_list); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3267 | |
Keith Busch | 0ff9d4e | 2016-05-12 08:37:14 -0600 | [diff] [blame] | 3268 | /* |
| 3269 | * The dead states indicates the controller was not gracefully |
| 3270 | * disconnected. In that case, we won't be able to flush any data while |
| 3271 | * removing the namespaces' disks; fail all the queues now to avoid |
| 3272 | * potentially having to clean up the failed sync later. |
| 3273 | */ |
| 3274 | if (ctrl->state == NVME_CTRL_DEAD) |
| 3275 | nvme_kill_queues(ctrl); |
| 3276 | |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3277 | down_write(&ctrl->namespaces_rwsem); |
Jianchao Wang | 6f8e0d7 | 2018-02-12 20:54:44 +0800 | [diff] [blame] | 3278 | list_splice_init(&ctrl->namespaces, &ns_list); |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3279 | up_write(&ctrl->namespaces_rwsem); |
Jianchao Wang | 6f8e0d7 | 2018-02-12 20:54:44 +0800 | [diff] [blame] | 3280 | |
| 3281 | list_for_each_entry_safe(ns, next, &ns_list, list) |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3282 | nvme_ns_remove(ns); |
| 3283 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 3284 | EXPORT_SYMBOL_GPL(nvme_remove_namespaces); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3285 | |
Keith Busch | e3d7874 | 2017-11-07 15:13:14 -0700 | [diff] [blame] | 3286 | static void nvme_aen_uevent(struct nvme_ctrl *ctrl) |
| 3287 | { |
| 3288 | char *envp[2] = { NULL, NULL }; |
| 3289 | u32 aen_result = ctrl->aen_result; |
| 3290 | |
| 3291 | ctrl->aen_result = 0; |
| 3292 | if (!aen_result) |
| 3293 | return; |
| 3294 | |
| 3295 | envp[0] = kasprintf(GFP_KERNEL, "NVME_AEN=%#08x", aen_result); |
| 3296 | if (!envp[0]) |
| 3297 | return; |
| 3298 | kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp); |
| 3299 | kfree(envp[0]); |
| 3300 | } |
| 3301 | |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3302 | static void nvme_async_event_work(struct work_struct *work) |
| 3303 | { |
| 3304 | struct nvme_ctrl *ctrl = |
| 3305 | container_of(work, struct nvme_ctrl, async_event_work); |
| 3306 | |
Keith Busch | e3d7874 | 2017-11-07 15:13:14 -0700 | [diff] [blame] | 3307 | nvme_aen_uevent(ctrl); |
Keith Busch | ad22c35 | 2017-11-07 15:13:12 -0700 | [diff] [blame] | 3308 | ctrl->ops->submit_async_event(ctrl); |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3309 | } |
| 3310 | |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 3311 | static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl) |
| 3312 | { |
| 3313 | |
| 3314 | u32 csts; |
| 3315 | |
| 3316 | if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) |
| 3317 | return false; |
| 3318 | |
| 3319 | if (csts == ~0) |
| 3320 | return false; |
| 3321 | |
| 3322 | return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP)); |
| 3323 | } |
| 3324 | |
| 3325 | static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl) |
| 3326 | { |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 3327 | struct nvme_fw_slot_info_log *log; |
| 3328 | |
| 3329 | log = kmalloc(sizeof(*log), GFP_KERNEL); |
| 3330 | if (!log) |
| 3331 | return; |
| 3332 | |
Keith Busch | c627c48 | 2017-11-07 10:28:31 -0700 | [diff] [blame] | 3333 | if (nvme_get_log(ctrl, NVME_LOG_FW_SLOT, log, sizeof(*log))) |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 3334 | dev_warn(ctrl->device, |
| 3335 | "Get FW SLOT INFO log error\n"); |
| 3336 | kfree(log); |
| 3337 | } |
| 3338 | |
| 3339 | static void nvme_fw_act_work(struct work_struct *work) |
| 3340 | { |
| 3341 | struct nvme_ctrl *ctrl = container_of(work, |
| 3342 | struct nvme_ctrl, fw_act_work); |
| 3343 | unsigned long fw_act_timeout; |
| 3344 | |
| 3345 | if (ctrl->mtfa) |
| 3346 | fw_act_timeout = jiffies + |
| 3347 | msecs_to_jiffies(ctrl->mtfa * 100); |
| 3348 | else |
| 3349 | fw_act_timeout = jiffies + |
| 3350 | msecs_to_jiffies(admin_timeout * 1000); |
| 3351 | |
| 3352 | nvme_stop_queues(ctrl); |
| 3353 | while (nvme_ctrl_pp_status(ctrl)) { |
| 3354 | if (time_after(jiffies, fw_act_timeout)) { |
| 3355 | dev_warn(ctrl->device, |
| 3356 | "Fw activation timeout, reset controller\n"); |
| 3357 | nvme_reset_ctrl(ctrl); |
| 3358 | break; |
| 3359 | } |
| 3360 | msleep(100); |
| 3361 | } |
| 3362 | |
| 3363 | if (ctrl->state != NVME_CTRL_LIVE) |
| 3364 | return; |
| 3365 | |
| 3366 | nvme_start_queues(ctrl); |
Minwoo Im | a806c6c | 2017-11-02 18:07:44 +0900 | [diff] [blame] | 3367 | /* read FW slot information to clear the AER */ |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 3368 | nvme_get_fw_slot_info(ctrl); |
| 3369 | } |
| 3370 | |
Christoph Hellwig | 868c239 | 2018-05-22 11:09:54 +0200 | [diff] [blame] | 3371 | static void nvme_handle_aen_notice(struct nvme_ctrl *ctrl, u32 result) |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3372 | { |
Christoph Hellwig | 868c239 | 2018-05-22 11:09:54 +0200 | [diff] [blame] | 3373 | switch ((result & 0xff00) >> 8) { |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3374 | case NVME_AER_NOTICE_NS_CHANGED: |
Dan Carpenter | 7701619 | 2018-06-07 11:27:41 +0300 | [diff] [blame] | 3375 | set_bit(NVME_AER_NOTICE_NS_CHANGED, &ctrl->events); |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3376 | nvme_queue_scan(ctrl); |
| 3377 | break; |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 3378 | case NVME_AER_NOTICE_FW_ACT_STARTING: |
Sagi Grimberg | 1a40d97 | 2017-09-21 17:01:36 +0300 | [diff] [blame] | 3379 | queue_work(nvme_wq, &ctrl->fw_act_work); |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 3380 | break; |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3381 | default: |
| 3382 | dev_warn(ctrl->device, "async event result %08x\n", result); |
| 3383 | } |
Christoph Hellwig | 868c239 | 2018-05-22 11:09:54 +0200 | [diff] [blame] | 3384 | } |
| 3385 | |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3386 | void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status, |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3387 | volatile union nvme_result *res) |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3388 | { |
| 3389 | u32 result = le32_to_cpu(res->u32); |
| 3390 | |
| 3391 | if (le16_to_cpu(status) >> 1 != NVME_SC_SUCCESS) |
| 3392 | return; |
| 3393 | |
| 3394 | switch (result & 0x7) { |
Christoph Hellwig | 868c239 | 2018-05-22 11:09:54 +0200 | [diff] [blame] | 3395 | case NVME_AER_NOTICE: |
| 3396 | nvme_handle_aen_notice(ctrl, result); |
| 3397 | break; |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3398 | case NVME_AER_ERROR: |
| 3399 | case NVME_AER_SMART: |
| 3400 | case NVME_AER_CSS: |
| 3401 | case NVME_AER_VS: |
| 3402 | ctrl->aen_result = result; |
| 3403 | break; |
| 3404 | default: |
| 3405 | break; |
| 3406 | } |
Sagi Grimberg | c669ccd | 2017-05-04 13:33:14 +0300 | [diff] [blame] | 3407 | queue_work(nvme_wq, &ctrl->async_event_work); |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3408 | } |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3409 | EXPORT_SYMBOL_GPL(nvme_complete_async_event); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3410 | |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 3411 | void nvme_stop_ctrl(struct nvme_ctrl *ctrl) |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 3412 | { |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 3413 | nvme_stop_keep_alive(ctrl); |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3414 | flush_work(&ctrl->async_event_work); |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 3415 | flush_work(&ctrl->scan_work); |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 3416 | cancel_work_sync(&ctrl->fw_act_work); |
Nitzan Carmi | b435ece | 2018-03-20 11:07:30 +0000 | [diff] [blame] | 3417 | if (ctrl->ops->stop_ctrl) |
| 3418 | ctrl->ops->stop_ctrl(ctrl); |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 3419 | } |
| 3420 | EXPORT_SYMBOL_GPL(nvme_stop_ctrl); |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 3421 | |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 3422 | void nvme_start_ctrl(struct nvme_ctrl *ctrl) |
| 3423 | { |
| 3424 | if (ctrl->kato) |
| 3425 | nvme_start_keep_alive(ctrl); |
| 3426 | |
| 3427 | if (ctrl->queue_count > 1) { |
| 3428 | nvme_queue_scan(ctrl); |
Hannes Reinecke | c0561f8 | 2018-05-22 11:09:55 +0200 | [diff] [blame] | 3429 | nvme_enable_aen(ctrl); |
Keith Busch | d99ca60 | 2017-11-07 15:13:13 -0700 | [diff] [blame] | 3430 | queue_work(nvme_wq, &ctrl->async_event_work); |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 3431 | nvme_start_queues(ctrl); |
| 3432 | } |
| 3433 | } |
| 3434 | EXPORT_SYMBOL_GPL(nvme_start_ctrl); |
| 3435 | |
| 3436 | void nvme_uninit_ctrl(struct nvme_ctrl *ctrl) |
| 3437 | { |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 3438 | cdev_device_del(&ctrl->cdev, ctrl->device); |
Keith Busch | 53029b0 | 2015-11-28 15:41:02 +0100 | [diff] [blame] | 3439 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 3440 | EXPORT_SYMBOL_GPL(nvme_uninit_ctrl); |
Keith Busch | 53029b0 | 2015-11-28 15:41:02 +0100 | [diff] [blame] | 3441 | |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 3442 | static void nvme_free_ctrl(struct device *dev) |
Keith Busch | 53029b0 | 2015-11-28 15:41:02 +0100 | [diff] [blame] | 3443 | { |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 3444 | struct nvme_ctrl *ctrl = |
| 3445 | container_of(dev, struct nvme_ctrl, ctrl_device); |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 3446 | struct nvme_subsystem *subsys = ctrl->subsys; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3447 | |
Christoph Hellwig | 9843f68 | 2017-10-18 13:10:01 +0200 | [diff] [blame] | 3448 | ida_simple_remove(&nvme_instance_ida, ctrl->instance); |
Keith Busch | 84fef62 | 2017-11-07 10:28:32 -0700 | [diff] [blame] | 3449 | kfree(ctrl->effects); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3450 | |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 3451 | if (subsys) { |
| 3452 | mutex_lock(&subsys->lock); |
| 3453 | list_del(&ctrl->subsys_entry); |
| 3454 | mutex_unlock(&subsys->lock); |
| 3455 | sysfs_remove_link(&subsys->dev.kobj, dev_name(ctrl->device)); |
| 3456 | } |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3457 | |
| 3458 | ctrl->ops->free_ctrl(ctrl); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3459 | |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 3460 | if (subsys) |
| 3461 | nvme_put_subsystem(subsys); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | /* |
| 3465 | * Initialize a NVMe controller structures. This needs to be called during |
| 3466 | * earliest initialization so that we have the initialized structured around |
| 3467 | * during probing. |
| 3468 | */ |
| 3469 | int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev, |
| 3470 | const struct nvme_ctrl_ops *ops, unsigned long quirks) |
| 3471 | { |
| 3472 | int ret; |
| 3473 | |
Christoph Hellwig | bb8d261 | 2016-04-26 13:51:57 +0200 | [diff] [blame] | 3474 | ctrl->state = NVME_CTRL_NEW; |
| 3475 | spin_lock_init(&ctrl->lock); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3476 | INIT_LIST_HEAD(&ctrl->namespaces); |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3477 | init_rwsem(&ctrl->namespaces_rwsem); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3478 | ctrl->dev = dev; |
| 3479 | ctrl->ops = ops; |
| 3480 | ctrl->quirks = quirks; |
Christoph Hellwig | 5955be2 | 2016-04-26 13:51:59 +0200 | [diff] [blame] | 3481 | INIT_WORK(&ctrl->scan_work, nvme_scan_work); |
Christoph Hellwig | f866fc42 | 2016-04-26 13:52:00 +0200 | [diff] [blame] | 3482 | INIT_WORK(&ctrl->async_event_work, nvme_async_event_work); |
Arnav Dawn | b6dccf7 | 2017-07-12 16:10:40 +0530 | [diff] [blame] | 3483 | INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work); |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 3484 | INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3485 | |
Christoph Hellwig | 9843f68 | 2017-10-18 13:10:01 +0200 | [diff] [blame] | 3486 | ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL); |
| 3487 | if (ret < 0) |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3488 | goto out; |
Christoph Hellwig | 9843f68 | 2017-10-18 13:10:01 +0200 | [diff] [blame] | 3489 | ctrl->instance = ret; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3490 | |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 3491 | device_initialize(&ctrl->ctrl_device); |
| 3492 | ctrl->device = &ctrl->ctrl_device; |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 3493 | ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance); |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 3494 | ctrl->device->class = nvme_class; |
| 3495 | ctrl->device->parent = ctrl->dev; |
| 3496 | ctrl->device->groups = nvme_dev_attr_groups; |
| 3497 | ctrl->device->release = nvme_free_ctrl; |
| 3498 | dev_set_drvdata(ctrl->device, ctrl); |
| 3499 | ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance); |
| 3500 | if (ret) |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3501 | goto out_release_instance; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3502 | |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 3503 | cdev_init(&ctrl->cdev, &nvme_dev_fops); |
| 3504 | ctrl->cdev.owner = ops->module; |
| 3505 | ret = cdev_device_add(&ctrl->cdev, ctrl->device); |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 3506 | if (ret) |
| 3507 | goto out_free_name; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3508 | |
Andy Lutomirski | c5552fd | 2017-02-07 10:08:45 -0800 | [diff] [blame] | 3509 | /* |
| 3510 | * Initialize latency tolerance controls. The sysfs files won't |
| 3511 | * be visible to userspace unless the device actually supports APST. |
| 3512 | */ |
| 3513 | ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance; |
| 3514 | dev_pm_qos_update_user_latency_tolerance(ctrl->device, |
| 3515 | min(default_ps_max_latency_us, (unsigned long)S32_MAX)); |
| 3516 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3517 | return 0; |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 3518 | out_free_name: |
| 3519 | kfree_const(dev->kobj.name); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3520 | out_release_instance: |
Christoph Hellwig | 9843f68 | 2017-10-18 13:10:01 +0200 | [diff] [blame] | 3521 | ida_simple_remove(&nvme_instance_ida, ctrl->instance); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3522 | out: |
| 3523 | return ret; |
| 3524 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 3525 | EXPORT_SYMBOL_GPL(nvme_init_ctrl); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3526 | |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 3527 | /** |
| 3528 | * nvme_kill_queues(): Ends all namespace queues |
| 3529 | * @ctrl: the dead controller that needs to end |
| 3530 | * |
| 3531 | * Call this function when the driver determines it is unable to get the |
| 3532 | * controller in a state capable of servicing IO. |
| 3533 | */ |
| 3534 | void nvme_kill_queues(struct nvme_ctrl *ctrl) |
| 3535 | { |
| 3536 | struct nvme_ns *ns; |
| 3537 | |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3538 | down_read(&ctrl->namespaces_rwsem); |
Ming Lei | 82654b6 | 2017-06-02 16:32:08 +0800 | [diff] [blame] | 3539 | |
Ming Lei | 443bd90 | 2017-06-19 10:21:08 +0800 | [diff] [blame] | 3540 | /* Forcibly unquiesce queues to avoid blocking dispatch */ |
Scott Bauer | 7dd1ab1 | 2017-07-25 10:27:06 -0600 | [diff] [blame] | 3541 | if (ctrl->admin_q) |
| 3542 | blk_mq_unquiesce_queue(ctrl->admin_q); |
Ming Lei | 443bd90 | 2017-06-19 10:21:08 +0800 | [diff] [blame] | 3543 | |
Keith Busch | 32f0c4a | 2016-07-13 11:45:02 -0600 | [diff] [blame] | 3544 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 3545 | /* |
| 3546 | * Revalidating a dead namespace sets capacity to 0. This will |
| 3547 | * end buffered writers dirtying pages that can't be synced. |
| 3548 | */ |
Keith Busch | f33447b | 2017-02-10 18:15:51 -0500 | [diff] [blame] | 3549 | if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags)) |
| 3550 | continue; |
| 3551 | revalidate_disk(ns->disk); |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 3552 | blk_set_queue_dying(ns->queue); |
Ming Lei | 806f026 | 2017-05-22 23:05:03 +0800 | [diff] [blame] | 3553 | |
Ming Lei | 443bd90 | 2017-06-19 10:21:08 +0800 | [diff] [blame] | 3554 | /* Forcibly unquiesce queues to avoid blocking dispatch */ |
| 3555 | blk_mq_unquiesce_queue(ns->queue); |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 3556 | } |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3557 | up_read(&ctrl->namespaces_rwsem); |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 3558 | } |
Linus Torvalds | 237045f | 2016-03-18 17:13:31 -0700 | [diff] [blame] | 3559 | EXPORT_SYMBOL_GPL(nvme_kill_queues); |
Keith Busch | 69d9a99 | 2016-02-24 09:15:56 -0700 | [diff] [blame] | 3560 | |
Keith Busch | 302ad8c | 2017-03-01 14:22:12 -0500 | [diff] [blame] | 3561 | void nvme_unfreeze(struct nvme_ctrl *ctrl) |
| 3562 | { |
| 3563 | struct nvme_ns *ns; |
| 3564 | |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3565 | down_read(&ctrl->namespaces_rwsem); |
Keith Busch | 302ad8c | 2017-03-01 14:22:12 -0500 | [diff] [blame] | 3566 | list_for_each_entry(ns, &ctrl->namespaces, list) |
| 3567 | blk_mq_unfreeze_queue(ns->queue); |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3568 | up_read(&ctrl->namespaces_rwsem); |
Keith Busch | 302ad8c | 2017-03-01 14:22:12 -0500 | [diff] [blame] | 3569 | } |
| 3570 | EXPORT_SYMBOL_GPL(nvme_unfreeze); |
| 3571 | |
| 3572 | void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout) |
| 3573 | { |
| 3574 | struct nvme_ns *ns; |
| 3575 | |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3576 | down_read(&ctrl->namespaces_rwsem); |
Keith Busch | 302ad8c | 2017-03-01 14:22:12 -0500 | [diff] [blame] | 3577 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
| 3578 | timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout); |
| 3579 | if (timeout <= 0) |
| 3580 | break; |
| 3581 | } |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3582 | up_read(&ctrl->namespaces_rwsem); |
Keith Busch | 302ad8c | 2017-03-01 14:22:12 -0500 | [diff] [blame] | 3583 | } |
| 3584 | EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout); |
| 3585 | |
| 3586 | void nvme_wait_freeze(struct nvme_ctrl *ctrl) |
| 3587 | { |
| 3588 | struct nvme_ns *ns; |
| 3589 | |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3590 | down_read(&ctrl->namespaces_rwsem); |
Keith Busch | 302ad8c | 2017-03-01 14:22:12 -0500 | [diff] [blame] | 3591 | list_for_each_entry(ns, &ctrl->namespaces, list) |
| 3592 | blk_mq_freeze_queue_wait(ns->queue); |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3593 | up_read(&ctrl->namespaces_rwsem); |
Keith Busch | 302ad8c | 2017-03-01 14:22:12 -0500 | [diff] [blame] | 3594 | } |
| 3595 | EXPORT_SYMBOL_GPL(nvme_wait_freeze); |
| 3596 | |
| 3597 | void nvme_start_freeze(struct nvme_ctrl *ctrl) |
| 3598 | { |
| 3599 | struct nvme_ns *ns; |
| 3600 | |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3601 | down_read(&ctrl->namespaces_rwsem); |
Keith Busch | 302ad8c | 2017-03-01 14:22:12 -0500 | [diff] [blame] | 3602 | list_for_each_entry(ns, &ctrl->namespaces, list) |
Ming Lei | 1671d52 | 2017-03-27 20:06:57 +0800 | [diff] [blame] | 3603 | blk_freeze_queue_start(ns->queue); |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3604 | up_read(&ctrl->namespaces_rwsem); |
Keith Busch | 302ad8c | 2017-03-01 14:22:12 -0500 | [diff] [blame] | 3605 | } |
| 3606 | EXPORT_SYMBOL_GPL(nvme_start_freeze); |
| 3607 | |
Keith Busch | 2564626 | 2016-01-04 09:10:57 -0700 | [diff] [blame] | 3608 | void nvme_stop_queues(struct nvme_ctrl *ctrl) |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 3609 | { |
| 3610 | struct nvme_ns *ns; |
| 3611 | |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3612 | down_read(&ctrl->namespaces_rwsem); |
Bart Van Assche | a6eaa88 | 2016-10-28 17:23:40 -0700 | [diff] [blame] | 3613 | list_for_each_entry(ns, &ctrl->namespaces, list) |
Bart Van Assche | 3174dd3 | 2016-10-28 17:23:19 -0700 | [diff] [blame] | 3614 | blk_mq_quiesce_queue(ns->queue); |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3615 | up_read(&ctrl->namespaces_rwsem); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 3616 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 3617 | EXPORT_SYMBOL_GPL(nvme_stop_queues); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 3618 | |
Keith Busch | 2564626 | 2016-01-04 09:10:57 -0700 | [diff] [blame] | 3619 | void nvme_start_queues(struct nvme_ctrl *ctrl) |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 3620 | { |
| 3621 | struct nvme_ns *ns; |
| 3622 | |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3623 | down_read(&ctrl->namespaces_rwsem); |
Sagi Grimberg | 8d7b8fa | 2017-07-04 18:16:58 +0300 | [diff] [blame] | 3624 | list_for_each_entry(ns, &ctrl->namespaces, list) |
Ming Lei | f660174 | 2017-06-06 23:22:04 +0800 | [diff] [blame] | 3625 | blk_mq_unquiesce_queue(ns->queue); |
Jianchao Wang | 765cc03 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 3626 | up_read(&ctrl->namespaces_rwsem); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 3627 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 3628 | EXPORT_SYMBOL_GPL(nvme_start_queues); |
Sagi Grimberg | 363c9aa | 2015-12-24 15:26:59 +0100 | [diff] [blame] | 3629 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3630 | int __init nvme_core_init(void) |
| 3631 | { |
Roy Shterman | b227c59 | 2018-01-14 12:39:02 +0200 | [diff] [blame] | 3632 | int result = -ENOMEM; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3633 | |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 3634 | nvme_wq = alloc_workqueue("nvme-wq", |
| 3635 | WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0); |
| 3636 | if (!nvme_wq) |
Roy Shterman | b227c59 | 2018-01-14 12:39:02 +0200 | [diff] [blame] | 3637 | goto out; |
| 3638 | |
| 3639 | nvme_reset_wq = alloc_workqueue("nvme-reset-wq", |
| 3640 | WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0); |
| 3641 | if (!nvme_reset_wq) |
| 3642 | goto destroy_wq; |
| 3643 | |
| 3644 | nvme_delete_wq = alloc_workqueue("nvme-delete-wq", |
| 3645 | WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0); |
| 3646 | if (!nvme_delete_wq) |
| 3647 | goto destroy_reset_wq; |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 3648 | |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 3649 | result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme"); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3650 | if (result < 0) |
Roy Shterman | b227c59 | 2018-01-14 12:39:02 +0200 | [diff] [blame] | 3651 | goto destroy_delete_wq; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3652 | |
| 3653 | nvme_class = class_create(THIS_MODULE, "nvme"); |
| 3654 | if (IS_ERR(nvme_class)) { |
| 3655 | result = PTR_ERR(nvme_class); |
| 3656 | goto unregister_chrdev; |
| 3657 | } |
| 3658 | |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 3659 | nvme_subsys_class = class_create(THIS_MODULE, "nvme-subsystem"); |
| 3660 | if (IS_ERR(nvme_subsys_class)) { |
| 3661 | result = PTR_ERR(nvme_subsys_class); |
| 3662 | goto destroy_class; |
| 3663 | } |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3664 | return 0; |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3665 | |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 3666 | destroy_class: |
| 3667 | class_destroy(nvme_class); |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 3668 | unregister_chrdev: |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 3669 | unregister_chrdev_region(nvme_chr_devt, NVME_MINORS); |
Roy Shterman | b227c59 | 2018-01-14 12:39:02 +0200 | [diff] [blame] | 3670 | destroy_delete_wq: |
| 3671 | destroy_workqueue(nvme_delete_wq); |
| 3672 | destroy_reset_wq: |
| 3673 | destroy_workqueue(nvme_reset_wq); |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 3674 | destroy_wq: |
| 3675 | destroy_workqueue(nvme_wq); |
Roy Shterman | b227c59 | 2018-01-14 12:39:02 +0200 | [diff] [blame] | 3676 | out: |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3677 | return result; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | void nvme_core_exit(void) |
| 3681 | { |
Christoph Hellwig | ab9e00cc | 2017-11-09 13:48:55 +0100 | [diff] [blame] | 3682 | ida_destroy(&nvme_subsystems_ida); |
| 3683 | class_destroy(nvme_subsys_class); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 3684 | class_destroy(nvme_class); |
Christoph Hellwig | a6a5149 | 2017-10-18 16:59:25 +0200 | [diff] [blame] | 3685 | unregister_chrdev_region(nvme_chr_devt, NVME_MINORS); |
Roy Shterman | b227c59 | 2018-01-14 12:39:02 +0200 | [diff] [blame] | 3686 | destroy_workqueue(nvme_delete_wq); |
| 3687 | destroy_workqueue(nvme_reset_wq); |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 3688 | destroy_workqueue(nvme_wq); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 3689 | } |
Ming Lin | 576d55d | 2016-02-10 10:03:32 -0800 | [diff] [blame] | 3690 | |
| 3691 | MODULE_LICENSE("GPL"); |
| 3692 | MODULE_VERSION("1.0"); |
| 3693 | module_init(nvme_core_init); |
| 3694 | module_exit(nvme_core_exit); |