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