forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-10 cde9070d9970eef1f7ec2360586c802a16230ad8
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,17 +19,21 @@
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
39
-#define SGES_PER_PAGE (PAGE_SIZE / sizeof(struct nvme_sgl_desc))
36
+#define SGES_PER_PAGE (NVME_CTRL_PAGE_SIZE / sizeof(struct nvme_sgl_desc))
4037
4138 /*
4239 * These can be higher, but we need to ensure that any command doesn't
....@@ -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,28 +119,29 @@
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
112141 /* shadow doorbell buffer support: */
113
- u32 *dbbuf_dbs;
142
+ __le32 *dbbuf_dbs;
114143 dma_addr_t dbbuf_dbs_dma_addr;
115
- u32 *dbbuf_eis;
144
+ __le32 *dbbuf_eis;
116145 dma_addr_t dbbuf_eis_dma_addr;
117146
118147 /* host memory buffer support: */
....@@ -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;
175
- u32 *dbbuf_sq_db;
176
- u32 *dbbuf_cq_db;
177
- u32 *dbbuf_sq_ei;
178
- u32 *dbbuf_cq_ei;
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
212
+ __le32 *dbbuf_sq_db;
213
+ __le32 *dbbuf_cq_db;
214
+ __le32 *dbbuf_sq_ei;
215
+ __le32 *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,
....@@ -316,11 +334,11 @@
316334 }
317335
318336 /* Update dbbuf and return true if an MMIO is required */
319
-static bool nvme_dbbuf_update_and_check_event(u16 value, u32 *dbbuf_db,
320
- volatile u32 *dbbuf_ei)
337
+static bool nvme_dbbuf_update_and_check_event(u16 value, __le32 *dbbuf_db,
338
+ volatile __le32 *dbbuf_ei)
321339 {
322340 if (dbbuf_db) {
323
- u16 old_value;
341
+ u16 old_value, event_idx;
324342
325343 /*
326344 * Ensure that the queue is written before updating
....@@ -328,8 +346,8 @@
328346 */
329347 wmb();
330348
331
- old_value = *dbbuf_db;
332
- *dbbuf_db = value;
349
+ old_value = le32_to_cpu(*dbbuf_db);
350
+ *dbbuf_db = cpu_to_le32(value);
333351
334352 /*
335353 * Ensure that the doorbell is updated before reading the event
....@@ -339,7 +357,8 @@
339357 */
340358 mb();
341359
342
- if (!nvme_dbbuf_need_event(*dbbuf_ei, value, old_value))
360
+ event_idx = le32_to_cpu(*dbbuf_ei);
361
+ if (!nvme_dbbuf_need_event(event_idx, value, old_value))
343362 return false;
344363 }
345364
....@@ -347,52 +366,25 @@
347366 }
348367
349368 /*
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
-/*
356369 * Will slightly overestimate the number of pages needed. This is OK
357370 * as it only leads to a small amount of wasted memory for the lifetime of
358371 * the I/O.
359372 */
360
-static int nvme_npages(unsigned size, struct nvme_dev *dev)
373
+static int nvme_pci_npages_prp(void)
361374 {
362
- unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
363
- dev->ctrl.page_size);
364
- return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
375
+ unsigned max_bytes = (NVME_MAX_KB_SZ * 1024) + NVME_CTRL_PAGE_SIZE;
376
+ unsigned nprps = DIV_ROUND_UP(max_bytes, NVME_CTRL_PAGE_SIZE);
377
+ return DIV_ROUND_UP(8 * nprps, NVME_CTRL_PAGE_SIZE - 8);
365378 }
366379
367380 /*
368381 * Calculates the number of pages needed for the SGL segments. For example a 4k
369382 * page can accommodate 256 SGL descriptors.
370383 */
371
-static int nvme_pci_npages_sgl(unsigned int num_seg)
384
+static int nvme_pci_npages_sgl(void)
372385 {
373
- return DIV_ROUND_UP(num_seg * sizeof(struct nvme_sgl_desc), PAGE_SIZE);
374
-}
375
-
376
-static unsigned int nvme_pci_iod_alloc_size(struct nvme_dev *dev,
377
- unsigned int size, unsigned int nseg, bool use_sgl)
378
-{
379
- size_t alloc_size;
380
-
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;
386
+ return DIV_ROUND_UP(NVME_MAX_SEGS * sizeof(struct nvme_sgl_desc),
387
+ NVME_CTRL_PAGE_SIZE);
396388 }
397389
398390 static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
....@@ -403,18 +395,9 @@
403395
404396 WARN_ON(hctx_idx != 0);
405397 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
406
- WARN_ON(nvmeq->tags);
407398
408399 hctx->driver_data = nvmeq;
409
- nvmeq->tags = &dev->admin_tagset.tags[0];
410400 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;
418401 }
419402
420403 static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
....@@ -422,9 +405,6 @@
422405 {
423406 struct nvme_dev *dev = data;
424407 struct nvme_queue *nvmeq = &dev->queues[hctx_idx + 1];
425
-
426
- if (!nvmeq->tags)
427
- nvmeq->tags = &dev->tagset.tags[hctx_idx];
428408
429409 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
430410 hctx->driver_data = nvmeq;
....@@ -446,33 +426,91 @@
446426 return 0;
447427 }
448428
429
+static int queue_irq_offset(struct nvme_dev *dev)
430
+{
431
+ /* if we have more than 1 vec, admin queue offsets us by 1 */
432
+ if (dev->num_vecs > 1)
433
+ return 1;
434
+
435
+ return 0;
436
+}
437
+
449438 static int nvme_pci_map_queues(struct blk_mq_tag_set *set)
450439 {
451440 struct nvme_dev *dev = set->driver_data;
441
+ int i, qoff, offset;
452442
453
- return blk_mq_pci_map_queues(set, to_pci_dev(dev->dev),
454
- dev->num_vecs > 1 ? 1 /* admin queue */ : 0);
443
+ offset = queue_irq_offset(dev);
444
+ for (i = 0, qoff = 0; i < set->nr_maps; i++) {
445
+ struct blk_mq_queue_map *map = &set->map[i];
446
+
447
+ map->nr_queues = dev->io_queues[i];
448
+ if (!map->nr_queues) {
449
+ BUG_ON(i == HCTX_TYPE_DEFAULT);
450
+ continue;
451
+ }
452
+
453
+ /*
454
+ * The poll queue(s) doesn't have an IRQ (and hence IRQ
455
+ * affinity), so use the regular blk-mq cpu mapping
456
+ */
457
+ map->queue_offset = qoff;
458
+ if (i != HCTX_TYPE_POLL && offset)
459
+ blk_mq_pci_map_queues(map, to_pci_dev(dev->dev), offset);
460
+ else
461
+ blk_mq_map_queues(map);
462
+ qoff += map->nr_queues;
463
+ offset += map->nr_queues;
464
+ }
465
+
466
+ return 0;
467
+}
468
+
469
+/*
470
+ * Write sq tail if we are asked to, or if the next command would wrap.
471
+ */
472
+static inline void nvme_write_sq_db(struct nvme_queue *nvmeq, bool write_sq)
473
+{
474
+ if (!write_sq) {
475
+ u16 next_tail = nvmeq->sq_tail + 1;
476
+
477
+ if (next_tail == nvmeq->q_depth)
478
+ next_tail = 0;
479
+ if (next_tail != nvmeq->last_sq_tail)
480
+ return;
481
+ }
482
+
483
+ if (nvme_dbbuf_update_and_check_event(nvmeq->sq_tail,
484
+ nvmeq->dbbuf_sq_db, nvmeq->dbbuf_sq_ei))
485
+ writel(nvmeq->sq_tail, nvmeq->q_db);
486
+ nvmeq->last_sq_tail = nvmeq->sq_tail;
455487 }
456488
457489 /**
458490 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
459491 * @nvmeq: The queue to use
460492 * @cmd: The command to send
493
+ * @write_sq: whether to write to the SQ doorbell
461494 */
462
-static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
495
+static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd,
496
+ bool write_sq)
463497 {
464498 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
-
499
+ memcpy(nvmeq->sq_cmds + (nvmeq->sq_tail << nvmeq->sqes),
500
+ cmd, sizeof(*cmd));
471501 if (++nvmeq->sq_tail == nvmeq->q_depth)
472502 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);
503
+ nvme_write_sq_db(nvmeq, write_sq);
504
+ spin_unlock(&nvmeq->sq_lock);
505
+}
506
+
507
+static void nvme_commit_rqs(struct blk_mq_hw_ctx *hctx)
508
+{
509
+ struct nvme_queue *nvmeq = hctx->driver_data;
510
+
511
+ spin_lock(&nvmeq->sq_lock);
512
+ if (nvmeq->sq_tail != nvmeq->last_sq_tail)
513
+ nvme_write_sq_db(nvmeq, true);
476514 spin_unlock(&nvmeq->sq_lock);
477515 }
478516
....@@ -488,9 +526,6 @@
488526 int nseg = blk_rq_nr_phys_segments(req);
489527 unsigned int avg_seg_size;
490528
491
- if (nseg == 0)
492
- return false;
493
-
494529 avg_seg_size = DIV_ROUND_UP(blk_rq_payload_bytes(req), nseg);
495530
496531 if (!(dev->ctrl.sgls & ((1 << 0) | (1 << 1))))
....@@ -502,62 +537,72 @@
502537 return true;
503538 }
504539
505
-static blk_status_t nvme_init_iod(struct request *rq, struct nvme_dev *dev)
540
+static void nvme_free_prps(struct nvme_dev *dev, struct request *req)
506541 {
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
-{
542
+ const int last_prp = NVME_CTRL_PAGE_SIZE / sizeof(__le64) - 1;
531543 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
-
544
+ dma_addr_t dma_addr = iod->first_dma;
535545 int i;
536546
537
- if (iod->npages == 0)
538
- dma_pool_free(dev->prp_small_pool, nvme_pci_iod_list(req)[0],
539
- dma_addr);
540
-
541547 for (i = 0; i < iod->npages; i++) {
542
- void *addr = nvme_pci_iod_list(req)[i];
548
+ __le64 *prp_list = nvme_pci_iod_list(req)[i];
549
+ dma_addr_t next_dma_addr = le64_to_cpu(prp_list[last_prp]);
543550
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);
551
+ dma_pool_free(dev->prp_page_pool, prp_list, dma_addr);
556552 dma_addr = next_dma_addr;
557553 }
558554
559
- if (iod->sg != iod->inline_sg)
560
- mempool_free(iod->sg, dev->iod_mempool);
555
+}
556
+
557
+static void nvme_free_sgls(struct nvme_dev *dev, struct request *req)
558
+{
559
+ const int last_sg = SGES_PER_PAGE - 1;
560
+ struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
561
+ dma_addr_t dma_addr = iod->first_dma;
562
+ int i;
563
+
564
+ for (i = 0; i < iod->npages; i++) {
565
+ struct nvme_sgl_desc *sg_list = nvme_pci_iod_list(req)[i];
566
+ dma_addr_t next_dma_addr = le64_to_cpu((sg_list[last_sg]).addr);
567
+
568
+ dma_pool_free(dev->prp_page_pool, sg_list, dma_addr);
569
+ dma_addr = next_dma_addr;
570
+ }
571
+
572
+}
573
+
574
+static void nvme_unmap_sg(struct nvme_dev *dev, struct request *req)
575
+{
576
+ struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
577
+
578
+ if (is_pci_p2pdma_page(sg_page(iod->sg)))
579
+ pci_p2pdma_unmap_sg(dev->dev, iod->sg, iod->nents,
580
+ rq_dma_dir(req));
581
+ else
582
+ dma_unmap_sg(dev->dev, iod->sg, iod->nents, rq_dma_dir(req));
583
+}
584
+
585
+static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
586
+{
587
+ struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
588
+
589
+ if (iod->dma_len) {
590
+ dma_unmap_page(dev->dev, iod->first_dma, iod->dma_len,
591
+ rq_dma_dir(req));
592
+ return;
593
+ }
594
+
595
+ WARN_ON_ONCE(!iod->nents);
596
+
597
+ nvme_unmap_sg(dev, req);
598
+ if (iod->npages == 0)
599
+ dma_pool_free(dev->prp_small_pool, nvme_pci_iod_list(req)[0],
600
+ iod->first_dma);
601
+ else if (iod->use_sgl)
602
+ nvme_free_sgls(dev, req);
603
+ else
604
+ nvme_free_prps(dev, req);
605
+ mempool_free(iod->sg, dev->iod_mempool);
561606 }
562607
563608 static void nvme_print_sgl(struct scatterlist *sgl, int nents)
....@@ -583,34 +628,33 @@
583628 struct scatterlist *sg = iod->sg;
584629 int dma_len = sg_dma_len(sg);
585630 u64 dma_addr = sg_dma_address(sg);
586
- u32 page_size = dev->ctrl.page_size;
587
- int offset = dma_addr & (page_size - 1);
631
+ int offset = dma_addr & (NVME_CTRL_PAGE_SIZE - 1);
588632 __le64 *prp_list;
589633 void **list = nvme_pci_iod_list(req);
590634 dma_addr_t prp_dma;
591635 int nprps, i;
592636
593
- length -= (page_size - offset);
637
+ length -= (NVME_CTRL_PAGE_SIZE - offset);
594638 if (length <= 0) {
595639 iod->first_dma = 0;
596640 goto done;
597641 }
598642
599
- dma_len -= (page_size - offset);
643
+ dma_len -= (NVME_CTRL_PAGE_SIZE - offset);
600644 if (dma_len) {
601
- dma_addr += (page_size - offset);
645
+ dma_addr += (NVME_CTRL_PAGE_SIZE - offset);
602646 } else {
603647 sg = sg_next(sg);
604648 dma_addr = sg_dma_address(sg);
605649 dma_len = sg_dma_len(sg);
606650 }
607651
608
- if (length <= page_size) {
652
+ if (length <= NVME_CTRL_PAGE_SIZE) {
609653 iod->first_dma = dma_addr;
610654 goto done;
611655 }
612656
613
- nprps = DIV_ROUND_UP(length, page_size);
657
+ nprps = DIV_ROUND_UP(length, NVME_CTRL_PAGE_SIZE);
614658 if (nprps <= (256 / 8)) {
615659 pool = dev->prp_small_pool;
616660 iod->npages = 0;
....@@ -629,20 +673,20 @@
629673 iod->first_dma = prp_dma;
630674 i = 0;
631675 for (;;) {
632
- if (i == page_size >> 3) {
676
+ if (i == NVME_CTRL_PAGE_SIZE >> 3) {
633677 __le64 *old_prp_list = prp_list;
634678 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
635679 if (!prp_list)
636
- return BLK_STS_RESOURCE;
680
+ goto free_prps;
637681 list[iod->npages++] = prp_list;
638682 prp_list[0] = old_prp_list[i - 1];
639683 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
640684 i = 1;
641685 }
642686 prp_list[i++] = cpu_to_le64(dma_addr);
643
- dma_len -= page_size;
644
- dma_addr += page_size;
645
- length -= page_size;
687
+ dma_len -= NVME_CTRL_PAGE_SIZE;
688
+ dma_addr += NVME_CTRL_PAGE_SIZE;
689
+ length -= NVME_CTRL_PAGE_SIZE;
646690 if (length <= 0)
647691 break;
648692 if (dma_len > 0)
....@@ -653,14 +697,14 @@
653697 dma_addr = sg_dma_address(sg);
654698 dma_len = sg_dma_len(sg);
655699 }
656
-
657700 done:
658701 cmnd->dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
659702 cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma);
660
-
661703 return BLK_STS_OK;
662
-
663
- bad_sgl:
704
+free_prps:
705
+ nvme_free_prps(dev, req);
706
+ return BLK_STS_RESOURCE;
707
+bad_sgl:
664708 WARN(DO_ONCE(nvme_print_sgl, iod->sg, iod->nents),
665709 "Invalid SGL for payload:%d nents:%d\n",
666710 blk_rq_payload_bytes(req), iod->nents);
....@@ -683,7 +727,7 @@
683727 sge->length = cpu_to_le32(entries * sizeof(*sge));
684728 sge->type = NVME_SGL_FMT_LAST_SEG_DESC << 4;
685729 } else {
686
- sge->length = cpu_to_le32(PAGE_SIZE);
730
+ sge->length = cpu_to_le32(NVME_CTRL_PAGE_SIZE);
687731 sge->type = NVME_SGL_FMT_SEG_DESC << 4;
688732 }
689733 }
....@@ -732,7 +776,7 @@
732776
733777 sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma);
734778 if (!sg_list)
735
- return BLK_STS_RESOURCE;
779
+ goto free_sgls;
736780
737781 i = 0;
738782 nvme_pci_iod_list(req)[iod->npages++] = sg_list;
....@@ -745,74 +789,117 @@
745789 } while (--entries > 0);
746790
747791 return BLK_STS_OK;
792
+free_sgls:
793
+ nvme_free_sgls(dev, req);
794
+ return BLK_STS_RESOURCE;
795
+}
796
+
797
+static blk_status_t nvme_setup_prp_simple(struct nvme_dev *dev,
798
+ struct request *req, struct nvme_rw_command *cmnd,
799
+ struct bio_vec *bv)
800
+{
801
+ struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
802
+ unsigned int offset = bv->bv_offset & (NVME_CTRL_PAGE_SIZE - 1);
803
+ unsigned int first_prp_len = NVME_CTRL_PAGE_SIZE - offset;
804
+
805
+ iod->first_dma = dma_map_bvec(dev->dev, bv, rq_dma_dir(req), 0);
806
+ if (dma_mapping_error(dev->dev, iod->first_dma))
807
+ return BLK_STS_RESOURCE;
808
+ iod->dma_len = bv->bv_len;
809
+
810
+ cmnd->dptr.prp1 = cpu_to_le64(iod->first_dma);
811
+ if (bv->bv_len > first_prp_len)
812
+ cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma + first_prp_len);
813
+ else
814
+ cmnd->dptr.prp2 = 0;
815
+ return BLK_STS_OK;
816
+}
817
+
818
+static blk_status_t nvme_setup_sgl_simple(struct nvme_dev *dev,
819
+ struct request *req, struct nvme_rw_command *cmnd,
820
+ struct bio_vec *bv)
821
+{
822
+ struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
823
+
824
+ iod->first_dma = dma_map_bvec(dev->dev, bv, rq_dma_dir(req), 0);
825
+ if (dma_mapping_error(dev->dev, iod->first_dma))
826
+ return BLK_STS_RESOURCE;
827
+ iod->dma_len = bv->bv_len;
828
+
829
+ cmnd->flags = NVME_CMD_SGL_METABUF;
830
+ cmnd->dptr.sgl.addr = cpu_to_le64(iod->first_dma);
831
+ cmnd->dptr.sgl.length = cpu_to_le32(iod->dma_len);
832
+ cmnd->dptr.sgl.type = NVME_SGL_FMT_DATA_DESC << 4;
833
+ return BLK_STS_OK;
748834 }
749835
750836 static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,
751837 struct nvme_command *cmnd)
752838 {
753839 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;
840
+ blk_status_t ret = BLK_STS_RESOURCE;
758841 int nr_mapped;
759842
843
+ if (blk_rq_nr_phys_segments(req) == 1) {
844
+ struct bio_vec bv = req_bvec(req);
845
+
846
+ if (!is_pci_p2pdma_page(bv.bv_page)) {
847
+ if (bv.bv_offset + bv.bv_len <= NVME_CTRL_PAGE_SIZE * 2)
848
+ return nvme_setup_prp_simple(dev, req,
849
+ &cmnd->rw, &bv);
850
+
851
+ if (iod->nvmeq->qid && sgl_threshold &&
852
+ dev->ctrl.sgls & ((1 << 0) | (1 << 1)))
853
+ return nvme_setup_sgl_simple(dev, req,
854
+ &cmnd->rw, &bv);
855
+ }
856
+ }
857
+
858
+ iod->dma_len = 0;
859
+ iod->sg = mempool_alloc(dev->iod_mempool, GFP_ATOMIC);
860
+ if (!iod->sg)
861
+ return BLK_STS_RESOURCE;
760862 sg_init_table(iod->sg, blk_rq_nr_phys_segments(req));
761
- iod->nents = blk_rq_map_sg(q, req, iod->sg);
863
+ iod->nents = blk_rq_map_sg(req->q, req, iod->sg);
762864 if (!iod->nents)
763
- goto out;
865
+ goto out_free_sg;
764866
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);
867
+ if (is_pci_p2pdma_page(sg_page(iod->sg)))
868
+ nr_mapped = pci_p2pdma_map_sg_attrs(dev->dev, iod->sg,
869
+ iod->nents, rq_dma_dir(req), DMA_ATTR_NO_WARN);
870
+ else
871
+ nr_mapped = dma_map_sg_attrs(dev->dev, iod->sg, iod->nents,
872
+ rq_dma_dir(req), DMA_ATTR_NO_WARN);
768873 if (!nr_mapped)
769
- goto out;
874
+ goto out_free_sg;
770875
876
+ iod->use_sgl = nvme_pci_use_sgls(dev, req);
771877 if (iod->use_sgl)
772878 ret = nvme_pci_setup_sgls(dev, req, &cmnd->rw, nr_mapped);
773879 else
774880 ret = nvme_pci_setup_prps(dev, req, &cmnd->rw);
775
-
776881 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));
882
+ goto out_unmap_sg;
794883 return BLK_STS_OK;
795884
796
-out_unmap:
797
- dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
798
-out:
885
+out_unmap_sg:
886
+ nvme_unmap_sg(dev, req);
887
+out_free_sg:
888
+ mempool_free(iod->sg, dev->iod_mempool);
799889 return ret;
800890 }
801891
802
-static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
892
+static blk_status_t nvme_map_metadata(struct nvme_dev *dev, struct request *req,
893
+ struct nvme_command *cmnd)
803894 {
804895 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;
807896
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);
897
+ iod->meta_dma = dma_map_bvec(dev->dev, rq_integrity_vec(req),
898
+ rq_dma_dir(req), 0);
899
+ if (dma_mapping_error(dev->dev, iod->meta_dma))
900
+ return BLK_STS_IOERR;
901
+ cmnd->rw.metadata = cpu_to_le64(iod->meta_dma);
902
+ return BLK_STS_OK;
816903 }
817904
818905 /*
....@@ -825,35 +912,42 @@
825912 struct nvme_queue *nvmeq = hctx->driver_data;
826913 struct nvme_dev *dev = nvmeq->dev;
827914 struct request *req = bd->rq;
828
- struct nvme_command cmnd;
915
+ struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
916
+ struct nvme_command *cmnd = &iod->cmd;
829917 blk_status_t ret;
918
+
919
+ iod->aborted = 0;
920
+ iod->npages = -1;
921
+ iod->nents = 0;
830922
831923 /*
832924 * We should not need to do this, but we're still using this to
833925 * ensure we can drain requests on a dying queue.
834926 */
835
- if (unlikely(nvmeq->cq_vector < 0))
927
+ if (unlikely(!test_bit(NVMEQ_ENABLED, &nvmeq->flags)))
836928 return BLK_STS_IOERR;
837929
838
- ret = nvme_setup_cmd(ns, req, &cmnd);
930
+ ret = nvme_setup_cmd(ns, req, cmnd);
839931 if (ret)
840932 return ret;
841933
842
- ret = nvme_init_iod(req, dev);
843
- if (ret)
844
- goto out_free_cmd;
845
-
846934 if (blk_rq_nr_phys_segments(req)) {
847
- ret = nvme_map_data(dev, req, &cmnd);
935
+ ret = nvme_map_data(dev, req, cmnd);
848936 if (ret)
849
- goto out_cleanup_iod;
937
+ goto out_free_cmd;
938
+ }
939
+
940
+ if (blk_integrity_rq(req)) {
941
+ ret = nvme_map_metadata(dev, req, cmnd);
942
+ if (ret)
943
+ goto out_unmap_data;
850944 }
851945
852946 blk_mq_start_request(req);
853
- nvme_submit_cmd(nvmeq, &cmnd);
947
+ nvme_submit_cmd(nvmeq, cmnd, bd->last);
854948 return BLK_STS_OK;
855
-out_cleanup_iod:
856
- nvme_free_iod(dev, req);
949
+out_unmap_data:
950
+ nvme_unmap_data(dev, req);
857951 out_free_cmd:
858952 nvme_cleanup_cmd(req);
859953 return ret;
....@@ -862,16 +956,23 @@
862956 static void nvme_pci_complete_rq(struct request *req)
863957 {
864958 struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
959
+ struct nvme_dev *dev = iod->nvmeq->dev;
865960
866
- nvme_unmap_data(iod->nvmeq->dev, req);
961
+ if (blk_integrity_rq(req))
962
+ dma_unmap_page(dev->dev, iod->meta_dma,
963
+ rq_integrity_vec(req)->bv_len, rq_dma_dir(req));
964
+
965
+ if (blk_rq_nr_phys_segments(req))
966
+ nvme_unmap_data(dev, req);
867967 nvme_complete_rq(req);
868968 }
869969
870970 /* We read the CQE phase first to check if the rest of the entry is valid */
871971 static inline bool nvme_cqe_pending(struct nvme_queue *nvmeq)
872972 {
873
- return (le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
874
- nvmeq->cq_phase;
973
+ struct nvme_completion *hcqe = &nvmeq->cqes[nvmeq->cq_head];
974
+
975
+ return (le16_to_cpu(READ_ONCE(hcqe->status)) & 1) == nvmeq->cq_phase;
875976 }
876977
877978 static inline void nvme_ring_cq_doorbell(struct nvme_queue *nvmeq)
....@@ -883,17 +984,18 @@
883984 writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
884985 }
885986
987
+static inline struct blk_mq_tags *nvme_queue_tagset(struct nvme_queue *nvmeq)
988
+{
989
+ if (!nvmeq->qid)
990
+ return nvmeq->dev->admin_tagset.tags[0];
991
+ return nvmeq->dev->tagset.tags[nvmeq->qid - 1];
992
+}
993
+
886994 static inline void nvme_handle_cqe(struct nvme_queue *nvmeq, u16 idx)
887995 {
888
- volatile struct nvme_completion *cqe = &nvmeq->cqes[idx];
996
+ struct nvme_completion *cqe = &nvmeq->cqes[idx];
997
+ __u16 command_id = READ_ONCE(cqe->command_id);
889998 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
- }
897999
8981000 /*
8991001 * AEN requests are special as they don't time out and can
....@@ -901,50 +1003,53 @@
9011003 * aborts. We don't even bother to allocate a struct request
9021004 * for them but rather special case them here.
9031005 */
904
- if (unlikely(nvmeq->qid == 0 &&
905
- cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH)) {
1006
+ if (unlikely(nvme_is_aen_req(nvmeq->qid, command_id))) {
9061007 nvme_complete_async_event(&nvmeq->dev->ctrl,
9071008 cqe->status, &cqe->result);
9081009 return;
9091010 }
9101011
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;
1012
+ req = nvme_find_rq(nvme_queue_tagset(nvmeq), command_id);
1013
+ if (unlikely(!req)) {
1014
+ dev_warn(nvmeq->dev->ctrl.device,
1015
+ "invalid id %d completed on queue %d\n",
1016
+ command_id, le16_to_cpu(cqe->sq_id));
1017
+ return;
9211018 }
1019
+
1020
+ trace_nvme_sq(req, cqe->sq_head, nvmeq->sq_tail);
1021
+ if (!nvme_try_complete_req(req, cqe->status, cqe->result))
1022
+ nvme_pci_complete_rq(req);
9221023 }
9231024
9241025 static inline void nvme_update_cq_head(struct nvme_queue *nvmeq)
9251026 {
926
- if (nvmeq->cq_head == nvmeq->q_depth - 1) {
1027
+ u32 tmp = nvmeq->cq_head + 1;
1028
+
1029
+ if (tmp == nvmeq->q_depth) {
9271030 nvmeq->cq_head = 0;
928
- nvmeq->cq_phase = !nvmeq->cq_phase;
1031
+ nvmeq->cq_phase ^= 1;
9291032 } else {
930
- nvmeq->cq_head++;
1033
+ nvmeq->cq_head = tmp;
9311034 }
9321035 }
9331036
934
-static inline bool nvme_process_cq(struct nvme_queue *nvmeq, u16 *start,
935
- u16 *end, int tag)
1037
+static inline int nvme_process_cq(struct nvme_queue *nvmeq)
9361038 {
937
- bool found = false;
1039
+ int found = 0;
9381040
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;
1041
+ while (nvme_cqe_pending(nvmeq)) {
1042
+ found++;
1043
+ /*
1044
+ * load-load control dependency between phase and the rest of
1045
+ * the cqe requires a full read memory barrier
1046
+ */
1047
+ dma_rmb();
1048
+ nvme_handle_cqe(nvmeq, nvmeq->cq_head);
9431049 nvme_update_cq_head(nvmeq);
9441050 }
945
- *end = nvmeq->cq_head;
9461051
947
- if (*start != *end)
1052
+ if (found)
9481053 nvme_ring_cq_doorbell(nvmeq);
9491054 return found;
9501055 }
....@@ -953,19 +1058,15 @@
9531058 {
9541059 struct nvme_queue *nvmeq = data;
9551060 irqreturn_t ret = IRQ_NONE;
956
- u16 start, end;
9571061
958
- spin_lock(&nvmeq->cq_lock);
959
- if (nvmeq->cq_head != nvmeq->last_cq_head)
1062
+ /*
1063
+ * The rmb/wmb pair ensures we see all updates from a previous run of
1064
+ * the irq handler, even if that was on another CPU.
1065
+ */
1066
+ rmb();
1067
+ if (nvme_process_cq(nvmeq))
9601068 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
- }
1069
+ wmb();
9691070
9701071 return ret;
9711072 }
....@@ -973,32 +1074,40 @@
9731074 static irqreturn_t nvme_irq_check(int irq, void *data)
9741075 {
9751076 struct nvme_queue *nvmeq = data;
1077
+
9761078 if (nvme_cqe_pending(nvmeq))
9771079 return IRQ_WAKE_THREAD;
9781080 return IRQ_NONE;
9791081 }
9801082
981
-static int __nvme_poll(struct nvme_queue *nvmeq, unsigned int tag)
1083
+/*
1084
+ * Poll for completions for any interrupt driven queue
1085
+ * Can be called from any context.
1086
+ */
1087
+static void nvme_poll_irqdisable(struct nvme_queue *nvmeq)
9821088 {
983
- u16 start, end;
1089
+ struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev);
1090
+
1091
+ WARN_ON_ONCE(test_bit(NVMEQ_POLLED, &nvmeq->flags));
1092
+
1093
+ disable_irq(pci_irq_vector(pdev, nvmeq->cq_vector));
1094
+ nvme_process_cq(nvmeq);
1095
+ enable_irq(pci_irq_vector(pdev, nvmeq->cq_vector));
1096
+}
1097
+
1098
+static int nvme_poll(struct blk_mq_hw_ctx *hctx)
1099
+{
1100
+ struct nvme_queue *nvmeq = hctx->driver_data;
9841101 bool found;
9851102
9861103 if (!nvme_cqe_pending(nvmeq))
9871104 return 0;
9881105
989
- spin_lock_irq(&nvmeq->cq_lock);
990
- found = nvme_process_cq(nvmeq, &start, &end, tag);
991
- spin_unlock_irq(&nvmeq->cq_lock);
1106
+ spin_lock(&nvmeq->cq_poll_lock);
1107
+ found = nvme_process_cq(nvmeq);
1108
+ spin_unlock(&nvmeq->cq_poll_lock);
9921109
993
- nvme_complete_cqes(nvmeq, start, end);
9941110 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);
10021111 }
10031112
10041113 static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl)
....@@ -1010,7 +1119,7 @@
10101119 memset(&c, 0, sizeof(c));
10111120 c.common.opcode = nvme_admin_async_event;
10121121 c.common.command_id = NVME_AQ_BLK_MQ_DEPTH;
1013
- nvme_submit_cmd(nvmeq, &c);
1122
+ nvme_submit_cmd(nvmeq, &c, true);
10141123 }
10151124
10161125 static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
....@@ -1028,7 +1137,10 @@
10281137 struct nvme_queue *nvmeq, s16 vector)
10291138 {
10301139 struct nvme_command c;
1031
- int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
1140
+ int flags = NVME_QUEUE_PHYS_CONTIG;
1141
+
1142
+ if (!test_bit(NVMEQ_POLLED, &nvmeq->flags))
1143
+ flags |= NVME_CQ_IRQ_ENABLED;
10321144
10331145 /*
10341146 * Note: we (ab)use the fact that the prp fields survive if no data
....@@ -1098,7 +1210,6 @@
10981210
10991211 static bool nvme_should_reset(struct nvme_dev *dev, u32 csts)
11001212 {
1101
-
11021213 /* If true, indicates loss of adapter communication, possibly by a
11031214 * NVMe Subsystem reset.
11041215 */
....@@ -1147,7 +1258,6 @@
11471258 struct nvme_dev *dev = nvmeq->dev;
11481259 struct request *abort_req;
11491260 struct nvme_command cmd;
1150
- bool shutdown = false;
11511261 u32 csts = readl(dev->bar + NVME_REG_CSTS);
11521262
11531263 /* If PCI error recovery process is happening, we cannot reset or
....@@ -1170,7 +1280,12 @@
11701280 /*
11711281 * Did we miss an interrupt?
11721282 */
1173
- if (__nvme_poll(nvmeq, req->tag)) {
1283
+ if (test_bit(NVMEQ_POLLED, &nvmeq->flags))
1284
+ nvme_poll(req->mq_hctx);
1285
+ else
1286
+ nvme_poll_irqdisable(nvmeq);
1287
+
1288
+ if (blk_mq_rq_state(req) != MQ_RQ_IN_FLIGHT) {
11741289 dev_warn(dev->ctrl.device,
11751290 "I/O %d QID %d timeout, completion polled\n",
11761291 req->tag, nvmeq->qid);
....@@ -1184,33 +1299,35 @@
11841299 * shutdown, so we return BLK_EH_DONE.
11851300 */
11861301 switch (dev->ctrl.state) {
1187
- case NVME_CTRL_DELETING:
1188
- shutdown = true;
11891302 case NVME_CTRL_CONNECTING:
1190
- case NVME_CTRL_RESETTING:
1303
+ nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
1304
+ fallthrough;
1305
+ case NVME_CTRL_DELETING:
11911306 dev_warn_ratelimited(dev->ctrl.device,
11921307 "I/O %d QID %d timeout, disable controller\n",
11931308 req->tag, nvmeq->qid);
1194
- nvme_dev_disable(dev, shutdown);
11951309 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
1310
+ nvme_dev_disable(dev, true);
11961311 return BLK_EH_DONE;
1312
+ case NVME_CTRL_RESETTING:
1313
+ return BLK_EH_RESET_TIMER;
11971314 default:
11981315 break;
11991316 }
12001317
12011318 /*
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.
1319
+ * Shutdown the controller immediately and schedule a reset if the
1320
+ * command was already aborted once before and still hasn't been
1321
+ * returned to the driver, or if this is the admin queue.
12051322 */
12061323 if (!nvmeq->qid || iod->aborted) {
12071324 dev_warn(dev->ctrl.device,
12081325 "I/O %d QID %d timeout, reset controller\n",
12091326 req->tag, nvmeq->qid);
1327
+ nvme_req(req)->flags |= NVME_REQ_CANCELLED;
12101328 nvme_dev_disable(dev, false);
12111329 nvme_reset_ctrl(&dev->ctrl);
12121330
1213
- nvme_req(req)->flags |= NVME_REQ_CANCELLED;
12141331 return BLK_EH_DONE;
12151332 }
12161333
....@@ -1222,7 +1339,7 @@
12221339
12231340 memset(&cmd, 0, sizeof(cmd));
12241341 cmd.abort.opcode = nvme_admin_abort_cmd;
1225
- cmd.abort.cid = req->tag;
1342
+ cmd.abort.cid = nvme_cid(req);
12261343 cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
12271344
12281345 dev_warn(nvmeq->dev->ctrl.device,
....@@ -1230,13 +1347,12 @@
12301347 req->tag, nvmeq->qid);
12311348
12321349 abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd,
1233
- BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
1350
+ BLK_MQ_REQ_NOWAIT);
12341351 if (IS_ERR(abort_req)) {
12351352 atomic_inc(&dev->ctrl.abort_limit);
12361353 return BLK_EH_RESET_TIMER;
12371354 }
12381355
1239
- abort_req->timeout = ADMIN_TIMEOUT;
12401356 abort_req->end_io_data = NULL;
12411357 blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio);
12421358
....@@ -1250,11 +1366,18 @@
12501366
12511367 static void nvme_free_queue(struct nvme_queue *nvmeq)
12521368 {
1253
- dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
1369
+ dma_free_coherent(nvmeq->dev->dev, CQ_SIZE(nvmeq),
12541370 (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);
1371
+ if (!nvmeq->sq_cmds)
1372
+ return;
1373
+
1374
+ if (test_and_clear_bit(NVMEQ_SQ_CMB, &nvmeq->flags)) {
1375
+ pci_free_p2pmem(to_pci_dev(nvmeq->dev->dev),
1376
+ nvmeq->sq_cmds, SQ_SIZE(nvmeq));
1377
+ } else {
1378
+ dma_free_coherent(nvmeq->dev->dev, SQ_SIZE(nvmeq),
1379
+ nvmeq->sq_cmds, nvmeq->sq_dma_addr);
1380
+ }
12581381 }
12591382
12601383 static void nvme_free_queues(struct nvme_dev *dev, int lowest)
....@@ -1269,51 +1392,59 @@
12691392
12701393 /**
12711394 * nvme_suspend_queue - put queue into suspended state
1272
- * @nvmeq - queue to suspend
1395
+ * @nvmeq: queue to suspend
12731396 */
12741397 static int nvme_suspend_queue(struct nvme_queue *nvmeq)
12751398 {
1276
- int vector;
1277
-
1278
- spin_lock_irq(&nvmeq->cq_lock);
1279
- if (nvmeq->cq_vector == -1) {
1280
- spin_unlock_irq(&nvmeq->cq_lock);
1399
+ if (!test_and_clear_bit(NVMEQ_ENABLED, &nvmeq->flags))
12811400 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);
12871401
1288
- /*
1289
- * Ensure that nvme_queue_rq() sees it ->cq_vector == -1 without
1290
- * having to grab the lock.
1291
- */
1402
+ /* ensure that nvme_queue_rq() sees NVMEQ_ENABLED cleared */
12921403 mb();
12931404
1405
+ nvmeq->dev->online_queues--;
12941406 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
12951407 blk_mq_quiesce_queue(nvmeq->dev->ctrl.admin_q);
1296
-
1297
- pci_free_irq(to_pci_dev(nvmeq->dev->dev), vector, nvmeq);
1298
-
1408
+ if (!test_and_clear_bit(NVMEQ_POLLED, &nvmeq->flags))
1409
+ pci_free_irq(to_pci_dev(nvmeq->dev->dev), nvmeq->cq_vector, nvmeq);
12991410 return 0;
1411
+}
1412
+
1413
+static void nvme_suspend_io_queues(struct nvme_dev *dev)
1414
+{
1415
+ int i;
1416
+
1417
+ for (i = dev->ctrl.queue_count - 1; i > 0; i--)
1418
+ nvme_suspend_queue(&dev->queues[i]);
13001419 }
13011420
13021421 static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown)
13031422 {
13041423 struct nvme_queue *nvmeq = &dev->queues[0];
1305
- u16 start, end;
13061424
13071425 if (shutdown)
13081426 nvme_shutdown_ctrl(&dev->ctrl);
13091427 else
1310
- nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
1428
+ nvme_disable_ctrl(&dev->ctrl);
13111429
1312
- spin_lock_irq(&nvmeq->cq_lock);
1313
- nvme_process_cq(nvmeq, &start, &end, -1);
1314
- spin_unlock_irq(&nvmeq->cq_lock);
1430
+ nvme_poll_irqdisable(nvmeq);
1431
+}
13151432
1316
- nvme_complete_cqes(nvmeq, start, end);
1433
+/*
1434
+ * Called only on a device that has been disabled and after all other threads
1435
+ * that can check this device's completion queues have synced, except
1436
+ * nvme_poll(). This is the last chance for the driver to see a natural
1437
+ * completion before nvme_cancel_request() terminates all incomplete requests.
1438
+ */
1439
+static void nvme_reap_pending_cqes(struct nvme_dev *dev)
1440
+{
1441
+ int i;
1442
+
1443
+ for (i = dev->ctrl.queue_count - 1; i > 0; i--) {
1444
+ spin_lock(&dev->queues[i].cq_poll_lock);
1445
+ nvme_process_cq(&dev->queues[i]);
1446
+ spin_unlock(&dev->queues[i].cq_poll_lock);
1447
+ }
13171448 }
13181449
13191450 static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
....@@ -1321,11 +1452,12 @@
13211452 {
13221453 int q_depth = dev->q_depth;
13231454 unsigned q_size_aligned = roundup(q_depth * entry_size,
1324
- dev->ctrl.page_size);
1455
+ NVME_CTRL_PAGE_SIZE);
13251456
13261457 if (q_size_aligned * nr_io_queues > dev->cmb_size) {
13271458 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);
1459
+
1460
+ mem_per_q = round_down(mem_per_q, NVME_CTRL_PAGE_SIZE);
13291461 q_depth = div_u64(mem_per_q, entry_size);
13301462
13311463 /*
....@@ -1341,14 +1473,26 @@
13411473 }
13421474
13431475 static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
1344
- int qid, int depth)
1476
+ int qid)
13451477 {
1346
- /* CMB SQEs will be mapped before creation */
1347
- if (qid && dev->cmb && use_cmb_sqes && (dev->cmbsz & NVME_CMBSZ_SQS))
1348
- return 0;
1478
+ struct pci_dev *pdev = to_pci_dev(dev->dev);
13491479
1350
- nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
1351
- &nvmeq->sq_dma_addr, GFP_KERNEL);
1480
+ if (qid && dev->cmb_use_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) {
1481
+ nvmeq->sq_cmds = pci_alloc_p2pmem(pdev, SQ_SIZE(nvmeq));
1482
+ if (nvmeq->sq_cmds) {
1483
+ nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
1484
+ nvmeq->sq_cmds);
1485
+ if (nvmeq->sq_dma_addr) {
1486
+ set_bit(NVMEQ_SQ_CMB, &nvmeq->flags);
1487
+ return 0;
1488
+ }
1489
+
1490
+ pci_free_p2pmem(pdev, nvmeq->sq_cmds, SQ_SIZE(nvmeq));
1491
+ }
1492
+ }
1493
+
1494
+ nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(nvmeq),
1495
+ &nvmeq->sq_dma_addr, GFP_KERNEL);
13521496 if (!nvmeq->sq_cmds)
13531497 return -ENOMEM;
13541498 return 0;
....@@ -1361,31 +1505,30 @@
13611505 if (dev->ctrl.queue_count > qid)
13621506 return 0;
13631507
1364
- nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
1365
- &nvmeq->cq_dma_addr, GFP_KERNEL);
1508
+ nvmeq->sqes = qid ? dev->io_sqes : NVME_ADM_SQES;
1509
+ nvmeq->q_depth = depth;
1510
+ nvmeq->cqes = dma_alloc_coherent(dev->dev, CQ_SIZE(nvmeq),
1511
+ &nvmeq->cq_dma_addr, GFP_KERNEL);
13661512 if (!nvmeq->cqes)
13671513 goto free_nvmeq;
13681514
1369
- if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
1515
+ if (nvme_alloc_sq_cmds(dev, nvmeq, qid))
13701516 goto free_cqdma;
13711517
1372
- nvmeq->q_dmadev = dev->dev;
13731518 nvmeq->dev = dev;
13741519 spin_lock_init(&nvmeq->sq_lock);
1375
- spin_lock_init(&nvmeq->cq_lock);
1520
+ spin_lock_init(&nvmeq->cq_poll_lock);
13761521 nvmeq->cq_head = 0;
13771522 nvmeq->cq_phase = 1;
13781523 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
1379
- nvmeq->q_depth = depth;
13801524 nvmeq->qid = qid;
1381
- nvmeq->cq_vector = -1;
13821525 dev->ctrl.queue_count++;
13831526
13841527 return 0;
13851528
13861529 free_cqdma:
1387
- dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
1388
- nvmeq->cq_dma_addr);
1530
+ dma_free_coherent(dev->dev, CQ_SIZE(nvmeq), (void *)nvmeq->cqes,
1531
+ nvmeq->cq_dma_addr);
13891532 free_nvmeq:
13901533 return -ENOMEM;
13911534 }
....@@ -1408,35 +1551,34 @@
14081551 {
14091552 struct nvme_dev *dev = nvmeq->dev;
14101553
1411
- spin_lock_irq(&nvmeq->cq_lock);
14121554 nvmeq->sq_tail = 0;
1555
+ nvmeq->last_sq_tail = 0;
14131556 nvmeq->cq_head = 0;
14141557 nvmeq->cq_phase = 1;
14151558 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
1416
- memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
1559
+ memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq));
14171560 nvme_dbbuf_init(dev, nvmeq, qid);
14181561 dev->online_queues++;
1419
- spin_unlock_irq(&nvmeq->cq_lock);
1562
+ wmb(); /* ensure the first interrupt sees the initialization */
14201563 }
14211564
1422
-static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
1565
+static int nvme_create_queue(struct nvme_queue *nvmeq, int qid, bool polled)
14231566 {
14241567 struct nvme_dev *dev = nvmeq->dev;
14251568 int result;
1426
- s16 vector;
1569
+ u16 vector = 0;
14271570
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
- }
1571
+ clear_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags);
14341572
14351573 /*
14361574 * A queue's vector matches the queue identifier unless the controller
14371575 * has only one vector available.
14381576 */
1439
- vector = dev->num_vecs == 1 ? 0 : qid;
1577
+ if (!polled)
1578
+ vector = dev->num_vecs == 1 ? 0 : qid;
1579
+ else
1580
+ set_bit(NVMEQ_POLLED, &nvmeq->flags);
1581
+
14401582 result = adapter_alloc_cq(dev, qid, nvmeq, vector);
14411583 if (result)
14421584 return result;
....@@ -1444,24 +1586,22 @@
14441586 result = adapter_alloc_sq(dev, qid, nvmeq);
14451587 if (result < 0)
14461588 return result;
1447
- else if (result)
1589
+ if (result)
14481590 goto release_cq;
14491591
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
- */
14551592 nvmeq->cq_vector = vector;
14561593 nvme_init_queue(nvmeq, qid);
1457
- result = queue_request_irq(nvmeq);
1458
- if (result < 0)
1459
- goto release_sq;
14601594
1595
+ if (!polled) {
1596
+ result = queue_request_irq(nvmeq);
1597
+ if (result < 0)
1598
+ goto release_sq;
1599
+ }
1600
+
1601
+ set_bit(NVMEQ_ENABLED, &nvmeq->flags);
14611602 return result;
14621603
14631604 release_sq:
1464
- nvmeq->cq_vector = -1;
14651605 dev->online_queues--;
14661606 adapter_delete_sq(dev, qid);
14671607 release_cq:
....@@ -1473,7 +1613,6 @@
14731613 .queue_rq = nvme_queue_rq,
14741614 .complete = nvme_pci_complete_rq,
14751615 .init_hctx = nvme_admin_init_hctx,
1476
- .exit_hctx = nvme_admin_exit_hctx,
14771616 .init_request = nvme_init_request,
14781617 .timeout = nvme_timeout,
14791618 };
....@@ -1481,6 +1620,7 @@
14811620 static const struct blk_mq_ops nvme_mq_ops = {
14821621 .queue_rq = nvme_queue_rq,
14831622 .complete = nvme_pci_complete_rq,
1623
+ .commit_rqs = nvme_commit_rqs,
14841624 .init_hctx = nvme_init_hctx,
14851625 .init_request = nvme_init_request,
14861626 .map_queues = nvme_pci_map_queues,
....@@ -1510,8 +1650,8 @@
15101650
15111651 dev->admin_tagset.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
15121652 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);
1653
+ dev->admin_tagset.numa_node = dev->ctrl.numa_node;
1654
+ dev->admin_tagset.cmd_size = sizeof(struct nvme_iod);
15151655 dev->admin_tagset.flags = BLK_MQ_F_NO_SCHED;
15161656 dev->admin_tagset.driver_data = dev;
15171657
....@@ -1522,6 +1662,7 @@
15221662 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
15231663 if (IS_ERR(dev->ctrl.admin_q)) {
15241664 blk_mq_free_tag_set(&dev->admin_tagset);
1665
+ dev->ctrl.admin_q = NULL;
15251666 return -ENOMEM;
15261667 }
15271668 if (!blk_get_queue(dev->ctrl.admin_q)) {
....@@ -1578,13 +1719,15 @@
15781719 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
15791720 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
15801721
1581
- result = nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
1722
+ result = nvme_disable_ctrl(&dev->ctrl);
15821723 if (result < 0)
15831724 return result;
15841725
15851726 result = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
15861727 if (result)
15871728 return result;
1729
+
1730
+ dev->ctrl.numa_node = dev_to_node(dev->dev);
15881731
15891732 nvmeq = &dev->queues[0];
15901733 aqa = nvmeq->q_depth - 1;
....@@ -1594,7 +1737,7 @@
15941737 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
15951738 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
15961739
1597
- result = nvme_enable_ctrl(&dev->ctrl, dev->ctrl.cap);
1740
+ result = nvme_enable_ctrl(&dev->ctrl);
15981741 if (result)
15991742 return result;
16001743
....@@ -1602,16 +1745,17 @@
16021745 nvme_init_queue(nvmeq, 0);
16031746 result = queue_request_irq(nvmeq);
16041747 if (result) {
1605
- nvmeq->cq_vector = -1;
1748
+ dev->online_queues--;
16061749 return result;
16071750 }
16081751
1752
+ set_bit(NVMEQ_ENABLED, &nvmeq->flags);
16091753 return result;
16101754 }
16111755
16121756 static int nvme_create_io_queues(struct nvme_dev *dev)
16131757 {
1614
- unsigned i, max;
1758
+ unsigned i, max, rw_queues;
16151759 int ret = 0;
16161760
16171761 for (i = dev->ctrl.queue_count; i <= dev->max_qid; i++) {
....@@ -1622,8 +1766,17 @@
16221766 }
16231767
16241768 max = min(dev->max_qid, dev->ctrl.queue_count - 1);
1769
+ if (max != 1 && dev->io_queues[HCTX_TYPE_POLL]) {
1770
+ rw_queues = dev->io_queues[HCTX_TYPE_DEFAULT] +
1771
+ dev->io_queues[HCTX_TYPE_READ];
1772
+ } else {
1773
+ rw_queues = max;
1774
+ }
1775
+
16251776 for (i = dev->online_queues; i <= max; i++) {
1626
- ret = nvme_create_queue(&dev->queues[i], i);
1777
+ bool polled = i > rw_queues;
1778
+
1779
+ ret = nvme_create_queue(&dev->queues[i], i, polled);
16271780 if (ret)
16281781 break;
16291782 }
....@@ -1670,13 +1823,13 @@
16701823 if (dev->cmb_size)
16711824 return;
16721825
1826
+ if (NVME_CAP_CMBS(dev->ctrl.cap))
1827
+ writel(NVME_CMBMSC_CRE, dev->bar + NVME_REG_CMBMSC);
1828
+
16731829 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
16741830 if (!dev->cmbsz)
16751831 return;
16761832 dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
1677
-
1678
- if (!use_cmb_sqes)
1679
- return;
16801833
16811834 size = nvme_cmb_size_unit(dev) * nvme_cmb_size(dev);
16821835 offset = nvme_cmb_size_unit(dev) * NVME_CMB_OFST(dev->cmbloc);
....@@ -1687,6 +1840,16 @@
16871840 return;
16881841
16891842 /*
1843
+ * Tell the controller about the host side address mapping the CMB,
1844
+ * and enable CMB decoding for the NVMe 1.4+ scheme:
1845
+ */
1846
+ if (NVME_CAP_CMBS(dev->ctrl.cap)) {
1847
+ hi_lo_writeq(NVME_CMBMSC_CRE | NVME_CMBMSC_CMSE |
1848
+ (pci_bus_address(pdev, bar) + offset),
1849
+ dev->bar + NVME_REG_CMBMSC);
1850
+ }
1851
+
1852
+ /*
16901853 * Controllers may support a CMB size larger than their BAR,
16911854 * for example, due to being behind a bridge. Reduce the CMB to
16921855 * the reported size of the BAR
....@@ -1694,11 +1857,18 @@
16941857 if (size > bar_size - offset)
16951858 size = bar_size - offset;
16961859
1697
- dev->cmb = ioremap_wc(pci_resource_start(pdev, bar) + offset, size);
1698
- if (!dev->cmb)
1860
+ if (pci_p2pdma_add_resource(pdev, bar, size, offset)) {
1861
+ dev_warn(dev->ctrl.device,
1862
+ "failed to register the CMB\n");
16991863 return;
1700
- dev->cmb_bus_addr = pci_bus_address(pdev, bar) + offset;
1864
+ }
1865
+
17011866 dev->cmb_size = size;
1867
+ dev->cmb_use_sqes = use_cmb_sqes && (dev->cmbsz & NVME_CMBSZ_SQS);
1868
+
1869
+ if ((dev->cmbsz & (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS)) ==
1870
+ (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS))
1871
+ pci_p2pmem_publish(pdev, true);
17021872
17031873 if (sysfs_add_file_to_group(&dev->ctrl.device->kobj,
17041874 &dev_attr_cmb.attr, NULL))
....@@ -1708,17 +1878,16 @@
17081878
17091879 static inline void nvme_release_cmb(struct nvme_dev *dev)
17101880 {
1711
- if (dev->cmb) {
1712
- iounmap(dev->cmb);
1713
- dev->cmb = NULL;
1881
+ if (dev->cmb_size) {
17141882 sysfs_remove_file_from_group(&dev->ctrl.device->kobj,
17151883 &dev_attr_cmb.attr, NULL);
1716
- dev->cmbsz = 0;
1884
+ dev->cmb_size = 0;
17171885 }
17181886 }
17191887
17201888 static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits)
17211889 {
1890
+ u32 host_mem_size = dev->host_mem_size >> NVME_CTRL_PAGE_SHIFT;
17221891 u64 dma_addr = dev->host_mem_descs_dma;
17231892 struct nvme_command c;
17241893 int ret;
....@@ -1727,8 +1896,7 @@
17271896 c.features.opcode = nvme_admin_set_features;
17281897 c.features.fid = cpu_to_le32(NVME_FEAT_HOST_MEM_BUF);
17291898 c.features.dword11 = cpu_to_le32(bits);
1730
- c.features.dword12 = cpu_to_le32(dev->host_mem_size >>
1731
- ilog2(dev->ctrl.page_size));
1899
+ c.features.dword12 = cpu_to_le32(host_mem_size);
17321900 c.features.dword13 = cpu_to_le32(lower_32_bits(dma_addr));
17331901 c.features.dword14 = cpu_to_le32(upper_32_bits(dma_addr));
17341902 c.features.dword15 = cpu_to_le32(dev->nr_host_mem_descs);
....@@ -1748,7 +1916,7 @@
17481916
17491917 for (i = 0; i < dev->nr_host_mem_descs; i++) {
17501918 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;
1919
+ size_t size = le32_to_cpu(desc->size) * NVME_CTRL_PAGE_SIZE;
17521920
17531921 dma_free_attrs(dev->dev, size, dev->host_mem_desc_bufs[i],
17541922 le64_to_cpu(desc->addr),
....@@ -1781,8 +1949,8 @@
17811949 if (dev->ctrl.hmmaxd && dev->ctrl.hmmaxd < max_entries)
17821950 max_entries = dev->ctrl.hmmaxd;
17831951
1784
- descs = dma_zalloc_coherent(dev->dev, max_entries * sizeof(*descs),
1785
- &descs_dma, GFP_KERNEL);
1952
+ descs = dma_alloc_coherent(dev->dev, max_entries * sizeof(*descs),
1953
+ &descs_dma, GFP_KERNEL);
17861954 if (!descs)
17871955 goto out;
17881956
....@@ -1800,7 +1968,7 @@
18001968 break;
18011969
18021970 descs[i].addr = cpu_to_le64(dma_addr);
1803
- descs[i].size = cpu_to_le32(len / dev->ctrl.page_size);
1971
+ descs[i].size = cpu_to_le32(len / NVME_CTRL_PAGE_SIZE);
18041972 i++;
18051973 }
18061974
....@@ -1816,7 +1984,7 @@
18161984
18171985 out_free_bufs:
18181986 while (--i >= 0) {
1819
- size_t size = le32_to_cpu(descs[i].size) * dev->ctrl.page_size;
1987
+ size_t size = le32_to_cpu(descs[i].size) * NVME_CTRL_PAGE_SIZE;
18201988
18211989 dma_free_attrs(dev->dev, size, bufs[i],
18221990 le64_to_cpu(descs[i].addr),
....@@ -1834,12 +2002,12 @@
18342002
18352003 static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
18362004 {
1837
- u32 chunk_size;
2005
+ u64 min_chunk = min_t(u64, preferred, PAGE_SIZE * MAX_ORDER_NR_PAGES);
2006
+ u64 hmminds = max_t(u32, dev->ctrl.hmminds * 4096, PAGE_SIZE * 2);
2007
+ u64 chunk_size;
18382008
18392009 /* 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) {
2010
+ for (chunk_size = min_chunk; chunk_size >= hmminds; chunk_size /= 2) {
18432011 if (!__nvme_alloc_host_mem(dev, preferred, chunk_size)) {
18442012 if (!min || dev->host_mem_size >= min)
18452013 return 0;
....@@ -1895,32 +2063,132 @@
18952063 return ret;
18962064 }
18972065
2066
+/*
2067
+ * nirqs is the number of interrupts available for write and read
2068
+ * queues. The core already reserved an interrupt for the admin queue.
2069
+ */
2070
+static void nvme_calc_irq_sets(struct irq_affinity *affd, unsigned int nrirqs)
2071
+{
2072
+ struct nvme_dev *dev = affd->priv;
2073
+ unsigned int nr_read_queues, nr_write_queues = dev->nr_write_queues;
2074
+
2075
+ /*
2076
+ * If there is no interrupt available for queues, ensure that
2077
+ * the default queue is set to 1. The affinity set size is
2078
+ * also set to one, but the irq core ignores it for this case.
2079
+ *
2080
+ * If only one interrupt is available or 'write_queue' == 0, combine
2081
+ * write and read queues.
2082
+ *
2083
+ * If 'write_queues' > 0, ensure it leaves room for at least one read
2084
+ * queue.
2085
+ */
2086
+ if (!nrirqs) {
2087
+ nrirqs = 1;
2088
+ nr_read_queues = 0;
2089
+ } else if (nrirqs == 1 || !nr_write_queues) {
2090
+ nr_read_queues = 0;
2091
+ } else if (nr_write_queues >= nrirqs) {
2092
+ nr_read_queues = 1;
2093
+ } else {
2094
+ nr_read_queues = nrirqs - nr_write_queues;
2095
+ }
2096
+
2097
+ dev->io_queues[HCTX_TYPE_DEFAULT] = nrirqs - nr_read_queues;
2098
+ affd->set_size[HCTX_TYPE_DEFAULT] = nrirqs - nr_read_queues;
2099
+ dev->io_queues[HCTX_TYPE_READ] = nr_read_queues;
2100
+ affd->set_size[HCTX_TYPE_READ] = nr_read_queues;
2101
+ affd->nr_sets = nr_read_queues ? 2 : 1;
2102
+}
2103
+
2104
+static int nvme_setup_irqs(struct nvme_dev *dev, unsigned int nr_io_queues)
2105
+{
2106
+ struct pci_dev *pdev = to_pci_dev(dev->dev);
2107
+ struct irq_affinity affd = {
2108
+ .pre_vectors = 1,
2109
+ .calc_sets = nvme_calc_irq_sets,
2110
+ .priv = dev,
2111
+ };
2112
+ unsigned int irq_queues, poll_queues;
2113
+
2114
+ /*
2115
+ * Poll queues don't need interrupts, but we need at least one I/O queue
2116
+ * left over for non-polled I/O.
2117
+ */
2118
+ poll_queues = min(dev->nr_poll_queues, nr_io_queues - 1);
2119
+ dev->io_queues[HCTX_TYPE_POLL] = poll_queues;
2120
+
2121
+ /*
2122
+ * Initialize for the single interrupt case, will be updated in
2123
+ * nvme_calc_irq_sets().
2124
+ */
2125
+ dev->io_queues[HCTX_TYPE_DEFAULT] = 1;
2126
+ dev->io_queues[HCTX_TYPE_READ] = 0;
2127
+
2128
+ /*
2129
+ * We need interrupts for the admin queue and each non-polled I/O queue,
2130
+ * but some Apple controllers require all queues to use the first
2131
+ * vector.
2132
+ */
2133
+ irq_queues = 1;
2134
+ if (!(dev->ctrl.quirks & NVME_QUIRK_SINGLE_VECTOR))
2135
+ irq_queues += (nr_io_queues - poll_queues);
2136
+ return pci_alloc_irq_vectors_affinity(pdev, 1, irq_queues,
2137
+ PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY, &affd);
2138
+}
2139
+
2140
+static void nvme_disable_io_queues(struct nvme_dev *dev)
2141
+{
2142
+ if (__nvme_disable_io_queues(dev, nvme_admin_delete_sq))
2143
+ __nvme_disable_io_queues(dev, nvme_admin_delete_cq);
2144
+}
2145
+
2146
+static unsigned int nvme_max_io_queues(struct nvme_dev *dev)
2147
+{
2148
+ return num_possible_cpus() + dev->nr_write_queues + dev->nr_poll_queues;
2149
+}
2150
+
18982151 static int nvme_setup_io_queues(struct nvme_dev *dev)
18992152 {
19002153 struct nvme_queue *adminq = &dev->queues[0];
19012154 struct pci_dev *pdev = to_pci_dev(dev->dev);
1902
- int result, nr_io_queues;
2155
+ unsigned int nr_io_queues;
19032156 unsigned long size;
2157
+ int result;
19042158
1905
- struct irq_affinity affd = {
1906
- .pre_vectors = 1
1907
- };
2159
+ /*
2160
+ * Sample the module parameters once at reset time so that we have
2161
+ * stable values to work with.
2162
+ */
2163
+ dev->nr_write_queues = write_queues;
2164
+ dev->nr_poll_queues = poll_queues;
19082165
1909
- nr_io_queues = num_possible_cpus();
2166
+ /*
2167
+ * If tags are shared with admin queue (Apple bug), then
2168
+ * make sure we only use one IO queue.
2169
+ */
2170
+ if (dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS)
2171
+ nr_io_queues = 1;
2172
+ else
2173
+ nr_io_queues = min(nvme_max_io_queues(dev),
2174
+ dev->nr_allocated_queues - 1);
2175
+
19102176 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
19112177 if (result < 0)
19122178 return result;
19132179
19142180 if (nr_io_queues == 0)
19152181 return 0;
2182
+
2183
+ clear_bit(NVMEQ_ENABLED, &adminq->flags);
19162184
1917
- if (dev->cmb && (dev->cmbsz & NVME_CMBSZ_SQS)) {
2185
+ if (dev->cmb_use_sqes) {
19182186 result = nvme_cmb_qdepth(dev, nr_io_queues,
19192187 sizeof(struct nvme_command));
19202188 if (result > 0)
19212189 dev->q_depth = result;
19222190 else
1923
- nvme_release_cmb(dev);
2191
+ dev->cmb_use_sqes = false;
19242192 }
19252193
19262194 do {
....@@ -1933,6 +2201,7 @@
19332201 } while (1);
19342202 adminq->q_db = dev->dbs;
19352203
2204
+ retry:
19362205 /* Deregister the admin queue's interrupt */
19372206 pci_free_irq(pdev, 0, adminq);
19382207
....@@ -1941,12 +2210,14 @@
19412210 * setting up the full range we need.
19422211 */
19432212 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);
2213
+
2214
+ result = nvme_setup_irqs(dev, nr_io_queues);
19462215 if (result <= 0)
19472216 return -EIO;
2217
+
19482218 dev->num_vecs = result;
1949
- dev->max_qid = max(result - 1, 1);
2219
+ result = max(result - 1, 1);
2220
+ dev->max_qid = result + dev->io_queues[HCTX_TYPE_POLL];
19502221
19512222 /*
19522223 * Should investigate if there's a performance win from allocating
....@@ -1954,13 +2225,26 @@
19542225 * path to scale better, even if the receive path is limited by the
19552226 * number of interrupts.
19562227 */
1957
-
19582228 result = queue_request_irq(adminq);
1959
- if (result) {
1960
- adminq->cq_vector = -1;
2229
+ if (result)
19612230 return result;
2231
+ set_bit(NVMEQ_ENABLED, &adminq->flags);
2232
+
2233
+ result = nvme_create_io_queues(dev);
2234
+ if (result || dev->online_queues < 2)
2235
+ return result;
2236
+
2237
+ if (dev->online_queues - 1 < dev->max_qid) {
2238
+ nr_io_queues = dev->online_queues - 1;
2239
+ nvme_disable_io_queues(dev);
2240
+ nvme_suspend_io_queues(dev);
2241
+ goto retry;
19622242 }
1963
- return nvme_create_io_queues(dev);
2243
+ dev_info(dev->ctrl.device, "%d/%d/%d default/read/poll queues\n",
2244
+ dev->io_queues[HCTX_TYPE_DEFAULT],
2245
+ dev->io_queues[HCTX_TYPE_READ],
2246
+ dev->io_queues[HCTX_TYPE_POLL]);
2247
+ return 0;
19642248 }
19652249
19662250 static void nvme_del_queue_end(struct request *req, blk_status_t error)
....@@ -1968,23 +2252,15 @@
19682252 struct nvme_queue *nvmeq = req->end_io_data;
19692253
19702254 blk_mq_free_request(req);
1971
- complete(&nvmeq->dev->ioq_wait);
2255
+ complete(&nvmeq->delete_done);
19722256 }
19732257
19742258 static void nvme_del_cq_end(struct request *req, blk_status_t error)
19752259 {
19762260 struct nvme_queue *nvmeq = req->end_io_data;
1977
- u16 start, end;
19782261
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
- }
2262
+ if (error)
2263
+ set_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags);
19882264
19892265 nvme_del_queue_end(req, error);
19902266 }
....@@ -1999,77 +2275,80 @@
19992275 cmd.delete_queue.opcode = opcode;
20002276 cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid);
20012277
2002
- req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
2278
+ req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT);
20032279 if (IS_ERR(req))
20042280 return PTR_ERR(req);
20052281
2006
- req->timeout = ADMIN_TIMEOUT;
20072282 req->end_io_data = nvmeq;
20082283
2284
+ init_completion(&nvmeq->delete_done);
20092285 blk_execute_rq_nowait(q, NULL, req, false,
20102286 opcode == nvme_admin_delete_cq ?
20112287 nvme_del_cq_end : nvme_del_queue_end);
20122288 return 0;
20132289 }
20142290
2015
-static void nvme_disable_io_queues(struct nvme_dev *dev)
2291
+static bool __nvme_disable_io_queues(struct nvme_dev *dev, u8 opcode)
20162292 {
2017
- int pass, queues = dev->online_queues - 1;
2293
+ int nr_queues = dev->online_queues - 1, sent = 0;
20182294 unsigned long timeout;
2019
- u8 opcode = nvme_admin_delete_sq;
20202295
2021
- for (pass = 0; pass < 2; pass++) {
2022
- int sent = 0, i = queues;
2023
-
2024
- reinit_completion(&dev->ioq_wait);
20252296 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;
2297
+ timeout = ADMIN_TIMEOUT;
2298
+ while (nr_queues > 0) {
2299
+ if (nvme_delete_queue(&dev->queues[nr_queues], opcode))
2300
+ break;
2301
+ nr_queues--;
2302
+ sent++;
20392303 }
2304
+ while (sent) {
2305
+ struct nvme_queue *nvmeq = &dev->queues[nr_queues + sent];
2306
+
2307
+ timeout = wait_for_completion_io_timeout(&nvmeq->delete_done,
2308
+ timeout);
2309
+ if (timeout == 0)
2310
+ return false;
2311
+
2312
+ sent--;
2313
+ if (nr_queues)
2314
+ goto retry;
2315
+ }
2316
+ return true;
20402317 }
20412318
2042
-/*
2043
- * return error value only when tagset allocation failed
2044
- */
2045
-static int nvme_dev_add(struct nvme_dev *dev)
2319
+static void nvme_dev_add(struct nvme_dev *dev)
20462320 {
20472321 int ret;
20482322
20492323 if (!dev->ctrl.tagset) {
20502324 dev->tagset.ops = &nvme_mq_ops;
20512325 dev->tagset.nr_hw_queues = dev->online_queues - 1;
2326
+ dev->tagset.nr_maps = 2; /* default + read */
2327
+ if (dev->io_queues[HCTX_TYPE_POLL])
2328
+ dev->tagset.nr_maps++;
20522329 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
- }
2330
+ dev->tagset.numa_node = dev->ctrl.numa_node;
2331
+ dev->tagset.queue_depth = min_t(unsigned int, dev->q_depth,
2332
+ BLK_MQ_MAX_DEPTH) - 1;
2333
+ dev->tagset.cmd_size = sizeof(struct nvme_iod);
20612334 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
20622335 dev->tagset.driver_data = dev;
2336
+
2337
+ /*
2338
+ * Some Apple controllers requires tags to be unique
2339
+ * across admin and IO queue, so reserve the first 32
2340
+ * tags of the IO queue.
2341
+ */
2342
+ if (dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS)
2343
+ dev->tagset.reserved_tags = NVME_AQ_DEPTH;
20632344
20642345 ret = blk_mq_alloc_tag_set(&dev->tagset);
20652346 if (ret) {
20662347 dev_warn(dev->ctrl.device,
20672348 "IO queues tagset allocation failed %d\n", ret);
2068
- return ret;
2349
+ return;
20692350 }
20702351 dev->ctrl.tagset = &dev->tagset;
2071
-
2072
- nvme_dbbuf_set(dev);
20732352 } else {
20742353 blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1);
20752354
....@@ -2077,7 +2356,7 @@
20772356 nvme_free_queues(dev, dev->online_queues);
20782357 }
20792358
2080
- return 0;
2359
+ nvme_dbbuf_set(dev);
20812360 }
20822361
20832362 static int nvme_pci_enable(struct nvme_dev *dev)
....@@ -2090,8 +2369,7 @@
20902369
20912370 pci_set_master(pdev);
20922371
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)))
2372
+ if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)))
20952373 goto disable;
20962374
20972375 if (readl(dev->bar + NVME_REG_CSTS) == -1) {
....@@ -2110,10 +2388,24 @@
21102388
21112389 dev->ctrl.cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
21122390
2113
- dev->q_depth = min_t(int, NVME_CAP_MQES(dev->ctrl.cap) + 1,
2391
+ if (dev->ctrl.quirks & NVME_QUIRK_LIMIT_IOQD32)
2392
+ io_queue_depth = 32;
2393
+
2394
+ dev->q_depth = min_t(u32, NVME_CAP_MQES(dev->ctrl.cap) + 1,
21142395 io_queue_depth);
2396
+ dev->ctrl.sqsize = dev->q_depth - 1; /* 0's based queue depth */
21152397 dev->db_stride = 1 << NVME_CAP_STRIDE(dev->ctrl.cap);
21162398 dev->dbs = dev->bar + 4096;
2399
+
2400
+ /*
2401
+ * Some Apple controllers require a non-standard SQE size.
2402
+ * Interestingly they also seem to ignore the CC:IOSQES register
2403
+ * so we don't bother updating it here.
2404
+ */
2405
+ if (dev->ctrl.quirks & NVME_QUIRK_128_BYTES_SQES)
2406
+ dev->io_sqes = 7;
2407
+ else
2408
+ dev->io_sqes = NVME_NVM_IOSQES;
21172409
21182410 /*
21192411 * Temporary fix for the Apple controller found in the MacBook8,1 and
....@@ -2131,6 +2423,18 @@
21312423 dev_err(dev->ctrl.device, "detected PM1725 NVMe controller, "
21322424 "set queue depth=%u\n", dev->q_depth);
21332425 }
2426
+
2427
+ /*
2428
+ * Controllers with the shared tags quirk need the IO queue to be
2429
+ * big enough so that we get 32 tags for the admin queue
2430
+ */
2431
+ if ((dev->ctrl.quirks & NVME_QUIRK_SHARED_TAGS) &&
2432
+ (dev->q_depth < (NVME_AQ_DEPTH + 2))) {
2433
+ dev->q_depth = NVME_AQ_DEPTH + 2;
2434
+ dev_warn(dev->ctrl.device, "IO queue depth clamped to %d\n",
2435
+ dev->q_depth);
2436
+ }
2437
+
21342438
21352439 nvme_map_cmb(dev);
21362440
....@@ -2164,8 +2468,7 @@
21642468
21652469 static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
21662470 {
2167
- int i;
2168
- bool dead = true;
2471
+ bool dead = true, freeze = false;
21692472 struct pci_dev *pdev = to_pci_dev(dev->dev);
21702473
21712474 mutex_lock(&dev->shutdown_lock);
....@@ -2173,8 +2476,10 @@
21732476 u32 csts = readl(dev->bar + NVME_REG_CSTS);
21742477
21752478 if (dev->ctrl.state == NVME_CTRL_LIVE ||
2176
- dev->ctrl.state == NVME_CTRL_RESETTING)
2479
+ dev->ctrl.state == NVME_CTRL_RESETTING) {
2480
+ freeze = true;
21772481 nvme_start_freeze(&dev->ctrl);
2482
+ }
21782483 dead = !!((csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) ||
21792484 pdev->error_state != pci_channel_io_normal);
21802485 }
....@@ -2183,10 +2488,8 @@
21832488 * Give the controller a chance to complete all entered requests if
21842489 * doing a safe shutdown.
21852490 */
2186
- if (!dead) {
2187
- if (shutdown)
2188
- nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
2189
- }
2491
+ if (!dead && shutdown && freeze)
2492
+ nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
21902493
21912494 nvme_stop_queues(&dev->ctrl);
21922495
....@@ -2194,13 +2497,15 @@
21942497 nvme_disable_io_queues(dev);
21952498 nvme_disable_admin_queue(dev, shutdown);
21962499 }
2197
- for (i = dev->ctrl.queue_count - 1; i >= 0; i--)
2198
- nvme_suspend_queue(&dev->queues[i]);
2199
-
2500
+ nvme_suspend_io_queues(dev);
2501
+ nvme_suspend_queue(&dev->queues[0]);
22002502 nvme_pci_disable(dev);
2503
+ nvme_reap_pending_cqes(dev);
22012504
22022505 blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl);
22032506 blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl);
2507
+ blk_mq_tagset_wait_completed_request(&dev->tagset);
2508
+ blk_mq_tagset_wait_completed_request(&dev->admin_tagset);
22042509
22052510 /*
22062511 * The driver will not be starting up queues again if shutting down so
....@@ -2215,10 +2520,19 @@
22152520 mutex_unlock(&dev->shutdown_lock);
22162521 }
22172522
2523
+static int nvme_disable_prepare_reset(struct nvme_dev *dev, bool shutdown)
2524
+{
2525
+ if (!nvme_wait_reset(&dev->ctrl))
2526
+ return -EBUSY;
2527
+ nvme_dev_disable(dev, shutdown);
2528
+ return 0;
2529
+}
2530
+
22182531 static int nvme_setup_prp_pools(struct nvme_dev *dev)
22192532 {
22202533 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
2221
- PAGE_SIZE, PAGE_SIZE, 0);
2534
+ NVME_CTRL_PAGE_SIZE,
2535
+ NVME_CTRL_PAGE_SIZE, 0);
22222536 if (!dev->prp_page_pool)
22232537 return -ENOMEM;
22242538
....@@ -2238,26 +2552,52 @@
22382552 dma_pool_destroy(dev->prp_small_pool);
22392553 }
22402554
2555
+static int nvme_pci_alloc_iod_mempool(struct nvme_dev *dev)
2556
+{
2557
+ size_t npages = max(nvme_pci_npages_prp(), nvme_pci_npages_sgl());
2558
+ size_t alloc_size = sizeof(__le64 *) * npages +
2559
+ sizeof(struct scatterlist) * NVME_MAX_SEGS;
2560
+
2561
+ WARN_ON_ONCE(alloc_size > PAGE_SIZE);
2562
+ dev->iod_mempool = mempool_create_node(1,
2563
+ mempool_kmalloc, mempool_kfree,
2564
+ (void *)alloc_size, GFP_KERNEL,
2565
+ dev_to_node(dev->dev));
2566
+ if (!dev->iod_mempool)
2567
+ return -ENOMEM;
2568
+ return 0;
2569
+}
2570
+
2571
+static void nvme_free_tagset(struct nvme_dev *dev)
2572
+{
2573
+ if (dev->tagset.tags)
2574
+ blk_mq_free_tag_set(&dev->tagset);
2575
+ dev->ctrl.tagset = NULL;
2576
+}
2577
+
2578
+/* pairs with nvme_pci_alloc_dev */
22412579 static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
22422580 {
22432581 struct nvme_dev *dev = to_nvme_dev(ctrl);
22442582
22452583 nvme_dbbuf_dma_free(dev);
2246
- put_device(dev->dev);
2247
- if (dev->tagset.tags)
2248
- blk_mq_free_tag_set(&dev->tagset);
2584
+ nvme_free_tagset(dev);
22492585 if (dev->ctrl.admin_q)
22502586 blk_put_queue(dev->ctrl.admin_q);
2251
- kfree(dev->queues);
22522587 free_opal_dev(dev->ctrl.opal_dev);
22532588 mempool_destroy(dev->iod_mempool);
2589
+ put_device(dev->dev);
2590
+ kfree(dev->queues);
22542591 kfree(dev);
22552592 }
22562593
2257
-static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
2594
+static void nvme_remove_dead_ctrl(struct nvme_dev *dev)
22582595 {
2259
- dev_warn(dev->ctrl.device, "Removing after probe failure status: %d\n", status);
2260
-
2596
+ /*
2597
+ * Set state to deleting now to avoid blocking nvme_wait_reset(), which
2598
+ * may be holding this pci_dev's device lock.
2599
+ */
2600
+ nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
22612601 nvme_get_ctrl(&dev->ctrl);
22622602 nvme_dev_disable(dev, false);
22632603 nvme_kill_queues(&dev->ctrl);
....@@ -2271,7 +2611,6 @@
22712611 container_of(work, struct nvme_dev, ctrl.reset_work);
22722612 bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
22732613 int result;
2274
- enum nvme_ctrl_state new_state = NVME_CTRL_LIVE;
22752614
22762615 if (dev->ctrl.state != NVME_CTRL_RESETTING) {
22772616 dev_warn(dev->ctrl.device, "ctrl state %d is not RESETTING\n",
....@@ -2286,6 +2625,7 @@
22862625 */
22872626 if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
22882627 nvme_dev_disable(dev, false);
2628
+ nvme_sync_queues(&dev->ctrl);
22892629
22902630 mutex_lock(&dev->shutdown_lock);
22912631 result = nvme_pci_enable(dev);
....@@ -2300,12 +2640,21 @@
23002640 if (result)
23012641 goto out_unlock;
23022642
2643
+ dma_set_min_align_mask(dev->dev, NVME_CTRL_PAGE_SIZE - 1);
2644
+
23032645 /*
23042646 * Limit the max command size to prevent iod->sg allocations going
23052647 * over a single page.
23062648 */
2307
- dev->ctrl.max_hw_sectors = NVME_MAX_KB_SZ << 1;
2649
+ dev->ctrl.max_hw_sectors = min_t(u32,
2650
+ NVME_MAX_KB_SZ << 1, dma_max_mapping_size(dev->dev) >> 9);
23082651 dev->ctrl.max_segments = NVME_MAX_SEGS;
2652
+
2653
+ /*
2654
+ * Don't limit the IOMMU merged segment size.
2655
+ */
2656
+ dma_set_max_seg_size(dev->dev, 0xffffffff);
2657
+
23092658 mutex_unlock(&dev->shutdown_lock);
23102659
23112660 /*
....@@ -2318,6 +2667,12 @@
23182667 result = -EBUSY;
23192668 goto out;
23202669 }
2670
+
2671
+ /*
2672
+ * We do not support an SGL for metadata (yet), so we are limited to a
2673
+ * single integrity segment for the separate metadata pointer.
2674
+ */
2675
+ dev->ctrl.max_integrity_segments = 1;
23212676
23222677 result = nvme_init_identify(&dev->ctrl);
23232678 if (result)
....@@ -2359,13 +2714,11 @@
23592714 dev_warn(dev->ctrl.device, "IO queues not created\n");
23602715 nvme_kill_queues(&dev->ctrl);
23612716 nvme_remove_namespaces(&dev->ctrl);
2362
- new_state = NVME_CTRL_ADMIN_ONLY;
2717
+ nvme_free_tagset(dev);
23632718 } else {
23642719 nvme_start_queues(&dev->ctrl);
23652720 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;
2721
+ nvme_dev_add(dev);
23692722 nvme_unfreeze(&dev->ctrl);
23702723 }
23712724
....@@ -2373,9 +2726,9 @@
23732726 * If only admin queue live, keep it to do further investigation or
23742727 * recovery.
23752728 */
2376
- if (!nvme_change_ctrl_state(&dev->ctrl, new_state)) {
2729
+ if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) {
23772730 dev_warn(dev->ctrl.device,
2378
- "failed to mark controller state %d\n", new_state);
2731
+ "failed to mark controller live state\n");
23792732 result = -ENODEV;
23802733 goto out;
23812734 }
....@@ -2386,7 +2739,10 @@
23862739 out_unlock:
23872740 mutex_unlock(&dev->shutdown_lock);
23882741 out:
2389
- nvme_remove_dead_ctrl(dev, result);
2742
+ if (result)
2743
+ dev_warn(dev->ctrl.device,
2744
+ "Removing after probe failure status: %d\n", result);
2745
+ nvme_remove_dead_ctrl(dev);
23902746 }
23912747
23922748 static void nvme_remove_dead_ctrl_work(struct work_struct *work)
....@@ -2421,13 +2777,14 @@
24212777 {
24222778 struct pci_dev *pdev = to_pci_dev(to_nvme_dev(ctrl)->dev);
24232779
2424
- return snprintf(buf, size, "%s", dev_name(&pdev->dev));
2780
+ return snprintf(buf, size, "%s\n", dev_name(&pdev->dev));
24252781 }
24262782
24272783 static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
24282784 .name = "pcie",
24292785 .module = THIS_MODULE,
2430
- .flags = NVME_F_METADATA_SUPPORTED,
2786
+ .flags = NVME_F_METADATA_SUPPORTED |
2787
+ NVME_F_PCI_P2PDMA,
24312788 .reg_read32 = nvme_pci_reg_read32,
24322789 .reg_write32 = nvme_pci_reg_write32,
24332790 .reg_read64 = nvme_pci_reg_read64,
....@@ -2478,6 +2835,18 @@
24782835 (dmi_match(DMI_BOARD_NAME, "PRIME B350M-A") ||
24792836 dmi_match(DMI_BOARD_NAME, "PRIME Z370-A")))
24802837 return NVME_QUIRK_NO_APST;
2838
+ } else if ((pdev->vendor == 0x144d && (pdev->device == 0xa801 ||
2839
+ pdev->device == 0xa808 || pdev->device == 0xa809)) ||
2840
+ (pdev->vendor == 0x1e0f && pdev->device == 0x0001)) {
2841
+ /*
2842
+ * Forcing to use host managed nvme power settings for
2843
+ * lowest idle power with quick resume latency on
2844
+ * Samsung and Toshiba SSDs based on suspend behavior
2845
+ * on Coffee Lake board for LENOVO C640
2846
+ */
2847
+ if ((dmi_match(DMI_BOARD_VENDOR, "LENOVO")) &&
2848
+ dmi_match(DMI_BOARD_NAME, "LNVNB161216"))
2849
+ return NVME_QUIRK_SIMPLE_SUSPEND;
24812850 }
24822851
24832852 return 0;
....@@ -2492,104 +2861,118 @@
24922861 nvme_put_ctrl(&dev->ctrl);
24932862 }
24942863
2495
-static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2864
+static struct nvme_dev *nvme_pci_alloc_dev(struct pci_dev *pdev,
2865
+ const struct pci_device_id *id)
24962866 {
2497
- int node, result = -ENOMEM;
2498
- struct nvme_dev *dev;
24992867 unsigned long quirks = id->driver_data;
2500
- size_t alloc_size;
2501
-
2502
- node = dev_to_node(&pdev->dev);
2503
- if (node == NUMA_NO_NODE)
2504
- set_dev_node(&pdev->dev, first_memory_node);
2868
+ int node = dev_to_node(&pdev->dev);
2869
+ struct nvme_dev *dev;
2870
+ int ret = -ENOMEM;
25052871
25062872 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
25072873 if (!dev)
2508
- return -ENOMEM;
2509
-
2510
- dev->queues = kcalloc_node(num_possible_cpus() + 1,
2511
- sizeof(struct nvme_queue), GFP_KERNEL, node);
2512
- if (!dev->queues)
2513
- goto free;
2514
-
2515
- dev->dev = get_device(&pdev->dev);
2516
- pci_set_drvdata(pdev, dev);
2517
-
2518
- result = nvme_dev_map(dev);
2519
- if (result)
2520
- goto put_pci;
2521
-
2874
+ return ERR_PTR(-ENOMEM);
25222875 INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work);
25232876 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
25242877 mutex_init(&dev->shutdown_lock);
2525
- init_completion(&dev->ioq_wait);
2878
+
2879
+ dev->nr_write_queues = write_queues;
2880
+ dev->nr_poll_queues = poll_queues;
2881
+ dev->nr_allocated_queues = nvme_max_io_queues(dev) + 1;
2882
+ dev->queues = kcalloc_node(dev->nr_allocated_queues,
2883
+ sizeof(struct nvme_queue), GFP_KERNEL, node);
2884
+ if (!dev->queues)
2885
+ goto out_free_dev;
2886
+
2887
+ dev->dev = get_device(&pdev->dev);
2888
+
2889
+ quirks |= check_vendor_combination_bug(pdev);
2890
+ if (!noacpi && acpi_storage_d3(&pdev->dev)) {
2891
+ /*
2892
+ * Some systems use a bios work around to ask for D3 on
2893
+ * platforms that support kernel managed suspend.
2894
+ */
2895
+ dev_info(&pdev->dev,
2896
+ "platform quirk: setting simple suspend\n");
2897
+ quirks |= NVME_QUIRK_SIMPLE_SUSPEND;
2898
+ }
2899
+ ret = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
2900
+ quirks);
2901
+ if (ret)
2902
+ goto out_put_device;
2903
+ return dev;
2904
+
2905
+out_put_device:
2906
+ put_device(dev->dev);
2907
+ kfree(dev->queues);
2908
+out_free_dev:
2909
+ kfree(dev);
2910
+ return ERR_PTR(ret);
2911
+}
2912
+
2913
+static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2914
+{
2915
+ struct nvme_dev *dev;
2916
+ int result = -ENOMEM;
2917
+
2918
+ dev = nvme_pci_alloc_dev(pdev, id);
2919
+ if (IS_ERR(dev))
2920
+ return PTR_ERR(dev);
2921
+
2922
+ result = nvme_dev_map(dev);
2923
+ if (result)
2924
+ goto out_uninit_ctrl;
25262925
25272926 result = nvme_setup_prp_pools(dev);
25282927 if (result)
2529
- goto unmap;
2928
+ goto out_dev_unmap;
25302929
2531
- quirks |= check_vendor_combination_bug(pdev);
2532
-
2533
- /*
2534
- * Double check that our mempool alloc size will cover the biggest
2535
- * command we support.
2536
- */
2537
- alloc_size = nvme_pci_iod_alloc_size(dev, NVME_MAX_KB_SZ,
2538
- NVME_MAX_SEGS, true);
2539
- WARN_ON_ONCE(alloc_size > PAGE_SIZE);
2540
-
2541
- dev->iod_mempool = mempool_create_node(1, mempool_kmalloc,
2542
- mempool_kfree,
2543
- (void *) alloc_size,
2544
- GFP_KERNEL, node);
2545
- if (!dev->iod_mempool) {
2546
- result = -ENOMEM;
2547
- goto release_pools;
2548
- }
2549
-
2550
- result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
2551
- quirks);
2930
+ result = nvme_pci_alloc_iod_mempool(dev);
25522931 if (result)
2553
- goto release_mempool;
2932
+ goto out_release_prp_pools;
25542933
25552934 dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev));
2935
+ pci_set_drvdata(pdev, dev);
25562936
25572937 nvme_reset_ctrl(&dev->ctrl);
2558
- nvme_get_ctrl(&dev->ctrl);
25592938 async_schedule(nvme_async_probe, dev);
2560
-
25612939 return 0;
25622940
2563
- release_mempool:
2564
- mempool_destroy(dev->iod_mempool);
2565
- release_pools:
2941
+out_release_prp_pools:
25662942 nvme_release_prp_pools(dev);
2567
- unmap:
2943
+out_dev_unmap:
25682944 nvme_dev_unmap(dev);
2569
- put_pci:
2570
- put_device(dev->dev);
2571
- free:
2572
- kfree(dev->queues);
2573
- kfree(dev);
2945
+out_uninit_ctrl:
2946
+ nvme_uninit_ctrl(&dev->ctrl);
25742947 return result;
25752948 }
25762949
25772950 static void nvme_reset_prepare(struct pci_dev *pdev)
25782951 {
25792952 struct nvme_dev *dev = pci_get_drvdata(pdev);
2580
- nvme_dev_disable(dev, false);
2953
+
2954
+ /*
2955
+ * We don't need to check the return value from waiting for the reset
2956
+ * state as pci_dev device lock is held, making it impossible to race
2957
+ * with ->remove().
2958
+ */
2959
+ nvme_disable_prepare_reset(dev, false);
2960
+ nvme_sync_queues(&dev->ctrl);
25812961 }
25822962
25832963 static void nvme_reset_done(struct pci_dev *pdev)
25842964 {
25852965 struct nvme_dev *dev = pci_get_drvdata(pdev);
2586
- nvme_reset_ctrl_sync(&dev->ctrl);
2966
+
2967
+ if (!nvme_try_sched_reset(&dev->ctrl))
2968
+ flush_work(&dev->ctrl.reset_work);
25872969 }
25882970
25892971 static void nvme_shutdown(struct pci_dev *pdev)
25902972 {
25912973 struct nvme_dev *dev = pci_get_drvdata(pdev);
2592
- nvme_dev_disable(dev, true);
2974
+
2975
+ nvme_disable_prepare_reset(dev, true);
25932976 }
25942977
25952978 /*
....@@ -2617,33 +3000,128 @@
26173000 nvme_free_host_mem(dev);
26183001 nvme_dev_remove_admin(dev);
26193002 nvme_free_queues(dev, 0);
2620
- nvme_uninit_ctrl(&dev->ctrl);
26213003 nvme_release_prp_pools(dev);
26223004 nvme_dev_unmap(dev);
2623
- nvme_put_ctrl(&dev->ctrl);
3005
+ nvme_uninit_ctrl(&dev->ctrl);
26243006 }
26253007
26263008 #ifdef CONFIG_PM_SLEEP
2627
-static int nvme_suspend(struct device *dev)
3009
+static int nvme_get_power_state(struct nvme_ctrl *ctrl, u32 *ps)
26283010 {
2629
- struct pci_dev *pdev = to_pci_dev(dev);
2630
- struct nvme_dev *ndev = pci_get_drvdata(pdev);
3011
+ return nvme_get_features(ctrl, NVME_FEAT_POWER_MGMT, 0, NULL, 0, ps);
3012
+}
26313013
2632
- nvme_dev_disable(ndev, true);
2633
- return 0;
3014
+static int nvme_set_power_state(struct nvme_ctrl *ctrl, u32 ps)
3015
+{
3016
+ return nvme_set_features(ctrl, NVME_FEAT_POWER_MGMT, ps, NULL, 0, NULL);
26343017 }
26353018
26363019 static int nvme_resume(struct device *dev)
26373020 {
3021
+ struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev));
3022
+ struct nvme_ctrl *ctrl = &ndev->ctrl;
3023
+
3024
+ if (ndev->last_ps == U32_MAX ||
3025
+ nvme_set_power_state(ctrl, ndev->last_ps) != 0)
3026
+ return nvme_try_sched_reset(&ndev->ctrl);
3027
+ return 0;
3028
+}
3029
+
3030
+static int nvme_suspend(struct device *dev)
3031
+{
3032
+ struct pci_dev *pdev = to_pci_dev(dev);
3033
+ struct nvme_dev *ndev = pci_get_drvdata(pdev);
3034
+ struct nvme_ctrl *ctrl = &ndev->ctrl;
3035
+ int ret = -EBUSY;
3036
+
3037
+ ndev->last_ps = U32_MAX;
3038
+
3039
+ /*
3040
+ * The platform does not remove power for a kernel managed suspend so
3041
+ * use host managed nvme power settings for lowest idle power if
3042
+ * possible. This should have quicker resume latency than a full device
3043
+ * shutdown. But if the firmware is involved after the suspend or the
3044
+ * device does not support any non-default power states, shut down the
3045
+ * device fully.
3046
+ *
3047
+ * If ASPM is not enabled for the device, shut down the device and allow
3048
+ * the PCI bus layer to put it into D3 in order to take the PCIe link
3049
+ * down, so as to allow the platform to achieve its minimum low-power
3050
+ * state (which may not be possible if the link is up).
3051
+ *
3052
+ * If a host memory buffer is enabled, shut down the device as the NVMe
3053
+ * specification allows the device to access the host memory buffer in
3054
+ * host DRAM from all power states, but hosts will fail access to DRAM
3055
+ * during S3.
3056
+ */
3057
+ if (pm_suspend_via_firmware() || !ctrl->npss ||
3058
+ !pcie_aspm_enabled(pdev) ||
3059
+ ndev->nr_host_mem_descs ||
3060
+ (ndev->ctrl.quirks & NVME_QUIRK_SIMPLE_SUSPEND))
3061
+ return nvme_disable_prepare_reset(ndev, true);
3062
+
3063
+ nvme_start_freeze(ctrl);
3064
+ nvme_wait_freeze(ctrl);
3065
+ nvme_sync_queues(ctrl);
3066
+
3067
+ if (ctrl->state != NVME_CTRL_LIVE)
3068
+ goto unfreeze;
3069
+
3070
+ ret = nvme_get_power_state(ctrl, &ndev->last_ps);
3071
+ if (ret < 0)
3072
+ goto unfreeze;
3073
+
3074
+ /*
3075
+ * A saved state prevents pci pm from generically controlling the
3076
+ * device's power. If we're using protocol specific settings, we don't
3077
+ * want pci interfering.
3078
+ */
3079
+ pci_save_state(pdev);
3080
+
3081
+ ret = nvme_set_power_state(ctrl, ctrl->npss);
3082
+ if (ret < 0)
3083
+ goto unfreeze;
3084
+
3085
+ if (ret) {
3086
+ /* discard the saved state */
3087
+ pci_load_saved_state(pdev, NULL);
3088
+
3089
+ /*
3090
+ * Clearing npss forces a controller reset on resume. The
3091
+ * correct value will be rediscovered then.
3092
+ */
3093
+ ret = nvme_disable_prepare_reset(ndev, true);
3094
+ ctrl->npss = 0;
3095
+ }
3096
+unfreeze:
3097
+ nvme_unfreeze(ctrl);
3098
+ return ret;
3099
+}
3100
+
3101
+static int nvme_simple_suspend(struct device *dev)
3102
+{
3103
+ struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev));
3104
+
3105
+ return nvme_disable_prepare_reset(ndev, true);
3106
+}
3107
+
3108
+static int nvme_simple_resume(struct device *dev)
3109
+{
26383110 struct pci_dev *pdev = to_pci_dev(dev);
26393111 struct nvme_dev *ndev = pci_get_drvdata(pdev);
26403112
2641
- nvme_reset_ctrl(&ndev->ctrl);
2642
- return 0;
3113
+ return nvme_try_sched_reset(&ndev->ctrl);
26433114 }
2644
-#endif
26453115
2646
-static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
3116
+static const struct dev_pm_ops nvme_dev_pm_ops = {
3117
+ .suspend = nvme_suspend,
3118
+ .resume = nvme_resume,
3119
+ .freeze = nvme_simple_suspend,
3120
+ .thaw = nvme_simple_resume,
3121
+ .poweroff = nvme_simple_suspend,
3122
+ .restore = nvme_simple_resume,
3123
+};
3124
+#endif /* CONFIG_PM_SLEEP */
26473125
26483126 static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
26493127 pci_channel_state_t state)
....@@ -2686,7 +3164,6 @@
26863164 struct nvme_dev *dev = pci_get_drvdata(pdev);
26873165
26883166 flush_work(&dev->ctrl.reset_work);
2689
- pci_cleanup_aer_uncorrect_error_status(pdev);
26903167 }
26913168
26923169 static const struct pci_error_handlers nvme_err_handler = {
....@@ -2698,25 +3175,37 @@
26983175 };
26993176
27003177 static const struct pci_device_id nvme_id_table[] = {
2701
- { PCI_VDEVICE(INTEL, 0x0953),
3178
+ { PCI_VDEVICE(INTEL, 0x0953), /* Intel 750/P3500/P3600/P3700 */
27023179 .driver_data = NVME_QUIRK_STRIPE_SIZE |
27033180 NVME_QUIRK_DEALLOCATE_ZEROES, },
2704
- { PCI_VDEVICE(INTEL, 0x0a53),
3181
+ { PCI_VDEVICE(INTEL, 0x0a53), /* Intel P3520 */
27053182 .driver_data = NVME_QUIRK_STRIPE_SIZE |
27063183 NVME_QUIRK_DEALLOCATE_ZEROES, },
2707
- { PCI_VDEVICE(INTEL, 0x0a54),
3184
+ { PCI_VDEVICE(INTEL, 0x0a54), /* Intel P4500/P4600 */
27083185 .driver_data = NVME_QUIRK_STRIPE_SIZE |
2709
- NVME_QUIRK_DEALLOCATE_ZEROES, },
2710
- { PCI_VDEVICE(INTEL, 0x0a55),
3186
+ NVME_QUIRK_DEALLOCATE_ZEROES |
3187
+ NVME_QUIRK_IGNORE_DEV_SUBNQN, },
3188
+ { PCI_VDEVICE(INTEL, 0x0a55), /* Dell Express Flash P4600 */
27113189 .driver_data = NVME_QUIRK_STRIPE_SIZE |
27123190 NVME_QUIRK_DEALLOCATE_ZEROES, },
27133191 { PCI_VDEVICE(INTEL, 0xf1a5), /* Intel 600P/P3100 */
27143192 .driver_data = NVME_QUIRK_NO_DEEPEST_PS |
2715
- NVME_QUIRK_MEDIUM_PRIO_SQ },
3193
+ NVME_QUIRK_MEDIUM_PRIO_SQ |
3194
+ NVME_QUIRK_NO_TEMP_THRESH_CHANGE |
3195
+ NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3196
+ { PCI_VDEVICE(INTEL, 0xf1a6), /* Intel 760p/Pro 7600p */
3197
+ .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN, },
27163198 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
2717
- .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
3199
+ .driver_data = NVME_QUIRK_IDENTIFY_CNS |
3200
+ NVME_QUIRK_DISABLE_WRITE_ZEROES |
3201
+ NVME_QUIRK_BOGUS_NID, },
3202
+ { PCI_VDEVICE(REDHAT, 0x0010), /* Qemu emulated controller */
3203
+ .driver_data = NVME_QUIRK_BOGUS_NID, },
3204
+ { PCI_DEVICE(0x126f, 0x2263), /* Silicon Motion unidentified */
3205
+ .driver_data = NVME_QUIRK_NO_NS_DESC_LIST, },
27183206 { PCI_DEVICE(0x1bb1, 0x0100), /* Seagate Nytro Flash Storage */
2719
- .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
3207
+ .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY |
3208
+ NVME_QUIRK_NO_NS_DESC_LIST, },
27203209 { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */
27213210 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
27223211 { PCI_DEVICE(0x1c58, 0x0023), /* WDC SN200 adapter */
....@@ -2726,18 +3215,50 @@
27263215 { PCI_DEVICE(0x144d, 0xa821), /* Samsung PM1725 */
27273216 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
27283217 { PCI_DEVICE(0x144d, 0xa822), /* Samsung PM1725a */
2729
- .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
3218
+ .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY |
3219
+ NVME_QUIRK_DISABLE_WRITE_ZEROES|
3220
+ NVME_QUIRK_IGNORE_DEV_SUBNQN, },
3221
+ { PCI_DEVICE(0x1987, 0x5013), /* Phison E13 */
3222
+ .driver_data = NVME_QUIRK_LIMIT_IOQD32},
3223
+ { PCI_DEVICE(0x1987, 0x5016), /* Phison E16 */
3224
+ .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN |
3225
+ NVME_QUIRK_BOGUS_NID, },
3226
+ { PCI_DEVICE(0x1b4b, 0x1092), /* Lexar 256 GB SSD */
3227
+ .driver_data = NVME_QUIRK_NO_NS_DESC_LIST |
3228
+ NVME_QUIRK_IGNORE_DEV_SUBNQN, },
27303229 { PCI_DEVICE(0x1d1d, 0x1f1f), /* LighNVM qemu device */
27313230 .driver_data = NVME_QUIRK_LIGHTNVM, },
27323231 { PCI_DEVICE(0x1d1d, 0x2807), /* CNEX WL */
27333232 .driver_data = NVME_QUIRK_LIGHTNVM, },
27343233 { PCI_DEVICE(0x1d1d, 0x2601), /* CNEX Granby */
27353234 .driver_data = NVME_QUIRK_LIGHTNVM, },
2736
- { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
3235
+ { PCI_DEVICE(0x10ec, 0x5762), /* ADATA SX6000LNP */
3236
+ .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN |
3237
+ NVME_QUIRK_BOGUS_NID, },
3238
+ { PCI_DEVICE(0x1cc1, 0x8201), /* ADATA SX8200PNP 512GB */
3239
+ .driver_data = NVME_QUIRK_NO_DEEPEST_PS |
3240
+ NVME_QUIRK_IGNORE_DEV_SUBNQN, },
3241
+ { PCI_DEVICE(0x1344, 0x5407), /* Micron Technology Inc NVMe SSD */
3242
+ .driver_data = NVME_QUIRK_IGNORE_DEV_SUBNQN },
3243
+ { PCI_DEVICE(0x1344, 0x6001), /* Micron Nitro NVMe */
3244
+ .driver_data = NVME_QUIRK_BOGUS_NID, },
3245
+ { PCI_DEVICE(0x1c5c, 0x1504), /* SK Hynix PC400 */
3246
+ .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3247
+ { PCI_DEVICE(0x15b7, 0x2001), /* Sandisk Skyhawk */
3248
+ .driver_data = NVME_QUIRK_DISABLE_WRITE_ZEROES, },
3249
+ { PCI_DEVICE(0x2646, 0x2262), /* KINGSTON SKC2000 NVMe SSD */
3250
+ .driver_data = NVME_QUIRK_NO_DEEPEST_PS, },
27373251 { PCI_DEVICE(0x2646, 0x2263), /* KINGSTON A2000 NVMe SSD */
27383252 .driver_data = NVME_QUIRK_NO_DEEPEST_PS, },
2739
- { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
3253
+ { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001),
3254
+ .driver_data = NVME_QUIRK_SINGLE_VECTOR },
27403255 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) },
3256
+ { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2005),
3257
+ .driver_data = NVME_QUIRK_SINGLE_VECTOR |
3258
+ NVME_QUIRK_128_BYTES_SQES |
3259
+ NVME_QUIRK_SHARED_TAGS |
3260
+ NVME_QUIRK_SKIP_CID_GEN },
3261
+ { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
27413262 { 0, }
27423263 };
27433264 MODULE_DEVICE_TABLE(pci, nvme_id_table);
....@@ -2748,15 +3269,22 @@
27483269 .probe = nvme_probe,
27493270 .remove = nvme_remove,
27503271 .shutdown = nvme_shutdown,
3272
+#ifdef CONFIG_PM_SLEEP
27513273 .driver = {
27523274 .pm = &nvme_dev_pm_ops,
27533275 },
3276
+#endif
27543277 .sriov_configure = pci_sriov_configure_simple,
27553278 .err_handler = &nvme_err_handler,
27563279 };
27573280
27583281 static int __init nvme_init(void)
27593282 {
3283
+ BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
3284
+ BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
3285
+ BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
3286
+ BUILD_BUG_ON(IRQ_AFFINITY_MAX_SETS < 2);
3287
+
27603288 return pci_register_driver(&nvme_driver);
27613289 }
27623290
....@@ -2764,7 +3292,6 @@
27643292 {
27653293 pci_unregister_driver(&nvme_driver);
27663294 flush_workqueue(nvme_wq);
2767
- _nvme_check_size();
27683295 }
27693296
27703297 MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");