forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/nvme/host/pci.c
....@@ -1,17 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * NVM Express device driver
34 * 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.
135 */
146
7
+#include <linux/acpi.h>
158 #include <linux/aer.h>
169 #include <linux/async.h>
1710 #include <linux/blkdev.h>
....@@ -26,15 +19,19 @@
2619 #include <linux/mutex.h>
2720 #include <linux/once.h>
2821 #include <linux/pci.h>
22
+#include <linux/suspend.h>
2923 #include <linux/t10-pi.h>
3024 #include <linux/types.h>
3125 #include <linux/io-64-nonatomic-lo-hi.h>
26
+#include <linux/io-64-nonatomic-hi-lo.h>
3227 #include <linux/sed-opal.h>
28
+#include <linux/pci-p2pdma.h>
3329
30
+#include "trace.h"
3431 #include "nvme.h"
3532
36
-#define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
37
-#define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
33
+#define SQ_SIZE(q) ((q)->q_depth << (q)->sqes)
34
+#define CQ_SIZE(q) ((q)->q_depth * sizeof(struct nvme_completion))
3835
3936 #define SGES_PER_PAGE (PAGE_SIZE / sizeof(struct nvme_sgl_desc))
4037
....@@ -66,17 +63,48 @@
6663 static int io_queue_depth_set(const char *val, const struct kernel_param *kp);
6764 static const struct kernel_param_ops io_queue_depth_ops = {
6865 .set = io_queue_depth_set,
69
- .get = param_get_int,
66
+ .get = param_get_uint,
7067 };
7168
72
-static int io_queue_depth = 1024;
69
+static unsigned int io_queue_depth = 1024;
7370 module_param_cb(io_queue_depth, &io_queue_depth_ops, &io_queue_depth, 0644);
7471 MODULE_PARM_DESC(io_queue_depth, "set io queue depth, should >= 2");
72
+
73
+static int io_queue_count_set(const char *val, const struct kernel_param *kp)
74
+{
75
+ unsigned int n;
76
+ int ret;
77
+
78
+ ret = kstrtouint(val, 10, &n);
79
+ if (ret != 0 || n > num_possible_cpus())
80
+ return -EINVAL;
81
+ return param_set_uint(val, kp);
82
+}
83
+
84
+static const struct kernel_param_ops io_queue_count_ops = {
85
+ .set = io_queue_count_set,
86
+ .get = param_get_uint,
87
+};
88
+
89
+static unsigned int write_queues;
90
+module_param_cb(write_queues, &io_queue_count_ops, &write_queues, 0644);
91
+MODULE_PARM_DESC(write_queues,
92
+ "Number of queues to use for writes. If not set, reads and writes "
93
+ "will share a queue set.");
94
+
95
+static unsigned int poll_queues;
96
+module_param_cb(poll_queues, &io_queue_count_ops, &poll_queues, 0644);
97
+MODULE_PARM_DESC(poll_queues, "Number of queues to use for polled IO.");
98
+
99
+static bool noacpi;
100
+module_param(noacpi, bool, 0444);
101
+MODULE_PARM_DESC(noacpi, "disable acpi bios quirks");
75102
76103 struct nvme_dev;
77104 struct nvme_queue;
78105
79106 static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown);
107
+static bool __nvme_disable_io_queues(struct nvme_dev *dev, u8 opcode);
80108
81109 /*
82110 * Represents an NVM Express device. Each nvme_dev is a PCI function.
....@@ -91,21 +119,22 @@
91119 struct dma_pool *prp_small_pool;
92120 unsigned online_queues;
93121 unsigned max_qid;
122
+ unsigned io_queues[HCTX_MAX_TYPES];
94123 unsigned int num_vecs;
95
- int q_depth;
124
+ u32 q_depth;
125
+ int io_sqes;
96126 u32 db_stride;
97127 void __iomem *bar;
98128 unsigned long bar_mapped_size;
99129 struct work_struct remove_work;
100130 struct mutex shutdown_lock;
101131 bool subsystem;
102
- void __iomem *cmb;
103
- pci_bus_addr_t cmb_bus_addr;
104132 u64 cmb_size;
133
+ bool cmb_use_sqes;
105134 u32 cmbsz;
106135 u32 cmbloc;
107136 struct nvme_ctrl ctrl;
108
- struct completion ioq_wait;
137
+ u32 last_ps;
109138
110139 mempool_t *iod_mempool;
111140
....@@ -121,17 +150,21 @@
121150 dma_addr_t host_mem_descs_dma;
122151 struct nvme_host_mem_buf_desc *host_mem_descs;
123152 void **host_mem_desc_bufs;
153
+ unsigned int nr_allocated_queues;
154
+ unsigned int nr_write_queues;
155
+ unsigned int nr_poll_queues;
124156 };
125157
126158 static int io_queue_depth_set(const char *val, const struct kernel_param *kp)
127159 {
128
- int n = 0, ret;
160
+ int ret;
161
+ u32 n;
129162
130
- ret = kstrtoint(val, 10, &n);
163
+ ret = kstrtou32(val, 10, &n);
131164 if (ret != 0 || n < 2)
132165 return -EINVAL;
133166
134
- return param_set_int(val, kp);
167
+ return param_set_uint(val, kp);
135168 }
136169
137170 static inline unsigned int sq_idx(unsigned int qid, u32 stride)
....@@ -154,78 +187,63 @@
154187 * commands and one for I/O commands).
155188 */
156189 struct nvme_queue {
157
- struct device *q_dmadev;
158190 struct nvme_dev *dev;
159191 spinlock_t sq_lock;
160
- struct nvme_command *sq_cmds;
161
- struct nvme_command __iomem *sq_cmds_io;
162
- spinlock_t cq_lock ____cacheline_aligned_in_smp;
163
- volatile struct nvme_completion *cqes;
164
- struct blk_mq_tags **tags;
192
+ void *sq_cmds;
193
+ /* only used for poll queues: */
194
+ spinlock_t cq_poll_lock ____cacheline_aligned_in_smp;
195
+ struct nvme_completion *cqes;
165196 dma_addr_t sq_dma_addr;
166197 dma_addr_t cq_dma_addr;
167198 u32 __iomem *q_db;
168
- u16 q_depth;
169
- s16 cq_vector;
199
+ u32 q_depth;
200
+ u16 cq_vector;
170201 u16 sq_tail;
202
+ u16 last_sq_tail;
171203 u16 cq_head;
172
- u16 last_cq_head;
173204 u16 qid;
174205 u8 cq_phase;
206
+ u8 sqes;
207
+ unsigned long flags;
208
+#define NVMEQ_ENABLED 0
209
+#define NVMEQ_SQ_CMB 1
210
+#define NVMEQ_DELETE_ERROR 2
211
+#define NVMEQ_POLLED 3
175212 u32 *dbbuf_sq_db;
176213 u32 *dbbuf_cq_db;
177214 u32 *dbbuf_sq_ei;
178215 u32 *dbbuf_cq_ei;
216
+ struct completion delete_done;
179217 };
180218
181219 /*
182
- * The nvme_iod describes the data in an I/O, including the list of PRP
183
- * entries. You can't see it in this data structure because C doesn't let
184
- * me express that. Use nvme_init_iod to ensure there's enough space
185
- * allocated to store the PRP list.
220
+ * The nvme_iod describes the data in an I/O.
221
+ *
222
+ * The sg pointer contains the list of PRP/SGL chunk allocations in addition
223
+ * to the actual struct scatterlist.
186224 */
187225 struct nvme_iod {
188226 struct nvme_request req;
227
+ struct nvme_command cmd;
189228 struct nvme_queue *nvmeq;
190229 bool use_sgl;
191230 int aborted;
192231 int npages; /* In the PRP list. 0 means small pool in use */
193232 int nents; /* Used in scatterlist */
194
- int length; /* Of data, in bytes */
195233 dma_addr_t first_dma;
196
- struct scatterlist meta_sg; /* metadata requires single contiguous buffer */
234
+ unsigned int dma_len; /* length of single DMA segment mapping */
235
+ dma_addr_t meta_dma;
197236 struct scatterlist *sg;
198
- struct scatterlist inline_sg[0];
199237 };
200238
201
-/*
202
- * Check we didin't inadvertently grow the command struct
203
- */
204
-static inline void _nvme_check_size(void)
239
+static inline unsigned int nvme_dbbuf_size(struct nvme_dev *dev)
205240 {
206
- BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
207
- BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
208
- BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
209
- BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
210
- BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
211
- BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
212
- BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
213
- BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
214
- BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != NVME_IDENTIFY_DATA_SIZE);
215
- BUILD_BUG_ON(sizeof(struct nvme_id_ns) != NVME_IDENTIFY_DATA_SIZE);
216
- BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
217
- BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
218
- BUILD_BUG_ON(sizeof(struct nvme_dbbuf) != 64);
219
-}
220
-
221
-static inline unsigned int nvme_dbbuf_size(u32 stride)
222
-{
223
- return ((num_possible_cpus() + 1) * 8 * stride);
241
+ return dev->nr_allocated_queues * 8 * dev->db_stride;
224242 }
225243
226244 static int nvme_dbbuf_dma_alloc(struct nvme_dev *dev)
227245 {
228
- unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
246
+ unsigned int mem_size = nvme_dbbuf_size(dev);
229247
230248 if (dev->dbbuf_dbs)
231249 return 0;
....@@ -250,7 +268,7 @@
250268
251269 static void nvme_dbbuf_dma_free(struct nvme_dev *dev)
252270 {
253
- unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
271
+ unsigned int mem_size = nvme_dbbuf_size(dev);
254272
255273 if (dev->dbbuf_dbs) {
256274 dma_free_coherent(dev->dev, mem_size,
....@@ -347,20 +365,14 @@
347365 }
348366
349367 /*
350
- * Max size of iod being embedded in the request payload
351
- */
352
-#define NVME_INT_PAGES 2
353
-#define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
354
-
355
-/*
356368 * Will slightly overestimate the number of pages needed. This is OK
357369 * as it only leads to a small amount of wasted memory for the lifetime of
358370 * the I/O.
359371 */
360
-static int nvme_npages(unsigned size, struct nvme_dev *dev)
372
+static int nvme_pci_npages_prp(void)
361373 {
362
- unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
363
- dev->ctrl.page_size);
374
+ unsigned nprps = DIV_ROUND_UP(NVME_MAX_KB_SZ + NVME_CTRL_PAGE_SIZE,
375
+ NVME_CTRL_PAGE_SIZE);
364376 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
365377 }
366378
....@@ -368,31 +380,18 @@
368380 * Calculates the number of pages needed for the SGL segments. For example a 4k
369381 * page can accommodate 256 SGL descriptors.
370382 */
371
-static int nvme_pci_npages_sgl(unsigned int num_seg)
383
+static int nvme_pci_npages_sgl(void)
372384 {
373
- return DIV_ROUND_UP(num_seg * sizeof(struct nvme_sgl_desc), PAGE_SIZE);
385
+ return DIV_ROUND_UP(NVME_MAX_SEGS * sizeof(struct nvme_sgl_desc),
386
+ PAGE_SIZE);
374387 }
375388
376
-static unsigned int nvme_pci_iod_alloc_size(struct nvme_dev *dev,
377
- unsigned int size, unsigned int nseg, bool use_sgl)
389
+static size_t nvme_pci_iod_alloc_size(void)
378390 {
379
- size_t alloc_size;
391
+ size_t npages = max(nvme_pci_npages_prp(), nvme_pci_npages_sgl());
380392
381
- if (use_sgl)
382
- alloc_size = sizeof(__le64 *) * nvme_pci_npages_sgl(nseg);
383
- else
384
- alloc_size = sizeof(__le64 *) * nvme_npages(size, dev);
385
-
386
- return alloc_size + sizeof(struct scatterlist) * nseg;
387
-}
388
-
389
-static unsigned int nvme_pci_cmd_size(struct nvme_dev *dev, bool use_sgl)
390
-{
391
- unsigned int alloc_size = nvme_pci_iod_alloc_size(dev,
392
- NVME_INT_BYTES(dev), NVME_INT_PAGES,
393
- use_sgl);
394
-
395
- return sizeof(struct nvme_iod) + alloc_size;
393
+ return sizeof(__le64 *) * npages +
394
+ sizeof(struct scatterlist) * NVME_MAX_SEGS;
396395 }
397396
398397 static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
....@@ -403,18 +402,9 @@
403402
404403 WARN_ON(hctx_idx != 0);
405404 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
406
- WARN_ON(nvmeq->tags);
407405
408406 hctx->driver_data = nvmeq;
409
- nvmeq->tags = &dev->admin_tagset.tags[0];
410407 return 0;
411
-}
412
-
413
-static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
414
-{
415
- struct nvme_queue *nvmeq = hctx->driver_data;
416
-
417
- nvmeq->tags = NULL;
418408 }
419409
420410 static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
....@@ -422,9 +412,6 @@
422412 {
423413 struct nvme_dev *dev = data;
424414 struct nvme_queue *nvmeq = &dev->queues[hctx_idx + 1];
425
-
426
- if (!nvmeq->tags)
427
- nvmeq->tags = &dev->tagset.tags[hctx_idx];
428415
429416 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
430417 hctx->driver_data = nvmeq;
....@@ -446,33 +433,91 @@
446433 return 0;
447434 }
448435
436
+static int queue_irq_offset(struct nvme_dev *dev)
437
+{
438
+ /* if we have more than 1 vec, admin queue offsets us by 1 */
439
+ if (dev->num_vecs > 1)
440
+ return 1;
441
+
442
+ return 0;
443
+}
444
+
449445 static int nvme_pci_map_queues(struct blk_mq_tag_set *set)
450446 {
451447 struct nvme_dev *dev = set->driver_data;
448
+ int i, qoff, offset;
452449
453
- return blk_mq_pci_map_queues(set, to_pci_dev(dev->dev),
454
- dev->num_vecs > 1 ? 1 /* admin queue */ : 0);
450
+ offset = queue_irq_offset(dev);
451
+ for (i = 0, qoff = 0; i < set->nr_maps; i++) {
452
+ struct blk_mq_queue_map *map = &set->map[i];
453
+
454
+ map->nr_queues = dev->io_queues[i];
455
+ if (!map->nr_queues) {
456
+ BUG_ON(i == HCTX_TYPE_DEFAULT);
457
+ continue;
458
+ }
459
+
460
+ /*
461
+ * The poll queue(s) doesn't have an IRQ (and hence IRQ
462
+ * affinity), so use the regular blk-mq cpu mapping
463
+ */
464
+ map->queue_offset = qoff;
465
+ if (i != HCTX_TYPE_POLL && offset)
466
+ blk_mq_pci_map_queues(map, to_pci_dev(dev->dev), offset);
467
+ else
468
+ blk_mq_map_queues(map);
469
+ qoff += map->nr_queues;
470
+ offset += map->nr_queues;
471
+ }
472
+
473
+ return 0;
474
+}
475
+
476
+/*
477
+ * Write sq tail if we are asked to, or if the next command would wrap.
478
+ */
479
+static inline void nvme_write_sq_db(struct nvme_queue *nvmeq, bool write_sq)
480
+{
481
+ if (!write_sq) {
482
+ u16 next_tail = nvmeq->sq_tail + 1;
483
+
484
+ if (next_tail == nvmeq->q_depth)
485
+ next_tail = 0;
486
+ if (next_tail != nvmeq->last_sq_tail)
487
+ return;
488
+ }
489
+
490
+ if (nvme_dbbuf_update_and_check_event(nvmeq->sq_tail,
491
+ nvmeq->dbbuf_sq_db, nvmeq->dbbuf_sq_ei))
492
+ writel(nvmeq->sq_tail, nvmeq->q_db);
493
+ nvmeq->last_sq_tail = nvmeq->sq_tail;
455494 }
456495
457496 /**
458497 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
459498 * @nvmeq: The queue to use
460499 * @cmd: The command to send
500
+ * @write_sq: whether to write to the SQ doorbell
461501 */
462
-static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
502
+static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd,
503
+ bool write_sq)
463504 {
464505 spin_lock(&nvmeq->sq_lock);
465
- if (nvmeq->sq_cmds_io)
466
- memcpy_toio(&nvmeq->sq_cmds_io[nvmeq->sq_tail], cmd,
467
- sizeof(*cmd));
468
- else
469
- memcpy(&nvmeq->sq_cmds[nvmeq->sq_tail], cmd, sizeof(*cmd));
470
-
506
+ memcpy(nvmeq->sq_cmds + (nvmeq->sq_tail << nvmeq->sqes),
507
+ cmd, sizeof(*cmd));
471508 if (++nvmeq->sq_tail == nvmeq->q_depth)
472509 nvmeq->sq_tail = 0;
473
- if (nvme_dbbuf_update_and_check_event(nvmeq->sq_tail,
474
- nvmeq->dbbuf_sq_db, nvmeq->dbbuf_sq_ei))
475
- writel(nvmeq->sq_tail, nvmeq->q_db);
510
+ nvme_write_sq_db(nvmeq, write_sq);
511
+ spin_unlock(&nvmeq->sq_lock);
512
+}
513
+
514
+static void nvme_commit_rqs(struct blk_mq_hw_ctx *hctx)
515
+{
516
+ struct nvme_queue *nvmeq = hctx->driver_data;
517
+
518
+ spin_lock(&nvmeq->sq_lock);
519
+ if (nvmeq->sq_tail != nvmeq->last_sq_tail)
520
+ nvme_write_sq_db(nvmeq, true);
476521 spin_unlock(&nvmeq->sq_lock);
477522 }
478523
....@@ -488,9 +533,6 @@
488533 int nseg = blk_rq_nr_phys_segments(req);
489534 unsigned int avg_seg_size;
490535
491
- if (nseg == 0)
492
- return false;
493
-
494536 avg_seg_size = DIV_ROUND_UP(blk_rq_payload_bytes(req), nseg);
495537
496538 if (!(dev->ctrl.sgls & ((1 << 0) | (1 << 1))))
....@@ -502,62 +544,72 @@
502544 return true;
503545 }
504546
505
-static blk_status_t nvme_init_iod(struct request *rq, struct nvme_dev *dev)
547
+static void nvme_free_prps(struct nvme_dev *dev, struct request *req)
506548 {
507
- struct nvme_iod *iod = blk_mq_rq_to_pdu(rq);
508
- int nseg = blk_rq_nr_phys_segments(rq);
509
- unsigned int size = blk_rq_payload_bytes(rq);
510
-
511
- iod->use_sgl = nvme_pci_use_sgls(dev, rq);
512
-
513
- if (nseg > NVME_INT_PAGES || size > NVME_INT_BYTES(dev)) {
514
- iod->sg = mempool_alloc(dev->iod_mempool, GFP_ATOMIC);
515
- if (!iod->sg)
516
- return BLK_STS_RESOURCE;
517
- } else {
518
- iod->sg = iod->inline_sg;
519
- }
520
-
521
- iod->aborted = 0;
522
- iod->npages = -1;
523
- iod->nents = 0;
524
- iod->length = size;
525
-
526
- return BLK_STS_OK;
527
-}
528
-
529
-static void nvme_free_iod(struct nvme_dev *dev, struct request *req)
530
-{
549
+ const int last_prp = NVME_CTRL_PAGE_SIZE / sizeof(__le64) - 1;
531550 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
532
- const int last_prp = dev->ctrl.page_size / sizeof(__le64) - 1;
533
- dma_addr_t dma_addr = iod->first_dma, next_dma_addr;
534
-
551
+ dma_addr_t dma_addr = iod->first_dma;
535552 int i;
536553
537
- if (iod->npages == 0)
538
- dma_pool_free(dev->prp_small_pool, nvme_pci_iod_list(req)[0],
539
- dma_addr);
540
-
541554 for (i = 0; i < iod->npages; i++) {
542
- void *addr = nvme_pci_iod_list(req)[i];
555
+ __le64 *prp_list = nvme_pci_iod_list(req)[i];
556
+ dma_addr_t next_dma_addr = le64_to_cpu(prp_list[last_prp]);
543557
544
- if (iod->use_sgl) {
545
- struct nvme_sgl_desc *sg_list = addr;
546
-
547
- next_dma_addr =
548
- le64_to_cpu((sg_list[SGES_PER_PAGE - 1]).addr);
549
- } else {
550
- __le64 *prp_list = addr;
551
-
552
- next_dma_addr = le64_to_cpu(prp_list[last_prp]);
553
- }
554
-
555
- dma_pool_free(dev->prp_page_pool, addr, dma_addr);
558
+ dma_pool_free(dev->prp_page_pool, prp_list, dma_addr);
556559 dma_addr = next_dma_addr;
557560 }
558561
559
- if (iod->sg != iod->inline_sg)
560
- mempool_free(iod->sg, dev->iod_mempool);
562
+}
563
+
564
+static void nvme_free_sgls(struct nvme_dev *dev, struct request *req)
565
+{
566
+ const int last_sg = SGES_PER_PAGE - 1;
567
+ struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
568
+ dma_addr_t dma_addr = iod->first_dma;
569
+ int i;
570
+
571
+ for (i = 0; i < iod->npages; i++) {
572
+ struct nvme_sgl_desc *sg_list = nvme_pci_iod_list(req)[i];
573
+ dma_addr_t next_dma_addr = le64_to_cpu((sg_list[last_sg]).addr);
574
+
575
+ dma_pool_free(dev->prp_page_pool, sg_list, dma_addr);
576
+ dma_addr = next_dma_addr;
577
+ }
578
+
579
+}
580
+
581
+static void nvme_unmap_sg(struct nvme_dev *dev, struct request *req)
582
+{
583
+ struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
584
+
585
+ if (is_pci_p2pdma_page(sg_page(iod->sg)))
586
+ pci_p2pdma_unmap_sg(dev->dev, iod->sg, iod->nents,
587
+ rq_dma_dir(req));
588
+ else
589
+ dma_unmap_sg(dev->dev, iod->sg, iod->nents, rq_dma_dir(req));
590
+}
591
+
592
+static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
593
+{
594
+ struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
595
+
596
+ if (iod->dma_len) {
597
+ dma_unmap_page(dev->dev, iod->first_dma, iod->dma_len,
598
+ rq_dma_dir(req));
599
+ return;
600
+ }
601
+
602
+ WARN_ON_ONCE(!iod->nents);
603
+
604
+ nvme_unmap_sg(dev, req);
605
+ if (iod->npages == 0)
606
+ dma_pool_free(dev->prp_small_pool, nvme_pci_iod_list(req)[0],
607
+ iod->first_dma);
608
+ else if (iod->use_sgl)
609
+ nvme_free_sgls(dev, req);
610
+ else
611
+ nvme_free_prps(dev, req);
612
+ mempool_free(iod->sg, dev->iod_mempool);
561613 }
562614
563615 static void nvme_print_sgl(struct scatterlist *sgl, int nents)
....@@ -583,34 +635,33 @@
583635 struct scatterlist *sg = iod->sg;
584636 int dma_len = sg_dma_len(sg);
585637 u64 dma_addr = sg_dma_address(sg);
586
- u32 page_size = dev->ctrl.page_size;
587
- int offset = dma_addr & (page_size - 1);
638
+ int offset = dma_addr & (NVME_CTRL_PAGE_SIZE - 1);
588639 __le64 *prp_list;
589640 void **list = nvme_pci_iod_list(req);
590641 dma_addr_t prp_dma;
591642 int nprps, i;
592643
593
- length -= (page_size - offset);
644
+ length -= (NVME_CTRL_PAGE_SIZE - offset);
594645 if (length <= 0) {
595646 iod->first_dma = 0;
596647 goto done;
597648 }
598649
599
- dma_len -= (page_size - offset);
650
+ dma_len -= (NVME_CTRL_PAGE_SIZE - offset);
600651 if (dma_len) {
601
- dma_addr += (page_size - offset);
652
+ dma_addr += (NVME_CTRL_PAGE_SIZE - offset);
602653 } else {
603654 sg = sg_next(sg);
604655 dma_addr = sg_dma_address(sg);
605656 dma_len = sg_dma_len(sg);
606657 }
607658
608
- if (length <= page_size) {
659
+ if (length <= NVME_CTRL_PAGE_SIZE) {
609660 iod->first_dma = dma_addr;
610661 goto done;
611662 }
612663
613
- nprps = DIV_ROUND_UP(length, page_size);
664
+ nprps = DIV_ROUND_UP(length, NVME_CTRL_PAGE_SIZE);
614665 if (nprps <= (256 / 8)) {
615666 pool = dev->prp_small_pool;
616667 iod->npages = 0;
....@@ -629,20 +680,20 @@
629680 iod->first_dma = prp_dma;
630681 i = 0;
631682 for (;;) {
632
- if (i == page_size >> 3) {
683
+ if (i == NVME_CTRL_PAGE_SIZE >> 3) {
633684 __le64 *old_prp_list = prp_list;
634685 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
635686 if (!prp_list)
636
- return BLK_STS_RESOURCE;
687
+ goto free_prps;
637688 list[iod->npages++] = prp_list;
638689 prp_list[0] = old_prp_list[i - 1];
639690 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
640691 i = 1;
641692 }
642693 prp_list[i++] = cpu_to_le64(dma_addr);
643
- dma_len -= page_size;
644
- dma_addr += page_size;
645
- length -= page_size;
694
+ dma_len -= NVME_CTRL_PAGE_SIZE;
695
+ dma_addr += NVME_CTRL_PAGE_SIZE;
696
+ length -= NVME_CTRL_PAGE_SIZE;
646697 if (length <= 0)
647698 break;
648699 if (dma_len > 0)
....@@ -653,14 +704,14 @@
653704 dma_addr = sg_dma_address(sg);
654705 dma_len = sg_dma_len(sg);
655706 }
656
-
657707 done:
658708 cmnd->dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
659709 cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma);
660
-
661710 return BLK_STS_OK;
662
-
663
- bad_sgl:
711
+free_prps:
712
+ nvme_free_prps(dev, req);
713
+ return BLK_STS_RESOURCE;
714
+bad_sgl:
664715 WARN(DO_ONCE(nvme_print_sgl, iod->sg, iod->nents),
665716 "Invalid SGL for payload:%d nents:%d\n",
666717 blk_rq_payload_bytes(req), iod->nents);
....@@ -732,7 +783,7 @@
732783
733784 sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma);
734785 if (!sg_list)
735
- return BLK_STS_RESOURCE;
786
+ goto free_sgls;
736787
737788 i = 0;
738789 nvme_pci_iod_list(req)[iod->npages++] = sg_list;
....@@ -745,74 +796,117 @@
745796 } while (--entries > 0);
746797
747798 return BLK_STS_OK;
799
+free_sgls:
800
+ nvme_free_sgls(dev, req);
801
+ return BLK_STS_RESOURCE;
802
+}
803
+
804
+static blk_status_t nvme_setup_prp_simple(struct nvme_dev *dev,
805
+ struct request *req, struct nvme_rw_command *cmnd,
806
+ struct bio_vec *bv)
807
+{
808
+ struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
809
+ unsigned int offset = bv->bv_offset & (NVME_CTRL_PAGE_SIZE - 1);
810
+ unsigned int first_prp_len = NVME_CTRL_PAGE_SIZE - offset;
811
+
812
+ iod->first_dma = dma_map_bvec(dev->dev, bv, rq_dma_dir(req), 0);
813
+ if (dma_mapping_error(dev->dev, iod->first_dma))
814
+ return BLK_STS_RESOURCE;
815
+ iod->dma_len = bv->bv_len;
816
+
817
+ cmnd->dptr.prp1 = cpu_to_le64(iod->first_dma);
818
+ if (bv->bv_len > first_prp_len)
819
+ cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma + first_prp_len);
820
+ else
821
+ cmnd->dptr.prp2 = 0;
822
+ return BLK_STS_OK;
823
+}
824
+
825
+static blk_status_t nvme_setup_sgl_simple(struct nvme_dev *dev,
826
+ struct request *req, struct nvme_rw_command *cmnd,
827
+ struct bio_vec *bv)
828
+{
829
+ struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
830
+
831
+ iod->first_dma = dma_map_bvec(dev->dev, bv, rq_dma_dir(req), 0);
832
+ if (dma_mapping_error(dev->dev, iod->first_dma))
833
+ return BLK_STS_RESOURCE;
834
+ iod->dma_len = bv->bv_len;
835
+
836
+ cmnd->flags = NVME_CMD_SGL_METABUF;
837
+ cmnd->dptr.sgl.addr = cpu_to_le64(iod->first_dma);
838
+ cmnd->dptr.sgl.length = cpu_to_le32(iod->dma_len);
839
+ cmnd->dptr.sgl.type = NVME_SGL_FMT_DATA_DESC << 4;
840
+ return BLK_STS_OK;
748841 }
749842
750843 static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,
751844 struct nvme_command *cmnd)
752845 {
753846 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
754
- struct request_queue *q = req->q;
755
- enum dma_data_direction dma_dir = rq_data_dir(req) ?
756
- DMA_TO_DEVICE : DMA_FROM_DEVICE;
757
- blk_status_t ret = BLK_STS_IOERR;
847
+ blk_status_t ret = BLK_STS_RESOURCE;
758848 int nr_mapped;
759849
850
+ if (blk_rq_nr_phys_segments(req) == 1) {
851
+ struct bio_vec bv = req_bvec(req);
852
+
853
+ if (!is_pci_p2pdma_page(bv.bv_page)) {
854
+ if (bv.bv_offset + bv.bv_len <= NVME_CTRL_PAGE_SIZE * 2)
855
+ return nvme_setup_prp_simple(dev, req,
856
+ &cmnd->rw, &bv);
857
+
858
+ if (iod->nvmeq->qid && sgl_threshold &&
859
+ dev->ctrl.sgls & ((1 << 0) | (1 << 1)))
860
+ return nvme_setup_sgl_simple(dev, req,
861
+ &cmnd->rw, &bv);
862
+ }
863
+ }
864
+
865
+ iod->dma_len = 0;
866
+ iod->sg = mempool_alloc(dev->iod_mempool, GFP_ATOMIC);
867
+ if (!iod->sg)
868
+ return BLK_STS_RESOURCE;
760869 sg_init_table(iod->sg, blk_rq_nr_phys_segments(req));
761
- iod->nents = blk_rq_map_sg(q, req, iod->sg);
870
+ iod->nents = blk_rq_map_sg(req->q, req, iod->sg);
762871 if (!iod->nents)
763
- goto out;
872
+ goto out_free_sg;
764873
765
- ret = BLK_STS_RESOURCE;
766
- nr_mapped = dma_map_sg_attrs(dev->dev, iod->sg, iod->nents, dma_dir,
767
- DMA_ATTR_NO_WARN);
874
+ if (is_pci_p2pdma_page(sg_page(iod->sg)))
875
+ nr_mapped = pci_p2pdma_map_sg_attrs(dev->dev, iod->sg,
876
+ iod->nents, rq_dma_dir(req), DMA_ATTR_NO_WARN);
877
+ else
878
+ nr_mapped = dma_map_sg_attrs(dev->dev, iod->sg, iod->nents,
879
+ rq_dma_dir(req), DMA_ATTR_NO_WARN);
768880 if (!nr_mapped)
769
- goto out;
881
+ goto out_free_sg;
770882
883
+ iod->use_sgl = nvme_pci_use_sgls(dev, req);
771884 if (iod->use_sgl)
772885 ret = nvme_pci_setup_sgls(dev, req, &cmnd->rw, nr_mapped);
773886 else
774887 ret = nvme_pci_setup_prps(dev, req, &cmnd->rw);
775
-
776888 if (ret != BLK_STS_OK)
777
- goto out_unmap;
778
-
779
- ret = BLK_STS_IOERR;
780
- if (blk_integrity_rq(req)) {
781
- if (blk_rq_count_integrity_sg(q, req->bio) != 1)
782
- goto out_unmap;
783
-
784
- sg_init_table(&iod->meta_sg, 1);
785
- if (blk_rq_map_integrity_sg(q, req->bio, &iod->meta_sg) != 1)
786
- goto out_unmap;
787
-
788
- if (!dma_map_sg(dev->dev, &iod->meta_sg, 1, dma_dir))
789
- goto out_unmap;
790
- }
791
-
792
- if (blk_integrity_rq(req))
793
- cmnd->rw.metadata = cpu_to_le64(sg_dma_address(&iod->meta_sg));
889
+ goto out_unmap_sg;
794890 return BLK_STS_OK;
795891
796
-out_unmap:
797
- dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
798
-out:
892
+out_unmap_sg:
893
+ nvme_unmap_sg(dev, req);
894
+out_free_sg:
895
+ mempool_free(iod->sg, dev->iod_mempool);
799896 return ret;
800897 }
801898
802
-static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
899
+static blk_status_t nvme_map_metadata(struct nvme_dev *dev, struct request *req,
900
+ struct nvme_command *cmnd)
803901 {
804902 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
805
- enum dma_data_direction dma_dir = rq_data_dir(req) ?
806
- DMA_TO_DEVICE : DMA_FROM_DEVICE;
807903
808
- if (iod->nents) {
809
- dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
810
- if (blk_integrity_rq(req))
811
- dma_unmap_sg(dev->dev, &iod->meta_sg, 1, dma_dir);
812
- }
813
-
814
- nvme_cleanup_cmd(req);
815
- nvme_free_iod(dev, req);
904
+ iod->meta_dma = dma_map_bvec(dev->dev, rq_integrity_vec(req),
905
+ rq_dma_dir(req), 0);
906
+ if (dma_mapping_error(dev->dev, iod->meta_dma))
907
+ return BLK_STS_IOERR;
908
+ cmnd->rw.metadata = cpu_to_le64(iod->meta_dma);
909
+ return BLK_STS_OK;
816910 }
817911
818912 /*
....@@ -825,35 +919,42 @@
825919 struct nvme_queue *nvmeq = hctx->driver_data;
826920 struct nvme_dev *dev = nvmeq->dev;
827921 struct request *req = bd->rq;
828
- struct nvme_command cmnd;
922
+ struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
923
+ struct nvme_command *cmnd = &iod->cmd;
829924 blk_status_t ret;
925
+
926
+ iod->aborted = 0;
927
+ iod->npages = -1;
928
+ iod->nents = 0;
830929
831930 /*
832931 * We should not need to do this, but we're still using this to
833932 * ensure we can drain requests on a dying queue.
834933 */
835
- if (unlikely(nvmeq->cq_vector < 0))
934
+ if (unlikely(!test_bit(NVMEQ_ENABLED, &nvmeq->flags)))
836935 return BLK_STS_IOERR;
837936
838
- ret = nvme_setup_cmd(ns, req, &cmnd);
937
+ ret = nvme_setup_cmd(ns, req, cmnd);
839938 if (ret)
840939 return ret;
841940
842
- ret = nvme_init_iod(req, dev);
843
- if (ret)
844
- goto out_free_cmd;
845
-
846941 if (blk_rq_nr_phys_segments(req)) {
847
- ret = nvme_map_data(dev, req, &cmnd);
942
+ ret = nvme_map_data(dev, req, cmnd);
848943 if (ret)
849
- goto out_cleanup_iod;
944
+ goto out_free_cmd;
945
+ }
946
+
947
+ if (blk_integrity_rq(req)) {
948
+ ret = nvme_map_metadata(dev, req, cmnd);
949
+ if (ret)
950
+ goto out_unmap_data;
850951 }
851952
852953 blk_mq_start_request(req);
853
- nvme_submit_cmd(nvmeq, &cmnd);
954
+ nvme_submit_cmd(nvmeq, cmnd, bd->last);
854955 return BLK_STS_OK;
855
-out_cleanup_iod:
856
- nvme_free_iod(dev, req);
956
+out_unmap_data:
957
+ nvme_unmap_data(dev, req);
857958 out_free_cmd:
858959 nvme_cleanup_cmd(req);
859960 return ret;
....@@ -862,16 +963,22 @@
862963 static void nvme_pci_complete_rq(struct request *req)
863964 {
864965 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
966
+ struct nvme_dev *dev = iod->nvmeq->dev;
865967
866
- nvme_unmap_data(iod->nvmeq->dev, req);
968
+ if (blk_integrity_rq(req))
969
+ dma_unmap_page(dev->dev, iod->meta_dma,
970
+ rq_integrity_vec(req)->bv_len, rq_data_dir(req));
971
+ if (blk_rq_nr_phys_segments(req))
972
+ nvme_unmap_data(dev, req);
867973 nvme_complete_rq(req);
868974 }
869975
870976 /* We read the CQE phase first to check if the rest of the entry is valid */
871977 static inline bool nvme_cqe_pending(struct nvme_queue *nvmeq)
872978 {
873
- return (le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
874
- nvmeq->cq_phase;
979
+ struct nvme_completion *hcqe = &nvmeq->cqes[nvmeq->cq_head];
980
+
981
+ return (le16_to_cpu(READ_ONCE(hcqe->status)) & 1) == nvmeq->cq_phase;
875982 }
876983
877984 static inline void nvme_ring_cq_doorbell(struct nvme_queue *nvmeq)
....@@ -883,17 +990,18 @@
883990 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
884991 }
885992
993
+static inline struct blk_mq_tags *nvme_queue_tagset(struct nvme_queue *nvmeq)
994
+{
995
+ if (!nvmeq->qid)
996
+ return nvmeq->dev->admin_tagset.tags[0];
997
+ return nvmeq->dev->tagset.tags[nvmeq->qid - 1];
998
+}
999
+
8861000 static inline void nvme_handle_cqe(struct nvme_queue *nvmeq, u16 idx)
8871001 {
888
- volatile struct nvme_completion *cqe = &nvmeq->cqes[idx];
1002
+ struct nvme_completion *cqe = &nvmeq->cqes[idx];
1003
+ __u16 command_id = READ_ONCE(cqe->command_id);
8891004 struct request *req;
890
-
891
- if (unlikely(cqe->command_id >= nvmeq->q_depth)) {
892
- dev_warn(nvmeq->dev->ctrl.device,
893
- "invalid id %d completed on queue %d\n",
894
- cqe->command_id, le16_to_cpu(cqe->sq_id));
895
- return;
896
- }
8971005
8981006 /*
8991007 * AEN requests are special as they don't time out and can
....@@ -901,50 +1009,53 @@
9011009 * aborts. We don't even bother to allocate a struct request
9021010 * for them but rather special case them here.
9031011 */
904
- if (unlikely(nvmeq->qid == 0 &&
905
- cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH)) {
1012
+ if (unlikely(nvme_is_aen_req(nvmeq->qid, command_id))) {
9061013 nvme_complete_async_event(&nvmeq->dev->ctrl,
9071014 cqe->status, &cqe->result);
9081015 return;
9091016 }
9101017
911
- req = blk_mq_tag_to_rq(*nvmeq->tags, cqe->command_id);
912
- nvme_end_request(req, cqe->status, cqe->result);
913
-}
914
-
915
-static void nvme_complete_cqes(struct nvme_queue *nvmeq, u16 start, u16 end)
916
-{
917
- while (start != end) {
918
- nvme_handle_cqe(nvmeq, start);
919
- if (++start == nvmeq->q_depth)
920
- start = 0;
1018
+ req = nvme_find_rq(nvme_queue_tagset(nvmeq), command_id);
1019
+ if (unlikely(!req)) {
1020
+ dev_warn(nvmeq->dev->ctrl.device,
1021
+ "invalid id %d completed on queue %d\n",
1022
+ command_id, le16_to_cpu(cqe->sq_id));
1023
+ return;
9211024 }
1025
+
1026
+ trace_nvme_sq(req, cqe->sq_head, nvmeq->sq_tail);
1027
+ if (!nvme_try_complete_req(req, cqe->status, cqe->result))
1028
+ nvme_pci_complete_rq(req);
9221029 }
9231030
9241031 static inline void nvme_update_cq_head(struct nvme_queue *nvmeq)
9251032 {
926
- if (nvmeq->cq_head == nvmeq->q_depth - 1) {
1033
+ u32 tmp = nvmeq->cq_head + 1;
1034
+
1035
+ if (tmp == nvmeq->q_depth) {
9271036 nvmeq->cq_head = 0;
928
- nvmeq->cq_phase = !nvmeq->cq_phase;
1037
+ nvmeq->cq_phase ^= 1;
9291038 } else {
930
- nvmeq->cq_head++;
1039
+ nvmeq->cq_head = tmp;
9311040 }
9321041 }
9331042
934
-static inline bool nvme_process_cq(struct nvme_queue *nvmeq, u16 *start,
935
- u16 *end, int tag)
1043
+static inline int nvme_process_cq(struct nvme_queue *nvmeq)
9361044 {
937
- bool found = false;
1045
+ int found = 0;
9381046
939
- *start = nvmeq->cq_head;
940
- while (!found && nvme_cqe_pending(nvmeq)) {
941
- if (nvmeq->cqes[nvmeq->cq_head].command_id == tag)
942
- found = true;
1047
+ while (nvme_cqe_pending(nvmeq)) {
1048
+ found++;
1049
+ /*
1050
+ * load-load control dependency between phase and the rest of
1051
+ * the cqe requires a full read memory barrier
1052
+ */
1053
+ dma_rmb();
1054
+ nvme_handle_cqe(nvmeq, nvmeq->cq_head);
9431055 nvme_update_cq_head(nvmeq);
9441056 }
945
- *end = nvmeq->cq_head;
9461057
947
- if (*start != *end)
1058
+ if (found)
9481059 nvme_ring_cq_doorbell(nvmeq);
9491060 return found;
9501061 }
....@@ -953,19 +1064,15 @@
9531064 {
9541065 struct nvme_queue *nvmeq = data;
9551066 irqreturn_t ret = IRQ_NONE;
956
- u16 start, end;
9571067
958
- spin_lock(&nvmeq->cq_lock);
959
- if (nvmeq->cq_head != nvmeq->last_cq_head)
1068
+ /*
1069
+ * The rmb/wmb pair ensures we see all updates from a previous run of
1070
+ * the irq handler, even if that was on another CPU.
1071
+ */
1072
+ rmb();
1073
+ if (nvme_process_cq(nvmeq))
9601074 ret = IRQ_HANDLED;
961
- nvme_process_cq(nvmeq, &start, &end, -1);
962
- nvmeq->last_cq_head = nvmeq->cq_head;
963
- spin_unlock(&nvmeq->cq_lock);
964
-
965
- if (start != end) {
966
- nvme_complete_cqes(nvmeq, start, end);
967
- return IRQ_HANDLED;
968
- }
1075
+ wmb();
9691076
9701077 return ret;
9711078 }
....@@ -973,32 +1080,40 @@
9731080 static irqreturn_t nvme_irq_check(int irq, void *data)
9741081 {
9751082 struct nvme_queue *nvmeq = data;
1083
+
9761084 if (nvme_cqe_pending(nvmeq))
9771085 return IRQ_WAKE_THREAD;
9781086 return IRQ_NONE;
9791087 }
9801088
981
-static int __nvme_poll(struct nvme_queue *nvmeq, unsigned int tag)
1089
+/*
1090
+ * Poll for completions for any interrupt driven queue
1091
+ * Can be called from any context.
1092
+ */
1093
+static void nvme_poll_irqdisable(struct nvme_queue *nvmeq)
9821094 {
983
- u16 start, end;
1095
+ struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev);
1096
+
1097
+ WARN_ON_ONCE(test_bit(NVMEQ_POLLED, &nvmeq->flags));
1098
+
1099
+ disable_irq(pci_irq_vector(pdev, nvmeq->cq_vector));
1100
+ nvme_process_cq(nvmeq);
1101
+ enable_irq(pci_irq_vector(pdev, nvmeq->cq_vector));
1102
+}
1103
+
1104
+static int nvme_poll(struct blk_mq_hw_ctx *hctx)
1105
+{
1106
+ struct nvme_queue *nvmeq = hctx->driver_data;
9841107 bool found;
9851108
9861109 if (!nvme_cqe_pending(nvmeq))
9871110 return 0;
9881111
989
- spin_lock_irq(&nvmeq->cq_lock);
990
- found = nvme_process_cq(nvmeq, &start, &end, tag);
991
- spin_unlock_irq(&nvmeq->cq_lock);
1112
+ spin_lock(&nvmeq->cq_poll_lock);
1113
+ found = nvme_process_cq(nvmeq);
1114
+ spin_unlock(&nvmeq->cq_poll_lock);
9921115
993
- nvme_complete_cqes(nvmeq, start, end);
9941116 return found;
995
-}
996
-
997
-static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
998
-{
999
- struct nvme_queue *nvmeq = hctx->driver_data;
1000
-
1001
- return __nvme_poll(nvmeq, tag);
10021117 }
10031118
10041119 static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl)
....@@ -1010,7 +1125,7 @@
10101125 memset(&c, 0, sizeof(c));
10111126 c.common.opcode = nvme_admin_async_event;
10121127 c.common.command_id = NVME_AQ_BLK_MQ_DEPTH;
1013
- nvme_submit_cmd(nvmeq, &c);
1128
+ nvme_submit_cmd(nvmeq, &c, true);
10141129 }
10151130
10161131 static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
....@@ -1028,7 +1143,10 @@
10281143 struct nvme_queue *nvmeq, s16 vector)
10291144 {
10301145 struct nvme_command c;
1031
- int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1146
+ int flags = NVME_QUEUE_PHYS_CONTIG;
1147
+
1148
+ if (!test_bit(NVMEQ_POLLED, &nvmeq->flags))
1149
+ flags |= NVME_CQ_IRQ_ENABLED;
10321150
10331151 /*
10341152 * Note: we (ab)use the fact that the prp fields survive if no data
....@@ -1098,7 +1216,6 @@
10981216
10991217 static bool nvme_should_reset(struct nvme_dev *dev, u32 csts)
11001218 {
1101
-
11021219 /* If true, indicates loss of adapter communication, possibly by a
11031220 * NVMe Subsystem reset.
11041221 */
....@@ -1147,7 +1264,6 @@
11471264 struct nvme_dev *dev = nvmeq->dev;
11481265 struct request *abort_req;
11491266 struct nvme_command cmd;
1150
- bool shutdown = false;
11511267 u32 csts = readl(dev->bar + NVME_REG_CSTS);
11521268
11531269 /* If PCI error recovery process is happening, we cannot reset or
....@@ -1170,7 +1286,12 @@
11701286 /*
11711287 * Did we miss an interrupt?
11721288 */
1173
- if (__nvme_poll(nvmeq, req->tag)) {
1289
+ if (test_bit(NVMEQ_POLLED, &nvmeq->flags))
1290
+ nvme_poll(req->mq_hctx);
1291
+ else
1292
+ nvme_poll_irqdisable(nvmeq);
1293
+
1294
+ if (blk_mq_request_completed(req)) {
11741295 dev_warn(dev->ctrl.device,
11751296 "I/O %d QID %d timeout, completion polled\n",
11761297 req->tag, nvmeq->qid);
....@@ -1184,33 +1305,35 @@
11841305 * shutdown, so we return BLK_EH_DONE.
11851306 */
11861307 switch (dev->ctrl.state) {
1187
- case NVME_CTRL_DELETING:
1188
- shutdown = true;
11891308 case NVME_CTRL_CONNECTING:
1190
- case NVME_CTRL_RESETTING:
1309
+ nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
1310
+ fallthrough;
1311
+ case NVME_CTRL_DELETING:
11911312 dev_warn_ratelimited(dev->ctrl.device,
11921313 "I/O %d QID %d timeout, disable controller\n",
11931314 req->tag, nvmeq->qid);
1194
- nvme_dev_disable(dev, shutdown);
11951315 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
1316
+ nvme_dev_disable(dev, true);
11961317 return BLK_EH_DONE;
1318
+ case NVME_CTRL_RESETTING:
1319
+ return BLK_EH_RESET_TIMER;
11971320 default:
11981321 break;
11991322 }
12001323
12011324 /*
1202
- * Shutdown the controller immediately and schedule a reset if the
1203
- * command was already aborted once before and still hasn't been
1204
- * returned to the driver, or if this is the admin queue.
1325
+ * Shutdown the controller immediately and schedule a reset if the
1326
+ * command was already aborted once before and still hasn't been
1327
+ * returned to the driver, or if this is the admin queue.
12051328 */
12061329 if (!nvmeq->qid || iod->aborted) {
12071330 dev_warn(dev->ctrl.device,
12081331 "I/O %d QID %d timeout, reset controller\n",
12091332 req->tag, nvmeq->qid);
1333
+ nvme_req(req)->flags |= NVME_REQ_CANCELLED;
12101334 nvme_dev_disable(dev, false);
12111335 nvme_reset_ctrl(&dev->ctrl);
12121336
1213
- nvme_req(req)->flags |= NVME_REQ_CANCELLED;
12141337 return BLK_EH_DONE;
12151338 }
12161339
....@@ -1222,7 +1345,7 @@
12221345
12231346 memset(&cmd, 0, sizeof(cmd));
12241347 cmd.abort.opcode = nvme_admin_abort_cmd;
1225
- cmd.abort.cid = req->tag;
1348
+ cmd.abort.cid = nvme_cid(req);
12261349 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
12271350
12281351 dev_warn(nvmeq->dev->ctrl.device,
....@@ -1230,13 +1353,12 @@
12301353 req->tag, nvmeq->qid);
12311354
12321355 abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd,
1233
- BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
1356
+ BLK_MQ_REQ_NOWAIT);
12341357 if (IS_ERR(abort_req)) {
12351358 atomic_inc(&dev->ctrl.abort_limit);
12361359 return BLK_EH_RESET_TIMER;
12371360 }
12381361
1239
- abort_req->timeout = ADMIN_TIMEOUT;
12401362 abort_req->end_io_data = NULL;
12411363 blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio);
12421364
....@@ -1250,11 +1372,18 @@
12501372
12511373 static void nvme_free_queue(struct nvme_queue *nvmeq)
12521374 {
1253
- dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1375
+ dma_free_coherent(nvmeq->dev->dev, CQ_SIZE(nvmeq),
12541376 (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
1255
- if (nvmeq->sq_cmds)
1256
- dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
1257
- nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1377
+ if (!nvmeq->sq_cmds)
1378
+ return;
1379
+
1380
+ if (test_and_clear_bit(NVMEQ_SQ_CMB, &nvmeq->flags)) {
1381
+ pci_free_p2pmem(to_pci_dev(nvmeq->dev->dev),
1382
+ nvmeq->sq_cmds, SQ_SIZE(nvmeq));
1383
+ } else {
1384
+ dma_free_coherent(nvmeq->dev->dev, SQ_SIZE(nvmeq),
1385
+ nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1386
+ }
12581387 }
12591388
12601389 static void nvme_free_queues(struct nvme_dev *dev, int lowest)
....@@ -1269,51 +1398,59 @@
12691398
12701399 /**
12711400 * nvme_suspend_queue - put queue into suspended state
1272
- * @nvmeq - queue to suspend
1401
+ * @nvmeq: queue to suspend
12731402 */
12741403 static int nvme_suspend_queue(struct nvme_queue *nvmeq)
12751404 {
1276
- int vector;
1277
-
1278
- spin_lock_irq(&nvmeq->cq_lock);
1279
- if (nvmeq->cq_vector == -1) {
1280
- spin_unlock_irq(&nvmeq->cq_lock);
1405
+ if (!test_and_clear_bit(NVMEQ_ENABLED, &nvmeq->flags))
12811406 return 1;
1282
- }
1283
- vector = nvmeq->cq_vector;
1284
- nvmeq->dev->online_queues--;
1285
- nvmeq->cq_vector = -1;
1286
- spin_unlock_irq(&nvmeq->cq_lock);
12871407
1288
- /*
1289
- * Ensure that nvme_queue_rq() sees it ->cq_vector == -1 without
1290
- * having to grab the lock.
1291
- */
1408
+ /* ensure that nvme_queue_rq() sees NVMEQ_ENABLED cleared */
12921409 mb();
12931410
1411
+ nvmeq->dev->online_queues--;
12941412 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
12951413 blk_mq_quiesce_queue(nvmeq->dev->ctrl.admin_q);
1296
-
1297
- pci_free_irq(to_pci_dev(nvmeq->dev->dev), vector, nvmeq);
1298
-
1414
+ if (!test_and_clear_bit(NVMEQ_POLLED, &nvmeq->flags))
1415
+ pci_free_irq(to_pci_dev(nvmeq->dev->dev), nvmeq->cq_vector, nvmeq);
12991416 return 0;
1417
+}
1418
+
1419
+static void nvme_suspend_io_queues(struct nvme_dev *dev)
1420
+{
1421
+ int i;
1422
+
1423
+ for (i = dev->ctrl.queue_count - 1; i > 0; i--)
1424
+ nvme_suspend_queue(&dev->queues[i]);
13001425 }
13011426
13021427 static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown)
13031428 {
13041429 struct nvme_queue *nvmeq = &dev->queues[0];
1305
- u16 start, end;
13061430
13071431 if (shutdown)
13081432 nvme_shutdown_ctrl(&dev->ctrl);
13091433 else
1310
- nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
1434
+ nvme_disable_ctrl(&dev->ctrl);
13111435
1312
- spin_lock_irq(&nvmeq->cq_lock);
1313
- nvme_process_cq(nvmeq, &start, &end, -1);
1314
- spin_unlock_irq(&nvmeq->cq_lock);
1436
+ nvme_poll_irqdisable(nvmeq);
1437
+}
13151438
1316
- nvme_complete_cqes(nvmeq, start, end);
1439
+/*
1440
+ * Called only on a device that has been disabled and after all other threads
1441
+ * that can check this device's completion queues have synced, except
1442
+ * nvme_poll(). This is the last chance for the driver to see a natural
1443
+ * completion before nvme_cancel_request() terminates all incomplete requests.
1444
+ */
1445
+static void nvme_reap_pending_cqes(struct nvme_dev *dev)
1446
+{
1447
+ int i;
1448
+
1449
+ for (i = dev->ctrl.queue_count - 1; i > 0; i--) {
1450
+ spin_lock(&dev->queues[i].cq_poll_lock);
1451
+ nvme_process_cq(&dev->queues[i]);
1452
+ spin_unlock(&dev->queues[i].cq_poll_lock);
1453
+ }
13171454 }
13181455
13191456 static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
....@@ -1321,11 +1458,12 @@
13211458 {
13221459 int q_depth = dev->q_depth;
13231460 unsigned q_size_aligned = roundup(q_depth * entry_size,
1324
- dev->ctrl.page_size);
1461
+ NVME_CTRL_PAGE_SIZE);
13251462
13261463 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
13271464 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
1328
- mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
1465
+
1466
+ mem_per_q = round_down(mem_per_q, NVME_CTRL_PAGE_SIZE);
13291467 q_depth = div_u64(mem_per_q, entry_size);
13301468
13311469 /*
....@@ -1341,14 +1479,26 @@
13411479 }
13421480
13431481 static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1344
- int qid, int depth)
1482
+ int qid)
13451483 {
1346
- /* CMB SQEs will be mapped before creation */
1347
- if (qid && dev->cmb && use_cmb_sqes && (dev->cmbsz & NVME_CMBSZ_SQS))
1348
- return 0;
1484
+ struct pci_dev *pdev = to_pci_dev(dev->dev);
13491485
1350
- nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1351
- &nvmeq->sq_dma_addr, GFP_KERNEL);
1486
+ if (qid && dev->cmb_use_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) {
1487
+ nvmeq->sq_cmds = pci_alloc_p2pmem(pdev, SQ_SIZE(nvmeq));
1488
+ if (nvmeq->sq_cmds) {
1489
+ nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
1490
+ nvmeq->sq_cmds);
1491
+ if (nvmeq->sq_dma_addr) {
1492
+ set_bit(NVMEQ_SQ_CMB, &nvmeq->flags);
1493
+ return 0;
1494
+ }
1495
+
1496
+ pci_free_p2pmem(pdev, nvmeq->sq_cmds, SQ_SIZE(nvmeq));
1497
+ }
1498
+ }
1499
+
1500
+ nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(nvmeq),
1501
+ &nvmeq->sq_dma_addr, GFP_KERNEL);
13521502 if (!nvmeq->sq_cmds)
13531503 return -ENOMEM;
13541504 return 0;
....@@ -1361,31 +1511,30 @@
13611511 if (dev->ctrl.queue_count > qid)
13621512 return 0;
13631513
1364
- nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
1365
- &nvmeq->cq_dma_addr, GFP_KERNEL);
1514
+ nvmeq->sqes = qid ? dev->io_sqes : NVME_ADM_SQES;
1515
+ nvmeq->q_depth = depth;
1516
+ nvmeq->cqes = dma_alloc_coherent(dev->dev, CQ_SIZE(nvmeq),
1517
+ &nvmeq->cq_dma_addr, GFP_KERNEL);
13661518 if (!nvmeq->cqes)
13671519 goto free_nvmeq;
13681520
1369
- if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
1521
+ if (nvme_alloc_sq_cmds(dev, nvmeq, qid))
13701522 goto free_cqdma;
13711523
1372
- nvmeq->q_dmadev = dev->dev;
13731524 nvmeq->dev = dev;
13741525 spin_lock_init(&nvmeq->sq_lock);
1375
- spin_lock_init(&nvmeq->cq_lock);
1526
+ spin_lock_init(&nvmeq->cq_poll_lock);
13761527 nvmeq->cq_head = 0;
13771528 nvmeq->cq_phase = 1;
13781529 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
1379
- nvmeq->q_depth = depth;
13801530 nvmeq->qid = qid;
1381
- nvmeq->cq_vector = -1;
13821531 dev->ctrl.queue_count++;
13831532
13841533 return 0;
13851534
13861535 free_cqdma:
1387
- dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
1388
- nvmeq->cq_dma_addr);
1536
+ dma_free_coherent(dev->dev, CQ_SIZE(nvmeq), (void *)nvmeq->cqes,
1537
+ nvmeq->cq_dma_addr);
13891538 free_nvmeq:
13901539 return -ENOMEM;
13911540 }
....@@ -1408,35 +1557,34 @@
14081557 {
14091558 struct nvme_dev *dev = nvmeq->dev;
14101559
1411
- spin_lock_irq(&nvmeq->cq_lock);
14121560 nvmeq->sq_tail = 0;
1561
+ nvmeq->last_sq_tail = 0;
14131562 nvmeq->cq_head = 0;
14141563 nvmeq->cq_phase = 1;
14151564 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
1416
- memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1565
+ memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq));
14171566 nvme_dbbuf_init(dev, nvmeq, qid);
14181567 dev->online_queues++;
1419
- spin_unlock_irq(&nvmeq->cq_lock);
1568
+ wmb(); /* ensure the first interrupt sees the initialization */
14201569 }
14211570
1422
-static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1571
+static int nvme_create_queue(struct nvme_queue *nvmeq, int qid, bool polled)
14231572 {
14241573 struct nvme_dev *dev = nvmeq->dev;
14251574 int result;
1426
- s16 vector;
1575
+ u16 vector = 0;
14271576
1428
- if (dev->cmb && use_cmb_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) {
1429
- unsigned offset = (qid - 1) * roundup(SQ_SIZE(nvmeq->q_depth),
1430
- dev->ctrl.page_size);
1431
- nvmeq->sq_dma_addr = dev->cmb_bus_addr + offset;
1432
- nvmeq->sq_cmds_io = dev->cmb + offset;
1433
- }
1577
+ clear_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags);
14341578
14351579 /*
14361580 * A queue's vector matches the queue identifier unless the controller
14371581 * has only one vector available.
14381582 */
1439
- vector = dev->num_vecs == 1 ? 0 : qid;
1583
+ if (!polled)
1584
+ vector = dev->num_vecs == 1 ? 0 : qid;
1585
+ else
1586
+ set_bit(NVMEQ_POLLED, &nvmeq->flags);
1587
+
14401588 result = adapter_alloc_cq(dev, qid, nvmeq, vector);
14411589 if (result)
14421590 return result;
....@@ -1444,24 +1592,22 @@
14441592 result = adapter_alloc_sq(dev, qid, nvmeq);
14451593 if (result < 0)
14461594 return result;
1447
- else if (result)
1595
+ if (result)
14481596 goto release_cq;
14491597
1450
- /*
1451
- * Set cq_vector after alloc cq/sq, otherwise nvme_suspend_queue will
1452
- * invoke free_irq for it and cause a 'Trying to free already-free IRQ
1453
- * xxx' warning if the create CQ/SQ command times out.
1454
- */
14551598 nvmeq->cq_vector = vector;
14561599 nvme_init_queue(nvmeq, qid);
1457
- result = queue_request_irq(nvmeq);
1458
- if (result < 0)
1459
- goto release_sq;
14601600
1601
+ if (!polled) {
1602
+ result = queue_request_irq(nvmeq);
1603
+ if (result < 0)
1604
+ goto release_sq;
1605
+ }
1606
+
1607
+ set_bit(NVMEQ_ENABLED, &nvmeq->flags);
14611608 return result;
14621609
14631610 release_sq:
1464
- nvmeq->cq_vector = -1;
14651611 dev->online_queues--;
14661612 adapter_delete_sq(dev, qid);
14671613 release_cq:
....@@ -1473,7 +1619,6 @@
14731619 .queue_rq = nvme_queue_rq,
14741620 .complete = nvme_pci_complete_rq,
14751621 .init_hctx = nvme_admin_init_hctx,
1476
- .exit_hctx = nvme_admin_exit_hctx,
14771622 .init_request = nvme_init_request,
14781623 .timeout = nvme_timeout,
14791624 };
....@@ -1481,6 +1626,7 @@
14811626 static const struct blk_mq_ops nvme_mq_ops = {
14821627 .queue_rq = nvme_queue_rq,
14831628 .complete = nvme_pci_complete_rq,
1629
+ .commit_rqs = nvme_commit_rqs,
14841630 .init_hctx = nvme_init_hctx,
14851631 .init_request = nvme_init_request,
14861632 .map_queues = nvme_pci_map_queues,
....@@ -1510,8 +1656,8 @@
15101656
15111657 dev->admin_tagset.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
15121658 dev->admin_tagset.timeout = ADMIN_TIMEOUT;
1513
- dev->admin_tagset.numa_node = dev_to_node(dev->dev);
1514
- dev->admin_tagset.cmd_size = nvme_pci_cmd_size(dev, false);
1659
+ dev->admin_tagset.numa_node = dev->ctrl.numa_node;
1660
+ dev->admin_tagset.cmd_size = sizeof(struct nvme_iod);
15151661 dev->admin_tagset.flags = BLK_MQ_F_NO_SCHED;
15161662 dev->admin_tagset.driver_data = dev;
15171663
....@@ -1522,6 +1668,7 @@
15221668 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
15231669 if (IS_ERR(dev->ctrl.admin_q)) {
15241670 blk_mq_free_tag_set(&dev->admin_tagset);
1671
+ dev->ctrl.admin_q = NULL;
15251672 return -ENOMEM;
15261673 }
15271674 if (!blk_get_queue(dev->ctrl.admin_q)) {
....@@ -1578,13 +1725,15 @@
15781725 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
15791726 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
15801727
1581
- result = nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
1728
+ result = nvme_disable_ctrl(&dev->ctrl);
15821729 if (result < 0)
15831730 return result;
15841731
15851732 result = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
15861733 if (result)
15871734 return result;
1735
+
1736
+ dev->ctrl.numa_node = dev_to_node(dev->dev);
15881737
15891738 nvmeq = &dev->queues[0];
15901739 aqa = nvmeq->q_depth - 1;
....@@ -1594,7 +1743,7 @@
15941743 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
15951744 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
15961745
1597
- result = nvme_enable_ctrl(&dev->ctrl, dev->ctrl.cap);
1746
+ result = nvme_enable_ctrl(&dev->ctrl);
15981747 if (result)
15991748 return result;
16001749
....@@ -1602,16 +1751,17 @@
16021751 nvme_init_queue(nvmeq, 0);
16031752 result = queue_request_irq(nvmeq);
16041753 if (result) {
1605
- nvmeq->cq_vector = -1;
1754
+ dev->online_queues--;
16061755 return result;
16071756 }
16081757
1758
+ set_bit(NVMEQ_ENABLED, &nvmeq->flags);
16091759 return result;
16101760 }
16111761
16121762 static int nvme_create_io_queues(struct nvme_dev *dev)
16131763 {
1614
- unsigned i, max;
1764
+ unsigned i, max, rw_queues;
16151765 int ret = 0;
16161766
16171767 for (i = dev->ctrl.queue_count; i <= dev->max_qid; i++) {
....@@ -1622,8 +1772,17 @@
16221772 }
16231773
16241774 max = min(dev->max_qid, dev->ctrl.queue_count - 1);
1775
+ if (max != 1 && dev->io_queues[HCTX_TYPE_POLL]) {
1776
+ rw_queues = dev->io_queues[HCTX_TYPE_DEFAULT] +
1777
+ dev->io_queues[HCTX_TYPE_READ];
1778
+ } else {
1779
+ rw_queues = max;
1780
+ }
1781
+
16251782 for (i = dev->online_queues; i <= max; i++) {
1626
- ret = nvme_create_queue(&dev->queues[i], i);
1783
+ bool polled = i > rw_queues;
1784
+
1785
+ ret = nvme_create_queue(&dev->queues[i], i, polled);
16271786 if (ret)
16281787 break;
16291788 }
....@@ -1670,13 +1829,13 @@
16701829 if (dev->cmb_size)
16711830 return;
16721831
1832
+ if (NVME_CAP_CMBS(dev->ctrl.cap))
1833
+ writel(NVME_CMBMSC_CRE, dev->bar + NVME_REG_CMBMSC);
1834
+
16731835 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
16741836 if (!dev->cmbsz)
16751837 return;
16761838 dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
1677
-
1678
- if (!use_cmb_sqes)
1679
- return;
16801839
16811840 size = nvme_cmb_size_unit(dev) * nvme_cmb_size(dev);
16821841 offset = nvme_cmb_size_unit(dev) * NVME_CMB_OFST(dev->cmbloc);
....@@ -1687,6 +1846,16 @@
16871846 return;
16881847
16891848 /*
1849
+ * Tell the controller about the host side address mapping the CMB,
1850
+ * and enable CMB decoding for the NVMe 1.4+ scheme:
1851
+ */
1852
+ if (NVME_CAP_CMBS(dev->ctrl.cap)) {
1853
+ hi_lo_writeq(NVME_CMBMSC_CRE | NVME_CMBMSC_CMSE |
1854
+ (pci_bus_address(pdev, bar) + offset),
1855
+ dev->bar + NVME_REG_CMBMSC);
1856
+ }
1857
+
1858
+ /*
16901859 * Controllers may support a CMB size larger than their BAR,
16911860 * for example, due to being behind a bridge. Reduce the CMB to
16921861 * the reported size of the BAR
....@@ -1694,11 +1863,18 @@
16941863 if (size > bar_size - offset)
16951864 size = bar_size - offset;
16961865
1697
- dev->cmb = ioremap_wc(pci_resource_start(pdev, bar) + offset, size);
1698
- if (!dev->cmb)
1866
+ if (pci_p2pdma_add_resource(pdev, bar, size, offset)) {
1867
+ dev_warn(dev->ctrl.device,
1868
+ "failed to register the CMB\n");
16991869 return;
1700
- dev->cmb_bus_addr = pci_bus_address(pdev, bar) + offset;
1870
+ }
1871
+
17011872 dev->cmb_size = size;
1873
+ dev->cmb_use_sqes = use_cmb_sqes && (dev->cmbsz & NVME_CMBSZ_SQS);
1874
+
1875
+ if ((dev->cmbsz & (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS)) ==
1876
+ (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS))
1877
+ pci_p2pmem_publish(pdev, true);
17021878
17031879 if (sysfs_add_file_to_group(&dev->ctrl.device->kobj,
17041880 &dev_attr_cmb.attr, NULL))
....@@ -1708,17 +1884,16 @@
17081884
17091885 static inline void nvme_release_cmb(struct nvme_dev *dev)
17101886 {
1711
- if (dev->cmb) {
1712
- iounmap(dev->cmb);
1713
- dev->cmb = NULL;
1887
+ if (dev->cmb_size) {
17141888 sysfs_remove_file_from_group(&dev->ctrl.device->kobj,
17151889 &dev_attr_cmb.attr, NULL);
1716
- dev->cmbsz = 0;
1890
+ dev->cmb_size = 0;
17171891 }
17181892 }
17191893
17201894 static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits)
17211895 {
1896
+ u32 host_mem_size = dev->host_mem_size >> NVME_CTRL_PAGE_SHIFT;
17221897 u64 dma_addr = dev->host_mem_descs_dma;
17231898 struct nvme_command c;
17241899 int ret;
....@@ -1727,8 +1902,7 @@
17271902 c.features.opcode = nvme_admin_set_features;
17281903 c.features.fid = cpu_to_le32(NVME_FEAT_HOST_MEM_BUF);
17291904 c.features.dword11 = cpu_to_le32(bits);
1730
- c.features.dword12 = cpu_to_le32(dev->host_mem_size >>
1731
- ilog2(dev->ctrl.page_size));
1905
+ c.features.dword12 = cpu_to_le32(host_mem_size);
17321906 c.features.dword13 = cpu_to_le32(lower_32_bits(dma_addr));
17331907 c.features.dword14 = cpu_to_le32(upper_32_bits(dma_addr));
17341908 c.features.dword15 = cpu_to_le32(dev->nr_host_mem_descs);
....@@ -1748,7 +1922,7 @@
17481922
17491923 for (i = 0; i < dev->nr_host_mem_descs; i++) {
17501924 struct nvme_host_mem_buf_desc *desc = &dev->host_mem_descs[i];
1751
- size_t size = le32_to_cpu(desc->size) * dev->ctrl.page_size;
1925
+ size_t size = le32_to_cpu(desc->size) * NVME_CTRL_PAGE_SIZE;
17521926
17531927 dma_free_attrs(dev->dev, size, dev->host_mem_desc_bufs[i],
17541928 le64_to_cpu(desc->addr),
....@@ -1781,8 +1955,8 @@
17811955 if (dev->ctrl.hmmaxd && dev->ctrl.hmmaxd < max_entries)
17821956 max_entries = dev->ctrl.hmmaxd;
17831957
1784
- descs = dma_zalloc_coherent(dev->dev, max_entries * sizeof(*descs),
1785
- &descs_dma, GFP_KERNEL);
1958
+ descs = dma_alloc_coherent(dev->dev, max_entries * sizeof(*descs),
1959
+ &descs_dma, GFP_KERNEL);
17861960 if (!descs)
17871961 goto out;
17881962
....@@ -1800,7 +1974,7 @@
18001974 break;
18011975
18021976 descs[i].addr = cpu_to_le64(dma_addr);
1803
- descs[i].size = cpu_to_le32(len / dev->ctrl.page_size);
1977
+ descs[i].size = cpu_to_le32(len / NVME_CTRL_PAGE_SIZE);
18041978 i++;
18051979 }
18061980
....@@ -1816,7 +1990,7 @@
18161990
18171991 out_free_bufs:
18181992 while (--i >= 0) {
1819
- size_t size = le32_to_cpu(descs[i].size) * dev->ctrl.page_size;
1993
+ size_t size = le32_to_cpu(descs[i].size) * NVME_CTRL_PAGE_SIZE;
18201994
18211995 dma_free_attrs(dev->dev, size, bufs[i],
18221996 le64_to_cpu(descs[i].addr),
....@@ -1834,12 +2008,12 @@
18342008
18352009 static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
18362010 {
1837
- u32 chunk_size;
2011
+ u64 min_chunk = min_t(u64, preferred, PAGE_SIZE * MAX_ORDER_NR_PAGES);
2012
+ u64 hmminds = max_t(u32, dev->ctrl.hmminds * 4096, PAGE_SIZE * 2);
2013
+ u64 chunk_size;
18382014
18392015 /* start big and work our way down */
1840
- for (chunk_size = min_t(u64, preferred, PAGE_SIZE * MAX_ORDER_NR_PAGES);
1841
- chunk_size >= max_t(u32, dev->ctrl.hmminds * 4096, PAGE_SIZE * 2);
1842
- chunk_size /= 2) {
2016
+ for (chunk_size = min_chunk; chunk_size >= hmminds; chunk_size /= 2) {
18432017 if (!__nvme_alloc_host_mem(dev, preferred, chunk_size)) {
18442018 if (!min || dev->host_mem_size >= min)
18452019 return 0;
....@@ -1895,32 +2069,132 @@
18952069 return ret;
18962070 }
18972071
2072
+/*
2073
+ * nirqs is the number of interrupts available for write and read
2074
+ * queues. The core already reserved an interrupt for the admin queue.
2075
+ */
2076
+static void nvme_calc_irq_sets(struct irq_affinity *affd, unsigned int nrirqs)
2077
+{
2078
+ struct nvme_dev *dev = affd->priv;
2079
+ unsigned int nr_read_queues, nr_write_queues = dev->nr_write_queues;
2080
+
2081
+ /*
2082
+ * If there is no interrupt available for queues, ensure that
2083
+ * the default queue is set to 1. The affinity set size is
2084
+ * also set to one, but the irq core ignores it for this case.
2085
+ *
2086
+ * If only one interrupt is available or 'write_queue' == 0, combine
2087
+ * write and read queues.
2088
+ *
2089
+ * If 'write_queues' > 0, ensure it leaves room for at least one read
2090
+ * queue.
2091
+ */
2092
+ if (!nrirqs) {
2093
+ nrirqs = 1;
2094
+ nr_read_queues = 0;
2095
+ } else if (nrirqs == 1 || !nr_write_queues) {
2096
+ nr_read_queues = 0;
2097
+ } else if (nr_write_queues >= nrirqs) {
2098
+ nr_read_queues = 1;
2099
+ } else {
2100
+ nr_read_queues = nrirqs - nr_write_queues;
2101
+ }
2102
+
2103
+ dev->io_queues[HCTX_TYPE_DEFAULT] = nrirqs - nr_read_queues;
2104
+ affd->set_size[HCTX_TYPE_DEFAULT] = nrirqs - nr_read_queues;
2105
+ dev->io_queues[HCTX_TYPE_READ] = nr_read_queues;
2106
+ affd->set_size[HCTX_TYPE_READ] = nr_read_queues;
2107
+ affd->nr_sets = nr_read_queues ? 2 : 1;
2108
+}
2109
+
2110
+static int nvme_setup_irqs(struct nvme_dev *dev, unsigned int nr_io_queues)
2111
+{
2112
+ struct pci_dev *pdev = to_pci_dev(dev->dev);
2113
+ struct irq_affinity affd = {
2114
+ .pre_vectors = 1,
2115
+ .calc_sets = nvme_calc_irq_sets,
2116
+ .priv = dev,
2117
+ };
2118
+ unsigned int irq_queues, poll_queues;
2119
+
2120
+ /*
2121
+ * Poll queues don't need interrupts, but we need at least one I/O queue
2122
+ * left over for non-polled I/O.
2123
+ */
2124
+ poll_queues = min(dev->nr_poll_queues, nr_io_queues - 1);
2125
+ dev->io_queues[HCTX_TYPE_POLL] = poll_queues;
2126
+
2127
+ /*
2128
+ * Initialize for the single interrupt case, will be updated in
2129
+ * nvme_calc_irq_sets().
2130
+ */
2131
+ dev->io_queues[HCTX_TYPE_DEFAULT] = 1;
2132
+ dev->io_queues[HCTX_TYPE_READ] = 0;
2133
+
2134
+ /*
2135
+ * We need interrupts for the admin queue and each non-polled I/O queue,
2136
+ * but some Apple controllers require all queues to use the first
2137
+ * vector.
2138
+ */
2139
+ irq_queues = 1;
2140
+ if (!(dev->ctrl.quirks & NVME_QUIRK_SINGLE_VECTOR))
2141
+ irq_queues += (nr_io_queues - poll_queues);
2142
+ return pci_alloc_irq_vectors_affinity(pdev, 1, irq_queues,
2143
+ PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY, &affd);
2144
+}
2145
+
2146
+static void nvme_disable_io_queues(struct nvme_dev *dev)
2147
+{
2148
+ if (__nvme_disable_io_queues(dev, nvme_admin_delete_sq))
2149
+ __nvme_disable_io_queues(dev, nvme_admin_delete_cq);
2150
+}
2151
+
2152
+static unsigned int nvme_max_io_queues(struct nvme_dev *dev)
2153
+{
2154
+ return num_possible_cpus() + dev->nr_write_queues + dev->nr_poll_queues;
2155
+}
2156
+
18982157 static int nvme_setup_io_queues(struct nvme_dev *dev)
18992158 {
19002159 struct nvme_queue *adminq = &dev->queues[0];
19012160 struct pci_dev *pdev = to_pci_dev(dev->dev);
1902
- int result, nr_io_queues;
2161
+ unsigned int nr_io_queues;
19032162 unsigned long size;
2163
+ int result;
19042164
1905
- struct irq_affinity affd = {
1906
- .pre_vectors = 1
1907
- };
2165
+ /*
2166
+ * Sample the module parameters once at reset time so that we have
2167
+ * stable values to work with.
2168
+ */
2169
+ dev->nr_write_queues = write_queues;
2170
+ dev->nr_poll_queues = poll_queues;
19082171
1909
- nr_io_queues = num_possible_cpus();
2172
+ /*
2173
+ * If tags are shared with admin queue (Apple bug), then
2174
+ * make sure we only use one IO queue.
2175
+ */
2176
+ if (dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS)
2177
+ nr_io_queues = 1;
2178
+ else
2179
+ nr_io_queues = min(nvme_max_io_queues(dev),
2180
+ dev->nr_allocated_queues - 1);
2181
+
19102182 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
19112183 if (result < 0)
19122184 return result;
19132185
19142186 if (nr_io_queues == 0)
19152187 return 0;
2188
+
2189
+ clear_bit(NVMEQ_ENABLED, &adminq->flags);
19162190
1917
- if (dev->cmb && (dev->cmbsz & NVME_CMBSZ_SQS)) {
2191
+ if (dev->cmb_use_sqes) {
19182192 result = nvme_cmb_qdepth(dev, nr_io_queues,
19192193 sizeof(struct nvme_command));
19202194 if (result > 0)
19212195 dev->q_depth = result;
19222196 else
1923
- nvme_release_cmb(dev);
2197
+ dev->cmb_use_sqes = false;
19242198 }
19252199
19262200 do {
....@@ -1933,6 +2207,7 @@
19332207 } while (1);
19342208 adminq->q_db = dev->dbs;
19352209
2210
+ retry:
19362211 /* Deregister the admin queue's interrupt */
19372212 pci_free_irq(pdev, 0, adminq);
19382213
....@@ -1941,12 +2216,14 @@
19412216 * setting up the full range we need.
19422217 */
19432218 pci_free_irq_vectors(pdev);
1944
- result = pci_alloc_irq_vectors_affinity(pdev, 1, nr_io_queues + 1,
1945
- PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY, &affd);
2219
+
2220
+ result = nvme_setup_irqs(dev, nr_io_queues);
19462221 if (result <= 0)
19472222 return -EIO;
2223
+
19482224 dev->num_vecs = result;
1949
- dev->max_qid = max(result - 1, 1);
2225
+ result = max(result - 1, 1);
2226
+ dev->max_qid = result + dev->io_queues[HCTX_TYPE_POLL];
19502227
19512228 /*
19522229 * Should investigate if there's a performance win from allocating
....@@ -1954,13 +2231,26 @@
19542231 * path to scale better, even if the receive path is limited by the
19552232 * number of interrupts.
19562233 */
1957
-
19582234 result = queue_request_irq(adminq);
1959
- if (result) {
1960
- adminq->cq_vector = -1;
2235
+ if (result)
19612236 return result;
2237
+ set_bit(NVMEQ_ENABLED, &adminq->flags);
2238
+
2239
+ result = nvme_create_io_queues(dev);
2240
+ if (result || dev->online_queues < 2)
2241
+ return result;
2242
+
2243
+ if (dev->online_queues - 1 < dev->max_qid) {
2244
+ nr_io_queues = dev->online_queues - 1;
2245
+ nvme_disable_io_queues(dev);
2246
+ nvme_suspend_io_queues(dev);
2247
+ goto retry;
19622248 }
1963
- return nvme_create_io_queues(dev);
2249
+ dev_info(dev->ctrl.device, "%d/%d/%d default/read/poll queues\n",
2250
+ dev->io_queues[HCTX_TYPE_DEFAULT],
2251
+ dev->io_queues[HCTX_TYPE_READ],
2252
+ dev->io_queues[HCTX_TYPE_POLL]);
2253
+ return 0;
19642254 }
19652255
19662256 static void nvme_del_queue_end(struct request *req, blk_status_t error)
....@@ -1968,23 +2258,15 @@
19682258 struct nvme_queue *nvmeq = req->end_io_data;
19692259
19702260 blk_mq_free_request(req);
1971
- complete(&nvmeq->dev->ioq_wait);
2261
+ complete(&nvmeq->delete_done);
19722262 }
19732263
19742264 static void nvme_del_cq_end(struct request *req, blk_status_t error)
19752265 {
19762266 struct nvme_queue *nvmeq = req->end_io_data;
1977
- u16 start, end;
19782267
1979
- if (!error) {
1980
- unsigned long flags;
1981
-
1982
- spin_lock_irqsave(&nvmeq->cq_lock, flags);
1983
- nvme_process_cq(nvmeq, &start, &end, -1);
1984
- spin_unlock_irqrestore(&nvmeq->cq_lock, flags);
1985
-
1986
- nvme_complete_cqes(nvmeq, start, end);
1987
- }
2268
+ if (error)
2269
+ set_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags);
19882270
19892271 nvme_del_queue_end(req, error);
19902272 }
....@@ -1999,77 +2281,80 @@
19992281 cmd.delete_queue.opcode = opcode;
20002282 cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid);
20012283
2002
- req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
2284
+ req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT);
20032285 if (IS_ERR(req))
20042286 return PTR_ERR(req);
20052287
2006
- req->timeout = ADMIN_TIMEOUT;
20072288 req->end_io_data = nvmeq;
20082289
2290
+ init_completion(&nvmeq->delete_done);
20092291 blk_execute_rq_nowait(q, NULL, req, false,
20102292 opcode == nvme_admin_delete_cq ?
20112293 nvme_del_cq_end : nvme_del_queue_end);
20122294 return 0;
20132295 }
20142296
2015
-static void nvme_disable_io_queues(struct nvme_dev *dev)
2297
+static bool __nvme_disable_io_queues(struct nvme_dev *dev, u8 opcode)
20162298 {
2017
- int pass, queues = dev->online_queues - 1;
2299
+ int nr_queues = dev->online_queues - 1, sent = 0;
20182300 unsigned long timeout;
2019
- u8 opcode = nvme_admin_delete_sq;
20202301
2021
- for (pass = 0; pass < 2; pass++) {
2022
- int sent = 0, i = queues;
2023
-
2024
- reinit_completion(&dev->ioq_wait);
20252302 retry:
2026
- timeout = ADMIN_TIMEOUT;
2027
- for (; i > 0; i--, sent++)
2028
- if (nvme_delete_queue(&dev->queues[i], opcode))
2029
- break;
2030
-
2031
- while (sent--) {
2032
- timeout = wait_for_completion_io_timeout(&dev->ioq_wait, timeout);
2033
- if (timeout == 0)
2034
- return;
2035
- if (i)
2036
- goto retry;
2037
- }
2038
- opcode = nvme_admin_delete_cq;
2303
+ timeout = ADMIN_TIMEOUT;
2304
+ while (nr_queues > 0) {
2305
+ if (nvme_delete_queue(&dev->queues[nr_queues], opcode))
2306
+ break;
2307
+ nr_queues--;
2308
+ sent++;
20392309 }
2310
+ while (sent) {
2311
+ struct nvme_queue *nvmeq = &dev->queues[nr_queues + sent];
2312
+
2313
+ timeout = wait_for_completion_io_timeout(&nvmeq->delete_done,
2314
+ timeout);
2315
+ if (timeout == 0)
2316
+ return false;
2317
+
2318
+ sent--;
2319
+ if (nr_queues)
2320
+ goto retry;
2321
+ }
2322
+ return true;
20402323 }
20412324
2042
-/*
2043
- * return error value only when tagset allocation failed
2044
- */
2045
-static int nvme_dev_add(struct nvme_dev *dev)
2325
+static void nvme_dev_add(struct nvme_dev *dev)
20462326 {
20472327 int ret;
20482328
20492329 if (!dev->ctrl.tagset) {
20502330 dev->tagset.ops = &nvme_mq_ops;
20512331 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2332
+ dev->tagset.nr_maps = 2; /* default + read */
2333
+ if (dev->io_queues[HCTX_TYPE_POLL])
2334
+ dev->tagset.nr_maps++;
20522335 dev->tagset.timeout = NVME_IO_TIMEOUT;
2053
- dev->tagset.numa_node = dev_to_node(dev->dev);
2054
- dev->tagset.queue_depth =
2055
- min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
2056
- dev->tagset.cmd_size = nvme_pci_cmd_size(dev, false);
2057
- if ((dev->ctrl.sgls & ((1 << 0) | (1 << 1))) && sgl_threshold) {
2058
- dev->tagset.cmd_size = max(dev->tagset.cmd_size,
2059
- nvme_pci_cmd_size(dev, true));
2060
- }
2336
+ dev->tagset.numa_node = dev->ctrl.numa_node;
2337
+ dev->tagset.queue_depth = min_t(unsigned int, dev->q_depth,
2338
+ BLK_MQ_MAX_DEPTH) - 1;
2339
+ dev->tagset.cmd_size = sizeof(struct nvme_iod);
20612340 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
20622341 dev->tagset.driver_data = dev;
2342
+
2343
+ /*
2344
+ * Some Apple controllers requires tags to be unique
2345
+ * across admin and IO queue, so reserve the first 32
2346
+ * tags of the IO queue.
2347
+ */
2348
+ if (dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS)
2349
+ dev->tagset.reserved_tags = NVME_AQ_DEPTH;
20632350
20642351 ret = blk_mq_alloc_tag_set(&dev->tagset);
20652352 if (ret) {
20662353 dev_warn(dev->ctrl.device,
20672354 "IO queues tagset allocation failed %d\n", ret);
2068
- return ret;
2355
+ return;
20692356 }
20702357 dev->ctrl.tagset = &dev->tagset;
2071
-
2072
- nvme_dbbuf_set(dev);
20732358 } else {
20742359 blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1);
20752360
....@@ -2077,7 +2362,7 @@
20772362 nvme_free_queues(dev, dev->online_queues);
20782363 }
20792364
2080
- return 0;
2365
+ nvme_dbbuf_set(dev);
20812366 }
20822367
20832368 static int nvme_pci_enable(struct nvme_dev *dev)
....@@ -2090,8 +2375,7 @@
20902375
20912376 pci_set_master(pdev);
20922377
2093
- if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
2094
- dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
2378
+ if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)))
20952379 goto disable;
20962380
20972381 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
....@@ -2110,10 +2394,21 @@
21102394
21112395 dev->ctrl.cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
21122396
2113
- dev->q_depth = min_t(int, NVME_CAP_MQES(dev->ctrl.cap) + 1,
2397
+ dev->q_depth = min_t(u32, NVME_CAP_MQES(dev->ctrl.cap) + 1,
21142398 io_queue_depth);
2399
+ dev->ctrl.sqsize = dev->q_depth - 1; /* 0's based queue depth */
21152400 dev->db_stride = 1 << NVME_CAP_STRIDE(dev->ctrl.cap);
21162401 dev->dbs = dev->bar + 4096;
2402
+
2403
+ /*
2404
+ * Some Apple controllers require a non-standard SQE size.
2405
+ * Interestingly they also seem to ignore the CC:IOSQES register
2406
+ * so we don't bother updating it here.
2407
+ */
2408
+ if (dev->ctrl.quirks & NVME_QUIRK_128_BYTES_SQES)
2409
+ dev->io_sqes = 7;
2410
+ else
2411
+ dev->io_sqes = NVME_NVM_IOSQES;
21172412
21182413 /*
21192414 * Temporary fix for the Apple controller found in the MacBook8,1 and
....@@ -2131,6 +2426,18 @@
21312426 dev_err(dev->ctrl.device, "detected PM1725 NVMe controller, "
21322427 "set queue depth=%u\n", dev->q_depth);
21332428 }
2429
+
2430
+ /*
2431
+ * Controllers with the shared tags quirk need the IO queue to be
2432
+ * big enough so that we get 32 tags for the admin queue
2433
+ */
2434
+ if ((dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS) &&
2435
+ (dev->q_depth < (NVME_AQ_DEPTH + 2))) {
2436
+ dev->q_depth = NVME_AQ_DEPTH + 2;
2437
+ dev_warn(dev->ctrl.device, "IO queue depth clamped to %d\n",
2438
+ dev->q_depth);
2439
+ }
2440
+
21342441
21352442 nvme_map_cmb(dev);
21362443
....@@ -2164,8 +2471,7 @@
21642471
21652472 static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
21662473 {
2167
- int i;
2168
- bool dead = true;
2474
+ bool dead = true, freeze = false;
21692475 struct pci_dev *pdev = to_pci_dev(dev->dev);
21702476
21712477 mutex_lock(&dev->shutdown_lock);
....@@ -2173,8 +2479,10 @@
21732479 u32 csts = readl(dev->bar + NVME_REG_CSTS);
21742480
21752481 if (dev->ctrl.state == NVME_CTRL_LIVE ||
2176
- dev->ctrl.state == NVME_CTRL_RESETTING)
2482
+ dev->ctrl.state == NVME_CTRL_RESETTING) {
2483
+ freeze = true;
21772484 nvme_start_freeze(&dev->ctrl);
2485
+ }
21782486 dead = !!((csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) ||
21792487 pdev->error_state != pci_channel_io_normal);
21802488 }
....@@ -2183,10 +2491,8 @@
21832491 * Give the controller a chance to complete all entered requests if
21842492 * doing a safe shutdown.
21852493 */
2186
- if (!dead) {
2187
- if (shutdown)
2188
- nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
2189
- }
2494
+ if (!dead && shutdown && freeze)
2495
+ nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
21902496
21912497 nvme_stop_queues(&dev->ctrl);
21922498
....@@ -2194,13 +2500,15 @@
21942500 nvme_disable_io_queues(dev);
21952501 nvme_disable_admin_queue(dev, shutdown);
21962502 }
2197
- for (i = dev->ctrl.queue_count - 1; i >= 0; i--)
2198
- nvme_suspend_queue(&dev->queues[i]);
2199
-
2503
+ nvme_suspend_io_queues(dev);
2504
+ nvme_suspend_queue(&dev->queues[0]);
22002505 nvme_pci_disable(dev);
2506
+ nvme_reap_pending_cqes(dev);
22012507
22022508 blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl);
22032509 blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl);
2510
+ blk_mq_tagset_wait_completed_request(&dev->tagset);
2511
+ blk_mq_tagset_wait_completed_request(&dev->admin_tagset);
22042512
22052513 /*
22062514 * The driver will not be starting up queues again if shutting down so
....@@ -2215,10 +2523,19 @@
22152523 mutex_unlock(&dev->shutdown_lock);
22162524 }
22172525
2526
+static int nvme_disable_prepare_reset(struct nvme_dev *dev, bool shutdown)
2527
+{
2528
+ if (!nvme_wait_reset(&dev->ctrl))
2529
+ return -EBUSY;
2530
+ nvme_dev_disable(dev, shutdown);
2531
+ return 0;
2532
+}
2533
+
22182534 static int nvme_setup_prp_pools(struct nvme_dev *dev)
22192535 {
22202536 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
2221
- PAGE_SIZE, PAGE_SIZE, 0);
2537
+ NVME_CTRL_PAGE_SIZE,
2538
+ NVME_CTRL_PAGE_SIZE, 0);
22222539 if (!dev->prp_page_pool)
22232540 return -ENOMEM;
22242541
....@@ -2238,26 +2555,35 @@
22382555 dma_pool_destroy(dev->prp_small_pool);
22392556 }
22402557
2558
+static void nvme_free_tagset(struct nvme_dev *dev)
2559
+{
2560
+ if (dev->tagset.tags)
2561
+ blk_mq_free_tag_set(&dev->tagset);
2562
+ dev->ctrl.tagset = NULL;
2563
+}
2564
+
22412565 static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
22422566 {
22432567 struct nvme_dev *dev = to_nvme_dev(ctrl);
22442568
22452569 nvme_dbbuf_dma_free(dev);
2246
- put_device(dev->dev);
2247
- if (dev->tagset.tags)
2248
- blk_mq_free_tag_set(&dev->tagset);
2570
+ nvme_free_tagset(dev);
22492571 if (dev->ctrl.admin_q)
22502572 blk_put_queue(dev->ctrl.admin_q);
2251
- kfree(dev->queues);
22522573 free_opal_dev(dev->ctrl.opal_dev);
22532574 mempool_destroy(dev->iod_mempool);
2575
+ put_device(dev->dev);
2576
+ kfree(dev->queues);
22542577 kfree(dev);
22552578 }
22562579
2257
-static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
2580
+static void nvme_remove_dead_ctrl(struct nvme_dev *dev)
22582581 {
2259
- dev_warn(dev->ctrl.device, "Removing after probe failure status: %d\n", status);
2260
-
2582
+ /*
2583
+ * Set state to deleting now to avoid blocking nvme_wait_reset(), which
2584
+ * may be holding this pci_dev's device lock.
2585
+ */
2586
+ nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
22612587 nvme_get_ctrl(&dev->ctrl);
22622588 nvme_dev_disable(dev, false);
22632589 nvme_kill_queues(&dev->ctrl);
....@@ -2271,7 +2597,6 @@
22712597 container_of(work, struct nvme_dev, ctrl.reset_work);
22722598 bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
22732599 int result;
2274
- enum nvme_ctrl_state new_state = NVME_CTRL_LIVE;
22752600
22762601 if (dev->ctrl.state != NVME_CTRL_RESETTING) {
22772602 dev_warn(dev->ctrl.device, "ctrl state %d is not RESETTING\n",
....@@ -2286,6 +2611,7 @@
22862611 */
22872612 if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
22882613 nvme_dev_disable(dev, false);
2614
+ nvme_sync_queues(&dev->ctrl);
22892615
22902616 mutex_lock(&dev->shutdown_lock);
22912617 result = nvme_pci_enable(dev);
....@@ -2300,12 +2626,21 @@
23002626 if (result)
23012627 goto out_unlock;
23022628
2629
+ dma_set_min_align_mask(dev->dev, NVME_CTRL_PAGE_SIZE - 1);
2630
+
23032631 /*
23042632 * Limit the max command size to prevent iod->sg allocations going
23052633 * over a single page.
23062634 */
2307
- dev->ctrl.max_hw_sectors = NVME_MAX_KB_SZ << 1;
2635
+ dev->ctrl.max_hw_sectors = min_t(u32,
2636
+ NVME_MAX_KB_SZ << 1, dma_max_mapping_size(dev->dev) >> 9);
23082637 dev->ctrl.max_segments = NVME_MAX_SEGS;
2638
+
2639
+ /*
2640
+ * Don't limit the IOMMU merged segment size.
2641
+ */
2642
+ dma_set_max_seg_size(dev->dev, 0xffffffff);
2643
+
23092644 mutex_unlock(&dev->shutdown_lock);
23102645
23112646 /*
....@@ -2318,6 +2653,12 @@
23182653 result = -EBUSY;
23192654 goto out;
23202655 }
2656
+
2657
+ /*
2658
+ * We do not support an SGL for metadata (yet), so we are limited to a
2659
+ * single integrity segment for the separate metadata pointer.
2660
+ */
2661
+ dev->ctrl.max_integrity_segments = 1;
23212662
23222663 result = nvme_init_identify(&dev->ctrl);
23232664 if (result)
....@@ -2359,13 +2700,11 @@
23592700 dev_warn(dev->ctrl.device, "IO queues not created\n");
23602701 nvme_kill_queues(&dev->ctrl);
23612702 nvme_remove_namespaces(&dev->ctrl);
2362
- new_state = NVME_CTRL_ADMIN_ONLY;
2703
+ nvme_free_tagset(dev);
23632704 } else {
23642705 nvme_start_queues(&dev->ctrl);
23652706 nvme_wait_freeze(&dev->ctrl);
2366
- /* hit this only when allocate tagset fails */
2367
- if (nvme_dev_add(dev))
2368
- new_state = NVME_CTRL_ADMIN_ONLY;
2707
+ nvme_dev_add(dev);
23692708 nvme_unfreeze(&dev->ctrl);
23702709 }
23712710
....@@ -2373,9 +2712,9 @@
23732712 * If only admin queue live, keep it to do further investigation or
23742713 * recovery.
23752714 */
2376
- if (!nvme_change_ctrl_state(&dev->ctrl, new_state)) {
2715
+ if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) {
23772716 dev_warn(dev->ctrl.device,
2378
- "failed to mark controller state %d\n", new_state);
2717
+ "failed to mark controller live state\n");
23792718 result = -ENODEV;
23802719 goto out;
23812720 }
....@@ -2386,7 +2725,10 @@
23862725 out_unlock:
23872726 mutex_unlock(&dev->shutdown_lock);
23882727 out:
2389
- nvme_remove_dead_ctrl(dev, result);
2728
+ if (result)
2729
+ dev_warn(dev->ctrl.device,
2730
+ "Removing after probe failure status: %d\n", result);
2731
+ nvme_remove_dead_ctrl(dev);
23902732 }
23912733
23922734 static void nvme_remove_dead_ctrl_work(struct work_struct *work)
....@@ -2421,13 +2763,14 @@
24212763 {
24222764 struct pci_dev *pdev = to_pci_dev(to_nvme_dev(ctrl)->dev);
24232765
2424
- return snprintf(buf, size, "%s", dev_name(&pdev->dev));
2766
+ return snprintf(buf, size, "%s\n", dev_name(&pdev->dev));
24252767 }
24262768
24272769 static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
24282770 .name = "pcie",
24292771 .module = THIS_MODULE,
2430
- .flags = NVME_F_METADATA_SUPPORTED,
2772
+ .flags = NVME_F_METADATA_SUPPORTED |
2773
+ NVME_F_PCI_P2PDMA,
24312774 .reg_read32 = nvme_pci_reg_read32,
24322775 .reg_write32 = nvme_pci_reg_write32,
24332776 .reg_read64 = nvme_pci_reg_read64,
....@@ -2478,10 +2821,48 @@
24782821 (dmi_match(DMI_BOARD_NAME, "PRIME B350M-A") ||
24792822 dmi_match(DMI_BOARD_NAME, "PRIME Z370-A")))
24802823 return NVME_QUIRK_NO_APST;
2824
+ } else if ((pdev->vendor == 0x144d && (pdev->device == 0xa801 ||
2825
+ pdev->device == 0xa808 || pdev->device == 0xa809)) ||
2826
+ (pdev->vendor == 0x1e0f && pdev->device == 0x0001)) {
2827
+ /*
2828
+ * Forcing to use host managed nvme power settings for
2829
+ * lowest idle power with quick resume latency on
2830
+ * Samsung and Toshiba SSDs based on suspend behavior
2831
+ * on Coffee Lake board for LENOVO C640
2832
+ */
2833
+ if ((dmi_match(DMI_BOARD_VENDOR, "LENOVO")) &&
2834
+ dmi_match(DMI_BOARD_NAME, "LNVNB161216"))
2835
+ return NVME_QUIRK_SIMPLE_SUSPEND;
24812836 }
24822837
24832838 return 0;
24842839 }
2840
+
2841
+#ifdef CONFIG_ACPI
2842
+static bool nvme_acpi_storage_d3(struct pci_dev *dev)
2843
+{
2844
+ struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
2845
+ u8 val;
2846
+
2847
+ /*
2848
+ * Look for _DSD property specifying that the storage device on the port
2849
+ * must use D3 to support deep platform power savings during
2850
+ * suspend-to-idle.
2851
+ */
2852
+
2853
+ if (!adev)
2854
+ return false;
2855
+ if (fwnode_property_read_u8(acpi_fwnode_handle(adev), "StorageD3Enable",
2856
+ &val))
2857
+ return false;
2858
+ return val == 1;
2859
+}
2860
+#else
2861
+static inline bool nvme_acpi_storage_d3(struct pci_dev *dev)
2862
+{
2863
+ return false;
2864
+}
2865
+#endif /* CONFIG_ACPI */
24852866
24862867 static void nvme_async_probe(void *data, async_cookie_t cookie)
24872868 {
....@@ -2507,7 +2888,10 @@
25072888 if (!dev)
25082889 return -ENOMEM;
25092890
2510
- dev->queues = kcalloc_node(num_possible_cpus() + 1,
2891
+ dev->nr_write_queues = write_queues;
2892
+ dev->nr_poll_queues = poll_queues;
2893
+ dev->nr_allocated_queues = nvme_max_io_queues(dev) + 1;
2894
+ dev->queues = kcalloc_node(dev->nr_allocated_queues,
25112895 sizeof(struct nvme_queue), GFP_KERNEL, node);
25122896 if (!dev->queues)
25132897 goto free;
....@@ -2522,7 +2906,6 @@
25222906 INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work);
25232907 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
25242908 mutex_init(&dev->shutdown_lock);
2525
- init_completion(&dev->ioq_wait);
25262909
25272910 result = nvme_setup_prp_pools(dev);
25282911 if (result)
....@@ -2530,12 +2913,21 @@
25302913
25312914 quirks |= check_vendor_combination_bug(pdev);
25322915
2916
+ if (!noacpi && nvme_acpi_storage_d3(pdev)) {
2917
+ /*
2918
+ * Some systems use a bios work around to ask for D3 on
2919
+ * platforms that support kernel managed suspend.
2920
+ */
2921
+ dev_info(&pdev->dev,
2922
+ "platform quirk: setting simple suspend\n");
2923
+ quirks |= NVME_QUIRK_SIMPLE_SUSPEND;
2924
+ }
2925
+
25332926 /*
25342927 * Double check that our mempool alloc size will cover the biggest
25352928 * command we support.
25362929 */
2537
- alloc_size = nvme_pci_iod_alloc_size(dev, NVME_MAX_KB_SZ,
2538
- NVME_MAX_SEGS, true);
2930
+ alloc_size = nvme_pci_iod_alloc_size();
25392931 WARN_ON_ONCE(alloc_size > PAGE_SIZE);
25402932
25412933 dev->iod_mempool = mempool_create_node(1, mempool_kmalloc,
....@@ -2555,7 +2947,6 @@
25552947 dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev));
25562948
25572949 nvme_reset_ctrl(&dev->ctrl);
2558
- nvme_get_ctrl(&dev->ctrl);
25592950 async_schedule(nvme_async_probe, dev);
25602951
25612952 return 0;
....@@ -2577,19 +2968,29 @@
25772968 static void nvme_reset_prepare(struct pci_dev *pdev)
25782969 {
25792970 struct nvme_dev *dev = pci_get_drvdata(pdev);
2580
- nvme_dev_disable(dev, false);
2971
+
2972
+ /*
2973
+ * We don't need to check the return value from waiting for the reset
2974
+ * state as pci_dev device lock is held, making it impossible to race
2975
+ * with ->remove().
2976
+ */
2977
+ nvme_disable_prepare_reset(dev, false);
2978
+ nvme_sync_queues(&dev->ctrl);
25812979 }
25822980
25832981 static void nvme_reset_done(struct pci_dev *pdev)
25842982 {
25852983 struct nvme_dev *dev = pci_get_drvdata(pdev);
2586
- nvme_reset_ctrl_sync(&dev->ctrl);
2984
+
2985
+ if (!nvme_try_sched_reset(&dev->ctrl))
2986
+ flush_work(&dev->ctrl.reset_work);
25872987 }
25882988
25892989 static void nvme_shutdown(struct pci_dev *pdev)
25902990 {
25912991 struct nvme_dev *dev = pci_get_drvdata(pdev);
2592
- nvme_dev_disable(dev, true);
2992
+
2993
+ nvme_disable_prepare_reset(dev, true);
25932994 }
25942995
25952996 /*
....@@ -2617,33 +3018,128 @@
26173018 nvme_free_host_mem(dev);
26183019 nvme_dev_remove_admin(dev);
26193020 nvme_free_queues(dev, 0);
2620
- nvme_uninit_ctrl(&dev->ctrl);
26213021 nvme_release_prp_pools(dev);
26223022 nvme_dev_unmap(dev);
2623
- nvme_put_ctrl(&dev->ctrl);
3023
+ nvme_uninit_ctrl(&dev->ctrl);
26243024 }
26253025
26263026 #ifdef CONFIG_PM_SLEEP
2627
-static int nvme_suspend(struct device *dev)
3027
+static int nvme_get_power_state(struct nvme_ctrl *ctrl, u32 *ps)
26283028 {
2629
- struct pci_dev *pdev = to_pci_dev(dev);
2630
- struct nvme_dev *ndev = pci_get_drvdata(pdev);
3029
+ return nvme_get_features(ctrl, NVME_FEAT_POWER_MGMT, 0, NULL, 0, ps);
3030
+}
26313031
2632
- nvme_dev_disable(ndev, true);
2633
- return 0;
3032
+static int nvme_set_power_state(struct nvme_ctrl *ctrl, u32 ps)
3033
+{
3034
+ return nvme_set_features(ctrl, NVME_FEAT_POWER_MGMT, ps, NULL, 0, NULL);
26343035 }
26353036
26363037 static int nvme_resume(struct device *dev)
26373038 {
3039
+ struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev));
3040
+ struct nvme_ctrl *ctrl = &ndev->ctrl;
3041
+
3042
+ if (ndev->last_ps == U32_MAX ||
3043
+ nvme_set_power_state(ctrl, ndev->last_ps) != 0)
3044
+ return nvme_try_sched_reset(&ndev->ctrl);
3045
+ return 0;
3046
+}
3047
+
3048
+static int nvme_suspend(struct device *dev)
3049
+{
3050
+ struct pci_dev *pdev = to_pci_dev(dev);
3051
+ struct nvme_dev *ndev = pci_get_drvdata(pdev);
3052
+ struct nvme_ctrl *ctrl = &ndev->ctrl;
3053
+ int ret = -EBUSY;
3054
+
3055
+ ndev->last_ps = U32_MAX;
3056
+
3057
+ /*
3058
+ * The platform does not remove power for a kernel managed suspend so
3059
+ * use host managed nvme power settings for lowest idle power if
3060
+ * possible. This should have quicker resume latency than a full device
3061
+ * shutdown. But if the firmware is involved after the suspend or the
3062
+ * device does not support any non-default power states, shut down the
3063
+ * device fully.
3064
+ *
3065
+ * If ASPM is not enabled for the device, shut down the device and allow
3066
+ * the PCI bus layer to put it into D3 in order to take the PCIe link
3067
+ * down, so as to allow the platform to achieve its minimum low-power
3068
+ * state (which may not be possible if the link is up).
3069
+ *
3070
+ * If a host memory buffer is enabled, shut down the device as the NVMe
3071
+ * specification allows the device to access the host memory buffer in
3072
+ * host DRAM from all power states, but hosts will fail access to DRAM
3073
+ * during S3.
3074
+ */
3075
+ if (pm_suspend_via_firmware() || !ctrl->npss ||
3076
+ !pcie_aspm_enabled(pdev) ||
3077
+ ndev->nr_host_mem_descs ||
3078
+ (ndev->ctrl.quirks & NVME_QUIRK_SIMPLE_SUSPEND))
3079
+ return nvme_disable_prepare_reset(ndev, true);
3080
+
3081
+ nvme_start_freeze(ctrl);
3082
+ nvme_wait_freeze(ctrl);
3083
+ nvme_sync_queues(ctrl);
3084
+
3085
+ if (ctrl->state != NVME_CTRL_LIVE)
3086
+ goto unfreeze;
3087
+
3088
+ ret = nvme_get_power_state(ctrl, &ndev->last_ps);
3089
+ if (ret < 0)
3090
+ goto unfreeze;
3091
+
3092
+ /*
3093
+ * A saved state prevents pci pm from generically controlling the
3094
+ * device's power. If we're using protocol specific settings, we don't
3095
+ * want pci interfering.
3096
+ */
3097
+ pci_save_state(pdev);
3098
+
3099
+ ret = nvme_set_power_state(ctrl, ctrl->npss);
3100
+ if (ret < 0)
3101
+ goto unfreeze;
3102
+
3103
+ if (ret) {
3104
+ /* discard the saved state */
3105
+ pci_load_saved_state(pdev, NULL);
3106
+
3107
+ /*
3108
+ * Clearing npss forces a controller reset on resume. The
3109
+ * correct value will be rediscovered then.
3110
+ */
3111
+ ret = nvme_disable_prepare_reset(ndev, true);
3112
+ ctrl->npss = 0;
3113
+ }
3114
+unfreeze:
3115
+ nvme_unfreeze(ctrl);
3116
+ return ret;
3117
+}
3118
+
3119
+static int nvme_simple_suspend(struct device *dev)
3120
+{
3121
+ struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev));
3122
+
3123
+ return nvme_disable_prepare_reset(ndev, true);
3124
+}
3125
+
3126
+static int nvme_simple_resume(struct device *dev)
3127
+{
26383128 struct pci_dev *pdev = to_pci_dev(dev);
26393129 struct nvme_dev *ndev = pci_get_drvdata(pdev);
26403130
2641
- nvme_reset_ctrl(&ndev->ctrl);
2642
- return 0;
3131
+ return nvme_try_sched_reset(&ndev->ctrl);
26433132 }
2644
-#endif
26453133
2646
-static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
3134
+static const struct dev_pm_ops nvme_dev_pm_ops = {
3135
+ .suspend = nvme_suspend,
3136
+ .resume = nvme_resume,
3137
+ .freeze = nvme_simple_suspend,
3138
+ .thaw = nvme_simple_resume,
3139
+ .poweroff = nvme_simple_suspend,
3140
+ .restore = nvme_simple_resume,
3141
+};
3142
+#endif /* CONFIG_PM_SLEEP */
26473143
26483144 static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
26493145 pci_channel_state_t state)
....@@ -2686,7 +3182,6 @@
26863182 struct nvme_dev *dev = pci_get_drvdata(pdev);
26873183
26883184 flush_work(&dev->ctrl.reset_work);
2689
- pci_cleanup_aer_uncorrect_error_status(pdev);
26903185 }
26913186
26923187 static const struct pci_error_handlers nvme_err_handler = {
....@@ -2698,25 +3193,37 @@
26983193 };
26993194
27003195 static const struct pci_device_id nvme_id_table[] = {
2701
- { PCI_VDEVICE(INTEL, 0x0953),
3196
+ { PCI_VDEVICE(INTEL, 0x0953), /* Intel 750/P3500/P3600/P3700 */
27023197 .driver_data = NVME_QUIRK_STRIPE_SIZE |
27033198 NVME_QUIRK_DEALLOCATE_ZEROES, },
2704
- { PCI_VDEVICE(INTEL, 0x0a53),
3199
+ { PCI_VDEVICE(INTEL, 0x0a53), /* Intel P3520 */
27053200 .driver_data = NVME_QUIRK_STRIPE_SIZE |
27063201 NVME_QUIRK_DEALLOCATE_ZEROES, },
2707
- { PCI_VDEVICE(INTEL, 0x0a54),
3202
+ { PCI_VDEVICE(INTEL, 0x0a54), /* Intel P4500/P4600 */
27083203 .driver_data = NVME_QUIRK_STRIPE_SIZE |
2709
- NVME_QUIRK_DEALLOCATE_ZEROES, },
2710
- { PCI_VDEVICE(INTEL, 0x0a55),
3204
+ NVME_QUIRK_DEALLOCATE_ZEROES |
3205
+ NVME_QUIRK_IGNORE_DEV_SUBNQN, },
3206
+ { PCI_VDEVICE(INTEL, 0x0a55), /* Dell Express Flash P4600 */
27113207 .driver_data = NVME_QUIRK_STRIPE_SIZE |
27123208 NVME_QUIRK_DEALLOCATE_ZEROES, },
27133209 { PCI_VDEVICE(INTEL, 0xf1a5), /* Intel 600P/P3100 */
27143210 .driver_data = NVME_QUIRK_NO_DEEPEST_PS |
2715
- NVME_QUIRK_MEDIUM_PRIO_SQ },
3211
+ NVME_QUIRK_MEDIUM_PRIO_SQ |
3212
+ NVME_QUIRK_NO_TEMP_THRESH_CHANGE |
3213
+ NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3214
+ { PCI_VDEVICE(INTEL, 0xf1a6), /* Intel 760p/Pro 7600p */
3215
+ .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN, },
27163216 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2717
- .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
3217
+ .driver_data = NVME_QUIRK_IDENTIFY_CNS |
3218
+ NVME_QUIRK_DISABLE_WRITE_ZEROES |
3219
+ NVME_QUIRK_BOGUS_NID, },
3220
+ { PCI_VDEVICE(REDHAT, 0x0010), /* Qemu emulated controller */
3221
+ .driver_data = NVME_QUIRK_BOGUS_NID, },
3222
+ { PCI_DEVICE(0x126f, 0x2263), /* Silicon Motion unidentified */
3223
+ .driver_data = NVME_QUIRK_NO_NS_DESC_LIST, },
27183224 { PCI_DEVICE(0x1bb1, 0x0100), /* Seagate Nytro Flash Storage */
2719
- .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
3225
+ .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY |
3226
+ NVME_QUIRK_NO_NS_DESC_LIST, },
27203227 { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */
27213228 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
27223229 { PCI_DEVICE(0x1c58, 0x0023), /* WDC SN200 adapter */
....@@ -2726,18 +3233,48 @@
27263233 { PCI_DEVICE(0x144d, 0xa821), /* Samsung PM1725 */
27273234 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
27283235 { PCI_DEVICE(0x144d, 0xa822), /* Samsung PM1725a */
2729
- .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
3236
+ .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY |
3237
+ NVME_QUIRK_DISABLE_WRITE_ZEROES|
3238
+ NVME_QUIRK_IGNORE_DEV_SUBNQN, },
3239
+ { PCI_DEVICE(0x1987, 0x5016), /* Phison E16 */
3240
+ .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN |
3241
+ NVME_QUIRK_BOGUS_NID, },
3242
+ { PCI_DEVICE(0x1b4b, 0x1092), /* Lexar 256 GB SSD */
3243
+ .driver_data = NVME_QUIRK_NO_NS_DESC_LIST |
3244
+ NVME_QUIRK_IGNORE_DEV_SUBNQN, },
27303245 { PCI_DEVICE(0x1d1d, 0x1f1f), /* LighNVM qemu device */
27313246 .driver_data = NVME_QUIRK_LIGHTNVM, },
27323247 { PCI_DEVICE(0x1d1d, 0x2807), /* CNEX WL */
27333248 .driver_data = NVME_QUIRK_LIGHTNVM, },
27343249 { PCI_DEVICE(0x1d1d, 0x2601), /* CNEX Granby */
27353250 .driver_data = NVME_QUIRK_LIGHTNVM, },
2736
- { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
3251
+ { PCI_DEVICE(0x10ec, 0x5762), /* ADATA SX6000LNP */
3252
+ .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN |
3253
+ NVME_QUIRK_BOGUS_NID, },
3254
+ { PCI_DEVICE(0x1cc1, 0x8201), /* ADATA SX8200PNP 512GB */
3255
+ .driver_data = NVME_QUIRK_NO_DEEPEST_PS |
3256
+ NVME_QUIRK_IGNORE_DEV_SUBNQN, },
3257
+ { PCI_DEVICE(0x1344, 0x5407), /* Micron Technology Inc NVMe SSD */
3258
+ .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN },
3259
+ { PCI_DEVICE(0x1344, 0x6001), /* Micron Nitro NVMe */
3260
+ .driver_data = NVME_QUIRK_BOGUS_NID, },
3261
+ { PCI_DEVICE(0x1c5c, 0x1504), /* SK Hynix PC400 */
3262
+ .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3263
+ { PCI_DEVICE(0x15b7, 0x2001), /* Sandisk Skyhawk */
3264
+ .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3265
+ { PCI_DEVICE(0x2646, 0x2262), /* KINGSTON SKC2000 NVMe SSD */
3266
+ .driver_data = NVME_QUIRK_NO_DEEPEST_PS, },
27373267 { PCI_DEVICE(0x2646, 0x2263), /* KINGSTON A2000 NVMe SSD */
27383268 .driver_data = NVME_QUIRK_NO_DEEPEST_PS, },
2739
- { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
3269
+ { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001),
3270
+ .driver_data = NVME_QUIRK_SINGLE_VECTOR },
27403271 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) },
3272
+ { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2005),
3273
+ .driver_data = NVME_QUIRK_SINGLE_VECTOR |
3274
+ NVME_QUIRK_128_BYTES_SQES |
3275
+ NVME_QUIRK_SHARED_TAGS |
3276
+ NVME_QUIRK_SKIP_CID_GEN },
3277
+ { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
27413278 { 0, }
27423279 };
27433280 MODULE_DEVICE_TABLE(pci, nvme_id_table);
....@@ -2748,15 +3285,22 @@
27483285 .probe = nvme_probe,
27493286 .remove = nvme_remove,
27503287 .shutdown = nvme_shutdown,
3288
+#ifdef CONFIG_PM_SLEEP
27513289 .driver = {
27523290 .pm = &nvme_dev_pm_ops,
27533291 },
3292
+#endif
27543293 .sriov_configure = pci_sriov_configure_simple,
27553294 .err_handler = &nvme_err_handler,
27563295 };
27573296
27583297 static int __init nvme_init(void)
27593298 {
3299
+ BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
3300
+ BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
3301
+ BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
3302
+ BUILD_BUG_ON(IRQ_AFFINITY_MAX_SETS < 2);
3303
+
27603304 return pci_register_driver(&nvme_driver);
27613305 }
27623306
....@@ -2764,7 +3308,6 @@
27643308 {
27653309 pci_unregister_driver(&nvme_driver);
27663310 flush_workqueue(nvme_wq);
2767
- _nvme_check_size();
27683311 }
27693312
27703313 MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");