blob: 11874afb2422cf12890b383f0e974cce4f99e0c8 [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);
Jan H. Schönherr5228b322017-08-27 15:56:37 +0200558 if (length <= 0) {
559 iod->first_dma = 0;
Keith Busch86eea282017-07-12 15:59:07 -0400560 return BLK_STS_OK;
Jan H. Schönherr5228b322017-08-27 15:56:37 +0200561 }
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500562
Keith Busch1d090622014-06-23 11:34:01 -0600563 dma_len -= (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500564 if (dma_len) {
Keith Busch1d090622014-06-23 11:34:01 -0600565 dma_addr += (page_size - offset);
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500566 } else {
567 sg = sg_next(sg);
568 dma_addr = sg_dma_address(sg);
569 dma_len = sg_dma_len(sg);
570 }
571
Keith Busch1d090622014-06-23 11:34:01 -0600572 if (length <= page_size) {
Keith Buschedd10d32014-04-03 16:45:23 -0600573 iod->first_dma = dma_addr;
Keith Busch86eea282017-07-12 15:59:07 -0400574 return BLK_STS_OK;
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500575 }
576
Keith Busch1d090622014-06-23 11:34:01 -0600577 nprps = DIV_ROUND_UP(length, page_size);
Matthew Wilcox99802a72011-02-10 10:30:34 -0500578 if (nprps <= (256 / 8)) {
579 pool = dev->prp_small_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500580 iod->npages = 0;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500581 } else {
582 pool = dev->prp_page_pool;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500583 iod->npages = 1;
Matthew Wilcox99802a72011-02-10 10:30:34 -0500584 }
585
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200586 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400587 if (!prp_list) {
Keith Buschedd10d32014-04-03 16:45:23 -0600588 iod->first_dma = dma_addr;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500589 iod->npages = -1;
Keith Busch86eea282017-07-12 15:59:07 -0400590 return BLK_STS_RESOURCE;
Matthew Wilcoxb77954c2011-05-12 13:51:41 -0400591 }
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500592 list[0] = prp_list;
593 iod->first_dma = prp_dma;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500594 i = 0;
595 for (;;) {
Keith Busch1d090622014-06-23 11:34:01 -0600596 if (i == page_size >> 3) {
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500597 __le64 *old_prp_list = prp_list;
Christoph Hellwig69d2b572015-10-16 07:58:37 +0200598 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500599 if (!prp_list)
Keith Busch86eea282017-07-12 15:59:07 -0400600 return BLK_STS_RESOURCE;
Matthew Wilcoxeca18b22011-12-20 13:34:52 -0500601 list[iod->npages++] = prp_list;
Matthew Wilcox7523d832011-03-16 16:43:40 -0400602 prp_list[0] = old_prp_list[i - 1];
603 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
604 i = 1;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500605 }
606 prp_list[i++] = cpu_to_le64(dma_addr);
Keith Busch1d090622014-06-23 11:34:01 -0600607 dma_len -= page_size;
608 dma_addr += page_size;
609 length -= page_size;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500610 if (length <= 0)
611 break;
612 if (dma_len > 0)
613 continue;
Keith Busch86eea282017-07-12 15:59:07 -0400614 if (unlikely(dma_len < 0))
615 goto bad_sgl;
Shane Michael Matthewse025344c2011-02-10 08:51:24 -0500616 sg = sg_next(sg);
617 dma_addr = sg_dma_address(sg);
618 dma_len = sg_dma_len(sg);
619 }
620
Keith Busch86eea282017-07-12 15:59:07 -0400621 return BLK_STS_OK;
622
623 bad_sgl:
624 if (WARN_ONCE(1, "Invalid SGL for payload:%d nents:%d\n",
625 blk_rq_payload_bytes(req), iod->nents)) {
626 for_each_sg(iod->sg, sg, iod->nents, i) {
627 dma_addr_t phys = sg_phys(sg);
628 pr_warn("sg[%d] phys_addr:%pad offset:%d length:%d "
629 "dma_address:%pad dma_length:%d\n", i, &phys,
630 sg->offset, sg->length,
631 &sg_dma_address(sg),
632 sg_dma_len(sg));
633 }
634 }
635 return BLK_STS_IOERR;
636
Matthew Wilcoxff22b542011-01-26 10:02:29 -0500637}
638
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200639static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,
Christoph Hellwigb131c612017-01-13 12:29:12 +0100640 struct nvme_command *cmnd)
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200641{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100642 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200643 struct request_queue *q = req->q;
644 enum dma_data_direction dma_dir = rq_data_dir(req) ?
645 DMA_TO_DEVICE : DMA_FROM_DEVICE;
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200646 blk_status_t ret = BLK_STS_IOERR;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200647
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700648 sg_init_table(iod->sg, blk_rq_nr_phys_segments(req));
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200649 iod->nents = blk_rq_map_sg(q, req, iod->sg);
650 if (!iod->nents)
651 goto out;
652
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200653 ret = BLK_STS_RESOURCE;
Mauricio Faria de Oliveira2b6b5352016-10-11 13:54:20 -0700654 if (!dma_map_sg_attrs(dev->dev, iod->sg, iod->nents, dma_dir,
655 DMA_ATTR_NO_WARN))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200656 goto out;
657
Keith Busch86eea282017-07-12 15:59:07 -0400658 ret = nvme_setup_prps(dev, req);
659 if (ret != BLK_STS_OK)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200660 goto out_unmap;
661
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200662 ret = BLK_STS_IOERR;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200663 if (blk_integrity_rq(req)) {
664 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
665 goto out_unmap;
666
Christoph Hellwigbf684052015-10-26 17:12:51 +0900667 sg_init_table(&iod->meta_sg, 1);
668 if (blk_rq_map_integrity_sg(q, req->bio, &iod->meta_sg) != 1)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200669 goto out_unmap;
670
Keith Buschb5d8af52017-08-29 17:46:02 -0400671 if (req_op(req) == REQ_OP_WRITE)
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200672 nvme_dif_remap(req, nvme_dif_prep);
673
Christoph Hellwigbf684052015-10-26 17:12:51 +0900674 if (!dma_map_sg(dev->dev, &iod->meta_sg, 1, dma_dir))
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200675 goto out_unmap;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200676 }
677
Christoph Hellwigeb793e22016-06-13 16:45:25 +0200678 cmnd->rw.dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
679 cmnd->rw.dptr.prp2 = cpu_to_le64(iod->first_dma);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200680 if (blk_integrity_rq(req))
Christoph Hellwigbf684052015-10-26 17:12:51 +0900681 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(&iod->meta_sg));
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200682 return BLK_STS_OK;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200683
684out_unmap:
685 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
686out:
687 return ret;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200688}
689
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100690static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100691{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100692 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100693 enum dma_data_direction dma_dir = rq_data_dir(req) ?
694 DMA_TO_DEVICE : DMA_FROM_DEVICE;
695
696 if (iod->nents) {
697 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
698 if (blk_integrity_rq(req)) {
Keith Buschb5d8af52017-08-29 17:46:02 -0400699 if (req_op(req) == REQ_OP_READ)
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100700 nvme_dif_remap(req, nvme_dif_complete);
Christoph Hellwigbf684052015-10-26 17:12:51 +0900701 dma_unmap_sg(dev->dev, &iod->meta_sg, 1, dma_dir);
Christoph Hellwigd4f6c3a2015-11-26 10:51:23 +0100702 }
703 }
704
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700705 nvme_cleanup_cmd(req);
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100706 nvme_free_iod(dev, req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500707}
708
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700709/*
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200710 * NOTE: ns is NULL when called on the admin queue.
711 */
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200712static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700713 const struct blk_mq_queue_data *bd)
Keith Busch53562be2014-04-29 11:41:29 -0600714{
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700715 struct nvme_ns *ns = hctx->queue->queuedata;
716 struct nvme_queue *nvmeq = hctx->driver_data;
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200717 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700718 struct request *req = bd->rq;
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200719 struct nvme_command cmnd;
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200720 blk_status_t ret;
Keith Busche1e5e562015-02-19 13:39:03 -0700721
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700722 ret = nvme_setup_cmd(ns, req, &cmnd);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200723 if (ret)
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100724 return ret;
Keith Buschedd10d32014-04-03 16:45:23 -0600725
Christoph Hellwigb131c612017-01-13 12:29:12 +0100726 ret = nvme_init_iod(req, dev);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200727 if (ret)
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700728 goto out_free_cmd;
Keith Buschedd10d32014-04-03 16:45:23 -0600729
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200730 if (blk_rq_nr_phys_segments(req)) {
Christoph Hellwigb131c612017-01-13 12:29:12 +0100731 ret = nvme_map_data(dev, req, &cmnd);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200732 if (ret)
733 goto out_cleanup_iod;
734 }
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700735
Christoph Hellwigaae239e2015-11-26 12:59:50 +0100736 blk_mq_start_request(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200737
738 spin_lock_irq(&nvmeq->q_lock);
Keith Buschae1fba22016-02-11 13:05:42 -0700739 if (unlikely(nvmeq->cq_vector < 0)) {
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200740 ret = BLK_STS_IOERR;
Keith Buschae1fba22016-02-11 13:05:42 -0700741 spin_unlock_irq(&nvmeq->q_lock);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700742 goto out_cleanup_iod;
Keith Buschae1fba22016-02-11 13:05:42 -0700743 }
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200744 __nvme_submit_cmd(nvmeq, &cmnd);
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700745 nvme_process_cq(nvmeq);
746 spin_unlock_irq(&nvmeq->q_lock);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200747 return BLK_STS_OK;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700748out_cleanup_iod:
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100749 nvme_free_iod(dev, req);
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700750out_free_cmd:
751 nvme_cleanup_cmd(req);
Christoph Hellwigba1ca372015-10-16 07:58:38 +0200752 return ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500753}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500754
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200755static void nvme_pci_complete_rq(struct request *req)
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100756{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100757 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
Christoph Hellwigeee417b2015-11-26 13:03:13 +0100758
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200759 nvme_unmap_data(iod->nvmeq->dev, req);
760 nvme_complete_rq(req);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500761}
762
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100763/* We read the CQE phase first to check if the rest of the entry is valid */
764static inline bool nvme_cqe_valid(struct nvme_queue *nvmeq, u16 head,
765 u16 phase)
766{
767 return (le16_to_cpu(nvmeq->cqes[head].status) & 1) == phase;
768}
769
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300770static inline void nvme_ring_cq_doorbell(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500771{
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300772 u16 head = nvmeq->cq_head;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500773
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300774 if (likely(nvmeq->cq_vector >= 0)) {
Helen Koikef9f38e32017-04-10 12:51:07 -0300775 if (nvme_dbbuf_update_and_check_event(head, nvmeq->dbbuf_cq_db,
776 nvmeq->dbbuf_cq_ei))
777 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
Sagi Grimbergeb281c82017-06-18 17:28:07 +0300778 }
779}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500780
Sagi Grimberg83a12fb2017-06-18 17:28:08 +0300781static inline void nvme_handle_cqe(struct nvme_queue *nvmeq,
782 struct nvme_completion *cqe)
783{
784 struct request *req;
785
786 if (unlikely(cqe->command_id >= nvmeq->q_depth)) {
787 dev_warn(nvmeq->dev->ctrl.device,
788 "invalid id %d completed on queue %d\n",
789 cqe->command_id, le16_to_cpu(cqe->sq_id));
790 return;
791 }
792
793 /*
794 * AEN requests are special as they don't time out and can
795 * survive any kind of queue freeze and often don't respond to
796 * aborts. We don't even bother to allocate a struct request
797 * for them but rather special case them here.
798 */
799 if (unlikely(nvmeq->qid == 0 &&
800 cqe->command_id >= NVME_AQ_BLKMQ_DEPTH)) {
801 nvme_complete_async_event(&nvmeq->dev->ctrl,
802 cqe->status, &cqe->result);
803 return;
804 }
805
Keith Busche9d8a0f2017-08-17 16:45:06 -0400806 nvmeq->cqe_seen = 1;
Sagi Grimberg83a12fb2017-06-18 17:28:08 +0300807 req = blk_mq_tag_to_rq(*nvmeq->tags, cqe->command_id);
808 nvme_end_request(req, cqe->status, cqe->result);
809}
810
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300811static inline bool nvme_read_cqe(struct nvme_queue *nvmeq,
812 struct nvme_completion *cqe)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500813{
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300814 if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase)) {
815 *cqe = nvmeq->cqes[nvmeq->cq_head];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500816
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300817 if (++nvmeq->cq_head == nvmeq->q_depth) {
818 nvmeq->cq_head = 0;
819 nvmeq->cq_phase = !nvmeq->cq_phase;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500820 }
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300821 return true;
822 }
823 return false;
Jens Axboea0fa9642015-11-03 20:37:26 -0700824}
825
826static void nvme_process_cq(struct nvme_queue *nvmeq)
827{
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300828 struct nvme_completion cqe;
829 int consumed = 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500830
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300831 while (nvme_read_cqe(nvmeq, &cqe)) {
Sagi Grimberg83a12fb2017-06-18 17:28:08 +0300832 nvme_handle_cqe(nvmeq, &cqe);
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300833 consumed++;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500834 }
835
Keith Busche9d8a0f2017-08-17 16:45:06 -0400836 if (consumed)
Sagi Grimberg920d13a2017-06-18 17:28:09 +0300837 nvme_ring_cq_doorbell(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500838}
839
840static irqreturn_t nvme_irq(int irq, void *data)
841{
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500842 irqreturn_t result;
843 struct nvme_queue *nvmeq = data;
844 spin_lock(&nvmeq->q_lock);
Matthew Wilcoxe9539f42013-06-24 11:47:34 -0400845 nvme_process_cq(nvmeq);
846 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
847 nvmeq->cqe_seen = 0;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500848 spin_unlock(&nvmeq->q_lock);
849 return result;
850}
851
852static irqreturn_t nvme_irq_check(int irq, void *data)
853{
854 struct nvme_queue *nvmeq = data;
Marta Rybczynskad783e0b2016-03-22 16:02:06 +0100855 if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase))
856 return IRQ_WAKE_THREAD;
857 return IRQ_NONE;
Matthew Wilcox58ffacb2011-02-06 07:28:06 -0500858}
859
Keith Busch7776db12017-02-24 17:59:28 -0500860static int __nvme_poll(struct nvme_queue *nvmeq, unsigned int tag)
Jens Axboea0fa9642015-11-03 20:37:26 -0700861{
Sagi Grimberg442e19b2017-06-18 17:28:10 +0300862 struct nvme_completion cqe;
863 int found = 0, consumed = 0;
Jens Axboea0fa9642015-11-03 20:37:26 -0700864
Sagi Grimberg442e19b2017-06-18 17:28:10 +0300865 if (!nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase))
866 return 0;
Jens Axboea0fa9642015-11-03 20:37:26 -0700867
Sagi Grimberg442e19b2017-06-18 17:28:10 +0300868 spin_lock_irq(&nvmeq->q_lock);
869 while (nvme_read_cqe(nvmeq, &cqe)) {
870 nvme_handle_cqe(nvmeq, &cqe);
871 consumed++;
872
873 if (tag == cqe.command_id) {
874 found = 1;
875 break;
876 }
877 }
878
879 if (consumed)
880 nvme_ring_cq_doorbell(nvmeq);
881 spin_unlock_irq(&nvmeq->q_lock);
882
883 return found;
Jens Axboea0fa9642015-11-03 20:37:26 -0700884}
885
Keith Busch7776db12017-02-24 17:59:28 -0500886static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
887{
888 struct nvme_queue *nvmeq = hctx->driver_data;
889
890 return __nvme_poll(nvmeq, tag);
891}
892
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200893static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl, int aer_idx)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500894{
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200895 struct nvme_dev *dev = to_nvme_dev(ctrl);
Christoph Hellwig9396dec2016-02-29 15:59:44 +0100896 struct nvme_queue *nvmeq = dev->queues[0];
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700897 struct nvme_command c;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700898
899 memset(&c, 0, sizeof(c));
900 c.common.opcode = nvme_admin_async_event;
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200901 c.common.command_id = NVME_AQ_BLKMQ_DEPTH + aer_idx;
Matias Bjørlinga4aea562014-11-04 08:20:14 -0700902
Christoph Hellwig9396dec2016-02-29 15:59:44 +0100903 spin_lock_irq(&nvmeq->q_lock);
Christoph Hellwigf866fc422016-04-26 13:52:00 +0200904 __nvme_submit_cmd(nvmeq, &c);
Christoph Hellwig9396dec2016-02-29 15:59:44 +0100905 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch4d115422013-12-10 13:10:40 -0700906}
907
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500908static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
909{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500910 struct nvme_command c;
911
912 memset(&c, 0, sizeof(c));
913 c.delete_queue.opcode = opcode;
914 c.delete_queue.qid = cpu_to_le16(id);
915
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100916 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500917}
918
919static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
920 struct nvme_queue *nvmeq)
921{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500922 struct nvme_command c;
923 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
924
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200925 /*
926 * Note: we (ab)use the fact the the prp fields survive if no data
927 * is attached to the request.
928 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500929 memset(&c, 0, sizeof(c));
930 c.create_cq.opcode = nvme_admin_create_cq;
931 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
932 c.create_cq.cqid = cpu_to_le16(qid);
933 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
934 c.create_cq.cq_flags = cpu_to_le16(flags);
935 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
936
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100937 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500938}
939
940static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
941 struct nvme_queue *nvmeq)
942{
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500943 struct nvme_command c;
Keith Busch81c1cd92017-04-04 18:18:12 -0400944 int flags = NVME_QUEUE_PHYS_CONTIG;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500945
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200946 /*
947 * Note: we (ab)use the fact the the prp fields survive if no data
948 * is attached to the request.
949 */
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500950 memset(&c, 0, sizeof(c));
951 c.create_sq.opcode = nvme_admin_create_sq;
952 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
953 c.create_sq.sqid = cpu_to_le16(qid);
954 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
955 c.create_sq.sq_flags = cpu_to_le16(flags);
956 c.create_sq.cqid = cpu_to_le16(qid);
957
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100958 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -0500959}
960
961static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
962{
963 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
964}
965
966static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
967{
968 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
969}
970
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200971static void abort_endio(struct request *req, blk_status_t error)
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400972{
Christoph Hellwigf4800d62015-11-28 15:43:10 +0100973 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
974 struct nvme_queue *nvmeq = iod->nvmeq;
Matthew Wilcoxbc5fc7e2011-09-19 17:08:14 -0400975
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200976 dev_warn(nvmeq->dev->ctrl.device,
977 "Abort status: 0x%x", nvme_req(req)->status);
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100978 atomic_inc(&nvmeq->dev->ctrl.abort_limit);
Christoph Hellwige7a2a872015-11-16 10:39:48 +0100979 blk_mq_free_request(req);
Christoph Hellwigd29ec822015-05-22 11:12:46 +0200980}
981
Keith Buschb2a0eb12017-06-07 20:32:50 +0200982static bool nvme_should_reset(struct nvme_dev *dev, u32 csts)
983{
984
985 /* If true, indicates loss of adapter communication, possibly by a
986 * NVMe Subsystem reset.
987 */
988 bool nssro = dev->subsystem && (csts & NVME_CSTS_NSSRO);
989
990 /* If there is a reset ongoing, we shouldn't reset again. */
991 if (dev->ctrl.state == NVME_CTRL_RESETTING)
992 return false;
993
994 /* We shouldn't reset unless the controller is on fatal error state
995 * _or_ if we lost the communication with it.
996 */
997 if (!(csts & NVME_CSTS_CFS) && !nssro)
998 return false;
999
1000 /* If PCI error recovery process is happening, we cannot reset or
1001 * the recovery mechanism will surely fail.
1002 */
1003 if (pci_channel_offline(to_pci_dev(dev->dev)))
1004 return false;
1005
1006 return true;
1007}
1008
1009static void nvme_warn_reset(struct nvme_dev *dev, u32 csts)
1010{
1011 /* Read a config register to help see what died. */
1012 u16 pci_status;
1013 int result;
1014
1015 result = pci_read_config_word(to_pci_dev(dev->dev), PCI_STATUS,
1016 &pci_status);
1017 if (result == PCIBIOS_SUCCESSFUL)
1018 dev_warn(dev->ctrl.device,
1019 "controller is down; will reset: CSTS=0x%x, PCI_STATUS=0x%hx\n",
1020 csts, pci_status);
1021 else
1022 dev_warn(dev->ctrl.device,
1023 "controller is down; will reset: CSTS=0x%x, PCI_STATUS read failed (%d)\n",
1024 csts, result);
1025}
1026
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001027static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001028{
Christoph Hellwigf4800d62015-11-28 15:43:10 +01001029 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
1030 struct nvme_queue *nvmeq = iod->nvmeq;
Keith Buschc30341d2013-12-10 13:10:38 -07001031 struct nvme_dev *dev = nvmeq->dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001032 struct request *abort_req;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001033 struct nvme_command cmd;
Keith Buschb2a0eb12017-06-07 20:32:50 +02001034 u32 csts = readl(dev->bar + NVME_REG_CSTS);
1035
1036 /*
1037 * Reset immediately if the controller is failed
1038 */
1039 if (nvme_should_reset(dev, csts)) {
1040 nvme_warn_reset(dev, csts);
1041 nvme_dev_disable(dev, false);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001042 nvme_reset_ctrl(&dev->ctrl);
Keith Buschb2a0eb12017-06-07 20:32:50 +02001043 return BLK_EH_HANDLED;
1044 }
Keith Buschc30341d2013-12-10 13:10:38 -07001045
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001046 /*
Keith Busch7776db12017-02-24 17:59:28 -05001047 * Did we miss an interrupt?
1048 */
1049 if (__nvme_poll(nvmeq, req->tag)) {
1050 dev_warn(dev->ctrl.device,
1051 "I/O %d QID %d timeout, completion polled\n",
1052 req->tag, nvmeq->qid);
1053 return BLK_EH_HANDLED;
1054 }
1055
1056 /*
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001057 * Shutdown immediately if controller times out while starting. The
1058 * reset work will see the pci device disabled when it gets the forced
1059 * cancellation error. All outstanding requests are completed on
1060 * shutdown, so we return BLK_EH_HANDLED.
1061 */
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02001062 if (dev->ctrl.state == NVME_CTRL_RESETTING) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001063 dev_warn(dev->ctrl.device,
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001064 "I/O %d QID %d timeout, disable controller\n",
1065 req->tag, nvmeq->qid);
Keith Buscha5cdb682016-01-12 14:41:18 -07001066 nvme_dev_disable(dev, false);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001067 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001068 return BLK_EH_HANDLED;
Keith Buschc30341d2013-12-10 13:10:38 -07001069 }
1070
Christoph Hellwigfd634f412015-11-26 12:42:26 +01001071 /*
1072 * Shutdown the controller immediately and schedule a reset if the
1073 * command was already aborted once before and still hasn't been
1074 * returned to the driver, or if this is the admin queue.
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001075 */
Christoph Hellwigf4800d62015-11-28 15:43:10 +01001076 if (!nvmeq->qid || iod->aborted) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001077 dev_warn(dev->ctrl.device,
Keith Busche1569a12015-11-26 12:11:07 +01001078 "I/O %d QID %d timeout, reset controller\n",
1079 req->tag, nvmeq->qid);
Keith Buscha5cdb682016-01-12 14:41:18 -07001080 nvme_dev_disable(dev, false);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001081 nvme_reset_ctrl(&dev->ctrl);
Keith Buschc30341d2013-12-10 13:10:38 -07001082
Keith Busche1569a12015-11-26 12:11:07 +01001083 /*
1084 * Mark the request as handled, since the inline shutdown
1085 * forces all outstanding requests to complete.
1086 */
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001087 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
Keith Busche1569a12015-11-26 12:11:07 +01001088 return BLK_EH_HANDLED;
Keith Buschc30341d2013-12-10 13:10:38 -07001089 }
Keith Buschc30341d2013-12-10 13:10:38 -07001090
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001091 if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) {
1092 atomic_inc(&dev->ctrl.abort_limit);
1093 return BLK_EH_RESET_TIMER;
1094 }
Keith Busch7bf7d772017-01-24 18:07:00 -05001095 iod->aborted = 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001096
Keith Buschc30341d2013-12-10 13:10:38 -07001097 memset(&cmd, 0, sizeof(cmd));
1098 cmd.abort.opcode = nvme_admin_abort_cmd;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001099 cmd.abort.cid = req->tag;
Keith Buschc30341d2013-12-10 13:10:38 -07001100 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
Keith Buschc30341d2013-12-10 13:10:38 -07001101
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001102 dev_warn(nvmeq->dev->ctrl.device,
1103 "I/O %d QID %d timeout, aborting\n",
1104 req->tag, nvmeq->qid);
Keith Buschc30341d2013-12-10 13:10:38 -07001105
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001106 abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd,
Christoph Hellwigeb71f432016-06-13 16:45:23 +02001107 BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001108 if (IS_ERR(abort_req)) {
1109 atomic_inc(&dev->ctrl.abort_limit);
Christoph Hellwig31c7c7d2015-10-22 14:03:35 +02001110 return BLK_EH_RESET_TIMER;
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001111 }
Keith Buschc30341d2013-12-10 13:10:38 -07001112
Christoph Hellwige7a2a872015-11-16 10:39:48 +01001113 abort_req->timeout = ADMIN_TIMEOUT;
1114 abort_req->end_io_data = NULL;
1115 blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio);
Keith Busch07836e62015-02-19 10:34:48 -07001116
Keith Busch7a509a62015-01-07 18:55:53 -07001117 /*
1118 * The aborted req will be completed on receiving the abort req.
1119 * We enable the timer again. If hit twice, it'll cause a device reset,
1120 * as the device then is in a faulty state.
1121 */
Keith Busch07836e62015-02-19 10:34:48 -07001122 return BLK_EH_RESET_TIMER;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001123}
1124
Keith Buschf435c282014-07-07 09:14:42 -06001125static void nvme_free_queue(struct nvme_queue *nvmeq)
Matthew Wilcox9e866772012-08-03 13:55:56 -04001126{
1127 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1128 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001129 if (nvmeq->sq_cmds)
1130 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
Matthew Wilcox9e866772012-08-03 13:55:56 -04001131 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1132 kfree(nvmeq);
1133}
1134
Keith Buscha1a5ef92013-12-16 13:50:00 -05001135static void nvme_free_queues(struct nvme_dev *dev, int lowest)
Keith Busch22404272013-07-15 15:02:20 -06001136{
1137 int i;
1138
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001139 for (i = dev->ctrl.queue_count - 1; i >= lowest; i--) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001140 struct nvme_queue *nvmeq = dev->queues[i];
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001141 dev->ctrl.queue_count--;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001142 dev->queues[i] = NULL;
Keith Buschf435c282014-07-07 09:14:42 -06001143 nvme_free_queue(nvmeq);
kaoudis121c7ad2015-01-14 21:01:58 -07001144 }
Keith Busch22404272013-07-15 15:02:20 -06001145}
1146
Keith Busch4d115422013-12-10 13:10:40 -07001147/**
1148 * nvme_suspend_queue - put queue into suspended state
1149 * @nvmeq - queue to suspend
Keith Busch4d115422013-12-10 13:10:40 -07001150 */
1151static int nvme_suspend_queue(struct nvme_queue *nvmeq)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001152{
Keith Busch2b25d982014-12-22 12:59:04 -07001153 int vector;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001154
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001155 spin_lock_irq(&nvmeq->q_lock);
Keith Busch2b25d982014-12-22 12:59:04 -07001156 if (nvmeq->cq_vector == -1) {
1157 spin_unlock_irq(&nvmeq->q_lock);
1158 return 1;
1159 }
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001160 vector = nvmeq->cq_vector;
Keith Busch42f61422014-03-24 10:46:25 -06001161 nvmeq->dev->online_queues--;
Keith Busch2b25d982014-12-22 12:59:04 -07001162 nvmeq->cq_vector = -1;
Matthew Wilcoxa09115b2012-08-07 15:56:23 -04001163 spin_unlock_irq(&nvmeq->q_lock);
1164
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001165 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
Sagi Grimbergc81545f2017-07-02 15:53:27 +03001166 blk_mq_quiesce_queue(nvmeq->dev->ctrl.admin_q);
Keith Busch6df3dbc2015-03-26 13:49:33 -06001167
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001168 pci_free_irq(to_pci_dev(nvmeq->dev->dev), vector, nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001169
Keith Busch4d115422013-12-10 13:10:40 -07001170 return 0;
1171}
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001172
Keith Buscha5cdb682016-01-12 14:41:18 -07001173static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown)
Keith Busch4d115422013-12-10 13:10:40 -07001174{
Keith Buscha5cdb682016-01-12 14:41:18 -07001175 struct nvme_queue *nvmeq = dev->queues[0];
Keith Busch4d115422013-12-10 13:10:40 -07001176
1177 if (!nvmeq)
1178 return;
1179 if (nvme_suspend_queue(nvmeq))
1180 return;
1181
Keith Buscha5cdb682016-01-12 14:41:18 -07001182 if (shutdown)
1183 nvme_shutdown_ctrl(&dev->ctrl);
1184 else
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001185 nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
Keith Busch07836e62015-02-19 10:34:48 -07001186
1187 spin_lock_irq(&nvmeq->q_lock);
1188 nvme_process_cq(nvmeq);
1189 spin_unlock_irq(&nvmeq->q_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001190}
1191
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001192static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1193 int entry_size)
1194{
1195 int q_depth = dev->q_depth;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001196 unsigned q_size_aligned = roundup(q_depth * entry_size,
1197 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001198
1199 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
Jon Derrickc45f5c92015-07-21 15:08:13 -06001200 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001201 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
Jon Derrickc45f5c92015-07-21 15:08:13 -06001202 q_depth = div_u64(mem_per_q, entry_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001203
1204 /*
1205 * Ensure the reduced q_depth is above some threshold where it
1206 * would be better to map queues in system memory with the
1207 * original depth
1208 */
1209 if (q_depth < 64)
1210 return -ENOMEM;
1211 }
1212
1213 return q_depth;
1214}
1215
1216static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1217 int qid, int depth)
1218{
1219 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001220 unsigned offset = (qid - 1) * roundup(SQ_SIZE(depth),
1221 dev->ctrl.page_size);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001222 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1223 nvmeq->sq_cmds_io = dev->cmb + offset;
1224 } else {
1225 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1226 &nvmeq->sq_dma_addr, GFP_KERNEL);
1227 if (!nvmeq->sq_cmds)
1228 return -ENOMEM;
1229 }
1230
1231 return 0;
1232}
1233
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001234static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001235 int depth, int node)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001236{
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001237 struct nvme_queue *nvmeq = kzalloc_node(sizeof(*nvmeq), GFP_KERNEL,
1238 node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001239 if (!nvmeq)
1240 return NULL;
1241
Christoph Hellwige75ec752015-05-22 11:12:39 +02001242 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
Joe Perches4d51abf2014-06-15 13:37:33 -07001243 &nvmeq->cq_dma_addr, GFP_KERNEL);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001244 if (!nvmeq->cqes)
1245 goto free_nvmeq;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001246
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001247 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001248 goto free_cqdma;
1249
Christoph Hellwige75ec752015-05-22 11:12:39 +02001250 nvmeq->q_dmadev = dev->dev;
Matthew Wilcox091b6092011-02-10 09:56:01 -05001251 nvmeq->dev = dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001252 spin_lock_init(&nvmeq->q_lock);
1253 nvmeq->cq_head = 0;
Matthew Wilcox82123462011-01-20 13:24:06 -05001254 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001255 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001256 nvmeq->q_depth = depth;
Keith Buschc30341d2013-12-10 13:10:38 -07001257 nvmeq->qid = qid;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001258 nvmeq->cq_vector = -1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001259 dev->queues[qid] = nvmeq;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001260 dev->ctrl.queue_count++;
Jon Derrick36a7e992015-05-27 12:26:23 -06001261
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001262 return nvmeq;
1263
1264 free_cqdma:
Christoph Hellwige75ec752015-05-22 11:12:39 +02001265 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001266 nvmeq->cq_dma_addr);
1267 free_nvmeq:
1268 kfree(nvmeq);
1269 return NULL;
1270}
1271
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001272static int queue_request_irq(struct nvme_queue *nvmeq)
Matthew Wilcox30010822011-01-20 09:10:15 -05001273{
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001274 struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev);
1275 int nr = nvmeq->dev->ctrl.instance;
1276
1277 if (use_threaded_interrupts) {
1278 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq_check,
1279 nvme_irq, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1280 } else {
1281 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq,
1282 NULL, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1283 }
Matthew Wilcox30010822011-01-20 09:10:15 -05001284}
1285
Keith Busch22404272013-07-15 15:02:20 -06001286static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001287{
Keith Busch22404272013-07-15 15:02:20 -06001288 struct nvme_dev *dev = nvmeq->dev;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001289
Keith Busch7be50e92014-09-10 15:48:47 -06001290 spin_lock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001291 nvmeq->sq_tail = 0;
1292 nvmeq->cq_head = 0;
1293 nvmeq->cq_phase = 1;
Haiyan Hub80d5cc2013-09-10 11:25:37 +08001294 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
Keith Busch22404272013-07-15 15:02:20 -06001295 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
Helen Koikef9f38e32017-04-10 12:51:07 -03001296 nvme_dbbuf_init(dev, nvmeq, qid);
Keith Busch42f61422014-03-24 10:46:25 -06001297 dev->online_queues++;
Keith Busch7be50e92014-09-10 15:48:47 -06001298 spin_unlock_irq(&nvmeq->q_lock);
Keith Busch22404272013-07-15 15:02:20 -06001299}
1300
1301static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1302{
1303 struct nvme_dev *dev = nvmeq->dev;
1304 int result;
Matthew Wilcox3f85d502011-02-01 08:39:04 -05001305
Keith Busch2b25d982014-12-22 12:59:04 -07001306 nvmeq->cq_vector = qid - 1;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001307 result = adapter_alloc_cq(dev, qid, nvmeq);
1308 if (result < 0)
Keith Busch22404272013-07-15 15:02:20 -06001309 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001310
1311 result = adapter_alloc_sq(dev, qid, nvmeq);
1312 if (result < 0)
1313 goto release_cq;
1314
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001315 result = queue_request_irq(nvmeq);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001316 if (result < 0)
1317 goto release_sq;
1318
Keith Busch22404272013-07-15 15:02:20 -06001319 nvme_init_queue(nvmeq, qid);
Keith Busch22404272013-07-15 15:02:20 -06001320 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001321
1322 release_sq:
1323 adapter_delete_sq(dev, qid);
1324 release_cq:
1325 adapter_delete_cq(dev, qid);
Keith Busch22404272013-07-15 15:02:20 -06001326 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001327}
1328
Eric Biggersf363b082017-03-30 13:39:16 -07001329static const struct blk_mq_ops nvme_mq_admin_ops = {
Christoph Hellwigd29ec822015-05-22 11:12:46 +02001330 .queue_rq = nvme_queue_rq,
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001331 .complete = nvme_pci_complete_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001332 .init_hctx = nvme_admin_init_hctx,
Keith Busch4af0e212015-06-08 10:08:13 -06001333 .exit_hctx = nvme_admin_exit_hctx,
Christoph Hellwig03508152017-06-13 09:15:18 +02001334 .init_request = nvme_init_request,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001335 .timeout = nvme_timeout,
1336};
1337
Eric Biggersf363b082017-03-30 13:39:16 -07001338static const struct blk_mq_ops nvme_mq_ops = {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001339 .queue_rq = nvme_queue_rq,
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001340 .complete = nvme_pci_complete_rq,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001341 .init_hctx = nvme_init_hctx,
1342 .init_request = nvme_init_request,
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001343 .map_queues = nvme_pci_map_queues,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001344 .timeout = nvme_timeout,
Jens Axboea0fa9642015-11-03 20:37:26 -07001345 .poll = nvme_poll,
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001346};
1347
Keith Buschea191d22015-01-07 18:55:49 -07001348static void nvme_dev_remove_admin(struct nvme_dev *dev)
1349{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001350 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
Keith Busch69d9a992016-02-24 09:15:56 -07001351 /*
1352 * If the controller was reset during removal, it's possible
1353 * user requests may be waiting on a stopped queue. Start the
1354 * queue to flush these to completion.
1355 */
Sagi Grimbergc81545f2017-07-02 15:53:27 +03001356 blk_mq_unquiesce_queue(dev->ctrl.admin_q);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001357 blk_cleanup_queue(dev->ctrl.admin_q);
Keith Buschea191d22015-01-07 18:55:49 -07001358 blk_mq_free_tag_set(&dev->admin_tagset);
1359 }
1360}
1361
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001362static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1363{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001364 if (!dev->ctrl.admin_q) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001365 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1366 dev->admin_tagset.nr_hw_queues = 1;
Keith Busche3e9d502016-01-04 09:10:55 -07001367
1368 /*
1369 * Subtract one to leave an empty queue entry for 'Full Queue'
1370 * condition. See NVM-Express 1.2 specification, section 4.1.2.
1371 */
1372 dev->admin_tagset.queue_depth = NVME_AQ_BLKMQ_DEPTH - 1;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001373 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001374 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
Jens Axboeac3dd5b2015-01-22 12:07:58 -07001375 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
Jens Axboed3484992017-01-13 14:43:58 -07001376 dev->admin_tagset.flags = BLK_MQ_F_NO_SCHED;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001377 dev->admin_tagset.driver_data = dev;
1378
1379 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1380 return -ENOMEM;
Sagi Grimberg34b6c232017-07-10 09:22:29 +03001381 dev->ctrl.admin_tagset = &dev->admin_tagset;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001382
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001383 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1384 if (IS_ERR(dev->ctrl.admin_q)) {
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001385 blk_mq_free_tag_set(&dev->admin_tagset);
1386 return -ENOMEM;
1387 }
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001388 if (!blk_get_queue(dev->ctrl.admin_q)) {
Keith Buschea191d22015-01-07 18:55:49 -07001389 nvme_dev_remove_admin(dev);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01001390 dev->ctrl.admin_q = NULL;
Keith Buschea191d22015-01-07 18:55:49 -07001391 return -ENODEV;
1392 }
Keith Busch0fb59cb2015-01-07 18:55:50 -07001393 } else
Sagi Grimbergc81545f2017-07-02 15:53:27 +03001394 blk_mq_unquiesce_queue(dev->ctrl.admin_q);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001395
1396 return 0;
1397}
1398
Xu Yu97f6ef62017-05-24 16:39:55 +08001399static unsigned long db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1400{
1401 return NVME_REG_DBS + ((nr_io_queues + 1) * 8 * dev->db_stride);
1402}
1403
1404static int nvme_remap_bar(struct nvme_dev *dev, unsigned long size)
1405{
1406 struct pci_dev *pdev = to_pci_dev(dev->dev);
1407
1408 if (size <= dev->bar_mapped_size)
1409 return 0;
1410 if (size > pci_resource_len(pdev, 0))
1411 return -ENOMEM;
1412 if (dev->bar)
1413 iounmap(dev->bar);
1414 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1415 if (!dev->bar) {
1416 dev->bar_mapped_size = 0;
1417 return -ENOMEM;
1418 }
1419 dev->bar_mapped_size = size;
1420 dev->dbs = dev->bar + NVME_REG_DBS;
1421
1422 return 0;
1423}
1424
Sagi Grimberg01ad0992017-05-01 00:27:17 +03001425static int nvme_pci_configure_admin_queue(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001426{
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001427 int result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001428 u32 aqa;
1429 struct nvme_queue *nvmeq;
Keith Busch1d090622014-06-23 11:34:01 -06001430
Xu Yu97f6ef62017-05-24 16:39:55 +08001431 result = nvme_remap_bar(dev, db_bar_size(dev, 0));
1432 if (result < 0)
1433 return result;
1434
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001435 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1, 0) ?
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001436 NVME_CAP_NSSRC(dev->ctrl.cap) : 0;
Keith Buschdfbac8c2015-08-10 15:20:40 -06001437
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001438 if (dev->subsystem &&
1439 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1440 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
Keith Buschdfbac8c2015-08-10 15:20:40 -06001441
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001442 result = nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
Matthew Wilcoxba47e382013-05-04 06:43:16 -04001443 if (result < 0)
1444 return result;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001445
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001446 nvmeq = dev->queues[0];
Keith Buschcd638942013-07-15 15:02:23 -06001447 if (!nvmeq) {
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001448 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH,
1449 dev_to_node(dev->dev));
Keith Buschcd638942013-07-15 15:02:23 -06001450 if (!nvmeq)
1451 return -ENOMEM;
Keith Buschcd638942013-07-15 15:02:23 -06001452 }
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001453
1454 aqa = nvmeq->q_depth - 1;
1455 aqa |= aqa << 16;
1456
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001457 writel(aqa, dev->bar + NVME_REG_AQA);
1458 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1459 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
Keith Busch1d090622014-06-23 11:34:01 -06001460
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001461 result = nvme_enable_ctrl(&dev->ctrl, dev->ctrl.cap);
Keith Busch025c5572013-05-01 13:07:51 -06001462 if (result)
Keith Buschd4875622016-11-15 15:56:26 -05001463 return result;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001464
Keith Busch2b25d982014-12-22 12:59:04 -07001465 nvmeq->cq_vector = 0;
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001466 result = queue_request_irq(nvmeq);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001467 if (result) {
1468 nvmeq->cq_vector = -1;
Keith Buschd4875622016-11-15 15:56:26 -05001469 return result;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001470 }
Keith Busch025c5572013-05-01 13:07:51 -06001471
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001472 return result;
1473}
1474
Christoph Hellwig749941f2015-11-26 11:46:39 +01001475static int nvme_create_io_queues(struct nvme_dev *dev)
Keith Busch42f61422014-03-24 10:46:25 -06001476{
Keith Busch949928c2015-12-17 17:08:15 -07001477 unsigned i, max;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001478 int ret = 0;
Keith Busch42f61422014-03-24 10:46:25 -06001479
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001480 for (i = dev->ctrl.queue_count; i <= dev->max_qid; i++) {
Shaohua Lid3af3ec2017-02-01 09:53:16 -08001481 /* vector == qid - 1, match nvme_create_queue */
1482 if (!nvme_alloc_queue(dev, i, dev->q_depth,
1483 pci_irq_get_node(to_pci_dev(dev->dev), i - 1))) {
Christoph Hellwig749941f2015-11-26 11:46:39 +01001484 ret = -ENOMEM;
Keith Busch42f61422014-03-24 10:46:25 -06001485 break;
Christoph Hellwig749941f2015-11-26 11:46:39 +01001486 }
1487 }
Keith Busch42f61422014-03-24 10:46:25 -06001488
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001489 max = min(dev->max_qid, dev->ctrl.queue_count - 1);
Keith Busch949928c2015-12-17 17:08:15 -07001490 for (i = dev->online_queues; i <= max; i++) {
Christoph Hellwig749941f2015-11-26 11:46:39 +01001491 ret = nvme_create_queue(dev->queues[i], i);
Keith Buschd4875622016-11-15 15:56:26 -05001492 if (ret)
Keith Busch42f61422014-03-24 10:46:25 -06001493 break;
Matthew Wilcox27e81662014-04-11 11:58:45 -04001494 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001495
1496 /*
1497 * Ignore failing Create SQ/CQ commands, we can continue with less
1498 * than the desired aount of queues, and even a controller without
1499 * I/O queues an still be used to issue admin commands. This might
1500 * be useful to upgrade a buggy firmware for example.
1501 */
1502 return ret >= 0 ? 0 : ret;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001503}
1504
Stephen Bates202021c2016-10-05 20:01:12 -06001505static ssize_t nvme_cmb_show(struct device *dev,
1506 struct device_attribute *attr,
1507 char *buf)
1508{
1509 struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
1510
Stephen Batesc9658092016-12-16 11:54:50 -07001511 return scnprintf(buf, PAGE_SIZE, "cmbloc : x%08x\ncmbsz : x%08x\n",
Stephen Bates202021c2016-10-05 20:01:12 -06001512 ndev->cmbloc, ndev->cmbsz);
1513}
1514static DEVICE_ATTR(cmb, S_IRUGO, nvme_cmb_show, NULL);
1515
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001516static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1517{
1518 u64 szu, size, offset;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001519 resource_size_t bar_size;
1520 struct pci_dev *pdev = to_pci_dev(dev->dev);
1521 void __iomem *cmb;
1522 dma_addr_t dma_addr;
1523
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001524 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001525 if (!(NVME_CMB_SZ(dev->cmbsz)))
1526 return NULL;
Stephen Bates202021c2016-10-05 20:01:12 -06001527 dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001528
Stephen Bates202021c2016-10-05 20:01:12 -06001529 if (!use_cmb_sqes)
1530 return NULL;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001531
1532 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1533 size = szu * NVME_CMB_SZ(dev->cmbsz);
Stephen Bates202021c2016-10-05 20:01:12 -06001534 offset = szu * NVME_CMB_OFST(dev->cmbloc);
1535 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(dev->cmbloc));
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001536
1537 if (offset > bar_size)
1538 return NULL;
1539
1540 /*
1541 * Controllers may support a CMB size larger than their BAR,
1542 * for example, due to being behind a bridge. Reduce the CMB to
1543 * the reported size of the BAR
1544 */
1545 if (size > bar_size - offset)
1546 size = bar_size - offset;
1547
Stephen Bates202021c2016-10-05 20:01:12 -06001548 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(dev->cmbloc)) + offset;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001549 cmb = ioremap_wc(dma_addr, size);
1550 if (!cmb)
1551 return NULL;
1552
1553 dev->cmb_dma_addr = dma_addr;
1554 dev->cmb_size = size;
1555 return cmb;
1556}
1557
1558static inline void nvme_release_cmb(struct nvme_dev *dev)
1559{
1560 if (dev->cmb) {
1561 iounmap(dev->cmb);
1562 dev->cmb = NULL;
Max Gurtovoy1c78f772017-07-30 01:45:08 +03001563 sysfs_remove_file_from_group(&dev->ctrl.device->kobj,
1564 &dev_attr_cmb.attr, NULL);
1565 dev->cmbsz = 0;
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001566 }
1567}
1568
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001569static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits)
Keith Busch9d713c22013-07-15 15:02:24 -06001570{
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001571 size_t len = dev->nr_host_mem_descs * sizeof(*dev->host_mem_descs);
1572 struct nvme_command c;
1573 u64 dma_addr;
1574 int ret;
1575
1576 dma_addr = dma_map_single(dev->dev, dev->host_mem_descs, len,
1577 DMA_TO_DEVICE);
1578 if (dma_mapping_error(dev->dev, dma_addr))
1579 return -ENOMEM;
1580
1581 memset(&c, 0, sizeof(c));
1582 c.features.opcode = nvme_admin_set_features;
1583 c.features.fid = cpu_to_le32(NVME_FEAT_HOST_MEM_BUF);
1584 c.features.dword11 = cpu_to_le32(bits);
1585 c.features.dword12 = cpu_to_le32(dev->host_mem_size >>
1586 ilog2(dev->ctrl.page_size));
1587 c.features.dword13 = cpu_to_le32(lower_32_bits(dma_addr));
1588 c.features.dword14 = cpu_to_le32(upper_32_bits(dma_addr));
1589 c.features.dword15 = cpu_to_le32(dev->nr_host_mem_descs);
1590
1591 ret = nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1592 if (ret) {
1593 dev_warn(dev->ctrl.device,
1594 "failed to set host mem (err %d, flags %#x).\n",
1595 ret, bits);
1596 }
1597 dma_unmap_single(dev->dev, dma_addr, len, DMA_TO_DEVICE);
1598 return ret;
1599}
1600
1601static void nvme_free_host_mem(struct nvme_dev *dev)
1602{
1603 int i;
1604
1605 for (i = 0; i < dev->nr_host_mem_descs; i++) {
1606 struct nvme_host_mem_buf_desc *desc = &dev->host_mem_descs[i];
1607 size_t size = le32_to_cpu(desc->size) * dev->ctrl.page_size;
1608
1609 dma_free_coherent(dev->dev, size, dev->host_mem_desc_bufs[i],
1610 le64_to_cpu(desc->addr));
1611 }
1612
1613 kfree(dev->host_mem_desc_bufs);
1614 dev->host_mem_desc_bufs = NULL;
1615 kfree(dev->host_mem_descs);
1616 dev->host_mem_descs = NULL;
1617}
1618
1619static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
1620{
1621 struct nvme_host_mem_buf_desc *descs;
Christoph Hellwig50cdb7c2017-07-25 17:39:07 +02001622 u32 chunk_size, max_entries, len;
Dan Carpenter2ee0e4e2017-07-06 12:26:52 +03001623 int i = 0;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001624 void **bufs;
Dan Carpenter2ee0e4e2017-07-06 12:26:52 +03001625 u64 size = 0, tmp;
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001626
1627 /* start big and work our way down */
1628 chunk_size = min(preferred, (u64)PAGE_SIZE << MAX_ORDER);
1629retry:
1630 tmp = (preferred + chunk_size - 1);
1631 do_div(tmp, chunk_size);
1632 max_entries = tmp;
1633 descs = kcalloc(max_entries, sizeof(*descs), GFP_KERNEL);
1634 if (!descs)
1635 goto out;
1636
1637 bufs = kcalloc(max_entries, sizeof(*bufs), GFP_KERNEL);
1638 if (!bufs)
1639 goto out_free_descs;
1640
Christoph Hellwig50cdb7c2017-07-25 17:39:07 +02001641 for (size = 0; size < preferred; size += len) {
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001642 dma_addr_t dma_addr;
1643
Christoph Hellwig50cdb7c2017-07-25 17:39:07 +02001644 len = min_t(u64, chunk_size, preferred - size);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02001645 bufs[i] = dma_alloc_attrs(dev->dev, len, &dma_addr, GFP_KERNEL,
1646 DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
1647 if (!bufs[i])
1648 break;
1649
1650 descs[i].addr = cpu_to_le64(dma_addr);
1651 descs[i].size = cpu_to_le32(len / dev->ctrl.page_size);
1652 i++;
1653 }
1654
1655 if (!size || (min && size < min)) {
1656 dev_warn(dev->ctrl.device,
1657 "failed to allocate host memory buffer.\n");
1658 goto out_free_bufs;
1659 }
1660
1661 dev_info(dev->ctrl.device,
1662 "allocated %lld MiB host memory buffer.\n",
1663 size >> ilog2(SZ_1M));
1664 dev->nr_host_mem_descs = i;
1665 dev->host_mem_size = size;
1666 dev->host_mem_descs = descs;
1667 dev->host_mem_desc_bufs = bufs;
1668 return 0;
1669
1670out_free_bufs:
1671 while (--i >= 0) {
1672 size_t size = le32_to_cpu(descs[i].size) * dev->ctrl.page_size;
1673
1674 dma_free_coherent(dev->dev, size, bufs[i],
1675 le64_to_cpu(descs[i].addr));
1676 }
1677
1678 kfree(bufs);
1679out_free_descs:
1680 kfree(descs);
1681out:
1682 /* try a smaller chunk size if we failed early */
1683 if (chunk_size >= PAGE_SIZE * 2 && (i == 0 || size < min)) {
1684 chunk_size /= 2;
1685 goto retry;
1686 }
1687 dev->host_mem_descs = NULL;
1688 return -ENOMEM;
1689}
1690
1691static void nvme_setup_host_mem(struct nvme_dev *dev)
1692{
1693 u64 max = (u64)max_host_mem_size_mb * SZ_1M;
1694 u64 preferred = (u64)dev->ctrl.hmpre * 4096;
1695 u64 min = (u64)dev->ctrl.hmmin * 4096;
1696 u32 enable_bits = NVME_HOST_MEM_ENABLE;
1697
1698 preferred = min(preferred, max);
1699 if (min > max) {
1700 dev_warn(dev->ctrl.device,
1701 "min host memory (%lld MiB) above limit (%d MiB).\n",
1702 min >> ilog2(SZ_1M), max_host_mem_size_mb);
1703 nvme_free_host_mem(dev);
1704 return;
1705 }
1706
1707 /*
1708 * If we already have a buffer allocated check if we can reuse it.
1709 */
1710 if (dev->host_mem_descs) {
1711 if (dev->host_mem_size >= min)
1712 enable_bits |= NVME_HOST_MEM_RETURN;
1713 else
1714 nvme_free_host_mem(dev);
1715 }
1716
1717 if (!dev->host_mem_descs) {
1718 if (nvme_alloc_host_mem(dev, min, preferred))
1719 return;
1720 }
1721
1722 if (nvme_set_host_mem(dev, enable_bits))
1723 nvme_free_host_mem(dev);
Keith Busch9d713c22013-07-15 15:02:24 -06001724}
1725
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001726static int nvme_setup_io_queues(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001727{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001728 struct nvme_queue *adminq = dev->queues[0];
Christoph Hellwige75ec752015-05-22 11:12:39 +02001729 struct pci_dev *pdev = to_pci_dev(dev->dev);
Xu Yu97f6ef62017-05-24 16:39:55 +08001730 int result, nr_io_queues;
1731 unsigned long size;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001732
Christoph Hellwig425a17c2017-06-26 12:20:58 +02001733 nr_io_queues = num_present_cpus();
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001734 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
1735 if (result < 0)
Matthew Wilcox1b234842011-01-20 13:01:49 -05001736 return result;
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001737
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001738 if (nr_io_queues == 0)
Keith Buscha5229052016-04-08 16:09:10 -06001739 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001740
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001741 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1742 result = nvme_cmb_qdepth(dev, nr_io_queues,
1743 sizeof(struct nvme_command));
1744 if (result > 0)
1745 dev->q_depth = result;
1746 else
1747 nvme_release_cmb(dev);
1748 }
1749
Xu Yu97f6ef62017-05-24 16:39:55 +08001750 do {
1751 size = db_bar_size(dev, nr_io_queues);
1752 result = nvme_remap_bar(dev, size);
1753 if (!result)
1754 break;
1755 if (!--nr_io_queues)
1756 return -ENOMEM;
1757 } while (1);
1758 adminq->q_db = dev->dbs;
Matthew Wilcoxf1938f62011-10-20 17:00:41 -04001759
Keith Busch9d713c22013-07-15 15:02:24 -06001760 /* Deregister the admin queue's interrupt */
Christoph Hellwig0ff199c2017-04-13 09:06:43 +02001761 pci_free_irq(pdev, 0, adminq);
Keith Busch9d713c22013-07-15 15:02:24 -06001762
Jens Axboee32efbf2014-11-14 09:49:26 -07001763 /*
1764 * If we enable msix early due to not intx, disable it again before
1765 * setting up the full range we need.
1766 */
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001767 pci_free_irq_vectors(pdev);
1768 nr_io_queues = pci_alloc_irq_vectors(pdev, 1, nr_io_queues,
1769 PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY);
1770 if (nr_io_queues <= 0)
1771 return -EIO;
1772 dev->max_qid = nr_io_queues;
Matthew Wilcox1b234842011-01-20 13:01:49 -05001773
Matthew Wilcox063a8092013-06-20 10:53:48 -04001774 /*
1775 * Should investigate if there's a performance win from allocating
1776 * more queues than interrupt vectors; it might allow the submission
1777 * path to scale better, even if the receive path is limited by the
1778 * number of interrupts.
1779 */
Ramachandra Rao Gajulafa08a392013-05-11 15:19:31 -07001780
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001781 result = queue_request_irq(adminq);
Jon Derrick758dd7f2015-06-30 11:22:52 -06001782 if (result) {
1783 adminq->cq_vector = -1;
Keith Buschd4875622016-11-15 15:56:26 -05001784 return result;
Jon Derrick758dd7f2015-06-30 11:22:52 -06001785 }
Christoph Hellwig749941f2015-11-26 11:46:39 +01001786 return nvme_create_io_queues(dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001787}
1788
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001789static void nvme_del_queue_end(struct request *req, blk_status_t error)
Keith Buschdb3cbff2016-01-12 14:41:17 -07001790{
1791 struct nvme_queue *nvmeq = req->end_io_data;
1792
1793 blk_mq_free_request(req);
1794 complete(&nvmeq->dev->ioq_wait);
1795}
1796
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001797static void nvme_del_cq_end(struct request *req, blk_status_t error)
Keith Buschdb3cbff2016-01-12 14:41:17 -07001798{
1799 struct nvme_queue *nvmeq = req->end_io_data;
1800
1801 if (!error) {
1802 unsigned long flags;
1803
Ming Lin2e39e0f2016-04-05 10:32:04 -07001804 /*
1805 * We might be called with the AQ q_lock held
1806 * and the I/O queue q_lock should always
1807 * nest inside the AQ one.
1808 */
1809 spin_lock_irqsave_nested(&nvmeq->q_lock, flags,
1810 SINGLE_DEPTH_NESTING);
Keith Buschdb3cbff2016-01-12 14:41:17 -07001811 nvme_process_cq(nvmeq);
1812 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
1813 }
1814
1815 nvme_del_queue_end(req, error);
1816}
1817
1818static int nvme_delete_queue(struct nvme_queue *nvmeq, u8 opcode)
1819{
1820 struct request_queue *q = nvmeq->dev->ctrl.admin_q;
1821 struct request *req;
1822 struct nvme_command cmd;
1823
1824 memset(&cmd, 0, sizeof(cmd));
1825 cmd.delete_queue.opcode = opcode;
1826 cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid);
1827
Christoph Hellwigeb71f432016-06-13 16:45:23 +02001828 req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
Keith Buschdb3cbff2016-01-12 14:41:17 -07001829 if (IS_ERR(req))
1830 return PTR_ERR(req);
1831
1832 req->timeout = ADMIN_TIMEOUT;
1833 req->end_io_data = nvmeq;
1834
1835 blk_execute_rq_nowait(q, NULL, req, false,
1836 opcode == nvme_admin_delete_cq ?
1837 nvme_del_cq_end : nvme_del_queue_end);
1838 return 0;
1839}
1840
Keith Busch70659062016-10-12 09:22:16 -06001841static void nvme_disable_io_queues(struct nvme_dev *dev, int queues)
Keith Buschdb3cbff2016-01-12 14:41:17 -07001842{
Keith Busch70659062016-10-12 09:22:16 -06001843 int pass;
Keith Buschdb3cbff2016-01-12 14:41:17 -07001844 unsigned long timeout;
1845 u8 opcode = nvme_admin_delete_sq;
1846
1847 for (pass = 0; pass < 2; pass++) {
Keith Busch014a0d62016-05-06 11:50:52 -06001848 int sent = 0, i = queues;
Keith Buschdb3cbff2016-01-12 14:41:17 -07001849
1850 reinit_completion(&dev->ioq_wait);
1851 retry:
1852 timeout = ADMIN_TIMEOUT;
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06001853 for (; i > 0; i--, sent++)
1854 if (nvme_delete_queue(dev->queues[i], opcode))
Keith Buschdb3cbff2016-01-12 14:41:17 -07001855 break;
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06001856
Keith Buschdb3cbff2016-01-12 14:41:17 -07001857 while (sent--) {
1858 timeout = wait_for_completion_io_timeout(&dev->ioq_wait, timeout);
1859 if (timeout == 0)
1860 return;
1861 if (i)
1862 goto retry;
1863 }
1864 opcode = nvme_admin_delete_cq;
1865 }
1866}
1867
Matthew Wilcox422ef0c2013-04-16 11:22:36 -04001868/*
1869 * Return: error value if an error occurred setting up the queues or calling
1870 * Identify Device. 0 if these succeeded, even if adding some of the
1871 * namespaces failed. At the moment, these failures are silent. TBD which
1872 * failures should be reported.
1873 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08001874static int nvme_dev_add(struct nvme_dev *dev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001875{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001876 if (!dev->ctrl.tagset) {
Keith Buschffe77042015-06-08 10:08:15 -06001877 dev->tagset.ops = &nvme_mq_ops;
1878 dev->tagset.nr_hw_queues = dev->online_queues - 1;
1879 dev->tagset.timeout = NVME_IO_TIMEOUT;
1880 dev->tagset.numa_node = dev_to_node(dev->dev);
1881 dev->tagset.queue_depth =
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001882 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
Keith Buschffe77042015-06-08 10:08:15 -06001883 dev->tagset.cmd_size = nvme_cmd_size(dev);
1884 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
1885 dev->tagset.driver_data = dev;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07001886
Keith Buschffe77042015-06-08 10:08:15 -06001887 if (blk_mq_alloc_tag_set(&dev->tagset))
1888 return 0;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001889 dev->ctrl.tagset = &dev->tagset;
Helen Koikef9f38e32017-04-10 12:51:07 -03001890
1891 nvme_dbbuf_set(dev);
Keith Busch949928c2015-12-17 17:08:15 -07001892 } else {
1893 blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1);
1894
1895 /* Free previously allocated queues that are no longer usable */
1896 nvme_free_queues(dev, dev->online_queues);
Keith Buschffe77042015-06-08 10:08:15 -06001897 }
Keith Busch949928c2015-12-17 17:08:15 -07001898
Keith Busche1e5e562015-02-19 13:39:03 -07001899 return 0;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05001900}
1901
Keith Buschb00a7262016-02-24 09:15:52 -07001902static int nvme_pci_enable(struct nvme_dev *dev)
Keith Busch0877cb02013-07-15 15:02:19 -06001903{
Keith Buschb00a7262016-02-24 09:15:52 -07001904 int result = -ENOMEM;
Christoph Hellwige75ec752015-05-22 11:12:39 +02001905 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch0877cb02013-07-15 15:02:19 -06001906
1907 if (pci_enable_device_mem(pdev))
1908 return result;
1909
Keith Busch0877cb02013-07-15 15:02:19 -06001910 pci_set_master(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001911
Christoph Hellwige75ec752015-05-22 11:12:39 +02001912 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
1913 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
Russell King052d0ef2013-06-26 23:49:11 +01001914 goto disable;
Keith Busch0877cb02013-07-15 15:02:19 -06001915
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001916 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
Keith Busch0e53d182013-12-10 13:10:39 -07001917 result = -ENODEV;
Keith Buschb00a7262016-02-24 09:15:52 -07001918 goto disable;
Keith Busch0e53d182013-12-10 13:10:39 -07001919 }
Jens Axboee32efbf2014-11-14 09:49:26 -07001920
1921 /*
Keith Buscha5229052016-04-08 16:09:10 -06001922 * Some devices and/or platforms don't advertise or work with INTx
1923 * interrupts. Pre-enable a single MSIX or MSI vec for setup. We'll
1924 * adjust this later.
Jens Axboee32efbf2014-11-14 09:49:26 -07001925 */
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001926 result = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
1927 if (result < 0)
1928 return result;
Jens Axboee32efbf2014-11-14 09:49:26 -07001929
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001930 dev->ctrl.cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001931
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001932 dev->q_depth = min_t(int, NVME_CAP_MQES(dev->ctrl.cap) + 1,
weiping zhangb27c1e62017-07-10 16:46:59 +08001933 io_queue_depth);
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001934 dev->db_stride = 1 << NVME_CAP_STRIDE(dev->ctrl.cap);
Christoph Hellwig7a67cbe2015-11-20 08:58:10 +01001935 dev->dbs = dev->bar + 4096;
Stephan Günther1f390c12015-12-01 13:23:22 -07001936
1937 /*
1938 * Temporary fix for the Apple controller found in the MacBook8,1 and
1939 * some MacBook7,1 to avoid controller resets and data loss.
1940 */
1941 if (pdev->vendor == PCI_VENDOR_ID_APPLE && pdev->device == 0x2001) {
1942 dev->q_depth = 2;
Christoph Hellwig9bdcfb12017-05-20 15:14:43 +02001943 dev_warn(dev->ctrl.device, "detected Apple NVMe controller, "
1944 "set queue depth=%u to work around controller resets\n",
Stephan Günther1f390c12015-12-01 13:23:22 -07001945 dev->q_depth);
Martin K. Petersend554b5e2017-06-27 22:27:57 -04001946 } else if (pdev->vendor == PCI_VENDOR_ID_SAMSUNG &&
1947 (pdev->device == 0xa821 || pdev->device == 0xa822) &&
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001948 NVME_CAP_MQES(dev->ctrl.cap) == 0) {
Martin K. Petersend554b5e2017-06-27 22:27:57 -04001949 dev->q_depth = 64;
1950 dev_err(dev->ctrl.device, "detected PM1725 NVMe controller, "
1951 "set queue depth=%u\n", dev->q_depth);
Stephan Günther1f390c12015-12-01 13:23:22 -07001952 }
1953
Stephen Bates202021c2016-10-05 20:01:12 -06001954 /*
1955 * CMBs can currently only exist on >=1.2 PCIe devices. We only
Max Gurtovoy1c78f772017-07-30 01:45:08 +03001956 * populate sysfs if a CMB is implemented. Since nvme_dev_attrs_group
1957 * has no name we can pass NULL as final argument to
1958 * sysfs_add_file_to_group.
Stephen Bates202021c2016-10-05 20:01:12 -06001959 */
1960
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001961 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2, 0)) {
Jon Derrick8ffaadf2015-07-20 10:14:09 -06001962 dev->cmb = nvme_map_cmb(dev);
Max Gurtovoy1c78f772017-07-30 01:45:08 +03001963 if (dev->cmb) {
Stephen Bates202021c2016-10-05 20:01:12 -06001964 if (sysfs_add_file_to_group(&dev->ctrl.device->kobj,
1965 &dev_attr_cmb.attr, NULL))
Christoph Hellwig9bdcfb12017-05-20 15:14:43 +02001966 dev_warn(dev->ctrl.device,
Stephen Bates202021c2016-10-05 20:01:12 -06001967 "failed to add sysfs attribute for CMB\n");
1968 }
1969 }
1970
Keith Buscha0a34082015-12-07 15:30:31 -07001971 pci_enable_pcie_error_reporting(pdev);
1972 pci_save_state(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001973 return 0;
1974
1975 disable:
Keith Busch0877cb02013-07-15 15:02:19 -06001976 pci_disable_device(pdev);
1977 return result;
1978}
1979
1980static void nvme_dev_unmap(struct nvme_dev *dev)
1981{
Keith Buschb00a7262016-02-24 09:15:52 -07001982 if (dev->bar)
1983 iounmap(dev->bar);
Johannes Thumshirna1f447b2016-06-07 09:44:02 +02001984 pci_release_mem_regions(to_pci_dev(dev->dev));
Keith Buschb00a7262016-02-24 09:15:52 -07001985}
1986
1987static void nvme_pci_disable(struct nvme_dev *dev)
1988{
Christoph Hellwige75ec752015-05-22 11:12:39 +02001989 struct pci_dev *pdev = to_pci_dev(dev->dev);
1990
Jon Derrickf63572d2017-05-05 14:52:06 -06001991 nvme_release_cmb(dev);
Christoph Hellwigdca51e72016-09-14 16:18:57 +02001992 pci_free_irq_vectors(pdev);
Keith Busch0877cb02013-07-15 15:02:19 -06001993
Keith Buscha0a34082015-12-07 15:30:31 -07001994 if (pci_is_enabled(pdev)) {
1995 pci_disable_pcie_error_reporting(pdev);
Christoph Hellwige75ec752015-05-22 11:12:39 +02001996 pci_disable_device(pdev);
Keith Busch4d115422013-12-10 13:10:40 -07001997 }
Keith Busch4d115422013-12-10 13:10:40 -07001998}
1999
Keith Buscha5cdb682016-01-12 14:41:18 -07002000static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002001{
Keith Busch70659062016-10-12 09:22:16 -06002002 int i, queues;
Keith Busch302ad8c2017-03-01 14:22:12 -05002003 bool dead = true;
2004 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch22404272013-07-15 15:02:20 -06002005
Keith Busch77bf25e2015-11-26 12:21:29 +01002006 mutex_lock(&dev->shutdown_lock);
Keith Busch302ad8c2017-03-01 14:22:12 -05002007 if (pci_is_enabled(pdev)) {
2008 u32 csts = readl(dev->bar + NVME_REG_CSTS);
2009
Keith Buschebef7362017-06-27 17:44:05 -06002010 if (dev->ctrl.state == NVME_CTRL_LIVE ||
2011 dev->ctrl.state == NVME_CTRL_RESETTING)
Keith Busch302ad8c2017-03-01 14:22:12 -05002012 nvme_start_freeze(&dev->ctrl);
2013 dead = !!((csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) ||
2014 pdev->error_state != pci_channel_io_normal);
Keith Buschc9d3bf82015-01-07 18:55:52 -07002015 }
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06002016
Keith Busch302ad8c2017-03-01 14:22:12 -05002017 /*
2018 * Give the controller a chance to complete all entered requests if
2019 * doing a safe shutdown.
2020 */
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002021 if (!dead) {
2022 if (shutdown)
2023 nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
2024
2025 /*
2026 * If the controller is still alive tell it to stop using the
2027 * host memory buffer. In theory the shutdown / reset should
2028 * make sure that it doesn't access the host memoery anymore,
2029 * but I'd rather be safe than sorry..
2030 */
2031 if (dev->host_mem_descs)
2032 nvme_set_host_mem(dev, 0);
2033
2034 }
Keith Busch302ad8c2017-03-01 14:22:12 -05002035 nvme_stop_queues(&dev->ctrl);
2036
Keith Busch70659062016-10-12 09:22:16 -06002037 queues = dev->online_queues - 1;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002038 for (i = dev->ctrl.queue_count - 1; i > 0; i--)
Gabriel Krisman Bertazic21377f2016-08-11 09:35:57 -06002039 nvme_suspend_queue(dev->queues[i]);
2040
Keith Busch302ad8c2017-03-01 14:22:12 -05002041 if (dead) {
Gabriel Krisman Bertazi82469c52016-09-06 17:39:13 -03002042 /* A device might become IO incapable very soon during
2043 * probe, before the admin queue is configured. Thus,
2044 * queue_count can be 0 here.
2045 */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002046 if (dev->ctrl.queue_count)
Gabriel Krisman Bertazi82469c52016-09-06 17:39:13 -03002047 nvme_suspend_queue(dev->queues[0]);
Keith Busch4d115422013-12-10 13:10:40 -07002048 } else {
Keith Busch70659062016-10-12 09:22:16 -06002049 nvme_disable_io_queues(dev, queues);
Keith Buscha5cdb682016-01-12 14:41:18 -07002050 nvme_disable_admin_queue(dev, shutdown);
Keith Busch4d115422013-12-10 13:10:40 -07002051 }
Keith Buschb00a7262016-02-24 09:15:52 -07002052 nvme_pci_disable(dev);
Keith Busch07836e62015-02-19 10:34:48 -07002053
Ming Line1958e62016-05-18 14:05:01 -07002054 blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl);
2055 blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl);
Keith Busch302ad8c2017-03-01 14:22:12 -05002056
2057 /*
2058 * The driver will not be starting up queues again if shutting down so
2059 * must flush all entered requests to their failed completion to avoid
2060 * deadlocking blk-mq hot-cpu notifier.
2061 */
2062 if (shutdown)
2063 nvme_start_queues(&dev->ctrl);
Keith Busch77bf25e2015-11-26 12:21:29 +01002064 mutex_unlock(&dev->shutdown_lock);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002065}
2066
Matthew Wilcox091b6092011-02-10 09:56:01 -05002067static int nvme_setup_prp_pools(struct nvme_dev *dev)
2068{
Christoph Hellwige75ec752015-05-22 11:12:39 +02002069 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
Matthew Wilcox091b6092011-02-10 09:56:01 -05002070 PAGE_SIZE, PAGE_SIZE, 0);
2071 if (!dev->prp_page_pool)
2072 return -ENOMEM;
2073
Matthew Wilcox99802a72011-02-10 10:30:34 -05002074 /* Optimisation for I/Os between 4k and 128k */
Christoph Hellwige75ec752015-05-22 11:12:39 +02002075 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
Matthew Wilcox99802a72011-02-10 10:30:34 -05002076 256, 256, 0);
2077 if (!dev->prp_small_pool) {
2078 dma_pool_destroy(dev->prp_page_pool);
2079 return -ENOMEM;
2080 }
Matthew Wilcox091b6092011-02-10 09:56:01 -05002081 return 0;
2082}
2083
2084static void nvme_release_prp_pools(struct nvme_dev *dev)
2085{
2086 dma_pool_destroy(dev->prp_page_pool);
Matthew Wilcox99802a72011-02-10 10:30:34 -05002087 dma_pool_destroy(dev->prp_small_pool);
Matthew Wilcox091b6092011-02-10 09:56:01 -05002088}
2089
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002090static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002091{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002092 struct nvme_dev *dev = to_nvme_dev(ctrl);
Keith Busch9ac27092014-01-31 16:53:39 -07002093
Helen Koikef9f38e32017-04-10 12:51:07 -03002094 nvme_dbbuf_dma_free(dev);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002095 put_device(dev->dev);
Keith Busch4af0e212015-06-08 10:08:13 -06002096 if (dev->tagset.tags)
2097 blk_mq_free_tag_set(&dev->tagset);
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002098 if (dev->ctrl.admin_q)
2099 blk_put_queue(dev->ctrl.admin_q);
Keith Busch5e82e952013-02-19 10:17:58 -07002100 kfree(dev->queues);
Scott Bauere286bcf2017-02-22 10:15:07 -07002101 free_opal_dev(dev->ctrl.opal_dev);
Keith Busch5e82e952013-02-19 10:17:58 -07002102 kfree(dev);
2103}
2104
Keith Buschf58944e2016-02-24 09:15:55 -07002105static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
2106{
Linus Torvalds237045f2016-03-18 17:13:31 -07002107 dev_warn(dev->ctrl.device, "Removing after probe failure status: %d\n", status);
Keith Buschf58944e2016-02-24 09:15:55 -07002108
2109 kref_get(&dev->ctrl.kref);
Keith Busch69d9a992016-02-24 09:15:56 -07002110 nvme_dev_disable(dev, false);
Keith Buschf58944e2016-02-24 09:15:55 -07002111 if (!schedule_work(&dev->remove_work))
2112 nvme_put_ctrl(&dev->ctrl);
2113}
2114
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002115static void nvme_reset_work(struct work_struct *work)
Keith Busch5e82e952013-02-19 10:17:58 -07002116{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002117 struct nvme_dev *dev =
2118 container_of(work, struct nvme_dev, ctrl.reset_work);
Scott Bauera98e58e52017-02-03 12:50:32 -07002119 bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
Keith Buschf58944e2016-02-24 09:15:55 -07002120 int result = -ENODEV;
Keith Buschf0b50732013-07-15 15:02:21 -06002121
Rakesh Pandit82b057c2017-06-05 14:43:11 +03002122 if (WARN_ON(dev->ctrl.state != NVME_CTRL_RESETTING))
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002123 goto out;
2124
2125 /*
2126 * If we're called to reset a live controller first shut it down before
2127 * moving on.
2128 */
Keith Buschb00a7262016-02-24 09:15:52 -07002129 if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
Keith Buscha5cdb682016-01-12 14:41:18 -07002130 nvme_dev_disable(dev, false);
Christoph Hellwigfd634f412015-11-26 12:42:26 +01002131
Keith Buschb00a7262016-02-24 09:15:52 -07002132 result = nvme_pci_enable(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002133 if (result)
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002134 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002135
Sagi Grimberg01ad0992017-05-01 00:27:17 +03002136 result = nvme_pci_configure_admin_queue(dev);
Keith Buschf0b50732013-07-15 15:02:21 -06002137 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002138 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002139
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002140 nvme_init_queue(dev->queues[0], 0);
Keith Busch0fb59cb2015-01-07 18:55:50 -07002141 result = nvme_alloc_admin_tags(dev);
2142 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002143 goto out;
Dan McLeranb9afca32014-04-07 17:10:11 -06002144
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002145 result = nvme_init_identify(&dev->ctrl);
2146 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002147 goto out;
Christoph Hellwigce4541f2015-10-16 07:58:46 +02002148
Scott Bauere286bcf2017-02-22 10:15:07 -07002149 if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) {
2150 if (!dev->ctrl.opal_dev)
2151 dev->ctrl.opal_dev =
2152 init_opal_dev(&dev->ctrl, &nvme_sec_submit);
2153 else if (was_suspend)
2154 opal_unlock_from_suspend(dev->ctrl.opal_dev);
2155 } else {
2156 free_opal_dev(dev->ctrl.opal_dev);
2157 dev->ctrl.opal_dev = NULL;
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01002158 }
Scott Bauera98e58e52017-02-03 12:50:32 -07002159
Helen Koikef9f38e32017-04-10 12:51:07 -03002160 if (dev->ctrl.oacs & NVME_CTRL_OACS_DBBUF_SUPP) {
2161 result = nvme_dbbuf_dma_alloc(dev);
2162 if (result)
2163 dev_warn(dev->dev,
2164 "unable to allocate dma for dbbuf\n");
2165 }
2166
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002167 if (dev->ctrl.hmpre)
2168 nvme_setup_host_mem(dev);
2169
Keith Buschf0b50732013-07-15 15:02:21 -06002170 result = nvme_setup_io_queues(dev);
Keith Buschbadc34d2014-06-23 14:25:35 -06002171 if (result)
Keith Buschf58944e2016-02-24 09:15:55 -07002172 goto out;
Keith Buschf0b50732013-07-15 15:02:21 -06002173
Keith Busch21f033f2016-04-12 11:13:11 -06002174 /*
Christoph Hellwig2659e572015-10-02 18:51:31 +02002175 * Keep the controller around but remove all namespaces if we don't have
2176 * any working I/O queue.
2177 */
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002178 if (dev->online_queues < 2) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002179 dev_warn(dev->ctrl.device, "IO queues not created\n");
Keith Busch3b247742016-04-27 15:51:18 -06002180 nvme_kill_queues(&dev->ctrl);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002181 nvme_remove_namespaces(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002182 } else {
Keith Busch25646262016-01-04 09:10:57 -07002183 nvme_start_queues(&dev->ctrl);
Keith Busch302ad8c2017-03-01 14:22:12 -05002184 nvme_wait_freeze(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002185 nvme_dev_add(dev);
Keith Busch302ad8c2017-03-01 14:22:12 -05002186 nvme_unfreeze(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002187 }
2188
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002189 if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) {
2190 dev_warn(dev->ctrl.device, "failed to mark controller live\n");
2191 goto out;
2192 }
Christoph Hellwig92911a52016-04-26 13:51:58 +02002193
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002194 nvme_start_ctrl(&dev->ctrl);
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002195 return;
Keith Buschf0b50732013-07-15 15:02:21 -06002196
Christoph Hellwig3cf519b2015-10-03 09:49:23 +02002197 out:
Keith Buschf58944e2016-02-24 09:15:55 -07002198 nvme_remove_dead_ctrl(dev, result);
Keith Buschf0b50732013-07-15 15:02:21 -06002199}
2200
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002201static void nvme_remove_dead_ctrl_work(struct work_struct *work)
Keith Busch9a6b9452013-12-10 13:10:36 -07002202{
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002203 struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
Christoph Hellwige75ec752015-05-22 11:12:39 +02002204 struct pci_dev *pdev = to_pci_dev(dev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002205
Keith Busch69d9a992016-02-24 09:15:56 -07002206 nvme_kill_queues(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002207 if (pci_get_drvdata(pdev))
Keith Busch921920a2016-03-28 16:03:21 -06002208 device_release_driver(&pdev->dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002209 nvme_put_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002210}
2211
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002212static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
Keith Busch4cc06522015-06-05 10:30:08 -06002213{
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002214 *val = readl(to_nvme_dev(ctrl)->bar + off);
2215 return 0;
Keith Busch4cc06522015-06-05 10:30:08 -06002216}
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002217
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002218static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2219{
2220 writel(val, to_nvme_dev(ctrl)->bar + off);
2221 return 0;
2222}
2223
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002224static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2225{
2226 *val = readq(to_nvme_dev(ctrl)->bar + off);
2227 return 0;
2228}
2229
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002230static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
Ming Lin1a353d82016-06-13 16:45:24 +02002231 .name = "pcie",
Sagi Grimberge439bb12016-02-10 10:03:29 -08002232 .module = THIS_MODULE,
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02002233 .flags = NVME_F_METADATA_SUPPORTED,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002234 .reg_read32 = nvme_pci_reg_read32,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01002235 .reg_write32 = nvme_pci_reg_write32,
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002236 .reg_read64 = nvme_pci_reg_read64,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002237 .free_ctrl = nvme_pci_free_ctrl,
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002238 .submit_async_event = nvme_pci_submit_async_event,
Christoph Hellwig1c63dc62015-11-26 10:06:56 +01002239};
Keith Busch4cc06522015-06-05 10:30:08 -06002240
Keith Buschb00a7262016-02-24 09:15:52 -07002241static int nvme_dev_map(struct nvme_dev *dev)
2242{
Keith Buschb00a7262016-02-24 09:15:52 -07002243 struct pci_dev *pdev = to_pci_dev(dev->dev);
2244
Johannes Thumshirna1f447b2016-06-07 09:44:02 +02002245 if (pci_request_mem_regions(pdev, "nvme"))
Keith Buschb00a7262016-02-24 09:15:52 -07002246 return -ENODEV;
2247
Xu Yu97f6ef62017-05-24 16:39:55 +08002248 if (nvme_remap_bar(dev, NVME_REG_DBS + 4096))
Keith Buschb00a7262016-02-24 09:15:52 -07002249 goto release;
2250
Max Gurtovoy9fa196e2016-12-19 16:18:24 +02002251 return 0;
Keith Buschb00a7262016-02-24 09:15:52 -07002252 release:
Max Gurtovoy9fa196e2016-12-19 16:18:24 +02002253 pci_release_mem_regions(pdev);
2254 return -ENODEV;
Keith Buschb00a7262016-02-24 09:15:52 -07002255}
2256
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002257static unsigned long check_dell_samsung_bug(struct pci_dev *pdev)
2258{
2259 if (pdev->vendor == 0x144d && pdev->device == 0xa802) {
2260 /*
2261 * Several Samsung devices seem to drop off the PCIe bus
2262 * randomly when APST is on and uses the deepest sleep state.
2263 * This has been observed on a Samsung "SM951 NVMe SAMSUNG
2264 * 256GB", a "PM951 NVMe SAMSUNG 512GB", and a "Samsung SSD
2265 * 950 PRO 256GB", but it seems to be restricted to two Dell
2266 * laptops.
2267 */
2268 if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.") &&
2269 (dmi_match(DMI_PRODUCT_NAME, "XPS 15 9550") ||
2270 dmi_match(DMI_PRODUCT_NAME, "Precision 5510")))
2271 return NVME_QUIRK_NO_DEEPEST_PS;
2272 }
2273
2274 return 0;
2275}
2276
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002277static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002278{
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002279 int node, result = -ENOMEM;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002280 struct nvme_dev *dev;
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002281 unsigned long quirks = id->driver_data;
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002282
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002283 node = dev_to_node(&pdev->dev);
2284 if (node == NUMA_NO_NODE)
Masayoshi Mizuma2fa84352016-06-20 09:33:17 +09002285 set_dev_node(&pdev->dev, first_memory_node);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002286
2287 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002288 if (!dev)
2289 return -ENOMEM;
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002290 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2291 GFP_KERNEL, node);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002292 if (!dev->queues)
2293 goto free;
2294
Christoph Hellwige75ec752015-05-22 11:12:39 +02002295 dev->dev = get_device(&pdev->dev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002296 pci_set_drvdata(pdev, dev);
Keith Buschb3fffde2015-02-03 11:21:42 -07002297
Keith Buschb00a7262016-02-24 09:15:52 -07002298 result = nvme_dev_map(dev);
2299 if (result)
Christophe JAILLETb00c9b72017-07-16 10:39:03 +02002300 goto put_pci;
Keith Buschb00a7262016-02-24 09:15:52 -07002301
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002302 INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work);
Christoph Hellwig5c8809e2015-11-26 12:35:49 +01002303 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
Keith Busch77bf25e2015-11-26 12:21:29 +01002304 mutex_init(&dev->shutdown_lock);
Keith Buschdb3cbff2016-01-12 14:41:17 -07002305 init_completion(&dev->ioq_wait);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002306
2307 result = nvme_setup_prp_pools(dev);
2308 if (result)
Christophe JAILLETb00c9b72017-07-16 10:39:03 +02002309 goto unmap;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002310
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002311 quirks |= check_dell_samsung_bug(pdev);
2312
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002313 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07002314 quirks);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002315 if (result)
2316 goto release_pools;
2317
Rakesh Pandit82b057c2017-06-05 14:43:11 +03002318 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING);
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002319 dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev));
2320
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002321 queue_work(nvme_wq, &dev->ctrl.reset_work);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002322 return 0;
2323
Keith Busch0877cb02013-07-15 15:02:19 -06002324 release_pools:
Matthew Wilcox091b6092011-02-10 09:56:01 -05002325 nvme_release_prp_pools(dev);
Christophe JAILLETb00c9b72017-07-16 10:39:03 +02002326 unmap:
2327 nvme_dev_unmap(dev);
Keith Buscha96d4f52014-08-19 19:15:59 -06002328 put_pci:
Christoph Hellwige75ec752015-05-22 11:12:39 +02002329 put_device(dev->dev);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002330 free:
2331 kfree(dev->queues);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002332 kfree(dev);
2333 return result;
2334}
2335
Christoph Hellwig775755e2017-06-01 13:10:38 +02002336static void nvme_reset_prepare(struct pci_dev *pdev)
Keith Buschf0d54a52014-05-02 10:40:43 -06002337{
Keith Buscha6739472014-06-23 16:03:21 -06002338 struct nvme_dev *dev = pci_get_drvdata(pdev);
Linus Torvaldsf263fbb2017-07-08 15:51:57 -07002339 nvme_dev_disable(dev, false);
Christoph Hellwig775755e2017-06-01 13:10:38 +02002340}
Keith Buschf0d54a52014-05-02 10:40:43 -06002341
Christoph Hellwig775755e2017-06-01 13:10:38 +02002342static void nvme_reset_done(struct pci_dev *pdev)
2343{
Linus Torvaldsf263fbb2017-07-08 15:51:57 -07002344 struct nvme_dev *dev = pci_get_drvdata(pdev);
2345 nvme_reset_ctrl(&dev->ctrl);
Keith Buschf0d54a52014-05-02 10:40:43 -06002346}
2347
Keith Busch09ece142014-01-27 11:29:40 -05002348static void nvme_shutdown(struct pci_dev *pdev)
2349{
2350 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Buscha5cdb682016-01-12 14:41:18 -07002351 nvme_dev_disable(dev, true);
Keith Busch09ece142014-01-27 11:29:40 -05002352}
2353
Keith Buschf58944e2016-02-24 09:15:55 -07002354/*
2355 * The driver's remove may be called on a device in a partially initialized
2356 * state. This function must not have any dependencies on the device state in
2357 * order to proceed.
2358 */
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002359static void nvme_remove(struct pci_dev *pdev)
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002360{
2361 struct nvme_dev *dev = pci_get_drvdata(pdev);
Keith Busch9a6b9452013-12-10 13:10:36 -07002362
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002363 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
2364
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002365 cancel_work_sync(&dev->ctrl.reset_work);
Keith Busch9a6b9452013-12-10 13:10:36 -07002366 pci_set_drvdata(pdev, NULL);
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002367
Keith Busch6db28ed2017-02-10 18:15:49 -05002368 if (!pci_device_is_present(pdev)) {
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002369 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD);
Keith Busch6db28ed2017-02-10 18:15:49 -05002370 nvme_dev_disable(dev, false);
2371 }
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002372
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002373 flush_work(&dev->ctrl.reset_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002374 nvme_stop_ctrl(&dev->ctrl);
2375 nvme_remove_namespaces(&dev->ctrl);
Keith Buscha5cdb682016-01-12 14:41:18 -07002376 nvme_dev_disable(dev, true);
Christoph Hellwig87ad72a2017-05-12 17:02:58 +02002377 nvme_free_host_mem(dev);
Matias Bjørlinga4aea562014-11-04 08:20:14 -07002378 nvme_dev_remove_admin(dev);
2379 nvme_free_queues(dev, 0);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002380 nvme_uninit_ctrl(&dev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002381 nvme_release_prp_pools(dev);
Keith Buschb00a7262016-02-24 09:15:52 -07002382 nvme_dev_unmap(dev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002383 nvme_put_ctrl(&dev->ctrl);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002384}
2385
Keith Busch13880f52016-06-20 09:41:06 -06002386static int nvme_pci_sriov_configure(struct pci_dev *pdev, int numvfs)
2387{
2388 int ret = 0;
2389
2390 if (numvfs == 0) {
2391 if (pci_vfs_assigned(pdev)) {
2392 dev_warn(&pdev->dev,
2393 "Cannot disable SR-IOV VFs while assigned\n");
2394 return -EPERM;
2395 }
2396 pci_disable_sriov(pdev);
2397 return 0;
2398 }
2399
2400 ret = pci_enable_sriov(pdev, numvfs);
2401 return ret ? ret : numvfs;
2402}
2403
Jingoo Han671a6012014-02-13 11:19:14 +09002404#ifdef CONFIG_PM_SLEEP
Keith Buschcd638942013-07-15 15:02:23 -06002405static int nvme_suspend(struct device *dev)
2406{
2407 struct pci_dev *pdev = to_pci_dev(dev);
2408 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2409
Keith Buscha5cdb682016-01-12 14:41:18 -07002410 nvme_dev_disable(ndev, true);
Keith Buschcd638942013-07-15 15:02:23 -06002411 return 0;
2412}
2413
2414static int nvme_resume(struct device *dev)
2415{
2416 struct pci_dev *pdev = to_pci_dev(dev);
2417 struct nvme_dev *ndev = pci_get_drvdata(pdev);
Keith Buschcd638942013-07-15 15:02:23 -06002418
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002419 nvme_reset_ctrl(&ndev->ctrl);
Keith Busch9a6b9452013-12-10 13:10:36 -07002420 return 0;
Keith Buschcd638942013-07-15 15:02:23 -06002421}
Jingoo Han671a6012014-02-13 11:19:14 +09002422#endif
Keith Buschcd638942013-07-15 15:02:23 -06002423
2424static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002425
Keith Buscha0a34082015-12-07 15:30:31 -07002426static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
2427 pci_channel_state_t state)
2428{
2429 struct nvme_dev *dev = pci_get_drvdata(pdev);
2430
2431 /*
2432 * A frozen channel requires a reset. When detected, this method will
2433 * shutdown the controller to quiesce. The controller will be restarted
2434 * after the slot reset through driver's slot_reset callback.
2435 */
Keith Buscha0a34082015-12-07 15:30:31 -07002436 switch (state) {
2437 case pci_channel_io_normal:
2438 return PCI_ERS_RESULT_CAN_RECOVER;
2439 case pci_channel_io_frozen:
Keith Buschd011fb32016-04-04 15:07:41 -06002440 dev_warn(dev->ctrl.device,
2441 "frozen state error detected, reset controller\n");
Keith Buscha5cdb682016-01-12 14:41:18 -07002442 nvme_dev_disable(dev, false);
Keith Buscha0a34082015-12-07 15:30:31 -07002443 return PCI_ERS_RESULT_NEED_RESET;
2444 case pci_channel_io_perm_failure:
Keith Buschd011fb32016-04-04 15:07:41 -06002445 dev_warn(dev->ctrl.device,
2446 "failure state error detected, request disconnect\n");
Keith Buscha0a34082015-12-07 15:30:31 -07002447 return PCI_ERS_RESULT_DISCONNECT;
2448 }
2449 return PCI_ERS_RESULT_NEED_RESET;
2450}
2451
2452static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev)
2453{
2454 struct nvme_dev *dev = pci_get_drvdata(pdev);
2455
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002456 dev_info(dev->ctrl.device, "restart after slot reset\n");
Keith Buscha0a34082015-12-07 15:30:31 -07002457 pci_restore_state(pdev);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002458 nvme_reset_ctrl(&dev->ctrl);
Keith Buscha0a34082015-12-07 15:30:31 -07002459 return PCI_ERS_RESULT_RECOVERED;
2460}
2461
2462static void nvme_error_resume(struct pci_dev *pdev)
2463{
2464 pci_cleanup_aer_uncorrect_error_status(pdev);
2465}
2466
Stephen Hemminger1d352032012-09-07 09:33:17 -07002467static const struct pci_error_handlers nvme_err_handler = {
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002468 .error_detected = nvme_error_detected,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002469 .slot_reset = nvme_slot_reset,
2470 .resume = nvme_error_resume,
Christoph Hellwig775755e2017-06-01 13:10:38 +02002471 .reset_prepare = nvme_reset_prepare,
2472 .reset_done = nvme_reset_done,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002473};
2474
Matthew Wilcox6eb0d692014-03-24 10:11:22 -04002475static const struct pci_device_id nvme_id_table[] = {
Christoph Hellwig106198e2015-11-26 10:07:41 +01002476 { PCI_VDEVICE(INTEL, 0x0953),
Keith Busch08095e72016-03-04 13:15:17 -07002477 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002478 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch99466e72016-05-02 15:14:24 -06002479 { PCI_VDEVICE(INTEL, 0x0a53),
2480 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002481 NVME_QUIRK_DEALLOCATE_ZEROES, },
Keith Busch99466e72016-05-02 15:14:24 -06002482 { PCI_VDEVICE(INTEL, 0x0a54),
2483 .driver_data = NVME_QUIRK_STRIPE_SIZE |
Christoph Hellwige850fd12017-04-05 19:21:13 +02002484 NVME_QUIRK_DEALLOCATE_ZEROES, },
David Wayne Fugatef99cb7af2017-07-10 12:39:59 -06002485 { PCI_VDEVICE(INTEL, 0x0a55),
2486 .driver_data = NVME_QUIRK_STRIPE_SIZE |
2487 NVME_QUIRK_DEALLOCATE_ZEROES, },
Andy Lutomirski50af47d2017-05-24 15:06:31 -07002488 { PCI_VDEVICE(INTEL, 0xf1a5), /* Intel 600P/P3100 */
2489 .driver_data = NVME_QUIRK_NO_DEEPEST_PS },
Keith Busch540c8012015-10-22 15:45:06 -06002490 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2491 .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03002492 { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */
2493 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Wenbo Wang015282c2016-09-08 12:12:11 -04002494 { PCI_DEVICE(0x1c5f, 0x0540), /* Memblaze Pblaze4 adapter */
2495 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Martin K. Petersend554b5e2017-06-27 22:27:57 -04002496 { PCI_DEVICE(0x144d, 0xa821), /* Samsung PM1725 */
2497 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
2498 { PCI_DEVICE(0x144d, 0xa822), /* Samsung PM1725a */
2499 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002500 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
Stephan Güntherc74dc782015-11-04 00:49:45 +01002501 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
Daniel Roschka124298b2017-02-22 15:17:29 -07002502 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) },
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002503 { 0, }
2504};
2505MODULE_DEVICE_TABLE(pci, nvme_id_table);
2506
2507static struct pci_driver nvme_driver = {
2508 .name = "nvme",
2509 .id_table = nvme_id_table,
2510 .probe = nvme_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -08002511 .remove = nvme_remove,
Keith Busch09ece142014-01-27 11:29:40 -05002512 .shutdown = nvme_shutdown,
Keith Buschcd638942013-07-15 15:02:23 -06002513 .driver = {
2514 .pm = &nvme_dev_pm_ops,
2515 },
Keith Busch13880f52016-06-20 09:41:06 -06002516 .sriov_configure = nvme_pci_sriov_configure,
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002517 .err_handler = &nvme_err_handler,
2518};
2519
2520static int __init nvme_init(void)
2521{
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002522 return pci_register_driver(&nvme_driver);
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002523}
2524
2525static void __exit nvme_exit(void)
2526{
2527 pci_unregister_driver(&nvme_driver);
Matthew Wilcox21bd78b2014-05-09 22:42:26 -04002528 _nvme_check_size();
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002529}
2530
2531MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2532MODULE_LICENSE("GPL");
Keith Buschc78b47132014-11-21 15:16:32 -07002533MODULE_VERSION("1.0");
Matthew Wilcoxb60503b2011-01-20 12:50:14 -05002534module_init(nvme_init);
2535module_exit(nvme_exit);