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