blob: fed803232edccbdf63139d89028c694efff52409 [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 Wilcoxb60503b2011-01-20 12:50:14 -0500135 spinlock_t q_lock;
136 struct nvme_command *sq_cmds;
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600137 struct nvme_command __iomem *sq_cmds_io;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500138 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600139 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500140 dma_addr_t sq_dma_addr;
141 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500142 u32 __iomem *q_db;
143 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700144 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500145 u16 sq_tail;
146 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700147 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400148 u8 cq_phase;
149 u8 cqe_seen;
Helen Koikef9f38e32017-04-10 12:51:07 -0300150 u32 *dbbuf_sq_db;
151 u32 *dbbuf_cq_db;
152 u32 *dbbuf_sq_ei;
153 u32 *dbbuf_cq_ei;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500154};
155
156/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200157 * The nvme_iod describes the data in an I/O, including the list of PRP
158 * entries. You can't see it in this data structure because C doesn't let
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100159 * me express that. Use nvme_init_iod to ensure there's enough space
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200160 * allocated to store the PRP list.
161 */
162struct nvme_iod {
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800163 struct nvme_request req;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100164 struct nvme_queue *nvmeq;
165 int aborted;
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200166 int npages; /* In the PRP list. 0 means small pool in use */
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200167 int nents; /* Used in scatterlist */
168 int length; /* Of data, in bytes */
169 dma_addr_t first_dma;
Christoph Hellwigbf684052015-10-26 17:12:51 +0900170 struct scatterlist meta_sg; /* metadata requires single contiguous buffer */
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100171 struct scatterlist *sg;
172 struct scatterlist inline_sg[0];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500173};
174
175/*
176 * Check we didin't inadvertently grow the command struct
177 */
178static inline void _nvme_check_size(void)
179{
180 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
181 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
182 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
183 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
184 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400185 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700186 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500187 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
188 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
189 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
190 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600191 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Helen Koikef9f38e32017-04-10 12:51:07 -0300192 BUILD_BUG_ON(sizeof(struct nvme_dbbuf) != 64);
193}
194
195static inline unsigned int nvme_dbbuf_size(u32 stride)
196{
197 return ((num_possible_cpus() + 1) * 8 * stride);
198}
199
200static int nvme_dbbuf_dma_alloc(struct nvme_dev *dev)
201{
202 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
203
204 if (dev->dbbuf_dbs)
205 return 0;
206
207 dev->dbbuf_dbs = dma_alloc_coherent(dev->dev, mem_size,
208 &dev->dbbuf_dbs_dma_addr,
209 GFP_KERNEL);
210 if (!dev->dbbuf_dbs)
211 return -ENOMEM;
212 dev->dbbuf_eis = dma_alloc_coherent(dev->dev, mem_size,
213 &dev->dbbuf_eis_dma_addr,
214 GFP_KERNEL);
215 if (!dev->dbbuf_eis) {
216 dma_free_coherent(dev->dev, mem_size,
217 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
218 dev->dbbuf_dbs = NULL;
219 return -ENOMEM;
220 }
221
222 return 0;
223}
224
225static void nvme_dbbuf_dma_free(struct nvme_dev *dev)
226{
227 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
228
229 if (dev->dbbuf_dbs) {
230 dma_free_coherent(dev->dev, mem_size,
231 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
232 dev->dbbuf_dbs = NULL;
233 }
234 if (dev->dbbuf_eis) {
235 dma_free_coherent(dev->dev, mem_size,
236 dev->dbbuf_eis, dev->dbbuf_eis_dma_addr);
237 dev->dbbuf_eis = NULL;
238 }
239}
240
241static void nvme_dbbuf_init(struct nvme_dev *dev,
242 struct nvme_queue *nvmeq, int qid)
243{
244 if (!dev->dbbuf_dbs || !qid)
245 return;
246
247 nvmeq->dbbuf_sq_db = &dev->dbbuf_dbs[sq_idx(qid, dev->db_stride)];
248 nvmeq->dbbuf_cq_db = &dev->dbbuf_dbs[cq_idx(qid, dev->db_stride)];
249 nvmeq->dbbuf_sq_ei = &dev->dbbuf_eis[sq_idx(qid, dev->db_stride)];
250 nvmeq->dbbuf_cq_ei = &dev->dbbuf_eis[cq_idx(qid, dev->db_stride)];
251}
252
253static void nvme_dbbuf_set(struct nvme_dev *dev)
254{
255 struct nvme_command c;
256
257 if (!dev->dbbuf_dbs)
258 return;
259
260 memset(&c, 0, sizeof(c));
261 c.dbbuf.opcode = nvme_admin_dbbuf;
262 c.dbbuf.prp1 = cpu_to_le64(dev->dbbuf_dbs_dma_addr);
263 c.dbbuf.prp2 = cpu_to_le64(dev->dbbuf_eis_dma_addr);
264
265 if (nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0)) {
266 dev_warn(dev->dev, "unable to set dbbuf\n");
267 /* Free memory and continue on */
268 nvme_dbbuf_dma_free(dev);
269 }
270}
271
272static inline int nvme_dbbuf_need_event(u16 event_idx, u16 new_idx, u16 old)
273{
274 return (u16)(new_idx - event_idx - 1) < (u16)(new_idx - old);
275}
276
277/* Update dbbuf and return true if an MMIO is required */
278static bool nvme_dbbuf_update_and_check_event(u16 value, u32 *dbbuf_db,
279 volatile u32 *dbbuf_ei)
280{
281 if (dbbuf_db) {
282 u16 old_value;
283
284 /*
285 * Ensure that the queue is written before updating
286 * the doorbell in memory
287 */
288 wmb();
289
290 old_value = *dbbuf_db;
291 *dbbuf_db = value;
292
293 if (!nvme_dbbuf_need_event(*dbbuf_ei, value, old_value))
294 return false;
295 }
296
297 return true;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500298}
299
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700300/*
301 * Max size of iod being embedded in the request payload
302 */
303#define NVME_INT_PAGES 2
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100304#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700305
306/*
307 * Will slightly overestimate the number of pages needed. This is OK
308 * as it only leads to a small amount of wasted memory for the lifetime of
309 * the I/O.
310 */
311static int nvme_npages(unsigned size, struct nvme_dev *dev)
312{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100313 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
314 dev->ctrl.page_size);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700315 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
316}
317
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100318static unsigned int nvme_iod_alloc_size(struct nvme_dev *dev,
319 unsigned int size, unsigned int nseg)
320{
321 return sizeof(__le64 *) * nvme_npages(size, dev) +
322 sizeof(struct scatterlist) * nseg;
323}
324
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700325static unsigned int nvme_cmd_size(struct nvme_dev *dev)
326{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100327 return sizeof(struct nvme_iod) +
328 nvme_iod_alloc_size(dev, NVME_INT_BYTES(dev), NVME_INT_PAGES);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700329}
330
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700331static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
332 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500333{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700334 struct nvme_dev *dev = data;
335 struct nvme_queue *nvmeq = dev->queues[0];
336
Keith Busch42483222015-06-01 09:29:54 -0600337 WARN_ON(hctx_idx != 0);
338 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
339 WARN_ON(nvmeq->tags);
340
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700341 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600342 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700343 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500344}
345
Keith Busch4af0e212015-06-08 10:08:13 -0600346static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
347{
348 struct nvme_queue *nvmeq = hctx->driver_data;
349
350 nvmeq->tags = NULL;
351}
352
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600353static int nvme_admin_init_request(struct blk_mq_tag_set *set,
354 struct request *req, unsigned int hctx_idx,
355 unsigned int numa_node)
Keith Busch22404272013-07-15 15:02:20 -0600356{
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600357 struct nvme_dev *dev = set->driver_data;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100358 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700359 struct nvme_queue *nvmeq = dev->queues[0];
360
361 BUG_ON(!nvmeq);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100362 iod->nvmeq = nvmeq;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700363 return 0;
Keith Busch22404272013-07-15 15:02:20 -0600364}
365
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700366static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
367 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500368{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700369 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600370 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500371
Keith Busch42483222015-06-01 09:29:54 -0600372 if (!nvmeq->tags)
373 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500374
Keith Busch42483222015-06-01 09:29:54 -0600375 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700376 hctx->driver_data = nvmeq;
377 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500378}
379
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600380static int nvme_init_request(struct blk_mq_tag_set *set, struct request *req,
381 unsigned int hctx_idx, unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500382{
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600383 struct nvme_dev *dev = set->driver_data;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100384 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700385 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
386
387 BUG_ON(!nvmeq);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100388 iod->nvmeq = nvmeq;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700389 return 0;
390}
391
Christoph Hellwigdca51e72016-09-14 16:18:57 +0200392static int nvme_pci_map_queues(struct blk_mq_tag_set *set)
393{
394 struct nvme_dev *dev = set->driver_data;
395
396 return blk_mq_pci_map_queues(set, to_pci_dev(dev->dev));
397}
398
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500399/**
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100400 * __nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500401 * @nvmeq: The queue to use
402 * @cmd: The command to send
403 *
404 * Safe to use from interrupt context
405 */
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530406static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
407 struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500408{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700409 u16 tail = nvmeq->sq_tail;
410
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600411 if (nvmeq->sq_cmds_io)
412 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
413 else
414 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
415
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500416 if (++tail == nvmeq->q_depth)
417 tail = 0;
Helen Koikef9f38e32017-04-10 12:51:07 -0300418 if (nvme_dbbuf_update_and_check_event(tail, nvmeq->dbbuf_sq_db,
419 nvmeq->dbbuf_sq_ei))
420 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500421 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500422}
423
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100424static __le64 **iod_list(struct request *req)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700425{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100426 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700427 return (__le64 **)(iod->sg + blk_rq_nr_phys_segments(req));
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700428}
429
Christoph Hellwigb131c612017-01-13 12:29:12 +0100430static int nvme_init_iod(struct request *rq, struct nvme_dev *dev)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500431{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100432 struct nvme_iod *iod = blk_mq_rq_to_pdu(rq);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700433 int nseg = blk_rq_nr_phys_segments(rq);
Christoph Hellwigb131c612017-01-13 12:29:12 +0100434 unsigned int size = blk_rq_payload_bytes(rq);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500435
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100436 if (nseg > NVME_INT_PAGES || size > NVME_INT_BYTES(dev)) {
437 iod->sg = kmalloc(nvme_iod_alloc_size(dev, size, nseg), GFP_ATOMIC);
438 if (!iod->sg)
439 return BLK_MQ_RQ_QUEUE_BUSY;
440 } else {
441 iod->sg = iod->inline_sg;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700442 }
443
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100444 iod->aborted = 0;
445 iod->npages = -1;
446 iod->nents = 0;
447 iod->length = size;
Keith Buschf80ec962016-07-12 16:20:31 -0700448
Omar Sandovalbac00002016-11-15 11:11:58 -0800449 return BLK_MQ_RQ_QUEUE_OK;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700450}
451
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100452static void nvme_free_iod(struct nvme_dev *dev, struct request *req)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500453{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100454 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100455 const int last_prp = dev->ctrl.page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500456 int i;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100457 __le64 **list = iod_list(req);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500458 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500459
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500460 if (iod->npages == 0)
461 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
462 for (i = 0; i < iod->npages; i++) {
463 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500464 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500465 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500466 prp_dma = next_prp_dma;
467 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700468
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100469 if (iod->sg != iod->inline_sg)
470 kfree(iod->sg);
Keith Buschb4ff9c82014-08-29 09:06:12 -0600471}
472
Keith Busch52b68d72015-02-23 09:16:21 -0700473#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700474static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
475{
476 if (be32_to_cpu(pi->ref_tag) == v)
477 pi->ref_tag = cpu_to_be32(p);
478}
479
480static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
481{
482 if (be32_to_cpu(pi->ref_tag) == p)
483 pi->ref_tag = cpu_to_be32(v);
484}
485
486/**
487 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
488 *
489 * The virtual start sector is the one that was originally submitted by the
490 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
491 * start sector may be different. Remap protection information to match the
492 * physical LBA on writes, and back to the original seed on reads.
493 *
494 * Type 0 and 3 do not have a ref tag, so no remapping required.
495 */
496static void nvme_dif_remap(struct request *req,
497 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
498{
499 struct nvme_ns *ns = req->rq_disk->private_data;
500 struct bio_integrity_payload *bip;
501 struct t10_pi_tuple *pi;
502 void *p, *pmap;
503 u32 i, nlb, ts, phys, virt;
504
505 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
506 return;
507
508 bip = bio_integrity(req->bio);
509 if (!bip)
510 return;
511
512 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700513
514 p = pmap;
515 virt = bip_get_seed(bip);
516 phys = nvme_block_nr(ns, blk_rq_pos(req));
517 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
Dan Williamsac6fc482015-10-21 13:20:18 -0400518 ts = ns->disk->queue->integrity.tuple_size;
Keith Busche1e5e562015-02-19 13:39:03 -0700519
520 for (i = 0; i < nlb; i++, virt++, phys++) {
521 pi = (struct t10_pi_tuple *)p;
522 dif_swap(phys, virt, pi);
523 p += ts;
524 }
525 kunmap_atomic(pmap);
526}
Keith Busch52b68d72015-02-23 09:16:21 -0700527#else /* CONFIG_BLK_DEV_INTEGRITY */
528static void nvme_dif_remap(struct request *req,
529 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
530{
531}
532static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
533{
534}
535static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
536{
537}
Keith Busch52b68d72015-02-23 09:16:21 -0700538#endif
539
Christoph Hellwigb131c612017-01-13 12:29:12 +0100540static bool nvme_setup_prps(struct nvme_dev *dev, struct request *req)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500541{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100542 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500543 struct dma_pool *pool;
Christoph Hellwigb131c612017-01-13 12:29:12 +0100544 int length = blk_rq_payload_bytes(req);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500545 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500546 int dma_len = sg_dma_len(sg);
547 u64 dma_addr = sg_dma_address(sg);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100548 u32 page_size = dev->ctrl.page_size;
Murali Iyerf137e0f2015-03-26 11:07:51 -0500549 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500550 __le64 *prp_list;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100551 __le64 **list = iod_list(req);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500552 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500553 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500554
Keith Busch1d090622014-06-23 11:34:01 -0600555 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500556 if (length <= 0)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200557 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500558
Keith Busch1d090622014-06-23 11:34:01 -0600559 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500560 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600561 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500562 } else {
563 sg = sg_next(sg);
564 dma_addr = sg_dma_address(sg);
565 dma_len = sg_dma_len(sg);
566 }
567
Keith Busch1d090622014-06-23 11:34:01 -0600568 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600569 iod->first_dma = dma_addr;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200570 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500571 }
572
Keith Busch1d090622014-06-23 11:34:01 -0600573 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500574 if (nprps <= (256 / 8)) {
575 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500576 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500577 } else {
578 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500579 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500580 }
581
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200582 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400583 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600584 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500585 iod->npages = -1;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200586 return false;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400587 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500588 list[0] = prp_list;
589 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500590 i = 0;
591 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600592 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500593 __le64 *old_prp_list = prp_list;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200594 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500595 if (!prp_list)
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200596 return false;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500597 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400598 prp_list[0] = old_prp_list[i - 1];
599 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
600 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500601 }
602 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600603 dma_len -= page_size;
604 dma_addr += page_size;
605 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500606 if (length <= 0)
607 break;
608 if (dma_len > 0)
609 continue;
610 BUG_ON(dma_len < 0);
611 sg = sg_next(sg);
612 dma_addr = sg_dma_address(sg);
613 dma_len = sg_dma_len(sg);
614 }
615
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200616 return true;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500617}
618
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100619static int nvme_map_data(struct nvme_dev *dev, struct request *req,
Christoph Hellwigb131c612017-01-13 12:29:12 +0100620 struct nvme_command *cmnd)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200621{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100622 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200623 struct request_queue *q = req->q;
624 enum dma_data_direction dma_dir = rq_data_dir(req) ?
625 DMA_TO_DEVICE : DMA_FROM_DEVICE;
626 int ret = BLK_MQ_RQ_QUEUE_ERROR;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200627
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700628 sg_init_table(iod->sg, blk_rq_nr_phys_segments(req));
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200629 iod->nents = blk_rq_map_sg(q, req, iod->sg);
630 if (!iod->nents)
631 goto out;
632
633 ret = BLK_MQ_RQ_QUEUE_BUSY;
Mauricio Faria de Oliveira2b6b5352016-10-11 13:54:20 -0700634 if (!dma_map_sg_attrs(dev->dev, iod->sg, iod->nents, dma_dir,
635 DMA_ATTR_NO_WARN))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200636 goto out;
637
Christoph Hellwigb131c612017-01-13 12:29:12 +0100638 if (!nvme_setup_prps(dev, req))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200639 goto out_unmap;
640
641 ret = BLK_MQ_RQ_QUEUE_ERROR;
642 if (blk_integrity_rq(req)) {
643 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
644 goto out_unmap;
645
Christoph Hellwigbf684052015-10-26 17:12:51 +0900646 sg_init_table(&iod->meta_sg, 1);
647 if (blk_rq_map_integrity_sg(q, req->bio, &iod->meta_sg) != 1)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200648 goto out_unmap;
649
650 if (rq_data_dir(req))
651 nvme_dif_remap(req, nvme_dif_prep);
652
Christoph Hellwigbf684052015-10-26 17:12:51 +0900653 if (!dma_map_sg(dev->dev, &iod->meta_sg, 1, dma_dir))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200654 goto out_unmap;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200655 }
656
Christoph Hellwigeb793e22016-06-13 16:45:25 +0200657 cmnd->rw.dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
658 cmnd->rw.dptr.prp2 = cpu_to_le64(iod->first_dma);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200659 if (blk_integrity_rq(req))
Christoph Hellwigbf684052015-10-26 17:12:51 +0900660 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(&iod->meta_sg));
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200661 return BLK_MQ_RQ_QUEUE_OK;
662
663out_unmap:
664 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
665out:
666 return ret;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200667}
668
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100669static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100670{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100671 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100672 enum dma_data_direction dma_dir = rq_data_dir(req) ?
673 DMA_TO_DEVICE : DMA_FROM_DEVICE;
674
675 if (iod->nents) {
676 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
677 if (blk_integrity_rq(req)) {
678 if (!rq_data_dir(req))
679 nvme_dif_remap(req, nvme_dif_complete);
Christoph Hellwigbf684052015-10-26 17:12:51 +0900680 dma_unmap_sg(dev->dev, &iod->meta_sg, 1, dma_dir);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100681 }
682 }
683
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700684 nvme_cleanup_cmd(req);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100685 nvme_free_iod(dev, req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500686}
687
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700688/*
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200689 * NOTE: ns is NULL when called on the admin queue.
690 */
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700691static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
692 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600693{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700694 struct nvme_ns *ns = hctx->queue->queuedata;
695 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200696 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700697 struct request *req = bd->rq;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200698 struct nvme_command cmnd;
699 int ret = BLK_MQ_RQ_QUEUE_OK;
Keith Buschedd10d32014-04-03 16:45:23 -0600700
Keith Busche1e5e562015-02-19 13:39:03 -0700701 /*
702 * If formated with metadata, require the block layer provide a buffer
703 * unless this namespace is formated such that the metadata can be
704 * stripped/generated by the controller with PRACT=1.
705 */
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200706 if (ns && ns->ms && !blk_integrity_rq(req)) {
Keith Busch71feb362015-06-19 11:07:30 -0600707 if (!(ns->pi_type && ns->ms == 8) &&
Christoph Hellwig57292b52017-01-31 16:57:29 +0100708 !blk_rq_is_passthrough(req)) {
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100709 blk_mq_end_request(req, -EFAULT);
Keith Busche1e5e562015-02-19 13:39:03 -0700710 return BLK_MQ_RQ_QUEUE_OK;
711 }
712 }
713
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700714 ret = nvme_setup_cmd(ns, req, &cmnd);
Omar Sandovalbac00002016-11-15 11:11:58 -0800715 if (ret != BLK_MQ_RQ_QUEUE_OK)
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100716 return ret;
Keith Buschedd10d32014-04-03 16:45:23 -0600717
Christoph Hellwigb131c612017-01-13 12:29:12 +0100718 ret = nvme_init_iod(req, dev);
Omar Sandovalbac00002016-11-15 11:11:58 -0800719 if (ret != BLK_MQ_RQ_QUEUE_OK)
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700720 goto out_free_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600721
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700722 if (blk_rq_nr_phys_segments(req))
Christoph Hellwigb131c612017-01-13 12:29:12 +0100723 ret = nvme_map_data(dev, req, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700724
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_cleanup_iod;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700727
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100728 blk_mq_start_request(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200729
730 spin_lock_irq(&nvmeq->q_lock);
Keith Buschae1fba22016-02-11 13:05:42 -0700731 if (unlikely(nvmeq->cq_vector < 0)) {
Keith Busch9ef39322017-02-10 18:15:52 -0500732 ret = BLK_MQ_RQ_QUEUE_ERROR;
Keith Buschae1fba22016-02-11 13:05:42 -0700733 spin_unlock_irq(&nvmeq->q_lock);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700734 goto out_cleanup_iod;
Keith Buschae1fba22016-02-11 13:05:42 -0700735 }
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200736 __nvme_submit_cmd(nvmeq, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700737 nvme_process_cq(nvmeq);
738 spin_unlock_irq(&nvmeq->q_lock);
739 return BLK_MQ_RQ_QUEUE_OK;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700740out_cleanup_iod:
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100741 nvme_free_iod(dev, req);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700742out_free_cmd:
743 nvme_cleanup_cmd(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200744 return ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500745}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500746
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200747static void nvme_pci_complete_rq(struct request *req)
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100748{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100749 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100750
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200751 nvme_unmap_data(iod->nvmeq->dev, req);
752 nvme_complete_rq(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500753}
754
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100755/* We read the CQE phase first to check if the rest of the entry is valid */
756static inline bool nvme_cqe_valid(struct nvme_queue *nvmeq, u16 head,
757 u16 phase)
758{
759 return (le16_to_cpu(nvmeq->cqes[head].status) & 1) == phase;
760}
761
Jens Axboea0fa9642015-11-03 20:37:26 -0700762static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500763{
Matthew Wilcox82123462011-01-20 13:24:06 -0500764 u16 head, phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500765
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500766 head = nvmeq->cq_head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500767 phase = nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500768
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100769 while (nvme_cqe_valid(nvmeq, head, phase)) {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500770 struct nvme_completion cqe = nvmeq->cqes[head];
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100771 struct request *req;
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100772
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500773 if (++head == nvmeq->q_depth) {
774 head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -0500775 phase = !phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500776 }
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100777
Jens Axboea0fa9642015-11-03 20:37:26 -0700778 if (tag && *tag == cqe.command_id)
779 *tag = -1;
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100780
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100781 if (unlikely(cqe.command_id >= nvmeq->q_depth)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -0700782 dev_warn(nvmeq->dev->ctrl.device,
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100783 "invalid id %d completed on queue %d\n",
784 cqe.command_id, le16_to_cpu(cqe.sq_id));
785 continue;
786 }
787
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100788 /*
789 * AEN requests are special as they don't time out and can
790 * survive any kind of queue freeze and often don't respond to
791 * aborts. We don't even bother to allocate a struct request
792 * for them but rather special case them here.
793 */
794 if (unlikely(nvmeq->qid == 0 &&
795 cqe.command_id >= NVME_AQ_BLKMQ_DEPTH)) {
Christoph Hellwig7bf58532016-11-10 07:32:34 -0800796 nvme_complete_async_event(&nvmeq->dev->ctrl,
797 cqe.status, &cqe.result);
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100798 continue;
799 }
800
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100801 req = blk_mq_tag_to_rq(*nvmeq->tags, cqe.command_id);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200802 nvme_end_request(req, cqe.status, cqe.result);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500803 }
804
Matthew Wilcox82123462011-01-20 13:24:06 -0500805 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
Jens Axboea0fa9642015-11-03 20:37:26 -0700806 return;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500807
Keith Busch604e8c82015-11-20 08:38:13 -0700808 if (likely(nvmeq->cq_vector >= 0))
Helen Koikef9f38e32017-04-10 12:51:07 -0300809 if (nvme_dbbuf_update_and_check_event(head, nvmeq->dbbuf_cq_db,
810 nvmeq->dbbuf_cq_ei))
811 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500812 nvmeq->cq_head = head;
Matthew Wilcox82123462011-01-20 13:24:06 -0500813 nvmeq->cq_phase = phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500814
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400815 nvmeq->cqe_seen = 1;
Jens Axboea0fa9642015-11-03 20:37:26 -0700816}
817
818static void nvme_process_cq(struct nvme_queue *nvmeq)
819{
820 __nvme_process_cq(nvmeq, NULL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500821}
822
823static irqreturn_t nvme_irq(int irq, void *data)
824{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500825 irqreturn_t result;
826 struct nvme_queue *nvmeq = data;
827 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400828 nvme_process_cq(nvmeq);
829 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
830 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500831 spin_unlock(&nvmeq->q_lock);
832 return result;
833}
834
835static irqreturn_t nvme_irq_check(int irq, void *data)
836{
837 struct nvme_queue *nvmeq = data;
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100838 if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase))
839 return IRQ_WAKE_THREAD;
840 return IRQ_NONE;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500841}
842
Keith Busch7776db12017-02-24 17:59:28 -0500843static int __nvme_poll(struct nvme_queue *nvmeq, unsigned int tag)
Jens Axboea0fa9642015-11-03 20:37:26 -0700844{
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100845 if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase)) {
Jens Axboea0fa9642015-11-03 20:37:26 -0700846 spin_lock_irq(&nvmeq->q_lock);
847 __nvme_process_cq(nvmeq, &tag);
848 spin_unlock_irq(&nvmeq->q_lock);
849
850 if (tag == -1)
851 return 1;
852 }
853
854 return 0;
855}
856
Keith Busch7776db12017-02-24 17:59:28 -0500857static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
858{
859 struct nvme_queue *nvmeq = hctx->driver_data;
860
861 return __nvme_poll(nvmeq, tag);
862}
863
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200864static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl, int aer_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500865{
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200866 struct nvme_dev *dev = to_nvme_dev(ctrl);
Christoph Hellwig9396dec2016-02-29 15:59:44 +0100867 struct nvme_queue *nvmeq = dev->queues[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700868 struct nvme_command c;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700869
870 memset(&c, 0, sizeof(c));
871 c.common.opcode = nvme_admin_async_event;
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200872 c.common.command_id = NVME_AQ_BLKMQ_DEPTH + aer_idx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700873
Christoph Hellwig9396dec2016-02-29 15:59:44 +0100874 spin_lock_irq(&nvmeq->q_lock);
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200875 __nvme_submit_cmd(nvmeq, &c);
Christoph Hellwig9396dec2016-02-29 15:59:44 +0100876 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -0700877}
878
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500879static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
880{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500881 struct nvme_command c;
882
883 memset(&c, 0, sizeof(c));
884 c.delete_queue.opcode = opcode;
885 c.delete_queue.qid = cpu_to_le16(id);
886
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100887 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500888}
889
890static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
891 struct nvme_queue *nvmeq)
892{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500893 struct nvme_command c;
894 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
895
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200896 /*
897 * Note: we (ab)use the fact the the prp fields survive if no data
898 * is attached to the request.
899 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500900 memset(&c, 0, sizeof(c));
901 c.create_cq.opcode = nvme_admin_create_cq;
902 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
903 c.create_cq.cqid = cpu_to_le16(qid);
904 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
905 c.create_cq.cq_flags = cpu_to_le16(flags);
906 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
907
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100908 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500909}
910
911static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
912 struct nvme_queue *nvmeq)
913{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500914 struct nvme_command c;
Keith Busch81c1cd92017-04-04 18:18:12 -0400915 int flags = NVME_QUEUE_PHYS_CONTIG;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500916
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200917 /*
918 * Note: we (ab)use the fact the the prp fields survive if no data
919 * is attached to the request.
920 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500921 memset(&c, 0, sizeof(c));
922 c.create_sq.opcode = nvme_admin_create_sq;
923 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
924 c.create_sq.sqid = cpu_to_le16(qid);
925 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
926 c.create_sq.sq_flags = cpu_to_le16(flags);
927 c.create_sq.cqid = cpu_to_le16(qid);
928
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100929 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500930}
931
932static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
933{
934 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
935}
936
937static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
938{
939 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
940}
941
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100942static void abort_endio(struct request *req, int error)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400943{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100944 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
945 struct nvme_queue *nvmeq = iod->nvmeq;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400946
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200947 dev_warn(nvmeq->dev->ctrl.device,
948 "Abort status: 0x%x", nvme_req(req)->status);
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100949 atomic_inc(&nvmeq->dev->ctrl.abort_limit);
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100950 blk_mq_free_request(req);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200951}
952
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +0200953static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200954{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100955 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
956 struct nvme_queue *nvmeq = iod->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -0700957 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700958 struct request *abort_req;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700959 struct nvme_command cmd;
Keith Buschc30341d2013-12-10 13:10:38 -0700960
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +0200961 /*
Keith Busch7776db12017-02-24 17:59:28 -0500962 * Did we miss an interrupt?
963 */
964 if (__nvme_poll(nvmeq, req->tag)) {
965 dev_warn(dev->ctrl.device,
966 "I/O %d QID %d timeout, completion polled\n",
967 req->tag, nvmeq->qid);
968 return BLK_EH_HANDLED;
969 }
970
971 /*
Christoph Hellwigfd634f412015-11-26 12:42:26 +0100972 * Shutdown immediately if controller times out while starting. The
973 * reset work will see the pci device disabled when it gets the forced
974 * cancellation error. All outstanding requests are completed on
975 * shutdown, so we return BLK_EH_HANDLED.
976 */
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200977 if (dev->ctrl.state == NVME_CTRL_RESETTING) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -0700978 dev_warn(dev->ctrl.device,
Christoph Hellwigfd634f412015-11-26 12:42:26 +0100979 "I/O %d QID %d timeout, disable controller\n",
980 req->tag, nvmeq->qid);
Keith Buscha5cdb682016-01-12 14:41:18 -0700981 nvme_dev_disable(dev, false);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200982 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
Christoph Hellwigfd634f412015-11-26 12:42:26 +0100983 return BLK_EH_HANDLED;
Keith Buschc30341d2013-12-10 13:10:38 -0700984 }
985
Christoph Hellwigfd634f412015-11-26 12:42:26 +0100986 /*
987 * Shutdown the controller immediately and schedule a reset if the
988 * command was already aborted once before and still hasn't been
989 * returned to the driver, or if this is the admin queue.
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +0200990 */
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100991 if (!nvmeq->qid || iod->aborted) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -0700992 dev_warn(dev->ctrl.device,
Keith Busche1569a12015-11-26 12:11:07 +0100993 "I/O %d QID %d timeout, reset controller\n",
994 req->tag, nvmeq->qid);
Keith Buscha5cdb682016-01-12 14:41:18 -0700995 nvme_dev_disable(dev, false);
Keith Buschc5f6ce92016-10-05 16:32:45 -0400996 nvme_reset(dev);
Keith Buschc30341d2013-12-10 13:10:38 -0700997
Keith Busche1569a12015-11-26 12:11:07 +0100998 /*
999 * Mark the request as handled, since the inline shutdown
1000 * forces all outstanding requests to complete.
1001 */
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001002 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
Keith Busche1569a12015-11-26 12:11:07 +01001003 return BLK_EH_HANDLED;
Keith Buschc30341d2013-12-10 13:10:38 -07001004 }
Keith Buschc30341d2013-12-10 13:10:38 -07001005
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001006 if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) {
1007 atomic_inc(&dev->ctrl.abort_limit);
1008 return BLK_EH_RESET_TIMER;
1009 }
Keith Busch7bf7d772017-01-24 18:07:00 -05001010 iod->aborted = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001011
Keith Buschc30341d2013-12-10 13:10:38 -07001012 memset(&cmd, 0, sizeof(cmd));
1013 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001014 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001015 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Keith Buschc30341d2013-12-10 13:10:38 -07001016
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001017 dev_warn(nvmeq->dev->ctrl.device,
1018 "I/O %d QID %d timeout, aborting\n",
1019 req->tag, nvmeq->qid);
Keith Buschc30341d2013-12-10 13:10:38 -07001020
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001021 abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd,
Christoph Hellwigeb71f432016-06-13 16:45:23 +02001022 BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001023 if (IS_ERR(abort_req)) {
1024 atomic_inc(&dev->ctrl.abort_limit);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001025 return BLK_EH_RESET_TIMER;
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001026 }
Keith Buschc30341d2013-12-10 13:10:38 -07001027
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001028 abort_req->timeout = ADMIN_TIMEOUT;
1029 abort_req->end_io_data = NULL;
1030 blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio);
Keith Busch07836e62015-02-19 10:34:48 -07001031
Keith Busch7a509a62015-01-07 18:55:53 -07001032 /*
1033 * The aborted req will be completed on receiving the abort req.
1034 * We enable the timer again. If hit twice, it'll cause a device reset,
1035 * as the device then is in a faulty state.
1036 */
Keith Busch07836e62015-02-19 10:34:48 -07001037 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001038}
1039
Keith Buschf435c282014-07-07 09:14:42 -06001040static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001041{
1042 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1043 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001044 if (nvmeq->sq_cmds)
1045 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001046 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1047 kfree(nvmeq);
1048}
1049
Keith Buscha1a5ef92013-12-16 13:50:00 -05001050static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001051{
1052 int i;
1053
Keith Buscha1a5ef92013-12-16 13:50:00 -05001054 for (i = dev->queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001055 struct nvme_queue *nvmeq = dev->queues[i];
Keith Busch22404272013-07-15 15:02:20 -06001056 dev->queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001057 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001058 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001059 }
Keith Busch22404272013-07-15 15:02:20 -06001060}
1061
Keith Busch4d115422013-12-10 13:10:40 -07001062/**
1063 * nvme_suspend_queue - put queue into suspended state
1064 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001065 */
1066static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001067{
Keith Busch2b25d982014-12-22 12:59:04 -07001068 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001069
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001070 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001071 if (nvmeq->cq_vector == -1) {
1072 spin_unlock_irq(&nvmeq->q_lock);
1073 return 1;
1074 }
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001075 vector = nvmeq->cq_vector;
Keith Busch42f61422014-03-24 10:46:25 -06001076 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001077 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001078 spin_unlock_irq(&nvmeq->q_lock);
1079
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001080 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
Keith Busch25646262016-01-04 09:10:57 -07001081 blk_mq_stop_hw_queues(nvmeq->dev->ctrl.admin_q);
Keith Busch6df3dbc2015-03-26 13:49:33 -06001082
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001083 pci_free_irq(to_pci_dev(nvmeq->dev->dev), vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001084
Keith Busch4d115422013-12-10 13:10:40 -07001085 return 0;
1086}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001087
Keith Buscha5cdb682016-01-12 14:41:18 -07001088static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown)
Keith Busch4d115422013-12-10 13:10:40 -07001089{
Keith Buscha5cdb682016-01-12 14:41:18 -07001090 struct nvme_queue *nvmeq = dev->queues[0];
Keith Busch4d115422013-12-10 13:10:40 -07001091
1092 if (!nvmeq)
1093 return;
1094 if (nvme_suspend_queue(nvmeq))
1095 return;
1096
Keith Buscha5cdb682016-01-12 14:41:18 -07001097 if (shutdown)
1098 nvme_shutdown_ctrl(&dev->ctrl);
1099 else
1100 nvme_disable_ctrl(&dev->ctrl, lo_hi_readq(
1101 dev->bar + NVME_REG_CAP));
Keith Busch07836e62015-02-19 10:34:48 -07001102
1103 spin_lock_irq(&nvmeq->q_lock);
1104 nvme_process_cq(nvmeq);
1105 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001106}
1107
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001108static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1109 int entry_size)
1110{
1111 int q_depth = dev->q_depth;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001112 unsigned q_size_aligned = roundup(q_depth * entry_size,
1113 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001114
1115 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001116 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001117 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
Jon Derrickc45f5c92015-07-21 15:08:13 -06001118 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001119
1120 /*
1121 * Ensure the reduced q_depth is above some threshold where it
1122 * would be better to map queues in system memory with the
1123 * original depth
1124 */
1125 if (q_depth < 64)
1126 return -ENOMEM;
1127 }
1128
1129 return q_depth;
1130}
1131
1132static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1133 int qid, int depth)
1134{
1135 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001136 unsigned offset = (qid - 1) * roundup(SQ_SIZE(depth),
1137 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001138 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1139 nvmeq->sq_cmds_io = dev->cmb + offset;
1140 } else {
1141 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1142 &nvmeq->sq_dma_addr, GFP_KERNEL);
1143 if (!nvmeq->sq_cmds)
1144 return -ENOMEM;
1145 }
1146
1147 return 0;
1148}
1149
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001150static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001151 int depth, int node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001152{
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001153 struct nvme_queue *nvmeq = kzalloc_node(sizeof(*nvmeq), GFP_KERNEL,
1154 node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001155 if (!nvmeq)
1156 return NULL;
1157
Christoph Hellwige75ec752015-05-22 11:12:39 +02001158 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001159 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001160 if (!nvmeq->cqes)
1161 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001162
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001163 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001164 goto free_cqdma;
1165
Christoph Hellwige75ec752015-05-22 11:12:39 +02001166 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001167 nvmeq->dev = dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001168 spin_lock_init(&nvmeq->q_lock);
1169 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001170 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001171 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001172 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001173 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001174 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001175 dev->queues[qid] = nvmeq;
Jon Derrick36a7e992015-05-27 12:26:23 -06001176 dev->queue_count++;
1177
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001178 return nvmeq;
1179
1180 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001181 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001182 nvmeq->cq_dma_addr);
1183 free_nvmeq:
1184 kfree(nvmeq);
1185 return NULL;
1186}
1187
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001188static int queue_request_irq(struct nvme_queue *nvmeq)
Matthew Wilcox30010822011-01-20 09:10:15 -05001189{
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001190 struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev);
1191 int nr = nvmeq->dev->ctrl.instance;
1192
1193 if (use_threaded_interrupts) {
1194 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq_check,
1195 nvme_irq, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1196 } else {
1197 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq,
1198 NULL, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1199 }
Matthew Wilcox30010822011-01-20 09:10:15 -05001200}
1201
Keith Busch22404272013-07-15 15:02:20 -06001202static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001203{
Keith Busch22404272013-07-15 15:02:20 -06001204 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001205
Keith Busch7be50e92014-09-10 15:48:47 -06001206 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001207 nvmeq->sq_tail = 0;
1208 nvmeq->cq_head = 0;
1209 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001210 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001211 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Helen Koikef9f38e32017-04-10 12:51:07 -03001212 nvme_dbbuf_init(dev, nvmeq, qid);
Keith Busch42f61422014-03-24 10:46:25 -06001213 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001214 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001215}
1216
1217static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1218{
1219 struct nvme_dev *dev = nvmeq->dev;
1220 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001221
Keith Busch2b25d982014-12-22 12:59:04 -07001222 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001223 result = adapter_alloc_cq(dev, qid, nvmeq);
1224 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001225 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001226
1227 result = adapter_alloc_sq(dev, qid, nvmeq);
1228 if (result < 0)
1229 goto release_cq;
1230
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001231 result = queue_request_irq(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001232 if (result < 0)
1233 goto release_sq;
1234
Keith Busch22404272013-07-15 15:02:20 -06001235 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001236 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001237
1238 release_sq:
1239 adapter_delete_sq(dev, qid);
1240 release_cq:
1241 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001242 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001243}
1244
Eric Biggersf363b082017-03-30 13:39:16 -07001245static const struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001246 .queue_rq = nvme_queue_rq,
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001247 .complete = nvme_pci_complete_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001248 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001249 .exit_hctx = nvme_admin_exit_hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001250 .init_request = nvme_admin_init_request,
1251 .timeout = nvme_timeout,
1252};
1253
Eric Biggersf363b082017-03-30 13:39:16 -07001254static const struct blk_mq_ops nvme_mq_ops = {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001255 .queue_rq = nvme_queue_rq,
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001256 .complete = nvme_pci_complete_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001257 .init_hctx = nvme_init_hctx,
1258 .init_request = nvme_init_request,
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001259 .map_queues = nvme_pci_map_queues,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001260 .timeout = nvme_timeout,
Jens Axboea0fa9642015-11-03 20:37:26 -07001261 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001262};
1263
Keith Buschea191d22015-01-07 18:55:49 -07001264static void nvme_dev_remove_admin(struct nvme_dev *dev)
1265{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001266 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
Keith Busch69d9a992016-02-24 09:15:56 -07001267 /*
1268 * If the controller was reset during removal, it's possible
1269 * user requests may be waiting on a stopped queue. Start the
1270 * queue to flush these to completion.
1271 */
1272 blk_mq_start_stopped_hw_queues(dev->ctrl.admin_q, true);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001273 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001274 blk_mq_free_tag_set(&dev->admin_tagset);
1275 }
1276}
1277
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001278static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1279{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001280 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001281 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1282 dev->admin_tagset.nr_hw_queues = 1;
Keith Busche3e9d502016-01-04 09:10:55 -07001283
1284 /*
1285 * Subtract one to leave an empty queue entry for 'Full Queue'
1286 * condition. See NVM-Express 1.2 specification, section 4.1.2.
1287 */
1288 dev->admin_tagset.queue_depth = NVME_AQ_BLKMQ_DEPTH - 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001289 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001290 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001291 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Jens Axboed3484992017-01-13 14:43:58 -07001292 dev->admin_tagset.flags = BLK_MQ_F_NO_SCHED;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001293 dev->admin_tagset.driver_data = dev;
1294
1295 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1296 return -ENOMEM;
1297
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001298 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1299 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001300 blk_mq_free_tag_set(&dev->admin_tagset);
1301 return -ENOMEM;
1302 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001303 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001304 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001305 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001306 return -ENODEV;
1307 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001308 } else
Keith Busch25646262016-01-04 09:10:57 -07001309 blk_mq_start_stopped_hw_queues(dev->ctrl.admin_q, true);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001310
1311 return 0;
1312}
1313
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001314static int nvme_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001315{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001316 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001317 u32 aqa;
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001318 u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001319 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001320
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001321 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1, 0) ?
Keith Buschdfbac8c2015-08-10 15:20:40 -06001322 NVME_CAP_NSSRC(cap) : 0;
1323
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001324 if (dev->subsystem &&
1325 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1326 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001327
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001328 result = nvme_disable_ctrl(&dev->ctrl, cap);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001329 if (result < 0)
1330 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001331
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001332 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001333 if (!nvmeq) {
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001334 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH,
1335 dev_to_node(dev->dev));
Keith Buschcd638942013-07-15 15:02:23 -06001336 if (!nvmeq)
1337 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001338 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001339
1340 aqa = nvmeq->q_depth - 1;
1341 aqa |= aqa << 16;
1342
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001343 writel(aqa, dev->bar + NVME_REG_AQA);
1344 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1345 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Keith Busch1d090622014-06-23 11:34:01 -06001346
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001347 result = nvme_enable_ctrl(&dev->ctrl, cap);
Keith Busch025c5572013-05-01 13:07:51 -06001348 if (result)
Keith Buschd4875622016-11-15 15:56:26 -05001349 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001350
Keith Busch2b25d982014-12-22 12:59:04 -07001351 nvmeq->cq_vector = 0;
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001352 result = queue_request_irq(nvmeq);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001353 if (result) {
1354 nvmeq->cq_vector = -1;
Keith Buschd4875622016-11-15 15:56:26 -05001355 return result;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001356 }
Keith Busch025c5572013-05-01 13:07:51 -06001357
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001358 return result;
1359}
1360
Guilherme G. Piccolic875a702016-04-13 11:08:20 -03001361static bool nvme_should_reset(struct nvme_dev *dev, u32 csts)
1362{
1363
1364 /* If true, indicates loss of adapter communication, possibly by a
1365 * NVMe Subsystem reset.
1366 */
1367 bool nssro = dev->subsystem && (csts & NVME_CSTS_NSSRO);
1368
1369 /* If there is a reset ongoing, we shouldn't reset again. */
1370 if (work_busy(&dev->reset_work))
1371 return false;
1372
1373 /* We shouldn't reset unless the controller is on fatal error state
1374 * _or_ if we lost the communication with it.
1375 */
1376 if (!(csts & NVME_CSTS_CFS) && !nssro)
1377 return false;
1378
1379 /* If PCI error recovery process is happening, we cannot reset or
1380 * the recovery mechanism will surely fail.
1381 */
1382 if (pci_channel_offline(to_pci_dev(dev->dev)))
1383 return false;
1384
1385 return true;
1386}
1387
Andy Lutomirskid2a61912016-12-02 21:14:15 -07001388static void nvme_warn_reset(struct nvme_dev *dev, u32 csts)
1389{
1390 /* Read a config register to help see what died. */
1391 u16 pci_status;
1392 int result;
1393
1394 result = pci_read_config_word(to_pci_dev(dev->dev), PCI_STATUS,
1395 &pci_status);
1396 if (result == PCIBIOS_SUCCESSFUL)
1397 dev_warn(dev->dev,
1398 "controller is down; will reset: CSTS=0x%x, PCI_STATUS=0x%hx\n",
1399 csts, pci_status);
1400 else
1401 dev_warn(dev->dev,
1402 "controller is down; will reset: CSTS=0x%x, PCI_STATUS read failed (%d)\n",
1403 csts, result);
1404}
1405
Christoph Hellwig2d55cd52016-02-29 15:59:46 +01001406static void nvme_watchdog_timer(unsigned long data)
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001407{
Christoph Hellwig2d55cd52016-02-29 15:59:46 +01001408 struct nvme_dev *dev = (struct nvme_dev *)data;
1409 u32 csts = readl(dev->bar + NVME_REG_CSTS);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001410
Guilherme G. Piccolic875a702016-04-13 11:08:20 -03001411 /* Skip controllers under certain specific conditions. */
1412 if (nvme_should_reset(dev, csts)) {
Keith Buschc5f6ce92016-10-05 16:32:45 -04001413 if (!nvme_reset(dev))
Andy Lutomirskid2a61912016-12-02 21:14:15 -07001414 nvme_warn_reset(dev, csts);
Christoph Hellwig2d55cd52016-02-29 15:59:46 +01001415 return;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001416 }
Christoph Hellwig2d55cd52016-02-29 15:59:46 +01001417
1418 mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + HZ));
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001419}
1420
Christoph Hellwig749941f2015-11-26 11:46:39 +01001421static int nvme_create_io_queues(struct nvme_dev *dev)
Keith Busch42f61422014-03-24 10:46:25 -06001422{
Keith Busch949928c2015-12-17 17:08:15 -07001423 unsigned i, max;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001424 int ret = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001425
Christoph Hellwig749941f2015-11-26 11:46:39 +01001426 for (i = dev->queue_count; i <= dev->max_qid; i++) {
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001427 /* vector == qid - 1, match nvme_create_queue */
1428 if (!nvme_alloc_queue(dev, i, dev->q_depth,
1429 pci_irq_get_node(to_pci_dev(dev->dev), i - 1))) {
Christoph Hellwig749941f2015-11-26 11:46:39 +01001430 ret = -ENOMEM;
Keith Busch42f61422014-03-24 10:46:25 -06001431 break;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001432 }
1433 }
Keith Busch42f61422014-03-24 10:46:25 -06001434
Keith Busch949928c2015-12-17 17:08:15 -07001435 max = min(dev->max_qid, dev->queue_count - 1);
1436 for (i = dev->online_queues; i <= max; i++) {
Christoph Hellwig749941f2015-11-26 11:46:39 +01001437 ret = nvme_create_queue(dev->queues[i], i);
Keith Buschd4875622016-11-15 15:56:26 -05001438 if (ret)
Keith Busch42f61422014-03-24 10:46:25 -06001439 break;
Matthew Wilcox27e81662014-04-11 11:58:45 -04001440 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001441
1442 /*
1443 * Ignore failing Create SQ/CQ commands, we can continue with less
1444 * than the desired aount of queues, and even a controller without
1445 * I/O queues an still be used to issue admin commands. This might
1446 * be useful to upgrade a buggy firmware for example.
1447 */
1448 return ret >= 0 ? 0 : ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001449}
1450
Stephen Bates202021c2016-10-05 20:01:12 -06001451static ssize_t nvme_cmb_show(struct device *dev,
1452 struct device_attribute *attr,
1453 char *buf)
1454{
1455 struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
1456
Stephen Batesc9658092016-12-16 11:54:50 -07001457 return scnprintf(buf, PAGE_SIZE, "cmbloc : x%08x\ncmbsz : x%08x\n",
Stephen Bates202021c2016-10-05 20:01:12 -06001458 ndev->cmbloc, ndev->cmbsz);
1459}
1460static DEVICE_ATTR(cmb, S_IRUGO, nvme_cmb_show, NULL);
1461
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001462static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1463{
1464 u64 szu, size, offset;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001465 resource_size_t bar_size;
1466 struct pci_dev *pdev = to_pci_dev(dev->dev);
1467 void __iomem *cmb;
1468 dma_addr_t dma_addr;
1469
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001470 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001471 if (!(NVME_CMB_SZ(dev->cmbsz)))
1472 return NULL;
Stephen Bates202021c2016-10-05 20:01:12 -06001473 dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001474
Stephen Bates202021c2016-10-05 20:01:12 -06001475 if (!use_cmb_sqes)
1476 return NULL;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001477
1478 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1479 size = szu * NVME_CMB_SZ(dev->cmbsz);
Stephen Bates202021c2016-10-05 20:01:12 -06001480 offset = szu * NVME_CMB_OFST(dev->cmbloc);
1481 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(dev->cmbloc));
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001482
1483 if (offset > bar_size)
1484 return NULL;
1485
1486 /*
1487 * Controllers may support a CMB size larger than their BAR,
1488 * for example, due to being behind a bridge. Reduce the CMB to
1489 * the reported size of the BAR
1490 */
1491 if (size > bar_size - offset)
1492 size = bar_size - offset;
1493
Stephen Bates202021c2016-10-05 20:01:12 -06001494 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(dev->cmbloc)) + offset;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001495 cmb = ioremap_wc(dma_addr, size);
1496 if (!cmb)
1497 return NULL;
1498
1499 dev->cmb_dma_addr = dma_addr;
1500 dev->cmb_size = size;
1501 return cmb;
1502}
1503
1504static inline void nvme_release_cmb(struct nvme_dev *dev)
1505{
1506 if (dev->cmb) {
1507 iounmap(dev->cmb);
1508 dev->cmb = NULL;
1509 }
1510}
1511
Keith Busch9d713c22013-07-15 15:02:24 -06001512static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1513{
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001514 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
Keith Busch9d713c22013-07-15 15:02:24 -06001515}
1516
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001517static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001518{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001519 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02001520 struct pci_dev *pdev = to_pci_dev(dev->dev);
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001521 int result, nr_io_queues, size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001522
Keith Busch2800b8e2016-05-13 12:38:09 -06001523 nr_io_queues = num_online_cpus();
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001524 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
1525 if (result < 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001526 return result;
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001527
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001528 if (nr_io_queues == 0)
Keith Buscha5229052016-04-08 16:09:10 -06001529 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001530
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001531 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1532 result = nvme_cmb_qdepth(dev, nr_io_queues,
1533 sizeof(struct nvme_command));
1534 if (result > 0)
1535 dev->q_depth = result;
1536 else
1537 nvme_release_cmb(dev);
1538 }
1539
Keith Busch9d713c22013-07-15 15:02:24 -06001540 size = db_bar_size(dev, nr_io_queues);
1541 if (size > 8192) {
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001542 iounmap(dev->bar);
Keith Busch9d713c22013-07-15 15:02:24 -06001543 do {
1544 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1545 if (dev->bar)
1546 break;
1547 if (!--nr_io_queues)
1548 return -ENOMEM;
1549 size = db_bar_size(dev, nr_io_queues);
1550 } while (1);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001551 dev->dbs = dev->bar + 4096;
Keith Busch5a92e702014-02-21 14:13:44 -07001552 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001553 }
1554
Keith Busch9d713c22013-07-15 15:02:24 -06001555 /* Deregister the admin queue's interrupt */
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001556 pci_free_irq(pdev, 0, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001557
Jens Axboee32efbf2014-11-14 09:49:26 -07001558 /*
1559 * If we enable msix early due to not intx, disable it again before
1560 * setting up the full range we need.
1561 */
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001562 pci_free_irq_vectors(pdev);
1563 nr_io_queues = pci_alloc_irq_vectors(pdev, 1, nr_io_queues,
1564 PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY);
1565 if (nr_io_queues <= 0)
1566 return -EIO;
1567 dev->max_qid = nr_io_queues;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001568
Matthew Wilcox063a8092013-06-20 10:53:48 -04001569 /*
1570 * Should investigate if there's a performance win from allocating
1571 * more queues than interrupt vectors; it might allow the submission
1572 * path to scale better, even if the receive path is limited by the
1573 * number of interrupts.
1574 */
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001575
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001576 result = queue_request_irq(adminq);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001577 if (result) {
1578 adminq->cq_vector = -1;
Keith Buschd4875622016-11-15 15:56:26 -05001579 return result;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001580 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001581 return nvme_create_io_queues(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001582}
1583
Keith Buschdb3cbff2016-01-12 14:41:17 -07001584static void nvme_del_queue_end(struct request *req, int error)
1585{
1586 struct nvme_queue *nvmeq = req->end_io_data;
1587
1588 blk_mq_free_request(req);
1589 complete(&nvmeq->dev->ioq_wait);
1590}
1591
1592static void nvme_del_cq_end(struct request *req, int error)
1593{
1594 struct nvme_queue *nvmeq = req->end_io_data;
1595
1596 if (!error) {
1597 unsigned long flags;
1598
Ming Lin2e39e0f2016-04-05 10:32:04 -07001599 /*
1600 * We might be called with the AQ q_lock held
1601 * and the I/O queue q_lock should always
1602 * nest inside the AQ one.
1603 */
1604 spin_lock_irqsave_nested(&nvmeq->q_lock, flags,
1605 SINGLE_DEPTH_NESTING);
Keith Buschdb3cbff2016-01-12 14:41:17 -07001606 nvme_process_cq(nvmeq);
1607 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
1608 }
1609
1610 nvme_del_queue_end(req, error);
1611}
1612
1613static int nvme_delete_queue(struct nvme_queue *nvmeq, u8 opcode)
1614{
1615 struct request_queue *q = nvmeq->dev->ctrl.admin_q;
1616 struct request *req;
1617 struct nvme_command cmd;
1618
1619 memset(&cmd, 0, sizeof(cmd));
1620 cmd.delete_queue.opcode = opcode;
1621 cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid);
1622
Christoph Hellwigeb71f432016-06-13 16:45:23 +02001623 req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
Keith Buschdb3cbff2016-01-12 14:41:17 -07001624 if (IS_ERR(req))
1625 return PTR_ERR(req);
1626
1627 req->timeout = ADMIN_TIMEOUT;
1628 req->end_io_data = nvmeq;
1629
1630 blk_execute_rq_nowait(q, NULL, req, false,
1631 opcode == nvme_admin_delete_cq ?
1632 nvme_del_cq_end : nvme_del_queue_end);
1633 return 0;
1634}
1635
Keith Busch70659062016-10-12 09:22:16 -06001636static void nvme_disable_io_queues(struct nvme_dev *dev, int queues)
Keith Buschdb3cbff2016-01-12 14:41:17 -07001637{
Keith Busch70659062016-10-12 09:22:16 -06001638 int pass;
Keith Buschdb3cbff2016-01-12 14:41:17 -07001639 unsigned long timeout;
1640 u8 opcode = nvme_admin_delete_sq;
1641
1642 for (pass = 0; pass < 2; pass++) {
Keith Busch014a0d62016-05-06 11:50:52 -06001643 int sent = 0, i = queues;
Keith Buschdb3cbff2016-01-12 14:41:17 -07001644
1645 reinit_completion(&dev->ioq_wait);
1646 retry:
1647 timeout = ADMIN_TIMEOUT;
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06001648 for (; i > 0; i--, sent++)
1649 if (nvme_delete_queue(dev->queues[i], opcode))
Keith Buschdb3cbff2016-01-12 14:41:17 -07001650 break;
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06001651
Keith Buschdb3cbff2016-01-12 14:41:17 -07001652 while (sent--) {
1653 timeout = wait_for_completion_io_timeout(&dev->ioq_wait, timeout);
1654 if (timeout == 0)
1655 return;
1656 if (i)
1657 goto retry;
1658 }
1659 opcode = nvme_admin_delete_cq;
1660 }
1661}
1662
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001663/*
1664 * Return: error value if an error occurred setting up the queues or calling
1665 * Identify Device. 0 if these succeeded, even if adding some of the
1666 * namespaces failed. At the moment, these failures are silent. TBD which
1667 * failures should be reported.
1668 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001669static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001670{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001671 if (!dev->ctrl.tagset) {
Keith Buschffe77042015-06-08 10:08:15 -06001672 dev->tagset.ops = &nvme_mq_ops;
1673 dev->tagset.nr_hw_queues = dev->online_queues - 1;
1674 dev->tagset.timeout = NVME_IO_TIMEOUT;
1675 dev->tagset.numa_node = dev_to_node(dev->dev);
1676 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001677 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06001678 dev->tagset.cmd_size = nvme_cmd_size(dev);
1679 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
1680 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001681
Keith Buschffe77042015-06-08 10:08:15 -06001682 if (blk_mq_alloc_tag_set(&dev->tagset))
1683 return 0;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001684 dev->ctrl.tagset = &dev->tagset;
Helen Koikef9f38e32017-04-10 12:51:07 -03001685
1686 nvme_dbbuf_set(dev);
Keith Busch949928c2015-12-17 17:08:15 -07001687 } else {
1688 blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1);
1689
1690 /* Free previously allocated queues that are no longer usable */
1691 nvme_free_queues(dev, dev->online_queues);
Keith Buschffe77042015-06-08 10:08:15 -06001692 }
Keith Busch949928c2015-12-17 17:08:15 -07001693
Keith Busche1e5e562015-02-19 13:39:03 -07001694 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001695}
1696
Keith Buschb00a7262016-02-24 09:15:52 -07001697static int nvme_pci_enable(struct nvme_dev *dev)
Keith Busch0877cb02013-07-15 15:02:19 -06001698{
Keith Busch42f61422014-03-24 10:46:25 -06001699 u64 cap;
Keith Buschb00a7262016-02-24 09:15:52 -07001700 int result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001701 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001702
1703 if (pci_enable_device_mem(pdev))
1704 return result;
1705
Keith Busch0877cb02013-07-15 15:02:19 -06001706 pci_set_master(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001707
Christoph Hellwige75ec752015-05-22 11:12:39 +02001708 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
1709 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01001710 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06001711
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001712 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07001713 result = -ENODEV;
Keith Buschb00a7262016-02-24 09:15:52 -07001714 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07001715 }
Jens Axboee32efbf2014-11-14 09:49:26 -07001716
1717 /*
Keith Buscha5229052016-04-08 16:09:10 -06001718 * Some devices and/or platforms don't advertise or work with INTx
1719 * interrupts. Pre-enable a single MSIX or MSI vec for setup. We'll
1720 * adjust this later.
Jens Axboee32efbf2014-11-14 09:49:26 -07001721 */
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001722 result = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
1723 if (result < 0)
1724 return result;
Jens Axboee32efbf2014-11-14 09:49:26 -07001725
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001726 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
1727
Keith Busch42f61422014-03-24 10:46:25 -06001728 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
1729 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001730 dev->dbs = dev->bar + 4096;
Stephan Günther1f390c12015-12-01 13:23:22 -07001731
1732 /*
1733 * Temporary fix for the Apple controller found in the MacBook8,1 and
1734 * some MacBook7,1 to avoid controller resets and data loss.
1735 */
1736 if (pdev->vendor == PCI_VENDOR_ID_APPLE && pdev->device == 0x2001) {
1737 dev->q_depth = 2;
1738 dev_warn(dev->dev, "detected Apple NVMe controller, set "
1739 "queue depth=%u to work around controller resets\n",
1740 dev->q_depth);
1741 }
1742
Stephen Bates202021c2016-10-05 20:01:12 -06001743 /*
1744 * CMBs can currently only exist on >=1.2 PCIe devices. We only
1745 * populate sysfs if a CMB is implemented. Note that we add the
1746 * CMB attribute to the nvme_ctrl kobj which removes the need to remove
1747 * it on exit. Since nvme_dev_attrs_group has no name we can pass
1748 * NULL as final argument to sysfs_add_file_to_group.
1749 */
1750
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001751 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2, 0)) {
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001752 dev->cmb = nvme_map_cmb(dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001753
Stephen Bates202021c2016-10-05 20:01:12 -06001754 if (dev->cmbsz) {
1755 if (sysfs_add_file_to_group(&dev->ctrl.device->kobj,
1756 &dev_attr_cmb.attr, NULL))
1757 dev_warn(dev->dev,
1758 "failed to add sysfs attribute for CMB\n");
1759 }
1760 }
1761
Keith Buscha0a34082015-12-07 15:30:31 -07001762 pci_enable_pcie_error_reporting(pdev);
1763 pci_save_state(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001764 return 0;
1765
1766 disable:
Keith Busch0877cb02013-07-15 15:02:19 -06001767 pci_disable_device(pdev);
1768 return result;
1769}
1770
1771static void nvme_dev_unmap(struct nvme_dev *dev)
1772{
Keith Buschb00a7262016-02-24 09:15:52 -07001773 if (dev->bar)
1774 iounmap(dev->bar);
Johannes Thumshirna1f447b2016-06-07 09:44:02 +02001775 pci_release_mem_regions(to_pci_dev(dev->dev));
Keith Buschb00a7262016-02-24 09:15:52 -07001776}
1777
1778static void nvme_pci_disable(struct nvme_dev *dev)
1779{
Christoph Hellwige75ec752015-05-22 11:12:39 +02001780 struct pci_dev *pdev = to_pci_dev(dev->dev);
1781
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001782 pci_free_irq_vectors(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001783
Keith Buscha0a34082015-12-07 15:30:31 -07001784 if (pci_is_enabled(pdev)) {
1785 pci_disable_pcie_error_reporting(pdev);
Christoph Hellwige75ec752015-05-22 11:12:39 +02001786 pci_disable_device(pdev);
Keith Busch4d115422013-12-10 13:10:40 -07001787 }
Keith Busch4d115422013-12-10 13:10:40 -07001788}
1789
Keith Buscha5cdb682016-01-12 14:41:18 -07001790static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001791{
Keith Busch70659062016-10-12 09:22:16 -06001792 int i, queues;
Keith Busch302ad8c2017-03-01 14:22:12 -05001793 bool dead = true;
1794 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch22404272013-07-15 15:02:20 -06001795
Christoph Hellwig2d55cd52016-02-29 15:59:46 +01001796 del_timer_sync(&dev->watchdog_timer);
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05001797
Keith Busch77bf25e2015-11-26 12:21:29 +01001798 mutex_lock(&dev->shutdown_lock);
Keith Busch302ad8c2017-03-01 14:22:12 -05001799 if (pci_is_enabled(pdev)) {
1800 u32 csts = readl(dev->bar + NVME_REG_CSTS);
1801
1802 if (dev->ctrl.state == NVME_CTRL_LIVE)
1803 nvme_start_freeze(&dev->ctrl);
1804 dead = !!((csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) ||
1805 pdev->error_state != pci_channel_io_normal);
Keith Buschc9d3bf82015-01-07 18:55:52 -07001806 }
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06001807
Keith Busch302ad8c2017-03-01 14:22:12 -05001808 /*
1809 * Give the controller a chance to complete all entered requests if
1810 * doing a safe shutdown.
1811 */
1812 if (!dead && shutdown)
1813 nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
1814 nvme_stop_queues(&dev->ctrl);
1815
Keith Busch70659062016-10-12 09:22:16 -06001816 queues = dev->online_queues - 1;
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06001817 for (i = dev->queue_count - 1; i > 0; i--)
1818 nvme_suspend_queue(dev->queues[i]);
1819
Keith Busch302ad8c2017-03-01 14:22:12 -05001820 if (dead) {
Gabriel Krisman Bertazi82469c52016-09-06 17:39:13 -03001821 /* A device might become IO incapable very soon during
1822 * probe, before the admin queue is configured. Thus,
1823 * queue_count can be 0 here.
1824 */
1825 if (dev->queue_count)
1826 nvme_suspend_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07001827 } else {
Keith Busch70659062016-10-12 09:22:16 -06001828 nvme_disable_io_queues(dev, queues);
Keith Buscha5cdb682016-01-12 14:41:18 -07001829 nvme_disable_admin_queue(dev, shutdown);
Keith Busch4d115422013-12-10 13:10:40 -07001830 }
Keith Buschb00a7262016-02-24 09:15:52 -07001831 nvme_pci_disable(dev);
Keith Busch07836e62015-02-19 10:34:48 -07001832
Ming Line1958e62016-05-18 14:05:01 -07001833 blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl);
1834 blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl);
Keith Busch302ad8c2017-03-01 14:22:12 -05001835
1836 /*
1837 * The driver will not be starting up queues again if shutting down so
1838 * must flush all entered requests to their failed completion to avoid
1839 * deadlocking blk-mq hot-cpu notifier.
1840 */
1841 if (shutdown)
1842 nvme_start_queues(&dev->ctrl);
Keith Busch77bf25e2015-11-26 12:21:29 +01001843 mutex_unlock(&dev->shutdown_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001844}
1845
Matthew Wilcox091b6092011-02-10 09:56:01 -05001846static int nvme_setup_prp_pools(struct nvme_dev *dev)
1847{
Christoph Hellwige75ec752015-05-22 11:12:39 +02001848 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05001849 PAGE_SIZE, PAGE_SIZE, 0);
1850 if (!dev->prp_page_pool)
1851 return -ENOMEM;
1852
Matthew Wilcox99802a72011-02-10 10:30:34 -05001853 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02001854 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05001855 256, 256, 0);
1856 if (!dev->prp_small_pool) {
1857 dma_pool_destroy(dev->prp_page_pool);
1858 return -ENOMEM;
1859 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05001860 return 0;
1861}
1862
1863static void nvme_release_prp_pools(struct nvme_dev *dev)
1864{
1865 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05001866 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05001867}
1868
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001869static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001870{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001871 struct nvme_dev *dev = to_nvme_dev(ctrl);
Keith Busch9ac27092014-01-31 16:53:39 -07001872
Helen Koikef9f38e32017-04-10 12:51:07 -03001873 nvme_dbbuf_dma_free(dev);
Christoph Hellwige75ec752015-05-22 11:12:39 +02001874 put_device(dev->dev);
Keith Busch4af0e212015-06-08 10:08:13 -06001875 if (dev->tagset.tags)
1876 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001877 if (dev->ctrl.admin_q)
1878 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07001879 kfree(dev->queues);
Scott Bauere286bcf2017-02-22 10:15:07 -07001880 free_opal_dev(dev->ctrl.opal_dev);
Keith Busch5e82e952013-02-19 10:17:58 -07001881 kfree(dev);
1882}
1883
Keith Buschf58944e2016-02-24 09:15:55 -07001884static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
1885{
Linus Torvalds237045f2016-03-18 17:13:31 -07001886 dev_warn(dev->ctrl.device, "Removing after probe failure status: %d\n", status);
Keith Buschf58944e2016-02-24 09:15:55 -07001887
1888 kref_get(&dev->ctrl.kref);
Keith Busch69d9a992016-02-24 09:15:56 -07001889 nvme_dev_disable(dev, false);
Keith Buschf58944e2016-02-24 09:15:55 -07001890 if (!schedule_work(&dev->remove_work))
1891 nvme_put_ctrl(&dev->ctrl);
1892}
1893
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001894static void nvme_reset_work(struct work_struct *work)
Keith Busch5e82e952013-02-19 10:17:58 -07001895{
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001896 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
Scott Bauera98e58e52017-02-03 12:50:32 -07001897 bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
Keith Buschf58944e2016-02-24 09:15:55 -07001898 int result = -ENODEV;
Keith Buschf0b50732013-07-15 15:02:21 -06001899
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02001900 if (WARN_ON(dev->ctrl.state == NVME_CTRL_RESETTING))
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001901 goto out;
1902
1903 /*
1904 * If we're called to reset a live controller first shut it down before
1905 * moving on.
1906 */
Keith Buschb00a7262016-02-24 09:15:52 -07001907 if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
Keith Buscha5cdb682016-01-12 14:41:18 -07001908 nvme_dev_disable(dev, false);
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001909
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02001910 if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING))
Keith Busch9bf2b972016-04-08 16:11:02 -06001911 goto out;
1912
Keith Buschb00a7262016-02-24 09:15:52 -07001913 result = nvme_pci_enable(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06001914 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001915 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06001916
1917 result = nvme_configure_admin_queue(dev);
1918 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07001919 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06001920
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001921 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07001922 result = nvme_alloc_admin_tags(dev);
1923 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07001924 goto out;
Dan McLeranb9afca32014-04-07 17:10:11 -06001925
Christoph Hellwigce4541f2015-10-16 07:58:46 +02001926 result = nvme_init_identify(&dev->ctrl);
1927 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07001928 goto out;
Christoph Hellwigce4541f2015-10-16 07:58:46 +02001929
Scott Bauere286bcf2017-02-22 10:15:07 -07001930 if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) {
1931 if (!dev->ctrl.opal_dev)
1932 dev->ctrl.opal_dev =
1933 init_opal_dev(&dev->ctrl, &nvme_sec_submit);
1934 else if (was_suspend)
1935 opal_unlock_from_suspend(dev->ctrl.opal_dev);
1936 } else {
1937 free_opal_dev(dev->ctrl.opal_dev);
1938 dev->ctrl.opal_dev = NULL;
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001939 }
Scott Bauera98e58e52017-02-03 12:50:32 -07001940
Helen Koikef9f38e32017-04-10 12:51:07 -03001941 if (dev->ctrl.oacs & NVME_CTRL_OACS_DBBUF_SUPP) {
1942 result = nvme_dbbuf_dma_alloc(dev);
1943 if (result)
1944 dev_warn(dev->dev,
1945 "unable to allocate dma for dbbuf\n");
1946 }
1947
Keith Buschf0b50732013-07-15 15:02:21 -06001948 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06001949 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07001950 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06001951
Keith Busch21f033f2016-04-12 11:13:11 -06001952 /*
1953 * A controller that can not execute IO typically requires user
1954 * intervention to correct. For such degraded controllers, the driver
1955 * should not submit commands the user did not request, so skip
1956 * registering for asynchronous event notification on this condition.
1957 */
Christoph Hellwigf866fc422016-04-26 13:52:00 +02001958 if (dev->online_queues > 1)
1959 nvme_queue_async_events(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001960
Christoph Hellwig2d55cd52016-02-29 15:59:46 +01001961 mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + HZ));
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001962
Christoph Hellwig2659e572015-10-02 18:51:31 +02001963 /*
1964 * Keep the controller around but remove all namespaces if we don't have
1965 * any working I/O queue.
1966 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001967 if (dev->online_queues < 2) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001968 dev_warn(dev->ctrl.device, "IO queues not created\n");
Keith Busch3b247742016-04-27 15:51:18 -06001969 nvme_kill_queues(&dev->ctrl);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001970 nvme_remove_namespaces(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001971 } else {
Keith Busch25646262016-01-04 09:10:57 -07001972 nvme_start_queues(&dev->ctrl);
Keith Busch302ad8c2017-03-01 14:22:12 -05001973 nvme_wait_freeze(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001974 nvme_dev_add(dev);
Keith Busch302ad8c2017-03-01 14:22:12 -05001975 nvme_unfreeze(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001976 }
1977
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02001978 if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) {
1979 dev_warn(dev->ctrl.device, "failed to mark controller live\n");
1980 goto out;
1981 }
Christoph Hellwig92911a52016-04-26 13:51:58 +02001982
1983 if (dev->online_queues > 1)
Christoph Hellwig5955be22016-04-26 13:51:59 +02001984 nvme_queue_scan(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001985 return;
Keith Buschf0b50732013-07-15 15:02:21 -06001986
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02001987 out:
Keith Buschf58944e2016-02-24 09:15:55 -07001988 nvme_remove_dead_ctrl(dev, result);
Keith Buschf0b50732013-07-15 15:02:21 -06001989}
1990
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01001991static void nvme_remove_dead_ctrl_work(struct work_struct *work)
Keith Busch9a6b9452013-12-10 13:10:36 -07001992{
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01001993 struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
Christoph Hellwige75ec752015-05-22 11:12:39 +02001994 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07001995
Keith Busch69d9a992016-02-24 09:15:56 -07001996 nvme_kill_queues(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07001997 if (pci_get_drvdata(pdev))
Keith Busch921920a2016-03-28 16:03:21 -06001998 device_release_driver(&pdev->dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001999 nvme_put_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002000}
2001
Keith Busch4cc06522015-06-05 10:30:08 -06002002static int nvme_reset(struct nvme_dev *dev)
2003{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002004 if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q))
Keith Busch4cc06522015-06-05 10:30:08 -06002005 return -ENODEV;
Keith Buschc5f6ce92016-10-05 16:32:45 -04002006 if (work_busy(&dev->reset_work))
2007 return -ENODEV;
Christoph Hellwig846cc052015-11-26 12:10:29 +01002008 if (!queue_work(nvme_workq, &dev->reset_work))
2009 return -EBUSY;
Christoph Hellwig846cc052015-11-26 12:10:29 +01002010 return 0;
Keith Busch4cc06522015-06-05 10:30:08 -06002011}
2012
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002013static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
Keith Busch4cc06522015-06-05 10:30:08 -06002014{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002015 *val = readl(to_nvme_dev(ctrl)->bar + off);
2016 return 0;
Keith Busch4cc06522015-06-05 10:30:08 -06002017}
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002018
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002019static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2020{
2021 writel(val, to_nvme_dev(ctrl)->bar + off);
2022 return 0;
2023}
2024
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002025static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2026{
2027 *val = readq(to_nvme_dev(ctrl)->bar + off);
2028 return 0;
2029}
2030
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002031static int nvme_pci_reset_ctrl(struct nvme_ctrl *ctrl)
2032{
Keith Buschc5f6ce92016-10-05 16:32:45 -04002033 struct nvme_dev *dev = to_nvme_dev(ctrl);
2034 int ret = nvme_reset(dev);
2035
2036 if (!ret)
2037 flush_work(&dev->reset_work);
2038 return ret;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002039}
2040
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002041static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
Ming Lin1a353d82016-06-13 16:45:24 +02002042 .name = "pcie",
Sagi Grimberge439bb12016-02-10 10:03:29 -08002043 .module = THIS_MODULE,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002044 .reg_read32 = nvme_pci_reg_read32,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002045 .reg_write32 = nvme_pci_reg_write32,
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002046 .reg_read64 = nvme_pci_reg_read64,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002047 .reset_ctrl = nvme_pci_reset_ctrl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002048 .free_ctrl = nvme_pci_free_ctrl,
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002049 .submit_async_event = nvme_pci_submit_async_event,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002050};
Keith Busch4cc06522015-06-05 10:30:08 -06002051
Keith Buschb00a7262016-02-24 09:15:52 -07002052static int nvme_dev_map(struct nvme_dev *dev)
2053{
Keith Buschb00a7262016-02-24 09:15:52 -07002054 struct pci_dev *pdev = to_pci_dev(dev->dev);
2055
Johannes Thumshirna1f447b2016-06-07 09:44:02 +02002056 if (pci_request_mem_regions(pdev, "nvme"))
Keith Buschb00a7262016-02-24 09:15:52 -07002057 return -ENODEV;
2058
2059 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2060 if (!dev->bar)
2061 goto release;
2062
Max Gurtovoy9fa196e2016-12-19 16:18:24 +02002063 return 0;
Keith Buschb00a7262016-02-24 09:15:52 -07002064 release:
Max Gurtovoy9fa196e2016-12-19 16:18:24 +02002065 pci_release_mem_regions(pdev);
2066 return -ENODEV;
Keith Buschb00a7262016-02-24 09:15:52 -07002067}
2068
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002069static unsigned long check_dell_samsung_bug(struct pci_dev *pdev)
2070{
2071 if (pdev->vendor == 0x144d && pdev->device == 0xa802) {
2072 /*
2073 * Several Samsung devices seem to drop off the PCIe bus
2074 * randomly when APST is on and uses the deepest sleep state.
2075 * This has been observed on a Samsung "SM951 NVMe SAMSUNG
2076 * 256GB", a "PM951 NVMe SAMSUNG 512GB", and a "Samsung SSD
2077 * 950 PRO 256GB", but it seems to be restricted to two Dell
2078 * laptops.
2079 */
2080 if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.") &&
2081 (dmi_match(DMI_PRODUCT_NAME, "XPS 15 9550") ||
2082 dmi_match(DMI_PRODUCT_NAME, "Precision 5510")))
2083 return NVME_QUIRK_NO_DEEPEST_PS;
2084 }
2085
2086 return 0;
2087}
2088
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002089static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002090{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002091 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002092 struct nvme_dev *dev;
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002093 unsigned long quirks = id->driver_data;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002094
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002095 node = dev_to_node(&pdev->dev);
2096 if (node == NUMA_NO_NODE)
Masayoshi Mizuma2fa84352016-06-20 09:33:17 +09002097 set_dev_node(&pdev->dev, first_memory_node);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002098
2099 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002100 if (!dev)
2101 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002102 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2103 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002104 if (!dev->queues)
2105 goto free;
2106
Christoph Hellwige75ec752015-05-22 11:12:39 +02002107 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002108 pci_set_drvdata(pdev, dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002109
Keith Buschb00a7262016-02-24 09:15:52 -07002110 result = nvme_dev_map(dev);
2111 if (result)
2112 goto free;
2113
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002114 INIT_WORK(&dev->reset_work, nvme_reset_work);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002115 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
Christoph Hellwig2d55cd52016-02-29 15:59:46 +01002116 setup_timer(&dev->watchdog_timer, nvme_watchdog_timer,
2117 (unsigned long)dev);
Keith Busch77bf25e2015-11-26 12:21:29 +01002118 mutex_init(&dev->shutdown_lock);
Keith Buschdb3cbff2016-01-12 14:41:17 -07002119 init_completion(&dev->ioq_wait);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002120
2121 result = nvme_setup_prp_pools(dev);
2122 if (result)
2123 goto put_pci;
2124
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002125 quirks |= check_dell_samsung_bug(pdev);
2126
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002127 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002128 quirks);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002129 if (result)
2130 goto release_pools;
2131
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002132 dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev));
2133
Keith Busch92f7a162015-10-23 11:42:02 -06002134 queue_work(nvme_workq, &dev->reset_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002135 return 0;
2136
Keith Busch0877cb02013-07-15 15:02:19 -06002137 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002138 nvme_release_prp_pools(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002139 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002140 put_device(dev->dev);
Keith Buschb00a7262016-02-24 09:15:52 -07002141 nvme_dev_unmap(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002142 free:
2143 kfree(dev->queues);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002144 kfree(dev);
2145 return result;
2146}
2147
Keith Buschf0d54a52014-05-02 10:40:43 -06002148static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2149{
Keith Buscha6739472014-06-23 16:03:21 -06002150 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002151
Keith Buscha6739472014-06-23 16:03:21 -06002152 if (prepare)
Keith Buscha5cdb682016-01-12 14:41:18 -07002153 nvme_dev_disable(dev, false);
Keith Buscha6739472014-06-23 16:03:21 -06002154 else
Keith Buschc5f6ce92016-10-05 16:32:45 -04002155 nvme_reset(dev);
Keith Buschf0d54a52014-05-02 10:40:43 -06002156}
2157
Keith Busch09ece142014-01-27 11:29:40 -05002158static void nvme_shutdown(struct pci_dev *pdev)
2159{
2160 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buscha5cdb682016-01-12 14:41:18 -07002161 nvme_dev_disable(dev, true);
Keith Busch09ece142014-01-27 11:29:40 -05002162}
2163
Keith Buschf58944e2016-02-24 09:15:55 -07002164/*
2165 * The driver's remove may be called on a device in a partially initialized
2166 * state. This function must not have any dependencies on the device state in
2167 * order to proceed.
2168 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002169static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002170{
2171 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002172
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002173 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
2174
Keith Busch9a6b9452013-12-10 13:10:36 -07002175 pci_set_drvdata(pdev, NULL);
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002176
Keith Busch6db28ed2017-02-10 18:15:49 -05002177 if (!pci_device_is_present(pdev)) {
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002178 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD);
Keith Busch6db28ed2017-02-10 18:15:49 -05002179 nvme_dev_disable(dev, false);
2180 }
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002181
Keith Busch9bf2b972016-04-08 16:11:02 -06002182 flush_work(&dev->reset_work);
Keith Busch53029b02015-11-28 15:41:02 +01002183 nvme_uninit_ctrl(&dev->ctrl);
Keith Buscha5cdb682016-01-12 14:41:18 -07002184 nvme_dev_disable(dev, true);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002185 nvme_dev_remove_admin(dev);
2186 nvme_free_queues(dev, 0);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002187 nvme_release_cmb(dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002188 nvme_release_prp_pools(dev);
Keith Buschb00a7262016-02-24 09:15:52 -07002189 nvme_dev_unmap(dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002190 nvme_put_ctrl(&dev->ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002191}
2192
Keith Busch13880f52016-06-20 09:41:06 -06002193static int nvme_pci_sriov_configure(struct pci_dev *pdev, int numvfs)
2194{
2195 int ret = 0;
2196
2197 if (numvfs == 0) {
2198 if (pci_vfs_assigned(pdev)) {
2199 dev_warn(&pdev->dev,
2200 "Cannot disable SR-IOV VFs while assigned\n");
2201 return -EPERM;
2202 }
2203 pci_disable_sriov(pdev);
2204 return 0;
2205 }
2206
2207 ret = pci_enable_sriov(pdev, numvfs);
2208 return ret ? ret : numvfs;
2209}
2210
Jingoo Han671a6012014-02-13 11:19:14 +09002211#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002212static int nvme_suspend(struct device *dev)
2213{
2214 struct pci_dev *pdev = to_pci_dev(dev);
2215 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2216
Keith Buscha5cdb682016-01-12 14:41:18 -07002217 nvme_dev_disable(ndev, true);
Keith Buschcd638942013-07-15 15:02:23 -06002218 return 0;
2219}
2220
2221static int nvme_resume(struct device *dev)
2222{
2223 struct pci_dev *pdev = to_pci_dev(dev);
2224 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002225
Keith Buschc5f6ce92016-10-05 16:32:45 -04002226 nvme_reset(ndev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002227 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002228}
Jingoo Han671a6012014-02-13 11:19:14 +09002229#endif
Keith Buschcd638942013-07-15 15:02:23 -06002230
2231static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002232
Keith Buscha0a34082015-12-07 15:30:31 -07002233static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
2234 pci_channel_state_t state)
2235{
2236 struct nvme_dev *dev = pci_get_drvdata(pdev);
2237
2238 /*
2239 * A frozen channel requires a reset. When detected, this method will
2240 * shutdown the controller to quiesce. The controller will be restarted
2241 * after the slot reset through driver's slot_reset callback.
2242 */
Keith Buscha0a34082015-12-07 15:30:31 -07002243 switch (state) {
2244 case pci_channel_io_normal:
2245 return PCI_ERS_RESULT_CAN_RECOVER;
2246 case pci_channel_io_frozen:
Keith Buschd011fb32016-04-04 15:07:41 -06002247 dev_warn(dev->ctrl.device,
2248 "frozen state error detected, reset controller\n");
Keith Buscha5cdb682016-01-12 14:41:18 -07002249 nvme_dev_disable(dev, false);
Keith Buscha0a34082015-12-07 15:30:31 -07002250 return PCI_ERS_RESULT_NEED_RESET;
2251 case pci_channel_io_perm_failure:
Keith Buschd011fb32016-04-04 15:07:41 -06002252 dev_warn(dev->ctrl.device,
2253 "failure state error detected, request disconnect\n");
Keith Buscha0a34082015-12-07 15:30:31 -07002254 return PCI_ERS_RESULT_DISCONNECT;
2255 }
2256 return PCI_ERS_RESULT_NEED_RESET;
2257}
2258
2259static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev)
2260{
2261 struct nvme_dev *dev = pci_get_drvdata(pdev);
2262
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002263 dev_info(dev->ctrl.device, "restart after slot reset\n");
Keith Buscha0a34082015-12-07 15:30:31 -07002264 pci_restore_state(pdev);
Keith Buschc5f6ce92016-10-05 16:32:45 -04002265 nvme_reset(dev);
Keith Buscha0a34082015-12-07 15:30:31 -07002266 return PCI_ERS_RESULT_RECOVERED;
2267}
2268
2269static void nvme_error_resume(struct pci_dev *pdev)
2270{
2271 pci_cleanup_aer_uncorrect_error_status(pdev);
2272}
2273
Stephen Hemminger1d352032012-09-07 09:33:17 -07002274static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002275 .error_detected = nvme_error_detected,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002276 .slot_reset = nvme_slot_reset,
2277 .resume = nvme_error_resume,
Keith Buschf0d54a52014-05-02 10:40:43 -06002278 .reset_notify = nvme_reset_notify,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002279};
2280
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002281static const struct pci_device_id nvme_id_table[] = {
Christoph Hellwig106198e2015-11-26 10:07:41 +01002282 { PCI_VDEVICE(INTEL, 0x0953),
Keith Busch08095e72016-03-04 13:15:17 -07002283 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002284 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch99466e72016-05-02 15:14:24 -06002285 { PCI_VDEVICE(INTEL, 0x0a53),
2286 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002287 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch99466e72016-05-02 15:14:24 -06002288 { PCI_VDEVICE(INTEL, 0x0a54),
2289 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002290 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch540c8012015-10-22 15:45:06 -06002291 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2292 .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03002293 { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */
2294 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Wenbo Wang015282c2016-09-08 12:12:11 -04002295 { PCI_DEVICE(0x1c5f, 0x0540), /* Memblaze Pblaze4 adapter */
2296 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002297 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01002298 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Daniel Roschka124298b2017-02-22 15:17:29 -07002299 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002300 { 0, }
2301};
2302MODULE_DEVICE_TABLE(pci, nvme_id_table);
2303
2304static struct pci_driver nvme_driver = {
2305 .name = "nvme",
2306 .id_table = nvme_id_table,
2307 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002308 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002309 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002310 .driver = {
2311 .pm = &nvme_dev_pm_ops,
2312 },
Keith Busch13880f52016-06-20 09:41:06 -06002313 .sriov_configure = nvme_pci_sriov_configure,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002314 .err_handler = &nvme_err_handler,
2315};
2316
2317static int __init nvme_init(void)
2318{
Matthew Wilcox0ac13142012-07-31 13:31:15 -04002319 int result;
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -05002320
Keith Busch92f7a162015-10-23 11:42:02 -06002321 nvme_workq = alloc_workqueue("nvme", WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
Keith Busch9a6b9452013-12-10 13:10:36 -07002322 if (!nvme_workq)
Dan McLeranb9afca32014-04-07 17:10:11 -06002323 return -ENOMEM;
Keith Busch9a6b9452013-12-10 13:10:36 -07002324
Keith Buschf3db22f2014-06-11 11:51:35 -06002325 result = pci_register_driver(&nvme_driver);
2326 if (result)
Ming Lin576d55d2016-02-10 10:03:32 -08002327 destroy_workqueue(nvme_workq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002328 return result;
2329}
2330
2331static void __exit nvme_exit(void)
2332{
2333 pci_unregister_driver(&nvme_driver);
Keith Busch9a6b9452013-12-10 13:10:36 -07002334 destroy_workqueue(nvme_workq);
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002335 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002336}
2337
2338MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2339MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002340MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002341module_init(nvme_init);
2342module_exit(nvme_exit);