xref: /openbmc/linux/drivers/nvme/host/pci.c (revision 4d75f5c664195b970e1cd2fd25b65b5eff257a0a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NVM Express device driver
4  * Copyright (c) 2011-2014, Intel Corporation.
5  */
6 
7 #include <linux/acpi.h>
8 #include <linux/async.h>
9 #include <linux/blkdev.h>
10 #include <linux/blk-mq.h>
11 #include <linux/blk-mq-pci.h>
12 #include <linux/blk-integrity.h>
13 #include <linux/dmi.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/kstrtox.h>
18 #include <linux/memremap.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/once.h>
23 #include <linux/pci.h>
24 #include <linux/suspend.h>
25 #include <linux/t10-pi.h>
26 #include <linux/types.h>
27 #include <linux/io-64-nonatomic-lo-hi.h>
28 #include <linux/io-64-nonatomic-hi-lo.h>
29 #include <linux/sed-opal.h>
30 #include <linux/pci-p2pdma.h>
31 
32 #include "trace.h"
33 #include "nvme.h"
34 
35 #define SQ_SIZE(q)	((q)->q_depth << (q)->sqes)
36 #define CQ_SIZE(q)	((q)->q_depth * sizeof(struct nvme_completion))
37 
38 #define SGES_PER_PAGE	(NVME_CTRL_PAGE_SIZE / sizeof(struct nvme_sgl_desc))
39 
40 /*
41  * These can be higher, but we need to ensure that any command doesn't
42  * require an sg allocation that needs more than a page of data.
43  */
44 #define NVME_MAX_KB_SZ	8192
45 #define NVME_MAX_SEGS	128
46 #define NVME_MAX_NR_ALLOCATIONS	5
47 
48 static int use_threaded_interrupts;
49 module_param(use_threaded_interrupts, int, 0444);
50 
51 static bool use_cmb_sqes = true;
52 module_param(use_cmb_sqes, bool, 0444);
53 MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
54 
55 static unsigned int max_host_mem_size_mb = 128;
56 module_param(max_host_mem_size_mb, uint, 0444);
57 MODULE_PARM_DESC(max_host_mem_size_mb,
58 	"Maximum Host Memory Buffer (HMB) size per controller (in MiB)");
59 
60 static unsigned int sgl_threshold = SZ_32K;
61 module_param(sgl_threshold, uint, 0644);
62 MODULE_PARM_DESC(sgl_threshold,
63 		"Use SGLs when average request segment size is larger or equal to "
64 		"this size. Use 0 to disable SGLs.");
65 
66 #define NVME_PCI_MIN_QUEUE_SIZE 2
67 #define NVME_PCI_MAX_QUEUE_SIZE 4095
68 static int io_queue_depth_set(const char *val, const struct kernel_param *kp);
69 static const struct kernel_param_ops io_queue_depth_ops = {
70 	.set = io_queue_depth_set,
71 	.get = param_get_uint,
72 };
73 
74 static unsigned int io_queue_depth = 1024;
75 module_param_cb(io_queue_depth, &io_queue_depth_ops, &io_queue_depth, 0644);
76 MODULE_PARM_DESC(io_queue_depth, "set io queue depth, should >= 2 and < 4096");
77 
io_queue_count_set(const char * val,const struct kernel_param * kp)78 static int io_queue_count_set(const char *val, const struct kernel_param *kp)
79 {
80 	unsigned int n;
81 	int ret;
82 
83 	ret = kstrtouint(val, 10, &n);
84 	if (ret != 0 || n > num_possible_cpus())
85 		return -EINVAL;
86 	return param_set_uint(val, kp);
87 }
88 
89 static const struct kernel_param_ops io_queue_count_ops = {
90 	.set = io_queue_count_set,
91 	.get = param_get_uint,
92 };
93 
94 static unsigned int write_queues;
95 module_param_cb(write_queues, &io_queue_count_ops, &write_queues, 0644);
96 MODULE_PARM_DESC(write_queues,
97 	"Number of queues to use for writes. If not set, reads and writes "
98 	"will share a queue set.");
99 
100 static unsigned int poll_queues;
101 module_param_cb(poll_queues, &io_queue_count_ops, &poll_queues, 0644);
102 MODULE_PARM_DESC(poll_queues, "Number of queues to use for polled IO.");
103 
104 static bool noacpi;
105 module_param(noacpi, bool, 0444);
106 MODULE_PARM_DESC(noacpi, "disable acpi bios quirks");
107 
108 struct nvme_dev;
109 struct nvme_queue;
110 
111 static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown);
112 static void nvme_delete_io_queues(struct nvme_dev *dev);
113 static void nvme_update_attrs(struct nvme_dev *dev);
114 
115 /*
116  * Represents an NVM Express device.  Each nvme_dev is a PCI function.
117  */
118 struct nvme_dev {
119 	struct nvme_queue *queues;
120 	struct blk_mq_tag_set tagset;
121 	struct blk_mq_tag_set admin_tagset;
122 	u32 __iomem *dbs;
123 	struct device *dev;
124 	struct dma_pool *prp_page_pool;
125 	struct dma_pool *prp_small_pool;
126 	unsigned online_queues;
127 	unsigned max_qid;
128 	unsigned io_queues[HCTX_MAX_TYPES];
129 	unsigned int num_vecs;
130 	u32 q_depth;
131 	int io_sqes;
132 	u32 db_stride;
133 	void __iomem *bar;
134 	unsigned long bar_mapped_size;
135 	struct mutex shutdown_lock;
136 	bool subsystem;
137 	u64 cmb_size;
138 	bool cmb_use_sqes;
139 	u32 cmbsz;
140 	u32 cmbloc;
141 	struct nvme_ctrl ctrl;
142 	u32 last_ps;
143 	bool hmb;
144 
145 	mempool_t *iod_mempool;
146 
147 	/* shadow doorbell buffer support: */
148 	__le32 *dbbuf_dbs;
149 	dma_addr_t dbbuf_dbs_dma_addr;
150 	__le32 *dbbuf_eis;
151 	dma_addr_t dbbuf_eis_dma_addr;
152 
153 	/* host memory buffer support: */
154 	u64 host_mem_size;
155 	u32 nr_host_mem_descs;
156 	u32 host_mem_descs_size;
157 	dma_addr_t host_mem_descs_dma;
158 	struct nvme_host_mem_buf_desc *host_mem_descs;
159 	void **host_mem_desc_bufs;
160 	unsigned int nr_allocated_queues;
161 	unsigned int nr_write_queues;
162 	unsigned int nr_poll_queues;
163 };
164 
io_queue_depth_set(const char * val,const struct kernel_param * kp)165 static int io_queue_depth_set(const char *val, const struct kernel_param *kp)
166 {
167 	return param_set_uint_minmax(val, kp, NVME_PCI_MIN_QUEUE_SIZE,
168 			NVME_PCI_MAX_QUEUE_SIZE);
169 }
170 
sq_idx(unsigned int qid,u32 stride)171 static inline unsigned int sq_idx(unsigned int qid, u32 stride)
172 {
173 	return qid * 2 * stride;
174 }
175 
cq_idx(unsigned int qid,u32 stride)176 static inline unsigned int cq_idx(unsigned int qid, u32 stride)
177 {
178 	return (qid * 2 + 1) * stride;
179 }
180 
to_nvme_dev(struct nvme_ctrl * ctrl)181 static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
182 {
183 	return container_of(ctrl, struct nvme_dev, ctrl);
184 }
185 
186 /*
187  * An NVM Express queue.  Each device has at least two (one for admin
188  * commands and one for I/O commands).
189  */
190 struct nvme_queue {
191 	struct nvme_dev *dev;
192 	spinlock_t sq_lock;
193 	void *sq_cmds;
194 	 /* only used for poll queues: */
195 	spinlock_t cq_poll_lock ____cacheline_aligned_in_smp;
196 	struct nvme_completion *cqes;
197 	dma_addr_t sq_dma_addr;
198 	dma_addr_t cq_dma_addr;
199 	u32 __iomem *q_db;
200 	u32 q_depth;
201 	u16 cq_vector;
202 	u16 sq_tail;
203 	u16 last_sq_tail;
204 	u16 cq_head;
205 	u16 qid;
206 	u8 cq_phase;
207 	u8 sqes;
208 	unsigned long flags;
209 #define NVMEQ_ENABLED		0
210 #define NVMEQ_SQ_CMB		1
211 #define NVMEQ_DELETE_ERROR	2
212 #define NVMEQ_POLLED		3
213 	__le32 *dbbuf_sq_db;
214 	__le32 *dbbuf_cq_db;
215 	__le32 *dbbuf_sq_ei;
216 	__le32 *dbbuf_cq_ei;
217 	struct completion delete_done;
218 };
219 
220 union nvme_descriptor {
221 	struct nvme_sgl_desc	*sg_list;
222 	__le64			*prp_list;
223 };
224 
225 /*
226  * The nvme_iod describes the data in an I/O.
227  *
228  * The sg pointer contains the list of PRP/SGL chunk allocations in addition
229  * to the actual struct scatterlist.
230  */
231 struct nvme_iod {
232 	struct nvme_request req;
233 	struct nvme_command cmd;
234 	bool aborted;
235 	s8 nr_allocations;	/* PRP list pool allocations. 0 means small
236 				   pool in use */
237 	unsigned int dma_len;	/* length of single DMA segment mapping */
238 	dma_addr_t first_dma;
239 	dma_addr_t meta_dma;
240 	struct sg_table sgt;
241 	union nvme_descriptor list[NVME_MAX_NR_ALLOCATIONS];
242 };
243 
nvme_dbbuf_size(struct nvme_dev * dev)244 static inline unsigned int nvme_dbbuf_size(struct nvme_dev *dev)
245 {
246 	return dev->nr_allocated_queues * 8 * dev->db_stride;
247 }
248 
nvme_dbbuf_dma_alloc(struct nvme_dev * dev)249 static void nvme_dbbuf_dma_alloc(struct nvme_dev *dev)
250 {
251 	unsigned int mem_size = nvme_dbbuf_size(dev);
252 
253 	if (!(dev->ctrl.oacs & NVME_CTRL_OACS_DBBUF_SUPP))
254 		return;
255 
256 	if (dev->dbbuf_dbs) {
257 		/*
258 		 * Clear the dbbuf memory so the driver doesn't observe stale
259 		 * values from the previous instantiation.
260 		 */
261 		memset(dev->dbbuf_dbs, 0, mem_size);
262 		memset(dev->dbbuf_eis, 0, mem_size);
263 		return;
264 	}
265 
266 	dev->dbbuf_dbs = dma_alloc_coherent(dev->dev, mem_size,
267 					    &dev->dbbuf_dbs_dma_addr,
268 					    GFP_KERNEL);
269 	if (!dev->dbbuf_dbs)
270 		goto fail;
271 	dev->dbbuf_eis = dma_alloc_coherent(dev->dev, mem_size,
272 					    &dev->dbbuf_eis_dma_addr,
273 					    GFP_KERNEL);
274 	if (!dev->dbbuf_eis)
275 		goto fail_free_dbbuf_dbs;
276 	return;
277 
278 fail_free_dbbuf_dbs:
279 	dma_free_coherent(dev->dev, mem_size, dev->dbbuf_dbs,
280 			  dev->dbbuf_dbs_dma_addr);
281 	dev->dbbuf_dbs = NULL;
282 fail:
283 	dev_warn(dev->dev, "unable to allocate dma for dbbuf\n");
284 }
285 
nvme_dbbuf_dma_free(struct nvme_dev * dev)286 static void nvme_dbbuf_dma_free(struct nvme_dev *dev)
287 {
288 	unsigned int mem_size = nvme_dbbuf_size(dev);
289 
290 	if (dev->dbbuf_dbs) {
291 		dma_free_coherent(dev->dev, mem_size,
292 				  dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
293 		dev->dbbuf_dbs = NULL;
294 	}
295 	if (dev->dbbuf_eis) {
296 		dma_free_coherent(dev->dev, mem_size,
297 				  dev->dbbuf_eis, dev->dbbuf_eis_dma_addr);
298 		dev->dbbuf_eis = NULL;
299 	}
300 }
301 
nvme_dbbuf_init(struct nvme_dev * dev,struct nvme_queue * nvmeq,int qid)302 static void nvme_dbbuf_init(struct nvme_dev *dev,
303 			    struct nvme_queue *nvmeq, int qid)
304 {
305 	if (!dev->dbbuf_dbs || !qid)
306 		return;
307 
308 	nvmeq->dbbuf_sq_db = &dev->dbbuf_dbs[sq_idx(qid, dev->db_stride)];
309 	nvmeq->dbbuf_cq_db = &dev->dbbuf_dbs[cq_idx(qid, dev->db_stride)];
310 	nvmeq->dbbuf_sq_ei = &dev->dbbuf_eis[sq_idx(qid, dev->db_stride)];
311 	nvmeq->dbbuf_cq_ei = &dev->dbbuf_eis[cq_idx(qid, dev->db_stride)];
312 }
313 
nvme_dbbuf_free(struct nvme_queue * nvmeq)314 static void nvme_dbbuf_free(struct nvme_queue *nvmeq)
315 {
316 	if (!nvmeq->qid)
317 		return;
318 
319 	nvmeq->dbbuf_sq_db = NULL;
320 	nvmeq->dbbuf_cq_db = NULL;
321 	nvmeq->dbbuf_sq_ei = NULL;
322 	nvmeq->dbbuf_cq_ei = NULL;
323 }
324 
nvme_dbbuf_set(struct nvme_dev * dev)325 static void nvme_dbbuf_set(struct nvme_dev *dev)
326 {
327 	struct nvme_command c = { };
328 	unsigned int i;
329 
330 	if (!dev->dbbuf_dbs)
331 		return;
332 
333 	c.dbbuf.opcode = nvme_admin_dbbuf;
334 	c.dbbuf.prp1 = cpu_to_le64(dev->dbbuf_dbs_dma_addr);
335 	c.dbbuf.prp2 = cpu_to_le64(dev->dbbuf_eis_dma_addr);
336 
337 	if (nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0)) {
338 		dev_warn(dev->ctrl.device, "unable to set dbbuf\n");
339 		/* Free memory and continue on */
340 		nvme_dbbuf_dma_free(dev);
341 
342 		for (i = 1; i <= dev->online_queues; i++)
343 			nvme_dbbuf_free(&dev->queues[i]);
344 	}
345 }
346 
nvme_dbbuf_need_event(u16 event_idx,u16 new_idx,u16 old)347 static inline int nvme_dbbuf_need_event(u16 event_idx, u16 new_idx, u16 old)
348 {
349 	return (u16)(new_idx - event_idx - 1) < (u16)(new_idx - old);
350 }
351 
352 /* 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)353 static bool nvme_dbbuf_update_and_check_event(u16 value, __le32 *dbbuf_db,
354 					      volatile __le32 *dbbuf_ei)
355 {
356 	if (dbbuf_db) {
357 		u16 old_value, event_idx;
358 
359 		/*
360 		 * Ensure that the queue is written before updating
361 		 * the doorbell in memory
362 		 */
363 		wmb();
364 
365 		old_value = le32_to_cpu(*dbbuf_db);
366 		*dbbuf_db = cpu_to_le32(value);
367 
368 		/*
369 		 * Ensure that the doorbell is updated before reading the event
370 		 * index from memory.  The controller needs to provide similar
371 		 * ordering to ensure the envent index is updated before reading
372 		 * the doorbell.
373 		 */
374 		mb();
375 
376 		event_idx = le32_to_cpu(*dbbuf_ei);
377 		if (!nvme_dbbuf_need_event(event_idx, value, old_value))
378 			return false;
379 	}
380 
381 	return true;
382 }
383 
384 /*
385  * Will slightly overestimate the number of pages needed.  This is OK
386  * as it only leads to a small amount of wasted memory for the lifetime of
387  * the I/O.
388  */
nvme_pci_npages_prp(void)389 static __always_inline int nvme_pci_npages_prp(void)
390 {
391 	unsigned max_bytes = (NVME_MAX_KB_SZ * 1024) + NVME_CTRL_PAGE_SIZE;
392 	unsigned nprps = DIV_ROUND_UP(max_bytes, NVME_CTRL_PAGE_SIZE);
393 	return DIV_ROUND_UP(8 * nprps, NVME_CTRL_PAGE_SIZE - 8);
394 }
395 
nvme_admin_init_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)396 static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
397 				unsigned int hctx_idx)
398 {
399 	struct nvme_dev *dev = to_nvme_dev(data);
400 	struct nvme_queue *nvmeq = &dev->queues[0];
401 
402 	WARN_ON(hctx_idx != 0);
403 	WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
404 
405 	hctx->driver_data = nvmeq;
406 	return 0;
407 }
408 
nvme_init_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)409 static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
410 			  unsigned int hctx_idx)
411 {
412 	struct nvme_dev *dev = to_nvme_dev(data);
413 	struct nvme_queue *nvmeq = &dev->queues[hctx_idx + 1];
414 
415 	WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
416 	hctx->driver_data = nvmeq;
417 	return 0;
418 }
419 
nvme_pci_init_request(struct blk_mq_tag_set * set,struct request * req,unsigned int hctx_idx,unsigned int numa_node)420 static int nvme_pci_init_request(struct blk_mq_tag_set *set,
421 		struct request *req, unsigned int hctx_idx,
422 		unsigned int numa_node)
423 {
424 	struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
425 
426 	nvme_req(req)->ctrl = set->driver_data;
427 	nvme_req(req)->cmd = &iod->cmd;
428 	return 0;
429 }
430 
queue_irq_offset(struct nvme_dev * dev)431 static int queue_irq_offset(struct nvme_dev *dev)
432 {
433 	/* if we have more than 1 vec, admin queue offsets us by 1 */
434 	if (dev->num_vecs > 1)
435 		return 1;
436 
437 	return 0;
438 }
439 
nvme_pci_map_queues(struct blk_mq_tag_set * set)440 static void nvme_pci_map_queues(struct blk_mq_tag_set *set)
441 {
442 	struct nvme_dev *dev = to_nvme_dev(set->driver_data);
443 	int i, qoff, offset;
444 
445 	offset = queue_irq_offset(dev);
446 	for (i = 0, qoff = 0; i < set->nr_maps; i++) {
447 		struct blk_mq_queue_map *map = &set->map[i];
448 
449 		map->nr_queues = dev->io_queues[i];
450 		if (!map->nr_queues) {
451 			BUG_ON(i == HCTX_TYPE_DEFAULT);
452 			continue;
453 		}
454 
455 		/*
456 		 * The poll queue(s) doesn't have an IRQ (and hence IRQ
457 		 * affinity), so use the regular blk-mq cpu mapping
458 		 */
459 		map->queue_offset = qoff;
460 		if (i != HCTX_TYPE_POLL && offset)
461 			blk_mq_pci_map_queues(map, to_pci_dev(dev->dev), offset);
462 		else
463 			blk_mq_map_queues(map);
464 		qoff += map->nr_queues;
465 		offset += map->nr_queues;
466 	}
467 }
468 
469 /*
470  * Write sq tail if we are asked to, or if the next command would wrap.
471  */
nvme_write_sq_db(struct nvme_queue * nvmeq,bool write_sq)472 static inline void nvme_write_sq_db(struct nvme_queue *nvmeq, bool write_sq)
473 {
474 	if (!write_sq) {
475 		u16 next_tail = nvmeq->sq_tail + 1;
476 
477 		if (next_tail == nvmeq->q_depth)
478 			next_tail = 0;
479 		if (next_tail != nvmeq->last_sq_tail)
480 			return;
481 	}
482 
483 	if (nvme_dbbuf_update_and_check_event(nvmeq->sq_tail,
484 			nvmeq->dbbuf_sq_db, nvmeq->dbbuf_sq_ei))
485 		writel(nvmeq->sq_tail, nvmeq->q_db);
486 	nvmeq->last_sq_tail = nvmeq->sq_tail;
487 }
488 
nvme_sq_copy_cmd(struct nvme_queue * nvmeq,struct nvme_command * cmd)489 static inline void nvme_sq_copy_cmd(struct nvme_queue *nvmeq,
490 				    struct nvme_command *cmd)
491 {
492 	memcpy(nvmeq->sq_cmds + (nvmeq->sq_tail << nvmeq->sqes),
493 		absolute_pointer(cmd), sizeof(*cmd));
494 	if (++nvmeq->sq_tail == nvmeq->q_depth)
495 		nvmeq->sq_tail = 0;
496 }
497 
nvme_commit_rqs(struct blk_mq_hw_ctx * hctx)498 static void nvme_commit_rqs(struct blk_mq_hw_ctx *hctx)
499 {
500 	struct nvme_queue *nvmeq = hctx->driver_data;
501 
502 	spin_lock(&nvmeq->sq_lock);
503 	if (nvmeq->sq_tail != nvmeq->last_sq_tail)
504 		nvme_write_sq_db(nvmeq, true);
505 	spin_unlock(&nvmeq->sq_lock);
506 }
507 
nvme_pci_use_sgls(struct nvme_dev * dev,struct request * req,int nseg)508 static inline bool nvme_pci_use_sgls(struct nvme_dev *dev, struct request *req,
509 				     int nseg)
510 {
511 	struct nvme_queue *nvmeq = req->mq_hctx->driver_data;
512 	unsigned int avg_seg_size;
513 
514 	avg_seg_size = DIV_ROUND_UP(blk_rq_payload_bytes(req), nseg);
515 
516 	if (!nvme_ctrl_sgl_supported(&dev->ctrl))
517 		return false;
518 	if (!nvmeq->qid)
519 		return false;
520 	if (!sgl_threshold || avg_seg_size < sgl_threshold)
521 		return false;
522 	return true;
523 }
524 
nvme_free_prps(struct nvme_dev * dev,struct request * req)525 static void nvme_free_prps(struct nvme_dev *dev, struct request *req)
526 {
527 	const int last_prp = NVME_CTRL_PAGE_SIZE / sizeof(__le64) - 1;
528 	struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
529 	dma_addr_t dma_addr = iod->first_dma;
530 	int i;
531 
532 	for (i = 0; i < iod->nr_allocations; i++) {
533 		__le64 *prp_list = iod->list[i].prp_list;
534 		dma_addr_t next_dma_addr = le64_to_cpu(prp_list[last_prp]);
535 
536 		dma_pool_free(dev->prp_page_pool, prp_list, dma_addr);
537 		dma_addr = next_dma_addr;
538 	}
539 }
540 
nvme_unmap_data(struct nvme_dev * dev,struct request * req)541 static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
542 {
543 	struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
544 
545 	if (iod->dma_len) {
546 		dma_unmap_page(dev->dev, iod->first_dma, iod->dma_len,
547 			       rq_dma_dir(req));
548 		return;
549 	}
550 
551 	WARN_ON_ONCE(!iod->sgt.nents);
552 
553 	dma_unmap_sgtable(dev->dev, &iod->sgt, rq_dma_dir(req), 0);
554 
555 	if (iod->nr_allocations == 0)
556 		dma_pool_free(dev->prp_small_pool, iod->list[0].sg_list,
557 			      iod->first_dma);
558 	else if (iod->nr_allocations == 1)
559 		dma_pool_free(dev->prp_page_pool, iod->list[0].sg_list,
560 			      iod->first_dma);
561 	else
562 		nvme_free_prps(dev, req);
563 	mempool_free(iod->sgt.sgl, dev->iod_mempool);
564 }
565 
nvme_print_sgl(struct scatterlist * sgl,int nents)566 static void nvme_print_sgl(struct scatterlist *sgl, int nents)
567 {
568 	int i;
569 	struct scatterlist *sg;
570 
571 	for_each_sg(sgl, sg, nents, i) {
572 		dma_addr_t phys = sg_phys(sg);
573 		pr_warn("sg[%d] phys_addr:%pad offset:%d length:%d "
574 			"dma_address:%pad dma_length:%d\n",
575 			i, &phys, sg->offset, sg->length, &sg_dma_address(sg),
576 			sg_dma_len(sg));
577 	}
578 }
579 
nvme_pci_setup_prps(struct nvme_dev * dev,struct request * req,struct nvme_rw_command * cmnd)580 static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev,
581 		struct request *req, struct nvme_rw_command *cmnd)
582 {
583 	struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
584 	struct dma_pool *pool;
585 	int length = blk_rq_payload_bytes(req);
586 	struct scatterlist *sg = iod->sgt.sgl;
587 	int dma_len = sg_dma_len(sg);
588 	u64 dma_addr = sg_dma_address(sg);
589 	int offset = dma_addr & (NVME_CTRL_PAGE_SIZE - 1);
590 	__le64 *prp_list;
591 	dma_addr_t prp_dma;
592 	int nprps, i;
593 
594 	length -= (NVME_CTRL_PAGE_SIZE - offset);
595 	if (length <= 0) {
596 		iod->first_dma = 0;
597 		goto done;
598 	}
599 
600 	dma_len -= (NVME_CTRL_PAGE_SIZE - offset);
601 	if (dma_len) {
602 		dma_addr += (NVME_CTRL_PAGE_SIZE - offset);
603 	} else {
604 		sg = sg_next(sg);
605 		dma_addr = sg_dma_address(sg);
606 		dma_len = sg_dma_len(sg);
607 	}
608 
609 	if (length <= NVME_CTRL_PAGE_SIZE) {
610 		iod->first_dma = dma_addr;
611 		goto done;
612 	}
613 
614 	nprps = DIV_ROUND_UP(length, NVME_CTRL_PAGE_SIZE);
615 	if (nprps <= (256 / 8)) {
616 		pool = dev->prp_small_pool;
617 		iod->nr_allocations = 0;
618 	} else {
619 		pool = dev->prp_page_pool;
620 		iod->nr_allocations = 1;
621 	}
622 
623 	prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
624 	if (!prp_list) {
625 		iod->nr_allocations = -1;
626 		return BLK_STS_RESOURCE;
627 	}
628 	iod->list[0].prp_list = prp_list;
629 	iod->first_dma = prp_dma;
630 	i = 0;
631 	for (;;) {
632 		if (i == NVME_CTRL_PAGE_SIZE >> 3) {
633 			__le64 *old_prp_list = prp_list;
634 			prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
635 			if (!prp_list)
636 				goto free_prps;
637 			iod->list[iod->nr_allocations++].prp_list = prp_list;
638 			prp_list[0] = old_prp_list[i - 1];
639 			old_prp_list[i - 1] = cpu_to_le64(prp_dma);
640 			i = 1;
641 		}
642 		prp_list[i++] = cpu_to_le64(dma_addr);
643 		dma_len -= NVME_CTRL_PAGE_SIZE;
644 		dma_addr += NVME_CTRL_PAGE_SIZE;
645 		length -= NVME_CTRL_PAGE_SIZE;
646 		if (length <= 0)
647 			break;
648 		if (dma_len > 0)
649 			continue;
650 		if (unlikely(dma_len < 0))
651 			goto bad_sgl;
652 		sg = sg_next(sg);
653 		dma_addr = sg_dma_address(sg);
654 		dma_len = sg_dma_len(sg);
655 	}
656 done:
657 	cmnd->dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sgt.sgl));
658 	cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma);
659 	return BLK_STS_OK;
660 free_prps:
661 	nvme_free_prps(dev, req);
662 	return BLK_STS_RESOURCE;
663 bad_sgl:
664 	WARN(DO_ONCE(nvme_print_sgl, iod->sgt.sgl, iod->sgt.nents),
665 			"Invalid SGL for payload:%d nents:%d\n",
666 			blk_rq_payload_bytes(req), iod->sgt.nents);
667 	return BLK_STS_IOERR;
668 }
669 
nvme_pci_sgl_set_data(struct nvme_sgl_desc * sge,struct scatterlist * sg)670 static void nvme_pci_sgl_set_data(struct nvme_sgl_desc *sge,
671 		struct scatterlist *sg)
672 {
673 	sge->addr = cpu_to_le64(sg_dma_address(sg));
674 	sge->length = cpu_to_le32(sg_dma_len(sg));
675 	sge->type = NVME_SGL_FMT_DATA_DESC << 4;
676 }
677 
nvme_pci_sgl_set_seg(struct nvme_sgl_desc * sge,dma_addr_t dma_addr,int entries)678 static void nvme_pci_sgl_set_seg(struct nvme_sgl_desc *sge,
679 		dma_addr_t dma_addr, int entries)
680 {
681 	sge->addr = cpu_to_le64(dma_addr);
682 	sge->length = cpu_to_le32(entries * sizeof(*sge));
683 	sge->type = NVME_SGL_FMT_LAST_SEG_DESC << 4;
684 }
685 
nvme_pci_setup_sgls(struct nvme_dev * dev,struct request * req,struct nvme_rw_command * cmd)686 static blk_status_t nvme_pci_setup_sgls(struct nvme_dev *dev,
687 		struct request *req, struct nvme_rw_command *cmd)
688 {
689 	struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
690 	struct dma_pool *pool;
691 	struct nvme_sgl_desc *sg_list;
692 	struct scatterlist *sg = iod->sgt.sgl;
693 	unsigned int entries = iod->sgt.nents;
694 	dma_addr_t sgl_dma;
695 	int i = 0;
696 
697 	/* setting the transfer type as SGL */
698 	cmd->flags = NVME_CMD_SGL_METABUF;
699 
700 	if (entries == 1) {
701 		nvme_pci_sgl_set_data(&cmd->dptr.sgl, sg);
702 		return BLK_STS_OK;
703 	}
704 
705 	if (entries <= (256 / sizeof(struct nvme_sgl_desc))) {
706 		pool = dev->prp_small_pool;
707 		iod->nr_allocations = 0;
708 	} else {
709 		pool = dev->prp_page_pool;
710 		iod->nr_allocations = 1;
711 	}
712 
713 	sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma);
714 	if (!sg_list) {
715 		iod->nr_allocations = -1;
716 		return BLK_STS_RESOURCE;
717 	}
718 
719 	iod->list[0].sg_list = sg_list;
720 	iod->first_dma = sgl_dma;
721 
722 	nvme_pci_sgl_set_seg(&cmd->dptr.sgl, sgl_dma, entries);
723 	do {
724 		nvme_pci_sgl_set_data(&sg_list[i++], sg);
725 		sg = sg_next(sg);
726 	} while (--entries > 0);
727 
728 	return BLK_STS_OK;
729 }
730 
nvme_setup_prp_simple(struct nvme_dev * dev,struct request * req,struct nvme_rw_command * cmnd,struct bio_vec * bv)731 static blk_status_t nvme_setup_prp_simple(struct nvme_dev *dev,
732 		struct request *req, struct nvme_rw_command *cmnd,
733 		struct bio_vec *bv)
734 {
735 	struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
736 	unsigned int offset = bv->bv_offset & (NVME_CTRL_PAGE_SIZE - 1);
737 	unsigned int first_prp_len = NVME_CTRL_PAGE_SIZE - offset;
738 
739 	iod->first_dma = dma_map_bvec(dev->dev, bv, rq_dma_dir(req), 0);
740 	if (dma_mapping_error(dev->dev, iod->first_dma))
741 		return BLK_STS_RESOURCE;
742 	iod->dma_len = bv->bv_len;
743 
744 	cmnd->dptr.prp1 = cpu_to_le64(iod->first_dma);
745 	if (bv->bv_len > first_prp_len)
746 		cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma + first_prp_len);
747 	else
748 		cmnd->dptr.prp2 = 0;
749 	return BLK_STS_OK;
750 }
751 
nvme_setup_sgl_simple(struct nvme_dev * dev,struct request * req,struct nvme_rw_command * cmnd,struct bio_vec * bv)752 static blk_status_t nvme_setup_sgl_simple(struct nvme_dev *dev,
753 		struct request *req, struct nvme_rw_command *cmnd,
754 		struct bio_vec *bv)
755 {
756 	struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
757 
758 	iod->first_dma = dma_map_bvec(dev->dev, bv, rq_dma_dir(req), 0);
759 	if (dma_mapping_error(dev->dev, iod->first_dma))
760 		return BLK_STS_RESOURCE;
761 	iod->dma_len = bv->bv_len;
762 
763 	cmnd->flags = NVME_CMD_SGL_METABUF;
764 	cmnd->dptr.sgl.addr = cpu_to_le64(iod->first_dma);
765 	cmnd->dptr.sgl.length = cpu_to_le32(iod->dma_len);
766 	cmnd->dptr.sgl.type = NVME_SGL_FMT_DATA_DESC << 4;
767 	return BLK_STS_OK;
768 }
769 
nvme_map_data(struct nvme_dev * dev,struct request * req,struct nvme_command * cmnd)770 static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,
771 		struct nvme_command *cmnd)
772 {
773 	struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
774 	blk_status_t ret = BLK_STS_RESOURCE;
775 	int rc;
776 
777 	if (blk_rq_nr_phys_segments(req) == 1) {
778 		struct nvme_queue *nvmeq = req->mq_hctx->driver_data;
779 		struct bio_vec bv = req_bvec(req);
780 
781 		if (!is_pci_p2pdma_page(bv.bv_page)) {
782 			if ((bv.bv_offset & (NVME_CTRL_PAGE_SIZE - 1)) +
783 			     bv.bv_len <= NVME_CTRL_PAGE_SIZE * 2)
784 				return nvme_setup_prp_simple(dev, req,
785 							     &cmnd->rw, &bv);
786 
787 			if (nvmeq->qid && sgl_threshold &&
788 			    nvme_ctrl_sgl_supported(&dev->ctrl))
789 				return nvme_setup_sgl_simple(dev, req,
790 							     &cmnd->rw, &bv);
791 		}
792 	}
793 
794 	iod->dma_len = 0;
795 	iod->sgt.sgl = mempool_alloc(dev->iod_mempool, GFP_ATOMIC);
796 	if (!iod->sgt.sgl)
797 		return BLK_STS_RESOURCE;
798 	sg_init_table(iod->sgt.sgl, blk_rq_nr_phys_segments(req));
799 	iod->sgt.orig_nents = blk_rq_map_sg(req->q, req, iod->sgt.sgl);
800 	if (!iod->sgt.orig_nents)
801 		goto out_free_sg;
802 
803 	rc = dma_map_sgtable(dev->dev, &iod->sgt, rq_dma_dir(req),
804 			     DMA_ATTR_NO_WARN);
805 	if (rc) {
806 		if (rc == -EREMOTEIO)
807 			ret = BLK_STS_TARGET;
808 		goto out_free_sg;
809 	}
810 
811 	if (nvme_pci_use_sgls(dev, req, iod->sgt.nents))
812 		ret = nvme_pci_setup_sgls(dev, req, &cmnd->rw);
813 	else
814 		ret = nvme_pci_setup_prps(dev, req, &cmnd->rw);
815 	if (ret != BLK_STS_OK)
816 		goto out_unmap_sg;
817 	return BLK_STS_OK;
818 
819 out_unmap_sg:
820 	dma_unmap_sgtable(dev->dev, &iod->sgt, rq_dma_dir(req), 0);
821 out_free_sg:
822 	mempool_free(iod->sgt.sgl, dev->iod_mempool);
823 	return ret;
824 }
825 
nvme_map_metadata(struct nvme_dev * dev,struct request * req,struct nvme_command * cmnd)826 static blk_status_t nvme_map_metadata(struct nvme_dev *dev, struct request *req,
827 		struct nvme_command *cmnd)
828 {
829 	struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
830 	struct bio_vec bv = rq_integrity_vec(req);
831 
832 	iod->meta_dma = dma_map_bvec(dev->dev, &bv, rq_dma_dir(req), 0);
833 	if (dma_mapping_error(dev->dev, iod->meta_dma))
834 		return BLK_STS_IOERR;
835 	cmnd->rw.metadata = cpu_to_le64(iod->meta_dma);
836 	return BLK_STS_OK;
837 }
838 
nvme_prep_rq(struct nvme_dev * dev,struct request * req)839 static blk_status_t nvme_prep_rq(struct nvme_dev *dev, struct request *req)
840 {
841 	struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
842 	blk_status_t ret;
843 
844 	iod->aborted = false;
845 	iod->nr_allocations = -1;
846 	iod->sgt.nents = 0;
847 
848 	ret = nvme_setup_cmd(req->q->queuedata, req);
849 	if (ret)
850 		return ret;
851 
852 	if (blk_rq_nr_phys_segments(req)) {
853 		ret = nvme_map_data(dev, req, &iod->cmd);
854 		if (ret)
855 			goto out_free_cmd;
856 	}
857 
858 	if (blk_integrity_rq(req)) {
859 		ret = nvme_map_metadata(dev, req, &iod->cmd);
860 		if (ret)
861 			goto out_unmap_data;
862 	}
863 
864 	nvme_start_request(req);
865 	return BLK_STS_OK;
866 out_unmap_data:
867 	if (blk_rq_nr_phys_segments(req))
868 		nvme_unmap_data(dev, req);
869 out_free_cmd:
870 	nvme_cleanup_cmd(req);
871 	return ret;
872 }
873 
874 /*
875  * NOTE: ns is NULL when called on the admin queue.
876  */
nvme_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)877 static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
878 			 const struct blk_mq_queue_data *bd)
879 {
880 	struct nvme_queue *nvmeq = hctx->driver_data;
881 	struct nvme_dev *dev = nvmeq->dev;
882 	struct request *req = bd->rq;
883 	struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
884 	blk_status_t ret;
885 
886 	/*
887 	 * We should not need to do this, but we're still using this to
888 	 * ensure we can drain requests on a dying queue.
889 	 */
890 	if (unlikely(!test_bit(NVMEQ_ENABLED, &nvmeq->flags)))
891 		return BLK_STS_IOERR;
892 
893 	if (unlikely(!nvme_check_ready(&dev->ctrl, req, true)))
894 		return nvme_fail_nonready_command(&dev->ctrl, req);
895 
896 	ret = nvme_prep_rq(dev, req);
897 	if (unlikely(ret))
898 		return ret;
899 	spin_lock(&nvmeq->sq_lock);
900 	nvme_sq_copy_cmd(nvmeq, &iod->cmd);
901 	nvme_write_sq_db(nvmeq, bd->last);
902 	spin_unlock(&nvmeq->sq_lock);
903 	return BLK_STS_OK;
904 }
905 
nvme_submit_cmds(struct nvme_queue * nvmeq,struct request ** rqlist)906 static void nvme_submit_cmds(struct nvme_queue *nvmeq, struct request **rqlist)
907 {
908 	struct request *req;
909 
910 	spin_lock(&nvmeq->sq_lock);
911 	while ((req = rq_list_pop(rqlist))) {
912 		struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
913 
914 		nvme_sq_copy_cmd(nvmeq, &iod->cmd);
915 	}
916 	nvme_write_sq_db(nvmeq, true);
917 	spin_unlock(&nvmeq->sq_lock);
918 }
919 
nvme_prep_rq_batch(struct nvme_queue * nvmeq,struct request * req)920 static bool nvme_prep_rq_batch(struct nvme_queue *nvmeq, struct request *req)
921 {
922 	/*
923 	 * We should not need to do this, but we're still using this to
924 	 * ensure we can drain requests on a dying queue.
925 	 */
926 	if (unlikely(!test_bit(NVMEQ_ENABLED, &nvmeq->flags)))
927 		return false;
928 	if (unlikely(!nvme_check_ready(&nvmeq->dev->ctrl, req, true)))
929 		return false;
930 
931 	req->mq_hctx->tags->rqs[req->tag] = req;
932 	return nvme_prep_rq(nvmeq->dev, req) == BLK_STS_OK;
933 }
934 
nvme_queue_rqs(struct request ** rqlist)935 static void nvme_queue_rqs(struct request **rqlist)
936 {
937 	struct request *submit_list = NULL;
938 	struct request *requeue_list = NULL;
939 	struct request **requeue_lastp = &requeue_list;
940 	struct nvme_queue *nvmeq = NULL;
941 	struct request *req;
942 
943 	while ((req = rq_list_pop(rqlist))) {
944 		if (nvmeq && nvmeq != req->mq_hctx->driver_data)
945 			nvme_submit_cmds(nvmeq, &submit_list);
946 		nvmeq = req->mq_hctx->driver_data;
947 
948 		if (nvme_prep_rq_batch(nvmeq, req))
949 			rq_list_add(&submit_list, req); /* reverse order */
950 		else
951 			rq_list_add_tail(&requeue_lastp, req);
952 	}
953 
954 	if (nvmeq)
955 		nvme_submit_cmds(nvmeq, &submit_list);
956 	*rqlist = requeue_list;
957 }
958 
nvme_pci_unmap_rq(struct request * req)959 static __always_inline void nvme_pci_unmap_rq(struct request *req)
960 {
961 	struct nvme_queue *nvmeq = req->mq_hctx->driver_data;
962 	struct nvme_dev *dev = nvmeq->dev;
963 
964 	if (blk_integrity_rq(req)) {
965 	        struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
966 
967 		dma_unmap_page(dev->dev, iod->meta_dma,
968 			       rq_integrity_vec(req).bv_len, rq_dma_dir(req));
969 	}
970 
971 	if (blk_rq_nr_phys_segments(req))
972 		nvme_unmap_data(dev, req);
973 }
974 
nvme_pci_complete_rq(struct request * req)975 static void nvme_pci_complete_rq(struct request *req)
976 {
977 	nvme_pci_unmap_rq(req);
978 	nvme_complete_rq(req);
979 }
980 
nvme_pci_complete_batch(struct io_comp_batch * iob)981 static void nvme_pci_complete_batch(struct io_comp_batch *iob)
982 {
983 	nvme_complete_batch(iob, nvme_pci_unmap_rq);
984 }
985 
986 /* We read the CQE phase first to check if the rest of the entry is valid */
nvme_cqe_pending(struct nvme_queue * nvmeq)987 static inline bool nvme_cqe_pending(struct nvme_queue *nvmeq)
988 {
989 	struct nvme_completion *hcqe = &nvmeq->cqes[nvmeq->cq_head];
990 
991 	return (le16_to_cpu(READ_ONCE(hcqe->status)) & 1) == nvmeq->cq_phase;
992 }
993 
nvme_ring_cq_doorbell(struct nvme_queue * nvmeq)994 static inline void nvme_ring_cq_doorbell(struct nvme_queue *nvmeq)
995 {
996 	u16 head = nvmeq->cq_head;
997 
998 	if (nvme_dbbuf_update_and_check_event(head, nvmeq->dbbuf_cq_db,
999 					      nvmeq->dbbuf_cq_ei))
1000 		writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
1001 }
1002 
nvme_queue_tagset(struct nvme_queue * nvmeq)1003 static inline struct blk_mq_tags *nvme_queue_tagset(struct nvme_queue *nvmeq)
1004 {
1005 	if (!nvmeq->qid)
1006 		return nvmeq->dev->admin_tagset.tags[0];
1007 	return nvmeq->dev->tagset.tags[nvmeq->qid - 1];
1008 }
1009 
nvme_handle_cqe(struct nvme_queue * nvmeq,struct io_comp_batch * iob,u16 idx)1010 static inline void nvme_handle_cqe(struct nvme_queue *nvmeq,
1011 				   struct io_comp_batch *iob, u16 idx)
1012 {
1013 	struct nvme_completion *cqe = &nvmeq->cqes[idx];
1014 	__u16 command_id = READ_ONCE(cqe->command_id);
1015 	struct request *req;
1016 
1017 	/*
1018 	 * AEN requests are special as they don't time out and can
1019 	 * survive any kind of queue freeze and often don't respond to
1020 	 * aborts.  We don't even bother to allocate a struct request
1021 	 * for them but rather special case them here.
1022 	 */
1023 	if (unlikely(nvme_is_aen_req(nvmeq->qid, command_id))) {
1024 		nvme_complete_async_event(&nvmeq->dev->ctrl,
1025 				cqe->status, &cqe->result);
1026 		return;
1027 	}
1028 
1029 	req = nvme_find_rq(nvme_queue_tagset(nvmeq), command_id);
1030 	if (unlikely(!req)) {
1031 		dev_warn(nvmeq->dev->ctrl.device,
1032 			"invalid id %d completed on queue %d\n",
1033 			command_id, le16_to_cpu(cqe->sq_id));
1034 		return;
1035 	}
1036 
1037 	trace_nvme_sq(req, cqe->sq_head, nvmeq->sq_tail);
1038 	if (!nvme_try_complete_req(req, cqe->status, cqe->result) &&
1039 	    !blk_mq_add_to_batch(req, iob, nvme_req(req)->status,
1040 					nvme_pci_complete_batch))
1041 		nvme_pci_complete_rq(req);
1042 }
1043 
nvme_update_cq_head(struct nvme_queue * nvmeq)1044 static inline void nvme_update_cq_head(struct nvme_queue *nvmeq)
1045 {
1046 	u32 tmp = nvmeq->cq_head + 1;
1047 
1048 	if (tmp == nvmeq->q_depth) {
1049 		nvmeq->cq_head = 0;
1050 		nvmeq->cq_phase ^= 1;
1051 	} else {
1052 		nvmeq->cq_head = tmp;
1053 	}
1054 }
1055 
nvme_poll_cq(struct nvme_queue * nvmeq,struct io_comp_batch * iob)1056 static inline int nvme_poll_cq(struct nvme_queue *nvmeq,
1057 			       struct io_comp_batch *iob)
1058 {
1059 	int found = 0;
1060 
1061 	while (nvme_cqe_pending(nvmeq)) {
1062 		found++;
1063 		/*
1064 		 * load-load control dependency between phase and the rest of
1065 		 * the cqe requires a full read memory barrier
1066 		 */
1067 		dma_rmb();
1068 		nvme_handle_cqe(nvmeq, iob, nvmeq->cq_head);
1069 		nvme_update_cq_head(nvmeq);
1070 	}
1071 
1072 	if (found)
1073 		nvme_ring_cq_doorbell(nvmeq);
1074 	return found;
1075 }
1076 
nvme_irq(int irq,void * data)1077 static irqreturn_t nvme_irq(int irq, void *data)
1078 {
1079 	struct nvme_queue *nvmeq = data;
1080 	DEFINE_IO_COMP_BATCH(iob);
1081 
1082 	if (nvme_poll_cq(nvmeq, &iob)) {
1083 		if (!rq_list_empty(iob.req_list))
1084 			nvme_pci_complete_batch(&iob);
1085 		return IRQ_HANDLED;
1086 	}
1087 	return IRQ_NONE;
1088 }
1089 
nvme_irq_check(int irq,void * data)1090 static irqreturn_t nvme_irq_check(int irq, void *data)
1091 {
1092 	struct nvme_queue *nvmeq = data;
1093 
1094 	if (nvme_cqe_pending(nvmeq))
1095 		return IRQ_WAKE_THREAD;
1096 	return IRQ_NONE;
1097 }
1098 
1099 /*
1100  * Poll for completions for any interrupt driven queue
1101  * Can be called from any context.
1102  */
nvme_poll_irqdisable(struct nvme_queue * nvmeq)1103 static void nvme_poll_irqdisable(struct nvme_queue *nvmeq)
1104 {
1105 	struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev);
1106 
1107 	WARN_ON_ONCE(test_bit(NVMEQ_POLLED, &nvmeq->flags));
1108 
1109 	disable_irq(pci_irq_vector(pdev, nvmeq->cq_vector));
1110 	spin_lock(&nvmeq->cq_poll_lock);
1111 	nvme_poll_cq(nvmeq, NULL);
1112 	spin_unlock(&nvmeq->cq_poll_lock);
1113 	enable_irq(pci_irq_vector(pdev, nvmeq->cq_vector));
1114 }
1115 
nvme_poll(struct blk_mq_hw_ctx * hctx,struct io_comp_batch * iob)1116 static int nvme_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
1117 {
1118 	struct nvme_queue *nvmeq = hctx->driver_data;
1119 	bool found;
1120 
1121 	if (!nvme_cqe_pending(nvmeq))
1122 		return 0;
1123 
1124 	spin_lock(&nvmeq->cq_poll_lock);
1125 	found = nvme_poll_cq(nvmeq, iob);
1126 	spin_unlock(&nvmeq->cq_poll_lock);
1127 
1128 	return found;
1129 }
1130 
nvme_pci_submit_async_event(struct nvme_ctrl * ctrl)1131 static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl)
1132 {
1133 	struct nvme_dev *dev = to_nvme_dev(ctrl);
1134 	struct nvme_queue *nvmeq = &dev->queues[0];
1135 	struct nvme_command c = { };
1136 
1137 	c.common.opcode = nvme_admin_async_event;
1138 	c.common.command_id = NVME_AQ_BLK_MQ_DEPTH;
1139 
1140 	spin_lock(&nvmeq->sq_lock);
1141 	nvme_sq_copy_cmd(nvmeq, &c);
1142 	nvme_write_sq_db(nvmeq, true);
1143 	spin_unlock(&nvmeq->sq_lock);
1144 }
1145 
adapter_delete_queue(struct nvme_dev * dev,u8 opcode,u16 id)1146 static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1147 {
1148 	struct nvme_command c = { };
1149 
1150 	c.delete_queue.opcode = opcode;
1151 	c.delete_queue.qid = cpu_to_le16(id);
1152 
1153 	return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1154 }
1155 
adapter_alloc_cq(struct nvme_dev * dev,u16 qid,struct nvme_queue * nvmeq,s16 vector)1156 static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1157 		struct nvme_queue *nvmeq, s16 vector)
1158 {
1159 	struct nvme_command c = { };
1160 	int flags = NVME_QUEUE_PHYS_CONTIG;
1161 
1162 	if (!test_bit(NVMEQ_POLLED, &nvmeq->flags))
1163 		flags |= NVME_CQ_IRQ_ENABLED;
1164 
1165 	/*
1166 	 * Note: we (ab)use the fact that the prp fields survive if no data
1167 	 * is attached to the request.
1168 	 */
1169 	c.create_cq.opcode = nvme_admin_create_cq;
1170 	c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1171 	c.create_cq.cqid = cpu_to_le16(qid);
1172 	c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1173 	c.create_cq.cq_flags = cpu_to_le16(flags);
1174 	c.create_cq.irq_vector = cpu_to_le16(vector);
1175 
1176 	return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1177 }
1178 
adapter_alloc_sq(struct nvme_dev * dev,u16 qid,struct nvme_queue * nvmeq)1179 static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1180 						struct nvme_queue *nvmeq)
1181 {
1182 	struct nvme_ctrl *ctrl = &dev->ctrl;
1183 	struct nvme_command c = { };
1184 	int flags = NVME_QUEUE_PHYS_CONTIG;
1185 
1186 	/*
1187 	 * Some drives have a bug that auto-enables WRRU if MEDIUM isn't
1188 	 * set. Since URGENT priority is zeroes, it makes all queues
1189 	 * URGENT.
1190 	 */
1191 	if (ctrl->quirks & NVME_QUIRK_MEDIUM_PRIO_SQ)
1192 		flags |= NVME_SQ_PRIO_MEDIUM;
1193 
1194 	/*
1195 	 * Note: we (ab)use the fact that the prp fields survive if no data
1196 	 * is attached to the request.
1197 	 */
1198 	c.create_sq.opcode = nvme_admin_create_sq;
1199 	c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1200 	c.create_sq.sqid = cpu_to_le16(qid);
1201 	c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1202 	c.create_sq.sq_flags = cpu_to_le16(flags);
1203 	c.create_sq.cqid = cpu_to_le16(qid);
1204 
1205 	return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1206 }
1207 
adapter_delete_cq(struct nvme_dev * dev,u16 cqid)1208 static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1209 {
1210 	return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1211 }
1212 
adapter_delete_sq(struct nvme_dev * dev,u16 sqid)1213 static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1214 {
1215 	return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1216 }
1217 
abort_endio(struct request * req,blk_status_t error)1218 static enum rq_end_io_ret abort_endio(struct request *req, blk_status_t error)
1219 {
1220 	struct nvme_queue *nvmeq = req->mq_hctx->driver_data;
1221 
1222 	dev_warn(nvmeq->dev->ctrl.device,
1223 		 "Abort status: 0x%x", nvme_req(req)->status);
1224 	atomic_inc(&nvmeq->dev->ctrl.abort_limit);
1225 	blk_mq_free_request(req);
1226 	return RQ_END_IO_NONE;
1227 }
1228 
nvme_should_reset(struct nvme_dev * dev,u32 csts)1229 static bool nvme_should_reset(struct nvme_dev *dev, u32 csts)
1230 {
1231 	/* If true, indicates loss of adapter communication, possibly by a
1232 	 * NVMe Subsystem reset.
1233 	 */
1234 	bool nssro = dev->subsystem && (csts & NVME_CSTS_NSSRO);
1235 
1236 	/* If there is a reset/reinit ongoing, we shouldn't reset again. */
1237 	switch (nvme_ctrl_state(&dev->ctrl)) {
1238 	case NVME_CTRL_RESETTING:
1239 	case NVME_CTRL_CONNECTING:
1240 		return false;
1241 	default:
1242 		break;
1243 	}
1244 
1245 	/* We shouldn't reset unless the controller is on fatal error state
1246 	 * _or_ if we lost the communication with it.
1247 	 */
1248 	if (!(csts & NVME_CSTS_CFS) && !nssro)
1249 		return false;
1250 
1251 	return true;
1252 }
1253 
nvme_warn_reset(struct nvme_dev * dev,u32 csts)1254 static void nvme_warn_reset(struct nvme_dev *dev, u32 csts)
1255 {
1256 	/* Read a config register to help see what died. */
1257 	u16 pci_status;
1258 	int result;
1259 
1260 	result = pci_read_config_word(to_pci_dev(dev->dev), PCI_STATUS,
1261 				      &pci_status);
1262 	if (result == PCIBIOS_SUCCESSFUL)
1263 		dev_warn(dev->ctrl.device,
1264 			 "controller is down; will reset: CSTS=0x%x, PCI_STATUS=0x%hx\n",
1265 			 csts, pci_status);
1266 	else
1267 		dev_warn(dev->ctrl.device,
1268 			 "controller is down; will reset: CSTS=0x%x, PCI_STATUS read failed (%d)\n",
1269 			 csts, result);
1270 
1271 	if (csts != ~0)
1272 		return;
1273 
1274 	dev_warn(dev->ctrl.device,
1275 		 "Does your device have a faulty power saving mode enabled?\n");
1276 	dev_warn(dev->ctrl.device,
1277 		 "Try \"nvme_core.default_ps_max_latency_us=0 pcie_aspm=off pcie_port_pm=off\" and report a bug\n");
1278 }
1279 
nvme_timeout(struct request * req)1280 static enum blk_eh_timer_return nvme_timeout(struct request *req)
1281 {
1282 	struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
1283 	struct nvme_queue *nvmeq = req->mq_hctx->driver_data;
1284 	struct nvme_dev *dev = nvmeq->dev;
1285 	struct request *abort_req;
1286 	struct nvme_command cmd = { };
1287 	struct pci_dev *pdev = to_pci_dev(dev->dev);
1288 	u32 csts = readl(dev->bar + NVME_REG_CSTS);
1289 
1290 	/*
1291 	 * Shutdown the device immediately if we see it is disconnected. This
1292 	 * unblocks PCIe error handling if the nvme driver is waiting in
1293 	 * error_resume for a device that has been removed. We can't unbind the
1294 	 * driver while the driver's error callback is waiting to complete, so
1295 	 * we're relying on a timeout to break that deadlock if a removal
1296 	 * occurs while reset work is running.
1297 	 */
1298 	if (pci_dev_is_disconnected(pdev))
1299 		nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
1300 	if (nvme_state_terminal(&dev->ctrl))
1301 		goto disable;
1302 
1303 	/* If PCI error recovery process is happening, we cannot reset or
1304 	 * the recovery mechanism will surely fail.
1305 	 */
1306 	mb();
1307 	if (pci_channel_offline(pdev))
1308 		return BLK_EH_RESET_TIMER;
1309 
1310 	/*
1311 	 * Reset immediately if the controller is failed
1312 	 */
1313 	if (nvme_should_reset(dev, csts)) {
1314 		nvme_warn_reset(dev, csts);
1315 		goto disable;
1316 	}
1317 
1318 	/*
1319 	 * Did we miss an interrupt?
1320 	 */
1321 	if (test_bit(NVMEQ_POLLED, &nvmeq->flags))
1322 		nvme_poll(req->mq_hctx, NULL);
1323 	else
1324 		nvme_poll_irqdisable(nvmeq);
1325 
1326 	if (blk_mq_rq_state(req) != MQ_RQ_IN_FLIGHT) {
1327 		dev_warn(dev->ctrl.device,
1328 			 "I/O %d QID %d timeout, completion polled\n",
1329 			 req->tag, nvmeq->qid);
1330 		return BLK_EH_DONE;
1331 	}
1332 
1333 	/*
1334 	 * Shutdown immediately if controller times out while starting. The
1335 	 * reset work will see the pci device disabled when it gets the forced
1336 	 * cancellation error. All outstanding requests are completed on
1337 	 * shutdown, so we return BLK_EH_DONE.
1338 	 */
1339 	switch (nvme_ctrl_state(&dev->ctrl)) {
1340 	case NVME_CTRL_CONNECTING:
1341 		nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
1342 		fallthrough;
1343 	case NVME_CTRL_DELETING:
1344 		dev_warn_ratelimited(dev->ctrl.device,
1345 			 "I/O %d QID %d timeout, disable controller\n",
1346 			 req->tag, nvmeq->qid);
1347 		nvme_req(req)->flags |= NVME_REQ_CANCELLED;
1348 		nvme_dev_disable(dev, true);
1349 		return BLK_EH_DONE;
1350 	case NVME_CTRL_RESETTING:
1351 		return BLK_EH_RESET_TIMER;
1352 	default:
1353 		break;
1354 	}
1355 
1356 	/*
1357 	 * Shutdown the controller immediately and schedule a reset if the
1358 	 * command was already aborted once before and still hasn't been
1359 	 * returned to the driver, or if this is the admin queue.
1360 	 */
1361 	if (!nvmeq->qid || iod->aborted) {
1362 		dev_warn(dev->ctrl.device,
1363 			 "I/O %d QID %d timeout, reset controller\n",
1364 			 req->tag, nvmeq->qid);
1365 		nvme_req(req)->flags |= NVME_REQ_CANCELLED;
1366 		goto disable;
1367 	}
1368 
1369 	if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) {
1370 		atomic_inc(&dev->ctrl.abort_limit);
1371 		return BLK_EH_RESET_TIMER;
1372 	}
1373 	iod->aborted = true;
1374 
1375 	cmd.abort.opcode = nvme_admin_abort_cmd;
1376 	cmd.abort.cid = nvme_cid(req);
1377 	cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
1378 
1379 	dev_warn(nvmeq->dev->ctrl.device,
1380 		"I/O %d (%s) QID %d timeout, aborting\n",
1381 		 req->tag,
1382 		 nvme_get_opcode_str(nvme_req(req)->cmd->common.opcode),
1383 		 nvmeq->qid);
1384 
1385 	abort_req = blk_mq_alloc_request(dev->ctrl.admin_q, nvme_req_op(&cmd),
1386 					 BLK_MQ_REQ_NOWAIT);
1387 	if (IS_ERR(abort_req)) {
1388 		atomic_inc(&dev->ctrl.abort_limit);
1389 		return BLK_EH_RESET_TIMER;
1390 	}
1391 	nvme_init_request(abort_req, &cmd);
1392 
1393 	abort_req->end_io = abort_endio;
1394 	abort_req->end_io_data = NULL;
1395 	blk_execute_rq_nowait(abort_req, false);
1396 
1397 	/*
1398 	 * The aborted req will be completed on receiving the abort req.
1399 	 * We enable the timer again. If hit twice, it'll cause a device reset,
1400 	 * as the device then is in a faulty state.
1401 	 */
1402 	return BLK_EH_RESET_TIMER;
1403 
1404 disable:
1405 	if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING)) {
1406 		if (nvme_state_terminal(&dev->ctrl))
1407 			nvme_dev_disable(dev, true);
1408 		return BLK_EH_DONE;
1409 	}
1410 
1411 	nvme_dev_disable(dev, false);
1412 	if (nvme_try_sched_reset(&dev->ctrl))
1413 		nvme_unquiesce_io_queues(&dev->ctrl);
1414 	return BLK_EH_DONE;
1415 }
1416 
nvme_free_queue(struct nvme_queue * nvmeq)1417 static void nvme_free_queue(struct nvme_queue *nvmeq)
1418 {
1419 	dma_free_coherent(nvmeq->dev->dev, CQ_SIZE(nvmeq),
1420 				(void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1421 	if (!nvmeq->sq_cmds)
1422 		return;
1423 
1424 	if (test_and_clear_bit(NVMEQ_SQ_CMB, &nvmeq->flags)) {
1425 		pci_free_p2pmem(to_pci_dev(nvmeq->dev->dev),
1426 				nvmeq->sq_cmds, SQ_SIZE(nvmeq));
1427 	} else {
1428 		dma_free_coherent(nvmeq->dev->dev, SQ_SIZE(nvmeq),
1429 				nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1430 	}
1431 }
1432 
nvme_free_queues(struct nvme_dev * dev,int lowest)1433 static void nvme_free_queues(struct nvme_dev *dev, int lowest)
1434 {
1435 	int i;
1436 
1437 	for (i = dev->ctrl.queue_count - 1; i >= lowest; i--) {
1438 		dev->ctrl.queue_count--;
1439 		nvme_free_queue(&dev->queues[i]);
1440 	}
1441 }
1442 
nvme_suspend_queue(struct nvme_dev * dev,unsigned int qid)1443 static void nvme_suspend_queue(struct nvme_dev *dev, unsigned int qid)
1444 {
1445 	struct nvme_queue *nvmeq = &dev->queues[qid];
1446 
1447 	if (!test_and_clear_bit(NVMEQ_ENABLED, &nvmeq->flags))
1448 		return;
1449 
1450 	/* ensure that nvme_queue_rq() sees NVMEQ_ENABLED cleared */
1451 	mb();
1452 
1453 	nvmeq->dev->online_queues--;
1454 	if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
1455 		nvme_quiesce_admin_queue(&nvmeq->dev->ctrl);
1456 	if (!test_and_clear_bit(NVMEQ_POLLED, &nvmeq->flags))
1457 		pci_free_irq(to_pci_dev(dev->dev), nvmeq->cq_vector, nvmeq);
1458 }
1459 
nvme_suspend_io_queues(struct nvme_dev * dev)1460 static void nvme_suspend_io_queues(struct nvme_dev *dev)
1461 {
1462 	int i;
1463 
1464 	for (i = dev->ctrl.queue_count - 1; i > 0; i--)
1465 		nvme_suspend_queue(dev, i);
1466 }
1467 
1468 /*
1469  * Called only on a device that has been disabled and after all other threads
1470  * that can check this device's completion queues have synced, except
1471  * nvme_poll(). This is the last chance for the driver to see a natural
1472  * completion before nvme_cancel_request() terminates all incomplete requests.
1473  */
nvme_reap_pending_cqes(struct nvme_dev * dev)1474 static void nvme_reap_pending_cqes(struct nvme_dev *dev)
1475 {
1476 	int i;
1477 
1478 	for (i = dev->ctrl.queue_count - 1; i > 0; i--) {
1479 		spin_lock(&dev->queues[i].cq_poll_lock);
1480 		nvme_poll_cq(&dev->queues[i], NULL);
1481 		spin_unlock(&dev->queues[i].cq_poll_lock);
1482 	}
1483 }
1484 
nvme_cmb_qdepth(struct nvme_dev * dev,int nr_io_queues,int entry_size)1485 static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1486 				int entry_size)
1487 {
1488 	int q_depth = dev->q_depth;
1489 	unsigned q_size_aligned = roundup(q_depth * entry_size,
1490 					  NVME_CTRL_PAGE_SIZE);
1491 
1492 	if (q_size_aligned * nr_io_queues > dev->cmb_size) {
1493 		u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
1494 
1495 		mem_per_q = round_down(mem_per_q, NVME_CTRL_PAGE_SIZE);
1496 		q_depth = div_u64(mem_per_q, entry_size);
1497 
1498 		/*
1499 		 * Ensure the reduced q_depth is above some threshold where it
1500 		 * would be better to map queues in system memory with the
1501 		 * original depth
1502 		 */
1503 		if (q_depth < 64)
1504 			return -ENOMEM;
1505 	}
1506 
1507 	return q_depth;
1508 }
1509 
nvme_alloc_sq_cmds(struct nvme_dev * dev,struct nvme_queue * nvmeq,int qid)1510 static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1511 				int qid)
1512 {
1513 	struct pci_dev *pdev = to_pci_dev(dev->dev);
1514 
1515 	if (qid && dev->cmb_use_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) {
1516 		nvmeq->sq_cmds = pci_alloc_p2pmem(pdev, SQ_SIZE(nvmeq));
1517 		if (nvmeq->sq_cmds) {
1518 			nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
1519 							nvmeq->sq_cmds);
1520 			if (nvmeq->sq_dma_addr) {
1521 				set_bit(NVMEQ_SQ_CMB, &nvmeq->flags);
1522 				return 0;
1523 			}
1524 
1525 			pci_free_p2pmem(pdev, nvmeq->sq_cmds, SQ_SIZE(nvmeq));
1526 		}
1527 	}
1528 
1529 	nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(nvmeq),
1530 				&nvmeq->sq_dma_addr, GFP_KERNEL);
1531 	if (!nvmeq->sq_cmds)
1532 		return -ENOMEM;
1533 	return 0;
1534 }
1535 
nvme_alloc_queue(struct nvme_dev * dev,int qid,int depth)1536 static int nvme_alloc_queue(struct nvme_dev *dev, int qid, int depth)
1537 {
1538 	struct nvme_queue *nvmeq = &dev->queues[qid];
1539 
1540 	if (dev->ctrl.queue_count > qid)
1541 		return 0;
1542 
1543 	nvmeq->sqes = qid ? dev->io_sqes : NVME_ADM_SQES;
1544 	nvmeq->q_depth = depth;
1545 	nvmeq->cqes = dma_alloc_coherent(dev->dev, CQ_SIZE(nvmeq),
1546 					 &nvmeq->cq_dma_addr, GFP_KERNEL);
1547 	if (!nvmeq->cqes)
1548 		goto free_nvmeq;
1549 
1550 	if (nvme_alloc_sq_cmds(dev, nvmeq, qid))
1551 		goto free_cqdma;
1552 
1553 	nvmeq->dev = dev;
1554 	spin_lock_init(&nvmeq->sq_lock);
1555 	spin_lock_init(&nvmeq->cq_poll_lock);
1556 	nvmeq->cq_head = 0;
1557 	nvmeq->cq_phase = 1;
1558 	nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
1559 	nvmeq->qid = qid;
1560 	dev->ctrl.queue_count++;
1561 
1562 	return 0;
1563 
1564  free_cqdma:
1565 	dma_free_coherent(dev->dev, CQ_SIZE(nvmeq), (void *)nvmeq->cqes,
1566 			  nvmeq->cq_dma_addr);
1567  free_nvmeq:
1568 	return -ENOMEM;
1569 }
1570 
queue_request_irq(struct nvme_queue * nvmeq)1571 static int queue_request_irq(struct nvme_queue *nvmeq)
1572 {
1573 	struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev);
1574 	int nr = nvmeq->dev->ctrl.instance;
1575 
1576 	if (use_threaded_interrupts) {
1577 		return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq_check,
1578 				nvme_irq, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1579 	} else {
1580 		return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq,
1581 				NULL, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1582 	}
1583 }
1584 
nvme_init_queue(struct nvme_queue * nvmeq,u16 qid)1585 static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
1586 {
1587 	struct nvme_dev *dev = nvmeq->dev;
1588 
1589 	nvmeq->sq_tail = 0;
1590 	nvmeq->last_sq_tail = 0;
1591 	nvmeq->cq_head = 0;
1592 	nvmeq->cq_phase = 1;
1593 	nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
1594 	memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq));
1595 	nvme_dbbuf_init(dev, nvmeq, qid);
1596 	dev->online_queues++;
1597 	wmb(); /* ensure the first interrupt sees the initialization */
1598 }
1599 
1600 /*
1601  * Try getting shutdown_lock while setting up IO queues.
1602  */
nvme_setup_io_queues_trylock(struct nvme_dev * dev)1603 static int nvme_setup_io_queues_trylock(struct nvme_dev *dev)
1604 {
1605 	/*
1606 	 * Give up if the lock is being held by nvme_dev_disable.
1607 	 */
1608 	if (!mutex_trylock(&dev->shutdown_lock))
1609 		return -ENODEV;
1610 
1611 	/*
1612 	 * Controller is in wrong state, fail early.
1613 	 */
1614 	if (nvme_ctrl_state(&dev->ctrl) != NVME_CTRL_CONNECTING) {
1615 		mutex_unlock(&dev->shutdown_lock);
1616 		return -ENODEV;
1617 	}
1618 
1619 	return 0;
1620 }
1621 
nvme_create_queue(struct nvme_queue * nvmeq,int qid,bool polled)1622 static int nvme_create_queue(struct nvme_queue *nvmeq, int qid, bool polled)
1623 {
1624 	struct nvme_dev *dev = nvmeq->dev;
1625 	int result;
1626 	u16 vector = 0;
1627 
1628 	clear_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags);
1629 
1630 	/*
1631 	 * A queue's vector matches the queue identifier unless the controller
1632 	 * has only one vector available.
1633 	 */
1634 	if (!polled)
1635 		vector = dev->num_vecs == 1 ? 0 : qid;
1636 	else
1637 		set_bit(NVMEQ_POLLED, &nvmeq->flags);
1638 
1639 	result = adapter_alloc_cq(dev, qid, nvmeq, vector);
1640 	if (result)
1641 		return result;
1642 
1643 	result = adapter_alloc_sq(dev, qid, nvmeq);
1644 	if (result < 0)
1645 		return result;
1646 	if (result)
1647 		goto release_cq;
1648 
1649 	nvmeq->cq_vector = vector;
1650 
1651 	result = nvme_setup_io_queues_trylock(dev);
1652 	if (result)
1653 		return result;
1654 	nvme_init_queue(nvmeq, qid);
1655 	if (!polled) {
1656 		result = queue_request_irq(nvmeq);
1657 		if (result < 0)
1658 			goto release_sq;
1659 	}
1660 
1661 	set_bit(NVMEQ_ENABLED, &nvmeq->flags);
1662 	mutex_unlock(&dev->shutdown_lock);
1663 	return result;
1664 
1665 release_sq:
1666 	dev->online_queues--;
1667 	mutex_unlock(&dev->shutdown_lock);
1668 	adapter_delete_sq(dev, qid);
1669 release_cq:
1670 	adapter_delete_cq(dev, qid);
1671 	return result;
1672 }
1673 
1674 static const struct blk_mq_ops nvme_mq_admin_ops = {
1675 	.queue_rq	= nvme_queue_rq,
1676 	.complete	= nvme_pci_complete_rq,
1677 	.init_hctx	= nvme_admin_init_hctx,
1678 	.init_request	= nvme_pci_init_request,
1679 	.timeout	= nvme_timeout,
1680 };
1681 
1682 static const struct blk_mq_ops nvme_mq_ops = {
1683 	.queue_rq	= nvme_queue_rq,
1684 	.queue_rqs	= nvme_queue_rqs,
1685 	.complete	= nvme_pci_complete_rq,
1686 	.commit_rqs	= nvme_commit_rqs,
1687 	.init_hctx	= nvme_init_hctx,
1688 	.init_request	= nvme_pci_init_request,
1689 	.map_queues	= nvme_pci_map_queues,
1690 	.timeout	= nvme_timeout,
1691 	.poll		= nvme_poll,
1692 };
1693 
nvme_dev_remove_admin(struct nvme_dev * dev)1694 static void nvme_dev_remove_admin(struct nvme_dev *dev)
1695 {
1696 	if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
1697 		/*
1698 		 * If the controller was reset during removal, it's possible
1699 		 * user requests may be waiting on a stopped queue. Start the
1700 		 * queue to flush these to completion.
1701 		 */
1702 		nvme_unquiesce_admin_queue(&dev->ctrl);
1703 		nvme_remove_admin_tag_set(&dev->ctrl);
1704 	}
1705 }
1706 
db_bar_size(struct nvme_dev * dev,unsigned nr_io_queues)1707 static unsigned long db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1708 {
1709 	return NVME_REG_DBS + ((nr_io_queues + 1) * 8 * dev->db_stride);
1710 }
1711 
nvme_remap_bar(struct nvme_dev * dev,unsigned long size)1712 static int nvme_remap_bar(struct nvme_dev *dev, unsigned long size)
1713 {
1714 	struct pci_dev *pdev = to_pci_dev(dev->dev);
1715 
1716 	if (size <= dev->bar_mapped_size)
1717 		return 0;
1718 	if (size > pci_resource_len(pdev, 0))
1719 		return -ENOMEM;
1720 	if (dev->bar)
1721 		iounmap(dev->bar);
1722 	dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1723 	if (!dev->bar) {
1724 		dev->bar_mapped_size = 0;
1725 		return -ENOMEM;
1726 	}
1727 	dev->bar_mapped_size = size;
1728 	dev->dbs = dev->bar + NVME_REG_DBS;
1729 
1730 	return 0;
1731 }
1732 
nvme_pci_configure_admin_queue(struct nvme_dev * dev)1733 static int nvme_pci_configure_admin_queue(struct nvme_dev *dev)
1734 {
1735 	int result;
1736 	u32 aqa;
1737 	struct nvme_queue *nvmeq;
1738 
1739 	result = nvme_remap_bar(dev, db_bar_size(dev, 0));
1740 	if (result < 0)
1741 		return result;
1742 
1743 	dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1, 0) ?
1744 				NVME_CAP_NSSRC(dev->ctrl.cap) : 0;
1745 
1746 	if (dev->subsystem &&
1747 	    (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1748 		writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
1749 
1750 	/*
1751 	 * If the device has been passed off to us in an enabled state, just
1752 	 * clear the enabled bit.  The spec says we should set the 'shutdown
1753 	 * notification bits', but doing so may cause the device to complete
1754 	 * commands to the admin queue ... and we don't know what memory that
1755 	 * might be pointing at!
1756 	 */
1757 	result = nvme_disable_ctrl(&dev->ctrl, false);
1758 	if (result < 0)
1759 		return result;
1760 
1761 	result = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
1762 	if (result)
1763 		return result;
1764 
1765 	dev->ctrl.numa_node = dev_to_node(dev->dev);
1766 
1767 	nvmeq = &dev->queues[0];
1768 	aqa = nvmeq->q_depth - 1;
1769 	aqa |= aqa << 16;
1770 
1771 	writel(aqa, dev->bar + NVME_REG_AQA);
1772 	lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1773 	lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
1774 
1775 	result = nvme_enable_ctrl(&dev->ctrl);
1776 	if (result)
1777 		return result;
1778 
1779 	nvmeq->cq_vector = 0;
1780 	nvme_init_queue(nvmeq, 0);
1781 	result = queue_request_irq(nvmeq);
1782 	if (result) {
1783 		dev->online_queues--;
1784 		return result;
1785 	}
1786 
1787 	set_bit(NVMEQ_ENABLED, &nvmeq->flags);
1788 	return result;
1789 }
1790 
nvme_create_io_queues(struct nvme_dev * dev)1791 static int nvme_create_io_queues(struct nvme_dev *dev)
1792 {
1793 	unsigned i, max, rw_queues;
1794 	int ret = 0;
1795 
1796 	for (i = dev->ctrl.queue_count; i <= dev->max_qid; i++) {
1797 		if (nvme_alloc_queue(dev, i, dev->q_depth)) {
1798 			ret = -ENOMEM;
1799 			break;
1800 		}
1801 	}
1802 
1803 	max = min(dev->max_qid, dev->ctrl.queue_count - 1);
1804 	if (max != 1 && dev->io_queues[HCTX_TYPE_POLL]) {
1805 		rw_queues = dev->io_queues[HCTX_TYPE_DEFAULT] +
1806 				dev->io_queues[HCTX_TYPE_READ];
1807 	} else {
1808 		rw_queues = max;
1809 	}
1810 
1811 	for (i = dev->online_queues; i <= max; i++) {
1812 		bool polled = i > rw_queues;
1813 
1814 		ret = nvme_create_queue(&dev->queues[i], i, polled);
1815 		if (ret)
1816 			break;
1817 	}
1818 
1819 	/*
1820 	 * Ignore failing Create SQ/CQ commands, we can continue with less
1821 	 * than the desired amount of queues, and even a controller without
1822 	 * I/O queues can still be used to issue admin commands.  This might
1823 	 * be useful to upgrade a buggy firmware for example.
1824 	 */
1825 	return ret >= 0 ? 0 : ret;
1826 }
1827 
nvme_cmb_size_unit(struct nvme_dev * dev)1828 static u64 nvme_cmb_size_unit(struct nvme_dev *dev)
1829 {
1830 	u8 szu = (dev->cmbsz >> NVME_CMBSZ_SZU_SHIFT) & NVME_CMBSZ_SZU_MASK;
1831 
1832 	return 1ULL << (12 + 4 * szu);
1833 }
1834 
nvme_cmb_size(struct nvme_dev * dev)1835 static u32 nvme_cmb_size(struct nvme_dev *dev)
1836 {
1837 	return (dev->cmbsz >> NVME_CMBSZ_SZ_SHIFT) & NVME_CMBSZ_SZ_MASK;
1838 }
1839 
nvme_map_cmb(struct nvme_dev * dev)1840 static void nvme_map_cmb(struct nvme_dev *dev)
1841 {
1842 	u64 size, offset;
1843 	resource_size_t bar_size;
1844 	struct pci_dev *pdev = to_pci_dev(dev->dev);
1845 	int bar;
1846 
1847 	if (dev->cmb_size)
1848 		return;
1849 
1850 	if (NVME_CAP_CMBS(dev->ctrl.cap))
1851 		writel(NVME_CMBMSC_CRE, dev->bar + NVME_REG_CMBMSC);
1852 
1853 	dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
1854 	if (!dev->cmbsz)
1855 		return;
1856 	dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
1857 
1858 	size = nvme_cmb_size_unit(dev) * nvme_cmb_size(dev);
1859 	offset = nvme_cmb_size_unit(dev) * NVME_CMB_OFST(dev->cmbloc);
1860 	bar = NVME_CMB_BIR(dev->cmbloc);
1861 	bar_size = pci_resource_len(pdev, bar);
1862 
1863 	if (offset > bar_size)
1864 		return;
1865 
1866 	/*
1867 	 * Controllers may support a CMB size larger than their BAR, for
1868 	 * example, due to being behind a bridge. Reduce the CMB to the
1869 	 * reported size of the BAR
1870 	 */
1871 	size = min(size, bar_size - offset);
1872 
1873 	if (!IS_ALIGNED(size, memremap_compat_align()) ||
1874 	    !IS_ALIGNED(pci_resource_start(pdev, bar),
1875 			memremap_compat_align()))
1876 		return;
1877 
1878 	/*
1879 	 * Tell the controller about the host side address mapping the CMB,
1880 	 * and enable CMB decoding for the NVMe 1.4+ scheme:
1881 	 */
1882 	if (NVME_CAP_CMBS(dev->ctrl.cap)) {
1883 		hi_lo_writeq(NVME_CMBMSC_CRE | NVME_CMBMSC_CMSE |
1884 			     (pci_bus_address(pdev, bar) + offset),
1885 			     dev->bar + NVME_REG_CMBMSC);
1886 	}
1887 
1888 	if (pci_p2pdma_add_resource(pdev, bar, size, offset)) {
1889 		dev_warn(dev->ctrl.device,
1890 			 "failed to register the CMB\n");
1891 		hi_lo_writeq(0, dev->bar + NVME_REG_CMBMSC);
1892 		return;
1893 	}
1894 
1895 	dev->cmb_size = size;
1896 	dev->cmb_use_sqes = use_cmb_sqes && (dev->cmbsz & NVME_CMBSZ_SQS);
1897 
1898 	if ((dev->cmbsz & (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS)) ==
1899 			(NVME_CMBSZ_WDS | NVME_CMBSZ_RDS))
1900 		pci_p2pmem_publish(pdev, true);
1901 
1902 	nvme_update_attrs(dev);
1903 }
1904 
nvme_set_host_mem(struct nvme_dev * dev,u32 bits)1905 static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits)
1906 {
1907 	u32 host_mem_size = dev->host_mem_size >> NVME_CTRL_PAGE_SHIFT;
1908 	u64 dma_addr = dev->host_mem_descs_dma;
1909 	struct nvme_command c = { };
1910 	int ret;
1911 
1912 	c.features.opcode	= nvme_admin_set_features;
1913 	c.features.fid		= cpu_to_le32(NVME_FEAT_HOST_MEM_BUF);
1914 	c.features.dword11	= cpu_to_le32(bits);
1915 	c.features.dword12	= cpu_to_le32(host_mem_size);
1916 	c.features.dword13	= cpu_to_le32(lower_32_bits(dma_addr));
1917 	c.features.dword14	= cpu_to_le32(upper_32_bits(dma_addr));
1918 	c.features.dword15	= cpu_to_le32(dev->nr_host_mem_descs);
1919 
1920 	ret = nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1921 	if (ret) {
1922 		dev_warn(dev->ctrl.device,
1923 			 "failed to set host mem (err %d, flags %#x).\n",
1924 			 ret, bits);
1925 	} else
1926 		dev->hmb = bits & NVME_HOST_MEM_ENABLE;
1927 
1928 	return ret;
1929 }
1930 
nvme_free_host_mem(struct nvme_dev * dev)1931 static void nvme_free_host_mem(struct nvme_dev *dev)
1932 {
1933 	int i;
1934 
1935 	for (i = 0; i < dev->nr_host_mem_descs; i++) {
1936 		struct nvme_host_mem_buf_desc *desc = &dev->host_mem_descs[i];
1937 		size_t size = le32_to_cpu(desc->size) * NVME_CTRL_PAGE_SIZE;
1938 
1939 		dma_free_attrs(dev->dev, size, dev->host_mem_desc_bufs[i],
1940 			       le64_to_cpu(desc->addr),
1941 			       DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
1942 	}
1943 
1944 	kfree(dev->host_mem_desc_bufs);
1945 	dev->host_mem_desc_bufs = NULL;
1946 	dma_free_coherent(dev->dev, dev->host_mem_descs_size,
1947 			dev->host_mem_descs, dev->host_mem_descs_dma);
1948 	dev->host_mem_descs = NULL;
1949 	dev->host_mem_descs_size = 0;
1950 	dev->nr_host_mem_descs = 0;
1951 }
1952 
__nvme_alloc_host_mem(struct nvme_dev * dev,u64 preferred,u32 chunk_size)1953 static int __nvme_alloc_host_mem(struct nvme_dev *dev, u64 preferred,
1954 		u32 chunk_size)
1955 {
1956 	struct nvme_host_mem_buf_desc *descs;
1957 	u32 max_entries, len, descs_size;
1958 	dma_addr_t descs_dma;
1959 	int i = 0;
1960 	void **bufs;
1961 	u64 size, tmp;
1962 
1963 	tmp = (preferred + chunk_size - 1);
1964 	do_div(tmp, chunk_size);
1965 	max_entries = tmp;
1966 
1967 	if (dev->ctrl.hmmaxd && dev->ctrl.hmmaxd < max_entries)
1968 		max_entries = dev->ctrl.hmmaxd;
1969 
1970 	descs_size = max_entries * sizeof(*descs);
1971 	descs = dma_alloc_coherent(dev->dev, descs_size, &descs_dma,
1972 			GFP_KERNEL);
1973 	if (!descs)
1974 		goto out;
1975 
1976 	bufs = kcalloc(max_entries, sizeof(*bufs), GFP_KERNEL);
1977 	if (!bufs)
1978 		goto out_free_descs;
1979 
1980 	for (size = 0; size < preferred && i < max_entries; size += len) {
1981 		dma_addr_t dma_addr;
1982 
1983 		len = min_t(u64, chunk_size, preferred - size);
1984 		bufs[i] = dma_alloc_attrs(dev->dev, len, &dma_addr, GFP_KERNEL,
1985 				DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
1986 		if (!bufs[i])
1987 			break;
1988 
1989 		descs[i].addr = cpu_to_le64(dma_addr);
1990 		descs[i].size = cpu_to_le32(len / NVME_CTRL_PAGE_SIZE);
1991 		i++;
1992 	}
1993 
1994 	if (!size)
1995 		goto out_free_bufs;
1996 
1997 	dev->nr_host_mem_descs = i;
1998 	dev->host_mem_size = size;
1999 	dev->host_mem_descs = descs;
2000 	dev->host_mem_descs_dma = descs_dma;
2001 	dev->host_mem_descs_size = descs_size;
2002 	dev->host_mem_desc_bufs = bufs;
2003 	return 0;
2004 
2005 out_free_bufs:
2006 	while (--i >= 0) {
2007 		size_t size = le32_to_cpu(descs[i].size) * NVME_CTRL_PAGE_SIZE;
2008 
2009 		dma_free_attrs(dev->dev, size, bufs[i],
2010 			       le64_to_cpu(descs[i].addr),
2011 			       DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
2012 	}
2013 
2014 	kfree(bufs);
2015 out_free_descs:
2016 	dma_free_coherent(dev->dev, descs_size, descs, descs_dma);
2017 out:
2018 	dev->host_mem_descs = NULL;
2019 	return -ENOMEM;
2020 }
2021 
nvme_alloc_host_mem(struct nvme_dev * dev,u64 min,u64 preferred)2022 static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
2023 {
2024 	u64 min_chunk = min_t(u64, preferred, PAGE_SIZE * MAX_ORDER_NR_PAGES);
2025 	u64 hmminds = max_t(u32, dev->ctrl.hmminds * 4096, PAGE_SIZE * 2);
2026 	u64 chunk_size;
2027 
2028 	/* start big and work our way down */
2029 	for (chunk_size = min_chunk; chunk_size >= hmminds; chunk_size /= 2) {
2030 		if (!__nvme_alloc_host_mem(dev, preferred, chunk_size)) {
2031 			if (!min || dev->host_mem_size >= min)
2032 				return 0;
2033 			nvme_free_host_mem(dev);
2034 		}
2035 	}
2036 
2037 	return -ENOMEM;
2038 }
2039 
nvme_setup_host_mem(struct nvme_dev * dev)2040 static int nvme_setup_host_mem(struct nvme_dev *dev)
2041 {
2042 	u64 max = (u64)max_host_mem_size_mb * SZ_1M;
2043 	u64 preferred = (u64)dev->ctrl.hmpre * 4096;
2044 	u64 min = (u64)dev->ctrl.hmmin * 4096;
2045 	u32 enable_bits = NVME_HOST_MEM_ENABLE;
2046 	int ret;
2047 
2048 	if (!dev->ctrl.hmpre)
2049 		return 0;
2050 
2051 	preferred = min(preferred, max);
2052 	if (min > max) {
2053 		dev_warn(dev->ctrl.device,
2054 			"min host memory (%lld MiB) above limit (%d MiB).\n",
2055 			min >> ilog2(SZ_1M), max_host_mem_size_mb);
2056 		nvme_free_host_mem(dev);
2057 		return 0;
2058 	}
2059 
2060 	/*
2061 	 * If we already have a buffer allocated check if we can reuse it.
2062 	 */
2063 	if (dev->host_mem_descs) {
2064 		if (dev->host_mem_size >= min)
2065 			enable_bits |= NVME_HOST_MEM_RETURN;
2066 		else
2067 			nvme_free_host_mem(dev);
2068 	}
2069 
2070 	if (!dev->host_mem_descs) {
2071 		if (nvme_alloc_host_mem(dev, min, preferred)) {
2072 			dev_warn(dev->ctrl.device,
2073 				"failed to allocate host memory buffer.\n");
2074 			return 0; /* controller must work without HMB */
2075 		}
2076 
2077 		dev_info(dev->ctrl.device,
2078 			"allocated %lld MiB host memory buffer.\n",
2079 			dev->host_mem_size >> ilog2(SZ_1M));
2080 	}
2081 
2082 	ret = nvme_set_host_mem(dev, enable_bits);
2083 	if (ret)
2084 		nvme_free_host_mem(dev);
2085 	return ret;
2086 }
2087 
cmb_show(struct device * dev,struct device_attribute * attr,char * buf)2088 static ssize_t cmb_show(struct device *dev, struct device_attribute *attr,
2089 		char *buf)
2090 {
2091 	struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
2092 
2093 	return sysfs_emit(buf, "cmbloc : x%08x\ncmbsz  : x%08x\n",
2094 		       ndev->cmbloc, ndev->cmbsz);
2095 }
2096 static DEVICE_ATTR_RO(cmb);
2097 
cmbloc_show(struct device * dev,struct device_attribute * attr,char * buf)2098 static ssize_t cmbloc_show(struct device *dev, struct device_attribute *attr,
2099 		char *buf)
2100 {
2101 	struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
2102 
2103 	return sysfs_emit(buf, "%u\n", ndev->cmbloc);
2104 }
2105 static DEVICE_ATTR_RO(cmbloc);
2106 
cmbsz_show(struct device * dev,struct device_attribute * attr,char * buf)2107 static ssize_t cmbsz_show(struct device *dev, struct device_attribute *attr,
2108 		char *buf)
2109 {
2110 	struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
2111 
2112 	return sysfs_emit(buf, "%u\n", ndev->cmbsz);
2113 }
2114 static DEVICE_ATTR_RO(cmbsz);
2115 
hmb_show(struct device * dev,struct device_attribute * attr,char * buf)2116 static ssize_t hmb_show(struct device *dev, struct device_attribute *attr,
2117 			char *buf)
2118 {
2119 	struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
2120 
2121 	return sysfs_emit(buf, "%d\n", ndev->hmb);
2122 }
2123 
hmb_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2124 static ssize_t hmb_store(struct device *dev, struct device_attribute *attr,
2125 			 const char *buf, size_t count)
2126 {
2127 	struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
2128 	bool new;
2129 	int ret;
2130 
2131 	if (kstrtobool(buf, &new) < 0)
2132 		return -EINVAL;
2133 
2134 	if (new == ndev->hmb)
2135 		return count;
2136 
2137 	if (new) {
2138 		ret = nvme_setup_host_mem(ndev);
2139 	} else {
2140 		ret = nvme_set_host_mem(ndev, 0);
2141 		if (!ret)
2142 			nvme_free_host_mem(ndev);
2143 	}
2144 
2145 	if (ret < 0)
2146 		return ret;
2147 
2148 	return count;
2149 }
2150 static DEVICE_ATTR_RW(hmb);
2151 
nvme_pci_attrs_are_visible(struct kobject * kobj,struct attribute * a,int n)2152 static umode_t nvme_pci_attrs_are_visible(struct kobject *kobj,
2153 		struct attribute *a, int n)
2154 {
2155 	struct nvme_ctrl *ctrl =
2156 		dev_get_drvdata(container_of(kobj, struct device, kobj));
2157 	struct nvme_dev *dev = to_nvme_dev(ctrl);
2158 
2159 	if (a == &dev_attr_cmb.attr ||
2160 	    a == &dev_attr_cmbloc.attr ||
2161 	    a == &dev_attr_cmbsz.attr) {
2162 	    	if (!dev->cmbsz)
2163 			return 0;
2164 	}
2165 	if (a == &dev_attr_hmb.attr && !ctrl->hmpre)
2166 		return 0;
2167 
2168 	return a->mode;
2169 }
2170 
2171 static struct attribute *nvme_pci_attrs[] = {
2172 	&dev_attr_cmb.attr,
2173 	&dev_attr_cmbloc.attr,
2174 	&dev_attr_cmbsz.attr,
2175 	&dev_attr_hmb.attr,
2176 	NULL,
2177 };
2178 
2179 static const struct attribute_group nvme_pci_dev_attrs_group = {
2180 	.attrs		= nvme_pci_attrs,
2181 	.is_visible	= nvme_pci_attrs_are_visible,
2182 };
2183 
2184 static const struct attribute_group *nvme_pci_dev_attr_groups[] = {
2185 	&nvme_dev_attrs_group,
2186 	&nvme_pci_dev_attrs_group,
2187 	NULL,
2188 };
2189 
nvme_update_attrs(struct nvme_dev * dev)2190 static void nvme_update_attrs(struct nvme_dev *dev)
2191 {
2192 	sysfs_update_group(&dev->ctrl.device->kobj, &nvme_pci_dev_attrs_group);
2193 }
2194 
2195 /*
2196  * nirqs is the number of interrupts available for write and read
2197  * queues. The core already reserved an interrupt for the admin queue.
2198  */
nvme_calc_irq_sets(struct irq_affinity * affd,unsigned int nrirqs)2199 static void nvme_calc_irq_sets(struct irq_affinity *affd, unsigned int nrirqs)
2200 {
2201 	struct nvme_dev *dev = affd->priv;
2202 	unsigned int nr_read_queues, nr_write_queues = dev->nr_write_queues;
2203 
2204 	/*
2205 	 * If there is no interrupt available for queues, ensure that
2206 	 * the default queue is set to 1. The affinity set size is
2207 	 * also set to one, but the irq core ignores it for this case.
2208 	 *
2209 	 * If only one interrupt is available or 'write_queue' == 0, combine
2210 	 * write and read queues.
2211 	 *
2212 	 * If 'write_queues' > 0, ensure it leaves room for at least one read
2213 	 * queue.
2214 	 */
2215 	if (!nrirqs) {
2216 		nrirqs = 1;
2217 		nr_read_queues = 0;
2218 	} else if (nrirqs == 1 || !nr_write_queues) {
2219 		nr_read_queues = 0;
2220 	} else if (nr_write_queues >= nrirqs) {
2221 		nr_read_queues = 1;
2222 	} else {
2223 		nr_read_queues = nrirqs - nr_write_queues;
2224 	}
2225 
2226 	dev->io_queues[HCTX_TYPE_DEFAULT] = nrirqs - nr_read_queues;
2227 	affd->set_size[HCTX_TYPE_DEFAULT] = nrirqs - nr_read_queues;
2228 	dev->io_queues[HCTX_TYPE_READ] = nr_read_queues;
2229 	affd->set_size[HCTX_TYPE_READ] = nr_read_queues;
2230 	affd->nr_sets = nr_read_queues ? 2 : 1;
2231 }
2232 
nvme_setup_irqs(struct nvme_dev * dev,unsigned int nr_io_queues)2233 static int nvme_setup_irqs(struct nvme_dev *dev, unsigned int nr_io_queues)
2234 {
2235 	struct pci_dev *pdev = to_pci_dev(dev->dev);
2236 	struct irq_affinity affd = {
2237 		.pre_vectors	= 1,
2238 		.calc_sets	= nvme_calc_irq_sets,
2239 		.priv		= dev,
2240 	};
2241 	unsigned int irq_queues, poll_queues;
2242 	unsigned int flags = PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY;
2243 
2244 	/*
2245 	 * Poll queues don't need interrupts, but we need at least one I/O queue
2246 	 * left over for non-polled I/O.
2247 	 */
2248 	poll_queues = min(dev->nr_poll_queues, nr_io_queues - 1);
2249 	dev->io_queues[HCTX_TYPE_POLL] = poll_queues;
2250 
2251 	/*
2252 	 * Initialize for the single interrupt case, will be updated in
2253 	 * nvme_calc_irq_sets().
2254 	 */
2255 	dev->io_queues[HCTX_TYPE_DEFAULT] = 1;
2256 	dev->io_queues[HCTX_TYPE_READ] = 0;
2257 
2258 	/*
2259 	 * We need interrupts for the admin queue and each non-polled I/O queue,
2260 	 * but some Apple controllers require all queues to use the first
2261 	 * vector.
2262 	 */
2263 	irq_queues = 1;
2264 	if (!(dev->ctrl.quirks & NVME_QUIRK_SINGLE_VECTOR))
2265 		irq_queues += (nr_io_queues - poll_queues);
2266 	if (dev->ctrl.quirks & NVME_QUIRK_BROKEN_MSI)
2267 		flags &= ~PCI_IRQ_MSI;
2268 	return pci_alloc_irq_vectors_affinity(pdev, 1, irq_queues, flags,
2269 					      &affd);
2270 }
2271 
nvme_max_io_queues(struct nvme_dev * dev)2272 static unsigned int nvme_max_io_queues(struct nvme_dev *dev)
2273 {
2274 	/*
2275 	 * If tags are shared with admin queue (Apple bug), then
2276 	 * make sure we only use one IO queue.
2277 	 */
2278 	if (dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS)
2279 		return 1;
2280 	return num_possible_cpus() + dev->nr_write_queues + dev->nr_poll_queues;
2281 }
2282 
nvme_setup_io_queues(struct nvme_dev * dev)2283 static int nvme_setup_io_queues(struct nvme_dev *dev)
2284 {
2285 	struct nvme_queue *adminq = &dev->queues[0];
2286 	struct pci_dev *pdev = to_pci_dev(dev->dev);
2287 	unsigned int nr_io_queues;
2288 	unsigned long size;
2289 	int result;
2290 
2291 	/*
2292 	 * Sample the module parameters once at reset time so that we have
2293 	 * stable values to work with.
2294 	 */
2295 	dev->nr_write_queues = write_queues;
2296 	dev->nr_poll_queues = poll_queues;
2297 
2298 	nr_io_queues = dev->nr_allocated_queues - 1;
2299 	result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
2300 	if (result < 0)
2301 		return result;
2302 
2303 	if (nr_io_queues == 0)
2304 		return 0;
2305 
2306 	/*
2307 	 * Free IRQ resources as soon as NVMEQ_ENABLED bit transitions
2308 	 * from set to unset. If there is a window to it is truely freed,
2309 	 * pci_free_irq_vectors() jumping into this window will crash.
2310 	 * And take lock to avoid racing with pci_free_irq_vectors() in
2311 	 * nvme_dev_disable() path.
2312 	 */
2313 	result = nvme_setup_io_queues_trylock(dev);
2314 	if (result)
2315 		return result;
2316 	if (test_and_clear_bit(NVMEQ_ENABLED, &adminq->flags))
2317 		pci_free_irq(pdev, 0, adminq);
2318 
2319 	if (dev->cmb_use_sqes) {
2320 		result = nvme_cmb_qdepth(dev, nr_io_queues,
2321 				sizeof(struct nvme_command));
2322 		if (result > 0) {
2323 			dev->q_depth = result;
2324 			dev->ctrl.sqsize = result - 1;
2325 		} else {
2326 			dev->cmb_use_sqes = false;
2327 		}
2328 	}
2329 
2330 	do {
2331 		size = db_bar_size(dev, nr_io_queues);
2332 		result = nvme_remap_bar(dev, size);
2333 		if (!result)
2334 			break;
2335 		if (!--nr_io_queues) {
2336 			result = -ENOMEM;
2337 			goto out_unlock;
2338 		}
2339 	} while (1);
2340 	adminq->q_db = dev->dbs;
2341 
2342  retry:
2343 	/* Deregister the admin queue's interrupt */
2344 	if (test_and_clear_bit(NVMEQ_ENABLED, &adminq->flags))
2345 		pci_free_irq(pdev, 0, adminq);
2346 
2347 	/*
2348 	 * If we enable msix early due to not intx, disable it again before
2349 	 * setting up the full range we need.
2350 	 */
2351 	pci_free_irq_vectors(pdev);
2352 
2353 	result = nvme_setup_irqs(dev, nr_io_queues);
2354 	if (result <= 0) {
2355 		result = -EIO;
2356 		goto out_unlock;
2357 	}
2358 
2359 	dev->num_vecs = result;
2360 	result = max(result - 1, 1);
2361 	dev->max_qid = result + dev->io_queues[HCTX_TYPE_POLL];
2362 
2363 	/*
2364 	 * Should investigate if there's a performance win from allocating
2365 	 * more queues than interrupt vectors; it might allow the submission
2366 	 * path to scale better, even if the receive path is limited by the
2367 	 * number of interrupts.
2368 	 */
2369 	result = queue_request_irq(adminq);
2370 	if (result)
2371 		goto out_unlock;
2372 	set_bit(NVMEQ_ENABLED, &adminq->flags);
2373 	mutex_unlock(&dev->shutdown_lock);
2374 
2375 	result = nvme_create_io_queues(dev);
2376 	if (result || dev->online_queues < 2)
2377 		return result;
2378 
2379 	if (dev->online_queues - 1 < dev->max_qid) {
2380 		nr_io_queues = dev->online_queues - 1;
2381 		nvme_delete_io_queues(dev);
2382 		result = nvme_setup_io_queues_trylock(dev);
2383 		if (result)
2384 			return result;
2385 		nvme_suspend_io_queues(dev);
2386 		goto retry;
2387 	}
2388 	dev_info(dev->ctrl.device, "%d/%d/%d default/read/poll queues\n",
2389 					dev->io_queues[HCTX_TYPE_DEFAULT],
2390 					dev->io_queues[HCTX_TYPE_READ],
2391 					dev->io_queues[HCTX_TYPE_POLL]);
2392 	return 0;
2393 out_unlock:
2394 	mutex_unlock(&dev->shutdown_lock);
2395 	return result;
2396 }
2397 
nvme_del_queue_end(struct request * req,blk_status_t error)2398 static enum rq_end_io_ret nvme_del_queue_end(struct request *req,
2399 					     blk_status_t error)
2400 {
2401 	struct nvme_queue *nvmeq = req->end_io_data;
2402 
2403 	blk_mq_free_request(req);
2404 	complete(&nvmeq->delete_done);
2405 	return RQ_END_IO_NONE;
2406 }
2407 
nvme_del_cq_end(struct request * req,blk_status_t error)2408 static enum rq_end_io_ret nvme_del_cq_end(struct request *req,
2409 					  blk_status_t error)
2410 {
2411 	struct nvme_queue *nvmeq = req->end_io_data;
2412 
2413 	if (error)
2414 		set_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags);
2415 
2416 	return nvme_del_queue_end(req, error);
2417 }
2418 
nvme_delete_queue(struct nvme_queue * nvmeq,u8 opcode)2419 static int nvme_delete_queue(struct nvme_queue *nvmeq, u8 opcode)
2420 {
2421 	struct request_queue *q = nvmeq->dev->ctrl.admin_q;
2422 	struct request *req;
2423 	struct nvme_command cmd = { };
2424 
2425 	cmd.delete_queue.opcode = opcode;
2426 	cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2427 
2428 	req = blk_mq_alloc_request(q, nvme_req_op(&cmd), BLK_MQ_REQ_NOWAIT);
2429 	if (IS_ERR(req))
2430 		return PTR_ERR(req);
2431 	nvme_init_request(req, &cmd);
2432 
2433 	if (opcode == nvme_admin_delete_cq)
2434 		req->end_io = nvme_del_cq_end;
2435 	else
2436 		req->end_io = nvme_del_queue_end;
2437 	req->end_io_data = nvmeq;
2438 
2439 	init_completion(&nvmeq->delete_done);
2440 	blk_execute_rq_nowait(req, false);
2441 	return 0;
2442 }
2443 
__nvme_delete_io_queues(struct nvme_dev * dev,u8 opcode)2444 static bool __nvme_delete_io_queues(struct nvme_dev *dev, u8 opcode)
2445 {
2446 	int nr_queues = dev->online_queues - 1, sent = 0;
2447 	unsigned long timeout;
2448 
2449  retry:
2450 	timeout = NVME_ADMIN_TIMEOUT;
2451 	while (nr_queues > 0) {
2452 		if (nvme_delete_queue(&dev->queues[nr_queues], opcode))
2453 			break;
2454 		nr_queues--;
2455 		sent++;
2456 	}
2457 	while (sent) {
2458 		struct nvme_queue *nvmeq = &dev->queues[nr_queues + sent];
2459 
2460 		timeout = wait_for_completion_io_timeout(&nvmeq->delete_done,
2461 				timeout);
2462 		if (timeout == 0)
2463 			return false;
2464 
2465 		sent--;
2466 		if (nr_queues)
2467 			goto retry;
2468 	}
2469 	return true;
2470 }
2471 
nvme_delete_io_queues(struct nvme_dev * dev)2472 static void nvme_delete_io_queues(struct nvme_dev *dev)
2473 {
2474 	if (__nvme_delete_io_queues(dev, nvme_admin_delete_sq))
2475 		__nvme_delete_io_queues(dev, nvme_admin_delete_cq);
2476 }
2477 
nvme_pci_nr_maps(struct nvme_dev * dev)2478 static unsigned int nvme_pci_nr_maps(struct nvme_dev *dev)
2479 {
2480 	if (dev->io_queues[HCTX_TYPE_POLL])
2481 		return 3;
2482 	if (dev->io_queues[HCTX_TYPE_READ])
2483 		return 2;
2484 	return 1;
2485 }
2486 
nvme_pci_update_nr_queues(struct nvme_dev * dev)2487 static bool nvme_pci_update_nr_queues(struct nvme_dev *dev)
2488 {
2489 	if (!dev->ctrl.tagset) {
2490 		nvme_alloc_io_tag_set(&dev->ctrl, &dev->tagset, &nvme_mq_ops,
2491 				nvme_pci_nr_maps(dev), sizeof(struct nvme_iod));
2492 		return true;
2493 	}
2494 
2495 	/* Give up if we are racing with nvme_dev_disable() */
2496 	if (!mutex_trylock(&dev->shutdown_lock))
2497 		return false;
2498 
2499 	/* Check if nvme_dev_disable() has been executed already */
2500 	if (!dev->online_queues) {
2501 		mutex_unlock(&dev->shutdown_lock);
2502 		return false;
2503 	}
2504 
2505 	blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1);
2506 	/* free previously allocated queues that are no longer usable */
2507 	nvme_free_queues(dev, dev->online_queues);
2508 	mutex_unlock(&dev->shutdown_lock);
2509 	return true;
2510 }
2511 
nvme_pci_enable(struct nvme_dev * dev)2512 static int nvme_pci_enable(struct nvme_dev *dev)
2513 {
2514 	int result = -ENOMEM;
2515 	struct pci_dev *pdev = to_pci_dev(dev->dev);
2516 	unsigned int flags = PCI_IRQ_ALL_TYPES;
2517 
2518 	if (pci_enable_device_mem(pdev))
2519 		return result;
2520 
2521 	pci_set_master(pdev);
2522 
2523 	if (readl(dev->bar + NVME_REG_CSTS) == -1) {
2524 		result = -ENODEV;
2525 		goto disable;
2526 	}
2527 
2528 	/*
2529 	 * Some devices and/or platforms don't advertise or work with INTx
2530 	 * interrupts. Pre-enable a single MSIX or MSI vec for setup. We'll
2531 	 * adjust this later.
2532 	 */
2533 	if (dev->ctrl.quirks & NVME_QUIRK_BROKEN_MSI)
2534 		flags &= ~PCI_IRQ_MSI;
2535 	result = pci_alloc_irq_vectors(pdev, 1, 1, flags);
2536 	if (result < 0)
2537 		goto disable;
2538 
2539 	dev->ctrl.cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
2540 
2541 	dev->q_depth = min_t(u32, NVME_CAP_MQES(dev->ctrl.cap) + 1,
2542 				io_queue_depth);
2543 	dev->db_stride = 1 << NVME_CAP_STRIDE(dev->ctrl.cap);
2544 	dev->dbs = dev->bar + 4096;
2545 
2546 	/*
2547 	 * Some Apple controllers require a non-standard SQE size.
2548 	 * Interestingly they also seem to ignore the CC:IOSQES register
2549 	 * so we don't bother updating it here.
2550 	 */
2551 	if (dev->ctrl.quirks & NVME_QUIRK_128_BYTES_SQES)
2552 		dev->io_sqes = 7;
2553 	else
2554 		dev->io_sqes = NVME_NVM_IOSQES;
2555 
2556 	if (dev->ctrl.quirks & NVME_QUIRK_QDEPTH_ONE) {
2557 		dev->q_depth = 2;
2558 	} else if (pdev->vendor == PCI_VENDOR_ID_SAMSUNG &&
2559 		   (pdev->device == 0xa821 || pdev->device == 0xa822) &&
2560 		   NVME_CAP_MQES(dev->ctrl.cap) == 0) {
2561 		dev->q_depth = 64;
2562 		dev_err(dev->ctrl.device, "detected PM1725 NVMe controller, "
2563                         "set queue depth=%u\n", dev->q_depth);
2564 	}
2565 
2566 	/*
2567 	 * Controllers with the shared tags quirk need the IO queue to be
2568 	 * big enough so that we get 32 tags for the admin queue
2569 	 */
2570 	if ((dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS) &&
2571 	    (dev->q_depth < (NVME_AQ_DEPTH + 2))) {
2572 		dev->q_depth = NVME_AQ_DEPTH + 2;
2573 		dev_warn(dev->ctrl.device, "IO queue depth clamped to %d\n",
2574 			 dev->q_depth);
2575 	}
2576 	dev->ctrl.sqsize = dev->q_depth - 1; /* 0's based queue depth */
2577 
2578 	nvme_map_cmb(dev);
2579 
2580 	pci_save_state(pdev);
2581 
2582 	result = nvme_pci_configure_admin_queue(dev);
2583 	if (result)
2584 		goto free_irq;
2585 	return result;
2586 
2587  free_irq:
2588 	pci_free_irq_vectors(pdev);
2589  disable:
2590 	pci_disable_device(pdev);
2591 	return result;
2592 }
2593 
nvme_dev_unmap(struct nvme_dev * dev)2594 static void nvme_dev_unmap(struct nvme_dev *dev)
2595 {
2596 	if (dev->bar)
2597 		iounmap(dev->bar);
2598 	pci_release_mem_regions(to_pci_dev(dev->dev));
2599 }
2600 
nvme_pci_ctrl_is_dead(struct nvme_dev * dev)2601 static bool nvme_pci_ctrl_is_dead(struct nvme_dev *dev)
2602 {
2603 	struct pci_dev *pdev = to_pci_dev(dev->dev);
2604 	u32 csts;
2605 
2606 	if (!pci_is_enabled(pdev) || !pci_device_is_present(pdev))
2607 		return true;
2608 	if (pdev->error_state != pci_channel_io_normal)
2609 		return true;
2610 
2611 	csts = readl(dev->bar + NVME_REG_CSTS);
2612 	return (csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY);
2613 }
2614 
nvme_dev_disable(struct nvme_dev * dev,bool shutdown)2615 static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
2616 {
2617 	enum nvme_ctrl_state state = nvme_ctrl_state(&dev->ctrl);
2618 	struct pci_dev *pdev = to_pci_dev(dev->dev);
2619 	bool dead;
2620 
2621 	mutex_lock(&dev->shutdown_lock);
2622 	dead = nvme_pci_ctrl_is_dead(dev);
2623 	if (state == NVME_CTRL_LIVE || state == NVME_CTRL_RESETTING) {
2624 		if (pci_is_enabled(pdev))
2625 			nvme_start_freeze(&dev->ctrl);
2626 		/*
2627 		 * Give the controller a chance to complete all entered requests
2628 		 * if doing a safe shutdown.
2629 		 */
2630 		if (!dead && shutdown)
2631 			nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
2632 	}
2633 
2634 	nvme_quiesce_io_queues(&dev->ctrl);
2635 
2636 	if (!dead && dev->ctrl.queue_count > 0) {
2637 		nvme_delete_io_queues(dev);
2638 		nvme_disable_ctrl(&dev->ctrl, shutdown);
2639 		nvme_poll_irqdisable(&dev->queues[0]);
2640 	}
2641 	nvme_suspend_io_queues(dev);
2642 	nvme_suspend_queue(dev, 0);
2643 	pci_free_irq_vectors(pdev);
2644 	if (pci_is_enabled(pdev))
2645 		pci_disable_device(pdev);
2646 	nvme_reap_pending_cqes(dev);
2647 
2648 	nvme_cancel_tagset(&dev->ctrl);
2649 	nvme_cancel_admin_tagset(&dev->ctrl);
2650 
2651 	/*
2652 	 * The driver will not be starting up queues again if shutting down so
2653 	 * must flush all entered requests to their failed completion to avoid
2654 	 * deadlocking blk-mq hot-cpu notifier.
2655 	 */
2656 	if (shutdown) {
2657 		nvme_unquiesce_io_queues(&dev->ctrl);
2658 		if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q))
2659 			nvme_unquiesce_admin_queue(&dev->ctrl);
2660 	}
2661 	mutex_unlock(&dev->shutdown_lock);
2662 }
2663 
nvme_disable_prepare_reset(struct nvme_dev * dev,bool shutdown)2664 static int nvme_disable_prepare_reset(struct nvme_dev *dev, bool shutdown)
2665 {
2666 	if (!nvme_wait_reset(&dev->ctrl))
2667 		return -EBUSY;
2668 	nvme_dev_disable(dev, shutdown);
2669 	return 0;
2670 }
2671 
nvme_setup_prp_pools(struct nvme_dev * dev)2672 static int nvme_setup_prp_pools(struct nvme_dev *dev)
2673 {
2674 	size_t small_align = 256;
2675 
2676 	dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
2677 						NVME_CTRL_PAGE_SIZE,
2678 						NVME_CTRL_PAGE_SIZE, 0);
2679 	if (!dev->prp_page_pool)
2680 		return -ENOMEM;
2681 
2682 	if (dev->ctrl.quirks & NVME_QUIRK_DMAPOOL_ALIGN_512)
2683 		small_align = 512;
2684 
2685 	/* Optimisation for I/Os between 4k and 128k */
2686 	dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
2687 						256, small_align, 0);
2688 	if (!dev->prp_small_pool) {
2689 		dma_pool_destroy(dev->prp_page_pool);
2690 		return -ENOMEM;
2691 	}
2692 	return 0;
2693 }
2694 
nvme_release_prp_pools(struct nvme_dev * dev)2695 static void nvme_release_prp_pools(struct nvme_dev *dev)
2696 {
2697 	dma_pool_destroy(dev->prp_page_pool);
2698 	dma_pool_destroy(dev->prp_small_pool);
2699 }
2700 
nvme_pci_alloc_iod_mempool(struct nvme_dev * dev)2701 static int nvme_pci_alloc_iod_mempool(struct nvme_dev *dev)
2702 {
2703 	size_t alloc_size = sizeof(struct scatterlist) * NVME_MAX_SEGS;
2704 
2705 	dev->iod_mempool = mempool_create_node(1,
2706 			mempool_kmalloc, mempool_kfree,
2707 			(void *)alloc_size, GFP_KERNEL,
2708 			dev_to_node(dev->dev));
2709 	if (!dev->iod_mempool)
2710 		return -ENOMEM;
2711 	return 0;
2712 }
2713 
nvme_free_tagset(struct nvme_dev * dev)2714 static void nvme_free_tagset(struct nvme_dev *dev)
2715 {
2716 	if (dev->tagset.tags)
2717 		nvme_remove_io_tag_set(&dev->ctrl);
2718 	dev->ctrl.tagset = NULL;
2719 }
2720 
2721 /* pairs with nvme_pci_alloc_dev */
nvme_pci_free_ctrl(struct nvme_ctrl * ctrl)2722 static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
2723 {
2724 	struct nvme_dev *dev = to_nvme_dev(ctrl);
2725 
2726 	nvme_free_tagset(dev);
2727 	put_device(dev->dev);
2728 	kfree(dev->queues);
2729 	kfree(dev);
2730 }
2731 
nvme_reset_work(struct work_struct * work)2732 static void nvme_reset_work(struct work_struct *work)
2733 {
2734 	struct nvme_dev *dev =
2735 		container_of(work, struct nvme_dev, ctrl.reset_work);
2736 	bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
2737 	int result;
2738 
2739 	if (nvme_ctrl_state(&dev->ctrl) != NVME_CTRL_RESETTING) {
2740 		dev_warn(dev->ctrl.device, "ctrl state %d is not RESETTING\n",
2741 			 dev->ctrl.state);
2742 		result = -ENODEV;
2743 		goto out;
2744 	}
2745 
2746 	/*
2747 	 * If we're called to reset a live controller first shut it down before
2748 	 * moving on.
2749 	 */
2750 	if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
2751 		nvme_dev_disable(dev, false);
2752 	nvme_sync_queues(&dev->ctrl);
2753 
2754 	mutex_lock(&dev->shutdown_lock);
2755 	result = nvme_pci_enable(dev);
2756 	if (result)
2757 		goto out_unlock;
2758 	nvme_unquiesce_admin_queue(&dev->ctrl);
2759 	mutex_unlock(&dev->shutdown_lock);
2760 
2761 	/*
2762 	 * Introduce CONNECTING state from nvme-fc/rdma transports to mark the
2763 	 * initializing procedure here.
2764 	 */
2765 	if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_CONNECTING)) {
2766 		dev_warn(dev->ctrl.device,
2767 			"failed to mark controller CONNECTING\n");
2768 		result = -EBUSY;
2769 		goto out;
2770 	}
2771 
2772 	result = nvme_init_ctrl_finish(&dev->ctrl, was_suspend);
2773 	if (result)
2774 		goto out;
2775 
2776 	nvme_dbbuf_dma_alloc(dev);
2777 
2778 	result = nvme_setup_host_mem(dev);
2779 	if (result < 0)
2780 		goto out;
2781 
2782 	result = nvme_setup_io_queues(dev);
2783 	if (result)
2784 		goto out;
2785 
2786 	/*
2787 	 * Freeze and update the number of I/O queues as thos might have
2788 	 * changed.  If there are no I/O queues left after this reset, keep the
2789 	 * controller around but remove all namespaces.
2790 	 */
2791 	if (dev->online_queues > 1) {
2792 		nvme_dbbuf_set(dev);
2793 		nvme_unquiesce_io_queues(&dev->ctrl);
2794 		nvme_wait_freeze(&dev->ctrl);
2795 		if (!nvme_pci_update_nr_queues(dev))
2796 			goto out;
2797 		nvme_unfreeze(&dev->ctrl);
2798 	} else {
2799 		dev_warn(dev->ctrl.device, "IO queues lost\n");
2800 		nvme_mark_namespaces_dead(&dev->ctrl);
2801 		nvme_unquiesce_io_queues(&dev->ctrl);
2802 		nvme_remove_namespaces(&dev->ctrl);
2803 		nvme_free_tagset(dev);
2804 	}
2805 
2806 	/*
2807 	 * If only admin queue live, keep it to do further investigation or
2808 	 * recovery.
2809 	 */
2810 	if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) {
2811 		dev_warn(dev->ctrl.device,
2812 			"failed to mark controller live state\n");
2813 		result = -ENODEV;
2814 		goto out;
2815 	}
2816 
2817 	nvme_start_ctrl(&dev->ctrl);
2818 	return;
2819 
2820  out_unlock:
2821 	mutex_unlock(&dev->shutdown_lock);
2822  out:
2823 	/*
2824 	 * Set state to deleting now to avoid blocking nvme_wait_reset(), which
2825 	 * may be holding this pci_dev's device lock.
2826 	 */
2827 	dev_warn(dev->ctrl.device, "Disabling device after reset failure: %d\n",
2828 		 result);
2829 	nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
2830 	nvme_dev_disable(dev, true);
2831 	nvme_sync_queues(&dev->ctrl);
2832 	nvme_mark_namespaces_dead(&dev->ctrl);
2833 	nvme_unquiesce_io_queues(&dev->ctrl);
2834 	nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD);
2835 }
2836 
nvme_pci_reg_read32(struct nvme_ctrl * ctrl,u32 off,u32 * val)2837 static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
2838 {
2839 	*val = readl(to_nvme_dev(ctrl)->bar + off);
2840 	return 0;
2841 }
2842 
nvme_pci_reg_write32(struct nvme_ctrl * ctrl,u32 off,u32 val)2843 static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2844 {
2845 	writel(val, to_nvme_dev(ctrl)->bar + off);
2846 	return 0;
2847 }
2848 
nvme_pci_reg_read64(struct nvme_ctrl * ctrl,u32 off,u64 * val)2849 static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2850 {
2851 	*val = lo_hi_readq(to_nvme_dev(ctrl)->bar + off);
2852 	return 0;
2853 }
2854 
nvme_pci_get_address(struct nvme_ctrl * ctrl,char * buf,int size)2855 static int nvme_pci_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
2856 {
2857 	struct pci_dev *pdev = to_pci_dev(to_nvme_dev(ctrl)->dev);
2858 
2859 	return snprintf(buf, size, "%s\n", dev_name(&pdev->dev));
2860 }
2861 
nvme_pci_print_device_info(struct nvme_ctrl * ctrl)2862 static void nvme_pci_print_device_info(struct nvme_ctrl *ctrl)
2863 {
2864 	struct pci_dev *pdev = to_pci_dev(to_nvme_dev(ctrl)->dev);
2865 	struct nvme_subsystem *subsys = ctrl->subsys;
2866 
2867 	dev_err(ctrl->device,
2868 		"VID:DID %04x:%04x model:%.*s firmware:%.*s\n",
2869 		pdev->vendor, pdev->device,
2870 		nvme_strlen(subsys->model, sizeof(subsys->model)),
2871 		subsys->model, nvme_strlen(subsys->firmware_rev,
2872 					   sizeof(subsys->firmware_rev)),
2873 		subsys->firmware_rev);
2874 }
2875 
nvme_pci_supports_pci_p2pdma(struct nvme_ctrl * ctrl)2876 static bool nvme_pci_supports_pci_p2pdma(struct nvme_ctrl *ctrl)
2877 {
2878 	struct nvme_dev *dev = to_nvme_dev(ctrl);
2879 
2880 	return dma_pci_p2pdma_supported(dev->dev);
2881 }
2882 
2883 static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
2884 	.name			= "pcie",
2885 	.module			= THIS_MODULE,
2886 	.flags			= NVME_F_METADATA_SUPPORTED,
2887 	.dev_attr_groups	= nvme_pci_dev_attr_groups,
2888 	.reg_read32		= nvme_pci_reg_read32,
2889 	.reg_write32		= nvme_pci_reg_write32,
2890 	.reg_read64		= nvme_pci_reg_read64,
2891 	.free_ctrl		= nvme_pci_free_ctrl,
2892 	.submit_async_event	= nvme_pci_submit_async_event,
2893 	.get_address		= nvme_pci_get_address,
2894 	.print_device_info	= nvme_pci_print_device_info,
2895 	.supports_pci_p2pdma	= nvme_pci_supports_pci_p2pdma,
2896 };
2897 
nvme_dev_map(struct nvme_dev * dev)2898 static int nvme_dev_map(struct nvme_dev *dev)
2899 {
2900 	struct pci_dev *pdev = to_pci_dev(dev->dev);
2901 
2902 	if (pci_request_mem_regions(pdev, "nvme"))
2903 		return -ENODEV;
2904 
2905 	if (nvme_remap_bar(dev, NVME_REG_DBS + 4096))
2906 		goto release;
2907 
2908 	return 0;
2909   release:
2910 	pci_release_mem_regions(pdev);
2911 	return -ENODEV;
2912 }
2913 
check_vendor_combination_bug(struct pci_dev * pdev)2914 static unsigned long check_vendor_combination_bug(struct pci_dev *pdev)
2915 {
2916 	if (pdev->vendor == 0x144d && pdev->device == 0xa802) {
2917 		/*
2918 		 * Several Samsung devices seem to drop off the PCIe bus
2919 		 * randomly when APST is on and uses the deepest sleep state.
2920 		 * This has been observed on a Samsung "SM951 NVMe SAMSUNG
2921 		 * 256GB", a "PM951 NVMe SAMSUNG 512GB", and a "Samsung SSD
2922 		 * 950 PRO 256GB", but it seems to be restricted to two Dell
2923 		 * laptops.
2924 		 */
2925 		if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.") &&
2926 		    (dmi_match(DMI_PRODUCT_NAME, "XPS 15 9550") ||
2927 		     dmi_match(DMI_PRODUCT_NAME, "Precision 5510")))
2928 			return NVME_QUIRK_NO_DEEPEST_PS;
2929 	} else if (pdev->vendor == 0x144d && pdev->device == 0xa804) {
2930 		/*
2931 		 * Samsung SSD 960 EVO drops off the PCIe bus after system
2932 		 * suspend on a Ryzen board, ASUS PRIME B350M-A, as well as
2933 		 * within few minutes after bootup on a Coffee Lake board -
2934 		 * ASUS PRIME Z370-A
2935 		 */
2936 		if (dmi_match(DMI_BOARD_VENDOR, "ASUSTeK COMPUTER INC.") &&
2937 		    (dmi_match(DMI_BOARD_NAME, "PRIME B350M-A") ||
2938 		     dmi_match(DMI_BOARD_NAME, "PRIME Z370-A")))
2939 			return NVME_QUIRK_NO_APST;
2940 	} else if ((pdev->vendor == 0x144d && (pdev->device == 0xa801 ||
2941 		    pdev->device == 0xa808 || pdev->device == 0xa809)) ||
2942 		   (pdev->vendor == 0x1e0f && pdev->device == 0x0001)) {
2943 		/*
2944 		 * Forcing to use host managed nvme power settings for
2945 		 * lowest idle power with quick resume latency on
2946 		 * Samsung and Toshiba SSDs based on suspend behavior
2947 		 * on Coffee Lake board for LENOVO C640
2948 		 */
2949 		if ((dmi_match(DMI_BOARD_VENDOR, "LENOVO")) &&
2950 		     dmi_match(DMI_BOARD_NAME, "LNVNB161216"))
2951 			return NVME_QUIRK_SIMPLE_SUSPEND;
2952 	} else if (pdev->vendor == 0x2646 && (pdev->device == 0x2263 ||
2953 		   pdev->device == 0x500f)) {
2954 		/*
2955 		 * Exclude some Kingston NV1 and A2000 devices from
2956 		 * NVME_QUIRK_SIMPLE_SUSPEND. Do a full suspend to save a
2957 		 * lot fo energy with s2idle sleep on some TUXEDO platforms.
2958 		 */
2959 		if (dmi_match(DMI_BOARD_NAME, "NS5X_NS7XAU") ||
2960 		    dmi_match(DMI_BOARD_NAME, "NS5x_7xAU") ||
2961 		    dmi_match(DMI_BOARD_NAME, "NS5x_7xPU") ||
2962 		    dmi_match(DMI_BOARD_NAME, "PH4PRX1_PH6PRX1"))
2963 			return NVME_QUIRK_FORCE_NO_SIMPLE_SUSPEND;
2964 	} else if (pdev->vendor == 0x144d && pdev->device == 0xa80d) {
2965 		/*
2966 		 * Exclude Samsung 990 Evo from NVME_QUIRK_SIMPLE_SUSPEND
2967 		 * because of high power consumption (> 2 Watt) in s2idle
2968 		 * sleep. Only some boards with Intel CPU are affected.
2969 		 */
2970 		if (dmi_match(DMI_BOARD_NAME, "DN50Z-140HC-YD") ||
2971 		    dmi_match(DMI_BOARD_NAME, "GMxPXxx") ||
2972 		    dmi_match(DMI_BOARD_NAME, "GXxMRXx") ||
2973 		    dmi_match(DMI_BOARD_NAME, "PH4PG31") ||
2974 		    dmi_match(DMI_BOARD_NAME, "PH4PRX1_PH6PRX1") ||
2975 		    dmi_match(DMI_BOARD_NAME, "PH6PG01_PH6PG71"))
2976 			return NVME_QUIRK_FORCE_NO_SIMPLE_SUSPEND;
2977 	}
2978 
2979 	/*
2980 	 * NVMe SSD drops off the PCIe bus after system idle
2981 	 * for 10 hours on a Lenovo N60z board.
2982 	 */
2983 	if (dmi_match(DMI_BOARD_NAME, "LXKT-ZXEG-N6"))
2984 		return NVME_QUIRK_NO_APST;
2985 
2986 	return 0;
2987 }
2988 
nvme_pci_alloc_dev(struct pci_dev * pdev,const struct pci_device_id * id)2989 static struct nvme_dev *nvme_pci_alloc_dev(struct pci_dev *pdev,
2990 		const struct pci_device_id *id)
2991 {
2992 	unsigned long quirks = id->driver_data;
2993 	int node = dev_to_node(&pdev->dev);
2994 	struct nvme_dev *dev;
2995 	int ret = -ENOMEM;
2996 
2997 	dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
2998 	if (!dev)
2999 		return ERR_PTR(-ENOMEM);
3000 	INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work);
3001 	mutex_init(&dev->shutdown_lock);
3002 
3003 	dev->nr_write_queues = write_queues;
3004 	dev->nr_poll_queues = poll_queues;
3005 	dev->nr_allocated_queues = nvme_max_io_queues(dev) + 1;
3006 	dev->queues = kcalloc_node(dev->nr_allocated_queues,
3007 			sizeof(struct nvme_queue), GFP_KERNEL, node);
3008 	if (!dev->queues)
3009 		goto out_free_dev;
3010 
3011 	dev->dev = get_device(&pdev->dev);
3012 
3013 	quirks |= check_vendor_combination_bug(pdev);
3014 	if (!noacpi &&
3015 	    !(quirks & NVME_QUIRK_FORCE_NO_SIMPLE_SUSPEND) &&
3016 	    acpi_storage_d3(&pdev->dev)) {
3017 		/*
3018 		 * Some systems use a bios work around to ask for D3 on
3019 		 * platforms that support kernel managed suspend.
3020 		 */
3021 		dev_info(&pdev->dev,
3022 			 "platform quirk: setting simple suspend\n");
3023 		quirks |= NVME_QUIRK_SIMPLE_SUSPEND;
3024 	}
3025 	ret = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
3026 			     quirks);
3027 	if (ret)
3028 		goto out_put_device;
3029 
3030 	if (dev->ctrl.quirks & NVME_QUIRK_DMA_ADDRESS_BITS_48)
3031 		dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48));
3032 	else
3033 		dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3034 	dma_set_min_align_mask(&pdev->dev, NVME_CTRL_PAGE_SIZE - 1);
3035 	dma_set_max_seg_size(&pdev->dev, 0xffffffff);
3036 
3037 	/*
3038 	 * Limit the max command size to prevent iod->sg allocations going
3039 	 * over a single page.
3040 	 */
3041 	dev->ctrl.max_hw_sectors = min_t(u32,
3042 		NVME_MAX_KB_SZ << 1, dma_opt_mapping_size(&pdev->dev) >> 9);
3043 	dev->ctrl.max_segments = NVME_MAX_SEGS;
3044 
3045 	/*
3046 	 * There is no support for SGLs for metadata (yet), so we are limited to
3047 	 * a single integrity segment for the separate metadata pointer.
3048 	 */
3049 	dev->ctrl.max_integrity_segments = 1;
3050 	return dev;
3051 
3052 out_put_device:
3053 	put_device(dev->dev);
3054 	kfree(dev->queues);
3055 out_free_dev:
3056 	kfree(dev);
3057 	return ERR_PTR(ret);
3058 }
3059 
nvme_probe(struct pci_dev * pdev,const struct pci_device_id * id)3060 static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3061 {
3062 	struct nvme_dev *dev;
3063 	int result = -ENOMEM;
3064 
3065 	dev = nvme_pci_alloc_dev(pdev, id);
3066 	if (IS_ERR(dev))
3067 		return PTR_ERR(dev);
3068 
3069 	result = nvme_dev_map(dev);
3070 	if (result)
3071 		goto out_uninit_ctrl;
3072 
3073 	result = nvme_setup_prp_pools(dev);
3074 	if (result)
3075 		goto out_dev_unmap;
3076 
3077 	result = nvme_pci_alloc_iod_mempool(dev);
3078 	if (result)
3079 		goto out_release_prp_pools;
3080 
3081 	dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev));
3082 
3083 	result = nvme_pci_enable(dev);
3084 	if (result)
3085 		goto out_release_iod_mempool;
3086 
3087 	result = nvme_alloc_admin_tag_set(&dev->ctrl, &dev->admin_tagset,
3088 				&nvme_mq_admin_ops, sizeof(struct nvme_iod));
3089 	if (result)
3090 		goto out_disable;
3091 
3092 	/*
3093 	 * Mark the controller as connecting before sending admin commands to
3094 	 * allow the timeout handler to do the right thing.
3095 	 */
3096 	if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_CONNECTING)) {
3097 		dev_warn(dev->ctrl.device,
3098 			"failed to mark controller CONNECTING\n");
3099 		result = -EBUSY;
3100 		goto out_disable;
3101 	}
3102 
3103 	result = nvme_init_ctrl_finish(&dev->ctrl, false);
3104 	if (result)
3105 		goto out_disable;
3106 
3107 	nvme_dbbuf_dma_alloc(dev);
3108 
3109 	result = nvme_setup_host_mem(dev);
3110 	if (result < 0)
3111 		goto out_disable;
3112 
3113 	result = nvme_setup_io_queues(dev);
3114 	if (result)
3115 		goto out_disable;
3116 
3117 	if (dev->online_queues > 1) {
3118 		nvme_alloc_io_tag_set(&dev->ctrl, &dev->tagset, &nvme_mq_ops,
3119 				nvme_pci_nr_maps(dev), sizeof(struct nvme_iod));
3120 		nvme_dbbuf_set(dev);
3121 	}
3122 
3123 	if (!dev->ctrl.tagset)
3124 		dev_warn(dev->ctrl.device, "IO queues not created\n");
3125 
3126 	if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) {
3127 		dev_warn(dev->ctrl.device,
3128 			"failed to mark controller live state\n");
3129 		result = -ENODEV;
3130 		goto out_disable;
3131 	}
3132 
3133 	pci_set_drvdata(pdev, dev);
3134 
3135 	nvme_start_ctrl(&dev->ctrl);
3136 	nvme_put_ctrl(&dev->ctrl);
3137 	flush_work(&dev->ctrl.scan_work);
3138 	return 0;
3139 
3140 out_disable:
3141 	nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
3142 	nvme_dev_disable(dev, true);
3143 	nvme_free_host_mem(dev);
3144 	nvme_dev_remove_admin(dev);
3145 	nvme_dbbuf_dma_free(dev);
3146 	nvme_free_queues(dev, 0);
3147 out_release_iod_mempool:
3148 	mempool_destroy(dev->iod_mempool);
3149 out_release_prp_pools:
3150 	nvme_release_prp_pools(dev);
3151 out_dev_unmap:
3152 	nvme_dev_unmap(dev);
3153 out_uninit_ctrl:
3154 	nvme_uninit_ctrl(&dev->ctrl);
3155 	nvme_put_ctrl(&dev->ctrl);
3156 	return result;
3157 }
3158 
nvme_reset_prepare(struct pci_dev * pdev)3159 static void nvme_reset_prepare(struct pci_dev *pdev)
3160 {
3161 	struct nvme_dev *dev = pci_get_drvdata(pdev);
3162 
3163 	/*
3164 	 * We don't need to check the return value from waiting for the reset
3165 	 * state as pci_dev device lock is held, making it impossible to race
3166 	 * with ->remove().
3167 	 */
3168 	nvme_disable_prepare_reset(dev, false);
3169 	nvme_sync_queues(&dev->ctrl);
3170 }
3171 
nvme_reset_done(struct pci_dev * pdev)3172 static void nvme_reset_done(struct pci_dev *pdev)
3173 {
3174 	struct nvme_dev *dev = pci_get_drvdata(pdev);
3175 
3176 	if (!nvme_try_sched_reset(&dev->ctrl))
3177 		flush_work(&dev->ctrl.reset_work);
3178 }
3179 
nvme_shutdown(struct pci_dev * pdev)3180 static void nvme_shutdown(struct pci_dev *pdev)
3181 {
3182 	struct nvme_dev *dev = pci_get_drvdata(pdev);
3183 
3184 	nvme_disable_prepare_reset(dev, true);
3185 }
3186 
3187 /*
3188  * The driver's remove may be called on a device in a partially initialized
3189  * state. This function must not have any dependencies on the device state in
3190  * order to proceed.
3191  */
nvme_remove(struct pci_dev * pdev)3192 static void nvme_remove(struct pci_dev *pdev)
3193 {
3194 	struct nvme_dev *dev = pci_get_drvdata(pdev);
3195 
3196 	nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
3197 	pci_set_drvdata(pdev, NULL);
3198 
3199 	if (!pci_device_is_present(pdev)) {
3200 		nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD);
3201 		nvme_dev_disable(dev, true);
3202 	}
3203 
3204 	flush_work(&dev->ctrl.reset_work);
3205 	nvme_stop_ctrl(&dev->ctrl);
3206 	nvme_remove_namespaces(&dev->ctrl);
3207 	nvme_dev_disable(dev, true);
3208 	nvme_free_host_mem(dev);
3209 	nvme_dev_remove_admin(dev);
3210 	nvme_dbbuf_dma_free(dev);
3211 	nvme_free_queues(dev, 0);
3212 	mempool_destroy(dev->iod_mempool);
3213 	nvme_release_prp_pools(dev);
3214 	nvme_dev_unmap(dev);
3215 	nvme_uninit_ctrl(&dev->ctrl);
3216 }
3217 
3218 #ifdef CONFIG_PM_SLEEP
nvme_get_power_state(struct nvme_ctrl * ctrl,u32 * ps)3219 static int nvme_get_power_state(struct nvme_ctrl *ctrl, u32 *ps)
3220 {
3221 	return nvme_get_features(ctrl, NVME_FEAT_POWER_MGMT, 0, NULL, 0, ps);
3222 }
3223 
nvme_set_power_state(struct nvme_ctrl * ctrl,u32 ps)3224 static int nvme_set_power_state(struct nvme_ctrl *ctrl, u32 ps)
3225 {
3226 	return nvme_set_features(ctrl, NVME_FEAT_POWER_MGMT, ps, NULL, 0, NULL);
3227 }
3228 
nvme_resume(struct device * dev)3229 static int nvme_resume(struct device *dev)
3230 {
3231 	struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev));
3232 	struct nvme_ctrl *ctrl = &ndev->ctrl;
3233 
3234 	if (ndev->last_ps == U32_MAX ||
3235 	    nvme_set_power_state(ctrl, ndev->last_ps) != 0)
3236 		goto reset;
3237 	if (ctrl->hmpre && nvme_setup_host_mem(ndev))
3238 		goto reset;
3239 
3240 	return 0;
3241 reset:
3242 	return nvme_try_sched_reset(ctrl);
3243 }
3244 
nvme_suspend(struct device * dev)3245 static int nvme_suspend(struct device *dev)
3246 {
3247 	struct pci_dev *pdev = to_pci_dev(dev);
3248 	struct nvme_dev *ndev = pci_get_drvdata(pdev);
3249 	struct nvme_ctrl *ctrl = &ndev->ctrl;
3250 	int ret = -EBUSY;
3251 
3252 	ndev->last_ps = U32_MAX;
3253 
3254 	/*
3255 	 * The platform does not remove power for a kernel managed suspend so
3256 	 * use host managed nvme power settings for lowest idle power if
3257 	 * possible. This should have quicker resume latency than a full device
3258 	 * shutdown.  But if the firmware is involved after the suspend or the
3259 	 * device does not support any non-default power states, shut down the
3260 	 * device fully.
3261 	 *
3262 	 * If ASPM is not enabled for the device, shut down the device and allow
3263 	 * the PCI bus layer to put it into D3 in order to take the PCIe link
3264 	 * down, so as to allow the platform to achieve its minimum low-power
3265 	 * state (which may not be possible if the link is up).
3266 	 */
3267 	if (pm_suspend_via_firmware() || !ctrl->npss ||
3268 	    !pcie_aspm_enabled(pdev) ||
3269 	    (ndev->ctrl.quirks & NVME_QUIRK_SIMPLE_SUSPEND))
3270 		return nvme_disable_prepare_reset(ndev, true);
3271 
3272 	nvme_start_freeze(ctrl);
3273 	nvme_wait_freeze(ctrl);
3274 	nvme_sync_queues(ctrl);
3275 
3276 	if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE)
3277 		goto unfreeze;
3278 
3279 	/*
3280 	 * Host memory access may not be successful in a system suspend state,
3281 	 * but the specification allows the controller to access memory in a
3282 	 * non-operational power state.
3283 	 */
3284 	if (ndev->hmb) {
3285 		ret = nvme_set_host_mem(ndev, 0);
3286 		if (ret < 0)
3287 			goto unfreeze;
3288 	}
3289 
3290 	ret = nvme_get_power_state(ctrl, &ndev->last_ps);
3291 	if (ret < 0)
3292 		goto unfreeze;
3293 
3294 	/*
3295 	 * A saved state prevents pci pm from generically controlling the
3296 	 * device's power. If we're using protocol specific settings, we don't
3297 	 * want pci interfering.
3298 	 */
3299 	pci_save_state(pdev);
3300 
3301 	ret = nvme_set_power_state(ctrl, ctrl->npss);
3302 	if (ret < 0)
3303 		goto unfreeze;
3304 
3305 	if (ret) {
3306 		/* discard the saved state */
3307 		pci_load_saved_state(pdev, NULL);
3308 
3309 		/*
3310 		 * Clearing npss forces a controller reset on resume. The
3311 		 * correct value will be rediscovered then.
3312 		 */
3313 		ret = nvme_disable_prepare_reset(ndev, true);
3314 		ctrl->npss = 0;
3315 	}
3316 unfreeze:
3317 	nvme_unfreeze(ctrl);
3318 	return ret;
3319 }
3320 
nvme_simple_suspend(struct device * dev)3321 static int nvme_simple_suspend(struct device *dev)
3322 {
3323 	struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev));
3324 
3325 	return nvme_disable_prepare_reset(ndev, true);
3326 }
3327 
nvme_simple_resume(struct device * dev)3328 static int nvme_simple_resume(struct device *dev)
3329 {
3330 	struct pci_dev *pdev = to_pci_dev(dev);
3331 	struct nvme_dev *ndev = pci_get_drvdata(pdev);
3332 
3333 	return nvme_try_sched_reset(&ndev->ctrl);
3334 }
3335 
3336 static const struct dev_pm_ops nvme_dev_pm_ops = {
3337 	.suspend	= nvme_suspend,
3338 	.resume		= nvme_resume,
3339 	.freeze		= nvme_simple_suspend,
3340 	.thaw		= nvme_simple_resume,
3341 	.poweroff	= nvme_simple_suspend,
3342 	.restore	= nvme_simple_resume,
3343 };
3344 #endif /* CONFIG_PM_SLEEP */
3345 
nvme_error_detected(struct pci_dev * pdev,pci_channel_state_t state)3346 static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
3347 						pci_channel_state_t state)
3348 {
3349 	struct nvme_dev *dev = pci_get_drvdata(pdev);
3350 
3351 	/*
3352 	 * A frozen channel requires a reset. When detected, this method will
3353 	 * shutdown the controller to quiesce. The controller will be restarted
3354 	 * after the slot reset through driver's slot_reset callback.
3355 	 */
3356 	switch (state) {
3357 	case pci_channel_io_normal:
3358 		return PCI_ERS_RESULT_CAN_RECOVER;
3359 	case pci_channel_io_frozen:
3360 		dev_warn(dev->ctrl.device,
3361 			"frozen state error detected, reset controller\n");
3362 		if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING)) {
3363 			nvme_dev_disable(dev, true);
3364 			return PCI_ERS_RESULT_DISCONNECT;
3365 		}
3366 		nvme_dev_disable(dev, false);
3367 		return PCI_ERS_RESULT_NEED_RESET;
3368 	case pci_channel_io_perm_failure:
3369 		dev_warn(dev->ctrl.device,
3370 			"failure state error detected, request disconnect\n");
3371 		return PCI_ERS_RESULT_DISCONNECT;
3372 	}
3373 	return PCI_ERS_RESULT_NEED_RESET;
3374 }
3375 
nvme_slot_reset(struct pci_dev * pdev)3376 static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev)
3377 {
3378 	struct nvme_dev *dev = pci_get_drvdata(pdev);
3379 
3380 	dev_info(dev->ctrl.device, "restart after slot reset\n");
3381 	pci_restore_state(pdev);
3382 	if (nvme_try_sched_reset(&dev->ctrl))
3383 		nvme_unquiesce_io_queues(&dev->ctrl);
3384 	return PCI_ERS_RESULT_RECOVERED;
3385 }
3386 
nvme_error_resume(struct pci_dev * pdev)3387 static void nvme_error_resume(struct pci_dev *pdev)
3388 {
3389 	struct nvme_dev *dev = pci_get_drvdata(pdev);
3390 
3391 	flush_work(&dev->ctrl.reset_work);
3392 }
3393 
3394 static const struct pci_error_handlers nvme_err_handler = {
3395 	.error_detected	= nvme_error_detected,
3396 	.slot_reset	= nvme_slot_reset,
3397 	.resume		= nvme_error_resume,
3398 	.reset_prepare	= nvme_reset_prepare,
3399 	.reset_done	= nvme_reset_done,
3400 };
3401 
3402 static const struct pci_device_id nvme_id_table[] = {
3403 	{ PCI_VDEVICE(INTEL, 0x0953),	/* Intel 750/P3500/P3600/P3700 */
3404 		.driver_data = NVME_QUIRK_STRIPE_SIZE |
3405 				NVME_QUIRK_DEALLOCATE_ZEROES, },
3406 	{ PCI_VDEVICE(INTEL, 0x0a53),	/* Intel P3520 */
3407 		.driver_data = NVME_QUIRK_STRIPE_SIZE |
3408 				NVME_QUIRK_DEALLOCATE_ZEROES, },
3409 	{ PCI_VDEVICE(INTEL, 0x0a54),	/* Intel P4500/P4600 */
3410 		.driver_data = NVME_QUIRK_STRIPE_SIZE |
3411 				NVME_QUIRK_DEALLOCATE_ZEROES |
3412 				NVME_QUIRK_IGNORE_DEV_SUBNQN |
3413 				NVME_QUIRK_BOGUS_NID, },
3414 	{ PCI_VDEVICE(INTEL, 0x0a55),	/* Dell Express Flash P4600 */
3415 		.driver_data = NVME_QUIRK_STRIPE_SIZE |
3416 				NVME_QUIRK_DEALLOCATE_ZEROES, },
3417 	{ PCI_VDEVICE(INTEL, 0xf1a5),	/* Intel 600P/P3100 */
3418 		.driver_data = NVME_QUIRK_NO_DEEPEST_PS |
3419 				NVME_QUIRK_MEDIUM_PRIO_SQ |
3420 				NVME_QUIRK_NO_TEMP_THRESH_CHANGE |
3421 				NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3422 	{ PCI_VDEVICE(INTEL, 0xf1a6),	/* Intel 760p/Pro 7600p */
3423 		.driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN, },
3424 	{ PCI_VDEVICE(INTEL, 0x5845),	/* Qemu emulated controller */
3425 		.driver_data = NVME_QUIRK_IDENTIFY_CNS |
3426 				NVME_QUIRK_DISABLE_WRITE_ZEROES |
3427 				NVME_QUIRK_BOGUS_NID, },
3428 	{ PCI_VDEVICE(REDHAT, 0x0010),	/* Qemu emulated controller */
3429 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3430 	{ PCI_DEVICE(0x1217, 0x8760), /* O2 Micro 64GB Steam Deck */
3431 		.driver_data = NVME_QUIRK_DMAPOOL_ALIGN_512, },
3432 	{ PCI_DEVICE(0x126f, 0x1001),	/* Silicon Motion generic */
3433 		.driver_data = NVME_QUIRK_NO_DEEPEST_PS |
3434 				NVME_QUIRK_IGNORE_DEV_SUBNQN, },
3435 	{ PCI_DEVICE(0x126f, 0x2262),	/* Silicon Motion generic */
3436 		.driver_data = NVME_QUIRK_NO_DEEPEST_PS |
3437 				NVME_QUIRK_BOGUS_NID, },
3438 	{ PCI_DEVICE(0x126f, 0x2263),	/* Silicon Motion unidentified */
3439 		.driver_data = NVME_QUIRK_NO_NS_DESC_LIST |
3440 				NVME_QUIRK_BOGUS_NID, },
3441 	{ PCI_DEVICE(0x1bb1, 0x0100),   /* Seagate Nytro Flash Storage */
3442 		.driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY |
3443 				NVME_QUIRK_NO_NS_DESC_LIST, },
3444 	{ PCI_DEVICE(0x1c58, 0x0003),	/* HGST adapter */
3445 		.driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
3446 	{ PCI_DEVICE(0x1c58, 0x0023),	/* WDC SN200 adapter */
3447 		.driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
3448 	{ PCI_DEVICE(0x1c5f, 0x0540),	/* Memblaze Pblaze4 adapter */
3449 		.driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
3450 	{ PCI_DEVICE(0x144d, 0xa821),   /* Samsung PM1725 */
3451 		.driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
3452 	{ PCI_DEVICE(0x144d, 0xa822),   /* Samsung PM1725a */
3453 		.driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY |
3454 				NVME_QUIRK_DISABLE_WRITE_ZEROES|
3455 				NVME_QUIRK_IGNORE_DEV_SUBNQN, },
3456 	{ PCI_DEVICE(0x15b7, 0x5008),   /* Sandisk SN530 */
3457 		.driver_data = NVME_QUIRK_BROKEN_MSI },
3458 	{ PCI_DEVICE(0x15b7, 0x5009),   /* Sandisk SN550 */
3459 		.driver_data = NVME_QUIRK_BROKEN_MSI |
3460 				NVME_QUIRK_NO_DEEPEST_PS },
3461 	{ PCI_DEVICE(0x1987, 0x5012),	/* Phison E12 */
3462 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3463 	{ PCI_DEVICE(0x1987, 0x5016),	/* Phison E16 */
3464 		.driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN |
3465 				NVME_QUIRK_BOGUS_NID, },
3466 	{ PCI_DEVICE(0x1987, 0x5019),  /* phison E19 */
3467 		.driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3468 	{ PCI_DEVICE(0x1987, 0x5021),   /* Phison E21 */
3469 		.driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3470 	{ PCI_DEVICE(0x1b4b, 0x1092),	/* Lexar 256 GB SSD */
3471 		.driver_data = NVME_QUIRK_NO_NS_DESC_LIST |
3472 				NVME_QUIRK_IGNORE_DEV_SUBNQN, },
3473 	{ PCI_DEVICE(0x1cc1, 0x33f8),   /* ADATA IM2P33F8ABR1 1 TB */
3474 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3475 	{ PCI_DEVICE(0x10ec, 0x5762),   /* ADATA SX6000LNP */
3476 		.driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN |
3477 				NVME_QUIRK_BOGUS_NID, },
3478 	{ PCI_DEVICE(0x10ec, 0x5763),  /* ADATA SX6000PNP */
3479 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3480 	{ PCI_DEVICE(0x1cc1, 0x8201),   /* ADATA SX8200PNP 512GB */
3481 		.driver_data = NVME_QUIRK_NO_DEEPEST_PS |
3482 				NVME_QUIRK_IGNORE_DEV_SUBNQN, },
3483 	 { PCI_DEVICE(0x1344, 0x5407), /* Micron Technology Inc NVMe SSD */
3484 		.driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN },
3485 	 { PCI_DEVICE(0x1344, 0x6001),   /* Micron Nitro NVMe */
3486 		 .driver_data = NVME_QUIRK_BOGUS_NID, },
3487 	{ PCI_DEVICE(0x1c5c, 0x1504),   /* SK Hynix PC400 */
3488 		.driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3489 	{ PCI_DEVICE(0x1c5c, 0x174a),   /* SK Hynix P31 SSD */
3490 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3491 	{ PCI_DEVICE(0x15b7, 0x2001),   /*  Sandisk Skyhawk */
3492 		.driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3493 	{ PCI_DEVICE(0x1d97, 0x2263),   /* SPCC */
3494 		.driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3495 	{ PCI_DEVICE(0x144d, 0xa80b),   /* Samsung PM9B1 256G and 512G */
3496 		.driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES |
3497 				NVME_QUIRK_BOGUS_NID, },
3498 	{ PCI_DEVICE(0x144d, 0xa809),   /* Samsung MZALQ256HBJD 256G */
3499 		.driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3500 	{ PCI_DEVICE(0x144d, 0xa802),   /* Samsung SM953 */
3501 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3502 	{ PCI_DEVICE(0x1cc4, 0x6303),   /* UMIS RPJTJ512MGE1QDY 512G */
3503 		.driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3504 	{ PCI_DEVICE(0x1cc4, 0x6302),   /* UMIS RPJTJ256MGE1QDY 256G */
3505 		.driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3506 	{ PCI_DEVICE(0x2646, 0x2262),   /* KINGSTON SKC2000 NVMe SSD */
3507 		.driver_data = NVME_QUIRK_NO_DEEPEST_PS, },
3508 	{ PCI_DEVICE(0x2646, 0x2263),   /* KINGSTON A2000 NVMe SSD  */
3509 		.driver_data = NVME_QUIRK_NO_DEEPEST_PS, },
3510 	{ PCI_DEVICE(0x2646, 0x5013),   /* Kingston KC3000, Kingston FURY Renegade */
3511 		.driver_data = NVME_QUIRK_NO_SECONDARY_TEMP_THRESH, },
3512 	{ PCI_DEVICE(0x2646, 0x5018),   /* KINGSTON OM8SFP4xxxxP OS21012 NVMe SSD */
3513 		.driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3514 	{ PCI_DEVICE(0x2646, 0x5016),   /* KINGSTON OM3PGP4xxxxP OS21011 NVMe SSD */
3515 		.driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3516 	{ PCI_DEVICE(0x2646, 0x501A),   /* KINGSTON OM8PGP4xxxxP OS21005 NVMe SSD */
3517 		.driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3518 	{ PCI_DEVICE(0x2646, 0x501B),   /* KINGSTON OM8PGP4xxxxQ OS21005 NVMe SSD */
3519 		.driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3520 	{ PCI_DEVICE(0x2646, 0x501E),   /* KINGSTON OM3PGP4xxxxQ OS21011 NVMe SSD */
3521 		.driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3522 	{ PCI_DEVICE(0x1f40, 0x1202),   /* Netac Technologies Co. NV3000 NVMe SSD */
3523 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3524 	{ PCI_DEVICE(0x1f40, 0x5236),   /* Netac Technologies Co. NV7000 NVMe SSD */
3525 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3526 	{ PCI_DEVICE(0x1e4B, 0x1001),   /* MAXIO MAP1001 */
3527 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3528 	{ PCI_DEVICE(0x1e4B, 0x1002),   /* MAXIO MAP1002 */
3529 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3530 	{ PCI_DEVICE(0x1e4B, 0x1202),   /* MAXIO MAP1202 */
3531 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3532 	{ PCI_DEVICE(0x1e4B, 0x1602),   /* MAXIO MAP1602 */
3533 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3534 	{ PCI_DEVICE(0x1cc1, 0x5350),   /* ADATA XPG GAMMIX S50 */
3535 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3536 	{ PCI_DEVICE(0x1dbe, 0x5216),   /* Acer/INNOGRIT FA100/5216 NVMe SSD */
3537 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3538 	{ PCI_DEVICE(0x1dbe, 0x5236),   /* ADATA XPG GAMMIX S70 */
3539 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3540 	{ PCI_DEVICE(0x1e49, 0x0021),   /* ZHITAI TiPro5000 NVMe SSD */
3541 		.driver_data = NVME_QUIRK_NO_DEEPEST_PS, },
3542 	{ PCI_DEVICE(0x1e49, 0x0041),   /* ZHITAI TiPro7000 NVMe SSD */
3543 		.driver_data = NVME_QUIRK_NO_DEEPEST_PS, },
3544 	{ PCI_DEVICE(0x025e, 0xf1ac),   /* SOLIDIGM  P44 pro SSDPFKKW020X7  */
3545 		.driver_data = NVME_QUIRK_NO_DEEPEST_PS, },
3546 	{ PCI_DEVICE(0xc0a9, 0x540a),   /* Crucial P2 */
3547 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3548 	{ PCI_DEVICE(0x1d97, 0x2263), /* Lexar NM610 */
3549 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3550 	{ PCI_DEVICE(0x1d97, 0x1d97), /* Lexar NM620 */
3551 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3552 	{ PCI_DEVICE(0x1d97, 0x2269), /* Lexar NM760 */
3553 		.driver_data = NVME_QUIRK_BOGUS_NID |
3554 				NVME_QUIRK_IGNORE_DEV_SUBNQN, },
3555 	{ PCI_DEVICE(0x10ec, 0x5763), /* TEAMGROUP T-FORCE CARDEA ZERO Z330 SSD */
3556 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3557 	{ PCI_DEVICE(0x1e4b, 0x1602), /* HS-SSD-FUTURE 2048G  */
3558 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3559 	{ PCI_DEVICE(0x10ec, 0x5765), /* TEAMGROUP MP33 2TB SSD */
3560 		.driver_data = NVME_QUIRK_BOGUS_NID, },
3561 	{ PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0x0061),
3562 		.driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, },
3563 	{ PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0x0065),
3564 		.driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, },
3565 	{ PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0x8061),
3566 		.driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, },
3567 	{ PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0xcd00),
3568 		.driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, },
3569 	{ PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0xcd01),
3570 		.driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, },
3571 	{ PCI_DEVICE(PCI_VENDOR_ID_AMAZON, 0xcd02),
3572 		.driver_data = NVME_QUIRK_DMA_ADDRESS_BITS_48, },
3573 	{ PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001),
3574 		/*
3575 		 * Fix for the Apple controller found in the MacBook8,1 and
3576 		 * some MacBook7,1 to avoid controller resets and data loss.
3577 		 */
3578 		.driver_data = NVME_QUIRK_SINGLE_VECTOR |
3579 				NVME_QUIRK_QDEPTH_ONE },
3580 	{ PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) },
3581 	{ PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2005),
3582 		.driver_data = NVME_QUIRK_SINGLE_VECTOR |
3583 				NVME_QUIRK_128_BYTES_SQES |
3584 				NVME_QUIRK_SHARED_TAGS |
3585 				NVME_QUIRK_SKIP_CID_GEN |
3586 				NVME_QUIRK_IDENTIFY_CNS },
3587 	{ PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
3588 	{ 0, }
3589 };
3590 MODULE_DEVICE_TABLE(pci, nvme_id_table);
3591 
3592 static struct pci_driver nvme_driver = {
3593 	.name		= "nvme",
3594 	.id_table	= nvme_id_table,
3595 	.probe		= nvme_probe,
3596 	.remove		= nvme_remove,
3597 	.shutdown	= nvme_shutdown,
3598 	.driver		= {
3599 		.probe_type	= PROBE_PREFER_ASYNCHRONOUS,
3600 #ifdef CONFIG_PM_SLEEP
3601 		.pm		= &nvme_dev_pm_ops,
3602 #endif
3603 	},
3604 	.sriov_configure = pci_sriov_configure_simple,
3605 	.err_handler	= &nvme_err_handler,
3606 };
3607 
nvme_init(void)3608 static int __init nvme_init(void)
3609 {
3610 	BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
3611 	BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
3612 	BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
3613 	BUILD_BUG_ON(IRQ_AFFINITY_MAX_SETS < 2);
3614 	BUILD_BUG_ON(NVME_MAX_SEGS > SGES_PER_PAGE);
3615 	BUILD_BUG_ON(sizeof(struct scatterlist) * NVME_MAX_SEGS > PAGE_SIZE);
3616 	BUILD_BUG_ON(nvme_pci_npages_prp() > NVME_MAX_NR_ALLOCATIONS);
3617 
3618 	return pci_register_driver(&nvme_driver);
3619 }
3620 
nvme_exit(void)3621 static void __exit nvme_exit(void)
3622 {
3623 	pci_unregister_driver(&nvme_driver);
3624 	flush_workqueue(nvme_wq);
3625 }
3626 
3627 MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
3628 MODULE_LICENSE("GPL");
3629 MODULE_VERSION("1.0");
3630 module_init(nvme_init);
3631 module_exit(nvme_exit);
3632