15f37396dSChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
257dacad5SJay Sternberg /*
357dacad5SJay Sternberg * NVM Express device driver
457dacad5SJay Sternberg * Copyright (c) 2011-2014, Intel Corporation.
557dacad5SJay Sternberg */
657dacad5SJay Sternberg
7df4f9bc4SDavid E. Box #include <linux/acpi.h>
818119775SKeith Busch #include <linux/async.h>
957dacad5SJay Sternberg #include <linux/blkdev.h>
1057dacad5SJay Sternberg #include <linux/blk-mq.h>
11dca51e78SChristoph Hellwig #include <linux/blk-mq-pci.h>
12fe45e630SChristoph Hellwig #include <linux/blk-integrity.h>
13ff5350a8SAndy Lutomirski #include <linux/dmi.h>
1457dacad5SJay Sternberg #include <linux/init.h>
1557dacad5SJay Sternberg #include <linux/interrupt.h>
1657dacad5SJay Sternberg #include <linux/io.h>
1799722c8aSChristophe JAILLET #include <linux/kstrtox.h>
18dc90f084SChristoph Hellwig #include <linux/memremap.h>
1957dacad5SJay Sternberg #include <linux/mm.h>
2057dacad5SJay Sternberg #include <linux/module.h>
2177bf25eaSKeith Busch #include <linux/mutex.h>
22d0877473SKeith Busch #include <linux/once.h>
2357dacad5SJay Sternberg #include <linux/pci.h>
24d916b1beSKeith Busch #include <linux/suspend.h>
2557dacad5SJay Sternberg #include <linux/t10-pi.h>
2657dacad5SJay Sternberg #include <linux/types.h>
279cf5c095SLinus Torvalds #include <linux/io-64-nonatomic-lo-hi.h>
2820d3bb92SKlaus Jensen #include <linux/io-64-nonatomic-hi-lo.h>
29a98e58e5SScott Bauer #include <linux/sed-opal.h>
300f238ff5SLogan Gunthorpe #include <linux/pci-p2pdma.h>
3157dacad5SJay Sternberg
32604c01d5Syupeng #include "trace.h"
3357dacad5SJay Sternberg #include "nvme.h"
3457dacad5SJay Sternberg
35c1e0cc7eSBenjamin Herrenschmidt #define SQ_SIZE(q) ((q)->q_depth << (q)->sqes)
368a1d09a6SBenjamin Herrenschmidt #define CQ_SIZE(q) ((q)->q_depth * sizeof(struct nvme_completion))
3757dacad5SJay Sternberg
3884173423SKeith Busch #define SGES_PER_PAGE (NVME_CTRL_PAGE_SIZE / sizeof(struct nvme_sgl_desc))
39adf68f21SChristoph Hellwig
40943e942eSJens Axboe /*
41943e942eSJens Axboe * These can be higher, but we need to ensure that any command doesn't
42943e942eSJens Axboe * require an sg allocation that needs more than a page of data.
43943e942eSJens Axboe */
447846c1b5SKeith Busch #define NVME_MAX_KB_SZ 8192
457846c1b5SKeith Busch #define NVME_MAX_SEGS 128
467846c1b5SKeith Busch #define NVME_MAX_NR_ALLOCATIONS 5
47943e942eSJens Axboe
4857dacad5SJay Sternberg static int use_threaded_interrupts;
492e21e445SXin Hao module_param(use_threaded_interrupts, int, 0444);
5057dacad5SJay Sternberg
5157dacad5SJay Sternberg static bool use_cmb_sqes = true;
5269f4eb9fSKeith Busch module_param(use_cmb_sqes, bool, 0444);
5357dacad5SJay Sternberg MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
5457dacad5SJay Sternberg
5587ad72a5SChristoph Hellwig static unsigned int max_host_mem_size_mb = 128;
5687ad72a5SChristoph Hellwig module_param(max_host_mem_size_mb, uint, 0444);
5787ad72a5SChristoph Hellwig MODULE_PARM_DESC(max_host_mem_size_mb,
5887ad72a5SChristoph Hellwig "Maximum Host Memory Buffer (HMB) size per controller (in MiB)");
5957dacad5SJay Sternberg
60a7a7cbe3SChaitanya Kulkarni static unsigned int sgl_threshold = SZ_32K;
61a7a7cbe3SChaitanya Kulkarni module_param(sgl_threshold, uint, 0644);
62a7a7cbe3SChaitanya Kulkarni MODULE_PARM_DESC(sgl_threshold,
63a7a7cbe3SChaitanya Kulkarni "Use SGLs when average request segment size is larger or equal to "
64a7a7cbe3SChaitanya Kulkarni "this size. Use 0 to disable SGLs.");
65a7a7cbe3SChaitanya Kulkarni
6627453b45SSagi Grimberg #define NVME_PCI_MIN_QUEUE_SIZE 2
6727453b45SSagi Grimberg #define NVME_PCI_MAX_QUEUE_SIZE 4095
68b27c1e68Sweiping zhang static int io_queue_depth_set(const char *val, const struct kernel_param *kp);
69b27c1e68Sweiping zhang static const struct kernel_param_ops io_queue_depth_ops = {
70b27c1e68Sweiping zhang .set = io_queue_depth_set,
7161f3b896SChaitanya Kulkarni .get = param_get_uint,
72b27c1e68Sweiping zhang };
73b27c1e68Sweiping zhang
7461f3b896SChaitanya Kulkarni static unsigned int io_queue_depth = 1024;
75b27c1e68Sweiping zhang module_param_cb(io_queue_depth, &io_queue_depth_ops, &io_queue_depth, 0644);
7627453b45SSagi Grimberg MODULE_PARM_DESC(io_queue_depth, "set io queue depth, should >= 2 and < 4096");
77b27c1e68Sweiping zhang
io_queue_count_set(const char * val,const struct kernel_param * kp)789c9e76d5SWeiping Zhang static int io_queue_count_set(const char *val, const struct kernel_param *kp)
799c9e76d5SWeiping Zhang {
809c9e76d5SWeiping Zhang unsigned int n;
819c9e76d5SWeiping Zhang int ret;
829c9e76d5SWeiping Zhang
839c9e76d5SWeiping Zhang ret = kstrtouint(val, 10, &n);
849c9e76d5SWeiping Zhang if (ret != 0 || n > num_possible_cpus())
859c9e76d5SWeiping Zhang return -EINVAL;
869c9e76d5SWeiping Zhang return param_set_uint(val, kp);
879c9e76d5SWeiping Zhang }
889c9e76d5SWeiping Zhang
899c9e76d5SWeiping Zhang static const struct kernel_param_ops io_queue_count_ops = {
909c9e76d5SWeiping Zhang .set = io_queue_count_set,
919c9e76d5SWeiping Zhang .get = param_get_uint,
929c9e76d5SWeiping Zhang };
939c9e76d5SWeiping Zhang
943f68baf7SKeith Busch static unsigned int write_queues;
959c9e76d5SWeiping Zhang module_param_cb(write_queues, &io_queue_count_ops, &write_queues, 0644);
963b6592f7SJens Axboe MODULE_PARM_DESC(write_queues,
973b6592f7SJens Axboe "Number of queues to use for writes. If not set, reads and writes "
983b6592f7SJens Axboe "will share a queue set.");
993b6592f7SJens Axboe
1003f68baf7SKeith Busch static unsigned int poll_queues;
1019c9e76d5SWeiping Zhang module_param_cb(poll_queues, &io_queue_count_ops, &poll_queues, 0644);
1024b04cc6aSJens Axboe MODULE_PARM_DESC(poll_queues, "Number of queues to use for polled IO.");
1034b04cc6aSJens Axboe
104df4f9bc4SDavid E. Box static bool noacpi;
105df4f9bc4SDavid E. Box module_param(noacpi, bool, 0444);
106df4f9bc4SDavid E. Box MODULE_PARM_DESC(noacpi, "disable acpi bios quirks");
107df4f9bc4SDavid E. Box
1081c63dc66SChristoph Hellwig struct nvme_dev;
1091c63dc66SChristoph Hellwig struct nvme_queue;
11057dacad5SJay Sternberg
111a5cdb68cSKeith Busch static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown);
1127d879c90SChristoph Hellwig static void nvme_delete_io_queues(struct nvme_dev *dev);
113e917a849SKeith Busch static void nvme_update_attrs(struct nvme_dev *dev);
11457dacad5SJay Sternberg
11557dacad5SJay Sternberg /*
1161c63dc66SChristoph Hellwig * Represents an NVM Express device. Each nvme_dev is a PCI function.
1171c63dc66SChristoph Hellwig */
1181c63dc66SChristoph Hellwig struct nvme_dev {
119147b27e4SSagi Grimberg struct nvme_queue *queues;
1201c63dc66SChristoph Hellwig struct blk_mq_tag_set tagset;
1211c63dc66SChristoph Hellwig struct blk_mq_tag_set admin_tagset;
1221c63dc66SChristoph Hellwig u32 __iomem *dbs;
1231c63dc66SChristoph Hellwig struct device *dev;
1241c63dc66SChristoph Hellwig struct dma_pool *prp_page_pool;
1251c63dc66SChristoph Hellwig struct dma_pool *prp_small_pool;
1261c63dc66SChristoph Hellwig unsigned online_queues;
1271c63dc66SChristoph Hellwig unsigned max_qid;
128e20ba6e1SChristoph Hellwig unsigned io_queues[HCTX_MAX_TYPES];
12922b55601SKeith Busch unsigned int num_vecs;
1307442ddceSJohn Garry u32 q_depth;
131c1e0cc7eSBenjamin Herrenschmidt int io_sqes;
1321c63dc66SChristoph Hellwig u32 db_stride;
1331c63dc66SChristoph Hellwig void __iomem *bar;
13497f6ef64SXu Yu unsigned long bar_mapped_size;
13577bf25eaSKeith Busch struct mutex shutdown_lock;
1361c63dc66SChristoph Hellwig bool subsystem;
1371c63dc66SChristoph Hellwig u64 cmb_size;
1380f238ff5SLogan Gunthorpe bool cmb_use_sqes;
1391c63dc66SChristoph Hellwig u32 cmbsz;
140202021c1SStephen Bates u32 cmbloc;
1411c63dc66SChristoph Hellwig struct nvme_ctrl ctrl;
142d916b1beSKeith Busch u32 last_ps;
143a5df5e79SKeith Busch bool hmb;
14487ad72a5SChristoph Hellwig
145943e942eSJens Axboe mempool_t *iod_mempool;
146943e942eSJens Axboe
14787ad72a5SChristoph Hellwig /* shadow doorbell buffer support: */
148b5f96cb7SKlaus Jensen __le32 *dbbuf_dbs;
149f9f38e33SHelen Koike dma_addr_t dbbuf_dbs_dma_addr;
150b5f96cb7SKlaus Jensen __le32 *dbbuf_eis;
151f9f38e33SHelen Koike dma_addr_t dbbuf_eis_dma_addr;
15287ad72a5SChristoph Hellwig
15387ad72a5SChristoph Hellwig /* host memory buffer support: */
15487ad72a5SChristoph Hellwig u64 host_mem_size;
15587ad72a5SChristoph Hellwig u32 nr_host_mem_descs;
156cee3bff5SChristoph Hellwig u32 host_mem_descs_size;
1574033f35dSChristoph Hellwig dma_addr_t host_mem_descs_dma;
15887ad72a5SChristoph Hellwig struct nvme_host_mem_buf_desc *host_mem_descs;
15987ad72a5SChristoph Hellwig void **host_mem_desc_bufs;
1602a5bcfddSWeiping Zhang unsigned int nr_allocated_queues;
1612a5bcfddSWeiping Zhang unsigned int nr_write_queues;
1622a5bcfddSWeiping Zhang unsigned int nr_poll_queues;
16357dacad5SJay Sternberg };
16457dacad5SJay Sternberg
io_queue_depth_set(const char * val,const struct kernel_param * kp)165b27c1e68Sweiping zhang static int io_queue_depth_set(const char *val, const struct kernel_param *kp)
166b27c1e68Sweiping zhang {
16727453b45SSagi Grimberg return param_set_uint_minmax(val, kp, NVME_PCI_MIN_QUEUE_SIZE,
16827453b45SSagi Grimberg NVME_PCI_MAX_QUEUE_SIZE);
169b27c1e68Sweiping zhang }
170b27c1e68Sweiping zhang
sq_idx(unsigned int qid,u32 stride)171f9f38e33SHelen Koike static inline unsigned int sq_idx(unsigned int qid, u32 stride)
172f9f38e33SHelen Koike {
173f9f38e33SHelen Koike return qid * 2 * stride;
174f9f38e33SHelen Koike }
175f9f38e33SHelen Koike
cq_idx(unsigned int qid,u32 stride)176f9f38e33SHelen Koike static inline unsigned int cq_idx(unsigned int qid, u32 stride)
177f9f38e33SHelen Koike {
178f9f38e33SHelen Koike return (qid * 2 + 1) * stride;
179f9f38e33SHelen Koike }
180f9f38e33SHelen Koike
to_nvme_dev(struct nvme_ctrl * ctrl)1811c63dc66SChristoph Hellwig static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
1821c63dc66SChristoph Hellwig {
1831c63dc66SChristoph Hellwig return container_of(ctrl, struct nvme_dev, ctrl);
1841c63dc66SChristoph Hellwig }
1851c63dc66SChristoph Hellwig
18657dacad5SJay Sternberg /*
18757dacad5SJay Sternberg * An NVM Express queue. Each device has at least two (one for admin
18857dacad5SJay Sternberg * commands and one for I/O commands).
18957dacad5SJay Sternberg */
19057dacad5SJay Sternberg struct nvme_queue {
19157dacad5SJay Sternberg struct nvme_dev *dev;
1921ab0cd69SJens Axboe spinlock_t sq_lock;
193c1e0cc7eSBenjamin Herrenschmidt void *sq_cmds;
1943a7afd8eSChristoph Hellwig /* only used for poll queues: */
1953a7afd8eSChristoph Hellwig spinlock_t cq_poll_lock ____cacheline_aligned_in_smp;
19674943d45SKeith Busch struct nvme_completion *cqes;
19757dacad5SJay Sternberg dma_addr_t sq_dma_addr;
19857dacad5SJay Sternberg dma_addr_t cq_dma_addr;
19957dacad5SJay Sternberg u32 __iomem *q_db;
2007442ddceSJohn Garry u32 q_depth;
2017c349ddeSKeith Busch u16 cq_vector;
20257dacad5SJay Sternberg u16 sq_tail;
20338210800SKeith Busch u16 last_sq_tail;
20457dacad5SJay Sternberg u16 cq_head;
20557dacad5SJay Sternberg u16 qid;
20657dacad5SJay Sternberg u8 cq_phase;
207c1e0cc7eSBenjamin Herrenschmidt u8 sqes;
2084e224106SChristoph Hellwig unsigned long flags;
2094e224106SChristoph Hellwig #define NVMEQ_ENABLED 0
21063223078SChristoph Hellwig #define NVMEQ_SQ_CMB 1
211d1ed6aa1SChristoph Hellwig #define NVMEQ_DELETE_ERROR 2
2127c349ddeSKeith Busch #define NVMEQ_POLLED 3
213b5f96cb7SKlaus Jensen __le32 *dbbuf_sq_db;
214b5f96cb7SKlaus Jensen __le32 *dbbuf_cq_db;
215b5f96cb7SKlaus Jensen __le32 *dbbuf_sq_ei;
216b5f96cb7SKlaus Jensen __le32 *dbbuf_cq_ei;
217d1ed6aa1SChristoph Hellwig struct completion delete_done;
21857dacad5SJay Sternberg };
21957dacad5SJay Sternberg
2207846c1b5SKeith Busch union nvme_descriptor {
2217846c1b5SKeith Busch struct nvme_sgl_desc *sg_list;
2227846c1b5SKeith Busch __le64 *prp_list;
2237846c1b5SKeith Busch };
2247846c1b5SKeith Busch
22557dacad5SJay Sternberg /*
2269b048119SChristoph Hellwig * The nvme_iod describes the data in an I/O.
2279b048119SChristoph Hellwig *
2289b048119SChristoph Hellwig * The sg pointer contains the list of PRP/SGL chunk allocations in addition
2299b048119SChristoph Hellwig * to the actual struct scatterlist.
23071bd150cSChristoph Hellwig */
23171bd150cSChristoph Hellwig struct nvme_iod {
232d49187e9SChristoph Hellwig struct nvme_request req;
233af7fae85SKeith Busch struct nvme_command cmd;
23452da4f3fSKeith Busch bool aborted;
235c372cdd1SKeith Busch s8 nr_allocations; /* PRP list pool allocations. 0 means small
236c372cdd1SKeith Busch pool in use */
237dff824b2SChristoph Hellwig unsigned int dma_len; /* length of single DMA segment mapping */
238c4c22c52SKeith Busch dma_addr_t first_dma;
239783b94bdSChristoph Hellwig dma_addr_t meta_dma;
24091fb2b60SLogan Gunthorpe struct sg_table sgt;
2417846c1b5SKeith Busch union nvme_descriptor list[NVME_MAX_NR_ALLOCATIONS];
24257dacad5SJay Sternberg };
24357dacad5SJay Sternberg
nvme_dbbuf_size(struct nvme_dev * dev)2442a5bcfddSWeiping Zhang static inline unsigned int nvme_dbbuf_size(struct nvme_dev *dev)
2453b6592f7SJens Axboe {
2462a5bcfddSWeiping Zhang return dev->nr_allocated_queues * 8 * dev->db_stride;
247f9f38e33SHelen Koike }
248f9f38e33SHelen Koike
nvme_dbbuf_dma_alloc(struct nvme_dev * dev)24965a54646SChristoph Hellwig static void nvme_dbbuf_dma_alloc(struct nvme_dev *dev)
250f9f38e33SHelen Koike {
2512a5bcfddSWeiping Zhang unsigned int mem_size = nvme_dbbuf_size(dev);
252f9f38e33SHelen Koike
25365a54646SChristoph Hellwig if (!(dev->ctrl.oacs & NVME_CTRL_OACS_DBBUF_SUPP))
25465a54646SChristoph Hellwig return;
25565a54646SChristoph Hellwig
25658847f12SKeith Busch if (dev->dbbuf_dbs) {
25758847f12SKeith Busch /*
25858847f12SKeith Busch * Clear the dbbuf memory so the driver doesn't observe stale
25958847f12SKeith Busch * values from the previous instantiation.
26058847f12SKeith Busch */
26158847f12SKeith Busch memset(dev->dbbuf_dbs, 0, mem_size);
26258847f12SKeith Busch memset(dev->dbbuf_eis, 0, mem_size);
26365a54646SChristoph Hellwig return;
26458847f12SKeith Busch }
265f9f38e33SHelen Koike
266f9f38e33SHelen Koike dev->dbbuf_dbs = dma_alloc_coherent(dev->dev, mem_size,
267f9f38e33SHelen Koike &dev->dbbuf_dbs_dma_addr,
268f9f38e33SHelen Koike GFP_KERNEL);
269f9f38e33SHelen Koike if (!dev->dbbuf_dbs)
27065a54646SChristoph Hellwig goto fail;
271f9f38e33SHelen Koike dev->dbbuf_eis = dma_alloc_coherent(dev->dev, mem_size,
272f9f38e33SHelen Koike &dev->dbbuf_eis_dma_addr,
273f9f38e33SHelen Koike GFP_KERNEL);
27465a54646SChristoph Hellwig if (!dev->dbbuf_eis)
27565a54646SChristoph Hellwig goto fail_free_dbbuf_dbs;
27665a54646SChristoph Hellwig return;
277f9f38e33SHelen Koike
27865a54646SChristoph Hellwig fail_free_dbbuf_dbs:
27965a54646SChristoph Hellwig dma_free_coherent(dev->dev, mem_size, dev->dbbuf_dbs,
28065a54646SChristoph Hellwig dev->dbbuf_dbs_dma_addr);
28165a54646SChristoph Hellwig dev->dbbuf_dbs = NULL;
28265a54646SChristoph Hellwig fail:
28365a54646SChristoph Hellwig dev_warn(dev->dev, "unable to allocate dma for dbbuf\n");
284f9f38e33SHelen Koike }
285f9f38e33SHelen Koike
nvme_dbbuf_dma_free(struct nvme_dev * dev)286f9f38e33SHelen Koike static void nvme_dbbuf_dma_free(struct nvme_dev *dev)
287f9f38e33SHelen Koike {
2882a5bcfddSWeiping Zhang unsigned int mem_size = nvme_dbbuf_size(dev);
289f9f38e33SHelen Koike
290f9f38e33SHelen Koike if (dev->dbbuf_dbs) {
291f9f38e33SHelen Koike dma_free_coherent(dev->dev, mem_size,
292f9f38e33SHelen Koike dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
293f9f38e33SHelen Koike dev->dbbuf_dbs = NULL;
294f9f38e33SHelen Koike }
295f9f38e33SHelen Koike if (dev->dbbuf_eis) {
296f9f38e33SHelen Koike dma_free_coherent(dev->dev, mem_size,
297f9f38e33SHelen Koike dev->dbbuf_eis, dev->dbbuf_eis_dma_addr);
298f9f38e33SHelen Koike dev->dbbuf_eis = NULL;
299f9f38e33SHelen Koike }
300f9f38e33SHelen Koike }
301f9f38e33SHelen Koike
nvme_dbbuf_init(struct nvme_dev * dev,struct nvme_queue * nvmeq,int qid)302f9f38e33SHelen Koike static void nvme_dbbuf_init(struct nvme_dev *dev,
303f9f38e33SHelen Koike struct nvme_queue *nvmeq, int qid)
304f9f38e33SHelen Koike {
305f9f38e33SHelen Koike if (!dev->dbbuf_dbs || !qid)
306f9f38e33SHelen Koike return;
307f9f38e33SHelen Koike
308f9f38e33SHelen Koike nvmeq->dbbuf_sq_db = &dev->dbbuf_dbs[sq_idx(qid, dev->db_stride)];
309f9f38e33SHelen Koike nvmeq->dbbuf_cq_db = &dev->dbbuf_dbs[cq_idx(qid, dev->db_stride)];
310f9f38e33SHelen Koike nvmeq->dbbuf_sq_ei = &dev->dbbuf_eis[sq_idx(qid, dev->db_stride)];
311f9f38e33SHelen Koike nvmeq->dbbuf_cq_ei = &dev->dbbuf_eis[cq_idx(qid, dev->db_stride)];
312f9f38e33SHelen Koike }
313f9f38e33SHelen Koike
nvme_dbbuf_free(struct nvme_queue * nvmeq)3140f0d2c87SMinwoo Im static void nvme_dbbuf_free(struct nvme_queue *nvmeq)
3150f0d2c87SMinwoo Im {
3160f0d2c87SMinwoo Im if (!nvmeq->qid)
3170f0d2c87SMinwoo Im return;
3180f0d2c87SMinwoo Im
3190f0d2c87SMinwoo Im nvmeq->dbbuf_sq_db = NULL;
3200f0d2c87SMinwoo Im nvmeq->dbbuf_cq_db = NULL;
3210f0d2c87SMinwoo Im nvmeq->dbbuf_sq_ei = NULL;
3220f0d2c87SMinwoo Im nvmeq->dbbuf_cq_ei = NULL;
3230f0d2c87SMinwoo Im }
3240f0d2c87SMinwoo Im
nvme_dbbuf_set(struct nvme_dev * dev)325f9f38e33SHelen Koike static void nvme_dbbuf_set(struct nvme_dev *dev)
326f9f38e33SHelen Koike {
327f66e2804SChaitanya Kulkarni struct nvme_command c = { };
3280f0d2c87SMinwoo Im unsigned int i;
329f9f38e33SHelen Koike
330f9f38e33SHelen Koike if (!dev->dbbuf_dbs)
331f9f38e33SHelen Koike return;
332f9f38e33SHelen Koike
333f9f38e33SHelen Koike c.dbbuf.opcode = nvme_admin_dbbuf;
334f9f38e33SHelen Koike c.dbbuf.prp1 = cpu_to_le64(dev->dbbuf_dbs_dma_addr);
335f9f38e33SHelen Koike c.dbbuf.prp2 = cpu_to_le64(dev->dbbuf_eis_dma_addr);
336f9f38e33SHelen Koike
337f9f38e33SHelen Koike if (nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0)) {
3389bdcfb10SChristoph Hellwig dev_warn(dev->ctrl.device, "unable to set dbbuf\n");
339f9f38e33SHelen Koike /* Free memory and continue on */
340f9f38e33SHelen Koike nvme_dbbuf_dma_free(dev);
3410f0d2c87SMinwoo Im
3420f0d2c87SMinwoo Im for (i = 1; i <= dev->online_queues; i++)
3430f0d2c87SMinwoo Im nvme_dbbuf_free(&dev->queues[i]);
344f9f38e33SHelen Koike }
345f9f38e33SHelen Koike }
346f9f38e33SHelen Koike
nvme_dbbuf_need_event(u16 event_idx,u16 new_idx,u16 old)347f9f38e33SHelen Koike static inline int nvme_dbbuf_need_event(u16 event_idx, u16 new_idx, u16 old)
348f9f38e33SHelen Koike {
349f9f38e33SHelen Koike return (u16)(new_idx - event_idx - 1) < (u16)(new_idx - old);
350f9f38e33SHelen Koike }
351f9f38e33SHelen Koike
352f9f38e33SHelen Koike /* Update dbbuf and return true if an MMIO is required */
nvme_dbbuf_update_and_check_event(u16 value,__le32 * dbbuf_db,volatile __le32 * dbbuf_ei)353b5f96cb7SKlaus Jensen static bool nvme_dbbuf_update_and_check_event(u16 value, __le32 *dbbuf_db,
354b5f96cb7SKlaus Jensen volatile __le32 *dbbuf_ei)
355f9f38e33SHelen Koike {
356f9f38e33SHelen Koike if (dbbuf_db) {
357b5f96cb7SKlaus Jensen u16 old_value, event_idx;
358f9f38e33SHelen Koike
359f9f38e33SHelen Koike /*
360f9f38e33SHelen Koike * Ensure that the queue is written before updating
361f9f38e33SHelen Koike * the doorbell in memory
362f9f38e33SHelen Koike */
363f9f38e33SHelen Koike wmb();
364f9f38e33SHelen Koike
365b5f96cb7SKlaus Jensen old_value = le32_to_cpu(*dbbuf_db);
366b5f96cb7SKlaus Jensen *dbbuf_db = cpu_to_le32(value);
367f9f38e33SHelen Koike
368f1ed3df2SMichal Wnukowski /*
369f1ed3df2SMichal Wnukowski * Ensure that the doorbell is updated before reading the event
370f1ed3df2SMichal Wnukowski * index from memory. The controller needs to provide similar
371f1ed3df2SMichal Wnukowski * ordering to ensure the envent index is updated before reading
372f1ed3df2SMichal Wnukowski * the doorbell.
373f1ed3df2SMichal Wnukowski */
374f1ed3df2SMichal Wnukowski mb();
375f1ed3df2SMichal Wnukowski
376b5f96cb7SKlaus Jensen event_idx = le32_to_cpu(*dbbuf_ei);
377b5f96cb7SKlaus Jensen if (!nvme_dbbuf_need_event(event_idx, value, old_value))
378f9f38e33SHelen Koike return false;
379f9f38e33SHelen Koike }
380f9f38e33SHelen Koike
381f9f38e33SHelen Koike return true;
38257dacad5SJay Sternberg }
38357dacad5SJay Sternberg
38457dacad5SJay Sternberg /*
38557dacad5SJay Sternberg * Will slightly overestimate the number of pages needed. This is OK
38657dacad5SJay Sternberg * as it only leads to a small amount of wasted memory for the lifetime of
38757dacad5SJay Sternberg * the I/O.
38857dacad5SJay Sternberg */
nvme_pci_npages_prp(void)389b13c6393SChaitanya Kulkarni static int nvme_pci_npages_prp(void)
39057dacad5SJay Sternberg {
391c89a529eSKeith Busch unsigned max_bytes = (NVME_MAX_KB_SZ * 1024) + NVME_CTRL_PAGE_SIZE;
392c89a529eSKeith Busch unsigned nprps = DIV_ROUND_UP(max_bytes, NVME_CTRL_PAGE_SIZE);
39384173423SKeith Busch return DIV_ROUND_UP(8 * nprps, NVME_CTRL_PAGE_SIZE - 8);
39457dacad5SJay Sternberg }
39557dacad5SJay Sternberg
nvme_admin_init_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)39657dacad5SJay Sternberg static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
39757dacad5SJay Sternberg unsigned int hctx_idx)
39857dacad5SJay Sternberg {
3990da7feaaSChristoph Hellwig struct nvme_dev *dev = to_nvme_dev(data);
400147b27e4SSagi Grimberg struct nvme_queue *nvmeq = &dev->queues[0];
40157dacad5SJay Sternberg
40257dacad5SJay Sternberg WARN_ON(hctx_idx != 0);
40357dacad5SJay Sternberg WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
40457dacad5SJay Sternberg
40557dacad5SJay Sternberg hctx->driver_data = nvmeq;
40657dacad5SJay Sternberg return 0;
40757dacad5SJay Sternberg }
40857dacad5SJay Sternberg
nvme_init_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)40957dacad5SJay Sternberg static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
41057dacad5SJay Sternberg unsigned int hctx_idx)
41157dacad5SJay Sternberg {
4120da7feaaSChristoph Hellwig struct nvme_dev *dev = to_nvme_dev(data);
413147b27e4SSagi Grimberg struct nvme_queue *nvmeq = &dev->queues[hctx_idx + 1];
41457dacad5SJay Sternberg
41557dacad5SJay Sternberg WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
41657dacad5SJay Sternberg hctx->driver_data = nvmeq;
41757dacad5SJay Sternberg return 0;
41857dacad5SJay Sternberg }
41957dacad5SJay Sternberg
nvme_pci_init_request(struct blk_mq_tag_set * set,struct request * req,unsigned int hctx_idx,unsigned int numa_node)420e559398fSChristoph Hellwig static int nvme_pci_init_request(struct blk_mq_tag_set *set,
421e559398fSChristoph Hellwig struct request *req, unsigned int hctx_idx,
422e559398fSChristoph Hellwig unsigned int numa_node)
42357dacad5SJay Sternberg {
424f4800d6dSChristoph Hellwig struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
42559e29ce6SSagi Grimberg
4264a4d9bc0SIrvin Cote nvme_req(req)->ctrl = set->driver_data;
427f4b9e6c9SKeith Busch nvme_req(req)->cmd = &iod->cmd;
42857dacad5SJay Sternberg return 0;
42957dacad5SJay Sternberg }
43057dacad5SJay Sternberg
queue_irq_offset(struct nvme_dev * dev)4313b6592f7SJens Axboe static int queue_irq_offset(struct nvme_dev *dev)
4323b6592f7SJens Axboe {
4333b6592f7SJens Axboe /* if we have more than 1 vec, admin queue offsets us by 1 */
4343b6592f7SJens Axboe if (dev->num_vecs > 1)
4353b6592f7SJens Axboe return 1;
4363b6592f7SJens Axboe
4373b6592f7SJens Axboe return 0;
4383b6592f7SJens Axboe }
4393b6592f7SJens Axboe
nvme_pci_map_queues(struct blk_mq_tag_set * set)440a4e1d0b7SBart Van Assche static void nvme_pci_map_queues(struct blk_mq_tag_set *set)
441dca51e78SChristoph Hellwig {
4420da7feaaSChristoph Hellwig struct nvme_dev *dev = to_nvme_dev(set->driver_data);
4433b6592f7SJens Axboe int i, qoff, offset;
444dca51e78SChristoph Hellwig
4453b6592f7SJens Axboe offset = queue_irq_offset(dev);
4463b6592f7SJens Axboe for (i = 0, qoff = 0; i < set->nr_maps; i++) {
4473b6592f7SJens Axboe struct blk_mq_queue_map *map = &set->map[i];
4483b6592f7SJens Axboe
4493b6592f7SJens Axboe map->nr_queues = dev->io_queues[i];
4503b6592f7SJens Axboe if (!map->nr_queues) {
451e20ba6e1SChristoph Hellwig BUG_ON(i == HCTX_TYPE_DEFAULT);
4527e849dd9SChristoph Hellwig continue;
4533b6592f7SJens Axboe }
4543b6592f7SJens Axboe
4554b04cc6aSJens Axboe /*
4564b04cc6aSJens Axboe * The poll queue(s) doesn't have an IRQ (and hence IRQ
4574b04cc6aSJens Axboe * affinity), so use the regular blk-mq cpu mapping
4584b04cc6aSJens Axboe */
4593b6592f7SJens Axboe map->queue_offset = qoff;
460cb9e0e50SKeith Busch if (i != HCTX_TYPE_POLL && offset)
4613b6592f7SJens Axboe blk_mq_pci_map_queues(map, to_pci_dev(dev->dev), offset);
4624b04cc6aSJens Axboe else
4634b04cc6aSJens Axboe blk_mq_map_queues(map);
4643b6592f7SJens Axboe qoff += map->nr_queues;
4653b6592f7SJens Axboe offset += map->nr_queues;
4663b6592f7SJens Axboe }
467dca51e78SChristoph Hellwig }
468dca51e78SChristoph Hellwig
46938210800SKeith Busch /*
47038210800SKeith Busch * Write sq tail if we are asked to, or if the next command would wrap.
47138210800SKeith Busch */
nvme_write_sq_db(struct nvme_queue * nvmeq,bool write_sq)47238210800SKeith Busch static inline void nvme_write_sq_db(struct nvme_queue *nvmeq, bool write_sq)
47304f3eafdSJens Axboe {
47438210800SKeith Busch if (!write_sq) {
47538210800SKeith Busch u16 next_tail = nvmeq->sq_tail + 1;
47638210800SKeith Busch
47738210800SKeith Busch if (next_tail == nvmeq->q_depth)
47838210800SKeith Busch next_tail = 0;
47938210800SKeith Busch if (next_tail != nvmeq->last_sq_tail)
48038210800SKeith Busch return;
48138210800SKeith Busch }
48238210800SKeith Busch
48304f3eafdSJens Axboe if (nvme_dbbuf_update_and_check_event(nvmeq->sq_tail,
48404f3eafdSJens Axboe nvmeq->dbbuf_sq_db, nvmeq->dbbuf_sq_ei))
48504f3eafdSJens Axboe writel(nvmeq->sq_tail, nvmeq->q_db);
48638210800SKeith Busch nvmeq->last_sq_tail = nvmeq->sq_tail;
48704f3eafdSJens Axboe }
48804f3eafdSJens Axboe
nvme_sq_copy_cmd(struct nvme_queue * nvmeq,struct nvme_command * cmd)4893233b94cSJens Axboe static inline void nvme_sq_copy_cmd(struct nvme_queue *nvmeq,
4903233b94cSJens Axboe struct nvme_command *cmd)
49157dacad5SJay Sternberg {
492c1e0cc7eSBenjamin Herrenschmidt memcpy(nvmeq->sq_cmds + (nvmeq->sq_tail << nvmeq->sqes),
4933233b94cSJens Axboe absolute_pointer(cmd), sizeof(*cmd));
49490ea5ca4SChristoph Hellwig if (++nvmeq->sq_tail == nvmeq->q_depth)
49590ea5ca4SChristoph Hellwig nvmeq->sq_tail = 0;
49604f3eafdSJens Axboe }
49704f3eafdSJens Axboe
nvme_commit_rqs(struct blk_mq_hw_ctx * hctx)49804f3eafdSJens Axboe static void nvme_commit_rqs(struct blk_mq_hw_ctx *hctx)
49904f3eafdSJens Axboe {
50004f3eafdSJens Axboe struct nvme_queue *nvmeq = hctx->driver_data;
50104f3eafdSJens Axboe
50204f3eafdSJens Axboe spin_lock(&nvmeq->sq_lock);
50338210800SKeith Busch if (nvmeq->sq_tail != nvmeq->last_sq_tail)
50438210800SKeith Busch nvme_write_sq_db(nvmeq, true);
50590ea5ca4SChristoph Hellwig spin_unlock(&nvmeq->sq_lock);
50657dacad5SJay Sternberg }
50757dacad5SJay Sternberg
nvme_pci_use_sgls(struct nvme_dev * dev,struct request * req,int nseg)508ae582935SKeith Busch static inline bool nvme_pci_use_sgls(struct nvme_dev *dev, struct request *req,
509ae582935SKeith Busch int nseg)
510955b1b5aSMinwoo Im {
511a53232cbSKeith Busch struct nvme_queue *nvmeq = req->mq_hctx->driver_data;
512955b1b5aSMinwoo Im unsigned int avg_seg_size;
513955b1b5aSMinwoo Im
51420469a37SKeith Busch avg_seg_size = DIV_ROUND_UP(blk_rq_payload_bytes(req), nseg);
515955b1b5aSMinwoo Im
516253a0b76SChaitanya Kulkarni if (!nvme_ctrl_sgl_supported(&dev->ctrl))
517955b1b5aSMinwoo Im return false;
518a53232cbSKeith Busch if (!nvmeq->qid)
519955b1b5aSMinwoo Im return false;
520955b1b5aSMinwoo Im if (!sgl_threshold || avg_seg_size < sgl_threshold)
521955b1b5aSMinwoo Im return false;
522955b1b5aSMinwoo Im return true;
523955b1b5aSMinwoo Im }
524955b1b5aSMinwoo Im
nvme_free_prps(struct nvme_dev * dev,struct request * req)5259275c206SChristoph Hellwig static void nvme_free_prps(struct nvme_dev *dev, struct request *req)
52657dacad5SJay Sternberg {
5276c3c05b0SChaitanya Kulkarni const int last_prp = NVME_CTRL_PAGE_SIZE / sizeof(__le64) - 1;
5289275c206SChristoph Hellwig struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
5299275c206SChristoph Hellwig dma_addr_t dma_addr = iod->first_dma;
53057dacad5SJay Sternberg int i;
53157dacad5SJay Sternberg
532c372cdd1SKeith Busch for (i = 0; i < iod->nr_allocations; i++) {
5337846c1b5SKeith Busch __le64 *prp_list = iod->list[i].prp_list;
5349275c206SChristoph Hellwig dma_addr_t next_dma_addr = le64_to_cpu(prp_list[last_prp]);
5359275c206SChristoph Hellwig
5369275c206SChristoph Hellwig dma_pool_free(dev->prp_page_pool, prp_list, dma_addr);
5379275c206SChristoph Hellwig dma_addr = next_dma_addr;
538dff824b2SChristoph Hellwig }
5399275c206SChristoph Hellwig }
5409275c206SChristoph Hellwig
nvme_unmap_data(struct nvme_dev * dev,struct request * req)5419275c206SChristoph Hellwig static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
5429275c206SChristoph Hellwig {
5439275c206SChristoph Hellwig struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
5447fe07d14SChristoph Hellwig
5459275c206SChristoph Hellwig if (iod->dma_len) {
5469275c206SChristoph Hellwig dma_unmap_page(dev->dev, iod->first_dma, iod->dma_len,
5479275c206SChristoph Hellwig rq_dma_dir(req));
5489275c206SChristoph Hellwig return;
5499275c206SChristoph Hellwig }
5509275c206SChristoph Hellwig
55191fb2b60SLogan Gunthorpe WARN_ON_ONCE(!iod->sgt.nents);
5529275c206SChristoph Hellwig
55391fb2b60SLogan Gunthorpe dma_unmap_sgtable(dev->dev, &iod->sgt, rq_dma_dir(req), 0);
55491fb2b60SLogan Gunthorpe
555c372cdd1SKeith Busch if (iod->nr_allocations == 0)
5567846c1b5SKeith Busch dma_pool_free(dev->prp_small_pool, iod->list[0].sg_list,
5579275c206SChristoph Hellwig iod->first_dma);
5588f0edf45SKeith Busch else if (iod->nr_allocations == 1)
5597846c1b5SKeith Busch dma_pool_free(dev->prp_page_pool, iod->list[0].sg_list,
56001df742dSKeith Busch iod->first_dma);
5619275c206SChristoph Hellwig else
5629275c206SChristoph Hellwig nvme_free_prps(dev, req);
56391fb2b60SLogan Gunthorpe mempool_free(iod->sgt.sgl, dev->iod_mempool);
56457dacad5SJay Sternberg }
56557dacad5SJay Sternberg
nvme_print_sgl(struct scatterlist * sgl,int nents)566d0877473SKeith Busch static void nvme_print_sgl(struct scatterlist *sgl, int nents)
567d0877473SKeith Busch {
568d0877473SKeith Busch int i;
569d0877473SKeith Busch struct scatterlist *sg;
570d0877473SKeith Busch
571d0877473SKeith Busch for_each_sg(sgl, sg, nents, i) {
572d0877473SKeith Busch dma_addr_t phys = sg_phys(sg);
573d0877473SKeith Busch pr_warn("sg[%d] phys_addr:%pad offset:%d length:%d "
574d0877473SKeith Busch "dma_address:%pad dma_length:%d\n",
575d0877473SKeith Busch i, &phys, sg->offset, sg->length, &sg_dma_address(sg),
576d0877473SKeith Busch sg_dma_len(sg));
577d0877473SKeith Busch }
578d0877473SKeith Busch }
579d0877473SKeith Busch
nvme_pci_setup_prps(struct nvme_dev * dev,struct request * req,struct nvme_rw_command * cmnd)580a7a7cbe3SChaitanya Kulkarni static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev,
581a7a7cbe3SChaitanya Kulkarni struct request *req, struct nvme_rw_command *cmnd)
58257dacad5SJay Sternberg {
583f4800d6dSChristoph Hellwig struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
58457dacad5SJay Sternberg struct dma_pool *pool;
585b131c61dSChristoph Hellwig int length = blk_rq_payload_bytes(req);
58691fb2b60SLogan Gunthorpe struct scatterlist *sg = iod->sgt.sgl;
58757dacad5SJay Sternberg int dma_len = sg_dma_len(sg);
58857dacad5SJay Sternberg u64 dma_addr = sg_dma_address(sg);
5896c3c05b0SChaitanya Kulkarni int offset = dma_addr & (NVME_CTRL_PAGE_SIZE - 1);
59057dacad5SJay Sternberg __le64 *prp_list;
59157dacad5SJay Sternberg dma_addr_t prp_dma;
59257dacad5SJay Sternberg int nprps, i;
59357dacad5SJay Sternberg
5946c3c05b0SChaitanya Kulkarni length -= (NVME_CTRL_PAGE_SIZE - offset);
5955228b328SJan H. Schönherr if (length <= 0) {
5965228b328SJan H. Schönherr iod->first_dma = 0;
597a7a7cbe3SChaitanya Kulkarni goto done;
5985228b328SJan H. Schönherr }
59957dacad5SJay Sternberg
6006c3c05b0SChaitanya Kulkarni dma_len -= (NVME_CTRL_PAGE_SIZE - offset);
60157dacad5SJay Sternberg if (dma_len) {
6026c3c05b0SChaitanya Kulkarni dma_addr += (NVME_CTRL_PAGE_SIZE - offset);
60357dacad5SJay Sternberg } else {
60457dacad5SJay Sternberg sg = sg_next(sg);
60557dacad5SJay Sternberg dma_addr = sg_dma_address(sg);
60657dacad5SJay Sternberg dma_len = sg_dma_len(sg);
60757dacad5SJay Sternberg }
60857dacad5SJay Sternberg
6096c3c05b0SChaitanya Kulkarni if (length <= NVME_CTRL_PAGE_SIZE) {
61057dacad5SJay Sternberg iod->first_dma = dma_addr;
611a7a7cbe3SChaitanya Kulkarni goto done;
61257dacad5SJay Sternberg }
61357dacad5SJay Sternberg
6146c3c05b0SChaitanya Kulkarni nprps = DIV_ROUND_UP(length, NVME_CTRL_PAGE_SIZE);
61557dacad5SJay Sternberg if (nprps <= (256 / 8)) {
61657dacad5SJay Sternberg pool = dev->prp_small_pool;
617c372cdd1SKeith Busch iod->nr_allocations = 0;
61857dacad5SJay Sternberg } else {
61957dacad5SJay Sternberg pool = dev->prp_page_pool;
620c372cdd1SKeith Busch iod->nr_allocations = 1;
62157dacad5SJay Sternberg }
62257dacad5SJay Sternberg
62369d2b571SChristoph Hellwig prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
62457dacad5SJay Sternberg if (!prp_list) {
625c372cdd1SKeith Busch iod->nr_allocations = -1;
62686eea289SKeith Busch return BLK_STS_RESOURCE;
62757dacad5SJay Sternberg }
6287846c1b5SKeith Busch iod->list[0].prp_list = prp_list;
62957dacad5SJay Sternberg iod->first_dma = prp_dma;
63057dacad5SJay Sternberg i = 0;
63157dacad5SJay Sternberg for (;;) {
6326c3c05b0SChaitanya Kulkarni if (i == NVME_CTRL_PAGE_SIZE >> 3) {
63357dacad5SJay Sternberg __le64 *old_prp_list = prp_list;
63469d2b571SChristoph Hellwig prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
63557dacad5SJay Sternberg if (!prp_list)
636fa073216SChristoph Hellwig goto free_prps;
6377846c1b5SKeith Busch iod->list[iod->nr_allocations++].prp_list = prp_list;
63857dacad5SJay Sternberg prp_list[0] = old_prp_list[i - 1];
63957dacad5SJay Sternberg old_prp_list[i - 1] = cpu_to_le64(prp_dma);
64057dacad5SJay Sternberg i = 1;
64157dacad5SJay Sternberg }
64257dacad5SJay Sternberg prp_list[i++] = cpu_to_le64(dma_addr);
6436c3c05b0SChaitanya Kulkarni dma_len -= NVME_CTRL_PAGE_SIZE;
6446c3c05b0SChaitanya Kulkarni dma_addr += NVME_CTRL_PAGE_SIZE;
6456c3c05b0SChaitanya Kulkarni length -= NVME_CTRL_PAGE_SIZE;
64657dacad5SJay Sternberg if (length <= 0)
64757dacad5SJay Sternberg break;
64857dacad5SJay Sternberg if (dma_len > 0)
64957dacad5SJay Sternberg continue;
65086eea289SKeith Busch if (unlikely(dma_len < 0))
65186eea289SKeith Busch goto bad_sgl;
65257dacad5SJay Sternberg sg = sg_next(sg);
65357dacad5SJay Sternberg dma_addr = sg_dma_address(sg);
65457dacad5SJay Sternberg dma_len = sg_dma_len(sg);
65557dacad5SJay Sternberg }
656a7a7cbe3SChaitanya Kulkarni done:
65791fb2b60SLogan Gunthorpe cmnd->dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sgt.sgl));
658a7a7cbe3SChaitanya Kulkarni cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma);
65986eea289SKeith Busch return BLK_STS_OK;
660fa073216SChristoph Hellwig free_prps:
661fa073216SChristoph Hellwig nvme_free_prps(dev, req);
662fa073216SChristoph Hellwig return BLK_STS_RESOURCE;
66386eea289SKeith Busch bad_sgl:
66491fb2b60SLogan Gunthorpe WARN(DO_ONCE(nvme_print_sgl, iod->sgt.sgl, iod->sgt.nents),
665d0877473SKeith Busch "Invalid SGL for payload:%d nents:%d\n",
66691fb2b60SLogan Gunthorpe blk_rq_payload_bytes(req), iod->sgt.nents);
66786eea289SKeith Busch return BLK_STS_IOERR;
66857dacad5SJay Sternberg }
66957dacad5SJay Sternberg
nvme_pci_sgl_set_data(struct nvme_sgl_desc * sge,struct scatterlist * sg)670a7a7cbe3SChaitanya Kulkarni static void nvme_pci_sgl_set_data(struct nvme_sgl_desc *sge,
671a7a7cbe3SChaitanya Kulkarni struct scatterlist *sg)
672a7a7cbe3SChaitanya Kulkarni {
673a7a7cbe3SChaitanya Kulkarni sge->addr = cpu_to_le64(sg_dma_address(sg));
674a7a7cbe3SChaitanya Kulkarni sge->length = cpu_to_le32(sg_dma_len(sg));
675a7a7cbe3SChaitanya Kulkarni sge->type = NVME_SGL_FMT_DATA_DESC << 4;
676a7a7cbe3SChaitanya Kulkarni }
677a7a7cbe3SChaitanya Kulkarni
nvme_pci_sgl_set_seg(struct nvme_sgl_desc * sge,dma_addr_t dma_addr,int entries)678a7a7cbe3SChaitanya Kulkarni static void nvme_pci_sgl_set_seg(struct nvme_sgl_desc *sge,
679a7a7cbe3SChaitanya Kulkarni dma_addr_t dma_addr, int entries)
680a7a7cbe3SChaitanya Kulkarni {
681a7a7cbe3SChaitanya Kulkarni sge->addr = cpu_to_le64(dma_addr);
682a7a7cbe3SChaitanya Kulkarni sge->length = cpu_to_le32(entries * sizeof(*sge));
683a7a7cbe3SChaitanya Kulkarni sge->type = NVME_SGL_FMT_LAST_SEG_DESC << 4;
684a7a7cbe3SChaitanya Kulkarni }
685a7a7cbe3SChaitanya Kulkarni
nvme_pci_setup_sgls(struct nvme_dev * dev,struct request * req,struct nvme_rw_command * cmd)686a7a7cbe3SChaitanya Kulkarni static blk_status_t nvme_pci_setup_sgls(struct nvme_dev *dev,
68791fb2b60SLogan Gunthorpe struct request *req, struct nvme_rw_command *cmd)
688a7a7cbe3SChaitanya Kulkarni {
689a7a7cbe3SChaitanya Kulkarni struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
690a7a7cbe3SChaitanya Kulkarni struct dma_pool *pool;
691a7a7cbe3SChaitanya Kulkarni struct nvme_sgl_desc *sg_list;
69291fb2b60SLogan Gunthorpe struct scatterlist *sg = iod->sgt.sgl;
69391fb2b60SLogan Gunthorpe unsigned int entries = iod->sgt.nents;
694a7a7cbe3SChaitanya Kulkarni dma_addr_t sgl_dma;
695b0f2853bSChristoph Hellwig int i = 0;
696a7a7cbe3SChaitanya Kulkarni
697a7a7cbe3SChaitanya Kulkarni /* setting the transfer type as SGL */
698a7a7cbe3SChaitanya Kulkarni cmd->flags = NVME_CMD_SGL_METABUF;
699a7a7cbe3SChaitanya Kulkarni
700b0f2853bSChristoph Hellwig if (entries == 1) {
701a7a7cbe3SChaitanya Kulkarni nvme_pci_sgl_set_data(&cmd->dptr.sgl, sg);
702a7a7cbe3SChaitanya Kulkarni return BLK_STS_OK;
703a7a7cbe3SChaitanya Kulkarni }
704a7a7cbe3SChaitanya Kulkarni
705a7a7cbe3SChaitanya Kulkarni if (entries <= (256 / sizeof(struct nvme_sgl_desc))) {
706a7a7cbe3SChaitanya Kulkarni pool = dev->prp_small_pool;
707c372cdd1SKeith Busch iod->nr_allocations = 0;
708a7a7cbe3SChaitanya Kulkarni } else {
709a7a7cbe3SChaitanya Kulkarni pool = dev->prp_page_pool;
710c372cdd1SKeith Busch iod->nr_allocations = 1;
711a7a7cbe3SChaitanya Kulkarni }
712a7a7cbe3SChaitanya Kulkarni
713a7a7cbe3SChaitanya Kulkarni sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma);
714a7a7cbe3SChaitanya Kulkarni if (!sg_list) {
715c372cdd1SKeith Busch iod->nr_allocations = -1;
716a7a7cbe3SChaitanya Kulkarni return BLK_STS_RESOURCE;
717a7a7cbe3SChaitanya Kulkarni }
718a7a7cbe3SChaitanya Kulkarni
7197846c1b5SKeith Busch iod->list[0].sg_list = sg_list;
720a7a7cbe3SChaitanya Kulkarni iod->first_dma = sgl_dma;
721a7a7cbe3SChaitanya Kulkarni
722a7a7cbe3SChaitanya Kulkarni nvme_pci_sgl_set_seg(&cmd->dptr.sgl, sgl_dma, entries);
723a7a7cbe3SChaitanya Kulkarni do {
724a7a7cbe3SChaitanya Kulkarni nvme_pci_sgl_set_data(&sg_list[i++], sg);
725a7a7cbe3SChaitanya Kulkarni sg = sg_next(sg);
726b0f2853bSChristoph Hellwig } while (--entries > 0);
727a7a7cbe3SChaitanya Kulkarni
728a7a7cbe3SChaitanya Kulkarni return BLK_STS_OK;
729a7a7cbe3SChaitanya Kulkarni }
730a7a7cbe3SChaitanya Kulkarni
nvme_setup_prp_simple(struct nvme_dev * dev,struct request * req,struct nvme_rw_command * cmnd,struct bio_vec * bv)731dff824b2SChristoph Hellwig static blk_status_t nvme_setup_prp_simple(struct nvme_dev *dev,
732dff824b2SChristoph Hellwig struct request *req, struct nvme_rw_command *cmnd,
733dff824b2SChristoph Hellwig struct bio_vec *bv)
734dff824b2SChristoph Hellwig {
735dff824b2SChristoph Hellwig struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
7366c3c05b0SChaitanya Kulkarni unsigned int offset = bv->bv_offset & (NVME_CTRL_PAGE_SIZE - 1);
7376c3c05b0SChaitanya Kulkarni unsigned int first_prp_len = NVME_CTRL_PAGE_SIZE - offset;
738dff824b2SChristoph Hellwig
739dff824b2SChristoph Hellwig iod->first_dma = dma_map_bvec(dev->dev, bv, rq_dma_dir(req), 0);
740dff824b2SChristoph Hellwig if (dma_mapping_error(dev->dev, iod->first_dma))
741dff824b2SChristoph Hellwig return BLK_STS_RESOURCE;
742dff824b2SChristoph Hellwig iod->dma_len = bv->bv_len;
743dff824b2SChristoph Hellwig
744dff824b2SChristoph Hellwig cmnd->dptr.prp1 = cpu_to_le64(iod->first_dma);
745dff824b2SChristoph Hellwig if (bv->bv_len > first_prp_len)
746dff824b2SChristoph Hellwig cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma + first_prp_len);
747a56ea614SLei Rao else
748a56ea614SLei Rao cmnd->dptr.prp2 = 0;
749359c1f88SBaolin Wang return BLK_STS_OK;
750dff824b2SChristoph Hellwig }
751dff824b2SChristoph Hellwig
nvme_setup_sgl_simple(struct nvme_dev * dev,struct request * req,struct nvme_rw_command * cmnd,struct bio_vec * bv)75229791057SChristoph Hellwig static blk_status_t nvme_setup_sgl_simple(struct nvme_dev *dev,
75329791057SChristoph Hellwig struct request *req, struct nvme_rw_command *cmnd,
75429791057SChristoph Hellwig struct bio_vec *bv)
75529791057SChristoph Hellwig {
75629791057SChristoph Hellwig struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
75729791057SChristoph Hellwig
75829791057SChristoph Hellwig iod->first_dma = dma_map_bvec(dev->dev, bv, rq_dma_dir(req), 0);
75929791057SChristoph Hellwig if (dma_mapping_error(dev->dev, iod->first_dma))
76029791057SChristoph Hellwig return BLK_STS_RESOURCE;
76129791057SChristoph Hellwig iod->dma_len = bv->bv_len;
76229791057SChristoph Hellwig
763049bf372SKlaus Birkelund Jensen cmnd->flags = NVME_CMD_SGL_METABUF;
76429791057SChristoph Hellwig cmnd->dptr.sgl.addr = cpu_to_le64(iod->first_dma);
76529791057SChristoph Hellwig cmnd->dptr.sgl.length = cpu_to_le32(iod->dma_len);
76629791057SChristoph Hellwig cmnd->dptr.sgl.type = NVME_SGL_FMT_DATA_DESC << 4;
767359c1f88SBaolin Wang return BLK_STS_OK;
76829791057SChristoph Hellwig }
76929791057SChristoph Hellwig
nvme_map_data(struct nvme_dev * dev,struct request * req,struct nvme_command * cmnd)770fc17b653SChristoph Hellwig static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,
771b131c61dSChristoph Hellwig struct nvme_command *cmnd)
77257dacad5SJay Sternberg {
773f4800d6dSChristoph Hellwig struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
77470479b71SChristoph Hellwig blk_status_t ret = BLK_STS_RESOURCE;
77591fb2b60SLogan Gunthorpe int rc;
77657dacad5SJay Sternberg
777dff824b2SChristoph Hellwig if (blk_rq_nr_phys_segments(req) == 1) {
778a53232cbSKeith Busch struct nvme_queue *nvmeq = req->mq_hctx->driver_data;
779dff824b2SChristoph Hellwig struct bio_vec bv = req_bvec(req);
780dff824b2SChristoph Hellwig
781dff824b2SChristoph Hellwig if (!is_pci_p2pdma_page(bv.bv_page)) {
782b955b479SKundan Kumar if ((bv.bv_offset & (NVME_CTRL_PAGE_SIZE - 1)) +
783b955b479SKundan Kumar bv.bv_len <= NVME_CTRL_PAGE_SIZE * 2)
784dff824b2SChristoph Hellwig return nvme_setup_prp_simple(dev, req,
785dff824b2SChristoph Hellwig &cmnd->rw, &bv);
78629791057SChristoph Hellwig
787a53232cbSKeith Busch if (nvmeq->qid && sgl_threshold &&
788253a0b76SChaitanya Kulkarni nvme_ctrl_sgl_supported(&dev->ctrl))
78929791057SChristoph Hellwig return nvme_setup_sgl_simple(dev, req,
79029791057SChristoph Hellwig &cmnd->rw, &bv);
791dff824b2SChristoph Hellwig }
792dff824b2SChristoph Hellwig }
793dff824b2SChristoph Hellwig
794dff824b2SChristoph Hellwig iod->dma_len = 0;
79591fb2b60SLogan Gunthorpe iod->sgt.sgl = mempool_alloc(dev->iod_mempool, GFP_ATOMIC);
79691fb2b60SLogan Gunthorpe if (!iod->sgt.sgl)
7979b048119SChristoph Hellwig return BLK_STS_RESOURCE;
79891fb2b60SLogan Gunthorpe sg_init_table(iod->sgt.sgl, blk_rq_nr_phys_segments(req));
79991fb2b60SLogan Gunthorpe iod->sgt.orig_nents = blk_rq_map_sg(req->q, req, iod->sgt.sgl);
80091fb2b60SLogan Gunthorpe if (!iod->sgt.orig_nents)
801fa073216SChristoph Hellwig goto out_free_sg;
802ba1ca37eSChristoph Hellwig
80391fb2b60SLogan Gunthorpe rc = dma_map_sgtable(dev->dev, &iod->sgt, rq_dma_dir(req),
80491fb2b60SLogan Gunthorpe DMA_ATTR_NO_WARN);
80591fb2b60SLogan Gunthorpe if (rc) {
80691fb2b60SLogan Gunthorpe if (rc == -EREMOTEIO)
80791fb2b60SLogan Gunthorpe ret = BLK_STS_TARGET;
808fa073216SChristoph Hellwig goto out_free_sg;
80991fb2b60SLogan Gunthorpe }
810ba1ca37eSChristoph Hellwig
811b6c0c237SKeith Busch if (nvme_pci_use_sgls(dev, req, iod->sgt.nents))
81291fb2b60SLogan Gunthorpe ret = nvme_pci_setup_sgls(dev, req, &cmnd->rw);
813a7a7cbe3SChaitanya Kulkarni else
814a7a7cbe3SChaitanya Kulkarni ret = nvme_pci_setup_prps(dev, req, &cmnd->rw);
8154aedb705SChristoph Hellwig if (ret != BLK_STS_OK)
816fa073216SChristoph Hellwig goto out_unmap_sg;
817fa073216SChristoph Hellwig return BLK_STS_OK;
818fa073216SChristoph Hellwig
819fa073216SChristoph Hellwig out_unmap_sg:
82091fb2b60SLogan Gunthorpe dma_unmap_sgtable(dev->dev, &iod->sgt, rq_dma_dir(req), 0);
821fa073216SChristoph Hellwig out_free_sg:
82291fb2b60SLogan Gunthorpe mempool_free(iod->sgt.sgl, dev->iod_mempool);
823ba1ca37eSChristoph Hellwig return ret;
82457dacad5SJay Sternberg }
82557dacad5SJay Sternberg
nvme_map_metadata(struct nvme_dev * dev,struct request * req,struct nvme_command * cmnd)8264aedb705SChristoph Hellwig static blk_status_t nvme_map_metadata(struct nvme_dev *dev, struct request *req,
8274aedb705SChristoph Hellwig struct nvme_command *cmnd)
8284aedb705SChristoph Hellwig {
8294aedb705SChristoph Hellwig struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
830c63b44fbSMikulas Patocka struct bio_vec bv = rq_integrity_vec(req);
8314aedb705SChristoph Hellwig
832c63b44fbSMikulas Patocka iod->meta_dma = dma_map_bvec(dev->dev, &bv, rq_dma_dir(req), 0);
8334aedb705SChristoph Hellwig if (dma_mapping_error(dev->dev, iod->meta_dma))
8344aedb705SChristoph Hellwig return BLK_STS_IOERR;
8354aedb705SChristoph Hellwig cmnd->rw.metadata = cpu_to_le64(iod->meta_dma);
836359c1f88SBaolin Wang return BLK_STS_OK;
8374aedb705SChristoph Hellwig }
8384aedb705SChristoph Hellwig
nvme_prep_rq(struct nvme_dev * dev,struct request * req)83962451a2bSJens Axboe static blk_status_t nvme_prep_rq(struct nvme_dev *dev, struct request *req)
84062451a2bSJens Axboe {
84162451a2bSJens Axboe struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
84262451a2bSJens Axboe blk_status_t ret;
84362451a2bSJens Axboe
84452da4f3fSKeith Busch iod->aborted = false;
845c372cdd1SKeith Busch iod->nr_allocations = -1;
84691fb2b60SLogan Gunthorpe iod->sgt.nents = 0;
84762451a2bSJens Axboe
84862451a2bSJens Axboe ret = nvme_setup_cmd(req->q->queuedata, req);
84962451a2bSJens Axboe if (ret)
85062451a2bSJens Axboe return ret;
85162451a2bSJens Axboe
85262451a2bSJens Axboe if (blk_rq_nr_phys_segments(req)) {
85362451a2bSJens Axboe ret = nvme_map_data(dev, req, &iod->cmd);
85462451a2bSJens Axboe if (ret)
85562451a2bSJens Axboe goto out_free_cmd;
85662451a2bSJens Axboe }
85762451a2bSJens Axboe
85862451a2bSJens Axboe if (blk_integrity_rq(req)) {
85962451a2bSJens Axboe ret = nvme_map_metadata(dev, req, &iod->cmd);
86062451a2bSJens Axboe if (ret)
86162451a2bSJens Axboe goto out_unmap_data;
86262451a2bSJens Axboe }
86362451a2bSJens Axboe
8646887fc64SSagi Grimberg nvme_start_request(req);
86562451a2bSJens Axboe return BLK_STS_OK;
86662451a2bSJens Axboe out_unmap_data:
86777848b37SLeon Romanovsky if (blk_rq_nr_phys_segments(req))
86862451a2bSJens Axboe nvme_unmap_data(dev, req);
86962451a2bSJens Axboe out_free_cmd:
87062451a2bSJens Axboe nvme_cleanup_cmd(req);
87162451a2bSJens Axboe return ret;
87262451a2bSJens Axboe }
87362451a2bSJens Axboe
87457dacad5SJay Sternberg /*
87557dacad5SJay Sternberg * NOTE: ns is NULL when called on the admin queue.
87657dacad5SJay Sternberg */
nvme_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)877fc17b653SChristoph Hellwig static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
87857dacad5SJay Sternberg const struct blk_mq_queue_data *bd)
87957dacad5SJay Sternberg {
88057dacad5SJay Sternberg struct nvme_queue *nvmeq = hctx->driver_data;
88157dacad5SJay Sternberg struct nvme_dev *dev = nvmeq->dev;
88257dacad5SJay Sternberg struct request *req = bd->rq;
8839b048119SChristoph Hellwig struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
884ebe6d874SChristoph Hellwig blk_status_t ret;
88557dacad5SJay Sternberg
886d1f06f4aSJens Axboe /*
887d1f06f4aSJens Axboe * We should not need to do this, but we're still using this to
888d1f06f4aSJens Axboe * ensure we can drain requests on a dying queue.
889d1f06f4aSJens Axboe */
8904e224106SChristoph Hellwig if (unlikely(!test_bit(NVMEQ_ENABLED, &nvmeq->flags)))
891d1f06f4aSJens Axboe return BLK_STS_IOERR;
892d1f06f4aSJens Axboe
89362451a2bSJens Axboe if (unlikely(!nvme_check_ready(&dev->ctrl, req, true)))
894d4060d2bSTao Chiu return nvme_fail_nonready_command(&dev->ctrl, req);
895d4060d2bSTao Chiu
89662451a2bSJens Axboe ret = nvme_prep_rq(dev, req);
89762451a2bSJens Axboe if (unlikely(ret))
898f4800d6dSChristoph Hellwig return ret;
8993233b94cSJens Axboe spin_lock(&nvmeq->sq_lock);
9003233b94cSJens Axboe nvme_sq_copy_cmd(nvmeq, &iod->cmd);
9013233b94cSJens Axboe nvme_write_sq_db(nvmeq, bd->last);
9023233b94cSJens Axboe spin_unlock(&nvmeq->sq_lock);
903fc17b653SChristoph Hellwig return BLK_STS_OK;
90457dacad5SJay Sternberg }
90557dacad5SJay Sternberg
nvme_submit_cmds(struct nvme_queue * nvmeq,struct request ** rqlist)906d62cbcf6SJens Axboe static void nvme_submit_cmds(struct nvme_queue *nvmeq, struct request **rqlist)
907d62cbcf6SJens Axboe {
90851989929SChristoph Hellwig struct request *req;
90951989929SChristoph Hellwig
910d62cbcf6SJens Axboe spin_lock(&nvmeq->sq_lock);
91151989929SChristoph Hellwig while ((req = rq_list_pop(rqlist))) {
912d62cbcf6SJens Axboe struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
913d62cbcf6SJens Axboe
914d62cbcf6SJens Axboe nvme_sq_copy_cmd(nvmeq, &iod->cmd);
915d62cbcf6SJens Axboe }
916d62cbcf6SJens Axboe nvme_write_sq_db(nvmeq, true);
917d62cbcf6SJens Axboe spin_unlock(&nvmeq->sq_lock);
918d62cbcf6SJens Axboe }
919d62cbcf6SJens Axboe
nvme_prep_rq_batch(struct nvme_queue * nvmeq,struct request * req)920d62cbcf6SJens Axboe static bool nvme_prep_rq_batch(struct nvme_queue *nvmeq, struct request *req)
921d62cbcf6SJens Axboe {
922d62cbcf6SJens Axboe /*
923d62cbcf6SJens Axboe * We should not need to do this, but we're still using this to
924d62cbcf6SJens Axboe * ensure we can drain requests on a dying queue.
925d62cbcf6SJens Axboe */
926d62cbcf6SJens Axboe if (unlikely(!test_bit(NVMEQ_ENABLED, &nvmeq->flags)))
927d62cbcf6SJens Axboe return false;
928d62cbcf6SJens Axboe if (unlikely(!nvme_check_ready(&nvmeq->dev->ctrl, req, true)))
929d62cbcf6SJens Axboe return false;
930d62cbcf6SJens Axboe
931d62cbcf6SJens Axboe req->mq_hctx->tags->rqs[req->tag] = req;
932d62cbcf6SJens Axboe return nvme_prep_rq(nvmeq->dev, req) == BLK_STS_OK;
933d62cbcf6SJens Axboe }
934d62cbcf6SJens Axboe
nvme_queue_rqs(struct request ** rqlist)935d62cbcf6SJens Axboe static void nvme_queue_rqs(struct request **rqlist)
936d62cbcf6SJens Axboe {
93751989929SChristoph Hellwig struct request *submit_list = NULL;
938d62cbcf6SJens Axboe struct request *requeue_list = NULL;
93951989929SChristoph Hellwig struct request **requeue_lastp = &requeue_list;
94051989929SChristoph Hellwig struct nvme_queue *nvmeq = NULL;
94151989929SChristoph Hellwig struct request *req;
942d62cbcf6SJens Axboe
94351989929SChristoph Hellwig while ((req = rq_list_pop(rqlist))) {
94451989929SChristoph Hellwig if (nvmeq && nvmeq != req->mq_hctx->driver_data)
94551989929SChristoph Hellwig nvme_submit_cmds(nvmeq, &submit_list);
94651989929SChristoph Hellwig nvmeq = req->mq_hctx->driver_data;
947d62cbcf6SJens Axboe
94851989929SChristoph Hellwig if (nvme_prep_rq_batch(nvmeq, req))
94951989929SChristoph Hellwig rq_list_add(&submit_list, req); /* reverse order */
95051989929SChristoph Hellwig else
95151989929SChristoph Hellwig rq_list_add_tail(&requeue_lastp, req);
952d62cbcf6SJens Axboe }
953d62cbcf6SJens Axboe
95451989929SChristoph Hellwig if (nvmeq)
95551989929SChristoph Hellwig nvme_submit_cmds(nvmeq, &submit_list);
956d62cbcf6SJens Axboe *rqlist = requeue_list;
957d62cbcf6SJens Axboe }
958d62cbcf6SJens Axboe
nvme_pci_unmap_rq(struct request * req)959c234a653SJens Axboe static __always_inline void nvme_pci_unmap_rq(struct request *req)
960eee417b0SChristoph Hellwig {
961a53232cbSKeith Busch struct nvme_queue *nvmeq = req->mq_hctx->driver_data;
962a53232cbSKeith Busch struct nvme_dev *dev = nvmeq->dev;
963eee417b0SChristoph Hellwig
964a53232cbSKeith Busch if (blk_integrity_rq(req)) {
965a53232cbSKeith Busch struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
966a53232cbSKeith Busch
9674aedb705SChristoph Hellwig dma_unmap_page(dev->dev, iod->meta_dma,
968c63b44fbSMikulas Patocka rq_integrity_vec(req).bv_len, rq_dma_dir(req));
969a53232cbSKeith Busch }
970a53232cbSKeith Busch
971b15c592dSChristoph Hellwig if (blk_rq_nr_phys_segments(req))
9724aedb705SChristoph Hellwig nvme_unmap_data(dev, req);
973c234a653SJens Axboe }
974c234a653SJens Axboe
nvme_pci_complete_rq(struct request * req)975c234a653SJens Axboe static void nvme_pci_complete_rq(struct request *req)
976c234a653SJens Axboe {
977c234a653SJens Axboe nvme_pci_unmap_rq(req);
97877f02a7aSChristoph Hellwig nvme_complete_rq(req);
97957dacad5SJay Sternberg }
98057dacad5SJay Sternberg
nvme_pci_complete_batch(struct io_comp_batch * iob)981c234a653SJens Axboe static void nvme_pci_complete_batch(struct io_comp_batch *iob)
982c234a653SJens Axboe {
983c234a653SJens Axboe nvme_complete_batch(iob, nvme_pci_unmap_rq);
984c234a653SJens Axboe }
985c234a653SJens Axboe
986d783e0bdSMarta Rybczynska /* We read the CQE phase first to check if the rest of the entry is valid */
nvme_cqe_pending(struct nvme_queue * nvmeq)987750dde44SChristoph Hellwig static inline bool nvme_cqe_pending(struct nvme_queue *nvmeq)
988d783e0bdSMarta Rybczynska {
98974943d45SKeith Busch struct nvme_completion *hcqe = &nvmeq->cqes[nvmeq->cq_head];
99074943d45SKeith Busch
99174943d45SKeith Busch return (le16_to_cpu(READ_ONCE(hcqe->status)) & 1) == nvmeq->cq_phase;
992d783e0bdSMarta Rybczynska }
993d783e0bdSMarta Rybczynska
nvme_ring_cq_doorbell(struct nvme_queue * nvmeq)994eb281c82SSagi Grimberg static inline void nvme_ring_cq_doorbell(struct nvme_queue *nvmeq)
99557dacad5SJay Sternberg {
996eb281c82SSagi Grimberg u16 head = nvmeq->cq_head;
99757dacad5SJay Sternberg
998eb281c82SSagi Grimberg if (nvme_dbbuf_update_and_check_event(head, nvmeq->dbbuf_cq_db,
999eb281c82SSagi Grimberg nvmeq->dbbuf_cq_ei))
1000eb281c82SSagi Grimberg writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
1001eb281c82SSagi Grimberg }
1002adf68f21SChristoph Hellwig
nvme_queue_tagset(struct nvme_queue * nvmeq)1003cfa27356SChristoph Hellwig static inline struct blk_mq_tags *nvme_queue_tagset(struct nvme_queue *nvmeq)
1004cfa27356SChristoph Hellwig {
1005cfa27356SChristoph Hellwig if (!nvmeq->qid)
1006cfa27356SChristoph Hellwig return nvmeq->dev->admin_tagset.tags[0];
1007cfa27356SChristoph Hellwig return nvmeq->dev->tagset.tags[nvmeq->qid - 1];
1008cfa27356SChristoph Hellwig }
1009cfa27356SChristoph Hellwig
nvme_handle_cqe(struct nvme_queue * nvmeq,struct io_comp_batch * iob,u16 idx)1010c234a653SJens Axboe static inline void nvme_handle_cqe(struct nvme_queue *nvmeq,
1011c234a653SJens Axboe struct io_comp_batch *iob, u16 idx)
101257dacad5SJay Sternberg {
101374943d45SKeith Busch struct nvme_completion *cqe = &nvmeq->cqes[idx];
101462df8016SLalithambika Krishnakumar __u16 command_id = READ_ONCE(cqe->command_id);
101557dacad5SJay Sternberg struct request *req;
1016adf68f21SChristoph Hellwig
1017adf68f21SChristoph Hellwig /*
1018adf68f21SChristoph Hellwig * AEN requests are special as they don't time out and can
1019adf68f21SChristoph Hellwig * survive any kind of queue freeze and often don't respond to
1020adf68f21SChristoph Hellwig * aborts. We don't even bother to allocate a struct request
1021adf68f21SChristoph Hellwig * for them but rather special case them here.
1022adf68f21SChristoph Hellwig */
102362df8016SLalithambika Krishnakumar if (unlikely(nvme_is_aen_req(nvmeq->qid, command_id))) {
10247bf58533SChristoph Hellwig nvme_complete_async_event(&nvmeq->dev->ctrl,
102583a12fb7SSagi Grimberg cqe->status, &cqe->result);
1026a0fa9647SJens Axboe return;
102757dacad5SJay Sternberg }
102857dacad5SJay Sternberg
1029e7006de6SSagi Grimberg req = nvme_find_rq(nvme_queue_tagset(nvmeq), command_id);
103050b7c243SXianting Tian if (unlikely(!req)) {
103150b7c243SXianting Tian dev_warn(nvmeq->dev->ctrl.device,
103250b7c243SXianting Tian "invalid id %d completed on queue %d\n",
103362df8016SLalithambika Krishnakumar command_id, le16_to_cpu(cqe->sq_id));
103450b7c243SXianting Tian return;
103550b7c243SXianting Tian }
103650b7c243SXianting Tian
1037604c01d5Syupeng trace_nvme_sq(req, cqe->sq_head, nvmeq->sq_tail);
1038c234a653SJens Axboe if (!nvme_try_complete_req(req, cqe->status, cqe->result) &&
1039c234a653SJens Axboe !blk_mq_add_to_batch(req, iob, nvme_req(req)->status,
1040c234a653SJens Axboe nvme_pci_complete_batch))
1041ff029451SChristoph Hellwig nvme_pci_complete_rq(req);
104283a12fb7SSagi Grimberg }
104357dacad5SJay Sternberg
nvme_update_cq_head(struct nvme_queue * nvmeq)10445cb525c8SJens Axboe static inline void nvme_update_cq_head(struct nvme_queue *nvmeq)
10455cb525c8SJens Axboe {
1046a0aac973SJK Kim u32 tmp = nvmeq->cq_head + 1;
1047a8de6639SAlexey Dobriyan
1048a8de6639SAlexey Dobriyan if (tmp == nvmeq->q_depth) {
1049920d13a8SSagi Grimberg nvmeq->cq_head = 0;
1050e2a366a4SAlexey Dobriyan nvmeq->cq_phase ^= 1;
1051a8de6639SAlexey Dobriyan } else {
1052a8de6639SAlexey Dobriyan nvmeq->cq_head = tmp;
1053920d13a8SSagi Grimberg }
1054a0fa9647SJens Axboe }
1055a0fa9647SJens Axboe
nvme_poll_cq(struct nvme_queue * nvmeq,struct io_comp_batch * iob)1056c234a653SJens Axboe static inline int nvme_poll_cq(struct nvme_queue *nvmeq,
1057c234a653SJens Axboe struct io_comp_batch *iob)
1058a0fa9647SJens Axboe {
10591052b8acSJens Axboe int found = 0;
106083a12fb7SSagi Grimberg
10611052b8acSJens Axboe while (nvme_cqe_pending(nvmeq)) {
10621052b8acSJens Axboe found++;
1063b69e2ef2SKeith Busch /*
1064b69e2ef2SKeith Busch * load-load control dependency between phase and the rest of
1065b69e2ef2SKeith Busch * the cqe requires a full read memory barrier
1066b69e2ef2SKeith Busch */
1067b69e2ef2SKeith Busch dma_rmb();
1068c234a653SJens Axboe nvme_handle_cqe(nvmeq, iob, nvmeq->cq_head);
10695cb525c8SJens Axboe nvme_update_cq_head(nvmeq);
107057dacad5SJay Sternberg }
107157dacad5SJay Sternberg
1072324b494cSKeith Busch if (found)
1073eb281c82SSagi Grimberg nvme_ring_cq_doorbell(nvmeq);
10745cb525c8SJens Axboe return found;
107557dacad5SJay Sternberg }
107657dacad5SJay Sternberg
nvme_irq(int irq,void * data)107757dacad5SJay Sternberg static irqreturn_t nvme_irq(int irq, void *data)
107857dacad5SJay Sternberg {
107957dacad5SJay Sternberg struct nvme_queue *nvmeq = data;
10804f502245SJens Axboe DEFINE_IO_COMP_BATCH(iob);
10815cb525c8SJens Axboe
10824f502245SJens Axboe if (nvme_poll_cq(nvmeq, &iob)) {
10834f502245SJens Axboe if (!rq_list_empty(iob.req_list))
10844f502245SJens Axboe nvme_pci_complete_batch(&iob);
108505fae499SChaitanya Kulkarni return IRQ_HANDLED;
10864f502245SJens Axboe }
108705fae499SChaitanya Kulkarni return IRQ_NONE;
108857dacad5SJay Sternberg }
108957dacad5SJay Sternberg
nvme_irq_check(int irq,void * data)109057dacad5SJay Sternberg static irqreturn_t nvme_irq_check(int irq, void *data)
109157dacad5SJay Sternberg {
109257dacad5SJay Sternberg struct nvme_queue *nvmeq = data;
10934e523547SBaolin Wang
1094750dde44SChristoph Hellwig if (nvme_cqe_pending(nvmeq))
109557dacad5SJay Sternberg return IRQ_WAKE_THREAD;
1096d783e0bdSMarta Rybczynska return IRQ_NONE;
109757dacad5SJay Sternberg }
109857dacad5SJay Sternberg
10990b2a8a9fSChristoph Hellwig /*
1100fa059b85SKeith Busch * Poll for completions for any interrupt driven queue
11010b2a8a9fSChristoph Hellwig * Can be called from any context.
11020b2a8a9fSChristoph Hellwig */
nvme_poll_irqdisable(struct nvme_queue * nvmeq)1103fa059b85SKeith Busch static void nvme_poll_irqdisable(struct nvme_queue *nvmeq)
1104a0fa9647SJens Axboe {
11053a7afd8eSChristoph Hellwig struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev);
1106a0fa9647SJens Axboe
1107fa059b85SKeith Busch WARN_ON_ONCE(test_bit(NVMEQ_POLLED, &nvmeq->flags));
1108fa059b85SKeith Busch
11093a7afd8eSChristoph Hellwig disable_irq(pci_irq_vector(pdev, nvmeq->cq_vector));
1110c234a653SJens Axboe nvme_poll_cq(nvmeq, NULL);
11113a7afd8eSChristoph Hellwig enable_irq(pci_irq_vector(pdev, nvmeq->cq_vector));
111291a509f8SChristoph Hellwig }
1113442e19b7SSagi Grimberg
nvme_poll(struct blk_mq_hw_ctx * hctx,struct io_comp_batch * iob)11145a72e899SJens Axboe static int nvme_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
11157776db1cSKeith Busch {
11167776db1cSKeith Busch struct nvme_queue *nvmeq = hctx->driver_data;
1117dabcefabSJens Axboe bool found;
1118dabcefabSJens Axboe
1119dabcefabSJens Axboe if (!nvme_cqe_pending(nvmeq))
1120dabcefabSJens Axboe return 0;
1121dabcefabSJens Axboe
11223a7afd8eSChristoph Hellwig spin_lock(&nvmeq->cq_poll_lock);
1123c234a653SJens Axboe found = nvme_poll_cq(nvmeq, iob);
11243a7afd8eSChristoph Hellwig spin_unlock(&nvmeq->cq_poll_lock);
1125dabcefabSJens Axboe
1126dabcefabSJens Axboe return found;
1127dabcefabSJens Axboe }
1128dabcefabSJens Axboe
nvme_pci_submit_async_event(struct nvme_ctrl * ctrl)1129ad22c355SKeith Busch static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl)
113057dacad5SJay Sternberg {
1131f866fc42SChristoph Hellwig struct nvme_dev *dev = to_nvme_dev(ctrl);
1132147b27e4SSagi Grimberg struct nvme_queue *nvmeq = &dev->queues[0];
1133f66e2804SChaitanya Kulkarni struct nvme_command c = { };
113457dacad5SJay Sternberg
113557dacad5SJay Sternberg c.common.opcode = nvme_admin_async_event;
1136ad22c355SKeith Busch c.common.command_id = NVME_AQ_BLK_MQ_DEPTH;
11373233b94cSJens Axboe
11383233b94cSJens Axboe spin_lock(&nvmeq->sq_lock);
11393233b94cSJens Axboe nvme_sq_copy_cmd(nvmeq, &c);
11403233b94cSJens Axboe nvme_write_sq_db(nvmeq, true);
11413233b94cSJens Axboe spin_unlock(&nvmeq->sq_lock);
114257dacad5SJay Sternberg }
114357dacad5SJay Sternberg
adapter_delete_queue(struct nvme_dev * dev,u8 opcode,u16 id)114457dacad5SJay Sternberg static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
114557dacad5SJay Sternberg {
1146f66e2804SChaitanya Kulkarni struct nvme_command c = { };
114757dacad5SJay Sternberg
114857dacad5SJay Sternberg c.delete_queue.opcode = opcode;
114957dacad5SJay Sternberg c.delete_queue.qid = cpu_to_le16(id);
115057dacad5SJay Sternberg
11511c63dc66SChristoph Hellwig return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
115257dacad5SJay Sternberg }
115357dacad5SJay Sternberg
adapter_alloc_cq(struct nvme_dev * dev,u16 qid,struct nvme_queue * nvmeq,s16 vector)115457dacad5SJay Sternberg static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1155a8e3e0bbSJianchao Wang struct nvme_queue *nvmeq, s16 vector)
115657dacad5SJay Sternberg {
1157f66e2804SChaitanya Kulkarni struct nvme_command c = { };
11584b04cc6aSJens Axboe int flags = NVME_QUEUE_PHYS_CONTIG;
11594b04cc6aSJens Axboe
11607c349ddeSKeith Busch if (!test_bit(NVMEQ_POLLED, &nvmeq->flags))
11614b04cc6aSJens Axboe flags |= NVME_CQ_IRQ_ENABLED;
116257dacad5SJay Sternberg
116357dacad5SJay Sternberg /*
116416772ae6SMinwoo Im * Note: we (ab)use the fact that the prp fields survive if no data
116557dacad5SJay Sternberg * is attached to the request.
116657dacad5SJay Sternberg */
116757dacad5SJay Sternberg c.create_cq.opcode = nvme_admin_create_cq;
116857dacad5SJay Sternberg c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
116957dacad5SJay Sternberg c.create_cq.cqid = cpu_to_le16(qid);
117057dacad5SJay Sternberg c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
117157dacad5SJay Sternberg c.create_cq.cq_flags = cpu_to_le16(flags);
1172a8e3e0bbSJianchao Wang c.create_cq.irq_vector = cpu_to_le16(vector);
117357dacad5SJay Sternberg
11741c63dc66SChristoph Hellwig return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
117557dacad5SJay Sternberg }
117657dacad5SJay Sternberg
adapter_alloc_sq(struct nvme_dev * dev,u16 qid,struct nvme_queue * nvmeq)117757dacad5SJay Sternberg static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
117857dacad5SJay Sternberg struct nvme_queue *nvmeq)
117957dacad5SJay Sternberg {
11809abd68efSJens Axboe struct nvme_ctrl *ctrl = &dev->ctrl;
1181f66e2804SChaitanya Kulkarni struct nvme_command c = { };
118281c1cd98SKeith Busch int flags = NVME_QUEUE_PHYS_CONTIG;
118357dacad5SJay Sternberg
118457dacad5SJay Sternberg /*
11859abd68efSJens Axboe * Some drives have a bug that auto-enables WRRU if MEDIUM isn't
11869abd68efSJens Axboe * set. Since URGENT priority is zeroes, it makes all queues
11879abd68efSJens Axboe * URGENT.
11889abd68efSJens Axboe */
11899abd68efSJens Axboe if (ctrl->quirks & NVME_QUIRK_MEDIUM_PRIO_SQ)
11909abd68efSJens Axboe flags |= NVME_SQ_PRIO_MEDIUM;
11919abd68efSJens Axboe
11929abd68efSJens Axboe /*
119316772ae6SMinwoo Im * Note: we (ab)use the fact that the prp fields survive if no data
119457dacad5SJay Sternberg * is attached to the request.
119557dacad5SJay Sternberg */
119657dacad5SJay Sternberg c.create_sq.opcode = nvme_admin_create_sq;
119757dacad5SJay Sternberg c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
119857dacad5SJay Sternberg c.create_sq.sqid = cpu_to_le16(qid);
119957dacad5SJay Sternberg c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
120057dacad5SJay Sternberg c.create_sq.sq_flags = cpu_to_le16(flags);
120157dacad5SJay Sternberg c.create_sq.cqid = cpu_to_le16(qid);
120257dacad5SJay Sternberg
12031c63dc66SChristoph Hellwig return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
120457dacad5SJay Sternberg }
120557dacad5SJay Sternberg
adapter_delete_cq(struct nvme_dev * dev,u16 cqid)120657dacad5SJay Sternberg static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
120757dacad5SJay Sternberg {
120857dacad5SJay Sternberg return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
120957dacad5SJay Sternberg }
121057dacad5SJay Sternberg
adapter_delete_sq(struct nvme_dev * dev,u16 sqid)121157dacad5SJay Sternberg static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
121257dacad5SJay Sternberg {
121357dacad5SJay Sternberg return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
121457dacad5SJay Sternberg }
121557dacad5SJay Sternberg
abort_endio(struct request * req,blk_status_t error)1216de671d61SJens Axboe static enum rq_end_io_ret abort_endio(struct request *req, blk_status_t error)
121757dacad5SJay Sternberg {
1218a53232cbSKeith Busch struct nvme_queue *nvmeq = req->mq_hctx->driver_data;
121957dacad5SJay Sternberg
122027fa9bc5SChristoph Hellwig dev_warn(nvmeq->dev->ctrl.device,
122127fa9bc5SChristoph Hellwig "Abort status: 0x%x", nvme_req(req)->status);
1222e7a2a87dSChristoph Hellwig atomic_inc(&nvmeq->dev->ctrl.abort_limit);
1223e7a2a87dSChristoph Hellwig blk_mq_free_request(req);
1224de671d61SJens Axboe return RQ_END_IO_NONE;
122557dacad5SJay Sternberg }
122657dacad5SJay Sternberg
nvme_should_reset(struct nvme_dev * dev,u32 csts)1227b2a0eb1aSKeith Busch static bool nvme_should_reset(struct nvme_dev *dev, u32 csts)
1228b2a0eb1aSKeith Busch {
1229b2a0eb1aSKeith Busch /* If true, indicates loss of adapter communication, possibly by a
1230b2a0eb1aSKeith Busch * NVMe Subsystem reset.
1231b2a0eb1aSKeith Busch */
1232b2a0eb1aSKeith Busch bool nssro = dev->subsystem && (csts & NVME_CSTS_NSSRO);
1233b2a0eb1aSKeith Busch
1234ad70062cSJianchao Wang /* If there is a reset/reinit ongoing, we shouldn't reset again. */
12358884a56dSKeith Busch switch (nvme_ctrl_state(&dev->ctrl)) {
1236ad70062cSJianchao Wang case NVME_CTRL_RESETTING:
1237ad6a0a52SMax Gurtovoy case NVME_CTRL_CONNECTING:
1238b2a0eb1aSKeith Busch return false;
1239ad70062cSJianchao Wang default:
1240ad70062cSJianchao Wang break;
1241ad70062cSJianchao Wang }
1242b2a0eb1aSKeith Busch
1243b2a0eb1aSKeith Busch /* We shouldn't reset unless the controller is on fatal error state
1244b2a0eb1aSKeith Busch * _or_ if we lost the communication with it.
1245b2a0eb1aSKeith Busch */
1246b2a0eb1aSKeith Busch if (!(csts & NVME_CSTS_CFS) && !nssro)
1247b2a0eb1aSKeith Busch return false;
1248b2a0eb1aSKeith Busch
1249b2a0eb1aSKeith Busch return true;
1250b2a0eb1aSKeith Busch }
1251b2a0eb1aSKeith Busch
nvme_warn_reset(struct nvme_dev * dev,u32 csts)1252b2a0eb1aSKeith Busch static void nvme_warn_reset(struct nvme_dev *dev, u32 csts)
1253b2a0eb1aSKeith Busch {
1254b2a0eb1aSKeith Busch /* Read a config register to help see what died. */
1255b2a0eb1aSKeith Busch u16 pci_status;
1256b2a0eb1aSKeith Busch int result;
1257b2a0eb1aSKeith Busch
1258b2a0eb1aSKeith Busch result = pci_read_config_word(to_pci_dev(dev->dev), PCI_STATUS,
1259b2a0eb1aSKeith Busch &pci_status);
1260b2a0eb1aSKeith Busch if (result == PCIBIOS_SUCCESSFUL)
1261b2a0eb1aSKeith Busch dev_warn(dev->ctrl.device,
1262b2a0eb1aSKeith Busch "controller is down; will reset: CSTS=0x%x, PCI_STATUS=0x%hx\n",
1263b2a0eb1aSKeith Busch csts, pci_status);
1264b2a0eb1aSKeith Busch else
1265b2a0eb1aSKeith Busch dev_warn(dev->ctrl.device,
1266b2a0eb1aSKeith Busch "controller is down; will reset: CSTS=0x%x, PCI_STATUS read failed (%d)\n",
1267b2a0eb1aSKeith Busch csts, result);
12684641a8e6SKeith Busch
12694641a8e6SKeith Busch if (csts != ~0)
12704641a8e6SKeith Busch return;
12714641a8e6SKeith Busch
12724641a8e6SKeith Busch dev_warn(dev->ctrl.device,
12734641a8e6SKeith Busch "Does your device have a faulty power saving mode enabled?\n");
12744641a8e6SKeith Busch dev_warn(dev->ctrl.device,
12758772be96SBart Van Assche "Try \"nvme_core.default_ps_max_latency_us=0 pcie_aspm=off pcie_port_pm=off\" and report a bug\n");
1276b2a0eb1aSKeith Busch }
1277b2a0eb1aSKeith Busch
nvme_timeout(struct request * req)12789bdb4833SJohn Garry static enum blk_eh_timer_return nvme_timeout(struct request *req)
127957dacad5SJay Sternberg {
1280f4800d6dSChristoph Hellwig struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
1281a53232cbSKeith Busch struct nvme_queue *nvmeq = req->mq_hctx->driver_data;
128257dacad5SJay Sternberg struct nvme_dev *dev = nvmeq->dev;
128357dacad5SJay Sternberg struct request *abort_req;
1284f66e2804SChaitanya Kulkarni struct nvme_command cmd = { };
1285b2a0eb1aSKeith Busch u32 csts = readl(dev->bar + NVME_REG_CSTS);
1286b2a0eb1aSKeith Busch
1287b6eaa53fSNilay Shroff if (nvme_state_terminal(&dev->ctrl))
1288b6eaa53fSNilay Shroff goto disable;
1289b6eaa53fSNilay Shroff
1290651438bbSWen Xiong /* If PCI error recovery process is happening, we cannot reset or
1291651438bbSWen Xiong * the recovery mechanism will surely fail.
1292651438bbSWen Xiong */
1293651438bbSWen Xiong mb();
1294651438bbSWen Xiong if (pci_channel_offline(to_pci_dev(dev->dev)))
1295651438bbSWen Xiong return BLK_EH_RESET_TIMER;
1296651438bbSWen Xiong
1297b2a0eb1aSKeith Busch /*
1298b2a0eb1aSKeith Busch * Reset immediately if the controller is failed
1299b2a0eb1aSKeith Busch */
1300b2a0eb1aSKeith Busch if (nvme_should_reset(dev, csts)) {
1301b2a0eb1aSKeith Busch nvme_warn_reset(dev, csts);
130271a5bb15SKeith Busch goto disable;
1303b2a0eb1aSKeith Busch }
130457dacad5SJay Sternberg
130531c7c7d2SChristoph Hellwig /*
13067776db1cSKeith Busch * Did we miss an interrupt?
13077776db1cSKeith Busch */
1308fa059b85SKeith Busch if (test_bit(NVMEQ_POLLED, &nvmeq->flags))
13095a72e899SJens Axboe nvme_poll(req->mq_hctx, NULL);
1310fa059b85SKeith Busch else
1311bf392a5dSKeith Busch nvme_poll_irqdisable(nvmeq);
1312fa059b85SKeith Busch
13131c584208SKeith Busch if (blk_mq_rq_state(req) != MQ_RQ_IN_FLIGHT) {
13147776db1cSKeith Busch dev_warn(dev->ctrl.device,
13157776db1cSKeith Busch "I/O %d QID %d timeout, completion polled\n",
13167776db1cSKeith Busch req->tag, nvmeq->qid);
1317db8c48e4SChristoph Hellwig return BLK_EH_DONE;
13187776db1cSKeith Busch }
13197776db1cSKeith Busch
13207776db1cSKeith Busch /*
1321fd634f41SChristoph Hellwig * Shutdown immediately if controller times out while starting. The
1322fd634f41SChristoph Hellwig * reset work will see the pci device disabled when it gets the forced
1323fd634f41SChristoph Hellwig * cancellation error. All outstanding requests are completed on
1324db8c48e4SChristoph Hellwig * shutdown, so we return BLK_EH_DONE.
1325fd634f41SChristoph Hellwig */
13268884a56dSKeith Busch switch (nvme_ctrl_state(&dev->ctrl)) {
13274244140dSKeith Busch case NVME_CTRL_CONNECTING:
13282036f726SKeith Busch nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
1329df561f66SGustavo A. R. Silva fallthrough;
13302036f726SKeith Busch case NVME_CTRL_DELETING:
1331b9cac43cSKeith Busch dev_warn_ratelimited(dev->ctrl.device,
1332fd634f41SChristoph Hellwig "I/O %d QID %d timeout, disable controller\n",
1333fd634f41SChristoph Hellwig req->tag, nvmeq->qid);
133427fa9bc5SChristoph Hellwig nvme_req(req)->flags |= NVME_REQ_CANCELLED;
13357ad92f65STong Zhang nvme_dev_disable(dev, true);
1336db8c48e4SChristoph Hellwig return BLK_EH_DONE;
133739a9dd81SKeith Busch case NVME_CTRL_RESETTING:
133839a9dd81SKeith Busch return BLK_EH_RESET_TIMER;
13394244140dSKeith Busch default:
13404244140dSKeith Busch break;
1341fd634f41SChristoph Hellwig }
1342fd634f41SChristoph Hellwig
1343fd634f41SChristoph Hellwig /*
1344e1569a16SKeith Busch * Shutdown the controller immediately and schedule a reset if the
1345e1569a16SKeith Busch * command was already aborted once before and still hasn't been
1346e1569a16SKeith Busch * returned to the driver, or if this is the admin queue.
134731c7c7d2SChristoph Hellwig */
1348f4800d6dSChristoph Hellwig if (!nvmeq->qid || iod->aborted) {
13491b3c47c1SSagi Grimberg dev_warn(dev->ctrl.device,
135057dacad5SJay Sternberg "I/O %d QID %d timeout, reset controller\n",
135157dacad5SJay Sternberg req->tag, nvmeq->qid);
13527ad92f65STong Zhang nvme_req(req)->flags |= NVME_REQ_CANCELLED;
135371a5bb15SKeith Busch goto disable;
135457dacad5SJay Sternberg }
135557dacad5SJay Sternberg
1356e7a2a87dSChristoph Hellwig if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) {
1357e7a2a87dSChristoph Hellwig atomic_inc(&dev->ctrl.abort_limit);
1358e7a2a87dSChristoph Hellwig return BLK_EH_RESET_TIMER;
1359e7a2a87dSChristoph Hellwig }
136052da4f3fSKeith Busch iod->aborted = true;
136157dacad5SJay Sternberg
136257dacad5SJay Sternberg cmd.abort.opcode = nvme_admin_abort_cmd;
136385f74acfSKeith Busch cmd.abort.cid = nvme_cid(req);
136457dacad5SJay Sternberg cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
136557dacad5SJay Sternberg
13661b3c47c1SSagi Grimberg dev_warn(nvmeq->dev->ctrl.device,
136786141440SChristoph Hellwig "I/O %d (%s) QID %d timeout, aborting\n",
136886141440SChristoph Hellwig req->tag,
136986141440SChristoph Hellwig nvme_get_opcode_str(nvme_req(req)->cmd->common.opcode),
137086141440SChristoph Hellwig nvmeq->qid);
1371e7a2a87dSChristoph Hellwig
1372e559398fSChristoph Hellwig abort_req = blk_mq_alloc_request(dev->ctrl.admin_q, nvme_req_op(&cmd),
137339dfe844SChaitanya Kulkarni BLK_MQ_REQ_NOWAIT);
13746bf25d16SChristoph Hellwig if (IS_ERR(abort_req)) {
13756bf25d16SChristoph Hellwig atomic_inc(&dev->ctrl.abort_limit);
137631c7c7d2SChristoph Hellwig return BLK_EH_RESET_TIMER;
137757dacad5SJay Sternberg }
1378e559398fSChristoph Hellwig nvme_init_request(abort_req, &cmd);
137957dacad5SJay Sternberg
1380e2e53086SChristoph Hellwig abort_req->end_io = abort_endio;
1381e7a2a87dSChristoph Hellwig abort_req->end_io_data = NULL;
1382e2e53086SChristoph Hellwig blk_execute_rq_nowait(abort_req, false);
138357dacad5SJay Sternberg
138457dacad5SJay Sternberg /*
138557dacad5SJay Sternberg * The aborted req will be completed on receiving the abort req.
138657dacad5SJay Sternberg * We enable the timer again. If hit twice, it'll cause a device reset,
138757dacad5SJay Sternberg * as the device then is in a faulty state.
138857dacad5SJay Sternberg */
138957dacad5SJay Sternberg return BLK_EH_RESET_TIMER;
139071a5bb15SKeith Busch
139171a5bb15SKeith Busch disable:
1392b6eaa53fSNilay Shroff if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING)) {
1393b6eaa53fSNilay Shroff if (nvme_state_terminal(&dev->ctrl))
1394b6eaa53fSNilay Shroff nvme_dev_disable(dev, true);
139571a5bb15SKeith Busch return BLK_EH_DONE;
1396b6eaa53fSNilay Shroff }
139771a5bb15SKeith Busch
139871a5bb15SKeith Busch nvme_dev_disable(dev, false);
139971a5bb15SKeith Busch if (nvme_try_sched_reset(&dev->ctrl))
140071a5bb15SKeith Busch nvme_unquiesce_io_queues(&dev->ctrl);
140171a5bb15SKeith Busch return BLK_EH_DONE;
140257dacad5SJay Sternberg }
140357dacad5SJay Sternberg
nvme_free_queue(struct nvme_queue * nvmeq)140457dacad5SJay Sternberg static void nvme_free_queue(struct nvme_queue *nvmeq)
140557dacad5SJay Sternberg {
14068a1d09a6SBenjamin Herrenschmidt dma_free_coherent(nvmeq->dev->dev, CQ_SIZE(nvmeq),
140757dacad5SJay Sternberg (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
140863223078SChristoph Hellwig if (!nvmeq->sq_cmds)
140963223078SChristoph Hellwig return;
14100f238ff5SLogan Gunthorpe
141163223078SChristoph Hellwig if (test_and_clear_bit(NVMEQ_SQ_CMB, &nvmeq->flags)) {
141288a041f4SKeith Busch pci_free_p2pmem(to_pci_dev(nvmeq->dev->dev),
14138a1d09a6SBenjamin Herrenschmidt nvmeq->sq_cmds, SQ_SIZE(nvmeq));
141463223078SChristoph Hellwig } else {
14158a1d09a6SBenjamin Herrenschmidt dma_free_coherent(nvmeq->dev->dev, SQ_SIZE(nvmeq),
141663223078SChristoph Hellwig nvmeq->sq_cmds, nvmeq->sq_dma_addr);
14170f238ff5SLogan Gunthorpe }
141857dacad5SJay Sternberg }
141957dacad5SJay Sternberg
nvme_free_queues(struct nvme_dev * dev,int lowest)142057dacad5SJay Sternberg static void nvme_free_queues(struct nvme_dev *dev, int lowest)
142157dacad5SJay Sternberg {
142257dacad5SJay Sternberg int i;
142357dacad5SJay Sternberg
1424d858e5f0SSagi Grimberg for (i = dev->ctrl.queue_count - 1; i >= lowest; i--) {
1425d858e5f0SSagi Grimberg dev->ctrl.queue_count--;
1426147b27e4SSagi Grimberg nvme_free_queue(&dev->queues[i]);
142757dacad5SJay Sternberg }
142857dacad5SJay Sternberg }
142957dacad5SJay Sternberg
nvme_suspend_queue(struct nvme_dev * dev,unsigned int qid)143010981f23SChristoph Hellwig static void nvme_suspend_queue(struct nvme_dev *dev, unsigned int qid)
143157dacad5SJay Sternberg {
143210981f23SChristoph Hellwig struct nvme_queue *nvmeq = &dev->queues[qid];
143310981f23SChristoph Hellwig
14344e224106SChristoph Hellwig if (!test_and_clear_bit(NVMEQ_ENABLED, &nvmeq->flags))
143510981f23SChristoph Hellwig return;
143657dacad5SJay Sternberg
14374e224106SChristoph Hellwig /* ensure that nvme_queue_rq() sees NVMEQ_ENABLED cleared */
1438d1f06f4aSJens Axboe mb();
143957dacad5SJay Sternberg
14404e224106SChristoph Hellwig nvmeq->dev->online_queues--;
14411c63dc66SChristoph Hellwig if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
14429f27bd70SChristoph Hellwig nvme_quiesce_admin_queue(&nvmeq->dev->ctrl);
14437c349ddeSKeith Busch if (!test_and_clear_bit(NVMEQ_POLLED, &nvmeq->flags))
144410981f23SChristoph Hellwig pci_free_irq(to_pci_dev(dev->dev), nvmeq->cq_vector, nvmeq);
144557dacad5SJay Sternberg }
144657dacad5SJay Sternberg
nvme_suspend_io_queues(struct nvme_dev * dev)14478fae268bSKeith Busch static void nvme_suspend_io_queues(struct nvme_dev *dev)
14488fae268bSKeith Busch {
14498fae268bSKeith Busch int i;
14508fae268bSKeith Busch
14518fae268bSKeith Busch for (i = dev->ctrl.queue_count - 1; i > 0; i--)
145210981f23SChristoph Hellwig nvme_suspend_queue(dev, i);
145357dacad5SJay Sternberg }
145457dacad5SJay Sternberg
1455fa46c6fbSKeith Busch /*
1456fa46c6fbSKeith Busch * Called only on a device that has been disabled and after all other threads
14579210c075SDongli Zhang * that can check this device's completion queues have synced, except
14589210c075SDongli Zhang * nvme_poll(). This is the last chance for the driver to see a natural
14599210c075SDongli Zhang * completion before nvme_cancel_request() terminates all incomplete requests.
1460fa46c6fbSKeith Busch */
nvme_reap_pending_cqes(struct nvme_dev * dev)1461fa46c6fbSKeith Busch static void nvme_reap_pending_cqes(struct nvme_dev *dev)
1462fa46c6fbSKeith Busch {
1463fa46c6fbSKeith Busch int i;
1464fa46c6fbSKeith Busch
14659210c075SDongli Zhang for (i = dev->ctrl.queue_count - 1; i > 0; i--) {
14669210c075SDongli Zhang spin_lock(&dev->queues[i].cq_poll_lock);
1467c234a653SJens Axboe nvme_poll_cq(&dev->queues[i], NULL);
14689210c075SDongli Zhang spin_unlock(&dev->queues[i].cq_poll_lock);
14699210c075SDongli Zhang }
1470fa46c6fbSKeith Busch }
1471fa46c6fbSKeith Busch
nvme_cmb_qdepth(struct nvme_dev * dev,int nr_io_queues,int entry_size)147257dacad5SJay Sternberg static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
147357dacad5SJay Sternberg int entry_size)
147457dacad5SJay Sternberg {
147557dacad5SJay Sternberg int q_depth = dev->q_depth;
14765fd4ce1bSChristoph Hellwig unsigned q_size_aligned = roundup(q_depth * entry_size,
14776c3c05b0SChaitanya Kulkarni NVME_CTRL_PAGE_SIZE);
147857dacad5SJay Sternberg
147957dacad5SJay Sternberg if (q_size_aligned * nr_io_queues > dev->cmb_size) {
148057dacad5SJay Sternberg u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
14814e523547SBaolin Wang
14826c3c05b0SChaitanya Kulkarni mem_per_q = round_down(mem_per_q, NVME_CTRL_PAGE_SIZE);
148357dacad5SJay Sternberg q_depth = div_u64(mem_per_q, entry_size);
148457dacad5SJay Sternberg
148557dacad5SJay Sternberg /*
148657dacad5SJay Sternberg * Ensure the reduced q_depth is above some threshold where it
148757dacad5SJay Sternberg * would be better to map queues in system memory with the
148857dacad5SJay Sternberg * original depth
148957dacad5SJay Sternberg */
149057dacad5SJay Sternberg if (q_depth < 64)
149157dacad5SJay Sternberg return -ENOMEM;
149257dacad5SJay Sternberg }
149357dacad5SJay Sternberg
149457dacad5SJay Sternberg return q_depth;
149557dacad5SJay Sternberg }
149657dacad5SJay Sternberg
nvme_alloc_sq_cmds(struct nvme_dev * dev,struct nvme_queue * nvmeq,int qid)149757dacad5SJay Sternberg static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
14988a1d09a6SBenjamin Herrenschmidt int qid)
149957dacad5SJay Sternberg {
15000f238ff5SLogan Gunthorpe struct pci_dev *pdev = to_pci_dev(dev->dev);
1501815c6704SKeith Busch
15020f238ff5SLogan Gunthorpe if (qid && dev->cmb_use_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) {
15038a1d09a6SBenjamin Herrenschmidt nvmeq->sq_cmds = pci_alloc_p2pmem(pdev, SQ_SIZE(nvmeq));
1504bfac8e9fSAlan Mikhak if (nvmeq->sq_cmds) {
15050f238ff5SLogan Gunthorpe nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
15060f238ff5SLogan Gunthorpe nvmeq->sq_cmds);
150763223078SChristoph Hellwig if (nvmeq->sq_dma_addr) {
150863223078SChristoph Hellwig set_bit(NVMEQ_SQ_CMB, &nvmeq->flags);
150963223078SChristoph Hellwig return 0;
151063223078SChristoph Hellwig }
1511bfac8e9fSAlan Mikhak
15128a1d09a6SBenjamin Herrenschmidt pci_free_p2pmem(pdev, nvmeq->sq_cmds, SQ_SIZE(nvmeq));
1513bfac8e9fSAlan Mikhak }
15140f238ff5SLogan Gunthorpe }
15150f238ff5SLogan Gunthorpe
15168a1d09a6SBenjamin Herrenschmidt nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(nvmeq),
151757dacad5SJay Sternberg &nvmeq->sq_dma_addr, GFP_KERNEL);
151857dacad5SJay Sternberg if (!nvmeq->sq_cmds)
151957dacad5SJay Sternberg return -ENOMEM;
152057dacad5SJay Sternberg return 0;
152157dacad5SJay Sternberg }
152257dacad5SJay Sternberg
nvme_alloc_queue(struct nvme_dev * dev,int qid,int depth)1523a6ff7262SKeith Busch static int nvme_alloc_queue(struct nvme_dev *dev, int qid, int depth)
152457dacad5SJay Sternberg {
1525147b27e4SSagi Grimberg struct nvme_queue *nvmeq = &dev->queues[qid];
152657dacad5SJay Sternberg
152762314e40SKeith Busch if (dev->ctrl.queue_count > qid)
152862314e40SKeith Busch return 0;
152957dacad5SJay Sternberg
1530c1e0cc7eSBenjamin Herrenschmidt nvmeq->sqes = qid ? dev->io_sqes : NVME_ADM_SQES;
15318a1d09a6SBenjamin Herrenschmidt nvmeq->q_depth = depth;
15328a1d09a6SBenjamin Herrenschmidt nvmeq->cqes = dma_alloc_coherent(dev->dev, CQ_SIZE(nvmeq),
153357dacad5SJay Sternberg &nvmeq->cq_dma_addr, GFP_KERNEL);
153457dacad5SJay Sternberg if (!nvmeq->cqes)
153557dacad5SJay Sternberg goto free_nvmeq;
153657dacad5SJay Sternberg
15378a1d09a6SBenjamin Herrenschmidt if (nvme_alloc_sq_cmds(dev, nvmeq, qid))
153857dacad5SJay Sternberg goto free_cqdma;
153957dacad5SJay Sternberg
154057dacad5SJay Sternberg nvmeq->dev = dev;
15411ab0cd69SJens Axboe spin_lock_init(&nvmeq->sq_lock);
15423a7afd8eSChristoph Hellwig spin_lock_init(&nvmeq->cq_poll_lock);
154357dacad5SJay Sternberg nvmeq->cq_head = 0;
154457dacad5SJay Sternberg nvmeq->cq_phase = 1;
154557dacad5SJay Sternberg nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
154657dacad5SJay Sternberg nvmeq->qid = qid;
1547d858e5f0SSagi Grimberg dev->ctrl.queue_count++;
154857dacad5SJay Sternberg
1549147b27e4SSagi Grimberg return 0;
155057dacad5SJay Sternberg
155157dacad5SJay Sternberg free_cqdma:
15528a1d09a6SBenjamin Herrenschmidt dma_free_coherent(dev->dev, CQ_SIZE(nvmeq), (void *)nvmeq->cqes,
155357dacad5SJay Sternberg nvmeq->cq_dma_addr);
155457dacad5SJay Sternberg free_nvmeq:
1555147b27e4SSagi Grimberg return -ENOMEM;
155657dacad5SJay Sternberg }
155757dacad5SJay Sternberg
queue_request_irq(struct nvme_queue * nvmeq)1558dca51e78SChristoph Hellwig static int queue_request_irq(struct nvme_queue *nvmeq)
155957dacad5SJay Sternberg {
15600ff199cbSChristoph Hellwig struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev);
15610ff199cbSChristoph Hellwig int nr = nvmeq->dev->ctrl.instance;
15620ff199cbSChristoph Hellwig
15630ff199cbSChristoph Hellwig if (use_threaded_interrupts) {
15640ff199cbSChristoph Hellwig return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq_check,
15650ff199cbSChristoph Hellwig nvme_irq, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
15660ff199cbSChristoph Hellwig } else {
15670ff199cbSChristoph Hellwig return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq,
15680ff199cbSChristoph Hellwig NULL, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
15690ff199cbSChristoph Hellwig }
157057dacad5SJay Sternberg }
157157dacad5SJay Sternberg
nvme_init_queue(struct nvme_queue * nvmeq,u16 qid)157257dacad5SJay Sternberg static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
157357dacad5SJay Sternberg {
157457dacad5SJay Sternberg struct nvme_dev *dev = nvmeq->dev;
157557dacad5SJay Sternberg
157657dacad5SJay Sternberg nvmeq->sq_tail = 0;
157738210800SKeith Busch nvmeq->last_sq_tail = 0;
157857dacad5SJay Sternberg nvmeq->cq_head = 0;
157957dacad5SJay Sternberg nvmeq->cq_phase = 1;
158057dacad5SJay Sternberg nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
15818a1d09a6SBenjamin Herrenschmidt memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq));
1582f9f38e33SHelen Koike nvme_dbbuf_init(dev, nvmeq, qid);
158357dacad5SJay Sternberg dev->online_queues++;
15843a7afd8eSChristoph Hellwig wmb(); /* ensure the first interrupt sees the initialization */
158557dacad5SJay Sternberg }
158657dacad5SJay Sternberg
1587e4b9852aSCasey Chen /*
1588e4b9852aSCasey Chen * Try getting shutdown_lock while setting up IO queues.
1589e4b9852aSCasey Chen */
nvme_setup_io_queues_trylock(struct nvme_dev * dev)1590e4b9852aSCasey Chen static int nvme_setup_io_queues_trylock(struct nvme_dev *dev)
1591e4b9852aSCasey Chen {
1592e4b9852aSCasey Chen /*
1593e4b9852aSCasey Chen * Give up if the lock is being held by nvme_dev_disable.
1594e4b9852aSCasey Chen */
1595e4b9852aSCasey Chen if (!mutex_trylock(&dev->shutdown_lock))
1596e4b9852aSCasey Chen return -ENODEV;
1597e4b9852aSCasey Chen
1598e4b9852aSCasey Chen /*
1599e4b9852aSCasey Chen * Controller is in wrong state, fail early.
1600e4b9852aSCasey Chen */
16018884a56dSKeith Busch if (nvme_ctrl_state(&dev->ctrl) != NVME_CTRL_CONNECTING) {
1602e4b9852aSCasey Chen mutex_unlock(&dev->shutdown_lock);
1603e4b9852aSCasey Chen return -ENODEV;
1604e4b9852aSCasey Chen }
1605e4b9852aSCasey Chen
1606e4b9852aSCasey Chen return 0;
1607e4b9852aSCasey Chen }
1608e4b9852aSCasey Chen
nvme_create_queue(struct nvme_queue * nvmeq,int qid,bool polled)16094b04cc6aSJens Axboe static int nvme_create_queue(struct nvme_queue *nvmeq, int qid, bool polled)
161057dacad5SJay Sternberg {
161157dacad5SJay Sternberg struct nvme_dev *dev = nvmeq->dev;
161257dacad5SJay Sternberg int result;
16137c349ddeSKeith Busch u16 vector = 0;
161457dacad5SJay Sternberg
1615d1ed6aa1SChristoph Hellwig clear_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags);
1616d1ed6aa1SChristoph Hellwig
161722b55601SKeith Busch /*
161822b55601SKeith Busch * A queue's vector matches the queue identifier unless the controller
161922b55601SKeith Busch * has only one vector available.
162022b55601SKeith Busch */
16214b04cc6aSJens Axboe if (!polled)
1622a8e3e0bbSJianchao Wang vector = dev->num_vecs == 1 ? 0 : qid;
16234b04cc6aSJens Axboe else
16247c349ddeSKeith Busch set_bit(NVMEQ_POLLED, &nvmeq->flags);
16254b04cc6aSJens Axboe
1626a8e3e0bbSJianchao Wang result = adapter_alloc_cq(dev, qid, nvmeq, vector);
1627ded45505SKeith Busch if (result)
1628ded45505SKeith Busch return result;
162957dacad5SJay Sternberg
163057dacad5SJay Sternberg result = adapter_alloc_sq(dev, qid, nvmeq);
163157dacad5SJay Sternberg if (result < 0)
1632ded45505SKeith Busch return result;
1633c80b36cdSEdmund Nadolski if (result)
163457dacad5SJay Sternberg goto release_cq;
163557dacad5SJay Sternberg
1636a8e3e0bbSJianchao Wang nvmeq->cq_vector = vector;
16374b04cc6aSJens Axboe
1638e4b9852aSCasey Chen result = nvme_setup_io_queues_trylock(dev);
1639e4b9852aSCasey Chen if (result)
1640e4b9852aSCasey Chen return result;
1641e4b9852aSCasey Chen nvme_init_queue(nvmeq, qid);
16427c349ddeSKeith Busch if (!polled) {
1643dca51e78SChristoph Hellwig result = queue_request_irq(nvmeq);
164457dacad5SJay Sternberg if (result < 0)
164557dacad5SJay Sternberg goto release_sq;
16464b04cc6aSJens Axboe }
164757dacad5SJay Sternberg
16484e224106SChristoph Hellwig set_bit(NVMEQ_ENABLED, &nvmeq->flags);
1649e4b9852aSCasey Chen mutex_unlock(&dev->shutdown_lock);
165057dacad5SJay Sternberg return result;
165157dacad5SJay Sternberg
165257dacad5SJay Sternberg release_sq:
1653f25a2dfcSJianchao Wang dev->online_queues--;
1654e4b9852aSCasey Chen mutex_unlock(&dev->shutdown_lock);
165557dacad5SJay Sternberg adapter_delete_sq(dev, qid);
165657dacad5SJay Sternberg release_cq:
165757dacad5SJay Sternberg adapter_delete_cq(dev, qid);
165857dacad5SJay Sternberg return result;
165957dacad5SJay Sternberg }
166057dacad5SJay Sternberg
1661f363b089SEric Biggers static const struct blk_mq_ops nvme_mq_admin_ops = {
166257dacad5SJay Sternberg .queue_rq = nvme_queue_rq,
166377f02a7aSChristoph Hellwig .complete = nvme_pci_complete_rq,
166457dacad5SJay Sternberg .init_hctx = nvme_admin_init_hctx,
1665e559398fSChristoph Hellwig .init_request = nvme_pci_init_request,
166657dacad5SJay Sternberg .timeout = nvme_timeout,
166757dacad5SJay Sternberg };
166857dacad5SJay Sternberg
1669f363b089SEric Biggers static const struct blk_mq_ops nvme_mq_ops = {
1670376f7ef8SChristoph Hellwig .queue_rq = nvme_queue_rq,
1671d62cbcf6SJens Axboe .queue_rqs = nvme_queue_rqs,
1672376f7ef8SChristoph Hellwig .complete = nvme_pci_complete_rq,
1673376f7ef8SChristoph Hellwig .commit_rqs = nvme_commit_rqs,
1674376f7ef8SChristoph Hellwig .init_hctx = nvme_init_hctx,
1675e559398fSChristoph Hellwig .init_request = nvme_pci_init_request,
1676376f7ef8SChristoph Hellwig .map_queues = nvme_pci_map_queues,
1677376f7ef8SChristoph Hellwig .timeout = nvme_timeout,
1678c6d962aeSChristoph Hellwig .poll = nvme_poll,
1679dabcefabSJens Axboe };
1680dabcefabSJens Axboe
nvme_dev_remove_admin(struct nvme_dev * dev)168157dacad5SJay Sternberg static void nvme_dev_remove_admin(struct nvme_dev *dev)
168257dacad5SJay Sternberg {
16831c63dc66SChristoph Hellwig if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
168469d9a99cSKeith Busch /*
168569d9a99cSKeith Busch * If the controller was reset during removal, it's possible
168669d9a99cSKeith Busch * user requests may be waiting on a stopped queue. Start the
168769d9a99cSKeith Busch * queue to flush these to completion.
168869d9a99cSKeith Busch */
16899f27bd70SChristoph Hellwig nvme_unquiesce_admin_queue(&dev->ctrl);
16900da7feaaSChristoph Hellwig nvme_remove_admin_tag_set(&dev->ctrl);
169157dacad5SJay Sternberg }
169257dacad5SJay Sternberg }
169357dacad5SJay Sternberg
db_bar_size(struct nvme_dev * dev,unsigned nr_io_queues)169497f6ef64SXu Yu static unsigned long db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
169597f6ef64SXu Yu {
169697f6ef64SXu Yu return NVME_REG_DBS + ((nr_io_queues + 1) * 8 * dev->db_stride);
169797f6ef64SXu Yu }
169897f6ef64SXu Yu
nvme_remap_bar(struct nvme_dev * dev,unsigned long size)169997f6ef64SXu Yu static int nvme_remap_bar(struct nvme_dev *dev, unsigned long size)
170097f6ef64SXu Yu {
170197f6ef64SXu Yu struct pci_dev *pdev = to_pci_dev(dev->dev);
170297f6ef64SXu Yu
170397f6ef64SXu Yu if (size <= dev->bar_mapped_size)
170497f6ef64SXu Yu return 0;
170597f6ef64SXu Yu if (size > pci_resource_len(pdev, 0))
170697f6ef64SXu Yu return -ENOMEM;
170797f6ef64SXu Yu if (dev->bar)
170897f6ef64SXu Yu iounmap(dev->bar);
170997f6ef64SXu Yu dev->bar = ioremap(pci_resource_start(pdev, 0), size);
171097f6ef64SXu Yu if (!dev->bar) {
171197f6ef64SXu Yu dev->bar_mapped_size = 0;
171297f6ef64SXu Yu return -ENOMEM;
171397f6ef64SXu Yu }
171497f6ef64SXu Yu dev->bar_mapped_size = size;
171597f6ef64SXu Yu dev->dbs = dev->bar + NVME_REG_DBS;
171697f6ef64SXu Yu
171797f6ef64SXu Yu return 0;
171897f6ef64SXu Yu }
171997f6ef64SXu Yu
nvme_pci_configure_admin_queue(struct nvme_dev * dev)172001ad0990SSagi Grimberg static int nvme_pci_configure_admin_queue(struct nvme_dev *dev)
172157dacad5SJay Sternberg {
172257dacad5SJay Sternberg int result;
172357dacad5SJay Sternberg u32 aqa;
172457dacad5SJay Sternberg struct nvme_queue *nvmeq;
172557dacad5SJay Sternberg
172697f6ef64SXu Yu result = nvme_remap_bar(dev, db_bar_size(dev, 0));
172797f6ef64SXu Yu if (result < 0)
172897f6ef64SXu Yu return result;
172997f6ef64SXu Yu
17308ef2074dSGabriel Krisman Bertazi dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1, 0) ?
173120d0dfe6SSagi Grimberg NVME_CAP_NSSRC(dev->ctrl.cap) : 0;
173257dacad5SJay Sternberg
17337a67cbeaSChristoph Hellwig if (dev->subsystem &&
17347a67cbeaSChristoph Hellwig (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
17357a67cbeaSChristoph Hellwig writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
173657dacad5SJay Sternberg
1737285b6e9bSChristoph Hellwig /*
1738285b6e9bSChristoph Hellwig * If the device has been passed off to us in an enabled state, just
1739285b6e9bSChristoph Hellwig * clear the enabled bit. The spec says we should set the 'shutdown
1740285b6e9bSChristoph Hellwig * notification bits', but doing so may cause the device to complete
1741285b6e9bSChristoph Hellwig * commands to the admin queue ... and we don't know what memory that
1742285b6e9bSChristoph Hellwig * might be pointing at!
1743285b6e9bSChristoph Hellwig */
1744285b6e9bSChristoph Hellwig result = nvme_disable_ctrl(&dev->ctrl, false);
174557dacad5SJay Sternberg if (result < 0)
174657dacad5SJay Sternberg return result;
174757dacad5SJay Sternberg
1748a6ff7262SKeith Busch result = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
1749147b27e4SSagi Grimberg if (result)
1750147b27e4SSagi Grimberg return result;
175157dacad5SJay Sternberg
1752635333e4SMax Gurtovoy dev->ctrl.numa_node = dev_to_node(dev->dev);
1753635333e4SMax Gurtovoy
1754147b27e4SSagi Grimberg nvmeq = &dev->queues[0];
175557dacad5SJay Sternberg aqa = nvmeq->q_depth - 1;
175657dacad5SJay Sternberg aqa |= aqa << 16;
175757dacad5SJay Sternberg
17587a67cbeaSChristoph Hellwig writel(aqa, dev->bar + NVME_REG_AQA);
17597a67cbeaSChristoph Hellwig lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
17607a67cbeaSChristoph Hellwig lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
176157dacad5SJay Sternberg
1762c0f2f45bSSagi Grimberg result = nvme_enable_ctrl(&dev->ctrl);
176357dacad5SJay Sternberg if (result)
1764d4875622SKeith Busch return result;
176557dacad5SJay Sternberg
176657dacad5SJay Sternberg nvmeq->cq_vector = 0;
1767161b8be2SKeith Busch nvme_init_queue(nvmeq, 0);
1768dca51e78SChristoph Hellwig result = queue_request_irq(nvmeq);
176957dacad5SJay Sternberg if (result) {
17707c349ddeSKeith Busch dev->online_queues--;
1771d4875622SKeith Busch return result;
177257dacad5SJay Sternberg }
177357dacad5SJay Sternberg
17744e224106SChristoph Hellwig set_bit(NVMEQ_ENABLED, &nvmeq->flags);
177557dacad5SJay Sternberg return result;
177657dacad5SJay Sternberg }
177757dacad5SJay Sternberg
nvme_create_io_queues(struct nvme_dev * dev)1778749941f2SChristoph Hellwig static int nvme_create_io_queues(struct nvme_dev *dev)
177957dacad5SJay Sternberg {
17804b04cc6aSJens Axboe unsigned i, max, rw_queues;
1781749941f2SChristoph Hellwig int ret = 0;
178257dacad5SJay Sternberg
1783d858e5f0SSagi Grimberg for (i = dev->ctrl.queue_count; i <= dev->max_qid; i++) {
1784a6ff7262SKeith Busch if (nvme_alloc_queue(dev, i, dev->q_depth)) {
1785749941f2SChristoph Hellwig ret = -ENOMEM;
178657dacad5SJay Sternberg break;
1787749941f2SChristoph Hellwig }
1788749941f2SChristoph Hellwig }
178957dacad5SJay Sternberg
1790d858e5f0SSagi Grimberg max = min(dev->max_qid, dev->ctrl.queue_count - 1);
1791e20ba6e1SChristoph Hellwig if (max != 1 && dev->io_queues[HCTX_TYPE_POLL]) {
1792e20ba6e1SChristoph Hellwig rw_queues = dev->io_queues[HCTX_TYPE_DEFAULT] +
1793e20ba6e1SChristoph Hellwig dev->io_queues[HCTX_TYPE_READ];
17944b04cc6aSJens Axboe } else {
17954b04cc6aSJens Axboe rw_queues = max;
17964b04cc6aSJens Axboe }
17974b04cc6aSJens Axboe
1798949928c1SKeith Busch for (i = dev->online_queues; i <= max; i++) {
17994b04cc6aSJens Axboe bool polled = i > rw_queues;
18004b04cc6aSJens Axboe
18014b04cc6aSJens Axboe ret = nvme_create_queue(&dev->queues[i], i, polled);
1802d4875622SKeith Busch if (ret)
180357dacad5SJay Sternberg break;
180457dacad5SJay Sternberg }
180557dacad5SJay Sternberg
1806749941f2SChristoph Hellwig /*
1807749941f2SChristoph Hellwig * Ignore failing Create SQ/CQ commands, we can continue with less
18088adb8c14SMinwoo Im * than the desired amount of queues, and even a controller without
18098adb8c14SMinwoo Im * I/O queues can still be used to issue admin commands. This might
1810749941f2SChristoph Hellwig * be useful to upgrade a buggy firmware for example.
1811749941f2SChristoph Hellwig */
1812749941f2SChristoph Hellwig return ret >= 0 ? 0 : ret;
181357dacad5SJay Sternberg }
181457dacad5SJay Sternberg
nvme_cmb_size_unit(struct nvme_dev * dev)181588de4598SChristoph Hellwig static u64 nvme_cmb_size_unit(struct nvme_dev *dev)
181657dacad5SJay Sternberg {
181788de4598SChristoph Hellwig u8 szu = (dev->cmbsz >> NVME_CMBSZ_SZU_SHIFT) & NVME_CMBSZ_SZU_MASK;
181888de4598SChristoph Hellwig
181988de4598SChristoph Hellwig return 1ULL << (12 + 4 * szu);
182088de4598SChristoph Hellwig }
182188de4598SChristoph Hellwig
nvme_cmb_size(struct nvme_dev * dev)182288de4598SChristoph Hellwig static u32 nvme_cmb_size(struct nvme_dev *dev)
182388de4598SChristoph Hellwig {
182488de4598SChristoph Hellwig return (dev->cmbsz >> NVME_CMBSZ_SZ_SHIFT) & NVME_CMBSZ_SZ_MASK;
182588de4598SChristoph Hellwig }
182688de4598SChristoph Hellwig
nvme_map_cmb(struct nvme_dev * dev)1827f65efd6dSChristoph Hellwig static void nvme_map_cmb(struct nvme_dev *dev)
182857dacad5SJay Sternberg {
182988de4598SChristoph Hellwig u64 size, offset;
183057dacad5SJay Sternberg resource_size_t bar_size;
183157dacad5SJay Sternberg struct pci_dev *pdev = to_pci_dev(dev->dev);
18328969f1f8SChristoph Hellwig int bar;
183357dacad5SJay Sternberg
18349fe5c59fSKeith Busch if (dev->cmb_size)
18359fe5c59fSKeith Busch return;
18369fe5c59fSKeith Busch
183720d3bb92SKlaus Jensen if (NVME_CAP_CMBS(dev->ctrl.cap))
183820d3bb92SKlaus Jensen writel(NVME_CMBMSC_CRE, dev->bar + NVME_REG_CMBMSC);
183920d3bb92SKlaus Jensen
18407a67cbeaSChristoph Hellwig dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
1841f65efd6dSChristoph Hellwig if (!dev->cmbsz)
1842f65efd6dSChristoph Hellwig return;
1843202021c1SStephen Bates dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
184457dacad5SJay Sternberg
184588de4598SChristoph Hellwig size = nvme_cmb_size_unit(dev) * nvme_cmb_size(dev);
184688de4598SChristoph Hellwig offset = nvme_cmb_size_unit(dev) * NVME_CMB_OFST(dev->cmbloc);
18478969f1f8SChristoph Hellwig bar = NVME_CMB_BIR(dev->cmbloc);
18488969f1f8SChristoph Hellwig bar_size = pci_resource_len(pdev, bar);
184957dacad5SJay Sternberg
185057dacad5SJay Sternberg if (offset > bar_size)
1851f65efd6dSChristoph Hellwig return;
185257dacad5SJay Sternberg
185357dacad5SJay Sternberg /*
185420d3bb92SKlaus Jensen * Tell the controller about the host side address mapping the CMB,
185520d3bb92SKlaus Jensen * and enable CMB decoding for the NVMe 1.4+ scheme:
185620d3bb92SKlaus Jensen */
185720d3bb92SKlaus Jensen if (NVME_CAP_CMBS(dev->ctrl.cap)) {
185820d3bb92SKlaus Jensen hi_lo_writeq(NVME_CMBMSC_CRE | NVME_CMBMSC_CMSE |
185920d3bb92SKlaus Jensen (pci_bus_address(pdev, bar) + offset),
186020d3bb92SKlaus Jensen dev->bar + NVME_REG_CMBMSC);
186120d3bb92SKlaus Jensen }
186220d3bb92SKlaus Jensen
186320d3bb92SKlaus Jensen /*
186457dacad5SJay Sternberg * Controllers may support a CMB size larger than their BAR,
186557dacad5SJay Sternberg * for example, due to being behind a bridge. Reduce the CMB to
186657dacad5SJay Sternberg * the reported size of the BAR
186757dacad5SJay Sternberg */
186857dacad5SJay Sternberg if (size > bar_size - offset)
186957dacad5SJay Sternberg size = bar_size - offset;
187057dacad5SJay Sternberg
18710f238ff5SLogan Gunthorpe if (pci_p2pdma_add_resource(pdev, bar, size, offset)) {
18720f238ff5SLogan Gunthorpe dev_warn(dev->ctrl.device,
18730f238ff5SLogan Gunthorpe "failed to register the CMB\n");
1874f65efd6dSChristoph Hellwig return;
18750f238ff5SLogan Gunthorpe }
18760f238ff5SLogan Gunthorpe
187757dacad5SJay Sternberg dev->cmb_size = size;
18780f238ff5SLogan Gunthorpe dev->cmb_use_sqes = use_cmb_sqes && (dev->cmbsz & NVME_CMBSZ_SQS);
18790f238ff5SLogan Gunthorpe
18800f238ff5SLogan Gunthorpe if ((dev->cmbsz & (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS)) ==
18810f238ff5SLogan Gunthorpe (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS))
18820f238ff5SLogan Gunthorpe pci_p2pmem_publish(pdev, true);
1883e917a849SKeith Busch
1884e917a849SKeith Busch nvme_update_attrs(dev);
188557dacad5SJay Sternberg }
188657dacad5SJay Sternberg
nvme_set_host_mem(struct nvme_dev * dev,u32 bits)188787ad72a5SChristoph Hellwig static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits)
188857dacad5SJay Sternberg {
18896c3c05b0SChaitanya Kulkarni u32 host_mem_size = dev->host_mem_size >> NVME_CTRL_PAGE_SHIFT;
18904033f35dSChristoph Hellwig u64 dma_addr = dev->host_mem_descs_dma;
1891f66e2804SChaitanya Kulkarni struct nvme_command c = { };
189287ad72a5SChristoph Hellwig int ret;
189387ad72a5SChristoph Hellwig
189487ad72a5SChristoph Hellwig c.features.opcode = nvme_admin_set_features;
189587ad72a5SChristoph Hellwig c.features.fid = cpu_to_le32(NVME_FEAT_HOST_MEM_BUF);
189687ad72a5SChristoph Hellwig c.features.dword11 = cpu_to_le32(bits);
18976c3c05b0SChaitanya Kulkarni c.features.dword12 = cpu_to_le32(host_mem_size);
189887ad72a5SChristoph Hellwig c.features.dword13 = cpu_to_le32(lower_32_bits(dma_addr));
189987ad72a5SChristoph Hellwig c.features.dword14 = cpu_to_le32(upper_32_bits(dma_addr));
190087ad72a5SChristoph Hellwig c.features.dword15 = cpu_to_le32(dev->nr_host_mem_descs);
190187ad72a5SChristoph Hellwig
190287ad72a5SChristoph Hellwig ret = nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
190387ad72a5SChristoph Hellwig if (ret) {
190487ad72a5SChristoph Hellwig dev_warn(dev->ctrl.device,
190587ad72a5SChristoph Hellwig "failed to set host mem (err %d, flags %#x).\n",
190687ad72a5SChristoph Hellwig ret, bits);
1907a5df5e79SKeith Busch } else
1908a5df5e79SKeith Busch dev->hmb = bits & NVME_HOST_MEM_ENABLE;
1909a5df5e79SKeith Busch
191087ad72a5SChristoph Hellwig return ret;
191187ad72a5SChristoph Hellwig }
191287ad72a5SChristoph Hellwig
nvme_free_host_mem(struct nvme_dev * dev)191387ad72a5SChristoph Hellwig static void nvme_free_host_mem(struct nvme_dev *dev)
191487ad72a5SChristoph Hellwig {
191587ad72a5SChristoph Hellwig int i;
191687ad72a5SChristoph Hellwig
191787ad72a5SChristoph Hellwig for (i = 0; i < dev->nr_host_mem_descs; i++) {
191887ad72a5SChristoph Hellwig struct nvme_host_mem_buf_desc *desc = &dev->host_mem_descs[i];
19196c3c05b0SChaitanya Kulkarni size_t size = le32_to_cpu(desc->size) * NVME_CTRL_PAGE_SIZE;
192087ad72a5SChristoph Hellwig
1921cc667f6dSLiviu Dudau dma_free_attrs(dev->dev, size, dev->host_mem_desc_bufs[i],
1922cc667f6dSLiviu Dudau le64_to_cpu(desc->addr),
1923cc667f6dSLiviu Dudau DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
192487ad72a5SChristoph Hellwig }
192587ad72a5SChristoph Hellwig
192687ad72a5SChristoph Hellwig kfree(dev->host_mem_desc_bufs);
192787ad72a5SChristoph Hellwig dev->host_mem_desc_bufs = NULL;
1928cee3bff5SChristoph Hellwig dma_free_coherent(dev->dev, dev->host_mem_descs_size,
19294033f35dSChristoph Hellwig dev->host_mem_descs, dev->host_mem_descs_dma);
193087ad72a5SChristoph Hellwig dev->host_mem_descs = NULL;
1931cee3bff5SChristoph Hellwig dev->host_mem_descs_size = 0;
19327e5dd57eSMinwoo Im dev->nr_host_mem_descs = 0;
193387ad72a5SChristoph Hellwig }
193487ad72a5SChristoph Hellwig
__nvme_alloc_host_mem(struct nvme_dev * dev,u64 preferred,u32 chunk_size)193592dc6895SChristoph Hellwig static int __nvme_alloc_host_mem(struct nvme_dev *dev, u64 preferred,
193692dc6895SChristoph Hellwig u32 chunk_size)
193787ad72a5SChristoph Hellwig {
193887ad72a5SChristoph Hellwig struct nvme_host_mem_buf_desc *descs;
1939cee3bff5SChristoph Hellwig u32 max_entries, len, descs_size;
19404033f35dSChristoph Hellwig dma_addr_t descs_dma;
19412ee0e4edSDan Carpenter int i = 0;
194287ad72a5SChristoph Hellwig void **bufs;
19436fbcde66SMinwoo Im u64 size, tmp;
194487ad72a5SChristoph Hellwig
194587ad72a5SChristoph Hellwig tmp = (preferred + chunk_size - 1);
194687ad72a5SChristoph Hellwig do_div(tmp, chunk_size);
194787ad72a5SChristoph Hellwig max_entries = tmp;
1948044a9df1SChristoph Hellwig
1949044a9df1SChristoph Hellwig if (dev->ctrl.hmmaxd && dev->ctrl.hmmaxd < max_entries)
1950044a9df1SChristoph Hellwig max_entries = dev->ctrl.hmmaxd;
1951044a9df1SChristoph Hellwig
1952cee3bff5SChristoph Hellwig descs_size = max_entries * sizeof(*descs);
1953cee3bff5SChristoph Hellwig descs = dma_alloc_coherent(dev->dev, descs_size, &descs_dma,
1954cee3bff5SChristoph Hellwig GFP_KERNEL);
195587ad72a5SChristoph Hellwig if (!descs)
195687ad72a5SChristoph Hellwig goto out;
195787ad72a5SChristoph Hellwig
195887ad72a5SChristoph Hellwig bufs = kcalloc(max_entries, sizeof(*bufs), GFP_KERNEL);
195987ad72a5SChristoph Hellwig if (!bufs)
196087ad72a5SChristoph Hellwig goto out_free_descs;
196187ad72a5SChristoph Hellwig
1962244a8fe4SMinwoo Im for (size = 0; size < preferred && i < max_entries; size += len) {
196387ad72a5SChristoph Hellwig dma_addr_t dma_addr;
196487ad72a5SChristoph Hellwig
196550cdb7c6SChristoph Hellwig len = min_t(u64, chunk_size, preferred - size);
196687ad72a5SChristoph Hellwig bufs[i] = dma_alloc_attrs(dev->dev, len, &dma_addr, GFP_KERNEL,
196787ad72a5SChristoph Hellwig DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
196887ad72a5SChristoph Hellwig if (!bufs[i])
196987ad72a5SChristoph Hellwig break;
197087ad72a5SChristoph Hellwig
197187ad72a5SChristoph Hellwig descs[i].addr = cpu_to_le64(dma_addr);
19726c3c05b0SChaitanya Kulkarni descs[i].size = cpu_to_le32(len / NVME_CTRL_PAGE_SIZE);
197387ad72a5SChristoph Hellwig i++;
197487ad72a5SChristoph Hellwig }
197587ad72a5SChristoph Hellwig
197692dc6895SChristoph Hellwig if (!size)
197787ad72a5SChristoph Hellwig goto out_free_bufs;
197887ad72a5SChristoph Hellwig
197987ad72a5SChristoph Hellwig dev->nr_host_mem_descs = i;
198087ad72a5SChristoph Hellwig dev->host_mem_size = size;
198187ad72a5SChristoph Hellwig dev->host_mem_descs = descs;
19824033f35dSChristoph Hellwig dev->host_mem_descs_dma = descs_dma;
1983cee3bff5SChristoph Hellwig dev->host_mem_descs_size = descs_size;
198487ad72a5SChristoph Hellwig dev->host_mem_desc_bufs = bufs;
198587ad72a5SChristoph Hellwig return 0;
198687ad72a5SChristoph Hellwig
198787ad72a5SChristoph Hellwig out_free_bufs:
198887ad72a5SChristoph Hellwig while (--i >= 0) {
19896c3c05b0SChaitanya Kulkarni size_t size = le32_to_cpu(descs[i].size) * NVME_CTRL_PAGE_SIZE;
199087ad72a5SChristoph Hellwig
1991cc667f6dSLiviu Dudau dma_free_attrs(dev->dev, size, bufs[i],
1992cc667f6dSLiviu Dudau le64_to_cpu(descs[i].addr),
1993cc667f6dSLiviu Dudau DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
199487ad72a5SChristoph Hellwig }
199587ad72a5SChristoph Hellwig
199687ad72a5SChristoph Hellwig kfree(bufs);
199787ad72a5SChristoph Hellwig out_free_descs:
1998cee3bff5SChristoph Hellwig dma_free_coherent(dev->dev, descs_size, descs, descs_dma);
199987ad72a5SChristoph Hellwig out:
200087ad72a5SChristoph Hellwig dev->host_mem_descs = NULL;
200187ad72a5SChristoph Hellwig return -ENOMEM;
200287ad72a5SChristoph Hellwig }
200387ad72a5SChristoph Hellwig
nvme_alloc_host_mem(struct nvme_dev * dev,u64 min,u64 preferred)200492dc6895SChristoph Hellwig static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
200592dc6895SChristoph Hellwig {
20069dc54a0dSChaitanya Kulkarni u64 min_chunk = min_t(u64, preferred, PAGE_SIZE * MAX_ORDER_NR_PAGES);
20079dc54a0dSChaitanya Kulkarni u64 hmminds = max_t(u32, dev->ctrl.hmminds * 4096, PAGE_SIZE * 2);
20089dc54a0dSChaitanya Kulkarni u64 chunk_size;
200992dc6895SChristoph Hellwig
201092dc6895SChristoph Hellwig /* start big and work our way down */
20119dc54a0dSChaitanya Kulkarni for (chunk_size = min_chunk; chunk_size >= hmminds; chunk_size /= 2) {
201292dc6895SChristoph Hellwig if (!__nvme_alloc_host_mem(dev, preferred, chunk_size)) {
201392dc6895SChristoph Hellwig if (!min || dev->host_mem_size >= min)
201492dc6895SChristoph Hellwig return 0;
201592dc6895SChristoph Hellwig nvme_free_host_mem(dev);
201692dc6895SChristoph Hellwig }
201792dc6895SChristoph Hellwig }
201892dc6895SChristoph Hellwig
201992dc6895SChristoph Hellwig return -ENOMEM;
202092dc6895SChristoph Hellwig }
202192dc6895SChristoph Hellwig
nvme_setup_host_mem(struct nvme_dev * dev)20229620cfbaSChristoph Hellwig static int nvme_setup_host_mem(struct nvme_dev *dev)
202387ad72a5SChristoph Hellwig {
202487ad72a5SChristoph Hellwig u64 max = (u64)max_host_mem_size_mb * SZ_1M;
202587ad72a5SChristoph Hellwig u64 preferred = (u64)dev->ctrl.hmpre * 4096;
202687ad72a5SChristoph Hellwig u64 min = (u64)dev->ctrl.hmmin * 4096;
202787ad72a5SChristoph Hellwig u32 enable_bits = NVME_HOST_MEM_ENABLE;
20286fbcde66SMinwoo Im int ret;
202987ad72a5SChristoph Hellwig
2030acb71e53SChristoph Hellwig if (!dev->ctrl.hmpre)
2031acb71e53SChristoph Hellwig return 0;
2032acb71e53SChristoph Hellwig
203387ad72a5SChristoph Hellwig preferred = min(preferred, max);
203487ad72a5SChristoph Hellwig if (min > max) {
203587ad72a5SChristoph Hellwig dev_warn(dev->ctrl.device,
203687ad72a5SChristoph Hellwig "min host memory (%lld MiB) above limit (%d MiB).\n",
203787ad72a5SChristoph Hellwig min >> ilog2(SZ_1M), max_host_mem_size_mb);
203887ad72a5SChristoph Hellwig nvme_free_host_mem(dev);
20399620cfbaSChristoph Hellwig return 0;
204087ad72a5SChristoph Hellwig }
204187ad72a5SChristoph Hellwig
204287ad72a5SChristoph Hellwig /*
204387ad72a5SChristoph Hellwig * If we already have a buffer allocated check if we can reuse it.
204487ad72a5SChristoph Hellwig */
204587ad72a5SChristoph Hellwig if (dev->host_mem_descs) {
204687ad72a5SChristoph Hellwig if (dev->host_mem_size >= min)
204787ad72a5SChristoph Hellwig enable_bits |= NVME_HOST_MEM_RETURN;
204887ad72a5SChristoph Hellwig else
204987ad72a5SChristoph Hellwig nvme_free_host_mem(dev);
205087ad72a5SChristoph Hellwig }
205187ad72a5SChristoph Hellwig
205287ad72a5SChristoph Hellwig if (!dev->host_mem_descs) {
205392dc6895SChristoph Hellwig if (nvme_alloc_host_mem(dev, min, preferred)) {
205492dc6895SChristoph Hellwig dev_warn(dev->ctrl.device,
205592dc6895SChristoph Hellwig "failed to allocate host memory buffer.\n");
20569620cfbaSChristoph Hellwig return 0; /* controller must work without HMB */
205787ad72a5SChristoph Hellwig }
205887ad72a5SChristoph Hellwig
205992dc6895SChristoph Hellwig dev_info(dev->ctrl.device,
206092dc6895SChristoph Hellwig "allocated %lld MiB host memory buffer.\n",
206192dc6895SChristoph Hellwig dev->host_mem_size >> ilog2(SZ_1M));
206292dc6895SChristoph Hellwig }
206392dc6895SChristoph Hellwig
20649620cfbaSChristoph Hellwig ret = nvme_set_host_mem(dev, enable_bits);
20659620cfbaSChristoph Hellwig if (ret)
206687ad72a5SChristoph Hellwig nvme_free_host_mem(dev);
20679620cfbaSChristoph Hellwig return ret;
206857dacad5SJay Sternberg }
206957dacad5SJay Sternberg
cmb_show(struct device * dev,struct device_attribute * attr,char * buf)20700521905eSKeith Busch static ssize_t cmb_show(struct device *dev, struct device_attribute *attr,
20710521905eSKeith Busch char *buf)
20720521905eSKeith Busch {
20730521905eSKeith Busch struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
20740521905eSKeith Busch
20750521905eSKeith Busch return sysfs_emit(buf, "cmbloc : x%08x\ncmbsz : x%08x\n",
20760521905eSKeith Busch ndev->cmbloc, ndev->cmbsz);
20770521905eSKeith Busch }
20780521905eSKeith Busch static DEVICE_ATTR_RO(cmb);
20790521905eSKeith Busch
cmbloc_show(struct device * dev,struct device_attribute * attr,char * buf)20801751e97aSKeith Busch static ssize_t cmbloc_show(struct device *dev, struct device_attribute *attr,
20811751e97aSKeith Busch char *buf)
20821751e97aSKeith Busch {
20831751e97aSKeith Busch struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
20841751e97aSKeith Busch
20851751e97aSKeith Busch return sysfs_emit(buf, "%u\n", ndev->cmbloc);
20861751e97aSKeith Busch }
20871751e97aSKeith Busch static DEVICE_ATTR_RO(cmbloc);
20881751e97aSKeith Busch
cmbsz_show(struct device * dev,struct device_attribute * attr,char * buf)20891751e97aSKeith Busch static ssize_t cmbsz_show(struct device *dev, struct device_attribute *attr,
20901751e97aSKeith Busch char *buf)
20911751e97aSKeith Busch {
20921751e97aSKeith Busch struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
20931751e97aSKeith Busch
20941751e97aSKeith Busch return sysfs_emit(buf, "%u\n", ndev->cmbsz);
20951751e97aSKeith Busch }
20961751e97aSKeith Busch static DEVICE_ATTR_RO(cmbsz);
20971751e97aSKeith Busch
hmb_show(struct device * dev,struct device_attribute * attr,char * buf)2098a5df5e79SKeith Busch static ssize_t hmb_show(struct device *dev, struct device_attribute *attr,
2099a5df5e79SKeith Busch char *buf)
2100a5df5e79SKeith Busch {
2101a5df5e79SKeith Busch struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
2102a5df5e79SKeith Busch
2103a5df5e79SKeith Busch return sysfs_emit(buf, "%d\n", ndev->hmb);
2104a5df5e79SKeith Busch }
2105a5df5e79SKeith Busch
hmb_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2106a5df5e79SKeith Busch static ssize_t hmb_store(struct device *dev, struct device_attribute *attr,
2107a5df5e79SKeith Busch const char *buf, size_t count)
2108a5df5e79SKeith Busch {
2109a5df5e79SKeith Busch struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
2110a5df5e79SKeith Busch bool new;
2111a5df5e79SKeith Busch int ret;
2112a5df5e79SKeith Busch
211399722c8aSChristophe JAILLET if (kstrtobool(buf, &new) < 0)
2114a5df5e79SKeith Busch return -EINVAL;
2115a5df5e79SKeith Busch
2116a5df5e79SKeith Busch if (new == ndev->hmb)
2117a5df5e79SKeith Busch return count;
2118a5df5e79SKeith Busch
2119a5df5e79SKeith Busch if (new) {
2120a5df5e79SKeith Busch ret = nvme_setup_host_mem(ndev);
2121a5df5e79SKeith Busch } else {
2122a5df5e79SKeith Busch ret = nvme_set_host_mem(ndev, 0);
2123a5df5e79SKeith Busch if (!ret)
2124a5df5e79SKeith Busch nvme_free_host_mem(ndev);
2125a5df5e79SKeith Busch }
2126a5df5e79SKeith Busch
2127a5df5e79SKeith Busch if (ret < 0)
2128a5df5e79SKeith Busch return ret;
2129a5df5e79SKeith Busch
2130a5df5e79SKeith Busch return count;
2131a5df5e79SKeith Busch }
2132a5df5e79SKeith Busch static DEVICE_ATTR_RW(hmb);
2133a5df5e79SKeith Busch
nvme_pci_attrs_are_visible(struct kobject * kobj,struct attribute * a,int n)21340521905eSKeith Busch static umode_t nvme_pci_attrs_are_visible(struct kobject *kobj,
21350521905eSKeith Busch struct attribute *a, int n)
21360521905eSKeith Busch {
21370521905eSKeith Busch struct nvme_ctrl *ctrl =
21380521905eSKeith Busch dev_get_drvdata(container_of(kobj, struct device, kobj));
21390521905eSKeith Busch struct nvme_dev *dev = to_nvme_dev(ctrl);
21400521905eSKeith Busch
21411751e97aSKeith Busch if (a == &dev_attr_cmb.attr ||
21421751e97aSKeith Busch a == &dev_attr_cmbloc.attr ||
21431751e97aSKeith Busch a == &dev_attr_cmbsz.attr) {
21441751e97aSKeith Busch if (!dev->cmbsz)
21450521905eSKeith Busch return 0;
21461751e97aSKeith Busch }
2147a5df5e79SKeith Busch if (a == &dev_attr_hmb.attr && !ctrl->hmpre)
2148a5df5e79SKeith Busch return 0;
2149a5df5e79SKeith Busch
21500521905eSKeith Busch return a->mode;
21510521905eSKeith Busch }
21520521905eSKeith Busch
21530521905eSKeith Busch static struct attribute *nvme_pci_attrs[] = {
21540521905eSKeith Busch &dev_attr_cmb.attr,
21551751e97aSKeith Busch &dev_attr_cmbloc.attr,
21561751e97aSKeith Busch &dev_attr_cmbsz.attr,
2157a5df5e79SKeith Busch &dev_attr_hmb.attr,
21580521905eSKeith Busch NULL,
21590521905eSKeith Busch };
21600521905eSKeith Busch
216186adbf0cSChristoph Hellwig static const struct attribute_group nvme_pci_dev_attrs_group = {
21620521905eSKeith Busch .attrs = nvme_pci_attrs,
21630521905eSKeith Busch .is_visible = nvme_pci_attrs_are_visible,
21640521905eSKeith Busch };
21650521905eSKeith Busch
216686adbf0cSChristoph Hellwig static const struct attribute_group *nvme_pci_dev_attr_groups[] = {
216786adbf0cSChristoph Hellwig &nvme_dev_attrs_group,
216886adbf0cSChristoph Hellwig &nvme_pci_dev_attrs_group,
216986adbf0cSChristoph Hellwig NULL,
217086adbf0cSChristoph Hellwig };
217186adbf0cSChristoph Hellwig
nvme_update_attrs(struct nvme_dev * dev)2172e917a849SKeith Busch static void nvme_update_attrs(struct nvme_dev *dev)
2173e917a849SKeith Busch {
2174e917a849SKeith Busch sysfs_update_group(&dev->ctrl.device->kobj, &nvme_pci_dev_attrs_group);
2175e917a849SKeith Busch }
2176e917a849SKeith Busch
2177612b7286SMing Lei /*
2178612b7286SMing Lei * nirqs is the number of interrupts available for write and read
2179612b7286SMing Lei * queues. The core already reserved an interrupt for the admin queue.
2180612b7286SMing Lei */
nvme_calc_irq_sets(struct irq_affinity * affd,unsigned int nrirqs)2181612b7286SMing Lei static void nvme_calc_irq_sets(struct irq_affinity *affd, unsigned int nrirqs)
21823b6592f7SJens Axboe {
2183612b7286SMing Lei struct nvme_dev *dev = affd->priv;
21842a5bcfddSWeiping Zhang unsigned int nr_read_queues, nr_write_queues = dev->nr_write_queues;
2185c45b1fa2SMing Lei
21863b6592f7SJens Axboe /*
2187ee0d96d3SBaolin Wang * If there is no interrupt available for queues, ensure that
2188612b7286SMing Lei * the default queue is set to 1. The affinity set size is
2189612b7286SMing Lei * also set to one, but the irq core ignores it for this case.
2190612b7286SMing Lei *
2191612b7286SMing Lei * If only one interrupt is available or 'write_queue' == 0, combine
2192612b7286SMing Lei * write and read queues.
2193612b7286SMing Lei *
2194612b7286SMing Lei * If 'write_queues' > 0, ensure it leaves room for at least one read
2195612b7286SMing Lei * queue.
21963b6592f7SJens Axboe */
2197612b7286SMing Lei if (!nrirqs) {
2198612b7286SMing Lei nrirqs = 1;
2199612b7286SMing Lei nr_read_queues = 0;
22002a5bcfddSWeiping Zhang } else if (nrirqs == 1 || !nr_write_queues) {
2201612b7286SMing Lei nr_read_queues = 0;
22022a5bcfddSWeiping Zhang } else if (nr_write_queues >= nrirqs) {
2203612b7286SMing Lei nr_read_queues = 1;
22043b6592f7SJens Axboe } else {
22052a5bcfddSWeiping Zhang nr_read_queues = nrirqs - nr_write_queues;
22063b6592f7SJens Axboe }
2207612b7286SMing Lei
2208612b7286SMing Lei dev->io_queues[HCTX_TYPE_DEFAULT] = nrirqs - nr_read_queues;
2209612b7286SMing Lei affd->set_size[HCTX_TYPE_DEFAULT] = nrirqs - nr_read_queues;
2210612b7286SMing Lei dev->io_queues[HCTX_TYPE_READ] = nr_read_queues;
2211612b7286SMing Lei affd->set_size[HCTX_TYPE_READ] = nr_read_queues;
2212612b7286SMing Lei affd->nr_sets = nr_read_queues ? 2 : 1;
22133b6592f7SJens Axboe }
22143b6592f7SJens Axboe
nvme_setup_irqs(struct nvme_dev * dev,unsigned int nr_io_queues)22156451fe73SJens Axboe static int nvme_setup_irqs(struct nvme_dev *dev, unsigned int nr_io_queues)
22163b6592f7SJens Axboe {
22173b6592f7SJens Axboe struct pci_dev *pdev = to_pci_dev(dev->dev);
22183b6592f7SJens Axboe struct irq_affinity affd = {
22193b6592f7SJens Axboe .pre_vectors = 1,
2220612b7286SMing Lei .calc_sets = nvme_calc_irq_sets,
2221612b7286SMing Lei .priv = dev,
22223b6592f7SJens Axboe };
222321cc2f3fSJeffle Xu unsigned int irq_queues, poll_queues;
2224a2da0e5cSSean Anderson unsigned int flags = PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY;
22256451fe73SJens Axboe
22266451fe73SJens Axboe /*
222721cc2f3fSJeffle Xu * Poll queues don't need interrupts, but we need at least one I/O queue
222821cc2f3fSJeffle Xu * left over for non-polled I/O.
22296451fe73SJens Axboe */
223021cc2f3fSJeffle Xu poll_queues = min(dev->nr_poll_queues, nr_io_queues - 1);
223121cc2f3fSJeffle Xu dev->io_queues[HCTX_TYPE_POLL] = poll_queues;
22323b6592f7SJens Axboe
223321cc2f3fSJeffle Xu /*
223421cc2f3fSJeffle Xu * Initialize for the single interrupt case, will be updated in
223521cc2f3fSJeffle Xu * nvme_calc_irq_sets().
223621cc2f3fSJeffle Xu */
2237612b7286SMing Lei dev->io_queues[HCTX_TYPE_DEFAULT] = 1;
2238612b7286SMing Lei dev->io_queues[HCTX_TYPE_READ] = 0;
22393b6592f7SJens Axboe
224066341331SBenjamin Herrenschmidt /*
224121cc2f3fSJeffle Xu * We need interrupts for the admin queue and each non-polled I/O queue,
224221cc2f3fSJeffle Xu * but some Apple controllers require all queues to use the first
224321cc2f3fSJeffle Xu * vector.
224466341331SBenjamin Herrenschmidt */
224566341331SBenjamin Herrenschmidt irq_queues = 1;
224621cc2f3fSJeffle Xu if (!(dev->ctrl.quirks & NVME_QUIRK_SINGLE_VECTOR))
224721cc2f3fSJeffle Xu irq_queues += (nr_io_queues - poll_queues);
2248a2da0e5cSSean Anderson if (dev->ctrl.quirks & NVME_QUIRK_BROKEN_MSI)
2249a2da0e5cSSean Anderson flags &= ~PCI_IRQ_MSI;
2250a2da0e5cSSean Anderson return pci_alloc_irq_vectors_affinity(pdev, 1, irq_queues, flags,
2251a2da0e5cSSean Anderson &affd);
22523b6592f7SJens Axboe }
22533b6592f7SJens Axboe
nvme_max_io_queues(struct nvme_dev * dev)22542a5bcfddSWeiping Zhang static unsigned int nvme_max_io_queues(struct nvme_dev *dev)
22552a5bcfddSWeiping Zhang {
2256e3aef095SNiklas Schnelle /*
2257e3aef095SNiklas Schnelle * If tags are shared with admin queue (Apple bug), then
2258e3aef095SNiklas Schnelle * make sure we only use one IO queue.
2259e3aef095SNiklas Schnelle */
2260e3aef095SNiklas Schnelle if (dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS)
2261e3aef095SNiklas Schnelle return 1;
22622a5bcfddSWeiping Zhang return num_possible_cpus() + dev->nr_write_queues + dev->nr_poll_queues;
22632a5bcfddSWeiping Zhang }
22642a5bcfddSWeiping Zhang
nvme_setup_io_queues(struct nvme_dev * dev)226557dacad5SJay Sternberg static int nvme_setup_io_queues(struct nvme_dev *dev)
226657dacad5SJay Sternberg {
2267147b27e4SSagi Grimberg struct nvme_queue *adminq = &dev->queues[0];
226857dacad5SJay Sternberg struct pci_dev *pdev = to_pci_dev(dev->dev);
22692a5bcfddSWeiping Zhang unsigned int nr_io_queues;
227097f6ef64SXu Yu unsigned long size;
22712a5bcfddSWeiping Zhang int result;
227257dacad5SJay Sternberg
22732a5bcfddSWeiping Zhang /*
22742a5bcfddSWeiping Zhang * Sample the module parameters once at reset time so that we have
22752a5bcfddSWeiping Zhang * stable values to work with.
22762a5bcfddSWeiping Zhang */
22772a5bcfddSWeiping Zhang dev->nr_write_queues = write_queues;
22782a5bcfddSWeiping Zhang dev->nr_poll_queues = poll_queues;
2279d38e9f04SBenjamin Herrenschmidt
2280ff4e5fbaSNiklas Schnelle nr_io_queues = dev->nr_allocated_queues - 1;
22819a0be7abSChristoph Hellwig result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
22829a0be7abSChristoph Hellwig if (result < 0)
228357dacad5SJay Sternberg return result;
22849a0be7abSChristoph Hellwig
2285f5fa90dcSChristoph Hellwig if (nr_io_queues == 0)
2286a5229050SKeith Busch return 0;
228757dacad5SJay Sternberg
2288e4b9852aSCasey Chen /*
2289e4b9852aSCasey Chen * Free IRQ resources as soon as NVMEQ_ENABLED bit transitions
2290e4b9852aSCasey Chen * from set to unset. If there is a window to it is truely freed,
2291e4b9852aSCasey Chen * pci_free_irq_vectors() jumping into this window will crash.
2292e4b9852aSCasey Chen * And take lock to avoid racing with pci_free_irq_vectors() in
2293e4b9852aSCasey Chen * nvme_dev_disable() path.
2294e4b9852aSCasey Chen */
2295e4b9852aSCasey Chen result = nvme_setup_io_queues_trylock(dev);
2296e4b9852aSCasey Chen if (result)
2297e4b9852aSCasey Chen return result;
2298e4b9852aSCasey Chen if (test_and_clear_bit(NVMEQ_ENABLED, &adminq->flags))
2299e4b9852aSCasey Chen pci_free_irq(pdev, 0, adminq);
23004e224106SChristoph Hellwig
23010f238ff5SLogan Gunthorpe if (dev->cmb_use_sqes) {
230257dacad5SJay Sternberg result = nvme_cmb_qdepth(dev, nr_io_queues,
230357dacad5SJay Sternberg sizeof(struct nvme_command));
230488d356caSChristoph Hellwig if (result > 0) {
230557dacad5SJay Sternberg dev->q_depth = result;
230688d356caSChristoph Hellwig dev->ctrl.sqsize = result - 1;
230788d356caSChristoph Hellwig } else {
23080f238ff5SLogan Gunthorpe dev->cmb_use_sqes = false;
230957dacad5SJay Sternberg }
231088d356caSChristoph Hellwig }
231157dacad5SJay Sternberg
231257dacad5SJay Sternberg do {
231397f6ef64SXu Yu size = db_bar_size(dev, nr_io_queues);
231497f6ef64SXu Yu result = nvme_remap_bar(dev, size);
231597f6ef64SXu Yu if (!result)
231657dacad5SJay Sternberg break;
2317e4b9852aSCasey Chen if (!--nr_io_queues) {
2318e4b9852aSCasey Chen result = -ENOMEM;
2319e4b9852aSCasey Chen goto out_unlock;
2320e4b9852aSCasey Chen }
232157dacad5SJay Sternberg } while (1);
232257dacad5SJay Sternberg adminq->q_db = dev->dbs;
232357dacad5SJay Sternberg
23248fae268bSKeith Busch retry:
232557dacad5SJay Sternberg /* Deregister the admin queue's interrupt */
2326e4b9852aSCasey Chen if (test_and_clear_bit(NVMEQ_ENABLED, &adminq->flags))
23270ff199cbSChristoph Hellwig pci_free_irq(pdev, 0, adminq);
232857dacad5SJay Sternberg
232957dacad5SJay Sternberg /*
233057dacad5SJay Sternberg * If we enable msix early due to not intx, disable it again before
233157dacad5SJay Sternberg * setting up the full range we need.
233257dacad5SJay Sternberg */
2333dca51e78SChristoph Hellwig pci_free_irq_vectors(pdev);
23343b6592f7SJens Axboe
23353b6592f7SJens Axboe result = nvme_setup_irqs(dev, nr_io_queues);
2336e4b9852aSCasey Chen if (result <= 0) {
2337e4b9852aSCasey Chen result = -EIO;
2338e4b9852aSCasey Chen goto out_unlock;
2339e4b9852aSCasey Chen }
23403b6592f7SJens Axboe
234122b55601SKeith Busch dev->num_vecs = result;
23424b04cc6aSJens Axboe result = max(result - 1, 1);
2343e20ba6e1SChristoph Hellwig dev->max_qid = result + dev->io_queues[HCTX_TYPE_POLL];
234457dacad5SJay Sternberg
234557dacad5SJay Sternberg /*
234657dacad5SJay Sternberg * Should investigate if there's a performance win from allocating
234757dacad5SJay Sternberg * more queues than interrupt vectors; it might allow the submission
234857dacad5SJay Sternberg * path to scale better, even if the receive path is limited by the
234957dacad5SJay Sternberg * number of interrupts.
235057dacad5SJay Sternberg */
2351dca51e78SChristoph Hellwig result = queue_request_irq(adminq);
23527c349ddeSKeith Busch if (result)
2353e4b9852aSCasey Chen goto out_unlock;
23544e224106SChristoph Hellwig set_bit(NVMEQ_ENABLED, &adminq->flags);
2355e4b9852aSCasey Chen mutex_unlock(&dev->shutdown_lock);
23568fae268bSKeith Busch
23578fae268bSKeith Busch result = nvme_create_io_queues(dev);
23588fae268bSKeith Busch if (result || dev->online_queues < 2)
23598fae268bSKeith Busch return result;
23608fae268bSKeith Busch
23618fae268bSKeith Busch if (dev->online_queues - 1 < dev->max_qid) {
23628fae268bSKeith Busch nr_io_queues = dev->online_queues - 1;
23637d879c90SChristoph Hellwig nvme_delete_io_queues(dev);
2364e4b9852aSCasey Chen result = nvme_setup_io_queues_trylock(dev);
2365e4b9852aSCasey Chen if (result)
2366e4b9852aSCasey Chen return result;
23678fae268bSKeith Busch nvme_suspend_io_queues(dev);
23688fae268bSKeith Busch goto retry;
23698fae268bSKeith Busch }
23708fae268bSKeith Busch dev_info(dev->ctrl.device, "%d/%d/%d default/read/poll queues\n",
23718fae268bSKeith Busch dev->io_queues[HCTX_TYPE_DEFAULT],
23728fae268bSKeith Busch dev->io_queues[HCTX_TYPE_READ],
23738fae268bSKeith Busch dev->io_queues[HCTX_TYPE_POLL]);
23748fae268bSKeith Busch return 0;
2375e4b9852aSCasey Chen out_unlock:
2376e4b9852aSCasey Chen mutex_unlock(&dev->shutdown_lock);
2377e4b9852aSCasey Chen return result;
237857dacad5SJay Sternberg }
237957dacad5SJay Sternberg
nvme_del_queue_end(struct request * req,blk_status_t error)2380de671d61SJens Axboe static enum rq_end_io_ret nvme_del_queue_end(struct request *req,
2381de671d61SJens Axboe blk_status_t error)
2382db3cbfffSKeith Busch {
2383db3cbfffSKeith Busch struct nvme_queue *nvmeq = req->end_io_data;
2384db3cbfffSKeith Busch
2385db3cbfffSKeith Busch blk_mq_free_request(req);
2386d1ed6aa1SChristoph Hellwig complete(&nvmeq->delete_done);
2387de671d61SJens Axboe return RQ_END_IO_NONE;
2388db3cbfffSKeith Busch }
2389db3cbfffSKeith Busch
nvme_del_cq_end(struct request * req,blk_status_t error)2390de671d61SJens Axboe static enum rq_end_io_ret nvme_del_cq_end(struct request *req,
2391de671d61SJens Axboe blk_status_t error)
2392db3cbfffSKeith Busch {
2393db3cbfffSKeith Busch struct nvme_queue *nvmeq = req->end_io_data;
2394db3cbfffSKeith Busch
2395d1ed6aa1SChristoph Hellwig if (error)
2396d1ed6aa1SChristoph Hellwig set_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags);
2397db3cbfffSKeith Busch
2398de671d61SJens Axboe return nvme_del_queue_end(req, error);
2399db3cbfffSKeith Busch }
2400db3cbfffSKeith Busch
nvme_delete_queue(struct nvme_queue * nvmeq,u8 opcode)2401db3cbfffSKeith Busch static int nvme_delete_queue(struct nvme_queue *nvmeq, u8 opcode)
2402db3cbfffSKeith Busch {
2403db3cbfffSKeith Busch struct request_queue *q = nvmeq->dev->ctrl.admin_q;
2404db3cbfffSKeith Busch struct request *req;
2405f66e2804SChaitanya Kulkarni struct nvme_command cmd = { };
2406db3cbfffSKeith Busch
2407db3cbfffSKeith Busch cmd.delete_queue.opcode = opcode;
2408db3cbfffSKeith Busch cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2409db3cbfffSKeith Busch
2410e559398fSChristoph Hellwig req = blk_mq_alloc_request(q, nvme_req_op(&cmd), BLK_MQ_REQ_NOWAIT);
2411db3cbfffSKeith Busch if (IS_ERR(req))
2412db3cbfffSKeith Busch return PTR_ERR(req);
2413e559398fSChristoph Hellwig nvme_init_request(req, &cmd);
2414db3cbfffSKeith Busch
2415e2e53086SChristoph Hellwig if (opcode == nvme_admin_delete_cq)
2416e2e53086SChristoph Hellwig req->end_io = nvme_del_cq_end;
2417e2e53086SChristoph Hellwig else
2418e2e53086SChristoph Hellwig req->end_io = nvme_del_queue_end;
2419db3cbfffSKeith Busch req->end_io_data = nvmeq;
2420db3cbfffSKeith Busch
2421d1ed6aa1SChristoph Hellwig init_completion(&nvmeq->delete_done);
2422e2e53086SChristoph Hellwig blk_execute_rq_nowait(req, false);
2423db3cbfffSKeith Busch return 0;
2424db3cbfffSKeith Busch }
2425db3cbfffSKeith Busch
__nvme_delete_io_queues(struct nvme_dev * dev,u8 opcode)24267d879c90SChristoph Hellwig static bool __nvme_delete_io_queues(struct nvme_dev *dev, u8 opcode)
2427db3cbfffSKeith Busch {
24285271edd4SChristoph Hellwig int nr_queues = dev->online_queues - 1, sent = 0;
2429db3cbfffSKeith Busch unsigned long timeout;
2430db3cbfffSKeith Busch
2431db3cbfffSKeith Busch retry:
2432dc96f938SChaitanya Kulkarni timeout = NVME_ADMIN_TIMEOUT;
24335271edd4SChristoph Hellwig while (nr_queues > 0) {
24345271edd4SChristoph Hellwig if (nvme_delete_queue(&dev->queues[nr_queues], opcode))
2435db3cbfffSKeith Busch break;
24365271edd4SChristoph Hellwig nr_queues--;
24375271edd4SChristoph Hellwig sent++;
24385271edd4SChristoph Hellwig }
2439d1ed6aa1SChristoph Hellwig while (sent) {
2440d1ed6aa1SChristoph Hellwig struct nvme_queue *nvmeq = &dev->queues[nr_queues + sent];
2441d1ed6aa1SChristoph Hellwig
2442d1ed6aa1SChristoph Hellwig timeout = wait_for_completion_io_timeout(&nvmeq->delete_done,
24435271edd4SChristoph Hellwig timeout);
2444db3cbfffSKeith Busch if (timeout == 0)
24455271edd4SChristoph Hellwig return false;
2446d1ed6aa1SChristoph Hellwig
2447d1ed6aa1SChristoph Hellwig sent--;
24485271edd4SChristoph Hellwig if (nr_queues)
2449db3cbfffSKeith Busch goto retry;
2450db3cbfffSKeith Busch }
24515271edd4SChristoph Hellwig return true;
2452db3cbfffSKeith Busch }
2453db3cbfffSKeith Busch
nvme_delete_io_queues(struct nvme_dev * dev)24547d879c90SChristoph Hellwig static void nvme_delete_io_queues(struct nvme_dev *dev)
245557dacad5SJay Sternberg {
24567d879c90SChristoph Hellwig if (__nvme_delete_io_queues(dev, nvme_admin_delete_sq))
24577d879c90SChristoph Hellwig __nvme_delete_io_queues(dev, nvme_admin_delete_cq);
24582b1b7e78SJianchao Wang }
24597d879c90SChristoph Hellwig
nvme_pci_nr_maps(struct nvme_dev * dev)24600da7feaaSChristoph Hellwig static unsigned int nvme_pci_nr_maps(struct nvme_dev *dev)
246157dacad5SJay Sternberg {
246257dacad5SJay Sternberg if (dev->io_queues[HCTX_TYPE_POLL])
24630da7feaaSChristoph Hellwig return 3;
24640da7feaaSChristoph Hellwig if (dev->io_queues[HCTX_TYPE_READ])
24650da7feaaSChristoph Hellwig return 2;
24660da7feaaSChristoph Hellwig return 1;
246757dacad5SJay Sternberg }
2468949928c1SKeith Busch
nvme_pci_update_nr_queues(struct nvme_dev * dev)24694ed32cc0SMaurizio Lombardi static bool nvme_pci_update_nr_queues(struct nvme_dev *dev)
24702455a4b7SChristoph Hellwig {
247105500a48SKeith Busch if (!dev->ctrl.tagset) {
247205500a48SKeith Busch nvme_alloc_io_tag_set(&dev->ctrl, &dev->tagset, &nvme_mq_ops,
247305500a48SKeith Busch nvme_pci_nr_maps(dev), sizeof(struct nvme_iod));
24744ed32cc0SMaurizio Lombardi return true;
24754ed32cc0SMaurizio Lombardi }
24764ed32cc0SMaurizio Lombardi
24774ed32cc0SMaurizio Lombardi /* Give up if we are racing with nvme_dev_disable() */
24784ed32cc0SMaurizio Lombardi if (!mutex_trylock(&dev->shutdown_lock))
24794ed32cc0SMaurizio Lombardi return false;
24804ed32cc0SMaurizio Lombardi
24814ed32cc0SMaurizio Lombardi /* Check if nvme_dev_disable() has been executed already */
24824ed32cc0SMaurizio Lombardi if (!dev->online_queues) {
24834ed32cc0SMaurizio Lombardi mutex_unlock(&dev->shutdown_lock);
24844ed32cc0SMaurizio Lombardi return false;
248505500a48SKeith Busch }
248605500a48SKeith Busch
24872455a4b7SChristoph Hellwig blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1);
24882455a4b7SChristoph Hellwig /* free previously allocated queues that are no longer usable */
24892455a4b7SChristoph Hellwig nvme_free_queues(dev, dev->online_queues);
24904ed32cc0SMaurizio Lombardi mutex_unlock(&dev->shutdown_lock);
24914ed32cc0SMaurizio Lombardi return true;
249257dacad5SJay Sternberg }
249357dacad5SJay Sternberg
nvme_pci_enable(struct nvme_dev * dev)2494b00a726aSKeith Busch static int nvme_pci_enable(struct nvme_dev *dev)
249557dacad5SJay Sternberg {
2496b00a726aSKeith Busch int result = -ENOMEM;
249757dacad5SJay Sternberg struct pci_dev *pdev = to_pci_dev(dev->dev);
2498a2da0e5cSSean Anderson unsigned int flags = PCI_IRQ_ALL_TYPES;
249957dacad5SJay Sternberg
250057dacad5SJay Sternberg if (pci_enable_device_mem(pdev))
250157dacad5SJay Sternberg return result;
250257dacad5SJay Sternberg
250357dacad5SJay Sternberg pci_set_master(pdev);
250457dacad5SJay Sternberg
25057a67cbeaSChristoph Hellwig if (readl(dev->bar + NVME_REG_CSTS) == -1) {
250657dacad5SJay Sternberg result = -ENODEV;
2507b00a726aSKeith Busch goto disable;
250857dacad5SJay Sternberg }
250957dacad5SJay Sternberg
251057dacad5SJay Sternberg /*
2511a5229050SKeith Busch * Some devices and/or platforms don't advertise or work with INTx
2512a5229050SKeith Busch * interrupts. Pre-enable a single MSIX or MSI vec for setup. We'll
2513a5229050SKeith Busch * adjust this later.
251457dacad5SJay Sternberg */
2515a2da0e5cSSean Anderson if (dev->ctrl.quirks & NVME_QUIRK_BROKEN_MSI)
2516a2da0e5cSSean Anderson flags &= ~PCI_IRQ_MSI;
2517a2da0e5cSSean Anderson result = pci_alloc_irq_vectors(pdev, 1, 1, flags);
2518dca51e78SChristoph Hellwig if (result < 0)
251909113abfSTong Zhang goto disable;
252057dacad5SJay Sternberg
252120d0dfe6SSagi Grimberg dev->ctrl.cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
25227a67cbeaSChristoph Hellwig
25237442ddceSJohn Garry dev->q_depth = min_t(u32, NVME_CAP_MQES(dev->ctrl.cap) + 1,
2524b27c1e68Sweiping zhang io_queue_depth);
252520d0dfe6SSagi Grimberg dev->db_stride = 1 << NVME_CAP_STRIDE(dev->ctrl.cap);
25267a67cbeaSChristoph Hellwig dev->dbs = dev->bar + 4096;
25271f390c1fSStephan Günther
25281f390c1fSStephan Günther /*
252966341331SBenjamin Herrenschmidt * Some Apple controllers require a non-standard SQE size.
253066341331SBenjamin Herrenschmidt * Interestingly they also seem to ignore the CC:IOSQES register
253166341331SBenjamin Herrenschmidt * so we don't bother updating it here.
253266341331SBenjamin Herrenschmidt */
253366341331SBenjamin Herrenschmidt if (dev->ctrl.quirks & NVME_QUIRK_128_BYTES_SQES)
253466341331SBenjamin Herrenschmidt dev->io_sqes = 7;
253566341331SBenjamin Herrenschmidt else
2536c1e0cc7eSBenjamin Herrenschmidt dev->io_sqes = NVME_NVM_IOSQES;
25371f390c1fSStephan Günther
2538ab51a98dSKeith Busch if (dev->ctrl.quirks & NVME_QUIRK_QDEPTH_ONE) {
25391f390c1fSStephan Günther dev->q_depth = 2;
2540d554b5e1SMartin K. Petersen } else if (pdev->vendor == PCI_VENDOR_ID_SAMSUNG &&
2541d554b5e1SMartin K. Petersen (pdev->device == 0xa821 || pdev->device == 0xa822) &&
254220d0dfe6SSagi Grimberg NVME_CAP_MQES(dev->ctrl.cap) == 0) {
2543d554b5e1SMartin K. Petersen dev->q_depth = 64;
2544d554b5e1SMartin K. Petersen dev_err(dev->ctrl.device, "detected PM1725 NVMe controller, "
2545d554b5e1SMartin K. Petersen "set queue depth=%u\n", dev->q_depth);
25461f390c1fSStephan Günther }
25471f390c1fSStephan Günther
2548d38e9f04SBenjamin Herrenschmidt /*
2549d38e9f04SBenjamin Herrenschmidt * Controllers with the shared tags quirk need the IO queue to be
2550d38e9f04SBenjamin Herrenschmidt * big enough so that we get 32 tags for the admin queue
2551d38e9f04SBenjamin Herrenschmidt */
2552d38e9f04SBenjamin Herrenschmidt if ((dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS) &&
2553d38e9f04SBenjamin Herrenschmidt (dev->q_depth < (NVME_AQ_DEPTH + 2))) {
2554d38e9f04SBenjamin Herrenschmidt dev->q_depth = NVME_AQ_DEPTH + 2;
2555d38e9f04SBenjamin Herrenschmidt dev_warn(dev->ctrl.device, "IO queue depth clamped to %d\n",
2556d38e9f04SBenjamin Herrenschmidt dev->q_depth);
2557d38e9f04SBenjamin Herrenschmidt }
255888d356caSChristoph Hellwig dev->ctrl.sqsize = dev->q_depth - 1; /* 0's based queue depth */
2559d38e9f04SBenjamin Herrenschmidt
2560f65efd6dSChristoph Hellwig nvme_map_cmb(dev);
2561202021c1SStephen Bates
2562a0a3408eSKeith Busch pci_save_state(pdev);
2563a6ee7f19SChristoph Hellwig
256409113abfSTong Zhang result = nvme_pci_configure_admin_queue(dev);
256509113abfSTong Zhang if (result)
256609113abfSTong Zhang goto free_irq;
256709113abfSTong Zhang return result;
256857dacad5SJay Sternberg
256909113abfSTong Zhang free_irq:
257009113abfSTong Zhang pci_free_irq_vectors(pdev);
257157dacad5SJay Sternberg disable:
257257dacad5SJay Sternberg pci_disable_device(pdev);
257357dacad5SJay Sternberg return result;
257457dacad5SJay Sternberg }
257557dacad5SJay Sternberg
nvme_dev_unmap(struct nvme_dev * dev)257657dacad5SJay Sternberg static void nvme_dev_unmap(struct nvme_dev *dev)
257757dacad5SJay Sternberg {
2578b00a726aSKeith Busch if (dev->bar)
2579b00a726aSKeith Busch iounmap(dev->bar);
2580a1f447b3SJohannes Thumshirn pci_release_mem_regions(to_pci_dev(dev->dev));
2581b00a726aSKeith Busch }
2582b00a726aSKeith Busch
nvme_pci_ctrl_is_dead(struct nvme_dev * dev)258368e81ebaSChristoph Hellwig static bool nvme_pci_ctrl_is_dead(struct nvme_dev *dev)
2584b00a726aSKeith Busch {
258557dacad5SJay Sternberg struct pci_dev *pdev = to_pci_dev(dev->dev);
2586081f5e75SKeith Busch u32 csts;
258757dacad5SJay Sternberg
258868e81ebaSChristoph Hellwig if (!pci_is_enabled(pdev) || !pci_device_is_present(pdev))
258968e81ebaSChristoph Hellwig return true;
259068e81ebaSChristoph Hellwig if (pdev->error_state != pci_channel_io_normal)
259168e81ebaSChristoph Hellwig return true;
259257dacad5SJay Sternberg
259368e81ebaSChristoph Hellwig csts = readl(dev->bar + NVME_REG_CSTS);
259468e81ebaSChristoph Hellwig return (csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY);
2595a0a3408eSKeith Busch }
259657dacad5SJay Sternberg
nvme_dev_disable(struct nvme_dev * dev,bool shutdown)2597a5cdb68cSKeith Busch static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
259857dacad5SJay Sternberg {
25998884a56dSKeith Busch enum nvme_ctrl_state state = nvme_ctrl_state(&dev->ctrl);
2600302ad8ccSKeith Busch struct pci_dev *pdev = to_pci_dev(dev->dev);
260168e81ebaSChristoph Hellwig bool dead;
260257dacad5SJay Sternberg
260377bf25eaSKeith Busch mutex_lock(&dev->shutdown_lock);
260468e81ebaSChristoph Hellwig dead = nvme_pci_ctrl_is_dead(dev);
26058884a56dSKeith Busch if (state == NVME_CTRL_LIVE || state == NVME_CTRL_RESETTING) {
260668e81ebaSChristoph Hellwig if (pci_is_enabled(pdev))
2607302ad8ccSKeith Busch nvme_start_freeze(&dev->ctrl);
2608302ad8ccSKeith Busch /*
260968e81ebaSChristoph Hellwig * Give the controller a chance to complete all entered requests
261068e81ebaSChristoph Hellwig * if doing a safe shutdown.
2611302ad8ccSKeith Busch */
261268e81ebaSChristoph Hellwig if (!dead && shutdown)
2613302ad8ccSKeith Busch nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
261468e81ebaSChristoph Hellwig }
261587ad72a5SChristoph Hellwig
26169f27bd70SChristoph Hellwig nvme_quiesce_io_queues(&dev->ctrl);
26179a915a5bSJianchao Wang
261864ee0ac0SKeith Busch if (!dead && dev->ctrl.queue_count > 0) {
26197d879c90SChristoph Hellwig nvme_delete_io_queues(dev);
262047d42d22SChristoph Hellwig nvme_disable_ctrl(&dev->ctrl, shutdown);
262147d42d22SChristoph Hellwig nvme_poll_irqdisable(&dev->queues[0]);
262257dacad5SJay Sternberg }
26238fae268bSKeith Busch nvme_suspend_io_queues(dev);
262410981f23SChristoph Hellwig nvme_suspend_queue(dev, 0);
2625c80767f7SChristoph Hellwig pci_free_irq_vectors(pdev);
26261ad11eafSBjorn Helgaas if (pci_is_enabled(pdev))
2627c80767f7SChristoph Hellwig pci_disable_device(pdev);
2628fa46c6fbSKeith Busch nvme_reap_pending_cqes(dev);
262957dacad5SJay Sternberg
26301fcfca78SGuixin Liu nvme_cancel_tagset(&dev->ctrl);
26311fcfca78SGuixin Liu nvme_cancel_admin_tagset(&dev->ctrl);
2632302ad8ccSKeith Busch
2633302ad8ccSKeith Busch /*
2634302ad8ccSKeith Busch * The driver will not be starting up queues again if shutting down so
2635302ad8ccSKeith Busch * must flush all entered requests to their failed completion to avoid
2636302ad8ccSKeith Busch * deadlocking blk-mq hot-cpu notifier.
2637302ad8ccSKeith Busch */
2638c8e9e9b7SKeith Busch if (shutdown) {
26399f27bd70SChristoph Hellwig nvme_unquiesce_io_queues(&dev->ctrl);
2640c8e9e9b7SKeith Busch if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q))
26419f27bd70SChristoph Hellwig nvme_unquiesce_admin_queue(&dev->ctrl);
2642c8e9e9b7SKeith Busch }
264377bf25eaSKeith Busch mutex_unlock(&dev->shutdown_lock);
264457dacad5SJay Sternberg }
264557dacad5SJay Sternberg
nvme_disable_prepare_reset(struct nvme_dev * dev,bool shutdown)2646c1ac9a4bSKeith Busch static int nvme_disable_prepare_reset(struct nvme_dev *dev, bool shutdown)
2647c1ac9a4bSKeith Busch {
2648c1ac9a4bSKeith Busch if (!nvme_wait_reset(&dev->ctrl))
2649c1ac9a4bSKeith Busch return -EBUSY;
2650c1ac9a4bSKeith Busch nvme_dev_disable(dev, shutdown);
2651c1ac9a4bSKeith Busch return 0;
2652c1ac9a4bSKeith Busch }
2653c1ac9a4bSKeith Busch
nvme_setup_prp_pools(struct nvme_dev * dev)265457dacad5SJay Sternberg static int nvme_setup_prp_pools(struct nvme_dev *dev)
265557dacad5SJay Sternberg {
265634765438SRobert Beckett size_t small_align = 256;
265734765438SRobert Beckett
265857dacad5SJay Sternberg dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
2659c61b82c7SChristoph Hellwig NVME_CTRL_PAGE_SIZE,
2660c61b82c7SChristoph Hellwig NVME_CTRL_PAGE_SIZE, 0);
266157dacad5SJay Sternberg if (!dev->prp_page_pool)
266257dacad5SJay Sternberg return -ENOMEM;
266357dacad5SJay Sternberg
266434765438SRobert Beckett if (dev->ctrl.quirks & NVME_QUIRK_DMAPOOL_ALIGN_512)
266534765438SRobert Beckett small_align = 512;
266634765438SRobert Beckett
266757dacad5SJay Sternberg /* Optimisation for I/Os between 4k and 128k */
266857dacad5SJay Sternberg dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
266934765438SRobert Beckett 256, small_align, 0);
267057dacad5SJay Sternberg if (!dev->prp_small_pool) {
267157dacad5SJay Sternberg dma_pool_destroy(dev->prp_page_pool);
267257dacad5SJay Sternberg return -ENOMEM;
267357dacad5SJay Sternberg }
267457dacad5SJay Sternberg return 0;
267557dacad5SJay Sternberg }
267657dacad5SJay Sternberg
nvme_release_prp_pools(struct nvme_dev * dev)267757dacad5SJay Sternberg static void nvme_release_prp_pools(struct nvme_dev *dev)
267857dacad5SJay Sternberg {
267957dacad5SJay Sternberg dma_pool_destroy(dev->prp_page_pool);
268057dacad5SJay Sternberg dma_pool_destroy(dev->prp_small_pool);
268157dacad5SJay Sternberg }
268257dacad5SJay Sternberg
nvme_pci_alloc_iod_mempool(struct nvme_dev * dev)2683081a7d95SChristoph Hellwig static int nvme_pci_alloc_iod_mempool(struct nvme_dev *dev)
2684081a7d95SChristoph Hellwig {
26857846c1b5SKeith Busch size_t alloc_size = sizeof(struct scatterlist) * NVME_MAX_SEGS;
2686081a7d95SChristoph Hellwig
2687081a7d95SChristoph Hellwig dev->iod_mempool = mempool_create_node(1,
2688081a7d95SChristoph Hellwig mempool_kmalloc, mempool_kfree,
2689081a7d95SChristoph Hellwig (void *)alloc_size, GFP_KERNEL,
2690081a7d95SChristoph Hellwig dev_to_node(dev->dev));
2691081a7d95SChristoph Hellwig if (!dev->iod_mempool)
2692081a7d95SChristoph Hellwig return -ENOMEM;
2693081a7d95SChristoph Hellwig return 0;
2694081a7d95SChristoph Hellwig }
2695081a7d95SChristoph Hellwig
nvme_free_tagset(struct nvme_dev * dev)2696770597ecSKeith Busch static void nvme_free_tagset(struct nvme_dev *dev)
2697770597ecSKeith Busch {
2698770597ecSKeith Busch if (dev->tagset.tags)
26990da7feaaSChristoph Hellwig nvme_remove_io_tag_set(&dev->ctrl);
2700770597ecSKeith Busch dev->ctrl.tagset = NULL;
2701770597ecSKeith Busch }
2702770597ecSKeith Busch
27032e87570bSChristoph Hellwig /* pairs with nvme_pci_alloc_dev */
nvme_pci_free_ctrl(struct nvme_ctrl * ctrl)27041673f1f0SChristoph Hellwig static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
270557dacad5SJay Sternberg {
27061673f1f0SChristoph Hellwig struct nvme_dev *dev = to_nvme_dev(ctrl);
270757dacad5SJay Sternberg
2708770597ecSKeith Busch nvme_free_tagset(dev);
2709253fd4acSIsrael Rukshin put_device(dev->dev);
2710253fd4acSIsrael Rukshin kfree(dev->queues);
271157dacad5SJay Sternberg kfree(dev);
271257dacad5SJay Sternberg }
271357dacad5SJay Sternberg
nvme_reset_work(struct work_struct * work)2714fd634f41SChristoph Hellwig static void nvme_reset_work(struct work_struct *work)
271557dacad5SJay Sternberg {
2716d86c4d8eSChristoph Hellwig struct nvme_dev *dev =
2717d86c4d8eSChristoph Hellwig container_of(work, struct nvme_dev, ctrl.reset_work);
2718a98e58e5SScott Bauer bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
2719e71afda4SChaitanya Kulkarni int result;
272057dacad5SJay Sternberg
27218884a56dSKeith Busch if (nvme_ctrl_state(&dev->ctrl) != NVME_CTRL_RESETTING) {
27227764656bSZhihao Cheng dev_warn(dev->ctrl.device, "ctrl state %d is not RESETTING\n",
27237764656bSZhihao Cheng dev->ctrl.state);
27244e69d4daSKeith Busch result = -ENODEV;
27254e69d4daSKeith Busch goto out;
2726e71afda4SChaitanya Kulkarni }
2727fd634f41SChristoph Hellwig
2728fd634f41SChristoph Hellwig /*
2729fd634f41SChristoph Hellwig * If we're called to reset a live controller first shut it down before
2730fd634f41SChristoph Hellwig * moving on.
2731fd634f41SChristoph Hellwig */
2732b00a726aSKeith Busch if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
2733a5cdb68cSKeith Busch nvme_dev_disable(dev, false);
2734d6135c3aSKeith Busch nvme_sync_queues(&dev->ctrl);
2735fd634f41SChristoph Hellwig
27365c959d73SKeith Busch mutex_lock(&dev->shutdown_lock);
2737b00a726aSKeith Busch result = nvme_pci_enable(dev);
273857dacad5SJay Sternberg if (result)
27394726bcf3SKeith Busch goto out_unlock;
27409f27bd70SChristoph Hellwig nvme_unquiesce_admin_queue(&dev->ctrl);
27415c959d73SKeith Busch mutex_unlock(&dev->shutdown_lock);
27425c959d73SKeith Busch
27435c959d73SKeith Busch /*
27445c959d73SKeith Busch * Introduce CONNECTING state from nvme-fc/rdma transports to mark the
27455c959d73SKeith Busch * initializing procedure here.
27465c959d73SKeith Busch */
27475c959d73SKeith Busch if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_CONNECTING)) {
27485c959d73SKeith Busch dev_warn(dev->ctrl.device,
27495c959d73SKeith Busch "failed to mark controller CONNECTING\n");
2750cee6c269SMinwoo Im result = -EBUSY;
27515c959d73SKeith Busch goto out;
27525c959d73SKeith Busch }
2753943e942eSJens Axboe
275494cc781fSChristoph Hellwig result = nvme_init_ctrl_finish(&dev->ctrl, was_suspend);
2755ce4541f4SChristoph Hellwig if (result)
2756f58944e2SKeith Busch goto out;
2757ce4541f4SChristoph Hellwig
275865a54646SChristoph Hellwig nvme_dbbuf_dma_alloc(dev);
2759a98e58e5SScott Bauer
27609620cfbaSChristoph Hellwig result = nvme_setup_host_mem(dev);
27619620cfbaSChristoph Hellwig if (result < 0)
27629620cfbaSChristoph Hellwig goto out;
276387ad72a5SChristoph Hellwig
276457dacad5SJay Sternberg result = nvme_setup_io_queues(dev);
276557dacad5SJay Sternberg if (result)
2766f58944e2SKeith Busch goto out;
276757dacad5SJay Sternberg
276821f033f7SKeith Busch /*
27690ffc7e98SChristoph Hellwig * Freeze and update the number of I/O queues as thos might have
2770eac3ef26SChristoph Hellwig * changed. If there are no I/O queues left after this reset, keep the
2771eac3ef26SChristoph Hellwig * controller around but remove all namespaces.
277257dacad5SJay Sternberg */
27730ffc7e98SChristoph Hellwig if (dev->online_queues > 1) {
2774e2c03a2cSWilliam Butler nvme_dbbuf_set(dev);
27759f27bd70SChristoph Hellwig nvme_unquiesce_io_queues(&dev->ctrl);
2776302ad8ccSKeith Busch nvme_wait_freeze(&dev->ctrl);
27774ed32cc0SMaurizio Lombardi if (!nvme_pci_update_nr_queues(dev))
27784ed32cc0SMaurizio Lombardi goto out;
2779302ad8ccSKeith Busch nvme_unfreeze(&dev->ctrl);
27800ffc7e98SChristoph Hellwig } else {
27810ffc7e98SChristoph Hellwig dev_warn(dev->ctrl.device, "IO queues lost\n");
2782cd50f9b2SChristoph Hellwig nvme_mark_namespaces_dead(&dev->ctrl);
27839f27bd70SChristoph Hellwig nvme_unquiesce_io_queues(&dev->ctrl);
27840ffc7e98SChristoph Hellwig nvme_remove_namespaces(&dev->ctrl);
27850ffc7e98SChristoph Hellwig nvme_free_tagset(dev);
278657dacad5SJay Sternberg }
278757dacad5SJay Sternberg
27882b1b7e78SJianchao Wang /*
27892b1b7e78SJianchao Wang * If only admin queue live, keep it to do further investigation or
27902b1b7e78SJianchao Wang * recovery.
27912b1b7e78SJianchao Wang */
27925d02a5c1SKeith Busch if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) {
27932b1b7e78SJianchao Wang dev_warn(dev->ctrl.device,
27945d02a5c1SKeith Busch "failed to mark controller live state\n");
2795e71afda4SChaitanya Kulkarni result = -ENODEV;
2796bb8d261eSChristoph Hellwig goto out;
2797bb8d261eSChristoph Hellwig }
279892911a55SChristoph Hellwig
2799d09f2b45SSagi Grimberg nvme_start_ctrl(&dev->ctrl);
280057dacad5SJay Sternberg return;
280157dacad5SJay Sternberg
28024726bcf3SKeith Busch out_unlock:
28034726bcf3SKeith Busch mutex_unlock(&dev->shutdown_lock);
280457dacad5SJay Sternberg out:
2805c7c16c5bSChristoph Hellwig /*
2806c7c16c5bSChristoph Hellwig * Set state to deleting now to avoid blocking nvme_wait_reset(), which
2807c7c16c5bSChristoph Hellwig * may be holding this pci_dev's device lock.
2808c7c16c5bSChristoph Hellwig */
2809c7c16c5bSChristoph Hellwig dev_warn(dev->ctrl.device, "Disabling device after reset failure: %d\n",
2810c7c16c5bSChristoph Hellwig result);
2811c7c16c5bSChristoph Hellwig nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
2812c7c16c5bSChristoph Hellwig nvme_dev_disable(dev, true);
2813a2b5d544SKeith Busch nvme_sync_queues(&dev->ctrl);
2814c7c16c5bSChristoph Hellwig nvme_mark_namespaces_dead(&dev->ctrl);
28152ab4e5f4SKeith Busch nvme_unquiesce_io_queues(&dev->ctrl);
2816c7c16c5bSChristoph Hellwig nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD);
281757dacad5SJay Sternberg }
281857dacad5SJay Sternberg
nvme_pci_reg_read32(struct nvme_ctrl * ctrl,u32 off,u32 * val)28191c63dc66SChristoph Hellwig static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
282057dacad5SJay Sternberg {
28211c63dc66SChristoph Hellwig *val = readl(to_nvme_dev(ctrl)->bar + off);
28221c63dc66SChristoph Hellwig return 0;
282357dacad5SJay Sternberg }
28241c63dc66SChristoph Hellwig
nvme_pci_reg_write32(struct nvme_ctrl * ctrl,u32 off,u32 val)28255fd4ce1bSChristoph Hellwig static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
28265fd4ce1bSChristoph Hellwig {
28275fd4ce1bSChristoph Hellwig writel(val, to_nvme_dev(ctrl)->bar + off);
28285fd4ce1bSChristoph Hellwig return 0;
28295fd4ce1bSChristoph Hellwig }
28305fd4ce1bSChristoph Hellwig
nvme_pci_reg_read64(struct nvme_ctrl * ctrl,u32 off,u64 * val)28317fd8930fSChristoph Hellwig static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
28327fd8930fSChristoph Hellwig {
28333a8ecc93SArd Biesheuvel *val = lo_hi_readq(to_nvme_dev(ctrl)->bar + off);
28347fd8930fSChristoph Hellwig return 0;
28357fd8930fSChristoph Hellwig }
28367fd8930fSChristoph Hellwig
nvme_pci_get_address(struct nvme_ctrl * ctrl,char * buf,int size)283797c12223SKeith Busch static int nvme_pci_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
283897c12223SKeith Busch {
283997c12223SKeith Busch struct pci_dev *pdev = to_pci_dev(to_nvme_dev(ctrl)->dev);
284097c12223SKeith Busch
28412db24e4aSMax Gurtovoy return snprintf(buf, size, "%s\n", dev_name(&pdev->dev));
284297c12223SKeith Busch }
284397c12223SKeith Busch
nvme_pci_print_device_info(struct nvme_ctrl * ctrl)28442f0dad17SKeith Busch static void nvme_pci_print_device_info(struct nvme_ctrl *ctrl)
28452f0dad17SKeith Busch {
28462f0dad17SKeith Busch struct pci_dev *pdev = to_pci_dev(to_nvme_dev(ctrl)->dev);
28472f0dad17SKeith Busch struct nvme_subsystem *subsys = ctrl->subsys;
28482f0dad17SKeith Busch
28492f0dad17SKeith Busch dev_err(ctrl->device,
28502f0dad17SKeith Busch "VID:DID %04x:%04x model:%.*s firmware:%.*s\n",
28512f0dad17SKeith Busch pdev->vendor, pdev->device,
28522f0dad17SKeith Busch nvme_strlen(subsys->model, sizeof(subsys->model)),
28532f0dad17SKeith Busch subsys->model, nvme_strlen(subsys->firmware_rev,
28542f0dad17SKeith Busch sizeof(subsys->firmware_rev)),
28552f0dad17SKeith Busch subsys->firmware_rev);
28562f0dad17SKeith Busch }
28572f0dad17SKeith Busch
nvme_pci_supports_pci_p2pdma(struct nvme_ctrl * ctrl)28582f859441SLogan Gunthorpe static bool nvme_pci_supports_pci_p2pdma(struct nvme_ctrl *ctrl)
28592f859441SLogan Gunthorpe {
28602f859441SLogan Gunthorpe struct nvme_dev *dev = to_nvme_dev(ctrl);
28612f859441SLogan Gunthorpe
28622f859441SLogan Gunthorpe return dma_pci_p2pdma_supported(dev->dev);
28632f859441SLogan Gunthorpe }
28642f859441SLogan Gunthorpe
28651c63dc66SChristoph Hellwig static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
28661a353d85SMing Lin .name = "pcie",
2867e439bb12SSagi Grimberg .module = THIS_MODULE,
28682f859441SLogan Gunthorpe .flags = NVME_F_METADATA_SUPPORTED,
286986adbf0cSChristoph Hellwig .dev_attr_groups = nvme_pci_dev_attr_groups,
28701c63dc66SChristoph Hellwig .reg_read32 = nvme_pci_reg_read32,
28715fd4ce1bSChristoph Hellwig .reg_write32 = nvme_pci_reg_write32,
28727fd8930fSChristoph Hellwig .reg_read64 = nvme_pci_reg_read64,
28731673f1f0SChristoph Hellwig .free_ctrl = nvme_pci_free_ctrl,
2874f866fc42SChristoph Hellwig .submit_async_event = nvme_pci_submit_async_event,
287597c12223SKeith Busch .get_address = nvme_pci_get_address,
28762f0dad17SKeith Busch .print_device_info = nvme_pci_print_device_info,
28772f859441SLogan Gunthorpe .supports_pci_p2pdma = nvme_pci_supports_pci_p2pdma,
28781c63dc66SChristoph Hellwig };
287957dacad5SJay Sternberg
nvme_dev_map(struct nvme_dev * dev)2880b00a726aSKeith Busch static int nvme_dev_map(struct nvme_dev *dev)
2881b00a726aSKeith Busch {
2882b00a726aSKeith Busch struct pci_dev *pdev = to_pci_dev(dev->dev);
2883b00a726aSKeith Busch
2884a1f447b3SJohannes Thumshirn if (pci_request_mem_regions(pdev, "nvme"))
2885b00a726aSKeith Busch return -ENODEV;
2886b00a726aSKeith Busch
288797f6ef64SXu Yu if (nvme_remap_bar(dev, NVME_REG_DBS + 4096))
2888b00a726aSKeith Busch goto release;
2889b00a726aSKeith Busch
2890b00a726aSKeith Busch return 0;
2891b00a726aSKeith Busch release:
2892a1f447b3SJohannes Thumshirn pci_release_mem_regions(pdev);
2893b00a726aSKeith Busch return -ENODEV;
2894b00a726aSKeith Busch }
2895b00a726aSKeith Busch
check_vendor_combination_bug(struct pci_dev * pdev)28968427bbc2SKai-Heng Feng static unsigned long check_vendor_combination_bug(struct pci_dev *pdev)
2897ff5350a8SAndy Lutomirski {
2898ff5350a8SAndy Lutomirski if (pdev->vendor == 0x144d && pdev->device == 0xa802) {
2899ff5350a8SAndy Lutomirski /*
2900ff5350a8SAndy Lutomirski * Several Samsung devices seem to drop off the PCIe bus
2901ff5350a8SAndy Lutomirski * randomly when APST is on and uses the deepest sleep state.
2902ff5350a8SAndy Lutomirski * This has been observed on a Samsung "SM951 NVMe SAMSUNG
2903ff5350a8SAndy Lutomirski * 256GB", a "PM951 NVMe SAMSUNG 512GB", and a "Samsung SSD
2904ff5350a8SAndy Lutomirski * 950 PRO 256GB", but it seems to be restricted to two Dell
2905ff5350a8SAndy Lutomirski * laptops.
2906ff5350a8SAndy Lutomirski */
2907ff5350a8SAndy Lutomirski if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.") &&
2908ff5350a8SAndy Lutomirski (dmi_match(DMI_PRODUCT_NAME, "XPS 15 9550") ||
2909ff5350a8SAndy Lutomirski dmi_match(DMI_PRODUCT_NAME, "Precision 5510")))
2910ff5350a8SAndy Lutomirski return NVME_QUIRK_NO_DEEPEST_PS;
29118427bbc2SKai-Heng Feng } else if (pdev->vendor == 0x144d && pdev->device == 0xa804) {
29128427bbc2SKai-Heng Feng /*
29138427bbc2SKai-Heng Feng * Samsung SSD 960 EVO drops off the PCIe bus after system
2914467c77d4SJarosław Janik * suspend on a Ryzen board, ASUS PRIME B350M-A, as well as
2915467c77d4SJarosław Janik * within few minutes after bootup on a Coffee Lake board -
2916467c77d4SJarosław Janik * ASUS PRIME Z370-A
29178427bbc2SKai-Heng Feng */
29188427bbc2SKai-Heng Feng if (dmi_match(DMI_BOARD_VENDOR, "ASUSTeK COMPUTER INC.") &&
2919467c77d4SJarosław Janik (dmi_match(DMI_BOARD_NAME, "PRIME B350M-A") ||
2920467c77d4SJarosław Janik dmi_match(DMI_BOARD_NAME, "PRIME Z370-A")))
29218427bbc2SKai-Heng Feng return NVME_QUIRK_NO_APST;
29221fae37acSShyjumon N } else if ((pdev->vendor == 0x144d && (pdev->device == 0xa801 ||
29231fae37acSShyjumon N pdev->device == 0xa808 || pdev->device == 0xa809)) ||
29241fae37acSShyjumon N (pdev->vendor == 0x1e0f && pdev->device == 0x0001)) {
29251fae37acSShyjumon N /*
29261fae37acSShyjumon N * Forcing to use host managed nvme power settings for
29271fae37acSShyjumon N * lowest idle power with quick resume latency on
29281fae37acSShyjumon N * Samsung and Toshiba SSDs based on suspend behavior
29291fae37acSShyjumon N * on Coffee Lake board for LENOVO C640
29301fae37acSShyjumon N */
29311fae37acSShyjumon N if ((dmi_match(DMI_BOARD_VENDOR, "LENOVO")) &&
29321fae37acSShyjumon N dmi_match(DMI_BOARD_NAME, "LNVNB161216"))
29331fae37acSShyjumon N return NVME_QUIRK_SIMPLE_SUSPEND;
2934dd864f6eSGeorg Gottleuber } else if (pdev->vendor == 0x2646 && (pdev->device == 0x2263 ||
2935dd864f6eSGeorg Gottleuber pdev->device == 0x500f)) {
2936dd864f6eSGeorg Gottleuber /*
2937dd864f6eSGeorg Gottleuber * Exclude some Kingston NV1 and A2000 devices from
2938dd864f6eSGeorg Gottleuber * NVME_QUIRK_SIMPLE_SUSPEND. Do a full suspend to save a
2939dd864f6eSGeorg Gottleuber * lot fo energy with s2idle sleep on some TUXEDO platforms.
2940dd864f6eSGeorg Gottleuber */
2941dd864f6eSGeorg Gottleuber if (dmi_match(DMI_BOARD_NAME, "NS5X_NS7XAU") ||
2942dd864f6eSGeorg Gottleuber dmi_match(DMI_BOARD_NAME, "NS5x_7xAU") ||
2943dd864f6eSGeorg Gottleuber dmi_match(DMI_BOARD_NAME, "NS5x_7xPU") ||
2944dd864f6eSGeorg Gottleuber dmi_match(DMI_BOARD_NAME, "PH4PRX1_PH6PRX1"))
2945dd864f6eSGeorg Gottleuber return NVME_QUIRK_FORCE_NO_SIMPLE_SUSPEND;
294677ee2eaeSGeorg Gottleuber } else if (pdev->vendor == 0x144d && pdev->device == 0xa80d) {
294777ee2eaeSGeorg Gottleuber /*
294877ee2eaeSGeorg Gottleuber * Exclude Samsung 990 Evo from NVME_QUIRK_SIMPLE_SUSPEND
294977ee2eaeSGeorg Gottleuber * because of high power consumption (> 2 Watt) in s2idle
295077ee2eaeSGeorg Gottleuber * sleep. Only some boards with Intel CPU are affected.
295177ee2eaeSGeorg Gottleuber */
29529db27ba3SGeorg Gottleuber if (dmi_match(DMI_BOARD_NAME, "DN50Z-140HC-YD") ||
29539db27ba3SGeorg Gottleuber dmi_match(DMI_BOARD_NAME, "GMxPXxx") ||
295442385f9cSGeorg Gottleuber dmi_match(DMI_BOARD_NAME, "GXxMRXx") ||
295577ee2eaeSGeorg Gottleuber dmi_match(DMI_BOARD_NAME, "PH4PG31") ||
295677ee2eaeSGeorg Gottleuber dmi_match(DMI_BOARD_NAME, "PH4PRX1_PH6PRX1") ||
295777ee2eaeSGeorg Gottleuber dmi_match(DMI_BOARD_NAME, "PH6PG01_PH6PG71"))
295877ee2eaeSGeorg Gottleuber return NVME_QUIRK_FORCE_NO_SIMPLE_SUSPEND;
2959ff5350a8SAndy Lutomirski }
2960ff5350a8SAndy Lutomirski
2961a47b5484SWangYuli /*
2962a47b5484SWangYuli * NVMe SSD drops off the PCIe bus after system idle
2963a47b5484SWangYuli * for 10 hours on a Lenovo N60z board.
2964a47b5484SWangYuli */
2965a47b5484SWangYuli if (dmi_match(DMI_BOARD_NAME, "LXKT-ZXEG-N6"))
2966a47b5484SWangYuli return NVME_QUIRK_NO_APST;
2967a47b5484SWangYuli
2968ff5350a8SAndy Lutomirski return 0;
2969ff5350a8SAndy Lutomirski }
2970ff5350a8SAndy Lutomirski
nvme_pci_alloc_dev(struct pci_dev * pdev,const struct pci_device_id * id)29712e87570bSChristoph Hellwig static struct nvme_dev *nvme_pci_alloc_dev(struct pci_dev *pdev,
29722e87570bSChristoph Hellwig const struct pci_device_id *id)
297318119775SKeith Busch {
2974ff5350a8SAndy Lutomirski unsigned long quirks = id->driver_data;
29752e87570bSChristoph Hellwig int node = dev_to_node(&pdev->dev);
29762e87570bSChristoph Hellwig struct nvme_dev *dev;
29772e87570bSChristoph Hellwig int ret = -ENOMEM;
297857dacad5SJay Sternberg
297957dacad5SJay Sternberg dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
298057dacad5SJay Sternberg if (!dev)
2981dc785d69SIrvin Cote return ERR_PTR(-ENOMEM);
29822e87570bSChristoph Hellwig INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work);
29832e87570bSChristoph Hellwig mutex_init(&dev->shutdown_lock);
2984147b27e4SSagi Grimberg
29852a5bcfddSWeiping Zhang dev->nr_write_queues = write_queues;
29862a5bcfddSWeiping Zhang dev->nr_poll_queues = poll_queues;
29872a5bcfddSWeiping Zhang dev->nr_allocated_queues = nvme_max_io_queues(dev) + 1;
29882a5bcfddSWeiping Zhang dev->queues = kcalloc_node(dev->nr_allocated_queues,
29892a5bcfddSWeiping Zhang sizeof(struct nvme_queue), GFP_KERNEL, node);
299057dacad5SJay Sternberg if (!dev->queues)
29912e87570bSChristoph Hellwig goto out_free_dev;
299257dacad5SJay Sternberg
299357dacad5SJay Sternberg dev->dev = get_device(&pdev->dev);
2994f3ca80fcSChristoph Hellwig
29958427bbc2SKai-Heng Feng quirks |= check_vendor_combination_bug(pdev);
2996dd864f6eSGeorg Gottleuber if (!noacpi &&
2997dd864f6eSGeorg Gottleuber !(quirks & NVME_QUIRK_FORCE_NO_SIMPLE_SUSPEND) &&
2998dd864f6eSGeorg Gottleuber acpi_storage_d3(&pdev->dev)) {
2999df4f9bc4SDavid E. Box /*
3000df4f9bc4SDavid E. Box * Some systems use a bios work around to ask for D3 on
3001df4f9bc4SDavid E. Box * platforms that support kernel managed suspend.
3002df4f9bc4SDavid E. Box */
3003df4f9bc4SDavid E. Box dev_info(&pdev->dev,
3004df4f9bc4SDavid E. Box "platform quirk: setting simple suspend\n");
3005df4f9bc4SDavid E. Box quirks |= NVME_QUIRK_SIMPLE_SUSPEND;
3006df4f9bc4SDavid E. Box }
30072e87570bSChristoph Hellwig ret = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
30082e87570bSChristoph Hellwig quirks);
30092e87570bSChristoph Hellwig if (ret)
30102e87570bSChristoph Hellwig goto out_put_device;
30113f30a79cSChristoph Hellwig
3012924bd96eSChristoph Hellwig if (dev->ctrl.quirks & NVME_QUIRK_DMA_ADDRESS_BITS_48)
3013924bd96eSChristoph Hellwig dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48));
3014924bd96eSChristoph Hellwig else
3015924bd96eSChristoph Hellwig dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
30163f30a79cSChristoph Hellwig dma_set_min_align_mask(&pdev->dev, NVME_CTRL_PAGE_SIZE - 1);
30173f30a79cSChristoph Hellwig dma_set_max_seg_size(&pdev->dev, 0xffffffff);
3018df4f9bc4SDavid E. Box
3019943e942eSJens Axboe /*
30203f30a79cSChristoph Hellwig * Limit the max command size to prevent iod->sg allocations going
30213f30a79cSChristoph Hellwig * over a single page.
3022943e942eSJens Axboe */
30233f30a79cSChristoph Hellwig dev->ctrl.max_hw_sectors = min_t(u32,
30243710e2b0SAdrian Huang NVME_MAX_KB_SZ << 1, dma_opt_mapping_size(&pdev->dev) >> 9);
30253f30a79cSChristoph Hellwig dev->ctrl.max_segments = NVME_MAX_SEGS;
3026943e942eSJens Axboe
30273f30a79cSChristoph Hellwig /*
30283f30a79cSChristoph Hellwig * There is no support for SGLs for metadata (yet), so we are limited to
30293f30a79cSChristoph Hellwig * a single integrity segment for the separate metadata pointer.
30303f30a79cSChristoph Hellwig */
30313f30a79cSChristoph Hellwig dev->ctrl.max_integrity_segments = 1;
30322e87570bSChristoph Hellwig return dev;
30332e87570bSChristoph Hellwig
30342e87570bSChristoph Hellwig out_put_device:
30352e87570bSChristoph Hellwig put_device(dev->dev);
30362e87570bSChristoph Hellwig kfree(dev->queues);
30372e87570bSChristoph Hellwig out_free_dev:
30382e87570bSChristoph Hellwig kfree(dev);
30392e87570bSChristoph Hellwig return ERR_PTR(ret);
3040943e942eSJens Axboe }
3041943e942eSJens Axboe
nvme_probe(struct pci_dev * pdev,const struct pci_device_id * id)30422e87570bSChristoph Hellwig static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
30432e87570bSChristoph Hellwig {
30442e87570bSChristoph Hellwig struct nvme_dev *dev;
30452e87570bSChristoph Hellwig int result = -ENOMEM;
30462e87570bSChristoph Hellwig
30472e87570bSChristoph Hellwig dev = nvme_pci_alloc_dev(pdev, id);
3048dc785d69SIrvin Cote if (IS_ERR(dev))
3049dc785d69SIrvin Cote return PTR_ERR(dev);
30502e87570bSChristoph Hellwig
30512e87570bSChristoph Hellwig result = nvme_dev_map(dev);
3052b6e44b4cSKeith Busch if (result)
30532e87570bSChristoph Hellwig goto out_uninit_ctrl;
30542e87570bSChristoph Hellwig
30552e87570bSChristoph Hellwig result = nvme_setup_prp_pools(dev);
30562e87570bSChristoph Hellwig if (result)
30572e87570bSChristoph Hellwig goto out_dev_unmap;
305857dacad5SJay Sternberg
3059081a7d95SChristoph Hellwig result = nvme_pci_alloc_iod_mempool(dev);
3060081a7d95SChristoph Hellwig if (result)
30612e87570bSChristoph Hellwig goto out_release_prp_pools;
3062b6e44b4cSKeith Busch
306357dacad5SJay Sternberg dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev));
306457dacad5SJay Sternberg
3065eac3ef26SChristoph Hellwig result = nvme_pci_enable(dev);
3066eac3ef26SChristoph Hellwig if (result)
3067eac3ef26SChristoph Hellwig goto out_release_iod_mempool;
306857dacad5SJay Sternberg
30690da7feaaSChristoph Hellwig result = nvme_alloc_admin_tag_set(&dev->ctrl, &dev->admin_tagset,
30700da7feaaSChristoph Hellwig &nvme_mq_admin_ops, sizeof(struct nvme_iod));
3071eac3ef26SChristoph Hellwig if (result)
3072eac3ef26SChristoph Hellwig goto out_disable;
3073eac3ef26SChristoph Hellwig
3074eac3ef26SChristoph Hellwig /*
3075eac3ef26SChristoph Hellwig * Mark the controller as connecting before sending admin commands to
3076eac3ef26SChristoph Hellwig * allow the timeout handler to do the right thing.
3077eac3ef26SChristoph Hellwig */
3078eac3ef26SChristoph Hellwig if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_CONNECTING)) {
3079eac3ef26SChristoph Hellwig dev_warn(dev->ctrl.device,
3080eac3ef26SChristoph Hellwig "failed to mark controller CONNECTING\n");
3081eac3ef26SChristoph Hellwig result = -EBUSY;
3082eac3ef26SChristoph Hellwig goto out_disable;
3083eac3ef26SChristoph Hellwig }
3084eac3ef26SChristoph Hellwig
3085eac3ef26SChristoph Hellwig result = nvme_init_ctrl_finish(&dev->ctrl, false);
3086eac3ef26SChristoph Hellwig if (result)
3087eac3ef26SChristoph Hellwig goto out_disable;
3088eac3ef26SChristoph Hellwig
3089eac3ef26SChristoph Hellwig nvme_dbbuf_dma_alloc(dev);
3090eac3ef26SChristoph Hellwig
3091eac3ef26SChristoph Hellwig result = nvme_setup_host_mem(dev);
3092eac3ef26SChristoph Hellwig if (result < 0)
3093eac3ef26SChristoph Hellwig goto out_disable;
3094eac3ef26SChristoph Hellwig
3095eac3ef26SChristoph Hellwig result = nvme_setup_io_queues(dev);
3096eac3ef26SChristoph Hellwig if (result)
3097eac3ef26SChristoph Hellwig goto out_disable;
3098eac3ef26SChristoph Hellwig
3099eac3ef26SChristoph Hellwig if (dev->online_queues > 1) {
31000da7feaaSChristoph Hellwig nvme_alloc_io_tag_set(&dev->ctrl, &dev->tagset, &nvme_mq_ops,
31010da7feaaSChristoph Hellwig nvme_pci_nr_maps(dev), sizeof(struct nvme_iod));
3102eac3ef26SChristoph Hellwig nvme_dbbuf_set(dev);
3103eac3ef26SChristoph Hellwig }
3104eac3ef26SChristoph Hellwig
31050da7feaaSChristoph Hellwig if (!dev->ctrl.tagset)
31060da7feaaSChristoph Hellwig dev_warn(dev->ctrl.device, "IO queues not created\n");
31070da7feaaSChristoph Hellwig
3108eac3ef26SChristoph Hellwig if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) {
3109eac3ef26SChristoph Hellwig dev_warn(dev->ctrl.device,
3110eac3ef26SChristoph Hellwig "failed to mark controller live state\n");
3111eac3ef26SChristoph Hellwig result = -ENODEV;
3112eac3ef26SChristoph Hellwig goto out_disable;
3113eac3ef26SChristoph Hellwig }
3114eac3ef26SChristoph Hellwig
31152e87570bSChristoph Hellwig pci_set_drvdata(pdev, dev);
311657dacad5SJay Sternberg
3117eac3ef26SChristoph Hellwig nvme_start_ctrl(&dev->ctrl);
3118eac3ef26SChristoph Hellwig nvme_put_ctrl(&dev->ctrl);
31195a5754a4SKeith Busch flush_work(&dev->ctrl.scan_work);
312057dacad5SJay Sternberg return 0;
312157dacad5SJay Sternberg
3122eac3ef26SChristoph Hellwig out_disable:
3123eac3ef26SChristoph Hellwig nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
3124eac3ef26SChristoph Hellwig nvme_dev_disable(dev, true);
3125eac3ef26SChristoph Hellwig nvme_free_host_mem(dev);
3126eac3ef26SChristoph Hellwig nvme_dev_remove_admin(dev);
3127eac3ef26SChristoph Hellwig nvme_dbbuf_dma_free(dev);
3128eac3ef26SChristoph Hellwig nvme_free_queues(dev, 0);
3129eac3ef26SChristoph Hellwig out_release_iod_mempool:
3130b6e44b4cSKeith Busch mempool_destroy(dev->iod_mempool);
31312e87570bSChristoph Hellwig out_release_prp_pools:
313257dacad5SJay Sternberg nvme_release_prp_pools(dev);
31332e87570bSChristoph Hellwig out_dev_unmap:
313457dacad5SJay Sternberg nvme_dev_unmap(dev);
31352e87570bSChristoph Hellwig out_uninit_ctrl:
31362e87570bSChristoph Hellwig nvme_uninit_ctrl(&dev->ctrl);
3137a61d2655SIrvin Cote nvme_put_ctrl(&dev->ctrl);
313857dacad5SJay Sternberg return result;
313957dacad5SJay Sternberg }
314057dacad5SJay Sternberg
nvme_reset_prepare(struct pci_dev * pdev)3141775755edSChristoph Hellwig static void nvme_reset_prepare(struct pci_dev *pdev)
314257dacad5SJay Sternberg {
314357dacad5SJay Sternberg struct nvme_dev *dev = pci_get_drvdata(pdev);
3144c1ac9a4bSKeith Busch
3145c1ac9a4bSKeith Busch /*
3146c1ac9a4bSKeith Busch * We don't need to check the return value from waiting for the reset
3147c1ac9a4bSKeith Busch * state as pci_dev device lock is held, making it impossible to race
3148c1ac9a4bSKeith Busch * with ->remove().
3149c1ac9a4bSKeith Busch */
3150c1ac9a4bSKeith Busch nvme_disable_prepare_reset(dev, false);
3151c1ac9a4bSKeith Busch nvme_sync_queues(&dev->ctrl);
3152775755edSChristoph Hellwig }
315357dacad5SJay Sternberg
nvme_reset_done(struct pci_dev * pdev)3154775755edSChristoph Hellwig static void nvme_reset_done(struct pci_dev *pdev)
3155775755edSChristoph Hellwig {
3156f263fbb8SLinus Torvalds struct nvme_dev *dev = pci_get_drvdata(pdev);
3157c1ac9a4bSKeith Busch
3158c1ac9a4bSKeith Busch if (!nvme_try_sched_reset(&dev->ctrl))
3159c1ac9a4bSKeith Busch flush_work(&dev->ctrl.reset_work);
316057dacad5SJay Sternberg }
316157dacad5SJay Sternberg
nvme_shutdown(struct pci_dev * pdev)316257dacad5SJay Sternberg static void nvme_shutdown(struct pci_dev *pdev)
316357dacad5SJay Sternberg {
316457dacad5SJay Sternberg struct nvme_dev *dev = pci_get_drvdata(pdev);
31654e523547SBaolin Wang
3166c1ac9a4bSKeith Busch nvme_disable_prepare_reset(dev, true);
316757dacad5SJay Sternberg }
316857dacad5SJay Sternberg
3169f58944e2SKeith Busch /*
3170f58944e2SKeith Busch * The driver's remove may be called on a device in a partially initialized
3171f58944e2SKeith Busch * state. This function must not have any dependencies on the device state in
3172f58944e2SKeith Busch * order to proceed.
3173f58944e2SKeith Busch */
nvme_remove(struct pci_dev * pdev)317457dacad5SJay Sternberg static void nvme_remove(struct pci_dev *pdev)
317557dacad5SJay Sternberg {
317657dacad5SJay Sternberg struct nvme_dev *dev = pci_get_drvdata(pdev);
317757dacad5SJay Sternberg
3178bb8d261eSChristoph Hellwig nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
317957dacad5SJay Sternberg pci_set_drvdata(pdev, NULL);
31800ff9d4e1SKeith Busch
31816db28edaSKeith Busch if (!pci_device_is_present(pdev)) {
31820ff9d4e1SKeith Busch nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD);
31831d39e692SKeith Busch nvme_dev_disable(dev, true);
31846db28edaSKeith Busch }
31850ff9d4e1SKeith Busch
3186d86c4d8eSChristoph Hellwig flush_work(&dev->ctrl.reset_work);
3187d09f2b45SSagi Grimberg nvme_stop_ctrl(&dev->ctrl);
3188d09f2b45SSagi Grimberg nvme_remove_namespaces(&dev->ctrl);
3189a5cdb68cSKeith Busch nvme_dev_disable(dev, true);
319087ad72a5SChristoph Hellwig nvme_free_host_mem(dev);
319157dacad5SJay Sternberg nvme_dev_remove_admin(dev);
3192c11b7716SChristoph Hellwig nvme_dbbuf_dma_free(dev);
319357dacad5SJay Sternberg nvme_free_queues(dev, 0);
3194c11b7716SChristoph Hellwig mempool_destroy(dev->iod_mempool);
319557dacad5SJay Sternberg nvme_release_prp_pools(dev);
3196b00a726aSKeith Busch nvme_dev_unmap(dev);
3197726612b6SIsrael Rukshin nvme_uninit_ctrl(&dev->ctrl);
319857dacad5SJay Sternberg }
319957dacad5SJay Sternberg
320057dacad5SJay Sternberg #ifdef CONFIG_PM_SLEEP
nvme_get_power_state(struct nvme_ctrl * ctrl,u32 * ps)3201d916b1beSKeith Busch static int nvme_get_power_state(struct nvme_ctrl *ctrl, u32 *ps)
3202d916b1beSKeith Busch {
3203d916b1beSKeith Busch return nvme_get_features(ctrl, NVME_FEAT_POWER_MGMT, 0, NULL, 0, ps);
3204d916b1beSKeith Busch }
3205d916b1beSKeith Busch
nvme_set_power_state(struct nvme_ctrl * ctrl,u32 ps)3206d916b1beSKeith Busch static int nvme_set_power_state(struct nvme_ctrl *ctrl, u32 ps)
3207d916b1beSKeith Busch {
3208d916b1beSKeith Busch return nvme_set_features(ctrl, NVME_FEAT_POWER_MGMT, ps, NULL, 0, NULL);
3209d916b1beSKeith Busch }
3210d916b1beSKeith Busch
nvme_resume(struct device * dev)3211d916b1beSKeith Busch static int nvme_resume(struct device *dev)
3212d916b1beSKeith Busch {
3213d916b1beSKeith Busch struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev));
3214d916b1beSKeith Busch struct nvme_ctrl *ctrl = &ndev->ctrl;
3215d916b1beSKeith Busch
32164eaefe8cSRafael J. Wysocki if (ndev->last_ps == U32_MAX ||
3217d916b1beSKeith Busch nvme_set_power_state(ctrl, ndev->last_ps) != 0)
3218e5ad96f3SKeith Busch goto reset;
3219e5ad96f3SKeith Busch if (ctrl->hmpre && nvme_setup_host_mem(ndev))
3220e5ad96f3SKeith Busch goto reset;
3221e5ad96f3SKeith Busch
3222d916b1beSKeith Busch return 0;
3223e5ad96f3SKeith Busch reset:
3224e5ad96f3SKeith Busch return nvme_try_sched_reset(ctrl);
3225d916b1beSKeith Busch }
3226d916b1beSKeith Busch
nvme_suspend(struct device * dev)322757dacad5SJay Sternberg static int nvme_suspend(struct device *dev)
322857dacad5SJay Sternberg {
322957dacad5SJay Sternberg struct pci_dev *pdev = to_pci_dev(dev);
323057dacad5SJay Sternberg struct nvme_dev *ndev = pci_get_drvdata(pdev);
3231d916b1beSKeith Busch struct nvme_ctrl *ctrl = &ndev->ctrl;
3232d916b1beSKeith Busch int ret = -EBUSY;
3233d916b1beSKeith Busch
32344eaefe8cSRafael J. Wysocki ndev->last_ps = U32_MAX;
32354eaefe8cSRafael J. Wysocki
3236d916b1beSKeith Busch /*
3237d916b1beSKeith Busch * The platform does not remove power for a kernel managed suspend so
3238d916b1beSKeith Busch * use host managed nvme power settings for lowest idle power if
3239d916b1beSKeith Busch * possible. This should have quicker resume latency than a full device
3240d916b1beSKeith Busch * shutdown. But if the firmware is involved after the suspend or the
3241d916b1beSKeith Busch * device does not support any non-default power states, shut down the
3242d916b1beSKeith Busch * device fully.
32434eaefe8cSRafael J. Wysocki *
32444eaefe8cSRafael J. Wysocki * If ASPM is not enabled for the device, shut down the device and allow
32454eaefe8cSRafael J. Wysocki * the PCI bus layer to put it into D3 in order to take the PCIe link
32464eaefe8cSRafael J. Wysocki * down, so as to allow the platform to achieve its minimum low-power
32474eaefe8cSRafael J. Wysocki * state (which may not be possible if the link is up).
3248d916b1beSKeith Busch */
32494eaefe8cSRafael J. Wysocki if (pm_suspend_via_firmware() || !ctrl->npss ||
3250cb32de1bSMario Limonciello !pcie_aspm_enabled(pdev) ||
3251c1ac9a4bSKeith Busch (ndev->ctrl.quirks & NVME_QUIRK_SIMPLE_SUSPEND))
3252c1ac9a4bSKeith Busch return nvme_disable_prepare_reset(ndev, true);
3253d916b1beSKeith Busch
3254d916b1beSKeith Busch nvme_start_freeze(ctrl);
3255d916b1beSKeith Busch nvme_wait_freeze(ctrl);
3256d916b1beSKeith Busch nvme_sync_queues(ctrl);
3257d916b1beSKeith Busch
32588884a56dSKeith Busch if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE)
3259d916b1beSKeith Busch goto unfreeze;
3260d916b1beSKeith Busch
3261e5ad96f3SKeith Busch /*
3262e5ad96f3SKeith Busch * Host memory access may not be successful in a system suspend state,
3263e5ad96f3SKeith Busch * but the specification allows the controller to access memory in a
3264e5ad96f3SKeith Busch * non-operational power state.
3265e5ad96f3SKeith Busch */
3266e5ad96f3SKeith Busch if (ndev->hmb) {
3267e5ad96f3SKeith Busch ret = nvme_set_host_mem(ndev, 0);
3268e5ad96f3SKeith Busch if (ret < 0)
3269e5ad96f3SKeith Busch goto unfreeze;
3270e5ad96f3SKeith Busch }
3271e5ad96f3SKeith Busch
3272d916b1beSKeith Busch ret = nvme_get_power_state(ctrl, &ndev->last_ps);
3273d916b1beSKeith Busch if (ret < 0)
3274d916b1beSKeith Busch goto unfreeze;
3275d916b1beSKeith Busch
32767cbb5c6fSMario Limonciello /*
32777cbb5c6fSMario Limonciello * A saved state prevents pci pm from generically controlling the
32787cbb5c6fSMario Limonciello * device's power. If we're using protocol specific settings, we don't
32797cbb5c6fSMario Limonciello * want pci interfering.
32807cbb5c6fSMario Limonciello */
32817cbb5c6fSMario Limonciello pci_save_state(pdev);
32827cbb5c6fSMario Limonciello
3283d916b1beSKeith Busch ret = nvme_set_power_state(ctrl, ctrl->npss);
3284d916b1beSKeith Busch if (ret < 0)
3285d916b1beSKeith Busch goto unfreeze;
3286d916b1beSKeith Busch
3287d916b1beSKeith Busch if (ret) {
32887cbb5c6fSMario Limonciello /* discard the saved state */
32897cbb5c6fSMario Limonciello pci_load_saved_state(pdev, NULL);
32907cbb5c6fSMario Limonciello
3291d916b1beSKeith Busch /*
3292d916b1beSKeith Busch * Clearing npss forces a controller reset on resume. The
329305d3046fSGeert Uytterhoeven * correct value will be rediscovered then.
3294d916b1beSKeith Busch */
3295c1ac9a4bSKeith Busch ret = nvme_disable_prepare_reset(ndev, true);
3296d916b1beSKeith Busch ctrl->npss = 0;
3297d916b1beSKeith Busch }
3298d916b1beSKeith Busch unfreeze:
3299d916b1beSKeith Busch nvme_unfreeze(ctrl);
3300d916b1beSKeith Busch return ret;
3301d916b1beSKeith Busch }
3302d916b1beSKeith Busch
nvme_simple_suspend(struct device * dev)3303d916b1beSKeith Busch static int nvme_simple_suspend(struct device *dev)
3304d916b1beSKeith Busch {
3305d916b1beSKeith Busch struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev));
33064e523547SBaolin Wang
3307c1ac9a4bSKeith Busch return nvme_disable_prepare_reset(ndev, true);
330857dacad5SJay Sternberg }
330957dacad5SJay Sternberg
nvme_simple_resume(struct device * dev)3310d916b1beSKeith Busch static int nvme_simple_resume(struct device *dev)
331157dacad5SJay Sternberg {
331257dacad5SJay Sternberg struct pci_dev *pdev = to_pci_dev(dev);
331357dacad5SJay Sternberg struct nvme_dev *ndev = pci_get_drvdata(pdev);
331457dacad5SJay Sternberg
3315c1ac9a4bSKeith Busch return nvme_try_sched_reset(&ndev->ctrl);
331657dacad5SJay Sternberg }
331757dacad5SJay Sternberg
331821774222SYueHaibing static const struct dev_pm_ops nvme_dev_pm_ops = {
3319d916b1beSKeith Busch .suspend = nvme_suspend,
3320d916b1beSKeith Busch .resume = nvme_resume,
3321d916b1beSKeith Busch .freeze = nvme_simple_suspend,
3322d916b1beSKeith Busch .thaw = nvme_simple_resume,
3323d916b1beSKeith Busch .poweroff = nvme_simple_suspend,
3324d916b1beSKeith Busch .restore = nvme_simple_resume,
3325d916b1beSKeith Busch };
3326d916b1beSKeith Busch #endif /* CONFIG_PM_SLEEP */
332757dacad5SJay Sternberg
nvme_error_detected(struct pci_dev * pdev,pci_channel_state_t state)3328a0a3408eSKeith Busch static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
3329a0a3408eSKeith Busch pci_channel_state_t state)
3330a0a3408eSKeith Busch {
3331a0a3408eSKeith Busch struct nvme_dev *dev = pci_get_drvdata(pdev);
3332a0a3408eSKeith Busch
3333a0a3408eSKeith Busch /*
3334a0a3408eSKeith Busch * A frozen channel requires a reset. When detected, this method will
3335a0a3408eSKeith Busch * shutdown the controller to quiesce. The controller will be restarted
3336a0a3408eSKeith Busch * after the slot reset through driver's slot_reset callback.
3337a0a3408eSKeith Busch */
3338a0a3408eSKeith Busch switch (state) {
3339a0a3408eSKeith Busch case pci_channel_io_normal:
3340a0a3408eSKeith Busch return PCI_ERS_RESULT_CAN_RECOVER;
3341a0a3408eSKeith Busch case pci_channel_io_frozen:
3342d011fb31SKeith Busch dev_warn(dev->ctrl.device,
3343d011fb31SKeith Busch "frozen state error detected, reset controller\n");
334471a5bb15SKeith Busch if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING)) {
334571a5bb15SKeith Busch nvme_dev_disable(dev, true);
334671a5bb15SKeith Busch return PCI_ERS_RESULT_DISCONNECT;
334771a5bb15SKeith Busch }
3348a5cdb68cSKeith Busch nvme_dev_disable(dev, false);
3349a0a3408eSKeith Busch return PCI_ERS_RESULT_NEED_RESET;
3350a0a3408eSKeith Busch case pci_channel_io_perm_failure:
3351d011fb31SKeith Busch dev_warn(dev->ctrl.device,
3352d011fb31SKeith Busch "failure state error detected, request disconnect\n");
3353a0a3408eSKeith Busch return PCI_ERS_RESULT_DISCONNECT;
3354a0a3408eSKeith Busch }
3355a0a3408eSKeith Busch return PCI_ERS_RESULT_NEED_RESET;
3356a0a3408eSKeith Busch }
3357a0a3408eSKeith Busch
nvme_slot_reset(struct pci_dev * pdev)3358a0a3408eSKeith Busch static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev)
3359a0a3408eSKeith Busch {
3360a0a3408eSKeith Busch struct nvme_dev *dev = pci_get_drvdata(pdev);
3361a0a3408eSKeith Busch
33621b3c47c1SSagi Grimberg dev_info(dev->ctrl.device, "restart after slot reset\n");
3363a0a3408eSKeith Busch pci_restore_state(pdev);
336471a5bb15SKeith Busch if (!nvme_try_sched_reset(&dev->ctrl))
336571a5bb15SKeith Busch nvme_unquiesce_io_queues(&dev->ctrl);
3366a0a3408eSKeith Busch return PCI_ERS_RESULT_RECOVERED;
3367a0a3408eSKeith Busch }
3368a0a3408eSKeith Busch
nvme_error_resume(struct pci_dev * pdev)3369a0a3408eSKeith Busch static void nvme_error_resume(struct pci_dev *pdev)
3370a0a3408eSKeith Busch {
337172cd4cc2SKeith Busch struct nvme_dev *dev = pci_get_drvdata(pdev);
337272cd4cc2SKeith Busch
337372cd4cc2SKeith Busch flush_work(&dev->ctrl.reset_work);
3374a0a3408eSKeith Busch }
3375a0a3408eSKeith Busch
337657dacad5SJay Sternberg static const struct pci_error_handlers nvme_err_handler = {
337757dacad5SJay Sternberg .error_detected = nvme_error_detected,
337857dacad5SJay Sternberg .slot_reset = nvme_slot_reset,
337957dacad5SJay Sternberg .resume = nvme_error_resume,
3380775755edSChristoph Hellwig .reset_prepare = nvme_reset_prepare,
3381775755edSChristoph Hellwig .reset_done = nvme_reset_done,
338257dacad5SJay Sternberg };
338357dacad5SJay Sternberg
338457dacad5SJay Sternberg static const struct pci_device_id nvme_id_table[] = {
3385972b13e2SDavid Fugate { PCI_VDEVICE(INTEL, 0x0953), /* Intel 750/P3500/P3600/P3700 */
338608095e70SKeith Busch .driver_data = NVME_QUIRK_STRIPE_SIZE |
3387e850fd16SChristoph Hellwig NVME_QUIRK_DEALLOCATE_ZEROES, },
3388972b13e2SDavid Fugate { PCI_VDEVICE(INTEL, 0x0a53), /* Intel P3520 */
338999466e70SKeith Busch .driver_data = NVME_QUIRK_STRIPE_SIZE |
3390e850fd16SChristoph Hellwig NVME_QUIRK_DEALLOCATE_ZEROES, },
3391972b13e2SDavid Fugate { PCI_VDEVICE(INTEL, 0x0a54), /* Intel P4500/P4600 */
339299466e70SKeith Busch .driver_data = NVME_QUIRK_STRIPE_SIZE |
339325e58af4SWu Zheng NVME_QUIRK_DEALLOCATE_ZEROES |
33945c3f4066SKeith Busch NVME_QUIRK_IGNORE_DEV_SUBNQN |
33955c3f4066SKeith Busch NVME_QUIRK_BOGUS_NID, },
3396972b13e2SDavid Fugate { PCI_VDEVICE(INTEL, 0x0a55), /* Dell Express Flash P4600 */
3397f99cb7afSDavid Wayne Fugate .driver_data = NVME_QUIRK_STRIPE_SIZE |
3398f99cb7afSDavid Wayne Fugate NVME_QUIRK_DEALLOCATE_ZEROES, },
339950af47d0SAndy Lutomirski { PCI_VDEVICE(INTEL, 0xf1a5), /* Intel 600P/P3100 */
34009abd68efSJens Axboe .driver_data = NVME_QUIRK_NO_DEEPEST_PS |
34016c6aa2f2SAkinobu Mita NVME_QUIRK_MEDIUM_PRIO_SQ |
3402ce4cc313SDavid Milburn NVME_QUIRK_NO_TEMP_THRESH_CHANGE |
3403ce4cc313SDavid Milburn NVME_QUIRK_DISABLE_WRITE_ZEROES, },
34046299358dSJames Dingwall { PCI_VDEVICE(INTEL, 0xf1a6), /* Intel 760p/Pro 7600p */
34056299358dSJames Dingwall .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN, },
3406540c801cSKeith Busch { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
34077b210e4eSChristoph Hellwig .driver_data = NVME_QUIRK_IDENTIFY_CNS |
340866dd346bSChristoph Hellwig NVME_QUIRK_DISABLE_WRITE_ZEROES |
340966dd346bSChristoph Hellwig NVME_QUIRK_BOGUS_NID, },
341066dd346bSChristoph Hellwig { PCI_VDEVICE(REDHAT, 0x0010), /* Qemu emulated controller */
341166dd346bSChristoph Hellwig .driver_data = NVME_QUIRK_BOGUS_NID, },
3412ab51a98dSKeith Busch { PCI_DEVICE(0x1217, 0x8760), /* O2 Micro 64GB Steam Deck */
341334765438SRobert Beckett .driver_data = NVME_QUIRK_DMAPOOL_ALIGN_512, },
34145e11bacfSJiawei Fu (iBug) { PCI_DEVICE(0x126f, 0x2262), /* Silicon Motion generic */
34155e11bacfSJiawei Fu (iBug) .driver_data = NVME_QUIRK_NO_DEEPEST_PS |
34165e11bacfSJiawei Fu (iBug) NVME_QUIRK_BOGUS_NID, },
34175bedd3afSChristoph Hellwig { PCI_DEVICE(0x126f, 0x2263), /* Silicon Motion unidentified */
3418c98a8793SKeith Busch .driver_data = NVME_QUIRK_NO_NS_DESC_LIST |
3419c98a8793SKeith Busch NVME_QUIRK_BOGUS_NID, },
34200302ae60SMicah Parrish { PCI_DEVICE(0x1bb1, 0x0100), /* Seagate Nytro Flash Storage */
34215e112d3fSJulian Einwag .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY |
34225e112d3fSJulian Einwag NVME_QUIRK_NO_NS_DESC_LIST, },
342354adc010SGuilherme G. Piccoli { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */
342454adc010SGuilherme G. Piccoli .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
34258c97eeccSJeff Lien { PCI_DEVICE(0x1c58, 0x0023), /* WDC SN200 adapter */
34268c97eeccSJeff Lien .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
3427015282c9SWenbo Wang { PCI_DEVICE(0x1c5f, 0x0540), /* Memblaze Pblaze4 adapter */
3428015282c9SWenbo Wang .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
3429d554b5e1SMartin K. Petersen { PCI_DEVICE(0x144d, 0xa821), /* Samsung PM1725 */
3430d554b5e1SMartin K. Petersen .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
3431d554b5e1SMartin K. Petersen { PCI_DEVICE(0x144d, 0xa822), /* Samsung PM1725a */
34327ee5c78cSGopal Tiwari .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY |
3433abbb5f59SDmitry Monakhov NVME_QUIRK_DISABLE_WRITE_ZEROES|
34347ee5c78cSGopal Tiwari NVME_QUIRK_IGNORE_DEV_SUBNQN, },
3435a2da0e5cSSean Anderson { PCI_DEVICE(0x15b7, 0x5008), /* Sandisk SN530 */
3436a2da0e5cSSean Anderson .driver_data = NVME_QUIRK_BROKEN_MSI },
34372cf7a77eSKeith Busch { PCI_DEVICE(0x1987, 0x5012), /* Phison E12 */
34382cf7a77eSKeith Busch .driver_data = NVME_QUIRK_BOGUS_NID, },
3439c9e95c39SClaus Stovgaard { PCI_DEVICE(0x1987, 0x5016), /* Phison E16 */
344073029c9bSKeith Busch .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN |
344173029c9bSKeith Busch NVME_QUIRK_BOGUS_NID, },
3442d14c2731STina Hsu { PCI_DEVICE(0x1987, 0x5019), /* phison E19 */
3443d14c2731STina Hsu .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3444d14c2731STina Hsu { PCI_DEVICE(0x1987, 0x5021), /* Phison E21 */
3445d14c2731STina Hsu .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
34466e6a6828SPascal Terjan { PCI_DEVICE(0x1b4b, 0x1092), /* Lexar 256 GB SSD */
34476e6a6828SPascal Terjan .driver_data = NVME_QUIRK_NO_NS_DESC_LIST |
34486e6a6828SPascal Terjan NVME_QUIRK_IGNORE_DEV_SUBNQN, },
3449e1c70d79SLamarque Vieira Souza { PCI_DEVICE(0x1cc1, 0x33f8), /* ADATA IM2P33F8ABR1 1 TB */
3450e1c70d79SLamarque Vieira Souza .driver_data = NVME_QUIRK_BOGUS_NID, },
345108b903b5SMisha Nasledov { PCI_DEVICE(0x10ec, 0x5762), /* ADATA SX6000LNP */
34521629de0eSPablo Greco .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN |
34531629de0eSPablo Greco NVME_QUIRK_BOGUS_NID, },
34545f69f009SDaniel Wagner { PCI_DEVICE(0x10ec, 0x5763), /* ADATA SX6000PNP */
34555f69f009SDaniel Wagner .driver_data = NVME_QUIRK_BOGUS_NID, },
3456f03e42c6SGabriel Craciunescu { PCI_DEVICE(0x1cc1, 0x8201), /* ADATA SX8200PNP 512GB */
3457f03e42c6SGabriel Craciunescu .driver_data = NVME_QUIRK_NO_DEEPEST_PS |
3458f03e42c6SGabriel Craciunescu NVME_QUIRK_IGNORE_DEV_SUBNQN, },
345941f38043SLeo Savernik { PCI_DEVICE(0x1344, 0x5407), /* Micron Technology Inc NVMe SSD */
346041f38043SLeo Savernik .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN },
3461d5ceb4d1SBean Huo { PCI_DEVICE(0x1344, 0x6001), /* Micron Nitro NVMe */
3462d5ceb4d1SBean Huo .driver_data = NVME_QUIRK_BOGUS_NID, },
34635611ec2bSKai-Heng Feng { PCI_DEVICE(0x1c5c, 0x1504), /* SK Hynix PC400 */
34645611ec2bSKai-Heng Feng .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3465c4f01a77SKeith Busch { PCI_DEVICE(0x1c5c, 0x174a), /* SK Hynix P31 SSD */
3466c4f01a77SKeith Busch .driver_data = NVME_QUIRK_BOGUS_NID, },
346702ca079cSKai-Heng Feng { PCI_DEVICE(0x15b7, 0x2001), /* Sandisk Skyhawk */
346802ca079cSKai-Heng Feng .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
346989919929SChaitanya Kulkarni { PCI_DEVICE(0x1d97, 0x2263), /* SPCC */
347089919929SChaitanya Kulkarni .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
347143047e08Srasheed.hsueh { PCI_DEVICE(0x144d, 0xa80b), /* Samsung PM9B1 256G and 512G */
3472688b419cSAugust Wikerfors .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES |
3473688b419cSAugust Wikerfors NVME_QUIRK_BOGUS_NID, },
347443047e08Srasheed.hsueh { PCI_DEVICE(0x144d, 0xa809), /* Samsung MZALQ256HBJD 256G */
347543047e08Srasheed.hsueh .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3476e5bb0988SPankaj Raghav { PCI_DEVICE(0x144d, 0xa802), /* Samsung SM953 */
3477e5bb0988SPankaj Raghav .driver_data = NVME_QUIRK_BOGUS_NID, },
347843047e08Srasheed.hsueh { PCI_DEVICE(0x1cc4, 0x6303), /* UMIS RPJTJ512MGE1QDY 512G */
347943047e08Srasheed.hsueh .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
348043047e08Srasheed.hsueh { PCI_DEVICE(0x1cc4, 0x6302), /* UMIS RPJTJ256MGE1QDY 256G */
348143047e08Srasheed.hsueh .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3482dc22c1c0SZoltán Böszörményi { PCI_DEVICE(0x2646, 0x2262), /* KINGSTON SKC2000 NVMe SSD */
3483dc22c1c0SZoltán Böszörményi .driver_data = NVME_QUIRK_NO_DEEPEST_PS, },
3484538e4a8cSThorsten Leemhuis { PCI_DEVICE(0x2646, 0x2263), /* KINGSTON A2000 NVMe SSD */
3485538e4a8cSThorsten Leemhuis .driver_data = NVME_QUIRK_NO_DEEPEST_PS, },
3486bd375feeSHristo Venev { PCI_DEVICE(0x2646, 0x5013), /* Kingston KC3000, Kingston FURY Renegade */
3487bd375feeSHristo Venev .driver_data = NVME_QUIRK_NO_SECONDARY_TEMP_THRESH, },
3488ac9b57d4SXander Li { PCI_DEVICE(0x2646, 0x5018), /* KINGSTON OM8SFP4xxxxP OS21012 NVMe SSD */
3489ac9b57d4SXander Li .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3490ac9b57d4SXander Li { PCI_DEVICE(0x2646, 0x5016), /* KINGSTON OM3PGP4xxxxP OS21011 NVMe SSD */
3491ac9b57d4SXander Li .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3492ac9b57d4SXander Li { PCI_DEVICE(0x2646, 0x501A), /* KINGSTON OM8PGP4xxxxP OS21005 NVMe SSD */
3493ac9b57d4SXander Li .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3494ac9b57d4SXander Li { PCI_DEVICE(0x2646, 0x501B), /* KINGSTON OM8PGP4xxxxQ OS21005 NVMe SSD */
3495ac9b57d4SXander Li .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3496ac9b57d4SXander Li { PCI_DEVICE(0x2646, 0x501E), /* KINGSTON OM3PGP4xxxxQ OS21011 NVMe SSD */
3497ac9b57d4SXander Li .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
34989630d806SElmer Miroslav Mosher Golovin { PCI_DEVICE(0x1f40, 0x1202), /* Netac Technologies Co. NV3000 NVMe SSD */
34999630d806SElmer Miroslav Mosher Golovin .driver_data = NVME_QUIRK_BOGUS_NID, },
35008d6e38f6STiago Dias Ferreira { PCI_DEVICE(0x1f40, 0x5236), /* Netac Technologies Co. NV7000 NVMe SSD */
35018d6e38f6STiago Dias Ferreira .driver_data = NVME_QUIRK_BOGUS_NID, },
350270ce3455SChristoph Hellwig { PCI_DEVICE(0x1e4B, 0x1001), /* MAXIO MAP1001 */
350370ce3455SChristoph Hellwig .driver_data = NVME_QUIRK_BOGUS_NID, },
3504a98a945bSChristoph Hellwig { PCI_DEVICE(0x1e4B, 0x1002), /* MAXIO MAP1002 */
3505a98a945bSChristoph Hellwig .driver_data = NVME_QUIRK_BOGUS_NID, },
3506a98a945bSChristoph Hellwig { PCI_DEVICE(0x1e4B, 0x1202), /* MAXIO MAP1202 */
3507a98a945bSChristoph Hellwig .driver_data = NVME_QUIRK_BOGUS_NID, },
3508a3a9d63dSTatsuki Sugiura { PCI_DEVICE(0x1e4B, 0x1602), /* MAXIO MAP1602 */
3509a3a9d63dSTatsuki Sugiura .driver_data = NVME_QUIRK_BOGUS_NID, },
35103765fad5SStefan Reiter { PCI_DEVICE(0x1cc1, 0x5350), /* ADATA XPG GAMMIX S50 */
35113765fad5SStefan Reiter .driver_data = NVME_QUIRK_BOGUS_NID, },
3512*aca3cdc3SChristopher Lentocha { PCI_DEVICE(0x1dbe, 0x5216), /* Acer/INNOGRIT FA100/5216 NVMe SSD */
3513*aca3cdc3SChristopher Lentocha .driver_data = NVME_QUIRK_BOGUS_NID, },
3514f37527a0SDennis P. Kliem { PCI_DEVICE(0x1dbe, 0x5236), /* ADATA XPG GAMMIX S70 */
3515f37527a0SDennis P. Kliem .driver_data = NVME_QUIRK_BOGUS_NID, },
3516d5d3c100SXi Ruoyao { PCI_DEVICE(0x1e49, 0x0021), /* ZHITAI TiPro5000 NVMe SSD */
3517d5d3c100SXi Ruoyao .driver_data = NVME_QUIRK_NO_DEEPEST_PS, },
35186b961bceSNing Wang { PCI_DEVICE(0x1e49, 0x0041), /* ZHITAI TiPro7000 NVMe SSD */
35196b961bceSNing Wang .driver_data = NVME_QUIRK_NO_DEEPEST_PS, },
3520d6c52fa3STobias Gruetzmacher { PCI_DEVICE(0xc0a9, 0x540a), /* Crucial P2 */
3521d6c52fa3STobias Gruetzmacher .driver_data = NVME_QUIRK_BOGUS_NID, },
3522200dccd0SShyamin Ayesh { PCI_DEVICE(0x1d97, 0x2263), /* Lexar NM610 */
3523200dccd0SShyamin Ayesh .driver_data = NVME_QUIRK_BOGUS_NID, },
3524b65d44faSPhilipp Geulen { PCI_DEVICE(0x1d97, 0x1d97), /* Lexar NM620 */
3525b65d44faSPhilipp Geulen .driver_data = NVME_QUIRK_BOGUS_NID, },
352680b26240SAbhijit { PCI_DEVICE(0x1d97, 0x2269), /* Lexar NM760 */
35271231363aSJuraj Pecigos .driver_data = NVME_QUIRK_BOGUS_NID |
35281231363aSJuraj Pecigos NVME_QUIRK_IGNORE_DEV_SUBNQN, },
352974391b3eSDuy Truong { PCI_DEVICE(0x10ec, 0x5763), /* TEAMGROUP T-FORCE CARDEA ZERO Z330 SSD */
353074391b3eSDuy Truong .driver_data = NVME_QUIRK_BOGUS_NID, },
35311616d6c3SSagi Grimberg { PCI_DEVICE(0x1e4b, 0x1602), /* HS-SSD-FUTURE 2048G */
35321616d6c3SSagi Grimberg .driver_data = NVME_QUIRK_BOGUS_NID, },
353306497281SDaniel Smith { PCI_DEVICE(0x10ec, 0x5765), /* TEAMGROUP MP33 2TB SSD */
353406497281SDaniel Smith .driver_data = NVME_QUIRK_BOGUS_NID, },
35354bdf2603SFilippo Sironi { PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0x0061),
35364bdf2603SFilippo Sironi .driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, },
35374bdf2603SFilippo Sironi { PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0x0065),
35384bdf2603SFilippo Sironi .driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, },
35394bdf2603SFilippo Sironi { PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0x8061),
35404bdf2603SFilippo Sironi .driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, },
35414bdf2603SFilippo Sironi { PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0xcd00),
35424bdf2603SFilippo Sironi .driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, },
35434bdf2603SFilippo Sironi { PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0xcd01),
35444bdf2603SFilippo Sironi .driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, },
35454bdf2603SFilippo Sironi { PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0xcd02),
35464bdf2603SFilippo Sironi .driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, },
354798f7b86aSAndy Shevchenko { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001),
3548ab51a98dSKeith Busch /*
3549ab51a98dSKeith Busch * Fix for the Apple controller found in the MacBook8,1 and
3550ab51a98dSKeith Busch * some MacBook7,1 to avoid controller resets and data loss.
3551ab51a98dSKeith Busch */
3552ab51a98dSKeith Busch .driver_data = NVME_QUIRK_SINGLE_VECTOR |
3553ab51a98dSKeith Busch NVME_QUIRK_QDEPTH_ONE },
3554124298bdSDaniel Roschka { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) },
355566341331SBenjamin Herrenschmidt { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2005),
355666341331SBenjamin Herrenschmidt .driver_data = NVME_QUIRK_SINGLE_VECTOR |
3557d38e9f04SBenjamin Herrenschmidt NVME_QUIRK_128_BYTES_SQES |
3558a2941f6aSKeith Busch NVME_QUIRK_SHARED_TAGS |
3559453116a4SHector Martin NVME_QUIRK_SKIP_CID_GEN |
3560453116a4SHector Martin NVME_QUIRK_IDENTIFY_CNS },
35610b85f59dSAndy Shevchenko { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
356257dacad5SJay Sternberg { 0, }
356357dacad5SJay Sternberg };
356457dacad5SJay Sternberg MODULE_DEVICE_TABLE(pci, nvme_id_table);
356557dacad5SJay Sternberg
356657dacad5SJay Sternberg static struct pci_driver nvme_driver = {
356757dacad5SJay Sternberg .name = "nvme",
356857dacad5SJay Sternberg .id_table = nvme_id_table,
356957dacad5SJay Sternberg .probe = nvme_probe,
357057dacad5SJay Sternberg .remove = nvme_remove,
357157dacad5SJay Sternberg .shutdown = nvme_shutdown,
357257dacad5SJay Sternberg .driver = {
3573eac3ef26SChristoph Hellwig .probe_type = PROBE_PREFER_ASYNCHRONOUS,
3574eac3ef26SChristoph Hellwig #ifdef CONFIG_PM_SLEEP
357557dacad5SJay Sternberg .pm = &nvme_dev_pm_ops,
3576d916b1beSKeith Busch #endif
3577eac3ef26SChristoph Hellwig },
357874d986abSAlexander Duyck .sriov_configure = pci_sriov_configure_simple,
357957dacad5SJay Sternberg .err_handler = &nvme_err_handler,
358057dacad5SJay Sternberg };
358157dacad5SJay Sternberg
nvme_init(void)358257dacad5SJay Sternberg static int __init nvme_init(void)
358357dacad5SJay Sternberg {
358481101540SChristoph Hellwig BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
358581101540SChristoph Hellwig BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
358681101540SChristoph Hellwig BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
3587612b7286SMing Lei BUILD_BUG_ON(IRQ_AFFINITY_MAX_SETS < 2);
358801df742dSKeith Busch BUILD_BUG_ON(NVME_MAX_SEGS > SGES_PER_PAGE);
35897846c1b5SKeith Busch BUILD_BUG_ON(sizeof(struct scatterlist) * NVME_MAX_SEGS > PAGE_SIZE);
35907846c1b5SKeith Busch BUILD_BUG_ON(nvme_pci_npages_prp() > NVME_MAX_NR_ALLOCATIONS);
359117c33167SKeith Busch
35929a6327d2SSagi Grimberg return pci_register_driver(&nvme_driver);
359357dacad5SJay Sternberg }
359457dacad5SJay Sternberg
nvme_exit(void)359557dacad5SJay Sternberg static void __exit nvme_exit(void)
359657dacad5SJay Sternberg {
359757dacad5SJay Sternberg pci_unregister_driver(&nvme_driver);
359803e0f3a6SMing Lei flush_workqueue(nvme_wq);
359957dacad5SJay Sternberg }
360057dacad5SJay Sternberg
360157dacad5SJay Sternberg MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
360257dacad5SJay Sternberg MODULE_LICENSE("GPL");
360357dacad5SJay Sternberg MODULE_VERSION("1.0");
360457dacad5SJay Sternberg module_init(nvme_init);
360557dacad5SJay Sternberg module_exit(nvme_exit);
3606