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