blob: b82bbea909cd594c7bebbf21d46aa552e2b9a000 [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
Keith Buscha0a34082015-12-07 15:30:31 -070015#include <linux/aer.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040016#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050017#include <linux/blkdev.h>
Matias Bjørlinga4aea562014-11-04 08:20:14 -070018#include <linux/blk-mq.h>
Keith Busch42f61422014-03-24 10:46:25 -060019#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040020#include <linux/delay.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050021#include <linux/errno.h>
22#include <linux/fs.h>
23#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060024#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040025#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050026#include <linux/init.h>
27#include <linux/interrupt.h>
28#include <linux/io.h>
29#include <linux/kdev_t.h>
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050030#include <linux/kthread.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050031#include <linux/kernel.h>
32#include <linux/mm.h>
33#include <linux/module.h>
34#include <linux/moduleparam.h>
Keith Busch77bf25e2015-11-26 12:21:29 +010035#include <linux/mutex.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050036#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050037#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040038#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050039#include <linux/sched.h>
40#include <linux/slab.h>
Keith Busche1e5e562015-02-19 13:39:03 -070041#include <linux/t10-pi.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050042#include <linux/types.h>
Linus Torvalds9cf5c092015-11-06 14:22:15 -080043#include <linux/io-64-nonatomic-lo-hi.h>
Keith Busch1d277a62015-10-15 14:10:52 +020044#include <asm/unaligned.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090045
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020046#include "nvme.h"
47
Keith Busch9d43cf62014-05-13 11:42:02 -060048#define NVME_Q_DEPTH 1024
Jens Axboed31af0a2015-03-06 12:56:13 -070049#define NVME_AQ_DEPTH 256
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050050#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
51#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Christoph Hellwigadf68f22015-11-28 15:42:28 +010052
53/*
54 * We handle AEN commands ourselves and don't even let the
55 * block layer know about them.
56 */
57#define NVME_NR_AEN_COMMANDS 1
58#define NVME_AQ_BLKMQ_DEPTH (NVME_AQ_DEPTH - NVME_NR_AEN_COMMANDS)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050059
Christoph Hellwig21d34712015-11-26 09:08:36 +010060unsigned char admin_timeout = 60;
Keith Busch9d43cf62014-05-13 11:42:02 -060061module_param(admin_timeout, byte, 0644);
62MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050063
Matthew Wilcoxbd676082014-06-03 23:04:30 -040064unsigned char nvme_io_timeout = 30;
65module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
Keith Buschb3550842014-04-04 11:43:36 -060066MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050067
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +010068unsigned char shutdown_timeout = 5;
Dan McLeran2484f402014-07-01 09:33:32 -060069module_param(shutdown_timeout, byte, 0644);
70MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
71
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050072static int use_threaded_interrupts;
73module_param(use_threaded_interrupts, int, 0);
74
Jon Derrick8ffaadf2015-07-20 10:14:09 -060075static bool use_cmb_sqes = true;
76module_param(use_cmb_sqes, bool, 0644);
77MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
78
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050079static LIST_HEAD(dev_list);
80static struct task_struct *nvme_thread;
Keith Busch9a6b9452013-12-10 13:10:36 -070081static struct workqueue_struct *nvme_workq;
Dan McLeranb9afca32014-04-07 17:10:11 -060082static wait_queue_head_t nvme_kthread_wait;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050083
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010084struct nvme_dev;
85struct nvme_queue;
86
Keith Busch4cc06522015-06-05 10:30:08 -060087static int nvme_reset(struct nvme_dev *dev);
Jens Axboea0fa9642015-11-03 20:37:26 -070088static void nvme_process_cq(struct nvme_queue *nvmeq);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +010089static void nvme_remove_dead_ctrl(struct nvme_dev *dev);
Keith Busche1569a12015-11-26 12:11:07 +010090static void nvme_dev_shutdown(struct nvme_dev *dev);
Keith Buschd4b4ff82013-12-10 13:10:37 -070091
Keith Busch4d115422013-12-10 13:10:40 -070092struct async_cmd_info {
93 struct kthread_work work;
94 struct kthread_worker *worker;
Keith Busch4d115422013-12-10 13:10:40 -070095 int status;
96 void *ctx;
97};
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050098
99/*
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100100 * Represents an NVM Express device. Each nvme_dev is a PCI function.
101 */
102struct nvme_dev {
103 struct list_head node;
104 struct nvme_queue **queues;
105 struct blk_mq_tag_set tagset;
106 struct blk_mq_tag_set admin_tagset;
107 u32 __iomem *dbs;
108 struct device *dev;
109 struct dma_pool *prp_page_pool;
110 struct dma_pool *prp_small_pool;
111 unsigned queue_count;
112 unsigned online_queues;
113 unsigned max_qid;
114 int q_depth;
115 u32 db_stride;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100116 struct msix_entry *entry;
117 void __iomem *bar;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100118 struct work_struct reset_work;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100119 struct work_struct scan_work;
Christoph Hellwig5c8809e2015-11-26 12:35:49 +0100120 struct work_struct remove_work;
Keith Busch77bf25e2015-11-26 12:21:29 +0100121 struct mutex shutdown_lock;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100122 bool subsystem;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100123 void __iomem *cmb;
124 dma_addr_t cmb_dma_addr;
125 u64 cmb_size;
126 u32 cmbsz;
Christoph Hellwigfd634f412015-11-26 12:42:26 +0100127 unsigned long flags;
128#define NVME_CTRL_RESETTING 0
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100129
130 struct nvme_ctrl ctrl;
131};
132
133static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
134{
135 return container_of(ctrl, struct nvme_dev, ctrl);
136}
137
138/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500139 * An NVM Express queue. Each device has at least two (one for admin
140 * commands and one for I/O commands).
141 */
142struct nvme_queue {
143 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500144 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500145 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500146 spinlock_t q_lock;
147 struct nvme_command *sq_cmds;
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600148 struct nvme_command __iomem *sq_cmds_io;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500149 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600150 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500151 dma_addr_t sq_dma_addr;
152 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500153 u32 __iomem *q_db;
154 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700155 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500156 u16 sq_head;
157 u16 sq_tail;
158 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700159 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400160 u8 cq_phase;
161 u8 cqe_seen;
Keith Busch4d115422013-12-10 13:10:40 -0700162 struct async_cmd_info cmdinfo;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500163};
164
165/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200166 * The nvme_iod describes the data in an I/O, including the list of PRP
167 * entries. You can't see it in this data structure because C doesn't let
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100168 * me express that. Use nvme_init_iod to ensure there's enough space
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200169 * allocated to store the PRP list.
170 */
171struct nvme_iod {
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100172 struct nvme_queue *nvmeq;
173 int aborted;
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200174 int npages; /* In the PRP list. 0 means small pool in use */
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200175 int nents; /* Used in scatterlist */
176 int length; /* Of data, in bytes */
177 dma_addr_t first_dma;
Christoph Hellwigbf684052015-10-26 17:12:51 +0900178 struct scatterlist meta_sg; /* metadata requires single contiguous buffer */
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100179 struct scatterlist *sg;
180 struct scatterlist inline_sg[0];
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200181};
182
183/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500184 * Check we didin't inadvertently grow the command struct
185 */
186static inline void _nvme_check_size(void)
187{
188 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
189 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
190 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
191 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
192 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400193 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700194 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500195 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
196 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
197 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
198 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600199 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500200}
201
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700202/*
203 * Max size of iod being embedded in the request payload
204 */
205#define NVME_INT_PAGES 2
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100206#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700207
208/*
209 * Will slightly overestimate the number of pages needed. This is OK
210 * as it only leads to a small amount of wasted memory for the lifetime of
211 * the I/O.
212 */
213static int nvme_npages(unsigned size, struct nvme_dev *dev)
214{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100215 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
216 dev->ctrl.page_size);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700217 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
218}
219
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100220static unsigned int nvme_iod_alloc_size(struct nvme_dev *dev,
221 unsigned int size, unsigned int nseg)
222{
223 return sizeof(__le64 *) * nvme_npages(size, dev) +
224 sizeof(struct scatterlist) * nseg;
225}
226
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700227static unsigned int nvme_cmd_size(struct nvme_dev *dev)
228{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100229 return sizeof(struct nvme_iod) +
230 nvme_iod_alloc_size(dev, NVME_INT_BYTES(dev), NVME_INT_PAGES);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700231}
232
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700233static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
234 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500235{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700236 struct nvme_dev *dev = data;
237 struct nvme_queue *nvmeq = dev->queues[0];
238
Keith Busch42483222015-06-01 09:29:54 -0600239 WARN_ON(hctx_idx != 0);
240 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
241 WARN_ON(nvmeq->tags);
242
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700243 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600244 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700245 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500246}
247
Keith Busch4af0e212015-06-08 10:08:13 -0600248static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
249{
250 struct nvme_queue *nvmeq = hctx->driver_data;
251
252 nvmeq->tags = NULL;
253}
254
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700255static int nvme_admin_init_request(void *data, struct request *req,
256 unsigned int hctx_idx, unsigned int rq_idx,
257 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600258{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700259 struct nvme_dev *dev = data;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100260 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700261 struct nvme_queue *nvmeq = dev->queues[0];
262
263 BUG_ON(!nvmeq);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100264 iod->nvmeq = nvmeq;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700265 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600266}
267
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700268static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
269 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500270{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700271 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600272 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500273
Keith Busch42483222015-06-01 09:29:54 -0600274 if (!nvmeq->tags)
275 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500276
Keith Busch42483222015-06-01 09:29:54 -0600277 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700278 hctx->driver_data = nvmeq;
279 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500280}
281
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700282static int nvme_init_request(void *data, struct request *req,
283 unsigned int hctx_idx, unsigned int rq_idx,
284 unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500285{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700286 struct nvme_dev *dev = data;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100287 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700288 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
289
290 BUG_ON(!nvmeq);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100291 iod->nvmeq = nvmeq;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700292 return 0;
293}
294
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100295static void nvme_complete_async_event(struct nvme_dev *dev,
296 struct nvme_completion *cqe)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700297{
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100298 u16 status = le16_to_cpu(cqe->status) >> 1;
299 u32 result = le32_to_cpu(cqe->result);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700300
301 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100302 ++dev->ctrl.event_limit;
Keith Buscha5768aa2015-06-01 14:28:14 -0600303 if (status != NVME_SC_SUCCESS)
304 return;
305
306 switch (result & 0xff07) {
307 case NVME_AER_NOTICE_NS_CHANGED:
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100308 dev_info(dev->dev, "rescanning\n");
309 queue_work(nvme_workq, &dev->scan_work);
Keith Buscha5768aa2015-06-01 14:28:14 -0600310 default:
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100311 dev_warn(dev->dev, "async event result %08x\n", result);
Keith Buscha5768aa2015-06-01 14:28:14 -0600312 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700313}
314
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500315/**
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100316 * __nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500317 * @nvmeq: The queue to use
318 * @cmd: The command to send
319 *
320 * Safe to use from interrupt context
321 */
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530322static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
323 struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500324{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700325 u16 tail = nvmeq->sq_tail;
326
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600327 if (nvmeq->sq_cmds_io)
328 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
329 else
330 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
331
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500332 if (++tail == nvmeq->q_depth)
333 tail = 0;
Matthew Wilcox75478812011-02-16 09:59:59 -0500334 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500335 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500336}
337
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100338static __le64 **iod_list(struct request *req)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500339{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100340 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
341 return (__le64 **)(iod->sg + req->nr_phys_segments);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500342}
343
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100344static int nvme_init_iod(struct request *rq, struct nvme_dev *dev)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500345{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100346 struct nvme_iod *iod = blk_mq_rq_to_pdu(rq);
347 int nseg = rq->nr_phys_segments;
348 unsigned size;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500349
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100350 if (rq->cmd_flags & REQ_DISCARD)
351 size = sizeof(struct nvme_dsm_range);
352 else
353 size = blk_rq_bytes(rq);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500354
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100355 if (nseg > NVME_INT_PAGES || size > NVME_INT_BYTES(dev)) {
356 iod->sg = kmalloc(nvme_iod_alloc_size(dev, size, nseg), GFP_ATOMIC);
357 if (!iod->sg)
358 return BLK_MQ_RQ_QUEUE_BUSY;
359 } else {
360 iod->sg = iod->inline_sg;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700361 }
362
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100363 iod->aborted = 0;
364 iod->npages = -1;
365 iod->nents = 0;
366 iod->length = size;
367 return 0;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700368}
369
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100370static void nvme_free_iod(struct nvme_dev *dev, struct request *req)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500371{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100372 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100373 const int last_prp = dev->ctrl.page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500374 int i;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100375 __le64 **list = iod_list(req);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500376 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500377
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500378 if (iod->npages == 0)
379 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
380 for (i = 0; i < iod->npages; i++) {
381 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500382 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500383 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500384 prp_dma = next_prp_dma;
385 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700386
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100387 if (iod->sg != iod->inline_sg)
388 kfree(iod->sg);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500389}
390
Keith Busch52b68d72015-02-23 09:16:21 -0700391#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700392static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
393{
394 if (be32_to_cpu(pi->ref_tag) == v)
395 pi->ref_tag = cpu_to_be32(p);
396}
397
398static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
399{
400 if (be32_to_cpu(pi->ref_tag) == p)
401 pi->ref_tag = cpu_to_be32(v);
402}
403
404/**
405 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
406 *
407 * The virtual start sector is the one that was originally submitted by the
408 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
409 * start sector may be different. Remap protection information to match the
410 * physical LBA on writes, and back to the original seed on reads.
411 *
412 * Type 0 and 3 do not have a ref tag, so no remapping required.
413 */
414static void nvme_dif_remap(struct request *req,
415 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
416{
417 struct nvme_ns *ns = req->rq_disk->private_data;
418 struct bio_integrity_payload *bip;
419 struct t10_pi_tuple *pi;
420 void *p, *pmap;
421 u32 i, nlb, ts, phys, virt;
422
423 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
424 return;
425
426 bip = bio_integrity(req->bio);
427 if (!bip)
428 return;
429
430 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700431
432 p = pmap;
433 virt = bip_get_seed(bip);
434 phys = nvme_block_nr(ns, blk_rq_pos(req));
435 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
Dan Williamsac6fc482015-10-21 13:20:18 -0400436 ts = ns->disk->queue->integrity.tuple_size;
Keith Busche1e5e562015-02-19 13:39:03 -0700437
438 for (i = 0; i < nlb; i++, virt++, phys++) {
439 pi = (struct t10_pi_tuple *)p;
440 dif_swap(phys, virt, pi);
441 p += ts;
442 }
443 kunmap_atomic(pmap);
444}
Keith Busch52b68d72015-02-23 09:16:21 -0700445#else /* CONFIG_BLK_DEV_INTEGRITY */
446static void nvme_dif_remap(struct request *req,
447 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
448{
449}
450static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
451{
452}
453static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
454{
455}
Keith Busch52b68d72015-02-23 09:16:21 -0700456#endif
457
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100458static bool nvme_setup_prps(struct nvme_dev *dev, struct request *req,
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200459 int total_len)
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500460{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100461 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500462 struct dma_pool *pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500463 int length = total_len;
464 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500465 int dma_len = sg_dma_len(sg);
466 u64 dma_addr = sg_dma_address(sg);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100467 u32 page_size = dev->ctrl.page_size;
Murali Iyerf137e0f2015-03-26 11:07:51 -0500468 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500469 __le64 *prp_list;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100470 __le64 **list = iod_list(req);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500471 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500472 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500473
Keith Busch1d090622014-06-23 11:34:01 -0600474 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500475 if (length <= 0)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200476 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500477
Keith Busch1d090622014-06-23 11:34:01 -0600478 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500479 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600480 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500481 } else {
482 sg = sg_next(sg);
483 dma_addr = sg_dma_address(sg);
484 dma_len = sg_dma_len(sg);
485 }
486
Keith Busch1d090622014-06-23 11:34:01 -0600487 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600488 iod->first_dma = dma_addr;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200489 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500490 }
491
Keith Busch1d090622014-06-23 11:34:01 -0600492 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500493 if (nprps <= (256 / 8)) {
494 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500495 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500496 } else {
497 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500498 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500499 }
500
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200501 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400502 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600503 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500504 iod->npages = -1;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200505 return false;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400506 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500507 list[0] = prp_list;
508 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500509 i = 0;
510 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600511 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500512 __le64 *old_prp_list = prp_list;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200513 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500514 if (!prp_list)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200515 return false;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500516 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400517 prp_list[0] = old_prp_list[i - 1];
518 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
519 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500520 }
521 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600522 dma_len -= page_size;
523 dma_addr += page_size;
524 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500525 if (length <= 0)
526 break;
527 if (dma_len > 0)
528 continue;
529 BUG_ON(dma_len < 0);
530 sg = sg_next(sg);
531 dma_addr = sg_dma_address(sg);
532 dma_len = sg_dma_len(sg);
533 }
534
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200535 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500536}
537
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100538static int nvme_map_data(struct nvme_dev *dev, struct request *req,
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200539 struct nvme_command *cmnd)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200540{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100541 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200542 struct request_queue *q = req->q;
543 enum dma_data_direction dma_dir = rq_data_dir(req) ?
544 DMA_TO_DEVICE : DMA_FROM_DEVICE;
545 int ret = BLK_MQ_RQ_QUEUE_ERROR;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200546
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200547 sg_init_table(iod->sg, req->nr_phys_segments);
548 iod->nents = blk_rq_map_sg(q, req, iod->sg);
549 if (!iod->nents)
550 goto out;
551
552 ret = BLK_MQ_RQ_QUEUE_BUSY;
553 if (!dma_map_sg(dev->dev, iod->sg, iod->nents, dma_dir))
554 goto out;
555
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100556 if (!nvme_setup_prps(dev, req, blk_rq_bytes(req)))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200557 goto out_unmap;
558
559 ret = BLK_MQ_RQ_QUEUE_ERROR;
560 if (blk_integrity_rq(req)) {
561 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
562 goto out_unmap;
563
Christoph Hellwigbf684052015-10-26 17:12:51 +0900564 sg_init_table(&iod->meta_sg, 1);
565 if (blk_rq_map_integrity_sg(q, req->bio, &iod->meta_sg) != 1)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200566 goto out_unmap;
567
568 if (rq_data_dir(req))
569 nvme_dif_remap(req, nvme_dif_prep);
570
Christoph Hellwigbf684052015-10-26 17:12:51 +0900571 if (!dma_map_sg(dev->dev, &iod->meta_sg, 1, dma_dir))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200572 goto out_unmap;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200573 }
574
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200575 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
576 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
577 if (blk_integrity_rq(req))
Christoph Hellwigbf684052015-10-26 17:12:51 +0900578 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(&iod->meta_sg));
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200579 return BLK_MQ_RQ_QUEUE_OK;
580
581out_unmap:
582 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
583out:
584 return ret;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200585}
586
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100587static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100588{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100589 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100590 enum dma_data_direction dma_dir = rq_data_dir(req) ?
591 DMA_TO_DEVICE : DMA_FROM_DEVICE;
592
593 if (iod->nents) {
594 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
595 if (blk_integrity_rq(req)) {
596 if (!rq_data_dir(req))
597 nvme_dif_remap(req, nvme_dif_complete);
Christoph Hellwigbf684052015-10-26 17:12:51 +0900598 dma_unmap_sg(dev->dev, &iod->meta_sg, 1, dma_dir);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100599 }
600 }
601
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100602 nvme_free_iod(dev, req);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100603}
604
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700605/*
606 * We reuse the small pool to allocate the 16-byte range here as it is not
607 * worth having a special pool for these or additional cases to handle freeing
608 * the iod.
609 */
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200610static int nvme_setup_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100611 struct request *req, struct nvme_command *cmnd)
Keith Busch0e5e4f02012-11-09 16:33:05 -0700612{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100613 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200614 struct nvme_dsm_range *range;
615
616 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
617 &iod->first_dma);
618 if (!range)
619 return BLK_MQ_RQ_QUEUE_BUSY;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100620 iod_list(req)[0] = (__le64 *)range;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200621 iod->npages = 0;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700622
Keith Busch0e5e4f02012-11-09 16:33:05 -0700623 range->cattr = cpu_to_le32(0);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700624 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
625 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
Keith Busch0e5e4f02012-11-09 16:33:05 -0700626
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200627 memset(cmnd, 0, sizeof(*cmnd));
628 cmnd->dsm.opcode = nvme_cmd_dsm;
629 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
630 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
631 cmnd->dsm.nr = 0;
632 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
633 return BLK_MQ_RQ_QUEUE_OK;
Keith Busch0e5e4f02012-11-09 16:33:05 -0700634}
635
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200636/*
637 * NOTE: ns is NULL when called on the admin queue.
638 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700639static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
640 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600641{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700642 struct nvme_ns *ns = hctx->queue->queuedata;
643 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200644 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700645 struct request *req = bd->rq;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200646 struct nvme_command cmnd;
647 int ret = BLK_MQ_RQ_QUEUE_OK;
Keith Buschedd10d32014-04-03 16:45:23 -0600648
Keith Busche1e5e562015-02-19 13:39:03 -0700649 /*
650 * If formated with metadata, require the block layer provide a buffer
651 * unless this namespace is formated such that the metadata can be
652 * stripped/generated by the controller with PRACT=1.
653 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200654 if (ns && ns->ms && !blk_integrity_rq(req)) {
Keith Busch71feb362015-06-19 11:07:30 -0600655 if (!(ns->pi_type && ns->ms == 8) &&
656 req->cmd_type != REQ_TYPE_DRV_PRIV) {
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100657 blk_mq_end_request(req, -EFAULT);
Keith Busche1e5e562015-02-19 13:39:03 -0700658 return BLK_MQ_RQ_QUEUE_OK;
659 }
660 }
661
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100662 ret = nvme_init_iod(req, dev);
663 if (ret)
664 return ret;
Keith Buschedd10d32014-04-03 16:45:23 -0600665
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700666 if (req->cmd_flags & REQ_DISCARD) {
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100667 ret = nvme_setup_discard(nvmeq, ns, req, &cmnd);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200668 } else {
669 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
670 memcpy(&cmnd, req->cmd, sizeof(cmnd));
671 else if (req->cmd_flags & REQ_FLUSH)
672 nvme_setup_flush(ns, &cmnd);
673 else
674 nvme_setup_rw(ns, req, &cmnd);
Keith Buschedd10d32014-04-03 16:45:23 -0600675
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200676 if (req->nr_phys_segments)
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100677 ret = nvme_map_data(dev, req, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700678 }
679
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200680 if (ret)
681 goto out;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700682
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200683 cmnd.common.command_id = req->tag;
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100684 blk_mq_start_request(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200685
686 spin_lock_irq(&nvmeq->q_lock);
687 __nvme_submit_cmd(nvmeq, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700688 nvme_process_cq(nvmeq);
689 spin_unlock_irq(&nvmeq->q_lock);
690 return BLK_MQ_RQ_QUEUE_OK;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200691out:
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100692 nvme_free_iod(dev, req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200693 return ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500694}
695
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100696static void nvme_complete_rq(struct request *req)
697{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100698 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
699 struct nvme_dev *dev = iod->nvmeq->dev;
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100700 int error = 0;
701
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100702 nvme_unmap_data(dev, req);
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100703
704 if (unlikely(req->errors)) {
705 if (nvme_req_needs_retry(req, req->errors)) {
706 nvme_requeue_req(req);
707 return;
708 }
709
710 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
711 error = req->errors;
712 else
713 error = nvme_error_status(req->errors);
714 }
715
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100716 if (unlikely(iod->aborted)) {
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100717 dev_warn(dev->dev,
718 "completing aborted command with status: %04x\n",
719 req->errors);
720 }
721
722 blk_mq_end_request(req, error);
723}
724
Jens Axboea0fa9642015-11-03 20:37:26 -0700725static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500726{
Matthew Wilcox82123462011-01-20 13:24:06 -0500727 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500728
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500729 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500730 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500731
732 for (;;) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500733 struct nvme_completion cqe = nvmeq->cqes[head];
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100734 u16 status = le16_to_cpu(cqe.status);
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100735 struct request *req;
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100736
737 if ((status & 1) != phase)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500738 break;
739 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
740 if (++head == nvmeq->q_depth) {
741 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500742 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500743 }
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100744
Jens Axboea0fa9642015-11-03 20:37:26 -0700745 if (tag && *tag == cqe.command_id)
746 *tag = -1;
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100747
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100748 if (unlikely(cqe.command_id >= nvmeq->q_depth)) {
749 dev_warn(nvmeq->q_dmadev,
750 "invalid id %d completed on queue %d\n",
751 cqe.command_id, le16_to_cpu(cqe.sq_id));
752 continue;
753 }
754
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100755 /*
756 * AEN requests are special as they don't time out and can
757 * survive any kind of queue freeze and often don't respond to
758 * aborts. We don't even bother to allocate a struct request
759 * for them but rather special case them here.
760 */
761 if (unlikely(nvmeq->qid == 0 &&
762 cqe.command_id >= NVME_AQ_BLKMQ_DEPTH)) {
763 nvme_complete_async_event(nvmeq->dev, &cqe);
764 continue;
765 }
766
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100767 req = blk_mq_tag_to_rq(*nvmeq->tags, cqe.command_id);
768 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
769 u32 result = le32_to_cpu(cqe.result);
770 req->special = (void *)(uintptr_t)result;
771 }
772 blk_mq_complete_request(req, status >> 1);
773
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500774 }
775
776 /* If the controller ignores the cq head doorbell and continuously
777 * writes to the queue, it is theoretically possible to wrap around
778 * the queue twice and mistakenly return IRQ_NONE. Linux only
779 * requires that 0.1% of your interrupts are handled, so this isn't
780 * a big problem.
781 */
Matthew Wilcox82123462011-01-20 13:24:06 -0500782 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Jens Axboea0fa9642015-11-03 20:37:26 -0700783 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500784
Keith Busch604e8c82015-11-20 08:38:13 -0700785 if (likely(nvmeq->cq_vector >= 0))
786 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500787 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500788 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500789
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400790 nvmeq->cqe_seen = 1;
Jens Axboea0fa9642015-11-03 20:37:26 -0700791}
792
793static void nvme_process_cq(struct nvme_queue *nvmeq)
794{
795 __nvme_process_cq(nvmeq, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500796}
797
798static irqreturn_t nvme_irq(int irq, void *data)
799{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500800 irqreturn_t result;
801 struct nvme_queue *nvmeq = data;
802 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400803 nvme_process_cq(nvmeq);
804 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
805 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500806 spin_unlock(&nvmeq->q_lock);
807 return result;
808}
809
810static irqreturn_t nvme_irq_check(int irq, void *data)
811{
812 struct nvme_queue *nvmeq = data;
813 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
814 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
815 return IRQ_NONE;
816 return IRQ_WAKE_THREAD;
817}
818
Jens Axboea0fa9642015-11-03 20:37:26 -0700819static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
820{
821 struct nvme_queue *nvmeq = hctx->driver_data;
822
823 if ((le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
824 nvmeq->cq_phase) {
825 spin_lock_irq(&nvmeq->q_lock);
826 __nvme_process_cq(nvmeq, &tag);
827 spin_unlock_irq(&nvmeq->q_lock);
828
829 if (tag == -1)
830 return 1;
831 }
832
833 return 0;
834}
835
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100836static void nvme_submit_async_event(struct nvme_dev *dev)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700837{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700838 struct nvme_command c;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700839
840 memset(&c, 0, sizeof(c));
841 c.common.opcode = nvme_admin_async_event;
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100842 c.common.command_id = NVME_AQ_BLKMQ_DEPTH + --dev->ctrl.event_limit;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700843
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100844 __nvme_submit_cmd(dev->queues[0], &c);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700845}
846
Christoph Hellwigd8f32162015-11-16 10:28:47 +0100847static void async_cmd_info_endio(struct request *req, int error)
Keith Busch4d115422013-12-10 13:10:40 -0700848{
Christoph Hellwigd8f32162015-11-16 10:28:47 +0100849 struct async_cmd_info *cmdinfo = req->end_io_data;
Keith Busch4d115422013-12-10 13:10:40 -0700850
Christoph Hellwigd8f32162015-11-16 10:28:47 +0100851 cmdinfo->status = req->errors;
852 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
853 blk_mq_free_request(req);
Keith Busch4d115422013-12-10 13:10:40 -0700854}
855
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500856static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
857{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500858 struct nvme_command c;
859
860 memset(&c, 0, sizeof(c));
861 c.delete_queue.opcode = opcode;
862 c.delete_queue.qid = cpu_to_le16(id);
863
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100864 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500865}
866
867static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
868 struct nvme_queue *nvmeq)
869{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500870 struct nvme_command c;
871 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
872
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200873 /*
874 * Note: we (ab)use the fact the the prp fields survive if no data
875 * is attached to the request.
876 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500877 memset(&c, 0, sizeof(c));
878 c.create_cq.opcode = nvme_admin_create_cq;
879 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
880 c.create_cq.cqid = cpu_to_le16(qid);
881 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
882 c.create_cq.cq_flags = cpu_to_le16(flags);
883 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
884
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100885 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500886}
887
888static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
889 struct nvme_queue *nvmeq)
890{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500891 struct nvme_command c;
892 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
893
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200894 /*
895 * Note: we (ab)use the fact the the prp fields survive if no data
896 * is attached to the request.
897 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500898 memset(&c, 0, sizeof(c));
899 c.create_sq.opcode = nvme_admin_create_sq;
900 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
901 c.create_sq.sqid = cpu_to_le16(qid);
902 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
903 c.create_sq.sq_flags = cpu_to_le16(flags);
904 c.create_sq.cqid = cpu_to_le16(qid);
905
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100906 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500907}
908
909static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
910{
911 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
912}
913
914static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
915{
916 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
917}
918
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100919static void abort_endio(struct request *req, int error)
920{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100921 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
922 struct nvme_queue *nvmeq = iod->nvmeq;
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100923 u32 result = (u32)(uintptr_t)req->special;
924 u16 status = req->errors;
925
926 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
927 atomic_inc(&nvmeq->dev->ctrl.abort_limit);
928
929 blk_mq_free_request(req);
930}
931
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +0200932static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
Keith Buschc30341d2013-12-10 13:10:38 -0700933{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100934 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
935 struct nvme_queue *nvmeq = iod->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -0700936 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700937 struct request *abort_req;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700938 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -0700939
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +0200940 /*
Christoph Hellwigfd634f412015-11-26 12:42:26 +0100941 * Shutdown immediately if controller times out while starting. The
942 * reset work will see the pci device disabled when it gets the forced
943 * cancellation error. All outstanding requests are completed on
944 * shutdown, so we return BLK_EH_HANDLED.
945 */
946 if (test_bit(NVME_CTRL_RESETTING, &dev->flags)) {
947 dev_warn(dev->dev,
948 "I/O %d QID %d timeout, disable controller\n",
949 req->tag, nvmeq->qid);
950 nvme_dev_shutdown(dev);
951 req->errors = NVME_SC_CANCELLED;
952 return BLK_EH_HANDLED;
953 }
954
955 /*
956 * Shutdown the controller immediately and schedule a reset if the
957 * command was already aborted once before and still hasn't been
958 * returned to the driver, or if this is the admin queue.
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +0200959 */
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100960 if (!nvmeq->qid || iod->aborted) {
Keith Busche1569a12015-11-26 12:11:07 +0100961 dev_warn(dev->dev,
962 "I/O %d QID %d timeout, reset controller\n",
963 req->tag, nvmeq->qid);
964 nvme_dev_shutdown(dev);
965 queue_work(nvme_workq, &dev->reset_work);
966
967 /*
968 * Mark the request as handled, since the inline shutdown
969 * forces all outstanding requests to complete.
970 */
971 req->errors = NVME_SC_CANCELLED;
972 return BLK_EH_HANDLED;
Keith Buschc30341d2013-12-10 13:10:38 -0700973 }
974
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100975 iod->aborted = 1;
Keith Buschc30341d2013-12-10 13:10:38 -0700976
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100977 if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) {
978 atomic_inc(&dev->ctrl.abort_limit);
979 return BLK_EH_RESET_TIMER;
980 }
981
982 memset(&cmd, 0, sizeof(cmd));
983 cmd.abort.opcode = nvme_admin_abort_cmd;
984 cmd.abort.cid = req->tag;
985 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
986
987 dev_warn(nvmeq->q_dmadev, "I/O %d QID %d timeout, aborting\n",
988 req->tag, nvmeq->qid);
989
990 abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100991 BLK_MQ_REQ_NOWAIT);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +0100992 if (IS_ERR(abort_req)) {
993 atomic_inc(&dev->ctrl.abort_limit);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +0200994 return BLK_EH_RESET_TIMER;
Christoph Hellwig6bf25d12015-11-20 09:36:44 +0100995 }
Keith Buschc30341d2013-12-10 13:10:38 -0700996
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100997 abort_req->timeout = ADMIN_TIMEOUT;
998 abort_req->end_io_data = NULL;
999 blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001000
1001 /*
1002 * The aborted req will be completed on receiving the abort req.
1003 * We enable the timer again. If hit twice, it'll cause a device reset,
1004 * as the device then is in a faulty state.
1005 */
1006 return BLK_EH_RESET_TIMER;
Keith Buschc30341d2013-12-10 13:10:38 -07001007}
1008
Keith Busch42483222015-06-01 09:29:54 -06001009static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001010{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001011 struct nvme_queue *nvmeq = data;
Christoph Hellwigaae239e2015-11-26 12:59:50 +01001012 int status;
Keith Buschcef6a942015-01-07 18:55:51 -07001013
1014 if (!blk_mq_request_started(req))
1015 return;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001016
Christoph Hellwigaae239e2015-11-26 12:59:50 +01001017 dev_warn(nvmeq->q_dmadev,
1018 "Cancelling I/O %d QID %d\n", req->tag, nvmeq->qid);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001019
Christoph Hellwigaae239e2015-11-26 12:59:50 +01001020 status = NVME_SC_CANCELLED;
Keith Buschcef6a942015-01-07 18:55:51 -07001021 if (blk_queue_dying(req->q))
Christoph Hellwigaae239e2015-11-26 12:59:50 +01001022 status |= NVME_SC_DNR;
1023 blk_mq_complete_request(req, status);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001024}
1025
Keith Buschf435c282014-07-07 09:14:42 -06001026static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001027{
1028 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1029 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001030 if (nvmeq->sq_cmds)
1031 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001032 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1033 kfree(nvmeq);
1034}
1035
Keith Buscha1a5ef92013-12-16 13:50:00 -05001036static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001037{
1038 int i;
1039
Keith Buscha1a5ef92013-12-16 13:50:00 -05001040 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001041 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001042 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001043 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001044 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001045 }
Keith Busch22404272013-07-15 15:02:20 -06001046}
1047
Keith Busch4d115422013-12-10 13:10:40 -07001048/**
1049 * nvme_suspend_queue - put queue into suspended state
1050 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001051 */
1052static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001053{
Keith Busch2b25d982014-12-22 12:59:04 -07001054 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001055
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001056 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001057 if (nvmeq->cq_vector == -1) {
1058 spin_unlock_irq(&nvmeq->q_lock);
1059 return 1;
1060 }
1061 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
Keith Busch42f61422014-03-24 10:46:25 -06001062 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001063 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001064 spin_unlock_irq(&nvmeq->q_lock);
1065
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001066 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
1067 blk_mq_freeze_queue_start(nvmeq->dev->ctrl.admin_q);
Keith Busch6df3dbc2015-03-26 13:49:33 -06001068
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001069 irq_set_affinity_hint(vector, NULL);
1070 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001071
Keith Busch4d115422013-12-10 13:10:40 -07001072 return 0;
1073}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001074
Keith Busch4d115422013-12-10 13:10:40 -07001075static void nvme_clear_queue(struct nvme_queue *nvmeq)
1076{
Keith Busch22404272013-07-15 15:02:20 -06001077 spin_lock_irq(&nvmeq->q_lock);
Keith Busch42483222015-06-01 09:29:54 -06001078 if (nvmeq->tags && *nvmeq->tags)
1079 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
Keith Busch22404272013-07-15 15:02:20 -06001080 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001081}
1082
Keith Busch4d115422013-12-10 13:10:40 -07001083static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1084{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001085 struct nvme_queue *nvmeq = dev->queues[qid];
Keith Busch4d115422013-12-10 13:10:40 -07001086
1087 if (!nvmeq)
1088 return;
1089 if (nvme_suspend_queue(nvmeq))
1090 return;
1091
Keith Busch0e53d182013-12-10 13:10:39 -07001092 /* Don't tell the adapter to delete the admin queue.
1093 * Don't tell a removed adapter to delete IO queues. */
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001094 if (qid && readl(dev->bar + NVME_REG_CSTS) != -1) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001095 adapter_delete_sq(dev, qid);
1096 adapter_delete_cq(dev, qid);
1097 }
Keith Busch07836e62015-02-19 10:34:48 -07001098
1099 spin_lock_irq(&nvmeq->q_lock);
1100 nvme_process_cq(nvmeq);
1101 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001102}
1103
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001104static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1105 int entry_size)
1106{
1107 int q_depth = dev->q_depth;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001108 unsigned q_size_aligned = roundup(q_depth * entry_size,
1109 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001110
1111 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001112 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001113 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
Jon Derrickc45f5c92015-07-21 15:08:13 -06001114 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001115
1116 /*
1117 * Ensure the reduced q_depth is above some threshold where it
1118 * would be better to map queues in system memory with the
1119 * original depth
1120 */
1121 if (q_depth < 64)
1122 return -ENOMEM;
1123 }
1124
1125 return q_depth;
1126}
1127
1128static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1129 int qid, int depth)
1130{
1131 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001132 unsigned offset = (qid - 1) * roundup(SQ_SIZE(depth),
1133 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001134 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1135 nvmeq->sq_cmds_io = dev->cmb + offset;
1136 } else {
1137 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1138 &nvmeq->sq_dma_addr, GFP_KERNEL);
1139 if (!nvmeq->sq_cmds)
1140 return -ENOMEM;
1141 }
1142
1143 return 0;
1144}
1145
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001146static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Keith Busch2b25d982014-12-22 12:59:04 -07001147 int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001148{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001149 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001150 if (!nvmeq)
1151 return NULL;
1152
Christoph Hellwige75ec752015-05-22 11:12:39 +02001153 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001154 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001155 if (!nvmeq->cqes)
1156 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001157
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001158 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001159 goto free_cqdma;
1160
Christoph Hellwige75ec752015-05-22 11:12:39 +02001161 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001162 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001163 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001164 dev->ctrl.instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001165 spin_lock_init(&nvmeq->q_lock);
1166 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001167 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001168 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001169 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001170 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001171 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001172 dev->queues[qid] = nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001173
Jon Derrick36a7e992015-05-27 12:26:23 -06001174 /* make sure queue descriptor is set before queue count, for kthread */
1175 mb();
1176 dev->queue_count++;
1177
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001178 return nvmeq;
1179
1180 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001181 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001182 nvmeq->cq_dma_addr);
1183 free_nvmeq:
1184 kfree(nvmeq);
1185 return NULL;
1186}
1187
Matthew Wilcox30010822011-01-20 09:10:15 -05001188static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1189 const char *name)
1190{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001191 if (use_threaded_interrupts)
1192 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001193 nvme_irq_check, nvme_irq, IRQF_SHARED,
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001194 name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001195 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
Michael Opdenacker481e5ba2013-10-12 06:23:29 +02001196 IRQF_SHARED, name, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001197}
1198
Keith Busch22404272013-07-15 15:02:20 -06001199static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001200{
Keith Busch22404272013-07-15 15:02:20 -06001201 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001202
Keith Busch7be50e92014-09-10 15:48:47 -06001203 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001204 nvmeq->sq_tail = 0;
1205 nvmeq->cq_head = 0;
1206 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001207 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001208 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Keith Busch42f61422014-03-24 10:46:25 -06001209 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001210 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001211}
1212
1213static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1214{
1215 struct nvme_dev *dev = nvmeq->dev;
1216 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001217
Keith Busch2b25d982014-12-22 12:59:04 -07001218 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001219 result = adapter_alloc_cq(dev, qid, nvmeq);
1220 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001221 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001222
1223 result = adapter_alloc_sq(dev, qid, nvmeq);
1224 if (result < 0)
1225 goto release_cq;
1226
Matthew Wilcox3193f072014-01-27 15:57:22 -05001227 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001228 if (result < 0)
1229 goto release_sq;
1230
Keith Busch22404272013-07-15 15:02:20 -06001231 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001232 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001233
1234 release_sq:
1235 adapter_delete_sq(dev, qid);
1236 release_cq:
1237 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001238 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001239}
1240
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001241static struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001242 .queue_rq = nvme_queue_rq,
Christoph Hellwigeee417b2015-11-26 13:03:13 +01001243 .complete = nvme_complete_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001244 .map_queue = blk_mq_map_queue,
1245 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001246 .exit_hctx = nvme_admin_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001247 .init_request = nvme_admin_init_request,
1248 .timeout = nvme_timeout,
1249};
1250
1251static struct blk_mq_ops nvme_mq_ops = {
1252 .queue_rq = nvme_queue_rq,
Christoph Hellwigeee417b2015-11-26 13:03:13 +01001253 .complete = nvme_complete_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001254 .map_queue = blk_mq_map_queue,
1255 .init_hctx = nvme_init_hctx,
1256 .init_request = nvme_init_request,
1257 .timeout = nvme_timeout,
Jens Axboea0fa9642015-11-03 20:37:26 -07001258 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001259};
1260
Keith Buschea191d22015-01-07 18:55:49 -07001261static void nvme_dev_remove_admin(struct nvme_dev *dev)
1262{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001263 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
1264 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001265 blk_mq_free_tag_set(&dev->admin_tagset);
1266 }
1267}
1268
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001269static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1270{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001271 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001272 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1273 dev->admin_tagset.nr_hw_queues = 1;
Christoph Hellwigadf68f22015-11-28 15:42:28 +01001274 dev->admin_tagset.queue_depth = NVME_AQ_BLKMQ_DEPTH;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001275 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001276 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001277 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001278 dev->admin_tagset.driver_data = dev;
1279
1280 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1281 return -ENOMEM;
1282
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001283 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1284 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001285 blk_mq_free_tag_set(&dev->admin_tagset);
1286 return -ENOMEM;
1287 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001288 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001289 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001290 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001291 return -ENODEV;
1292 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001293 } else
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001294 blk_mq_unfreeze_queue(dev->ctrl.admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001295
1296 return 0;
1297}
1298
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001299static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001300{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001301 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001302 u32 aqa;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001303 u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001304 struct nvme_queue *nvmeq;
1305
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001306 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1) ?
Keith Buschdfbac8c2015-08-10 15:20:40 -06001307 NVME_CAP_NSSRC(cap) : 0;
1308
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001309 if (dev->subsystem &&
1310 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1311 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001312
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001313 result = nvme_disable_ctrl(&dev->ctrl, cap);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001314 if (result < 0)
1315 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001316
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001317 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001318 if (!nvmeq) {
Keith Busch2b25d982014-12-22 12:59:04 -07001319 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Keith Buschcd638942013-07-15 15:02:23 -06001320 if (!nvmeq)
1321 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001322 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001323
1324 aqa = nvmeq->q_depth - 1;
1325 aqa |= aqa << 16;
1326
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001327 writel(aqa, dev->bar + NVME_REG_AQA);
1328 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1329 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001330
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001331 result = nvme_enable_ctrl(&dev->ctrl, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001332 if (result)
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001333 goto free_nvmeq;
1334
Keith Busch2b25d982014-12-22 12:59:04 -07001335 nvmeq->cq_vector = 0;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001336 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001337 if (result) {
1338 nvmeq->cq_vector = -1;
Keith Busch0fb59cb2015-01-07 18:55:50 -07001339 goto free_nvmeq;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001340 }
Keith Busch025c5572013-05-01 13:07:51 -06001341
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001342 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001343
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001344 free_nvmeq:
1345 nvme_free_queues(dev, 0);
1346 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001347}
1348
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001349static int nvme_kthread(void *data)
1350{
Keith Buschd4b4ff82013-12-10 13:10:37 -07001351 struct nvme_dev *dev, *next;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001352
1353 while (!kthread_should_stop()) {
Arjan van de Ven564a2322013-05-01 16:38:23 -04001354 set_current_state(TASK_INTERRUPTIBLE);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001355 spin_lock(&dev_list_lock);
Keith Buschd4b4ff82013-12-10 13:10:37 -07001356 list_for_each_entry_safe(dev, next, &dev_list, node) {
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001357 int i;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001358 u32 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001359
Christoph Hellwig846cc052015-11-26 12:10:29 +01001360 /*
1361 * Skip controllers currently under reset.
1362 */
1363 if (work_pending(&dev->reset_work) || work_busy(&dev->reset_work))
1364 continue;
1365
Keith Buschdfbac8c2015-08-10 15:20:40 -06001366 if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
1367 csts & NVME_CSTS_CFS) {
Christoph Hellwig846cc052015-11-26 12:10:29 +01001368 if (queue_work(nvme_workq, &dev->reset_work)) {
Christoph Hellwig906678922015-10-02 18:49:23 +02001369 dev_warn(dev->dev,
1370 "Failed status: %x, reset controller\n",
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001371 readl(dev->bar + NVME_REG_CSTS));
Christoph Hellwig906678922015-10-02 18:49:23 +02001372 }
Keith Buschd4b4ff82013-12-10 13:10:37 -07001373 continue;
1374 }
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001375 for (i = 0; i < dev->queue_count; i++) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001376 struct nvme_queue *nvmeq = dev->queues[i];
Matthew Wilcox740216f2011-02-15 16:28:20 -05001377 if (!nvmeq)
1378 continue;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001379 spin_lock_irq(&nvmeq->q_lock);
Matthew Wilcoxbc57a0f2013-06-24 11:56:42 -04001380 nvme_process_cq(nvmeq);
Keith Busch6fccf932014-06-18 13:58:57 -06001381
Christoph Hellwigadf68f22015-11-28 15:42:28 +01001382 while (i == 0 && dev->ctrl.event_limit > 0)
1383 nvme_submit_async_event(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001384 spin_unlock_irq(&nvmeq->q_lock);
1385 }
1386 }
1387 spin_unlock(&dev_list_lock);
Arjan van de Venacb7aa02013-02-04 14:44:33 -08001388 schedule_timeout(round_jiffies_relative(HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001389 }
1390 return 0;
1391}
1392
Christoph Hellwig749941f2015-11-26 11:46:39 +01001393static int nvme_create_io_queues(struct nvme_dev *dev)
Keith Busch42f61422014-03-24 10:46:25 -06001394{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001395 unsigned i;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001396 int ret = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001397
Christoph Hellwig749941f2015-11-26 11:46:39 +01001398 for (i = dev->queue_count; i <= dev->max_qid; i++) {
1399 if (!nvme_alloc_queue(dev, i, dev->q_depth)) {
1400 ret = -ENOMEM;
Keith Busch42f61422014-03-24 10:46:25 -06001401 break;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001402 }
1403 }
Keith Busch42f61422014-03-24 10:46:25 -06001404
Christoph Hellwig749941f2015-11-26 11:46:39 +01001405 for (i = dev->online_queues; i <= dev->queue_count - 1; i++) {
1406 ret = nvme_create_queue(dev->queues[i], i);
1407 if (ret) {
Christoph Hellwig2659e572015-10-02 18:51:31 +02001408 nvme_free_queues(dev, i);
Keith Busch42f61422014-03-24 10:46:25 -06001409 break;
Christoph Hellwig2659e572015-10-02 18:51:31 +02001410 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001411 }
1412
1413 /*
1414 * Ignore failing Create SQ/CQ commands, we can continue with less
1415 * than the desired aount of queues, and even a controller without
1416 * I/O queues an still be used to issue admin commands. This might
1417 * be useful to upgrade a buggy firmware for example.
1418 */
1419 return ret >= 0 ? 0 : ret;
Keith Busch42f61422014-03-24 10:46:25 -06001420}
1421
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001422static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1423{
1424 u64 szu, size, offset;
1425 u32 cmbloc;
1426 resource_size_t bar_size;
1427 struct pci_dev *pdev = to_pci_dev(dev->dev);
1428 void __iomem *cmb;
1429 dma_addr_t dma_addr;
1430
1431 if (!use_cmb_sqes)
1432 return NULL;
1433
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001434 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001435 if (!(NVME_CMB_SZ(dev->cmbsz)))
1436 return NULL;
1437
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001438 cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001439
1440 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1441 size = szu * NVME_CMB_SZ(dev->cmbsz);
1442 offset = szu * NVME_CMB_OFST(cmbloc);
1443 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
1444
1445 if (offset > bar_size)
1446 return NULL;
1447
1448 /*
1449 * Controllers may support a CMB size larger than their BAR,
1450 * for example, due to being behind a bridge. Reduce the CMB to
1451 * the reported size of the BAR
1452 */
1453 if (size > bar_size - offset)
1454 size = bar_size - offset;
1455
1456 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
1457 cmb = ioremap_wc(dma_addr, size);
1458 if (!cmb)
1459 return NULL;
1460
1461 dev->cmb_dma_addr = dma_addr;
1462 dev->cmb_size = size;
1463 return cmb;
1464}
1465
1466static inline void nvme_release_cmb(struct nvme_dev *dev)
1467{
1468 if (dev->cmb) {
1469 iounmap(dev->cmb);
1470 dev->cmb = NULL;
1471 }
1472}
1473
Keith Busch9d713c22013-07-15 15:02:24 -06001474static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1475{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001476 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001477}
1478
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001479static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001480{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001481 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02001482 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch42f61422014-03-24 10:46:25 -06001483 int result, i, vecs, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001484
Keith Busch42f61422014-03-24 10:46:25 -06001485 nr_io_queues = num_possible_cpus();
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001486 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
1487 if (result < 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001488 return result;
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001489
1490 /*
1491 * Degraded controllers might return an error when setting the queue
1492 * count. We still want to be able to bring them online and offer
1493 * access to the admin queue, as that might be only way to fix them up.
1494 */
1495 if (result > 0) {
1496 dev_err(dev->dev, "Could not set queue count (%d)\n", result);
1497 nr_io_queues = 0;
1498 result = 0;
1499 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001500
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001501 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1502 result = nvme_cmb_qdepth(dev, nr_io_queues,
1503 sizeof(struct nvme_command));
1504 if (result > 0)
1505 dev->q_depth = result;
1506 else
1507 nvme_release_cmb(dev);
1508 }
1509
Keith Busch9d713c22013-07-15 15:02:24 -06001510 size = db_bar_size(dev, nr_io_queues);
1511 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001512 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001513 do {
1514 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1515 if (dev->bar)
1516 break;
1517 if (!--nr_io_queues)
1518 return -ENOMEM;
1519 size = db_bar_size(dev, nr_io_queues);
1520 } while (1);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001521 dev->dbs = dev->bar + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07001522 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001523 }
1524
Keith Busch9d713c22013-07-15 15:02:24 -06001525 /* Deregister the admin queue's interrupt */
Matthew Wilcox3193f072014-01-27 15:57:22 -05001526 free_irq(dev->entry[0].vector, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001527
Jens Axboee32efbf2014-11-14 09:49:26 -07001528 /*
1529 * If we enable msix early due to not intx, disable it again before
1530 * setting up the full range we need.
1531 */
1532 if (!pdev->irq)
1533 pci_disable_msix(pdev);
1534
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001535 for (i = 0; i < nr_io_queues; i++)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001536 dev->entry[i].entry = i;
Alexander Gordeevbe577fa2014-03-04 16:22:00 +01001537 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
1538 if (vecs < 0) {
1539 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
1540 if (vecs < 0) {
1541 vecs = 1;
1542 } else {
1543 for (i = 0; i < vecs; i++)
1544 dev->entry[i].vector = i + pdev->irq;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001545 }
1546 }
1547
Matthew Wilcox063a8092013-06-20 10:53:48 -04001548 /*
1549 * Should investigate if there's a performance win from allocating
1550 * more queues than interrupt vectors; it might allow the submission
1551 * path to scale better, even if the receive path is limited by the
1552 * number of interrupts.
1553 */
1554 nr_io_queues = vecs;
Keith Busch42f61422014-03-24 10:46:25 -06001555 dev->max_qid = nr_io_queues;
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001556
Matthew Wilcox3193f072014-01-27 15:57:22 -05001557 result = queue_request_irq(dev, adminq, adminq->irqname);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001558 if (result) {
1559 adminq->cq_vector = -1;
Keith Busch22404272013-07-15 15:02:20 -06001560 goto free_queues;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001561 }
Matthew Wilcox1b234842011-01-20 13:01:49 -05001562
Keith Buschcd638942013-07-15 15:02:23 -06001563 /* Free previously allocated queues that are no longer usable */
Keith Busch42f61422014-03-24 10:46:25 -06001564 nvme_free_queues(dev, nr_io_queues + 1);
Christoph Hellwig749941f2015-11-26 11:46:39 +01001565 return nvme_create_io_queues(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001566
Keith Busch22404272013-07-15 15:02:20 -06001567 free_queues:
Keith Buscha1a5ef92013-12-16 13:50:00 -05001568 nvme_free_queues(dev, 1);
Keith Busch22404272013-07-15 15:02:20 -06001569 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001570}
1571
Keith Buschbda4e0f2015-09-03 08:18:17 -06001572static void nvme_set_irq_hints(struct nvme_dev *dev)
1573{
1574 struct nvme_queue *nvmeq;
1575 int i;
1576
1577 for (i = 0; i < dev->online_queues; i++) {
1578 nvmeq = dev->queues[i];
1579
1580 if (!nvmeq->tags || !(*nvmeq->tags))
1581 continue;
1582
1583 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
1584 blk_mq_tags_cpumask(*nvmeq->tags));
1585 }
1586}
1587
Keith Buscha5768aa2015-06-01 14:28:14 -06001588static void nvme_dev_scan(struct work_struct *work)
1589{
1590 struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06001591
1592 if (!dev->tagset.tags)
1593 return;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001594 nvme_scan_namespaces(&dev->ctrl);
Keith Buschbda4e0f2015-09-03 08:18:17 -06001595 nvme_set_irq_hints(dev);
Keith Buscha5768aa2015-06-01 14:28:14 -06001596}
1597
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001598/*
1599 * Return: error value if an error occurred setting up the queues or calling
1600 * Identify Device. 0 if these succeeded, even if adding some of the
1601 * namespaces failed. At the moment, these failures are silent. TBD which
1602 * failures should be reported.
1603 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001604static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001605{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001606 if (!dev->ctrl.tagset) {
Keith Buschffe77042015-06-08 10:08:15 -06001607 dev->tagset.ops = &nvme_mq_ops;
1608 dev->tagset.nr_hw_queues = dev->online_queues - 1;
1609 dev->tagset.timeout = NVME_IO_TIMEOUT;
1610 dev->tagset.numa_node = dev_to_node(dev->dev);
1611 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001612 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06001613 dev->tagset.cmd_size = nvme_cmd_size(dev);
1614 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
1615 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001616
Keith Buschffe77042015-06-08 10:08:15 -06001617 if (blk_mq_alloc_tag_set(&dev->tagset))
1618 return 0;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001619 dev->ctrl.tagset = &dev->tagset;
Keith Buschffe77042015-06-08 10:08:15 -06001620 }
Keith Busch92f7a162015-10-23 11:42:02 -06001621 queue_work(nvme_workq, &dev->scan_work);
Keith Busche1e5e562015-02-19 13:39:03 -07001622 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001623}
1624
Keith Busch0877cb02013-07-15 15:02:19 -06001625static int nvme_dev_map(struct nvme_dev *dev)
1626{
Keith Busch42f61422014-03-24 10:46:25 -06001627 u64 cap;
Keith Busch0877cb02013-07-15 15:02:19 -06001628 int bars, result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001629 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001630
1631 if (pci_enable_device_mem(pdev))
1632 return result;
1633
1634 dev->entry[0].vector = pdev->irq;
1635 pci_set_master(pdev);
1636 bars = pci_select_bars(pdev, IORESOURCE_MEM);
Jens Axboebe7837e2014-11-14 09:50:19 -07001637 if (!bars)
1638 goto disable_pci;
1639
Keith Busch0877cb02013-07-15 15:02:19 -06001640 if (pci_request_selected_regions(pdev, bars, "nvme"))
1641 goto disable_pci;
1642
Christoph Hellwige75ec752015-05-22 11:12:39 +02001643 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
1644 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01001645 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06001646
Keith Busch0877cb02013-07-15 15:02:19 -06001647 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1648 if (!dev->bar)
1649 goto disable;
Jens Axboee32efbf2014-11-14 09:49:26 -07001650
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001651 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07001652 result = -ENODEV;
1653 goto unmap;
1654 }
Jens Axboee32efbf2014-11-14 09:49:26 -07001655
1656 /*
1657 * Some devices don't advertse INTx interrupts, pre-enable a single
1658 * MSIX vec for setup. We'll adjust this later.
1659 */
1660 if (!pdev->irq) {
1661 result = pci_enable_msix(pdev, dev->entry, 1);
1662 if (result < 0)
1663 goto unmap;
1664 }
1665
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001666 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
1667
Keith Busch42f61422014-03-24 10:46:25 -06001668 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
1669 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001670 dev->dbs = dev->bar + 4096;
1671 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2))
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001672 dev->cmb = nvme_map_cmb(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001673
Keith Buscha0a34082015-12-07 15:30:31 -07001674 pci_enable_pcie_error_reporting(pdev);
1675 pci_save_state(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001676 return 0;
1677
Keith Busch0e53d182013-12-10 13:10:39 -07001678 unmap:
1679 iounmap(dev->bar);
1680 dev->bar = NULL;
Keith Busch0877cb02013-07-15 15:02:19 -06001681 disable:
1682 pci_release_regions(pdev);
1683 disable_pci:
1684 pci_disable_device(pdev);
1685 return result;
1686}
1687
1688static void nvme_dev_unmap(struct nvme_dev *dev)
1689{
Christoph Hellwige75ec752015-05-22 11:12:39 +02001690 struct pci_dev *pdev = to_pci_dev(dev->dev);
1691
1692 if (pdev->msi_enabled)
1693 pci_disable_msi(pdev);
1694 else if (pdev->msix_enabled)
1695 pci_disable_msix(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001696
1697 if (dev->bar) {
1698 iounmap(dev->bar);
1699 dev->bar = NULL;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001700 pci_release_regions(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001701 }
1702
Keith Buscha0a34082015-12-07 15:30:31 -07001703 if (pci_is_enabled(pdev)) {
1704 pci_disable_pcie_error_reporting(pdev);
Christoph Hellwige75ec752015-05-22 11:12:39 +02001705 pci_disable_device(pdev);
Keith Buscha0a34082015-12-07 15:30:31 -07001706 }
Keith Busch0877cb02013-07-15 15:02:19 -06001707}
1708
Keith Busch4d115422013-12-10 13:10:40 -07001709struct nvme_delq_ctx {
1710 struct task_struct *waiter;
1711 struct kthread_worker *worker;
1712 atomic_t refcount;
1713};
1714
1715static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
1716{
1717 dq->waiter = current;
1718 mb();
1719
1720 for (;;) {
1721 set_current_state(TASK_KILLABLE);
1722 if (!atomic_read(&dq->refcount))
1723 break;
1724 if (!schedule_timeout(ADMIN_TIMEOUT) ||
1725 fatal_signal_pending(current)) {
Keith Busch0fb59cb2015-01-07 18:55:50 -07001726 /*
1727 * Disable the controller first since we can't trust it
1728 * at this point, but leave the admin queue enabled
1729 * until all queue deletion requests are flushed.
1730 * FIXME: This may take a while if there are more h/w
1731 * queues than admin tags.
1732 */
Keith Busch4d115422013-12-10 13:10:40 -07001733 set_current_state(TASK_RUNNING);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001734 nvme_disable_ctrl(&dev->ctrl,
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001735 lo_hi_readq(dev->bar + NVME_REG_CAP));
Keith Busch0fb59cb2015-01-07 18:55:50 -07001736 nvme_clear_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07001737 flush_kthread_worker(dq->worker);
Keith Busch0fb59cb2015-01-07 18:55:50 -07001738 nvme_disable_queue(dev, 0);
Keith Busch4d115422013-12-10 13:10:40 -07001739 return;
1740 }
1741 }
1742 set_current_state(TASK_RUNNING);
1743}
1744
1745static void nvme_put_dq(struct nvme_delq_ctx *dq)
1746{
1747 atomic_dec(&dq->refcount);
1748 if (dq->waiter)
1749 wake_up_process(dq->waiter);
1750}
1751
1752static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
1753{
1754 atomic_inc(&dq->refcount);
1755 return dq;
1756}
1757
1758static void nvme_del_queue_end(struct nvme_queue *nvmeq)
1759{
1760 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
Keith Busch4d115422013-12-10 13:10:40 -07001761 nvme_put_dq(dq);
Keith Busch604e8c82015-11-20 08:38:13 -07001762
1763 spin_lock_irq(&nvmeq->q_lock);
1764 nvme_process_cq(nvmeq);
1765 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -07001766}
1767
1768static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
1769 kthread_work_func_t fn)
1770{
Christoph Hellwigd8f32162015-11-16 10:28:47 +01001771 struct request *req;
Keith Busch4d115422013-12-10 13:10:40 -07001772 struct nvme_command c;
1773
1774 memset(&c, 0, sizeof(c));
1775 c.delete_queue.opcode = opcode;
1776 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
1777
1778 init_kthread_work(&nvmeq->cmdinfo.work, fn);
Christoph Hellwigd8f32162015-11-16 10:28:47 +01001779
1780 req = nvme_alloc_request(nvmeq->dev->ctrl.admin_q, &c, 0);
1781 if (IS_ERR(req))
1782 return PTR_ERR(req);
1783
1784 req->timeout = ADMIN_TIMEOUT;
1785 req->end_io_data = &nvmeq->cmdinfo;
1786 blk_execute_rq_nowait(req->q, NULL, req, 0, async_cmd_info_endio);
1787 return 0;
Keith Busch4d115422013-12-10 13:10:40 -07001788}
1789
1790static void nvme_del_cq_work_handler(struct kthread_work *work)
1791{
1792 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1793 cmdinfo.work);
1794 nvme_del_queue_end(nvmeq);
1795}
1796
1797static int nvme_delete_cq(struct nvme_queue *nvmeq)
1798{
1799 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
1800 nvme_del_cq_work_handler);
1801}
1802
1803static void nvme_del_sq_work_handler(struct kthread_work *work)
1804{
1805 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1806 cmdinfo.work);
1807 int status = nvmeq->cmdinfo.status;
1808
1809 if (!status)
1810 status = nvme_delete_cq(nvmeq);
1811 if (status)
1812 nvme_del_queue_end(nvmeq);
1813}
1814
1815static int nvme_delete_sq(struct nvme_queue *nvmeq)
1816{
1817 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
1818 nvme_del_sq_work_handler);
1819}
1820
1821static void nvme_del_queue_start(struct kthread_work *work)
1822{
1823 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1824 cmdinfo.work);
Keith Busch4d115422013-12-10 13:10:40 -07001825 if (nvme_delete_sq(nvmeq))
1826 nvme_del_queue_end(nvmeq);
1827}
1828
1829static void nvme_disable_io_queues(struct nvme_dev *dev)
1830{
1831 int i;
1832 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
1833 struct nvme_delq_ctx dq;
1834 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001835 &worker, "nvme%d", dev->ctrl.instance);
Keith Busch4d115422013-12-10 13:10:40 -07001836
1837 if (IS_ERR(kworker_task)) {
Christoph Hellwige75ec752015-05-22 11:12:39 +02001838 dev_err(dev->dev,
Keith Busch4d115422013-12-10 13:10:40 -07001839 "Failed to create queue del task\n");
1840 for (i = dev->queue_count - 1; i > 0; i--)
1841 nvme_disable_queue(dev, i);
1842 return;
1843 }
1844
1845 dq.waiter = NULL;
1846 atomic_set(&dq.refcount, 0);
1847 dq.worker = &worker;
1848 for (i = dev->queue_count - 1; i > 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001849 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07001850
1851 if (nvme_suspend_queue(nvmeq))
1852 continue;
1853 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
1854 nvmeq->cmdinfo.worker = dq.worker;
1855 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
1856 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
1857 }
1858 nvme_wait_dq(&dq, dev);
1859 kthread_stop(kworker_task);
1860}
1861
Christoph Hellwig73850142015-10-22 14:03:33 +02001862static int nvme_dev_list_add(struct nvme_dev *dev)
1863{
1864 bool start_thread = false;
1865
1866 spin_lock(&dev_list_lock);
1867 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
1868 start_thread = true;
1869 nvme_thread = NULL;
1870 }
1871 list_add(&dev->node, &dev_list);
1872 spin_unlock(&dev_list_lock);
1873
1874 if (start_thread) {
1875 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
1876 wake_up_all(&nvme_kthread_wait);
1877 } else
1878 wait_event_killable(nvme_kthread_wait, nvme_thread);
1879
1880 if (IS_ERR_OR_NULL(nvme_thread))
1881 return nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
1882
1883 return 0;
1884}
1885
Dan McLeranb9afca32014-04-07 17:10:11 -06001886/*
1887* Remove the node from the device list and check
1888* for whether or not we need to stop the nvme_thread.
1889*/
1890static void nvme_dev_list_remove(struct nvme_dev *dev)
1891{
1892 struct task_struct *tmp = NULL;
1893
1894 spin_lock(&dev_list_lock);
1895 list_del_init(&dev->node);
1896 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
1897 tmp = nvme_thread;
1898 nvme_thread = NULL;
1899 }
1900 spin_unlock(&dev_list_lock);
1901
1902 if (tmp)
1903 kthread_stop(tmp);
1904}
1905
Keith Buschc9d3bf82015-01-07 18:55:52 -07001906static void nvme_freeze_queues(struct nvme_dev *dev)
1907{
1908 struct nvme_ns *ns;
1909
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001910 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
Keith Buschc9d3bf82015-01-07 18:55:52 -07001911 blk_mq_freeze_queue_start(ns->queue);
1912
Christoph Hellwigcddcd722015-05-07 09:38:14 +02001913 spin_lock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07001914 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
Christoph Hellwigcddcd722015-05-07 09:38:14 +02001915 spin_unlock_irq(ns->queue->queue_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07001916
1917 blk_mq_cancel_requeue_work(ns->queue);
1918 blk_mq_stop_hw_queues(ns->queue);
1919 }
1920}
1921
1922static void nvme_unfreeze_queues(struct nvme_dev *dev)
1923{
1924 struct nvme_ns *ns;
1925
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001926 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
Keith Buschc9d3bf82015-01-07 18:55:52 -07001927 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
1928 blk_mq_unfreeze_queue(ns->queue);
1929 blk_mq_start_stopped_hw_queues(ns->queue, true);
1930 blk_mq_kick_requeue_list(ns->queue);
1931 }
1932}
1933
Keith Buschf0b50732013-07-15 15:02:21 -06001934static void nvme_dev_shutdown(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001935{
Keith Busch22404272013-07-15 15:02:20 -06001936 int i;
Keith Busch7c1b2452014-06-25 11:18:12 -06001937 u32 csts = -1;
Keith Busch22404272013-07-15 15:02:20 -06001938
Dan McLeranb9afca32014-04-07 17:10:11 -06001939 nvme_dev_list_remove(dev);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001940
Keith Busch77bf25e2015-11-26 12:21:29 +01001941 mutex_lock(&dev->shutdown_lock);
Keith Buschc9d3bf82015-01-07 18:55:52 -07001942 if (dev->bar) {
1943 nvme_freeze_queues(dev);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001944 csts = readl(dev->bar + NVME_REG_CSTS);
Keith Buschc9d3bf82015-01-07 18:55:52 -07001945 }
Keith Busch7c1b2452014-06-25 11:18:12 -06001946 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
Keith Busch4d115422013-12-10 13:10:40 -07001947 for (i = dev->queue_count - 1; i >= 0; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001948 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch4d115422013-12-10 13:10:40 -07001949 nvme_suspend_queue(nvmeq);
Keith Busch4d115422013-12-10 13:10:40 -07001950 }
1951 } else {
1952 nvme_disable_io_queues(dev);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001953 nvme_shutdown_ctrl(&dev->ctrl);
Keith Busch4d115422013-12-10 13:10:40 -07001954 nvme_disable_queue(dev, 0);
1955 }
Keith Buschf0b50732013-07-15 15:02:21 -06001956 nvme_dev_unmap(dev);
Keith Busch07836e62015-02-19 10:34:48 -07001957
1958 for (i = dev->queue_count - 1; i >= 0; i--)
1959 nvme_clear_queue(dev->queues[i]);
Keith Busch77bf25e2015-11-26 12:21:29 +01001960 mutex_unlock(&dev->shutdown_lock);
Keith Buschf0b50732013-07-15 15:02:21 -06001961}
1962
Matthew Wilcox091b6092011-02-10 09:56:01 -05001963static int nvme_setup_prp_pools(struct nvme_dev *dev)
1964{
Christoph Hellwige75ec752015-05-22 11:12:39 +02001965 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05001966 PAGE_SIZE, PAGE_SIZE, 0);
1967 if (!dev->prp_page_pool)
1968 return -ENOMEM;
1969
Matthew Wilcox99802a72011-02-10 10:30:34 -05001970 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02001971 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05001972 256, 256, 0);
1973 if (!dev->prp_small_pool) {
1974 dma_pool_destroy(dev->prp_page_pool);
1975 return -ENOMEM;
1976 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05001977 return 0;
1978}
1979
1980static void nvme_release_prp_pools(struct nvme_dev *dev)
1981{
1982 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05001983 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05001984}
1985
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001986static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
Keith Busch5e82e952013-02-19 10:17:58 -07001987{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001988 struct nvme_dev *dev = to_nvme_dev(ctrl);
Keith Busch9ac27092014-01-31 16:53:39 -07001989
Christoph Hellwige75ec752015-05-22 11:12:39 +02001990 put_device(dev->dev);
Keith Busch4af0e212015-06-08 10:08:13 -06001991 if (dev->tagset.tags)
1992 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001993 if (dev->ctrl.admin_q)
1994 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07001995 kfree(dev->queues);
1996 kfree(dev->entry);
1997 kfree(dev);
1998}
1999
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002000static void nvme_reset_work(struct work_struct *work)
Keith Buschf0b50732013-07-15 15:02:21 -06002001{
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002002 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002003 int result;
Keith Buschf0b50732013-07-15 15:02:21 -06002004
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002005 if (WARN_ON(test_bit(NVME_CTRL_RESETTING, &dev->flags)))
2006 goto out;
2007
2008 /*
2009 * If we're called to reset a live controller first shut it down before
2010 * moving on.
2011 */
2012 if (dev->bar)
2013 nvme_dev_shutdown(dev);
2014
2015 set_bit(NVME_CTRL_RESETTING, &dev->flags);
2016
Keith Buschf0b50732013-07-15 15:02:21 -06002017 result = nvme_dev_map(dev);
2018 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002019 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002020
2021 result = nvme_configure_admin_queue(dev);
2022 if (result)
2023 goto unmap;
2024
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002025 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002026 result = nvme_alloc_admin_tags(dev);
2027 if (result)
2028 goto disable;
Dan McLeranb9afca32014-04-07 17:10:11 -06002029
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002030 result = nvme_init_identify(&dev->ctrl);
2031 if (result)
2032 goto free_tags;
2033
Keith Buschf0b50732013-07-15 15:02:21 -06002034 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002035 if (result)
Keith Busch0fb59cb2015-01-07 18:55:50 -07002036 goto free_tags;
Keith Buschf0b50732013-07-15 15:02:21 -06002037
Christoph Hellwigadf68f22015-11-28 15:42:28 +01002038 dev->ctrl.event_limit = NVME_NR_AEN_COMMANDS;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002039
Christoph Hellwig73850142015-10-22 14:03:33 +02002040 result = nvme_dev_list_add(dev);
2041 if (result)
2042 goto remove;
2043
Christoph Hellwig2659e572015-10-02 18:51:31 +02002044 /*
2045 * Keep the controller around but remove all namespaces if we don't have
2046 * any working I/O queue.
2047 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002048 if (dev->online_queues < 2) {
2049 dev_warn(dev->dev, "IO queues not created\n");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002050 nvme_remove_namespaces(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002051 } else {
2052 nvme_unfreeze_queues(dev);
2053 nvme_dev_add(dev);
2054 }
2055
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002056 clear_bit(NVME_CTRL_RESETTING, &dev->flags);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002057 return;
Keith Buschf0b50732013-07-15 15:02:21 -06002058
Christoph Hellwig73850142015-10-22 14:03:33 +02002059 remove:
2060 nvme_dev_list_remove(dev);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002061 free_tags:
2062 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002063 blk_put_queue(dev->ctrl.admin_q);
2064 dev->ctrl.admin_q = NULL;
Keith Busch4af0e212015-06-08 10:08:13 -06002065 dev->queues[0]->tags = NULL;
Keith Buschf0b50732013-07-15 15:02:21 -06002066 disable:
Keith Buscha1a5ef92013-12-16 13:50:00 -05002067 nvme_disable_queue(dev, 0);
Keith Buschf0b50732013-07-15 15:02:21 -06002068 unmap:
2069 nvme_dev_unmap(dev);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002070 out:
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002071 nvme_remove_dead_ctrl(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002072}
2073
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002074static void nvme_remove_dead_ctrl_work(struct work_struct *work)
Keith Busch9a6b9452013-12-10 13:10:36 -07002075{
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002076 struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002077 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002078
2079 if (pci_get_drvdata(pdev))
Keith Buschc81f4972014-06-23 15:24:53 -06002080 pci_stop_and_remove_bus_device_locked(pdev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002081 nvme_put_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002082}
2083
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002084static void nvme_remove_dead_ctrl(struct nvme_dev *dev)
Keith Buschde3eff22015-06-18 13:36:39 -06002085{
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002086 dev_warn(dev->dev, "Removing after probe failure\n");
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002087 kref_get(&dev->ctrl.kref);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002088 if (!schedule_work(&dev->remove_work))
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002089 nvme_put_ctrl(&dev->ctrl);
Keith Buschde3eff22015-06-18 13:36:39 -06002090}
2091
Keith Busch4cc06522015-06-05 10:30:08 -06002092static int nvme_reset(struct nvme_dev *dev)
2093{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002094 if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q))
Keith Busch4cc06522015-06-05 10:30:08 -06002095 return -ENODEV;
2096
Christoph Hellwig846cc052015-11-26 12:10:29 +01002097 if (!queue_work(nvme_workq, &dev->reset_work))
2098 return -EBUSY;
Keith Busch4cc06522015-06-05 10:30:08 -06002099
Christoph Hellwig846cc052015-11-26 12:10:29 +01002100 flush_work(&dev->reset_work);
Christoph Hellwig846cc052015-11-26 12:10:29 +01002101 return 0;
Keith Busch4cc06522015-06-05 10:30:08 -06002102}
2103
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002104static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
2105{
2106 *val = readl(to_nvme_dev(ctrl)->bar + off);
2107 return 0;
2108}
2109
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002110static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2111{
2112 writel(val, to_nvme_dev(ctrl)->bar + off);
2113 return 0;
2114}
2115
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002116static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2117{
2118 *val = readq(to_nvme_dev(ctrl)->bar + off);
2119 return 0;
2120}
2121
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002122static bool nvme_pci_io_incapable(struct nvme_ctrl *ctrl)
2123{
2124 struct nvme_dev *dev = to_nvme_dev(ctrl);
2125
2126 return !dev->bar || dev->online_queues < 2;
2127}
2128
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002129static int nvme_pci_reset_ctrl(struct nvme_ctrl *ctrl)
2130{
2131 return nvme_reset(to_nvme_dev(ctrl));
2132}
2133
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002134static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
2135 .reg_read32 = nvme_pci_reg_read32,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002136 .reg_write32 = nvme_pci_reg_write32,
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002137 .reg_read64 = nvme_pci_reg_read64,
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002138 .io_incapable = nvme_pci_io_incapable,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002139 .reset_ctrl = nvme_pci_reset_ctrl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002140 .free_ctrl = nvme_pci_free_ctrl,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002141};
2142
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002143static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002144{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002145 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002146 struct nvme_dev *dev;
2147
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002148 node = dev_to_node(&pdev->dev);
2149 if (node == NUMA_NO_NODE)
2150 set_dev_node(&pdev->dev, 0);
2151
2152 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002153 if (!dev)
2154 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002155 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2156 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002157 if (!dev->entry)
2158 goto free;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002159 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2160 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002161 if (!dev->queues)
2162 goto free;
2163
Christoph Hellwige75ec752015-05-22 11:12:39 +02002164 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002165 pci_set_drvdata(pdev, dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002166
Keith Busche6e96d72015-03-23 09:32:37 -06002167 INIT_LIST_HEAD(&dev->node);
Keith Buscha5768aa2015-06-01 14:28:14 -06002168 INIT_WORK(&dev->scan_work, nvme_dev_scan);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002169 INIT_WORK(&dev->reset_work, nvme_reset_work);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002170 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
Keith Busch77bf25e2015-11-26 12:21:29 +01002171 mutex_init(&dev->shutdown_lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002172
2173 result = nvme_setup_prp_pools(dev);
2174 if (result)
2175 goto put_pci;
2176
2177 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
2178 id->driver_data);
2179 if (result)
2180 goto release_pools;
2181
Keith Busch92f7a162015-10-23 11:42:02 -06002182 queue_work(nvme_workq, &dev->reset_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002183 return 0;
2184
Keith Busch0877cb02013-07-15 15:02:19 -06002185 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002186 nvme_release_prp_pools(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002187 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002188 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002189 free:
2190 kfree(dev->queues);
2191 kfree(dev->entry);
2192 kfree(dev);
2193 return result;
2194}
2195
Keith Buschf0d54a52014-05-02 10:40:43 -06002196static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2197{
Keith Buscha6739472014-06-23 16:03:21 -06002198 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002199
Keith Buscha6739472014-06-23 16:03:21 -06002200 if (prepare)
2201 nvme_dev_shutdown(dev);
2202 else
Keith Busch92f7a162015-10-23 11:42:02 -06002203 queue_work(nvme_workq, &dev->reset_work);
Keith Buschf0d54a52014-05-02 10:40:43 -06002204}
2205
Keith Busch09ece142014-01-27 11:29:40 -05002206static void nvme_shutdown(struct pci_dev *pdev)
2207{
2208 struct nvme_dev *dev = pci_get_drvdata(pdev);
2209 nvme_dev_shutdown(dev);
2210}
2211
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002212static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002213{
2214 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002215
2216 spin_lock(&dev_list_lock);
2217 list_del_init(&dev->node);
2218 spin_unlock(&dev_list_lock);
2219
2220 pci_set_drvdata(pdev, NULL);
2221 flush_work(&dev->reset_work);
Keith Buscha5768aa2015-06-01 14:28:14 -06002222 flush_work(&dev->scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002223 nvme_remove_namespaces(&dev->ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002224 nvme_uninit_ctrl(&dev->ctrl);
Keith Busch3399a3f2015-06-18 13:36:40 -06002225 nvme_dev_shutdown(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002226 nvme_dev_remove_admin(dev);
2227 nvme_free_queues(dev, 0);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002228 nvme_release_cmb(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002229 nvme_release_prp_pools(dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002230 nvme_put_ctrl(&dev->ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002231}
2232
Jingoo Han671a6012014-02-13 11:19:14 +09002233#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002234static int nvme_suspend(struct device *dev)
2235{
2236 struct pci_dev *pdev = to_pci_dev(dev);
2237 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2238
2239 nvme_dev_shutdown(ndev);
2240 return 0;
2241}
2242
2243static int nvme_resume(struct device *dev)
2244{
2245 struct pci_dev *pdev = to_pci_dev(dev);
2246 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002247
Keith Busch92f7a162015-10-23 11:42:02 -06002248 queue_work(nvme_workq, &ndev->reset_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002249 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002250}
Jingoo Han671a6012014-02-13 11:19:14 +09002251#endif
Keith Buschcd638942013-07-15 15:02:23 -06002252
2253static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002254
Keith Buscha0a34082015-12-07 15:30:31 -07002255static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
2256 pci_channel_state_t state)
2257{
2258 struct nvme_dev *dev = pci_get_drvdata(pdev);
2259
2260 /*
2261 * A frozen channel requires a reset. When detected, this method will
2262 * shutdown the controller to quiesce. The controller will be restarted
2263 * after the slot reset through driver's slot_reset callback.
2264 */
2265 dev_warn(&pdev->dev, "error detected: state:%d\n", state);
2266 switch (state) {
2267 case pci_channel_io_normal:
2268 return PCI_ERS_RESULT_CAN_RECOVER;
2269 case pci_channel_io_frozen:
2270 nvme_dev_shutdown(dev);
2271 return PCI_ERS_RESULT_NEED_RESET;
2272 case pci_channel_io_perm_failure:
2273 return PCI_ERS_RESULT_DISCONNECT;
2274 }
2275 return PCI_ERS_RESULT_NEED_RESET;
2276}
2277
2278static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev)
2279{
2280 struct nvme_dev *dev = pci_get_drvdata(pdev);
2281
2282 dev_info(&pdev->dev, "restart after slot reset\n");
2283 pci_restore_state(pdev);
2284 queue_work(nvme_workq, &dev->reset_work);
2285 return PCI_ERS_RESULT_RECOVERED;
2286}
2287
2288static void nvme_error_resume(struct pci_dev *pdev)
2289{
2290 pci_cleanup_aer_uncorrect_error_status(pdev);
2291}
2292
Stephen Hemminger1d352032012-09-07 09:33:17 -07002293static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002294 .error_detected = nvme_error_detected,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002295 .slot_reset = nvme_slot_reset,
2296 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002297 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002298};
2299
2300/* Move to pci_ids.h later */
2301#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2302
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002303static const struct pci_device_id nvme_id_table[] = {
Christoph Hellwig106198e2015-11-26 10:07:41 +01002304 { PCI_VDEVICE(INTEL, 0x0953),
2305 .driver_data = NVME_QUIRK_STRIPE_SIZE, },
Keith Busch540c8012015-10-22 15:45:06 -06002306 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2307 .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002308 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01002309 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002310 { 0, }
2311};
2312MODULE_DEVICE_TABLE(pci, nvme_id_table);
2313
2314static struct pci_driver nvme_driver = {
2315 .name = "nvme",
2316 .id_table = nvme_id_table,
2317 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002318 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002319 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002320 .driver = {
2321 .pm = &nvme_dev_pm_ops,
2322 },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002323 .err_handler = &nvme_err_handler,
2324};
2325
2326static int __init nvme_init(void)
2327{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002328 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002329
Dan McLeranb9afca32014-04-07 17:10:11 -06002330 init_waitqueue_head(&nvme_kthread_wait);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002331
Keith Busch92f7a162015-10-23 11:42:02 -06002332 nvme_workq = alloc_workqueue("nvme", WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
Keith Busch9a6b9452013-12-10 13:10:36 -07002333 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002334 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002335
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002336 result = nvme_core_init();
Keith Busch5c42ea12012-07-25 16:05:18 -06002337 if (result < 0)
Keith Busch9a6b9452013-12-10 13:10:36 -07002338 goto kill_workq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002339
Keith Buschf3db22f2014-06-11 11:51:35 -06002340 result = pci_register_driver(&nvme_driver);
2341 if (result)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002342 goto core_exit;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002343 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002344
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002345 core_exit:
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002346 nvme_core_exit();
Keith Busch9a6b9452013-12-10 13:10:36 -07002347 kill_workq:
2348 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002349 return result;
2350}
2351
2352static void __exit nvme_exit(void)
2353{
2354 pci_unregister_driver(&nvme_driver);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002355 nvme_core_exit();
Keith Busch9a6b9452013-12-10 13:10:36 -07002356 destroy_workqueue(nvme_workq);
Dan McLeranb9afca32014-04-07 17:10:11 -06002357 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002358 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002359}
2360
2361MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2362MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002363MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002364module_init(nvme_init);
2365module_exit(nvme_exit);