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