]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/nvme/host/pci.c
nvme: move remaining CC setup into nvme_enable_ctrl
[mirror_ubuntu-bionic-kernel.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 30#include <linux/kernel.h>
a5768aa8 31#include <linux/list_sort.h>
b60503ba
MW
32#include <linux/mm.h>
33#include <linux/module.h>
34#include <linux/moduleparam.h>
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>
1d277a63 42#include <linux/pr.h>
5d0f6131 43#include <scsi/sg.h>
2f8e2c87 44#include <linux/io-64-nonatomic-lo-hi.h>
1d277a63 45#include <asm/unaligned.h>
797a796a 46
9d99a8dd 47#include <uapi/linux/nvme_ioctl.h>
f11bb3e2
CH
48#include "nvme.h"
49
b3fffdef 50#define NVME_MINORS (1U << MINORBITS)
9d43cf64 51#define NVME_Q_DEPTH 1024
d31af0a3 52#define NVME_AQ_DEPTH 256
b60503ba
MW
53#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
54#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
2484f407 55#define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ)
9d43cf64 56
21d34711 57unsigned char admin_timeout = 60;
9d43cf64
KB
58module_param(admin_timeout, byte, 0644);
59MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
b60503ba 60
bd67608a
MW
61unsigned char nvme_io_timeout = 30;
62module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
b355084a 63MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
b60503ba 64
2484f407
DM
65static unsigned char shutdown_timeout = 5;
66module_param(shutdown_timeout, byte, 0644);
67MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
68
b60503ba
MW
69static int nvme_major;
70module_param(nvme_major, int, 0);
71
b3fffdef
KB
72static int nvme_char_major;
73module_param(nvme_char_major, int, 0);
74
58ffacb5
MW
75static int use_threaded_interrupts;
76module_param(use_threaded_interrupts, int, 0);
77
8ffaadf7
JD
78static bool use_cmb_sqes = true;
79module_param(use_cmb_sqes, bool, 0644);
80MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
81
1fa6aead
MW
82static LIST_HEAD(dev_list);
83static struct task_struct *nvme_thread;
9a6b9458 84static struct workqueue_struct *nvme_workq;
b9afca3e 85static wait_queue_head_t nvme_kthread_wait;
1fa6aead 86
b3fffdef
KB
87static struct class *nvme_class;
88
1c63dc66
CH
89struct nvme_dev;
90struct nvme_queue;
d4f6c3ab 91struct nvme_iod;
1c63dc66 92
90667892 93static int __nvme_reset(struct nvme_dev *dev);
4cc06521 94static int nvme_reset(struct nvme_dev *dev);
a0fa9647 95static void nvme_process_cq(struct nvme_queue *nvmeq);
d4f6c3ab 96static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod);
3cf519b5 97static void nvme_dead_ctrl(struct nvme_dev *dev);
d4b4ff8e 98
4d115420
KB
99struct async_cmd_info {
100 struct kthread_work work;
101 struct kthread_worker *worker;
a4aea562 102 struct request *req;
4d115420
KB
103 u32 result;
104 int status;
105 void *ctx;
106};
1fa6aead 107
1c63dc66
CH
108/*
109 * Represents an NVM Express device. Each nvme_dev is a PCI function.
110 */
111struct nvme_dev {
112 struct list_head node;
113 struct nvme_queue **queues;
114 struct blk_mq_tag_set tagset;
115 struct blk_mq_tag_set admin_tagset;
116 u32 __iomem *dbs;
117 struct device *dev;
118 struct dma_pool *prp_page_pool;
119 struct dma_pool *prp_small_pool;
120 unsigned queue_count;
121 unsigned online_queues;
122 unsigned max_qid;
123 int q_depth;
124 u32 db_stride;
125 u32 ctrl_config;
126 struct msix_entry *entry;
127 void __iomem *bar;
128 struct list_head namespaces;
1c63dc66
CH
129 struct device *device;
130 struct work_struct reset_work;
131 struct work_struct probe_work;
132 struct work_struct scan_work;
133 bool subsystem;
134 u32 max_hw_sectors;
135 u32 stripe_size;
136 u32 page_size;
137 void __iomem *cmb;
138 dma_addr_t cmb_dma_addr;
139 u64 cmb_size;
140 u32 cmbsz;
141
142 struct nvme_ctrl ctrl;
143};
144
145static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
146{
147 return container_of(ctrl, struct nvme_dev, ctrl);
148}
149
b60503ba
MW
150/*
151 * An NVM Express queue. Each device has at least two (one for admin
152 * commands and one for I/O commands).
153 */
154struct nvme_queue {
155 struct device *q_dmadev;
091b6092 156 struct nvme_dev *dev;
3193f07b 157 char irqname[24]; /* nvme4294967295-65535\0 */
b60503ba
MW
158 spinlock_t q_lock;
159 struct nvme_command *sq_cmds;
8ffaadf7 160 struct nvme_command __iomem *sq_cmds_io;
b60503ba 161 volatile struct nvme_completion *cqes;
42483228 162 struct blk_mq_tags **tags;
b60503ba
MW
163 dma_addr_t sq_dma_addr;
164 dma_addr_t cq_dma_addr;
b60503ba
MW
165 u32 __iomem *q_db;
166 u16 q_depth;
6222d172 167 s16 cq_vector;
b60503ba
MW
168 u16 sq_head;
169 u16 sq_tail;
170 u16 cq_head;
c30341dc 171 u16 qid;
e9539f47
MW
172 u8 cq_phase;
173 u8 cqe_seen;
4d115420 174 struct async_cmd_info cmdinfo;
b60503ba
MW
175};
176
71bd150c
CH
177/*
178 * The nvme_iod describes the data in an I/O, including the list of PRP
179 * entries. You can't see it in this data structure because C doesn't let
180 * me express that. Use nvme_alloc_iod to ensure there's enough space
181 * allocated to store the PRP list.
182 */
183struct nvme_iod {
184 unsigned long private; /* For the use of the submitter of the I/O */
185 int npages; /* In the PRP list. 0 means small pool in use */
186 int offset; /* Of PRP list */
187 int nents; /* Used in scatterlist */
188 int length; /* Of data, in bytes */
189 dma_addr_t first_dma;
190 struct scatterlist meta_sg[1]; /* metadata requires single contiguous buffer */
191 struct scatterlist sg[0];
192};
193
b60503ba
MW
194/*
195 * Check we didin't inadvertently grow the command struct
196 */
197static inline void _nvme_check_size(void)
198{
199 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
200 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
201 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
202 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
203 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
f8ebf840 204 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
c30341dc 205 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
b60503ba
MW
206 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
207 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
208 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
209 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
6ecec745 210 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
b60503ba
MW
211}
212
edd10d33 213typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
c2f5b650
MW
214 struct nvme_completion *);
215
e85248e5 216struct nvme_cmd_info {
c2f5b650
MW
217 nvme_completion_fn fn;
218 void *ctx;
c30341dc 219 int aborted;
a4aea562 220 struct nvme_queue *nvmeq;
ac3dd5bd 221 struct nvme_iod iod[0];
e85248e5
MW
222};
223
ac3dd5bd
JA
224/*
225 * Max size of iod being embedded in the request payload
226 */
227#define NVME_INT_PAGES 2
228#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->page_size)
fda631ff 229#define NVME_INT_MASK 0x01
ac3dd5bd
JA
230
231/*
232 * Will slightly overestimate the number of pages needed. This is OK
233 * as it only leads to a small amount of wasted memory for the lifetime of
234 * the I/O.
235 */
236static int nvme_npages(unsigned size, struct nvme_dev *dev)
237{
238 unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size);
239 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
240}
241
242static unsigned int nvme_cmd_size(struct nvme_dev *dev)
243{
244 unsigned int ret = sizeof(struct nvme_cmd_info);
245
246 ret += sizeof(struct nvme_iod);
247 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
248 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
249
250 return ret;
251}
252
a4aea562
MB
253static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
254 unsigned int hctx_idx)
e85248e5 255{
a4aea562
MB
256 struct nvme_dev *dev = data;
257 struct nvme_queue *nvmeq = dev->queues[0];
258
42483228
KB
259 WARN_ON(hctx_idx != 0);
260 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
261 WARN_ON(nvmeq->tags);
262
a4aea562 263 hctx->driver_data = nvmeq;
42483228 264 nvmeq->tags = &dev->admin_tagset.tags[0];
a4aea562 265 return 0;
e85248e5
MW
266}
267
4af0e21c
KB
268static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
269{
270 struct nvme_queue *nvmeq = hctx->driver_data;
271
272 nvmeq->tags = NULL;
273}
274
a4aea562
MB
275static int nvme_admin_init_request(void *data, struct request *req,
276 unsigned int hctx_idx, unsigned int rq_idx,
277 unsigned int numa_node)
22404274 278{
a4aea562
MB
279 struct nvme_dev *dev = data;
280 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
281 struct nvme_queue *nvmeq = dev->queues[0];
282
283 BUG_ON(!nvmeq);
284 cmd->nvmeq = nvmeq;
285 return 0;
22404274
KB
286}
287
a4aea562
MB
288static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
289 unsigned int hctx_idx)
b60503ba 290{
a4aea562 291 struct nvme_dev *dev = data;
42483228 292 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
a4aea562 293
42483228
KB
294 if (!nvmeq->tags)
295 nvmeq->tags = &dev->tagset.tags[hctx_idx];
b60503ba 296
42483228 297 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
a4aea562
MB
298 hctx->driver_data = nvmeq;
299 return 0;
b60503ba
MW
300}
301
a4aea562
MB
302static int nvme_init_request(void *data, struct request *req,
303 unsigned int hctx_idx, unsigned int rq_idx,
304 unsigned int numa_node)
b60503ba 305{
a4aea562
MB
306 struct nvme_dev *dev = data;
307 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
308 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
309
310 BUG_ON(!nvmeq);
311 cmd->nvmeq = nvmeq;
312 return 0;
313}
314
315static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
316 nvme_completion_fn handler)
317{
318 cmd->fn = handler;
319 cmd->ctx = ctx;
320 cmd->aborted = 0;
c917dfe5 321 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
b60503ba
MW
322}
323
ac3dd5bd
JA
324static void *iod_get_private(struct nvme_iod *iod)
325{
326 return (void *) (iod->private & ~0x1UL);
327}
328
329/*
330 * If bit 0 is set, the iod is embedded in the request payload.
331 */
332static bool iod_should_kfree(struct nvme_iod *iod)
333{
fda631ff 334 return (iod->private & NVME_INT_MASK) == 0;
ac3dd5bd
JA
335}
336
c2f5b650
MW
337/* Special values must be less than 0x1000 */
338#define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
d2d87034
MW
339#define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
340#define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
341#define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
be7b6275 342
edd10d33 343static void special_completion(struct nvme_queue *nvmeq, void *ctx,
c2f5b650
MW
344 struct nvme_completion *cqe)
345{
346 if (ctx == CMD_CTX_CANCELLED)
347 return;
c2f5b650 348 if (ctx == CMD_CTX_COMPLETED) {
edd10d33 349 dev_warn(nvmeq->q_dmadev,
c2f5b650
MW
350 "completed id %d twice on queue %d\n",
351 cqe->command_id, le16_to_cpup(&cqe->sq_id));
352 return;
353 }
354 if (ctx == CMD_CTX_INVALID) {
edd10d33 355 dev_warn(nvmeq->q_dmadev,
c2f5b650
MW
356 "invalid id %d completed on queue %d\n",
357 cqe->command_id, le16_to_cpup(&cqe->sq_id));
358 return;
359 }
edd10d33 360 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
c2f5b650
MW
361}
362
a4aea562 363static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
b60503ba 364{
c2f5b650 365 void *ctx;
b60503ba 366
859361a2 367 if (fn)
a4aea562
MB
368 *fn = cmd->fn;
369 ctx = cmd->ctx;
370 cmd->fn = special_completion;
371 cmd->ctx = CMD_CTX_CANCELLED;
c2f5b650 372 return ctx;
b60503ba
MW
373}
374
a4aea562
MB
375static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
376 struct nvme_completion *cqe)
3c0cf138 377{
a4aea562
MB
378 u32 result = le32_to_cpup(&cqe->result);
379 u16 status = le16_to_cpup(&cqe->status) >> 1;
380
381 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
1c63dc66 382 ++nvmeq->dev->ctrl.event_limit;
a5768aa8
KB
383 if (status != NVME_SC_SUCCESS)
384 return;
385
386 switch (result & 0xff07) {
387 case NVME_AER_NOTICE_NS_CHANGED:
388 dev_info(nvmeq->q_dmadev, "rescanning\n");
389 schedule_work(&nvmeq->dev->scan_work);
390 default:
391 dev_warn(nvmeq->q_dmadev, "async event result %08x\n", result);
392 }
b60503ba
MW
393}
394
a4aea562
MB
395static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
396 struct nvme_completion *cqe)
5a92e700 397{
a4aea562
MB
398 struct request *req = ctx;
399
400 u16 status = le16_to_cpup(&cqe->status) >> 1;
401 u32 result = le32_to_cpup(&cqe->result);
a51afb54 402
42483228 403 blk_mq_free_request(req);
a51afb54 404
a4aea562 405 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
1c63dc66 406 ++nvmeq->dev->ctrl.abort_limit;
5a92e700
KB
407}
408
a4aea562
MB
409static void async_completion(struct nvme_queue *nvmeq, void *ctx,
410 struct nvme_completion *cqe)
b60503ba 411{
a4aea562
MB
412 struct async_cmd_info *cmdinfo = ctx;
413 cmdinfo->result = le32_to_cpup(&cqe->result);
414 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
415 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
42483228 416 blk_mq_free_request(cmdinfo->req);
b60503ba
MW
417}
418
a4aea562
MB
419static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
420 unsigned int tag)
b60503ba 421{
42483228 422 struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag);
a51afb54 423
a4aea562 424 return blk_mq_rq_to_pdu(req);
4f5099af
KB
425}
426
a4aea562
MB
427/*
428 * Called with local interrupts disabled and the q_lock held. May not sleep.
429 */
430static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
431 nvme_completion_fn *fn)
4f5099af 432{
a4aea562
MB
433 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
434 void *ctx;
435 if (tag >= nvmeq->q_depth) {
436 *fn = special_completion;
437 return CMD_CTX_INVALID;
438 }
439 if (fn)
440 *fn = cmd->fn;
441 ctx = cmd->ctx;
442 cmd->fn = special_completion;
443 cmd->ctx = CMD_CTX_COMPLETED;
444 return ctx;
b60503ba
MW
445}
446
447/**
714a7a22 448 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
b60503ba
MW
449 * @nvmeq: The queue to use
450 * @cmd: The command to send
451 *
452 * Safe to use from interrupt context
453 */
e3f879bf
SB
454static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
455 struct nvme_command *cmd)
b60503ba 456{
a4aea562
MB
457 u16 tail = nvmeq->sq_tail;
458
8ffaadf7
JD
459 if (nvmeq->sq_cmds_io)
460 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
461 else
462 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
463
b60503ba
MW
464 if (++tail == nvmeq->q_depth)
465 tail = 0;
7547881d 466 writel(tail, nvmeq->q_db);
b60503ba 467 nvmeq->sq_tail = tail;
b60503ba
MW
468}
469
e3f879bf 470static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
a4aea562
MB
471{
472 unsigned long flags;
a4aea562 473 spin_lock_irqsave(&nvmeq->q_lock, flags);
e3f879bf 474 __nvme_submit_cmd(nvmeq, cmd);
a4aea562 475 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
a4aea562
MB
476}
477
eca18b23 478static __le64 **iod_list(struct nvme_iod *iod)
e025344c 479{
eca18b23 480 return ((void *)iod) + iod->offset;
e025344c
SMM
481}
482
ac3dd5bd
JA
483static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
484 unsigned nseg, unsigned long private)
eca18b23 485{
ac3dd5bd
JA
486 iod->private = private;
487 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
488 iod->npages = -1;
489 iod->length = nbytes;
490 iod->nents = 0;
eca18b23 491}
b60503ba 492
eca18b23 493static struct nvme_iod *
ac3dd5bd
JA
494__nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
495 unsigned long priv, gfp_t gfp)
b60503ba 496{
eca18b23 497 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
ac3dd5bd 498 sizeof(__le64 *) * nvme_npages(bytes, dev) +
eca18b23
MW
499 sizeof(struct scatterlist) * nseg, gfp);
500
ac3dd5bd
JA
501 if (iod)
502 iod_init(iod, bytes, nseg, priv);
eca18b23
MW
503
504 return iod;
b60503ba
MW
505}
506
ac3dd5bd
JA
507static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
508 gfp_t gfp)
509{
510 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
511 sizeof(struct nvme_dsm_range);
ac3dd5bd
JA
512 struct nvme_iod *iod;
513
514 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
515 size <= NVME_INT_BYTES(dev)) {
516 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
517
518 iod = cmd->iod;
ac3dd5bd 519 iod_init(iod, size, rq->nr_phys_segments,
fda631ff 520 (unsigned long) rq | NVME_INT_MASK);
ac3dd5bd
JA
521 return iod;
522 }
523
524 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
525 (unsigned long) rq, gfp);
526}
527
d29ec824 528static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
b60503ba 529{
1d090624 530 const int last_prp = dev->page_size / 8 - 1;
eca18b23
MW
531 int i;
532 __le64 **list = iod_list(iod);
533 dma_addr_t prp_dma = iod->first_dma;
534
535 if (iod->npages == 0)
536 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
537 for (i = 0; i < iod->npages; i++) {
538 __le64 *prp_list = list[i];
539 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
540 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
541 prp_dma = next_prp_dma;
542 }
ac3dd5bd
JA
543
544 if (iod_should_kfree(iod))
545 kfree(iod);
b60503ba
MW
546}
547
52b68d7e 548#ifdef CONFIG_BLK_DEV_INTEGRITY
e1e5e564
KB
549static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
550{
551 if (be32_to_cpu(pi->ref_tag) == v)
552 pi->ref_tag = cpu_to_be32(p);
553}
554
555static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
556{
557 if (be32_to_cpu(pi->ref_tag) == p)
558 pi->ref_tag = cpu_to_be32(v);
559}
560
561/**
562 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
563 *
564 * The virtual start sector is the one that was originally submitted by the
565 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
566 * start sector may be different. Remap protection information to match the
567 * physical LBA on writes, and back to the original seed on reads.
568 *
569 * Type 0 and 3 do not have a ref tag, so no remapping required.
570 */
571static void nvme_dif_remap(struct request *req,
572 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
573{
574 struct nvme_ns *ns = req->rq_disk->private_data;
575 struct bio_integrity_payload *bip;
576 struct t10_pi_tuple *pi;
577 void *p, *pmap;
578 u32 i, nlb, ts, phys, virt;
579
580 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
581 return;
582
583 bip = bio_integrity(req->bio);
584 if (!bip)
585 return;
586
587 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
e1e5e564
KB
588
589 p = pmap;
590 virt = bip_get_seed(bip);
591 phys = nvme_block_nr(ns, blk_rq_pos(req));
592 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
ac6fc48c 593 ts = ns->disk->queue->integrity.tuple_size;
e1e5e564
KB
594
595 for (i = 0; i < nlb; i++, virt++, phys++) {
596 pi = (struct t10_pi_tuple *)p;
597 dif_swap(phys, virt, pi);
598 p += ts;
599 }
600 kunmap_atomic(pmap);
601}
52b68d7e
KB
602#else /* CONFIG_BLK_DEV_INTEGRITY */
603static void nvme_dif_remap(struct request *req,
604 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
605{
606}
607static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
608{
609}
610static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
611{
612}
52b68d7e
KB
613#endif
614
a4aea562 615static void req_completion(struct nvme_queue *nvmeq, void *ctx,
b60503ba
MW
616 struct nvme_completion *cqe)
617{
eca18b23 618 struct nvme_iod *iod = ctx;
ac3dd5bd 619 struct request *req = iod_get_private(iod);
a4aea562 620 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
b60503ba 621 u16 status = le16_to_cpup(&cqe->status) >> 1;
81c04b94 622 int error = 0;
b60503ba 623
edd10d33 624 if (unlikely(status)) {
a4aea562
MB
625 if (!(status & NVME_SC_DNR || blk_noretry_request(req))
626 && (jiffies - req->start_time) < req->timeout) {
c9d3bf88
KB
627 unsigned long flags;
628
d4f6c3ab
CH
629 nvme_unmap_data(nvmeq->dev, iod);
630
a4aea562 631 blk_mq_requeue_request(req);
c9d3bf88
KB
632 spin_lock_irqsave(req->q->queue_lock, flags);
633 if (!blk_queue_stopped(req->q))
634 blk_mq_kick_requeue_list(req->q);
635 spin_unlock_irqrestore(req->q->queue_lock, flags);
d4f6c3ab 636 return;
edd10d33 637 }
f4829a9b 638
d29ec824 639 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
17188bb4 640 if (cmd_rq->ctx == CMD_CTX_CANCELLED)
81c04b94
CH
641 error = -EINTR;
642 else
643 error = status;
d29ec824 644 } else {
81c04b94 645 error = nvme_error_status(status);
d29ec824 646 }
f4829a9b
CH
647 }
648
a0a931d6
KB
649 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
650 u32 result = le32_to_cpup(&cqe->result);
651 req->special = (void *)(uintptr_t)result;
652 }
a4aea562
MB
653
654 if (cmd_rq->aborted)
e75ec752 655 dev_warn(nvmeq->dev->dev,
a4aea562 656 "completing aborted command with status:%04x\n",
81c04b94 657 error);
a4aea562 658
d4f6c3ab
CH
659 nvme_unmap_data(nvmeq->dev, iod);
660 blk_mq_complete_request(req, error);
b60503ba
MW
661}
662
69d2b571
CH
663static bool nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
664 int total_len)
ff22b54f 665{
99802a7a 666 struct dma_pool *pool;
eca18b23
MW
667 int length = total_len;
668 struct scatterlist *sg = iod->sg;
ff22b54f
MW
669 int dma_len = sg_dma_len(sg);
670 u64 dma_addr = sg_dma_address(sg);
f137e0f1
MI
671 u32 page_size = dev->page_size;
672 int offset = dma_addr & (page_size - 1);
e025344c 673 __le64 *prp_list;
eca18b23 674 __le64 **list = iod_list(iod);
e025344c 675 dma_addr_t prp_dma;
eca18b23 676 int nprps, i;
ff22b54f 677
1d090624 678 length -= (page_size - offset);
ff22b54f 679 if (length <= 0)
69d2b571 680 return true;
ff22b54f 681
1d090624 682 dma_len -= (page_size - offset);
ff22b54f 683 if (dma_len) {
1d090624 684 dma_addr += (page_size - offset);
ff22b54f
MW
685 } else {
686 sg = sg_next(sg);
687 dma_addr = sg_dma_address(sg);
688 dma_len = sg_dma_len(sg);
689 }
690
1d090624 691 if (length <= page_size) {
edd10d33 692 iod->first_dma = dma_addr;
69d2b571 693 return true;
e025344c
SMM
694 }
695
1d090624 696 nprps = DIV_ROUND_UP(length, page_size);
99802a7a
MW
697 if (nprps <= (256 / 8)) {
698 pool = dev->prp_small_pool;
eca18b23 699 iod->npages = 0;
99802a7a
MW
700 } else {
701 pool = dev->prp_page_pool;
eca18b23 702 iod->npages = 1;
99802a7a
MW
703 }
704
69d2b571 705 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
b77954cb 706 if (!prp_list) {
edd10d33 707 iod->first_dma = dma_addr;
eca18b23 708 iod->npages = -1;
69d2b571 709 return false;
b77954cb 710 }
eca18b23
MW
711 list[0] = prp_list;
712 iod->first_dma = prp_dma;
e025344c
SMM
713 i = 0;
714 for (;;) {
1d090624 715 if (i == page_size >> 3) {
e025344c 716 __le64 *old_prp_list = prp_list;
69d2b571 717 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
eca18b23 718 if (!prp_list)
69d2b571 719 return false;
eca18b23 720 list[iod->npages++] = prp_list;
7523d834
MW
721 prp_list[0] = old_prp_list[i - 1];
722 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
723 i = 1;
e025344c
SMM
724 }
725 prp_list[i++] = cpu_to_le64(dma_addr);
1d090624
KB
726 dma_len -= page_size;
727 dma_addr += page_size;
728 length -= page_size;
e025344c
SMM
729 if (length <= 0)
730 break;
731 if (dma_len > 0)
732 continue;
733 BUG_ON(dma_len < 0);
734 sg = sg_next(sg);
735 dma_addr = sg_dma_address(sg);
736 dma_len = sg_dma_len(sg);
ff22b54f
MW
737 }
738
69d2b571 739 return true;
ff22b54f
MW
740}
741
ba1ca37e
CH
742static int nvme_map_data(struct nvme_dev *dev, struct nvme_iod *iod,
743 struct nvme_command *cmnd)
d29ec824 744{
ba1ca37e
CH
745 struct request *req = iod_get_private(iod);
746 struct request_queue *q = req->q;
747 enum dma_data_direction dma_dir = rq_data_dir(req) ?
748 DMA_TO_DEVICE : DMA_FROM_DEVICE;
749 int ret = BLK_MQ_RQ_QUEUE_ERROR;
750
751 sg_init_table(iod->sg, req->nr_phys_segments);
752 iod->nents = blk_rq_map_sg(q, req, iod->sg);
753 if (!iod->nents)
754 goto out;
755
756 ret = BLK_MQ_RQ_QUEUE_BUSY;
757 if (!dma_map_sg(dev->dev, iod->sg, iod->nents, dma_dir))
758 goto out;
759
760 if (!nvme_setup_prps(dev, iod, blk_rq_bytes(req)))
761 goto out_unmap;
762
763 ret = BLK_MQ_RQ_QUEUE_ERROR;
764 if (blk_integrity_rq(req)) {
765 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
766 goto out_unmap;
767
768 sg_init_table(iod->meta_sg, 1);
769 if (blk_rq_map_integrity_sg(q, req->bio, iod->meta_sg) != 1)
770 goto out_unmap;
d29ec824 771
ba1ca37e
CH
772 if (rq_data_dir(req))
773 nvme_dif_remap(req, nvme_dif_prep);
774
775 if (!dma_map_sg(dev->dev, iod->meta_sg, 1, dma_dir))
776 goto out_unmap;
d29ec824
CH
777 }
778
ba1ca37e
CH
779 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
780 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
781 if (blk_integrity_rq(req))
782 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg));
783 return BLK_MQ_RQ_QUEUE_OK;
784
785out_unmap:
786 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
787out:
788 return ret;
d29ec824
CH
789}
790
d4f6c3ab
CH
791static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod)
792{
793 struct request *req = iod_get_private(iod);
794 enum dma_data_direction dma_dir = rq_data_dir(req) ?
795 DMA_TO_DEVICE : DMA_FROM_DEVICE;
796
797 if (iod->nents) {
798 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
799 if (blk_integrity_rq(req)) {
800 if (!rq_data_dir(req))
801 nvme_dif_remap(req, nvme_dif_complete);
802 dma_unmap_sg(dev->dev, iod->meta_sg, 1, dma_dir);
803 }
804 }
805
806 nvme_free_iod(dev, iod);
807}
808
a4aea562
MB
809/*
810 * We reuse the small pool to allocate the 16-byte range here as it is not
811 * worth having a special pool for these or additional cases to handle freeing
812 * the iod.
813 */
ba1ca37e
CH
814static int nvme_setup_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
815 struct nvme_iod *iod, struct nvme_command *cmnd)
0e5e4f0e 816{
ba1ca37e
CH
817 struct request *req = iod_get_private(iod);
818 struct nvme_dsm_range *range;
819
820 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
821 &iod->first_dma);
822 if (!range)
823 return BLK_MQ_RQ_QUEUE_BUSY;
824 iod_list(iod)[0] = (__le64 *)range;
825 iod->npages = 0;
0e5e4f0e 826
0e5e4f0e 827 range->cattr = cpu_to_le32(0);
a4aea562
MB
828 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
829 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
0e5e4f0e 830
ba1ca37e
CH
831 memset(cmnd, 0, sizeof(*cmnd));
832 cmnd->dsm.opcode = nvme_cmd_dsm;
833 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
834 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
835 cmnd->dsm.nr = 0;
836 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
837 return BLK_MQ_RQ_QUEUE_OK;
0e5e4f0e
KB
838}
839
d29ec824
CH
840/*
841 * NOTE: ns is NULL when called on the admin queue.
842 */
a4aea562
MB
843static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
844 const struct blk_mq_queue_data *bd)
edd10d33 845{
a4aea562
MB
846 struct nvme_ns *ns = hctx->queue->queuedata;
847 struct nvme_queue *nvmeq = hctx->driver_data;
d29ec824 848 struct nvme_dev *dev = nvmeq->dev;
a4aea562
MB
849 struct request *req = bd->rq;
850 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
edd10d33 851 struct nvme_iod *iod;
ba1ca37e
CH
852 struct nvme_command cmnd;
853 int ret = BLK_MQ_RQ_QUEUE_OK;
edd10d33 854
e1e5e564
KB
855 /*
856 * If formated with metadata, require the block layer provide a buffer
857 * unless this namespace is formated such that the metadata can be
858 * stripped/generated by the controller with PRACT=1.
859 */
d29ec824 860 if (ns && ns->ms && !blk_integrity_rq(req)) {
71feb364
KB
861 if (!(ns->pi_type && ns->ms == 8) &&
862 req->cmd_type != REQ_TYPE_DRV_PRIV) {
f4829a9b 863 blk_mq_complete_request(req, -EFAULT);
e1e5e564
KB
864 return BLK_MQ_RQ_QUEUE_OK;
865 }
866 }
867
d29ec824 868 iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
edd10d33 869 if (!iod)
fe54303e 870 return BLK_MQ_RQ_QUEUE_BUSY;
a4aea562 871
a4aea562 872 if (req->cmd_flags & REQ_DISCARD) {
ba1ca37e
CH
873 ret = nvme_setup_discard(nvmeq, ns, iod, &cmnd);
874 } else {
875 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
876 memcpy(&cmnd, req->cmd, sizeof(cmnd));
877 else if (req->cmd_flags & REQ_FLUSH)
878 nvme_setup_flush(ns, &cmnd);
879 else
880 nvme_setup_rw(ns, req, &cmnd);
a4aea562 881
ba1ca37e
CH
882 if (req->nr_phys_segments)
883 ret = nvme_map_data(dev, iod, &cmnd);
edd10d33 884 }
1974b1ae 885
ba1ca37e
CH
886 if (ret)
887 goto out;
888
889 cmnd.common.command_id = req->tag;
9af8785a 890 nvme_set_info(cmd, iod, req_completion);
a4aea562 891
ba1ca37e
CH
892 spin_lock_irq(&nvmeq->q_lock);
893 __nvme_submit_cmd(nvmeq, &cmnd);
a4aea562
MB
894 nvme_process_cq(nvmeq);
895 spin_unlock_irq(&nvmeq->q_lock);
896 return BLK_MQ_RQ_QUEUE_OK;
ba1ca37e 897out:
d29ec824 898 nvme_free_iod(dev, iod);
ba1ca37e 899 return ret;
b60503ba
MW
900}
901
a0fa9647 902static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
b60503ba 903{
82123460 904 u16 head, phase;
b60503ba 905
b60503ba 906 head = nvmeq->cq_head;
82123460 907 phase = nvmeq->cq_phase;
b60503ba
MW
908
909 for (;;) {
c2f5b650
MW
910 void *ctx;
911 nvme_completion_fn fn;
b60503ba 912 struct nvme_completion cqe = nvmeq->cqes[head];
82123460 913 if ((le16_to_cpu(cqe.status) & 1) != phase)
b60503ba
MW
914 break;
915 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
916 if (++head == nvmeq->q_depth) {
917 head = 0;
82123460 918 phase = !phase;
b60503ba 919 }
a0fa9647
JA
920 if (tag && *tag == cqe.command_id)
921 *tag = -1;
a4aea562 922 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
edd10d33 923 fn(nvmeq, ctx, &cqe);
b60503ba
MW
924 }
925
926 /* If the controller ignores the cq head doorbell and continuously
927 * writes to the queue, it is theoretically possible to wrap around
928 * the queue twice and mistakenly return IRQ_NONE. Linux only
929 * requires that 0.1% of your interrupts are handled, so this isn't
930 * a big problem.
931 */
82123460 932 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
a0fa9647 933 return;
b60503ba 934
604e8c8d
KB
935 if (likely(nvmeq->cq_vector >= 0))
936 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
b60503ba 937 nvmeq->cq_head = head;
82123460 938 nvmeq->cq_phase = phase;
b60503ba 939
e9539f47 940 nvmeq->cqe_seen = 1;
a0fa9647
JA
941}
942
943static void nvme_process_cq(struct nvme_queue *nvmeq)
944{
945 __nvme_process_cq(nvmeq, NULL);
b60503ba
MW
946}
947
948static irqreturn_t nvme_irq(int irq, void *data)
58ffacb5
MW
949{
950 irqreturn_t result;
951 struct nvme_queue *nvmeq = data;
952 spin_lock(&nvmeq->q_lock);
e9539f47
MW
953 nvme_process_cq(nvmeq);
954 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
955 nvmeq->cqe_seen = 0;
58ffacb5
MW
956 spin_unlock(&nvmeq->q_lock);
957 return result;
958}
959
960static irqreturn_t nvme_irq_check(int irq, void *data)
961{
962 struct nvme_queue *nvmeq = data;
963 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
964 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
965 return IRQ_NONE;
966 return IRQ_WAKE_THREAD;
967}
968
a0fa9647
JA
969static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
970{
971 struct nvme_queue *nvmeq = hctx->driver_data;
972
973 if ((le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
974 nvmeq->cq_phase) {
975 spin_lock_irq(&nvmeq->q_lock);
976 __nvme_process_cq(nvmeq, &tag);
977 spin_unlock_irq(&nvmeq->q_lock);
978
979 if (tag == -1)
980 return 1;
981 }
982
983 return 0;
984}
985
a4aea562
MB
986static int nvme_submit_async_admin_req(struct nvme_dev *dev)
987{
988 struct nvme_queue *nvmeq = dev->queues[0];
989 struct nvme_command c;
990 struct nvme_cmd_info *cmd_info;
991 struct request *req;
992
1c63dc66 993 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
6f3b0e8b 994 BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED);
9f173b33
DC
995 if (IS_ERR(req))
996 return PTR_ERR(req);
a4aea562 997
c917dfe5 998 req->cmd_flags |= REQ_NO_TIMEOUT;
a4aea562 999 cmd_info = blk_mq_rq_to_pdu(req);
1efccc9d 1000 nvme_set_info(cmd_info, NULL, async_req_completion);
a4aea562
MB
1001
1002 memset(&c, 0, sizeof(c));
1003 c.common.opcode = nvme_admin_async_event;
1004 c.common.command_id = req->tag;
1005
42483228 1006 blk_mq_free_request(req);
e3f879bf
SB
1007 __nvme_submit_cmd(nvmeq, &c);
1008 return 0;
a4aea562
MB
1009}
1010
1011static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
4d115420
KB
1012 struct nvme_command *cmd,
1013 struct async_cmd_info *cmdinfo, unsigned timeout)
1014{
a4aea562
MB
1015 struct nvme_queue *nvmeq = dev->queues[0];
1016 struct request *req;
1017 struct nvme_cmd_info *cmd_rq;
4d115420 1018
1c63dc66 1019 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE, 0);
9f173b33
DC
1020 if (IS_ERR(req))
1021 return PTR_ERR(req);
a4aea562
MB
1022
1023 req->timeout = timeout;
1024 cmd_rq = blk_mq_rq_to_pdu(req);
1025 cmdinfo->req = req;
1026 nvme_set_info(cmd_rq, cmdinfo, async_completion);
4d115420 1027 cmdinfo->status = -EINTR;
a4aea562
MB
1028
1029 cmd->common.command_id = req->tag;
1030
e3f879bf
SB
1031 nvme_submit_cmd(nvmeq, cmd);
1032 return 0;
4d115420
KB
1033}
1034
b60503ba
MW
1035static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1036{
b60503ba
MW
1037 struct nvme_command c;
1038
1039 memset(&c, 0, sizeof(c));
1040 c.delete_queue.opcode = opcode;
1041 c.delete_queue.qid = cpu_to_le16(id);
1042
1c63dc66 1043 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
b60503ba
MW
1044}
1045
1046static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1047 struct nvme_queue *nvmeq)
1048{
b60503ba
MW
1049 struct nvme_command c;
1050 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1051
d29ec824
CH
1052 /*
1053 * Note: we (ab)use the fact the the prp fields survive if no data
1054 * is attached to the request.
1055 */
b60503ba
MW
1056 memset(&c, 0, sizeof(c));
1057 c.create_cq.opcode = nvme_admin_create_cq;
1058 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1059 c.create_cq.cqid = cpu_to_le16(qid);
1060 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1061 c.create_cq.cq_flags = cpu_to_le16(flags);
1062 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1063
1c63dc66 1064 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
b60503ba
MW
1065}
1066
1067static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1068 struct nvme_queue *nvmeq)
1069{
b60503ba
MW
1070 struct nvme_command c;
1071 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1072
d29ec824
CH
1073 /*
1074 * Note: we (ab)use the fact the the prp fields survive if no data
1075 * is attached to the request.
1076 */
b60503ba
MW
1077 memset(&c, 0, sizeof(c));
1078 c.create_sq.opcode = nvme_admin_create_sq;
1079 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1080 c.create_sq.sqid = cpu_to_le16(qid);
1081 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1082 c.create_sq.sq_flags = cpu_to_le16(flags);
1083 c.create_sq.cqid = cpu_to_le16(qid);
1084
1c63dc66 1085 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
b60503ba
MW
1086}
1087
1088static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1089{
1090 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1091}
1092
1093static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1094{
1095 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1096}
1097
c30341dc 1098/**
a4aea562 1099 * nvme_abort_req - Attempt aborting a request
c30341dc
KB
1100 *
1101 * Schedule controller reset if the command was already aborted once before and
1102 * still hasn't been returned to the driver, or if this is the admin queue.
1103 */
a4aea562 1104static void nvme_abort_req(struct request *req)
c30341dc 1105{
a4aea562
MB
1106 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1107 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
c30341dc 1108 struct nvme_dev *dev = nvmeq->dev;
a4aea562
MB
1109 struct request *abort_req;
1110 struct nvme_cmd_info *abort_cmd;
1111 struct nvme_command cmd;
c30341dc 1112
a4aea562 1113 if (!nvmeq->qid || cmd_rq->aborted) {
90667892
CH
1114 spin_lock(&dev_list_lock);
1115 if (!__nvme_reset(dev)) {
1116 dev_warn(dev->dev,
1117 "I/O %d QID %d timeout, reset controller\n",
1118 req->tag, nvmeq->qid);
1119 }
1120 spin_unlock(&dev_list_lock);
c30341dc
KB
1121 return;
1122 }
1123
1c63dc66 1124 if (!dev->ctrl.abort_limit)
c30341dc
KB
1125 return;
1126
1c63dc66 1127 abort_req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
6f3b0e8b 1128 BLK_MQ_REQ_NOWAIT);
9f173b33 1129 if (IS_ERR(abort_req))
c30341dc
KB
1130 return;
1131
a4aea562
MB
1132 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1133 nvme_set_info(abort_cmd, abort_req, abort_completion);
1134
c30341dc
KB
1135 memset(&cmd, 0, sizeof(cmd));
1136 cmd.abort.opcode = nvme_admin_abort_cmd;
a4aea562 1137 cmd.abort.cid = req->tag;
c30341dc 1138 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
a4aea562 1139 cmd.abort.command_id = abort_req->tag;
c30341dc 1140
1c63dc66 1141 --dev->ctrl.abort_limit;
a4aea562 1142 cmd_rq->aborted = 1;
c30341dc 1143
a4aea562 1144 dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag,
c30341dc 1145 nvmeq->qid);
e3f879bf 1146 nvme_submit_cmd(dev->queues[0], &cmd);
c30341dc
KB
1147}
1148
42483228 1149static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
a09115b2 1150{
a4aea562
MB
1151 struct nvme_queue *nvmeq = data;
1152 void *ctx;
1153 nvme_completion_fn fn;
1154 struct nvme_cmd_info *cmd;
cef6a948
KB
1155 struct nvme_completion cqe;
1156
1157 if (!blk_mq_request_started(req))
1158 return;
a09115b2 1159
a4aea562 1160 cmd = blk_mq_rq_to_pdu(req);
a09115b2 1161
a4aea562
MB
1162 if (cmd->ctx == CMD_CTX_CANCELLED)
1163 return;
1164
cef6a948
KB
1165 if (blk_queue_dying(req->q))
1166 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1167 else
1168 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1169
1170
a4aea562
MB
1171 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1172 req->tag, nvmeq->qid);
1173 ctx = cancel_cmd_info(cmd, &fn);
1174 fn(nvmeq, ctx, &cqe);
a09115b2
MW
1175}
1176
a4aea562 1177static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
9e866774 1178{
a4aea562
MB
1179 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
1180 struct nvme_queue *nvmeq = cmd->nvmeq;
1181
1182 dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag,
1183 nvmeq->qid);
7a509a6b 1184 spin_lock_irq(&nvmeq->q_lock);
07836e65 1185 nvme_abort_req(req);
7a509a6b 1186 spin_unlock_irq(&nvmeq->q_lock);
a4aea562 1187
07836e65
KB
1188 /*
1189 * The aborted req will be completed on receiving the abort req.
1190 * We enable the timer again. If hit twice, it'll cause a device reset,
1191 * as the device then is in a faulty state.
1192 */
1193 return BLK_EH_RESET_TIMER;
a4aea562 1194}
22404274 1195
a4aea562
MB
1196static void nvme_free_queue(struct nvme_queue *nvmeq)
1197{
9e866774
MW
1198 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1199 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
8ffaadf7
JD
1200 if (nvmeq->sq_cmds)
1201 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
9e866774
MW
1202 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1203 kfree(nvmeq);
1204}
1205
a1a5ef99 1206static void nvme_free_queues(struct nvme_dev *dev, int lowest)
22404274
KB
1207{
1208 int i;
1209
a1a5ef99 1210 for (i = dev->queue_count - 1; i >= lowest; i--) {
a4aea562 1211 struct nvme_queue *nvmeq = dev->queues[i];
22404274 1212 dev->queue_count--;
a4aea562 1213 dev->queues[i] = NULL;
f435c282 1214 nvme_free_queue(nvmeq);
121c7ad4 1215 }
22404274
KB
1216}
1217
4d115420
KB
1218/**
1219 * nvme_suspend_queue - put queue into suspended state
1220 * @nvmeq - queue to suspend
4d115420
KB
1221 */
1222static int nvme_suspend_queue(struct nvme_queue *nvmeq)
b60503ba 1223{
2b25d981 1224 int vector;
b60503ba 1225
a09115b2 1226 spin_lock_irq(&nvmeq->q_lock);
2b25d981
KB
1227 if (nvmeq->cq_vector == -1) {
1228 spin_unlock_irq(&nvmeq->q_lock);
1229 return 1;
1230 }
1231 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
42f61420 1232 nvmeq->dev->online_queues--;
2b25d981 1233 nvmeq->cq_vector = -1;
a09115b2
MW
1234 spin_unlock_irq(&nvmeq->q_lock);
1235
1c63dc66
CH
1236 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
1237 blk_mq_freeze_queue_start(nvmeq->dev->ctrl.admin_q);
6df3dbc8 1238
aba2080f
MW
1239 irq_set_affinity_hint(vector, NULL);
1240 free_irq(vector, nvmeq);
b60503ba 1241
4d115420
KB
1242 return 0;
1243}
b60503ba 1244
4d115420
KB
1245static void nvme_clear_queue(struct nvme_queue *nvmeq)
1246{
22404274 1247 spin_lock_irq(&nvmeq->q_lock);
42483228
KB
1248 if (nvmeq->tags && *nvmeq->tags)
1249 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
22404274 1250 spin_unlock_irq(&nvmeq->q_lock);
b60503ba
MW
1251}
1252
4d115420
KB
1253static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1254{
a4aea562 1255 struct nvme_queue *nvmeq = dev->queues[qid];
4d115420
KB
1256
1257 if (!nvmeq)
1258 return;
1259 if (nvme_suspend_queue(nvmeq))
1260 return;
1261
0e53d180
KB
1262 /* Don't tell the adapter to delete the admin queue.
1263 * Don't tell a removed adapter to delete IO queues. */
7a67cbea 1264 if (qid && readl(dev->bar + NVME_REG_CSTS) != -1) {
b60503ba
MW
1265 adapter_delete_sq(dev, qid);
1266 adapter_delete_cq(dev, qid);
1267 }
07836e65
KB
1268
1269 spin_lock_irq(&nvmeq->q_lock);
1270 nvme_process_cq(nvmeq);
1271 spin_unlock_irq(&nvmeq->q_lock);
b60503ba
MW
1272}
1273
8ffaadf7
JD
1274static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1275 int entry_size)
1276{
1277 int q_depth = dev->q_depth;
1278 unsigned q_size_aligned = roundup(q_depth * entry_size, dev->page_size);
1279
1280 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
c45f5c99
JD
1281 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
1282 mem_per_q = round_down(mem_per_q, dev->page_size);
1283 q_depth = div_u64(mem_per_q, entry_size);
8ffaadf7
JD
1284
1285 /*
1286 * Ensure the reduced q_depth is above some threshold where it
1287 * would be better to map queues in system memory with the
1288 * original depth
1289 */
1290 if (q_depth < 64)
1291 return -ENOMEM;
1292 }
1293
1294 return q_depth;
1295}
1296
1297static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1298 int qid, int depth)
1299{
1300 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
1301 unsigned offset = (qid - 1) *
1302 roundup(SQ_SIZE(depth), dev->page_size);
1303 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1304 nvmeq->sq_cmds_io = dev->cmb + offset;
1305 } else {
1306 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1307 &nvmeq->sq_dma_addr, GFP_KERNEL);
1308 if (!nvmeq->sq_cmds)
1309 return -ENOMEM;
1310 }
1311
1312 return 0;
1313}
1314
b60503ba 1315static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
2b25d981 1316 int depth)
b60503ba 1317{
a4aea562 1318 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
b60503ba
MW
1319 if (!nvmeq)
1320 return NULL;
1321
e75ec752 1322 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
4d51abf9 1323 &nvmeq->cq_dma_addr, GFP_KERNEL);
b60503ba
MW
1324 if (!nvmeq->cqes)
1325 goto free_nvmeq;
b60503ba 1326
8ffaadf7 1327 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
b60503ba
MW
1328 goto free_cqdma;
1329
e75ec752 1330 nvmeq->q_dmadev = dev->dev;
091b6092 1331 nvmeq->dev = dev;
3193f07b 1332 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1c63dc66 1333 dev->ctrl.instance, qid);
b60503ba
MW
1334 spin_lock_init(&nvmeq->q_lock);
1335 nvmeq->cq_head = 0;
82123460 1336 nvmeq->cq_phase = 1;
b80d5ccc 1337 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
b60503ba 1338 nvmeq->q_depth = depth;
c30341dc 1339 nvmeq->qid = qid;
758dd7fd 1340 nvmeq->cq_vector = -1;
a4aea562 1341 dev->queues[qid] = nvmeq;
b60503ba 1342
36a7e993
JD
1343 /* make sure queue descriptor is set before queue count, for kthread */
1344 mb();
1345 dev->queue_count++;
1346
b60503ba
MW
1347 return nvmeq;
1348
1349 free_cqdma:
e75ec752 1350 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
b60503ba
MW
1351 nvmeq->cq_dma_addr);
1352 free_nvmeq:
1353 kfree(nvmeq);
1354 return NULL;
1355}
1356
3001082c
MW
1357static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1358 const char *name)
1359{
58ffacb5
MW
1360 if (use_threaded_interrupts)
1361 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
481e5bad 1362 nvme_irq_check, nvme_irq, IRQF_SHARED,
58ffacb5 1363 name, nvmeq);
3001082c 1364 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
481e5bad 1365 IRQF_SHARED, name, nvmeq);
3001082c
MW
1366}
1367
22404274 1368static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
b60503ba 1369{
22404274 1370 struct nvme_dev *dev = nvmeq->dev;
b60503ba 1371
7be50e93 1372 spin_lock_irq(&nvmeq->q_lock);
22404274
KB
1373 nvmeq->sq_tail = 0;
1374 nvmeq->cq_head = 0;
1375 nvmeq->cq_phase = 1;
b80d5ccc 1376 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
22404274 1377 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
42f61420 1378 dev->online_queues++;
7be50e93 1379 spin_unlock_irq(&nvmeq->q_lock);
22404274
KB
1380}
1381
1382static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1383{
1384 struct nvme_dev *dev = nvmeq->dev;
1385 int result;
3f85d50b 1386
2b25d981 1387 nvmeq->cq_vector = qid - 1;
b60503ba
MW
1388 result = adapter_alloc_cq(dev, qid, nvmeq);
1389 if (result < 0)
22404274 1390 return result;
b60503ba
MW
1391
1392 result = adapter_alloc_sq(dev, qid, nvmeq);
1393 if (result < 0)
1394 goto release_cq;
1395
3193f07b 1396 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
b60503ba
MW
1397 if (result < 0)
1398 goto release_sq;
1399
22404274 1400 nvme_init_queue(nvmeq, qid);
22404274 1401 return result;
b60503ba
MW
1402
1403 release_sq:
1404 adapter_delete_sq(dev, qid);
1405 release_cq:
1406 adapter_delete_cq(dev, qid);
22404274 1407 return result;
b60503ba
MW
1408}
1409
ba47e386
MW
1410static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
1411{
1412 unsigned long timeout;
1413 u32 bit = enabled ? NVME_CSTS_RDY : 0;
1414
1415 timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1416
7a67cbea 1417 while ((readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_RDY) != bit) {
ba47e386
MW
1418 msleep(100);
1419 if (fatal_signal_pending(current))
1420 return -EINTR;
1421 if (time_after(jiffies, timeout)) {
e75ec752 1422 dev_err(dev->dev,
27e8166c
MW
1423 "Device not ready; aborting %s\n", enabled ?
1424 "initialisation" : "reset");
ba47e386
MW
1425 return -ENODEV;
1426 }
1427 }
1428
1429 return 0;
1430}
1431
1432/*
1433 * If the device has been passed off to us in an enabled state, just clear
1434 * the enabled bit. The spec says we should set the 'shutdown notification
1435 * bits', but doing so may cause the device to complete commands to the
1436 * admin queue ... and we don't know what memory that might be pointing at!
1437 */
1438static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
1439{
01079522
DM
1440 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1441 dev->ctrl_config &= ~NVME_CC_ENABLE;
7a67cbea 1442 writel(dev->ctrl_config, dev->bar + NVME_REG_CC);
44af146a 1443
ba47e386
MW
1444 return nvme_wait_ready(dev, cap, false);
1445}
1446
1447static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
1448{
1b2eb374
CH
1449 /*
1450 * Default to a 4K page size, with the intention to update this
1451 * path in the future to accomodate architectures with differing
1452 * kernel and IO page sizes.
1453 */
1454 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1455
1456 if (page_shift < dev_page_min) {
1457 dev_err(dev->dev,
1458 "Minimum device page size %u too large for host (%u)\n",
1459 1 << dev_page_min, 1 << page_shift);
1460 return -ENODEV;
1461 }
1462
1463 dev->page_size = 1 << page_shift;
1464
1465 dev->ctrl_config = NVME_CC_CSS_NVM;
1466 dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
1467 dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
1468 dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
01079522 1469 dev->ctrl_config |= NVME_CC_ENABLE;
1b2eb374 1470
7a67cbea 1471 writel(dev->ctrl_config, dev->bar + NVME_REG_CC);
01079522 1472
ba47e386
MW
1473 return nvme_wait_ready(dev, cap, true);
1474}
1475
1894d8f1
KB
1476static int nvme_shutdown_ctrl(struct nvme_dev *dev)
1477{
1478 unsigned long timeout;
1894d8f1 1479
01079522
DM
1480 dev->ctrl_config &= ~NVME_CC_SHN_MASK;
1481 dev->ctrl_config |= NVME_CC_SHN_NORMAL;
1482
7a67cbea 1483 writel(dev->ctrl_config, dev->bar + NVME_REG_CC);
1894d8f1 1484
2484f407 1485 timeout = SHUTDOWN_TIMEOUT + jiffies;
7a67cbea 1486 while ((readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_SHST_MASK) !=
1894d8f1
KB
1487 NVME_CSTS_SHST_CMPLT) {
1488 msleep(100);
1489 if (fatal_signal_pending(current))
1490 return -EINTR;
1491 if (time_after(jiffies, timeout)) {
e75ec752 1492 dev_err(dev->dev,
1894d8f1
KB
1493 "Device shutdown incomplete; abort shutdown\n");
1494 return -ENODEV;
1495 }
1496 }
1497
1498 return 0;
1499}
1500
a4aea562 1501static struct blk_mq_ops nvme_mq_admin_ops = {
d29ec824 1502 .queue_rq = nvme_queue_rq,
a4aea562
MB
1503 .map_queue = blk_mq_map_queue,
1504 .init_hctx = nvme_admin_init_hctx,
4af0e21c 1505 .exit_hctx = nvme_admin_exit_hctx,
a4aea562
MB
1506 .init_request = nvme_admin_init_request,
1507 .timeout = nvme_timeout,
1508};
1509
1510static struct blk_mq_ops nvme_mq_ops = {
1511 .queue_rq = nvme_queue_rq,
1512 .map_queue = blk_mq_map_queue,
1513 .init_hctx = nvme_init_hctx,
1514 .init_request = nvme_init_request,
1515 .timeout = nvme_timeout,
a0fa9647 1516 .poll = nvme_poll,
a4aea562
MB
1517};
1518
ea191d2f
KB
1519static void nvme_dev_remove_admin(struct nvme_dev *dev)
1520{
1c63dc66
CH
1521 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
1522 blk_cleanup_queue(dev->ctrl.admin_q);
ea191d2f
KB
1523 blk_mq_free_tag_set(&dev->admin_tagset);
1524 }
1525}
1526
a4aea562
MB
1527static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1528{
1c63dc66 1529 if (!dev->ctrl.admin_q) {
a4aea562
MB
1530 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1531 dev->admin_tagset.nr_hw_queues = 1;
1532 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
1efccc9d 1533 dev->admin_tagset.reserved_tags = 1;
a4aea562 1534 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
e75ec752 1535 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
ac3dd5bd 1536 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
a4aea562
MB
1537 dev->admin_tagset.driver_data = dev;
1538
1539 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1540 return -ENOMEM;
1541
1c63dc66
CH
1542 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1543 if (IS_ERR(dev->ctrl.admin_q)) {
a4aea562
MB
1544 blk_mq_free_tag_set(&dev->admin_tagset);
1545 return -ENOMEM;
1546 }
1c63dc66 1547 if (!blk_get_queue(dev->ctrl.admin_q)) {
ea191d2f 1548 nvme_dev_remove_admin(dev);
1c63dc66 1549 dev->ctrl.admin_q = NULL;
ea191d2f
KB
1550 return -ENODEV;
1551 }
0fb59cbc 1552 } else
1c63dc66 1553 blk_mq_unfreeze_queue(dev->ctrl.admin_q);
a4aea562
MB
1554
1555 return 0;
1556}
1557
8d85fce7 1558static int nvme_configure_admin_queue(struct nvme_dev *dev)
b60503ba 1559{
ba47e386 1560 int result;
b60503ba 1561 u32 aqa;
7a67cbea 1562 u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
b60503ba
MW
1563 struct nvme_queue *nvmeq;
1564
7a67cbea 1565 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1) ?
dfbac8c7
KB
1566 NVME_CAP_NSSRC(cap) : 0;
1567
7a67cbea
CH
1568 if (dev->subsystem &&
1569 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1570 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
dfbac8c7 1571
ba47e386
MW
1572 result = nvme_disable_ctrl(dev, cap);
1573 if (result < 0)
1574 return result;
b60503ba 1575
a4aea562 1576 nvmeq = dev->queues[0];
cd638946 1577 if (!nvmeq) {
2b25d981 1578 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
cd638946
KB
1579 if (!nvmeq)
1580 return -ENOMEM;
cd638946 1581 }
b60503ba
MW
1582
1583 aqa = nvmeq->q_depth - 1;
1584 aqa |= aqa << 16;
1585
7a67cbea
CH
1586 writel(aqa, dev->bar + NVME_REG_AQA);
1587 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1588 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
b60503ba 1589
ba47e386 1590 result = nvme_enable_ctrl(dev, cap);
025c557a 1591 if (result)
a4aea562
MB
1592 goto free_nvmeq;
1593
2b25d981 1594 nvmeq->cq_vector = 0;
3193f07b 1595 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
758dd7fd
JD
1596 if (result) {
1597 nvmeq->cq_vector = -1;
0fb59cbc 1598 goto free_nvmeq;
758dd7fd 1599 }
025c557a 1600
b60503ba 1601 return result;
a4aea562 1602
a4aea562
MB
1603 free_nvmeq:
1604 nvme_free_queues(dev, 0);
1605 return result;
b60503ba
MW
1606}
1607
81f03fed
JD
1608static int nvme_subsys_reset(struct nvme_dev *dev)
1609{
1610 if (!dev->subsystem)
1611 return -ENOTTY;
1612
7a67cbea 1613 writel(0x4E564D65, dev->bar + NVME_REG_NSSR); /* "NVMe" */
81f03fed
JD
1614 return 0;
1615}
1616
1fa6aead
MW
1617static int nvme_kthread(void *data)
1618{
d4b4ff8e 1619 struct nvme_dev *dev, *next;
1fa6aead
MW
1620
1621 while (!kthread_should_stop()) {
564a232c 1622 set_current_state(TASK_INTERRUPTIBLE);
1fa6aead 1623 spin_lock(&dev_list_lock);
d4b4ff8e 1624 list_for_each_entry_safe(dev, next, &dev_list, node) {
1fa6aead 1625 int i;
7a67cbea 1626 u32 csts = readl(dev->bar + NVME_REG_CSTS);
dfbac8c7
KB
1627
1628 if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
1629 csts & NVME_CSTS_CFS) {
90667892
CH
1630 if (!__nvme_reset(dev)) {
1631 dev_warn(dev->dev,
1632 "Failed status: %x, reset controller\n",
7a67cbea 1633 readl(dev->bar + NVME_REG_CSTS));
90667892 1634 }
d4b4ff8e
KB
1635 continue;
1636 }
1fa6aead 1637 for (i = 0; i < dev->queue_count; i++) {
a4aea562 1638 struct nvme_queue *nvmeq = dev->queues[i];
740216fc
MW
1639 if (!nvmeq)
1640 continue;
1fa6aead 1641 spin_lock_irq(&nvmeq->q_lock);
bc57a0f7 1642 nvme_process_cq(nvmeq);
6fccf938 1643
1c63dc66 1644 while (i == 0 && dev->ctrl.event_limit > 0) {
a4aea562 1645 if (nvme_submit_async_admin_req(dev))
6fccf938 1646 break;
1c63dc66 1647 dev->ctrl.event_limit--;
6fccf938 1648 }
1fa6aead
MW
1649 spin_unlock_irq(&nvmeq->q_lock);
1650 }
1651 }
1652 spin_unlock(&dev_list_lock);
acb7aa0d 1653 schedule_timeout(round_jiffies_relative(HZ));
1fa6aead
MW
1654 }
1655 return 0;
1656}
1657
e1e5e564 1658static void nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid)
b60503ba
MW
1659{
1660 struct nvme_ns *ns;
1661 struct gendisk *disk;
e75ec752 1662 int node = dev_to_node(dev->dev);
b60503ba 1663
a4aea562 1664 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
b60503ba 1665 if (!ns)
e1e5e564
KB
1666 return;
1667
a4aea562 1668 ns->queue = blk_mq_init_queue(&dev->tagset);
9f173b33 1669 if (IS_ERR(ns->queue))
b60503ba 1670 goto out_free_ns;
4eeb9215
MW
1671 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
1672 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
1c63dc66 1673 ns->ctrl = &dev->ctrl;
b60503ba
MW
1674 ns->queue->queuedata = ns;
1675
a4aea562 1676 disk = alloc_disk_node(0, node);
b60503ba
MW
1677 if (!disk)
1678 goto out_free_queue;
a4aea562 1679
188c3568 1680 kref_init(&ns->kref);
5aff9382 1681 ns->ns_id = nsid;
b60503ba 1682 ns->disk = disk;
e1e5e564
KB
1683 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
1684 list_add_tail(&ns->list, &dev->namespaces);
1685
e9ef4636 1686 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
e824410f 1687 if (dev->max_hw_sectors) {
8fc23e03 1688 blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
e824410f 1689 blk_queue_max_segments(ns->queue,
6824c5ef 1690 (dev->max_hw_sectors / (dev->page_size >> 9)) + 1);
e824410f 1691 }
a4aea562
MB
1692 if (dev->stripe_size)
1693 blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9);
1c63dc66 1694 if (dev->ctrl.vwc & NVME_CTRL_VWC_PRESENT)
a7d2ce28 1695 blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
03100aad 1696 blk_queue_virt_boundary(ns->queue, dev->page_size - 1);
b60503ba
MW
1697
1698 disk->major = nvme_major;
469071a3 1699 disk->first_minor = 0;
b60503ba
MW
1700 disk->fops = &nvme_fops;
1701 disk->private_data = ns;
1702 disk->queue = ns->queue;
b3fffdef 1703 disk->driverfs_dev = dev->device;
469071a3 1704 disk->flags = GENHD_FL_EXT_DEVT;
1c63dc66 1705 sprintf(disk->disk_name, "nvme%dn%d", dev->ctrl.instance, nsid);
b60503ba 1706
e1e5e564
KB
1707 /*
1708 * Initialize capacity to 0 until we establish the namespace format and
1709 * setup integrity extentions if necessary. The revalidate_disk after
1710 * add_disk allows the driver to register with integrity if the format
1711 * requires it.
1712 */
1713 set_capacity(disk, 0);
a5768aa8
KB
1714 if (nvme_revalidate_disk(ns->disk))
1715 goto out_free_disk;
1716
1673f1f0 1717 kref_get(&dev->ctrl.kref);
ca064085
MB
1718 if (ns->type != NVME_NS_LIGHTNVM) {
1719 add_disk(ns->disk);
1720 if (ns->ms) {
1721 struct block_device *bd = bdget_disk(ns->disk, 0);
1722 if (!bd)
1723 return;
1724 if (blkdev_get(bd, FMODE_READ, NULL)) {
1725 bdput(bd);
1726 return;
1727 }
1728 blkdev_reread_part(bd);
1729 blkdev_put(bd, FMODE_READ);
7bee6074 1730 }
7bee6074 1731 }
e1e5e564 1732 return;
a5768aa8
KB
1733 out_free_disk:
1734 kfree(disk);
1735 list_del(&ns->list);
b60503ba
MW
1736 out_free_queue:
1737 blk_cleanup_queue(ns->queue);
1738 out_free_ns:
1739 kfree(ns);
b60503ba
MW
1740}
1741
2659e57b
CH
1742/*
1743 * Create I/O queues. Failing to create an I/O queue is not an issue,
1744 * we can continue with less than the desired amount of queues, and
1745 * even a controller without I/O queues an still be used to issue
1746 * admin commands. This might be useful to upgrade a buggy firmware
1747 * for example.
1748 */
42f61420
KB
1749static void nvme_create_io_queues(struct nvme_dev *dev)
1750{
a4aea562 1751 unsigned i;
42f61420 1752
a4aea562 1753 for (i = dev->queue_count; i <= dev->max_qid; i++)
2b25d981 1754 if (!nvme_alloc_queue(dev, i, dev->q_depth))
42f61420
KB
1755 break;
1756
a4aea562 1757 for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
2659e57b
CH
1758 if (nvme_create_queue(dev->queues[i], i)) {
1759 nvme_free_queues(dev, i);
42f61420 1760 break;
2659e57b 1761 }
42f61420
KB
1762}
1763
b3b06812 1764static int set_queue_count(struct nvme_dev *dev, int count)
b60503ba
MW
1765{
1766 int status;
1767 u32 result;
b3b06812 1768 u32 q_count = (count - 1) | ((count - 1) << 16);
b60503ba 1769
1c63dc66 1770 status = nvme_set_features(&dev->ctrl, NVME_FEAT_NUM_QUEUES, q_count, 0,
bc5fc7e4 1771 &result);
27e8166c
MW
1772 if (status < 0)
1773 return status;
1774 if (status > 0) {
e75ec752 1775 dev_err(dev->dev, "Could not set queue count (%d)\n", status);
badc34d4 1776 return 0;
27e8166c 1777 }
b60503ba
MW
1778 return min(result & 0xffff, result >> 16) + 1;
1779}
1780
8ffaadf7
JD
1781static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1782{
1783 u64 szu, size, offset;
1784 u32 cmbloc;
1785 resource_size_t bar_size;
1786 struct pci_dev *pdev = to_pci_dev(dev->dev);
1787 void __iomem *cmb;
1788 dma_addr_t dma_addr;
1789
1790 if (!use_cmb_sqes)
1791 return NULL;
1792
7a67cbea 1793 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
8ffaadf7
JD
1794 if (!(NVME_CMB_SZ(dev->cmbsz)))
1795 return NULL;
1796
7a67cbea 1797 cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
8ffaadf7
JD
1798
1799 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1800 size = szu * NVME_CMB_SZ(dev->cmbsz);
1801 offset = szu * NVME_CMB_OFST(cmbloc);
1802 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
1803
1804 if (offset > bar_size)
1805 return NULL;
1806
1807 /*
1808 * Controllers may support a CMB size larger than their BAR,
1809 * for example, due to being behind a bridge. Reduce the CMB to
1810 * the reported size of the BAR
1811 */
1812 if (size > bar_size - offset)
1813 size = bar_size - offset;
1814
1815 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
1816 cmb = ioremap_wc(dma_addr, size);
1817 if (!cmb)
1818 return NULL;
1819
1820 dev->cmb_dma_addr = dma_addr;
1821 dev->cmb_size = size;
1822 return cmb;
1823}
1824
1825static inline void nvme_release_cmb(struct nvme_dev *dev)
1826{
1827 if (dev->cmb) {
1828 iounmap(dev->cmb);
1829 dev->cmb = NULL;
1830 }
1831}
1832
9d713c2b
KB
1833static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1834{
b80d5ccc 1835 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
9d713c2b
KB
1836}
1837
8d85fce7 1838static int nvme_setup_io_queues(struct nvme_dev *dev)
b60503ba 1839{
a4aea562 1840 struct nvme_queue *adminq = dev->queues[0];
e75ec752 1841 struct pci_dev *pdev = to_pci_dev(dev->dev);
42f61420 1842 int result, i, vecs, nr_io_queues, size;
b60503ba 1843
42f61420 1844 nr_io_queues = num_possible_cpus();
b348b7d5 1845 result = set_queue_count(dev, nr_io_queues);
badc34d4 1846 if (result <= 0)
1b23484b 1847 return result;
b348b7d5
MW
1848 if (result < nr_io_queues)
1849 nr_io_queues = result;
b60503ba 1850
8ffaadf7
JD
1851 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1852 result = nvme_cmb_qdepth(dev, nr_io_queues,
1853 sizeof(struct nvme_command));
1854 if (result > 0)
1855 dev->q_depth = result;
1856 else
1857 nvme_release_cmb(dev);
1858 }
1859
9d713c2b
KB
1860 size = db_bar_size(dev, nr_io_queues);
1861 if (size > 8192) {
f1938f6e 1862 iounmap(dev->bar);
9d713c2b
KB
1863 do {
1864 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1865 if (dev->bar)
1866 break;
1867 if (!--nr_io_queues)
1868 return -ENOMEM;
1869 size = db_bar_size(dev, nr_io_queues);
1870 } while (1);
7a67cbea 1871 dev->dbs = dev->bar + 4096;
5a92e700 1872 adminq->q_db = dev->dbs;
f1938f6e
MW
1873 }
1874
9d713c2b 1875 /* Deregister the admin queue's interrupt */
3193f07b 1876 free_irq(dev->entry[0].vector, adminq);
9d713c2b 1877
e32efbfc
JA
1878 /*
1879 * If we enable msix early due to not intx, disable it again before
1880 * setting up the full range we need.
1881 */
1882 if (!pdev->irq)
1883 pci_disable_msix(pdev);
1884
be577fab 1885 for (i = 0; i < nr_io_queues; i++)
1b23484b 1886 dev->entry[i].entry = i;
be577fab
AG
1887 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
1888 if (vecs < 0) {
1889 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
1890 if (vecs < 0) {
1891 vecs = 1;
1892 } else {
1893 for (i = 0; i < vecs; i++)
1894 dev->entry[i].vector = i + pdev->irq;
fa08a396
RRG
1895 }
1896 }
1897
063a8096
MW
1898 /*
1899 * Should investigate if there's a performance win from allocating
1900 * more queues than interrupt vectors; it might allow the submission
1901 * path to scale better, even if the receive path is limited by the
1902 * number of interrupts.
1903 */
1904 nr_io_queues = vecs;
42f61420 1905 dev->max_qid = nr_io_queues;
063a8096 1906
3193f07b 1907 result = queue_request_irq(dev, adminq, adminq->irqname);
758dd7fd
JD
1908 if (result) {
1909 adminq->cq_vector = -1;
22404274 1910 goto free_queues;
758dd7fd 1911 }
1b23484b 1912
cd638946 1913 /* Free previously allocated queues that are no longer usable */
42f61420 1914 nvme_free_queues(dev, nr_io_queues + 1);
a4aea562 1915 nvme_create_io_queues(dev);
9ecdc946 1916
22404274 1917 return 0;
b60503ba 1918
22404274 1919 free_queues:
a1a5ef99 1920 nvme_free_queues(dev, 1);
22404274 1921 return result;
b60503ba
MW
1922}
1923
a5768aa8
KB
1924static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
1925{
1926 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
1927 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
1928
1929 return nsa->ns_id - nsb->ns_id;
1930}
1931
1932static struct nvme_ns *nvme_find_ns(struct nvme_dev *dev, unsigned nsid)
1933{
1934 struct nvme_ns *ns;
1935
1936 list_for_each_entry(ns, &dev->namespaces, list) {
1937 if (ns->ns_id == nsid)
1938 return ns;
1939 if (ns->ns_id > nsid)
1940 break;
1941 }
1942 return NULL;
1943}
1944
1945static inline bool nvme_io_incapable(struct nvme_dev *dev)
1946{
7a67cbea
CH
1947 return (!dev->bar ||
1948 readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_CFS ||
1949 dev->online_queues < 2);
a5768aa8
KB
1950}
1951
1952static void nvme_ns_remove(struct nvme_ns *ns)
1953{
1c63dc66
CH
1954 bool kill = nvme_io_incapable(to_nvme_dev(ns->ctrl)) &&
1955 !blk_queue_dying(ns->queue);
a5768aa8
KB
1956
1957 if (kill)
1958 blk_set_queue_dying(ns->queue);
9609b994 1959 if (ns->disk->flags & GENHD_FL_UP)
a5768aa8 1960 del_gendisk(ns->disk);
a5768aa8
KB
1961 if (kill || !blk_queue_dying(ns->queue)) {
1962 blk_mq_abort_requeue_list(ns->queue);
1963 blk_cleanup_queue(ns->queue);
5105aa55
KB
1964 }
1965 list_del_init(&ns->list);
1673f1f0 1966 nvme_put_ns(ns);
a5768aa8
KB
1967}
1968
1969static void nvme_scan_namespaces(struct nvme_dev *dev, unsigned nn)
1970{
1971 struct nvme_ns *ns, *next;
1972 unsigned i;
1973
1974 for (i = 1; i <= nn; i++) {
1975 ns = nvme_find_ns(dev, i);
1976 if (ns) {
5105aa55 1977 if (revalidate_disk(ns->disk))
a5768aa8 1978 nvme_ns_remove(ns);
a5768aa8
KB
1979 } else
1980 nvme_alloc_ns(dev, i);
1981 }
1982 list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
5105aa55 1983 if (ns->ns_id > nn)
a5768aa8 1984 nvme_ns_remove(ns);
a5768aa8
KB
1985 }
1986 list_sort(NULL, &dev->namespaces, ns_cmp);
1987}
1988
bda4e0fb
KB
1989static void nvme_set_irq_hints(struct nvme_dev *dev)
1990{
1991 struct nvme_queue *nvmeq;
1992 int i;
1993
1994 for (i = 0; i < dev->online_queues; i++) {
1995 nvmeq = dev->queues[i];
1996
1997 if (!nvmeq->tags || !(*nvmeq->tags))
1998 continue;
1999
2000 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
2001 blk_mq_tags_cpumask(*nvmeq->tags));
2002 }
2003}
2004
a5768aa8
KB
2005static void nvme_dev_scan(struct work_struct *work)
2006{
2007 struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
2008 struct nvme_id_ctrl *ctrl;
2009
2010 if (!dev->tagset.tags)
2011 return;
1c63dc66 2012 if (nvme_identify_ctrl(&dev->ctrl, &ctrl))
a5768aa8
KB
2013 return;
2014 nvme_scan_namespaces(dev, le32_to_cpup(&ctrl->nn));
2015 kfree(ctrl);
bda4e0fb 2016 nvme_set_irq_hints(dev);
a5768aa8
KB
2017}
2018
422ef0c7
MW
2019/*
2020 * Return: error value if an error occurred setting up the queues or calling
2021 * Identify Device. 0 if these succeeded, even if adding some of the
2022 * namespaces failed. At the moment, these failures are silent. TBD which
2023 * failures should be reported.
2024 */
8d85fce7 2025static int nvme_dev_add(struct nvme_dev *dev)
b60503ba 2026{
c3bfe717 2027 int res;
51814232 2028 struct nvme_id_ctrl *ctrl;
7a67cbea 2029 int shift = NVME_CAP_MPSMIN(lo_hi_readq(dev->bar + NVME_REG_CAP)) + 12;
b60503ba 2030
1c63dc66 2031 res = nvme_identify_ctrl(&dev->ctrl, &ctrl);
b60503ba 2032 if (res) {
e75ec752 2033 dev_err(dev->dev, "Identify Controller failed (%d)\n", res);
e1e5e564 2034 return -EIO;
b60503ba
MW
2035 }
2036
1c63dc66
CH
2037 dev->ctrl.oncs = le16_to_cpup(&ctrl->oncs);
2038 dev->ctrl.abort_limit = ctrl->acl + 1;
2039 dev->ctrl.vwc = ctrl->vwc;
2040 memcpy(dev->ctrl.serial, ctrl->sn, sizeof(ctrl->sn));
2041 memcpy(dev->ctrl.model, ctrl->mn, sizeof(ctrl->mn));
2042 memcpy(dev->ctrl.firmware_rev, ctrl->fr, sizeof(ctrl->fr));
159b67d7 2043 if (ctrl->mdts)
8fc23e03 2044 dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
b12363d0
S
2045 else
2046 dev->max_hw_sectors = UINT_MAX;
106198ed
CH
2047
2048 if ((dev->ctrl.quirks & NVME_QUIRK_STRIPE_SIZE) && ctrl->vs[3]) {
a4aea562
MB
2049 unsigned int max_hw_sectors;
2050
159b67d7 2051 dev->stripe_size = 1 << (ctrl->vs[3] + shift);
a4aea562
MB
2052 max_hw_sectors = dev->stripe_size >> (shift - 9);
2053 if (dev->max_hw_sectors) {
2054 dev->max_hw_sectors = min(max_hw_sectors,
2055 dev->max_hw_sectors);
2056 } else
2057 dev->max_hw_sectors = max_hw_sectors;
2058 }
d29ec824 2059 kfree(ctrl);
a4aea562 2060
ffe7704d
KB
2061 if (!dev->tagset.tags) {
2062 dev->tagset.ops = &nvme_mq_ops;
2063 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2064 dev->tagset.timeout = NVME_IO_TIMEOUT;
2065 dev->tagset.numa_node = dev_to_node(dev->dev);
2066 dev->tagset.queue_depth =
a4aea562 2067 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
ffe7704d
KB
2068 dev->tagset.cmd_size = nvme_cmd_size(dev);
2069 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2070 dev->tagset.driver_data = dev;
b60503ba 2071
ffe7704d
KB
2072 if (blk_mq_alloc_tag_set(&dev->tagset))
2073 return 0;
2074 }
a5768aa8 2075 schedule_work(&dev->scan_work);
e1e5e564 2076 return 0;
b60503ba
MW
2077}
2078
0877cb0d
KB
2079static int nvme_dev_map(struct nvme_dev *dev)
2080{
42f61420 2081 u64 cap;
0877cb0d 2082 int bars, result = -ENOMEM;
e75ec752 2083 struct pci_dev *pdev = to_pci_dev(dev->dev);
0877cb0d
KB
2084
2085 if (pci_enable_device_mem(pdev))
2086 return result;
2087
2088 dev->entry[0].vector = pdev->irq;
2089 pci_set_master(pdev);
2090 bars = pci_select_bars(pdev, IORESOURCE_MEM);
be7837e8
JA
2091 if (!bars)
2092 goto disable_pci;
2093
0877cb0d
KB
2094 if (pci_request_selected_regions(pdev, bars, "nvme"))
2095 goto disable_pci;
2096
e75ec752
CH
2097 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
2098 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
052d0efa 2099 goto disable;
0877cb0d 2100
0877cb0d
KB
2101 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
2102 if (!dev->bar)
2103 goto disable;
e32efbfc 2104
7a67cbea 2105 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
0e53d180
KB
2106 result = -ENODEV;
2107 goto unmap;
2108 }
e32efbfc
JA
2109
2110 /*
2111 * Some devices don't advertse INTx interrupts, pre-enable a single
2112 * MSIX vec for setup. We'll adjust this later.
2113 */
2114 if (!pdev->irq) {
2115 result = pci_enable_msix(pdev, dev->entry, 1);
2116 if (result < 0)
2117 goto unmap;
2118 }
2119
7a67cbea
CH
2120 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
2121
42f61420
KB
2122 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
2123 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
7a67cbea
CH
2124 dev->dbs = dev->bar + 4096;
2125 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2))
8ffaadf7 2126 dev->cmb = nvme_map_cmb(dev);
0877cb0d
KB
2127
2128 return 0;
2129
0e53d180
KB
2130 unmap:
2131 iounmap(dev->bar);
2132 dev->bar = NULL;
0877cb0d
KB
2133 disable:
2134 pci_release_regions(pdev);
2135 disable_pci:
2136 pci_disable_device(pdev);
2137 return result;
2138}
2139
2140static void nvme_dev_unmap(struct nvme_dev *dev)
2141{
e75ec752
CH
2142 struct pci_dev *pdev = to_pci_dev(dev->dev);
2143
2144 if (pdev->msi_enabled)
2145 pci_disable_msi(pdev);
2146 else if (pdev->msix_enabled)
2147 pci_disable_msix(pdev);
0877cb0d
KB
2148
2149 if (dev->bar) {
2150 iounmap(dev->bar);
2151 dev->bar = NULL;
e75ec752 2152 pci_release_regions(pdev);
0877cb0d
KB
2153 }
2154
e75ec752
CH
2155 if (pci_is_enabled(pdev))
2156 pci_disable_device(pdev);
0877cb0d
KB
2157}
2158
4d115420
KB
2159struct nvme_delq_ctx {
2160 struct task_struct *waiter;
2161 struct kthread_worker *worker;
2162 atomic_t refcount;
2163};
2164
2165static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
2166{
2167 dq->waiter = current;
2168 mb();
2169
2170 for (;;) {
2171 set_current_state(TASK_KILLABLE);
2172 if (!atomic_read(&dq->refcount))
2173 break;
2174 if (!schedule_timeout(ADMIN_TIMEOUT) ||
2175 fatal_signal_pending(current)) {
0fb59cbc
KB
2176 /*
2177 * Disable the controller first since we can't trust it
2178 * at this point, but leave the admin queue enabled
2179 * until all queue deletion requests are flushed.
2180 * FIXME: This may take a while if there are more h/w
2181 * queues than admin tags.
2182 */
4d115420 2183 set_current_state(TASK_RUNNING);
7a67cbea
CH
2184 nvme_disable_ctrl(dev,
2185 lo_hi_readq(dev->bar + NVME_REG_CAP));
0fb59cbc 2186 nvme_clear_queue(dev->queues[0]);
4d115420 2187 flush_kthread_worker(dq->worker);
0fb59cbc 2188 nvme_disable_queue(dev, 0);
4d115420
KB
2189 return;
2190 }
2191 }
2192 set_current_state(TASK_RUNNING);
2193}
2194
2195static void nvme_put_dq(struct nvme_delq_ctx *dq)
2196{
2197 atomic_dec(&dq->refcount);
2198 if (dq->waiter)
2199 wake_up_process(dq->waiter);
2200}
2201
2202static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
2203{
2204 atomic_inc(&dq->refcount);
2205 return dq;
2206}
2207
2208static void nvme_del_queue_end(struct nvme_queue *nvmeq)
2209{
2210 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
4d115420 2211 nvme_put_dq(dq);
604e8c8d
KB
2212
2213 spin_lock_irq(&nvmeq->q_lock);
2214 nvme_process_cq(nvmeq);
2215 spin_unlock_irq(&nvmeq->q_lock);
4d115420
KB
2216}
2217
2218static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
2219 kthread_work_func_t fn)
2220{
2221 struct nvme_command c;
2222
2223 memset(&c, 0, sizeof(c));
2224 c.delete_queue.opcode = opcode;
2225 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2226
2227 init_kthread_work(&nvmeq->cmdinfo.work, fn);
a4aea562
MB
2228 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
2229 ADMIN_TIMEOUT);
4d115420
KB
2230}
2231
2232static void nvme_del_cq_work_handler(struct kthread_work *work)
2233{
2234 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2235 cmdinfo.work);
2236 nvme_del_queue_end(nvmeq);
2237}
2238
2239static int nvme_delete_cq(struct nvme_queue *nvmeq)
2240{
2241 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
2242 nvme_del_cq_work_handler);
2243}
2244
2245static void nvme_del_sq_work_handler(struct kthread_work *work)
2246{
2247 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2248 cmdinfo.work);
2249 int status = nvmeq->cmdinfo.status;
2250
2251 if (!status)
2252 status = nvme_delete_cq(nvmeq);
2253 if (status)
2254 nvme_del_queue_end(nvmeq);
2255}
2256
2257static int nvme_delete_sq(struct nvme_queue *nvmeq)
2258{
2259 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
2260 nvme_del_sq_work_handler);
2261}
2262
2263static void nvme_del_queue_start(struct kthread_work *work)
2264{
2265 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
2266 cmdinfo.work);
4d115420
KB
2267 if (nvme_delete_sq(nvmeq))
2268 nvme_del_queue_end(nvmeq);
2269}
2270
2271static void nvme_disable_io_queues(struct nvme_dev *dev)
2272{
2273 int i;
2274 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
2275 struct nvme_delq_ctx dq;
2276 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
1c63dc66 2277 &worker, "nvme%d", dev->ctrl.instance);
4d115420
KB
2278
2279 if (IS_ERR(kworker_task)) {
e75ec752 2280 dev_err(dev->dev,
4d115420
KB
2281 "Failed to create queue del task\n");
2282 for (i = dev->queue_count - 1; i > 0; i--)
2283 nvme_disable_queue(dev, i);
2284 return;
2285 }
2286
2287 dq.waiter = NULL;
2288 atomic_set(&dq.refcount, 0);
2289 dq.worker = &worker;
2290 for (i = dev->queue_count - 1; i > 0; i--) {
a4aea562 2291 struct nvme_queue *nvmeq = dev->queues[i];
4d115420
KB
2292
2293 if (nvme_suspend_queue(nvmeq))
2294 continue;
2295 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
2296 nvmeq->cmdinfo.worker = dq.worker;
2297 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2298 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2299 }
2300 nvme_wait_dq(&dq, dev);
2301 kthread_stop(kworker_task);
2302}
2303
b9afca3e
DM
2304/*
2305* Remove the node from the device list and check
2306* for whether or not we need to stop the nvme_thread.
2307*/
2308static void nvme_dev_list_remove(struct nvme_dev *dev)
2309{
2310 struct task_struct *tmp = NULL;
2311
2312 spin_lock(&dev_list_lock);
2313 list_del_init(&dev->node);
2314 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2315 tmp = nvme_thread;
2316 nvme_thread = NULL;
2317 }
2318 spin_unlock(&dev_list_lock);
2319
2320 if (tmp)
2321 kthread_stop(tmp);
2322}
2323
c9d3bf88
KB
2324static void nvme_freeze_queues(struct nvme_dev *dev)
2325{
2326 struct nvme_ns *ns;
2327
2328 list_for_each_entry(ns, &dev->namespaces, list) {
2329 blk_mq_freeze_queue_start(ns->queue);
2330
cddcd72b 2331 spin_lock_irq(ns->queue->queue_lock);
c9d3bf88 2332 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
cddcd72b 2333 spin_unlock_irq(ns->queue->queue_lock);
c9d3bf88
KB
2334
2335 blk_mq_cancel_requeue_work(ns->queue);
2336 blk_mq_stop_hw_queues(ns->queue);
2337 }
2338}
2339
2340static void nvme_unfreeze_queues(struct nvme_dev *dev)
2341{
2342 struct nvme_ns *ns;
2343
2344 list_for_each_entry(ns, &dev->namespaces, list) {
2345 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2346 blk_mq_unfreeze_queue(ns->queue);
2347 blk_mq_start_stopped_hw_queues(ns->queue, true);
2348 blk_mq_kick_requeue_list(ns->queue);
2349 }
2350}
2351
f0b50732 2352static void nvme_dev_shutdown(struct nvme_dev *dev)
b60503ba 2353{
22404274 2354 int i;
7c1b2450 2355 u32 csts = -1;
22404274 2356
b9afca3e 2357 nvme_dev_list_remove(dev);
1fa6aead 2358
c9d3bf88
KB
2359 if (dev->bar) {
2360 nvme_freeze_queues(dev);
7a67cbea 2361 csts = readl(dev->bar + NVME_REG_CSTS);
c9d3bf88 2362 }
7c1b2450 2363 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
4d115420 2364 for (i = dev->queue_count - 1; i >= 0; i--) {
a4aea562 2365 struct nvme_queue *nvmeq = dev->queues[i];
4d115420 2366 nvme_suspend_queue(nvmeq);
4d115420
KB
2367 }
2368 } else {
2369 nvme_disable_io_queues(dev);
1894d8f1 2370 nvme_shutdown_ctrl(dev);
4d115420
KB
2371 nvme_disable_queue(dev, 0);
2372 }
f0b50732 2373 nvme_dev_unmap(dev);
07836e65
KB
2374
2375 for (i = dev->queue_count - 1; i >= 0; i--)
2376 nvme_clear_queue(dev->queues[i]);
f0b50732
KB
2377}
2378
2379static void nvme_dev_remove(struct nvme_dev *dev)
2380{
5105aa55 2381 struct nvme_ns *ns, *next;
f0b50732 2382
5105aa55 2383 list_for_each_entry_safe(ns, next, &dev->namespaces, list)
a5768aa8 2384 nvme_ns_remove(ns);
b60503ba
MW
2385}
2386
091b6092
MW
2387static int nvme_setup_prp_pools(struct nvme_dev *dev)
2388{
e75ec752 2389 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
091b6092
MW
2390 PAGE_SIZE, PAGE_SIZE, 0);
2391 if (!dev->prp_page_pool)
2392 return -ENOMEM;
2393
99802a7a 2394 /* Optimisation for I/Os between 4k and 128k */
e75ec752 2395 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
99802a7a
MW
2396 256, 256, 0);
2397 if (!dev->prp_small_pool) {
2398 dma_pool_destroy(dev->prp_page_pool);
2399 return -ENOMEM;
2400 }
091b6092
MW
2401 return 0;
2402}
2403
2404static void nvme_release_prp_pools(struct nvme_dev *dev)
2405{
2406 dma_pool_destroy(dev->prp_page_pool);
99802a7a 2407 dma_pool_destroy(dev->prp_small_pool);
091b6092
MW
2408}
2409
cd58ad7d
QSA
2410static DEFINE_IDA(nvme_instance_ida);
2411
2412static int nvme_set_instance(struct nvme_dev *dev)
b60503ba 2413{
cd58ad7d
QSA
2414 int instance, error;
2415
2416 do {
2417 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2418 return -ENODEV;
2419
2420 spin_lock(&dev_list_lock);
2421 error = ida_get_new(&nvme_instance_ida, &instance);
2422 spin_unlock(&dev_list_lock);
2423 } while (error == -EAGAIN);
2424
2425 if (error)
2426 return -ENODEV;
2427
1c63dc66 2428 dev->ctrl.instance = instance;
cd58ad7d 2429 return 0;
b60503ba
MW
2430}
2431
2432static void nvme_release_instance(struct nvme_dev *dev)
2433{
cd58ad7d 2434 spin_lock(&dev_list_lock);
1c63dc66 2435 ida_remove(&nvme_instance_ida, dev->ctrl.instance);
cd58ad7d 2436 spin_unlock(&dev_list_lock);
b60503ba
MW
2437}
2438
1673f1f0 2439static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
5e82e952 2440{
1673f1f0 2441 struct nvme_dev *dev = to_nvme_dev(ctrl);
9ac27090 2442
e75ec752 2443 put_device(dev->dev);
b3fffdef 2444 put_device(dev->device);
285dffc9 2445 nvme_release_instance(dev);
4af0e21c
KB
2446 if (dev->tagset.tags)
2447 blk_mq_free_tag_set(&dev->tagset);
1c63dc66
CH
2448 if (dev->ctrl.admin_q)
2449 blk_put_queue(dev->ctrl.admin_q);
5e82e952
KB
2450 kfree(dev->queues);
2451 kfree(dev->entry);
2452 kfree(dev);
2453}
2454
2455static int nvme_dev_open(struct inode *inode, struct file *f)
2456{
b3fffdef
KB
2457 struct nvme_dev *dev;
2458 int instance = iminor(inode);
2459 int ret = -ENODEV;
2460
2461 spin_lock(&dev_list_lock);
2462 list_for_each_entry(dev, &dev_list, node) {
1c63dc66
CH
2463 if (dev->ctrl.instance == instance) {
2464 if (!dev->ctrl.admin_q) {
2e1d8448
KB
2465 ret = -EWOULDBLOCK;
2466 break;
2467 }
1673f1f0 2468 if (!kref_get_unless_zero(&dev->ctrl.kref))
b3fffdef
KB
2469 break;
2470 f->private_data = dev;
2471 ret = 0;
2472 break;
2473 }
2474 }
2475 spin_unlock(&dev_list_lock);
2476
2477 return ret;
5e82e952
KB
2478}
2479
2480static int nvme_dev_release(struct inode *inode, struct file *f)
2481{
2482 struct nvme_dev *dev = f->private_data;
1673f1f0 2483 nvme_put_ctrl(&dev->ctrl);
5e82e952
KB
2484 return 0;
2485}
2486
2487static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2488{
2489 struct nvme_dev *dev = f->private_data;
a4aea562
MB
2490 struct nvme_ns *ns;
2491
5e82e952
KB
2492 switch (cmd) {
2493 case NVME_IOCTL_ADMIN_CMD:
1c63dc66 2494 return nvme_user_cmd(&dev->ctrl, NULL, (void __user *)arg);
7963e521 2495 case NVME_IOCTL_IO_CMD:
a4aea562
MB
2496 if (list_empty(&dev->namespaces))
2497 return -ENOTTY;
2498 ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
1c63dc66 2499 return nvme_user_cmd(&dev->ctrl, ns, (void __user *)arg);
4cc06521
KB
2500 case NVME_IOCTL_RESET:
2501 dev_warn(dev->dev, "resetting controller\n");
2502 return nvme_reset(dev);
81f03fed
JD
2503 case NVME_IOCTL_SUBSYS_RESET:
2504 return nvme_subsys_reset(dev);
5e82e952
KB
2505 default:
2506 return -ENOTTY;
2507 }
2508}
2509
2510static const struct file_operations nvme_dev_fops = {
2511 .owner = THIS_MODULE,
2512 .open = nvme_dev_open,
2513 .release = nvme_dev_release,
2514 .unlocked_ioctl = nvme_dev_ioctl,
2515 .compat_ioctl = nvme_dev_ioctl,
2516};
2517
3cf519b5 2518static void nvme_probe_work(struct work_struct *work)
f0b50732 2519{
3cf519b5 2520 struct nvme_dev *dev = container_of(work, struct nvme_dev, probe_work);
b9afca3e 2521 bool start_thread = false;
3cf519b5 2522 int result;
f0b50732
KB
2523
2524 result = nvme_dev_map(dev);
2525 if (result)
3cf519b5 2526 goto out;
f0b50732
KB
2527
2528 result = nvme_configure_admin_queue(dev);
2529 if (result)
2530 goto unmap;
2531
2532 spin_lock(&dev_list_lock);
b9afca3e
DM
2533 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2534 start_thread = true;
2535 nvme_thread = NULL;
2536 }
f0b50732
KB
2537 list_add(&dev->node, &dev_list);
2538 spin_unlock(&dev_list_lock);
2539
b9afca3e
DM
2540 if (start_thread) {
2541 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
387caa5a 2542 wake_up_all(&nvme_kthread_wait);
b9afca3e
DM
2543 } else
2544 wait_event_killable(nvme_kthread_wait, nvme_thread);
2545
2546 if (IS_ERR_OR_NULL(nvme_thread)) {
2547 result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2548 goto disable;
2549 }
a4aea562
MB
2550
2551 nvme_init_queue(dev->queues[0], 0);
0fb59cbc
KB
2552 result = nvme_alloc_admin_tags(dev);
2553 if (result)
2554 goto disable;
b9afca3e 2555
f0b50732 2556 result = nvme_setup_io_queues(dev);
badc34d4 2557 if (result)
0fb59cbc 2558 goto free_tags;
f0b50732 2559
1c63dc66 2560 dev->ctrl.event_limit = 1;
3cf519b5 2561
2659e57b
CH
2562 /*
2563 * Keep the controller around but remove all namespaces if we don't have
2564 * any working I/O queue.
2565 */
3cf519b5
CH
2566 if (dev->online_queues < 2) {
2567 dev_warn(dev->dev, "IO queues not created\n");
3cf519b5
CH
2568 nvme_dev_remove(dev);
2569 } else {
2570 nvme_unfreeze_queues(dev);
2571 nvme_dev_add(dev);
2572 }
2573
2574 return;
f0b50732 2575
0fb59cbc
KB
2576 free_tags:
2577 nvme_dev_remove_admin(dev);
1c63dc66
CH
2578 blk_put_queue(dev->ctrl.admin_q);
2579 dev->ctrl.admin_q = NULL;
4af0e21c 2580 dev->queues[0]->tags = NULL;
f0b50732 2581 disable:
a1a5ef99 2582 nvme_disable_queue(dev, 0);
b9afca3e 2583 nvme_dev_list_remove(dev);
f0b50732
KB
2584 unmap:
2585 nvme_dev_unmap(dev);
3cf519b5
CH
2586 out:
2587 if (!work_busy(&dev->reset_work))
2588 nvme_dead_ctrl(dev);
f0b50732
KB
2589}
2590
9a6b9458
KB
2591static int nvme_remove_dead_ctrl(void *arg)
2592{
2593 struct nvme_dev *dev = (struct nvme_dev *)arg;
e75ec752 2594 struct pci_dev *pdev = to_pci_dev(dev->dev);
9a6b9458
KB
2595
2596 if (pci_get_drvdata(pdev))
c81f4975 2597 pci_stop_and_remove_bus_device_locked(pdev);
1673f1f0 2598 nvme_put_ctrl(&dev->ctrl);
9a6b9458
KB
2599 return 0;
2600}
2601
de3eff2b
KB
2602static void nvme_dead_ctrl(struct nvme_dev *dev)
2603{
2604 dev_warn(dev->dev, "Device failed to resume\n");
1673f1f0 2605 kref_get(&dev->ctrl.kref);
de3eff2b 2606 if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
1c63dc66 2607 dev->ctrl.instance))) {
de3eff2b
KB
2608 dev_err(dev->dev,
2609 "Failed to start controller remove task\n");
1673f1f0 2610 nvme_put_ctrl(&dev->ctrl);
de3eff2b
KB
2611 }
2612}
2613
77b50d9e 2614static void nvme_reset_work(struct work_struct *ws)
9a6b9458 2615{
77b50d9e 2616 struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
ffe7704d
KB
2617 bool in_probe = work_busy(&dev->probe_work);
2618
9a6b9458 2619 nvme_dev_shutdown(dev);
ffe7704d
KB
2620
2621 /* Synchronize with device probe so that work will see failure status
2622 * and exit gracefully without trying to schedule another reset */
2623 flush_work(&dev->probe_work);
2624
2625 /* Fail this device if reset occured during probe to avoid
2626 * infinite initialization loops. */
2627 if (in_probe) {
de3eff2b 2628 nvme_dead_ctrl(dev);
ffe7704d 2629 return;
9a6b9458 2630 }
ffe7704d
KB
2631 /* Schedule device resume asynchronously so the reset work is available
2632 * to cleanup errors that may occur during reinitialization */
2633 schedule_work(&dev->probe_work);
9a6b9458
KB
2634}
2635
90667892 2636static int __nvme_reset(struct nvme_dev *dev)
9ca97374 2637{
90667892
CH
2638 if (work_pending(&dev->reset_work))
2639 return -EBUSY;
2640 list_del_init(&dev->node);
2641 queue_work(nvme_workq, &dev->reset_work);
2642 return 0;
9ca97374
TH
2643}
2644
4cc06521
KB
2645static int nvme_reset(struct nvme_dev *dev)
2646{
90667892 2647 int ret;
4cc06521 2648
1c63dc66 2649 if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q))
4cc06521
KB
2650 return -ENODEV;
2651
2652 spin_lock(&dev_list_lock);
90667892 2653 ret = __nvme_reset(dev);
4cc06521
KB
2654 spin_unlock(&dev_list_lock);
2655
2656 if (!ret) {
2657 flush_work(&dev->reset_work);
ffe7704d 2658 flush_work(&dev->probe_work);
4cc06521
KB
2659 return 0;
2660 }
2661
2662 return ret;
2663}
2664
2665static ssize_t nvme_sysfs_reset(struct device *dev,
2666 struct device_attribute *attr, const char *buf,
2667 size_t count)
2668{
2669 struct nvme_dev *ndev = dev_get_drvdata(dev);
2670 int ret;
2671
2672 ret = nvme_reset(ndev);
2673 if (ret < 0)
2674 return ret;
2675
2676 return count;
2677}
2678static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2679
1c63dc66
CH
2680static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
2681{
2682 *val = readl(to_nvme_dev(ctrl)->bar + off);
2683 return 0;
2684}
2685
2686static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
2687 .reg_read32 = nvme_pci_reg_read32,
1673f1f0 2688 .free_ctrl = nvme_pci_free_ctrl,
1c63dc66
CH
2689};
2690
8d85fce7 2691static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
b60503ba 2692{
a4aea562 2693 int node, result = -ENOMEM;
b60503ba
MW
2694 struct nvme_dev *dev;
2695
a4aea562
MB
2696 node = dev_to_node(&pdev->dev);
2697 if (node == NUMA_NO_NODE)
2698 set_dev_node(&pdev->dev, 0);
2699
2700 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
b60503ba
MW
2701 if (!dev)
2702 return -ENOMEM;
a4aea562
MB
2703 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2704 GFP_KERNEL, node);
b60503ba
MW
2705 if (!dev->entry)
2706 goto free;
a4aea562
MB
2707 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2708 GFP_KERNEL, node);
b60503ba
MW
2709 if (!dev->queues)
2710 goto free;
2711
2712 INIT_LIST_HEAD(&dev->namespaces);
77b50d9e 2713 INIT_WORK(&dev->reset_work, nvme_reset_work);
e75ec752 2714 dev->dev = get_device(&pdev->dev);
9a6b9458 2715 pci_set_drvdata(pdev, dev);
1c63dc66
CH
2716
2717 dev->ctrl.ops = &nvme_pci_ctrl_ops;
2718 dev->ctrl.dev = dev->dev;
106198ed 2719 dev->ctrl.quirks = id->driver_data;
1c63dc66 2720
cd58ad7d
QSA
2721 result = nvme_set_instance(dev);
2722 if (result)
a96d4f5c 2723 goto put_pci;
b60503ba 2724
091b6092
MW
2725 result = nvme_setup_prp_pools(dev);
2726 if (result)
0877cb0d 2727 goto release;
091b6092 2728
1673f1f0 2729 kref_init(&dev->ctrl.kref);
b3fffdef 2730 dev->device = device_create(nvme_class, &pdev->dev,
1c63dc66
CH
2731 MKDEV(nvme_char_major, dev->ctrl.instance),
2732 dev, "nvme%d", dev->ctrl.instance);
b3fffdef
KB
2733 if (IS_ERR(dev->device)) {
2734 result = PTR_ERR(dev->device);
2e1d8448 2735 goto release_pools;
b3fffdef
KB
2736 }
2737 get_device(dev->device);
4cc06521
KB
2738 dev_set_drvdata(dev->device, dev);
2739
2740 result = device_create_file(dev->device, &dev_attr_reset_controller);
2741 if (result)
2742 goto put_dev;
740216fc 2743
e6e96d73 2744 INIT_LIST_HEAD(&dev->node);
a5768aa8 2745 INIT_WORK(&dev->scan_work, nvme_dev_scan);
3cf519b5 2746 INIT_WORK(&dev->probe_work, nvme_probe_work);
2e1d8448 2747 schedule_work(&dev->probe_work);
b60503ba
MW
2748 return 0;
2749
4cc06521 2750 put_dev:
1c63dc66 2751 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->ctrl.instance));
4cc06521 2752 put_device(dev->device);
0877cb0d 2753 release_pools:
091b6092 2754 nvme_release_prp_pools(dev);
0877cb0d
KB
2755 release:
2756 nvme_release_instance(dev);
a96d4f5c 2757 put_pci:
e75ec752 2758 put_device(dev->dev);
b60503ba
MW
2759 free:
2760 kfree(dev->queues);
2761 kfree(dev->entry);
2762 kfree(dev);
2763 return result;
2764}
2765
f0d54a54
KB
2766static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2767{
a6739479 2768 struct nvme_dev *dev = pci_get_drvdata(pdev);
f0d54a54 2769
a6739479
KB
2770 if (prepare)
2771 nvme_dev_shutdown(dev);
2772 else
0a7385ad 2773 schedule_work(&dev->probe_work);
f0d54a54
KB
2774}
2775
09ece142
KB
2776static void nvme_shutdown(struct pci_dev *pdev)
2777{
2778 struct nvme_dev *dev = pci_get_drvdata(pdev);
2779 nvme_dev_shutdown(dev);
2780}
2781
8d85fce7 2782static void nvme_remove(struct pci_dev *pdev)
b60503ba
MW
2783{
2784 struct nvme_dev *dev = pci_get_drvdata(pdev);
9a6b9458
KB
2785
2786 spin_lock(&dev_list_lock);
2787 list_del_init(&dev->node);
2788 spin_unlock(&dev_list_lock);
2789
2790 pci_set_drvdata(pdev, NULL);
2e1d8448 2791 flush_work(&dev->probe_work);
9a6b9458 2792 flush_work(&dev->reset_work);
a5768aa8 2793 flush_work(&dev->scan_work);
4cc06521 2794 device_remove_file(dev->device, &dev_attr_reset_controller);
c9d3bf88 2795 nvme_dev_remove(dev);
3399a3f7 2796 nvme_dev_shutdown(dev);
a4aea562 2797 nvme_dev_remove_admin(dev);
1c63dc66 2798 device_destroy(nvme_class, MKDEV(nvme_char_major, dev->ctrl.instance));
a1a5ef99 2799 nvme_free_queues(dev, 0);
8ffaadf7 2800 nvme_release_cmb(dev);
9a6b9458 2801 nvme_release_prp_pools(dev);
1673f1f0 2802 nvme_put_ctrl(&dev->ctrl);
b60503ba
MW
2803}
2804
2805/* These functions are yet to be implemented */
2806#define nvme_error_detected NULL
2807#define nvme_dump_registers NULL
2808#define nvme_link_reset NULL
2809#define nvme_slot_reset NULL
2810#define nvme_error_resume NULL
cd638946 2811
671a6018 2812#ifdef CONFIG_PM_SLEEP
cd638946
KB
2813static int nvme_suspend(struct device *dev)
2814{
2815 struct pci_dev *pdev = to_pci_dev(dev);
2816 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2817
2818 nvme_dev_shutdown(ndev);
2819 return 0;
2820}
2821
2822static int nvme_resume(struct device *dev)
2823{
2824 struct pci_dev *pdev = to_pci_dev(dev);
2825 struct nvme_dev *ndev = pci_get_drvdata(pdev);
cd638946 2826
0a7385ad 2827 schedule_work(&ndev->probe_work);
9a6b9458 2828 return 0;
cd638946 2829}
671a6018 2830#endif
cd638946
KB
2831
2832static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
b60503ba 2833
1d352035 2834static const struct pci_error_handlers nvme_err_handler = {
b60503ba
MW
2835 .error_detected = nvme_error_detected,
2836 .mmio_enabled = nvme_dump_registers,
2837 .link_reset = nvme_link_reset,
2838 .slot_reset = nvme_slot_reset,
2839 .resume = nvme_error_resume,
f0d54a54 2840 .reset_notify = nvme_reset_notify,
b60503ba
MW
2841};
2842
2843/* Move to pci_ids.h later */
2844#define PCI_CLASS_STORAGE_EXPRESS 0x010802
2845
6eb0d698 2846static const struct pci_device_id nvme_id_table[] = {
106198ed
CH
2847 { PCI_VDEVICE(INTEL, 0x0953),
2848 .driver_data = NVME_QUIRK_STRIPE_SIZE, },
b60503ba 2849 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
c74dc780 2850 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
b60503ba
MW
2851 { 0, }
2852};
2853MODULE_DEVICE_TABLE(pci, nvme_id_table);
2854
2855static struct pci_driver nvme_driver = {
2856 .name = "nvme",
2857 .id_table = nvme_id_table,
2858 .probe = nvme_probe,
8d85fce7 2859 .remove = nvme_remove,
09ece142 2860 .shutdown = nvme_shutdown,
cd638946
KB
2861 .driver = {
2862 .pm = &nvme_dev_pm_ops,
2863 },
b60503ba
MW
2864 .err_handler = &nvme_err_handler,
2865};
2866
2867static int __init nvme_init(void)
2868{
0ac13140 2869 int result;
1fa6aead 2870
b9afca3e 2871 init_waitqueue_head(&nvme_kthread_wait);
b60503ba 2872
9a6b9458
KB
2873 nvme_workq = create_singlethread_workqueue("nvme");
2874 if (!nvme_workq)
b9afca3e 2875 return -ENOMEM;
9a6b9458 2876
5c42ea16
KB
2877 result = register_blkdev(nvme_major, "nvme");
2878 if (result < 0)
9a6b9458 2879 goto kill_workq;
5c42ea16 2880 else if (result > 0)
0ac13140 2881 nvme_major = result;
b60503ba 2882
b3fffdef
KB
2883 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2884 &nvme_dev_fops);
2885 if (result < 0)
2886 goto unregister_blkdev;
2887 else if (result > 0)
2888 nvme_char_major = result;
2889
2890 nvme_class = class_create(THIS_MODULE, "nvme");
c727040b
AK
2891 if (IS_ERR(nvme_class)) {
2892 result = PTR_ERR(nvme_class);
b3fffdef 2893 goto unregister_chrdev;
c727040b 2894 }
b3fffdef 2895
f3db22fe
KB
2896 result = pci_register_driver(&nvme_driver);
2897 if (result)
b3fffdef 2898 goto destroy_class;
1fa6aead 2899 return 0;
b60503ba 2900
b3fffdef
KB
2901 destroy_class:
2902 class_destroy(nvme_class);
2903 unregister_chrdev:
2904 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
1fa6aead 2905 unregister_blkdev:
b60503ba 2906 unregister_blkdev(nvme_major, "nvme");
9a6b9458
KB
2907 kill_workq:
2908 destroy_workqueue(nvme_workq);
b60503ba
MW
2909 return result;
2910}
2911
2912static void __exit nvme_exit(void)
2913{
2914 pci_unregister_driver(&nvme_driver);
2915 unregister_blkdev(nvme_major, "nvme");
9a6b9458 2916 destroy_workqueue(nvme_workq);
b3fffdef
KB
2917 class_destroy(nvme_class);
2918 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
b9afca3e 2919 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
21bd78bc 2920 _nvme_check_size();
b60503ba
MW
2921}
2922
2923MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2924MODULE_LICENSE("GPL");
c78b4713 2925MODULE_VERSION("1.0");
b60503ba
MW
2926module_init(nvme_init);
2927module_exit(nvme_exit);