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