blob: e6283745ecd233794731044787ec8207e4326c54 [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>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050027#include <linux/pci.h>
Matthew Wilcoxbe7b6272011-02-06 07:53:23 -050028#include <linux/poison.h>
Keith Busche1e5e562015-02-19 13:39:03 -070029#include <linux/t10-pi.h>
Christoph Hellwig2d55cd52016-02-29 15:59:46 +010030#include <linux/timer.h>
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050031#include <linux/types.h>
Linus Torvalds9cf5c092015-11-06 14:22:15 -080032#include <linux/io-64-nonatomic-lo-hi.h>
Keith Busch1d277a62015-10-15 14:10:52 +020033#include <asm/unaligned.h>
Scott Bauera98e58e52017-02-03 12:50:32 -070034#include <linux/sed-opal.h>
Hitoshi Mitake797a7962012-02-07 11:45:33 +090035
Christoph Hellwigf11bb3e2015-10-03 15:46:41 +020036#include "nvme.h"
37
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050038#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
39#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
Stephen Batesc9658092016-12-16 11:54:50 -070040
Christoph Hellwigadf68f22015-11-28 15:42:28 +010041/*
42 * We handle AEN commands ourselves and don't even let the
43 * block layer know about them.
44 */
Christoph Hellwigf866fc422016-04-26 13:52:00 +020045#define NVME_AQ_BLKMQ_DEPTH (NVME_AQ_DEPTH - NVME_NR_AERS)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050046
Matthew Wilcox58ffacb2011-02-06 07:28:06 -050047static int use_threaded_interrupts;
48module_param(use_threaded_interrupts, int, 0);
49
Jon Derrick8ffaadf2015-07-20 10:14:09 -060050static bool use_cmb_sqes = true;
51module_param(use_cmb_sqes, bool, 0644);
52MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
53
Christoph Hellwig87ad72a2017-05-12 17:02:58 +020054static unsigned int max_host_mem_size_mb = 128;
55module_param(max_host_mem_size_mb, uint, 0444);
56MODULE_PARM_DESC(max_host_mem_size_mb,
57 "Maximum Host Memory Buffer (HMB) size per controller (in MiB)");
Matthew Wilcox1fa6aea2011-03-02 18:37:18 -050058
weiping zhangb27c1e62017-07-10 16:46:59 +080059static int io_queue_depth_set(const char *val, const struct kernel_param *kp);
60static const struct kernel_param_ops io_queue_depth_ops = {
61 .set = io_queue_depth_set,
62 .get = param_get_int,
63};
64
65static int io_queue_depth = 1024;
66module_param_cb(io_queue_depth, &io_queue_depth_ops, &io_queue_depth, 0644);
67MODULE_PARM_DESC(io_queue_depth, "set io queue depth, should >= 2");
68
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010069struct nvme_dev;
70struct nvme_queue;
Keith Buschb3fffde2015-02-03 11:21:42 -070071
Jens Axboea0fa9642015-11-03 20:37:26 -070072static void nvme_process_cq(struct nvme_queue *nvmeq);
Keith Buscha5cdb682016-01-12 14:41:18 -070073static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown);
Keith Buschd4b4ff82013-12-10 13:10:37 -070074
Matthew Wilcoxb60503b2011-01-20 12:50:14 -050075/*
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010076 * Represents an NVM Express device. Each nvme_dev is a PCI function.
77 */
78struct nvme_dev {
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010079 struct nvme_queue **queues;
80 struct blk_mq_tag_set tagset;
81 struct blk_mq_tag_set admin_tagset;
82 u32 __iomem *dbs;
83 struct device *dev;
84 struct dma_pool *prp_page_pool;
85 struct dma_pool *prp_small_pool;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010086 unsigned online_queues;
87 unsigned max_qid;
88 int q_depth;
89 u32 db_stride;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010090 void __iomem *bar;
Xu Yu97f6ef62017-05-24 16:39:55 +080091 unsigned long bar_mapped_size;
Christoph Hellwig5c8809e2015-11-26 12:35:49 +010092 struct work_struct remove_work;
Keith Busch77bf25e2015-11-26 12:21:29 +010093 struct mutex shutdown_lock;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010094 bool subsystem;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +010095 void __iomem *cmb;
96 dma_addr_t cmb_dma_addr;
97 u64 cmb_size;
98 u32 cmbsz;
Stephen Bates202021c2016-10-05 20:01:12 -060099 u32 cmbloc;
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100100 struct nvme_ctrl ctrl;
Keith Buschdb3cbff2016-01-12 14:41:17 -0700101 struct completion ioq_wait;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +0200102
103 /* shadow doorbell buffer support: */
Helen Koikef9f38e32017-04-10 12:51:07 -0300104 u32 *dbbuf_dbs;
105 dma_addr_t dbbuf_dbs_dma_addr;
106 u32 *dbbuf_eis;
107 dma_addr_t dbbuf_eis_dma_addr;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +0200108
109 /* host memory buffer support: */
110 u64 host_mem_size;
111 u32 nr_host_mem_descs;
112 struct nvme_host_mem_buf_desc *host_mem_descs;
113 void **host_mem_desc_bufs;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500114};
115
weiping zhangb27c1e62017-07-10 16:46:59 +0800116static int io_queue_depth_set(const char *val, const struct kernel_param *kp)
117{
118 int n = 0, ret;
119
120 ret = kstrtoint(val, 10, &n);
121 if (ret != 0 || n < 2)
122 return -EINVAL;
123
124 return param_set_int(val, kp);
125}
126
Helen Koikef9f38e32017-04-10 12:51:07 -0300127static inline unsigned int sq_idx(unsigned int qid, u32 stride)
128{
129 return qid * 2 * stride;
130}
131
132static inline unsigned int cq_idx(unsigned int qid, u32 stride)
133{
134 return (qid * 2 + 1) * stride;
135}
136
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100137static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
138{
139 return container_of(ctrl, struct nvme_dev, ctrl);
140}
141
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500142/*
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500143 * An NVM Express queue. Each device has at least two (one for admin
144 * commands and one for I/O commands).
145 */
146struct nvme_queue {
147 struct device *q_dmadev;
Matthew Wilcox091b6092011-02-10 09:56:01 -0500148 struct nvme_dev *dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500149 spinlock_t q_lock;
150 struct nvme_command *sq_cmds;
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600151 struct nvme_command __iomem *sq_cmds_io;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500152 volatile struct nvme_completion *cqes;
Keith Busch42483222015-06-01 09:29:54 -0600153 struct blk_mq_tags **tags;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500154 dma_addr_t sq_dma_addr;
155 dma_addr_t cq_dma_addr;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500156 u32 __iomem *q_db;
157 u16 q_depth;
Jens Axboe6222d172015-01-15 15:19:10 -0700158 s16 cq_vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500159 u16 sq_tail;
160 u16 cq_head;
Keith Buschc30341d2013-12-10 13:10:38 -0700161 u16 qid;
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400162 u8 cq_phase;
163 u8 cqe_seen;
Helen Koikef9f38e32017-04-10 12:51:07 -0300164 u32 *dbbuf_sq_db;
165 u32 *dbbuf_cq_db;
166 u32 *dbbuf_sq_ei;
167 u32 *dbbuf_cq_ei;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500168};
169
170/*
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200171 * The nvme_iod describes the data in an I/O, including the list of PRP
172 * entries. You can't see it in this data structure because C doesn't let
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100173 * me express that. Use nvme_init_iod to ensure there's enough space
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200174 * allocated to store the PRP list.
175 */
176struct nvme_iod {
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800177 struct nvme_request req;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100178 struct nvme_queue *nvmeq;
179 int aborted;
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200180 int npages; /* In the PRP list. 0 means small pool in use */
Christoph Hellwig71bd1502015-10-16 07:58:32 +0200181 int nents; /* Used in scatterlist */
182 int length; /* Of data, in bytes */
183 dma_addr_t first_dma;
Christoph Hellwigbf684052015-10-26 17:12:51 +0900184 struct scatterlist meta_sg; /* metadata requires single contiguous buffer */
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100185 struct scatterlist *sg;
186 struct scatterlist inline_sg[0];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500187};
188
189/*
190 * Check we didin't inadvertently grow the command struct
191 */
192static inline void _nvme_check_size(void)
193{
194 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
195 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
196 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
197 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
198 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
Vishal Vermaf8ebf842013-03-27 07:13:41 -0400199 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
Keith Buschc30341d2013-12-10 13:10:38 -0700200 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500201 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
Johannes Thumshirn0add5e82017-06-07 11:45:29 +0200202 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != NVME_IDENTIFY_DATA_SIZE);
203 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != NVME_IDENTIFY_DATA_SIZE);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500204 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
Keith Busch6ecec742012-09-26 12:49:27 -0600205 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
Helen Koikef9f38e32017-04-10 12:51:07 -0300206 BUILD_BUG_ON(sizeof(struct nvme_dbbuf) != 64);
207}
208
209static inline unsigned int nvme_dbbuf_size(u32 stride)
210{
211 return ((num_possible_cpus() + 1) * 8 * stride);
212}
213
214static int nvme_dbbuf_dma_alloc(struct nvme_dev *dev)
215{
216 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
217
218 if (dev->dbbuf_dbs)
219 return 0;
220
221 dev->dbbuf_dbs = dma_alloc_coherent(dev->dev, mem_size,
222 &dev->dbbuf_dbs_dma_addr,
223 GFP_KERNEL);
224 if (!dev->dbbuf_dbs)
225 return -ENOMEM;
226 dev->dbbuf_eis = dma_alloc_coherent(dev->dev, mem_size,
227 &dev->dbbuf_eis_dma_addr,
228 GFP_KERNEL);
229 if (!dev->dbbuf_eis) {
230 dma_free_coherent(dev->dev, mem_size,
231 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
232 dev->dbbuf_dbs = NULL;
233 return -ENOMEM;
234 }
235
236 return 0;
237}
238
239static void nvme_dbbuf_dma_free(struct nvme_dev *dev)
240{
241 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
242
243 if (dev->dbbuf_dbs) {
244 dma_free_coherent(dev->dev, mem_size,
245 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
246 dev->dbbuf_dbs = NULL;
247 }
248 if (dev->dbbuf_eis) {
249 dma_free_coherent(dev->dev, mem_size,
250 dev->dbbuf_eis, dev->dbbuf_eis_dma_addr);
251 dev->dbbuf_eis = NULL;
252 }
253}
254
255static void nvme_dbbuf_init(struct nvme_dev *dev,
256 struct nvme_queue *nvmeq, int qid)
257{
258 if (!dev->dbbuf_dbs || !qid)
259 return;
260
261 nvmeq->dbbuf_sq_db = &dev->dbbuf_dbs[sq_idx(qid, dev->db_stride)];
262 nvmeq->dbbuf_cq_db = &dev->dbbuf_dbs[cq_idx(qid, dev->db_stride)];
263 nvmeq->dbbuf_sq_ei = &dev->dbbuf_eis[sq_idx(qid, dev->db_stride)];
264 nvmeq->dbbuf_cq_ei = &dev->dbbuf_eis[cq_idx(qid, dev->db_stride)];
265}
266
267static void nvme_dbbuf_set(struct nvme_dev *dev)
268{
269 struct nvme_command c;
270
271 if (!dev->dbbuf_dbs)
272 return;
273
274 memset(&c, 0, sizeof(c));
275 c.dbbuf.opcode = nvme_admin_dbbuf;
276 c.dbbuf.prp1 = cpu_to_le64(dev->dbbuf_dbs_dma_addr);
277 c.dbbuf.prp2 = cpu_to_le64(dev->dbbuf_eis_dma_addr);
278
279 if (nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0)) {
Christoph Hellwig9bdcfb12017-05-20 15:14:43 +0200280 dev_warn(dev->ctrl.device, "unable to set dbbuf\n");
Helen Koikef9f38e32017-04-10 12:51:07 -0300281 /* Free memory and continue on */
282 nvme_dbbuf_dma_free(dev);
283 }
284}
285
286static inline int nvme_dbbuf_need_event(u16 event_idx, u16 new_idx, u16 old)
287{
288 return (u16)(new_idx - event_idx - 1) < (u16)(new_idx - old);
289}
290
291/* Update dbbuf and return true if an MMIO is required */
292static bool nvme_dbbuf_update_and_check_event(u16 value, u32 *dbbuf_db,
293 volatile u32 *dbbuf_ei)
294{
295 if (dbbuf_db) {
296 u16 old_value;
297
298 /*
299 * Ensure that the queue is written before updating
300 * the doorbell in memory
301 */
302 wmb();
303
304 old_value = *dbbuf_db;
305 *dbbuf_db = value;
306
307 if (!nvme_dbbuf_need_event(*dbbuf_ei, value, old_value))
308 return false;
309 }
310
311 return true;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500312}
313
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700314/*
315 * Max size of iod being embedded in the request payload
316 */
317#define NVME_INT_PAGES 2
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100318#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700319
320/*
321 * Will slightly overestimate the number of pages needed. This is OK
322 * as it only leads to a small amount of wasted memory for the lifetime of
323 * the I/O.
324 */
325static int nvme_npages(unsigned size, struct nvme_dev *dev)
326{
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100327 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
328 dev->ctrl.page_size);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700329 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
330}
331
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100332static unsigned int nvme_iod_alloc_size(struct nvme_dev *dev,
333 unsigned int size, unsigned int nseg)
334{
335 return sizeof(__le64 *) * nvme_npages(size, dev) +
336 sizeof(struct scatterlist) * nseg;
337}
338
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700339static unsigned int nvme_cmd_size(struct nvme_dev *dev)
340{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100341 return sizeof(struct nvme_iod) +
342 nvme_iod_alloc_size(dev, NVME_INT_BYTES(dev), NVME_INT_PAGES);
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700343}
344
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700345static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
346 unsigned int hctx_idx)
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500347{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700348 struct nvme_dev *dev = data;
349 struct nvme_queue *nvmeq = dev->queues[0];
350
Keith Busch42483222015-06-01 09:29:54 -0600351 WARN_ON(hctx_idx != 0);
352 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
353 WARN_ON(nvmeq->tags);
354
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700355 hctx->driver_data = nvmeq;
Keith Busch42483222015-06-01 09:29:54 -0600356 nvmeq->tags = &dev->admin_tagset.tags[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700357 return 0;
Matthew Wilcoxe85248e2011-02-06 18:30:16 -0500358}
359
Keith Busch4af0e212015-06-08 10:08:13 -0600360static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
361{
362 struct nvme_queue *nvmeq = hctx->driver_data;
363
364 nvmeq->tags = NULL;
365}
366
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700367static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
368 unsigned int hctx_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500369{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700370 struct nvme_dev *dev = data;
Keith Busch42483222015-06-01 09:29:54 -0600371 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500372
Keith Busch42483222015-06-01 09:29:54 -0600373 if (!nvmeq->tags)
374 nvmeq->tags = &dev->tagset.tags[hctx_idx];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500375
Keith Busch42483222015-06-01 09:29:54 -0600376 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700377 hctx->driver_data = nvmeq;
378 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500379}
380
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600381static int nvme_init_request(struct blk_mq_tag_set *set, struct request *req,
382 unsigned int hctx_idx, unsigned int numa_node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500383{
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600384 struct nvme_dev *dev = set->driver_data;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100385 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwig03508152017-06-13 09:15:18 +0200386 int queue_idx = (set == &dev->tagset) ? hctx_idx + 1 : 0;
387 struct nvme_queue *nvmeq = dev->queues[queue_idx];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700388
389 BUG_ON(!nvmeq);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100390 iod->nvmeq = nvmeq;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700391 return 0;
392}
393
Christoph Hellwigdca51e72016-09-14 16:18:57 +0200394static int nvme_pci_map_queues(struct blk_mq_tag_set *set)
395{
396 struct nvme_dev *dev = set->driver_data;
397
398 return blk_mq_pci_map_queues(set, to_pci_dev(dev->dev));
399}
400
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500401/**
Christoph Hellwigadf68f22015-11-28 15:42:28 +0100402 * __nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500403 * @nvmeq: The queue to use
404 * @cmd: The command to send
405 *
406 * Safe to use from interrupt context
407 */
Sunad Bhandarye3f879b2015-07-31 18:56:58 +0530408static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
409 struct nvme_command *cmd)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500410{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700411 u16 tail = nvmeq->sq_tail;
412
Jon Derrick8ffaadf2015-07-20 10:14:09 -0600413 if (nvmeq->sq_cmds_io)
414 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
415 else
416 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
417
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500418 if (++tail == nvmeq->q_depth)
419 tail = 0;
Helen Koikef9f38e32017-04-10 12:51:07 -0300420 if (nvme_dbbuf_update_and_check_event(tail, nvmeq->dbbuf_sq_db,
421 nvmeq->dbbuf_sq_ei))
422 writel(tail, nvmeq->q_db);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500423 nvmeq->sq_tail = tail;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500424}
425
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100426static __le64 **iod_list(struct request *req)
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700427{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100428 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700429 return (__le64 **)(iod->sg + blk_rq_nr_phys_segments(req));
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700430}
431
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200432static blk_status_t nvme_init_iod(struct request *rq, struct nvme_dev *dev)
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500433{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100434 struct nvme_iod *iod = blk_mq_rq_to_pdu(rq);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700435 int nseg = blk_rq_nr_phys_segments(rq);
Christoph Hellwigb131c612017-01-13 12:29:12 +0100436 unsigned int size = blk_rq_payload_bytes(rq);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500437
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100438 if (nseg > NVME_INT_PAGES || size > NVME_INT_BYTES(dev)) {
439 iod->sg = kmalloc(nvme_iod_alloc_size(dev, size, nseg), GFP_ATOMIC);
440 if (!iod->sg)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200441 return BLK_STS_RESOURCE;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100442 } else {
443 iod->sg = iod->inline_sg;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700444 }
445
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100446 iod->aborted = 0;
447 iod->npages = -1;
448 iod->nents = 0;
449 iod->length = size;
Keith Buschf80ec962016-07-12 16:20:31 -0700450
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200451 return BLK_STS_OK;
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700452}
453
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100454static void nvme_free_iod(struct nvme_dev *dev, struct request *req)
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500455{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100456 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100457 const int last_prp = dev->ctrl.page_size / 8 - 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500458 int i;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100459 __le64 **list = iod_list(req);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500460 dma_addr_t prp_dma = iod->first_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500461
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500462 if (iod->npages == 0)
463 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
464 for (i = 0; i < iod->npages; i++) {
465 __le64 *prp_list = list[i];
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500466 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
Matthew Wilcox091b6092011-02-10 09:56:01 -0500467 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500468 prp_dma = next_prp_dma;
469 }
Jens Axboeac3dd5b2015-01-22 12:07:58 -0700470
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100471 if (iod->sg != iod->inline_sg)
472 kfree(iod->sg);
Keith Buschb4ff9c82014-08-29 09:06:12 -0600473}
474
Keith Busch52b68d72015-02-23 09:16:21 -0700475#ifdef CONFIG_BLK_DEV_INTEGRITY
Keith Busche1e5e562015-02-19 13:39:03 -0700476static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
477{
478 if (be32_to_cpu(pi->ref_tag) == v)
479 pi->ref_tag = cpu_to_be32(p);
480}
481
482static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
483{
484 if (be32_to_cpu(pi->ref_tag) == p)
485 pi->ref_tag = cpu_to_be32(v);
486}
487
488/**
489 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
490 *
491 * The virtual start sector is the one that was originally submitted by the
492 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
493 * start sector may be different. Remap protection information to match the
494 * physical LBA on writes, and back to the original seed on reads.
495 *
496 * Type 0 and 3 do not have a ref tag, so no remapping required.
497 */
498static void nvme_dif_remap(struct request *req,
499 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
500{
501 struct nvme_ns *ns = req->rq_disk->private_data;
502 struct bio_integrity_payload *bip;
503 struct t10_pi_tuple *pi;
504 void *p, *pmap;
505 u32 i, nlb, ts, phys, virt;
506
507 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
508 return;
509
510 bip = bio_integrity(req->bio);
511 if (!bip)
512 return;
513
514 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
Keith Busche1e5e562015-02-19 13:39:03 -0700515
516 p = pmap;
517 virt = bip_get_seed(bip);
518 phys = nvme_block_nr(ns, blk_rq_pos(req));
519 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
Dan Williamsac6fc482015-10-21 13:20:18 -0400520 ts = ns->disk->queue->integrity.tuple_size;
Keith Busche1e5e562015-02-19 13:39:03 -0700521
522 for (i = 0; i < nlb; i++, virt++, phys++) {
523 pi = (struct t10_pi_tuple *)p;
524 dif_swap(phys, virt, pi);
525 p += ts;
526 }
527 kunmap_atomic(pmap);
528}
Keith Busch52b68d72015-02-23 09:16:21 -0700529#else /* CONFIG_BLK_DEV_INTEGRITY */
530static void nvme_dif_remap(struct request *req,
531 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
532{
533}
534static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
535{
536}
537static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
538{
539}
Keith Busch52b68d72015-02-23 09:16:21 -0700540#endif
541
Keith Busch86eea282017-07-12 15:59:07 -0400542static blk_status_t nvme_setup_prps(struct nvme_dev *dev, struct request *req)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500543{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100544 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500545 struct dma_pool *pool;
Christoph Hellwigb131c612017-01-13 12:29:12 +0100546 int length = blk_rq_payload_bytes(req);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500547 struct scatterlist *sg = iod->sg;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500548 int dma_len = sg_dma_len(sg);
549 u64 dma_addr = sg_dma_address(sg);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +0100550 u32 page_size = dev->ctrl.page_size;
Murali Iyerf137e0f2015-03-26 11:07:51 -0500551 int offset = dma_addr & (page_size - 1);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500552 __le64 *prp_list;
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100553 __le64 **list = iod_list(req);
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500554 dma_addr_t prp_dma;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500555 int nprps, i;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500556
Keith Busch1d090622014-06-23 11:34:01 -0600557 length -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500558 if (length <= 0)
Keith Busch86eea282017-07-12 15:59:07 -0400559 return BLK_STS_OK;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500560
Keith Busch1d090622014-06-23 11:34:01 -0600561 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500562 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600563 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500564 } else {
565 sg = sg_next(sg);
566 dma_addr = sg_dma_address(sg);
567 dma_len = sg_dma_len(sg);
568 }
569
Keith Busch1d090622014-06-23 11:34:01 -0600570 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600571 iod->first_dma = dma_addr;
Keith Busch86eea282017-07-12 15:59:07 -0400572 return BLK_STS_OK;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500573 }
574
Keith Busch1d090622014-06-23 11:34:01 -0600575 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500576 if (nprps <= (256 / 8)) {
577 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500578 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500579 } else {
580 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500581 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500582 }
583
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200584 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400585 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600586 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500587 iod->npages = -1;
Keith Busch86eea282017-07-12 15:59:07 -0400588 return BLK_STS_RESOURCE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400589 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500590 list[0] = prp_list;
591 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500592 i = 0;
593 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600594 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500595 __le64 *old_prp_list = prp_list;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200596 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500597 if (!prp_list)
Keith Busch86eea282017-07-12 15:59:07 -0400598 return BLK_STS_RESOURCE;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500599 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400600 prp_list[0] = old_prp_list[i - 1];
601 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
602 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500603 }
604 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600605 dma_len -= page_size;
606 dma_addr += page_size;
607 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500608 if (length <= 0)
609 break;
610 if (dma_len > 0)
611 continue;
Keith Busch86eea282017-07-12 15:59:07 -0400612 if (unlikely(dma_len < 0))
613 goto bad_sgl;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500614 sg = sg_next(sg);
615 dma_addr = sg_dma_address(sg);
616 dma_len = sg_dma_len(sg);
617 }
618
Keith Busch86eea282017-07-12 15:59:07 -0400619 return BLK_STS_OK;
620
621 bad_sgl:
622 if (WARN_ONCE(1, "Invalid SGL for payload:%d nents:%d\n",
623 blk_rq_payload_bytes(req), iod->nents)) {
624 for_each_sg(iod->sg, sg, iod->nents, i) {
625 dma_addr_t phys = sg_phys(sg);
626 pr_warn("sg[%d] phys_addr:%pad offset:%d length:%d "
627 "dma_address:%pad dma_length:%d\n", i, &phys,
628 sg->offset, sg->length,
629 &sg_dma_address(sg),
630 sg_dma_len(sg));
631 }
632 }
633 return BLK_STS_IOERR;
634
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500635}
636
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200637static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,
Christoph Hellwigb131c612017-01-13 12:29:12 +0100638 struct nvme_command *cmnd)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200639{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100640 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200641 struct request_queue *q = req->q;
642 enum dma_data_direction dma_dir = rq_data_dir(req) ?
643 DMA_TO_DEVICE : DMA_FROM_DEVICE;
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200644 blk_status_t ret = BLK_STS_IOERR;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200645
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700646 sg_init_table(iod->sg, blk_rq_nr_phys_segments(req));
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200647 iod->nents = blk_rq_map_sg(q, req, iod->sg);
648 if (!iod->nents)
649 goto out;
650
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200651 ret = BLK_STS_RESOURCE;
Mauricio Faria de Oliveira2b6b5352016-10-11 13:54:20 -0700652 if (!dma_map_sg_attrs(dev->dev, iod->sg, iod->nents, dma_dir,
653 DMA_ATTR_NO_WARN))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200654 goto out;
655
Keith Busch86eea282017-07-12 15:59:07 -0400656 ret = nvme_setup_prps(dev, req);
657 if (ret != BLK_STS_OK)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200658 goto out_unmap;
659
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200660 ret = BLK_STS_IOERR;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200661 if (blk_integrity_rq(req)) {
662 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
663 goto out_unmap;
664
Christoph Hellwigbf684052015-10-26 17:12:51 +0900665 sg_init_table(&iod->meta_sg, 1);
666 if (blk_rq_map_integrity_sg(q, req->bio, &iod->meta_sg) != 1)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200667 goto out_unmap;
668
669 if (rq_data_dir(req))
670 nvme_dif_remap(req, nvme_dif_prep);
671
Christoph Hellwigbf684052015-10-26 17:12:51 +0900672 if (!dma_map_sg(dev->dev, &iod->meta_sg, 1, dma_dir))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200673 goto out_unmap;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200674 }
675
Christoph Hellwigeb793e22016-06-13 16:45:25 +0200676 cmnd->rw.dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
677 cmnd->rw.dptr.prp2 = cpu_to_le64(iod->first_dma);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200678 if (blk_integrity_rq(req))
Christoph Hellwigbf684052015-10-26 17:12:51 +0900679 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(&iod->meta_sg));
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200680 return BLK_STS_OK;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200681
682out_unmap:
683 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
684out:
685 return ret;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200686}
687
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100688static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100689{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100690 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100691 enum dma_data_direction dma_dir = rq_data_dir(req) ?
692 DMA_TO_DEVICE : DMA_FROM_DEVICE;
693
694 if (iod->nents) {
695 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
696 if (blk_integrity_rq(req)) {
697 if (!rq_data_dir(req))
698 nvme_dif_remap(req, nvme_dif_complete);
Christoph Hellwigbf684052015-10-26 17:12:51 +0900699 dma_unmap_sg(dev->dev, &iod->meta_sg, 1, dma_dir);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100700 }
701 }
702
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700703 nvme_cleanup_cmd(req);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100704 nvme_free_iod(dev, req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500705}
706
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700707/*
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200708 * NOTE: ns is NULL when called on the admin queue.
709 */
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200710static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700711 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600712{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700713 struct nvme_ns *ns = hctx->queue->queuedata;
714 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200715 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700716 struct request *req = bd->rq;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200717 struct nvme_command cmnd;
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200718 blk_status_t ret;
Keith Busche1e5e562015-02-19 13:39:03 -0700719
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700720 ret = nvme_setup_cmd(ns, req, &cmnd);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200721 if (ret)
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100722 return ret;
Keith Buschedd10d32014-04-03 16:45:23 -0600723
Christoph Hellwigb131c612017-01-13 12:29:12 +0100724 ret = nvme_init_iod(req, dev);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200725 if (ret)
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700726 goto out_free_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600727
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200728 if (blk_rq_nr_phys_segments(req)) {
Christoph Hellwigb131c612017-01-13 12:29:12 +0100729 ret = nvme_map_data(dev, req, &cmnd);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200730 if (ret)
731 goto out_cleanup_iod;
732 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700733
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100734 blk_mq_start_request(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200735
736 spin_lock_irq(&nvmeq->q_lock);
Keith Buschae1fba22016-02-11 13:05:42 -0700737 if (unlikely(nvmeq->cq_vector < 0)) {
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200738 ret = BLK_STS_IOERR;
Keith Buschae1fba22016-02-11 13:05:42 -0700739 spin_unlock_irq(&nvmeq->q_lock);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700740 goto out_cleanup_iod;
Keith Buschae1fba22016-02-11 13:05:42 -0700741 }
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200742 __nvme_submit_cmd(nvmeq, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700743 nvme_process_cq(nvmeq);
744 spin_unlock_irq(&nvmeq->q_lock);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200745 return BLK_STS_OK;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700746out_cleanup_iod:
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100747 nvme_free_iod(dev, req);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700748out_free_cmd:
749 nvme_cleanup_cmd(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200750 return ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500751}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500752
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200753static void nvme_pci_complete_rq(struct request *req)
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100754{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100755 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100756
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200757 nvme_unmap_data(iod->nvmeq->dev, req);
758 nvme_complete_rq(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500759}
760
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100761/* We read the CQE phase first to check if the rest of the entry is valid */
762static inline bool nvme_cqe_valid(struct nvme_queue *nvmeq, u16 head,
763 u16 phase)
764{
765 return (le16_to_cpu(nvmeq->cqes[head].status) & 1) == phase;
766}
767
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300768static inline void nvme_ring_cq_doorbell(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500769{
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300770 u16 head = nvmeq->cq_head;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500771
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300772 if (likely(nvmeq->cq_vector >= 0)) {
Helen Koikef9f38e32017-04-10 12:51:07 -0300773 if (nvme_dbbuf_update_and_check_event(head, nvmeq->dbbuf_cq_db,
774 nvmeq->dbbuf_cq_ei))
775 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300776 }
777}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500778
Sagi Grimberg83a12fb2017-06-18 17:28:08 +0300779static inline void nvme_handle_cqe(struct nvme_queue *nvmeq,
780 struct nvme_completion *cqe)
781{
782 struct request *req;
783
784 if (unlikely(cqe->command_id >= nvmeq->q_depth)) {
785 dev_warn(nvmeq->dev->ctrl.device,
786 "invalid id %d completed on queue %d\n",
787 cqe->command_id, le16_to_cpu(cqe->sq_id));
788 return;
789 }
790
791 /*
792 * AEN requests are special as they don't time out and can
793 * survive any kind of queue freeze and often don't respond to
794 * aborts. We don't even bother to allocate a struct request
795 * for them but rather special case them here.
796 */
797 if (unlikely(nvmeq->qid == 0 &&
798 cqe->command_id >= NVME_AQ_BLKMQ_DEPTH)) {
799 nvme_complete_async_event(&nvmeq->dev->ctrl,
800 cqe->status, &cqe->result);
801 return;
802 }
803
Keith Busche9d8a0f2017-08-17 16:45:06 -0400804 nvmeq->cqe_seen = 1;
Sagi Grimberg83a12fb2017-06-18 17:28:08 +0300805 req = blk_mq_tag_to_rq(*nvmeq->tags, cqe->command_id);
806 nvme_end_request(req, cqe->status, cqe->result);
807}
808
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300809static inline bool nvme_read_cqe(struct nvme_queue *nvmeq,
810 struct nvme_completion *cqe)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500811{
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300812 if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase)) {
813 *cqe = nvmeq->cqes[nvmeq->cq_head];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500814
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300815 if (++nvmeq->cq_head == nvmeq->q_depth) {
816 nvmeq->cq_head = 0;
817 nvmeq->cq_phase = !nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500818 }
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300819 return true;
820 }
821 return false;
Jens Axboea0fa9642015-11-03 20:37:26 -0700822}
823
824static void nvme_process_cq(struct nvme_queue *nvmeq)
825{
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300826 struct nvme_completion cqe;
827 int consumed = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500828
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300829 while (nvme_read_cqe(nvmeq, &cqe)) {
Sagi Grimberg83a12fb2017-06-18 17:28:08 +0300830 nvme_handle_cqe(nvmeq, &cqe);
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300831 consumed++;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500832 }
833
Keith Busche9d8a0f2017-08-17 16:45:06 -0400834 if (consumed)
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300835 nvme_ring_cq_doorbell(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500836}
837
838static irqreturn_t nvme_irq(int irq, void *data)
839{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500840 irqreturn_t result;
841 struct nvme_queue *nvmeq = data;
842 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400843 nvme_process_cq(nvmeq);
844 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
845 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500846 spin_unlock(&nvmeq->q_lock);
847 return result;
848}
849
850static irqreturn_t nvme_irq_check(int irq, void *data)
851{
852 struct nvme_queue *nvmeq = data;
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100853 if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase))
854 return IRQ_WAKE_THREAD;
855 return IRQ_NONE;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500856}
857
Keith Busch7776db12017-02-24 17:59:28 -0500858static int __nvme_poll(struct nvme_queue *nvmeq, unsigned int tag)
Jens Axboea0fa9642015-11-03 20:37:26 -0700859{
Sagi Grimberg442e19b2017-06-18 17:28:10 +0300860 struct nvme_completion cqe;
861 int found = 0, consumed = 0;
Jens Axboea0fa9642015-11-03 20:37:26 -0700862
Sagi Grimberg442e19b2017-06-18 17:28:10 +0300863 if (!nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase))
864 return 0;
Jens Axboea0fa9642015-11-03 20:37:26 -0700865
Sagi Grimberg442e19b2017-06-18 17:28:10 +0300866 spin_lock_irq(&nvmeq->q_lock);
867 while (nvme_read_cqe(nvmeq, &cqe)) {
868 nvme_handle_cqe(nvmeq, &cqe);
869 consumed++;
870
871 if (tag == cqe.command_id) {
872 found = 1;
873 break;
874 }
875 }
876
877 if (consumed)
878 nvme_ring_cq_doorbell(nvmeq);
879 spin_unlock_irq(&nvmeq->q_lock);
880
881 return found;
Jens Axboea0fa9642015-11-03 20:37:26 -0700882}
883
Keith Busch7776db12017-02-24 17:59:28 -0500884static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
885{
886 struct nvme_queue *nvmeq = hctx->driver_data;
887
888 return __nvme_poll(nvmeq, tag);
889}
890
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200891static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl, int aer_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500892{
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200893 struct nvme_dev *dev = to_nvme_dev(ctrl);
Christoph Hellwig9396dec2016-02-29 15:59:44 +0100894 struct nvme_queue *nvmeq = dev->queues[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700895 struct nvme_command c;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700896
897 memset(&c, 0, sizeof(c));
898 c.common.opcode = nvme_admin_async_event;
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200899 c.common.command_id = NVME_AQ_BLKMQ_DEPTH + aer_idx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700900
Christoph Hellwig9396dec2016-02-29 15:59:44 +0100901 spin_lock_irq(&nvmeq->q_lock);
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200902 __nvme_submit_cmd(nvmeq, &c);
Christoph Hellwig9396dec2016-02-29 15:59:44 +0100903 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -0700904}
905
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500906static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
907{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500908 struct nvme_command c;
909
910 memset(&c, 0, sizeof(c));
911 c.delete_queue.opcode = opcode;
912 c.delete_queue.qid = cpu_to_le16(id);
913
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100914 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500915}
916
917static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
918 struct nvme_queue *nvmeq)
919{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500920 struct nvme_command c;
921 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
922
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200923 /*
924 * Note: we (ab)use the fact the the prp fields survive if no data
925 * is attached to the request.
926 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500927 memset(&c, 0, sizeof(c));
928 c.create_cq.opcode = nvme_admin_create_cq;
929 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
930 c.create_cq.cqid = cpu_to_le16(qid);
931 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
932 c.create_cq.cq_flags = cpu_to_le16(flags);
933 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
934
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100935 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500936}
937
938static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
939 struct nvme_queue *nvmeq)
940{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500941 struct nvme_command c;
Keith Busch81c1cd92017-04-04 18:18:12 -0400942 int flags = NVME_QUEUE_PHYS_CONTIG;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500943
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200944 /*
945 * Note: we (ab)use the fact the the prp fields survive if no data
946 * is attached to the request.
947 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500948 memset(&c, 0, sizeof(c));
949 c.create_sq.opcode = nvme_admin_create_sq;
950 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
951 c.create_sq.sqid = cpu_to_le16(qid);
952 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
953 c.create_sq.sq_flags = cpu_to_le16(flags);
954 c.create_sq.cqid = cpu_to_le16(qid);
955
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100956 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500957}
958
959static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
960{
961 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
962}
963
964static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
965{
966 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
967}
968
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200969static void abort_endio(struct request *req, blk_status_t error)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400970{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100971 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
972 struct nvme_queue *nvmeq = iod->nvmeq;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400973
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200974 dev_warn(nvmeq->dev->ctrl.device,
975 "Abort status: 0x%x", nvme_req(req)->status);
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100976 atomic_inc(&nvmeq->dev->ctrl.abort_limit);
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100977 blk_mq_free_request(req);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200978}
979
Keith Buschb2a0eb12017-06-07 20:32:50 +0200980static bool nvme_should_reset(struct nvme_dev *dev, u32 csts)
981{
982
983 /* If true, indicates loss of adapter communication, possibly by a
984 * NVMe Subsystem reset.
985 */
986 bool nssro = dev->subsystem && (csts & NVME_CSTS_NSSRO);
987
988 /* If there is a reset ongoing, we shouldn't reset again. */
989 if (dev->ctrl.state == NVME_CTRL_RESETTING)
990 return false;
991
992 /* We shouldn't reset unless the controller is on fatal error state
993 * _or_ if we lost the communication with it.
994 */
995 if (!(csts & NVME_CSTS_CFS) && !nssro)
996 return false;
997
998 /* If PCI error recovery process is happening, we cannot reset or
999 * the recovery mechanism will surely fail.
1000 */
1001 if (pci_channel_offline(to_pci_dev(dev->dev)))
1002 return false;
1003
1004 return true;
1005}
1006
1007static void nvme_warn_reset(struct nvme_dev *dev, u32 csts)
1008{
1009 /* Read a config register to help see what died. */
1010 u16 pci_status;
1011 int result;
1012
1013 result = pci_read_config_word(to_pci_dev(dev->dev), PCI_STATUS,
1014 &pci_status);
1015 if (result == PCIBIOS_SUCCESSFUL)
1016 dev_warn(dev->ctrl.device,
1017 "controller is down; will reset: CSTS=0x%x, PCI_STATUS=0x%hx\n",
1018 csts, pci_status);
1019 else
1020 dev_warn(dev->ctrl.device,
1021 "controller is down; will reset: CSTS=0x%x, PCI_STATUS read failed (%d)\n",
1022 csts, result);
1023}
1024
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001025static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001026{
Christoph Hellwigf4800d62015-11-28 15:43:10 +01001027 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
1028 struct nvme_queue *nvmeq = iod->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001029 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001030 struct request *abort_req;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001031 struct nvme_command cmd;
Keith Buschb2a0eb12017-06-07 20:32:50 +02001032 u32 csts = readl(dev->bar + NVME_REG_CSTS);
1033
1034 /*
1035 * Reset immediately if the controller is failed
1036 */
1037 if (nvme_should_reset(dev, csts)) {
1038 nvme_warn_reset(dev, csts);
1039 nvme_dev_disable(dev, false);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001040 nvme_reset_ctrl(&dev->ctrl);
Keith Buschb2a0eb12017-06-07 20:32:50 +02001041 return BLK_EH_HANDLED;
1042 }
Keith Buschc30341d2013-12-10 13:10:38 -07001043
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001044 /*
Keith Busch7776db12017-02-24 17:59:28 -05001045 * Did we miss an interrupt?
1046 */
1047 if (__nvme_poll(nvmeq, req->tag)) {
1048 dev_warn(dev->ctrl.device,
1049 "I/O %d QID %d timeout, completion polled\n",
1050 req->tag, nvmeq->qid);
1051 return BLK_EH_HANDLED;
1052 }
1053
1054 /*
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001055 * Shutdown immediately if controller times out while starting. The
1056 * reset work will see the pci device disabled when it gets the forced
1057 * cancellation error. All outstanding requests are completed on
1058 * shutdown, so we return BLK_EH_HANDLED.
1059 */
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02001060 if (dev->ctrl.state == NVME_CTRL_RESETTING) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001061 dev_warn(dev->ctrl.device,
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001062 "I/O %d QID %d timeout, disable controller\n",
1063 req->tag, nvmeq->qid);
Keith Buscha5cdb682016-01-12 14:41:18 -07001064 nvme_dev_disable(dev, false);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001065 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001066 return BLK_EH_HANDLED;
Keith Buschc30341d2013-12-10 13:10:38 -07001067 }
1068
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001069 /*
1070 * Shutdown the controller immediately and schedule a reset if the
1071 * command was already aborted once before and still hasn't been
1072 * returned to the driver, or if this is the admin queue.
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001073 */
Christoph Hellwigf4800d62015-11-28 15:43:10 +01001074 if (!nvmeq->qid || iod->aborted) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001075 dev_warn(dev->ctrl.device,
Keith Busche1569a12015-11-26 12:11:07 +01001076 "I/O %d QID %d timeout, reset controller\n",
1077 req->tag, nvmeq->qid);
Keith Buscha5cdb682016-01-12 14:41:18 -07001078 nvme_dev_disable(dev, false);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001079 nvme_reset_ctrl(&dev->ctrl);
Keith Buschc30341d2013-12-10 13:10:38 -07001080
Keith Busche1569a12015-11-26 12:11:07 +01001081 /*
1082 * Mark the request as handled, since the inline shutdown
1083 * forces all outstanding requests to complete.
1084 */
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001085 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
Keith Busche1569a12015-11-26 12:11:07 +01001086 return BLK_EH_HANDLED;
Keith Buschc30341d2013-12-10 13:10:38 -07001087 }
Keith Buschc30341d2013-12-10 13:10:38 -07001088
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001089 if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) {
1090 atomic_inc(&dev->ctrl.abort_limit);
1091 return BLK_EH_RESET_TIMER;
1092 }
Keith Busch7bf7d772017-01-24 18:07:00 -05001093 iod->aborted = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001094
Keith Buschc30341d2013-12-10 13:10:38 -07001095 memset(&cmd, 0, sizeof(cmd));
1096 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001097 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001098 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Keith Buschc30341d2013-12-10 13:10:38 -07001099
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001100 dev_warn(nvmeq->dev->ctrl.device,
1101 "I/O %d QID %d timeout, aborting\n",
1102 req->tag, nvmeq->qid);
Keith Buschc30341d2013-12-10 13:10:38 -07001103
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001104 abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd,
Christoph Hellwigeb71f432016-06-13 16:45:23 +02001105 BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001106 if (IS_ERR(abort_req)) {
1107 atomic_inc(&dev->ctrl.abort_limit);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001108 return BLK_EH_RESET_TIMER;
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001109 }
Keith Buschc30341d2013-12-10 13:10:38 -07001110
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001111 abort_req->timeout = ADMIN_TIMEOUT;
1112 abort_req->end_io_data = NULL;
1113 blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio);
Keith Busch07836e62015-02-19 10:34:48 -07001114
Keith Busch7a509a62015-01-07 18:55:53 -07001115 /*
1116 * The aborted req will be completed on receiving the abort req.
1117 * We enable the timer again. If hit twice, it'll cause a device reset,
1118 * as the device then is in a faulty state.
1119 */
Keith Busch07836e62015-02-19 10:34:48 -07001120 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001121}
1122
Keith Buschf435c282014-07-07 09:14:42 -06001123static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001124{
1125 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1126 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001127 if (nvmeq->sq_cmds)
1128 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001129 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1130 kfree(nvmeq);
1131}
1132
Keith Buscha1a5ef92013-12-16 13:50:00 -05001133static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001134{
1135 int i;
1136
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001137 for (i = dev->ctrl.queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001138 struct nvme_queue *nvmeq = dev->queues[i];
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001139 dev->ctrl.queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001140 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001141 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001142 }
Keith Busch22404272013-07-15 15:02:20 -06001143}
1144
Keith Busch4d115422013-12-10 13:10:40 -07001145/**
1146 * nvme_suspend_queue - put queue into suspended state
1147 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001148 */
1149static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001150{
Keith Busch2b25d982014-12-22 12:59:04 -07001151 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001152
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001153 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001154 if (nvmeq->cq_vector == -1) {
1155 spin_unlock_irq(&nvmeq->q_lock);
1156 return 1;
1157 }
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001158 vector = nvmeq->cq_vector;
Keith Busch42f61422014-03-24 10:46:25 -06001159 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001160 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001161 spin_unlock_irq(&nvmeq->q_lock);
1162
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001163 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
Sagi Grimbergc81545f2017-07-02 15:53:27 +03001164 blk_mq_quiesce_queue(nvmeq->dev->ctrl.admin_q);
Keith Busch6df3dbc2015-03-26 13:49:33 -06001165
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001166 pci_free_irq(to_pci_dev(nvmeq->dev->dev), vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001167
Keith Busch4d115422013-12-10 13:10:40 -07001168 return 0;
1169}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001170
Keith Buscha5cdb682016-01-12 14:41:18 -07001171static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown)
Keith Busch4d115422013-12-10 13:10:40 -07001172{
Keith Buscha5cdb682016-01-12 14:41:18 -07001173 struct nvme_queue *nvmeq = dev->queues[0];
Keith Busch4d115422013-12-10 13:10:40 -07001174
1175 if (!nvmeq)
1176 return;
1177 if (nvme_suspend_queue(nvmeq))
1178 return;
1179
Keith Buscha5cdb682016-01-12 14:41:18 -07001180 if (shutdown)
1181 nvme_shutdown_ctrl(&dev->ctrl);
1182 else
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001183 nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
Keith Busch07836e62015-02-19 10:34:48 -07001184
1185 spin_lock_irq(&nvmeq->q_lock);
1186 nvme_process_cq(nvmeq);
1187 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001188}
1189
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001190static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1191 int entry_size)
1192{
1193 int q_depth = dev->q_depth;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001194 unsigned q_size_aligned = roundup(q_depth * entry_size,
1195 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001196
1197 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001198 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001199 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
Jon Derrickc45f5c92015-07-21 15:08:13 -06001200 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001201
1202 /*
1203 * Ensure the reduced q_depth is above some threshold where it
1204 * would be better to map queues in system memory with the
1205 * original depth
1206 */
1207 if (q_depth < 64)
1208 return -ENOMEM;
1209 }
1210
1211 return q_depth;
1212}
1213
1214static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1215 int qid, int depth)
1216{
1217 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001218 unsigned offset = (qid - 1) * roundup(SQ_SIZE(depth),
1219 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001220 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1221 nvmeq->sq_cmds_io = dev->cmb + offset;
1222 } else {
1223 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1224 &nvmeq->sq_dma_addr, GFP_KERNEL);
1225 if (!nvmeq->sq_cmds)
1226 return -ENOMEM;
1227 }
1228
1229 return 0;
1230}
1231
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001232static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001233 int depth, int node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001234{
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001235 struct nvme_queue *nvmeq = kzalloc_node(sizeof(*nvmeq), GFP_KERNEL,
1236 node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001237 if (!nvmeq)
1238 return NULL;
1239
Christoph Hellwige75ec752015-05-22 11:12:39 +02001240 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001241 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001242 if (!nvmeq->cqes)
1243 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001244
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001245 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001246 goto free_cqdma;
1247
Christoph Hellwige75ec752015-05-22 11:12:39 +02001248 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001249 nvmeq->dev = dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001250 spin_lock_init(&nvmeq->q_lock);
1251 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001252 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001253 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001254 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001255 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001256 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001257 dev->queues[qid] = nvmeq;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001258 dev->ctrl.queue_count++;
Jon Derrick36a7e992015-05-27 12:26:23 -06001259
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001260 return nvmeq;
1261
1262 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001263 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001264 nvmeq->cq_dma_addr);
1265 free_nvmeq:
1266 kfree(nvmeq);
1267 return NULL;
1268}
1269
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001270static int queue_request_irq(struct nvme_queue *nvmeq)
Matthew Wilcox30010822011-01-20 09:10:15 -05001271{
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001272 struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev);
1273 int nr = nvmeq->dev->ctrl.instance;
1274
1275 if (use_threaded_interrupts) {
1276 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq_check,
1277 nvme_irq, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1278 } else {
1279 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq,
1280 NULL, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1281 }
Matthew Wilcox30010822011-01-20 09:10:15 -05001282}
1283
Keith Busch22404272013-07-15 15:02:20 -06001284static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001285{
Keith Busch22404272013-07-15 15:02:20 -06001286 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001287
Keith Busch7be50e92014-09-10 15:48:47 -06001288 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001289 nvmeq->sq_tail = 0;
1290 nvmeq->cq_head = 0;
1291 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001292 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001293 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Helen Koikef9f38e32017-04-10 12:51:07 -03001294 nvme_dbbuf_init(dev, nvmeq, qid);
Keith Busch42f61422014-03-24 10:46:25 -06001295 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001296 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001297}
1298
1299static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1300{
1301 struct nvme_dev *dev = nvmeq->dev;
1302 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001303
Keith Busch2b25d982014-12-22 12:59:04 -07001304 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001305 result = adapter_alloc_cq(dev, qid, nvmeq);
1306 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001307 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001308
1309 result = adapter_alloc_sq(dev, qid, nvmeq);
1310 if (result < 0)
1311 goto release_cq;
1312
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001313 result = queue_request_irq(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001314 if (result < 0)
1315 goto release_sq;
1316
Keith Busch22404272013-07-15 15:02:20 -06001317 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001318 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001319
1320 release_sq:
1321 adapter_delete_sq(dev, qid);
1322 release_cq:
1323 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001324 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001325}
1326
Eric Biggersf363b082017-03-30 13:39:16 -07001327static const struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001328 .queue_rq = nvme_queue_rq,
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001329 .complete = nvme_pci_complete_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001330 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001331 .exit_hctx = nvme_admin_exit_hctx,
Christoph Hellwig03508152017-06-13 09:15:18 +02001332 .init_request = nvme_init_request,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001333 .timeout = nvme_timeout,
1334};
1335
Eric Biggersf363b082017-03-30 13:39:16 -07001336static const struct blk_mq_ops nvme_mq_ops = {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001337 .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_init_hctx,
1340 .init_request = nvme_init_request,
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001341 .map_queues = nvme_pci_map_queues,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001342 .timeout = nvme_timeout,
Jens Axboea0fa9642015-11-03 20:37:26 -07001343 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001344};
1345
Keith Buschea191d22015-01-07 18:55:49 -07001346static void nvme_dev_remove_admin(struct nvme_dev *dev)
1347{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001348 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
Keith Busch69d9a992016-02-24 09:15:56 -07001349 /*
1350 * If the controller was reset during removal, it's possible
1351 * user requests may be waiting on a stopped queue. Start the
1352 * queue to flush these to completion.
1353 */
Sagi Grimbergc81545f2017-07-02 15:53:27 +03001354 blk_mq_unquiesce_queue(dev->ctrl.admin_q);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001355 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001356 blk_mq_free_tag_set(&dev->admin_tagset);
1357 }
1358}
1359
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001360static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1361{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001362 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001363 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1364 dev->admin_tagset.nr_hw_queues = 1;
Keith Busche3e9d502016-01-04 09:10:55 -07001365
1366 /*
1367 * Subtract one to leave an empty queue entry for 'Full Queue'
1368 * condition. See NVM-Express 1.2 specification, section 4.1.2.
1369 */
1370 dev->admin_tagset.queue_depth = NVME_AQ_BLKMQ_DEPTH - 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001371 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001372 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001373 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Jens Axboed3484992017-01-13 14:43:58 -07001374 dev->admin_tagset.flags = BLK_MQ_F_NO_SCHED;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001375 dev->admin_tagset.driver_data = dev;
1376
1377 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1378 return -ENOMEM;
Sagi Grimberg34b6c232017-07-10 09:22:29 +03001379 dev->ctrl.admin_tagset = &dev->admin_tagset;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001380
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001381 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1382 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001383 blk_mq_free_tag_set(&dev->admin_tagset);
1384 return -ENOMEM;
1385 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001386 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001387 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001388 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001389 return -ENODEV;
1390 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001391 } else
Sagi Grimbergc81545f2017-07-02 15:53:27 +03001392 blk_mq_unquiesce_queue(dev->ctrl.admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001393
1394 return 0;
1395}
1396
Xu Yu97f6ef62017-05-24 16:39:55 +08001397static unsigned long db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1398{
1399 return NVME_REG_DBS + ((nr_io_queues + 1) * 8 * dev->db_stride);
1400}
1401
1402static int nvme_remap_bar(struct nvme_dev *dev, unsigned long size)
1403{
1404 struct pci_dev *pdev = to_pci_dev(dev->dev);
1405
1406 if (size <= dev->bar_mapped_size)
1407 return 0;
1408 if (size > pci_resource_len(pdev, 0))
1409 return -ENOMEM;
1410 if (dev->bar)
1411 iounmap(dev->bar);
1412 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1413 if (!dev->bar) {
1414 dev->bar_mapped_size = 0;
1415 return -ENOMEM;
1416 }
1417 dev->bar_mapped_size = size;
1418 dev->dbs = dev->bar + NVME_REG_DBS;
1419
1420 return 0;
1421}
1422
Sagi Grimberg01ad0992017-05-01 00:27:17 +03001423static int nvme_pci_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001424{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001425 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001426 u32 aqa;
1427 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001428
Xu Yu97f6ef62017-05-24 16:39:55 +08001429 result = nvme_remap_bar(dev, db_bar_size(dev, 0));
1430 if (result < 0)
1431 return result;
1432
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001433 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1, 0) ?
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001434 NVME_CAP_NSSRC(dev->ctrl.cap) : 0;
Keith Buschdfbac8c2015-08-10 15:20:40 -06001435
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001436 if (dev->subsystem &&
1437 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1438 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001439
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001440 result = nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001441 if (result < 0)
1442 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001443
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001444 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001445 if (!nvmeq) {
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001446 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH,
1447 dev_to_node(dev->dev));
Keith Buschcd638942013-07-15 15:02:23 -06001448 if (!nvmeq)
1449 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001450 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001451
1452 aqa = nvmeq->q_depth - 1;
1453 aqa |= aqa << 16;
1454
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001455 writel(aqa, dev->bar + NVME_REG_AQA);
1456 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1457 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Keith Busch1d090622014-06-23 11:34:01 -06001458
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001459 result = nvme_enable_ctrl(&dev->ctrl, dev->ctrl.cap);
Keith Busch025c5572013-05-01 13:07:51 -06001460 if (result)
Keith Buschd4875622016-11-15 15:56:26 -05001461 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001462
Keith Busch2b25d982014-12-22 12:59:04 -07001463 nvmeq->cq_vector = 0;
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001464 result = queue_request_irq(nvmeq);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001465 if (result) {
1466 nvmeq->cq_vector = -1;
Keith Buschd4875622016-11-15 15:56:26 -05001467 return result;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001468 }
Keith Busch025c5572013-05-01 13:07:51 -06001469
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001470 return result;
1471}
1472
Christoph Hellwig749941f2015-11-26 11:46:39 +01001473static int nvme_create_io_queues(struct nvme_dev *dev)
Keith Busch42f61422014-03-24 10:46:25 -06001474{
Keith Busch949928c2015-12-17 17:08:15 -07001475 unsigned i, max;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001476 int ret = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001477
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001478 for (i = dev->ctrl.queue_count; i <= dev->max_qid; i++) {
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001479 /* vector == qid - 1, match nvme_create_queue */
1480 if (!nvme_alloc_queue(dev, i, dev->q_depth,
1481 pci_irq_get_node(to_pci_dev(dev->dev), i - 1))) {
Christoph Hellwig749941f2015-11-26 11:46:39 +01001482 ret = -ENOMEM;
Keith Busch42f61422014-03-24 10:46:25 -06001483 break;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001484 }
1485 }
Keith Busch42f61422014-03-24 10:46:25 -06001486
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001487 max = min(dev->max_qid, dev->ctrl.queue_count - 1);
Keith Busch949928c2015-12-17 17:08:15 -07001488 for (i = dev->online_queues; i <= max; i++) {
Christoph Hellwig749941f2015-11-26 11:46:39 +01001489 ret = nvme_create_queue(dev->queues[i], i);
Keith Buschd4875622016-11-15 15:56:26 -05001490 if (ret)
Keith Busch42f61422014-03-24 10:46:25 -06001491 break;
Matthew Wilcox27e81662014-04-11 11:58:45 -04001492 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001493
1494 /*
1495 * Ignore failing Create SQ/CQ commands, we can continue with less
1496 * than the desired aount of queues, and even a controller without
1497 * I/O queues an still be used to issue admin commands. This might
1498 * be useful to upgrade a buggy firmware for example.
1499 */
1500 return ret >= 0 ? 0 : ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001501}
1502
Stephen Bates202021c2016-10-05 20:01:12 -06001503static ssize_t nvme_cmb_show(struct device *dev,
1504 struct device_attribute *attr,
1505 char *buf)
1506{
1507 struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
1508
Stephen Batesc9658092016-12-16 11:54:50 -07001509 return scnprintf(buf, PAGE_SIZE, "cmbloc : x%08x\ncmbsz : x%08x\n",
Stephen Bates202021c2016-10-05 20:01:12 -06001510 ndev->cmbloc, ndev->cmbsz);
1511}
1512static DEVICE_ATTR(cmb, S_IRUGO, nvme_cmb_show, NULL);
1513
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001514static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1515{
1516 u64 szu, size, offset;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001517 resource_size_t bar_size;
1518 struct pci_dev *pdev = to_pci_dev(dev->dev);
1519 void __iomem *cmb;
1520 dma_addr_t dma_addr;
1521
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001522 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001523 if (!(NVME_CMB_SZ(dev->cmbsz)))
1524 return NULL;
Stephen Bates202021c2016-10-05 20:01:12 -06001525 dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001526
Stephen Bates202021c2016-10-05 20:01:12 -06001527 if (!use_cmb_sqes)
1528 return NULL;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001529
1530 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1531 size = szu * NVME_CMB_SZ(dev->cmbsz);
Stephen Bates202021c2016-10-05 20:01:12 -06001532 offset = szu * NVME_CMB_OFST(dev->cmbloc);
1533 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(dev->cmbloc));
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001534
1535 if (offset > bar_size)
1536 return NULL;
1537
1538 /*
1539 * Controllers may support a CMB size larger than their BAR,
1540 * for example, due to being behind a bridge. Reduce the CMB to
1541 * the reported size of the BAR
1542 */
1543 if (size > bar_size - offset)
1544 size = bar_size - offset;
1545
Stephen Bates202021c2016-10-05 20:01:12 -06001546 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(dev->cmbloc)) + offset;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001547 cmb = ioremap_wc(dma_addr, size);
1548 if (!cmb)
1549 return NULL;
1550
1551 dev->cmb_dma_addr = dma_addr;
1552 dev->cmb_size = size;
1553 return cmb;
1554}
1555
1556static inline void nvme_release_cmb(struct nvme_dev *dev)
1557{
1558 if (dev->cmb) {
1559 iounmap(dev->cmb);
1560 dev->cmb = NULL;
Max Gurtovoy1c78f772017-07-30 01:45:08 +03001561 sysfs_remove_file_from_group(&dev->ctrl.device->kobj,
1562 &dev_attr_cmb.attr, NULL);
1563 dev->cmbsz = 0;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001564 }
1565}
1566
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001567static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits)
Keith Busch9d713c22013-07-15 15:02:24 -06001568{
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001569 size_t len = dev->nr_host_mem_descs * sizeof(*dev->host_mem_descs);
1570 struct nvme_command c;
1571 u64 dma_addr;
1572 int ret;
1573
1574 dma_addr = dma_map_single(dev->dev, dev->host_mem_descs, len,
1575 DMA_TO_DEVICE);
1576 if (dma_mapping_error(dev->dev, dma_addr))
1577 return -ENOMEM;
1578
1579 memset(&c, 0, sizeof(c));
1580 c.features.opcode = nvme_admin_set_features;
1581 c.features.fid = cpu_to_le32(NVME_FEAT_HOST_MEM_BUF);
1582 c.features.dword11 = cpu_to_le32(bits);
1583 c.features.dword12 = cpu_to_le32(dev->host_mem_size >>
1584 ilog2(dev->ctrl.page_size));
1585 c.features.dword13 = cpu_to_le32(lower_32_bits(dma_addr));
1586 c.features.dword14 = cpu_to_le32(upper_32_bits(dma_addr));
1587 c.features.dword15 = cpu_to_le32(dev->nr_host_mem_descs);
1588
1589 ret = nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1590 if (ret) {
1591 dev_warn(dev->ctrl.device,
1592 "failed to set host mem (err %d, flags %#x).\n",
1593 ret, bits);
1594 }
1595 dma_unmap_single(dev->dev, dma_addr, len, DMA_TO_DEVICE);
1596 return ret;
1597}
1598
1599static void nvme_free_host_mem(struct nvme_dev *dev)
1600{
1601 int i;
1602
1603 for (i = 0; i < dev->nr_host_mem_descs; i++) {
1604 struct nvme_host_mem_buf_desc *desc = &dev->host_mem_descs[i];
1605 size_t size = le32_to_cpu(desc->size) * dev->ctrl.page_size;
1606
1607 dma_free_coherent(dev->dev, size, dev->host_mem_desc_bufs[i],
1608 le64_to_cpu(desc->addr));
1609 }
1610
1611 kfree(dev->host_mem_desc_bufs);
1612 dev->host_mem_desc_bufs = NULL;
1613 kfree(dev->host_mem_descs);
1614 dev->host_mem_descs = NULL;
1615}
1616
1617static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
1618{
1619 struct nvme_host_mem_buf_desc *descs;
Christoph Hellwig50cdb7c2017-07-25 17:39:07 +02001620 u32 chunk_size, max_entries, len;
Dan Carpenter2ee0e4e2017-07-06 12:26:52 +03001621 int i = 0;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001622 void **bufs;
Dan Carpenter2ee0e4e2017-07-06 12:26:52 +03001623 u64 size = 0, tmp;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001624
1625 /* start big and work our way down */
1626 chunk_size = min(preferred, (u64)PAGE_SIZE << MAX_ORDER);
1627retry:
1628 tmp = (preferred + chunk_size - 1);
1629 do_div(tmp, chunk_size);
1630 max_entries = tmp;
1631 descs = kcalloc(max_entries, sizeof(*descs), GFP_KERNEL);
1632 if (!descs)
1633 goto out;
1634
1635 bufs = kcalloc(max_entries, sizeof(*bufs), GFP_KERNEL);
1636 if (!bufs)
1637 goto out_free_descs;
1638
Christoph Hellwig50cdb7c2017-07-25 17:39:07 +02001639 for (size = 0; size < preferred; size += len) {
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001640 dma_addr_t dma_addr;
1641
Christoph Hellwig50cdb7c2017-07-25 17:39:07 +02001642 len = min_t(u64, chunk_size, preferred - size);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001643 bufs[i] = dma_alloc_attrs(dev->dev, len, &dma_addr, GFP_KERNEL,
1644 DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
1645 if (!bufs[i])
1646 break;
1647
1648 descs[i].addr = cpu_to_le64(dma_addr);
1649 descs[i].size = cpu_to_le32(len / dev->ctrl.page_size);
1650 i++;
1651 }
1652
1653 if (!size || (min && size < min)) {
1654 dev_warn(dev->ctrl.device,
1655 "failed to allocate host memory buffer.\n");
1656 goto out_free_bufs;
1657 }
1658
1659 dev_info(dev->ctrl.device,
1660 "allocated %lld MiB host memory buffer.\n",
1661 size >> ilog2(SZ_1M));
1662 dev->nr_host_mem_descs = i;
1663 dev->host_mem_size = size;
1664 dev->host_mem_descs = descs;
1665 dev->host_mem_desc_bufs = bufs;
1666 return 0;
1667
1668out_free_bufs:
1669 while (--i >= 0) {
1670 size_t size = le32_to_cpu(descs[i].size) * dev->ctrl.page_size;
1671
1672 dma_free_coherent(dev->dev, size, bufs[i],
1673 le64_to_cpu(descs[i].addr));
1674 }
1675
1676 kfree(bufs);
1677out_free_descs:
1678 kfree(descs);
1679out:
1680 /* try a smaller chunk size if we failed early */
1681 if (chunk_size >= PAGE_SIZE * 2 && (i == 0 || size < min)) {
1682 chunk_size /= 2;
1683 goto retry;
1684 }
1685 dev->host_mem_descs = NULL;
1686 return -ENOMEM;
1687}
1688
1689static void nvme_setup_host_mem(struct nvme_dev *dev)
1690{
1691 u64 max = (u64)max_host_mem_size_mb * SZ_1M;
1692 u64 preferred = (u64)dev->ctrl.hmpre * 4096;
1693 u64 min = (u64)dev->ctrl.hmmin * 4096;
1694 u32 enable_bits = NVME_HOST_MEM_ENABLE;
1695
1696 preferred = min(preferred, max);
1697 if (min > max) {
1698 dev_warn(dev->ctrl.device,
1699 "min host memory (%lld MiB) above limit (%d MiB).\n",
1700 min >> ilog2(SZ_1M), max_host_mem_size_mb);
1701 nvme_free_host_mem(dev);
1702 return;
1703 }
1704
1705 /*
1706 * If we already have a buffer allocated check if we can reuse it.
1707 */
1708 if (dev->host_mem_descs) {
1709 if (dev->host_mem_size >= min)
1710 enable_bits |= NVME_HOST_MEM_RETURN;
1711 else
1712 nvme_free_host_mem(dev);
1713 }
1714
1715 if (!dev->host_mem_descs) {
1716 if (nvme_alloc_host_mem(dev, min, preferred))
1717 return;
1718 }
1719
1720 if (nvme_set_host_mem(dev, enable_bits))
1721 nvme_free_host_mem(dev);
Keith Busch9d713c22013-07-15 15:02:24 -06001722}
1723
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001724static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001725{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001726 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02001727 struct pci_dev *pdev = to_pci_dev(dev->dev);
Xu Yu97f6ef62017-05-24 16:39:55 +08001728 int result, nr_io_queues;
1729 unsigned long size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001730
Christoph Hellwig425a17c2017-06-26 12:20:58 +02001731 nr_io_queues = num_present_cpus();
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001732 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
1733 if (result < 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001734 return result;
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001735
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001736 if (nr_io_queues == 0)
Keith Buscha5229052016-04-08 16:09:10 -06001737 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001738
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001739 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1740 result = nvme_cmb_qdepth(dev, nr_io_queues,
1741 sizeof(struct nvme_command));
1742 if (result > 0)
1743 dev->q_depth = result;
1744 else
1745 nvme_release_cmb(dev);
1746 }
1747
Xu Yu97f6ef62017-05-24 16:39:55 +08001748 do {
1749 size = db_bar_size(dev, nr_io_queues);
1750 result = nvme_remap_bar(dev, size);
1751 if (!result)
1752 break;
1753 if (!--nr_io_queues)
1754 return -ENOMEM;
1755 } while (1);
1756 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001757
Keith Busch9d713c22013-07-15 15:02:24 -06001758 /* Deregister the admin queue's interrupt */
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001759 pci_free_irq(pdev, 0, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001760
Jens Axboee32efbf2014-11-14 09:49:26 -07001761 /*
1762 * If we enable msix early due to not intx, disable it again before
1763 * setting up the full range we need.
1764 */
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001765 pci_free_irq_vectors(pdev);
1766 nr_io_queues = pci_alloc_irq_vectors(pdev, 1, nr_io_queues,
1767 PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY);
1768 if (nr_io_queues <= 0)
1769 return -EIO;
1770 dev->max_qid = nr_io_queues;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001771
Matthew Wilcox063a8092013-06-20 10:53:48 -04001772 /*
1773 * Should investigate if there's a performance win from allocating
1774 * more queues than interrupt vectors; it might allow the submission
1775 * path to scale better, even if the receive path is limited by the
1776 * number of interrupts.
1777 */
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001778
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001779 result = queue_request_irq(adminq);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001780 if (result) {
1781 adminq->cq_vector = -1;
Keith Buschd4875622016-11-15 15:56:26 -05001782 return result;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001783 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001784 return nvme_create_io_queues(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001785}
1786
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001787static void nvme_del_queue_end(struct request *req, blk_status_t error)
Keith Buschdb3cbff2016-01-12 14:41:17 -07001788{
1789 struct nvme_queue *nvmeq = req->end_io_data;
1790
1791 blk_mq_free_request(req);
1792 complete(&nvmeq->dev->ioq_wait);
1793}
1794
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001795static void nvme_del_cq_end(struct request *req, blk_status_t error)
Keith Buschdb3cbff2016-01-12 14:41:17 -07001796{
1797 struct nvme_queue *nvmeq = req->end_io_data;
1798
1799 if (!error) {
1800 unsigned long flags;
1801
Ming Lin2e39e0f2016-04-05 10:32:04 -07001802 /*
1803 * We might be called with the AQ q_lock held
1804 * and the I/O queue q_lock should always
1805 * nest inside the AQ one.
1806 */
1807 spin_lock_irqsave_nested(&nvmeq->q_lock, flags,
1808 SINGLE_DEPTH_NESTING);
Keith Buschdb3cbff2016-01-12 14:41:17 -07001809 nvme_process_cq(nvmeq);
1810 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
1811 }
1812
1813 nvme_del_queue_end(req, error);
1814}
1815
1816static int nvme_delete_queue(struct nvme_queue *nvmeq, u8 opcode)
1817{
1818 struct request_queue *q = nvmeq->dev->ctrl.admin_q;
1819 struct request *req;
1820 struct nvme_command cmd;
1821
1822 memset(&cmd, 0, sizeof(cmd));
1823 cmd.delete_queue.opcode = opcode;
1824 cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid);
1825
Christoph Hellwigeb71f432016-06-13 16:45:23 +02001826 req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
Keith Buschdb3cbff2016-01-12 14:41:17 -07001827 if (IS_ERR(req))
1828 return PTR_ERR(req);
1829
1830 req->timeout = ADMIN_TIMEOUT;
1831 req->end_io_data = nvmeq;
1832
1833 blk_execute_rq_nowait(q, NULL, req, false,
1834 opcode == nvme_admin_delete_cq ?
1835 nvme_del_cq_end : nvme_del_queue_end);
1836 return 0;
1837}
1838
Keith Busch70659062016-10-12 09:22:16 -06001839static void nvme_disable_io_queues(struct nvme_dev *dev, int queues)
Keith Buschdb3cbff2016-01-12 14:41:17 -07001840{
Keith Busch70659062016-10-12 09:22:16 -06001841 int pass;
Keith Buschdb3cbff2016-01-12 14:41:17 -07001842 unsigned long timeout;
1843 u8 opcode = nvme_admin_delete_sq;
1844
1845 for (pass = 0; pass < 2; pass++) {
Keith Busch014a0d62016-05-06 11:50:52 -06001846 int sent = 0, i = queues;
Keith Buschdb3cbff2016-01-12 14:41:17 -07001847
1848 reinit_completion(&dev->ioq_wait);
1849 retry:
1850 timeout = ADMIN_TIMEOUT;
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06001851 for (; i > 0; i--, sent++)
1852 if (nvme_delete_queue(dev->queues[i], opcode))
Keith Buschdb3cbff2016-01-12 14:41:17 -07001853 break;
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06001854
Keith Buschdb3cbff2016-01-12 14:41:17 -07001855 while (sent--) {
1856 timeout = wait_for_completion_io_timeout(&dev->ioq_wait, timeout);
1857 if (timeout == 0)
1858 return;
1859 if (i)
1860 goto retry;
1861 }
1862 opcode = nvme_admin_delete_cq;
1863 }
1864}
1865
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001866/*
1867 * Return: error value if an error occurred setting up the queues or calling
1868 * Identify Device. 0 if these succeeded, even if adding some of the
1869 * namespaces failed. At the moment, these failures are silent. TBD which
1870 * failures should be reported.
1871 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001872static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001873{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001874 if (!dev->ctrl.tagset) {
Keith Buschffe77042015-06-08 10:08:15 -06001875 dev->tagset.ops = &nvme_mq_ops;
1876 dev->tagset.nr_hw_queues = dev->online_queues - 1;
1877 dev->tagset.timeout = NVME_IO_TIMEOUT;
1878 dev->tagset.numa_node = dev_to_node(dev->dev);
1879 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001880 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06001881 dev->tagset.cmd_size = nvme_cmd_size(dev);
1882 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
1883 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001884
Keith Buschffe77042015-06-08 10:08:15 -06001885 if (blk_mq_alloc_tag_set(&dev->tagset))
1886 return 0;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001887 dev->ctrl.tagset = &dev->tagset;
Helen Koikef9f38e32017-04-10 12:51:07 -03001888
1889 nvme_dbbuf_set(dev);
Keith Busch949928c2015-12-17 17:08:15 -07001890 } else {
1891 blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1);
1892
1893 /* Free previously allocated queues that are no longer usable */
1894 nvme_free_queues(dev, dev->online_queues);
Keith Buschffe77042015-06-08 10:08:15 -06001895 }
Keith Busch949928c2015-12-17 17:08:15 -07001896
Keith Busche1e5e562015-02-19 13:39:03 -07001897 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001898}
1899
Keith Buschb00a7262016-02-24 09:15:52 -07001900static int nvme_pci_enable(struct nvme_dev *dev)
Keith Busch0877cb02013-07-15 15:02:19 -06001901{
Keith Buschb00a7262016-02-24 09:15:52 -07001902 int result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001903 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001904
1905 if (pci_enable_device_mem(pdev))
1906 return result;
1907
Keith Busch0877cb02013-07-15 15:02:19 -06001908 pci_set_master(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001909
Christoph Hellwige75ec752015-05-22 11:12:39 +02001910 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
1911 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01001912 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06001913
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001914 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07001915 result = -ENODEV;
Keith Buschb00a7262016-02-24 09:15:52 -07001916 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07001917 }
Jens Axboee32efbf2014-11-14 09:49:26 -07001918
1919 /*
Keith Buscha5229052016-04-08 16:09:10 -06001920 * Some devices and/or platforms don't advertise or work with INTx
1921 * interrupts. Pre-enable a single MSIX or MSI vec for setup. We'll
1922 * adjust this later.
Jens Axboee32efbf2014-11-14 09:49:26 -07001923 */
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001924 result = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
1925 if (result < 0)
1926 return result;
Jens Axboee32efbf2014-11-14 09:49:26 -07001927
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001928 dev->ctrl.cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001929
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001930 dev->q_depth = min_t(int, NVME_CAP_MQES(dev->ctrl.cap) + 1,
weiping zhangb27c1e62017-07-10 16:46:59 +08001931 io_queue_depth);
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001932 dev->db_stride = 1 << NVME_CAP_STRIDE(dev->ctrl.cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001933 dev->dbs = dev->bar + 4096;
Stephan Günther1f390c12015-12-01 13:23:22 -07001934
1935 /*
1936 * Temporary fix for the Apple controller found in the MacBook8,1 and
1937 * some MacBook7,1 to avoid controller resets and data loss.
1938 */
1939 if (pdev->vendor == PCI_VENDOR_ID_APPLE && pdev->device == 0x2001) {
1940 dev->q_depth = 2;
Christoph Hellwig9bdcfb12017-05-20 15:14:43 +02001941 dev_warn(dev->ctrl.device, "detected Apple NVMe controller, "
1942 "set queue depth=%u to work around controller resets\n",
Stephan Günther1f390c12015-12-01 13:23:22 -07001943 dev->q_depth);
Martin K. Petersend554b5e2017-06-27 22:27:57 -04001944 } else if (pdev->vendor == PCI_VENDOR_ID_SAMSUNG &&
1945 (pdev->device == 0xa821 || pdev->device == 0xa822) &&
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001946 NVME_CAP_MQES(dev->ctrl.cap) == 0) {
Martin K. Petersend554b5e2017-06-27 22:27:57 -04001947 dev->q_depth = 64;
1948 dev_err(dev->ctrl.device, "detected PM1725 NVMe controller, "
1949 "set queue depth=%u\n", dev->q_depth);
Stephan Günther1f390c12015-12-01 13:23:22 -07001950 }
1951
Stephen Bates202021c2016-10-05 20:01:12 -06001952 /*
1953 * CMBs can currently only exist on >=1.2 PCIe devices. We only
Max Gurtovoy1c78f772017-07-30 01:45:08 +03001954 * populate sysfs if a CMB is implemented. Since nvme_dev_attrs_group
1955 * has no name we can pass NULL as final argument to
1956 * sysfs_add_file_to_group.
Stephen Bates202021c2016-10-05 20:01:12 -06001957 */
1958
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001959 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2, 0)) {
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001960 dev->cmb = nvme_map_cmb(dev);
Max Gurtovoy1c78f772017-07-30 01:45:08 +03001961 if (dev->cmb) {
Stephen Bates202021c2016-10-05 20:01:12 -06001962 if (sysfs_add_file_to_group(&dev->ctrl.device->kobj,
1963 &dev_attr_cmb.attr, NULL))
Christoph Hellwig9bdcfb12017-05-20 15:14:43 +02001964 dev_warn(dev->ctrl.device,
Stephen Bates202021c2016-10-05 20:01:12 -06001965 "failed to add sysfs attribute for CMB\n");
1966 }
1967 }
1968
Keith Buscha0a34082015-12-07 15:30:31 -07001969 pci_enable_pcie_error_reporting(pdev);
1970 pci_save_state(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001971 return 0;
1972
1973 disable:
Keith Busch0877cb02013-07-15 15:02:19 -06001974 pci_disable_device(pdev);
1975 return result;
1976}
1977
1978static void nvme_dev_unmap(struct nvme_dev *dev)
1979{
Keith Buschb00a7262016-02-24 09:15:52 -07001980 if (dev->bar)
1981 iounmap(dev->bar);
Johannes Thumshirna1f447b2016-06-07 09:44:02 +02001982 pci_release_mem_regions(to_pci_dev(dev->dev));
Keith Buschb00a7262016-02-24 09:15:52 -07001983}
1984
1985static void nvme_pci_disable(struct nvme_dev *dev)
1986{
Christoph Hellwige75ec752015-05-22 11:12:39 +02001987 struct pci_dev *pdev = to_pci_dev(dev->dev);
1988
Jon Derrickf63572d2017-05-05 14:52:06 -06001989 nvme_release_cmb(dev);
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001990 pci_free_irq_vectors(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001991
Keith Buscha0a34082015-12-07 15:30:31 -07001992 if (pci_is_enabled(pdev)) {
1993 pci_disable_pcie_error_reporting(pdev);
Christoph Hellwige75ec752015-05-22 11:12:39 +02001994 pci_disable_device(pdev);
Keith Busch4d115422013-12-10 13:10:40 -07001995 }
Keith Busch4d115422013-12-10 13:10:40 -07001996}
1997
Keith Buscha5cdb682016-01-12 14:41:18 -07001998static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001999{
Keith Busch70659062016-10-12 09:22:16 -06002000 int i, queues;
Keith Busch302ad8c2017-03-01 14:22:12 -05002001 bool dead = true;
2002 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch22404272013-07-15 15:02:20 -06002003
Keith Busch77bf25e2015-11-26 12:21:29 +01002004 mutex_lock(&dev->shutdown_lock);
Keith Busch302ad8c2017-03-01 14:22:12 -05002005 if (pci_is_enabled(pdev)) {
2006 u32 csts = readl(dev->bar + NVME_REG_CSTS);
2007
Keith Buschebef7362017-06-27 17:44:05 -06002008 if (dev->ctrl.state == NVME_CTRL_LIVE ||
2009 dev->ctrl.state == NVME_CTRL_RESETTING)
Keith Busch302ad8c2017-03-01 14:22:12 -05002010 nvme_start_freeze(&dev->ctrl);
2011 dead = !!((csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) ||
2012 pdev->error_state != pci_channel_io_normal);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002013 }
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06002014
Keith Busch302ad8c2017-03-01 14:22:12 -05002015 /*
2016 * Give the controller a chance to complete all entered requests if
2017 * doing a safe shutdown.
2018 */
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002019 if (!dead) {
2020 if (shutdown)
2021 nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
2022
2023 /*
2024 * If the controller is still alive tell it to stop using the
2025 * host memory buffer. In theory the shutdown / reset should
2026 * make sure that it doesn't access the host memoery anymore,
2027 * but I'd rather be safe than sorry..
2028 */
2029 if (dev->host_mem_descs)
2030 nvme_set_host_mem(dev, 0);
2031
2032 }
Keith Busch302ad8c2017-03-01 14:22:12 -05002033 nvme_stop_queues(&dev->ctrl);
2034
Keith Busch70659062016-10-12 09:22:16 -06002035 queues = dev->online_queues - 1;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002036 for (i = dev->ctrl.queue_count - 1; i > 0; i--)
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06002037 nvme_suspend_queue(dev->queues[i]);
2038
Keith Busch302ad8c2017-03-01 14:22:12 -05002039 if (dead) {
Gabriel Krisman Bertazi82469c52016-09-06 17:39:13 -03002040 /* A device might become IO incapable very soon during
2041 * probe, before the admin queue is configured. Thus,
2042 * queue_count can be 0 here.
2043 */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002044 if (dev->ctrl.queue_count)
Gabriel Krisman Bertazi82469c52016-09-06 17:39:13 -03002045 nvme_suspend_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07002046 } else {
Keith Busch70659062016-10-12 09:22:16 -06002047 nvme_disable_io_queues(dev, queues);
Keith Buscha5cdb682016-01-12 14:41:18 -07002048 nvme_disable_admin_queue(dev, shutdown);
Keith Busch4d115422013-12-10 13:10:40 -07002049 }
Keith Buschb00a7262016-02-24 09:15:52 -07002050 nvme_pci_disable(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002051
Ming Line1958e62016-05-18 14:05:01 -07002052 blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl);
2053 blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl);
Keith Busch302ad8c2017-03-01 14:22:12 -05002054
2055 /*
2056 * The driver will not be starting up queues again if shutting down so
2057 * must flush all entered requests to their failed completion to avoid
2058 * deadlocking blk-mq hot-cpu notifier.
2059 */
2060 if (shutdown)
2061 nvme_start_queues(&dev->ctrl);
Keith Busch77bf25e2015-11-26 12:21:29 +01002062 mutex_unlock(&dev->shutdown_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002063}
2064
Matthew Wilcox091b6092011-02-10 09:56:01 -05002065static int nvme_setup_prp_pools(struct nvme_dev *dev)
2066{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002067 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002068 PAGE_SIZE, PAGE_SIZE, 0);
2069 if (!dev->prp_page_pool)
2070 return -ENOMEM;
2071
Matthew Wilcox99802a72011-02-10 10:30:34 -05002072 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002073 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002074 256, 256, 0);
2075 if (!dev->prp_small_pool) {
2076 dma_pool_destroy(dev->prp_page_pool);
2077 return -ENOMEM;
2078 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002079 return 0;
2080}
2081
2082static void nvme_release_prp_pools(struct nvme_dev *dev)
2083{
2084 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002085 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002086}
2087
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002088static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002089{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002090 struct nvme_dev *dev = to_nvme_dev(ctrl);
Keith Busch9ac27092014-01-31 16:53:39 -07002091
Helen Koikef9f38e32017-04-10 12:51:07 -03002092 nvme_dbbuf_dma_free(dev);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002093 put_device(dev->dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002094 if (dev->tagset.tags)
2095 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002096 if (dev->ctrl.admin_q)
2097 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002098 kfree(dev->queues);
Scott Bauere286bcf2017-02-22 10:15:07 -07002099 free_opal_dev(dev->ctrl.opal_dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002100 kfree(dev);
2101}
2102
Keith Buschf58944e2016-02-24 09:15:55 -07002103static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
2104{
Linus Torvalds237045f2016-03-18 17:13:31 -07002105 dev_warn(dev->ctrl.device, "Removing after probe failure status: %d\n", status);
Keith Buschf58944e2016-02-24 09:15:55 -07002106
2107 kref_get(&dev->ctrl.kref);
Keith Busch69d9a992016-02-24 09:15:56 -07002108 nvme_dev_disable(dev, false);
Keith Buschf58944e2016-02-24 09:15:55 -07002109 if (!schedule_work(&dev->remove_work))
2110 nvme_put_ctrl(&dev->ctrl);
2111}
2112
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002113static void nvme_reset_work(struct work_struct *work)
Keith Busch5e82e952013-02-19 10:17:58 -07002114{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002115 struct nvme_dev *dev =
2116 container_of(work, struct nvme_dev, ctrl.reset_work);
Scott Bauera98e58e52017-02-03 12:50:32 -07002117 bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
Keith Buschf58944e2016-02-24 09:15:55 -07002118 int result = -ENODEV;
Keith Buschf0b50732013-07-15 15:02:21 -06002119
Rakesh Pandit82b057c2017-06-05 14:43:11 +03002120 if (WARN_ON(dev->ctrl.state != NVME_CTRL_RESETTING))
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002121 goto out;
2122
2123 /*
2124 * If we're called to reset a live controller first shut it down before
2125 * moving on.
2126 */
Keith Buschb00a7262016-02-24 09:15:52 -07002127 if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
Keith Buscha5cdb682016-01-12 14:41:18 -07002128 nvme_dev_disable(dev, false);
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002129
Keith Buschb00a7262016-02-24 09:15:52 -07002130 result = nvme_pci_enable(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002131 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002132 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002133
Sagi Grimberg01ad0992017-05-01 00:27:17 +03002134 result = nvme_pci_configure_admin_queue(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002135 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002136 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002137
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002138 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002139 result = nvme_alloc_admin_tags(dev);
2140 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002141 goto out;
Dan McLeranb9afca32014-04-07 17:10:11 -06002142
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002143 result = nvme_init_identify(&dev->ctrl);
2144 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002145 goto out;
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002146
Scott Bauere286bcf2017-02-22 10:15:07 -07002147 if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) {
2148 if (!dev->ctrl.opal_dev)
2149 dev->ctrl.opal_dev =
2150 init_opal_dev(&dev->ctrl, &nvme_sec_submit);
2151 else if (was_suspend)
2152 opal_unlock_from_suspend(dev->ctrl.opal_dev);
2153 } else {
2154 free_opal_dev(dev->ctrl.opal_dev);
2155 dev->ctrl.opal_dev = NULL;
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01002156 }
Scott Bauera98e58e52017-02-03 12:50:32 -07002157
Helen Koikef9f38e32017-04-10 12:51:07 -03002158 if (dev->ctrl.oacs & NVME_CTRL_OACS_DBBUF_SUPP) {
2159 result = nvme_dbbuf_dma_alloc(dev);
2160 if (result)
2161 dev_warn(dev->dev,
2162 "unable to allocate dma for dbbuf\n");
2163 }
2164
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002165 if (dev->ctrl.hmpre)
2166 nvme_setup_host_mem(dev);
2167
Keith Buschf0b50732013-07-15 15:02:21 -06002168 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002169 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002170 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002171
Keith Busch21f033f2016-04-12 11:13:11 -06002172 /*
Christoph Hellwig2659e572015-10-02 18:51:31 +02002173 * Keep the controller around but remove all namespaces if we don't have
2174 * any working I/O queue.
2175 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002176 if (dev->online_queues < 2) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002177 dev_warn(dev->ctrl.device, "IO queues not created\n");
Keith Busch3b247742016-04-27 15:51:18 -06002178 nvme_kill_queues(&dev->ctrl);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002179 nvme_remove_namespaces(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002180 } else {
Keith Busch25646262016-01-04 09:10:57 -07002181 nvme_start_queues(&dev->ctrl);
Keith Busch302ad8c2017-03-01 14:22:12 -05002182 nvme_wait_freeze(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002183 nvme_dev_add(dev);
Keith Busch302ad8c2017-03-01 14:22:12 -05002184 nvme_unfreeze(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002185 }
2186
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002187 if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) {
2188 dev_warn(dev->ctrl.device, "failed to mark controller live\n");
2189 goto out;
2190 }
Christoph Hellwig92911a52016-04-26 13:51:58 +02002191
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002192 nvme_start_ctrl(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002193 return;
Keith Buschf0b50732013-07-15 15:02:21 -06002194
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002195 out:
Keith Buschf58944e2016-02-24 09:15:55 -07002196 nvme_remove_dead_ctrl(dev, result);
Keith Buschf0b50732013-07-15 15:02:21 -06002197}
2198
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002199static void nvme_remove_dead_ctrl_work(struct work_struct *work)
Keith Busch9a6b9452013-12-10 13:10:36 -07002200{
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002201 struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002202 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002203
Keith Busch69d9a992016-02-24 09:15:56 -07002204 nvme_kill_queues(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002205 if (pci_get_drvdata(pdev))
Keith Busch921920a2016-03-28 16:03:21 -06002206 device_release_driver(&pdev->dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002207 nvme_put_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002208}
2209
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002210static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
Keith Busch4cc06522015-06-05 10:30:08 -06002211{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002212 *val = readl(to_nvme_dev(ctrl)->bar + off);
2213 return 0;
Keith Busch4cc06522015-06-05 10:30:08 -06002214}
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002215
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002216static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2217{
2218 writel(val, to_nvme_dev(ctrl)->bar + off);
2219 return 0;
2220}
2221
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002222static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2223{
2224 *val = readq(to_nvme_dev(ctrl)->bar + off);
2225 return 0;
2226}
2227
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002228static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
Ming Lin1a353d82016-06-13 16:45:24 +02002229 .name = "pcie",
Sagi Grimberge439bb12016-02-10 10:03:29 -08002230 .module = THIS_MODULE,
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02002231 .flags = NVME_F_METADATA_SUPPORTED,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002232 .reg_read32 = nvme_pci_reg_read32,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002233 .reg_write32 = nvme_pci_reg_write32,
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002234 .reg_read64 = nvme_pci_reg_read64,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002235 .free_ctrl = nvme_pci_free_ctrl,
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002236 .submit_async_event = nvme_pci_submit_async_event,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002237};
Keith Busch4cc06522015-06-05 10:30:08 -06002238
Keith Buschb00a7262016-02-24 09:15:52 -07002239static int nvme_dev_map(struct nvme_dev *dev)
2240{
Keith Buschb00a7262016-02-24 09:15:52 -07002241 struct pci_dev *pdev = to_pci_dev(dev->dev);
2242
Johannes Thumshirna1f447b2016-06-07 09:44:02 +02002243 if (pci_request_mem_regions(pdev, "nvme"))
Keith Buschb00a7262016-02-24 09:15:52 -07002244 return -ENODEV;
2245
Xu Yu97f6ef62017-05-24 16:39:55 +08002246 if (nvme_remap_bar(dev, NVME_REG_DBS + 4096))
Keith Buschb00a7262016-02-24 09:15:52 -07002247 goto release;
2248
Max Gurtovoy9fa196e2016-12-19 16:18:24 +02002249 return 0;
Keith Buschb00a7262016-02-24 09:15:52 -07002250 release:
Max Gurtovoy9fa196e2016-12-19 16:18:24 +02002251 pci_release_mem_regions(pdev);
2252 return -ENODEV;
Keith Buschb00a7262016-02-24 09:15:52 -07002253}
2254
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002255static unsigned long check_dell_samsung_bug(struct pci_dev *pdev)
2256{
2257 if (pdev->vendor == 0x144d && pdev->device == 0xa802) {
2258 /*
2259 * Several Samsung devices seem to drop off the PCIe bus
2260 * randomly when APST is on and uses the deepest sleep state.
2261 * This has been observed on a Samsung "SM951 NVMe SAMSUNG
2262 * 256GB", a "PM951 NVMe SAMSUNG 512GB", and a "Samsung SSD
2263 * 950 PRO 256GB", but it seems to be restricted to two Dell
2264 * laptops.
2265 */
2266 if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.") &&
2267 (dmi_match(DMI_PRODUCT_NAME, "XPS 15 9550") ||
2268 dmi_match(DMI_PRODUCT_NAME, "Precision 5510")))
2269 return NVME_QUIRK_NO_DEEPEST_PS;
2270 }
2271
2272 return 0;
2273}
2274
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002275static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002276{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002277 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002278 struct nvme_dev *dev;
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002279 unsigned long quirks = id->driver_data;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002280
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002281 node = dev_to_node(&pdev->dev);
2282 if (node == NUMA_NO_NODE)
Masayoshi Mizuma2fa84352016-06-20 09:33:17 +09002283 set_dev_node(&pdev->dev, first_memory_node);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002284
2285 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002286 if (!dev)
2287 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002288 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2289 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002290 if (!dev->queues)
2291 goto free;
2292
Christoph Hellwige75ec752015-05-22 11:12:39 +02002293 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002294 pci_set_drvdata(pdev, dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002295
Keith Buschb00a7262016-02-24 09:15:52 -07002296 result = nvme_dev_map(dev);
2297 if (result)
Christophe JAILLETb00c9b72017-07-16 10:39:03 +02002298 goto put_pci;
Keith Buschb00a7262016-02-24 09:15:52 -07002299
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002300 INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002301 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
Keith Busch77bf25e2015-11-26 12:21:29 +01002302 mutex_init(&dev->shutdown_lock);
Keith Buschdb3cbff2016-01-12 14:41:17 -07002303 init_completion(&dev->ioq_wait);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002304
2305 result = nvme_setup_prp_pools(dev);
2306 if (result)
Christophe JAILLETb00c9b72017-07-16 10:39:03 +02002307 goto unmap;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002308
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002309 quirks |= check_dell_samsung_bug(pdev);
2310
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002311 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002312 quirks);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002313 if (result)
2314 goto release_pools;
2315
Rakesh Pandit82b057c2017-06-05 14:43:11 +03002316 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING);
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002317 dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev));
2318
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002319 queue_work(nvme_wq, &dev->ctrl.reset_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002320 return 0;
2321
Keith Busch0877cb02013-07-15 15:02:19 -06002322 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002323 nvme_release_prp_pools(dev);
Christophe JAILLETb00c9b72017-07-16 10:39:03 +02002324 unmap:
2325 nvme_dev_unmap(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002326 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002327 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002328 free:
2329 kfree(dev->queues);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002330 kfree(dev);
2331 return result;
2332}
2333
Christoph Hellwig775755e2017-06-01 13:10:38 +02002334static void nvme_reset_prepare(struct pci_dev *pdev)
Keith Buschf0d54a52014-05-02 10:40:43 -06002335{
Keith Buscha6739472014-06-23 16:03:21 -06002336 struct nvme_dev *dev = pci_get_drvdata(pdev);
Linus Torvaldsf263fbb2017-07-08 15:51:57 -07002337 nvme_dev_disable(dev, false);
Christoph Hellwig775755e2017-06-01 13:10:38 +02002338}
Keith Buschf0d54a52014-05-02 10:40:43 -06002339
Christoph Hellwig775755e2017-06-01 13:10:38 +02002340static void nvme_reset_done(struct pci_dev *pdev)
2341{
Linus Torvaldsf263fbb2017-07-08 15:51:57 -07002342 struct nvme_dev *dev = pci_get_drvdata(pdev);
2343 nvme_reset_ctrl(&dev->ctrl);
Keith Buschf0d54a52014-05-02 10:40:43 -06002344}
2345
Keith Busch09ece142014-01-27 11:29:40 -05002346static void nvme_shutdown(struct pci_dev *pdev)
2347{
2348 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buscha5cdb682016-01-12 14:41:18 -07002349 nvme_dev_disable(dev, true);
Keith Busch09ece142014-01-27 11:29:40 -05002350}
2351
Keith Buschf58944e2016-02-24 09:15:55 -07002352/*
2353 * The driver's remove may be called on a device in a partially initialized
2354 * state. This function must not have any dependencies on the device state in
2355 * order to proceed.
2356 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002357static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002358{
2359 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002360
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002361 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
2362
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002363 cancel_work_sync(&dev->ctrl.reset_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002364 pci_set_drvdata(pdev, NULL);
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002365
Keith Busch6db28ed2017-02-10 18:15:49 -05002366 if (!pci_device_is_present(pdev)) {
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002367 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD);
Keith Busch6db28ed2017-02-10 18:15:49 -05002368 nvme_dev_disable(dev, false);
2369 }
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002370
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002371 flush_work(&dev->ctrl.reset_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002372 nvme_stop_ctrl(&dev->ctrl);
2373 nvme_remove_namespaces(&dev->ctrl);
Keith Buscha5cdb682016-01-12 14:41:18 -07002374 nvme_dev_disable(dev, true);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002375 nvme_free_host_mem(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002376 nvme_dev_remove_admin(dev);
2377 nvme_free_queues(dev, 0);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002378 nvme_uninit_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002379 nvme_release_prp_pools(dev);
Keith Buschb00a7262016-02-24 09:15:52 -07002380 nvme_dev_unmap(dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002381 nvme_put_ctrl(&dev->ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002382}
2383
Keith Busch13880f52016-06-20 09:41:06 -06002384static int nvme_pci_sriov_configure(struct pci_dev *pdev, int numvfs)
2385{
2386 int ret = 0;
2387
2388 if (numvfs == 0) {
2389 if (pci_vfs_assigned(pdev)) {
2390 dev_warn(&pdev->dev,
2391 "Cannot disable SR-IOV VFs while assigned\n");
2392 return -EPERM;
2393 }
2394 pci_disable_sriov(pdev);
2395 return 0;
2396 }
2397
2398 ret = pci_enable_sriov(pdev, numvfs);
2399 return ret ? ret : numvfs;
2400}
2401
Jingoo Han671a6012014-02-13 11:19:14 +09002402#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002403static int nvme_suspend(struct device *dev)
2404{
2405 struct pci_dev *pdev = to_pci_dev(dev);
2406 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2407
Keith Buscha5cdb682016-01-12 14:41:18 -07002408 nvme_dev_disable(ndev, true);
Keith Buschcd638942013-07-15 15:02:23 -06002409 return 0;
2410}
2411
2412static int nvme_resume(struct device *dev)
2413{
2414 struct pci_dev *pdev = to_pci_dev(dev);
2415 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002416
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002417 nvme_reset_ctrl(&ndev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002418 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002419}
Jingoo Han671a6012014-02-13 11:19:14 +09002420#endif
Keith Buschcd638942013-07-15 15:02:23 -06002421
2422static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002423
Keith Buscha0a34082015-12-07 15:30:31 -07002424static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
2425 pci_channel_state_t state)
2426{
2427 struct nvme_dev *dev = pci_get_drvdata(pdev);
2428
2429 /*
2430 * A frozen channel requires a reset. When detected, this method will
2431 * shutdown the controller to quiesce. The controller will be restarted
2432 * after the slot reset through driver's slot_reset callback.
2433 */
Keith Buscha0a34082015-12-07 15:30:31 -07002434 switch (state) {
2435 case pci_channel_io_normal:
2436 return PCI_ERS_RESULT_CAN_RECOVER;
2437 case pci_channel_io_frozen:
Keith Buschd011fb32016-04-04 15:07:41 -06002438 dev_warn(dev->ctrl.device,
2439 "frozen state error detected, reset controller\n");
Keith Buscha5cdb682016-01-12 14:41:18 -07002440 nvme_dev_disable(dev, false);
Keith Buscha0a34082015-12-07 15:30:31 -07002441 return PCI_ERS_RESULT_NEED_RESET;
2442 case pci_channel_io_perm_failure:
Keith Buschd011fb32016-04-04 15:07:41 -06002443 dev_warn(dev->ctrl.device,
2444 "failure state error detected, request disconnect\n");
Keith Buscha0a34082015-12-07 15:30:31 -07002445 return PCI_ERS_RESULT_DISCONNECT;
2446 }
2447 return PCI_ERS_RESULT_NEED_RESET;
2448}
2449
2450static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev)
2451{
2452 struct nvme_dev *dev = pci_get_drvdata(pdev);
2453
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002454 dev_info(dev->ctrl.device, "restart after slot reset\n");
Keith Buscha0a34082015-12-07 15:30:31 -07002455 pci_restore_state(pdev);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002456 nvme_reset_ctrl(&dev->ctrl);
Keith Buscha0a34082015-12-07 15:30:31 -07002457 return PCI_ERS_RESULT_RECOVERED;
2458}
2459
2460static void nvme_error_resume(struct pci_dev *pdev)
2461{
2462 pci_cleanup_aer_uncorrect_error_status(pdev);
2463}
2464
Stephen Hemminger1d352032012-09-07 09:33:17 -07002465static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002466 .error_detected = nvme_error_detected,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002467 .slot_reset = nvme_slot_reset,
2468 .resume = nvme_error_resume,
Christoph Hellwig775755e2017-06-01 13:10:38 +02002469 .reset_prepare = nvme_reset_prepare,
2470 .reset_done = nvme_reset_done,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002471};
2472
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002473static const struct pci_device_id nvme_id_table[] = {
Christoph Hellwig106198e2015-11-26 10:07:41 +01002474 { PCI_VDEVICE(INTEL, 0x0953),
Keith Busch08095e72016-03-04 13:15:17 -07002475 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002476 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch99466e72016-05-02 15:14:24 -06002477 { PCI_VDEVICE(INTEL, 0x0a53),
2478 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002479 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch99466e72016-05-02 15:14:24 -06002480 { PCI_VDEVICE(INTEL, 0x0a54),
2481 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002482 NVME_QUIRK_DEALLOCATE_ZEROES, },
David Wayne Fugatef99cb7af2017-07-10 12:39:59 -06002483 { PCI_VDEVICE(INTEL, 0x0a55),
2484 .driver_data = NVME_QUIRK_STRIPE_SIZE |
2485 NVME_QUIRK_DEALLOCATE_ZEROES, },
Andy Lutomirski50af47d2017-05-24 15:06:31 -07002486 { PCI_VDEVICE(INTEL, 0xf1a5), /* Intel 600P/P3100 */
2487 .driver_data = NVME_QUIRK_NO_DEEPEST_PS },
Keith Busch540c8012015-10-22 15:45:06 -06002488 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2489 .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03002490 { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */
2491 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Wenbo Wang015282c2016-09-08 12:12:11 -04002492 { PCI_DEVICE(0x1c5f, 0x0540), /* Memblaze Pblaze4 adapter */
2493 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Martin K. Petersend554b5e2017-06-27 22:27:57 -04002494 { PCI_DEVICE(0x144d, 0xa821), /* Samsung PM1725 */
2495 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
2496 { PCI_DEVICE(0x144d, 0xa822), /* Samsung PM1725a */
2497 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002498 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01002499 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Daniel Roschka124298b2017-02-22 15:17:29 -07002500 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002501 { 0, }
2502};
2503MODULE_DEVICE_TABLE(pci, nvme_id_table);
2504
2505static struct pci_driver nvme_driver = {
2506 .name = "nvme",
2507 .id_table = nvme_id_table,
2508 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002509 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002510 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002511 .driver = {
2512 .pm = &nvme_dev_pm_ops,
2513 },
Keith Busch13880f52016-06-20 09:41:06 -06002514 .sriov_configure = nvme_pci_sriov_configure,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002515 .err_handler = &nvme_err_handler,
2516};
2517
2518static int __init nvme_init(void)
2519{
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002520 return pci_register_driver(&nvme_driver);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002521}
2522
2523static void __exit nvme_exit(void)
2524{
2525 pci_unregister_driver(&nvme_driver);
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002526 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002527}
2528
2529MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2530MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002531MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002532module_init(nvme_init);
2533module_exit(nvme_exit);