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