1 /* 2 * NVM Express device driver 3 * Copyright (c) 2011-2014, Intel Corporation. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms and conditions of the GNU General Public License, 7 * version 2, as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 */ 14 15 #include <linux/aer.h> 16 #include <linux/async.h> 17 #include <linux/blkdev.h> 18 #include <linux/blk-mq.h> 19 #include <linux/blk-mq-pci.h> 20 #include <linux/dmi.h> 21 #include <linux/init.h> 22 #include <linux/interrupt.h> 23 #include <linux/io.h> 24 #include <linux/mm.h> 25 #include <linux/module.h> 26 #include <linux/mutex.h> 27 #include <linux/once.h> 28 #include <linux/pci.h> 29 #include <linux/t10-pi.h> 30 #include <linux/types.h> 31 #include <linux/io-64-nonatomic-lo-hi.h> 32 #include <linux/sed-opal.h> 33 #include <linux/pci-p2pdma.h> 34 35 #include "trace.h" 36 #include "nvme.h" 37 38 #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command)) 39 #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion)) 40 41 #define SGES_PER_PAGE (PAGE_SIZE / sizeof(struct nvme_sgl_desc)) 42 43 /* 44 * These can be higher, but we need to ensure that any command doesn't 45 * require an sg allocation that needs more than a page of data. 46 */ 47 #define NVME_MAX_KB_SZ 4096 48 #define NVME_MAX_SEGS 127 49 50 static int use_threaded_interrupts; 51 module_param(use_threaded_interrupts, int, 0); 52 53 static bool use_cmb_sqes = true; 54 module_param(use_cmb_sqes, bool, 0444); 55 MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes"); 56 57 static unsigned int max_host_mem_size_mb = 128; 58 module_param(max_host_mem_size_mb, uint, 0444); 59 MODULE_PARM_DESC(max_host_mem_size_mb, 60 "Maximum Host Memory Buffer (HMB) size per controller (in MiB)"); 61 62 static unsigned int sgl_threshold = SZ_32K; 63 module_param(sgl_threshold, uint, 0644); 64 MODULE_PARM_DESC(sgl_threshold, 65 "Use SGLs when average request segment size is larger or equal to " 66 "this size. Use 0 to disable SGLs."); 67 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_int, 72 }; 73 74 static 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"); 77 78 static int queue_count_set(const char *val, const struct kernel_param *kp); 79 static const struct kernel_param_ops queue_count_ops = { 80 .set = queue_count_set, 81 .get = param_get_int, 82 }; 83 84 static int write_queues; 85 module_param_cb(write_queues, &queue_count_ops, &write_queues, 0644); 86 MODULE_PARM_DESC(write_queues, 87 "Number of queues to use for writes. If not set, reads and writes " 88 "will share a queue set."); 89 90 static int poll_queues = 0; 91 module_param_cb(poll_queues, &queue_count_ops, &poll_queues, 0644); 92 MODULE_PARM_DESC(poll_queues, "Number of queues to use for polled IO."); 93 94 struct nvme_dev; 95 struct nvme_queue; 96 97 static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown); 98 99 /* 100 * Represents an NVM Express device. Each nvme_dev is a PCI function. 101 */ 102 struct nvme_dev { 103 struct nvme_queue *queues; 104 struct blk_mq_tag_set tagset; 105 struct blk_mq_tag_set admin_tagset; 106 u32 __iomem *dbs; 107 struct device *dev; 108 struct dma_pool *prp_page_pool; 109 struct dma_pool *prp_small_pool; 110 unsigned online_queues; 111 unsigned max_qid; 112 unsigned io_queues[HCTX_MAX_TYPES]; 113 unsigned int num_vecs; 114 int q_depth; 115 u32 db_stride; 116 void __iomem *bar; 117 unsigned long bar_mapped_size; 118 struct work_struct remove_work; 119 struct mutex shutdown_lock; 120 bool subsystem; 121 u64 cmb_size; 122 bool cmb_use_sqes; 123 u32 cmbsz; 124 u32 cmbloc; 125 struct nvme_ctrl ctrl; 126 127 mempool_t *iod_mempool; 128 129 /* shadow doorbell buffer support: */ 130 u32 *dbbuf_dbs; 131 dma_addr_t dbbuf_dbs_dma_addr; 132 u32 *dbbuf_eis; 133 dma_addr_t dbbuf_eis_dma_addr; 134 135 /* host memory buffer support: */ 136 u64 host_mem_size; 137 u32 nr_host_mem_descs; 138 dma_addr_t host_mem_descs_dma; 139 struct nvme_host_mem_buf_desc *host_mem_descs; 140 void **host_mem_desc_bufs; 141 }; 142 143 static int io_queue_depth_set(const char *val, const struct kernel_param *kp) 144 { 145 int n = 0, ret; 146 147 ret = kstrtoint(val, 10, &n); 148 if (ret != 0 || n < 2) 149 return -EINVAL; 150 151 return param_set_int(val, kp); 152 } 153 154 static int queue_count_set(const char *val, const struct kernel_param *kp) 155 { 156 int n = 0, ret; 157 158 ret = kstrtoint(val, 10, &n); 159 if (n > num_possible_cpus()) 160 n = num_possible_cpus(); 161 162 return param_set_int(val, kp); 163 } 164 165 static inline unsigned int sq_idx(unsigned int qid, u32 stride) 166 { 167 return qid * 2 * stride; 168 } 169 170 static inline unsigned int cq_idx(unsigned int qid, u32 stride) 171 { 172 return (qid * 2 + 1) * stride; 173 } 174 175 static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl) 176 { 177 return container_of(ctrl, struct nvme_dev, ctrl); 178 } 179 180 /* 181 * An NVM Express queue. Each device has at least two (one for admin 182 * commands and one for I/O commands). 183 */ 184 struct nvme_queue { 185 struct device *q_dmadev; 186 struct nvme_dev *dev; 187 spinlock_t sq_lock; 188 struct nvme_command *sq_cmds; 189 /* only used for poll queues: */ 190 spinlock_t cq_poll_lock ____cacheline_aligned_in_smp; 191 volatile struct nvme_completion *cqes; 192 struct blk_mq_tags **tags; 193 dma_addr_t sq_dma_addr; 194 dma_addr_t cq_dma_addr; 195 u32 __iomem *q_db; 196 u16 q_depth; 197 s16 cq_vector; 198 u16 sq_tail; 199 u16 last_sq_tail; 200 u16 cq_head; 201 u16 last_cq_head; 202 u16 qid; 203 u8 cq_phase; 204 unsigned long flags; 205 #define NVMEQ_ENABLED 0 206 #define NVMEQ_SQ_CMB 1 207 #define NVMEQ_DELETE_ERROR 2 208 u32 *dbbuf_sq_db; 209 u32 *dbbuf_cq_db; 210 u32 *dbbuf_sq_ei; 211 u32 *dbbuf_cq_ei; 212 struct completion delete_done; 213 }; 214 215 /* 216 * The nvme_iod describes the data in an I/O, including the list of PRP 217 * entries. You can't see it in this data structure because C doesn't let 218 * me express that. Use nvme_init_iod to ensure there's enough space 219 * allocated to store the PRP list. 220 */ 221 struct nvme_iod { 222 struct nvme_request req; 223 struct nvme_queue *nvmeq; 224 bool use_sgl; 225 int aborted; 226 int npages; /* In the PRP list. 0 means small pool in use */ 227 int nents; /* Used in scatterlist */ 228 int length; /* Of data, in bytes */ 229 dma_addr_t first_dma; 230 struct scatterlist meta_sg; /* metadata requires single contiguous buffer */ 231 struct scatterlist *sg; 232 struct scatterlist inline_sg[0]; 233 }; 234 235 /* 236 * Check we didin't inadvertently grow the command struct 237 */ 238 static inline void _nvme_check_size(void) 239 { 240 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64); 241 BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64); 242 BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64); 243 BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); 244 BUILD_BUG_ON(sizeof(struct nvme_features) != 64); 245 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64); 246 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64); 247 BUILD_BUG_ON(sizeof(struct nvme_command) != 64); 248 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != NVME_IDENTIFY_DATA_SIZE); 249 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != NVME_IDENTIFY_DATA_SIZE); 250 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64); 251 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512); 252 BUILD_BUG_ON(sizeof(struct nvme_dbbuf) != 64); 253 } 254 255 static unsigned int max_io_queues(void) 256 { 257 return num_possible_cpus() + write_queues + poll_queues; 258 } 259 260 static unsigned int max_queue_count(void) 261 { 262 /* IO queues + admin queue */ 263 return 1 + max_io_queues(); 264 } 265 266 static inline unsigned int nvme_dbbuf_size(u32 stride) 267 { 268 return (max_queue_count() * 8 * stride); 269 } 270 271 static int nvme_dbbuf_dma_alloc(struct nvme_dev *dev) 272 { 273 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride); 274 275 if (dev->dbbuf_dbs) 276 return 0; 277 278 dev->dbbuf_dbs = dma_alloc_coherent(dev->dev, mem_size, 279 &dev->dbbuf_dbs_dma_addr, 280 GFP_KERNEL); 281 if (!dev->dbbuf_dbs) 282 return -ENOMEM; 283 dev->dbbuf_eis = dma_alloc_coherent(dev->dev, mem_size, 284 &dev->dbbuf_eis_dma_addr, 285 GFP_KERNEL); 286 if (!dev->dbbuf_eis) { 287 dma_free_coherent(dev->dev, mem_size, 288 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr); 289 dev->dbbuf_dbs = NULL; 290 return -ENOMEM; 291 } 292 293 return 0; 294 } 295 296 static void nvme_dbbuf_dma_free(struct nvme_dev *dev) 297 { 298 unsigned int mem_size = nvme_dbbuf_size(dev->db_stride); 299 300 if (dev->dbbuf_dbs) { 301 dma_free_coherent(dev->dev, mem_size, 302 dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr); 303 dev->dbbuf_dbs = NULL; 304 } 305 if (dev->dbbuf_eis) { 306 dma_free_coherent(dev->dev, mem_size, 307 dev->dbbuf_eis, dev->dbbuf_eis_dma_addr); 308 dev->dbbuf_eis = NULL; 309 } 310 } 311 312 static void nvme_dbbuf_init(struct nvme_dev *dev, 313 struct nvme_queue *nvmeq, int qid) 314 { 315 if (!dev->dbbuf_dbs || !qid) 316 return; 317 318 nvmeq->dbbuf_sq_db = &dev->dbbuf_dbs[sq_idx(qid, dev->db_stride)]; 319 nvmeq->dbbuf_cq_db = &dev->dbbuf_dbs[cq_idx(qid, dev->db_stride)]; 320 nvmeq->dbbuf_sq_ei = &dev->dbbuf_eis[sq_idx(qid, dev->db_stride)]; 321 nvmeq->dbbuf_cq_ei = &dev->dbbuf_eis[cq_idx(qid, dev->db_stride)]; 322 } 323 324 static void nvme_dbbuf_set(struct nvme_dev *dev) 325 { 326 struct nvme_command c; 327 328 if (!dev->dbbuf_dbs) 329 return; 330 331 memset(&c, 0, sizeof(c)); 332 c.dbbuf.opcode = nvme_admin_dbbuf; 333 c.dbbuf.prp1 = cpu_to_le64(dev->dbbuf_dbs_dma_addr); 334 c.dbbuf.prp2 = cpu_to_le64(dev->dbbuf_eis_dma_addr); 335 336 if (nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0)) { 337 dev_warn(dev->ctrl.device, "unable to set dbbuf\n"); 338 /* Free memory and continue on */ 339 nvme_dbbuf_dma_free(dev); 340 } 341 } 342 343 static inline int nvme_dbbuf_need_event(u16 event_idx, u16 new_idx, u16 old) 344 { 345 return (u16)(new_idx - event_idx - 1) < (u16)(new_idx - old); 346 } 347 348 /* Update dbbuf and return true if an MMIO is required */ 349 static bool nvme_dbbuf_update_and_check_event(u16 value, u32 *dbbuf_db, 350 volatile u32 *dbbuf_ei) 351 { 352 if (dbbuf_db) { 353 u16 old_value; 354 355 /* 356 * Ensure that the queue is written before updating 357 * the doorbell in memory 358 */ 359 wmb(); 360 361 old_value = *dbbuf_db; 362 *dbbuf_db = value; 363 364 /* 365 * Ensure that the doorbell is updated before reading the event 366 * index from memory. The controller needs to provide similar 367 * ordering to ensure the envent index is updated before reading 368 * the doorbell. 369 */ 370 mb(); 371 372 if (!nvme_dbbuf_need_event(*dbbuf_ei, value, old_value)) 373 return false; 374 } 375 376 return true; 377 } 378 379 /* 380 * Max size of iod being embedded in the request payload 381 */ 382 #define NVME_INT_PAGES 2 383 #define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size) 384 385 /* 386 * Will slightly overestimate the number of pages needed. This is OK 387 * as it only leads to a small amount of wasted memory for the lifetime of 388 * the I/O. 389 */ 390 static int nvme_npages(unsigned size, struct nvme_dev *dev) 391 { 392 unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size, 393 dev->ctrl.page_size); 394 return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8); 395 } 396 397 /* 398 * Calculates the number of pages needed for the SGL segments. For example a 4k 399 * page can accommodate 256 SGL descriptors. 400 */ 401 static int nvme_pci_npages_sgl(unsigned int num_seg) 402 { 403 return DIV_ROUND_UP(num_seg * sizeof(struct nvme_sgl_desc), PAGE_SIZE); 404 } 405 406 static unsigned int nvme_pci_iod_alloc_size(struct nvme_dev *dev, 407 unsigned int size, unsigned int nseg, bool use_sgl) 408 { 409 size_t alloc_size; 410 411 if (use_sgl) 412 alloc_size = sizeof(__le64 *) * nvme_pci_npages_sgl(nseg); 413 else 414 alloc_size = sizeof(__le64 *) * nvme_npages(size, dev); 415 416 return alloc_size + sizeof(struct scatterlist) * nseg; 417 } 418 419 static unsigned int nvme_pci_cmd_size(struct nvme_dev *dev, bool use_sgl) 420 { 421 unsigned int alloc_size = nvme_pci_iod_alloc_size(dev, 422 NVME_INT_BYTES(dev), NVME_INT_PAGES, 423 use_sgl); 424 425 return sizeof(struct nvme_iod) + alloc_size; 426 } 427 428 static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, 429 unsigned int hctx_idx) 430 { 431 struct nvme_dev *dev = data; 432 struct nvme_queue *nvmeq = &dev->queues[0]; 433 434 WARN_ON(hctx_idx != 0); 435 WARN_ON(dev->admin_tagset.tags[0] != hctx->tags); 436 WARN_ON(nvmeq->tags); 437 438 hctx->driver_data = nvmeq; 439 nvmeq->tags = &dev->admin_tagset.tags[0]; 440 return 0; 441 } 442 443 static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) 444 { 445 struct nvme_queue *nvmeq = hctx->driver_data; 446 447 nvmeq->tags = NULL; 448 } 449 450 static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, 451 unsigned int hctx_idx) 452 { 453 struct nvme_dev *dev = data; 454 struct nvme_queue *nvmeq = &dev->queues[hctx_idx + 1]; 455 456 if (!nvmeq->tags) 457 nvmeq->tags = &dev->tagset.tags[hctx_idx]; 458 459 WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags); 460 hctx->driver_data = nvmeq; 461 return 0; 462 } 463 464 static int nvme_init_request(struct blk_mq_tag_set *set, struct request *req, 465 unsigned int hctx_idx, unsigned int numa_node) 466 { 467 struct nvme_dev *dev = set->driver_data; 468 struct nvme_iod *iod = blk_mq_rq_to_pdu(req); 469 int queue_idx = (set == &dev->tagset) ? hctx_idx + 1 : 0; 470 struct nvme_queue *nvmeq = &dev->queues[queue_idx]; 471 472 BUG_ON(!nvmeq); 473 iod->nvmeq = nvmeq; 474 475 nvme_req(req)->ctrl = &dev->ctrl; 476 return 0; 477 } 478 479 static int queue_irq_offset(struct nvme_dev *dev) 480 { 481 /* if we have more than 1 vec, admin queue offsets us by 1 */ 482 if (dev->num_vecs > 1) 483 return 1; 484 485 return 0; 486 } 487 488 static int nvme_pci_map_queues(struct blk_mq_tag_set *set) 489 { 490 struct nvme_dev *dev = set->driver_data; 491 int i, qoff, offset; 492 493 offset = queue_irq_offset(dev); 494 for (i = 0, qoff = 0; i < set->nr_maps; i++) { 495 struct blk_mq_queue_map *map = &set->map[i]; 496 497 map->nr_queues = dev->io_queues[i]; 498 if (!map->nr_queues) { 499 BUG_ON(i == HCTX_TYPE_DEFAULT); 500 continue; 501 } 502 503 /* 504 * The poll queue(s) doesn't have an IRQ (and hence IRQ 505 * affinity), so use the regular blk-mq cpu mapping 506 */ 507 map->queue_offset = qoff; 508 if (i != HCTX_TYPE_POLL) 509 blk_mq_pci_map_queues(map, to_pci_dev(dev->dev), offset); 510 else 511 blk_mq_map_queues(map); 512 qoff += map->nr_queues; 513 offset += map->nr_queues; 514 } 515 516 return 0; 517 } 518 519 /* 520 * Write sq tail if we are asked to, or if the next command would wrap. 521 */ 522 static inline void nvme_write_sq_db(struct nvme_queue *nvmeq, bool write_sq) 523 { 524 if (!write_sq) { 525 u16 next_tail = nvmeq->sq_tail + 1; 526 527 if (next_tail == nvmeq->q_depth) 528 next_tail = 0; 529 if (next_tail != nvmeq->last_sq_tail) 530 return; 531 } 532 533 if (nvme_dbbuf_update_and_check_event(nvmeq->sq_tail, 534 nvmeq->dbbuf_sq_db, nvmeq->dbbuf_sq_ei)) 535 writel(nvmeq->sq_tail, nvmeq->q_db); 536 nvmeq->last_sq_tail = nvmeq->sq_tail; 537 } 538 539 /** 540 * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell 541 * @nvmeq: The queue to use 542 * @cmd: The command to send 543 * @write_sq: whether to write to the SQ doorbell 544 */ 545 static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd, 546 bool write_sq) 547 { 548 spin_lock(&nvmeq->sq_lock); 549 memcpy(&nvmeq->sq_cmds[nvmeq->sq_tail], cmd, sizeof(*cmd)); 550 if (++nvmeq->sq_tail == nvmeq->q_depth) 551 nvmeq->sq_tail = 0; 552 nvme_write_sq_db(nvmeq, write_sq); 553 spin_unlock(&nvmeq->sq_lock); 554 } 555 556 static void nvme_commit_rqs(struct blk_mq_hw_ctx *hctx) 557 { 558 struct nvme_queue *nvmeq = hctx->driver_data; 559 560 spin_lock(&nvmeq->sq_lock); 561 if (nvmeq->sq_tail != nvmeq->last_sq_tail) 562 nvme_write_sq_db(nvmeq, true); 563 spin_unlock(&nvmeq->sq_lock); 564 } 565 566 static void **nvme_pci_iod_list(struct request *req) 567 { 568 struct nvme_iod *iod = blk_mq_rq_to_pdu(req); 569 return (void **)(iod->sg + blk_rq_nr_phys_segments(req)); 570 } 571 572 static inline bool nvme_pci_use_sgls(struct nvme_dev *dev, struct request *req) 573 { 574 struct nvme_iod *iod = blk_mq_rq_to_pdu(req); 575 int nseg = blk_rq_nr_phys_segments(req); 576 unsigned int avg_seg_size; 577 578 if (nseg == 0) 579 return false; 580 581 avg_seg_size = DIV_ROUND_UP(blk_rq_payload_bytes(req), nseg); 582 583 if (!(dev->ctrl.sgls & ((1 << 0) | (1 << 1)))) 584 return false; 585 if (!iod->nvmeq->qid) 586 return false; 587 if (!sgl_threshold || avg_seg_size < sgl_threshold) 588 return false; 589 return true; 590 } 591 592 static blk_status_t nvme_init_iod(struct request *rq, struct nvme_dev *dev) 593 { 594 struct nvme_iod *iod = blk_mq_rq_to_pdu(rq); 595 int nseg = blk_rq_nr_phys_segments(rq); 596 unsigned int size = blk_rq_payload_bytes(rq); 597 598 iod->use_sgl = nvme_pci_use_sgls(dev, rq); 599 600 if (nseg > NVME_INT_PAGES || size > NVME_INT_BYTES(dev)) { 601 iod->sg = mempool_alloc(dev->iod_mempool, GFP_ATOMIC); 602 if (!iod->sg) 603 return BLK_STS_RESOURCE; 604 } else { 605 iod->sg = iod->inline_sg; 606 } 607 608 iod->aborted = 0; 609 iod->npages = -1; 610 iod->nents = 0; 611 iod->length = size; 612 613 return BLK_STS_OK; 614 } 615 616 static void nvme_free_iod(struct nvme_dev *dev, struct request *req) 617 { 618 struct nvme_iod *iod = blk_mq_rq_to_pdu(req); 619 const int last_prp = dev->ctrl.page_size / sizeof(__le64) - 1; 620 dma_addr_t dma_addr = iod->first_dma, next_dma_addr; 621 622 int i; 623 624 if (iod->npages == 0) 625 dma_pool_free(dev->prp_small_pool, nvme_pci_iod_list(req)[0], 626 dma_addr); 627 628 for (i = 0; i < iod->npages; i++) { 629 void *addr = nvme_pci_iod_list(req)[i]; 630 631 if (iod->use_sgl) { 632 struct nvme_sgl_desc *sg_list = addr; 633 634 next_dma_addr = 635 le64_to_cpu((sg_list[SGES_PER_PAGE - 1]).addr); 636 } else { 637 __le64 *prp_list = addr; 638 639 next_dma_addr = le64_to_cpu(prp_list[last_prp]); 640 } 641 642 dma_pool_free(dev->prp_page_pool, addr, dma_addr); 643 dma_addr = next_dma_addr; 644 } 645 646 if (iod->sg != iod->inline_sg) 647 mempool_free(iod->sg, dev->iod_mempool); 648 } 649 650 static void nvme_print_sgl(struct scatterlist *sgl, int nents) 651 { 652 int i; 653 struct scatterlist *sg; 654 655 for_each_sg(sgl, sg, nents, i) { 656 dma_addr_t phys = sg_phys(sg); 657 pr_warn("sg[%d] phys_addr:%pad offset:%d length:%d " 658 "dma_address:%pad dma_length:%d\n", 659 i, &phys, sg->offset, sg->length, &sg_dma_address(sg), 660 sg_dma_len(sg)); 661 } 662 } 663 664 static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev, 665 struct request *req, struct nvme_rw_command *cmnd) 666 { 667 struct nvme_iod *iod = blk_mq_rq_to_pdu(req); 668 struct dma_pool *pool; 669 int length = blk_rq_payload_bytes(req); 670 struct scatterlist *sg = iod->sg; 671 int dma_len = sg_dma_len(sg); 672 u64 dma_addr = sg_dma_address(sg); 673 u32 page_size = dev->ctrl.page_size; 674 int offset = dma_addr & (page_size - 1); 675 __le64 *prp_list; 676 void **list = nvme_pci_iod_list(req); 677 dma_addr_t prp_dma; 678 int nprps, i; 679 680 length -= (page_size - offset); 681 if (length <= 0) { 682 iod->first_dma = 0; 683 goto done; 684 } 685 686 dma_len -= (page_size - offset); 687 if (dma_len) { 688 dma_addr += (page_size - offset); 689 } else { 690 sg = sg_next(sg); 691 dma_addr = sg_dma_address(sg); 692 dma_len = sg_dma_len(sg); 693 } 694 695 if (length <= page_size) { 696 iod->first_dma = dma_addr; 697 goto done; 698 } 699 700 nprps = DIV_ROUND_UP(length, page_size); 701 if (nprps <= (256 / 8)) { 702 pool = dev->prp_small_pool; 703 iod->npages = 0; 704 } else { 705 pool = dev->prp_page_pool; 706 iod->npages = 1; 707 } 708 709 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma); 710 if (!prp_list) { 711 iod->first_dma = dma_addr; 712 iod->npages = -1; 713 return BLK_STS_RESOURCE; 714 } 715 list[0] = prp_list; 716 iod->first_dma = prp_dma; 717 i = 0; 718 for (;;) { 719 if (i == page_size >> 3) { 720 __le64 *old_prp_list = prp_list; 721 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma); 722 if (!prp_list) 723 return BLK_STS_RESOURCE; 724 list[iod->npages++] = prp_list; 725 prp_list[0] = old_prp_list[i - 1]; 726 old_prp_list[i - 1] = cpu_to_le64(prp_dma); 727 i = 1; 728 } 729 prp_list[i++] = cpu_to_le64(dma_addr); 730 dma_len -= page_size; 731 dma_addr += page_size; 732 length -= page_size; 733 if (length <= 0) 734 break; 735 if (dma_len > 0) 736 continue; 737 if (unlikely(dma_len < 0)) 738 goto bad_sgl; 739 sg = sg_next(sg); 740 dma_addr = sg_dma_address(sg); 741 dma_len = sg_dma_len(sg); 742 } 743 744 done: 745 cmnd->dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sg)); 746 cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma); 747 748 return BLK_STS_OK; 749 750 bad_sgl: 751 WARN(DO_ONCE(nvme_print_sgl, iod->sg, iod->nents), 752 "Invalid SGL for payload:%d nents:%d\n", 753 blk_rq_payload_bytes(req), iod->nents); 754 return BLK_STS_IOERR; 755 } 756 757 static void nvme_pci_sgl_set_data(struct nvme_sgl_desc *sge, 758 struct scatterlist *sg) 759 { 760 sge->addr = cpu_to_le64(sg_dma_address(sg)); 761 sge->length = cpu_to_le32(sg_dma_len(sg)); 762 sge->type = NVME_SGL_FMT_DATA_DESC << 4; 763 } 764 765 static void nvme_pci_sgl_set_seg(struct nvme_sgl_desc *sge, 766 dma_addr_t dma_addr, int entries) 767 { 768 sge->addr = cpu_to_le64(dma_addr); 769 if (entries < SGES_PER_PAGE) { 770 sge->length = cpu_to_le32(entries * sizeof(*sge)); 771 sge->type = NVME_SGL_FMT_LAST_SEG_DESC << 4; 772 } else { 773 sge->length = cpu_to_le32(PAGE_SIZE); 774 sge->type = NVME_SGL_FMT_SEG_DESC << 4; 775 } 776 } 777 778 static blk_status_t nvme_pci_setup_sgls(struct nvme_dev *dev, 779 struct request *req, struct nvme_rw_command *cmd, int entries) 780 { 781 struct nvme_iod *iod = blk_mq_rq_to_pdu(req); 782 struct dma_pool *pool; 783 struct nvme_sgl_desc *sg_list; 784 struct scatterlist *sg = iod->sg; 785 dma_addr_t sgl_dma; 786 int i = 0; 787 788 /* setting the transfer type as SGL */ 789 cmd->flags = NVME_CMD_SGL_METABUF; 790 791 if (entries == 1) { 792 nvme_pci_sgl_set_data(&cmd->dptr.sgl, sg); 793 return BLK_STS_OK; 794 } 795 796 if (entries <= (256 / sizeof(struct nvme_sgl_desc))) { 797 pool = dev->prp_small_pool; 798 iod->npages = 0; 799 } else { 800 pool = dev->prp_page_pool; 801 iod->npages = 1; 802 } 803 804 sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma); 805 if (!sg_list) { 806 iod->npages = -1; 807 return BLK_STS_RESOURCE; 808 } 809 810 nvme_pci_iod_list(req)[0] = sg_list; 811 iod->first_dma = sgl_dma; 812 813 nvme_pci_sgl_set_seg(&cmd->dptr.sgl, sgl_dma, entries); 814 815 do { 816 if (i == SGES_PER_PAGE) { 817 struct nvme_sgl_desc *old_sg_desc = sg_list; 818 struct nvme_sgl_desc *link = &old_sg_desc[i - 1]; 819 820 sg_list = dma_pool_alloc(pool, GFP_ATOMIC, &sgl_dma); 821 if (!sg_list) 822 return BLK_STS_RESOURCE; 823 824 i = 0; 825 nvme_pci_iod_list(req)[iod->npages++] = sg_list; 826 sg_list[i++] = *link; 827 nvme_pci_sgl_set_seg(link, sgl_dma, entries); 828 } 829 830 nvme_pci_sgl_set_data(&sg_list[i++], sg); 831 sg = sg_next(sg); 832 } while (--entries > 0); 833 834 return BLK_STS_OK; 835 } 836 837 static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req, 838 struct nvme_command *cmnd) 839 { 840 struct nvme_iod *iod = blk_mq_rq_to_pdu(req); 841 struct request_queue *q = req->q; 842 enum dma_data_direction dma_dir = rq_data_dir(req) ? 843 DMA_TO_DEVICE : DMA_FROM_DEVICE; 844 blk_status_t ret = BLK_STS_IOERR; 845 int nr_mapped; 846 847 sg_init_table(iod->sg, blk_rq_nr_phys_segments(req)); 848 iod->nents = blk_rq_map_sg(q, req, iod->sg); 849 if (!iod->nents) 850 goto out; 851 852 ret = BLK_STS_RESOURCE; 853 854 if (is_pci_p2pdma_page(sg_page(iod->sg))) 855 nr_mapped = pci_p2pdma_map_sg(dev->dev, iod->sg, iod->nents, 856 dma_dir); 857 else 858 nr_mapped = dma_map_sg_attrs(dev->dev, iod->sg, iod->nents, 859 dma_dir, DMA_ATTR_NO_WARN); 860 if (!nr_mapped) 861 goto out; 862 863 if (iod->use_sgl) 864 ret = nvme_pci_setup_sgls(dev, req, &cmnd->rw, nr_mapped); 865 else 866 ret = nvme_pci_setup_prps(dev, req, &cmnd->rw); 867 868 if (ret != BLK_STS_OK) 869 goto out_unmap; 870 871 ret = BLK_STS_IOERR; 872 if (blk_integrity_rq(req)) { 873 if (blk_rq_count_integrity_sg(q, req->bio) != 1) 874 goto out_unmap; 875 876 sg_init_table(&iod->meta_sg, 1); 877 if (blk_rq_map_integrity_sg(q, req->bio, &iod->meta_sg) != 1) 878 goto out_unmap; 879 880 if (!dma_map_sg(dev->dev, &iod->meta_sg, 1, dma_dir)) 881 goto out_unmap; 882 883 cmnd->rw.metadata = cpu_to_le64(sg_dma_address(&iod->meta_sg)); 884 } 885 886 return BLK_STS_OK; 887 888 out_unmap: 889 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir); 890 out: 891 return ret; 892 } 893 894 static void nvme_unmap_data(struct nvme_dev *dev, struct request *req) 895 { 896 struct nvme_iod *iod = blk_mq_rq_to_pdu(req); 897 enum dma_data_direction dma_dir = rq_data_dir(req) ? 898 DMA_TO_DEVICE : DMA_FROM_DEVICE; 899 900 if (iod->nents) { 901 /* P2PDMA requests do not need to be unmapped */ 902 if (!is_pci_p2pdma_page(sg_page(iod->sg))) 903 dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir); 904 905 if (blk_integrity_rq(req)) 906 dma_unmap_sg(dev->dev, &iod->meta_sg, 1, dma_dir); 907 } 908 909 nvme_cleanup_cmd(req); 910 nvme_free_iod(dev, req); 911 } 912 913 /* 914 * NOTE: ns is NULL when called on the admin queue. 915 */ 916 static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx, 917 const struct blk_mq_queue_data *bd) 918 { 919 struct nvme_ns *ns = hctx->queue->queuedata; 920 struct nvme_queue *nvmeq = hctx->driver_data; 921 struct nvme_dev *dev = nvmeq->dev; 922 struct request *req = bd->rq; 923 struct nvme_command cmnd; 924 blk_status_t ret; 925 926 /* 927 * We should not need to do this, but we're still using this to 928 * ensure we can drain requests on a dying queue. 929 */ 930 if (unlikely(!test_bit(NVMEQ_ENABLED, &nvmeq->flags))) 931 return BLK_STS_IOERR; 932 933 ret = nvme_setup_cmd(ns, req, &cmnd); 934 if (ret) 935 return ret; 936 937 ret = nvme_init_iod(req, dev); 938 if (ret) 939 goto out_free_cmd; 940 941 if (blk_rq_nr_phys_segments(req)) { 942 ret = nvme_map_data(dev, req, &cmnd); 943 if (ret) 944 goto out_cleanup_iod; 945 } 946 947 blk_mq_start_request(req); 948 nvme_submit_cmd(nvmeq, &cmnd, bd->last); 949 return BLK_STS_OK; 950 out_cleanup_iod: 951 nvme_free_iod(dev, req); 952 out_free_cmd: 953 nvme_cleanup_cmd(req); 954 return ret; 955 } 956 957 static void nvme_pci_complete_rq(struct request *req) 958 { 959 struct nvme_iod *iod = blk_mq_rq_to_pdu(req); 960 961 nvme_unmap_data(iod->nvmeq->dev, req); 962 nvme_complete_rq(req); 963 } 964 965 /* We read the CQE phase first to check if the rest of the entry is valid */ 966 static inline bool nvme_cqe_pending(struct nvme_queue *nvmeq) 967 { 968 return (le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) == 969 nvmeq->cq_phase; 970 } 971 972 static inline void nvme_ring_cq_doorbell(struct nvme_queue *nvmeq) 973 { 974 u16 head = nvmeq->cq_head; 975 976 if (nvme_dbbuf_update_and_check_event(head, nvmeq->dbbuf_cq_db, 977 nvmeq->dbbuf_cq_ei)) 978 writel(head, nvmeq->q_db + nvmeq->dev->db_stride); 979 } 980 981 static inline void nvme_handle_cqe(struct nvme_queue *nvmeq, u16 idx) 982 { 983 volatile struct nvme_completion *cqe = &nvmeq->cqes[idx]; 984 struct request *req; 985 986 if (unlikely(cqe->command_id >= nvmeq->q_depth)) { 987 dev_warn(nvmeq->dev->ctrl.device, 988 "invalid id %d completed on queue %d\n", 989 cqe->command_id, le16_to_cpu(cqe->sq_id)); 990 return; 991 } 992 993 /* 994 * AEN requests are special as they don't time out and can 995 * survive any kind of queue freeze and often don't respond to 996 * aborts. We don't even bother to allocate a struct request 997 * for them but rather special case them here. 998 */ 999 if (unlikely(nvmeq->qid == 0 && 1000 cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH)) { 1001 nvme_complete_async_event(&nvmeq->dev->ctrl, 1002 cqe->status, &cqe->result); 1003 return; 1004 } 1005 1006 req = blk_mq_tag_to_rq(*nvmeq->tags, cqe->command_id); 1007 trace_nvme_sq(req, cqe->sq_head, nvmeq->sq_tail); 1008 nvme_end_request(req, cqe->status, cqe->result); 1009 } 1010 1011 static void nvme_complete_cqes(struct nvme_queue *nvmeq, u16 start, u16 end) 1012 { 1013 while (start != end) { 1014 nvme_handle_cqe(nvmeq, start); 1015 if (++start == nvmeq->q_depth) 1016 start = 0; 1017 } 1018 } 1019 1020 static inline void nvme_update_cq_head(struct nvme_queue *nvmeq) 1021 { 1022 if (++nvmeq->cq_head == nvmeq->q_depth) { 1023 nvmeq->cq_head = 0; 1024 nvmeq->cq_phase = !nvmeq->cq_phase; 1025 } 1026 } 1027 1028 static inline int nvme_process_cq(struct nvme_queue *nvmeq, u16 *start, 1029 u16 *end, unsigned int tag) 1030 { 1031 int found = 0; 1032 1033 *start = nvmeq->cq_head; 1034 while (nvme_cqe_pending(nvmeq)) { 1035 if (tag == -1U || nvmeq->cqes[nvmeq->cq_head].command_id == tag) 1036 found++; 1037 nvme_update_cq_head(nvmeq); 1038 } 1039 *end = nvmeq->cq_head; 1040 1041 if (*start != *end) 1042 nvme_ring_cq_doorbell(nvmeq); 1043 return found; 1044 } 1045 1046 static irqreturn_t nvme_irq(int irq, void *data) 1047 { 1048 struct nvme_queue *nvmeq = data; 1049 irqreturn_t ret = IRQ_NONE; 1050 u16 start, end; 1051 1052 /* 1053 * The rmb/wmb pair ensures we see all updates from a previous run of 1054 * the irq handler, even if that was on another CPU. 1055 */ 1056 rmb(); 1057 if (nvmeq->cq_head != nvmeq->last_cq_head) 1058 ret = IRQ_HANDLED; 1059 nvme_process_cq(nvmeq, &start, &end, -1); 1060 nvmeq->last_cq_head = nvmeq->cq_head; 1061 wmb(); 1062 1063 if (start != end) { 1064 nvme_complete_cqes(nvmeq, start, end); 1065 return IRQ_HANDLED; 1066 } 1067 1068 return ret; 1069 } 1070 1071 static irqreturn_t nvme_irq_check(int irq, void *data) 1072 { 1073 struct nvme_queue *nvmeq = data; 1074 if (nvme_cqe_pending(nvmeq)) 1075 return IRQ_WAKE_THREAD; 1076 return IRQ_NONE; 1077 } 1078 1079 /* 1080 * Poll for completions any queue, including those not dedicated to polling. 1081 * Can be called from any context. 1082 */ 1083 static int nvme_poll_irqdisable(struct nvme_queue *nvmeq, unsigned int tag) 1084 { 1085 struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev); 1086 u16 start, end; 1087 int found; 1088 1089 /* 1090 * For a poll queue we need to protect against the polling thread 1091 * using the CQ lock. For normal interrupt driven threads we have 1092 * to disable the interrupt to avoid racing with it. 1093 */ 1094 if (nvmeq->cq_vector == -1) { 1095 spin_lock(&nvmeq->cq_poll_lock); 1096 found = nvme_process_cq(nvmeq, &start, &end, tag); 1097 spin_unlock(&nvmeq->cq_poll_lock); 1098 } else { 1099 disable_irq(pci_irq_vector(pdev, nvmeq->cq_vector)); 1100 found = nvme_process_cq(nvmeq, &start, &end, tag); 1101 enable_irq(pci_irq_vector(pdev, nvmeq->cq_vector)); 1102 } 1103 1104 nvme_complete_cqes(nvmeq, start, end); 1105 return found; 1106 } 1107 1108 static int nvme_poll(struct blk_mq_hw_ctx *hctx) 1109 { 1110 struct nvme_queue *nvmeq = hctx->driver_data; 1111 u16 start, end; 1112 bool found; 1113 1114 if (!nvme_cqe_pending(nvmeq)) 1115 return 0; 1116 1117 spin_lock(&nvmeq->cq_poll_lock); 1118 found = nvme_process_cq(nvmeq, &start, &end, -1); 1119 spin_unlock(&nvmeq->cq_poll_lock); 1120 1121 nvme_complete_cqes(nvmeq, start, end); 1122 return found; 1123 } 1124 1125 static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl) 1126 { 1127 struct nvme_dev *dev = to_nvme_dev(ctrl); 1128 struct nvme_queue *nvmeq = &dev->queues[0]; 1129 struct nvme_command c; 1130 1131 memset(&c, 0, sizeof(c)); 1132 c.common.opcode = nvme_admin_async_event; 1133 c.common.command_id = NVME_AQ_BLK_MQ_DEPTH; 1134 nvme_submit_cmd(nvmeq, &c, true); 1135 } 1136 1137 static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id) 1138 { 1139 struct nvme_command c; 1140 1141 memset(&c, 0, sizeof(c)); 1142 c.delete_queue.opcode = opcode; 1143 c.delete_queue.qid = cpu_to_le16(id); 1144 1145 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0); 1146 } 1147 1148 static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid, 1149 struct nvme_queue *nvmeq, s16 vector) 1150 { 1151 struct nvme_command c; 1152 int flags = NVME_QUEUE_PHYS_CONTIG; 1153 1154 if (vector != -1) 1155 flags |= NVME_CQ_IRQ_ENABLED; 1156 1157 /* 1158 * Note: we (ab)use the fact that the prp fields survive if no data 1159 * is attached to the request. 1160 */ 1161 memset(&c, 0, sizeof(c)); 1162 c.create_cq.opcode = nvme_admin_create_cq; 1163 c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr); 1164 c.create_cq.cqid = cpu_to_le16(qid); 1165 c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1); 1166 c.create_cq.cq_flags = cpu_to_le16(flags); 1167 if (vector != -1) 1168 c.create_cq.irq_vector = cpu_to_le16(vector); 1169 else 1170 c.create_cq.irq_vector = 0; 1171 1172 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0); 1173 } 1174 1175 static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid, 1176 struct nvme_queue *nvmeq) 1177 { 1178 struct nvme_ctrl *ctrl = &dev->ctrl; 1179 struct nvme_command c; 1180 int flags = NVME_QUEUE_PHYS_CONTIG; 1181 1182 /* 1183 * Some drives have a bug that auto-enables WRRU if MEDIUM isn't 1184 * set. Since URGENT priority is zeroes, it makes all queues 1185 * URGENT. 1186 */ 1187 if (ctrl->quirks & NVME_QUIRK_MEDIUM_PRIO_SQ) 1188 flags |= NVME_SQ_PRIO_MEDIUM; 1189 1190 /* 1191 * Note: we (ab)use the fact that the prp fields survive if no data 1192 * is attached to the request. 1193 */ 1194 memset(&c, 0, sizeof(c)); 1195 c.create_sq.opcode = nvme_admin_create_sq; 1196 c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr); 1197 c.create_sq.sqid = cpu_to_le16(qid); 1198 c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1); 1199 c.create_sq.sq_flags = cpu_to_le16(flags); 1200 c.create_sq.cqid = cpu_to_le16(qid); 1201 1202 return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0); 1203 } 1204 1205 static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid) 1206 { 1207 return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid); 1208 } 1209 1210 static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid) 1211 { 1212 return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid); 1213 } 1214 1215 static void abort_endio(struct request *req, blk_status_t error) 1216 { 1217 struct nvme_iod *iod = blk_mq_rq_to_pdu(req); 1218 struct nvme_queue *nvmeq = iod->nvmeq; 1219 1220 dev_warn(nvmeq->dev->ctrl.device, 1221 "Abort status: 0x%x", nvme_req(req)->status); 1222 atomic_inc(&nvmeq->dev->ctrl.abort_limit); 1223 blk_mq_free_request(req); 1224 } 1225 1226 static bool nvme_should_reset(struct nvme_dev *dev, u32 csts) 1227 { 1228 1229 /* If true, indicates loss of adapter communication, possibly by a 1230 * NVMe Subsystem reset. 1231 */ 1232 bool nssro = dev->subsystem && (csts & NVME_CSTS_NSSRO); 1233 1234 /* If there is a reset/reinit ongoing, we shouldn't reset again. */ 1235 switch (dev->ctrl.state) { 1236 case NVME_CTRL_RESETTING: 1237 case NVME_CTRL_CONNECTING: 1238 return false; 1239 default: 1240 break; 1241 } 1242 1243 /* We shouldn't reset unless the controller is on fatal error state 1244 * _or_ if we lost the communication with it. 1245 */ 1246 if (!(csts & NVME_CSTS_CFS) && !nssro) 1247 return false; 1248 1249 return true; 1250 } 1251 1252 static void nvme_warn_reset(struct nvme_dev *dev, u32 csts) 1253 { 1254 /* Read a config register to help see what died. */ 1255 u16 pci_status; 1256 int result; 1257 1258 result = pci_read_config_word(to_pci_dev(dev->dev), PCI_STATUS, 1259 &pci_status); 1260 if (result == PCIBIOS_SUCCESSFUL) 1261 dev_warn(dev->ctrl.device, 1262 "controller is down; will reset: CSTS=0x%x, PCI_STATUS=0x%hx\n", 1263 csts, pci_status); 1264 else 1265 dev_warn(dev->ctrl.device, 1266 "controller is down; will reset: CSTS=0x%x, PCI_STATUS read failed (%d)\n", 1267 csts, result); 1268 } 1269 1270 static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved) 1271 { 1272 struct nvme_iod *iod = blk_mq_rq_to_pdu(req); 1273 struct nvme_queue *nvmeq = iod->nvmeq; 1274 struct nvme_dev *dev = nvmeq->dev; 1275 struct request *abort_req; 1276 struct nvme_command cmd; 1277 u32 csts = readl(dev->bar + NVME_REG_CSTS); 1278 1279 /* If PCI error recovery process is happening, we cannot reset or 1280 * the recovery mechanism will surely fail. 1281 */ 1282 mb(); 1283 if (pci_channel_offline(to_pci_dev(dev->dev))) 1284 return BLK_EH_RESET_TIMER; 1285 1286 /* 1287 * Reset immediately if the controller is failed 1288 */ 1289 if (nvme_should_reset(dev, csts)) { 1290 nvme_warn_reset(dev, csts); 1291 nvme_dev_disable(dev, false); 1292 nvme_reset_ctrl(&dev->ctrl); 1293 return BLK_EH_DONE; 1294 } 1295 1296 /* 1297 * Did we miss an interrupt? 1298 */ 1299 if (nvme_poll_irqdisable(nvmeq, req->tag)) { 1300 dev_warn(dev->ctrl.device, 1301 "I/O %d QID %d timeout, completion polled\n", 1302 req->tag, nvmeq->qid); 1303 return BLK_EH_DONE; 1304 } 1305 1306 /* 1307 * Shutdown immediately if controller times out while starting. The 1308 * reset work will see the pci device disabled when it gets the forced 1309 * cancellation error. All outstanding requests are completed on 1310 * shutdown, so we return BLK_EH_DONE. 1311 */ 1312 switch (dev->ctrl.state) { 1313 case NVME_CTRL_CONNECTING: 1314 case NVME_CTRL_RESETTING: 1315 dev_warn_ratelimited(dev->ctrl.device, 1316 "I/O %d QID %d timeout, disable controller\n", 1317 req->tag, nvmeq->qid); 1318 nvme_dev_disable(dev, false); 1319 nvme_req(req)->flags |= NVME_REQ_CANCELLED; 1320 return BLK_EH_DONE; 1321 default: 1322 break; 1323 } 1324 1325 /* 1326 * Shutdown the controller immediately and schedule a reset if the 1327 * command was already aborted once before and still hasn't been 1328 * returned to the driver, or if this is the admin queue. 1329 */ 1330 if (!nvmeq->qid || iod->aborted) { 1331 dev_warn(dev->ctrl.device, 1332 "I/O %d QID %d timeout, reset controller\n", 1333 req->tag, nvmeq->qid); 1334 nvme_dev_disable(dev, false); 1335 nvme_reset_ctrl(&dev->ctrl); 1336 1337 nvme_req(req)->flags |= NVME_REQ_CANCELLED; 1338 return BLK_EH_DONE; 1339 } 1340 1341 if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) { 1342 atomic_inc(&dev->ctrl.abort_limit); 1343 return BLK_EH_RESET_TIMER; 1344 } 1345 iod->aborted = 1; 1346 1347 memset(&cmd, 0, sizeof(cmd)); 1348 cmd.abort.opcode = nvme_admin_abort_cmd; 1349 cmd.abort.cid = req->tag; 1350 cmd.abort.sqid = cpu_to_le16(nvmeq->qid); 1351 1352 dev_warn(nvmeq->dev->ctrl.device, 1353 "I/O %d QID %d timeout, aborting\n", 1354 req->tag, nvmeq->qid); 1355 1356 abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd, 1357 BLK_MQ_REQ_NOWAIT, NVME_QID_ANY); 1358 if (IS_ERR(abort_req)) { 1359 atomic_inc(&dev->ctrl.abort_limit); 1360 return BLK_EH_RESET_TIMER; 1361 } 1362 1363 abort_req->timeout = ADMIN_TIMEOUT; 1364 abort_req->end_io_data = NULL; 1365 blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio); 1366 1367 /* 1368 * The aborted req will be completed on receiving the abort req. 1369 * We enable the timer again. If hit twice, it'll cause a device reset, 1370 * as the device then is in a faulty state. 1371 */ 1372 return BLK_EH_RESET_TIMER; 1373 } 1374 1375 static void nvme_free_queue(struct nvme_queue *nvmeq) 1376 { 1377 dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth), 1378 (void *)nvmeq->cqes, nvmeq->cq_dma_addr); 1379 if (!nvmeq->sq_cmds) 1380 return; 1381 1382 if (test_and_clear_bit(NVMEQ_SQ_CMB, &nvmeq->flags)) { 1383 pci_free_p2pmem(to_pci_dev(nvmeq->q_dmadev), 1384 nvmeq->sq_cmds, SQ_SIZE(nvmeq->q_depth)); 1385 } else { 1386 dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth), 1387 nvmeq->sq_cmds, nvmeq->sq_dma_addr); 1388 } 1389 } 1390 1391 static void nvme_free_queues(struct nvme_dev *dev, int lowest) 1392 { 1393 int i; 1394 1395 for (i = dev->ctrl.queue_count - 1; i >= lowest; i--) { 1396 dev->ctrl.queue_count--; 1397 nvme_free_queue(&dev->queues[i]); 1398 } 1399 } 1400 1401 /** 1402 * nvme_suspend_queue - put queue into suspended state 1403 * @nvmeq: queue to suspend 1404 */ 1405 static int nvme_suspend_queue(struct nvme_queue *nvmeq) 1406 { 1407 if (!test_and_clear_bit(NVMEQ_ENABLED, &nvmeq->flags)) 1408 return 1; 1409 1410 /* ensure that nvme_queue_rq() sees NVMEQ_ENABLED cleared */ 1411 mb(); 1412 1413 nvmeq->dev->online_queues--; 1414 if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q) 1415 blk_mq_quiesce_queue(nvmeq->dev->ctrl.admin_q); 1416 if (nvmeq->cq_vector == -1) 1417 return 0; 1418 pci_free_irq(to_pci_dev(nvmeq->dev->dev), nvmeq->cq_vector, nvmeq); 1419 nvmeq->cq_vector = -1; 1420 return 0; 1421 } 1422 1423 static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown) 1424 { 1425 struct nvme_queue *nvmeq = &dev->queues[0]; 1426 1427 if (shutdown) 1428 nvme_shutdown_ctrl(&dev->ctrl); 1429 else 1430 nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap); 1431 1432 nvme_poll_irqdisable(nvmeq, -1); 1433 } 1434 1435 static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues, 1436 int entry_size) 1437 { 1438 int q_depth = dev->q_depth; 1439 unsigned q_size_aligned = roundup(q_depth * entry_size, 1440 dev->ctrl.page_size); 1441 1442 if (q_size_aligned * nr_io_queues > dev->cmb_size) { 1443 u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues); 1444 mem_per_q = round_down(mem_per_q, dev->ctrl.page_size); 1445 q_depth = div_u64(mem_per_q, entry_size); 1446 1447 /* 1448 * Ensure the reduced q_depth is above some threshold where it 1449 * would be better to map queues in system memory with the 1450 * original depth 1451 */ 1452 if (q_depth < 64) 1453 return -ENOMEM; 1454 } 1455 1456 return q_depth; 1457 } 1458 1459 static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq, 1460 int qid, int depth) 1461 { 1462 struct pci_dev *pdev = to_pci_dev(dev->dev); 1463 1464 if (qid && dev->cmb_use_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) { 1465 nvmeq->sq_cmds = pci_alloc_p2pmem(pdev, SQ_SIZE(depth)); 1466 nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev, 1467 nvmeq->sq_cmds); 1468 if (nvmeq->sq_dma_addr) { 1469 set_bit(NVMEQ_SQ_CMB, &nvmeq->flags); 1470 return 0; 1471 } 1472 } 1473 1474 nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth), 1475 &nvmeq->sq_dma_addr, GFP_KERNEL); 1476 if (!nvmeq->sq_cmds) 1477 return -ENOMEM; 1478 return 0; 1479 } 1480 1481 static int nvme_alloc_queue(struct nvme_dev *dev, int qid, int depth) 1482 { 1483 struct nvme_queue *nvmeq = &dev->queues[qid]; 1484 1485 if (dev->ctrl.queue_count > qid) 1486 return 0; 1487 1488 nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth), 1489 &nvmeq->cq_dma_addr, GFP_KERNEL); 1490 if (!nvmeq->cqes) 1491 goto free_nvmeq; 1492 1493 if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth)) 1494 goto free_cqdma; 1495 1496 nvmeq->q_dmadev = dev->dev; 1497 nvmeq->dev = dev; 1498 spin_lock_init(&nvmeq->sq_lock); 1499 spin_lock_init(&nvmeq->cq_poll_lock); 1500 nvmeq->cq_head = 0; 1501 nvmeq->cq_phase = 1; 1502 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; 1503 nvmeq->q_depth = depth; 1504 nvmeq->qid = qid; 1505 nvmeq->cq_vector = -1; 1506 dev->ctrl.queue_count++; 1507 1508 return 0; 1509 1510 free_cqdma: 1511 dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes, 1512 nvmeq->cq_dma_addr); 1513 free_nvmeq: 1514 return -ENOMEM; 1515 } 1516 1517 static int queue_request_irq(struct nvme_queue *nvmeq) 1518 { 1519 struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev); 1520 int nr = nvmeq->dev->ctrl.instance; 1521 1522 if (use_threaded_interrupts) { 1523 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq_check, 1524 nvme_irq, nvmeq, "nvme%dq%d", nr, nvmeq->qid); 1525 } else { 1526 return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq, 1527 NULL, nvmeq, "nvme%dq%d", nr, nvmeq->qid); 1528 } 1529 } 1530 1531 static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid) 1532 { 1533 struct nvme_dev *dev = nvmeq->dev; 1534 1535 nvmeq->sq_tail = 0; 1536 nvmeq->last_sq_tail = 0; 1537 nvmeq->cq_head = 0; 1538 nvmeq->cq_phase = 1; 1539 nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride]; 1540 memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth)); 1541 nvme_dbbuf_init(dev, nvmeq, qid); 1542 dev->online_queues++; 1543 wmb(); /* ensure the first interrupt sees the initialization */ 1544 } 1545 1546 static int nvme_create_queue(struct nvme_queue *nvmeq, int qid, bool polled) 1547 { 1548 struct nvme_dev *dev = nvmeq->dev; 1549 int result; 1550 s16 vector; 1551 1552 clear_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags); 1553 1554 /* 1555 * A queue's vector matches the queue identifier unless the controller 1556 * has only one vector available. 1557 */ 1558 if (!polled) 1559 vector = dev->num_vecs == 1 ? 0 : qid; 1560 else 1561 vector = -1; 1562 1563 result = adapter_alloc_cq(dev, qid, nvmeq, vector); 1564 if (result) 1565 return result; 1566 1567 result = adapter_alloc_sq(dev, qid, nvmeq); 1568 if (result < 0) 1569 return result; 1570 else if (result) 1571 goto release_cq; 1572 1573 nvmeq->cq_vector = vector; 1574 nvme_init_queue(nvmeq, qid); 1575 1576 if (vector != -1) { 1577 result = queue_request_irq(nvmeq); 1578 if (result < 0) 1579 goto release_sq; 1580 } 1581 1582 set_bit(NVMEQ_ENABLED, &nvmeq->flags); 1583 return result; 1584 1585 release_sq: 1586 nvmeq->cq_vector = -1; 1587 dev->online_queues--; 1588 adapter_delete_sq(dev, qid); 1589 release_cq: 1590 adapter_delete_cq(dev, qid); 1591 return result; 1592 } 1593 1594 static const struct blk_mq_ops nvme_mq_admin_ops = { 1595 .queue_rq = nvme_queue_rq, 1596 .complete = nvme_pci_complete_rq, 1597 .init_hctx = nvme_admin_init_hctx, 1598 .exit_hctx = nvme_admin_exit_hctx, 1599 .init_request = nvme_init_request, 1600 .timeout = nvme_timeout, 1601 }; 1602 1603 static const struct blk_mq_ops nvme_mq_ops = { 1604 .queue_rq = nvme_queue_rq, 1605 .complete = nvme_pci_complete_rq, 1606 .commit_rqs = nvme_commit_rqs, 1607 .init_hctx = nvme_init_hctx, 1608 .init_request = nvme_init_request, 1609 .map_queues = nvme_pci_map_queues, 1610 .timeout = nvme_timeout, 1611 .poll = nvme_poll, 1612 }; 1613 1614 static void nvme_dev_remove_admin(struct nvme_dev *dev) 1615 { 1616 if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) { 1617 /* 1618 * If the controller was reset during removal, it's possible 1619 * user requests may be waiting on a stopped queue. Start the 1620 * queue to flush these to completion. 1621 */ 1622 blk_mq_unquiesce_queue(dev->ctrl.admin_q); 1623 blk_cleanup_queue(dev->ctrl.admin_q); 1624 blk_mq_free_tag_set(&dev->admin_tagset); 1625 } 1626 } 1627 1628 static int nvme_alloc_admin_tags(struct nvme_dev *dev) 1629 { 1630 if (!dev->ctrl.admin_q) { 1631 dev->admin_tagset.ops = &nvme_mq_admin_ops; 1632 dev->admin_tagset.nr_hw_queues = 1; 1633 1634 dev->admin_tagset.queue_depth = NVME_AQ_MQ_TAG_DEPTH; 1635 dev->admin_tagset.timeout = ADMIN_TIMEOUT; 1636 dev->admin_tagset.numa_node = dev_to_node(dev->dev); 1637 dev->admin_tagset.cmd_size = nvme_pci_cmd_size(dev, false); 1638 dev->admin_tagset.flags = BLK_MQ_F_NO_SCHED; 1639 dev->admin_tagset.driver_data = dev; 1640 1641 if (blk_mq_alloc_tag_set(&dev->admin_tagset)) 1642 return -ENOMEM; 1643 dev->ctrl.admin_tagset = &dev->admin_tagset; 1644 1645 dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset); 1646 if (IS_ERR(dev->ctrl.admin_q)) { 1647 blk_mq_free_tag_set(&dev->admin_tagset); 1648 return -ENOMEM; 1649 } 1650 if (!blk_get_queue(dev->ctrl.admin_q)) { 1651 nvme_dev_remove_admin(dev); 1652 dev->ctrl.admin_q = NULL; 1653 return -ENODEV; 1654 } 1655 } else 1656 blk_mq_unquiesce_queue(dev->ctrl.admin_q); 1657 1658 return 0; 1659 } 1660 1661 static unsigned long db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues) 1662 { 1663 return NVME_REG_DBS + ((nr_io_queues + 1) * 8 * dev->db_stride); 1664 } 1665 1666 static int nvme_remap_bar(struct nvme_dev *dev, unsigned long size) 1667 { 1668 struct pci_dev *pdev = to_pci_dev(dev->dev); 1669 1670 if (size <= dev->bar_mapped_size) 1671 return 0; 1672 if (size > pci_resource_len(pdev, 0)) 1673 return -ENOMEM; 1674 if (dev->bar) 1675 iounmap(dev->bar); 1676 dev->bar = ioremap(pci_resource_start(pdev, 0), size); 1677 if (!dev->bar) { 1678 dev->bar_mapped_size = 0; 1679 return -ENOMEM; 1680 } 1681 dev->bar_mapped_size = size; 1682 dev->dbs = dev->bar + NVME_REG_DBS; 1683 1684 return 0; 1685 } 1686 1687 static int nvme_pci_configure_admin_queue(struct nvme_dev *dev) 1688 { 1689 int result; 1690 u32 aqa; 1691 struct nvme_queue *nvmeq; 1692 1693 result = nvme_remap_bar(dev, db_bar_size(dev, 0)); 1694 if (result < 0) 1695 return result; 1696 1697 dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1, 0) ? 1698 NVME_CAP_NSSRC(dev->ctrl.cap) : 0; 1699 1700 if (dev->subsystem && 1701 (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO)) 1702 writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS); 1703 1704 result = nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap); 1705 if (result < 0) 1706 return result; 1707 1708 result = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH); 1709 if (result) 1710 return result; 1711 1712 nvmeq = &dev->queues[0]; 1713 aqa = nvmeq->q_depth - 1; 1714 aqa |= aqa << 16; 1715 1716 writel(aqa, dev->bar + NVME_REG_AQA); 1717 lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ); 1718 lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ); 1719 1720 result = nvme_enable_ctrl(&dev->ctrl, dev->ctrl.cap); 1721 if (result) 1722 return result; 1723 1724 nvmeq->cq_vector = 0; 1725 nvme_init_queue(nvmeq, 0); 1726 result = queue_request_irq(nvmeq); 1727 if (result) { 1728 nvmeq->cq_vector = -1; 1729 return result; 1730 } 1731 1732 set_bit(NVMEQ_ENABLED, &nvmeq->flags); 1733 return result; 1734 } 1735 1736 static int nvme_create_io_queues(struct nvme_dev *dev) 1737 { 1738 unsigned i, max, rw_queues; 1739 int ret = 0; 1740 1741 for (i = dev->ctrl.queue_count; i <= dev->max_qid; i++) { 1742 if (nvme_alloc_queue(dev, i, dev->q_depth)) { 1743 ret = -ENOMEM; 1744 break; 1745 } 1746 } 1747 1748 max = min(dev->max_qid, dev->ctrl.queue_count - 1); 1749 if (max != 1 && dev->io_queues[HCTX_TYPE_POLL]) { 1750 rw_queues = dev->io_queues[HCTX_TYPE_DEFAULT] + 1751 dev->io_queues[HCTX_TYPE_READ]; 1752 } else { 1753 rw_queues = max; 1754 } 1755 1756 for (i = dev->online_queues; i <= max; i++) { 1757 bool polled = i > rw_queues; 1758 1759 ret = nvme_create_queue(&dev->queues[i], i, polled); 1760 if (ret) 1761 break; 1762 } 1763 1764 /* 1765 * Ignore failing Create SQ/CQ commands, we can continue with less 1766 * than the desired amount of queues, and even a controller without 1767 * I/O queues can still be used to issue admin commands. This might 1768 * be useful to upgrade a buggy firmware for example. 1769 */ 1770 return ret >= 0 ? 0 : ret; 1771 } 1772 1773 static ssize_t nvme_cmb_show(struct device *dev, 1774 struct device_attribute *attr, 1775 char *buf) 1776 { 1777 struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev)); 1778 1779 return scnprintf(buf, PAGE_SIZE, "cmbloc : x%08x\ncmbsz : x%08x\n", 1780 ndev->cmbloc, ndev->cmbsz); 1781 } 1782 static DEVICE_ATTR(cmb, S_IRUGO, nvme_cmb_show, NULL); 1783 1784 static u64 nvme_cmb_size_unit(struct nvme_dev *dev) 1785 { 1786 u8 szu = (dev->cmbsz >> NVME_CMBSZ_SZU_SHIFT) & NVME_CMBSZ_SZU_MASK; 1787 1788 return 1ULL << (12 + 4 * szu); 1789 } 1790 1791 static u32 nvme_cmb_size(struct nvme_dev *dev) 1792 { 1793 return (dev->cmbsz >> NVME_CMBSZ_SZ_SHIFT) & NVME_CMBSZ_SZ_MASK; 1794 } 1795 1796 static void nvme_map_cmb(struct nvme_dev *dev) 1797 { 1798 u64 size, offset; 1799 resource_size_t bar_size; 1800 struct pci_dev *pdev = to_pci_dev(dev->dev); 1801 int bar; 1802 1803 if (dev->cmb_size) 1804 return; 1805 1806 dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ); 1807 if (!dev->cmbsz) 1808 return; 1809 dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC); 1810 1811 size = nvme_cmb_size_unit(dev) * nvme_cmb_size(dev); 1812 offset = nvme_cmb_size_unit(dev) * NVME_CMB_OFST(dev->cmbloc); 1813 bar = NVME_CMB_BIR(dev->cmbloc); 1814 bar_size = pci_resource_len(pdev, bar); 1815 1816 if (offset > bar_size) 1817 return; 1818 1819 /* 1820 * Controllers may support a CMB size larger than their BAR, 1821 * for example, due to being behind a bridge. Reduce the CMB to 1822 * the reported size of the BAR 1823 */ 1824 if (size > bar_size - offset) 1825 size = bar_size - offset; 1826 1827 if (pci_p2pdma_add_resource(pdev, bar, size, offset)) { 1828 dev_warn(dev->ctrl.device, 1829 "failed to register the CMB\n"); 1830 return; 1831 } 1832 1833 dev->cmb_size = size; 1834 dev->cmb_use_sqes = use_cmb_sqes && (dev->cmbsz & NVME_CMBSZ_SQS); 1835 1836 if ((dev->cmbsz & (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS)) == 1837 (NVME_CMBSZ_WDS | NVME_CMBSZ_RDS)) 1838 pci_p2pmem_publish(pdev, true); 1839 1840 if (sysfs_add_file_to_group(&dev->ctrl.device->kobj, 1841 &dev_attr_cmb.attr, NULL)) 1842 dev_warn(dev->ctrl.device, 1843 "failed to add sysfs attribute for CMB\n"); 1844 } 1845 1846 static inline void nvme_release_cmb(struct nvme_dev *dev) 1847 { 1848 if (dev->cmb_size) { 1849 sysfs_remove_file_from_group(&dev->ctrl.device->kobj, 1850 &dev_attr_cmb.attr, NULL); 1851 dev->cmb_size = 0; 1852 } 1853 } 1854 1855 static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits) 1856 { 1857 u64 dma_addr = dev->host_mem_descs_dma; 1858 struct nvme_command c; 1859 int ret; 1860 1861 memset(&c, 0, sizeof(c)); 1862 c.features.opcode = nvme_admin_set_features; 1863 c.features.fid = cpu_to_le32(NVME_FEAT_HOST_MEM_BUF); 1864 c.features.dword11 = cpu_to_le32(bits); 1865 c.features.dword12 = cpu_to_le32(dev->host_mem_size >> 1866 ilog2(dev->ctrl.page_size)); 1867 c.features.dword13 = cpu_to_le32(lower_32_bits(dma_addr)); 1868 c.features.dword14 = cpu_to_le32(upper_32_bits(dma_addr)); 1869 c.features.dword15 = cpu_to_le32(dev->nr_host_mem_descs); 1870 1871 ret = nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0); 1872 if (ret) { 1873 dev_warn(dev->ctrl.device, 1874 "failed to set host mem (err %d, flags %#x).\n", 1875 ret, bits); 1876 } 1877 return ret; 1878 } 1879 1880 static void nvme_free_host_mem(struct nvme_dev *dev) 1881 { 1882 int i; 1883 1884 for (i = 0; i < dev->nr_host_mem_descs; i++) { 1885 struct nvme_host_mem_buf_desc *desc = &dev->host_mem_descs[i]; 1886 size_t size = le32_to_cpu(desc->size) * dev->ctrl.page_size; 1887 1888 dma_free_coherent(dev->dev, size, dev->host_mem_desc_bufs[i], 1889 le64_to_cpu(desc->addr)); 1890 } 1891 1892 kfree(dev->host_mem_desc_bufs); 1893 dev->host_mem_desc_bufs = NULL; 1894 dma_free_coherent(dev->dev, 1895 dev->nr_host_mem_descs * sizeof(*dev->host_mem_descs), 1896 dev->host_mem_descs, dev->host_mem_descs_dma); 1897 dev->host_mem_descs = NULL; 1898 dev->nr_host_mem_descs = 0; 1899 } 1900 1901 static int __nvme_alloc_host_mem(struct nvme_dev *dev, u64 preferred, 1902 u32 chunk_size) 1903 { 1904 struct nvme_host_mem_buf_desc *descs; 1905 u32 max_entries, len; 1906 dma_addr_t descs_dma; 1907 int i = 0; 1908 void **bufs; 1909 u64 size, tmp; 1910 1911 tmp = (preferred + chunk_size - 1); 1912 do_div(tmp, chunk_size); 1913 max_entries = tmp; 1914 1915 if (dev->ctrl.hmmaxd && dev->ctrl.hmmaxd < max_entries) 1916 max_entries = dev->ctrl.hmmaxd; 1917 1918 descs = dma_zalloc_coherent(dev->dev, max_entries * sizeof(*descs), 1919 &descs_dma, GFP_KERNEL); 1920 if (!descs) 1921 goto out; 1922 1923 bufs = kcalloc(max_entries, sizeof(*bufs), GFP_KERNEL); 1924 if (!bufs) 1925 goto out_free_descs; 1926 1927 for (size = 0; size < preferred && i < max_entries; size += len) { 1928 dma_addr_t dma_addr; 1929 1930 len = min_t(u64, chunk_size, preferred - size); 1931 bufs[i] = dma_alloc_attrs(dev->dev, len, &dma_addr, GFP_KERNEL, 1932 DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN); 1933 if (!bufs[i]) 1934 break; 1935 1936 descs[i].addr = cpu_to_le64(dma_addr); 1937 descs[i].size = cpu_to_le32(len / dev->ctrl.page_size); 1938 i++; 1939 } 1940 1941 if (!size) 1942 goto out_free_bufs; 1943 1944 dev->nr_host_mem_descs = i; 1945 dev->host_mem_size = size; 1946 dev->host_mem_descs = descs; 1947 dev->host_mem_descs_dma = descs_dma; 1948 dev->host_mem_desc_bufs = bufs; 1949 return 0; 1950 1951 out_free_bufs: 1952 while (--i >= 0) { 1953 size_t size = le32_to_cpu(descs[i].size) * dev->ctrl.page_size; 1954 1955 dma_free_coherent(dev->dev, size, bufs[i], 1956 le64_to_cpu(descs[i].addr)); 1957 } 1958 1959 kfree(bufs); 1960 out_free_descs: 1961 dma_free_coherent(dev->dev, max_entries * sizeof(*descs), descs, 1962 descs_dma); 1963 out: 1964 dev->host_mem_descs = NULL; 1965 return -ENOMEM; 1966 } 1967 1968 static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred) 1969 { 1970 u32 chunk_size; 1971 1972 /* start big and work our way down */ 1973 for (chunk_size = min_t(u64, preferred, PAGE_SIZE * MAX_ORDER_NR_PAGES); 1974 chunk_size >= max_t(u32, dev->ctrl.hmminds * 4096, PAGE_SIZE * 2); 1975 chunk_size /= 2) { 1976 if (!__nvme_alloc_host_mem(dev, preferred, chunk_size)) { 1977 if (!min || dev->host_mem_size >= min) 1978 return 0; 1979 nvme_free_host_mem(dev); 1980 } 1981 } 1982 1983 return -ENOMEM; 1984 } 1985 1986 static int nvme_setup_host_mem(struct nvme_dev *dev) 1987 { 1988 u64 max = (u64)max_host_mem_size_mb * SZ_1M; 1989 u64 preferred = (u64)dev->ctrl.hmpre * 4096; 1990 u64 min = (u64)dev->ctrl.hmmin * 4096; 1991 u32 enable_bits = NVME_HOST_MEM_ENABLE; 1992 int ret; 1993 1994 preferred = min(preferred, max); 1995 if (min > max) { 1996 dev_warn(dev->ctrl.device, 1997 "min host memory (%lld MiB) above limit (%d MiB).\n", 1998 min >> ilog2(SZ_1M), max_host_mem_size_mb); 1999 nvme_free_host_mem(dev); 2000 return 0; 2001 } 2002 2003 /* 2004 * If we already have a buffer allocated check if we can reuse it. 2005 */ 2006 if (dev->host_mem_descs) { 2007 if (dev->host_mem_size >= min) 2008 enable_bits |= NVME_HOST_MEM_RETURN; 2009 else 2010 nvme_free_host_mem(dev); 2011 } 2012 2013 if (!dev->host_mem_descs) { 2014 if (nvme_alloc_host_mem(dev, min, preferred)) { 2015 dev_warn(dev->ctrl.device, 2016 "failed to allocate host memory buffer.\n"); 2017 return 0; /* controller must work without HMB */ 2018 } 2019 2020 dev_info(dev->ctrl.device, 2021 "allocated %lld MiB host memory buffer.\n", 2022 dev->host_mem_size >> ilog2(SZ_1M)); 2023 } 2024 2025 ret = nvme_set_host_mem(dev, enable_bits); 2026 if (ret) 2027 nvme_free_host_mem(dev); 2028 return ret; 2029 } 2030 2031 static void nvme_calc_io_queues(struct nvme_dev *dev, unsigned int irq_queues) 2032 { 2033 unsigned int this_w_queues = write_queues; 2034 2035 /* 2036 * Setup read/write queue split 2037 */ 2038 if (irq_queues == 1) { 2039 dev->io_queues[HCTX_TYPE_DEFAULT] = 1; 2040 dev->io_queues[HCTX_TYPE_READ] = 0; 2041 return; 2042 } 2043 2044 /* 2045 * If 'write_queues' is set, ensure it leaves room for at least 2046 * one read queue 2047 */ 2048 if (this_w_queues >= irq_queues) 2049 this_w_queues = irq_queues - 1; 2050 2051 /* 2052 * If 'write_queues' is set to zero, reads and writes will share 2053 * a queue set. 2054 */ 2055 if (!this_w_queues) { 2056 dev->io_queues[HCTX_TYPE_DEFAULT] = irq_queues; 2057 dev->io_queues[HCTX_TYPE_READ] = 0; 2058 } else { 2059 dev->io_queues[HCTX_TYPE_DEFAULT] = this_w_queues; 2060 dev->io_queues[HCTX_TYPE_READ] = irq_queues - this_w_queues; 2061 } 2062 } 2063 2064 static int nvme_setup_irqs(struct nvme_dev *dev, unsigned int nr_io_queues) 2065 { 2066 struct pci_dev *pdev = to_pci_dev(dev->dev); 2067 int irq_sets[2]; 2068 struct irq_affinity affd = { 2069 .pre_vectors = 1, 2070 .nr_sets = ARRAY_SIZE(irq_sets), 2071 .sets = irq_sets, 2072 }; 2073 int result = 0; 2074 unsigned int irq_queues, this_p_queues; 2075 2076 /* 2077 * Poll queues don't need interrupts, but we need at least one IO 2078 * queue left over for non-polled IO. 2079 */ 2080 this_p_queues = poll_queues; 2081 if (this_p_queues >= nr_io_queues) { 2082 this_p_queues = nr_io_queues - 1; 2083 irq_queues = 1; 2084 } else { 2085 irq_queues = nr_io_queues - this_p_queues; 2086 } 2087 dev->io_queues[HCTX_TYPE_POLL] = this_p_queues; 2088 2089 /* 2090 * For irq sets, we have to ask for minvec == maxvec. This passes 2091 * any reduction back to us, so we can adjust our queue counts and 2092 * IRQ vector needs. 2093 */ 2094 do { 2095 nvme_calc_io_queues(dev, irq_queues); 2096 irq_sets[0] = dev->io_queues[HCTX_TYPE_DEFAULT]; 2097 irq_sets[1] = dev->io_queues[HCTX_TYPE_READ]; 2098 if (!irq_sets[1]) 2099 affd.nr_sets = 1; 2100 2101 /* 2102 * If we got a failure and we're down to asking for just 2103 * 1 + 1 queues, just ask for a single vector. We'll share 2104 * that between the single IO queue and the admin queue. 2105 */ 2106 if (result >= 0 && irq_queues > 1) 2107 irq_queues = irq_sets[0] + irq_sets[1] + 1; 2108 2109 result = pci_alloc_irq_vectors_affinity(pdev, irq_queues, 2110 irq_queues, 2111 PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY, &affd); 2112 2113 /* 2114 * Need to reduce our vec counts. If we get ENOSPC, the 2115 * platform should support mulitple vecs, we just need 2116 * to decrease our ask. If we get EINVAL, the platform 2117 * likely does not. Back down to ask for just one vector. 2118 */ 2119 if (result == -ENOSPC) { 2120 irq_queues--; 2121 if (!irq_queues) 2122 return result; 2123 continue; 2124 } else if (result == -EINVAL) { 2125 irq_queues = 1; 2126 continue; 2127 } else if (result <= 0) 2128 return -EIO; 2129 break; 2130 } while (1); 2131 2132 return result; 2133 } 2134 2135 static int nvme_setup_io_queues(struct nvme_dev *dev) 2136 { 2137 struct nvme_queue *adminq = &dev->queues[0]; 2138 struct pci_dev *pdev = to_pci_dev(dev->dev); 2139 int result, nr_io_queues; 2140 unsigned long size; 2141 2142 nr_io_queues = max_io_queues(); 2143 result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues); 2144 if (result < 0) 2145 return result; 2146 2147 if (nr_io_queues == 0) 2148 return 0; 2149 2150 clear_bit(NVMEQ_ENABLED, &adminq->flags); 2151 2152 if (dev->cmb_use_sqes) { 2153 result = nvme_cmb_qdepth(dev, nr_io_queues, 2154 sizeof(struct nvme_command)); 2155 if (result > 0) 2156 dev->q_depth = result; 2157 else 2158 dev->cmb_use_sqes = false; 2159 } 2160 2161 do { 2162 size = db_bar_size(dev, nr_io_queues); 2163 result = nvme_remap_bar(dev, size); 2164 if (!result) 2165 break; 2166 if (!--nr_io_queues) 2167 return -ENOMEM; 2168 } while (1); 2169 adminq->q_db = dev->dbs; 2170 2171 /* Deregister the admin queue's interrupt */ 2172 pci_free_irq(pdev, 0, adminq); 2173 2174 /* 2175 * If we enable msix early due to not intx, disable it again before 2176 * setting up the full range we need. 2177 */ 2178 pci_free_irq_vectors(pdev); 2179 2180 result = nvme_setup_irqs(dev, nr_io_queues); 2181 if (result <= 0) 2182 return -EIO; 2183 2184 dev->num_vecs = result; 2185 result = max(result - 1, 1); 2186 dev->max_qid = result + dev->io_queues[HCTX_TYPE_POLL]; 2187 2188 dev_info(dev->ctrl.device, "%d/%d/%d default/read/poll queues\n", 2189 dev->io_queues[HCTX_TYPE_DEFAULT], 2190 dev->io_queues[HCTX_TYPE_READ], 2191 dev->io_queues[HCTX_TYPE_POLL]); 2192 2193 /* 2194 * Should investigate if there's a performance win from allocating 2195 * more queues than interrupt vectors; it might allow the submission 2196 * path to scale better, even if the receive path is limited by the 2197 * number of interrupts. 2198 */ 2199 2200 result = queue_request_irq(adminq); 2201 if (result) { 2202 adminq->cq_vector = -1; 2203 return result; 2204 } 2205 set_bit(NVMEQ_ENABLED, &adminq->flags); 2206 return nvme_create_io_queues(dev); 2207 } 2208 2209 static void nvme_del_queue_end(struct request *req, blk_status_t error) 2210 { 2211 struct nvme_queue *nvmeq = req->end_io_data; 2212 2213 blk_mq_free_request(req); 2214 complete(&nvmeq->delete_done); 2215 } 2216 2217 static void nvme_del_cq_end(struct request *req, blk_status_t error) 2218 { 2219 struct nvme_queue *nvmeq = req->end_io_data; 2220 2221 if (error) 2222 set_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags); 2223 2224 nvme_del_queue_end(req, error); 2225 } 2226 2227 static int nvme_delete_queue(struct nvme_queue *nvmeq, u8 opcode) 2228 { 2229 struct request_queue *q = nvmeq->dev->ctrl.admin_q; 2230 struct request *req; 2231 struct nvme_command cmd; 2232 2233 memset(&cmd, 0, sizeof(cmd)); 2234 cmd.delete_queue.opcode = opcode; 2235 cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid); 2236 2237 req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT, NVME_QID_ANY); 2238 if (IS_ERR(req)) 2239 return PTR_ERR(req); 2240 2241 req->timeout = ADMIN_TIMEOUT; 2242 req->end_io_data = nvmeq; 2243 2244 init_completion(&nvmeq->delete_done); 2245 blk_execute_rq_nowait(q, NULL, req, false, 2246 opcode == nvme_admin_delete_cq ? 2247 nvme_del_cq_end : nvme_del_queue_end); 2248 return 0; 2249 } 2250 2251 static bool nvme_disable_io_queues(struct nvme_dev *dev, u8 opcode) 2252 { 2253 int nr_queues = dev->online_queues - 1, sent = 0; 2254 unsigned long timeout; 2255 2256 retry: 2257 timeout = ADMIN_TIMEOUT; 2258 while (nr_queues > 0) { 2259 if (nvme_delete_queue(&dev->queues[nr_queues], opcode)) 2260 break; 2261 nr_queues--; 2262 sent++; 2263 } 2264 while (sent) { 2265 struct nvme_queue *nvmeq = &dev->queues[nr_queues + sent]; 2266 2267 timeout = wait_for_completion_io_timeout(&nvmeq->delete_done, 2268 timeout); 2269 if (timeout == 0) 2270 return false; 2271 2272 /* handle any remaining CQEs */ 2273 if (opcode == nvme_admin_delete_cq && 2274 !test_bit(NVMEQ_DELETE_ERROR, &nvmeq->flags)) 2275 nvme_poll_irqdisable(nvmeq, -1); 2276 2277 sent--; 2278 if (nr_queues) 2279 goto retry; 2280 } 2281 return true; 2282 } 2283 2284 /* 2285 * return error value only when tagset allocation failed 2286 */ 2287 static int nvme_dev_add(struct nvme_dev *dev) 2288 { 2289 int ret; 2290 2291 if (!dev->ctrl.tagset) { 2292 dev->tagset.ops = &nvme_mq_ops; 2293 dev->tagset.nr_hw_queues = dev->online_queues - 1; 2294 dev->tagset.nr_maps = 2; /* default + read */ 2295 if (dev->io_queues[HCTX_TYPE_POLL]) 2296 dev->tagset.nr_maps++; 2297 dev->tagset.nr_maps = HCTX_MAX_TYPES; 2298 dev->tagset.timeout = NVME_IO_TIMEOUT; 2299 dev->tagset.numa_node = dev_to_node(dev->dev); 2300 dev->tagset.queue_depth = 2301 min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1; 2302 dev->tagset.cmd_size = nvme_pci_cmd_size(dev, false); 2303 if ((dev->ctrl.sgls & ((1 << 0) | (1 << 1))) && sgl_threshold) { 2304 dev->tagset.cmd_size = max(dev->tagset.cmd_size, 2305 nvme_pci_cmd_size(dev, true)); 2306 } 2307 dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE; 2308 dev->tagset.driver_data = dev; 2309 2310 ret = blk_mq_alloc_tag_set(&dev->tagset); 2311 if (ret) { 2312 dev_warn(dev->ctrl.device, 2313 "IO queues tagset allocation failed %d\n", ret); 2314 return ret; 2315 } 2316 dev->ctrl.tagset = &dev->tagset; 2317 2318 nvme_dbbuf_set(dev); 2319 } else { 2320 blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1); 2321 2322 /* Free previously allocated queues that are no longer usable */ 2323 nvme_free_queues(dev, dev->online_queues); 2324 } 2325 2326 return 0; 2327 } 2328 2329 static int nvme_pci_enable(struct nvme_dev *dev) 2330 { 2331 int result = -ENOMEM; 2332 struct pci_dev *pdev = to_pci_dev(dev->dev); 2333 2334 if (pci_enable_device_mem(pdev)) 2335 return result; 2336 2337 pci_set_master(pdev); 2338 2339 if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) && 2340 dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32))) 2341 goto disable; 2342 2343 if (readl(dev->bar + NVME_REG_CSTS) == -1) { 2344 result = -ENODEV; 2345 goto disable; 2346 } 2347 2348 /* 2349 * Some devices and/or platforms don't advertise or work with INTx 2350 * interrupts. Pre-enable a single MSIX or MSI vec for setup. We'll 2351 * adjust this later. 2352 */ 2353 result = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES); 2354 if (result < 0) 2355 return result; 2356 2357 dev->ctrl.cap = lo_hi_readq(dev->bar + NVME_REG_CAP); 2358 2359 dev->q_depth = min_t(int, NVME_CAP_MQES(dev->ctrl.cap) + 1, 2360 io_queue_depth); 2361 dev->db_stride = 1 << NVME_CAP_STRIDE(dev->ctrl.cap); 2362 dev->dbs = dev->bar + 4096; 2363 2364 /* 2365 * Temporary fix for the Apple controller found in the MacBook8,1 and 2366 * some MacBook7,1 to avoid controller resets and data loss. 2367 */ 2368 if (pdev->vendor == PCI_VENDOR_ID_APPLE && pdev->device == 0x2001) { 2369 dev->q_depth = 2; 2370 dev_warn(dev->ctrl.device, "detected Apple NVMe controller, " 2371 "set queue depth=%u to work around controller resets\n", 2372 dev->q_depth); 2373 } else if (pdev->vendor == PCI_VENDOR_ID_SAMSUNG && 2374 (pdev->device == 0xa821 || pdev->device == 0xa822) && 2375 NVME_CAP_MQES(dev->ctrl.cap) == 0) { 2376 dev->q_depth = 64; 2377 dev_err(dev->ctrl.device, "detected PM1725 NVMe controller, " 2378 "set queue depth=%u\n", dev->q_depth); 2379 } 2380 2381 nvme_map_cmb(dev); 2382 2383 pci_enable_pcie_error_reporting(pdev); 2384 pci_save_state(pdev); 2385 return 0; 2386 2387 disable: 2388 pci_disable_device(pdev); 2389 return result; 2390 } 2391 2392 static void nvme_dev_unmap(struct nvme_dev *dev) 2393 { 2394 if (dev->bar) 2395 iounmap(dev->bar); 2396 pci_release_mem_regions(to_pci_dev(dev->dev)); 2397 } 2398 2399 static void nvme_pci_disable(struct nvme_dev *dev) 2400 { 2401 struct pci_dev *pdev = to_pci_dev(dev->dev); 2402 2403 pci_free_irq_vectors(pdev); 2404 2405 if (pci_is_enabled(pdev)) { 2406 pci_disable_pcie_error_reporting(pdev); 2407 pci_disable_device(pdev); 2408 } 2409 } 2410 2411 static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown) 2412 { 2413 int i; 2414 bool dead = true; 2415 struct pci_dev *pdev = to_pci_dev(dev->dev); 2416 2417 mutex_lock(&dev->shutdown_lock); 2418 if (pci_is_enabled(pdev)) { 2419 u32 csts = readl(dev->bar + NVME_REG_CSTS); 2420 2421 if (dev->ctrl.state == NVME_CTRL_LIVE || 2422 dev->ctrl.state == NVME_CTRL_RESETTING) 2423 nvme_start_freeze(&dev->ctrl); 2424 dead = !!((csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) || 2425 pdev->error_state != pci_channel_io_normal); 2426 } 2427 2428 /* 2429 * Give the controller a chance to complete all entered requests if 2430 * doing a safe shutdown. 2431 */ 2432 if (!dead) { 2433 if (shutdown) 2434 nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT); 2435 } 2436 2437 nvme_stop_queues(&dev->ctrl); 2438 2439 if (!dead && dev->ctrl.queue_count > 0) { 2440 if (nvme_disable_io_queues(dev, nvme_admin_delete_sq)) 2441 nvme_disable_io_queues(dev, nvme_admin_delete_cq); 2442 nvme_disable_admin_queue(dev, shutdown); 2443 } 2444 for (i = dev->ctrl.queue_count - 1; i >= 0; i--) 2445 nvme_suspend_queue(&dev->queues[i]); 2446 2447 nvme_pci_disable(dev); 2448 2449 blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl); 2450 blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl); 2451 2452 /* 2453 * The driver will not be starting up queues again if shutting down so 2454 * must flush all entered requests to their failed completion to avoid 2455 * deadlocking blk-mq hot-cpu notifier. 2456 */ 2457 if (shutdown) 2458 nvme_start_queues(&dev->ctrl); 2459 mutex_unlock(&dev->shutdown_lock); 2460 } 2461 2462 static int nvme_setup_prp_pools(struct nvme_dev *dev) 2463 { 2464 dev->prp_page_pool = dma_pool_create("prp list page", dev->dev, 2465 PAGE_SIZE, PAGE_SIZE, 0); 2466 if (!dev->prp_page_pool) 2467 return -ENOMEM; 2468 2469 /* Optimisation for I/Os between 4k and 128k */ 2470 dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev, 2471 256, 256, 0); 2472 if (!dev->prp_small_pool) { 2473 dma_pool_destroy(dev->prp_page_pool); 2474 return -ENOMEM; 2475 } 2476 return 0; 2477 } 2478 2479 static void nvme_release_prp_pools(struct nvme_dev *dev) 2480 { 2481 dma_pool_destroy(dev->prp_page_pool); 2482 dma_pool_destroy(dev->prp_small_pool); 2483 } 2484 2485 static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl) 2486 { 2487 struct nvme_dev *dev = to_nvme_dev(ctrl); 2488 2489 nvme_dbbuf_dma_free(dev); 2490 put_device(dev->dev); 2491 if (dev->tagset.tags) 2492 blk_mq_free_tag_set(&dev->tagset); 2493 if (dev->ctrl.admin_q) 2494 blk_put_queue(dev->ctrl.admin_q); 2495 kfree(dev->queues); 2496 free_opal_dev(dev->ctrl.opal_dev); 2497 mempool_destroy(dev->iod_mempool); 2498 kfree(dev); 2499 } 2500 2501 static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status) 2502 { 2503 dev_warn(dev->ctrl.device, "Removing after probe failure status: %d\n", status); 2504 2505 nvme_get_ctrl(&dev->ctrl); 2506 nvme_dev_disable(dev, false); 2507 nvme_kill_queues(&dev->ctrl); 2508 if (!queue_work(nvme_wq, &dev->remove_work)) 2509 nvme_put_ctrl(&dev->ctrl); 2510 } 2511 2512 static void nvme_reset_work(struct work_struct *work) 2513 { 2514 struct nvme_dev *dev = 2515 container_of(work, struct nvme_dev, ctrl.reset_work); 2516 bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL); 2517 int result = -ENODEV; 2518 enum nvme_ctrl_state new_state = NVME_CTRL_LIVE; 2519 2520 if (WARN_ON(dev->ctrl.state != NVME_CTRL_RESETTING)) 2521 goto out; 2522 2523 /* 2524 * If we're called to reset a live controller first shut it down before 2525 * moving on. 2526 */ 2527 if (dev->ctrl.ctrl_config & NVME_CC_ENABLE) 2528 nvme_dev_disable(dev, false); 2529 2530 /* 2531 * Introduce CONNECTING state from nvme-fc/rdma transports to mark the 2532 * initializing procedure here. 2533 */ 2534 if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_CONNECTING)) { 2535 dev_warn(dev->ctrl.device, 2536 "failed to mark controller CONNECTING\n"); 2537 goto out; 2538 } 2539 2540 result = nvme_pci_enable(dev); 2541 if (result) 2542 goto out; 2543 2544 result = nvme_pci_configure_admin_queue(dev); 2545 if (result) 2546 goto out; 2547 2548 result = nvme_alloc_admin_tags(dev); 2549 if (result) 2550 goto out; 2551 2552 /* 2553 * Limit the max command size to prevent iod->sg allocations going 2554 * over a single page. 2555 */ 2556 dev->ctrl.max_hw_sectors = NVME_MAX_KB_SZ << 1; 2557 dev->ctrl.max_segments = NVME_MAX_SEGS; 2558 2559 result = nvme_init_identify(&dev->ctrl); 2560 if (result) 2561 goto out; 2562 2563 if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) { 2564 if (!dev->ctrl.opal_dev) 2565 dev->ctrl.opal_dev = 2566 init_opal_dev(&dev->ctrl, &nvme_sec_submit); 2567 else if (was_suspend) 2568 opal_unlock_from_suspend(dev->ctrl.opal_dev); 2569 } else { 2570 free_opal_dev(dev->ctrl.opal_dev); 2571 dev->ctrl.opal_dev = NULL; 2572 } 2573 2574 if (dev->ctrl.oacs & NVME_CTRL_OACS_DBBUF_SUPP) { 2575 result = nvme_dbbuf_dma_alloc(dev); 2576 if (result) 2577 dev_warn(dev->dev, 2578 "unable to allocate dma for dbbuf\n"); 2579 } 2580 2581 if (dev->ctrl.hmpre) { 2582 result = nvme_setup_host_mem(dev); 2583 if (result < 0) 2584 goto out; 2585 } 2586 2587 result = nvme_setup_io_queues(dev); 2588 if (result) 2589 goto out; 2590 2591 /* 2592 * Keep the controller around but remove all namespaces if we don't have 2593 * any working I/O queue. 2594 */ 2595 if (dev->online_queues < 2) { 2596 dev_warn(dev->ctrl.device, "IO queues not created\n"); 2597 nvme_kill_queues(&dev->ctrl); 2598 nvme_remove_namespaces(&dev->ctrl); 2599 new_state = NVME_CTRL_ADMIN_ONLY; 2600 } else { 2601 nvme_start_queues(&dev->ctrl); 2602 nvme_wait_freeze(&dev->ctrl); 2603 /* hit this only when allocate tagset fails */ 2604 if (nvme_dev_add(dev)) 2605 new_state = NVME_CTRL_ADMIN_ONLY; 2606 nvme_unfreeze(&dev->ctrl); 2607 } 2608 2609 /* 2610 * If only admin queue live, keep it to do further investigation or 2611 * recovery. 2612 */ 2613 if (!nvme_change_ctrl_state(&dev->ctrl, new_state)) { 2614 dev_warn(dev->ctrl.device, 2615 "failed to mark controller state %d\n", new_state); 2616 goto out; 2617 } 2618 2619 nvme_start_ctrl(&dev->ctrl); 2620 return; 2621 2622 out: 2623 nvme_remove_dead_ctrl(dev, result); 2624 } 2625 2626 static void nvme_remove_dead_ctrl_work(struct work_struct *work) 2627 { 2628 struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work); 2629 struct pci_dev *pdev = to_pci_dev(dev->dev); 2630 2631 if (pci_get_drvdata(pdev)) 2632 device_release_driver(&pdev->dev); 2633 nvme_put_ctrl(&dev->ctrl); 2634 } 2635 2636 static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val) 2637 { 2638 *val = readl(to_nvme_dev(ctrl)->bar + off); 2639 return 0; 2640 } 2641 2642 static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val) 2643 { 2644 writel(val, to_nvme_dev(ctrl)->bar + off); 2645 return 0; 2646 } 2647 2648 static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val) 2649 { 2650 *val = readq(to_nvme_dev(ctrl)->bar + off); 2651 return 0; 2652 } 2653 2654 static int nvme_pci_get_address(struct nvme_ctrl *ctrl, char *buf, int size) 2655 { 2656 struct pci_dev *pdev = to_pci_dev(to_nvme_dev(ctrl)->dev); 2657 2658 return snprintf(buf, size, "%s", dev_name(&pdev->dev)); 2659 } 2660 2661 static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = { 2662 .name = "pcie", 2663 .module = THIS_MODULE, 2664 .flags = NVME_F_METADATA_SUPPORTED | 2665 NVME_F_PCI_P2PDMA, 2666 .reg_read32 = nvme_pci_reg_read32, 2667 .reg_write32 = nvme_pci_reg_write32, 2668 .reg_read64 = nvme_pci_reg_read64, 2669 .free_ctrl = nvme_pci_free_ctrl, 2670 .submit_async_event = nvme_pci_submit_async_event, 2671 .get_address = nvme_pci_get_address, 2672 }; 2673 2674 static int nvme_dev_map(struct nvme_dev *dev) 2675 { 2676 struct pci_dev *pdev = to_pci_dev(dev->dev); 2677 2678 if (pci_request_mem_regions(pdev, "nvme")) 2679 return -ENODEV; 2680 2681 if (nvme_remap_bar(dev, NVME_REG_DBS + 4096)) 2682 goto release; 2683 2684 return 0; 2685 release: 2686 pci_release_mem_regions(pdev); 2687 return -ENODEV; 2688 } 2689 2690 static unsigned long check_vendor_combination_bug(struct pci_dev *pdev) 2691 { 2692 if (pdev->vendor == 0x144d && pdev->device == 0xa802) { 2693 /* 2694 * Several Samsung devices seem to drop off the PCIe bus 2695 * randomly when APST is on and uses the deepest sleep state. 2696 * This has been observed on a Samsung "SM951 NVMe SAMSUNG 2697 * 256GB", a "PM951 NVMe SAMSUNG 512GB", and a "Samsung SSD 2698 * 950 PRO 256GB", but it seems to be restricted to two Dell 2699 * laptops. 2700 */ 2701 if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.") && 2702 (dmi_match(DMI_PRODUCT_NAME, "XPS 15 9550") || 2703 dmi_match(DMI_PRODUCT_NAME, "Precision 5510"))) 2704 return NVME_QUIRK_NO_DEEPEST_PS; 2705 } else if (pdev->vendor == 0x144d && pdev->device == 0xa804) { 2706 /* 2707 * Samsung SSD 960 EVO drops off the PCIe bus after system 2708 * suspend on a Ryzen board, ASUS PRIME B350M-A, as well as 2709 * within few minutes after bootup on a Coffee Lake board - 2710 * ASUS PRIME Z370-A 2711 */ 2712 if (dmi_match(DMI_BOARD_VENDOR, "ASUSTeK COMPUTER INC.") && 2713 (dmi_match(DMI_BOARD_NAME, "PRIME B350M-A") || 2714 dmi_match(DMI_BOARD_NAME, "PRIME Z370-A"))) 2715 return NVME_QUIRK_NO_APST; 2716 } 2717 2718 return 0; 2719 } 2720 2721 static void nvme_async_probe(void *data, async_cookie_t cookie) 2722 { 2723 struct nvme_dev *dev = data; 2724 2725 nvme_reset_ctrl_sync(&dev->ctrl); 2726 flush_work(&dev->ctrl.scan_work); 2727 nvme_put_ctrl(&dev->ctrl); 2728 } 2729 2730 static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id) 2731 { 2732 int node, result = -ENOMEM; 2733 struct nvme_dev *dev; 2734 unsigned long quirks = id->driver_data; 2735 size_t alloc_size; 2736 2737 node = dev_to_node(&pdev->dev); 2738 if (node == NUMA_NO_NODE) 2739 set_dev_node(&pdev->dev, first_memory_node); 2740 2741 dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node); 2742 if (!dev) 2743 return -ENOMEM; 2744 2745 dev->queues = kcalloc_node(max_queue_count(), sizeof(struct nvme_queue), 2746 GFP_KERNEL, node); 2747 if (!dev->queues) 2748 goto free; 2749 2750 dev->dev = get_device(&pdev->dev); 2751 pci_set_drvdata(pdev, dev); 2752 2753 result = nvme_dev_map(dev); 2754 if (result) 2755 goto put_pci; 2756 2757 INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work); 2758 INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work); 2759 mutex_init(&dev->shutdown_lock); 2760 2761 result = nvme_setup_prp_pools(dev); 2762 if (result) 2763 goto unmap; 2764 2765 quirks |= check_vendor_combination_bug(pdev); 2766 2767 /* 2768 * Double check that our mempool alloc size will cover the biggest 2769 * command we support. 2770 */ 2771 alloc_size = nvme_pci_iod_alloc_size(dev, NVME_MAX_KB_SZ, 2772 NVME_MAX_SEGS, true); 2773 WARN_ON_ONCE(alloc_size > PAGE_SIZE); 2774 2775 dev->iod_mempool = mempool_create_node(1, mempool_kmalloc, 2776 mempool_kfree, 2777 (void *) alloc_size, 2778 GFP_KERNEL, node); 2779 if (!dev->iod_mempool) { 2780 result = -ENOMEM; 2781 goto release_pools; 2782 } 2783 2784 result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops, 2785 quirks); 2786 if (result) 2787 goto release_mempool; 2788 2789 dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev)); 2790 2791 nvme_get_ctrl(&dev->ctrl); 2792 async_schedule(nvme_async_probe, dev); 2793 2794 return 0; 2795 2796 release_mempool: 2797 mempool_destroy(dev->iod_mempool); 2798 release_pools: 2799 nvme_release_prp_pools(dev); 2800 unmap: 2801 nvme_dev_unmap(dev); 2802 put_pci: 2803 put_device(dev->dev); 2804 free: 2805 kfree(dev->queues); 2806 kfree(dev); 2807 return result; 2808 } 2809 2810 static void nvme_reset_prepare(struct pci_dev *pdev) 2811 { 2812 struct nvme_dev *dev = pci_get_drvdata(pdev); 2813 nvme_dev_disable(dev, false); 2814 } 2815 2816 static void nvme_reset_done(struct pci_dev *pdev) 2817 { 2818 struct nvme_dev *dev = pci_get_drvdata(pdev); 2819 nvme_reset_ctrl_sync(&dev->ctrl); 2820 } 2821 2822 static void nvme_shutdown(struct pci_dev *pdev) 2823 { 2824 struct nvme_dev *dev = pci_get_drvdata(pdev); 2825 nvme_dev_disable(dev, true); 2826 } 2827 2828 /* 2829 * The driver's remove may be called on a device in a partially initialized 2830 * state. This function must not have any dependencies on the device state in 2831 * order to proceed. 2832 */ 2833 static void nvme_remove(struct pci_dev *pdev) 2834 { 2835 struct nvme_dev *dev = pci_get_drvdata(pdev); 2836 2837 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING); 2838 pci_set_drvdata(pdev, NULL); 2839 2840 if (!pci_device_is_present(pdev)) { 2841 nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD); 2842 nvme_dev_disable(dev, true); 2843 nvme_dev_remove_admin(dev); 2844 } 2845 2846 flush_work(&dev->ctrl.reset_work); 2847 nvme_stop_ctrl(&dev->ctrl); 2848 nvme_remove_namespaces(&dev->ctrl); 2849 nvme_dev_disable(dev, true); 2850 nvme_release_cmb(dev); 2851 nvme_free_host_mem(dev); 2852 nvme_dev_remove_admin(dev); 2853 nvme_free_queues(dev, 0); 2854 nvme_uninit_ctrl(&dev->ctrl); 2855 nvme_release_prp_pools(dev); 2856 nvme_dev_unmap(dev); 2857 nvme_put_ctrl(&dev->ctrl); 2858 } 2859 2860 #ifdef CONFIG_PM_SLEEP 2861 static int nvme_suspend(struct device *dev) 2862 { 2863 struct pci_dev *pdev = to_pci_dev(dev); 2864 struct nvme_dev *ndev = pci_get_drvdata(pdev); 2865 2866 nvme_dev_disable(ndev, true); 2867 return 0; 2868 } 2869 2870 static int nvme_resume(struct device *dev) 2871 { 2872 struct pci_dev *pdev = to_pci_dev(dev); 2873 struct nvme_dev *ndev = pci_get_drvdata(pdev); 2874 2875 nvme_reset_ctrl(&ndev->ctrl); 2876 return 0; 2877 } 2878 #endif 2879 2880 static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume); 2881 2882 static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev, 2883 pci_channel_state_t state) 2884 { 2885 struct nvme_dev *dev = pci_get_drvdata(pdev); 2886 2887 /* 2888 * A frozen channel requires a reset. When detected, this method will 2889 * shutdown the controller to quiesce. The controller will be restarted 2890 * after the slot reset through driver's slot_reset callback. 2891 */ 2892 switch (state) { 2893 case pci_channel_io_normal: 2894 return PCI_ERS_RESULT_CAN_RECOVER; 2895 case pci_channel_io_frozen: 2896 dev_warn(dev->ctrl.device, 2897 "frozen state error detected, reset controller\n"); 2898 nvme_dev_disable(dev, false); 2899 return PCI_ERS_RESULT_NEED_RESET; 2900 case pci_channel_io_perm_failure: 2901 dev_warn(dev->ctrl.device, 2902 "failure state error detected, request disconnect\n"); 2903 return PCI_ERS_RESULT_DISCONNECT; 2904 } 2905 return PCI_ERS_RESULT_NEED_RESET; 2906 } 2907 2908 static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev) 2909 { 2910 struct nvme_dev *dev = pci_get_drvdata(pdev); 2911 2912 dev_info(dev->ctrl.device, "restart after slot reset\n"); 2913 pci_restore_state(pdev); 2914 nvme_reset_ctrl(&dev->ctrl); 2915 return PCI_ERS_RESULT_RECOVERED; 2916 } 2917 2918 static void nvme_error_resume(struct pci_dev *pdev) 2919 { 2920 struct nvme_dev *dev = pci_get_drvdata(pdev); 2921 2922 flush_work(&dev->ctrl.reset_work); 2923 } 2924 2925 static const struct pci_error_handlers nvme_err_handler = { 2926 .error_detected = nvme_error_detected, 2927 .slot_reset = nvme_slot_reset, 2928 .resume = nvme_error_resume, 2929 .reset_prepare = nvme_reset_prepare, 2930 .reset_done = nvme_reset_done, 2931 }; 2932 2933 static const struct pci_device_id nvme_id_table[] = { 2934 { PCI_VDEVICE(INTEL, 0x0953), 2935 .driver_data = NVME_QUIRK_STRIPE_SIZE | 2936 NVME_QUIRK_DEALLOCATE_ZEROES, }, 2937 { PCI_VDEVICE(INTEL, 0x0a53), 2938 .driver_data = NVME_QUIRK_STRIPE_SIZE | 2939 NVME_QUIRK_DEALLOCATE_ZEROES, }, 2940 { PCI_VDEVICE(INTEL, 0x0a54), 2941 .driver_data = NVME_QUIRK_STRIPE_SIZE | 2942 NVME_QUIRK_DEALLOCATE_ZEROES, }, 2943 { PCI_VDEVICE(INTEL, 0x0a55), 2944 .driver_data = NVME_QUIRK_STRIPE_SIZE | 2945 NVME_QUIRK_DEALLOCATE_ZEROES, }, 2946 { PCI_VDEVICE(INTEL, 0xf1a5), /* Intel 600P/P3100 */ 2947 .driver_data = NVME_QUIRK_NO_DEEPEST_PS | 2948 NVME_QUIRK_MEDIUM_PRIO_SQ }, 2949 { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */ 2950 .driver_data = NVME_QUIRK_IDENTIFY_CNS, }, 2951 { PCI_DEVICE(0x1bb1, 0x0100), /* Seagate Nytro Flash Storage */ 2952 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, }, 2953 { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */ 2954 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, }, 2955 { PCI_DEVICE(0x1c58, 0x0023), /* WDC SN200 adapter */ 2956 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, }, 2957 { PCI_DEVICE(0x1c5f, 0x0540), /* Memblaze Pblaze4 adapter */ 2958 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, }, 2959 { PCI_DEVICE(0x144d, 0xa821), /* Samsung PM1725 */ 2960 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, }, 2961 { PCI_DEVICE(0x144d, 0xa822), /* Samsung PM1725a */ 2962 .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, }, 2963 { PCI_DEVICE(0x1d1d, 0x1f1f), /* LighNVM qemu device */ 2964 .driver_data = NVME_QUIRK_LIGHTNVM, }, 2965 { PCI_DEVICE(0x1d1d, 0x2807), /* CNEX WL */ 2966 .driver_data = NVME_QUIRK_LIGHTNVM, }, 2967 { PCI_DEVICE(0x1d1d, 0x2601), /* CNEX Granby */ 2968 .driver_data = NVME_QUIRK_LIGHTNVM, }, 2969 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) }, 2970 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) }, 2971 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) }, 2972 { 0, } 2973 }; 2974 MODULE_DEVICE_TABLE(pci, nvme_id_table); 2975 2976 static struct pci_driver nvme_driver = { 2977 .name = "nvme", 2978 .id_table = nvme_id_table, 2979 .probe = nvme_probe, 2980 .remove = nvme_remove, 2981 .shutdown = nvme_shutdown, 2982 .driver = { 2983 .pm = &nvme_dev_pm_ops, 2984 }, 2985 .sriov_configure = pci_sriov_configure_simple, 2986 .err_handler = &nvme_err_handler, 2987 }; 2988 2989 static int __init nvme_init(void) 2990 { 2991 return pci_register_driver(&nvme_driver); 2992 } 2993 2994 static void __exit nvme_exit(void) 2995 { 2996 pci_unregister_driver(&nvme_driver); 2997 flush_workqueue(nvme_wq); 2998 _nvme_check_size(); 2999 } 3000 3001 MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>"); 3002 MODULE_LICENSE("GPL"); 3003 MODULE_VERSION("1.0"); 3004 module_init(nvme_init); 3005 module_exit(nvme_exit); 3006