blob: 87ad57bcc7ed3b1f141a22e12b31992eb56bd572 [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>
34#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050035#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040036#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050037#include <linux/sched.h>
38#include <linux/slab.h>
Keith Busche1e5e562015-02-19 13:39:03 -070039#include <linux/t10-pi.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050040#include <linux/types.h>
Linus Torvalds9cf5c092015-11-06 14:22:15 -080041#include <linux/io-64-nonatomic-lo-hi.h>
Keith Busch1d277a62015-10-15 14:10:52 +020042#include <asm/unaligned.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090043
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020044#include "nvme.h"
45
Keith Busch9d43cf62014-05-13 11:42:02 -060046#define NVME_Q_DEPTH 1024
Jens Axboed31af0a2015-03-06 12:56:13 -070047#define NVME_AQ_DEPTH 256
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050048#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
49#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050050
Christoph Hellwig21d34712015-11-26 09:08:36 +010051unsigned char admin_timeout = 60;
Keith Busch9d43cf62014-05-13 11:42:02 -060052module_param(admin_timeout, byte, 0644);
53MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050054
Matthew Wilcoxbd676082014-06-03 23:04:30 -040055unsigned char nvme_io_timeout = 30;
56module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060057MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050058
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +010059unsigned char shutdown_timeout = 5;
Dan McLeran2484f402014-07-01 09:33:32 -060060module_param(shutdown_timeout, byte, 0644);
61MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
62
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050063static int use_threaded_interrupts;
64module_param(use_threaded_interrupts, int, 0);
65
Jon Derrick8ffaadf2015-07-20 10:14:09 -060066static bool use_cmb_sqes = true;
67module_param(use_cmb_sqes, bool, 0644);
68MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
69
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050070static LIST_HEAD(dev_list);
71static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070072static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060073static wait_queue_head_t nvme_kthread_wait;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050074
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010075struct nvme_dev;
76struct nvme_queue;
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +010077struct nvme_iod;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010078
Christoph Hellwig906678922015-10-02 18:49:23 +020079static int __nvme_reset(struct nvme_dev *dev);
Keith Busch4cc06522015-06-05 10:30:08 -060080static int nvme_reset(struct nvme_dev *dev);
Jens Axboea0fa9642015-11-03 20:37:26 -070081static void nvme_process_cq(struct nvme_queue *nvmeq);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +010082static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +020083static void nvme_dead_ctrl(struct nvme_dev *dev);
Keith Buschd4b4ff82013-12-10 13:10:37 -070084
Keith Busch4d115422013-12-10 13:10:40 -070085struct async_cmd_info {
86 struct kthread_work work;
87 struct kthread_worker *worker;
Matias Bjørlinga4aea562014-11-04 08:20:14 -070088 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -070089 u32 result;
90 int status;
91 void *ctx;
92};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050093
94/*
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010095 * Represents an NVM Express device. Each nvme_dev is a PCI function.
96 */
97struct nvme_dev {
98 struct list_head node;
99 struct nvme_queue **queues;
100 struct blk_mq_tag_set tagset;
101 struct blk_mq_tag_set admin_tagset;
102 u32 __iomem *dbs;
103 struct device *dev;
104 struct dma_pool *prp_page_pool;
105 struct dma_pool *prp_small_pool;
106 unsigned queue_count;
107 unsigned online_queues;
108 unsigned max_qid;
109 int q_depth;
110 u32 db_stride;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100111 struct msix_entry *entry;
112 void __iomem *bar;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100113 struct work_struct reset_work;
114 struct work_struct probe_work;
115 struct work_struct scan_work;
116 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;
121
122 struct nvme_ctrl ctrl;
123};
124
125static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
126{
127 return container_of(ctrl, struct nvme_dev, ctrl);
128}
129
130/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500131 * An NVM Express queue. Each device has at least two (one for admin
132 * commands and one for I/O commands).
133 */
134struct nvme_queue {
135 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500136 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500137 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500138 spinlock_t q_lock;
139 struct nvme_command *sq_cmds;
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600140 struct nvme_command __iomem *sq_cmds_io;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500141 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600142 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500143 dma_addr_t sq_dma_addr;
144 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500145 u32 __iomem *q_db;
146 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700147 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500148 u16 sq_head;
149 u16 sq_tail;
150 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700151 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400152 u8 cq_phase;
153 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700154 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500155};
156
157/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200158 * The nvme_iod describes the data in an I/O, including the list of PRP
159 * entries. You can't see it in this data structure because C doesn't let
160 * me express that. Use nvme_alloc_iod to ensure there's enough space
161 * allocated to store the PRP list.
162 */
163struct nvme_iod {
164 unsigned long private; /* For the use of the submitter of the I/O */
165 int npages; /* In the PRP list. 0 means small pool in use */
166 int offset; /* Of PRP list */
167 int nents; /* Used in scatterlist */
168 int length; /* Of data, in bytes */
169 dma_addr_t first_dma;
170 struct scatterlist meta_sg[1]; /* metadata requires single contiguous buffer */
171 struct scatterlist sg[0];
172};
173
174/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500175 * Check we didin't inadvertently grow the command struct
176 */
177static inline void _nvme_check_size(void)
178{
179 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
180 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
181 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
182 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
183 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400184 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700185 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500186 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
187 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
188 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
189 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600190 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500191}
192
Keith Buschedd10d32014-04-03 16:45:23 -0600193typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400194 struct nvme_completion *);
195
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500196struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400197 nvme_completion_fn fn;
198 void *ctx;
Keith Buschc30341d2013-12-10 13:10:38 -0700199 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700200 struct nvme_queue *nvmeq;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700201 struct nvme_iod iod[0];
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500202};
203
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700204/*
205 * Max size of iod being embedded in the request payload
206 */
207#define NVME_INT_PAGES 2
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100208#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
Chong Yuanfda631f2015-03-27 09:21:32 +0800209#define NVME_INT_MASK 0x01
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700210
211/*
212 * Will slightly overestimate the number of pages needed. This is OK
213 * as it only leads to a small amount of wasted memory for the lifetime of
214 * the I/O.
215 */
216static int nvme_npages(unsigned size, struct nvme_dev *dev)
217{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100218 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
219 dev->ctrl.page_size);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700220 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
221}
222
223static unsigned int nvme_cmd_size(struct nvme_dev *dev)
224{
225 unsigned int ret = sizeof(struct nvme_cmd_info);
226
227 ret += sizeof(struct nvme_iod);
228 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
229 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
230
231 return ret;
232}
233
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700234static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
235 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500236{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700237 struct nvme_dev *dev = data;
238 struct nvme_queue *nvmeq = dev->queues[0];
239
Keith Busch42483222015-06-01 09:29:54 -0600240 WARN_ON(hctx_idx != 0);
241 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
242 WARN_ON(nvmeq->tags);
243
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700244 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600245 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700246 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500247}
248
Keith Busch4af0e212015-06-08 10:08:13 -0600249static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
250{
251 struct nvme_queue *nvmeq = hctx->driver_data;
252
253 nvmeq->tags = NULL;
254}
255
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700256static int nvme_admin_init_request(void *data, struct request *req,
257 unsigned int hctx_idx, unsigned int rq_idx,
258 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600259{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700260 struct nvme_dev *dev = data;
261 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
262 struct nvme_queue *nvmeq = dev->queues[0];
263
264 BUG_ON(!nvmeq);
265 cmd->nvmeq = nvmeq;
266 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600267}
268
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700269static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
270 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500271{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700272 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600273 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500274
Keith Busch42483222015-06-01 09:29:54 -0600275 if (!nvmeq->tags)
276 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500277
Keith Busch42483222015-06-01 09:29:54 -0600278 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700279 hctx->driver_data = nvmeq;
280 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500281}
282
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700283static int nvme_init_request(void *data, struct request *req,
284 unsigned int hctx_idx, unsigned int rq_idx,
285 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500286{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700287 struct nvme_dev *dev = data;
288 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
289 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
290
291 BUG_ON(!nvmeq);
292 cmd->nvmeq = nvmeq;
293 return 0;
294}
295
296static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
297 nvme_completion_fn handler)
298{
299 cmd->fn = handler;
300 cmd->ctx = ctx;
301 cmd->aborted = 0;
Keith Buschc917dfe2015-01-07 18:55:48 -0700302 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500303}
304
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700305static void *iod_get_private(struct nvme_iod *iod)
306{
307 return (void *) (iod->private & ~0x1UL);
308}
309
310/*
311 * If bit 0 is set, the iod is embedded in the request payload.
312 */
313static bool iod_should_kfree(struct nvme_iod *iod)
314{
Chong Yuanfda631f2015-03-27 09:21:32 +0800315 return (iod->private & NVME_INT_MASK) == 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700316}
317
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400318/* Special values must be less than 0x1000 */
319#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500320#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
321#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
322#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500323
Keith Buschedd10d32014-04-03 16:45:23 -0600324static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400325 struct nvme_completion *cqe)
326{
327 if (ctx == CMD_CTX_CANCELLED)
328 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400329 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600330 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400331 "completed id %d twice on queue %d\n",
332 cqe->command_id, le16_to_cpup(&cqe->sq_id));
333 return;
334 }
335 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600336 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400337 "invalid id %d completed on queue %d\n",
338 cqe->command_id, le16_to_cpup(&cqe->sq_id));
339 return;
340 }
Keith Buschedd10d32014-04-03 16:45:23 -0600341 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400342}
343
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700344static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
345{
346 void *ctx;
347
348 if (fn)
349 *fn = cmd->fn;
350 ctx = cmd->ctx;
351 cmd->fn = special_completion;
352 cmd->ctx = CMD_CTX_CANCELLED;
353 return ctx;
354}
355
356static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
357 struct nvme_completion *cqe)
358{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700359 u32 result = le32_to_cpup(&cqe->result);
360 u16 status = le16_to_cpup(&cqe->status) >> 1;
361
362 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100363 ++nvmeq->dev->ctrl.event_limit;
Keith Buscha5768aa2015-06-01 14:28:14 -0600364 if (status != NVME_SC_SUCCESS)
365 return;
366
367 switch (result & 0xff07) {
368 case NVME_AER_NOTICE_NS_CHANGED:
369 dev_info(nvmeq->q_dmadev, "rescanning\n");
370 schedule_work(&nvmeq->dev->scan_work);
371 default:
372 dev_warn(nvmeq->q_dmadev, "async event result %08x\n", result);
373 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700374}
375
376static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
377 struct nvme_completion *cqe)
378{
379 struct request *req = ctx;
380
381 u16 status = le16_to_cpup(&cqe->status) >> 1;
382 u32 result = le32_to_cpup(&cqe->result);
383
Keith Busch42483222015-06-01 09:29:54 -0600384 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700385
386 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100387 ++nvmeq->dev->ctrl.abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700388}
389
Keith Buschedd10d32014-04-03 16:45:23 -0600390static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700391 struct nvme_completion *cqe)
392{
393 struct async_cmd_info *cmdinfo = ctx;
394 cmdinfo->result = le32_to_cpup(&cqe->result);
395 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
396 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
Keith Busch42483222015-06-01 09:29:54 -0600397 blk_mq_free_request(cmdinfo->req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700398}
399
400static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
401 unsigned int tag)
402{
Keith Busch42483222015-06-01 09:29:54 -0600403 struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700404
405 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700406}
407
Matthew Wilcox184d2942011-05-11 21:36:38 -0400408/*
409 * Called with local interrupts disabled and the q_lock held. May not sleep.
410 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700411static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400412 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500413{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700414 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400415 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700416 if (tag >= nvmeq->q_depth) {
417 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500418 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400419 }
Keith Busch859361a2012-08-02 14:05:59 -0600420 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700421 *fn = cmd->fn;
422 ctx = cmd->ctx;
423 cmd->fn = special_completion;
424 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400425 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500426}
427
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500428/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400429 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500430 * @nvmeq: The queue to use
431 * @cmd: The command to send
432 *
433 * Safe to use from interrupt context
434 */
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530435static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
436 struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500437{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700438 u16 tail = nvmeq->sq_tail;
439
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600440 if (nvmeq->sq_cmds_io)
441 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
442 else
443 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
444
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500445 if (++tail == nvmeq->q_depth)
446 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500447 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500448 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500449}
450
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530451static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700452{
453 unsigned long flags;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700454 spin_lock_irqsave(&nvmeq->q_lock, flags);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530455 __nvme_submit_cmd(nvmeq, cmd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700456 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700457}
458
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500459static __le64 **iod_list(struct nvme_iod *iod)
460{
461 return ((void *)iod) + iod->offset;
462}
463
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700464static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
465 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500466{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700467 iod->private = private;
468 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
469 iod->npages = -1;
470 iod->length = nbytes;
471 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500472}
473
474static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700475__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
476 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500477{
478 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700479 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500480 sizeof(struct scatterlist) * nseg, gfp);
481
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700482 if (iod)
483 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500484
485 return iod;
486}
487
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700488static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
489 gfp_t gfp)
490{
491 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
492 sizeof(struct nvme_dsm_range);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700493 struct nvme_iod *iod;
494
495 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
496 size <= NVME_INT_BYTES(dev)) {
497 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
498
499 iod = cmd->iod;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700500 iod_init(iod, size, rq->nr_phys_segments,
Chong Yuanfda631f2015-03-27 09:21:32 +0800501 (unsigned long) rq | NVME_INT_MASK);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700502 return iod;
503 }
504
505 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
506 (unsigned long) rq, gfp);
507}
508
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200509static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500510{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100511 const int last_prp = dev->ctrl.page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500512 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500513 __le64 **list = iod_list(iod);
514 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500515
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500516 if (iod->npages == 0)
517 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
518 for (i = 0; i < iod->npages; i++) {
519 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500520 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500521 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500522 prp_dma = next_prp_dma;
523 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700524
525 if (iod_should_kfree(iod))
526 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500527}
528
Keith Busch52b68d72015-02-23 09:16:21 -0700529#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700530static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
531{
532 if (be32_to_cpu(pi->ref_tag) == v)
533 pi->ref_tag = cpu_to_be32(p);
534}
535
536static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
537{
538 if (be32_to_cpu(pi->ref_tag) == p)
539 pi->ref_tag = cpu_to_be32(v);
540}
541
542/**
543 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
544 *
545 * The virtual start sector is the one that was originally submitted by the
546 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
547 * start sector may be different. Remap protection information to match the
548 * physical LBA on writes, and back to the original seed on reads.
549 *
550 * Type 0 and 3 do not have a ref tag, so no remapping required.
551 */
552static void nvme_dif_remap(struct request *req,
553 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
554{
555 struct nvme_ns *ns = req->rq_disk->private_data;
556 struct bio_integrity_payload *bip;
557 struct t10_pi_tuple *pi;
558 void *p, *pmap;
559 u32 i, nlb, ts, phys, virt;
560
561 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
562 return;
563
564 bip = bio_integrity(req->bio);
565 if (!bip)
566 return;
567
568 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700569
570 p = pmap;
571 virt = bip_get_seed(bip);
572 phys = nvme_block_nr(ns, blk_rq_pos(req));
573 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
Dan Williamsac6fc482015-10-21 13:20:18 -0400574 ts = ns->disk->queue->integrity.tuple_size;
Keith Busche1e5e562015-02-19 13:39:03 -0700575
576 for (i = 0; i < nlb; i++, virt++, phys++) {
577 pi = (struct t10_pi_tuple *)p;
578 dif_swap(phys, virt, pi);
579 p += ts;
580 }
581 kunmap_atomic(pmap);
582}
Keith Busch52b68d72015-02-23 09:16:21 -0700583#else /* CONFIG_BLK_DEV_INTEGRITY */
584static void nvme_dif_remap(struct request *req,
585 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
586{
587}
588static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
589{
590}
591static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
592{
593}
Keith Busch52b68d72015-02-23 09:16:21 -0700594#endif
595
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700596static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500597 struct nvme_completion *cqe)
598{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500599 struct nvme_iod *iod = ctx;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700600 struct request *req = iod_get_private(iod);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700601 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500602 u16 status = le16_to_cpup(&cqe->status) >> 1;
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200603 int error = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500604
Keith Buschedd10d32014-04-03 16:45:23 -0600605 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700606 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
607 && (jiffies - req->start_time) < req->timeout) {
Keith Buschc9d3bf82015-01-07 18:55:52 -0700608 unsigned long flags;
609
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100610 nvme_unmap_data(nvmeq->dev, iod);
611
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700612 blk_mq_requeue_request(req);
Keith Buschc9d3bf82015-01-07 18:55:52 -0700613 spin_lock_irqsave(req->q->queue_lock, flags);
614 if (!blk_queue_stopped(req->q))
615 blk_mq_kick_requeue_list(req->q);
616 spin_unlock_irqrestore(req->q->queue_lock, flags);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100617 return;
Keith Buschedd10d32014-04-03 16:45:23 -0600618 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200619
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200620 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
Keith Busch17188bb2015-06-08 10:08:14 -0600621 if (cmd_rq->ctx == CMD_CTX_CANCELLED)
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200622 error = -EINTR;
623 else
624 error = status;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200625 } else {
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200626 error = nvme_error_status(status);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200627 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200628 }
629
Keith Buscha0a931d2015-05-22 12:28:31 -0600630 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
631 u32 result = le32_to_cpup(&cqe->result);
632 req->special = (void *)(uintptr_t)result;
633 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700634
635 if (cmd_rq->aborted)
Christoph Hellwige75ec752015-05-22 11:12:39 +0200636 dev_warn(nvmeq->dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700637 "completing aborted command with status:%04x\n",
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200638 error);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700639
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100640 nvme_unmap_data(nvmeq->dev, iod);
641 blk_mq_complete_request(req, error);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500642}
643
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200644static bool nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
645 int total_len)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500646{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500647 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500648 int length = total_len;
649 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500650 int dma_len = sg_dma_len(sg);
651 u64 dma_addr = sg_dma_address(sg);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100652 u32 page_size = dev->ctrl.page_size;
Murali Iyerf137e0f2015-03-26 11:07:51 -0500653 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500654 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500655 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500656 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500657 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500658
Keith Busch1d090622014-06-23 11:34:01 -0600659 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500660 if (length <= 0)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200661 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500662
Keith Busch1d090622014-06-23 11:34:01 -0600663 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500664 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600665 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500666 } else {
667 sg = sg_next(sg);
668 dma_addr = sg_dma_address(sg);
669 dma_len = sg_dma_len(sg);
670 }
671
Keith Busch1d090622014-06-23 11:34:01 -0600672 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600673 iod->first_dma = dma_addr;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200674 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500675 }
676
Keith Busch1d090622014-06-23 11:34:01 -0600677 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500678 if (nprps <= (256 / 8)) {
679 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500680 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500681 } else {
682 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500683 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500684 }
685
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200686 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400687 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600688 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500689 iod->npages = -1;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200690 return false;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400691 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500692 list[0] = prp_list;
693 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500694 i = 0;
695 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600696 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500697 __le64 *old_prp_list = prp_list;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200698 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500699 if (!prp_list)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200700 return false;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500701 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400702 prp_list[0] = old_prp_list[i - 1];
703 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
704 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500705 }
706 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600707 dma_len -= page_size;
708 dma_addr += page_size;
709 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500710 if (length <= 0)
711 break;
712 if (dma_len > 0)
713 continue;
714 BUG_ON(dma_len < 0);
715 sg = sg_next(sg);
716 dma_addr = sg_dma_address(sg);
717 dma_len = sg_dma_len(sg);
718 }
719
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200720 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500721}
722
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200723static int nvme_map_data(struct nvme_dev *dev, struct nvme_iod *iod,
724 struct nvme_command *cmnd)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200725{
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200726 struct request *req = iod_get_private(iod);
727 struct request_queue *q = req->q;
728 enum dma_data_direction dma_dir = rq_data_dir(req) ?
729 DMA_TO_DEVICE : DMA_FROM_DEVICE;
730 int ret = BLK_MQ_RQ_QUEUE_ERROR;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200731
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200732 sg_init_table(iod->sg, req->nr_phys_segments);
733 iod->nents = blk_rq_map_sg(q, req, iod->sg);
734 if (!iod->nents)
735 goto out;
736
737 ret = BLK_MQ_RQ_QUEUE_BUSY;
738 if (!dma_map_sg(dev->dev, iod->sg, iod->nents, dma_dir))
739 goto out;
740
741 if (!nvme_setup_prps(dev, iod, blk_rq_bytes(req)))
742 goto out_unmap;
743
744 ret = BLK_MQ_RQ_QUEUE_ERROR;
745 if (blk_integrity_rq(req)) {
746 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
747 goto out_unmap;
748
749 sg_init_table(iod->meta_sg, 1);
750 if (blk_rq_map_integrity_sg(q, req->bio, iod->meta_sg) != 1)
751 goto out_unmap;
752
753 if (rq_data_dir(req))
754 nvme_dif_remap(req, nvme_dif_prep);
755
756 if (!dma_map_sg(dev->dev, iod->meta_sg, 1, dma_dir))
757 goto out_unmap;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200758 }
759
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200760 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
761 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
762 if (blk_integrity_rq(req))
763 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg));
764 return BLK_MQ_RQ_QUEUE_OK;
765
766out_unmap:
767 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
768out:
769 return ret;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200770}
771
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100772static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod)
773{
774 struct request *req = iod_get_private(iod);
775 enum dma_data_direction dma_dir = rq_data_dir(req) ?
776 DMA_TO_DEVICE : DMA_FROM_DEVICE;
777
778 if (iod->nents) {
779 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
780 if (blk_integrity_rq(req)) {
781 if (!rq_data_dir(req))
782 nvme_dif_remap(req, nvme_dif_complete);
783 dma_unmap_sg(dev->dev, iod->meta_sg, 1, dma_dir);
784 }
785 }
786
787 nvme_free_iod(dev, iod);
788}
789
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700790/*
791 * We reuse the small pool to allocate the 16-byte range here as it is not
792 * worth having a special pool for these or additional cases to handle freeing
793 * the iod.
794 */
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200795static int nvme_setup_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
796 struct nvme_iod *iod, struct nvme_command *cmnd)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700797{
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200798 struct request *req = iod_get_private(iod);
799 struct nvme_dsm_range *range;
800
801 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
802 &iod->first_dma);
803 if (!range)
804 return BLK_MQ_RQ_QUEUE_BUSY;
805 iod_list(iod)[0] = (__le64 *)range;
806 iod->npages = 0;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700807
Keith Busch0e5e4f02012-11-09 16:33:05 -0700808 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700809 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
810 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700811
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200812 memset(cmnd, 0, sizeof(*cmnd));
813 cmnd->dsm.opcode = nvme_cmd_dsm;
814 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
815 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
816 cmnd->dsm.nr = 0;
817 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
818 return BLK_MQ_RQ_QUEUE_OK;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700819}
820
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200821/*
822 * NOTE: ns is NULL when called on the admin queue.
823 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700824static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
825 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600826{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700827 struct nvme_ns *ns = hctx->queue->queuedata;
828 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200829 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700830 struct request *req = bd->rq;
831 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600832 struct nvme_iod *iod;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200833 struct nvme_command cmnd;
834 int ret = BLK_MQ_RQ_QUEUE_OK;
Keith Buschedd10d32014-04-03 16:45:23 -0600835
Keith Busche1e5e562015-02-19 13:39:03 -0700836 /*
837 * If formated with metadata, require the block layer provide a buffer
838 * unless this namespace is formated such that the metadata can be
839 * stripped/generated by the controller with PRACT=1.
840 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200841 if (ns && ns->ms && !blk_integrity_rq(req)) {
Keith Busch71feb362015-06-19 11:07:30 -0600842 if (!(ns->pi_type && ns->ms == 8) &&
843 req->cmd_type != REQ_TYPE_DRV_PRIV) {
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200844 blk_mq_complete_request(req, -EFAULT);
Keith Busche1e5e562015-02-19 13:39:03 -0700845 return BLK_MQ_RQ_QUEUE_OK;
846 }
847 }
848
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200849 iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600850 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700851 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600852
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700853 if (req->cmd_flags & REQ_DISCARD) {
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200854 ret = nvme_setup_discard(nvmeq, ns, iod, &cmnd);
855 } else {
856 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
857 memcpy(&cmnd, req->cmd, sizeof(cmnd));
858 else if (req->cmd_flags & REQ_FLUSH)
859 nvme_setup_flush(ns, &cmnd);
860 else
861 nvme_setup_rw(ns, req, &cmnd);
Keith Buschedd10d32014-04-03 16:45:23 -0600862
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200863 if (req->nr_phys_segments)
864 ret = nvme_map_data(dev, iod, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700865 }
866
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200867 if (ret)
868 goto out;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700869
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200870 cmnd.common.command_id = req->tag;
871 nvme_set_info(cmd, iod, req_completion);
872
873 spin_lock_irq(&nvmeq->q_lock);
874 __nvme_submit_cmd(nvmeq, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700875 nvme_process_cq(nvmeq);
876 spin_unlock_irq(&nvmeq->q_lock);
877 return BLK_MQ_RQ_QUEUE_OK;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200878out:
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200879 nvme_free_iod(dev, iod);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200880 return ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500881}
882
Jens Axboea0fa9642015-11-03 20:37:26 -0700883static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500884{
Matthew Wilcox82123462011-01-20 13:24:06 -0500885 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500886
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500887 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500888 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500889
890 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400891 void *ctx;
892 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500893 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500894 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500895 break;
896 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
897 if (++head == nvmeq->q_depth) {
898 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500899 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500900 }
Jens Axboea0fa9642015-11-03 20:37:26 -0700901 if (tag && *tag == cqe.command_id)
902 *tag = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700903 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600904 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500905 }
906
907 /* If the controller ignores the cq head doorbell and continuously
908 * writes to the queue, it is theoretically possible to wrap around
909 * the queue twice and mistakenly return IRQ_NONE. Linux only
910 * requires that 0.1% of your interrupts are handled, so this isn't
911 * a big problem.
912 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500913 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Jens Axboea0fa9642015-11-03 20:37:26 -0700914 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500915
Keith Busch604e8c82015-11-20 08:38:13 -0700916 if (likely(nvmeq->cq_vector >= 0))
917 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500918 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500919 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500920
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400921 nvmeq->cqe_seen = 1;
Jens Axboea0fa9642015-11-03 20:37:26 -0700922}
923
924static void nvme_process_cq(struct nvme_queue *nvmeq)
925{
926 __nvme_process_cq(nvmeq, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500927}
928
929static irqreturn_t nvme_irq(int irq, void *data)
930{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500931 irqreturn_t result;
932 struct nvme_queue *nvmeq = data;
933 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400934 nvme_process_cq(nvmeq);
935 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
936 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500937 spin_unlock(&nvmeq->q_lock);
938 return result;
939}
940
941static irqreturn_t nvme_irq_check(int irq, void *data)
942{
943 struct nvme_queue *nvmeq = data;
944 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
945 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
946 return IRQ_NONE;
947 return IRQ_WAKE_THREAD;
948}
949
Jens Axboea0fa9642015-11-03 20:37:26 -0700950static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
951{
952 struct nvme_queue *nvmeq = hctx->driver_data;
953
954 if ((le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
955 nvmeq->cq_phase) {
956 spin_lock_irq(&nvmeq->q_lock);
957 __nvme_process_cq(nvmeq, &tag);
958 spin_unlock_irq(&nvmeq->q_lock);
959
960 if (tag == -1)
961 return 1;
962 }
963
964 return 0;
965}
966
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700967static int nvme_submit_async_admin_req(struct nvme_dev *dev)
968{
969 struct nvme_queue *nvmeq = dev->queues[0];
970 struct nvme_command c;
971 struct nvme_cmd_info *cmd_info;
972 struct request *req;
973
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100974 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100975 BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED);
Dan Carpenter9f173b32014-11-05 23:39:09 +0300976 if (IS_ERR(req))
977 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700978
Keith Buschc917dfe2015-01-07 18:55:48 -0700979 req->cmd_flags |= REQ_NO_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700980 cmd_info = blk_mq_rq_to_pdu(req);
Keith Busch1efccc92015-03-31 10:37:17 -0600981 nvme_set_info(cmd_info, NULL, async_req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700982
983 memset(&c, 0, sizeof(c));
984 c.common.opcode = nvme_admin_async_event;
985 c.common.command_id = req->tag;
986
Keith Busch42483222015-06-01 09:29:54 -0600987 blk_mq_free_request(req);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530988 __nvme_submit_cmd(nvmeq, &c);
989 return 0;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700990}
991
992static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -0700993 struct nvme_command *cmd,
994 struct async_cmd_info *cmdinfo, unsigned timeout)
995{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700996 struct nvme_queue *nvmeq = dev->queues[0];
997 struct request *req;
998 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -0700999
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001000 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE, 0);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001001 if (IS_ERR(req))
1002 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001003
1004 req->timeout = timeout;
1005 cmd_rq = blk_mq_rq_to_pdu(req);
1006 cmdinfo->req = req;
1007 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -07001008 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001009
1010 cmd->common.command_id = req->tag;
1011
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301012 nvme_submit_cmd(nvmeq, cmd);
1013 return 0;
Keith Busch4d115422013-12-10 13:10:40 -07001014}
1015
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001016static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1017{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001018 struct nvme_command c;
1019
1020 memset(&c, 0, sizeof(c));
1021 c.delete_queue.opcode = opcode;
1022 c.delete_queue.qid = cpu_to_le16(id);
1023
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001024 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001025}
1026
1027static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1028 struct nvme_queue *nvmeq)
1029{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001030 struct nvme_command c;
1031 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1032
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001033 /*
1034 * Note: we (ab)use the fact the the prp fields survive if no data
1035 * is attached to the request.
1036 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001037 memset(&c, 0, sizeof(c));
1038 c.create_cq.opcode = nvme_admin_create_cq;
1039 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1040 c.create_cq.cqid = cpu_to_le16(qid);
1041 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1042 c.create_cq.cq_flags = cpu_to_le16(flags);
1043 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1044
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001045 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001046}
1047
1048static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1049 struct nvme_queue *nvmeq)
1050{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001051 struct nvme_command c;
1052 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1053
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001054 /*
1055 * Note: we (ab)use the fact the the prp fields survive if no data
1056 * is attached to the request.
1057 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001058 memset(&c, 0, sizeof(c));
1059 c.create_sq.opcode = nvme_admin_create_sq;
1060 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1061 c.create_sq.sqid = cpu_to_le16(qid);
1062 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1063 c.create_sq.sq_flags = cpu_to_le16(flags);
1064 c.create_sq.cqid = cpu_to_le16(qid);
1065
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001066 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001067}
1068
1069static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1070{
1071 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1072}
1073
1074static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1075{
1076 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1077}
1078
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001079/**
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001080 * nvme_abort_req - Attempt aborting a request
Keith Buschc30341d2013-12-10 13:10:38 -07001081 *
1082 * Schedule controller reset if the command was already aborted once before and
1083 * still hasn't been returned to the driver, or if this is the admin queue.
1084 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001085static void nvme_abort_req(struct request *req)
Keith Buschc30341d2013-12-10 13:10:38 -07001086{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001087 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1088 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001089 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001090 struct request *abort_req;
1091 struct nvme_cmd_info *abort_cmd;
1092 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001093
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001094 if (!nvmeq->qid || cmd_rq->aborted) {
Christoph Hellwig906678922015-10-02 18:49:23 +02001095 spin_lock(&dev_list_lock);
1096 if (!__nvme_reset(dev)) {
1097 dev_warn(dev->dev,
1098 "I/O %d QID %d timeout, reset controller\n",
1099 req->tag, nvmeq->qid);
1100 }
1101 spin_unlock(&dev_list_lock);
Keith Buschc30341d2013-12-10 13:10:38 -07001102 return;
1103 }
1104
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001105 if (!dev->ctrl.abort_limit)
Keith Buschc30341d2013-12-10 13:10:38 -07001106 return;
1107
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001108 abort_req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001109 BLK_MQ_REQ_NOWAIT);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001110 if (IS_ERR(abort_req))
Keith Buschc30341d2013-12-10 13:10:38 -07001111 return;
1112
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001113 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1114 nvme_set_info(abort_cmd, abort_req, abort_completion);
1115
Keith Buschc30341d2013-12-10 13:10:38 -07001116 memset(&cmd, 0, sizeof(cmd));
1117 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001118 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001119 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001120 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001121
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001122 --dev->ctrl.abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001123 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001124
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001125 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag,
Keith Buschc30341d2013-12-10 13:10:38 -07001126 nvmeq->qid);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301127 nvme_submit_cmd(dev->queues[0], &cmd);
Keith Buschc30341d2013-12-10 13:10:38 -07001128}
1129
Keith Busch42483222015-06-01 09:29:54 -06001130static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001131{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001132 struct nvme_queue *nvmeq = data;
1133 void *ctx;
1134 nvme_completion_fn fn;
1135 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001136 struct nvme_completion cqe;
1137
1138 if (!blk_mq_request_started(req))
1139 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001140
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001141 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001142
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001143 if (cmd->ctx == CMD_CTX_CANCELLED)
1144 return;
1145
Keith Buschcef6a942015-01-07 18:55:51 -07001146 if (blk_queue_dying(req->q))
1147 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1148 else
1149 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1150
1151
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001152 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1153 req->tag, nvmeq->qid);
1154 ctx = cancel_cmd_info(cmd, &fn);
1155 fn(nvmeq, ctx, &cqe);
1156}
1157
1158static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
1159{
1160 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
1161 struct nvme_queue *nvmeq = cmd->nvmeq;
1162
Keith Busch07836e62015-02-19 10:34:48 -07001163 dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag,
1164 nvmeq->qid);
1165 spin_lock_irq(&nvmeq->q_lock);
1166 nvme_abort_req(req);
1167 spin_unlock_irq(&nvmeq->q_lock);
1168
Keith Busch7a509a62015-01-07 18:55:53 -07001169 /*
1170 * The aborted req will be completed on receiving the abort req.
1171 * We enable the timer again. If hit twice, it'll cause a device reset,
1172 * as the device then is in a faulty state.
1173 */
Keith Busch07836e62015-02-19 10:34:48 -07001174 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001175}
1176
Keith Buschf435c282014-07-07 09:14:42 -06001177static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001178{
1179 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1180 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001181 if (nvmeq->sq_cmds)
1182 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001183 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1184 kfree(nvmeq);
1185}
1186
Keith Buscha1a5ef92013-12-16 13:50:00 -05001187static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001188{
1189 int i;
1190
Keith Buscha1a5ef92013-12-16 13:50:00 -05001191 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001192 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001193 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001194 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001195 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001196 }
Keith Busch22404272013-07-15 15:02:20 -06001197}
1198
Keith Busch4d115422013-12-10 13:10:40 -07001199/**
1200 * nvme_suspend_queue - put queue into suspended state
1201 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001202 */
1203static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001204{
Keith Busch2b25d982014-12-22 12:59:04 -07001205 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001206
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001207 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001208 if (nvmeq->cq_vector == -1) {
1209 spin_unlock_irq(&nvmeq->q_lock);
1210 return 1;
1211 }
1212 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001213 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001214 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001215 spin_unlock_irq(&nvmeq->q_lock);
1216
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001217 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
1218 blk_mq_freeze_queue_start(nvmeq->dev->ctrl.admin_q);
Keith Busch6df3dbc2015-03-26 13:49:33 -06001219
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001220 irq_set_affinity_hint(vector, NULL);
1221 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001222
Keith Busch4d115422013-12-10 13:10:40 -07001223 return 0;
1224}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001225
Keith Busch4d115422013-12-10 13:10:40 -07001226static void nvme_clear_queue(struct nvme_queue *nvmeq)
1227{
Keith Busch22404272013-07-15 15:02:20 -06001228 spin_lock_irq(&nvmeq->q_lock);
Keith Busch42483222015-06-01 09:29:54 -06001229 if (nvmeq->tags && *nvmeq->tags)
1230 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001231 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001232}
1233
Keith Busch4d115422013-12-10 13:10:40 -07001234static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1235{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001236 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001237
1238 if (!nvmeq)
1239 return;
1240 if (nvme_suspend_queue(nvmeq))
1241 return;
1242
Keith Busch0e53d182013-12-10 13:10:39 -07001243 /* Don't tell the adapter to delete the admin queue.
1244 * Don't tell a removed adapter to delete IO queues. */
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001245 if (qid && readl(dev->bar + NVME_REG_CSTS) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001246 adapter_delete_sq(dev, qid);
1247 adapter_delete_cq(dev, qid);
1248 }
Keith Busch07836e62015-02-19 10:34:48 -07001249
1250 spin_lock_irq(&nvmeq->q_lock);
1251 nvme_process_cq(nvmeq);
1252 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001253}
1254
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001255static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1256 int entry_size)
1257{
1258 int q_depth = dev->q_depth;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001259 unsigned q_size_aligned = roundup(q_depth * entry_size,
1260 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001261
1262 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001263 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001264 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
Jon Derrickc45f5c92015-07-21 15:08:13 -06001265 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001266
1267 /*
1268 * Ensure the reduced q_depth is above some threshold where it
1269 * would be better to map queues in system memory with the
1270 * original depth
1271 */
1272 if (q_depth < 64)
1273 return -ENOMEM;
1274 }
1275
1276 return q_depth;
1277}
1278
1279static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1280 int qid, int depth)
1281{
1282 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001283 unsigned offset = (qid - 1) * roundup(SQ_SIZE(depth),
1284 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001285 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1286 nvmeq->sq_cmds_io = dev->cmb + offset;
1287 } else {
1288 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1289 &nvmeq->sq_dma_addr, GFP_KERNEL);
1290 if (!nvmeq->sq_cmds)
1291 return -ENOMEM;
1292 }
1293
1294 return 0;
1295}
1296
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001297static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001298 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001299{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001300 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001301 if (!nvmeq)
1302 return NULL;
1303
Christoph Hellwige75ec752015-05-22 11:12:39 +02001304 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001305 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001306 if (!nvmeq->cqes)
1307 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001308
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001309 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001310 goto free_cqdma;
1311
Christoph Hellwige75ec752015-05-22 11:12:39 +02001312 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001313 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001314 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001315 dev->ctrl.instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001316 spin_lock_init(&nvmeq->q_lock);
1317 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001318 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001319 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001320 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001321 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001322 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001323 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001324
Jon Derrick36a7e992015-05-27 12:26:23 -06001325 /* make sure queue descriptor is set before queue count, for kthread */
1326 mb();
1327 dev->queue_count++;
1328
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001329 return nvmeq;
1330
1331 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001332 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001333 nvmeq->cq_dma_addr);
1334 free_nvmeq:
1335 kfree(nvmeq);
1336 return NULL;
1337}
1338
Matthew Wilcox30010822011-01-20 09:10:15 -05001339static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1340 const char *name)
1341{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001342 if (use_threaded_interrupts)
1343 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001344 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001345 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001346 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001347 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001348}
1349
Keith Busch22404272013-07-15 15:02:20 -06001350static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001351{
Keith Busch22404272013-07-15 15:02:20 -06001352 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001353
Keith Busch7be50e92014-09-10 15:48:47 -06001354 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001355 nvmeq->sq_tail = 0;
1356 nvmeq->cq_head = 0;
1357 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001358 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001359 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001360 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001361 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001362}
1363
1364static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1365{
1366 struct nvme_dev *dev = nvmeq->dev;
1367 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001368
Keith Busch2b25d982014-12-22 12:59:04 -07001369 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001370 result = adapter_alloc_cq(dev, qid, nvmeq);
1371 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001372 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001373
1374 result = adapter_alloc_sq(dev, qid, nvmeq);
1375 if (result < 0)
1376 goto release_cq;
1377
Matthew Wilcox3193f072014-01-27 15:57:22 -05001378 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001379 if (result < 0)
1380 goto release_sq;
1381
Keith Busch22404272013-07-15 15:02:20 -06001382 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001383 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001384
1385 release_sq:
1386 adapter_delete_sq(dev, qid);
1387 release_cq:
1388 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001389 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001390}
1391
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001392static struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001393 .queue_rq = nvme_queue_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001394 .map_queue = blk_mq_map_queue,
1395 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001396 .exit_hctx = nvme_admin_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001397 .init_request = nvme_admin_init_request,
1398 .timeout = nvme_timeout,
1399};
1400
1401static struct blk_mq_ops nvme_mq_ops = {
1402 .queue_rq = nvme_queue_rq,
1403 .map_queue = blk_mq_map_queue,
1404 .init_hctx = nvme_init_hctx,
1405 .init_request = nvme_init_request,
1406 .timeout = nvme_timeout,
Jens Axboea0fa9642015-11-03 20:37:26 -07001407 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001408};
1409
Keith Buschea191d22015-01-07 18:55:49 -07001410static void nvme_dev_remove_admin(struct nvme_dev *dev)
1411{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001412 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
1413 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001414 blk_mq_free_tag_set(&dev->admin_tagset);
1415 }
1416}
1417
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001418static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1419{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001420 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001421 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1422 dev->admin_tagset.nr_hw_queues = 1;
1423 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
Keith Busch1efccc92015-03-31 10:37:17 -06001424 dev->admin_tagset.reserved_tags = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001425 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001426 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001427 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001428 dev->admin_tagset.driver_data = dev;
1429
1430 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1431 return -ENOMEM;
1432
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001433 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1434 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001435 blk_mq_free_tag_set(&dev->admin_tagset);
1436 return -ENOMEM;
1437 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001438 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001439 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001440 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001441 return -ENODEV;
1442 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001443 } else
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001444 blk_mq_unfreeze_queue(dev->ctrl.admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001445
1446 return 0;
1447}
1448
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001449static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001450{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001451 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001452 u32 aqa;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001453 u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001454 struct nvme_queue *nvmeq;
1455
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001456 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1) ?
Keith Buschdfbac8c2015-08-10 15:20:40 -06001457 NVME_CAP_NSSRC(cap) : 0;
1458
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001459 if (dev->subsystem &&
1460 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1461 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001462
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001463 result = nvme_disable_ctrl(&dev->ctrl, cap);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001464 if (result < 0)
1465 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001466
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001467 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001468 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001469 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001470 if (!nvmeq)
1471 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001472 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001473
1474 aqa = nvmeq->q_depth - 1;
1475 aqa |= aqa << 16;
1476
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001477 writel(aqa, dev->bar + NVME_REG_AQA);
1478 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1479 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001480
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001481 result = nvme_enable_ctrl(&dev->ctrl, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001482 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001483 goto free_nvmeq;
1484
Keith Busch2b25d982014-12-22 12:59:04 -07001485 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001486 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001487 if (result) {
1488 nvmeq->cq_vector = -1;
Keith Busch0fb59cb2015-01-07 18:55:50 -07001489 goto free_nvmeq;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001490 }
Keith Busch025c5572013-05-01 13:07:51 -06001491
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001492 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001493
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001494 free_nvmeq:
1495 nvme_free_queues(dev, 0);
1496 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001497}
1498
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001499static int nvme_kthread(void *data)
1500{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001501 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001502
1503 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001504 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001505 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001506 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001507 int i;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001508 u32 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001509
1510 if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
1511 csts & NVME_CSTS_CFS) {
Christoph Hellwig906678922015-10-02 18:49:23 +02001512 if (!__nvme_reset(dev)) {
1513 dev_warn(dev->dev,
1514 "Failed status: %x, reset controller\n",
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001515 readl(dev->bar + NVME_REG_CSTS));
Christoph Hellwig906678922015-10-02 18:49:23 +02001516 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07001517 continue;
1518 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001519 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001520 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001521 if (!nvmeq)
1522 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001523 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001524 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06001525
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001526 while (i == 0 && dev->ctrl.event_limit > 0) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001527 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06001528 break;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001529 dev->ctrl.event_limit--;
Keith Busch6fccf932014-06-18 13:58:57 -06001530 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001531 spin_unlock_irq(&nvmeq->q_lock);
1532 }
1533 }
1534 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001535 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001536 }
1537 return 0;
1538}
1539
Christoph Hellwig2659e572015-10-02 18:51:31 +02001540/*
1541 * Create I/O queues. Failing to create an I/O queue is not an issue,
1542 * we can continue with less than the desired amount of queues, and
1543 * even a controller without I/O queues an still be used to issue
1544 * admin commands. This might be useful to upgrade a buggy firmware
1545 * for example.
1546 */
Keith Busch42f61422014-03-24 10:46:25 -06001547static void nvme_create_io_queues(struct nvme_dev *dev)
1548{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001549 unsigned i;
Keith Busch42f61422014-03-24 10:46:25 -06001550
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001551 for (i = dev->queue_count; i <= dev->max_qid; i++)
Keith Busch2b25d982014-12-22 12:59:04 -07001552 if (!nvme_alloc_queue(dev, i, dev->q_depth))
Keith Busch42f61422014-03-24 10:46:25 -06001553 break;
1554
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001555 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
Christoph Hellwig2659e572015-10-02 18:51:31 +02001556 if (nvme_create_queue(dev->queues[i], i)) {
1557 nvme_free_queues(dev, i);
Keith Busch42f61422014-03-24 10:46:25 -06001558 break;
Christoph Hellwig2659e572015-10-02 18:51:31 +02001559 }
Keith Busch42f61422014-03-24 10:46:25 -06001560}
1561
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001562static int set_queue_count(struct nvme_dev *dev, int count)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001563{
1564 int status;
1565 u32 result;
Matthew Wilcoxb3b06812011-01-20 09:14:34 -05001566 u32 q_count = (count - 1) | ((count - 1) << 16);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001567
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001568 status = nvme_set_features(&dev->ctrl, NVME_FEAT_NUM_QUEUES, q_count, 0,
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001569 &result);
Matthew Wilcox27e81662014-04-11 11:58:45 -04001570 if (status < 0)
1571 return status;
1572 if (status > 0) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001573 dev_err(dev->dev, "Could not set queue count (%d)\n", status);
Keith Buschbadc34d2014-06-23 14:25:35 -06001574 return 0;
Matthew Wilcox27e81662014-04-11 11:58:45 -04001575 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001576 return min(result & 0xffff, result >> 16) + 1;
1577}
1578
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001579static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1580{
1581 u64 szu, size, offset;
1582 u32 cmbloc;
1583 resource_size_t bar_size;
1584 struct pci_dev *pdev = to_pci_dev(dev->dev);
1585 void __iomem *cmb;
1586 dma_addr_t dma_addr;
1587
1588 if (!use_cmb_sqes)
1589 return NULL;
1590
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001591 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001592 if (!(NVME_CMB_SZ(dev->cmbsz)))
1593 return NULL;
1594
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001595 cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001596
1597 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1598 size = szu * NVME_CMB_SZ(dev->cmbsz);
1599 offset = szu * NVME_CMB_OFST(cmbloc);
1600 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
1601
1602 if (offset > bar_size)
1603 return NULL;
1604
1605 /*
1606 * Controllers may support a CMB size larger than their BAR,
1607 * for example, due to being behind a bridge. Reduce the CMB to
1608 * the reported size of the BAR
1609 */
1610 if (size > bar_size - offset)
1611 size = bar_size - offset;
1612
1613 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
1614 cmb = ioremap_wc(dma_addr, size);
1615 if (!cmb)
1616 return NULL;
1617
1618 dev->cmb_dma_addr = dma_addr;
1619 dev->cmb_size = size;
1620 return cmb;
1621}
1622
1623static inline void nvme_release_cmb(struct nvme_dev *dev)
1624{
1625 if (dev->cmb) {
1626 iounmap(dev->cmb);
1627 dev->cmb = NULL;
1628 }
1629}
1630
Keith Busch9d713c22013-07-15 15:02:24 -06001631static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1632{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001633 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001634}
1635
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001636static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001637{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001638 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02001639 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch42f61422014-03-24 10:46:25 -06001640 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001641
Keith Busch42f61422014-03-24 10:46:25 -06001642 nr_io_queues = num_possible_cpus();
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001643 result = set_queue_count(dev, nr_io_queues);
Keith Buschbadc34d2014-06-23 14:25:35 -06001644 if (result <= 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001645 return result;
Matthew Wilcoxb348b7d2011-02-15 16:16:02 -05001646 if (result < nr_io_queues)
1647 nr_io_queues = result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001648
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001649 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1650 result = nvme_cmb_qdepth(dev, nr_io_queues,
1651 sizeof(struct nvme_command));
1652 if (result > 0)
1653 dev->q_depth = result;
1654 else
1655 nvme_release_cmb(dev);
1656 }
1657
Keith Busch9d713c22013-07-15 15:02:24 -06001658 size = db_bar_size(dev, nr_io_queues);
1659 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001660 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001661 do {
1662 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1663 if (dev->bar)
1664 break;
1665 if (!--nr_io_queues)
1666 return -ENOMEM;
1667 size = db_bar_size(dev, nr_io_queues);
1668 } while (1);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001669 dev->dbs = dev->bar + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07001670 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001671 }
1672
Keith Busch9d713c22013-07-15 15:02:24 -06001673 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05001674 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001675
Jens Axboee32efbf2014-11-14 09:49:26 -07001676 /*
1677 * If we enable msix early due to not intx, disable it again before
1678 * setting up the full range we need.
1679 */
1680 if (!pdev->irq)
1681 pci_disable_msix(pdev);
1682
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001683 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001684 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001685 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
1686 if (vecs < 0) {
1687 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
1688 if (vecs < 0) {
1689 vecs = 1;
1690 } else {
1691 for (i = 0; i < vecs; i++)
1692 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001693 }
1694 }
1695
Matthew Wilcox063a8092013-06-20 10:53:48 -04001696 /*
1697 * Should investigate if there's a performance win from allocating
1698 * more queues than interrupt vectors; it might allow the submission
1699 * path to scale better, even if the receive path is limited by the
1700 * number of interrupts.
1701 */
1702 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06001703 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001704
Matthew Wilcox3193f072014-01-27 15:57:22 -05001705 result = queue_request_irq(dev, adminq, adminq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001706 if (result) {
1707 adminq->cq_vector = -1;
Keith Busch22404272013-07-15 15:02:20 -06001708 goto free_queues;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001709 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001710
Keith Buschcd638942013-07-15 15:02:23 -06001711 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06001712 nvme_free_queues(dev, nr_io_queues + 1);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001713 nvme_create_io_queues(dev);
Keith Buschcd638942013-07-15 15:02:23 -06001714
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001715 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001716
Keith Busch22404272013-07-15 15:02:20 -06001717 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05001718 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06001719 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001720}
1721
Keith Buschbda4e0f2015-09-03 08:18:17 -06001722static void nvme_set_irq_hints(struct nvme_dev *dev)
1723{
1724 struct nvme_queue *nvmeq;
1725 int i;
1726
1727 for (i = 0; i < dev->online_queues; i++) {
1728 nvmeq = dev->queues[i];
1729
1730 if (!nvmeq->tags || !(*nvmeq->tags))
1731 continue;
1732
1733 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
1734 blk_mq_tags_cpumask(*nvmeq->tags));
1735 }
1736}
1737
Keith Buscha5768aa2015-06-01 14:28:14 -06001738static void nvme_dev_scan(struct work_struct *work)
1739{
1740 struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06001741
1742 if (!dev->tagset.tags)
1743 return;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001744 nvme_scan_namespaces(&dev->ctrl);
Keith Buschbda4e0f2015-09-03 08:18:17 -06001745 nvme_set_irq_hints(dev);
Keith Buscha5768aa2015-06-01 14:28:14 -06001746}
1747
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001748/*
1749 * Return: error value if an error occurred setting up the queues or calling
1750 * Identify Device. 0 if these succeeded, even if adding some of the
1751 * namespaces failed. At the moment, these failures are silent. TBD which
1752 * failures should be reported.
1753 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001754static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001755{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001756 if (!dev->ctrl.tagset) {
Keith Buschffe77042015-06-08 10:08:15 -06001757 dev->tagset.ops = &nvme_mq_ops;
1758 dev->tagset.nr_hw_queues = dev->online_queues - 1;
1759 dev->tagset.timeout = NVME_IO_TIMEOUT;
1760 dev->tagset.numa_node = dev_to_node(dev->dev);
1761 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001762 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06001763 dev->tagset.cmd_size = nvme_cmd_size(dev);
1764 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
1765 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001766
Keith Buschffe77042015-06-08 10:08:15 -06001767 if (blk_mq_alloc_tag_set(&dev->tagset))
1768 return 0;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001769 dev->ctrl.tagset = &dev->tagset;
Keith Buschffe77042015-06-08 10:08:15 -06001770 }
Keith Buscha5768aa2015-06-01 14:28:14 -06001771 schedule_work(&dev->scan_work);
Keith Busche1e5e562015-02-19 13:39:03 -07001772 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001773}
1774
Keith Busch0877cb02013-07-15 15:02:19 -06001775static int nvme_dev_map(struct nvme_dev *dev)
1776{
Keith Busch42f61422014-03-24 10:46:25 -06001777 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06001778 int bars, result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001779 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001780
1781 if (pci_enable_device_mem(pdev))
1782 return result;
1783
1784 dev->entry[0].vector = pdev->irq;
1785 pci_set_master(pdev);
1786 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07001787 if (!bars)
1788 goto disable_pci;
1789
Keith Busch0877cb02013-07-15 15:02:19 -06001790 if (pci_request_selected_regions(pdev, bars, "nvme"))
1791 goto disable_pci;
1792
Christoph Hellwige75ec752015-05-22 11:12:39 +02001793 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
1794 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01001795 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06001796
Keith Busch0877cb02013-07-15 15:02:19 -06001797 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1798 if (!dev->bar)
1799 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07001800
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001801 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07001802 result = -ENODEV;
1803 goto unmap;
1804 }
Jens Axboee32efbf2014-11-14 09:49:26 -07001805
1806 /*
1807 * Some devices don't advertse INTx interrupts, pre-enable a single
1808 * MSIX vec for setup. We'll adjust this later.
1809 */
1810 if (!pdev->irq) {
1811 result = pci_enable_msix(pdev, dev->entry, 1);
1812 if (result < 0)
1813 goto unmap;
1814 }
1815
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001816 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
1817
Keith Busch42f61422014-03-24 10:46:25 -06001818 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
1819 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001820 dev->dbs = dev->bar + 4096;
1821 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2))
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001822 dev->cmb = nvme_map_cmb(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001823
1824 return 0;
1825
Keith Busch0e53d182013-12-10 13:10:39 -07001826 unmap:
1827 iounmap(dev->bar);
1828 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06001829 disable:
1830 pci_release_regions(pdev);
1831 disable_pci:
1832 pci_disable_device(pdev);
1833 return result;
1834}
1835
1836static void nvme_dev_unmap(struct nvme_dev *dev)
1837{
Christoph Hellwige75ec752015-05-22 11:12:39 +02001838 struct pci_dev *pdev = to_pci_dev(dev->dev);
1839
1840 if (pdev->msi_enabled)
1841 pci_disable_msi(pdev);
1842 else if (pdev->msix_enabled)
1843 pci_disable_msix(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001844
1845 if (dev->bar) {
1846 iounmap(dev->bar);
1847 dev->bar = NULL;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001848 pci_release_regions(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001849 }
1850
Christoph Hellwige75ec752015-05-22 11:12:39 +02001851 if (pci_is_enabled(pdev))
1852 pci_disable_device(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001853}
1854
Keith Busch4d115422013-12-10 13:10:40 -07001855struct nvme_delq_ctx {
1856 struct task_struct *waiter;
1857 struct kthread_worker *worker;
1858 atomic_t refcount;
1859};
1860
1861static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
1862{
1863 dq->waiter = current;
1864 mb();
1865
1866 for (;;) {
1867 set_current_state(TASK_KILLABLE);
1868 if (!atomic_read(&dq->refcount))
1869 break;
1870 if (!schedule_timeout(ADMIN_TIMEOUT) ||
1871 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07001872 /*
1873 * Disable the controller first since we can't trust it
1874 * at this point, but leave the admin queue enabled
1875 * until all queue deletion requests are flushed.
1876 * FIXME: This may take a while if there are more h/w
1877 * queues than admin tags.
1878 */
Keith Busch4d115422013-12-10 13:10:40 -07001879 set_current_state(TASK_RUNNING);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001880 nvme_disable_ctrl(&dev->ctrl,
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001881 lo_hi_readq(dev->bar + NVME_REG_CAP));
Keith Busch0fb59cb2015-01-07 18:55:50 -07001882 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07001883 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07001884 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07001885 return;
1886 }
1887 }
1888 set_current_state(TASK_RUNNING);
1889}
1890
1891static void nvme_put_dq(struct nvme_delq_ctx *dq)
1892{
1893 atomic_dec(&dq->refcount);
1894 if (dq->waiter)
1895 wake_up_process(dq->waiter);
1896}
1897
1898static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
1899{
1900 atomic_inc(&dq->refcount);
1901 return dq;
1902}
1903
1904static void nvme_del_queue_end(struct nvme_queue *nvmeq)
1905{
1906 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
Keith Busch4d115422013-12-10 13:10:40 -07001907 nvme_put_dq(dq);
Keith Busch604e8c82015-11-20 08:38:13 -07001908
1909 spin_lock_irq(&nvmeq->q_lock);
1910 nvme_process_cq(nvmeq);
1911 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001912}
1913
1914static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
1915 kthread_work_func_t fn)
1916{
1917 struct nvme_command c;
1918
1919 memset(&c, 0, sizeof(c));
1920 c.delete_queue.opcode = opcode;
1921 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
1922
1923 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001924 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
1925 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07001926}
1927
1928static void nvme_del_cq_work_handler(struct kthread_work *work)
1929{
1930 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1931 cmdinfo.work);
1932 nvme_del_queue_end(nvmeq);
1933}
1934
1935static int nvme_delete_cq(struct nvme_queue *nvmeq)
1936{
1937 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
1938 nvme_del_cq_work_handler);
1939}
1940
1941static void nvme_del_sq_work_handler(struct kthread_work *work)
1942{
1943 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1944 cmdinfo.work);
1945 int status = nvmeq->cmdinfo.status;
1946
1947 if (!status)
1948 status = nvme_delete_cq(nvmeq);
1949 if (status)
1950 nvme_del_queue_end(nvmeq);
1951}
1952
1953static int nvme_delete_sq(struct nvme_queue *nvmeq)
1954{
1955 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
1956 nvme_del_sq_work_handler);
1957}
1958
1959static void nvme_del_queue_start(struct kthread_work *work)
1960{
1961 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1962 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07001963 if (nvme_delete_sq(nvmeq))
1964 nvme_del_queue_end(nvmeq);
1965}
1966
1967static void nvme_disable_io_queues(struct nvme_dev *dev)
1968{
1969 int i;
1970 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
1971 struct nvme_delq_ctx dq;
1972 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001973 &worker, "nvme%d", dev->ctrl.instance);
Keith Busch4d115422013-12-10 13:10:40 -07001974
1975 if (IS_ERR(kworker_task)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001976 dev_err(dev->dev,
Keith Busch4d115422013-12-10 13:10:40 -07001977 "Failed to create queue del task\n");
1978 for (i = dev->queue_count - 1; i > 0; i--)
1979 nvme_disable_queue(dev, i);
1980 return;
1981 }
1982
1983 dq.waiter = NULL;
1984 atomic_set(&dq.refcount, 0);
1985 dq.worker = &worker;
1986 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001987 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07001988
1989 if (nvme_suspend_queue(nvmeq))
1990 continue;
1991 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
1992 nvmeq->cmdinfo.worker = dq.worker;
1993 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
1994 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
1995 }
1996 nvme_wait_dq(&dq, dev);
1997 kthread_stop(kworker_task);
1998}
1999
Dan McLeranb9afca32014-04-07 17:10:11 -06002000/*
2001* Remove the node from the device list and check
2002* for whether or not we need to stop the nvme_thread.
2003*/
2004static void nvme_dev_list_remove(struct nvme_dev *dev)
2005{
2006 struct task_struct *tmp = NULL;
2007
2008 spin_lock(&dev_list_lock);
2009 list_del_init(&dev->node);
2010 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2011 tmp = nvme_thread;
2012 nvme_thread = NULL;
2013 }
2014 spin_unlock(&dev_list_lock);
2015
2016 if (tmp)
2017 kthread_stop(tmp);
2018}
2019
Keith Buschc9d3bf82015-01-07 18:55:52 -07002020static void nvme_freeze_queues(struct nvme_dev *dev)
2021{
2022 struct nvme_ns *ns;
2023
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002024 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
Keith Buschc9d3bf82015-01-07 18:55:52 -07002025 blk_mq_freeze_queue_start(ns->queue);
2026
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002027 spin_lock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002028 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002029 spin_unlock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002030
2031 blk_mq_cancel_requeue_work(ns->queue);
2032 blk_mq_stop_hw_queues(ns->queue);
2033 }
2034}
2035
2036static void nvme_unfreeze_queues(struct nvme_dev *dev)
2037{
2038 struct nvme_ns *ns;
2039
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002040 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
Keith Buschc9d3bf82015-01-07 18:55:52 -07002041 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2042 blk_mq_unfreeze_queue(ns->queue);
2043 blk_mq_start_stopped_hw_queues(ns->queue, true);
2044 blk_mq_kick_requeue_list(ns->queue);
2045 }
2046}
2047
Keith Buschf0b50732013-07-15 15:02:21 -06002048static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002049{
Keith Busch22404272013-07-15 15:02:20 -06002050 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002051 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002052
Dan McLeranb9afca32014-04-07 17:10:11 -06002053 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002054
Keith Buschc9d3bf82015-01-07 18:55:52 -07002055 if (dev->bar) {
2056 nvme_freeze_queues(dev);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002057 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002058 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002059 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002060 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002061 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002062 nvme_suspend_queue(nvmeq);
Keith Busch4d115422013-12-10 13:10:40 -07002063 }
2064 } else {
2065 nvme_disable_io_queues(dev);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002066 nvme_shutdown_ctrl(&dev->ctrl);
Keith Busch4d115422013-12-10 13:10:40 -07002067 nvme_disable_queue(dev, 0);
2068 }
Keith Buschf0b50732013-07-15 15:02:21 -06002069 nvme_dev_unmap(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002070
2071 for (i = dev->queue_count - 1; i >= 0; i--)
2072 nvme_clear_queue(dev->queues[i]);
Keith Buschf0b50732013-07-15 15:02:21 -06002073}
2074
Matthew Wilcox091b6092011-02-10 09:56:01 -05002075static int nvme_setup_prp_pools(struct nvme_dev *dev)
2076{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002077 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002078 PAGE_SIZE, PAGE_SIZE, 0);
2079 if (!dev->prp_page_pool)
2080 return -ENOMEM;
2081
Matthew Wilcox99802a72011-02-10 10:30:34 -05002082 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002083 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002084 256, 256, 0);
2085 if (!dev->prp_small_pool) {
2086 dma_pool_destroy(dev->prp_page_pool);
2087 return -ENOMEM;
2088 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002089 return 0;
2090}
2091
2092static void nvme_release_prp_pools(struct nvme_dev *dev)
2093{
2094 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002095 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002096}
2097
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002098static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
Keith Busch5e82e952013-02-19 10:17:58 -07002099{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002100 struct nvme_dev *dev = to_nvme_dev(ctrl);
Keith Busch9ac27092014-01-31 16:53:39 -07002101
Christoph Hellwige75ec752015-05-22 11:12:39 +02002102 put_device(dev->dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002103 if (dev->tagset.tags)
2104 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002105 if (dev->ctrl.admin_q)
2106 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002107 kfree(dev->queues);
2108 kfree(dev->entry);
2109 kfree(dev);
2110}
2111
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002112static void nvme_probe_work(struct work_struct *work)
Keith Buschf0b50732013-07-15 15:02:21 -06002113{
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002114 struct nvme_dev *dev = container_of(work, struct nvme_dev, probe_work);
Dan McLeranb9afca32014-04-07 17:10:11 -06002115 bool start_thread = false;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002116 int result;
Keith Buschf0b50732013-07-15 15:02:21 -06002117
2118 result = nvme_dev_map(dev);
2119 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002120 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002121
2122 result = nvme_configure_admin_queue(dev);
2123 if (result)
2124 goto unmap;
2125
2126 spin_lock(&dev_list_lock);
Dan McLeranb9afca32014-04-07 17:10:11 -06002127 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2128 start_thread = true;
2129 nvme_thread = NULL;
2130 }
Keith Buschf0b50732013-07-15 15:02:21 -06002131 list_add(&dev->node, &dev_list);
2132 spin_unlock(&dev_list_lock);
2133
Dan McLeranb9afca32014-04-07 17:10:11 -06002134 if (start_thread) {
2135 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
Keith Busch387caa52014-09-22 13:46:19 -06002136 wake_up_all(&nvme_kthread_wait);
Dan McLeranb9afca32014-04-07 17:10:11 -06002137 } else
2138 wait_event_killable(nvme_kthread_wait, nvme_thread);
2139
2140 if (IS_ERR_OR_NULL(nvme_thread)) {
2141 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2142 goto disable;
2143 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002144
2145 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002146 result = nvme_alloc_admin_tags(dev);
2147 if (result)
2148 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06002149
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002150 result = nvme_init_identify(&dev->ctrl);
2151 if (result)
2152 goto free_tags;
2153
Keith Buschf0b50732013-07-15 15:02:21 -06002154 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002155 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07002156 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06002157
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002158 dev->ctrl.event_limit = 1;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002159
Christoph Hellwig2659e572015-10-02 18:51:31 +02002160 /*
2161 * Keep the controller around but remove all namespaces if we don't have
2162 * any working I/O queue.
2163 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002164 if (dev->online_queues < 2) {
2165 dev_warn(dev->dev, "IO queues not created\n");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002166 nvme_remove_namespaces(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002167 } else {
2168 nvme_unfreeze_queues(dev);
2169 nvme_dev_add(dev);
2170 }
2171
2172 return;
Keith Buschf0b50732013-07-15 15:02:21 -06002173
Keith Busch0fb59cb2015-01-07 18:55:50 -07002174 free_tags:
2175 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002176 blk_put_queue(dev->ctrl.admin_q);
2177 dev->ctrl.admin_q = NULL;
Keith Busch4af0e212015-06-08 10:08:13 -06002178 dev->queues[0]->tags = NULL;
Keith Buschf0b50732013-07-15 15:02:21 -06002179 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002180 nvme_disable_queue(dev, 0);
Dan McLeranb9afca32014-04-07 17:10:11 -06002181 nvme_dev_list_remove(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002182 unmap:
2183 nvme_dev_unmap(dev);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002184 out:
2185 if (!work_busy(&dev->reset_work))
2186 nvme_dead_ctrl(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002187}
2188
Keith Busch9a6b9452013-12-10 13:10:36 -07002189static int nvme_remove_dead_ctrl(void *arg)
2190{
2191 struct nvme_dev *dev = (struct nvme_dev *)arg;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002192 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002193
2194 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002195 pci_stop_and_remove_bus_device_locked(pdev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002196 nvme_put_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002197 return 0;
2198}
2199
Keith Buschde3eff22015-06-18 13:36:39 -06002200static void nvme_dead_ctrl(struct nvme_dev *dev)
2201{
2202 dev_warn(dev->dev, "Device failed to resume\n");
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002203 kref_get(&dev->ctrl.kref);
Keith Buschde3eff22015-06-18 13:36:39 -06002204 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002205 dev->ctrl.instance))) {
Keith Buschde3eff22015-06-18 13:36:39 -06002206 dev_err(dev->dev,
2207 "Failed to start controller remove task\n");
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002208 nvme_put_ctrl(&dev->ctrl);
Keith Buschde3eff22015-06-18 13:36:39 -06002209 }
2210}
2211
Christoph Hellwig77b50d92015-10-02 17:41:18 +02002212static void nvme_reset_work(struct work_struct *ws)
Keith Busch9a6b9452013-12-10 13:10:36 -07002213{
Christoph Hellwig77b50d92015-10-02 17:41:18 +02002214 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
Keith Buschffe77042015-06-08 10:08:15 -06002215 bool in_probe = work_busy(&dev->probe_work);
2216
Keith Busch9a6b9452013-12-10 13:10:36 -07002217 nvme_dev_shutdown(dev);
Keith Buschffe77042015-06-08 10:08:15 -06002218
2219 /* Synchronize with device probe so that work will see failure status
2220 * and exit gracefully without trying to schedule another reset */
2221 flush_work(&dev->probe_work);
2222
2223 /* Fail this device if reset occured during probe to avoid
2224 * infinite initialization loops. */
2225 if (in_probe) {
Keith Buschde3eff22015-06-18 13:36:39 -06002226 nvme_dead_ctrl(dev);
Keith Buschffe77042015-06-08 10:08:15 -06002227 return;
Keith Busch9a6b9452013-12-10 13:10:36 -07002228 }
Keith Buschffe77042015-06-08 10:08:15 -06002229 /* Schedule device resume asynchronously so the reset work is available
2230 * to cleanup errors that may occur during reinitialization */
2231 schedule_work(&dev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002232}
2233
Christoph Hellwig906678922015-10-02 18:49:23 +02002234static int __nvme_reset(struct nvme_dev *dev)
Keith Busch9a6b9452013-12-10 13:10:36 -07002235{
Christoph Hellwig906678922015-10-02 18:49:23 +02002236 if (work_pending(&dev->reset_work))
2237 return -EBUSY;
2238 list_del_init(&dev->node);
2239 queue_work(nvme_workq, &dev->reset_work);
2240 return 0;
Tejun Heo9ca97372014-03-07 10:24:49 -05002241}
2242
Keith Busch4cc06522015-06-05 10:30:08 -06002243static int nvme_reset(struct nvme_dev *dev)
2244{
Christoph Hellwig906678922015-10-02 18:49:23 +02002245 int ret;
Keith Busch4cc06522015-06-05 10:30:08 -06002246
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002247 if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q))
Keith Busch4cc06522015-06-05 10:30:08 -06002248 return -ENODEV;
2249
2250 spin_lock(&dev_list_lock);
Christoph Hellwig906678922015-10-02 18:49:23 +02002251 ret = __nvme_reset(dev);
Keith Busch4cc06522015-06-05 10:30:08 -06002252 spin_unlock(&dev_list_lock);
2253
2254 if (!ret) {
2255 flush_work(&dev->reset_work);
Keith Buschffe77042015-06-08 10:08:15 -06002256 flush_work(&dev->probe_work);
Keith Busch4cc06522015-06-05 10:30:08 -06002257 return 0;
2258 }
2259
2260 return ret;
2261}
2262
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002263static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
2264{
2265 *val = readl(to_nvme_dev(ctrl)->bar + off);
2266 return 0;
2267}
2268
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002269static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2270{
2271 writel(val, to_nvme_dev(ctrl)->bar + off);
2272 return 0;
2273}
2274
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002275static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2276{
2277 *val = readq(to_nvme_dev(ctrl)->bar + off);
2278 return 0;
2279}
2280
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002281static bool nvme_pci_io_incapable(struct nvme_ctrl *ctrl)
2282{
2283 struct nvme_dev *dev = to_nvme_dev(ctrl);
2284
2285 return !dev->bar || dev->online_queues < 2;
2286}
2287
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002288static int nvme_pci_reset_ctrl(struct nvme_ctrl *ctrl)
2289{
2290 return nvme_reset(to_nvme_dev(ctrl));
2291}
2292
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002293static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
2294 .reg_read32 = nvme_pci_reg_read32,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002295 .reg_write32 = nvme_pci_reg_write32,
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002296 .reg_read64 = nvme_pci_reg_read64,
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002297 .io_incapable = nvme_pci_io_incapable,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002298 .reset_ctrl = nvme_pci_reset_ctrl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002299 .free_ctrl = nvme_pci_free_ctrl,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002300};
2301
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002302static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002303{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002304 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002305 struct nvme_dev *dev;
2306
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002307 node = dev_to_node(&pdev->dev);
2308 if (node == NUMA_NO_NODE)
2309 set_dev_node(&pdev->dev, 0);
2310
2311 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002312 if (!dev)
2313 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002314 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2315 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002316 if (!dev->entry)
2317 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002318 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2319 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002320 if (!dev->queues)
2321 goto free;
2322
Christoph Hellwige75ec752015-05-22 11:12:39 +02002323 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002324 pci_set_drvdata(pdev, dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002325
Keith Busche6e96d72015-03-23 09:32:37 -06002326 INIT_LIST_HEAD(&dev->node);
Keith Buscha5768aa2015-06-01 14:28:14 -06002327 INIT_WORK(&dev->scan_work, nvme_dev_scan);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002328 INIT_WORK(&dev->probe_work, nvme_probe_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002329 INIT_WORK(&dev->reset_work, nvme_reset_work);
2330
2331 result = nvme_setup_prp_pools(dev);
2332 if (result)
2333 goto put_pci;
2334
2335 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
2336 id->driver_data);
2337 if (result)
2338 goto release_pools;
2339
Keith Busch2e1d8442015-02-12 15:33:00 -07002340 schedule_work(&dev->probe_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002341 return 0;
2342
Keith Busch0877cb02013-07-15 15:02:19 -06002343 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002344 nvme_release_prp_pools(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002345 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002346 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002347 free:
2348 kfree(dev->queues);
2349 kfree(dev->entry);
2350 kfree(dev);
2351 return result;
2352}
2353
Keith Buschf0d54a52014-05-02 10:40:43 -06002354static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2355{
Keith Buscha6739472014-06-23 16:03:21 -06002356 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002357
Keith Buscha6739472014-06-23 16:03:21 -06002358 if (prepare)
2359 nvme_dev_shutdown(dev);
2360 else
Keith Busch0a7385a2015-10-02 10:37:29 -06002361 schedule_work(&dev->probe_work);
Keith Buschf0d54a52014-05-02 10:40:43 -06002362}
2363
Keith Busch09ece142014-01-27 11:29:40 -05002364static void nvme_shutdown(struct pci_dev *pdev)
2365{
2366 struct nvme_dev *dev = pci_get_drvdata(pdev);
2367 nvme_dev_shutdown(dev);
2368}
2369
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002370static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002371{
2372 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002373
2374 spin_lock(&dev_list_lock);
2375 list_del_init(&dev->node);
2376 spin_unlock(&dev_list_lock);
2377
2378 pci_set_drvdata(pdev, NULL);
Keith Busch2e1d8442015-02-12 15:33:00 -07002379 flush_work(&dev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002380 flush_work(&dev->reset_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06002381 flush_work(&dev->scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002382 nvme_remove_namespaces(&dev->ctrl);
Keith Busch3399a3f2015-06-18 13:36:40 -06002383 nvme_dev_shutdown(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002384 nvme_dev_remove_admin(dev);
2385 nvme_free_queues(dev, 0);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002386 nvme_release_cmb(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002387 nvme_release_prp_pools(dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002388 nvme_put_ctrl(&dev->ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002389}
2390
2391/* These functions are yet to be implemented */
2392#define nvme_error_detected NULL
2393#define nvme_dump_registers NULL
2394#define nvme_link_reset NULL
2395#define nvme_slot_reset NULL
2396#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002397
Jingoo Han671a6012014-02-13 11:19:14 +09002398#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002399static int nvme_suspend(struct device *dev)
2400{
2401 struct pci_dev *pdev = to_pci_dev(dev);
2402 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2403
2404 nvme_dev_shutdown(ndev);
2405 return 0;
2406}
2407
2408static int nvme_resume(struct device *dev)
2409{
2410 struct pci_dev *pdev = to_pci_dev(dev);
2411 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002412
Keith Busch0a7385a2015-10-02 10:37:29 -06002413 schedule_work(&ndev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002414 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002415}
Jingoo Han671a6012014-02-13 11:19:14 +09002416#endif
Keith Buschcd638942013-07-15 15:02:23 -06002417
2418static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002419
Stephen Hemminger1d352032012-09-07 09:33:17 -07002420static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002421 .error_detected = nvme_error_detected,
2422 .mmio_enabled = nvme_dump_registers,
2423 .link_reset = nvme_link_reset,
2424 .slot_reset = nvme_slot_reset,
2425 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002426 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002427};
2428
2429/* Move to pci_ids.h later */
2430#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2431
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002432static const struct pci_device_id nvme_id_table[] = {
Christoph Hellwig106198e2015-11-26 10:07:41 +01002433 { PCI_VDEVICE(INTEL, 0x0953),
2434 .driver_data = NVME_QUIRK_STRIPE_SIZE, },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002435 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01002436 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002437 { 0, }
2438};
2439MODULE_DEVICE_TABLE(pci, nvme_id_table);
2440
2441static struct pci_driver nvme_driver = {
2442 .name = "nvme",
2443 .id_table = nvme_id_table,
2444 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002445 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002446 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002447 .driver = {
2448 .pm = &nvme_dev_pm_ops,
2449 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002450 .err_handler = &nvme_err_handler,
2451};
2452
2453static int __init nvme_init(void)
2454{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002455 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002456
Dan McLeranb9afca32014-04-07 17:10:11 -06002457 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002458
Keith Busch9a6b9452013-12-10 13:10:36 -07002459 nvme_workq = create_singlethread_workqueue("nvme");
2460 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002461 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002462
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002463 result = nvme_core_init();
Keith Busch5c42ea12012-07-25 16:05:18 -06002464 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002465 goto kill_workq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002466
Keith Buschf3db22f2014-06-11 11:51:35 -06002467 result = pci_register_driver(&nvme_driver);
2468 if (result)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002469 goto core_exit;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002470 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002471
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002472 core_exit:
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002473 nvme_core_exit();
Keith Busch9a6b9452013-12-10 13:10:36 -07002474 kill_workq:
2475 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002476 return result;
2477}
2478
2479static void __exit nvme_exit(void)
2480{
2481 pci_unregister_driver(&nvme_driver);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002482 nvme_core_exit();
Keith Busch9a6b9452013-12-10 13:10:36 -07002483 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06002484 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002485 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002486}
2487
2488MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2489MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002490MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002491module_init(nvme_init);
2492module_exit(nvme_exit);