nvme: special case AEN requests
[linux-2.6-block.git] / drivers / nvme / host / pci.c
CommitLineData
b60503ba
MW
1/*
2 * NVM Express device driver
6eb0d698 3 * Copyright (c) 2011-2014, Intel Corporation.
b60503ba
MW
4 *
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.
b60503ba
MW
13 */
14
8de05535 15#include <linux/bitops.h>
b60503ba 16#include <linux/blkdev.h>
a4aea562 17#include <linux/blk-mq.h>
42f61420 18#include <linux/cpu.h>
fd63e9ce 19#include <linux/delay.h>
b60503ba
MW
20#include <linux/errno.h>
21#include <linux/fs.h>
22#include <linux/genhd.h>
4cc09e2d 23#include <linux/hdreg.h>
5aff9382 24#include <linux/idr.h>
b60503ba
MW
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/kdev_t.h>
1fa6aead 29#include <linux/kthread.h>
b60503ba
MW
30#include <linux/kernel.h>
31#include <linux/mm.h>
32#include <linux/module.h>
33#include <linux/moduleparam.h>
77bf25ea 34#include <linux/mutex.h>
b60503ba 35#include <linux/pci.h>
be7b6275 36#include <linux/poison.h>
c3bfe717 37#include <linux/ptrace.h>
b60503ba
MW
38#include <linux/sched.h>
39#include <linux/slab.h>
e1e5e564 40#include <linux/t10-pi.h>
b60503ba 41#include <linux/types.h>
2f8e2c87 42#include <linux/io-64-nonatomic-lo-hi.h>
1d277a63 43#include <asm/unaligned.h>
797a796a 44
f11bb3e2
CH
45#include "nvme.h"
46
9d43cf64 47#define NVME_Q_DEPTH 1024
d31af0a3 48#define NVME_AQ_DEPTH 256
b60503ba
MW
49#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
50#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
adf68f21
CH
51
52/*
53 * We handle AEN commands ourselves and don't even let the
54 * block layer know about them.
55 */
56#define NVME_NR_AEN_COMMANDS 1
57#define NVME_AQ_BLKMQ_DEPTH (NVME_AQ_DEPTH - NVME_NR_AEN_COMMANDS)
9d43cf64 58
21d34711 59unsigned char admin_timeout = 60;
9d43cf64
KB
60module_param(admin_timeout, byte, 0644);
61MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
b60503ba 62
bd67608a
MW
63unsigned char nvme_io_timeout = 30;
64module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
b355084a 65MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
b60503ba 66
5fd4ce1b 67unsigned char shutdown_timeout = 5;
2484f407
DM
68module_param(shutdown_timeout, byte, 0644);
69MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
70
58ffacb5
MW
71static int use_threaded_interrupts;
72module_param(use_threaded_interrupts, int, 0);
73
8ffaadf7
JD
74static bool use_cmb_sqes = true;
75module_param(use_cmb_sqes, bool, 0644);
76MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
77
1fa6aead
MW
78static LIST_HEAD(dev_list);
79static struct task_struct *nvme_thread;
9a6b9458 80static struct workqueue_struct *nvme_workq;
b9afca3e 81static wait_queue_head_t nvme_kthread_wait;
1fa6aead 82
1c63dc66
CH
83struct nvme_dev;
84struct nvme_queue;
d4f6c3ab 85struct nvme_iod;
1c63dc66 86
4cc06521 87static int nvme_reset(struct nvme_dev *dev);
a0fa9647 88static void nvme_process_cq(struct nvme_queue *nvmeq);
d4f6c3ab 89static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod);
5c8809e6 90static void nvme_remove_dead_ctrl(struct nvme_dev *dev);
e1569a16 91static void nvme_dev_shutdown(struct nvme_dev *dev);
d4b4ff8e 92
4d115420
KB
93struct async_cmd_info {
94 struct kthread_work work;
95 struct kthread_worker *worker;
4d115420
KB
96 int status;
97 void *ctx;
98};
1fa6aead 99
1c63dc66
CH
100/*
101 * Represents an NVM Express device. Each nvme_dev is a PCI function.
102 */
103struct nvme_dev {
104 struct list_head node;
105 struct nvme_queue **queues;
106 struct blk_mq_tag_set tagset;
107 struct blk_mq_tag_set admin_tagset;
108 u32 __iomem *dbs;
109 struct device *dev;
110 struct dma_pool *prp_page_pool;
111 struct dma_pool *prp_small_pool;
112 unsigned queue_count;
113 unsigned online_queues;
114 unsigned max_qid;
115 int q_depth;
116 u32 db_stride;
1c63dc66
CH
117 struct msix_entry *entry;
118 void __iomem *bar;
1c63dc66 119 struct work_struct reset_work;
1c63dc66 120 struct work_struct scan_work;
5c8809e6 121 struct work_struct remove_work;
77bf25ea 122 struct mutex shutdown_lock;
1c63dc66 123 bool subsystem;
1c63dc66
CH
124 void __iomem *cmb;
125 dma_addr_t cmb_dma_addr;
126 u64 cmb_size;
127 u32 cmbsz;
fd634f41
CH
128 unsigned long flags;
129#define NVME_CTRL_RESETTING 0
1c63dc66
CH
130
131 struct nvme_ctrl ctrl;
132};
133
134static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
135{
136 return container_of(ctrl, struct nvme_dev, ctrl);
137}
138
b60503ba
MW
139/*
140 * An NVM Express queue. Each device has at least two (one for admin
141 * commands and one for I/O commands).
142 */
143struct nvme_queue {
144 struct device *q_dmadev;
091b6092 145 struct nvme_dev *dev;
3193f07b 146 char irqname[24]; /* nvme4294967295-65535\0 */
b60503ba
MW
147 spinlock_t q_lock;
148 struct nvme_command *sq_cmds;
8ffaadf7 149 struct nvme_command __iomem *sq_cmds_io;
b60503ba 150 volatile struct nvme_completion *cqes;
42483228 151 struct blk_mq_tags **tags;
b60503ba
MW
152 dma_addr_t sq_dma_addr;
153 dma_addr_t cq_dma_addr;
b60503ba
MW
154 u32 __iomem *q_db;
155 u16 q_depth;
6222d172 156 s16 cq_vector;
b60503ba
MW
157 u16 sq_head;
158 u16 sq_tail;
159 u16 cq_head;
c30341dc 160 u16 qid;
e9539f47
MW
161 u8 cq_phase;
162 u8 cqe_seen;
4d115420 163 struct async_cmd_info cmdinfo;
b60503ba
MW
164};
165
71bd150c
CH
166/*
167 * The nvme_iod describes the data in an I/O, including the list of PRP
168 * entries. You can't see it in this data structure because C doesn't let
169 * me express that. Use nvme_alloc_iod to ensure there's enough space
170 * allocated to store the PRP list.
171 */
172struct nvme_iod {
173 unsigned long private; /* For the use of the submitter of the I/O */
174 int npages; /* In the PRP list. 0 means small pool in use */
175 int offset; /* Of PRP list */
176 int nents; /* Used in scatterlist */
177 int length; /* Of data, in bytes */
178 dma_addr_t first_dma;
179 struct scatterlist meta_sg[1]; /* metadata requires single contiguous buffer */
180 struct scatterlist sg[0];
181};
182
b60503ba
MW
183/*
184 * Check we didin't inadvertently grow the command struct
185 */
186static inline void _nvme_check_size(void)
187{
188 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
189 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
190 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
191 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
192 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
f8ebf840 193 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
c30341dc 194 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
b60503ba
MW
195 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
196 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
197 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
198 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
6ecec745 199 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
b60503ba
MW
200}
201
edd10d33 202typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
c2f5b650
MW
203 struct nvme_completion *);
204
e85248e5 205struct nvme_cmd_info {
c2f5b650
MW
206 nvme_completion_fn fn;
207 void *ctx;
c30341dc 208 int aborted;
a4aea562 209 struct nvme_queue *nvmeq;
ac3dd5bd 210 struct nvme_iod iod[0];
e85248e5
MW
211};
212
ac3dd5bd
JA
213/*
214 * Max size of iod being embedded in the request payload
215 */
216#define NVME_INT_PAGES 2
5fd4ce1b 217#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
fda631ff 218#define NVME_INT_MASK 0x01
ac3dd5bd
JA
219
220/*
221 * Will slightly overestimate the number of pages needed. This is OK
222 * as it only leads to a small amount of wasted memory for the lifetime of
223 * the I/O.
224 */
225static int nvme_npages(unsigned size, struct nvme_dev *dev)
226{
5fd4ce1b
CH
227 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
228 dev->ctrl.page_size);
ac3dd5bd
JA
229 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
230}
231
232static unsigned int nvme_cmd_size(struct nvme_dev *dev)
233{
234 unsigned int ret = sizeof(struct nvme_cmd_info);
235
236 ret += sizeof(struct nvme_iod);
237 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
238 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
239
240 return ret;
241}
242
a4aea562
MB
243static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
244 unsigned int hctx_idx)
e85248e5 245{
a4aea562
MB
246 struct nvme_dev *dev = data;
247 struct nvme_queue *nvmeq = dev->queues[0];
248
42483228
KB
249 WARN_ON(hctx_idx != 0);
250 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
251 WARN_ON(nvmeq->tags);
252
a4aea562 253 hctx->driver_data = nvmeq;
42483228 254 nvmeq->tags = &dev->admin_tagset.tags[0];
a4aea562 255 return 0;
e85248e5
MW
256}
257
4af0e21c
KB
258static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
259{
260 struct nvme_queue *nvmeq = hctx->driver_data;
261
262 nvmeq->tags = NULL;
263}
264
a4aea562
MB
265static int nvme_admin_init_request(void *data, struct request *req,
266 unsigned int hctx_idx, unsigned int rq_idx,
267 unsigned int numa_node)
22404274 268{
a4aea562
MB
269 struct nvme_dev *dev = data;
270 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
271 struct nvme_queue *nvmeq = dev->queues[0];
272
273 BUG_ON(!nvmeq);
274 cmd->nvmeq = nvmeq;
275 return 0;
22404274
KB
276}
277
a4aea562
MB
278static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
279 unsigned int hctx_idx)
b60503ba 280{
a4aea562 281 struct nvme_dev *dev = data;
42483228 282 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
a4aea562 283
42483228
KB
284 if (!nvmeq->tags)
285 nvmeq->tags = &dev->tagset.tags[hctx_idx];
b60503ba 286
42483228 287 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
a4aea562
MB
288 hctx->driver_data = nvmeq;
289 return 0;
b60503ba
MW
290}
291
a4aea562
MB
292static int nvme_init_request(void *data, struct request *req,
293 unsigned int hctx_idx, unsigned int rq_idx,
294 unsigned int numa_node)
b60503ba 295{
a4aea562
MB
296 struct nvme_dev *dev = data;
297 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
298 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
299
300 BUG_ON(!nvmeq);
301 cmd->nvmeq = nvmeq;
302 return 0;
303}
304
305static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
306 nvme_completion_fn handler)
307{
308 cmd->fn = handler;
309 cmd->ctx = ctx;
310 cmd->aborted = 0;
c917dfe5 311 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
b60503ba
MW
312}
313
ac3dd5bd
JA
314static void *iod_get_private(struct nvme_iod *iod)
315{
316 return (void *) (iod->private & ~0x1UL);
317}
318
319/*
320 * If bit 0 is set, the iod is embedded in the request payload.
321 */
322static bool iod_should_kfree(struct nvme_iod *iod)
323{
fda631ff 324 return (iod->private & NVME_INT_MASK) == 0;
ac3dd5bd
JA
325}
326
c2f5b650
MW
327/* Special values must be less than 0x1000 */
328#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
d2d87034
MW
329#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
330#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
331#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
be7b6275 332
edd10d33 333static void special_completion(struct nvme_queue *nvmeq, void *ctx,
c2f5b650
MW
334 struct nvme_completion *cqe)
335{
336 if (ctx == CMD_CTX_CANCELLED)
337 return;
c2f5b650 338 if (ctx == CMD_CTX_COMPLETED) {
edd10d33 339 dev_warn(nvmeq->q_dmadev,
c2f5b650
MW
340 "completed id %d twice on queue %d\n",
341 cqe->command_id, le16_to_cpup(&cqe->sq_id));
342 return;
343 }
344 if (ctx == CMD_CTX_INVALID) {
edd10d33 345 dev_warn(nvmeq->q_dmadev,
c2f5b650
MW
346 "invalid id %d completed on queue %d\n",
347 cqe->command_id, le16_to_cpup(&cqe->sq_id));
348 return;
349 }
edd10d33 350 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
c2f5b650
MW
351}
352
a4aea562 353static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
b60503ba 354{
c2f5b650 355 void *ctx;
b60503ba 356
859361a2 357 if (fn)
a4aea562
MB
358 *fn = cmd->fn;
359 ctx = cmd->ctx;
360 cmd->fn = special_completion;
361 cmd->ctx = CMD_CTX_CANCELLED;
c2f5b650 362 return ctx;
b60503ba
MW
363}
364
adf68f21
CH
365static void nvme_complete_async_event(struct nvme_dev *dev,
366 struct nvme_completion *cqe)
3c0cf138 367{
adf68f21
CH
368 u16 status = le16_to_cpu(cqe->status) >> 1;
369 u32 result = le32_to_cpu(cqe->result);
a4aea562
MB
370
371 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
adf68f21 372 ++dev->ctrl.event_limit;
a5768aa8
KB
373 if (status != NVME_SC_SUCCESS)
374 return;
375
376 switch (result & 0xff07) {
377 case NVME_AER_NOTICE_NS_CHANGED:
adf68f21
CH
378 dev_info(dev->dev, "rescanning\n");
379 queue_work(nvme_workq, &dev->scan_work);
a5768aa8 380 default:
adf68f21 381 dev_warn(dev->dev, "async event result %08x\n", result);
a5768aa8 382 }
b60503ba
MW
383}
384
a4aea562
MB
385static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
386 unsigned int tag)
b60503ba 387{
42483228 388 struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag);
a51afb54 389
a4aea562 390 return blk_mq_rq_to_pdu(req);
4f5099af
KB
391}
392
a4aea562
MB
393/*
394 * Called with local interrupts disabled and the q_lock held. May not sleep.
395 */
396static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
397 nvme_completion_fn *fn)
4f5099af 398{
a4aea562
MB
399 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
400 void *ctx;
401 if (tag >= nvmeq->q_depth) {
402 *fn = special_completion;
403 return CMD_CTX_INVALID;
404 }
405 if (fn)
406 *fn = cmd->fn;
407 ctx = cmd->ctx;
408 cmd->fn = special_completion;
409 cmd->ctx = CMD_CTX_COMPLETED;
410 return ctx;
b60503ba
MW
411}
412
413/**
adf68f21 414 * __nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
b60503ba
MW
415 * @nvmeq: The queue to use
416 * @cmd: The command to send
417 *
418 * Safe to use from interrupt context
419 */
e3f879bf
SB
420static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
421 struct nvme_command *cmd)
b60503ba 422{
a4aea562
MB
423 u16 tail = nvmeq->sq_tail;
424
8ffaadf7
JD
425 if (nvmeq->sq_cmds_io)
426 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
427 else
428 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
429
b60503ba
MW
430 if (++tail == nvmeq->q_depth)
431 tail = 0;
7547881d 432 writel(tail, nvmeq->q_db);
b60503ba 433 nvmeq->sq_tail = tail;
b60503ba
MW
434}
435
eca18b23 436static __le64 **iod_list(struct nvme_iod *iod)
e025344c 437{
eca18b23 438 return ((void *)iod) + iod->offset;
e025344c
SMM
439}
440
ac3dd5bd
JA
441static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
442 unsigned nseg, unsigned long private)
eca18b23 443{
ac3dd5bd
JA
444 iod->private = private;
445 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
446 iod->npages = -1;
447 iod->length = nbytes;
448 iod->nents = 0;
eca18b23 449}
b60503ba 450
eca18b23 451static struct nvme_iod *
ac3dd5bd
JA
452__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
453 unsigned long priv, gfp_t gfp)
b60503ba 454{
eca18b23 455 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
ac3dd5bd 456 sizeof(__le64 *) * nvme_npages(bytes, dev) +
eca18b23
MW
457 sizeof(struct scatterlist) * nseg, gfp);
458
ac3dd5bd
JA
459 if (iod)
460 iod_init(iod, bytes, nseg, priv);
eca18b23
MW
461
462 return iod;
b60503ba
MW
463}
464
ac3dd5bd
JA
465static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
466 gfp_t gfp)
467{
468 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
469 sizeof(struct nvme_dsm_range);
ac3dd5bd
JA
470 struct nvme_iod *iod;
471
472 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
473 size <= NVME_INT_BYTES(dev)) {
474 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
475
476 iod = cmd->iod;
ac3dd5bd 477 iod_init(iod, size, rq->nr_phys_segments,
fda631ff 478 (unsigned long) rq | NVME_INT_MASK);
ac3dd5bd
JA
479 return iod;
480 }
481
482 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
483 (unsigned long) rq, gfp);
484}
485
d29ec824 486static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
b60503ba 487{
5fd4ce1b 488 const int last_prp = dev->ctrl.page_size / 8 - 1;
eca18b23
MW
489 int i;
490 __le64 **list = iod_list(iod);
491 dma_addr_t prp_dma = iod->first_dma;
492
493 if (iod->npages == 0)
494 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
495 for (i = 0; i < iod->npages; i++) {
496 __le64 *prp_list = list[i];
497 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
498 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
499 prp_dma = next_prp_dma;
500 }
ac3dd5bd
JA
501
502 if (iod_should_kfree(iod))
503 kfree(iod);
b60503ba
MW
504}
505
52b68d7e 506#ifdef CONFIG_BLK_DEV_INTEGRITY
e1e5e564
KB
507static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
508{
509 if (be32_to_cpu(pi->ref_tag) == v)
510 pi->ref_tag = cpu_to_be32(p);
511}
512
513static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
514{
515 if (be32_to_cpu(pi->ref_tag) == p)
516 pi->ref_tag = cpu_to_be32(v);
517}
518
519/**
520 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
521 *
522 * The virtual start sector is the one that was originally submitted by the
523 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
524 * start sector may be different. Remap protection information to match the
525 * physical LBA on writes, and back to the original seed on reads.
526 *
527 * Type 0 and 3 do not have a ref tag, so no remapping required.
528 */
529static void nvme_dif_remap(struct request *req,
530 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
531{
532 struct nvme_ns *ns = req->rq_disk->private_data;
533 struct bio_integrity_payload *bip;
534 struct t10_pi_tuple *pi;
535 void *p, *pmap;
536 u32 i, nlb, ts, phys, virt;
537
538 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
539 return;
540
541 bip = bio_integrity(req->bio);
542 if (!bip)
543 return;
544
545 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
e1e5e564
KB
546
547 p = pmap;
548 virt = bip_get_seed(bip);
549 phys = nvme_block_nr(ns, blk_rq_pos(req));
550 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
ac6fc48c 551 ts = ns->disk->queue->integrity.tuple_size;
e1e5e564
KB
552
553 for (i = 0; i < nlb; i++, virt++, phys++) {
554 pi = (struct t10_pi_tuple *)p;
555 dif_swap(phys, virt, pi);
556 p += ts;
557 }
558 kunmap_atomic(pmap);
559}
52b68d7e
KB
560#else /* CONFIG_BLK_DEV_INTEGRITY */
561static void nvme_dif_remap(struct request *req,
562 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
563{
564}
565static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
566{
567}
568static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
569{
570}
52b68d7e
KB
571#endif
572
a4aea562 573static void req_completion(struct nvme_queue *nvmeq, void *ctx,
b60503ba
MW
574 struct nvme_completion *cqe)
575{
eca18b23 576 struct nvme_iod *iod = ctx;
ac3dd5bd 577 struct request *req = iod_get_private(iod);
a4aea562 578 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
b60503ba 579 u16 status = le16_to_cpup(&cqe->status) >> 1;
81c04b94 580 int error = 0;
b60503ba 581
edd10d33 582 if (unlikely(status)) {
7688faa6 583 if (nvme_req_needs_retry(req, status)) {
d4f6c3ab 584 nvme_unmap_data(nvmeq->dev, iod);
7688faa6 585 nvme_requeue_req(req);
d4f6c3ab 586 return;
edd10d33 587 }
f4829a9b 588
d29ec824 589 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
17188bb4 590 if (cmd_rq->ctx == CMD_CTX_CANCELLED)
297465c8 591 error = NVME_SC_CANCELLED;
81c04b94
CH
592 else
593 error = status;
d29ec824 594 } else {
81c04b94 595 error = nvme_error_status(status);
d29ec824 596 }
f4829a9b
CH
597 }
598
a0a931d6
KB
599 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
600 u32 result = le32_to_cpup(&cqe->result);
601 req->special = (void *)(uintptr_t)result;
602 }
a4aea562
MB
603
604 if (cmd_rq->aborted)
e75ec752 605 dev_warn(nvmeq->dev->dev,
a4aea562 606 "completing aborted command with status:%04x\n",
81c04b94 607 error);
a4aea562 608
d4f6c3ab
CH
609 nvme_unmap_data(nvmeq->dev, iod);
610 blk_mq_complete_request(req, error);
b60503ba
MW
611}
612
69d2b571
CH
613static bool nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
614 int total_len)
ff22b54f 615{
99802a7a 616 struct dma_pool *pool;
eca18b23
MW
617 int length = total_len;
618 struct scatterlist *sg = iod->sg;
ff22b54f
MW
619 int dma_len = sg_dma_len(sg);
620 u64 dma_addr = sg_dma_address(sg);
5fd4ce1b 621 u32 page_size = dev->ctrl.page_size;
f137e0f1 622 int offset = dma_addr & (page_size - 1);
e025344c 623 __le64 *prp_list;
eca18b23 624 __le64 **list = iod_list(iod);
e025344c 625 dma_addr_t prp_dma;
eca18b23 626 int nprps, i;
ff22b54f 627
1d090624 628 length -= (page_size - offset);
ff22b54f 629 if (length <= 0)
69d2b571 630 return true;
ff22b54f 631
1d090624 632 dma_len -= (page_size - offset);
ff22b54f 633 if (dma_len) {
1d090624 634 dma_addr += (page_size - offset);
ff22b54f
MW
635 } else {
636 sg = sg_next(sg);
637 dma_addr = sg_dma_address(sg);
638 dma_len = sg_dma_len(sg);
639 }
640
1d090624 641 if (length <= page_size) {
edd10d33 642 iod->first_dma = dma_addr;
69d2b571 643 return true;
e025344c
SMM
644 }
645
1d090624 646 nprps = DIV_ROUND_UP(length, page_size);
99802a7a
MW
647 if (nprps <= (256 / 8)) {
648 pool = dev->prp_small_pool;
eca18b23 649 iod->npages = 0;
99802a7a
MW
650 } else {
651 pool = dev->prp_page_pool;
eca18b23 652 iod->npages = 1;
99802a7a
MW
653 }
654
69d2b571 655 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
b77954cb 656 if (!prp_list) {
edd10d33 657 iod->first_dma = dma_addr;
eca18b23 658 iod->npages = -1;
69d2b571 659 return false;
b77954cb 660 }
eca18b23
MW
661 list[0] = prp_list;
662 iod->first_dma = prp_dma;
e025344c
SMM
663 i = 0;
664 for (;;) {
1d090624 665 if (i == page_size >> 3) {
e025344c 666 __le64 *old_prp_list = prp_list;
69d2b571 667 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
eca18b23 668 if (!prp_list)
69d2b571 669 return false;
eca18b23 670 list[iod->npages++] = prp_list;
7523d834
MW
671 prp_list[0] = old_prp_list[i - 1];
672 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
673 i = 1;
e025344c
SMM
674 }
675 prp_list[i++] = cpu_to_le64(dma_addr);
1d090624
KB
676 dma_len -= page_size;
677 dma_addr += page_size;
678 length -= page_size;
e025344c
SMM
679 if (length <= 0)
680 break;
681 if (dma_len > 0)
682 continue;
683 BUG_ON(dma_len < 0);
684 sg = sg_next(sg);
685 dma_addr = sg_dma_address(sg);
686 dma_len = sg_dma_len(sg);
ff22b54f
MW
687 }
688
69d2b571 689 return true;
ff22b54f
MW
690}
691
ba1ca37e
CH
692static int nvme_map_data(struct nvme_dev *dev, struct nvme_iod *iod,
693 struct nvme_command *cmnd)
d29ec824 694{
ba1ca37e
CH
695 struct request *req = iod_get_private(iod);
696 struct request_queue *q = req->q;
697 enum dma_data_direction dma_dir = rq_data_dir(req) ?
698 DMA_TO_DEVICE : DMA_FROM_DEVICE;
699 int ret = BLK_MQ_RQ_QUEUE_ERROR;
700
701 sg_init_table(iod->sg, req->nr_phys_segments);
702 iod->nents = blk_rq_map_sg(q, req, iod->sg);
703 if (!iod->nents)
704 goto out;
705
706 ret = BLK_MQ_RQ_QUEUE_BUSY;
707 if (!dma_map_sg(dev->dev, iod->sg, iod->nents, dma_dir))
708 goto out;
709
710 if (!nvme_setup_prps(dev, iod, blk_rq_bytes(req)))
711 goto out_unmap;
712
713 ret = BLK_MQ_RQ_QUEUE_ERROR;
714 if (blk_integrity_rq(req)) {
715 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
716 goto out_unmap;
717
718 sg_init_table(iod->meta_sg, 1);
719 if (blk_rq_map_integrity_sg(q, req->bio, iod->meta_sg) != 1)
720 goto out_unmap;
d29ec824 721
ba1ca37e
CH
722 if (rq_data_dir(req))
723 nvme_dif_remap(req, nvme_dif_prep);
724
725 if (!dma_map_sg(dev->dev, iod->meta_sg, 1, dma_dir))
726 goto out_unmap;
d29ec824
CH
727 }
728
ba1ca37e
CH
729 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
730 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
731 if (blk_integrity_rq(req))
732 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg));
733 return BLK_MQ_RQ_QUEUE_OK;
734
735out_unmap:
736 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
737out:
738 return ret;
d29ec824
CH
739}
740
d4f6c3ab
CH
741static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod)
742{
743 struct request *req = iod_get_private(iod);
744 enum dma_data_direction dma_dir = rq_data_dir(req) ?
745 DMA_TO_DEVICE : DMA_FROM_DEVICE;
746
747 if (iod->nents) {
748 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
749 if (blk_integrity_rq(req)) {
750 if (!rq_data_dir(req))
751 nvme_dif_remap(req, nvme_dif_complete);
752 dma_unmap_sg(dev->dev, iod->meta_sg, 1, dma_dir);
753 }
754 }
755
756 nvme_free_iod(dev, iod);
757}
758
a4aea562
MB
759/*
760 * We reuse the small pool to allocate the 16-byte range here as it is not
761 * worth having a special pool for these or additional cases to handle freeing
762 * the iod.
763 */
ba1ca37e
CH
764static int nvme_setup_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
765 struct nvme_iod *iod, struct nvme_command *cmnd)
0e5e4f0e 766{
ba1ca37e
CH
767 struct request *req = iod_get_private(iod);
768 struct nvme_dsm_range *range;
769
770 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
771 &iod->first_dma);
772 if (!range)
773 return BLK_MQ_RQ_QUEUE_BUSY;
774 iod_list(iod)[0] = (__le64 *)range;
775 iod->npages = 0;
0e5e4f0e 776
0e5e4f0e 777 range->cattr = cpu_to_le32(0);
a4aea562
MB
778 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
779 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
0e5e4f0e 780
ba1ca37e
CH
781 memset(cmnd, 0, sizeof(*cmnd));
782 cmnd->dsm.opcode = nvme_cmd_dsm;
783 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
784 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
785 cmnd->dsm.nr = 0;
786 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
787 return BLK_MQ_RQ_QUEUE_OK;
0e5e4f0e
KB
788}
789
d29ec824
CH
790/*
791 * NOTE: ns is NULL when called on the admin queue.
792 */
a4aea562
MB
793static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
794 const struct blk_mq_queue_data *bd)
edd10d33 795{
a4aea562
MB
796 struct nvme_ns *ns = hctx->queue->queuedata;
797 struct nvme_queue *nvmeq = hctx->driver_data;
d29ec824 798 struct nvme_dev *dev = nvmeq->dev;
a4aea562
MB
799 struct request *req = bd->rq;
800 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
edd10d33 801 struct nvme_iod *iod;
ba1ca37e
CH
802 struct nvme_command cmnd;
803 int ret = BLK_MQ_RQ_QUEUE_OK;
edd10d33 804
e1e5e564
KB
805 /*
806 * If formated with metadata, require the block layer provide a buffer
807 * unless this namespace is formated such that the metadata can be
808 * stripped/generated by the controller with PRACT=1.
809 */
d29ec824 810 if (ns && ns->ms && !blk_integrity_rq(req)) {
71feb364
KB
811 if (!(ns->pi_type && ns->ms == 8) &&
812 req->cmd_type != REQ_TYPE_DRV_PRIV) {
f4829a9b 813 blk_mq_complete_request(req, -EFAULT);
e1e5e564
KB
814 return BLK_MQ_RQ_QUEUE_OK;
815 }
816 }
817
d29ec824 818 iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
edd10d33 819 if (!iod)
fe54303e 820 return BLK_MQ_RQ_QUEUE_BUSY;
a4aea562 821
a4aea562 822 if (req->cmd_flags & REQ_DISCARD) {
ba1ca37e
CH
823 ret = nvme_setup_discard(nvmeq, ns, iod, &cmnd);
824 } else {
825 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
826 memcpy(&cmnd, req->cmd, sizeof(cmnd));
827 else if (req->cmd_flags & REQ_FLUSH)
828 nvme_setup_flush(ns, &cmnd);
829 else
830 nvme_setup_rw(ns, req, &cmnd);
a4aea562 831
ba1ca37e
CH
832 if (req->nr_phys_segments)
833 ret = nvme_map_data(dev, iod, &cmnd);
edd10d33 834 }
1974b1ae 835
ba1ca37e
CH
836 if (ret)
837 goto out;
838
839 cmnd.common.command_id = req->tag;
9af8785a 840 nvme_set_info(cmd, iod, req_completion);
a4aea562 841
ba1ca37e
CH
842 spin_lock_irq(&nvmeq->q_lock);
843 __nvme_submit_cmd(nvmeq, &cmnd);
a4aea562
MB
844 nvme_process_cq(nvmeq);
845 spin_unlock_irq(&nvmeq->q_lock);
846 return BLK_MQ_RQ_QUEUE_OK;
ba1ca37e 847out:
d29ec824 848 nvme_free_iod(dev, iod);
ba1ca37e 849 return ret;
b60503ba
MW
850}
851
a0fa9647 852static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
b60503ba 853{
82123460 854 u16 head, phase;
b60503ba 855
b60503ba 856 head = nvmeq->cq_head;
82123460 857 phase = nvmeq->cq_phase;
b60503ba
MW
858
859 for (;;) {
c2f5b650
MW
860 void *ctx;
861 nvme_completion_fn fn;
b60503ba 862 struct nvme_completion cqe = nvmeq->cqes[head];
adf68f21
CH
863 u16 status = le16_to_cpu(cqe.status);
864
865 if ((status & 1) != phase)
b60503ba
MW
866 break;
867 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
868 if (++head == nvmeq->q_depth) {
869 head = 0;
82123460 870 phase = !phase;
b60503ba 871 }
adf68f21 872
a0fa9647
JA
873 if (tag && *tag == cqe.command_id)
874 *tag = -1;
adf68f21
CH
875
876 /*
877 * AEN requests are special as they don't time out and can
878 * survive any kind of queue freeze and often don't respond to
879 * aborts. We don't even bother to allocate a struct request
880 * for them but rather special case them here.
881 */
882 if (unlikely(nvmeq->qid == 0 &&
883 cqe.command_id >= NVME_AQ_BLKMQ_DEPTH)) {
884 nvme_complete_async_event(nvmeq->dev, &cqe);
885 continue;
886 }
887
a4aea562 888 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
edd10d33 889 fn(nvmeq, ctx, &cqe);
b60503ba
MW
890 }
891
892 /* If the controller ignores the cq head doorbell and continuously
893 * writes to the queue, it is theoretically possible to wrap around
894 * the queue twice and mistakenly return IRQ_NONE. Linux only
895 * requires that 0.1% of your interrupts are handled, so this isn't
896 * a big problem.
897 */
82123460 898 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
a0fa9647 899 return;
b60503ba 900
604e8c8d
KB
901 if (likely(nvmeq->cq_vector >= 0))
902 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
b60503ba 903 nvmeq->cq_head = head;
82123460 904 nvmeq->cq_phase = phase;
b60503ba 905
e9539f47 906 nvmeq->cqe_seen = 1;
a0fa9647
JA
907}
908
909static void nvme_process_cq(struct nvme_queue *nvmeq)
910{
911 __nvme_process_cq(nvmeq, NULL);
b60503ba
MW
912}
913
914static irqreturn_t nvme_irq(int irq, void *data)
58ffacb5
MW
915{
916 irqreturn_t result;
917 struct nvme_queue *nvmeq = data;
918 spin_lock(&nvmeq->q_lock);
e9539f47
MW
919 nvme_process_cq(nvmeq);
920 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
921 nvmeq->cqe_seen = 0;
58ffacb5
MW
922 spin_unlock(&nvmeq->q_lock);
923 return result;
924}
925
926static irqreturn_t nvme_irq_check(int irq, void *data)
927{
928 struct nvme_queue *nvmeq = data;
929 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
930 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
931 return IRQ_NONE;
932 return IRQ_WAKE_THREAD;
933}
934
a0fa9647
JA
935static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
936{
937 struct nvme_queue *nvmeq = hctx->driver_data;
938
939 if ((le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
940 nvmeq->cq_phase) {
941 spin_lock_irq(&nvmeq->q_lock);
942 __nvme_process_cq(nvmeq, &tag);
943 spin_unlock_irq(&nvmeq->q_lock);
944
945 if (tag == -1)
946 return 1;
947 }
948
949 return 0;
950}
951
adf68f21 952static void nvme_submit_async_event(struct nvme_dev *dev)
a4aea562 953{
a4aea562 954 struct nvme_command c;
a4aea562
MB
955
956 memset(&c, 0, sizeof(c));
957 c.common.opcode = nvme_admin_async_event;
adf68f21 958 c.common.command_id = NVME_AQ_BLKMQ_DEPTH + --dev->ctrl.event_limit;
a4aea562 959
adf68f21 960 __nvme_submit_cmd(dev->queues[0], &c);
a4aea562
MB
961}
962
d8f32166 963static void async_cmd_info_endio(struct request *req, int error)
4d115420 964{
d8f32166 965 struct async_cmd_info *cmdinfo = req->end_io_data;
a4aea562 966
d8f32166
CH
967 cmdinfo->status = req->errors;
968 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
969 blk_mq_free_request(req);
4d115420
KB
970}
971
b60503ba
MW
972static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
973{
b60503ba
MW
974 struct nvme_command c;
975
976 memset(&c, 0, sizeof(c));
977 c.delete_queue.opcode = opcode;
978 c.delete_queue.qid = cpu_to_le16(id);
979
1c63dc66 980 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
b60503ba
MW
981}
982
983static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
984 struct nvme_queue *nvmeq)
985{
b60503ba
MW
986 struct nvme_command c;
987 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
988
d29ec824
CH
989 /*
990 * Note: we (ab)use the fact the the prp fields survive if no data
991 * is attached to the request.
992 */
b60503ba
MW
993 memset(&c, 0, sizeof(c));
994 c.create_cq.opcode = nvme_admin_create_cq;
995 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
996 c.create_cq.cqid = cpu_to_le16(qid);
997 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
998 c.create_cq.cq_flags = cpu_to_le16(flags);
999 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1000
1c63dc66 1001 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
b60503ba
MW
1002}
1003
1004static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1005 struct nvme_queue *nvmeq)
1006{
b60503ba
MW
1007 struct nvme_command c;
1008 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1009
d29ec824
CH
1010 /*
1011 * Note: we (ab)use the fact the the prp fields survive if no data
1012 * is attached to the request.
1013 */
b60503ba
MW
1014 memset(&c, 0, sizeof(c));
1015 c.create_sq.opcode = nvme_admin_create_sq;
1016 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1017 c.create_sq.sqid = cpu_to_le16(qid);
1018 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1019 c.create_sq.sq_flags = cpu_to_le16(flags);
1020 c.create_sq.cqid = cpu_to_le16(qid);
1021
1c63dc66 1022 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
b60503ba
MW
1023}
1024
1025static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1026{
1027 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1028}
1029
1030static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1031{
1032 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1033}
1034
e7a2a87d
CH
1035static void abort_endio(struct request *req, int error)
1036{
1037 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
1038 struct nvme_queue *nvmeq = cmd->nvmeq;
1039 u32 result = (u32)(uintptr_t)req->special;
1040 u16 status = req->errors;
1041
1042 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
1043 atomic_inc(&nvmeq->dev->ctrl.abort_limit);
1044
1045 blk_mq_free_request(req);
1046}
1047
31c7c7d2 1048static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
c30341dc 1049{
a4aea562
MB
1050 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1051 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
c30341dc 1052 struct nvme_dev *dev = nvmeq->dev;
a4aea562 1053 struct request *abort_req;
a4aea562 1054 struct nvme_command cmd;
c30341dc 1055
31c7c7d2 1056 /*
fd634f41
CH
1057 * 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 */
1062 if (test_bit(NVME_CTRL_RESETTING, &dev->flags)) {
1063 dev_warn(dev->dev,
1064 "I/O %d QID %d timeout, disable controller\n",
1065 req->tag, nvmeq->qid);
1066 nvme_dev_shutdown(dev);
1067 req->errors = NVME_SC_CANCELLED;
1068 return BLK_EH_HANDLED;
1069 }
1070
1071 /*
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.
31c7c7d2 1075 */
a4aea562 1076 if (!nvmeq->qid || cmd_rq->aborted) {
e1569a16
KB
1077 dev_warn(dev->dev,
1078 "I/O %d QID %d timeout, reset controller\n",
1079 req->tag, nvmeq->qid);
1080 nvme_dev_shutdown(dev);
1081 queue_work(nvme_workq, &dev->reset_work);
1082
1083 /*
1084 * Mark the request as handled, since the inline shutdown
1085 * forces all outstanding requests to complete.
1086 */
1087 req->errors = NVME_SC_CANCELLED;
1088 return BLK_EH_HANDLED;
c30341dc
KB
1089 }
1090
e7a2a87d 1091 cmd_rq->aborted = 1;
c30341dc 1092
e7a2a87d 1093 if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) {
6bf25d16 1094 atomic_inc(&dev->ctrl.abort_limit);
31c7c7d2 1095 return BLK_EH_RESET_TIMER;
6bf25d16 1096 }
c30341dc
KB
1097
1098 memset(&cmd, 0, sizeof(cmd));
1099 cmd.abort.opcode = nvme_admin_abort_cmd;
a4aea562 1100 cmd.abort.cid = req->tag;
c30341dc 1101 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
c30341dc 1102
31c7c7d2
CH
1103 dev_warn(nvmeq->q_dmadev, "I/O %d QID %d timeout, aborting\n",
1104 req->tag, nvmeq->qid);
e7a2a87d
CH
1105
1106 abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd,
1107 BLK_MQ_REQ_NOWAIT);
1108 if (IS_ERR(abort_req)) {
1109 atomic_inc(&dev->ctrl.abort_limit);
1110 return BLK_EH_RESET_TIMER;
1111 }
1112
1113 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);
31c7c7d2
CH
1116
1117 /*
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 */
1122 return BLK_EH_RESET_TIMER;
c30341dc
KB
1123}
1124
42483228 1125static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
a09115b2 1126{
a4aea562
MB
1127 struct nvme_queue *nvmeq = data;
1128 void *ctx;
1129 nvme_completion_fn fn;
1130 struct nvme_cmd_info *cmd;
cef6a948
KB
1131 struct nvme_completion cqe;
1132
1133 if (!blk_mq_request_started(req))
1134 return;
a09115b2 1135
a4aea562 1136 cmd = blk_mq_rq_to_pdu(req);
a09115b2 1137
a4aea562
MB
1138 if (cmd->ctx == CMD_CTX_CANCELLED)
1139 return;
1140
cef6a948
KB
1141 if (blk_queue_dying(req->q))
1142 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1143 else
1144 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1145
1146
a4aea562
MB
1147 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1148 req->tag, nvmeq->qid);
1149 ctx = cancel_cmd_info(cmd, &fn);
1150 fn(nvmeq, ctx, &cqe);
a09115b2
MW
1151}
1152
a4aea562
MB
1153static void nvme_free_queue(struct nvme_queue *nvmeq)
1154{
9e866774
MW
1155 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1156 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
8ffaadf7
JD
1157 if (nvmeq->sq_cmds)
1158 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
9e866774
MW
1159 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1160 kfree(nvmeq);
1161}
1162
a1a5ef99 1163static void nvme_free_queues(struct nvme_dev *dev, int lowest)
22404274
KB
1164{
1165 int i;
1166
a1a5ef99 1167 for (i = dev->queue_count - 1; i >= lowest; i--) {
a4aea562 1168 struct nvme_queue *nvmeq = dev->queues[i];
22404274 1169 dev->queue_count--;
a4aea562 1170 dev->queues[i] = NULL;
f435c282 1171 nvme_free_queue(nvmeq);
121c7ad4 1172 }
22404274
KB
1173}
1174
4d115420
KB
1175/**
1176 * nvme_suspend_queue - put queue into suspended state
1177 * @nvmeq - queue to suspend
4d115420
KB
1178 */
1179static int nvme_suspend_queue(struct nvme_queue *nvmeq)
b60503ba 1180{
2b25d981 1181 int vector;
b60503ba 1182
a09115b2 1183 spin_lock_irq(&nvmeq->q_lock);
2b25d981
KB
1184 if (nvmeq->cq_vector == -1) {
1185 spin_unlock_irq(&nvmeq->q_lock);
1186 return 1;
1187 }
1188 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
42f61420 1189 nvmeq->dev->online_queues--;
2b25d981 1190 nvmeq->cq_vector = -1;
a09115b2
MW
1191 spin_unlock_irq(&nvmeq->q_lock);
1192
1c63dc66
CH
1193 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
1194 blk_mq_freeze_queue_start(nvmeq->dev->ctrl.admin_q);
6df3dbc8 1195
aba2080f
MW
1196 irq_set_affinity_hint(vector, NULL);
1197 free_irq(vector, nvmeq);
b60503ba 1198
4d115420
KB
1199 return 0;
1200}
b60503ba 1201
4d115420
KB
1202static void nvme_clear_queue(struct nvme_queue *nvmeq)
1203{
22404274 1204 spin_lock_irq(&nvmeq->q_lock);
42483228
KB
1205 if (nvmeq->tags && *nvmeq->tags)
1206 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
22404274 1207 spin_unlock_irq(&nvmeq->q_lock);
b60503ba
MW
1208}
1209
4d115420
KB
1210static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1211{
a4aea562 1212 struct nvme_queue *nvmeq = dev->queues[qid];
4d115420
KB
1213
1214 if (!nvmeq)
1215 return;
1216 if (nvme_suspend_queue(nvmeq))
1217 return;
1218
0e53d180
KB
1219 /* Don't tell the adapter to delete the admin queue.
1220 * Don't tell a removed adapter to delete IO queues. */
7a67cbea 1221 if (qid && readl(dev->bar + NVME_REG_CSTS) != -1) {
b60503ba
MW
1222 adapter_delete_sq(dev, qid);
1223 adapter_delete_cq(dev, qid);
1224 }
07836e65
KB
1225
1226 spin_lock_irq(&nvmeq->q_lock);
1227 nvme_process_cq(nvmeq);
1228 spin_unlock_irq(&nvmeq->q_lock);
b60503ba
MW
1229}
1230
8ffaadf7
JD
1231static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1232 int entry_size)
1233{
1234 int q_depth = dev->q_depth;
5fd4ce1b
CH
1235 unsigned q_size_aligned = roundup(q_depth * entry_size,
1236 dev->ctrl.page_size);
8ffaadf7
JD
1237
1238 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
c45f5c99 1239 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
5fd4ce1b 1240 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
c45f5c99 1241 q_depth = div_u64(mem_per_q, entry_size);
8ffaadf7
JD
1242
1243 /*
1244 * Ensure the reduced q_depth is above some threshold where it
1245 * would be better to map queues in system memory with the
1246 * original depth
1247 */
1248 if (q_depth < 64)
1249 return -ENOMEM;
1250 }
1251
1252 return q_depth;
1253}
1254
1255static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1256 int qid, int depth)
1257{
1258 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
5fd4ce1b
CH
1259 unsigned offset = (qid - 1) * roundup(SQ_SIZE(depth),
1260 dev->ctrl.page_size);
8ffaadf7
JD
1261 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1262 nvmeq->sq_cmds_io = dev->cmb + offset;
1263 } else {
1264 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1265 &nvmeq->sq_dma_addr, GFP_KERNEL);
1266 if (!nvmeq->sq_cmds)
1267 return -ENOMEM;
1268 }
1269
1270 return 0;
1271}
1272
b60503ba 1273static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
2b25d981 1274 int depth)
b60503ba 1275{
a4aea562 1276 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
b60503ba
MW
1277 if (!nvmeq)
1278 return NULL;
1279
e75ec752 1280 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
4d51abf9 1281 &nvmeq->cq_dma_addr, GFP_KERNEL);
b60503ba
MW
1282 if (!nvmeq->cqes)
1283 goto free_nvmeq;
b60503ba 1284
8ffaadf7 1285 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
b60503ba
MW
1286 goto free_cqdma;
1287
e75ec752 1288 nvmeq->q_dmadev = dev->dev;
091b6092 1289 nvmeq->dev = dev;
3193f07b 1290 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1c63dc66 1291 dev->ctrl.instance, qid);
b60503ba
MW
1292 spin_lock_init(&nvmeq->q_lock);
1293 nvmeq->cq_head = 0;
82123460 1294 nvmeq->cq_phase = 1;
b80d5ccc 1295 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
b60503ba 1296 nvmeq->q_depth = depth;
c30341dc 1297 nvmeq->qid = qid;
758dd7fd 1298 nvmeq->cq_vector = -1;
a4aea562 1299 dev->queues[qid] = nvmeq;
b60503ba 1300
36a7e993
JD
1301 /* make sure queue descriptor is set before queue count, for kthread */
1302 mb();
1303 dev->queue_count++;
1304
b60503ba
MW
1305 return nvmeq;
1306
1307 free_cqdma:
e75ec752 1308 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
b60503ba
MW
1309 nvmeq->cq_dma_addr);
1310 free_nvmeq:
1311 kfree(nvmeq);
1312 return NULL;
1313}
1314
3001082c
MW
1315static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1316 const char *name)
1317{
58ffacb5
MW
1318 if (use_threaded_interrupts)
1319 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
481e5bad 1320 nvme_irq_check, nvme_irq, IRQF_SHARED,
58ffacb5 1321 name, nvmeq);
3001082c 1322 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
481e5bad 1323 IRQF_SHARED, name, nvmeq);
3001082c
MW
1324}
1325
22404274 1326static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
b60503ba 1327{
22404274 1328 struct nvme_dev *dev = nvmeq->dev;
b60503ba 1329
7be50e93 1330 spin_lock_irq(&nvmeq->q_lock);
22404274
KB
1331 nvmeq->sq_tail = 0;
1332 nvmeq->cq_head = 0;
1333 nvmeq->cq_phase = 1;
b80d5ccc 1334 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
22404274 1335 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
42f61420 1336 dev->online_queues++;
7be50e93 1337 spin_unlock_irq(&nvmeq->q_lock);
22404274
KB
1338}
1339
1340static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1341{
1342 struct nvme_dev *dev = nvmeq->dev;
1343 int result;
3f85d50b 1344
2b25d981 1345 nvmeq->cq_vector = qid - 1;
b60503ba
MW
1346 result = adapter_alloc_cq(dev, qid, nvmeq);
1347 if (result < 0)
22404274 1348 return result;
b60503ba
MW
1349
1350 result = adapter_alloc_sq(dev, qid, nvmeq);
1351 if (result < 0)
1352 goto release_cq;
1353
3193f07b 1354 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
b60503ba
MW
1355 if (result < 0)
1356 goto release_sq;
1357
22404274 1358 nvme_init_queue(nvmeq, qid);
22404274 1359 return result;
b60503ba
MW
1360
1361 release_sq:
1362 adapter_delete_sq(dev, qid);
1363 release_cq:
1364 adapter_delete_cq(dev, qid);
22404274 1365 return result;
b60503ba
MW
1366}
1367
a4aea562 1368static struct blk_mq_ops nvme_mq_admin_ops = {
d29ec824 1369 .queue_rq = nvme_queue_rq,
a4aea562
MB
1370 .map_queue = blk_mq_map_queue,
1371 .init_hctx = nvme_admin_init_hctx,
4af0e21c 1372 .exit_hctx = nvme_admin_exit_hctx,
a4aea562
MB
1373 .init_request = nvme_admin_init_request,
1374 .timeout = nvme_timeout,
1375};
1376
1377static struct blk_mq_ops nvme_mq_ops = {
1378 .queue_rq = nvme_queue_rq,
1379 .map_queue = blk_mq_map_queue,
1380 .init_hctx = nvme_init_hctx,
1381 .init_request = nvme_init_request,
1382 .timeout = nvme_timeout,
a0fa9647 1383 .poll = nvme_poll,
a4aea562
MB
1384};
1385
ea191d2f
KB
1386static void nvme_dev_remove_admin(struct nvme_dev *dev)
1387{
1c63dc66
CH
1388 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
1389 blk_cleanup_queue(dev->ctrl.admin_q);
ea191d2f
KB
1390 blk_mq_free_tag_set(&dev->admin_tagset);
1391 }
1392}
1393
a4aea562
MB
1394static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1395{
1c63dc66 1396 if (!dev->ctrl.admin_q) {
a4aea562
MB
1397 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1398 dev->admin_tagset.nr_hw_queues = 1;
adf68f21 1399 dev->admin_tagset.queue_depth = NVME_AQ_BLKMQ_DEPTH;
a4aea562 1400 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
e75ec752 1401 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
ac3dd5bd 1402 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
a4aea562
MB
1403 dev->admin_tagset.driver_data = dev;
1404
1405 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1406 return -ENOMEM;
1407
1c63dc66
CH
1408 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1409 if (IS_ERR(dev->ctrl.admin_q)) {
a4aea562
MB
1410 blk_mq_free_tag_set(&dev->admin_tagset);
1411 return -ENOMEM;
1412 }
1c63dc66 1413 if (!blk_get_queue(dev->ctrl.admin_q)) {
ea191d2f 1414 nvme_dev_remove_admin(dev);
1c63dc66 1415 dev->ctrl.admin_q = NULL;
ea191d2f
KB
1416 return -ENODEV;
1417 }
0fb59cbc 1418 } else
1c63dc66 1419 blk_mq_unfreeze_queue(dev->ctrl.admin_q);
a4aea562
MB
1420
1421 return 0;
1422}
1423
8d85fce7 1424static int nvme_configure_admin_queue(struct nvme_dev *dev)
b60503ba 1425{
ba47e386 1426 int result;
b60503ba 1427 u32 aqa;
7a67cbea 1428 u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
b60503ba
MW
1429 struct nvme_queue *nvmeq;
1430
7a67cbea 1431 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1) ?
dfbac8c7
KB
1432 NVME_CAP_NSSRC(cap) : 0;
1433
7a67cbea
CH
1434 if (dev->subsystem &&
1435 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1436 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
dfbac8c7 1437
5fd4ce1b 1438 result = nvme_disable_ctrl(&dev->ctrl, cap);
ba47e386
MW
1439 if (result < 0)
1440 return result;
b60503ba 1441
a4aea562 1442 nvmeq = dev->queues[0];
cd638946 1443 if (!nvmeq) {
2b25d981 1444 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
cd638946
KB
1445 if (!nvmeq)
1446 return -ENOMEM;
cd638946 1447 }
b60503ba
MW
1448
1449 aqa = nvmeq->q_depth - 1;
1450 aqa |= aqa << 16;
1451
7a67cbea
CH
1452 writel(aqa, dev->bar + NVME_REG_AQA);
1453 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1454 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
b60503ba 1455
5fd4ce1b 1456 result = nvme_enable_ctrl(&dev->ctrl, cap);
025c557a 1457 if (result)
a4aea562
MB
1458 goto free_nvmeq;
1459
2b25d981 1460 nvmeq->cq_vector = 0;
3193f07b 1461 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
758dd7fd
JD
1462 if (result) {
1463 nvmeq->cq_vector = -1;
0fb59cbc 1464 goto free_nvmeq;
758dd7fd 1465 }
025c557a 1466
b60503ba 1467 return result;
a4aea562 1468
a4aea562
MB
1469 free_nvmeq:
1470 nvme_free_queues(dev, 0);
1471 return result;
b60503ba
MW
1472}
1473
1fa6aead
MW
1474static int nvme_kthread(void *data)
1475{
d4b4ff8e 1476 struct nvme_dev *dev, *next;
1fa6aead
MW
1477
1478 while (!kthread_should_stop()) {
564a232c 1479 set_current_state(TASK_INTERRUPTIBLE);
1fa6aead 1480 spin_lock(&dev_list_lock);
d4b4ff8e 1481 list_for_each_entry_safe(dev, next, &dev_list, node) {
1fa6aead 1482 int i;
7a67cbea 1483 u32 csts = readl(dev->bar + NVME_REG_CSTS);
dfbac8c7 1484
846cc05f
CH
1485 /*
1486 * Skip controllers currently under reset.
1487 */
1488 if (work_pending(&dev->reset_work) || work_busy(&dev->reset_work))
1489 continue;
1490
dfbac8c7
KB
1491 if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
1492 csts & NVME_CSTS_CFS) {
846cc05f 1493 if (queue_work(nvme_workq, &dev->reset_work)) {
90667892
CH
1494 dev_warn(dev->dev,
1495 "Failed status: %x, reset controller\n",
7a67cbea 1496 readl(dev->bar + NVME_REG_CSTS));
90667892 1497 }
d4b4ff8e
KB
1498 continue;
1499 }
1fa6aead 1500 for (i = 0; i < dev->queue_count; i++) {
a4aea562 1501 struct nvme_queue *nvmeq = dev->queues[i];
740216fc
MW
1502 if (!nvmeq)
1503 continue;
1fa6aead 1504 spin_lock_irq(&nvmeq->q_lock);
bc57a0f7 1505 nvme_process_cq(nvmeq);
6fccf938 1506
adf68f21
CH
1507 while (i == 0 && dev->ctrl.event_limit > 0)
1508 nvme_submit_async_event(dev);
1fa6aead
MW
1509 spin_unlock_irq(&nvmeq->q_lock);
1510 }
1511 }
1512 spin_unlock(&dev_list_lock);
acb7aa0d 1513 schedule_timeout(round_jiffies_relative(HZ));
1fa6aead
MW
1514 }
1515 return 0;
1516}
1517
749941f2 1518static int nvme_create_io_queues(struct nvme_dev *dev)
42f61420 1519{
a4aea562 1520 unsigned i;
749941f2 1521 int ret = 0;
42f61420 1522
749941f2
CH
1523 for (i = dev->queue_count; i <= dev->max_qid; i++) {
1524 if (!nvme_alloc_queue(dev, i, dev->q_depth)) {
1525 ret = -ENOMEM;
42f61420 1526 break;
749941f2
CH
1527 }
1528 }
42f61420 1529
749941f2
CH
1530 for (i = dev->online_queues; i <= dev->queue_count - 1; i++) {
1531 ret = nvme_create_queue(dev->queues[i], i);
1532 if (ret) {
2659e57b 1533 nvme_free_queues(dev, i);
42f61420 1534 break;
2659e57b 1535 }
749941f2
CH
1536 }
1537
1538 /*
1539 * Ignore failing Create SQ/CQ commands, we can continue with less
1540 * than the desired aount of queues, and even a controller without
1541 * I/O queues an still be used to issue admin commands. This might
1542 * be useful to upgrade a buggy firmware for example.
1543 */
1544 return ret >= 0 ? 0 : ret;
42f61420
KB
1545}
1546
8ffaadf7
JD
1547static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1548{
1549 u64 szu, size, offset;
1550 u32 cmbloc;
1551 resource_size_t bar_size;
1552 struct pci_dev *pdev = to_pci_dev(dev->dev);
1553 void __iomem *cmb;
1554 dma_addr_t dma_addr;
1555
1556 if (!use_cmb_sqes)
1557 return NULL;
1558
7a67cbea 1559 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
8ffaadf7
JD
1560 if (!(NVME_CMB_SZ(dev->cmbsz)))
1561 return NULL;
1562
7a67cbea 1563 cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
8ffaadf7
JD
1564
1565 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1566 size = szu * NVME_CMB_SZ(dev->cmbsz);
1567 offset = szu * NVME_CMB_OFST(cmbloc);
1568 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
1569
1570 if (offset > bar_size)
1571 return NULL;
1572
1573 /*
1574 * Controllers may support a CMB size larger than their BAR,
1575 * for example, due to being behind a bridge. Reduce the CMB to
1576 * the reported size of the BAR
1577 */
1578 if (size > bar_size - offset)
1579 size = bar_size - offset;
1580
1581 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
1582 cmb = ioremap_wc(dma_addr, size);
1583 if (!cmb)
1584 return NULL;
1585
1586 dev->cmb_dma_addr = dma_addr;
1587 dev->cmb_size = size;
1588 return cmb;
1589}
1590
1591static inline void nvme_release_cmb(struct nvme_dev *dev)
1592{
1593 if (dev->cmb) {
1594 iounmap(dev->cmb);
1595 dev->cmb = NULL;
1596 }
1597}
1598
9d713c2b
KB
1599static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1600{
b80d5ccc 1601 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
9d713c2b
KB
1602}
1603
8d85fce7 1604static int nvme_setup_io_queues(struct nvme_dev *dev)
b60503ba 1605{
a4aea562 1606 struct nvme_queue *adminq = dev->queues[0];
e75ec752 1607 struct pci_dev *pdev = to_pci_dev(dev->dev);
42f61420 1608 int result, i, vecs, nr_io_queues, size;
b60503ba 1609
42f61420 1610 nr_io_queues = num_possible_cpus();
9a0be7ab
CH
1611 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
1612 if (result < 0)
1b23484b 1613 return result;
9a0be7ab
CH
1614
1615 /*
1616 * Degraded controllers might return an error when setting the queue
1617 * count. We still want to be able to bring them online and offer
1618 * access to the admin queue, as that might be only way to fix them up.
1619 */
1620 if (result > 0) {
1621 dev_err(dev->dev, "Could not set queue count (%d)\n", result);
1622 nr_io_queues = 0;
1623 result = 0;
1624 }
b60503ba 1625
8ffaadf7
JD
1626 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1627 result = nvme_cmb_qdepth(dev, nr_io_queues,
1628 sizeof(struct nvme_command));
1629 if (result > 0)
1630 dev->q_depth = result;
1631 else
1632 nvme_release_cmb(dev);
1633 }
1634
9d713c2b
KB
1635 size = db_bar_size(dev, nr_io_queues);
1636 if (size > 8192) {
f1938f6e 1637 iounmap(dev->bar);
9d713c2b
KB
1638 do {
1639 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1640 if (dev->bar)
1641 break;
1642 if (!--nr_io_queues)
1643 return -ENOMEM;
1644 size = db_bar_size(dev, nr_io_queues);
1645 } while (1);
7a67cbea 1646 dev->dbs = dev->bar + 4096;
5a92e700 1647 adminq->q_db = dev->dbs;
f1938f6e
MW
1648 }
1649
9d713c2b 1650 /* Deregister the admin queue's interrupt */
3193f07b 1651 free_irq(dev->entry[0].vector, adminq);
9d713c2b 1652
e32efbfc
JA
1653 /*
1654 * If we enable msix early due to not intx, disable it again before
1655 * setting up the full range we need.
1656 */
1657 if (!pdev->irq)
1658 pci_disable_msix(pdev);
1659
be577fab 1660 for (i = 0; i < nr_io_queues; i++)
1b23484b 1661 dev->entry[i].entry = i;
be577fab
AG
1662 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
1663 if (vecs < 0) {
1664 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
1665 if (vecs < 0) {
1666 vecs = 1;
1667 } else {
1668 for (i = 0; i < vecs; i++)
1669 dev->entry[i].vector = i + pdev->irq;
fa08a396
RRG
1670 }
1671 }
1672
063a8096
MW
1673 /*
1674 * Should investigate if there's a performance win from allocating
1675 * more queues than interrupt vectors; it might allow the submission
1676 * path to scale better, even if the receive path is limited by the
1677 * number of interrupts.
1678 */
1679 nr_io_queues = vecs;
42f61420 1680 dev->max_qid = nr_io_queues;
063a8096 1681
3193f07b 1682 result = queue_request_irq(dev, adminq, adminq->irqname);
758dd7fd
JD
1683 if (result) {
1684 adminq->cq_vector = -1;
22404274 1685 goto free_queues;
758dd7fd 1686 }
1b23484b 1687
cd638946 1688 /* Free previously allocated queues that are no longer usable */
42f61420 1689 nvme_free_queues(dev, nr_io_queues + 1);
749941f2 1690 return nvme_create_io_queues(dev);
b60503ba 1691
22404274 1692 free_queues:
a1a5ef99 1693 nvme_free_queues(dev, 1);
22404274 1694 return result;
b60503ba
MW
1695}
1696
bda4e0fb
KB
1697static void nvme_set_irq_hints(struct nvme_dev *dev)
1698{
1699 struct nvme_queue *nvmeq;
1700 int i;
1701
1702 for (i = 0; i < dev->online_queues; i++) {
1703 nvmeq = dev->queues[i];
1704
1705 if (!nvmeq->tags || !(*nvmeq->tags))
1706 continue;
1707
1708 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
1709 blk_mq_tags_cpumask(*nvmeq->tags));
1710 }
1711}
1712
a5768aa8
KB
1713static void nvme_dev_scan(struct work_struct *work)
1714{
1715 struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
a5768aa8
KB
1716
1717 if (!dev->tagset.tags)
1718 return;
5bae7f73 1719 nvme_scan_namespaces(&dev->ctrl);
bda4e0fb 1720 nvme_set_irq_hints(dev);
a5768aa8
KB
1721}
1722
422ef0c7
MW
1723/*
1724 * Return: error value if an error occurred setting up the queues or calling
1725 * Identify Device. 0 if these succeeded, even if adding some of the
1726 * namespaces failed. At the moment, these failures are silent. TBD which
1727 * failures should be reported.
1728 */
8d85fce7 1729static int nvme_dev_add(struct nvme_dev *dev)
b60503ba 1730{
5bae7f73 1731 if (!dev->ctrl.tagset) {
ffe7704d
KB
1732 dev->tagset.ops = &nvme_mq_ops;
1733 dev->tagset.nr_hw_queues = dev->online_queues - 1;
1734 dev->tagset.timeout = NVME_IO_TIMEOUT;
1735 dev->tagset.numa_node = dev_to_node(dev->dev);
1736 dev->tagset.queue_depth =
a4aea562 1737 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
ffe7704d
KB
1738 dev->tagset.cmd_size = nvme_cmd_size(dev);
1739 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
1740 dev->tagset.driver_data = dev;
b60503ba 1741
ffe7704d
KB
1742 if (blk_mq_alloc_tag_set(&dev->tagset))
1743 return 0;
5bae7f73 1744 dev->ctrl.tagset = &dev->tagset;
ffe7704d 1745 }
92f7a162 1746 queue_work(nvme_workq, &dev->scan_work);
e1e5e564 1747 return 0;
b60503ba
MW
1748}
1749
0877cb0d
KB
1750static int nvme_dev_map(struct nvme_dev *dev)
1751{
42f61420 1752 u64 cap;
0877cb0d 1753 int bars, result = -ENOMEM;
e75ec752 1754 struct pci_dev *pdev = to_pci_dev(dev->dev);
0877cb0d
KB
1755
1756 if (pci_enable_device_mem(pdev))
1757 return result;
1758
1759 dev->entry[0].vector = pdev->irq;
1760 pci_set_master(pdev);
1761 bars = pci_select_bars(pdev, IORESOURCE_MEM);
be7837e8
JA
1762 if (!bars)
1763 goto disable_pci;
1764
0877cb0d
KB
1765 if (pci_request_selected_regions(pdev, bars, "nvme"))
1766 goto disable_pci;
1767
e75ec752
CH
1768 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
1769 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
052d0efa 1770 goto disable;
0877cb0d 1771
0877cb0d
KB
1772 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1773 if (!dev->bar)
1774 goto disable;
e32efbfc 1775
7a67cbea 1776 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
0e53d180
KB
1777 result = -ENODEV;
1778 goto unmap;
1779 }
e32efbfc
JA
1780
1781 /*
1782 * Some devices don't advertse INTx interrupts, pre-enable a single
1783 * MSIX vec for setup. We'll adjust this later.
1784 */
1785 if (!pdev->irq) {
1786 result = pci_enable_msix(pdev, dev->entry, 1);
1787 if (result < 0)
1788 goto unmap;
1789 }
1790
7a67cbea
CH
1791 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
1792
42f61420
KB
1793 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
1794 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
7a67cbea
CH
1795 dev->dbs = dev->bar + 4096;
1796 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2))
8ffaadf7 1797 dev->cmb = nvme_map_cmb(dev);
0877cb0d
KB
1798
1799 return 0;
1800
0e53d180
KB
1801 unmap:
1802 iounmap(dev->bar);
1803 dev->bar = NULL;
0877cb0d
KB
1804 disable:
1805 pci_release_regions(pdev);
1806 disable_pci:
1807 pci_disable_device(pdev);
1808 return result;
1809}
1810
1811static void nvme_dev_unmap(struct nvme_dev *dev)
1812{
e75ec752
CH
1813 struct pci_dev *pdev = to_pci_dev(dev->dev);
1814
1815 if (pdev->msi_enabled)
1816 pci_disable_msi(pdev);
1817 else if (pdev->msix_enabled)
1818 pci_disable_msix(pdev);
0877cb0d
KB
1819
1820 if (dev->bar) {
1821 iounmap(dev->bar);
1822 dev->bar = NULL;
e75ec752 1823 pci_release_regions(pdev);
0877cb0d
KB
1824 }
1825
e75ec752
CH
1826 if (pci_is_enabled(pdev))
1827 pci_disable_device(pdev);
0877cb0d
KB
1828}
1829
4d115420
KB
1830struct nvme_delq_ctx {
1831 struct task_struct *waiter;
1832 struct kthread_worker *worker;
1833 atomic_t refcount;
1834};
1835
1836static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
1837{
1838 dq->waiter = current;
1839 mb();
1840
1841 for (;;) {
1842 set_current_state(TASK_KILLABLE);
1843 if (!atomic_read(&dq->refcount))
1844 break;
1845 if (!schedule_timeout(ADMIN_TIMEOUT) ||
1846 fatal_signal_pending(current)) {
0fb59cbc
KB
1847 /*
1848 * Disable the controller first since we can't trust it
1849 * at this point, but leave the admin queue enabled
1850 * until all queue deletion requests are flushed.
1851 * FIXME: This may take a while if there are more h/w
1852 * queues than admin tags.
1853 */
4d115420 1854 set_current_state(TASK_RUNNING);
5fd4ce1b 1855 nvme_disable_ctrl(&dev->ctrl,
7a67cbea 1856 lo_hi_readq(dev->bar + NVME_REG_CAP));
0fb59cbc 1857 nvme_clear_queue(dev->queues[0]);
4d115420 1858 flush_kthread_worker(dq->worker);
0fb59cbc 1859 nvme_disable_queue(dev, 0);
4d115420
KB
1860 return;
1861 }
1862 }
1863 set_current_state(TASK_RUNNING);
1864}
1865
1866static void nvme_put_dq(struct nvme_delq_ctx *dq)
1867{
1868 atomic_dec(&dq->refcount);
1869 if (dq->waiter)
1870 wake_up_process(dq->waiter);
1871}
1872
1873static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
1874{
1875 atomic_inc(&dq->refcount);
1876 return dq;
1877}
1878
1879static void nvme_del_queue_end(struct nvme_queue *nvmeq)
1880{
1881 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
4d115420 1882 nvme_put_dq(dq);
604e8c8d
KB
1883
1884 spin_lock_irq(&nvmeq->q_lock);
1885 nvme_process_cq(nvmeq);
1886 spin_unlock_irq(&nvmeq->q_lock);
4d115420
KB
1887}
1888
1889static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
1890 kthread_work_func_t fn)
1891{
d8f32166 1892 struct request *req;
4d115420
KB
1893 struct nvme_command c;
1894
1895 memset(&c, 0, sizeof(c));
1896 c.delete_queue.opcode = opcode;
1897 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
1898
1899 init_kthread_work(&nvmeq->cmdinfo.work, fn);
d8f32166
CH
1900
1901 req = nvme_alloc_request(nvmeq->dev->ctrl.admin_q, &c, 0);
1902 if (IS_ERR(req))
1903 return PTR_ERR(req);
1904
1905 req->timeout = ADMIN_TIMEOUT;
1906 req->end_io_data = &nvmeq->cmdinfo;
1907 blk_execute_rq_nowait(req->q, NULL, req, 0, async_cmd_info_endio);
1908 return 0;
4d115420
KB
1909}
1910
1911static void nvme_del_cq_work_handler(struct kthread_work *work)
1912{
1913 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1914 cmdinfo.work);
1915 nvme_del_queue_end(nvmeq);
1916}
1917
1918static int nvme_delete_cq(struct nvme_queue *nvmeq)
1919{
1920 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
1921 nvme_del_cq_work_handler);
1922}
1923
1924static void nvme_del_sq_work_handler(struct kthread_work *work)
1925{
1926 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1927 cmdinfo.work);
1928 int status = nvmeq->cmdinfo.status;
1929
1930 if (!status)
1931 status = nvme_delete_cq(nvmeq);
1932 if (status)
1933 nvme_del_queue_end(nvmeq);
1934}
1935
1936static int nvme_delete_sq(struct nvme_queue *nvmeq)
1937{
1938 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
1939 nvme_del_sq_work_handler);
1940}
1941
1942static void nvme_del_queue_start(struct kthread_work *work)
1943{
1944 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1945 cmdinfo.work);
4d115420
KB
1946 if (nvme_delete_sq(nvmeq))
1947 nvme_del_queue_end(nvmeq);
1948}
1949
1950static void nvme_disable_io_queues(struct nvme_dev *dev)
1951{
1952 int i;
1953 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
1954 struct nvme_delq_ctx dq;
1955 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
1c63dc66 1956 &worker, "nvme%d", dev->ctrl.instance);
4d115420
KB
1957
1958 if (IS_ERR(kworker_task)) {
e75ec752 1959 dev_err(dev->dev,
4d115420
KB
1960 "Failed to create queue del task\n");
1961 for (i = dev->queue_count - 1; i > 0; i--)
1962 nvme_disable_queue(dev, i);
1963 return;
1964 }
1965
1966 dq.waiter = NULL;
1967 atomic_set(&dq.refcount, 0);
1968 dq.worker = &worker;
1969 for (i = dev->queue_count - 1; i > 0; i--) {
a4aea562 1970 struct nvme_queue *nvmeq = dev->queues[i];
4d115420
KB
1971
1972 if (nvme_suspend_queue(nvmeq))
1973 continue;
1974 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
1975 nvmeq->cmdinfo.worker = dq.worker;
1976 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
1977 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
1978 }
1979 nvme_wait_dq(&dq, dev);
1980 kthread_stop(kworker_task);
1981}
1982
7385014c
CH
1983static int nvme_dev_list_add(struct nvme_dev *dev)
1984{
1985 bool start_thread = false;
1986
1987 spin_lock(&dev_list_lock);
1988 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
1989 start_thread = true;
1990 nvme_thread = NULL;
1991 }
1992 list_add(&dev->node, &dev_list);
1993 spin_unlock(&dev_list_lock);
1994
1995 if (start_thread) {
1996 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
1997 wake_up_all(&nvme_kthread_wait);
1998 } else
1999 wait_event_killable(nvme_kthread_wait, nvme_thread);
2000
2001 if (IS_ERR_OR_NULL(nvme_thread))
2002 return nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2003
2004 return 0;
2005}
2006
b9afca3e
DM
2007/*
2008* Remove the node from the device list and check
2009* for whether or not we need to stop the nvme_thread.
2010*/
2011static void nvme_dev_list_remove(struct nvme_dev *dev)
2012{
2013 struct task_struct *tmp = NULL;
2014
2015 spin_lock(&dev_list_lock);
2016 list_del_init(&dev->node);
2017 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2018 tmp = nvme_thread;
2019 nvme_thread = NULL;
2020 }
2021 spin_unlock(&dev_list_lock);
2022
2023 if (tmp)
2024 kthread_stop(tmp);
2025}
2026
c9d3bf88
KB
2027static void nvme_freeze_queues(struct nvme_dev *dev)
2028{
2029 struct nvme_ns *ns;
2030
5bae7f73 2031 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
c9d3bf88
KB
2032 blk_mq_freeze_queue_start(ns->queue);
2033
cddcd72b 2034 spin_lock_irq(ns->queue->queue_lock);
c9d3bf88 2035 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
cddcd72b 2036 spin_unlock_irq(ns->queue->queue_lock);
c9d3bf88
KB
2037
2038 blk_mq_cancel_requeue_work(ns->queue);
2039 blk_mq_stop_hw_queues(ns->queue);
2040 }
2041}
2042
2043static void nvme_unfreeze_queues(struct nvme_dev *dev)
2044{
2045 struct nvme_ns *ns;
2046
5bae7f73 2047 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
c9d3bf88
KB
2048 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2049 blk_mq_unfreeze_queue(ns->queue);
2050 blk_mq_start_stopped_hw_queues(ns->queue, true);
2051 blk_mq_kick_requeue_list(ns->queue);
2052 }
2053}
2054
f0b50732 2055static void nvme_dev_shutdown(struct nvme_dev *dev)
b60503ba 2056{
22404274 2057 int i;
7c1b2450 2058 u32 csts = -1;
22404274 2059
b9afca3e 2060 nvme_dev_list_remove(dev);
1fa6aead 2061
77bf25ea 2062 mutex_lock(&dev->shutdown_lock);
c9d3bf88
KB
2063 if (dev->bar) {
2064 nvme_freeze_queues(dev);
7a67cbea 2065 csts = readl(dev->bar + NVME_REG_CSTS);
c9d3bf88 2066 }
7c1b2450 2067 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
4d115420 2068 for (i = dev->queue_count - 1; i >= 0; i--) {
a4aea562 2069 struct nvme_queue *nvmeq = dev->queues[i];
4d115420 2070 nvme_suspend_queue(nvmeq);
4d115420
KB
2071 }
2072 } else {
2073 nvme_disable_io_queues(dev);
5fd4ce1b 2074 nvme_shutdown_ctrl(&dev->ctrl);
4d115420
KB
2075 nvme_disable_queue(dev, 0);
2076 }
f0b50732 2077 nvme_dev_unmap(dev);
07836e65
KB
2078
2079 for (i = dev->queue_count - 1; i >= 0; i--)
2080 nvme_clear_queue(dev->queues[i]);
77bf25ea 2081 mutex_unlock(&dev->shutdown_lock);
f0b50732
KB
2082}
2083
091b6092
MW
2084static int nvme_setup_prp_pools(struct nvme_dev *dev)
2085{
e75ec752 2086 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
091b6092
MW
2087 PAGE_SIZE, PAGE_SIZE, 0);
2088 if (!dev->prp_page_pool)
2089 return -ENOMEM;
2090
99802a7a 2091 /* Optimisation for I/Os between 4k and 128k */
e75ec752 2092 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
99802a7a
MW
2093 256, 256, 0);
2094 if (!dev->prp_small_pool) {
2095 dma_pool_destroy(dev->prp_page_pool);
2096 return -ENOMEM;
2097 }
091b6092
MW
2098 return 0;
2099}
2100
2101static void nvme_release_prp_pools(struct nvme_dev *dev)
2102{
2103 dma_pool_destroy(dev->prp_page_pool);
99802a7a 2104 dma_pool_destroy(dev->prp_small_pool);
091b6092
MW
2105}
2106
1673f1f0 2107static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
5e82e952 2108{
1673f1f0 2109 struct nvme_dev *dev = to_nvme_dev(ctrl);
9ac27090 2110
e75ec752 2111 put_device(dev->dev);
4af0e21c
KB
2112 if (dev->tagset.tags)
2113 blk_mq_free_tag_set(&dev->tagset);
1c63dc66
CH
2114 if (dev->ctrl.admin_q)
2115 blk_put_queue(dev->ctrl.admin_q);
5e82e952
KB
2116 kfree(dev->queues);
2117 kfree(dev->entry);
2118 kfree(dev);
2119}
2120
fd634f41 2121static void nvme_reset_work(struct work_struct *work)
f0b50732 2122{
fd634f41 2123 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
3cf519b5 2124 int result;
f0b50732 2125
fd634f41
CH
2126 if (WARN_ON(test_bit(NVME_CTRL_RESETTING, &dev->flags)))
2127 goto out;
2128
2129 /*
2130 * If we're called to reset a live controller first shut it down before
2131 * moving on.
2132 */
2133 if (dev->bar)
2134 nvme_dev_shutdown(dev);
2135
2136 set_bit(NVME_CTRL_RESETTING, &dev->flags);
2137
f0b50732
KB
2138 result = nvme_dev_map(dev);
2139 if (result)
3cf519b5 2140 goto out;
f0b50732
KB
2141
2142 result = nvme_configure_admin_queue(dev);
2143 if (result)
2144 goto unmap;
2145
a4aea562 2146 nvme_init_queue(dev->queues[0], 0);
0fb59cbc
KB
2147 result = nvme_alloc_admin_tags(dev);
2148 if (result)
2149 goto disable;
b9afca3e 2150
ce4541f4
CH
2151 result = nvme_init_identify(&dev->ctrl);
2152 if (result)
2153 goto free_tags;
2154
f0b50732 2155 result = nvme_setup_io_queues(dev);
badc34d4 2156 if (result)
0fb59cbc 2157 goto free_tags;
f0b50732 2158
adf68f21 2159 dev->ctrl.event_limit = NVME_NR_AEN_COMMANDS;
3cf519b5 2160
7385014c
CH
2161 result = nvme_dev_list_add(dev);
2162 if (result)
2163 goto remove;
2164
2659e57b
CH
2165 /*
2166 * Keep the controller around but remove all namespaces if we don't have
2167 * any working I/O queue.
2168 */
3cf519b5
CH
2169 if (dev->online_queues < 2) {
2170 dev_warn(dev->dev, "IO queues not created\n");
5bae7f73 2171 nvme_remove_namespaces(&dev->ctrl);
3cf519b5
CH
2172 } else {
2173 nvme_unfreeze_queues(dev);
2174 nvme_dev_add(dev);
2175 }
2176
fd634f41 2177 clear_bit(NVME_CTRL_RESETTING, &dev->flags);
3cf519b5 2178 return;
f0b50732 2179
7385014c
CH
2180 remove:
2181 nvme_dev_list_remove(dev);
0fb59cbc
KB
2182 free_tags:
2183 nvme_dev_remove_admin(dev);
1c63dc66
CH
2184 blk_put_queue(dev->ctrl.admin_q);
2185 dev->ctrl.admin_q = NULL;
4af0e21c 2186 dev->queues[0]->tags = NULL;
f0b50732 2187 disable:
a1a5ef99 2188 nvme_disable_queue(dev, 0);
f0b50732
KB
2189 unmap:
2190 nvme_dev_unmap(dev);
3cf519b5 2191 out:
5c8809e6 2192 nvme_remove_dead_ctrl(dev);
f0b50732
KB
2193}
2194
5c8809e6 2195static void nvme_remove_dead_ctrl_work(struct work_struct *work)
9a6b9458 2196{
5c8809e6 2197 struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
e75ec752 2198 struct pci_dev *pdev = to_pci_dev(dev->dev);
9a6b9458
KB
2199
2200 if (pci_get_drvdata(pdev))
c81f4975 2201 pci_stop_and_remove_bus_device_locked(pdev);
1673f1f0 2202 nvme_put_ctrl(&dev->ctrl);
9a6b9458
KB
2203}
2204
5c8809e6 2205static void nvme_remove_dead_ctrl(struct nvme_dev *dev)
de3eff2b 2206{
5c8809e6 2207 dev_warn(dev->dev, "Removing after probe failure\n");
1673f1f0 2208 kref_get(&dev->ctrl.kref);
5c8809e6 2209 if (!schedule_work(&dev->remove_work))
1673f1f0 2210 nvme_put_ctrl(&dev->ctrl);
de3eff2b
KB
2211}
2212
4cc06521
KB
2213static int nvme_reset(struct nvme_dev *dev)
2214{
1c63dc66 2215 if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q))
4cc06521
KB
2216 return -ENODEV;
2217
846cc05f
CH
2218 if (!queue_work(nvme_workq, &dev->reset_work))
2219 return -EBUSY;
4cc06521 2220
846cc05f 2221 flush_work(&dev->reset_work);
846cc05f 2222 return 0;
4cc06521
KB
2223}
2224
1c63dc66
CH
2225static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
2226{
2227 *val = readl(to_nvme_dev(ctrl)->bar + off);
2228 return 0;
2229}
2230
5fd4ce1b
CH
2231static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2232{
2233 writel(val, to_nvme_dev(ctrl)->bar + off);
2234 return 0;
2235}
2236
7fd8930f
CH
2237static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2238{
2239 *val = readq(to_nvme_dev(ctrl)->bar + off);
2240 return 0;
2241}
2242
5bae7f73
CH
2243static bool nvme_pci_io_incapable(struct nvme_ctrl *ctrl)
2244{
2245 struct nvme_dev *dev = to_nvme_dev(ctrl);
2246
2247 return !dev->bar || dev->online_queues < 2;
2248}
2249
f3ca80fc
CH
2250static int nvme_pci_reset_ctrl(struct nvme_ctrl *ctrl)
2251{
2252 return nvme_reset(to_nvme_dev(ctrl));
2253}
2254
1c63dc66
CH
2255static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
2256 .reg_read32 = nvme_pci_reg_read32,
5fd4ce1b 2257 .reg_write32 = nvme_pci_reg_write32,
7fd8930f 2258 .reg_read64 = nvme_pci_reg_read64,
5bae7f73 2259 .io_incapable = nvme_pci_io_incapable,
f3ca80fc 2260 .reset_ctrl = nvme_pci_reset_ctrl,
1673f1f0 2261 .free_ctrl = nvme_pci_free_ctrl,
1c63dc66
CH
2262};
2263
8d85fce7 2264static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
b60503ba 2265{
a4aea562 2266 int node, result = -ENOMEM;
b60503ba
MW
2267 struct nvme_dev *dev;
2268
a4aea562
MB
2269 node = dev_to_node(&pdev->dev);
2270 if (node == NUMA_NO_NODE)
2271 set_dev_node(&pdev->dev, 0);
2272
2273 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
b60503ba
MW
2274 if (!dev)
2275 return -ENOMEM;
a4aea562
MB
2276 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2277 GFP_KERNEL, node);
b60503ba
MW
2278 if (!dev->entry)
2279 goto free;
a4aea562
MB
2280 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2281 GFP_KERNEL, node);
b60503ba
MW
2282 if (!dev->queues)
2283 goto free;
2284
e75ec752 2285 dev->dev = get_device(&pdev->dev);
9a6b9458 2286 pci_set_drvdata(pdev, dev);
1c63dc66 2287
f3ca80fc
CH
2288 INIT_LIST_HEAD(&dev->node);
2289 INIT_WORK(&dev->scan_work, nvme_dev_scan);
f3ca80fc 2290 INIT_WORK(&dev->reset_work, nvme_reset_work);
5c8809e6 2291 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
77bf25ea 2292 mutex_init(&dev->shutdown_lock);
1c63dc66 2293
f3ca80fc 2294 result = nvme_setup_prp_pools(dev);
cd58ad7d 2295 if (result)
a96d4f5c 2296 goto put_pci;
b60503ba 2297
f3ca80fc
CH
2298 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
2299 id->driver_data);
091b6092 2300 if (result)
2e1d8448 2301 goto release_pools;
740216fc 2302
92f7a162 2303 queue_work(nvme_workq, &dev->reset_work);
b60503ba
MW
2304 return 0;
2305
0877cb0d 2306 release_pools:
091b6092 2307 nvme_release_prp_pools(dev);
a96d4f5c 2308 put_pci:
e75ec752 2309 put_device(dev->dev);
b60503ba
MW
2310 free:
2311 kfree(dev->queues);
2312 kfree(dev->entry);
2313 kfree(dev);
2314 return result;
2315}
2316
f0d54a54
KB
2317static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2318{
a6739479 2319 struct nvme_dev *dev = pci_get_drvdata(pdev);
f0d54a54 2320
a6739479
KB
2321 if (prepare)
2322 nvme_dev_shutdown(dev);
2323 else
92f7a162 2324 queue_work(nvme_workq, &dev->reset_work);
f0d54a54
KB
2325}
2326
09ece142
KB
2327static void nvme_shutdown(struct pci_dev *pdev)
2328{
2329 struct nvme_dev *dev = pci_get_drvdata(pdev);
2330 nvme_dev_shutdown(dev);
2331}
2332
8d85fce7 2333static void nvme_remove(struct pci_dev *pdev)
b60503ba
MW
2334{
2335 struct nvme_dev *dev = pci_get_drvdata(pdev);
9a6b9458
KB
2336
2337 spin_lock(&dev_list_lock);
2338 list_del_init(&dev->node);
2339 spin_unlock(&dev_list_lock);
2340
2341 pci_set_drvdata(pdev, NULL);
2342 flush_work(&dev->reset_work);
a5768aa8 2343 flush_work(&dev->scan_work);
5bae7f73 2344 nvme_remove_namespaces(&dev->ctrl);
53029b04 2345 nvme_uninit_ctrl(&dev->ctrl);
3399a3f7 2346 nvme_dev_shutdown(dev);
a4aea562 2347 nvme_dev_remove_admin(dev);
a1a5ef99 2348 nvme_free_queues(dev, 0);
8ffaadf7 2349 nvme_release_cmb(dev);
9a6b9458 2350 nvme_release_prp_pools(dev);
1673f1f0 2351 nvme_put_ctrl(&dev->ctrl);
b60503ba
MW
2352}
2353
2354/* These functions are yet to be implemented */
2355#define nvme_error_detected NULL
2356#define nvme_dump_registers NULL
2357#define nvme_link_reset NULL
2358#define nvme_slot_reset NULL
2359#define nvme_error_resume NULL
cd638946 2360
671a6018 2361#ifdef CONFIG_PM_SLEEP
cd638946
KB
2362static int nvme_suspend(struct device *dev)
2363{
2364 struct pci_dev *pdev = to_pci_dev(dev);
2365 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2366
2367 nvme_dev_shutdown(ndev);
2368 return 0;
2369}
2370
2371static int nvme_resume(struct device *dev)
2372{
2373 struct pci_dev *pdev = to_pci_dev(dev);
2374 struct nvme_dev *ndev = pci_get_drvdata(pdev);
cd638946 2375
92f7a162 2376 queue_work(nvme_workq, &ndev->reset_work);
9a6b9458 2377 return 0;
cd638946 2378}
671a6018 2379#endif
cd638946
KB
2380
2381static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
b60503ba 2382
1d352035 2383static const struct pci_error_handlers nvme_err_handler = {
b60503ba
MW
2384 .error_detected = nvme_error_detected,
2385 .mmio_enabled = nvme_dump_registers,
2386 .link_reset = nvme_link_reset,
2387 .slot_reset = nvme_slot_reset,
2388 .resume = nvme_error_resume,
f0d54a54 2389 .reset_notify = nvme_reset_notify,
b60503ba
MW
2390};
2391
2392/* Move to pci_ids.h later */
2393#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2394
6eb0d698 2395static const struct pci_device_id nvme_id_table[] = {
106198ed
CH
2396 { PCI_VDEVICE(INTEL, 0x0953),
2397 .driver_data = NVME_QUIRK_STRIPE_SIZE, },
540c801c
KB
2398 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2399 .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
b60503ba 2400 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
c74dc780 2401 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
b60503ba
MW
2402 { 0, }
2403};
2404MODULE_DEVICE_TABLE(pci, nvme_id_table);
2405
2406static struct pci_driver nvme_driver = {
2407 .name = "nvme",
2408 .id_table = nvme_id_table,
2409 .probe = nvme_probe,
8d85fce7 2410 .remove = nvme_remove,
09ece142 2411 .shutdown = nvme_shutdown,
cd638946
KB
2412 .driver = {
2413 .pm = &nvme_dev_pm_ops,
2414 },
b60503ba
MW
2415 .err_handler = &nvme_err_handler,
2416};
2417
2418static int __init nvme_init(void)
2419{
0ac13140 2420 int result;
1fa6aead 2421
b9afca3e 2422 init_waitqueue_head(&nvme_kthread_wait);
b60503ba 2423
92f7a162 2424 nvme_workq = alloc_workqueue("nvme", WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
9a6b9458 2425 if (!nvme_workq)
b9afca3e 2426 return -ENOMEM;
9a6b9458 2427
5bae7f73 2428 result = nvme_core_init();
5c42ea16 2429 if (result < 0)
9a6b9458 2430 goto kill_workq;
b60503ba 2431
f3db22fe
KB
2432 result = pci_register_driver(&nvme_driver);
2433 if (result)
f3ca80fc 2434 goto core_exit;
1fa6aead 2435 return 0;
b60503ba 2436
f3ca80fc 2437 core_exit:
5bae7f73 2438 nvme_core_exit();
9a6b9458
KB
2439 kill_workq:
2440 destroy_workqueue(nvme_workq);
b60503ba
MW
2441 return result;
2442}
2443
2444static void __exit nvme_exit(void)
2445{
2446 pci_unregister_driver(&nvme_driver);
5bae7f73 2447 nvme_core_exit();
9a6b9458 2448 destroy_workqueue(nvme_workq);
b9afca3e 2449 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
21bd78bc 2450 _nvme_check_size();
b60503ba
MW
2451}
2452
2453MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2454MODULE_LICENSE("GPL");
c78b4713 2455MODULE_VERSION("1.0");
b60503ba
MW
2456module_init(nvme_init);
2457module_exit(nvme_exit);