]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/nvme/host/pci.c
nvme-pci: fix hot removal during error handling
[mirror_ubuntu-bionic-kernel.git] / drivers / nvme / host / pci.c
1 /*
2 * NVM Express device driver
3 * Copyright (c) 2011-2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15 #include <linux/aer.h>
16 #include <linux/blkdev.h>
17 #include <linux/blk-mq.h>
18 #include <linux/blk-mq-pci.h>
19 #include <linux/dmi.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/io.h>
23 #include <linux/mm.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/once.h>
27 #include <linux/pci.h>
28 #include <linux/suspend.h>
29 #include <linux/t10-pi.h>
30 #include <linux/types.h>
31 #include <linux/io-64-nonatomic-lo-hi.h>
32 #include <linux/sed-opal.h>
33
34 #include "nvme.h"
35
36 #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
37 #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
38
39 #define SGES_PER_PAGE (PAGE_SIZE / sizeof(struct nvme_sgl_desc))
40
41 static int use_threaded_interrupts;
42 module_param(use_threaded_interrupts, int, 0);
43
44 static bool use_cmb_sqes = true;
45 module_param(use_cmb_sqes, bool, 0644);
46 MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
47
48 static unsigned int max_host_mem_size_mb = 128;
49 module_param(max_host_mem_size_mb, uint, 0444);
50 MODULE_PARM_DESC(max_host_mem_size_mb,
51 "Maximum Host Memory Buffer (HMB) size per controller (in MiB)");
52
53 static unsigned int sgl_threshold = SZ_32K;
54 module_param(sgl_threshold, uint, 0644);
55 MODULE_PARM_DESC(sgl_threshold,
56 "Use SGLs when average request segment size is larger or equal to "
57 "this size. Use 0 to disable SGLs.");
58
59 static int io_queue_depth_set(const char *val, const struct kernel_param *kp);
60 static const struct kernel_param_ops io_queue_depth_ops = {
61 .set = io_queue_depth_set,
62 .get = param_get_int,
63 };
64
65 static int io_queue_depth = 1024;
66 module_param_cb(io_queue_depth, &io_queue_depth_ops, &io_queue_depth, 0644);
67 MODULE_PARM_DESC(io_queue_depth, "set io queue depth, should >= 2");
68
69 struct nvme_dev;
70 struct nvme_queue;
71
72 static void nvme_process_cq(struct nvme_queue *nvmeq);
73 static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown);
74
75 /*
76 * Represents an NVM Express device. Each nvme_dev is a PCI function.
77 */
78 struct nvme_dev {
79 struct nvme_queue *queues;
80 struct blk_mq_tag_set tagset;
81 struct blk_mq_tag_set admin_tagset;
82 u32 __iomem *dbs;
83 struct device *dev;
84 struct dma_pool *prp_page_pool;
85 struct dma_pool *prp_small_pool;
86 unsigned online_queues;
87 unsigned max_qid;
88 int q_depth;
89 u32 db_stride;
90 void __iomem *bar;
91 unsigned long bar_mapped_size;
92 struct work_struct remove_work;
93 struct mutex shutdown_lock;
94 bool subsystem;
95 void __iomem *cmb;
96 pci_bus_addr_t cmb_bus_addr;
97 u64 cmb_size;
98 u32 cmbsz;
99 u32 cmbloc;
100 struct nvme_ctrl ctrl;
101 struct completion ioq_wait;
102 u32 last_ps;
103
104 /* shadow doorbell buffer support: */
105 u32 *dbbuf_dbs;
106 dma_addr_t dbbuf_dbs_dma_addr;
107 u32 *dbbuf_eis;
108 dma_addr_t dbbuf_eis_dma_addr;
109
110 /* host memory buffer support: */
111 u64 host_mem_size;
112 u32 nr_host_mem_descs;
113 dma_addr_t host_mem_descs_dma;
114 struct nvme_host_mem_buf_desc *host_mem_descs;
115 void **host_mem_desc_bufs;
116 };
117
118 static int io_queue_depth_set(const char *val, const struct kernel_param *kp)
119 {
120 int n = 0, ret;
121
122 ret = kstrtoint(val, 10, &n);
123 if (ret != 0 || n < 2)
124 return -EINVAL;
125
126 return param_set_int(val, kp);
127 }
128
129 static inline unsigned int sq_idx(unsigned int qid, u32 stride)
130 {
131 return qid * 2 * stride;
132 }
133
134 static inline unsigned int cq_idx(unsigned int qid, u32 stride)
135 {
136 return (qid * 2 + 1) * stride;
137 }
138
139 static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
140 {
141 return container_of(ctrl, struct nvme_dev, ctrl);
142 }
143
144 /*
145 * An NVM Express queue. Each device has at least two (one for admin
146 * commands and one for I/O commands).
147 */
148 struct nvme_queue {
149 struct device *q_dmadev;
150 struct nvme_dev *dev;
151 spinlock_t q_lock;
152 struct nvme_command *sq_cmds;
153 struct nvme_command __iomem *sq_cmds_io;
154 volatile struct nvme_completion *cqes;
155 struct blk_mq_tags **tags;
156 dma_addr_t sq_dma_addr;
157 dma_addr_t cq_dma_addr;
158 u32 __iomem *q_db;
159 u16 q_depth;
160 s16 cq_vector;
161 u16 sq_tail;
162 u16 cq_head;
163 u16 qid;
164 u8 cq_phase;
165 u8 cqe_seen;
166 u32 *dbbuf_sq_db;
167 u32 *dbbuf_cq_db;
168 u32 *dbbuf_sq_ei;
169 u32 *dbbuf_cq_ei;
170 };
171
172 /*
173 * The nvme_iod describes the data in an I/O, including the list of PRP
174 * entries. You can't see it in this data structure because C doesn't let
175 * me express that. Use nvme_init_iod to ensure there's enough space
176 * allocated to store the PRP list.
177 */
178 struct nvme_iod {
179 struct nvme_request req;
180 struct nvme_queue *nvmeq;
181 bool use_sgl;
182 int aborted;
183 int npages; /* In the PRP list. 0 means small pool in use */
184 int nents; /* Used in scatterlist */
185 int length; /* Of data, in bytes */
186 dma_addr_t first_dma;
187 struct scatterlist meta_sg; /* metadata requires single contiguous buffer */
188 struct scatterlist *sg;
189 struct scatterlist inline_sg[0];
190 };
191
192 /*
193 * Check we didin't inadvertently grow the command struct
194 */
195 static inline void _nvme_check_size(void)
196 {
197 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
198 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
199 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
200 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
201 BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
202 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
203 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
204 BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
205 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != NVME_IDENTIFY_DATA_SIZE);
206 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != NVME_IDENTIFY_DATA_SIZE);
207 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
208 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
209 BUILD_BUG_ON(sizeof(struct nvme_dbbuf) != 64);
210 }
211
212 static inline unsigned int nvme_dbbuf_size(u32 stride)
213 {
214 return ((num_possible_cpus() + 1) * 8 * stride);
215 }
216
217 static int nvme_dbbuf_dma_alloc(struct nvme_dev *dev)
218 {
219 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
220
221 if (dev->dbbuf_dbs)
222 return 0;
223
224 dev->dbbuf_dbs = dma_alloc_coherent(dev->dev, mem_size,
225 &dev->dbbuf_dbs_dma_addr,
226 GFP_KERNEL);
227 if (!dev->dbbuf_dbs)
228 return -ENOMEM;
229 dev->dbbuf_eis = dma_alloc_coherent(dev->dev, mem_size,
230 &dev->dbbuf_eis_dma_addr,
231 GFP_KERNEL);
232 if (!dev->dbbuf_eis) {
233 dma_free_coherent(dev->dev, mem_size,
234 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
235 dev->dbbuf_dbs = NULL;
236 return -ENOMEM;
237 }
238
239 return 0;
240 }
241
242 static void nvme_dbbuf_dma_free(struct nvme_dev *dev)
243 {
244 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
245
246 if (dev->dbbuf_dbs) {
247 dma_free_coherent(dev->dev, mem_size,
248 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
249 dev->dbbuf_dbs = NULL;
250 }
251 if (dev->dbbuf_eis) {
252 dma_free_coherent(dev->dev, mem_size,
253 dev->dbbuf_eis, dev->dbbuf_eis_dma_addr);
254 dev->dbbuf_eis = NULL;
255 }
256 }
257
258 static void nvme_dbbuf_init(struct nvme_dev *dev,
259 struct nvme_queue *nvmeq, int qid)
260 {
261 if (!dev->dbbuf_dbs || !qid)
262 return;
263
264 nvmeq->dbbuf_sq_db = &dev->dbbuf_dbs[sq_idx(qid, dev->db_stride)];
265 nvmeq->dbbuf_cq_db = &dev->dbbuf_dbs[cq_idx(qid, dev->db_stride)];
266 nvmeq->dbbuf_sq_ei = &dev->dbbuf_eis[sq_idx(qid, dev->db_stride)];
267 nvmeq->dbbuf_cq_ei = &dev->dbbuf_eis[cq_idx(qid, dev->db_stride)];
268 }
269
270 static void nvme_dbbuf_set(struct nvme_dev *dev)
271 {
272 struct nvme_command c;
273
274 if (!dev->dbbuf_dbs)
275 return;
276
277 memset(&c, 0, sizeof(c));
278 c.dbbuf.opcode = nvme_admin_dbbuf;
279 c.dbbuf.prp1 = cpu_to_le64(dev->dbbuf_dbs_dma_addr);
280 c.dbbuf.prp2 = cpu_to_le64(dev->dbbuf_eis_dma_addr);
281
282 if (nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0)) {
283 dev_warn(dev->ctrl.device, "unable to set dbbuf\n");
284 /* Free memory and continue on */
285 nvme_dbbuf_dma_free(dev);
286 }
287 }
288
289 static inline int nvme_dbbuf_need_event(u16 event_idx, u16 new_idx, u16 old)
290 {
291 return (u16)(new_idx - event_idx - 1) < (u16)(new_idx - old);
292 }
293
294 /* Update dbbuf and return true if an MMIO is required */
295 static bool nvme_dbbuf_update_and_check_event(u16 value, u32 *dbbuf_db,
296 volatile u32 *dbbuf_ei)
297 {
298 if (dbbuf_db) {
299 u16 old_value;
300
301 /*
302 * Ensure that the queue is written before updating
303 * the doorbell in memory
304 */
305 wmb();
306
307 old_value = *dbbuf_db;
308 *dbbuf_db = value;
309
310 /*
311 * Ensure that the doorbell is updated before reading the event
312 * index from memory. The controller needs to provide similar
313 * ordering to ensure the envent index is updated before reading
314 * the doorbell.
315 */
316 mb();
317
318 if (!nvme_dbbuf_need_event(*dbbuf_ei, value, old_value))
319 return false;
320 }
321
322 return true;
323 }
324
325 /*
326 * Max size of iod being embedded in the request payload
327 */
328 #define NVME_INT_PAGES 2
329 #define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
330
331 /*
332 * Will slightly overestimate the number of pages needed. This is OK
333 * as it only leads to a small amount of wasted memory for the lifetime of
334 * the I/O.
335 */
336 static int nvme_npages(unsigned size, struct nvme_dev *dev)
337 {
338 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
339 dev->ctrl.page_size);
340 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
341 }
342
343 /*
344 * Calculates the number of pages needed for the SGL segments. For example a 4k
345 * page can accommodate 256 SGL descriptors.
346 */
347 static int nvme_pci_npages_sgl(unsigned int num_seg)
348 {
349 return DIV_ROUND_UP(num_seg * sizeof(struct nvme_sgl_desc), PAGE_SIZE);
350 }
351
352 static unsigned int nvme_pci_iod_alloc_size(struct nvme_dev *dev,
353 unsigned int size, unsigned int nseg, bool use_sgl)
354 {
355 size_t alloc_size;
356
357 if (use_sgl)
358 alloc_size = sizeof(__le64 *) * nvme_pci_npages_sgl(nseg);
359 else
360 alloc_size = sizeof(__le64 *) * nvme_npages(size, dev);
361
362 return alloc_size + sizeof(struct scatterlist) * nseg;
363 }
364
365 static unsigned int nvme_pci_cmd_size(struct nvme_dev *dev, bool use_sgl)
366 {
367 unsigned int alloc_size = nvme_pci_iod_alloc_size(dev,
368 NVME_INT_BYTES(dev), NVME_INT_PAGES,
369 use_sgl);
370
371 return sizeof(struct nvme_iod) + alloc_size;
372 }
373
374 static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
375 unsigned int hctx_idx)
376 {
377 struct nvme_dev *dev = data;
378 struct nvme_queue *nvmeq = &dev->queues[0];
379
380 WARN_ON(hctx_idx != 0);
381 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
382 WARN_ON(nvmeq->tags);
383
384 hctx->driver_data = nvmeq;
385 nvmeq->tags = &dev->admin_tagset.tags[0];
386 return 0;
387 }
388
389 static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
390 {
391 struct nvme_queue *nvmeq = hctx->driver_data;
392
393 nvmeq->tags = NULL;
394 }
395
396 static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
397 unsigned int hctx_idx)
398 {
399 struct nvme_dev *dev = data;
400 struct nvme_queue *nvmeq = &dev->queues[hctx_idx + 1];
401
402 if (!nvmeq->tags)
403 nvmeq->tags = &dev->tagset.tags[hctx_idx];
404
405 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
406 hctx->driver_data = nvmeq;
407 return 0;
408 }
409
410 static int nvme_init_request(struct blk_mq_tag_set *set, struct request *req,
411 unsigned int hctx_idx, unsigned int numa_node)
412 {
413 struct nvme_dev *dev = set->driver_data;
414 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
415 int queue_idx = (set == &dev->tagset) ? hctx_idx + 1 : 0;
416 struct nvme_queue *nvmeq = &dev->queues[queue_idx];
417
418 BUG_ON(!nvmeq);
419 iod->nvmeq = nvmeq;
420 return 0;
421 }
422
423 static int nvme_pci_map_queues(struct blk_mq_tag_set *set)
424 {
425 struct nvme_dev *dev = set->driver_data;
426
427 return blk_mq_pci_map_queues(set, to_pci_dev(dev->dev));
428 }
429
430 /**
431 * __nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
432 * @nvmeq: The queue to use
433 * @cmd: The command to send
434 *
435 * Safe to use from interrupt context
436 */
437 static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
438 struct nvme_command *cmd)
439 {
440 u16 tail = nvmeq->sq_tail;
441
442 if (nvmeq->sq_cmds_io)
443 memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
444 else
445 memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
446
447 if (++tail == nvmeq->q_depth)
448 tail = 0;
449 if (nvme_dbbuf_update_and_check_event(tail, nvmeq->dbbuf_sq_db,
450 nvmeq->dbbuf_sq_ei))
451 writel(tail, nvmeq->q_db);
452 nvmeq->sq_tail = tail;
453 }
454
455 static void **nvme_pci_iod_list(struct request *req)
456 {
457 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
458 return (void **)(iod->sg + blk_rq_nr_phys_segments(req));
459 }
460
461 static inline bool nvme_pci_use_sgls(struct nvme_dev *dev, struct request *req)
462 {
463 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
464 int nseg = blk_rq_nr_phys_segments(req);
465 unsigned int avg_seg_size;
466
467 if (nseg == 0)
468 return false;
469
470 avg_seg_size = DIV_ROUND_UP(blk_rq_payload_bytes(req), nseg);
471
472 if (!(dev->ctrl.sgls & ((1 << 0) | (1 << 1))))
473 return false;
474 if (!iod->nvmeq->qid)
475 return false;
476 if (!sgl_threshold || avg_seg_size < sgl_threshold)
477 return false;
478 return true;
479 }
480
481 static blk_status_t nvme_init_iod(struct request *rq, struct nvme_dev *dev)
482 {
483 struct nvme_iod *iod = blk_mq_rq_to_pdu(rq);
484 int nseg = blk_rq_nr_phys_segments(rq);
485 unsigned int size = blk_rq_payload_bytes(rq);
486
487 iod->use_sgl = nvme_pci_use_sgls(dev, rq);
488
489 if (nseg > NVME_INT_PAGES || size > NVME_INT_BYTES(dev)) {
490 size_t alloc_size = nvme_pci_iod_alloc_size(dev, size, nseg,
491 iod->use_sgl);
492
493 iod->sg = kmalloc(alloc_size, GFP_ATOMIC);
494 if (!iod->sg)
495 return BLK_STS_RESOURCE;
496 } else {
497 iod->sg = iod->inline_sg;
498 }
499
500 iod->aborted = 0;
501 iod->npages = -1;
502 iod->nents = 0;
503 iod->length = size;
504
505 return BLK_STS_OK;
506 }
507
508 static void nvme_free_iod(struct nvme_dev *dev, struct request *req)
509 {
510 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
511 const int last_prp = dev->ctrl.page_size / sizeof(__le64) - 1;
512 dma_addr_t dma_addr = iod->first_dma, next_dma_addr;
513
514 int i;
515
516 if (iod->npages == 0)
517 dma_pool_free(dev->prp_small_pool, nvme_pci_iod_list(req)[0],
518 dma_addr);
519
520 for (i = 0; i < iod->npages; i++) {
521 void *addr = nvme_pci_iod_list(req)[i];
522
523 if (iod->use_sgl) {
524 struct nvme_sgl_desc *sg_list = addr;
525
526 next_dma_addr =
527 le64_to_cpu((sg_list[SGES_PER_PAGE - 1]).addr);
528 } else {
529 __le64 *prp_list = addr;
530
531 next_dma_addr = le64_to_cpu(prp_list[last_prp]);
532 }
533
534 dma_pool_free(dev->prp_page_pool, addr, dma_addr);
535 dma_addr = next_dma_addr;
536 }
537
538 if (iod->sg != iod->inline_sg)
539 kfree(iod->sg);
540 }
541
542 #ifdef CONFIG_BLK_DEV_INTEGRITY
543 static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
544 {
545 if (be32_to_cpu(pi->ref_tag) == v)
546 pi->ref_tag = cpu_to_be32(p);
547 }
548
549 static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
550 {
551 if (be32_to_cpu(pi->ref_tag) == p)
552 pi->ref_tag = cpu_to_be32(v);
553 }
554
555 /**
556 * nvme_dif_remap - remaps ref tags to bip seed and physical lba
557 *
558 * The virtual start sector is the one that was originally submitted by the
559 * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
560 * start sector may be different. Remap protection information to match the
561 * physical LBA on writes, and back to the original seed on reads.
562 *
563 * Type 0 and 3 do not have a ref tag, so no remapping required.
564 */
565 static void nvme_dif_remap(struct request *req,
566 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
567 {
568 struct nvme_ns *ns = req->rq_disk->private_data;
569 struct bio_integrity_payload *bip;
570 struct t10_pi_tuple *pi;
571 void *p, *pmap;
572 u32 i, nlb, ts, phys, virt;
573
574 if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
575 return;
576
577 bip = bio_integrity(req->bio);
578 if (!bip)
579 return;
580
581 pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
582
583 p = pmap;
584 virt = bip_get_seed(bip);
585 phys = nvme_block_nr(ns, blk_rq_pos(req));
586 nlb = (blk_rq_bytes(req) >> ns->lba_shift);
587 ts = ns->disk->queue->integrity.tuple_size;
588
589 for (i = 0; i < nlb; i++, virt++, phys++) {
590 pi = (struct t10_pi_tuple *)p;
591 dif_swap(phys, virt, pi);
592 p += ts;
593 }
594 kunmap_atomic(pmap);
595 }
596 #else /* CONFIG_BLK_DEV_INTEGRITY */
597 static void nvme_dif_remap(struct request *req,
598 void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
599 {
600 }
601 static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
602 {
603 }
604 static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
605 {
606 }
607 #endif
608
609 static void nvme_print_sgl(struct scatterlist *sgl, int nents)
610 {
611 int i;
612 struct scatterlist *sg;
613
614 for_each_sg(sgl, sg, nents, i) {
615 dma_addr_t phys = sg_phys(sg);
616 pr_warn("sg[%d] phys_addr:%pad offset:%d length:%d "
617 "dma_address:%pad dma_length:%d\n",
618 i, &phys, sg->offset, sg->length, &sg_dma_address(sg),
619 sg_dma_len(sg));
620 }
621 }
622
623 static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev,
624 struct request *req, struct nvme_rw_command *cmnd)
625 {
626 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
627 struct dma_pool *pool;
628 int length = blk_rq_payload_bytes(req);
629 struct scatterlist *sg = iod->sg;
630 int dma_len = sg_dma_len(sg);
631 u64 dma_addr = sg_dma_address(sg);
632 u32 page_size = dev->ctrl.page_size;
633 int offset = dma_addr & (page_size - 1);
634 __le64 *prp_list;
635 void **list = nvme_pci_iod_list(req);
636 dma_addr_t prp_dma;
637 int nprps, i;
638
639 length -= (page_size - offset);
640 if (length <= 0) {
641 iod->first_dma = 0;
642 goto done;
643 }
644
645 dma_len -= (page_size - offset);
646 if (dma_len) {
647 dma_addr += (page_size - offset);
648 } else {
649 sg = sg_next(sg);
650 dma_addr = sg_dma_address(sg);
651 dma_len = sg_dma_len(sg);
652 }
653
654 if (length <= page_size) {
655 iod->first_dma = dma_addr;
656 goto done;
657 }
658
659 nprps = DIV_ROUND_UP(length, page_size);
660 if (nprps <= (256 / 8)) {
661 pool = dev->prp_small_pool;
662 iod->npages = 0;
663 } else {
664 pool = dev->prp_page_pool;
665 iod->npages = 1;
666 }
667
668 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
669 if (!prp_list) {
670 iod->first_dma = dma_addr;
671 iod->npages = -1;
672 return BLK_STS_RESOURCE;
673 }
674 list[0] = prp_list;
675 iod->first_dma = prp_dma;
676 i = 0;
677 for (;;) {
678 if (i == page_size >> 3) {
679 __le64 *old_prp_list = prp_list;
680 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
681 if (!prp_list)
682 return BLK_STS_RESOURCE;
683 list[iod->npages++] = prp_list;
684 prp_list[0] = old_prp_list[i - 1];
685 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
686 i = 1;
687 }
688 prp_list[i++] = cpu_to_le64(dma_addr);
689 dma_len -= page_size;
690 dma_addr += page_size;
691 length -= page_size;
692 if (length <= 0)
693 break;
694 if (dma_len > 0)
695 continue;
696 if (unlikely(dma_len < 0))
697 goto bad_sgl;
698 sg = sg_next(sg);
699 dma_addr = sg_dma_address(sg);
700 dma_len = sg_dma_len(sg);
701 }
702
703 done:
704 cmnd->dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
705 cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma);
706
707 return BLK_STS_OK;
708
709 bad_sgl:
710 WARN(DO_ONCE(nvme_print_sgl, iod->sg, iod->nents),
711 "Invalid SGL for payload:%d nents:%d\n",
712 blk_rq_payload_bytes(req), iod->nents);
713 return BLK_STS_IOERR;
714 }
715
716 static void nvme_pci_sgl_set_data(struct nvme_sgl_desc *sge,
717 struct scatterlist *sg)
718 {
719 sge->addr = cpu_to_le64(sg_dma_address(sg));
720 sge->length = cpu_to_le32(sg_dma_len(sg));
721 sge->type = NVME_SGL_FMT_DATA_DESC << 4;
722 }
723
724 static void nvme_pci_sgl_set_seg(struct nvme_sgl_desc *sge,
725 dma_addr_t dma_addr, int entries)
726 {
727 sge->addr = cpu_to_le64(dma_addr);
728 if (entries < SGES_PER_PAGE) {
729 sge->length = cpu_to_le32(entries * sizeof(*sge));
730 sge->type = NVME_SGL_FMT_LAST_SEG_DESC << 4;
731 } else {
732 sge->length = cpu_to_le32(PAGE_SIZE);
733 sge->type = NVME_SGL_FMT_SEG_DESC << 4;
734 }
735 }
736
737 static blk_status_t nvme_pci_setup_sgls(struct nvme_dev *dev,
738 struct request *req, struct nvme_rw_command *cmd, int entries)
739 {
740 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
741 struct dma_pool *pool;
742 struct nvme_sgl_desc *sg_list;
743 struct scatterlist *sg = iod->sg;
744 dma_addr_t sgl_dma;
745 int i = 0;
746
747 /* setting the transfer type as SGL */
748 cmd->flags = NVME_CMD_SGL_METABUF;
749
750 if (entries == 1) {
751 nvme_pci_sgl_set_data(&cmd->dptr.sgl, sg);
752 return BLK_STS_OK;
753 }
754
755 if (entries <= (256 / sizeof(struct nvme_sgl_desc))) {
756 pool = dev->prp_small_pool;
757 iod->npages = 0;
758 } else {
759 pool = dev->prp_page_pool;
760 iod->npages = 1;
761 }
762
763 sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma);
764 if (!sg_list) {
765 iod->npages = -1;
766 return BLK_STS_RESOURCE;
767 }
768
769 nvme_pci_iod_list(req)[0] = sg_list;
770 iod->first_dma = sgl_dma;
771
772 nvme_pci_sgl_set_seg(&cmd->dptr.sgl, sgl_dma, entries);
773
774 do {
775 if (i == SGES_PER_PAGE) {
776 struct nvme_sgl_desc *old_sg_desc = sg_list;
777 struct nvme_sgl_desc *link = &old_sg_desc[i - 1];
778
779 sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma);
780 if (!sg_list)
781 return BLK_STS_RESOURCE;
782
783 i = 0;
784 nvme_pci_iod_list(req)[iod->npages++] = sg_list;
785 sg_list[i++] = *link;
786 nvme_pci_sgl_set_seg(link, sgl_dma, entries);
787 }
788
789 nvme_pci_sgl_set_data(&sg_list[i++], sg);
790 sg = sg_next(sg);
791 } while (--entries > 0);
792
793 return BLK_STS_OK;
794 }
795
796 static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,
797 struct nvme_command *cmnd)
798 {
799 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
800 struct request_queue *q = req->q;
801 enum dma_data_direction dma_dir = rq_data_dir(req) ?
802 DMA_TO_DEVICE : DMA_FROM_DEVICE;
803 blk_status_t ret = BLK_STS_IOERR;
804 int nr_mapped;
805
806 sg_init_table(iod->sg, blk_rq_nr_phys_segments(req));
807 iod->nents = blk_rq_map_sg(q, req, iod->sg);
808 if (!iod->nents)
809 goto out;
810
811 ret = BLK_STS_RESOURCE;
812 nr_mapped = dma_map_sg_attrs(dev->dev, iod->sg, iod->nents, dma_dir,
813 DMA_ATTR_NO_WARN);
814 if (!nr_mapped)
815 goto out;
816
817 if (iod->use_sgl)
818 ret = nvme_pci_setup_sgls(dev, req, &cmnd->rw, nr_mapped);
819 else
820 ret = nvme_pci_setup_prps(dev, req, &cmnd->rw);
821
822 if (ret != BLK_STS_OK)
823 goto out_unmap;
824
825 ret = BLK_STS_IOERR;
826 if (blk_integrity_rq(req)) {
827 if (blk_rq_count_integrity_sg(q, req->bio) != 1)
828 goto out_unmap;
829
830 sg_init_table(&iod->meta_sg, 1);
831 if (blk_rq_map_integrity_sg(q, req->bio, &iod->meta_sg) != 1)
832 goto out_unmap;
833
834 if (req_op(req) == REQ_OP_WRITE)
835 nvme_dif_remap(req, nvme_dif_prep);
836
837 if (!dma_map_sg(dev->dev, &iod->meta_sg, 1, dma_dir))
838 goto out_unmap;
839 }
840
841 if (blk_integrity_rq(req))
842 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(&iod->meta_sg));
843 return BLK_STS_OK;
844
845 out_unmap:
846 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
847 out:
848 return ret;
849 }
850
851 static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
852 {
853 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
854 enum dma_data_direction dma_dir = rq_data_dir(req) ?
855 DMA_TO_DEVICE : DMA_FROM_DEVICE;
856
857 if (iod->nents) {
858 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
859 if (blk_integrity_rq(req)) {
860 if (req_op(req) == REQ_OP_READ)
861 nvme_dif_remap(req, nvme_dif_complete);
862 dma_unmap_sg(dev->dev, &iod->meta_sg, 1, dma_dir);
863 }
864 }
865
866 nvme_cleanup_cmd(req);
867 nvme_free_iod(dev, req);
868 }
869
870 /*
871 * NOTE: ns is NULL when called on the admin queue.
872 */
873 static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
874 const struct blk_mq_queue_data *bd)
875 {
876 struct nvme_ns *ns = hctx->queue->queuedata;
877 struct nvme_queue *nvmeq = hctx->driver_data;
878 struct nvme_dev *dev = nvmeq->dev;
879 struct request *req = bd->rq;
880 struct nvme_command cmnd;
881 blk_status_t ret;
882
883 ret = nvme_setup_cmd(ns, req, &cmnd);
884 if (ret)
885 return ret;
886
887 ret = nvme_init_iod(req, dev);
888 if (ret)
889 goto out_free_cmd;
890
891 if (blk_rq_nr_phys_segments(req)) {
892 ret = nvme_map_data(dev, req, &cmnd);
893 if (ret)
894 goto out_cleanup_iod;
895 }
896
897 blk_mq_start_request(req);
898
899 spin_lock_irq(&nvmeq->q_lock);
900 if (unlikely(nvmeq->cq_vector < 0)) {
901 ret = BLK_STS_IOERR;
902 spin_unlock_irq(&nvmeq->q_lock);
903 goto out_cleanup_iod;
904 }
905 __nvme_submit_cmd(nvmeq, &cmnd);
906 nvme_process_cq(nvmeq);
907 spin_unlock_irq(&nvmeq->q_lock);
908 return BLK_STS_OK;
909 out_cleanup_iod:
910 nvme_free_iod(dev, req);
911 out_free_cmd:
912 nvme_cleanup_cmd(req);
913 return ret;
914 }
915
916 static void nvme_pci_complete_rq(struct request *req)
917 {
918 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
919
920 nvme_unmap_data(iod->nvmeq->dev, req);
921 nvme_complete_rq(req);
922 }
923
924 /* We read the CQE phase first to check if the rest of the entry is valid */
925 static inline bool nvme_cqe_valid(struct nvme_queue *nvmeq, u16 head,
926 u16 phase)
927 {
928 return (le16_to_cpu(nvmeq->cqes[head].status) & 1) == phase;
929 }
930
931 static inline void nvme_ring_cq_doorbell(struct nvme_queue *nvmeq)
932 {
933 u16 head = nvmeq->cq_head;
934
935 if (likely(nvmeq->cq_vector >= 0)) {
936 if (nvme_dbbuf_update_and_check_event(head, nvmeq->dbbuf_cq_db,
937 nvmeq->dbbuf_cq_ei))
938 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
939 }
940 }
941
942 static inline void nvme_handle_cqe(struct nvme_queue *nvmeq,
943 struct nvme_completion *cqe)
944 {
945 struct request *req;
946
947 if (unlikely(cqe->command_id >= nvmeq->q_depth)) {
948 dev_warn(nvmeq->dev->ctrl.device,
949 "invalid id %d completed on queue %d\n",
950 cqe->command_id, le16_to_cpu(cqe->sq_id));
951 return;
952 }
953
954 /*
955 * AEN requests are special as they don't time out and can
956 * survive any kind of queue freeze and often don't respond to
957 * aborts. We don't even bother to allocate a struct request
958 * for them but rather special case them here.
959 */
960 if (unlikely(nvmeq->qid == 0 &&
961 cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH)) {
962 nvme_complete_async_event(&nvmeq->dev->ctrl,
963 cqe->status, &cqe->result);
964 return;
965 }
966
967 nvmeq->cqe_seen = 1;
968 req = blk_mq_tag_to_rq(*nvmeq->tags, cqe->command_id);
969 nvme_end_request(req, cqe->status, cqe->result);
970 }
971
972 static inline bool nvme_read_cqe(struct nvme_queue *nvmeq,
973 struct nvme_completion *cqe)
974 {
975 if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase)) {
976 *cqe = nvmeq->cqes[nvmeq->cq_head];
977
978 if (nvmeq->cq_head == nvmeq->q_depth - 1) {
979 nvmeq->cq_head = 0;
980 nvmeq->cq_phase = !nvmeq->cq_phase;
981 } else {
982 nvmeq->cq_head++;
983 }
984 return true;
985 }
986 return false;
987 }
988
989 static void nvme_process_cq(struct nvme_queue *nvmeq)
990 {
991 struct nvme_completion cqe;
992 int consumed = 0;
993
994 while (nvme_read_cqe(nvmeq, &cqe)) {
995 nvme_handle_cqe(nvmeq, &cqe);
996 consumed++;
997 }
998
999 if (consumed)
1000 nvme_ring_cq_doorbell(nvmeq);
1001 }
1002
1003 static irqreturn_t nvme_irq(int irq, void *data)
1004 {
1005 irqreturn_t result;
1006 struct nvme_queue *nvmeq = data;
1007 spin_lock(&nvmeq->q_lock);
1008 nvme_process_cq(nvmeq);
1009 result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
1010 nvmeq->cqe_seen = 0;
1011 spin_unlock(&nvmeq->q_lock);
1012 return result;
1013 }
1014
1015 static irqreturn_t nvme_irq_check(int irq, void *data)
1016 {
1017 struct nvme_queue *nvmeq = data;
1018 if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase))
1019 return IRQ_WAKE_THREAD;
1020 return IRQ_NONE;
1021 }
1022
1023 static int __nvme_poll(struct nvme_queue *nvmeq, unsigned int tag)
1024 {
1025 struct nvme_completion cqe;
1026 int found = 0, consumed = 0;
1027
1028 if (!nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase))
1029 return 0;
1030
1031 spin_lock_irq(&nvmeq->q_lock);
1032 while (nvme_read_cqe(nvmeq, &cqe)) {
1033 nvme_handle_cqe(nvmeq, &cqe);
1034 consumed++;
1035
1036 if (tag == cqe.command_id) {
1037 found = 1;
1038 break;
1039 }
1040 }
1041
1042 if (consumed)
1043 nvme_ring_cq_doorbell(nvmeq);
1044 spin_unlock_irq(&nvmeq->q_lock);
1045
1046 return found;
1047 }
1048
1049 static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1050 {
1051 struct nvme_queue *nvmeq = hctx->driver_data;
1052
1053 return __nvme_poll(nvmeq, tag);
1054 }
1055
1056 static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl)
1057 {
1058 struct nvme_dev *dev = to_nvme_dev(ctrl);
1059 struct nvme_queue *nvmeq = &dev->queues[0];
1060 struct nvme_command c;
1061
1062 memset(&c, 0, sizeof(c));
1063 c.common.opcode = nvme_admin_async_event;
1064 c.common.command_id = NVME_AQ_BLK_MQ_DEPTH;
1065
1066 spin_lock_irq(&nvmeq->q_lock);
1067 __nvme_submit_cmd(nvmeq, &c);
1068 spin_unlock_irq(&nvmeq->q_lock);
1069 }
1070
1071 static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
1072 {
1073 struct nvme_command c;
1074
1075 memset(&c, 0, sizeof(c));
1076 c.delete_queue.opcode = opcode;
1077 c.delete_queue.qid = cpu_to_le16(id);
1078
1079 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1080 }
1081
1082 static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
1083 struct nvme_queue *nvmeq)
1084 {
1085 struct nvme_ctrl *ctrl = &dev->ctrl;
1086 struct nvme_command c;
1087 int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1088
1089 /*
1090 * Some drives have a bug that auto-enables WRRU if MEDIUM isn't
1091 * set. Since URGENT priority is zeroes, it makes all queues
1092 * URGENT.
1093 */
1094 if (ctrl->quirks & NVME_QUIRK_MEDIUM_PRIO_SQ)
1095 flags |= NVME_SQ_PRIO_MEDIUM;
1096
1097 /*
1098 * Note: we (ab)use the fact that the prp fields survive if no data
1099 * is attached to the request.
1100 */
1101 memset(&c, 0, sizeof(c));
1102 c.create_cq.opcode = nvme_admin_create_cq;
1103 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
1104 c.create_cq.cqid = cpu_to_le16(qid);
1105 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1106 c.create_cq.cq_flags = cpu_to_le16(flags);
1107 c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
1108
1109 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1110 }
1111
1112 static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
1113 struct nvme_queue *nvmeq)
1114 {
1115 struct nvme_command c;
1116 int flags = NVME_QUEUE_PHYS_CONTIG;
1117
1118 /*
1119 * Note: we (ab)use the fact that the prp fields survive if no data
1120 * is attached to the request.
1121 */
1122 memset(&c, 0, sizeof(c));
1123 c.create_sq.opcode = nvme_admin_create_sq;
1124 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
1125 c.create_sq.sqid = cpu_to_le16(qid);
1126 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
1127 c.create_sq.sq_flags = cpu_to_le16(flags);
1128 c.create_sq.cqid = cpu_to_le16(qid);
1129
1130 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1131 }
1132
1133 static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
1134 {
1135 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
1136 }
1137
1138 static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
1139 {
1140 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
1141 }
1142
1143 static void abort_endio(struct request *req, blk_status_t error)
1144 {
1145 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
1146 struct nvme_queue *nvmeq = iod->nvmeq;
1147
1148 dev_warn(nvmeq->dev->ctrl.device,
1149 "Abort status: 0x%x", nvme_req(req)->status);
1150 atomic_inc(&nvmeq->dev->ctrl.abort_limit);
1151 blk_mq_free_request(req);
1152 }
1153
1154 static bool nvme_should_reset(struct nvme_dev *dev, u32 csts)
1155 {
1156
1157 /* If true, indicates loss of adapter communication, possibly by a
1158 * NVMe Subsystem reset.
1159 */
1160 bool nssro = dev->subsystem && (csts & NVME_CSTS_NSSRO);
1161
1162 /* If there is a reset ongoing, we shouldn't reset again. */
1163 if (dev->ctrl.state == NVME_CTRL_RESETTING)
1164 return false;
1165
1166 /* We shouldn't reset unless the controller is on fatal error state
1167 * _or_ if we lost the communication with it.
1168 */
1169 if (!(csts & NVME_CSTS_CFS) && !nssro)
1170 return false;
1171
1172 return true;
1173 }
1174
1175 static void nvme_warn_reset(struct nvme_dev *dev, u32 csts)
1176 {
1177 /* Read a config register to help see what died. */
1178 u16 pci_status;
1179 int result;
1180
1181 result = pci_read_config_word(to_pci_dev(dev->dev), PCI_STATUS,
1182 &pci_status);
1183 if (result == PCIBIOS_SUCCESSFUL)
1184 dev_warn(dev->ctrl.device,
1185 "controller is down; will reset: CSTS=0x%x, PCI_STATUS=0x%hx\n",
1186 csts, pci_status);
1187 else
1188 dev_warn(dev->ctrl.device,
1189 "controller is down; will reset: CSTS=0x%x, PCI_STATUS read failed (%d)\n",
1190 csts, result);
1191 }
1192
1193 static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
1194 {
1195 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
1196 struct nvme_queue *nvmeq = iod->nvmeq;
1197 struct nvme_dev *dev = nvmeq->dev;
1198 struct request *abort_req;
1199 struct nvme_command cmd;
1200 u32 csts = readl(dev->bar + NVME_REG_CSTS);
1201
1202 /* If PCI error recovery process is happening, we cannot reset or
1203 * the recovery mechanism will surely fail.
1204 */
1205 mb();
1206 if (pci_channel_offline(to_pci_dev(dev->dev)))
1207 return BLK_EH_RESET_TIMER;
1208
1209 /*
1210 * Reset immediately if the controller is failed
1211 */
1212 if (nvme_should_reset(dev, csts)) {
1213 nvme_warn_reset(dev, csts);
1214 nvme_dev_disable(dev, false);
1215 nvme_reset_ctrl(&dev->ctrl);
1216 return BLK_EH_HANDLED;
1217 }
1218
1219 /*
1220 * Did we miss an interrupt?
1221 */
1222 if (__nvme_poll(nvmeq, req->tag)) {
1223 dev_warn(dev->ctrl.device,
1224 "I/O %d QID %d timeout, completion polled\n",
1225 req->tag, nvmeq->qid);
1226 return BLK_EH_HANDLED;
1227 }
1228
1229 /*
1230 * Shutdown immediately if controller times out while starting. The
1231 * reset work will see the pci device disabled when it gets the forced
1232 * cancellation error. All outstanding requests are completed on
1233 * shutdown, so we return BLK_EH_HANDLED.
1234 */
1235 if (dev->ctrl.state == NVME_CTRL_RESETTING) {
1236 dev_warn(dev->ctrl.device,
1237 "I/O %d QID %d timeout, disable controller\n",
1238 req->tag, nvmeq->qid);
1239 nvme_dev_disable(dev, false);
1240 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
1241 return BLK_EH_HANDLED;
1242 }
1243
1244 /*
1245 * Shutdown the controller immediately and schedule a reset if the
1246 * command was already aborted once before and still hasn't been
1247 * returned to the driver, or if this is the admin queue.
1248 */
1249 if (!nvmeq->qid || iod->aborted) {
1250 dev_warn(dev->ctrl.device,
1251 "I/O %d QID %d timeout, reset controller\n",
1252 req->tag, nvmeq->qid);
1253 nvme_dev_disable(dev, false);
1254 nvme_reset_ctrl(&dev->ctrl);
1255
1256 /*
1257 * Mark the request as handled, since the inline shutdown
1258 * forces all outstanding requests to complete.
1259 */
1260 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
1261 return BLK_EH_HANDLED;
1262 }
1263
1264 if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) {
1265 atomic_inc(&dev->ctrl.abort_limit);
1266 return BLK_EH_RESET_TIMER;
1267 }
1268 iod->aborted = 1;
1269
1270 memset(&cmd, 0, sizeof(cmd));
1271 cmd.abort.opcode = nvme_admin_abort_cmd;
1272 cmd.abort.cid = req->tag;
1273 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
1274
1275 dev_warn(nvmeq->dev->ctrl.device,
1276 "I/O %d QID %d timeout, aborting\n",
1277 req->tag, nvmeq->qid);
1278
1279 abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd,
1280 BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
1281 if (IS_ERR(abort_req)) {
1282 atomic_inc(&dev->ctrl.abort_limit);
1283 return BLK_EH_RESET_TIMER;
1284 }
1285
1286 abort_req->timeout = ADMIN_TIMEOUT;
1287 abort_req->end_io_data = NULL;
1288 blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio);
1289
1290 /*
1291 * The aborted req will be completed on receiving the abort req.
1292 * We enable the timer again. If hit twice, it'll cause a device reset,
1293 * as the device then is in a faulty state.
1294 */
1295 return BLK_EH_RESET_TIMER;
1296 }
1297
1298 static void nvme_free_queue(struct nvme_queue *nvmeq)
1299 {
1300 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1301 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1302 if (nvmeq->sq_cmds)
1303 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1304 nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1305 }
1306
1307 static void nvme_free_queues(struct nvme_dev *dev, int lowest)
1308 {
1309 int i;
1310
1311 for (i = dev->ctrl.queue_count - 1; i >= lowest; i--) {
1312 dev->ctrl.queue_count--;
1313 nvme_free_queue(&dev->queues[i]);
1314 }
1315 }
1316
1317 /**
1318 * nvme_suspend_queue - put queue into suspended state
1319 * @nvmeq - queue to suspend
1320 */
1321 static int nvme_suspend_queue(struct nvme_queue *nvmeq)
1322 {
1323 int vector;
1324
1325 spin_lock_irq(&nvmeq->q_lock);
1326 if (nvmeq->cq_vector == -1) {
1327 spin_unlock_irq(&nvmeq->q_lock);
1328 return 1;
1329 }
1330 vector = nvmeq->cq_vector;
1331 nvmeq->dev->online_queues--;
1332 nvmeq->cq_vector = -1;
1333 spin_unlock_irq(&nvmeq->q_lock);
1334
1335 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
1336 blk_mq_quiesce_queue(nvmeq->dev->ctrl.admin_q);
1337
1338 pci_free_irq(to_pci_dev(nvmeq->dev->dev), vector, nvmeq);
1339
1340 return 0;
1341 }
1342
1343 static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown)
1344 {
1345 struct nvme_queue *nvmeq = &dev->queues[0];
1346
1347 if (nvme_suspend_queue(nvmeq))
1348 return;
1349
1350 if (shutdown)
1351 nvme_shutdown_ctrl(&dev->ctrl);
1352 else
1353 nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
1354
1355 spin_lock_irq(&nvmeq->q_lock);
1356 nvme_process_cq(nvmeq);
1357 spin_unlock_irq(&nvmeq->q_lock);
1358 }
1359
1360 static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
1361 int entry_size)
1362 {
1363 int q_depth = dev->q_depth;
1364 unsigned q_size_aligned = roundup(q_depth * entry_size,
1365 dev->ctrl.page_size);
1366
1367 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
1368 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
1369 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
1370 q_depth = div_u64(mem_per_q, entry_size);
1371
1372 /*
1373 * Ensure the reduced q_depth is above some threshold where it
1374 * would be better to map queues in system memory with the
1375 * original depth
1376 */
1377 if (q_depth < 64)
1378 return -ENOMEM;
1379 }
1380
1381 return q_depth;
1382 }
1383
1384 static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1385 int qid, int depth)
1386 {
1387
1388 /* CMB SQEs will be mapped before creation */
1389 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz))
1390 return 0;
1391
1392 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1393 &nvmeq->sq_dma_addr, GFP_KERNEL);
1394 if (!nvmeq->sq_cmds)
1395 return -ENOMEM;
1396
1397 return 0;
1398 }
1399
1400 static int nvme_alloc_queue(struct nvme_dev *dev, int qid,
1401 int depth, int node)
1402 {
1403 struct nvme_queue *nvmeq = &dev->queues[qid];
1404
1405 if (dev->ctrl.queue_count > qid)
1406 return 0;
1407
1408 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
1409 &nvmeq->cq_dma_addr, GFP_KERNEL);
1410 if (!nvmeq->cqes)
1411 goto free_nvmeq;
1412
1413 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
1414 goto free_cqdma;
1415
1416 nvmeq->q_dmadev = dev->dev;
1417 nvmeq->dev = dev;
1418 spin_lock_init(&nvmeq->q_lock);
1419 nvmeq->cq_head = 0;
1420 nvmeq->cq_phase = 1;
1421 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
1422 nvmeq->q_depth = depth;
1423 nvmeq->qid = qid;
1424 nvmeq->cq_vector = -1;
1425 dev->ctrl.queue_count++;
1426
1427 return 0;
1428
1429 free_cqdma:
1430 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
1431 nvmeq->cq_dma_addr);
1432 free_nvmeq:
1433 return -ENOMEM;
1434 }
1435
1436 static int queue_request_irq(struct nvme_queue *nvmeq)
1437 {
1438 struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev);
1439 int nr = nvmeq->dev->ctrl.instance;
1440
1441 if (use_threaded_interrupts) {
1442 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq_check,
1443 nvme_irq, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1444 } else {
1445 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq,
1446 NULL, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
1447 }
1448 }
1449
1450 static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
1451 {
1452 struct nvme_dev *dev = nvmeq->dev;
1453
1454 spin_lock_irq(&nvmeq->q_lock);
1455 nvmeq->sq_tail = 0;
1456 nvmeq->cq_head = 0;
1457 nvmeq->cq_phase = 1;
1458 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
1459 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1460 nvme_dbbuf_init(dev, nvmeq, qid);
1461 dev->online_queues++;
1462 spin_unlock_irq(&nvmeq->q_lock);
1463 }
1464
1465 static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1466 {
1467 struct nvme_dev *dev = nvmeq->dev;
1468 int result;
1469
1470 if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
1471 unsigned offset = (qid - 1) * roundup(SQ_SIZE(nvmeq->q_depth),
1472 dev->ctrl.page_size);
1473 nvmeq->sq_dma_addr = dev->cmb_bus_addr + offset;
1474 nvmeq->sq_cmds_io = dev->cmb + offset;
1475 }
1476
1477 nvmeq->cq_vector = qid - 1;
1478 result = adapter_alloc_cq(dev, qid, nvmeq);
1479 if (result < 0)
1480 goto release_vector;
1481
1482 result = adapter_alloc_sq(dev, qid, nvmeq);
1483 if (result < 0)
1484 goto release_cq;
1485
1486 nvme_init_queue(nvmeq, qid);
1487 result = queue_request_irq(nvmeq);
1488 if (result < 0)
1489 goto release_sq;
1490
1491 return result;
1492
1493 release_sq:
1494 dev->online_queues--;
1495 adapter_delete_sq(dev, qid);
1496 release_cq:
1497 adapter_delete_cq(dev, qid);
1498 release_vector:
1499 nvmeq->cq_vector = -1;
1500 return result;
1501 }
1502
1503 static const struct blk_mq_ops nvme_mq_admin_ops = {
1504 .queue_rq = nvme_queue_rq,
1505 .complete = nvme_pci_complete_rq,
1506 .init_hctx = nvme_admin_init_hctx,
1507 .exit_hctx = nvme_admin_exit_hctx,
1508 .init_request = nvme_init_request,
1509 .timeout = nvme_timeout,
1510 };
1511
1512 static const struct blk_mq_ops nvme_mq_ops = {
1513 .queue_rq = nvme_queue_rq,
1514 .complete = nvme_pci_complete_rq,
1515 .init_hctx = nvme_init_hctx,
1516 .init_request = nvme_init_request,
1517 .map_queues = nvme_pci_map_queues,
1518 .timeout = nvme_timeout,
1519 .poll = nvme_poll,
1520 };
1521
1522 static void nvme_dev_remove_admin(struct nvme_dev *dev)
1523 {
1524 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
1525 /*
1526 * If the controller was reset during removal, it's possible
1527 * user requests may be waiting on a stopped queue. Start the
1528 * queue to flush these to completion.
1529 */
1530 blk_mq_unquiesce_queue(dev->ctrl.admin_q);
1531 blk_cleanup_queue(dev->ctrl.admin_q);
1532 blk_mq_free_tag_set(&dev->admin_tagset);
1533 }
1534 }
1535
1536 static int nvme_alloc_admin_tags(struct nvme_dev *dev)
1537 {
1538 if (!dev->ctrl.admin_q) {
1539 dev->admin_tagset.ops = &nvme_mq_admin_ops;
1540 dev->admin_tagset.nr_hw_queues = 1;
1541
1542 dev->admin_tagset.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
1543 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
1544 dev->admin_tagset.numa_node = dev_to_node(dev->dev);
1545 dev->admin_tagset.cmd_size = nvme_pci_cmd_size(dev, false);
1546 dev->admin_tagset.flags = BLK_MQ_F_NO_SCHED;
1547 dev->admin_tagset.driver_data = dev;
1548
1549 if (blk_mq_alloc_tag_set(&dev->admin_tagset))
1550 return -ENOMEM;
1551 dev->ctrl.admin_tagset = &dev->admin_tagset;
1552
1553 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
1554 if (IS_ERR(dev->ctrl.admin_q)) {
1555 blk_mq_free_tag_set(&dev->admin_tagset);
1556 return -ENOMEM;
1557 }
1558 if (!blk_get_queue(dev->ctrl.admin_q)) {
1559 nvme_dev_remove_admin(dev);
1560 dev->ctrl.admin_q = NULL;
1561 return -ENODEV;
1562 }
1563 } else
1564 blk_mq_unquiesce_queue(dev->ctrl.admin_q);
1565
1566 return 0;
1567 }
1568
1569 static unsigned long db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
1570 {
1571 return NVME_REG_DBS + ((nr_io_queues + 1) * 8 * dev->db_stride);
1572 }
1573
1574 static int nvme_remap_bar(struct nvme_dev *dev, unsigned long size)
1575 {
1576 struct pci_dev *pdev = to_pci_dev(dev->dev);
1577
1578 if (size <= dev->bar_mapped_size)
1579 return 0;
1580 if (size > pci_resource_len(pdev, 0))
1581 return -ENOMEM;
1582 if (dev->bar)
1583 iounmap(dev->bar);
1584 dev->bar = ioremap(pci_resource_start(pdev, 0), size);
1585 if (!dev->bar) {
1586 dev->bar_mapped_size = 0;
1587 return -ENOMEM;
1588 }
1589 dev->bar_mapped_size = size;
1590 dev->dbs = dev->bar + NVME_REG_DBS;
1591
1592 return 0;
1593 }
1594
1595 static int nvme_pci_configure_admin_queue(struct nvme_dev *dev)
1596 {
1597 int result;
1598 u32 aqa;
1599 struct nvme_queue *nvmeq;
1600
1601 result = nvme_remap_bar(dev, db_bar_size(dev, 0));
1602 if (result < 0)
1603 return result;
1604
1605 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1, 0) ?
1606 NVME_CAP_NSSRC(dev->ctrl.cap) : 0;
1607
1608 if (dev->subsystem &&
1609 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
1610 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
1611
1612 result = nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
1613 if (result < 0)
1614 return result;
1615
1616 result = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH,
1617 dev_to_node(dev->dev));
1618 if (result)
1619 return result;
1620
1621 nvmeq = &dev->queues[0];
1622 aqa = nvmeq->q_depth - 1;
1623 aqa |= aqa << 16;
1624
1625 writel(aqa, dev->bar + NVME_REG_AQA);
1626 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
1627 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
1628
1629 result = nvme_enable_ctrl(&dev->ctrl, dev->ctrl.cap);
1630 if (result)
1631 return result;
1632
1633 nvmeq->cq_vector = 0;
1634 nvme_init_queue(nvmeq, 0);
1635 result = queue_request_irq(nvmeq);
1636 if (result) {
1637 nvmeq->cq_vector = -1;
1638 return result;
1639 }
1640
1641 return result;
1642 }
1643
1644 static int nvme_create_io_queues(struct nvme_dev *dev)
1645 {
1646 unsigned i, max;
1647 int ret = 0;
1648
1649 for (i = dev->ctrl.queue_count; i <= dev->max_qid; i++) {
1650 /* vector == qid - 1, match nvme_create_queue */
1651 if (nvme_alloc_queue(dev, i, dev->q_depth,
1652 pci_irq_get_node(to_pci_dev(dev->dev), i - 1))) {
1653 ret = -ENOMEM;
1654 break;
1655 }
1656 }
1657
1658 max = min(dev->max_qid, dev->ctrl.queue_count - 1);
1659 for (i = dev->online_queues; i <= max; i++) {
1660 ret = nvme_create_queue(&dev->queues[i], i);
1661 if (ret)
1662 break;
1663 }
1664
1665 /*
1666 * Ignore failing Create SQ/CQ commands, we can continue with less
1667 * than the desired aount of queues, and even a controller without
1668 * I/O queues an still be used to issue admin commands. This might
1669 * be useful to upgrade a buggy firmware for example.
1670 */
1671 return ret >= 0 ? 0 : ret;
1672 }
1673
1674 static ssize_t nvme_cmb_show(struct device *dev,
1675 struct device_attribute *attr,
1676 char *buf)
1677 {
1678 struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
1679
1680 return scnprintf(buf, PAGE_SIZE, "cmbloc : x%08x\ncmbsz : x%08x\n",
1681 ndev->cmbloc, ndev->cmbsz);
1682 }
1683 static DEVICE_ATTR(cmb, S_IRUGO, nvme_cmb_show, NULL);
1684
1685 static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
1686 {
1687 u64 szu, size, offset;
1688 resource_size_t bar_size;
1689 struct pci_dev *pdev = to_pci_dev(dev->dev);
1690 void __iomem *cmb;
1691 int bar;
1692
1693 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
1694 if (!(NVME_CMB_SZ(dev->cmbsz)))
1695 return NULL;
1696 dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
1697
1698 if (!use_cmb_sqes)
1699 return NULL;
1700
1701 szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
1702 size = szu * NVME_CMB_SZ(dev->cmbsz);
1703 offset = szu * NVME_CMB_OFST(dev->cmbloc);
1704 bar = NVME_CMB_BIR(dev->cmbloc);
1705 bar_size = pci_resource_len(pdev, bar);
1706
1707 if (offset > bar_size)
1708 return NULL;
1709
1710 /*
1711 * Controllers may support a CMB size larger than their BAR,
1712 * for example, due to being behind a bridge. Reduce the CMB to
1713 * the reported size of the BAR
1714 */
1715 if (size > bar_size - offset)
1716 size = bar_size - offset;
1717
1718 cmb = ioremap_wc(pci_resource_start(pdev, bar) + offset, size);
1719 if (!cmb)
1720 return NULL;
1721
1722 dev->cmb_bus_addr = pci_bus_address(pdev, bar) + offset;
1723 dev->cmb_size = size;
1724 return cmb;
1725 }
1726
1727 static inline void nvme_release_cmb(struct nvme_dev *dev)
1728 {
1729 if (dev->cmb) {
1730 iounmap(dev->cmb);
1731 dev->cmb = NULL;
1732 sysfs_remove_file_from_group(&dev->ctrl.device->kobj,
1733 &dev_attr_cmb.attr, NULL);
1734 dev->cmbsz = 0;
1735 }
1736 }
1737
1738 static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits)
1739 {
1740 u64 dma_addr = dev->host_mem_descs_dma;
1741 struct nvme_command c;
1742 int ret;
1743
1744 memset(&c, 0, sizeof(c));
1745 c.features.opcode = nvme_admin_set_features;
1746 c.features.fid = cpu_to_le32(NVME_FEAT_HOST_MEM_BUF);
1747 c.features.dword11 = cpu_to_le32(bits);
1748 c.features.dword12 = cpu_to_le32(dev->host_mem_size >>
1749 ilog2(dev->ctrl.page_size));
1750 c.features.dword13 = cpu_to_le32(lower_32_bits(dma_addr));
1751 c.features.dword14 = cpu_to_le32(upper_32_bits(dma_addr));
1752 c.features.dword15 = cpu_to_le32(dev->nr_host_mem_descs);
1753
1754 ret = nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
1755 if (ret) {
1756 dev_warn(dev->ctrl.device,
1757 "failed to set host mem (err %d, flags %#x).\n",
1758 ret, bits);
1759 }
1760 return ret;
1761 }
1762
1763 static void nvme_free_host_mem(struct nvme_dev *dev)
1764 {
1765 int i;
1766
1767 for (i = 0; i < dev->nr_host_mem_descs; i++) {
1768 struct nvme_host_mem_buf_desc *desc = &dev->host_mem_descs[i];
1769 size_t size = le32_to_cpu(desc->size) * dev->ctrl.page_size;
1770
1771 dma_free_attrs(dev->dev, size, dev->host_mem_desc_bufs[i],
1772 le64_to_cpu(desc->addr),
1773 DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
1774 }
1775
1776 kfree(dev->host_mem_desc_bufs);
1777 dev->host_mem_desc_bufs = NULL;
1778 dma_free_coherent(dev->dev,
1779 dev->nr_host_mem_descs * sizeof(*dev->host_mem_descs),
1780 dev->host_mem_descs, dev->host_mem_descs_dma);
1781 dev->host_mem_descs = NULL;
1782 dev->nr_host_mem_descs = 0;
1783 }
1784
1785 static int __nvme_alloc_host_mem(struct nvme_dev *dev, u64 preferred,
1786 u32 chunk_size)
1787 {
1788 struct nvme_host_mem_buf_desc *descs;
1789 u32 max_entries, len;
1790 dma_addr_t descs_dma;
1791 int i = 0;
1792 void **bufs;
1793 u64 size = 0, tmp;
1794
1795 tmp = (preferred + chunk_size - 1);
1796 do_div(tmp, chunk_size);
1797 max_entries = tmp;
1798
1799 if (dev->ctrl.hmmaxd && dev->ctrl.hmmaxd < max_entries)
1800 max_entries = dev->ctrl.hmmaxd;
1801
1802 descs = dma_zalloc_coherent(dev->dev, max_entries * sizeof(*descs),
1803 &descs_dma, GFP_KERNEL);
1804 if (!descs)
1805 goto out;
1806
1807 bufs = kcalloc(max_entries, sizeof(*bufs), GFP_KERNEL);
1808 if (!bufs)
1809 goto out_free_descs;
1810
1811 for (size = 0; size < preferred && i < max_entries; size += len) {
1812 dma_addr_t dma_addr;
1813
1814 len = min_t(u64, chunk_size, preferred - size);
1815 bufs[i] = dma_alloc_attrs(dev->dev, len, &dma_addr, GFP_KERNEL,
1816 DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
1817 if (!bufs[i])
1818 break;
1819
1820 descs[i].addr = cpu_to_le64(dma_addr);
1821 descs[i].size = cpu_to_le32(len / dev->ctrl.page_size);
1822 i++;
1823 }
1824
1825 if (!size)
1826 goto out_free_bufs;
1827
1828 dev->nr_host_mem_descs = i;
1829 dev->host_mem_size = size;
1830 dev->host_mem_descs = descs;
1831 dev->host_mem_descs_dma = descs_dma;
1832 dev->host_mem_desc_bufs = bufs;
1833 return 0;
1834
1835 out_free_bufs:
1836 while (--i >= 0) {
1837 size_t size = le32_to_cpu(descs[i].size) * dev->ctrl.page_size;
1838
1839 dma_free_attrs(dev->dev, size, bufs[i],
1840 le64_to_cpu(descs[i].addr),
1841 DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
1842 }
1843
1844 kfree(bufs);
1845 out_free_descs:
1846 dma_free_coherent(dev->dev, max_entries * sizeof(*descs), descs,
1847 descs_dma);
1848 out:
1849 dev->host_mem_descs = NULL;
1850 return -ENOMEM;
1851 }
1852
1853 static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
1854 {
1855 u32 chunk_size;
1856
1857 /* start big and work our way down */
1858 for (chunk_size = min_t(u64, preferred, PAGE_SIZE * MAX_ORDER_NR_PAGES);
1859 chunk_size >= max_t(u32, dev->ctrl.hmminds * 4096, PAGE_SIZE * 2);
1860 chunk_size /= 2) {
1861 if (!__nvme_alloc_host_mem(dev, preferred, chunk_size)) {
1862 if (!min || dev->host_mem_size >= min)
1863 return 0;
1864 nvme_free_host_mem(dev);
1865 }
1866 }
1867
1868 return -ENOMEM;
1869 }
1870
1871 static int nvme_setup_host_mem(struct nvme_dev *dev)
1872 {
1873 u64 max = (u64)max_host_mem_size_mb * SZ_1M;
1874 u64 preferred = (u64)dev->ctrl.hmpre * 4096;
1875 u64 min = (u64)dev->ctrl.hmmin * 4096;
1876 u32 enable_bits = NVME_HOST_MEM_ENABLE;
1877 int ret = 0;
1878
1879 preferred = min(preferred, max);
1880 if (min > max) {
1881 dev_warn(dev->ctrl.device,
1882 "min host memory (%lld MiB) above limit (%d MiB).\n",
1883 min >> ilog2(SZ_1M), max_host_mem_size_mb);
1884 nvme_free_host_mem(dev);
1885 return 0;
1886 }
1887
1888 /*
1889 * If we already have a buffer allocated check if we can reuse it.
1890 */
1891 if (dev->host_mem_descs) {
1892 if (dev->host_mem_size >= min)
1893 enable_bits |= NVME_HOST_MEM_RETURN;
1894 else
1895 nvme_free_host_mem(dev);
1896 }
1897
1898 if (!dev->host_mem_descs) {
1899 if (nvme_alloc_host_mem(dev, min, preferred)) {
1900 dev_warn(dev->ctrl.device,
1901 "failed to allocate host memory buffer.\n");
1902 return 0; /* controller must work without HMB */
1903 }
1904
1905 dev_info(dev->ctrl.device,
1906 "allocated %lld MiB host memory buffer.\n",
1907 dev->host_mem_size >> ilog2(SZ_1M));
1908 }
1909
1910 ret = nvme_set_host_mem(dev, enable_bits);
1911 if (ret)
1912 nvme_free_host_mem(dev);
1913 return ret;
1914 }
1915
1916 static int nvme_setup_io_queues(struct nvme_dev *dev)
1917 {
1918 struct nvme_queue *adminq = &dev->queues[0];
1919 struct pci_dev *pdev = to_pci_dev(dev->dev);
1920 int result, nr_io_queues;
1921 unsigned long size;
1922
1923 nr_io_queues = num_possible_cpus();
1924 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
1925 if (result < 0)
1926 return result;
1927
1928 if (nr_io_queues == 0)
1929 return 0;
1930
1931 if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
1932 result = nvme_cmb_qdepth(dev, nr_io_queues,
1933 sizeof(struct nvme_command));
1934 if (result > 0)
1935 dev->q_depth = result;
1936 else
1937 nvme_release_cmb(dev);
1938 }
1939
1940 do {
1941 size = db_bar_size(dev, nr_io_queues);
1942 result = nvme_remap_bar(dev, size);
1943 if (!result)
1944 break;
1945 if (!--nr_io_queues)
1946 return -ENOMEM;
1947 } while (1);
1948 adminq->q_db = dev->dbs;
1949
1950 /* Deregister the admin queue's interrupt */
1951 pci_free_irq(pdev, 0, adminq);
1952
1953 /*
1954 * If we enable msix early due to not intx, disable it again before
1955 * setting up the full range we need.
1956 */
1957 pci_free_irq_vectors(pdev);
1958 nr_io_queues = pci_alloc_irq_vectors(pdev, 1, nr_io_queues,
1959 PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY);
1960 if (nr_io_queues <= 0)
1961 return -EIO;
1962 dev->max_qid = nr_io_queues;
1963
1964 /*
1965 * Should investigate if there's a performance win from allocating
1966 * more queues than interrupt vectors; it might allow the submission
1967 * path to scale better, even if the receive path is limited by the
1968 * number of interrupts.
1969 */
1970
1971 result = queue_request_irq(adminq);
1972 if (result) {
1973 adminq->cq_vector = -1;
1974 return result;
1975 }
1976 return nvme_create_io_queues(dev);
1977 }
1978
1979 static void nvme_del_queue_end(struct request *req, blk_status_t error)
1980 {
1981 struct nvme_queue *nvmeq = req->end_io_data;
1982
1983 blk_mq_free_request(req);
1984 complete(&nvmeq->dev->ioq_wait);
1985 }
1986
1987 static void nvme_del_cq_end(struct request *req, blk_status_t error)
1988 {
1989 struct nvme_queue *nvmeq = req->end_io_data;
1990
1991 if (!error) {
1992 unsigned long flags;
1993
1994 /*
1995 * We might be called with the AQ q_lock held
1996 * and the I/O queue q_lock should always
1997 * nest inside the AQ one.
1998 */
1999 spin_lock_irqsave_nested(&nvmeq->q_lock, flags,
2000 SINGLE_DEPTH_NESTING);
2001 nvme_process_cq(nvmeq);
2002 spin_unlock_irqrestore(&nvmeq->q_lock, flags);
2003 }
2004
2005 nvme_del_queue_end(req, error);
2006 }
2007
2008 static int nvme_delete_queue(struct nvme_queue *nvmeq, u8 opcode)
2009 {
2010 struct request_queue *q = nvmeq->dev->ctrl.admin_q;
2011 struct request *req;
2012 struct nvme_command cmd;
2013
2014 memset(&cmd, 0, sizeof(cmd));
2015 cmd.delete_queue.opcode = opcode;
2016 cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid);
2017
2018 req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
2019 if (IS_ERR(req))
2020 return PTR_ERR(req);
2021
2022 req->timeout = ADMIN_TIMEOUT;
2023 req->end_io_data = nvmeq;
2024
2025 blk_execute_rq_nowait(q, NULL, req, false,
2026 opcode == nvme_admin_delete_cq ?
2027 nvme_del_cq_end : nvme_del_queue_end);
2028 return 0;
2029 }
2030
2031 static void nvme_disable_io_queues(struct nvme_dev *dev, int queues)
2032 {
2033 int pass;
2034 unsigned long timeout;
2035 u8 opcode = nvme_admin_delete_sq;
2036
2037 for (pass = 0; pass < 2; pass++) {
2038 int sent = 0, i = queues;
2039
2040 reinit_completion(&dev->ioq_wait);
2041 retry:
2042 timeout = ADMIN_TIMEOUT;
2043 for (; i > 0; i--, sent++)
2044 if (nvme_delete_queue(&dev->queues[i], opcode))
2045 break;
2046
2047 while (sent--) {
2048 timeout = wait_for_completion_io_timeout(&dev->ioq_wait, timeout);
2049 if (timeout == 0)
2050 return;
2051 if (i)
2052 goto retry;
2053 }
2054 opcode = nvme_admin_delete_cq;
2055 }
2056 }
2057
2058 /*
2059 * Return: error value if an error occurred setting up the queues or calling
2060 * Identify Device. 0 if these succeeded, even if adding some of the
2061 * namespaces failed. At the moment, these failures are silent. TBD which
2062 * failures should be reported.
2063 */
2064 static int nvme_dev_add(struct nvme_dev *dev)
2065 {
2066 if (!dev->ctrl.tagset) {
2067 dev->tagset.ops = &nvme_mq_ops;
2068 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2069 dev->tagset.timeout = NVME_IO_TIMEOUT;
2070 dev->tagset.numa_node = dev_to_node(dev->dev);
2071 dev->tagset.queue_depth =
2072 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
2073 dev->tagset.cmd_size = nvme_pci_cmd_size(dev, false);
2074 if ((dev->ctrl.sgls & ((1 << 0) | (1 << 1))) && sgl_threshold) {
2075 dev->tagset.cmd_size = max(dev->tagset.cmd_size,
2076 nvme_pci_cmd_size(dev, true));
2077 }
2078 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
2079 dev->tagset.driver_data = dev;
2080
2081 if (blk_mq_alloc_tag_set(&dev->tagset))
2082 return 0;
2083 dev->ctrl.tagset = &dev->tagset;
2084
2085 nvme_dbbuf_set(dev);
2086 } else {
2087 blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1);
2088
2089 /* Free previously allocated queues that are no longer usable */
2090 nvme_free_queues(dev, dev->online_queues);
2091 }
2092
2093 return 0;
2094 }
2095
2096 static int nvme_pci_enable(struct nvme_dev *dev)
2097 {
2098 int result = -ENOMEM;
2099 struct pci_dev *pdev = to_pci_dev(dev->dev);
2100
2101 if (pci_enable_device_mem(pdev))
2102 return result;
2103
2104 pci_set_master(pdev);
2105
2106 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
2107 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
2108 goto disable;
2109
2110 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
2111 result = -ENODEV;
2112 goto disable;
2113 }
2114
2115 /*
2116 * Some devices and/or platforms don't advertise or work with INTx
2117 * interrupts. Pre-enable a single MSIX or MSI vec for setup. We'll
2118 * adjust this later.
2119 */
2120 result = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
2121 if (result < 0)
2122 return result;
2123
2124 dev->ctrl.cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
2125
2126 dev->q_depth = min_t(int, NVME_CAP_MQES(dev->ctrl.cap) + 1,
2127 io_queue_depth);
2128 dev->db_stride = 1 << NVME_CAP_STRIDE(dev->ctrl.cap);
2129 dev->dbs = dev->bar + 4096;
2130
2131 /*
2132 * Temporary fix for the Apple controller found in the MacBook8,1 and
2133 * some MacBook7,1 to avoid controller resets and data loss.
2134 */
2135 if (pdev->vendor == PCI_VENDOR_ID_APPLE && pdev->device == 0x2001) {
2136 dev->q_depth = 2;
2137 dev_warn(dev->ctrl.device, "detected Apple NVMe controller, "
2138 "set queue depth=%u to work around controller resets\n",
2139 dev->q_depth);
2140 } else if (pdev->vendor == PCI_VENDOR_ID_SAMSUNG &&
2141 (pdev->device == 0xa821 || pdev->device == 0xa822) &&
2142 NVME_CAP_MQES(dev->ctrl.cap) == 0) {
2143 dev->q_depth = 64;
2144 dev_err(dev->ctrl.device, "detected PM1725 NVMe controller, "
2145 "set queue depth=%u\n", dev->q_depth);
2146 }
2147
2148 /*
2149 * CMBs can currently only exist on >=1.2 PCIe devices. We only
2150 * populate sysfs if a CMB is implemented. Since nvme_dev_attrs_group
2151 * has no name we can pass NULL as final argument to
2152 * sysfs_add_file_to_group.
2153 */
2154
2155 if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2, 0)) {
2156 dev->cmb = nvme_map_cmb(dev);
2157 if (dev->cmb) {
2158 if (sysfs_add_file_to_group(&dev->ctrl.device->kobj,
2159 &dev_attr_cmb.attr, NULL))
2160 dev_warn(dev->ctrl.device,
2161 "failed to add sysfs attribute for CMB\n");
2162 }
2163 }
2164
2165 pci_enable_pcie_error_reporting(pdev);
2166 pci_save_state(pdev);
2167 return 0;
2168
2169 disable:
2170 pci_disable_device(pdev);
2171 return result;
2172 }
2173
2174 static void nvme_dev_unmap(struct nvme_dev *dev)
2175 {
2176 if (dev->bar)
2177 iounmap(dev->bar);
2178 pci_release_mem_regions(to_pci_dev(dev->dev));
2179 }
2180
2181 static void nvme_pci_disable(struct nvme_dev *dev)
2182 {
2183 struct pci_dev *pdev = to_pci_dev(dev->dev);
2184
2185 nvme_release_cmb(dev);
2186 pci_free_irq_vectors(pdev);
2187
2188 if (pci_is_enabled(pdev)) {
2189 pci_disable_pcie_error_reporting(pdev);
2190 pci_disable_device(pdev);
2191 }
2192 }
2193
2194 static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
2195 {
2196 int i, queues;
2197 bool dead = true;
2198 struct pci_dev *pdev = to_pci_dev(dev->dev);
2199
2200 mutex_lock(&dev->shutdown_lock);
2201 if (pci_is_enabled(pdev)) {
2202 u32 csts = readl(dev->bar + NVME_REG_CSTS);
2203
2204 if (dev->ctrl.state == NVME_CTRL_LIVE ||
2205 dev->ctrl.state == NVME_CTRL_RESETTING)
2206 nvme_start_freeze(&dev->ctrl);
2207 dead = !!((csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) ||
2208 pdev->error_state != pci_channel_io_normal);
2209 }
2210
2211 /*
2212 * Give the controller a chance to complete all entered requests if
2213 * doing a safe shutdown.
2214 */
2215 if (!dead) {
2216 if (shutdown)
2217 nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
2218
2219 /*
2220 * If the controller is still alive tell it to stop using the
2221 * host memory buffer. In theory the shutdown / reset should
2222 * make sure that it doesn't access the host memoery anymore,
2223 * but I'd rather be safe than sorry..
2224 */
2225 if (dev->host_mem_descs)
2226 nvme_set_host_mem(dev, 0);
2227
2228 }
2229 nvme_stop_queues(&dev->ctrl);
2230
2231 queues = dev->online_queues - 1;
2232 for (i = dev->ctrl.queue_count - 1; i > 0; i--)
2233 nvme_suspend_queue(&dev->queues[i]);
2234
2235 if (dead) {
2236 /* A device might become IO incapable very soon during
2237 * probe, before the admin queue is configured. Thus,
2238 * queue_count can be 0 here.
2239 */
2240 if (dev->ctrl.queue_count)
2241 nvme_suspend_queue(&dev->queues[0]);
2242 } else {
2243 nvme_disable_io_queues(dev, queues);
2244 nvme_disable_admin_queue(dev, shutdown);
2245 }
2246 nvme_pci_disable(dev);
2247
2248 blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl);
2249 blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl);
2250
2251 /*
2252 * The driver will not be starting up queues again if shutting down so
2253 * must flush all entered requests to their failed completion to avoid
2254 * deadlocking blk-mq hot-cpu notifier.
2255 */
2256 if (shutdown) {
2257 nvme_start_queues(&dev->ctrl);
2258 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q))
2259 blk_mq_unquiesce_queue(dev->ctrl.admin_q);
2260 }
2261 mutex_unlock(&dev->shutdown_lock);
2262 }
2263
2264 static int nvme_setup_prp_pools(struct nvme_dev *dev)
2265 {
2266 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
2267 PAGE_SIZE, PAGE_SIZE, 0);
2268 if (!dev->prp_page_pool)
2269 return -ENOMEM;
2270
2271 /* Optimisation for I/Os between 4k and 128k */
2272 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
2273 256, 256, 0);
2274 if (!dev->prp_small_pool) {
2275 dma_pool_destroy(dev->prp_page_pool);
2276 return -ENOMEM;
2277 }
2278 return 0;
2279 }
2280
2281 static void nvme_release_prp_pools(struct nvme_dev *dev)
2282 {
2283 dma_pool_destroy(dev->prp_page_pool);
2284 dma_pool_destroy(dev->prp_small_pool);
2285 }
2286
2287 static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
2288 {
2289 struct nvme_dev *dev = to_nvme_dev(ctrl);
2290
2291 nvme_dbbuf_dma_free(dev);
2292 put_device(dev->dev);
2293 if (dev->tagset.tags)
2294 blk_mq_free_tag_set(&dev->tagset);
2295 if (dev->ctrl.admin_q)
2296 blk_put_queue(dev->ctrl.admin_q);
2297 kfree(dev->queues);
2298 free_opal_dev(dev->ctrl.opal_dev);
2299 kfree(dev);
2300 }
2301
2302 static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
2303 {
2304 dev_warn(dev->ctrl.device, "Removing after probe failure status: %d\n", status);
2305
2306 nvme_get_ctrl(&dev->ctrl);
2307 nvme_dev_disable(dev, false);
2308 nvme_kill_queues(&dev->ctrl);
2309 if (!queue_work(nvme_wq, &dev->remove_work))
2310 nvme_put_ctrl(&dev->ctrl);
2311 }
2312
2313 static void nvme_reset_work(struct work_struct *work)
2314 {
2315 struct nvme_dev *dev =
2316 container_of(work, struct nvme_dev, ctrl.reset_work);
2317 bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
2318 int result;
2319
2320 if (WARN_ON(dev->ctrl.state != NVME_CTRL_RESETTING)) {
2321 result = -ENODEV;
2322 goto out;
2323 }
2324
2325 /*
2326 * If we're called to reset a live controller first shut it down before
2327 * moving on.
2328 */
2329 if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
2330 nvme_dev_disable(dev, false);
2331 nvme_sync_queues(&dev->ctrl);
2332
2333 result = nvme_pci_enable(dev);
2334 if (result)
2335 goto out_unlock;
2336
2337 result = nvme_pci_configure_admin_queue(dev);
2338 if (result)
2339 goto out_unlock;
2340
2341 result = nvme_alloc_admin_tags(dev);
2342 if (result)
2343 goto out_unlock;
2344
2345 result = nvme_init_identify(&dev->ctrl);
2346 if (result)
2347 goto out;
2348
2349 if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) {
2350 if (!dev->ctrl.opal_dev)
2351 dev->ctrl.opal_dev =
2352 init_opal_dev(&dev->ctrl, &nvme_sec_submit);
2353 else if (was_suspend)
2354 opal_unlock_from_suspend(dev->ctrl.opal_dev);
2355 } else {
2356 free_opal_dev(dev->ctrl.opal_dev);
2357 dev->ctrl.opal_dev = NULL;
2358 }
2359
2360 if (dev->ctrl.oacs & NVME_CTRL_OACS_DBBUF_SUPP) {
2361 result = nvme_dbbuf_dma_alloc(dev);
2362 if (result)
2363 dev_warn(dev->dev,
2364 "unable to allocate dma for dbbuf\n");
2365 }
2366
2367 if (dev->ctrl.hmpre) {
2368 result = nvme_setup_host_mem(dev);
2369 if (result < 0)
2370 goto out;
2371 }
2372
2373 result = nvme_setup_io_queues(dev);
2374 if (result)
2375 goto out;
2376
2377 /*
2378 * Keep the controller around but remove all namespaces if we don't have
2379 * any working I/O queue.
2380 */
2381 if (dev->online_queues < 2) {
2382 dev_warn(dev->ctrl.device, "IO queues not created\n");
2383 nvme_kill_queues(&dev->ctrl);
2384 nvme_remove_namespaces(&dev->ctrl);
2385 } else {
2386 nvme_start_queues(&dev->ctrl);
2387 nvme_wait_freeze(&dev->ctrl);
2388 nvme_dev_add(dev);
2389 nvme_unfreeze(&dev->ctrl);
2390 }
2391
2392 if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) {
2393 dev_warn(dev->ctrl.device, "failed to mark controller live\n");
2394 result = -EBUSY;
2395 goto out;
2396 }
2397
2398 nvme_start_ctrl(&dev->ctrl);
2399 return;
2400
2401 out_unlock:
2402 mutex_unlock(&dev->shutdown_lock);
2403 out:
2404 nvme_remove_dead_ctrl(dev, result);
2405 }
2406
2407 static void nvme_remove_dead_ctrl_work(struct work_struct *work)
2408 {
2409 struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
2410 struct pci_dev *pdev = to_pci_dev(dev->dev);
2411
2412 if (pci_get_drvdata(pdev))
2413 device_release_driver(&pdev->dev);
2414 nvme_put_ctrl(&dev->ctrl);
2415 }
2416
2417 static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
2418 {
2419 *val = readl(to_nvme_dev(ctrl)->bar + off);
2420 return 0;
2421 }
2422
2423 static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
2424 {
2425 writel(val, to_nvme_dev(ctrl)->bar + off);
2426 return 0;
2427 }
2428
2429 static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
2430 {
2431 *val = readq(to_nvme_dev(ctrl)->bar + off);
2432 return 0;
2433 }
2434
2435 static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
2436 .name = "pcie",
2437 .module = THIS_MODULE,
2438 .flags = NVME_F_METADATA_SUPPORTED,
2439 .reg_read32 = nvme_pci_reg_read32,
2440 .reg_write32 = nvme_pci_reg_write32,
2441 .reg_read64 = nvme_pci_reg_read64,
2442 .free_ctrl = nvme_pci_free_ctrl,
2443 .submit_async_event = nvme_pci_submit_async_event,
2444 };
2445
2446 static int nvme_dev_map(struct nvme_dev *dev)
2447 {
2448 struct pci_dev *pdev = to_pci_dev(dev->dev);
2449
2450 if (pci_request_mem_regions(pdev, "nvme"))
2451 return -ENODEV;
2452
2453 if (nvme_remap_bar(dev, NVME_REG_DBS + 4096))
2454 goto release;
2455
2456 return 0;
2457 release:
2458 pci_release_mem_regions(pdev);
2459 return -ENODEV;
2460 }
2461
2462 static unsigned long check_vendor_combination_bug(struct pci_dev *pdev)
2463 {
2464 if (pdev->vendor == 0x144d && pdev->device == 0xa802) {
2465 /*
2466 * Several Samsung devices seem to drop off the PCIe bus
2467 * randomly when APST is on and uses the deepest sleep state.
2468 * This has been observed on a Samsung "SM951 NVMe SAMSUNG
2469 * 256GB", a "PM951 NVMe SAMSUNG 512GB", and a "Samsung SSD
2470 * 950 PRO 256GB", but it seems to be restricted to two Dell
2471 * laptops.
2472 */
2473 if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.") &&
2474 (dmi_match(DMI_PRODUCT_NAME, "XPS 15 9550") ||
2475 dmi_match(DMI_PRODUCT_NAME, "Precision 5510")))
2476 return NVME_QUIRK_NO_DEEPEST_PS;
2477 } else if (pdev->vendor == 0x144d && pdev->device == 0xa804) {
2478 /*
2479 * Samsung SSD 960 EVO drops off the PCIe bus after system
2480 * suspend on a Ryzen board, ASUS PRIME B350M-A, as well as
2481 * within few minutes after bootup on a Coffee Lake board -
2482 * ASUS PRIME Z370-A
2483 */
2484 if (dmi_match(DMI_BOARD_VENDOR, "ASUSTeK COMPUTER INC.") &&
2485 (dmi_match(DMI_BOARD_NAME, "PRIME B350M-A") ||
2486 dmi_match(DMI_BOARD_NAME, "PRIME Z370-A")))
2487 return NVME_QUIRK_NO_APST;
2488 }
2489
2490 return 0;
2491 }
2492
2493 static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2494 {
2495 int node, result = -ENOMEM;
2496 struct nvme_dev *dev;
2497 unsigned long quirks = id->driver_data;
2498
2499 node = dev_to_node(&pdev->dev);
2500 if (node == NUMA_NO_NODE)
2501 set_dev_node(&pdev->dev, first_memory_node);
2502
2503 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
2504 if (!dev)
2505 return -ENOMEM;
2506
2507 dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(struct nvme_queue),
2508 GFP_KERNEL, node);
2509 if (!dev->queues)
2510 goto free;
2511
2512 dev->dev = get_device(&pdev->dev);
2513 pci_set_drvdata(pdev, dev);
2514
2515 result = nvme_dev_map(dev);
2516 if (result)
2517 goto put_pci;
2518
2519 INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work);
2520 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
2521 mutex_init(&dev->shutdown_lock);
2522 init_completion(&dev->ioq_wait);
2523
2524 result = nvme_setup_prp_pools(dev);
2525 if (result)
2526 goto unmap;
2527
2528 quirks |= check_vendor_combination_bug(pdev);
2529
2530 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
2531 quirks);
2532 if (result)
2533 goto release_pools;
2534
2535 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING);
2536 dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev));
2537
2538 queue_work(nvme_wq, &dev->ctrl.reset_work);
2539 return 0;
2540
2541 release_pools:
2542 nvme_release_prp_pools(dev);
2543 unmap:
2544 nvme_dev_unmap(dev);
2545 put_pci:
2546 put_device(dev->dev);
2547 free:
2548 kfree(dev->queues);
2549 kfree(dev);
2550 return result;
2551 }
2552
2553 static void nvme_reset_prepare(struct pci_dev *pdev)
2554 {
2555 struct nvme_dev *dev = pci_get_drvdata(pdev);
2556 nvme_dev_disable(dev, false);
2557 }
2558
2559 static void nvme_reset_done(struct pci_dev *pdev)
2560 {
2561 struct nvme_dev *dev = pci_get_drvdata(pdev);
2562 nvme_reset_ctrl(&dev->ctrl);
2563 }
2564
2565 static void nvme_shutdown(struct pci_dev *pdev)
2566 {
2567 struct nvme_dev *dev = pci_get_drvdata(pdev);
2568 nvme_dev_disable(dev, true);
2569 }
2570
2571 /*
2572 * The driver's remove may be called on a device in a partially initialized
2573 * state. This function must not have any dependencies on the device state in
2574 * order to proceed.
2575 */
2576 static void nvme_remove(struct pci_dev *pdev)
2577 {
2578 struct nvme_dev *dev = pci_get_drvdata(pdev);
2579
2580 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
2581 pci_set_drvdata(pdev, NULL);
2582
2583 if (!pci_device_is_present(pdev)) {
2584 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD);
2585 nvme_dev_disable(dev, false);
2586 nvme_dev_remove_admin(dev);
2587 }
2588
2589 flush_work(&dev->ctrl.reset_work);
2590 nvme_stop_ctrl(&dev->ctrl);
2591 nvme_remove_namespaces(&dev->ctrl);
2592 nvme_dev_disable(dev, true);
2593 nvme_free_host_mem(dev);
2594 nvme_dev_remove_admin(dev);
2595 nvme_free_queues(dev, 0);
2596 nvme_uninit_ctrl(&dev->ctrl);
2597 nvme_release_prp_pools(dev);
2598 nvme_dev_unmap(dev);
2599 nvme_put_ctrl(&dev->ctrl);
2600 }
2601
2602 static int nvme_pci_sriov_configure(struct pci_dev *pdev, int numvfs)
2603 {
2604 int ret = 0;
2605
2606 if (numvfs == 0) {
2607 if (pci_vfs_assigned(pdev)) {
2608 dev_warn(&pdev->dev,
2609 "Cannot disable SR-IOV VFs while assigned\n");
2610 return -EPERM;
2611 }
2612 pci_disable_sriov(pdev);
2613 return 0;
2614 }
2615
2616 ret = pci_enable_sriov(pdev, numvfs);
2617 return ret ? ret : numvfs;
2618 }
2619
2620 #ifdef CONFIG_PM_SLEEP
2621 static int nvme_get_power_state(struct nvme_ctrl *ctrl, u32 *ps)
2622 {
2623 return nvme_get_features(ctrl, NVME_FEAT_POWER_MGMT, 0, NULL, 0, ps);
2624 }
2625
2626 static int nvme_set_power_state(struct nvme_ctrl *ctrl, u32 ps)
2627 {
2628 return nvme_set_features(ctrl, NVME_FEAT_POWER_MGMT, ps, NULL, 0, NULL);
2629 }
2630
2631 static int nvme_resume(struct device *dev)
2632 {
2633 struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev));
2634 struct nvme_ctrl *ctrl = &ndev->ctrl;
2635
2636 if (pm_resume_via_firmware() || !ctrl->npss ||
2637 nvme_set_power_state(ctrl, ndev->last_ps) != 0)
2638 nvme_reset_ctrl(ctrl);
2639 return 0;
2640 }
2641
2642 static int nvme_suspend(struct device *dev)
2643 {
2644 struct pci_dev *pdev = to_pci_dev(dev);
2645 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2646 struct nvme_ctrl *ctrl = &ndev->ctrl;
2647 int ret = -EBUSY;
2648
2649 /*
2650 * The platform does not remove power for a kernel managed suspend so
2651 * use host managed nvme power settings for lowest idle power if
2652 * possible. This should have quicker resume latency than a full device
2653 * shutdown. But if the firmware is involved after the suspend or the
2654 * device does not support any non-default power states, shut down the
2655 * device fully.
2656 */
2657 if (pm_suspend_via_firmware() || !ctrl->npss) {
2658 nvme_dev_disable(ndev, true);
2659 return 0;
2660 }
2661
2662 nvme_start_freeze(ctrl);
2663 nvme_wait_freeze(ctrl);
2664 nvme_sync_queues(ctrl);
2665
2666 if (ctrl->state != NVME_CTRL_LIVE)
2667 goto unfreeze;
2668
2669 ndev->last_ps = 0;
2670 ret = nvme_get_power_state(ctrl, &ndev->last_ps);
2671 if (ret < 0)
2672 goto unfreeze;
2673
2674 ret = nvme_set_power_state(ctrl, ctrl->npss);
2675 if (ret < 0)
2676 goto unfreeze;
2677
2678 if (ret) {
2679 /*
2680 * Clearing npss forces a controller reset on resume. The
2681 * correct value will be resdicovered then.
2682 */
2683 nvme_dev_disable(ndev, true);
2684 ctrl->npss = 0;
2685 ret = 0;
2686 goto unfreeze;
2687 }
2688 /*
2689 * A saved state prevents pci pm from generically controlling the
2690 * device's power. If we're using protocol specific settings, we don't
2691 * want pci interfering.
2692 */
2693 pci_save_state(pdev);
2694 unfreeze:
2695 nvme_unfreeze(ctrl);
2696 return ret;
2697 }
2698
2699 static int nvme_simple_suspend(struct device *dev)
2700 {
2701 struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev));
2702
2703 nvme_dev_disable(ndev, true);
2704 return 0;
2705 }
2706
2707 static int nvme_simple_resume(struct device *dev)
2708 {
2709 struct pci_dev *pdev = to_pci_dev(dev);
2710 struct nvme_dev *ndev = pci_get_drvdata(pdev);
2711
2712 nvme_reset_ctrl(&ndev->ctrl);
2713 return 0;
2714 }
2715
2716 const struct dev_pm_ops nvme_dev_pm_ops = {
2717 .suspend = nvme_suspend,
2718 .resume = nvme_resume,
2719 .freeze = nvme_simple_suspend,
2720 .thaw = nvme_simple_resume,
2721 .poweroff = nvme_simple_suspend,
2722 .restore = nvme_simple_resume,
2723 };
2724
2725 #else
2726 #define nvme_dev_pm_ops NULL
2727 #endif
2728
2729 static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
2730 pci_channel_state_t state)
2731 {
2732 struct nvme_dev *dev = pci_get_drvdata(pdev);
2733
2734 /*
2735 * A frozen channel requires a reset. When detected, this method will
2736 * shutdown the controller to quiesce. The controller will be restarted
2737 * after the slot reset through driver's slot_reset callback.
2738 */
2739 switch (state) {
2740 case pci_channel_io_normal:
2741 return PCI_ERS_RESULT_CAN_RECOVER;
2742 case pci_channel_io_frozen:
2743 dev_warn(dev->ctrl.device,
2744 "frozen state error detected, reset controller\n");
2745 nvme_dev_disable(dev, false);
2746 return PCI_ERS_RESULT_NEED_RESET;
2747 case pci_channel_io_perm_failure:
2748 dev_warn(dev->ctrl.device,
2749 "failure state error detected, request disconnect\n");
2750 return PCI_ERS_RESULT_DISCONNECT;
2751 }
2752 return PCI_ERS_RESULT_NEED_RESET;
2753 }
2754
2755 static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev)
2756 {
2757 struct nvme_dev *dev = pci_get_drvdata(pdev);
2758
2759 dev_info(dev->ctrl.device, "restart after slot reset\n");
2760 pci_restore_state(pdev);
2761 nvme_reset_ctrl(&dev->ctrl);
2762 return PCI_ERS_RESULT_RECOVERED;
2763 }
2764
2765 static void nvme_error_resume(struct pci_dev *pdev)
2766 {
2767 struct nvme_dev *dev = pci_get_drvdata(pdev);
2768
2769 flush_work(&dev->ctrl.reset_work);
2770 pci_cleanup_aer_uncorrect_error_status(pdev);
2771 }
2772
2773 static const struct pci_error_handlers nvme_err_handler = {
2774 .error_detected = nvme_error_detected,
2775 .slot_reset = nvme_slot_reset,
2776 .resume = nvme_error_resume,
2777 .reset_prepare = nvme_reset_prepare,
2778 .reset_done = nvme_reset_done,
2779 };
2780
2781 static const struct pci_device_id nvme_id_table[] = {
2782 { PCI_VDEVICE(INTEL, 0x0953),
2783 .driver_data = NVME_QUIRK_STRIPE_SIZE |
2784 NVME_QUIRK_DEALLOCATE_ZEROES, },
2785 { PCI_VDEVICE(INTEL, 0x0a53),
2786 .driver_data = NVME_QUIRK_STRIPE_SIZE |
2787 NVME_QUIRK_DEALLOCATE_ZEROES, },
2788 { PCI_VDEVICE(INTEL, 0x0a54),
2789 .driver_data = NVME_QUIRK_STRIPE_SIZE |
2790 NVME_QUIRK_DEALLOCATE_ZEROES, },
2791 { PCI_VDEVICE(INTEL, 0x0a55),
2792 .driver_data = NVME_QUIRK_STRIPE_SIZE |
2793 NVME_QUIRK_DEALLOCATE_ZEROES, },
2794 { PCI_VDEVICE(INTEL, 0xf1a5), /* Intel 600P/P3100 */
2795 .driver_data = NVME_QUIRK_NO_DEEPEST_PS |
2796 NVME_QUIRK_MEDIUM_PRIO_SQ },
2797 { PCI_VDEVICE(INTEL, 0xf1a6), /* Intel 760p/Pro 7600p */
2798 .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN, },
2799 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2800 .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
2801 { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */
2802 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
2803 { PCI_DEVICE(0x1c58, 0x0023), /* WDC SN200 adapter */
2804 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
2805 { PCI_DEVICE(0x1c5f, 0x0540), /* Memblaze Pblaze4 adapter */
2806 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
2807 { PCI_DEVICE(0x144d, 0xa821), /* Samsung PM1725 */
2808 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
2809 { PCI_DEVICE(0x144d, 0xa822), /* Samsung PM1725a */
2810 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
2811 { PCI_DEVICE(0x1d1d, 0x1f1f), /* LighNVM qemu device */
2812 .driver_data = NVME_QUIRK_LIGHTNVM, },
2813 { PCI_DEVICE(0x1d1d, 0x2807), /* CNEX WL */
2814 .driver_data = NVME_QUIRK_LIGHTNVM, },
2815 { PCI_DEVICE(0x1d1d, 0x2601), /* CNEX Granby */
2816 .driver_data = NVME_QUIRK_LIGHTNVM, },
2817 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
2818 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
2819 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) },
2820 { 0, }
2821 };
2822 MODULE_DEVICE_TABLE(pci, nvme_id_table);
2823
2824 static struct pci_driver nvme_driver = {
2825 .name = "nvme",
2826 .id_table = nvme_id_table,
2827 .probe = nvme_probe,
2828 .remove = nvme_remove,
2829 .shutdown = nvme_shutdown,
2830 .driver = {
2831 .pm = &nvme_dev_pm_ops,
2832 },
2833 .sriov_configure = nvme_pci_sriov_configure,
2834 .err_handler = &nvme_err_handler,
2835 };
2836
2837 static int __init nvme_init(void)
2838 {
2839 return pci_register_driver(&nvme_driver);
2840 }
2841
2842 static void __exit nvme_exit(void)
2843 {
2844 pci_unregister_driver(&nvme_driver);
2845 flush_workqueue(nvme_wq);
2846 _nvme_check_size();
2847 }
2848
2849 MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
2850 MODULE_LICENSE("GPL");
2851 MODULE_VERSION("1.0");
2852 module_init(nvme_init);
2853 module_exit(nvme_exit);