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