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