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