blob: 41730190d93276e6b27002f0266a50ba0616bfe2 [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 Axboe4b04cc62018-11-05 12:44:33 -070089static int poll_queues = 1;
90module_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
Jens Axboe3b6592f2018-10-31 08:36:31 -060098enum {
99 NVMEQ_TYPE_READ,
100 NVMEQ_TYPE_WRITE,
Jens Axboe4b04cc62018-11-05 12:44:33 -0700101 NVMEQ_TYPE_POLL,
Jens Axboe3b6592f2018-10-31 08:36:31 -0600102 NVMEQ_TYPE_NR,
103};
104
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500105/*
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100106 * Represents an NVM Express device. Each nvme_dev is a PCI function.
107 */
108struct nvme_dev {
Sagi Grimberg147b27e2018-01-14 12:39:01 +0200109 struct nvme_queue *queues;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100110 struct blk_mq_tag_set tagset;
111 struct blk_mq_tag_set admin_tagset;
112 u32 __iomem *dbs;
113 struct device *dev;
114 struct dma_pool *prp_page_pool;
115 struct dma_pool *prp_small_pool;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100116 unsigned online_queues;
117 unsigned max_qid;
Jens Axboe3b6592f2018-10-31 08:36:31 -0600118 unsigned io_queues[NVMEQ_TYPE_NR];
Keith Busch22b55602018-04-12 09:16:10 -0600119 unsigned int num_vecs;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100120 int q_depth;
121 u32 db_stride;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100122 void __iomem *bar;
Xu Yu97f6ef62017-05-24 16:39:55 +0800123 unsigned long bar_mapped_size;
Christoph Hellwig5c8809e2015-11-26 12:35:49 +0100124 struct work_struct remove_work;
Keith Busch77bf25e2015-11-26 12:21:29 +0100125 struct mutex shutdown_lock;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100126 bool subsystem;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100127 u64 cmb_size;
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -0600128 bool cmb_use_sqes;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100129 u32 cmbsz;
Stephen Bates202021c2016-10-05 20:01:12 -0600130 u32 cmbloc;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100131 struct nvme_ctrl ctrl;
Keith Buschdb3cbff2016-01-12 14:41:17 -0700132 struct completion ioq_wait;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +0200133
Jens Axboe943e9422018-06-21 09:49:37 -0600134 mempool_t *iod_mempool;
135
Christoph Hellwig87ad72a2017-05-12 17:02:58 +0200136 /* shadow doorbell buffer support: */
Helen Koikef9f38e32017-04-10 12:51:07 -0300137 u32 *dbbuf_dbs;
138 dma_addr_t dbbuf_dbs_dma_addr;
139 u32 *dbbuf_eis;
140 dma_addr_t dbbuf_eis_dma_addr;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +0200141
142 /* host memory buffer support: */
143 u64 host_mem_size;
144 u32 nr_host_mem_descs;
Christoph Hellwig4033f352017-08-28 10:47:18 +0200145 dma_addr_t host_mem_descs_dma;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +0200146 struct nvme_host_mem_buf_desc *host_mem_descs;
147 void **host_mem_desc_bufs;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500148};
149
weiping zhangb27c1e62017-07-10 16:46:59 +0800150static int io_queue_depth_set(const char *val, const struct kernel_param *kp)
151{
152 int n = 0, ret;
153
154 ret = kstrtoint(val, 10, &n);
155 if (ret != 0 || n < 2)
156 return -EINVAL;
157
158 return param_set_int(val, kp);
159}
160
Jens Axboe3b6592f2018-10-31 08:36:31 -0600161static int queue_count_set(const char *val, const struct kernel_param *kp)
162{
163 int n = 0, ret;
164
165 ret = kstrtoint(val, 10, &n);
166 if (n > num_possible_cpus())
167 n = num_possible_cpus();
168
169 return param_set_int(val, kp);
170}
171
Helen Koikef9f38e32017-04-10 12:51:07 -0300172static inline unsigned int sq_idx(unsigned int qid, u32 stride)
173{
174 return qid * 2 * stride;
175}
176
177static inline unsigned int cq_idx(unsigned int qid, u32 stride)
178{
179 return (qid * 2 + 1) * stride;
180}
181
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100182static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
183{
184 return container_of(ctrl, struct nvme_dev, ctrl);
185}
186
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500187/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500188 * An NVM Express queue. Each device has at least two (one for admin
189 * commands and one for I/O commands).
190 */
191struct nvme_queue {
192 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500193 struct nvme_dev *dev;
Jens Axboe1ab0cd62018-05-17 18:31:51 +0200194 spinlock_t sq_lock;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500195 struct nvme_command *sq_cmds;
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -0600196 bool sq_cmds_is_io;
Jens Axboe1ab0cd62018-05-17 18:31:51 +0200197 spinlock_t cq_lock ____cacheline_aligned_in_smp;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500198 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600199 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500200 dma_addr_t sq_dma_addr;
201 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500202 u32 __iomem *q_db;
203 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700204 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500205 u16 sq_tail;
206 u16 cq_head;
Jens Axboe68fa9db2018-05-21 08:41:52 -0600207 u16 last_cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700208 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400209 u8 cq_phase;
Jens Axboe4b04cc62018-11-05 12:44:33 -0700210 u8 polled;
Helen Koikef9f38e32017-04-10 12:51:07 -0300211 u32 *dbbuf_sq_db;
212 u32 *dbbuf_cq_db;
213 u32 *dbbuf_sq_ei;
214 u32 *dbbuf_cq_ei;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500215};
216
217/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200218 * The nvme_iod describes the data in an I/O, including the list of PRP
219 * entries. You can't see it in this data structure because C doesn't let
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100220 * me express that. Use nvme_init_iod to ensure there's enough space
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200221 * allocated to store the PRP list.
222 */
223struct nvme_iod {
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800224 struct nvme_request req;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100225 struct nvme_queue *nvmeq;
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700226 bool use_sgl;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100227 int aborted;
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200228 int npages; /* In the PRP list. 0 means small pool in use */
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200229 int nents; /* Used in scatterlist */
230 int length; /* Of data, in bytes */
231 dma_addr_t first_dma;
Christoph Hellwigbf684052015-10-26 17:12:51 +0900232 struct scatterlist meta_sg; /* metadata requires single contiguous buffer */
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100233 struct scatterlist *sg;
234 struct scatterlist inline_sg[0];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500235};
236
237/*
238 * Check we didin't inadvertently grow the command struct
239 */
240static inline void _nvme_check_size(void)
241{
242 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
243 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
244 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
245 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
246 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400247 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700248 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500249 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
Johannes Thumshirn0add5e82017-06-07 11:45:29 +0200250 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != NVME_IDENTIFY_DATA_SIZE);
251 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != NVME_IDENTIFY_DATA_SIZE);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500252 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600253 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Helen Koikef9f38e32017-04-10 12:51:07 -0300254 BUILD_BUG_ON(sizeof(struct nvme_dbbuf) != 64);
255}
256
Jens Axboe3b6592f2018-10-31 08:36:31 -0600257static unsigned int max_io_queues(void)
258{
Jens Axboe4b04cc62018-11-05 12:44:33 -0700259 return num_possible_cpus() + write_queues + poll_queues;
Jens Axboe3b6592f2018-10-31 08:36:31 -0600260}
261
262static unsigned int max_queue_count(void)
263{
264 /* IO queues + admin queue */
265 return 1 + max_io_queues();
266}
267
Helen Koikef9f38e32017-04-10 12:51:07 -0300268static inline unsigned int nvme_dbbuf_size(u32 stride)
269{
Jens Axboe3b6592f2018-10-31 08:36:31 -0600270 return (max_queue_count() * 8 * stride);
Helen Koikef9f38e32017-04-10 12:51:07 -0300271}
272
273static int nvme_dbbuf_dma_alloc(struct nvme_dev *dev)
274{
275 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
276
277 if (dev->dbbuf_dbs)
278 return 0;
279
280 dev->dbbuf_dbs = dma_alloc_coherent(dev->dev, mem_size,
281 &dev->dbbuf_dbs_dma_addr,
282 GFP_KERNEL);
283 if (!dev->dbbuf_dbs)
284 return -ENOMEM;
285 dev->dbbuf_eis = dma_alloc_coherent(dev->dev, mem_size,
286 &dev->dbbuf_eis_dma_addr,
287 GFP_KERNEL);
288 if (!dev->dbbuf_eis) {
289 dma_free_coherent(dev->dev, mem_size,
290 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
291 dev->dbbuf_dbs = NULL;
292 return -ENOMEM;
293 }
294
295 return 0;
296}
297
298static void nvme_dbbuf_dma_free(struct nvme_dev *dev)
299{
300 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
301
302 if (dev->dbbuf_dbs) {
303 dma_free_coherent(dev->dev, mem_size,
304 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
305 dev->dbbuf_dbs = NULL;
306 }
307 if (dev->dbbuf_eis) {
308 dma_free_coherent(dev->dev, mem_size,
309 dev->dbbuf_eis, dev->dbbuf_eis_dma_addr);
310 dev->dbbuf_eis = NULL;
311 }
312}
313
314static void nvme_dbbuf_init(struct nvme_dev *dev,
315 struct nvme_queue *nvmeq, int qid)
316{
317 if (!dev->dbbuf_dbs || !qid)
318 return;
319
320 nvmeq->dbbuf_sq_db = &dev->dbbuf_dbs[sq_idx(qid, dev->db_stride)];
321 nvmeq->dbbuf_cq_db = &dev->dbbuf_dbs[cq_idx(qid, dev->db_stride)];
322 nvmeq->dbbuf_sq_ei = &dev->dbbuf_eis[sq_idx(qid, dev->db_stride)];
323 nvmeq->dbbuf_cq_ei = &dev->dbbuf_eis[cq_idx(qid, dev->db_stride)];
324}
325
326static void nvme_dbbuf_set(struct nvme_dev *dev)
327{
328 struct nvme_command c;
329
330 if (!dev->dbbuf_dbs)
331 return;
332
333 memset(&c, 0, sizeof(c));
334 c.dbbuf.opcode = nvme_admin_dbbuf;
335 c.dbbuf.prp1 = cpu_to_le64(dev->dbbuf_dbs_dma_addr);
336 c.dbbuf.prp2 = cpu_to_le64(dev->dbbuf_eis_dma_addr);
337
338 if (nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0)) {
Christoph Hellwig9bdcfb12017-05-20 15:14:43 +0200339 dev_warn(dev->ctrl.device, "unable to set dbbuf\n");
Helen Koikef9f38e32017-04-10 12:51:07 -0300340 /* Free memory and continue on */
341 nvme_dbbuf_dma_free(dev);
342 }
343}
344
345static inline int nvme_dbbuf_need_event(u16 event_idx, u16 new_idx, u16 old)
346{
347 return (u16)(new_idx - event_idx - 1) < (u16)(new_idx - old);
348}
349
350/* Update dbbuf and return true if an MMIO is required */
351static bool nvme_dbbuf_update_and_check_event(u16 value, u32 *dbbuf_db,
352 volatile u32 *dbbuf_ei)
353{
354 if (dbbuf_db) {
355 u16 old_value;
356
357 /*
358 * Ensure that the queue is written before updating
359 * the doorbell in memory
360 */
361 wmb();
362
363 old_value = *dbbuf_db;
364 *dbbuf_db = value;
365
Michal Wnukowskif1ed3df2018-08-15 15:51:57 -0700366 /*
367 * Ensure that the doorbell is updated before reading the event
368 * index from memory. The controller needs to provide similar
369 * ordering to ensure the envent index is updated before reading
370 * the doorbell.
371 */
372 mb();
373
Helen Koikef9f38e32017-04-10 12:51:07 -0300374 if (!nvme_dbbuf_need_event(*dbbuf_ei, value, old_value))
375 return false;
376 }
377
378 return true;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500379}
380
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700381/*
382 * Max size of iod being embedded in the request payload
383 */
384#define NVME_INT_PAGES 2
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100385#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700386
387/*
388 * Will slightly overestimate the number of pages needed. This is OK
389 * as it only leads to a small amount of wasted memory for the lifetime of
390 * the I/O.
391 */
392static int nvme_npages(unsigned size, struct nvme_dev *dev)
393{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100394 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
395 dev->ctrl.page_size);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700396 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
397}
398
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700399/*
400 * Calculates the number of pages needed for the SGL segments. For example a 4k
401 * page can accommodate 256 SGL descriptors.
402 */
403static int nvme_pci_npages_sgl(unsigned int num_seg)
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100404{
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700405 return DIV_ROUND_UP(num_seg * sizeof(struct nvme_sgl_desc), PAGE_SIZE);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100406}
407
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700408static unsigned int nvme_pci_iod_alloc_size(struct nvme_dev *dev,
409 unsigned int size, unsigned int nseg, bool use_sgl)
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700410{
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700411 size_t alloc_size;
412
413 if (use_sgl)
414 alloc_size = sizeof(__le64 *) * nvme_pci_npages_sgl(nseg);
415 else
416 alloc_size = sizeof(__le64 *) * nvme_npages(size, dev);
417
418 return alloc_size + sizeof(struct scatterlist) * nseg;
419}
420
421static unsigned int nvme_pci_cmd_size(struct nvme_dev *dev, bool use_sgl)
422{
423 unsigned int alloc_size = nvme_pci_iod_alloc_size(dev,
424 NVME_INT_BYTES(dev), NVME_INT_PAGES,
425 use_sgl);
426
427 return sizeof(struct nvme_iod) + alloc_size;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700428}
429
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700430static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
431 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500432{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700433 struct nvme_dev *dev = data;
Sagi Grimberg147b27e2018-01-14 12:39:01 +0200434 struct nvme_queue *nvmeq = &dev->queues[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700435
Keith Busch42483222015-06-01 09:29:54 -0600436 WARN_ON(hctx_idx != 0);
437 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
438 WARN_ON(nvmeq->tags);
439
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700440 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600441 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700442 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500443}
444
Keith Busch4af0e212015-06-08 10:08:13 -0600445static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
446{
447 struct nvme_queue *nvmeq = hctx->driver_data;
448
449 nvmeq->tags = NULL;
450}
451
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700452static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
453 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500454{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700455 struct nvme_dev *dev = data;
Sagi Grimberg147b27e2018-01-14 12:39:01 +0200456 struct nvme_queue *nvmeq = &dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500457
Keith Busch42483222015-06-01 09:29:54 -0600458 if (!nvmeq->tags)
459 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500460
Keith Busch42483222015-06-01 09:29:54 -0600461 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700462 hctx->driver_data = nvmeq;
463 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500464}
465
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600466static int nvme_init_request(struct blk_mq_tag_set *set, struct request *req,
467 unsigned int hctx_idx, unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500468{
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600469 struct nvme_dev *dev = set->driver_data;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100470 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwig03508152017-06-13 09:15:18 +0200471 int queue_idx = (set == &dev->tagset) ? hctx_idx + 1 : 0;
Sagi Grimberg147b27e2018-01-14 12:39:01 +0200472 struct nvme_queue *nvmeq = &dev->queues[queue_idx];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700473
474 BUG_ON(!nvmeq);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100475 iod->nvmeq = nvmeq;
Sagi Grimberg59e29ce2018-06-29 16:50:00 -0600476
477 nvme_req(req)->ctrl = &dev->ctrl;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700478 return 0;
479}
480
Jens Axboe3b6592f2018-10-31 08:36:31 -0600481static int queue_irq_offset(struct nvme_dev *dev)
482{
483 /* if we have more than 1 vec, admin queue offsets us by 1 */
484 if (dev->num_vecs > 1)
485 return 1;
486
487 return 0;
488}
489
Christoph Hellwigdca51e72016-09-14 16:18:57 +0200490static int nvme_pci_map_queues(struct blk_mq_tag_set *set)
491{
492 struct nvme_dev *dev = set->driver_data;
Jens Axboe3b6592f2018-10-31 08:36:31 -0600493 int i, qoff, offset;
Christoph Hellwigdca51e72016-09-14 16:18:57 +0200494
Jens Axboe3b6592f2018-10-31 08:36:31 -0600495 offset = queue_irq_offset(dev);
496 for (i = 0, qoff = 0; i < set->nr_maps; i++) {
497 struct blk_mq_queue_map *map = &set->map[i];
498
499 map->nr_queues = dev->io_queues[i];
500 if (!map->nr_queues) {
501 BUG_ON(i == NVMEQ_TYPE_READ);
502
503 /* shared set, resuse read set parameters */
504 map->nr_queues = dev->io_queues[NVMEQ_TYPE_READ];
505 qoff = 0;
506 offset = queue_irq_offset(dev);
507 }
508
Jens Axboe4b04cc62018-11-05 12:44:33 -0700509 /*
510 * The poll queue(s) doesn't have an IRQ (and hence IRQ
511 * affinity), so use the regular blk-mq cpu mapping
512 */
Jens Axboe3b6592f2018-10-31 08:36:31 -0600513 map->queue_offset = qoff;
Jens Axboe4b04cc62018-11-05 12:44:33 -0700514 if (i != NVMEQ_TYPE_POLL)
515 blk_mq_pci_map_queues(map, to_pci_dev(dev->dev), offset);
516 else
517 blk_mq_map_queues(map);
Jens Axboe3b6592f2018-10-31 08:36:31 -0600518 qoff += map->nr_queues;
519 offset += map->nr_queues;
520 }
521
522 return 0;
Christoph Hellwigdca51e72016-09-14 16:18:57 +0200523}
524
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500525/**
Christoph Hellwig90ea5ca2018-05-26 13:45:55 +0200526 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500527 * @nvmeq: The queue to use
528 * @cmd: The command to send
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500529 */
Christoph Hellwig90ea5ca2018-05-26 13:45:55 +0200530static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500531{
Christoph Hellwig90ea5ca2018-05-26 13:45:55 +0200532 spin_lock(&nvmeq->sq_lock);
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -0600533
534 memcpy(&nvmeq->sq_cmds[nvmeq->sq_tail], cmd, sizeof(*cmd));
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600535
Christoph Hellwig90ea5ca2018-05-26 13:45:55 +0200536 if (++nvmeq->sq_tail == nvmeq->q_depth)
537 nvmeq->sq_tail = 0;
538 if (nvme_dbbuf_update_and_check_event(nvmeq->sq_tail,
539 nvmeq->dbbuf_sq_db, nvmeq->dbbuf_sq_ei))
540 writel(nvmeq->sq_tail, nvmeq->q_db);
541 spin_unlock(&nvmeq->sq_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500542}
543
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700544static void **nvme_pci_iod_list(struct request *req)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700545{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100546 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700547 return (void **)(iod->sg + blk_rq_nr_phys_segments(req));
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700548}
549
Minwoo Im955b1b52017-12-20 16:30:50 +0900550static inline bool nvme_pci_use_sgls(struct nvme_dev *dev, struct request *req)
551{
552 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Keith Busch20469a32018-01-17 22:04:37 +0100553 int nseg = blk_rq_nr_phys_segments(req);
Minwoo Im955b1b52017-12-20 16:30:50 +0900554 unsigned int avg_seg_size;
555
Keith Busch20469a32018-01-17 22:04:37 +0100556 if (nseg == 0)
557 return false;
558
559 avg_seg_size = DIV_ROUND_UP(blk_rq_payload_bytes(req), nseg);
Minwoo Im955b1b52017-12-20 16:30:50 +0900560
561 if (!(dev->ctrl.sgls & ((1 << 0) | (1 << 1))))
562 return false;
563 if (!iod->nvmeq->qid)
564 return false;
565 if (!sgl_threshold || avg_seg_size < sgl_threshold)
566 return false;
567 return true;
568}
569
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200570static blk_status_t nvme_init_iod(struct request *rq, struct nvme_dev *dev)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500571{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100572 struct nvme_iod *iod = blk_mq_rq_to_pdu(rq);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700573 int nseg = blk_rq_nr_phys_segments(rq);
Christoph Hellwigb131c612017-01-13 12:29:12 +0100574 unsigned int size = blk_rq_payload_bytes(rq);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500575
Minwoo Im955b1b52017-12-20 16:30:50 +0900576 iod->use_sgl = nvme_pci_use_sgls(dev, rq);
577
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100578 if (nseg > NVME_INT_PAGES || size > NVME_INT_BYTES(dev)) {
Jens Axboe943e9422018-06-21 09:49:37 -0600579 iod->sg = mempool_alloc(dev->iod_mempool, GFP_ATOMIC);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100580 if (!iod->sg)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200581 return BLK_STS_RESOURCE;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100582 } else {
583 iod->sg = iod->inline_sg;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700584 }
585
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100586 iod->aborted = 0;
587 iod->npages = -1;
588 iod->nents = 0;
589 iod->length = size;
Keith Buschf80ec962016-07-12 16:20:31 -0700590
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200591 return BLK_STS_OK;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700592}
593
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100594static void nvme_free_iod(struct nvme_dev *dev, struct request *req)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500595{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100596 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700597 const int last_prp = dev->ctrl.page_size / sizeof(__le64) - 1;
598 dma_addr_t dma_addr = iod->first_dma, next_dma_addr;
599
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500600 int i;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500601
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500602 if (iod->npages == 0)
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700603 dma_pool_free(dev->prp_small_pool, nvme_pci_iod_list(req)[0],
604 dma_addr);
605
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500606 for (i = 0; i < iod->npages; i++) {
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700607 void *addr = nvme_pci_iod_list(req)[i];
608
609 if (iod->use_sgl) {
610 struct nvme_sgl_desc *sg_list = addr;
611
612 next_dma_addr =
613 le64_to_cpu((sg_list[SGES_PER_PAGE - 1]).addr);
614 } else {
615 __le64 *prp_list = addr;
616
617 next_dma_addr = le64_to_cpu(prp_list[last_prp]);
618 }
619
620 dma_pool_free(dev->prp_page_pool, addr, dma_addr);
621 dma_addr = next_dma_addr;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500622 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700623
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100624 if (iod->sg != iod->inline_sg)
Jens Axboe943e9422018-06-21 09:49:37 -0600625 mempool_free(iod->sg, dev->iod_mempool);
Keith Buschb4ff9c82014-08-29 09:06:12 -0600626}
627
Keith Buschd0877472017-09-15 13:05:38 -0400628static void nvme_print_sgl(struct scatterlist *sgl, int nents)
629{
630 int i;
631 struct scatterlist *sg;
632
633 for_each_sg(sgl, sg, nents, i) {
634 dma_addr_t phys = sg_phys(sg);
635 pr_warn("sg[%d] phys_addr:%pad offset:%d length:%d "
636 "dma_address:%pad dma_length:%d\n",
637 i, &phys, sg->offset, sg->length, &sg_dma_address(sg),
638 sg_dma_len(sg));
639 }
640}
641
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700642static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev,
643 struct request *req, struct nvme_rw_command *cmnd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500644{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100645 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500646 struct dma_pool *pool;
Christoph Hellwigb131c612017-01-13 12:29:12 +0100647 int length = blk_rq_payload_bytes(req);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500648 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500649 int dma_len = sg_dma_len(sg);
650 u64 dma_addr = sg_dma_address(sg);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100651 u32 page_size = dev->ctrl.page_size;
Murali Iyerf137e0f2015-03-26 11:07:51 -0500652 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500653 __le64 *prp_list;
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700654 void **list = nvme_pci_iod_list(req);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500655 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500656 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500657
Keith Busch1d090622014-06-23 11:34:01 -0600658 length -= (page_size - offset);
Jan H. Schönherr5228b322017-08-27 15:56:37 +0200659 if (length <= 0) {
660 iod->first_dma = 0;
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700661 goto done;
Jan H. Schönherr5228b322017-08-27 15:56:37 +0200662 }
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500663
Keith Busch1d090622014-06-23 11:34:01 -0600664 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500665 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600666 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500667 } else {
668 sg = sg_next(sg);
669 dma_addr = sg_dma_address(sg);
670 dma_len = sg_dma_len(sg);
671 }
672
Keith Busch1d090622014-06-23 11:34:01 -0600673 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600674 iod->first_dma = dma_addr;
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700675 goto done;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500676 }
677
Keith Busch1d090622014-06-23 11:34:01 -0600678 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500679 if (nprps <= (256 / 8)) {
680 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500681 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500682 } else {
683 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500684 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500685 }
686
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200687 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400688 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600689 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500690 iod->npages = -1;
Keith Busch86eea282017-07-12 15:59:07 -0400691 return BLK_STS_RESOURCE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400692 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500693 list[0] = prp_list;
694 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500695 i = 0;
696 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600697 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500698 __le64 *old_prp_list = prp_list;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200699 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500700 if (!prp_list)
Keith Busch86eea282017-07-12 15:59:07 -0400701 return BLK_STS_RESOURCE;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500702 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400703 prp_list[0] = old_prp_list[i - 1];
704 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
705 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500706 }
707 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600708 dma_len -= page_size;
709 dma_addr += page_size;
710 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500711 if (length <= 0)
712 break;
713 if (dma_len > 0)
714 continue;
Keith Busch86eea282017-07-12 15:59:07 -0400715 if (unlikely(dma_len < 0))
716 goto bad_sgl;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500717 sg = sg_next(sg);
718 dma_addr = sg_dma_address(sg);
719 dma_len = sg_dma_len(sg);
720 }
721
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700722done:
723 cmnd->dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
724 cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma);
725
Keith Busch86eea282017-07-12 15:59:07 -0400726 return BLK_STS_OK;
727
728 bad_sgl:
Keith Buschd0877472017-09-15 13:05:38 -0400729 WARN(DO_ONCE(nvme_print_sgl, iod->sg, iod->nents),
730 "Invalid SGL for payload:%d nents:%d\n",
731 blk_rq_payload_bytes(req), iod->nents);
Keith Busch86eea282017-07-12 15:59:07 -0400732 return BLK_STS_IOERR;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500733}
734
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700735static void nvme_pci_sgl_set_data(struct nvme_sgl_desc *sge,
736 struct scatterlist *sg)
737{
738 sge->addr = cpu_to_le64(sg_dma_address(sg));
739 sge->length = cpu_to_le32(sg_dma_len(sg));
740 sge->type = NVME_SGL_FMT_DATA_DESC << 4;
741}
742
743static void nvme_pci_sgl_set_seg(struct nvme_sgl_desc *sge,
744 dma_addr_t dma_addr, int entries)
745{
746 sge->addr = cpu_to_le64(dma_addr);
747 if (entries < SGES_PER_PAGE) {
748 sge->length = cpu_to_le32(entries * sizeof(*sge));
749 sge->type = NVME_SGL_FMT_LAST_SEG_DESC << 4;
750 } else {
751 sge->length = cpu_to_le32(PAGE_SIZE);
752 sge->type = NVME_SGL_FMT_SEG_DESC << 4;
753 }
754}
755
756static blk_status_t nvme_pci_setup_sgls(struct nvme_dev *dev,
Christoph Hellwigb0f28532018-01-17 22:04:38 +0100757 struct request *req, struct nvme_rw_command *cmd, int entries)
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700758{
759 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700760 struct dma_pool *pool;
761 struct nvme_sgl_desc *sg_list;
762 struct scatterlist *sg = iod->sg;
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700763 dma_addr_t sgl_dma;
Christoph Hellwigb0f28532018-01-17 22:04:38 +0100764 int i = 0;
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700765
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700766 /* setting the transfer type as SGL */
767 cmd->flags = NVME_CMD_SGL_METABUF;
768
Christoph Hellwigb0f28532018-01-17 22:04:38 +0100769 if (entries == 1) {
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700770 nvme_pci_sgl_set_data(&cmd->dptr.sgl, sg);
771 return BLK_STS_OK;
772 }
773
774 if (entries <= (256 / sizeof(struct nvme_sgl_desc))) {
775 pool = dev->prp_small_pool;
776 iod->npages = 0;
777 } else {
778 pool = dev->prp_page_pool;
779 iod->npages = 1;
780 }
781
782 sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma);
783 if (!sg_list) {
784 iod->npages = -1;
785 return BLK_STS_RESOURCE;
786 }
787
788 nvme_pci_iod_list(req)[0] = sg_list;
789 iod->first_dma = sgl_dma;
790
791 nvme_pci_sgl_set_seg(&cmd->dptr.sgl, sgl_dma, entries);
792
793 do {
794 if (i == SGES_PER_PAGE) {
795 struct nvme_sgl_desc *old_sg_desc = sg_list;
796 struct nvme_sgl_desc *link = &old_sg_desc[i - 1];
797
798 sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma);
799 if (!sg_list)
800 return BLK_STS_RESOURCE;
801
802 i = 0;
803 nvme_pci_iod_list(req)[iod->npages++] = sg_list;
804 sg_list[i++] = *link;
805 nvme_pci_sgl_set_seg(link, sgl_dma, entries);
806 }
807
808 nvme_pci_sgl_set_data(&sg_list[i++], sg);
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700809 sg = sg_next(sg);
Christoph Hellwigb0f28532018-01-17 22:04:38 +0100810 } while (--entries > 0);
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700811
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700812 return BLK_STS_OK;
813}
814
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200815static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,
Christoph Hellwigb131c612017-01-13 12:29:12 +0100816 struct nvme_command *cmnd)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200817{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100818 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200819 struct request_queue *q = req->q;
820 enum dma_data_direction dma_dir = rq_data_dir(req) ?
821 DMA_TO_DEVICE : DMA_FROM_DEVICE;
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200822 blk_status_t ret = BLK_STS_IOERR;
Christoph Hellwigb0f28532018-01-17 22:04:38 +0100823 int nr_mapped;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200824
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700825 sg_init_table(iod->sg, blk_rq_nr_phys_segments(req));
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200826 iod->nents = blk_rq_map_sg(q, req, iod->sg);
827 if (!iod->nents)
828 goto out;
829
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200830 ret = BLK_STS_RESOURCE;
Logan Gunthorpee0596ab2018-10-04 15:27:44 -0600831
832 if (is_pci_p2pdma_page(sg_page(iod->sg)))
833 nr_mapped = pci_p2pdma_map_sg(dev->dev, iod->sg, iod->nents,
834 dma_dir);
835 else
836 nr_mapped = dma_map_sg_attrs(dev->dev, iod->sg, iod->nents,
837 dma_dir, DMA_ATTR_NO_WARN);
Christoph Hellwigb0f28532018-01-17 22:04:38 +0100838 if (!nr_mapped)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200839 goto out;
840
Minwoo Im955b1b52017-12-20 16:30:50 +0900841 if (iod->use_sgl)
Christoph Hellwigb0f28532018-01-17 22:04:38 +0100842 ret = nvme_pci_setup_sgls(dev, req, &cmnd->rw, nr_mapped);
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -0700843 else
844 ret = nvme_pci_setup_prps(dev, req, &cmnd->rw);
845
Keith Busch86eea282017-07-12 15:59:07 -0400846 if (ret != BLK_STS_OK)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200847 goto out_unmap;
848
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200849 ret = BLK_STS_IOERR;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200850 if (blk_integrity_rq(req)) {
851 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
852 goto out_unmap;
853
Christoph Hellwigbf684052015-10-26 17:12:51 +0900854 sg_init_table(&iod->meta_sg, 1);
855 if (blk_rq_map_integrity_sg(q, req->bio, &iod->meta_sg) != 1)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200856 goto out_unmap;
857
Christoph Hellwigbf684052015-10-26 17:12:51 +0900858 if (!dma_map_sg(dev->dev, &iod->meta_sg, 1, dma_dir))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200859 goto out_unmap;
Chaitanya Kulkarni3045c0d2018-10-17 11:34:15 -0700860
861 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(&iod->meta_sg));
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200862 }
863
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200864 return BLK_STS_OK;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200865
866out_unmap:
867 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
868out:
869 return ret;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200870}
871
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100872static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100873{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100874 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100875 enum dma_data_direction dma_dir = rq_data_dir(req) ?
876 DMA_TO_DEVICE : DMA_FROM_DEVICE;
877
878 if (iod->nents) {
Logan Gunthorpee0596ab2018-10-04 15:27:44 -0600879 /* P2PDMA requests do not need to be unmapped */
880 if (!is_pci_p2pdma_page(sg_page(iod->sg)))
881 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
882
Max Gurtovoyf7f1fc32018-07-30 00:15:33 +0300883 if (blk_integrity_rq(req))
Christoph Hellwigbf684052015-10-26 17:12:51 +0900884 dma_unmap_sg(dev->dev, &iod->meta_sg, 1, dma_dir);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100885 }
886
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700887 nvme_cleanup_cmd(req);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100888 nvme_free_iod(dev, req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500889}
890
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700891/*
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200892 * NOTE: ns is NULL when called on the admin queue.
893 */
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200894static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700895 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600896{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700897 struct nvme_ns *ns = hctx->queue->queuedata;
898 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200899 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700900 struct request *req = bd->rq;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200901 struct nvme_command cmnd;
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200902 blk_status_t ret;
Keith Busche1e5e562015-02-19 13:39:03 -0700903
Jens Axboed1f06f42018-05-17 18:31:49 +0200904 /*
905 * We should not need to do this, but we're still using this to
906 * ensure we can drain requests on a dying queue.
907 */
Jens Axboe4b04cc62018-11-05 12:44:33 -0700908 if (unlikely(nvmeq->cq_vector < 0 && !nvmeq->polled))
Jens Axboed1f06f42018-05-17 18:31:49 +0200909 return BLK_STS_IOERR;
910
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700911 ret = nvme_setup_cmd(ns, req, &cmnd);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200912 if (ret)
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100913 return ret;
Keith Buschedd10d32014-04-03 16:45:23 -0600914
Christoph Hellwigb131c612017-01-13 12:29:12 +0100915 ret = nvme_init_iod(req, dev);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200916 if (ret)
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700917 goto out_free_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600918
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200919 if (blk_rq_nr_phys_segments(req)) {
Christoph Hellwigb131c612017-01-13 12:29:12 +0100920 ret = nvme_map_data(dev, req, &cmnd);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200921 if (ret)
922 goto out_cleanup_iod;
923 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700924
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100925 blk_mq_start_request(req);
Christoph Hellwig90ea5ca2018-05-26 13:45:55 +0200926 nvme_submit_cmd(nvmeq, &cmnd);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200927 return BLK_STS_OK;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700928out_cleanup_iod:
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100929 nvme_free_iod(dev, req);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700930out_free_cmd:
931 nvme_cleanup_cmd(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200932 return ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500933}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500934
Jens Axboe3b6592f2018-10-31 08:36:31 -0600935static int nvme_rq_flags_to_type(struct request_queue *q, unsigned int flags)
936{
Jens Axboe4b04cc62018-11-05 12:44:33 -0700937 if ((flags & REQ_HIPRI) && test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
938 return NVMEQ_TYPE_POLL;
Jens Axboe3b6592f2018-10-31 08:36:31 -0600939 if ((flags & REQ_OP_MASK) == REQ_OP_READ)
940 return NVMEQ_TYPE_READ;
941
942 return NVMEQ_TYPE_WRITE;
943}
944
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200945static void nvme_pci_complete_rq(struct request *req)
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100946{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100947 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100948
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200949 nvme_unmap_data(iod->nvmeq->dev, req);
950 nvme_complete_rq(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500951}
952
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100953/* We read the CQE phase first to check if the rest of the entry is valid */
Christoph Hellwig750dde42018-05-18 08:37:04 -0600954static inline bool nvme_cqe_pending(struct nvme_queue *nvmeq)
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100955{
Christoph Hellwig750dde42018-05-18 08:37:04 -0600956 return (le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
957 nvmeq->cq_phase;
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100958}
959
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300960static inline void nvme_ring_cq_doorbell(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500961{
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300962 u16 head = nvmeq->cq_head;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500963
Keith Busch397c6992018-06-06 08:13:05 -0600964 if (nvme_dbbuf_update_and_check_event(head, nvmeq->dbbuf_cq_db,
965 nvmeq->dbbuf_cq_ei))
966 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300967}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500968
Jens Axboe5cb525c2018-05-17 18:31:50 +0200969static inline void nvme_handle_cqe(struct nvme_queue *nvmeq, u16 idx)
Sagi Grimberg83a12fb2017-06-18 17:28:08 +0300970{
Jens Axboe5cb525c2018-05-17 18:31:50 +0200971 volatile struct nvme_completion *cqe = &nvmeq->cqes[idx];
Sagi Grimberg83a12fb2017-06-18 17:28:08 +0300972 struct request *req;
973
974 if (unlikely(cqe->command_id >= nvmeq->q_depth)) {
975 dev_warn(nvmeq->dev->ctrl.device,
976 "invalid id %d completed on queue %d\n",
977 cqe->command_id, le16_to_cpu(cqe->sq_id));
978 return;
979 }
980
981 /*
982 * AEN requests are special as they don't time out and can
983 * survive any kind of queue freeze and often don't respond to
984 * aborts. We don't even bother to allocate a struct request
985 * for them but rather special case them here.
986 */
987 if (unlikely(nvmeq->qid == 0 &&
Keith Busch38dabe22017-11-07 15:13:10 -0700988 cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH)) {
Sagi Grimberg83a12fb2017-06-18 17:28:08 +0300989 nvme_complete_async_event(&nvmeq->dev->ctrl,
990 cqe->status, &cqe->result);
991 return;
992 }
993
994 req = blk_mq_tag_to_rq(*nvmeq->tags, cqe->command_id);
995 nvme_end_request(req, cqe->status, cqe->result);
996}
997
Jens Axboe5cb525c2018-05-17 18:31:50 +0200998static void nvme_complete_cqes(struct nvme_queue *nvmeq, u16 start, u16 end)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500999{
Jens Axboe5cb525c2018-05-17 18:31:50 +02001000 while (start != end) {
1001 nvme_handle_cqe(nvmeq, start);
1002 if (++start == nvmeq->q_depth)
1003 start = 0;
Sagi Grimberg920d13a2017-06-18 17:28:09 +03001004 }
Jens Axboea0fa9642015-11-03 20:37:26 -07001005}
1006
Jens Axboe5cb525c2018-05-17 18:31:50 +02001007static inline void nvme_update_cq_head(struct nvme_queue *nvmeq)
Jens Axboea0fa9642015-11-03 20:37:26 -07001008{
Jens Axboe5cb525c2018-05-17 18:31:50 +02001009 if (++nvmeq->cq_head == nvmeq->q_depth) {
1010 nvmeq->cq_head = 0;
1011 nvmeq->cq_phase = !nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001012 }
Jens Axboe5cb525c2018-05-17 18:31:50 +02001013}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001014
Jens Axboe5cb525c2018-05-17 18:31:50 +02001015static inline bool nvme_process_cq(struct nvme_queue *nvmeq, u16 *start,
1016 u16 *end, int tag)
1017{
1018 bool found = false;
1019
1020 *start = nvmeq->cq_head;
1021 while (!found && nvme_cqe_pending(nvmeq)) {
1022 if (nvmeq->cqes[nvmeq->cq_head].command_id == tag)
1023 found = true;
1024 nvme_update_cq_head(nvmeq);
1025 }
1026 *end = nvmeq->cq_head;
1027
1028 if (*start != *end)
Sagi Grimberg920d13a2017-06-18 17:28:09 +03001029 nvme_ring_cq_doorbell(nvmeq);
Jens Axboe5cb525c2018-05-17 18:31:50 +02001030 return found;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001031}
1032
1033static irqreturn_t nvme_irq(int irq, void *data)
1034{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001035 struct nvme_queue *nvmeq = data;
Jens Axboe68fa9db2018-05-21 08:41:52 -06001036 irqreturn_t ret = IRQ_NONE;
Jens Axboe5cb525c2018-05-17 18:31:50 +02001037 u16 start, end;
1038
Jens Axboe1ab0cd62018-05-17 18:31:51 +02001039 spin_lock(&nvmeq->cq_lock);
Jens Axboe68fa9db2018-05-21 08:41:52 -06001040 if (nvmeq->cq_head != nvmeq->last_cq_head)
1041 ret = IRQ_HANDLED;
Jens Axboe5cb525c2018-05-17 18:31:50 +02001042 nvme_process_cq(nvmeq, &start, &end, -1);
Jens Axboe68fa9db2018-05-21 08:41:52 -06001043 nvmeq->last_cq_head = nvmeq->cq_head;
Jens Axboe1ab0cd62018-05-17 18:31:51 +02001044 spin_unlock(&nvmeq->cq_lock);
Jens Axboe5cb525c2018-05-17 18:31:50 +02001045
Jens Axboe68fa9db2018-05-21 08:41:52 -06001046 if (start != end) {
1047 nvme_complete_cqes(nvmeq, start, end);
1048 return IRQ_HANDLED;
1049 }
1050
1051 return ret;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001052}
1053
1054static irqreturn_t nvme_irq_check(int irq, void *data)
1055{
1056 struct nvme_queue *nvmeq = data;
Christoph Hellwig750dde42018-05-18 08:37:04 -06001057 if (nvme_cqe_pending(nvmeq))
Marta Rybczynskad783e0b2016-03-22 16:02:06 +01001058 return IRQ_WAKE_THREAD;
1059 return IRQ_NONE;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -05001060}
1061
Keith Busch7776db12017-02-24 17:59:28 -05001062static int __nvme_poll(struct nvme_queue *nvmeq, unsigned int tag)
Jens Axboea0fa9642015-11-03 20:37:26 -07001063{
Jens Axboe5cb525c2018-05-17 18:31:50 +02001064 u16 start, end;
1065 bool found;
Jens Axboea0fa9642015-11-03 20:37:26 -07001066
Christoph Hellwig750dde42018-05-18 08:37:04 -06001067 if (!nvme_cqe_pending(nvmeq))
Sagi Grimberg442e19b2017-06-18 17:28:10 +03001068 return 0;
Jens Axboea0fa9642015-11-03 20:37:26 -07001069
Jens Axboe1ab0cd62018-05-17 18:31:51 +02001070 spin_lock_irq(&nvmeq->cq_lock);
Jens Axboe5cb525c2018-05-17 18:31:50 +02001071 found = nvme_process_cq(nvmeq, &start, &end, tag);
Jens Axboe1ab0cd62018-05-17 18:31:51 +02001072 spin_unlock_irq(&nvmeq->cq_lock);
Sagi Grimberg442e19b2017-06-18 17:28:10 +03001073
Jens Axboe5cb525c2018-05-17 18:31:50 +02001074 nvme_complete_cqes(nvmeq, start, end);
Sagi Grimberg442e19b2017-06-18 17:28:10 +03001075 return found;
Jens Axboea0fa9642015-11-03 20:37:26 -07001076}
1077
Keith Busch7776db12017-02-24 17:59:28 -05001078static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1079{
1080 struct nvme_queue *nvmeq = hctx->driver_data;
1081
1082 return __nvme_poll(nvmeq, tag);
1083}
1084
Keith Buschad22c352017-11-07 15:13:12 -07001085static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001086{
Christoph Hellwigf866fc422016-04-26 13:52:00 +02001087 struct nvme_dev *dev = to_nvme_dev(ctrl);
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001088 struct nvme_queue *nvmeq = &dev->queues[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001089 struct nvme_command c;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001090
1091 memset(&c, 0, sizeof(c));
1092 c.common.opcode = nvme_admin_async_event;
Keith Buschad22c352017-11-07 15:13:12 -07001093 c.common.command_id = NVME_AQ_BLK_MQ_DEPTH;
Christoph Hellwig90ea5ca2018-05-26 13:45:55 +02001094 nvme_submit_cmd(nvmeq, &c);
Keith Busch4d115422013-12-10 13:10:40 -07001095}
1096
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001097static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1098{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001099 struct nvme_command c;
1100
1101 memset(&c, 0, sizeof(c));
1102 c.delete_queue.opcode = opcode;
1103 c.delete_queue.qid = cpu_to_le16(id);
1104
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001105 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001106}
1107
1108static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
Jianchao Wanga8e3e0b2018-05-24 17:51:33 +08001109 struct nvme_queue *nvmeq, s16 vector)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001110{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001111 struct nvme_command c;
Jens Axboe4b04cc62018-11-05 12:44:33 -07001112 int flags = NVME_QUEUE_PHYS_CONTIG;
1113
1114 if (vector != -1)
1115 flags |= NVME_CQ_IRQ_ENABLED;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001116
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001117 /*
Minwoo Im16772ae2017-10-18 22:56:09 +09001118 * Note: we (ab)use the fact that the prp fields survive if no data
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001119 * is attached to the request.
1120 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001121 memset(&c, 0, sizeof(c));
1122 c.create_cq.opcode = nvme_admin_create_cq;
1123 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1124 c.create_cq.cqid = cpu_to_le16(qid);
1125 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1126 c.create_cq.cq_flags = cpu_to_le16(flags);
Jens Axboe4b04cc62018-11-05 12:44:33 -07001127 if (vector != -1)
1128 c.create_cq.irq_vector = cpu_to_le16(vector);
1129 else
1130 c.create_cq.irq_vector = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001131
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001132 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001133}
1134
1135static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1136 struct nvme_queue *nvmeq)
1137{
Jens Axboe9abd68e2018-05-08 10:25:15 -06001138 struct nvme_ctrl *ctrl = &dev->ctrl;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001139 struct nvme_command c;
Keith Busch81c1cd92017-04-04 18:18:12 -04001140 int flags = NVME_QUEUE_PHYS_CONTIG;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001141
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001142 /*
Jens Axboe9abd68e2018-05-08 10:25:15 -06001143 * Some drives have a bug that auto-enables WRRU if MEDIUM isn't
1144 * set. Since URGENT priority is zeroes, it makes all queues
1145 * URGENT.
1146 */
1147 if (ctrl->quirks & NVME_QUIRK_MEDIUM_PRIO_SQ)
1148 flags |= NVME_SQ_PRIO_MEDIUM;
1149
1150 /*
Minwoo Im16772ae2017-10-18 22:56:09 +09001151 * Note: we (ab)use the fact that the prp fields survive if no data
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001152 * is attached to the request.
1153 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001154 memset(&c, 0, sizeof(c));
1155 c.create_sq.opcode = nvme_admin_create_sq;
1156 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1157 c.create_sq.sqid = cpu_to_le16(qid);
1158 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1159 c.create_sq.sq_flags = cpu_to_le16(flags);
1160 c.create_sq.cqid = cpu_to_le16(qid);
1161
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001162 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001163}
1164
1165static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1166{
1167 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1168}
1169
1170static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1171{
1172 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1173}
1174
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001175static void abort_endio(struct request *req, blk_status_t error)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001176{
Christoph Hellwigf4800d62015-11-28 15:43:10 +01001177 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
1178 struct nvme_queue *nvmeq = iod->nvmeq;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -04001179
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001180 dev_warn(nvmeq->dev->ctrl.device,
1181 "Abort status: 0x%x", nvme_req(req)->status);
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001182 atomic_inc(&nvmeq->dev->ctrl.abort_limit);
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001183 blk_mq_free_request(req);
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001184}
1185
Keith Buschb2a0eb12017-06-07 20:32:50 +02001186static bool nvme_should_reset(struct nvme_dev *dev, u32 csts)
1187{
1188
1189 /* If true, indicates loss of adapter communication, possibly by a
1190 * NVMe Subsystem reset.
1191 */
1192 bool nssro = dev->subsystem && (csts & NVME_CSTS_NSSRO);
1193
Jianchao Wangad700622018-01-22 22:03:16 +08001194 /* If there is a reset/reinit ongoing, we shouldn't reset again. */
1195 switch (dev->ctrl.state) {
1196 case NVME_CTRL_RESETTING:
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02001197 case NVME_CTRL_CONNECTING:
Keith Buschb2a0eb12017-06-07 20:32:50 +02001198 return false;
Jianchao Wangad700622018-01-22 22:03:16 +08001199 default:
1200 break;
1201 }
Keith Buschb2a0eb12017-06-07 20:32:50 +02001202
1203 /* We shouldn't reset unless the controller is on fatal error state
1204 * _or_ if we lost the communication with it.
1205 */
1206 if (!(csts & NVME_CSTS_CFS) && !nssro)
1207 return false;
1208
Keith Buschb2a0eb12017-06-07 20:32:50 +02001209 return true;
1210}
1211
1212static void nvme_warn_reset(struct nvme_dev *dev, u32 csts)
1213{
1214 /* Read a config register to help see what died. */
1215 u16 pci_status;
1216 int result;
1217
1218 result = pci_read_config_word(to_pci_dev(dev->dev), PCI_STATUS,
1219 &pci_status);
1220 if (result == PCIBIOS_SUCCESSFUL)
1221 dev_warn(dev->ctrl.device,
1222 "controller is down; will reset: CSTS=0x%x, PCI_STATUS=0x%hx\n",
1223 csts, pci_status);
1224 else
1225 dev_warn(dev->ctrl.device,
1226 "controller is down; will reset: CSTS=0x%x, PCI_STATUS read failed (%d)\n",
1227 csts, result);
1228}
1229
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001230static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001231{
Christoph Hellwigf4800d62015-11-28 15:43:10 +01001232 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
1233 struct nvme_queue *nvmeq = iod->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001234 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001235 struct request *abort_req;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001236 struct nvme_command cmd;
Keith Buschb2a0eb12017-06-07 20:32:50 +02001237 u32 csts = readl(dev->bar + NVME_REG_CSTS);
1238
Wen Xiong651438b2018-02-15 14:05:10 -06001239 /* If PCI error recovery process is happening, we cannot reset or
1240 * the recovery mechanism will surely fail.
1241 */
1242 mb();
1243 if (pci_channel_offline(to_pci_dev(dev->dev)))
1244 return BLK_EH_RESET_TIMER;
1245
Keith Buschb2a0eb12017-06-07 20:32:50 +02001246 /*
1247 * Reset immediately if the controller is failed
1248 */
1249 if (nvme_should_reset(dev, csts)) {
1250 nvme_warn_reset(dev, csts);
1251 nvme_dev_disable(dev, false);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001252 nvme_reset_ctrl(&dev->ctrl);
Christoph Hellwigdb8c48e2018-05-29 15:52:30 +02001253 return BLK_EH_DONE;
Keith Buschb2a0eb12017-06-07 20:32:50 +02001254 }
Keith Buschc30341d2013-12-10 13:10:38 -07001255
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001256 /*
Keith Busch7776db12017-02-24 17:59:28 -05001257 * Did we miss an interrupt?
1258 */
1259 if (__nvme_poll(nvmeq, req->tag)) {
1260 dev_warn(dev->ctrl.device,
1261 "I/O %d QID %d timeout, completion polled\n",
1262 req->tag, nvmeq->qid);
Christoph Hellwigdb8c48e2018-05-29 15:52:30 +02001263 return BLK_EH_DONE;
Keith Busch7776db12017-02-24 17:59:28 -05001264 }
1265
1266 /*
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001267 * Shutdown immediately if controller times out while starting. The
1268 * reset work will see the pci device disabled when it gets the forced
1269 * cancellation error. All outstanding requests are completed on
Christoph Hellwigdb8c48e2018-05-29 15:52:30 +02001270 * shutdown, so we return BLK_EH_DONE.
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001271 */
Keith Busch42441402018-02-08 08:55:34 -07001272 switch (dev->ctrl.state) {
1273 case NVME_CTRL_CONNECTING:
1274 case NVME_CTRL_RESETTING:
Keith Buschb9cac432018-05-24 14:34:55 -06001275 dev_warn_ratelimited(dev->ctrl.device,
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001276 "I/O %d QID %d timeout, disable controller\n",
1277 req->tag, nvmeq->qid);
Keith Buscha5cdb682016-01-12 14:41:18 -07001278 nvme_dev_disable(dev, false);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001279 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
Christoph Hellwigdb8c48e2018-05-29 15:52:30 +02001280 return BLK_EH_DONE;
Keith Busch42441402018-02-08 08:55:34 -07001281 default:
1282 break;
Keith Buschc30341d2013-12-10 13:10:38 -07001283 }
1284
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001285 /*
1286 * Shutdown the controller immediately and schedule a reset if the
1287 * command was already aborted once before and still hasn't been
1288 * returned to the driver, or if this is the admin queue.
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001289 */
Christoph Hellwigf4800d62015-11-28 15:43:10 +01001290 if (!nvmeq->qid || iod->aborted) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001291 dev_warn(dev->ctrl.device,
Keith Busche1569a12015-11-26 12:11:07 +01001292 "I/O %d QID %d timeout, reset controller\n",
1293 req->tag, nvmeq->qid);
Keith Buscha5cdb682016-01-12 14:41:18 -07001294 nvme_dev_disable(dev, false);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001295 nvme_reset_ctrl(&dev->ctrl);
Keith Buschc30341d2013-12-10 13:10:38 -07001296
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001297 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
Christoph Hellwigdb8c48e2018-05-29 15:52:30 +02001298 return BLK_EH_DONE;
Keith Buschc30341d2013-12-10 13:10:38 -07001299 }
Keith Buschc30341d2013-12-10 13:10:38 -07001300
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001301 if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) {
1302 atomic_inc(&dev->ctrl.abort_limit);
1303 return BLK_EH_RESET_TIMER;
1304 }
Keith Busch7bf7d772017-01-24 18:07:00 -05001305 iod->aborted = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001306
Keith Buschc30341d2013-12-10 13:10:38 -07001307 memset(&cmd, 0, sizeof(cmd));
1308 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001309 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001310 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Keith Buschc30341d2013-12-10 13:10:38 -07001311
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001312 dev_warn(nvmeq->dev->ctrl.device,
1313 "I/O %d QID %d timeout, aborting\n",
1314 req->tag, nvmeq->qid);
Keith Buschc30341d2013-12-10 13:10:38 -07001315
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001316 abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd,
Christoph Hellwigeb71f432016-06-13 16:45:23 +02001317 BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001318 if (IS_ERR(abort_req)) {
1319 atomic_inc(&dev->ctrl.abort_limit);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001320 return BLK_EH_RESET_TIMER;
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001321 }
Keith Buschc30341d2013-12-10 13:10:38 -07001322
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001323 abort_req->timeout = ADMIN_TIMEOUT;
1324 abort_req->end_io_data = NULL;
1325 blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio);
Keith Busch07836e62015-02-19 10:34:48 -07001326
Keith Busch7a509a62015-01-07 18:55:53 -07001327 /*
1328 * The aborted req will be completed on receiving the abort req.
1329 * We enable the timer again. If hit twice, it'll cause a device reset,
1330 * as the device then is in a faulty state.
1331 */
Keith Busch07836e62015-02-19 10:34:48 -07001332 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001333}
1334
Keith Buschf435c282014-07-07 09:14:42 -06001335static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001336{
1337 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1338 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001339
1340 if (nvmeq->sq_cmds) {
1341 if (nvmeq->sq_cmds_is_io)
1342 pci_free_p2pmem(to_pci_dev(nvmeq->q_dmadev),
1343 nvmeq->sq_cmds,
1344 SQ_SIZE(nvmeq->q_depth));
1345 else
1346 dma_free_coherent(nvmeq->q_dmadev,
1347 SQ_SIZE(nvmeq->q_depth),
1348 nvmeq->sq_cmds,
1349 nvmeq->sq_dma_addr);
1350 }
Matthew Wilcox9e866772012-08-03 13:55:56 -04001351}
1352
Keith Buscha1a5ef92013-12-16 13:50:00 -05001353static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001354{
1355 int i;
1356
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001357 for (i = dev->ctrl.queue_count - 1; i >= lowest; i--) {
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001358 dev->ctrl.queue_count--;
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001359 nvme_free_queue(&dev->queues[i]);
kaoudis121c7ad2015-01-14 21:01:58 -07001360 }
Keith Busch22404272013-07-15 15:02:20 -06001361}
1362
Keith Busch4d115422013-12-10 13:10:40 -07001363/**
1364 * nvme_suspend_queue - put queue into suspended state
Bart Van Assche40581d12018-10-08 14:28:43 -07001365 * @nvmeq: queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001366 */
1367static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001368{
Keith Busch2b25d982014-12-22 12:59:04 -07001369 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001370
Jens Axboe1ab0cd62018-05-17 18:31:51 +02001371 spin_lock_irq(&nvmeq->cq_lock);
Jens Axboe4b04cc62018-11-05 12:44:33 -07001372 if (nvmeq->cq_vector == -1 && !nvmeq->polled) {
Jens Axboe1ab0cd62018-05-17 18:31:51 +02001373 spin_unlock_irq(&nvmeq->cq_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001374 return 1;
1375 }
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001376 vector = nvmeq->cq_vector;
Keith Busch42f61422014-03-24 10:46:25 -06001377 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001378 nvmeq->cq_vector = -1;
Jens Axboe4b04cc62018-11-05 12:44:33 -07001379 nvmeq->polled = false;
Jens Axboe1ab0cd62018-05-17 18:31:51 +02001380 spin_unlock_irq(&nvmeq->cq_lock);
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001381
Jens Axboed1f06f42018-05-17 18:31:49 +02001382 /*
1383 * Ensure that nvme_queue_rq() sees it ->cq_vector == -1 without
1384 * having to grab the lock.
1385 */
1386 mb();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001387
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001388 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
Sagi Grimbergc81545f2017-07-02 15:53:27 +03001389 blk_mq_quiesce_queue(nvmeq->dev->ctrl.admin_q);
Keith Busch6df3dbc2015-03-26 13:49:33 -06001390
Jens Axboe4b04cc62018-11-05 12:44:33 -07001391 if (vector != -1)
1392 pci_free_irq(to_pci_dev(nvmeq->dev->dev), vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001393
Keith Busch4d115422013-12-10 13:10:40 -07001394 return 0;
1395}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001396
Keith Buscha5cdb682016-01-12 14:41:18 -07001397static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown)
Keith Busch4d115422013-12-10 13:10:40 -07001398{
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001399 struct nvme_queue *nvmeq = &dev->queues[0];
Jens Axboe5cb525c2018-05-17 18:31:50 +02001400 u16 start, end;
Keith Busch4d115422013-12-10 13:10:40 -07001401
Keith Buscha5cdb682016-01-12 14:41:18 -07001402 if (shutdown)
1403 nvme_shutdown_ctrl(&dev->ctrl);
1404 else
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001405 nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
Keith Busch07836e62015-02-19 10:34:48 -07001406
Jens Axboe1ab0cd62018-05-17 18:31:51 +02001407 spin_lock_irq(&nvmeq->cq_lock);
Jens Axboe5cb525c2018-05-17 18:31:50 +02001408 nvme_process_cq(nvmeq, &start, &end, -1);
Jens Axboe1ab0cd62018-05-17 18:31:51 +02001409 spin_unlock_irq(&nvmeq->cq_lock);
Jens Axboe5cb525c2018-05-17 18:31:50 +02001410
1411 nvme_complete_cqes(nvmeq, start, end);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001412}
1413
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001414static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1415 int entry_size)
1416{
1417 int q_depth = dev->q_depth;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001418 unsigned q_size_aligned = roundup(q_depth * entry_size,
1419 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001420
1421 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001422 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001423 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
Jon Derrickc45f5c92015-07-21 15:08:13 -06001424 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001425
1426 /*
1427 * Ensure the reduced q_depth is above some threshold where it
1428 * would be better to map queues in system memory with the
1429 * original depth
1430 */
1431 if (q_depth < 64)
1432 return -ENOMEM;
1433 }
1434
1435 return q_depth;
1436}
1437
1438static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1439 int qid, int depth)
1440{
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001441 struct pci_dev *pdev = to_pci_dev(dev->dev);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001442
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001443 if (qid && dev->cmb_use_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) {
1444 nvmeq->sq_cmds = pci_alloc_p2pmem(pdev, SQ_SIZE(depth));
1445 nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
1446 nvmeq->sq_cmds);
1447 nvmeq->sq_cmds_is_io = true;
1448 }
1449
1450 if (!nvmeq->sq_cmds) {
1451 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1452 &nvmeq->sq_dma_addr, GFP_KERNEL);
1453 nvmeq->sq_cmds_is_io = false;
1454 }
1455
Keith Busch815c6702018-02-13 05:44:44 -07001456 if (!nvmeq->sq_cmds)
1457 return -ENOMEM;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001458 return 0;
1459}
1460
Keith Buscha6ff7262018-04-12 09:16:09 -06001461static int nvme_alloc_queue(struct nvme_dev *dev, int qid, int depth)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001462{
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001463 struct nvme_queue *nvmeq = &dev->queues[qid];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001464
Keith Busch62314e42018-01-23 09:16:19 -07001465 if (dev->ctrl.queue_count > qid)
1466 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001467
Christoph Hellwige75ec752015-05-22 11:12:39 +02001468 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001469 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001470 if (!nvmeq->cqes)
1471 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001472
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001473 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001474 goto free_cqdma;
1475
Christoph Hellwige75ec752015-05-22 11:12:39 +02001476 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001477 nvmeq->dev = dev;
Jens Axboe1ab0cd62018-05-17 18:31:51 +02001478 spin_lock_init(&nvmeq->sq_lock);
1479 spin_lock_init(&nvmeq->cq_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001480 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001481 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001482 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001483 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001484 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001485 nvmeq->cq_vector = -1;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001486 dev->ctrl.queue_count++;
Jon Derrick36a7e992015-05-27 12:26:23 -06001487
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001488 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001489
1490 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001491 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001492 nvmeq->cq_dma_addr);
1493 free_nvmeq:
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001494 return -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001495}
1496
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001497static int queue_request_irq(struct nvme_queue *nvmeq)
Matthew Wilcox30010822011-01-20 09:10:15 -05001498{
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001499 struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev);
1500 int nr = nvmeq->dev->ctrl.instance;
1501
1502 if (use_threaded_interrupts) {
1503 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq_check,
1504 nvme_irq, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1505 } else {
1506 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq,
1507 NULL, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1508 }
Matthew Wilcox30010822011-01-20 09:10:15 -05001509}
1510
Keith Busch22404272013-07-15 15:02:20 -06001511static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001512{
Keith Busch22404272013-07-15 15:02:20 -06001513 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001514
Jens Axboe1ab0cd62018-05-17 18:31:51 +02001515 spin_lock_irq(&nvmeq->cq_lock);
Keith Busch22404272013-07-15 15:02:20 -06001516 nvmeq->sq_tail = 0;
1517 nvmeq->cq_head = 0;
1518 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001519 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001520 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Helen Koikef9f38e32017-04-10 12:51:07 -03001521 nvme_dbbuf_init(dev, nvmeq, qid);
Keith Busch42f61422014-03-24 10:46:25 -06001522 dev->online_queues++;
Jens Axboe1ab0cd62018-05-17 18:31:51 +02001523 spin_unlock_irq(&nvmeq->cq_lock);
Keith Busch22404272013-07-15 15:02:20 -06001524}
1525
Jens Axboe4b04cc62018-11-05 12:44:33 -07001526static int nvme_create_queue(struct nvme_queue *nvmeq, int qid, bool polled)
Keith Busch22404272013-07-15 15:02:20 -06001527{
1528 struct nvme_dev *dev = nvmeq->dev;
1529 int result;
Jianchao Wanga8e3e0b2018-05-24 17:51:33 +08001530 s16 vector;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001531
Keith Busch22b55602018-04-12 09:16:10 -06001532 /*
1533 * A queue's vector matches the queue identifier unless the controller
1534 * has only one vector available.
1535 */
Jens Axboe4b04cc62018-11-05 12:44:33 -07001536 if (!polled)
1537 vector = dev->num_vecs == 1 ? 0 : qid;
1538 else
1539 vector = -1;
1540
Jianchao Wanga8e3e0b2018-05-24 17:51:33 +08001541 result = adapter_alloc_cq(dev, qid, nvmeq, vector);
Keith Buschded45502018-06-06 08:13:06 -06001542 if (result)
1543 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001544
1545 result = adapter_alloc_sq(dev, qid, nvmeq);
1546 if (result < 0)
Keith Buschded45502018-06-06 08:13:06 -06001547 return result;
1548 else if (result)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001549 goto release_cq;
1550
Jianchao Wanga8e3e0b2018-05-24 17:51:33 +08001551 /*
1552 * Set cq_vector after alloc cq/sq, otherwise nvme_suspend_queue will
1553 * invoke free_irq for it and cause a 'Trying to free already-free IRQ
1554 * xxx' warning if the create CQ/SQ command times out.
1555 */
1556 nvmeq->cq_vector = vector;
Jens Axboe4b04cc62018-11-05 12:44:33 -07001557 nvmeq->polled = polled;
Keith Busch161b8be2017-09-14 13:54:39 -04001558 nvme_init_queue(nvmeq, qid);
Jens Axboe4b04cc62018-11-05 12:44:33 -07001559
1560 if (vector != -1) {
1561 result = queue_request_irq(nvmeq);
1562 if (result < 0)
1563 goto release_sq;
1564 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001565
Keith Busch22404272013-07-15 15:02:20 -06001566 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001567
Jianchao Wanga8e3e0b2018-05-24 17:51:33 +08001568release_sq:
1569 nvmeq->cq_vector = -1;
Jens Axboe4b04cc62018-11-05 12:44:33 -07001570 nvmeq->polled = false;
Jianchao Wangf25a2df2018-02-15 19:13:41 +08001571 dev->online_queues--;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001572 adapter_delete_sq(dev, qid);
Jianchao Wanga8e3e0b2018-05-24 17:51:33 +08001573release_cq:
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001574 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001575 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001576}
1577
Eric Biggersf363b082017-03-30 13:39:16 -07001578static const struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001579 .queue_rq = nvme_queue_rq,
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001580 .complete = nvme_pci_complete_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001581 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001582 .exit_hctx = nvme_admin_exit_hctx,
Christoph Hellwig03508152017-06-13 09:15:18 +02001583 .init_request = nvme_init_request,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001584 .timeout = nvme_timeout,
1585};
1586
Eric Biggersf363b082017-03-30 13:39:16 -07001587static const struct blk_mq_ops nvme_mq_ops = {
Jens Axboe3b6592f2018-10-31 08:36:31 -06001588 .queue_rq = nvme_queue_rq,
1589 .rq_flags_to_type = nvme_rq_flags_to_type,
1590 .complete = nvme_pci_complete_rq,
1591 .init_hctx = nvme_init_hctx,
1592 .init_request = nvme_init_request,
1593 .map_queues = nvme_pci_map_queues,
1594 .timeout = nvme_timeout,
1595 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001596};
1597
Keith Buschea191d22015-01-07 18:55:49 -07001598static void nvme_dev_remove_admin(struct nvme_dev *dev)
1599{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001600 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
Keith Busch69d9a992016-02-24 09:15:56 -07001601 /*
1602 * If the controller was reset during removal, it's possible
1603 * user requests may be waiting on a stopped queue. Start the
1604 * queue to flush these to completion.
1605 */
Sagi Grimbergc81545f2017-07-02 15:53:27 +03001606 blk_mq_unquiesce_queue(dev->ctrl.admin_q);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001607 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001608 blk_mq_free_tag_set(&dev->admin_tagset);
1609 }
1610}
1611
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001612static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1613{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001614 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001615 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1616 dev->admin_tagset.nr_hw_queues = 1;
Keith Busche3e9d502016-01-04 09:10:55 -07001617
Keith Busch38dabe22017-11-07 15:13:10 -07001618 dev->admin_tagset.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001619 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001620 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -07001621 dev->admin_tagset.cmd_size = nvme_pci_cmd_size(dev, false);
Jens Axboed3484992017-01-13 14:43:58 -07001622 dev->admin_tagset.flags = BLK_MQ_F_NO_SCHED;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001623 dev->admin_tagset.driver_data = dev;
1624
1625 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1626 return -ENOMEM;
Sagi Grimberg34b6c232017-07-10 09:22:29 +03001627 dev->ctrl.admin_tagset = &dev->admin_tagset;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001628
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001629 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1630 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001631 blk_mq_free_tag_set(&dev->admin_tagset);
1632 return -ENOMEM;
1633 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001634 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001635 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001636 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001637 return -ENODEV;
1638 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001639 } else
Sagi Grimbergc81545f2017-07-02 15:53:27 +03001640 blk_mq_unquiesce_queue(dev->ctrl.admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001641
1642 return 0;
1643}
1644
Xu Yu97f6ef62017-05-24 16:39:55 +08001645static unsigned long db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1646{
1647 return NVME_REG_DBS + ((nr_io_queues + 1) * 8 * dev->db_stride);
1648}
1649
1650static int nvme_remap_bar(struct nvme_dev *dev, unsigned long size)
1651{
1652 struct pci_dev *pdev = to_pci_dev(dev->dev);
1653
1654 if (size <= dev->bar_mapped_size)
1655 return 0;
1656 if (size > pci_resource_len(pdev, 0))
1657 return -ENOMEM;
1658 if (dev->bar)
1659 iounmap(dev->bar);
1660 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1661 if (!dev->bar) {
1662 dev->bar_mapped_size = 0;
1663 return -ENOMEM;
1664 }
1665 dev->bar_mapped_size = size;
1666 dev->dbs = dev->bar + NVME_REG_DBS;
1667
1668 return 0;
1669}
1670
Sagi Grimberg01ad0992017-05-01 00:27:17 +03001671static int nvme_pci_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001672{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001673 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001674 u32 aqa;
1675 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001676
Xu Yu97f6ef62017-05-24 16:39:55 +08001677 result = nvme_remap_bar(dev, db_bar_size(dev, 0));
1678 if (result < 0)
1679 return result;
1680
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001681 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1, 0) ?
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001682 NVME_CAP_NSSRC(dev->ctrl.cap) : 0;
Keith Buschdfbac8c2015-08-10 15:20:40 -06001683
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001684 if (dev->subsystem &&
1685 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1686 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001687
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001688 result = nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001689 if (result < 0)
1690 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001691
Keith Buscha6ff7262018-04-12 09:16:09 -06001692 result = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001693 if (result)
1694 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001695
Sagi Grimberg147b27e2018-01-14 12:39:01 +02001696 nvmeq = &dev->queues[0];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001697 aqa = nvmeq->q_depth - 1;
1698 aqa |= aqa << 16;
1699
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001700 writel(aqa, dev->bar + NVME_REG_AQA);
1701 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1702 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Keith Busch1d090622014-06-23 11:34:01 -06001703
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001704 result = nvme_enable_ctrl(&dev->ctrl, dev->ctrl.cap);
Keith Busch025c5572013-05-01 13:07:51 -06001705 if (result)
Keith Buschd4875622016-11-15 15:56:26 -05001706 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001707
Keith Busch2b25d982014-12-22 12:59:04 -07001708 nvmeq->cq_vector = 0;
Keith Busch161b8be2017-09-14 13:54:39 -04001709 nvme_init_queue(nvmeq, 0);
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001710 result = queue_request_irq(nvmeq);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001711 if (result) {
1712 nvmeq->cq_vector = -1;
Keith Buschd4875622016-11-15 15:56:26 -05001713 return result;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001714 }
Keith Busch025c5572013-05-01 13:07:51 -06001715
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001716 return result;
1717}
1718
Christoph Hellwig749941f2015-11-26 11:46:39 +01001719static int nvme_create_io_queues(struct nvme_dev *dev)
Keith Busch42f61422014-03-24 10:46:25 -06001720{
Jens Axboe4b04cc62018-11-05 12:44:33 -07001721 unsigned i, max, rw_queues;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001722 int ret = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001723
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001724 for (i = dev->ctrl.queue_count; i <= dev->max_qid; i++) {
Keith Buscha6ff7262018-04-12 09:16:09 -06001725 if (nvme_alloc_queue(dev, i, dev->q_depth)) {
Christoph Hellwig749941f2015-11-26 11:46:39 +01001726 ret = -ENOMEM;
Keith Busch42f61422014-03-24 10:46:25 -06001727 break;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001728 }
1729 }
Keith Busch42f61422014-03-24 10:46:25 -06001730
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001731 max = min(dev->max_qid, dev->ctrl.queue_count - 1);
Jens Axboe4b04cc62018-11-05 12:44:33 -07001732 if (max != 1 && dev->io_queues[NVMEQ_TYPE_POLL]) {
1733 rw_queues = dev->io_queues[NVMEQ_TYPE_READ] +
1734 dev->io_queues[NVMEQ_TYPE_WRITE];
1735 } else {
1736 rw_queues = max;
1737 }
1738
Keith Busch949928c2015-12-17 17:08:15 -07001739 for (i = dev->online_queues; i <= max; i++) {
Jens Axboe4b04cc62018-11-05 12:44:33 -07001740 bool polled = i > rw_queues;
1741
1742 ret = nvme_create_queue(&dev->queues[i], i, polled);
Keith Buschd4875622016-11-15 15:56:26 -05001743 if (ret)
Keith Busch42f61422014-03-24 10:46:25 -06001744 break;
Matthew Wilcox27e81662014-04-11 11:58:45 -04001745 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001746
1747 /*
1748 * Ignore failing Create SQ/CQ commands, we can continue with less
Minwoo Im8adb8c12018-01-14 16:14:27 +09001749 * than the desired amount of queues, and even a controller without
1750 * I/O queues can still be used to issue admin commands. This might
Christoph Hellwig749941f2015-11-26 11:46:39 +01001751 * be useful to upgrade a buggy firmware for example.
1752 */
1753 return ret >= 0 ? 0 : ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001754}
1755
Stephen Bates202021c2016-10-05 20:01:12 -06001756static ssize_t nvme_cmb_show(struct device *dev,
1757 struct device_attribute *attr,
1758 char *buf)
1759{
1760 struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
1761
Stephen Batesc9658092016-12-16 11:54:50 -07001762 return scnprintf(buf, PAGE_SIZE, "cmbloc : x%08x\ncmbsz : x%08x\n",
Stephen Bates202021c2016-10-05 20:01:12 -06001763 ndev->cmbloc, ndev->cmbsz);
1764}
1765static DEVICE_ATTR(cmb, S_IRUGO, nvme_cmb_show, NULL);
1766
Christoph Hellwig88de4592017-12-20 14:50:00 +01001767static u64 nvme_cmb_size_unit(struct nvme_dev *dev)
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001768{
Christoph Hellwig88de4592017-12-20 14:50:00 +01001769 u8 szu = (dev->cmbsz >> NVME_CMBSZ_SZU_SHIFT) & NVME_CMBSZ_SZU_MASK;
1770
1771 return 1ULL << (12 + 4 * szu);
1772}
1773
1774static u32 nvme_cmb_size(struct nvme_dev *dev)
1775{
1776 return (dev->cmbsz >> NVME_CMBSZ_SZ_SHIFT) & NVME_CMBSZ_SZ_MASK;
1777}
1778
Christoph Hellwigf65efd62017-12-20 14:25:11 +01001779static void nvme_map_cmb(struct nvme_dev *dev)
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001780{
Christoph Hellwig88de4592017-12-20 14:50:00 +01001781 u64 size, offset;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001782 resource_size_t bar_size;
1783 struct pci_dev *pdev = to_pci_dev(dev->dev);
Christoph Hellwig8969f1f2017-10-01 09:37:35 +02001784 int bar;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001785
Keith Busch9fe5c592018-10-31 13:15:29 -06001786 if (dev->cmb_size)
1787 return;
1788
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001789 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Christoph Hellwigf65efd62017-12-20 14:25:11 +01001790 if (!dev->cmbsz)
1791 return;
Stephen Bates202021c2016-10-05 20:01:12 -06001792 dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001793
Christoph Hellwig88de4592017-12-20 14:50:00 +01001794 size = nvme_cmb_size_unit(dev) * nvme_cmb_size(dev);
1795 offset = nvme_cmb_size_unit(dev) * NVME_CMB_OFST(dev->cmbloc);
Christoph Hellwig8969f1f2017-10-01 09:37:35 +02001796 bar = NVME_CMB_BIR(dev->cmbloc);
1797 bar_size = pci_resource_len(pdev, bar);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001798
1799 if (offset > bar_size)
Christoph Hellwigf65efd62017-12-20 14:25:11 +01001800 return;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001801
1802 /*
1803 * Controllers may support a CMB size larger than their BAR,
1804 * for example, due to being behind a bridge. Reduce the CMB to
1805 * the reported size of the BAR
1806 */
1807 if (size > bar_size - offset)
1808 size = bar_size - offset;
1809
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001810 if (pci_p2pdma_add_resource(pdev, bar, size, offset)) {
1811 dev_warn(dev->ctrl.device,
1812 "failed to register the CMB\n");
Christoph Hellwigf65efd62017-12-20 14:25:11 +01001813 return;
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001814 }
1815
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001816 dev->cmb_size = size;
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001817 dev->cmb_use_sqes = use_cmb_sqes && (dev->cmbsz & NVME_CMBSZ_SQS);
1818
1819 if ((dev->cmbsz & (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS)) ==
1820 (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS))
1821 pci_p2pmem_publish(pdev, true);
Christoph Hellwigf65efd62017-12-20 14:25:11 +01001822
1823 if (sysfs_add_file_to_group(&dev->ctrl.device->kobj,
1824 &dev_attr_cmb.attr, NULL))
1825 dev_warn(dev->ctrl.device,
1826 "failed to add sysfs attribute for CMB\n");
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001827}
1828
1829static inline void nvme_release_cmb(struct nvme_dev *dev)
1830{
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001831 if (dev->cmb_size) {
Max Gurtovoy1c78f772017-07-30 01:45:08 +03001832 sysfs_remove_file_from_group(&dev->ctrl.device->kobj,
1833 &dev_attr_cmb.attr, NULL);
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06001834 dev->cmb_size = 0;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001835 }
1836}
1837
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001838static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits)
Keith Busch9d713c22013-07-15 15:02:24 -06001839{
Christoph Hellwig4033f352017-08-28 10:47:18 +02001840 u64 dma_addr = dev->host_mem_descs_dma;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001841 struct nvme_command c;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001842 int ret;
1843
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001844 memset(&c, 0, sizeof(c));
1845 c.features.opcode = nvme_admin_set_features;
1846 c.features.fid = cpu_to_le32(NVME_FEAT_HOST_MEM_BUF);
1847 c.features.dword11 = cpu_to_le32(bits);
1848 c.features.dword12 = cpu_to_le32(dev->host_mem_size >>
1849 ilog2(dev->ctrl.page_size));
1850 c.features.dword13 = cpu_to_le32(lower_32_bits(dma_addr));
1851 c.features.dword14 = cpu_to_le32(upper_32_bits(dma_addr));
1852 c.features.dword15 = cpu_to_le32(dev->nr_host_mem_descs);
1853
1854 ret = nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1855 if (ret) {
1856 dev_warn(dev->ctrl.device,
1857 "failed to set host mem (err %d, flags %#x).\n",
1858 ret, bits);
1859 }
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001860 return ret;
1861}
1862
1863static void nvme_free_host_mem(struct nvme_dev *dev)
1864{
1865 int i;
1866
1867 for (i = 0; i < dev->nr_host_mem_descs; i++) {
1868 struct nvme_host_mem_buf_desc *desc = &dev->host_mem_descs[i];
1869 size_t size = le32_to_cpu(desc->size) * dev->ctrl.page_size;
1870
1871 dma_free_coherent(dev->dev, size, dev->host_mem_desc_bufs[i],
1872 le64_to_cpu(desc->addr));
1873 }
1874
1875 kfree(dev->host_mem_desc_bufs);
1876 dev->host_mem_desc_bufs = NULL;
Christoph Hellwig4033f352017-08-28 10:47:18 +02001877 dma_free_coherent(dev->dev,
1878 dev->nr_host_mem_descs * sizeof(*dev->host_mem_descs),
1879 dev->host_mem_descs, dev->host_mem_descs_dma);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001880 dev->host_mem_descs = NULL;
Minwoo Im7e5dd572017-11-25 03:03:00 +09001881 dev->nr_host_mem_descs = 0;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001882}
1883
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001884static int __nvme_alloc_host_mem(struct nvme_dev *dev, u64 preferred,
1885 u32 chunk_size)
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001886{
1887 struct nvme_host_mem_buf_desc *descs;
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001888 u32 max_entries, len;
Christoph Hellwig4033f352017-08-28 10:47:18 +02001889 dma_addr_t descs_dma;
Dan Carpenter2ee0e4e2017-07-06 12:26:52 +03001890 int i = 0;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001891 void **bufs;
Minwoo Im6fbcde62017-12-05 05:23:54 +09001892 u64 size, tmp;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001893
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001894 tmp = (preferred + chunk_size - 1);
1895 do_div(tmp, chunk_size);
1896 max_entries = tmp;
Christoph Hellwig044a9df2017-09-11 12:09:28 -04001897
1898 if (dev->ctrl.hmmaxd && dev->ctrl.hmmaxd < max_entries)
1899 max_entries = dev->ctrl.hmmaxd;
1900
Christoph Hellwig4033f352017-08-28 10:47:18 +02001901 descs = dma_zalloc_coherent(dev->dev, max_entries * sizeof(*descs),
1902 &descs_dma, GFP_KERNEL);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001903 if (!descs)
1904 goto out;
1905
1906 bufs = kcalloc(max_entries, sizeof(*bufs), GFP_KERNEL);
1907 if (!bufs)
1908 goto out_free_descs;
1909
Minwoo Im244a8fe2017-11-17 01:34:24 +09001910 for (size = 0; size < preferred && i < max_entries; size += len) {
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001911 dma_addr_t dma_addr;
1912
Christoph Hellwig50cdb7c2017-07-25 17:39:07 +02001913 len = min_t(u64, chunk_size, preferred - size);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001914 bufs[i] = dma_alloc_attrs(dev->dev, len, &dma_addr, GFP_KERNEL,
1915 DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
1916 if (!bufs[i])
1917 break;
1918
1919 descs[i].addr = cpu_to_le64(dma_addr);
1920 descs[i].size = cpu_to_le32(len / dev->ctrl.page_size);
1921 i++;
1922 }
1923
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001924 if (!size)
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001925 goto out_free_bufs;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001926
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001927 dev->nr_host_mem_descs = i;
1928 dev->host_mem_size = size;
1929 dev->host_mem_descs = descs;
Christoph Hellwig4033f352017-08-28 10:47:18 +02001930 dev->host_mem_descs_dma = descs_dma;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001931 dev->host_mem_desc_bufs = bufs;
1932 return 0;
1933
1934out_free_bufs:
1935 while (--i >= 0) {
1936 size_t size = le32_to_cpu(descs[i].size) * dev->ctrl.page_size;
1937
1938 dma_free_coherent(dev->dev, size, bufs[i],
1939 le64_to_cpu(descs[i].addr));
1940 }
1941
1942 kfree(bufs);
1943out_free_descs:
Christoph Hellwig4033f352017-08-28 10:47:18 +02001944 dma_free_coherent(dev->dev, max_entries * sizeof(*descs), descs,
1945 descs_dma);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001946out:
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001947 dev->host_mem_descs = NULL;
1948 return -ENOMEM;
1949}
1950
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001951static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
1952{
1953 u32 chunk_size;
1954
1955 /* start big and work our way down */
Akinobu Mita30f92d62017-09-06 12:15:31 +02001956 for (chunk_size = min_t(u64, preferred, PAGE_SIZE * MAX_ORDER_NR_PAGES);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04001957 chunk_size >= max_t(u32, dev->ctrl.hmminds * 4096, PAGE_SIZE * 2);
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001958 chunk_size /= 2) {
1959 if (!__nvme_alloc_host_mem(dev, preferred, chunk_size)) {
1960 if (!min || dev->host_mem_size >= min)
1961 return 0;
1962 nvme_free_host_mem(dev);
1963 }
1964 }
1965
1966 return -ENOMEM;
1967}
1968
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02001969static int nvme_setup_host_mem(struct nvme_dev *dev)
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001970{
1971 u64 max = (u64)max_host_mem_size_mb * SZ_1M;
1972 u64 preferred = (u64)dev->ctrl.hmpre * 4096;
1973 u64 min = (u64)dev->ctrl.hmmin * 4096;
1974 u32 enable_bits = NVME_HOST_MEM_ENABLE;
Minwoo Im6fbcde62017-12-05 05:23:54 +09001975 int ret;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001976
1977 preferred = min(preferred, max);
1978 if (min > max) {
1979 dev_warn(dev->ctrl.device,
1980 "min host memory (%lld MiB) above limit (%d MiB).\n",
1981 min >> ilog2(SZ_1M), max_host_mem_size_mb);
1982 nvme_free_host_mem(dev);
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02001983 return 0;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001984 }
1985
1986 /*
1987 * If we already have a buffer allocated check if we can reuse it.
1988 */
1989 if (dev->host_mem_descs) {
1990 if (dev->host_mem_size >= min)
1991 enable_bits |= NVME_HOST_MEM_RETURN;
1992 else
1993 nvme_free_host_mem(dev);
1994 }
1995
1996 if (!dev->host_mem_descs) {
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001997 if (nvme_alloc_host_mem(dev, min, preferred)) {
1998 dev_warn(dev->ctrl.device,
1999 "failed to allocate host memory buffer.\n");
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02002000 return 0; /* controller must work without HMB */
Christoph Hellwig92dc6892017-09-11 12:08:43 -04002001 }
2002
2003 dev_info(dev->ctrl.device,
2004 "allocated %lld MiB host memory buffer.\n",
2005 dev->host_mem_size >> ilog2(SZ_1M));
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002006 }
2007
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02002008 ret = nvme_set_host_mem(dev, enable_bits);
2009 if (ret)
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002010 nvme_free_host_mem(dev);
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02002011 return ret;
Keith Busch9d713c22013-07-15 15:02:24 -06002012}
2013
Jens Axboe3b6592f2018-10-31 08:36:31 -06002014static void nvme_calc_io_queues(struct nvme_dev *dev, unsigned int nr_io_queues)
2015{
2016 unsigned int this_w_queues = write_queues;
Jens Axboe4b04cc62018-11-05 12:44:33 -07002017 unsigned int this_p_queues = poll_queues;
Jens Axboe3b6592f2018-10-31 08:36:31 -06002018
2019 /*
2020 * Setup read/write queue split
2021 */
2022 if (nr_io_queues == 1) {
2023 dev->io_queues[NVMEQ_TYPE_READ] = 1;
2024 dev->io_queues[NVMEQ_TYPE_WRITE] = 0;
Jens Axboe4b04cc62018-11-05 12:44:33 -07002025 dev->io_queues[NVMEQ_TYPE_POLL] = 0;
Jens Axboe3b6592f2018-10-31 08:36:31 -06002026 return;
2027 }
2028
2029 /*
Jens Axboe4b04cc62018-11-05 12:44:33 -07002030 * Configure number of poll queues, if set
2031 */
2032 if (this_p_queues) {
2033 /*
2034 * We need at least one queue left. With just one queue, we'll
2035 * have a single shared read/write set.
2036 */
2037 if (this_p_queues >= nr_io_queues) {
2038 this_w_queues = 0;
2039 this_p_queues = nr_io_queues - 1;
2040 }
2041
2042 dev->io_queues[NVMEQ_TYPE_POLL] = this_p_queues;
2043 nr_io_queues -= this_p_queues;
2044 } else
2045 dev->io_queues[NVMEQ_TYPE_POLL] = 0;
2046
2047 /*
Jens Axboe3b6592f2018-10-31 08:36:31 -06002048 * If 'write_queues' is set, ensure it leaves room for at least
2049 * one read queue
2050 */
2051 if (this_w_queues >= nr_io_queues)
2052 this_w_queues = nr_io_queues - 1;
2053
2054 /*
2055 * If 'write_queues' is set to zero, reads and writes will share
2056 * a queue set.
2057 */
2058 if (!this_w_queues) {
2059 dev->io_queues[NVMEQ_TYPE_WRITE] = 0;
2060 dev->io_queues[NVMEQ_TYPE_READ] = nr_io_queues;
2061 } else {
2062 dev->io_queues[NVMEQ_TYPE_WRITE] = this_w_queues;
2063 dev->io_queues[NVMEQ_TYPE_READ] = nr_io_queues - this_w_queues;
2064 }
2065}
2066
2067static int nvme_setup_irqs(struct nvme_dev *dev, int nr_io_queues)
2068{
2069 struct pci_dev *pdev = to_pci_dev(dev->dev);
2070 int irq_sets[2];
2071 struct irq_affinity affd = {
2072 .pre_vectors = 1,
2073 .nr_sets = ARRAY_SIZE(irq_sets),
2074 .sets = irq_sets,
2075 };
Jens Axboe30e06622018-11-14 10:13:50 -07002076 int result = 0;
Jens Axboe3b6592f2018-10-31 08:36:31 -06002077
2078 /*
2079 * For irq sets, we have to ask for minvec == maxvec. This passes
2080 * any reduction back to us, so we can adjust our queue counts and
2081 * IRQ vector needs.
2082 */
2083 do {
2084 nvme_calc_io_queues(dev, nr_io_queues);
2085 irq_sets[0] = dev->io_queues[NVMEQ_TYPE_READ];
2086 irq_sets[1] = dev->io_queues[NVMEQ_TYPE_WRITE];
2087 if (!irq_sets[1])
2088 affd.nr_sets = 1;
2089
2090 /*
Jens Axboedb29eb02018-11-15 16:05:02 -07002091 * If we got a failure and we're down to asking for just
2092 * 1 + 1 queues, just ask for a single vector. We'll share
2093 * that between the single IO queue and the admin queue.
Jens Axboe3b6592f2018-10-31 08:36:31 -06002094 */
Jens Axboedb29eb02018-11-15 16:05:02 -07002095 if (!(result < 0 && nr_io_queues == 1))
Jens Axboe30e06622018-11-14 10:13:50 -07002096 nr_io_queues = irq_sets[0] + irq_sets[1] + 1;
Jens Axboe3b6592f2018-10-31 08:36:31 -06002097
2098 result = pci_alloc_irq_vectors_affinity(pdev, nr_io_queues,
2099 nr_io_queues,
2100 PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY, &affd);
2101
2102 /*
Jens Axboedb29eb02018-11-15 16:05:02 -07002103 * Need to reduce our vec counts. If we get ENOSPC, the
2104 * platform should support mulitple vecs, we just need
2105 * to decrease our ask. If we get EINVAL, the platform
2106 * likely does not. Back down to ask for just one vector.
Jens Axboe3b6592f2018-10-31 08:36:31 -06002107 */
2108 if (result == -ENOSPC) {
2109 nr_io_queues--;
2110 if (!nr_io_queues)
2111 return result;
2112 continue;
Jens Axboedb29eb02018-11-15 16:05:02 -07002113 } else if (result == -EINVAL) {
2114 nr_io_queues = 1;
2115 continue;
Jens Axboe3b6592f2018-10-31 08:36:31 -06002116 } else if (result <= 0)
2117 return -EIO;
2118 break;
2119 } while (1);
2120
2121 return result;
2122}
2123
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002124static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002125{
Sagi Grimberg147b27e2018-01-14 12:39:01 +02002126 struct nvme_queue *adminq = &dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02002127 struct pci_dev *pdev = to_pci_dev(dev->dev);
Xu Yu97f6ef62017-05-24 16:39:55 +08002128 int result, nr_io_queues;
2129 unsigned long size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002130
Jens Axboe3b6592f2018-10-31 08:36:31 -06002131 nr_io_queues = max_io_queues();
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01002132 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
2133 if (result < 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05002134 return result;
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01002135
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02002136 if (nr_io_queues == 0)
Keith Buscha5229052016-04-08 16:09:10 -06002137 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002138
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06002139 if (dev->cmb_use_sqes) {
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002140 result = nvme_cmb_qdepth(dev, nr_io_queues,
2141 sizeof(struct nvme_command));
2142 if (result > 0)
2143 dev->q_depth = result;
2144 else
Logan Gunthorpe0f238ff2018-10-04 15:27:43 -06002145 dev->cmb_use_sqes = false;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06002146 }
2147
Xu Yu97f6ef62017-05-24 16:39:55 +08002148 do {
2149 size = db_bar_size(dev, nr_io_queues);
2150 result = nvme_remap_bar(dev, size);
2151 if (!result)
2152 break;
2153 if (!--nr_io_queues)
2154 return -ENOMEM;
2155 } while (1);
2156 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04002157
Keith Busch9d713c22013-07-15 15:02:24 -06002158 /* Deregister the admin queue's interrupt */
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02002159 pci_free_irq(pdev, 0, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06002160
Jens Axboee32efbf2014-11-14 09:49:26 -07002161 /*
2162 * If we enable msix early due to not intx, disable it again before
2163 * setting up the full range we need.
2164 */
Christoph Hellwigdca51e72016-09-14 16:18:57 +02002165 pci_free_irq_vectors(pdev);
Jens Axboe3b6592f2018-10-31 08:36:31 -06002166
2167 result = nvme_setup_irqs(dev, nr_io_queues);
Keith Busch22b55602018-04-12 09:16:10 -06002168 if (result <= 0)
Christoph Hellwigdca51e72016-09-14 16:18:57 +02002169 return -EIO;
Jens Axboe3b6592f2018-10-31 08:36:31 -06002170
Keith Busch22b55602018-04-12 09:16:10 -06002171 dev->num_vecs = result;
Jens Axboe4b04cc62018-11-05 12:44:33 -07002172 result = max(result - 1, 1);
2173 dev->max_qid = result + dev->io_queues[NVMEQ_TYPE_POLL];
Matthew Wilcox1b234842011-01-20 13:01:49 -05002174
Jens Axboe4b04cc62018-11-05 12:44:33 -07002175 dev_info(dev->ctrl.device, "%d/%d/%d read/write/poll queues\n",
Jens Axboe3b6592f2018-10-31 08:36:31 -06002176 dev->io_queues[NVMEQ_TYPE_READ],
Jens Axboe4b04cc62018-11-05 12:44:33 -07002177 dev->io_queues[NVMEQ_TYPE_WRITE],
2178 dev->io_queues[NVMEQ_TYPE_POLL]);
Jens Axboe3b6592f2018-10-31 08:36:31 -06002179
Matthew Wilcox063a8092013-06-20 10:53:48 -04002180 /*
2181 * Should investigate if there's a performance win from allocating
2182 * more queues than interrupt vectors; it might allow the submission
2183 * path to scale better, even if the receive path is limited by the
2184 * number of interrupts.
2185 */
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07002186
Christoph Hellwigdca51e72016-09-14 16:18:57 +02002187 result = queue_request_irq(adminq);
Jon Derrick758dd7f2015-06-30 11:22:52 -06002188 if (result) {
2189 adminq->cq_vector = -1;
Keith Buschd4875622016-11-15 15:56:26 -05002190 return result;
Jon Derrick758dd7f2015-06-30 11:22:52 -06002191 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01002192 return nvme_create_io_queues(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002193}
2194
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002195static void nvme_del_queue_end(struct request *req, blk_status_t error)
Keith Buschdb3cbff2016-01-12 14:41:17 -07002196{
2197 struct nvme_queue *nvmeq = req->end_io_data;
2198
2199 blk_mq_free_request(req);
2200 complete(&nvmeq->dev->ioq_wait);
2201}
2202
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002203static void nvme_del_cq_end(struct request *req, blk_status_t error)
Keith Buschdb3cbff2016-01-12 14:41:17 -07002204{
2205 struct nvme_queue *nvmeq = req->end_io_data;
Jens Axboe5cb525c2018-05-17 18:31:50 +02002206 u16 start, end;
Keith Buschdb3cbff2016-01-12 14:41:17 -07002207
2208 if (!error) {
2209 unsigned long flags;
2210
Keith Busch0bc88192018-06-06 08:13:04 -06002211 spin_lock_irqsave(&nvmeq->cq_lock, flags);
Jens Axboe5cb525c2018-05-17 18:31:50 +02002212 nvme_process_cq(nvmeq, &start, &end, -1);
Jens Axboe1ab0cd62018-05-17 18:31:51 +02002213 spin_unlock_irqrestore(&nvmeq->cq_lock, flags);
Jens Axboe5cb525c2018-05-17 18:31:50 +02002214
2215 nvme_complete_cqes(nvmeq, start, end);
Keith Buschdb3cbff2016-01-12 14:41:17 -07002216 }
2217
2218 nvme_del_queue_end(req, error);
2219}
2220
2221static int nvme_delete_queue(struct nvme_queue *nvmeq, u8 opcode)
2222{
2223 struct request_queue *q = nvmeq->dev->ctrl.admin_q;
2224 struct request *req;
2225 struct nvme_command cmd;
2226
2227 memset(&cmd, 0, sizeof(cmd));
2228 cmd.delete_queue.opcode = opcode;
2229 cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2230
Christoph Hellwigeb71f432016-06-13 16:45:23 +02002231 req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
Keith Buschdb3cbff2016-01-12 14:41:17 -07002232 if (IS_ERR(req))
2233 return PTR_ERR(req);
2234
2235 req->timeout = ADMIN_TIMEOUT;
2236 req->end_io_data = nvmeq;
2237
2238 blk_execute_rq_nowait(q, NULL, req, false,
2239 opcode == nvme_admin_delete_cq ?
2240 nvme_del_cq_end : nvme_del_queue_end);
2241 return 0;
2242}
2243
Keith Buschee9aebb2018-01-24 14:55:12 -07002244static void nvme_disable_io_queues(struct nvme_dev *dev)
Keith Buschdb3cbff2016-01-12 14:41:17 -07002245{
Keith Buschee9aebb2018-01-24 14:55:12 -07002246 int pass, queues = dev->online_queues - 1;
Keith Buschdb3cbff2016-01-12 14:41:17 -07002247 unsigned long timeout;
2248 u8 opcode = nvme_admin_delete_sq;
2249
2250 for (pass = 0; pass < 2; pass++) {
Keith Busch014a0d62016-05-06 11:50:52 -06002251 int sent = 0, i = queues;
Keith Buschdb3cbff2016-01-12 14:41:17 -07002252
2253 reinit_completion(&dev->ioq_wait);
2254 retry:
2255 timeout = ADMIN_TIMEOUT;
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06002256 for (; i > 0; i--, sent++)
Sagi Grimberg147b27e2018-01-14 12:39:01 +02002257 if (nvme_delete_queue(&dev->queues[i], opcode))
Keith Buschdb3cbff2016-01-12 14:41:17 -07002258 break;
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06002259
Keith Buschdb3cbff2016-01-12 14:41:17 -07002260 while (sent--) {
2261 timeout = wait_for_completion_io_timeout(&dev->ioq_wait, timeout);
2262 if (timeout == 0)
2263 return;
2264 if (i)
2265 goto retry;
2266 }
2267 opcode = nvme_admin_delete_cq;
2268 }
2269}
2270
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002271/*
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002272 * return error value only when tagset allocation failed
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04002273 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002274static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002275{
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002276 int ret;
2277
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002278 if (!dev->ctrl.tagset) {
Keith Buschffe77042015-06-08 10:08:15 -06002279 dev->tagset.ops = &nvme_mq_ops;
2280 dev->tagset.nr_hw_queues = dev->online_queues - 1;
Jens Axboe3b6592f2018-10-31 08:36:31 -06002281 dev->tagset.nr_maps = NVMEQ_TYPE_NR;
Keith Buschffe77042015-06-08 10:08:15 -06002282 dev->tagset.timeout = NVME_IO_TIMEOUT;
2283 dev->tagset.numa_node = dev_to_node(dev->dev);
2284 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002285 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Chaitanya Kulkarnia7a7cbe2017-10-16 18:24:20 -07002286 dev->tagset.cmd_size = nvme_pci_cmd_size(dev, false);
2287 if ((dev->ctrl.sgls & ((1 << 0) | (1 << 1))) && sgl_threshold) {
2288 dev->tagset.cmd_size = max(dev->tagset.cmd_size,
2289 nvme_pci_cmd_size(dev, true));
2290 }
Keith Buschffe77042015-06-08 10:08:15 -06002291 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2292 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002293
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002294 ret = blk_mq_alloc_tag_set(&dev->tagset);
2295 if (ret) {
2296 dev_warn(dev->ctrl.device,
2297 "IO queues tagset allocation failed %d\n", ret);
2298 return ret;
2299 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002300 dev->ctrl.tagset = &dev->tagset;
Helen Koikef9f38e32017-04-10 12:51:07 -03002301
2302 nvme_dbbuf_set(dev);
Keith Busch949928c2015-12-17 17:08:15 -07002303 } else {
2304 blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1);
2305
2306 /* Free previously allocated queues that are no longer usable */
2307 nvme_free_queues(dev, dev->online_queues);
Keith Buschffe77042015-06-08 10:08:15 -06002308 }
Keith Busch949928c2015-12-17 17:08:15 -07002309
Keith Busche1e5e562015-02-19 13:39:03 -07002310 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002311}
2312
Keith Buschb00a7262016-02-24 09:15:52 -07002313static int nvme_pci_enable(struct nvme_dev *dev)
Keith Busch0877cb02013-07-15 15:02:19 -06002314{
Keith Buschb00a7262016-02-24 09:15:52 -07002315 int result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02002316 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06002317
2318 if (pci_enable_device_mem(pdev))
2319 return result;
2320
Keith Busch0877cb02013-07-15 15:02:19 -06002321 pci_set_master(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002322
Christoph Hellwige75ec752015-05-22 11:12:39 +02002323 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
2324 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01002325 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06002326
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002327 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07002328 result = -ENODEV;
Keith Buschb00a7262016-02-24 09:15:52 -07002329 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07002330 }
Jens Axboee32efbf2014-11-14 09:49:26 -07002331
2332 /*
Keith Buscha5229052016-04-08 16:09:10 -06002333 * Some devices and/or platforms don't advertise or work with INTx
2334 * interrupts. Pre-enable a single MSIX or MSI vec for setup. We'll
2335 * adjust this later.
Jens Axboee32efbf2014-11-14 09:49:26 -07002336 */
Christoph Hellwigdca51e72016-09-14 16:18:57 +02002337 result = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
2338 if (result < 0)
2339 return result;
Jens Axboee32efbf2014-11-14 09:49:26 -07002340
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002341 dev->ctrl.cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002342
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002343 dev->q_depth = min_t(int, NVME_CAP_MQES(dev->ctrl.cap) + 1,
weiping zhangb27c1e62017-07-10 16:46:59 +08002344 io_queue_depth);
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002345 dev->db_stride = 1 << NVME_CAP_STRIDE(dev->ctrl.cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01002346 dev->dbs = dev->bar + 4096;
Stephan Günther1f390c12015-12-01 13:23:22 -07002347
2348 /*
2349 * Temporary fix for the Apple controller found in the MacBook8,1 and
2350 * some MacBook7,1 to avoid controller resets and data loss.
2351 */
2352 if (pdev->vendor == PCI_VENDOR_ID_APPLE && pdev->device == 0x2001) {
2353 dev->q_depth = 2;
Christoph Hellwig9bdcfb12017-05-20 15:14:43 +02002354 dev_warn(dev->ctrl.device, "detected Apple NVMe controller, "
2355 "set queue depth=%u to work around controller resets\n",
Stephan Günther1f390c12015-12-01 13:23:22 -07002356 dev->q_depth);
Martin K. Petersend554b5e2017-06-27 22:27:57 -04002357 } else if (pdev->vendor == PCI_VENDOR_ID_SAMSUNG &&
2358 (pdev->device == 0xa821 || pdev->device == 0xa822) &&
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03002359 NVME_CAP_MQES(dev->ctrl.cap) == 0) {
Martin K. Petersend554b5e2017-06-27 22:27:57 -04002360 dev->q_depth = 64;
2361 dev_err(dev->ctrl.device, "detected PM1725 NVMe controller, "
2362 "set queue depth=%u\n", dev->q_depth);
Stephan Günther1f390c12015-12-01 13:23:22 -07002363 }
2364
Christoph Hellwigf65efd62017-12-20 14:25:11 +01002365 nvme_map_cmb(dev);
Stephen Bates202021c2016-10-05 20:01:12 -06002366
Keith Buscha0a34082015-12-07 15:30:31 -07002367 pci_enable_pcie_error_reporting(pdev);
2368 pci_save_state(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002369 return 0;
2370
2371 disable:
Keith Busch0877cb02013-07-15 15:02:19 -06002372 pci_disable_device(pdev);
2373 return result;
2374}
2375
2376static void nvme_dev_unmap(struct nvme_dev *dev)
2377{
Keith Buschb00a7262016-02-24 09:15:52 -07002378 if (dev->bar)
2379 iounmap(dev->bar);
Johannes Thumshirna1f447b2016-06-07 09:44:02 +02002380 pci_release_mem_regions(to_pci_dev(dev->dev));
Keith Buschb00a7262016-02-24 09:15:52 -07002381}
2382
2383static void nvme_pci_disable(struct nvme_dev *dev)
2384{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002385 struct pci_dev *pdev = to_pci_dev(dev->dev);
2386
Christoph Hellwigdca51e72016-09-14 16:18:57 +02002387 pci_free_irq_vectors(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002388
Keith Buscha0a34082015-12-07 15:30:31 -07002389 if (pci_is_enabled(pdev)) {
2390 pci_disable_pcie_error_reporting(pdev);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002391 pci_disable_device(pdev);
Keith Busch4d115422013-12-10 13:10:40 -07002392 }
Keith Busch4d115422013-12-10 13:10:40 -07002393}
2394
Keith Buscha5cdb682016-01-12 14:41:18 -07002395static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002396{
Keith Buschee9aebb2018-01-24 14:55:12 -07002397 int i;
Keith Busch302ad8c2017-03-01 14:22:12 -05002398 bool dead = true;
2399 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch22404272013-07-15 15:02:20 -06002400
Keith Busch77bf25e2015-11-26 12:21:29 +01002401 mutex_lock(&dev->shutdown_lock);
Keith Busch302ad8c2017-03-01 14:22:12 -05002402 if (pci_is_enabled(pdev)) {
2403 u32 csts = readl(dev->bar + NVME_REG_CSTS);
2404
Keith Buschebef7362017-06-27 17:44:05 -06002405 if (dev->ctrl.state == NVME_CTRL_LIVE ||
2406 dev->ctrl.state == NVME_CTRL_RESETTING)
Keith Busch302ad8c2017-03-01 14:22:12 -05002407 nvme_start_freeze(&dev->ctrl);
2408 dead = !!((csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) ||
2409 pdev->error_state != pci_channel_io_normal);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002410 }
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06002411
Keith Busch302ad8c2017-03-01 14:22:12 -05002412 /*
2413 * Give the controller a chance to complete all entered requests if
2414 * doing a safe shutdown.
2415 */
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002416 if (!dead) {
2417 if (shutdown)
2418 nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
Jianchao Wang9a915a52018-02-12 20:57:24 +08002419 }
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002420
Jianchao Wang9a915a52018-02-12 20:57:24 +08002421 nvme_stop_queues(&dev->ctrl);
2422
Keith Busch64ee0ac2018-04-12 09:16:08 -06002423 if (!dead && dev->ctrl.queue_count > 0) {
Keith Buschee9aebb2018-01-24 14:55:12 -07002424 nvme_disable_io_queues(dev);
Keith Buscha5cdb682016-01-12 14:41:18 -07002425 nvme_disable_admin_queue(dev, shutdown);
Keith Busch4d115422013-12-10 13:10:40 -07002426 }
Keith Buschee9aebb2018-01-24 14:55:12 -07002427 for (i = dev->ctrl.queue_count - 1; i >= 0; i--)
2428 nvme_suspend_queue(&dev->queues[i]);
2429
Keith Buschb00a7262016-02-24 09:15:52 -07002430 nvme_pci_disable(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002431
Ming Line1958e62016-05-18 14:05:01 -07002432 blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl);
2433 blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl);
Keith Busch302ad8c2017-03-01 14:22:12 -05002434
2435 /*
2436 * The driver will not be starting up queues again if shutting down so
2437 * must flush all entered requests to their failed completion to avoid
2438 * deadlocking blk-mq hot-cpu notifier.
2439 */
2440 if (shutdown)
2441 nvme_start_queues(&dev->ctrl);
Keith Busch77bf25e2015-11-26 12:21:29 +01002442 mutex_unlock(&dev->shutdown_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002443}
2444
Matthew Wilcox091b6092011-02-10 09:56:01 -05002445static int nvme_setup_prp_pools(struct nvme_dev *dev)
2446{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002447 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002448 PAGE_SIZE, PAGE_SIZE, 0);
2449 if (!dev->prp_page_pool)
2450 return -ENOMEM;
2451
Matthew Wilcox99802a72011-02-10 10:30:34 -05002452 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002453 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002454 256, 256, 0);
2455 if (!dev->prp_small_pool) {
2456 dma_pool_destroy(dev->prp_page_pool);
2457 return -ENOMEM;
2458 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002459 return 0;
2460}
2461
2462static void nvme_release_prp_pools(struct nvme_dev *dev)
2463{
2464 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002465 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002466}
2467
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002468static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002469{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002470 struct nvme_dev *dev = to_nvme_dev(ctrl);
Keith Busch9ac27092014-01-31 16:53:39 -07002471
Helen Koikef9f38e32017-04-10 12:51:07 -03002472 nvme_dbbuf_dma_free(dev);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002473 put_device(dev->dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002474 if (dev->tagset.tags)
2475 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002476 if (dev->ctrl.admin_q)
2477 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002478 kfree(dev->queues);
Scott Bauere286bcf2017-02-22 10:15:07 -07002479 free_opal_dev(dev->ctrl.opal_dev);
Jens Axboe943e9422018-06-21 09:49:37 -06002480 mempool_destroy(dev->iod_mempool);
Keith Busch5e82e952013-02-19 10:17:58 -07002481 kfree(dev);
2482}
2483
Keith Buschf58944e2016-02-24 09:15:55 -07002484static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
2485{
Linus Torvalds237045f2016-03-18 17:13:31 -07002486 dev_warn(dev->ctrl.device, "Removing after probe failure status: %d\n", status);
Keith Buschf58944e2016-02-24 09:15:55 -07002487
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002488 nvme_get_ctrl(&dev->ctrl);
Keith Busch69d9a992016-02-24 09:15:56 -07002489 nvme_dev_disable(dev, false);
Jianchao Wang9f9cafc2018-06-20 13:42:22 +08002490 nvme_kill_queues(&dev->ctrl);
Ming Lei03e0f3a2017-11-09 19:32:07 +08002491 if (!queue_work(nvme_wq, &dev->remove_work))
Keith Buschf58944e2016-02-24 09:15:55 -07002492 nvme_put_ctrl(&dev->ctrl);
2493}
2494
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002495static void nvme_reset_work(struct work_struct *work)
Keith Busch5e82e952013-02-19 10:17:58 -07002496{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002497 struct nvme_dev *dev =
2498 container_of(work, struct nvme_dev, ctrl.reset_work);
Scott Bauera98e58e52017-02-03 12:50:32 -07002499 bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
Keith Buschf58944e2016-02-24 09:15:55 -07002500 int result = -ENODEV;
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002501 enum nvme_ctrl_state new_state = NVME_CTRL_LIVE;
Keith Buschf0b50732013-07-15 15:02:21 -06002502
Rakesh Pandit82b057c2017-06-05 14:43:11 +03002503 if (WARN_ON(dev->ctrl.state != NVME_CTRL_RESETTING))
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002504 goto out;
2505
2506 /*
2507 * If we're called to reset a live controller first shut it down before
2508 * moving on.
2509 */
Keith Buschb00a7262016-02-24 09:15:52 -07002510 if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
Keith Buscha5cdb682016-01-12 14:41:18 -07002511 nvme_dev_disable(dev, false);
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002512
Jianchao Wangad700622018-01-22 22:03:16 +08002513 /*
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02002514 * Introduce CONNECTING state from nvme-fc/rdma transports to mark the
Jianchao Wangad700622018-01-22 22:03:16 +08002515 * initializing procedure here.
2516 */
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02002517 if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_CONNECTING)) {
Jianchao Wangad700622018-01-22 22:03:16 +08002518 dev_warn(dev->ctrl.device,
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02002519 "failed to mark controller CONNECTING\n");
Jianchao Wangad700622018-01-22 22:03:16 +08002520 goto out;
2521 }
2522
Keith Buschb00a7262016-02-24 09:15:52 -07002523 result = nvme_pci_enable(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002524 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002525 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002526
Sagi Grimberg01ad0992017-05-01 00:27:17 +03002527 result = nvme_pci_configure_admin_queue(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002528 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002529 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002530
Keith Busch0fb59cb2015-01-07 18:55:50 -07002531 result = nvme_alloc_admin_tags(dev);
2532 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002533 goto out;
Dan McLeranb9afca32014-04-07 17:10:11 -06002534
Jens Axboe943e9422018-06-21 09:49:37 -06002535 /*
2536 * Limit the max command size to prevent iod->sg allocations going
2537 * over a single page.
2538 */
2539 dev->ctrl.max_hw_sectors = NVME_MAX_KB_SZ << 1;
2540 dev->ctrl.max_segments = NVME_MAX_SEGS;
2541
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002542 result = nvme_init_identify(&dev->ctrl);
2543 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002544 goto out;
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002545
Scott Bauere286bcf2017-02-22 10:15:07 -07002546 if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) {
2547 if (!dev->ctrl.opal_dev)
2548 dev->ctrl.opal_dev =
2549 init_opal_dev(&dev->ctrl, &nvme_sec_submit);
2550 else if (was_suspend)
2551 opal_unlock_from_suspend(dev->ctrl.opal_dev);
2552 } else {
2553 free_opal_dev(dev->ctrl.opal_dev);
2554 dev->ctrl.opal_dev = NULL;
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01002555 }
Scott Bauera98e58e52017-02-03 12:50:32 -07002556
Helen Koikef9f38e32017-04-10 12:51:07 -03002557 if (dev->ctrl.oacs & NVME_CTRL_OACS_DBBUF_SUPP) {
2558 result = nvme_dbbuf_dma_alloc(dev);
2559 if (result)
2560 dev_warn(dev->dev,
2561 "unable to allocate dma for dbbuf\n");
2562 }
2563
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02002564 if (dev->ctrl.hmpre) {
2565 result = nvme_setup_host_mem(dev);
2566 if (result < 0)
2567 goto out;
2568 }
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002569
Keith Buschf0b50732013-07-15 15:02:21 -06002570 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002571 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002572 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002573
Keith Busch21f033f2016-04-12 11:13:11 -06002574 /*
Christoph Hellwig2659e572015-10-02 18:51:31 +02002575 * Keep the controller around but remove all namespaces if we don't have
2576 * any working I/O queue.
2577 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002578 if (dev->online_queues < 2) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002579 dev_warn(dev->ctrl.device, "IO queues not created\n");
Keith Busch3b247742016-04-27 15:51:18 -06002580 nvme_kill_queues(&dev->ctrl);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002581 nvme_remove_namespaces(&dev->ctrl);
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002582 new_state = NVME_CTRL_ADMIN_ONLY;
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002583 } else {
Keith Busch25646262016-01-04 09:10:57 -07002584 nvme_start_queues(&dev->ctrl);
Keith Busch302ad8c2017-03-01 14:22:12 -05002585 nvme_wait_freeze(&dev->ctrl);
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002586 /* hit this only when allocate tagset fails */
2587 if (nvme_dev_add(dev))
2588 new_state = NVME_CTRL_ADMIN_ONLY;
Keith Busch302ad8c2017-03-01 14:22:12 -05002589 nvme_unfreeze(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002590 }
2591
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002592 /*
2593 * If only admin queue live, keep it to do further investigation or
2594 * recovery.
2595 */
2596 if (!nvme_change_ctrl_state(&dev->ctrl, new_state)) {
2597 dev_warn(dev->ctrl.device,
2598 "failed to mark controller state %d\n", new_state);
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002599 goto out;
2600 }
Christoph Hellwig92911a52016-04-26 13:51:58 +02002601
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002602 nvme_start_ctrl(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002603 return;
Keith Buschf0b50732013-07-15 15:02:21 -06002604
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002605 out:
Keith Buschf58944e2016-02-24 09:15:55 -07002606 nvme_remove_dead_ctrl(dev, result);
Keith Buschf0b50732013-07-15 15:02:21 -06002607}
2608
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002609static void nvme_remove_dead_ctrl_work(struct work_struct *work)
Keith Busch9a6b9452013-12-10 13:10:36 -07002610{
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002611 struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002612 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002613
2614 if (pci_get_drvdata(pdev))
Keith Busch921920a2016-03-28 16:03:21 -06002615 device_release_driver(&pdev->dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002616 nvme_put_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002617}
2618
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002619static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
Keith Busch4cc06522015-06-05 10:30:08 -06002620{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002621 *val = readl(to_nvme_dev(ctrl)->bar + off);
2622 return 0;
Keith Busch4cc06522015-06-05 10:30:08 -06002623}
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002624
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002625static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2626{
2627 writel(val, to_nvme_dev(ctrl)->bar + off);
2628 return 0;
2629}
2630
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002631static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2632{
2633 *val = readq(to_nvme_dev(ctrl)->bar + off);
2634 return 0;
2635}
2636
Keith Busch97c12222018-03-08 14:50:32 -07002637static int nvme_pci_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
2638{
2639 struct pci_dev *pdev = to_pci_dev(to_nvme_dev(ctrl)->dev);
2640
2641 return snprintf(buf, size, "%s", dev_name(&pdev->dev));
2642}
2643
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002644static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
Ming Lin1a353d82016-06-13 16:45:24 +02002645 .name = "pcie",
Sagi Grimberge439bb12016-02-10 10:03:29 -08002646 .module = THIS_MODULE,
Logan Gunthorpee0596ab2018-10-04 15:27:44 -06002647 .flags = NVME_F_METADATA_SUPPORTED |
2648 NVME_F_PCI_P2PDMA,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002649 .reg_read32 = nvme_pci_reg_read32,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002650 .reg_write32 = nvme_pci_reg_write32,
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002651 .reg_read64 = nvme_pci_reg_read64,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002652 .free_ctrl = nvme_pci_free_ctrl,
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002653 .submit_async_event = nvme_pci_submit_async_event,
Keith Busch97c12222018-03-08 14:50:32 -07002654 .get_address = nvme_pci_get_address,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002655};
Keith Busch4cc06522015-06-05 10:30:08 -06002656
Keith Buschb00a7262016-02-24 09:15:52 -07002657static int nvme_dev_map(struct nvme_dev *dev)
2658{
Keith Buschb00a7262016-02-24 09:15:52 -07002659 struct pci_dev *pdev = to_pci_dev(dev->dev);
2660
Johannes Thumshirna1f447b2016-06-07 09:44:02 +02002661 if (pci_request_mem_regions(pdev, "nvme"))
Keith Buschb00a7262016-02-24 09:15:52 -07002662 return -ENODEV;
2663
Xu Yu97f6ef62017-05-24 16:39:55 +08002664 if (nvme_remap_bar(dev, NVME_REG_DBS + 4096))
Keith Buschb00a7262016-02-24 09:15:52 -07002665 goto release;
2666
Max Gurtovoy9fa196e2016-12-19 16:18:24 +02002667 return 0;
Keith Buschb00a7262016-02-24 09:15:52 -07002668 release:
Max Gurtovoy9fa196e2016-12-19 16:18:24 +02002669 pci_release_mem_regions(pdev);
2670 return -ENODEV;
Keith Buschb00a7262016-02-24 09:15:52 -07002671}
2672
Kai-Heng Feng8427bbc2017-11-09 01:12:03 -05002673static unsigned long check_vendor_combination_bug(struct pci_dev *pdev)
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002674{
2675 if (pdev->vendor == 0x144d && pdev->device == 0xa802) {
2676 /*
2677 * Several Samsung devices seem to drop off the PCIe bus
2678 * randomly when APST is on and uses the deepest sleep state.
2679 * This has been observed on a Samsung "SM951 NVMe SAMSUNG
2680 * 256GB", a "PM951 NVMe SAMSUNG 512GB", and a "Samsung SSD
2681 * 950 PRO 256GB", but it seems to be restricted to two Dell
2682 * laptops.
2683 */
2684 if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.") &&
2685 (dmi_match(DMI_PRODUCT_NAME, "XPS 15 9550") ||
2686 dmi_match(DMI_PRODUCT_NAME, "Precision 5510")))
2687 return NVME_QUIRK_NO_DEEPEST_PS;
Kai-Heng Feng8427bbc2017-11-09 01:12:03 -05002688 } else if (pdev->vendor == 0x144d && pdev->device == 0xa804) {
2689 /*
2690 * Samsung SSD 960 EVO drops off the PCIe bus after system
Jarosław Janik467c77d42018-03-11 19:51:56 +01002691 * suspend on a Ryzen board, ASUS PRIME B350M-A, as well as
2692 * within few minutes after bootup on a Coffee Lake board -
2693 * ASUS PRIME Z370-A
Kai-Heng Feng8427bbc2017-11-09 01:12:03 -05002694 */
2695 if (dmi_match(DMI_BOARD_VENDOR, "ASUSTeK COMPUTER INC.") &&
Jarosław Janik467c77d42018-03-11 19:51:56 +01002696 (dmi_match(DMI_BOARD_NAME, "PRIME B350M-A") ||
2697 dmi_match(DMI_BOARD_NAME, "PRIME Z370-A")))
Kai-Heng Feng8427bbc2017-11-09 01:12:03 -05002698 return NVME_QUIRK_NO_APST;
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002699 }
2700
2701 return 0;
2702}
2703
Keith Busch181197752018-04-27 13:42:52 -06002704static void nvme_async_probe(void *data, async_cookie_t cookie)
2705{
2706 struct nvme_dev *dev = data;
Keith Busch80f513b2018-05-07 08:30:24 -06002707
Keith Busch181197752018-04-27 13:42:52 -06002708 nvme_reset_ctrl_sync(&dev->ctrl);
2709 flush_work(&dev->ctrl.scan_work);
Keith Busch80f513b2018-05-07 08:30:24 -06002710 nvme_put_ctrl(&dev->ctrl);
Keith Busch181197752018-04-27 13:42:52 -06002711}
2712
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002713static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002714{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002715 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002716 struct nvme_dev *dev;
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002717 unsigned long quirks = id->driver_data;
Jens Axboe943e9422018-06-21 09:49:37 -06002718 size_t alloc_size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002719
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002720 node = dev_to_node(&pdev->dev);
2721 if (node == NUMA_NO_NODE)
Masayoshi Mizuma2fa84352016-06-20 09:33:17 +09002722 set_dev_node(&pdev->dev, first_memory_node);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002723
2724 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002725 if (!dev)
2726 return -ENOMEM;
Sagi Grimberg147b27e2018-01-14 12:39:01 +02002727
Jens Axboe3b6592f2018-10-31 08:36:31 -06002728 dev->queues = kcalloc_node(max_queue_count(), sizeof(struct nvme_queue),
2729 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002730 if (!dev->queues)
2731 goto free;
2732
Christoph Hellwige75ec752015-05-22 11:12:39 +02002733 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002734 pci_set_drvdata(pdev, dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002735
Keith Buschb00a7262016-02-24 09:15:52 -07002736 result = nvme_dev_map(dev);
2737 if (result)
Christophe JAILLETb00c9b72017-07-16 10:39:03 +02002738 goto put_pci;
Keith Buschb00a7262016-02-24 09:15:52 -07002739
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002740 INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002741 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
Keith Busch77bf25e2015-11-26 12:21:29 +01002742 mutex_init(&dev->shutdown_lock);
Keith Buschdb3cbff2016-01-12 14:41:17 -07002743 init_completion(&dev->ioq_wait);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002744
2745 result = nvme_setup_prp_pools(dev);
2746 if (result)
Christophe JAILLETb00c9b72017-07-16 10:39:03 +02002747 goto unmap;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002748
Kai-Heng Feng8427bbc2017-11-09 01:12:03 -05002749 quirks |= check_vendor_combination_bug(pdev);
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002750
Jens Axboe943e9422018-06-21 09:49:37 -06002751 /*
2752 * Double check that our mempool alloc size will cover the biggest
2753 * command we support.
2754 */
2755 alloc_size = nvme_pci_iod_alloc_size(dev, NVME_MAX_KB_SZ,
2756 NVME_MAX_SEGS, true);
2757 WARN_ON_ONCE(alloc_size > PAGE_SIZE);
2758
2759 dev->iod_mempool = mempool_create_node(1, mempool_kmalloc,
2760 mempool_kfree,
2761 (void *) alloc_size,
2762 GFP_KERNEL, node);
2763 if (!dev->iod_mempool) {
2764 result = -ENOMEM;
2765 goto release_pools;
2766 }
2767
Keith Buschb6e44b42018-07-11 16:44:44 -06002768 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
2769 quirks);
2770 if (result)
2771 goto release_mempool;
2772
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002773 dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev));
2774
Keith Busch80f513b2018-05-07 08:30:24 -06002775 nvme_get_ctrl(&dev->ctrl);
Keith Busch181197752018-04-27 13:42:52 -06002776 async_schedule(nvme_async_probe, dev);
Sagi Grimberg4caff8f2017-12-31 14:01:19 +02002777
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002778 return 0;
2779
Keith Buschb6e44b42018-07-11 16:44:44 -06002780 release_mempool:
2781 mempool_destroy(dev->iod_mempool);
Keith Busch0877cb02013-07-15 15:02:19 -06002782 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002783 nvme_release_prp_pools(dev);
Christophe JAILLETb00c9b72017-07-16 10:39:03 +02002784 unmap:
2785 nvme_dev_unmap(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002786 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002787 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002788 free:
2789 kfree(dev->queues);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002790 kfree(dev);
2791 return result;
2792}
2793
Christoph Hellwig775755e2017-06-01 13:10:38 +02002794static void nvme_reset_prepare(struct pci_dev *pdev)
Keith Buschf0d54a52014-05-02 10:40:43 -06002795{
Keith Buscha6739472014-06-23 16:03:21 -06002796 struct nvme_dev *dev = pci_get_drvdata(pdev);
Linus Torvaldsf263fbb2017-07-08 15:51:57 -07002797 nvme_dev_disable(dev, false);
Christoph Hellwig775755e2017-06-01 13:10:38 +02002798}
Keith Buschf0d54a52014-05-02 10:40:43 -06002799
Christoph Hellwig775755e2017-06-01 13:10:38 +02002800static void nvme_reset_done(struct pci_dev *pdev)
2801{
Linus Torvaldsf263fbb2017-07-08 15:51:57 -07002802 struct nvme_dev *dev = pci_get_drvdata(pdev);
Sagi Grimberg79c48cc2018-01-14 12:39:00 +02002803 nvme_reset_ctrl_sync(&dev->ctrl);
Keith Buschf0d54a52014-05-02 10:40:43 -06002804}
2805
Keith Busch09ece142014-01-27 11:29:40 -05002806static void nvme_shutdown(struct pci_dev *pdev)
2807{
2808 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buscha5cdb682016-01-12 14:41:18 -07002809 nvme_dev_disable(dev, true);
Keith Busch09ece142014-01-27 11:29:40 -05002810}
2811
Keith Buschf58944e2016-02-24 09:15:55 -07002812/*
2813 * The driver's remove may be called on a device in a partially initialized
2814 * state. This function must not have any dependencies on the device state in
2815 * order to proceed.
2816 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002817static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002818{
2819 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002820
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002821 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
Keith Busch9a6b9452013-12-10 13:10:36 -07002822 pci_set_drvdata(pdev, NULL);
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002823
Keith Busch6db28ed2017-02-10 18:15:49 -05002824 if (!pci_device_is_present(pdev)) {
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002825 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD);
Keith Busch1d39e692018-06-06 08:13:08 -06002826 nvme_dev_disable(dev, true);
Keith Buschcb4bfda2018-10-15 10:19:06 -06002827 nvme_dev_remove_admin(dev);
Keith Busch6db28ed2017-02-10 18:15:49 -05002828 }
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002829
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002830 flush_work(&dev->ctrl.reset_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002831 nvme_stop_ctrl(&dev->ctrl);
2832 nvme_remove_namespaces(&dev->ctrl);
Keith Buscha5cdb682016-01-12 14:41:18 -07002833 nvme_dev_disable(dev, true);
Keith Busch9fe5c592018-10-31 13:15:29 -06002834 nvme_release_cmb(dev);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002835 nvme_free_host_mem(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002836 nvme_dev_remove_admin(dev);
2837 nvme_free_queues(dev, 0);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002838 nvme_uninit_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002839 nvme_release_prp_pools(dev);
Keith Buschb00a7262016-02-24 09:15:52 -07002840 nvme_dev_unmap(dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002841 nvme_put_ctrl(&dev->ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002842}
2843
Jingoo Han671a6012014-02-13 11:19:14 +09002844#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002845static int nvme_suspend(struct device *dev)
2846{
2847 struct pci_dev *pdev = to_pci_dev(dev);
2848 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2849
Keith Buscha5cdb682016-01-12 14:41:18 -07002850 nvme_dev_disable(ndev, true);
Keith Buschcd638942013-07-15 15:02:23 -06002851 return 0;
2852}
2853
2854static int nvme_resume(struct device *dev)
2855{
2856 struct pci_dev *pdev = to_pci_dev(dev);
2857 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002858
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002859 nvme_reset_ctrl(&ndev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002860 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002861}
Jingoo Han671a6012014-02-13 11:19:14 +09002862#endif
Keith Buschcd638942013-07-15 15:02:23 -06002863
2864static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002865
Keith Buscha0a34082015-12-07 15:30:31 -07002866static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
2867 pci_channel_state_t state)
2868{
2869 struct nvme_dev *dev = pci_get_drvdata(pdev);
2870
2871 /*
2872 * A frozen channel requires a reset. When detected, this method will
2873 * shutdown the controller to quiesce. The controller will be restarted
2874 * after the slot reset through driver's slot_reset callback.
2875 */
Keith Buscha0a34082015-12-07 15:30:31 -07002876 switch (state) {
2877 case pci_channel_io_normal:
2878 return PCI_ERS_RESULT_CAN_RECOVER;
2879 case pci_channel_io_frozen:
Keith Buschd011fb32016-04-04 15:07:41 -06002880 dev_warn(dev->ctrl.device,
2881 "frozen state error detected, reset controller\n");
Keith Buscha5cdb682016-01-12 14:41:18 -07002882 nvme_dev_disable(dev, false);
Keith Buscha0a34082015-12-07 15:30:31 -07002883 return PCI_ERS_RESULT_NEED_RESET;
2884 case pci_channel_io_perm_failure:
Keith Buschd011fb32016-04-04 15:07:41 -06002885 dev_warn(dev->ctrl.device,
2886 "failure state error detected, request disconnect\n");
Keith Buscha0a34082015-12-07 15:30:31 -07002887 return PCI_ERS_RESULT_DISCONNECT;
2888 }
2889 return PCI_ERS_RESULT_NEED_RESET;
2890}
2891
2892static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev)
2893{
2894 struct nvme_dev *dev = pci_get_drvdata(pdev);
2895
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002896 dev_info(dev->ctrl.device, "restart after slot reset\n");
Keith Buscha0a34082015-12-07 15:30:31 -07002897 pci_restore_state(pdev);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002898 nvme_reset_ctrl(&dev->ctrl);
Keith Buscha0a34082015-12-07 15:30:31 -07002899 return PCI_ERS_RESULT_RECOVERED;
2900}
2901
2902static void nvme_error_resume(struct pci_dev *pdev)
2903{
Keith Busch72cd4cc2018-05-24 16:16:04 -06002904 struct nvme_dev *dev = pci_get_drvdata(pdev);
2905
2906 flush_work(&dev->ctrl.reset_work);
Keith Buscha0a34082015-12-07 15:30:31 -07002907}
2908
Stephen Hemminger1d352032012-09-07 09:33:17 -07002909static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002910 .error_detected = nvme_error_detected,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002911 .slot_reset = nvme_slot_reset,
2912 .resume = nvme_error_resume,
Christoph Hellwig775755e2017-06-01 13:10:38 +02002913 .reset_prepare = nvme_reset_prepare,
2914 .reset_done = nvme_reset_done,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002915};
2916
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002917static const struct pci_device_id nvme_id_table[] = {
Christoph Hellwig106198e2015-11-26 10:07:41 +01002918 { PCI_VDEVICE(INTEL, 0x0953),
Keith Busch08095e72016-03-04 13:15:17 -07002919 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002920 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch99466e72016-05-02 15:14:24 -06002921 { PCI_VDEVICE(INTEL, 0x0a53),
2922 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002923 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch99466e72016-05-02 15:14:24 -06002924 { PCI_VDEVICE(INTEL, 0x0a54),
2925 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002926 NVME_QUIRK_DEALLOCATE_ZEROES, },
David Wayne Fugatef99cb7af2017-07-10 12:39:59 -06002927 { PCI_VDEVICE(INTEL, 0x0a55),
2928 .driver_data = NVME_QUIRK_STRIPE_SIZE |
2929 NVME_QUIRK_DEALLOCATE_ZEROES, },
Andy Lutomirski50af47d2017-05-24 15:06:31 -07002930 { PCI_VDEVICE(INTEL, 0xf1a5), /* Intel 600P/P3100 */
Jens Axboe9abd68e2018-05-08 10:25:15 -06002931 .driver_data = NVME_QUIRK_NO_DEEPEST_PS |
2932 NVME_QUIRK_MEDIUM_PRIO_SQ },
Keith Busch540c8012015-10-22 15:45:06 -06002933 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2934 .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
Micah Parrish0302ae62018-04-12 13:25:25 -06002935 { PCI_DEVICE(0x1bb1, 0x0100), /* Seagate Nytro Flash Storage */
2936 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03002937 { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */
2938 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Jeff Lien8c97eec2017-11-21 10:44:37 -06002939 { PCI_DEVICE(0x1c58, 0x0023), /* WDC SN200 adapter */
2940 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Wenbo Wang015282c2016-09-08 12:12:11 -04002941 { PCI_DEVICE(0x1c5f, 0x0540), /* Memblaze Pblaze4 adapter */
2942 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Martin K. Petersend554b5e2017-06-27 22:27:57 -04002943 { PCI_DEVICE(0x144d, 0xa821), /* Samsung PM1725 */
2944 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
2945 { PCI_DEVICE(0x144d, 0xa822), /* Samsung PM1725a */
2946 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002947 { PCI_DEVICE(0x1d1d, 0x1f1f), /* LighNVM qemu device */
2948 .driver_data = NVME_QUIRK_LIGHTNVM, },
2949 { PCI_DEVICE(0x1d1d, 0x2807), /* CNEX WL */
2950 .driver_data = NVME_QUIRK_LIGHTNVM, },
Wei Xuea48e872018-04-26 14:59:19 -06002951 { PCI_DEVICE(0x1d1d, 0x2601), /* CNEX Granby */
2952 .driver_data = NVME_QUIRK_LIGHTNVM, },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002953 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01002954 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Daniel Roschka124298b2017-02-22 15:17:29 -07002955 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002956 { 0, }
2957};
2958MODULE_DEVICE_TABLE(pci, nvme_id_table);
2959
2960static struct pci_driver nvme_driver = {
2961 .name = "nvme",
2962 .id_table = nvme_id_table,
2963 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002964 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002965 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002966 .driver = {
2967 .pm = &nvme_dev_pm_ops,
2968 },
Alexander Duyck74d986a2018-04-24 16:47:27 -05002969 .sriov_configure = pci_sriov_configure_simple,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002970 .err_handler = &nvme_err_handler,
2971};
2972
2973static int __init nvme_init(void)
2974{
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002975 return pci_register_driver(&nvme_driver);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002976}
2977
2978static void __exit nvme_exit(void)
2979{
2980 pci_unregister_driver(&nvme_driver);
Ming Lei03e0f3a2017-11-09 19:32:07 +08002981 flush_workqueue(nvme_wq);
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002982 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002983}
2984
2985MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2986MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002987MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002988module_init(nvme_init);
2989module_exit(nvme_exit);