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