blob: 56a315bd4d9627213e8a49a13ade68da74007a88 [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>
Christoph Hellwigdca51e72016-09-14 16:18:57 +020019#include <linux/blk-mq-pci.h>
Keith Busch42f61422014-03-24 10:46:25 -060020#include <linux/cpu.h>
Matthew Wilcoxfd63e9ce2011-05-06 08:37:54 -040021#include <linux/delay.h>
Andy Lutomirskiff5350a2017-04-20 13:37:55 -070022#include <linux/dmi.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050023#include <linux/errno.h>
24#include <linux/fs.h>
25#include <linux/genhd.h>
Keith Busch4cc09e22014-04-02 15:45:37 -060026#include <linux/hdreg.h>
Matthew Wilcox5aff9382011-05-06 08:45:47 -040027#include <linux/idr.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050028#include <linux/init.h>
29#include <linux/interrupt.h>
30#include <linux/io.h>
31#include <linux/kdev_t.h>
32#include <linux/kernel.h>
33#include <linux/mm.h>
34#include <linux/module.h>
35#include <linux/moduleparam.h>
Keith Busch77bf25e2015-11-26 12:21:29 +010036#include <linux/mutex.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050037#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050038#include <linux/poison.h>
Matthew Wilcoxc3bfe712013-07-08 17:26:25 -040039#include <linux/ptrace.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050040#include <linux/sched.h>
41#include <linux/slab.h>
Keith Busche1e5e562015-02-19 13:39:03 -070042#include <linux/t10-pi.h>
Christoph Hellwig2d55cd52016-02-29 15:59:46 +010043#include <linux/timer.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050044#include <linux/types.h>
Linus Torvalds9cf5c092015-11-06 14:22:15 -080045#include <linux/io-64-nonatomic-lo-hi.h>
Keith Busch1d277a62015-10-15 14:10:52 +020046#include <asm/unaligned.h>
Scott Bauera98e58e52017-02-03 12:50:32 -070047#include <linux/sed-opal.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090048
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020049#include "nvme.h"
50
Keith Busch9d43cf62014-05-13 11:42:02 -060051#define NVME_Q_DEPTH 1024
Jens Axboed31af0a2015-03-06 12:56:13 -070052#define NVME_AQ_DEPTH 256
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050053#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
54#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Stephen Batesc9658092016-12-16 11:54:50 -070055
Christoph Hellwigadf68f22015-11-28 15:42:28 +010056/*
57 * We handle AEN commands ourselves and don't even let the
58 * block layer know about them.
59 */
Christoph Hellwigf866fc422016-04-26 13:52:00 +020060#define NVME_AQ_BLKMQ_DEPTH (NVME_AQ_DEPTH - NVME_NR_AERS)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050061
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050062static int use_threaded_interrupts;
63module_param(use_threaded_interrupts, int, 0);
64
Jon Derrick8ffaadf2015-07-20 10:14:09 -060065static bool use_cmb_sqes = true;
66module_param(use_cmb_sqes, bool, 0644);
67MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
68
Keith Busch9a6b9452013-12-10 13:10:36 -070069static struct workqueue_struct *nvme_workq;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050070
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010071struct nvme_dev;
72struct nvme_queue;
Keith Buschb3fffde2015-02-03 11:21:42 -070073
Keith Busch4cc06522015-06-05 10:30:08 -060074static int nvme_reset(struct nvme_dev *dev);
Jens Axboea0fa9642015-11-03 20:37:26 -070075static void nvme_process_cq(struct nvme_queue *nvmeq);
Keith Buscha5cdb682016-01-12 14:41:18 -070076static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown);
Keith Buschd4b4ff82013-12-10 13:10:37 -070077
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050078/*
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010079 * Represents an NVM Express device. Each nvme_dev is a PCI function.
80 */
81struct nvme_dev {
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010082 struct nvme_queue **queues;
83 struct blk_mq_tag_set tagset;
84 struct blk_mq_tag_set admin_tagset;
85 u32 __iomem *dbs;
86 struct device *dev;
87 struct dma_pool *prp_page_pool;
88 struct dma_pool *prp_small_pool;
89 unsigned queue_count;
90 unsigned online_queues;
91 unsigned max_qid;
92 int q_depth;
93 u32 db_stride;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010094 void __iomem *bar;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010095 struct work_struct reset_work;
Christoph Hellwig5c8809e2015-11-26 12:35:49 +010096 struct work_struct remove_work;
Christoph Hellwig2d55cd52016-02-29 15:59:46 +010097 struct timer_list watchdog_timer;
Keith Busch77bf25e2015-11-26 12:21:29 +010098 struct mutex shutdown_lock;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010099 bool subsystem;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100100 void __iomem *cmb;
101 dma_addr_t cmb_dma_addr;
102 u64 cmb_size;
103 u32 cmbsz;
Stephen Bates202021c2016-10-05 20:01:12 -0600104 u32 cmbloc;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100105 struct nvme_ctrl ctrl;
Keith Buschdb3cbff2016-01-12 14:41:17 -0700106 struct completion ioq_wait;
Helen Koikef9f38e32017-04-10 12:51:07 -0300107 u32 *dbbuf_dbs;
108 dma_addr_t dbbuf_dbs_dma_addr;
109 u32 *dbbuf_eis;
110 dma_addr_t dbbuf_eis_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500111};
112
Helen Koikef9f38e32017-04-10 12:51:07 -0300113static inline unsigned int sq_idx(unsigned int qid, u32 stride)
114{
115 return qid * 2 * stride;
116}
117
118static inline unsigned int cq_idx(unsigned int qid, u32 stride)
119{
120 return (qid * 2 + 1) * stride;
121}
122
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100123static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
124{
125 return container_of(ctrl, struct nvme_dev, ctrl);
126}
127
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500128/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500129 * An NVM Express queue. Each device has at least two (one for admin
130 * commands and one for I/O commands).
131 */
132struct nvme_queue {
133 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500134 struct nvme_dev *dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -0500135 char irqname[24]; /* nvme4294967295-65535\0 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500136 spinlock_t q_lock;
137 struct nvme_command *sq_cmds;
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600138 struct nvme_command __iomem *sq_cmds_io;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500139 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600140 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500141 dma_addr_t sq_dma_addr;
142 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500143 u32 __iomem *q_db;
144 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700145 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500146 u16 sq_tail;
147 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700148 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400149 u8 cq_phase;
150 u8 cqe_seen;
Helen Koikef9f38e32017-04-10 12:51:07 -0300151 u32 *dbbuf_sq_db;
152 u32 *dbbuf_cq_db;
153 u32 *dbbuf_sq_ei;
154 u32 *dbbuf_cq_ei;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500155};
156
157/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200158 * The nvme_iod describes the data in an I/O, including the list of PRP
159 * entries. You can't see it in this data structure because C doesn't let
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100160 * me express that. Use nvme_init_iod to ensure there's enough space
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200161 * allocated to store the PRP list.
162 */
163struct nvme_iod {
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800164 struct nvme_request req;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100165 struct nvme_queue *nvmeq;
166 int aborted;
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200167 int npages; /* In the PRP list. 0 means small pool in use */
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200168 int nents; /* Used in scatterlist */
169 int length; /* Of data, in bytes */
170 dma_addr_t first_dma;
Christoph Hellwigbf684052015-10-26 17:12:51 +0900171 struct scatterlist meta_sg; /* metadata requires single contiguous buffer */
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100172 struct scatterlist *sg;
173 struct scatterlist inline_sg[0];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500174};
175
176/*
177 * Check we didin't inadvertently grow the command struct
178 */
179static inline void _nvme_check_size(void)
180{
181 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
182 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
183 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
184 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
185 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400186 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700187 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500188 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
189 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
190 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
191 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600192 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Helen Koikef9f38e32017-04-10 12:51:07 -0300193 BUILD_BUG_ON(sizeof(struct nvme_dbbuf) != 64);
194}
195
196static inline unsigned int nvme_dbbuf_size(u32 stride)
197{
198 return ((num_possible_cpus() + 1) * 8 * stride);
199}
200
201static int nvme_dbbuf_dma_alloc(struct nvme_dev *dev)
202{
203 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
204
205 if (dev->dbbuf_dbs)
206 return 0;
207
208 dev->dbbuf_dbs = dma_alloc_coherent(dev->dev, mem_size,
209 &dev->dbbuf_dbs_dma_addr,
210 GFP_KERNEL);
211 if (!dev->dbbuf_dbs)
212 return -ENOMEM;
213 dev->dbbuf_eis = dma_alloc_coherent(dev->dev, mem_size,
214 &dev->dbbuf_eis_dma_addr,
215 GFP_KERNEL);
216 if (!dev->dbbuf_eis) {
217 dma_free_coherent(dev->dev, mem_size,
218 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
219 dev->dbbuf_dbs = NULL;
220 return -ENOMEM;
221 }
222
223 return 0;
224}
225
226static void nvme_dbbuf_dma_free(struct nvme_dev *dev)
227{
228 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
229
230 if (dev->dbbuf_dbs) {
231 dma_free_coherent(dev->dev, mem_size,
232 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
233 dev->dbbuf_dbs = NULL;
234 }
235 if (dev->dbbuf_eis) {
236 dma_free_coherent(dev->dev, mem_size,
237 dev->dbbuf_eis, dev->dbbuf_eis_dma_addr);
238 dev->dbbuf_eis = NULL;
239 }
240}
241
242static void nvme_dbbuf_init(struct nvme_dev *dev,
243 struct nvme_queue *nvmeq, int qid)
244{
245 if (!dev->dbbuf_dbs || !qid)
246 return;
247
248 nvmeq->dbbuf_sq_db = &dev->dbbuf_dbs[sq_idx(qid, dev->db_stride)];
249 nvmeq->dbbuf_cq_db = &dev->dbbuf_dbs[cq_idx(qid, dev->db_stride)];
250 nvmeq->dbbuf_sq_ei = &dev->dbbuf_eis[sq_idx(qid, dev->db_stride)];
251 nvmeq->dbbuf_cq_ei = &dev->dbbuf_eis[cq_idx(qid, dev->db_stride)];
252}
253
254static void nvme_dbbuf_set(struct nvme_dev *dev)
255{
256 struct nvme_command c;
257
258 if (!dev->dbbuf_dbs)
259 return;
260
261 memset(&c, 0, sizeof(c));
262 c.dbbuf.opcode = nvme_admin_dbbuf;
263 c.dbbuf.prp1 = cpu_to_le64(dev->dbbuf_dbs_dma_addr);
264 c.dbbuf.prp2 = cpu_to_le64(dev->dbbuf_eis_dma_addr);
265
266 if (nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0)) {
267 dev_warn(dev->dev, "unable to set dbbuf\n");
268 /* Free memory and continue on */
269 nvme_dbbuf_dma_free(dev);
270 }
271}
272
273static inline int nvme_dbbuf_need_event(u16 event_idx, u16 new_idx, u16 old)
274{
275 return (u16)(new_idx - event_idx - 1) < (u16)(new_idx - old);
276}
277
278/* Update dbbuf and return true if an MMIO is required */
279static bool nvme_dbbuf_update_and_check_event(u16 value, u32 *dbbuf_db,
280 volatile u32 *dbbuf_ei)
281{
282 if (dbbuf_db) {
283 u16 old_value;
284
285 /*
286 * Ensure that the queue is written before updating
287 * the doorbell in memory
288 */
289 wmb();
290
291 old_value = *dbbuf_db;
292 *dbbuf_db = value;
293
294 if (!nvme_dbbuf_need_event(*dbbuf_ei, value, old_value))
295 return false;
296 }
297
298 return true;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500299}
300
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700301/*
302 * Max size of iod being embedded in the request payload
303 */
304#define NVME_INT_PAGES 2
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100305#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700306
307/*
308 * Will slightly overestimate the number of pages needed. This is OK
309 * as it only leads to a small amount of wasted memory for the lifetime of
310 * the I/O.
311 */
312static int nvme_npages(unsigned size, struct nvme_dev *dev)
313{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100314 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
315 dev->ctrl.page_size);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700316 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
317}
318
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100319static unsigned int nvme_iod_alloc_size(struct nvme_dev *dev,
320 unsigned int size, unsigned int nseg)
321{
322 return sizeof(__le64 *) * nvme_npages(size, dev) +
323 sizeof(struct scatterlist) * nseg;
324}
325
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700326static unsigned int nvme_cmd_size(struct nvme_dev *dev)
327{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100328 return sizeof(struct nvme_iod) +
329 nvme_iod_alloc_size(dev, NVME_INT_BYTES(dev), NVME_INT_PAGES);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700330}
331
Christoph Hellwigdca51e72016-09-14 16:18:57 +0200332static int nvmeq_irq(struct nvme_queue *nvmeq)
333{
334 return pci_irq_vector(to_pci_dev(nvmeq->dev->dev), nvmeq->cq_vector);
335}
336
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700337static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
338 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500339{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700340 struct nvme_dev *dev = data;
341 struct nvme_queue *nvmeq = dev->queues[0];
342
Keith Busch42483222015-06-01 09:29:54 -0600343 WARN_ON(hctx_idx != 0);
344 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
345 WARN_ON(nvmeq->tags);
346
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700347 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600348 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700349 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500350}
351
Keith Busch4af0e212015-06-08 10:08:13 -0600352static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
353{
354 struct nvme_queue *nvmeq = hctx->driver_data;
355
356 nvmeq->tags = NULL;
357}
358
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600359static int nvme_admin_init_request(struct blk_mq_tag_set *set,
360 struct request *req, unsigned int hctx_idx,
361 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600362{
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600363 struct nvme_dev *dev = set->driver_data;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100364 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700365 struct nvme_queue *nvmeq = dev->queues[0];
366
367 BUG_ON(!nvmeq);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100368 iod->nvmeq = nvmeq;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700369 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600370}
371
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700372static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
373 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500374{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700375 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600376 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500377
Keith Busch42483222015-06-01 09:29:54 -0600378 if (!nvmeq->tags)
379 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500380
Keith Busch42483222015-06-01 09:29:54 -0600381 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700382 hctx->driver_data = nvmeq;
383 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500384}
385
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600386static int nvme_init_request(struct blk_mq_tag_set *set, struct request *req,
387 unsigned int hctx_idx, unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500388{
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600389 struct nvme_dev *dev = set->driver_data;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100390 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700391 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
392
393 BUG_ON(!nvmeq);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100394 iod->nvmeq = nvmeq;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700395 return 0;
396}
397
Christoph Hellwigdca51e72016-09-14 16:18:57 +0200398static int nvme_pci_map_queues(struct blk_mq_tag_set *set)
399{
400 struct nvme_dev *dev = set->driver_data;
401
402 return blk_mq_pci_map_queues(set, to_pci_dev(dev->dev));
403}
404
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500405/**
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100406 * __nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500407 * @nvmeq: The queue to use
408 * @cmd: The command to send
409 *
410 * Safe to use from interrupt context
411 */
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530412static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
413 struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500414{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700415 u16 tail = nvmeq->sq_tail;
416
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600417 if (nvmeq->sq_cmds_io)
418 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
419 else
420 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
421
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500422 if (++tail == nvmeq->q_depth)
423 tail = 0;
Helen Koikef9f38e32017-04-10 12:51:07 -0300424 if (nvme_dbbuf_update_and_check_event(tail, nvmeq->dbbuf_sq_db,
425 nvmeq->dbbuf_sq_ei))
426 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500427 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500428}
429
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100430static __le64 **iod_list(struct request *req)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700431{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100432 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700433 return (__le64 **)(iod->sg + blk_rq_nr_phys_segments(req));
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700434}
435
Christoph Hellwigb131c612017-01-13 12:29:12 +0100436static int nvme_init_iod(struct request *rq, struct nvme_dev *dev)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500437{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100438 struct nvme_iod *iod = blk_mq_rq_to_pdu(rq);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700439 int nseg = blk_rq_nr_phys_segments(rq);
Christoph Hellwigb131c612017-01-13 12:29:12 +0100440 unsigned int size = blk_rq_payload_bytes(rq);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500441
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100442 if (nseg > NVME_INT_PAGES || size > NVME_INT_BYTES(dev)) {
443 iod->sg = kmalloc(nvme_iod_alloc_size(dev, size, nseg), GFP_ATOMIC);
444 if (!iod->sg)
445 return BLK_MQ_RQ_QUEUE_BUSY;
446 } else {
447 iod->sg = iod->inline_sg;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700448 }
449
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100450 iod->aborted = 0;
451 iod->npages = -1;
452 iod->nents = 0;
453 iod->length = size;
Keith Buschf80ec962016-07-12 16:20:31 -0700454
Omar Sandovalbac00002016-11-15 11:11:58 -0800455 return BLK_MQ_RQ_QUEUE_OK;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700456}
457
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100458static void nvme_free_iod(struct nvme_dev *dev, struct request *req)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500459{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100460 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100461 const int last_prp = dev->ctrl.page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500462 int i;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100463 __le64 **list = iod_list(req);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500464 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500465
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500466 if (iod->npages == 0)
467 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
468 for (i = 0; i < iod->npages; i++) {
469 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500470 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500471 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500472 prp_dma = next_prp_dma;
473 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700474
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100475 if (iod->sg != iod->inline_sg)
476 kfree(iod->sg);
Keith Buschb4ff9c82014-08-29 09:06:12 -0600477}
478
Keith Busch52b68d72015-02-23 09:16:21 -0700479#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700480static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
481{
482 if (be32_to_cpu(pi->ref_tag) == v)
483 pi->ref_tag = cpu_to_be32(p);
484}
485
486static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
487{
488 if (be32_to_cpu(pi->ref_tag) == p)
489 pi->ref_tag = cpu_to_be32(v);
490}
491
492/**
493 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
494 *
495 * The virtual start sector is the one that was originally submitted by the
496 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
497 * start sector may be different. Remap protection information to match the
498 * physical LBA on writes, and back to the original seed on reads.
499 *
500 * Type 0 and 3 do not have a ref tag, so no remapping required.
501 */
502static void nvme_dif_remap(struct request *req,
503 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
504{
505 struct nvme_ns *ns = req->rq_disk->private_data;
506 struct bio_integrity_payload *bip;
507 struct t10_pi_tuple *pi;
508 void *p, *pmap;
509 u32 i, nlb, ts, phys, virt;
510
511 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
512 return;
513
514 bip = bio_integrity(req->bio);
515 if (!bip)
516 return;
517
518 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700519
520 p = pmap;
521 virt = bip_get_seed(bip);
522 phys = nvme_block_nr(ns, blk_rq_pos(req));
523 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
Dan Williamsac6fc482015-10-21 13:20:18 -0400524 ts = ns->disk->queue->integrity.tuple_size;
Keith Busche1e5e562015-02-19 13:39:03 -0700525
526 for (i = 0; i < nlb; i++, virt++, phys++) {
527 pi = (struct t10_pi_tuple *)p;
528 dif_swap(phys, virt, pi);
529 p += ts;
530 }
531 kunmap_atomic(pmap);
532}
Keith Busch52b68d72015-02-23 09:16:21 -0700533#else /* CONFIG_BLK_DEV_INTEGRITY */
534static void nvme_dif_remap(struct request *req,
535 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
536{
537}
538static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
539{
540}
541static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
542{
543}
Keith Busch52b68d72015-02-23 09:16:21 -0700544#endif
545
Christoph Hellwigb131c612017-01-13 12:29:12 +0100546static bool nvme_setup_prps(struct nvme_dev *dev, struct request *req)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500547{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100548 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500549 struct dma_pool *pool;
Christoph Hellwigb131c612017-01-13 12:29:12 +0100550 int length = blk_rq_payload_bytes(req);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500551 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500552 int dma_len = sg_dma_len(sg);
553 u64 dma_addr = sg_dma_address(sg);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100554 u32 page_size = dev->ctrl.page_size;
Murali Iyerf137e0f2015-03-26 11:07:51 -0500555 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500556 __le64 *prp_list;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100557 __le64 **list = iod_list(req);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500558 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500559 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500560
Keith Busch1d090622014-06-23 11:34:01 -0600561 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500562 if (length <= 0)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200563 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500564
Keith Busch1d090622014-06-23 11:34:01 -0600565 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500566 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600567 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500568 } else {
569 sg = sg_next(sg);
570 dma_addr = sg_dma_address(sg);
571 dma_len = sg_dma_len(sg);
572 }
573
Keith Busch1d090622014-06-23 11:34:01 -0600574 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600575 iod->first_dma = dma_addr;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200576 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500577 }
578
Keith Busch1d090622014-06-23 11:34:01 -0600579 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500580 if (nprps <= (256 / 8)) {
581 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500582 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500583 } else {
584 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500585 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500586 }
587
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200588 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400589 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600590 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500591 iod->npages = -1;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200592 return false;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400593 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500594 list[0] = prp_list;
595 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500596 i = 0;
597 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600598 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500599 __le64 *old_prp_list = prp_list;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200600 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500601 if (!prp_list)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200602 return false;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500603 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400604 prp_list[0] = old_prp_list[i - 1];
605 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
606 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500607 }
608 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600609 dma_len -= page_size;
610 dma_addr += page_size;
611 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500612 if (length <= 0)
613 break;
614 if (dma_len > 0)
615 continue;
616 BUG_ON(dma_len < 0);
617 sg = sg_next(sg);
618 dma_addr = sg_dma_address(sg);
619 dma_len = sg_dma_len(sg);
620 }
621
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200622 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500623}
624
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100625static int nvme_map_data(struct nvme_dev *dev, struct request *req,
Christoph Hellwigb131c612017-01-13 12:29:12 +0100626 struct nvme_command *cmnd)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200627{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100628 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200629 struct request_queue *q = req->q;
630 enum dma_data_direction dma_dir = rq_data_dir(req) ?
631 DMA_TO_DEVICE : DMA_FROM_DEVICE;
632 int ret = BLK_MQ_RQ_QUEUE_ERROR;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200633
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700634 sg_init_table(iod->sg, blk_rq_nr_phys_segments(req));
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200635 iod->nents = blk_rq_map_sg(q, req, iod->sg);
636 if (!iod->nents)
637 goto out;
638
639 ret = BLK_MQ_RQ_QUEUE_BUSY;
Mauricio Faria de Oliveira2b6b5352016-10-11 13:54:20 -0700640 if (!dma_map_sg_attrs(dev->dev, iod->sg, iod->nents, dma_dir,
641 DMA_ATTR_NO_WARN))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200642 goto out;
643
Christoph Hellwigb131c612017-01-13 12:29:12 +0100644 if (!nvme_setup_prps(dev, req))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200645 goto out_unmap;
646
647 ret = BLK_MQ_RQ_QUEUE_ERROR;
648 if (blk_integrity_rq(req)) {
649 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
650 goto out_unmap;
651
Christoph Hellwigbf684052015-10-26 17:12:51 +0900652 sg_init_table(&iod->meta_sg, 1);
653 if (blk_rq_map_integrity_sg(q, req->bio, &iod->meta_sg) != 1)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200654 goto out_unmap;
655
656 if (rq_data_dir(req))
657 nvme_dif_remap(req, nvme_dif_prep);
658
Christoph Hellwigbf684052015-10-26 17:12:51 +0900659 if (!dma_map_sg(dev->dev, &iod->meta_sg, 1, dma_dir))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200660 goto out_unmap;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200661 }
662
Christoph Hellwigeb793e22016-06-13 16:45:25 +0200663 cmnd->rw.dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
664 cmnd->rw.dptr.prp2 = cpu_to_le64(iod->first_dma);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200665 if (blk_integrity_rq(req))
Christoph Hellwigbf684052015-10-26 17:12:51 +0900666 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(&iod->meta_sg));
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200667 return BLK_MQ_RQ_QUEUE_OK;
668
669out_unmap:
670 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
671out:
672 return ret;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200673}
674
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100675static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100676{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100677 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100678 enum dma_data_direction dma_dir = rq_data_dir(req) ?
679 DMA_TO_DEVICE : DMA_FROM_DEVICE;
680
681 if (iod->nents) {
682 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
683 if (blk_integrity_rq(req)) {
684 if (!rq_data_dir(req))
685 nvme_dif_remap(req, nvme_dif_complete);
Christoph Hellwigbf684052015-10-26 17:12:51 +0900686 dma_unmap_sg(dev->dev, &iod->meta_sg, 1, dma_dir);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100687 }
688 }
689
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700690 nvme_cleanup_cmd(req);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100691 nvme_free_iod(dev, req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500692}
693
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700694/*
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200695 * NOTE: ns is NULL when called on the admin queue.
696 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700697static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
698 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600699{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700700 struct nvme_ns *ns = hctx->queue->queuedata;
701 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200702 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700703 struct request *req = bd->rq;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200704 struct nvme_command cmnd;
705 int ret = BLK_MQ_RQ_QUEUE_OK;
Keith Buschedd10d32014-04-03 16:45:23 -0600706
Keith Busche1e5e562015-02-19 13:39:03 -0700707 /*
708 * If formated with metadata, require the block layer provide a buffer
709 * unless this namespace is formated such that the metadata can be
710 * stripped/generated by the controller with PRACT=1.
711 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200712 if (ns && ns->ms && !blk_integrity_rq(req)) {
Keith Busch71feb362015-06-19 11:07:30 -0600713 if (!(ns->pi_type && ns->ms == 8) &&
Christoph Hellwig57292b52017-01-31 16:57:29 +0100714 !blk_rq_is_passthrough(req)) {
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100715 blk_mq_end_request(req, -EFAULT);
Keith Busche1e5e562015-02-19 13:39:03 -0700716 return BLK_MQ_RQ_QUEUE_OK;
717 }
718 }
719
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700720 ret = nvme_setup_cmd(ns, req, &cmnd);
Omar Sandovalbac00002016-11-15 11:11:58 -0800721 if (ret != BLK_MQ_RQ_QUEUE_OK)
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100722 return ret;
Keith Buschedd10d32014-04-03 16:45:23 -0600723
Christoph Hellwigb131c612017-01-13 12:29:12 +0100724 ret = nvme_init_iod(req, dev);
Omar Sandovalbac00002016-11-15 11:11:58 -0800725 if (ret != BLK_MQ_RQ_QUEUE_OK)
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700726 goto out_free_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600727
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700728 if (blk_rq_nr_phys_segments(req))
Christoph Hellwigb131c612017-01-13 12:29:12 +0100729 ret = nvme_map_data(dev, req, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700730
Omar Sandovalbac00002016-11-15 11:11:58 -0800731 if (ret != BLK_MQ_RQ_QUEUE_OK)
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700732 goto out_cleanup_iod;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700733
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100734 blk_mq_start_request(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200735
736 spin_lock_irq(&nvmeq->q_lock);
Keith Buschae1fba22016-02-11 13:05:42 -0700737 if (unlikely(nvmeq->cq_vector < 0)) {
Keith Busch9ef39322017-02-10 18:15:52 -0500738 ret = BLK_MQ_RQ_QUEUE_ERROR;
Keith Buschae1fba22016-02-11 13:05:42 -0700739 spin_unlock_irq(&nvmeq->q_lock);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700740 goto out_cleanup_iod;
Keith Buschae1fba22016-02-11 13:05:42 -0700741 }
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200742 __nvme_submit_cmd(nvmeq, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700743 nvme_process_cq(nvmeq);
744 spin_unlock_irq(&nvmeq->q_lock);
745 return BLK_MQ_RQ_QUEUE_OK;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700746out_cleanup_iod:
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100747 nvme_free_iod(dev, req);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700748out_free_cmd:
749 nvme_cleanup_cmd(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200750 return ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500751}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500752
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200753static void nvme_pci_complete_rq(struct request *req)
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100754{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100755 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100756
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200757 nvme_unmap_data(iod->nvmeq->dev, req);
758 nvme_complete_rq(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500759}
760
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100761/* We read the CQE phase first to check if the rest of the entry is valid */
762static inline bool nvme_cqe_valid(struct nvme_queue *nvmeq, u16 head,
763 u16 phase)
764{
765 return (le16_to_cpu(nvmeq->cqes[head].status) & 1) == phase;
766}
767
Jens Axboea0fa9642015-11-03 20:37:26 -0700768static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500769{
Matthew Wilcox82123462011-01-20 13:24:06 -0500770 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500771
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500772 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500773 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500774
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100775 while (nvme_cqe_valid(nvmeq, head, phase)) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500776 struct nvme_completion cqe = nvmeq->cqes[head];
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100777 struct request *req;
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100778
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500779 if (++head == nvmeq->q_depth) {
780 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500781 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500782 }
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100783
Jens Axboea0fa9642015-11-03 20:37:26 -0700784 if (tag && *tag == cqe.command_id)
785 *tag = -1;
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100786
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100787 if (unlikely(cqe.command_id >= nvmeq->q_depth)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -0700788 dev_warn(nvmeq->dev->ctrl.device,
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100789 "invalid id %d completed on queue %d\n",
790 cqe.command_id, le16_to_cpu(cqe.sq_id));
791 continue;
792 }
793
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100794 /*
795 * AEN requests are special as they don't time out and can
796 * survive any kind of queue freeze and often don't respond to
797 * aborts. We don't even bother to allocate a struct request
798 * for them but rather special case them here.
799 */
800 if (unlikely(nvmeq->qid == 0 &&
801 cqe.command_id >= NVME_AQ_BLKMQ_DEPTH)) {
Christoph Hellwig7bf58532016-11-10 07:32:34 -0800802 nvme_complete_async_event(&nvmeq->dev->ctrl,
803 cqe.status, &cqe.result);
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100804 continue;
805 }
806
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100807 req = blk_mq_tag_to_rq(*nvmeq->tags, cqe.command_id);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200808 nvme_end_request(req, cqe.status, cqe.result);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500809 }
810
Matthew Wilcox82123462011-01-20 13:24:06 -0500811 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Jens Axboea0fa9642015-11-03 20:37:26 -0700812 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500813
Keith Busch604e8c82015-11-20 08:38:13 -0700814 if (likely(nvmeq->cq_vector >= 0))
Helen Koikef9f38e32017-04-10 12:51:07 -0300815 if (nvme_dbbuf_update_and_check_event(head, nvmeq->dbbuf_cq_db,
816 nvmeq->dbbuf_cq_ei))
817 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500818 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500819 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500820
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400821 nvmeq->cqe_seen = 1;
Jens Axboea0fa9642015-11-03 20:37:26 -0700822}
823
824static void nvme_process_cq(struct nvme_queue *nvmeq)
825{
826 __nvme_process_cq(nvmeq, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500827}
828
829static irqreturn_t nvme_irq(int irq, void *data)
830{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500831 irqreturn_t result;
832 struct nvme_queue *nvmeq = data;
833 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400834 nvme_process_cq(nvmeq);
835 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
836 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500837 spin_unlock(&nvmeq->q_lock);
838 return result;
839}
840
841static irqreturn_t nvme_irq_check(int irq, void *data)
842{
843 struct nvme_queue *nvmeq = data;
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100844 if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase))
845 return IRQ_WAKE_THREAD;
846 return IRQ_NONE;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500847}
848
Keith Busch7776db12017-02-24 17:59:28 -0500849static int __nvme_poll(struct nvme_queue *nvmeq, unsigned int tag)
Jens Axboea0fa9642015-11-03 20:37:26 -0700850{
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100851 if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase)) {
Jens Axboea0fa9642015-11-03 20:37:26 -0700852 spin_lock_irq(&nvmeq->q_lock);
853 __nvme_process_cq(nvmeq, &tag);
854 spin_unlock_irq(&nvmeq->q_lock);
855
856 if (tag == -1)
857 return 1;
858 }
859
860 return 0;
861}
862
Keith Busch7776db12017-02-24 17:59:28 -0500863static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
864{
865 struct nvme_queue *nvmeq = hctx->driver_data;
866
867 return __nvme_poll(nvmeq, tag);
868}
869
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200870static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl, int aer_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500871{
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200872 struct nvme_dev *dev = to_nvme_dev(ctrl);
Christoph Hellwig9396dec2016-02-29 15:59:44 +0100873 struct nvme_queue *nvmeq = dev->queues[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700874 struct nvme_command c;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700875
876 memset(&c, 0, sizeof(c));
877 c.common.opcode = nvme_admin_async_event;
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200878 c.common.command_id = NVME_AQ_BLKMQ_DEPTH + aer_idx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700879
Christoph Hellwig9396dec2016-02-29 15:59:44 +0100880 spin_lock_irq(&nvmeq->q_lock);
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200881 __nvme_submit_cmd(nvmeq, &c);
Christoph Hellwig9396dec2016-02-29 15:59:44 +0100882 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -0700883}
884
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500885static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
886{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500887 struct nvme_command c;
888
889 memset(&c, 0, sizeof(c));
890 c.delete_queue.opcode = opcode;
891 c.delete_queue.qid = cpu_to_le16(id);
892
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100893 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500894}
895
896static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
897 struct nvme_queue *nvmeq)
898{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500899 struct nvme_command c;
900 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
901
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200902 /*
903 * Note: we (ab)use the fact the the prp fields survive if no data
904 * is attached to the request.
905 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500906 memset(&c, 0, sizeof(c));
907 c.create_cq.opcode = nvme_admin_create_cq;
908 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
909 c.create_cq.cqid = cpu_to_le16(qid);
910 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
911 c.create_cq.cq_flags = cpu_to_le16(flags);
912 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
913
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100914 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500915}
916
917static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
918 struct nvme_queue *nvmeq)
919{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500920 struct nvme_command c;
Keith Busch81c1cd92017-04-04 18:18:12 -0400921 int flags = NVME_QUEUE_PHYS_CONTIG;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500922
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200923 /*
924 * Note: we (ab)use the fact the the prp fields survive if no data
925 * is attached to the request.
926 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500927 memset(&c, 0, sizeof(c));
928 c.create_sq.opcode = nvme_admin_create_sq;
929 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
930 c.create_sq.sqid = cpu_to_le16(qid);
931 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
932 c.create_sq.sq_flags = cpu_to_le16(flags);
933 c.create_sq.cqid = cpu_to_le16(qid);
934
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100935 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500936}
937
938static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
939{
940 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
941}
942
943static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
944{
945 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
946}
947
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100948static void abort_endio(struct request *req, int error)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400949{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100950 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
951 struct nvme_queue *nvmeq = iod->nvmeq;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400952
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200953 dev_warn(nvmeq->dev->ctrl.device,
954 "Abort status: 0x%x", nvme_req(req)->status);
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100955 atomic_inc(&nvmeq->dev->ctrl.abort_limit);
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100956 blk_mq_free_request(req);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200957}
958
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +0200959static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200960{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100961 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
962 struct nvme_queue *nvmeq = iod->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -0700963 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700964 struct request *abort_req;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700965 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -0700966
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +0200967 /*
Keith Busch7776db12017-02-24 17:59:28 -0500968 * Did we miss an interrupt?
969 */
970 if (__nvme_poll(nvmeq, req->tag)) {
971 dev_warn(dev->ctrl.device,
972 "I/O %d QID %d timeout, completion polled\n",
973 req->tag, nvmeq->qid);
974 return BLK_EH_HANDLED;
975 }
976
977 /*
Christoph Hellwigfd634f412015-11-26 12:42:26 +0100978 * Shutdown immediately if controller times out while starting. The
979 * reset work will see the pci device disabled when it gets the forced
980 * cancellation error. All outstanding requests are completed on
981 * shutdown, so we return BLK_EH_HANDLED.
982 */
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200983 if (dev->ctrl.state == NVME_CTRL_RESETTING) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -0700984 dev_warn(dev->ctrl.device,
Christoph Hellwigfd634f412015-11-26 12:42:26 +0100985 "I/O %d QID %d timeout, disable controller\n",
986 req->tag, nvmeq->qid);
Keith Buscha5cdb682016-01-12 14:41:18 -0700987 nvme_dev_disable(dev, false);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200988 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
Christoph Hellwigfd634f412015-11-26 12:42:26 +0100989 return BLK_EH_HANDLED;
Keith Buschc30341d2013-12-10 13:10:38 -0700990 }
991
Christoph Hellwigfd634f412015-11-26 12:42:26 +0100992 /*
993 * Shutdown the controller immediately and schedule a reset if the
994 * command was already aborted once before and still hasn't been
995 * returned to the driver, or if this is the admin queue.
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +0200996 */
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100997 if (!nvmeq->qid || iod->aborted) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -0700998 dev_warn(dev->ctrl.device,
Keith Busche1569a12015-11-26 12:11:07 +0100999 "I/O %d QID %d timeout, reset controller\n",
1000 req->tag, nvmeq->qid);
Keith Buscha5cdb682016-01-12 14:41:18 -07001001 nvme_dev_disable(dev, false);
Keith Buschc5f6ce92016-10-05 16:32:45 -04001002 nvme_reset(dev);
Keith Buschc30341d2013-12-10 13:10:38 -07001003
Keith Busche1569a12015-11-26 12:11:07 +01001004 /*
1005 * Mark the request as handled, since the inline shutdown
1006 * forces all outstanding requests to complete.
1007 */
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001008 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
Keith Busche1569a12015-11-26 12:11:07 +01001009 return BLK_EH_HANDLED;
Keith Buschc30341d2013-12-10 13:10:38 -07001010 }
Keith Buschc30341d2013-12-10 13:10:38 -07001011
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001012 if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) {
1013 atomic_inc(&dev->ctrl.abort_limit);
1014 return BLK_EH_RESET_TIMER;
1015 }
Keith Busch7bf7d772017-01-24 18:07:00 -05001016 iod->aborted = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001017
Keith Buschc30341d2013-12-10 13:10:38 -07001018 memset(&cmd, 0, sizeof(cmd));
1019 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001020 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001021 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Keith Buschc30341d2013-12-10 13:10:38 -07001022
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001023 dev_warn(nvmeq->dev->ctrl.device,
1024 "I/O %d QID %d timeout, aborting\n",
1025 req->tag, nvmeq->qid);
Keith Buschc30341d2013-12-10 13:10:38 -07001026
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001027 abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd,
Christoph Hellwigeb71f432016-06-13 16:45:23 +02001028 BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001029 if (IS_ERR(abort_req)) {
1030 atomic_inc(&dev->ctrl.abort_limit);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001031 return BLK_EH_RESET_TIMER;
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001032 }
Keith Buschc30341d2013-12-10 13:10:38 -07001033
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001034 abort_req->timeout = ADMIN_TIMEOUT;
1035 abort_req->end_io_data = NULL;
1036 blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio);
Keith Busch07836e62015-02-19 10:34:48 -07001037
Keith Busch7a509a62015-01-07 18:55:53 -07001038 /*
1039 * The aborted req will be completed on receiving the abort req.
1040 * We enable the timer again. If hit twice, it'll cause a device reset,
1041 * as the device then is in a faulty state.
1042 */
Keith Busch07836e62015-02-19 10:34:48 -07001043 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001044}
1045
Keith Buschf435c282014-07-07 09:14:42 -06001046static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001047{
1048 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1049 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001050 if (nvmeq->sq_cmds)
1051 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001052 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1053 kfree(nvmeq);
1054}
1055
Keith Buscha1a5ef92013-12-16 13:50:00 -05001056static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001057{
1058 int i;
1059
Keith Buscha1a5ef92013-12-16 13:50:00 -05001060 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001061 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001062 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001063 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001064 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001065 }
Keith Busch22404272013-07-15 15:02:20 -06001066}
1067
Keith Busch4d115422013-12-10 13:10:40 -07001068/**
1069 * nvme_suspend_queue - put queue into suspended state
1070 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001071 */
1072static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001073{
Keith Busch2b25d982014-12-22 12:59:04 -07001074 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001075
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001076 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001077 if (nvmeq->cq_vector == -1) {
1078 spin_unlock_irq(&nvmeq->q_lock);
1079 return 1;
1080 }
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001081 vector = nvmeq_irq(nvmeq);
Keith Busch42f61422014-03-24 10:46:25 -06001082 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001083 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001084 spin_unlock_irq(&nvmeq->q_lock);
1085
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001086 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
Keith Busch25646262016-01-04 09:10:57 -07001087 blk_mq_stop_hw_queues(nvmeq->dev->ctrl.admin_q);
Keith Busch6df3dbc2015-03-26 13:49:33 -06001088
Matthew Wilcoxaba20802011-03-27 08:52:06 -04001089 free_irq(vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001090
Keith Busch4d115422013-12-10 13:10:40 -07001091 return 0;
1092}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001093
Keith Buscha5cdb682016-01-12 14:41:18 -07001094static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown)
Keith Busch4d115422013-12-10 13:10:40 -07001095{
Keith Buscha5cdb682016-01-12 14:41:18 -07001096 struct nvme_queue *nvmeq = dev->queues[0];
Keith Busch4d115422013-12-10 13:10:40 -07001097
1098 if (!nvmeq)
1099 return;
1100 if (nvme_suspend_queue(nvmeq))
1101 return;
1102
Keith Buscha5cdb682016-01-12 14:41:18 -07001103 if (shutdown)
1104 nvme_shutdown_ctrl(&dev->ctrl);
1105 else
1106 nvme_disable_ctrl(&dev->ctrl, lo_hi_readq(
1107 dev->bar + NVME_REG_CAP));
Keith Busch07836e62015-02-19 10:34:48 -07001108
1109 spin_lock_irq(&nvmeq->q_lock);
1110 nvme_process_cq(nvmeq);
1111 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001112}
1113
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001114static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1115 int entry_size)
1116{
1117 int q_depth = dev->q_depth;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001118 unsigned q_size_aligned = roundup(q_depth * entry_size,
1119 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001120
1121 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001122 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001123 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
Jon Derrickc45f5c92015-07-21 15:08:13 -06001124 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001125
1126 /*
1127 * Ensure the reduced q_depth is above some threshold where it
1128 * would be better to map queues in system memory with the
1129 * original depth
1130 */
1131 if (q_depth < 64)
1132 return -ENOMEM;
1133 }
1134
1135 return q_depth;
1136}
1137
1138static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1139 int qid, int depth)
1140{
1141 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001142 unsigned offset = (qid - 1) * roundup(SQ_SIZE(depth),
1143 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001144 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1145 nvmeq->sq_cmds_io = dev->cmb + offset;
1146 } else {
1147 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1148 &nvmeq->sq_dma_addr, GFP_KERNEL);
1149 if (!nvmeq->sq_cmds)
1150 return -ENOMEM;
1151 }
1152
1153 return 0;
1154}
1155
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001156static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001157 int depth, int node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001158{
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001159 struct nvme_queue *nvmeq = kzalloc_node(sizeof(*nvmeq), GFP_KERNEL,
1160 node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001161 if (!nvmeq)
1162 return NULL;
1163
Christoph Hellwige75ec752015-05-22 11:12:39 +02001164 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001165 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001166 if (!nvmeq->cqes)
1167 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001168
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001169 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001170 goto free_cqdma;
1171
Christoph Hellwige75ec752015-05-22 11:12:39 +02001172 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001173 nvmeq->dev = dev;
Matthew Wilcox3193f072014-01-27 15:57:22 -05001174 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001175 dev->ctrl.instance, qid);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001176 spin_lock_init(&nvmeq->q_lock);
1177 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001178 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001179 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001180 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001181 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001182 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001183 dev->queues[qid] = nvmeq;
Jon Derrick36a7e992015-05-27 12:26:23 -06001184 dev->queue_count++;
1185
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001186 return nvmeq;
1187
1188 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001189 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001190 nvmeq->cq_dma_addr);
1191 free_nvmeq:
1192 kfree(nvmeq);
1193 return NULL;
1194}
1195
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001196static int queue_request_irq(struct nvme_queue *nvmeq)
Matthew Wilcox30010822011-01-20 09:10:15 -05001197{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001198 if (use_threaded_interrupts)
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001199 return request_threaded_irq(nvmeq_irq(nvmeq), nvme_irq_check,
1200 nvme_irq, IRQF_SHARED, nvmeq->irqname, nvmeq);
1201 else
1202 return request_irq(nvmeq_irq(nvmeq), nvme_irq, IRQF_SHARED,
1203 nvmeq->irqname, nvmeq);
Matthew Wilcox30010822011-01-20 09:10:15 -05001204}
1205
Keith Busch22404272013-07-15 15:02:20 -06001206static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001207{
Keith Busch22404272013-07-15 15:02:20 -06001208 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001209
Keith Busch7be50e92014-09-10 15:48:47 -06001210 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001211 nvmeq->sq_tail = 0;
1212 nvmeq->cq_head = 0;
1213 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001214 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001215 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Helen Koikef9f38e32017-04-10 12:51:07 -03001216 nvme_dbbuf_init(dev, nvmeq, qid);
Keith Busch42f61422014-03-24 10:46:25 -06001217 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001218 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001219}
1220
1221static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1222{
1223 struct nvme_dev *dev = nvmeq->dev;
1224 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001225
Keith Busch2b25d982014-12-22 12:59:04 -07001226 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001227 result = adapter_alloc_cq(dev, qid, nvmeq);
1228 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001229 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001230
1231 result = adapter_alloc_sq(dev, qid, nvmeq);
1232 if (result < 0)
1233 goto release_cq;
1234
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001235 result = queue_request_irq(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001236 if (result < 0)
1237 goto release_sq;
1238
Keith Busch22404272013-07-15 15:02:20 -06001239 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001240 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001241
1242 release_sq:
1243 adapter_delete_sq(dev, qid);
1244 release_cq:
1245 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001246 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001247}
1248
Eric Biggersf363b082017-03-30 13:39:16 -07001249static const struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001250 .queue_rq = nvme_queue_rq,
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001251 .complete = nvme_pci_complete_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001252 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001253 .exit_hctx = nvme_admin_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001254 .init_request = nvme_admin_init_request,
1255 .timeout = nvme_timeout,
1256};
1257
Eric Biggersf363b082017-03-30 13:39:16 -07001258static const struct blk_mq_ops nvme_mq_ops = {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001259 .queue_rq = nvme_queue_rq,
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001260 .complete = nvme_pci_complete_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001261 .init_hctx = nvme_init_hctx,
1262 .init_request = nvme_init_request,
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001263 .map_queues = nvme_pci_map_queues,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001264 .timeout = nvme_timeout,
Jens Axboea0fa9642015-11-03 20:37:26 -07001265 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001266};
1267
Keith Buschea191d22015-01-07 18:55:49 -07001268static void nvme_dev_remove_admin(struct nvme_dev *dev)
1269{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001270 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
Keith Busch69d9a992016-02-24 09:15:56 -07001271 /*
1272 * If the controller was reset during removal, it's possible
1273 * user requests may be waiting on a stopped queue. Start the
1274 * queue to flush these to completion.
1275 */
1276 blk_mq_start_stopped_hw_queues(dev->ctrl.admin_q, true);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001277 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001278 blk_mq_free_tag_set(&dev->admin_tagset);
1279 }
1280}
1281
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001282static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1283{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001284 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001285 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1286 dev->admin_tagset.nr_hw_queues = 1;
Keith Busche3e9d502016-01-04 09:10:55 -07001287
1288 /*
1289 * Subtract one to leave an empty queue entry for 'Full Queue'
1290 * condition. See NVM-Express 1.2 specification, section 4.1.2.
1291 */
1292 dev->admin_tagset.queue_depth = NVME_AQ_BLKMQ_DEPTH - 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001293 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001294 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001295 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Jens Axboed3484992017-01-13 14:43:58 -07001296 dev->admin_tagset.flags = BLK_MQ_F_NO_SCHED;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001297 dev->admin_tagset.driver_data = dev;
1298
1299 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1300 return -ENOMEM;
1301
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001302 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1303 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001304 blk_mq_free_tag_set(&dev->admin_tagset);
1305 return -ENOMEM;
1306 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001307 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001308 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001309 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001310 return -ENODEV;
1311 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001312 } else
Keith Busch25646262016-01-04 09:10:57 -07001313 blk_mq_start_stopped_hw_queues(dev->ctrl.admin_q, true);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001314
1315 return 0;
1316}
1317
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001318static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001319{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001320 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001321 u32 aqa;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001322 u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001323 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001324
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001325 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1, 0) ?
Keith Buschdfbac8c2015-08-10 15:20:40 -06001326 NVME_CAP_NSSRC(cap) : 0;
1327
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001328 if (dev->subsystem &&
1329 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1330 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001331
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001332 result = nvme_disable_ctrl(&dev->ctrl, cap);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001333 if (result < 0)
1334 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001335
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001336 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001337 if (!nvmeq) {
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001338 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH,
1339 dev_to_node(dev->dev));
Keith Buschcd638942013-07-15 15:02:23 -06001340 if (!nvmeq)
1341 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001342 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001343
1344 aqa = nvmeq->q_depth - 1;
1345 aqa |= aqa << 16;
1346
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001347 writel(aqa, dev->bar + NVME_REG_AQA);
1348 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1349 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Keith Busch1d090622014-06-23 11:34:01 -06001350
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001351 result = nvme_enable_ctrl(&dev->ctrl, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001352 if (result)
Keith Buschd4875622016-11-15 15:56:26 -05001353 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001354
Keith Busch2b25d982014-12-22 12:59:04 -07001355 nvmeq->cq_vector = 0;
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001356 result = queue_request_irq(nvmeq);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001357 if (result) {
1358 nvmeq->cq_vector = -1;
Keith Buschd4875622016-11-15 15:56:26 -05001359 return result;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001360 }
Keith Busch025c5572013-05-01 13:07:51 -06001361
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001362 return result;
1363}
1364
Guilherme G. Piccolic875a702016-04-13 11:08:20 -03001365static bool nvme_should_reset(struct nvme_dev *dev, u32 csts)
1366{
1367
1368 /* If true, indicates loss of adapter communication, possibly by a
1369 * NVMe Subsystem reset.
1370 */
1371 bool nssro = dev->subsystem && (csts & NVME_CSTS_NSSRO);
1372
1373 /* If there is a reset ongoing, we shouldn't reset again. */
1374 if (work_busy(&dev->reset_work))
1375 return false;
1376
1377 /* We shouldn't reset unless the controller is on fatal error state
1378 * _or_ if we lost the communication with it.
1379 */
1380 if (!(csts & NVME_CSTS_CFS) && !nssro)
1381 return false;
1382
1383 /* If PCI error recovery process is happening, we cannot reset or
1384 * the recovery mechanism will surely fail.
1385 */
1386 if (pci_channel_offline(to_pci_dev(dev->dev)))
1387 return false;
1388
1389 return true;
1390}
1391
Andy Lutomirskid2a61912016-12-02 21:14:15 -07001392static void nvme_warn_reset(struct nvme_dev *dev, u32 csts)
1393{
1394 /* Read a config register to help see what died. */
1395 u16 pci_status;
1396 int result;
1397
1398 result = pci_read_config_word(to_pci_dev(dev->dev), PCI_STATUS,
1399 &pci_status);
1400 if (result == PCIBIOS_SUCCESSFUL)
1401 dev_warn(dev->dev,
1402 "controller is down; will reset: CSTS=0x%x, PCI_STATUS=0x%hx\n",
1403 csts, pci_status);
1404 else
1405 dev_warn(dev->dev,
1406 "controller is down; will reset: CSTS=0x%x, PCI_STATUS read failed (%d)\n",
1407 csts, result);
1408}
1409
Christoph Hellwig2d55cd52016-02-29 15:59:46 +01001410static void nvme_watchdog_timer(unsigned long data)
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001411{
Christoph Hellwig2d55cd52016-02-29 15:59:46 +01001412 struct nvme_dev *dev = (struct nvme_dev *)data;
1413 u32 csts = readl(dev->bar + NVME_REG_CSTS);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001414
Guilherme G. Piccolic875a702016-04-13 11:08:20 -03001415 /* Skip controllers under certain specific conditions. */
1416 if (nvme_should_reset(dev, csts)) {
Keith Buschc5f6ce92016-10-05 16:32:45 -04001417 if (!nvme_reset(dev))
Andy Lutomirskid2a61912016-12-02 21:14:15 -07001418 nvme_warn_reset(dev, csts);
Christoph Hellwig2d55cd52016-02-29 15:59:46 +01001419 return;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001420 }
Christoph Hellwig2d55cd52016-02-29 15:59:46 +01001421
1422 mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001423}
1424
Christoph Hellwig749941f2015-11-26 11:46:39 +01001425static int nvme_create_io_queues(struct nvme_dev *dev)
Keith Busch42f61422014-03-24 10:46:25 -06001426{
Keith Busch949928c2015-12-17 17:08:15 -07001427 unsigned i, max;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001428 int ret = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001429
Christoph Hellwig749941f2015-11-26 11:46:39 +01001430 for (i = dev->queue_count; i <= dev->max_qid; i++) {
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001431 /* vector == qid - 1, match nvme_create_queue */
1432 if (!nvme_alloc_queue(dev, i, dev->q_depth,
1433 pci_irq_get_node(to_pci_dev(dev->dev), i - 1))) {
Christoph Hellwig749941f2015-11-26 11:46:39 +01001434 ret = -ENOMEM;
Keith Busch42f61422014-03-24 10:46:25 -06001435 break;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001436 }
1437 }
Keith Busch42f61422014-03-24 10:46:25 -06001438
Keith Busch949928c2015-12-17 17:08:15 -07001439 max = min(dev->max_qid, dev->queue_count - 1);
1440 for (i = dev->online_queues; i <= max; i++) {
Christoph Hellwig749941f2015-11-26 11:46:39 +01001441 ret = nvme_create_queue(dev->queues[i], i);
Keith Buschd4875622016-11-15 15:56:26 -05001442 if (ret)
Keith Busch42f61422014-03-24 10:46:25 -06001443 break;
Matthew Wilcox27e81662014-04-11 11:58:45 -04001444 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001445
1446 /*
1447 * Ignore failing Create SQ/CQ commands, we can continue with less
1448 * than the desired aount of queues, and even a controller without
1449 * I/O queues an still be used to issue admin commands. This might
1450 * be useful to upgrade a buggy firmware for example.
1451 */
1452 return ret >= 0 ? 0 : ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001453}
1454
Stephen Bates202021c2016-10-05 20:01:12 -06001455static ssize_t nvme_cmb_show(struct device *dev,
1456 struct device_attribute *attr,
1457 char *buf)
1458{
1459 struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
1460
Stephen Batesc9658092016-12-16 11:54:50 -07001461 return scnprintf(buf, PAGE_SIZE, "cmbloc : x%08x\ncmbsz : x%08x\n",
Stephen Bates202021c2016-10-05 20:01:12 -06001462 ndev->cmbloc, ndev->cmbsz);
1463}
1464static DEVICE_ATTR(cmb, S_IRUGO, nvme_cmb_show, NULL);
1465
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001466static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1467{
1468 u64 szu, size, offset;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001469 resource_size_t bar_size;
1470 struct pci_dev *pdev = to_pci_dev(dev->dev);
1471 void __iomem *cmb;
1472 dma_addr_t dma_addr;
1473
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001474 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001475 if (!(NVME_CMB_SZ(dev->cmbsz)))
1476 return NULL;
Stephen Bates202021c2016-10-05 20:01:12 -06001477 dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001478
Stephen Bates202021c2016-10-05 20:01:12 -06001479 if (!use_cmb_sqes)
1480 return NULL;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001481
1482 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1483 size = szu * NVME_CMB_SZ(dev->cmbsz);
Stephen Bates202021c2016-10-05 20:01:12 -06001484 offset = szu * NVME_CMB_OFST(dev->cmbloc);
1485 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(dev->cmbloc));
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001486
1487 if (offset > bar_size)
1488 return NULL;
1489
1490 /*
1491 * Controllers may support a CMB size larger than their BAR,
1492 * for example, due to being behind a bridge. Reduce the CMB to
1493 * the reported size of the BAR
1494 */
1495 if (size > bar_size - offset)
1496 size = bar_size - offset;
1497
Stephen Bates202021c2016-10-05 20:01:12 -06001498 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(dev->cmbloc)) + offset;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001499 cmb = ioremap_wc(dma_addr, size);
1500 if (!cmb)
1501 return NULL;
1502
1503 dev->cmb_dma_addr = dma_addr;
1504 dev->cmb_size = size;
1505 return cmb;
1506}
1507
1508static inline void nvme_release_cmb(struct nvme_dev *dev)
1509{
1510 if (dev->cmb) {
1511 iounmap(dev->cmb);
1512 dev->cmb = NULL;
1513 }
1514}
1515
Keith Busch9d713c22013-07-15 15:02:24 -06001516static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1517{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001518 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001519}
1520
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001521static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001522{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001523 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02001524 struct pci_dev *pdev = to_pci_dev(dev->dev);
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001525 int result, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001526
Keith Busch2800b8e2016-05-13 12:38:09 -06001527 nr_io_queues = num_online_cpus();
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001528 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
1529 if (result < 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001530 return result;
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001531
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001532 if (nr_io_queues == 0)
Keith Buscha5229052016-04-08 16:09:10 -06001533 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001534
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001535 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1536 result = nvme_cmb_qdepth(dev, nr_io_queues,
1537 sizeof(struct nvme_command));
1538 if (result > 0)
1539 dev->q_depth = result;
1540 else
1541 nvme_release_cmb(dev);
1542 }
1543
Keith Busch9d713c22013-07-15 15:02:24 -06001544 size = db_bar_size(dev, nr_io_queues);
1545 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001546 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001547 do {
1548 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1549 if (dev->bar)
1550 break;
1551 if (!--nr_io_queues)
1552 return -ENOMEM;
1553 size = db_bar_size(dev, nr_io_queues);
1554 } while (1);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001555 dev->dbs = dev->bar + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07001556 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001557 }
1558
Keith Busch9d713c22013-07-15 15:02:24 -06001559 /* Deregister the admin queue's interrupt */
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001560 free_irq(pci_irq_vector(pdev, 0), adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001561
Jens Axboee32efbf2014-11-14 09:49:26 -07001562 /*
1563 * If we enable msix early due to not intx, disable it again before
1564 * setting up the full range we need.
1565 */
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001566 pci_free_irq_vectors(pdev);
1567 nr_io_queues = pci_alloc_irq_vectors(pdev, 1, nr_io_queues,
1568 PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY);
1569 if (nr_io_queues <= 0)
1570 return -EIO;
1571 dev->max_qid = nr_io_queues;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001572
Matthew Wilcox063a8092013-06-20 10:53:48 -04001573 /*
1574 * Should investigate if there's a performance win from allocating
1575 * more queues than interrupt vectors; it might allow the submission
1576 * path to scale better, even if the receive path is limited by the
1577 * number of interrupts.
1578 */
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001579
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001580 result = queue_request_irq(adminq);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001581 if (result) {
1582 adminq->cq_vector = -1;
Keith Buschd4875622016-11-15 15:56:26 -05001583 return result;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001584 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001585 return nvme_create_io_queues(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001586}
1587
Keith Buschdb3cbff2016-01-12 14:41:17 -07001588static void nvme_del_queue_end(struct request *req, int error)
1589{
1590 struct nvme_queue *nvmeq = req->end_io_data;
1591
1592 blk_mq_free_request(req);
1593 complete(&nvmeq->dev->ioq_wait);
1594}
1595
1596static void nvme_del_cq_end(struct request *req, int error)
1597{
1598 struct nvme_queue *nvmeq = req->end_io_data;
1599
1600 if (!error) {
1601 unsigned long flags;
1602
Ming Lin2e39e0f2016-04-05 10:32:04 -07001603 /*
1604 * We might be called with the AQ q_lock held
1605 * and the I/O queue q_lock should always
1606 * nest inside the AQ one.
1607 */
1608 spin_lock_irqsave_nested(&nvmeq->q_lock, flags,
1609 SINGLE_DEPTH_NESTING);
Keith Buschdb3cbff2016-01-12 14:41:17 -07001610 nvme_process_cq(nvmeq);
1611 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
1612 }
1613
1614 nvme_del_queue_end(req, error);
1615}
1616
1617static int nvme_delete_queue(struct nvme_queue *nvmeq, u8 opcode)
1618{
1619 struct request_queue *q = nvmeq->dev->ctrl.admin_q;
1620 struct request *req;
1621 struct nvme_command cmd;
1622
1623 memset(&cmd, 0, sizeof(cmd));
1624 cmd.delete_queue.opcode = opcode;
1625 cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid);
1626
Christoph Hellwigeb71f432016-06-13 16:45:23 +02001627 req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
Keith Buschdb3cbff2016-01-12 14:41:17 -07001628 if (IS_ERR(req))
1629 return PTR_ERR(req);
1630
1631 req->timeout = ADMIN_TIMEOUT;
1632 req->end_io_data = nvmeq;
1633
1634 blk_execute_rq_nowait(q, NULL, req, false,
1635 opcode == nvme_admin_delete_cq ?
1636 nvme_del_cq_end : nvme_del_queue_end);
1637 return 0;
1638}
1639
Keith Busch70659062016-10-12 09:22:16 -06001640static void nvme_disable_io_queues(struct nvme_dev *dev, int queues)
Keith Buschdb3cbff2016-01-12 14:41:17 -07001641{
Keith Busch70659062016-10-12 09:22:16 -06001642 int pass;
Keith Buschdb3cbff2016-01-12 14:41:17 -07001643 unsigned long timeout;
1644 u8 opcode = nvme_admin_delete_sq;
1645
1646 for (pass = 0; pass < 2; pass++) {
Keith Busch014a0d62016-05-06 11:50:52 -06001647 int sent = 0, i = queues;
Keith Buschdb3cbff2016-01-12 14:41:17 -07001648
1649 reinit_completion(&dev->ioq_wait);
1650 retry:
1651 timeout = ADMIN_TIMEOUT;
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06001652 for (; i > 0; i--, sent++)
1653 if (nvme_delete_queue(dev->queues[i], opcode))
Keith Buschdb3cbff2016-01-12 14:41:17 -07001654 break;
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06001655
Keith Buschdb3cbff2016-01-12 14:41:17 -07001656 while (sent--) {
1657 timeout = wait_for_completion_io_timeout(&dev->ioq_wait, timeout);
1658 if (timeout == 0)
1659 return;
1660 if (i)
1661 goto retry;
1662 }
1663 opcode = nvme_admin_delete_cq;
1664 }
1665}
1666
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001667/*
1668 * Return: error value if an error occurred setting up the queues or calling
1669 * Identify Device. 0 if these succeeded, even if adding some of the
1670 * namespaces failed. At the moment, these failures are silent. TBD which
1671 * failures should be reported.
1672 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001673static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001674{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001675 if (!dev->ctrl.tagset) {
Keith Buschffe77042015-06-08 10:08:15 -06001676 dev->tagset.ops = &nvme_mq_ops;
1677 dev->tagset.nr_hw_queues = dev->online_queues - 1;
1678 dev->tagset.timeout = NVME_IO_TIMEOUT;
1679 dev->tagset.numa_node = dev_to_node(dev->dev);
1680 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001681 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06001682 dev->tagset.cmd_size = nvme_cmd_size(dev);
1683 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
1684 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001685
Keith Buschffe77042015-06-08 10:08:15 -06001686 if (blk_mq_alloc_tag_set(&dev->tagset))
1687 return 0;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001688 dev->ctrl.tagset = &dev->tagset;
Helen Koikef9f38e32017-04-10 12:51:07 -03001689
1690 nvme_dbbuf_set(dev);
Keith Busch949928c2015-12-17 17:08:15 -07001691 } else {
1692 blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1);
1693
1694 /* Free previously allocated queues that are no longer usable */
1695 nvme_free_queues(dev, dev->online_queues);
Keith Buschffe77042015-06-08 10:08:15 -06001696 }
Keith Busch949928c2015-12-17 17:08:15 -07001697
Keith Busche1e5e562015-02-19 13:39:03 -07001698 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001699}
1700
Keith Buschb00a7262016-02-24 09:15:52 -07001701static int nvme_pci_enable(struct nvme_dev *dev)
Keith Busch0877cb02013-07-15 15:02:19 -06001702{
Keith Busch42f61422014-03-24 10:46:25 -06001703 u64 cap;
Keith Buschb00a7262016-02-24 09:15:52 -07001704 int result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001705 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001706
1707 if (pci_enable_device_mem(pdev))
1708 return result;
1709
Keith Busch0877cb02013-07-15 15:02:19 -06001710 pci_set_master(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001711
Christoph Hellwige75ec752015-05-22 11:12:39 +02001712 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
1713 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01001714 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06001715
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001716 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07001717 result = -ENODEV;
Keith Buschb00a7262016-02-24 09:15:52 -07001718 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07001719 }
Jens Axboee32efbf2014-11-14 09:49:26 -07001720
1721 /*
Keith Buscha5229052016-04-08 16:09:10 -06001722 * Some devices and/or platforms don't advertise or work with INTx
1723 * interrupts. Pre-enable a single MSIX or MSI vec for setup. We'll
1724 * adjust this later.
Jens Axboee32efbf2014-11-14 09:49:26 -07001725 */
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001726 result = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
1727 if (result < 0)
1728 return result;
Jens Axboee32efbf2014-11-14 09:49:26 -07001729
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001730 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
1731
Keith Busch42f61422014-03-24 10:46:25 -06001732 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
1733 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001734 dev->dbs = dev->bar + 4096;
Stephan Günther1f390c12015-12-01 13:23:22 -07001735
1736 /*
1737 * Temporary fix for the Apple controller found in the MacBook8,1 and
1738 * some MacBook7,1 to avoid controller resets and data loss.
1739 */
1740 if (pdev->vendor == PCI_VENDOR_ID_APPLE && pdev->device == 0x2001) {
1741 dev->q_depth = 2;
1742 dev_warn(dev->dev, "detected Apple NVMe controller, set "
1743 "queue depth=%u to work around controller resets\n",
1744 dev->q_depth);
1745 }
1746
Stephen Bates202021c2016-10-05 20:01:12 -06001747 /*
1748 * CMBs can currently only exist on >=1.2 PCIe devices. We only
1749 * populate sysfs if a CMB is implemented. Note that we add the
1750 * CMB attribute to the nvme_ctrl kobj which removes the need to remove
1751 * it on exit. Since nvme_dev_attrs_group has no name we can pass
1752 * NULL as final argument to sysfs_add_file_to_group.
1753 */
1754
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001755 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2, 0)) {
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001756 dev->cmb = nvme_map_cmb(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001757
Stephen Bates202021c2016-10-05 20:01:12 -06001758 if (dev->cmbsz) {
1759 if (sysfs_add_file_to_group(&dev->ctrl.device->kobj,
1760 &dev_attr_cmb.attr, NULL))
1761 dev_warn(dev->dev,
1762 "failed to add sysfs attribute for CMB\n");
1763 }
1764 }
1765
Keith Buscha0a34082015-12-07 15:30:31 -07001766 pci_enable_pcie_error_reporting(pdev);
1767 pci_save_state(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001768 return 0;
1769
1770 disable:
Keith Busch0877cb02013-07-15 15:02:19 -06001771 pci_disable_device(pdev);
1772 return result;
1773}
1774
1775static void nvme_dev_unmap(struct nvme_dev *dev)
1776{
Keith Buschb00a7262016-02-24 09:15:52 -07001777 if (dev->bar)
1778 iounmap(dev->bar);
Johannes Thumshirna1f447b2016-06-07 09:44:02 +02001779 pci_release_mem_regions(to_pci_dev(dev->dev));
Keith Buschb00a7262016-02-24 09:15:52 -07001780}
1781
1782static void nvme_pci_disable(struct nvme_dev *dev)
1783{
Christoph Hellwige75ec752015-05-22 11:12:39 +02001784 struct pci_dev *pdev = to_pci_dev(dev->dev);
1785
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001786 pci_free_irq_vectors(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001787
Keith Buscha0a34082015-12-07 15:30:31 -07001788 if (pci_is_enabled(pdev)) {
1789 pci_disable_pcie_error_reporting(pdev);
Christoph Hellwige75ec752015-05-22 11:12:39 +02001790 pci_disable_device(pdev);
Keith Busch4d115422013-12-10 13:10:40 -07001791 }
Keith Busch4d115422013-12-10 13:10:40 -07001792}
1793
Keith Buscha5cdb682016-01-12 14:41:18 -07001794static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001795{
Keith Busch70659062016-10-12 09:22:16 -06001796 int i, queues;
Keith Busch302ad8c2017-03-01 14:22:12 -05001797 bool dead = true;
1798 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch22404272013-07-15 15:02:20 -06001799
Christoph Hellwig2d55cd52016-02-29 15:59:46 +01001800 del_timer_sync(&dev->watchdog_timer);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001801
Keith Busch77bf25e2015-11-26 12:21:29 +01001802 mutex_lock(&dev->shutdown_lock);
Keith Busch302ad8c2017-03-01 14:22:12 -05001803 if (pci_is_enabled(pdev)) {
1804 u32 csts = readl(dev->bar + NVME_REG_CSTS);
1805
1806 if (dev->ctrl.state == NVME_CTRL_LIVE)
1807 nvme_start_freeze(&dev->ctrl);
1808 dead = !!((csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) ||
1809 pdev->error_state != pci_channel_io_normal);
Keith Buschc9d3bf82015-01-07 18:55:52 -07001810 }
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06001811
Keith Busch302ad8c2017-03-01 14:22:12 -05001812 /*
1813 * Give the controller a chance to complete all entered requests if
1814 * doing a safe shutdown.
1815 */
1816 if (!dead && shutdown)
1817 nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
1818 nvme_stop_queues(&dev->ctrl);
1819
Keith Busch70659062016-10-12 09:22:16 -06001820 queues = dev->online_queues - 1;
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06001821 for (i = dev->queue_count - 1; i > 0; i--)
1822 nvme_suspend_queue(dev->queues[i]);
1823
Keith Busch302ad8c2017-03-01 14:22:12 -05001824 if (dead) {
Gabriel Krisman Bertazi82469c52016-09-06 17:39:13 -03001825 /* A device might become IO incapable very soon during
1826 * probe, before the admin queue is configured. Thus,
1827 * queue_count can be 0 here.
1828 */
1829 if (dev->queue_count)
1830 nvme_suspend_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07001831 } else {
Keith Busch70659062016-10-12 09:22:16 -06001832 nvme_disable_io_queues(dev, queues);
Keith Buscha5cdb682016-01-12 14:41:18 -07001833 nvme_disable_admin_queue(dev, shutdown);
Keith Busch4d115422013-12-10 13:10:40 -07001834 }
Keith Buschb00a7262016-02-24 09:15:52 -07001835 nvme_pci_disable(dev);
Keith Busch07836e62015-02-19 10:34:48 -07001836
Ming Line1958e62016-05-18 14:05:01 -07001837 blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl);
1838 blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl);
Keith Busch302ad8c2017-03-01 14:22:12 -05001839
1840 /*
1841 * The driver will not be starting up queues again if shutting down so
1842 * must flush all entered requests to their failed completion to avoid
1843 * deadlocking blk-mq hot-cpu notifier.
1844 */
1845 if (shutdown)
1846 nvme_start_queues(&dev->ctrl);
Keith Busch77bf25e2015-11-26 12:21:29 +01001847 mutex_unlock(&dev->shutdown_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001848}
1849
Matthew Wilcox091b6092011-02-10 09:56:01 -05001850static int nvme_setup_prp_pools(struct nvme_dev *dev)
1851{
Christoph Hellwige75ec752015-05-22 11:12:39 +02001852 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05001853 PAGE_SIZE, PAGE_SIZE, 0);
1854 if (!dev->prp_page_pool)
1855 return -ENOMEM;
1856
Matthew Wilcox99802a72011-02-10 10:30:34 -05001857 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02001858 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05001859 256, 256, 0);
1860 if (!dev->prp_small_pool) {
1861 dma_pool_destroy(dev->prp_page_pool);
1862 return -ENOMEM;
1863 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05001864 return 0;
1865}
1866
1867static void nvme_release_prp_pools(struct nvme_dev *dev)
1868{
1869 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05001870 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05001871}
1872
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001873static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001874{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001875 struct nvme_dev *dev = to_nvme_dev(ctrl);
Keith Busch9ac27092014-01-31 16:53:39 -07001876
Helen Koikef9f38e32017-04-10 12:51:07 -03001877 nvme_dbbuf_dma_free(dev);
Christoph Hellwige75ec752015-05-22 11:12:39 +02001878 put_device(dev->dev);
Keith Busch4af0e212015-06-08 10:08:13 -06001879 if (dev->tagset.tags)
1880 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001881 if (dev->ctrl.admin_q)
1882 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07001883 kfree(dev->queues);
Scott Bauere286bcf2017-02-22 10:15:07 -07001884 free_opal_dev(dev->ctrl.opal_dev);
Keith Busch5e82e952013-02-19 10:17:58 -07001885 kfree(dev);
1886}
1887
Keith Buschf58944e2016-02-24 09:15:55 -07001888static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
1889{
Linus Torvalds237045f2016-03-18 17:13:31 -07001890 dev_warn(dev->ctrl.device, "Removing after probe failure status: %d\n", status);
Keith Buschf58944e2016-02-24 09:15:55 -07001891
1892 kref_get(&dev->ctrl.kref);
Keith Busch69d9a992016-02-24 09:15:56 -07001893 nvme_dev_disable(dev, false);
Keith Buschf58944e2016-02-24 09:15:55 -07001894 if (!schedule_work(&dev->remove_work))
1895 nvme_put_ctrl(&dev->ctrl);
1896}
1897
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001898static void nvme_reset_work(struct work_struct *work)
Keith Busch5e82e952013-02-19 10:17:58 -07001899{
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001900 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
Scott Bauera98e58e52017-02-03 12:50:32 -07001901 bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
Keith Buschf58944e2016-02-24 09:15:55 -07001902 int result = -ENODEV;
Keith Buschf0b50732013-07-15 15:02:21 -06001903
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02001904 if (WARN_ON(dev->ctrl.state == NVME_CTRL_RESETTING))
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001905 goto out;
1906
1907 /*
1908 * If we're called to reset a live controller first shut it down before
1909 * moving on.
1910 */
Keith Buschb00a7262016-02-24 09:15:52 -07001911 if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
Keith Buscha5cdb682016-01-12 14:41:18 -07001912 nvme_dev_disable(dev, false);
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001913
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02001914 if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING))
Keith Busch9bf2b972016-04-08 16:11:02 -06001915 goto out;
1916
Keith Buschb00a7262016-02-24 09:15:52 -07001917 result = nvme_pci_enable(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06001918 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001919 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06001920
1921 result = nvme_configure_admin_queue(dev);
1922 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07001923 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06001924
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001925 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07001926 result = nvme_alloc_admin_tags(dev);
1927 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07001928 goto out;
Dan McLeranb9afca32014-04-07 17:10:11 -06001929
Christoph Hellwigce4541f2015-10-16 07:58:46 +02001930 result = nvme_init_identify(&dev->ctrl);
1931 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07001932 goto out;
Christoph Hellwigce4541f2015-10-16 07:58:46 +02001933
Scott Bauere286bcf2017-02-22 10:15:07 -07001934 if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) {
1935 if (!dev->ctrl.opal_dev)
1936 dev->ctrl.opal_dev =
1937 init_opal_dev(&dev->ctrl, &nvme_sec_submit);
1938 else if (was_suspend)
1939 opal_unlock_from_suspend(dev->ctrl.opal_dev);
1940 } else {
1941 free_opal_dev(dev->ctrl.opal_dev);
1942 dev->ctrl.opal_dev = NULL;
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001943 }
Scott Bauera98e58e52017-02-03 12:50:32 -07001944
Helen Koikef9f38e32017-04-10 12:51:07 -03001945 if (dev->ctrl.oacs & NVME_CTRL_OACS_DBBUF_SUPP) {
1946 result = nvme_dbbuf_dma_alloc(dev);
1947 if (result)
1948 dev_warn(dev->dev,
1949 "unable to allocate dma for dbbuf\n");
1950 }
1951
Keith Buschf0b50732013-07-15 15:02:21 -06001952 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06001953 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07001954 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06001955
Keith Busch21f033f2016-04-12 11:13:11 -06001956 /*
1957 * A controller that can not execute IO typically requires user
1958 * intervention to correct. For such degraded controllers, the driver
1959 * should not submit commands the user did not request, so skip
1960 * registering for asynchronous event notification on this condition.
1961 */
Christoph Hellwigf866fc422016-04-26 13:52:00 +02001962 if (dev->online_queues > 1)
1963 nvme_queue_async_events(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001964
Christoph Hellwig2d55cd52016-02-29 15:59:46 +01001965 mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + HZ));
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001966
Christoph Hellwig2659e572015-10-02 18:51:31 +02001967 /*
1968 * Keep the controller around but remove all namespaces if we don't have
1969 * any working I/O queue.
1970 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001971 if (dev->online_queues < 2) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001972 dev_warn(dev->ctrl.device, "IO queues not created\n");
Keith Busch3b247742016-04-27 15:51:18 -06001973 nvme_kill_queues(&dev->ctrl);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001974 nvme_remove_namespaces(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001975 } else {
Keith Busch25646262016-01-04 09:10:57 -07001976 nvme_start_queues(&dev->ctrl);
Keith Busch302ad8c2017-03-01 14:22:12 -05001977 nvme_wait_freeze(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001978 nvme_dev_add(dev);
Keith Busch302ad8c2017-03-01 14:22:12 -05001979 nvme_unfreeze(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001980 }
1981
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02001982 if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) {
1983 dev_warn(dev->ctrl.device, "failed to mark controller live\n");
1984 goto out;
1985 }
Christoph Hellwig92911a52016-04-26 13:51:58 +02001986
1987 if (dev->online_queues > 1)
Christoph Hellwig5955be22016-04-26 13:51:59 +02001988 nvme_queue_scan(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001989 return;
Keith Buschf0b50732013-07-15 15:02:21 -06001990
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001991 out:
Keith Buschf58944e2016-02-24 09:15:55 -07001992 nvme_remove_dead_ctrl(dev, result);
Keith Buschf0b50732013-07-15 15:02:21 -06001993}
1994
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01001995static void nvme_remove_dead_ctrl_work(struct work_struct *work)
Keith Busch9a6b9452013-12-10 13:10:36 -07001996{
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01001997 struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
Christoph Hellwige75ec752015-05-22 11:12:39 +02001998 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07001999
Keith Busch69d9a992016-02-24 09:15:56 -07002000 nvme_kill_queues(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002001 if (pci_get_drvdata(pdev))
Keith Busch921920a2016-03-28 16:03:21 -06002002 device_release_driver(&pdev->dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002003 nvme_put_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002004}
2005
Keith Busch4cc06522015-06-05 10:30:08 -06002006static int nvme_reset(struct nvme_dev *dev)
2007{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002008 if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q))
Keith Busch4cc06522015-06-05 10:30:08 -06002009 return -ENODEV;
Keith Buschc5f6ce92016-10-05 16:32:45 -04002010 if (work_busy(&dev->reset_work))
2011 return -ENODEV;
Christoph Hellwig846cc052015-11-26 12:10:29 +01002012 if (!queue_work(nvme_workq, &dev->reset_work))
2013 return -EBUSY;
Christoph Hellwig846cc052015-11-26 12:10:29 +01002014 return 0;
Keith Busch4cc06522015-06-05 10:30:08 -06002015}
2016
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002017static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
Keith Busch4cc06522015-06-05 10:30:08 -06002018{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002019 *val = readl(to_nvme_dev(ctrl)->bar + off);
2020 return 0;
Keith Busch4cc06522015-06-05 10:30:08 -06002021}
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002022
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002023static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2024{
2025 writel(val, to_nvme_dev(ctrl)->bar + off);
2026 return 0;
2027}
2028
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002029static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2030{
2031 *val = readq(to_nvme_dev(ctrl)->bar + off);
2032 return 0;
2033}
2034
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002035static int nvme_pci_reset_ctrl(struct nvme_ctrl *ctrl)
2036{
Keith Buschc5f6ce92016-10-05 16:32:45 -04002037 struct nvme_dev *dev = to_nvme_dev(ctrl);
2038 int ret = nvme_reset(dev);
2039
2040 if (!ret)
2041 flush_work(&dev->reset_work);
2042 return ret;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002043}
2044
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002045static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
Ming Lin1a353d82016-06-13 16:45:24 +02002046 .name = "pcie",
Sagi Grimberge439bb12016-02-10 10:03:29 -08002047 .module = THIS_MODULE,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002048 .reg_read32 = nvme_pci_reg_read32,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002049 .reg_write32 = nvme_pci_reg_write32,
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002050 .reg_read64 = nvme_pci_reg_read64,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002051 .reset_ctrl = nvme_pci_reset_ctrl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002052 .free_ctrl = nvme_pci_free_ctrl,
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002053 .submit_async_event = nvme_pci_submit_async_event,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002054};
Keith Busch4cc06522015-06-05 10:30:08 -06002055
Keith Buschb00a7262016-02-24 09:15:52 -07002056static int nvme_dev_map(struct nvme_dev *dev)
2057{
Keith Buschb00a7262016-02-24 09:15:52 -07002058 struct pci_dev *pdev = to_pci_dev(dev->dev);
2059
Johannes Thumshirna1f447b2016-06-07 09:44:02 +02002060 if (pci_request_mem_regions(pdev, "nvme"))
Keith Buschb00a7262016-02-24 09:15:52 -07002061 return -ENODEV;
2062
2063 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2064 if (!dev->bar)
2065 goto release;
2066
Max Gurtovoy9fa196e2016-12-19 16:18:24 +02002067 return 0;
Keith Buschb00a7262016-02-24 09:15:52 -07002068 release:
Max Gurtovoy9fa196e2016-12-19 16:18:24 +02002069 pci_release_mem_regions(pdev);
2070 return -ENODEV;
Keith Buschb00a7262016-02-24 09:15:52 -07002071}
2072
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002073static unsigned long check_dell_samsung_bug(struct pci_dev *pdev)
2074{
2075 if (pdev->vendor == 0x144d && pdev->device == 0xa802) {
2076 /*
2077 * Several Samsung devices seem to drop off the PCIe bus
2078 * randomly when APST is on and uses the deepest sleep state.
2079 * This has been observed on a Samsung "SM951 NVMe SAMSUNG
2080 * 256GB", a "PM951 NVMe SAMSUNG 512GB", and a "Samsung SSD
2081 * 950 PRO 256GB", but it seems to be restricted to two Dell
2082 * laptops.
2083 */
2084 if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.") &&
2085 (dmi_match(DMI_PRODUCT_NAME, "XPS 15 9550") ||
2086 dmi_match(DMI_PRODUCT_NAME, "Precision 5510")))
2087 return NVME_QUIRK_NO_DEEPEST_PS;
2088 }
2089
2090 return 0;
2091}
2092
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002093static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002094{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002095 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002096 struct nvme_dev *dev;
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002097 unsigned long quirks = id->driver_data;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002098
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002099 node = dev_to_node(&pdev->dev);
2100 if (node == NUMA_NO_NODE)
Masayoshi Mizuma2fa84352016-06-20 09:33:17 +09002101 set_dev_node(&pdev->dev, first_memory_node);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002102
2103 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002104 if (!dev)
2105 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002106 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2107 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002108 if (!dev->queues)
2109 goto free;
2110
Christoph Hellwige75ec752015-05-22 11:12:39 +02002111 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002112 pci_set_drvdata(pdev, dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002113
Keith Buschb00a7262016-02-24 09:15:52 -07002114 result = nvme_dev_map(dev);
2115 if (result)
2116 goto free;
2117
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002118 INIT_WORK(&dev->reset_work, nvme_reset_work);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002119 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
Christoph Hellwig2d55cd52016-02-29 15:59:46 +01002120 setup_timer(&dev->watchdog_timer, nvme_watchdog_timer,
2121 (unsigned long)dev);
Keith Busch77bf25e2015-11-26 12:21:29 +01002122 mutex_init(&dev->shutdown_lock);
Keith Buschdb3cbff2016-01-12 14:41:17 -07002123 init_completion(&dev->ioq_wait);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002124
2125 result = nvme_setup_prp_pools(dev);
2126 if (result)
2127 goto put_pci;
2128
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002129 quirks |= check_dell_samsung_bug(pdev);
2130
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002131 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002132 quirks);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002133 if (result)
2134 goto release_pools;
2135
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002136 dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev));
2137
Keith Busch92f7a162015-10-23 11:42:02 -06002138 queue_work(nvme_workq, &dev->reset_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002139 return 0;
2140
Keith Busch0877cb02013-07-15 15:02:19 -06002141 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002142 nvme_release_prp_pools(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002143 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002144 put_device(dev->dev);
Keith Buschb00a7262016-02-24 09:15:52 -07002145 nvme_dev_unmap(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002146 free:
2147 kfree(dev->queues);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002148 kfree(dev);
2149 return result;
2150}
2151
Keith Buschf0d54a52014-05-02 10:40:43 -06002152static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2153{
Keith Buscha6739472014-06-23 16:03:21 -06002154 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002155
Keith Buscha6739472014-06-23 16:03:21 -06002156 if (prepare)
Keith Buscha5cdb682016-01-12 14:41:18 -07002157 nvme_dev_disable(dev, false);
Keith Buscha6739472014-06-23 16:03:21 -06002158 else
Keith Buschc5f6ce92016-10-05 16:32:45 -04002159 nvme_reset(dev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002160}
2161
Keith Busch09ece142014-01-27 11:29:40 -05002162static void nvme_shutdown(struct pci_dev *pdev)
2163{
2164 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buscha5cdb682016-01-12 14:41:18 -07002165 nvme_dev_disable(dev, true);
Keith Busch09ece142014-01-27 11:29:40 -05002166}
2167
Keith Buschf58944e2016-02-24 09:15:55 -07002168/*
2169 * The driver's remove may be called on a device in a partially initialized
2170 * state. This function must not have any dependencies on the device state in
2171 * order to proceed.
2172 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002173static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002174{
2175 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002176
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002177 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
2178
Keith Busch9a6b9452013-12-10 13:10:36 -07002179 pci_set_drvdata(pdev, NULL);
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002180
Keith Busch6db28ed2017-02-10 18:15:49 -05002181 if (!pci_device_is_present(pdev)) {
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002182 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD);
Keith Busch6db28ed2017-02-10 18:15:49 -05002183 nvme_dev_disable(dev, false);
2184 }
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002185
Keith Busch9bf2b972016-04-08 16:11:02 -06002186 flush_work(&dev->reset_work);
Keith Busch53029b02015-11-28 15:41:02 +01002187 nvme_uninit_ctrl(&dev->ctrl);
Keith Buscha5cdb682016-01-12 14:41:18 -07002188 nvme_dev_disable(dev, true);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002189 nvme_dev_remove_admin(dev);
2190 nvme_free_queues(dev, 0);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002191 nvme_release_cmb(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002192 nvme_release_prp_pools(dev);
Keith Buschb00a7262016-02-24 09:15:52 -07002193 nvme_dev_unmap(dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002194 nvme_put_ctrl(&dev->ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002195}
2196
Keith Busch13880f52016-06-20 09:41:06 -06002197static int nvme_pci_sriov_configure(struct pci_dev *pdev, int numvfs)
2198{
2199 int ret = 0;
2200
2201 if (numvfs == 0) {
2202 if (pci_vfs_assigned(pdev)) {
2203 dev_warn(&pdev->dev,
2204 "Cannot disable SR-IOV VFs while assigned\n");
2205 return -EPERM;
2206 }
2207 pci_disable_sriov(pdev);
2208 return 0;
2209 }
2210
2211 ret = pci_enable_sriov(pdev, numvfs);
2212 return ret ? ret : numvfs;
2213}
2214
Jingoo Han671a6012014-02-13 11:19:14 +09002215#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002216static int nvme_suspend(struct device *dev)
2217{
2218 struct pci_dev *pdev = to_pci_dev(dev);
2219 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2220
Keith Buscha5cdb682016-01-12 14:41:18 -07002221 nvme_dev_disable(ndev, true);
Keith Buschcd638942013-07-15 15:02:23 -06002222 return 0;
2223}
2224
2225static int nvme_resume(struct device *dev)
2226{
2227 struct pci_dev *pdev = to_pci_dev(dev);
2228 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002229
Keith Buschc5f6ce92016-10-05 16:32:45 -04002230 nvme_reset(ndev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002231 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002232}
Jingoo Han671a6012014-02-13 11:19:14 +09002233#endif
Keith Buschcd638942013-07-15 15:02:23 -06002234
2235static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002236
Keith Buscha0a34082015-12-07 15:30:31 -07002237static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
2238 pci_channel_state_t state)
2239{
2240 struct nvme_dev *dev = pci_get_drvdata(pdev);
2241
2242 /*
2243 * A frozen channel requires a reset. When detected, this method will
2244 * shutdown the controller to quiesce. The controller will be restarted
2245 * after the slot reset through driver's slot_reset callback.
2246 */
Keith Buscha0a34082015-12-07 15:30:31 -07002247 switch (state) {
2248 case pci_channel_io_normal:
2249 return PCI_ERS_RESULT_CAN_RECOVER;
2250 case pci_channel_io_frozen:
Keith Buschd011fb32016-04-04 15:07:41 -06002251 dev_warn(dev->ctrl.device,
2252 "frozen state error detected, reset controller\n");
Keith Buscha5cdb682016-01-12 14:41:18 -07002253 nvme_dev_disable(dev, false);
Keith Buscha0a34082015-12-07 15:30:31 -07002254 return PCI_ERS_RESULT_NEED_RESET;
2255 case pci_channel_io_perm_failure:
Keith Buschd011fb32016-04-04 15:07:41 -06002256 dev_warn(dev->ctrl.device,
2257 "failure state error detected, request disconnect\n");
Keith Buscha0a34082015-12-07 15:30:31 -07002258 return PCI_ERS_RESULT_DISCONNECT;
2259 }
2260 return PCI_ERS_RESULT_NEED_RESET;
2261}
2262
2263static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev)
2264{
2265 struct nvme_dev *dev = pci_get_drvdata(pdev);
2266
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002267 dev_info(dev->ctrl.device, "restart after slot reset\n");
Keith Buscha0a34082015-12-07 15:30:31 -07002268 pci_restore_state(pdev);
Keith Buschc5f6ce92016-10-05 16:32:45 -04002269 nvme_reset(dev);
Keith Buscha0a34082015-12-07 15:30:31 -07002270 return PCI_ERS_RESULT_RECOVERED;
2271}
2272
2273static void nvme_error_resume(struct pci_dev *pdev)
2274{
2275 pci_cleanup_aer_uncorrect_error_status(pdev);
2276}
2277
Stephen Hemminger1d352032012-09-07 09:33:17 -07002278static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002279 .error_detected = nvme_error_detected,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002280 .slot_reset = nvme_slot_reset,
2281 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002282 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002283};
2284
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002285static const struct pci_device_id nvme_id_table[] = {
Christoph Hellwig106198e2015-11-26 10:07:41 +01002286 { PCI_VDEVICE(INTEL, 0x0953),
Keith Busch08095e72016-03-04 13:15:17 -07002287 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002288 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch99466e72016-05-02 15:14:24 -06002289 { PCI_VDEVICE(INTEL, 0x0a53),
2290 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002291 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch99466e72016-05-02 15:14:24 -06002292 { PCI_VDEVICE(INTEL, 0x0a54),
2293 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002294 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch540c8012015-10-22 15:45:06 -06002295 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2296 .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03002297 { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */
2298 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Wenbo Wang015282c2016-09-08 12:12:11 -04002299 { PCI_DEVICE(0x1c5f, 0x0540), /* Memblaze Pblaze4 adapter */
2300 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002301 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01002302 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Daniel Roschka124298b2017-02-22 15:17:29 -07002303 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002304 { 0, }
2305};
2306MODULE_DEVICE_TABLE(pci, nvme_id_table);
2307
2308static struct pci_driver nvme_driver = {
2309 .name = "nvme",
2310 .id_table = nvme_id_table,
2311 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002312 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002313 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002314 .driver = {
2315 .pm = &nvme_dev_pm_ops,
2316 },
Keith Busch13880f52016-06-20 09:41:06 -06002317 .sriov_configure = nvme_pci_sriov_configure,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002318 .err_handler = &nvme_err_handler,
2319};
2320
2321static int __init nvme_init(void)
2322{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002323 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002324
Keith Busch92f7a162015-10-23 11:42:02 -06002325 nvme_workq = alloc_workqueue("nvme", WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
Keith Busch9a6b9452013-12-10 13:10:36 -07002326 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002327 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002328
Keith Buschf3db22f2014-06-11 11:51:35 -06002329 result = pci_register_driver(&nvme_driver);
2330 if (result)
Ming Lin576d55d2016-02-10 10:03:32 -08002331 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002332 return result;
2333}
2334
2335static void __exit nvme_exit(void)
2336{
2337 pci_unregister_driver(&nvme_driver);
Keith Busch9a6b9452013-12-10 13:10:36 -07002338 destroy_workqueue(nvme_workq);
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002339 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002340}
2341
2342MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2343MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002344MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002345module_init(nvme_init);
2346module_exit(nvme_exit);