blob: bafdc2ab5be3c542ce3acfb1bbb0078bc8c79660 [file] [log] [blame]
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001/*
2 * NVM Express device driver
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04003 * Copyright (c) 2011-2014, Intel Corporation.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050013 */
14
Keith Buscha0a34082015-12-07 15:30:31 -070015#include <linux/aer.h>
Matthew Wilcox8de05532011-05-12 13:50:28 -040016#include <linux/bitops.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050017#include <linux/blkdev.h>
Matias Bjørlinga4aea562014-11-04 08:20:14 -070018#include <linux/blk-mq.h>
Christoph Hellwigdca51e72016-09-14 16:18:57 +020019#include <linux/blk-mq-pci.h>
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>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050029#include <linux/poison.h>
Keith Busche1e5e562015-02-19 13:39:03 -070030#include <linux/t10-pi.h>
Christoph Hellwig2d55cd52016-02-29 15:59:46 +010031#include <linux/timer.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050032#include <linux/types.h>
Linus Torvalds9cf5c092015-11-06 14:22:15 -080033#include <linux/io-64-nonatomic-lo-hi.h>
Keith Busch1d277a62015-10-15 14:10:52 +020034#include <asm/unaligned.h>
Scott Bauera98e58e52017-02-03 12:50:32 -070035#include <linux/sed-opal.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090036
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020037#include "nvme.h"
38
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050039#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
40#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Stephen Batesc9658092016-12-16 11:54:50 -070041
Christoph Hellwigadf68f22015-11-28 15:42:28 +010042/*
43 * We handle AEN commands ourselves and don't even let the
44 * block layer know about them.
45 */
Christoph Hellwigf866fc422016-04-26 13:52:00 +020046#define NVME_AQ_BLKMQ_DEPTH (NVME_AQ_DEPTH - NVME_NR_AERS)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050047
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050048static int use_threaded_interrupts;
49module_param(use_threaded_interrupts, int, 0);
50
Jon Derrick8ffaadf2015-07-20 10:14:09 -060051static bool use_cmb_sqes = true;
52module_param(use_cmb_sqes, bool, 0644);
53MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
54
Christoph Hellwig87ad72a2017-05-12 17:02:58 +020055static unsigned int max_host_mem_size_mb = 128;
56module_param(max_host_mem_size_mb, uint, 0444);
57MODULE_PARM_DESC(max_host_mem_size_mb,
58 "Maximum Host Memory Buffer (HMB) size per controller (in MiB)");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050059
weiping zhangb27c1e62017-07-10 16:46:59 +080060static int io_queue_depth_set(const char *val, const struct kernel_param *kp);
61static const struct kernel_param_ops io_queue_depth_ops = {
62 .set = io_queue_depth_set,
63 .get = param_get_int,
64};
65
66static int io_queue_depth = 1024;
67module_param_cb(io_queue_depth, &io_queue_depth_ops, &io_queue_depth, 0644);
68MODULE_PARM_DESC(io_queue_depth, "set io queue depth, should >= 2");
69
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010070struct nvme_dev;
71struct nvme_queue;
Keith Buschb3fffde2015-02-03 11:21:42 -070072
Jens Axboea0fa9642015-11-03 20:37:26 -070073static void nvme_process_cq(struct nvme_queue *nvmeq);
Keith Buscha5cdb682016-01-12 14:41:18 -070074static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown);
Keith Buschd4b4ff82013-12-10 13:10:37 -070075
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050076/*
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010077 * Represents an NVM Express device. Each nvme_dev is a PCI function.
78 */
79struct nvme_dev {
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010080 struct nvme_queue **queues;
81 struct blk_mq_tag_set tagset;
82 struct blk_mq_tag_set admin_tagset;
83 u32 __iomem *dbs;
84 struct device *dev;
85 struct dma_pool *prp_page_pool;
86 struct dma_pool *prp_small_pool;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010087 unsigned online_queues;
88 unsigned max_qid;
89 int q_depth;
90 u32 db_stride;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010091 void __iomem *bar;
Xu Yu97f6ef62017-05-24 16:39:55 +080092 unsigned long bar_mapped_size;
Christoph Hellwig5c8809e2015-11-26 12:35:49 +010093 struct work_struct remove_work;
Keith Busch77bf25e2015-11-26 12:21:29 +010094 struct mutex shutdown_lock;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010095 bool subsystem;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010096 void __iomem *cmb;
97 dma_addr_t cmb_dma_addr;
98 u64 cmb_size;
99 u32 cmbsz;
Stephen Bates202021c2016-10-05 20:01:12 -0600100 u32 cmbloc;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100101 struct nvme_ctrl ctrl;
Keith Buschdb3cbff2016-01-12 14:41:17 -0700102 struct completion ioq_wait;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +0200103
104 /* shadow doorbell buffer support: */
Helen Koikef9f38e32017-04-10 12:51:07 -0300105 u32 *dbbuf_dbs;
106 dma_addr_t dbbuf_dbs_dma_addr;
107 u32 *dbbuf_eis;
108 dma_addr_t dbbuf_eis_dma_addr;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +0200109
110 /* host memory buffer support: */
111 u64 host_mem_size;
112 u32 nr_host_mem_descs;
Christoph Hellwig4033f352017-08-28 10:47:18 +0200113 dma_addr_t host_mem_descs_dma;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +0200114 struct nvme_host_mem_buf_desc *host_mem_descs;
115 void **host_mem_desc_bufs;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500116};
117
weiping zhangb27c1e62017-07-10 16:46:59 +0800118static int io_queue_depth_set(const char *val, const struct kernel_param *kp)
119{
120 int n = 0, ret;
121
122 ret = kstrtoint(val, 10, &n);
123 if (ret != 0 || n < 2)
124 return -EINVAL;
125
126 return param_set_int(val, kp);
127}
128
Helen Koikef9f38e32017-04-10 12:51:07 -0300129static inline unsigned int sq_idx(unsigned int qid, u32 stride)
130{
131 return qid * 2 * stride;
132}
133
134static inline unsigned int cq_idx(unsigned int qid, u32 stride)
135{
136 return (qid * 2 + 1) * stride;
137}
138
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100139static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
140{
141 return container_of(ctrl, struct nvme_dev, ctrl);
142}
143
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500144/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500145 * An NVM Express queue. Each device has at least two (one for admin
146 * commands and one for I/O commands).
147 */
148struct nvme_queue {
149 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500150 struct nvme_dev *dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500151 spinlock_t q_lock;
152 struct nvme_command *sq_cmds;
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600153 struct nvme_command __iomem *sq_cmds_io;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500154 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600155 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500156 dma_addr_t sq_dma_addr;
157 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500158 u32 __iomem *q_db;
159 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700160 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500161 u16 sq_tail;
162 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700163 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400164 u8 cq_phase;
165 u8 cqe_seen;
Helen Koikef9f38e32017-04-10 12:51:07 -0300166 u32 *dbbuf_sq_db;
167 u32 *dbbuf_cq_db;
168 u32 *dbbuf_sq_ei;
169 u32 *dbbuf_cq_ei;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500170};
171
172/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200173 * The nvme_iod describes the data in an I/O, including the list of PRP
174 * entries. You can't see it in this data structure because C doesn't let
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100175 * me express that. Use nvme_init_iod to ensure there's enough space
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200176 * allocated to store the PRP list.
177 */
178struct nvme_iod {
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800179 struct nvme_request req;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100180 struct nvme_queue *nvmeq;
181 int aborted;
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200182 int npages; /* In the PRP list. 0 means small pool in use */
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200183 int nents; /* Used in scatterlist */
184 int length; /* Of data, in bytes */
185 dma_addr_t first_dma;
Christoph Hellwigbf684052015-10-26 17:12:51 +0900186 struct scatterlist meta_sg; /* metadata requires single contiguous buffer */
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100187 struct scatterlist *sg;
188 struct scatterlist inline_sg[0];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500189};
190
191/*
192 * Check we didin't inadvertently grow the command struct
193 */
194static inline void _nvme_check_size(void)
195{
196 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
197 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
198 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
199 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
200 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400201 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700202 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500203 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
Johannes Thumshirn0add5e82017-06-07 11:45:29 +0200204 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != NVME_IDENTIFY_DATA_SIZE);
205 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != NVME_IDENTIFY_DATA_SIZE);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500206 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600207 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Helen Koikef9f38e32017-04-10 12:51:07 -0300208 BUILD_BUG_ON(sizeof(struct nvme_dbbuf) != 64);
209}
210
211static inline unsigned int nvme_dbbuf_size(u32 stride)
212{
213 return ((num_possible_cpus() + 1) * 8 * stride);
214}
215
216static int nvme_dbbuf_dma_alloc(struct nvme_dev *dev)
217{
218 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
219
220 if (dev->dbbuf_dbs)
221 return 0;
222
223 dev->dbbuf_dbs = dma_alloc_coherent(dev->dev, mem_size,
224 &dev->dbbuf_dbs_dma_addr,
225 GFP_KERNEL);
226 if (!dev->dbbuf_dbs)
227 return -ENOMEM;
228 dev->dbbuf_eis = dma_alloc_coherent(dev->dev, mem_size,
229 &dev->dbbuf_eis_dma_addr,
230 GFP_KERNEL);
231 if (!dev->dbbuf_eis) {
232 dma_free_coherent(dev->dev, mem_size,
233 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
234 dev->dbbuf_dbs = NULL;
235 return -ENOMEM;
236 }
237
238 return 0;
239}
240
241static void nvme_dbbuf_dma_free(struct nvme_dev *dev)
242{
243 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
244
245 if (dev->dbbuf_dbs) {
246 dma_free_coherent(dev->dev, mem_size,
247 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
248 dev->dbbuf_dbs = NULL;
249 }
250 if (dev->dbbuf_eis) {
251 dma_free_coherent(dev->dev, mem_size,
252 dev->dbbuf_eis, dev->dbbuf_eis_dma_addr);
253 dev->dbbuf_eis = NULL;
254 }
255}
256
257static void nvme_dbbuf_init(struct nvme_dev *dev,
258 struct nvme_queue *nvmeq, int qid)
259{
260 if (!dev->dbbuf_dbs || !qid)
261 return;
262
263 nvmeq->dbbuf_sq_db = &dev->dbbuf_dbs[sq_idx(qid, dev->db_stride)];
264 nvmeq->dbbuf_cq_db = &dev->dbbuf_dbs[cq_idx(qid, dev->db_stride)];
265 nvmeq->dbbuf_sq_ei = &dev->dbbuf_eis[sq_idx(qid, dev->db_stride)];
266 nvmeq->dbbuf_cq_ei = &dev->dbbuf_eis[cq_idx(qid, dev->db_stride)];
267}
268
269static void nvme_dbbuf_set(struct nvme_dev *dev)
270{
271 struct nvme_command c;
272
273 if (!dev->dbbuf_dbs)
274 return;
275
276 memset(&c, 0, sizeof(c));
277 c.dbbuf.opcode = nvme_admin_dbbuf;
278 c.dbbuf.prp1 = cpu_to_le64(dev->dbbuf_dbs_dma_addr);
279 c.dbbuf.prp2 = cpu_to_le64(dev->dbbuf_eis_dma_addr);
280
281 if (nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0)) {
Christoph Hellwig9bdcfb12017-05-20 15:14:43 +0200282 dev_warn(dev->ctrl.device, "unable to set dbbuf\n");
Helen Koikef9f38e32017-04-10 12:51:07 -0300283 /* Free memory and continue on */
284 nvme_dbbuf_dma_free(dev);
285 }
286}
287
288static inline int nvme_dbbuf_need_event(u16 event_idx, u16 new_idx, u16 old)
289{
290 return (u16)(new_idx - event_idx - 1) < (u16)(new_idx - old);
291}
292
293/* Update dbbuf and return true if an MMIO is required */
294static bool nvme_dbbuf_update_and_check_event(u16 value, u32 *dbbuf_db,
295 volatile u32 *dbbuf_ei)
296{
297 if (dbbuf_db) {
298 u16 old_value;
299
300 /*
301 * Ensure that the queue is written before updating
302 * the doorbell in memory
303 */
304 wmb();
305
306 old_value = *dbbuf_db;
307 *dbbuf_db = value;
308
309 if (!nvme_dbbuf_need_event(*dbbuf_ei, value, old_value))
310 return false;
311 }
312
313 return true;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500314}
315
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700316/*
317 * Max size of iod being embedded in the request payload
318 */
319#define NVME_INT_PAGES 2
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100320#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700321
322/*
323 * Will slightly overestimate the number of pages needed. This is OK
324 * as it only leads to a small amount of wasted memory for the lifetime of
325 * the I/O.
326 */
327static int nvme_npages(unsigned size, struct nvme_dev *dev)
328{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100329 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
330 dev->ctrl.page_size);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700331 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
332}
333
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100334static unsigned int nvme_iod_alloc_size(struct nvme_dev *dev,
335 unsigned int size, unsigned int nseg)
336{
337 return sizeof(__le64 *) * nvme_npages(size, dev) +
338 sizeof(struct scatterlist) * nseg;
339}
340
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700341static unsigned int nvme_cmd_size(struct nvme_dev *dev)
342{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100343 return sizeof(struct nvme_iod) +
344 nvme_iod_alloc_size(dev, NVME_INT_BYTES(dev), NVME_INT_PAGES);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700345}
346
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700347static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
348 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500349{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700350 struct nvme_dev *dev = data;
351 struct nvme_queue *nvmeq = dev->queues[0];
352
Keith Busch42483222015-06-01 09:29:54 -0600353 WARN_ON(hctx_idx != 0);
354 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
355 WARN_ON(nvmeq->tags);
356
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700357 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600358 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700359 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500360}
361
Keith Busch4af0e212015-06-08 10:08:13 -0600362static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
363{
364 struct nvme_queue *nvmeq = hctx->driver_data;
365
366 nvmeq->tags = NULL;
367}
368
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700369static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
370 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500371{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700372 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600373 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500374
Keith Busch42483222015-06-01 09:29:54 -0600375 if (!nvmeq->tags)
376 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500377
Keith Busch42483222015-06-01 09:29:54 -0600378 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700379 hctx->driver_data = nvmeq;
380 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500381}
382
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600383static int nvme_init_request(struct blk_mq_tag_set *set, struct request *req,
384 unsigned int hctx_idx, unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500385{
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600386 struct nvme_dev *dev = set->driver_data;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100387 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwig03508152017-06-13 09:15:18 +0200388 int queue_idx = (set == &dev->tagset) ? hctx_idx + 1 : 0;
389 struct nvme_queue *nvmeq = dev->queues[queue_idx];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700390
391 BUG_ON(!nvmeq);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100392 iod->nvmeq = nvmeq;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700393 return 0;
394}
395
Christoph Hellwigdca51e72016-09-14 16:18:57 +0200396static int nvme_pci_map_queues(struct blk_mq_tag_set *set)
397{
398 struct nvme_dev *dev = set->driver_data;
399
400 return blk_mq_pci_map_queues(set, to_pci_dev(dev->dev));
401}
402
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500403/**
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100404 * __nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500405 * @nvmeq: The queue to use
406 * @cmd: The command to send
407 *
408 * Safe to use from interrupt context
409 */
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530410static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
411 struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500412{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700413 u16 tail = nvmeq->sq_tail;
414
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600415 if (nvmeq->sq_cmds_io)
416 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
417 else
418 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
419
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500420 if (++tail == nvmeq->q_depth)
421 tail = 0;
Helen Koikef9f38e32017-04-10 12:51:07 -0300422 if (nvme_dbbuf_update_and_check_event(tail, nvmeq->dbbuf_sq_db,
423 nvmeq->dbbuf_sq_ei))
424 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500425 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500426}
427
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100428static __le64 **iod_list(struct request *req)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700429{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100430 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700431 return (__le64 **)(iod->sg + blk_rq_nr_phys_segments(req));
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700432}
433
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200434static blk_status_t nvme_init_iod(struct request *rq, struct nvme_dev *dev)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500435{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100436 struct nvme_iod *iod = blk_mq_rq_to_pdu(rq);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700437 int nseg = blk_rq_nr_phys_segments(rq);
Christoph Hellwigb131c612017-01-13 12:29:12 +0100438 unsigned int size = blk_rq_payload_bytes(rq);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500439
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100440 if (nseg > NVME_INT_PAGES || size > NVME_INT_BYTES(dev)) {
441 iod->sg = kmalloc(nvme_iod_alloc_size(dev, size, nseg), GFP_ATOMIC);
442 if (!iod->sg)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200443 return BLK_STS_RESOURCE;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100444 } else {
445 iod->sg = iod->inline_sg;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700446 }
447
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100448 iod->aborted = 0;
449 iod->npages = -1;
450 iod->nents = 0;
451 iod->length = size;
Keith Buschf80ec962016-07-12 16:20:31 -0700452
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200453 return BLK_STS_OK;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700454}
455
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100456static void nvme_free_iod(struct nvme_dev *dev, struct request *req)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500457{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100458 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100459 const int last_prp = dev->ctrl.page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500460 int i;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100461 __le64 **list = iod_list(req);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500462 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500463
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500464 if (iod->npages == 0)
465 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
466 for (i = 0; i < iod->npages; i++) {
467 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500468 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500469 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500470 prp_dma = next_prp_dma;
471 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700472
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100473 if (iod->sg != iod->inline_sg)
474 kfree(iod->sg);
Keith Buschb4ff9c82014-08-29 09:06:12 -0600475}
476
Keith Busch52b68d72015-02-23 09:16:21 -0700477#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700478static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
479{
480 if (be32_to_cpu(pi->ref_tag) == v)
481 pi->ref_tag = cpu_to_be32(p);
482}
483
484static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
485{
486 if (be32_to_cpu(pi->ref_tag) == p)
487 pi->ref_tag = cpu_to_be32(v);
488}
489
490/**
491 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
492 *
493 * The virtual start sector is the one that was originally submitted by the
494 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
495 * start sector may be different. Remap protection information to match the
496 * physical LBA on writes, and back to the original seed on reads.
497 *
498 * Type 0 and 3 do not have a ref tag, so no remapping required.
499 */
500static void nvme_dif_remap(struct request *req,
501 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
502{
503 struct nvme_ns *ns = req->rq_disk->private_data;
504 struct bio_integrity_payload *bip;
505 struct t10_pi_tuple *pi;
506 void *p, *pmap;
507 u32 i, nlb, ts, phys, virt;
508
509 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
510 return;
511
512 bip = bio_integrity(req->bio);
513 if (!bip)
514 return;
515
516 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700517
518 p = pmap;
519 virt = bip_get_seed(bip);
520 phys = nvme_block_nr(ns, blk_rq_pos(req));
521 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
Dan Williamsac6fc482015-10-21 13:20:18 -0400522 ts = ns->disk->queue->integrity.tuple_size;
Keith Busche1e5e562015-02-19 13:39:03 -0700523
524 for (i = 0; i < nlb; i++, virt++, phys++) {
525 pi = (struct t10_pi_tuple *)p;
526 dif_swap(phys, virt, pi);
527 p += ts;
528 }
529 kunmap_atomic(pmap);
530}
Keith Busch52b68d72015-02-23 09:16:21 -0700531#else /* CONFIG_BLK_DEV_INTEGRITY */
532static void nvme_dif_remap(struct request *req,
533 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
534{
535}
536static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
537{
538}
539static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
540{
541}
Keith Busch52b68d72015-02-23 09:16:21 -0700542#endif
543
Keith Buschd0877472017-09-15 13:05:38 -0400544static void nvme_print_sgl(struct scatterlist *sgl, int nents)
545{
546 int i;
547 struct scatterlist *sg;
548
549 for_each_sg(sgl, sg, nents, i) {
550 dma_addr_t phys = sg_phys(sg);
551 pr_warn("sg[%d] phys_addr:%pad offset:%d length:%d "
552 "dma_address:%pad dma_length:%d\n",
553 i, &phys, sg->offset, sg->length, &sg_dma_address(sg),
554 sg_dma_len(sg));
555 }
556}
557
Keith Busch86eea282017-07-12 15:59:07 -0400558static blk_status_t nvme_setup_prps(struct nvme_dev *dev, struct request *req)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500559{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100560 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500561 struct dma_pool *pool;
Christoph Hellwigb131c612017-01-13 12:29:12 +0100562 int length = blk_rq_payload_bytes(req);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500563 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500564 int dma_len = sg_dma_len(sg);
565 u64 dma_addr = sg_dma_address(sg);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100566 u32 page_size = dev->ctrl.page_size;
Murali Iyerf137e0f2015-03-26 11:07:51 -0500567 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500568 __le64 *prp_list;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100569 __le64 **list = iod_list(req);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500570 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500571 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500572
Keith Busch1d090622014-06-23 11:34:01 -0600573 length -= (page_size - offset);
Jan H. Schönherr5228b322017-08-27 15:56:37 +0200574 if (length <= 0) {
575 iod->first_dma = 0;
Keith Busch86eea282017-07-12 15:59:07 -0400576 return BLK_STS_OK;
Jan H. Schönherr5228b322017-08-27 15:56:37 +0200577 }
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500578
Keith Busch1d090622014-06-23 11:34:01 -0600579 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500580 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600581 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500582 } else {
583 sg = sg_next(sg);
584 dma_addr = sg_dma_address(sg);
585 dma_len = sg_dma_len(sg);
586 }
587
Keith Busch1d090622014-06-23 11:34:01 -0600588 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600589 iod->first_dma = dma_addr;
Keith Busch86eea282017-07-12 15:59:07 -0400590 return BLK_STS_OK;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500591 }
592
Keith Busch1d090622014-06-23 11:34:01 -0600593 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500594 if (nprps <= (256 / 8)) {
595 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500596 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500597 } else {
598 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500599 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500600 }
601
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200602 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400603 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600604 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500605 iod->npages = -1;
Keith Busch86eea282017-07-12 15:59:07 -0400606 return BLK_STS_RESOURCE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400607 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500608 list[0] = prp_list;
609 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500610 i = 0;
611 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600612 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500613 __le64 *old_prp_list = prp_list;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200614 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500615 if (!prp_list)
Keith Busch86eea282017-07-12 15:59:07 -0400616 return BLK_STS_RESOURCE;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500617 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400618 prp_list[0] = old_prp_list[i - 1];
619 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
620 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500621 }
622 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600623 dma_len -= page_size;
624 dma_addr += page_size;
625 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500626 if (length <= 0)
627 break;
628 if (dma_len > 0)
629 continue;
Keith Busch86eea282017-07-12 15:59:07 -0400630 if (unlikely(dma_len < 0))
631 goto bad_sgl;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500632 sg = sg_next(sg);
633 dma_addr = sg_dma_address(sg);
634 dma_len = sg_dma_len(sg);
635 }
636
Keith Busch86eea282017-07-12 15:59:07 -0400637 return BLK_STS_OK;
638
639 bad_sgl:
Keith Buschd0877472017-09-15 13:05:38 -0400640 WARN(DO_ONCE(nvme_print_sgl, iod->sg, iod->nents),
641 "Invalid SGL for payload:%d nents:%d\n",
642 blk_rq_payload_bytes(req), iod->nents);
Keith Busch86eea282017-07-12 15:59:07 -0400643 return BLK_STS_IOERR;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500644}
645
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200646static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,
Christoph Hellwigb131c612017-01-13 12:29:12 +0100647 struct nvme_command *cmnd)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200648{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100649 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200650 struct request_queue *q = req->q;
651 enum dma_data_direction dma_dir = rq_data_dir(req) ?
652 DMA_TO_DEVICE : DMA_FROM_DEVICE;
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200653 blk_status_t ret = BLK_STS_IOERR;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200654
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700655 sg_init_table(iod->sg, blk_rq_nr_phys_segments(req));
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200656 iod->nents = blk_rq_map_sg(q, req, iod->sg);
657 if (!iod->nents)
658 goto out;
659
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200660 ret = BLK_STS_RESOURCE;
Mauricio Faria de Oliveira2b6b5352016-10-11 13:54:20 -0700661 if (!dma_map_sg_attrs(dev->dev, iod->sg, iod->nents, dma_dir,
662 DMA_ATTR_NO_WARN))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200663 goto out;
664
Keith Busch86eea282017-07-12 15:59:07 -0400665 ret = nvme_setup_prps(dev, req);
666 if (ret != BLK_STS_OK)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200667 goto out_unmap;
668
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200669 ret = BLK_STS_IOERR;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200670 if (blk_integrity_rq(req)) {
671 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
672 goto out_unmap;
673
Christoph Hellwigbf684052015-10-26 17:12:51 +0900674 sg_init_table(&iod->meta_sg, 1);
675 if (blk_rq_map_integrity_sg(q, req->bio, &iod->meta_sg) != 1)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200676 goto out_unmap;
677
Keith Buschb5d8af52017-08-29 17:46:02 -0400678 if (req_op(req) == REQ_OP_WRITE)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200679 nvme_dif_remap(req, nvme_dif_prep);
680
Christoph Hellwigbf684052015-10-26 17:12:51 +0900681 if (!dma_map_sg(dev->dev, &iod->meta_sg, 1, dma_dir))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200682 goto out_unmap;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200683 }
684
Christoph Hellwigeb793e22016-06-13 16:45:25 +0200685 cmnd->rw.dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
686 cmnd->rw.dptr.prp2 = cpu_to_le64(iod->first_dma);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200687 if (blk_integrity_rq(req))
Christoph Hellwigbf684052015-10-26 17:12:51 +0900688 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(&iod->meta_sg));
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200689 return BLK_STS_OK;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200690
691out_unmap:
692 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
693out:
694 return ret;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200695}
696
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100697static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100698{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100699 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100700 enum dma_data_direction dma_dir = rq_data_dir(req) ?
701 DMA_TO_DEVICE : DMA_FROM_DEVICE;
702
703 if (iod->nents) {
704 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
705 if (blk_integrity_rq(req)) {
Keith Buschb5d8af52017-08-29 17:46:02 -0400706 if (req_op(req) == REQ_OP_READ)
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100707 nvme_dif_remap(req, nvme_dif_complete);
Christoph Hellwigbf684052015-10-26 17:12:51 +0900708 dma_unmap_sg(dev->dev, &iod->meta_sg, 1, dma_dir);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100709 }
710 }
711
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700712 nvme_cleanup_cmd(req);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100713 nvme_free_iod(dev, req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500714}
715
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700716/*
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200717 * NOTE: ns is NULL when called on the admin queue.
718 */
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200719static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700720 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600721{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700722 struct nvme_ns *ns = hctx->queue->queuedata;
723 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200724 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700725 struct request *req = bd->rq;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200726 struct nvme_command cmnd;
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200727 blk_status_t ret;
Keith Busche1e5e562015-02-19 13:39:03 -0700728
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700729 ret = nvme_setup_cmd(ns, req, &cmnd);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200730 if (ret)
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100731 return ret;
Keith Buschedd10d32014-04-03 16:45:23 -0600732
Christoph Hellwigb131c612017-01-13 12:29:12 +0100733 ret = nvme_init_iod(req, dev);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200734 if (ret)
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700735 goto out_free_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600736
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200737 if (blk_rq_nr_phys_segments(req)) {
Christoph Hellwigb131c612017-01-13 12:29:12 +0100738 ret = nvme_map_data(dev, req, &cmnd);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200739 if (ret)
740 goto out_cleanup_iod;
741 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700742
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100743 blk_mq_start_request(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200744
745 spin_lock_irq(&nvmeq->q_lock);
Keith Buschae1fba22016-02-11 13:05:42 -0700746 if (unlikely(nvmeq->cq_vector < 0)) {
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200747 ret = BLK_STS_IOERR;
Keith Buschae1fba22016-02-11 13:05:42 -0700748 spin_unlock_irq(&nvmeq->q_lock);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700749 goto out_cleanup_iod;
Keith Buschae1fba22016-02-11 13:05:42 -0700750 }
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200751 __nvme_submit_cmd(nvmeq, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700752 nvme_process_cq(nvmeq);
753 spin_unlock_irq(&nvmeq->q_lock);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200754 return BLK_STS_OK;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700755out_cleanup_iod:
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100756 nvme_free_iod(dev, req);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700757out_free_cmd:
758 nvme_cleanup_cmd(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200759 return ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500760}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500761
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200762static void nvme_pci_complete_rq(struct request *req)
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100763{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100764 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100765
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200766 nvme_unmap_data(iod->nvmeq->dev, req);
767 nvme_complete_rq(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500768}
769
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100770/* We read the CQE phase first to check if the rest of the entry is valid */
771static inline bool nvme_cqe_valid(struct nvme_queue *nvmeq, u16 head,
772 u16 phase)
773{
774 return (le16_to_cpu(nvmeq->cqes[head].status) & 1) == phase;
775}
776
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300777static inline void nvme_ring_cq_doorbell(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500778{
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300779 u16 head = nvmeq->cq_head;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500780
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300781 if (likely(nvmeq->cq_vector >= 0)) {
Helen Koikef9f38e32017-04-10 12:51:07 -0300782 if (nvme_dbbuf_update_and_check_event(head, nvmeq->dbbuf_cq_db,
783 nvmeq->dbbuf_cq_ei))
784 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300785 }
786}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500787
Sagi Grimberg83a12fb2017-06-18 17:28:08 +0300788static inline void nvme_handle_cqe(struct nvme_queue *nvmeq,
789 struct nvme_completion *cqe)
790{
791 struct request *req;
792
793 if (unlikely(cqe->command_id >= nvmeq->q_depth)) {
794 dev_warn(nvmeq->dev->ctrl.device,
795 "invalid id %d completed on queue %d\n",
796 cqe->command_id, le16_to_cpu(cqe->sq_id));
797 return;
798 }
799
800 /*
801 * AEN requests are special as they don't time out and can
802 * survive any kind of queue freeze and often don't respond to
803 * aborts. We don't even bother to allocate a struct request
804 * for them but rather special case them here.
805 */
806 if (unlikely(nvmeq->qid == 0 &&
807 cqe->command_id >= NVME_AQ_BLKMQ_DEPTH)) {
808 nvme_complete_async_event(&nvmeq->dev->ctrl,
809 cqe->status, &cqe->result);
810 return;
811 }
812
Keith Busche9d8a0f2017-08-17 16:45:06 -0400813 nvmeq->cqe_seen = 1;
Sagi Grimberg83a12fb2017-06-18 17:28:08 +0300814 req = blk_mq_tag_to_rq(*nvmeq->tags, cqe->command_id);
815 nvme_end_request(req, cqe->status, cqe->result);
816}
817
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300818static inline bool nvme_read_cqe(struct nvme_queue *nvmeq,
819 struct nvme_completion *cqe)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500820{
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300821 if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase)) {
822 *cqe = nvmeq->cqes[nvmeq->cq_head];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500823
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300824 if (++nvmeq->cq_head == nvmeq->q_depth) {
825 nvmeq->cq_head = 0;
826 nvmeq->cq_phase = !nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500827 }
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300828 return true;
829 }
830 return false;
Jens Axboea0fa9642015-11-03 20:37:26 -0700831}
832
833static void nvme_process_cq(struct nvme_queue *nvmeq)
834{
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300835 struct nvme_completion cqe;
836 int consumed = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500837
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300838 while (nvme_read_cqe(nvmeq, &cqe)) {
Sagi Grimberg83a12fb2017-06-18 17:28:08 +0300839 nvme_handle_cqe(nvmeq, &cqe);
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300840 consumed++;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500841 }
842
Keith Busche9d8a0f2017-08-17 16:45:06 -0400843 if (consumed)
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300844 nvme_ring_cq_doorbell(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500845}
846
847static irqreturn_t nvme_irq(int irq, void *data)
848{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500849 irqreturn_t result;
850 struct nvme_queue *nvmeq = data;
851 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400852 nvme_process_cq(nvmeq);
853 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
854 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500855 spin_unlock(&nvmeq->q_lock);
856 return result;
857}
858
859static irqreturn_t nvme_irq_check(int irq, void *data)
860{
861 struct nvme_queue *nvmeq = data;
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100862 if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase))
863 return IRQ_WAKE_THREAD;
864 return IRQ_NONE;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500865}
866
Keith Busch7776db12017-02-24 17:59:28 -0500867static int __nvme_poll(struct nvme_queue *nvmeq, unsigned int tag)
Jens Axboea0fa9642015-11-03 20:37:26 -0700868{
Sagi Grimberg442e19b2017-06-18 17:28:10 +0300869 struct nvme_completion cqe;
870 int found = 0, consumed = 0;
Jens Axboea0fa9642015-11-03 20:37:26 -0700871
Sagi Grimberg442e19b2017-06-18 17:28:10 +0300872 if (!nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase))
873 return 0;
Jens Axboea0fa9642015-11-03 20:37:26 -0700874
Sagi Grimberg442e19b2017-06-18 17:28:10 +0300875 spin_lock_irq(&nvmeq->q_lock);
876 while (nvme_read_cqe(nvmeq, &cqe)) {
877 nvme_handle_cqe(nvmeq, &cqe);
878 consumed++;
879
880 if (tag == cqe.command_id) {
881 found = 1;
882 break;
883 }
884 }
885
886 if (consumed)
887 nvme_ring_cq_doorbell(nvmeq);
888 spin_unlock_irq(&nvmeq->q_lock);
889
890 return found;
Jens Axboea0fa9642015-11-03 20:37:26 -0700891}
892
Keith Busch7776db12017-02-24 17:59:28 -0500893static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
894{
895 struct nvme_queue *nvmeq = hctx->driver_data;
896
897 return __nvme_poll(nvmeq, tag);
898}
899
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200900static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl, int aer_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500901{
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200902 struct nvme_dev *dev = to_nvme_dev(ctrl);
Christoph Hellwig9396dec2016-02-29 15:59:44 +0100903 struct nvme_queue *nvmeq = dev->queues[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700904 struct nvme_command c;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700905
906 memset(&c, 0, sizeof(c));
907 c.common.opcode = nvme_admin_async_event;
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200908 c.common.command_id = NVME_AQ_BLKMQ_DEPTH + aer_idx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700909
Christoph Hellwig9396dec2016-02-29 15:59:44 +0100910 spin_lock_irq(&nvmeq->q_lock);
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200911 __nvme_submit_cmd(nvmeq, &c);
Christoph Hellwig9396dec2016-02-29 15:59:44 +0100912 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -0700913}
914
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500915static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
916{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500917 struct nvme_command c;
918
919 memset(&c, 0, sizeof(c));
920 c.delete_queue.opcode = opcode;
921 c.delete_queue.qid = cpu_to_le16(id);
922
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100923 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500924}
925
926static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
927 struct nvme_queue *nvmeq)
928{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500929 struct nvme_command c;
930 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
931
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200932 /*
Minwoo Im16772ae2017-10-18 22:56:09 +0900933 * Note: we (ab)use the fact that the prp fields survive if no data
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200934 * is attached to the request.
935 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500936 memset(&c, 0, sizeof(c));
937 c.create_cq.opcode = nvme_admin_create_cq;
938 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
939 c.create_cq.cqid = cpu_to_le16(qid);
940 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
941 c.create_cq.cq_flags = cpu_to_le16(flags);
942 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
943
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100944 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500945}
946
947static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
948 struct nvme_queue *nvmeq)
949{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500950 struct nvme_command c;
Keith Busch81c1cd92017-04-04 18:18:12 -0400951 int flags = NVME_QUEUE_PHYS_CONTIG;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500952
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200953 /*
Minwoo Im16772ae2017-10-18 22:56:09 +0900954 * Note: we (ab)use the fact that the prp fields survive if no data
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200955 * is attached to the request.
956 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500957 memset(&c, 0, sizeof(c));
958 c.create_sq.opcode = nvme_admin_create_sq;
959 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
960 c.create_sq.sqid = cpu_to_le16(qid);
961 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
962 c.create_sq.sq_flags = cpu_to_le16(flags);
963 c.create_sq.cqid = cpu_to_le16(qid);
964
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100965 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500966}
967
968static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
969{
970 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
971}
972
973static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
974{
975 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
976}
977
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200978static void abort_endio(struct request *req, blk_status_t error)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400979{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100980 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
981 struct nvme_queue *nvmeq = iod->nvmeq;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400982
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200983 dev_warn(nvmeq->dev->ctrl.device,
984 "Abort status: 0x%x", nvme_req(req)->status);
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100985 atomic_inc(&nvmeq->dev->ctrl.abort_limit);
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100986 blk_mq_free_request(req);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200987}
988
Keith Buschb2a0eb12017-06-07 20:32:50 +0200989static bool nvme_should_reset(struct nvme_dev *dev, u32 csts)
990{
991
992 /* If true, indicates loss of adapter communication, possibly by a
993 * NVMe Subsystem reset.
994 */
995 bool nssro = dev->subsystem && (csts & NVME_CSTS_NSSRO);
996
997 /* If there is a reset ongoing, we shouldn't reset again. */
998 if (dev->ctrl.state == NVME_CTRL_RESETTING)
999 return false;
1000
1001 /* We shouldn't reset unless the controller is on fatal error state
1002 * _or_ if we lost the communication with it.
1003 */
1004 if (!(csts & NVME_CSTS_CFS) && !nssro)
1005 return false;
1006
1007 /* If PCI error recovery process is happening, we cannot reset or
1008 * the recovery mechanism will surely fail.
1009 */
1010 if (pci_channel_offline(to_pci_dev(dev->dev)))
1011 return false;
1012
1013 return true;
1014}
1015
1016static void nvme_warn_reset(struct nvme_dev *dev, u32 csts)
1017{
1018 /* Read a config register to help see what died. */
1019 u16 pci_status;
1020 int result;
1021
1022 result = pci_read_config_word(to_pci_dev(dev->dev), PCI_STATUS,
1023 &pci_status);
1024 if (result == PCIBIOS_SUCCESSFUL)
1025 dev_warn(dev->ctrl.device,
1026 "controller is down; will reset: CSTS=0x%x, PCI_STATUS=0x%hx\n",
1027 csts, pci_status);
1028 else
1029 dev_warn(dev->ctrl.device,
1030 "controller is down; will reset: CSTS=0x%x, PCI_STATUS read failed (%d)\n",
1031 csts, result);
1032}
1033
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001034static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001035{
Christoph Hellwigf4800d62015-11-28 15:43:10 +01001036 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
1037 struct nvme_queue *nvmeq = iod->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001038 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001039 struct request *abort_req;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001040 struct nvme_command cmd;
Keith Buschb2a0eb12017-06-07 20:32:50 +02001041 u32 csts = readl(dev->bar + NVME_REG_CSTS);
1042
1043 /*
1044 * Reset immediately if the controller is failed
1045 */
1046 if (nvme_should_reset(dev, csts)) {
1047 nvme_warn_reset(dev, csts);
1048 nvme_dev_disable(dev, false);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001049 nvme_reset_ctrl(&dev->ctrl);
Keith Buschb2a0eb12017-06-07 20:32:50 +02001050 return BLK_EH_HANDLED;
1051 }
Keith Buschc30341d2013-12-10 13:10:38 -07001052
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001053 /*
Keith Busch7776db12017-02-24 17:59:28 -05001054 * Did we miss an interrupt?
1055 */
1056 if (__nvme_poll(nvmeq, req->tag)) {
1057 dev_warn(dev->ctrl.device,
1058 "I/O %d QID %d timeout, completion polled\n",
1059 req->tag, nvmeq->qid);
1060 return BLK_EH_HANDLED;
1061 }
1062
1063 /*
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001064 * Shutdown immediately if controller times out while starting. The
1065 * reset work will see the pci device disabled when it gets the forced
1066 * cancellation error. All outstanding requests are completed on
1067 * shutdown, so we return BLK_EH_HANDLED.
1068 */
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02001069 if (dev->ctrl.state == NVME_CTRL_RESETTING) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001070 dev_warn(dev->ctrl.device,
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001071 "I/O %d QID %d timeout, disable controller\n",
1072 req->tag, nvmeq->qid);
Keith Buscha5cdb682016-01-12 14:41:18 -07001073 nvme_dev_disable(dev, false);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001074 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001075 return BLK_EH_HANDLED;
Keith Buschc30341d2013-12-10 13:10:38 -07001076 }
1077
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001078 /*
1079 * Shutdown the controller immediately and schedule a reset if the
1080 * command was already aborted once before and still hasn't been
1081 * returned to the driver, or if this is the admin queue.
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001082 */
Christoph Hellwigf4800d62015-11-28 15:43:10 +01001083 if (!nvmeq->qid || iod->aborted) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001084 dev_warn(dev->ctrl.device,
Keith Busche1569a12015-11-26 12:11:07 +01001085 "I/O %d QID %d timeout, reset controller\n",
1086 req->tag, nvmeq->qid);
Keith Buscha5cdb682016-01-12 14:41:18 -07001087 nvme_dev_disable(dev, false);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001088 nvme_reset_ctrl(&dev->ctrl);
Keith Buschc30341d2013-12-10 13:10:38 -07001089
Keith Busche1569a12015-11-26 12:11:07 +01001090 /*
1091 * Mark the request as handled, since the inline shutdown
1092 * forces all outstanding requests to complete.
1093 */
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001094 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
Keith Busche1569a12015-11-26 12:11:07 +01001095 return BLK_EH_HANDLED;
Keith Buschc30341d2013-12-10 13:10:38 -07001096 }
Keith Buschc30341d2013-12-10 13:10:38 -07001097
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001098 if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) {
1099 atomic_inc(&dev->ctrl.abort_limit);
1100 return BLK_EH_RESET_TIMER;
1101 }
Keith Busch7bf7d772017-01-24 18:07:00 -05001102 iod->aborted = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001103
Keith Buschc30341d2013-12-10 13:10:38 -07001104 memset(&cmd, 0, sizeof(cmd));
1105 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001106 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001107 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Keith Buschc30341d2013-12-10 13:10:38 -07001108
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001109 dev_warn(nvmeq->dev->ctrl.device,
1110 "I/O %d QID %d timeout, aborting\n",
1111 req->tag, nvmeq->qid);
Keith Buschc30341d2013-12-10 13:10:38 -07001112
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001113 abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd,
Christoph Hellwigeb71f432016-06-13 16:45:23 +02001114 BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001115 if (IS_ERR(abort_req)) {
1116 atomic_inc(&dev->ctrl.abort_limit);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001117 return BLK_EH_RESET_TIMER;
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001118 }
Keith Buschc30341d2013-12-10 13:10:38 -07001119
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001120 abort_req->timeout = ADMIN_TIMEOUT;
1121 abort_req->end_io_data = NULL;
1122 blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio);
Keith Busch07836e62015-02-19 10:34:48 -07001123
Keith Busch7a509a62015-01-07 18:55:53 -07001124 /*
1125 * The aborted req will be completed on receiving the abort req.
1126 * We enable the timer again. If hit twice, it'll cause a device reset,
1127 * as the device then is in a faulty state.
1128 */
Keith Busch07836e62015-02-19 10:34:48 -07001129 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001130}
1131
Keith Buschf435c282014-07-07 09:14:42 -06001132static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001133{
1134 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1135 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001136 if (nvmeq->sq_cmds)
1137 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001138 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1139 kfree(nvmeq);
1140}
1141
Keith Buscha1a5ef92013-12-16 13:50:00 -05001142static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001143{
1144 int i;
1145
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001146 for (i = dev->ctrl.queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001147 struct nvme_queue *nvmeq = dev->queues[i];
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001148 dev->ctrl.queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001149 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001150 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001151 }
Keith Busch22404272013-07-15 15:02:20 -06001152}
1153
Keith Busch4d115422013-12-10 13:10:40 -07001154/**
1155 * nvme_suspend_queue - put queue into suspended state
1156 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001157 */
1158static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001159{
Keith Busch2b25d982014-12-22 12:59:04 -07001160 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001161
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001162 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001163 if (nvmeq->cq_vector == -1) {
1164 spin_unlock_irq(&nvmeq->q_lock);
1165 return 1;
1166 }
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001167 vector = nvmeq->cq_vector;
Keith Busch42f61422014-03-24 10:46:25 -06001168 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001169 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001170 spin_unlock_irq(&nvmeq->q_lock);
1171
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001172 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
Sagi Grimbergc81545f2017-07-02 15:53:27 +03001173 blk_mq_quiesce_queue(nvmeq->dev->ctrl.admin_q);
Keith Busch6df3dbc2015-03-26 13:49:33 -06001174
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001175 pci_free_irq(to_pci_dev(nvmeq->dev->dev), vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001176
Keith Busch4d115422013-12-10 13:10:40 -07001177 return 0;
1178}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001179
Keith Buscha5cdb682016-01-12 14:41:18 -07001180static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown)
Keith Busch4d115422013-12-10 13:10:40 -07001181{
Keith Buscha5cdb682016-01-12 14:41:18 -07001182 struct nvme_queue *nvmeq = dev->queues[0];
Keith Busch4d115422013-12-10 13:10:40 -07001183
1184 if (!nvmeq)
1185 return;
1186 if (nvme_suspend_queue(nvmeq))
1187 return;
1188
Keith Buscha5cdb682016-01-12 14:41:18 -07001189 if (shutdown)
1190 nvme_shutdown_ctrl(&dev->ctrl);
1191 else
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001192 nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
Keith Busch07836e62015-02-19 10:34:48 -07001193
1194 spin_lock_irq(&nvmeq->q_lock);
1195 nvme_process_cq(nvmeq);
1196 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001197}
1198
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001199static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1200 int entry_size)
1201{
1202 int q_depth = dev->q_depth;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001203 unsigned q_size_aligned = roundup(q_depth * entry_size,
1204 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001205
1206 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001207 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001208 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
Jon Derrickc45f5c92015-07-21 15:08:13 -06001209 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001210
1211 /*
1212 * Ensure the reduced q_depth is above some threshold where it
1213 * would be better to map queues in system memory with the
1214 * original depth
1215 */
1216 if (q_depth < 64)
1217 return -ENOMEM;
1218 }
1219
1220 return q_depth;
1221}
1222
1223static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1224 int qid, int depth)
1225{
1226 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001227 unsigned offset = (qid - 1) * roundup(SQ_SIZE(depth),
1228 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001229 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1230 nvmeq->sq_cmds_io = dev->cmb + offset;
1231 } else {
1232 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1233 &nvmeq->sq_dma_addr, GFP_KERNEL);
1234 if (!nvmeq->sq_cmds)
1235 return -ENOMEM;
1236 }
1237
1238 return 0;
1239}
1240
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001241static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001242 int depth, int node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001243{
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001244 struct nvme_queue *nvmeq = kzalloc_node(sizeof(*nvmeq), GFP_KERNEL,
1245 node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001246 if (!nvmeq)
1247 return NULL;
1248
Christoph Hellwige75ec752015-05-22 11:12:39 +02001249 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001250 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001251 if (!nvmeq->cqes)
1252 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001253
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001254 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001255 goto free_cqdma;
1256
Christoph Hellwige75ec752015-05-22 11:12:39 +02001257 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001258 nvmeq->dev = dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001259 spin_lock_init(&nvmeq->q_lock);
1260 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001261 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001262 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001263 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001264 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001265 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001266 dev->queues[qid] = nvmeq;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001267 dev->ctrl.queue_count++;
Jon Derrick36a7e992015-05-27 12:26:23 -06001268
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001269 return nvmeq;
1270
1271 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001272 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001273 nvmeq->cq_dma_addr);
1274 free_nvmeq:
1275 kfree(nvmeq);
1276 return NULL;
1277}
1278
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001279static int queue_request_irq(struct nvme_queue *nvmeq)
Matthew Wilcox30010822011-01-20 09:10:15 -05001280{
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001281 struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev);
1282 int nr = nvmeq->dev->ctrl.instance;
1283
1284 if (use_threaded_interrupts) {
1285 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq_check,
1286 nvme_irq, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1287 } else {
1288 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq,
1289 NULL, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1290 }
Matthew Wilcox30010822011-01-20 09:10:15 -05001291}
1292
Keith Busch22404272013-07-15 15:02:20 -06001293static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001294{
Keith Busch22404272013-07-15 15:02:20 -06001295 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001296
Keith Busch7be50e92014-09-10 15:48:47 -06001297 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001298 nvmeq->sq_tail = 0;
1299 nvmeq->cq_head = 0;
1300 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001301 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001302 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Helen Koikef9f38e32017-04-10 12:51:07 -03001303 nvme_dbbuf_init(dev, nvmeq, qid);
Keith Busch42f61422014-03-24 10:46:25 -06001304 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001305 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001306}
1307
1308static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1309{
1310 struct nvme_dev *dev = nvmeq->dev;
1311 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001312
Keith Busch2b25d982014-12-22 12:59:04 -07001313 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001314 result = adapter_alloc_cq(dev, qid, nvmeq);
1315 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001316 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001317
1318 result = adapter_alloc_sq(dev, qid, nvmeq);
1319 if (result < 0)
1320 goto release_cq;
1321
Keith Busch161b8be2017-09-14 13:54:39 -04001322 nvme_init_queue(nvmeq, qid);
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001323 result = queue_request_irq(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001324 if (result < 0)
1325 goto release_sq;
1326
Keith Busch22404272013-07-15 15:02:20 -06001327 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001328
1329 release_sq:
1330 adapter_delete_sq(dev, qid);
1331 release_cq:
1332 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001333 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001334}
1335
Eric Biggersf363b082017-03-30 13:39:16 -07001336static const struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001337 .queue_rq = nvme_queue_rq,
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001338 .complete = nvme_pci_complete_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001339 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001340 .exit_hctx = nvme_admin_exit_hctx,
Christoph Hellwig03508152017-06-13 09:15:18 +02001341 .init_request = nvme_init_request,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001342 .timeout = nvme_timeout,
1343};
1344
Eric Biggersf363b082017-03-30 13:39:16 -07001345static const struct blk_mq_ops nvme_mq_ops = {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001346 .queue_rq = nvme_queue_rq,
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001347 .complete = nvme_pci_complete_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001348 .init_hctx = nvme_init_hctx,
1349 .init_request = nvme_init_request,
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001350 .map_queues = nvme_pci_map_queues,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001351 .timeout = nvme_timeout,
Jens Axboea0fa9642015-11-03 20:37:26 -07001352 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001353};
1354
Keith Buschea191d22015-01-07 18:55:49 -07001355static void nvme_dev_remove_admin(struct nvme_dev *dev)
1356{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001357 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
Keith Busch69d9a992016-02-24 09:15:56 -07001358 /*
1359 * If the controller was reset during removal, it's possible
1360 * user requests may be waiting on a stopped queue. Start the
1361 * queue to flush these to completion.
1362 */
Sagi Grimbergc81545f2017-07-02 15:53:27 +03001363 blk_mq_unquiesce_queue(dev->ctrl.admin_q);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001364 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001365 blk_mq_free_tag_set(&dev->admin_tagset);
1366 }
1367}
1368
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001369static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1370{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001371 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001372 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1373 dev->admin_tagset.nr_hw_queues = 1;
Keith Busche3e9d502016-01-04 09:10:55 -07001374
1375 /*
1376 * Subtract one to leave an empty queue entry for 'Full Queue'
1377 * condition. See NVM-Express 1.2 specification, section 4.1.2.
1378 */
1379 dev->admin_tagset.queue_depth = NVME_AQ_BLKMQ_DEPTH - 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001380 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001381 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001382 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Jens Axboed3484992017-01-13 14:43:58 -07001383 dev->admin_tagset.flags = BLK_MQ_F_NO_SCHED;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001384 dev->admin_tagset.driver_data = dev;
1385
1386 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1387 return -ENOMEM;
Sagi Grimberg34b6c232017-07-10 09:22:29 +03001388 dev->ctrl.admin_tagset = &dev->admin_tagset;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001389
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001390 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1391 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001392 blk_mq_free_tag_set(&dev->admin_tagset);
1393 return -ENOMEM;
1394 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001395 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001396 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001397 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001398 return -ENODEV;
1399 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001400 } else
Sagi Grimbergc81545f2017-07-02 15:53:27 +03001401 blk_mq_unquiesce_queue(dev->ctrl.admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001402
1403 return 0;
1404}
1405
Xu Yu97f6ef62017-05-24 16:39:55 +08001406static unsigned long db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1407{
1408 return NVME_REG_DBS + ((nr_io_queues + 1) * 8 * dev->db_stride);
1409}
1410
1411static int nvme_remap_bar(struct nvme_dev *dev, unsigned long size)
1412{
1413 struct pci_dev *pdev = to_pci_dev(dev->dev);
1414
1415 if (size <= dev->bar_mapped_size)
1416 return 0;
1417 if (size > pci_resource_len(pdev, 0))
1418 return -ENOMEM;
1419 if (dev->bar)
1420 iounmap(dev->bar);
1421 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1422 if (!dev->bar) {
1423 dev->bar_mapped_size = 0;
1424 return -ENOMEM;
1425 }
1426 dev->bar_mapped_size = size;
1427 dev->dbs = dev->bar + NVME_REG_DBS;
1428
1429 return 0;
1430}
1431
Sagi Grimberg01ad0992017-05-01 00:27:17 +03001432static int nvme_pci_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001433{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001434 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001435 u32 aqa;
1436 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001437
Xu Yu97f6ef62017-05-24 16:39:55 +08001438 result = nvme_remap_bar(dev, db_bar_size(dev, 0));
1439 if (result < 0)
1440 return result;
1441
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001442 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1, 0) ?
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001443 NVME_CAP_NSSRC(dev->ctrl.cap) : 0;
Keith Buschdfbac8c2015-08-10 15:20:40 -06001444
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001445 if (dev->subsystem &&
1446 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1447 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001448
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001449 result = nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001450 if (result < 0)
1451 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001452
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001453 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001454 if (!nvmeq) {
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001455 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH,
1456 dev_to_node(dev->dev));
Keith Buschcd638942013-07-15 15:02:23 -06001457 if (!nvmeq)
1458 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001459 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001460
1461 aqa = nvmeq->q_depth - 1;
1462 aqa |= aqa << 16;
1463
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001464 writel(aqa, dev->bar + NVME_REG_AQA);
1465 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1466 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Keith Busch1d090622014-06-23 11:34:01 -06001467
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001468 result = nvme_enable_ctrl(&dev->ctrl, dev->ctrl.cap);
Keith Busch025c5572013-05-01 13:07:51 -06001469 if (result)
Keith Buschd4875622016-11-15 15:56:26 -05001470 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001471
Keith Busch2b25d982014-12-22 12:59:04 -07001472 nvmeq->cq_vector = 0;
Keith Busch161b8be2017-09-14 13:54:39 -04001473 nvme_init_queue(nvmeq, 0);
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001474 result = queue_request_irq(nvmeq);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001475 if (result) {
1476 nvmeq->cq_vector = -1;
Keith Buschd4875622016-11-15 15:56:26 -05001477 return result;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001478 }
Keith Busch025c5572013-05-01 13:07:51 -06001479
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001480 return result;
1481}
1482
Christoph Hellwig749941f2015-11-26 11:46:39 +01001483static int nvme_create_io_queues(struct nvme_dev *dev)
Keith Busch42f61422014-03-24 10:46:25 -06001484{
Keith Busch949928c2015-12-17 17:08:15 -07001485 unsigned i, max;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001486 int ret = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001487
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001488 for (i = dev->ctrl.queue_count; i <= dev->max_qid; i++) {
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001489 /* vector == qid - 1, match nvme_create_queue */
1490 if (!nvme_alloc_queue(dev, i, dev->q_depth,
1491 pci_irq_get_node(to_pci_dev(dev->dev), i - 1))) {
Christoph Hellwig749941f2015-11-26 11:46:39 +01001492 ret = -ENOMEM;
Keith Busch42f61422014-03-24 10:46:25 -06001493 break;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001494 }
1495 }
Keith Busch42f61422014-03-24 10:46:25 -06001496
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001497 max = min(dev->max_qid, dev->ctrl.queue_count - 1);
Keith Busch949928c2015-12-17 17:08:15 -07001498 for (i = dev->online_queues; i <= max; i++) {
Christoph Hellwig749941f2015-11-26 11:46:39 +01001499 ret = nvme_create_queue(dev->queues[i], i);
Keith Buschd4875622016-11-15 15:56:26 -05001500 if (ret)
Keith Busch42f61422014-03-24 10:46:25 -06001501 break;
Matthew Wilcox27e81662014-04-11 11:58:45 -04001502 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001503
1504 /*
1505 * Ignore failing Create SQ/CQ commands, we can continue with less
1506 * than the desired aount of queues, and even a controller without
1507 * I/O queues an still be used to issue admin commands. This might
1508 * be useful to upgrade a buggy firmware for example.
1509 */
1510 return ret >= 0 ? 0 : ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001511}
1512
Stephen Bates202021c2016-10-05 20:01:12 -06001513static ssize_t nvme_cmb_show(struct device *dev,
1514 struct device_attribute *attr,
1515 char *buf)
1516{
1517 struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
1518
Stephen Batesc9658092016-12-16 11:54:50 -07001519 return scnprintf(buf, PAGE_SIZE, "cmbloc : x%08x\ncmbsz : x%08x\n",
Stephen Bates202021c2016-10-05 20:01:12 -06001520 ndev->cmbloc, ndev->cmbsz);
1521}
1522static DEVICE_ATTR(cmb, S_IRUGO, nvme_cmb_show, NULL);
1523
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001524static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1525{
1526 u64 szu, size, offset;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001527 resource_size_t bar_size;
1528 struct pci_dev *pdev = to_pci_dev(dev->dev);
1529 void __iomem *cmb;
1530 dma_addr_t dma_addr;
1531
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001532 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001533 if (!(NVME_CMB_SZ(dev->cmbsz)))
1534 return NULL;
Stephen Bates202021c2016-10-05 20:01:12 -06001535 dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001536
Stephen Bates202021c2016-10-05 20:01:12 -06001537 if (!use_cmb_sqes)
1538 return NULL;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001539
1540 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1541 size = szu * NVME_CMB_SZ(dev->cmbsz);
Stephen Bates202021c2016-10-05 20:01:12 -06001542 offset = szu * NVME_CMB_OFST(dev->cmbloc);
1543 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(dev->cmbloc));
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001544
1545 if (offset > bar_size)
1546 return NULL;
1547
1548 /*
1549 * Controllers may support a CMB size larger than their BAR,
1550 * for example, due to being behind a bridge. Reduce the CMB to
1551 * the reported size of the BAR
1552 */
1553 if (size > bar_size - offset)
1554 size = bar_size - offset;
1555
Stephen Bates202021c2016-10-05 20:01:12 -06001556 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(dev->cmbloc)) + offset;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001557 cmb = ioremap_wc(dma_addr, size);
1558 if (!cmb)
1559 return NULL;
1560
1561 dev->cmb_dma_addr = dma_addr;
1562 dev->cmb_size = size;
1563 return cmb;
1564}
1565
1566static inline void nvme_release_cmb(struct nvme_dev *dev)
1567{
1568 if (dev->cmb) {
1569 iounmap(dev->cmb);
1570 dev->cmb = NULL;
Max Gurtovoy1c78f772017-07-30 01:45:08 +03001571 sysfs_remove_file_from_group(&dev->ctrl.device->kobj,
1572 &dev_attr_cmb.attr, NULL);
1573 dev->cmbsz = 0;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001574 }
1575}
1576
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001577static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits)
Keith Busch9d713c22013-07-15 15:02:24 -06001578{
Christoph Hellwig4033f352017-08-28 10:47:18 +02001579 u64 dma_addr = dev->host_mem_descs_dma;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001580 struct nvme_command c;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001581 int ret;
1582
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001583 memset(&c, 0, sizeof(c));
1584 c.features.opcode = nvme_admin_set_features;
1585 c.features.fid = cpu_to_le32(NVME_FEAT_HOST_MEM_BUF);
1586 c.features.dword11 = cpu_to_le32(bits);
1587 c.features.dword12 = cpu_to_le32(dev->host_mem_size >>
1588 ilog2(dev->ctrl.page_size));
1589 c.features.dword13 = cpu_to_le32(lower_32_bits(dma_addr));
1590 c.features.dword14 = cpu_to_le32(upper_32_bits(dma_addr));
1591 c.features.dword15 = cpu_to_le32(dev->nr_host_mem_descs);
1592
1593 ret = nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1594 if (ret) {
1595 dev_warn(dev->ctrl.device,
1596 "failed to set host mem (err %d, flags %#x).\n",
1597 ret, bits);
1598 }
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001599 return ret;
1600}
1601
1602static void nvme_free_host_mem(struct nvme_dev *dev)
1603{
1604 int i;
1605
1606 for (i = 0; i < dev->nr_host_mem_descs; i++) {
1607 struct nvme_host_mem_buf_desc *desc = &dev->host_mem_descs[i];
1608 size_t size = le32_to_cpu(desc->size) * dev->ctrl.page_size;
1609
1610 dma_free_coherent(dev->dev, size, dev->host_mem_desc_bufs[i],
1611 le64_to_cpu(desc->addr));
1612 }
1613
1614 kfree(dev->host_mem_desc_bufs);
1615 dev->host_mem_desc_bufs = NULL;
Christoph Hellwig4033f352017-08-28 10:47:18 +02001616 dma_free_coherent(dev->dev,
1617 dev->nr_host_mem_descs * sizeof(*dev->host_mem_descs),
1618 dev->host_mem_descs, dev->host_mem_descs_dma);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001619 dev->host_mem_descs = NULL;
1620}
1621
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001622static int __nvme_alloc_host_mem(struct nvme_dev *dev, u64 preferred,
1623 u32 chunk_size)
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001624{
1625 struct nvme_host_mem_buf_desc *descs;
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001626 u32 max_entries, len;
Christoph Hellwig4033f352017-08-28 10:47:18 +02001627 dma_addr_t descs_dma;
Dan Carpenter2ee0e4e2017-07-06 12:26:52 +03001628 int i = 0;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001629 void **bufs;
Dan Carpenter2ee0e4e2017-07-06 12:26:52 +03001630 u64 size = 0, tmp;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001631
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001632 tmp = (preferred + chunk_size - 1);
1633 do_div(tmp, chunk_size);
1634 max_entries = tmp;
Christoph Hellwig044a9df2017-09-11 12:09:28 -04001635
1636 if (dev->ctrl.hmmaxd && dev->ctrl.hmmaxd < max_entries)
1637 max_entries = dev->ctrl.hmmaxd;
1638
Christoph Hellwig4033f352017-08-28 10:47:18 +02001639 descs = dma_zalloc_coherent(dev->dev, max_entries * sizeof(*descs),
1640 &descs_dma, GFP_KERNEL);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001641 if (!descs)
1642 goto out;
1643
1644 bufs = kcalloc(max_entries, sizeof(*bufs), GFP_KERNEL);
1645 if (!bufs)
1646 goto out_free_descs;
1647
Christoph Hellwig50cdb7c2017-07-25 17:39:07 +02001648 for (size = 0; size < preferred; size += len) {
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001649 dma_addr_t dma_addr;
1650
Christoph Hellwig50cdb7c2017-07-25 17:39:07 +02001651 len = min_t(u64, chunk_size, preferred - size);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001652 bufs[i] = dma_alloc_attrs(dev->dev, len, &dma_addr, GFP_KERNEL,
1653 DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
1654 if (!bufs[i])
1655 break;
1656
1657 descs[i].addr = cpu_to_le64(dma_addr);
1658 descs[i].size = cpu_to_le32(len / dev->ctrl.page_size);
1659 i++;
1660 }
1661
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001662 if (!size)
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001663 goto out_free_bufs;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001664
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001665 dev->nr_host_mem_descs = i;
1666 dev->host_mem_size = size;
1667 dev->host_mem_descs = descs;
Christoph Hellwig4033f352017-08-28 10:47:18 +02001668 dev->host_mem_descs_dma = descs_dma;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001669 dev->host_mem_desc_bufs = bufs;
1670 return 0;
1671
1672out_free_bufs:
1673 while (--i >= 0) {
1674 size_t size = le32_to_cpu(descs[i].size) * dev->ctrl.page_size;
1675
1676 dma_free_coherent(dev->dev, size, bufs[i],
1677 le64_to_cpu(descs[i].addr));
1678 }
1679
1680 kfree(bufs);
1681out_free_descs:
Christoph Hellwig4033f352017-08-28 10:47:18 +02001682 dma_free_coherent(dev->dev, max_entries * sizeof(*descs), descs,
1683 descs_dma);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001684out:
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001685 dev->host_mem_descs = NULL;
1686 return -ENOMEM;
1687}
1688
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001689static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
1690{
1691 u32 chunk_size;
1692
1693 /* start big and work our way down */
Akinobu Mita30f92d62017-09-06 12:15:31 +02001694 for (chunk_size = min_t(u64, preferred, PAGE_SIZE * MAX_ORDER_NR_PAGES);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04001695 chunk_size >= max_t(u32, dev->ctrl.hmminds * 4096, PAGE_SIZE * 2);
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001696 chunk_size /= 2) {
1697 if (!__nvme_alloc_host_mem(dev, preferred, chunk_size)) {
1698 if (!min || dev->host_mem_size >= min)
1699 return 0;
1700 nvme_free_host_mem(dev);
1701 }
1702 }
1703
1704 return -ENOMEM;
1705}
1706
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02001707static int nvme_setup_host_mem(struct nvme_dev *dev)
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001708{
1709 u64 max = (u64)max_host_mem_size_mb * SZ_1M;
1710 u64 preferred = (u64)dev->ctrl.hmpre * 4096;
1711 u64 min = (u64)dev->ctrl.hmmin * 4096;
1712 u32 enable_bits = NVME_HOST_MEM_ENABLE;
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02001713 int ret = 0;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001714
1715 preferred = min(preferred, max);
1716 if (min > max) {
1717 dev_warn(dev->ctrl.device,
1718 "min host memory (%lld MiB) above limit (%d MiB).\n",
1719 min >> ilog2(SZ_1M), max_host_mem_size_mb);
1720 nvme_free_host_mem(dev);
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02001721 return 0;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001722 }
1723
1724 /*
1725 * If we already have a buffer allocated check if we can reuse it.
1726 */
1727 if (dev->host_mem_descs) {
1728 if (dev->host_mem_size >= min)
1729 enable_bits |= NVME_HOST_MEM_RETURN;
1730 else
1731 nvme_free_host_mem(dev);
1732 }
1733
1734 if (!dev->host_mem_descs) {
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001735 if (nvme_alloc_host_mem(dev, min, preferred)) {
1736 dev_warn(dev->ctrl.device,
1737 "failed to allocate host memory buffer.\n");
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02001738 return 0; /* controller must work without HMB */
Christoph Hellwig92dc6892017-09-11 12:08:43 -04001739 }
1740
1741 dev_info(dev->ctrl.device,
1742 "allocated %lld MiB host memory buffer.\n",
1743 dev->host_mem_size >> ilog2(SZ_1M));
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001744 }
1745
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02001746 ret = nvme_set_host_mem(dev, enable_bits);
1747 if (ret)
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001748 nvme_free_host_mem(dev);
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02001749 return ret;
Keith Busch9d713c22013-07-15 15:02:24 -06001750}
1751
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001752static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001753{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001754 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02001755 struct pci_dev *pdev = to_pci_dev(dev->dev);
Xu Yu97f6ef62017-05-24 16:39:55 +08001756 int result, nr_io_queues;
1757 unsigned long size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001758
Christoph Hellwig425a17c2017-06-26 12:20:58 +02001759 nr_io_queues = num_present_cpus();
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001760 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
1761 if (result < 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001762 return result;
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001763
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001764 if (nr_io_queues == 0)
Keith Buscha5229052016-04-08 16:09:10 -06001765 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001766
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001767 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1768 result = nvme_cmb_qdepth(dev, nr_io_queues,
1769 sizeof(struct nvme_command));
1770 if (result > 0)
1771 dev->q_depth = result;
1772 else
1773 nvme_release_cmb(dev);
1774 }
1775
Xu Yu97f6ef62017-05-24 16:39:55 +08001776 do {
1777 size = db_bar_size(dev, nr_io_queues);
1778 result = nvme_remap_bar(dev, size);
1779 if (!result)
1780 break;
1781 if (!--nr_io_queues)
1782 return -ENOMEM;
1783 } while (1);
1784 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001785
Keith Busch9d713c22013-07-15 15:02:24 -06001786 /* Deregister the admin queue's interrupt */
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001787 pci_free_irq(pdev, 0, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001788
Jens Axboee32efbf2014-11-14 09:49:26 -07001789 /*
1790 * If we enable msix early due to not intx, disable it again before
1791 * setting up the full range we need.
1792 */
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001793 pci_free_irq_vectors(pdev);
1794 nr_io_queues = pci_alloc_irq_vectors(pdev, 1, nr_io_queues,
1795 PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY);
1796 if (nr_io_queues <= 0)
1797 return -EIO;
1798 dev->max_qid = nr_io_queues;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001799
Matthew Wilcox063a8092013-06-20 10:53:48 -04001800 /*
1801 * Should investigate if there's a performance win from allocating
1802 * more queues than interrupt vectors; it might allow the submission
1803 * path to scale better, even if the receive path is limited by the
1804 * number of interrupts.
1805 */
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001806
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001807 result = queue_request_irq(adminq);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001808 if (result) {
1809 adminq->cq_vector = -1;
Keith Buschd4875622016-11-15 15:56:26 -05001810 return result;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001811 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001812 return nvme_create_io_queues(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001813}
1814
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001815static void nvme_del_queue_end(struct request *req, blk_status_t error)
Keith Buschdb3cbff2016-01-12 14:41:17 -07001816{
1817 struct nvme_queue *nvmeq = req->end_io_data;
1818
1819 blk_mq_free_request(req);
1820 complete(&nvmeq->dev->ioq_wait);
1821}
1822
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001823static void nvme_del_cq_end(struct request *req, blk_status_t error)
Keith Buschdb3cbff2016-01-12 14:41:17 -07001824{
1825 struct nvme_queue *nvmeq = req->end_io_data;
1826
1827 if (!error) {
1828 unsigned long flags;
1829
Ming Lin2e39e0f2016-04-05 10:32:04 -07001830 /*
1831 * We might be called with the AQ q_lock held
1832 * and the I/O queue q_lock should always
1833 * nest inside the AQ one.
1834 */
1835 spin_lock_irqsave_nested(&nvmeq->q_lock, flags,
1836 SINGLE_DEPTH_NESTING);
Keith Buschdb3cbff2016-01-12 14:41:17 -07001837 nvme_process_cq(nvmeq);
1838 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
1839 }
1840
1841 nvme_del_queue_end(req, error);
1842}
1843
1844static int nvme_delete_queue(struct nvme_queue *nvmeq, u8 opcode)
1845{
1846 struct request_queue *q = nvmeq->dev->ctrl.admin_q;
1847 struct request *req;
1848 struct nvme_command cmd;
1849
1850 memset(&cmd, 0, sizeof(cmd));
1851 cmd.delete_queue.opcode = opcode;
1852 cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid);
1853
Christoph Hellwigeb71f432016-06-13 16:45:23 +02001854 req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
Keith Buschdb3cbff2016-01-12 14:41:17 -07001855 if (IS_ERR(req))
1856 return PTR_ERR(req);
1857
1858 req->timeout = ADMIN_TIMEOUT;
1859 req->end_io_data = nvmeq;
1860
1861 blk_execute_rq_nowait(q, NULL, req, false,
1862 opcode == nvme_admin_delete_cq ?
1863 nvme_del_cq_end : nvme_del_queue_end);
1864 return 0;
1865}
1866
Keith Busch70659062016-10-12 09:22:16 -06001867static void nvme_disable_io_queues(struct nvme_dev *dev, int queues)
Keith Buschdb3cbff2016-01-12 14:41:17 -07001868{
Keith Busch70659062016-10-12 09:22:16 -06001869 int pass;
Keith Buschdb3cbff2016-01-12 14:41:17 -07001870 unsigned long timeout;
1871 u8 opcode = nvme_admin_delete_sq;
1872
1873 for (pass = 0; pass < 2; pass++) {
Keith Busch014a0d62016-05-06 11:50:52 -06001874 int sent = 0, i = queues;
Keith Buschdb3cbff2016-01-12 14:41:17 -07001875
1876 reinit_completion(&dev->ioq_wait);
1877 retry:
1878 timeout = ADMIN_TIMEOUT;
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06001879 for (; i > 0; i--, sent++)
1880 if (nvme_delete_queue(dev->queues[i], opcode))
Keith Buschdb3cbff2016-01-12 14:41:17 -07001881 break;
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06001882
Keith Buschdb3cbff2016-01-12 14:41:17 -07001883 while (sent--) {
1884 timeout = wait_for_completion_io_timeout(&dev->ioq_wait, timeout);
1885 if (timeout == 0)
1886 return;
1887 if (i)
1888 goto retry;
1889 }
1890 opcode = nvme_admin_delete_cq;
1891 }
1892}
1893
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001894/*
1895 * Return: error value if an error occurred setting up the queues or calling
1896 * Identify Device. 0 if these succeeded, even if adding some of the
1897 * namespaces failed. At the moment, these failures are silent. TBD which
1898 * failures should be reported.
1899 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001900static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001901{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001902 if (!dev->ctrl.tagset) {
Keith Buschffe77042015-06-08 10:08:15 -06001903 dev->tagset.ops = &nvme_mq_ops;
1904 dev->tagset.nr_hw_queues = dev->online_queues - 1;
1905 dev->tagset.timeout = NVME_IO_TIMEOUT;
1906 dev->tagset.numa_node = dev_to_node(dev->dev);
1907 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001908 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06001909 dev->tagset.cmd_size = nvme_cmd_size(dev);
1910 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
1911 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001912
Keith Buschffe77042015-06-08 10:08:15 -06001913 if (blk_mq_alloc_tag_set(&dev->tagset))
1914 return 0;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001915 dev->ctrl.tagset = &dev->tagset;
Helen Koikef9f38e32017-04-10 12:51:07 -03001916
1917 nvme_dbbuf_set(dev);
Keith Busch949928c2015-12-17 17:08:15 -07001918 } else {
1919 blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1);
1920
1921 /* Free previously allocated queues that are no longer usable */
1922 nvme_free_queues(dev, dev->online_queues);
Keith Buschffe77042015-06-08 10:08:15 -06001923 }
Keith Busch949928c2015-12-17 17:08:15 -07001924
Keith Busche1e5e562015-02-19 13:39:03 -07001925 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001926}
1927
Keith Buschb00a7262016-02-24 09:15:52 -07001928static int nvme_pci_enable(struct nvme_dev *dev)
Keith Busch0877cb02013-07-15 15:02:19 -06001929{
Keith Buschb00a7262016-02-24 09:15:52 -07001930 int result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001931 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001932
1933 if (pci_enable_device_mem(pdev))
1934 return result;
1935
Keith Busch0877cb02013-07-15 15:02:19 -06001936 pci_set_master(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001937
Christoph Hellwige75ec752015-05-22 11:12:39 +02001938 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
1939 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01001940 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06001941
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001942 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07001943 result = -ENODEV;
Keith Buschb00a7262016-02-24 09:15:52 -07001944 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07001945 }
Jens Axboee32efbf2014-11-14 09:49:26 -07001946
1947 /*
Keith Buscha5229052016-04-08 16:09:10 -06001948 * Some devices and/or platforms don't advertise or work with INTx
1949 * interrupts. Pre-enable a single MSIX or MSI vec for setup. We'll
1950 * adjust this later.
Jens Axboee32efbf2014-11-14 09:49:26 -07001951 */
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001952 result = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
1953 if (result < 0)
1954 return result;
Jens Axboee32efbf2014-11-14 09:49:26 -07001955
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001956 dev->ctrl.cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001957
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001958 dev->q_depth = min_t(int, NVME_CAP_MQES(dev->ctrl.cap) + 1,
weiping zhangb27c1e62017-07-10 16:46:59 +08001959 io_queue_depth);
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001960 dev->db_stride = 1 << NVME_CAP_STRIDE(dev->ctrl.cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001961 dev->dbs = dev->bar + 4096;
Stephan Günther1f390c12015-12-01 13:23:22 -07001962
1963 /*
1964 * Temporary fix for the Apple controller found in the MacBook8,1 and
1965 * some MacBook7,1 to avoid controller resets and data loss.
1966 */
1967 if (pdev->vendor == PCI_VENDOR_ID_APPLE && pdev->device == 0x2001) {
1968 dev->q_depth = 2;
Christoph Hellwig9bdcfb12017-05-20 15:14:43 +02001969 dev_warn(dev->ctrl.device, "detected Apple NVMe controller, "
1970 "set queue depth=%u to work around controller resets\n",
Stephan Günther1f390c12015-12-01 13:23:22 -07001971 dev->q_depth);
Martin K. Petersend554b5e2017-06-27 22:27:57 -04001972 } else if (pdev->vendor == PCI_VENDOR_ID_SAMSUNG &&
1973 (pdev->device == 0xa821 || pdev->device == 0xa822) &&
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001974 NVME_CAP_MQES(dev->ctrl.cap) == 0) {
Martin K. Petersend554b5e2017-06-27 22:27:57 -04001975 dev->q_depth = 64;
1976 dev_err(dev->ctrl.device, "detected PM1725 NVMe controller, "
1977 "set queue depth=%u\n", dev->q_depth);
Stephan Günther1f390c12015-12-01 13:23:22 -07001978 }
1979
Stephen Bates202021c2016-10-05 20:01:12 -06001980 /*
1981 * CMBs can currently only exist on >=1.2 PCIe devices. We only
Max Gurtovoy1c78f772017-07-30 01:45:08 +03001982 * populate sysfs if a CMB is implemented. Since nvme_dev_attrs_group
1983 * has no name we can pass NULL as final argument to
1984 * sysfs_add_file_to_group.
Stephen Bates202021c2016-10-05 20:01:12 -06001985 */
1986
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001987 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2, 0)) {
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001988 dev->cmb = nvme_map_cmb(dev);
Max Gurtovoy1c78f772017-07-30 01:45:08 +03001989 if (dev->cmb) {
Stephen Bates202021c2016-10-05 20:01:12 -06001990 if (sysfs_add_file_to_group(&dev->ctrl.device->kobj,
1991 &dev_attr_cmb.attr, NULL))
Christoph Hellwig9bdcfb12017-05-20 15:14:43 +02001992 dev_warn(dev->ctrl.device,
Stephen Bates202021c2016-10-05 20:01:12 -06001993 "failed to add sysfs attribute for CMB\n");
1994 }
1995 }
1996
Keith Buscha0a34082015-12-07 15:30:31 -07001997 pci_enable_pcie_error_reporting(pdev);
1998 pci_save_state(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001999 return 0;
2000
2001 disable:
Keith Busch0877cb02013-07-15 15:02:19 -06002002 pci_disable_device(pdev);
2003 return result;
2004}
2005
2006static void nvme_dev_unmap(struct nvme_dev *dev)
2007{
Keith Buschb00a7262016-02-24 09:15:52 -07002008 if (dev->bar)
2009 iounmap(dev->bar);
Johannes Thumshirna1f447b2016-06-07 09:44:02 +02002010 pci_release_mem_regions(to_pci_dev(dev->dev));
Keith Buschb00a7262016-02-24 09:15:52 -07002011}
2012
2013static void nvme_pci_disable(struct nvme_dev *dev)
2014{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002015 struct pci_dev *pdev = to_pci_dev(dev->dev);
2016
Jon Derrickf63572d2017-05-05 14:52:06 -06002017 nvme_release_cmb(dev);
Christoph Hellwigdca51e72016-09-14 16:18:57 +02002018 pci_free_irq_vectors(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06002019
Keith Buscha0a34082015-12-07 15:30:31 -07002020 if (pci_is_enabled(pdev)) {
2021 pci_disable_pcie_error_reporting(pdev);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002022 pci_disable_device(pdev);
Keith Busch4d115422013-12-10 13:10:40 -07002023 }
Keith Busch4d115422013-12-10 13:10:40 -07002024}
2025
Keith Buscha5cdb682016-01-12 14:41:18 -07002026static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002027{
Keith Busch70659062016-10-12 09:22:16 -06002028 int i, queues;
Keith Busch302ad8c2017-03-01 14:22:12 -05002029 bool dead = true;
2030 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch22404272013-07-15 15:02:20 -06002031
Keith Busch77bf25e2015-11-26 12:21:29 +01002032 mutex_lock(&dev->shutdown_lock);
Keith Busch302ad8c2017-03-01 14:22:12 -05002033 if (pci_is_enabled(pdev)) {
2034 u32 csts = readl(dev->bar + NVME_REG_CSTS);
2035
Keith Buschebef7362017-06-27 17:44:05 -06002036 if (dev->ctrl.state == NVME_CTRL_LIVE ||
2037 dev->ctrl.state == NVME_CTRL_RESETTING)
Keith Busch302ad8c2017-03-01 14:22:12 -05002038 nvme_start_freeze(&dev->ctrl);
2039 dead = !!((csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) ||
2040 pdev->error_state != pci_channel_io_normal);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002041 }
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06002042
Keith Busch302ad8c2017-03-01 14:22:12 -05002043 /*
2044 * Give the controller a chance to complete all entered requests if
2045 * doing a safe shutdown.
2046 */
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002047 if (!dead) {
2048 if (shutdown)
2049 nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
2050
2051 /*
2052 * If the controller is still alive tell it to stop using the
2053 * host memory buffer. In theory the shutdown / reset should
2054 * make sure that it doesn't access the host memoery anymore,
2055 * but I'd rather be safe than sorry..
2056 */
2057 if (dev->host_mem_descs)
2058 nvme_set_host_mem(dev, 0);
2059
2060 }
Keith Busch302ad8c2017-03-01 14:22:12 -05002061 nvme_stop_queues(&dev->ctrl);
2062
Keith Busch70659062016-10-12 09:22:16 -06002063 queues = dev->online_queues - 1;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002064 for (i = dev->ctrl.queue_count - 1; i > 0; i--)
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06002065 nvme_suspend_queue(dev->queues[i]);
2066
Keith Busch302ad8c2017-03-01 14:22:12 -05002067 if (dead) {
Gabriel Krisman Bertazi82469c52016-09-06 17:39:13 -03002068 /* A device might become IO incapable very soon during
2069 * probe, before the admin queue is configured. Thus,
2070 * queue_count can be 0 here.
2071 */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002072 if (dev->ctrl.queue_count)
Gabriel Krisman Bertazi82469c52016-09-06 17:39:13 -03002073 nvme_suspend_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07002074 } else {
Keith Busch70659062016-10-12 09:22:16 -06002075 nvme_disable_io_queues(dev, queues);
Keith Buscha5cdb682016-01-12 14:41:18 -07002076 nvme_disable_admin_queue(dev, shutdown);
Keith Busch4d115422013-12-10 13:10:40 -07002077 }
Keith Buschb00a7262016-02-24 09:15:52 -07002078 nvme_pci_disable(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002079
Ming Line1958e62016-05-18 14:05:01 -07002080 blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl);
2081 blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl);
Keith Busch302ad8c2017-03-01 14:22:12 -05002082
2083 /*
2084 * The driver will not be starting up queues again if shutting down so
2085 * must flush all entered requests to their failed completion to avoid
2086 * deadlocking blk-mq hot-cpu notifier.
2087 */
2088 if (shutdown)
2089 nvme_start_queues(&dev->ctrl);
Keith Busch77bf25e2015-11-26 12:21:29 +01002090 mutex_unlock(&dev->shutdown_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002091}
2092
Matthew Wilcox091b6092011-02-10 09:56:01 -05002093static int nvme_setup_prp_pools(struct nvme_dev *dev)
2094{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002095 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002096 PAGE_SIZE, PAGE_SIZE, 0);
2097 if (!dev->prp_page_pool)
2098 return -ENOMEM;
2099
Matthew Wilcox99802a72011-02-10 10:30:34 -05002100 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002101 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002102 256, 256, 0);
2103 if (!dev->prp_small_pool) {
2104 dma_pool_destroy(dev->prp_page_pool);
2105 return -ENOMEM;
2106 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002107 return 0;
2108}
2109
2110static void nvme_release_prp_pools(struct nvme_dev *dev)
2111{
2112 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002113 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002114}
2115
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002116static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002117{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002118 struct nvme_dev *dev = to_nvme_dev(ctrl);
Keith Busch9ac27092014-01-31 16:53:39 -07002119
Helen Koikef9f38e32017-04-10 12:51:07 -03002120 nvme_dbbuf_dma_free(dev);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002121 put_device(dev->dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002122 if (dev->tagset.tags)
2123 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002124 if (dev->ctrl.admin_q)
2125 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002126 kfree(dev->queues);
Scott Bauere286bcf2017-02-22 10:15:07 -07002127 free_opal_dev(dev->ctrl.opal_dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002128 kfree(dev);
2129}
2130
Keith Buschf58944e2016-02-24 09:15:55 -07002131static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
2132{
Linus Torvalds237045f2016-03-18 17:13:31 -07002133 dev_warn(dev->ctrl.device, "Removing after probe failure status: %d\n", status);
Keith Buschf58944e2016-02-24 09:15:55 -07002134
2135 kref_get(&dev->ctrl.kref);
Keith Busch69d9a992016-02-24 09:15:56 -07002136 nvme_dev_disable(dev, false);
Keith Buschf58944e2016-02-24 09:15:55 -07002137 if (!schedule_work(&dev->remove_work))
2138 nvme_put_ctrl(&dev->ctrl);
2139}
2140
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002141static void nvme_reset_work(struct work_struct *work)
Keith Busch5e82e952013-02-19 10:17:58 -07002142{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002143 struct nvme_dev *dev =
2144 container_of(work, struct nvme_dev, ctrl.reset_work);
Scott Bauera98e58e52017-02-03 12:50:32 -07002145 bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
Keith Buschf58944e2016-02-24 09:15:55 -07002146 int result = -ENODEV;
Keith Buschf0b50732013-07-15 15:02:21 -06002147
Rakesh Pandit82b057c2017-06-05 14:43:11 +03002148 if (WARN_ON(dev->ctrl.state != NVME_CTRL_RESETTING))
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002149 goto out;
2150
2151 /*
2152 * If we're called to reset a live controller first shut it down before
2153 * moving on.
2154 */
Keith Buschb00a7262016-02-24 09:15:52 -07002155 if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
Keith Buscha5cdb682016-01-12 14:41:18 -07002156 nvme_dev_disable(dev, false);
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002157
Keith Buschb00a7262016-02-24 09:15:52 -07002158 result = nvme_pci_enable(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002159 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002160 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002161
Sagi Grimberg01ad0992017-05-01 00:27:17 +03002162 result = nvme_pci_configure_admin_queue(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002163 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002164 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002165
Keith Busch0fb59cb2015-01-07 18:55:50 -07002166 result = nvme_alloc_admin_tags(dev);
2167 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002168 goto out;
Dan McLeranb9afca32014-04-07 17:10:11 -06002169
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002170 result = nvme_init_identify(&dev->ctrl);
2171 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002172 goto out;
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002173
Scott Bauere286bcf2017-02-22 10:15:07 -07002174 if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) {
2175 if (!dev->ctrl.opal_dev)
2176 dev->ctrl.opal_dev =
2177 init_opal_dev(&dev->ctrl, &nvme_sec_submit);
2178 else if (was_suspend)
2179 opal_unlock_from_suspend(dev->ctrl.opal_dev);
2180 } else {
2181 free_opal_dev(dev->ctrl.opal_dev);
2182 dev->ctrl.opal_dev = NULL;
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01002183 }
Scott Bauera98e58e52017-02-03 12:50:32 -07002184
Helen Koikef9f38e32017-04-10 12:51:07 -03002185 if (dev->ctrl.oacs & NVME_CTRL_OACS_DBBUF_SUPP) {
2186 result = nvme_dbbuf_dma_alloc(dev);
2187 if (result)
2188 dev_warn(dev->dev,
2189 "unable to allocate dma for dbbuf\n");
2190 }
2191
Christoph Hellwig9620cfb2017-09-06 12:19:57 +02002192 if (dev->ctrl.hmpre) {
2193 result = nvme_setup_host_mem(dev);
2194 if (result < 0)
2195 goto out;
2196 }
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002197
Keith Buschf0b50732013-07-15 15:02:21 -06002198 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002199 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002200 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002201
Keith Busch21f033f2016-04-12 11:13:11 -06002202 /*
Christoph Hellwig2659e572015-10-02 18:51:31 +02002203 * Keep the controller around but remove all namespaces if we don't have
2204 * any working I/O queue.
2205 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002206 if (dev->online_queues < 2) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002207 dev_warn(dev->ctrl.device, "IO queues not created\n");
Keith Busch3b247742016-04-27 15:51:18 -06002208 nvme_kill_queues(&dev->ctrl);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002209 nvme_remove_namespaces(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002210 } else {
Keith Busch25646262016-01-04 09:10:57 -07002211 nvme_start_queues(&dev->ctrl);
Keith Busch302ad8c2017-03-01 14:22:12 -05002212 nvme_wait_freeze(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002213 nvme_dev_add(dev);
Keith Busch302ad8c2017-03-01 14:22:12 -05002214 nvme_unfreeze(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002215 }
2216
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002217 if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) {
2218 dev_warn(dev->ctrl.device, "failed to mark controller live\n");
2219 goto out;
2220 }
Christoph Hellwig92911a52016-04-26 13:51:58 +02002221
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002222 nvme_start_ctrl(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002223 return;
Keith Buschf0b50732013-07-15 15:02:21 -06002224
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002225 out:
Keith Buschf58944e2016-02-24 09:15:55 -07002226 nvme_remove_dead_ctrl(dev, result);
Keith Buschf0b50732013-07-15 15:02:21 -06002227}
2228
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002229static void nvme_remove_dead_ctrl_work(struct work_struct *work)
Keith Busch9a6b9452013-12-10 13:10:36 -07002230{
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002231 struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002232 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002233
Keith Busch69d9a992016-02-24 09:15:56 -07002234 nvme_kill_queues(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002235 if (pci_get_drvdata(pdev))
Keith Busch921920a2016-03-28 16:03:21 -06002236 device_release_driver(&pdev->dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002237 nvme_put_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002238}
2239
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002240static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
Keith Busch4cc06522015-06-05 10:30:08 -06002241{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002242 *val = readl(to_nvme_dev(ctrl)->bar + off);
2243 return 0;
Keith Busch4cc06522015-06-05 10:30:08 -06002244}
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002245
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002246static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2247{
2248 writel(val, to_nvme_dev(ctrl)->bar + off);
2249 return 0;
2250}
2251
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002252static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2253{
2254 *val = readq(to_nvme_dev(ctrl)->bar + off);
2255 return 0;
2256}
2257
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002258static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
Ming Lin1a353d82016-06-13 16:45:24 +02002259 .name = "pcie",
Sagi Grimberge439bb12016-02-10 10:03:29 -08002260 .module = THIS_MODULE,
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02002261 .flags = NVME_F_METADATA_SUPPORTED,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002262 .reg_read32 = nvme_pci_reg_read32,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002263 .reg_write32 = nvme_pci_reg_write32,
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002264 .reg_read64 = nvme_pci_reg_read64,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002265 .free_ctrl = nvme_pci_free_ctrl,
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002266 .submit_async_event = nvme_pci_submit_async_event,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002267};
Keith Busch4cc06522015-06-05 10:30:08 -06002268
Keith Buschb00a7262016-02-24 09:15:52 -07002269static int nvme_dev_map(struct nvme_dev *dev)
2270{
Keith Buschb00a7262016-02-24 09:15:52 -07002271 struct pci_dev *pdev = to_pci_dev(dev->dev);
2272
Johannes Thumshirna1f447b2016-06-07 09:44:02 +02002273 if (pci_request_mem_regions(pdev, "nvme"))
Keith Buschb00a7262016-02-24 09:15:52 -07002274 return -ENODEV;
2275
Xu Yu97f6ef62017-05-24 16:39:55 +08002276 if (nvme_remap_bar(dev, NVME_REG_DBS + 4096))
Keith Buschb00a7262016-02-24 09:15:52 -07002277 goto release;
2278
Max Gurtovoy9fa196e2016-12-19 16:18:24 +02002279 return 0;
Keith Buschb00a7262016-02-24 09:15:52 -07002280 release:
Max Gurtovoy9fa196e2016-12-19 16:18:24 +02002281 pci_release_mem_regions(pdev);
2282 return -ENODEV;
Keith Buschb00a7262016-02-24 09:15:52 -07002283}
2284
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002285static unsigned long check_dell_samsung_bug(struct pci_dev *pdev)
2286{
2287 if (pdev->vendor == 0x144d && pdev->device == 0xa802) {
2288 /*
2289 * Several Samsung devices seem to drop off the PCIe bus
2290 * randomly when APST is on and uses the deepest sleep state.
2291 * This has been observed on a Samsung "SM951 NVMe SAMSUNG
2292 * 256GB", a "PM951 NVMe SAMSUNG 512GB", and a "Samsung SSD
2293 * 950 PRO 256GB", but it seems to be restricted to two Dell
2294 * laptops.
2295 */
2296 if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.") &&
2297 (dmi_match(DMI_PRODUCT_NAME, "XPS 15 9550") ||
2298 dmi_match(DMI_PRODUCT_NAME, "Precision 5510")))
2299 return NVME_QUIRK_NO_DEEPEST_PS;
2300 }
2301
2302 return 0;
2303}
2304
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002305static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002306{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002307 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002308 struct nvme_dev *dev;
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002309 unsigned long quirks = id->driver_data;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002310
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002311 node = dev_to_node(&pdev->dev);
2312 if (node == NUMA_NO_NODE)
Masayoshi Mizuma2fa84352016-06-20 09:33:17 +09002313 set_dev_node(&pdev->dev, first_memory_node);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002314
2315 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002316 if (!dev)
2317 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002318 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2319 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002320 if (!dev->queues)
2321 goto free;
2322
Christoph Hellwige75ec752015-05-22 11:12:39 +02002323 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002324 pci_set_drvdata(pdev, dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002325
Keith Buschb00a7262016-02-24 09:15:52 -07002326 result = nvme_dev_map(dev);
2327 if (result)
Christophe JAILLETb00c9b72017-07-16 10:39:03 +02002328 goto put_pci;
Keith Buschb00a7262016-02-24 09:15:52 -07002329
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002330 INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002331 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
Keith Busch77bf25e2015-11-26 12:21:29 +01002332 mutex_init(&dev->shutdown_lock);
Keith Buschdb3cbff2016-01-12 14:41:17 -07002333 init_completion(&dev->ioq_wait);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002334
2335 result = nvme_setup_prp_pools(dev);
2336 if (result)
Christophe JAILLETb00c9b72017-07-16 10:39:03 +02002337 goto unmap;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002338
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002339 quirks |= check_dell_samsung_bug(pdev);
2340
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002341 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002342 quirks);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002343 if (result)
2344 goto release_pools;
2345
Rakesh Pandit82b057c2017-06-05 14:43:11 +03002346 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING);
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002347 dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev));
2348
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002349 queue_work(nvme_wq, &dev->ctrl.reset_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002350 return 0;
2351
Keith Busch0877cb02013-07-15 15:02:19 -06002352 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002353 nvme_release_prp_pools(dev);
Christophe JAILLETb00c9b72017-07-16 10:39:03 +02002354 unmap:
2355 nvme_dev_unmap(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002356 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002357 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002358 free:
2359 kfree(dev->queues);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002360 kfree(dev);
2361 return result;
2362}
2363
Christoph Hellwig775755e2017-06-01 13:10:38 +02002364static void nvme_reset_prepare(struct pci_dev *pdev)
Keith Buschf0d54a52014-05-02 10:40:43 -06002365{
Keith Buscha6739472014-06-23 16:03:21 -06002366 struct nvme_dev *dev = pci_get_drvdata(pdev);
Linus Torvaldsf263fbb2017-07-08 15:51:57 -07002367 nvme_dev_disable(dev, false);
Christoph Hellwig775755e2017-06-01 13:10:38 +02002368}
Keith Buschf0d54a52014-05-02 10:40:43 -06002369
Christoph Hellwig775755e2017-06-01 13:10:38 +02002370static void nvme_reset_done(struct pci_dev *pdev)
2371{
Linus Torvaldsf263fbb2017-07-08 15:51:57 -07002372 struct nvme_dev *dev = pci_get_drvdata(pdev);
2373 nvme_reset_ctrl(&dev->ctrl);
Keith Buschf0d54a52014-05-02 10:40:43 -06002374}
2375
Keith Busch09ece142014-01-27 11:29:40 -05002376static void nvme_shutdown(struct pci_dev *pdev)
2377{
2378 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buscha5cdb682016-01-12 14:41:18 -07002379 nvme_dev_disable(dev, true);
Keith Busch09ece142014-01-27 11:29:40 -05002380}
2381
Keith Buschf58944e2016-02-24 09:15:55 -07002382/*
2383 * The driver's remove may be called on a device in a partially initialized
2384 * state. This function must not have any dependencies on the device state in
2385 * order to proceed.
2386 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002387static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002388{
2389 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002390
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002391 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
2392
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002393 cancel_work_sync(&dev->ctrl.reset_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002394 pci_set_drvdata(pdev, NULL);
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002395
Keith Busch6db28ed2017-02-10 18:15:49 -05002396 if (!pci_device_is_present(pdev)) {
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002397 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD);
Keith Busch6db28ed2017-02-10 18:15:49 -05002398 nvme_dev_disable(dev, false);
2399 }
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002400
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002401 flush_work(&dev->ctrl.reset_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002402 nvme_stop_ctrl(&dev->ctrl);
2403 nvme_remove_namespaces(&dev->ctrl);
Keith Buscha5cdb682016-01-12 14:41:18 -07002404 nvme_dev_disable(dev, true);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002405 nvme_free_host_mem(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002406 nvme_dev_remove_admin(dev);
2407 nvme_free_queues(dev, 0);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002408 nvme_uninit_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002409 nvme_release_prp_pools(dev);
Keith Buschb00a7262016-02-24 09:15:52 -07002410 nvme_dev_unmap(dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002411 nvme_put_ctrl(&dev->ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002412}
2413
Keith Busch13880f52016-06-20 09:41:06 -06002414static int nvme_pci_sriov_configure(struct pci_dev *pdev, int numvfs)
2415{
2416 int ret = 0;
2417
2418 if (numvfs == 0) {
2419 if (pci_vfs_assigned(pdev)) {
2420 dev_warn(&pdev->dev,
2421 "Cannot disable SR-IOV VFs while assigned\n");
2422 return -EPERM;
2423 }
2424 pci_disable_sriov(pdev);
2425 return 0;
2426 }
2427
2428 ret = pci_enable_sriov(pdev, numvfs);
2429 return ret ? ret : numvfs;
2430}
2431
Jingoo Han671a6012014-02-13 11:19:14 +09002432#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002433static int nvme_suspend(struct device *dev)
2434{
2435 struct pci_dev *pdev = to_pci_dev(dev);
2436 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2437
Keith Buscha5cdb682016-01-12 14:41:18 -07002438 nvme_dev_disable(ndev, true);
Keith Buschcd638942013-07-15 15:02:23 -06002439 return 0;
2440}
2441
2442static int nvme_resume(struct device *dev)
2443{
2444 struct pci_dev *pdev = to_pci_dev(dev);
2445 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002446
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002447 nvme_reset_ctrl(&ndev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002448 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002449}
Jingoo Han671a6012014-02-13 11:19:14 +09002450#endif
Keith Buschcd638942013-07-15 15:02:23 -06002451
2452static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002453
Keith Buscha0a34082015-12-07 15:30:31 -07002454static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
2455 pci_channel_state_t state)
2456{
2457 struct nvme_dev *dev = pci_get_drvdata(pdev);
2458
2459 /*
2460 * A frozen channel requires a reset. When detected, this method will
2461 * shutdown the controller to quiesce. The controller will be restarted
2462 * after the slot reset through driver's slot_reset callback.
2463 */
Keith Buscha0a34082015-12-07 15:30:31 -07002464 switch (state) {
2465 case pci_channel_io_normal:
2466 return PCI_ERS_RESULT_CAN_RECOVER;
2467 case pci_channel_io_frozen:
Keith Buschd011fb32016-04-04 15:07:41 -06002468 dev_warn(dev->ctrl.device,
2469 "frozen state error detected, reset controller\n");
Keith Buscha5cdb682016-01-12 14:41:18 -07002470 nvme_dev_disable(dev, false);
Keith Buscha0a34082015-12-07 15:30:31 -07002471 return PCI_ERS_RESULT_NEED_RESET;
2472 case pci_channel_io_perm_failure:
Keith Buschd011fb32016-04-04 15:07:41 -06002473 dev_warn(dev->ctrl.device,
2474 "failure state error detected, request disconnect\n");
Keith Buscha0a34082015-12-07 15:30:31 -07002475 return PCI_ERS_RESULT_DISCONNECT;
2476 }
2477 return PCI_ERS_RESULT_NEED_RESET;
2478}
2479
2480static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev)
2481{
2482 struct nvme_dev *dev = pci_get_drvdata(pdev);
2483
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002484 dev_info(dev->ctrl.device, "restart after slot reset\n");
Keith Buscha0a34082015-12-07 15:30:31 -07002485 pci_restore_state(pdev);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002486 nvme_reset_ctrl(&dev->ctrl);
Keith Buscha0a34082015-12-07 15:30:31 -07002487 return PCI_ERS_RESULT_RECOVERED;
2488}
2489
2490static void nvme_error_resume(struct pci_dev *pdev)
2491{
2492 pci_cleanup_aer_uncorrect_error_status(pdev);
2493}
2494
Stephen Hemminger1d352032012-09-07 09:33:17 -07002495static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002496 .error_detected = nvme_error_detected,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002497 .slot_reset = nvme_slot_reset,
2498 .resume = nvme_error_resume,
Christoph Hellwig775755e2017-06-01 13:10:38 +02002499 .reset_prepare = nvme_reset_prepare,
2500 .reset_done = nvme_reset_done,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002501};
2502
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002503static const struct pci_device_id nvme_id_table[] = {
Christoph Hellwig106198e2015-11-26 10:07:41 +01002504 { PCI_VDEVICE(INTEL, 0x0953),
Keith Busch08095e72016-03-04 13:15:17 -07002505 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002506 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch99466e72016-05-02 15:14:24 -06002507 { PCI_VDEVICE(INTEL, 0x0a53),
2508 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002509 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch99466e72016-05-02 15:14:24 -06002510 { PCI_VDEVICE(INTEL, 0x0a54),
2511 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002512 NVME_QUIRK_DEALLOCATE_ZEROES, },
David Wayne Fugatef99cb7af2017-07-10 12:39:59 -06002513 { PCI_VDEVICE(INTEL, 0x0a55),
2514 .driver_data = NVME_QUIRK_STRIPE_SIZE |
2515 NVME_QUIRK_DEALLOCATE_ZEROES, },
Andy Lutomirski50af47d2017-05-24 15:06:31 -07002516 { PCI_VDEVICE(INTEL, 0xf1a5), /* Intel 600P/P3100 */
2517 .driver_data = NVME_QUIRK_NO_DEEPEST_PS },
Keith Busch540c8012015-10-22 15:45:06 -06002518 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2519 .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03002520 { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */
2521 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Wenbo Wang015282c2016-09-08 12:12:11 -04002522 { PCI_DEVICE(0x1c5f, 0x0540), /* Memblaze Pblaze4 adapter */
2523 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Martin K. Petersend554b5e2017-06-27 22:27:57 -04002524 { PCI_DEVICE(0x144d, 0xa821), /* Samsung PM1725 */
2525 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
2526 { PCI_DEVICE(0x144d, 0xa822), /* Samsung PM1725a */
2527 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002528 { PCI_DEVICE(0x1d1d, 0x1f1f), /* LighNVM qemu device */
2529 .driver_data = NVME_QUIRK_LIGHTNVM, },
2530 { PCI_DEVICE(0x1d1d, 0x2807), /* CNEX WL */
2531 .driver_data = NVME_QUIRK_LIGHTNVM, },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002532 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01002533 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Daniel Roschka124298b2017-02-22 15:17:29 -07002534 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002535 { 0, }
2536};
2537MODULE_DEVICE_TABLE(pci, nvme_id_table);
2538
2539static struct pci_driver nvme_driver = {
2540 .name = "nvme",
2541 .id_table = nvme_id_table,
2542 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002543 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002544 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002545 .driver = {
2546 .pm = &nvme_dev_pm_ops,
2547 },
Keith Busch13880f52016-06-20 09:41:06 -06002548 .sriov_configure = nvme_pci_sriov_configure,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002549 .err_handler = &nvme_err_handler,
2550};
2551
2552static int __init nvme_init(void)
2553{
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002554 return pci_register_driver(&nvme_driver);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002555}
2556
2557static void __exit nvme_exit(void)
2558{
2559 pci_unregister_driver(&nvme_driver);
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002560 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002561}
2562
2563MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2564MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002565MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002566module_init(nvme_init);
2567module_exit(nvme_exit);