2 * NVM Express device driver
3 * Copyright (c) 2011-2014, Intel Corporation.
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.
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
15 #include <linux/bitops.h>
16 #include <linux/blkdev.h>
17 #include <linux/blk-mq.h>
18 #include <linux/cpu.h>
19 #include <linux/delay.h>
20 #include <linux/errno.h>
22 #include <linux/genhd.h>
23 #include <linux/hdreg.h>
24 #include <linux/idr.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
28 #include <linux/kdev_t.h>
29 #include <linux/kthread.h>
30 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/mutex.h>
35 #include <linux/pci.h>
36 #include <linux/poison.h>
37 #include <linux/ptrace.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/t10-pi.h>
41 #include <linux/types.h>
42 #include <linux/io-64-nonatomic-lo-hi.h>
43 #include <asm/unaligned.h>
47 #define NVME_Q_DEPTH 1024
48 #define NVME_AQ_DEPTH 256
49 #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
50 #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
52 unsigned char admin_timeout = 60;
53 module_param(admin_timeout, byte, 0644);
54 MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
56 unsigned char nvme_io_timeout = 30;
57 module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
58 MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
60 unsigned char shutdown_timeout = 5;
61 module_param(shutdown_timeout, byte, 0644);
62 MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
64 static int use_threaded_interrupts;
65 module_param(use_threaded_interrupts, int, 0);
67 static bool use_cmb_sqes = true;
68 module_param(use_cmb_sqes, bool, 0644);
69 MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
71 static LIST_HEAD(dev_list);
72 static struct task_struct *nvme_thread;
73 static struct workqueue_struct *nvme_workq;
74 static wait_queue_head_t nvme_kthread_wait;
80 static int nvme_reset(struct nvme_dev *dev);
81 static void nvme_process_cq(struct nvme_queue *nvmeq);
82 static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod);
83 static void nvme_remove_dead_ctrl(struct nvme_dev *dev);
84 static void nvme_dev_shutdown(struct nvme_dev *dev);
86 struct async_cmd_info {
87 struct kthread_work work;
88 struct kthread_worker *worker;
96 * Represents an NVM Express device. Each nvme_dev is a PCI function.
99 struct list_head node;
100 struct nvme_queue **queues;
101 struct blk_mq_tag_set tagset;
102 struct blk_mq_tag_set admin_tagset;
105 struct dma_pool *prp_page_pool;
106 struct dma_pool *prp_small_pool;
107 unsigned queue_count;
108 unsigned online_queues;
112 struct msix_entry *entry;
114 struct work_struct reset_work;
115 struct work_struct scan_work;
116 struct work_struct remove_work;
117 struct mutex shutdown_lock;
120 dma_addr_t cmb_dma_addr;
124 #define NVME_CTRL_RESETTING 0
126 struct nvme_ctrl ctrl;
129 static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
131 return container_of(ctrl, struct nvme_dev, ctrl);
135 * An NVM Express queue. Each device has at least two (one for admin
136 * commands and one for I/O commands).
139 struct device *q_dmadev;
140 struct nvme_dev *dev;
141 char irqname[24]; /* nvme4294967295-65535\0 */
143 struct nvme_command *sq_cmds;
144 struct nvme_command __iomem *sq_cmds_io;
145 volatile struct nvme_completion *cqes;
146 struct blk_mq_tags **tags;
147 dma_addr_t sq_dma_addr;
148 dma_addr_t cq_dma_addr;
158 struct async_cmd_info cmdinfo;
162 * The nvme_iod describes the data in an I/O, including the list of PRP
163 * entries. You can't see it in this data structure because C doesn't let
164 * me express that. Use nvme_alloc_iod to ensure there's enough space
165 * allocated to store the PRP list.
168 unsigned long private; /* For the use of the submitter of the I/O */
169 int npages; /* In the PRP list. 0 means small pool in use */
170 int offset; /* Of PRP list */
171 int nents; /* Used in scatterlist */
172 int length; /* Of data, in bytes */
173 dma_addr_t first_dma;
174 struct scatterlist meta_sg[1]; /* metadata requires single contiguous buffer */
175 struct scatterlist sg[0];
179 * Check we didin't inadvertently grow the command struct
181 static inline void _nvme_check_size(void)
183 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
184 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
185 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
186 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
187 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
188 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
189 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
190 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
191 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
192 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
193 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
194 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
197 typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
198 struct nvme_completion *);
200 struct nvme_cmd_info {
201 nvme_completion_fn fn;
204 struct nvme_queue *nvmeq;
205 struct nvme_iod iod[0];
209 * Max size of iod being embedded in the request payload
211 #define NVME_INT_PAGES 2
212 #define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
213 #define NVME_INT_MASK 0x01
216 * Will slightly overestimate the number of pages needed. This is OK
217 * as it only leads to a small amount of wasted memory for the lifetime of
220 static int nvme_npages(unsigned size, struct nvme_dev *dev)
222 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
223 dev->ctrl.page_size);
224 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
227 static unsigned int nvme_cmd_size(struct nvme_dev *dev)
229 unsigned int ret = sizeof(struct nvme_cmd_info);
231 ret += sizeof(struct nvme_iod);
232 ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
233 ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
238 static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
239 unsigned int hctx_idx)
241 struct nvme_dev *dev = data;
242 struct nvme_queue *nvmeq = dev->queues[0];
244 WARN_ON(hctx_idx != 0);
245 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
246 WARN_ON(nvmeq->tags);
248 hctx->driver_data = nvmeq;
249 nvmeq->tags = &dev->admin_tagset.tags[0];
253 static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
255 struct nvme_queue *nvmeq = hctx->driver_data;
260 static int nvme_admin_init_request(void *data, struct request *req,
261 unsigned int hctx_idx, unsigned int rq_idx,
262 unsigned int numa_node)
264 struct nvme_dev *dev = data;
265 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
266 struct nvme_queue *nvmeq = dev->queues[0];
273 static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
274 unsigned int hctx_idx)
276 struct nvme_dev *dev = data;
277 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
280 nvmeq->tags = &dev->tagset.tags[hctx_idx];
282 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
283 hctx->driver_data = nvmeq;
287 static int nvme_init_request(void *data, struct request *req,
288 unsigned int hctx_idx, unsigned int rq_idx,
289 unsigned int numa_node)
291 struct nvme_dev *dev = data;
292 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
293 struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
300 static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
301 nvme_completion_fn handler)
306 blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
309 static void *iod_get_private(struct nvme_iod *iod)
311 return (void *) (iod->private & ~0x1UL);
315 * If bit 0 is set, the iod is embedded in the request payload.
317 static bool iod_should_kfree(struct nvme_iod *iod)
319 return (iod->private & NVME_INT_MASK) == 0;
322 /* Special values must be less than 0x1000 */
323 #define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
324 #define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
325 #define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
326 #define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
328 static void special_completion(struct nvme_queue *nvmeq, void *ctx,
329 struct nvme_completion *cqe)
331 if (ctx == CMD_CTX_CANCELLED)
333 if (ctx == CMD_CTX_COMPLETED) {
334 dev_warn(nvmeq->q_dmadev,
335 "completed id %d twice on queue %d\n",
336 cqe->command_id, le16_to_cpup(&cqe->sq_id));
339 if (ctx == CMD_CTX_INVALID) {
340 dev_warn(nvmeq->q_dmadev,
341 "invalid id %d completed on queue %d\n",
342 cqe->command_id, le16_to_cpup(&cqe->sq_id));
345 dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
348 static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
355 cmd->fn = special_completion;
356 cmd->ctx = CMD_CTX_CANCELLED;
360 static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
361 struct nvme_completion *cqe)
363 u32 result = le32_to_cpup(&cqe->result);
364 u16 status = le16_to_cpup(&cqe->status) >> 1;
366 if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
367 ++nvmeq->dev->ctrl.event_limit;
368 if (status != NVME_SC_SUCCESS)
371 switch (result & 0xff07) {
372 case NVME_AER_NOTICE_NS_CHANGED:
373 dev_info(nvmeq->q_dmadev, "rescanning\n");
374 queue_work(nvme_workq, &nvmeq->dev->scan_work);
376 dev_warn(nvmeq->q_dmadev, "async event result %08x\n", result);
380 static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
381 struct nvme_completion *cqe)
383 struct request *req = ctx;
385 u16 status = le16_to_cpup(&cqe->status) >> 1;
386 u32 result = le32_to_cpup(&cqe->result);
388 blk_mq_free_request(req);
390 dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
391 atomic_inc(&nvmeq->dev->ctrl.abort_limit);
394 static void async_completion(struct nvme_queue *nvmeq, void *ctx,
395 struct nvme_completion *cqe)
397 struct async_cmd_info *cmdinfo = ctx;
398 cmdinfo->result = le32_to_cpup(&cqe->result);
399 cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
400 queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
401 blk_mq_free_request(cmdinfo->req);
404 static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
407 struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag);
409 return blk_mq_rq_to_pdu(req);
413 * Called with local interrupts disabled and the q_lock held. May not sleep.
415 static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
416 nvme_completion_fn *fn)
418 struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
420 if (tag >= nvmeq->q_depth) {
421 *fn = special_completion;
422 return CMD_CTX_INVALID;
427 cmd->fn = special_completion;
428 cmd->ctx = CMD_CTX_COMPLETED;
433 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
434 * @nvmeq: The queue to use
435 * @cmd: The command to send
437 * Safe to use from interrupt context
439 static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
440 struct nvme_command *cmd)
442 u16 tail = nvmeq->sq_tail;
444 if (nvmeq->sq_cmds_io)
445 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
447 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
449 if (++tail == nvmeq->q_depth)
451 writel(tail, nvmeq->q_db);
452 nvmeq->sq_tail = tail;
455 static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
458 spin_lock_irqsave(&nvmeq->q_lock, flags);
459 __nvme_submit_cmd(nvmeq, cmd);
460 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
463 static __le64 **iod_list(struct nvme_iod *iod)
465 return ((void *)iod) + iod->offset;
468 static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
469 unsigned nseg, unsigned long private)
471 iod->private = private;
472 iod->offset = offsetof(struct nvme_iod, sg[nseg]);
474 iod->length = nbytes;
478 static struct nvme_iod *
479 __nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
480 unsigned long priv, gfp_t gfp)
482 struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
483 sizeof(__le64 *) * nvme_npages(bytes, dev) +
484 sizeof(struct scatterlist) * nseg, gfp);
487 iod_init(iod, bytes, nseg, priv);
492 static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
495 unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
496 sizeof(struct nvme_dsm_range);
497 struct nvme_iod *iod;
499 if (rq->nr_phys_segments <= NVME_INT_PAGES &&
500 size <= NVME_INT_BYTES(dev)) {
501 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
504 iod_init(iod, size, rq->nr_phys_segments,
505 (unsigned long) rq | NVME_INT_MASK);
509 return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
510 (unsigned long) rq, gfp);
513 static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
515 const int last_prp = dev->ctrl.page_size / 8 - 1;
517 __le64 **list = iod_list(iod);
518 dma_addr_t prp_dma = iod->first_dma;
520 if (iod->npages == 0)
521 dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
522 for (i = 0; i < iod->npages; i++) {
523 __le64 *prp_list = list[i];
524 dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
525 dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
526 prp_dma = next_prp_dma;
529 if (iod_should_kfree(iod))
533 #ifdef CONFIG_BLK_DEV_INTEGRITY
534 static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
536 if (be32_to_cpu(pi->ref_tag) == v)
537 pi->ref_tag = cpu_to_be32(p);
540 static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
542 if (be32_to_cpu(pi->ref_tag) == p)
543 pi->ref_tag = cpu_to_be32(v);
547 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
549 * The virtual start sector is the one that was originally submitted by the
550 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
551 * start sector may be different. Remap protection information to match the
552 * physical LBA on writes, and back to the original seed on reads.
554 * Type 0 and 3 do not have a ref tag, so no remapping required.
556 static void nvme_dif_remap(struct request *req,
557 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
559 struct nvme_ns *ns = req->rq_disk->private_data;
560 struct bio_integrity_payload *bip;
561 struct t10_pi_tuple *pi;
563 u32 i, nlb, ts, phys, virt;
565 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
568 bip = bio_integrity(req->bio);
572 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
575 virt = bip_get_seed(bip);
576 phys = nvme_block_nr(ns, blk_rq_pos(req));
577 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
578 ts = ns->disk->queue->integrity.tuple_size;
580 for (i = 0; i < nlb; i++, virt++, phys++) {
581 pi = (struct t10_pi_tuple *)p;
582 dif_swap(phys, virt, pi);
587 #else /* CONFIG_BLK_DEV_INTEGRITY */
588 static void nvme_dif_remap(struct request *req,
589 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
592 static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
595 static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
600 static void req_completion(struct nvme_queue *nvmeq, void *ctx,
601 struct nvme_completion *cqe)
603 struct nvme_iod *iod = ctx;
604 struct request *req = iod_get_private(iod);
605 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
606 u16 status = le16_to_cpup(&cqe->status) >> 1;
609 if (unlikely(status)) {
610 if (nvme_req_needs_retry(req, status)) {
611 nvme_unmap_data(nvmeq->dev, iod);
612 nvme_requeue_req(req);
616 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
617 if (cmd_rq->ctx == CMD_CTX_CANCELLED)
618 error = NVME_SC_CANCELLED;
622 error = nvme_error_status(status);
626 if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
627 u32 result = le32_to_cpup(&cqe->result);
628 req->special = (void *)(uintptr_t)result;
632 dev_warn(nvmeq->dev->dev,
633 "completing aborted command with status:%04x\n",
636 nvme_unmap_data(nvmeq->dev, iod);
637 blk_mq_complete_request(req, error);
640 static bool nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
643 struct dma_pool *pool;
644 int length = total_len;
645 struct scatterlist *sg = iod->sg;
646 int dma_len = sg_dma_len(sg);
647 u64 dma_addr = sg_dma_address(sg);
648 u32 page_size = dev->ctrl.page_size;
649 int offset = dma_addr & (page_size - 1);
651 __le64 **list = iod_list(iod);
655 length -= (page_size - offset);
659 dma_len -= (page_size - offset);
661 dma_addr += (page_size - offset);
664 dma_addr = sg_dma_address(sg);
665 dma_len = sg_dma_len(sg);
668 if (length <= page_size) {
669 iod->first_dma = dma_addr;
673 nprps = DIV_ROUND_UP(length, page_size);
674 if (nprps <= (256 / 8)) {
675 pool = dev->prp_small_pool;
678 pool = dev->prp_page_pool;
682 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
684 iod->first_dma = dma_addr;
689 iod->first_dma = prp_dma;
692 if (i == page_size >> 3) {
693 __le64 *old_prp_list = prp_list;
694 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
697 list[iod->npages++] = prp_list;
698 prp_list[0] = old_prp_list[i - 1];
699 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
702 prp_list[i++] = cpu_to_le64(dma_addr);
703 dma_len -= page_size;
704 dma_addr += page_size;
712 dma_addr = sg_dma_address(sg);
713 dma_len = sg_dma_len(sg);
719 static int nvme_map_data(struct nvme_dev *dev, struct nvme_iod *iod,
720 struct nvme_command *cmnd)
722 struct request *req = iod_get_private(iod);
723 struct request_queue *q = req->q;
724 enum dma_data_direction dma_dir = rq_data_dir(req) ?
725 DMA_TO_DEVICE : DMA_FROM_DEVICE;
726 int ret = BLK_MQ_RQ_QUEUE_ERROR;
728 sg_init_table(iod->sg, req->nr_phys_segments);
729 iod->nents = blk_rq_map_sg(q, req, iod->sg);
733 ret = BLK_MQ_RQ_QUEUE_BUSY;
734 if (!dma_map_sg(dev->dev, iod->sg, iod->nents, dma_dir))
737 if (!nvme_setup_prps(dev, iod, blk_rq_bytes(req)))
740 ret = BLK_MQ_RQ_QUEUE_ERROR;
741 if (blk_integrity_rq(req)) {
742 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
745 sg_init_table(iod->meta_sg, 1);
746 if (blk_rq_map_integrity_sg(q, req->bio, iod->meta_sg) != 1)
749 if (rq_data_dir(req))
750 nvme_dif_remap(req, nvme_dif_prep);
752 if (!dma_map_sg(dev->dev, iod->meta_sg, 1, dma_dir))
756 cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
757 cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
758 if (blk_integrity_rq(req))
759 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(iod->meta_sg));
760 return BLK_MQ_RQ_QUEUE_OK;
763 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
768 static void nvme_unmap_data(struct nvme_dev *dev, struct nvme_iod *iod)
770 struct request *req = iod_get_private(iod);
771 enum dma_data_direction dma_dir = rq_data_dir(req) ?
772 DMA_TO_DEVICE : DMA_FROM_DEVICE;
775 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
776 if (blk_integrity_rq(req)) {
777 if (!rq_data_dir(req))
778 nvme_dif_remap(req, nvme_dif_complete);
779 dma_unmap_sg(dev->dev, iod->meta_sg, 1, dma_dir);
783 nvme_free_iod(dev, iod);
787 * We reuse the small pool to allocate the 16-byte range here as it is not
788 * worth having a special pool for these or additional cases to handle freeing
791 static int nvme_setup_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
792 struct nvme_iod *iod, struct nvme_command *cmnd)
794 struct request *req = iod_get_private(iod);
795 struct nvme_dsm_range *range;
797 range = dma_pool_alloc(nvmeq->dev->prp_small_pool, GFP_ATOMIC,
800 return BLK_MQ_RQ_QUEUE_BUSY;
801 iod_list(iod)[0] = (__le64 *)range;
804 range->cattr = cpu_to_le32(0);
805 range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
806 range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
808 memset(cmnd, 0, sizeof(*cmnd));
809 cmnd->dsm.opcode = nvme_cmd_dsm;
810 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
811 cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
813 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
814 return BLK_MQ_RQ_QUEUE_OK;
818 * NOTE: ns is NULL when called on the admin queue.
820 static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
821 const struct blk_mq_queue_data *bd)
823 struct nvme_ns *ns = hctx->queue->queuedata;
824 struct nvme_queue *nvmeq = hctx->driver_data;
825 struct nvme_dev *dev = nvmeq->dev;
826 struct request *req = bd->rq;
827 struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
828 struct nvme_iod *iod;
829 struct nvme_command cmnd;
830 int ret = BLK_MQ_RQ_QUEUE_OK;
833 * If formated with metadata, require the block layer provide a buffer
834 * unless this namespace is formated such that the metadata can be
835 * stripped/generated by the controller with PRACT=1.
837 if (ns && ns->ms && !blk_integrity_rq(req)) {
838 if (!(ns->pi_type && ns->ms == 8) &&
839 req->cmd_type != REQ_TYPE_DRV_PRIV) {
840 blk_mq_complete_request(req, -EFAULT);
841 return BLK_MQ_RQ_QUEUE_OK;
845 iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
847 return BLK_MQ_RQ_QUEUE_BUSY;
849 if (req->cmd_flags & REQ_DISCARD) {
850 ret = nvme_setup_discard(nvmeq, ns, iod, &cmnd);
852 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
853 memcpy(&cmnd, req->cmd, sizeof(cmnd));
854 else if (req->cmd_flags & REQ_FLUSH)
855 nvme_setup_flush(ns, &cmnd);
857 nvme_setup_rw(ns, req, &cmnd);
859 if (req->nr_phys_segments)
860 ret = nvme_map_data(dev, iod, &cmnd);
866 cmnd.common.command_id = req->tag;
867 nvme_set_info(cmd, iod, req_completion);
869 spin_lock_irq(&nvmeq->q_lock);
870 __nvme_submit_cmd(nvmeq, &cmnd);
871 nvme_process_cq(nvmeq);
872 spin_unlock_irq(&nvmeq->q_lock);
873 return BLK_MQ_RQ_QUEUE_OK;
875 nvme_free_iod(dev, iod);
879 static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
883 head = nvmeq->cq_head;
884 phase = nvmeq->cq_phase;
888 nvme_completion_fn fn;
889 struct nvme_completion cqe = nvmeq->cqes[head];
890 if ((le16_to_cpu(cqe.status) & 1) != phase)
892 nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
893 if (++head == nvmeq->q_depth) {
897 if (tag && *tag == cqe.command_id)
899 ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
900 fn(nvmeq, ctx, &cqe);
903 /* If the controller ignores the cq head doorbell and continuously
904 * writes to the queue, it is theoretically possible to wrap around
905 * the queue twice and mistakenly return IRQ_NONE. Linux only
906 * requires that 0.1% of your interrupts are handled, so this isn't
909 if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
912 if (likely(nvmeq->cq_vector >= 0))
913 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
914 nvmeq->cq_head = head;
915 nvmeq->cq_phase = phase;
920 static void nvme_process_cq(struct nvme_queue *nvmeq)
922 __nvme_process_cq(nvmeq, NULL);
925 static irqreturn_t nvme_irq(int irq, void *data)
928 struct nvme_queue *nvmeq = data;
929 spin_lock(&nvmeq->q_lock);
930 nvme_process_cq(nvmeq);
931 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
933 spin_unlock(&nvmeq->q_lock);
937 static irqreturn_t nvme_irq_check(int irq, void *data)
939 struct nvme_queue *nvmeq = data;
940 struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
941 if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
943 return IRQ_WAKE_THREAD;
946 static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
948 struct nvme_queue *nvmeq = hctx->driver_data;
950 if ((le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
952 spin_lock_irq(&nvmeq->q_lock);
953 __nvme_process_cq(nvmeq, &tag);
954 spin_unlock_irq(&nvmeq->q_lock);
963 static int nvme_submit_async_admin_req(struct nvme_dev *dev)
965 struct nvme_queue *nvmeq = dev->queues[0];
966 struct nvme_command c;
967 struct nvme_cmd_info *cmd_info;
970 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
971 BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED);
975 req->cmd_flags |= REQ_NO_TIMEOUT;
976 cmd_info = blk_mq_rq_to_pdu(req);
977 nvme_set_info(cmd_info, NULL, async_req_completion);
979 memset(&c, 0, sizeof(c));
980 c.common.opcode = nvme_admin_async_event;
981 c.common.command_id = req->tag;
983 blk_mq_free_request(req);
984 __nvme_submit_cmd(nvmeq, &c);
988 static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
989 struct nvme_command *cmd,
990 struct async_cmd_info *cmdinfo, unsigned timeout)
992 struct nvme_queue *nvmeq = dev->queues[0];
994 struct nvme_cmd_info *cmd_rq;
996 req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE, 0);
1000 req->timeout = timeout;
1001 cmd_rq = blk_mq_rq_to_pdu(req);
1003 nvme_set_info(cmd_rq, cmdinfo, async_completion);
1004 cmdinfo->status = -EINTR;
1006 cmd->common.command_id = req->tag;
1008 nvme_submit_cmd(nvmeq, cmd);
1012 static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1014 struct nvme_command c;
1016 memset(&c, 0, sizeof(c));
1017 c.delete_queue.opcode = opcode;
1018 c.delete_queue.qid = cpu_to_le16(id);
1020 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1023 static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1024 struct nvme_queue *nvmeq)
1026 struct nvme_command c;
1027 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1030 * Note: we (ab)use the fact the the prp fields survive if no data
1031 * is attached to the request.
1033 memset(&c, 0, sizeof(c));
1034 c.create_cq.opcode = nvme_admin_create_cq;
1035 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1036 c.create_cq.cqid = cpu_to_le16(qid);
1037 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1038 c.create_cq.cq_flags = cpu_to_le16(flags);
1039 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1041 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1044 static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1045 struct nvme_queue *nvmeq)
1047 struct nvme_command c;
1048 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
1051 * Note: we (ab)use the fact the the prp fields survive if no data
1052 * is attached to the request.
1054 memset(&c, 0, sizeof(c));
1055 c.create_sq.opcode = nvme_admin_create_sq;
1056 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1057 c.create_sq.sqid = cpu_to_le16(qid);
1058 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1059 c.create_sq.sq_flags = cpu_to_le16(flags);
1060 c.create_sq.cqid = cpu_to_le16(qid);
1062 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1065 static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1067 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1070 static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1072 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1075 static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
1077 struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
1078 struct nvme_queue *nvmeq = cmd_rq->nvmeq;
1079 struct nvme_dev *dev = nvmeq->dev;
1080 struct request *abort_req;
1081 struct nvme_cmd_info *abort_cmd;
1082 struct nvme_command cmd;
1085 * Shutdown immediately if controller times out while starting. The
1086 * reset work will see the pci device disabled when it gets the forced
1087 * cancellation error. All outstanding requests are completed on
1088 * shutdown, so we return BLK_EH_HANDLED.
1090 if (test_bit(NVME_CTRL_RESETTING, &dev->flags)) {
1092 "I/O %d QID %d timeout, disable controller\n",
1093 req->tag, nvmeq->qid);
1094 nvme_dev_shutdown(dev);
1095 req->errors = NVME_SC_CANCELLED;
1096 return BLK_EH_HANDLED;
1100 * Shutdown the controller immediately and schedule a reset if the
1101 * command was already aborted once before and still hasn't been
1102 * returned to the driver, or if this is the admin queue.
1104 if (!nvmeq->qid || cmd_rq->aborted) {
1106 "I/O %d QID %d timeout, reset controller\n",
1107 req->tag, nvmeq->qid);
1108 nvme_dev_shutdown(dev);
1109 queue_work(nvme_workq, &dev->reset_work);
1112 * Mark the request as handled, since the inline shutdown
1113 * forces all outstanding requests to complete.
1115 req->errors = NVME_SC_CANCELLED;
1116 return BLK_EH_HANDLED;
1119 if (atomic_dec_and_test(&dev->ctrl.abort_limit))
1120 return BLK_EH_RESET_TIMER;
1122 abort_req = blk_mq_alloc_request(dev->ctrl.admin_q, WRITE,
1124 if (IS_ERR(abort_req)) {
1125 atomic_inc(&dev->ctrl.abort_limit);
1126 return BLK_EH_RESET_TIMER;
1129 abort_cmd = blk_mq_rq_to_pdu(abort_req);
1130 nvme_set_info(abort_cmd, abort_req, abort_completion);
1132 memset(&cmd, 0, sizeof(cmd));
1133 cmd.abort.opcode = nvme_admin_abort_cmd;
1134 cmd.abort.cid = req->tag;
1135 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
1136 cmd.abort.command_id = abort_req->tag;
1138 cmd_rq->aborted = 1;
1140 dev_warn(nvmeq->q_dmadev, "I/O %d QID %d timeout, aborting\n",
1141 req->tag, nvmeq->qid);
1142 nvme_submit_cmd(dev->queues[0], &cmd);
1145 * The aborted req will be completed on receiving the abort req.
1146 * We enable the timer again. If hit twice, it'll cause a device reset,
1147 * as the device then is in a faulty state.
1149 return BLK_EH_RESET_TIMER;
1152 static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
1154 struct nvme_queue *nvmeq = data;
1156 nvme_completion_fn fn;
1157 struct nvme_cmd_info *cmd;
1158 struct nvme_completion cqe;
1160 if (!blk_mq_request_started(req))
1163 cmd = blk_mq_rq_to_pdu(req);
1165 if (cmd->ctx == CMD_CTX_CANCELLED)
1168 if (blk_queue_dying(req->q))
1169 cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
1171 cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
1174 dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
1175 req->tag, nvmeq->qid);
1176 ctx = cancel_cmd_info(cmd, &fn);
1177 fn(nvmeq, ctx, &cqe);
1180 static void nvme_free_queue(struct nvme_queue *nvmeq)
1182 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1183 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1185 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1186 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1190 static void nvme_free_queues(struct nvme_dev *dev, int lowest)
1194 for (i = dev->queue_count - 1; i >= lowest; i--) {
1195 struct nvme_queue *nvmeq = dev->queues[i];
1197 dev->queues[i] = NULL;
1198 nvme_free_queue(nvmeq);
1203 * nvme_suspend_queue - put queue into suspended state
1204 * @nvmeq - queue to suspend
1206 static int nvme_suspend_queue(struct nvme_queue *nvmeq)
1210 spin_lock_irq(&nvmeq->q_lock);
1211 if (nvmeq->cq_vector == -1) {
1212 spin_unlock_irq(&nvmeq->q_lock);
1215 vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
1216 nvmeq->dev->online_queues--;
1217 nvmeq->cq_vector = -1;
1218 spin_unlock_irq(&nvmeq->q_lock);
1220 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
1221 blk_mq_freeze_queue_start(nvmeq->dev->ctrl.admin_q);
1223 irq_set_affinity_hint(vector, NULL);
1224 free_irq(vector, nvmeq);
1229 static void nvme_clear_queue(struct nvme_queue *nvmeq)
1231 spin_lock_irq(&nvmeq->q_lock);
1232 if (nvmeq->tags && *nvmeq->tags)
1233 blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
1234 spin_unlock_irq(&nvmeq->q_lock);
1237 static void nvme_disable_queue(struct nvme_dev *dev, int qid)
1239 struct nvme_queue *nvmeq = dev->queues[qid];
1243 if (nvme_suspend_queue(nvmeq))
1246 /* Don't tell the adapter to delete the admin queue.
1247 * Don't tell a removed adapter to delete IO queues. */
1248 if (qid && readl(dev->bar + NVME_REG_CSTS) != -1) {
1249 adapter_delete_sq(dev, qid);
1250 adapter_delete_cq(dev, qid);
1253 spin_lock_irq(&nvmeq->q_lock);
1254 nvme_process_cq(nvmeq);
1255 spin_unlock_irq(&nvmeq->q_lock);
1258 static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1261 int q_depth = dev->q_depth;
1262 unsigned q_size_aligned = roundup(q_depth * entry_size,
1263 dev->ctrl.page_size);
1265 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
1266 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
1267 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
1268 q_depth = div_u64(mem_per_q, entry_size);
1271 * Ensure the reduced q_depth is above some threshold where it
1272 * would be better to map queues in system memory with the
1282 static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1285 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
1286 unsigned offset = (qid - 1) * roundup(SQ_SIZE(depth),
1287 dev->ctrl.page_size);
1288 nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
1289 nvmeq->sq_cmds_io = dev->cmb + offset;
1291 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1292 &nvmeq->sq_dma_addr, GFP_KERNEL);
1293 if (!nvmeq->sq_cmds)
1300 static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
1303 struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
1307 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
1308 &nvmeq->cq_dma_addr, GFP_KERNEL);
1312 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
1315 nvmeq->q_dmadev = dev->dev;
1317 snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
1318 dev->ctrl.instance, qid);
1319 spin_lock_init(&nvmeq->q_lock);
1321 nvmeq->cq_phase = 1;
1322 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
1323 nvmeq->q_depth = depth;
1325 nvmeq->cq_vector = -1;
1326 dev->queues[qid] = nvmeq;
1328 /* make sure queue descriptor is set before queue count, for kthread */
1335 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
1336 nvmeq->cq_dma_addr);
1342 static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1345 if (use_threaded_interrupts)
1346 return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
1347 nvme_irq_check, nvme_irq, IRQF_SHARED,
1349 return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
1350 IRQF_SHARED, name, nvmeq);
1353 static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
1355 struct nvme_dev *dev = nvmeq->dev;
1357 spin_lock_irq(&nvmeq->q_lock);
1360 nvmeq->cq_phase = 1;
1361 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
1362 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1363 dev->online_queues++;
1364 spin_unlock_irq(&nvmeq->q_lock);
1367 static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1369 struct nvme_dev *dev = nvmeq->dev;
1372 nvmeq->cq_vector = qid - 1;
1373 result = adapter_alloc_cq(dev, qid, nvmeq);
1377 result = adapter_alloc_sq(dev, qid, nvmeq);
1381 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
1385 nvme_init_queue(nvmeq, qid);
1389 adapter_delete_sq(dev, qid);
1391 adapter_delete_cq(dev, qid);
1395 static struct blk_mq_ops nvme_mq_admin_ops = {
1396 .queue_rq = nvme_queue_rq,
1397 .map_queue = blk_mq_map_queue,
1398 .init_hctx = nvme_admin_init_hctx,
1399 .exit_hctx = nvme_admin_exit_hctx,
1400 .init_request = nvme_admin_init_request,
1401 .timeout = nvme_timeout,
1404 static struct blk_mq_ops nvme_mq_ops = {
1405 .queue_rq = nvme_queue_rq,
1406 .map_queue = blk_mq_map_queue,
1407 .init_hctx = nvme_init_hctx,
1408 .init_request = nvme_init_request,
1409 .timeout = nvme_timeout,
1413 static void nvme_dev_remove_admin(struct nvme_dev *dev)
1415 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
1416 blk_cleanup_queue(dev->ctrl.admin_q);
1417 blk_mq_free_tag_set(&dev->admin_tagset);
1421 static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1423 if (!dev->ctrl.admin_q) {
1424 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1425 dev->admin_tagset.nr_hw_queues = 1;
1426 dev->admin_tagset.queue_depth = NVME_AQ_DEPTH;
1427 dev->admin_tagset.reserved_tags = 1;
1428 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
1429 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
1430 dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
1431 dev->admin_tagset.driver_data = dev;
1433 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1436 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1437 if (IS_ERR(dev->ctrl.admin_q)) {
1438 blk_mq_free_tag_set(&dev->admin_tagset);
1441 if (!blk_get_queue(dev->ctrl.admin_q)) {
1442 nvme_dev_remove_admin(dev);
1443 dev->ctrl.admin_q = NULL;
1447 blk_mq_unfreeze_queue(dev->ctrl.admin_q);
1452 static int nvme_configure_admin_queue(struct nvme_dev *dev)
1456 u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
1457 struct nvme_queue *nvmeq;
1459 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1) ?
1460 NVME_CAP_NSSRC(cap) : 0;
1462 if (dev->subsystem &&
1463 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1464 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
1466 result = nvme_disable_ctrl(&dev->ctrl, cap);
1470 nvmeq = dev->queues[0];
1472 nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
1477 aqa = nvmeq->q_depth - 1;
1480 writel(aqa, dev->bar + NVME_REG_AQA);
1481 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1482 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
1484 result = nvme_enable_ctrl(&dev->ctrl, cap);
1488 nvmeq->cq_vector = 0;
1489 result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
1491 nvmeq->cq_vector = -1;
1498 nvme_free_queues(dev, 0);
1502 static int nvme_kthread(void *data)
1504 struct nvme_dev *dev, *next;
1506 while (!kthread_should_stop()) {
1507 set_current_state(TASK_INTERRUPTIBLE);
1508 spin_lock(&dev_list_lock);
1509 list_for_each_entry_safe(dev, next, &dev_list, node) {
1511 u32 csts = readl(dev->bar + NVME_REG_CSTS);
1514 * Skip controllers currently under reset.
1516 if (work_pending(&dev->reset_work) || work_busy(&dev->reset_work))
1519 if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
1520 csts & NVME_CSTS_CFS) {
1521 if (queue_work(nvme_workq, &dev->reset_work)) {
1523 "Failed status: %x, reset controller\n",
1524 readl(dev->bar + NVME_REG_CSTS));
1528 for (i = 0; i < dev->queue_count; i++) {
1529 struct nvme_queue *nvmeq = dev->queues[i];
1532 spin_lock_irq(&nvmeq->q_lock);
1533 nvme_process_cq(nvmeq);
1535 while (i == 0 && dev->ctrl.event_limit > 0) {
1536 if (nvme_submit_async_admin_req(dev))
1538 dev->ctrl.event_limit--;
1540 spin_unlock_irq(&nvmeq->q_lock);
1543 spin_unlock(&dev_list_lock);
1544 schedule_timeout(round_jiffies_relative(HZ));
1549 static int nvme_create_io_queues(struct nvme_dev *dev)
1554 for (i = dev->queue_count; i <= dev->max_qid; i++) {
1555 if (!nvme_alloc_queue(dev, i, dev->q_depth)) {
1561 for (i = dev->online_queues; i <= dev->queue_count - 1; i++) {
1562 ret = nvme_create_queue(dev->queues[i], i);
1564 nvme_free_queues(dev, i);
1570 * Ignore failing Create SQ/CQ commands, we can continue with less
1571 * than the desired aount of queues, and even a controller without
1572 * I/O queues an still be used to issue admin commands. This might
1573 * be useful to upgrade a buggy firmware for example.
1575 return ret >= 0 ? 0 : ret;
1578 static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1580 u64 szu, size, offset;
1582 resource_size_t bar_size;
1583 struct pci_dev *pdev = to_pci_dev(dev->dev);
1585 dma_addr_t dma_addr;
1590 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
1591 if (!(NVME_CMB_SZ(dev->cmbsz)))
1594 cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
1596 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1597 size = szu * NVME_CMB_SZ(dev->cmbsz);
1598 offset = szu * NVME_CMB_OFST(cmbloc);
1599 bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
1601 if (offset > bar_size)
1605 * Controllers may support a CMB size larger than their BAR,
1606 * for example, due to being behind a bridge. Reduce the CMB to
1607 * the reported size of the BAR
1609 if (size > bar_size - offset)
1610 size = bar_size - offset;
1612 dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
1613 cmb = ioremap_wc(dma_addr, size);
1617 dev->cmb_dma_addr = dma_addr;
1618 dev->cmb_size = size;
1622 static inline void nvme_release_cmb(struct nvme_dev *dev)
1630 static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1632 return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
1635 static int nvme_setup_io_queues(struct nvme_dev *dev)
1637 struct nvme_queue *adminq = dev->queues[0];
1638 struct pci_dev *pdev = to_pci_dev(dev->dev);
1639 int result, i, vecs, nr_io_queues, size;
1641 nr_io_queues = num_possible_cpus();
1642 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
1647 * Degraded controllers might return an error when setting the queue
1648 * count. We still want to be able to bring them online and offer
1649 * access to the admin queue, as that might be only way to fix them up.
1652 dev_err(dev->dev, "Could not set queue count (%d)\n", result);
1657 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1658 result = nvme_cmb_qdepth(dev, nr_io_queues,
1659 sizeof(struct nvme_command));
1661 dev->q_depth = result;
1663 nvme_release_cmb(dev);
1666 size = db_bar_size(dev, nr_io_queues);
1670 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1673 if (!--nr_io_queues)
1675 size = db_bar_size(dev, nr_io_queues);
1677 dev->dbs = dev->bar + 4096;
1678 adminq->q_db = dev->dbs;
1681 /* Deregister the admin queue's interrupt */
1682 free_irq(dev->entry[0].vector, adminq);
1685 * If we enable msix early due to not intx, disable it again before
1686 * setting up the full range we need.
1689 pci_disable_msix(pdev);
1691 for (i = 0; i < nr_io_queues; i++)
1692 dev->entry[i].entry = i;
1693 vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
1695 vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
1699 for (i = 0; i < vecs; i++)
1700 dev->entry[i].vector = i + pdev->irq;
1705 * Should investigate if there's a performance win from allocating
1706 * more queues than interrupt vectors; it might allow the submission
1707 * path to scale better, even if the receive path is limited by the
1708 * number of interrupts.
1710 nr_io_queues = vecs;
1711 dev->max_qid = nr_io_queues;
1713 result = queue_request_irq(dev, adminq, adminq->irqname);
1715 adminq->cq_vector = -1;
1719 /* Free previously allocated queues that are no longer usable */
1720 nvme_free_queues(dev, nr_io_queues + 1);
1721 return nvme_create_io_queues(dev);
1724 nvme_free_queues(dev, 1);
1728 static void nvme_set_irq_hints(struct nvme_dev *dev)
1730 struct nvme_queue *nvmeq;
1733 for (i = 0; i < dev->online_queues; i++) {
1734 nvmeq = dev->queues[i];
1736 if (!nvmeq->tags || !(*nvmeq->tags))
1739 irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
1740 blk_mq_tags_cpumask(*nvmeq->tags));
1744 static void nvme_dev_scan(struct work_struct *work)
1746 struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
1748 if (!dev->tagset.tags)
1750 nvme_scan_namespaces(&dev->ctrl);
1751 nvme_set_irq_hints(dev);
1755 * Return: error value if an error occurred setting up the queues or calling
1756 * Identify Device. 0 if these succeeded, even if adding some of the
1757 * namespaces failed. At the moment, these failures are silent. TBD which
1758 * failures should be reported.
1760 static int nvme_dev_add(struct nvme_dev *dev)
1762 if (!dev->ctrl.tagset) {
1763 dev->tagset.ops = &nvme_mq_ops;
1764 dev->tagset.nr_hw_queues = dev->online_queues - 1;
1765 dev->tagset.timeout = NVME_IO_TIMEOUT;
1766 dev->tagset.numa_node = dev_to_node(dev->dev);
1767 dev->tagset.queue_depth =
1768 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
1769 dev->tagset.cmd_size = nvme_cmd_size(dev);
1770 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
1771 dev->tagset.driver_data = dev;
1773 if (blk_mq_alloc_tag_set(&dev->tagset))
1775 dev->ctrl.tagset = &dev->tagset;
1777 queue_work(nvme_workq, &dev->scan_work);
1781 static int nvme_dev_map(struct nvme_dev *dev)
1784 int bars, result = -ENOMEM;
1785 struct pci_dev *pdev = to_pci_dev(dev->dev);
1787 if (pci_enable_device_mem(pdev))
1790 dev->entry[0].vector = pdev->irq;
1791 pci_set_master(pdev);
1792 bars = pci_select_bars(pdev, IORESOURCE_MEM);
1796 if (pci_request_selected_regions(pdev, bars, "nvme"))
1799 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
1800 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
1803 dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
1807 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
1813 * Some devices don't advertse INTx interrupts, pre-enable a single
1814 * MSIX vec for setup. We'll adjust this later.
1817 result = pci_enable_msix(pdev, dev->entry, 1);
1822 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
1824 dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
1825 dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
1826 dev->dbs = dev->bar + 4096;
1827 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2))
1828 dev->cmb = nvme_map_cmb(dev);
1836 pci_release_regions(pdev);
1838 pci_disable_device(pdev);
1842 static void nvme_dev_unmap(struct nvme_dev *dev)
1844 struct pci_dev *pdev = to_pci_dev(dev->dev);
1846 if (pdev->msi_enabled)
1847 pci_disable_msi(pdev);
1848 else if (pdev->msix_enabled)
1849 pci_disable_msix(pdev);
1854 pci_release_regions(pdev);
1857 if (pci_is_enabled(pdev))
1858 pci_disable_device(pdev);
1861 struct nvme_delq_ctx {
1862 struct task_struct *waiter;
1863 struct kthread_worker *worker;
1867 static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
1869 dq->waiter = current;
1873 set_current_state(TASK_KILLABLE);
1874 if (!atomic_read(&dq->refcount))
1876 if (!schedule_timeout(ADMIN_TIMEOUT) ||
1877 fatal_signal_pending(current)) {
1879 * Disable the controller first since we can't trust it
1880 * at this point, but leave the admin queue enabled
1881 * until all queue deletion requests are flushed.
1882 * FIXME: This may take a while if there are more h/w
1883 * queues than admin tags.
1885 set_current_state(TASK_RUNNING);
1886 nvme_disable_ctrl(&dev->ctrl,
1887 lo_hi_readq(dev->bar + NVME_REG_CAP));
1888 nvme_clear_queue(dev->queues[0]);
1889 flush_kthread_worker(dq->worker);
1890 nvme_disable_queue(dev, 0);
1894 set_current_state(TASK_RUNNING);
1897 static void nvme_put_dq(struct nvme_delq_ctx *dq)
1899 atomic_dec(&dq->refcount);
1901 wake_up_process(dq->waiter);
1904 static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
1906 atomic_inc(&dq->refcount);
1910 static void nvme_del_queue_end(struct nvme_queue *nvmeq)
1912 struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
1915 spin_lock_irq(&nvmeq->q_lock);
1916 nvme_process_cq(nvmeq);
1917 spin_unlock_irq(&nvmeq->q_lock);
1920 static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
1921 kthread_work_func_t fn)
1923 struct nvme_command c;
1925 memset(&c, 0, sizeof(c));
1926 c.delete_queue.opcode = opcode;
1927 c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
1929 init_kthread_work(&nvmeq->cmdinfo.work, fn);
1930 return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
1934 static void nvme_del_cq_work_handler(struct kthread_work *work)
1936 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1938 nvme_del_queue_end(nvmeq);
1941 static int nvme_delete_cq(struct nvme_queue *nvmeq)
1943 return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
1944 nvme_del_cq_work_handler);
1947 static void nvme_del_sq_work_handler(struct kthread_work *work)
1949 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1951 int status = nvmeq->cmdinfo.status;
1954 status = nvme_delete_cq(nvmeq);
1956 nvme_del_queue_end(nvmeq);
1959 static int nvme_delete_sq(struct nvme_queue *nvmeq)
1961 return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
1962 nvme_del_sq_work_handler);
1965 static void nvme_del_queue_start(struct kthread_work *work)
1967 struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
1969 if (nvme_delete_sq(nvmeq))
1970 nvme_del_queue_end(nvmeq);
1973 static void nvme_disable_io_queues(struct nvme_dev *dev)
1976 DEFINE_KTHREAD_WORKER_ONSTACK(worker);
1977 struct nvme_delq_ctx dq;
1978 struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
1979 &worker, "nvme%d", dev->ctrl.instance);
1981 if (IS_ERR(kworker_task)) {
1983 "Failed to create queue del task\n");
1984 for (i = dev->queue_count - 1; i > 0; i--)
1985 nvme_disable_queue(dev, i);
1990 atomic_set(&dq.refcount, 0);
1991 dq.worker = &worker;
1992 for (i = dev->queue_count - 1; i > 0; i--) {
1993 struct nvme_queue *nvmeq = dev->queues[i];
1995 if (nvme_suspend_queue(nvmeq))
1997 nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
1998 nvmeq->cmdinfo.worker = dq.worker;
1999 init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
2000 queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
2002 nvme_wait_dq(&dq, dev);
2003 kthread_stop(kworker_task);
2006 static int nvme_dev_list_add(struct nvme_dev *dev)
2008 bool start_thread = false;
2010 spin_lock(&dev_list_lock);
2011 if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
2012 start_thread = true;
2015 list_add(&dev->node, &dev_list);
2016 spin_unlock(&dev_list_lock);
2019 nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
2020 wake_up_all(&nvme_kthread_wait);
2022 wait_event_killable(nvme_kthread_wait, nvme_thread);
2024 if (IS_ERR_OR_NULL(nvme_thread))
2025 return nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
2031 * Remove the node from the device list and check
2032 * for whether or not we need to stop the nvme_thread.
2034 static void nvme_dev_list_remove(struct nvme_dev *dev)
2036 struct task_struct *tmp = NULL;
2038 spin_lock(&dev_list_lock);
2039 list_del_init(&dev->node);
2040 if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
2044 spin_unlock(&dev_list_lock);
2050 static void nvme_freeze_queues(struct nvme_dev *dev)
2054 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
2055 blk_mq_freeze_queue_start(ns->queue);
2057 spin_lock_irq(ns->queue->queue_lock);
2058 queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
2059 spin_unlock_irq(ns->queue->queue_lock);
2061 blk_mq_cancel_requeue_work(ns->queue);
2062 blk_mq_stop_hw_queues(ns->queue);
2066 static void nvme_unfreeze_queues(struct nvme_dev *dev)
2070 list_for_each_entry(ns, &dev->ctrl.namespaces, list) {
2071 queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
2072 blk_mq_unfreeze_queue(ns->queue);
2073 blk_mq_start_stopped_hw_queues(ns->queue, true);
2074 blk_mq_kick_requeue_list(ns->queue);
2078 static void nvme_dev_shutdown(struct nvme_dev *dev)
2083 nvme_dev_list_remove(dev);
2085 mutex_lock(&dev->shutdown_lock);
2087 nvme_freeze_queues(dev);
2088 csts = readl(dev->bar + NVME_REG_CSTS);
2090 if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
2091 for (i = dev->queue_count - 1; i >= 0; i--) {
2092 struct nvme_queue *nvmeq = dev->queues[i];
2093 nvme_suspend_queue(nvmeq);
2096 nvme_disable_io_queues(dev);
2097 nvme_shutdown_ctrl(&dev->ctrl);
2098 nvme_disable_queue(dev, 0);
2100 nvme_dev_unmap(dev);
2102 for (i = dev->queue_count - 1; i >= 0; i--)
2103 nvme_clear_queue(dev->queues[i]);
2104 mutex_unlock(&dev->shutdown_lock);
2107 static int nvme_setup_prp_pools(struct nvme_dev *dev)
2109 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
2110 PAGE_SIZE, PAGE_SIZE, 0);
2111 if (!dev->prp_page_pool)
2114 /* Optimisation for I/Os between 4k and 128k */
2115 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
2117 if (!dev->prp_small_pool) {
2118 dma_pool_destroy(dev->prp_page_pool);
2124 static void nvme_release_prp_pools(struct nvme_dev *dev)
2126 dma_pool_destroy(dev->prp_page_pool);
2127 dma_pool_destroy(dev->prp_small_pool);
2130 static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
2132 struct nvme_dev *dev = to_nvme_dev(ctrl);
2134 put_device(dev->dev);
2135 if (dev->tagset.tags)
2136 blk_mq_free_tag_set(&dev->tagset);
2137 if (dev->ctrl.admin_q)
2138 blk_put_queue(dev->ctrl.admin_q);
2144 static void nvme_reset_work(struct work_struct *work)
2146 struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
2149 if (WARN_ON(test_bit(NVME_CTRL_RESETTING, &dev->flags)))
2153 * If we're called to reset a live controller first shut it down before
2157 nvme_dev_shutdown(dev);
2159 set_bit(NVME_CTRL_RESETTING, &dev->flags);
2161 result = nvme_dev_map(dev);
2165 result = nvme_configure_admin_queue(dev);
2169 nvme_init_queue(dev->queues[0], 0);
2170 result = nvme_alloc_admin_tags(dev);
2174 result = nvme_init_identify(&dev->ctrl);
2178 result = nvme_setup_io_queues(dev);
2182 dev->ctrl.event_limit = 1;
2184 result = nvme_dev_list_add(dev);
2189 * Keep the controller around but remove all namespaces if we don't have
2190 * any working I/O queue.
2192 if (dev->online_queues < 2) {
2193 dev_warn(dev->dev, "IO queues not created\n");
2194 nvme_remove_namespaces(&dev->ctrl);
2196 nvme_unfreeze_queues(dev);
2200 clear_bit(NVME_CTRL_RESETTING, &dev->flags);
2204 nvme_dev_list_remove(dev);
2206 nvme_dev_remove_admin(dev);
2207 blk_put_queue(dev->ctrl.admin_q);
2208 dev->ctrl.admin_q = NULL;
2209 dev->queues[0]->tags = NULL;
2211 nvme_disable_queue(dev, 0);
2213 nvme_dev_unmap(dev);
2215 nvme_remove_dead_ctrl(dev);
2218 static void nvme_remove_dead_ctrl_work(struct work_struct *work)
2220 struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
2221 struct pci_dev *pdev = to_pci_dev(dev->dev);
2223 if (pci_get_drvdata(pdev))
2224 pci_stop_and_remove_bus_device_locked(pdev);
2225 nvme_put_ctrl(&dev->ctrl);
2228 static void nvme_remove_dead_ctrl(struct nvme_dev *dev)
2230 dev_warn(dev->dev, "Removing after probe failure\n");
2231 kref_get(&dev->ctrl.kref);
2232 if (!schedule_work(&dev->remove_work))
2233 nvme_put_ctrl(&dev->ctrl);
2236 static int nvme_reset(struct nvme_dev *dev)
2238 if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q))
2241 if (!queue_work(nvme_workq, &dev->reset_work))
2244 flush_work(&dev->reset_work);
2248 static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
2250 *val = readl(to_nvme_dev(ctrl)->bar + off);
2254 static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2256 writel(val, to_nvme_dev(ctrl)->bar + off);
2260 static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2262 *val = readq(to_nvme_dev(ctrl)->bar + off);
2266 static bool nvme_pci_io_incapable(struct nvme_ctrl *ctrl)
2268 struct nvme_dev *dev = to_nvme_dev(ctrl);
2270 return !dev->bar || dev->online_queues < 2;
2273 static int nvme_pci_reset_ctrl(struct nvme_ctrl *ctrl)
2275 return nvme_reset(to_nvme_dev(ctrl));
2278 static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
2279 .reg_read32 = nvme_pci_reg_read32,
2280 .reg_write32 = nvme_pci_reg_write32,
2281 .reg_read64 = nvme_pci_reg_read64,
2282 .io_incapable = nvme_pci_io_incapable,
2283 .reset_ctrl = nvme_pci_reset_ctrl,
2284 .free_ctrl = nvme_pci_free_ctrl,
2287 static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2289 int node, result = -ENOMEM;
2290 struct nvme_dev *dev;
2292 node = dev_to_node(&pdev->dev);
2293 if (node == NUMA_NO_NODE)
2294 set_dev_node(&pdev->dev, 0);
2296 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
2299 dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
2303 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
2308 dev->dev = get_device(&pdev->dev);
2309 pci_set_drvdata(pdev, dev);
2311 INIT_LIST_HEAD(&dev->node);
2312 INIT_WORK(&dev->scan_work, nvme_dev_scan);
2313 INIT_WORK(&dev->reset_work, nvme_reset_work);
2314 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
2315 mutex_init(&dev->shutdown_lock);
2317 result = nvme_setup_prp_pools(dev);
2321 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
2326 queue_work(nvme_workq, &dev->reset_work);
2330 nvme_release_prp_pools(dev);
2332 put_device(dev->dev);
2340 static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
2342 struct nvme_dev *dev = pci_get_drvdata(pdev);
2345 nvme_dev_shutdown(dev);
2347 queue_work(nvme_workq, &dev->reset_work);
2350 static void nvme_shutdown(struct pci_dev *pdev)
2352 struct nvme_dev *dev = pci_get_drvdata(pdev);
2353 nvme_dev_shutdown(dev);
2356 static void nvme_remove(struct pci_dev *pdev)
2358 struct nvme_dev *dev = pci_get_drvdata(pdev);
2360 spin_lock(&dev_list_lock);
2361 list_del_init(&dev->node);
2362 spin_unlock(&dev_list_lock);
2364 pci_set_drvdata(pdev, NULL);
2365 flush_work(&dev->reset_work);
2366 flush_work(&dev->scan_work);
2367 nvme_remove_namespaces(&dev->ctrl);
2368 nvme_uninit_ctrl(&dev->ctrl);
2369 nvme_dev_shutdown(dev);
2370 nvme_dev_remove_admin(dev);
2371 nvme_free_queues(dev, 0);
2372 nvme_release_cmb(dev);
2373 nvme_release_prp_pools(dev);
2374 nvme_put_ctrl(&dev->ctrl);
2377 /* These functions are yet to be implemented */
2378 #define nvme_error_detected NULL
2379 #define nvme_dump_registers NULL
2380 #define nvme_link_reset NULL
2381 #define nvme_slot_reset NULL
2382 #define nvme_error_resume NULL
2384 #ifdef CONFIG_PM_SLEEP
2385 static int nvme_suspend(struct device *dev)
2387 struct pci_dev *pdev = to_pci_dev(dev);
2388 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2390 nvme_dev_shutdown(ndev);
2394 static int nvme_resume(struct device *dev)
2396 struct pci_dev *pdev = to_pci_dev(dev);
2397 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2399 queue_work(nvme_workq, &ndev->reset_work);
2404 static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
2406 static const struct pci_error_handlers nvme_err_handler = {
2407 .error_detected = nvme_error_detected,
2408 .mmio_enabled = nvme_dump_registers,
2409 .link_reset = nvme_link_reset,
2410 .slot_reset = nvme_slot_reset,
2411 .resume = nvme_error_resume,
2412 .reset_notify = nvme_reset_notify,
2415 /* Move to pci_ids.h later */
2416 #define PCI_CLASS_STORAGE_EXPRESS 0x010802
2418 static const struct pci_device_id nvme_id_table[] = {
2419 { PCI_VDEVICE(INTEL, 0x0953),
2420 .driver_data = NVME_QUIRK_STRIPE_SIZE, },
2421 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2422 .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
2423 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2424 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
2427 MODULE_DEVICE_TABLE(pci, nvme_id_table);
2429 static struct pci_driver nvme_driver = {
2431 .id_table = nvme_id_table,
2432 .probe = nvme_probe,
2433 .remove = nvme_remove,
2434 .shutdown = nvme_shutdown,
2436 .pm = &nvme_dev_pm_ops,
2438 .err_handler = &nvme_err_handler,
2441 static int __init nvme_init(void)
2445 init_waitqueue_head(&nvme_kthread_wait);
2447 nvme_workq = alloc_workqueue("nvme", WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
2451 result = nvme_core_init();
2455 result = pci_register_driver(&nvme_driver);
2463 destroy_workqueue(nvme_workq);
2467 static void __exit nvme_exit(void)
2469 pci_unregister_driver(&nvme_driver);
2471 destroy_workqueue(nvme_workq);
2472 BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
2476 MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2477 MODULE_LICENSE("GPL");
2478 MODULE_VERSION("1.0");
2479 module_init(nvme_init);
2480 module_exit(nvme_exit);