blob: febcef5ae0aad70d05f309156643cf2b613820c8 [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003 * Copyright (c) 2011-2014, Intel Corporation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050013 */
14
Matthew Wilcox8de05532011-05-12 13:50:28 -040015#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050016#include <linux/blkdev.h>
Matias Bjørlinga4aea562014-11-04 08:20:14 -070017#include <linux/blk-mq.h>
Keith Busch42f61422014-03-24 10:46:25 -060018#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040019#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050020#include <linux/errno.h>
21#include <linux/fs.h>
22#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060023#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040024#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050025#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050029#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050030#include <linux/kernel.h>
31#include <linux/mm.h>
32#include <linux/module.h>
33#include <linux/moduleparam.h>
Keith Busch77bf25e2015-11-26 12:21:29 +010034#include <linux/mutex.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050035#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050036#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040037#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050038#include <linux/sched.h>
39#include <linux/slab.h>
Keith Busche1e5e562015-02-19 13:39:03 -070040#include <linux/t10-pi.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050041#include <linux/types.h>
Linus Torvalds9cf5c092015-11-06 14:22:15 -080042#include <linux/io-64-nonatomic-lo-hi.h>
Keith Busch1d277a62015-10-15 14:10:52 +020043#include <asm/unaligned.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090044
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020045#include "nvme.h"
46
Keith Busch9d43cf62014-05-13 11:42:02 -060047#define NVME_Q_DEPTH 1024
Jens Axboed31af0a2015-03-06 12:56:13 -070048#define NVME_AQ_DEPTH 256
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050049#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
50#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050051
Christoph Hellwig21d34712015-11-26 09:08:36 +010052unsigned char admin_timeout = 60;
Keith Busch9d43cf62014-05-13 11:42:02 -060053module_param(admin_timeout, byte, 0644);
54MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050055
Matthew Wilcoxbd676082014-06-03 23:04:30 -040056unsigned char nvme_io_timeout = 30;
57module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060058MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050059
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +010060unsigned char shutdown_timeout = 5;
Dan McLeran2484f402014-07-01 09:33:32 -060061module_param(shutdown_timeout, byte, 0644);
62MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
63
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050064static int use_threaded_interrupts;
65module_param(use_threaded_interrupts, int, 0);
66
Jon Derrick8ffaadf2015-07-20 10:14:09 -060067static bool use_cmb_sqes = true;
68module_param(use_cmb_sqes, bool, 0644);
69MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
70
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050071static LIST_HEAD(dev_list);
72static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070073static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060074static wait_queue_head_t nvme_kthread_wait;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050075
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010076struct nvme_dev;
77struct nvme_queue;
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +010078struct nvme_iod;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010079
Keith Busch4cc06522015-06-05 10:30:08 -060080static int nvme_reset(struct nvme_dev *dev);
Jens Axboea0fa9642015-11-03 20:37:26 -070081static void nvme_process_cq(struct nvme_queue *nvmeq);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +010082static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +020083static void nvme_dead_ctrl(struct nvme_dev *dev);
Keith 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;
Keith Busch77bf25e2015-11-26 12:21:29 +0100116 struct mutex shutdown_lock;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100117 bool subsystem;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100118 void __iomem *cmb;
119 dma_addr_t cmb_dma_addr;
120 u64 cmb_size;
121 u32 cmbsz;
122
123 struct nvme_ctrl ctrl;
124};
125
126static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
127{
128 return container_of(ctrl, struct nvme_dev, ctrl);
129}
130
131/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500132 * An NVM Express queue. Each device has at least two (one for admin
133 * commands and one for I/O commands).
134 */
135struct nvme_queue {
136 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500137 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500138 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500139 spinlock_t q_lock;
140 struct nvme_command *sq_cmds;
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600141 struct nvme_command __iomem *sq_cmds_io;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500142 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600143 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500144 dma_addr_t sq_dma_addr;
145 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500146 u32 __iomem *q_db;
147 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700148 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500149 u16 sq_head;
150 u16 sq_tail;
151 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700152 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400153 u8 cq_phase;
154 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700155 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500156};
157
158/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200159 * The nvme_iod describes the data in an I/O, including the list of PRP
160 * entries. You can't see it in this data structure because C doesn't let
161 * me express that. Use nvme_alloc_iod to ensure there's enough space
162 * allocated to store the PRP list.
163 */
164struct nvme_iod {
165 unsigned long private; /* For the use of the submitter of the I/O */
166 int npages; /* In the PRP list. 0 means small pool in use */
167 int offset; /* Of PRP list */
168 int nents; /* Used in scatterlist */
169 int length; /* Of data, in bytes */
170 dma_addr_t first_dma;
171 struct scatterlist meta_sg[1]; /* metadata requires single contiguous buffer */
172 struct scatterlist sg[0];
173};
174
175/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500176 * Check we didin't inadvertently grow the command struct
177 */
178static inline void _nvme_check_size(void)
179{
180 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
181 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
182 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
183 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
184 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400185 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700186 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500187 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
188 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
189 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
190 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600191 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500192}
193
Keith Buschedd10d32014-04-03 16:45:23 -0600194typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400195 struct nvme_completion *);
196
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500197struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400198 nvme_completion_fn fn;
199 void *ctx;
Keith Buschc30341d2013-12-10 13:10:38 -0700200 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700201 struct nvme_queue *nvmeq;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700202 struct nvme_iod iod[0];
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500203};
204
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700205/*
206 * Max size of iod being embedded in the request payload
207 */
208#define NVME_INT_PAGES 2
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100209#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
Chong Yuanfda631f2015-03-27 09:21:32 +0800210#define NVME_INT_MASK 0x01
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700211
212/*
213 * Will slightly overestimate the number of pages needed. This is OK
214 * as it only leads to a small amount of wasted memory for the lifetime of
215 * the I/O.
216 */
217static int nvme_npages(unsigned size, struct nvme_dev *dev)
218{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100219 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
220 dev->ctrl.page_size);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700221 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
222}
223
224static unsigned int nvme_cmd_size(struct nvme_dev *dev)
225{
226 unsigned int ret = sizeof(struct nvme_cmd_info);
227
228 ret += sizeof(struct nvme_iod);
229 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
230 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
231
232 return ret;
233}
234
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700235static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
236 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500237{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700238 struct nvme_dev *dev = data;
239 struct nvme_queue *nvmeq = dev->queues[0];
240
Keith Busch42483222015-06-01 09:29:54 -0600241 WARN_ON(hctx_idx != 0);
242 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
243 WARN_ON(nvmeq->tags);
244
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700245 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600246 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700247 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500248}
249
Keith Busch4af0e212015-06-08 10:08:13 -0600250static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
251{
252 struct nvme_queue *nvmeq = hctx->driver_data;
253
254 nvmeq->tags = NULL;
255}
256
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700257static int nvme_admin_init_request(void *data, struct request *req,
258 unsigned int hctx_idx, unsigned int rq_idx,
259 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600260{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700261 struct nvme_dev *dev = data;
262 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
263 struct nvme_queue *nvmeq = dev->queues[0];
264
265 BUG_ON(!nvmeq);
266 cmd->nvmeq = nvmeq;
267 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600268}
269
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700270static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
271 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500272{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700273 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600274 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500275
Keith Busch42483222015-06-01 09:29:54 -0600276 if (!nvmeq->tags)
277 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500278
Keith Busch42483222015-06-01 09:29:54 -0600279 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700280 hctx->driver_data = nvmeq;
281 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500282}
283
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700284static int nvme_init_request(void *data, struct request *req,
285 unsigned int hctx_idx, unsigned int rq_idx,
286 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500287{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700288 struct nvme_dev *dev = data;
289 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
290 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
291
292 BUG_ON(!nvmeq);
293 cmd->nvmeq = nvmeq;
294 return 0;
295}
296
297static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
298 nvme_completion_fn handler)
299{
300 cmd->fn = handler;
301 cmd->ctx = ctx;
302 cmd->aborted = 0;
Keith Buschc917dfe2015-01-07 18:55:48 -0700303 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500304}
305
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700306static void *iod_get_private(struct nvme_iod *iod)
307{
308 return (void *) (iod->private & ~0x1UL);
309}
310
311/*
312 * If bit 0 is set, the iod is embedded in the request payload.
313 */
314static bool iod_should_kfree(struct nvme_iod *iod)
315{
Chong Yuanfda631f2015-03-27 09:21:32 +0800316 return (iod->private & NVME_INT_MASK) == 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700317}
318
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400319/* Special values must be less than 0x1000 */
320#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500321#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
322#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
323#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500324
Keith Buschedd10d32014-04-03 16:45:23 -0600325static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400326 struct nvme_completion *cqe)
327{
328 if (ctx == CMD_CTX_CANCELLED)
329 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400330 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600331 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400332 "completed id %d twice on queue %d\n",
333 cqe->command_id, le16_to_cpup(&cqe->sq_id));
334 return;
335 }
336 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600337 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400338 "invalid id %d completed on queue %d\n",
339 cqe->command_id, le16_to_cpup(&cqe->sq_id));
340 return;
341 }
Keith Buschedd10d32014-04-03 16:45:23 -0600342 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400343}
344
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700345static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
346{
347 void *ctx;
348
349 if (fn)
350 *fn = cmd->fn;
351 ctx = cmd->ctx;
352 cmd->fn = special_completion;
353 cmd->ctx = CMD_CTX_CANCELLED;
354 return ctx;
355}
356
357static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
358 struct nvme_completion *cqe)
359{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700360 u32 result = le32_to_cpup(&cqe->result);
361 u16 status = le16_to_cpup(&cqe->status) >> 1;
362
363 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100364 ++nvmeq->dev->ctrl.event_limit;
Keith Buscha5768aa2015-06-01 14:28:14 -0600365 if (status != NVME_SC_SUCCESS)
366 return;
367
368 switch (result & 0xff07) {
369 case NVME_AER_NOTICE_NS_CHANGED:
370 dev_info(nvmeq->q_dmadev, "rescanning\n");
371 schedule_work(&nvmeq->dev->scan_work);
372 default:
373 dev_warn(nvmeq->q_dmadev, "async event result %08x\n", result);
374 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700375}
376
377static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
378 struct nvme_completion *cqe)
379{
380 struct request *req = ctx;
381
382 u16 status = le16_to_cpup(&cqe->status) >> 1;
383 u32 result = le32_to_cpup(&cqe->result);
384
Keith Busch42483222015-06-01 09:29:54 -0600385 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700386
387 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100388 ++nvmeq->dev->ctrl.abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700389}
390
Keith Buschedd10d32014-04-03 16:45:23 -0600391static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700392 struct nvme_completion *cqe)
393{
394 struct async_cmd_info *cmdinfo = ctx;
395 cmdinfo->result = le32_to_cpup(&cqe->result);
396 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
397 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
Keith Busch42483222015-06-01 09:29:54 -0600398 blk_mq_free_request(cmdinfo->req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700399}
400
401static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
402 unsigned int tag)
403{
Keith Busch42483222015-06-01 09:29:54 -0600404 struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700405
406 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700407}
408
Matthew Wilcox184d2942011-05-11 21:36:38 -0400409/*
410 * Called with local interrupts disabled and the q_lock held. May not sleep.
411 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700412static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400413 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500414{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700415 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400416 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700417 if (tag >= nvmeq->q_depth) {
418 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500419 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400420 }
Keith Busch859361a2012-08-02 14:05:59 -0600421 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700422 *fn = cmd->fn;
423 ctx = cmd->ctx;
424 cmd->fn = special_completion;
425 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400426 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500427}
428
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500429/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400430 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500431 * @nvmeq: The queue to use
432 * @cmd: The command to send
433 *
434 * Safe to use from interrupt context
435 */
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530436static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
437 struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500438{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700439 u16 tail = nvmeq->sq_tail;
440
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600441 if (nvmeq->sq_cmds_io)
442 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
443 else
444 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
445
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500446 if (++tail == nvmeq->q_depth)
447 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500448 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500449 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500450}
451
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530452static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700453{
454 unsigned long flags;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700455 spin_lock_irqsave(&nvmeq->q_lock, flags);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530456 __nvme_submit_cmd(nvmeq, cmd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700457 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700458}
459
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500460static __le64 **iod_list(struct nvme_iod *iod)
461{
462 return ((void *)iod) + iod->offset;
463}
464
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700465static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
466 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500467{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700468 iod->private = private;
469 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
470 iod->npages = -1;
471 iod->length = nbytes;
472 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500473}
474
475static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700476__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
477 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500478{
479 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700480 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500481 sizeof(struct scatterlist) * nseg, gfp);
482
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700483 if (iod)
484 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500485
486 return iod;
487}
488
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700489static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
490 gfp_t gfp)
491{
492 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
493 sizeof(struct nvme_dsm_range);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700494 struct nvme_iod *iod;
495
496 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
497 size <= NVME_INT_BYTES(dev)) {
498 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
499
500 iod = cmd->iod;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700501 iod_init(iod, size, rq->nr_phys_segments,
Chong Yuanfda631f2015-03-27 09:21:32 +0800502 (unsigned long) rq | NVME_INT_MASK);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700503 return iod;
504 }
505
506 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
507 (unsigned long) rq, gfp);
508}
509
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200510static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500511{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100512 const int last_prp = dev->ctrl.page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500513 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500514 __le64 **list = iod_list(iod);
515 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500516
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500517 if (iod->npages == 0)
518 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
519 for (i = 0; i < iod->npages; i++) {
520 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500521 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500522 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500523 prp_dma = next_prp_dma;
524 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700525
526 if (iod_should_kfree(iod))
527 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500528}
529
Keith Busch52b68d72015-02-23 09:16:21 -0700530#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700531static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
532{
533 if (be32_to_cpu(pi->ref_tag) == v)
534 pi->ref_tag = cpu_to_be32(p);
535}
536
537static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
538{
539 if (be32_to_cpu(pi->ref_tag) == p)
540 pi->ref_tag = cpu_to_be32(v);
541}
542
543/**
544 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
545 *
546 * The virtual start sector is the one that was originally submitted by the
547 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
548 * start sector may be different. Remap protection information to match the
549 * physical LBA on writes, and back to the original seed on reads.
550 *
551 * Type 0 and 3 do not have a ref tag, so no remapping required.
552 */
553static void nvme_dif_remap(struct request *req,
554 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
555{
556 struct nvme_ns *ns = req->rq_disk->private_data;
557 struct bio_integrity_payload *bip;
558 struct t10_pi_tuple *pi;
559 void *p, *pmap;
560 u32 i, nlb, ts, phys, virt;
561
562 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
563 return;
564
565 bip = bio_integrity(req->bio);
566 if (!bip)
567 return;
568
569 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700570
571 p = pmap;
572 virt = bip_get_seed(bip);
573 phys = nvme_block_nr(ns, blk_rq_pos(req));
574 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
Dan Williamsac6fc482015-10-21 13:20:18 -0400575 ts = ns->disk->queue->integrity.tuple_size;
Keith Busche1e5e562015-02-19 13:39:03 -0700576
577 for (i = 0; i < nlb; i++, virt++, phys++) {
578 pi = (struct t10_pi_tuple *)p;
579 dif_swap(phys, virt, pi);
580 p += ts;
581 }
582 kunmap_atomic(pmap);
583}
Keith Busch52b68d72015-02-23 09:16:21 -0700584#else /* CONFIG_BLK_DEV_INTEGRITY */
585static void nvme_dif_remap(struct request *req,
586 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
587{
588}
589static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
590{
591}
592static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
593{
594}
Keith Busch52b68d72015-02-23 09:16:21 -0700595#endif
596
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700597static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500598 struct nvme_completion *cqe)
599{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500600 struct nvme_iod *iod = ctx;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700601 struct request *req = iod_get_private(iod);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700602 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500603 u16 status = le16_to_cpup(&cqe->status) >> 1;
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200604 int error = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500605
Keith Buschedd10d32014-04-03 16:45:23 -0600606 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700607 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
608 && (jiffies - req->start_time) < req->timeout) {
Keith Buschc9d3bf82015-01-07 18:55:52 -0700609 unsigned long flags;
610
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100611 nvme_unmap_data(nvmeq->dev, iod);
612
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700613 blk_mq_requeue_request(req);
Keith Buschc9d3bf82015-01-07 18:55:52 -0700614 spin_lock_irqsave(req->q->queue_lock, flags);
615 if (!blk_queue_stopped(req->q))
616 blk_mq_kick_requeue_list(req->q);
617 spin_unlock_irqrestore(req->q->queue_lock, flags);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100618 return;
Keith Buschedd10d32014-04-03 16:45:23 -0600619 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200620
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200621 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
Keith Busch17188bb42015-06-08 10:08:14 -0600622 if (cmd_rq->ctx == CMD_CTX_CANCELLED)
Christoph Hellwig297465c2015-11-26 12:58:11 +0100623 error = NVME_SC_CANCELLED;
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200624 else
625 error = status;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200626 } else {
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200627 error = nvme_error_status(status);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200628 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200629 }
630
Keith Buscha0a931d2015-05-22 12:28:31 -0600631 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
632 u32 result = le32_to_cpup(&cqe->result);
633 req->special = (void *)(uintptr_t)result;
634 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700635
636 if (cmd_rq->aborted)
Christoph Hellwige75ec752015-05-22 11:12:39 +0200637 dev_warn(nvmeq->dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700638 "completing aborted command with status:%04x\n",
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200639 error);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700640
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100641 nvme_unmap_data(nvmeq->dev, iod);
642 blk_mq_complete_request(req, error);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500643}
644
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200645static bool nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
646 int total_len)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500647{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500648 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500649 int length = total_len;
650 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500651 int dma_len = sg_dma_len(sg);
652 u64 dma_addr = sg_dma_address(sg);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100653 u32 page_size = dev->ctrl.page_size;
Murali Iyerf137e0f2015-03-26 11:07:51 -0500654 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500655 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500656 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500657 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500658 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500659
Keith Busch1d090622014-06-23 11:34:01 -0600660 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500661 if (length <= 0)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200662 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500663
Keith Busch1d090622014-06-23 11:34:01 -0600664 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500665 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600666 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500667 } else {
668 sg = sg_next(sg);
669 dma_addr = sg_dma_address(sg);
670 dma_len = sg_dma_len(sg);
671 }
672
Keith Busch1d090622014-06-23 11:34:01 -0600673 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600674 iod->first_dma = dma_addr;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200675 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500676 }
677
Keith Busch1d090622014-06-23 11:34:01 -0600678 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500679 if (nprps <= (256 / 8)) {
680 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500681 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500682 } else {
683 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500684 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500685 }
686
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200687 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400688 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600689 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500690 iod->npages = -1;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200691 return false;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400692 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500693 list[0] = prp_list;
694 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500695 i = 0;
696 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600697 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500698 __le64 *old_prp_list = prp_list;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200699 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500700 if (!prp_list)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200701 return false;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500702 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400703 prp_list[0] = old_prp_list[i - 1];
704 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
705 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500706 }
707 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600708 dma_len -= page_size;
709 dma_addr += page_size;
710 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500711 if (length <= 0)
712 break;
713 if (dma_len > 0)
714 continue;
715 BUG_ON(dma_len < 0);
716 sg = sg_next(sg);
717 dma_addr = sg_dma_address(sg);
718 dma_len = sg_dma_len(sg);
719 }
720
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200721 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500722}
723
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200724static int nvme_map_data(struct nvme_dev *dev, struct nvme_iod *iod,
725 struct nvme_command *cmnd)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200726{
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200727 struct request *req = iod_get_private(iod);
728 struct request_queue *q = req->q;
729 enum dma_data_direction dma_dir = rq_data_dir(req) ?
730 DMA_TO_DEVICE : DMA_FROM_DEVICE;
731 int ret = BLK_MQ_RQ_QUEUE_ERROR;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200732
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200733 sg_init_table(iod->sg, req->nr_phys_segments);
734 iod->nents = blk_rq_map_sg(q, req, iod->sg);
735 if (!iod->nents)
736 goto out;
737
738 ret = BLK_MQ_RQ_QUEUE_BUSY;
739 if (!dma_map_sg(dev->dev, iod->sg, iod->nents, dma_dir))
740 goto out;
741
742 if (!nvme_setup_prps(dev, iod, blk_rq_bytes(req)))
743 goto out_unmap;
744
745 ret = BLK_MQ_RQ_QUEUE_ERROR;
746 if (blk_integrity_rq(req)) {
747 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
748 goto out_unmap;
749
750 sg_init_table(iod->meta_sg, 1);
751 if (blk_rq_map_integrity_sg(q, req->bio, iod->meta_sg) != 1)
752 goto out_unmap;
753
754 if (rq_data_dir(req))
755 nvme_dif_remap(req, nvme_dif_prep);
756
757 if (!dma_map_sg(dev->dev, iod->meta_sg, 1, dma_dir))
758 goto out_unmap;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200759 }
760
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200761 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
762 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
763 if (blk_integrity_rq(req))
764 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg));
765 return BLK_MQ_RQ_QUEUE_OK;
766
767out_unmap:
768 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
769out:
770 return ret;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200771}
772
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100773static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod)
774{
775 struct request *req = iod_get_private(iod);
776 enum dma_data_direction dma_dir = rq_data_dir(req) ?
777 DMA_TO_DEVICE : DMA_FROM_DEVICE;
778
779 if (iod->nents) {
780 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
781 if (blk_integrity_rq(req)) {
782 if (!rq_data_dir(req))
783 nvme_dif_remap(req, nvme_dif_complete);
784 dma_unmap_sg(dev->dev, iod->meta_sg, 1, dma_dir);
785 }
786 }
787
788 nvme_free_iod(dev, iod);
789}
790
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700791/*
792 * We reuse the small pool to allocate the 16-byte range here as it is not
793 * worth having a special pool for these or additional cases to handle freeing
794 * the iod.
795 */
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200796static int nvme_setup_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
797 struct nvme_iod *iod, struct nvme_command *cmnd)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700798{
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200799 struct request *req = iod_get_private(iod);
800 struct nvme_dsm_range *range;
801
802 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
803 &iod->first_dma);
804 if (!range)
805 return BLK_MQ_RQ_QUEUE_BUSY;
806 iod_list(iod)[0] = (__le64 *)range;
807 iod->npages = 0;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700808
Keith Busch0e5e4f02012-11-09 16:33:05 -0700809 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700810 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
811 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700812
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200813 memset(cmnd, 0, sizeof(*cmnd));
814 cmnd->dsm.opcode = nvme_cmd_dsm;
815 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
816 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
817 cmnd->dsm.nr = 0;
818 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
819 return BLK_MQ_RQ_QUEUE_OK;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700820}
821
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200822/*
823 * NOTE: ns is NULL when called on the admin queue.
824 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700825static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
826 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600827{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700828 struct nvme_ns *ns = hctx->queue->queuedata;
829 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200830 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700831 struct request *req = bd->rq;
832 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600833 struct nvme_iod *iod;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200834 struct nvme_command cmnd;
835 int ret = BLK_MQ_RQ_QUEUE_OK;
Keith Buschedd10d32014-04-03 16:45:23 -0600836
Keith Busche1e5e562015-02-19 13:39:03 -0700837 /*
838 * If formated with metadata, require the block layer provide a buffer
839 * unless this namespace is formated such that the metadata can be
840 * stripped/generated by the controller with PRACT=1.
841 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200842 if (ns && ns->ms && !blk_integrity_rq(req)) {
Keith Busch71feb362015-06-19 11:07:30 -0600843 if (!(ns->pi_type && ns->ms == 8) &&
844 req->cmd_type != REQ_TYPE_DRV_PRIV) {
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200845 blk_mq_complete_request(req, -EFAULT);
Keith Busche1e5e562015-02-19 13:39:03 -0700846 return BLK_MQ_RQ_QUEUE_OK;
847 }
848 }
849
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200850 iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600851 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700852 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600853
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700854 if (req->cmd_flags & REQ_DISCARD) {
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200855 ret = nvme_setup_discard(nvmeq, ns, iod, &cmnd);
856 } else {
857 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
858 memcpy(&cmnd, req->cmd, sizeof(cmnd));
859 else if (req->cmd_flags & REQ_FLUSH)
860 nvme_setup_flush(ns, &cmnd);
861 else
862 nvme_setup_rw(ns, req, &cmnd);
Keith Buschedd10d32014-04-03 16:45:23 -0600863
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200864 if (req->nr_phys_segments)
865 ret = nvme_map_data(dev, iod, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700866 }
867
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200868 if (ret)
869 goto out;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700870
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200871 cmnd.common.command_id = req->tag;
872 nvme_set_info(cmd, iod, req_completion);
873
874 spin_lock_irq(&nvmeq->q_lock);
875 __nvme_submit_cmd(nvmeq, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700876 nvme_process_cq(nvmeq);
877 spin_unlock_irq(&nvmeq->q_lock);
878 return BLK_MQ_RQ_QUEUE_OK;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200879out:
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200880 nvme_free_iod(dev, iod);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200881 return ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500882}
883
Jens Axboea0fa9642015-11-03 20:37:26 -0700884static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500885{
Matthew Wilcox82123462011-01-20 13:24:06 -0500886 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500887
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500888 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500889 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500890
891 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400892 void *ctx;
893 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500894 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500895 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500896 break;
897 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
898 if (++head == nvmeq->q_depth) {
899 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500900 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500901 }
Jens Axboea0fa9642015-11-03 20:37:26 -0700902 if (tag && *tag == cqe.command_id)
903 *tag = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700904 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600905 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500906 }
907
908 /* If the controller ignores the cq head doorbell and continuously
909 * writes to the queue, it is theoretically possible to wrap around
910 * the queue twice and mistakenly return IRQ_NONE. Linux only
911 * requires that 0.1% of your interrupts are handled, so this isn't
912 * a big problem.
913 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500914 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Jens Axboea0fa9642015-11-03 20:37:26 -0700915 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500916
Keith Busch604e8c82015-11-20 08:38:13 -0700917 if (likely(nvmeq->cq_vector >= 0))
918 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500919 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500920 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500921
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400922 nvmeq->cqe_seen = 1;
Jens Axboea0fa9642015-11-03 20:37:26 -0700923}
924
925static void nvme_process_cq(struct nvme_queue *nvmeq)
926{
927 __nvme_process_cq(nvmeq, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500928}
929
930static irqreturn_t nvme_irq(int irq, void *data)
931{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500932 irqreturn_t result;
933 struct nvme_queue *nvmeq = data;
934 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400935 nvme_process_cq(nvmeq);
936 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
937 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500938 spin_unlock(&nvmeq->q_lock);
939 return result;
940}
941
942static irqreturn_t nvme_irq_check(int irq, void *data)
943{
944 struct nvme_queue *nvmeq = data;
945 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
946 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
947 return IRQ_NONE;
948 return IRQ_WAKE_THREAD;
949}
950
Jens Axboea0fa9642015-11-03 20:37:26 -0700951static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
952{
953 struct nvme_queue *nvmeq = hctx->driver_data;
954
955 if ((le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
956 nvmeq->cq_phase) {
957 spin_lock_irq(&nvmeq->q_lock);
958 __nvme_process_cq(nvmeq, &tag);
959 spin_unlock_irq(&nvmeq->q_lock);
960
961 if (tag == -1)
962 return 1;
963 }
964
965 return 0;
966}
967
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700968static int nvme_submit_async_admin_req(struct nvme_dev *dev)
969{
970 struct nvme_queue *nvmeq = dev->queues[0];
971 struct nvme_command c;
972 struct nvme_cmd_info *cmd_info;
973 struct request *req;
974
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100975 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100976 BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED);
Dan Carpenter9f173b32014-11-05 23:39:09 +0300977 if (IS_ERR(req))
978 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700979
Keith Buschc917dfe2015-01-07 18:55:48 -0700980 req->cmd_flags |= REQ_NO_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700981 cmd_info = blk_mq_rq_to_pdu(req);
Keith Busch1efccc92015-03-31 10:37:17 -0600982 nvme_set_info(cmd_info, NULL, async_req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700983
984 memset(&c, 0, sizeof(c));
985 c.common.opcode = nvme_admin_async_event;
986 c.common.command_id = req->tag;
987
Keith Busch42483222015-06-01 09:29:54 -0600988 blk_mq_free_request(req);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530989 __nvme_submit_cmd(nvmeq, &c);
990 return 0;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700991}
992
993static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -0700994 struct nvme_command *cmd,
995 struct async_cmd_info *cmdinfo, unsigned timeout)
996{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700997 struct nvme_queue *nvmeq = dev->queues[0];
998 struct request *req;
999 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -07001000
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001001 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE, 0);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001002 if (IS_ERR(req))
1003 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001004
1005 req->timeout = timeout;
1006 cmd_rq = blk_mq_rq_to_pdu(req);
1007 cmdinfo->req = req;
1008 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -07001009 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001010
1011 cmd->common.command_id = req->tag;
1012
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301013 nvme_submit_cmd(nvmeq, cmd);
1014 return 0;
Keith Busch4d115422013-12-10 13:10:40 -07001015}
1016
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001017static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1018{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001019 struct nvme_command c;
1020
1021 memset(&c, 0, sizeof(c));
1022 c.delete_queue.opcode = opcode;
1023 c.delete_queue.qid = cpu_to_le16(id);
1024
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001025 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001026}
1027
1028static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1029 struct nvme_queue *nvmeq)
1030{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001031 struct nvme_command c;
1032 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1033
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001034 /*
1035 * Note: we (ab)use the fact the the prp fields survive if no data
1036 * is attached to the request.
1037 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001038 memset(&c, 0, sizeof(c));
1039 c.create_cq.opcode = nvme_admin_create_cq;
1040 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1041 c.create_cq.cqid = cpu_to_le16(qid);
1042 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1043 c.create_cq.cq_flags = cpu_to_le16(flags);
1044 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1045
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001046 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001047}
1048
1049static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1050 struct nvme_queue *nvmeq)
1051{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001052 struct nvme_command c;
1053 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1054
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001055 /*
1056 * Note: we (ab)use the fact the the prp fields survive if no data
1057 * is attached to the request.
1058 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001059 memset(&c, 0, sizeof(c));
1060 c.create_sq.opcode = nvme_admin_create_sq;
1061 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1062 c.create_sq.sqid = cpu_to_le16(qid);
1063 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1064 c.create_sq.sq_flags = cpu_to_le16(flags);
1065 c.create_sq.cqid = cpu_to_le16(qid);
1066
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001067 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001068}
1069
1070static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1071{
1072 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1073}
1074
1075static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1076{
1077 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1078}
1079
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001080static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
Keith Buschc30341d2013-12-10 13:10:38 -07001081{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001082 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1083 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001084 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001085 struct request *abort_req;
1086 struct nvme_cmd_info *abort_cmd;
1087 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001088
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001089 /*
1090 * Schedule controller reset if the command was already aborted once
1091 * before and still hasn't been returned to the driver, or if this is
1092 * the admin queue.
1093 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001094 if (!nvmeq->qid || cmd_rq->aborted) {
Christoph Hellwig846cc052015-11-26 12:10:29 +01001095 if (queue_work(nvme_workq, &dev->reset_work)) {
Christoph Hellwig906678922015-10-02 18:49:23 +02001096 dev_warn(dev->dev,
1097 "I/O %d QID %d timeout, reset controller\n",
1098 req->tag, nvmeq->qid);
1099 }
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001100 return BLK_EH_RESET_TIMER;
Keith Buschc30341d2013-12-10 13:10:38 -07001101 }
1102
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001103 if (!dev->ctrl.abort_limit)
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001104 return BLK_EH_RESET_TIMER;
Keith Buschc30341d2013-12-10 13:10:38 -07001105
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001106 abort_req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001107 BLK_MQ_REQ_NOWAIT);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001108 if (IS_ERR(abort_req))
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001109 return BLK_EH_RESET_TIMER;
Keith Buschc30341d2013-12-10 13:10:38 -07001110
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001111 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1112 nvme_set_info(abort_cmd, abort_req, abort_completion);
1113
Keith Buschc30341d2013-12-10 13:10:38 -07001114 memset(&cmd, 0, sizeof(cmd));
1115 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001116 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001117 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001118 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001119
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001120 --dev->ctrl.abort_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001121 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001122
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001123 dev_warn(nvmeq->q_dmadev, "I/O %d QID %d timeout, aborting\n",
1124 req->tag, nvmeq->qid);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301125 nvme_submit_cmd(dev->queues[0], &cmd);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001126
1127 /*
1128 * The aborted req will be completed on receiving the abort req.
1129 * We enable the timer again. If hit twice, it'll cause a device reset,
1130 * as the device then is in a faulty state.
1131 */
1132 return BLK_EH_RESET_TIMER;
Keith Buschc30341d2013-12-10 13:10:38 -07001133}
1134
Keith Busch42483222015-06-01 09:29:54 -06001135static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001136{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001137 struct nvme_queue *nvmeq = data;
1138 void *ctx;
1139 nvme_completion_fn fn;
1140 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001141 struct nvme_completion cqe;
1142
1143 if (!blk_mq_request_started(req))
1144 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001145
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001146 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001147
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001148 if (cmd->ctx == CMD_CTX_CANCELLED)
1149 return;
1150
Keith Buschcef6a942015-01-07 18:55:51 -07001151 if (blk_queue_dying(req->q))
1152 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1153 else
1154 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1155
1156
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001157 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1158 req->tag, nvmeq->qid);
1159 ctx = cancel_cmd_info(cmd, &fn);
1160 fn(nvmeq, ctx, &cqe);
1161}
1162
Keith Buschf435c282014-07-07 09:14:42 -06001163static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001164{
1165 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1166 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001167 if (nvmeq->sq_cmds)
1168 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001169 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1170 kfree(nvmeq);
1171}
1172
Keith Buscha1a5ef92013-12-16 13:50:00 -05001173static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001174{
1175 int i;
1176
Keith Buscha1a5ef92013-12-16 13:50:00 -05001177 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001178 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001179 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001180 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001181 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001182 }
Keith Busch22404272013-07-15 15:02:20 -06001183}
1184
Keith Busch4d115422013-12-10 13:10:40 -07001185/**
1186 * nvme_suspend_queue - put queue into suspended state
1187 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001188 */
1189static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001190{
Keith Busch2b25d982014-12-22 12:59:04 -07001191 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001192
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001193 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001194 if (nvmeq->cq_vector == -1) {
1195 spin_unlock_irq(&nvmeq->q_lock);
1196 return 1;
1197 }
1198 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001199 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001200 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001201 spin_unlock_irq(&nvmeq->q_lock);
1202
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001203 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
1204 blk_mq_freeze_queue_start(nvmeq->dev->ctrl.admin_q);
Keith Busch6df3dbc2015-03-26 13:49:33 -06001205
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001206 irq_set_affinity_hint(vector, NULL);
1207 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001208
Keith Busch4d115422013-12-10 13:10:40 -07001209 return 0;
1210}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001211
Keith Busch4d115422013-12-10 13:10:40 -07001212static void nvme_clear_queue(struct nvme_queue *nvmeq)
1213{
Keith Busch22404272013-07-15 15:02:20 -06001214 spin_lock_irq(&nvmeq->q_lock);
Keith Busch42483222015-06-01 09:29:54 -06001215 if (nvmeq->tags && *nvmeq->tags)
1216 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001217 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001218}
1219
Keith Busch4d115422013-12-10 13:10:40 -07001220static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1221{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001222 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001223
1224 if (!nvmeq)
1225 return;
1226 if (nvme_suspend_queue(nvmeq))
1227 return;
1228
Keith Busch0e53d182013-12-10 13:10:39 -07001229 /* Don't tell the adapter to delete the admin queue.
1230 * Don't tell a removed adapter to delete IO queues. */
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001231 if (qid && readl(dev->bar + NVME_REG_CSTS) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001232 adapter_delete_sq(dev, qid);
1233 adapter_delete_cq(dev, qid);
1234 }
Keith Busch07836e62015-02-19 10:34:48 -07001235
1236 spin_lock_irq(&nvmeq->q_lock);
1237 nvme_process_cq(nvmeq);
1238 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001239}
1240
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001241static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1242 int entry_size)
1243{
1244 int q_depth = dev->q_depth;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001245 unsigned q_size_aligned = roundup(q_depth * entry_size,
1246 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001247
1248 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001249 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001250 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
Jon Derrickc45f5c92015-07-21 15:08:13 -06001251 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001252
1253 /*
1254 * Ensure the reduced q_depth is above some threshold where it
1255 * would be better to map queues in system memory with the
1256 * original depth
1257 */
1258 if (q_depth < 64)
1259 return -ENOMEM;
1260 }
1261
1262 return q_depth;
1263}
1264
1265static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1266 int qid, int depth)
1267{
1268 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001269 unsigned offset = (qid - 1) * roundup(SQ_SIZE(depth),
1270 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001271 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1272 nvmeq->sq_cmds_io = dev->cmb + offset;
1273 } else {
1274 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1275 &nvmeq->sq_dma_addr, GFP_KERNEL);
1276 if (!nvmeq->sq_cmds)
1277 return -ENOMEM;
1278 }
1279
1280 return 0;
1281}
1282
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001283static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001284 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001285{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001286 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001287 if (!nvmeq)
1288 return NULL;
1289
Christoph Hellwige75ec752015-05-22 11:12:39 +02001290 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001291 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001292 if (!nvmeq->cqes)
1293 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001294
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001295 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001296 goto free_cqdma;
1297
Christoph Hellwige75ec752015-05-22 11:12:39 +02001298 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001299 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001300 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001301 dev->ctrl.instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001302 spin_lock_init(&nvmeq->q_lock);
1303 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001304 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001305 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001306 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001307 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001308 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001309 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001310
Jon Derrick36a7e992015-05-27 12:26:23 -06001311 /* make sure queue descriptor is set before queue count, for kthread */
1312 mb();
1313 dev->queue_count++;
1314
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001315 return nvmeq;
1316
1317 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001318 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001319 nvmeq->cq_dma_addr);
1320 free_nvmeq:
1321 kfree(nvmeq);
1322 return NULL;
1323}
1324
Matthew Wilcox30010822011-01-20 09:10:15 -05001325static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1326 const char *name)
1327{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001328 if (use_threaded_interrupts)
1329 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001330 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001331 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001332 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001333 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001334}
1335
Keith Busch22404272013-07-15 15:02:20 -06001336static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001337{
Keith Busch22404272013-07-15 15:02:20 -06001338 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001339
Keith Busch7be50e92014-09-10 15:48:47 -06001340 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001341 nvmeq->sq_tail = 0;
1342 nvmeq->cq_head = 0;
1343 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001344 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001345 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001346 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001347 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001348}
1349
1350static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1351{
1352 struct nvme_dev *dev = nvmeq->dev;
1353 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001354
Keith Busch2b25d982014-12-22 12:59:04 -07001355 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001356 result = adapter_alloc_cq(dev, qid, nvmeq);
1357 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001358 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001359
1360 result = adapter_alloc_sq(dev, qid, nvmeq);
1361 if (result < 0)
1362 goto release_cq;
1363
Matthew Wilcox3193f072014-01-27 15:57:22 -05001364 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001365 if (result < 0)
1366 goto release_sq;
1367
Keith Busch22404272013-07-15 15:02:20 -06001368 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001369 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001370
1371 release_sq:
1372 adapter_delete_sq(dev, qid);
1373 release_cq:
1374 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001375 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001376}
1377
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001378static struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001379 .queue_rq = nvme_queue_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001380 .map_queue = blk_mq_map_queue,
1381 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001382 .exit_hctx = nvme_admin_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001383 .init_request = nvme_admin_init_request,
1384 .timeout = nvme_timeout,
1385};
1386
1387static struct blk_mq_ops nvme_mq_ops = {
1388 .queue_rq = nvme_queue_rq,
1389 .map_queue = blk_mq_map_queue,
1390 .init_hctx = nvme_init_hctx,
1391 .init_request = nvme_init_request,
1392 .timeout = nvme_timeout,
Jens Axboea0fa9642015-11-03 20:37:26 -07001393 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001394};
1395
Keith Buschea191d22015-01-07 18:55:49 -07001396static void nvme_dev_remove_admin(struct nvme_dev *dev)
1397{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001398 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
1399 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001400 blk_mq_free_tag_set(&dev->admin_tagset);
1401 }
1402}
1403
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001404static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1405{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001406 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001407 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1408 dev->admin_tagset.nr_hw_queues = 1;
1409 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
Keith Busch1efccc92015-03-31 10:37:17 -06001410 dev->admin_tagset.reserved_tags = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001411 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001412 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001413 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001414 dev->admin_tagset.driver_data = dev;
1415
1416 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1417 return -ENOMEM;
1418
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001419 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1420 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001421 blk_mq_free_tag_set(&dev->admin_tagset);
1422 return -ENOMEM;
1423 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001424 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001425 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001426 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001427 return -ENODEV;
1428 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001429 } else
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001430 blk_mq_unfreeze_queue(dev->ctrl.admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001431
1432 return 0;
1433}
1434
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001435static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001436{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001437 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001438 u32 aqa;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001439 u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001440 struct nvme_queue *nvmeq;
1441
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001442 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1) ?
Keith Buschdfbac8c2015-08-10 15:20:40 -06001443 NVME_CAP_NSSRC(cap) : 0;
1444
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001445 if (dev->subsystem &&
1446 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1447 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001448
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001449 result = nvme_disable_ctrl(&dev->ctrl, cap);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001450 if (result < 0)
1451 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001452
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001453 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001454 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001455 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001456 if (!nvmeq)
1457 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001458 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001459
1460 aqa = nvmeq->q_depth - 1;
1461 aqa |= aqa << 16;
1462
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001463 writel(aqa, dev->bar + NVME_REG_AQA);
1464 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1465 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001466
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001467 result = nvme_enable_ctrl(&dev->ctrl, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001468 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001469 goto free_nvmeq;
1470
Keith Busch2b25d982014-12-22 12:59:04 -07001471 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001472 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001473 if (result) {
1474 nvmeq->cq_vector = -1;
Keith Busch0fb59cb2015-01-07 18:55:50 -07001475 goto free_nvmeq;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001476 }
Keith Busch025c5572013-05-01 13:07:51 -06001477
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001478 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001479
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001480 free_nvmeq:
1481 nvme_free_queues(dev, 0);
1482 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001483}
1484
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001485static int nvme_kthread(void *data)
1486{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001487 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001488
1489 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001490 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001491 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001492 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001493 int i;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001494 u32 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001495
Christoph Hellwig846cc052015-11-26 12:10:29 +01001496 /*
1497 * Skip controllers currently under reset.
1498 */
1499 if (work_pending(&dev->reset_work) || work_busy(&dev->reset_work))
1500 continue;
1501
Keith Buschdfbac8c2015-08-10 15:20:40 -06001502 if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
1503 csts & NVME_CSTS_CFS) {
Christoph Hellwig846cc052015-11-26 12:10:29 +01001504 if (queue_work(nvme_workq, &dev->reset_work)) {
Christoph Hellwig906678922015-10-02 18:49:23 +02001505 dev_warn(dev->dev,
1506 "Failed status: %x, reset controller\n",
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001507 readl(dev->bar + NVME_REG_CSTS));
Christoph Hellwig906678922015-10-02 18:49:23 +02001508 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07001509 continue;
1510 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001511 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001512 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001513 if (!nvmeq)
1514 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001515 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001516 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06001517
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001518 while (i == 0 && dev->ctrl.event_limit > 0) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001519 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06001520 break;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001521 dev->ctrl.event_limit--;
Keith Busch6fccf932014-06-18 13:58:57 -06001522 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001523 spin_unlock_irq(&nvmeq->q_lock);
1524 }
1525 }
1526 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001527 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001528 }
1529 return 0;
1530}
1531
Christoph Hellwig749941f2015-11-26 11:46:39 +01001532static int nvme_create_io_queues(struct nvme_dev *dev)
Keith Busch42f61422014-03-24 10:46:25 -06001533{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001534 unsigned i;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001535 int ret = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001536
Christoph Hellwig749941f2015-11-26 11:46:39 +01001537 for (i = dev->queue_count; i <= dev->max_qid; i++) {
1538 if (!nvme_alloc_queue(dev, i, dev->q_depth)) {
1539 ret = -ENOMEM;
Keith Busch42f61422014-03-24 10:46:25 -06001540 break;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001541 }
1542 }
Keith Busch42f61422014-03-24 10:46:25 -06001543
Christoph Hellwig749941f2015-11-26 11:46:39 +01001544 for (i = dev->online_queues; i <= dev->queue_count - 1; i++) {
1545 ret = nvme_create_queue(dev->queues[i], i);
1546 if (ret) {
Christoph Hellwig2659e572015-10-02 18:51:31 +02001547 nvme_free_queues(dev, i);
Keith Busch42f61422014-03-24 10:46:25 -06001548 break;
Christoph Hellwig2659e572015-10-02 18:51:31 +02001549 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001550 }
1551
1552 /*
1553 * Ignore failing Create SQ/CQ commands, we can continue with less
1554 * than the desired aount of queues, and even a controller without
1555 * I/O queues an still be used to issue admin commands. This might
1556 * be useful to upgrade a buggy firmware for example.
1557 */
1558 return ret >= 0 ? 0 : ret;
Keith Busch42f61422014-03-24 10:46:25 -06001559}
1560
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001561static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1562{
1563 u64 szu, size, offset;
1564 u32 cmbloc;
1565 resource_size_t bar_size;
1566 struct pci_dev *pdev = to_pci_dev(dev->dev);
1567 void __iomem *cmb;
1568 dma_addr_t dma_addr;
1569
1570 if (!use_cmb_sqes)
1571 return NULL;
1572
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001573 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001574 if (!(NVME_CMB_SZ(dev->cmbsz)))
1575 return NULL;
1576
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001577 cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001578
1579 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1580 size = szu * NVME_CMB_SZ(dev->cmbsz);
1581 offset = szu * NVME_CMB_OFST(cmbloc);
1582 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
1583
1584 if (offset > bar_size)
1585 return NULL;
1586
1587 /*
1588 * Controllers may support a CMB size larger than their BAR,
1589 * for example, due to being behind a bridge. Reduce the CMB to
1590 * the reported size of the BAR
1591 */
1592 if (size > bar_size - offset)
1593 size = bar_size - offset;
1594
1595 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
1596 cmb = ioremap_wc(dma_addr, size);
1597 if (!cmb)
1598 return NULL;
1599
1600 dev->cmb_dma_addr = dma_addr;
1601 dev->cmb_size = size;
1602 return cmb;
1603}
1604
1605static inline void nvme_release_cmb(struct nvme_dev *dev)
1606{
1607 if (dev->cmb) {
1608 iounmap(dev->cmb);
1609 dev->cmb = NULL;
1610 }
1611}
1612
Keith Busch9d713c22013-07-15 15:02:24 -06001613static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1614{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001615 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001616}
1617
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001618static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001619{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001620 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02001621 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch42f61422014-03-24 10:46:25 -06001622 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001623
Keith Busch42f61422014-03-24 10:46:25 -06001624 nr_io_queues = num_possible_cpus();
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001625 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
1626 if (result < 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001627 return result;
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001628
1629 /*
1630 * Degraded controllers might return an error when setting the queue
1631 * count. We still want to be able to bring them online and offer
1632 * access to the admin queue, as that might be only way to fix them up.
1633 */
1634 if (result > 0) {
1635 dev_err(dev->dev, "Could not set queue count (%d)\n", result);
1636 nr_io_queues = 0;
1637 result = 0;
1638 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001639
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001640 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1641 result = nvme_cmb_qdepth(dev, nr_io_queues,
1642 sizeof(struct nvme_command));
1643 if (result > 0)
1644 dev->q_depth = result;
1645 else
1646 nvme_release_cmb(dev);
1647 }
1648
Keith Busch9d713c22013-07-15 15:02:24 -06001649 size = db_bar_size(dev, nr_io_queues);
1650 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001651 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001652 do {
1653 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1654 if (dev->bar)
1655 break;
1656 if (!--nr_io_queues)
1657 return -ENOMEM;
1658 size = db_bar_size(dev, nr_io_queues);
1659 } while (1);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001660 dev->dbs = dev->bar + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07001661 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001662 }
1663
Keith Busch9d713c22013-07-15 15:02:24 -06001664 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05001665 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001666
Jens Axboee32efbf2014-11-14 09:49:26 -07001667 /*
1668 * If we enable msix early due to not intx, disable it again before
1669 * setting up the full range we need.
1670 */
1671 if (!pdev->irq)
1672 pci_disable_msix(pdev);
1673
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001674 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001675 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001676 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
1677 if (vecs < 0) {
1678 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
1679 if (vecs < 0) {
1680 vecs = 1;
1681 } else {
1682 for (i = 0; i < vecs; i++)
1683 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001684 }
1685 }
1686
Matthew Wilcox063a8092013-06-20 10:53:48 -04001687 /*
1688 * Should investigate if there's a performance win from allocating
1689 * more queues than interrupt vectors; it might allow the submission
1690 * path to scale better, even if the receive path is limited by the
1691 * number of interrupts.
1692 */
1693 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06001694 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001695
Matthew Wilcox3193f072014-01-27 15:57:22 -05001696 result = queue_request_irq(dev, adminq, adminq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001697 if (result) {
1698 adminq->cq_vector = -1;
Keith Busch22404272013-07-15 15:02:20 -06001699 goto free_queues;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001700 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001701
Keith Buschcd638942013-07-15 15:02:23 -06001702 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06001703 nvme_free_queues(dev, nr_io_queues + 1);
Christoph Hellwig749941f2015-11-26 11:46:39 +01001704 return nvme_create_io_queues(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001705
Keith Busch22404272013-07-15 15:02:20 -06001706 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05001707 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06001708 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001709}
1710
Keith Buschbda4e0f2015-09-03 08:18:17 -06001711static void nvme_set_irq_hints(struct nvme_dev *dev)
1712{
1713 struct nvme_queue *nvmeq;
1714 int i;
1715
1716 for (i = 0; i < dev->online_queues; i++) {
1717 nvmeq = dev->queues[i];
1718
1719 if (!nvmeq->tags || !(*nvmeq->tags))
1720 continue;
1721
1722 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
1723 blk_mq_tags_cpumask(*nvmeq->tags));
1724 }
1725}
1726
Keith Buscha5768aa2015-06-01 14:28:14 -06001727static void nvme_dev_scan(struct work_struct *work)
1728{
1729 struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06001730
1731 if (!dev->tagset.tags)
1732 return;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001733 nvme_scan_namespaces(&dev->ctrl);
Keith Buschbda4e0f2015-09-03 08:18:17 -06001734 nvme_set_irq_hints(dev);
Keith Buscha5768aa2015-06-01 14:28:14 -06001735}
1736
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001737/*
1738 * Return: error value if an error occurred setting up the queues or calling
1739 * Identify Device. 0 if these succeeded, even if adding some of the
1740 * namespaces failed. At the moment, these failures are silent. TBD which
1741 * failures should be reported.
1742 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001743static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001744{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001745 if (!dev->ctrl.tagset) {
Keith Buschffe77042015-06-08 10:08:15 -06001746 dev->tagset.ops = &nvme_mq_ops;
1747 dev->tagset.nr_hw_queues = dev->online_queues - 1;
1748 dev->tagset.timeout = NVME_IO_TIMEOUT;
1749 dev->tagset.numa_node = dev_to_node(dev->dev);
1750 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001751 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06001752 dev->tagset.cmd_size = nvme_cmd_size(dev);
1753 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
1754 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001755
Keith Buschffe77042015-06-08 10:08:15 -06001756 if (blk_mq_alloc_tag_set(&dev->tagset))
1757 return 0;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001758 dev->ctrl.tagset = &dev->tagset;
Keith Buschffe77042015-06-08 10:08:15 -06001759 }
Keith Buscha5768aa2015-06-01 14:28:14 -06001760 schedule_work(&dev->scan_work);
Keith Busche1e5e562015-02-19 13:39:03 -07001761 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001762}
1763
Keith Busch0877cb02013-07-15 15:02:19 -06001764static int nvme_dev_map(struct nvme_dev *dev)
1765{
Keith Busch42f61422014-03-24 10:46:25 -06001766 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06001767 int bars, result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001768 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001769
1770 if (pci_enable_device_mem(pdev))
1771 return result;
1772
1773 dev->entry[0].vector = pdev->irq;
1774 pci_set_master(pdev);
1775 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07001776 if (!bars)
1777 goto disable_pci;
1778
Keith Busch0877cb02013-07-15 15:02:19 -06001779 if (pci_request_selected_regions(pdev, bars, "nvme"))
1780 goto disable_pci;
1781
Christoph Hellwige75ec752015-05-22 11:12:39 +02001782 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
1783 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01001784 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06001785
Keith Busch0877cb02013-07-15 15:02:19 -06001786 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1787 if (!dev->bar)
1788 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07001789
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001790 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07001791 result = -ENODEV;
1792 goto unmap;
1793 }
Jens Axboee32efbf2014-11-14 09:49:26 -07001794
1795 /*
1796 * Some devices don't advertse INTx interrupts, pre-enable a single
1797 * MSIX vec for setup. We'll adjust this later.
1798 */
1799 if (!pdev->irq) {
1800 result = pci_enable_msix(pdev, dev->entry, 1);
1801 if (result < 0)
1802 goto unmap;
1803 }
1804
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001805 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
1806
Keith Busch42f61422014-03-24 10:46:25 -06001807 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
1808 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001809 dev->dbs = dev->bar + 4096;
1810 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2))
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001811 dev->cmb = nvme_map_cmb(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001812
1813 return 0;
1814
Keith Busch0e53d182013-12-10 13:10:39 -07001815 unmap:
1816 iounmap(dev->bar);
1817 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06001818 disable:
1819 pci_release_regions(pdev);
1820 disable_pci:
1821 pci_disable_device(pdev);
1822 return result;
1823}
1824
1825static void nvme_dev_unmap(struct nvme_dev *dev)
1826{
Christoph Hellwige75ec752015-05-22 11:12:39 +02001827 struct pci_dev *pdev = to_pci_dev(dev->dev);
1828
1829 if (pdev->msi_enabled)
1830 pci_disable_msi(pdev);
1831 else if (pdev->msix_enabled)
1832 pci_disable_msix(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001833
1834 if (dev->bar) {
1835 iounmap(dev->bar);
1836 dev->bar = NULL;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001837 pci_release_regions(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001838 }
1839
Christoph Hellwige75ec752015-05-22 11:12:39 +02001840 if (pci_is_enabled(pdev))
1841 pci_disable_device(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001842}
1843
Keith Busch4d115422013-12-10 13:10:40 -07001844struct nvme_delq_ctx {
1845 struct task_struct *waiter;
1846 struct kthread_worker *worker;
1847 atomic_t refcount;
1848};
1849
1850static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
1851{
1852 dq->waiter = current;
1853 mb();
1854
1855 for (;;) {
1856 set_current_state(TASK_KILLABLE);
1857 if (!atomic_read(&dq->refcount))
1858 break;
1859 if (!schedule_timeout(ADMIN_TIMEOUT) ||
1860 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07001861 /*
1862 * Disable the controller first since we can't trust it
1863 * at this point, but leave the admin queue enabled
1864 * until all queue deletion requests are flushed.
1865 * FIXME: This may take a while if there are more h/w
1866 * queues than admin tags.
1867 */
Keith Busch4d115422013-12-10 13:10:40 -07001868 set_current_state(TASK_RUNNING);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001869 nvme_disable_ctrl(&dev->ctrl,
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001870 lo_hi_readq(dev->bar + NVME_REG_CAP));
Keith Busch0fb59cb2015-01-07 18:55:50 -07001871 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07001872 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07001873 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07001874 return;
1875 }
1876 }
1877 set_current_state(TASK_RUNNING);
1878}
1879
1880static void nvme_put_dq(struct nvme_delq_ctx *dq)
1881{
1882 atomic_dec(&dq->refcount);
1883 if (dq->waiter)
1884 wake_up_process(dq->waiter);
1885}
1886
1887static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
1888{
1889 atomic_inc(&dq->refcount);
1890 return dq;
1891}
1892
1893static void nvme_del_queue_end(struct nvme_queue *nvmeq)
1894{
1895 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
Keith Busch4d115422013-12-10 13:10:40 -07001896 nvme_put_dq(dq);
Keith Busch604e8c82015-11-20 08:38:13 -07001897
1898 spin_lock_irq(&nvmeq->q_lock);
1899 nvme_process_cq(nvmeq);
1900 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001901}
1902
1903static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
1904 kthread_work_func_t fn)
1905{
1906 struct nvme_command c;
1907
1908 memset(&c, 0, sizeof(c));
1909 c.delete_queue.opcode = opcode;
1910 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
1911
1912 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001913 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
1914 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07001915}
1916
1917static void nvme_del_cq_work_handler(struct kthread_work *work)
1918{
1919 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1920 cmdinfo.work);
1921 nvme_del_queue_end(nvmeq);
1922}
1923
1924static int nvme_delete_cq(struct nvme_queue *nvmeq)
1925{
1926 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
1927 nvme_del_cq_work_handler);
1928}
1929
1930static void nvme_del_sq_work_handler(struct kthread_work *work)
1931{
1932 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1933 cmdinfo.work);
1934 int status = nvmeq->cmdinfo.status;
1935
1936 if (!status)
1937 status = nvme_delete_cq(nvmeq);
1938 if (status)
1939 nvme_del_queue_end(nvmeq);
1940}
1941
1942static int nvme_delete_sq(struct nvme_queue *nvmeq)
1943{
1944 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
1945 nvme_del_sq_work_handler);
1946}
1947
1948static void nvme_del_queue_start(struct kthread_work *work)
1949{
1950 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1951 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07001952 if (nvme_delete_sq(nvmeq))
1953 nvme_del_queue_end(nvmeq);
1954}
1955
1956static void nvme_disable_io_queues(struct nvme_dev *dev)
1957{
1958 int i;
1959 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
1960 struct nvme_delq_ctx dq;
1961 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001962 &worker, "nvme%d", dev->ctrl.instance);
Keith Busch4d115422013-12-10 13:10:40 -07001963
1964 if (IS_ERR(kworker_task)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001965 dev_err(dev->dev,
Keith Busch4d115422013-12-10 13:10:40 -07001966 "Failed to create queue del task\n");
1967 for (i = dev->queue_count - 1; i > 0; i--)
1968 nvme_disable_queue(dev, i);
1969 return;
1970 }
1971
1972 dq.waiter = NULL;
1973 atomic_set(&dq.refcount, 0);
1974 dq.worker = &worker;
1975 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001976 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07001977
1978 if (nvme_suspend_queue(nvmeq))
1979 continue;
1980 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
1981 nvmeq->cmdinfo.worker = dq.worker;
1982 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
1983 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
1984 }
1985 nvme_wait_dq(&dq, dev);
1986 kthread_stop(kworker_task);
1987}
1988
Christoph Hellwig73850142015-10-22 14:03:33 +02001989static int nvme_dev_list_add(struct nvme_dev *dev)
1990{
1991 bool start_thread = false;
1992
1993 spin_lock(&dev_list_lock);
1994 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
1995 start_thread = true;
1996 nvme_thread = NULL;
1997 }
1998 list_add(&dev->node, &dev_list);
1999 spin_unlock(&dev_list_lock);
2000
2001 if (start_thread) {
2002 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2003 wake_up_all(&nvme_kthread_wait);
2004 } else
2005 wait_event_killable(nvme_kthread_wait, nvme_thread);
2006
2007 if (IS_ERR_OR_NULL(nvme_thread))
2008 return nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2009
2010 return 0;
2011}
2012
Dan McLeranb9afca32014-04-07 17:10:11 -06002013/*
2014* Remove the node from the device list and check
2015* for whether or not we need to stop the nvme_thread.
2016*/
2017static void nvme_dev_list_remove(struct nvme_dev *dev)
2018{
2019 struct task_struct *tmp = NULL;
2020
2021 spin_lock(&dev_list_lock);
2022 list_del_init(&dev->node);
2023 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2024 tmp = nvme_thread;
2025 nvme_thread = NULL;
2026 }
2027 spin_unlock(&dev_list_lock);
2028
2029 if (tmp)
2030 kthread_stop(tmp);
2031}
2032
Keith Buschc9d3bf82015-01-07 18:55:52 -07002033static void nvme_freeze_queues(struct nvme_dev *dev)
2034{
2035 struct nvme_ns *ns;
2036
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002037 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
Keith Buschc9d3bf82015-01-07 18:55:52 -07002038 blk_mq_freeze_queue_start(ns->queue);
2039
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002040 spin_lock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002041 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002042 spin_unlock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002043
2044 blk_mq_cancel_requeue_work(ns->queue);
2045 blk_mq_stop_hw_queues(ns->queue);
2046 }
2047}
2048
2049static void nvme_unfreeze_queues(struct nvme_dev *dev)
2050{
2051 struct nvme_ns *ns;
2052
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002053 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
Keith Buschc9d3bf82015-01-07 18:55:52 -07002054 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2055 blk_mq_unfreeze_queue(ns->queue);
2056 blk_mq_start_stopped_hw_queues(ns->queue, true);
2057 blk_mq_kick_requeue_list(ns->queue);
2058 }
2059}
2060
Keith Buschf0b50732013-07-15 15:02:21 -06002061static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002062{
Keith Busch22404272013-07-15 15:02:20 -06002063 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002064 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002065
Dan McLeranb9afca32014-04-07 17:10:11 -06002066 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002067
Keith Busch77bf25e2015-11-26 12:21:29 +01002068 mutex_lock(&dev->shutdown_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002069 if (dev->bar) {
2070 nvme_freeze_queues(dev);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002071 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002072 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002073 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002074 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002075 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002076 nvme_suspend_queue(nvmeq);
Keith Busch4d115422013-12-10 13:10:40 -07002077 }
2078 } else {
2079 nvme_disable_io_queues(dev);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002080 nvme_shutdown_ctrl(&dev->ctrl);
Keith Busch4d115422013-12-10 13:10:40 -07002081 nvme_disable_queue(dev, 0);
2082 }
Keith Buschf0b50732013-07-15 15:02:21 -06002083 nvme_dev_unmap(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002084
2085 for (i = dev->queue_count - 1; i >= 0; i--)
2086 nvme_clear_queue(dev->queues[i]);
Keith Busch77bf25e2015-11-26 12:21:29 +01002087 mutex_unlock(&dev->shutdown_lock);
Keith Buschf0b50732013-07-15 15:02:21 -06002088}
2089
Matthew Wilcox091b6092011-02-10 09:56:01 -05002090static int nvme_setup_prp_pools(struct nvme_dev *dev)
2091{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002092 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002093 PAGE_SIZE, PAGE_SIZE, 0);
2094 if (!dev->prp_page_pool)
2095 return -ENOMEM;
2096
Matthew Wilcox99802a72011-02-10 10:30:34 -05002097 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002098 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002099 256, 256, 0);
2100 if (!dev->prp_small_pool) {
2101 dma_pool_destroy(dev->prp_page_pool);
2102 return -ENOMEM;
2103 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002104 return 0;
2105}
2106
2107static void nvme_release_prp_pools(struct nvme_dev *dev)
2108{
2109 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002110 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002111}
2112
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002113static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
Keith Busch5e82e952013-02-19 10:17:58 -07002114{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002115 struct nvme_dev *dev = to_nvme_dev(ctrl);
Keith Busch9ac27092014-01-31 16:53:39 -07002116
Christoph Hellwige75ec752015-05-22 11:12:39 +02002117 put_device(dev->dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002118 if (dev->tagset.tags)
2119 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002120 if (dev->ctrl.admin_q)
2121 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002122 kfree(dev->queues);
2123 kfree(dev->entry);
2124 kfree(dev);
2125}
2126
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002127static void nvme_probe_work(struct work_struct *work)
Keith Buschf0b50732013-07-15 15:02:21 -06002128{
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002129 struct nvme_dev *dev = container_of(work, struct nvme_dev, probe_work);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002130 int result;
Keith Buschf0b50732013-07-15 15:02:21 -06002131
2132 result = nvme_dev_map(dev);
2133 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002134 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002135
2136 result = nvme_configure_admin_queue(dev);
2137 if (result)
2138 goto unmap;
2139
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002140 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002141 result = nvme_alloc_admin_tags(dev);
2142 if (result)
2143 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06002144
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002145 result = nvme_init_identify(&dev->ctrl);
2146 if (result)
2147 goto free_tags;
2148
Keith Buschf0b50732013-07-15 15:02:21 -06002149 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002150 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07002151 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06002152
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002153 dev->ctrl.event_limit = 1;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002154
Christoph Hellwig73850142015-10-22 14:03:33 +02002155 result = nvme_dev_list_add(dev);
2156 if (result)
2157 goto remove;
2158
Christoph Hellwig2659e572015-10-02 18:51:31 +02002159 /*
2160 * Keep the controller around but remove all namespaces if we don't have
2161 * any working I/O queue.
2162 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002163 if (dev->online_queues < 2) {
2164 dev_warn(dev->dev, "IO queues not created\n");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002165 nvme_remove_namespaces(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002166 } else {
2167 nvme_unfreeze_queues(dev);
2168 nvme_dev_add(dev);
2169 }
2170
2171 return;
Keith Buschf0b50732013-07-15 15:02:21 -06002172
Christoph Hellwig73850142015-10-22 14:03:33 +02002173 remove:
2174 nvme_dev_list_remove(dev);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002175 free_tags:
2176 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002177 blk_put_queue(dev->ctrl.admin_q);
2178 dev->ctrl.admin_q = NULL;
Keith Busch4af0e212015-06-08 10:08:13 -06002179 dev->queues[0]->tags = NULL;
Keith Buschf0b50732013-07-15 15:02:21 -06002180 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002181 nvme_disable_queue(dev, 0);
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
Keith Busch4cc06522015-06-05 10:30:08 -06002234static int nvme_reset(struct nvme_dev *dev)
2235{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002236 if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q))
Keith Busch4cc06522015-06-05 10:30:08 -06002237 return -ENODEV;
2238
Christoph Hellwig846cc052015-11-26 12:10:29 +01002239 if (!queue_work(nvme_workq, &dev->reset_work))
2240 return -EBUSY;
Keith Busch4cc06522015-06-05 10:30:08 -06002241
Christoph Hellwig846cc052015-11-26 12:10:29 +01002242 flush_work(&dev->reset_work);
2243 flush_work(&dev->probe_work);
2244 return 0;
Keith Busch4cc06522015-06-05 10:30:08 -06002245}
2246
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002247static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
2248{
2249 *val = readl(to_nvme_dev(ctrl)->bar + off);
2250 return 0;
2251}
2252
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002253static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2254{
2255 writel(val, to_nvme_dev(ctrl)->bar + off);
2256 return 0;
2257}
2258
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002259static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2260{
2261 *val = readq(to_nvme_dev(ctrl)->bar + off);
2262 return 0;
2263}
2264
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002265static bool nvme_pci_io_incapable(struct nvme_ctrl *ctrl)
2266{
2267 struct nvme_dev *dev = to_nvme_dev(ctrl);
2268
2269 return !dev->bar || dev->online_queues < 2;
2270}
2271
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002272static int nvme_pci_reset_ctrl(struct nvme_ctrl *ctrl)
2273{
2274 return nvme_reset(to_nvme_dev(ctrl));
2275}
2276
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002277static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
2278 .reg_read32 = nvme_pci_reg_read32,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002279 .reg_write32 = nvme_pci_reg_write32,
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002280 .reg_read64 = nvme_pci_reg_read64,
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002281 .io_incapable = nvme_pci_io_incapable,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002282 .reset_ctrl = nvme_pci_reset_ctrl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002283 .free_ctrl = nvme_pci_free_ctrl,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002284};
2285
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002286static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002287{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002288 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002289 struct nvme_dev *dev;
2290
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002291 node = dev_to_node(&pdev->dev);
2292 if (node == NUMA_NO_NODE)
2293 set_dev_node(&pdev->dev, 0);
2294
2295 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002296 if (!dev)
2297 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002298 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2299 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002300 if (!dev->entry)
2301 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002302 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2303 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002304 if (!dev->queues)
2305 goto free;
2306
Christoph Hellwige75ec752015-05-22 11:12:39 +02002307 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002308 pci_set_drvdata(pdev, dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002309
Keith Busche6e96d72015-03-23 09:32:37 -06002310 INIT_LIST_HEAD(&dev->node);
Keith Buscha5768aa2015-06-01 14:28:14 -06002311 INIT_WORK(&dev->scan_work, nvme_dev_scan);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002312 INIT_WORK(&dev->probe_work, nvme_probe_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002313 INIT_WORK(&dev->reset_work, nvme_reset_work);
Keith Busch77bf25e2015-11-26 12:21:29 +01002314 mutex_init(&dev->shutdown_lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002315
2316 result = nvme_setup_prp_pools(dev);
2317 if (result)
2318 goto put_pci;
2319
2320 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
2321 id->driver_data);
2322 if (result)
2323 goto release_pools;
2324
Keith Busch2e1d8442015-02-12 15:33:00 -07002325 schedule_work(&dev->probe_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002326 return 0;
2327
Keith Busch0877cb02013-07-15 15:02:19 -06002328 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002329 nvme_release_prp_pools(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002330 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002331 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002332 free:
2333 kfree(dev->queues);
2334 kfree(dev->entry);
2335 kfree(dev);
2336 return result;
2337}
2338
Keith Buschf0d54a52014-05-02 10:40:43 -06002339static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2340{
Keith Buscha6739472014-06-23 16:03:21 -06002341 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002342
Keith Buscha6739472014-06-23 16:03:21 -06002343 if (prepare)
2344 nvme_dev_shutdown(dev);
2345 else
Keith Busch0a7385a2015-10-02 10:37:29 -06002346 schedule_work(&dev->probe_work);
Keith Buschf0d54a52014-05-02 10:40:43 -06002347}
2348
Keith Busch09ece142014-01-27 11:29:40 -05002349static void nvme_shutdown(struct pci_dev *pdev)
2350{
2351 struct nvme_dev *dev = pci_get_drvdata(pdev);
2352 nvme_dev_shutdown(dev);
2353}
2354
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002355static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002356{
2357 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002358
2359 spin_lock(&dev_list_lock);
2360 list_del_init(&dev->node);
2361 spin_unlock(&dev_list_lock);
2362
2363 pci_set_drvdata(pdev, NULL);
Keith Busch2e1d8442015-02-12 15:33:00 -07002364 flush_work(&dev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002365 flush_work(&dev->reset_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06002366 flush_work(&dev->scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002367 nvme_remove_namespaces(&dev->ctrl);
Keith Busch3399a3f2015-06-18 13:36:40 -06002368 nvme_dev_shutdown(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002369 nvme_dev_remove_admin(dev);
2370 nvme_free_queues(dev, 0);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002371 nvme_release_cmb(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002372 nvme_release_prp_pools(dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002373 nvme_put_ctrl(&dev->ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002374}
2375
2376/* These functions are yet to be implemented */
2377#define nvme_error_detected NULL
2378#define nvme_dump_registers NULL
2379#define nvme_link_reset NULL
2380#define nvme_slot_reset NULL
2381#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002382
Jingoo Han671a6012014-02-13 11:19:14 +09002383#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002384static int nvme_suspend(struct device *dev)
2385{
2386 struct pci_dev *pdev = to_pci_dev(dev);
2387 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2388
2389 nvme_dev_shutdown(ndev);
2390 return 0;
2391}
2392
2393static int nvme_resume(struct device *dev)
2394{
2395 struct pci_dev *pdev = to_pci_dev(dev);
2396 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002397
Keith Busch0a7385a2015-10-02 10:37:29 -06002398 schedule_work(&ndev->probe_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002399 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002400}
Jingoo Han671a6012014-02-13 11:19:14 +09002401#endif
Keith Buschcd638942013-07-15 15:02:23 -06002402
2403static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002404
Stephen Hemminger1d352032012-09-07 09:33:17 -07002405static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002406 .error_detected = nvme_error_detected,
2407 .mmio_enabled = nvme_dump_registers,
2408 .link_reset = nvme_link_reset,
2409 .slot_reset = nvme_slot_reset,
2410 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002411 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002412};
2413
2414/* Move to pci_ids.h later */
2415#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2416
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002417static const struct pci_device_id nvme_id_table[] = {
Christoph Hellwig106198e2015-11-26 10:07:41 +01002418 { PCI_VDEVICE(INTEL, 0x0953),
2419 .driver_data = NVME_QUIRK_STRIPE_SIZE, },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002420 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01002421 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002422 { 0, }
2423};
2424MODULE_DEVICE_TABLE(pci, nvme_id_table);
2425
2426static struct pci_driver nvme_driver = {
2427 .name = "nvme",
2428 .id_table = nvme_id_table,
2429 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002430 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002431 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002432 .driver = {
2433 .pm = &nvme_dev_pm_ops,
2434 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002435 .err_handler = &nvme_err_handler,
2436};
2437
2438static int __init nvme_init(void)
2439{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002440 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002441
Dan McLeranb9afca32014-04-07 17:10:11 -06002442 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002443
Keith Busch9a6b9452013-12-10 13:10:36 -07002444 nvme_workq = create_singlethread_workqueue("nvme");
2445 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002446 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002447
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002448 result = nvme_core_init();
Keith Busch5c42ea12012-07-25 16:05:18 -06002449 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002450 goto kill_workq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002451
Keith Buschf3db22f2014-06-11 11:51:35 -06002452 result = pci_register_driver(&nvme_driver);
2453 if (result)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002454 goto core_exit;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002455 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002456
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002457 core_exit:
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002458 nvme_core_exit();
Keith Busch9a6b9452013-12-10 13:10:36 -07002459 kill_workq:
2460 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002461 return result;
2462}
2463
2464static void __exit nvme_exit(void)
2465{
2466 pci_unregister_driver(&nvme_driver);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002467 nvme_core_exit();
Keith Busch9a6b9452013-12-10 13:10:36 -07002468 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06002469 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002470 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002471}
2472
2473MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2474MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002475MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002476module_init(nvme_init);
2477module_exit(nvme_exit);