blob: 23cbd93c0c56d422c7a7b0d5aa0cb81fd01834c8 [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003 * Copyright (c) 2011-2014, Intel Corporation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05004 *
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 Wilcoxb60503b2011-01-20 12:50:14 -050013 */
14
Matthew Wilcox8de05532011-05-12 13:50:28 -040015#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050016#include <linux/blkdev.h>
Matias Bjørlinga4aea562014-11-04 08:20:14 -070017#include <linux/blk-mq.h>
Keith Busch42f61422014-03-24 10:46:25 -060018#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040019#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050020#include <linux/errno.h>
21#include <linux/fs.h>
22#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060023#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040024#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050025#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050029#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050030#include <linux/kernel.h>
31#include <linux/mm.h>
32#include <linux/module.h>
33#include <linux/moduleparam.h>
Keith Busch77bf25e2015-11-26 12:21:29 +010034#include <linux/mutex.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050035#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050036#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040037#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050038#include <linux/sched.h>
39#include <linux/slab.h>
Keith Busche1e5e562015-02-19 13:39:03 -070040#include <linux/t10-pi.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050041#include <linux/types.h>
Linus Torvalds9cf5c092015-11-06 14:22:15 -080042#include <linux/io-64-nonatomic-lo-hi.h>
Keith Busch1d277a62015-10-15 14:10:52 +020043#include <asm/unaligned.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090044
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020045#include "nvme.h"
46
Keith Busch9d43cf62014-05-13 11:42:02 -060047#define NVME_Q_DEPTH 1024
Jens Axboed31af0a2015-03-06 12:56:13 -070048#define NVME_AQ_DEPTH 256
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050049#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
50#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050051
Christoph Hellwig21d34712015-11-26 09:08:36 +010052unsigned char admin_timeout = 60;
Keith Busch9d43cf62014-05-13 11:42:02 -060053module_param(admin_timeout, byte, 0644);
54MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050055
Matthew Wilcoxbd676082014-06-03 23:04:30 -040056unsigned char nvme_io_timeout = 30;
57module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060058MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050059
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +010060unsigned char shutdown_timeout = 5;
Dan McLeran2484f402014-07-01 09:33:32 -060061module_param(shutdown_timeout, byte, 0644);
62MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
63
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050064static int use_threaded_interrupts;
65module_param(use_threaded_interrupts, int, 0);
66
Jon Derrick8ffaadf2015-07-20 10:14:09 -060067static bool use_cmb_sqes = true;
68module_param(use_cmb_sqes, bool, 0644);
69MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
70
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050071static LIST_HEAD(dev_list);
72static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070073static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060074static wait_queue_head_t nvme_kthread_wait;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050075
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010076struct nvme_dev;
77struct nvme_queue;
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +010078struct nvme_iod;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010079
Keith Busch4cc06522015-06-05 10:30:08 -060080static int nvme_reset(struct nvme_dev *dev);
Jens Axboea0fa9642015-11-03 20:37:26 -070081static void nvme_process_cq(struct nvme_queue *nvmeq);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +010082static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +020083static void nvme_dead_ctrl(struct nvme_dev *dev);
Keith Busche1569a12015-11-26 12:11:07 +010084static void nvme_dev_shutdown(struct nvme_dev *dev);
Keith Buschd4b4ff82013-12-10 13:10:37 -070085
Keith Busch4d115422013-12-10 13:10:40 -070086struct async_cmd_info {
87 struct kthread_work work;
88 struct kthread_worker *worker;
Matias Bjørlinga4aea562014-11-04 08:20:14 -070089 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -070090 u32 result;
91 int status;
92 void *ctx;
93};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050094
95/*
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010096 * Represents an NVM Express device. Each nvme_dev is a PCI function.
97 */
98struct nvme_dev {
99 struct list_head node;
100 struct nvme_queue **queues;
101 struct blk_mq_tag_set tagset;
102 struct blk_mq_tag_set admin_tagset;
103 u32 __iomem *dbs;
104 struct device *dev;
105 struct dma_pool *prp_page_pool;
106 struct dma_pool *prp_small_pool;
107 unsigned queue_count;
108 unsigned online_queues;
109 unsigned max_qid;
110 int q_depth;
111 u32 db_stride;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100112 struct msix_entry *entry;
113 void __iomem *bar;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100114 struct work_struct reset_work;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100115 struct work_struct scan_work;
Keith Busch77bf25e2015-11-26 12:21:29 +0100116 struct mutex shutdown_lock;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100117 bool subsystem;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100118 void __iomem *cmb;
119 dma_addr_t cmb_dma_addr;
120 u64 cmb_size;
121 u32 cmbsz;
Christoph Hellwigfd634f412015-11-26 12:42:26 +0100122 unsigned long flags;
123#define NVME_CTRL_RESETTING 0
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100124
125 struct nvme_ctrl ctrl;
126};
127
128static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
129{
130 return container_of(ctrl, struct nvme_dev, ctrl);
131}
132
133/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500134 * An NVM Express queue. Each device has at least two (one for admin
135 * commands and one for I/O commands).
136 */
137struct nvme_queue {
138 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500139 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500140 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500141 spinlock_t q_lock;
142 struct nvme_command *sq_cmds;
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600143 struct nvme_command __iomem *sq_cmds_io;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500144 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600145 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500146 dma_addr_t sq_dma_addr;
147 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500148 u32 __iomem *q_db;
149 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700150 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500151 u16 sq_head;
152 u16 sq_tail;
153 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700154 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400155 u8 cq_phase;
156 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700157 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500158};
159
160/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200161 * The nvme_iod describes the data in an I/O, including the list of PRP
162 * entries. You can't see it in this data structure because C doesn't let
163 * me express that. Use nvme_alloc_iod to ensure there's enough space
164 * allocated to store the PRP list.
165 */
166struct nvme_iod {
167 unsigned long private; /* For the use of the submitter of the I/O */
168 int npages; /* In the PRP list. 0 means small pool in use */
169 int offset; /* Of PRP list */
170 int nents; /* Used in scatterlist */
171 int length; /* Of data, in bytes */
172 dma_addr_t first_dma;
173 struct scatterlist meta_sg[1]; /* metadata requires single contiguous buffer */
174 struct scatterlist sg[0];
175};
176
177/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500178 * Check we didin't inadvertently grow the command struct
179 */
180static inline void _nvme_check_size(void)
181{
182 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
183 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
184 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
185 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
186 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400187 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700188 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500189 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
190 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
191 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
192 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600193 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500194}
195
Keith Buschedd10d32014-04-03 16:45:23 -0600196typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400197 struct nvme_completion *);
198
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500199struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400200 nvme_completion_fn fn;
201 void *ctx;
Keith Buschc30341d2013-12-10 13:10:38 -0700202 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700203 struct nvme_queue *nvmeq;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700204 struct nvme_iod iod[0];
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500205};
206
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700207/*
208 * Max size of iod being embedded in the request payload
209 */
210#define NVME_INT_PAGES 2
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100211#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
Chong Yuanfda631f2015-03-27 09:21:32 +0800212#define NVME_INT_MASK 0x01
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700213
214/*
215 * Will slightly overestimate the number of pages needed. This is OK
216 * as it only leads to a small amount of wasted memory for the lifetime of
217 * the I/O.
218 */
219static int nvme_npages(unsigned size, struct nvme_dev *dev)
220{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100221 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
222 dev->ctrl.page_size);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700223 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
224}
225
226static unsigned int nvme_cmd_size(struct nvme_dev *dev)
227{
228 unsigned int ret = sizeof(struct nvme_cmd_info);
229
230 ret += sizeof(struct nvme_iod);
231 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
232 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
233
234 return ret;
235}
236
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700237static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
238 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500239{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700240 struct nvme_dev *dev = data;
241 struct nvme_queue *nvmeq = dev->queues[0];
242
Keith Busch42483222015-06-01 09:29:54 -0600243 WARN_ON(hctx_idx != 0);
244 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
245 WARN_ON(nvmeq->tags);
246
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700247 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600248 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700249 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500250}
251
Keith Busch4af0e212015-06-08 10:08:13 -0600252static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
253{
254 struct nvme_queue *nvmeq = hctx->driver_data;
255
256 nvmeq->tags = NULL;
257}
258
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700259static int nvme_admin_init_request(void *data, struct request *req,
260 unsigned int hctx_idx, unsigned int rq_idx,
261 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600262{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700263 struct nvme_dev *dev = data;
264 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
265 struct nvme_queue *nvmeq = dev->queues[0];
266
267 BUG_ON(!nvmeq);
268 cmd->nvmeq = nvmeq;
269 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600270}
271
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700272static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
273 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500274{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700275 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600276 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500277
Keith Busch42483222015-06-01 09:29:54 -0600278 if (!nvmeq->tags)
279 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500280
Keith Busch42483222015-06-01 09:29:54 -0600281 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700282 hctx->driver_data = nvmeq;
283 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500284}
285
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700286static int nvme_init_request(void *data, struct request *req,
287 unsigned int hctx_idx, unsigned int rq_idx,
288 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500289{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700290 struct nvme_dev *dev = data;
291 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
292 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
293
294 BUG_ON(!nvmeq);
295 cmd->nvmeq = nvmeq;
296 return 0;
297}
298
299static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
300 nvme_completion_fn handler)
301{
302 cmd->fn = handler;
303 cmd->ctx = ctx;
304 cmd->aborted = 0;
Keith Buschc917dfe2015-01-07 18:55:48 -0700305 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500306}
307
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700308static void *iod_get_private(struct nvme_iod *iod)
309{
310 return (void *) (iod->private & ~0x1UL);
311}
312
313/*
314 * If bit 0 is set, the iod is embedded in the request payload.
315 */
316static bool iod_should_kfree(struct nvme_iod *iod)
317{
Chong Yuanfda631f2015-03-27 09:21:32 +0800318 return (iod->private & NVME_INT_MASK) == 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700319}
320
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400321/* Special values must be less than 0x1000 */
322#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500323#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
324#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
325#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500326
Keith Buschedd10d32014-04-03 16:45:23 -0600327static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400328 struct nvme_completion *cqe)
329{
330 if (ctx == CMD_CTX_CANCELLED)
331 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400332 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600333 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400334 "completed id %d twice on queue %d\n",
335 cqe->command_id, le16_to_cpup(&cqe->sq_id));
336 return;
337 }
338 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600339 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400340 "invalid id %d completed on queue %d\n",
341 cqe->command_id, le16_to_cpup(&cqe->sq_id));
342 return;
343 }
Keith Buschedd10d32014-04-03 16:45:23 -0600344 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400345}
346
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700347static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
348{
349 void *ctx;
350
351 if (fn)
352 *fn = cmd->fn;
353 ctx = cmd->ctx;
354 cmd->fn = special_completion;
355 cmd->ctx = CMD_CTX_CANCELLED;
356 return ctx;
357}
358
359static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
360 struct nvme_completion *cqe)
361{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700362 u32 result = le32_to_cpup(&cqe->result);
363 u16 status = le16_to_cpup(&cqe->status) >> 1;
364
365 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100366 ++nvmeq->dev->ctrl.event_limit;
Keith Buscha5768aa2015-06-01 14:28:14 -0600367 if (status != NVME_SC_SUCCESS)
368 return;
369
370 switch (result & 0xff07) {
371 case NVME_AER_NOTICE_NS_CHANGED:
372 dev_info(nvmeq->q_dmadev, "rescanning\n");
373 schedule_work(&nvmeq->dev->scan_work);
374 default:
375 dev_warn(nvmeq->q_dmadev, "async event result %08x\n", result);
376 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700377}
378
379static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
380 struct nvme_completion *cqe)
381{
382 struct request *req = ctx;
383
384 u16 status = le16_to_cpup(&cqe->status) >> 1;
385 u32 result = le32_to_cpup(&cqe->result);
386
Keith Busch42483222015-06-01 09:29:54 -0600387 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700388
389 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100390 ++nvmeq->dev->ctrl.abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700391}
392
Keith Buschedd10d32014-04-03 16:45:23 -0600393static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700394 struct nvme_completion *cqe)
395{
396 struct async_cmd_info *cmdinfo = ctx;
397 cmdinfo->result = le32_to_cpup(&cqe->result);
398 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
399 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
Keith Busch42483222015-06-01 09:29:54 -0600400 blk_mq_free_request(cmdinfo->req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700401}
402
403static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
404 unsigned int tag)
405{
Keith Busch42483222015-06-01 09:29:54 -0600406 struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700407
408 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700409}
410
Matthew Wilcox184d2942011-05-11 21:36:38 -0400411/*
412 * Called with local interrupts disabled and the q_lock held. May not sleep.
413 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700414static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400415 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500416{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700417 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400418 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700419 if (tag >= nvmeq->q_depth) {
420 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500421 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400422 }
Keith Busch859361a2012-08-02 14:05:59 -0600423 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700424 *fn = cmd->fn;
425 ctx = cmd->ctx;
426 cmd->fn = special_completion;
427 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400428 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500429}
430
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500431/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400432 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500433 * @nvmeq: The queue to use
434 * @cmd: The command to send
435 *
436 * Safe to use from interrupt context
437 */
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530438static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
439 struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500440{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700441 u16 tail = nvmeq->sq_tail;
442
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600443 if (nvmeq->sq_cmds_io)
444 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
445 else
446 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
447
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500448 if (++tail == nvmeq->q_depth)
449 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500450 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500451 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500452}
453
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530454static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700455{
456 unsigned long flags;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700457 spin_lock_irqsave(&nvmeq->q_lock, flags);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530458 __nvme_submit_cmd(nvmeq, cmd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700459 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700460}
461
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500462static __le64 **iod_list(struct nvme_iod *iod)
463{
464 return ((void *)iod) + iod->offset;
465}
466
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700467static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
468 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500469{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700470 iod->private = private;
471 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
472 iod->npages = -1;
473 iod->length = nbytes;
474 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500475}
476
477static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700478__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
479 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500480{
481 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700482 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500483 sizeof(struct scatterlist) * nseg, gfp);
484
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700485 if (iod)
486 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500487
488 return iod;
489}
490
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700491static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
492 gfp_t gfp)
493{
494 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
495 sizeof(struct nvme_dsm_range);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700496 struct nvme_iod *iod;
497
498 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
499 size <= NVME_INT_BYTES(dev)) {
500 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
501
502 iod = cmd->iod;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700503 iod_init(iod, size, rq->nr_phys_segments,
Chong Yuanfda631f2015-03-27 09:21:32 +0800504 (unsigned long) rq | NVME_INT_MASK);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700505 return iod;
506 }
507
508 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
509 (unsigned long) rq, gfp);
510}
511
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200512static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500513{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100514 const int last_prp = dev->ctrl.page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500515 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500516 __le64 **list = iod_list(iod);
517 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500518
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500519 if (iod->npages == 0)
520 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
521 for (i = 0; i < iod->npages; i++) {
522 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500523 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500524 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500525 prp_dma = next_prp_dma;
526 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700527
528 if (iod_should_kfree(iod))
529 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500530}
531
Keith Busch52b68d72015-02-23 09:16:21 -0700532#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700533static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
534{
535 if (be32_to_cpu(pi->ref_tag) == v)
536 pi->ref_tag = cpu_to_be32(p);
537}
538
539static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
540{
541 if (be32_to_cpu(pi->ref_tag) == p)
542 pi->ref_tag = cpu_to_be32(v);
543}
544
545/**
546 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
547 *
548 * The virtual start sector is the one that was originally submitted by the
549 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
550 * start sector may be different. Remap protection information to match the
551 * physical LBA on writes, and back to the original seed on reads.
552 *
553 * Type 0 and 3 do not have a ref tag, so no remapping required.
554 */
555static void nvme_dif_remap(struct request *req,
556 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
557{
558 struct nvme_ns *ns = req->rq_disk->private_data;
559 struct bio_integrity_payload *bip;
560 struct t10_pi_tuple *pi;
561 void *p, *pmap;
562 u32 i, nlb, ts, phys, virt;
563
564 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
565 return;
566
567 bip = bio_integrity(req->bio);
568 if (!bip)
569 return;
570
571 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700572
573 p = pmap;
574 virt = bip_get_seed(bip);
575 phys = nvme_block_nr(ns, blk_rq_pos(req));
576 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
Dan Williamsac6fc482015-10-21 13:20:18 -0400577 ts = ns->disk->queue->integrity.tuple_size;
Keith Busche1e5e562015-02-19 13:39:03 -0700578
579 for (i = 0; i < nlb; i++, virt++, phys++) {
580 pi = (struct t10_pi_tuple *)p;
581 dif_swap(phys, virt, pi);
582 p += ts;
583 }
584 kunmap_atomic(pmap);
585}
Keith Busch52b68d72015-02-23 09:16:21 -0700586#else /* CONFIG_BLK_DEV_INTEGRITY */
587static void nvme_dif_remap(struct request *req,
588 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
589{
590}
591static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
592{
593}
594static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
595{
596}
Keith Busch52b68d72015-02-23 09:16:21 -0700597#endif
598
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700599static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500600 struct nvme_completion *cqe)
601{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500602 struct nvme_iod *iod = ctx;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700603 struct request *req = iod_get_private(iod);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700604 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500605 u16 status = le16_to_cpup(&cqe->status) >> 1;
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200606 int error = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500607
Keith Buschedd10d32014-04-03 16:45:23 -0600608 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700609 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
610 && (jiffies - req->start_time) < req->timeout) {
Keith Buschc9d3bf82015-01-07 18:55:52 -0700611 unsigned long flags;
612
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100613 nvme_unmap_data(nvmeq->dev, iod);
614
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700615 blk_mq_requeue_request(req);
Keith Buschc9d3bf82015-01-07 18:55:52 -0700616 spin_lock_irqsave(req->q->queue_lock, flags);
617 if (!blk_queue_stopped(req->q))
618 blk_mq_kick_requeue_list(req->q);
619 spin_unlock_irqrestore(req->q->queue_lock, flags);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100620 return;
Keith Buschedd10d32014-04-03 16:45:23 -0600621 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200622
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200623 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
Keith Busch17188bb42015-06-08 10:08:14 -0600624 if (cmd_rq->ctx == CMD_CTX_CANCELLED)
Christoph Hellwig297465c2015-11-26 12:58:11 +0100625 error = NVME_SC_CANCELLED;
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200626 else
627 error = status;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200628 } else {
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200629 error = nvme_error_status(status);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200630 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200631 }
632
Keith Buscha0a931d2015-05-22 12:28:31 -0600633 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
634 u32 result = le32_to_cpup(&cqe->result);
635 req->special = (void *)(uintptr_t)result;
636 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700637
638 if (cmd_rq->aborted)
Christoph Hellwige75ec752015-05-22 11:12:39 +0200639 dev_warn(nvmeq->dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700640 "completing aborted command with status:%04x\n",
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200641 error);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700642
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100643 nvme_unmap_data(nvmeq->dev, iod);
644 blk_mq_complete_request(req, error);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500645}
646
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200647static bool nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
648 int total_len)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500649{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500650 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500651 int length = total_len;
652 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500653 int dma_len = sg_dma_len(sg);
654 u64 dma_addr = sg_dma_address(sg);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100655 u32 page_size = dev->ctrl.page_size;
Murali Iyerf137e0f2015-03-26 11:07:51 -0500656 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500657 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500658 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500659 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500660 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500661
Keith Busch1d090622014-06-23 11:34:01 -0600662 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500663 if (length <= 0)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200664 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500665
Keith Busch1d090622014-06-23 11:34:01 -0600666 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500667 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600668 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500669 } else {
670 sg = sg_next(sg);
671 dma_addr = sg_dma_address(sg);
672 dma_len = sg_dma_len(sg);
673 }
674
Keith Busch1d090622014-06-23 11:34:01 -0600675 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600676 iod->first_dma = dma_addr;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200677 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500678 }
679
Keith Busch1d090622014-06-23 11:34:01 -0600680 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500681 if (nprps <= (256 / 8)) {
682 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500683 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500684 } else {
685 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500686 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500687 }
688
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200689 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400690 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600691 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500692 iod->npages = -1;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200693 return false;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400694 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500695 list[0] = prp_list;
696 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500697 i = 0;
698 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600699 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500700 __le64 *old_prp_list = prp_list;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200701 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500702 if (!prp_list)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200703 return false;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500704 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400705 prp_list[0] = old_prp_list[i - 1];
706 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
707 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500708 }
709 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600710 dma_len -= page_size;
711 dma_addr += page_size;
712 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500713 if (length <= 0)
714 break;
715 if (dma_len > 0)
716 continue;
717 BUG_ON(dma_len < 0);
718 sg = sg_next(sg);
719 dma_addr = sg_dma_address(sg);
720 dma_len = sg_dma_len(sg);
721 }
722
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200723 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500724}
725
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200726static int nvme_map_data(struct nvme_dev *dev, struct nvme_iod *iod,
727 struct nvme_command *cmnd)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200728{
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200729 struct request *req = iod_get_private(iod);
730 struct request_queue *q = req->q;
731 enum dma_data_direction dma_dir = rq_data_dir(req) ?
732 DMA_TO_DEVICE : DMA_FROM_DEVICE;
733 int ret = BLK_MQ_RQ_QUEUE_ERROR;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200734
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200735 sg_init_table(iod->sg, req->nr_phys_segments);
736 iod->nents = blk_rq_map_sg(q, req, iod->sg);
737 if (!iod->nents)
738 goto out;
739
740 ret = BLK_MQ_RQ_QUEUE_BUSY;
741 if (!dma_map_sg(dev->dev, iod->sg, iod->nents, dma_dir))
742 goto out;
743
744 if (!nvme_setup_prps(dev, iod, blk_rq_bytes(req)))
745 goto out_unmap;
746
747 ret = BLK_MQ_RQ_QUEUE_ERROR;
748 if (blk_integrity_rq(req)) {
749 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
750 goto out_unmap;
751
752 sg_init_table(iod->meta_sg, 1);
753 if (blk_rq_map_integrity_sg(q, req->bio, iod->meta_sg) != 1)
754 goto out_unmap;
755
756 if (rq_data_dir(req))
757 nvme_dif_remap(req, nvme_dif_prep);
758
759 if (!dma_map_sg(dev->dev, iod->meta_sg, 1, dma_dir))
760 goto out_unmap;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200761 }
762
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200763 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
764 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
765 if (blk_integrity_rq(req))
766 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg));
767 return BLK_MQ_RQ_QUEUE_OK;
768
769out_unmap:
770 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
771out:
772 return ret;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200773}
774
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100775static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod)
776{
777 struct request *req = iod_get_private(iod);
778 enum dma_data_direction dma_dir = rq_data_dir(req) ?
779 DMA_TO_DEVICE : DMA_FROM_DEVICE;
780
781 if (iod->nents) {
782 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
783 if (blk_integrity_rq(req)) {
784 if (!rq_data_dir(req))
785 nvme_dif_remap(req, nvme_dif_complete);
786 dma_unmap_sg(dev->dev, iod->meta_sg, 1, dma_dir);
787 }
788 }
789
790 nvme_free_iod(dev, iod);
791}
792
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700793/*
794 * We reuse the small pool to allocate the 16-byte range here as it is not
795 * worth having a special pool for these or additional cases to handle freeing
796 * the iod.
797 */
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200798static int nvme_setup_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
799 struct nvme_iod *iod, struct nvme_command *cmnd)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700800{
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200801 struct request *req = iod_get_private(iod);
802 struct nvme_dsm_range *range;
803
804 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
805 &iod->first_dma);
806 if (!range)
807 return BLK_MQ_RQ_QUEUE_BUSY;
808 iod_list(iod)[0] = (__le64 *)range;
809 iod->npages = 0;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700810
Keith Busch0e5e4f02012-11-09 16:33:05 -0700811 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700812 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
813 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700814
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200815 memset(cmnd, 0, sizeof(*cmnd));
816 cmnd->dsm.opcode = nvme_cmd_dsm;
817 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
818 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
819 cmnd->dsm.nr = 0;
820 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
821 return BLK_MQ_RQ_QUEUE_OK;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700822}
823
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200824/*
825 * NOTE: ns is NULL when called on the admin queue.
826 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700827static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
828 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600829{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700830 struct nvme_ns *ns = hctx->queue->queuedata;
831 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200832 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700833 struct request *req = bd->rq;
834 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600835 struct nvme_iod *iod;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200836 struct nvme_command cmnd;
837 int ret = BLK_MQ_RQ_QUEUE_OK;
Keith Buschedd10d32014-04-03 16:45:23 -0600838
Keith Busche1e5e562015-02-19 13:39:03 -0700839 /*
840 * If formated with metadata, require the block layer provide a buffer
841 * unless this namespace is formated such that the metadata can be
842 * stripped/generated by the controller with PRACT=1.
843 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200844 if (ns && ns->ms && !blk_integrity_rq(req)) {
Keith Busch71feb362015-06-19 11:07:30 -0600845 if (!(ns->pi_type && ns->ms == 8) &&
846 req->cmd_type != REQ_TYPE_DRV_PRIV) {
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200847 blk_mq_complete_request(req, -EFAULT);
Keith Busche1e5e562015-02-19 13:39:03 -0700848 return BLK_MQ_RQ_QUEUE_OK;
849 }
850 }
851
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200852 iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600853 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700854 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600855
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700856 if (req->cmd_flags & REQ_DISCARD) {
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200857 ret = nvme_setup_discard(nvmeq, ns, iod, &cmnd);
858 } else {
859 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
860 memcpy(&cmnd, req->cmd, sizeof(cmnd));
861 else if (req->cmd_flags & REQ_FLUSH)
862 nvme_setup_flush(ns, &cmnd);
863 else
864 nvme_setup_rw(ns, req, &cmnd);
Keith Buschedd10d32014-04-03 16:45:23 -0600865
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200866 if (req->nr_phys_segments)
867 ret = nvme_map_data(dev, iod, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700868 }
869
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200870 if (ret)
871 goto out;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700872
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200873 cmnd.common.command_id = req->tag;
874 nvme_set_info(cmd, iod, req_completion);
875
876 spin_lock_irq(&nvmeq->q_lock);
877 __nvme_submit_cmd(nvmeq, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700878 nvme_process_cq(nvmeq);
879 spin_unlock_irq(&nvmeq->q_lock);
880 return BLK_MQ_RQ_QUEUE_OK;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200881out:
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200882 nvme_free_iod(dev, iod);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200883 return ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500884}
885
Jens Axboea0fa9642015-11-03 20:37:26 -0700886static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500887{
Matthew Wilcox82123462011-01-20 13:24:06 -0500888 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500889
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500890 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500891 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500892
893 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400894 void *ctx;
895 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500896 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500897 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500898 break;
899 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
900 if (++head == nvmeq->q_depth) {
901 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500902 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500903 }
Jens Axboea0fa9642015-11-03 20:37:26 -0700904 if (tag && *tag == cqe.command_id)
905 *tag = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700906 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600907 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500908 }
909
910 /* If the controller ignores the cq head doorbell and continuously
911 * writes to the queue, it is theoretically possible to wrap around
912 * the queue twice and mistakenly return IRQ_NONE. Linux only
913 * requires that 0.1% of your interrupts are handled, so this isn't
914 * a big problem.
915 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500916 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Jens Axboea0fa9642015-11-03 20:37:26 -0700917 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500918
Keith Busch604e8c82015-11-20 08:38:13 -0700919 if (likely(nvmeq->cq_vector >= 0))
920 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500921 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500922 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500923
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400924 nvmeq->cqe_seen = 1;
Jens Axboea0fa9642015-11-03 20:37:26 -0700925}
926
927static void nvme_process_cq(struct nvme_queue *nvmeq)
928{
929 __nvme_process_cq(nvmeq, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500930}
931
932static irqreturn_t nvme_irq(int irq, void *data)
933{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500934 irqreturn_t result;
935 struct nvme_queue *nvmeq = data;
936 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400937 nvme_process_cq(nvmeq);
938 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
939 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500940 spin_unlock(&nvmeq->q_lock);
941 return result;
942}
943
944static irqreturn_t nvme_irq_check(int irq, void *data)
945{
946 struct nvme_queue *nvmeq = data;
947 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
948 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
949 return IRQ_NONE;
950 return IRQ_WAKE_THREAD;
951}
952
Jens Axboea0fa9642015-11-03 20:37:26 -0700953static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
954{
955 struct nvme_queue *nvmeq = hctx->driver_data;
956
957 if ((le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
958 nvmeq->cq_phase) {
959 spin_lock_irq(&nvmeq->q_lock);
960 __nvme_process_cq(nvmeq, &tag);
961 spin_unlock_irq(&nvmeq->q_lock);
962
963 if (tag == -1)
964 return 1;
965 }
966
967 return 0;
968}
969
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700970static int nvme_submit_async_admin_req(struct nvme_dev *dev)
971{
972 struct nvme_queue *nvmeq = dev->queues[0];
973 struct nvme_command c;
974 struct nvme_cmd_info *cmd_info;
975 struct request *req;
976
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100977 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100978 BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED);
Dan Carpenter9f173b32014-11-05 23:39:09 +0300979 if (IS_ERR(req))
980 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700981
Keith Buschc917dfe2015-01-07 18:55:48 -0700982 req->cmd_flags |= REQ_NO_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700983 cmd_info = blk_mq_rq_to_pdu(req);
Keith Busch1efccc92015-03-31 10:37:17 -0600984 nvme_set_info(cmd_info, NULL, async_req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700985
986 memset(&c, 0, sizeof(c));
987 c.common.opcode = nvme_admin_async_event;
988 c.common.command_id = req->tag;
989
Keith Busch42483222015-06-01 09:29:54 -0600990 blk_mq_free_request(req);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530991 __nvme_submit_cmd(nvmeq, &c);
992 return 0;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700993}
994
995static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -0700996 struct nvme_command *cmd,
997 struct async_cmd_info *cmdinfo, unsigned timeout)
998{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700999 struct nvme_queue *nvmeq = dev->queues[0];
1000 struct request *req;
1001 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -07001002
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001003 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE, 0);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001004 if (IS_ERR(req))
1005 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001006
1007 req->timeout = timeout;
1008 cmd_rq = blk_mq_rq_to_pdu(req);
1009 cmdinfo->req = req;
1010 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -07001011 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001012
1013 cmd->common.command_id = req->tag;
1014
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301015 nvme_submit_cmd(nvmeq, cmd);
1016 return 0;
Keith Busch4d115422013-12-10 13:10:40 -07001017}
1018
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001019static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1020{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001021 struct nvme_command c;
1022
1023 memset(&c, 0, sizeof(c));
1024 c.delete_queue.opcode = opcode;
1025 c.delete_queue.qid = cpu_to_le16(id);
1026
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001027 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001028}
1029
1030static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1031 struct nvme_queue *nvmeq)
1032{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001033 struct nvme_command c;
1034 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1035
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001036 /*
1037 * Note: we (ab)use the fact the the prp fields survive if no data
1038 * is attached to the request.
1039 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001040 memset(&c, 0, sizeof(c));
1041 c.create_cq.opcode = nvme_admin_create_cq;
1042 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1043 c.create_cq.cqid = cpu_to_le16(qid);
1044 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1045 c.create_cq.cq_flags = cpu_to_le16(flags);
1046 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1047
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001048 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001049}
1050
1051static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1052 struct nvme_queue *nvmeq)
1053{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001054 struct nvme_command c;
1055 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1056
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001057 /*
1058 * Note: we (ab)use the fact the the prp fields survive if no data
1059 * is attached to the request.
1060 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001061 memset(&c, 0, sizeof(c));
1062 c.create_sq.opcode = nvme_admin_create_sq;
1063 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1064 c.create_sq.sqid = cpu_to_le16(qid);
1065 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1066 c.create_sq.sq_flags = cpu_to_le16(flags);
1067 c.create_sq.cqid = cpu_to_le16(qid);
1068
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001069 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001070}
1071
1072static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1073{
1074 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1075}
1076
1077static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1078{
1079 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1080}
1081
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001082static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
Keith Buschc30341d2013-12-10 13:10:38 -07001083{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001084 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1085 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001086 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001087 struct request *abort_req;
1088 struct nvme_cmd_info *abort_cmd;
1089 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001090
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001091 /*
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001092 * Shutdown immediately if controller times out while starting. The
1093 * reset work will see the pci device disabled when it gets the forced
1094 * cancellation error. All outstanding requests are completed on
1095 * shutdown, so we return BLK_EH_HANDLED.
1096 */
1097 if (test_bit(NVME_CTRL_RESETTING, &dev->flags)) {
1098 dev_warn(dev->dev,
1099 "I/O %d QID %d timeout, disable controller\n",
1100 req->tag, nvmeq->qid);
1101 nvme_dev_shutdown(dev);
1102 req->errors = NVME_SC_CANCELLED;
1103 return BLK_EH_HANDLED;
1104 }
1105
1106 /*
1107 * Shutdown the controller immediately and schedule a reset if the
1108 * command was already aborted once before and still hasn't been
1109 * returned to the driver, or if this is the admin queue.
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001110 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001111 if (!nvmeq->qid || cmd_rq->aborted) {
Keith Busche1569a12015-11-26 12:11:07 +01001112 dev_warn(dev->dev,
1113 "I/O %d QID %d timeout, reset controller\n",
1114 req->tag, nvmeq->qid);
1115 nvme_dev_shutdown(dev);
1116 queue_work(nvme_workq, &dev->reset_work);
1117
1118 /*
1119 * Mark the request as handled, since the inline shutdown
1120 * forces all outstanding requests to complete.
1121 */
1122 req->errors = NVME_SC_CANCELLED;
1123 return BLK_EH_HANDLED;
Keith Buschc30341d2013-12-10 13:10:38 -07001124 }
1125
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001126 if (!dev->ctrl.abort_limit)
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001127 return BLK_EH_RESET_TIMER;
Keith Buschc30341d2013-12-10 13:10:38 -07001128
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001129 abort_req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001130 BLK_MQ_REQ_NOWAIT);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001131 if (IS_ERR(abort_req))
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001132 return BLK_EH_RESET_TIMER;
Keith Buschc30341d2013-12-10 13:10:38 -07001133
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001134 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1135 nvme_set_info(abort_cmd, abort_req, abort_completion);
1136
Keith Buschc30341d2013-12-10 13:10:38 -07001137 memset(&cmd, 0, sizeof(cmd));
1138 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001139 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001140 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001141 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001142
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001143 --dev->ctrl.abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001144 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001145
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001146 dev_warn(nvmeq->q_dmadev, "I/O %d QID %d timeout, aborting\n",
1147 req->tag, nvmeq->qid);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301148 nvme_submit_cmd(dev->queues[0], &cmd);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001149
1150 /*
1151 * The aborted req will be completed on receiving the abort req.
1152 * We enable the timer again. If hit twice, it'll cause a device reset,
1153 * as the device then is in a faulty state.
1154 */
1155 return BLK_EH_RESET_TIMER;
Keith Buschc30341d2013-12-10 13:10:38 -07001156}
1157
Keith Busch42483222015-06-01 09:29:54 -06001158static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001159{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001160 struct nvme_queue *nvmeq = data;
1161 void *ctx;
1162 nvme_completion_fn fn;
1163 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001164 struct nvme_completion cqe;
1165
1166 if (!blk_mq_request_started(req))
1167 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001168
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001169 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001170
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001171 if (cmd->ctx == CMD_CTX_CANCELLED)
1172 return;
1173
Keith Buschcef6a942015-01-07 18:55:51 -07001174 if (blk_queue_dying(req->q))
1175 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1176 else
1177 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1178
1179
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001180 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1181 req->tag, nvmeq->qid);
1182 ctx = cancel_cmd_info(cmd, &fn);
1183 fn(nvmeq, ctx, &cqe);
1184}
1185
Keith Buschf435c282014-07-07 09:14:42 -06001186static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001187{
1188 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1189 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001190 if (nvmeq->sq_cmds)
1191 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001192 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1193 kfree(nvmeq);
1194}
1195
Keith Buscha1a5ef92013-12-16 13:50:00 -05001196static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001197{
1198 int i;
1199
Keith Buscha1a5ef92013-12-16 13:50:00 -05001200 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001201 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001202 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001203 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001204 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001205 }
Keith Busch22404272013-07-15 15:02:20 -06001206}
1207
Keith Busch4d115422013-12-10 13:10:40 -07001208/**
1209 * nvme_suspend_queue - put queue into suspended state
1210 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001211 */
1212static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001213{
Keith Busch2b25d982014-12-22 12:59:04 -07001214 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001215
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001216 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001217 if (nvmeq->cq_vector == -1) {
1218 spin_unlock_irq(&nvmeq->q_lock);
1219 return 1;
1220 }
1221 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001222 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001223 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001224 spin_unlock_irq(&nvmeq->q_lock);
1225
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001226 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
1227 blk_mq_freeze_queue_start(nvmeq->dev->ctrl.admin_q);
Keith Busch6df3dbc2015-03-26 13:49:33 -06001228
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001229 irq_set_affinity_hint(vector, NULL);
1230 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001231
Keith Busch4d115422013-12-10 13:10:40 -07001232 return 0;
1233}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001234
Keith Busch4d115422013-12-10 13:10:40 -07001235static void nvme_clear_queue(struct nvme_queue *nvmeq)
1236{
Keith Busch22404272013-07-15 15:02:20 -06001237 spin_lock_irq(&nvmeq->q_lock);
Keith Busch42483222015-06-01 09:29:54 -06001238 if (nvmeq->tags && *nvmeq->tags)
1239 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001240 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001241}
1242
Keith Busch4d115422013-12-10 13:10:40 -07001243static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1244{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001245 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001246
1247 if (!nvmeq)
1248 return;
1249 if (nvme_suspend_queue(nvmeq))
1250 return;
1251
Keith Busch0e53d182013-12-10 13:10:39 -07001252 /* Don't tell the adapter to delete the admin queue.
1253 * Don't tell a removed adapter to delete IO queues. */
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001254 if (qid && readl(dev->bar + NVME_REG_CSTS) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001255 adapter_delete_sq(dev, qid);
1256 adapter_delete_cq(dev, qid);
1257 }
Keith Busch07836e62015-02-19 10:34:48 -07001258
1259 spin_lock_irq(&nvmeq->q_lock);
1260 nvme_process_cq(nvmeq);
1261 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001262}
1263
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001264static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1265 int entry_size)
1266{
1267 int q_depth = dev->q_depth;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001268 unsigned q_size_aligned = roundup(q_depth * entry_size,
1269 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001270
1271 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001272 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001273 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
Jon Derrickc45f5c92015-07-21 15:08:13 -06001274 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001275
1276 /*
1277 * Ensure the reduced q_depth is above some threshold where it
1278 * would be better to map queues in system memory with the
1279 * original depth
1280 */
1281 if (q_depth < 64)
1282 return -ENOMEM;
1283 }
1284
1285 return q_depth;
1286}
1287
1288static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1289 int qid, int depth)
1290{
1291 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001292 unsigned offset = (qid - 1) * roundup(SQ_SIZE(depth),
1293 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001294 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1295 nvmeq->sq_cmds_io = dev->cmb + offset;
1296 } else {
1297 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1298 &nvmeq->sq_dma_addr, GFP_KERNEL);
1299 if (!nvmeq->sq_cmds)
1300 return -ENOMEM;
1301 }
1302
1303 return 0;
1304}
1305
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001306static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001307 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001308{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001309 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001310 if (!nvmeq)
1311 return NULL;
1312
Christoph Hellwige75ec752015-05-22 11:12:39 +02001313 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001314 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001315 if (!nvmeq->cqes)
1316 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001317
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001318 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001319 goto free_cqdma;
1320
Christoph Hellwige75ec752015-05-22 11:12:39 +02001321 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001322 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001323 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001324 dev->ctrl.instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001325 spin_lock_init(&nvmeq->q_lock);
1326 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001327 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001328 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001329 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001330 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001331 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001332 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001333
Jon Derrick36a7e992015-05-27 12:26:23 -06001334 /* make sure queue descriptor is set before queue count, for kthread */
1335 mb();
1336 dev->queue_count++;
1337
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001338 return nvmeq;
1339
1340 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001341 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001342 nvmeq->cq_dma_addr);
1343 free_nvmeq:
1344 kfree(nvmeq);
1345 return NULL;
1346}
1347
Matthew Wilcox30010822011-01-20 09:10:15 -05001348static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1349 const char *name)
1350{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001351 if (use_threaded_interrupts)
1352 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001353 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001354 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001355 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001356 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001357}
1358
Keith Busch22404272013-07-15 15:02:20 -06001359static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001360{
Keith Busch22404272013-07-15 15:02:20 -06001361 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001362
Keith Busch7be50e92014-09-10 15:48:47 -06001363 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001364 nvmeq->sq_tail = 0;
1365 nvmeq->cq_head = 0;
1366 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001367 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001368 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001369 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001370 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001371}
1372
1373static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1374{
1375 struct nvme_dev *dev = nvmeq->dev;
1376 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001377
Keith Busch2b25d982014-12-22 12:59:04 -07001378 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001379 result = adapter_alloc_cq(dev, qid, nvmeq);
1380 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001381 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001382
1383 result = adapter_alloc_sq(dev, qid, nvmeq);
1384 if (result < 0)
1385 goto release_cq;
1386
Matthew Wilcox3193f072014-01-27 15:57:22 -05001387 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001388 if (result < 0)
1389 goto release_sq;
1390
Keith Busch22404272013-07-15 15:02:20 -06001391 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001392 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001393
1394 release_sq:
1395 adapter_delete_sq(dev, qid);
1396 release_cq:
1397 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001398 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001399}
1400
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001401static struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001402 .queue_rq = nvme_queue_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001403 .map_queue = blk_mq_map_queue,
1404 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001405 .exit_hctx = nvme_admin_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001406 .init_request = nvme_admin_init_request,
1407 .timeout = nvme_timeout,
1408};
1409
1410static struct blk_mq_ops nvme_mq_ops = {
1411 .queue_rq = nvme_queue_rq,
1412 .map_queue = blk_mq_map_queue,
1413 .init_hctx = nvme_init_hctx,
1414 .init_request = nvme_init_request,
1415 .timeout = nvme_timeout,
Jens Axboea0fa9642015-11-03 20:37:26 -07001416 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001417};
1418
Keith Buschea191d22015-01-07 18:55:49 -07001419static void nvme_dev_remove_admin(struct nvme_dev *dev)
1420{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001421 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
1422 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001423 blk_mq_free_tag_set(&dev->admin_tagset);
1424 }
1425}
1426
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001427static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1428{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001429 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001430 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1431 dev->admin_tagset.nr_hw_queues = 1;
1432 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
Keith Busch1efccc92015-03-31 10:37:17 -06001433 dev->admin_tagset.reserved_tags = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001434 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001435 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001436 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001437 dev->admin_tagset.driver_data = dev;
1438
1439 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1440 return -ENOMEM;
1441
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001442 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1443 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001444 blk_mq_free_tag_set(&dev->admin_tagset);
1445 return -ENOMEM;
1446 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001447 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001448 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001449 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001450 return -ENODEV;
1451 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001452 } else
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001453 blk_mq_unfreeze_queue(dev->ctrl.admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001454
1455 return 0;
1456}
1457
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001458static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001459{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001460 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001461 u32 aqa;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001462 u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001463 struct nvme_queue *nvmeq;
1464
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001465 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1) ?
Keith Buschdfbac8c2015-08-10 15:20:40 -06001466 NVME_CAP_NSSRC(cap) : 0;
1467
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001468 if (dev->subsystem &&
1469 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1470 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001471
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001472 result = nvme_disable_ctrl(&dev->ctrl, cap);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001473 if (result < 0)
1474 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001475
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001476 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001477 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001478 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001479 if (!nvmeq)
1480 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001481 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001482
1483 aqa = nvmeq->q_depth - 1;
1484 aqa |= aqa << 16;
1485
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001486 writel(aqa, dev->bar + NVME_REG_AQA);
1487 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1488 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001489
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001490 result = nvme_enable_ctrl(&dev->ctrl, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001491 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001492 goto free_nvmeq;
1493
Keith Busch2b25d982014-12-22 12:59:04 -07001494 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001495 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001496 if (result) {
1497 nvmeq->cq_vector = -1;
Keith Busch0fb59cb2015-01-07 18:55:50 -07001498 goto free_nvmeq;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001499 }
Keith Busch025c5572013-05-01 13:07:51 -06001500
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001501 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001502
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001503 free_nvmeq:
1504 nvme_free_queues(dev, 0);
1505 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001506}
1507
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001508static int nvme_kthread(void *data)
1509{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001510 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001511
1512 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001513 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001514 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001515 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001516 int i;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001517 u32 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001518
Christoph Hellwig846cc052015-11-26 12:10:29 +01001519 /*
1520 * Skip controllers currently under reset.
1521 */
1522 if (work_pending(&dev->reset_work) || work_busy(&dev->reset_work))
1523 continue;
1524
Keith Buschdfbac8c2015-08-10 15:20:40 -06001525 if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
1526 csts & NVME_CSTS_CFS) {
Christoph Hellwig846cc052015-11-26 12:10:29 +01001527 if (queue_work(nvme_workq, &dev->reset_work)) {
Christoph Hellwig906678922015-10-02 18:49:23 +02001528 dev_warn(dev->dev,
1529 "Failed status: %x, reset controller\n",
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001530 readl(dev->bar + NVME_REG_CSTS));
Christoph Hellwig906678922015-10-02 18:49:23 +02001531 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07001532 continue;
1533 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001534 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001535 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001536 if (!nvmeq)
1537 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001538 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001539 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06001540
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001541 while (i == 0 && dev->ctrl.event_limit > 0) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001542 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06001543 break;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001544 dev->ctrl.event_limit--;
Keith Busch6fccf932014-06-18 13:58:57 -06001545 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001546 spin_unlock_irq(&nvmeq->q_lock);
1547 }
1548 }
1549 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001550 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001551 }
1552 return 0;
1553}
1554
Christoph Hellwig749941f2015-11-26 11:46:39 +01001555static int nvme_create_io_queues(struct nvme_dev *dev)
Keith Busch42f61422014-03-24 10:46:25 -06001556{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001557 unsigned i;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001558 int ret = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001559
Christoph Hellwig749941f2015-11-26 11:46:39 +01001560 for (i = dev->queue_count; i <= dev->max_qid; i++) {
1561 if (!nvme_alloc_queue(dev, i, dev->q_depth)) {
1562 ret = -ENOMEM;
Keith Busch42f61422014-03-24 10:46:25 -06001563 break;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001564 }
1565 }
Keith Busch42f61422014-03-24 10:46:25 -06001566
Christoph Hellwig749941f2015-11-26 11:46:39 +01001567 for (i = dev->online_queues; i <= dev->queue_count - 1; i++) {
1568 ret = nvme_create_queue(dev->queues[i], i);
1569 if (ret) {
Christoph Hellwig2659e572015-10-02 18:51:31 +02001570 nvme_free_queues(dev, i);
Keith Busch42f61422014-03-24 10:46:25 -06001571 break;
Christoph Hellwig2659e572015-10-02 18:51:31 +02001572 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001573 }
1574
1575 /*
1576 * Ignore failing Create SQ/CQ commands, we can continue with less
1577 * than the desired aount of queues, and even a controller without
1578 * I/O queues an still be used to issue admin commands. This might
1579 * be useful to upgrade a buggy firmware for example.
1580 */
1581 return ret >= 0 ? 0 : ret;
Keith Busch42f61422014-03-24 10:46:25 -06001582}
1583
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001584static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1585{
1586 u64 szu, size, offset;
1587 u32 cmbloc;
1588 resource_size_t bar_size;
1589 struct pci_dev *pdev = to_pci_dev(dev->dev);
1590 void __iomem *cmb;
1591 dma_addr_t dma_addr;
1592
1593 if (!use_cmb_sqes)
1594 return NULL;
1595
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001596 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001597 if (!(NVME_CMB_SZ(dev->cmbsz)))
1598 return NULL;
1599
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001600 cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001601
1602 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1603 size = szu * NVME_CMB_SZ(dev->cmbsz);
1604 offset = szu * NVME_CMB_OFST(cmbloc);
1605 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
1606
1607 if (offset > bar_size)
1608 return NULL;
1609
1610 /*
1611 * Controllers may support a CMB size larger than their BAR,
1612 * for example, due to being behind a bridge. Reduce the CMB to
1613 * the reported size of the BAR
1614 */
1615 if (size > bar_size - offset)
1616 size = bar_size - offset;
1617
1618 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
1619 cmb = ioremap_wc(dma_addr, size);
1620 if (!cmb)
1621 return NULL;
1622
1623 dev->cmb_dma_addr = dma_addr;
1624 dev->cmb_size = size;
1625 return cmb;
1626}
1627
1628static inline void nvme_release_cmb(struct nvme_dev *dev)
1629{
1630 if (dev->cmb) {
1631 iounmap(dev->cmb);
1632 dev->cmb = NULL;
1633 }
1634}
1635
Keith Busch9d713c22013-07-15 15:02:24 -06001636static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1637{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001638 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001639}
1640
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001641static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001642{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001643 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02001644 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch42f61422014-03-24 10:46:25 -06001645 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001646
Keith Busch42f61422014-03-24 10:46:25 -06001647 nr_io_queues = num_possible_cpus();
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001648 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
1649 if (result < 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001650 return result;
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001651
1652 /*
1653 * Degraded controllers might return an error when setting the queue
1654 * count. We still want to be able to bring them online and offer
1655 * access to the admin queue, as that might be only way to fix them up.
1656 */
1657 if (result > 0) {
1658 dev_err(dev->dev, "Could not set queue count (%d)\n", result);
1659 nr_io_queues = 0;
1660 result = 0;
1661 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001662
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001663 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1664 result = nvme_cmb_qdepth(dev, nr_io_queues,
1665 sizeof(struct nvme_command));
1666 if (result > 0)
1667 dev->q_depth = result;
1668 else
1669 nvme_release_cmb(dev);
1670 }
1671
Keith Busch9d713c22013-07-15 15:02:24 -06001672 size = db_bar_size(dev, nr_io_queues);
1673 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001674 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001675 do {
1676 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1677 if (dev->bar)
1678 break;
1679 if (!--nr_io_queues)
1680 return -ENOMEM;
1681 size = db_bar_size(dev, nr_io_queues);
1682 } while (1);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001683 dev->dbs = dev->bar + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07001684 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001685 }
1686
Keith Busch9d713c22013-07-15 15:02:24 -06001687 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05001688 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001689
Jens Axboee32efbf2014-11-14 09:49:26 -07001690 /*
1691 * If we enable msix early due to not intx, disable it again before
1692 * setting up the full range we need.
1693 */
1694 if (!pdev->irq)
1695 pci_disable_msix(pdev);
1696
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001697 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001698 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001699 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
1700 if (vecs < 0) {
1701 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
1702 if (vecs < 0) {
1703 vecs = 1;
1704 } else {
1705 for (i = 0; i < vecs; i++)
1706 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001707 }
1708 }
1709
Matthew Wilcox063a8092013-06-20 10:53:48 -04001710 /*
1711 * Should investigate if there's a performance win from allocating
1712 * more queues than interrupt vectors; it might allow the submission
1713 * path to scale better, even if the receive path is limited by the
1714 * number of interrupts.
1715 */
1716 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06001717 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001718
Matthew Wilcox3193f072014-01-27 15:57:22 -05001719 result = queue_request_irq(dev, adminq, adminq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001720 if (result) {
1721 adminq->cq_vector = -1;
Keith Busch22404272013-07-15 15:02:20 -06001722 goto free_queues;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001723 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001724
Keith Buschcd638942013-07-15 15:02:23 -06001725 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06001726 nvme_free_queues(dev, nr_io_queues + 1);
Christoph Hellwig749941f2015-11-26 11:46:39 +01001727 return nvme_create_io_queues(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001728
Keith Busch22404272013-07-15 15:02:20 -06001729 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05001730 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06001731 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001732}
1733
Keith Buschbda4e0f2015-09-03 08:18:17 -06001734static void nvme_set_irq_hints(struct nvme_dev *dev)
1735{
1736 struct nvme_queue *nvmeq;
1737 int i;
1738
1739 for (i = 0; i < dev->online_queues; i++) {
1740 nvmeq = dev->queues[i];
1741
1742 if (!nvmeq->tags || !(*nvmeq->tags))
1743 continue;
1744
1745 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
1746 blk_mq_tags_cpumask(*nvmeq->tags));
1747 }
1748}
1749
Keith Buscha5768aa2015-06-01 14:28:14 -06001750static void nvme_dev_scan(struct work_struct *work)
1751{
1752 struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06001753
1754 if (!dev->tagset.tags)
1755 return;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001756 nvme_scan_namespaces(&dev->ctrl);
Keith Buschbda4e0f2015-09-03 08:18:17 -06001757 nvme_set_irq_hints(dev);
Keith Buscha5768aa2015-06-01 14:28:14 -06001758}
1759
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001760/*
1761 * Return: error value if an error occurred setting up the queues or calling
1762 * Identify Device. 0 if these succeeded, even if adding some of the
1763 * namespaces failed. At the moment, these failures are silent. TBD which
1764 * failures should be reported.
1765 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001766static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001767{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001768 if (!dev->ctrl.tagset) {
Keith Buschffe77042015-06-08 10:08:15 -06001769 dev->tagset.ops = &nvme_mq_ops;
1770 dev->tagset.nr_hw_queues = dev->online_queues - 1;
1771 dev->tagset.timeout = NVME_IO_TIMEOUT;
1772 dev->tagset.numa_node = dev_to_node(dev->dev);
1773 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001774 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06001775 dev->tagset.cmd_size = nvme_cmd_size(dev);
1776 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
1777 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001778
Keith Buschffe77042015-06-08 10:08:15 -06001779 if (blk_mq_alloc_tag_set(&dev->tagset))
1780 return 0;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001781 dev->ctrl.tagset = &dev->tagset;
Keith Buschffe77042015-06-08 10:08:15 -06001782 }
Keith Buscha5768aa2015-06-01 14:28:14 -06001783 schedule_work(&dev->scan_work);
Keith Busche1e5e562015-02-19 13:39:03 -07001784 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001785}
1786
Keith Busch0877cb02013-07-15 15:02:19 -06001787static int nvme_dev_map(struct nvme_dev *dev)
1788{
Keith Busch42f61422014-03-24 10:46:25 -06001789 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06001790 int bars, result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001791 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001792
1793 if (pci_enable_device_mem(pdev))
1794 return result;
1795
1796 dev->entry[0].vector = pdev->irq;
1797 pci_set_master(pdev);
1798 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07001799 if (!bars)
1800 goto disable_pci;
1801
Keith Busch0877cb02013-07-15 15:02:19 -06001802 if (pci_request_selected_regions(pdev, bars, "nvme"))
1803 goto disable_pci;
1804
Christoph Hellwige75ec752015-05-22 11:12:39 +02001805 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
1806 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01001807 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06001808
Keith Busch0877cb02013-07-15 15:02:19 -06001809 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1810 if (!dev->bar)
1811 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07001812
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001813 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07001814 result = -ENODEV;
1815 goto unmap;
1816 }
Jens Axboee32efbf2014-11-14 09:49:26 -07001817
1818 /*
1819 * Some devices don't advertse INTx interrupts, pre-enable a single
1820 * MSIX vec for setup. We'll adjust this later.
1821 */
1822 if (!pdev->irq) {
1823 result = pci_enable_msix(pdev, dev->entry, 1);
1824 if (result < 0)
1825 goto unmap;
1826 }
1827
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001828 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
1829
Keith Busch42f61422014-03-24 10:46:25 -06001830 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
1831 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001832 dev->dbs = dev->bar + 4096;
1833 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2))
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001834 dev->cmb = nvme_map_cmb(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001835
1836 return 0;
1837
Keith Busch0e53d182013-12-10 13:10:39 -07001838 unmap:
1839 iounmap(dev->bar);
1840 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06001841 disable:
1842 pci_release_regions(pdev);
1843 disable_pci:
1844 pci_disable_device(pdev);
1845 return result;
1846}
1847
1848static void nvme_dev_unmap(struct nvme_dev *dev)
1849{
Christoph Hellwige75ec752015-05-22 11:12:39 +02001850 struct pci_dev *pdev = to_pci_dev(dev->dev);
1851
1852 if (pdev->msi_enabled)
1853 pci_disable_msi(pdev);
1854 else if (pdev->msix_enabled)
1855 pci_disable_msix(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001856
1857 if (dev->bar) {
1858 iounmap(dev->bar);
1859 dev->bar = NULL;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001860 pci_release_regions(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001861 }
1862
Christoph Hellwige75ec752015-05-22 11:12:39 +02001863 if (pci_is_enabled(pdev))
1864 pci_disable_device(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001865}
1866
Keith Busch4d115422013-12-10 13:10:40 -07001867struct nvme_delq_ctx {
1868 struct task_struct *waiter;
1869 struct kthread_worker *worker;
1870 atomic_t refcount;
1871};
1872
1873static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
1874{
1875 dq->waiter = current;
1876 mb();
1877
1878 for (;;) {
1879 set_current_state(TASK_KILLABLE);
1880 if (!atomic_read(&dq->refcount))
1881 break;
1882 if (!schedule_timeout(ADMIN_TIMEOUT) ||
1883 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07001884 /*
1885 * Disable the controller first since we can't trust it
1886 * at this point, but leave the admin queue enabled
1887 * until all queue deletion requests are flushed.
1888 * FIXME: This may take a while if there are more h/w
1889 * queues than admin tags.
1890 */
Keith Busch4d115422013-12-10 13:10:40 -07001891 set_current_state(TASK_RUNNING);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001892 nvme_disable_ctrl(&dev->ctrl,
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001893 lo_hi_readq(dev->bar + NVME_REG_CAP));
Keith Busch0fb59cb2015-01-07 18:55:50 -07001894 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07001895 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07001896 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07001897 return;
1898 }
1899 }
1900 set_current_state(TASK_RUNNING);
1901}
1902
1903static void nvme_put_dq(struct nvme_delq_ctx *dq)
1904{
1905 atomic_dec(&dq->refcount);
1906 if (dq->waiter)
1907 wake_up_process(dq->waiter);
1908}
1909
1910static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
1911{
1912 atomic_inc(&dq->refcount);
1913 return dq;
1914}
1915
1916static void nvme_del_queue_end(struct nvme_queue *nvmeq)
1917{
1918 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
Keith Busch4d115422013-12-10 13:10:40 -07001919 nvme_put_dq(dq);
Keith Busch604e8c82015-11-20 08:38:13 -07001920
1921 spin_lock_irq(&nvmeq->q_lock);
1922 nvme_process_cq(nvmeq);
1923 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001924}
1925
1926static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
1927 kthread_work_func_t fn)
1928{
1929 struct nvme_command c;
1930
1931 memset(&c, 0, sizeof(c));
1932 c.delete_queue.opcode = opcode;
1933 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
1934
1935 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001936 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
1937 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07001938}
1939
1940static void nvme_del_cq_work_handler(struct kthread_work *work)
1941{
1942 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1943 cmdinfo.work);
1944 nvme_del_queue_end(nvmeq);
1945}
1946
1947static int nvme_delete_cq(struct nvme_queue *nvmeq)
1948{
1949 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
1950 nvme_del_cq_work_handler);
1951}
1952
1953static void nvme_del_sq_work_handler(struct kthread_work *work)
1954{
1955 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1956 cmdinfo.work);
1957 int status = nvmeq->cmdinfo.status;
1958
1959 if (!status)
1960 status = nvme_delete_cq(nvmeq);
1961 if (status)
1962 nvme_del_queue_end(nvmeq);
1963}
1964
1965static int nvme_delete_sq(struct nvme_queue *nvmeq)
1966{
1967 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
1968 nvme_del_sq_work_handler);
1969}
1970
1971static void nvme_del_queue_start(struct kthread_work *work)
1972{
1973 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1974 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07001975 if (nvme_delete_sq(nvmeq))
1976 nvme_del_queue_end(nvmeq);
1977}
1978
1979static void nvme_disable_io_queues(struct nvme_dev *dev)
1980{
1981 int i;
1982 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
1983 struct nvme_delq_ctx dq;
1984 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001985 &worker, "nvme%d", dev->ctrl.instance);
Keith Busch4d115422013-12-10 13:10:40 -07001986
1987 if (IS_ERR(kworker_task)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001988 dev_err(dev->dev,
Keith Busch4d115422013-12-10 13:10:40 -07001989 "Failed to create queue del task\n");
1990 for (i = dev->queue_count - 1; i > 0; i--)
1991 nvme_disable_queue(dev, i);
1992 return;
1993 }
1994
1995 dq.waiter = NULL;
1996 atomic_set(&dq.refcount, 0);
1997 dq.worker = &worker;
1998 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001999 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002000
2001 if (nvme_suspend_queue(nvmeq))
2002 continue;
2003 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2004 nvmeq->cmdinfo.worker = dq.worker;
2005 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2006 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2007 }
2008 nvme_wait_dq(&dq, dev);
2009 kthread_stop(kworker_task);
2010}
2011
Christoph Hellwig73850142015-10-22 14:03:33 +02002012static int nvme_dev_list_add(struct nvme_dev *dev)
2013{
2014 bool start_thread = false;
2015
2016 spin_lock(&dev_list_lock);
2017 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2018 start_thread = true;
2019 nvme_thread = NULL;
2020 }
2021 list_add(&dev->node, &dev_list);
2022 spin_unlock(&dev_list_lock);
2023
2024 if (start_thread) {
2025 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2026 wake_up_all(&nvme_kthread_wait);
2027 } else
2028 wait_event_killable(nvme_kthread_wait, nvme_thread);
2029
2030 if (IS_ERR_OR_NULL(nvme_thread))
2031 return nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2032
2033 return 0;
2034}
2035
Dan McLeranb9afca32014-04-07 17:10:11 -06002036/*
2037* Remove the node from the device list and check
2038* for whether or not we need to stop the nvme_thread.
2039*/
2040static void nvme_dev_list_remove(struct nvme_dev *dev)
2041{
2042 struct task_struct *tmp = NULL;
2043
2044 spin_lock(&dev_list_lock);
2045 list_del_init(&dev->node);
2046 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2047 tmp = nvme_thread;
2048 nvme_thread = NULL;
2049 }
2050 spin_unlock(&dev_list_lock);
2051
2052 if (tmp)
2053 kthread_stop(tmp);
2054}
2055
Keith Buschc9d3bf82015-01-07 18:55:52 -07002056static void nvme_freeze_queues(struct nvme_dev *dev)
2057{
2058 struct nvme_ns *ns;
2059
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002060 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
Keith Buschc9d3bf82015-01-07 18:55:52 -07002061 blk_mq_freeze_queue_start(ns->queue);
2062
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002063 spin_lock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002064 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002065 spin_unlock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002066
2067 blk_mq_cancel_requeue_work(ns->queue);
2068 blk_mq_stop_hw_queues(ns->queue);
2069 }
2070}
2071
2072static void nvme_unfreeze_queues(struct nvme_dev *dev)
2073{
2074 struct nvme_ns *ns;
2075
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002076 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
Keith Buschc9d3bf82015-01-07 18:55:52 -07002077 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2078 blk_mq_unfreeze_queue(ns->queue);
2079 blk_mq_start_stopped_hw_queues(ns->queue, true);
2080 blk_mq_kick_requeue_list(ns->queue);
2081 }
2082}
2083
Keith Buschf0b50732013-07-15 15:02:21 -06002084static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002085{
Keith Busch22404272013-07-15 15:02:20 -06002086 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002087 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002088
Dan McLeranb9afca32014-04-07 17:10:11 -06002089 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002090
Keith Busch77bf25e2015-11-26 12:21:29 +01002091 mutex_lock(&dev->shutdown_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002092 if (dev->bar) {
2093 nvme_freeze_queues(dev);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002094 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002095 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002096 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002097 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002098 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002099 nvme_suspend_queue(nvmeq);
Keith Busch4d115422013-12-10 13:10:40 -07002100 }
2101 } else {
2102 nvme_disable_io_queues(dev);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002103 nvme_shutdown_ctrl(&dev->ctrl);
Keith Busch4d115422013-12-10 13:10:40 -07002104 nvme_disable_queue(dev, 0);
2105 }
Keith Buschf0b50732013-07-15 15:02:21 -06002106 nvme_dev_unmap(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002107
2108 for (i = dev->queue_count - 1; i >= 0; i--)
2109 nvme_clear_queue(dev->queues[i]);
Keith Busch77bf25e2015-11-26 12:21:29 +01002110 mutex_unlock(&dev->shutdown_lock);
Keith Buschf0b50732013-07-15 15:02:21 -06002111}
2112
Matthew Wilcox091b6092011-02-10 09:56:01 -05002113static int nvme_setup_prp_pools(struct nvme_dev *dev)
2114{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002115 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002116 PAGE_SIZE, PAGE_SIZE, 0);
2117 if (!dev->prp_page_pool)
2118 return -ENOMEM;
2119
Matthew Wilcox99802a72011-02-10 10:30:34 -05002120 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002121 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002122 256, 256, 0);
2123 if (!dev->prp_small_pool) {
2124 dma_pool_destroy(dev->prp_page_pool);
2125 return -ENOMEM;
2126 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002127 return 0;
2128}
2129
2130static void nvme_release_prp_pools(struct nvme_dev *dev)
2131{
2132 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002133 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002134}
2135
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002136static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
Keith Busch5e82e952013-02-19 10:17:58 -07002137{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002138 struct nvme_dev *dev = to_nvme_dev(ctrl);
Keith Busch9ac27092014-01-31 16:53:39 -07002139
Christoph Hellwige75ec752015-05-22 11:12:39 +02002140 put_device(dev->dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002141 if (dev->tagset.tags)
2142 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002143 if (dev->ctrl.admin_q)
2144 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002145 kfree(dev->queues);
2146 kfree(dev->entry);
2147 kfree(dev);
2148}
2149
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002150static void nvme_reset_work(struct work_struct *work)
Keith Buschf0b50732013-07-15 15:02:21 -06002151{
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002152 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002153 int result;
Keith Buschf0b50732013-07-15 15:02:21 -06002154
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002155 if (WARN_ON(test_bit(NVME_CTRL_RESETTING, &dev->flags)))
2156 goto out;
2157
2158 /*
2159 * If we're called to reset a live controller first shut it down before
2160 * moving on.
2161 */
2162 if (dev->bar)
2163 nvme_dev_shutdown(dev);
2164
2165 set_bit(NVME_CTRL_RESETTING, &dev->flags);
2166
Keith Buschf0b50732013-07-15 15:02:21 -06002167 result = nvme_dev_map(dev);
2168 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002169 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002170
2171 result = nvme_configure_admin_queue(dev);
2172 if (result)
2173 goto unmap;
2174
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002175 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002176 result = nvme_alloc_admin_tags(dev);
2177 if (result)
2178 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06002179
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002180 result = nvme_init_identify(&dev->ctrl);
2181 if (result)
2182 goto free_tags;
2183
Keith Buschf0b50732013-07-15 15:02:21 -06002184 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002185 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07002186 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06002187
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002188 dev->ctrl.event_limit = 1;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002189
Christoph Hellwig73850142015-10-22 14:03:33 +02002190 result = nvme_dev_list_add(dev);
2191 if (result)
2192 goto remove;
2193
Christoph Hellwig2659e572015-10-02 18:51:31 +02002194 /*
2195 * Keep the controller around but remove all namespaces if we don't have
2196 * any working I/O queue.
2197 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002198 if (dev->online_queues < 2) {
2199 dev_warn(dev->dev, "IO queues not created\n");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002200 nvme_remove_namespaces(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002201 } else {
2202 nvme_unfreeze_queues(dev);
2203 nvme_dev_add(dev);
2204 }
2205
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002206 clear_bit(NVME_CTRL_RESETTING, &dev->flags);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002207 return;
Keith Buschf0b50732013-07-15 15:02:21 -06002208
Christoph Hellwig73850142015-10-22 14:03:33 +02002209 remove:
2210 nvme_dev_list_remove(dev);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002211 free_tags:
2212 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002213 blk_put_queue(dev->ctrl.admin_q);
2214 dev->ctrl.admin_q = NULL;
Keith Busch4af0e212015-06-08 10:08:13 -06002215 dev->queues[0]->tags = NULL;
Keith Buschf0b50732013-07-15 15:02:21 -06002216 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002217 nvme_disable_queue(dev, 0);
Keith Buschf0b50732013-07-15 15:02:21 -06002218 unmap:
2219 nvme_dev_unmap(dev);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002220 out:
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002221 if (!work_pending(&dev->reset_work))
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002222 nvme_dead_ctrl(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002223}
2224
Keith Busch9a6b9452013-12-10 13:10:36 -07002225static int nvme_remove_dead_ctrl(void *arg)
2226{
2227 struct nvme_dev *dev = (struct nvme_dev *)arg;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002228 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002229
2230 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002231 pci_stop_and_remove_bus_device_locked(pdev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002232 nvme_put_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002233 return 0;
2234}
2235
Keith Buschde3eff22015-06-18 13:36:39 -06002236static void nvme_dead_ctrl(struct nvme_dev *dev)
2237{
2238 dev_warn(dev->dev, "Device failed to resume\n");
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002239 kref_get(&dev->ctrl.kref);
Keith Buschde3eff22015-06-18 13:36:39 -06002240 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002241 dev->ctrl.instance))) {
Keith Buschde3eff22015-06-18 13:36:39 -06002242 dev_err(dev->dev,
2243 "Failed to start controller remove task\n");
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002244 nvme_put_ctrl(&dev->ctrl);
Keith Buschde3eff22015-06-18 13:36:39 -06002245 }
2246}
2247
Keith Busch4cc06522015-06-05 10:30:08 -06002248static int nvme_reset(struct nvme_dev *dev)
2249{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002250 if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q))
Keith Busch4cc06522015-06-05 10:30:08 -06002251 return -ENODEV;
2252
Christoph Hellwig846cc052015-11-26 12:10:29 +01002253 if (!queue_work(nvme_workq, &dev->reset_work))
2254 return -EBUSY;
Keith Busch4cc06522015-06-05 10:30:08 -06002255
Christoph Hellwig846cc052015-11-26 12:10:29 +01002256 flush_work(&dev->reset_work);
Christoph Hellwig846cc052015-11-26 12:10:29 +01002257 return 0;
Keith Busch4cc06522015-06-05 10:30:08 -06002258}
2259
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002260static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
2261{
2262 *val = readl(to_nvme_dev(ctrl)->bar + off);
2263 return 0;
2264}
2265
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002266static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2267{
2268 writel(val, to_nvme_dev(ctrl)->bar + off);
2269 return 0;
2270}
2271
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002272static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2273{
2274 *val = readq(to_nvme_dev(ctrl)->bar + off);
2275 return 0;
2276}
2277
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002278static bool nvme_pci_io_incapable(struct nvme_ctrl *ctrl)
2279{
2280 struct nvme_dev *dev = to_nvme_dev(ctrl);
2281
2282 return !dev->bar || dev->online_queues < 2;
2283}
2284
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002285static int nvme_pci_reset_ctrl(struct nvme_ctrl *ctrl)
2286{
2287 return nvme_reset(to_nvme_dev(ctrl));
2288}
2289
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002290static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
2291 .reg_read32 = nvme_pci_reg_read32,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002292 .reg_write32 = nvme_pci_reg_write32,
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002293 .reg_read64 = nvme_pci_reg_read64,
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002294 .io_incapable = nvme_pci_io_incapable,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002295 .reset_ctrl = nvme_pci_reset_ctrl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002296 .free_ctrl = nvme_pci_free_ctrl,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002297};
2298
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002299static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002300{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002301 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002302 struct nvme_dev *dev;
2303
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002304 node = dev_to_node(&pdev->dev);
2305 if (node == NUMA_NO_NODE)
2306 set_dev_node(&pdev->dev, 0);
2307
2308 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002309 if (!dev)
2310 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002311 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2312 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002313 if (!dev->entry)
2314 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002315 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2316 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002317 if (!dev->queues)
2318 goto free;
2319
Christoph Hellwige75ec752015-05-22 11:12:39 +02002320 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002321 pci_set_drvdata(pdev, dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002322
Keith Busche6e96d72015-03-23 09:32:37 -06002323 INIT_LIST_HEAD(&dev->node);
Keith Buscha5768aa2015-06-01 14:28:14 -06002324 INIT_WORK(&dev->scan_work, nvme_dev_scan);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002325 INIT_WORK(&dev->reset_work, nvme_reset_work);
Keith Busch77bf25e2015-11-26 12:21:29 +01002326 mutex_init(&dev->shutdown_lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002327
2328 result = nvme_setup_prp_pools(dev);
2329 if (result)
2330 goto put_pci;
2331
2332 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
2333 id->driver_data);
2334 if (result)
2335 goto release_pools;
2336
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002337 schedule_work(&dev->reset_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002338 return 0;
2339
Keith Busch0877cb02013-07-15 15:02:19 -06002340 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002341 nvme_release_prp_pools(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002342 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002343 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002344 free:
2345 kfree(dev->queues);
2346 kfree(dev->entry);
2347 kfree(dev);
2348 return result;
2349}
2350
Keith Buschf0d54a52014-05-02 10:40:43 -06002351static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2352{
Keith Buscha6739472014-06-23 16:03:21 -06002353 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002354
Keith Buscha6739472014-06-23 16:03:21 -06002355 if (prepare)
2356 nvme_dev_shutdown(dev);
2357 else
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002358 schedule_work(&dev->reset_work);
Keith Buschf0d54a52014-05-02 10:40:43 -06002359}
2360
Keith Busch09ece142014-01-27 11:29:40 -05002361static void nvme_shutdown(struct pci_dev *pdev)
2362{
2363 struct nvme_dev *dev = pci_get_drvdata(pdev);
2364 nvme_dev_shutdown(dev);
2365}
2366
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002367static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002368{
2369 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002370
2371 spin_lock(&dev_list_lock);
2372 list_del_init(&dev->node);
2373 spin_unlock(&dev_list_lock);
2374
2375 pci_set_drvdata(pdev, NULL);
2376 flush_work(&dev->reset_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06002377 flush_work(&dev->scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002378 nvme_remove_namespaces(&dev->ctrl);
Keith Busch3399a3f2015-06-18 13:36:40 -06002379 nvme_dev_shutdown(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002380 nvme_dev_remove_admin(dev);
2381 nvme_free_queues(dev, 0);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002382 nvme_release_cmb(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002383 nvme_release_prp_pools(dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002384 nvme_put_ctrl(&dev->ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002385}
2386
2387/* These functions are yet to be implemented */
2388#define nvme_error_detected NULL
2389#define nvme_dump_registers NULL
2390#define nvme_link_reset NULL
2391#define nvme_slot_reset NULL
2392#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002393
Jingoo Han671a6012014-02-13 11:19:14 +09002394#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002395static int nvme_suspend(struct device *dev)
2396{
2397 struct pci_dev *pdev = to_pci_dev(dev);
2398 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2399
2400 nvme_dev_shutdown(ndev);
2401 return 0;
2402}
2403
2404static int nvme_resume(struct device *dev)
2405{
2406 struct pci_dev *pdev = to_pci_dev(dev);
2407 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002408
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002409 schedule_work(&ndev->reset_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002410 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002411}
Jingoo Han671a6012014-02-13 11:19:14 +09002412#endif
Keith Buschcd638942013-07-15 15:02:23 -06002413
2414static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002415
Stephen Hemminger1d352032012-09-07 09:33:17 -07002416static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002417 .error_detected = nvme_error_detected,
2418 .mmio_enabled = nvme_dump_registers,
2419 .link_reset = nvme_link_reset,
2420 .slot_reset = nvme_slot_reset,
2421 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002422 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002423};
2424
2425/* Move to pci_ids.h later */
2426#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2427
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002428static const struct pci_device_id nvme_id_table[] = {
Christoph Hellwig106198e2015-11-26 10:07:41 +01002429 { PCI_VDEVICE(INTEL, 0x0953),
2430 .driver_data = NVME_QUIRK_STRIPE_SIZE, },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002431 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01002432 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002433 { 0, }
2434};
2435MODULE_DEVICE_TABLE(pci, nvme_id_table);
2436
2437static struct pci_driver nvme_driver = {
2438 .name = "nvme",
2439 .id_table = nvme_id_table,
2440 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002441 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002442 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002443 .driver = {
2444 .pm = &nvme_dev_pm_ops,
2445 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002446 .err_handler = &nvme_err_handler,
2447};
2448
2449static int __init nvme_init(void)
2450{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002451 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002452
Dan McLeranb9afca32014-04-07 17:10:11 -06002453 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002454
Keith Busch9a6b9452013-12-10 13:10:36 -07002455 nvme_workq = create_singlethread_workqueue("nvme");
2456 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002457 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002458
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002459 result = nvme_core_init();
Keith Busch5c42ea12012-07-25 16:05:18 -06002460 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002461 goto kill_workq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002462
Keith Buschf3db22f2014-06-11 11:51:35 -06002463 result = pci_register_driver(&nvme_driver);
2464 if (result)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002465 goto core_exit;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002466 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002467
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002468 core_exit:
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002469 nvme_core_exit();
Keith Busch9a6b9452013-12-10 13:10:36 -07002470 kill_workq:
2471 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002472 return result;
2473}
2474
2475static void __exit nvme_exit(void)
2476{
2477 pci_unregister_driver(&nvme_driver);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002478 nvme_core_exit();
Keith Busch9a6b9452013-12-10 13:10:36 -07002479 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06002480 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002481 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002482}
2483
2484MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2485MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002486MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002487module_init(nvme_init);
2488module_exit(nvme_exit);