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