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 | |
Matthew Wilcox | 8de0553 | 2011-05-12 13:50:28 -0400 | [diff] [blame] | 15 | #include <linux/bitops.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 16 | #include <linux/blkdev.h> |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 17 | #include <linux/blk-mq.h> |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 18 | #include <linux/cpu.h> |
Matthew Wilcox | fd63e9ce | 2011-05-06 08:37:54 -0400 | [diff] [blame] | 19 | #include <linux/delay.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 20 | #include <linux/errno.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/genhd.h> |
Keith Busch | 4cc09e2 | 2014-04-02 15:45:37 -0600 | [diff] [blame] | 23 | #include <linux/hdreg.h> |
Matthew Wilcox | 5aff938 | 2011-05-06 08:45:47 -0400 | [diff] [blame] | 24 | #include <linux/idr.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 25 | #include <linux/init.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/io.h> |
| 28 | #include <linux/kdev_t.h> |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 29 | #include <linux/kthread.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 30 | #include <linux/kernel.h> |
| 31 | #include <linux/mm.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/moduleparam.h> |
Keith Busch | 77bf25e | 2015-11-26 12:21:29 +0100 | [diff] [blame^] | 34 | #include <linux/mutex.h> |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 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> |
Linus Torvalds | 9cf5c09 | 2015-11-06 14:22:15 -0800 | [diff] [blame] | 42 | #include <linux/io-64-nonatomic-lo-hi.h> |
Keith Busch | 1d277a6 | 2015-10-15 14:10:52 +0200 | [diff] [blame] | 43 | #include <asm/unaligned.h> |
Hitoshi Mitake | 797a796 | 2012-02-07 11:45:33 +0900 | [diff] [blame] | 44 | |
Christoph Hellwig | f11bb3e | 2015-10-03 15:46:41 +0200 | [diff] [blame] | 45 | #include "nvme.h" |
| 46 | |
Keith Busch | 9d43cf6 | 2014-05-13 11:42:02 -0600 | [diff] [blame] | 47 | #define NVME_Q_DEPTH 1024 |
Jens Axboe | d31af0a | 2015-03-06 12:56:13 -0700 | [diff] [blame] | 48 | #define NVME_AQ_DEPTH 256 |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 49 | #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command)) |
| 50 | #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion)) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 51 | |
Christoph Hellwig | 21d3471 | 2015-11-26 09:08:36 +0100 | [diff] [blame] | 52 | unsigned char admin_timeout = 60; |
Keith Busch | 9d43cf6 | 2014-05-13 11:42:02 -0600 | [diff] [blame] | 53 | module_param(admin_timeout, byte, 0644); |
| 54 | MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands"); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 55 | |
Matthew Wilcox | bd67608 | 2014-06-03 23:04:30 -0400 | [diff] [blame] | 56 | unsigned char nvme_io_timeout = 30; |
| 57 | module_param_named(io_timeout, nvme_io_timeout, byte, 0644); |
Keith Busch | b355084 | 2014-04-04 11:43:36 -0600 | [diff] [blame] | 58 | MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O"); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 59 | |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 60 | unsigned char shutdown_timeout = 5; |
Dan McLeran | 2484f40 | 2014-07-01 09:33:32 -0600 | [diff] [blame] | 61 | module_param(shutdown_timeout, byte, 0644); |
| 62 | MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown"); |
| 63 | |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 64 | static int use_threaded_interrupts; |
| 65 | module_param(use_threaded_interrupts, int, 0); |
| 66 | |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 67 | static bool use_cmb_sqes = true; |
| 68 | module_param(use_cmb_sqes, bool, 0644); |
| 69 | MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes"); |
| 70 | |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 71 | static LIST_HEAD(dev_list); |
| 72 | static struct task_struct *nvme_thread; |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 73 | static struct workqueue_struct *nvme_workq; |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 74 | static wait_queue_head_t nvme_kthread_wait; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 75 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 76 | struct nvme_dev; |
| 77 | struct nvme_queue; |
Christoph Hellwig | d4f6c3a | 2015-11-26 10:51:23 +0100 | [diff] [blame] | 78 | struct nvme_iod; |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 79 | |
Christoph Hellwig | 90667892 | 2015-10-02 18:49:23 +0200 | [diff] [blame] | 80 | static int __nvme_reset(struct nvme_dev *dev); |
Keith Busch | 4cc0652 | 2015-06-05 10:30:08 -0600 | [diff] [blame] | 81 | static int nvme_reset(struct nvme_dev *dev); |
Jens Axboe | a0fa964 | 2015-11-03 20:37:26 -0700 | [diff] [blame] | 82 | static void nvme_process_cq(struct nvme_queue *nvmeq); |
Christoph Hellwig | d4f6c3a | 2015-11-26 10:51:23 +0100 | [diff] [blame] | 83 | static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod); |
Christoph Hellwig | 3cf519b | 2015-10-03 09:49:23 +0200 | [diff] [blame] | 84 | static void nvme_dead_ctrl(struct nvme_dev *dev); |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 85 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 86 | struct async_cmd_info { |
| 87 | struct kthread_work work; |
| 88 | struct kthread_worker *worker; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 89 | struct request *req; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 90 | u32 result; |
| 91 | int status; |
| 92 | void *ctx; |
| 93 | }; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 94 | |
| 95 | /* |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 96 | * Represents an NVM Express device. Each nvme_dev is a PCI function. |
| 97 | */ |
| 98 | struct nvme_dev { |
| 99 | struct list_head node; |
| 100 | struct nvme_queue **queues; |
| 101 | struct blk_mq_tag_set tagset; |
| 102 | struct blk_mq_tag_set admin_tagset; |
| 103 | u32 __iomem *dbs; |
| 104 | struct device *dev; |
| 105 | struct dma_pool *prp_page_pool; |
| 106 | struct dma_pool *prp_small_pool; |
| 107 | unsigned queue_count; |
| 108 | unsigned online_queues; |
| 109 | unsigned max_qid; |
| 110 | int q_depth; |
| 111 | u32 db_stride; |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 112 | struct msix_entry *entry; |
| 113 | void __iomem *bar; |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 114 | struct work_struct reset_work; |
| 115 | struct work_struct probe_work; |
| 116 | struct work_struct scan_work; |
Keith Busch | 77bf25e | 2015-11-26 12:21:29 +0100 | [diff] [blame^] | 117 | struct mutex shutdown_lock; |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 118 | bool subsystem; |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 119 | void __iomem *cmb; |
| 120 | dma_addr_t cmb_dma_addr; |
| 121 | u64 cmb_size; |
| 122 | u32 cmbsz; |
| 123 | |
| 124 | struct nvme_ctrl ctrl; |
| 125 | }; |
| 126 | |
| 127 | static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl) |
| 128 | { |
| 129 | return container_of(ctrl, struct nvme_dev, ctrl); |
| 130 | } |
| 131 | |
| 132 | /* |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 133 | * An NVM Express queue. Each device has at least two (one for admin |
| 134 | * commands and one for I/O commands). |
| 135 | */ |
| 136 | struct nvme_queue { |
| 137 | struct device *q_dmadev; |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 138 | struct nvme_dev *dev; |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 139 | char irqname[24]; /* nvme4294967295-65535\0 */ |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 140 | spinlock_t q_lock; |
| 141 | struct nvme_command *sq_cmds; |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 142 | struct nvme_command __iomem *sq_cmds_io; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 143 | volatile struct nvme_completion *cqes; |
Keith Busch | 4248322 | 2015-06-01 09:29:54 -0600 | [diff] [blame] | 144 | struct blk_mq_tags **tags; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 145 | dma_addr_t sq_dma_addr; |
| 146 | dma_addr_t cq_dma_addr; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 147 | u32 __iomem *q_db; |
| 148 | u16 q_depth; |
Jens Axboe | 6222d17 | 2015-01-15 15:19:10 -0700 | [diff] [blame] | 149 | s16 cq_vector; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 150 | u16 sq_head; |
| 151 | u16 sq_tail; |
| 152 | u16 cq_head; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 153 | u16 qid; |
Matthew Wilcox | e9539f4 | 2013-06-24 11:47:34 -0400 | [diff] [blame] | 154 | u8 cq_phase; |
| 155 | u8 cqe_seen; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 156 | struct async_cmd_info cmdinfo; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | /* |
Christoph Hellwig | 71bd150 | 2015-10-16 07:58:32 +0200 | [diff] [blame] | 160 | * The nvme_iod describes the data in an I/O, including the list of PRP |
| 161 | * entries. You can't see it in this data structure because C doesn't let |
| 162 | * me express that. Use nvme_alloc_iod to ensure there's enough space |
| 163 | * allocated to store the PRP list. |
| 164 | */ |
| 165 | struct nvme_iod { |
| 166 | unsigned long private; /* For the use of the submitter of the I/O */ |
| 167 | int npages; /* In the PRP list. 0 means small pool in use */ |
| 168 | int offset; /* Of PRP list */ |
| 169 | int nents; /* Used in scatterlist */ |
| 170 | int length; /* Of data, in bytes */ |
| 171 | dma_addr_t first_dma; |
| 172 | struct scatterlist meta_sg[1]; /* metadata requires single contiguous buffer */ |
| 173 | struct scatterlist sg[0]; |
| 174 | }; |
| 175 | |
| 176 | /* |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 177 | * Check we didin't inadvertently grow the command struct |
| 178 | */ |
| 179 | static inline void _nvme_check_size(void) |
| 180 | { |
| 181 | BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64); |
| 182 | BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64); |
| 183 | BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64); |
| 184 | BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); |
| 185 | BUILD_BUG_ON(sizeof(struct nvme_features) != 64); |
Vishal Verma | f8ebf84 | 2013-03-27 07:13:41 -0400 | [diff] [blame] | 186 | BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64); |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 187 | BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 188 | BUILD_BUG_ON(sizeof(struct nvme_command) != 64); |
| 189 | BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096); |
| 190 | BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096); |
| 191 | BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64); |
Keith Busch | 6ecec74 | 2012-09-26 12:49:27 -0600 | [diff] [blame] | 192 | BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 193 | } |
| 194 | |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 195 | typedef void (*nvme_completion_fn)(struct nvme_queue *, void *, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 196 | struct nvme_completion *); |
| 197 | |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 198 | struct nvme_cmd_info { |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 199 | nvme_completion_fn fn; |
| 200 | void *ctx; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 201 | int aborted; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 202 | struct nvme_queue *nvmeq; |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 203 | struct nvme_iod iod[0]; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 204 | }; |
| 205 | |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 206 | /* |
| 207 | * Max size of iod being embedded in the request payload |
| 208 | */ |
| 209 | #define NVME_INT_PAGES 2 |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 210 | #define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size) |
Chong Yuan | fda631f | 2015-03-27 09:21:32 +0800 | [diff] [blame] | 211 | #define NVME_INT_MASK 0x01 |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * Will slightly overestimate the number of pages needed. This is OK |
| 215 | * as it only leads to a small amount of wasted memory for the lifetime of |
| 216 | * the I/O. |
| 217 | */ |
| 218 | static int nvme_npages(unsigned size, struct nvme_dev *dev) |
| 219 | { |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 220 | unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size, |
| 221 | dev->ctrl.page_size); |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 222 | return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8); |
| 223 | } |
| 224 | |
| 225 | static unsigned int nvme_cmd_size(struct nvme_dev *dev) |
| 226 | { |
| 227 | unsigned int ret = sizeof(struct nvme_cmd_info); |
| 228 | |
| 229 | ret += sizeof(struct nvme_iod); |
| 230 | ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev); |
| 231 | ret += sizeof(struct scatterlist) * NVME_INT_PAGES; |
| 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 236 | static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 237 | unsigned int hctx_idx) |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 238 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 239 | struct nvme_dev *dev = data; |
| 240 | struct nvme_queue *nvmeq = dev->queues[0]; |
| 241 | |
Keith Busch | 4248322 | 2015-06-01 09:29:54 -0600 | [diff] [blame] | 242 | WARN_ON(hctx_idx != 0); |
| 243 | WARN_ON(dev->admin_tagset.tags[0] != hctx->tags); |
| 244 | WARN_ON(nvmeq->tags); |
| 245 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 246 | hctx->driver_data = nvmeq; |
Keith Busch | 4248322 | 2015-06-01 09:29:54 -0600 | [diff] [blame] | 247 | nvmeq->tags = &dev->admin_tagset.tags[0]; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 248 | return 0; |
Matthew Wilcox | e85248e | 2011-02-06 18:30:16 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Keith Busch | 4af0e21 | 2015-06-08 10:08:13 -0600 | [diff] [blame] | 251 | static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) |
| 252 | { |
| 253 | struct nvme_queue *nvmeq = hctx->driver_data; |
| 254 | |
| 255 | nvmeq->tags = NULL; |
| 256 | } |
| 257 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 258 | static int nvme_admin_init_request(void *data, struct request *req, |
| 259 | unsigned int hctx_idx, unsigned int rq_idx, |
| 260 | unsigned int numa_node) |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 261 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 262 | struct nvme_dev *dev = data; |
| 263 | struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req); |
| 264 | struct nvme_queue *nvmeq = dev->queues[0]; |
| 265 | |
| 266 | BUG_ON(!nvmeq); |
| 267 | cmd->nvmeq = nvmeq; |
| 268 | return 0; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 269 | } |
| 270 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 271 | static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 272 | unsigned int hctx_idx) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 273 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 274 | struct nvme_dev *dev = data; |
Keith Busch | 4248322 | 2015-06-01 09:29:54 -0600 | [diff] [blame] | 275 | struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1]; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 276 | |
Keith Busch | 4248322 | 2015-06-01 09:29:54 -0600 | [diff] [blame] | 277 | if (!nvmeq->tags) |
| 278 | nvmeq->tags = &dev->tagset.tags[hctx_idx]; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 279 | |
Keith Busch | 4248322 | 2015-06-01 09:29:54 -0600 | [diff] [blame] | 280 | WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 281 | hctx->driver_data = nvmeq; |
| 282 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 283 | } |
| 284 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 285 | static int nvme_init_request(void *data, struct request *req, |
| 286 | unsigned int hctx_idx, unsigned int rq_idx, |
| 287 | unsigned int numa_node) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 288 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 289 | struct nvme_dev *dev = data; |
| 290 | struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req); |
| 291 | struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1]; |
| 292 | |
| 293 | BUG_ON(!nvmeq); |
| 294 | cmd->nvmeq = nvmeq; |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx, |
| 299 | nvme_completion_fn handler) |
| 300 | { |
| 301 | cmd->fn = handler; |
| 302 | cmd->ctx = ctx; |
| 303 | cmd->aborted = 0; |
Keith Busch | c917dfe | 2015-01-07 18:55:48 -0700 | [diff] [blame] | 304 | blk_mq_start_request(blk_mq_rq_from_pdu(cmd)); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 305 | } |
| 306 | |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 307 | static void *iod_get_private(struct nvme_iod *iod) |
| 308 | { |
| 309 | return (void *) (iod->private & ~0x1UL); |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * If bit 0 is set, the iod is embedded in the request payload. |
| 314 | */ |
| 315 | static bool iod_should_kfree(struct nvme_iod *iod) |
| 316 | { |
Chong Yuan | fda631f | 2015-03-27 09:21:32 +0800 | [diff] [blame] | 317 | return (iod->private & NVME_INT_MASK) == 0; |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 320 | /* Special values must be less than 0x1000 */ |
| 321 | #define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA) |
Matthew Wilcox | d2d8703 | 2011-02-07 15:55:59 -0500 | [diff] [blame] | 322 | #define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE) |
| 323 | #define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE) |
| 324 | #define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE) |
Matthew Wilcox | be7b627 | 2011-02-06 07:53:23 -0500 | [diff] [blame] | 325 | |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 326 | static void special_completion(struct nvme_queue *nvmeq, void *ctx, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 327 | struct nvme_completion *cqe) |
| 328 | { |
| 329 | if (ctx == CMD_CTX_CANCELLED) |
| 330 | return; |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 331 | if (ctx == CMD_CTX_COMPLETED) { |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 332 | dev_warn(nvmeq->q_dmadev, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 333 | "completed id %d twice on queue %d\n", |
| 334 | cqe->command_id, le16_to_cpup(&cqe->sq_id)); |
| 335 | return; |
| 336 | } |
| 337 | if (ctx == CMD_CTX_INVALID) { |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 338 | dev_warn(nvmeq->q_dmadev, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 339 | "invalid id %d completed on queue %d\n", |
| 340 | cqe->command_id, le16_to_cpup(&cqe->sq_id)); |
| 341 | return; |
| 342 | } |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 343 | dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx); |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 344 | } |
| 345 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 346 | static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn) |
| 347 | { |
| 348 | void *ctx; |
| 349 | |
| 350 | if (fn) |
| 351 | *fn = cmd->fn; |
| 352 | ctx = cmd->ctx; |
| 353 | cmd->fn = special_completion; |
| 354 | cmd->ctx = CMD_CTX_CANCELLED; |
| 355 | return ctx; |
| 356 | } |
| 357 | |
| 358 | static void async_req_completion(struct nvme_queue *nvmeq, void *ctx, |
| 359 | struct nvme_completion *cqe) |
| 360 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 361 | u32 result = le32_to_cpup(&cqe->result); |
| 362 | u16 status = le16_to_cpup(&cqe->status) >> 1; |
| 363 | |
| 364 | if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ) |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 365 | ++nvmeq->dev->ctrl.event_limit; |
Keith Busch | a5768aa | 2015-06-01 14:28:14 -0600 | [diff] [blame] | 366 | if (status != NVME_SC_SUCCESS) |
| 367 | return; |
| 368 | |
| 369 | switch (result & 0xff07) { |
| 370 | case NVME_AER_NOTICE_NS_CHANGED: |
| 371 | dev_info(nvmeq->q_dmadev, "rescanning\n"); |
| 372 | schedule_work(&nvmeq->dev->scan_work); |
| 373 | default: |
| 374 | dev_warn(nvmeq->q_dmadev, "async event result %08x\n", result); |
| 375 | } |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static void abort_completion(struct nvme_queue *nvmeq, void *ctx, |
| 379 | struct nvme_completion *cqe) |
| 380 | { |
| 381 | struct request *req = ctx; |
| 382 | |
| 383 | u16 status = le16_to_cpup(&cqe->status) >> 1; |
| 384 | u32 result = le32_to_cpup(&cqe->result); |
| 385 | |
Keith Busch | 4248322 | 2015-06-01 09:29:54 -0600 | [diff] [blame] | 386 | blk_mq_free_request(req); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 387 | |
| 388 | dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result); |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 389 | ++nvmeq->dev->ctrl.abort_limit; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 392 | static void async_completion(struct nvme_queue *nvmeq, void *ctx, |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 393 | struct nvme_completion *cqe) |
| 394 | { |
| 395 | struct async_cmd_info *cmdinfo = ctx; |
| 396 | cmdinfo->result = le32_to_cpup(&cqe->result); |
| 397 | cmdinfo->status = le16_to_cpup(&cqe->status) >> 1; |
| 398 | queue_kthread_work(cmdinfo->worker, &cmdinfo->work); |
Keith Busch | 4248322 | 2015-06-01 09:29:54 -0600 | [diff] [blame] | 399 | blk_mq_free_request(cmdinfo->req); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq, |
| 403 | unsigned int tag) |
| 404 | { |
Keith Busch | 4248322 | 2015-06-01 09:29:54 -0600 | [diff] [blame] | 405 | struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 406 | |
| 407 | return blk_mq_rq_to_pdu(req); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Matthew Wilcox | 184d294 | 2011-05-11 21:36:38 -0400 | [diff] [blame] | 410 | /* |
| 411 | * Called with local interrupts disabled and the q_lock held. May not sleep. |
| 412 | */ |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 413 | static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag, |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 414 | nvme_completion_fn *fn) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 415 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 416 | struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag); |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 417 | void *ctx; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 418 | if (tag >= nvmeq->q_depth) { |
| 419 | *fn = special_completion; |
Matthew Wilcox | 48e3d39 | 2011-02-06 08:51:15 -0500 | [diff] [blame] | 420 | return CMD_CTX_INVALID; |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 421 | } |
Keith Busch | 859361a | 2012-08-02 14:05:59 -0600 | [diff] [blame] | 422 | if (fn) |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 423 | *fn = cmd->fn; |
| 424 | ctx = cmd->ctx; |
| 425 | cmd->fn = special_completion; |
| 426 | cmd->ctx = CMD_CTX_COMPLETED; |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 427 | return ctx; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 428 | } |
| 429 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 430 | /** |
Matthew Wilcox | 714a7a2 | 2011-03-16 16:28:24 -0400 | [diff] [blame] | 431 | * 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] | 432 | * @nvmeq: The queue to use |
| 433 | * @cmd: The command to send |
| 434 | * |
| 435 | * Safe to use from interrupt context |
| 436 | */ |
Sunad Bhandary | e3f879b | 2015-07-31 18:56:58 +0530 | [diff] [blame] | 437 | static void __nvme_submit_cmd(struct nvme_queue *nvmeq, |
| 438 | struct nvme_command *cmd) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 439 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 440 | u16 tail = nvmeq->sq_tail; |
| 441 | |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 442 | if (nvmeq->sq_cmds_io) |
| 443 | memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd)); |
| 444 | else |
| 445 | memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd)); |
| 446 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 447 | if (++tail == nvmeq->q_depth) |
| 448 | tail = 0; |
Matthew Wilcox | 7547881 | 2011-02-16 09:59:59 -0500 | [diff] [blame] | 449 | writel(tail, nvmeq->q_db); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 450 | nvmeq->sq_tail = tail; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 451 | } |
| 452 | |
Sunad Bhandary | e3f879b | 2015-07-31 18:56:58 +0530 | [diff] [blame] | 453 | static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd) |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 454 | { |
| 455 | unsigned long flags; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 456 | spin_lock_irqsave(&nvmeq->q_lock, flags); |
Sunad Bhandary | e3f879b | 2015-07-31 18:56:58 +0530 | [diff] [blame] | 457 | __nvme_submit_cmd(nvmeq, cmd); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 458 | spin_unlock_irqrestore(&nvmeq->q_lock, flags); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 461 | static __le64 **iod_list(struct nvme_iod *iod) |
| 462 | { |
| 463 | return ((void *)iod) + iod->offset; |
| 464 | } |
| 465 | |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 466 | static inline void iod_init(struct nvme_iod *iod, unsigned nbytes, |
| 467 | unsigned nseg, unsigned long private) |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 468 | { |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 469 | iod->private = private; |
| 470 | iod->offset = offsetof(struct nvme_iod, sg[nseg]); |
| 471 | iod->npages = -1; |
| 472 | iod->length = nbytes; |
| 473 | iod->nents = 0; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static struct nvme_iod * |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 477 | __nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev, |
| 478 | unsigned long priv, gfp_t gfp) |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 479 | { |
| 480 | struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) + |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 481 | sizeof(__le64 *) * nvme_npages(bytes, dev) + |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 482 | sizeof(struct scatterlist) * nseg, gfp); |
| 483 | |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 484 | if (iod) |
| 485 | iod_init(iod, bytes, nseg, priv); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 486 | |
| 487 | return iod; |
| 488 | } |
| 489 | |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 490 | static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev, |
| 491 | gfp_t gfp) |
| 492 | { |
| 493 | unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) : |
| 494 | sizeof(struct nvme_dsm_range); |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 495 | struct nvme_iod *iod; |
| 496 | |
| 497 | if (rq->nr_phys_segments <= NVME_INT_PAGES && |
| 498 | size <= NVME_INT_BYTES(dev)) { |
| 499 | struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq); |
| 500 | |
| 501 | iod = cmd->iod; |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 502 | iod_init(iod, size, rq->nr_phys_segments, |
Chong Yuan | fda631f | 2015-03-27 09:21:32 +0800 | [diff] [blame] | 503 | (unsigned long) rq | NVME_INT_MASK); |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 504 | return iod; |
| 505 | } |
| 506 | |
| 507 | return __nvme_alloc_iod(rq->nr_phys_segments, size, dev, |
| 508 | (unsigned long) rq, gfp); |
| 509 | } |
| 510 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 511 | 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] | 512 | { |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 513 | const int last_prp = dev->ctrl.page_size / 8 - 1; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 514 | int i; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 515 | __le64 **list = iod_list(iod); |
| 516 | dma_addr_t prp_dma = iod->first_dma; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 517 | |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 518 | if (iod->npages == 0) |
| 519 | dma_pool_free(dev->prp_small_pool, list[0], prp_dma); |
| 520 | for (i = 0; i < iod->npages; i++) { |
| 521 | __le64 *prp_list = list[i]; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 522 | 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] | 523 | dma_pool_free(dev->prp_page_pool, prp_list, prp_dma); |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 524 | prp_dma = next_prp_dma; |
| 525 | } |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 526 | |
| 527 | if (iod_should_kfree(iod)) |
| 528 | kfree(iod); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 529 | } |
| 530 | |
Keith Busch | 52b68d7 | 2015-02-23 09:16:21 -0700 | [diff] [blame] | 531 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 532 | static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi) |
| 533 | { |
| 534 | if (be32_to_cpu(pi->ref_tag) == v) |
| 535 | pi->ref_tag = cpu_to_be32(p); |
| 536 | } |
| 537 | |
| 538 | static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi) |
| 539 | { |
| 540 | if (be32_to_cpu(pi->ref_tag) == p) |
| 541 | pi->ref_tag = cpu_to_be32(v); |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * nvme_dif_remap - remaps ref tags to bip seed and physical lba |
| 546 | * |
| 547 | * The virtual start sector is the one that was originally submitted by the |
| 548 | * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical |
| 549 | * start sector may be different. Remap protection information to match the |
| 550 | * physical LBA on writes, and back to the original seed on reads. |
| 551 | * |
| 552 | * Type 0 and 3 do not have a ref tag, so no remapping required. |
| 553 | */ |
| 554 | static void nvme_dif_remap(struct request *req, |
| 555 | void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi)) |
| 556 | { |
| 557 | struct nvme_ns *ns = req->rq_disk->private_data; |
| 558 | struct bio_integrity_payload *bip; |
| 559 | struct t10_pi_tuple *pi; |
| 560 | void *p, *pmap; |
| 561 | u32 i, nlb, ts, phys, virt; |
| 562 | |
| 563 | if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3) |
| 564 | return; |
| 565 | |
| 566 | bip = bio_integrity(req->bio); |
| 567 | if (!bip) |
| 568 | return; |
| 569 | |
| 570 | 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] | 571 | |
| 572 | p = pmap; |
| 573 | virt = bip_get_seed(bip); |
| 574 | phys = nvme_block_nr(ns, blk_rq_pos(req)); |
| 575 | nlb = (blk_rq_bytes(req) >> ns->lba_shift); |
Dan Williams | ac6fc48 | 2015-10-21 13:20:18 -0400 | [diff] [blame] | 576 | ts = ns->disk->queue->integrity.tuple_size; |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 577 | |
| 578 | for (i = 0; i < nlb; i++, virt++, phys++) { |
| 579 | pi = (struct t10_pi_tuple *)p; |
| 580 | dif_swap(phys, virt, pi); |
| 581 | p += ts; |
| 582 | } |
| 583 | kunmap_atomic(pmap); |
| 584 | } |
Keith Busch | 52b68d7 | 2015-02-23 09:16:21 -0700 | [diff] [blame] | 585 | #else /* CONFIG_BLK_DEV_INTEGRITY */ |
| 586 | static void nvme_dif_remap(struct request *req, |
| 587 | void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi)) |
| 588 | { |
| 589 | } |
| 590 | static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi) |
| 591 | { |
| 592 | } |
| 593 | static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi) |
| 594 | { |
| 595 | } |
Keith Busch | 52b68d7 | 2015-02-23 09:16:21 -0700 | [diff] [blame] | 596 | #endif |
| 597 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 598 | static void req_completion(struct nvme_queue *nvmeq, void *ctx, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 599 | struct nvme_completion *cqe) |
| 600 | { |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 601 | struct nvme_iod *iod = ctx; |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 602 | struct request *req = iod_get_private(iod); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 603 | struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 604 | u16 status = le16_to_cpup(&cqe->status) >> 1; |
Christoph Hellwig | 81c04b9 | 2015-10-12 21:23:39 +0200 | [diff] [blame] | 605 | int error = 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 606 | |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 607 | if (unlikely(status)) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 608 | if (!(status & NVME_SC_DNR || blk_noretry_request(req)) |
| 609 | && (jiffies - req->start_time) < req->timeout) { |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 610 | unsigned long flags; |
| 611 | |
Christoph Hellwig | d4f6c3a | 2015-11-26 10:51:23 +0100 | [diff] [blame] | 612 | nvme_unmap_data(nvmeq->dev, iod); |
| 613 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 614 | blk_mq_requeue_request(req); |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 615 | spin_lock_irqsave(req->q->queue_lock, flags); |
| 616 | if (!blk_queue_stopped(req->q)) |
| 617 | blk_mq_kick_requeue_list(req->q); |
| 618 | spin_unlock_irqrestore(req->q->queue_lock, flags); |
Christoph Hellwig | d4f6c3a | 2015-11-26 10:51:23 +0100 | [diff] [blame] | 619 | return; |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 620 | } |
Christoph Hellwig | f4829a9 | 2015-09-27 21:01:50 +0200 | [diff] [blame] | 621 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 622 | if (req->cmd_type == REQ_TYPE_DRV_PRIV) { |
Keith Busch | 17188bb | 2015-06-08 10:08:14 -0600 | [diff] [blame] | 623 | if (cmd_rq->ctx == CMD_CTX_CANCELLED) |
Christoph Hellwig | 81c04b9 | 2015-10-12 21:23:39 +0200 | [diff] [blame] | 624 | error = -EINTR; |
| 625 | else |
| 626 | error = status; |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 627 | } else { |
Christoph Hellwig | 81c04b9 | 2015-10-12 21:23:39 +0200 | [diff] [blame] | 628 | error = nvme_error_status(status); |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 629 | } |
Christoph Hellwig | f4829a9 | 2015-09-27 21:01:50 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Keith Busch | a0a931d | 2015-05-22 12:28:31 -0600 | [diff] [blame] | 632 | if (req->cmd_type == REQ_TYPE_DRV_PRIV) { |
| 633 | u32 result = le32_to_cpup(&cqe->result); |
| 634 | req->special = (void *)(uintptr_t)result; |
| 635 | } |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 636 | |
| 637 | if (cmd_rq->aborted) |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 638 | dev_warn(nvmeq->dev->dev, |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 639 | "completing aborted command with status:%04x\n", |
Christoph Hellwig | 81c04b9 | 2015-10-12 21:23:39 +0200 | [diff] [blame] | 640 | error); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 641 | |
Christoph Hellwig | d4f6c3a | 2015-11-26 10:51:23 +0100 | [diff] [blame] | 642 | nvme_unmap_data(nvmeq->dev, iod); |
| 643 | blk_mq_complete_request(req, error); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 644 | } |
| 645 | |
Christoph Hellwig | 69d2b57 | 2015-10-16 07:58:37 +0200 | [diff] [blame] | 646 | static bool nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, |
| 647 | int total_len) |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 648 | { |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 649 | struct dma_pool *pool; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 650 | int length = total_len; |
| 651 | struct scatterlist *sg = iod->sg; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 652 | int dma_len = sg_dma_len(sg); |
| 653 | u64 dma_addr = sg_dma_address(sg); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 654 | u32 page_size = dev->ctrl.page_size; |
Murali Iyer | f137e0f | 2015-03-26 11:07:51 -0500 | [diff] [blame] | 655 | int offset = dma_addr & (page_size - 1); |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 656 | __le64 *prp_list; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 657 | __le64 **list = iod_list(iod); |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 658 | dma_addr_t prp_dma; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 659 | int nprps, i; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 660 | |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 661 | length -= (page_size - offset); |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 662 | if (length <= 0) |
Christoph Hellwig | 69d2b57 | 2015-10-16 07:58:37 +0200 | [diff] [blame] | 663 | return true; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 664 | |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 665 | dma_len -= (page_size - offset); |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 666 | if (dma_len) { |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 667 | dma_addr += (page_size - offset); |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 668 | } else { |
| 669 | sg = sg_next(sg); |
| 670 | dma_addr = sg_dma_address(sg); |
| 671 | dma_len = sg_dma_len(sg); |
| 672 | } |
| 673 | |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 674 | if (length <= page_size) { |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 675 | iod->first_dma = dma_addr; |
Christoph Hellwig | 69d2b57 | 2015-10-16 07:58:37 +0200 | [diff] [blame] | 676 | return true; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 677 | } |
| 678 | |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 679 | nprps = DIV_ROUND_UP(length, page_size); |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 680 | if (nprps <= (256 / 8)) { |
| 681 | pool = dev->prp_small_pool; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 682 | iod->npages = 0; |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 683 | } else { |
| 684 | pool = dev->prp_page_pool; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 685 | iod->npages = 1; |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 686 | } |
| 687 | |
Christoph Hellwig | 69d2b57 | 2015-10-16 07:58:37 +0200 | [diff] [blame] | 688 | prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma); |
Matthew Wilcox | b77954c | 2011-05-12 13:51:41 -0400 | [diff] [blame] | 689 | if (!prp_list) { |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 690 | iod->first_dma = dma_addr; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 691 | iod->npages = -1; |
Christoph Hellwig | 69d2b57 | 2015-10-16 07:58:37 +0200 | [diff] [blame] | 692 | return false; |
Matthew Wilcox | b77954c | 2011-05-12 13:51:41 -0400 | [diff] [blame] | 693 | } |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 694 | list[0] = prp_list; |
| 695 | iod->first_dma = prp_dma; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 696 | i = 0; |
| 697 | for (;;) { |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 698 | if (i == page_size >> 3) { |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 699 | __le64 *old_prp_list = prp_list; |
Christoph Hellwig | 69d2b57 | 2015-10-16 07:58:37 +0200 | [diff] [blame] | 700 | prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma); |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 701 | if (!prp_list) |
Christoph Hellwig | 69d2b57 | 2015-10-16 07:58:37 +0200 | [diff] [blame] | 702 | return false; |
Matthew Wilcox | eca18b2 | 2011-12-20 13:34:52 -0500 | [diff] [blame] | 703 | list[iod->npages++] = prp_list; |
Matthew Wilcox | 7523d83 | 2011-03-16 16:43:40 -0400 | [diff] [blame] | 704 | prp_list[0] = old_prp_list[i - 1]; |
| 705 | old_prp_list[i - 1] = cpu_to_le64(prp_dma); |
| 706 | i = 1; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 707 | } |
| 708 | prp_list[i++] = cpu_to_le64(dma_addr); |
Keith Busch | 1d09062 | 2014-06-23 11:34:01 -0600 | [diff] [blame] | 709 | dma_len -= page_size; |
| 710 | dma_addr += page_size; |
| 711 | length -= page_size; |
Shane Michael Matthews | e025344c | 2011-02-10 08:51:24 -0500 | [diff] [blame] | 712 | if (length <= 0) |
| 713 | break; |
| 714 | if (dma_len > 0) |
| 715 | continue; |
| 716 | BUG_ON(dma_len < 0); |
| 717 | sg = sg_next(sg); |
| 718 | dma_addr = sg_dma_address(sg); |
| 719 | dma_len = sg_dma_len(sg); |
| 720 | } |
| 721 | |
Christoph Hellwig | 69d2b57 | 2015-10-16 07:58:37 +0200 | [diff] [blame] | 722 | return true; |
Matthew Wilcox | ff22b54 | 2011-01-26 10:02:29 -0500 | [diff] [blame] | 723 | } |
| 724 | |
Christoph Hellwig | ba1ca37 | 2015-10-16 07:58:38 +0200 | [diff] [blame] | 725 | static int nvme_map_data(struct nvme_dev *dev, struct nvme_iod *iod, |
| 726 | struct nvme_command *cmnd) |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 727 | { |
Christoph Hellwig | ba1ca37 | 2015-10-16 07:58:38 +0200 | [diff] [blame] | 728 | struct request *req = iod_get_private(iod); |
| 729 | struct request_queue *q = req->q; |
| 730 | enum dma_data_direction dma_dir = rq_data_dir(req) ? |
| 731 | DMA_TO_DEVICE : DMA_FROM_DEVICE; |
| 732 | int ret = BLK_MQ_RQ_QUEUE_ERROR; |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 733 | |
Christoph Hellwig | ba1ca37 | 2015-10-16 07:58:38 +0200 | [diff] [blame] | 734 | sg_init_table(iod->sg, req->nr_phys_segments); |
| 735 | iod->nents = blk_rq_map_sg(q, req, iod->sg); |
| 736 | if (!iod->nents) |
| 737 | goto out; |
| 738 | |
| 739 | ret = BLK_MQ_RQ_QUEUE_BUSY; |
| 740 | if (!dma_map_sg(dev->dev, iod->sg, iod->nents, dma_dir)) |
| 741 | goto out; |
| 742 | |
| 743 | if (!nvme_setup_prps(dev, iod, blk_rq_bytes(req))) |
| 744 | goto out_unmap; |
| 745 | |
| 746 | ret = BLK_MQ_RQ_QUEUE_ERROR; |
| 747 | if (blk_integrity_rq(req)) { |
| 748 | if (blk_rq_count_integrity_sg(q, req->bio) != 1) |
| 749 | goto out_unmap; |
| 750 | |
| 751 | sg_init_table(iod->meta_sg, 1); |
| 752 | if (blk_rq_map_integrity_sg(q, req->bio, iod->meta_sg) != 1) |
| 753 | goto out_unmap; |
| 754 | |
| 755 | if (rq_data_dir(req)) |
| 756 | nvme_dif_remap(req, nvme_dif_prep); |
| 757 | |
| 758 | if (!dma_map_sg(dev->dev, iod->meta_sg, 1, dma_dir)) |
| 759 | goto out_unmap; |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 760 | } |
| 761 | |
Christoph Hellwig | ba1ca37 | 2015-10-16 07:58:38 +0200 | [diff] [blame] | 762 | cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg)); |
| 763 | cmnd->rw.prp2 = cpu_to_le64(iod->first_dma); |
| 764 | if (blk_integrity_rq(req)) |
| 765 | cmnd->rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg)); |
| 766 | return BLK_MQ_RQ_QUEUE_OK; |
| 767 | |
| 768 | out_unmap: |
| 769 | dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir); |
| 770 | out: |
| 771 | return ret; |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 772 | } |
| 773 | |
Christoph Hellwig | d4f6c3a | 2015-11-26 10:51:23 +0100 | [diff] [blame] | 774 | static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod) |
| 775 | { |
| 776 | struct request *req = iod_get_private(iod); |
| 777 | enum dma_data_direction dma_dir = rq_data_dir(req) ? |
| 778 | DMA_TO_DEVICE : DMA_FROM_DEVICE; |
| 779 | |
| 780 | if (iod->nents) { |
| 781 | dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir); |
| 782 | if (blk_integrity_rq(req)) { |
| 783 | if (!rq_data_dir(req)) |
| 784 | nvme_dif_remap(req, nvme_dif_complete); |
| 785 | dma_unmap_sg(dev->dev, iod->meta_sg, 1, dma_dir); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | nvme_free_iod(dev, iod); |
| 790 | } |
| 791 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 792 | /* |
| 793 | * We reuse the small pool to allocate the 16-byte range here as it is not |
| 794 | * worth having a special pool for these or additional cases to handle freeing |
| 795 | * the iod. |
| 796 | */ |
Christoph Hellwig | ba1ca37 | 2015-10-16 07:58:38 +0200 | [diff] [blame] | 797 | static int nvme_setup_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns, |
| 798 | struct nvme_iod *iod, struct nvme_command *cmnd) |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 799 | { |
Christoph Hellwig | ba1ca37 | 2015-10-16 07:58:38 +0200 | [diff] [blame] | 800 | struct request *req = iod_get_private(iod); |
| 801 | struct nvme_dsm_range *range; |
| 802 | |
| 803 | range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC, |
| 804 | &iod->first_dma); |
| 805 | if (!range) |
| 806 | return BLK_MQ_RQ_QUEUE_BUSY; |
| 807 | iod_list(iod)[0] = (__le64 *)range; |
| 808 | iod->npages = 0; |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 809 | |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 810 | range->cattr = cpu_to_le32(0); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 811 | range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift); |
| 812 | 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] | 813 | |
Christoph Hellwig | ba1ca37 | 2015-10-16 07:58:38 +0200 | [diff] [blame] | 814 | memset(cmnd, 0, sizeof(*cmnd)); |
| 815 | cmnd->dsm.opcode = nvme_cmd_dsm; |
| 816 | cmnd->dsm.nsid = cpu_to_le32(ns->ns_id); |
| 817 | cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma); |
| 818 | cmnd->dsm.nr = 0; |
| 819 | cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD); |
| 820 | return BLK_MQ_RQ_QUEUE_OK; |
Keith Busch | 0e5e4f0 | 2012-11-09 16:33:05 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 823 | /* |
| 824 | * NOTE: ns is NULL when called on the admin queue. |
| 825 | */ |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 826 | static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 827 | const struct blk_mq_queue_data *bd) |
Keith Busch | 53562be | 2014-04-29 11:41:29 -0600 | [diff] [blame] | 828 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 829 | struct nvme_ns *ns = hctx->queue->queuedata; |
| 830 | struct nvme_queue *nvmeq = hctx->driver_data; |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 831 | struct nvme_dev *dev = nvmeq->dev; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 832 | struct request *req = bd->rq; |
| 833 | struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req); |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 834 | struct nvme_iod *iod; |
Christoph Hellwig | ba1ca37 | 2015-10-16 07:58:38 +0200 | [diff] [blame] | 835 | struct nvme_command cmnd; |
| 836 | int ret = BLK_MQ_RQ_QUEUE_OK; |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 837 | |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 838 | /* |
| 839 | * If formated with metadata, require the block layer provide a buffer |
| 840 | * unless this namespace is formated such that the metadata can be |
| 841 | * stripped/generated by the controller with PRACT=1. |
| 842 | */ |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 843 | if (ns && ns->ms && !blk_integrity_rq(req)) { |
Keith Busch | 71feb36 | 2015-06-19 11:07:30 -0600 | [diff] [blame] | 844 | if (!(ns->pi_type && ns->ms == 8) && |
| 845 | req->cmd_type != REQ_TYPE_DRV_PRIV) { |
Christoph Hellwig | f4829a9 | 2015-09-27 21:01:50 +0200 | [diff] [blame] | 846 | blk_mq_complete_request(req, -EFAULT); |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 847 | return BLK_MQ_RQ_QUEUE_OK; |
| 848 | } |
| 849 | } |
| 850 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 851 | iod = nvme_alloc_iod(req, dev, GFP_ATOMIC); |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 852 | if (!iod) |
Jens Axboe | fe54303 | 2014-12-11 13:58:39 -0700 | [diff] [blame] | 853 | return BLK_MQ_RQ_QUEUE_BUSY; |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 854 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 855 | if (req->cmd_flags & REQ_DISCARD) { |
Christoph Hellwig | ba1ca37 | 2015-10-16 07:58:38 +0200 | [diff] [blame] | 856 | ret = nvme_setup_discard(nvmeq, ns, iod, &cmnd); |
| 857 | } else { |
| 858 | if (req->cmd_type == REQ_TYPE_DRV_PRIV) |
| 859 | memcpy(&cmnd, req->cmd, sizeof(cmnd)); |
| 860 | else if (req->cmd_flags & REQ_FLUSH) |
| 861 | nvme_setup_flush(ns, &cmnd); |
| 862 | else |
| 863 | nvme_setup_rw(ns, req, &cmnd); |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 864 | |
Christoph Hellwig | ba1ca37 | 2015-10-16 07:58:38 +0200 | [diff] [blame] | 865 | if (req->nr_phys_segments) |
| 866 | ret = nvme_map_data(dev, iod, &cmnd); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Christoph Hellwig | ba1ca37 | 2015-10-16 07:58:38 +0200 | [diff] [blame] | 869 | if (ret) |
| 870 | goto out; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 871 | |
Christoph Hellwig | ba1ca37 | 2015-10-16 07:58:38 +0200 | [diff] [blame] | 872 | cmnd.common.command_id = req->tag; |
| 873 | nvme_set_info(cmd, iod, req_completion); |
| 874 | |
| 875 | spin_lock_irq(&nvmeq->q_lock); |
| 876 | __nvme_submit_cmd(nvmeq, &cmnd); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 877 | nvme_process_cq(nvmeq); |
| 878 | spin_unlock_irq(&nvmeq->q_lock); |
| 879 | return BLK_MQ_RQ_QUEUE_OK; |
Christoph Hellwig | ba1ca37 | 2015-10-16 07:58:38 +0200 | [diff] [blame] | 880 | out: |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 881 | nvme_free_iod(dev, iod); |
Christoph Hellwig | ba1ca37 | 2015-10-16 07:58:38 +0200 | [diff] [blame] | 882 | return ret; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 883 | } |
| 884 | |
Jens Axboe | a0fa964 | 2015-11-03 20:37:26 -0700 | [diff] [blame] | 885 | static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 886 | { |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 887 | u16 head, phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 888 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 889 | head = nvmeq->cq_head; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 890 | phase = nvmeq->cq_phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 891 | |
| 892 | for (;;) { |
Matthew Wilcox | c2f5b65 | 2011-10-15 07:33:46 -0400 | [diff] [blame] | 893 | void *ctx; |
| 894 | nvme_completion_fn fn; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 895 | struct nvme_completion cqe = nvmeq->cqes[head]; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 896 | if ((le16_to_cpu(cqe.status) & 1) != phase) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 897 | break; |
| 898 | nvmeq->sq_head = le16_to_cpu(cqe.sq_head); |
| 899 | if (++head == nvmeq->q_depth) { |
| 900 | head = 0; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 901 | phase = !phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 902 | } |
Jens Axboe | a0fa964 | 2015-11-03 20:37:26 -0700 | [diff] [blame] | 903 | if (tag && *tag == cqe.command_id) |
| 904 | *tag = -1; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 905 | ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn); |
Keith Busch | edd10d3 | 2014-04-03 16:45:23 -0600 | [diff] [blame] | 906 | fn(nvmeq, ctx, &cqe); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /* If the controller ignores the cq head doorbell and continuously |
| 910 | * writes to the queue, it is theoretically possible to wrap around |
| 911 | * the queue twice and mistakenly return IRQ_NONE. Linux only |
| 912 | * requires that 0.1% of your interrupts are handled, so this isn't |
| 913 | * a big problem. |
| 914 | */ |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 915 | if (head == nvmeq->cq_head && phase == nvmeq->cq_phase) |
Jens Axboe | a0fa964 | 2015-11-03 20:37:26 -0700 | [diff] [blame] | 916 | return; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 917 | |
Keith Busch | 604e8c8 | 2015-11-20 08:38:13 -0700 | [diff] [blame] | 918 | if (likely(nvmeq->cq_vector >= 0)) |
| 919 | writel(head, nvmeq->q_db + nvmeq->dev->db_stride); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 920 | nvmeq->cq_head = head; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 921 | nvmeq->cq_phase = phase; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 922 | |
Matthew Wilcox | e9539f4 | 2013-06-24 11:47:34 -0400 | [diff] [blame] | 923 | nvmeq->cqe_seen = 1; |
Jens Axboe | a0fa964 | 2015-11-03 20:37:26 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | static void nvme_process_cq(struct nvme_queue *nvmeq) |
| 927 | { |
| 928 | __nvme_process_cq(nvmeq, NULL); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | static irqreturn_t nvme_irq(int irq, void *data) |
| 932 | { |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 933 | irqreturn_t result; |
| 934 | struct nvme_queue *nvmeq = data; |
| 935 | spin_lock(&nvmeq->q_lock); |
Matthew Wilcox | e9539f4 | 2013-06-24 11:47:34 -0400 | [diff] [blame] | 936 | nvme_process_cq(nvmeq); |
| 937 | result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE; |
| 938 | nvmeq->cqe_seen = 0; |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 939 | spin_unlock(&nvmeq->q_lock); |
| 940 | return result; |
| 941 | } |
| 942 | |
| 943 | static irqreturn_t nvme_irq_check(int irq, void *data) |
| 944 | { |
| 945 | struct nvme_queue *nvmeq = data; |
| 946 | struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head]; |
| 947 | if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase) |
| 948 | return IRQ_NONE; |
| 949 | return IRQ_WAKE_THREAD; |
| 950 | } |
| 951 | |
Jens Axboe | a0fa964 | 2015-11-03 20:37:26 -0700 | [diff] [blame] | 952 | static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag) |
| 953 | { |
| 954 | struct nvme_queue *nvmeq = hctx->driver_data; |
| 955 | |
| 956 | if ((le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) == |
| 957 | nvmeq->cq_phase) { |
| 958 | spin_lock_irq(&nvmeq->q_lock); |
| 959 | __nvme_process_cq(nvmeq, &tag); |
| 960 | spin_unlock_irq(&nvmeq->q_lock); |
| 961 | |
| 962 | if (tag == -1) |
| 963 | return 1; |
| 964 | } |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 969 | static int nvme_submit_async_admin_req(struct nvme_dev *dev) |
| 970 | { |
| 971 | struct nvme_queue *nvmeq = dev->queues[0]; |
| 972 | struct nvme_command c; |
| 973 | struct nvme_cmd_info *cmd_info; |
| 974 | struct request *req; |
| 975 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 976 | req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE, |
Christoph Hellwig | 6f3b0e8 | 2015-11-26 09:13:05 +0100 | [diff] [blame] | 977 | BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED); |
Dan Carpenter | 9f173b3 | 2014-11-05 23:39:09 +0300 | [diff] [blame] | 978 | if (IS_ERR(req)) |
| 979 | return PTR_ERR(req); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 980 | |
Keith Busch | c917dfe | 2015-01-07 18:55:48 -0700 | [diff] [blame] | 981 | req->cmd_flags |= REQ_NO_TIMEOUT; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 982 | cmd_info = blk_mq_rq_to_pdu(req); |
Keith Busch | 1efccc9 | 2015-03-31 10:37:17 -0600 | [diff] [blame] | 983 | nvme_set_info(cmd_info, NULL, async_req_completion); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 984 | |
| 985 | memset(&c, 0, sizeof(c)); |
| 986 | c.common.opcode = nvme_admin_async_event; |
| 987 | c.common.command_id = req->tag; |
| 988 | |
Keith Busch | 4248322 | 2015-06-01 09:29:54 -0600 | [diff] [blame] | 989 | blk_mq_free_request(req); |
Sunad Bhandary | e3f879b | 2015-07-31 18:56:58 +0530 | [diff] [blame] | 990 | __nvme_submit_cmd(nvmeq, &c); |
| 991 | return 0; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | static int nvme_submit_admin_async_cmd(struct nvme_dev *dev, |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 995 | struct nvme_command *cmd, |
| 996 | struct async_cmd_info *cmdinfo, unsigned timeout) |
| 997 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 998 | struct nvme_queue *nvmeq = dev->queues[0]; |
| 999 | struct request *req; |
| 1000 | struct nvme_cmd_info *cmd_rq; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1001 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1002 | req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE, 0); |
Dan Carpenter | 9f173b3 | 2014-11-05 23:39:09 +0300 | [diff] [blame] | 1003 | if (IS_ERR(req)) |
| 1004 | return PTR_ERR(req); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1005 | |
| 1006 | req->timeout = timeout; |
| 1007 | cmd_rq = blk_mq_rq_to_pdu(req); |
| 1008 | cmdinfo->req = req; |
| 1009 | nvme_set_info(cmd_rq, cmdinfo, async_completion); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1010 | cmdinfo->status = -EINTR; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1011 | |
| 1012 | cmd->common.command_id = req->tag; |
| 1013 | |
Sunad Bhandary | e3f879b | 2015-07-31 18:56:58 +0530 | [diff] [blame] | 1014 | nvme_submit_cmd(nvmeq, cmd); |
| 1015 | return 0; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1018 | static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) |
| 1019 | { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1020 | struct nvme_command c; |
| 1021 | |
| 1022 | memset(&c, 0, sizeof(c)); |
| 1023 | c.delete_queue.opcode = opcode; |
| 1024 | c.delete_queue.qid = cpu_to_le16(id); |
| 1025 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1026 | return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid, |
| 1030 | struct nvme_queue *nvmeq) |
| 1031 | { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1032 | struct nvme_command c; |
| 1033 | int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED; |
| 1034 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1035 | /* |
| 1036 | * Note: we (ab)use the fact the the prp fields survive if no data |
| 1037 | * is attached to the request. |
| 1038 | */ |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1039 | memset(&c, 0, sizeof(c)); |
| 1040 | c.create_cq.opcode = nvme_admin_create_cq; |
| 1041 | c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr); |
| 1042 | c.create_cq.cqid = cpu_to_le16(qid); |
| 1043 | c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1); |
| 1044 | c.create_cq.cq_flags = cpu_to_le16(flags); |
| 1045 | c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector); |
| 1046 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1047 | return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid, |
| 1051 | struct nvme_queue *nvmeq) |
| 1052 | { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1053 | struct nvme_command c; |
| 1054 | int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM; |
| 1055 | |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1056 | /* |
| 1057 | * Note: we (ab)use the fact the the prp fields survive if no data |
| 1058 | * is attached to the request. |
| 1059 | */ |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1060 | memset(&c, 0, sizeof(c)); |
| 1061 | c.create_sq.opcode = nvme_admin_create_sq; |
| 1062 | c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr); |
| 1063 | c.create_sq.sqid = cpu_to_le16(qid); |
| 1064 | c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1); |
| 1065 | c.create_sq.sq_flags = cpu_to_le16(flags); |
| 1066 | c.create_sq.cqid = cpu_to_le16(qid); |
| 1067 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1068 | return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid) |
| 1072 | { |
| 1073 | return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid); |
| 1074 | } |
| 1075 | |
| 1076 | static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid) |
| 1077 | { |
| 1078 | return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid); |
| 1079 | } |
| 1080 | |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1081 | /** |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1082 | * nvme_abort_req - Attempt aborting a request |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1083 | * |
| 1084 | * Schedule controller reset if the command was already aborted once before and |
| 1085 | * still hasn't been returned to the driver, or if this is the admin queue. |
| 1086 | */ |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1087 | static void nvme_abort_req(struct request *req) |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1088 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1089 | struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req); |
| 1090 | struct nvme_queue *nvmeq = cmd_rq->nvmeq; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1091 | struct nvme_dev *dev = nvmeq->dev; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1092 | struct request *abort_req; |
| 1093 | struct nvme_cmd_info *abort_cmd; |
| 1094 | struct nvme_command cmd; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1095 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1096 | if (!nvmeq->qid || cmd_rq->aborted) { |
Christoph Hellwig | 90667892 | 2015-10-02 18:49:23 +0200 | [diff] [blame] | 1097 | spin_lock(&dev_list_lock); |
| 1098 | if (!__nvme_reset(dev)) { |
| 1099 | dev_warn(dev->dev, |
| 1100 | "I/O %d QID %d timeout, reset controller\n", |
| 1101 | req->tag, nvmeq->qid); |
| 1102 | } |
| 1103 | spin_unlock(&dev_list_lock); |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1104 | return; |
| 1105 | } |
| 1106 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1107 | if (!dev->ctrl.abort_limit) |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1108 | return; |
| 1109 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1110 | abort_req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE, |
Christoph Hellwig | 6f3b0e8 | 2015-11-26 09:13:05 +0100 | [diff] [blame] | 1111 | BLK_MQ_REQ_NOWAIT); |
Dan Carpenter | 9f173b3 | 2014-11-05 23:39:09 +0300 | [diff] [blame] | 1112 | if (IS_ERR(abort_req)) |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1113 | return; |
| 1114 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1115 | abort_cmd = blk_mq_rq_to_pdu(abort_req); |
| 1116 | nvme_set_info(abort_cmd, abort_req, abort_completion); |
| 1117 | |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1118 | memset(&cmd, 0, sizeof(cmd)); |
| 1119 | cmd.abort.opcode = nvme_admin_abort_cmd; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1120 | cmd.abort.cid = req->tag; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1121 | cmd.abort.sqid = cpu_to_le16(nvmeq->qid); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1122 | cmd.abort.command_id = abort_req->tag; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1123 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1124 | --dev->ctrl.abort_limit; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1125 | cmd_rq->aborted = 1; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1126 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1127 | 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] | 1128 | nvmeq->qid); |
Sunad Bhandary | e3f879b | 2015-07-31 18:56:58 +0530 | [diff] [blame] | 1129 | nvme_submit_cmd(dev->queues[0], &cmd); |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Keith Busch | 4248322 | 2015-06-01 09:29:54 -0600 | [diff] [blame] | 1132 | static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved) |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1133 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1134 | struct nvme_queue *nvmeq = data; |
| 1135 | void *ctx; |
| 1136 | nvme_completion_fn fn; |
| 1137 | struct nvme_cmd_info *cmd; |
Keith Busch | cef6a94 | 2015-01-07 18:55:51 -0700 | [diff] [blame] | 1138 | struct nvme_completion cqe; |
| 1139 | |
| 1140 | if (!blk_mq_request_started(req)) |
| 1141 | return; |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1142 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1143 | cmd = blk_mq_rq_to_pdu(req); |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1144 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1145 | if (cmd->ctx == CMD_CTX_CANCELLED) |
| 1146 | return; |
| 1147 | |
Keith Busch | cef6a94 | 2015-01-07 18:55:51 -0700 | [diff] [blame] | 1148 | if (blk_queue_dying(req->q)) |
| 1149 | cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1); |
| 1150 | else |
| 1151 | cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1); |
| 1152 | |
| 1153 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1154 | dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", |
| 1155 | req->tag, nvmeq->qid); |
| 1156 | ctx = cancel_cmd_info(cmd, &fn); |
| 1157 | fn(nvmeq, ctx, &cqe); |
| 1158 | } |
| 1159 | |
| 1160 | static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved) |
| 1161 | { |
| 1162 | struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req); |
| 1163 | struct nvme_queue *nvmeq = cmd->nvmeq; |
| 1164 | |
Keith Busch | 07836e6 | 2015-02-19 10:34:48 -0700 | [diff] [blame] | 1165 | dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag, |
| 1166 | nvmeq->qid); |
| 1167 | spin_lock_irq(&nvmeq->q_lock); |
| 1168 | nvme_abort_req(req); |
| 1169 | spin_unlock_irq(&nvmeq->q_lock); |
| 1170 | |
Keith Busch | 7a509a6 | 2015-01-07 18:55:53 -0700 | [diff] [blame] | 1171 | /* |
| 1172 | * The aborted req will be completed on receiving the abort req. |
| 1173 | * We enable the timer again. If hit twice, it'll cause a device reset, |
| 1174 | * as the device then is in a faulty state. |
| 1175 | */ |
Keith Busch | 07836e6 | 2015-02-19 10:34:48 -0700 | [diff] [blame] | 1176 | return BLK_EH_RESET_TIMER; |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1177 | } |
| 1178 | |
Keith Busch | f435c28 | 2014-07-07 09:14:42 -0600 | [diff] [blame] | 1179 | static void nvme_free_queue(struct nvme_queue *nvmeq) |
Matthew Wilcox | 9e86677 | 2012-08-03 13:55:56 -0400 | [diff] [blame] | 1180 | { |
| 1181 | dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), |
| 1182 | (void *)nvmeq->cqes, nvmeq->cq_dma_addr); |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 1183 | if (nvmeq->sq_cmds) |
| 1184 | dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), |
Matthew Wilcox | 9e86677 | 2012-08-03 13:55:56 -0400 | [diff] [blame] | 1185 | nvmeq->sq_cmds, nvmeq->sq_dma_addr); |
| 1186 | kfree(nvmeq); |
| 1187 | } |
| 1188 | |
Keith Busch | a1a5ef9 | 2013-12-16 13:50:00 -0500 | [diff] [blame] | 1189 | static void nvme_free_queues(struct nvme_dev *dev, int lowest) |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1190 | { |
| 1191 | int i; |
| 1192 | |
Keith Busch | a1a5ef9 | 2013-12-16 13:50:00 -0500 | [diff] [blame] | 1193 | for (i = dev->queue_count - 1; i >= lowest; i--) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1194 | struct nvme_queue *nvmeq = dev->queues[i]; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1195 | dev->queue_count--; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1196 | dev->queues[i] = NULL; |
Keith Busch | f435c28 | 2014-07-07 09:14:42 -0600 | [diff] [blame] | 1197 | nvme_free_queue(nvmeq); |
kaoudis | 121c7ad | 2015-01-14 21:01:58 -0700 | [diff] [blame] | 1198 | } |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1199 | } |
| 1200 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1201 | /** |
| 1202 | * nvme_suspend_queue - put queue into suspended state |
| 1203 | * @nvmeq - queue to suspend |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1204 | */ |
| 1205 | static int nvme_suspend_queue(struct nvme_queue *nvmeq) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1206 | { |
Keith Busch | 2b25d98 | 2014-12-22 12:59:04 -0700 | [diff] [blame] | 1207 | int vector; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1208 | |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1209 | spin_lock_irq(&nvmeq->q_lock); |
Keith Busch | 2b25d98 | 2014-12-22 12:59:04 -0700 | [diff] [blame] | 1210 | if (nvmeq->cq_vector == -1) { |
| 1211 | spin_unlock_irq(&nvmeq->q_lock); |
| 1212 | return 1; |
| 1213 | } |
| 1214 | vector = nvmeq->dev->entry[nvmeq->cq_vector].vector; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1215 | nvmeq->dev->online_queues--; |
Keith Busch | 2b25d98 | 2014-12-22 12:59:04 -0700 | [diff] [blame] | 1216 | nvmeq->cq_vector = -1; |
Matthew Wilcox | a09115b | 2012-08-07 15:56:23 -0400 | [diff] [blame] | 1217 | spin_unlock_irq(&nvmeq->q_lock); |
| 1218 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1219 | if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q) |
| 1220 | blk_mq_freeze_queue_start(nvmeq->dev->ctrl.admin_q); |
Keith Busch | 6df3dbc | 2015-03-26 13:49:33 -0600 | [diff] [blame] | 1221 | |
Matthew Wilcox | aba2080 | 2011-03-27 08:52:06 -0400 | [diff] [blame] | 1222 | irq_set_affinity_hint(vector, NULL); |
| 1223 | free_irq(vector, nvmeq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1224 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1225 | return 0; |
| 1226 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1227 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1228 | static void nvme_clear_queue(struct nvme_queue *nvmeq) |
| 1229 | { |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1230 | spin_lock_irq(&nvmeq->q_lock); |
Keith Busch | 4248322 | 2015-06-01 09:29:54 -0600 | [diff] [blame] | 1231 | if (nvmeq->tags && *nvmeq->tags) |
| 1232 | blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1233 | spin_unlock_irq(&nvmeq->q_lock); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1234 | } |
| 1235 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1236 | static void nvme_disable_queue(struct nvme_dev *dev, int qid) |
| 1237 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1238 | struct nvme_queue *nvmeq = dev->queues[qid]; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1239 | |
| 1240 | if (!nvmeq) |
| 1241 | return; |
| 1242 | if (nvme_suspend_queue(nvmeq)) |
| 1243 | return; |
| 1244 | |
Keith Busch | 0e53d18 | 2013-12-10 13:10:39 -0700 | [diff] [blame] | 1245 | /* Don't tell the adapter to delete the admin queue. |
| 1246 | * Don't tell a removed adapter to delete IO queues. */ |
Christoph Hellwig | 7a67cbe | 2015-11-20 08:58:10 +0100 | [diff] [blame] | 1247 | if (qid && readl(dev->bar + NVME_REG_CSTS) != -1) { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1248 | adapter_delete_sq(dev, qid); |
| 1249 | adapter_delete_cq(dev, qid); |
| 1250 | } |
Keith Busch | 07836e6 | 2015-02-19 10:34:48 -0700 | [diff] [blame] | 1251 | |
| 1252 | spin_lock_irq(&nvmeq->q_lock); |
| 1253 | nvme_process_cq(nvmeq); |
| 1254 | spin_unlock_irq(&nvmeq->q_lock); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1255 | } |
| 1256 | |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 1257 | static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues, |
| 1258 | int entry_size) |
| 1259 | { |
| 1260 | int q_depth = dev->q_depth; |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1261 | unsigned q_size_aligned = roundup(q_depth * entry_size, |
| 1262 | dev->ctrl.page_size); |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 1263 | |
| 1264 | if (q_size_aligned * nr_io_queues > dev->cmb_size) { |
Jon Derrick | c45f5c9 | 2015-07-21 15:08:13 -0600 | [diff] [blame] | 1265 | u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1266 | mem_per_q = round_down(mem_per_q, dev->ctrl.page_size); |
Jon Derrick | c45f5c9 | 2015-07-21 15:08:13 -0600 | [diff] [blame] | 1267 | q_depth = div_u64(mem_per_q, entry_size); |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 1268 | |
| 1269 | /* |
| 1270 | * Ensure the reduced q_depth is above some threshold where it |
| 1271 | * would be better to map queues in system memory with the |
| 1272 | * original depth |
| 1273 | */ |
| 1274 | if (q_depth < 64) |
| 1275 | return -ENOMEM; |
| 1276 | } |
| 1277 | |
| 1278 | return q_depth; |
| 1279 | } |
| 1280 | |
| 1281 | static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq, |
| 1282 | int qid, int depth) |
| 1283 | { |
| 1284 | if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) { |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1285 | unsigned offset = (qid - 1) * roundup(SQ_SIZE(depth), |
| 1286 | dev->ctrl.page_size); |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 1287 | nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset; |
| 1288 | nvmeq->sq_cmds_io = dev->cmb + offset; |
| 1289 | } else { |
| 1290 | nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth), |
| 1291 | &nvmeq->sq_dma_addr, GFP_KERNEL); |
| 1292 | if (!nvmeq->sq_cmds) |
| 1293 | return -ENOMEM; |
| 1294 | } |
| 1295 | |
| 1296 | return 0; |
| 1297 | } |
| 1298 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1299 | 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] | 1300 | int depth) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1301 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1302 | struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1303 | if (!nvmeq) |
| 1304 | return NULL; |
| 1305 | |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1306 | nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth), |
Joe Perches | 4d51abf | 2014-06-15 13:37:33 -0700 | [diff] [blame] | 1307 | &nvmeq->cq_dma_addr, GFP_KERNEL); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1308 | if (!nvmeq->cqes) |
| 1309 | goto free_nvmeq; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1310 | |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 1311 | if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth)) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1312 | goto free_cqdma; |
| 1313 | |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1314 | nvmeq->q_dmadev = dev->dev; |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 1315 | nvmeq->dev = dev; |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 1316 | snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d", |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1317 | dev->ctrl.instance, qid); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1318 | spin_lock_init(&nvmeq->q_lock); |
| 1319 | nvmeq->cq_head = 0; |
Matthew Wilcox | 8212346 | 2011-01-20 13:24:06 -0500 | [diff] [blame] | 1320 | nvmeq->cq_phase = 1; |
Haiyan Hu | b80d5cc | 2013-09-10 11:25:37 +0800 | [diff] [blame] | 1321 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1322 | nvmeq->q_depth = depth; |
Keith Busch | c30341d | 2013-12-10 13:10:38 -0700 | [diff] [blame] | 1323 | nvmeq->qid = qid; |
Jon Derrick | 758dd7f | 2015-06-30 11:22:52 -0600 | [diff] [blame] | 1324 | nvmeq->cq_vector = -1; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1325 | dev->queues[qid] = nvmeq; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1326 | |
Jon Derrick | 36a7e99 | 2015-05-27 12:26:23 -0600 | [diff] [blame] | 1327 | /* make sure queue descriptor is set before queue count, for kthread */ |
| 1328 | mb(); |
| 1329 | dev->queue_count++; |
| 1330 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1331 | return nvmeq; |
| 1332 | |
| 1333 | free_cqdma: |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1334 | dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1335 | nvmeq->cq_dma_addr); |
| 1336 | free_nvmeq: |
| 1337 | kfree(nvmeq); |
| 1338 | return NULL; |
| 1339 | } |
| 1340 | |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 1341 | static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq, |
| 1342 | const char *name) |
| 1343 | { |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 1344 | if (use_threaded_interrupts) |
| 1345 | return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector, |
Michael Opdenacker | 481e5ba | 2013-10-12 06:23:29 +0200 | [diff] [blame] | 1346 | nvme_irq_check, nvme_irq, IRQF_SHARED, |
Matthew Wilcox | 58ffacb | 2011-02-06 07:28:06 -0500 | [diff] [blame] | 1347 | name, nvmeq); |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 1348 | return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq, |
Michael Opdenacker | 481e5ba | 2013-10-12 06:23:29 +0200 | [diff] [blame] | 1349 | IRQF_SHARED, name, nvmeq); |
Matthew Wilcox | 3001082 | 2011-01-20 09:10:15 -0500 | [diff] [blame] | 1350 | } |
| 1351 | |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1352 | static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1353 | { |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1354 | struct nvme_dev *dev = nvmeq->dev; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1355 | |
Keith Busch | 7be50e9 | 2014-09-10 15:48:47 -0600 | [diff] [blame] | 1356 | spin_lock_irq(&nvmeq->q_lock); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1357 | nvmeq->sq_tail = 0; |
| 1358 | nvmeq->cq_head = 0; |
| 1359 | nvmeq->cq_phase = 1; |
Haiyan Hu | b80d5cc | 2013-09-10 11:25:37 +0800 | [diff] [blame] | 1360 | nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1361 | memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth)); |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1362 | dev->online_queues++; |
Keith Busch | 7be50e9 | 2014-09-10 15:48:47 -0600 | [diff] [blame] | 1363 | spin_unlock_irq(&nvmeq->q_lock); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | static int nvme_create_queue(struct nvme_queue *nvmeq, int qid) |
| 1367 | { |
| 1368 | struct nvme_dev *dev = nvmeq->dev; |
| 1369 | int result; |
Matthew Wilcox | 3f85d50 | 2011-02-01 08:39:04 -0500 | [diff] [blame] | 1370 | |
Keith Busch | 2b25d98 | 2014-12-22 12:59:04 -0700 | [diff] [blame] | 1371 | nvmeq->cq_vector = qid - 1; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1372 | result = adapter_alloc_cq(dev, qid, nvmeq); |
| 1373 | if (result < 0) |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1374 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1375 | |
| 1376 | result = adapter_alloc_sq(dev, qid, nvmeq); |
| 1377 | if (result < 0) |
| 1378 | goto release_cq; |
| 1379 | |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 1380 | result = queue_request_irq(dev, nvmeq, nvmeq->irqname); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1381 | if (result < 0) |
| 1382 | goto release_sq; |
| 1383 | |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1384 | nvme_init_queue(nvmeq, qid); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1385 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1386 | |
| 1387 | release_sq: |
| 1388 | adapter_delete_sq(dev, qid); |
| 1389 | release_cq: |
| 1390 | adapter_delete_cq(dev, qid); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1391 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1392 | } |
| 1393 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1394 | static struct blk_mq_ops nvme_mq_admin_ops = { |
Christoph Hellwig | d29ec82 | 2015-05-22 11:12:46 +0200 | [diff] [blame] | 1395 | .queue_rq = nvme_queue_rq, |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1396 | .map_queue = blk_mq_map_queue, |
| 1397 | .init_hctx = nvme_admin_init_hctx, |
Keith Busch | 4af0e21 | 2015-06-08 10:08:13 -0600 | [diff] [blame] | 1398 | .exit_hctx = nvme_admin_exit_hctx, |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1399 | .init_request = nvme_admin_init_request, |
| 1400 | .timeout = nvme_timeout, |
| 1401 | }; |
| 1402 | |
| 1403 | static struct blk_mq_ops nvme_mq_ops = { |
| 1404 | .queue_rq = nvme_queue_rq, |
| 1405 | .map_queue = blk_mq_map_queue, |
| 1406 | .init_hctx = nvme_init_hctx, |
| 1407 | .init_request = nvme_init_request, |
| 1408 | .timeout = nvme_timeout, |
Jens Axboe | a0fa964 | 2015-11-03 20:37:26 -0700 | [diff] [blame] | 1409 | .poll = nvme_poll, |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1410 | }; |
| 1411 | |
Keith Busch | ea191d2 | 2015-01-07 18:55:49 -0700 | [diff] [blame] | 1412 | static void nvme_dev_remove_admin(struct nvme_dev *dev) |
| 1413 | { |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1414 | if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) { |
| 1415 | blk_cleanup_queue(dev->ctrl.admin_q); |
Keith Busch | ea191d2 | 2015-01-07 18:55:49 -0700 | [diff] [blame] | 1416 | blk_mq_free_tag_set(&dev->admin_tagset); |
| 1417 | } |
| 1418 | } |
| 1419 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1420 | static int nvme_alloc_admin_tags(struct nvme_dev *dev) |
| 1421 | { |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1422 | if (!dev->ctrl.admin_q) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1423 | dev->admin_tagset.ops = &nvme_mq_admin_ops; |
| 1424 | dev->admin_tagset.nr_hw_queues = 1; |
| 1425 | dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1; |
Keith Busch | 1efccc9 | 2015-03-31 10:37:17 -0600 | [diff] [blame] | 1426 | dev->admin_tagset.reserved_tags = 1; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1427 | dev->admin_tagset.timeout = ADMIN_TIMEOUT; |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1428 | dev->admin_tagset.numa_node = dev_to_node(dev->dev); |
Jens Axboe | ac3dd5b | 2015-01-22 12:07:58 -0700 | [diff] [blame] | 1429 | dev->admin_tagset.cmd_size = nvme_cmd_size(dev); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1430 | dev->admin_tagset.driver_data = dev; |
| 1431 | |
| 1432 | if (blk_mq_alloc_tag_set(&dev->admin_tagset)) |
| 1433 | return -ENOMEM; |
| 1434 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1435 | dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset); |
| 1436 | if (IS_ERR(dev->ctrl.admin_q)) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1437 | blk_mq_free_tag_set(&dev->admin_tagset); |
| 1438 | return -ENOMEM; |
| 1439 | } |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1440 | if (!blk_get_queue(dev->ctrl.admin_q)) { |
Keith Busch | ea191d2 | 2015-01-07 18:55:49 -0700 | [diff] [blame] | 1441 | nvme_dev_remove_admin(dev); |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1442 | dev->ctrl.admin_q = NULL; |
Keith Busch | ea191d2 | 2015-01-07 18:55:49 -0700 | [diff] [blame] | 1443 | return -ENODEV; |
| 1444 | } |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 1445 | } else |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1446 | blk_mq_unfreeze_queue(dev->ctrl.admin_q); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1447 | |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1451 | static int nvme_configure_admin_queue(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1452 | { |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1453 | int result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1454 | u32 aqa; |
Christoph Hellwig | 7a67cbe | 2015-11-20 08:58:10 +0100 | [diff] [blame] | 1455 | u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1456 | struct nvme_queue *nvmeq; |
| 1457 | |
Christoph Hellwig | 7a67cbe | 2015-11-20 08:58:10 +0100 | [diff] [blame] | 1458 | dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1) ? |
Keith Busch | dfbac8c | 2015-08-10 15:20:40 -0600 | [diff] [blame] | 1459 | NVME_CAP_NSSRC(cap) : 0; |
| 1460 | |
Christoph Hellwig | 7a67cbe | 2015-11-20 08:58:10 +0100 | [diff] [blame] | 1461 | if (dev->subsystem && |
| 1462 | (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO)) |
| 1463 | writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS); |
Keith Busch | dfbac8c | 2015-08-10 15:20:40 -0600 | [diff] [blame] | 1464 | |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1465 | result = nvme_disable_ctrl(&dev->ctrl, cap); |
Matthew Wilcox | ba47e38 | 2013-05-04 06:43:16 -0400 | [diff] [blame] | 1466 | if (result < 0) |
| 1467 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1468 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1469 | nvmeq = dev->queues[0]; |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 1470 | if (!nvmeq) { |
Keith Busch | 2b25d98 | 2014-12-22 12:59:04 -0700 | [diff] [blame] | 1471 | nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH); |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 1472 | if (!nvmeq) |
| 1473 | return -ENOMEM; |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 1474 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1475 | |
| 1476 | aqa = nvmeq->q_depth - 1; |
| 1477 | aqa |= aqa << 16; |
| 1478 | |
Christoph Hellwig | 7a67cbe | 2015-11-20 08:58:10 +0100 | [diff] [blame] | 1479 | writel(aqa, dev->bar + NVME_REG_AQA); |
| 1480 | lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ); |
| 1481 | lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1482 | |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1483 | result = nvme_enable_ctrl(&dev->ctrl, cap); |
Keith Busch | 025c557 | 2013-05-01 13:07:51 -0600 | [diff] [blame] | 1484 | if (result) |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1485 | goto free_nvmeq; |
| 1486 | |
Keith Busch | 2b25d98 | 2014-12-22 12:59:04 -0700 | [diff] [blame] | 1487 | nvmeq->cq_vector = 0; |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 1488 | result = queue_request_irq(dev, nvmeq, nvmeq->irqname); |
Jon Derrick | 758dd7f | 2015-06-30 11:22:52 -0600 | [diff] [blame] | 1489 | if (result) { |
| 1490 | nvmeq->cq_vector = -1; |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 1491 | goto free_nvmeq; |
Jon Derrick | 758dd7f | 2015-06-30 11:22:52 -0600 | [diff] [blame] | 1492 | } |
Keith Busch | 025c557 | 2013-05-01 13:07:51 -0600 | [diff] [blame] | 1493 | |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1494 | return result; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1495 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1496 | free_nvmeq: |
| 1497 | nvme_free_queues(dev, 0); |
| 1498 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1499 | } |
| 1500 | |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1501 | static int nvme_kthread(void *data) |
| 1502 | { |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 1503 | struct nvme_dev *dev, *next; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1504 | |
| 1505 | while (!kthread_should_stop()) { |
Arjan van de Ven | 564a232 | 2013-05-01 16:38:23 -0400 | [diff] [blame] | 1506 | set_current_state(TASK_INTERRUPTIBLE); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1507 | spin_lock(&dev_list_lock); |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 1508 | list_for_each_entry_safe(dev, next, &dev_list, node) { |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1509 | int i; |
Christoph Hellwig | 7a67cbe | 2015-11-20 08:58:10 +0100 | [diff] [blame] | 1510 | u32 csts = readl(dev->bar + NVME_REG_CSTS); |
Keith Busch | dfbac8c | 2015-08-10 15:20:40 -0600 | [diff] [blame] | 1511 | |
| 1512 | if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) || |
| 1513 | csts & NVME_CSTS_CFS) { |
Christoph Hellwig | 90667892 | 2015-10-02 18:49:23 +0200 | [diff] [blame] | 1514 | if (!__nvme_reset(dev)) { |
| 1515 | dev_warn(dev->dev, |
| 1516 | "Failed status: %x, reset controller\n", |
Christoph Hellwig | 7a67cbe | 2015-11-20 08:58:10 +0100 | [diff] [blame] | 1517 | readl(dev->bar + NVME_REG_CSTS)); |
Christoph Hellwig | 90667892 | 2015-10-02 18:49:23 +0200 | [diff] [blame] | 1518 | } |
Keith Busch | d4b4ff8 | 2013-12-10 13:10:37 -0700 | [diff] [blame] | 1519 | continue; |
| 1520 | } |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1521 | for (i = 0; i < dev->queue_count; i++) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1522 | struct nvme_queue *nvmeq = dev->queues[i]; |
Matthew Wilcox | 740216f | 2011-02-15 16:28:20 -0500 | [diff] [blame] | 1523 | if (!nvmeq) |
| 1524 | continue; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1525 | spin_lock_irq(&nvmeq->q_lock); |
Matthew Wilcox | bc57a0f | 2013-06-24 11:56:42 -0400 | [diff] [blame] | 1526 | nvme_process_cq(nvmeq); |
Keith Busch | 6fccf93 | 2014-06-18 13:58:57 -0600 | [diff] [blame] | 1527 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1528 | while (i == 0 && dev->ctrl.event_limit > 0) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1529 | if (nvme_submit_async_admin_req(dev)) |
Keith Busch | 6fccf93 | 2014-06-18 13:58:57 -0600 | [diff] [blame] | 1530 | break; |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1531 | dev->ctrl.event_limit--; |
Keith Busch | 6fccf93 | 2014-06-18 13:58:57 -0600 | [diff] [blame] | 1532 | } |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1533 | spin_unlock_irq(&nvmeq->q_lock); |
| 1534 | } |
| 1535 | } |
| 1536 | spin_unlock(&dev_list_lock); |
Arjan van de Ven | acb7aa0 | 2013-02-04 14:44:33 -0800 | [diff] [blame] | 1537 | schedule_timeout(round_jiffies_relative(HZ)); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 1538 | } |
| 1539 | return 0; |
| 1540 | } |
| 1541 | |
Christoph Hellwig | 749941f | 2015-11-26 11:46:39 +0100 | [diff] [blame] | 1542 | static int nvme_create_io_queues(struct nvme_dev *dev) |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1543 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1544 | unsigned i; |
Christoph Hellwig | 749941f | 2015-11-26 11:46:39 +0100 | [diff] [blame] | 1545 | int ret = 0; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1546 | |
Christoph Hellwig | 749941f | 2015-11-26 11:46:39 +0100 | [diff] [blame] | 1547 | for (i = dev->queue_count; i <= dev->max_qid; i++) { |
| 1548 | if (!nvme_alloc_queue(dev, i, dev->q_depth)) { |
| 1549 | ret = -ENOMEM; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1550 | break; |
Christoph Hellwig | 749941f | 2015-11-26 11:46:39 +0100 | [diff] [blame] | 1551 | } |
| 1552 | } |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1553 | |
Christoph Hellwig | 749941f | 2015-11-26 11:46:39 +0100 | [diff] [blame] | 1554 | for (i = dev->online_queues; i <= dev->queue_count - 1; i++) { |
| 1555 | ret = nvme_create_queue(dev->queues[i], i); |
| 1556 | if (ret) { |
Christoph Hellwig | 2659e57 | 2015-10-02 18:51:31 +0200 | [diff] [blame] | 1557 | nvme_free_queues(dev, i); |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1558 | break; |
Christoph Hellwig | 2659e57 | 2015-10-02 18:51:31 +0200 | [diff] [blame] | 1559 | } |
Christoph Hellwig | 749941f | 2015-11-26 11:46:39 +0100 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | /* |
| 1563 | * Ignore failing Create SQ/CQ commands, we can continue with less |
| 1564 | * than the desired aount of queues, and even a controller without |
| 1565 | * I/O queues an still be used to issue admin commands. This might |
| 1566 | * be useful to upgrade a buggy firmware for example. |
| 1567 | */ |
| 1568 | return ret >= 0 ? 0 : ret; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1569 | } |
| 1570 | |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 1571 | static void __iomem *nvme_map_cmb(struct nvme_dev *dev) |
| 1572 | { |
| 1573 | u64 szu, size, offset; |
| 1574 | u32 cmbloc; |
| 1575 | resource_size_t bar_size; |
| 1576 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 1577 | void __iomem *cmb; |
| 1578 | dma_addr_t dma_addr; |
| 1579 | |
| 1580 | if (!use_cmb_sqes) |
| 1581 | return NULL; |
| 1582 | |
Christoph Hellwig | 7a67cbe | 2015-11-20 08:58:10 +0100 | [diff] [blame] | 1583 | dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ); |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 1584 | if (!(NVME_CMB_SZ(dev->cmbsz))) |
| 1585 | return NULL; |
| 1586 | |
Christoph Hellwig | 7a67cbe | 2015-11-20 08:58:10 +0100 | [diff] [blame] | 1587 | cmbloc = readl(dev->bar + NVME_REG_CMBLOC); |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 1588 | |
| 1589 | szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz)); |
| 1590 | size = szu * NVME_CMB_SZ(dev->cmbsz); |
| 1591 | offset = szu * NVME_CMB_OFST(cmbloc); |
| 1592 | bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc)); |
| 1593 | |
| 1594 | if (offset > bar_size) |
| 1595 | return NULL; |
| 1596 | |
| 1597 | /* |
| 1598 | * Controllers may support a CMB size larger than their BAR, |
| 1599 | * for example, due to being behind a bridge. Reduce the CMB to |
| 1600 | * the reported size of the BAR |
| 1601 | */ |
| 1602 | if (size > bar_size - offset) |
| 1603 | size = bar_size - offset; |
| 1604 | |
| 1605 | dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset; |
| 1606 | cmb = ioremap_wc(dma_addr, size); |
| 1607 | if (!cmb) |
| 1608 | return NULL; |
| 1609 | |
| 1610 | dev->cmb_dma_addr = dma_addr; |
| 1611 | dev->cmb_size = size; |
| 1612 | return cmb; |
| 1613 | } |
| 1614 | |
| 1615 | static inline void nvme_release_cmb(struct nvme_dev *dev) |
| 1616 | { |
| 1617 | if (dev->cmb) { |
| 1618 | iounmap(dev->cmb); |
| 1619 | dev->cmb = NULL; |
| 1620 | } |
| 1621 | } |
| 1622 | |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 1623 | static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues) |
| 1624 | { |
Haiyan Hu | b80d5cc | 2013-09-10 11:25:37 +0800 | [diff] [blame] | 1625 | return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride); |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 1626 | } |
| 1627 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1628 | static int nvme_setup_io_queues(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1629 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1630 | struct nvme_queue *adminq = dev->queues[0]; |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1631 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1632 | int result, i, vecs, nr_io_queues, size; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1633 | |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1634 | nr_io_queues = num_possible_cpus(); |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 1635 | result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues); |
| 1636 | if (result < 0) |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 1637 | return result; |
Christoph Hellwig | 9a0be7a | 2015-11-26 11:09:06 +0100 | [diff] [blame] | 1638 | |
| 1639 | /* |
| 1640 | * Degraded controllers might return an error when setting the queue |
| 1641 | * count. We still want to be able to bring them online and offer |
| 1642 | * access to the admin queue, as that might be only way to fix them up. |
| 1643 | */ |
| 1644 | if (result > 0) { |
| 1645 | dev_err(dev->dev, "Could not set queue count (%d)\n", result); |
| 1646 | nr_io_queues = 0; |
| 1647 | result = 0; |
| 1648 | } |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1649 | |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 1650 | if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) { |
| 1651 | result = nvme_cmb_qdepth(dev, nr_io_queues, |
| 1652 | sizeof(struct nvme_command)); |
| 1653 | if (result > 0) |
| 1654 | dev->q_depth = result; |
| 1655 | else |
| 1656 | nvme_release_cmb(dev); |
| 1657 | } |
| 1658 | |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 1659 | size = db_bar_size(dev, nr_io_queues); |
| 1660 | if (size > 8192) { |
Matthew Wilcox | f1938f6 | 2011-10-20 17:00:41 -0400 | [diff] [blame] | 1661 | iounmap(dev->bar); |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 1662 | do { |
| 1663 | dev->bar = ioremap(pci_resource_start(pdev, 0), size); |
| 1664 | if (dev->bar) |
| 1665 | break; |
| 1666 | if (!--nr_io_queues) |
| 1667 | return -ENOMEM; |
| 1668 | size = db_bar_size(dev, nr_io_queues); |
| 1669 | } while (1); |
Christoph Hellwig | 7a67cbe | 2015-11-20 08:58:10 +0100 | [diff] [blame] | 1670 | dev->dbs = dev->bar + 4096; |
Keith Busch | 5a92e70 | 2014-02-21 14:13:44 -0700 | [diff] [blame] | 1671 | adminq->q_db = dev->dbs; |
Matthew Wilcox | f1938f6 | 2011-10-20 17:00:41 -0400 | [diff] [blame] | 1672 | } |
| 1673 | |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 1674 | /* Deregister the admin queue's interrupt */ |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 1675 | free_irq(dev->entry[0].vector, adminq); |
Keith Busch | 9d713c2 | 2013-07-15 15:02:24 -0600 | [diff] [blame] | 1676 | |
Jens Axboe | e32efbf | 2014-11-14 09:49:26 -0700 | [diff] [blame] | 1677 | /* |
| 1678 | * If we enable msix early due to not intx, disable it again before |
| 1679 | * setting up the full range we need. |
| 1680 | */ |
| 1681 | if (!pdev->irq) |
| 1682 | pci_disable_msix(pdev); |
| 1683 | |
Alexander Gordeev | be577fa | 2014-03-04 16:22:00 +0100 | [diff] [blame] | 1684 | for (i = 0; i < nr_io_queues; i++) |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 1685 | dev->entry[i].entry = i; |
Alexander Gordeev | be577fa | 2014-03-04 16:22:00 +0100 | [diff] [blame] | 1686 | vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues); |
| 1687 | if (vecs < 0) { |
| 1688 | vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32)); |
| 1689 | if (vecs < 0) { |
| 1690 | vecs = 1; |
| 1691 | } else { |
| 1692 | for (i = 0; i < vecs; i++) |
| 1693 | dev->entry[i].vector = i + pdev->irq; |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 1694 | } |
| 1695 | } |
| 1696 | |
Matthew Wilcox | 063a809 | 2013-06-20 10:53:48 -0400 | [diff] [blame] | 1697 | /* |
| 1698 | * Should investigate if there's a performance win from allocating |
| 1699 | * more queues than interrupt vectors; it might allow the submission |
| 1700 | * path to scale better, even if the receive path is limited by the |
| 1701 | * number of interrupts. |
| 1702 | */ |
| 1703 | nr_io_queues = vecs; |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1704 | dev->max_qid = nr_io_queues; |
Ramachandra Rao Gajula | fa08a39 | 2013-05-11 15:19:31 -0700 | [diff] [blame] | 1705 | |
Matthew Wilcox | 3193f07 | 2014-01-27 15:57:22 -0500 | [diff] [blame] | 1706 | result = queue_request_irq(dev, adminq, adminq->irqname); |
Jon Derrick | 758dd7f | 2015-06-30 11:22:52 -0600 | [diff] [blame] | 1707 | if (result) { |
| 1708 | adminq->cq_vector = -1; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1709 | goto free_queues; |
Jon Derrick | 758dd7f | 2015-06-30 11:22:52 -0600 | [diff] [blame] | 1710 | } |
Matthew Wilcox | 1b23484 | 2011-01-20 13:01:49 -0500 | [diff] [blame] | 1711 | |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 1712 | /* Free previously allocated queues that are no longer usable */ |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1713 | nvme_free_queues(dev, nr_io_queues + 1); |
Christoph Hellwig | 749941f | 2015-11-26 11:46:39 +0100 | [diff] [blame] | 1714 | return nvme_create_io_queues(dev); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1715 | |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1716 | free_queues: |
Keith Busch | a1a5ef9 | 2013-12-16 13:50:00 -0500 | [diff] [blame] | 1717 | nvme_free_queues(dev, 1); |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 1718 | return result; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1719 | } |
| 1720 | |
Keith Busch | bda4e0f | 2015-09-03 08:18:17 -0600 | [diff] [blame] | 1721 | static void nvme_set_irq_hints(struct nvme_dev *dev) |
| 1722 | { |
| 1723 | struct nvme_queue *nvmeq; |
| 1724 | int i; |
| 1725 | |
| 1726 | for (i = 0; i < dev->online_queues; i++) { |
| 1727 | nvmeq = dev->queues[i]; |
| 1728 | |
| 1729 | if (!nvmeq->tags || !(*nvmeq->tags)) |
| 1730 | continue; |
| 1731 | |
| 1732 | irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector, |
| 1733 | blk_mq_tags_cpumask(*nvmeq->tags)); |
| 1734 | } |
| 1735 | } |
| 1736 | |
Keith Busch | a5768aa | 2015-06-01 14:28:14 -0600 | [diff] [blame] | 1737 | static void nvme_dev_scan(struct work_struct *work) |
| 1738 | { |
| 1739 | struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work); |
Keith Busch | a5768aa | 2015-06-01 14:28:14 -0600 | [diff] [blame] | 1740 | |
| 1741 | if (!dev->tagset.tags) |
| 1742 | return; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1743 | nvme_scan_namespaces(&dev->ctrl); |
Keith Busch | bda4e0f | 2015-09-03 08:18:17 -0600 | [diff] [blame] | 1744 | nvme_set_irq_hints(dev); |
Keith Busch | a5768aa | 2015-06-01 14:28:14 -0600 | [diff] [blame] | 1745 | } |
| 1746 | |
Matthew Wilcox | 422ef0c | 2013-04-16 11:22:36 -0400 | [diff] [blame] | 1747 | /* |
| 1748 | * Return: error value if an error occurred setting up the queues or calling |
| 1749 | * Identify Device. 0 if these succeeded, even if adding some of the |
| 1750 | * namespaces failed. At the moment, these failures are silent. TBD which |
| 1751 | * failures should be reported. |
| 1752 | */ |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1753 | static int nvme_dev_add(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1754 | { |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1755 | if (!dev->ctrl.tagset) { |
Keith Busch | ffe7704 | 2015-06-08 10:08:15 -0600 | [diff] [blame] | 1756 | dev->tagset.ops = &nvme_mq_ops; |
| 1757 | dev->tagset.nr_hw_queues = dev->online_queues - 1; |
| 1758 | dev->tagset.timeout = NVME_IO_TIMEOUT; |
| 1759 | dev->tagset.numa_node = dev_to_node(dev->dev); |
| 1760 | dev->tagset.queue_depth = |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1761 | min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1; |
Keith Busch | ffe7704 | 2015-06-08 10:08:15 -0600 | [diff] [blame] | 1762 | dev->tagset.cmd_size = nvme_cmd_size(dev); |
| 1763 | dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE; |
| 1764 | dev->tagset.driver_data = dev; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1765 | |
Keith Busch | ffe7704 | 2015-06-08 10:08:15 -0600 | [diff] [blame] | 1766 | if (blk_mq_alloc_tag_set(&dev->tagset)) |
| 1767 | return 0; |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 1768 | dev->ctrl.tagset = &dev->tagset; |
Keith Busch | ffe7704 | 2015-06-08 10:08:15 -0600 | [diff] [blame] | 1769 | } |
Keith Busch | a5768aa | 2015-06-01 14:28:14 -0600 | [diff] [blame] | 1770 | schedule_work(&dev->scan_work); |
Keith Busch | e1e5e56 | 2015-02-19 13:39:03 -0700 | [diff] [blame] | 1771 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 1772 | } |
| 1773 | |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 1774 | static int nvme_dev_map(struct nvme_dev *dev) |
| 1775 | { |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1776 | u64 cap; |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 1777 | int bars, result = -ENOMEM; |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1778 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 1779 | |
| 1780 | if (pci_enable_device_mem(pdev)) |
| 1781 | return result; |
| 1782 | |
| 1783 | dev->entry[0].vector = pdev->irq; |
| 1784 | pci_set_master(pdev); |
| 1785 | bars = pci_select_bars(pdev, IORESOURCE_MEM); |
Jens Axboe | be7837e | 2014-11-14 09:50:19 -0700 | [diff] [blame] | 1786 | if (!bars) |
| 1787 | goto disable_pci; |
| 1788 | |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 1789 | if (pci_request_selected_regions(pdev, bars, "nvme")) |
| 1790 | goto disable_pci; |
| 1791 | |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1792 | if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) && |
| 1793 | dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32))) |
Russell King | 052d0ef | 2013-06-26 23:49:11 +0100 | [diff] [blame] | 1794 | goto disable; |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 1795 | |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 1796 | dev->bar = ioremap(pci_resource_start(pdev, 0), 8192); |
| 1797 | if (!dev->bar) |
| 1798 | goto disable; |
Jens Axboe | e32efbf | 2014-11-14 09:49:26 -0700 | [diff] [blame] | 1799 | |
Christoph Hellwig | 7a67cbe | 2015-11-20 08:58:10 +0100 | [diff] [blame] | 1800 | if (readl(dev->bar + NVME_REG_CSTS) == -1) { |
Keith Busch | 0e53d18 | 2013-12-10 13:10:39 -0700 | [diff] [blame] | 1801 | result = -ENODEV; |
| 1802 | goto unmap; |
| 1803 | } |
Jens Axboe | e32efbf | 2014-11-14 09:49:26 -0700 | [diff] [blame] | 1804 | |
| 1805 | /* |
| 1806 | * Some devices don't advertse INTx interrupts, pre-enable a single |
| 1807 | * MSIX vec for setup. We'll adjust this later. |
| 1808 | */ |
| 1809 | if (!pdev->irq) { |
| 1810 | result = pci_enable_msix(pdev, dev->entry, 1); |
| 1811 | if (result < 0) |
| 1812 | goto unmap; |
| 1813 | } |
| 1814 | |
Christoph Hellwig | 7a67cbe | 2015-11-20 08:58:10 +0100 | [diff] [blame] | 1815 | cap = lo_hi_readq(dev->bar + NVME_REG_CAP); |
| 1816 | |
Keith Busch | 42f6142 | 2014-03-24 10:46:25 -0600 | [diff] [blame] | 1817 | dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH); |
| 1818 | dev->db_stride = 1 << NVME_CAP_STRIDE(cap); |
Christoph Hellwig | 7a67cbe | 2015-11-20 08:58:10 +0100 | [diff] [blame] | 1819 | dev->dbs = dev->bar + 4096; |
| 1820 | if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2)) |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 1821 | dev->cmb = nvme_map_cmb(dev); |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 1822 | |
| 1823 | return 0; |
| 1824 | |
Keith Busch | 0e53d18 | 2013-12-10 13:10:39 -0700 | [diff] [blame] | 1825 | unmap: |
| 1826 | iounmap(dev->bar); |
| 1827 | dev->bar = NULL; |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 1828 | disable: |
| 1829 | pci_release_regions(pdev); |
| 1830 | disable_pci: |
| 1831 | pci_disable_device(pdev); |
| 1832 | return result; |
| 1833 | } |
| 1834 | |
| 1835 | static void nvme_dev_unmap(struct nvme_dev *dev) |
| 1836 | { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1837 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
| 1838 | |
| 1839 | if (pdev->msi_enabled) |
| 1840 | pci_disable_msi(pdev); |
| 1841 | else if (pdev->msix_enabled) |
| 1842 | pci_disable_msix(pdev); |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 1843 | |
| 1844 | if (dev->bar) { |
| 1845 | iounmap(dev->bar); |
| 1846 | dev->bar = NULL; |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1847 | pci_release_regions(pdev); |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 1848 | } |
| 1849 | |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1850 | if (pci_is_enabled(pdev)) |
| 1851 | pci_disable_device(pdev); |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 1852 | } |
| 1853 | |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1854 | struct nvme_delq_ctx { |
| 1855 | struct task_struct *waiter; |
| 1856 | struct kthread_worker *worker; |
| 1857 | atomic_t refcount; |
| 1858 | }; |
| 1859 | |
| 1860 | static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev) |
| 1861 | { |
| 1862 | dq->waiter = current; |
| 1863 | mb(); |
| 1864 | |
| 1865 | for (;;) { |
| 1866 | set_current_state(TASK_KILLABLE); |
| 1867 | if (!atomic_read(&dq->refcount)) |
| 1868 | break; |
| 1869 | if (!schedule_timeout(ADMIN_TIMEOUT) || |
| 1870 | fatal_signal_pending(current)) { |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 1871 | /* |
| 1872 | * Disable the controller first since we can't trust it |
| 1873 | * at this point, but leave the admin queue enabled |
| 1874 | * until all queue deletion requests are flushed. |
| 1875 | * FIXME: This may take a while if there are more h/w |
| 1876 | * queues than admin tags. |
| 1877 | */ |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1878 | set_current_state(TASK_RUNNING); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 1879 | nvme_disable_ctrl(&dev->ctrl, |
Christoph Hellwig | 7a67cbe | 2015-11-20 08:58:10 +0100 | [diff] [blame] | 1880 | lo_hi_readq(dev->bar + NVME_REG_CAP)); |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 1881 | nvme_clear_queue(dev->queues[0]); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1882 | flush_kthread_worker(dq->worker); |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 1883 | nvme_disable_queue(dev, 0); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1884 | return; |
| 1885 | } |
| 1886 | } |
| 1887 | set_current_state(TASK_RUNNING); |
| 1888 | } |
| 1889 | |
| 1890 | static void nvme_put_dq(struct nvme_delq_ctx *dq) |
| 1891 | { |
| 1892 | atomic_dec(&dq->refcount); |
| 1893 | if (dq->waiter) |
| 1894 | wake_up_process(dq->waiter); |
| 1895 | } |
| 1896 | |
| 1897 | static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq) |
| 1898 | { |
| 1899 | atomic_inc(&dq->refcount); |
| 1900 | return dq; |
| 1901 | } |
| 1902 | |
| 1903 | static void nvme_del_queue_end(struct nvme_queue *nvmeq) |
| 1904 | { |
| 1905 | struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1906 | nvme_put_dq(dq); |
Keith Busch | 604e8c8 | 2015-11-20 08:38:13 -0700 | [diff] [blame] | 1907 | |
| 1908 | spin_lock_irq(&nvmeq->q_lock); |
| 1909 | nvme_process_cq(nvmeq); |
| 1910 | spin_unlock_irq(&nvmeq->q_lock); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode, |
| 1914 | kthread_work_func_t fn) |
| 1915 | { |
| 1916 | struct nvme_command c; |
| 1917 | |
| 1918 | memset(&c, 0, sizeof(c)); |
| 1919 | c.delete_queue.opcode = opcode; |
| 1920 | c.delete_queue.qid = cpu_to_le16(nvmeq->qid); |
| 1921 | |
| 1922 | init_kthread_work(&nvmeq->cmdinfo.work, fn); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1923 | return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo, |
| 1924 | ADMIN_TIMEOUT); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | static void nvme_del_cq_work_handler(struct kthread_work *work) |
| 1928 | { |
| 1929 | struct nvme_queue *nvmeq = container_of(work, struct nvme_queue, |
| 1930 | cmdinfo.work); |
| 1931 | nvme_del_queue_end(nvmeq); |
| 1932 | } |
| 1933 | |
| 1934 | static int nvme_delete_cq(struct nvme_queue *nvmeq) |
| 1935 | { |
| 1936 | return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq, |
| 1937 | nvme_del_cq_work_handler); |
| 1938 | } |
| 1939 | |
| 1940 | static void nvme_del_sq_work_handler(struct kthread_work *work) |
| 1941 | { |
| 1942 | struct nvme_queue *nvmeq = container_of(work, struct nvme_queue, |
| 1943 | cmdinfo.work); |
| 1944 | int status = nvmeq->cmdinfo.status; |
| 1945 | |
| 1946 | if (!status) |
| 1947 | status = nvme_delete_cq(nvmeq); |
| 1948 | if (status) |
| 1949 | nvme_del_queue_end(nvmeq); |
| 1950 | } |
| 1951 | |
| 1952 | static int nvme_delete_sq(struct nvme_queue *nvmeq) |
| 1953 | { |
| 1954 | return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq, |
| 1955 | nvme_del_sq_work_handler); |
| 1956 | } |
| 1957 | |
| 1958 | static void nvme_del_queue_start(struct kthread_work *work) |
| 1959 | { |
| 1960 | struct nvme_queue *nvmeq = container_of(work, struct nvme_queue, |
| 1961 | cmdinfo.work); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1962 | if (nvme_delete_sq(nvmeq)) |
| 1963 | nvme_del_queue_end(nvmeq); |
| 1964 | } |
| 1965 | |
| 1966 | static void nvme_disable_io_queues(struct nvme_dev *dev) |
| 1967 | { |
| 1968 | int i; |
| 1969 | DEFINE_KTHREAD_WORKER_ONSTACK(worker); |
| 1970 | struct nvme_delq_ctx dq; |
| 1971 | struct task_struct *kworker_task = kthread_run(kthread_worker_fn, |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 1972 | &worker, "nvme%d", dev->ctrl.instance); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1973 | |
| 1974 | if (IS_ERR(kworker_task)) { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 1975 | dev_err(dev->dev, |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1976 | "Failed to create queue del task\n"); |
| 1977 | for (i = dev->queue_count - 1; i > 0; i--) |
| 1978 | nvme_disable_queue(dev, i); |
| 1979 | return; |
| 1980 | } |
| 1981 | |
| 1982 | dq.waiter = NULL; |
| 1983 | atomic_set(&dq.refcount, 0); |
| 1984 | dq.worker = &worker; |
| 1985 | for (i = dev->queue_count - 1; i > 0; i--) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 1986 | struct nvme_queue *nvmeq = dev->queues[i]; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 1987 | |
| 1988 | if (nvme_suspend_queue(nvmeq)) |
| 1989 | continue; |
| 1990 | nvmeq->cmdinfo.ctx = nvme_get_dq(&dq); |
| 1991 | nvmeq->cmdinfo.worker = dq.worker; |
| 1992 | init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start); |
| 1993 | queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work); |
| 1994 | } |
| 1995 | nvme_wait_dq(&dq, dev); |
| 1996 | kthread_stop(kworker_task); |
| 1997 | } |
| 1998 | |
Christoph Hellwig | 7385014 | 2015-10-22 14:03:33 +0200 | [diff] [blame] | 1999 | static int nvme_dev_list_add(struct nvme_dev *dev) |
| 2000 | { |
| 2001 | bool start_thread = false; |
| 2002 | |
| 2003 | spin_lock(&dev_list_lock); |
| 2004 | if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) { |
| 2005 | start_thread = true; |
| 2006 | nvme_thread = NULL; |
| 2007 | } |
| 2008 | list_add(&dev->node, &dev_list); |
| 2009 | spin_unlock(&dev_list_lock); |
| 2010 | |
| 2011 | if (start_thread) { |
| 2012 | nvme_thread = kthread_run(nvme_kthread, NULL, "nvme"); |
| 2013 | wake_up_all(&nvme_kthread_wait); |
| 2014 | } else |
| 2015 | wait_event_killable(nvme_kthread_wait, nvme_thread); |
| 2016 | |
| 2017 | if (IS_ERR_OR_NULL(nvme_thread)) |
| 2018 | return nvme_thread ? PTR_ERR(nvme_thread) : -EINTR; |
| 2019 | |
| 2020 | return 0; |
| 2021 | } |
| 2022 | |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 2023 | /* |
| 2024 | * Remove the node from the device list and check |
| 2025 | * for whether or not we need to stop the nvme_thread. |
| 2026 | */ |
| 2027 | static void nvme_dev_list_remove(struct nvme_dev *dev) |
| 2028 | { |
| 2029 | struct task_struct *tmp = NULL; |
| 2030 | |
| 2031 | spin_lock(&dev_list_lock); |
| 2032 | list_del_init(&dev->node); |
| 2033 | if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) { |
| 2034 | tmp = nvme_thread; |
| 2035 | nvme_thread = NULL; |
| 2036 | } |
| 2037 | spin_unlock(&dev_list_lock); |
| 2038 | |
| 2039 | if (tmp) |
| 2040 | kthread_stop(tmp); |
| 2041 | } |
| 2042 | |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 2043 | static void nvme_freeze_queues(struct nvme_dev *dev) |
| 2044 | { |
| 2045 | struct nvme_ns *ns; |
| 2046 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2047 | list_for_each_entry(ns, &dev->ctrl.namespaces, list) { |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 2048 | blk_mq_freeze_queue_start(ns->queue); |
| 2049 | |
Christoph Hellwig | cddcd72 | 2015-05-07 09:38:14 +0200 | [diff] [blame] | 2050 | spin_lock_irq(ns->queue->queue_lock); |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 2051 | queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue); |
Christoph Hellwig | cddcd72 | 2015-05-07 09:38:14 +0200 | [diff] [blame] | 2052 | spin_unlock_irq(ns->queue->queue_lock); |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 2053 | |
| 2054 | blk_mq_cancel_requeue_work(ns->queue); |
| 2055 | blk_mq_stop_hw_queues(ns->queue); |
| 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | static void nvme_unfreeze_queues(struct nvme_dev *dev) |
| 2060 | { |
| 2061 | struct nvme_ns *ns; |
| 2062 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2063 | list_for_each_entry(ns, &dev->ctrl.namespaces, list) { |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 2064 | queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue); |
| 2065 | blk_mq_unfreeze_queue(ns->queue); |
| 2066 | blk_mq_start_stopped_hw_queues(ns->queue, true); |
| 2067 | blk_mq_kick_requeue_list(ns->queue); |
| 2068 | } |
| 2069 | } |
| 2070 | |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2071 | static void nvme_dev_shutdown(struct nvme_dev *dev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2072 | { |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 2073 | int i; |
Keith Busch | 7c1b245 | 2014-06-25 11:18:12 -0600 | [diff] [blame] | 2074 | u32 csts = -1; |
Keith Busch | 2240427 | 2013-07-15 15:02:20 -0600 | [diff] [blame] | 2075 | |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 2076 | nvme_dev_list_remove(dev); |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 2077 | |
Keith Busch | 77bf25e | 2015-11-26 12:21:29 +0100 | [diff] [blame^] | 2078 | mutex_lock(&dev->shutdown_lock); |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 2079 | if (dev->bar) { |
| 2080 | nvme_freeze_queues(dev); |
Christoph Hellwig | 7a67cbe | 2015-11-20 08:58:10 +0100 | [diff] [blame] | 2081 | csts = readl(dev->bar + NVME_REG_CSTS); |
Keith Busch | c9d3bf8 | 2015-01-07 18:55:52 -0700 | [diff] [blame] | 2082 | } |
Keith Busch | 7c1b245 | 2014-06-25 11:18:12 -0600 | [diff] [blame] | 2083 | if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) { |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2084 | for (i = dev->queue_count - 1; i >= 0; i--) { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2085 | struct nvme_queue *nvmeq = dev->queues[i]; |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2086 | nvme_suspend_queue(nvmeq); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2087 | } |
| 2088 | } else { |
| 2089 | nvme_disable_io_queues(dev); |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 2090 | nvme_shutdown_ctrl(&dev->ctrl); |
Keith Busch | 4d11542 | 2013-12-10 13:10:40 -0700 | [diff] [blame] | 2091 | nvme_disable_queue(dev, 0); |
| 2092 | } |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2093 | nvme_dev_unmap(dev); |
Keith Busch | 07836e6 | 2015-02-19 10:34:48 -0700 | [diff] [blame] | 2094 | |
| 2095 | for (i = dev->queue_count - 1; i >= 0; i--) |
| 2096 | nvme_clear_queue(dev->queues[i]); |
Keith Busch | 77bf25e | 2015-11-26 12:21:29 +0100 | [diff] [blame^] | 2097 | mutex_unlock(&dev->shutdown_lock); |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2098 | } |
| 2099 | |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2100 | static int nvme_setup_prp_pools(struct nvme_dev *dev) |
| 2101 | { |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2102 | dev->prp_page_pool = dma_pool_create("prp list page", dev->dev, |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2103 | PAGE_SIZE, PAGE_SIZE, 0); |
| 2104 | if (!dev->prp_page_pool) |
| 2105 | return -ENOMEM; |
| 2106 | |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 2107 | /* Optimisation for I/Os between 4k and 128k */ |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2108 | dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev, |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 2109 | 256, 256, 0); |
| 2110 | if (!dev->prp_small_pool) { |
| 2111 | dma_pool_destroy(dev->prp_page_pool); |
| 2112 | return -ENOMEM; |
| 2113 | } |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2114 | return 0; |
| 2115 | } |
| 2116 | |
| 2117 | static void nvme_release_prp_pools(struct nvme_dev *dev) |
| 2118 | { |
| 2119 | dma_pool_destroy(dev->prp_page_pool); |
Matthew Wilcox | 99802a7 | 2011-02-10 10:30:34 -0500 | [diff] [blame] | 2120 | dma_pool_destroy(dev->prp_small_pool); |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2121 | } |
| 2122 | |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 2123 | static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl) |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 2124 | { |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 2125 | struct nvme_dev *dev = to_nvme_dev(ctrl); |
Keith Busch | 9ac2709 | 2014-01-31 16:53:39 -0700 | [diff] [blame] | 2126 | |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2127 | put_device(dev->dev); |
Keith Busch | 4af0e21 | 2015-06-08 10:08:13 -0600 | [diff] [blame] | 2128 | if (dev->tagset.tags) |
| 2129 | blk_mq_free_tag_set(&dev->tagset); |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 2130 | if (dev->ctrl.admin_q) |
| 2131 | blk_put_queue(dev->ctrl.admin_q); |
Keith Busch | 5e82e95 | 2013-02-19 10:17:58 -0700 | [diff] [blame] | 2132 | kfree(dev->queues); |
| 2133 | kfree(dev->entry); |
| 2134 | kfree(dev); |
| 2135 | } |
| 2136 | |
Christoph Hellwig | 3cf519b | 2015-10-03 09:49:23 +0200 | [diff] [blame] | 2137 | static void nvme_probe_work(struct work_struct *work) |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2138 | { |
Christoph Hellwig | 3cf519b | 2015-10-03 09:49:23 +0200 | [diff] [blame] | 2139 | struct nvme_dev *dev = container_of(work, struct nvme_dev, probe_work); |
Christoph Hellwig | 3cf519b | 2015-10-03 09:49:23 +0200 | [diff] [blame] | 2140 | int result; |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2141 | |
| 2142 | result = nvme_dev_map(dev); |
| 2143 | if (result) |
Christoph Hellwig | 3cf519b | 2015-10-03 09:49:23 +0200 | [diff] [blame] | 2144 | goto out; |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2145 | |
| 2146 | result = nvme_configure_admin_queue(dev); |
| 2147 | if (result) |
| 2148 | goto unmap; |
| 2149 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2150 | nvme_init_queue(dev->queues[0], 0); |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 2151 | result = nvme_alloc_admin_tags(dev); |
| 2152 | if (result) |
| 2153 | goto disable; |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 2154 | |
Christoph Hellwig | ce4541f | 2015-10-16 07:58:46 +0200 | [diff] [blame] | 2155 | result = nvme_init_identify(&dev->ctrl); |
| 2156 | if (result) |
| 2157 | goto free_tags; |
| 2158 | |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2159 | result = nvme_setup_io_queues(dev); |
Keith Busch | badc34d | 2014-06-23 14:25:35 -0600 | [diff] [blame] | 2160 | if (result) |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 2161 | goto free_tags; |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2162 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 2163 | dev->ctrl.event_limit = 1; |
Christoph Hellwig | 3cf519b | 2015-10-03 09:49:23 +0200 | [diff] [blame] | 2164 | |
Christoph Hellwig | 7385014 | 2015-10-22 14:03:33 +0200 | [diff] [blame] | 2165 | result = nvme_dev_list_add(dev); |
| 2166 | if (result) |
| 2167 | goto remove; |
| 2168 | |
Christoph Hellwig | 2659e57 | 2015-10-02 18:51:31 +0200 | [diff] [blame] | 2169 | /* |
| 2170 | * Keep the controller around but remove all namespaces if we don't have |
| 2171 | * any working I/O queue. |
| 2172 | */ |
Christoph Hellwig | 3cf519b | 2015-10-03 09:49:23 +0200 | [diff] [blame] | 2173 | if (dev->online_queues < 2) { |
| 2174 | dev_warn(dev->dev, "IO queues not created\n"); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2175 | nvme_remove_namespaces(&dev->ctrl); |
Christoph Hellwig | 3cf519b | 2015-10-03 09:49:23 +0200 | [diff] [blame] | 2176 | } else { |
| 2177 | nvme_unfreeze_queues(dev); |
| 2178 | nvme_dev_add(dev); |
| 2179 | } |
| 2180 | |
| 2181 | return; |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2182 | |
Christoph Hellwig | 7385014 | 2015-10-22 14:03:33 +0200 | [diff] [blame] | 2183 | remove: |
| 2184 | nvme_dev_list_remove(dev); |
Keith Busch | 0fb59cb | 2015-01-07 18:55:50 -0700 | [diff] [blame] | 2185 | free_tags: |
| 2186 | nvme_dev_remove_admin(dev); |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 2187 | blk_put_queue(dev->ctrl.admin_q); |
| 2188 | dev->ctrl.admin_q = NULL; |
Keith Busch | 4af0e21 | 2015-06-08 10:08:13 -0600 | [diff] [blame] | 2189 | dev->queues[0]->tags = NULL; |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2190 | disable: |
Keith Busch | a1a5ef9 | 2013-12-16 13:50:00 -0500 | [diff] [blame] | 2191 | nvme_disable_queue(dev, 0); |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2192 | unmap: |
| 2193 | nvme_dev_unmap(dev); |
Christoph Hellwig | 3cf519b | 2015-10-03 09:49:23 +0200 | [diff] [blame] | 2194 | out: |
| 2195 | if (!work_busy(&dev->reset_work)) |
| 2196 | nvme_dead_ctrl(dev); |
Keith Busch | f0b5073 | 2013-07-15 15:02:21 -0600 | [diff] [blame] | 2197 | } |
| 2198 | |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2199 | static int nvme_remove_dead_ctrl(void *arg) |
| 2200 | { |
| 2201 | struct nvme_dev *dev = (struct nvme_dev *)arg; |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2202 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2203 | |
| 2204 | if (pci_get_drvdata(pdev)) |
Keith Busch | c81f497 | 2014-06-23 15:24:53 -0600 | [diff] [blame] | 2205 | pci_stop_and_remove_bus_device_locked(pdev); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 2206 | nvme_put_ctrl(&dev->ctrl); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2207 | return 0; |
| 2208 | } |
| 2209 | |
Keith Busch | de3eff2 | 2015-06-18 13:36:39 -0600 | [diff] [blame] | 2210 | static void nvme_dead_ctrl(struct nvme_dev *dev) |
| 2211 | { |
| 2212 | dev_warn(dev->dev, "Device failed to resume\n"); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 2213 | kref_get(&dev->ctrl.kref); |
Keith Busch | de3eff2 | 2015-06-18 13:36:39 -0600 | [diff] [blame] | 2214 | if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d", |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 2215 | dev->ctrl.instance))) { |
Keith Busch | de3eff2 | 2015-06-18 13:36:39 -0600 | [diff] [blame] | 2216 | dev_err(dev->dev, |
| 2217 | "Failed to start controller remove task\n"); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 2218 | nvme_put_ctrl(&dev->ctrl); |
Keith Busch | de3eff2 | 2015-06-18 13:36:39 -0600 | [diff] [blame] | 2219 | } |
| 2220 | } |
| 2221 | |
Christoph Hellwig | 77b50d9 | 2015-10-02 17:41:18 +0200 | [diff] [blame] | 2222 | static void nvme_reset_work(struct work_struct *ws) |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2223 | { |
Christoph Hellwig | 77b50d9 | 2015-10-02 17:41:18 +0200 | [diff] [blame] | 2224 | struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work); |
Keith Busch | ffe7704 | 2015-06-08 10:08:15 -0600 | [diff] [blame] | 2225 | bool in_probe = work_busy(&dev->probe_work); |
| 2226 | |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2227 | nvme_dev_shutdown(dev); |
Keith Busch | ffe7704 | 2015-06-08 10:08:15 -0600 | [diff] [blame] | 2228 | |
| 2229 | /* Synchronize with device probe so that work will see failure status |
| 2230 | * and exit gracefully without trying to schedule another reset */ |
| 2231 | flush_work(&dev->probe_work); |
| 2232 | |
| 2233 | /* Fail this device if reset occured during probe to avoid |
| 2234 | * infinite initialization loops. */ |
| 2235 | if (in_probe) { |
Keith Busch | de3eff2 | 2015-06-18 13:36:39 -0600 | [diff] [blame] | 2236 | nvme_dead_ctrl(dev); |
Keith Busch | ffe7704 | 2015-06-08 10:08:15 -0600 | [diff] [blame] | 2237 | return; |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2238 | } |
Keith Busch | ffe7704 | 2015-06-08 10:08:15 -0600 | [diff] [blame] | 2239 | /* Schedule device resume asynchronously so the reset work is available |
| 2240 | * to cleanup errors that may occur during reinitialization */ |
| 2241 | schedule_work(&dev->probe_work); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2242 | } |
| 2243 | |
Christoph Hellwig | 90667892 | 2015-10-02 18:49:23 +0200 | [diff] [blame] | 2244 | static int __nvme_reset(struct nvme_dev *dev) |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2245 | { |
Christoph Hellwig | 90667892 | 2015-10-02 18:49:23 +0200 | [diff] [blame] | 2246 | if (work_pending(&dev->reset_work)) |
| 2247 | return -EBUSY; |
| 2248 | list_del_init(&dev->node); |
| 2249 | queue_work(nvme_workq, &dev->reset_work); |
| 2250 | return 0; |
Tejun Heo | 9ca9737 | 2014-03-07 10:24:49 -0500 | [diff] [blame] | 2251 | } |
| 2252 | |
Keith Busch | 4cc0652 | 2015-06-05 10:30:08 -0600 | [diff] [blame] | 2253 | static int nvme_reset(struct nvme_dev *dev) |
| 2254 | { |
Christoph Hellwig | 90667892 | 2015-10-02 18:49:23 +0200 | [diff] [blame] | 2255 | int ret; |
Keith Busch | 4cc0652 | 2015-06-05 10:30:08 -0600 | [diff] [blame] | 2256 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 2257 | if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q)) |
Keith Busch | 4cc0652 | 2015-06-05 10:30:08 -0600 | [diff] [blame] | 2258 | return -ENODEV; |
| 2259 | |
| 2260 | spin_lock(&dev_list_lock); |
Christoph Hellwig | 90667892 | 2015-10-02 18:49:23 +0200 | [diff] [blame] | 2261 | ret = __nvme_reset(dev); |
Keith Busch | 4cc0652 | 2015-06-05 10:30:08 -0600 | [diff] [blame] | 2262 | spin_unlock(&dev_list_lock); |
| 2263 | |
| 2264 | if (!ret) { |
| 2265 | flush_work(&dev->reset_work); |
Keith Busch | ffe7704 | 2015-06-08 10:08:15 -0600 | [diff] [blame] | 2266 | flush_work(&dev->probe_work); |
Keith Busch | 4cc0652 | 2015-06-05 10:30:08 -0600 | [diff] [blame] | 2267 | return 0; |
| 2268 | } |
| 2269 | |
| 2270 | return ret; |
| 2271 | } |
| 2272 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 2273 | static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val) |
| 2274 | { |
| 2275 | *val = readl(to_nvme_dev(ctrl)->bar + off); |
| 2276 | return 0; |
| 2277 | } |
| 2278 | |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 2279 | static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val) |
| 2280 | { |
| 2281 | writel(val, to_nvme_dev(ctrl)->bar + off); |
| 2282 | return 0; |
| 2283 | } |
| 2284 | |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2285 | static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val) |
| 2286 | { |
| 2287 | *val = readq(to_nvme_dev(ctrl)->bar + off); |
| 2288 | return 0; |
| 2289 | } |
| 2290 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2291 | static bool nvme_pci_io_incapable(struct nvme_ctrl *ctrl) |
| 2292 | { |
| 2293 | struct nvme_dev *dev = to_nvme_dev(ctrl); |
| 2294 | |
| 2295 | return !dev->bar || dev->online_queues < 2; |
| 2296 | } |
| 2297 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2298 | static int nvme_pci_reset_ctrl(struct nvme_ctrl *ctrl) |
| 2299 | { |
| 2300 | return nvme_reset(to_nvme_dev(ctrl)); |
| 2301 | } |
| 2302 | |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 2303 | static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = { |
| 2304 | .reg_read32 = nvme_pci_reg_read32, |
Christoph Hellwig | 5fd4ce1 | 2015-11-28 15:03:49 +0100 | [diff] [blame] | 2305 | .reg_write32 = nvme_pci_reg_write32, |
Christoph Hellwig | 7fd8930 | 2015-11-28 15:37:52 +0100 | [diff] [blame] | 2306 | .reg_read64 = nvme_pci_reg_read64, |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2307 | .io_incapable = nvme_pci_io_incapable, |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2308 | .reset_ctrl = nvme_pci_reset_ctrl, |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 2309 | .free_ctrl = nvme_pci_free_ctrl, |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 2310 | }; |
| 2311 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 2312 | 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] | 2313 | { |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2314 | int node, result = -ENOMEM; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2315 | struct nvme_dev *dev; |
| 2316 | |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2317 | node = dev_to_node(&pdev->dev); |
| 2318 | if (node == NUMA_NO_NODE) |
| 2319 | set_dev_node(&pdev->dev, 0); |
| 2320 | |
| 2321 | dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2322 | if (!dev) |
| 2323 | return -ENOMEM; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2324 | dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry), |
| 2325 | GFP_KERNEL, node); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2326 | if (!dev->entry) |
| 2327 | goto free; |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2328 | dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *), |
| 2329 | GFP_KERNEL, node); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2330 | if (!dev->queues) |
| 2331 | goto free; |
| 2332 | |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2333 | dev->dev = get_device(&pdev->dev); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2334 | pci_set_drvdata(pdev, dev); |
Christoph Hellwig | 1c63dc6 | 2015-11-26 10:06:56 +0100 | [diff] [blame] | 2335 | |
Keith Busch | e6e96d7 | 2015-03-23 09:32:37 -0600 | [diff] [blame] | 2336 | INIT_LIST_HEAD(&dev->node); |
Keith Busch | a5768aa | 2015-06-01 14:28:14 -0600 | [diff] [blame] | 2337 | INIT_WORK(&dev->scan_work, nvme_dev_scan); |
Christoph Hellwig | 3cf519b | 2015-10-03 09:49:23 +0200 | [diff] [blame] | 2338 | INIT_WORK(&dev->probe_work, nvme_probe_work); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2339 | INIT_WORK(&dev->reset_work, nvme_reset_work); |
Keith Busch | 77bf25e | 2015-11-26 12:21:29 +0100 | [diff] [blame^] | 2340 | mutex_init(&dev->shutdown_lock); |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2341 | |
| 2342 | result = nvme_setup_prp_pools(dev); |
| 2343 | if (result) |
| 2344 | goto put_pci; |
| 2345 | |
| 2346 | result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops, |
| 2347 | id->driver_data); |
| 2348 | if (result) |
| 2349 | goto release_pools; |
| 2350 | |
Keith Busch | 2e1d844 | 2015-02-12 15:33:00 -0700 | [diff] [blame] | 2351 | schedule_work(&dev->probe_work); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2352 | return 0; |
| 2353 | |
Keith Busch | 0877cb0 | 2013-07-15 15:02:19 -0600 | [diff] [blame] | 2354 | release_pools: |
Matthew Wilcox | 091b609 | 2011-02-10 09:56:01 -0500 | [diff] [blame] | 2355 | nvme_release_prp_pools(dev); |
Keith Busch | a96d4f5 | 2014-08-19 19:15:59 -0600 | [diff] [blame] | 2356 | put_pci: |
Christoph Hellwig | e75ec75 | 2015-05-22 11:12:39 +0200 | [diff] [blame] | 2357 | put_device(dev->dev); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2358 | free: |
| 2359 | kfree(dev->queues); |
| 2360 | kfree(dev->entry); |
| 2361 | kfree(dev); |
| 2362 | return result; |
| 2363 | } |
| 2364 | |
Keith Busch | f0d54a5 | 2014-05-02 10:40:43 -0600 | [diff] [blame] | 2365 | static void nvme_reset_notify(struct pci_dev *pdev, bool prepare) |
| 2366 | { |
Keith Busch | a673947 | 2014-06-23 16:03:21 -0600 | [diff] [blame] | 2367 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
Keith Busch | f0d54a5 | 2014-05-02 10:40:43 -0600 | [diff] [blame] | 2368 | |
Keith Busch | a673947 | 2014-06-23 16:03:21 -0600 | [diff] [blame] | 2369 | if (prepare) |
| 2370 | nvme_dev_shutdown(dev); |
| 2371 | else |
Keith Busch | 0a7385a | 2015-10-02 10:37:29 -0600 | [diff] [blame] | 2372 | schedule_work(&dev->probe_work); |
Keith Busch | f0d54a5 | 2014-05-02 10:40:43 -0600 | [diff] [blame] | 2373 | } |
| 2374 | |
Keith Busch | 09ece14 | 2014-01-27 11:29:40 -0500 | [diff] [blame] | 2375 | static void nvme_shutdown(struct pci_dev *pdev) |
| 2376 | { |
| 2377 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
| 2378 | nvme_dev_shutdown(dev); |
| 2379 | } |
| 2380 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 2381 | static void nvme_remove(struct pci_dev *pdev) |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2382 | { |
| 2383 | struct nvme_dev *dev = pci_get_drvdata(pdev); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2384 | |
| 2385 | spin_lock(&dev_list_lock); |
| 2386 | list_del_init(&dev->node); |
| 2387 | spin_unlock(&dev_list_lock); |
| 2388 | |
| 2389 | pci_set_drvdata(pdev, NULL); |
Keith Busch | 2e1d844 | 2015-02-12 15:33:00 -0700 | [diff] [blame] | 2390 | flush_work(&dev->probe_work); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2391 | flush_work(&dev->reset_work); |
Keith Busch | a5768aa | 2015-06-01 14:28:14 -0600 | [diff] [blame] | 2392 | flush_work(&dev->scan_work); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2393 | nvme_remove_namespaces(&dev->ctrl); |
Keith Busch | 3399a3f | 2015-06-18 13:36:40 -0600 | [diff] [blame] | 2394 | nvme_dev_shutdown(dev); |
Matias Bjørling | a4aea56 | 2014-11-04 08:20:14 -0700 | [diff] [blame] | 2395 | nvme_dev_remove_admin(dev); |
| 2396 | nvme_free_queues(dev, 0); |
Jon Derrick | 8ffaadf | 2015-07-20 10:14:09 -0600 | [diff] [blame] | 2397 | nvme_release_cmb(dev); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2398 | nvme_release_prp_pools(dev); |
Christoph Hellwig | 1673f1f | 2015-11-26 10:54:19 +0100 | [diff] [blame] | 2399 | nvme_put_ctrl(&dev->ctrl); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | /* These functions are yet to be implemented */ |
| 2403 | #define nvme_error_detected NULL |
| 2404 | #define nvme_dump_registers NULL |
| 2405 | #define nvme_link_reset NULL |
| 2406 | #define nvme_slot_reset NULL |
| 2407 | #define nvme_error_resume NULL |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2408 | |
Jingoo Han | 671a601 | 2014-02-13 11:19:14 +0900 | [diff] [blame] | 2409 | #ifdef CONFIG_PM_SLEEP |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2410 | static int nvme_suspend(struct device *dev) |
| 2411 | { |
| 2412 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2413 | struct nvme_dev *ndev = pci_get_drvdata(pdev); |
| 2414 | |
| 2415 | nvme_dev_shutdown(ndev); |
| 2416 | return 0; |
| 2417 | } |
| 2418 | |
| 2419 | static int nvme_resume(struct device *dev) |
| 2420 | { |
| 2421 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2422 | struct nvme_dev *ndev = pci_get_drvdata(pdev); |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2423 | |
Keith Busch | 0a7385a | 2015-10-02 10:37:29 -0600 | [diff] [blame] | 2424 | schedule_work(&ndev->probe_work); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2425 | return 0; |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2426 | } |
Jingoo Han | 671a601 | 2014-02-13 11:19:14 +0900 | [diff] [blame] | 2427 | #endif |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2428 | |
| 2429 | 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] | 2430 | |
Stephen Hemminger | 1d35203 | 2012-09-07 09:33:17 -0700 | [diff] [blame] | 2431 | static const struct pci_error_handlers nvme_err_handler = { |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2432 | .error_detected = nvme_error_detected, |
| 2433 | .mmio_enabled = nvme_dump_registers, |
| 2434 | .link_reset = nvme_link_reset, |
| 2435 | .slot_reset = nvme_slot_reset, |
| 2436 | .resume = nvme_error_resume, |
Keith Busch | f0d54a5 | 2014-05-02 10:40:43 -0600 | [diff] [blame] | 2437 | .reset_notify = nvme_reset_notify, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2438 | }; |
| 2439 | |
| 2440 | /* Move to pci_ids.h later */ |
| 2441 | #define PCI_CLASS_STORAGE_EXPRESS 0x010802 |
| 2442 | |
Matthew Wilcox | 6eb0d69 | 2014-03-24 10:11:22 -0400 | [diff] [blame] | 2443 | static const struct pci_device_id nvme_id_table[] = { |
Christoph Hellwig | 106198e | 2015-11-26 10:07:41 +0100 | [diff] [blame] | 2444 | { PCI_VDEVICE(INTEL, 0x0953), |
| 2445 | .driver_data = NVME_QUIRK_STRIPE_SIZE, }, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2446 | { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) }, |
Stephan Günther | c74dc78 | 2015-11-04 00:49:45 +0100 | [diff] [blame] | 2447 | { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) }, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2448 | { 0, } |
| 2449 | }; |
| 2450 | MODULE_DEVICE_TABLE(pci, nvme_id_table); |
| 2451 | |
| 2452 | static struct pci_driver nvme_driver = { |
| 2453 | .name = "nvme", |
| 2454 | .id_table = nvme_id_table, |
| 2455 | .probe = nvme_probe, |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 2456 | .remove = nvme_remove, |
Keith Busch | 09ece14 | 2014-01-27 11:29:40 -0500 | [diff] [blame] | 2457 | .shutdown = nvme_shutdown, |
Keith Busch | cd63894 | 2013-07-15 15:02:23 -0600 | [diff] [blame] | 2458 | .driver = { |
| 2459 | .pm = &nvme_dev_pm_ops, |
| 2460 | }, |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2461 | .err_handler = &nvme_err_handler, |
| 2462 | }; |
| 2463 | |
| 2464 | static int __init nvme_init(void) |
| 2465 | { |
Matthew Wilcox | 0ac1314 | 2012-07-31 13:31:15 -0400 | [diff] [blame] | 2466 | int result; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 2467 | |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 2468 | init_waitqueue_head(&nvme_kthread_wait); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2469 | |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2470 | nvme_workq = create_singlethread_workqueue("nvme"); |
| 2471 | if (!nvme_workq) |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 2472 | return -ENOMEM; |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2473 | |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2474 | result = nvme_core_init(); |
Keith Busch | 5c42ea1 | 2012-07-25 16:05:18 -0600 | [diff] [blame] | 2475 | if (result < 0) |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2476 | goto kill_workq; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2477 | |
Keith Busch | f3db22f | 2014-06-11 11:51:35 -0600 | [diff] [blame] | 2478 | result = pci_register_driver(&nvme_driver); |
| 2479 | if (result) |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2480 | goto core_exit; |
Matthew Wilcox | 1fa6aea | 2011-03-02 18:37:18 -0500 | [diff] [blame] | 2481 | return 0; |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2482 | |
Christoph Hellwig | f3ca80f | 2015-11-28 15:40:19 +0100 | [diff] [blame] | 2483 | core_exit: |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2484 | nvme_core_exit(); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2485 | kill_workq: |
| 2486 | destroy_workqueue(nvme_workq); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2487 | return result; |
| 2488 | } |
| 2489 | |
| 2490 | static void __exit nvme_exit(void) |
| 2491 | { |
| 2492 | pci_unregister_driver(&nvme_driver); |
Christoph Hellwig | 5bae7f7 | 2015-11-28 15:39:07 +0100 | [diff] [blame] | 2493 | nvme_core_exit(); |
Keith Busch | 9a6b945 | 2013-12-10 13:10:36 -0700 | [diff] [blame] | 2494 | destroy_workqueue(nvme_workq); |
Dan McLeran | b9afca3 | 2014-04-07 17:10:11 -0600 | [diff] [blame] | 2495 | BUG_ON(nvme_thread && !IS_ERR(nvme_thread)); |
Matthew Wilcox | 21bd78b | 2014-05-09 22:42:26 -0400 | [diff] [blame] | 2496 | _nvme_check_size(); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>"); |
| 2500 | MODULE_LICENSE("GPL"); |
Keith Busch | c78b4713 | 2014-11-21 15:16:32 -0700 | [diff] [blame] | 2501 | MODULE_VERSION("1.0"); |
Matthew Wilcox | b60503b | 2011-01-20 12:50:14 -0500 | [diff] [blame] | 2502 | module_init(nvme_init); |
| 2503 | module_exit(nvme_exit); |