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