Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1 | /* |
| 2 | * NVM Express device driver |
Matthew Wilcox | 6eb0d69 | 2014-03-24 10:11:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2011-2014, Intel Corporation. |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 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. |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/nvme.h> |
Matthew Wilcox | 8de0553 | 2011-05-12 13:50:28 -0400 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 17 | #include <linux/blkdev.h> |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 18 | #include <linux/blk-mq.h> |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 19 | #include <linux/cpu.h> |
Matthew Wilcox | fd63e9ce | 2011-05-06 08:37:54 -0400 | [diff] [blame] | 20 | #include <linux/delay.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 21 | #include <linux/errno.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/genhd.h> |
Keith Busch | 4cc09e2 | 2014-04-02 15:45:37 -0600 | [diff] [blame] | 24 | #include <linux/hdreg.h> |
Matthew Wilcox | 5aff938 | 2011-05-06 08:45:47 -0400 | [diff] [blame] | 25 | #include <linux/idr.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 26 | #include <linux/init.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/io.h> |
| 29 | #include <linux/kdev_t.h> |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 30 | #include <linux/kthread.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 31 | #include <linux/kernel.h> |
| 32 | #include <linux/mm.h> |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/moduleparam.h> |
| 35 | #include <linux/pci.h> |
Matthew Wilcox | be7b627 | 2011-02-06 07:53:23 -0500 | [diff] [blame] | 36 | #include <linux/poison.h> |
Matthew Wilcox | c3bfe71 | 2013-07-08 17:26:25 -0400 | [diff] [blame] | 37 | #include <linux/ptrace.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 38 | #include <linux/sched.h> |
| 39 | #include <linux/slab.h> |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 40 | #include <linux/t10-pi.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 41 | #include <linux/types.h> |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 42 | #include <scsi/sg.h> |
Hitoshi Mitake | 797a796 | 2012-02-07 11:45:33 +0900 | [diff] [blame] | 43 | #include <asm-generic/io-64-nonatomic-lo-hi.h> |
| 44 | |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 45 | #define NVME_MINORS (1U << MINORBITS) |
Keith Busch | 9d43cf6 | 2014-05-13 11:42:02 -0600 | [diff] [blame] | 46 | #define NVME_Q_DEPTH 1024 |
Jens Axboe | d31af0a | 2015-03-06 12:56:13 -0700 | [diff] [blame] | 47 | #define NVME_AQ_DEPTH 256 |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 48 | #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command)) |
| 49 | #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion)) |
Keith Busch | 9d43cf6 | 2014-05-13 11:42:02 -0600 | [diff] [blame] | 50 | #define ADMIN_TIMEOUT (admin_timeout * HZ) |
Dan McLeran | 2484f40 | 2014-07-01 09:33:32 -0600 | [diff] [blame] | 51 | #define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 52 | |
Keith Busch | 9d43cf6 | 2014-05-13 11:42:02 -0600 | [diff] [blame] | 53 | static unsigned char admin_timeout = 60; |
| 54 | module_param(admin_timeout, byte, 0644); |
| 55 | MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands"); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 56 | |
Matthew Wilcox | bd67608 | 2014-06-03 23:04:30 -0400 | [diff] [blame] | 57 | unsigned char nvme_io_timeout = 30; |
| 58 | module_param_named(io_timeout, nvme_io_timeout, byte, 0644); |
Keith Busch | b355084 | 2014-04-04 11:43:36 -0600 | [diff] [blame] | 59 | MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O"); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 60 | |
Dan McLeran | 2484f40 | 2014-07-01 09:33:32 -0600 | [diff] [blame] | 61 | static unsigned char shutdown_timeout = 5; |
| 62 | module_param(shutdown_timeout, byte, 0644); |
| 63 | MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown"); |
| 64 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 65 | static int nvme_major; |
| 66 | module_param(nvme_major, int, 0); |
| 67 | |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 68 | static int nvme_char_major; |
| 69 | module_param(nvme_char_major, int, 0); |
| 70 | |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 71 | static int use_threaded_interrupts; |
| 72 | module_param(use_threaded_interrupts, int, 0); |
| 73 | |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 74 | static DEFINE_SPINLOCK(dev_list_lock); |
| 75 | static LIST_HEAD(dev_list); |
| 76 | static struct task_struct *nvme_thread; |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 77 | static struct workqueue_struct *nvme_workq; |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 78 | static wait_queue_head_t nvme_kthread_wait; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 79 | |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 80 | static struct class *nvme_class; |
| 81 | |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 82 | static void nvme_reset_failed_dev(struct work_struct *ws); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 83 | static int nvme_process_cq(struct nvme_queue *nvmeq); |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 84 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 85 | struct async_cmd_info { |
| 86 | struct kthread_work work; |
| 87 | struct kthread_worker *worker; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 88 | struct request *req; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 89 | u32 result; |
| 90 | int status; |
| 91 | void *ctx; |
| 92 | }; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 93 | |
| 94 | /* |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 95 | * An NVM Express queue. Each device has at least two (one for admin |
| 96 | * commands and one for I/O commands). |
| 97 | */ |
| 98 | struct nvme_queue { |
| 99 | struct device *q_dmadev; |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 100 | struct nvme_dev *dev; |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 101 | char irqname[24]; /* nvme4294967295-65535\0 */ |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 102 | spinlock_t q_lock; |
| 103 | struct nvme_command *sq_cmds; |
| 104 | volatile struct nvme_completion *cqes; |
| 105 | dma_addr_t sq_dma_addr; |
| 106 | dma_addr_t cq_dma_addr; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 107 | u32 __iomem *q_db; |
| 108 | u16 q_depth; |
Jens Axboe | 6222d17 | 2015-01-15 15:19:10 -0700 | [diff] [blame] | 109 | s16 cq_vector; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 110 | u16 sq_head; |
| 111 | u16 sq_tail; |
| 112 | u16 cq_head; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 113 | u16 qid; |
Matthew Wilcox | e9539f4 | 2013-06-24 11:47:34 -0400 | [diff] [blame] | 114 | u8 cq_phase; |
| 115 | u8 cqe_seen; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 116 | struct async_cmd_info cmdinfo; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 117 | struct blk_mq_hw_ctx *hctx; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /* |
| 121 | * Check we didin't inadvertently grow the command struct |
| 122 | */ |
| 123 | static inline void _nvme_check_size(void) |
| 124 | { |
| 125 | BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64); |
| 126 | BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64); |
| 127 | BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64); |
| 128 | BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); |
| 129 | BUILD_BUG_ON(sizeof(struct nvme_features) != 64); |
Vishal Verma | f8ebf84 | 2013-03-27 07:13:41 -0400 | [diff] [blame] | 130 | BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64); |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 131 | BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 132 | BUILD_BUG_ON(sizeof(struct nvme_command) != 64); |
| 133 | BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096); |
| 134 | BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096); |
| 135 | BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64); |
Keith Busch | 6ecec74 | 2012-09-26 12:49:27 -0600 | [diff] [blame] | 136 | BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 139 | typedef void (*nvme_completion_fn)(struct nvme_queue *, void *, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 140 | struct nvme_completion *); |
| 141 | |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 142 | struct nvme_cmd_info { |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 143 | nvme_completion_fn fn; |
| 144 | void *ctx; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 145 | int aborted; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 146 | struct nvme_queue *nvmeq; |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 147 | struct nvme_iod iod[0]; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 148 | }; |
| 149 | |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 150 | /* |
| 151 | * Max size of iod being embedded in the request payload |
| 152 | */ |
| 153 | #define NVME_INT_PAGES 2 |
| 154 | #define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->page_size) |
Chong Yuan | fda631f | 2015-03-27 09:21:32 +0800 | [diff] [blame] | 155 | #define NVME_INT_MASK 0x01 |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * Will slightly overestimate the number of pages needed. This is OK |
| 159 | * as it only leads to a small amount of wasted memory for the lifetime of |
| 160 | * the I/O. |
| 161 | */ |
| 162 | static int nvme_npages(unsigned size, struct nvme_dev *dev) |
| 163 | { |
| 164 | unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size); |
| 165 | return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8); |
| 166 | } |
| 167 | |
| 168 | static unsigned int nvme_cmd_size(struct nvme_dev *dev) |
| 169 | { |
| 170 | unsigned int ret = sizeof(struct nvme_cmd_info); |
| 171 | |
| 172 | ret += sizeof(struct nvme_iod); |
| 173 | ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev); |
| 174 | ret += sizeof(struct scatterlist) * NVME_INT_PAGES; |
| 175 | |
| 176 | return ret; |
| 177 | } |
| 178 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 179 | static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 180 | unsigned int hctx_idx) |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 181 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 182 | struct nvme_dev *dev = data; |
| 183 | struct nvme_queue *nvmeq = dev->queues[0]; |
| 184 | |
| 185 | WARN_ON(nvmeq->hctx); |
| 186 | nvmeq->hctx = hctx; |
| 187 | hctx->driver_data = nvmeq; |
| 188 | return 0; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 191 | static int nvme_admin_init_request(void *data, struct request *req, |
| 192 | unsigned int hctx_idx, unsigned int rq_idx, |
| 193 | unsigned int numa_node) |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 194 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 195 | struct nvme_dev *dev = data; |
| 196 | struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req); |
| 197 | struct nvme_queue *nvmeq = dev->queues[0]; |
| 198 | |
| 199 | BUG_ON(!nvmeq); |
| 200 | cmd->nvmeq = nvmeq; |
| 201 | return 0; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 202 | } |
| 203 | |
Jens Axboe | 2c30540b | 2014-11-14 09:47:32 -0700 | [diff] [blame] | 204 | static void nvme_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) |
| 205 | { |
| 206 | struct nvme_queue *nvmeq = hctx->driver_data; |
| 207 | |
| 208 | nvmeq->hctx = NULL; |
| 209 | } |
| 210 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 211 | static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 212 | unsigned int hctx_idx) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 213 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 214 | struct nvme_dev *dev = data; |
| 215 | struct nvme_queue *nvmeq = dev->queues[ |
| 216 | (hctx_idx % dev->queue_count) + 1]; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 217 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 218 | if (!nvmeq->hctx) |
| 219 | nvmeq->hctx = hctx; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 220 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 221 | /* nvmeq queues are shared between namespaces. We assume here that |
| 222 | * blk-mq map the tags so they match up with the nvme queue tags. */ |
| 223 | WARN_ON(nvmeq->hctx->tags != hctx->tags); |
| 224 | |
| 225 | hctx->driver_data = nvmeq; |
| 226 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 227 | } |
| 228 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 229 | static int nvme_init_request(void *data, struct request *req, |
| 230 | unsigned int hctx_idx, unsigned int rq_idx, |
| 231 | unsigned int numa_node) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 232 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 233 | struct nvme_dev *dev = data; |
| 234 | struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req); |
| 235 | struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1]; |
| 236 | |
| 237 | BUG_ON(!nvmeq); |
| 238 | cmd->nvmeq = nvmeq; |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx, |
| 243 | nvme_completion_fn handler) |
| 244 | { |
| 245 | cmd->fn = handler; |
| 246 | cmd->ctx = ctx; |
| 247 | cmd->aborted = 0; |
Keith Busch | c917dfe | 2015-01-07 18:55:48 -0700 | [diff] [blame] | 248 | blk_mq_start_request(blk_mq_rq_from_pdu(cmd)); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 251 | static void *iod_get_private(struct nvme_iod *iod) |
| 252 | { |
| 253 | return (void *) (iod->private & ~0x1UL); |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * If bit 0 is set, the iod is embedded in the request payload. |
| 258 | */ |
| 259 | static bool iod_should_kfree(struct nvme_iod *iod) |
| 260 | { |
Chong Yuan | fda631f | 2015-03-27 09:21:32 +0800 | [diff] [blame] | 261 | return (iod->private & NVME_INT_MASK) == 0; |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 264 | /* Special values must be less than 0x1000 */ |
| 265 | #define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA) |
Matthew Wilcox | d2d8703 | 2011-02-07 15:55:59 -0500 | [diff] [blame] | 266 | #define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE) |
| 267 | #define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE) |
| 268 | #define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE) |
Matthew Wilcox | be7b627 | 2011-02-06 07:53:23 -0500 | [diff] [blame] | 269 | |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 270 | static void special_completion(struct nvme_queue *nvmeq, void *ctx, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 271 | struct nvme_completion *cqe) |
| 272 | { |
| 273 | if (ctx == CMD_CTX_CANCELLED) |
| 274 | return; |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 275 | if (ctx == CMD_CTX_COMPLETED) { |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 276 | dev_warn(nvmeq->q_dmadev, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 277 | "completed id %d twice on queue %d\n", |
| 278 | cqe->command_id, le16_to_cpup(&cqe->sq_id)); |
| 279 | return; |
| 280 | } |
| 281 | if (ctx == CMD_CTX_INVALID) { |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 282 | dev_warn(nvmeq->q_dmadev, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 283 | "invalid id %d completed on queue %d\n", |
| 284 | cqe->command_id, le16_to_cpup(&cqe->sq_id)); |
| 285 | return; |
| 286 | } |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 287 | dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx); |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 288 | } |
| 289 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 290 | static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn) |
| 291 | { |
| 292 | void *ctx; |
| 293 | |
| 294 | if (fn) |
| 295 | *fn = cmd->fn; |
| 296 | ctx = cmd->ctx; |
| 297 | cmd->fn = special_completion; |
| 298 | cmd->ctx = CMD_CTX_CANCELLED; |
| 299 | return ctx; |
| 300 | } |
| 301 | |
| 302 | static void async_req_completion(struct nvme_queue *nvmeq, void *ctx, |
| 303 | struct nvme_completion *cqe) |
| 304 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 305 | u32 result = le32_to_cpup(&cqe->result); |
| 306 | u16 status = le16_to_cpup(&cqe->status) >> 1; |
| 307 | |
| 308 | if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ) |
| 309 | ++nvmeq->dev->event_limit; |
| 310 | if (status == NVME_SC_SUCCESS) |
| 311 | dev_warn(nvmeq->q_dmadev, |
| 312 | "async event result %08x\n", result); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static void abort_completion(struct nvme_queue *nvmeq, void *ctx, |
| 316 | struct nvme_completion *cqe) |
| 317 | { |
| 318 | struct request *req = ctx; |
| 319 | |
| 320 | u16 status = le16_to_cpup(&cqe->status) >> 1; |
| 321 | u32 result = le32_to_cpup(&cqe->result); |
| 322 | |
Jens Axboe | 9d135bb | 2014-11-17 10:43:42 -0700 | [diff] [blame] | 323 | blk_mq_free_hctx_request(nvmeq->hctx, req); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 324 | |
| 325 | dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result); |
| 326 | ++nvmeq->dev->abort_limit; |
| 327 | } |
| 328 | |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 329 | static void async_completion(struct nvme_queue *nvmeq, void *ctx, |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 330 | struct nvme_completion *cqe) |
| 331 | { |
| 332 | struct async_cmd_info *cmdinfo = ctx; |
| 333 | cmdinfo->result = le32_to_cpup(&cqe->result); |
| 334 | cmdinfo->status = le16_to_cpup(&cqe->status) >> 1; |
| 335 | queue_kthread_work(cmdinfo->worker, &cmdinfo->work); |
Jens Axboe | 9d135bb | 2014-11-17 10:43:42 -0700 | [diff] [blame] | 336 | blk_mq_free_hctx_request(nvmeq->hctx, cmdinfo->req); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq, |
| 340 | unsigned int tag) |
| 341 | { |
| 342 | struct blk_mq_hw_ctx *hctx = nvmeq->hctx; |
| 343 | struct request *req = blk_mq_tag_to_rq(hctx->tags, tag); |
| 344 | |
| 345 | return blk_mq_rq_to_pdu(req); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Matthew Wilcox | 184d294 | 2011-05-11 21:36:38 -0400 | [diff] [blame] | 348 | /* |
| 349 | * Called with local interrupts disabled and the q_lock held. May not sleep. |
| 350 | */ |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 351 | static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 352 | nvme_completion_fn *fn) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 353 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 354 | struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag); |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 355 | void *ctx; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 356 | if (tag >= nvmeq->q_depth) { |
| 357 | *fn = special_completion; |
Matthew Wilcox | 48e3d39 | 2011-02-06 08:51:15 -0500 | [diff] [blame] | 358 | return CMD_CTX_INVALID; |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 359 | } |
Keith Busch | 859361a | 2012-08-02 14:05:59 -0600 | [diff] [blame] | 360 | if (fn) |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 361 | *fn = cmd->fn; |
| 362 | ctx = cmd->ctx; |
| 363 | cmd->fn = special_completion; |
| 364 | cmd->ctx = CMD_CTX_COMPLETED; |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 365 | return ctx; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 366 | } |
| 367 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 368 | /** |
Matthew Wilcox | 714a7a2 | 2011-03-16 16:28:24 -0400 | [diff] [blame] | 369 | * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 370 | * @nvmeq: The queue to use |
| 371 | * @cmd: The command to send |
| 372 | * |
| 373 | * Safe to use from interrupt context |
| 374 | */ |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 375 | static int __nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 376 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 377 | u16 tail = nvmeq->sq_tail; |
| 378 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 379 | memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd)); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 380 | if (++tail == nvmeq->q_depth) |
| 381 | tail = 0; |
Matthew Wilcox | 7547881 | 2011-02-16 09:59:59 -0500 | [diff] [blame] | 382 | writel(tail, nvmeq->q_db); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 383 | nvmeq->sq_tail = tail; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 388 | static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd) |
| 389 | { |
| 390 | unsigned long flags; |
| 391 | int ret; |
| 392 | spin_lock_irqsave(&nvmeq->q_lock, flags); |
| 393 | ret = __nvme_submit_cmd(nvmeq, cmd); |
| 394 | spin_unlock_irqrestore(&nvmeq->q_lock, flags); |
| 395 | return ret; |
| 396 | } |
| 397 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 398 | static __le64 **iod_list(struct nvme_iod *iod) |
| 399 | { |
| 400 | return ((void *)iod) + iod->offset; |
| 401 | } |
| 402 | |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 403 | static inline void iod_init(struct nvme_iod *iod, unsigned nbytes, |
| 404 | unsigned nseg, unsigned long private) |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 405 | { |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 406 | iod->private = private; |
| 407 | iod->offset = offsetof(struct nvme_iod, sg[nseg]); |
| 408 | iod->npages = -1; |
| 409 | iod->length = nbytes; |
| 410 | iod->nents = 0; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static struct nvme_iod * |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 414 | __nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev, |
| 415 | unsigned long priv, gfp_t gfp) |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 416 | { |
| 417 | struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) + |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 418 | sizeof(__le64 *) * nvme_npages(bytes, dev) + |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 419 | sizeof(struct scatterlist) * nseg, gfp); |
| 420 | |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 421 | if (iod) |
| 422 | iod_init(iod, bytes, nseg, priv); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 423 | |
| 424 | return iod; |
| 425 | } |
| 426 | |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 427 | static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev, |
| 428 | gfp_t gfp) |
| 429 | { |
| 430 | unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) : |
| 431 | sizeof(struct nvme_dsm_range); |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 432 | struct nvme_iod *iod; |
| 433 | |
| 434 | if (rq->nr_phys_segments <= NVME_INT_PAGES && |
| 435 | size <= NVME_INT_BYTES(dev)) { |
| 436 | struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq); |
| 437 | |
| 438 | iod = cmd->iod; |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 439 | iod_init(iod, size, rq->nr_phys_segments, |
Chong Yuan | fda631f | 2015-03-27 09:21:32 +0800 | [diff] [blame] | 440 | (unsigned long) rq | NVME_INT_MASK); |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 441 | return iod; |
| 442 | } |
| 443 | |
| 444 | return __nvme_alloc_iod(rq->nr_phys_segments, size, dev, |
| 445 | (unsigned long) rq, gfp); |
| 446 | } |
| 447 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 448 | static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod) |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 449 | { |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 450 | const int last_prp = dev->page_size / 8 - 1; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 451 | int i; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 452 | __le64 **list = iod_list(iod); |
| 453 | dma_addr_t prp_dma = iod->first_dma; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 454 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 455 | if (iod->npages == 0) |
| 456 | dma_pool_free(dev->prp_small_pool, list[0], prp_dma); |
| 457 | for (i = 0; i < iod->npages; i++) { |
| 458 | __le64 *prp_list = list[i]; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 459 | dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]); |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 460 | dma_pool_free(dev->prp_page_pool, prp_list, prp_dma); |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 461 | prp_dma = next_prp_dma; |
| 462 | } |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 463 | |
| 464 | if (iod_should_kfree(iod)) |
| 465 | kfree(iod); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 466 | } |
| 467 | |
Keith Busch | b4ff9c8 | 2014-08-29 09:06:12 -0600 | [diff] [blame] | 468 | static int nvme_error_status(u16 status) |
| 469 | { |
| 470 | switch (status & 0x7ff) { |
| 471 | case NVME_SC_SUCCESS: |
| 472 | return 0; |
| 473 | case NVME_SC_CAP_EXCEEDED: |
| 474 | return -ENOSPC; |
| 475 | default: |
| 476 | return -EIO; |
| 477 | } |
| 478 | } |
| 479 | |
Keith Busch | 52b68d7 | 2015-02-23 09:16:21 -0700 | [diff] [blame] | 480 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 481 | static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi) |
| 482 | { |
| 483 | if (be32_to_cpu(pi->ref_tag) == v) |
| 484 | pi->ref_tag = cpu_to_be32(p); |
| 485 | } |
| 486 | |
| 487 | static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi) |
| 488 | { |
| 489 | if (be32_to_cpu(pi->ref_tag) == p) |
| 490 | pi->ref_tag = cpu_to_be32(v); |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * nvme_dif_remap - remaps ref tags to bip seed and physical lba |
| 495 | * |
| 496 | * The virtual start sector is the one that was originally submitted by the |
| 497 | * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical |
| 498 | * start sector may be different. Remap protection information to match the |
| 499 | * physical LBA on writes, and back to the original seed on reads. |
| 500 | * |
| 501 | * Type 0 and 3 do not have a ref tag, so no remapping required. |
| 502 | */ |
| 503 | static void nvme_dif_remap(struct request *req, |
| 504 | void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi)) |
| 505 | { |
| 506 | struct nvme_ns *ns = req->rq_disk->private_data; |
| 507 | struct bio_integrity_payload *bip; |
| 508 | struct t10_pi_tuple *pi; |
| 509 | void *p, *pmap; |
| 510 | u32 i, nlb, ts, phys, virt; |
| 511 | |
| 512 | if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3) |
| 513 | return; |
| 514 | |
| 515 | bip = bio_integrity(req->bio); |
| 516 | if (!bip) |
| 517 | return; |
| 518 | |
| 519 | pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset; |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 520 | |
| 521 | p = pmap; |
| 522 | virt = bip_get_seed(bip); |
| 523 | phys = nvme_block_nr(ns, blk_rq_pos(req)); |
| 524 | nlb = (blk_rq_bytes(req) >> ns->lba_shift); |
| 525 | ts = ns->disk->integrity->tuple_size; |
| 526 | |
| 527 | for (i = 0; i < nlb; i++, virt++, phys++) { |
| 528 | pi = (struct t10_pi_tuple *)p; |
| 529 | dif_swap(phys, virt, pi); |
| 530 | p += ts; |
| 531 | } |
| 532 | kunmap_atomic(pmap); |
| 533 | } |
| 534 | |
Keith Busch | 52b68d7 | 2015-02-23 09:16:21 -0700 | [diff] [blame] | 535 | static int nvme_noop_verify(struct blk_integrity_iter *iter) |
| 536 | { |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | static int nvme_noop_generate(struct blk_integrity_iter *iter) |
| 541 | { |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | struct blk_integrity nvme_meta_noop = { |
| 546 | .name = "NVME_META_NOOP", |
| 547 | .generate_fn = nvme_noop_generate, |
| 548 | .verify_fn = nvme_noop_verify, |
| 549 | }; |
| 550 | |
| 551 | static void nvme_init_integrity(struct nvme_ns *ns) |
| 552 | { |
| 553 | struct blk_integrity integrity; |
| 554 | |
| 555 | switch (ns->pi_type) { |
| 556 | case NVME_NS_DPS_PI_TYPE3: |
| 557 | integrity = t10_pi_type3_crc; |
| 558 | break; |
| 559 | case NVME_NS_DPS_PI_TYPE1: |
| 560 | case NVME_NS_DPS_PI_TYPE2: |
| 561 | integrity = t10_pi_type1_crc; |
| 562 | break; |
| 563 | default: |
| 564 | integrity = nvme_meta_noop; |
| 565 | break; |
| 566 | } |
| 567 | integrity.tuple_size = ns->ms; |
| 568 | blk_integrity_register(ns->disk, &integrity); |
| 569 | blk_queue_max_integrity_segments(ns->queue, 1); |
| 570 | } |
| 571 | #else /* CONFIG_BLK_DEV_INTEGRITY */ |
| 572 | static void nvme_dif_remap(struct request *req, |
| 573 | void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi)) |
| 574 | { |
| 575 | } |
| 576 | static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi) |
| 577 | { |
| 578 | } |
| 579 | static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi) |
| 580 | { |
| 581 | } |
| 582 | static void nvme_init_integrity(struct nvme_ns *ns) |
| 583 | { |
| 584 | } |
| 585 | #endif |
| 586 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 587 | static void req_completion(struct nvme_queue *nvmeq, void *ctx, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 588 | struct nvme_completion *cqe) |
| 589 | { |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 590 | struct nvme_iod *iod = ctx; |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 591 | struct request *req = iod_get_private(iod); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 592 | struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req); |
| 593 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 594 | u16 status = le16_to_cpup(&cqe->status) >> 1; |
| 595 | |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 596 | if (unlikely(status)) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 597 | if (!(status & NVME_SC_DNR || blk_noretry_request(req)) |
| 598 | && (jiffies - req->start_time) < req->timeout) { |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 599 | unsigned long flags; |
| 600 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 601 | blk_mq_requeue_request(req); |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 602 | spin_lock_irqsave(req->q->queue_lock, flags); |
| 603 | if (!blk_queue_stopped(req->q)) |
| 604 | blk_mq_kick_requeue_list(req->q); |
| 605 | spin_unlock_irqrestore(req->q->queue_lock, flags); |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 606 | return; |
| 607 | } |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 608 | if (req->cmd_type == REQ_TYPE_DRV_PRIV) { |
| 609 | req->sense_len = le32_to_cpup(&cqe->result); |
| 610 | req->errors = status; |
| 611 | } else { |
| 612 | req->errors = nvme_error_status(status); |
| 613 | } |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 614 | } else |
| 615 | req->errors = 0; |
| 616 | |
| 617 | if (cmd_rq->aborted) |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 618 | dev_warn(nvmeq->dev->dev, |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 619 | "completing aborted command with status:%04x\n", |
| 620 | status); |
| 621 | |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 622 | if (iod->nents) { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 623 | dma_unmap_sg(nvmeq->dev->dev, iod->sg, iod->nents, |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 624 | rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 625 | if (blk_integrity_rq(req)) { |
| 626 | if (!rq_data_dir(req)) |
| 627 | nvme_dif_remap(req, nvme_dif_complete); |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 628 | dma_unmap_sg(nvmeq->dev->dev, iod->meta_sg, 1, |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 629 | rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 630 | } |
| 631 | } |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 632 | nvme_free_iod(nvmeq->dev, iod); |
Keith Busch | 3291fa5 | 2014-04-28 12:30:52 -0600 | [diff] [blame] | 633 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 634 | blk_mq_complete_request(req); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 635 | } |
| 636 | |
Matthew Wilcox | 184d294 | 2011-05-11 21:36:38 -0400 | [diff] [blame] | 637 | /* length is in bytes. gfp flags indicates whether we may sleep. */ |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 638 | static int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, |
| 639 | int total_len, gfp_t gfp) |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 640 | { |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 641 | struct dma_pool *pool; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 642 | int length = total_len; |
| 643 | struct scatterlist *sg = iod->sg; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 644 | int dma_len = sg_dma_len(sg); |
| 645 | u64 dma_addr = sg_dma_address(sg); |
Murali Iyer | f137e0f | 2015-03-26 11:07:51 -0500 | [diff] [blame] | 646 | u32 page_size = dev->page_size; |
| 647 | int offset = dma_addr & (page_size - 1); |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 648 | __le64 *prp_list; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 649 | __le64 **list = iod_list(iod); |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 650 | dma_addr_t prp_dma; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 651 | int nprps, i; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 652 | |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 653 | length -= (page_size - offset); |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 654 | if (length <= 0) |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 655 | return total_len; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 656 | |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 657 | dma_len -= (page_size - offset); |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 658 | if (dma_len) { |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 659 | dma_addr += (page_size - offset); |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 660 | } else { |
| 661 | sg = sg_next(sg); |
| 662 | dma_addr = sg_dma_address(sg); |
| 663 | dma_len = sg_dma_len(sg); |
| 664 | } |
| 665 | |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 666 | if (length <= page_size) { |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 667 | iod->first_dma = dma_addr; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 668 | return total_len; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 669 | } |
| 670 | |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 671 | nprps = DIV_ROUND_UP(length, page_size); |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 672 | if (nprps <= (256 / 8)) { |
| 673 | pool = dev->prp_small_pool; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 674 | iod->npages = 0; |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 675 | } else { |
| 676 | pool = dev->prp_page_pool; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 677 | iod->npages = 1; |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 678 | } |
| 679 | |
Matthew Wilcox | b77954c | 2011-05-12 13:51:41 -0400 | [diff] [blame] | 680 | prp_list = dma_pool_alloc(pool, gfp, &prp_dma); |
| 681 | if (!prp_list) { |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 682 | iod->first_dma = dma_addr; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 683 | iod->npages = -1; |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 684 | return (total_len - length) + page_size; |
Matthew Wilcox | b77954c | 2011-05-12 13:51:41 -0400 | [diff] [blame] | 685 | } |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 686 | list[0] = prp_list; |
| 687 | iod->first_dma = prp_dma; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 688 | i = 0; |
| 689 | for (;;) { |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 690 | if (i == page_size >> 3) { |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 691 | __le64 *old_prp_list = prp_list; |
Matthew Wilcox | b77954c | 2011-05-12 13:51:41 -0400 | [diff] [blame] | 692 | prp_list = dma_pool_alloc(pool, gfp, &prp_dma); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 693 | if (!prp_list) |
| 694 | return total_len - length; |
| 695 | list[iod->npages++] = prp_list; |
Matthew Wilcox | 7523d83 | 2011-03-16 16:43:40 -0400 | [diff] [blame] | 696 | prp_list[0] = old_prp_list[i - 1]; |
| 697 | old_prp_list[i - 1] = cpu_to_le64(prp_dma); |
| 698 | i = 1; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 699 | } |
| 700 | prp_list[i++] = cpu_to_le64(dma_addr); |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 701 | dma_len -= page_size; |
| 702 | dma_addr += page_size; |
| 703 | length -= page_size; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 704 | if (length <= 0) |
| 705 | break; |
| 706 | if (dma_len > 0) |
| 707 | continue; |
| 708 | BUG_ON(dma_len < 0); |
| 709 | sg = sg_next(sg); |
| 710 | dma_addr = sg_dma_address(sg); |
| 711 | dma_len = sg_dma_len(sg); |
| 712 | } |
| 713 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 714 | return total_len; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 715 | } |
| 716 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 717 | static void nvme_submit_priv(struct nvme_queue *nvmeq, struct request *req, |
| 718 | struct nvme_iod *iod) |
| 719 | { |
| 720 | struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; |
| 721 | |
| 722 | memcpy(cmnd, req->cmd, sizeof(struct nvme_command)); |
| 723 | cmnd->rw.command_id = req->tag; |
| 724 | if (req->nr_phys_segments) { |
| 725 | cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg)); |
| 726 | cmnd->rw.prp2 = cpu_to_le64(iod->first_dma); |
| 727 | } |
| 728 | |
| 729 | if (++nvmeq->sq_tail == nvmeq->q_depth) |
| 730 | nvmeq->sq_tail = 0; |
| 731 | writel(nvmeq->sq_tail, nvmeq->q_db); |
| 732 | } |
| 733 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 734 | /* |
| 735 | * We reuse the small pool to allocate the 16-byte range here as it is not |
| 736 | * worth having a special pool for these or additional cases to handle freeing |
| 737 | * the iod. |
| 738 | */ |
| 739 | static void nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns, |
| 740 | struct request *req, struct nvme_iod *iod) |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 741 | { |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 742 | struct nvme_dsm_range *range = |
| 743 | (struct nvme_dsm_range *)iod_list(iod)[0]; |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 744 | struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; |
| 745 | |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 746 | range->cattr = cpu_to_le32(0); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 747 | range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift); |
| 748 | range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req))); |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 749 | |
| 750 | memset(cmnd, 0, sizeof(*cmnd)); |
| 751 | cmnd->dsm.opcode = nvme_cmd_dsm; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 752 | cmnd->dsm.command_id = req->tag; |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 753 | cmnd->dsm.nsid = cpu_to_le32(ns->ns_id); |
| 754 | cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma); |
| 755 | cmnd->dsm.nr = 0; |
| 756 | cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD); |
| 757 | |
| 758 | if (++nvmeq->sq_tail == nvmeq->q_depth) |
| 759 | nvmeq->sq_tail = 0; |
| 760 | writel(nvmeq->sq_tail, nvmeq->q_db); |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 763 | static void nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns, |
Matthew Wilcox | 00df5cb | 2011-02-22 14:18:30 -0500 | [diff] [blame] | 764 | int cmdid) |
| 765 | { |
| 766 | struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; |
| 767 | |
| 768 | memset(cmnd, 0, sizeof(*cmnd)); |
| 769 | cmnd->common.opcode = nvme_cmd_flush; |
| 770 | cmnd->common.command_id = cmdid; |
| 771 | cmnd->common.nsid = cpu_to_le32(ns->ns_id); |
| 772 | |
| 773 | if (++nvmeq->sq_tail == nvmeq->q_depth) |
| 774 | nvmeq->sq_tail = 0; |
| 775 | writel(nvmeq->sq_tail, nvmeq->q_db); |
Matthew Wilcox | 00df5cb | 2011-02-22 14:18:30 -0500 | [diff] [blame] | 776 | } |
| 777 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 778 | static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod, |
| 779 | struct nvme_ns *ns) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 780 | { |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 781 | struct request *req = iod_get_private(iod); |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 782 | struct nvme_command *cmnd; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 783 | u16 control = 0; |
| 784 | u32 dsmgmt = 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 785 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 786 | if (req->cmd_flags & REQ_FUA) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 787 | control |= NVME_RW_FUA; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 788 | if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD)) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 789 | control |= NVME_RW_LR; |
| 790 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 791 | if (req->cmd_flags & REQ_RAHEAD) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 792 | dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; |
| 793 | |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 794 | cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail]; |
Matthew Wilcox | b8deb62 | 2011-01-26 10:08:25 -0500 | [diff] [blame] | 795 | memset(cmnd, 0, sizeof(*cmnd)); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 796 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 797 | cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read); |
| 798 | cmnd->rw.command_id = req->tag; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 799 | cmnd->rw.nsid = cpu_to_le32(ns->ns_id); |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 800 | cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg)); |
| 801 | cmnd->rw.prp2 = cpu_to_le64(iod->first_dma); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 802 | cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req))); |
| 803 | cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1); |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 804 | |
| 805 | if (blk_integrity_rq(req)) { |
| 806 | cmnd->rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg)); |
| 807 | switch (ns->pi_type) { |
| 808 | case NVME_NS_DPS_PI_TYPE3: |
| 809 | control |= NVME_RW_PRINFO_PRCHK_GUARD; |
| 810 | break; |
| 811 | case NVME_NS_DPS_PI_TYPE1: |
| 812 | case NVME_NS_DPS_PI_TYPE2: |
| 813 | control |= NVME_RW_PRINFO_PRCHK_GUARD | |
| 814 | NVME_RW_PRINFO_PRCHK_REF; |
| 815 | cmnd->rw.reftag = cpu_to_le32( |
| 816 | nvme_block_nr(ns, blk_rq_pos(req))); |
| 817 | break; |
| 818 | } |
| 819 | } else if (ns->ms) |
| 820 | control |= NVME_RW_PRINFO_PRACT; |
| 821 | |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 822 | cmnd->rw.control = cpu_to_le16(control); |
| 823 | cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 824 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 825 | if (++nvmeq->sq_tail == nvmeq->q_depth) |
| 826 | nvmeq->sq_tail = 0; |
Matthew Wilcox | 7547881 | 2011-02-16 09:59:59 -0500 | [diff] [blame] | 827 | writel(nvmeq->sq_tail, nvmeq->q_db); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 828 | |
Matthew Wilcox | 1974b1a | 2011-02-10 12:01:09 -0500 | [diff] [blame] | 829 | return 0; |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 830 | } |
Matthew Wilcox | 1974b1a | 2011-02-10 12:01:09 -0500 | [diff] [blame] | 831 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 832 | /* |
| 833 | * NOTE: ns is NULL when called on the admin queue. |
| 834 | */ |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 835 | static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 836 | const struct blk_mq_queue_data *bd) |
Keith Busch | 53562be | 2014-04-29 11:41:29 -0600 | [diff] [blame] | 837 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 838 | struct nvme_ns *ns = hctx->queue->queuedata; |
| 839 | struct nvme_queue *nvmeq = hctx->driver_data; |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 840 | struct nvme_dev *dev = nvmeq->dev; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 841 | struct request *req = bd->rq; |
| 842 | struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req); |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 843 | struct nvme_iod *iod; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 844 | enum dma_data_direction dma_dir; |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 845 | |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 846 | /* |
| 847 | * If formated with metadata, require the block layer provide a buffer |
| 848 | * unless this namespace is formated such that the metadata can be |
| 849 | * stripped/generated by the controller with PRACT=1. |
| 850 | */ |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 851 | if (ns && ns->ms && !blk_integrity_rq(req)) { |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 852 | if (!(ns->pi_type && ns->ms == 8)) { |
| 853 | req->errors = -EFAULT; |
| 854 | blk_mq_complete_request(req); |
| 855 | return BLK_MQ_RQ_QUEUE_OK; |
| 856 | } |
| 857 | } |
| 858 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 859 | iod = nvme_alloc_iod(req, dev, GFP_ATOMIC); |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 860 | if (!iod) |
Jens Axboe | fe54303 | 2014-12-11 13:58:39 -0700 | [diff] [blame] | 861 | return BLK_MQ_RQ_QUEUE_BUSY; |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 862 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 863 | if (req->cmd_flags & REQ_DISCARD) { |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 864 | void *range; |
| 865 | /* |
| 866 | * We reuse the small pool to allocate the 16-byte range here |
| 867 | * as it is not worth having a special pool for these or |
| 868 | * additional cases to handle freeing the iod. |
| 869 | */ |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 870 | range = dma_pool_alloc(dev->prp_small_pool, GFP_ATOMIC, |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 871 | &iod->first_dma); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 872 | if (!range) |
Jens Axboe | fe54303 | 2014-12-11 13:58:39 -0700 | [diff] [blame] | 873 | goto retry_cmd; |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 874 | iod_list(iod)[0] = (__le64 *)range; |
| 875 | iod->npages = 0; |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 876 | } else if (req->nr_phys_segments) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 877 | dma_dir = rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE; |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 878 | |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 879 | sg_init_table(iod->sg, req->nr_phys_segments); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 880 | iod->nents = blk_rq_map_sg(req->q, req, iod->sg); |
Jens Axboe | fe54303 | 2014-12-11 13:58:39 -0700 | [diff] [blame] | 881 | if (!iod->nents) |
| 882 | goto error_cmd; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 883 | |
| 884 | if (!dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir)) |
Jens Axboe | fe54303 | 2014-12-11 13:58:39 -0700 | [diff] [blame] | 885 | goto retry_cmd; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 886 | |
Jens Axboe | fe54303 | 2014-12-11 13:58:39 -0700 | [diff] [blame] | 887 | if (blk_rq_bytes(req) != |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 888 | nvme_setup_prps(dev, iod, blk_rq_bytes(req), GFP_ATOMIC)) { |
| 889 | dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir); |
Jens Axboe | fe54303 | 2014-12-11 13:58:39 -0700 | [diff] [blame] | 890 | goto retry_cmd; |
| 891 | } |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 892 | if (blk_integrity_rq(req)) { |
| 893 | if (blk_rq_count_integrity_sg(req->q, req->bio) != 1) |
| 894 | goto error_cmd; |
| 895 | |
| 896 | sg_init_table(iod->meta_sg, 1); |
| 897 | if (blk_rq_map_integrity_sg( |
| 898 | req->q, req->bio, iod->meta_sg) != 1) |
| 899 | goto error_cmd; |
| 900 | |
| 901 | if (rq_data_dir(req)) |
| 902 | nvme_dif_remap(req, nvme_dif_prep); |
| 903 | |
| 904 | if (!dma_map_sg(nvmeq->q_dmadev, iod->meta_sg, 1, dma_dir)) |
| 905 | goto error_cmd; |
| 906 | } |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Keith Busch | 9af8785 | 2014-12-03 17:07:13 -0700 | [diff] [blame] | 909 | nvme_set_info(cmd, iod, req_completion); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 910 | spin_lock_irq(&nvmeq->q_lock); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 911 | if (req->cmd_type == REQ_TYPE_DRV_PRIV) |
| 912 | nvme_submit_priv(nvmeq, req, iod); |
| 913 | else if (req->cmd_flags & REQ_DISCARD) |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 914 | nvme_submit_discard(nvmeq, ns, req, iod); |
| 915 | else if (req->cmd_flags & REQ_FLUSH) |
| 916 | nvme_submit_flush(nvmeq, ns, req->tag); |
| 917 | else |
| 918 | nvme_submit_iod(nvmeq, iod, ns); |
| 919 | |
| 920 | nvme_process_cq(nvmeq); |
| 921 | spin_unlock_irq(&nvmeq->q_lock); |
| 922 | return BLK_MQ_RQ_QUEUE_OK; |
| 923 | |
Jens Axboe | fe54303 | 2014-12-11 13:58:39 -0700 | [diff] [blame] | 924 | error_cmd: |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 925 | nvme_free_iod(dev, iod); |
Jens Axboe | fe54303 | 2014-12-11 13:58:39 -0700 | [diff] [blame] | 926 | return BLK_MQ_RQ_QUEUE_ERROR; |
| 927 | retry_cmd: |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 928 | nvme_free_iod(dev, iod); |
Jens Axboe | fe54303 | 2014-12-11 13:58:39 -0700 | [diff] [blame] | 929 | return BLK_MQ_RQ_QUEUE_BUSY; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 930 | } |
| 931 | |
Matthew Wilcox | e9539f4 | 2013-06-24 11:47:34 -0400 | [diff] [blame] | 932 | static int nvme_process_cq(struct nvme_queue *nvmeq) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 933 | { |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 934 | u16 head, phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 935 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 936 | head = nvmeq->cq_head; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 937 | phase = nvmeq->cq_phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 938 | |
| 939 | for (;;) { |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 940 | void *ctx; |
| 941 | nvme_completion_fn fn; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 942 | struct nvme_completion cqe = nvmeq->cqes[head]; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 943 | if ((le16_to_cpu(cqe.status) & 1) != phase) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 944 | break; |
| 945 | nvmeq->sq_head = le16_to_cpu(cqe.sq_head); |
| 946 | if (++head == nvmeq->q_depth) { |
| 947 | head = 0; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 948 | phase = !phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 949 | } |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 950 | ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn); |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 951 | fn(nvmeq, ctx, &cqe); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /* If the controller ignores the cq head doorbell and continuously |
| 955 | * writes to the queue, it is theoretically possible to wrap around |
| 956 | * the queue twice and mistakenly return IRQ_NONE. Linux only |
| 957 | * requires that 0.1% of your interrupts are handled, so this isn't |
| 958 | * a big problem. |
| 959 | */ |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 960 | if (head == nvmeq->cq_head && phase == nvmeq->cq_phase) |
Matthew Wilcox | e9539f4 | 2013-06-24 11:47:34 -0400 | [diff] [blame] | 961 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 962 | |
Haiyan Hu | b80d5cc | 2013-09-10 11:25:37 +0800 | [diff] [blame] | 963 | writel(head, nvmeq->q_db + nvmeq->dev->db_stride); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 964 | nvmeq->cq_head = head; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 965 | nvmeq->cq_phase = phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 966 | |
Matthew Wilcox | e9539f4 | 2013-06-24 11:47:34 -0400 | [diff] [blame] | 967 | nvmeq->cqe_seen = 1; |
| 968 | return 1; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | static irqreturn_t nvme_irq(int irq, void *data) |
| 972 | { |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 973 | irqreturn_t result; |
| 974 | struct nvme_queue *nvmeq = data; |
| 975 | spin_lock(&nvmeq->q_lock); |
Matthew Wilcox | e9539f4 | 2013-06-24 11:47:34 -0400 | [diff] [blame] | 976 | nvme_process_cq(nvmeq); |
| 977 | result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE; |
| 978 | nvmeq->cqe_seen = 0; |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 979 | spin_unlock(&nvmeq->q_lock); |
| 980 | return result; |
| 981 | } |
| 982 | |
| 983 | static irqreturn_t nvme_irq_check(int irq, void *data) |
| 984 | { |
| 985 | struct nvme_queue *nvmeq = data; |
| 986 | struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head]; |
| 987 | if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase) |
| 988 | return IRQ_NONE; |
| 989 | return IRQ_WAKE_THREAD; |
| 990 | } |
| 991 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 992 | /* |
| 993 | * Returns 0 on success. If the result is negative, it's a Linux error code; |
| 994 | * if the result is positive, it's an NVM Express status code |
| 995 | */ |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 996 | int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, |
| 997 | void *buffer, void __user *ubuffer, unsigned bufflen, |
| 998 | u32 *result, unsigned timeout) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 999 | { |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1000 | bool write = cmd->common.opcode & 1; |
| 1001 | struct bio *bio = NULL; |
Christoph Hellwig | f705f83 | 2015-05-22 11:12:38 +0200 | [diff] [blame] | 1002 | struct request *req; |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1003 | int ret; |
Christoph Hellwig | f705f83 | 2015-05-22 11:12:38 +0200 | [diff] [blame] | 1004 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1005 | req = blk_mq_alloc_request(q, write, GFP_KERNEL, false); |
Christoph Hellwig | f705f83 | 2015-05-22 11:12:38 +0200 | [diff] [blame] | 1006 | if (IS_ERR(req)) |
| 1007 | return PTR_ERR(req); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1008 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1009 | req->cmd_type = REQ_TYPE_DRV_PRIV; |
| 1010 | req->__data_len = 0; |
| 1011 | req->__sector = (sector_t) -1; |
| 1012 | req->bio = req->biotail = NULL; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1013 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1014 | req->timeout = ADMIN_TIMEOUT; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1015 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1016 | req->cmd = (unsigned char *)cmd; |
| 1017 | req->cmd_len = sizeof(struct nvme_command); |
| 1018 | req->sense = NULL; |
| 1019 | req->sense_len = 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1020 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1021 | if (buffer && bufflen) { |
| 1022 | ret = blk_rq_map_kern(q, req, buffer, bufflen, __GFP_WAIT); |
| 1023 | if (ret) |
| 1024 | goto out; |
| 1025 | } else if (ubuffer && bufflen) { |
| 1026 | ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen, __GFP_WAIT); |
| 1027 | if (ret) |
| 1028 | goto out; |
| 1029 | bio = req->bio; |
| 1030 | } |
Matthew Wilcox | 3c0cf13 | 2011-02-04 16:03:56 -0500 | [diff] [blame] | 1031 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1032 | blk_execute_rq(req->q, NULL, req, 0); |
| 1033 | if (bio) |
| 1034 | blk_rq_unmap_user(bio); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1035 | if (result) |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1036 | *result = req->sense_len; |
| 1037 | ret = req->errors; |
| 1038 | out: |
Christoph Hellwig | f705f83 | 2015-05-22 11:12:38 +0200 | [diff] [blame] | 1039 | blk_mq_free_request(req); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1040 | return ret; |
Christoph Hellwig | f705f83 | 2015-05-22 11:12:38 +0200 | [diff] [blame] | 1041 | } |
| 1042 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1043 | int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, |
| 1044 | void *buffer, unsigned bufflen) |
Christoph Hellwig | f705f83 | 2015-05-22 11:12:38 +0200 | [diff] [blame] | 1045 | { |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1046 | return __nvme_submit_sync_cmd(q, cmd, buffer, NULL, bufflen, NULL, 0); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1047 | } |
| 1048 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1049 | static int nvme_submit_async_admin_req(struct nvme_dev *dev) |
| 1050 | { |
| 1051 | struct nvme_queue *nvmeq = dev->queues[0]; |
| 1052 | struct nvme_command c; |
| 1053 | struct nvme_cmd_info *cmd_info; |
| 1054 | struct request *req; |
| 1055 | |
Keith Busch | 1efccc9 | 2015-03-31 10:37:17 -0600 | [diff] [blame] | 1056 | req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC, true); |
Dan Carpenter | 9f173b3 | 2014-11-05 23:39:09 +0300 | [diff] [blame] | 1057 | if (IS_ERR(req)) |
| 1058 | return PTR_ERR(req); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1059 | |
Keith Busch | c917dfe | 2015-01-07 18:55:48 -0700 | [diff] [blame] | 1060 | req->cmd_flags |= REQ_NO_TIMEOUT; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1061 | cmd_info = blk_mq_rq_to_pdu(req); |
Keith Busch | 1efccc9 | 2015-03-31 10:37:17 -0600 | [diff] [blame] | 1062 | nvme_set_info(cmd_info, NULL, async_req_completion); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1063 | |
| 1064 | memset(&c, 0, sizeof(c)); |
| 1065 | c.common.opcode = nvme_admin_async_event; |
| 1066 | c.common.command_id = req->tag; |
| 1067 | |
Keith Busch | 1efccc9 | 2015-03-31 10:37:17 -0600 | [diff] [blame] | 1068 | blk_mq_free_hctx_request(nvmeq->hctx, req); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1069 | return __nvme_submit_cmd(nvmeq, &c); |
| 1070 | } |
| 1071 | |
| 1072 | static int nvme_submit_admin_async_cmd(struct nvme_dev *dev, |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1073 | struct nvme_command *cmd, |
| 1074 | struct async_cmd_info *cmdinfo, unsigned timeout) |
| 1075 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1076 | struct nvme_queue *nvmeq = dev->queues[0]; |
| 1077 | struct request *req; |
| 1078 | struct nvme_cmd_info *cmd_rq; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1079 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1080 | req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false); |
Dan Carpenter | 9f173b3 | 2014-11-05 23:39:09 +0300 | [diff] [blame] | 1081 | if (IS_ERR(req)) |
| 1082 | return PTR_ERR(req); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1083 | |
| 1084 | req->timeout = timeout; |
| 1085 | cmd_rq = blk_mq_rq_to_pdu(req); |
| 1086 | cmdinfo->req = req; |
| 1087 | nvme_set_info(cmd_rq, cmdinfo, async_completion); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1088 | cmdinfo->status = -EINTR; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1089 | |
| 1090 | cmd->common.command_id = req->tag; |
| 1091 | |
Keith Busch | 4f5099a | 2014-03-03 16:39:13 -0700 | [diff] [blame] | 1092 | return nvme_submit_cmd(nvmeq, cmd); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1095 | static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) |
| 1096 | { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1097 | struct nvme_command c; |
| 1098 | |
| 1099 | memset(&c, 0, sizeof(c)); |
| 1100 | c.delete_queue.opcode = opcode; |
| 1101 | c.delete_queue.qid = cpu_to_le16(id); |
| 1102 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1103 | return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid, |
| 1107 | struct nvme_queue *nvmeq) |
| 1108 | { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1109 | struct nvme_command c; |
| 1110 | int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED; |
| 1111 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1112 | /* |
| 1113 | * Note: we (ab)use the fact the the prp fields survive if no data |
| 1114 | * is attached to the request. |
| 1115 | */ |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1116 | memset(&c, 0, sizeof(c)); |
| 1117 | c.create_cq.opcode = nvme_admin_create_cq; |
| 1118 | c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr); |
| 1119 | c.create_cq.cqid = cpu_to_le16(qid); |
| 1120 | c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1); |
| 1121 | c.create_cq.cq_flags = cpu_to_le16(flags); |
| 1122 | c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector); |
| 1123 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1124 | return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid, |
| 1128 | struct nvme_queue *nvmeq) |
| 1129 | { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1130 | struct nvme_command c; |
| 1131 | int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM; |
| 1132 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1133 | /* |
| 1134 | * Note: we (ab)use the fact the the prp fields survive if no data |
| 1135 | * is attached to the request. |
| 1136 | */ |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1137 | memset(&c, 0, sizeof(c)); |
| 1138 | c.create_sq.opcode = nvme_admin_create_sq; |
| 1139 | c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr); |
| 1140 | c.create_sq.sqid = cpu_to_le16(qid); |
| 1141 | c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1); |
| 1142 | c.create_sq.sq_flags = cpu_to_le16(flags); |
| 1143 | c.create_sq.cqid = cpu_to_le16(qid); |
| 1144 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1145 | return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid) |
| 1149 | { |
| 1150 | return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid); |
| 1151 | } |
| 1152 | |
| 1153 | static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid) |
| 1154 | { |
| 1155 | return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid); |
| 1156 | } |
| 1157 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1158 | int nvme_identify_ctrl(struct nvme_dev *dev, struct nvme_id_ctrl **id) |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1159 | { |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1160 | struct nvme_command c = { |
| 1161 | .identify.opcode = nvme_admin_identify, |
| 1162 | .identify.cns = cpu_to_le32(1), |
| 1163 | }; |
| 1164 | int error; |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1165 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1166 | *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL); |
| 1167 | if (!*id) |
| 1168 | return -ENOMEM; |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1169 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1170 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, |
| 1171 | sizeof(struct nvme_id_ctrl)); |
| 1172 | if (error) |
| 1173 | kfree(*id); |
| 1174 | return error; |
| 1175 | } |
| 1176 | |
| 1177 | int nvme_identify_ns(struct nvme_dev *dev, unsigned nsid, |
| 1178 | struct nvme_id_ns **id) |
| 1179 | { |
| 1180 | struct nvme_command c = { |
| 1181 | .identify.opcode = nvme_admin_identify, |
| 1182 | .identify.nsid = cpu_to_le32(nsid), |
| 1183 | }; |
| 1184 | int error; |
| 1185 | |
| 1186 | *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL); |
| 1187 | if (!*id) |
| 1188 | return -ENOMEM; |
| 1189 | |
| 1190 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, |
| 1191 | sizeof(struct nvme_id_ns)); |
| 1192 | if (error) |
| 1193 | kfree(*id); |
| 1194 | return error; |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1195 | } |
| 1196 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1197 | int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid, |
Keith Busch | 08df1e0 | 2012-09-21 10:52:13 -0600 | [diff] [blame] | 1198 | dma_addr_t dma_addr, u32 *result) |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1199 | { |
| 1200 | struct nvme_command c; |
| 1201 | |
| 1202 | memset(&c, 0, sizeof(c)); |
| 1203 | c.features.opcode = nvme_admin_get_features; |
Keith Busch | a42cecc | 2012-07-25 16:06:38 -0600 | [diff] [blame] | 1204 | c.features.nsid = cpu_to_le32(nsid); |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1205 | c.features.prp1 = cpu_to_le64(dma_addr); |
| 1206 | c.features.fid = cpu_to_le32(fid); |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1207 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1208 | return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, NULL, 0, |
| 1209 | result, 0); |
Matthew Wilcox | df34813 | 2012-01-11 07:29:56 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1212 | int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11, |
| 1213 | dma_addr_t dma_addr, u32 *result) |
Matthew Wilcox | df34813 | 2012-01-11 07:29:56 -0700 | [diff] [blame] | 1214 | { |
| 1215 | struct nvme_command c; |
| 1216 | |
| 1217 | memset(&c, 0, sizeof(c)); |
| 1218 | c.features.opcode = nvme_admin_set_features; |
| 1219 | c.features.prp1 = cpu_to_le64(dma_addr); |
| 1220 | c.features.fid = cpu_to_le32(fid); |
| 1221 | c.features.dword11 = cpu_to_le32(dword11); |
| 1222 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1223 | return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, NULL, 0, |
| 1224 | result, 0); |
| 1225 | } |
| 1226 | |
| 1227 | int nvme_get_log_page(struct nvme_dev *dev, struct nvme_smart_log **log) |
| 1228 | { |
| 1229 | struct nvme_command c = { |
| 1230 | .common.opcode = nvme_admin_get_log_page, |
| 1231 | .common.nsid = cpu_to_le32(0xFFFFFFFF), |
| 1232 | .common.cdw10[0] = cpu_to_le32( |
| 1233 | (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) | |
| 1234 | NVME_LOG_SMART), |
| 1235 | }; |
| 1236 | int error; |
| 1237 | |
| 1238 | *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL); |
| 1239 | if (!*log) |
| 1240 | return -ENOMEM; |
| 1241 | |
| 1242 | error = nvme_submit_sync_cmd(dev->admin_q, &c, *log, |
| 1243 | sizeof(struct nvme_smart_log)); |
| 1244 | if (error) |
| 1245 | kfree(*log); |
| 1246 | return error; |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 1247 | } |
| 1248 | |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1249 | /** |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1250 | * nvme_abort_req - Attempt aborting a request |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1251 | * |
| 1252 | * Schedule controller reset if the command was already aborted once before and |
| 1253 | * still hasn't been returned to the driver, or if this is the admin queue. |
| 1254 | */ |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1255 | static void nvme_abort_req(struct request *req) |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1256 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1257 | struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req); |
| 1258 | struct nvme_queue *nvmeq = cmd_rq->nvmeq; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1259 | struct nvme_dev *dev = nvmeq->dev; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1260 | struct request *abort_req; |
| 1261 | struct nvme_cmd_info *abort_cmd; |
| 1262 | struct nvme_command cmd; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1263 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1264 | if (!nvmeq->qid || cmd_rq->aborted) { |
Keith Busch | 7a509a6 | 2015-01-07 18:55:53 -0700 | [diff] [blame] | 1265 | unsigned long flags; |
| 1266 | |
| 1267 | spin_lock_irqsave(&dev_list_lock, flags); |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1268 | if (work_busy(&dev->reset_work)) |
Keith Busch | 7a509a6 | 2015-01-07 18:55:53 -0700 | [diff] [blame] | 1269 | goto out; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1270 | list_del_init(&dev->node); |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1271 | dev_warn(dev->dev, "I/O %d QID %d timeout, reset controller\n", |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1272 | req->tag, nvmeq->qid); |
Tejun Heo | 9ca9737 | 2014-03-07 10:24:49 -0500 | [diff] [blame] | 1273 | dev->reset_workfn = nvme_reset_failed_dev; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1274 | queue_work(nvme_workq, &dev->reset_work); |
Keith Busch | 7a509a6 | 2015-01-07 18:55:53 -0700 | [diff] [blame] | 1275 | out: |
| 1276 | spin_unlock_irqrestore(&dev_list_lock, flags); |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1277 | return; |
| 1278 | } |
| 1279 | |
| 1280 | if (!dev->abort_limit) |
| 1281 | return; |
| 1282 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1283 | abort_req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC, |
| 1284 | false); |
Dan Carpenter | 9f173b3 | 2014-11-05 23:39:09 +0300 | [diff] [blame] | 1285 | if (IS_ERR(abort_req)) |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1286 | return; |
| 1287 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1288 | abort_cmd = blk_mq_rq_to_pdu(abort_req); |
| 1289 | nvme_set_info(abort_cmd, abort_req, abort_completion); |
| 1290 | |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1291 | memset(&cmd, 0, sizeof(cmd)); |
| 1292 | cmd.abort.opcode = nvme_admin_abort_cmd; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1293 | cmd.abort.cid = req->tag; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1294 | cmd.abort.sqid = cpu_to_le16(nvmeq->qid); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1295 | cmd.abort.command_id = abort_req->tag; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1296 | |
| 1297 | --dev->abort_limit; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1298 | cmd_rq->aborted = 1; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1299 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1300 | dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag, |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1301 | nvmeq->qid); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1302 | if (nvme_submit_cmd(dev->queues[0], &cmd) < 0) { |
| 1303 | dev_warn(nvmeq->q_dmadev, |
| 1304 | "Could not abort I/O %d QID %d", |
| 1305 | req->tag, nvmeq->qid); |
Sam Bradshaw | c87fd54 | 2014-12-10 13:00:31 -0700 | [diff] [blame] | 1306 | blk_mq_free_request(abort_req); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1307 | } |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1310 | static void nvme_cancel_queue_ios(struct blk_mq_hw_ctx *hctx, |
| 1311 | struct request *req, void *data, bool reserved) |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1312 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1313 | struct nvme_queue *nvmeq = data; |
| 1314 | void *ctx; |
| 1315 | nvme_completion_fn fn; |
| 1316 | struct nvme_cmd_info *cmd; |
Keith Busch | cef6a94 | 2015-01-07 18:55:51 -0700 | [diff] [blame] | 1317 | struct nvme_completion cqe; |
| 1318 | |
| 1319 | if (!blk_mq_request_started(req)) |
| 1320 | return; |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1321 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1322 | cmd = blk_mq_rq_to_pdu(req); |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1323 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1324 | if (cmd->ctx == CMD_CTX_CANCELLED) |
| 1325 | return; |
| 1326 | |
Keith Busch | cef6a94 | 2015-01-07 18:55:51 -0700 | [diff] [blame] | 1327 | if (blk_queue_dying(req->q)) |
| 1328 | cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1); |
| 1329 | else |
| 1330 | cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1); |
| 1331 | |
| 1332 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1333 | dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", |
| 1334 | req->tag, nvmeq->qid); |
| 1335 | ctx = cancel_cmd_info(cmd, &fn); |
| 1336 | fn(nvmeq, ctx, &cqe); |
| 1337 | } |
| 1338 | |
| 1339 | static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved) |
| 1340 | { |
| 1341 | struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req); |
| 1342 | struct nvme_queue *nvmeq = cmd->nvmeq; |
| 1343 | |
Keith Busch | 07836e6 | 2015-02-19 10:34:48 -0700 | [diff] [blame] | 1344 | dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag, |
| 1345 | nvmeq->qid); |
| 1346 | spin_lock_irq(&nvmeq->q_lock); |
| 1347 | nvme_abort_req(req); |
| 1348 | spin_unlock_irq(&nvmeq->q_lock); |
| 1349 | |
Keith Busch | 7a509a6 | 2015-01-07 18:55:53 -0700 | [diff] [blame] | 1350 | /* |
| 1351 | * The aborted req will be completed on receiving the abort req. |
| 1352 | * We enable the timer again. If hit twice, it'll cause a device reset, |
| 1353 | * as the device then is in a faulty state. |
| 1354 | */ |
Keith Busch | 07836e6 | 2015-02-19 10:34:48 -0700 | [diff] [blame] | 1355 | return BLK_EH_RESET_TIMER; |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1356 | } |
| 1357 | |
Keith Busch | f435c28 | 2014-07-07 09:14:42 -0600 | [diff] [blame] | 1358 | static void nvme_free_queue(struct nvme_queue *nvmeq) |
Matthew Wilcox | 9e86677 | 2012-08-03 13:55:56 -0400 | [diff] [blame] | 1359 | { |
| 1360 | dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), |
| 1361 | (void *)nvmeq->cqes, nvmeq->cq_dma_addr); |
| 1362 | dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), |
| 1363 | nvmeq->sq_cmds, nvmeq->sq_dma_addr); |
| 1364 | kfree(nvmeq); |
| 1365 | } |
| 1366 | |
Keith Busch | a1a5ef9 | 2013-12-16 13:50:00 -0500 | [diff] [blame] | 1367 | static void nvme_free_queues(struct nvme_dev *dev, int lowest) |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1368 | { |
| 1369 | int i; |
| 1370 | |
Keith Busch | a1a5ef9 | 2013-12-16 13:50:00 -0500 | [diff] [blame] | 1371 | for (i = dev->queue_count - 1; i >= lowest; i--) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1372 | struct nvme_queue *nvmeq = dev->queues[i]; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1373 | dev->queue_count--; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1374 | dev->queues[i] = NULL; |
Keith Busch | f435c28 | 2014-07-07 09:14:42 -0600 | [diff] [blame] | 1375 | nvme_free_queue(nvmeq); |
kaoudis | 121c7ad | 2015-01-14 21:01:58 -0700 | [diff] [blame] | 1376 | } |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1377 | } |
| 1378 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1379 | /** |
| 1380 | * nvme_suspend_queue - put queue into suspended state |
| 1381 | * @nvmeq - queue to suspend |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1382 | */ |
| 1383 | static int nvme_suspend_queue(struct nvme_queue *nvmeq) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1384 | { |
Keith Busch | 2b25d98 | 2014-12-22 12:59:04 -0700 | [diff] [blame] | 1385 | int vector; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1386 | |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1387 | spin_lock_irq(&nvmeq->q_lock); |
Keith Busch | 2b25d98 | 2014-12-22 12:59:04 -0700 | [diff] [blame] | 1388 | if (nvmeq->cq_vector == -1) { |
| 1389 | spin_unlock_irq(&nvmeq->q_lock); |
| 1390 | return 1; |
| 1391 | } |
| 1392 | vector = nvmeq->dev->entry[nvmeq->cq_vector].vector; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1393 | nvmeq->dev->online_queues--; |
Keith Busch | 2b25d98 | 2014-12-22 12:59:04 -0700 | [diff] [blame] | 1394 | nvmeq->cq_vector = -1; |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1395 | spin_unlock_irq(&nvmeq->q_lock); |
| 1396 | |
Keith Busch | 6df3dbc | 2015-03-26 13:49:33 -0600 | [diff] [blame] | 1397 | if (!nvmeq->qid && nvmeq->dev->admin_q) |
| 1398 | blk_mq_freeze_queue_start(nvmeq->dev->admin_q); |
| 1399 | |
Matthew Wilcox | aba2080 | 2011-03-27 08:52:06 -0400 | [diff] [blame] | 1400 | irq_set_affinity_hint(vector, NULL); |
| 1401 | free_irq(vector, nvmeq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1402 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1403 | return 0; |
| 1404 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1405 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1406 | static void nvme_clear_queue(struct nvme_queue *nvmeq) |
| 1407 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1408 | struct blk_mq_hw_ctx *hctx = nvmeq->hctx; |
| 1409 | |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1410 | spin_lock_irq(&nvmeq->q_lock); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1411 | if (hctx && hctx->tags) |
| 1412 | blk_mq_tag_busy_iter(hctx, nvme_cancel_queue_ios, nvmeq); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1413 | spin_unlock_irq(&nvmeq->q_lock); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1414 | } |
| 1415 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1416 | static void nvme_disable_queue(struct nvme_dev *dev, int qid) |
| 1417 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1418 | struct nvme_queue *nvmeq = dev->queues[qid]; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1419 | |
| 1420 | if (!nvmeq) |
| 1421 | return; |
| 1422 | if (nvme_suspend_queue(nvmeq)) |
| 1423 | return; |
| 1424 | |
Keith Busch | 0e53d18 | 2013-12-10 13:10:39 -0700 | [diff] [blame] | 1425 | /* Don't tell the adapter to delete the admin queue. |
| 1426 | * Don't tell a removed adapter to delete IO queues. */ |
| 1427 | if (qid && readl(&dev->bar->csts) != -1) { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1428 | adapter_delete_sq(dev, qid); |
| 1429 | adapter_delete_cq(dev, qid); |
| 1430 | } |
Keith Busch | 07836e6 | 2015-02-19 10:34:48 -0700 | [diff] [blame] | 1431 | |
| 1432 | spin_lock_irq(&nvmeq->q_lock); |
| 1433 | nvme_process_cq(nvmeq); |
| 1434 | spin_unlock_irq(&nvmeq->q_lock); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid, |
Keith Busch | 2b25d98 | 2014-12-22 12:59:04 -0700 | [diff] [blame] | 1438 | int depth) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1439 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1440 | struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1441 | if (!nvmeq) |
| 1442 | return NULL; |
| 1443 | |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1444 | nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth), |
Joe Perches | 4d51abf | 2014-06-15 13:37:33 -0700 | [diff] [blame] | 1445 | &nvmeq->cq_dma_addr, GFP_KERNEL); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1446 | if (!nvmeq->cqes) |
| 1447 | goto free_nvmeq; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1448 | |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1449 | nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth), |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1450 | &nvmeq->sq_dma_addr, GFP_KERNEL); |
| 1451 | if (!nvmeq->sq_cmds) |
| 1452 | goto free_cqdma; |
| 1453 | |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1454 | nvmeq->q_dmadev = dev->dev; |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 1455 | nvmeq->dev = dev; |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 1456 | snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d", |
| 1457 | dev->instance, qid); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1458 | spin_lock_init(&nvmeq->q_lock); |
| 1459 | nvmeq->cq_head = 0; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 1460 | nvmeq->cq_phase = 1; |
Haiyan Hu | b80d5cc | 2013-09-10 11:25:37 +0800 | [diff] [blame] | 1461 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1462 | nvmeq->q_depth = depth; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1463 | nvmeq->qid = qid; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1464 | dev->queue_count++; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1465 | dev->queues[qid] = nvmeq; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1466 | |
| 1467 | return nvmeq; |
| 1468 | |
| 1469 | free_cqdma: |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1470 | dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1471 | nvmeq->cq_dma_addr); |
| 1472 | free_nvmeq: |
| 1473 | kfree(nvmeq); |
| 1474 | return NULL; |
| 1475 | } |
| 1476 | |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 1477 | static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq, |
| 1478 | const char *name) |
| 1479 | { |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 1480 | if (use_threaded_interrupts) |
| 1481 | return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector, |
Michael Opdenacker | 481e5ba | 2013-10-12 06:23:29 +0200 | [diff] [blame] | 1482 | nvme_irq_check, nvme_irq, IRQF_SHARED, |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 1483 | name, nvmeq); |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 1484 | return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq, |
Michael Opdenacker | 481e5ba | 2013-10-12 06:23:29 +0200 | [diff] [blame] | 1485 | IRQF_SHARED, name, nvmeq); |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 1486 | } |
| 1487 | |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1488 | static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1489 | { |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1490 | struct nvme_dev *dev = nvmeq->dev; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1491 | |
Keith Busch | 7be50e9 | 2014-09-10 15:48:47 -0600 | [diff] [blame] | 1492 | spin_lock_irq(&nvmeq->q_lock); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1493 | nvmeq->sq_tail = 0; |
| 1494 | nvmeq->cq_head = 0; |
| 1495 | nvmeq->cq_phase = 1; |
Haiyan Hu | b80d5cc | 2013-09-10 11:25:37 +0800 | [diff] [blame] | 1496 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1497 | memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth)); |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1498 | dev->online_queues++; |
Keith Busch | 7be50e9 | 2014-09-10 15:48:47 -0600 | [diff] [blame] | 1499 | spin_unlock_irq(&nvmeq->q_lock); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | static int nvme_create_queue(struct nvme_queue *nvmeq, int qid) |
| 1503 | { |
| 1504 | struct nvme_dev *dev = nvmeq->dev; |
| 1505 | int result; |
Matthew Wilcox | 3f85d50 | 2011-02-01 08:39:04 -0500 | [diff] [blame] | 1506 | |
Keith Busch | 2b25d98 | 2014-12-22 12:59:04 -0700 | [diff] [blame] | 1507 | nvmeq->cq_vector = qid - 1; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1508 | result = adapter_alloc_cq(dev, qid, nvmeq); |
| 1509 | if (result < 0) |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1510 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1511 | |
| 1512 | result = adapter_alloc_sq(dev, qid, nvmeq); |
| 1513 | if (result < 0) |
| 1514 | goto release_cq; |
| 1515 | |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 1516 | result = queue_request_irq(dev, nvmeq, nvmeq->irqname); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1517 | if (result < 0) |
| 1518 | goto release_sq; |
| 1519 | |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1520 | nvme_init_queue(nvmeq, qid); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1521 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1522 | |
| 1523 | release_sq: |
| 1524 | adapter_delete_sq(dev, qid); |
| 1525 | release_cq: |
| 1526 | adapter_delete_cq(dev, qid); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1527 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1528 | } |
| 1529 | |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1530 | static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled) |
| 1531 | { |
| 1532 | unsigned long timeout; |
| 1533 | u32 bit = enabled ? NVME_CSTS_RDY : 0; |
| 1534 | |
| 1535 | timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies; |
| 1536 | |
| 1537 | while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) { |
| 1538 | msleep(100); |
| 1539 | if (fatal_signal_pending(current)) |
| 1540 | return -EINTR; |
| 1541 | if (time_after(jiffies, timeout)) { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1542 | dev_err(dev->dev, |
Matthew Wilcox | 27e8166 | 2014-04-11 11:58:45 -0400 | [diff] [blame] | 1543 | "Device not ready; aborting %s\n", enabled ? |
| 1544 | "initialisation" : "reset"); |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1545 | return -ENODEV; |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
| 1552 | /* |
| 1553 | * If the device has been passed off to us in an enabled state, just clear |
| 1554 | * the enabled bit. The spec says we should set the 'shutdown notification |
| 1555 | * bits', but doing so may cause the device to complete commands to the |
| 1556 | * admin queue ... and we don't know what memory that might be pointing at! |
| 1557 | */ |
| 1558 | static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap) |
| 1559 | { |
Dan McLeran | 0107952 | 2014-06-23 08:24:36 -0600 | [diff] [blame] | 1560 | dev->ctrl_config &= ~NVME_CC_SHN_MASK; |
| 1561 | dev->ctrl_config &= ~NVME_CC_ENABLE; |
| 1562 | writel(dev->ctrl_config, &dev->bar->cc); |
Matthew Wilcox | 44af146 | 2013-05-04 06:43:17 -0400 | [diff] [blame] | 1563 | |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1564 | return nvme_wait_ready(dev, cap, false); |
| 1565 | } |
| 1566 | |
| 1567 | static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap) |
| 1568 | { |
Dan McLeran | 0107952 | 2014-06-23 08:24:36 -0600 | [diff] [blame] | 1569 | dev->ctrl_config &= ~NVME_CC_SHN_MASK; |
| 1570 | dev->ctrl_config |= NVME_CC_ENABLE; |
| 1571 | writel(dev->ctrl_config, &dev->bar->cc); |
| 1572 | |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1573 | return nvme_wait_ready(dev, cap, true); |
| 1574 | } |
| 1575 | |
Keith Busch | 1894d8f | 2013-07-15 15:02:22 -0600 | [diff] [blame] | 1576 | static int nvme_shutdown_ctrl(struct nvme_dev *dev) |
| 1577 | { |
| 1578 | unsigned long timeout; |
Keith Busch | 1894d8f | 2013-07-15 15:02:22 -0600 | [diff] [blame] | 1579 | |
Dan McLeran | 0107952 | 2014-06-23 08:24:36 -0600 | [diff] [blame] | 1580 | dev->ctrl_config &= ~NVME_CC_SHN_MASK; |
| 1581 | dev->ctrl_config |= NVME_CC_SHN_NORMAL; |
| 1582 | |
| 1583 | writel(dev->ctrl_config, &dev->bar->cc); |
Keith Busch | 1894d8f | 2013-07-15 15:02:22 -0600 | [diff] [blame] | 1584 | |
Dan McLeran | 2484f40 | 2014-07-01 09:33:32 -0600 | [diff] [blame] | 1585 | timeout = SHUTDOWN_TIMEOUT + jiffies; |
Keith Busch | 1894d8f | 2013-07-15 15:02:22 -0600 | [diff] [blame] | 1586 | while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) != |
| 1587 | NVME_CSTS_SHST_CMPLT) { |
| 1588 | msleep(100); |
| 1589 | if (fatal_signal_pending(current)) |
| 1590 | return -EINTR; |
| 1591 | if (time_after(jiffies, timeout)) { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1592 | dev_err(dev->dev, |
Keith Busch | 1894d8f | 2013-07-15 15:02:22 -0600 | [diff] [blame] | 1593 | "Device shutdown incomplete; abort shutdown\n"); |
| 1594 | return -ENODEV; |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1601 | static struct blk_mq_ops nvme_mq_admin_ops = { |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1602 | .queue_rq = nvme_queue_rq, |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1603 | .map_queue = blk_mq_map_queue, |
| 1604 | .init_hctx = nvme_admin_init_hctx, |
Jens Axboe | 2c30540b | 2014-11-14 09:47:32 -0700 | [diff] [blame] | 1605 | .exit_hctx = nvme_exit_hctx, |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1606 | .init_request = nvme_admin_init_request, |
| 1607 | .timeout = nvme_timeout, |
| 1608 | }; |
| 1609 | |
| 1610 | static struct blk_mq_ops nvme_mq_ops = { |
| 1611 | .queue_rq = nvme_queue_rq, |
| 1612 | .map_queue = blk_mq_map_queue, |
| 1613 | .init_hctx = nvme_init_hctx, |
Jens Axboe | 2c30540b | 2014-11-14 09:47:32 -0700 | [diff] [blame] | 1614 | .exit_hctx = nvme_exit_hctx, |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1615 | .init_request = nvme_init_request, |
| 1616 | .timeout = nvme_timeout, |
| 1617 | }; |
| 1618 | |
Keith Busch | ea191d2 | 2015-01-07 18:55:49 -0700 | [diff] [blame] | 1619 | static void nvme_dev_remove_admin(struct nvme_dev *dev) |
| 1620 | { |
| 1621 | if (dev->admin_q && !blk_queue_dying(dev->admin_q)) { |
| 1622 | blk_cleanup_queue(dev->admin_q); |
| 1623 | blk_mq_free_tag_set(&dev->admin_tagset); |
| 1624 | } |
| 1625 | } |
| 1626 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1627 | static int nvme_alloc_admin_tags(struct nvme_dev *dev) |
| 1628 | { |
| 1629 | if (!dev->admin_q) { |
| 1630 | dev->admin_tagset.ops = &nvme_mq_admin_ops; |
| 1631 | dev->admin_tagset.nr_hw_queues = 1; |
| 1632 | dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1; |
Keith Busch | 1efccc9 | 2015-03-31 10:37:17 -0600 | [diff] [blame] | 1633 | dev->admin_tagset.reserved_tags = 1; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1634 | dev->admin_tagset.timeout = ADMIN_TIMEOUT; |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1635 | dev->admin_tagset.numa_node = dev_to_node(dev->dev); |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 1636 | dev->admin_tagset.cmd_size = nvme_cmd_size(dev); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1637 | dev->admin_tagset.driver_data = dev; |
| 1638 | |
| 1639 | if (blk_mq_alloc_tag_set(&dev->admin_tagset)) |
| 1640 | return -ENOMEM; |
| 1641 | |
| 1642 | dev->admin_q = blk_mq_init_queue(&dev->admin_tagset); |
Ming Lei | 35b489d | 2015-01-02 14:25:27 +0000 | [diff] [blame] | 1643 | if (IS_ERR(dev->admin_q)) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1644 | blk_mq_free_tag_set(&dev->admin_tagset); |
| 1645 | return -ENOMEM; |
| 1646 | } |
Keith Busch | ea191d2 | 2015-01-07 18:55:49 -0700 | [diff] [blame] | 1647 | if (!blk_get_queue(dev->admin_q)) { |
| 1648 | nvme_dev_remove_admin(dev); |
| 1649 | return -ENODEV; |
| 1650 | } |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 1651 | } else |
| 1652 | blk_mq_unfreeze_queue(dev->admin_q); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1653 | |
| 1654 | return 0; |
| 1655 | } |
| 1656 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1657 | static int nvme_configure_admin_queue(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1658 | { |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1659 | int result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1660 | u32 aqa; |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1661 | u64 cap = readq(&dev->bar->cap); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1662 | struct nvme_queue *nvmeq; |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 1663 | unsigned page_shift = PAGE_SHIFT; |
| 1664 | unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12; |
| 1665 | unsigned dev_page_max = NVME_CAP_MPSMAX(cap) + 12; |
| 1666 | |
| 1667 | if (page_shift < dev_page_min) { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1668 | dev_err(dev->dev, |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 1669 | "Minimum device page size (%u) too large for " |
| 1670 | "host (%u)\n", 1 << dev_page_min, |
| 1671 | 1 << page_shift); |
| 1672 | return -ENODEV; |
| 1673 | } |
| 1674 | if (page_shift > dev_page_max) { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1675 | dev_info(dev->dev, |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 1676 | "Device maximum page size (%u) smaller than " |
| 1677 | "host (%u); enabling work-around\n", |
| 1678 | 1 << dev_page_max, 1 << page_shift); |
| 1679 | page_shift = dev_page_max; |
| 1680 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1681 | |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1682 | result = nvme_disable_ctrl(dev, cap); |
| 1683 | if (result < 0) |
| 1684 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1685 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1686 | nvmeq = dev->queues[0]; |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 1687 | if (!nvmeq) { |
Keith Busch | 2b25d98 | 2014-12-22 12:59:04 -0700 | [diff] [blame] | 1688 | nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH); |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 1689 | if (!nvmeq) |
| 1690 | return -ENOMEM; |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 1691 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1692 | |
| 1693 | aqa = nvmeq->q_depth - 1; |
| 1694 | aqa |= aqa << 16; |
| 1695 | |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 1696 | dev->page_size = 1 << page_shift; |
| 1697 | |
Dan McLeran | 0107952 | 2014-06-23 08:24:36 -0600 | [diff] [blame] | 1698 | dev->ctrl_config = NVME_CC_CSS_NVM; |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 1699 | dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1700 | dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE; |
Matthew Wilcox | 7f53f9d | 2011-03-22 15:55:45 -0400 | [diff] [blame] | 1701 | dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1702 | |
| 1703 | writel(aqa, &dev->bar->aqa); |
| 1704 | writeq(nvmeq->sq_dma_addr, &dev->bar->asq); |
| 1705 | writeq(nvmeq->cq_dma_addr, &dev->bar->acq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1706 | |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1707 | result = nvme_enable_ctrl(dev, cap); |
Keith Busch | 025c557 | 2013-05-01 13:07:51 -0600 | [diff] [blame] | 1708 | if (result) |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1709 | goto free_nvmeq; |
| 1710 | |
Keith Busch | 2b25d98 | 2014-12-22 12:59:04 -0700 | [diff] [blame] | 1711 | nvmeq->cq_vector = 0; |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 1712 | result = queue_request_irq(dev, nvmeq, nvmeq->irqname); |
Keith Busch | 025c557 | 2013-05-01 13:07:51 -0600 | [diff] [blame] | 1713 | if (result) |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 1714 | goto free_nvmeq; |
Keith Busch | 025c557 | 2013-05-01 13:07:51 -0600 | [diff] [blame] | 1715 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1716 | return result; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1717 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1718 | free_nvmeq: |
| 1719 | nvme_free_queues(dev, 0); |
| 1720 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1721 | } |
| 1722 | |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1723 | static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) |
| 1724 | { |
| 1725 | struct nvme_dev *dev = ns->dev; |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1726 | struct nvme_user_io io; |
| 1727 | struct nvme_command c; |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1728 | unsigned length, meta_len; |
Keith Busch | a67a951 | 2015-04-07 16:57:19 -0600 | [diff] [blame] | 1729 | int status, write; |
Keith Busch | a67a951 | 2015-04-07 16:57:19 -0600 | [diff] [blame] | 1730 | dma_addr_t meta_dma = 0; |
| 1731 | void *meta = NULL; |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1732 | |
| 1733 | if (copy_from_user(&io, uio, sizeof(io))) |
| 1734 | return -EFAULT; |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1735 | |
| 1736 | switch (io.opcode) { |
| 1737 | case nvme_cmd_write: |
| 1738 | case nvme_cmd_read: |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1739 | case nvme_cmd_compare: |
Matthew Wilcox | 6413214 | 2011-08-09 12:56:37 -0400 | [diff] [blame] | 1740 | break; |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1741 | default: |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1742 | return -EINVAL; |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1743 | } |
| 1744 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1745 | length = (io.nblocks + 1) << ns->lba_shift; |
| 1746 | meta_len = (io.nblocks + 1) * ns->ms; |
| 1747 | write = io.opcode & 1; |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1748 | |
Keith Busch | a67a951 | 2015-04-07 16:57:19 -0600 | [diff] [blame] | 1749 | if (meta_len) { |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1750 | if (((io.metadata & 3) || !io.metadata) && !ns->ext) |
| 1751 | return -EINVAL; |
| 1752 | |
| 1753 | if (ns->ext) { |
| 1754 | length += meta_len; |
| 1755 | meta_len = 0; |
| 1756 | } |
| 1757 | |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1758 | meta = dma_alloc_coherent(dev->dev, meta_len, |
Keith Busch | a67a951 | 2015-04-07 16:57:19 -0600 | [diff] [blame] | 1759 | &meta_dma, GFP_KERNEL); |
| 1760 | if (!meta) { |
| 1761 | status = -ENOMEM; |
| 1762 | goto unmap; |
| 1763 | } |
| 1764 | if (write) { |
| 1765 | if (copy_from_user(meta, (void __user *)io.metadata, |
| 1766 | meta_len)) { |
| 1767 | status = -EFAULT; |
| 1768 | goto unmap; |
| 1769 | } |
| 1770 | } |
| 1771 | } |
| 1772 | |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1773 | memset(&c, 0, sizeof(c)); |
| 1774 | c.rw.opcode = io.opcode; |
| 1775 | c.rw.flags = io.flags; |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1776 | c.rw.nsid = cpu_to_le32(ns->ns_id); |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1777 | c.rw.slba = cpu_to_le64(io.slba); |
Matthew Wilcox | 6c7d494 | 2011-03-21 09:48:57 -0400 | [diff] [blame] | 1778 | c.rw.length = cpu_to_le16(io.nblocks); |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1779 | c.rw.control = cpu_to_le16(io.control); |
Matthew Wilcox | 1c9b526 | 2013-04-16 15:21:06 -0400 | [diff] [blame] | 1780 | c.rw.dsmgmt = cpu_to_le32(io.dsmgmt); |
| 1781 | c.rw.reftag = cpu_to_le32(io.reftag); |
| 1782 | c.rw.apptag = cpu_to_le16(io.apptag); |
| 1783 | c.rw.appmask = cpu_to_le16(io.appmask); |
Keith Busch | a67a951 | 2015-04-07 16:57:19 -0600 | [diff] [blame] | 1784 | c.rw.metadata = cpu_to_le64(meta_dma); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1785 | |
| 1786 | status = __nvme_submit_sync_cmd(ns->queue, &c, NULL, |
| 1787 | (void __user *)io.addr, length, NULL, 0); |
Keith Busch | f410c68 | 2013-04-23 17:23:59 -0600 | [diff] [blame] | 1788 | unmap: |
Keith Busch | a67a951 | 2015-04-07 16:57:19 -0600 | [diff] [blame] | 1789 | if (meta) { |
| 1790 | if (status == NVME_SC_SUCCESS && !write) { |
| 1791 | if (copy_to_user((void __user *)io.metadata, meta, |
| 1792 | meta_len)) |
| 1793 | status = -EFAULT; |
| 1794 | } |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1795 | dma_free_coherent(dev->dev, meta_len, meta, meta_dma); |
Keith Busch | f410c68 | 2013-04-23 17:23:59 -0600 | [diff] [blame] | 1796 | } |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1797 | return status; |
| 1798 | } |
| 1799 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1800 | static int nvme_user_cmd(struct nvme_dev *dev, struct nvme_ns *ns, |
| 1801 | struct nvme_passthru_cmd __user *ucmd) |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1802 | { |
Keith Busch | 7963e52 | 2014-09-12 16:07:20 -0600 | [diff] [blame] | 1803 | struct nvme_passthru_cmd cmd; |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1804 | struct nvme_command c; |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1805 | unsigned timeout = 0; |
| 1806 | int status; |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1807 | |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1808 | if (!capable(CAP_SYS_ADMIN)) |
| 1809 | return -EACCES; |
| 1810 | if (copy_from_user(&cmd, ucmd, sizeof(cmd))) |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1811 | return -EFAULT; |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1812 | |
| 1813 | memset(&c, 0, sizeof(c)); |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1814 | c.common.opcode = cmd.opcode; |
| 1815 | c.common.flags = cmd.flags; |
| 1816 | c.common.nsid = cpu_to_le32(cmd.nsid); |
| 1817 | c.common.cdw2[0] = cpu_to_le32(cmd.cdw2); |
| 1818 | c.common.cdw2[1] = cpu_to_le32(cmd.cdw3); |
| 1819 | c.common.cdw10[0] = cpu_to_le32(cmd.cdw10); |
| 1820 | c.common.cdw10[1] = cpu_to_le32(cmd.cdw11); |
| 1821 | c.common.cdw10[2] = cpu_to_le32(cmd.cdw12); |
| 1822 | c.common.cdw10[3] = cpu_to_le32(cmd.cdw13); |
| 1823 | c.common.cdw10[4] = cpu_to_le32(cmd.cdw14); |
| 1824 | c.common.cdw10[5] = cpu_to_le32(cmd.cdw15); |
| 1825 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1826 | if (cmd.timeout_ms) |
| 1827 | timeout = msecs_to_jiffies(cmd.timeout_ms); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1828 | |
Christoph Hellwig | f705f83 | 2015-05-22 11:12:38 +0200 | [diff] [blame] | 1829 | status = __nvme_submit_sync_cmd(ns ? ns->queue : dev->admin_q, &c, |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1830 | NULL, (void __user *)cmd.addr, cmd.data_len, |
| 1831 | &cmd.result, timeout); |
| 1832 | if (status >= 0) { |
| 1833 | if (put_user(cmd.result, &ucmd->result)) |
| 1834 | return -EFAULT; |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1835 | } |
Keith Busch | f4f117f | 2012-09-21 10:49:05 -0600 | [diff] [blame] | 1836 | |
Matthew Wilcox | 6ee44cd | 2011-02-03 10:58:26 -0500 | [diff] [blame] | 1837 | return status; |
| 1838 | } |
| 1839 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1840 | static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, |
| 1841 | unsigned long arg) |
| 1842 | { |
| 1843 | struct nvme_ns *ns = bdev->bd_disk->private_data; |
| 1844 | |
| 1845 | switch (cmd) { |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1846 | case NVME_IOCTL_ID: |
Matthew Wilcox | c3bfe71 | 2013-07-08 17:26:25 -0400 | [diff] [blame] | 1847 | force_successful_syscall_return(); |
Matthew Wilcox | 6bbf1ac | 2011-05-20 13:03:42 -0400 | [diff] [blame] | 1848 | return ns->ns_id; |
| 1849 | case NVME_IOCTL_ADMIN_CMD: |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1850 | return nvme_user_cmd(ns->dev, NULL, (void __user *)arg); |
Keith Busch | 7963e52 | 2014-09-12 16:07:20 -0600 | [diff] [blame] | 1851 | case NVME_IOCTL_IO_CMD: |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1852 | return nvme_user_cmd(ns->dev, ns, (void __user *)arg); |
Matthew Wilcox | a53295b | 2011-02-01 16:13:29 -0500 | [diff] [blame] | 1853 | case NVME_IOCTL_SUBMIT_IO: |
| 1854 | return nvme_submit_io(ns, (void __user *)arg); |
Vishal Verma | 5d0f613 | 2013-03-04 18:40:58 -0700 | [diff] [blame] | 1855 | case SG_GET_VERSION_NUM: |
| 1856 | return nvme_sg_get_version_num((void __user *)arg); |
| 1857 | case SG_IO: |
| 1858 | return nvme_sg_io(ns, (void __user *)arg); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1859 | default: |
| 1860 | return -ENOTTY; |
| 1861 | } |
| 1862 | } |
| 1863 | |
Keith Busch | 320a382 | 2013-10-23 13:07:34 -0600 | [diff] [blame] | 1864 | #ifdef CONFIG_COMPAT |
| 1865 | static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 1866 | unsigned int cmd, unsigned long arg) |
| 1867 | { |
Keith Busch | 320a382 | 2013-10-23 13:07:34 -0600 | [diff] [blame] | 1868 | switch (cmd) { |
| 1869 | case SG_IO: |
Keith Busch | e179729 | 2014-08-27 13:55:38 -0600 | [diff] [blame] | 1870 | return -ENOIOCTLCMD; |
Keith Busch | 320a382 | 2013-10-23 13:07:34 -0600 | [diff] [blame] | 1871 | } |
| 1872 | return nvme_ioctl(bdev, mode, cmd, arg); |
| 1873 | } |
| 1874 | #else |
| 1875 | #define nvme_compat_ioctl NULL |
| 1876 | #endif |
| 1877 | |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 1878 | static int nvme_open(struct block_device *bdev, fmode_t mode) |
| 1879 | { |
Keith Busch | 9e60352 | 2014-10-03 11:15:47 -0600 | [diff] [blame] | 1880 | int ret = 0; |
| 1881 | struct nvme_ns *ns; |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 1882 | |
Keith Busch | 9e60352 | 2014-10-03 11:15:47 -0600 | [diff] [blame] | 1883 | spin_lock(&dev_list_lock); |
| 1884 | ns = bdev->bd_disk->private_data; |
| 1885 | if (!ns) |
| 1886 | ret = -ENXIO; |
| 1887 | else if (!kref_get_unless_zero(&ns->dev->kref)) |
| 1888 | ret = -ENXIO; |
| 1889 | spin_unlock(&dev_list_lock); |
| 1890 | |
| 1891 | return ret; |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | static void nvme_free_dev(struct kref *kref); |
| 1895 | |
| 1896 | static void nvme_release(struct gendisk *disk, fmode_t mode) |
| 1897 | { |
| 1898 | struct nvme_ns *ns = disk->private_data; |
| 1899 | struct nvme_dev *dev = ns->dev; |
| 1900 | |
| 1901 | kref_put(&dev->kref, nvme_free_dev); |
| 1902 | } |
| 1903 | |
Keith Busch | 4cc09e2 | 2014-04-02 15:45:37 -0600 | [diff] [blame] | 1904 | static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo) |
| 1905 | { |
| 1906 | /* some standard values */ |
| 1907 | geo->heads = 1 << 6; |
| 1908 | geo->sectors = 1 << 5; |
| 1909 | geo->cylinders = get_capacity(bd->bd_disk) >> 11; |
| 1910 | return 0; |
| 1911 | } |
| 1912 | |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 1913 | static void nvme_config_discard(struct nvme_ns *ns) |
| 1914 | { |
| 1915 | u32 logical_block_size = queue_logical_block_size(ns->queue); |
| 1916 | ns->queue->limits.discard_zeroes_data = 0; |
| 1917 | ns->queue->limits.discard_alignment = logical_block_size; |
| 1918 | ns->queue->limits.discard_granularity = logical_block_size; |
| 1919 | ns->queue->limits.max_discard_sectors = 0xffffffff; |
| 1920 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue); |
| 1921 | } |
| 1922 | |
Keith Busch | 1b9dbf7 | 2014-09-10 17:21:14 -0600 | [diff] [blame] | 1923 | static int nvme_revalidate_disk(struct gendisk *disk) |
| 1924 | { |
| 1925 | struct nvme_ns *ns = disk->private_data; |
| 1926 | struct nvme_dev *dev = ns->dev; |
| 1927 | struct nvme_id_ns *id; |
Keith Busch | a67a951 | 2015-04-07 16:57:19 -0600 | [diff] [blame] | 1928 | u8 lbaf, pi_type; |
| 1929 | u16 old_ms; |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 1930 | unsigned short bs; |
Keith Busch | 1b9dbf7 | 2014-09-10 17:21:14 -0600 | [diff] [blame] | 1931 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1932 | if (nvme_identify_ns(dev, ns->ns_id, &id)) { |
| 1933 | dev_warn(dev->dev, "%s: Identify failure\n", __func__); |
Keith Busch | 1b9dbf7 | 2014-09-10 17:21:14 -0600 | [diff] [blame] | 1934 | return 0; |
| 1935 | } |
| 1936 | |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 1937 | old_ms = ns->ms; |
| 1938 | lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK; |
Keith Busch | 1b9dbf7 | 2014-09-10 17:21:14 -0600 | [diff] [blame] | 1939 | ns->lba_shift = id->lbaf[lbaf].ds; |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 1940 | ns->ms = le16_to_cpu(id->lbaf[lbaf].ms); |
Keith Busch | a67a951 | 2015-04-07 16:57:19 -0600 | [diff] [blame] | 1941 | ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT); |
Keith Busch | 1b9dbf7 | 2014-09-10 17:21:14 -0600 | [diff] [blame] | 1942 | |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 1943 | /* |
| 1944 | * If identify namespace failed, use default 512 byte block size so |
| 1945 | * block layer can use before failing read/write for 0 capacity. |
| 1946 | */ |
| 1947 | if (ns->lba_shift == 0) |
| 1948 | ns->lba_shift = 9; |
| 1949 | bs = 1 << ns->lba_shift; |
| 1950 | |
| 1951 | /* XXX: PI implementation requires metadata equal t10 pi tuple size */ |
| 1952 | pi_type = ns->ms == sizeof(struct t10_pi_tuple) ? |
| 1953 | id->dps & NVME_NS_DPS_PI_MASK : 0; |
| 1954 | |
Keith Busch | 52b68d7 | 2015-02-23 09:16:21 -0700 | [diff] [blame] | 1955 | if (blk_get_integrity(disk) && (ns->pi_type != pi_type || |
| 1956 | ns->ms != old_ms || |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 1957 | bs != queue_logical_block_size(disk->queue) || |
Keith Busch | a67a951 | 2015-04-07 16:57:19 -0600 | [diff] [blame] | 1958 | (ns->ms && ns->ext))) |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 1959 | blk_integrity_unregister(disk); |
| 1960 | |
| 1961 | ns->pi_type = pi_type; |
| 1962 | blk_queue_logical_block_size(ns->queue, bs); |
| 1963 | |
Keith Busch | 52b68d7 | 2015-02-23 09:16:21 -0700 | [diff] [blame] | 1964 | if (ns->ms && !blk_get_integrity(disk) && (disk->flags & GENHD_FL_UP) && |
Keith Busch | a67a951 | 2015-04-07 16:57:19 -0600 | [diff] [blame] | 1965 | !ns->ext) |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 1966 | nvme_init_integrity(ns); |
| 1967 | |
Keith Busch | 52b68d7 | 2015-02-23 09:16:21 -0700 | [diff] [blame] | 1968 | if (id->ncap == 0 || (ns->ms && !blk_get_integrity(disk))) |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 1969 | set_capacity(disk, 0); |
| 1970 | else |
| 1971 | set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9)); |
| 1972 | |
| 1973 | if (dev->oncs & NVME_CTRL_ONCS_DSM) |
| 1974 | nvme_config_discard(ns); |
| 1975 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 1976 | kfree(id); |
Keith Busch | 1b9dbf7 | 2014-09-10 17:21:14 -0600 | [diff] [blame] | 1977 | return 0; |
| 1978 | } |
| 1979 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1980 | static const struct block_device_operations nvme_fops = { |
| 1981 | .owner = THIS_MODULE, |
| 1982 | .ioctl = nvme_ioctl, |
Keith Busch | 320a382 | 2013-10-23 13:07:34 -0600 | [diff] [blame] | 1983 | .compat_ioctl = nvme_compat_ioctl, |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 1984 | .open = nvme_open, |
| 1985 | .release = nvme_release, |
Keith Busch | 4cc09e2 | 2014-04-02 15:45:37 -0600 | [diff] [blame] | 1986 | .getgeo = nvme_getgeo, |
Keith Busch | 1b9dbf7 | 2014-09-10 17:21:14 -0600 | [diff] [blame] | 1987 | .revalidate_disk= nvme_revalidate_disk, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1988 | }; |
| 1989 | |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1990 | static int nvme_kthread(void *data) |
| 1991 | { |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 1992 | struct nvme_dev *dev, *next; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1993 | |
| 1994 | while (!kthread_should_stop()) { |
Arjan van de Ven | 564a232 | 2013-05-01 16:38:23 -0400 | [diff] [blame] | 1995 | set_current_state(TASK_INTERRUPTIBLE); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1996 | spin_lock(&dev_list_lock); |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 1997 | list_for_each_entry_safe(dev, next, &dev_list, node) { |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1998 | int i; |
Keith Busch | 07836e6 | 2015-02-19 10:34:48 -0700 | [diff] [blame] | 1999 | if (readl(&dev->bar->csts) & NVME_CSTS_CFS) { |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 2000 | if (work_busy(&dev->reset_work)) |
| 2001 | continue; |
| 2002 | list_del_init(&dev->node); |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2003 | dev_warn(dev->dev, |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2004 | "Failed status: %x, reset controller\n", |
| 2005 | readl(&dev->bar->csts)); |
Tejun Heo | 9ca9737 | 2014-03-07 10:24:49 -0500 | [diff] [blame] | 2006 | dev->reset_workfn = nvme_reset_failed_dev; |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 2007 | queue_work(nvme_workq, &dev->reset_work); |
| 2008 | continue; |
| 2009 | } |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 2010 | for (i = 0; i < dev->queue_count; i++) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2011 | struct nvme_queue *nvmeq = dev->queues[i]; |
Matthew Wilcox | 740216f | 2011-02-15 16:28:20 -0500 | [diff] [blame] | 2012 | if (!nvmeq) |
| 2013 | continue; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 2014 | spin_lock_irq(&nvmeq->q_lock); |
Matthew Wilcox | bc57a0f | 2013-06-24 11:56:42 -0400 | [diff] [blame] | 2015 | nvme_process_cq(nvmeq); |
Keith Busch | 6fccf93 | 2014-06-18 13:58:57 -0600 | [diff] [blame] | 2016 | |
| 2017 | while ((i == 0) && (dev->event_limit > 0)) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2018 | if (nvme_submit_async_admin_req(dev)) |
Keith Busch | 6fccf93 | 2014-06-18 13:58:57 -0600 | [diff] [blame] | 2019 | break; |
| 2020 | dev->event_limit--; |
| 2021 | } |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 2022 | spin_unlock_irq(&nvmeq->q_lock); |
| 2023 | } |
| 2024 | } |
| 2025 | spin_unlock(&dev_list_lock); |
Arjan van de Ven | acb7aa0 | 2013-02-04 14:44:33 -0800 | [diff] [blame] | 2026 | schedule_timeout(round_jiffies_relative(HZ)); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 2027 | } |
| 2028 | return 0; |
| 2029 | } |
| 2030 | |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 2031 | static void nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2032 | { |
| 2033 | struct nvme_ns *ns; |
| 2034 | struct gendisk *disk; |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2035 | int node = dev_to_node(dev->dev); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2036 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2037 | ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2038 | if (!ns) |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 2039 | return; |
| 2040 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2041 | ns->queue = blk_mq_init_queue(&dev->tagset); |
Dan Carpenter | 9f173b3 | 2014-11-05 23:39:09 +0300 | [diff] [blame] | 2042 | if (IS_ERR(ns->queue)) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2043 | goto out_free_ns; |
Matthew Wilcox | 4eeb921 | 2012-01-10 14:35:08 -0700 | [diff] [blame] | 2044 | queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue); |
| 2045 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2046 | queue_flag_set_unlocked(QUEUE_FLAG_SG_GAPS, ns->queue); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2047 | ns->dev = dev; |
| 2048 | ns->queue->queuedata = ns; |
| 2049 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2050 | disk = alloc_disk_node(0, node); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2051 | if (!disk) |
| 2052 | goto out_free_queue; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2053 | |
Matthew Wilcox | 5aff938 | 2011-05-06 08:45:47 -0400 | [diff] [blame] | 2054 | ns->ns_id = nsid; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2055 | ns->disk = disk; |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 2056 | ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */ |
| 2057 | list_add_tail(&ns->list, &dev->namespaces); |
| 2058 | |
Keith Busch | e9ef463 | 2012-07-24 15:01:04 -0600 | [diff] [blame] | 2059 | blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift); |
Keith Busch | 8fc23e0 | 2012-07-26 11:29:57 -0600 | [diff] [blame] | 2060 | if (dev->max_hw_sectors) |
| 2061 | blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2062 | if (dev->stripe_size) |
| 2063 | blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9); |
Keith Busch | a7d2ce2 | 2014-04-29 11:41:28 -0600 | [diff] [blame] | 2064 | if (dev->vwc & NVME_CTRL_VWC_PRESENT) |
| 2065 | blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2066 | |
| 2067 | disk->major = nvme_major; |
Matthew Wilcox | 469071a | 2013-12-09 12:58:46 -0500 | [diff] [blame] | 2068 | disk->first_minor = 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2069 | disk->fops = &nvme_fops; |
| 2070 | disk->private_data = ns; |
| 2071 | disk->queue = ns->queue; |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 2072 | disk->driverfs_dev = dev->device; |
Matthew Wilcox | 469071a | 2013-12-09 12:58:46 -0500 | [diff] [blame] | 2073 | disk->flags = GENHD_FL_EXT_DEVT; |
Matthew Wilcox | 5aff938 | 2011-05-06 08:45:47 -0400 | [diff] [blame] | 2074 | sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2075 | |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 2076 | /* |
| 2077 | * Initialize capacity to 0 until we establish the namespace format and |
| 2078 | * setup integrity extentions if necessary. The revalidate_disk after |
| 2079 | * add_disk allows the driver to register with integrity if the format |
| 2080 | * requires it. |
| 2081 | */ |
| 2082 | set_capacity(disk, 0); |
| 2083 | nvme_revalidate_disk(ns->disk); |
| 2084 | add_disk(ns->disk); |
| 2085 | if (ns->ms) |
| 2086 | revalidate_disk(ns->disk); |
| 2087 | return; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2088 | out_free_queue: |
| 2089 | blk_cleanup_queue(ns->queue); |
| 2090 | out_free_ns: |
| 2091 | kfree(ns); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2092 | } |
| 2093 | |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2094 | static void nvme_create_io_queues(struct nvme_dev *dev) |
| 2095 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2096 | unsigned i; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2097 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2098 | for (i = dev->queue_count; i <= dev->max_qid; i++) |
Keith Busch | 2b25d98 | 2014-12-22 12:59:04 -0700 | [diff] [blame] | 2099 | if (!nvme_alloc_queue(dev, i, dev->q_depth)) |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2100 | break; |
| 2101 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2102 | for (i = dev->online_queues; i <= dev->queue_count - 1; i++) |
| 2103 | if (nvme_create_queue(dev->queues[i], i)) |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2104 | break; |
| 2105 | } |
| 2106 | |
Matthew Wilcox | b3b0681 | 2011-01-20 09:14:34 -0500 | [diff] [blame] | 2107 | static int set_queue_count(struct nvme_dev *dev, int count) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2108 | { |
| 2109 | int status; |
| 2110 | u32 result; |
Matthew Wilcox | b3b0681 | 2011-01-20 09:14:34 -0500 | [diff] [blame] | 2111 | u32 q_count = (count - 1) | ((count - 1) << 16); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2112 | |
Matthew Wilcox | df34813 | 2012-01-11 07:29:56 -0700 | [diff] [blame] | 2113 | status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0, |
Matthew Wilcox | bc5fc7e | 2011-09-19 17:08:14 -0400 | [diff] [blame] | 2114 | &result); |
Matthew Wilcox | 27e8166 | 2014-04-11 11:58:45 -0400 | [diff] [blame] | 2115 | if (status < 0) |
| 2116 | return status; |
| 2117 | if (status > 0) { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2118 | dev_err(dev->dev, "Could not set queue count (%d)\n", status); |
Keith Busch | badc34d | 2014-06-23 14:25:35 -0600 | [diff] [blame] | 2119 | return 0; |
Matthew Wilcox | 27e8166 | 2014-04-11 11:58:45 -0400 | [diff] [blame] | 2120 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2121 | return min(result & 0xffff, result >> 16) + 1; |
| 2122 | } |
| 2123 | |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 2124 | static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues) |
| 2125 | { |
Haiyan Hu | b80d5cc | 2013-09-10 11:25:37 +0800 | [diff] [blame] | 2126 | return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride); |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 2127 | } |
| 2128 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 2129 | static int nvme_setup_io_queues(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2130 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2131 | struct nvme_queue *adminq = dev->queues[0]; |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2132 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2133 | int result, i, vecs, nr_io_queues, size; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2134 | |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2135 | nr_io_queues = num_possible_cpus(); |
Matthew Wilcox | b348b7d | 2011-02-15 16:16:02 -0500 | [diff] [blame] | 2136 | result = set_queue_count(dev, nr_io_queues); |
Keith Busch | badc34d | 2014-06-23 14:25:35 -0600 | [diff] [blame] | 2137 | if (result <= 0) |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 2138 | return result; |
Matthew Wilcox | b348b7d | 2011-02-15 16:16:02 -0500 | [diff] [blame] | 2139 | if (result < nr_io_queues) |
| 2140 | nr_io_queues = result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2141 | |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 2142 | size = db_bar_size(dev, nr_io_queues); |
| 2143 | if (size > 8192) { |
Matthew Wilcox | f1938f6 | 2011-10-20 17:00:41 -0400 | [diff] [blame] | 2144 | iounmap(dev->bar); |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 2145 | do { |
| 2146 | dev->bar = ioremap(pci_resource_start(pdev, 0), size); |
| 2147 | if (dev->bar) |
| 2148 | break; |
| 2149 | if (!--nr_io_queues) |
| 2150 | return -ENOMEM; |
| 2151 | size = db_bar_size(dev, nr_io_queues); |
| 2152 | } while (1); |
Matthew Wilcox | f1938f6 | 2011-10-20 17:00:41 -0400 | [diff] [blame] | 2153 | dev->dbs = ((void __iomem *)dev->bar) + 4096; |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 2154 | adminq->q_db = dev->dbs; |
Matthew Wilcox | f1938f6 | 2011-10-20 17:00:41 -0400 | [diff] [blame] | 2155 | } |
| 2156 | |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 2157 | /* Deregister the admin queue's interrupt */ |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 2158 | free_irq(dev->entry[0].vector, adminq); |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 2159 | |
Jens Axboe | e32efbf | 2014-11-14 09:49:26 -0700 | [diff] [blame] | 2160 | /* |
| 2161 | * If we enable msix early due to not intx, disable it again before |
| 2162 | * setting up the full range we need. |
| 2163 | */ |
| 2164 | if (!pdev->irq) |
| 2165 | pci_disable_msix(pdev); |
| 2166 | |
Alexander Gordeev | be577fa | 2014-03-04 16:22:00 +0100 | [diff] [blame] | 2167 | for (i = 0; i < nr_io_queues; i++) |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 2168 | dev->entry[i].entry = i; |
Alexander Gordeev | be577fa | 2014-03-04 16:22:00 +0100 | [diff] [blame] | 2169 | vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues); |
| 2170 | if (vecs < 0) { |
| 2171 | vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32)); |
| 2172 | if (vecs < 0) { |
| 2173 | vecs = 1; |
| 2174 | } else { |
| 2175 | for (i = 0; i < vecs; i++) |
| 2176 | dev->entry[i].vector = i + pdev->irq; |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 2177 | } |
| 2178 | } |
| 2179 | |
Matthew Wilcox | 063a809 | 2013-06-20 10:53:48 -0400 | [diff] [blame] | 2180 | /* |
| 2181 | * Should investigate if there's a performance win from allocating |
| 2182 | * more queues than interrupt vectors; it might allow the submission |
| 2183 | * path to scale better, even if the receive path is limited by the |
| 2184 | * number of interrupts. |
| 2185 | */ |
| 2186 | nr_io_queues = vecs; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2187 | dev->max_qid = nr_io_queues; |
Ramachandra Rao Gajula | fa08a39 | 2013-05-11 15:19:31 -0700 | [diff] [blame] | 2188 | |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 2189 | result = queue_request_irq(dev, adminq, adminq->irqname); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2190 | if (result) |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 2191 | goto free_queues; |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 2192 | |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2193 | /* Free previously allocated queues that are no longer usable */ |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2194 | nvme_free_queues(dev, nr_io_queues + 1); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2195 | nvme_create_io_queues(dev); |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2196 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2197 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2198 | |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 2199 | free_queues: |
Keith Busch | a1a5ef9 | 2013-12-16 13:50:00 -0500 | [diff] [blame] | 2200 | nvme_free_queues(dev, 1); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 2201 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2202 | } |
| 2203 | |
Matthew Wilcox | 422ef0c | 2013-04-16 11:22:36 -0400 | [diff] [blame] | 2204 | /* |
| 2205 | * Return: error value if an error occurred setting up the queues or calling |
| 2206 | * Identify Device. 0 if these succeeded, even if adding some of the |
| 2207 | * namespaces failed. At the moment, these failures are silent. TBD which |
| 2208 | * failures should be reported. |
| 2209 | */ |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 2210 | static int nvme_dev_add(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2211 | { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2212 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
Matthew Wilcox | c3bfe71 | 2013-07-08 17:26:25 -0400 | [diff] [blame] | 2213 | int res; |
| 2214 | unsigned nn, i; |
Matthew Wilcox | 5181423 | 2011-02-01 16:18:08 -0500 | [diff] [blame] | 2215 | struct nvme_id_ctrl *ctrl; |
Keith Busch | 159b67d | 2013-04-09 17:13:20 -0600 | [diff] [blame] | 2216 | int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2217 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 2218 | res = nvme_identify_ctrl(dev, &ctrl); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2219 | if (res) { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2220 | dev_err(dev->dev, "Identify Controller failed (%d)\n", res); |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 2221 | return -EIO; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2222 | } |
| 2223 | |
Matthew Wilcox | 5181423 | 2011-02-01 16:18:08 -0500 | [diff] [blame] | 2224 | nn = le32_to_cpup(&ctrl->nn); |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 2225 | dev->oncs = le16_to_cpup(&ctrl->oncs); |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 2226 | dev->abort_limit = ctrl->acl + 1; |
Keith Busch | a7d2ce2 | 2014-04-29 11:41:28 -0600 | [diff] [blame] | 2227 | dev->vwc = ctrl->vwc; |
Matthew Wilcox | 5181423 | 2011-02-01 16:18:08 -0500 | [diff] [blame] | 2228 | memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn)); |
| 2229 | memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn)); |
| 2230 | memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr)); |
Keith Busch | 159b67d | 2013-04-09 17:13:20 -0600 | [diff] [blame] | 2231 | if (ctrl->mdts) |
Keith Busch | 8fc23e0 | 2012-07-26 11:29:57 -0600 | [diff] [blame] | 2232 | dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9); |
Matthew Wilcox | 68608c26 | 2013-06-21 14:36:34 -0400 | [diff] [blame] | 2233 | if ((pdev->vendor == PCI_VENDOR_ID_INTEL) && |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2234 | (pdev->device == 0x0953) && ctrl->vs[3]) { |
| 2235 | unsigned int max_hw_sectors; |
| 2236 | |
Keith Busch | 159b67d | 2013-04-09 17:13:20 -0600 | [diff] [blame] | 2237 | dev->stripe_size = 1 << (ctrl->vs[3] + shift); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2238 | max_hw_sectors = dev->stripe_size >> (shift - 9); |
| 2239 | if (dev->max_hw_sectors) { |
| 2240 | dev->max_hw_sectors = min(max_hw_sectors, |
| 2241 | dev->max_hw_sectors); |
| 2242 | } else |
| 2243 | dev->max_hw_sectors = max_hw_sectors; |
| 2244 | } |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame^] | 2245 | kfree(ctrl); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2246 | |
| 2247 | dev->tagset.ops = &nvme_mq_ops; |
| 2248 | dev->tagset.nr_hw_queues = dev->online_queues - 1; |
| 2249 | dev->tagset.timeout = NVME_IO_TIMEOUT; |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2250 | dev->tagset.numa_node = dev_to_node(dev->dev); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2251 | dev->tagset.queue_depth = |
| 2252 | min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1; |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 2253 | dev->tagset.cmd_size = nvme_cmd_size(dev); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2254 | dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE; |
| 2255 | dev->tagset.driver_data = dev; |
| 2256 | |
| 2257 | if (blk_mq_alloc_tag_set(&dev->tagset)) |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 2258 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2259 | |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 2260 | for (i = 1; i <= nn; i++) |
| 2261 | nvme_alloc_ns(dev, i); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2262 | |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 2263 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2264 | } |
| 2265 | |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2266 | static int nvme_dev_map(struct nvme_dev *dev) |
| 2267 | { |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2268 | u64 cap; |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2269 | int bars, result = -ENOMEM; |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2270 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2271 | |
| 2272 | if (pci_enable_device_mem(pdev)) |
| 2273 | return result; |
| 2274 | |
| 2275 | dev->entry[0].vector = pdev->irq; |
| 2276 | pci_set_master(pdev); |
| 2277 | bars = pci_select_bars(pdev, IORESOURCE_MEM); |
Jens Axboe | be7837e | 2014-11-14 09:50:19 -0700 | [diff] [blame] | 2278 | if (!bars) |
| 2279 | goto disable_pci; |
| 2280 | |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2281 | if (pci_request_selected_regions(pdev, bars, "nvme")) |
| 2282 | goto disable_pci; |
| 2283 | |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2284 | if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) && |
| 2285 | dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32))) |
Russell King | 052d0ef | 2013-06-26 23:49:11 +0100 | [diff] [blame] | 2286 | goto disable; |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2287 | |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2288 | dev->bar = ioremap(pci_resource_start(pdev, 0), 8192); |
| 2289 | if (!dev->bar) |
| 2290 | goto disable; |
Jens Axboe | e32efbf | 2014-11-14 09:49:26 -0700 | [diff] [blame] | 2291 | |
Keith Busch | 0e53d18 | 2013-12-10 13:10:39 -0700 | [diff] [blame] | 2292 | if (readl(&dev->bar->csts) == -1) { |
| 2293 | result = -ENODEV; |
| 2294 | goto unmap; |
| 2295 | } |
Jens Axboe | e32efbf | 2014-11-14 09:49:26 -0700 | [diff] [blame] | 2296 | |
| 2297 | /* |
| 2298 | * Some devices don't advertse INTx interrupts, pre-enable a single |
| 2299 | * MSIX vec for setup. We'll adjust this later. |
| 2300 | */ |
| 2301 | if (!pdev->irq) { |
| 2302 | result = pci_enable_msix(pdev, dev->entry, 1); |
| 2303 | if (result < 0) |
| 2304 | goto unmap; |
| 2305 | } |
| 2306 | |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 2307 | cap = readq(&dev->bar->cap); |
| 2308 | dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH); |
| 2309 | dev->db_stride = 1 << NVME_CAP_STRIDE(cap); |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2310 | dev->dbs = ((void __iomem *)dev->bar) + 4096; |
| 2311 | |
| 2312 | return 0; |
| 2313 | |
Keith Busch | 0e53d18 | 2013-12-10 13:10:39 -0700 | [diff] [blame] | 2314 | unmap: |
| 2315 | iounmap(dev->bar); |
| 2316 | dev->bar = NULL; |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2317 | disable: |
| 2318 | pci_release_regions(pdev); |
| 2319 | disable_pci: |
| 2320 | pci_disable_device(pdev); |
| 2321 | return result; |
| 2322 | } |
| 2323 | |
| 2324 | static void nvme_dev_unmap(struct nvme_dev *dev) |
| 2325 | { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2326 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 2327 | |
| 2328 | if (pdev->msi_enabled) |
| 2329 | pci_disable_msi(pdev); |
| 2330 | else if (pdev->msix_enabled) |
| 2331 | pci_disable_msix(pdev); |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2332 | |
| 2333 | if (dev->bar) { |
| 2334 | iounmap(dev->bar); |
| 2335 | dev->bar = NULL; |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2336 | pci_release_regions(pdev); |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2337 | } |
| 2338 | |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2339 | if (pci_is_enabled(pdev)) |
| 2340 | pci_disable_device(pdev); |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2341 | } |
| 2342 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2343 | struct nvme_delq_ctx { |
| 2344 | struct task_struct *waiter; |
| 2345 | struct kthread_worker *worker; |
| 2346 | atomic_t refcount; |
| 2347 | }; |
| 2348 | |
| 2349 | static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev) |
| 2350 | { |
| 2351 | dq->waiter = current; |
| 2352 | mb(); |
| 2353 | |
| 2354 | for (;;) { |
| 2355 | set_current_state(TASK_KILLABLE); |
| 2356 | if (!atomic_read(&dq->refcount)) |
| 2357 | break; |
| 2358 | if (!schedule_timeout(ADMIN_TIMEOUT) || |
| 2359 | fatal_signal_pending(current)) { |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 2360 | /* |
| 2361 | * Disable the controller first since we can't trust it |
| 2362 | * at this point, but leave the admin queue enabled |
| 2363 | * until all queue deletion requests are flushed. |
| 2364 | * FIXME: This may take a while if there are more h/w |
| 2365 | * queues than admin tags. |
| 2366 | */ |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2367 | set_current_state(TASK_RUNNING); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2368 | nvme_disable_ctrl(dev, readq(&dev->bar->cap)); |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 2369 | nvme_clear_queue(dev->queues[0]); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2370 | flush_kthread_worker(dq->worker); |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 2371 | nvme_disable_queue(dev, 0); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2372 | return; |
| 2373 | } |
| 2374 | } |
| 2375 | set_current_state(TASK_RUNNING); |
| 2376 | } |
| 2377 | |
| 2378 | static void nvme_put_dq(struct nvme_delq_ctx *dq) |
| 2379 | { |
| 2380 | atomic_dec(&dq->refcount); |
| 2381 | if (dq->waiter) |
| 2382 | wake_up_process(dq->waiter); |
| 2383 | } |
| 2384 | |
| 2385 | static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq) |
| 2386 | { |
| 2387 | atomic_inc(&dq->refcount); |
| 2388 | return dq; |
| 2389 | } |
| 2390 | |
| 2391 | static void nvme_del_queue_end(struct nvme_queue *nvmeq) |
| 2392 | { |
| 2393 | struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2394 | nvme_put_dq(dq); |
| 2395 | } |
| 2396 | |
| 2397 | static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode, |
| 2398 | kthread_work_func_t fn) |
| 2399 | { |
| 2400 | struct nvme_command c; |
| 2401 | |
| 2402 | memset(&c, 0, sizeof(c)); |
| 2403 | c.delete_queue.opcode = opcode; |
| 2404 | c.delete_queue.qid = cpu_to_le16(nvmeq->qid); |
| 2405 | |
| 2406 | init_kthread_work(&nvmeq->cmdinfo.work, fn); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2407 | return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo, |
| 2408 | ADMIN_TIMEOUT); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2409 | } |
| 2410 | |
| 2411 | static void nvme_del_cq_work_handler(struct kthread_work *work) |
| 2412 | { |
| 2413 | struct nvme_queue *nvmeq = container_of(work, struct nvme_queue, |
| 2414 | cmdinfo.work); |
| 2415 | nvme_del_queue_end(nvmeq); |
| 2416 | } |
| 2417 | |
| 2418 | static int nvme_delete_cq(struct nvme_queue *nvmeq) |
| 2419 | { |
| 2420 | return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq, |
| 2421 | nvme_del_cq_work_handler); |
| 2422 | } |
| 2423 | |
| 2424 | static void nvme_del_sq_work_handler(struct kthread_work *work) |
| 2425 | { |
| 2426 | struct nvme_queue *nvmeq = container_of(work, struct nvme_queue, |
| 2427 | cmdinfo.work); |
| 2428 | int status = nvmeq->cmdinfo.status; |
| 2429 | |
| 2430 | if (!status) |
| 2431 | status = nvme_delete_cq(nvmeq); |
| 2432 | if (status) |
| 2433 | nvme_del_queue_end(nvmeq); |
| 2434 | } |
| 2435 | |
| 2436 | static int nvme_delete_sq(struct nvme_queue *nvmeq) |
| 2437 | { |
| 2438 | return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq, |
| 2439 | nvme_del_sq_work_handler); |
| 2440 | } |
| 2441 | |
| 2442 | static void nvme_del_queue_start(struct kthread_work *work) |
| 2443 | { |
| 2444 | struct nvme_queue *nvmeq = container_of(work, struct nvme_queue, |
| 2445 | cmdinfo.work); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2446 | if (nvme_delete_sq(nvmeq)) |
| 2447 | nvme_del_queue_end(nvmeq); |
| 2448 | } |
| 2449 | |
| 2450 | static void nvme_disable_io_queues(struct nvme_dev *dev) |
| 2451 | { |
| 2452 | int i; |
| 2453 | DEFINE_KTHREAD_WORKER_ONSTACK(worker); |
| 2454 | struct nvme_delq_ctx dq; |
| 2455 | struct task_struct *kworker_task = kthread_run(kthread_worker_fn, |
| 2456 | &worker, "nvme%d", dev->instance); |
| 2457 | |
| 2458 | if (IS_ERR(kworker_task)) { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2459 | dev_err(dev->dev, |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2460 | "Failed to create queue del task\n"); |
| 2461 | for (i = dev->queue_count - 1; i > 0; i--) |
| 2462 | nvme_disable_queue(dev, i); |
| 2463 | return; |
| 2464 | } |
| 2465 | |
| 2466 | dq.waiter = NULL; |
| 2467 | atomic_set(&dq.refcount, 0); |
| 2468 | dq.worker = &worker; |
| 2469 | for (i = dev->queue_count - 1; i > 0; i--) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2470 | struct nvme_queue *nvmeq = dev->queues[i]; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2471 | |
| 2472 | if (nvme_suspend_queue(nvmeq)) |
| 2473 | continue; |
| 2474 | nvmeq->cmdinfo.ctx = nvme_get_dq(&dq); |
| 2475 | nvmeq->cmdinfo.worker = dq.worker; |
| 2476 | init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start); |
| 2477 | queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work); |
| 2478 | } |
| 2479 | nvme_wait_dq(&dq, dev); |
| 2480 | kthread_stop(kworker_task); |
| 2481 | } |
| 2482 | |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 2483 | /* |
| 2484 | * Remove the node from the device list and check |
| 2485 | * for whether or not we need to stop the nvme_thread. |
| 2486 | */ |
| 2487 | static void nvme_dev_list_remove(struct nvme_dev *dev) |
| 2488 | { |
| 2489 | struct task_struct *tmp = NULL; |
| 2490 | |
| 2491 | spin_lock(&dev_list_lock); |
| 2492 | list_del_init(&dev->node); |
| 2493 | if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) { |
| 2494 | tmp = nvme_thread; |
| 2495 | nvme_thread = NULL; |
| 2496 | } |
| 2497 | spin_unlock(&dev_list_lock); |
| 2498 | |
| 2499 | if (tmp) |
| 2500 | kthread_stop(tmp); |
| 2501 | } |
| 2502 | |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 2503 | static void nvme_freeze_queues(struct nvme_dev *dev) |
| 2504 | { |
| 2505 | struct nvme_ns *ns; |
| 2506 | |
| 2507 | list_for_each_entry(ns, &dev->namespaces, list) { |
| 2508 | blk_mq_freeze_queue_start(ns->queue); |
| 2509 | |
Christoph Hellwig | cddcd72 | 2015-05-07 09:38:14 +0200 | [diff] [blame] | 2510 | spin_lock_irq(ns->queue->queue_lock); |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 2511 | queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue); |
Christoph Hellwig | cddcd72 | 2015-05-07 09:38:14 +0200 | [diff] [blame] | 2512 | spin_unlock_irq(ns->queue->queue_lock); |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 2513 | |
| 2514 | blk_mq_cancel_requeue_work(ns->queue); |
| 2515 | blk_mq_stop_hw_queues(ns->queue); |
| 2516 | } |
| 2517 | } |
| 2518 | |
| 2519 | static void nvme_unfreeze_queues(struct nvme_dev *dev) |
| 2520 | { |
| 2521 | struct nvme_ns *ns; |
| 2522 | |
| 2523 | list_for_each_entry(ns, &dev->namespaces, list) { |
| 2524 | queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue); |
| 2525 | blk_mq_unfreeze_queue(ns->queue); |
| 2526 | blk_mq_start_stopped_hw_queues(ns->queue, true); |
| 2527 | blk_mq_kick_requeue_list(ns->queue); |
| 2528 | } |
| 2529 | } |
| 2530 | |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2531 | static void nvme_dev_shutdown(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2532 | { |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 2533 | int i; |
Keith Busch | 7c1b245 | 2014-06-25 11:18:12 -0600 | [diff] [blame] | 2534 | u32 csts = -1; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 2535 | |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 2536 | nvme_dev_list_remove(dev); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 2537 | |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 2538 | if (dev->bar) { |
| 2539 | nvme_freeze_queues(dev); |
Keith Busch | 7c1b245 | 2014-06-25 11:18:12 -0600 | [diff] [blame] | 2540 | csts = readl(&dev->bar->csts); |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 2541 | } |
Keith Busch | 7c1b245 | 2014-06-25 11:18:12 -0600 | [diff] [blame] | 2542 | if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) { |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2543 | for (i = dev->queue_count - 1; i >= 0; i--) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2544 | struct nvme_queue *nvmeq = dev->queues[i]; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2545 | nvme_suspend_queue(nvmeq); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2546 | } |
| 2547 | } else { |
| 2548 | nvme_disable_io_queues(dev); |
Keith Busch | 1894d8f | 2013-07-15 15:02:22 -0600 | [diff] [blame] | 2549 | nvme_shutdown_ctrl(dev); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2550 | nvme_disable_queue(dev, 0); |
| 2551 | } |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2552 | nvme_dev_unmap(dev); |
Keith Busch | 07836e6 | 2015-02-19 10:34:48 -0700 | [diff] [blame] | 2553 | |
| 2554 | for (i = dev->queue_count - 1; i >= 0; i--) |
| 2555 | nvme_clear_queue(dev->queues[i]); |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2556 | } |
| 2557 | |
| 2558 | static void nvme_dev_remove(struct nvme_dev *dev) |
| 2559 | { |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 2560 | struct nvme_ns *ns; |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2561 | |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 2562 | list_for_each_entry(ns, &dev->namespaces, list) { |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 2563 | if (ns->disk->flags & GENHD_FL_UP) { |
Keith Busch | 52b68d7 | 2015-02-23 09:16:21 -0700 | [diff] [blame] | 2564 | if (blk_get_integrity(ns->disk)) |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 2565 | blk_integrity_unregister(ns->disk); |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 2566 | del_gendisk(ns->disk); |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 2567 | } |
Keith Busch | cef6a94 | 2015-01-07 18:55:51 -0700 | [diff] [blame] | 2568 | if (!blk_queue_dying(ns->queue)) { |
| 2569 | blk_mq_abort_requeue_list(ns->queue); |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 2570 | blk_cleanup_queue(ns->queue); |
Keith Busch | cef6a94 | 2015-01-07 18:55:51 -0700 | [diff] [blame] | 2571 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2572 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2573 | } |
| 2574 | |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2575 | static int nvme_setup_prp_pools(struct nvme_dev *dev) |
| 2576 | { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2577 | dev->prp_page_pool = dma_pool_create("prp list page", dev->dev, |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2578 | PAGE_SIZE, PAGE_SIZE, 0); |
| 2579 | if (!dev->prp_page_pool) |
| 2580 | return -ENOMEM; |
| 2581 | |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 2582 | /* Optimisation for I/Os between 4k and 128k */ |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2583 | dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev, |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 2584 | 256, 256, 0); |
| 2585 | if (!dev->prp_small_pool) { |
| 2586 | dma_pool_destroy(dev->prp_page_pool); |
| 2587 | return -ENOMEM; |
| 2588 | } |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2589 | return 0; |
| 2590 | } |
| 2591 | |
| 2592 | static void nvme_release_prp_pools(struct nvme_dev *dev) |
| 2593 | { |
| 2594 | dma_pool_destroy(dev->prp_page_pool); |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 2595 | dma_pool_destroy(dev->prp_small_pool); |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2596 | } |
| 2597 | |
Quoc-Son Anh | cd58ad7 | 2012-02-21 16:50:53 -0700 | [diff] [blame] | 2598 | static DEFINE_IDA(nvme_instance_ida); |
| 2599 | |
| 2600 | static int nvme_set_instance(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2601 | { |
Quoc-Son Anh | cd58ad7 | 2012-02-21 16:50:53 -0700 | [diff] [blame] | 2602 | int instance, error; |
| 2603 | |
| 2604 | do { |
| 2605 | if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL)) |
| 2606 | return -ENODEV; |
| 2607 | |
| 2608 | spin_lock(&dev_list_lock); |
| 2609 | error = ida_get_new(&nvme_instance_ida, &instance); |
| 2610 | spin_unlock(&dev_list_lock); |
| 2611 | } while (error == -EAGAIN); |
| 2612 | |
| 2613 | if (error) |
| 2614 | return -ENODEV; |
| 2615 | |
| 2616 | dev->instance = instance; |
| 2617 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | static void nvme_release_instance(struct nvme_dev *dev) |
| 2621 | { |
Quoc-Son Anh | cd58ad7 | 2012-02-21 16:50:53 -0700 | [diff] [blame] | 2622 | spin_lock(&dev_list_lock); |
| 2623 | ida_remove(&nvme_instance_ida, dev->instance); |
| 2624 | spin_unlock(&dev_list_lock); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2625 | } |
| 2626 | |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 2627 | static void nvme_free_namespaces(struct nvme_dev *dev) |
| 2628 | { |
| 2629 | struct nvme_ns *ns, *next; |
| 2630 | |
| 2631 | list_for_each_entry_safe(ns, next, &dev->namespaces, list) { |
| 2632 | list_del(&ns->list); |
Keith Busch | 9e60352 | 2014-10-03 11:15:47 -0600 | [diff] [blame] | 2633 | |
| 2634 | spin_lock(&dev_list_lock); |
| 2635 | ns->disk->private_data = NULL; |
| 2636 | spin_unlock(&dev_list_lock); |
| 2637 | |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 2638 | put_disk(ns->disk); |
| 2639 | kfree(ns); |
| 2640 | } |
| 2641 | } |
| 2642 | |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 2643 | static void nvme_free_dev(struct kref *kref) |
| 2644 | { |
| 2645 | struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref); |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 2646 | |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2647 | put_device(dev->dev); |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 2648 | put_device(dev->device); |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 2649 | nvme_free_namespaces(dev); |
Indraneel M | 285dffc | 2014-12-11 08:24:18 -0700 | [diff] [blame] | 2650 | nvme_release_instance(dev); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2651 | blk_mq_free_tag_set(&dev->tagset); |
Keith Busch | ea191d2 | 2015-01-07 18:55:49 -0700 | [diff] [blame] | 2652 | blk_put_queue(dev->admin_q); |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 2653 | kfree(dev->queues); |
| 2654 | kfree(dev->entry); |
| 2655 | kfree(dev); |
| 2656 | } |
| 2657 | |
| 2658 | static int nvme_dev_open(struct inode *inode, struct file *f) |
| 2659 | { |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 2660 | struct nvme_dev *dev; |
| 2661 | int instance = iminor(inode); |
| 2662 | int ret = -ENODEV; |
| 2663 | |
| 2664 | spin_lock(&dev_list_lock); |
| 2665 | list_for_each_entry(dev, &dev_list, node) { |
| 2666 | if (dev->instance == instance) { |
Keith Busch | 2e1d844 | 2015-02-12 15:33:00 -0700 | [diff] [blame] | 2667 | if (!dev->admin_q) { |
| 2668 | ret = -EWOULDBLOCK; |
| 2669 | break; |
| 2670 | } |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 2671 | if (!kref_get_unless_zero(&dev->kref)) |
| 2672 | break; |
| 2673 | f->private_data = dev; |
| 2674 | ret = 0; |
| 2675 | break; |
| 2676 | } |
| 2677 | } |
| 2678 | spin_unlock(&dev_list_lock); |
| 2679 | |
| 2680 | return ret; |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | static int nvme_dev_release(struct inode *inode, struct file *f) |
| 2684 | { |
| 2685 | struct nvme_dev *dev = f->private_data; |
| 2686 | kref_put(&dev->kref, nvme_free_dev); |
| 2687 | return 0; |
| 2688 | } |
| 2689 | |
| 2690 | static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg) |
| 2691 | { |
| 2692 | struct nvme_dev *dev = f->private_data; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2693 | struct nvme_ns *ns; |
| 2694 | |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 2695 | switch (cmd) { |
| 2696 | case NVME_IOCTL_ADMIN_CMD: |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2697 | return nvme_user_cmd(dev, NULL, (void __user *)arg); |
Keith Busch | 7963e52 | 2014-09-12 16:07:20 -0600 | [diff] [blame] | 2698 | case NVME_IOCTL_IO_CMD: |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2699 | if (list_empty(&dev->namespaces)) |
| 2700 | return -ENOTTY; |
| 2701 | ns = list_first_entry(&dev->namespaces, struct nvme_ns, list); |
| 2702 | return nvme_user_cmd(dev, ns, (void __user *)arg); |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 2703 | default: |
| 2704 | return -ENOTTY; |
| 2705 | } |
| 2706 | } |
| 2707 | |
| 2708 | static const struct file_operations nvme_dev_fops = { |
| 2709 | .owner = THIS_MODULE, |
| 2710 | .open = nvme_dev_open, |
| 2711 | .release = nvme_dev_release, |
| 2712 | .unlocked_ioctl = nvme_dev_ioctl, |
| 2713 | .compat_ioctl = nvme_dev_ioctl, |
| 2714 | }; |
| 2715 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2716 | static void nvme_set_irq_hints(struct nvme_dev *dev) |
| 2717 | { |
| 2718 | struct nvme_queue *nvmeq; |
| 2719 | int i; |
| 2720 | |
| 2721 | for (i = 0; i < dev->online_queues; i++) { |
| 2722 | nvmeq = dev->queues[i]; |
| 2723 | |
| 2724 | if (!nvmeq->hctx) |
| 2725 | continue; |
| 2726 | |
| 2727 | irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector, |
| 2728 | nvmeq->hctx->cpumask); |
| 2729 | } |
| 2730 | } |
| 2731 | |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2732 | static int nvme_dev_start(struct nvme_dev *dev) |
| 2733 | { |
| 2734 | int result; |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 2735 | bool start_thread = false; |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2736 | |
| 2737 | result = nvme_dev_map(dev); |
| 2738 | if (result) |
| 2739 | return result; |
| 2740 | |
| 2741 | result = nvme_configure_admin_queue(dev); |
| 2742 | if (result) |
| 2743 | goto unmap; |
| 2744 | |
| 2745 | spin_lock(&dev_list_lock); |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 2746 | if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) { |
| 2747 | start_thread = true; |
| 2748 | nvme_thread = NULL; |
| 2749 | } |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2750 | list_add(&dev->node, &dev_list); |
| 2751 | spin_unlock(&dev_list_lock); |
| 2752 | |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 2753 | if (start_thread) { |
| 2754 | nvme_thread = kthread_run(nvme_kthread, NULL, "nvme"); |
Keith Busch | 387caa5 | 2014-09-22 13:46:19 -0600 | [diff] [blame] | 2755 | wake_up_all(&nvme_kthread_wait); |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 2756 | } else |
| 2757 | wait_event_killable(nvme_kthread_wait, nvme_thread); |
| 2758 | |
| 2759 | if (IS_ERR_OR_NULL(nvme_thread)) { |
| 2760 | result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR; |
| 2761 | goto disable; |
| 2762 | } |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2763 | |
| 2764 | nvme_init_queue(dev->queues[0], 0); |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 2765 | result = nvme_alloc_admin_tags(dev); |
| 2766 | if (result) |
| 2767 | goto disable; |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 2768 | |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2769 | result = nvme_setup_io_queues(dev); |
Keith Busch | badc34d | 2014-06-23 14:25:35 -0600 | [diff] [blame] | 2770 | if (result) |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 2771 | goto free_tags; |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2772 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2773 | nvme_set_irq_hints(dev); |
| 2774 | |
Keith Busch | 1efccc9 | 2015-03-31 10:37:17 -0600 | [diff] [blame] | 2775 | dev->event_limit = 1; |
Keith Busch | d82e8bf | 2013-09-05 14:45:07 -0600 | [diff] [blame] | 2776 | return result; |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2777 | |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 2778 | free_tags: |
| 2779 | nvme_dev_remove_admin(dev); |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2780 | disable: |
Keith Busch | a1a5ef9 | 2013-12-16 13:50:00 -0500 | [diff] [blame] | 2781 | nvme_disable_queue(dev, 0); |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 2782 | nvme_dev_list_remove(dev); |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2783 | unmap: |
| 2784 | nvme_dev_unmap(dev); |
| 2785 | return result; |
| 2786 | } |
| 2787 | |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2788 | static int nvme_remove_dead_ctrl(void *arg) |
| 2789 | { |
| 2790 | struct nvme_dev *dev = (struct nvme_dev *)arg; |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2791 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2792 | |
| 2793 | if (pci_get_drvdata(pdev)) |
Keith Busch | c81f497 | 2014-06-23 15:24:53 -0600 | [diff] [blame] | 2794 | pci_stop_and_remove_bus_device_locked(pdev); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2795 | kref_put(&dev->kref, nvme_free_dev); |
| 2796 | return 0; |
| 2797 | } |
| 2798 | |
| 2799 | static void nvme_remove_disks(struct work_struct *ws) |
| 2800 | { |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2801 | struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work); |
| 2802 | |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 2803 | nvme_free_queues(dev, 1); |
Keith Busch | 302c672 | 2014-07-18 11:40:20 -0600 | [diff] [blame] | 2804 | nvme_dev_remove(dev); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | static int nvme_dev_resume(struct nvme_dev *dev) |
| 2808 | { |
| 2809 | int ret; |
| 2810 | |
| 2811 | ret = nvme_dev_start(dev); |
Keith Busch | badc34d | 2014-06-23 14:25:35 -0600 | [diff] [blame] | 2812 | if (ret) |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2813 | return ret; |
Keith Busch | badc34d | 2014-06-23 14:25:35 -0600 | [diff] [blame] | 2814 | if (dev->online_queues < 2) { |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2815 | spin_lock(&dev_list_lock); |
Tejun Heo | 9ca9737 | 2014-03-07 10:24:49 -0500 | [diff] [blame] | 2816 | dev->reset_workfn = nvme_remove_disks; |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2817 | queue_work(nvme_workq, &dev->reset_work); |
| 2818 | spin_unlock(&dev_list_lock); |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 2819 | } else { |
| 2820 | nvme_unfreeze_queues(dev); |
| 2821 | nvme_set_irq_hints(dev); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2822 | } |
| 2823 | return 0; |
| 2824 | } |
| 2825 | |
| 2826 | static void nvme_dev_reset(struct nvme_dev *dev) |
| 2827 | { |
| 2828 | nvme_dev_shutdown(dev); |
| 2829 | if (nvme_dev_resume(dev)) { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2830 | dev_warn(dev->dev, "Device failed to resume\n"); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2831 | kref_get(&dev->kref); |
| 2832 | if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d", |
| 2833 | dev->instance))) { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2834 | dev_err(dev->dev, |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2835 | "Failed to start controller remove task\n"); |
| 2836 | kref_put(&dev->kref, nvme_free_dev); |
| 2837 | } |
| 2838 | } |
| 2839 | } |
| 2840 | |
| 2841 | static void nvme_reset_failed_dev(struct work_struct *ws) |
| 2842 | { |
| 2843 | struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work); |
| 2844 | nvme_dev_reset(dev); |
| 2845 | } |
| 2846 | |
Tejun Heo | 9ca9737 | 2014-03-07 10:24:49 -0500 | [diff] [blame] | 2847 | static void nvme_reset_workfn(struct work_struct *work) |
| 2848 | { |
| 2849 | struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work); |
| 2850 | dev->reset_workfn(work); |
| 2851 | } |
| 2852 | |
Keith Busch | 2e1d844 | 2015-02-12 15:33:00 -0700 | [diff] [blame] | 2853 | static void nvme_async_probe(struct work_struct *work); |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 2854 | static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2855 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2856 | int node, result = -ENOMEM; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2857 | struct nvme_dev *dev; |
| 2858 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2859 | node = dev_to_node(&pdev->dev); |
| 2860 | if (node == NUMA_NO_NODE) |
| 2861 | set_dev_node(&pdev->dev, 0); |
| 2862 | |
| 2863 | dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2864 | if (!dev) |
| 2865 | return -ENOMEM; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2866 | dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry), |
| 2867 | GFP_KERNEL, node); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2868 | if (!dev->entry) |
| 2869 | goto free; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2870 | dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *), |
| 2871 | GFP_KERNEL, node); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2872 | if (!dev->queues) |
| 2873 | goto free; |
| 2874 | |
| 2875 | INIT_LIST_HEAD(&dev->namespaces); |
Tejun Heo | 9ca9737 | 2014-03-07 10:24:49 -0500 | [diff] [blame] | 2876 | dev->reset_workfn = nvme_reset_failed_dev; |
| 2877 | INIT_WORK(&dev->reset_work, nvme_reset_workfn); |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2878 | dev->dev = get_device(&pdev->dev); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2879 | pci_set_drvdata(pdev, dev); |
Quoc-Son Anh | cd58ad7 | 2012-02-21 16:50:53 -0700 | [diff] [blame] | 2880 | result = nvme_set_instance(dev); |
| 2881 | if (result) |
Keith Busch | a96d4f5 | 2014-08-19 19:15:59 -0600 | [diff] [blame] | 2882 | goto put_pci; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2883 | |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2884 | result = nvme_setup_prp_pools(dev); |
| 2885 | if (result) |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2886 | goto release; |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2887 | |
Keith Busch | fb35e91 | 2014-03-03 11:09:47 -0700 | [diff] [blame] | 2888 | kref_init(&dev->kref); |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 2889 | dev->device = device_create(nvme_class, &pdev->dev, |
| 2890 | MKDEV(nvme_char_major, dev->instance), |
| 2891 | dev, "nvme%d", dev->instance); |
| 2892 | if (IS_ERR(dev->device)) { |
| 2893 | result = PTR_ERR(dev->device); |
Keith Busch | 2e1d844 | 2015-02-12 15:33:00 -0700 | [diff] [blame] | 2894 | goto release_pools; |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 2895 | } |
| 2896 | get_device(dev->device); |
| 2897 | |
Keith Busch | e6e96d7 | 2015-03-23 09:32:37 -0600 | [diff] [blame] | 2898 | INIT_LIST_HEAD(&dev->node); |
Keith Busch | 2e1d844 | 2015-02-12 15:33:00 -0700 | [diff] [blame] | 2899 | INIT_WORK(&dev->probe_work, nvme_async_probe); |
| 2900 | schedule_work(&dev->probe_work); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2901 | return 0; |
| 2902 | |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2903 | release_pools: |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2904 | nvme_release_prp_pools(dev); |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2905 | release: |
| 2906 | nvme_release_instance(dev); |
Keith Busch | a96d4f5 | 2014-08-19 19:15:59 -0600 | [diff] [blame] | 2907 | put_pci: |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2908 | put_device(dev->dev); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2909 | free: |
| 2910 | kfree(dev->queues); |
| 2911 | kfree(dev->entry); |
| 2912 | kfree(dev); |
| 2913 | return result; |
| 2914 | } |
| 2915 | |
Keith Busch | 2e1d844 | 2015-02-12 15:33:00 -0700 | [diff] [blame] | 2916 | static void nvme_async_probe(struct work_struct *work) |
| 2917 | { |
| 2918 | struct nvme_dev *dev = container_of(work, struct nvme_dev, probe_work); |
| 2919 | int result; |
| 2920 | |
| 2921 | result = nvme_dev_start(dev); |
| 2922 | if (result) |
| 2923 | goto reset; |
| 2924 | |
| 2925 | if (dev->online_queues > 1) |
| 2926 | result = nvme_dev_add(dev); |
| 2927 | if (result) |
| 2928 | goto reset; |
| 2929 | |
| 2930 | nvme_set_irq_hints(dev); |
Keith Busch | 2e1d844 | 2015-02-12 15:33:00 -0700 | [diff] [blame] | 2931 | return; |
| 2932 | reset: |
Keith Busch | 07836e6 | 2015-02-19 10:34:48 -0700 | [diff] [blame] | 2933 | if (!work_busy(&dev->reset_work)) { |
| 2934 | dev->reset_workfn = nvme_reset_failed_dev; |
| 2935 | queue_work(nvme_workq, &dev->reset_work); |
| 2936 | } |
Keith Busch | 2e1d844 | 2015-02-12 15:33:00 -0700 | [diff] [blame] | 2937 | } |
| 2938 | |
Keith Busch | f0d54a5 | 2014-05-02 10:40:43 -0600 | [diff] [blame] | 2939 | static void nvme_reset_notify(struct pci_dev *pdev, bool prepare) |
| 2940 | { |
Keith Busch | a673947 | 2014-06-23 16:03:21 -0600 | [diff] [blame] | 2941 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
Keith Busch | f0d54a5 | 2014-05-02 10:40:43 -0600 | [diff] [blame] | 2942 | |
Keith Busch | a673947 | 2014-06-23 16:03:21 -0600 | [diff] [blame] | 2943 | if (prepare) |
| 2944 | nvme_dev_shutdown(dev); |
| 2945 | else |
| 2946 | nvme_dev_resume(dev); |
Keith Busch | f0d54a5 | 2014-05-02 10:40:43 -0600 | [diff] [blame] | 2947 | } |
| 2948 | |
Keith Busch | 09ece14 | 2014-01-27 11:29:40 -0500 | [diff] [blame] | 2949 | static void nvme_shutdown(struct pci_dev *pdev) |
| 2950 | { |
| 2951 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 2952 | nvme_dev_shutdown(dev); |
| 2953 | } |
| 2954 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 2955 | static void nvme_remove(struct pci_dev *pdev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2956 | { |
| 2957 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2958 | |
| 2959 | spin_lock(&dev_list_lock); |
| 2960 | list_del_init(&dev->node); |
| 2961 | spin_unlock(&dev_list_lock); |
| 2962 | |
| 2963 | pci_set_drvdata(pdev, NULL); |
Keith Busch | 2e1d844 | 2015-02-12 15:33:00 -0700 | [diff] [blame] | 2964 | flush_work(&dev->probe_work); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2965 | flush_work(&dev->reset_work); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2966 | nvme_dev_shutdown(dev); |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 2967 | nvme_dev_remove(dev); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2968 | nvme_dev_remove_admin(dev); |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 2969 | device_destroy(nvme_class, MKDEV(nvme_char_major, dev->instance)); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2970 | nvme_free_queues(dev, 0); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2971 | nvme_release_prp_pools(dev); |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 2972 | kref_put(&dev->kref, nvme_free_dev); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2973 | } |
| 2974 | |
| 2975 | /* These functions are yet to be implemented */ |
| 2976 | #define nvme_error_detected NULL |
| 2977 | #define nvme_dump_registers NULL |
| 2978 | #define nvme_link_reset NULL |
| 2979 | #define nvme_slot_reset NULL |
| 2980 | #define nvme_error_resume NULL |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2981 | |
Jingoo Han | 671a601 | 2014-02-13 11:19:14 +0900 | [diff] [blame] | 2982 | #ifdef CONFIG_PM_SLEEP |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2983 | static int nvme_suspend(struct device *dev) |
| 2984 | { |
| 2985 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2986 | struct nvme_dev *ndev = pci_get_drvdata(pdev); |
| 2987 | |
| 2988 | nvme_dev_shutdown(ndev); |
| 2989 | return 0; |
| 2990 | } |
| 2991 | |
| 2992 | static int nvme_resume(struct device *dev) |
| 2993 | { |
| 2994 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2995 | struct nvme_dev *ndev = pci_get_drvdata(pdev); |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2996 | |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2997 | if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) { |
Tejun Heo | 9ca9737 | 2014-03-07 10:24:49 -0500 | [diff] [blame] | 2998 | ndev->reset_workfn = nvme_reset_failed_dev; |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2999 | queue_work(nvme_workq, &ndev->reset_work); |
| 3000 | } |
| 3001 | return 0; |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 3002 | } |
Jingoo Han | 671a601 | 2014-02-13 11:19:14 +0900 | [diff] [blame] | 3003 | #endif |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 3004 | |
| 3005 | static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 3006 | |
Stephen Hemminger | 1d35203 | 2012-09-07 09:33:17 -0700 | [diff] [blame] | 3007 | static const struct pci_error_handlers nvme_err_handler = { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 3008 | .error_detected = nvme_error_detected, |
| 3009 | .mmio_enabled = nvme_dump_registers, |
| 3010 | .link_reset = nvme_link_reset, |
| 3011 | .slot_reset = nvme_slot_reset, |
| 3012 | .resume = nvme_error_resume, |
Keith Busch | f0d54a5 | 2014-05-02 10:40:43 -0600 | [diff] [blame] | 3013 | .reset_notify = nvme_reset_notify, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 3014 | }; |
| 3015 | |
| 3016 | /* Move to pci_ids.h later */ |
| 3017 | #define PCI_CLASS_STORAGE_EXPRESS 0x010802 |
| 3018 | |
Matthew Wilcox | 6eb0d69 | 2014-03-24 10:11:22 -0400 | [diff] [blame] | 3019 | static const struct pci_device_id nvme_id_table[] = { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 3020 | { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) }, |
| 3021 | { 0, } |
| 3022 | }; |
| 3023 | MODULE_DEVICE_TABLE(pci, nvme_id_table); |
| 3024 | |
| 3025 | static struct pci_driver nvme_driver = { |
| 3026 | .name = "nvme", |
| 3027 | .id_table = nvme_id_table, |
| 3028 | .probe = nvme_probe, |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 3029 | .remove = nvme_remove, |
Keith Busch | 09ece14 | 2014-01-27 11:29:40 -0500 | [diff] [blame] | 3030 | .shutdown = nvme_shutdown, |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 3031 | .driver = { |
| 3032 | .pm = &nvme_dev_pm_ops, |
| 3033 | }, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 3034 | .err_handler = &nvme_err_handler, |
| 3035 | }; |
| 3036 | |
| 3037 | static int __init nvme_init(void) |
| 3038 | { |
Matthew Wilcox | 0ac1314 | 2012-07-31 13:31:15 -0400 | [diff] [blame] | 3039 | int result; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 3040 | |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 3041 | init_waitqueue_head(&nvme_kthread_wait); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 3042 | |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 3043 | nvme_workq = create_singlethread_workqueue("nvme"); |
| 3044 | if (!nvme_workq) |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 3045 | return -ENOMEM; |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 3046 | |
Keith Busch | 5c42ea1 | 2012-07-25 16:05:18 -0600 | [diff] [blame] | 3047 | result = register_blkdev(nvme_major, "nvme"); |
| 3048 | if (result < 0) |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 3049 | goto kill_workq; |
Keith Busch | 5c42ea1 | 2012-07-25 16:05:18 -0600 | [diff] [blame] | 3050 | else if (result > 0) |
Matthew Wilcox | 0ac1314 | 2012-07-31 13:31:15 -0400 | [diff] [blame] | 3051 | nvme_major = result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 3052 | |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 3053 | result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme", |
| 3054 | &nvme_dev_fops); |
| 3055 | if (result < 0) |
| 3056 | goto unregister_blkdev; |
| 3057 | else if (result > 0) |
| 3058 | nvme_char_major = result; |
| 3059 | |
| 3060 | nvme_class = class_create(THIS_MODULE, "nvme"); |
Alexey Khoroshilov | c727040 | 2015-03-07 01:43:41 +0300 | [diff] [blame] | 3061 | if (IS_ERR(nvme_class)) { |
| 3062 | result = PTR_ERR(nvme_class); |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 3063 | goto unregister_chrdev; |
Alexey Khoroshilov | c727040 | 2015-03-07 01:43:41 +0300 | [diff] [blame] | 3064 | } |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 3065 | |
Keith Busch | f3db22f | 2014-06-11 11:51:35 -0600 | [diff] [blame] | 3066 | result = pci_register_driver(&nvme_driver); |
| 3067 | if (result) |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 3068 | goto destroy_class; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 3069 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 3070 | |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 3071 | destroy_class: |
| 3072 | class_destroy(nvme_class); |
| 3073 | unregister_chrdev: |
| 3074 | __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme"); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 3075 | unregister_blkdev: |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 3076 | unregister_blkdev(nvme_major, "nvme"); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 3077 | kill_workq: |
| 3078 | destroy_workqueue(nvme_workq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 3079 | return result; |
| 3080 | } |
| 3081 | |
| 3082 | static void __exit nvme_exit(void) |
| 3083 | { |
| 3084 | pci_unregister_driver(&nvme_driver); |
| 3085 | unregister_blkdev(nvme_major, "nvme"); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 3086 | destroy_workqueue(nvme_workq); |
Keith Busch | b3fffde | 2015-02-03 11:21:42 -0700 | [diff] [blame] | 3087 | class_destroy(nvme_class); |
| 3088 | __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme"); |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 3089 | BUG_ON(nvme_thread && !IS_ERR(nvme_thread)); |
Matthew Wilcox | 21bd78b | 2014-05-09 22:42:26 -0400 | [diff] [blame] | 3090 | _nvme_check_size(); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 3091 | } |
| 3092 | |
| 3093 | MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>"); |
| 3094 | MODULE_LICENSE("GPL"); |
Keith Busch | c78b4713 | 2014-11-21 15:16:32 -0700 | [diff] [blame] | 3095 | MODULE_VERSION("1.0"); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 3096 | module_init(nvme_init); |
| 3097 | module_exit(nvme_exit); |