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