blob: d6d92b022f972f03de2e3a003090302f740e5e91 [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 Hellwig5c8809e2015-11-26 12:35:49 +010083static void nvme_remove_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;
Keith Busch4d115422013-12-10 13:10:40 -070089 int status;
90 void *ctx;
91};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050092
93/*
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010094 * Represents an NVM Express device. Each nvme_dev is a PCI function.
95 */
96struct nvme_dev {
97 struct list_head node;
98 struct nvme_queue **queues;
99 struct blk_mq_tag_set tagset;
100 struct blk_mq_tag_set admin_tagset;
101 u32 __iomem *dbs;
102 struct device *dev;
103 struct dma_pool *prp_page_pool;
104 struct dma_pool *prp_small_pool;
105 unsigned queue_count;
106 unsigned online_queues;
107 unsigned max_qid;
108 int q_depth;
109 u32 db_stride;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100110 struct msix_entry *entry;
111 void __iomem *bar;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100112 struct work_struct reset_work;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100113 struct work_struct scan_work;
Christoph Hellwig5c8809e2015-11-26 12:35:49 +0100114 struct work_struct remove_work;
Keith Busch77bf25e2015-11-26 12:21:29 +0100115 struct mutex shutdown_lock;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100116 bool subsystem;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100117 void __iomem *cmb;
118 dma_addr_t cmb_dma_addr;
119 u64 cmb_size;
120 u32 cmbsz;
Christoph Hellwigfd634f412015-11-26 12:42:26 +0100121 unsigned long flags;
122#define NVME_CTRL_RESETTING 0
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100123
124 struct nvme_ctrl ctrl;
125};
126
127static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
128{
129 return container_of(ctrl, struct nvme_dev, ctrl);
130}
131
132/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500133 * An NVM Express queue. Each device has at least two (one for admin
134 * commands and one for I/O commands).
135 */
136struct nvme_queue {
137 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500138 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500139 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500140 spinlock_t q_lock;
141 struct nvme_command *sq_cmds;
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600142 struct nvme_command __iomem *sq_cmds_io;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500143 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600144 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500145 dma_addr_t sq_dma_addr;
146 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500147 u32 __iomem *q_db;
148 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700149 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500150 u16 sq_head;
151 u16 sq_tail;
152 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700153 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400154 u8 cq_phase;
155 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700156 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500157};
158
159/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200160 * The nvme_iod describes the data in an I/O, including the list of PRP
161 * entries. You can't see it in this data structure because C doesn't let
162 * me express that. Use nvme_alloc_iod to ensure there's enough space
163 * allocated to store the PRP list.
164 */
165struct nvme_iod {
166 unsigned long private; /* For the use of the submitter of the I/O */
167 int npages; /* In the PRP list. 0 means small pool in use */
168 int offset; /* Of PRP list */
169 int nents; /* Used in scatterlist */
170 int length; /* Of data, in bytes */
171 dma_addr_t first_dma;
172 struct scatterlist meta_sg[1]; /* metadata requires single contiguous buffer */
173 struct scatterlist sg[0];
174};
175
176/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500177 * Check we didin't inadvertently grow the command struct
178 */
179static inline void _nvme_check_size(void)
180{
181 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
182 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
183 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
184 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
185 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400186 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700187 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500188 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
189 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
190 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
191 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600192 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500193}
194
Keith Buschedd10d32014-04-03 16:45:23 -0600195typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400196 struct nvme_completion *);
197
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500198struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400199 nvme_completion_fn fn;
200 void *ctx;
Keith Buschc30341d2013-12-10 13:10:38 -0700201 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700202 struct nvme_queue *nvmeq;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700203 struct nvme_iod iod[0];
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500204};
205
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700206/*
207 * Max size of iod being embedded in the request payload
208 */
209#define NVME_INT_PAGES 2
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100210#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
Chong Yuanfda631f2015-03-27 09:21:32 +0800211#define NVME_INT_MASK 0x01
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700212
213/*
214 * Will slightly overestimate the number of pages needed. This is OK
215 * as it only leads to a small amount of wasted memory for the lifetime of
216 * the I/O.
217 */
218static int nvme_npages(unsigned size, struct nvme_dev *dev)
219{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100220 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
221 dev->ctrl.page_size);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700222 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
223}
224
225static unsigned int nvme_cmd_size(struct nvme_dev *dev)
226{
227 unsigned int ret = sizeof(struct nvme_cmd_info);
228
229 ret += sizeof(struct nvme_iod);
230 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
231 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
232
233 return ret;
234}
235
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700236static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
237 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500238{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700239 struct nvme_dev *dev = data;
240 struct nvme_queue *nvmeq = dev->queues[0];
241
Keith Busch42483222015-06-01 09:29:54 -0600242 WARN_ON(hctx_idx != 0);
243 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
244 WARN_ON(nvmeq->tags);
245
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700246 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600247 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700248 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500249}
250
Keith Busch4af0e212015-06-08 10:08:13 -0600251static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
252{
253 struct nvme_queue *nvmeq = hctx->driver_data;
254
255 nvmeq->tags = NULL;
256}
257
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700258static int nvme_admin_init_request(void *data, struct request *req,
259 unsigned int hctx_idx, unsigned int rq_idx,
260 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600261{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700262 struct nvme_dev *dev = data;
263 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
264 struct nvme_queue *nvmeq = dev->queues[0];
265
266 BUG_ON(!nvmeq);
267 cmd->nvmeq = nvmeq;
268 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600269}
270
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700271static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
272 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500273{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700274 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600275 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500276
Keith Busch42483222015-06-01 09:29:54 -0600277 if (!nvmeq->tags)
278 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500279
Keith Busch42483222015-06-01 09:29:54 -0600280 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700281 hctx->driver_data = nvmeq;
282 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500283}
284
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700285static int nvme_init_request(void *data, struct request *req,
286 unsigned int hctx_idx, unsigned int rq_idx,
287 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500288{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700289 struct nvme_dev *dev = data;
290 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
291 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
292
293 BUG_ON(!nvmeq);
294 cmd->nvmeq = nvmeq;
295 return 0;
296}
297
298static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
299 nvme_completion_fn handler)
300{
301 cmd->fn = handler;
302 cmd->ctx = ctx;
303 cmd->aborted = 0;
Keith Buschc917dfe2015-01-07 18:55:48 -0700304 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500305}
306
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700307static void *iod_get_private(struct nvme_iod *iod)
308{
309 return (void *) (iod->private & ~0x1UL);
310}
311
312/*
313 * If bit 0 is set, the iod is embedded in the request payload.
314 */
315static bool iod_should_kfree(struct nvme_iod *iod)
316{
Chong Yuanfda631f2015-03-27 09:21:32 +0800317 return (iod->private & NVME_INT_MASK) == 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700318}
319
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400320/* Special values must be less than 0x1000 */
321#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500322#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
323#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
324#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500325
Keith Buschedd10d32014-04-03 16:45:23 -0600326static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400327 struct nvme_completion *cqe)
328{
329 if (ctx == CMD_CTX_CANCELLED)
330 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400331 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600332 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400333 "completed id %d twice on queue %d\n",
334 cqe->command_id, le16_to_cpup(&cqe->sq_id));
335 return;
336 }
337 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600338 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400339 "invalid id %d completed on queue %d\n",
340 cqe->command_id, le16_to_cpup(&cqe->sq_id));
341 return;
342 }
Keith Buschedd10d32014-04-03 16:45:23 -0600343 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400344}
345
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700346static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
347{
348 void *ctx;
349
350 if (fn)
351 *fn = cmd->fn;
352 ctx = cmd->ctx;
353 cmd->fn = special_completion;
354 cmd->ctx = CMD_CTX_CANCELLED;
355 return ctx;
356}
357
358static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
359 struct nvme_completion *cqe)
360{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700361 u32 result = le32_to_cpup(&cqe->result);
362 u16 status = le16_to_cpup(&cqe->status) >> 1;
363
364 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100365 ++nvmeq->dev->ctrl.event_limit;
Keith Buscha5768aa2015-06-01 14:28:14 -0600366 if (status != NVME_SC_SUCCESS)
367 return;
368
369 switch (result & 0xff07) {
370 case NVME_AER_NOTICE_NS_CHANGED:
371 dev_info(nvmeq->q_dmadev, "rescanning\n");
Keith Busch92f7a162015-10-23 11:42:02 -0600372 queue_work(nvme_workq, &nvmeq->dev->scan_work);
Keith Buscha5768aa2015-06-01 14:28:14 -0600373 default:
374 dev_warn(nvmeq->q_dmadev, "async event result %08x\n", result);
375 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700376}
377
378static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
379 struct nvme_completion *cqe)
380{
381 struct request *req = ctx;
382
383 u16 status = le16_to_cpup(&cqe->status) >> 1;
384 u32 result = le32_to_cpup(&cqe->result);
385
Keith Busch42483222015-06-01 09:29:54 -0600386 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700387
388 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +0100389 atomic_inc(&nvmeq->dev->ctrl.abort_limit);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700390}
391
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700392static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
393 unsigned int tag)
394{
Keith Busch42483222015-06-01 09:29:54 -0600395 struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700396
397 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700398}
399
Matthew Wilcox184d2942011-05-11 21:36:38 -0400400/*
401 * Called with local interrupts disabled and the q_lock held. May not sleep.
402 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700403static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400404 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500405{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700406 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400407 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700408 if (tag >= nvmeq->q_depth) {
409 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500410 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400411 }
Keith Busch859361a2012-08-02 14:05:59 -0600412 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700413 *fn = cmd->fn;
414 ctx = cmd->ctx;
415 cmd->fn = special_completion;
416 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400417 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500418}
419
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500420/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400421 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500422 * @nvmeq: The queue to use
423 * @cmd: The command to send
424 *
425 * Safe to use from interrupt context
426 */
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530427static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
428 struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500429{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700430 u16 tail = nvmeq->sq_tail;
431
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600432 if (nvmeq->sq_cmds_io)
433 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
434 else
435 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
436
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500437 if (++tail == nvmeq->q_depth)
438 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500439 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500440 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500441}
442
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530443static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700444{
445 unsigned long flags;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700446 spin_lock_irqsave(&nvmeq->q_lock, flags);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530447 __nvme_submit_cmd(nvmeq, cmd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700448 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700449}
450
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500451static __le64 **iod_list(struct nvme_iod *iod)
452{
453 return ((void *)iod) + iod->offset;
454}
455
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700456static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
457 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500458{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700459 iod->private = private;
460 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
461 iod->npages = -1;
462 iod->length = nbytes;
463 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500464}
465
466static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700467__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
468 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500469{
470 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700471 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500472 sizeof(struct scatterlist) * nseg, gfp);
473
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700474 if (iod)
475 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500476
477 return iod;
478}
479
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700480static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
481 gfp_t gfp)
482{
483 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
484 sizeof(struct nvme_dsm_range);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700485 struct nvme_iod *iod;
486
487 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
488 size <= NVME_INT_BYTES(dev)) {
489 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
490
491 iod = cmd->iod;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700492 iod_init(iod, size, rq->nr_phys_segments,
Chong Yuanfda631f2015-03-27 09:21:32 +0800493 (unsigned long) rq | NVME_INT_MASK);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700494 return iod;
495 }
496
497 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
498 (unsigned long) rq, gfp);
499}
500
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200501static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500502{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100503 const int last_prp = dev->ctrl.page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500504 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500505 __le64 **list = iod_list(iod);
506 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500507
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500508 if (iod->npages == 0)
509 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
510 for (i = 0; i < iod->npages; i++) {
511 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500512 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500513 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500514 prp_dma = next_prp_dma;
515 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700516
517 if (iod_should_kfree(iod))
518 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500519}
520
Keith Busch52b68d72015-02-23 09:16:21 -0700521#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700522static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
523{
524 if (be32_to_cpu(pi->ref_tag) == v)
525 pi->ref_tag = cpu_to_be32(p);
526}
527
528static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
529{
530 if (be32_to_cpu(pi->ref_tag) == p)
531 pi->ref_tag = cpu_to_be32(v);
532}
533
534/**
535 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
536 *
537 * The virtual start sector is the one that was originally submitted by the
538 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
539 * start sector may be different. Remap protection information to match the
540 * physical LBA on writes, and back to the original seed on reads.
541 *
542 * Type 0 and 3 do not have a ref tag, so no remapping required.
543 */
544static void nvme_dif_remap(struct request *req,
545 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
546{
547 struct nvme_ns *ns = req->rq_disk->private_data;
548 struct bio_integrity_payload *bip;
549 struct t10_pi_tuple *pi;
550 void *p, *pmap;
551 u32 i, nlb, ts, phys, virt;
552
553 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
554 return;
555
556 bip = bio_integrity(req->bio);
557 if (!bip)
558 return;
559
560 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700561
562 p = pmap;
563 virt = bip_get_seed(bip);
564 phys = nvme_block_nr(ns, blk_rq_pos(req));
565 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
Dan Williamsac6fc482015-10-21 13:20:18 -0400566 ts = ns->disk->queue->integrity.tuple_size;
Keith Busche1e5e562015-02-19 13:39:03 -0700567
568 for (i = 0; i < nlb; i++, virt++, phys++) {
569 pi = (struct t10_pi_tuple *)p;
570 dif_swap(phys, virt, pi);
571 p += ts;
572 }
573 kunmap_atomic(pmap);
574}
Keith Busch52b68d72015-02-23 09:16:21 -0700575#else /* CONFIG_BLK_DEV_INTEGRITY */
576static void nvme_dif_remap(struct request *req,
577 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
578{
579}
580static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
581{
582}
583static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
584{
585}
Keith Busch52b68d72015-02-23 09:16:21 -0700586#endif
587
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700588static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500589 struct nvme_completion *cqe)
590{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500591 struct nvme_iod *iod = ctx;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700592 struct request *req = iod_get_private(iod);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700593 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500594 u16 status = le16_to_cpup(&cqe->status) >> 1;
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200595 int error = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500596
Keith Buschedd10d32014-04-03 16:45:23 -0600597 if (unlikely(status)) {
Christoph Hellwig7688faa2015-11-28 15:41:58 +0100598 if (nvme_req_needs_retry(req, status)) {
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100599 nvme_unmap_data(nvmeq->dev, iod);
Christoph Hellwig7688faa2015-11-28 15:41:58 +0100600 nvme_requeue_req(req);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100601 return;
Keith Buschedd10d32014-04-03 16:45:23 -0600602 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200603
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200604 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
Keith Busch17188bb42015-06-08 10:08:14 -0600605 if (cmd_rq->ctx == CMD_CTX_CANCELLED)
Christoph Hellwig297465c2015-11-26 12:58:11 +0100606 error = NVME_SC_CANCELLED;
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200607 else
608 error = status;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200609 } else {
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200610 error = nvme_error_status(status);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200611 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200612 }
613
Keith Buscha0a931d2015-05-22 12:28:31 -0600614 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
615 u32 result = le32_to_cpup(&cqe->result);
616 req->special = (void *)(uintptr_t)result;
617 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700618
619 if (cmd_rq->aborted)
Christoph Hellwige75ec752015-05-22 11:12:39 +0200620 dev_warn(nvmeq->dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700621 "completing aborted command with status:%04x\n",
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200622 error);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700623
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100624 nvme_unmap_data(nvmeq->dev, iod);
625 blk_mq_complete_request(req, error);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500626}
627
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200628static bool nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
629 int total_len)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500630{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500631 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500632 int length = total_len;
633 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500634 int dma_len = sg_dma_len(sg);
635 u64 dma_addr = sg_dma_address(sg);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100636 u32 page_size = dev->ctrl.page_size;
Murali Iyerf137e0f2015-03-26 11:07:51 -0500637 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500638 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500639 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500640 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500641 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500642
Keith Busch1d090622014-06-23 11:34:01 -0600643 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500644 if (length <= 0)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200645 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500646
Keith Busch1d090622014-06-23 11:34:01 -0600647 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500648 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600649 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500650 } else {
651 sg = sg_next(sg);
652 dma_addr = sg_dma_address(sg);
653 dma_len = sg_dma_len(sg);
654 }
655
Keith Busch1d090622014-06-23 11:34:01 -0600656 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600657 iod->first_dma = dma_addr;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200658 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500659 }
660
Keith Busch1d090622014-06-23 11:34:01 -0600661 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500662 if (nprps <= (256 / 8)) {
663 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500664 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500665 } else {
666 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500667 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500668 }
669
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200670 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400671 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600672 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500673 iod->npages = -1;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200674 return false;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400675 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500676 list[0] = prp_list;
677 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500678 i = 0;
679 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600680 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500681 __le64 *old_prp_list = prp_list;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200682 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500683 if (!prp_list)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200684 return false;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500685 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400686 prp_list[0] = old_prp_list[i - 1];
687 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
688 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500689 }
690 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600691 dma_len -= page_size;
692 dma_addr += page_size;
693 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500694 if (length <= 0)
695 break;
696 if (dma_len > 0)
697 continue;
698 BUG_ON(dma_len < 0);
699 sg = sg_next(sg);
700 dma_addr = sg_dma_address(sg);
701 dma_len = sg_dma_len(sg);
702 }
703
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200704 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500705}
706
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200707static int nvme_map_data(struct nvme_dev *dev, struct nvme_iod *iod,
708 struct nvme_command *cmnd)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200709{
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200710 struct request *req = iod_get_private(iod);
711 struct request_queue *q = req->q;
712 enum dma_data_direction dma_dir = rq_data_dir(req) ?
713 DMA_TO_DEVICE : DMA_FROM_DEVICE;
714 int ret = BLK_MQ_RQ_QUEUE_ERROR;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200715
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200716 sg_init_table(iod->sg, req->nr_phys_segments);
717 iod->nents = blk_rq_map_sg(q, req, iod->sg);
718 if (!iod->nents)
719 goto out;
720
721 ret = BLK_MQ_RQ_QUEUE_BUSY;
722 if (!dma_map_sg(dev->dev, iod->sg, iod->nents, dma_dir))
723 goto out;
724
725 if (!nvme_setup_prps(dev, iod, blk_rq_bytes(req)))
726 goto out_unmap;
727
728 ret = BLK_MQ_RQ_QUEUE_ERROR;
729 if (blk_integrity_rq(req)) {
730 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
731 goto out_unmap;
732
733 sg_init_table(iod->meta_sg, 1);
734 if (blk_rq_map_integrity_sg(q, req->bio, iod->meta_sg) != 1)
735 goto out_unmap;
736
737 if (rq_data_dir(req))
738 nvme_dif_remap(req, nvme_dif_prep);
739
740 if (!dma_map_sg(dev->dev, iod->meta_sg, 1, dma_dir))
741 goto out_unmap;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200742 }
743
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200744 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
745 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
746 if (blk_integrity_rq(req))
747 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg));
748 return BLK_MQ_RQ_QUEUE_OK;
749
750out_unmap:
751 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
752out:
753 return ret;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200754}
755
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100756static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod)
757{
758 struct request *req = iod_get_private(iod);
759 enum dma_data_direction dma_dir = rq_data_dir(req) ?
760 DMA_TO_DEVICE : DMA_FROM_DEVICE;
761
762 if (iod->nents) {
763 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
764 if (blk_integrity_rq(req)) {
765 if (!rq_data_dir(req))
766 nvme_dif_remap(req, nvme_dif_complete);
767 dma_unmap_sg(dev->dev, iod->meta_sg, 1, dma_dir);
768 }
769 }
770
771 nvme_free_iod(dev, iod);
772}
773
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700774/*
775 * We reuse the small pool to allocate the 16-byte range here as it is not
776 * worth having a special pool for these or additional cases to handle freeing
777 * the iod.
778 */
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200779static int nvme_setup_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
780 struct nvme_iod *iod, struct nvme_command *cmnd)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700781{
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200782 struct request *req = iod_get_private(iod);
783 struct nvme_dsm_range *range;
784
785 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
786 &iod->first_dma);
787 if (!range)
788 return BLK_MQ_RQ_QUEUE_BUSY;
789 iod_list(iod)[0] = (__le64 *)range;
790 iod->npages = 0;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700791
Keith Busch0e5e4f02012-11-09 16:33:05 -0700792 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700793 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
794 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700795
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200796 memset(cmnd, 0, sizeof(*cmnd));
797 cmnd->dsm.opcode = nvme_cmd_dsm;
798 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
799 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
800 cmnd->dsm.nr = 0;
801 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
802 return BLK_MQ_RQ_QUEUE_OK;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700803}
804
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200805/*
806 * NOTE: ns is NULL when called on the admin queue.
807 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700808static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
809 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600810{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700811 struct nvme_ns *ns = hctx->queue->queuedata;
812 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200813 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700814 struct request *req = bd->rq;
815 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600816 struct nvme_iod *iod;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200817 struct nvme_command cmnd;
818 int ret = BLK_MQ_RQ_QUEUE_OK;
Keith Buschedd10d32014-04-03 16:45:23 -0600819
Keith Busche1e5e562015-02-19 13:39:03 -0700820 /*
821 * If formated with metadata, require the block layer provide a buffer
822 * unless this namespace is formated such that the metadata can be
823 * stripped/generated by the controller with PRACT=1.
824 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200825 if (ns && ns->ms && !blk_integrity_rq(req)) {
Keith Busch71feb362015-06-19 11:07:30 -0600826 if (!(ns->pi_type && ns->ms == 8) &&
827 req->cmd_type != REQ_TYPE_DRV_PRIV) {
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200828 blk_mq_complete_request(req, -EFAULT);
Keith Busche1e5e562015-02-19 13:39:03 -0700829 return BLK_MQ_RQ_QUEUE_OK;
830 }
831 }
832
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200833 iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600834 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700835 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600836
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700837 if (req->cmd_flags & REQ_DISCARD) {
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200838 ret = nvme_setup_discard(nvmeq, ns, iod, &cmnd);
839 } else {
840 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
841 memcpy(&cmnd, req->cmd, sizeof(cmnd));
842 else if (req->cmd_flags & REQ_FLUSH)
843 nvme_setup_flush(ns, &cmnd);
844 else
845 nvme_setup_rw(ns, req, &cmnd);
Keith Buschedd10d32014-04-03 16:45:23 -0600846
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200847 if (req->nr_phys_segments)
848 ret = nvme_map_data(dev, iod, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700849 }
850
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200851 if (ret)
852 goto out;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700853
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200854 cmnd.common.command_id = req->tag;
855 nvme_set_info(cmd, iod, req_completion);
856
857 spin_lock_irq(&nvmeq->q_lock);
858 __nvme_submit_cmd(nvmeq, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700859 nvme_process_cq(nvmeq);
860 spin_unlock_irq(&nvmeq->q_lock);
861 return BLK_MQ_RQ_QUEUE_OK;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200862out:
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200863 nvme_free_iod(dev, iod);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200864 return ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500865}
866
Jens Axboea0fa9642015-11-03 20:37:26 -0700867static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500868{
Matthew Wilcox82123462011-01-20 13:24:06 -0500869 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500870
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500871 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500872 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500873
874 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400875 void *ctx;
876 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500877 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500878 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500879 break;
880 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
881 if (++head == nvmeq->q_depth) {
882 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500883 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500884 }
Jens Axboea0fa9642015-11-03 20:37:26 -0700885 if (tag && *tag == cqe.command_id)
886 *tag = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700887 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600888 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500889 }
890
891 /* If the controller ignores the cq head doorbell and continuously
892 * writes to the queue, it is theoretically possible to wrap around
893 * the queue twice and mistakenly return IRQ_NONE. Linux only
894 * requires that 0.1% of your interrupts are handled, so this isn't
895 * a big problem.
896 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500897 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Jens Axboea0fa9642015-11-03 20:37:26 -0700898 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500899
Keith Busch604e8c82015-11-20 08:38:13 -0700900 if (likely(nvmeq->cq_vector >= 0))
901 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500902 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500903 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500904
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400905 nvmeq->cqe_seen = 1;
Jens Axboea0fa9642015-11-03 20:37:26 -0700906}
907
908static void nvme_process_cq(struct nvme_queue *nvmeq)
909{
910 __nvme_process_cq(nvmeq, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500911}
912
913static irqreturn_t nvme_irq(int irq, void *data)
914{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500915 irqreturn_t result;
916 struct nvme_queue *nvmeq = data;
917 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400918 nvme_process_cq(nvmeq);
919 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
920 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500921 spin_unlock(&nvmeq->q_lock);
922 return result;
923}
924
925static irqreturn_t nvme_irq_check(int irq, void *data)
926{
927 struct nvme_queue *nvmeq = data;
928 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
929 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
930 return IRQ_NONE;
931 return IRQ_WAKE_THREAD;
932}
933
Jens Axboea0fa9642015-11-03 20:37:26 -0700934static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
935{
936 struct nvme_queue *nvmeq = hctx->driver_data;
937
938 if ((le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
939 nvmeq->cq_phase) {
940 spin_lock_irq(&nvmeq->q_lock);
941 __nvme_process_cq(nvmeq, &tag);
942 spin_unlock_irq(&nvmeq->q_lock);
943
944 if (tag == -1)
945 return 1;
946 }
947
948 return 0;
949}
950
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700951static int nvme_submit_async_admin_req(struct nvme_dev *dev)
952{
953 struct nvme_queue *nvmeq = dev->queues[0];
954 struct nvme_command c;
955 struct nvme_cmd_info *cmd_info;
956 struct request *req;
957
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100958 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100959 BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED);
Dan Carpenter9f173b32014-11-05 23:39:09 +0300960 if (IS_ERR(req))
961 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700962
Keith Buschc917dfe2015-01-07 18:55:48 -0700963 req->cmd_flags |= REQ_NO_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700964 cmd_info = blk_mq_rq_to_pdu(req);
Keith Busch1efccc92015-03-31 10:37:17 -0600965 nvme_set_info(cmd_info, NULL, async_req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700966
967 memset(&c, 0, sizeof(c));
968 c.common.opcode = nvme_admin_async_event;
969 c.common.command_id = req->tag;
970
Keith Busch42483222015-06-01 09:29:54 -0600971 blk_mq_free_request(req);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530972 __nvme_submit_cmd(nvmeq, &c);
973 return 0;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700974}
975
Christoph Hellwigd8f32162015-11-16 10:28:47 +0100976static void async_cmd_info_endio(struct request *req, int error)
Keith Busch4d115422013-12-10 13:10:40 -0700977{
Christoph Hellwigd8f32162015-11-16 10:28:47 +0100978 struct async_cmd_info *cmdinfo = req->end_io_data;
Keith Busch4d115422013-12-10 13:10:40 -0700979
Christoph Hellwigd8f32162015-11-16 10:28:47 +0100980 cmdinfo->status = req->errors;
981 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
982 blk_mq_free_request(req);
Keith Busch4d115422013-12-10 13:10:40 -0700983}
984
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500985static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
986{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500987 struct nvme_command c;
988
989 memset(&c, 0, sizeof(c));
990 c.delete_queue.opcode = opcode;
991 c.delete_queue.qid = cpu_to_le16(id);
992
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100993 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500994}
995
996static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
997 struct nvme_queue *nvmeq)
998{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500999 struct nvme_command c;
1000 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1001
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001002 /*
1003 * Note: we (ab)use the fact the the prp fields survive if no data
1004 * is attached to the request.
1005 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001006 memset(&c, 0, sizeof(c));
1007 c.create_cq.opcode = nvme_admin_create_cq;
1008 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1009 c.create_cq.cqid = cpu_to_le16(qid);
1010 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1011 c.create_cq.cq_flags = cpu_to_le16(flags);
1012 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1013
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001014 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001015}
1016
1017static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1018 struct nvme_queue *nvmeq)
1019{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001020 struct nvme_command c;
1021 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1022
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001023 /*
1024 * Note: we (ab)use the fact the the prp fields survive if no data
1025 * is attached to the request.
1026 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001027 memset(&c, 0, sizeof(c));
1028 c.create_sq.opcode = nvme_admin_create_sq;
1029 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1030 c.create_sq.sqid = cpu_to_le16(qid);
1031 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1032 c.create_sq.sq_flags = cpu_to_le16(flags);
1033 c.create_sq.cqid = cpu_to_le16(qid);
1034
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001035 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001036}
1037
1038static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1039{
1040 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1041}
1042
1043static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1044{
1045 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1046}
1047
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001048static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
Keith Buschc30341d2013-12-10 13:10:38 -07001049{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001050 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1051 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001052 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001053 struct request *abort_req;
1054 struct nvme_cmd_info *abort_cmd;
1055 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001056
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001057 /*
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001058 * Shutdown immediately if controller times out while starting. The
1059 * reset work will see the pci device disabled when it gets the forced
1060 * cancellation error. All outstanding requests are completed on
1061 * shutdown, so we return BLK_EH_HANDLED.
1062 */
1063 if (test_bit(NVME_CTRL_RESETTING, &dev->flags)) {
1064 dev_warn(dev->dev,
1065 "I/O %d QID %d timeout, disable controller\n",
1066 req->tag, nvmeq->qid);
1067 nvme_dev_shutdown(dev);
1068 req->errors = NVME_SC_CANCELLED;
1069 return BLK_EH_HANDLED;
1070 }
1071
1072 /*
1073 * Shutdown the controller immediately and schedule a reset if the
1074 * command was already aborted once before and still hasn't been
1075 * returned to the driver, or if this is the admin queue.
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001076 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001077 if (!nvmeq->qid || cmd_rq->aborted) {
Keith Busche1569a12015-11-26 12:11:07 +01001078 dev_warn(dev->dev,
1079 "I/O %d QID %d timeout, reset controller\n",
1080 req->tag, nvmeq->qid);
1081 nvme_dev_shutdown(dev);
1082 queue_work(nvme_workq, &dev->reset_work);
1083
1084 /*
1085 * Mark the request as handled, since the inline shutdown
1086 * forces all outstanding requests to complete.
1087 */
1088 req->errors = NVME_SC_CANCELLED;
1089 return BLK_EH_HANDLED;
Keith Buschc30341d2013-12-10 13:10:38 -07001090 }
1091
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001092 if (atomic_dec_and_test(&dev->ctrl.abort_limit))
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001093 return BLK_EH_RESET_TIMER;
Keith Buschc30341d2013-12-10 13:10:38 -07001094
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001095 abort_req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001096 BLK_MQ_REQ_NOWAIT);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001097 if (IS_ERR(abort_req)) {
1098 atomic_inc(&dev->ctrl.abort_limit);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001099 return BLK_EH_RESET_TIMER;
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001100 }
Keith Buschc30341d2013-12-10 13:10:38 -07001101
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001102 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1103 nvme_set_info(abort_cmd, abort_req, abort_completion);
1104
Keith Buschc30341d2013-12-10 13:10:38 -07001105 memset(&cmd, 0, sizeof(cmd));
1106 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001107 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001108 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001109 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001110
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001111 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001112
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001113 dev_warn(nvmeq->q_dmadev, "I/O %d QID %d timeout, aborting\n",
1114 req->tag, nvmeq->qid);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301115 nvme_submit_cmd(dev->queues[0], &cmd);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001116
1117 /*
1118 * The aborted req will be completed on receiving the abort req.
1119 * We enable the timer again. If hit twice, it'll cause a device reset,
1120 * as the device then is in a faulty state.
1121 */
1122 return BLK_EH_RESET_TIMER;
Keith Buschc30341d2013-12-10 13:10:38 -07001123}
1124
Keith Busch42483222015-06-01 09:29:54 -06001125static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001126{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001127 struct nvme_queue *nvmeq = data;
1128 void *ctx;
1129 nvme_completion_fn fn;
1130 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001131 struct nvme_completion cqe;
1132
1133 if (!blk_mq_request_started(req))
1134 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001135
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001136 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001137
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001138 if (cmd->ctx == CMD_CTX_CANCELLED)
1139 return;
1140
Keith Buschcef6a942015-01-07 18:55:51 -07001141 if (blk_queue_dying(req->q))
1142 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1143 else
1144 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1145
1146
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001147 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1148 req->tag, nvmeq->qid);
1149 ctx = cancel_cmd_info(cmd, &fn);
1150 fn(nvmeq, ctx, &cqe);
1151}
1152
Keith Buschf435c282014-07-07 09:14:42 -06001153static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001154{
1155 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1156 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001157 if (nvmeq->sq_cmds)
1158 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001159 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1160 kfree(nvmeq);
1161}
1162
Keith Buscha1a5ef92013-12-16 13:50:00 -05001163static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001164{
1165 int i;
1166
Keith Buscha1a5ef92013-12-16 13:50:00 -05001167 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001168 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001169 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001170 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001171 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001172 }
Keith Busch22404272013-07-15 15:02:20 -06001173}
1174
Keith Busch4d115422013-12-10 13:10:40 -07001175/**
1176 * nvme_suspend_queue - put queue into suspended state
1177 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001178 */
1179static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001180{
Keith Busch2b25d982014-12-22 12:59:04 -07001181 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001182
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001183 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001184 if (nvmeq->cq_vector == -1) {
1185 spin_unlock_irq(&nvmeq->q_lock);
1186 return 1;
1187 }
1188 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001189 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001190 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001191 spin_unlock_irq(&nvmeq->q_lock);
1192
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001193 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
1194 blk_mq_freeze_queue_start(nvmeq->dev->ctrl.admin_q);
Keith Busch6df3dbc2015-03-26 13:49:33 -06001195
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001196 irq_set_affinity_hint(vector, NULL);
1197 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001198
Keith Busch4d115422013-12-10 13:10:40 -07001199 return 0;
1200}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001201
Keith Busch4d115422013-12-10 13:10:40 -07001202static void nvme_clear_queue(struct nvme_queue *nvmeq)
1203{
Keith Busch22404272013-07-15 15:02:20 -06001204 spin_lock_irq(&nvmeq->q_lock);
Keith Busch42483222015-06-01 09:29:54 -06001205 if (nvmeq->tags && *nvmeq->tags)
1206 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001207 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001208}
1209
Keith Busch4d115422013-12-10 13:10:40 -07001210static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1211{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001212 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001213
1214 if (!nvmeq)
1215 return;
1216 if (nvme_suspend_queue(nvmeq))
1217 return;
1218
Keith Busch0e53d182013-12-10 13:10:39 -07001219 /* Don't tell the adapter to delete the admin queue.
1220 * Don't tell a removed adapter to delete IO queues. */
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001221 if (qid && readl(dev->bar + NVME_REG_CSTS) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001222 adapter_delete_sq(dev, qid);
1223 adapter_delete_cq(dev, qid);
1224 }
Keith Busch07836e62015-02-19 10:34:48 -07001225
1226 spin_lock_irq(&nvmeq->q_lock);
1227 nvme_process_cq(nvmeq);
1228 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001229}
1230
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001231static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1232 int entry_size)
1233{
1234 int q_depth = dev->q_depth;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001235 unsigned q_size_aligned = roundup(q_depth * entry_size,
1236 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001237
1238 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001239 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001240 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
Jon Derrickc45f5c92015-07-21 15:08:13 -06001241 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001242
1243 /*
1244 * Ensure the reduced q_depth is above some threshold where it
1245 * would be better to map queues in system memory with the
1246 * original depth
1247 */
1248 if (q_depth < 64)
1249 return -ENOMEM;
1250 }
1251
1252 return q_depth;
1253}
1254
1255static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1256 int qid, int depth)
1257{
1258 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001259 unsigned offset = (qid - 1) * roundup(SQ_SIZE(depth),
1260 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001261 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1262 nvmeq->sq_cmds_io = dev->cmb + offset;
1263 } else {
1264 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1265 &nvmeq->sq_dma_addr, GFP_KERNEL);
1266 if (!nvmeq->sq_cmds)
1267 return -ENOMEM;
1268 }
1269
1270 return 0;
1271}
1272
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001273static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001274 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001275{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001276 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001277 if (!nvmeq)
1278 return NULL;
1279
Christoph Hellwige75ec752015-05-22 11:12:39 +02001280 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001281 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001282 if (!nvmeq->cqes)
1283 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001284
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001285 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001286 goto free_cqdma;
1287
Christoph Hellwige75ec752015-05-22 11:12:39 +02001288 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001289 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001290 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001291 dev->ctrl.instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001292 spin_lock_init(&nvmeq->q_lock);
1293 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001294 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001295 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001296 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001297 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001298 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001299 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001300
Jon Derrick36a7e992015-05-27 12:26:23 -06001301 /* make sure queue descriptor is set before queue count, for kthread */
1302 mb();
1303 dev->queue_count++;
1304
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001305 return nvmeq;
1306
1307 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001308 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001309 nvmeq->cq_dma_addr);
1310 free_nvmeq:
1311 kfree(nvmeq);
1312 return NULL;
1313}
1314
Matthew Wilcox30010822011-01-20 09:10:15 -05001315static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1316 const char *name)
1317{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001318 if (use_threaded_interrupts)
1319 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001320 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001321 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001322 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001323 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001324}
1325
Keith Busch22404272013-07-15 15:02:20 -06001326static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001327{
Keith Busch22404272013-07-15 15:02:20 -06001328 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001329
Keith Busch7be50e92014-09-10 15:48:47 -06001330 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001331 nvmeq->sq_tail = 0;
1332 nvmeq->cq_head = 0;
1333 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001334 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001335 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001336 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001337 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001338}
1339
1340static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1341{
1342 struct nvme_dev *dev = nvmeq->dev;
1343 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001344
Keith Busch2b25d982014-12-22 12:59:04 -07001345 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001346 result = adapter_alloc_cq(dev, qid, nvmeq);
1347 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001348 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001349
1350 result = adapter_alloc_sq(dev, qid, nvmeq);
1351 if (result < 0)
1352 goto release_cq;
1353
Matthew Wilcox3193f072014-01-27 15:57:22 -05001354 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001355 if (result < 0)
1356 goto release_sq;
1357
Keith Busch22404272013-07-15 15:02:20 -06001358 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001359 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001360
1361 release_sq:
1362 adapter_delete_sq(dev, qid);
1363 release_cq:
1364 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001365 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001366}
1367
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001368static struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001369 .queue_rq = nvme_queue_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001370 .map_queue = blk_mq_map_queue,
1371 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001372 .exit_hctx = nvme_admin_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001373 .init_request = nvme_admin_init_request,
1374 .timeout = nvme_timeout,
1375};
1376
1377static struct blk_mq_ops nvme_mq_ops = {
1378 .queue_rq = nvme_queue_rq,
1379 .map_queue = blk_mq_map_queue,
1380 .init_hctx = nvme_init_hctx,
1381 .init_request = nvme_init_request,
1382 .timeout = nvme_timeout,
Jens Axboea0fa9642015-11-03 20:37:26 -07001383 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001384};
1385
Keith Buschea191d22015-01-07 18:55:49 -07001386static void nvme_dev_remove_admin(struct nvme_dev *dev)
1387{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001388 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
1389 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001390 blk_mq_free_tag_set(&dev->admin_tagset);
1391 }
1392}
1393
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001394static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1395{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001396 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001397 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1398 dev->admin_tagset.nr_hw_queues = 1;
Christoph Hellwig46800722015-11-16 12:40:02 +01001399 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH;
Keith Busch1efccc92015-03-31 10:37:17 -06001400 dev->admin_tagset.reserved_tags = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001401 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001402 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001403 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001404 dev->admin_tagset.driver_data = dev;
1405
1406 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1407 return -ENOMEM;
1408
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001409 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1410 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001411 blk_mq_free_tag_set(&dev->admin_tagset);
1412 return -ENOMEM;
1413 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001414 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001415 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001416 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001417 return -ENODEV;
1418 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001419 } else
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001420 blk_mq_unfreeze_queue(dev->ctrl.admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001421
1422 return 0;
1423}
1424
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001425static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001426{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001427 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001428 u32 aqa;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001429 u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001430 struct nvme_queue *nvmeq;
1431
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001432 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1) ?
Keith Buschdfbac8c2015-08-10 15:20:40 -06001433 NVME_CAP_NSSRC(cap) : 0;
1434
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001435 if (dev->subsystem &&
1436 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1437 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001438
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001439 result = nvme_disable_ctrl(&dev->ctrl, cap);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001440 if (result < 0)
1441 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001442
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001443 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001444 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001445 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001446 if (!nvmeq)
1447 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001448 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001449
1450 aqa = nvmeq->q_depth - 1;
1451 aqa |= aqa << 16;
1452
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001453 writel(aqa, dev->bar + NVME_REG_AQA);
1454 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1455 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001456
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001457 result = nvme_enable_ctrl(&dev->ctrl, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001458 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001459 goto free_nvmeq;
1460
Keith Busch2b25d982014-12-22 12:59:04 -07001461 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001462 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001463 if (result) {
1464 nvmeq->cq_vector = -1;
Keith Busch0fb59cb2015-01-07 18:55:50 -07001465 goto free_nvmeq;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001466 }
Keith Busch025c5572013-05-01 13:07:51 -06001467
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001468 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001469
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001470 free_nvmeq:
1471 nvme_free_queues(dev, 0);
1472 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001473}
1474
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001475static int nvme_kthread(void *data)
1476{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001477 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001478
1479 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001480 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001481 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001482 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001483 int i;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001484 u32 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001485
Christoph Hellwig846cc052015-11-26 12:10:29 +01001486 /*
1487 * Skip controllers currently under reset.
1488 */
1489 if (work_pending(&dev->reset_work) || work_busy(&dev->reset_work))
1490 continue;
1491
Keith Buschdfbac8c2015-08-10 15:20:40 -06001492 if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
1493 csts & NVME_CSTS_CFS) {
Christoph Hellwig846cc052015-11-26 12:10:29 +01001494 if (queue_work(nvme_workq, &dev->reset_work)) {
Christoph Hellwig906678922015-10-02 18:49:23 +02001495 dev_warn(dev->dev,
1496 "Failed status: %x, reset controller\n",
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001497 readl(dev->bar + NVME_REG_CSTS));
Christoph Hellwig906678922015-10-02 18:49:23 +02001498 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07001499 continue;
1500 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001501 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001502 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001503 if (!nvmeq)
1504 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001505 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001506 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06001507
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001508 while (i == 0 && dev->ctrl.event_limit > 0) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001509 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06001510 break;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001511 dev->ctrl.event_limit--;
Keith Busch6fccf932014-06-18 13:58:57 -06001512 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001513 spin_unlock_irq(&nvmeq->q_lock);
1514 }
1515 }
1516 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001517 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001518 }
1519 return 0;
1520}
1521
Christoph Hellwig749941f2015-11-26 11:46:39 +01001522static int nvme_create_io_queues(struct nvme_dev *dev)
Keith Busch42f61422014-03-24 10:46:25 -06001523{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001524 unsigned i;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001525 int ret = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001526
Christoph Hellwig749941f2015-11-26 11:46:39 +01001527 for (i = dev->queue_count; i <= dev->max_qid; i++) {
1528 if (!nvme_alloc_queue(dev, i, dev->q_depth)) {
1529 ret = -ENOMEM;
Keith Busch42f61422014-03-24 10:46:25 -06001530 break;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001531 }
1532 }
Keith Busch42f61422014-03-24 10:46:25 -06001533
Christoph Hellwig749941f2015-11-26 11:46:39 +01001534 for (i = dev->online_queues; i <= dev->queue_count - 1; i++) {
1535 ret = nvme_create_queue(dev->queues[i], i);
1536 if (ret) {
Christoph Hellwig2659e572015-10-02 18:51:31 +02001537 nvme_free_queues(dev, i);
Keith Busch42f61422014-03-24 10:46:25 -06001538 break;
Christoph Hellwig2659e572015-10-02 18:51:31 +02001539 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001540 }
1541
1542 /*
1543 * Ignore failing Create SQ/CQ commands, we can continue with less
1544 * than the desired aount of queues, and even a controller without
1545 * I/O queues an still be used to issue admin commands. This might
1546 * be useful to upgrade a buggy firmware for example.
1547 */
1548 return ret >= 0 ? 0 : ret;
Keith Busch42f61422014-03-24 10:46:25 -06001549}
1550
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001551static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1552{
1553 u64 szu, size, offset;
1554 u32 cmbloc;
1555 resource_size_t bar_size;
1556 struct pci_dev *pdev = to_pci_dev(dev->dev);
1557 void __iomem *cmb;
1558 dma_addr_t dma_addr;
1559
1560 if (!use_cmb_sqes)
1561 return NULL;
1562
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001563 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001564 if (!(NVME_CMB_SZ(dev->cmbsz)))
1565 return NULL;
1566
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001567 cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001568
1569 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1570 size = szu * NVME_CMB_SZ(dev->cmbsz);
1571 offset = szu * NVME_CMB_OFST(cmbloc);
1572 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
1573
1574 if (offset > bar_size)
1575 return NULL;
1576
1577 /*
1578 * Controllers may support a CMB size larger than their BAR,
1579 * for example, due to being behind a bridge. Reduce the CMB to
1580 * the reported size of the BAR
1581 */
1582 if (size > bar_size - offset)
1583 size = bar_size - offset;
1584
1585 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
1586 cmb = ioremap_wc(dma_addr, size);
1587 if (!cmb)
1588 return NULL;
1589
1590 dev->cmb_dma_addr = dma_addr;
1591 dev->cmb_size = size;
1592 return cmb;
1593}
1594
1595static inline void nvme_release_cmb(struct nvme_dev *dev)
1596{
1597 if (dev->cmb) {
1598 iounmap(dev->cmb);
1599 dev->cmb = NULL;
1600 }
1601}
1602
Keith Busch9d713c22013-07-15 15:02:24 -06001603static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1604{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001605 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001606}
1607
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001608static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001609{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001610 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02001611 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch42f61422014-03-24 10:46:25 -06001612 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001613
Keith Busch42f61422014-03-24 10:46:25 -06001614 nr_io_queues = num_possible_cpus();
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001615 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
1616 if (result < 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001617 return result;
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001618
1619 /*
1620 * Degraded controllers might return an error when setting the queue
1621 * count. We still want to be able to bring them online and offer
1622 * access to the admin queue, as that might be only way to fix them up.
1623 */
1624 if (result > 0) {
1625 dev_err(dev->dev, "Could not set queue count (%d)\n", result);
1626 nr_io_queues = 0;
1627 result = 0;
1628 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001629
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001630 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1631 result = nvme_cmb_qdepth(dev, nr_io_queues,
1632 sizeof(struct nvme_command));
1633 if (result > 0)
1634 dev->q_depth = result;
1635 else
1636 nvme_release_cmb(dev);
1637 }
1638
Keith Busch9d713c22013-07-15 15:02:24 -06001639 size = db_bar_size(dev, nr_io_queues);
1640 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001641 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001642 do {
1643 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1644 if (dev->bar)
1645 break;
1646 if (!--nr_io_queues)
1647 return -ENOMEM;
1648 size = db_bar_size(dev, nr_io_queues);
1649 } while (1);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001650 dev->dbs = dev->bar + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07001651 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001652 }
1653
Keith Busch9d713c22013-07-15 15:02:24 -06001654 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05001655 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001656
Jens Axboee32efbf2014-11-14 09:49:26 -07001657 /*
1658 * If we enable msix early due to not intx, disable it again before
1659 * setting up the full range we need.
1660 */
1661 if (!pdev->irq)
1662 pci_disable_msix(pdev);
1663
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001664 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001665 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001666 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
1667 if (vecs < 0) {
1668 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
1669 if (vecs < 0) {
1670 vecs = 1;
1671 } else {
1672 for (i = 0; i < vecs; i++)
1673 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001674 }
1675 }
1676
Matthew Wilcox063a8092013-06-20 10:53:48 -04001677 /*
1678 * Should investigate if there's a performance win from allocating
1679 * more queues than interrupt vectors; it might allow the submission
1680 * path to scale better, even if the receive path is limited by the
1681 * number of interrupts.
1682 */
1683 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06001684 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001685
Matthew Wilcox3193f072014-01-27 15:57:22 -05001686 result = queue_request_irq(dev, adminq, adminq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001687 if (result) {
1688 adminq->cq_vector = -1;
Keith Busch22404272013-07-15 15:02:20 -06001689 goto free_queues;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001690 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001691
Keith Buschcd638942013-07-15 15:02:23 -06001692 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06001693 nvme_free_queues(dev, nr_io_queues + 1);
Christoph Hellwig749941f2015-11-26 11:46:39 +01001694 return nvme_create_io_queues(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001695
Keith Busch22404272013-07-15 15:02:20 -06001696 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05001697 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06001698 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001699}
1700
Keith Buschbda4e0f2015-09-03 08:18:17 -06001701static void nvme_set_irq_hints(struct nvme_dev *dev)
1702{
1703 struct nvme_queue *nvmeq;
1704 int i;
1705
1706 for (i = 0; i < dev->online_queues; i++) {
1707 nvmeq = dev->queues[i];
1708
1709 if (!nvmeq->tags || !(*nvmeq->tags))
1710 continue;
1711
1712 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
1713 blk_mq_tags_cpumask(*nvmeq->tags));
1714 }
1715}
1716
Keith Buscha5768aa2015-06-01 14:28:14 -06001717static void nvme_dev_scan(struct work_struct *work)
1718{
1719 struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06001720
1721 if (!dev->tagset.tags)
1722 return;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001723 nvme_scan_namespaces(&dev->ctrl);
Keith Buschbda4e0f2015-09-03 08:18:17 -06001724 nvme_set_irq_hints(dev);
Keith Buscha5768aa2015-06-01 14:28:14 -06001725}
1726
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001727/*
1728 * Return: error value if an error occurred setting up the queues or calling
1729 * Identify Device. 0 if these succeeded, even if adding some of the
1730 * namespaces failed. At the moment, these failures are silent. TBD which
1731 * failures should be reported.
1732 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001733static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001734{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001735 if (!dev->ctrl.tagset) {
Keith Buschffe77042015-06-08 10:08:15 -06001736 dev->tagset.ops = &nvme_mq_ops;
1737 dev->tagset.nr_hw_queues = dev->online_queues - 1;
1738 dev->tagset.timeout = NVME_IO_TIMEOUT;
1739 dev->tagset.numa_node = dev_to_node(dev->dev);
1740 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001741 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06001742 dev->tagset.cmd_size = nvme_cmd_size(dev);
1743 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
1744 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001745
Keith Buschffe77042015-06-08 10:08:15 -06001746 if (blk_mq_alloc_tag_set(&dev->tagset))
1747 return 0;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001748 dev->ctrl.tagset = &dev->tagset;
Keith Buschffe77042015-06-08 10:08:15 -06001749 }
Keith Busch92f7a162015-10-23 11:42:02 -06001750 queue_work(nvme_workq, &dev->scan_work);
Keith Busche1e5e562015-02-19 13:39:03 -07001751 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001752}
1753
Keith Busch0877cb02013-07-15 15:02:19 -06001754static int nvme_dev_map(struct nvme_dev *dev)
1755{
Keith Busch42f61422014-03-24 10:46:25 -06001756 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06001757 int bars, result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001758 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001759
1760 if (pci_enable_device_mem(pdev))
1761 return result;
1762
1763 dev->entry[0].vector = pdev->irq;
1764 pci_set_master(pdev);
1765 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07001766 if (!bars)
1767 goto disable_pci;
1768
Keith Busch0877cb02013-07-15 15:02:19 -06001769 if (pci_request_selected_regions(pdev, bars, "nvme"))
1770 goto disable_pci;
1771
Christoph Hellwige75ec752015-05-22 11:12:39 +02001772 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
1773 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01001774 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06001775
Keith Busch0877cb02013-07-15 15:02:19 -06001776 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1777 if (!dev->bar)
1778 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07001779
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001780 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07001781 result = -ENODEV;
1782 goto unmap;
1783 }
Jens Axboee32efbf2014-11-14 09:49:26 -07001784
1785 /*
1786 * Some devices don't advertse INTx interrupts, pre-enable a single
1787 * MSIX vec for setup. We'll adjust this later.
1788 */
1789 if (!pdev->irq) {
1790 result = pci_enable_msix(pdev, dev->entry, 1);
1791 if (result < 0)
1792 goto unmap;
1793 }
1794
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001795 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
1796
Keith Busch42f61422014-03-24 10:46:25 -06001797 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
1798 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001799 dev->dbs = dev->bar + 4096;
1800 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2))
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001801 dev->cmb = nvme_map_cmb(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001802
1803 return 0;
1804
Keith Busch0e53d182013-12-10 13:10:39 -07001805 unmap:
1806 iounmap(dev->bar);
1807 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06001808 disable:
1809 pci_release_regions(pdev);
1810 disable_pci:
1811 pci_disable_device(pdev);
1812 return result;
1813}
1814
1815static void nvme_dev_unmap(struct nvme_dev *dev)
1816{
Christoph Hellwige75ec752015-05-22 11:12:39 +02001817 struct pci_dev *pdev = to_pci_dev(dev->dev);
1818
1819 if (pdev->msi_enabled)
1820 pci_disable_msi(pdev);
1821 else if (pdev->msix_enabled)
1822 pci_disable_msix(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001823
1824 if (dev->bar) {
1825 iounmap(dev->bar);
1826 dev->bar = NULL;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001827 pci_release_regions(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001828 }
1829
Christoph Hellwige75ec752015-05-22 11:12:39 +02001830 if (pci_is_enabled(pdev))
1831 pci_disable_device(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001832}
1833
Keith Busch4d115422013-12-10 13:10:40 -07001834struct nvme_delq_ctx {
1835 struct task_struct *waiter;
1836 struct kthread_worker *worker;
1837 atomic_t refcount;
1838};
1839
1840static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
1841{
1842 dq->waiter = current;
1843 mb();
1844
1845 for (;;) {
1846 set_current_state(TASK_KILLABLE);
1847 if (!atomic_read(&dq->refcount))
1848 break;
1849 if (!schedule_timeout(ADMIN_TIMEOUT) ||
1850 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07001851 /*
1852 * Disable the controller first since we can't trust it
1853 * at this point, but leave the admin queue enabled
1854 * until all queue deletion requests are flushed.
1855 * FIXME: This may take a while if there are more h/w
1856 * queues than admin tags.
1857 */
Keith Busch4d115422013-12-10 13:10:40 -07001858 set_current_state(TASK_RUNNING);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001859 nvme_disable_ctrl(&dev->ctrl,
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001860 lo_hi_readq(dev->bar + NVME_REG_CAP));
Keith Busch0fb59cb2015-01-07 18:55:50 -07001861 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07001862 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07001863 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07001864 return;
1865 }
1866 }
1867 set_current_state(TASK_RUNNING);
1868}
1869
1870static void nvme_put_dq(struct nvme_delq_ctx *dq)
1871{
1872 atomic_dec(&dq->refcount);
1873 if (dq->waiter)
1874 wake_up_process(dq->waiter);
1875}
1876
1877static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
1878{
1879 atomic_inc(&dq->refcount);
1880 return dq;
1881}
1882
1883static void nvme_del_queue_end(struct nvme_queue *nvmeq)
1884{
1885 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
Keith Busch4d115422013-12-10 13:10:40 -07001886 nvme_put_dq(dq);
Keith Busch604e8c82015-11-20 08:38:13 -07001887
1888 spin_lock_irq(&nvmeq->q_lock);
1889 nvme_process_cq(nvmeq);
1890 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001891}
1892
1893static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
1894 kthread_work_func_t fn)
1895{
Christoph Hellwigd8f32162015-11-16 10:28:47 +01001896 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -07001897 struct nvme_command c;
1898
1899 memset(&c, 0, sizeof(c));
1900 c.delete_queue.opcode = opcode;
1901 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
1902
1903 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Christoph Hellwigd8f32162015-11-16 10:28:47 +01001904
1905 req = nvme_alloc_request(nvmeq->dev->ctrl.admin_q, &c, 0);
1906 if (IS_ERR(req))
1907 return PTR_ERR(req);
1908
1909 req->timeout = ADMIN_TIMEOUT;
1910 req->end_io_data = &nvmeq->cmdinfo;
1911 blk_execute_rq_nowait(req->q, NULL, req, 0, async_cmd_info_endio);
1912 return 0;
Keith Busch4d115422013-12-10 13:10:40 -07001913}
1914
1915static void nvme_del_cq_work_handler(struct kthread_work *work)
1916{
1917 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1918 cmdinfo.work);
1919 nvme_del_queue_end(nvmeq);
1920}
1921
1922static int nvme_delete_cq(struct nvme_queue *nvmeq)
1923{
1924 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
1925 nvme_del_cq_work_handler);
1926}
1927
1928static void nvme_del_sq_work_handler(struct kthread_work *work)
1929{
1930 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1931 cmdinfo.work);
1932 int status = nvmeq->cmdinfo.status;
1933
1934 if (!status)
1935 status = nvme_delete_cq(nvmeq);
1936 if (status)
1937 nvme_del_queue_end(nvmeq);
1938}
1939
1940static int nvme_delete_sq(struct nvme_queue *nvmeq)
1941{
1942 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
1943 nvme_del_sq_work_handler);
1944}
1945
1946static void nvme_del_queue_start(struct kthread_work *work)
1947{
1948 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1949 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07001950 if (nvme_delete_sq(nvmeq))
1951 nvme_del_queue_end(nvmeq);
1952}
1953
1954static void nvme_disable_io_queues(struct nvme_dev *dev)
1955{
1956 int i;
1957 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
1958 struct nvme_delq_ctx dq;
1959 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001960 &worker, "nvme%d", dev->ctrl.instance);
Keith Busch4d115422013-12-10 13:10:40 -07001961
1962 if (IS_ERR(kworker_task)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001963 dev_err(dev->dev,
Keith Busch4d115422013-12-10 13:10:40 -07001964 "Failed to create queue del task\n");
1965 for (i = dev->queue_count - 1; i > 0; i--)
1966 nvme_disable_queue(dev, i);
1967 return;
1968 }
1969
1970 dq.waiter = NULL;
1971 atomic_set(&dq.refcount, 0);
1972 dq.worker = &worker;
1973 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001974 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07001975
1976 if (nvme_suspend_queue(nvmeq))
1977 continue;
1978 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
1979 nvmeq->cmdinfo.worker = dq.worker;
1980 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
1981 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
1982 }
1983 nvme_wait_dq(&dq, dev);
1984 kthread_stop(kworker_task);
1985}
1986
Christoph Hellwig73850142015-10-22 14:03:33 +02001987static int nvme_dev_list_add(struct nvme_dev *dev)
1988{
1989 bool start_thread = false;
1990
1991 spin_lock(&dev_list_lock);
1992 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
1993 start_thread = true;
1994 nvme_thread = NULL;
1995 }
1996 list_add(&dev->node, &dev_list);
1997 spin_unlock(&dev_list_lock);
1998
1999 if (start_thread) {
2000 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2001 wake_up_all(&nvme_kthread_wait);
2002 } else
2003 wait_event_killable(nvme_kthread_wait, nvme_thread);
2004
2005 if (IS_ERR_OR_NULL(nvme_thread))
2006 return nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2007
2008 return 0;
2009}
2010
Dan McLeranb9afca32014-04-07 17:10:11 -06002011/*
2012* Remove the node from the device list and check
2013* for whether or not we need to stop the nvme_thread.
2014*/
2015static void nvme_dev_list_remove(struct nvme_dev *dev)
2016{
2017 struct task_struct *tmp = NULL;
2018
2019 spin_lock(&dev_list_lock);
2020 list_del_init(&dev->node);
2021 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2022 tmp = nvme_thread;
2023 nvme_thread = NULL;
2024 }
2025 spin_unlock(&dev_list_lock);
2026
2027 if (tmp)
2028 kthread_stop(tmp);
2029}
2030
Keith Buschc9d3bf82015-01-07 18:55:52 -07002031static void nvme_freeze_queues(struct nvme_dev *dev)
2032{
2033 struct nvme_ns *ns;
2034
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002035 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
Keith Buschc9d3bf82015-01-07 18:55:52 -07002036 blk_mq_freeze_queue_start(ns->queue);
2037
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002038 spin_lock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002039 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002040 spin_unlock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002041
2042 blk_mq_cancel_requeue_work(ns->queue);
2043 blk_mq_stop_hw_queues(ns->queue);
2044 }
2045}
2046
2047static void nvme_unfreeze_queues(struct nvme_dev *dev)
2048{
2049 struct nvme_ns *ns;
2050
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002051 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
Keith Buschc9d3bf82015-01-07 18:55:52 -07002052 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2053 blk_mq_unfreeze_queue(ns->queue);
2054 blk_mq_start_stopped_hw_queues(ns->queue, true);
2055 blk_mq_kick_requeue_list(ns->queue);
2056 }
2057}
2058
Keith Buschf0b50732013-07-15 15:02:21 -06002059static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002060{
Keith Busch22404272013-07-15 15:02:20 -06002061 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002062 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002063
Dan McLeranb9afca32014-04-07 17:10:11 -06002064 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002065
Keith Busch77bf25e2015-11-26 12:21:29 +01002066 mutex_lock(&dev->shutdown_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002067 if (dev->bar) {
2068 nvme_freeze_queues(dev);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002069 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002070 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002071 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002072 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002073 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002074 nvme_suspend_queue(nvmeq);
Keith Busch4d115422013-12-10 13:10:40 -07002075 }
2076 } else {
2077 nvme_disable_io_queues(dev);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002078 nvme_shutdown_ctrl(&dev->ctrl);
Keith Busch4d115422013-12-10 13:10:40 -07002079 nvme_disable_queue(dev, 0);
2080 }
Keith Buschf0b50732013-07-15 15:02:21 -06002081 nvme_dev_unmap(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002082
2083 for (i = dev->queue_count - 1; i >= 0; i--)
2084 nvme_clear_queue(dev->queues[i]);
Keith Busch77bf25e2015-11-26 12:21:29 +01002085 mutex_unlock(&dev->shutdown_lock);
Keith Buschf0b50732013-07-15 15:02:21 -06002086}
2087
Matthew Wilcox091b6092011-02-10 09:56:01 -05002088static int nvme_setup_prp_pools(struct nvme_dev *dev)
2089{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002090 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002091 PAGE_SIZE, PAGE_SIZE, 0);
2092 if (!dev->prp_page_pool)
2093 return -ENOMEM;
2094
Matthew Wilcox99802a72011-02-10 10:30:34 -05002095 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002096 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002097 256, 256, 0);
2098 if (!dev->prp_small_pool) {
2099 dma_pool_destroy(dev->prp_page_pool);
2100 return -ENOMEM;
2101 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002102 return 0;
2103}
2104
2105static void nvme_release_prp_pools(struct nvme_dev *dev)
2106{
2107 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002108 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002109}
2110
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002111static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
Keith Busch5e82e952013-02-19 10:17:58 -07002112{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002113 struct nvme_dev *dev = to_nvme_dev(ctrl);
Keith Busch9ac27092014-01-31 16:53:39 -07002114
Christoph Hellwige75ec752015-05-22 11:12:39 +02002115 put_device(dev->dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002116 if (dev->tagset.tags)
2117 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002118 if (dev->ctrl.admin_q)
2119 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002120 kfree(dev->queues);
2121 kfree(dev->entry);
2122 kfree(dev);
2123}
2124
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002125static void nvme_reset_work(struct work_struct *work)
Keith Buschf0b50732013-07-15 15:02:21 -06002126{
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002127 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002128 int result;
Keith Buschf0b50732013-07-15 15:02:21 -06002129
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002130 if (WARN_ON(test_bit(NVME_CTRL_RESETTING, &dev->flags)))
2131 goto out;
2132
2133 /*
2134 * If we're called to reset a live controller first shut it down before
2135 * moving on.
2136 */
2137 if (dev->bar)
2138 nvme_dev_shutdown(dev);
2139
2140 set_bit(NVME_CTRL_RESETTING, &dev->flags);
2141
Keith Buschf0b50732013-07-15 15:02:21 -06002142 result = nvme_dev_map(dev);
2143 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002144 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002145
2146 result = nvme_configure_admin_queue(dev);
2147 if (result)
2148 goto unmap;
2149
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002150 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002151 result = nvme_alloc_admin_tags(dev);
2152 if (result)
2153 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06002154
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002155 result = nvme_init_identify(&dev->ctrl);
2156 if (result)
2157 goto free_tags;
2158
Keith Buschf0b50732013-07-15 15:02:21 -06002159 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002160 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07002161 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06002162
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002163 dev->ctrl.event_limit = 1;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002164
Christoph Hellwig73850142015-10-22 14:03:33 +02002165 result = nvme_dev_list_add(dev);
2166 if (result)
2167 goto remove;
2168
Christoph Hellwig2659e572015-10-02 18:51:31 +02002169 /*
2170 * Keep the controller around but remove all namespaces if we don't have
2171 * any working I/O queue.
2172 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002173 if (dev->online_queues < 2) {
2174 dev_warn(dev->dev, "IO queues not created\n");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002175 nvme_remove_namespaces(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002176 } else {
2177 nvme_unfreeze_queues(dev);
2178 nvme_dev_add(dev);
2179 }
2180
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002181 clear_bit(NVME_CTRL_RESETTING, &dev->flags);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002182 return;
Keith Buschf0b50732013-07-15 15:02:21 -06002183
Christoph Hellwig73850142015-10-22 14:03:33 +02002184 remove:
2185 nvme_dev_list_remove(dev);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002186 free_tags:
2187 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002188 blk_put_queue(dev->ctrl.admin_q);
2189 dev->ctrl.admin_q = NULL;
Keith Busch4af0e212015-06-08 10:08:13 -06002190 dev->queues[0]->tags = NULL;
Keith Buschf0b50732013-07-15 15:02:21 -06002191 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002192 nvme_disable_queue(dev, 0);
Keith Buschf0b50732013-07-15 15:02:21 -06002193 unmap:
2194 nvme_dev_unmap(dev);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002195 out:
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002196 nvme_remove_dead_ctrl(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002197}
2198
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002199static void nvme_remove_dead_ctrl_work(struct work_struct *work)
Keith Busch9a6b9452013-12-10 13:10:36 -07002200{
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002201 struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002202 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002203
2204 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002205 pci_stop_and_remove_bus_device_locked(pdev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002206 nvme_put_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002207}
2208
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002209static void nvme_remove_dead_ctrl(struct nvme_dev *dev)
Keith Buschde3eff22015-06-18 13:36:39 -06002210{
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002211 dev_warn(dev->dev, "Removing after probe failure\n");
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002212 kref_get(&dev->ctrl.kref);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002213 if (!schedule_work(&dev->remove_work))
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002214 nvme_put_ctrl(&dev->ctrl);
Keith Buschde3eff22015-06-18 13:36:39 -06002215}
2216
Keith Busch4cc06522015-06-05 10:30:08 -06002217static int nvme_reset(struct nvme_dev *dev)
2218{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002219 if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q))
Keith Busch4cc06522015-06-05 10:30:08 -06002220 return -ENODEV;
2221
Christoph Hellwig846cc052015-11-26 12:10:29 +01002222 if (!queue_work(nvme_workq, &dev->reset_work))
2223 return -EBUSY;
Keith Busch4cc06522015-06-05 10:30:08 -06002224
Christoph Hellwig846cc052015-11-26 12:10:29 +01002225 flush_work(&dev->reset_work);
Christoph Hellwig846cc052015-11-26 12:10:29 +01002226 return 0;
Keith Busch4cc06522015-06-05 10:30:08 -06002227}
2228
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002229static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
2230{
2231 *val = readl(to_nvme_dev(ctrl)->bar + off);
2232 return 0;
2233}
2234
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002235static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2236{
2237 writel(val, to_nvme_dev(ctrl)->bar + off);
2238 return 0;
2239}
2240
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002241static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2242{
2243 *val = readq(to_nvme_dev(ctrl)->bar + off);
2244 return 0;
2245}
2246
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002247static bool nvme_pci_io_incapable(struct nvme_ctrl *ctrl)
2248{
2249 struct nvme_dev *dev = to_nvme_dev(ctrl);
2250
2251 return !dev->bar || dev->online_queues < 2;
2252}
2253
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002254static int nvme_pci_reset_ctrl(struct nvme_ctrl *ctrl)
2255{
2256 return nvme_reset(to_nvme_dev(ctrl));
2257}
2258
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002259static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
2260 .reg_read32 = nvme_pci_reg_read32,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002261 .reg_write32 = nvme_pci_reg_write32,
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002262 .reg_read64 = nvme_pci_reg_read64,
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002263 .io_incapable = nvme_pci_io_incapable,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002264 .reset_ctrl = nvme_pci_reset_ctrl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002265 .free_ctrl = nvme_pci_free_ctrl,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002266};
2267
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002268static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002269{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002270 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002271 struct nvme_dev *dev;
2272
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002273 node = dev_to_node(&pdev->dev);
2274 if (node == NUMA_NO_NODE)
2275 set_dev_node(&pdev->dev, 0);
2276
2277 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002278 if (!dev)
2279 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002280 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2281 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002282 if (!dev->entry)
2283 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002284 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2285 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002286 if (!dev->queues)
2287 goto free;
2288
Christoph Hellwige75ec752015-05-22 11:12:39 +02002289 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002290 pci_set_drvdata(pdev, dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002291
Keith Busche6e96d72015-03-23 09:32:37 -06002292 INIT_LIST_HEAD(&dev->node);
Keith Buscha5768aa2015-06-01 14:28:14 -06002293 INIT_WORK(&dev->scan_work, nvme_dev_scan);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002294 INIT_WORK(&dev->reset_work, nvme_reset_work);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002295 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
Keith Busch77bf25e2015-11-26 12:21:29 +01002296 mutex_init(&dev->shutdown_lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002297
2298 result = nvme_setup_prp_pools(dev);
2299 if (result)
2300 goto put_pci;
2301
2302 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
2303 id->driver_data);
2304 if (result)
2305 goto release_pools;
2306
Keith Busch92f7a162015-10-23 11:42:02 -06002307 queue_work(nvme_workq, &dev->reset_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002308 return 0;
2309
Keith Busch0877cb02013-07-15 15:02:19 -06002310 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002311 nvme_release_prp_pools(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002312 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002313 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002314 free:
2315 kfree(dev->queues);
2316 kfree(dev->entry);
2317 kfree(dev);
2318 return result;
2319}
2320
Keith Buschf0d54a52014-05-02 10:40:43 -06002321static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2322{
Keith Buscha6739472014-06-23 16:03:21 -06002323 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002324
Keith Buscha6739472014-06-23 16:03:21 -06002325 if (prepare)
2326 nvme_dev_shutdown(dev);
2327 else
Keith Busch92f7a162015-10-23 11:42:02 -06002328 queue_work(nvme_workq, &dev->reset_work);
Keith Buschf0d54a52014-05-02 10:40:43 -06002329}
2330
Keith Busch09ece142014-01-27 11:29:40 -05002331static void nvme_shutdown(struct pci_dev *pdev)
2332{
2333 struct nvme_dev *dev = pci_get_drvdata(pdev);
2334 nvme_dev_shutdown(dev);
2335}
2336
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002337static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002338{
2339 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002340
2341 spin_lock(&dev_list_lock);
2342 list_del_init(&dev->node);
2343 spin_unlock(&dev_list_lock);
2344
2345 pci_set_drvdata(pdev, NULL);
2346 flush_work(&dev->reset_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06002347 flush_work(&dev->scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002348 nvme_remove_namespaces(&dev->ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002349 nvme_uninit_ctrl(&dev->ctrl);
Keith Busch3399a3f2015-06-18 13:36:40 -06002350 nvme_dev_shutdown(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002351 nvme_dev_remove_admin(dev);
2352 nvme_free_queues(dev, 0);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002353 nvme_release_cmb(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002354 nvme_release_prp_pools(dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002355 nvme_put_ctrl(&dev->ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002356}
2357
2358/* These functions are yet to be implemented */
2359#define nvme_error_detected NULL
2360#define nvme_dump_registers NULL
2361#define nvme_link_reset NULL
2362#define nvme_slot_reset NULL
2363#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002364
Jingoo Han671a6012014-02-13 11:19:14 +09002365#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002366static int nvme_suspend(struct device *dev)
2367{
2368 struct pci_dev *pdev = to_pci_dev(dev);
2369 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2370
2371 nvme_dev_shutdown(ndev);
2372 return 0;
2373}
2374
2375static int nvme_resume(struct device *dev)
2376{
2377 struct pci_dev *pdev = to_pci_dev(dev);
2378 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002379
Keith Busch92f7a162015-10-23 11:42:02 -06002380 queue_work(nvme_workq, &ndev->reset_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002381 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002382}
Jingoo Han671a6012014-02-13 11:19:14 +09002383#endif
Keith Buschcd638942013-07-15 15:02:23 -06002384
2385static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002386
Stephen Hemminger1d352032012-09-07 09:33:17 -07002387static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002388 .error_detected = nvme_error_detected,
2389 .mmio_enabled = nvme_dump_registers,
2390 .link_reset = nvme_link_reset,
2391 .slot_reset = nvme_slot_reset,
2392 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002393 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002394};
2395
2396/* Move to pci_ids.h later */
2397#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2398
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002399static const struct pci_device_id nvme_id_table[] = {
Christoph Hellwig106198e2015-11-26 10:07:41 +01002400 { PCI_VDEVICE(INTEL, 0x0953),
2401 .driver_data = NVME_QUIRK_STRIPE_SIZE, },
Keith Busch540c8012015-10-22 15:45:06 -06002402 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2403 .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002404 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01002405 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002406 { 0, }
2407};
2408MODULE_DEVICE_TABLE(pci, nvme_id_table);
2409
2410static struct pci_driver nvme_driver = {
2411 .name = "nvme",
2412 .id_table = nvme_id_table,
2413 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002414 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002415 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002416 .driver = {
2417 .pm = &nvme_dev_pm_ops,
2418 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002419 .err_handler = &nvme_err_handler,
2420};
2421
2422static int __init nvme_init(void)
2423{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002424 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002425
Dan McLeranb9afca32014-04-07 17:10:11 -06002426 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002427
Keith Busch92f7a162015-10-23 11:42:02 -06002428 nvme_workq = alloc_workqueue("nvme", WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
Keith Busch9a6b9452013-12-10 13:10:36 -07002429 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002430 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002431
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002432 result = nvme_core_init();
Keith Busch5c42ea12012-07-25 16:05:18 -06002433 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002434 goto kill_workq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002435
Keith Buschf3db22f2014-06-11 11:51:35 -06002436 result = pci_register_driver(&nvme_driver);
2437 if (result)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002438 goto core_exit;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002439 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002440
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002441 core_exit:
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002442 nvme_core_exit();
Keith Busch9a6b9452013-12-10 13:10:36 -07002443 kill_workq:
2444 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002445 return result;
2446}
2447
2448static void __exit nvme_exit(void)
2449{
2450 pci_unregister_driver(&nvme_driver);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002451 nvme_core_exit();
Keith Busch9a6b9452013-12-10 13:10:36 -07002452 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06002453 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002454 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002455}
2456
2457MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2458MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002459MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002460module_init(nvme_init);
2461module_exit(nvme_exit);