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