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/blkdev.h> 8 #include <linux/blk-mq.h> 9 #include <linux/delay.h> 10 #include <linux/errno.h> 11 #include <linux/hdreg.h> 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/backing-dev.h> 15 #include <linux/list_sort.h> 16 #include <linux/slab.h> 17 #include <linux/types.h> 18 #include <linux/pr.h> 19 #include <linux/ptrace.h> 20 #include <linux/nvme_ioctl.h> 21 #include <linux/t10-pi.h> 22 #include <linux/pm_qos.h> 23 #include <asm/unaligned.h> 24 25 #include "nvme.h" 26 #include "fabrics.h" 27 28 #define CREATE_TRACE_POINTS 29 #include "trace.h" 30 31 #define NVME_MINORS (1U << MINORBITS) 32 33 unsigned int admin_timeout = 60; 34 module_param(admin_timeout, uint, 0644); 35 MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands"); 36 EXPORT_SYMBOL_GPL(admin_timeout); 37 38 unsigned int nvme_io_timeout = 30; 39 module_param_named(io_timeout, nvme_io_timeout, uint, 0644); 40 MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O"); 41 EXPORT_SYMBOL_GPL(nvme_io_timeout); 42 43 static unsigned char shutdown_timeout = 5; 44 module_param(shutdown_timeout, byte, 0644); 45 MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown"); 46 47 static u8 nvme_max_retries = 5; 48 module_param_named(max_retries, nvme_max_retries, byte, 0644); 49 MODULE_PARM_DESC(max_retries, "max number of retries a command may have"); 50 51 static unsigned long default_ps_max_latency_us = 100000; 52 module_param(default_ps_max_latency_us, ulong, 0644); 53 MODULE_PARM_DESC(default_ps_max_latency_us, 54 "max power saving latency for new devices; use PM QOS to change per device"); 55 56 static bool force_apst; 57 module_param(force_apst, bool, 0644); 58 MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off"); 59 60 static bool streams; 61 module_param(streams, bool, 0644); 62 MODULE_PARM_DESC(streams, "turn on support for Streams write directives"); 63 64 /* 65 * nvme_wq - hosts nvme related works that are not reset or delete 66 * nvme_reset_wq - hosts nvme reset works 67 * nvme_delete_wq - hosts nvme delete works 68 * 69 * nvme_wq will host works such are scan, aen handling, fw activation, 70 * keep-alive error recovery, periodic reconnects etc. nvme_reset_wq 71 * runs reset works which also flush works hosted on nvme_wq for 72 * serialization purposes. nvme_delete_wq host controller deletion 73 * works which flush reset works for serialization. 74 */ 75 struct workqueue_struct *nvme_wq; 76 EXPORT_SYMBOL_GPL(nvme_wq); 77 78 struct workqueue_struct *nvme_reset_wq; 79 EXPORT_SYMBOL_GPL(nvme_reset_wq); 80 81 struct workqueue_struct *nvme_delete_wq; 82 EXPORT_SYMBOL_GPL(nvme_delete_wq); 83 84 static LIST_HEAD(nvme_subsystems); 85 static DEFINE_MUTEX(nvme_subsystems_lock); 86 87 static DEFINE_IDA(nvme_instance_ida); 88 static dev_t nvme_chr_devt; 89 static struct class *nvme_class; 90 static struct class *nvme_subsys_class; 91 92 static int nvme_revalidate_disk(struct gendisk *disk); 93 static void nvme_put_subsystem(struct nvme_subsystem *subsys); 94 static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl, 95 unsigned nsid); 96 97 static void nvme_set_queue_dying(struct nvme_ns *ns) 98 { 99 /* 100 * Revalidating a dead namespace sets capacity to 0. This will end 101 * buffered writers dirtying pages that can't be synced. 102 */ 103 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags)) 104 return; 105 revalidate_disk(ns->disk); 106 blk_set_queue_dying(ns->queue); 107 /* Forcibly unquiesce queues to avoid blocking dispatch */ 108 blk_mq_unquiesce_queue(ns->queue); 109 } 110 111 static void nvme_queue_scan(struct nvme_ctrl *ctrl) 112 { 113 /* 114 * Only new queue scan work when admin and IO queues are both alive 115 */ 116 if (ctrl->state == NVME_CTRL_LIVE) 117 queue_work(nvme_wq, &ctrl->scan_work); 118 } 119 120 int nvme_reset_ctrl(struct nvme_ctrl *ctrl) 121 { 122 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING)) 123 return -EBUSY; 124 if (!queue_work(nvme_reset_wq, &ctrl->reset_work)) 125 return -EBUSY; 126 return 0; 127 } 128 EXPORT_SYMBOL_GPL(nvme_reset_ctrl); 129 130 int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl) 131 { 132 int ret; 133 134 ret = nvme_reset_ctrl(ctrl); 135 if (!ret) { 136 flush_work(&ctrl->reset_work); 137 if (ctrl->state != NVME_CTRL_LIVE && 138 ctrl->state != NVME_CTRL_ADMIN_ONLY) 139 ret = -ENETRESET; 140 } 141 142 return ret; 143 } 144 EXPORT_SYMBOL_GPL(nvme_reset_ctrl_sync); 145 146 static void nvme_do_delete_ctrl(struct nvme_ctrl *ctrl) 147 { 148 dev_info(ctrl->device, 149 "Removing ctrl: NQN \"%s\"\n", ctrl->opts->subsysnqn); 150 151 flush_work(&ctrl->reset_work); 152 nvme_stop_ctrl(ctrl); 153 nvme_remove_namespaces(ctrl); 154 ctrl->ops->delete_ctrl(ctrl); 155 nvme_uninit_ctrl(ctrl); 156 nvme_put_ctrl(ctrl); 157 } 158 159 static void nvme_delete_ctrl_work(struct work_struct *work) 160 { 161 struct nvme_ctrl *ctrl = 162 container_of(work, struct nvme_ctrl, delete_work); 163 164 nvme_do_delete_ctrl(ctrl); 165 } 166 167 int nvme_delete_ctrl(struct nvme_ctrl *ctrl) 168 { 169 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING)) 170 return -EBUSY; 171 if (!queue_work(nvme_delete_wq, &ctrl->delete_work)) 172 return -EBUSY; 173 return 0; 174 } 175 EXPORT_SYMBOL_GPL(nvme_delete_ctrl); 176 177 static int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl) 178 { 179 int ret = 0; 180 181 /* 182 * Keep a reference until nvme_do_delete_ctrl() complete, 183 * since ->delete_ctrl can free the controller. 184 */ 185 nvme_get_ctrl(ctrl); 186 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING)) 187 ret = -EBUSY; 188 if (!ret) 189 nvme_do_delete_ctrl(ctrl); 190 nvme_put_ctrl(ctrl); 191 return ret; 192 } 193 194 static inline bool nvme_ns_has_pi(struct nvme_ns *ns) 195 { 196 return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple); 197 } 198 199 static blk_status_t nvme_error_status(u16 status) 200 { 201 switch (status & 0x7ff) { 202 case NVME_SC_SUCCESS: 203 return BLK_STS_OK; 204 case NVME_SC_CAP_EXCEEDED: 205 return BLK_STS_NOSPC; 206 case NVME_SC_LBA_RANGE: 207 return BLK_STS_TARGET; 208 case NVME_SC_BAD_ATTRIBUTES: 209 case NVME_SC_ONCS_NOT_SUPPORTED: 210 case NVME_SC_INVALID_OPCODE: 211 case NVME_SC_INVALID_FIELD: 212 case NVME_SC_INVALID_NS: 213 return BLK_STS_NOTSUPP; 214 case NVME_SC_WRITE_FAULT: 215 case NVME_SC_READ_ERROR: 216 case NVME_SC_UNWRITTEN_BLOCK: 217 case NVME_SC_ACCESS_DENIED: 218 case NVME_SC_READ_ONLY: 219 case NVME_SC_COMPARE_FAILED: 220 return BLK_STS_MEDIUM; 221 case NVME_SC_GUARD_CHECK: 222 case NVME_SC_APPTAG_CHECK: 223 case NVME_SC_REFTAG_CHECK: 224 case NVME_SC_INVALID_PI: 225 return BLK_STS_PROTECTION; 226 case NVME_SC_RESERVATION_CONFLICT: 227 return BLK_STS_NEXUS; 228 case NVME_SC_HOST_PATH_ERROR: 229 return BLK_STS_TRANSPORT; 230 default: 231 return BLK_STS_IOERR; 232 } 233 } 234 235 static inline bool nvme_req_needs_retry(struct request *req) 236 { 237 if (blk_noretry_request(req)) 238 return false; 239 if (nvme_req(req)->status & NVME_SC_DNR) 240 return false; 241 if (nvme_req(req)->retries >= nvme_max_retries) 242 return false; 243 return true; 244 } 245 246 static void nvme_retry_req(struct request *req) 247 { 248 struct nvme_ns *ns = req->q->queuedata; 249 unsigned long delay = 0; 250 u16 crd; 251 252 /* The mask and shift result must be <= 3 */ 253 crd = (nvme_req(req)->status & NVME_SC_CRD) >> 11; 254 if (ns && crd) 255 delay = ns->ctrl->crdt[crd - 1] * 100; 256 257 nvme_req(req)->retries++; 258 blk_mq_requeue_request(req, false); 259 blk_mq_delay_kick_requeue_list(req->q, delay); 260 } 261 262 void nvme_complete_rq(struct request *req) 263 { 264 blk_status_t status = nvme_error_status(nvme_req(req)->status); 265 266 trace_nvme_complete_rq(req); 267 268 if (nvme_req(req)->ctrl->kas) 269 nvme_req(req)->ctrl->comp_seen = true; 270 271 if (unlikely(status != BLK_STS_OK && nvme_req_needs_retry(req))) { 272 if ((req->cmd_flags & REQ_NVME_MPATH) && 273 blk_path_error(status)) { 274 nvme_failover_req(req); 275 return; 276 } 277 278 if (!blk_queue_dying(req->q)) { 279 nvme_retry_req(req); 280 return; 281 } 282 } 283 284 nvme_trace_bio_complete(req, status); 285 blk_mq_end_request(req, status); 286 } 287 EXPORT_SYMBOL_GPL(nvme_complete_rq); 288 289 bool nvme_cancel_request(struct request *req, void *data, bool reserved) 290 { 291 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device, 292 "Cancelling I/O %d", req->tag); 293 294 /* don't abort one completed request */ 295 if (blk_mq_request_completed(req)) 296 return true; 297 298 nvme_req(req)->status = NVME_SC_HOST_PATH_ERROR; 299 blk_mq_complete_request(req); 300 return true; 301 } 302 EXPORT_SYMBOL_GPL(nvme_cancel_request); 303 304 bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl, 305 enum nvme_ctrl_state new_state) 306 { 307 enum nvme_ctrl_state old_state; 308 unsigned long flags; 309 bool changed = false; 310 311 spin_lock_irqsave(&ctrl->lock, flags); 312 313 old_state = ctrl->state; 314 switch (new_state) { 315 case NVME_CTRL_ADMIN_ONLY: 316 switch (old_state) { 317 case NVME_CTRL_CONNECTING: 318 changed = true; 319 /* FALLTHRU */ 320 default: 321 break; 322 } 323 break; 324 case NVME_CTRL_LIVE: 325 switch (old_state) { 326 case NVME_CTRL_NEW: 327 case NVME_CTRL_RESETTING: 328 case NVME_CTRL_CONNECTING: 329 changed = true; 330 /* FALLTHRU */ 331 default: 332 break; 333 } 334 break; 335 case NVME_CTRL_RESETTING: 336 switch (old_state) { 337 case NVME_CTRL_NEW: 338 case NVME_CTRL_LIVE: 339 case NVME_CTRL_ADMIN_ONLY: 340 changed = true; 341 /* FALLTHRU */ 342 default: 343 break; 344 } 345 break; 346 case NVME_CTRL_CONNECTING: 347 switch (old_state) { 348 case NVME_CTRL_NEW: 349 case NVME_CTRL_RESETTING: 350 changed = true; 351 /* FALLTHRU */ 352 default: 353 break; 354 } 355 break; 356 case NVME_CTRL_DELETING: 357 switch (old_state) { 358 case NVME_CTRL_LIVE: 359 case NVME_CTRL_ADMIN_ONLY: 360 case NVME_CTRL_RESETTING: 361 case NVME_CTRL_CONNECTING: 362 changed = true; 363 /* FALLTHRU */ 364 default: 365 break; 366 } 367 break; 368 case NVME_CTRL_DEAD: 369 switch (old_state) { 370 case NVME_CTRL_DELETING: 371 changed = true; 372 /* FALLTHRU */ 373 default: 374 break; 375 } 376 break; 377 default: 378 break; 379 } 380 381 if (changed) 382 ctrl->state = new_state; 383 384 spin_unlock_irqrestore(&ctrl->lock, flags); 385 if (changed && ctrl->state == NVME_CTRL_LIVE) 386 nvme_kick_requeue_lists(ctrl); 387 return changed; 388 } 389 EXPORT_SYMBOL_GPL(nvme_change_ctrl_state); 390 391 static void nvme_free_ns_head(struct kref *ref) 392 { 393 struct nvme_ns_head *head = 394 container_of(ref, struct nvme_ns_head, ref); 395 396 nvme_mpath_remove_disk(head); 397 ida_simple_remove(&head->subsys->ns_ida, head->instance); 398 list_del_init(&head->entry); 399 cleanup_srcu_struct(&head->srcu); 400 nvme_put_subsystem(head->subsys); 401 kfree(head); 402 } 403 404 static void nvme_put_ns_head(struct nvme_ns_head *head) 405 { 406 kref_put(&head->ref, nvme_free_ns_head); 407 } 408 409 static void nvme_free_ns(struct kref *kref) 410 { 411 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref); 412 413 if (ns->ndev) 414 nvme_nvm_unregister(ns); 415 416 put_disk(ns->disk); 417 nvme_put_ns_head(ns->head); 418 nvme_put_ctrl(ns->ctrl); 419 kfree(ns); 420 } 421 422 static void nvme_put_ns(struct nvme_ns *ns) 423 { 424 kref_put(&ns->kref, nvme_free_ns); 425 } 426 427 static inline void nvme_clear_nvme_request(struct request *req) 428 { 429 if (!(req->rq_flags & RQF_DONTPREP)) { 430 nvme_req(req)->retries = 0; 431 nvme_req(req)->flags = 0; 432 req->rq_flags |= RQF_DONTPREP; 433 } 434 } 435 436 struct request *nvme_alloc_request(struct request_queue *q, 437 struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid) 438 { 439 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN; 440 struct request *req; 441 442 if (qid == NVME_QID_ANY) { 443 req = blk_mq_alloc_request(q, op, flags); 444 } else { 445 req = blk_mq_alloc_request_hctx(q, op, flags, 446 qid ? qid - 1 : 0); 447 } 448 if (IS_ERR(req)) 449 return req; 450 451 req->cmd_flags |= REQ_FAILFAST_DRIVER; 452 nvme_clear_nvme_request(req); 453 nvme_req(req)->cmd = cmd; 454 455 return req; 456 } 457 EXPORT_SYMBOL_GPL(nvme_alloc_request); 458 459 static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable) 460 { 461 struct nvme_command c; 462 463 memset(&c, 0, sizeof(c)); 464 465 c.directive.opcode = nvme_admin_directive_send; 466 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL); 467 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE; 468 c.directive.dtype = NVME_DIR_IDENTIFY; 469 c.directive.tdtype = NVME_DIR_STREAMS; 470 c.directive.endir = enable ? NVME_DIR_ENDIR : 0; 471 472 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0); 473 } 474 475 static int nvme_disable_streams(struct nvme_ctrl *ctrl) 476 { 477 return nvme_toggle_streams(ctrl, false); 478 } 479 480 static int nvme_enable_streams(struct nvme_ctrl *ctrl) 481 { 482 return nvme_toggle_streams(ctrl, true); 483 } 484 485 static int nvme_get_stream_params(struct nvme_ctrl *ctrl, 486 struct streams_directive_params *s, u32 nsid) 487 { 488 struct nvme_command c; 489 490 memset(&c, 0, sizeof(c)); 491 memset(s, 0, sizeof(*s)); 492 493 c.directive.opcode = nvme_admin_directive_recv; 494 c.directive.nsid = cpu_to_le32(nsid); 495 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1); 496 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM; 497 c.directive.dtype = NVME_DIR_STREAMS; 498 499 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s)); 500 } 501 502 static int nvme_configure_directives(struct nvme_ctrl *ctrl) 503 { 504 struct streams_directive_params s; 505 int ret; 506 507 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES)) 508 return 0; 509 if (!streams) 510 return 0; 511 512 ret = nvme_enable_streams(ctrl); 513 if (ret) 514 return ret; 515 516 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL); 517 if (ret) 518 return ret; 519 520 ctrl->nssa = le16_to_cpu(s.nssa); 521 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) { 522 dev_info(ctrl->device, "too few streams (%u) available\n", 523 ctrl->nssa); 524 nvme_disable_streams(ctrl); 525 return 0; 526 } 527 528 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1); 529 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams); 530 return 0; 531 } 532 533 /* 534 * Check if 'req' has a write hint associated with it. If it does, assign 535 * a valid namespace stream to the write. 536 */ 537 static void nvme_assign_write_stream(struct nvme_ctrl *ctrl, 538 struct request *req, u16 *control, 539 u32 *dsmgmt) 540 { 541 enum rw_hint streamid = req->write_hint; 542 543 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE) 544 streamid = 0; 545 else { 546 streamid--; 547 if (WARN_ON_ONCE(streamid > ctrl->nr_streams)) 548 return; 549 550 *control |= NVME_RW_DTYPE_STREAMS; 551 *dsmgmt |= streamid << 16; 552 } 553 554 if (streamid < ARRAY_SIZE(req->q->write_hints)) 555 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9; 556 } 557 558 static inline void nvme_setup_flush(struct nvme_ns *ns, 559 struct nvme_command *cmnd) 560 { 561 cmnd->common.opcode = nvme_cmd_flush; 562 cmnd->common.nsid = cpu_to_le32(ns->head->ns_id); 563 } 564 565 static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req, 566 struct nvme_command *cmnd) 567 { 568 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0; 569 struct nvme_dsm_range *range; 570 struct bio *bio; 571 572 range = kmalloc_array(segments, sizeof(*range), 573 GFP_ATOMIC | __GFP_NOWARN); 574 if (!range) { 575 /* 576 * If we fail allocation our range, fallback to the controller 577 * discard page. If that's also busy, it's safe to return 578 * busy, as we know we can make progress once that's freed. 579 */ 580 if (test_and_set_bit_lock(0, &ns->ctrl->discard_page_busy)) 581 return BLK_STS_RESOURCE; 582 583 range = page_address(ns->ctrl->discard_page); 584 } 585 586 __rq_for_each_bio(bio, req) { 587 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector); 588 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift; 589 590 if (n < segments) { 591 range[n].cattr = cpu_to_le32(0); 592 range[n].nlb = cpu_to_le32(nlb); 593 range[n].slba = cpu_to_le64(slba); 594 } 595 n++; 596 } 597 598 if (WARN_ON_ONCE(n != segments)) { 599 if (virt_to_page(range) == ns->ctrl->discard_page) 600 clear_bit_unlock(0, &ns->ctrl->discard_page_busy); 601 else 602 kfree(range); 603 return BLK_STS_IOERR; 604 } 605 606 cmnd->dsm.opcode = nvme_cmd_dsm; 607 cmnd->dsm.nsid = cpu_to_le32(ns->head->ns_id); 608 cmnd->dsm.nr = cpu_to_le32(segments - 1); 609 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD); 610 611 req->special_vec.bv_page = virt_to_page(range); 612 req->special_vec.bv_offset = offset_in_page(range); 613 req->special_vec.bv_len = sizeof(*range) * segments; 614 req->rq_flags |= RQF_SPECIAL_PAYLOAD; 615 616 return BLK_STS_OK; 617 } 618 619 static inline blk_status_t nvme_setup_write_zeroes(struct nvme_ns *ns, 620 struct request *req, struct nvme_command *cmnd) 621 { 622 if (ns->ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES) 623 return nvme_setup_discard(ns, req, cmnd); 624 625 cmnd->write_zeroes.opcode = nvme_cmd_write_zeroes; 626 cmnd->write_zeroes.nsid = cpu_to_le32(ns->head->ns_id); 627 cmnd->write_zeroes.slba = 628 cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req))); 629 cmnd->write_zeroes.length = 630 cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1); 631 cmnd->write_zeroes.control = 0; 632 return BLK_STS_OK; 633 } 634 635 static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns, 636 struct request *req, struct nvme_command *cmnd) 637 { 638 struct nvme_ctrl *ctrl = ns->ctrl; 639 u16 control = 0; 640 u32 dsmgmt = 0; 641 642 if (req->cmd_flags & REQ_FUA) 643 control |= NVME_RW_FUA; 644 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD)) 645 control |= NVME_RW_LR; 646 647 if (req->cmd_flags & REQ_RAHEAD) 648 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; 649 650 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read); 651 cmnd->rw.nsid = cpu_to_le32(ns->head->ns_id); 652 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req))); 653 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1); 654 655 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams) 656 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt); 657 658 if (ns->ms) { 659 /* 660 * If formated with metadata, the block layer always provides a 661 * metadata buffer if CONFIG_BLK_DEV_INTEGRITY is enabled. Else 662 * we enable the PRACT bit for protection information or set the 663 * namespace capacity to zero to prevent any I/O. 664 */ 665 if (!blk_integrity_rq(req)) { 666 if (WARN_ON_ONCE(!nvme_ns_has_pi(ns))) 667 return BLK_STS_NOTSUPP; 668 control |= NVME_RW_PRINFO_PRACT; 669 } 670 671 switch (ns->pi_type) { 672 case NVME_NS_DPS_PI_TYPE3: 673 control |= NVME_RW_PRINFO_PRCHK_GUARD; 674 break; 675 case NVME_NS_DPS_PI_TYPE1: 676 case NVME_NS_DPS_PI_TYPE2: 677 control |= NVME_RW_PRINFO_PRCHK_GUARD | 678 NVME_RW_PRINFO_PRCHK_REF; 679 cmnd->rw.reftag = cpu_to_le32(t10_pi_ref_tag(req)); 680 break; 681 } 682 } 683 684 cmnd->rw.control = cpu_to_le16(control); 685 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); 686 return 0; 687 } 688 689 void nvme_cleanup_cmd(struct request *req) 690 { 691 if (req->rq_flags & RQF_SPECIAL_PAYLOAD) { 692 struct nvme_ns *ns = req->rq_disk->private_data; 693 struct page *page = req->special_vec.bv_page; 694 695 if (page == ns->ctrl->discard_page) 696 clear_bit_unlock(0, &ns->ctrl->discard_page_busy); 697 else 698 kfree(page_address(page) + req->special_vec.bv_offset); 699 } 700 } 701 EXPORT_SYMBOL_GPL(nvme_cleanup_cmd); 702 703 blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req, 704 struct nvme_command *cmd) 705 { 706 blk_status_t ret = BLK_STS_OK; 707 708 nvme_clear_nvme_request(req); 709 710 memset(cmd, 0, sizeof(*cmd)); 711 switch (req_op(req)) { 712 case REQ_OP_DRV_IN: 713 case REQ_OP_DRV_OUT: 714 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd)); 715 break; 716 case REQ_OP_FLUSH: 717 nvme_setup_flush(ns, cmd); 718 break; 719 case REQ_OP_WRITE_ZEROES: 720 ret = nvme_setup_write_zeroes(ns, req, cmd); 721 break; 722 case REQ_OP_DISCARD: 723 ret = nvme_setup_discard(ns, req, cmd); 724 break; 725 case REQ_OP_READ: 726 case REQ_OP_WRITE: 727 ret = nvme_setup_rw(ns, req, cmd); 728 break; 729 default: 730 WARN_ON_ONCE(1); 731 return BLK_STS_IOERR; 732 } 733 734 cmd->common.command_id = req->tag; 735 trace_nvme_setup_cmd(req, cmd); 736 return ret; 737 } 738 EXPORT_SYMBOL_GPL(nvme_setup_cmd); 739 740 static void nvme_end_sync_rq(struct request *rq, blk_status_t error) 741 { 742 struct completion *waiting = rq->end_io_data; 743 744 rq->end_io_data = NULL; 745 complete(waiting); 746 } 747 748 static void nvme_execute_rq_polled(struct request_queue *q, 749 struct gendisk *bd_disk, struct request *rq, int at_head) 750 { 751 DECLARE_COMPLETION_ONSTACK(wait); 752 753 WARN_ON_ONCE(!test_bit(QUEUE_FLAG_POLL, &q->queue_flags)); 754 755 rq->cmd_flags |= REQ_HIPRI; 756 rq->end_io_data = &wait; 757 blk_execute_rq_nowait(q, bd_disk, rq, at_head, nvme_end_sync_rq); 758 759 while (!completion_done(&wait)) { 760 blk_poll(q, request_to_qc_t(rq->mq_hctx, rq), true); 761 cond_resched(); 762 } 763 } 764 765 /* 766 * Returns 0 on success. If the result is negative, it's a Linux error code; 767 * if the result is positive, it's an NVM Express status code 768 */ 769 int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, 770 union nvme_result *result, void *buffer, unsigned bufflen, 771 unsigned timeout, int qid, int at_head, 772 blk_mq_req_flags_t flags, bool poll) 773 { 774 struct request *req; 775 int ret; 776 777 req = nvme_alloc_request(q, cmd, flags, qid); 778 if (IS_ERR(req)) 779 return PTR_ERR(req); 780 781 req->timeout = timeout ? timeout : ADMIN_TIMEOUT; 782 783 if (buffer && bufflen) { 784 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL); 785 if (ret) 786 goto out; 787 } 788 789 if (poll) 790 nvme_execute_rq_polled(req->q, NULL, req, at_head); 791 else 792 blk_execute_rq(req->q, NULL, req, at_head); 793 if (result) 794 *result = nvme_req(req)->result; 795 if (nvme_req(req)->flags & NVME_REQ_CANCELLED) 796 ret = -EINTR; 797 else 798 ret = nvme_req(req)->status; 799 out: 800 blk_mq_free_request(req); 801 return ret; 802 } 803 EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd); 804 805 int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, 806 void *buffer, unsigned bufflen) 807 { 808 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0, 809 NVME_QID_ANY, 0, 0, false); 810 } 811 EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd); 812 813 static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf, 814 unsigned len, u32 seed, bool write) 815 { 816 struct bio_integrity_payload *bip; 817 int ret = -ENOMEM; 818 void *buf; 819 820 buf = kmalloc(len, GFP_KERNEL); 821 if (!buf) 822 goto out; 823 824 ret = -EFAULT; 825 if (write && copy_from_user(buf, ubuf, len)) 826 goto out_free_meta; 827 828 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1); 829 if (IS_ERR(bip)) { 830 ret = PTR_ERR(bip); 831 goto out_free_meta; 832 } 833 834 bip->bip_iter.bi_size = len; 835 bip->bip_iter.bi_sector = seed; 836 ret = bio_integrity_add_page(bio, virt_to_page(buf), len, 837 offset_in_page(buf)); 838 if (ret == len) 839 return buf; 840 ret = -ENOMEM; 841 out_free_meta: 842 kfree(buf); 843 out: 844 return ERR_PTR(ret); 845 } 846 847 static int nvme_submit_user_cmd(struct request_queue *q, 848 struct nvme_command *cmd, void __user *ubuffer, 849 unsigned bufflen, void __user *meta_buffer, unsigned meta_len, 850 u32 meta_seed, u32 *result, unsigned timeout) 851 { 852 bool write = nvme_is_write(cmd); 853 struct nvme_ns *ns = q->queuedata; 854 struct gendisk *disk = ns ? ns->disk : NULL; 855 struct request *req; 856 struct bio *bio = NULL; 857 void *meta = NULL; 858 int ret; 859 860 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY); 861 if (IS_ERR(req)) 862 return PTR_ERR(req); 863 864 req->timeout = timeout ? timeout : ADMIN_TIMEOUT; 865 nvme_req(req)->flags |= NVME_REQ_USERCMD; 866 867 if (ubuffer && bufflen) { 868 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen, 869 GFP_KERNEL); 870 if (ret) 871 goto out; 872 bio = req->bio; 873 bio->bi_disk = disk; 874 if (disk && meta_buffer && meta_len) { 875 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len, 876 meta_seed, write); 877 if (IS_ERR(meta)) { 878 ret = PTR_ERR(meta); 879 goto out_unmap; 880 } 881 req->cmd_flags |= REQ_INTEGRITY; 882 } 883 } 884 885 blk_execute_rq(req->q, disk, req, 0); 886 if (nvme_req(req)->flags & NVME_REQ_CANCELLED) 887 ret = -EINTR; 888 else 889 ret = nvme_req(req)->status; 890 if (result) 891 *result = le32_to_cpu(nvme_req(req)->result.u32); 892 if (meta && !ret && !write) { 893 if (copy_to_user(meta_buffer, meta, meta_len)) 894 ret = -EFAULT; 895 } 896 kfree(meta); 897 out_unmap: 898 if (bio) 899 blk_rq_unmap_user(bio); 900 out: 901 blk_mq_free_request(req); 902 return ret; 903 } 904 905 static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status) 906 { 907 struct nvme_ctrl *ctrl = rq->end_io_data; 908 unsigned long flags; 909 bool startka = false; 910 911 blk_mq_free_request(rq); 912 913 if (status) { 914 dev_err(ctrl->device, 915 "failed nvme_keep_alive_end_io error=%d\n", 916 status); 917 return; 918 } 919 920 ctrl->comp_seen = false; 921 spin_lock_irqsave(&ctrl->lock, flags); 922 if (ctrl->state == NVME_CTRL_LIVE || 923 ctrl->state == NVME_CTRL_CONNECTING) 924 startka = true; 925 spin_unlock_irqrestore(&ctrl->lock, flags); 926 if (startka) 927 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ); 928 } 929 930 static int nvme_keep_alive(struct nvme_ctrl *ctrl) 931 { 932 struct request *rq; 933 934 rq = nvme_alloc_request(ctrl->admin_q, &ctrl->ka_cmd, BLK_MQ_REQ_RESERVED, 935 NVME_QID_ANY); 936 if (IS_ERR(rq)) 937 return PTR_ERR(rq); 938 939 rq->timeout = ctrl->kato * HZ; 940 rq->end_io_data = ctrl; 941 942 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io); 943 944 return 0; 945 } 946 947 static void nvme_keep_alive_work(struct work_struct *work) 948 { 949 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work), 950 struct nvme_ctrl, ka_work); 951 bool comp_seen = ctrl->comp_seen; 952 953 if ((ctrl->ctratt & NVME_CTRL_ATTR_TBKAS) && comp_seen) { 954 dev_dbg(ctrl->device, 955 "reschedule traffic based keep-alive timer\n"); 956 ctrl->comp_seen = false; 957 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ); 958 return; 959 } 960 961 if (nvme_keep_alive(ctrl)) { 962 /* allocation failure, reset the controller */ 963 dev_err(ctrl->device, "keep-alive failed\n"); 964 nvme_reset_ctrl(ctrl); 965 return; 966 } 967 } 968 969 static void nvme_start_keep_alive(struct nvme_ctrl *ctrl) 970 { 971 if (unlikely(ctrl->kato == 0)) 972 return; 973 974 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ); 975 } 976 977 void nvme_stop_keep_alive(struct nvme_ctrl *ctrl) 978 { 979 if (unlikely(ctrl->kato == 0)) 980 return; 981 982 cancel_delayed_work_sync(&ctrl->ka_work); 983 } 984 EXPORT_SYMBOL_GPL(nvme_stop_keep_alive); 985 986 static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id) 987 { 988 struct nvme_command c = { }; 989 int error; 990 991 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ 992 c.identify.opcode = nvme_admin_identify; 993 c.identify.cns = NVME_ID_CNS_CTRL; 994 995 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL); 996 if (!*id) 997 return -ENOMEM; 998 999 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, 1000 sizeof(struct nvme_id_ctrl)); 1001 if (error) 1002 kfree(*id); 1003 return error; 1004 } 1005 1006 static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid, 1007 struct nvme_ns_ids *ids) 1008 { 1009 struct nvme_command c = { }; 1010 int status; 1011 void *data; 1012 int pos; 1013 int len; 1014 1015 c.identify.opcode = nvme_admin_identify; 1016 c.identify.nsid = cpu_to_le32(nsid); 1017 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST; 1018 1019 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL); 1020 if (!data) 1021 return -ENOMEM; 1022 1023 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data, 1024 NVME_IDENTIFY_DATA_SIZE); 1025 if (status) 1026 goto free_data; 1027 1028 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) { 1029 struct nvme_ns_id_desc *cur = data + pos; 1030 1031 if (cur->nidl == 0) 1032 break; 1033 1034 switch (cur->nidt) { 1035 case NVME_NIDT_EUI64: 1036 if (cur->nidl != NVME_NIDT_EUI64_LEN) { 1037 dev_warn(ctrl->device, 1038 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n", 1039 cur->nidl); 1040 goto free_data; 1041 } 1042 len = NVME_NIDT_EUI64_LEN; 1043 memcpy(ids->eui64, data + pos + sizeof(*cur), len); 1044 break; 1045 case NVME_NIDT_NGUID: 1046 if (cur->nidl != NVME_NIDT_NGUID_LEN) { 1047 dev_warn(ctrl->device, 1048 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n", 1049 cur->nidl); 1050 goto free_data; 1051 } 1052 len = NVME_NIDT_NGUID_LEN; 1053 memcpy(ids->nguid, data + pos + sizeof(*cur), len); 1054 break; 1055 case NVME_NIDT_UUID: 1056 if (cur->nidl != NVME_NIDT_UUID_LEN) { 1057 dev_warn(ctrl->device, 1058 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n", 1059 cur->nidl); 1060 goto free_data; 1061 } 1062 len = NVME_NIDT_UUID_LEN; 1063 uuid_copy(&ids->uuid, data + pos + sizeof(*cur)); 1064 break; 1065 default: 1066 /* Skip unknown types */ 1067 len = cur->nidl; 1068 break; 1069 } 1070 1071 len += sizeof(*cur); 1072 } 1073 free_data: 1074 kfree(data); 1075 return status; 1076 } 1077 1078 static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list) 1079 { 1080 struct nvme_command c = { }; 1081 1082 c.identify.opcode = nvme_admin_identify; 1083 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST; 1084 c.identify.nsid = cpu_to_le32(nsid); 1085 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 1086 NVME_IDENTIFY_DATA_SIZE); 1087 } 1088 1089 static int nvme_identify_ns(struct nvme_ctrl *ctrl, 1090 unsigned nsid, struct nvme_id_ns **id) 1091 { 1092 struct nvme_command c = { }; 1093 int error; 1094 1095 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ 1096 c.identify.opcode = nvme_admin_identify; 1097 c.identify.nsid = cpu_to_le32(nsid); 1098 c.identify.cns = NVME_ID_CNS_NS; 1099 1100 *id = kmalloc(sizeof(**id), GFP_KERNEL); 1101 if (!*id) 1102 return -ENOMEM; 1103 1104 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, *id, sizeof(**id)); 1105 if (error) { 1106 dev_warn(ctrl->device, "Identify namespace failed (%d)\n", error); 1107 kfree(*id); 1108 } 1109 1110 return error; 1111 } 1112 1113 static int nvme_features(struct nvme_ctrl *dev, u8 op, unsigned int fid, 1114 unsigned int dword11, void *buffer, size_t buflen, u32 *result) 1115 { 1116 struct nvme_command c; 1117 union nvme_result res; 1118 int ret; 1119 1120 memset(&c, 0, sizeof(c)); 1121 c.features.opcode = op; 1122 c.features.fid = cpu_to_le32(fid); 1123 c.features.dword11 = cpu_to_le32(dword11); 1124 1125 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, 1126 buffer, buflen, 0, NVME_QID_ANY, 0, 0, false); 1127 if (ret >= 0 && result) 1128 *result = le32_to_cpu(res.u32); 1129 return ret; 1130 } 1131 1132 int nvme_set_features(struct nvme_ctrl *dev, unsigned int fid, 1133 unsigned int dword11, void *buffer, size_t buflen, 1134 u32 *result) 1135 { 1136 return nvme_features(dev, nvme_admin_set_features, fid, dword11, buffer, 1137 buflen, result); 1138 } 1139 EXPORT_SYMBOL_GPL(nvme_set_features); 1140 1141 int nvme_get_features(struct nvme_ctrl *dev, unsigned int fid, 1142 unsigned int dword11, void *buffer, size_t buflen, 1143 u32 *result) 1144 { 1145 return nvme_features(dev, nvme_admin_get_features, fid, dword11, buffer, 1146 buflen, result); 1147 } 1148 EXPORT_SYMBOL_GPL(nvme_get_features); 1149 1150 int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count) 1151 { 1152 u32 q_count = (*count - 1) | ((*count - 1) << 16); 1153 u32 result; 1154 int status, nr_io_queues; 1155 1156 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0, 1157 &result); 1158 if (status < 0) 1159 return status; 1160 1161 /* 1162 * Degraded controllers might return an error when setting the queue 1163 * count. We still want to be able to bring them online and offer 1164 * access to the admin queue, as that might be only way to fix them up. 1165 */ 1166 if (status > 0) { 1167 dev_err(ctrl->device, "Could not set queue count (%d)\n", status); 1168 *count = 0; 1169 } else { 1170 nr_io_queues = min(result & 0xffff, result >> 16) + 1; 1171 *count = min(*count, nr_io_queues); 1172 } 1173 1174 return 0; 1175 } 1176 EXPORT_SYMBOL_GPL(nvme_set_queue_count); 1177 1178 #define NVME_AEN_SUPPORTED \ 1179 (NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_FW_ACT | \ 1180 NVME_AEN_CFG_ANA_CHANGE | NVME_AEN_CFG_DISC_CHANGE) 1181 1182 static void nvme_enable_aen(struct nvme_ctrl *ctrl) 1183 { 1184 u32 result, supported_aens = ctrl->oaes & NVME_AEN_SUPPORTED; 1185 int status; 1186 1187 if (!supported_aens) 1188 return; 1189 1190 status = nvme_set_features(ctrl, NVME_FEAT_ASYNC_EVENT, supported_aens, 1191 NULL, 0, &result); 1192 if (status) 1193 dev_warn(ctrl->device, "Failed to configure AEN (cfg %x)\n", 1194 supported_aens); 1195 1196 queue_work(nvme_wq, &ctrl->async_event_work); 1197 } 1198 1199 static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio) 1200 { 1201 struct nvme_user_io io; 1202 struct nvme_command c; 1203 unsigned length, meta_len; 1204 void __user *metadata; 1205 1206 if (copy_from_user(&io, uio, sizeof(io))) 1207 return -EFAULT; 1208 if (io.flags) 1209 return -EINVAL; 1210 1211 switch (io.opcode) { 1212 case nvme_cmd_write: 1213 case nvme_cmd_read: 1214 case nvme_cmd_compare: 1215 break; 1216 default: 1217 return -EINVAL; 1218 } 1219 1220 length = (io.nblocks + 1) << ns->lba_shift; 1221 meta_len = (io.nblocks + 1) * ns->ms; 1222 metadata = (void __user *)(uintptr_t)io.metadata; 1223 1224 if (ns->ext) { 1225 length += meta_len; 1226 meta_len = 0; 1227 } else if (meta_len) { 1228 if ((io.metadata & 3) || !io.metadata) 1229 return -EINVAL; 1230 } 1231 1232 memset(&c, 0, sizeof(c)); 1233 c.rw.opcode = io.opcode; 1234 c.rw.flags = io.flags; 1235 c.rw.nsid = cpu_to_le32(ns->head->ns_id); 1236 c.rw.slba = cpu_to_le64(io.slba); 1237 c.rw.length = cpu_to_le16(io.nblocks); 1238 c.rw.control = cpu_to_le16(io.control); 1239 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt); 1240 c.rw.reftag = cpu_to_le32(io.reftag); 1241 c.rw.apptag = cpu_to_le16(io.apptag); 1242 c.rw.appmask = cpu_to_le16(io.appmask); 1243 1244 return nvme_submit_user_cmd(ns->queue, &c, 1245 (void __user *)(uintptr_t)io.addr, length, 1246 metadata, meta_len, lower_32_bits(io.slba), NULL, 0); 1247 } 1248 1249 static u32 nvme_known_admin_effects(u8 opcode) 1250 { 1251 switch (opcode) { 1252 case nvme_admin_format_nvm: 1253 return NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC | 1254 NVME_CMD_EFFECTS_CSE_MASK; 1255 case nvme_admin_sanitize_nvm: 1256 return NVME_CMD_EFFECTS_CSE_MASK; 1257 default: 1258 break; 1259 } 1260 return 0; 1261 } 1262 1263 static u32 nvme_passthru_start(struct nvme_ctrl *ctrl, struct nvme_ns *ns, 1264 u8 opcode) 1265 { 1266 u32 effects = 0; 1267 1268 if (ns) { 1269 if (ctrl->effects) 1270 effects = le32_to_cpu(ctrl->effects->iocs[opcode]); 1271 if (effects & ~(NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC)) 1272 dev_warn(ctrl->device, 1273 "IO command:%02x has unhandled effects:%08x\n", 1274 opcode, effects); 1275 return 0; 1276 } 1277 1278 if (ctrl->effects) 1279 effects = le32_to_cpu(ctrl->effects->acs[opcode]); 1280 effects |= nvme_known_admin_effects(opcode); 1281 1282 /* 1283 * For simplicity, IO to all namespaces is quiesced even if the command 1284 * effects say only one namespace is affected. 1285 */ 1286 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) { 1287 mutex_lock(&ctrl->scan_lock); 1288 mutex_lock(&ctrl->subsys->lock); 1289 nvme_mpath_start_freeze(ctrl->subsys); 1290 nvme_mpath_wait_freeze(ctrl->subsys); 1291 nvme_start_freeze(ctrl); 1292 nvme_wait_freeze(ctrl); 1293 } 1294 return effects; 1295 } 1296 1297 static void nvme_update_formats(struct nvme_ctrl *ctrl) 1298 { 1299 struct nvme_ns *ns; 1300 1301 down_read(&ctrl->namespaces_rwsem); 1302 list_for_each_entry(ns, &ctrl->namespaces, list) 1303 if (ns->disk && nvme_revalidate_disk(ns->disk)) 1304 nvme_set_queue_dying(ns); 1305 up_read(&ctrl->namespaces_rwsem); 1306 1307 nvme_remove_invalid_namespaces(ctrl, NVME_NSID_ALL); 1308 } 1309 1310 static void nvme_passthru_end(struct nvme_ctrl *ctrl, u32 effects) 1311 { 1312 /* 1313 * Revalidate LBA changes prior to unfreezing. This is necessary to 1314 * prevent memory corruption if a logical block size was changed by 1315 * this command. 1316 */ 1317 if (effects & NVME_CMD_EFFECTS_LBCC) 1318 nvme_update_formats(ctrl); 1319 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) { 1320 nvme_unfreeze(ctrl); 1321 nvme_mpath_unfreeze(ctrl->subsys); 1322 mutex_unlock(&ctrl->subsys->lock); 1323 mutex_unlock(&ctrl->scan_lock); 1324 } 1325 if (effects & NVME_CMD_EFFECTS_CCC) 1326 nvme_init_identify(ctrl); 1327 if (effects & (NVME_CMD_EFFECTS_NIC | NVME_CMD_EFFECTS_NCC)) 1328 nvme_queue_scan(ctrl); 1329 } 1330 1331 static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns, 1332 struct nvme_passthru_cmd __user *ucmd) 1333 { 1334 struct nvme_passthru_cmd cmd; 1335 struct nvme_command c; 1336 unsigned timeout = 0; 1337 u32 effects; 1338 int status; 1339 1340 if (!capable(CAP_SYS_ADMIN)) 1341 return -EACCES; 1342 if (copy_from_user(&cmd, ucmd, sizeof(cmd))) 1343 return -EFAULT; 1344 if (cmd.flags) 1345 return -EINVAL; 1346 1347 memset(&c, 0, sizeof(c)); 1348 c.common.opcode = cmd.opcode; 1349 c.common.flags = cmd.flags; 1350 c.common.nsid = cpu_to_le32(cmd.nsid); 1351 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2); 1352 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3); 1353 c.common.cdw10 = cpu_to_le32(cmd.cdw10); 1354 c.common.cdw11 = cpu_to_le32(cmd.cdw11); 1355 c.common.cdw12 = cpu_to_le32(cmd.cdw12); 1356 c.common.cdw13 = cpu_to_le32(cmd.cdw13); 1357 c.common.cdw14 = cpu_to_le32(cmd.cdw14); 1358 c.common.cdw15 = cpu_to_le32(cmd.cdw15); 1359 1360 if (cmd.timeout_ms) 1361 timeout = msecs_to_jiffies(cmd.timeout_ms); 1362 1363 effects = nvme_passthru_start(ctrl, ns, cmd.opcode); 1364 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c, 1365 (void __user *)(uintptr_t)cmd.addr, cmd.data_len, 1366 (void __user *)(uintptr_t)cmd.metadata, cmd.metadata_len, 1367 0, &cmd.result, timeout); 1368 nvme_passthru_end(ctrl, effects); 1369 1370 if (status >= 0) { 1371 if (put_user(cmd.result, &ucmd->result)) 1372 return -EFAULT; 1373 } 1374 1375 return status; 1376 } 1377 1378 /* 1379 * Issue ioctl requests on the first available path. Note that unlike normal 1380 * block layer requests we will not retry failed request on another controller. 1381 */ 1382 static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk, 1383 struct nvme_ns_head **head, int *srcu_idx) 1384 { 1385 #ifdef CONFIG_NVME_MULTIPATH 1386 if (disk->fops == &nvme_ns_head_ops) { 1387 struct nvme_ns *ns; 1388 1389 *head = disk->private_data; 1390 *srcu_idx = srcu_read_lock(&(*head)->srcu); 1391 ns = nvme_find_path(*head); 1392 if (!ns) 1393 srcu_read_unlock(&(*head)->srcu, *srcu_idx); 1394 return ns; 1395 } 1396 #endif 1397 *head = NULL; 1398 *srcu_idx = -1; 1399 return disk->private_data; 1400 } 1401 1402 static void nvme_put_ns_from_disk(struct nvme_ns_head *head, int idx) 1403 { 1404 if (head) 1405 srcu_read_unlock(&head->srcu, idx); 1406 } 1407 1408 static int nvme_ioctl(struct block_device *bdev, fmode_t mode, 1409 unsigned int cmd, unsigned long arg) 1410 { 1411 struct nvme_ns_head *head = NULL; 1412 void __user *argp = (void __user *)arg; 1413 struct nvme_ns *ns; 1414 int srcu_idx, ret; 1415 1416 ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx); 1417 if (unlikely(!ns)) 1418 return -EWOULDBLOCK; 1419 1420 /* 1421 * Handle ioctls that apply to the controller instead of the namespace 1422 * seperately and drop the ns SRCU reference early. This avoids a 1423 * deadlock when deleting namespaces using the passthrough interface. 1424 */ 1425 if (cmd == NVME_IOCTL_ADMIN_CMD || is_sed_ioctl(cmd)) { 1426 struct nvme_ctrl *ctrl = ns->ctrl; 1427 1428 nvme_get_ctrl(ns->ctrl); 1429 nvme_put_ns_from_disk(head, srcu_idx); 1430 1431 if (cmd == NVME_IOCTL_ADMIN_CMD) 1432 ret = nvme_user_cmd(ctrl, NULL, argp); 1433 else 1434 ret = sed_ioctl(ctrl->opal_dev, cmd, argp); 1435 1436 nvme_put_ctrl(ctrl); 1437 return ret; 1438 } 1439 1440 switch (cmd) { 1441 case NVME_IOCTL_ID: 1442 force_successful_syscall_return(); 1443 ret = ns->head->ns_id; 1444 break; 1445 case NVME_IOCTL_IO_CMD: 1446 ret = nvme_user_cmd(ns->ctrl, ns, argp); 1447 break; 1448 case NVME_IOCTL_SUBMIT_IO: 1449 ret = nvme_submit_io(ns, argp); 1450 break; 1451 default: 1452 if (ns->ndev) 1453 ret = nvme_nvm_ioctl(ns, cmd, arg); 1454 else 1455 ret = -ENOTTY; 1456 } 1457 1458 nvme_put_ns_from_disk(head, srcu_idx); 1459 return ret; 1460 } 1461 1462 static int nvme_open(struct block_device *bdev, fmode_t mode) 1463 { 1464 struct nvme_ns *ns = bdev->bd_disk->private_data; 1465 1466 #ifdef CONFIG_NVME_MULTIPATH 1467 /* should never be called due to GENHD_FL_HIDDEN */ 1468 if (WARN_ON_ONCE(ns->head->disk)) 1469 goto fail; 1470 #endif 1471 if (!kref_get_unless_zero(&ns->kref)) 1472 goto fail; 1473 if (!try_module_get(ns->ctrl->ops->module)) 1474 goto fail_put_ns; 1475 1476 return 0; 1477 1478 fail_put_ns: 1479 nvme_put_ns(ns); 1480 fail: 1481 return -ENXIO; 1482 } 1483 1484 static void nvme_release(struct gendisk *disk, fmode_t mode) 1485 { 1486 struct nvme_ns *ns = disk->private_data; 1487 1488 module_put(ns->ctrl->ops->module); 1489 nvme_put_ns(ns); 1490 } 1491 1492 static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo) 1493 { 1494 /* some standard values */ 1495 geo->heads = 1 << 6; 1496 geo->sectors = 1 << 5; 1497 geo->cylinders = get_capacity(bdev->bd_disk) >> 11; 1498 return 0; 1499 } 1500 1501 #ifdef CONFIG_BLK_DEV_INTEGRITY 1502 static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type) 1503 { 1504 struct blk_integrity integrity; 1505 1506 memset(&integrity, 0, sizeof(integrity)); 1507 switch (pi_type) { 1508 case NVME_NS_DPS_PI_TYPE3: 1509 integrity.profile = &t10_pi_type3_crc; 1510 integrity.tag_size = sizeof(u16) + sizeof(u32); 1511 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; 1512 break; 1513 case NVME_NS_DPS_PI_TYPE1: 1514 case NVME_NS_DPS_PI_TYPE2: 1515 integrity.profile = &t10_pi_type1_crc; 1516 integrity.tag_size = sizeof(u16); 1517 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; 1518 break; 1519 default: 1520 integrity.profile = NULL; 1521 break; 1522 } 1523 integrity.tuple_size = ms; 1524 blk_integrity_register(disk, &integrity); 1525 blk_queue_max_integrity_segments(disk->queue, 1); 1526 } 1527 #else 1528 static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type) 1529 { 1530 } 1531 #endif /* CONFIG_BLK_DEV_INTEGRITY */ 1532 1533 static void nvme_set_chunk_size(struct nvme_ns *ns) 1534 { 1535 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9)); 1536 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size)); 1537 } 1538 1539 static void nvme_config_discard(struct gendisk *disk, struct nvme_ns *ns) 1540 { 1541 struct nvme_ctrl *ctrl = ns->ctrl; 1542 struct request_queue *queue = disk->queue; 1543 u32 size = queue_logical_block_size(queue); 1544 1545 if (!(ctrl->oncs & NVME_CTRL_ONCS_DSM)) { 1546 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, queue); 1547 return; 1548 } 1549 1550 if (ctrl->nr_streams && ns->sws && ns->sgs) 1551 size *= ns->sws * ns->sgs; 1552 1553 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) < 1554 NVME_DSM_MAX_RANGES); 1555 1556 queue->limits.discard_alignment = 0; 1557 queue->limits.discard_granularity = size; 1558 1559 /* If discard is already enabled, don't reset queue limits */ 1560 if (blk_queue_flag_test_and_set(QUEUE_FLAG_DISCARD, queue)) 1561 return; 1562 1563 blk_queue_max_discard_sectors(queue, UINT_MAX); 1564 blk_queue_max_discard_segments(queue, NVME_DSM_MAX_RANGES); 1565 1566 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES) 1567 blk_queue_max_write_zeroes_sectors(queue, UINT_MAX); 1568 } 1569 1570 static void nvme_config_write_zeroes(struct gendisk *disk, struct nvme_ns *ns) 1571 { 1572 u32 max_sectors; 1573 unsigned short bs = 1 << ns->lba_shift; 1574 1575 if (!(ns->ctrl->oncs & NVME_CTRL_ONCS_WRITE_ZEROES) || 1576 (ns->ctrl->quirks & NVME_QUIRK_DISABLE_WRITE_ZEROES)) 1577 return; 1578 /* 1579 * Even though NVMe spec explicitly states that MDTS is not 1580 * applicable to the write-zeroes:- "The restriction does not apply to 1581 * commands that do not transfer data between the host and the 1582 * controller (e.g., Write Uncorrectable ro Write Zeroes command).". 1583 * In order to be more cautious use controller's max_hw_sectors value 1584 * to configure the maximum sectors for the write-zeroes which is 1585 * configured based on the controller's MDTS field in the 1586 * nvme_init_identify() if available. 1587 */ 1588 if (ns->ctrl->max_hw_sectors == UINT_MAX) 1589 max_sectors = ((u32)(USHRT_MAX + 1) * bs) >> 9; 1590 else 1591 max_sectors = ((u32)(ns->ctrl->max_hw_sectors + 1) * bs) >> 9; 1592 1593 blk_queue_max_write_zeroes_sectors(disk->queue, max_sectors); 1594 } 1595 1596 static int nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid, 1597 struct nvme_id_ns *id, struct nvme_ns_ids *ids) 1598 { 1599 int ret = 0; 1600 1601 memset(ids, 0, sizeof(*ids)); 1602 1603 if (ctrl->vs >= NVME_VS(1, 1, 0)) 1604 memcpy(ids->eui64, id->eui64, sizeof(id->eui64)); 1605 if (ctrl->vs >= NVME_VS(1, 2, 0)) 1606 memcpy(ids->nguid, id->nguid, sizeof(id->nguid)); 1607 if (ctrl->vs >= NVME_VS(1, 3, 0)) { 1608 /* Don't treat error as fatal we potentially 1609 * already have a NGUID or EUI-64 1610 */ 1611 ret = nvme_identify_ns_descs(ctrl, nsid, ids); 1612 if (ret) 1613 dev_warn(ctrl->device, 1614 "Identify Descriptors failed (%d)\n", ret); 1615 } 1616 return ret; 1617 } 1618 1619 static bool nvme_ns_ids_valid(struct nvme_ns_ids *ids) 1620 { 1621 return !uuid_is_null(&ids->uuid) || 1622 memchr_inv(ids->nguid, 0, sizeof(ids->nguid)) || 1623 memchr_inv(ids->eui64, 0, sizeof(ids->eui64)); 1624 } 1625 1626 static bool nvme_ns_ids_equal(struct nvme_ns_ids *a, struct nvme_ns_ids *b) 1627 { 1628 return uuid_equal(&a->uuid, &b->uuid) && 1629 memcmp(&a->nguid, &b->nguid, sizeof(a->nguid)) == 0 && 1630 memcmp(&a->eui64, &b->eui64, sizeof(a->eui64)) == 0; 1631 } 1632 1633 static void nvme_update_disk_info(struct gendisk *disk, 1634 struct nvme_ns *ns, struct nvme_id_ns *id) 1635 { 1636 sector_t capacity = le64_to_cpu(id->nsze) << (ns->lba_shift - 9); 1637 unsigned short bs = 1 << ns->lba_shift; 1638 u32 atomic_bs, phys_bs, io_opt; 1639 1640 if (ns->lba_shift > PAGE_SHIFT) { 1641 /* unsupported block size, set capacity to 0 later */ 1642 bs = (1 << 9); 1643 } 1644 blk_mq_freeze_queue(disk->queue); 1645 blk_integrity_unregister(disk); 1646 1647 if (id->nabo == 0) { 1648 /* 1649 * Bit 1 indicates whether NAWUPF is defined for this namespace 1650 * and whether it should be used instead of AWUPF. If NAWUPF == 1651 * 0 then AWUPF must be used instead. 1652 */ 1653 if (id->nsfeat & (1 << 1) && id->nawupf) 1654 atomic_bs = (1 + le16_to_cpu(id->nawupf)) * bs; 1655 else 1656 atomic_bs = (1 + ns->ctrl->subsys->awupf) * bs; 1657 } else { 1658 atomic_bs = bs; 1659 } 1660 phys_bs = bs; 1661 io_opt = bs; 1662 if (id->nsfeat & (1 << 4)) { 1663 /* NPWG = Namespace Preferred Write Granularity */ 1664 phys_bs *= 1 + le16_to_cpu(id->npwg); 1665 /* NOWS = Namespace Optimal Write Size */ 1666 io_opt *= 1 + le16_to_cpu(id->nows); 1667 } 1668 1669 blk_queue_logical_block_size(disk->queue, bs); 1670 /* 1671 * Linux filesystems assume writing a single physical block is 1672 * an atomic operation. Hence limit the physical block size to the 1673 * value of the Atomic Write Unit Power Fail parameter. 1674 */ 1675 blk_queue_physical_block_size(disk->queue, min(phys_bs, atomic_bs)); 1676 blk_queue_io_min(disk->queue, phys_bs); 1677 blk_queue_io_opt(disk->queue, io_opt); 1678 1679 if (ns->ms && !ns->ext && 1680 (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED)) 1681 nvme_init_integrity(disk, ns->ms, ns->pi_type); 1682 if ((ns->ms && !nvme_ns_has_pi(ns) && !blk_get_integrity(disk)) || 1683 ns->lba_shift > PAGE_SHIFT) 1684 capacity = 0; 1685 1686 set_capacity(disk, capacity); 1687 1688 nvme_config_discard(disk, ns); 1689 nvme_config_write_zeroes(disk, ns); 1690 1691 if (id->nsattr & (1 << 0)) 1692 set_disk_ro(disk, true); 1693 else 1694 set_disk_ro(disk, false); 1695 1696 blk_mq_unfreeze_queue(disk->queue); 1697 } 1698 1699 static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id) 1700 { 1701 struct nvme_ns *ns = disk->private_data; 1702 1703 /* 1704 * If identify namespace failed, use default 512 byte block size so 1705 * block layer can use before failing read/write for 0 capacity. 1706 */ 1707 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds; 1708 if (ns->lba_shift == 0) 1709 ns->lba_shift = 9; 1710 ns->noiob = le16_to_cpu(id->noiob); 1711 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms); 1712 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT); 1713 /* the PI implementation requires metadata equal t10 pi tuple size */ 1714 if (ns->ms == sizeof(struct t10_pi_tuple)) 1715 ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK; 1716 else 1717 ns->pi_type = 0; 1718 1719 if (ns->noiob) 1720 nvme_set_chunk_size(ns); 1721 nvme_update_disk_info(disk, ns, id); 1722 #ifdef CONFIG_NVME_MULTIPATH 1723 if (ns->head->disk) { 1724 nvme_update_disk_info(ns->head->disk, ns, id); 1725 blk_queue_stack_limits(ns->head->disk->queue, ns->queue); 1726 revalidate_disk(ns->head->disk); 1727 } 1728 #endif 1729 } 1730 1731 static int nvme_revalidate_disk(struct gendisk *disk) 1732 { 1733 struct nvme_ns *ns = disk->private_data; 1734 struct nvme_ctrl *ctrl = ns->ctrl; 1735 struct nvme_id_ns *id; 1736 struct nvme_ns_ids ids; 1737 int ret = 0; 1738 1739 if (test_bit(NVME_NS_DEAD, &ns->flags)) { 1740 set_capacity(disk, 0); 1741 return -ENODEV; 1742 } 1743 1744 ret = nvme_identify_ns(ctrl, ns->head->ns_id, &id); 1745 if (ret) 1746 goto out; 1747 1748 if (id->ncap == 0) { 1749 ret = -ENODEV; 1750 goto free_id; 1751 } 1752 1753 __nvme_revalidate_disk(disk, id); 1754 ret = nvme_report_ns_ids(ctrl, ns->head->ns_id, id, &ids); 1755 if (ret) 1756 goto free_id; 1757 1758 if (!nvme_ns_ids_equal(&ns->head->ids, &ids)) { 1759 dev_err(ctrl->device, 1760 "identifiers changed for nsid %d\n", ns->head->ns_id); 1761 ret = -ENODEV; 1762 } 1763 1764 free_id: 1765 kfree(id); 1766 out: 1767 /* 1768 * Only fail the function if we got a fatal error back from the 1769 * device, otherwise ignore the error and just move on. 1770 */ 1771 if (ret == -ENOMEM || (ret > 0 && !(ret & NVME_SC_DNR))) 1772 ret = 0; 1773 else if (ret > 0) 1774 ret = blk_status_to_errno(nvme_error_status(ret)); 1775 return ret; 1776 } 1777 1778 static char nvme_pr_type(enum pr_type type) 1779 { 1780 switch (type) { 1781 case PR_WRITE_EXCLUSIVE: 1782 return 1; 1783 case PR_EXCLUSIVE_ACCESS: 1784 return 2; 1785 case PR_WRITE_EXCLUSIVE_REG_ONLY: 1786 return 3; 1787 case PR_EXCLUSIVE_ACCESS_REG_ONLY: 1788 return 4; 1789 case PR_WRITE_EXCLUSIVE_ALL_REGS: 1790 return 5; 1791 case PR_EXCLUSIVE_ACCESS_ALL_REGS: 1792 return 6; 1793 default: 1794 return 0; 1795 } 1796 }; 1797 1798 static int nvme_pr_command(struct block_device *bdev, u32 cdw10, 1799 u64 key, u64 sa_key, u8 op) 1800 { 1801 struct nvme_ns_head *head = NULL; 1802 struct nvme_ns *ns; 1803 struct nvme_command c; 1804 int srcu_idx, ret; 1805 u8 data[16] = { 0, }; 1806 1807 ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx); 1808 if (unlikely(!ns)) 1809 return -EWOULDBLOCK; 1810 1811 put_unaligned_le64(key, &data[0]); 1812 put_unaligned_le64(sa_key, &data[8]); 1813 1814 memset(&c, 0, sizeof(c)); 1815 c.common.opcode = op; 1816 c.common.nsid = cpu_to_le32(ns->head->ns_id); 1817 c.common.cdw10 = cpu_to_le32(cdw10); 1818 1819 ret = nvme_submit_sync_cmd(ns->queue, &c, data, 16); 1820 nvme_put_ns_from_disk(head, srcu_idx); 1821 return ret; 1822 } 1823 1824 static int nvme_pr_register(struct block_device *bdev, u64 old, 1825 u64 new, unsigned flags) 1826 { 1827 u32 cdw10; 1828 1829 if (flags & ~PR_FL_IGNORE_KEY) 1830 return -EOPNOTSUPP; 1831 1832 cdw10 = old ? 2 : 0; 1833 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0; 1834 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */ 1835 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register); 1836 } 1837 1838 static int nvme_pr_reserve(struct block_device *bdev, u64 key, 1839 enum pr_type type, unsigned flags) 1840 { 1841 u32 cdw10; 1842 1843 if (flags & ~PR_FL_IGNORE_KEY) 1844 return -EOPNOTSUPP; 1845 1846 cdw10 = nvme_pr_type(type) << 8; 1847 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0); 1848 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire); 1849 } 1850 1851 static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new, 1852 enum pr_type type, bool abort) 1853 { 1854 u32 cdw10 = nvme_pr_type(type) << 8 | (abort ? 2 : 1); 1855 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire); 1856 } 1857 1858 static int nvme_pr_clear(struct block_device *bdev, u64 key) 1859 { 1860 u32 cdw10 = 1 | (key ? 1 << 3 : 0); 1861 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register); 1862 } 1863 1864 static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type) 1865 { 1866 u32 cdw10 = nvme_pr_type(type) << 8 | (key ? 1 << 3 : 0); 1867 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release); 1868 } 1869 1870 static const struct pr_ops nvme_pr_ops = { 1871 .pr_register = nvme_pr_register, 1872 .pr_reserve = nvme_pr_reserve, 1873 .pr_release = nvme_pr_release, 1874 .pr_preempt = nvme_pr_preempt, 1875 .pr_clear = nvme_pr_clear, 1876 }; 1877 1878 #ifdef CONFIG_BLK_SED_OPAL 1879 int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len, 1880 bool send) 1881 { 1882 struct nvme_ctrl *ctrl = data; 1883 struct nvme_command cmd; 1884 1885 memset(&cmd, 0, sizeof(cmd)); 1886 if (send) 1887 cmd.common.opcode = nvme_admin_security_send; 1888 else 1889 cmd.common.opcode = nvme_admin_security_recv; 1890 cmd.common.nsid = 0; 1891 cmd.common.cdw10 = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8); 1892 cmd.common.cdw11 = cpu_to_le32(len); 1893 1894 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len, 1895 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0, false); 1896 } 1897 EXPORT_SYMBOL_GPL(nvme_sec_submit); 1898 #endif /* CONFIG_BLK_SED_OPAL */ 1899 1900 static const struct block_device_operations nvme_fops = { 1901 .owner = THIS_MODULE, 1902 .ioctl = nvme_ioctl, 1903 .compat_ioctl = nvme_ioctl, 1904 .open = nvme_open, 1905 .release = nvme_release, 1906 .getgeo = nvme_getgeo, 1907 .revalidate_disk= nvme_revalidate_disk, 1908 .pr_ops = &nvme_pr_ops, 1909 }; 1910 1911 #ifdef CONFIG_NVME_MULTIPATH 1912 static int nvme_ns_head_open(struct block_device *bdev, fmode_t mode) 1913 { 1914 struct nvme_ns_head *head = bdev->bd_disk->private_data; 1915 1916 if (!kref_get_unless_zero(&head->ref)) 1917 return -ENXIO; 1918 return 0; 1919 } 1920 1921 static void nvme_ns_head_release(struct gendisk *disk, fmode_t mode) 1922 { 1923 nvme_put_ns_head(disk->private_data); 1924 } 1925 1926 const struct block_device_operations nvme_ns_head_ops = { 1927 .owner = THIS_MODULE, 1928 .open = nvme_ns_head_open, 1929 .release = nvme_ns_head_release, 1930 .ioctl = nvme_ioctl, 1931 .compat_ioctl = nvme_ioctl, 1932 .getgeo = nvme_getgeo, 1933 .pr_ops = &nvme_pr_ops, 1934 }; 1935 #endif /* CONFIG_NVME_MULTIPATH */ 1936 1937 static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled) 1938 { 1939 unsigned long timeout = 1940 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies; 1941 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0; 1942 int ret; 1943 1944 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { 1945 if (csts == ~0) 1946 return -ENODEV; 1947 if ((csts & NVME_CSTS_RDY) == bit) 1948 break; 1949 1950 msleep(100); 1951 if (fatal_signal_pending(current)) 1952 return -EINTR; 1953 if (time_after(jiffies, timeout)) { 1954 dev_err(ctrl->device, 1955 "Device not ready; aborting %s\n", enabled ? 1956 "initialisation" : "reset"); 1957 return -ENODEV; 1958 } 1959 } 1960 1961 return ret; 1962 } 1963 1964 /* 1965 * If the device has been passed off to us in an enabled state, just clear 1966 * the enabled bit. The spec says we should set the 'shutdown notification 1967 * bits', but doing so may cause the device to complete commands to the 1968 * admin queue ... and we don't know what memory that might be pointing at! 1969 */ 1970 int nvme_disable_ctrl(struct nvme_ctrl *ctrl) 1971 { 1972 int ret; 1973 1974 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; 1975 ctrl->ctrl_config &= ~NVME_CC_ENABLE; 1976 1977 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); 1978 if (ret) 1979 return ret; 1980 1981 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY) 1982 msleep(NVME_QUIRK_DELAY_AMOUNT); 1983 1984 return nvme_wait_ready(ctrl, ctrl->cap, false); 1985 } 1986 EXPORT_SYMBOL_GPL(nvme_disable_ctrl); 1987 1988 int nvme_enable_ctrl(struct nvme_ctrl *ctrl) 1989 { 1990 /* 1991 * Default to a 4K page size, with the intention to update this 1992 * path in the future to accomodate architectures with differing 1993 * kernel and IO page sizes. 1994 */ 1995 unsigned dev_page_min, page_shift = 12; 1996 int ret; 1997 1998 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &ctrl->cap); 1999 if (ret) { 2000 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret); 2001 return ret; 2002 } 2003 dev_page_min = NVME_CAP_MPSMIN(ctrl->cap) + 12; 2004 2005 if (page_shift < dev_page_min) { 2006 dev_err(ctrl->device, 2007 "Minimum device page size %u too large for host (%u)\n", 2008 1 << dev_page_min, 1 << page_shift); 2009 return -ENODEV; 2010 } 2011 2012 ctrl->page_size = 1 << page_shift; 2013 2014 ctrl->ctrl_config = NVME_CC_CSS_NVM; 2015 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT; 2016 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE; 2017 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; 2018 ctrl->ctrl_config |= NVME_CC_ENABLE; 2019 2020 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); 2021 if (ret) 2022 return ret; 2023 return nvme_wait_ready(ctrl, ctrl->cap, true); 2024 } 2025 EXPORT_SYMBOL_GPL(nvme_enable_ctrl); 2026 2027 int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl) 2028 { 2029 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ); 2030 u32 csts; 2031 int ret; 2032 2033 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; 2034 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL; 2035 2036 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); 2037 if (ret) 2038 return ret; 2039 2040 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { 2041 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT) 2042 break; 2043 2044 msleep(100); 2045 if (fatal_signal_pending(current)) 2046 return -EINTR; 2047 if (time_after(jiffies, timeout)) { 2048 dev_err(ctrl->device, 2049 "Device shutdown incomplete; abort shutdown\n"); 2050 return -ENODEV; 2051 } 2052 } 2053 2054 return ret; 2055 } 2056 EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl); 2057 2058 static void nvme_set_queue_limits(struct nvme_ctrl *ctrl, 2059 struct request_queue *q) 2060 { 2061 bool vwc = false; 2062 2063 if (ctrl->max_hw_sectors) { 2064 u32 max_segments = 2065 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1; 2066 2067 max_segments = min_not_zero(max_segments, ctrl->max_segments); 2068 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors); 2069 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX)); 2070 } 2071 if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) && 2072 is_power_of_2(ctrl->max_hw_sectors)) 2073 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors); 2074 blk_queue_virt_boundary(q, ctrl->page_size - 1); 2075 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT) 2076 vwc = true; 2077 blk_queue_write_cache(q, vwc, vwc); 2078 } 2079 2080 static int nvme_configure_timestamp(struct nvme_ctrl *ctrl) 2081 { 2082 __le64 ts; 2083 int ret; 2084 2085 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP)) 2086 return 0; 2087 2088 ts = cpu_to_le64(ktime_to_ms(ktime_get_real())); 2089 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts), 2090 NULL); 2091 if (ret) 2092 dev_warn_once(ctrl->device, 2093 "could not set timestamp (%d)\n", ret); 2094 return ret; 2095 } 2096 2097 static int nvme_configure_acre(struct nvme_ctrl *ctrl) 2098 { 2099 struct nvme_feat_host_behavior *host; 2100 int ret; 2101 2102 /* Don't bother enabling the feature if retry delay is not reported */ 2103 if (!ctrl->crdt[0]) 2104 return 0; 2105 2106 host = kzalloc(sizeof(*host), GFP_KERNEL); 2107 if (!host) 2108 return 0; 2109 2110 host->acre = NVME_ENABLE_ACRE; 2111 ret = nvme_set_features(ctrl, NVME_FEAT_HOST_BEHAVIOR, 0, 2112 host, sizeof(*host), NULL); 2113 kfree(host); 2114 return ret; 2115 } 2116 2117 static int nvme_configure_apst(struct nvme_ctrl *ctrl) 2118 { 2119 /* 2120 * APST (Autonomous Power State Transition) lets us program a 2121 * table of power state transitions that the controller will 2122 * perform automatically. We configure it with a simple 2123 * heuristic: we are willing to spend at most 2% of the time 2124 * transitioning between power states. Therefore, when running 2125 * in any given state, we will enter the next lower-power 2126 * non-operational state after waiting 50 * (enlat + exlat) 2127 * microseconds, as long as that state's exit latency is under 2128 * the requested maximum latency. 2129 * 2130 * We will not autonomously enter any non-operational state for 2131 * which the total latency exceeds ps_max_latency_us. Users 2132 * can set ps_max_latency_us to zero to turn off APST. 2133 */ 2134 2135 unsigned apste; 2136 struct nvme_feat_auto_pst *table; 2137 u64 max_lat_us = 0; 2138 int max_ps = -1; 2139 int ret; 2140 2141 /* 2142 * If APST isn't supported or if we haven't been initialized yet, 2143 * then don't do anything. 2144 */ 2145 if (!ctrl->apsta) 2146 return 0; 2147 2148 if (ctrl->npss > 31) { 2149 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n"); 2150 return 0; 2151 } 2152 2153 table = kzalloc(sizeof(*table), GFP_KERNEL); 2154 if (!table) 2155 return 0; 2156 2157 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) { 2158 /* Turn off APST. */ 2159 apste = 0; 2160 dev_dbg(ctrl->device, "APST disabled\n"); 2161 } else { 2162 __le64 target = cpu_to_le64(0); 2163 int state; 2164 2165 /* 2166 * Walk through all states from lowest- to highest-power. 2167 * According to the spec, lower-numbered states use more 2168 * power. NPSS, despite the name, is the index of the 2169 * lowest-power state, not the number of states. 2170 */ 2171 for (state = (int)ctrl->npss; state >= 0; state--) { 2172 u64 total_latency_us, exit_latency_us, transition_ms; 2173 2174 if (target) 2175 table->entries[state] = target; 2176 2177 /* 2178 * Don't allow transitions to the deepest state 2179 * if it's quirked off. 2180 */ 2181 if (state == ctrl->npss && 2182 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) 2183 continue; 2184 2185 /* 2186 * Is this state a useful non-operational state for 2187 * higher-power states to autonomously transition to? 2188 */ 2189 if (!(ctrl->psd[state].flags & 2190 NVME_PS_FLAGS_NON_OP_STATE)) 2191 continue; 2192 2193 exit_latency_us = 2194 (u64)le32_to_cpu(ctrl->psd[state].exit_lat); 2195 if (exit_latency_us > ctrl->ps_max_latency_us) 2196 continue; 2197 2198 total_latency_us = 2199 exit_latency_us + 2200 le32_to_cpu(ctrl->psd[state].entry_lat); 2201 2202 /* 2203 * This state is good. Use it as the APST idle 2204 * target for higher power states. 2205 */ 2206 transition_ms = total_latency_us + 19; 2207 do_div(transition_ms, 20); 2208 if (transition_ms > (1 << 24) - 1) 2209 transition_ms = (1 << 24) - 1; 2210 2211 target = cpu_to_le64((state << 3) | 2212 (transition_ms << 8)); 2213 2214 if (max_ps == -1) 2215 max_ps = state; 2216 2217 if (total_latency_us > max_lat_us) 2218 max_lat_us = total_latency_us; 2219 } 2220 2221 apste = 1; 2222 2223 if (max_ps == -1) { 2224 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n"); 2225 } else { 2226 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n", 2227 max_ps, max_lat_us, (int)sizeof(*table), table); 2228 } 2229 } 2230 2231 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste, 2232 table, sizeof(*table), NULL); 2233 if (ret) 2234 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret); 2235 2236 kfree(table); 2237 return ret; 2238 } 2239 2240 static void nvme_set_latency_tolerance(struct device *dev, s32 val) 2241 { 2242 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 2243 u64 latency; 2244 2245 switch (val) { 2246 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT: 2247 case PM_QOS_LATENCY_ANY: 2248 latency = U64_MAX; 2249 break; 2250 2251 default: 2252 latency = val; 2253 } 2254 2255 if (ctrl->ps_max_latency_us != latency) { 2256 ctrl->ps_max_latency_us = latency; 2257 nvme_configure_apst(ctrl); 2258 } 2259 } 2260 2261 struct nvme_core_quirk_entry { 2262 /* 2263 * NVMe model and firmware strings are padded with spaces. For 2264 * simplicity, strings in the quirk table are padded with NULLs 2265 * instead. 2266 */ 2267 u16 vid; 2268 const char *mn; 2269 const char *fr; 2270 unsigned long quirks; 2271 }; 2272 2273 static const struct nvme_core_quirk_entry core_quirks[] = { 2274 { 2275 /* 2276 * This Toshiba device seems to die using any APST states. See: 2277 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11 2278 */ 2279 .vid = 0x1179, 2280 .mn = "THNSF5256GPUK TOSHIBA", 2281 .quirks = NVME_QUIRK_NO_APST, 2282 }, 2283 { 2284 /* 2285 * This LiteON CL1-3D*-Q11 firmware version has a race 2286 * condition associated with actions related to suspend to idle 2287 * LiteON has resolved the problem in future firmware 2288 */ 2289 .vid = 0x14a4, 2290 .fr = "22301111", 2291 .quirks = NVME_QUIRK_SIMPLE_SUSPEND, 2292 } 2293 }; 2294 2295 /* match is null-terminated but idstr is space-padded. */ 2296 static bool string_matches(const char *idstr, const char *match, size_t len) 2297 { 2298 size_t matchlen; 2299 2300 if (!match) 2301 return true; 2302 2303 matchlen = strlen(match); 2304 WARN_ON_ONCE(matchlen > len); 2305 2306 if (memcmp(idstr, match, matchlen)) 2307 return false; 2308 2309 for (; matchlen < len; matchlen++) 2310 if (idstr[matchlen] != ' ') 2311 return false; 2312 2313 return true; 2314 } 2315 2316 static bool quirk_matches(const struct nvme_id_ctrl *id, 2317 const struct nvme_core_quirk_entry *q) 2318 { 2319 return q->vid == le16_to_cpu(id->vid) && 2320 string_matches(id->mn, q->mn, sizeof(id->mn)) && 2321 string_matches(id->fr, q->fr, sizeof(id->fr)); 2322 } 2323 2324 static void nvme_init_subnqn(struct nvme_subsystem *subsys, struct nvme_ctrl *ctrl, 2325 struct nvme_id_ctrl *id) 2326 { 2327 size_t nqnlen; 2328 int off; 2329 2330 if(!(ctrl->quirks & NVME_QUIRK_IGNORE_DEV_SUBNQN)) { 2331 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE); 2332 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) { 2333 strlcpy(subsys->subnqn, id->subnqn, NVMF_NQN_SIZE); 2334 return; 2335 } 2336 2337 if (ctrl->vs >= NVME_VS(1, 2, 1)) 2338 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n"); 2339 } 2340 2341 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */ 2342 off = snprintf(subsys->subnqn, NVMF_NQN_SIZE, 2343 "nqn.2014.08.org.nvmexpress:%04x%04x", 2344 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid)); 2345 memcpy(subsys->subnqn + off, id->sn, sizeof(id->sn)); 2346 off += sizeof(id->sn); 2347 memcpy(subsys->subnqn + off, id->mn, sizeof(id->mn)); 2348 off += sizeof(id->mn); 2349 memset(subsys->subnqn + off, 0, sizeof(subsys->subnqn) - off); 2350 } 2351 2352 static void nvme_release_subsystem(struct device *dev) 2353 { 2354 struct nvme_subsystem *subsys = 2355 container_of(dev, struct nvme_subsystem, dev); 2356 2357 if (subsys->instance >= 0) 2358 ida_simple_remove(&nvme_instance_ida, subsys->instance); 2359 kfree(subsys); 2360 } 2361 2362 static void nvme_destroy_subsystem(struct kref *ref) 2363 { 2364 struct nvme_subsystem *subsys = 2365 container_of(ref, struct nvme_subsystem, ref); 2366 2367 mutex_lock(&nvme_subsystems_lock); 2368 list_del(&subsys->entry); 2369 mutex_unlock(&nvme_subsystems_lock); 2370 2371 ida_destroy(&subsys->ns_ida); 2372 device_del(&subsys->dev); 2373 put_device(&subsys->dev); 2374 } 2375 2376 static void nvme_put_subsystem(struct nvme_subsystem *subsys) 2377 { 2378 kref_put(&subsys->ref, nvme_destroy_subsystem); 2379 } 2380 2381 static struct nvme_subsystem *__nvme_find_get_subsystem(const char *subsysnqn) 2382 { 2383 struct nvme_subsystem *subsys; 2384 2385 lockdep_assert_held(&nvme_subsystems_lock); 2386 2387 /* 2388 * Fail matches for discovery subsystems. This results 2389 * in each discovery controller bound to a unique subsystem. 2390 * This avoids issues with validating controller values 2391 * that can only be true when there is a single unique subsystem. 2392 * There may be multiple and completely independent entities 2393 * that provide discovery controllers. 2394 */ 2395 if (!strcmp(subsysnqn, NVME_DISC_SUBSYS_NAME)) 2396 return NULL; 2397 2398 list_for_each_entry(subsys, &nvme_subsystems, entry) { 2399 if (strcmp(subsys->subnqn, subsysnqn)) 2400 continue; 2401 if (!kref_get_unless_zero(&subsys->ref)) 2402 continue; 2403 return subsys; 2404 } 2405 2406 return NULL; 2407 } 2408 2409 #define SUBSYS_ATTR_RO(_name, _mode, _show) \ 2410 struct device_attribute subsys_attr_##_name = \ 2411 __ATTR(_name, _mode, _show, NULL) 2412 2413 static ssize_t nvme_subsys_show_nqn(struct device *dev, 2414 struct device_attribute *attr, 2415 char *buf) 2416 { 2417 struct nvme_subsystem *subsys = 2418 container_of(dev, struct nvme_subsystem, dev); 2419 2420 return snprintf(buf, PAGE_SIZE, "%s\n", subsys->subnqn); 2421 } 2422 static SUBSYS_ATTR_RO(subsysnqn, S_IRUGO, nvme_subsys_show_nqn); 2423 2424 #define nvme_subsys_show_str_function(field) \ 2425 static ssize_t subsys_##field##_show(struct device *dev, \ 2426 struct device_attribute *attr, char *buf) \ 2427 { \ 2428 struct nvme_subsystem *subsys = \ 2429 container_of(dev, struct nvme_subsystem, dev); \ 2430 return sprintf(buf, "%.*s\n", \ 2431 (int)sizeof(subsys->field), subsys->field); \ 2432 } \ 2433 static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show); 2434 2435 nvme_subsys_show_str_function(model); 2436 nvme_subsys_show_str_function(serial); 2437 nvme_subsys_show_str_function(firmware_rev); 2438 2439 static struct attribute *nvme_subsys_attrs[] = { 2440 &subsys_attr_model.attr, 2441 &subsys_attr_serial.attr, 2442 &subsys_attr_firmware_rev.attr, 2443 &subsys_attr_subsysnqn.attr, 2444 #ifdef CONFIG_NVME_MULTIPATH 2445 &subsys_attr_iopolicy.attr, 2446 #endif 2447 NULL, 2448 }; 2449 2450 static struct attribute_group nvme_subsys_attrs_group = { 2451 .attrs = nvme_subsys_attrs, 2452 }; 2453 2454 static const struct attribute_group *nvme_subsys_attrs_groups[] = { 2455 &nvme_subsys_attrs_group, 2456 NULL, 2457 }; 2458 2459 static bool nvme_validate_cntlid(struct nvme_subsystem *subsys, 2460 struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) 2461 { 2462 struct nvme_ctrl *tmp; 2463 2464 lockdep_assert_held(&nvme_subsystems_lock); 2465 2466 list_for_each_entry(tmp, &subsys->ctrls, subsys_entry) { 2467 if (tmp->state == NVME_CTRL_DELETING || 2468 tmp->state == NVME_CTRL_DEAD) 2469 continue; 2470 2471 if (tmp->cntlid == ctrl->cntlid) { 2472 dev_err(ctrl->device, 2473 "Duplicate cntlid %u with %s, rejecting\n", 2474 ctrl->cntlid, dev_name(tmp->device)); 2475 return false; 2476 } 2477 2478 if ((id->cmic & (1 << 1)) || 2479 (ctrl->opts && ctrl->opts->discovery_nqn)) 2480 continue; 2481 2482 dev_err(ctrl->device, 2483 "Subsystem does not support multiple controllers\n"); 2484 return false; 2485 } 2486 2487 return true; 2488 } 2489 2490 static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) 2491 { 2492 struct nvme_subsystem *subsys, *found; 2493 int ret; 2494 2495 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL); 2496 if (!subsys) 2497 return -ENOMEM; 2498 2499 subsys->instance = -1; 2500 mutex_init(&subsys->lock); 2501 kref_init(&subsys->ref); 2502 INIT_LIST_HEAD(&subsys->ctrls); 2503 INIT_LIST_HEAD(&subsys->nsheads); 2504 nvme_init_subnqn(subsys, ctrl, id); 2505 memcpy(subsys->serial, id->sn, sizeof(subsys->serial)); 2506 memcpy(subsys->model, id->mn, sizeof(subsys->model)); 2507 memcpy(subsys->firmware_rev, id->fr, sizeof(subsys->firmware_rev)); 2508 subsys->vendor_id = le16_to_cpu(id->vid); 2509 subsys->cmic = id->cmic; 2510 subsys->awupf = le16_to_cpu(id->awupf); 2511 #ifdef CONFIG_NVME_MULTIPATH 2512 subsys->iopolicy = NVME_IOPOLICY_NUMA; 2513 #endif 2514 2515 subsys->dev.class = nvme_subsys_class; 2516 subsys->dev.release = nvme_release_subsystem; 2517 subsys->dev.groups = nvme_subsys_attrs_groups; 2518 dev_set_name(&subsys->dev, "nvme-subsys%d", ctrl->instance); 2519 device_initialize(&subsys->dev); 2520 2521 mutex_lock(&nvme_subsystems_lock); 2522 found = __nvme_find_get_subsystem(subsys->subnqn); 2523 if (found) { 2524 put_device(&subsys->dev); 2525 subsys = found; 2526 2527 if (!nvme_validate_cntlid(subsys, ctrl, id)) { 2528 ret = -EINVAL; 2529 goto out_put_subsystem; 2530 } 2531 } else { 2532 ret = device_add(&subsys->dev); 2533 if (ret) { 2534 dev_err(ctrl->device, 2535 "failed to register subsystem device.\n"); 2536 put_device(&subsys->dev); 2537 goto out_unlock; 2538 } 2539 ida_init(&subsys->ns_ida); 2540 list_add_tail(&subsys->entry, &nvme_subsystems); 2541 } 2542 2543 if (sysfs_create_link(&subsys->dev.kobj, &ctrl->device->kobj, 2544 dev_name(ctrl->device))) { 2545 dev_err(ctrl->device, 2546 "failed to create sysfs link from subsystem.\n"); 2547 goto out_put_subsystem; 2548 } 2549 2550 if (!found) 2551 subsys->instance = ctrl->instance; 2552 ctrl->subsys = subsys; 2553 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls); 2554 mutex_unlock(&nvme_subsystems_lock); 2555 return 0; 2556 2557 out_put_subsystem: 2558 nvme_put_subsystem(subsys); 2559 out_unlock: 2560 mutex_unlock(&nvme_subsystems_lock); 2561 return ret; 2562 } 2563 2564 int nvme_get_log(struct nvme_ctrl *ctrl, u32 nsid, u8 log_page, u8 lsp, 2565 void *log, size_t size, u64 offset) 2566 { 2567 struct nvme_command c = { }; 2568 unsigned long dwlen = size / 4 - 1; 2569 2570 c.get_log_page.opcode = nvme_admin_get_log_page; 2571 c.get_log_page.nsid = cpu_to_le32(nsid); 2572 c.get_log_page.lid = log_page; 2573 c.get_log_page.lsp = lsp; 2574 c.get_log_page.numdl = cpu_to_le16(dwlen & ((1 << 16) - 1)); 2575 c.get_log_page.numdu = cpu_to_le16(dwlen >> 16); 2576 c.get_log_page.lpol = cpu_to_le32(lower_32_bits(offset)); 2577 c.get_log_page.lpou = cpu_to_le32(upper_32_bits(offset)); 2578 2579 return nvme_submit_sync_cmd(ctrl->admin_q, &c, log, size); 2580 } 2581 2582 static int nvme_get_effects_log(struct nvme_ctrl *ctrl) 2583 { 2584 int ret; 2585 2586 if (!ctrl->effects) 2587 ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL); 2588 2589 if (!ctrl->effects) 2590 return 0; 2591 2592 ret = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_CMD_EFFECTS, 0, 2593 ctrl->effects, sizeof(*ctrl->effects), 0); 2594 if (ret) { 2595 kfree(ctrl->effects); 2596 ctrl->effects = NULL; 2597 } 2598 return ret; 2599 } 2600 2601 /* 2602 * Initialize the cached copies of the Identify data and various controller 2603 * register in our nvme_ctrl structure. This should be called as soon as 2604 * the admin queue is fully up and running. 2605 */ 2606 int nvme_init_identify(struct nvme_ctrl *ctrl) 2607 { 2608 struct nvme_id_ctrl *id; 2609 int ret, page_shift; 2610 u32 max_hw_sectors; 2611 bool prev_apst_enabled; 2612 2613 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs); 2614 if (ret) { 2615 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret); 2616 return ret; 2617 } 2618 page_shift = NVME_CAP_MPSMIN(ctrl->cap) + 12; 2619 ctrl->sqsize = min_t(int, NVME_CAP_MQES(ctrl->cap), ctrl->sqsize); 2620 2621 if (ctrl->vs >= NVME_VS(1, 1, 0)) 2622 ctrl->subsystem = NVME_CAP_NSSRC(ctrl->cap); 2623 2624 ret = nvme_identify_ctrl(ctrl, &id); 2625 if (ret) { 2626 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret); 2627 return -EIO; 2628 } 2629 2630 if (id->lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG) { 2631 ret = nvme_get_effects_log(ctrl); 2632 if (ret < 0) 2633 goto out_free; 2634 } 2635 2636 if (!(ctrl->ops->flags & NVME_F_FABRICS)) 2637 ctrl->cntlid = le16_to_cpu(id->cntlid); 2638 2639 if (!ctrl->identified) { 2640 int i; 2641 2642 ret = nvme_init_subsystem(ctrl, id); 2643 if (ret) 2644 goto out_free; 2645 2646 /* 2647 * Check for quirks. Quirk can depend on firmware version, 2648 * so, in principle, the set of quirks present can change 2649 * across a reset. As a possible future enhancement, we 2650 * could re-scan for quirks every time we reinitialize 2651 * the device, but we'd have to make sure that the driver 2652 * behaves intelligently if the quirks change. 2653 */ 2654 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) { 2655 if (quirk_matches(id, &core_quirks[i])) 2656 ctrl->quirks |= core_quirks[i].quirks; 2657 } 2658 } 2659 2660 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) { 2661 dev_warn(ctrl->device, "forcibly allowing all power states due to nvme_core.force_apst -- use at your own risk\n"); 2662 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS; 2663 } 2664 2665 ctrl->crdt[0] = le16_to_cpu(id->crdt1); 2666 ctrl->crdt[1] = le16_to_cpu(id->crdt2); 2667 ctrl->crdt[2] = le16_to_cpu(id->crdt3); 2668 2669 ctrl->oacs = le16_to_cpu(id->oacs); 2670 ctrl->oncs = le16_to_cpu(id->oncs); 2671 ctrl->mtfa = le16_to_cpu(id->mtfa); 2672 ctrl->oaes = le32_to_cpu(id->oaes); 2673 atomic_set(&ctrl->abort_limit, id->acl + 1); 2674 ctrl->vwc = id->vwc; 2675 if (id->mdts) 2676 max_hw_sectors = 1 << (id->mdts + page_shift - 9); 2677 else 2678 max_hw_sectors = UINT_MAX; 2679 ctrl->max_hw_sectors = 2680 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors); 2681 2682 nvme_set_queue_limits(ctrl, ctrl->admin_q); 2683 ctrl->sgls = le32_to_cpu(id->sgls); 2684 ctrl->kas = le16_to_cpu(id->kas); 2685 ctrl->max_namespaces = le32_to_cpu(id->mnan); 2686 ctrl->ctratt = le32_to_cpu(id->ctratt); 2687 2688 if (id->rtd3e) { 2689 /* us -> s */ 2690 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000; 2691 2692 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time, 2693 shutdown_timeout, 60); 2694 2695 if (ctrl->shutdown_timeout != shutdown_timeout) 2696 dev_info(ctrl->device, 2697 "Shutdown timeout set to %u seconds\n", 2698 ctrl->shutdown_timeout); 2699 } else 2700 ctrl->shutdown_timeout = shutdown_timeout; 2701 2702 ctrl->npss = id->npss; 2703 ctrl->apsta = id->apsta; 2704 prev_apst_enabled = ctrl->apst_enabled; 2705 if (ctrl->quirks & NVME_QUIRK_NO_APST) { 2706 if (force_apst && id->apsta) { 2707 dev_warn(ctrl->device, "forcibly allowing APST due to nvme_core.force_apst -- use at your own risk\n"); 2708 ctrl->apst_enabled = true; 2709 } else { 2710 ctrl->apst_enabled = false; 2711 } 2712 } else { 2713 ctrl->apst_enabled = id->apsta; 2714 } 2715 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd)); 2716 2717 if (ctrl->ops->flags & NVME_F_FABRICS) { 2718 ctrl->icdoff = le16_to_cpu(id->icdoff); 2719 ctrl->ioccsz = le32_to_cpu(id->ioccsz); 2720 ctrl->iorcsz = le32_to_cpu(id->iorcsz); 2721 ctrl->maxcmd = le16_to_cpu(id->maxcmd); 2722 2723 /* 2724 * In fabrics we need to verify the cntlid matches the 2725 * admin connect 2726 */ 2727 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) { 2728 ret = -EINVAL; 2729 goto out_free; 2730 } 2731 2732 if (!ctrl->opts->discovery_nqn && !ctrl->kas) { 2733 dev_err(ctrl->device, 2734 "keep-alive support is mandatory for fabrics\n"); 2735 ret = -EINVAL; 2736 goto out_free; 2737 } 2738 } else { 2739 ctrl->hmpre = le32_to_cpu(id->hmpre); 2740 ctrl->hmmin = le32_to_cpu(id->hmmin); 2741 ctrl->hmminds = le32_to_cpu(id->hmminds); 2742 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd); 2743 } 2744 2745 ret = nvme_mpath_init(ctrl, id); 2746 kfree(id); 2747 2748 if (ret < 0) 2749 return ret; 2750 2751 if (ctrl->apst_enabled && !prev_apst_enabled) 2752 dev_pm_qos_expose_latency_tolerance(ctrl->device); 2753 else if (!ctrl->apst_enabled && prev_apst_enabled) 2754 dev_pm_qos_hide_latency_tolerance(ctrl->device); 2755 2756 ret = nvme_configure_apst(ctrl); 2757 if (ret < 0) 2758 return ret; 2759 2760 ret = nvme_configure_timestamp(ctrl); 2761 if (ret < 0) 2762 return ret; 2763 2764 ret = nvme_configure_directives(ctrl); 2765 if (ret < 0) 2766 return ret; 2767 2768 ret = nvme_configure_acre(ctrl); 2769 if (ret < 0) 2770 return ret; 2771 2772 ctrl->identified = true; 2773 2774 return 0; 2775 2776 out_free: 2777 kfree(id); 2778 return ret; 2779 } 2780 EXPORT_SYMBOL_GPL(nvme_init_identify); 2781 2782 static int nvme_dev_open(struct inode *inode, struct file *file) 2783 { 2784 struct nvme_ctrl *ctrl = 2785 container_of(inode->i_cdev, struct nvme_ctrl, cdev); 2786 2787 switch (ctrl->state) { 2788 case NVME_CTRL_LIVE: 2789 case NVME_CTRL_ADMIN_ONLY: 2790 break; 2791 default: 2792 return -EWOULDBLOCK; 2793 } 2794 2795 file->private_data = ctrl; 2796 return 0; 2797 } 2798 2799 static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp) 2800 { 2801 struct nvme_ns *ns; 2802 int ret; 2803 2804 down_read(&ctrl->namespaces_rwsem); 2805 if (list_empty(&ctrl->namespaces)) { 2806 ret = -ENOTTY; 2807 goto out_unlock; 2808 } 2809 2810 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list); 2811 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) { 2812 dev_warn(ctrl->device, 2813 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n"); 2814 ret = -EINVAL; 2815 goto out_unlock; 2816 } 2817 2818 dev_warn(ctrl->device, 2819 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n"); 2820 kref_get(&ns->kref); 2821 up_read(&ctrl->namespaces_rwsem); 2822 2823 ret = nvme_user_cmd(ctrl, ns, argp); 2824 nvme_put_ns(ns); 2825 return ret; 2826 2827 out_unlock: 2828 up_read(&ctrl->namespaces_rwsem); 2829 return ret; 2830 } 2831 2832 static long nvme_dev_ioctl(struct file *file, unsigned int cmd, 2833 unsigned long arg) 2834 { 2835 struct nvme_ctrl *ctrl = file->private_data; 2836 void __user *argp = (void __user *)arg; 2837 2838 switch (cmd) { 2839 case NVME_IOCTL_ADMIN_CMD: 2840 return nvme_user_cmd(ctrl, NULL, argp); 2841 case NVME_IOCTL_IO_CMD: 2842 return nvme_dev_user_cmd(ctrl, argp); 2843 case NVME_IOCTL_RESET: 2844 dev_warn(ctrl->device, "resetting controller\n"); 2845 return nvme_reset_ctrl_sync(ctrl); 2846 case NVME_IOCTL_SUBSYS_RESET: 2847 return nvme_reset_subsystem(ctrl); 2848 case NVME_IOCTL_RESCAN: 2849 nvme_queue_scan(ctrl); 2850 return 0; 2851 default: 2852 return -ENOTTY; 2853 } 2854 } 2855 2856 static const struct file_operations nvme_dev_fops = { 2857 .owner = THIS_MODULE, 2858 .open = nvme_dev_open, 2859 .unlocked_ioctl = nvme_dev_ioctl, 2860 .compat_ioctl = nvme_dev_ioctl, 2861 }; 2862 2863 static ssize_t nvme_sysfs_reset(struct device *dev, 2864 struct device_attribute *attr, const char *buf, 2865 size_t count) 2866 { 2867 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 2868 int ret; 2869 2870 ret = nvme_reset_ctrl_sync(ctrl); 2871 if (ret < 0) 2872 return ret; 2873 return count; 2874 } 2875 static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset); 2876 2877 static ssize_t nvme_sysfs_rescan(struct device *dev, 2878 struct device_attribute *attr, const char *buf, 2879 size_t count) 2880 { 2881 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 2882 2883 nvme_queue_scan(ctrl); 2884 return count; 2885 } 2886 static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan); 2887 2888 static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev) 2889 { 2890 struct gendisk *disk = dev_to_disk(dev); 2891 2892 if (disk->fops == &nvme_fops) 2893 return nvme_get_ns_from_dev(dev)->head; 2894 else 2895 return disk->private_data; 2896 } 2897 2898 static ssize_t wwid_show(struct device *dev, struct device_attribute *attr, 2899 char *buf) 2900 { 2901 struct nvme_ns_head *head = dev_to_ns_head(dev); 2902 struct nvme_ns_ids *ids = &head->ids; 2903 struct nvme_subsystem *subsys = head->subsys; 2904 int serial_len = sizeof(subsys->serial); 2905 int model_len = sizeof(subsys->model); 2906 2907 if (!uuid_is_null(&ids->uuid)) 2908 return sprintf(buf, "uuid.%pU\n", &ids->uuid); 2909 2910 if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid))) 2911 return sprintf(buf, "eui.%16phN\n", ids->nguid); 2912 2913 if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64))) 2914 return sprintf(buf, "eui.%8phN\n", ids->eui64); 2915 2916 while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' || 2917 subsys->serial[serial_len - 1] == '\0')) 2918 serial_len--; 2919 while (model_len > 0 && (subsys->model[model_len - 1] == ' ' || 2920 subsys->model[model_len - 1] == '\0')) 2921 model_len--; 2922 2923 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id, 2924 serial_len, subsys->serial, model_len, subsys->model, 2925 head->ns_id); 2926 } 2927 static DEVICE_ATTR_RO(wwid); 2928 2929 static ssize_t nguid_show(struct device *dev, struct device_attribute *attr, 2930 char *buf) 2931 { 2932 return sprintf(buf, "%pU\n", dev_to_ns_head(dev)->ids.nguid); 2933 } 2934 static DEVICE_ATTR_RO(nguid); 2935 2936 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, 2937 char *buf) 2938 { 2939 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids; 2940 2941 /* For backward compatibility expose the NGUID to userspace if 2942 * we have no UUID set 2943 */ 2944 if (uuid_is_null(&ids->uuid)) { 2945 printk_ratelimited(KERN_WARNING 2946 "No UUID available providing old NGUID\n"); 2947 return sprintf(buf, "%pU\n", ids->nguid); 2948 } 2949 return sprintf(buf, "%pU\n", &ids->uuid); 2950 } 2951 static DEVICE_ATTR_RO(uuid); 2952 2953 static ssize_t eui_show(struct device *dev, struct device_attribute *attr, 2954 char *buf) 2955 { 2956 return sprintf(buf, "%8ph\n", dev_to_ns_head(dev)->ids.eui64); 2957 } 2958 static DEVICE_ATTR_RO(eui); 2959 2960 static ssize_t nsid_show(struct device *dev, struct device_attribute *attr, 2961 char *buf) 2962 { 2963 return sprintf(buf, "%d\n", dev_to_ns_head(dev)->ns_id); 2964 } 2965 static DEVICE_ATTR_RO(nsid); 2966 2967 static struct attribute *nvme_ns_id_attrs[] = { 2968 &dev_attr_wwid.attr, 2969 &dev_attr_uuid.attr, 2970 &dev_attr_nguid.attr, 2971 &dev_attr_eui.attr, 2972 &dev_attr_nsid.attr, 2973 #ifdef CONFIG_NVME_MULTIPATH 2974 &dev_attr_ana_grpid.attr, 2975 &dev_attr_ana_state.attr, 2976 #endif 2977 NULL, 2978 }; 2979 2980 static umode_t nvme_ns_id_attrs_are_visible(struct kobject *kobj, 2981 struct attribute *a, int n) 2982 { 2983 struct device *dev = container_of(kobj, struct device, kobj); 2984 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids; 2985 2986 if (a == &dev_attr_uuid.attr) { 2987 if (uuid_is_null(&ids->uuid) && 2988 !memchr_inv(ids->nguid, 0, sizeof(ids->nguid))) 2989 return 0; 2990 } 2991 if (a == &dev_attr_nguid.attr) { 2992 if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid))) 2993 return 0; 2994 } 2995 if (a == &dev_attr_eui.attr) { 2996 if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64))) 2997 return 0; 2998 } 2999 #ifdef CONFIG_NVME_MULTIPATH 3000 if (a == &dev_attr_ana_grpid.attr || a == &dev_attr_ana_state.attr) { 3001 if (dev_to_disk(dev)->fops != &nvme_fops) /* per-path attr */ 3002 return 0; 3003 if (!nvme_ctrl_use_ana(nvme_get_ns_from_dev(dev)->ctrl)) 3004 return 0; 3005 } 3006 #endif 3007 return a->mode; 3008 } 3009 3010 static const struct attribute_group nvme_ns_id_attr_group = { 3011 .attrs = nvme_ns_id_attrs, 3012 .is_visible = nvme_ns_id_attrs_are_visible, 3013 }; 3014 3015 const struct attribute_group *nvme_ns_id_attr_groups[] = { 3016 &nvme_ns_id_attr_group, 3017 #ifdef CONFIG_NVM 3018 &nvme_nvm_attr_group, 3019 #endif 3020 NULL, 3021 }; 3022 3023 #define nvme_show_str_function(field) \ 3024 static ssize_t field##_show(struct device *dev, \ 3025 struct device_attribute *attr, char *buf) \ 3026 { \ 3027 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ 3028 return sprintf(buf, "%.*s\n", \ 3029 (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \ 3030 } \ 3031 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); 3032 3033 nvme_show_str_function(model); 3034 nvme_show_str_function(serial); 3035 nvme_show_str_function(firmware_rev); 3036 3037 #define nvme_show_int_function(field) \ 3038 static ssize_t field##_show(struct device *dev, \ 3039 struct device_attribute *attr, char *buf) \ 3040 { \ 3041 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ 3042 return sprintf(buf, "%d\n", ctrl->field); \ 3043 } \ 3044 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); 3045 3046 nvme_show_int_function(cntlid); 3047 nvme_show_int_function(numa_node); 3048 3049 static ssize_t nvme_sysfs_delete(struct device *dev, 3050 struct device_attribute *attr, const char *buf, 3051 size_t count) 3052 { 3053 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3054 3055 if (device_remove_file_self(dev, attr)) 3056 nvme_delete_ctrl_sync(ctrl); 3057 return count; 3058 } 3059 static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete); 3060 3061 static ssize_t nvme_sysfs_show_transport(struct device *dev, 3062 struct device_attribute *attr, 3063 char *buf) 3064 { 3065 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3066 3067 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name); 3068 } 3069 static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL); 3070 3071 static ssize_t nvme_sysfs_show_state(struct device *dev, 3072 struct device_attribute *attr, 3073 char *buf) 3074 { 3075 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3076 static const char *const state_name[] = { 3077 [NVME_CTRL_NEW] = "new", 3078 [NVME_CTRL_LIVE] = "live", 3079 [NVME_CTRL_ADMIN_ONLY] = "only-admin", 3080 [NVME_CTRL_RESETTING] = "resetting", 3081 [NVME_CTRL_CONNECTING] = "connecting", 3082 [NVME_CTRL_DELETING] = "deleting", 3083 [NVME_CTRL_DEAD] = "dead", 3084 }; 3085 3086 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) && 3087 state_name[ctrl->state]) 3088 return sprintf(buf, "%s\n", state_name[ctrl->state]); 3089 3090 return sprintf(buf, "unknown state\n"); 3091 } 3092 3093 static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL); 3094 3095 static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev, 3096 struct device_attribute *attr, 3097 char *buf) 3098 { 3099 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3100 3101 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subsys->subnqn); 3102 } 3103 static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL); 3104 3105 static ssize_t nvme_sysfs_show_address(struct device *dev, 3106 struct device_attribute *attr, 3107 char *buf) 3108 { 3109 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3110 3111 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE); 3112 } 3113 static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL); 3114 3115 static struct attribute *nvme_dev_attrs[] = { 3116 &dev_attr_reset_controller.attr, 3117 &dev_attr_rescan_controller.attr, 3118 &dev_attr_model.attr, 3119 &dev_attr_serial.attr, 3120 &dev_attr_firmware_rev.attr, 3121 &dev_attr_cntlid.attr, 3122 &dev_attr_delete_controller.attr, 3123 &dev_attr_transport.attr, 3124 &dev_attr_subsysnqn.attr, 3125 &dev_attr_address.attr, 3126 &dev_attr_state.attr, 3127 &dev_attr_numa_node.attr, 3128 NULL 3129 }; 3130 3131 static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj, 3132 struct attribute *a, int n) 3133 { 3134 struct device *dev = container_of(kobj, struct device, kobj); 3135 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3136 3137 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl) 3138 return 0; 3139 if (a == &dev_attr_address.attr && !ctrl->ops->get_address) 3140 return 0; 3141 3142 return a->mode; 3143 } 3144 3145 static struct attribute_group nvme_dev_attrs_group = { 3146 .attrs = nvme_dev_attrs, 3147 .is_visible = nvme_dev_attrs_are_visible, 3148 }; 3149 3150 static const struct attribute_group *nvme_dev_attr_groups[] = { 3151 &nvme_dev_attrs_group, 3152 NULL, 3153 }; 3154 3155 static struct nvme_ns_head *__nvme_find_ns_head(struct nvme_subsystem *subsys, 3156 unsigned nsid) 3157 { 3158 struct nvme_ns_head *h; 3159 3160 lockdep_assert_held(&subsys->lock); 3161 3162 list_for_each_entry(h, &subsys->nsheads, entry) { 3163 if (h->ns_id == nsid && kref_get_unless_zero(&h->ref)) 3164 return h; 3165 } 3166 3167 return NULL; 3168 } 3169 3170 static int __nvme_check_ids(struct nvme_subsystem *subsys, 3171 struct nvme_ns_head *new) 3172 { 3173 struct nvme_ns_head *h; 3174 3175 lockdep_assert_held(&subsys->lock); 3176 3177 list_for_each_entry(h, &subsys->nsheads, entry) { 3178 if (nvme_ns_ids_valid(&new->ids) && 3179 !list_empty(&h->list) && 3180 nvme_ns_ids_equal(&new->ids, &h->ids)) 3181 return -EINVAL; 3182 } 3183 3184 return 0; 3185 } 3186 3187 static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl, 3188 unsigned nsid, struct nvme_id_ns *id) 3189 { 3190 struct nvme_ns_head *head; 3191 size_t size = sizeof(*head); 3192 int ret = -ENOMEM; 3193 3194 #ifdef CONFIG_NVME_MULTIPATH 3195 size += num_possible_nodes() * sizeof(struct nvme_ns *); 3196 #endif 3197 3198 head = kzalloc(size, GFP_KERNEL); 3199 if (!head) 3200 goto out; 3201 ret = ida_simple_get(&ctrl->subsys->ns_ida, 1, 0, GFP_KERNEL); 3202 if (ret < 0) 3203 goto out_free_head; 3204 head->instance = ret; 3205 INIT_LIST_HEAD(&head->list); 3206 ret = init_srcu_struct(&head->srcu); 3207 if (ret) 3208 goto out_ida_remove; 3209 head->subsys = ctrl->subsys; 3210 head->ns_id = nsid; 3211 kref_init(&head->ref); 3212 3213 ret = nvme_report_ns_ids(ctrl, nsid, id, &head->ids); 3214 if (ret) 3215 goto out_cleanup_srcu; 3216 3217 ret = __nvme_check_ids(ctrl->subsys, head); 3218 if (ret) { 3219 dev_err(ctrl->device, 3220 "duplicate IDs for nsid %d\n", nsid); 3221 goto out_cleanup_srcu; 3222 } 3223 3224 ret = nvme_mpath_alloc_disk(ctrl, head); 3225 if (ret) 3226 goto out_cleanup_srcu; 3227 3228 list_add_tail(&head->entry, &ctrl->subsys->nsheads); 3229 3230 kref_get(&ctrl->subsys->ref); 3231 3232 return head; 3233 out_cleanup_srcu: 3234 cleanup_srcu_struct(&head->srcu); 3235 out_ida_remove: 3236 ida_simple_remove(&ctrl->subsys->ns_ida, head->instance); 3237 out_free_head: 3238 kfree(head); 3239 out: 3240 if (ret > 0) 3241 ret = blk_status_to_errno(nvme_error_status(ret)); 3242 return ERR_PTR(ret); 3243 } 3244 3245 static int nvme_init_ns_head(struct nvme_ns *ns, unsigned nsid, 3246 struct nvme_id_ns *id) 3247 { 3248 struct nvme_ctrl *ctrl = ns->ctrl; 3249 bool is_shared = id->nmic & (1 << 0); 3250 struct nvme_ns_head *head = NULL; 3251 int ret = 0; 3252 3253 mutex_lock(&ctrl->subsys->lock); 3254 if (is_shared) 3255 head = __nvme_find_ns_head(ctrl->subsys, nsid); 3256 if (!head) { 3257 head = nvme_alloc_ns_head(ctrl, nsid, id); 3258 if (IS_ERR(head)) { 3259 ret = PTR_ERR(head); 3260 goto out_unlock; 3261 } 3262 } else { 3263 struct nvme_ns_ids ids; 3264 3265 ret = nvme_report_ns_ids(ctrl, nsid, id, &ids); 3266 if (ret) 3267 goto out_unlock; 3268 3269 if (!nvme_ns_ids_equal(&head->ids, &ids)) { 3270 dev_err(ctrl->device, 3271 "IDs don't match for shared namespace %d\n", 3272 nsid); 3273 ret = -EINVAL; 3274 goto out_unlock; 3275 } 3276 } 3277 3278 list_add_tail(&ns->siblings, &head->list); 3279 ns->head = head; 3280 3281 out_unlock: 3282 mutex_unlock(&ctrl->subsys->lock); 3283 if (ret > 0) 3284 ret = blk_status_to_errno(nvme_error_status(ret)); 3285 return ret; 3286 } 3287 3288 static int ns_cmp(void *priv, struct list_head *a, struct list_head *b) 3289 { 3290 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list); 3291 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list); 3292 3293 return nsa->head->ns_id - nsb->head->ns_id; 3294 } 3295 3296 static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid) 3297 { 3298 struct nvme_ns *ns, *ret = NULL; 3299 3300 down_read(&ctrl->namespaces_rwsem); 3301 list_for_each_entry(ns, &ctrl->namespaces, list) { 3302 if (ns->head->ns_id == nsid) { 3303 if (!kref_get_unless_zero(&ns->kref)) 3304 continue; 3305 ret = ns; 3306 break; 3307 } 3308 if (ns->head->ns_id > nsid) 3309 break; 3310 } 3311 up_read(&ctrl->namespaces_rwsem); 3312 return ret; 3313 } 3314 3315 static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns) 3316 { 3317 struct streams_directive_params s; 3318 int ret; 3319 3320 if (!ctrl->nr_streams) 3321 return 0; 3322 3323 ret = nvme_get_stream_params(ctrl, &s, ns->head->ns_id); 3324 if (ret) 3325 return ret; 3326 3327 ns->sws = le32_to_cpu(s.sws); 3328 ns->sgs = le16_to_cpu(s.sgs); 3329 3330 if (ns->sws) { 3331 unsigned int bs = 1 << ns->lba_shift; 3332 3333 blk_queue_io_min(ns->queue, bs * ns->sws); 3334 if (ns->sgs) 3335 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs); 3336 } 3337 3338 return 0; 3339 } 3340 3341 static int nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid) 3342 { 3343 struct nvme_ns *ns; 3344 struct gendisk *disk; 3345 struct nvme_id_ns *id; 3346 char disk_name[DISK_NAME_LEN]; 3347 int node = ctrl->numa_node, flags = GENHD_FL_EXT_DEVT, ret; 3348 3349 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node); 3350 if (!ns) 3351 return -ENOMEM; 3352 3353 ns->queue = blk_mq_init_queue(ctrl->tagset); 3354 if (IS_ERR(ns->queue)) { 3355 ret = PTR_ERR(ns->queue); 3356 goto out_free_ns; 3357 } 3358 3359 if (ctrl->opts && ctrl->opts->data_digest) 3360 ns->queue->backing_dev_info->capabilities 3361 |= BDI_CAP_STABLE_WRITES; 3362 3363 blk_queue_flag_set(QUEUE_FLAG_NONROT, ns->queue); 3364 if (ctrl->ops->flags & NVME_F_PCI_P2PDMA) 3365 blk_queue_flag_set(QUEUE_FLAG_PCI_P2PDMA, ns->queue); 3366 3367 ns->queue->queuedata = ns; 3368 ns->ctrl = ctrl; 3369 3370 kref_init(&ns->kref); 3371 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */ 3372 3373 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift); 3374 nvme_set_queue_limits(ctrl, ns->queue); 3375 3376 ret = nvme_identify_ns(ctrl, nsid, &id); 3377 if (ret) 3378 goto out_free_queue; 3379 3380 if (id->ncap == 0) { 3381 ret = -EINVAL; 3382 goto out_free_id; 3383 } 3384 3385 ret = nvme_init_ns_head(ns, nsid, id); 3386 if (ret) 3387 goto out_free_id; 3388 nvme_setup_streams_ns(ctrl, ns); 3389 nvme_set_disk_name(disk_name, ns, ctrl, &flags); 3390 3391 disk = alloc_disk_node(0, node); 3392 if (!disk) { 3393 ret = -ENOMEM; 3394 goto out_unlink_ns; 3395 } 3396 3397 disk->fops = &nvme_fops; 3398 disk->private_data = ns; 3399 disk->queue = ns->queue; 3400 disk->flags = flags; 3401 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN); 3402 ns->disk = disk; 3403 3404 __nvme_revalidate_disk(disk, id); 3405 3406 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) { 3407 ret = nvme_nvm_register(ns, disk_name, node); 3408 if (ret) { 3409 dev_warn(ctrl->device, "LightNVM init failure\n"); 3410 goto out_put_disk; 3411 } 3412 } 3413 3414 down_write(&ctrl->namespaces_rwsem); 3415 list_add_tail(&ns->list, &ctrl->namespaces); 3416 up_write(&ctrl->namespaces_rwsem); 3417 3418 nvme_get_ctrl(ctrl); 3419 3420 device_add_disk(ctrl->device, ns->disk, nvme_ns_id_attr_groups); 3421 3422 nvme_mpath_add_disk(ns, id); 3423 nvme_fault_inject_init(&ns->fault_inject, ns->disk->disk_name); 3424 kfree(id); 3425 3426 return 0; 3427 out_put_disk: 3428 put_disk(ns->disk); 3429 out_unlink_ns: 3430 mutex_lock(&ctrl->subsys->lock); 3431 list_del_rcu(&ns->siblings); 3432 mutex_unlock(&ctrl->subsys->lock); 3433 nvme_put_ns_head(ns->head); 3434 out_free_id: 3435 kfree(id); 3436 out_free_queue: 3437 blk_cleanup_queue(ns->queue); 3438 out_free_ns: 3439 kfree(ns); 3440 if (ret > 0) 3441 ret = blk_status_to_errno(nvme_error_status(ret)); 3442 return ret; 3443 } 3444 3445 static void nvme_ns_remove(struct nvme_ns *ns) 3446 { 3447 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags)) 3448 return; 3449 3450 nvme_fault_inject_fini(&ns->fault_inject); 3451 3452 mutex_lock(&ns->ctrl->subsys->lock); 3453 list_del_rcu(&ns->siblings); 3454 mutex_unlock(&ns->ctrl->subsys->lock); 3455 synchronize_rcu(); /* guarantee not available in head->list */ 3456 nvme_mpath_clear_current_path(ns); 3457 synchronize_srcu(&ns->head->srcu); /* wait for concurrent submissions */ 3458 3459 if (ns->disk && ns->disk->flags & GENHD_FL_UP) { 3460 del_gendisk(ns->disk); 3461 blk_cleanup_queue(ns->queue); 3462 if (blk_get_integrity(ns->disk)) 3463 blk_integrity_unregister(ns->disk); 3464 } 3465 3466 down_write(&ns->ctrl->namespaces_rwsem); 3467 list_del_init(&ns->list); 3468 up_write(&ns->ctrl->namespaces_rwsem); 3469 3470 nvme_mpath_check_last_path(ns); 3471 nvme_put_ns(ns); 3472 } 3473 3474 static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid) 3475 { 3476 struct nvme_ns *ns; 3477 3478 ns = nvme_find_get_ns(ctrl, nsid); 3479 if (ns) { 3480 if (ns->disk && revalidate_disk(ns->disk)) 3481 nvme_ns_remove(ns); 3482 nvme_put_ns(ns); 3483 } else 3484 nvme_alloc_ns(ctrl, nsid); 3485 } 3486 3487 static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl, 3488 unsigned nsid) 3489 { 3490 struct nvme_ns *ns, *next; 3491 LIST_HEAD(rm_list); 3492 3493 down_write(&ctrl->namespaces_rwsem); 3494 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) { 3495 if (ns->head->ns_id > nsid || test_bit(NVME_NS_DEAD, &ns->flags)) 3496 list_move_tail(&ns->list, &rm_list); 3497 } 3498 up_write(&ctrl->namespaces_rwsem); 3499 3500 list_for_each_entry_safe(ns, next, &rm_list, list) 3501 nvme_ns_remove(ns); 3502 3503 } 3504 3505 static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn) 3506 { 3507 struct nvme_ns *ns; 3508 __le32 *ns_list; 3509 unsigned i, j, nsid, prev = 0; 3510 unsigned num_lists = DIV_ROUND_UP_ULL((u64)nn, 1024); 3511 int ret = 0; 3512 3513 ns_list = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL); 3514 if (!ns_list) 3515 return -ENOMEM; 3516 3517 for (i = 0; i < num_lists; i++) { 3518 ret = nvme_identify_ns_list(ctrl, prev, ns_list); 3519 if (ret) 3520 goto free; 3521 3522 for (j = 0; j < min(nn, 1024U); j++) { 3523 nsid = le32_to_cpu(ns_list[j]); 3524 if (!nsid) 3525 goto out; 3526 3527 nvme_validate_ns(ctrl, nsid); 3528 3529 while (++prev < nsid) { 3530 ns = nvme_find_get_ns(ctrl, prev); 3531 if (ns) { 3532 nvme_ns_remove(ns); 3533 nvme_put_ns(ns); 3534 } 3535 } 3536 } 3537 nn -= j; 3538 } 3539 out: 3540 nvme_remove_invalid_namespaces(ctrl, prev); 3541 free: 3542 kfree(ns_list); 3543 return ret; 3544 } 3545 3546 static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn) 3547 { 3548 unsigned i; 3549 3550 for (i = 1; i <= nn; i++) 3551 nvme_validate_ns(ctrl, i); 3552 3553 nvme_remove_invalid_namespaces(ctrl, nn); 3554 } 3555 3556 static void nvme_clear_changed_ns_log(struct nvme_ctrl *ctrl) 3557 { 3558 size_t log_size = NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32); 3559 __le32 *log; 3560 int error; 3561 3562 log = kzalloc(log_size, GFP_KERNEL); 3563 if (!log) 3564 return; 3565 3566 /* 3567 * We need to read the log to clear the AEN, but we don't want to rely 3568 * on it for the changed namespace information as userspace could have 3569 * raced with us in reading the log page, which could cause us to miss 3570 * updates. 3571 */ 3572 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_CHANGED_NS, 0, log, 3573 log_size, 0); 3574 if (error) 3575 dev_warn(ctrl->device, 3576 "reading changed ns log failed: %d\n", error); 3577 3578 kfree(log); 3579 } 3580 3581 static void nvme_scan_work(struct work_struct *work) 3582 { 3583 struct nvme_ctrl *ctrl = 3584 container_of(work, struct nvme_ctrl, scan_work); 3585 struct nvme_id_ctrl *id; 3586 unsigned nn; 3587 3588 if (ctrl->state != NVME_CTRL_LIVE) 3589 return; 3590 3591 WARN_ON_ONCE(!ctrl->tagset); 3592 3593 if (test_and_clear_bit(NVME_AER_NOTICE_NS_CHANGED, &ctrl->events)) { 3594 dev_info(ctrl->device, "rescanning namespaces.\n"); 3595 nvme_clear_changed_ns_log(ctrl); 3596 } 3597 3598 if (nvme_identify_ctrl(ctrl, &id)) 3599 return; 3600 3601 mutex_lock(&ctrl->scan_lock); 3602 nn = le32_to_cpu(id->nn); 3603 if (ctrl->vs >= NVME_VS(1, 1, 0) && 3604 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) { 3605 if (!nvme_scan_ns_list(ctrl, nn)) 3606 goto out_free_id; 3607 } 3608 nvme_scan_ns_sequential(ctrl, nn); 3609 out_free_id: 3610 mutex_unlock(&ctrl->scan_lock); 3611 kfree(id); 3612 down_write(&ctrl->namespaces_rwsem); 3613 list_sort(NULL, &ctrl->namespaces, ns_cmp); 3614 up_write(&ctrl->namespaces_rwsem); 3615 } 3616 3617 /* 3618 * This function iterates the namespace list unlocked to allow recovery from 3619 * controller failure. It is up to the caller to ensure the namespace list is 3620 * not modified by scan work while this function is executing. 3621 */ 3622 void nvme_remove_namespaces(struct nvme_ctrl *ctrl) 3623 { 3624 struct nvme_ns *ns, *next; 3625 LIST_HEAD(ns_list); 3626 3627 /* 3628 * make sure to requeue I/O to all namespaces as these 3629 * might result from the scan itself and must complete 3630 * for the scan_work to make progress 3631 */ 3632 nvme_mpath_clear_ctrl_paths(ctrl); 3633 3634 /* prevent racing with ns scanning */ 3635 flush_work(&ctrl->scan_work); 3636 3637 /* 3638 * The dead states indicates the controller was not gracefully 3639 * disconnected. In that case, we won't be able to flush any data while 3640 * removing the namespaces' disks; fail all the queues now to avoid 3641 * potentially having to clean up the failed sync later. 3642 */ 3643 if (ctrl->state == NVME_CTRL_DEAD) 3644 nvme_kill_queues(ctrl); 3645 3646 down_write(&ctrl->namespaces_rwsem); 3647 list_splice_init(&ctrl->namespaces, &ns_list); 3648 up_write(&ctrl->namespaces_rwsem); 3649 3650 list_for_each_entry_safe(ns, next, &ns_list, list) 3651 nvme_ns_remove(ns); 3652 } 3653 EXPORT_SYMBOL_GPL(nvme_remove_namespaces); 3654 3655 static int nvme_class_uevent(struct device *dev, struct kobj_uevent_env *env) 3656 { 3657 struct nvme_ctrl *ctrl = 3658 container_of(dev, struct nvme_ctrl, ctrl_device); 3659 struct nvmf_ctrl_options *opts = ctrl->opts; 3660 int ret; 3661 3662 ret = add_uevent_var(env, "NVME_TRTYPE=%s", ctrl->ops->name); 3663 if (ret) 3664 return ret; 3665 3666 if (opts) { 3667 ret = add_uevent_var(env, "NVME_TRADDR=%s", opts->traddr); 3668 if (ret) 3669 return ret; 3670 3671 ret = add_uevent_var(env, "NVME_TRSVCID=%s", 3672 opts->trsvcid ?: "none"); 3673 if (ret) 3674 return ret; 3675 3676 ret = add_uevent_var(env, "NVME_HOST_TRADDR=%s", 3677 opts->host_traddr ?: "none"); 3678 } 3679 return ret; 3680 } 3681 3682 static void nvme_aen_uevent(struct nvme_ctrl *ctrl) 3683 { 3684 char *envp[2] = { NULL, NULL }; 3685 u32 aen_result = ctrl->aen_result; 3686 3687 ctrl->aen_result = 0; 3688 if (!aen_result) 3689 return; 3690 3691 envp[0] = kasprintf(GFP_KERNEL, "NVME_AEN=%#08x", aen_result); 3692 if (!envp[0]) 3693 return; 3694 kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp); 3695 kfree(envp[0]); 3696 } 3697 3698 static void nvme_async_event_work(struct work_struct *work) 3699 { 3700 struct nvme_ctrl *ctrl = 3701 container_of(work, struct nvme_ctrl, async_event_work); 3702 3703 nvme_aen_uevent(ctrl); 3704 ctrl->ops->submit_async_event(ctrl); 3705 } 3706 3707 static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl) 3708 { 3709 3710 u32 csts; 3711 3712 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) 3713 return false; 3714 3715 if (csts == ~0) 3716 return false; 3717 3718 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP)); 3719 } 3720 3721 static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl) 3722 { 3723 struct nvme_fw_slot_info_log *log; 3724 3725 log = kmalloc(sizeof(*log), GFP_KERNEL); 3726 if (!log) 3727 return; 3728 3729 if (nvme_get_log(ctrl, NVME_NSID_ALL, 0, NVME_LOG_FW_SLOT, log, 3730 sizeof(*log), 0)) 3731 dev_warn(ctrl->device, "Get FW SLOT INFO log error\n"); 3732 kfree(log); 3733 } 3734 3735 static void nvme_fw_act_work(struct work_struct *work) 3736 { 3737 struct nvme_ctrl *ctrl = container_of(work, 3738 struct nvme_ctrl, fw_act_work); 3739 unsigned long fw_act_timeout; 3740 3741 if (ctrl->mtfa) 3742 fw_act_timeout = jiffies + 3743 msecs_to_jiffies(ctrl->mtfa * 100); 3744 else 3745 fw_act_timeout = jiffies + 3746 msecs_to_jiffies(admin_timeout * 1000); 3747 3748 nvme_stop_queues(ctrl); 3749 while (nvme_ctrl_pp_status(ctrl)) { 3750 if (time_after(jiffies, fw_act_timeout)) { 3751 dev_warn(ctrl->device, 3752 "Fw activation timeout, reset controller\n"); 3753 nvme_reset_ctrl(ctrl); 3754 break; 3755 } 3756 msleep(100); 3757 } 3758 3759 if (ctrl->state != NVME_CTRL_LIVE) 3760 return; 3761 3762 nvme_start_queues(ctrl); 3763 /* read FW slot information to clear the AER */ 3764 nvme_get_fw_slot_info(ctrl); 3765 } 3766 3767 static void nvme_handle_aen_notice(struct nvme_ctrl *ctrl, u32 result) 3768 { 3769 u32 aer_notice_type = (result & 0xff00) >> 8; 3770 3771 trace_nvme_async_event(ctrl, aer_notice_type); 3772 3773 switch (aer_notice_type) { 3774 case NVME_AER_NOTICE_NS_CHANGED: 3775 set_bit(NVME_AER_NOTICE_NS_CHANGED, &ctrl->events); 3776 nvme_queue_scan(ctrl); 3777 break; 3778 case NVME_AER_NOTICE_FW_ACT_STARTING: 3779 queue_work(nvme_wq, &ctrl->fw_act_work); 3780 break; 3781 #ifdef CONFIG_NVME_MULTIPATH 3782 case NVME_AER_NOTICE_ANA: 3783 if (!ctrl->ana_log_buf) 3784 break; 3785 queue_work(nvme_wq, &ctrl->ana_work); 3786 break; 3787 #endif 3788 case NVME_AER_NOTICE_DISC_CHANGED: 3789 ctrl->aen_result = result; 3790 break; 3791 default: 3792 dev_warn(ctrl->device, "async event result %08x\n", result); 3793 } 3794 } 3795 3796 void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status, 3797 volatile union nvme_result *res) 3798 { 3799 u32 result = le32_to_cpu(res->u32); 3800 u32 aer_type = result & 0x07; 3801 3802 if (le16_to_cpu(status) >> 1 != NVME_SC_SUCCESS) 3803 return; 3804 3805 switch (aer_type) { 3806 case NVME_AER_NOTICE: 3807 nvme_handle_aen_notice(ctrl, result); 3808 break; 3809 case NVME_AER_ERROR: 3810 case NVME_AER_SMART: 3811 case NVME_AER_CSS: 3812 case NVME_AER_VS: 3813 trace_nvme_async_event(ctrl, aer_type); 3814 ctrl->aen_result = result; 3815 break; 3816 default: 3817 break; 3818 } 3819 queue_work(nvme_wq, &ctrl->async_event_work); 3820 } 3821 EXPORT_SYMBOL_GPL(nvme_complete_async_event); 3822 3823 void nvme_stop_ctrl(struct nvme_ctrl *ctrl) 3824 { 3825 nvme_mpath_stop(ctrl); 3826 nvme_stop_keep_alive(ctrl); 3827 flush_work(&ctrl->async_event_work); 3828 cancel_work_sync(&ctrl->fw_act_work); 3829 } 3830 EXPORT_SYMBOL_GPL(nvme_stop_ctrl); 3831 3832 void nvme_start_ctrl(struct nvme_ctrl *ctrl) 3833 { 3834 if (ctrl->kato) 3835 nvme_start_keep_alive(ctrl); 3836 3837 nvme_enable_aen(ctrl); 3838 3839 if (ctrl->queue_count > 1) { 3840 nvme_queue_scan(ctrl); 3841 nvme_start_queues(ctrl); 3842 } 3843 } 3844 EXPORT_SYMBOL_GPL(nvme_start_ctrl); 3845 3846 void nvme_uninit_ctrl(struct nvme_ctrl *ctrl) 3847 { 3848 nvme_fault_inject_fini(&ctrl->fault_inject); 3849 dev_pm_qos_hide_latency_tolerance(ctrl->device); 3850 cdev_device_del(&ctrl->cdev, ctrl->device); 3851 } 3852 EXPORT_SYMBOL_GPL(nvme_uninit_ctrl); 3853 3854 static void nvme_free_ctrl(struct device *dev) 3855 { 3856 struct nvme_ctrl *ctrl = 3857 container_of(dev, struct nvme_ctrl, ctrl_device); 3858 struct nvme_subsystem *subsys = ctrl->subsys; 3859 3860 if (subsys && ctrl->instance != subsys->instance) 3861 ida_simple_remove(&nvme_instance_ida, ctrl->instance); 3862 3863 kfree(ctrl->effects); 3864 nvme_mpath_uninit(ctrl); 3865 __free_page(ctrl->discard_page); 3866 3867 if (subsys) { 3868 mutex_lock(&nvme_subsystems_lock); 3869 list_del(&ctrl->subsys_entry); 3870 sysfs_remove_link(&subsys->dev.kobj, dev_name(ctrl->device)); 3871 mutex_unlock(&nvme_subsystems_lock); 3872 } 3873 3874 ctrl->ops->free_ctrl(ctrl); 3875 3876 if (subsys) 3877 nvme_put_subsystem(subsys); 3878 } 3879 3880 /* 3881 * Initialize a NVMe controller structures. This needs to be called during 3882 * earliest initialization so that we have the initialized structured around 3883 * during probing. 3884 */ 3885 int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev, 3886 const struct nvme_ctrl_ops *ops, unsigned long quirks) 3887 { 3888 int ret; 3889 3890 ctrl->state = NVME_CTRL_NEW; 3891 spin_lock_init(&ctrl->lock); 3892 mutex_init(&ctrl->scan_lock); 3893 INIT_LIST_HEAD(&ctrl->namespaces); 3894 init_rwsem(&ctrl->namespaces_rwsem); 3895 ctrl->dev = dev; 3896 ctrl->ops = ops; 3897 ctrl->quirks = quirks; 3898 INIT_WORK(&ctrl->scan_work, nvme_scan_work); 3899 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work); 3900 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work); 3901 INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work); 3902 3903 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work); 3904 memset(&ctrl->ka_cmd, 0, sizeof(ctrl->ka_cmd)); 3905 ctrl->ka_cmd.common.opcode = nvme_admin_keep_alive; 3906 3907 BUILD_BUG_ON(NVME_DSM_MAX_RANGES * sizeof(struct nvme_dsm_range) > 3908 PAGE_SIZE); 3909 ctrl->discard_page = alloc_page(GFP_KERNEL); 3910 if (!ctrl->discard_page) { 3911 ret = -ENOMEM; 3912 goto out; 3913 } 3914 3915 ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL); 3916 if (ret < 0) 3917 goto out; 3918 ctrl->instance = ret; 3919 3920 device_initialize(&ctrl->ctrl_device); 3921 ctrl->device = &ctrl->ctrl_device; 3922 ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance); 3923 ctrl->device->class = nvme_class; 3924 ctrl->device->parent = ctrl->dev; 3925 ctrl->device->groups = nvme_dev_attr_groups; 3926 ctrl->device->release = nvme_free_ctrl; 3927 dev_set_drvdata(ctrl->device, ctrl); 3928 ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance); 3929 if (ret) 3930 goto out_release_instance; 3931 3932 cdev_init(&ctrl->cdev, &nvme_dev_fops); 3933 ctrl->cdev.owner = ops->module; 3934 ret = cdev_device_add(&ctrl->cdev, ctrl->device); 3935 if (ret) 3936 goto out_free_name; 3937 3938 /* 3939 * Initialize latency tolerance controls. The sysfs files won't 3940 * be visible to userspace unless the device actually supports APST. 3941 */ 3942 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance; 3943 dev_pm_qos_update_user_latency_tolerance(ctrl->device, 3944 min(default_ps_max_latency_us, (unsigned long)S32_MAX)); 3945 3946 nvme_fault_inject_init(&ctrl->fault_inject, dev_name(ctrl->device)); 3947 3948 return 0; 3949 out_free_name: 3950 kfree_const(ctrl->device->kobj.name); 3951 out_release_instance: 3952 ida_simple_remove(&nvme_instance_ida, ctrl->instance); 3953 out: 3954 if (ctrl->discard_page) 3955 __free_page(ctrl->discard_page); 3956 return ret; 3957 } 3958 EXPORT_SYMBOL_GPL(nvme_init_ctrl); 3959 3960 /** 3961 * nvme_kill_queues(): Ends all namespace queues 3962 * @ctrl: the dead controller that needs to end 3963 * 3964 * Call this function when the driver determines it is unable to get the 3965 * controller in a state capable of servicing IO. 3966 */ 3967 void nvme_kill_queues(struct nvme_ctrl *ctrl) 3968 { 3969 struct nvme_ns *ns; 3970 3971 down_read(&ctrl->namespaces_rwsem); 3972 3973 /* Forcibly unquiesce queues to avoid blocking dispatch */ 3974 if (ctrl->admin_q && !blk_queue_dying(ctrl->admin_q)) 3975 blk_mq_unquiesce_queue(ctrl->admin_q); 3976 3977 list_for_each_entry(ns, &ctrl->namespaces, list) 3978 nvme_set_queue_dying(ns); 3979 3980 up_read(&ctrl->namespaces_rwsem); 3981 } 3982 EXPORT_SYMBOL_GPL(nvme_kill_queues); 3983 3984 void nvme_unfreeze(struct nvme_ctrl *ctrl) 3985 { 3986 struct nvme_ns *ns; 3987 3988 down_read(&ctrl->namespaces_rwsem); 3989 list_for_each_entry(ns, &ctrl->namespaces, list) 3990 blk_mq_unfreeze_queue(ns->queue); 3991 up_read(&ctrl->namespaces_rwsem); 3992 } 3993 EXPORT_SYMBOL_GPL(nvme_unfreeze); 3994 3995 void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout) 3996 { 3997 struct nvme_ns *ns; 3998 3999 down_read(&ctrl->namespaces_rwsem); 4000 list_for_each_entry(ns, &ctrl->namespaces, list) { 4001 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout); 4002 if (timeout <= 0) 4003 break; 4004 } 4005 up_read(&ctrl->namespaces_rwsem); 4006 } 4007 EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout); 4008 4009 void nvme_wait_freeze(struct nvme_ctrl *ctrl) 4010 { 4011 struct nvme_ns *ns; 4012 4013 down_read(&ctrl->namespaces_rwsem); 4014 list_for_each_entry(ns, &ctrl->namespaces, list) 4015 blk_mq_freeze_queue_wait(ns->queue); 4016 up_read(&ctrl->namespaces_rwsem); 4017 } 4018 EXPORT_SYMBOL_GPL(nvme_wait_freeze); 4019 4020 void nvme_start_freeze(struct nvme_ctrl *ctrl) 4021 { 4022 struct nvme_ns *ns; 4023 4024 down_read(&ctrl->namespaces_rwsem); 4025 list_for_each_entry(ns, &ctrl->namespaces, list) 4026 blk_freeze_queue_start(ns->queue); 4027 up_read(&ctrl->namespaces_rwsem); 4028 } 4029 EXPORT_SYMBOL_GPL(nvme_start_freeze); 4030 4031 void nvme_stop_queues(struct nvme_ctrl *ctrl) 4032 { 4033 struct nvme_ns *ns; 4034 4035 down_read(&ctrl->namespaces_rwsem); 4036 list_for_each_entry(ns, &ctrl->namespaces, list) 4037 blk_mq_quiesce_queue(ns->queue); 4038 up_read(&ctrl->namespaces_rwsem); 4039 } 4040 EXPORT_SYMBOL_GPL(nvme_stop_queues); 4041 4042 void nvme_start_queues(struct nvme_ctrl *ctrl) 4043 { 4044 struct nvme_ns *ns; 4045 4046 down_read(&ctrl->namespaces_rwsem); 4047 list_for_each_entry(ns, &ctrl->namespaces, list) 4048 blk_mq_unquiesce_queue(ns->queue); 4049 up_read(&ctrl->namespaces_rwsem); 4050 } 4051 EXPORT_SYMBOL_GPL(nvme_start_queues); 4052 4053 4054 void nvme_sync_queues(struct nvme_ctrl *ctrl) 4055 { 4056 struct nvme_ns *ns; 4057 4058 down_read(&ctrl->namespaces_rwsem); 4059 list_for_each_entry(ns, &ctrl->namespaces, list) 4060 blk_sync_queue(ns->queue); 4061 up_read(&ctrl->namespaces_rwsem); 4062 4063 if (ctrl->admin_q) 4064 blk_sync_queue(ctrl->admin_q); 4065 } 4066 EXPORT_SYMBOL_GPL(nvme_sync_queues); 4067 4068 /* 4069 * Check we didn't inadvertently grow the command structure sizes: 4070 */ 4071 static inline void _nvme_check_size(void) 4072 { 4073 BUILD_BUG_ON(sizeof(struct nvme_common_command) != 64); 4074 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64); 4075 BUILD_BUG_ON(sizeof(struct nvme_identify) != 64); 4076 BUILD_BUG_ON(sizeof(struct nvme_features) != 64); 4077 BUILD_BUG_ON(sizeof(struct nvme_download_firmware) != 64); 4078 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64); 4079 BUILD_BUG_ON(sizeof(struct nvme_dsm_cmd) != 64); 4080 BUILD_BUG_ON(sizeof(struct nvme_write_zeroes_cmd) != 64); 4081 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64); 4082 BUILD_BUG_ON(sizeof(struct nvme_get_log_page_command) != 64); 4083 BUILD_BUG_ON(sizeof(struct nvme_command) != 64); 4084 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != NVME_IDENTIFY_DATA_SIZE); 4085 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != NVME_IDENTIFY_DATA_SIZE); 4086 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64); 4087 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512); 4088 BUILD_BUG_ON(sizeof(struct nvme_dbbuf) != 64); 4089 BUILD_BUG_ON(sizeof(struct nvme_directive_cmd) != 64); 4090 } 4091 4092 4093 static int __init nvme_core_init(void) 4094 { 4095 int result = -ENOMEM; 4096 4097 _nvme_check_size(); 4098 4099 nvme_wq = alloc_workqueue("nvme-wq", 4100 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0); 4101 if (!nvme_wq) 4102 goto out; 4103 4104 nvme_reset_wq = alloc_workqueue("nvme-reset-wq", 4105 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0); 4106 if (!nvme_reset_wq) 4107 goto destroy_wq; 4108 4109 nvme_delete_wq = alloc_workqueue("nvme-delete-wq", 4110 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0); 4111 if (!nvme_delete_wq) 4112 goto destroy_reset_wq; 4113 4114 result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme"); 4115 if (result < 0) 4116 goto destroy_delete_wq; 4117 4118 nvme_class = class_create(THIS_MODULE, "nvme"); 4119 if (IS_ERR(nvme_class)) { 4120 result = PTR_ERR(nvme_class); 4121 goto unregister_chrdev; 4122 } 4123 nvme_class->dev_uevent = nvme_class_uevent; 4124 4125 nvme_subsys_class = class_create(THIS_MODULE, "nvme-subsystem"); 4126 if (IS_ERR(nvme_subsys_class)) { 4127 result = PTR_ERR(nvme_subsys_class); 4128 goto destroy_class; 4129 } 4130 return 0; 4131 4132 destroy_class: 4133 class_destroy(nvme_class); 4134 unregister_chrdev: 4135 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS); 4136 destroy_delete_wq: 4137 destroy_workqueue(nvme_delete_wq); 4138 destroy_reset_wq: 4139 destroy_workqueue(nvme_reset_wq); 4140 destroy_wq: 4141 destroy_workqueue(nvme_wq); 4142 out: 4143 return result; 4144 } 4145 4146 static void __exit nvme_core_exit(void) 4147 { 4148 class_destroy(nvme_subsys_class); 4149 class_destroy(nvme_class); 4150 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS); 4151 destroy_workqueue(nvme_delete_wq); 4152 destroy_workqueue(nvme_reset_wq); 4153 destroy_workqueue(nvme_wq); 4154 } 4155 4156 MODULE_LICENSE("GPL"); 4157 MODULE_VERSION("1.0"); 4158 module_init(nvme_core_init); 4159 module_exit(nvme_core_exit); 4160