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