blob: a909a8ba228aebb94588b66f6b80820472b92ef7 [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003 * Copyright (c) 2011-2014, Intel Corporation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050013 */
14
Matthew Wilcox8de05532011-05-12 13:50:28 -040015#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050016#include <linux/blkdev.h>
Matias Bjørlinga4aea562014-11-04 08:20:14 -070017#include <linux/blk-mq.h>
Keith Busch42f61422014-03-24 10:46:25 -060018#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040019#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050020#include <linux/errno.h>
21#include <linux/fs.h>
22#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060023#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040024#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050025#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050029#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050030#include <linux/kernel.h>
31#include <linux/mm.h>
32#include <linux/module.h>
33#include <linux/moduleparam.h>
Keith Busch77bf25e2015-11-26 12:21:29 +010034#include <linux/mutex.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050035#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050036#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040037#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050038#include <linux/sched.h>
39#include <linux/slab.h>
Keith Busche1e5e562015-02-19 13:39:03 -070040#include <linux/t10-pi.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050041#include <linux/types.h>
Linus Torvalds9cf5c092015-11-06 14:22:15 -080042#include <linux/io-64-nonatomic-lo-hi.h>
Keith Busch1d277a62015-10-15 14:10:52 +020043#include <asm/unaligned.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090044
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020045#include "nvme.h"
46
Keith Busch9d43cf62014-05-13 11:42:02 -060047#define NVME_Q_DEPTH 1024
Jens Axboed31af0a2015-03-06 12:56:13 -070048#define NVME_AQ_DEPTH 256
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050049#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
50#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050051
Christoph Hellwig21d34712015-11-26 09:08:36 +010052unsigned char admin_timeout = 60;
Keith Busch9d43cf62014-05-13 11:42:02 -060053module_param(admin_timeout, byte, 0644);
54MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050055
Matthew Wilcoxbd676082014-06-03 23:04:30 -040056unsigned char nvme_io_timeout = 30;
57module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060058MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050059
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +010060unsigned char shutdown_timeout = 5;
Dan McLeran2484f402014-07-01 09:33:32 -060061module_param(shutdown_timeout, byte, 0644);
62MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
63
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050064static int use_threaded_interrupts;
65module_param(use_threaded_interrupts, int, 0);
66
Jon Derrick8ffaadf2015-07-20 10:14:09 -060067static bool use_cmb_sqes = true;
68module_param(use_cmb_sqes, bool, 0644);
69MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
70
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050071static LIST_HEAD(dev_list);
72static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070073static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060074static wait_queue_head_t nvme_kthread_wait;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050075
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010076struct nvme_dev;
77struct nvme_queue;
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +010078struct nvme_iod;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010079
Keith Busch4cc06522015-06-05 10:30:08 -060080static int nvme_reset(struct nvme_dev *dev);
Jens Axboea0fa9642015-11-03 20:37:26 -070081static void nvme_process_cq(struct nvme_queue *nvmeq);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +010082static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +010083static void nvme_remove_dead_ctrl(struct nvme_dev *dev);
Keith Busche1569a12015-11-26 12:11:07 +010084static void nvme_dev_shutdown(struct nvme_dev *dev);
Keith Buschd4b4ff82013-12-10 13:10:37 -070085
Keith Busch4d115422013-12-10 13:10:40 -070086struct async_cmd_info {
87 struct kthread_work work;
88 struct kthread_worker *worker;
Matias Bjørlinga4aea562014-11-04 08:20:14 -070089 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -070090 u32 result;
91 int status;
92 void *ctx;
93};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050094
95/*
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010096 * Represents an NVM Express device. Each nvme_dev is a PCI function.
97 */
98struct nvme_dev {
99 struct list_head node;
100 struct nvme_queue **queues;
101 struct blk_mq_tag_set tagset;
102 struct blk_mq_tag_set admin_tagset;
103 u32 __iomem *dbs;
104 struct device *dev;
105 struct dma_pool *prp_page_pool;
106 struct dma_pool *prp_small_pool;
107 unsigned queue_count;
108 unsigned online_queues;
109 unsigned max_qid;
110 int q_depth;
111 u32 db_stride;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100112 struct msix_entry *entry;
113 void __iomem *bar;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100114 struct work_struct reset_work;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100115 struct work_struct scan_work;
Christoph Hellwig5c8809e2015-11-26 12:35:49 +0100116 struct work_struct remove_work;
Keith Busch77bf25e2015-11-26 12:21:29 +0100117 struct mutex shutdown_lock;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100118 bool subsystem;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100119 void __iomem *cmb;
120 dma_addr_t cmb_dma_addr;
121 u64 cmb_size;
122 u32 cmbsz;
Christoph Hellwigfd634f412015-11-26 12:42:26 +0100123 unsigned long flags;
124#define NVME_CTRL_RESETTING 0
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100125
126 struct nvme_ctrl ctrl;
127};
128
129static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
130{
131 return container_of(ctrl, struct nvme_dev, ctrl);
132}
133
134/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500135 * An NVM Express queue. Each device has at least two (one for admin
136 * commands and one for I/O commands).
137 */
138struct nvme_queue {
139 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500140 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500141 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500142 spinlock_t q_lock;
143 struct nvme_command *sq_cmds;
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600144 struct nvme_command __iomem *sq_cmds_io;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500145 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600146 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500147 dma_addr_t sq_dma_addr;
148 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500149 u32 __iomem *q_db;
150 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700151 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500152 u16 sq_head;
153 u16 sq_tail;
154 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700155 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400156 u8 cq_phase;
157 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700158 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500159};
160
161/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200162 * The nvme_iod describes the data in an I/O, including the list of PRP
163 * entries. You can't see it in this data structure because C doesn't let
164 * me express that. Use nvme_alloc_iod to ensure there's enough space
165 * allocated to store the PRP list.
166 */
167struct nvme_iod {
168 unsigned long private; /* For the use of the submitter of the I/O */
169 int npages; /* In the PRP list. 0 means small pool in use */
170 int offset; /* Of PRP list */
171 int nents; /* Used in scatterlist */
172 int length; /* Of data, in bytes */
173 dma_addr_t first_dma;
174 struct scatterlist meta_sg[1]; /* metadata requires single contiguous buffer */
175 struct scatterlist sg[0];
176};
177
178/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500179 * Check we didin't inadvertently grow the command struct
180 */
181static inline void _nvme_check_size(void)
182{
183 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
184 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
185 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
186 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
187 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400188 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700189 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500190 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
191 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
192 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
193 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600194 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500195}
196
Keith Buschedd10d32014-04-03 16:45:23 -0600197typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400198 struct nvme_completion *);
199
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500200struct nvme_cmd_info {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400201 nvme_completion_fn fn;
202 void *ctx;
Keith Buschc30341d2013-12-10 13:10:38 -0700203 int aborted;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700204 struct nvme_queue *nvmeq;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700205 struct nvme_iod iod[0];
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500206};
207
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700208/*
209 * Max size of iod being embedded in the request payload
210 */
211#define NVME_INT_PAGES 2
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100212#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
Chong Yuanfda631f2015-03-27 09:21:32 +0800213#define NVME_INT_MASK 0x01
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700214
215/*
216 * Will slightly overestimate the number of pages needed. This is OK
217 * as it only leads to a small amount of wasted memory for the lifetime of
218 * the I/O.
219 */
220static int nvme_npages(unsigned size, struct nvme_dev *dev)
221{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100222 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
223 dev->ctrl.page_size);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700224 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
225}
226
227static unsigned int nvme_cmd_size(struct nvme_dev *dev)
228{
229 unsigned int ret = sizeof(struct nvme_cmd_info);
230
231 ret += sizeof(struct nvme_iod);
232 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
233 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
234
235 return ret;
236}
237
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700238static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
239 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500240{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700241 struct nvme_dev *dev = data;
242 struct nvme_queue *nvmeq = dev->queues[0];
243
Keith Busch42483222015-06-01 09:29:54 -0600244 WARN_ON(hctx_idx != 0);
245 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
246 WARN_ON(nvmeq->tags);
247
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700248 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600249 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700250 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500251}
252
Keith Busch4af0e212015-06-08 10:08:13 -0600253static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
254{
255 struct nvme_queue *nvmeq = hctx->driver_data;
256
257 nvmeq->tags = NULL;
258}
259
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700260static int nvme_admin_init_request(void *data, struct request *req,
261 unsigned int hctx_idx, unsigned int rq_idx,
262 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600263{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700264 struct nvme_dev *dev = data;
265 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
266 struct nvme_queue *nvmeq = dev->queues[0];
267
268 BUG_ON(!nvmeq);
269 cmd->nvmeq = nvmeq;
270 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600271}
272
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700273static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
274 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500275{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700276 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600277 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500278
Keith Busch42483222015-06-01 09:29:54 -0600279 if (!nvmeq->tags)
280 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500281
Keith Busch42483222015-06-01 09:29:54 -0600282 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700283 hctx->driver_data = nvmeq;
284 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500285}
286
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700287static int nvme_init_request(void *data, struct request *req,
288 unsigned int hctx_idx, unsigned int rq_idx,
289 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500290{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700291 struct nvme_dev *dev = data;
292 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
293 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
294
295 BUG_ON(!nvmeq);
296 cmd->nvmeq = nvmeq;
297 return 0;
298}
299
300static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
301 nvme_completion_fn handler)
302{
303 cmd->fn = handler;
304 cmd->ctx = ctx;
305 cmd->aborted = 0;
Keith Buschc917dfe2015-01-07 18:55:48 -0700306 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500307}
308
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700309static void *iod_get_private(struct nvme_iod *iod)
310{
311 return (void *) (iod->private & ~0x1UL);
312}
313
314/*
315 * If bit 0 is set, the iod is embedded in the request payload.
316 */
317static bool iod_should_kfree(struct nvme_iod *iod)
318{
Chong Yuanfda631f2015-03-27 09:21:32 +0800319 return (iod->private & NVME_INT_MASK) == 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700320}
321
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400322/* Special values must be less than 0x1000 */
323#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
Matthew Wilcoxd2d87032011-02-07 15:55:59 -0500324#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
325#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
326#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -0500327
Keith Buschedd10d32014-04-03 16:45:23 -0600328static void special_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400329 struct nvme_completion *cqe)
330{
331 if (ctx == CMD_CTX_CANCELLED)
332 return;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400333 if (ctx == CMD_CTX_COMPLETED) {
Keith Buschedd10d32014-04-03 16:45:23 -0600334 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400335 "completed id %d twice on queue %d\n",
336 cqe->command_id, le16_to_cpup(&cqe->sq_id));
337 return;
338 }
339 if (ctx == CMD_CTX_INVALID) {
Keith Buschedd10d32014-04-03 16:45:23 -0600340 dev_warn(nvmeq->q_dmadev,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400341 "invalid id %d completed on queue %d\n",
342 cqe->command_id, le16_to_cpup(&cqe->sq_id));
343 return;
344 }
Keith Buschedd10d32014-04-03 16:45:23 -0600345 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400346}
347
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700348static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
349{
350 void *ctx;
351
352 if (fn)
353 *fn = cmd->fn;
354 ctx = cmd->ctx;
355 cmd->fn = special_completion;
356 cmd->ctx = CMD_CTX_CANCELLED;
357 return ctx;
358}
359
360static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
361 struct nvme_completion *cqe)
362{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700363 u32 result = le32_to_cpup(&cqe->result);
364 u16 status = le16_to_cpup(&cqe->status) >> 1;
365
366 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100367 ++nvmeq->dev->ctrl.event_limit;
Keith Buscha5768aa2015-06-01 14:28:14 -0600368 if (status != NVME_SC_SUCCESS)
369 return;
370
371 switch (result & 0xff07) {
372 case NVME_AER_NOTICE_NS_CHANGED:
373 dev_info(nvmeq->q_dmadev, "rescanning\n");
Keith Busch92f7a162015-10-23 11:42:02 -0600374 queue_work(nvme_workq, &nvmeq->dev->scan_work);
Keith Buscha5768aa2015-06-01 14:28:14 -0600375 default:
376 dev_warn(nvmeq->q_dmadev, "async event result %08x\n", result);
377 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700378}
379
380static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
381 struct nvme_completion *cqe)
382{
383 struct request *req = ctx;
384
385 u16 status = le16_to_cpup(&cqe->status) >> 1;
386 u32 result = le32_to_cpup(&cqe->result);
387
Keith Busch42483222015-06-01 09:29:54 -0600388 blk_mq_free_request(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700389
390 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +0100391 atomic_inc(&nvmeq->dev->ctrl.abort_limit);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700392}
393
Keith Buschedd10d32014-04-03 16:45:23 -0600394static void async_completion(struct nvme_queue *nvmeq, void *ctx,
Keith Busch4d115422013-12-10 13:10:40 -0700395 struct nvme_completion *cqe)
396{
397 struct async_cmd_info *cmdinfo = ctx;
398 cmdinfo->result = le32_to_cpup(&cqe->result);
399 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
400 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
Keith Busch42483222015-06-01 09:29:54 -0600401 blk_mq_free_request(cmdinfo->req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700402}
403
404static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
405 unsigned int tag)
406{
Keith Busch42483222015-06-01 09:29:54 -0600407 struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700408
409 return blk_mq_rq_to_pdu(req);
Keith Busch4d115422013-12-10 13:10:40 -0700410}
411
Matthew Wilcox184d2942011-05-11 21:36:38 -0400412/*
413 * Called with local interrupts disabled and the q_lock held. May not sleep.
414 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700415static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400416 nvme_completion_fn *fn)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500417{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700418 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400419 void *ctx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700420 if (tag >= nvmeq->q_depth) {
421 *fn = special_completion;
Matthew Wilcox48e3d392011-02-06 08:51:15 -0500422 return CMD_CTX_INVALID;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400423 }
Keith Busch859361a2012-08-02 14:05:59 -0600424 if (fn)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700425 *fn = cmd->fn;
426 ctx = cmd->ctx;
427 cmd->fn = special_completion;
428 cmd->ctx = CMD_CTX_COMPLETED;
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400429 return ctx;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500430}
431
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500432/**
Matthew Wilcox714a7a22011-03-16 16:28:24 -0400433 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500434 * @nvmeq: The queue to use
435 * @cmd: The command to send
436 *
437 * Safe to use from interrupt context
438 */
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530439static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
440 struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500441{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700442 u16 tail = nvmeq->sq_tail;
443
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600444 if (nvmeq->sq_cmds_io)
445 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
446 else
447 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
448
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500449 if (++tail == nvmeq->q_depth)
450 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500451 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500452 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500453}
454
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530455static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700456{
457 unsigned long flags;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700458 spin_lock_irqsave(&nvmeq->q_lock, flags);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530459 __nvme_submit_cmd(nvmeq, cmd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700460 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700461}
462
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500463static __le64 **iod_list(struct nvme_iod *iod)
464{
465 return ((void *)iod) + iod->offset;
466}
467
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700468static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
469 unsigned nseg, unsigned long private)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500470{
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700471 iod->private = private;
472 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
473 iod->npages = -1;
474 iod->length = nbytes;
475 iod->nents = 0;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500476}
477
478static struct nvme_iod *
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700479__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
480 unsigned long priv, gfp_t gfp)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500481{
482 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700483 sizeof(__le64 *) * nvme_npages(bytes, dev) +
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500484 sizeof(struct scatterlist) * nseg, gfp);
485
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700486 if (iod)
487 iod_init(iod, bytes, nseg, priv);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500488
489 return iod;
490}
491
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700492static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
493 gfp_t gfp)
494{
495 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
496 sizeof(struct nvme_dsm_range);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700497 struct nvme_iod *iod;
498
499 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
500 size <= NVME_INT_BYTES(dev)) {
501 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
502
503 iod = cmd->iod;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700504 iod_init(iod, size, rq->nr_phys_segments,
Chong Yuanfda631f2015-03-27 09:21:32 +0800505 (unsigned long) rq | NVME_INT_MASK);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700506 return iod;
507 }
508
509 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
510 (unsigned long) rq, gfp);
511}
512
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200513static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500514{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100515 const int last_prp = dev->ctrl.page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500516 int i;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500517 __le64 **list = iod_list(iod);
518 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500519
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500520 if (iod->npages == 0)
521 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
522 for (i = 0; i < iod->npages; i++) {
523 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500524 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500525 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500526 prp_dma = next_prp_dma;
527 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700528
529 if (iod_should_kfree(iod))
530 kfree(iod);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500531}
532
Keith Busch52b68d72015-02-23 09:16:21 -0700533#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700534static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
535{
536 if (be32_to_cpu(pi->ref_tag) == v)
537 pi->ref_tag = cpu_to_be32(p);
538}
539
540static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
541{
542 if (be32_to_cpu(pi->ref_tag) == p)
543 pi->ref_tag = cpu_to_be32(v);
544}
545
546/**
547 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
548 *
549 * The virtual start sector is the one that was originally submitted by the
550 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
551 * start sector may be different. Remap protection information to match the
552 * physical LBA on writes, and back to the original seed on reads.
553 *
554 * Type 0 and 3 do not have a ref tag, so no remapping required.
555 */
556static void nvme_dif_remap(struct request *req,
557 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
558{
559 struct nvme_ns *ns = req->rq_disk->private_data;
560 struct bio_integrity_payload *bip;
561 struct t10_pi_tuple *pi;
562 void *p, *pmap;
563 u32 i, nlb, ts, phys, virt;
564
565 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
566 return;
567
568 bip = bio_integrity(req->bio);
569 if (!bip)
570 return;
571
572 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700573
574 p = pmap;
575 virt = bip_get_seed(bip);
576 phys = nvme_block_nr(ns, blk_rq_pos(req));
577 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
Dan Williamsac6fc482015-10-21 13:20:18 -0400578 ts = ns->disk->queue->integrity.tuple_size;
Keith Busche1e5e562015-02-19 13:39:03 -0700579
580 for (i = 0; i < nlb; i++, virt++, phys++) {
581 pi = (struct t10_pi_tuple *)p;
582 dif_swap(phys, virt, pi);
583 p += ts;
584 }
585 kunmap_atomic(pmap);
586}
Keith Busch52b68d72015-02-23 09:16:21 -0700587#else /* CONFIG_BLK_DEV_INTEGRITY */
588static void nvme_dif_remap(struct request *req,
589 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
590{
591}
592static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
593{
594}
595static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
596{
597}
Keith Busch52b68d72015-02-23 09:16:21 -0700598#endif
599
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700600static void req_completion(struct nvme_queue *nvmeq, void *ctx,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500601 struct nvme_completion *cqe)
602{
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500603 struct nvme_iod *iod = ctx;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700604 struct request *req = iod_get_private(iod);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700605 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500606 u16 status = le16_to_cpup(&cqe->status) >> 1;
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200607 int error = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500608
Keith Buschedd10d32014-04-03 16:45:23 -0600609 if (unlikely(status)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700610 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
611 && (jiffies - req->start_time) < req->timeout) {
Keith Buschc9d3bf82015-01-07 18:55:52 -0700612 unsigned long flags;
613
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100614 nvme_unmap_data(nvmeq->dev, iod);
615
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700616 blk_mq_requeue_request(req);
Keith Buschc9d3bf82015-01-07 18:55:52 -0700617 spin_lock_irqsave(req->q->queue_lock, flags);
618 if (!blk_queue_stopped(req->q))
619 blk_mq_kick_requeue_list(req->q);
620 spin_unlock_irqrestore(req->q->queue_lock, flags);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100621 return;
Keith Buschedd10d32014-04-03 16:45:23 -0600622 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200623
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200624 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
Keith Busch17188bb2015-06-08 10:08:14 -0600625 if (cmd_rq->ctx == CMD_CTX_CANCELLED)
Christoph Hellwig297465c2015-11-26 12:58:11 +0100626 error = NVME_SC_CANCELLED;
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200627 else
628 error = status;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200629 } else {
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200630 error = nvme_error_status(status);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200631 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200632 }
633
Keith Buscha0a931d2015-05-22 12:28:31 -0600634 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
635 u32 result = le32_to_cpup(&cqe->result);
636 req->special = (void *)(uintptr_t)result;
637 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700638
639 if (cmd_rq->aborted)
Christoph Hellwige75ec752015-05-22 11:12:39 +0200640 dev_warn(nvmeq->dev->dev,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700641 "completing aborted command with status:%04x\n",
Christoph Hellwig81c04b92015-10-12 21:23:39 +0200642 error);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700643
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100644 nvme_unmap_data(nvmeq->dev, iod);
645 blk_mq_complete_request(req, error);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500646}
647
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200648static bool nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
649 int total_len)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500650{
Matthew Wilcox99802a72011-02-10 10:30:34 -0500651 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500652 int length = total_len;
653 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500654 int dma_len = sg_dma_len(sg);
655 u64 dma_addr = sg_dma_address(sg);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100656 u32 page_size = dev->ctrl.page_size;
Murali Iyerf137e0f2015-03-26 11:07:51 -0500657 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500658 __le64 *prp_list;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500659 __le64 **list = iod_list(iod);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500660 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500661 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500662
Keith Busch1d090622014-06-23 11:34:01 -0600663 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500664 if (length <= 0)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200665 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500666
Keith Busch1d090622014-06-23 11:34:01 -0600667 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500668 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600669 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500670 } else {
671 sg = sg_next(sg);
672 dma_addr = sg_dma_address(sg);
673 dma_len = sg_dma_len(sg);
674 }
675
Keith Busch1d090622014-06-23 11:34:01 -0600676 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600677 iod->first_dma = dma_addr;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200678 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500679 }
680
Keith Busch1d090622014-06-23 11:34:01 -0600681 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500682 if (nprps <= (256 / 8)) {
683 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500684 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500685 } else {
686 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500687 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500688 }
689
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200690 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400691 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600692 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500693 iod->npages = -1;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200694 return false;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400695 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500696 list[0] = prp_list;
697 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500698 i = 0;
699 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600700 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500701 __le64 *old_prp_list = prp_list;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200702 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500703 if (!prp_list)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200704 return false;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500705 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400706 prp_list[0] = old_prp_list[i - 1];
707 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
708 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500709 }
710 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600711 dma_len -= page_size;
712 dma_addr += page_size;
713 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500714 if (length <= 0)
715 break;
716 if (dma_len > 0)
717 continue;
718 BUG_ON(dma_len < 0);
719 sg = sg_next(sg);
720 dma_addr = sg_dma_address(sg);
721 dma_len = sg_dma_len(sg);
722 }
723
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200724 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500725}
726
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200727static int nvme_map_data(struct nvme_dev *dev, struct nvme_iod *iod,
728 struct nvme_command *cmnd)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200729{
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200730 struct request *req = iod_get_private(iod);
731 struct request_queue *q = req->q;
732 enum dma_data_direction dma_dir = rq_data_dir(req) ?
733 DMA_TO_DEVICE : DMA_FROM_DEVICE;
734 int ret = BLK_MQ_RQ_QUEUE_ERROR;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200735
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200736 sg_init_table(iod->sg, req->nr_phys_segments);
737 iod->nents = blk_rq_map_sg(q, req, iod->sg);
738 if (!iod->nents)
739 goto out;
740
741 ret = BLK_MQ_RQ_QUEUE_BUSY;
742 if (!dma_map_sg(dev->dev, iod->sg, iod->nents, dma_dir))
743 goto out;
744
745 if (!nvme_setup_prps(dev, iod, blk_rq_bytes(req)))
746 goto out_unmap;
747
748 ret = BLK_MQ_RQ_QUEUE_ERROR;
749 if (blk_integrity_rq(req)) {
750 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
751 goto out_unmap;
752
753 sg_init_table(iod->meta_sg, 1);
754 if (blk_rq_map_integrity_sg(q, req->bio, iod->meta_sg) != 1)
755 goto out_unmap;
756
757 if (rq_data_dir(req))
758 nvme_dif_remap(req, nvme_dif_prep);
759
760 if (!dma_map_sg(dev->dev, iod->meta_sg, 1, dma_dir))
761 goto out_unmap;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200762 }
763
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200764 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
765 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
766 if (blk_integrity_rq(req))
767 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg));
768 return BLK_MQ_RQ_QUEUE_OK;
769
770out_unmap:
771 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
772out:
773 return ret;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200774}
775
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100776static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod)
777{
778 struct request *req = iod_get_private(iod);
779 enum dma_data_direction dma_dir = rq_data_dir(req) ?
780 DMA_TO_DEVICE : DMA_FROM_DEVICE;
781
782 if (iod->nents) {
783 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
784 if (blk_integrity_rq(req)) {
785 if (!rq_data_dir(req))
786 nvme_dif_remap(req, nvme_dif_complete);
787 dma_unmap_sg(dev->dev, iod->meta_sg, 1, dma_dir);
788 }
789 }
790
791 nvme_free_iod(dev, iod);
792}
793
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700794/*
795 * We reuse the small pool to allocate the 16-byte range here as it is not
796 * worth having a special pool for these or additional cases to handle freeing
797 * the iod.
798 */
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200799static int nvme_setup_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
800 struct nvme_iod *iod, struct nvme_command *cmnd)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700801{
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200802 struct request *req = iod_get_private(iod);
803 struct nvme_dsm_range *range;
804
805 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
806 &iod->first_dma);
807 if (!range)
808 return BLK_MQ_RQ_QUEUE_BUSY;
809 iod_list(iod)[0] = (__le64 *)range;
810 iod->npages = 0;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700811
Keith Busch0e5e4f02012-11-09 16:33:05 -0700812 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700813 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
814 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700815
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200816 memset(cmnd, 0, sizeof(*cmnd));
817 cmnd->dsm.opcode = nvme_cmd_dsm;
818 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
819 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
820 cmnd->dsm.nr = 0;
821 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
822 return BLK_MQ_RQ_QUEUE_OK;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700823}
824
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200825/*
826 * NOTE: ns is NULL when called on the admin queue.
827 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700828static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
829 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600830{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700831 struct nvme_ns *ns = hctx->queue->queuedata;
832 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200833 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700834 struct request *req = bd->rq;
835 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
Keith Buschedd10d32014-04-03 16:45:23 -0600836 struct nvme_iod *iod;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200837 struct nvme_command cmnd;
838 int ret = BLK_MQ_RQ_QUEUE_OK;
Keith Buschedd10d32014-04-03 16:45:23 -0600839
Keith Busche1e5e562015-02-19 13:39:03 -0700840 /*
841 * If formated with metadata, require the block layer provide a buffer
842 * unless this namespace is formated such that the metadata can be
843 * stripped/generated by the controller with PRACT=1.
844 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200845 if (ns && ns->ms && !blk_integrity_rq(req)) {
Keith Busch71feb362015-06-19 11:07:30 -0600846 if (!(ns->pi_type && ns->ms == 8) &&
847 req->cmd_type != REQ_TYPE_DRV_PRIV) {
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200848 blk_mq_complete_request(req, -EFAULT);
Keith Busche1e5e562015-02-19 13:39:03 -0700849 return BLK_MQ_RQ_QUEUE_OK;
850 }
851 }
852
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200853 iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
Keith Buschedd10d32014-04-03 16:45:23 -0600854 if (!iod)
Jens Axboefe543032014-12-11 13:58:39 -0700855 return BLK_MQ_RQ_QUEUE_BUSY;
Keith Buschedd10d32014-04-03 16:45:23 -0600856
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700857 if (req->cmd_flags & REQ_DISCARD) {
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200858 ret = nvme_setup_discard(nvmeq, ns, iod, &cmnd);
859 } else {
860 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
861 memcpy(&cmnd, req->cmd, sizeof(cmnd));
862 else if (req->cmd_flags & REQ_FLUSH)
863 nvme_setup_flush(ns, &cmnd);
864 else
865 nvme_setup_rw(ns, req, &cmnd);
Keith Buschedd10d32014-04-03 16:45:23 -0600866
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200867 if (req->nr_phys_segments)
868 ret = nvme_map_data(dev, iod, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700869 }
870
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200871 if (ret)
872 goto out;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700873
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200874 cmnd.common.command_id = req->tag;
875 nvme_set_info(cmd, iod, req_completion);
876
877 spin_lock_irq(&nvmeq->q_lock);
878 __nvme_submit_cmd(nvmeq, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700879 nvme_process_cq(nvmeq);
880 spin_unlock_irq(&nvmeq->q_lock);
881 return BLK_MQ_RQ_QUEUE_OK;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200882out:
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200883 nvme_free_iod(dev, iod);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200884 return ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500885}
886
Jens Axboea0fa9642015-11-03 20:37:26 -0700887static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500888{
Matthew Wilcox82123462011-01-20 13:24:06 -0500889 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500890
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500891 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500892 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500893
894 for (;;) {
Matthew Wilcoxc2f5b652011-10-15 07:33:46 -0400895 void *ctx;
896 nvme_completion_fn fn;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500897 struct nvme_completion cqe = nvmeq->cqes[head];
Matthew Wilcox82123462011-01-20 13:24:06 -0500898 if ((le16_to_cpu(cqe.status) & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500899 break;
900 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
901 if (++head == nvmeq->q_depth) {
902 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500903 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500904 }
Jens Axboea0fa9642015-11-03 20:37:26 -0700905 if (tag && *tag == cqe.command_id)
906 *tag = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700907 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
Keith Buschedd10d32014-04-03 16:45:23 -0600908 fn(nvmeq, ctx, &cqe);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500909 }
910
911 /* If the controller ignores the cq head doorbell and continuously
912 * writes to the queue, it is theoretically possible to wrap around
913 * the queue twice and mistakenly return IRQ_NONE. Linux only
914 * requires that 0.1% of your interrupts are handled, so this isn't
915 * a big problem.
916 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500917 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Jens Axboea0fa9642015-11-03 20:37:26 -0700918 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500919
Keith Busch604e8c82015-11-20 08:38:13 -0700920 if (likely(nvmeq->cq_vector >= 0))
921 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500922 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500923 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500924
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400925 nvmeq->cqe_seen = 1;
Jens Axboea0fa9642015-11-03 20:37:26 -0700926}
927
928static void nvme_process_cq(struct nvme_queue *nvmeq)
929{
930 __nvme_process_cq(nvmeq, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500931}
932
933static irqreturn_t nvme_irq(int irq, void *data)
934{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500935 irqreturn_t result;
936 struct nvme_queue *nvmeq = data;
937 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400938 nvme_process_cq(nvmeq);
939 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
940 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500941 spin_unlock(&nvmeq->q_lock);
942 return result;
943}
944
945static irqreturn_t nvme_irq_check(int irq, void *data)
946{
947 struct nvme_queue *nvmeq = data;
948 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
949 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
950 return IRQ_NONE;
951 return IRQ_WAKE_THREAD;
952}
953
Jens Axboea0fa9642015-11-03 20:37:26 -0700954static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
955{
956 struct nvme_queue *nvmeq = hctx->driver_data;
957
958 if ((le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
959 nvmeq->cq_phase) {
960 spin_lock_irq(&nvmeq->q_lock);
961 __nvme_process_cq(nvmeq, &tag);
962 spin_unlock_irq(&nvmeq->q_lock);
963
964 if (tag == -1)
965 return 1;
966 }
967
968 return 0;
969}
970
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700971static int nvme_submit_async_admin_req(struct nvme_dev *dev)
972{
973 struct nvme_queue *nvmeq = dev->queues[0];
974 struct nvme_command c;
975 struct nvme_cmd_info *cmd_info;
976 struct request *req;
977
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100978 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100979 BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED);
Dan Carpenter9f173b32014-11-05 23:39:09 +0300980 if (IS_ERR(req))
981 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700982
Keith Buschc917dfe2015-01-07 18:55:48 -0700983 req->cmd_flags |= REQ_NO_TIMEOUT;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700984 cmd_info = blk_mq_rq_to_pdu(req);
Keith Busch1efccc92015-03-31 10:37:17 -0600985 nvme_set_info(cmd_info, NULL, async_req_completion);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700986
987 memset(&c, 0, sizeof(c));
988 c.common.opcode = nvme_admin_async_event;
989 c.common.command_id = req->tag;
990
Keith Busch42483222015-06-01 09:29:54 -0600991 blk_mq_free_request(req);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530992 __nvme_submit_cmd(nvmeq, &c);
993 return 0;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700994}
995
996static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
Keith Busch4d115422013-12-10 13:10:40 -0700997 struct nvme_command *cmd,
998 struct async_cmd_info *cmdinfo, unsigned timeout)
999{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001000 struct nvme_queue *nvmeq = dev->queues[0];
1001 struct request *req;
1002 struct nvme_cmd_info *cmd_rq;
Keith Busch4d115422013-12-10 13:10:40 -07001003
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001004 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE, 0);
Dan Carpenter9f173b32014-11-05 23:39:09 +03001005 if (IS_ERR(req))
1006 return PTR_ERR(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001007
1008 req->timeout = timeout;
1009 cmd_rq = blk_mq_rq_to_pdu(req);
1010 cmdinfo->req = req;
1011 nvme_set_info(cmd_rq, cmdinfo, async_completion);
Keith Busch4d115422013-12-10 13:10:40 -07001012 cmdinfo->status = -EINTR;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001013
1014 cmd->common.command_id = req->tag;
1015
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301016 nvme_submit_cmd(nvmeq, cmd);
1017 return 0;
Keith Busch4d115422013-12-10 13:10:40 -07001018}
1019
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001020static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1021{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001022 struct nvme_command c;
1023
1024 memset(&c, 0, sizeof(c));
1025 c.delete_queue.opcode = opcode;
1026 c.delete_queue.qid = cpu_to_le16(id);
1027
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001028 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001029}
1030
1031static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1032 struct nvme_queue *nvmeq)
1033{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001034 struct nvme_command c;
1035 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1036
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001037 /*
1038 * Note: we (ab)use the fact the the prp fields survive if no data
1039 * is attached to the request.
1040 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001041 memset(&c, 0, sizeof(c));
1042 c.create_cq.opcode = nvme_admin_create_cq;
1043 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1044 c.create_cq.cqid = cpu_to_le16(qid);
1045 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1046 c.create_cq.cq_flags = cpu_to_le16(flags);
1047 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1048
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001049 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001050}
1051
1052static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1053 struct nvme_queue *nvmeq)
1054{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001055 struct nvme_command c;
1056 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1057
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001058 /*
1059 * Note: we (ab)use the fact the the prp fields survive if no data
1060 * is attached to the request.
1061 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001062 memset(&c, 0, sizeof(c));
1063 c.create_sq.opcode = nvme_admin_create_sq;
1064 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1065 c.create_sq.sqid = cpu_to_le16(qid);
1066 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1067 c.create_sq.sq_flags = cpu_to_le16(flags);
1068 c.create_sq.cqid = cpu_to_le16(qid);
1069
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001070 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001071}
1072
1073static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1074{
1075 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1076}
1077
1078static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1079{
1080 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1081}
1082
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001083static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
Keith Buschc30341d2013-12-10 13:10:38 -07001084{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001085 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1086 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001087 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001088 struct request *abort_req;
1089 struct nvme_cmd_info *abort_cmd;
1090 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -07001091
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001092 /*
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001093 * Shutdown immediately if controller times out while starting. The
1094 * reset work will see the pci device disabled when it gets the forced
1095 * cancellation error. All outstanding requests are completed on
1096 * shutdown, so we return BLK_EH_HANDLED.
1097 */
1098 if (test_bit(NVME_CTRL_RESETTING, &dev->flags)) {
1099 dev_warn(dev->dev,
1100 "I/O %d QID %d timeout, disable controller\n",
1101 req->tag, nvmeq->qid);
1102 nvme_dev_shutdown(dev);
1103 req->errors = NVME_SC_CANCELLED;
1104 return BLK_EH_HANDLED;
1105 }
1106
1107 /*
1108 * Shutdown the controller immediately and schedule a reset if the
1109 * command was already aborted once before and still hasn't been
1110 * returned to the driver, or if this is the admin queue.
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001111 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001112 if (!nvmeq->qid || cmd_rq->aborted) {
Keith Busche1569a12015-11-26 12:11:07 +01001113 dev_warn(dev->dev,
1114 "I/O %d QID %d timeout, reset controller\n",
1115 req->tag, nvmeq->qid);
1116 nvme_dev_shutdown(dev);
1117 queue_work(nvme_workq, &dev->reset_work);
1118
1119 /*
1120 * Mark the request as handled, since the inline shutdown
1121 * forces all outstanding requests to complete.
1122 */
1123 req->errors = NVME_SC_CANCELLED;
1124 return BLK_EH_HANDLED;
Keith Buschc30341d2013-12-10 13:10:38 -07001125 }
1126
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001127 if (atomic_dec_and_test(&dev->ctrl.abort_limit))
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001128 return BLK_EH_RESET_TIMER;
Keith Buschc30341d2013-12-10 13:10:38 -07001129
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001130 abort_req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001131 BLK_MQ_REQ_NOWAIT);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001132 if (IS_ERR(abort_req)) {
1133 atomic_inc(&dev->ctrl.abort_limit);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001134 return BLK_EH_RESET_TIMER;
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001135 }
Keith Buschc30341d2013-12-10 13:10:38 -07001136
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001137 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1138 nvme_set_info(abort_cmd, abort_req, abort_completion);
1139
Keith Buschc30341d2013-12-10 13:10:38 -07001140 memset(&cmd, 0, sizeof(cmd));
1141 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001142 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001143 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001144 cmd.abort.command_id = abort_req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001145
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001146 cmd_rq->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -07001147
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001148 dev_warn(nvmeq->q_dmadev, "I/O %d QID %d timeout, aborting\n",
1149 req->tag, nvmeq->qid);
Sunad Bhandarye3f879b2015-07-31 18:56:58 +05301150 nvme_submit_cmd(dev->queues[0], &cmd);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001151
1152 /*
1153 * The aborted req will be completed on receiving the abort req.
1154 * We enable the timer again. If hit twice, it'll cause a device reset,
1155 * as the device then is in a faulty state.
1156 */
1157 return BLK_EH_RESET_TIMER;
Keith Buschc30341d2013-12-10 13:10:38 -07001158}
1159
Keith Busch42483222015-06-01 09:29:54 -06001160static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001161{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001162 struct nvme_queue *nvmeq = data;
1163 void *ctx;
1164 nvme_completion_fn fn;
1165 struct nvme_cmd_info *cmd;
Keith Buschcef6a942015-01-07 18:55:51 -07001166 struct nvme_completion cqe;
1167
1168 if (!blk_mq_request_started(req))
1169 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001170
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001171 cmd = blk_mq_rq_to_pdu(req);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001172
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001173 if (cmd->ctx == CMD_CTX_CANCELLED)
1174 return;
1175
Keith Buschcef6a942015-01-07 18:55:51 -07001176 if (blk_queue_dying(req->q))
1177 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1178 else
1179 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1180
1181
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001182 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1183 req->tag, nvmeq->qid);
1184 ctx = cancel_cmd_info(cmd, &fn);
1185 fn(nvmeq, ctx, &cqe);
1186}
1187
Keith Buschf435c282014-07-07 09:14:42 -06001188static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001189{
1190 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1191 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001192 if (nvmeq->sq_cmds)
1193 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001194 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1195 kfree(nvmeq);
1196}
1197
Keith Buscha1a5ef92013-12-16 13:50:00 -05001198static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001199{
1200 int i;
1201
Keith Buscha1a5ef92013-12-16 13:50:00 -05001202 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001203 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001204 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001205 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001206 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001207 }
Keith Busch22404272013-07-15 15:02:20 -06001208}
1209
Keith Busch4d115422013-12-10 13:10:40 -07001210/**
1211 * nvme_suspend_queue - put queue into suspended state
1212 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001213 */
1214static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001215{
Keith Busch2b25d982014-12-22 12:59:04 -07001216 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001217
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001218 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001219 if (nvmeq->cq_vector == -1) {
1220 spin_unlock_irq(&nvmeq->q_lock);
1221 return 1;
1222 }
1223 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001224 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001225 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001226 spin_unlock_irq(&nvmeq->q_lock);
1227
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001228 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
1229 blk_mq_freeze_queue_start(nvmeq->dev->ctrl.admin_q);
Keith Busch6df3dbc2015-03-26 13:49:33 -06001230
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001231 irq_set_affinity_hint(vector, NULL);
1232 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001233
Keith Busch4d115422013-12-10 13:10:40 -07001234 return 0;
1235}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001236
Keith Busch4d115422013-12-10 13:10:40 -07001237static void nvme_clear_queue(struct nvme_queue *nvmeq)
1238{
Keith Busch22404272013-07-15 15:02:20 -06001239 spin_lock_irq(&nvmeq->q_lock);
Keith Busch42483222015-06-01 09:29:54 -06001240 if (nvmeq->tags && *nvmeq->tags)
1241 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001242 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001243}
1244
Keith Busch4d115422013-12-10 13:10:40 -07001245static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1246{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001247 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001248
1249 if (!nvmeq)
1250 return;
1251 if (nvme_suspend_queue(nvmeq))
1252 return;
1253
Keith Busch0e53d182013-12-10 13:10:39 -07001254 /* Don't tell the adapter to delete the admin queue.
1255 * Don't tell a removed adapter to delete IO queues. */
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001256 if (qid && readl(dev->bar + NVME_REG_CSTS) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001257 adapter_delete_sq(dev, qid);
1258 adapter_delete_cq(dev, qid);
1259 }
Keith Busch07836e62015-02-19 10:34:48 -07001260
1261 spin_lock_irq(&nvmeq->q_lock);
1262 nvme_process_cq(nvmeq);
1263 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001264}
1265
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001266static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1267 int entry_size)
1268{
1269 int q_depth = dev->q_depth;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001270 unsigned q_size_aligned = roundup(q_depth * entry_size,
1271 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001272
1273 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001274 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001275 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
Jon Derrickc45f5c92015-07-21 15:08:13 -06001276 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001277
1278 /*
1279 * Ensure the reduced q_depth is above some threshold where it
1280 * would be better to map queues in system memory with the
1281 * original depth
1282 */
1283 if (q_depth < 64)
1284 return -ENOMEM;
1285 }
1286
1287 return q_depth;
1288}
1289
1290static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1291 int qid, int depth)
1292{
1293 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001294 unsigned offset = (qid - 1) * roundup(SQ_SIZE(depth),
1295 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001296 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1297 nvmeq->sq_cmds_io = dev->cmb + offset;
1298 } else {
1299 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1300 &nvmeq->sq_dma_addr, GFP_KERNEL);
1301 if (!nvmeq->sq_cmds)
1302 return -ENOMEM;
1303 }
1304
1305 return 0;
1306}
1307
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001308static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001309 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001310{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001311 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001312 if (!nvmeq)
1313 return NULL;
1314
Christoph Hellwige75ec752015-05-22 11:12:39 +02001315 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001316 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001317 if (!nvmeq->cqes)
1318 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001319
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001320 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001321 goto free_cqdma;
1322
Christoph Hellwige75ec752015-05-22 11:12:39 +02001323 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001324 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001325 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001326 dev->ctrl.instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001327 spin_lock_init(&nvmeq->q_lock);
1328 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001329 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001330 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001331 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001332 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001333 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001334 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001335
Jon Derrick36a7e992015-05-27 12:26:23 -06001336 /* make sure queue descriptor is set before queue count, for kthread */
1337 mb();
1338 dev->queue_count++;
1339
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001340 return nvmeq;
1341
1342 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001343 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001344 nvmeq->cq_dma_addr);
1345 free_nvmeq:
1346 kfree(nvmeq);
1347 return NULL;
1348}
1349
Matthew Wilcox30010822011-01-20 09:10:15 -05001350static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1351 const char *name)
1352{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001353 if (use_threaded_interrupts)
1354 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001355 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001356 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001357 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001358 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001359}
1360
Keith Busch22404272013-07-15 15:02:20 -06001361static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001362{
Keith Busch22404272013-07-15 15:02:20 -06001363 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001364
Keith Busch7be50e92014-09-10 15:48:47 -06001365 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001366 nvmeq->sq_tail = 0;
1367 nvmeq->cq_head = 0;
1368 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001369 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001370 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001371 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001372 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001373}
1374
1375static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1376{
1377 struct nvme_dev *dev = nvmeq->dev;
1378 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001379
Keith Busch2b25d982014-12-22 12:59:04 -07001380 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001381 result = adapter_alloc_cq(dev, qid, nvmeq);
1382 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001383 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001384
1385 result = adapter_alloc_sq(dev, qid, nvmeq);
1386 if (result < 0)
1387 goto release_cq;
1388
Matthew Wilcox3193f072014-01-27 15:57:22 -05001389 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001390 if (result < 0)
1391 goto release_sq;
1392
Keith Busch22404272013-07-15 15:02:20 -06001393 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001394 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001395
1396 release_sq:
1397 adapter_delete_sq(dev, qid);
1398 release_cq:
1399 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001400 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001401}
1402
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001403static struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001404 .queue_rq = nvme_queue_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001405 .map_queue = blk_mq_map_queue,
1406 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001407 .exit_hctx = nvme_admin_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001408 .init_request = nvme_admin_init_request,
1409 .timeout = nvme_timeout,
1410};
1411
1412static struct blk_mq_ops nvme_mq_ops = {
1413 .queue_rq = nvme_queue_rq,
1414 .map_queue = blk_mq_map_queue,
1415 .init_hctx = nvme_init_hctx,
1416 .init_request = nvme_init_request,
1417 .timeout = nvme_timeout,
Jens Axboea0fa9642015-11-03 20:37:26 -07001418 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001419};
1420
Keith Buschea191d22015-01-07 18:55:49 -07001421static void nvme_dev_remove_admin(struct nvme_dev *dev)
1422{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001423 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
1424 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001425 blk_mq_free_tag_set(&dev->admin_tagset);
1426 }
1427}
1428
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001429static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1430{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001431 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001432 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1433 dev->admin_tagset.nr_hw_queues = 1;
1434 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
Keith Busch1efccc92015-03-31 10:37:17 -06001435 dev->admin_tagset.reserved_tags = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001436 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001437 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001438 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001439 dev->admin_tagset.driver_data = dev;
1440
1441 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1442 return -ENOMEM;
1443
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001444 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1445 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001446 blk_mq_free_tag_set(&dev->admin_tagset);
1447 return -ENOMEM;
1448 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001449 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001450 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001451 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001452 return -ENODEV;
1453 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001454 } else
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001455 blk_mq_unfreeze_queue(dev->ctrl.admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001456
1457 return 0;
1458}
1459
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001460static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001461{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001462 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001463 u32 aqa;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001464 u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001465 struct nvme_queue *nvmeq;
1466
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001467 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1) ?
Keith Buschdfbac8c2015-08-10 15:20:40 -06001468 NVME_CAP_NSSRC(cap) : 0;
1469
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001470 if (dev->subsystem &&
1471 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1472 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001473
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001474 result = nvme_disable_ctrl(&dev->ctrl, cap);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001475 if (result < 0)
1476 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001477
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001478 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001479 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001480 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001481 if (!nvmeq)
1482 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001483 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001484
1485 aqa = nvmeq->q_depth - 1;
1486 aqa |= aqa << 16;
1487
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001488 writel(aqa, dev->bar + NVME_REG_AQA);
1489 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1490 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001491
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001492 result = nvme_enable_ctrl(&dev->ctrl, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001493 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001494 goto free_nvmeq;
1495
Keith Busch2b25d982014-12-22 12:59:04 -07001496 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001497 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001498 if (result) {
1499 nvmeq->cq_vector = -1;
Keith Busch0fb59cb2015-01-07 18:55:50 -07001500 goto free_nvmeq;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001501 }
Keith Busch025c5572013-05-01 13:07:51 -06001502
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001503 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001504
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001505 free_nvmeq:
1506 nvme_free_queues(dev, 0);
1507 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001508}
1509
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001510static int nvme_kthread(void *data)
1511{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001512 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001513
1514 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001515 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001516 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001517 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001518 int i;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001519 u32 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001520
Christoph Hellwig846cc052015-11-26 12:10:29 +01001521 /*
1522 * Skip controllers currently under reset.
1523 */
1524 if (work_pending(&dev->reset_work) || work_busy(&dev->reset_work))
1525 continue;
1526
Keith Buschdfbac8c2015-08-10 15:20:40 -06001527 if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
1528 csts & NVME_CSTS_CFS) {
Christoph Hellwig846cc052015-11-26 12:10:29 +01001529 if (queue_work(nvme_workq, &dev->reset_work)) {
Christoph Hellwig906678922015-10-02 18:49:23 +02001530 dev_warn(dev->dev,
1531 "Failed status: %x, reset controller\n",
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001532 readl(dev->bar + NVME_REG_CSTS));
Christoph Hellwig906678922015-10-02 18:49:23 +02001533 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07001534 continue;
1535 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001536 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001537 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001538 if (!nvmeq)
1539 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001540 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001541 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06001542
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001543 while (i == 0 && dev->ctrl.event_limit > 0) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001544 if (nvme_submit_async_admin_req(dev))
Keith Busch6fccf932014-06-18 13:58:57 -06001545 break;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001546 dev->ctrl.event_limit--;
Keith Busch6fccf932014-06-18 13:58:57 -06001547 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001548 spin_unlock_irq(&nvmeq->q_lock);
1549 }
1550 }
1551 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001552 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001553 }
1554 return 0;
1555}
1556
Christoph Hellwig749941f2015-11-26 11:46:39 +01001557static int nvme_create_io_queues(struct nvme_dev *dev)
Keith Busch42f61422014-03-24 10:46:25 -06001558{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001559 unsigned i;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001560 int ret = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001561
Christoph Hellwig749941f2015-11-26 11:46:39 +01001562 for (i = dev->queue_count; i <= dev->max_qid; i++) {
1563 if (!nvme_alloc_queue(dev, i, dev->q_depth)) {
1564 ret = -ENOMEM;
Keith Busch42f61422014-03-24 10:46:25 -06001565 break;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001566 }
1567 }
Keith Busch42f61422014-03-24 10:46:25 -06001568
Christoph Hellwig749941f2015-11-26 11:46:39 +01001569 for (i = dev->online_queues; i <= dev->queue_count - 1; i++) {
1570 ret = nvme_create_queue(dev->queues[i], i);
1571 if (ret) {
Christoph Hellwig2659e572015-10-02 18:51:31 +02001572 nvme_free_queues(dev, i);
Keith Busch42f61422014-03-24 10:46:25 -06001573 break;
Christoph Hellwig2659e572015-10-02 18:51:31 +02001574 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001575 }
1576
1577 /*
1578 * Ignore failing Create SQ/CQ commands, we can continue with less
1579 * than the desired aount of queues, and even a controller without
1580 * I/O queues an still be used to issue admin commands. This might
1581 * be useful to upgrade a buggy firmware for example.
1582 */
1583 return ret >= 0 ? 0 : ret;
Keith Busch42f61422014-03-24 10:46:25 -06001584}
1585
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001586static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1587{
1588 u64 szu, size, offset;
1589 u32 cmbloc;
1590 resource_size_t bar_size;
1591 struct pci_dev *pdev = to_pci_dev(dev->dev);
1592 void __iomem *cmb;
1593 dma_addr_t dma_addr;
1594
1595 if (!use_cmb_sqes)
1596 return NULL;
1597
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001598 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001599 if (!(NVME_CMB_SZ(dev->cmbsz)))
1600 return NULL;
1601
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001602 cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001603
1604 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1605 size = szu * NVME_CMB_SZ(dev->cmbsz);
1606 offset = szu * NVME_CMB_OFST(cmbloc);
1607 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
1608
1609 if (offset > bar_size)
1610 return NULL;
1611
1612 /*
1613 * Controllers may support a CMB size larger than their BAR,
1614 * for example, due to being behind a bridge. Reduce the CMB to
1615 * the reported size of the BAR
1616 */
1617 if (size > bar_size - offset)
1618 size = bar_size - offset;
1619
1620 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
1621 cmb = ioremap_wc(dma_addr, size);
1622 if (!cmb)
1623 return NULL;
1624
1625 dev->cmb_dma_addr = dma_addr;
1626 dev->cmb_size = size;
1627 return cmb;
1628}
1629
1630static inline void nvme_release_cmb(struct nvme_dev *dev)
1631{
1632 if (dev->cmb) {
1633 iounmap(dev->cmb);
1634 dev->cmb = NULL;
1635 }
1636}
1637
Keith Busch9d713c22013-07-15 15:02:24 -06001638static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1639{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001640 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001641}
1642
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001643static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001644{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001645 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02001646 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch42f61422014-03-24 10:46:25 -06001647 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001648
Keith Busch42f61422014-03-24 10:46:25 -06001649 nr_io_queues = num_possible_cpus();
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001650 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
1651 if (result < 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001652 return result;
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001653
1654 /*
1655 * Degraded controllers might return an error when setting the queue
1656 * count. We still want to be able to bring them online and offer
1657 * access to the admin queue, as that might be only way to fix them up.
1658 */
1659 if (result > 0) {
1660 dev_err(dev->dev, "Could not set queue count (%d)\n", result);
1661 nr_io_queues = 0;
1662 result = 0;
1663 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001664
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001665 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1666 result = nvme_cmb_qdepth(dev, nr_io_queues,
1667 sizeof(struct nvme_command));
1668 if (result > 0)
1669 dev->q_depth = result;
1670 else
1671 nvme_release_cmb(dev);
1672 }
1673
Keith Busch9d713c22013-07-15 15:02:24 -06001674 size = db_bar_size(dev, nr_io_queues);
1675 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001676 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001677 do {
1678 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1679 if (dev->bar)
1680 break;
1681 if (!--nr_io_queues)
1682 return -ENOMEM;
1683 size = db_bar_size(dev, nr_io_queues);
1684 } while (1);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001685 dev->dbs = dev->bar + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07001686 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001687 }
1688
Keith Busch9d713c22013-07-15 15:02:24 -06001689 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05001690 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001691
Jens Axboee32efbf2014-11-14 09:49:26 -07001692 /*
1693 * If we enable msix early due to not intx, disable it again before
1694 * setting up the full range we need.
1695 */
1696 if (!pdev->irq)
1697 pci_disable_msix(pdev);
1698
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001699 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001700 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001701 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
1702 if (vecs < 0) {
1703 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
1704 if (vecs < 0) {
1705 vecs = 1;
1706 } else {
1707 for (i = 0; i < vecs; i++)
1708 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001709 }
1710 }
1711
Matthew Wilcox063a8092013-06-20 10:53:48 -04001712 /*
1713 * Should investigate if there's a performance win from allocating
1714 * more queues than interrupt vectors; it might allow the submission
1715 * path to scale better, even if the receive path is limited by the
1716 * number of interrupts.
1717 */
1718 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06001719 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001720
Matthew Wilcox3193f072014-01-27 15:57:22 -05001721 result = queue_request_irq(dev, adminq, adminq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001722 if (result) {
1723 adminq->cq_vector = -1;
Keith Busch22404272013-07-15 15:02:20 -06001724 goto free_queues;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001725 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001726
Keith Buschcd638942013-07-15 15:02:23 -06001727 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06001728 nvme_free_queues(dev, nr_io_queues + 1);
Christoph Hellwig749941f2015-11-26 11:46:39 +01001729 return nvme_create_io_queues(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001730
Keith Busch22404272013-07-15 15:02:20 -06001731 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05001732 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06001733 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001734}
1735
Keith Buschbda4e0f2015-09-03 08:18:17 -06001736static void nvme_set_irq_hints(struct nvme_dev *dev)
1737{
1738 struct nvme_queue *nvmeq;
1739 int i;
1740
1741 for (i = 0; i < dev->online_queues; i++) {
1742 nvmeq = dev->queues[i];
1743
1744 if (!nvmeq->tags || !(*nvmeq->tags))
1745 continue;
1746
1747 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
1748 blk_mq_tags_cpumask(*nvmeq->tags));
1749 }
1750}
1751
Keith Buscha5768aa2015-06-01 14:28:14 -06001752static void nvme_dev_scan(struct work_struct *work)
1753{
1754 struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06001755
1756 if (!dev->tagset.tags)
1757 return;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001758 nvme_scan_namespaces(&dev->ctrl);
Keith Buschbda4e0f2015-09-03 08:18:17 -06001759 nvme_set_irq_hints(dev);
Keith Buscha5768aa2015-06-01 14:28:14 -06001760}
1761
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001762/*
1763 * Return: error value if an error occurred setting up the queues or calling
1764 * Identify Device. 0 if these succeeded, even if adding some of the
1765 * namespaces failed. At the moment, these failures are silent. TBD which
1766 * failures should be reported.
1767 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001768static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001769{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001770 if (!dev->ctrl.tagset) {
Keith Buschffe77042015-06-08 10:08:15 -06001771 dev->tagset.ops = &nvme_mq_ops;
1772 dev->tagset.nr_hw_queues = dev->online_queues - 1;
1773 dev->tagset.timeout = NVME_IO_TIMEOUT;
1774 dev->tagset.numa_node = dev_to_node(dev->dev);
1775 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001776 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06001777 dev->tagset.cmd_size = nvme_cmd_size(dev);
1778 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
1779 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001780
Keith Buschffe77042015-06-08 10:08:15 -06001781 if (blk_mq_alloc_tag_set(&dev->tagset))
1782 return 0;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001783 dev->ctrl.tagset = &dev->tagset;
Keith Buschffe77042015-06-08 10:08:15 -06001784 }
Keith Busch92f7a162015-10-23 11:42:02 -06001785 queue_work(nvme_workq, &dev->scan_work);
Keith Busche1e5e562015-02-19 13:39:03 -07001786 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001787}
1788
Keith Busch0877cb02013-07-15 15:02:19 -06001789static int nvme_dev_map(struct nvme_dev *dev)
1790{
Keith Busch42f61422014-03-24 10:46:25 -06001791 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06001792 int bars, result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001793 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001794
1795 if (pci_enable_device_mem(pdev))
1796 return result;
1797
1798 dev->entry[0].vector = pdev->irq;
1799 pci_set_master(pdev);
1800 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07001801 if (!bars)
1802 goto disable_pci;
1803
Keith Busch0877cb02013-07-15 15:02:19 -06001804 if (pci_request_selected_regions(pdev, bars, "nvme"))
1805 goto disable_pci;
1806
Christoph Hellwige75ec752015-05-22 11:12:39 +02001807 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
1808 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01001809 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06001810
Keith Busch0877cb02013-07-15 15:02:19 -06001811 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1812 if (!dev->bar)
1813 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07001814
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001815 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07001816 result = -ENODEV;
1817 goto unmap;
1818 }
Jens Axboee32efbf2014-11-14 09:49:26 -07001819
1820 /*
1821 * Some devices don't advertse INTx interrupts, pre-enable a single
1822 * MSIX vec for setup. We'll adjust this later.
1823 */
1824 if (!pdev->irq) {
1825 result = pci_enable_msix(pdev, dev->entry, 1);
1826 if (result < 0)
1827 goto unmap;
1828 }
1829
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001830 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
1831
Keith Busch42f61422014-03-24 10:46:25 -06001832 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
1833 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001834 dev->dbs = dev->bar + 4096;
1835 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2))
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001836 dev->cmb = nvme_map_cmb(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001837
1838 return 0;
1839
Keith Busch0e53d182013-12-10 13:10:39 -07001840 unmap:
1841 iounmap(dev->bar);
1842 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06001843 disable:
1844 pci_release_regions(pdev);
1845 disable_pci:
1846 pci_disable_device(pdev);
1847 return result;
1848}
1849
1850static void nvme_dev_unmap(struct nvme_dev *dev)
1851{
Christoph Hellwige75ec752015-05-22 11:12:39 +02001852 struct pci_dev *pdev = to_pci_dev(dev->dev);
1853
1854 if (pdev->msi_enabled)
1855 pci_disable_msi(pdev);
1856 else if (pdev->msix_enabled)
1857 pci_disable_msix(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001858
1859 if (dev->bar) {
1860 iounmap(dev->bar);
1861 dev->bar = NULL;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001862 pci_release_regions(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001863 }
1864
Christoph Hellwige75ec752015-05-22 11:12:39 +02001865 if (pci_is_enabled(pdev))
1866 pci_disable_device(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001867}
1868
Keith Busch4d115422013-12-10 13:10:40 -07001869struct nvme_delq_ctx {
1870 struct task_struct *waiter;
1871 struct kthread_worker *worker;
1872 atomic_t refcount;
1873};
1874
1875static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
1876{
1877 dq->waiter = current;
1878 mb();
1879
1880 for (;;) {
1881 set_current_state(TASK_KILLABLE);
1882 if (!atomic_read(&dq->refcount))
1883 break;
1884 if (!schedule_timeout(ADMIN_TIMEOUT) ||
1885 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07001886 /*
1887 * Disable the controller first since we can't trust it
1888 * at this point, but leave the admin queue enabled
1889 * until all queue deletion requests are flushed.
1890 * FIXME: This may take a while if there are more h/w
1891 * queues than admin tags.
1892 */
Keith Busch4d115422013-12-10 13:10:40 -07001893 set_current_state(TASK_RUNNING);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001894 nvme_disable_ctrl(&dev->ctrl,
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001895 lo_hi_readq(dev->bar + NVME_REG_CAP));
Keith Busch0fb59cb2015-01-07 18:55:50 -07001896 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07001897 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07001898 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07001899 return;
1900 }
1901 }
1902 set_current_state(TASK_RUNNING);
1903}
1904
1905static void nvme_put_dq(struct nvme_delq_ctx *dq)
1906{
1907 atomic_dec(&dq->refcount);
1908 if (dq->waiter)
1909 wake_up_process(dq->waiter);
1910}
1911
1912static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
1913{
1914 atomic_inc(&dq->refcount);
1915 return dq;
1916}
1917
1918static void nvme_del_queue_end(struct nvme_queue *nvmeq)
1919{
1920 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
Keith Busch4d115422013-12-10 13:10:40 -07001921 nvme_put_dq(dq);
Keith Busch604e8c82015-11-20 08:38:13 -07001922
1923 spin_lock_irq(&nvmeq->q_lock);
1924 nvme_process_cq(nvmeq);
1925 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001926}
1927
1928static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
1929 kthread_work_func_t fn)
1930{
1931 struct nvme_command c;
1932
1933 memset(&c, 0, sizeof(c));
1934 c.delete_queue.opcode = opcode;
1935 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
1936
1937 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001938 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
1939 ADMIN_TIMEOUT);
Keith Busch4d115422013-12-10 13:10:40 -07001940}
1941
1942static void nvme_del_cq_work_handler(struct kthread_work *work)
1943{
1944 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1945 cmdinfo.work);
1946 nvme_del_queue_end(nvmeq);
1947}
1948
1949static int nvme_delete_cq(struct nvme_queue *nvmeq)
1950{
1951 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
1952 nvme_del_cq_work_handler);
1953}
1954
1955static void nvme_del_sq_work_handler(struct kthread_work *work)
1956{
1957 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1958 cmdinfo.work);
1959 int status = nvmeq->cmdinfo.status;
1960
1961 if (!status)
1962 status = nvme_delete_cq(nvmeq);
1963 if (status)
1964 nvme_del_queue_end(nvmeq);
1965}
1966
1967static int nvme_delete_sq(struct nvme_queue *nvmeq)
1968{
1969 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
1970 nvme_del_sq_work_handler);
1971}
1972
1973static void nvme_del_queue_start(struct kthread_work *work)
1974{
1975 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1976 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07001977 if (nvme_delete_sq(nvmeq))
1978 nvme_del_queue_end(nvmeq);
1979}
1980
1981static void nvme_disable_io_queues(struct nvme_dev *dev)
1982{
1983 int i;
1984 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
1985 struct nvme_delq_ctx dq;
1986 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001987 &worker, "nvme%d", dev->ctrl.instance);
Keith Busch4d115422013-12-10 13:10:40 -07001988
1989 if (IS_ERR(kworker_task)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001990 dev_err(dev->dev,
Keith Busch4d115422013-12-10 13:10:40 -07001991 "Failed to create queue del task\n");
1992 for (i = dev->queue_count - 1; i > 0; i--)
1993 nvme_disable_queue(dev, i);
1994 return;
1995 }
1996
1997 dq.waiter = NULL;
1998 atomic_set(&dq.refcount, 0);
1999 dq.worker = &worker;
2000 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002001 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002002
2003 if (nvme_suspend_queue(nvmeq))
2004 continue;
2005 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2006 nvmeq->cmdinfo.worker = dq.worker;
2007 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2008 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2009 }
2010 nvme_wait_dq(&dq, dev);
2011 kthread_stop(kworker_task);
2012}
2013
Christoph Hellwig73850142015-10-22 14:03:33 +02002014static int nvme_dev_list_add(struct nvme_dev *dev)
2015{
2016 bool start_thread = false;
2017
2018 spin_lock(&dev_list_lock);
2019 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2020 start_thread = true;
2021 nvme_thread = NULL;
2022 }
2023 list_add(&dev->node, &dev_list);
2024 spin_unlock(&dev_list_lock);
2025
2026 if (start_thread) {
2027 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2028 wake_up_all(&nvme_kthread_wait);
2029 } else
2030 wait_event_killable(nvme_kthread_wait, nvme_thread);
2031
2032 if (IS_ERR_OR_NULL(nvme_thread))
2033 return nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2034
2035 return 0;
2036}
2037
Dan McLeranb9afca32014-04-07 17:10:11 -06002038/*
2039* Remove the node from the device list and check
2040* for whether or not we need to stop the nvme_thread.
2041*/
2042static void nvme_dev_list_remove(struct nvme_dev *dev)
2043{
2044 struct task_struct *tmp = NULL;
2045
2046 spin_lock(&dev_list_lock);
2047 list_del_init(&dev->node);
2048 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2049 tmp = nvme_thread;
2050 nvme_thread = NULL;
2051 }
2052 spin_unlock(&dev_list_lock);
2053
2054 if (tmp)
2055 kthread_stop(tmp);
2056}
2057
Keith Buschc9d3bf82015-01-07 18:55:52 -07002058static void nvme_freeze_queues(struct nvme_dev *dev)
2059{
2060 struct nvme_ns *ns;
2061
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002062 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
Keith Buschc9d3bf82015-01-07 18:55:52 -07002063 blk_mq_freeze_queue_start(ns->queue);
2064
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002065 spin_lock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002066 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
Christoph Hellwigcddcd722015-05-07 09:38:14 +02002067 spin_unlock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002068
2069 blk_mq_cancel_requeue_work(ns->queue);
2070 blk_mq_stop_hw_queues(ns->queue);
2071 }
2072}
2073
2074static void nvme_unfreeze_queues(struct nvme_dev *dev)
2075{
2076 struct nvme_ns *ns;
2077
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002078 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
Keith Buschc9d3bf82015-01-07 18:55:52 -07002079 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2080 blk_mq_unfreeze_queue(ns->queue);
2081 blk_mq_start_stopped_hw_queues(ns->queue, true);
2082 blk_mq_kick_requeue_list(ns->queue);
2083 }
2084}
2085
Keith Buschf0b50732013-07-15 15:02:21 -06002086static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002087{
Keith Busch22404272013-07-15 15:02:20 -06002088 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06002089 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06002090
Dan McLeranb9afca32014-04-07 17:10:11 -06002091 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002092
Keith Busch77bf25e2015-11-26 12:21:29 +01002093 mutex_lock(&dev->shutdown_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002094 if (dev->bar) {
2095 nvme_freeze_queues(dev);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002096 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002097 }
Keith Busch7c1b2452014-06-25 11:18:12 -06002098 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07002099 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002100 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07002101 nvme_suspend_queue(nvmeq);
Keith Busch4d115422013-12-10 13:10:40 -07002102 }
2103 } else {
2104 nvme_disable_io_queues(dev);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002105 nvme_shutdown_ctrl(&dev->ctrl);
Keith Busch4d115422013-12-10 13:10:40 -07002106 nvme_disable_queue(dev, 0);
2107 }
Keith Buschf0b50732013-07-15 15:02:21 -06002108 nvme_dev_unmap(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002109
2110 for (i = dev->queue_count - 1; i >= 0; i--)
2111 nvme_clear_queue(dev->queues[i]);
Keith Busch77bf25e2015-11-26 12:21:29 +01002112 mutex_unlock(&dev->shutdown_lock);
Keith Buschf0b50732013-07-15 15:02:21 -06002113}
2114
Matthew Wilcox091b6092011-02-10 09:56:01 -05002115static int nvme_setup_prp_pools(struct nvme_dev *dev)
2116{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002117 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002118 PAGE_SIZE, PAGE_SIZE, 0);
2119 if (!dev->prp_page_pool)
2120 return -ENOMEM;
2121
Matthew Wilcox99802a72011-02-10 10:30:34 -05002122 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002123 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002124 256, 256, 0);
2125 if (!dev->prp_small_pool) {
2126 dma_pool_destroy(dev->prp_page_pool);
2127 return -ENOMEM;
2128 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002129 return 0;
2130}
2131
2132static void nvme_release_prp_pools(struct nvme_dev *dev)
2133{
2134 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002135 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002136}
2137
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002138static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
Keith Busch5e82e952013-02-19 10:17:58 -07002139{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002140 struct nvme_dev *dev = to_nvme_dev(ctrl);
Keith Busch9ac27092014-01-31 16:53:39 -07002141
Christoph Hellwige75ec752015-05-22 11:12:39 +02002142 put_device(dev->dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002143 if (dev->tagset.tags)
2144 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002145 if (dev->ctrl.admin_q)
2146 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002147 kfree(dev->queues);
2148 kfree(dev->entry);
2149 kfree(dev);
2150}
2151
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002152static void nvme_reset_work(struct work_struct *work)
Keith Buschf0b50732013-07-15 15:02:21 -06002153{
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002154 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002155 int result;
Keith Buschf0b50732013-07-15 15:02:21 -06002156
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002157 if (WARN_ON(test_bit(NVME_CTRL_RESETTING, &dev->flags)))
2158 goto out;
2159
2160 /*
2161 * If we're called to reset a live controller first shut it down before
2162 * moving on.
2163 */
2164 if (dev->bar)
2165 nvme_dev_shutdown(dev);
2166
2167 set_bit(NVME_CTRL_RESETTING, &dev->flags);
2168
Keith Buschf0b50732013-07-15 15:02:21 -06002169 result = nvme_dev_map(dev);
2170 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002171 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002172
2173 result = nvme_configure_admin_queue(dev);
2174 if (result)
2175 goto unmap;
2176
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002177 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002178 result = nvme_alloc_admin_tags(dev);
2179 if (result)
2180 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06002181
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002182 result = nvme_init_identify(&dev->ctrl);
2183 if (result)
2184 goto free_tags;
2185
Keith Buschf0b50732013-07-15 15:02:21 -06002186 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002187 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07002188 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06002189
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002190 dev->ctrl.event_limit = 1;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002191
Christoph Hellwig73850142015-10-22 14:03:33 +02002192 result = nvme_dev_list_add(dev);
2193 if (result)
2194 goto remove;
2195
Christoph Hellwig2659e572015-10-02 18:51:31 +02002196 /*
2197 * Keep the controller around but remove all namespaces if we don't have
2198 * any working I/O queue.
2199 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002200 if (dev->online_queues < 2) {
2201 dev_warn(dev->dev, "IO queues not created\n");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002202 nvme_remove_namespaces(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002203 } else {
2204 nvme_unfreeze_queues(dev);
2205 nvme_dev_add(dev);
2206 }
2207
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002208 clear_bit(NVME_CTRL_RESETTING, &dev->flags);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002209 return;
Keith Buschf0b50732013-07-15 15:02:21 -06002210
Christoph Hellwig73850142015-10-22 14:03:33 +02002211 remove:
2212 nvme_dev_list_remove(dev);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002213 free_tags:
2214 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002215 blk_put_queue(dev->ctrl.admin_q);
2216 dev->ctrl.admin_q = NULL;
Keith Busch4af0e212015-06-08 10:08:13 -06002217 dev->queues[0]->tags = NULL;
Keith Buschf0b50732013-07-15 15:02:21 -06002218 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002219 nvme_disable_queue(dev, 0);
Keith Buschf0b50732013-07-15 15:02:21 -06002220 unmap:
2221 nvme_dev_unmap(dev);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002222 out:
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002223 nvme_remove_dead_ctrl(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002224}
2225
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002226static void nvme_remove_dead_ctrl_work(struct work_struct *work)
Keith Busch9a6b9452013-12-10 13:10:36 -07002227{
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002228 struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002229 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002230
2231 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002232 pci_stop_and_remove_bus_device_locked(pdev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002233 nvme_put_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002234}
2235
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002236static void nvme_remove_dead_ctrl(struct nvme_dev *dev)
Keith Buschde3eff22015-06-18 13:36:39 -06002237{
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002238 dev_warn(dev->dev, "Removing after probe failure\n");
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002239 kref_get(&dev->ctrl.kref);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002240 if (!schedule_work(&dev->remove_work))
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002241 nvme_put_ctrl(&dev->ctrl);
Keith Buschde3eff22015-06-18 13:36:39 -06002242}
2243
Keith Busch4cc06522015-06-05 10:30:08 -06002244static int nvme_reset(struct nvme_dev *dev)
2245{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002246 if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q))
Keith Busch4cc06522015-06-05 10:30:08 -06002247 return -ENODEV;
2248
Christoph Hellwig846cc052015-11-26 12:10:29 +01002249 if (!queue_work(nvme_workq, &dev->reset_work))
2250 return -EBUSY;
Keith Busch4cc06522015-06-05 10:30:08 -06002251
Christoph Hellwig846cc052015-11-26 12:10:29 +01002252 flush_work(&dev->reset_work);
Christoph Hellwig846cc052015-11-26 12:10:29 +01002253 return 0;
Keith Busch4cc06522015-06-05 10:30:08 -06002254}
2255
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002256static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
2257{
2258 *val = readl(to_nvme_dev(ctrl)->bar + off);
2259 return 0;
2260}
2261
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002262static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2263{
2264 writel(val, to_nvme_dev(ctrl)->bar + off);
2265 return 0;
2266}
2267
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002268static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2269{
2270 *val = readq(to_nvme_dev(ctrl)->bar + off);
2271 return 0;
2272}
2273
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002274static bool nvme_pci_io_incapable(struct nvme_ctrl *ctrl)
2275{
2276 struct nvme_dev *dev = to_nvme_dev(ctrl);
2277
2278 return !dev->bar || dev->online_queues < 2;
2279}
2280
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002281static int nvme_pci_reset_ctrl(struct nvme_ctrl *ctrl)
2282{
2283 return nvme_reset(to_nvme_dev(ctrl));
2284}
2285
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002286static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
2287 .reg_read32 = nvme_pci_reg_read32,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002288 .reg_write32 = nvme_pci_reg_write32,
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002289 .reg_read64 = nvme_pci_reg_read64,
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002290 .io_incapable = nvme_pci_io_incapable,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002291 .reset_ctrl = nvme_pci_reset_ctrl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002292 .free_ctrl = nvme_pci_free_ctrl,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002293};
2294
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002295static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002296{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002297 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002298 struct nvme_dev *dev;
2299
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002300 node = dev_to_node(&pdev->dev);
2301 if (node == NUMA_NO_NODE)
2302 set_dev_node(&pdev->dev, 0);
2303
2304 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002305 if (!dev)
2306 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002307 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2308 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002309 if (!dev->entry)
2310 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002311 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2312 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002313 if (!dev->queues)
2314 goto free;
2315
Christoph Hellwige75ec752015-05-22 11:12:39 +02002316 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002317 pci_set_drvdata(pdev, dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002318
Keith Busche6e96d72015-03-23 09:32:37 -06002319 INIT_LIST_HEAD(&dev->node);
Keith Buscha5768aa2015-06-01 14:28:14 -06002320 INIT_WORK(&dev->scan_work, nvme_dev_scan);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002321 INIT_WORK(&dev->reset_work, nvme_reset_work);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002322 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
Keith Busch77bf25e2015-11-26 12:21:29 +01002323 mutex_init(&dev->shutdown_lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002324
2325 result = nvme_setup_prp_pools(dev);
2326 if (result)
2327 goto put_pci;
2328
2329 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
2330 id->driver_data);
2331 if (result)
2332 goto release_pools;
2333
Keith Busch92f7a162015-10-23 11:42:02 -06002334 queue_work(nvme_workq, &dev->reset_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002335 return 0;
2336
Keith Busch0877cb02013-07-15 15:02:19 -06002337 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002338 nvme_release_prp_pools(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002339 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002340 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002341 free:
2342 kfree(dev->queues);
2343 kfree(dev->entry);
2344 kfree(dev);
2345 return result;
2346}
2347
Keith Buschf0d54a52014-05-02 10:40:43 -06002348static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2349{
Keith Buscha6739472014-06-23 16:03:21 -06002350 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002351
Keith Buscha6739472014-06-23 16:03:21 -06002352 if (prepare)
2353 nvme_dev_shutdown(dev);
2354 else
Keith Busch92f7a162015-10-23 11:42:02 -06002355 queue_work(nvme_workq, &dev->reset_work);
Keith Buschf0d54a52014-05-02 10:40:43 -06002356}
2357
Keith Busch09ece142014-01-27 11:29:40 -05002358static void nvme_shutdown(struct pci_dev *pdev)
2359{
2360 struct nvme_dev *dev = pci_get_drvdata(pdev);
2361 nvme_dev_shutdown(dev);
2362}
2363
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002364static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002365{
2366 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002367
2368 spin_lock(&dev_list_lock);
2369 list_del_init(&dev->node);
2370 spin_unlock(&dev_list_lock);
2371
2372 pci_set_drvdata(pdev, NULL);
2373 flush_work(&dev->reset_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06002374 flush_work(&dev->scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002375 nvme_remove_namespaces(&dev->ctrl);
Keith Busch3399a3f2015-06-18 13:36:40 -06002376 nvme_dev_shutdown(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002377 nvme_dev_remove_admin(dev);
2378 nvme_free_queues(dev, 0);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002379 nvme_release_cmb(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002380 nvme_release_prp_pools(dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002381 nvme_put_ctrl(&dev->ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002382}
2383
2384/* These functions are yet to be implemented */
2385#define nvme_error_detected NULL
2386#define nvme_dump_registers NULL
2387#define nvme_link_reset NULL
2388#define nvme_slot_reset NULL
2389#define nvme_error_resume NULL
Keith Buschcd638942013-07-15 15:02:23 -06002390
Jingoo Han671a6012014-02-13 11:19:14 +09002391#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002392static int nvme_suspend(struct device *dev)
2393{
2394 struct pci_dev *pdev = to_pci_dev(dev);
2395 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2396
2397 nvme_dev_shutdown(ndev);
2398 return 0;
2399}
2400
2401static int nvme_resume(struct device *dev)
2402{
2403 struct pci_dev *pdev = to_pci_dev(dev);
2404 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002405
Keith Busch92f7a162015-10-23 11:42:02 -06002406 queue_work(nvme_workq, &ndev->reset_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002407 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002408}
Jingoo Han671a6012014-02-13 11:19:14 +09002409#endif
Keith Buschcd638942013-07-15 15:02:23 -06002410
2411static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002412
Stephen Hemminger1d352032012-09-07 09:33:17 -07002413static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002414 .error_detected = nvme_error_detected,
2415 .mmio_enabled = nvme_dump_registers,
2416 .link_reset = nvme_link_reset,
2417 .slot_reset = nvme_slot_reset,
2418 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002419 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002420};
2421
2422/* Move to pci_ids.h later */
2423#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2424
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002425static const struct pci_device_id nvme_id_table[] = {
Christoph Hellwig106198e2015-11-26 10:07:41 +01002426 { PCI_VDEVICE(INTEL, 0x0953),
2427 .driver_data = NVME_QUIRK_STRIPE_SIZE, },
Keith Busch540c8012015-10-22 15:45:06 -06002428 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2429 .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002430 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01002431 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002432 { 0, }
2433};
2434MODULE_DEVICE_TABLE(pci, nvme_id_table);
2435
2436static struct pci_driver nvme_driver = {
2437 .name = "nvme",
2438 .id_table = nvme_id_table,
2439 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002440 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002441 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002442 .driver = {
2443 .pm = &nvme_dev_pm_ops,
2444 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002445 .err_handler = &nvme_err_handler,
2446};
2447
2448static int __init nvme_init(void)
2449{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002450 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002451
Dan McLeranb9afca32014-04-07 17:10:11 -06002452 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002453
Keith Busch92f7a162015-10-23 11:42:02 -06002454 nvme_workq = alloc_workqueue("nvme", WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
Keith Busch9a6b9452013-12-10 13:10:36 -07002455 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002456 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002457
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002458 result = nvme_core_init();
Keith Busch5c42ea12012-07-25 16:05:18 -06002459 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002460 goto kill_workq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002461
Keith Buschf3db22f2014-06-11 11:51:35 -06002462 result = pci_register_driver(&nvme_driver);
2463 if (result)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002464 goto core_exit;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002465 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002466
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002467 core_exit:
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002468 nvme_core_exit();
Keith Busch9a6b9452013-12-10 13:10:36 -07002469 kill_workq:
2470 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002471 return result;
2472}
2473
2474static void __exit nvme_exit(void)
2475{
2476 pci_unregister_driver(&nvme_driver);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002477 nvme_core_exit();
Keith Busch9a6b9452013-12-10 13:10:36 -07002478 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06002479 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002480 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002481}
2482
2483MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2484MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002485MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002486module_init(nvme_init);
2487module_exit(nvme_exit);