blob: a3e0b9378e54c01a734698142a5a2db5abb3591c [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>
Keith Busch181197752018-04-27 13:42:52 -060016#include <linux/async.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>
Andy Lutomirskiff5350a2017-04-20 13:37:55 -070020#include <linux/dmi.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050021#include <linux/init.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050024#include <linux/mm.h>
25#include <linux/module.h>
Keith Busch77bf25e2015-11-26 12:21:29 +010026#include <linux/mutex.h>
Keith Buschd0877472017-09-15 13:05:38 -040027#include <linux/once.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050028#include <linux/pci.h>
Keith Busche1e5e562015-02-19 13:39:03 -070029#include <linux/t10-pi.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050030#include <linux/types.h>
Linus Torvalds9cf5c092015-11-06 14:22:15 -080031#include <linux/io-64-nonatomic-lo-hi.h>
Scott Bauera98e58e52017-02-03 12:50:32 -070032#include <linux/sed-opal.h>
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -060033#include <linux/pci-p2pdma.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090034
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020035#include "nvme.h"
36
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050037#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
38#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Stephen Batesc9658092016-12-16 11:54:50 -070039
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -070040#define SGES_PER_PAGE (PAGE_SIZE / sizeof(struct nvme_sgl_desc))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050041
Jens Axboe943e9422018-06-21 09:49:37 -060042/*
43 * These can be higher, but we need to ensure that any command doesn't
44 * require an sg allocation that needs more than a page of data.
45 */
46#define NVME_MAX_KB_SZ 4096
47#define NVME_MAX_SEGS 127
48
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050049static int use_threaded_interrupts;
50module_param(use_threaded_interrupts, int, 0);
51
Jon Derrick8ffaadf2015-07-20 10:14:09 -060052static bool use_cmb_sqes = true;
Keith Busch69f4eb92018-06-06 08:13:09 -060053module_param(use_cmb_sqes, bool, 0444);
Jon Derrick8ffaadf2015-07-20 10:14:09 -060054MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
55
Christoph Hellwig87ad72a2017-05-12 17:02:58 +020056static unsigned int max_host_mem_size_mb = 128;
57module_param(max_host_mem_size_mb, uint, 0444);
58MODULE_PARM_DESC(max_host_mem_size_mb,
59 "Maximum Host Memory Buffer (HMB) size per controller (in MiB)");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050060
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -070061static unsigned int sgl_threshold = SZ_32K;
62module_param(sgl_threshold, uint, 0644);
63MODULE_PARM_DESC(sgl_threshold,
64 "Use SGLs when average request segment size is larger or equal to "
65 "this size. Use 0 to disable SGLs.");
66
weiping zhangb27c1e62017-07-10 16:46:59 +080067static int io_queue_depth_set(const char *val, const struct kernel_param *kp);
68static const struct kernel_param_ops io_queue_depth_ops = {
69 .set = io_queue_depth_set,
70 .get = param_get_int,
71};
72
73static int io_queue_depth = 1024;
74module_param_cb(io_queue_depth, &io_queue_depth_ops, &io_queue_depth, 0644);
75MODULE_PARM_DESC(io_queue_depth, "set io queue depth, should >= 2");
76
Jens Axboe3b6592f2018-10-31 08:36:31 -060077static int queue_count_set(const char *val, const struct kernel_param *kp);
78static const struct kernel_param_ops queue_count_ops = {
79 .set = queue_count_set,
80 .get = param_get_int,
81};
82
83static int write_queues;
84module_param_cb(write_queues, &queue_count_ops, &write_queues, 0644);
85MODULE_PARM_DESC(write_queues,
86 "Number of queues to use for writes. If not set, reads and writes "
87 "will share a queue set.");
88
Jens Axboea4668d92018-11-19 08:18:24 -070089static int poll_queues = 0;
Jens Axboe4b04cc62018-11-05 12:44:33 -070090module_param_cb(poll_queues, &queue_count_ops, &poll_queues, 0644);
91MODULE_PARM_DESC(poll_queues, "Number of queues to use for polled IO.");
92
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010093struct nvme_dev;
94struct nvme_queue;
Keith Buschb3fffde2015-02-03 11:21:42 -070095
Keith Buscha5cdb682016-01-12 14:41:18 -070096static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown);
Keith Buschd4b4ff82013-12-10 13:10:37 -070097
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050098/*
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010099 * Represents an NVM Express device. Each nvme_dev is a PCI function.
100 */
101struct nvme_dev {
Sagi Grimberg147b27e2018-01-14 12:39:01 +0200102 struct nvme_queue *queues;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100103 struct blk_mq_tag_set tagset;
104 struct blk_mq_tag_set admin_tagset;
105 u32 __iomem *dbs;
106 struct device *dev;
107 struct dma_pool *prp_page_pool;
108 struct dma_pool *prp_small_pool;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100109 unsigned online_queues;
110 unsigned max_qid;
Christoph Hellwige20ba6e2018-12-02 17:46:16 +0100111 unsigned io_queues[HCTX_MAX_TYPES];
Keith Busch22b55602018-04-12 09:16:10 -0600112 unsigned int num_vecs;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100113 int q_depth;
114 u32 db_stride;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100115 void __iomem *bar;
Xu Yu97f6ef62017-05-24 16:39:55 +0800116 unsigned long bar_mapped_size;
Christoph Hellwig5c8809e2015-11-26 12:35:49 +0100117 struct work_struct remove_work;
Keith Busch77bf25e2015-11-26 12:21:29 +0100118 struct mutex shutdown_lock;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100119 bool subsystem;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100120 u64 cmb_size;
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -0600121 bool cmb_use_sqes;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100122 u32 cmbsz;
Stephen Bates202021c2016-10-05 20:01:12 -0600123 u32 cmbloc;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100124 struct nvme_ctrl ctrl;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +0200125
Jens Axboe943e9422018-06-21 09:49:37 -0600126 mempool_t *iod_mempool;
127
Christoph Hellwig87ad72a2017-05-12 17:02:58 +0200128 /* shadow doorbell buffer support: */
Helen Koikef9f38e32017-04-10 12:51:07 -0300129 u32 *dbbuf_dbs;
130 dma_addr_t dbbuf_dbs_dma_addr;
131 u32 *dbbuf_eis;
132 dma_addr_t dbbuf_eis_dma_addr;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +0200133
134 /* host memory buffer support: */
135 u64 host_mem_size;
136 u32 nr_host_mem_descs;
Christoph Hellwig4033f352017-08-28 10:47:18 +0200137 dma_addr_t host_mem_descs_dma;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +0200138 struct nvme_host_mem_buf_desc *host_mem_descs;
139 void **host_mem_desc_bufs;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500140};
141
weiping zhangb27c1e62017-07-10 16:46:59 +0800142static int io_queue_depth_set(const char *val, const struct kernel_param *kp)
143{
144 int n = 0, ret;
145
146 ret = kstrtoint(val, 10, &n);
147 if (ret != 0 || n < 2)
148 return -EINVAL;
149
150 return param_set_int(val, kp);
151}
152
Jens Axboe3b6592f2018-10-31 08:36:31 -0600153static int queue_count_set(const char *val, const struct kernel_param *kp)
154{
155 int n = 0, ret;
156
157 ret = kstrtoint(val, 10, &n);
158 if (n > num_possible_cpus())
159 n = num_possible_cpus();
160
161 return param_set_int(val, kp);
162}
163
Helen Koikef9f38e32017-04-10 12:51:07 -0300164static inline unsigned int sq_idx(unsigned int qid, u32 stride)
165{
166 return qid * 2 * stride;
167}
168
169static inline unsigned int cq_idx(unsigned int qid, u32 stride)
170{
171 return (qid * 2 + 1) * stride;
172}
173
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100174static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
175{
176 return container_of(ctrl, struct nvme_dev, ctrl);
177}
178
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500179/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500180 * An NVM Express queue. Each device has at least two (one for admin
181 * commands and one for I/O commands).
182 */
183struct nvme_queue {
184 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500185 struct nvme_dev *dev;
Jens Axboe1ab0cd62018-05-17 18:31:51 +0200186 spinlock_t sq_lock;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500187 struct nvme_command *sq_cmds;
Christoph Hellwig3a7afd82018-12-02 17:46:23 +0100188 /* only used for poll queues: */
189 spinlock_t cq_poll_lock ____cacheline_aligned_in_smp;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500190 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600191 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500192 dma_addr_t sq_dma_addr;
193 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500194 u32 __iomem *q_db;
195 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700196 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500197 u16 sq_tail;
Jens Axboe04f3eaf2018-11-29 10:02:29 -0700198 u16 last_sq_tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500199 u16 cq_head;
Jens Axboe68fa9db2018-05-21 08:41:52 -0600200 u16 last_cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700201 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400202 u8 cq_phase;
Christoph Hellwig4e224102018-12-02 17:46:17 +0100203 unsigned long flags;
204#define NVMEQ_ENABLED 0
Christoph Hellwig63223072018-12-02 17:46:18 +0100205#define NVMEQ_SQ_CMB 1
Christoph Hellwigd1ed6aa2018-12-02 17:46:22 +0100206#define NVMEQ_DELETE_ERROR 2
Helen Koikef9f38e32017-04-10 12:51:07 -0300207 u32 *dbbuf_sq_db;
208 u32 *dbbuf_cq_db;
209 u32 *dbbuf_sq_ei;
210 u32 *dbbuf_cq_ei;
Christoph Hellwigd1ed6aa2018-12-02 17:46:22 +0100211 struct completion delete_done;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500212};
213
214/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200215 * The nvme_iod describes the data in an I/O, including the list of PRP
216 * entries. You can't see it in this data structure because C doesn't let
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100217 * me express that. Use nvme_init_iod to ensure there's enough space
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200218 * allocated to store the PRP list.
219 */
220struct nvme_iod {
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800221 struct nvme_request req;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100222 struct nvme_queue *nvmeq;
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700223 bool use_sgl;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100224 int aborted;
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200225 int npages; /* In the PRP list. 0 means small pool in use */
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200226 int nents; /* Used in scatterlist */
227 int length; /* Of data, in bytes */
228 dma_addr_t first_dma;
Christoph Hellwigbf684052015-10-26 17:12:51 +0900229 struct scatterlist meta_sg; /* metadata requires single contiguous buffer */
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100230 struct scatterlist *sg;
231 struct scatterlist inline_sg[0];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500232};
233
234/*
235 * Check we didin't inadvertently grow the command struct
236 */
237static inline void _nvme_check_size(void)
238{
239 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
240 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
241 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
242 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
243 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400244 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700245 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500246 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
Johannes Thumshirn0add5e82017-06-07 11:45:29 +0200247 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != NVME_IDENTIFY_DATA_SIZE);
248 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != NVME_IDENTIFY_DATA_SIZE);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500249 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600250 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Helen Koikef9f38e32017-04-10 12:51:07 -0300251 BUILD_BUG_ON(sizeof(struct nvme_dbbuf) != 64);
252}
253
Jens Axboe3b6592f2018-10-31 08:36:31 -0600254static unsigned int max_io_queues(void)
255{
Jens Axboe4b04cc62018-11-05 12:44:33 -0700256 return num_possible_cpus() + write_queues + poll_queues;
Jens Axboe3b6592f2018-10-31 08:36:31 -0600257}
258
259static unsigned int max_queue_count(void)
260{
261 /* IO queues + admin queue */
262 return 1 + max_io_queues();
263}
264
Helen Koikef9f38e32017-04-10 12:51:07 -0300265static inline unsigned int nvme_dbbuf_size(u32 stride)
266{
Jens Axboe3b6592f2018-10-31 08:36:31 -0600267 return (max_queue_count() * 8 * stride);
Helen Koikef9f38e32017-04-10 12:51:07 -0300268}
269
270static int nvme_dbbuf_dma_alloc(struct nvme_dev *dev)
271{
272 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
273
274 if (dev->dbbuf_dbs)
275 return 0;
276
277 dev->dbbuf_dbs = dma_alloc_coherent(dev->dev, mem_size,
278 &dev->dbbuf_dbs_dma_addr,
279 GFP_KERNEL);
280 if (!dev->dbbuf_dbs)
281 return -ENOMEM;
282 dev->dbbuf_eis = dma_alloc_coherent(dev->dev, mem_size,
283 &dev->dbbuf_eis_dma_addr,
284 GFP_KERNEL);
285 if (!dev->dbbuf_eis) {
286 dma_free_coherent(dev->dev, mem_size,
287 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
288 dev->dbbuf_dbs = NULL;
289 return -ENOMEM;
290 }
291
292 return 0;
293}
294
295static void nvme_dbbuf_dma_free(struct nvme_dev *dev)
296{
297 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
298
299 if (dev->dbbuf_dbs) {
300 dma_free_coherent(dev->dev, mem_size,
301 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
302 dev->dbbuf_dbs = NULL;
303 }
304 if (dev->dbbuf_eis) {
305 dma_free_coherent(dev->dev, mem_size,
306 dev->dbbuf_eis, dev->dbbuf_eis_dma_addr);
307 dev->dbbuf_eis = NULL;
308 }
309}
310
311static void nvme_dbbuf_init(struct nvme_dev *dev,
312 struct nvme_queue *nvmeq, int qid)
313{
314 if (!dev->dbbuf_dbs || !qid)
315 return;
316
317 nvmeq->dbbuf_sq_db = &dev->dbbuf_dbs[sq_idx(qid, dev->db_stride)];
318 nvmeq->dbbuf_cq_db = &dev->dbbuf_dbs[cq_idx(qid, dev->db_stride)];
319 nvmeq->dbbuf_sq_ei = &dev->dbbuf_eis[sq_idx(qid, dev->db_stride)];
320 nvmeq->dbbuf_cq_ei = &dev->dbbuf_eis[cq_idx(qid, dev->db_stride)];
321}
322
323static void nvme_dbbuf_set(struct nvme_dev *dev)
324{
325 struct nvme_command c;
326
327 if (!dev->dbbuf_dbs)
328 return;
329
330 memset(&c, 0, sizeof(c));
331 c.dbbuf.opcode = nvme_admin_dbbuf;
332 c.dbbuf.prp1 = cpu_to_le64(dev->dbbuf_dbs_dma_addr);
333 c.dbbuf.prp2 = cpu_to_le64(dev->dbbuf_eis_dma_addr);
334
335 if (nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0)) {
Christoph Hellwig9bdcfb12017-05-20 15:14:43 +0200336 dev_warn(dev->ctrl.device, "unable to set dbbuf\n");
Helen Koikef9f38e32017-04-10 12:51:07 -0300337 /* Free memory and continue on */
338 nvme_dbbuf_dma_free(dev);
339 }
340}
341
342static inline int nvme_dbbuf_need_event(u16 event_idx, u16 new_idx, u16 old)
343{
344 return (u16)(new_idx - event_idx - 1) < (u16)(new_idx - old);
345}
346
347/* Update dbbuf and return true if an MMIO is required */
348static bool nvme_dbbuf_update_and_check_event(u16 value, u32 *dbbuf_db,
349 volatile u32 *dbbuf_ei)
350{
351 if (dbbuf_db) {
352 u16 old_value;
353
354 /*
355 * Ensure that the queue is written before updating
356 * the doorbell in memory
357 */
358 wmb();
359
360 old_value = *dbbuf_db;
361 *dbbuf_db = value;
362
Michal Wnukowskif1ed3df2018-08-15 15:51:57 -0700363 /*
364 * Ensure that the doorbell is updated before reading the event
365 * index from memory. The controller needs to provide similar
366 * ordering to ensure the envent index is updated before reading
367 * the doorbell.
368 */
369 mb();
370
Helen Koikef9f38e32017-04-10 12:51:07 -0300371 if (!nvme_dbbuf_need_event(*dbbuf_ei, value, old_value))
372 return false;
373 }
374
375 return true;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500376}
377
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700378/*
379 * Max size of iod being embedded in the request payload
380 */
381#define NVME_INT_PAGES 2
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100382#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700383
384/*
385 * Will slightly overestimate the number of pages needed. This is OK
386 * as it only leads to a small amount of wasted memory for the lifetime of
387 * the I/O.
388 */
389static int nvme_npages(unsigned size, struct nvme_dev *dev)
390{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100391 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
392 dev->ctrl.page_size);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700393 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
394}
395
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700396/*
397 * Calculates the number of pages needed for the SGL segments. For example a 4k
398 * page can accommodate 256 SGL descriptors.
399 */
400static int nvme_pci_npages_sgl(unsigned int num_seg)
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100401{
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700402 return DIV_ROUND_UP(num_seg * sizeof(struct nvme_sgl_desc), PAGE_SIZE);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100403}
404
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700405static unsigned int nvme_pci_iod_alloc_size(struct nvme_dev *dev,
406 unsigned int size, unsigned int nseg, bool use_sgl)
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700407{
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700408 size_t alloc_size;
409
410 if (use_sgl)
411 alloc_size = sizeof(__le64 *) * nvme_pci_npages_sgl(nseg);
412 else
413 alloc_size = sizeof(__le64 *) * nvme_npages(size, dev);
414
415 return alloc_size + sizeof(struct scatterlist) * nseg;
416}
417
418static unsigned int nvme_pci_cmd_size(struct nvme_dev *dev, bool use_sgl)
419{
420 unsigned int alloc_size = nvme_pci_iod_alloc_size(dev,
421 NVME_INT_BYTES(dev), NVME_INT_PAGES,
422 use_sgl);
423
424 return sizeof(struct nvme_iod) + alloc_size;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700425}
426
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700427static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
428 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500429{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700430 struct nvme_dev *dev = data;
Sagi Grimberg147b27e2018-01-14 12:39:01 +0200431 struct nvme_queue *nvmeq = &dev->queues[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700432
Keith Busch42483222015-06-01 09:29:54 -0600433 WARN_ON(hctx_idx != 0);
434 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
435 WARN_ON(nvmeq->tags);
436
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700437 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600438 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700439 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500440}
441
Keith Busch4af0e212015-06-08 10:08:13 -0600442static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
443{
444 struct nvme_queue *nvmeq = hctx->driver_data;
445
446 nvmeq->tags = NULL;
447}
448
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700449static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
450 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500451{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700452 struct nvme_dev *dev = data;
Sagi Grimberg147b27e2018-01-14 12:39:01 +0200453 struct nvme_queue *nvmeq = &dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500454
Keith Busch42483222015-06-01 09:29:54 -0600455 if (!nvmeq->tags)
456 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500457
Keith Busch42483222015-06-01 09:29:54 -0600458 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700459 hctx->driver_data = nvmeq;
460 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500461}
462
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600463static int nvme_init_request(struct blk_mq_tag_set *set, struct request *req,
464 unsigned int hctx_idx, unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500465{
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600466 struct nvme_dev *dev = set->driver_data;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100467 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwig03508152017-06-13 09:15:18 +0200468 int queue_idx = (set == &dev->tagset) ? hctx_idx + 1 : 0;
Sagi Grimberg147b27e2018-01-14 12:39:01 +0200469 struct nvme_queue *nvmeq = &dev->queues[queue_idx];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700470
471 BUG_ON(!nvmeq);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100472 iod->nvmeq = nvmeq;
Sagi Grimberg59e29ce2018-06-29 16:50:00 -0600473
474 nvme_req(req)->ctrl = &dev->ctrl;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700475 return 0;
476}
477
Jens Axboe3b6592f2018-10-31 08:36:31 -0600478static int queue_irq_offset(struct nvme_dev *dev)
479{
480 /* if we have more than 1 vec, admin queue offsets us by 1 */
481 if (dev->num_vecs > 1)
482 return 1;
483
484 return 0;
485}
486
Christoph Hellwigdca51e72016-09-14 16:18:57 +0200487static int nvme_pci_map_queues(struct blk_mq_tag_set *set)
488{
489 struct nvme_dev *dev = set->driver_data;
Jens Axboe3b6592f2018-10-31 08:36:31 -0600490 int i, qoff, offset;
Christoph Hellwigdca51e72016-09-14 16:18:57 +0200491
Jens Axboe3b6592f2018-10-31 08:36:31 -0600492 offset = queue_irq_offset(dev);
493 for (i = 0, qoff = 0; i < set->nr_maps; i++) {
494 struct blk_mq_queue_map *map = &set->map[i];
495
496 map->nr_queues = dev->io_queues[i];
497 if (!map->nr_queues) {
Christoph Hellwige20ba6e2018-12-02 17:46:16 +0100498 BUG_ON(i == HCTX_TYPE_DEFAULT);
Christoph Hellwig7e849dd2018-12-17 12:16:27 +0100499 continue;
Jens Axboe3b6592f2018-10-31 08:36:31 -0600500 }
501
Jens Axboe4b04cc62018-11-05 12:44:33 -0700502 /*
503 * The poll queue(s) doesn't have an IRQ (and hence IRQ
504 * affinity), so use the regular blk-mq cpu mapping
505 */
Jens Axboe3b6592f2018-10-31 08:36:31 -0600506 map->queue_offset = qoff;
Christoph Hellwige20ba6e2018-12-02 17:46:16 +0100507 if (i != HCTX_TYPE_POLL)
Jens Axboe4b04cc62018-11-05 12:44:33 -0700508 blk_mq_pci_map_queues(map, to_pci_dev(dev->dev), offset);
509 else
510 blk_mq_map_queues(map);
Jens Axboe3b6592f2018-10-31 08:36:31 -0600511 qoff += map->nr_queues;
512 offset += map->nr_queues;
513 }
514
515 return 0;
Christoph Hellwigdca51e72016-09-14 16:18:57 +0200516}
517
Jens Axboe04f3eaf2018-11-29 10:02:29 -0700518/*
519 * Write sq tail if we are asked to, or if the next command would wrap.
520 */
521static inline void nvme_write_sq_db(struct nvme_queue *nvmeq, bool write_sq)
522{
523 if (!write_sq) {
524 u16 next_tail = nvmeq->sq_tail + 1;
525
526 if (next_tail == nvmeq->q_depth)
527 next_tail = 0;
528 if (next_tail != nvmeq->last_sq_tail)
529 return;
530 }
531
532 if (nvme_dbbuf_update_and_check_event(nvmeq->sq_tail,
533 nvmeq->dbbuf_sq_db, nvmeq->dbbuf_sq_ei))
534 writel(nvmeq->sq_tail, nvmeq->q_db);
535 nvmeq->last_sq_tail = nvmeq->sq_tail;
536}
537
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500538/**
Christoph Hellwig90ea5ca2018-05-26 13:45:55 +0200539 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500540 * @nvmeq: The queue to use
541 * @cmd: The command to send
Jens Axboe04f3eaf2018-11-29 10:02:29 -0700542 * @write_sq: whether to write to the SQ doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500543 */
Jens Axboe04f3eaf2018-11-29 10:02:29 -0700544static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd,
545 bool write_sq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500546{
Christoph Hellwig90ea5ca2018-05-26 13:45:55 +0200547 spin_lock(&nvmeq->sq_lock);
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -0600548 memcpy(&nvmeq->sq_cmds[nvmeq->sq_tail], cmd, sizeof(*cmd));
Christoph Hellwig90ea5ca2018-05-26 13:45:55 +0200549 if (++nvmeq->sq_tail == nvmeq->q_depth)
550 nvmeq->sq_tail = 0;
Jens Axboe04f3eaf2018-11-29 10:02:29 -0700551 nvme_write_sq_db(nvmeq, write_sq);
552 spin_unlock(&nvmeq->sq_lock);
553}
554
555static void nvme_commit_rqs(struct blk_mq_hw_ctx *hctx)
556{
557 struct nvme_queue *nvmeq = hctx->driver_data;
558
559 spin_lock(&nvmeq->sq_lock);
560 if (nvmeq->sq_tail != nvmeq->last_sq_tail)
561 nvme_write_sq_db(nvmeq, true);
Christoph Hellwig90ea5ca2018-05-26 13:45:55 +0200562 spin_unlock(&nvmeq->sq_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500563}
564
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700565static void **nvme_pci_iod_list(struct request *req)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700566{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100567 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700568 return (void **)(iod->sg + blk_rq_nr_phys_segments(req));
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700569}
570
Minwoo Im955b1b52017-12-20 16:30:50 +0900571static inline bool nvme_pci_use_sgls(struct nvme_dev *dev, struct request *req)
572{
573 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Keith Busch20469a32018-01-17 22:04:37 +0100574 int nseg = blk_rq_nr_phys_segments(req);
Minwoo Im955b1b52017-12-20 16:30:50 +0900575 unsigned int avg_seg_size;
576
Keith Busch20469a32018-01-17 22:04:37 +0100577 if (nseg == 0)
578 return false;
579
580 avg_seg_size = DIV_ROUND_UP(blk_rq_payload_bytes(req), nseg);
Minwoo Im955b1b52017-12-20 16:30:50 +0900581
582 if (!(dev->ctrl.sgls & ((1 << 0) | (1 << 1))))
583 return false;
584 if (!iod->nvmeq->qid)
585 return false;
586 if (!sgl_threshold || avg_seg_size < sgl_threshold)
587 return false;
588 return true;
589}
590
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200591static blk_status_t nvme_init_iod(struct request *rq, struct nvme_dev *dev)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500592{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100593 struct nvme_iod *iod = blk_mq_rq_to_pdu(rq);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700594 int nseg = blk_rq_nr_phys_segments(rq);
Christoph Hellwigb131c612017-01-13 12:29:12 +0100595 unsigned int size = blk_rq_payload_bytes(rq);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500596
Minwoo Im955b1b52017-12-20 16:30:50 +0900597 iod->use_sgl = nvme_pci_use_sgls(dev, rq);
598
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100599 if (nseg > NVME_INT_PAGES || size > NVME_INT_BYTES(dev)) {
Jens Axboe943e9422018-06-21 09:49:37 -0600600 iod->sg = mempool_alloc(dev->iod_mempool, GFP_ATOMIC);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100601 if (!iod->sg)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200602 return BLK_STS_RESOURCE;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100603 } else {
604 iod->sg = iod->inline_sg;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700605 }
606
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100607 iod->aborted = 0;
608 iod->npages = -1;
609 iod->nents = 0;
610 iod->length = size;
Keith Buschf80ec962016-07-12 16:20:31 -0700611
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200612 return BLK_STS_OK;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700613}
614
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100615static void nvme_free_iod(struct nvme_dev *dev, struct request *req)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500616{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100617 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700618 const int last_prp = dev->ctrl.page_size / sizeof(__le64) - 1;
619 dma_addr_t dma_addr = iod->first_dma, next_dma_addr;
620
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500621 int i;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500622
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500623 if (iod->npages == 0)
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700624 dma_pool_free(dev->prp_small_pool, nvme_pci_iod_list(req)[0],
625 dma_addr);
626
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500627 for (i = 0; i < iod->npages; i++) {
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700628 void *addr = nvme_pci_iod_list(req)[i];
629
630 if (iod->use_sgl) {
631 struct nvme_sgl_desc *sg_list = addr;
632
633 next_dma_addr =
634 le64_to_cpu((sg_list[SGES_PER_PAGE - 1]).addr);
635 } else {
636 __le64 *prp_list = addr;
637
638 next_dma_addr = le64_to_cpu(prp_list[last_prp]);
639 }
640
641 dma_pool_free(dev->prp_page_pool, addr, dma_addr);
642 dma_addr = next_dma_addr;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500643 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700644
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100645 if (iod->sg != iod->inline_sg)
Jens Axboe943e9422018-06-21 09:49:37 -0600646 mempool_free(iod->sg, dev->iod_mempool);
Keith Buschb4ff9c82014-08-29 09:06:12 -0600647}
648
Keith Buschd0877472017-09-15 13:05:38 -0400649static void nvme_print_sgl(struct scatterlist *sgl, int nents)
650{
651 int i;
652 struct scatterlist *sg;
653
654 for_each_sg(sgl, sg, nents, i) {
655 dma_addr_t phys = sg_phys(sg);
656 pr_warn("sg[%d] phys_addr:%pad offset:%d length:%d "
657 "dma_address:%pad dma_length:%d\n",
658 i, &phys, sg->offset, sg->length, &sg_dma_address(sg),
659 sg_dma_len(sg));
660 }
661}
662
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700663static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev,
664 struct request *req, struct nvme_rw_command *cmnd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500665{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100666 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500667 struct dma_pool *pool;
Christoph Hellwigb131c612017-01-13 12:29:12 +0100668 int length = blk_rq_payload_bytes(req);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500669 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500670 int dma_len = sg_dma_len(sg);
671 u64 dma_addr = sg_dma_address(sg);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100672 u32 page_size = dev->ctrl.page_size;
Murali Iyerf137e0f2015-03-26 11:07:51 -0500673 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500674 __le64 *prp_list;
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700675 void **list = nvme_pci_iod_list(req);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500676 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500677 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500678
Keith Busch1d090622014-06-23 11:34:01 -0600679 length -= (page_size - offset);
Jan H. Schönherr5228b322017-08-27 15:56:37 +0200680 if (length <= 0) {
681 iod->first_dma = 0;
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700682 goto done;
Jan H. Schönherr5228b322017-08-27 15:56:37 +0200683 }
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500684
Keith Busch1d090622014-06-23 11:34:01 -0600685 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500686 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600687 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500688 } else {
689 sg = sg_next(sg);
690 dma_addr = sg_dma_address(sg);
691 dma_len = sg_dma_len(sg);
692 }
693
Keith Busch1d090622014-06-23 11:34:01 -0600694 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600695 iod->first_dma = dma_addr;
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700696 goto done;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500697 }
698
Keith Busch1d090622014-06-23 11:34:01 -0600699 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500700 if (nprps <= (256 / 8)) {
701 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500702 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500703 } else {
704 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500705 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500706 }
707
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200708 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400709 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600710 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500711 iod->npages = -1;
Keith Busch86eea282017-07-12 15:59:07 -0400712 return BLK_STS_RESOURCE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400713 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500714 list[0] = prp_list;
715 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500716 i = 0;
717 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600718 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500719 __le64 *old_prp_list = prp_list;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200720 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500721 if (!prp_list)
Keith Busch86eea282017-07-12 15:59:07 -0400722 return BLK_STS_RESOURCE;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500723 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400724 prp_list[0] = old_prp_list[i - 1];
725 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
726 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500727 }
728 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600729 dma_len -= page_size;
730 dma_addr += page_size;
731 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500732 if (length <= 0)
733 break;
734 if (dma_len > 0)
735 continue;
Keith Busch86eea282017-07-12 15:59:07 -0400736 if (unlikely(dma_len < 0))
737 goto bad_sgl;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500738 sg = sg_next(sg);
739 dma_addr = sg_dma_address(sg);
740 dma_len = sg_dma_len(sg);
741 }
742
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700743done:
744 cmnd->dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
745 cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma);
746
Keith Busch86eea282017-07-12 15:59:07 -0400747 return BLK_STS_OK;
748
749 bad_sgl:
Keith Buschd0877472017-09-15 13:05:38 -0400750 WARN(DO_ONCE(nvme_print_sgl, iod->sg, iod->nents),
751 "Invalid SGL for payload:%d nents:%d\n",
752 blk_rq_payload_bytes(req), iod->nents);
Keith Busch86eea282017-07-12 15:59:07 -0400753 return BLK_STS_IOERR;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500754}
755
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700756static void nvme_pci_sgl_set_data(struct nvme_sgl_desc *sge,
757 struct scatterlist *sg)
758{
759 sge->addr = cpu_to_le64(sg_dma_address(sg));
760 sge->length = cpu_to_le32(sg_dma_len(sg));
761 sge->type = NVME_SGL_FMT_DATA_DESC << 4;
762}
763
764static void nvme_pci_sgl_set_seg(struct nvme_sgl_desc *sge,
765 dma_addr_t dma_addr, int entries)
766{
767 sge->addr = cpu_to_le64(dma_addr);
768 if (entries < SGES_PER_PAGE) {
769 sge->length = cpu_to_le32(entries * sizeof(*sge));
770 sge->type = NVME_SGL_FMT_LAST_SEG_DESC << 4;
771 } else {
772 sge->length = cpu_to_le32(PAGE_SIZE);
773 sge->type = NVME_SGL_FMT_SEG_DESC << 4;
774 }
775}
776
777static blk_status_t nvme_pci_setup_sgls(struct nvme_dev *dev,
Christoph Hellwigb0f28532018-01-17 22:04:38 +0100778 struct request *req, struct nvme_rw_command *cmd, int entries)
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700779{
780 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700781 struct dma_pool *pool;
782 struct nvme_sgl_desc *sg_list;
783 struct scatterlist *sg = iod->sg;
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700784 dma_addr_t sgl_dma;
Christoph Hellwigb0f28532018-01-17 22:04:38 +0100785 int i = 0;
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700786
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700787 /* setting the transfer type as SGL */
788 cmd->flags = NVME_CMD_SGL_METABUF;
789
Christoph Hellwigb0f28532018-01-17 22:04:38 +0100790 if (entries == 1) {
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700791 nvme_pci_sgl_set_data(&cmd->dptr.sgl, sg);
792 return BLK_STS_OK;
793 }
794
795 if (entries <= (256 / sizeof(struct nvme_sgl_desc))) {
796 pool = dev->prp_small_pool;
797 iod->npages = 0;
798 } else {
799 pool = dev->prp_page_pool;
800 iod->npages = 1;
801 }
802
803 sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma);
804 if (!sg_list) {
805 iod->npages = -1;
806 return BLK_STS_RESOURCE;
807 }
808
809 nvme_pci_iod_list(req)[0] = sg_list;
810 iod->first_dma = sgl_dma;
811
812 nvme_pci_sgl_set_seg(&cmd->dptr.sgl, sgl_dma, entries);
813
814 do {
815 if (i == SGES_PER_PAGE) {
816 struct nvme_sgl_desc *old_sg_desc = sg_list;
817 struct nvme_sgl_desc *link = &old_sg_desc[i - 1];
818
819 sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma);
820 if (!sg_list)
821 return BLK_STS_RESOURCE;
822
823 i = 0;
824 nvme_pci_iod_list(req)[iod->npages++] = sg_list;
825 sg_list[i++] = *link;
826 nvme_pci_sgl_set_seg(link, sgl_dma, entries);
827 }
828
829 nvme_pci_sgl_set_data(&sg_list[i++], sg);
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700830 sg = sg_next(sg);
Christoph Hellwigb0f28532018-01-17 22:04:38 +0100831 } while (--entries > 0);
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700832
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700833 return BLK_STS_OK;
834}
835
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200836static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,
Christoph Hellwigb131c612017-01-13 12:29:12 +0100837 struct nvme_command *cmnd)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200838{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100839 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200840 struct request_queue *q = req->q;
841 enum dma_data_direction dma_dir = rq_data_dir(req) ?
842 DMA_TO_DEVICE : DMA_FROM_DEVICE;
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200843 blk_status_t ret = BLK_STS_IOERR;
Christoph Hellwigb0f28532018-01-17 22:04:38 +0100844 int nr_mapped;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200845
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700846 sg_init_table(iod->sg, blk_rq_nr_phys_segments(req));
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200847 iod->nents = blk_rq_map_sg(q, req, iod->sg);
848 if (!iod->nents)
849 goto out;
850
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200851 ret = BLK_STS_RESOURCE;
Logan Gunthorpee0596ab2018-10-04 15:27:44 -0600852
853 if (is_pci_p2pdma_page(sg_page(iod->sg)))
854 nr_mapped = pci_p2pdma_map_sg(dev->dev, iod->sg, iod->nents,
855 dma_dir);
856 else
857 nr_mapped = dma_map_sg_attrs(dev->dev, iod->sg, iod->nents,
858 dma_dir, DMA_ATTR_NO_WARN);
Christoph Hellwigb0f28532018-01-17 22:04:38 +0100859 if (!nr_mapped)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200860 goto out;
861
Minwoo Im955b1b52017-12-20 16:30:50 +0900862 if (iod->use_sgl)
Christoph Hellwigb0f28532018-01-17 22:04:38 +0100863 ret = nvme_pci_setup_sgls(dev, req, &cmnd->rw, nr_mapped);
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700864 else
865 ret = nvme_pci_setup_prps(dev, req, &cmnd->rw);
866
Keith Busch86eea282017-07-12 15:59:07 -0400867 if (ret != BLK_STS_OK)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200868 goto out_unmap;
869
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200870 ret = BLK_STS_IOERR;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200871 if (blk_integrity_rq(req)) {
872 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
873 goto out_unmap;
874
Christoph Hellwigbf684052015-10-26 17:12:51 +0900875 sg_init_table(&iod->meta_sg, 1);
876 if (blk_rq_map_integrity_sg(q, req->bio, &iod->meta_sg) != 1)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200877 goto out_unmap;
878
Christoph Hellwigbf684052015-10-26 17:12:51 +0900879 if (!dma_map_sg(dev->dev, &iod->meta_sg, 1, dma_dir))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200880 goto out_unmap;
Chaitanya Kulkarni3045c0d2018-10-17 11:34:15 -0700881
882 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(&iod->meta_sg));
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200883 }
884
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200885 return BLK_STS_OK;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200886
887out_unmap:
888 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
889out:
890 return ret;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200891}
892
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100893static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100894{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100895 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100896 enum dma_data_direction dma_dir = rq_data_dir(req) ?
897 DMA_TO_DEVICE : DMA_FROM_DEVICE;
898
899 if (iod->nents) {
Logan Gunthorpee0596ab2018-10-04 15:27:44 -0600900 /* P2PDMA requests do not need to be unmapped */
901 if (!is_pci_p2pdma_page(sg_page(iod->sg)))
902 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
903
Max Gurtovoyf7f1fc32018-07-30 00:15:33 +0300904 if (blk_integrity_rq(req))
Christoph Hellwigbf684052015-10-26 17:12:51 +0900905 dma_unmap_sg(dev->dev, &iod->meta_sg, 1, dma_dir);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100906 }
907
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700908 nvme_cleanup_cmd(req);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100909 nvme_free_iod(dev, req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500910}
911
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700912/*
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200913 * NOTE: ns is NULL when called on the admin queue.
914 */
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200915static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700916 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600917{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700918 struct nvme_ns *ns = hctx->queue->queuedata;
919 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200920 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700921 struct request *req = bd->rq;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200922 struct nvme_command cmnd;
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200923 blk_status_t ret;
Keith Busche1e5e562015-02-19 13:39:03 -0700924
Jens Axboed1f06f42018-05-17 18:31:49 +0200925 /*
926 * We should not need to do this, but we're still using this to
927 * ensure we can drain requests on a dying queue.
928 */
Christoph Hellwig4e224102018-12-02 17:46:17 +0100929 if (unlikely(!test_bit(NVMEQ_ENABLED, &nvmeq->flags)))
Jens Axboed1f06f42018-05-17 18:31:49 +0200930 return BLK_STS_IOERR;
931
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700932 ret = nvme_setup_cmd(ns, req, &cmnd);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200933 if (ret)
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100934 return ret;
Keith Buschedd10d32014-04-03 16:45:23 -0600935
Christoph Hellwigb131c612017-01-13 12:29:12 +0100936 ret = nvme_init_iod(req, dev);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200937 if (ret)
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700938 goto out_free_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600939
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200940 if (blk_rq_nr_phys_segments(req)) {
Christoph Hellwigb131c612017-01-13 12:29:12 +0100941 ret = nvme_map_data(dev, req, &cmnd);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200942 if (ret)
943 goto out_cleanup_iod;
944 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700945
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100946 blk_mq_start_request(req);
Jens Axboe04f3eaf2018-11-29 10:02:29 -0700947 nvme_submit_cmd(nvmeq, &cmnd, bd->last);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200948 return BLK_STS_OK;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700949out_cleanup_iod:
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100950 nvme_free_iod(dev, req);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700951out_free_cmd:
952 nvme_cleanup_cmd(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200953 return ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500954}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500955
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200956static void nvme_pci_complete_rq(struct request *req)
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100957{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100958 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100959
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200960 nvme_unmap_data(iod->nvmeq->dev, req);
961 nvme_complete_rq(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500962}
963
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100964/* We read the CQE phase first to check if the rest of the entry is valid */
Christoph Hellwig750dde42018-05-18 08:37:04 -0600965static inline bool nvme_cqe_pending(struct nvme_queue *nvmeq)
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100966{
Christoph Hellwig750dde42018-05-18 08:37:04 -0600967 return (le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
968 nvmeq->cq_phase;
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100969}
970
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300971static inline void nvme_ring_cq_doorbell(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500972{
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300973 u16 head = nvmeq->cq_head;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500974
Keith Busch397c6992018-06-06 08:13:05 -0600975 if (nvme_dbbuf_update_and_check_event(head, nvmeq->dbbuf_cq_db,
976 nvmeq->dbbuf_cq_ei))
977 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300978}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500979
Jens Axboe5cb525c2018-05-17 18:31:50 +0200980static inline void nvme_handle_cqe(struct nvme_queue *nvmeq, u16 idx)
Sagi Grimberg83a12fb2017-06-18 17:28:08 +0300981{
Jens Axboe5cb525c2018-05-17 18:31:50 +0200982 volatile struct nvme_completion *cqe = &nvmeq->cqes[idx];
Sagi Grimberg83a12fb2017-06-18 17:28:08 +0300983 struct request *req;
984
985 if (unlikely(cqe->command_id >= nvmeq->q_depth)) {
986 dev_warn(nvmeq->dev->ctrl.device,
987 "invalid id %d completed on queue %d\n",
988 cqe->command_id, le16_to_cpu(cqe->sq_id));
989 return;
990 }
991
992 /*
993 * AEN requests are special as they don't time out and can
994 * survive any kind of queue freeze and often don't respond to
995 * aborts. We don't even bother to allocate a struct request
996 * for them but rather special case them here.
997 */
998 if (unlikely(nvmeq->qid == 0 &&
Keith Busch38dabe22017-11-07 15:13:10 -0700999 cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH)) {
Sagi Grimberg83a12fb2017-06-18 17:28:08 +03001000 nvme_complete_async_event(&nvmeq->dev->ctrl,
1001 cqe->status, &cqe->result);
1002 return;
1003 }
1004
1005 req = blk_mq_tag_to_rq(*nvmeq->tags, cqe->command_id);
1006 nvme_end_request(req, cqe->status, cqe->result);
1007}
1008
Jens Axboe5cb525c2018-05-17 18:31:50 +02001009static void nvme_complete_cqes(struct nvme_queue *nvmeq, u16 start, u16 end)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001010{
Jens Axboe5cb525c2018-05-17 18:31:50 +02001011 while (start != end) {
1012 nvme_handle_cqe(nvmeq, start);
1013 if (++start == nvmeq->q_depth)
1014 start = 0;
Sagi Grimberg920d13a2017-06-18 17:28:09 +03001015 }
Jens Axboea0fa9642015-11-03 20:37:26 -07001016}
1017
Jens Axboe5cb525c2018-05-17 18:31:50 +02001018static inline void nvme_update_cq_head(struct nvme_queue *nvmeq)
Jens Axboea0fa9642015-11-03 20:37:26 -07001019{
Jens Axboe5cb525c2018-05-17 18:31:50 +02001020 if (++nvmeq->cq_head == nvmeq->q_depth) {
1021 nvmeq->cq_head = 0;
1022 nvmeq->cq_phase = !nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001023 }
Jens Axboe5cb525c2018-05-17 18:31:50 +02001024}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001025
Jens Axboe1052b8a2018-11-26 08:21:49 -07001026static inline int nvme_process_cq(struct nvme_queue *nvmeq, u16 *start,
1027 u16 *end, unsigned int tag)
Jens Axboe5cb525c2018-05-17 18:31:50 +02001028{
Jens Axboe1052b8a2018-11-26 08:21:49 -07001029 int found = 0;
Jens Axboe5cb525c2018-05-17 18:31:50 +02001030
1031 *start = nvmeq->cq_head;
Jens Axboe1052b8a2018-11-26 08:21:49 -07001032 while (nvme_cqe_pending(nvmeq)) {
1033 if (tag == -1U || nvmeq->cqes[nvmeq->cq_head].command_id == tag)
1034 found++;
Jens Axboe5cb525c2018-05-17 18:31:50 +02001035 nvme_update_cq_head(nvmeq);
1036 }
1037 *end = nvmeq->cq_head;
1038
1039 if (*start != *end)
Sagi Grimberg920d13a2017-06-18 17:28:09 +03001040 nvme_ring_cq_doorbell(nvmeq);
Jens Axboe5cb525c2018-05-17 18:31:50 +02001041 return found;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001042}
1043
1044static irqreturn_t nvme_irq(int irq, void *data)
1045{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001046 struct nvme_queue *nvmeq = data;
Jens Axboe68fa9db2018-05-21 08:41:52 -06001047 irqreturn_t ret = IRQ_NONE;
Jens Axboe5cb525c2018-05-17 18:31:50 +02001048 u16 start, end;
1049
Christoph Hellwig3a7afd82018-12-02 17:46:23 +01001050 /*
1051 * The rmb/wmb pair ensures we see all updates from a previous run of
1052 * the irq handler, even if that was on another CPU.
1053 */
1054 rmb();
Jens Axboe68fa9db2018-05-21 08:41:52 -06001055 if (nvmeq->cq_head != nvmeq->last_cq_head)
1056 ret = IRQ_HANDLED;
Jens Axboe5cb525c2018-05-17 18:31:50 +02001057 nvme_process_cq(nvmeq, &start, &end, -1);
Jens Axboe68fa9db2018-05-21 08:41:52 -06001058 nvmeq->last_cq_head = nvmeq->cq_head;
Christoph Hellwig3a7afd82018-12-02 17:46:23 +01001059 wmb();
Jens Axboe5cb525c2018-05-17 18:31:50 +02001060
Jens Axboe68fa9db2018-05-21 08:41:52 -06001061 if (start != end) {
1062 nvme_complete_cqes(nvmeq, start, end);
1063 return IRQ_HANDLED;
1064 }
1065
1066 return ret;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001067}
1068
1069static irqreturn_t nvme_irq_check(int irq, void *data)
1070{
1071 struct nvme_queue *nvmeq = data;
Christoph Hellwig750dde42018-05-18 08:37:04 -06001072 if (nvme_cqe_pending(nvmeq))
Marta Rybczynskad783e0b2016-03-22 16:02:06 +01001073 return IRQ_WAKE_THREAD;
1074 return IRQ_NONE;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001075}
1076
Christoph Hellwig0b2a8a92018-12-02 17:46:20 +01001077/*
1078 * Poll for completions any queue, including those not dedicated to polling.
1079 * Can be called from any context.
1080 */
1081static int nvme_poll_irqdisable(struct nvme_queue *nvmeq, unsigned int tag)
Jens Axboea0fa9642015-11-03 20:37:26 -07001082{
Christoph Hellwig3a7afd82018-12-02 17:46:23 +01001083 struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev);
Jens Axboe5cb525c2018-05-17 18:31:50 +02001084 u16 start, end;
Jens Axboe1052b8a2018-11-26 08:21:49 -07001085 int found;
Jens Axboea0fa9642015-11-03 20:37:26 -07001086
Christoph Hellwig3a7afd82018-12-02 17:46:23 +01001087 /*
1088 * For a poll queue we need to protect against the polling thread
1089 * using the CQ lock. For normal interrupt driven threads we have
1090 * to disable the interrupt to avoid racing with it.
1091 */
1092 if (nvmeq->cq_vector == -1)
1093 spin_lock(&nvmeq->cq_poll_lock);
1094 else
1095 disable_irq(pci_irq_vector(pdev, nvmeq->cq_vector));
Jens Axboe5cb525c2018-05-17 18:31:50 +02001096 found = nvme_process_cq(nvmeq, &start, &end, tag);
Christoph Hellwig3a7afd82018-12-02 17:46:23 +01001097 if (nvmeq->cq_vector == -1)
1098 spin_unlock(&nvmeq->cq_poll_lock);
1099 else
1100 enable_irq(pci_irq_vector(pdev, nvmeq->cq_vector));
Sagi Grimberg442e19b2017-06-18 17:28:10 +03001101
Jens Axboe5cb525c2018-05-17 18:31:50 +02001102 nvme_complete_cqes(nvmeq, start, end);
Sagi Grimberg442e19b2017-06-18 17:28:10 +03001103 return found;
Jens Axboea0fa9642015-11-03 20:37:26 -07001104}
1105
Jens Axboe97431392018-11-16 09:48:21 -07001106static int nvme_poll(struct blk_mq_hw_ctx *hctx)
Keith Busch7776db12017-02-24 17:59:28 -05001107{
1108 struct nvme_queue *nvmeq = hctx->driver_data;
Jens Axboedabcefa2018-11-14 09:38:28 -07001109 u16 start, end;
1110 bool found;
1111
1112 if (!nvme_cqe_pending(nvmeq))
1113 return 0;
1114
Christoph Hellwig3a7afd82018-12-02 17:46:23 +01001115 spin_lock(&nvmeq->cq_poll_lock);
Jens Axboe97431392018-11-16 09:48:21 -07001116 found = nvme_process_cq(nvmeq, &start, &end, -1);
Christoph Hellwig3a7afd82018-12-02 17:46:23 +01001117 spin_unlock(&nvmeq->cq_poll_lock);
Jens Axboedabcefa2018-11-14 09:38:28 -07001118
1119 nvme_complete_cqes(nvmeq, start, end);
1120 return found;
1121}
1122
Keith Buschad22c352017-11-07 15:13:12 -07001123static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001124{
Christoph Hellwigf866fc422016-04-26 13:52:00 +02001125 struct nvme_dev *dev = to_nvme_dev(ctrl);
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001126 struct nvme_queue *nvmeq = &dev->queues[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001127 struct nvme_command c;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001128
1129 memset(&c, 0, sizeof(c));
1130 c.common.opcode = nvme_admin_async_event;
Keith Buschad22c352017-11-07 15:13:12 -07001131 c.common.command_id = NVME_AQ_BLK_MQ_DEPTH;
Jens Axboe04f3eaf2018-11-29 10:02:29 -07001132 nvme_submit_cmd(nvmeq, &c, true);
Keith Busch4d115422013-12-10 13:10:40 -07001133}
1134
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001135static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1136{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001137 struct nvme_command c;
1138
1139 memset(&c, 0, sizeof(c));
1140 c.delete_queue.opcode = opcode;
1141 c.delete_queue.qid = cpu_to_le16(id);
1142
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001143 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001144}
1145
1146static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
Jianchao Wanga8e3e0b2018-05-24 17:51:33 +08001147 struct nvme_queue *nvmeq, s16 vector)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001148{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001149 struct nvme_command c;
Jens Axboe4b04cc62018-11-05 12:44:33 -07001150 int flags = NVME_QUEUE_PHYS_CONTIG;
1151
1152 if (vector != -1)
1153 flags |= NVME_CQ_IRQ_ENABLED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001154
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001155 /*
Minwoo Im16772ae2017-10-18 22:56:09 +09001156 * Note: we (ab)use the fact that the prp fields survive if no data
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001157 * is attached to the request.
1158 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001159 memset(&c, 0, sizeof(c));
1160 c.create_cq.opcode = nvme_admin_create_cq;
1161 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1162 c.create_cq.cqid = cpu_to_le16(qid);
1163 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1164 c.create_cq.cq_flags = cpu_to_le16(flags);
Jens Axboe4b04cc62018-11-05 12:44:33 -07001165 if (vector != -1)
1166 c.create_cq.irq_vector = cpu_to_le16(vector);
1167 else
1168 c.create_cq.irq_vector = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001169
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001170 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001171}
1172
1173static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1174 struct nvme_queue *nvmeq)
1175{
Jens Axboe9abd68e2018-05-08 10:25:15 -06001176 struct nvme_ctrl *ctrl = &dev->ctrl;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001177 struct nvme_command c;
Keith Busch81c1cd92017-04-04 18:18:12 -04001178 int flags = NVME_QUEUE_PHYS_CONTIG;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001179
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001180 /*
Jens Axboe9abd68e2018-05-08 10:25:15 -06001181 * Some drives have a bug that auto-enables WRRU if MEDIUM isn't
1182 * set. Since URGENT priority is zeroes, it makes all queues
1183 * URGENT.
1184 */
1185 if (ctrl->quirks & NVME_QUIRK_MEDIUM_PRIO_SQ)
1186 flags |= NVME_SQ_PRIO_MEDIUM;
1187
1188 /*
Minwoo Im16772ae2017-10-18 22:56:09 +09001189 * Note: we (ab)use the fact that the prp fields survive if no data
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001190 * is attached to the request.
1191 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001192 memset(&c, 0, sizeof(c));
1193 c.create_sq.opcode = nvme_admin_create_sq;
1194 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1195 c.create_sq.sqid = cpu_to_le16(qid);
1196 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1197 c.create_sq.sq_flags = cpu_to_le16(flags);
1198 c.create_sq.cqid = cpu_to_le16(qid);
1199
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001200 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001201}
1202
1203static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1204{
1205 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1206}
1207
1208static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1209{
1210 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1211}
1212
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001213static void abort_endio(struct request *req, blk_status_t error)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001214{
Christoph Hellwigf4800d62015-11-28 15:43:10 +01001215 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
1216 struct nvme_queue *nvmeq = iod->nvmeq;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001217
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001218 dev_warn(nvmeq->dev->ctrl.device,
1219 "Abort status: 0x%x", nvme_req(req)->status);
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001220 atomic_inc(&nvmeq->dev->ctrl.abort_limit);
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001221 blk_mq_free_request(req);
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001222}
1223
Keith Buschb2a0eb12017-06-07 20:32:50 +02001224static bool nvme_should_reset(struct nvme_dev *dev, u32 csts)
1225{
1226
1227 /* If true, indicates loss of adapter communication, possibly by a
1228 * NVMe Subsystem reset.
1229 */
1230 bool nssro = dev->subsystem && (csts & NVME_CSTS_NSSRO);
1231
Jianchao Wangad700622018-01-22 22:03:16 +08001232 /* If there is a reset/reinit ongoing, we shouldn't reset again. */
1233 switch (dev->ctrl.state) {
1234 case NVME_CTRL_RESETTING:
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02001235 case NVME_CTRL_CONNECTING:
Keith Buschb2a0eb12017-06-07 20:32:50 +02001236 return false;
Jianchao Wangad700622018-01-22 22:03:16 +08001237 default:
1238 break;
1239 }
Keith Buschb2a0eb12017-06-07 20:32:50 +02001240
1241 /* We shouldn't reset unless the controller is on fatal error state
1242 * _or_ if we lost the communication with it.
1243 */
1244 if (!(csts & NVME_CSTS_CFS) && !nssro)
1245 return false;
1246
Keith Buschb2a0eb12017-06-07 20:32:50 +02001247 return true;
1248}
1249
1250static void nvme_warn_reset(struct nvme_dev *dev, u32 csts)
1251{
1252 /* Read a config register to help see what died. */
1253 u16 pci_status;
1254 int result;
1255
1256 result = pci_read_config_word(to_pci_dev(dev->dev), PCI_STATUS,
1257 &pci_status);
1258 if (result == PCIBIOS_SUCCESSFUL)
1259 dev_warn(dev->ctrl.device,
1260 "controller is down; will reset: CSTS=0x%x, PCI_STATUS=0x%hx\n",
1261 csts, pci_status);
1262 else
1263 dev_warn(dev->ctrl.device,
1264 "controller is down; will reset: CSTS=0x%x, PCI_STATUS read failed (%d)\n",
1265 csts, result);
1266}
1267
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001268static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001269{
Christoph Hellwigf4800d62015-11-28 15:43:10 +01001270 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
1271 struct nvme_queue *nvmeq = iod->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001272 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001273 struct request *abort_req;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001274 struct nvme_command cmd;
Keith Buschb2a0eb12017-06-07 20:32:50 +02001275 u32 csts = readl(dev->bar + NVME_REG_CSTS);
1276
Wen Xiong651438b2018-02-15 14:05:10 -06001277 /* If PCI error recovery process is happening, we cannot reset or
1278 * the recovery mechanism will surely fail.
1279 */
1280 mb();
1281 if (pci_channel_offline(to_pci_dev(dev->dev)))
1282 return BLK_EH_RESET_TIMER;
1283
Keith Buschb2a0eb12017-06-07 20:32:50 +02001284 /*
1285 * Reset immediately if the controller is failed
1286 */
1287 if (nvme_should_reset(dev, csts)) {
1288 nvme_warn_reset(dev, csts);
1289 nvme_dev_disable(dev, false);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001290 nvme_reset_ctrl(&dev->ctrl);
Christoph Hellwigdb8c48e2018-05-29 15:52:30 +02001291 return BLK_EH_DONE;
Keith Buschb2a0eb12017-06-07 20:32:50 +02001292 }
Keith Buschc30341d2013-12-10 13:10:38 -07001293
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001294 /*
Keith Busch7776db12017-02-24 17:59:28 -05001295 * Did we miss an interrupt?
1296 */
Christoph Hellwig0b2a8a92018-12-02 17:46:20 +01001297 if (nvme_poll_irqdisable(nvmeq, req->tag)) {
Keith Busch7776db12017-02-24 17:59:28 -05001298 dev_warn(dev->ctrl.device,
1299 "I/O %d QID %d timeout, completion polled\n",
1300 req->tag, nvmeq->qid);
Christoph Hellwigdb8c48e2018-05-29 15:52:30 +02001301 return BLK_EH_DONE;
Keith Busch7776db12017-02-24 17:59:28 -05001302 }
1303
1304 /*
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001305 * Shutdown immediately if controller times out while starting. The
1306 * reset work will see the pci device disabled when it gets the forced
1307 * cancellation error. All outstanding requests are completed on
Christoph Hellwigdb8c48e2018-05-29 15:52:30 +02001308 * shutdown, so we return BLK_EH_DONE.
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001309 */
Keith Busch42441402018-02-08 08:55:34 -07001310 switch (dev->ctrl.state) {
1311 case NVME_CTRL_CONNECTING:
1312 case NVME_CTRL_RESETTING:
Keith Buschb9cac432018-05-24 14:34:55 -06001313 dev_warn_ratelimited(dev->ctrl.device,
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001314 "I/O %d QID %d timeout, disable controller\n",
1315 req->tag, nvmeq->qid);
Keith Buscha5cdb682016-01-12 14:41:18 -07001316 nvme_dev_disable(dev, false);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001317 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
Christoph Hellwigdb8c48e2018-05-29 15:52:30 +02001318 return BLK_EH_DONE;
Keith Busch42441402018-02-08 08:55:34 -07001319 default:
1320 break;
Keith Buschc30341d2013-12-10 13:10:38 -07001321 }
1322
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001323 /*
1324 * Shutdown the controller immediately and schedule a reset if the
1325 * command was already aborted once before and still hasn't been
1326 * returned to the driver, or if this is the admin queue.
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001327 */
Christoph Hellwigf4800d62015-11-28 15:43:10 +01001328 if (!nvmeq->qid || iod->aborted) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001329 dev_warn(dev->ctrl.device,
Keith Busche1569a12015-11-26 12:11:07 +01001330 "I/O %d QID %d timeout, reset controller\n",
1331 req->tag, nvmeq->qid);
Keith Buscha5cdb682016-01-12 14:41:18 -07001332 nvme_dev_disable(dev, false);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001333 nvme_reset_ctrl(&dev->ctrl);
Keith Buschc30341d2013-12-10 13:10:38 -07001334
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001335 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
Christoph Hellwigdb8c48e2018-05-29 15:52:30 +02001336 return BLK_EH_DONE;
Keith Buschc30341d2013-12-10 13:10:38 -07001337 }
Keith Buschc30341d2013-12-10 13:10:38 -07001338
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001339 if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) {
1340 atomic_inc(&dev->ctrl.abort_limit);
1341 return BLK_EH_RESET_TIMER;
1342 }
Keith Busch7bf7d772017-01-24 18:07:00 -05001343 iod->aborted = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001344
Keith Buschc30341d2013-12-10 13:10:38 -07001345 memset(&cmd, 0, sizeof(cmd));
1346 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001347 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001348 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Keith Buschc30341d2013-12-10 13:10:38 -07001349
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001350 dev_warn(nvmeq->dev->ctrl.device,
1351 "I/O %d QID %d timeout, aborting\n",
1352 req->tag, nvmeq->qid);
Keith Buschc30341d2013-12-10 13:10:38 -07001353
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001354 abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd,
Christoph Hellwigeb71f432016-06-13 16:45:23 +02001355 BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001356 if (IS_ERR(abort_req)) {
1357 atomic_inc(&dev->ctrl.abort_limit);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001358 return BLK_EH_RESET_TIMER;
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001359 }
Keith Buschc30341d2013-12-10 13:10:38 -07001360
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001361 abort_req->timeout = ADMIN_TIMEOUT;
1362 abort_req->end_io_data = NULL;
1363 blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio);
Keith Busch07836e62015-02-19 10:34:48 -07001364
Keith Busch7a509a62015-01-07 18:55:53 -07001365 /*
1366 * The aborted req will be completed on receiving the abort req.
1367 * We enable the timer again. If hit twice, it'll cause a device reset,
1368 * as the device then is in a faulty state.
1369 */
Keith Busch07836e62015-02-19 10:34:48 -07001370 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001371}
1372
Keith Buschf435c282014-07-07 09:14:42 -06001373static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001374{
1375 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1376 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Christoph Hellwig63223072018-12-02 17:46:18 +01001377 if (!nvmeq->sq_cmds)
1378 return;
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001379
Christoph Hellwig63223072018-12-02 17:46:18 +01001380 if (test_and_clear_bit(NVMEQ_SQ_CMB, &nvmeq->flags)) {
1381 pci_free_p2pmem(to_pci_dev(nvmeq->q_dmadev),
1382 nvmeq->sq_cmds, SQ_SIZE(nvmeq->q_depth));
1383 } else {
1384 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1385 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001386 }
Matthew Wilcox9e866772012-08-03 13:55:56 -04001387}
1388
Keith Buscha1a5ef92013-12-16 13:50:00 -05001389static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001390{
1391 int i;
1392
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001393 for (i = dev->ctrl.queue_count - 1; i >= lowest; i--) {
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001394 dev->ctrl.queue_count--;
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001395 nvme_free_queue(&dev->queues[i]);
kaoudis121c7ad2015-01-14 21:01:58 -07001396 }
Keith Busch22404272013-07-15 15:02:20 -06001397}
1398
Keith Busch4d115422013-12-10 13:10:40 -07001399/**
1400 * nvme_suspend_queue - put queue into suspended state
Bart Van Assche40581d12018-10-08 14:28:43 -07001401 * @nvmeq: queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001402 */
1403static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001404{
Christoph Hellwig4e224102018-12-02 17:46:17 +01001405 if (!test_and_clear_bit(NVMEQ_ENABLED, &nvmeq->flags))
Keith Busch2b25d982014-12-22 12:59:04 -07001406 return 1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001407
Christoph Hellwig4e224102018-12-02 17:46:17 +01001408 /* ensure that nvme_queue_rq() sees NVMEQ_ENABLED cleared */
Jens Axboed1f06f42018-05-17 18:31:49 +02001409 mb();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001410
Christoph Hellwig4e224102018-12-02 17:46:17 +01001411 nvmeq->dev->online_queues--;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001412 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
Sagi Grimbergc81545f2017-07-02 15:53:27 +03001413 blk_mq_quiesce_queue(nvmeq->dev->ctrl.admin_q);
Christoph Hellwig4e224102018-12-02 17:46:17 +01001414 if (nvmeq->cq_vector == -1)
1415 return 0;
1416 pci_free_irq(to_pci_dev(nvmeq->dev->dev), nvmeq->cq_vector, nvmeq);
1417 nvmeq->cq_vector = -1;
Keith Busch4d115422013-12-10 13:10:40 -07001418 return 0;
1419}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001420
Keith Buscha5cdb682016-01-12 14:41:18 -07001421static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown)
Keith Busch4d115422013-12-10 13:10:40 -07001422{
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001423 struct nvme_queue *nvmeq = &dev->queues[0];
Keith Busch4d115422013-12-10 13:10:40 -07001424
Keith Buscha5cdb682016-01-12 14:41:18 -07001425 if (shutdown)
1426 nvme_shutdown_ctrl(&dev->ctrl);
1427 else
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001428 nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
Keith Busch07836e62015-02-19 10:34:48 -07001429
Christoph Hellwig0b2a8a92018-12-02 17:46:20 +01001430 nvme_poll_irqdisable(nvmeq, -1);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001431}
1432
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001433static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1434 int entry_size)
1435{
1436 int q_depth = dev->q_depth;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001437 unsigned q_size_aligned = roundup(q_depth * entry_size,
1438 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001439
1440 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001441 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001442 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
Jon Derrickc45f5c92015-07-21 15:08:13 -06001443 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001444
1445 /*
1446 * Ensure the reduced q_depth is above some threshold where it
1447 * would be better to map queues in system memory with the
1448 * original depth
1449 */
1450 if (q_depth < 64)
1451 return -ENOMEM;
1452 }
1453
1454 return q_depth;
1455}
1456
1457static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1458 int qid, int depth)
1459{
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001460 struct pci_dev *pdev = to_pci_dev(dev->dev);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001461
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001462 if (qid && dev->cmb_use_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) {
1463 nvmeq->sq_cmds = pci_alloc_p2pmem(pdev, SQ_SIZE(depth));
1464 nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
1465 nvmeq->sq_cmds);
Christoph Hellwig63223072018-12-02 17:46:18 +01001466 if (nvmeq->sq_dma_addr) {
1467 set_bit(NVMEQ_SQ_CMB, &nvmeq->flags);
1468 return 0;
1469 }
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001470 }
1471
Christoph Hellwig63223072018-12-02 17:46:18 +01001472 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1473 &nvmeq->sq_dma_addr, GFP_KERNEL);
Keith Busch815c6702018-02-13 05:44:44 -07001474 if (!nvmeq->sq_cmds)
1475 return -ENOMEM;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001476 return 0;
1477}
1478
Keith Buscha6ff7262018-04-12 09:16:09 -06001479static int nvme_alloc_queue(struct nvme_dev *dev, int qid, int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001480{
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001481 struct nvme_queue *nvmeq = &dev->queues[qid];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001482
Keith Busch62314e42018-01-23 09:16:19 -07001483 if (dev->ctrl.queue_count > qid)
1484 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001485
Christoph Hellwige75ec752015-05-22 11:12:39 +02001486 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001487 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001488 if (!nvmeq->cqes)
1489 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001490
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001491 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001492 goto free_cqdma;
1493
Christoph Hellwige75ec752015-05-22 11:12:39 +02001494 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001495 nvmeq->dev = dev;
Jens Axboe1ab0cd62018-05-17 18:31:51 +02001496 spin_lock_init(&nvmeq->sq_lock);
Christoph Hellwig3a7afd82018-12-02 17:46:23 +01001497 spin_lock_init(&nvmeq->cq_poll_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001498 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001499 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001500 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001501 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001502 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001503 nvmeq->cq_vector = -1;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001504 dev->ctrl.queue_count++;
Jon Derrick36a7e992015-05-27 12:26:23 -06001505
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001506 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001507
1508 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001509 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001510 nvmeq->cq_dma_addr);
1511 free_nvmeq:
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001512 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001513}
1514
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001515static int queue_request_irq(struct nvme_queue *nvmeq)
Matthew Wilcox30010822011-01-20 09:10:15 -05001516{
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001517 struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev);
1518 int nr = nvmeq->dev->ctrl.instance;
1519
1520 if (use_threaded_interrupts) {
1521 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq_check,
1522 nvme_irq, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1523 } else {
1524 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq,
1525 NULL, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1526 }
Matthew Wilcox30010822011-01-20 09:10:15 -05001527}
1528
Keith Busch22404272013-07-15 15:02:20 -06001529static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001530{
Keith Busch22404272013-07-15 15:02:20 -06001531 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001532
Keith Busch22404272013-07-15 15:02:20 -06001533 nvmeq->sq_tail = 0;
Jens Axboe04f3eaf2018-11-29 10:02:29 -07001534 nvmeq->last_sq_tail = 0;
Keith Busch22404272013-07-15 15:02:20 -06001535 nvmeq->cq_head = 0;
1536 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001537 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001538 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Helen Koikef9f38e32017-04-10 12:51:07 -03001539 nvme_dbbuf_init(dev, nvmeq, qid);
Keith Busch42f61422014-03-24 10:46:25 -06001540 dev->online_queues++;
Christoph Hellwig3a7afd82018-12-02 17:46:23 +01001541 wmb(); /* ensure the first interrupt sees the initialization */
Keith Busch22404272013-07-15 15:02:20 -06001542}
1543
Jens Axboe4b04cc62018-11-05 12:44:33 -07001544static int nvme_create_queue(struct nvme_queue *nvmeq, int qid, bool polled)
Keith Busch22404272013-07-15 15:02:20 -06001545{
1546 struct nvme_dev *dev = nvmeq->dev;
1547 int result;
Jianchao Wanga8e3e0b2018-05-24 17:51:33 +08001548 s16 vector;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001549
Christoph Hellwigd1ed6aa2018-12-02 17:46:22 +01001550 clear_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags);
1551
Keith Busch22b55602018-04-12 09:16:10 -06001552 /*
1553 * A queue's vector matches the queue identifier unless the controller
1554 * has only one vector available.
1555 */
Jens Axboe4b04cc62018-11-05 12:44:33 -07001556 if (!polled)
1557 vector = dev->num_vecs == 1 ? 0 : qid;
1558 else
1559 vector = -1;
1560
Jianchao Wanga8e3e0b2018-05-24 17:51:33 +08001561 result = adapter_alloc_cq(dev, qid, nvmeq, vector);
Keith Buschded45502018-06-06 08:13:06 -06001562 if (result)
1563 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001564
1565 result = adapter_alloc_sq(dev, qid, nvmeq);
1566 if (result < 0)
Keith Buschded45502018-06-06 08:13:06 -06001567 return result;
1568 else if (result)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001569 goto release_cq;
1570
Jianchao Wanga8e3e0b2018-05-24 17:51:33 +08001571 nvmeq->cq_vector = vector;
Keith Busch161b8be2017-09-14 13:54:39 -04001572 nvme_init_queue(nvmeq, qid);
Jens Axboe4b04cc62018-11-05 12:44:33 -07001573
1574 if (vector != -1) {
1575 result = queue_request_irq(nvmeq);
1576 if (result < 0)
1577 goto release_sq;
1578 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001579
Christoph Hellwig4e224102018-12-02 17:46:17 +01001580 set_bit(NVMEQ_ENABLED, &nvmeq->flags);
Keith Busch22404272013-07-15 15:02:20 -06001581 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001582
Jianchao Wanga8e3e0b2018-05-24 17:51:33 +08001583release_sq:
1584 nvmeq->cq_vector = -1;
Jianchao Wangf25a2df2018-02-15 19:13:41 +08001585 dev->online_queues--;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001586 adapter_delete_sq(dev, qid);
Jianchao Wanga8e3e0b2018-05-24 17:51:33 +08001587release_cq:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001588 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001589 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001590}
1591
Eric Biggersf363b082017-03-30 13:39:16 -07001592static const struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001593 .queue_rq = nvme_queue_rq,
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001594 .complete = nvme_pci_complete_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001595 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001596 .exit_hctx = nvme_admin_exit_hctx,
Christoph Hellwig03508152017-06-13 09:15:18 +02001597 .init_request = nvme_init_request,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001598 .timeout = nvme_timeout,
1599};
1600
Eric Biggersf363b082017-03-30 13:39:16 -07001601static const struct blk_mq_ops nvme_mq_ops = {
Christoph Hellwig376f7ef2018-12-02 17:46:27 +01001602 .queue_rq = nvme_queue_rq,
1603 .complete = nvme_pci_complete_rq,
1604 .commit_rqs = nvme_commit_rqs,
1605 .init_hctx = nvme_init_hctx,
1606 .init_request = nvme_init_request,
1607 .map_queues = nvme_pci_map_queues,
1608 .timeout = nvme_timeout,
1609 .poll = nvme_poll,
Jens Axboedabcefa2018-11-14 09:38:28 -07001610};
1611
Keith Buschea191d22015-01-07 18:55:49 -07001612static void nvme_dev_remove_admin(struct nvme_dev *dev)
1613{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001614 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
Keith Busch69d9a992016-02-24 09:15:56 -07001615 /*
1616 * If the controller was reset during removal, it's possible
1617 * user requests may be waiting on a stopped queue. Start the
1618 * queue to flush these to completion.
1619 */
Sagi Grimbergc81545f2017-07-02 15:53:27 +03001620 blk_mq_unquiesce_queue(dev->ctrl.admin_q);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001621 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001622 blk_mq_free_tag_set(&dev->admin_tagset);
1623 }
1624}
1625
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001626static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1627{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001628 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001629 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1630 dev->admin_tagset.nr_hw_queues = 1;
Keith Busche3e9d502016-01-04 09:10:55 -07001631
Keith Busch38dabe22017-11-07 15:13:10 -07001632 dev->admin_tagset.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001633 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001634 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -07001635 dev->admin_tagset.cmd_size = nvme_pci_cmd_size(dev, false);
Jens Axboed3484992017-01-13 14:43:58 -07001636 dev->admin_tagset.flags = BLK_MQ_F_NO_SCHED;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001637 dev->admin_tagset.driver_data = dev;
1638
1639 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1640 return -ENOMEM;
Sagi Grimberg34b6c232017-07-10 09:22:29 +03001641 dev->ctrl.admin_tagset = &dev->admin_tagset;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001642
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001643 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1644 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001645 blk_mq_free_tag_set(&dev->admin_tagset);
1646 return -ENOMEM;
1647 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001648 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001649 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001650 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001651 return -ENODEV;
1652 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001653 } else
Sagi Grimbergc81545f2017-07-02 15:53:27 +03001654 blk_mq_unquiesce_queue(dev->ctrl.admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001655
1656 return 0;
1657}
1658
Xu Yu97f6ef62017-05-24 16:39:55 +08001659static unsigned long db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1660{
1661 return NVME_REG_DBS + ((nr_io_queues + 1) * 8 * dev->db_stride);
1662}
1663
1664static int nvme_remap_bar(struct nvme_dev *dev, unsigned long size)
1665{
1666 struct pci_dev *pdev = to_pci_dev(dev->dev);
1667
1668 if (size <= dev->bar_mapped_size)
1669 return 0;
1670 if (size > pci_resource_len(pdev, 0))
1671 return -ENOMEM;
1672 if (dev->bar)
1673 iounmap(dev->bar);
1674 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1675 if (!dev->bar) {
1676 dev->bar_mapped_size = 0;
1677 return -ENOMEM;
1678 }
1679 dev->bar_mapped_size = size;
1680 dev->dbs = dev->bar + NVME_REG_DBS;
1681
1682 return 0;
1683}
1684
Sagi Grimberg01ad0992017-05-01 00:27:17 +03001685static int nvme_pci_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001686{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001687 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001688 u32 aqa;
1689 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001690
Xu Yu97f6ef62017-05-24 16:39:55 +08001691 result = nvme_remap_bar(dev, db_bar_size(dev, 0));
1692 if (result < 0)
1693 return result;
1694
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001695 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1, 0) ?
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001696 NVME_CAP_NSSRC(dev->ctrl.cap) : 0;
Keith Buschdfbac8c2015-08-10 15:20:40 -06001697
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001698 if (dev->subsystem &&
1699 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1700 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001701
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001702 result = nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001703 if (result < 0)
1704 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001705
Keith Buscha6ff7262018-04-12 09:16:09 -06001706 result = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001707 if (result)
1708 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001709
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001710 nvmeq = &dev->queues[0];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001711 aqa = nvmeq->q_depth - 1;
1712 aqa |= aqa << 16;
1713
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001714 writel(aqa, dev->bar + NVME_REG_AQA);
1715 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1716 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Keith Busch1d090622014-06-23 11:34:01 -06001717
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001718 result = nvme_enable_ctrl(&dev->ctrl, dev->ctrl.cap);
Keith Busch025c5572013-05-01 13:07:51 -06001719 if (result)
Keith Buschd4875622016-11-15 15:56:26 -05001720 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001721
Keith Busch2b25d982014-12-22 12:59:04 -07001722 nvmeq->cq_vector = 0;
Keith Busch161b8be2017-09-14 13:54:39 -04001723 nvme_init_queue(nvmeq, 0);
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001724 result = queue_request_irq(nvmeq);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001725 if (result) {
1726 nvmeq->cq_vector = -1;
Keith Buschd4875622016-11-15 15:56:26 -05001727 return result;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001728 }
Keith Busch025c5572013-05-01 13:07:51 -06001729
Christoph Hellwig4e224102018-12-02 17:46:17 +01001730 set_bit(NVMEQ_ENABLED, &nvmeq->flags);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001731 return result;
1732}
1733
Christoph Hellwig749941f2015-11-26 11:46:39 +01001734static int nvme_create_io_queues(struct nvme_dev *dev)
Keith Busch42f61422014-03-24 10:46:25 -06001735{
Jens Axboe4b04cc62018-11-05 12:44:33 -07001736 unsigned i, max, rw_queues;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001737 int ret = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001738
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001739 for (i = dev->ctrl.queue_count; i <= dev->max_qid; i++) {
Keith Buscha6ff7262018-04-12 09:16:09 -06001740 if (nvme_alloc_queue(dev, i, dev->q_depth)) {
Christoph Hellwig749941f2015-11-26 11:46:39 +01001741 ret = -ENOMEM;
Keith Busch42f61422014-03-24 10:46:25 -06001742 break;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001743 }
1744 }
Keith Busch42f61422014-03-24 10:46:25 -06001745
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001746 max = min(dev->max_qid, dev->ctrl.queue_count - 1);
Christoph Hellwige20ba6e2018-12-02 17:46:16 +01001747 if (max != 1 && dev->io_queues[HCTX_TYPE_POLL]) {
1748 rw_queues = dev->io_queues[HCTX_TYPE_DEFAULT] +
1749 dev->io_queues[HCTX_TYPE_READ];
Jens Axboe4b04cc62018-11-05 12:44:33 -07001750 } else {
1751 rw_queues = max;
1752 }
1753
Keith Busch949928c2015-12-17 17:08:15 -07001754 for (i = dev->online_queues; i <= max; i++) {
Jens Axboe4b04cc62018-11-05 12:44:33 -07001755 bool polled = i > rw_queues;
1756
1757 ret = nvme_create_queue(&dev->queues[i], i, polled);
Keith Buschd4875622016-11-15 15:56:26 -05001758 if (ret)
Keith Busch42f61422014-03-24 10:46:25 -06001759 break;
Matthew Wilcox27e81662014-04-11 11:58:45 -04001760 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001761
1762 /*
1763 * Ignore failing Create SQ/CQ commands, we can continue with less
Minwoo Im8adb8c12018-01-14 16:14:27 +09001764 * than the desired amount of queues, and even a controller without
1765 * I/O queues can still be used to issue admin commands. This might
Christoph Hellwig749941f2015-11-26 11:46:39 +01001766 * be useful to upgrade a buggy firmware for example.
1767 */
1768 return ret >= 0 ? 0 : ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001769}
1770
Stephen Bates202021c2016-10-05 20:01:12 -06001771static ssize_t nvme_cmb_show(struct device *dev,
1772 struct device_attribute *attr,
1773 char *buf)
1774{
1775 struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
1776
Stephen Batesc9658092016-12-16 11:54:50 -07001777 return scnprintf(buf, PAGE_SIZE, "cmbloc : x%08x\ncmbsz : x%08x\n",
Stephen Bates202021c2016-10-05 20:01:12 -06001778 ndev->cmbloc, ndev->cmbsz);
1779}
1780static DEVICE_ATTR(cmb, S_IRUGO, nvme_cmb_show, NULL);
1781
Christoph Hellwig88de4592017-12-20 14:50:00 +01001782static u64 nvme_cmb_size_unit(struct nvme_dev *dev)
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001783{
Christoph Hellwig88de4592017-12-20 14:50:00 +01001784 u8 szu = (dev->cmbsz >> NVME_CMBSZ_SZU_SHIFT) & NVME_CMBSZ_SZU_MASK;
1785
1786 return 1ULL << (12 + 4 * szu);
1787}
1788
1789static u32 nvme_cmb_size(struct nvme_dev *dev)
1790{
1791 return (dev->cmbsz >> NVME_CMBSZ_SZ_SHIFT) & NVME_CMBSZ_SZ_MASK;
1792}
1793
Christoph Hellwigf65efd62017-12-20 14:25:11 +01001794static void nvme_map_cmb(struct nvme_dev *dev)
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001795{
Christoph Hellwig88de4592017-12-20 14:50:00 +01001796 u64 size, offset;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001797 resource_size_t bar_size;
1798 struct pci_dev *pdev = to_pci_dev(dev->dev);
Christoph Hellwig8969f1f2017-10-01 09:37:35 +02001799 int bar;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001800
Keith Busch9fe5c592018-10-31 13:15:29 -06001801 if (dev->cmb_size)
1802 return;
1803
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001804 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Christoph Hellwigf65efd62017-12-20 14:25:11 +01001805 if (!dev->cmbsz)
1806 return;
Stephen Bates202021c2016-10-05 20:01:12 -06001807 dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001808
Christoph Hellwig88de4592017-12-20 14:50:00 +01001809 size = nvme_cmb_size_unit(dev) * nvme_cmb_size(dev);
1810 offset = nvme_cmb_size_unit(dev) * NVME_CMB_OFST(dev->cmbloc);
Christoph Hellwig8969f1f2017-10-01 09:37:35 +02001811 bar = NVME_CMB_BIR(dev->cmbloc);
1812 bar_size = pci_resource_len(pdev, bar);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001813
1814 if (offset > bar_size)
Christoph Hellwigf65efd62017-12-20 14:25:11 +01001815 return;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001816
1817 /*
1818 * Controllers may support a CMB size larger than their BAR,
1819 * for example, due to being behind a bridge. Reduce the CMB to
1820 * the reported size of the BAR
1821 */
1822 if (size > bar_size - offset)
1823 size = bar_size - offset;
1824
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001825 if (pci_p2pdma_add_resource(pdev, bar, size, offset)) {
1826 dev_warn(dev->ctrl.device,
1827 "failed to register the CMB\n");
Christoph Hellwigf65efd62017-12-20 14:25:11 +01001828 return;
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001829 }
1830
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001831 dev->cmb_size = size;
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001832 dev->cmb_use_sqes = use_cmb_sqes && (dev->cmbsz & NVME_CMBSZ_SQS);
1833
1834 if ((dev->cmbsz & (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS)) ==
1835 (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS))
1836 pci_p2pmem_publish(pdev, true);
Christoph Hellwigf65efd62017-12-20 14:25:11 +01001837
1838 if (sysfs_add_file_to_group(&dev->ctrl.device->kobj,
1839 &dev_attr_cmb.attr, NULL))
1840 dev_warn(dev->ctrl.device,
1841 "failed to add sysfs attribute for CMB\n");
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001842}
1843
1844static inline void nvme_release_cmb(struct nvme_dev *dev)
1845{
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001846 if (dev->cmb_size) {
Max Gurtovoy1c78f772017-07-30 01:45:08 +03001847 sysfs_remove_file_from_group(&dev->ctrl.device->kobj,
1848 &dev_attr_cmb.attr, NULL);
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001849 dev->cmb_size = 0;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001850 }
1851}
1852
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001853static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits)
Keith Busch9d713c22013-07-15 15:02:24 -06001854{
Christoph Hellwig4033f352017-08-28 10:47:18 +02001855 u64 dma_addr = dev->host_mem_descs_dma;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001856 struct nvme_command c;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001857 int ret;
1858
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001859 memset(&c, 0, sizeof(c));
1860 c.features.opcode = nvme_admin_set_features;
1861 c.features.fid = cpu_to_le32(NVME_FEAT_HOST_MEM_BUF);
1862 c.features.dword11 = cpu_to_le32(bits);
1863 c.features.dword12 = cpu_to_le32(dev->host_mem_size >>
1864 ilog2(dev->ctrl.page_size));
1865 c.features.dword13 = cpu_to_le32(lower_32_bits(dma_addr));
1866 c.features.dword14 = cpu_to_le32(upper_32_bits(dma_addr));
1867 c.features.dword15 = cpu_to_le32(dev->nr_host_mem_descs);
1868
1869 ret = nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1870 if (ret) {
1871 dev_warn(dev->ctrl.device,
1872 "failed to set host mem (err %d, flags %#x).\n",
1873 ret, bits);
1874 }
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001875 return ret;
1876}
1877
1878static void nvme_free_host_mem(struct nvme_dev *dev)
1879{
1880 int i;
1881
1882 for (i = 0; i < dev->nr_host_mem_descs; i++) {
1883 struct nvme_host_mem_buf_desc *desc = &dev->host_mem_descs[i];
1884 size_t size = le32_to_cpu(desc->size) * dev->ctrl.page_size;
1885
1886 dma_free_coherent(dev->dev, size, dev->host_mem_desc_bufs[i],
1887 le64_to_cpu(desc->addr));
1888 }
1889
1890 kfree(dev->host_mem_desc_bufs);
1891 dev->host_mem_desc_bufs = NULL;
Christoph Hellwig4033f352017-08-28 10:47:18 +02001892 dma_free_coherent(dev->dev,
1893 dev->nr_host_mem_descs * sizeof(*dev->host_mem_descs),
1894 dev->host_mem_descs, dev->host_mem_descs_dma);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001895 dev->host_mem_descs = NULL;
Minwoo Im7e5dd572017-11-25 03:03:00 +09001896 dev->nr_host_mem_descs = 0;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001897}
1898
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001899static int __nvme_alloc_host_mem(struct nvme_dev *dev, u64 preferred,
1900 u32 chunk_size)
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001901{
1902 struct nvme_host_mem_buf_desc *descs;
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001903 u32 max_entries, len;
Christoph Hellwig4033f352017-08-28 10:47:18 +02001904 dma_addr_t descs_dma;
Dan Carpenter2ee0e4e2017-07-06 12:26:52 +03001905 int i = 0;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001906 void **bufs;
Minwoo Im6fbcde62017-12-05 05:23:54 +09001907 u64 size, tmp;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001908
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001909 tmp = (preferred + chunk_size - 1);
1910 do_div(tmp, chunk_size);
1911 max_entries = tmp;
Christoph Hellwig044a9df2017-09-11 12:09:28 -04001912
1913 if (dev->ctrl.hmmaxd && dev->ctrl.hmmaxd < max_entries)
1914 max_entries = dev->ctrl.hmmaxd;
1915
Christoph Hellwig4033f352017-08-28 10:47:18 +02001916 descs = dma_zalloc_coherent(dev->dev, max_entries * sizeof(*descs),
1917 &descs_dma, GFP_KERNEL);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001918 if (!descs)
1919 goto out;
1920
1921 bufs = kcalloc(max_entries, sizeof(*bufs), GFP_KERNEL);
1922 if (!bufs)
1923 goto out_free_descs;
1924
Minwoo Im244a8fe2017-11-17 01:34:24 +09001925 for (size = 0; size < preferred && i < max_entries; size += len) {
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001926 dma_addr_t dma_addr;
1927
Christoph Hellwig50cdb7c2017-07-25 17:39:07 +02001928 len = min_t(u64, chunk_size, preferred - size);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001929 bufs[i] = dma_alloc_attrs(dev->dev, len, &dma_addr, GFP_KERNEL,
1930 DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
1931 if (!bufs[i])
1932 break;
1933
1934 descs[i].addr = cpu_to_le64(dma_addr);
1935 descs[i].size = cpu_to_le32(len / dev->ctrl.page_size);
1936 i++;
1937 }
1938
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001939 if (!size)
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001940 goto out_free_bufs;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001941
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001942 dev->nr_host_mem_descs = i;
1943 dev->host_mem_size = size;
1944 dev->host_mem_descs = descs;
Christoph Hellwig4033f352017-08-28 10:47:18 +02001945 dev->host_mem_descs_dma = descs_dma;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001946 dev->host_mem_desc_bufs = bufs;
1947 return 0;
1948
1949out_free_bufs:
1950 while (--i >= 0) {
1951 size_t size = le32_to_cpu(descs[i].size) * dev->ctrl.page_size;
1952
1953 dma_free_coherent(dev->dev, size, bufs[i],
1954 le64_to_cpu(descs[i].addr));
1955 }
1956
1957 kfree(bufs);
1958out_free_descs:
Christoph Hellwig4033f352017-08-28 10:47:18 +02001959 dma_free_coherent(dev->dev, max_entries * sizeof(*descs), descs,
1960 descs_dma);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001961out:
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001962 dev->host_mem_descs = NULL;
1963 return -ENOMEM;
1964}
1965
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001966static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
1967{
1968 u32 chunk_size;
1969
1970 /* start big and work our way down */
Akinobu Mita30f92d62017-09-06 12:15:31 +02001971 for (chunk_size = min_t(u64, preferred, PAGE_SIZE * MAX_ORDER_NR_PAGES);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04001972 chunk_size >= max_t(u32, dev->ctrl.hmminds * 4096, PAGE_SIZE * 2);
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001973 chunk_size /= 2) {
1974 if (!__nvme_alloc_host_mem(dev, preferred, chunk_size)) {
1975 if (!min || dev->host_mem_size >= min)
1976 return 0;
1977 nvme_free_host_mem(dev);
1978 }
1979 }
1980
1981 return -ENOMEM;
1982}
1983
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02001984static int nvme_setup_host_mem(struct nvme_dev *dev)
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001985{
1986 u64 max = (u64)max_host_mem_size_mb * SZ_1M;
1987 u64 preferred = (u64)dev->ctrl.hmpre * 4096;
1988 u64 min = (u64)dev->ctrl.hmmin * 4096;
1989 u32 enable_bits = NVME_HOST_MEM_ENABLE;
Minwoo Im6fbcde62017-12-05 05:23:54 +09001990 int ret;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001991
1992 preferred = min(preferred, max);
1993 if (min > max) {
1994 dev_warn(dev->ctrl.device,
1995 "min host memory (%lld MiB) above limit (%d MiB).\n",
1996 min >> ilog2(SZ_1M), max_host_mem_size_mb);
1997 nvme_free_host_mem(dev);
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02001998 return 0;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001999 }
2000
2001 /*
2002 * If we already have a buffer allocated check if we can reuse it.
2003 */
2004 if (dev->host_mem_descs) {
2005 if (dev->host_mem_size >= min)
2006 enable_bits |= NVME_HOST_MEM_RETURN;
2007 else
2008 nvme_free_host_mem(dev);
2009 }
2010
2011 if (!dev->host_mem_descs) {
Christoph Hellwig92dc6892017-09-11 12:08:43 -04002012 if (nvme_alloc_host_mem(dev, min, preferred)) {
2013 dev_warn(dev->ctrl.device,
2014 "failed to allocate host memory buffer.\n");
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02002015 return 0; /* controller must work without HMB */
Christoph Hellwig92dc6892017-09-11 12:08:43 -04002016 }
2017
2018 dev_info(dev->ctrl.device,
2019 "allocated %lld MiB host memory buffer.\n",
2020 dev->host_mem_size >> ilog2(SZ_1M));
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002021 }
2022
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02002023 ret = nvme_set_host_mem(dev, enable_bits);
2024 if (ret)
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002025 nvme_free_host_mem(dev);
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02002026 return ret;
Keith Busch9d713c22013-07-15 15:02:24 -06002027}
2028
Jens Axboe6451fe72018-12-09 11:21:45 -07002029static void nvme_calc_io_queues(struct nvme_dev *dev, unsigned int irq_queues)
Jens Axboe3b6592f2018-10-31 08:36:31 -06002030{
2031 unsigned int this_w_queues = write_queues;
2032
2033 /*
2034 * Setup read/write queue split
2035 */
Jens Axboe6451fe72018-12-09 11:21:45 -07002036 if (irq_queues == 1) {
Christoph Hellwige20ba6e2018-12-02 17:46:16 +01002037 dev->io_queues[HCTX_TYPE_DEFAULT] = 1;
2038 dev->io_queues[HCTX_TYPE_READ] = 0;
Jens Axboe3b6592f2018-10-31 08:36:31 -06002039 return;
2040 }
2041
2042 /*
2043 * If 'write_queues' is set, ensure it leaves room for at least
2044 * one read queue
2045 */
Jens Axboe6451fe72018-12-09 11:21:45 -07002046 if (this_w_queues >= irq_queues)
2047 this_w_queues = irq_queues - 1;
Jens Axboe3b6592f2018-10-31 08:36:31 -06002048
2049 /*
2050 * If 'write_queues' is set to zero, reads and writes will share
2051 * a queue set.
2052 */
2053 if (!this_w_queues) {
Jens Axboe6451fe72018-12-09 11:21:45 -07002054 dev->io_queues[HCTX_TYPE_DEFAULT] = irq_queues;
Christoph Hellwige20ba6e2018-12-02 17:46:16 +01002055 dev->io_queues[HCTX_TYPE_READ] = 0;
Jens Axboe3b6592f2018-10-31 08:36:31 -06002056 } else {
Christoph Hellwige20ba6e2018-12-02 17:46:16 +01002057 dev->io_queues[HCTX_TYPE_DEFAULT] = this_w_queues;
Jens Axboe6451fe72018-12-09 11:21:45 -07002058 dev->io_queues[HCTX_TYPE_READ] = irq_queues - this_w_queues;
Jens Axboe3b6592f2018-10-31 08:36:31 -06002059 }
2060}
2061
Jens Axboe6451fe72018-12-09 11:21:45 -07002062static int nvme_setup_irqs(struct nvme_dev *dev, unsigned int nr_io_queues)
Jens Axboe3b6592f2018-10-31 08:36:31 -06002063{
2064 struct pci_dev *pdev = to_pci_dev(dev->dev);
2065 int irq_sets[2];
2066 struct irq_affinity affd = {
2067 .pre_vectors = 1,
2068 .nr_sets = ARRAY_SIZE(irq_sets),
2069 .sets = irq_sets,
2070 };
Jens Axboe30e06622018-11-14 10:13:50 -07002071 int result = 0;
Jens Axboe6451fe72018-12-09 11:21:45 -07002072 unsigned int irq_queues, this_p_queues;
2073
2074 /*
2075 * Poll queues don't need interrupts, but we need at least one IO
2076 * queue left over for non-polled IO.
2077 */
2078 this_p_queues = poll_queues;
2079 if (this_p_queues >= nr_io_queues) {
2080 this_p_queues = nr_io_queues - 1;
2081 irq_queues = 1;
2082 } else {
2083 irq_queues = nr_io_queues - this_p_queues;
2084 }
2085 dev->io_queues[HCTX_TYPE_POLL] = this_p_queues;
Jens Axboe3b6592f2018-10-31 08:36:31 -06002086
2087 /*
2088 * For irq sets, we have to ask for minvec == maxvec. This passes
2089 * any reduction back to us, so we can adjust our queue counts and
2090 * IRQ vector needs.
2091 */
2092 do {
Jens Axboe6451fe72018-12-09 11:21:45 -07002093 nvme_calc_io_queues(dev, irq_queues);
Christoph Hellwige20ba6e2018-12-02 17:46:16 +01002094 irq_sets[0] = dev->io_queues[HCTX_TYPE_DEFAULT];
2095 irq_sets[1] = dev->io_queues[HCTX_TYPE_READ];
Jens Axboe3b6592f2018-10-31 08:36:31 -06002096 if (!irq_sets[1])
2097 affd.nr_sets = 1;
2098
2099 /*
Jens Axboedb29eb02018-11-15 16:05:02 -07002100 * If we got a failure and we're down to asking for just
2101 * 1 + 1 queues, just ask for a single vector. We'll share
2102 * that between the single IO queue and the admin queue.
Jens Axboe3b6592f2018-10-31 08:36:31 -06002103 */
Jens Axboe6451fe72018-12-09 11:21:45 -07002104 if (result >= 0 && irq_queues > 1)
2105 irq_queues = irq_sets[0] + irq_sets[1] + 1;
Jens Axboe3b6592f2018-10-31 08:36:31 -06002106
Jens Axboe6451fe72018-12-09 11:21:45 -07002107 result = pci_alloc_irq_vectors_affinity(pdev, irq_queues,
2108 irq_queues,
Jens Axboe3b6592f2018-10-31 08:36:31 -06002109 PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY, &affd);
2110
2111 /*
Jens Axboedb29eb02018-11-15 16:05:02 -07002112 * Need to reduce our vec counts. If we get ENOSPC, the
2113 * platform should support mulitple vecs, we just need
2114 * to decrease our ask. If we get EINVAL, the platform
2115 * likely does not. Back down to ask for just one vector.
Jens Axboe3b6592f2018-10-31 08:36:31 -06002116 */
2117 if (result == -ENOSPC) {
Jens Axboe6451fe72018-12-09 11:21:45 -07002118 irq_queues--;
2119 if (!irq_queues)
Jens Axboe3b6592f2018-10-31 08:36:31 -06002120 return result;
2121 continue;
Jens Axboedb29eb02018-11-15 16:05:02 -07002122 } else if (result == -EINVAL) {
Jens Axboe6451fe72018-12-09 11:21:45 -07002123 irq_queues = 1;
Jens Axboedb29eb02018-11-15 16:05:02 -07002124 continue;
Jens Axboe3b6592f2018-10-31 08:36:31 -06002125 } else if (result <= 0)
2126 return -EIO;
2127 break;
2128 } while (1);
2129
2130 return result;
2131}
2132
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002133static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002134{
Sagi Grimberg147b27e2018-01-14 12:39:01 +02002135 struct nvme_queue *adminq = &dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02002136 struct pci_dev *pdev = to_pci_dev(dev->dev);
Xu Yu97f6ef62017-05-24 16:39:55 +08002137 int result, nr_io_queues;
2138 unsigned long size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002139
Jens Axboe3b6592f2018-10-31 08:36:31 -06002140 nr_io_queues = max_io_queues();
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01002141 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
2142 if (result < 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002143 return result;
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01002144
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02002145 if (nr_io_queues == 0)
Keith Buscha5229052016-04-08 16:09:10 -06002146 return 0;
Christoph Hellwig4e224102018-12-02 17:46:17 +01002147
2148 clear_bit(NVMEQ_ENABLED, &adminq->flags);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002149
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06002150 if (dev->cmb_use_sqes) {
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002151 result = nvme_cmb_qdepth(dev, nr_io_queues,
2152 sizeof(struct nvme_command));
2153 if (result > 0)
2154 dev->q_depth = result;
2155 else
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06002156 dev->cmb_use_sqes = false;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002157 }
2158
Xu Yu97f6ef62017-05-24 16:39:55 +08002159 do {
2160 size = db_bar_size(dev, nr_io_queues);
2161 result = nvme_remap_bar(dev, size);
2162 if (!result)
2163 break;
2164 if (!--nr_io_queues)
2165 return -ENOMEM;
2166 } while (1);
2167 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002168
Keith Busch9d713c22013-07-15 15:02:24 -06002169 /* Deregister the admin queue's interrupt */
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02002170 pci_free_irq(pdev, 0, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002171
Jens Axboee32efbf2014-11-14 09:49:26 -07002172 /*
2173 * If we enable msix early due to not intx, disable it again before
2174 * setting up the full range we need.
2175 */
Christoph Hellwigdca51e72016-09-14 16:18:57 +02002176 pci_free_irq_vectors(pdev);
Jens Axboe3b6592f2018-10-31 08:36:31 -06002177
2178 result = nvme_setup_irqs(dev, nr_io_queues);
Keith Busch22b55602018-04-12 09:16:10 -06002179 if (result <= 0)
Christoph Hellwigdca51e72016-09-14 16:18:57 +02002180 return -EIO;
Jens Axboe3b6592f2018-10-31 08:36:31 -06002181
Keith Busch22b55602018-04-12 09:16:10 -06002182 dev->num_vecs = result;
Jens Axboe4b04cc62018-11-05 12:44:33 -07002183 result = max(result - 1, 1);
Christoph Hellwige20ba6e2018-12-02 17:46:16 +01002184 dev->max_qid = result + dev->io_queues[HCTX_TYPE_POLL];
Matthew Wilcox1b234842011-01-20 13:01:49 -05002185
Christoph Hellwige20ba6e2018-12-02 17:46:16 +01002186 dev_info(dev->ctrl.device, "%d/%d/%d default/read/poll queues\n",
2187 dev->io_queues[HCTX_TYPE_DEFAULT],
2188 dev->io_queues[HCTX_TYPE_READ],
2189 dev->io_queues[HCTX_TYPE_POLL]);
Jens Axboe3b6592f2018-10-31 08:36:31 -06002190
Matthew Wilcox063a8092013-06-20 10:53:48 -04002191 /*
2192 * Should investigate if there's a performance win from allocating
2193 * more queues than interrupt vectors; it might allow the submission
2194 * path to scale better, even if the receive path is limited by the
2195 * number of interrupts.
2196 */
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002197
Christoph Hellwigdca51e72016-09-14 16:18:57 +02002198 result = queue_request_irq(adminq);
Jon Derrick758dd7f2015-06-30 11:22:52 -06002199 if (result) {
2200 adminq->cq_vector = -1;
Keith Buschd4875622016-11-15 15:56:26 -05002201 return result;
Jon Derrick758dd7f2015-06-30 11:22:52 -06002202 }
Christoph Hellwig4e224102018-12-02 17:46:17 +01002203 set_bit(NVMEQ_ENABLED, &adminq->flags);
Christoph Hellwig749941f2015-11-26 11:46:39 +01002204 return nvme_create_io_queues(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002205}
2206
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002207static void nvme_del_queue_end(struct request *req, blk_status_t error)
Keith Buschdb3cbff2016-01-12 14:41:17 -07002208{
2209 struct nvme_queue *nvmeq = req->end_io_data;
2210
2211 blk_mq_free_request(req);
Christoph Hellwigd1ed6aa2018-12-02 17:46:22 +01002212 complete(&nvmeq->delete_done);
Keith Buschdb3cbff2016-01-12 14:41:17 -07002213}
2214
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002215static void nvme_del_cq_end(struct request *req, blk_status_t error)
Keith Buschdb3cbff2016-01-12 14:41:17 -07002216{
2217 struct nvme_queue *nvmeq = req->end_io_data;
2218
Christoph Hellwigd1ed6aa2018-12-02 17:46:22 +01002219 if (error)
2220 set_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags);
Keith Buschdb3cbff2016-01-12 14:41:17 -07002221
2222 nvme_del_queue_end(req, error);
2223}
2224
2225static int nvme_delete_queue(struct nvme_queue *nvmeq, u8 opcode)
2226{
2227 struct request_queue *q = nvmeq->dev->ctrl.admin_q;
2228 struct request *req;
2229 struct nvme_command cmd;
2230
2231 memset(&cmd, 0, sizeof(cmd));
2232 cmd.delete_queue.opcode = opcode;
2233 cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2234
Christoph Hellwigeb71f432016-06-13 16:45:23 +02002235 req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
Keith Buschdb3cbff2016-01-12 14:41:17 -07002236 if (IS_ERR(req))
2237 return PTR_ERR(req);
2238
2239 req->timeout = ADMIN_TIMEOUT;
2240 req->end_io_data = nvmeq;
2241
Christoph Hellwigd1ed6aa2018-12-02 17:46:22 +01002242 init_completion(&nvmeq->delete_done);
Keith Buschdb3cbff2016-01-12 14:41:17 -07002243 blk_execute_rq_nowait(q, NULL, req, false,
2244 opcode == nvme_admin_delete_cq ?
2245 nvme_del_cq_end : nvme_del_queue_end);
2246 return 0;
2247}
2248
Christoph Hellwig5271edd2018-12-02 17:46:21 +01002249static bool nvme_disable_io_queues(struct nvme_dev *dev, u8 opcode)
Keith Buschdb3cbff2016-01-12 14:41:17 -07002250{
Christoph Hellwig5271edd2018-12-02 17:46:21 +01002251 int nr_queues = dev->online_queues - 1, sent = 0;
Keith Buschdb3cbff2016-01-12 14:41:17 -07002252 unsigned long timeout;
Keith Buschdb3cbff2016-01-12 14:41:17 -07002253
Keith Buschdb3cbff2016-01-12 14:41:17 -07002254 retry:
Christoph Hellwig5271edd2018-12-02 17:46:21 +01002255 timeout = ADMIN_TIMEOUT;
2256 while (nr_queues > 0) {
2257 if (nvme_delete_queue(&dev->queues[nr_queues], opcode))
2258 break;
2259 nr_queues--;
2260 sent++;
Keith Buschdb3cbff2016-01-12 14:41:17 -07002261 }
Christoph Hellwigd1ed6aa2018-12-02 17:46:22 +01002262 while (sent) {
2263 struct nvme_queue *nvmeq = &dev->queues[nr_queues + sent];
2264
2265 timeout = wait_for_completion_io_timeout(&nvmeq->delete_done,
Christoph Hellwig5271edd2018-12-02 17:46:21 +01002266 timeout);
2267 if (timeout == 0)
2268 return false;
Christoph Hellwigd1ed6aa2018-12-02 17:46:22 +01002269
2270 /* handle any remaining CQEs */
2271 if (opcode == nvme_admin_delete_cq &&
2272 !test_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags))
2273 nvme_poll_irqdisable(nvmeq, -1);
2274
2275 sent--;
Christoph Hellwig5271edd2018-12-02 17:46:21 +01002276 if (nr_queues)
2277 goto retry;
2278 }
2279 return true;
Keith Buschdb3cbff2016-01-12 14:41:17 -07002280}
2281
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002282/*
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002283 * return error value only when tagset allocation failed
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002284 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002285static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002286{
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002287 int ret;
2288
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002289 if (!dev->ctrl.tagset) {
Christoph Hellwig376f7ef2018-12-02 17:46:27 +01002290 dev->tagset.ops = &nvme_mq_ops;
Keith Buschffe77042015-06-08 10:08:15 -06002291 dev->tagset.nr_hw_queues = dev->online_queues - 1;
Christoph Hellwiged92ad32018-12-14 14:06:59 +01002292 dev->tagset.nr_maps = 2; /* default + read */
2293 if (dev->io_queues[HCTX_TYPE_POLL])
2294 dev->tagset.nr_maps++;
Christoph Hellwige20ba6e2018-12-02 17:46:16 +01002295 dev->tagset.nr_maps = HCTX_MAX_TYPES;
Keith Buschffe77042015-06-08 10:08:15 -06002296 dev->tagset.timeout = NVME_IO_TIMEOUT;
2297 dev->tagset.numa_node = dev_to_node(dev->dev);
2298 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002299 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -07002300 dev->tagset.cmd_size = nvme_pci_cmd_size(dev, false);
2301 if ((dev->ctrl.sgls & ((1 << 0) | (1 << 1))) && sgl_threshold) {
2302 dev->tagset.cmd_size = max(dev->tagset.cmd_size,
2303 nvme_pci_cmd_size(dev, true));
2304 }
Keith Buschffe77042015-06-08 10:08:15 -06002305 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2306 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002307
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002308 ret = blk_mq_alloc_tag_set(&dev->tagset);
2309 if (ret) {
2310 dev_warn(dev->ctrl.device,
2311 "IO queues tagset allocation failed %d\n", ret);
2312 return ret;
2313 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002314 dev->ctrl.tagset = &dev->tagset;
Helen Koikef9f38e32017-04-10 12:51:07 -03002315
2316 nvme_dbbuf_set(dev);
Keith Busch949928c2015-12-17 17:08:15 -07002317 } else {
2318 blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1);
2319
2320 /* Free previously allocated queues that are no longer usable */
2321 nvme_free_queues(dev, dev->online_queues);
Keith Buschffe77042015-06-08 10:08:15 -06002322 }
Keith Busch949928c2015-12-17 17:08:15 -07002323
Keith Busche1e5e562015-02-19 13:39:03 -07002324 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002325}
2326
Keith Buschb00a7262016-02-24 09:15:52 -07002327static int nvme_pci_enable(struct nvme_dev *dev)
Keith Busch0877cb02013-07-15 15:02:19 -06002328{
Keith Buschb00a7262016-02-24 09:15:52 -07002329 int result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002330 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002331
2332 if (pci_enable_device_mem(pdev))
2333 return result;
2334
Keith Busch0877cb02013-07-15 15:02:19 -06002335 pci_set_master(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002336
Christoph Hellwige75ec752015-05-22 11:12:39 +02002337 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
2338 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01002339 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002340
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002341 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07002342 result = -ENODEV;
Keith Buschb00a7262016-02-24 09:15:52 -07002343 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07002344 }
Jens Axboee32efbf2014-11-14 09:49:26 -07002345
2346 /*
Keith Buscha5229052016-04-08 16:09:10 -06002347 * Some devices and/or platforms don't advertise or work with INTx
2348 * interrupts. Pre-enable a single MSIX or MSI vec for setup. We'll
2349 * adjust this later.
Jens Axboee32efbf2014-11-14 09:49:26 -07002350 */
Christoph Hellwigdca51e72016-09-14 16:18:57 +02002351 result = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
2352 if (result < 0)
2353 return result;
Jens Axboee32efbf2014-11-14 09:49:26 -07002354
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002355 dev->ctrl.cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002356
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002357 dev->q_depth = min_t(int, NVME_CAP_MQES(dev->ctrl.cap) + 1,
weiping zhangb27c1e62017-07-10 16:46:59 +08002358 io_queue_depth);
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002359 dev->db_stride = 1 << NVME_CAP_STRIDE(dev->ctrl.cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002360 dev->dbs = dev->bar + 4096;
Stephan Günther1f390c12015-12-01 13:23:22 -07002361
2362 /*
2363 * Temporary fix for the Apple controller found in the MacBook8,1 and
2364 * some MacBook7,1 to avoid controller resets and data loss.
2365 */
2366 if (pdev->vendor == PCI_VENDOR_ID_APPLE && pdev->device == 0x2001) {
2367 dev->q_depth = 2;
Christoph Hellwig9bdcfb12017-05-20 15:14:43 +02002368 dev_warn(dev->ctrl.device, "detected Apple NVMe controller, "
2369 "set queue depth=%u to work around controller resets\n",
Stephan Günther1f390c12015-12-01 13:23:22 -07002370 dev->q_depth);
Martin K. Petersend554b5e2017-06-27 22:27:57 -04002371 } else if (pdev->vendor == PCI_VENDOR_ID_SAMSUNG &&
2372 (pdev->device == 0xa821 || pdev->device == 0xa822) &&
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002373 NVME_CAP_MQES(dev->ctrl.cap) == 0) {
Martin K. Petersend554b5e2017-06-27 22:27:57 -04002374 dev->q_depth = 64;
2375 dev_err(dev->ctrl.device, "detected PM1725 NVMe controller, "
2376 "set queue depth=%u\n", dev->q_depth);
Stephan Günther1f390c12015-12-01 13:23:22 -07002377 }
2378
Christoph Hellwigf65efd62017-12-20 14:25:11 +01002379 nvme_map_cmb(dev);
Stephen Bates202021c2016-10-05 20:01:12 -06002380
Keith Buscha0a34082015-12-07 15:30:31 -07002381 pci_enable_pcie_error_reporting(pdev);
2382 pci_save_state(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002383 return 0;
2384
2385 disable:
Keith Busch0877cb02013-07-15 15:02:19 -06002386 pci_disable_device(pdev);
2387 return result;
2388}
2389
2390static void nvme_dev_unmap(struct nvme_dev *dev)
2391{
Keith Buschb00a7262016-02-24 09:15:52 -07002392 if (dev->bar)
2393 iounmap(dev->bar);
Johannes Thumshirna1f447b2016-06-07 09:44:02 +02002394 pci_release_mem_regions(to_pci_dev(dev->dev));
Keith Buschb00a7262016-02-24 09:15:52 -07002395}
2396
2397static void nvme_pci_disable(struct nvme_dev *dev)
2398{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002399 struct pci_dev *pdev = to_pci_dev(dev->dev);
2400
Christoph Hellwigdca51e72016-09-14 16:18:57 +02002401 pci_free_irq_vectors(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002402
Keith Buscha0a34082015-12-07 15:30:31 -07002403 if (pci_is_enabled(pdev)) {
2404 pci_disable_pcie_error_reporting(pdev);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002405 pci_disable_device(pdev);
Keith Busch4d115422013-12-10 13:10:40 -07002406 }
Keith Busch4d115422013-12-10 13:10:40 -07002407}
2408
Keith Buscha5cdb682016-01-12 14:41:18 -07002409static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002410{
Keith Buschee9aebb2018-01-24 14:55:12 -07002411 int i;
Keith Busch302ad8c2017-03-01 14:22:12 -05002412 bool dead = true;
2413 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch22404272013-07-15 15:02:20 -06002414
Keith Busch77bf25e2015-11-26 12:21:29 +01002415 mutex_lock(&dev->shutdown_lock);
Keith Busch302ad8c2017-03-01 14:22:12 -05002416 if (pci_is_enabled(pdev)) {
2417 u32 csts = readl(dev->bar + NVME_REG_CSTS);
2418
Keith Buschebef7362017-06-27 17:44:05 -06002419 if (dev->ctrl.state == NVME_CTRL_LIVE ||
2420 dev->ctrl.state == NVME_CTRL_RESETTING)
Keith Busch302ad8c2017-03-01 14:22:12 -05002421 nvme_start_freeze(&dev->ctrl);
2422 dead = !!((csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) ||
2423 pdev->error_state != pci_channel_io_normal);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002424 }
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06002425
Keith Busch302ad8c2017-03-01 14:22:12 -05002426 /*
2427 * Give the controller a chance to complete all entered requests if
2428 * doing a safe shutdown.
2429 */
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002430 if (!dead) {
2431 if (shutdown)
2432 nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
Jianchao Wang9a915a52018-02-12 20:57:24 +08002433 }
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002434
Jianchao Wang9a915a52018-02-12 20:57:24 +08002435 nvme_stop_queues(&dev->ctrl);
2436
Keith Busch64ee0ac2018-04-12 09:16:08 -06002437 if (!dead && dev->ctrl.queue_count > 0) {
Christoph Hellwig5271edd2018-12-02 17:46:21 +01002438 if (nvme_disable_io_queues(dev, nvme_admin_delete_sq))
2439 nvme_disable_io_queues(dev, nvme_admin_delete_cq);
Keith Buscha5cdb682016-01-12 14:41:18 -07002440 nvme_disable_admin_queue(dev, shutdown);
Keith Busch4d115422013-12-10 13:10:40 -07002441 }
Keith Buschee9aebb2018-01-24 14:55:12 -07002442 for (i = dev->ctrl.queue_count - 1; i >= 0; i--)
2443 nvme_suspend_queue(&dev->queues[i]);
2444
Keith Buschb00a7262016-02-24 09:15:52 -07002445 nvme_pci_disable(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002446
Ming Line1958e62016-05-18 14:05:01 -07002447 blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl);
2448 blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl);
Keith Busch302ad8c2017-03-01 14:22:12 -05002449
2450 /*
2451 * The driver will not be starting up queues again if shutting down so
2452 * must flush all entered requests to their failed completion to avoid
2453 * deadlocking blk-mq hot-cpu notifier.
2454 */
2455 if (shutdown)
2456 nvme_start_queues(&dev->ctrl);
Keith Busch77bf25e2015-11-26 12:21:29 +01002457 mutex_unlock(&dev->shutdown_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002458}
2459
Matthew Wilcox091b6092011-02-10 09:56:01 -05002460static int nvme_setup_prp_pools(struct nvme_dev *dev)
2461{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002462 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002463 PAGE_SIZE, PAGE_SIZE, 0);
2464 if (!dev->prp_page_pool)
2465 return -ENOMEM;
2466
Matthew Wilcox99802a72011-02-10 10:30:34 -05002467 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002468 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002469 256, 256, 0);
2470 if (!dev->prp_small_pool) {
2471 dma_pool_destroy(dev->prp_page_pool);
2472 return -ENOMEM;
2473 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002474 return 0;
2475}
2476
2477static void nvme_release_prp_pools(struct nvme_dev *dev)
2478{
2479 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002480 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002481}
2482
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002483static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002484{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002485 struct nvme_dev *dev = to_nvme_dev(ctrl);
Keith Busch9ac27092014-01-31 16:53:39 -07002486
Helen Koikef9f38e32017-04-10 12:51:07 -03002487 nvme_dbbuf_dma_free(dev);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002488 put_device(dev->dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002489 if (dev->tagset.tags)
2490 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002491 if (dev->ctrl.admin_q)
2492 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002493 kfree(dev->queues);
Scott Bauere286bcf2017-02-22 10:15:07 -07002494 free_opal_dev(dev->ctrl.opal_dev);
Jens Axboe943e9422018-06-21 09:49:37 -06002495 mempool_destroy(dev->iod_mempool);
Keith Busch5e82e952013-02-19 10:17:58 -07002496 kfree(dev);
2497}
2498
Keith Buschf58944e2016-02-24 09:15:55 -07002499static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
2500{
Linus Torvalds237045f2016-03-18 17:13:31 -07002501 dev_warn(dev->ctrl.device, "Removing after probe failure status: %d\n", status);
Keith Buschf58944e2016-02-24 09:15:55 -07002502
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002503 nvme_get_ctrl(&dev->ctrl);
Keith Busch69d9a992016-02-24 09:15:56 -07002504 nvme_dev_disable(dev, false);
Jianchao Wang9f9cafc2018-06-20 13:42:22 +08002505 nvme_kill_queues(&dev->ctrl);
Ming Lei03e0f3a2017-11-09 19:32:07 +08002506 if (!queue_work(nvme_wq, &dev->remove_work))
Keith Buschf58944e2016-02-24 09:15:55 -07002507 nvme_put_ctrl(&dev->ctrl);
2508}
2509
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002510static void nvme_reset_work(struct work_struct *work)
Keith Busch5e82e952013-02-19 10:17:58 -07002511{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002512 struct nvme_dev *dev =
2513 container_of(work, struct nvme_dev, ctrl.reset_work);
Scott Bauera98e58e52017-02-03 12:50:32 -07002514 bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
Keith Buschf58944e2016-02-24 09:15:55 -07002515 int result = -ENODEV;
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002516 enum nvme_ctrl_state new_state = NVME_CTRL_LIVE;
Keith Buschf0b50732013-07-15 15:02:21 -06002517
Rakesh Pandit82b057c2017-06-05 14:43:11 +03002518 if (WARN_ON(dev->ctrl.state != NVME_CTRL_RESETTING))
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002519 goto out;
2520
2521 /*
2522 * If we're called to reset a live controller first shut it down before
2523 * moving on.
2524 */
Keith Buschb00a7262016-02-24 09:15:52 -07002525 if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
Keith Buscha5cdb682016-01-12 14:41:18 -07002526 nvme_dev_disable(dev, false);
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002527
Jianchao Wangad700622018-01-22 22:03:16 +08002528 /*
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02002529 * Introduce CONNECTING state from nvme-fc/rdma transports to mark the
Jianchao Wangad700622018-01-22 22:03:16 +08002530 * initializing procedure here.
2531 */
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02002532 if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_CONNECTING)) {
Jianchao Wangad700622018-01-22 22:03:16 +08002533 dev_warn(dev->ctrl.device,
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02002534 "failed to mark controller CONNECTING\n");
Jianchao Wangad700622018-01-22 22:03:16 +08002535 goto out;
2536 }
2537
Keith Buschb00a7262016-02-24 09:15:52 -07002538 result = nvme_pci_enable(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002539 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002540 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002541
Sagi Grimberg01ad0992017-05-01 00:27:17 +03002542 result = nvme_pci_configure_admin_queue(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002543 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002544 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002545
Keith Busch0fb59cb2015-01-07 18:55:50 -07002546 result = nvme_alloc_admin_tags(dev);
2547 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002548 goto out;
Dan McLeranb9afca32014-04-07 17:10:11 -06002549
Jens Axboe943e9422018-06-21 09:49:37 -06002550 /*
2551 * Limit the max command size to prevent iod->sg allocations going
2552 * over a single page.
2553 */
2554 dev->ctrl.max_hw_sectors = NVME_MAX_KB_SZ << 1;
2555 dev->ctrl.max_segments = NVME_MAX_SEGS;
2556
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002557 result = nvme_init_identify(&dev->ctrl);
2558 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002559 goto out;
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002560
Scott Bauere286bcf2017-02-22 10:15:07 -07002561 if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) {
2562 if (!dev->ctrl.opal_dev)
2563 dev->ctrl.opal_dev =
2564 init_opal_dev(&dev->ctrl, &nvme_sec_submit);
2565 else if (was_suspend)
2566 opal_unlock_from_suspend(dev->ctrl.opal_dev);
2567 } else {
2568 free_opal_dev(dev->ctrl.opal_dev);
2569 dev->ctrl.opal_dev = NULL;
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01002570 }
Scott Bauera98e58e52017-02-03 12:50:32 -07002571
Helen Koikef9f38e32017-04-10 12:51:07 -03002572 if (dev->ctrl.oacs & NVME_CTRL_OACS_DBBUF_SUPP) {
2573 result = nvme_dbbuf_dma_alloc(dev);
2574 if (result)
2575 dev_warn(dev->dev,
2576 "unable to allocate dma for dbbuf\n");
2577 }
2578
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02002579 if (dev->ctrl.hmpre) {
2580 result = nvme_setup_host_mem(dev);
2581 if (result < 0)
2582 goto out;
2583 }
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002584
Keith Buschf0b50732013-07-15 15:02:21 -06002585 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002586 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002587 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002588
Keith Busch21f033f2016-04-12 11:13:11 -06002589 /*
Christoph Hellwig2659e572015-10-02 18:51:31 +02002590 * Keep the controller around but remove all namespaces if we don't have
2591 * any working I/O queue.
2592 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002593 if (dev->online_queues < 2) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002594 dev_warn(dev->ctrl.device, "IO queues not created\n");
Keith Busch3b247742016-04-27 15:51:18 -06002595 nvme_kill_queues(&dev->ctrl);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002596 nvme_remove_namespaces(&dev->ctrl);
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002597 new_state = NVME_CTRL_ADMIN_ONLY;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002598 } else {
Keith Busch25646262016-01-04 09:10:57 -07002599 nvme_start_queues(&dev->ctrl);
Keith Busch302ad8c2017-03-01 14:22:12 -05002600 nvme_wait_freeze(&dev->ctrl);
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002601 /* hit this only when allocate tagset fails */
2602 if (nvme_dev_add(dev))
2603 new_state = NVME_CTRL_ADMIN_ONLY;
Keith Busch302ad8c2017-03-01 14:22:12 -05002604 nvme_unfreeze(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002605 }
2606
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002607 /*
2608 * If only admin queue live, keep it to do further investigation or
2609 * recovery.
2610 */
2611 if (!nvme_change_ctrl_state(&dev->ctrl, new_state)) {
2612 dev_warn(dev->ctrl.device,
2613 "failed to mark controller state %d\n", new_state);
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002614 goto out;
2615 }
Christoph Hellwig92911a52016-04-26 13:51:58 +02002616
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002617 nvme_start_ctrl(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002618 return;
Keith Buschf0b50732013-07-15 15:02:21 -06002619
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002620 out:
Keith Buschf58944e2016-02-24 09:15:55 -07002621 nvme_remove_dead_ctrl(dev, result);
Keith Buschf0b50732013-07-15 15:02:21 -06002622}
2623
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002624static void nvme_remove_dead_ctrl_work(struct work_struct *work)
Keith Busch9a6b9452013-12-10 13:10:36 -07002625{
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002626 struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002627 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002628
2629 if (pci_get_drvdata(pdev))
Keith Busch921920a2016-03-28 16:03:21 -06002630 device_release_driver(&pdev->dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002631 nvme_put_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002632}
2633
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002634static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
Keith Busch4cc06522015-06-05 10:30:08 -06002635{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002636 *val = readl(to_nvme_dev(ctrl)->bar + off);
2637 return 0;
Keith Busch4cc06522015-06-05 10:30:08 -06002638}
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002639
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002640static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2641{
2642 writel(val, to_nvme_dev(ctrl)->bar + off);
2643 return 0;
2644}
2645
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002646static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2647{
2648 *val = readq(to_nvme_dev(ctrl)->bar + off);
2649 return 0;
2650}
2651
Keith Busch97c12222018-03-08 14:50:32 -07002652static int nvme_pci_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
2653{
2654 struct pci_dev *pdev = to_pci_dev(to_nvme_dev(ctrl)->dev);
2655
2656 return snprintf(buf, size, "%s", dev_name(&pdev->dev));
2657}
2658
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002659static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
Ming Lin1a353d82016-06-13 16:45:24 +02002660 .name = "pcie",
Sagi Grimberge439bb12016-02-10 10:03:29 -08002661 .module = THIS_MODULE,
Logan Gunthorpee0596ab2018-10-04 15:27:44 -06002662 .flags = NVME_F_METADATA_SUPPORTED |
2663 NVME_F_PCI_P2PDMA,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002664 .reg_read32 = nvme_pci_reg_read32,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002665 .reg_write32 = nvme_pci_reg_write32,
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002666 .reg_read64 = nvme_pci_reg_read64,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002667 .free_ctrl = nvme_pci_free_ctrl,
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002668 .submit_async_event = nvme_pci_submit_async_event,
Keith Busch97c12222018-03-08 14:50:32 -07002669 .get_address = nvme_pci_get_address,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002670};
Keith Busch4cc06522015-06-05 10:30:08 -06002671
Keith Buschb00a7262016-02-24 09:15:52 -07002672static int nvme_dev_map(struct nvme_dev *dev)
2673{
Keith Buschb00a7262016-02-24 09:15:52 -07002674 struct pci_dev *pdev = to_pci_dev(dev->dev);
2675
Johannes Thumshirna1f447b2016-06-07 09:44:02 +02002676 if (pci_request_mem_regions(pdev, "nvme"))
Keith Buschb00a7262016-02-24 09:15:52 -07002677 return -ENODEV;
2678
Xu Yu97f6ef62017-05-24 16:39:55 +08002679 if (nvme_remap_bar(dev, NVME_REG_DBS + 4096))
Keith Buschb00a7262016-02-24 09:15:52 -07002680 goto release;
2681
Max Gurtovoy9fa196e2016-12-19 16:18:24 +02002682 return 0;
Keith Buschb00a7262016-02-24 09:15:52 -07002683 release:
Max Gurtovoy9fa196e2016-12-19 16:18:24 +02002684 pci_release_mem_regions(pdev);
2685 return -ENODEV;
Keith Buschb00a7262016-02-24 09:15:52 -07002686}
2687
Kai-Heng Feng8427bbc2017-11-09 01:12:03 -05002688static unsigned long check_vendor_combination_bug(struct pci_dev *pdev)
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002689{
2690 if (pdev->vendor == 0x144d && pdev->device == 0xa802) {
2691 /*
2692 * Several Samsung devices seem to drop off the PCIe bus
2693 * randomly when APST is on and uses the deepest sleep state.
2694 * This has been observed on a Samsung "SM951 NVMe SAMSUNG
2695 * 256GB", a "PM951 NVMe SAMSUNG 512GB", and a "Samsung SSD
2696 * 950 PRO 256GB", but it seems to be restricted to two Dell
2697 * laptops.
2698 */
2699 if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.") &&
2700 (dmi_match(DMI_PRODUCT_NAME, "XPS 15 9550") ||
2701 dmi_match(DMI_PRODUCT_NAME, "Precision 5510")))
2702 return NVME_QUIRK_NO_DEEPEST_PS;
Kai-Heng Feng8427bbc2017-11-09 01:12:03 -05002703 } else if (pdev->vendor == 0x144d && pdev->device == 0xa804) {
2704 /*
2705 * Samsung SSD 960 EVO drops off the PCIe bus after system
Jarosław Janik467c77d42018-03-11 19:51:56 +01002706 * suspend on a Ryzen board, ASUS PRIME B350M-A, as well as
2707 * within few minutes after bootup on a Coffee Lake board -
2708 * ASUS PRIME Z370-A
Kai-Heng Feng8427bbc2017-11-09 01:12:03 -05002709 */
2710 if (dmi_match(DMI_BOARD_VENDOR, "ASUSTeK COMPUTER INC.") &&
Jarosław Janik467c77d42018-03-11 19:51:56 +01002711 (dmi_match(DMI_BOARD_NAME, "PRIME B350M-A") ||
2712 dmi_match(DMI_BOARD_NAME, "PRIME Z370-A")))
Kai-Heng Feng8427bbc2017-11-09 01:12:03 -05002713 return NVME_QUIRK_NO_APST;
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002714 }
2715
2716 return 0;
2717}
2718
Keith Busch181197752018-04-27 13:42:52 -06002719static void nvme_async_probe(void *data, async_cookie_t cookie)
2720{
2721 struct nvme_dev *dev = data;
Keith Busch80f513b2018-05-07 08:30:24 -06002722
Keith Busch181197752018-04-27 13:42:52 -06002723 nvme_reset_ctrl_sync(&dev->ctrl);
2724 flush_work(&dev->ctrl.scan_work);
Keith Busch80f513b2018-05-07 08:30:24 -06002725 nvme_put_ctrl(&dev->ctrl);
Keith Busch181197752018-04-27 13:42:52 -06002726}
2727
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002728static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002729{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002730 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002731 struct nvme_dev *dev;
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002732 unsigned long quirks = id->driver_data;
Jens Axboe943e9422018-06-21 09:49:37 -06002733 size_t alloc_size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002734
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002735 node = dev_to_node(&pdev->dev);
2736 if (node == NUMA_NO_NODE)
Masayoshi Mizuma2fa84352016-06-20 09:33:17 +09002737 set_dev_node(&pdev->dev, first_memory_node);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002738
2739 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002740 if (!dev)
2741 return -ENOMEM;
Sagi Grimberg147b27e2018-01-14 12:39:01 +02002742
Jens Axboe3b6592f2018-10-31 08:36:31 -06002743 dev->queues = kcalloc_node(max_queue_count(), sizeof(struct nvme_queue),
2744 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002745 if (!dev->queues)
2746 goto free;
2747
Christoph Hellwige75ec752015-05-22 11:12:39 +02002748 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002749 pci_set_drvdata(pdev, dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002750
Keith Buschb00a7262016-02-24 09:15:52 -07002751 result = nvme_dev_map(dev);
2752 if (result)
Christophe JAILLETb00c9b72017-07-16 10:39:03 +02002753 goto put_pci;
Keith Buschb00a7262016-02-24 09:15:52 -07002754
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002755 INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002756 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
Keith Busch77bf25e2015-11-26 12:21:29 +01002757 mutex_init(&dev->shutdown_lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002758
2759 result = nvme_setup_prp_pools(dev);
2760 if (result)
Christophe JAILLETb00c9b72017-07-16 10:39:03 +02002761 goto unmap;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002762
Kai-Heng Feng8427bbc2017-11-09 01:12:03 -05002763 quirks |= check_vendor_combination_bug(pdev);
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002764
Jens Axboe943e9422018-06-21 09:49:37 -06002765 /*
2766 * Double check that our mempool alloc size will cover the biggest
2767 * command we support.
2768 */
2769 alloc_size = nvme_pci_iod_alloc_size(dev, NVME_MAX_KB_SZ,
2770 NVME_MAX_SEGS, true);
2771 WARN_ON_ONCE(alloc_size > PAGE_SIZE);
2772
2773 dev->iod_mempool = mempool_create_node(1, mempool_kmalloc,
2774 mempool_kfree,
2775 (void *) alloc_size,
2776 GFP_KERNEL, node);
2777 if (!dev->iod_mempool) {
2778 result = -ENOMEM;
2779 goto release_pools;
2780 }
2781
Keith Buschb6e44b42018-07-11 16:44:44 -06002782 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
2783 quirks);
2784 if (result)
2785 goto release_mempool;
2786
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002787 dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev));
2788
Keith Busch80f513b2018-05-07 08:30:24 -06002789 nvme_get_ctrl(&dev->ctrl);
Keith Busch181197752018-04-27 13:42:52 -06002790 async_schedule(nvme_async_probe, dev);
Sagi Grimberg4caff8f2017-12-31 14:01:19 +02002791
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002792 return 0;
2793
Keith Buschb6e44b42018-07-11 16:44:44 -06002794 release_mempool:
2795 mempool_destroy(dev->iod_mempool);
Keith Busch0877cb02013-07-15 15:02:19 -06002796 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002797 nvme_release_prp_pools(dev);
Christophe JAILLETb00c9b72017-07-16 10:39:03 +02002798 unmap:
2799 nvme_dev_unmap(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002800 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002801 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002802 free:
2803 kfree(dev->queues);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002804 kfree(dev);
2805 return result;
2806}
2807
Christoph Hellwig775755e2017-06-01 13:10:38 +02002808static void nvme_reset_prepare(struct pci_dev *pdev)
Keith Buschf0d54a52014-05-02 10:40:43 -06002809{
Keith Buscha6739472014-06-23 16:03:21 -06002810 struct nvme_dev *dev = pci_get_drvdata(pdev);
Linus Torvaldsf263fbb2017-07-08 15:51:57 -07002811 nvme_dev_disable(dev, false);
Christoph Hellwig775755e2017-06-01 13:10:38 +02002812}
Keith Buschf0d54a52014-05-02 10:40:43 -06002813
Christoph Hellwig775755e2017-06-01 13:10:38 +02002814static void nvme_reset_done(struct pci_dev *pdev)
2815{
Linus Torvaldsf263fbb2017-07-08 15:51:57 -07002816 struct nvme_dev *dev = pci_get_drvdata(pdev);
Sagi Grimberg79c48cc2018-01-14 12:39:00 +02002817 nvme_reset_ctrl_sync(&dev->ctrl);
Keith Buschf0d54a52014-05-02 10:40:43 -06002818}
2819
Keith Busch09ece142014-01-27 11:29:40 -05002820static void nvme_shutdown(struct pci_dev *pdev)
2821{
2822 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buscha5cdb682016-01-12 14:41:18 -07002823 nvme_dev_disable(dev, true);
Keith Busch09ece142014-01-27 11:29:40 -05002824}
2825
Keith Buschf58944e2016-02-24 09:15:55 -07002826/*
2827 * The driver's remove may be called on a device in a partially initialized
2828 * state. This function must not have any dependencies on the device state in
2829 * order to proceed.
2830 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002831static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002832{
2833 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002834
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002835 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
Keith Busch9a6b9452013-12-10 13:10:36 -07002836 pci_set_drvdata(pdev, NULL);
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002837
Keith Busch6db28ed2017-02-10 18:15:49 -05002838 if (!pci_device_is_present(pdev)) {
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002839 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD);
Keith Busch1d39e692018-06-06 08:13:08 -06002840 nvme_dev_disable(dev, true);
Keith Buschcb4bfda2018-10-15 10:19:06 -06002841 nvme_dev_remove_admin(dev);
Keith Busch6db28ed2017-02-10 18:15:49 -05002842 }
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002843
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002844 flush_work(&dev->ctrl.reset_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002845 nvme_stop_ctrl(&dev->ctrl);
2846 nvme_remove_namespaces(&dev->ctrl);
Keith Buscha5cdb682016-01-12 14:41:18 -07002847 nvme_dev_disable(dev, true);
Keith Busch9fe5c592018-10-31 13:15:29 -06002848 nvme_release_cmb(dev);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002849 nvme_free_host_mem(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002850 nvme_dev_remove_admin(dev);
2851 nvme_free_queues(dev, 0);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002852 nvme_uninit_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002853 nvme_release_prp_pools(dev);
Keith Buschb00a7262016-02-24 09:15:52 -07002854 nvme_dev_unmap(dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002855 nvme_put_ctrl(&dev->ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002856}
2857
Jingoo Han671a6012014-02-13 11:19:14 +09002858#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002859static int nvme_suspend(struct device *dev)
2860{
2861 struct pci_dev *pdev = to_pci_dev(dev);
2862 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2863
Keith Buscha5cdb682016-01-12 14:41:18 -07002864 nvme_dev_disable(ndev, true);
Keith Buschcd638942013-07-15 15:02:23 -06002865 return 0;
2866}
2867
2868static int nvme_resume(struct device *dev)
2869{
2870 struct pci_dev *pdev = to_pci_dev(dev);
2871 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002872
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002873 nvme_reset_ctrl(&ndev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002874 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002875}
Jingoo Han671a6012014-02-13 11:19:14 +09002876#endif
Keith Buschcd638942013-07-15 15:02:23 -06002877
2878static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002879
Keith Buscha0a34082015-12-07 15:30:31 -07002880static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
2881 pci_channel_state_t state)
2882{
2883 struct nvme_dev *dev = pci_get_drvdata(pdev);
2884
2885 /*
2886 * A frozen channel requires a reset. When detected, this method will
2887 * shutdown the controller to quiesce. The controller will be restarted
2888 * after the slot reset through driver's slot_reset callback.
2889 */
Keith Buscha0a34082015-12-07 15:30:31 -07002890 switch (state) {
2891 case pci_channel_io_normal:
2892 return PCI_ERS_RESULT_CAN_RECOVER;
2893 case pci_channel_io_frozen:
Keith Buschd011fb32016-04-04 15:07:41 -06002894 dev_warn(dev->ctrl.device,
2895 "frozen state error detected, reset controller\n");
Keith Buscha5cdb682016-01-12 14:41:18 -07002896 nvme_dev_disable(dev, false);
Keith Buscha0a34082015-12-07 15:30:31 -07002897 return PCI_ERS_RESULT_NEED_RESET;
2898 case pci_channel_io_perm_failure:
Keith Buschd011fb32016-04-04 15:07:41 -06002899 dev_warn(dev->ctrl.device,
2900 "failure state error detected, request disconnect\n");
Keith Buscha0a34082015-12-07 15:30:31 -07002901 return PCI_ERS_RESULT_DISCONNECT;
2902 }
2903 return PCI_ERS_RESULT_NEED_RESET;
2904}
2905
2906static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev)
2907{
2908 struct nvme_dev *dev = pci_get_drvdata(pdev);
2909
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002910 dev_info(dev->ctrl.device, "restart after slot reset\n");
Keith Buscha0a34082015-12-07 15:30:31 -07002911 pci_restore_state(pdev);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002912 nvme_reset_ctrl(&dev->ctrl);
Keith Buscha0a34082015-12-07 15:30:31 -07002913 return PCI_ERS_RESULT_RECOVERED;
2914}
2915
2916static void nvme_error_resume(struct pci_dev *pdev)
2917{
Keith Busch72cd4cc2018-05-24 16:16:04 -06002918 struct nvme_dev *dev = pci_get_drvdata(pdev);
2919
2920 flush_work(&dev->ctrl.reset_work);
Keith Buscha0a34082015-12-07 15:30:31 -07002921}
2922
Stephen Hemminger1d352032012-09-07 09:33:17 -07002923static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002924 .error_detected = nvme_error_detected,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002925 .slot_reset = nvme_slot_reset,
2926 .resume = nvme_error_resume,
Christoph Hellwig775755e2017-06-01 13:10:38 +02002927 .reset_prepare = nvme_reset_prepare,
2928 .reset_done = nvme_reset_done,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002929};
2930
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002931static const struct pci_device_id nvme_id_table[] = {
Christoph Hellwig106198e2015-11-26 10:07:41 +01002932 { PCI_VDEVICE(INTEL, 0x0953),
Keith Busch08095e72016-03-04 13:15:17 -07002933 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002934 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch99466e72016-05-02 15:14:24 -06002935 { PCI_VDEVICE(INTEL, 0x0a53),
2936 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002937 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch99466e72016-05-02 15:14:24 -06002938 { PCI_VDEVICE(INTEL, 0x0a54),
2939 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002940 NVME_QUIRK_DEALLOCATE_ZEROES, },
David Wayne Fugatef99cb7af2017-07-10 12:39:59 -06002941 { PCI_VDEVICE(INTEL, 0x0a55),
2942 .driver_data = NVME_QUIRK_STRIPE_SIZE |
2943 NVME_QUIRK_DEALLOCATE_ZEROES, },
Andy Lutomirski50af47d2017-05-24 15:06:31 -07002944 { PCI_VDEVICE(INTEL, 0xf1a5), /* Intel 600P/P3100 */
Jens Axboe9abd68e2018-05-08 10:25:15 -06002945 .driver_data = NVME_QUIRK_NO_DEEPEST_PS |
2946 NVME_QUIRK_MEDIUM_PRIO_SQ },
Keith Busch540c8012015-10-22 15:45:06 -06002947 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2948 .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
Micah Parrish0302ae62018-04-12 13:25:25 -06002949 { PCI_DEVICE(0x1bb1, 0x0100), /* Seagate Nytro Flash Storage */
2950 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03002951 { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */
2952 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Jeff Lien8c97eec2017-11-21 10:44:37 -06002953 { PCI_DEVICE(0x1c58, 0x0023), /* WDC SN200 adapter */
2954 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Wenbo Wang015282c2016-09-08 12:12:11 -04002955 { PCI_DEVICE(0x1c5f, 0x0540), /* Memblaze Pblaze4 adapter */
2956 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Martin K. Petersend554b5e2017-06-27 22:27:57 -04002957 { PCI_DEVICE(0x144d, 0xa821), /* Samsung PM1725 */
2958 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
2959 { PCI_DEVICE(0x144d, 0xa822), /* Samsung PM1725a */
2960 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002961 { PCI_DEVICE(0x1d1d, 0x1f1f), /* LighNVM qemu device */
2962 .driver_data = NVME_QUIRK_LIGHTNVM, },
2963 { PCI_DEVICE(0x1d1d, 0x2807), /* CNEX WL */
2964 .driver_data = NVME_QUIRK_LIGHTNVM, },
Wei Xuea48e872018-04-26 14:59:19 -06002965 { PCI_DEVICE(0x1d1d, 0x2601), /* CNEX Granby */
2966 .driver_data = NVME_QUIRK_LIGHTNVM, },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002967 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01002968 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Daniel Roschka124298b2017-02-22 15:17:29 -07002969 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002970 { 0, }
2971};
2972MODULE_DEVICE_TABLE(pci, nvme_id_table);
2973
2974static struct pci_driver nvme_driver = {
2975 .name = "nvme",
2976 .id_table = nvme_id_table,
2977 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002978 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002979 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002980 .driver = {
2981 .pm = &nvme_dev_pm_ops,
2982 },
Alexander Duyck74d986a2018-04-24 16:47:27 -05002983 .sriov_configure = pci_sriov_configure_simple,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002984 .err_handler = &nvme_err_handler,
2985};
2986
2987static int __init nvme_init(void)
2988{
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002989 return pci_register_driver(&nvme_driver);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002990}
2991
2992static void __exit nvme_exit(void)
2993{
2994 pci_unregister_driver(&nvme_driver);
Ming Lei03e0f3a2017-11-09 19:32:07 +08002995 flush_workqueue(nvme_wq);
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002996 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002997}
2998
2999MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
3000MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07003001MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05003002module_init(nvme_init);
3003module_exit(nvme_exit);