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/blk-integrity.h> 10 #include <linux/compat.h> 11 #include <linux/delay.h> 12 #include <linux/errno.h> 13 #include <linux/hdreg.h> 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/backing-dev.h> 17 #include <linux/slab.h> 18 #include <linux/types.h> 19 #include <linux/pr.h> 20 #include <linux/ptrace.h> 21 #include <linux/nvme_ioctl.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 unsigned long apst_primary_timeout_ms = 100; 61 module_param(apst_primary_timeout_ms, ulong, 0644); 62 MODULE_PARM_DESC(apst_primary_timeout_ms, 63 "primary APST timeout in ms"); 64 65 static unsigned long apst_secondary_timeout_ms = 2000; 66 module_param(apst_secondary_timeout_ms, ulong, 0644); 67 MODULE_PARM_DESC(apst_secondary_timeout_ms, 68 "secondary APST timeout in ms"); 69 70 static unsigned long apst_primary_latency_tol_us = 15000; 71 module_param(apst_primary_latency_tol_us, ulong, 0644); 72 MODULE_PARM_DESC(apst_primary_latency_tol_us, 73 "primary APST latency tolerance in us"); 74 75 static unsigned long apst_secondary_latency_tol_us = 100000; 76 module_param(apst_secondary_latency_tol_us, ulong, 0644); 77 MODULE_PARM_DESC(apst_secondary_latency_tol_us, 78 "secondary APST latency tolerance in us"); 79 80 /* 81 * nvme_wq - hosts nvme related works that are not reset or delete 82 * nvme_reset_wq - hosts nvme reset works 83 * nvme_delete_wq - hosts nvme delete works 84 * 85 * nvme_wq will host works such as scan, aen handling, fw activation, 86 * keep-alive, periodic reconnects etc. nvme_reset_wq 87 * runs reset works which also flush works hosted on nvme_wq for 88 * serialization purposes. nvme_delete_wq host controller deletion 89 * works which flush reset works for serialization. 90 */ 91 struct workqueue_struct *nvme_wq; 92 EXPORT_SYMBOL_GPL(nvme_wq); 93 94 struct workqueue_struct *nvme_reset_wq; 95 EXPORT_SYMBOL_GPL(nvme_reset_wq); 96 97 struct workqueue_struct *nvme_delete_wq; 98 EXPORT_SYMBOL_GPL(nvme_delete_wq); 99 100 static LIST_HEAD(nvme_subsystems); 101 static DEFINE_MUTEX(nvme_subsystems_lock); 102 103 static DEFINE_IDA(nvme_instance_ida); 104 static dev_t nvme_ctrl_base_chr_devt; 105 static struct class *nvme_class; 106 static struct class *nvme_subsys_class; 107 108 static DEFINE_IDA(nvme_ns_chr_minor_ida); 109 static dev_t nvme_ns_chr_devt; 110 static struct class *nvme_ns_chr_class; 111 112 static void nvme_put_subsystem(struct nvme_subsystem *subsys); 113 static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl, 114 unsigned nsid); 115 static void nvme_update_keep_alive(struct nvme_ctrl *ctrl, 116 struct nvme_command *cmd); 117 118 void nvme_queue_scan(struct nvme_ctrl *ctrl) 119 { 120 /* 121 * Only new queue scan work when admin and IO queues are both alive 122 */ 123 if (ctrl->state == NVME_CTRL_LIVE && ctrl->tagset) 124 queue_work(nvme_wq, &ctrl->scan_work); 125 } 126 127 /* 128 * Use this function to proceed with scheduling reset_work for a controller 129 * that had previously been set to the resetting state. This is intended for 130 * code paths that can't be interrupted by other reset attempts. A hot removal 131 * may prevent this from succeeding. 132 */ 133 int nvme_try_sched_reset(struct nvme_ctrl *ctrl) 134 { 135 if (ctrl->state != NVME_CTRL_RESETTING) 136 return -EBUSY; 137 if (!queue_work(nvme_reset_wq, &ctrl->reset_work)) 138 return -EBUSY; 139 return 0; 140 } 141 EXPORT_SYMBOL_GPL(nvme_try_sched_reset); 142 143 static void nvme_failfast_work(struct work_struct *work) 144 { 145 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work), 146 struct nvme_ctrl, failfast_work); 147 148 if (ctrl->state != NVME_CTRL_CONNECTING) 149 return; 150 151 set_bit(NVME_CTRL_FAILFAST_EXPIRED, &ctrl->flags); 152 dev_info(ctrl->device, "failfast expired\n"); 153 nvme_kick_requeue_lists(ctrl); 154 } 155 156 static inline void nvme_start_failfast_work(struct nvme_ctrl *ctrl) 157 { 158 if (!ctrl->opts || ctrl->opts->fast_io_fail_tmo == -1) 159 return; 160 161 schedule_delayed_work(&ctrl->failfast_work, 162 ctrl->opts->fast_io_fail_tmo * HZ); 163 } 164 165 static inline void nvme_stop_failfast_work(struct nvme_ctrl *ctrl) 166 { 167 if (!ctrl->opts) 168 return; 169 170 cancel_delayed_work_sync(&ctrl->failfast_work); 171 clear_bit(NVME_CTRL_FAILFAST_EXPIRED, &ctrl->flags); 172 } 173 174 175 int nvme_reset_ctrl(struct nvme_ctrl *ctrl) 176 { 177 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING)) 178 return -EBUSY; 179 if (!queue_work(nvme_reset_wq, &ctrl->reset_work)) 180 return -EBUSY; 181 return 0; 182 } 183 EXPORT_SYMBOL_GPL(nvme_reset_ctrl); 184 185 int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl) 186 { 187 int ret; 188 189 ret = nvme_reset_ctrl(ctrl); 190 if (!ret) { 191 flush_work(&ctrl->reset_work); 192 if (ctrl->state != NVME_CTRL_LIVE) 193 ret = -ENETRESET; 194 } 195 196 return ret; 197 } 198 199 static void nvme_do_delete_ctrl(struct nvme_ctrl *ctrl) 200 { 201 dev_info(ctrl->device, 202 "Removing ctrl: NQN \"%s\"\n", nvmf_ctrl_subsysnqn(ctrl)); 203 204 flush_work(&ctrl->reset_work); 205 nvme_stop_ctrl(ctrl); 206 nvme_remove_namespaces(ctrl); 207 ctrl->ops->delete_ctrl(ctrl); 208 nvme_uninit_ctrl(ctrl); 209 } 210 211 static void nvme_delete_ctrl_work(struct work_struct *work) 212 { 213 struct nvme_ctrl *ctrl = 214 container_of(work, struct nvme_ctrl, delete_work); 215 216 nvme_do_delete_ctrl(ctrl); 217 } 218 219 int nvme_delete_ctrl(struct nvme_ctrl *ctrl) 220 { 221 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING)) 222 return -EBUSY; 223 if (!queue_work(nvme_delete_wq, &ctrl->delete_work)) 224 return -EBUSY; 225 return 0; 226 } 227 EXPORT_SYMBOL_GPL(nvme_delete_ctrl); 228 229 static void nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl) 230 { 231 /* 232 * Keep a reference until nvme_do_delete_ctrl() complete, 233 * since ->delete_ctrl can free the controller. 234 */ 235 nvme_get_ctrl(ctrl); 236 if (nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING)) 237 nvme_do_delete_ctrl(ctrl); 238 nvme_put_ctrl(ctrl); 239 } 240 241 static blk_status_t nvme_error_status(u16 status) 242 { 243 switch (status & 0x7ff) { 244 case NVME_SC_SUCCESS: 245 return BLK_STS_OK; 246 case NVME_SC_CAP_EXCEEDED: 247 return BLK_STS_NOSPC; 248 case NVME_SC_LBA_RANGE: 249 case NVME_SC_CMD_INTERRUPTED: 250 case NVME_SC_NS_NOT_READY: 251 return BLK_STS_TARGET; 252 case NVME_SC_BAD_ATTRIBUTES: 253 case NVME_SC_ONCS_NOT_SUPPORTED: 254 case NVME_SC_INVALID_OPCODE: 255 case NVME_SC_INVALID_FIELD: 256 case NVME_SC_INVALID_NS: 257 return BLK_STS_NOTSUPP; 258 case NVME_SC_WRITE_FAULT: 259 case NVME_SC_READ_ERROR: 260 case NVME_SC_UNWRITTEN_BLOCK: 261 case NVME_SC_ACCESS_DENIED: 262 case NVME_SC_READ_ONLY: 263 case NVME_SC_COMPARE_FAILED: 264 return BLK_STS_MEDIUM; 265 case NVME_SC_GUARD_CHECK: 266 case NVME_SC_APPTAG_CHECK: 267 case NVME_SC_REFTAG_CHECK: 268 case NVME_SC_INVALID_PI: 269 return BLK_STS_PROTECTION; 270 case NVME_SC_RESERVATION_CONFLICT: 271 return BLK_STS_NEXUS; 272 case NVME_SC_HOST_PATH_ERROR: 273 return BLK_STS_TRANSPORT; 274 case NVME_SC_ZONE_TOO_MANY_ACTIVE: 275 return BLK_STS_ZONE_ACTIVE_RESOURCE; 276 case NVME_SC_ZONE_TOO_MANY_OPEN: 277 return BLK_STS_ZONE_OPEN_RESOURCE; 278 default: 279 return BLK_STS_IOERR; 280 } 281 } 282 283 static void nvme_retry_req(struct request *req) 284 { 285 unsigned long delay = 0; 286 u16 crd; 287 288 /* The mask and shift result must be <= 3 */ 289 crd = (nvme_req(req)->status & NVME_SC_CRD) >> 11; 290 if (crd) 291 delay = nvme_req(req)->ctrl->crdt[crd - 1] * 100; 292 293 nvme_req(req)->retries++; 294 blk_mq_requeue_request(req, false); 295 blk_mq_delay_kick_requeue_list(req->q, delay); 296 } 297 298 static void nvme_log_error(struct request *req) 299 { 300 struct nvme_ns *ns = req->q->queuedata; 301 struct nvme_request *nr = nvme_req(req); 302 303 if (ns) { 304 pr_err_ratelimited("%s: %s(0x%x) @ LBA %llu, %llu blocks, %s (sct 0x%x / sc 0x%x) %s%s\n", 305 ns->disk ? ns->disk->disk_name : "?", 306 nvme_get_opcode_str(nr->cmd->common.opcode), 307 nr->cmd->common.opcode, 308 (unsigned long long)nvme_sect_to_lba(ns, blk_rq_pos(req)), 309 (unsigned long long)blk_rq_bytes(req) >> ns->lba_shift, 310 nvme_get_error_status_str(nr->status), 311 nr->status >> 8 & 7, /* Status Code Type */ 312 nr->status & 0xff, /* Status Code */ 313 nr->status & NVME_SC_MORE ? "MORE " : "", 314 nr->status & NVME_SC_DNR ? "DNR " : ""); 315 return; 316 } 317 318 pr_err_ratelimited("%s: %s(0x%x), %s (sct 0x%x / sc 0x%x) %s%s\n", 319 dev_name(nr->ctrl->device), 320 nvme_get_admin_opcode_str(nr->cmd->common.opcode), 321 nr->cmd->common.opcode, 322 nvme_get_error_status_str(nr->status), 323 nr->status >> 8 & 7, /* Status Code Type */ 324 nr->status & 0xff, /* Status Code */ 325 nr->status & NVME_SC_MORE ? "MORE " : "", 326 nr->status & NVME_SC_DNR ? "DNR " : ""); 327 } 328 329 enum nvme_disposition { 330 COMPLETE, 331 RETRY, 332 FAILOVER, 333 }; 334 335 static inline enum nvme_disposition nvme_decide_disposition(struct request *req) 336 { 337 if (likely(nvme_req(req)->status == 0)) 338 return COMPLETE; 339 340 if (blk_noretry_request(req) || 341 (nvme_req(req)->status & NVME_SC_DNR) || 342 nvme_req(req)->retries >= nvme_max_retries) 343 return COMPLETE; 344 345 if (req->cmd_flags & REQ_NVME_MPATH) { 346 if (nvme_is_path_error(nvme_req(req)->status) || 347 blk_queue_dying(req->q)) 348 return FAILOVER; 349 } else { 350 if (blk_queue_dying(req->q)) 351 return COMPLETE; 352 } 353 354 return RETRY; 355 } 356 357 static inline void nvme_end_req_zoned(struct request *req) 358 { 359 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) && 360 req_op(req) == REQ_OP_ZONE_APPEND) 361 req->__sector = nvme_lba_to_sect(req->q->queuedata, 362 le64_to_cpu(nvme_req(req)->result.u64)); 363 } 364 365 static inline void nvme_end_req(struct request *req) 366 { 367 blk_status_t status = nvme_error_status(nvme_req(req)->status); 368 369 if (unlikely(nvme_req(req)->status != NVME_SC_SUCCESS)) 370 nvme_log_error(req); 371 nvme_end_req_zoned(req); 372 nvme_trace_bio_complete(req); 373 blk_mq_end_request(req, status); 374 } 375 376 void nvme_complete_rq(struct request *req) 377 { 378 trace_nvme_complete_rq(req); 379 nvme_cleanup_cmd(req); 380 381 if (nvme_req(req)->ctrl->kas) 382 nvme_req(req)->ctrl->comp_seen = true; 383 384 switch (nvme_decide_disposition(req)) { 385 case COMPLETE: 386 nvme_end_req(req); 387 return; 388 case RETRY: 389 nvme_retry_req(req); 390 return; 391 case FAILOVER: 392 nvme_failover_req(req); 393 return; 394 } 395 } 396 EXPORT_SYMBOL_GPL(nvme_complete_rq); 397 398 void nvme_complete_batch_req(struct request *req) 399 { 400 trace_nvme_complete_rq(req); 401 nvme_cleanup_cmd(req); 402 nvme_end_req_zoned(req); 403 } 404 EXPORT_SYMBOL_GPL(nvme_complete_batch_req); 405 406 /* 407 * Called to unwind from ->queue_rq on a failed command submission so that the 408 * multipathing code gets called to potentially failover to another path. 409 * The caller needs to unwind all transport specific resource allocations and 410 * must return propagate the return value. 411 */ 412 blk_status_t nvme_host_path_error(struct request *req) 413 { 414 nvme_req(req)->status = NVME_SC_HOST_PATH_ERROR; 415 blk_mq_set_request_complete(req); 416 nvme_complete_rq(req); 417 return BLK_STS_OK; 418 } 419 EXPORT_SYMBOL_GPL(nvme_host_path_error); 420 421 bool nvme_cancel_request(struct request *req, void *data, bool reserved) 422 { 423 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device, 424 "Cancelling I/O %d", req->tag); 425 426 /* don't abort one completed request */ 427 if (blk_mq_request_completed(req)) 428 return true; 429 430 nvme_req(req)->status = NVME_SC_HOST_ABORTED_CMD; 431 nvme_req(req)->flags |= NVME_REQ_CANCELLED; 432 blk_mq_complete_request(req); 433 return true; 434 } 435 EXPORT_SYMBOL_GPL(nvme_cancel_request); 436 437 void nvme_cancel_tagset(struct nvme_ctrl *ctrl) 438 { 439 if (ctrl->tagset) { 440 blk_mq_tagset_busy_iter(ctrl->tagset, 441 nvme_cancel_request, ctrl); 442 blk_mq_tagset_wait_completed_request(ctrl->tagset); 443 } 444 } 445 EXPORT_SYMBOL_GPL(nvme_cancel_tagset); 446 447 void nvme_cancel_admin_tagset(struct nvme_ctrl *ctrl) 448 { 449 if (ctrl->admin_tagset) { 450 blk_mq_tagset_busy_iter(ctrl->admin_tagset, 451 nvme_cancel_request, ctrl); 452 blk_mq_tagset_wait_completed_request(ctrl->admin_tagset); 453 } 454 } 455 EXPORT_SYMBOL_GPL(nvme_cancel_admin_tagset); 456 457 bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl, 458 enum nvme_ctrl_state new_state) 459 { 460 enum nvme_ctrl_state old_state; 461 unsigned long flags; 462 bool changed = false; 463 464 spin_lock_irqsave(&ctrl->lock, flags); 465 466 old_state = ctrl->state; 467 switch (new_state) { 468 case NVME_CTRL_LIVE: 469 switch (old_state) { 470 case NVME_CTRL_NEW: 471 case NVME_CTRL_RESETTING: 472 case NVME_CTRL_CONNECTING: 473 changed = true; 474 fallthrough; 475 default: 476 break; 477 } 478 break; 479 case NVME_CTRL_RESETTING: 480 switch (old_state) { 481 case NVME_CTRL_NEW: 482 case NVME_CTRL_LIVE: 483 changed = true; 484 fallthrough; 485 default: 486 break; 487 } 488 break; 489 case NVME_CTRL_CONNECTING: 490 switch (old_state) { 491 case NVME_CTRL_NEW: 492 case NVME_CTRL_RESETTING: 493 changed = true; 494 fallthrough; 495 default: 496 break; 497 } 498 break; 499 case NVME_CTRL_DELETING: 500 switch (old_state) { 501 case NVME_CTRL_LIVE: 502 case NVME_CTRL_RESETTING: 503 case NVME_CTRL_CONNECTING: 504 changed = true; 505 fallthrough; 506 default: 507 break; 508 } 509 break; 510 case NVME_CTRL_DELETING_NOIO: 511 switch (old_state) { 512 case NVME_CTRL_DELETING: 513 case NVME_CTRL_DEAD: 514 changed = true; 515 fallthrough; 516 default: 517 break; 518 } 519 break; 520 case NVME_CTRL_DEAD: 521 switch (old_state) { 522 case NVME_CTRL_DELETING: 523 changed = true; 524 fallthrough; 525 default: 526 break; 527 } 528 break; 529 default: 530 break; 531 } 532 533 if (changed) { 534 ctrl->state = new_state; 535 wake_up_all(&ctrl->state_wq); 536 } 537 538 spin_unlock_irqrestore(&ctrl->lock, flags); 539 if (!changed) 540 return false; 541 542 if (ctrl->state == NVME_CTRL_LIVE) { 543 if (old_state == NVME_CTRL_CONNECTING) 544 nvme_stop_failfast_work(ctrl); 545 nvme_kick_requeue_lists(ctrl); 546 } else if (ctrl->state == NVME_CTRL_CONNECTING && 547 old_state == NVME_CTRL_RESETTING) { 548 nvme_start_failfast_work(ctrl); 549 } 550 return changed; 551 } 552 EXPORT_SYMBOL_GPL(nvme_change_ctrl_state); 553 554 /* 555 * Returns true for sink states that can't ever transition back to live. 556 */ 557 static bool nvme_state_terminal(struct nvme_ctrl *ctrl) 558 { 559 switch (ctrl->state) { 560 case NVME_CTRL_NEW: 561 case NVME_CTRL_LIVE: 562 case NVME_CTRL_RESETTING: 563 case NVME_CTRL_CONNECTING: 564 return false; 565 case NVME_CTRL_DELETING: 566 case NVME_CTRL_DELETING_NOIO: 567 case NVME_CTRL_DEAD: 568 return true; 569 default: 570 WARN_ONCE(1, "Unhandled ctrl state:%d", ctrl->state); 571 return true; 572 } 573 } 574 575 /* 576 * Waits for the controller state to be resetting, or returns false if it is 577 * not possible to ever transition to that state. 578 */ 579 bool nvme_wait_reset(struct nvme_ctrl *ctrl) 580 { 581 wait_event(ctrl->state_wq, 582 nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING) || 583 nvme_state_terminal(ctrl)); 584 return ctrl->state == NVME_CTRL_RESETTING; 585 } 586 EXPORT_SYMBOL_GPL(nvme_wait_reset); 587 588 static void nvme_free_ns_head(struct kref *ref) 589 { 590 struct nvme_ns_head *head = 591 container_of(ref, struct nvme_ns_head, ref); 592 593 nvme_mpath_remove_disk(head); 594 ida_free(&head->subsys->ns_ida, head->instance); 595 cleanup_srcu_struct(&head->srcu); 596 nvme_put_subsystem(head->subsys); 597 kfree(head); 598 } 599 600 bool nvme_tryget_ns_head(struct nvme_ns_head *head) 601 { 602 return kref_get_unless_zero(&head->ref); 603 } 604 605 void nvme_put_ns_head(struct nvme_ns_head *head) 606 { 607 kref_put(&head->ref, nvme_free_ns_head); 608 } 609 610 static void nvme_free_ns(struct kref *kref) 611 { 612 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref); 613 614 put_disk(ns->disk); 615 nvme_put_ns_head(ns->head); 616 nvme_put_ctrl(ns->ctrl); 617 kfree(ns); 618 } 619 620 static inline bool nvme_get_ns(struct nvme_ns *ns) 621 { 622 return kref_get_unless_zero(&ns->kref); 623 } 624 625 void nvme_put_ns(struct nvme_ns *ns) 626 { 627 kref_put(&ns->kref, nvme_free_ns); 628 } 629 EXPORT_SYMBOL_NS_GPL(nvme_put_ns, NVME_TARGET_PASSTHRU); 630 631 static inline void nvme_clear_nvme_request(struct request *req) 632 { 633 nvme_req(req)->status = 0; 634 nvme_req(req)->retries = 0; 635 nvme_req(req)->flags = 0; 636 req->rq_flags |= RQF_DONTPREP; 637 } 638 639 /* initialize a passthrough request */ 640 void nvme_init_request(struct request *req, struct nvme_command *cmd) 641 { 642 if (req->q->queuedata) 643 req->timeout = NVME_IO_TIMEOUT; 644 else /* no queuedata implies admin queue */ 645 req->timeout = NVME_ADMIN_TIMEOUT; 646 647 /* passthru commands should let the driver set the SGL flags */ 648 cmd->common.flags &= ~NVME_CMD_SGL_ALL; 649 650 req->cmd_flags |= REQ_FAILFAST_DRIVER; 651 if (req->mq_hctx->type == HCTX_TYPE_POLL) 652 req->cmd_flags |= REQ_POLLED; 653 nvme_clear_nvme_request(req); 654 memcpy(nvme_req(req)->cmd, cmd, sizeof(*cmd)); 655 } 656 EXPORT_SYMBOL_GPL(nvme_init_request); 657 658 /* 659 * For something we're not in a state to send to the device the default action 660 * is to busy it and retry it after the controller state is recovered. However, 661 * if the controller is deleting or if anything is marked for failfast or 662 * nvme multipath it is immediately failed. 663 * 664 * Note: commands used to initialize the controller will be marked for failfast. 665 * Note: nvme cli/ioctl commands are marked for failfast. 666 */ 667 blk_status_t nvme_fail_nonready_command(struct nvme_ctrl *ctrl, 668 struct request *rq) 669 { 670 if (ctrl->state != NVME_CTRL_DELETING_NOIO && 671 ctrl->state != NVME_CTRL_DELETING && 672 ctrl->state != NVME_CTRL_DEAD && 673 !test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ctrl->flags) && 674 !blk_noretry_request(rq) && !(rq->cmd_flags & REQ_NVME_MPATH)) 675 return BLK_STS_RESOURCE; 676 return nvme_host_path_error(rq); 677 } 678 EXPORT_SYMBOL_GPL(nvme_fail_nonready_command); 679 680 bool __nvme_check_ready(struct nvme_ctrl *ctrl, struct request *rq, 681 bool queue_live) 682 { 683 struct nvme_request *req = nvme_req(rq); 684 685 /* 686 * currently we have a problem sending passthru commands 687 * on the admin_q if the controller is not LIVE because we can't 688 * make sure that they are going out after the admin connect, 689 * controller enable and/or other commands in the initialization 690 * sequence. until the controller will be LIVE, fail with 691 * BLK_STS_RESOURCE so that they will be rescheduled. 692 */ 693 if (rq->q == ctrl->admin_q && (req->flags & NVME_REQ_USERCMD)) 694 return false; 695 696 if (ctrl->ops->flags & NVME_F_FABRICS) { 697 /* 698 * Only allow commands on a live queue, except for the connect 699 * command, which is require to set the queue live in the 700 * appropinquate states. 701 */ 702 switch (ctrl->state) { 703 case NVME_CTRL_CONNECTING: 704 if (blk_rq_is_passthrough(rq) && nvme_is_fabrics(req->cmd) && 705 req->cmd->fabrics.fctype == nvme_fabrics_type_connect) 706 return true; 707 break; 708 default: 709 break; 710 case NVME_CTRL_DEAD: 711 return false; 712 } 713 } 714 715 return queue_live; 716 } 717 EXPORT_SYMBOL_GPL(__nvme_check_ready); 718 719 static inline void nvme_setup_flush(struct nvme_ns *ns, 720 struct nvme_command *cmnd) 721 { 722 memset(cmnd, 0, sizeof(*cmnd)); 723 cmnd->common.opcode = nvme_cmd_flush; 724 cmnd->common.nsid = cpu_to_le32(ns->head->ns_id); 725 } 726 727 static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req, 728 struct nvme_command *cmnd) 729 { 730 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0; 731 struct nvme_dsm_range *range; 732 struct bio *bio; 733 734 /* 735 * Some devices do not consider the DSM 'Number of Ranges' field when 736 * determining how much data to DMA. Always allocate memory for maximum 737 * number of segments to prevent device reading beyond end of buffer. 738 */ 739 static const size_t alloc_size = sizeof(*range) * NVME_DSM_MAX_RANGES; 740 741 range = kzalloc(alloc_size, GFP_ATOMIC | __GFP_NOWARN); 742 if (!range) { 743 /* 744 * If we fail allocation our range, fallback to the controller 745 * discard page. If that's also busy, it's safe to return 746 * busy, as we know we can make progress once that's freed. 747 */ 748 if (test_and_set_bit_lock(0, &ns->ctrl->discard_page_busy)) 749 return BLK_STS_RESOURCE; 750 751 range = page_address(ns->ctrl->discard_page); 752 } 753 754 __rq_for_each_bio(bio, req) { 755 u64 slba = nvme_sect_to_lba(ns, bio->bi_iter.bi_sector); 756 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift; 757 758 if (n < segments) { 759 range[n].cattr = cpu_to_le32(0); 760 range[n].nlb = cpu_to_le32(nlb); 761 range[n].slba = cpu_to_le64(slba); 762 } 763 n++; 764 } 765 766 if (WARN_ON_ONCE(n != segments)) { 767 if (virt_to_page(range) == ns->ctrl->discard_page) 768 clear_bit_unlock(0, &ns->ctrl->discard_page_busy); 769 else 770 kfree(range); 771 return BLK_STS_IOERR; 772 } 773 774 memset(cmnd, 0, sizeof(*cmnd)); 775 cmnd->dsm.opcode = nvme_cmd_dsm; 776 cmnd->dsm.nsid = cpu_to_le32(ns->head->ns_id); 777 cmnd->dsm.nr = cpu_to_le32(segments - 1); 778 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD); 779 780 req->special_vec.bv_page = virt_to_page(range); 781 req->special_vec.bv_offset = offset_in_page(range); 782 req->special_vec.bv_len = alloc_size; 783 req->rq_flags |= RQF_SPECIAL_PAYLOAD; 784 785 return BLK_STS_OK; 786 } 787 788 static void nvme_set_ref_tag(struct nvme_ns *ns, struct nvme_command *cmnd, 789 struct request *req) 790 { 791 u32 upper, lower; 792 u64 ref48; 793 794 /* both rw and write zeroes share the same reftag format */ 795 switch (ns->guard_type) { 796 case NVME_NVM_NS_16B_GUARD: 797 cmnd->rw.reftag = cpu_to_le32(t10_pi_ref_tag(req)); 798 break; 799 case NVME_NVM_NS_64B_GUARD: 800 ref48 = ext_pi_ref_tag(req); 801 lower = lower_32_bits(ref48); 802 upper = upper_32_bits(ref48); 803 804 cmnd->rw.reftag = cpu_to_le32(lower); 805 cmnd->rw.cdw3 = cpu_to_le32(upper); 806 break; 807 default: 808 break; 809 } 810 } 811 812 static inline blk_status_t nvme_setup_write_zeroes(struct nvme_ns *ns, 813 struct request *req, struct nvme_command *cmnd) 814 { 815 memset(cmnd, 0, sizeof(*cmnd)); 816 817 if (ns->ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES) 818 return nvme_setup_discard(ns, req, cmnd); 819 820 cmnd->write_zeroes.opcode = nvme_cmd_write_zeroes; 821 cmnd->write_zeroes.nsid = cpu_to_le32(ns->head->ns_id); 822 cmnd->write_zeroes.slba = 823 cpu_to_le64(nvme_sect_to_lba(ns, blk_rq_pos(req))); 824 cmnd->write_zeroes.length = 825 cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1); 826 827 if (nvme_ns_has_pi(ns)) { 828 cmnd->write_zeroes.control = cpu_to_le16(NVME_RW_PRINFO_PRACT); 829 830 switch (ns->pi_type) { 831 case NVME_NS_DPS_PI_TYPE1: 832 case NVME_NS_DPS_PI_TYPE2: 833 nvme_set_ref_tag(ns, cmnd, req); 834 break; 835 } 836 } 837 838 return BLK_STS_OK; 839 } 840 841 static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns, 842 struct request *req, struct nvme_command *cmnd, 843 enum nvme_opcode op) 844 { 845 u16 control = 0; 846 u32 dsmgmt = 0; 847 848 if (req->cmd_flags & REQ_FUA) 849 control |= NVME_RW_FUA; 850 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD)) 851 control |= NVME_RW_LR; 852 853 if (req->cmd_flags & REQ_RAHEAD) 854 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH; 855 856 cmnd->rw.opcode = op; 857 cmnd->rw.flags = 0; 858 cmnd->rw.nsid = cpu_to_le32(ns->head->ns_id); 859 cmnd->rw.cdw2 = 0; 860 cmnd->rw.cdw3 = 0; 861 cmnd->rw.metadata = 0; 862 cmnd->rw.slba = cpu_to_le64(nvme_sect_to_lba(ns, blk_rq_pos(req))); 863 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1); 864 cmnd->rw.reftag = 0; 865 cmnd->rw.apptag = 0; 866 cmnd->rw.appmask = 0; 867 868 if (ns->ms) { 869 /* 870 * If formated with metadata, the block layer always provides a 871 * metadata buffer if CONFIG_BLK_DEV_INTEGRITY is enabled. Else 872 * we enable the PRACT bit for protection information or set the 873 * namespace capacity to zero to prevent any I/O. 874 */ 875 if (!blk_integrity_rq(req)) { 876 if (WARN_ON_ONCE(!nvme_ns_has_pi(ns))) 877 return BLK_STS_NOTSUPP; 878 control |= NVME_RW_PRINFO_PRACT; 879 } 880 881 switch (ns->pi_type) { 882 case NVME_NS_DPS_PI_TYPE3: 883 control |= NVME_RW_PRINFO_PRCHK_GUARD; 884 break; 885 case NVME_NS_DPS_PI_TYPE1: 886 case NVME_NS_DPS_PI_TYPE2: 887 control |= NVME_RW_PRINFO_PRCHK_GUARD | 888 NVME_RW_PRINFO_PRCHK_REF; 889 if (op == nvme_cmd_zone_append) 890 control |= NVME_RW_APPEND_PIREMAP; 891 nvme_set_ref_tag(ns, cmnd, req); 892 break; 893 } 894 } 895 896 cmnd->rw.control = cpu_to_le16(control); 897 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt); 898 return 0; 899 } 900 901 void nvme_cleanup_cmd(struct request *req) 902 { 903 if (req->rq_flags & RQF_SPECIAL_PAYLOAD) { 904 struct nvme_ctrl *ctrl = nvme_req(req)->ctrl; 905 906 if (req->special_vec.bv_page == ctrl->discard_page) 907 clear_bit_unlock(0, &ctrl->discard_page_busy); 908 else 909 kfree(bvec_virt(&req->special_vec)); 910 } 911 } 912 EXPORT_SYMBOL_GPL(nvme_cleanup_cmd); 913 914 blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req) 915 { 916 struct nvme_command *cmd = nvme_req(req)->cmd; 917 blk_status_t ret = BLK_STS_OK; 918 919 if (!(req->rq_flags & RQF_DONTPREP)) 920 nvme_clear_nvme_request(req); 921 922 switch (req_op(req)) { 923 case REQ_OP_DRV_IN: 924 case REQ_OP_DRV_OUT: 925 /* these are setup prior to execution in nvme_init_request() */ 926 break; 927 case REQ_OP_FLUSH: 928 nvme_setup_flush(ns, cmd); 929 break; 930 case REQ_OP_ZONE_RESET_ALL: 931 case REQ_OP_ZONE_RESET: 932 ret = nvme_setup_zone_mgmt_send(ns, req, cmd, NVME_ZONE_RESET); 933 break; 934 case REQ_OP_ZONE_OPEN: 935 ret = nvme_setup_zone_mgmt_send(ns, req, cmd, NVME_ZONE_OPEN); 936 break; 937 case REQ_OP_ZONE_CLOSE: 938 ret = nvme_setup_zone_mgmt_send(ns, req, cmd, NVME_ZONE_CLOSE); 939 break; 940 case REQ_OP_ZONE_FINISH: 941 ret = nvme_setup_zone_mgmt_send(ns, req, cmd, NVME_ZONE_FINISH); 942 break; 943 case REQ_OP_WRITE_ZEROES: 944 ret = nvme_setup_write_zeroes(ns, req, cmd); 945 break; 946 case REQ_OP_DISCARD: 947 ret = nvme_setup_discard(ns, req, cmd); 948 break; 949 case REQ_OP_READ: 950 ret = nvme_setup_rw(ns, req, cmd, nvme_cmd_read); 951 break; 952 case REQ_OP_WRITE: 953 ret = nvme_setup_rw(ns, req, cmd, nvme_cmd_write); 954 break; 955 case REQ_OP_ZONE_APPEND: 956 ret = nvme_setup_rw(ns, req, cmd, nvme_cmd_zone_append); 957 break; 958 default: 959 WARN_ON_ONCE(1); 960 return BLK_STS_IOERR; 961 } 962 963 cmd->common.command_id = nvme_cid(req); 964 trace_nvme_setup_cmd(req, cmd); 965 return ret; 966 } 967 EXPORT_SYMBOL_GPL(nvme_setup_cmd); 968 969 /* 970 * Return values: 971 * 0: success 972 * >0: nvme controller's cqe status response 973 * <0: kernel error in lieu of controller response 974 */ 975 static int nvme_execute_rq(struct request *rq, bool at_head) 976 { 977 blk_status_t status; 978 979 status = blk_execute_rq(rq, at_head); 980 if (nvme_req(rq)->flags & NVME_REQ_CANCELLED) 981 return -EINTR; 982 if (nvme_req(rq)->status) 983 return nvme_req(rq)->status; 984 return blk_status_to_errno(status); 985 } 986 987 /* 988 * Returns 0 on success. If the result is negative, it's a Linux error code; 989 * if the result is positive, it's an NVM Express status code 990 */ 991 int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, 992 union nvme_result *result, void *buffer, unsigned bufflen, 993 unsigned timeout, int qid, int at_head, 994 blk_mq_req_flags_t flags) 995 { 996 struct request *req; 997 int ret; 998 999 if (qid == NVME_QID_ANY) 1000 req = blk_mq_alloc_request(q, nvme_req_op(cmd), flags); 1001 else 1002 req = blk_mq_alloc_request_hctx(q, nvme_req_op(cmd), flags, 1003 qid ? qid - 1 : 0); 1004 1005 if (IS_ERR(req)) 1006 return PTR_ERR(req); 1007 nvme_init_request(req, cmd); 1008 1009 if (timeout) 1010 req->timeout = timeout; 1011 1012 if (buffer && bufflen) { 1013 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL); 1014 if (ret) 1015 goto out; 1016 } 1017 1018 ret = nvme_execute_rq(req, at_head); 1019 if (result && ret >= 0) 1020 *result = nvme_req(req)->result; 1021 out: 1022 blk_mq_free_request(req); 1023 return ret; 1024 } 1025 EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd); 1026 1027 int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd, 1028 void *buffer, unsigned bufflen) 1029 { 1030 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0, 1031 NVME_QID_ANY, 0, 0); 1032 } 1033 EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd); 1034 1035 static u32 nvme_known_admin_effects(u8 opcode) 1036 { 1037 switch (opcode) { 1038 case nvme_admin_format_nvm: 1039 return NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_NCC | 1040 NVME_CMD_EFFECTS_CSE_MASK; 1041 case nvme_admin_sanitize_nvm: 1042 return NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK; 1043 default: 1044 break; 1045 } 1046 return 0; 1047 } 1048 1049 u32 nvme_command_effects(struct nvme_ctrl *ctrl, struct nvme_ns *ns, u8 opcode) 1050 { 1051 u32 effects = 0; 1052 1053 if (ns) { 1054 if (ns->head->effects) 1055 effects = le32_to_cpu(ns->head->effects->iocs[opcode]); 1056 if (effects & ~(NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC)) 1057 dev_warn_once(ctrl->device, 1058 "IO command:%02x has unhandled effects:%08x\n", 1059 opcode, effects); 1060 return 0; 1061 } 1062 1063 if (ctrl->effects) 1064 effects = le32_to_cpu(ctrl->effects->acs[opcode]); 1065 effects |= nvme_known_admin_effects(opcode); 1066 1067 return effects; 1068 } 1069 EXPORT_SYMBOL_NS_GPL(nvme_command_effects, NVME_TARGET_PASSTHRU); 1070 1071 static u32 nvme_passthru_start(struct nvme_ctrl *ctrl, struct nvme_ns *ns, 1072 u8 opcode) 1073 { 1074 u32 effects = nvme_command_effects(ctrl, ns, opcode); 1075 1076 /* 1077 * For simplicity, IO to all namespaces is quiesced even if the command 1078 * effects say only one namespace is affected. 1079 */ 1080 if (effects & NVME_CMD_EFFECTS_CSE_MASK) { 1081 mutex_lock(&ctrl->scan_lock); 1082 mutex_lock(&ctrl->subsys->lock); 1083 nvme_mpath_start_freeze(ctrl->subsys); 1084 nvme_mpath_wait_freeze(ctrl->subsys); 1085 nvme_start_freeze(ctrl); 1086 nvme_wait_freeze(ctrl); 1087 } 1088 return effects; 1089 } 1090 1091 static void nvme_passthru_end(struct nvme_ctrl *ctrl, u32 effects, 1092 struct nvme_command *cmd, int status) 1093 { 1094 if (effects & NVME_CMD_EFFECTS_CSE_MASK) { 1095 nvme_unfreeze(ctrl); 1096 nvme_mpath_unfreeze(ctrl->subsys); 1097 mutex_unlock(&ctrl->subsys->lock); 1098 nvme_remove_invalid_namespaces(ctrl, NVME_NSID_ALL); 1099 mutex_unlock(&ctrl->scan_lock); 1100 } 1101 if (effects & NVME_CMD_EFFECTS_CCC) 1102 nvme_init_ctrl_finish(ctrl); 1103 if (effects & (NVME_CMD_EFFECTS_NIC | NVME_CMD_EFFECTS_NCC)) { 1104 nvme_queue_scan(ctrl); 1105 flush_work(&ctrl->scan_work); 1106 } 1107 1108 switch (cmd->common.opcode) { 1109 case nvme_admin_set_features: 1110 switch (le32_to_cpu(cmd->common.cdw10) & 0xFF) { 1111 case NVME_FEAT_KATO: 1112 /* 1113 * Keep alive commands interval on the host should be 1114 * updated when KATO is modified by Set Features 1115 * commands. 1116 */ 1117 if (!status) 1118 nvme_update_keep_alive(ctrl, cmd); 1119 break; 1120 default: 1121 break; 1122 } 1123 break; 1124 default: 1125 break; 1126 } 1127 } 1128 1129 int nvme_execute_passthru_rq(struct request *rq) 1130 { 1131 struct nvme_command *cmd = nvme_req(rq)->cmd; 1132 struct nvme_ctrl *ctrl = nvme_req(rq)->ctrl; 1133 struct nvme_ns *ns = rq->q->queuedata; 1134 u32 effects; 1135 int ret; 1136 1137 effects = nvme_passthru_start(ctrl, ns, cmd->common.opcode); 1138 ret = nvme_execute_rq(rq, false); 1139 if (effects) /* nothing to be done for zero cmd effects */ 1140 nvme_passthru_end(ctrl, effects, cmd, ret); 1141 1142 return ret; 1143 } 1144 EXPORT_SYMBOL_NS_GPL(nvme_execute_passthru_rq, NVME_TARGET_PASSTHRU); 1145 1146 /* 1147 * Recommended frequency for KATO commands per NVMe 1.4 section 7.12.1: 1148 * 1149 * The host should send Keep Alive commands at half of the Keep Alive Timeout 1150 * accounting for transport roundtrip times [..]. 1151 */ 1152 static void nvme_queue_keep_alive_work(struct nvme_ctrl *ctrl) 1153 { 1154 queue_delayed_work(nvme_wq, &ctrl->ka_work, ctrl->kato * HZ / 2); 1155 } 1156 1157 static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status) 1158 { 1159 struct nvme_ctrl *ctrl = rq->end_io_data; 1160 unsigned long flags; 1161 bool startka = false; 1162 1163 blk_mq_free_request(rq); 1164 1165 if (status) { 1166 dev_err(ctrl->device, 1167 "failed nvme_keep_alive_end_io error=%d\n", 1168 status); 1169 return; 1170 } 1171 1172 ctrl->comp_seen = false; 1173 spin_lock_irqsave(&ctrl->lock, flags); 1174 if (ctrl->state == NVME_CTRL_LIVE || 1175 ctrl->state == NVME_CTRL_CONNECTING) 1176 startka = true; 1177 spin_unlock_irqrestore(&ctrl->lock, flags); 1178 if (startka) 1179 nvme_queue_keep_alive_work(ctrl); 1180 } 1181 1182 static void nvme_keep_alive_work(struct work_struct *work) 1183 { 1184 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work), 1185 struct nvme_ctrl, ka_work); 1186 bool comp_seen = ctrl->comp_seen; 1187 struct request *rq; 1188 1189 if ((ctrl->ctratt & NVME_CTRL_ATTR_TBKAS) && comp_seen) { 1190 dev_dbg(ctrl->device, 1191 "reschedule traffic based keep-alive timer\n"); 1192 ctrl->comp_seen = false; 1193 nvme_queue_keep_alive_work(ctrl); 1194 return; 1195 } 1196 1197 rq = blk_mq_alloc_request(ctrl->admin_q, nvme_req_op(&ctrl->ka_cmd), 1198 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT); 1199 if (IS_ERR(rq)) { 1200 /* allocation failure, reset the controller */ 1201 dev_err(ctrl->device, "keep-alive failed: %ld\n", PTR_ERR(rq)); 1202 nvme_reset_ctrl(ctrl); 1203 return; 1204 } 1205 nvme_init_request(rq, &ctrl->ka_cmd); 1206 1207 rq->timeout = ctrl->kato * HZ; 1208 rq->end_io_data = ctrl; 1209 blk_execute_rq_nowait(rq, false, nvme_keep_alive_end_io); 1210 } 1211 1212 static void nvme_start_keep_alive(struct nvme_ctrl *ctrl) 1213 { 1214 if (unlikely(ctrl->kato == 0)) 1215 return; 1216 1217 nvme_queue_keep_alive_work(ctrl); 1218 } 1219 1220 void nvme_stop_keep_alive(struct nvme_ctrl *ctrl) 1221 { 1222 if (unlikely(ctrl->kato == 0)) 1223 return; 1224 1225 cancel_delayed_work_sync(&ctrl->ka_work); 1226 } 1227 EXPORT_SYMBOL_GPL(nvme_stop_keep_alive); 1228 1229 static void nvme_update_keep_alive(struct nvme_ctrl *ctrl, 1230 struct nvme_command *cmd) 1231 { 1232 unsigned int new_kato = 1233 DIV_ROUND_UP(le32_to_cpu(cmd->common.cdw11), 1000); 1234 1235 dev_info(ctrl->device, 1236 "keep alive interval updated from %u ms to %u ms\n", 1237 ctrl->kato * 1000 / 2, new_kato * 1000 / 2); 1238 1239 nvme_stop_keep_alive(ctrl); 1240 ctrl->kato = new_kato; 1241 nvme_start_keep_alive(ctrl); 1242 } 1243 1244 /* 1245 * In NVMe 1.0 the CNS field was just a binary controller or namespace 1246 * flag, thus sending any new CNS opcodes has a big chance of not working. 1247 * Qemu unfortunately had that bug after reporting a 1.1 version compliance 1248 * (but not for any later version). 1249 */ 1250 static bool nvme_ctrl_limited_cns(struct nvme_ctrl *ctrl) 1251 { 1252 if (ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS) 1253 return ctrl->vs < NVME_VS(1, 2, 0); 1254 return ctrl->vs < NVME_VS(1, 1, 0); 1255 } 1256 1257 static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id) 1258 { 1259 struct nvme_command c = { }; 1260 int error; 1261 1262 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ 1263 c.identify.opcode = nvme_admin_identify; 1264 c.identify.cns = NVME_ID_CNS_CTRL; 1265 1266 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL); 1267 if (!*id) 1268 return -ENOMEM; 1269 1270 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id, 1271 sizeof(struct nvme_id_ctrl)); 1272 if (error) 1273 kfree(*id); 1274 return error; 1275 } 1276 1277 static int nvme_process_ns_desc(struct nvme_ctrl *ctrl, struct nvme_ns_ids *ids, 1278 struct nvme_ns_id_desc *cur, bool *csi_seen) 1279 { 1280 const char *warn_str = "ctrl returned bogus length:"; 1281 void *data = cur; 1282 1283 switch (cur->nidt) { 1284 case NVME_NIDT_EUI64: 1285 if (cur->nidl != NVME_NIDT_EUI64_LEN) { 1286 dev_warn(ctrl->device, "%s %d for NVME_NIDT_EUI64\n", 1287 warn_str, cur->nidl); 1288 return -1; 1289 } 1290 memcpy(ids->eui64, data + sizeof(*cur), NVME_NIDT_EUI64_LEN); 1291 return NVME_NIDT_EUI64_LEN; 1292 case NVME_NIDT_NGUID: 1293 if (cur->nidl != NVME_NIDT_NGUID_LEN) { 1294 dev_warn(ctrl->device, "%s %d for NVME_NIDT_NGUID\n", 1295 warn_str, cur->nidl); 1296 return -1; 1297 } 1298 memcpy(ids->nguid, data + sizeof(*cur), NVME_NIDT_NGUID_LEN); 1299 return NVME_NIDT_NGUID_LEN; 1300 case NVME_NIDT_UUID: 1301 if (cur->nidl != NVME_NIDT_UUID_LEN) { 1302 dev_warn(ctrl->device, "%s %d for NVME_NIDT_UUID\n", 1303 warn_str, cur->nidl); 1304 return -1; 1305 } 1306 uuid_copy(&ids->uuid, data + sizeof(*cur)); 1307 return NVME_NIDT_UUID_LEN; 1308 case NVME_NIDT_CSI: 1309 if (cur->nidl != NVME_NIDT_CSI_LEN) { 1310 dev_warn(ctrl->device, "%s %d for NVME_NIDT_CSI\n", 1311 warn_str, cur->nidl); 1312 return -1; 1313 } 1314 memcpy(&ids->csi, data + sizeof(*cur), NVME_NIDT_CSI_LEN); 1315 *csi_seen = true; 1316 return NVME_NIDT_CSI_LEN; 1317 default: 1318 /* Skip unknown types */ 1319 return cur->nidl; 1320 } 1321 } 1322 1323 static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid, 1324 struct nvme_ns_ids *ids) 1325 { 1326 struct nvme_command c = { }; 1327 bool csi_seen = false; 1328 int status, pos, len; 1329 void *data; 1330 1331 if (ctrl->vs < NVME_VS(1, 3, 0) && !nvme_multi_css(ctrl)) 1332 return 0; 1333 if (ctrl->quirks & NVME_QUIRK_NO_NS_DESC_LIST) 1334 return 0; 1335 1336 c.identify.opcode = nvme_admin_identify; 1337 c.identify.nsid = cpu_to_le32(nsid); 1338 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST; 1339 1340 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL); 1341 if (!data) 1342 return -ENOMEM; 1343 1344 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data, 1345 NVME_IDENTIFY_DATA_SIZE); 1346 if (status) { 1347 dev_warn(ctrl->device, 1348 "Identify Descriptors failed (nsid=%u, status=0x%x)\n", 1349 nsid, status); 1350 goto free_data; 1351 } 1352 1353 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) { 1354 struct nvme_ns_id_desc *cur = data + pos; 1355 1356 if (cur->nidl == 0) 1357 break; 1358 1359 len = nvme_process_ns_desc(ctrl, ids, cur, &csi_seen); 1360 if (len < 0) 1361 break; 1362 1363 len += sizeof(*cur); 1364 } 1365 1366 if (nvme_multi_css(ctrl) && !csi_seen) { 1367 dev_warn(ctrl->device, "Command set not reported for nsid:%d\n", 1368 nsid); 1369 status = -EINVAL; 1370 } 1371 1372 free_data: 1373 kfree(data); 1374 return status; 1375 } 1376 1377 static int nvme_identify_ns(struct nvme_ctrl *ctrl, unsigned nsid, 1378 struct nvme_ns_ids *ids, struct nvme_id_ns **id) 1379 { 1380 struct nvme_command c = { }; 1381 int error; 1382 1383 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */ 1384 c.identify.opcode = nvme_admin_identify; 1385 c.identify.nsid = cpu_to_le32(nsid); 1386 c.identify.cns = NVME_ID_CNS_NS; 1387 1388 *id = kmalloc(sizeof(**id), GFP_KERNEL); 1389 if (!*id) 1390 return -ENOMEM; 1391 1392 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, *id, sizeof(**id)); 1393 if (error) { 1394 dev_warn(ctrl->device, "Identify namespace failed (%d)\n", error); 1395 goto out_free_id; 1396 } 1397 1398 error = NVME_SC_INVALID_NS | NVME_SC_DNR; 1399 if ((*id)->ncap == 0) /* namespace not allocated or attached */ 1400 goto out_free_id; 1401 1402 if (ctrl->vs >= NVME_VS(1, 1, 0) && 1403 !memchr_inv(ids->eui64, 0, sizeof(ids->eui64))) 1404 memcpy(ids->eui64, (*id)->eui64, sizeof(ids->eui64)); 1405 if (ctrl->vs >= NVME_VS(1, 2, 0) && 1406 !memchr_inv(ids->nguid, 0, sizeof(ids->nguid))) 1407 memcpy(ids->nguid, (*id)->nguid, sizeof(ids->nguid)); 1408 1409 return 0; 1410 1411 out_free_id: 1412 kfree(*id); 1413 return error; 1414 } 1415 1416 static int nvme_features(struct nvme_ctrl *dev, u8 op, unsigned int fid, 1417 unsigned int dword11, void *buffer, size_t buflen, u32 *result) 1418 { 1419 union nvme_result res = { 0 }; 1420 struct nvme_command c = { }; 1421 int ret; 1422 1423 c.features.opcode = op; 1424 c.features.fid = cpu_to_le32(fid); 1425 c.features.dword11 = cpu_to_le32(dword11); 1426 1427 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, 1428 buffer, buflen, 0, NVME_QID_ANY, 0, 0); 1429 if (ret >= 0 && result) 1430 *result = le32_to_cpu(res.u32); 1431 return ret; 1432 } 1433 1434 int nvme_set_features(struct nvme_ctrl *dev, unsigned int fid, 1435 unsigned int dword11, void *buffer, size_t buflen, 1436 u32 *result) 1437 { 1438 return nvme_features(dev, nvme_admin_set_features, fid, dword11, buffer, 1439 buflen, result); 1440 } 1441 EXPORT_SYMBOL_GPL(nvme_set_features); 1442 1443 int nvme_get_features(struct nvme_ctrl *dev, unsigned int fid, 1444 unsigned int dword11, void *buffer, size_t buflen, 1445 u32 *result) 1446 { 1447 return nvme_features(dev, nvme_admin_get_features, fid, dword11, buffer, 1448 buflen, result); 1449 } 1450 EXPORT_SYMBOL_GPL(nvme_get_features); 1451 1452 int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count) 1453 { 1454 u32 q_count = (*count - 1) | ((*count - 1) << 16); 1455 u32 result; 1456 int status, nr_io_queues; 1457 1458 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0, 1459 &result); 1460 if (status < 0) 1461 return status; 1462 1463 /* 1464 * Degraded controllers might return an error when setting the queue 1465 * count. We still want to be able to bring them online and offer 1466 * access to the admin queue, as that might be only way to fix them up. 1467 */ 1468 if (status > 0) { 1469 dev_err(ctrl->device, "Could not set queue count (%d)\n", status); 1470 *count = 0; 1471 } else { 1472 nr_io_queues = min(result & 0xffff, result >> 16) + 1; 1473 *count = min(*count, nr_io_queues); 1474 } 1475 1476 return 0; 1477 } 1478 EXPORT_SYMBOL_GPL(nvme_set_queue_count); 1479 1480 #define NVME_AEN_SUPPORTED \ 1481 (NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_FW_ACT | \ 1482 NVME_AEN_CFG_ANA_CHANGE | NVME_AEN_CFG_DISC_CHANGE) 1483 1484 static void nvme_enable_aen(struct nvme_ctrl *ctrl) 1485 { 1486 u32 result, supported_aens = ctrl->oaes & NVME_AEN_SUPPORTED; 1487 int status; 1488 1489 if (!supported_aens) 1490 return; 1491 1492 status = nvme_set_features(ctrl, NVME_FEAT_ASYNC_EVENT, supported_aens, 1493 NULL, 0, &result); 1494 if (status) 1495 dev_warn(ctrl->device, "Failed to configure AEN (cfg %x)\n", 1496 supported_aens); 1497 1498 queue_work(nvme_wq, &ctrl->async_event_work); 1499 } 1500 1501 static int nvme_ns_open(struct nvme_ns *ns) 1502 { 1503 1504 /* should never be called due to GENHD_FL_HIDDEN */ 1505 if (WARN_ON_ONCE(nvme_ns_head_multipath(ns->head))) 1506 goto fail; 1507 if (!nvme_get_ns(ns)) 1508 goto fail; 1509 if (!try_module_get(ns->ctrl->ops->module)) 1510 goto fail_put_ns; 1511 1512 return 0; 1513 1514 fail_put_ns: 1515 nvme_put_ns(ns); 1516 fail: 1517 return -ENXIO; 1518 } 1519 1520 static void nvme_ns_release(struct nvme_ns *ns) 1521 { 1522 1523 module_put(ns->ctrl->ops->module); 1524 nvme_put_ns(ns); 1525 } 1526 1527 static int nvme_open(struct block_device *bdev, fmode_t mode) 1528 { 1529 return nvme_ns_open(bdev->bd_disk->private_data); 1530 } 1531 1532 static void nvme_release(struct gendisk *disk, fmode_t mode) 1533 { 1534 nvme_ns_release(disk->private_data); 1535 } 1536 1537 int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo) 1538 { 1539 /* some standard values */ 1540 geo->heads = 1 << 6; 1541 geo->sectors = 1 << 5; 1542 geo->cylinders = get_capacity(bdev->bd_disk) >> 11; 1543 return 0; 1544 } 1545 1546 #ifdef CONFIG_BLK_DEV_INTEGRITY 1547 static void nvme_init_integrity(struct gendisk *disk, struct nvme_ns *ns, 1548 u32 max_integrity_segments) 1549 { 1550 struct blk_integrity integrity = { }; 1551 1552 switch (ns->pi_type) { 1553 case NVME_NS_DPS_PI_TYPE3: 1554 switch (ns->guard_type) { 1555 case NVME_NVM_NS_16B_GUARD: 1556 integrity.profile = &t10_pi_type3_crc; 1557 integrity.tag_size = sizeof(u16) + sizeof(u32); 1558 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; 1559 break; 1560 case NVME_NVM_NS_64B_GUARD: 1561 integrity.profile = &ext_pi_type3_crc64; 1562 integrity.tag_size = sizeof(u16) + 6; 1563 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; 1564 break; 1565 default: 1566 integrity.profile = NULL; 1567 break; 1568 } 1569 break; 1570 case NVME_NS_DPS_PI_TYPE1: 1571 case NVME_NS_DPS_PI_TYPE2: 1572 switch (ns->guard_type) { 1573 case NVME_NVM_NS_16B_GUARD: 1574 integrity.profile = &t10_pi_type1_crc; 1575 integrity.tag_size = sizeof(u16); 1576 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; 1577 break; 1578 case NVME_NVM_NS_64B_GUARD: 1579 integrity.profile = &ext_pi_type1_crc64; 1580 integrity.tag_size = sizeof(u16); 1581 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE; 1582 break; 1583 default: 1584 integrity.profile = NULL; 1585 break; 1586 } 1587 break; 1588 default: 1589 integrity.profile = NULL; 1590 break; 1591 } 1592 1593 integrity.tuple_size = ns->ms; 1594 blk_integrity_register(disk, &integrity); 1595 blk_queue_max_integrity_segments(disk->queue, max_integrity_segments); 1596 } 1597 #else 1598 static void nvme_init_integrity(struct gendisk *disk, struct nvme_ns *ns, 1599 u32 max_integrity_segments) 1600 { 1601 } 1602 #endif /* CONFIG_BLK_DEV_INTEGRITY */ 1603 1604 static void nvme_config_discard(struct gendisk *disk, struct nvme_ns *ns) 1605 { 1606 struct nvme_ctrl *ctrl = ns->ctrl; 1607 struct request_queue *queue = disk->queue; 1608 u32 size = queue_logical_block_size(queue); 1609 1610 if (ctrl->max_discard_sectors == 0) { 1611 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, queue); 1612 return; 1613 } 1614 1615 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) < 1616 NVME_DSM_MAX_RANGES); 1617 1618 queue->limits.discard_alignment = 0; 1619 queue->limits.discard_granularity = size; 1620 1621 /* If discard is already enabled, don't reset queue limits */ 1622 if (blk_queue_flag_test_and_set(QUEUE_FLAG_DISCARD, queue)) 1623 return; 1624 1625 blk_queue_max_discard_sectors(queue, ctrl->max_discard_sectors); 1626 blk_queue_max_discard_segments(queue, ctrl->max_discard_segments); 1627 1628 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES) 1629 blk_queue_max_write_zeroes_sectors(queue, UINT_MAX); 1630 } 1631 1632 static bool nvme_ns_ids_equal(struct nvme_ns_ids *a, struct nvme_ns_ids *b) 1633 { 1634 return uuid_equal(&a->uuid, &b->uuid) && 1635 memcmp(&a->nguid, &b->nguid, sizeof(a->nguid)) == 0 && 1636 memcmp(&a->eui64, &b->eui64, sizeof(a->eui64)) == 0 && 1637 a->csi == b->csi; 1638 } 1639 1640 static int nvme_init_ms(struct nvme_ns *ns, struct nvme_id_ns *id) 1641 { 1642 bool first = id->dps & NVME_NS_DPS_PI_FIRST; 1643 unsigned lbaf = nvme_lbaf_index(id->flbas); 1644 struct nvme_ctrl *ctrl = ns->ctrl; 1645 struct nvme_command c = { }; 1646 struct nvme_id_ns_nvm *nvm; 1647 int ret = 0; 1648 u32 elbaf; 1649 1650 ns->pi_size = 0; 1651 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms); 1652 if (!(ctrl->ctratt & NVME_CTRL_ATTR_ELBAS)) { 1653 ns->pi_size = sizeof(struct t10_pi_tuple); 1654 ns->guard_type = NVME_NVM_NS_16B_GUARD; 1655 goto set_pi; 1656 } 1657 1658 nvm = kzalloc(sizeof(*nvm), GFP_KERNEL); 1659 if (!nvm) 1660 return -ENOMEM; 1661 1662 c.identify.opcode = nvme_admin_identify; 1663 c.identify.nsid = cpu_to_le32(ns->head->ns_id); 1664 c.identify.cns = NVME_ID_CNS_CS_NS; 1665 c.identify.csi = NVME_CSI_NVM; 1666 1667 ret = nvme_submit_sync_cmd(ns->ctrl->admin_q, &c, nvm, sizeof(*nvm)); 1668 if (ret) 1669 goto free_data; 1670 1671 elbaf = le32_to_cpu(nvm->elbaf[lbaf]); 1672 1673 /* no support for storage tag formats right now */ 1674 if (nvme_elbaf_sts(elbaf)) 1675 goto free_data; 1676 1677 ns->guard_type = nvme_elbaf_guard_type(elbaf); 1678 switch (ns->guard_type) { 1679 case NVME_NVM_NS_64B_GUARD: 1680 ns->pi_size = sizeof(struct crc64_pi_tuple); 1681 break; 1682 case NVME_NVM_NS_16B_GUARD: 1683 ns->pi_size = sizeof(struct t10_pi_tuple); 1684 break; 1685 default: 1686 break; 1687 } 1688 1689 free_data: 1690 kfree(nvm); 1691 set_pi: 1692 if (ns->pi_size && (first || ns->ms == ns->pi_size)) 1693 ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK; 1694 else 1695 ns->pi_type = 0; 1696 1697 return ret; 1698 } 1699 1700 static void nvme_configure_metadata(struct nvme_ns *ns, struct nvme_id_ns *id) 1701 { 1702 struct nvme_ctrl *ctrl = ns->ctrl; 1703 1704 if (nvme_init_ms(ns, id)) 1705 return; 1706 1707 ns->features &= ~(NVME_NS_METADATA_SUPPORTED | NVME_NS_EXT_LBAS); 1708 if (!ns->ms || !(ctrl->ops->flags & NVME_F_METADATA_SUPPORTED)) 1709 return; 1710 1711 if (ctrl->ops->flags & NVME_F_FABRICS) { 1712 /* 1713 * The NVMe over Fabrics specification only supports metadata as 1714 * part of the extended data LBA. We rely on HCA/HBA support to 1715 * remap the separate metadata buffer from the block layer. 1716 */ 1717 if (WARN_ON_ONCE(!(id->flbas & NVME_NS_FLBAS_META_EXT))) 1718 return; 1719 1720 ns->features |= NVME_NS_EXT_LBAS; 1721 1722 /* 1723 * The current fabrics transport drivers support namespace 1724 * metadata formats only if nvme_ns_has_pi() returns true. 1725 * Suppress support for all other formats so the namespace will 1726 * have a 0 capacity and not be usable through the block stack. 1727 * 1728 * Note, this check will need to be modified if any drivers 1729 * gain the ability to use other metadata formats. 1730 */ 1731 if (ctrl->max_integrity_segments && nvme_ns_has_pi(ns)) 1732 ns->features |= NVME_NS_METADATA_SUPPORTED; 1733 } else { 1734 /* 1735 * For PCIe controllers, we can't easily remap the separate 1736 * metadata buffer from the block layer and thus require a 1737 * separate metadata buffer for block layer metadata/PI support. 1738 * We allow extended LBAs for the passthrough interface, though. 1739 */ 1740 if (id->flbas & NVME_NS_FLBAS_META_EXT) 1741 ns->features |= NVME_NS_EXT_LBAS; 1742 else 1743 ns->features |= NVME_NS_METADATA_SUPPORTED; 1744 } 1745 } 1746 1747 static void nvme_set_queue_limits(struct nvme_ctrl *ctrl, 1748 struct request_queue *q) 1749 { 1750 bool vwc = ctrl->vwc & NVME_CTRL_VWC_PRESENT; 1751 1752 if (ctrl->max_hw_sectors) { 1753 u32 max_segments = 1754 (ctrl->max_hw_sectors / (NVME_CTRL_PAGE_SIZE >> 9)) + 1; 1755 1756 max_segments = min_not_zero(max_segments, ctrl->max_segments); 1757 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors); 1758 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX)); 1759 } 1760 blk_queue_virt_boundary(q, NVME_CTRL_PAGE_SIZE - 1); 1761 blk_queue_dma_alignment(q, 7); 1762 blk_queue_write_cache(q, vwc, vwc); 1763 } 1764 1765 static void nvme_update_disk_info(struct gendisk *disk, 1766 struct nvme_ns *ns, struct nvme_id_ns *id) 1767 { 1768 sector_t capacity = nvme_lba_to_sect(ns, le64_to_cpu(id->nsze)); 1769 unsigned short bs = 1 << ns->lba_shift; 1770 u32 atomic_bs, phys_bs, io_opt = 0; 1771 1772 /* 1773 * The block layer can't support LBA sizes larger than the page size 1774 * yet, so catch this early and don't allow block I/O. 1775 */ 1776 if (ns->lba_shift > PAGE_SHIFT) { 1777 capacity = 0; 1778 bs = (1 << 9); 1779 } 1780 1781 blk_integrity_unregister(disk); 1782 1783 atomic_bs = phys_bs = bs; 1784 if (id->nabo == 0) { 1785 /* 1786 * Bit 1 indicates whether NAWUPF is defined for this namespace 1787 * and whether it should be used instead of AWUPF. If NAWUPF == 1788 * 0 then AWUPF must be used instead. 1789 */ 1790 if (id->nsfeat & NVME_NS_FEAT_ATOMICS && id->nawupf) 1791 atomic_bs = (1 + le16_to_cpu(id->nawupf)) * bs; 1792 else 1793 atomic_bs = (1 + ns->ctrl->subsys->awupf) * bs; 1794 } 1795 1796 if (id->nsfeat & NVME_NS_FEAT_IO_OPT) { 1797 /* NPWG = Namespace Preferred Write Granularity */ 1798 phys_bs = bs * (1 + le16_to_cpu(id->npwg)); 1799 /* NOWS = Namespace Optimal Write Size */ 1800 io_opt = bs * (1 + le16_to_cpu(id->nows)); 1801 } 1802 1803 blk_queue_logical_block_size(disk->queue, bs); 1804 /* 1805 * Linux filesystems assume writing a single physical block is 1806 * an atomic operation. Hence limit the physical block size to the 1807 * value of the Atomic Write Unit Power Fail parameter. 1808 */ 1809 blk_queue_physical_block_size(disk->queue, min(phys_bs, atomic_bs)); 1810 blk_queue_io_min(disk->queue, phys_bs); 1811 blk_queue_io_opt(disk->queue, io_opt); 1812 1813 /* 1814 * Register a metadata profile for PI, or the plain non-integrity NVMe 1815 * metadata masquerading as Type 0 if supported, otherwise reject block 1816 * I/O to namespaces with metadata except when the namespace supports 1817 * PI, as it can strip/insert in that case. 1818 */ 1819 if (ns->ms) { 1820 if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) && 1821 (ns->features & NVME_NS_METADATA_SUPPORTED)) 1822 nvme_init_integrity(disk, ns, 1823 ns->ctrl->max_integrity_segments); 1824 else if (!nvme_ns_has_pi(ns)) 1825 capacity = 0; 1826 } 1827 1828 set_capacity_and_notify(disk, capacity); 1829 1830 nvme_config_discard(disk, ns); 1831 blk_queue_max_write_zeroes_sectors(disk->queue, 1832 ns->ctrl->max_zeroes_sectors); 1833 } 1834 1835 static inline bool nvme_first_scan(struct gendisk *disk) 1836 { 1837 /* nvme_alloc_ns() scans the disk prior to adding it */ 1838 return !disk_live(disk); 1839 } 1840 1841 static void nvme_set_chunk_sectors(struct nvme_ns *ns, struct nvme_id_ns *id) 1842 { 1843 struct nvme_ctrl *ctrl = ns->ctrl; 1844 u32 iob; 1845 1846 if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) && 1847 is_power_of_2(ctrl->max_hw_sectors)) 1848 iob = ctrl->max_hw_sectors; 1849 else 1850 iob = nvme_lba_to_sect(ns, le16_to_cpu(id->noiob)); 1851 1852 if (!iob) 1853 return; 1854 1855 if (!is_power_of_2(iob)) { 1856 if (nvme_first_scan(ns->disk)) 1857 pr_warn("%s: ignoring unaligned IO boundary:%u\n", 1858 ns->disk->disk_name, iob); 1859 return; 1860 } 1861 1862 if (blk_queue_is_zoned(ns->disk->queue)) { 1863 if (nvme_first_scan(ns->disk)) 1864 pr_warn("%s: ignoring zoned namespace IO boundary\n", 1865 ns->disk->disk_name); 1866 return; 1867 } 1868 1869 blk_queue_chunk_sectors(ns->queue, iob); 1870 } 1871 1872 static int nvme_update_ns_info(struct nvme_ns *ns, struct nvme_id_ns *id) 1873 { 1874 unsigned lbaf = nvme_lbaf_index(id->flbas); 1875 int ret; 1876 1877 blk_mq_freeze_queue(ns->disk->queue); 1878 ns->lba_shift = id->lbaf[lbaf].ds; 1879 nvme_set_queue_limits(ns->ctrl, ns->queue); 1880 1881 nvme_configure_metadata(ns, id); 1882 nvme_set_chunk_sectors(ns, id); 1883 nvme_update_disk_info(ns->disk, ns, id); 1884 1885 if (ns->head->ids.csi == NVME_CSI_ZNS) { 1886 ret = nvme_update_zone_info(ns, lbaf); 1887 if (ret) 1888 goto out_unfreeze; 1889 } 1890 1891 set_disk_ro(ns->disk, (id->nsattr & NVME_NS_ATTR_RO) || 1892 test_bit(NVME_NS_FORCE_RO, &ns->flags)); 1893 set_bit(NVME_NS_READY, &ns->flags); 1894 blk_mq_unfreeze_queue(ns->disk->queue); 1895 1896 if (blk_queue_is_zoned(ns->queue)) { 1897 ret = nvme_revalidate_zones(ns); 1898 if (ret && !nvme_first_scan(ns->disk)) 1899 return ret; 1900 } 1901 1902 if (nvme_ns_head_multipath(ns->head)) { 1903 blk_mq_freeze_queue(ns->head->disk->queue); 1904 nvme_update_disk_info(ns->head->disk, ns, id); 1905 set_disk_ro(ns->head->disk, 1906 (id->nsattr & NVME_NS_ATTR_RO) || 1907 test_bit(NVME_NS_FORCE_RO, &ns->flags)); 1908 nvme_mpath_revalidate_paths(ns); 1909 blk_stack_limits(&ns->head->disk->queue->limits, 1910 &ns->queue->limits, 0); 1911 disk_update_readahead(ns->head->disk); 1912 blk_mq_unfreeze_queue(ns->head->disk->queue); 1913 } 1914 return 0; 1915 1916 out_unfreeze: 1917 /* 1918 * If probing fails due an unsupported feature, hide the block device, 1919 * but still allow other access. 1920 */ 1921 if (ret == -ENODEV) { 1922 ns->disk->flags |= GENHD_FL_HIDDEN; 1923 set_bit(NVME_NS_READY, &ns->flags); 1924 ret = 0; 1925 } 1926 blk_mq_unfreeze_queue(ns->disk->queue); 1927 return ret; 1928 } 1929 1930 static char nvme_pr_type(enum pr_type type) 1931 { 1932 switch (type) { 1933 case PR_WRITE_EXCLUSIVE: 1934 return 1; 1935 case PR_EXCLUSIVE_ACCESS: 1936 return 2; 1937 case PR_WRITE_EXCLUSIVE_REG_ONLY: 1938 return 3; 1939 case PR_EXCLUSIVE_ACCESS_REG_ONLY: 1940 return 4; 1941 case PR_WRITE_EXCLUSIVE_ALL_REGS: 1942 return 5; 1943 case PR_EXCLUSIVE_ACCESS_ALL_REGS: 1944 return 6; 1945 default: 1946 return 0; 1947 } 1948 } 1949 1950 static int nvme_send_ns_head_pr_command(struct block_device *bdev, 1951 struct nvme_command *c, u8 data[16]) 1952 { 1953 struct nvme_ns_head *head = bdev->bd_disk->private_data; 1954 int srcu_idx = srcu_read_lock(&head->srcu); 1955 struct nvme_ns *ns = nvme_find_path(head); 1956 int ret = -EWOULDBLOCK; 1957 1958 if (ns) { 1959 c->common.nsid = cpu_to_le32(ns->head->ns_id); 1960 ret = nvme_submit_sync_cmd(ns->queue, c, data, 16); 1961 } 1962 srcu_read_unlock(&head->srcu, srcu_idx); 1963 return ret; 1964 } 1965 1966 static int nvme_send_ns_pr_command(struct nvme_ns *ns, struct nvme_command *c, 1967 u8 data[16]) 1968 { 1969 c->common.nsid = cpu_to_le32(ns->head->ns_id); 1970 return nvme_submit_sync_cmd(ns->queue, c, data, 16); 1971 } 1972 1973 static int nvme_pr_command(struct block_device *bdev, u32 cdw10, 1974 u64 key, u64 sa_key, u8 op) 1975 { 1976 struct nvme_command c = { }; 1977 u8 data[16] = { 0, }; 1978 1979 put_unaligned_le64(key, &data[0]); 1980 put_unaligned_le64(sa_key, &data[8]); 1981 1982 c.common.opcode = op; 1983 c.common.cdw10 = cpu_to_le32(cdw10); 1984 1985 if (IS_ENABLED(CONFIG_NVME_MULTIPATH) && 1986 bdev->bd_disk->fops == &nvme_ns_head_ops) 1987 return nvme_send_ns_head_pr_command(bdev, &c, data); 1988 return nvme_send_ns_pr_command(bdev->bd_disk->private_data, &c, data); 1989 } 1990 1991 static int nvme_pr_register(struct block_device *bdev, u64 old, 1992 u64 new, unsigned flags) 1993 { 1994 u32 cdw10; 1995 1996 if (flags & ~PR_FL_IGNORE_KEY) 1997 return -EOPNOTSUPP; 1998 1999 cdw10 = old ? 2 : 0; 2000 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0; 2001 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */ 2002 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register); 2003 } 2004 2005 static int nvme_pr_reserve(struct block_device *bdev, u64 key, 2006 enum pr_type type, unsigned flags) 2007 { 2008 u32 cdw10; 2009 2010 if (flags & ~PR_FL_IGNORE_KEY) 2011 return -EOPNOTSUPP; 2012 2013 cdw10 = nvme_pr_type(type) << 8; 2014 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0); 2015 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire); 2016 } 2017 2018 static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new, 2019 enum pr_type type, bool abort) 2020 { 2021 u32 cdw10 = nvme_pr_type(type) << 8 | (abort ? 2 : 1); 2022 2023 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire); 2024 } 2025 2026 static int nvme_pr_clear(struct block_device *bdev, u64 key) 2027 { 2028 u32 cdw10 = 1 | (key ? 1 << 3 : 0); 2029 2030 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register); 2031 } 2032 2033 static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type) 2034 { 2035 u32 cdw10 = nvme_pr_type(type) << 8 | (key ? 1 << 3 : 0); 2036 2037 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release); 2038 } 2039 2040 const struct pr_ops nvme_pr_ops = { 2041 .pr_register = nvme_pr_register, 2042 .pr_reserve = nvme_pr_reserve, 2043 .pr_release = nvme_pr_release, 2044 .pr_preempt = nvme_pr_preempt, 2045 .pr_clear = nvme_pr_clear, 2046 }; 2047 2048 #ifdef CONFIG_BLK_SED_OPAL 2049 int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len, 2050 bool send) 2051 { 2052 struct nvme_ctrl *ctrl = data; 2053 struct nvme_command cmd = { }; 2054 2055 if (send) 2056 cmd.common.opcode = nvme_admin_security_send; 2057 else 2058 cmd.common.opcode = nvme_admin_security_recv; 2059 cmd.common.nsid = 0; 2060 cmd.common.cdw10 = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8); 2061 cmd.common.cdw11 = cpu_to_le32(len); 2062 2063 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len, 0, 2064 NVME_QID_ANY, 1, 0); 2065 } 2066 EXPORT_SYMBOL_GPL(nvme_sec_submit); 2067 #endif /* CONFIG_BLK_SED_OPAL */ 2068 2069 #ifdef CONFIG_BLK_DEV_ZONED 2070 static int nvme_report_zones(struct gendisk *disk, sector_t sector, 2071 unsigned int nr_zones, report_zones_cb cb, void *data) 2072 { 2073 return nvme_ns_report_zones(disk->private_data, sector, nr_zones, cb, 2074 data); 2075 } 2076 #else 2077 #define nvme_report_zones NULL 2078 #endif /* CONFIG_BLK_DEV_ZONED */ 2079 2080 static const struct block_device_operations nvme_bdev_ops = { 2081 .owner = THIS_MODULE, 2082 .ioctl = nvme_ioctl, 2083 .open = nvme_open, 2084 .release = nvme_release, 2085 .getgeo = nvme_getgeo, 2086 .report_zones = nvme_report_zones, 2087 .pr_ops = &nvme_pr_ops, 2088 }; 2089 2090 static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled) 2091 { 2092 unsigned long timeout = 2093 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies; 2094 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0; 2095 int ret; 2096 2097 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { 2098 if (csts == ~0) 2099 return -ENODEV; 2100 if ((csts & NVME_CSTS_RDY) == bit) 2101 break; 2102 2103 usleep_range(1000, 2000); 2104 if (fatal_signal_pending(current)) 2105 return -EINTR; 2106 if (time_after(jiffies, timeout)) { 2107 dev_err(ctrl->device, 2108 "Device not ready; aborting %s, CSTS=0x%x\n", 2109 enabled ? "initialisation" : "reset", csts); 2110 return -ENODEV; 2111 } 2112 } 2113 2114 return ret; 2115 } 2116 2117 /* 2118 * If the device has been passed off to us in an enabled state, just clear 2119 * the enabled bit. The spec says we should set the 'shutdown notification 2120 * bits', but doing so may cause the device to complete commands to the 2121 * admin queue ... and we don't know what memory that might be pointing at! 2122 */ 2123 int nvme_disable_ctrl(struct nvme_ctrl *ctrl) 2124 { 2125 int ret; 2126 2127 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; 2128 ctrl->ctrl_config &= ~NVME_CC_ENABLE; 2129 2130 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); 2131 if (ret) 2132 return ret; 2133 2134 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY) 2135 msleep(NVME_QUIRK_DELAY_AMOUNT); 2136 2137 return nvme_wait_ready(ctrl, ctrl->cap, false); 2138 } 2139 EXPORT_SYMBOL_GPL(nvme_disable_ctrl); 2140 2141 int nvme_enable_ctrl(struct nvme_ctrl *ctrl) 2142 { 2143 unsigned dev_page_min; 2144 int ret; 2145 2146 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &ctrl->cap); 2147 if (ret) { 2148 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret); 2149 return ret; 2150 } 2151 dev_page_min = NVME_CAP_MPSMIN(ctrl->cap) + 12; 2152 2153 if (NVME_CTRL_PAGE_SHIFT < dev_page_min) { 2154 dev_err(ctrl->device, 2155 "Minimum device page size %u too large for host (%u)\n", 2156 1 << dev_page_min, 1 << NVME_CTRL_PAGE_SHIFT); 2157 return -ENODEV; 2158 } 2159 2160 if (NVME_CAP_CSS(ctrl->cap) & NVME_CAP_CSS_CSI) 2161 ctrl->ctrl_config = NVME_CC_CSS_CSI; 2162 else 2163 ctrl->ctrl_config = NVME_CC_CSS_NVM; 2164 ctrl->ctrl_config |= (NVME_CTRL_PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT; 2165 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE; 2166 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES; 2167 ctrl->ctrl_config |= NVME_CC_ENABLE; 2168 2169 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); 2170 if (ret) 2171 return ret; 2172 return nvme_wait_ready(ctrl, ctrl->cap, true); 2173 } 2174 EXPORT_SYMBOL_GPL(nvme_enable_ctrl); 2175 2176 int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl) 2177 { 2178 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ); 2179 u32 csts; 2180 int ret; 2181 2182 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK; 2183 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL; 2184 2185 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config); 2186 if (ret) 2187 return ret; 2188 2189 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) { 2190 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT) 2191 break; 2192 2193 msleep(100); 2194 if (fatal_signal_pending(current)) 2195 return -EINTR; 2196 if (time_after(jiffies, timeout)) { 2197 dev_err(ctrl->device, 2198 "Device shutdown incomplete; abort shutdown\n"); 2199 return -ENODEV; 2200 } 2201 } 2202 2203 return ret; 2204 } 2205 EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl); 2206 2207 static int nvme_configure_timestamp(struct nvme_ctrl *ctrl) 2208 { 2209 __le64 ts; 2210 int ret; 2211 2212 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP)) 2213 return 0; 2214 2215 ts = cpu_to_le64(ktime_to_ms(ktime_get_real())); 2216 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts), 2217 NULL); 2218 if (ret) 2219 dev_warn_once(ctrl->device, 2220 "could not set timestamp (%d)\n", ret); 2221 return ret; 2222 } 2223 2224 static int nvme_configure_host_options(struct nvme_ctrl *ctrl) 2225 { 2226 struct nvme_feat_host_behavior *host; 2227 u8 acre = 0, lbafee = 0; 2228 int ret; 2229 2230 /* Don't bother enabling the feature if retry delay is not reported */ 2231 if (ctrl->crdt[0]) 2232 acre = NVME_ENABLE_ACRE; 2233 if (ctrl->ctratt & NVME_CTRL_ATTR_ELBAS) 2234 lbafee = NVME_ENABLE_LBAFEE; 2235 2236 if (!acre && !lbafee) 2237 return 0; 2238 2239 host = kzalloc(sizeof(*host), GFP_KERNEL); 2240 if (!host) 2241 return 0; 2242 2243 host->acre = acre; 2244 host->lbafee = lbafee; 2245 ret = nvme_set_features(ctrl, NVME_FEAT_HOST_BEHAVIOR, 0, 2246 host, sizeof(*host), NULL); 2247 kfree(host); 2248 return ret; 2249 } 2250 2251 /* 2252 * The function checks whether the given total (exlat + enlat) latency of 2253 * a power state allows the latter to be used as an APST transition target. 2254 * It does so by comparing the latency to the primary and secondary latency 2255 * tolerances defined by module params. If there's a match, the corresponding 2256 * timeout value is returned and the matching tolerance index (1 or 2) is 2257 * reported. 2258 */ 2259 static bool nvme_apst_get_transition_time(u64 total_latency, 2260 u64 *transition_time, unsigned *last_index) 2261 { 2262 if (total_latency <= apst_primary_latency_tol_us) { 2263 if (*last_index == 1) 2264 return false; 2265 *last_index = 1; 2266 *transition_time = apst_primary_timeout_ms; 2267 return true; 2268 } 2269 if (apst_secondary_timeout_ms && 2270 total_latency <= apst_secondary_latency_tol_us) { 2271 if (*last_index <= 2) 2272 return false; 2273 *last_index = 2; 2274 *transition_time = apst_secondary_timeout_ms; 2275 return true; 2276 } 2277 return false; 2278 } 2279 2280 /* 2281 * APST (Autonomous Power State Transition) lets us program a table of power 2282 * state transitions that the controller will perform automatically. 2283 * 2284 * Depending on module params, one of the two supported techniques will be used: 2285 * 2286 * - If the parameters provide explicit timeouts and tolerances, they will be 2287 * used to build a table with up to 2 non-operational states to transition to. 2288 * The default parameter values were selected based on the values used by 2289 * Microsoft's and Intel's NVMe drivers. Yet, since we don't implement dynamic 2290 * regeneration of the APST table in the event of switching between external 2291 * and battery power, the timeouts and tolerances reflect a compromise 2292 * between values used by Microsoft for AC and battery scenarios. 2293 * - If not, we'll configure the table with a simple heuristic: we are willing 2294 * to spend at most 2% of the time transitioning between power states. 2295 * Therefore, when running in any given state, we will enter the next 2296 * lower-power non-operational state after waiting 50 * (enlat + exlat) 2297 * microseconds, as long as that state's exit latency is under the requested 2298 * maximum latency. 2299 * 2300 * We will not autonomously enter any non-operational state for which the total 2301 * latency exceeds ps_max_latency_us. 2302 * 2303 * Users can set ps_max_latency_us to zero to turn off APST. 2304 */ 2305 static int nvme_configure_apst(struct nvme_ctrl *ctrl) 2306 { 2307 struct nvme_feat_auto_pst *table; 2308 unsigned apste = 0; 2309 u64 max_lat_us = 0; 2310 __le64 target = 0; 2311 int max_ps = -1; 2312 int state; 2313 int ret; 2314 unsigned last_lt_index = UINT_MAX; 2315 2316 /* 2317 * If APST isn't supported or if we haven't been initialized yet, 2318 * then don't do anything. 2319 */ 2320 if (!ctrl->apsta) 2321 return 0; 2322 2323 if (ctrl->npss > 31) { 2324 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n"); 2325 return 0; 2326 } 2327 2328 table = kzalloc(sizeof(*table), GFP_KERNEL); 2329 if (!table) 2330 return 0; 2331 2332 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) { 2333 /* Turn off APST. */ 2334 dev_dbg(ctrl->device, "APST disabled\n"); 2335 goto done; 2336 } 2337 2338 /* 2339 * Walk through all states from lowest- to highest-power. 2340 * According to the spec, lower-numbered states use more power. NPSS, 2341 * despite the name, is the index of the lowest-power state, not the 2342 * number of states. 2343 */ 2344 for (state = (int)ctrl->npss; state >= 0; state--) { 2345 u64 total_latency_us, exit_latency_us, transition_ms; 2346 2347 if (target) 2348 table->entries[state] = target; 2349 2350 /* 2351 * Don't allow transitions to the deepest state if it's quirked 2352 * off. 2353 */ 2354 if (state == ctrl->npss && 2355 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) 2356 continue; 2357 2358 /* 2359 * Is this state a useful non-operational state for higher-power 2360 * states to autonomously transition to? 2361 */ 2362 if (!(ctrl->psd[state].flags & NVME_PS_FLAGS_NON_OP_STATE)) 2363 continue; 2364 2365 exit_latency_us = (u64)le32_to_cpu(ctrl->psd[state].exit_lat); 2366 if (exit_latency_us > ctrl->ps_max_latency_us) 2367 continue; 2368 2369 total_latency_us = exit_latency_us + 2370 le32_to_cpu(ctrl->psd[state].entry_lat); 2371 2372 /* 2373 * This state is good. It can be used as the APST idle target 2374 * for higher power states. 2375 */ 2376 if (apst_primary_timeout_ms && apst_primary_latency_tol_us) { 2377 if (!nvme_apst_get_transition_time(total_latency_us, 2378 &transition_ms, &last_lt_index)) 2379 continue; 2380 } else { 2381 transition_ms = total_latency_us + 19; 2382 do_div(transition_ms, 20); 2383 if (transition_ms > (1 << 24) - 1) 2384 transition_ms = (1 << 24) - 1; 2385 } 2386 2387 target = cpu_to_le64((state << 3) | (transition_ms << 8)); 2388 if (max_ps == -1) 2389 max_ps = state; 2390 if (total_latency_us > max_lat_us) 2391 max_lat_us = total_latency_us; 2392 } 2393 2394 if (max_ps == -1) 2395 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n"); 2396 else 2397 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n", 2398 max_ps, max_lat_us, (int)sizeof(*table), table); 2399 apste = 1; 2400 2401 done: 2402 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste, 2403 table, sizeof(*table), NULL); 2404 if (ret) 2405 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret); 2406 kfree(table); 2407 return ret; 2408 } 2409 2410 static void nvme_set_latency_tolerance(struct device *dev, s32 val) 2411 { 2412 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 2413 u64 latency; 2414 2415 switch (val) { 2416 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT: 2417 case PM_QOS_LATENCY_ANY: 2418 latency = U64_MAX; 2419 break; 2420 2421 default: 2422 latency = val; 2423 } 2424 2425 if (ctrl->ps_max_latency_us != latency) { 2426 ctrl->ps_max_latency_us = latency; 2427 if (ctrl->state == NVME_CTRL_LIVE) 2428 nvme_configure_apst(ctrl); 2429 } 2430 } 2431 2432 struct nvme_core_quirk_entry { 2433 /* 2434 * NVMe model and firmware strings are padded with spaces. For 2435 * simplicity, strings in the quirk table are padded with NULLs 2436 * instead. 2437 */ 2438 u16 vid; 2439 const char *mn; 2440 const char *fr; 2441 unsigned long quirks; 2442 }; 2443 2444 static const struct nvme_core_quirk_entry core_quirks[] = { 2445 { 2446 /* 2447 * This Toshiba device seems to die using any APST states. See: 2448 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11 2449 */ 2450 .vid = 0x1179, 2451 .mn = "THNSF5256GPUK TOSHIBA", 2452 .quirks = NVME_QUIRK_NO_APST, 2453 }, 2454 { 2455 /* 2456 * This LiteON CL1-3D*-Q11 firmware version has a race 2457 * condition associated with actions related to suspend to idle 2458 * LiteON has resolved the problem in future firmware 2459 */ 2460 .vid = 0x14a4, 2461 .fr = "22301111", 2462 .quirks = NVME_QUIRK_SIMPLE_SUSPEND, 2463 }, 2464 { 2465 /* 2466 * This Kioxia CD6-V Series / HPE PE8030 device times out and 2467 * aborts I/O during any load, but more easily reproducible 2468 * with discards (fstrim). 2469 * 2470 * The device is left in a state where it is also not possible 2471 * to use "nvme set-feature" to disable APST, but booting with 2472 * nvme_core.default_ps_max_latency=0 works. 2473 */ 2474 .vid = 0x1e0f, 2475 .mn = "KCD6XVUL6T40", 2476 .quirks = NVME_QUIRK_NO_APST, 2477 } 2478 }; 2479 2480 /* match is null-terminated but idstr is space-padded. */ 2481 static bool string_matches(const char *idstr, const char *match, size_t len) 2482 { 2483 size_t matchlen; 2484 2485 if (!match) 2486 return true; 2487 2488 matchlen = strlen(match); 2489 WARN_ON_ONCE(matchlen > len); 2490 2491 if (memcmp(idstr, match, matchlen)) 2492 return false; 2493 2494 for (; matchlen < len; matchlen++) 2495 if (idstr[matchlen] != ' ') 2496 return false; 2497 2498 return true; 2499 } 2500 2501 static bool quirk_matches(const struct nvme_id_ctrl *id, 2502 const struct nvme_core_quirk_entry *q) 2503 { 2504 return q->vid == le16_to_cpu(id->vid) && 2505 string_matches(id->mn, q->mn, sizeof(id->mn)) && 2506 string_matches(id->fr, q->fr, sizeof(id->fr)); 2507 } 2508 2509 static void nvme_init_subnqn(struct nvme_subsystem *subsys, struct nvme_ctrl *ctrl, 2510 struct nvme_id_ctrl *id) 2511 { 2512 size_t nqnlen; 2513 int off; 2514 2515 if(!(ctrl->quirks & NVME_QUIRK_IGNORE_DEV_SUBNQN)) { 2516 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE); 2517 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) { 2518 strlcpy(subsys->subnqn, id->subnqn, NVMF_NQN_SIZE); 2519 return; 2520 } 2521 2522 if (ctrl->vs >= NVME_VS(1, 2, 1)) 2523 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n"); 2524 } 2525 2526 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */ 2527 off = snprintf(subsys->subnqn, NVMF_NQN_SIZE, 2528 "nqn.2014.08.org.nvmexpress:%04x%04x", 2529 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid)); 2530 memcpy(subsys->subnqn + off, id->sn, sizeof(id->sn)); 2531 off += sizeof(id->sn); 2532 memcpy(subsys->subnqn + off, id->mn, sizeof(id->mn)); 2533 off += sizeof(id->mn); 2534 memset(subsys->subnqn + off, 0, sizeof(subsys->subnqn) - off); 2535 } 2536 2537 static void nvme_release_subsystem(struct device *dev) 2538 { 2539 struct nvme_subsystem *subsys = 2540 container_of(dev, struct nvme_subsystem, dev); 2541 2542 if (subsys->instance >= 0) 2543 ida_free(&nvme_instance_ida, subsys->instance); 2544 kfree(subsys); 2545 } 2546 2547 static void nvme_destroy_subsystem(struct kref *ref) 2548 { 2549 struct nvme_subsystem *subsys = 2550 container_of(ref, struct nvme_subsystem, ref); 2551 2552 mutex_lock(&nvme_subsystems_lock); 2553 list_del(&subsys->entry); 2554 mutex_unlock(&nvme_subsystems_lock); 2555 2556 ida_destroy(&subsys->ns_ida); 2557 device_del(&subsys->dev); 2558 put_device(&subsys->dev); 2559 } 2560 2561 static void nvme_put_subsystem(struct nvme_subsystem *subsys) 2562 { 2563 kref_put(&subsys->ref, nvme_destroy_subsystem); 2564 } 2565 2566 static struct nvme_subsystem *__nvme_find_get_subsystem(const char *subsysnqn) 2567 { 2568 struct nvme_subsystem *subsys; 2569 2570 lockdep_assert_held(&nvme_subsystems_lock); 2571 2572 /* 2573 * Fail matches for discovery subsystems. This results 2574 * in each discovery controller bound to a unique subsystem. 2575 * This avoids issues with validating controller values 2576 * that can only be true when there is a single unique subsystem. 2577 * There may be multiple and completely independent entities 2578 * that provide discovery controllers. 2579 */ 2580 if (!strcmp(subsysnqn, NVME_DISC_SUBSYS_NAME)) 2581 return NULL; 2582 2583 list_for_each_entry(subsys, &nvme_subsystems, entry) { 2584 if (strcmp(subsys->subnqn, subsysnqn)) 2585 continue; 2586 if (!kref_get_unless_zero(&subsys->ref)) 2587 continue; 2588 return subsys; 2589 } 2590 2591 return NULL; 2592 } 2593 2594 #define SUBSYS_ATTR_RO(_name, _mode, _show) \ 2595 struct device_attribute subsys_attr_##_name = \ 2596 __ATTR(_name, _mode, _show, NULL) 2597 2598 static ssize_t nvme_subsys_show_nqn(struct device *dev, 2599 struct device_attribute *attr, 2600 char *buf) 2601 { 2602 struct nvme_subsystem *subsys = 2603 container_of(dev, struct nvme_subsystem, dev); 2604 2605 return sysfs_emit(buf, "%s\n", subsys->subnqn); 2606 } 2607 static SUBSYS_ATTR_RO(subsysnqn, S_IRUGO, nvme_subsys_show_nqn); 2608 2609 static ssize_t nvme_subsys_show_type(struct device *dev, 2610 struct device_attribute *attr, 2611 char *buf) 2612 { 2613 struct nvme_subsystem *subsys = 2614 container_of(dev, struct nvme_subsystem, dev); 2615 2616 switch (subsys->subtype) { 2617 case NVME_NQN_DISC: 2618 return sysfs_emit(buf, "discovery\n"); 2619 case NVME_NQN_NVME: 2620 return sysfs_emit(buf, "nvm\n"); 2621 default: 2622 return sysfs_emit(buf, "reserved\n"); 2623 } 2624 } 2625 static SUBSYS_ATTR_RO(subsystype, S_IRUGO, nvme_subsys_show_type); 2626 2627 #define nvme_subsys_show_str_function(field) \ 2628 static ssize_t subsys_##field##_show(struct device *dev, \ 2629 struct device_attribute *attr, char *buf) \ 2630 { \ 2631 struct nvme_subsystem *subsys = \ 2632 container_of(dev, struct nvme_subsystem, dev); \ 2633 return sysfs_emit(buf, "%.*s\n", \ 2634 (int)sizeof(subsys->field), subsys->field); \ 2635 } \ 2636 static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show); 2637 2638 nvme_subsys_show_str_function(model); 2639 nvme_subsys_show_str_function(serial); 2640 nvme_subsys_show_str_function(firmware_rev); 2641 2642 static struct attribute *nvme_subsys_attrs[] = { 2643 &subsys_attr_model.attr, 2644 &subsys_attr_serial.attr, 2645 &subsys_attr_firmware_rev.attr, 2646 &subsys_attr_subsysnqn.attr, 2647 &subsys_attr_subsystype.attr, 2648 #ifdef CONFIG_NVME_MULTIPATH 2649 &subsys_attr_iopolicy.attr, 2650 #endif 2651 NULL, 2652 }; 2653 2654 static const struct attribute_group nvme_subsys_attrs_group = { 2655 .attrs = nvme_subsys_attrs, 2656 }; 2657 2658 static const struct attribute_group *nvme_subsys_attrs_groups[] = { 2659 &nvme_subsys_attrs_group, 2660 NULL, 2661 }; 2662 2663 static inline bool nvme_discovery_ctrl(struct nvme_ctrl *ctrl) 2664 { 2665 return ctrl->opts && ctrl->opts->discovery_nqn; 2666 } 2667 2668 static bool nvme_validate_cntlid(struct nvme_subsystem *subsys, 2669 struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) 2670 { 2671 struct nvme_ctrl *tmp; 2672 2673 lockdep_assert_held(&nvme_subsystems_lock); 2674 2675 list_for_each_entry(tmp, &subsys->ctrls, subsys_entry) { 2676 if (nvme_state_terminal(tmp)) 2677 continue; 2678 2679 if (tmp->cntlid == ctrl->cntlid) { 2680 dev_err(ctrl->device, 2681 "Duplicate cntlid %u with %s, subsys %s, rejecting\n", 2682 ctrl->cntlid, dev_name(tmp->device), 2683 subsys->subnqn); 2684 return false; 2685 } 2686 2687 if ((id->cmic & NVME_CTRL_CMIC_MULTI_CTRL) || 2688 nvme_discovery_ctrl(ctrl)) 2689 continue; 2690 2691 dev_err(ctrl->device, 2692 "Subsystem does not support multiple controllers\n"); 2693 return false; 2694 } 2695 2696 return true; 2697 } 2698 2699 static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) 2700 { 2701 struct nvme_subsystem *subsys, *found; 2702 int ret; 2703 2704 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL); 2705 if (!subsys) 2706 return -ENOMEM; 2707 2708 subsys->instance = -1; 2709 mutex_init(&subsys->lock); 2710 kref_init(&subsys->ref); 2711 INIT_LIST_HEAD(&subsys->ctrls); 2712 INIT_LIST_HEAD(&subsys->nsheads); 2713 nvme_init_subnqn(subsys, ctrl, id); 2714 memcpy(subsys->serial, id->sn, sizeof(subsys->serial)); 2715 memcpy(subsys->model, id->mn, sizeof(subsys->model)); 2716 memcpy(subsys->firmware_rev, id->fr, sizeof(subsys->firmware_rev)); 2717 subsys->vendor_id = le16_to_cpu(id->vid); 2718 subsys->cmic = id->cmic; 2719 2720 /* Versions prior to 1.4 don't necessarily report a valid type */ 2721 if (id->cntrltype == NVME_CTRL_DISC || 2722 !strcmp(subsys->subnqn, NVME_DISC_SUBSYS_NAME)) 2723 subsys->subtype = NVME_NQN_DISC; 2724 else 2725 subsys->subtype = NVME_NQN_NVME; 2726 2727 if (nvme_discovery_ctrl(ctrl) && subsys->subtype != NVME_NQN_DISC) { 2728 dev_err(ctrl->device, 2729 "Subsystem %s is not a discovery controller", 2730 subsys->subnqn); 2731 kfree(subsys); 2732 return -EINVAL; 2733 } 2734 subsys->awupf = le16_to_cpu(id->awupf); 2735 nvme_mpath_default_iopolicy(subsys); 2736 2737 subsys->dev.class = nvme_subsys_class; 2738 subsys->dev.release = nvme_release_subsystem; 2739 subsys->dev.groups = nvme_subsys_attrs_groups; 2740 dev_set_name(&subsys->dev, "nvme-subsys%d", ctrl->instance); 2741 device_initialize(&subsys->dev); 2742 2743 mutex_lock(&nvme_subsystems_lock); 2744 found = __nvme_find_get_subsystem(subsys->subnqn); 2745 if (found) { 2746 put_device(&subsys->dev); 2747 subsys = found; 2748 2749 if (!nvme_validate_cntlid(subsys, ctrl, id)) { 2750 ret = -EINVAL; 2751 goto out_put_subsystem; 2752 } 2753 } else { 2754 ret = device_add(&subsys->dev); 2755 if (ret) { 2756 dev_err(ctrl->device, 2757 "failed to register subsystem device.\n"); 2758 put_device(&subsys->dev); 2759 goto out_unlock; 2760 } 2761 ida_init(&subsys->ns_ida); 2762 list_add_tail(&subsys->entry, &nvme_subsystems); 2763 } 2764 2765 ret = sysfs_create_link(&subsys->dev.kobj, &ctrl->device->kobj, 2766 dev_name(ctrl->device)); 2767 if (ret) { 2768 dev_err(ctrl->device, 2769 "failed to create sysfs link from subsystem.\n"); 2770 goto out_put_subsystem; 2771 } 2772 2773 if (!found) 2774 subsys->instance = ctrl->instance; 2775 ctrl->subsys = subsys; 2776 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls); 2777 mutex_unlock(&nvme_subsystems_lock); 2778 return 0; 2779 2780 out_put_subsystem: 2781 nvme_put_subsystem(subsys); 2782 out_unlock: 2783 mutex_unlock(&nvme_subsystems_lock); 2784 return ret; 2785 } 2786 2787 int nvme_get_log(struct nvme_ctrl *ctrl, u32 nsid, u8 log_page, u8 lsp, u8 csi, 2788 void *log, size_t size, u64 offset) 2789 { 2790 struct nvme_command c = { }; 2791 u32 dwlen = nvme_bytes_to_numd(size); 2792 2793 c.get_log_page.opcode = nvme_admin_get_log_page; 2794 c.get_log_page.nsid = cpu_to_le32(nsid); 2795 c.get_log_page.lid = log_page; 2796 c.get_log_page.lsp = lsp; 2797 c.get_log_page.numdl = cpu_to_le16(dwlen & ((1 << 16) - 1)); 2798 c.get_log_page.numdu = cpu_to_le16(dwlen >> 16); 2799 c.get_log_page.lpol = cpu_to_le32(lower_32_bits(offset)); 2800 c.get_log_page.lpou = cpu_to_le32(upper_32_bits(offset)); 2801 c.get_log_page.csi = csi; 2802 2803 return nvme_submit_sync_cmd(ctrl->admin_q, &c, log, size); 2804 } 2805 2806 static int nvme_get_effects_log(struct nvme_ctrl *ctrl, u8 csi, 2807 struct nvme_effects_log **log) 2808 { 2809 struct nvme_effects_log *cel = xa_load(&ctrl->cels, csi); 2810 int ret; 2811 2812 if (cel) 2813 goto out; 2814 2815 cel = kzalloc(sizeof(*cel), GFP_KERNEL); 2816 if (!cel) 2817 return -ENOMEM; 2818 2819 ret = nvme_get_log(ctrl, 0x00, NVME_LOG_CMD_EFFECTS, 0, csi, 2820 cel, sizeof(*cel), 0); 2821 if (ret) { 2822 kfree(cel); 2823 return ret; 2824 } 2825 2826 xa_store(&ctrl->cels, csi, cel, GFP_KERNEL); 2827 out: 2828 *log = cel; 2829 return 0; 2830 } 2831 2832 static inline u32 nvme_mps_to_sectors(struct nvme_ctrl *ctrl, u32 units) 2833 { 2834 u32 page_shift = NVME_CAP_MPSMIN(ctrl->cap) + 12, val; 2835 2836 if (check_shl_overflow(1U, units + page_shift - 9, &val)) 2837 return UINT_MAX; 2838 return val; 2839 } 2840 2841 static int nvme_init_non_mdts_limits(struct nvme_ctrl *ctrl) 2842 { 2843 struct nvme_command c = { }; 2844 struct nvme_id_ctrl_nvm *id; 2845 int ret; 2846 2847 if (ctrl->oncs & NVME_CTRL_ONCS_DSM) { 2848 ctrl->max_discard_sectors = UINT_MAX; 2849 ctrl->max_discard_segments = NVME_DSM_MAX_RANGES; 2850 } else { 2851 ctrl->max_discard_sectors = 0; 2852 ctrl->max_discard_segments = 0; 2853 } 2854 2855 /* 2856 * Even though NVMe spec explicitly states that MDTS is not applicable 2857 * to the write-zeroes, we are cautious and limit the size to the 2858 * controllers max_hw_sectors value, which is based on the MDTS field 2859 * and possibly other limiting factors. 2860 */ 2861 if ((ctrl->oncs & NVME_CTRL_ONCS_WRITE_ZEROES) && 2862 !(ctrl->quirks & NVME_QUIRK_DISABLE_WRITE_ZEROES)) 2863 ctrl->max_zeroes_sectors = ctrl->max_hw_sectors; 2864 else 2865 ctrl->max_zeroes_sectors = 0; 2866 2867 if (nvme_ctrl_limited_cns(ctrl)) 2868 return 0; 2869 2870 id = kzalloc(sizeof(*id), GFP_KERNEL); 2871 if (!id) 2872 return 0; 2873 2874 c.identify.opcode = nvme_admin_identify; 2875 c.identify.cns = NVME_ID_CNS_CS_CTRL; 2876 c.identify.csi = NVME_CSI_NVM; 2877 2878 ret = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id)); 2879 if (ret) 2880 goto free_data; 2881 2882 if (id->dmrl) 2883 ctrl->max_discard_segments = id->dmrl; 2884 if (id->dmrsl) 2885 ctrl->max_discard_sectors = le32_to_cpu(id->dmrsl); 2886 if (id->wzsl) 2887 ctrl->max_zeroes_sectors = nvme_mps_to_sectors(ctrl, id->wzsl); 2888 2889 free_data: 2890 kfree(id); 2891 return ret; 2892 } 2893 2894 static int nvme_init_identify(struct nvme_ctrl *ctrl) 2895 { 2896 struct nvme_id_ctrl *id; 2897 u32 max_hw_sectors; 2898 bool prev_apst_enabled; 2899 int ret; 2900 2901 ret = nvme_identify_ctrl(ctrl, &id); 2902 if (ret) { 2903 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret); 2904 return -EIO; 2905 } 2906 2907 if (id->lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG) { 2908 ret = nvme_get_effects_log(ctrl, NVME_CSI_NVM, &ctrl->effects); 2909 if (ret < 0) 2910 goto out_free; 2911 } 2912 2913 if (!(ctrl->ops->flags & NVME_F_FABRICS)) 2914 ctrl->cntlid = le16_to_cpu(id->cntlid); 2915 2916 if (!ctrl->identified) { 2917 unsigned int i; 2918 2919 ret = nvme_init_subsystem(ctrl, id); 2920 if (ret) 2921 goto out_free; 2922 2923 /* 2924 * Check for quirks. Quirk can depend on firmware version, 2925 * so, in principle, the set of quirks present can change 2926 * across a reset. As a possible future enhancement, we 2927 * could re-scan for quirks every time we reinitialize 2928 * the device, but we'd have to make sure that the driver 2929 * behaves intelligently if the quirks change. 2930 */ 2931 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) { 2932 if (quirk_matches(id, &core_quirks[i])) 2933 ctrl->quirks |= core_quirks[i].quirks; 2934 } 2935 } 2936 2937 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) { 2938 dev_warn(ctrl->device, "forcibly allowing all power states due to nvme_core.force_apst -- use at your own risk\n"); 2939 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS; 2940 } 2941 2942 ctrl->crdt[0] = le16_to_cpu(id->crdt1); 2943 ctrl->crdt[1] = le16_to_cpu(id->crdt2); 2944 ctrl->crdt[2] = le16_to_cpu(id->crdt3); 2945 2946 ctrl->oacs = le16_to_cpu(id->oacs); 2947 ctrl->oncs = le16_to_cpu(id->oncs); 2948 ctrl->mtfa = le16_to_cpu(id->mtfa); 2949 ctrl->oaes = le32_to_cpu(id->oaes); 2950 ctrl->wctemp = le16_to_cpu(id->wctemp); 2951 ctrl->cctemp = le16_to_cpu(id->cctemp); 2952 2953 atomic_set(&ctrl->abort_limit, id->acl + 1); 2954 ctrl->vwc = id->vwc; 2955 if (id->mdts) 2956 max_hw_sectors = nvme_mps_to_sectors(ctrl, id->mdts); 2957 else 2958 max_hw_sectors = UINT_MAX; 2959 ctrl->max_hw_sectors = 2960 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors); 2961 2962 nvme_set_queue_limits(ctrl, ctrl->admin_q); 2963 ctrl->sgls = le32_to_cpu(id->sgls); 2964 ctrl->kas = le16_to_cpu(id->kas); 2965 ctrl->max_namespaces = le32_to_cpu(id->mnan); 2966 ctrl->ctratt = le32_to_cpu(id->ctratt); 2967 2968 ctrl->cntrltype = id->cntrltype; 2969 ctrl->dctype = id->dctype; 2970 2971 if (id->rtd3e) { 2972 /* us -> s */ 2973 u32 transition_time = le32_to_cpu(id->rtd3e) / USEC_PER_SEC; 2974 2975 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time, 2976 shutdown_timeout, 60); 2977 2978 if (ctrl->shutdown_timeout != shutdown_timeout) 2979 dev_info(ctrl->device, 2980 "Shutdown timeout set to %u seconds\n", 2981 ctrl->shutdown_timeout); 2982 } else 2983 ctrl->shutdown_timeout = shutdown_timeout; 2984 2985 ctrl->npss = id->npss; 2986 ctrl->apsta = id->apsta; 2987 prev_apst_enabled = ctrl->apst_enabled; 2988 if (ctrl->quirks & NVME_QUIRK_NO_APST) { 2989 if (force_apst && id->apsta) { 2990 dev_warn(ctrl->device, "forcibly allowing APST due to nvme_core.force_apst -- use at your own risk\n"); 2991 ctrl->apst_enabled = true; 2992 } else { 2993 ctrl->apst_enabled = false; 2994 } 2995 } else { 2996 ctrl->apst_enabled = id->apsta; 2997 } 2998 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd)); 2999 3000 if (ctrl->ops->flags & NVME_F_FABRICS) { 3001 ctrl->icdoff = le16_to_cpu(id->icdoff); 3002 ctrl->ioccsz = le32_to_cpu(id->ioccsz); 3003 ctrl->iorcsz = le32_to_cpu(id->iorcsz); 3004 ctrl->maxcmd = le16_to_cpu(id->maxcmd); 3005 3006 /* 3007 * In fabrics we need to verify the cntlid matches the 3008 * admin connect 3009 */ 3010 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) { 3011 dev_err(ctrl->device, 3012 "Mismatching cntlid: Connect %u vs Identify " 3013 "%u, rejecting\n", 3014 ctrl->cntlid, le16_to_cpu(id->cntlid)); 3015 ret = -EINVAL; 3016 goto out_free; 3017 } 3018 3019 if (!nvme_discovery_ctrl(ctrl) && !ctrl->kas) { 3020 dev_err(ctrl->device, 3021 "keep-alive support is mandatory for fabrics\n"); 3022 ret = -EINVAL; 3023 goto out_free; 3024 } 3025 } else { 3026 ctrl->hmpre = le32_to_cpu(id->hmpre); 3027 ctrl->hmmin = le32_to_cpu(id->hmmin); 3028 ctrl->hmminds = le32_to_cpu(id->hmminds); 3029 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd); 3030 } 3031 3032 ret = nvme_mpath_init_identify(ctrl, id); 3033 if (ret < 0) 3034 goto out_free; 3035 3036 if (ctrl->apst_enabled && !prev_apst_enabled) 3037 dev_pm_qos_expose_latency_tolerance(ctrl->device); 3038 else if (!ctrl->apst_enabled && prev_apst_enabled) 3039 dev_pm_qos_hide_latency_tolerance(ctrl->device); 3040 3041 out_free: 3042 kfree(id); 3043 return ret; 3044 } 3045 3046 /* 3047 * Initialize the cached copies of the Identify data and various controller 3048 * register in our nvme_ctrl structure. This should be called as soon as 3049 * the admin queue is fully up and running. 3050 */ 3051 int nvme_init_ctrl_finish(struct nvme_ctrl *ctrl) 3052 { 3053 int ret; 3054 3055 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs); 3056 if (ret) { 3057 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret); 3058 return ret; 3059 } 3060 3061 ctrl->sqsize = min_t(u16, NVME_CAP_MQES(ctrl->cap), ctrl->sqsize); 3062 3063 if (ctrl->vs >= NVME_VS(1, 1, 0)) 3064 ctrl->subsystem = NVME_CAP_NSSRC(ctrl->cap); 3065 3066 ret = nvme_init_identify(ctrl); 3067 if (ret) 3068 return ret; 3069 3070 ret = nvme_init_non_mdts_limits(ctrl); 3071 if (ret < 0) 3072 return ret; 3073 3074 ret = nvme_configure_apst(ctrl); 3075 if (ret < 0) 3076 return ret; 3077 3078 ret = nvme_configure_timestamp(ctrl); 3079 if (ret < 0) 3080 return ret; 3081 3082 ret = nvme_configure_host_options(ctrl); 3083 if (ret < 0) 3084 return ret; 3085 3086 if (!ctrl->identified && !nvme_discovery_ctrl(ctrl)) { 3087 ret = nvme_hwmon_init(ctrl); 3088 if (ret < 0) 3089 return ret; 3090 } 3091 3092 ctrl->identified = true; 3093 3094 return 0; 3095 } 3096 EXPORT_SYMBOL_GPL(nvme_init_ctrl_finish); 3097 3098 static int nvme_dev_open(struct inode *inode, struct file *file) 3099 { 3100 struct nvme_ctrl *ctrl = 3101 container_of(inode->i_cdev, struct nvme_ctrl, cdev); 3102 3103 switch (ctrl->state) { 3104 case NVME_CTRL_LIVE: 3105 break; 3106 default: 3107 return -EWOULDBLOCK; 3108 } 3109 3110 nvme_get_ctrl(ctrl); 3111 if (!try_module_get(ctrl->ops->module)) { 3112 nvme_put_ctrl(ctrl); 3113 return -EINVAL; 3114 } 3115 3116 file->private_data = ctrl; 3117 return 0; 3118 } 3119 3120 static int nvme_dev_release(struct inode *inode, struct file *file) 3121 { 3122 struct nvme_ctrl *ctrl = 3123 container_of(inode->i_cdev, struct nvme_ctrl, cdev); 3124 3125 module_put(ctrl->ops->module); 3126 nvme_put_ctrl(ctrl); 3127 return 0; 3128 } 3129 3130 static const struct file_operations nvme_dev_fops = { 3131 .owner = THIS_MODULE, 3132 .open = nvme_dev_open, 3133 .release = nvme_dev_release, 3134 .unlocked_ioctl = nvme_dev_ioctl, 3135 .compat_ioctl = compat_ptr_ioctl, 3136 }; 3137 3138 static ssize_t nvme_sysfs_reset(struct device *dev, 3139 struct device_attribute *attr, const char *buf, 3140 size_t count) 3141 { 3142 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3143 int ret; 3144 3145 ret = nvme_reset_ctrl_sync(ctrl); 3146 if (ret < 0) 3147 return ret; 3148 return count; 3149 } 3150 static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset); 3151 3152 static ssize_t nvme_sysfs_rescan(struct device *dev, 3153 struct device_attribute *attr, const char *buf, 3154 size_t count) 3155 { 3156 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3157 3158 nvme_queue_scan(ctrl); 3159 return count; 3160 } 3161 static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan); 3162 3163 static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev) 3164 { 3165 struct gendisk *disk = dev_to_disk(dev); 3166 3167 if (disk->fops == &nvme_bdev_ops) 3168 return nvme_get_ns_from_dev(dev)->head; 3169 else 3170 return disk->private_data; 3171 } 3172 3173 static ssize_t wwid_show(struct device *dev, struct device_attribute *attr, 3174 char *buf) 3175 { 3176 struct nvme_ns_head *head = dev_to_ns_head(dev); 3177 struct nvme_ns_ids *ids = &head->ids; 3178 struct nvme_subsystem *subsys = head->subsys; 3179 int serial_len = sizeof(subsys->serial); 3180 int model_len = sizeof(subsys->model); 3181 3182 if (!uuid_is_null(&ids->uuid)) 3183 return sysfs_emit(buf, "uuid.%pU\n", &ids->uuid); 3184 3185 if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid))) 3186 return sysfs_emit(buf, "eui.%16phN\n", ids->nguid); 3187 3188 if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64))) 3189 return sysfs_emit(buf, "eui.%8phN\n", ids->eui64); 3190 3191 while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' || 3192 subsys->serial[serial_len - 1] == '\0')) 3193 serial_len--; 3194 while (model_len > 0 && (subsys->model[model_len - 1] == ' ' || 3195 subsys->model[model_len - 1] == '\0')) 3196 model_len--; 3197 3198 return sysfs_emit(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id, 3199 serial_len, subsys->serial, model_len, subsys->model, 3200 head->ns_id); 3201 } 3202 static DEVICE_ATTR_RO(wwid); 3203 3204 static ssize_t nguid_show(struct device *dev, struct device_attribute *attr, 3205 char *buf) 3206 { 3207 return sysfs_emit(buf, "%pU\n", dev_to_ns_head(dev)->ids.nguid); 3208 } 3209 static DEVICE_ATTR_RO(nguid); 3210 3211 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, 3212 char *buf) 3213 { 3214 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids; 3215 3216 /* For backward compatibility expose the NGUID to userspace if 3217 * we have no UUID set 3218 */ 3219 if (uuid_is_null(&ids->uuid)) { 3220 printk_ratelimited(KERN_WARNING 3221 "No UUID available providing old NGUID\n"); 3222 return sysfs_emit(buf, "%pU\n", ids->nguid); 3223 } 3224 return sysfs_emit(buf, "%pU\n", &ids->uuid); 3225 } 3226 static DEVICE_ATTR_RO(uuid); 3227 3228 static ssize_t eui_show(struct device *dev, struct device_attribute *attr, 3229 char *buf) 3230 { 3231 return sysfs_emit(buf, "%8ph\n", dev_to_ns_head(dev)->ids.eui64); 3232 } 3233 static DEVICE_ATTR_RO(eui); 3234 3235 static ssize_t nsid_show(struct device *dev, struct device_attribute *attr, 3236 char *buf) 3237 { 3238 return sysfs_emit(buf, "%d\n", dev_to_ns_head(dev)->ns_id); 3239 } 3240 static DEVICE_ATTR_RO(nsid); 3241 3242 static struct attribute *nvme_ns_id_attrs[] = { 3243 &dev_attr_wwid.attr, 3244 &dev_attr_uuid.attr, 3245 &dev_attr_nguid.attr, 3246 &dev_attr_eui.attr, 3247 &dev_attr_nsid.attr, 3248 #ifdef CONFIG_NVME_MULTIPATH 3249 &dev_attr_ana_grpid.attr, 3250 &dev_attr_ana_state.attr, 3251 #endif 3252 NULL, 3253 }; 3254 3255 static umode_t nvme_ns_id_attrs_are_visible(struct kobject *kobj, 3256 struct attribute *a, int n) 3257 { 3258 struct device *dev = container_of(kobj, struct device, kobj); 3259 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids; 3260 3261 if (a == &dev_attr_uuid.attr) { 3262 if (uuid_is_null(&ids->uuid) && 3263 !memchr_inv(ids->nguid, 0, sizeof(ids->nguid))) 3264 return 0; 3265 } 3266 if (a == &dev_attr_nguid.attr) { 3267 if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid))) 3268 return 0; 3269 } 3270 if (a == &dev_attr_eui.attr) { 3271 if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64))) 3272 return 0; 3273 } 3274 #ifdef CONFIG_NVME_MULTIPATH 3275 if (a == &dev_attr_ana_grpid.attr || a == &dev_attr_ana_state.attr) { 3276 if (dev_to_disk(dev)->fops != &nvme_bdev_ops) /* per-path attr */ 3277 return 0; 3278 if (!nvme_ctrl_use_ana(nvme_get_ns_from_dev(dev)->ctrl)) 3279 return 0; 3280 } 3281 #endif 3282 return a->mode; 3283 } 3284 3285 static const struct attribute_group nvme_ns_id_attr_group = { 3286 .attrs = nvme_ns_id_attrs, 3287 .is_visible = nvme_ns_id_attrs_are_visible, 3288 }; 3289 3290 const struct attribute_group *nvme_ns_id_attr_groups[] = { 3291 &nvme_ns_id_attr_group, 3292 NULL, 3293 }; 3294 3295 #define nvme_show_str_function(field) \ 3296 static ssize_t field##_show(struct device *dev, \ 3297 struct device_attribute *attr, char *buf) \ 3298 { \ 3299 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ 3300 return sysfs_emit(buf, "%.*s\n", \ 3301 (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \ 3302 } \ 3303 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); 3304 3305 nvme_show_str_function(model); 3306 nvme_show_str_function(serial); 3307 nvme_show_str_function(firmware_rev); 3308 3309 #define nvme_show_int_function(field) \ 3310 static ssize_t field##_show(struct device *dev, \ 3311 struct device_attribute *attr, char *buf) \ 3312 { \ 3313 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ 3314 return sysfs_emit(buf, "%d\n", ctrl->field); \ 3315 } \ 3316 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL); 3317 3318 nvme_show_int_function(cntlid); 3319 nvme_show_int_function(numa_node); 3320 nvme_show_int_function(queue_count); 3321 nvme_show_int_function(sqsize); 3322 nvme_show_int_function(kato); 3323 3324 static ssize_t nvme_sysfs_delete(struct device *dev, 3325 struct device_attribute *attr, const char *buf, 3326 size_t count) 3327 { 3328 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3329 3330 if (device_remove_file_self(dev, attr)) 3331 nvme_delete_ctrl_sync(ctrl); 3332 return count; 3333 } 3334 static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete); 3335 3336 static ssize_t nvme_sysfs_show_transport(struct device *dev, 3337 struct device_attribute *attr, 3338 char *buf) 3339 { 3340 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3341 3342 return sysfs_emit(buf, "%s\n", ctrl->ops->name); 3343 } 3344 static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL); 3345 3346 static ssize_t nvme_sysfs_show_state(struct device *dev, 3347 struct device_attribute *attr, 3348 char *buf) 3349 { 3350 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3351 static const char *const state_name[] = { 3352 [NVME_CTRL_NEW] = "new", 3353 [NVME_CTRL_LIVE] = "live", 3354 [NVME_CTRL_RESETTING] = "resetting", 3355 [NVME_CTRL_CONNECTING] = "connecting", 3356 [NVME_CTRL_DELETING] = "deleting", 3357 [NVME_CTRL_DELETING_NOIO]= "deleting (no IO)", 3358 [NVME_CTRL_DEAD] = "dead", 3359 }; 3360 3361 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) && 3362 state_name[ctrl->state]) 3363 return sysfs_emit(buf, "%s\n", state_name[ctrl->state]); 3364 3365 return sysfs_emit(buf, "unknown state\n"); 3366 } 3367 3368 static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL); 3369 3370 static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev, 3371 struct device_attribute *attr, 3372 char *buf) 3373 { 3374 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3375 3376 return sysfs_emit(buf, "%s\n", ctrl->subsys->subnqn); 3377 } 3378 static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL); 3379 3380 static ssize_t nvme_sysfs_show_hostnqn(struct device *dev, 3381 struct device_attribute *attr, 3382 char *buf) 3383 { 3384 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3385 3386 return sysfs_emit(buf, "%s\n", ctrl->opts->host->nqn); 3387 } 3388 static DEVICE_ATTR(hostnqn, S_IRUGO, nvme_sysfs_show_hostnqn, NULL); 3389 3390 static ssize_t nvme_sysfs_show_hostid(struct device *dev, 3391 struct device_attribute *attr, 3392 char *buf) 3393 { 3394 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3395 3396 return sysfs_emit(buf, "%pU\n", &ctrl->opts->host->id); 3397 } 3398 static DEVICE_ATTR(hostid, S_IRUGO, nvme_sysfs_show_hostid, NULL); 3399 3400 static ssize_t nvme_sysfs_show_address(struct device *dev, 3401 struct device_attribute *attr, 3402 char *buf) 3403 { 3404 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3405 3406 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE); 3407 } 3408 static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL); 3409 3410 static ssize_t nvme_ctrl_loss_tmo_show(struct device *dev, 3411 struct device_attribute *attr, char *buf) 3412 { 3413 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3414 struct nvmf_ctrl_options *opts = ctrl->opts; 3415 3416 if (ctrl->opts->max_reconnects == -1) 3417 return sysfs_emit(buf, "off\n"); 3418 return sysfs_emit(buf, "%d\n", 3419 opts->max_reconnects * opts->reconnect_delay); 3420 } 3421 3422 static ssize_t nvme_ctrl_loss_tmo_store(struct device *dev, 3423 struct device_attribute *attr, const char *buf, size_t count) 3424 { 3425 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3426 struct nvmf_ctrl_options *opts = ctrl->opts; 3427 int ctrl_loss_tmo, err; 3428 3429 err = kstrtoint(buf, 10, &ctrl_loss_tmo); 3430 if (err) 3431 return -EINVAL; 3432 3433 if (ctrl_loss_tmo < 0) 3434 opts->max_reconnects = -1; 3435 else 3436 opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo, 3437 opts->reconnect_delay); 3438 return count; 3439 } 3440 static DEVICE_ATTR(ctrl_loss_tmo, S_IRUGO | S_IWUSR, 3441 nvme_ctrl_loss_tmo_show, nvme_ctrl_loss_tmo_store); 3442 3443 static ssize_t nvme_ctrl_reconnect_delay_show(struct device *dev, 3444 struct device_attribute *attr, char *buf) 3445 { 3446 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3447 3448 if (ctrl->opts->reconnect_delay == -1) 3449 return sysfs_emit(buf, "off\n"); 3450 return sysfs_emit(buf, "%d\n", ctrl->opts->reconnect_delay); 3451 } 3452 3453 static ssize_t nvme_ctrl_reconnect_delay_store(struct device *dev, 3454 struct device_attribute *attr, const char *buf, size_t count) 3455 { 3456 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3457 unsigned int v; 3458 int err; 3459 3460 err = kstrtou32(buf, 10, &v); 3461 if (err) 3462 return err; 3463 3464 ctrl->opts->reconnect_delay = v; 3465 return count; 3466 } 3467 static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, 3468 nvme_ctrl_reconnect_delay_show, nvme_ctrl_reconnect_delay_store); 3469 3470 static ssize_t nvme_ctrl_fast_io_fail_tmo_show(struct device *dev, 3471 struct device_attribute *attr, char *buf) 3472 { 3473 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3474 3475 if (ctrl->opts->fast_io_fail_tmo == -1) 3476 return sysfs_emit(buf, "off\n"); 3477 return sysfs_emit(buf, "%d\n", ctrl->opts->fast_io_fail_tmo); 3478 } 3479 3480 static ssize_t nvme_ctrl_fast_io_fail_tmo_store(struct device *dev, 3481 struct device_attribute *attr, const char *buf, size_t count) 3482 { 3483 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3484 struct nvmf_ctrl_options *opts = ctrl->opts; 3485 int fast_io_fail_tmo, err; 3486 3487 err = kstrtoint(buf, 10, &fast_io_fail_tmo); 3488 if (err) 3489 return -EINVAL; 3490 3491 if (fast_io_fail_tmo < 0) 3492 opts->fast_io_fail_tmo = -1; 3493 else 3494 opts->fast_io_fail_tmo = fast_io_fail_tmo; 3495 return count; 3496 } 3497 static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR, 3498 nvme_ctrl_fast_io_fail_tmo_show, nvme_ctrl_fast_io_fail_tmo_store); 3499 3500 static ssize_t cntrltype_show(struct device *dev, 3501 struct device_attribute *attr, char *buf) 3502 { 3503 static const char * const type[] = { 3504 [NVME_CTRL_IO] = "io\n", 3505 [NVME_CTRL_DISC] = "discovery\n", 3506 [NVME_CTRL_ADMIN] = "admin\n", 3507 }; 3508 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3509 3510 if (ctrl->cntrltype > NVME_CTRL_ADMIN || !type[ctrl->cntrltype]) 3511 return sysfs_emit(buf, "reserved\n"); 3512 3513 return sysfs_emit(buf, type[ctrl->cntrltype]); 3514 } 3515 static DEVICE_ATTR_RO(cntrltype); 3516 3517 static ssize_t dctype_show(struct device *dev, 3518 struct device_attribute *attr, char *buf) 3519 { 3520 static const char * const type[] = { 3521 [NVME_DCTYPE_NOT_REPORTED] = "none\n", 3522 [NVME_DCTYPE_DDC] = "ddc\n", 3523 [NVME_DCTYPE_CDC] = "cdc\n", 3524 }; 3525 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3526 3527 if (ctrl->dctype > NVME_DCTYPE_CDC || !type[ctrl->dctype]) 3528 return sysfs_emit(buf, "reserved\n"); 3529 3530 return sysfs_emit(buf, type[ctrl->dctype]); 3531 } 3532 static DEVICE_ATTR_RO(dctype); 3533 3534 static struct attribute *nvme_dev_attrs[] = { 3535 &dev_attr_reset_controller.attr, 3536 &dev_attr_rescan_controller.attr, 3537 &dev_attr_model.attr, 3538 &dev_attr_serial.attr, 3539 &dev_attr_firmware_rev.attr, 3540 &dev_attr_cntlid.attr, 3541 &dev_attr_delete_controller.attr, 3542 &dev_attr_transport.attr, 3543 &dev_attr_subsysnqn.attr, 3544 &dev_attr_address.attr, 3545 &dev_attr_state.attr, 3546 &dev_attr_numa_node.attr, 3547 &dev_attr_queue_count.attr, 3548 &dev_attr_sqsize.attr, 3549 &dev_attr_hostnqn.attr, 3550 &dev_attr_hostid.attr, 3551 &dev_attr_ctrl_loss_tmo.attr, 3552 &dev_attr_reconnect_delay.attr, 3553 &dev_attr_fast_io_fail_tmo.attr, 3554 &dev_attr_kato.attr, 3555 &dev_attr_cntrltype.attr, 3556 &dev_attr_dctype.attr, 3557 NULL 3558 }; 3559 3560 static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj, 3561 struct attribute *a, int n) 3562 { 3563 struct device *dev = container_of(kobj, struct device, kobj); 3564 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); 3565 3566 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl) 3567 return 0; 3568 if (a == &dev_attr_address.attr && !ctrl->ops->get_address) 3569 return 0; 3570 if (a == &dev_attr_hostnqn.attr && !ctrl->opts) 3571 return 0; 3572 if (a == &dev_attr_hostid.attr && !ctrl->opts) 3573 return 0; 3574 if (a == &dev_attr_ctrl_loss_tmo.attr && !ctrl->opts) 3575 return 0; 3576 if (a == &dev_attr_reconnect_delay.attr && !ctrl->opts) 3577 return 0; 3578 if (a == &dev_attr_fast_io_fail_tmo.attr && !ctrl->opts) 3579 return 0; 3580 3581 return a->mode; 3582 } 3583 3584 static const struct attribute_group nvme_dev_attrs_group = { 3585 .attrs = nvme_dev_attrs, 3586 .is_visible = nvme_dev_attrs_are_visible, 3587 }; 3588 3589 static const struct attribute_group *nvme_dev_attr_groups[] = { 3590 &nvme_dev_attrs_group, 3591 NULL, 3592 }; 3593 3594 static struct nvme_ns_head *nvme_find_ns_head(struct nvme_ctrl *ctrl, 3595 unsigned nsid) 3596 { 3597 struct nvme_ns_head *h; 3598 3599 lockdep_assert_held(&ctrl->subsys->lock); 3600 3601 list_for_each_entry(h, &ctrl->subsys->nsheads, entry) { 3602 /* 3603 * Private namespaces can share NSIDs under some conditions. 3604 * In that case we can't use the same ns_head for namespaces 3605 * with the same NSID. 3606 */ 3607 if (h->ns_id != nsid || !nvme_is_unique_nsid(ctrl, h)) 3608 continue; 3609 if (!list_empty(&h->list) && nvme_tryget_ns_head(h)) 3610 return h; 3611 } 3612 3613 return NULL; 3614 } 3615 3616 static int nvme_subsys_check_duplicate_ids(struct nvme_subsystem *subsys, 3617 struct nvme_ns_ids *ids) 3618 { 3619 bool has_uuid = !uuid_is_null(&ids->uuid); 3620 bool has_nguid = memchr_inv(ids->nguid, 0, sizeof(ids->nguid)); 3621 bool has_eui64 = memchr_inv(ids->eui64, 0, sizeof(ids->eui64)); 3622 struct nvme_ns_head *h; 3623 3624 lockdep_assert_held(&subsys->lock); 3625 3626 list_for_each_entry(h, &subsys->nsheads, entry) { 3627 if (has_uuid && uuid_equal(&ids->uuid, &h->ids.uuid)) 3628 return -EINVAL; 3629 if (has_nguid && 3630 memcmp(&ids->nguid, &h->ids.nguid, sizeof(ids->nguid)) == 0) 3631 return -EINVAL; 3632 if (has_eui64 && 3633 memcmp(&ids->eui64, &h->ids.eui64, sizeof(ids->eui64)) == 0) 3634 return -EINVAL; 3635 } 3636 3637 return 0; 3638 } 3639 3640 static void nvme_cdev_rel(struct device *dev) 3641 { 3642 ida_free(&nvme_ns_chr_minor_ida, MINOR(dev->devt)); 3643 } 3644 3645 void nvme_cdev_del(struct cdev *cdev, struct device *cdev_device) 3646 { 3647 cdev_device_del(cdev, cdev_device); 3648 put_device(cdev_device); 3649 } 3650 3651 int nvme_cdev_add(struct cdev *cdev, struct device *cdev_device, 3652 const struct file_operations *fops, struct module *owner) 3653 { 3654 int minor, ret; 3655 3656 minor = ida_alloc(&nvme_ns_chr_minor_ida, GFP_KERNEL); 3657 if (minor < 0) 3658 return minor; 3659 cdev_device->devt = MKDEV(MAJOR(nvme_ns_chr_devt), minor); 3660 cdev_device->class = nvme_ns_chr_class; 3661 cdev_device->release = nvme_cdev_rel; 3662 device_initialize(cdev_device); 3663 cdev_init(cdev, fops); 3664 cdev->owner = owner; 3665 ret = cdev_device_add(cdev, cdev_device); 3666 if (ret) 3667 put_device(cdev_device); 3668 3669 return ret; 3670 } 3671 3672 static int nvme_ns_chr_open(struct inode *inode, struct file *file) 3673 { 3674 return nvme_ns_open(container_of(inode->i_cdev, struct nvme_ns, cdev)); 3675 } 3676 3677 static int nvme_ns_chr_release(struct inode *inode, struct file *file) 3678 { 3679 nvme_ns_release(container_of(inode->i_cdev, struct nvme_ns, cdev)); 3680 return 0; 3681 } 3682 3683 static const struct file_operations nvme_ns_chr_fops = { 3684 .owner = THIS_MODULE, 3685 .open = nvme_ns_chr_open, 3686 .release = nvme_ns_chr_release, 3687 .unlocked_ioctl = nvme_ns_chr_ioctl, 3688 .compat_ioctl = compat_ptr_ioctl, 3689 }; 3690 3691 static int nvme_add_ns_cdev(struct nvme_ns *ns) 3692 { 3693 int ret; 3694 3695 ns->cdev_device.parent = ns->ctrl->device; 3696 ret = dev_set_name(&ns->cdev_device, "ng%dn%d", 3697 ns->ctrl->instance, ns->head->instance); 3698 if (ret) 3699 return ret; 3700 3701 return nvme_cdev_add(&ns->cdev, &ns->cdev_device, &nvme_ns_chr_fops, 3702 ns->ctrl->ops->module); 3703 } 3704 3705 static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl, 3706 unsigned nsid, struct nvme_ns_ids *ids) 3707 { 3708 struct nvme_ns_head *head; 3709 size_t size = sizeof(*head); 3710 int ret = -ENOMEM; 3711 3712 #ifdef CONFIG_NVME_MULTIPATH 3713 size += num_possible_nodes() * sizeof(struct nvme_ns *); 3714 #endif 3715 3716 head = kzalloc(size, GFP_KERNEL); 3717 if (!head) 3718 goto out; 3719 ret = ida_alloc_min(&ctrl->subsys->ns_ida, 1, GFP_KERNEL); 3720 if (ret < 0) 3721 goto out_free_head; 3722 head->instance = ret; 3723 INIT_LIST_HEAD(&head->list); 3724 ret = init_srcu_struct(&head->srcu); 3725 if (ret) 3726 goto out_ida_remove; 3727 head->subsys = ctrl->subsys; 3728 head->ns_id = nsid; 3729 head->ids = *ids; 3730 kref_init(&head->ref); 3731 3732 if (head->ids.csi) { 3733 ret = nvme_get_effects_log(ctrl, head->ids.csi, &head->effects); 3734 if (ret) 3735 goto out_cleanup_srcu; 3736 } else 3737 head->effects = ctrl->effects; 3738 3739 ret = nvme_mpath_alloc_disk(ctrl, head); 3740 if (ret) 3741 goto out_cleanup_srcu; 3742 3743 list_add_tail(&head->entry, &ctrl->subsys->nsheads); 3744 3745 kref_get(&ctrl->subsys->ref); 3746 3747 return head; 3748 out_cleanup_srcu: 3749 cleanup_srcu_struct(&head->srcu); 3750 out_ida_remove: 3751 ida_free(&ctrl->subsys->ns_ida, head->instance); 3752 out_free_head: 3753 kfree(head); 3754 out: 3755 if (ret > 0) 3756 ret = blk_status_to_errno(nvme_error_status(ret)); 3757 return ERR_PTR(ret); 3758 } 3759 3760 static int nvme_global_check_duplicate_ids(struct nvme_subsystem *this, 3761 struct nvme_ns_ids *ids) 3762 { 3763 struct nvme_subsystem *s; 3764 int ret = 0; 3765 3766 /* 3767 * Note that this check is racy as we try to avoid holding the global 3768 * lock over the whole ns_head creation. But it is only intended as 3769 * a sanity check anyway. 3770 */ 3771 mutex_lock(&nvme_subsystems_lock); 3772 list_for_each_entry(s, &nvme_subsystems, entry) { 3773 if (s == this) 3774 continue; 3775 mutex_lock(&s->lock); 3776 ret = nvme_subsys_check_duplicate_ids(s, ids); 3777 mutex_unlock(&s->lock); 3778 if (ret) 3779 break; 3780 } 3781 mutex_unlock(&nvme_subsystems_lock); 3782 3783 return ret; 3784 } 3785 3786 static int nvme_init_ns_head(struct nvme_ns *ns, unsigned nsid, 3787 struct nvme_ns_ids *ids, bool is_shared) 3788 { 3789 struct nvme_ctrl *ctrl = ns->ctrl; 3790 struct nvme_ns_head *head = NULL; 3791 int ret; 3792 3793 ret = nvme_global_check_duplicate_ids(ctrl->subsys, ids); 3794 if (ret) { 3795 dev_err(ctrl->device, 3796 "globally duplicate IDs for nsid %d\n", nsid); 3797 return ret; 3798 } 3799 3800 mutex_lock(&ctrl->subsys->lock); 3801 head = nvme_find_ns_head(ctrl, nsid); 3802 if (!head) { 3803 ret = nvme_subsys_check_duplicate_ids(ctrl->subsys, ids); 3804 if (ret) { 3805 dev_err(ctrl->device, 3806 "duplicate IDs in subsystem for nsid %d\n", 3807 nsid); 3808 goto out_unlock; 3809 } 3810 head = nvme_alloc_ns_head(ctrl, nsid, ids); 3811 if (IS_ERR(head)) { 3812 ret = PTR_ERR(head); 3813 goto out_unlock; 3814 } 3815 head->shared = is_shared; 3816 } else { 3817 ret = -EINVAL; 3818 if (!is_shared || !head->shared) { 3819 dev_err(ctrl->device, 3820 "Duplicate unshared namespace %d\n", nsid); 3821 goto out_put_ns_head; 3822 } 3823 if (!nvme_ns_ids_equal(&head->ids, ids)) { 3824 dev_err(ctrl->device, 3825 "IDs don't match for shared namespace %d\n", 3826 nsid); 3827 goto out_put_ns_head; 3828 } 3829 3830 if (!multipath && !list_empty(&head->list)) { 3831 dev_warn(ctrl->device, 3832 "Found shared namespace %d, but multipathing not supported.\n", 3833 nsid); 3834 dev_warn_once(ctrl->device, 3835 "Support for shared namespaces without CONFIG_NVME_MULTIPATH is deprecated and will be removed in Linux 6.0\n."); 3836 } 3837 } 3838 3839 list_add_tail_rcu(&ns->siblings, &head->list); 3840 ns->head = head; 3841 mutex_unlock(&ctrl->subsys->lock); 3842 return 0; 3843 3844 out_put_ns_head: 3845 nvme_put_ns_head(head); 3846 out_unlock: 3847 mutex_unlock(&ctrl->subsys->lock); 3848 return ret; 3849 } 3850 3851 struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid) 3852 { 3853 struct nvme_ns *ns, *ret = NULL; 3854 3855 down_read(&ctrl->namespaces_rwsem); 3856 list_for_each_entry(ns, &ctrl->namespaces, list) { 3857 if (ns->head->ns_id == nsid) { 3858 if (!nvme_get_ns(ns)) 3859 continue; 3860 ret = ns; 3861 break; 3862 } 3863 if (ns->head->ns_id > nsid) 3864 break; 3865 } 3866 up_read(&ctrl->namespaces_rwsem); 3867 return ret; 3868 } 3869 EXPORT_SYMBOL_NS_GPL(nvme_find_get_ns, NVME_TARGET_PASSTHRU); 3870 3871 /* 3872 * Add the namespace to the controller list while keeping the list ordered. 3873 */ 3874 static void nvme_ns_add_to_ctrl_list(struct nvme_ns *ns) 3875 { 3876 struct nvme_ns *tmp; 3877 3878 list_for_each_entry_reverse(tmp, &ns->ctrl->namespaces, list) { 3879 if (tmp->head->ns_id < ns->head->ns_id) { 3880 list_add(&ns->list, &tmp->list); 3881 return; 3882 } 3883 } 3884 list_add(&ns->list, &ns->ctrl->namespaces); 3885 } 3886 3887 static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid, 3888 struct nvme_ns_ids *ids) 3889 { 3890 struct nvme_ns *ns; 3891 struct gendisk *disk; 3892 struct nvme_id_ns *id; 3893 int node = ctrl->numa_node; 3894 3895 if (nvme_identify_ns(ctrl, nsid, ids, &id)) 3896 return; 3897 3898 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node); 3899 if (!ns) 3900 goto out_free_id; 3901 3902 disk = blk_mq_alloc_disk(ctrl->tagset, ns); 3903 if (IS_ERR(disk)) 3904 goto out_free_ns; 3905 disk->fops = &nvme_bdev_ops; 3906 disk->private_data = ns; 3907 3908 ns->disk = disk; 3909 ns->queue = disk->queue; 3910 3911 if (ctrl->opts && ctrl->opts->data_digest) 3912 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, ns->queue); 3913 3914 blk_queue_flag_set(QUEUE_FLAG_NONROT, ns->queue); 3915 if (ctrl->ops->flags & NVME_F_PCI_P2PDMA) 3916 blk_queue_flag_set(QUEUE_FLAG_PCI_P2PDMA, ns->queue); 3917 3918 ns->ctrl = ctrl; 3919 kref_init(&ns->kref); 3920 3921 if (nvme_init_ns_head(ns, nsid, ids, id->nmic & NVME_NS_NMIC_SHARED)) 3922 goto out_cleanup_disk; 3923 3924 /* 3925 * If multipathing is enabled, the device name for all disks and not 3926 * just those that represent shared namespaces needs to be based on the 3927 * subsystem instance. Using the controller instance for private 3928 * namespaces could lead to naming collisions between shared and private 3929 * namespaces if they don't use a common numbering scheme. 3930 * 3931 * If multipathing is not enabled, disk names must use the controller 3932 * instance as shared namespaces will show up as multiple block 3933 * devices. 3934 */ 3935 if (ns->head->disk) { 3936 sprintf(disk->disk_name, "nvme%dc%dn%d", ctrl->subsys->instance, 3937 ctrl->instance, ns->head->instance); 3938 disk->flags |= GENHD_FL_HIDDEN; 3939 } else if (multipath) { 3940 sprintf(disk->disk_name, "nvme%dn%d", ctrl->subsys->instance, 3941 ns->head->instance); 3942 } else { 3943 sprintf(disk->disk_name, "nvme%dn%d", ctrl->instance, 3944 ns->head->instance); 3945 } 3946 3947 if (nvme_update_ns_info(ns, id)) 3948 goto out_unlink_ns; 3949 3950 down_write(&ctrl->namespaces_rwsem); 3951 nvme_ns_add_to_ctrl_list(ns); 3952 up_write(&ctrl->namespaces_rwsem); 3953 nvme_get_ctrl(ctrl); 3954 3955 if (device_add_disk(ctrl->device, ns->disk, nvme_ns_id_attr_groups)) 3956 goto out_cleanup_ns_from_list; 3957 3958 if (!nvme_ns_head_multipath(ns->head)) 3959 nvme_add_ns_cdev(ns); 3960 3961 nvme_mpath_add_disk(ns, id); 3962 nvme_fault_inject_init(&ns->fault_inject, ns->disk->disk_name); 3963 kfree(id); 3964 3965 return; 3966 3967 out_cleanup_ns_from_list: 3968 nvme_put_ctrl(ctrl); 3969 down_write(&ctrl->namespaces_rwsem); 3970 list_del_init(&ns->list); 3971 up_write(&ctrl->namespaces_rwsem); 3972 out_unlink_ns: 3973 mutex_lock(&ctrl->subsys->lock); 3974 list_del_rcu(&ns->siblings); 3975 if (list_empty(&ns->head->list)) 3976 list_del_init(&ns->head->entry); 3977 mutex_unlock(&ctrl->subsys->lock); 3978 nvme_put_ns_head(ns->head); 3979 out_cleanup_disk: 3980 blk_cleanup_disk(disk); 3981 out_free_ns: 3982 kfree(ns); 3983 out_free_id: 3984 kfree(id); 3985 } 3986 3987 static void nvme_ns_remove(struct nvme_ns *ns) 3988 { 3989 bool last_path = false; 3990 3991 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags)) 3992 return; 3993 3994 clear_bit(NVME_NS_READY, &ns->flags); 3995 set_capacity(ns->disk, 0); 3996 nvme_fault_inject_fini(&ns->fault_inject); 3997 3998 /* 3999 * Ensure that !NVME_NS_READY is seen by other threads to prevent 4000 * this ns going back into current_path. 4001 */ 4002 synchronize_srcu(&ns->head->srcu); 4003 4004 /* wait for concurrent submissions */ 4005 if (nvme_mpath_clear_current_path(ns)) 4006 synchronize_srcu(&ns->head->srcu); 4007 4008 mutex_lock(&ns->ctrl->subsys->lock); 4009 list_del_rcu(&ns->siblings); 4010 if (list_empty(&ns->head->list)) { 4011 list_del_init(&ns->head->entry); 4012 last_path = true; 4013 } 4014 mutex_unlock(&ns->ctrl->subsys->lock); 4015 4016 /* guarantee not available in head->list */ 4017 synchronize_rcu(); 4018 4019 if (!nvme_ns_head_multipath(ns->head)) 4020 nvme_cdev_del(&ns->cdev, &ns->cdev_device); 4021 del_gendisk(ns->disk); 4022 blk_cleanup_queue(ns->queue); 4023 4024 down_write(&ns->ctrl->namespaces_rwsem); 4025 list_del_init(&ns->list); 4026 up_write(&ns->ctrl->namespaces_rwsem); 4027 4028 if (last_path) 4029 nvme_mpath_shutdown_disk(ns->head); 4030 nvme_put_ns(ns); 4031 } 4032 4033 static void nvme_ns_remove_by_nsid(struct nvme_ctrl *ctrl, u32 nsid) 4034 { 4035 struct nvme_ns *ns = nvme_find_get_ns(ctrl, nsid); 4036 4037 if (ns) { 4038 nvme_ns_remove(ns); 4039 nvme_put_ns(ns); 4040 } 4041 } 4042 4043 static void nvme_validate_ns(struct nvme_ns *ns, struct nvme_ns_ids *ids) 4044 { 4045 struct nvme_id_ns *id; 4046 int ret = NVME_SC_INVALID_NS | NVME_SC_DNR; 4047 4048 if (test_bit(NVME_NS_DEAD, &ns->flags)) 4049 goto out; 4050 4051 ret = nvme_identify_ns(ns->ctrl, ns->head->ns_id, ids, &id); 4052 if (ret) 4053 goto out; 4054 4055 ret = NVME_SC_INVALID_NS | NVME_SC_DNR; 4056 if (!nvme_ns_ids_equal(&ns->head->ids, ids)) { 4057 dev_err(ns->ctrl->device, 4058 "identifiers changed for nsid %d\n", ns->head->ns_id); 4059 goto out_free_id; 4060 } 4061 4062 ret = nvme_update_ns_info(ns, id); 4063 4064 out_free_id: 4065 kfree(id); 4066 out: 4067 /* 4068 * Only remove the namespace if we got a fatal error back from the 4069 * device, otherwise ignore the error and just move on. 4070 * 4071 * TODO: we should probably schedule a delayed retry here. 4072 */ 4073 if (ret > 0 && (ret & NVME_SC_DNR)) 4074 nvme_ns_remove(ns); 4075 } 4076 4077 static void nvme_validate_or_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid) 4078 { 4079 struct nvme_ns_ids ids = { }; 4080 struct nvme_ns *ns; 4081 4082 if (nvme_identify_ns_descs(ctrl, nsid, &ids)) 4083 return; 4084 4085 ns = nvme_find_get_ns(ctrl, nsid); 4086 if (ns) { 4087 nvme_validate_ns(ns, &ids); 4088 nvme_put_ns(ns); 4089 return; 4090 } 4091 4092 switch (ids.csi) { 4093 case NVME_CSI_NVM: 4094 nvme_alloc_ns(ctrl, nsid, &ids); 4095 break; 4096 case NVME_CSI_ZNS: 4097 if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED)) { 4098 dev_warn(ctrl->device, 4099 "nsid %u not supported without CONFIG_BLK_DEV_ZONED\n", 4100 nsid); 4101 break; 4102 } 4103 if (!nvme_multi_css(ctrl)) { 4104 dev_warn(ctrl->device, 4105 "command set not reported for nsid: %d\n", 4106 nsid); 4107 break; 4108 } 4109 nvme_alloc_ns(ctrl, nsid, &ids); 4110 break; 4111 default: 4112 dev_warn(ctrl->device, "unknown csi %u for nsid %u\n", 4113 ids.csi, nsid); 4114 break; 4115 } 4116 } 4117 4118 static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl, 4119 unsigned nsid) 4120 { 4121 struct nvme_ns *ns, *next; 4122 LIST_HEAD(rm_list); 4123 4124 down_write(&ctrl->namespaces_rwsem); 4125 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) { 4126 if (ns->head->ns_id > nsid || test_bit(NVME_NS_DEAD, &ns->flags)) 4127 list_move_tail(&ns->list, &rm_list); 4128 } 4129 up_write(&ctrl->namespaces_rwsem); 4130 4131 list_for_each_entry_safe(ns, next, &rm_list, list) 4132 nvme_ns_remove(ns); 4133 4134 } 4135 4136 static int nvme_scan_ns_list(struct nvme_ctrl *ctrl) 4137 { 4138 const int nr_entries = NVME_IDENTIFY_DATA_SIZE / sizeof(__le32); 4139 __le32 *ns_list; 4140 u32 prev = 0; 4141 int ret = 0, i; 4142 4143 if (nvme_ctrl_limited_cns(ctrl)) 4144 return -EOPNOTSUPP; 4145 4146 ns_list = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL); 4147 if (!ns_list) 4148 return -ENOMEM; 4149 4150 for (;;) { 4151 struct nvme_command cmd = { 4152 .identify.opcode = nvme_admin_identify, 4153 .identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST, 4154 .identify.nsid = cpu_to_le32(prev), 4155 }; 4156 4157 ret = nvme_submit_sync_cmd(ctrl->admin_q, &cmd, ns_list, 4158 NVME_IDENTIFY_DATA_SIZE); 4159 if (ret) { 4160 dev_warn(ctrl->device, 4161 "Identify NS List failed (status=0x%x)\n", ret); 4162 goto free; 4163 } 4164 4165 for (i = 0; i < nr_entries; i++) { 4166 u32 nsid = le32_to_cpu(ns_list[i]); 4167 4168 if (!nsid) /* end of the list? */ 4169 goto out; 4170 nvme_validate_or_alloc_ns(ctrl, nsid); 4171 while (++prev < nsid) 4172 nvme_ns_remove_by_nsid(ctrl, prev); 4173 } 4174 } 4175 out: 4176 nvme_remove_invalid_namespaces(ctrl, prev); 4177 free: 4178 kfree(ns_list); 4179 return ret; 4180 } 4181 4182 static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl) 4183 { 4184 struct nvme_id_ctrl *id; 4185 u32 nn, i; 4186 4187 if (nvme_identify_ctrl(ctrl, &id)) 4188 return; 4189 nn = le32_to_cpu(id->nn); 4190 kfree(id); 4191 4192 for (i = 1; i <= nn; i++) 4193 nvme_validate_or_alloc_ns(ctrl, i); 4194 4195 nvme_remove_invalid_namespaces(ctrl, nn); 4196 } 4197 4198 static void nvme_clear_changed_ns_log(struct nvme_ctrl *ctrl) 4199 { 4200 size_t log_size = NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32); 4201 __le32 *log; 4202 int error; 4203 4204 log = kzalloc(log_size, GFP_KERNEL); 4205 if (!log) 4206 return; 4207 4208 /* 4209 * We need to read the log to clear the AEN, but we don't want to rely 4210 * on it for the changed namespace information as userspace could have 4211 * raced with us in reading the log page, which could cause us to miss 4212 * updates. 4213 */ 4214 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_CHANGED_NS, 0, 4215 NVME_CSI_NVM, log, log_size, 0); 4216 if (error) 4217 dev_warn(ctrl->device, 4218 "reading changed ns log failed: %d\n", error); 4219 4220 kfree(log); 4221 } 4222 4223 static void nvme_scan_work(struct work_struct *work) 4224 { 4225 struct nvme_ctrl *ctrl = 4226 container_of(work, struct nvme_ctrl, scan_work); 4227 4228 /* No tagset on a live ctrl means IO queues could not created */ 4229 if (ctrl->state != NVME_CTRL_LIVE || !ctrl->tagset) 4230 return; 4231 4232 if (test_and_clear_bit(NVME_AER_NOTICE_NS_CHANGED, &ctrl->events)) { 4233 dev_info(ctrl->device, "rescanning namespaces.\n"); 4234 nvme_clear_changed_ns_log(ctrl); 4235 } 4236 4237 mutex_lock(&ctrl->scan_lock); 4238 if (nvme_scan_ns_list(ctrl) != 0) 4239 nvme_scan_ns_sequential(ctrl); 4240 mutex_unlock(&ctrl->scan_lock); 4241 } 4242 4243 /* 4244 * This function iterates the namespace list unlocked to allow recovery from 4245 * controller failure. It is up to the caller to ensure the namespace list is 4246 * not modified by scan work while this function is executing. 4247 */ 4248 void nvme_remove_namespaces(struct nvme_ctrl *ctrl) 4249 { 4250 struct nvme_ns *ns, *next; 4251 LIST_HEAD(ns_list); 4252 4253 /* 4254 * make sure to requeue I/O to all namespaces as these 4255 * might result from the scan itself and must complete 4256 * for the scan_work to make progress 4257 */ 4258 nvme_mpath_clear_ctrl_paths(ctrl); 4259 4260 /* prevent racing with ns scanning */ 4261 flush_work(&ctrl->scan_work); 4262 4263 /* 4264 * The dead states indicates the controller was not gracefully 4265 * disconnected. In that case, we won't be able to flush any data while 4266 * removing the namespaces' disks; fail all the queues now to avoid 4267 * potentially having to clean up the failed sync later. 4268 */ 4269 if (ctrl->state == NVME_CTRL_DEAD) 4270 nvme_kill_queues(ctrl); 4271 4272 /* this is a no-op when called from the controller reset handler */ 4273 nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING_NOIO); 4274 4275 down_write(&ctrl->namespaces_rwsem); 4276 list_splice_init(&ctrl->namespaces, &ns_list); 4277 up_write(&ctrl->namespaces_rwsem); 4278 4279 list_for_each_entry_safe(ns, next, &ns_list, list) 4280 nvme_ns_remove(ns); 4281 } 4282 EXPORT_SYMBOL_GPL(nvme_remove_namespaces); 4283 4284 static int nvme_class_uevent(struct device *dev, struct kobj_uevent_env *env) 4285 { 4286 struct nvme_ctrl *ctrl = 4287 container_of(dev, struct nvme_ctrl, ctrl_device); 4288 struct nvmf_ctrl_options *opts = ctrl->opts; 4289 int ret; 4290 4291 ret = add_uevent_var(env, "NVME_TRTYPE=%s", ctrl->ops->name); 4292 if (ret) 4293 return ret; 4294 4295 if (opts) { 4296 ret = add_uevent_var(env, "NVME_TRADDR=%s", opts->traddr); 4297 if (ret) 4298 return ret; 4299 4300 ret = add_uevent_var(env, "NVME_TRSVCID=%s", 4301 opts->trsvcid ?: "none"); 4302 if (ret) 4303 return ret; 4304 4305 ret = add_uevent_var(env, "NVME_HOST_TRADDR=%s", 4306 opts->host_traddr ?: "none"); 4307 if (ret) 4308 return ret; 4309 4310 ret = add_uevent_var(env, "NVME_HOST_IFACE=%s", 4311 opts->host_iface ?: "none"); 4312 } 4313 return ret; 4314 } 4315 4316 static void nvme_change_uevent(struct nvme_ctrl *ctrl, char *envdata) 4317 { 4318 char *envp[2] = { envdata, NULL }; 4319 4320 kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp); 4321 } 4322 4323 static void nvme_aen_uevent(struct nvme_ctrl *ctrl) 4324 { 4325 char *envp[2] = { NULL, NULL }; 4326 u32 aen_result = ctrl->aen_result; 4327 4328 ctrl->aen_result = 0; 4329 if (!aen_result) 4330 return; 4331 4332 envp[0] = kasprintf(GFP_KERNEL, "NVME_AEN=%#08x", aen_result); 4333 if (!envp[0]) 4334 return; 4335 kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp); 4336 kfree(envp[0]); 4337 } 4338 4339 static void nvme_async_event_work(struct work_struct *work) 4340 { 4341 struct nvme_ctrl *ctrl = 4342 container_of(work, struct nvme_ctrl, async_event_work); 4343 4344 nvme_aen_uevent(ctrl); 4345 4346 /* 4347 * The transport drivers must guarantee AER submission here is safe by 4348 * flushing ctrl async_event_work after changing the controller state 4349 * from LIVE and before freeing the admin queue. 4350 */ 4351 if (ctrl->state == NVME_CTRL_LIVE) 4352 ctrl->ops->submit_async_event(ctrl); 4353 } 4354 4355 static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl) 4356 { 4357 4358 u32 csts; 4359 4360 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) 4361 return false; 4362 4363 if (csts == ~0) 4364 return false; 4365 4366 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP)); 4367 } 4368 4369 static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl) 4370 { 4371 struct nvme_fw_slot_info_log *log; 4372 4373 log = kmalloc(sizeof(*log), GFP_KERNEL); 4374 if (!log) 4375 return; 4376 4377 if (nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_FW_SLOT, 0, NVME_CSI_NVM, 4378 log, sizeof(*log), 0)) 4379 dev_warn(ctrl->device, "Get FW SLOT INFO log error\n"); 4380 kfree(log); 4381 } 4382 4383 static void nvme_fw_act_work(struct work_struct *work) 4384 { 4385 struct nvme_ctrl *ctrl = container_of(work, 4386 struct nvme_ctrl, fw_act_work); 4387 unsigned long fw_act_timeout; 4388 4389 if (ctrl->mtfa) 4390 fw_act_timeout = jiffies + 4391 msecs_to_jiffies(ctrl->mtfa * 100); 4392 else 4393 fw_act_timeout = jiffies + 4394 msecs_to_jiffies(admin_timeout * 1000); 4395 4396 nvme_stop_queues(ctrl); 4397 while (nvme_ctrl_pp_status(ctrl)) { 4398 if (time_after(jiffies, fw_act_timeout)) { 4399 dev_warn(ctrl->device, 4400 "Fw activation timeout, reset controller\n"); 4401 nvme_try_sched_reset(ctrl); 4402 return; 4403 } 4404 msleep(100); 4405 } 4406 4407 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) 4408 return; 4409 4410 nvme_start_queues(ctrl); 4411 /* read FW slot information to clear the AER */ 4412 nvme_get_fw_slot_info(ctrl); 4413 } 4414 4415 static void nvme_handle_aen_notice(struct nvme_ctrl *ctrl, u32 result) 4416 { 4417 u32 aer_notice_type = (result & 0xff00) >> 8; 4418 4419 trace_nvme_async_event(ctrl, aer_notice_type); 4420 4421 switch (aer_notice_type) { 4422 case NVME_AER_NOTICE_NS_CHANGED: 4423 set_bit(NVME_AER_NOTICE_NS_CHANGED, &ctrl->events); 4424 nvme_queue_scan(ctrl); 4425 break; 4426 case NVME_AER_NOTICE_FW_ACT_STARTING: 4427 /* 4428 * We are (ab)using the RESETTING state to prevent subsequent 4429 * recovery actions from interfering with the controller's 4430 * firmware activation. 4431 */ 4432 if (nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING)) 4433 queue_work(nvme_wq, &ctrl->fw_act_work); 4434 break; 4435 #ifdef CONFIG_NVME_MULTIPATH 4436 case NVME_AER_NOTICE_ANA: 4437 if (!ctrl->ana_log_buf) 4438 break; 4439 queue_work(nvme_wq, &ctrl->ana_work); 4440 break; 4441 #endif 4442 case NVME_AER_NOTICE_DISC_CHANGED: 4443 ctrl->aen_result = result; 4444 break; 4445 default: 4446 dev_warn(ctrl->device, "async event result %08x\n", result); 4447 } 4448 } 4449 4450 void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status, 4451 volatile union nvme_result *res) 4452 { 4453 u32 result = le32_to_cpu(res->u32); 4454 u32 aer_type = result & 0x07; 4455 4456 if (le16_to_cpu(status) >> 1 != NVME_SC_SUCCESS) 4457 return; 4458 4459 switch (aer_type) { 4460 case NVME_AER_NOTICE: 4461 nvme_handle_aen_notice(ctrl, result); 4462 break; 4463 case NVME_AER_ERROR: 4464 case NVME_AER_SMART: 4465 case NVME_AER_CSS: 4466 case NVME_AER_VS: 4467 trace_nvme_async_event(ctrl, aer_type); 4468 ctrl->aen_result = result; 4469 break; 4470 default: 4471 break; 4472 } 4473 queue_work(nvme_wq, &ctrl->async_event_work); 4474 } 4475 EXPORT_SYMBOL_GPL(nvme_complete_async_event); 4476 4477 void nvme_stop_ctrl(struct nvme_ctrl *ctrl) 4478 { 4479 nvme_mpath_stop(ctrl); 4480 nvme_stop_keep_alive(ctrl); 4481 nvme_stop_failfast_work(ctrl); 4482 flush_work(&ctrl->async_event_work); 4483 cancel_work_sync(&ctrl->fw_act_work); 4484 } 4485 EXPORT_SYMBOL_GPL(nvme_stop_ctrl); 4486 4487 void nvme_start_ctrl(struct nvme_ctrl *ctrl) 4488 { 4489 nvme_start_keep_alive(ctrl); 4490 4491 nvme_enable_aen(ctrl); 4492 4493 if (ctrl->queue_count > 1) { 4494 nvme_queue_scan(ctrl); 4495 nvme_start_queues(ctrl); 4496 nvme_mpath_update(ctrl); 4497 } 4498 4499 nvme_change_uevent(ctrl, "NVME_EVENT=connected"); 4500 } 4501 EXPORT_SYMBOL_GPL(nvme_start_ctrl); 4502 4503 void nvme_uninit_ctrl(struct nvme_ctrl *ctrl) 4504 { 4505 nvme_hwmon_exit(ctrl); 4506 nvme_fault_inject_fini(&ctrl->fault_inject); 4507 dev_pm_qos_hide_latency_tolerance(ctrl->device); 4508 cdev_device_del(&ctrl->cdev, ctrl->device); 4509 nvme_put_ctrl(ctrl); 4510 } 4511 EXPORT_SYMBOL_GPL(nvme_uninit_ctrl); 4512 4513 static void nvme_free_cels(struct nvme_ctrl *ctrl) 4514 { 4515 struct nvme_effects_log *cel; 4516 unsigned long i; 4517 4518 xa_for_each(&ctrl->cels, i, cel) { 4519 xa_erase(&ctrl->cels, i); 4520 kfree(cel); 4521 } 4522 4523 xa_destroy(&ctrl->cels); 4524 } 4525 4526 static void nvme_free_ctrl(struct device *dev) 4527 { 4528 struct nvme_ctrl *ctrl = 4529 container_of(dev, struct nvme_ctrl, ctrl_device); 4530 struct nvme_subsystem *subsys = ctrl->subsys; 4531 4532 if (!subsys || ctrl->instance != subsys->instance) 4533 ida_free(&nvme_instance_ida, ctrl->instance); 4534 4535 nvme_free_cels(ctrl); 4536 nvme_mpath_uninit(ctrl); 4537 __free_page(ctrl->discard_page); 4538 4539 if (subsys) { 4540 mutex_lock(&nvme_subsystems_lock); 4541 list_del(&ctrl->subsys_entry); 4542 sysfs_remove_link(&subsys->dev.kobj, dev_name(ctrl->device)); 4543 mutex_unlock(&nvme_subsystems_lock); 4544 } 4545 4546 ctrl->ops->free_ctrl(ctrl); 4547 4548 if (subsys) 4549 nvme_put_subsystem(subsys); 4550 } 4551 4552 /* 4553 * Initialize a NVMe controller structures. This needs to be called during 4554 * earliest initialization so that we have the initialized structured around 4555 * during probing. 4556 */ 4557 int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev, 4558 const struct nvme_ctrl_ops *ops, unsigned long quirks) 4559 { 4560 int ret; 4561 4562 ctrl->state = NVME_CTRL_NEW; 4563 clear_bit(NVME_CTRL_FAILFAST_EXPIRED, &ctrl->flags); 4564 spin_lock_init(&ctrl->lock); 4565 mutex_init(&ctrl->scan_lock); 4566 INIT_LIST_HEAD(&ctrl->namespaces); 4567 xa_init(&ctrl->cels); 4568 init_rwsem(&ctrl->namespaces_rwsem); 4569 ctrl->dev = dev; 4570 ctrl->ops = ops; 4571 ctrl->quirks = quirks; 4572 ctrl->numa_node = NUMA_NO_NODE; 4573 INIT_WORK(&ctrl->scan_work, nvme_scan_work); 4574 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work); 4575 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work); 4576 INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work); 4577 init_waitqueue_head(&ctrl->state_wq); 4578 4579 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work); 4580 INIT_DELAYED_WORK(&ctrl->failfast_work, nvme_failfast_work); 4581 memset(&ctrl->ka_cmd, 0, sizeof(ctrl->ka_cmd)); 4582 ctrl->ka_cmd.common.opcode = nvme_admin_keep_alive; 4583 4584 BUILD_BUG_ON(NVME_DSM_MAX_RANGES * sizeof(struct nvme_dsm_range) > 4585 PAGE_SIZE); 4586 ctrl->discard_page = alloc_page(GFP_KERNEL); 4587 if (!ctrl->discard_page) { 4588 ret = -ENOMEM; 4589 goto out; 4590 } 4591 4592 ret = ida_alloc(&nvme_instance_ida, GFP_KERNEL); 4593 if (ret < 0) 4594 goto out; 4595 ctrl->instance = ret; 4596 4597 device_initialize(&ctrl->ctrl_device); 4598 ctrl->device = &ctrl->ctrl_device; 4599 ctrl->device->devt = MKDEV(MAJOR(nvme_ctrl_base_chr_devt), 4600 ctrl->instance); 4601 ctrl->device->class = nvme_class; 4602 ctrl->device->parent = ctrl->dev; 4603 ctrl->device->groups = nvme_dev_attr_groups; 4604 ctrl->device->release = nvme_free_ctrl; 4605 dev_set_drvdata(ctrl->device, ctrl); 4606 ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance); 4607 if (ret) 4608 goto out_release_instance; 4609 4610 nvme_get_ctrl(ctrl); 4611 cdev_init(&ctrl->cdev, &nvme_dev_fops); 4612 ctrl->cdev.owner = ops->module; 4613 ret = cdev_device_add(&ctrl->cdev, ctrl->device); 4614 if (ret) 4615 goto out_free_name; 4616 4617 /* 4618 * Initialize latency tolerance controls. The sysfs files won't 4619 * be visible to userspace unless the device actually supports APST. 4620 */ 4621 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance; 4622 dev_pm_qos_update_user_latency_tolerance(ctrl->device, 4623 min(default_ps_max_latency_us, (unsigned long)S32_MAX)); 4624 4625 nvme_fault_inject_init(&ctrl->fault_inject, dev_name(ctrl->device)); 4626 nvme_mpath_init_ctrl(ctrl); 4627 4628 return 0; 4629 out_free_name: 4630 nvme_put_ctrl(ctrl); 4631 kfree_const(ctrl->device->kobj.name); 4632 out_release_instance: 4633 ida_free(&nvme_instance_ida, ctrl->instance); 4634 out: 4635 if (ctrl->discard_page) 4636 __free_page(ctrl->discard_page); 4637 return ret; 4638 } 4639 EXPORT_SYMBOL_GPL(nvme_init_ctrl); 4640 4641 static void nvme_start_ns_queue(struct nvme_ns *ns) 4642 { 4643 if (test_and_clear_bit(NVME_NS_STOPPED, &ns->flags)) 4644 blk_mq_unquiesce_queue(ns->queue); 4645 } 4646 4647 static void nvme_stop_ns_queue(struct nvme_ns *ns) 4648 { 4649 if (!test_and_set_bit(NVME_NS_STOPPED, &ns->flags)) 4650 blk_mq_quiesce_queue(ns->queue); 4651 else 4652 blk_mq_wait_quiesce_done(ns->queue); 4653 } 4654 4655 /* 4656 * Prepare a queue for teardown. 4657 * 4658 * This must forcibly unquiesce queues to avoid blocking dispatch, and only set 4659 * the capacity to 0 after that to avoid blocking dispatchers that may be 4660 * holding bd_butex. This will end buffered writers dirtying pages that can't 4661 * be synced. 4662 */ 4663 static void nvme_set_queue_dying(struct nvme_ns *ns) 4664 { 4665 if (test_and_set_bit(NVME_NS_DEAD, &ns->flags)) 4666 return; 4667 4668 blk_mark_disk_dead(ns->disk); 4669 nvme_start_ns_queue(ns); 4670 4671 set_capacity_and_notify(ns->disk, 0); 4672 } 4673 4674 /** 4675 * nvme_kill_queues(): Ends all namespace queues 4676 * @ctrl: the dead controller that needs to end 4677 * 4678 * Call this function when the driver determines it is unable to get the 4679 * controller in a state capable of servicing IO. 4680 */ 4681 void nvme_kill_queues(struct nvme_ctrl *ctrl) 4682 { 4683 struct nvme_ns *ns; 4684 4685 down_read(&ctrl->namespaces_rwsem); 4686 4687 /* Forcibly unquiesce queues to avoid blocking dispatch */ 4688 if (ctrl->admin_q && !blk_queue_dying(ctrl->admin_q)) 4689 nvme_start_admin_queue(ctrl); 4690 4691 list_for_each_entry(ns, &ctrl->namespaces, list) 4692 nvme_set_queue_dying(ns); 4693 4694 up_read(&ctrl->namespaces_rwsem); 4695 } 4696 EXPORT_SYMBOL_GPL(nvme_kill_queues); 4697 4698 void nvme_unfreeze(struct nvme_ctrl *ctrl) 4699 { 4700 struct nvme_ns *ns; 4701 4702 down_read(&ctrl->namespaces_rwsem); 4703 list_for_each_entry(ns, &ctrl->namespaces, list) 4704 blk_mq_unfreeze_queue(ns->queue); 4705 up_read(&ctrl->namespaces_rwsem); 4706 } 4707 EXPORT_SYMBOL_GPL(nvme_unfreeze); 4708 4709 int nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout) 4710 { 4711 struct nvme_ns *ns; 4712 4713 down_read(&ctrl->namespaces_rwsem); 4714 list_for_each_entry(ns, &ctrl->namespaces, list) { 4715 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout); 4716 if (timeout <= 0) 4717 break; 4718 } 4719 up_read(&ctrl->namespaces_rwsem); 4720 return timeout; 4721 } 4722 EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout); 4723 4724 void nvme_wait_freeze(struct nvme_ctrl *ctrl) 4725 { 4726 struct nvme_ns *ns; 4727 4728 down_read(&ctrl->namespaces_rwsem); 4729 list_for_each_entry(ns, &ctrl->namespaces, list) 4730 blk_mq_freeze_queue_wait(ns->queue); 4731 up_read(&ctrl->namespaces_rwsem); 4732 } 4733 EXPORT_SYMBOL_GPL(nvme_wait_freeze); 4734 4735 void nvme_start_freeze(struct nvme_ctrl *ctrl) 4736 { 4737 struct nvme_ns *ns; 4738 4739 down_read(&ctrl->namespaces_rwsem); 4740 list_for_each_entry(ns, &ctrl->namespaces, list) 4741 blk_freeze_queue_start(ns->queue); 4742 up_read(&ctrl->namespaces_rwsem); 4743 } 4744 EXPORT_SYMBOL_GPL(nvme_start_freeze); 4745 4746 void nvme_stop_queues(struct nvme_ctrl *ctrl) 4747 { 4748 struct nvme_ns *ns; 4749 4750 down_read(&ctrl->namespaces_rwsem); 4751 list_for_each_entry(ns, &ctrl->namespaces, list) 4752 nvme_stop_ns_queue(ns); 4753 up_read(&ctrl->namespaces_rwsem); 4754 } 4755 EXPORT_SYMBOL_GPL(nvme_stop_queues); 4756 4757 void nvme_start_queues(struct nvme_ctrl *ctrl) 4758 { 4759 struct nvme_ns *ns; 4760 4761 down_read(&ctrl->namespaces_rwsem); 4762 list_for_each_entry(ns, &ctrl->namespaces, list) 4763 nvme_start_ns_queue(ns); 4764 up_read(&ctrl->namespaces_rwsem); 4765 } 4766 EXPORT_SYMBOL_GPL(nvme_start_queues); 4767 4768 void nvme_stop_admin_queue(struct nvme_ctrl *ctrl) 4769 { 4770 if (!test_and_set_bit(NVME_CTRL_ADMIN_Q_STOPPED, &ctrl->flags)) 4771 blk_mq_quiesce_queue(ctrl->admin_q); 4772 else 4773 blk_mq_wait_quiesce_done(ctrl->admin_q); 4774 } 4775 EXPORT_SYMBOL_GPL(nvme_stop_admin_queue); 4776 4777 void nvme_start_admin_queue(struct nvme_ctrl *ctrl) 4778 { 4779 if (test_and_clear_bit(NVME_CTRL_ADMIN_Q_STOPPED, &ctrl->flags)) 4780 blk_mq_unquiesce_queue(ctrl->admin_q); 4781 } 4782 EXPORT_SYMBOL_GPL(nvme_start_admin_queue); 4783 4784 void nvme_sync_io_queues(struct nvme_ctrl *ctrl) 4785 { 4786 struct nvme_ns *ns; 4787 4788 down_read(&ctrl->namespaces_rwsem); 4789 list_for_each_entry(ns, &ctrl->namespaces, list) 4790 blk_sync_queue(ns->queue); 4791 up_read(&ctrl->namespaces_rwsem); 4792 } 4793 EXPORT_SYMBOL_GPL(nvme_sync_io_queues); 4794 4795 void nvme_sync_queues(struct nvme_ctrl *ctrl) 4796 { 4797 nvme_sync_io_queues(ctrl); 4798 if (ctrl->admin_q) 4799 blk_sync_queue(ctrl->admin_q); 4800 } 4801 EXPORT_SYMBOL_GPL(nvme_sync_queues); 4802 4803 struct nvme_ctrl *nvme_ctrl_from_file(struct file *file) 4804 { 4805 if (file->f_op != &nvme_dev_fops) 4806 return NULL; 4807 return file->private_data; 4808 } 4809 EXPORT_SYMBOL_NS_GPL(nvme_ctrl_from_file, NVME_TARGET_PASSTHRU); 4810 4811 /* 4812 * Check we didn't inadvertently grow the command structure sizes: 4813 */ 4814 static inline void _nvme_check_size(void) 4815 { 4816 BUILD_BUG_ON(sizeof(struct nvme_common_command) != 64); 4817 BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64); 4818 BUILD_BUG_ON(sizeof(struct nvme_identify) != 64); 4819 BUILD_BUG_ON(sizeof(struct nvme_features) != 64); 4820 BUILD_BUG_ON(sizeof(struct nvme_download_firmware) != 64); 4821 BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64); 4822 BUILD_BUG_ON(sizeof(struct nvme_dsm_cmd) != 64); 4823 BUILD_BUG_ON(sizeof(struct nvme_write_zeroes_cmd) != 64); 4824 BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64); 4825 BUILD_BUG_ON(sizeof(struct nvme_get_log_page_command) != 64); 4826 BUILD_BUG_ON(sizeof(struct nvme_command) != 64); 4827 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != NVME_IDENTIFY_DATA_SIZE); 4828 BUILD_BUG_ON(sizeof(struct nvme_id_ns) != NVME_IDENTIFY_DATA_SIZE); 4829 BUILD_BUG_ON(sizeof(struct nvme_id_ns_zns) != NVME_IDENTIFY_DATA_SIZE); 4830 BUILD_BUG_ON(sizeof(struct nvme_id_ns_nvm) != NVME_IDENTIFY_DATA_SIZE); 4831 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl_zns) != NVME_IDENTIFY_DATA_SIZE); 4832 BUILD_BUG_ON(sizeof(struct nvme_id_ctrl_nvm) != NVME_IDENTIFY_DATA_SIZE); 4833 BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64); 4834 BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512); 4835 BUILD_BUG_ON(sizeof(struct nvme_dbbuf) != 64); 4836 BUILD_BUG_ON(sizeof(struct nvme_directive_cmd) != 64); 4837 BUILD_BUG_ON(sizeof(struct nvme_feat_host_behavior) != 512); 4838 } 4839 4840 4841 static int __init nvme_core_init(void) 4842 { 4843 int result = -ENOMEM; 4844 4845 _nvme_check_size(); 4846 4847 nvme_wq = alloc_workqueue("nvme-wq", 4848 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0); 4849 if (!nvme_wq) 4850 goto out; 4851 4852 nvme_reset_wq = alloc_workqueue("nvme-reset-wq", 4853 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0); 4854 if (!nvme_reset_wq) 4855 goto destroy_wq; 4856 4857 nvme_delete_wq = alloc_workqueue("nvme-delete-wq", 4858 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0); 4859 if (!nvme_delete_wq) 4860 goto destroy_reset_wq; 4861 4862 result = alloc_chrdev_region(&nvme_ctrl_base_chr_devt, 0, 4863 NVME_MINORS, "nvme"); 4864 if (result < 0) 4865 goto destroy_delete_wq; 4866 4867 nvme_class = class_create(THIS_MODULE, "nvme"); 4868 if (IS_ERR(nvme_class)) { 4869 result = PTR_ERR(nvme_class); 4870 goto unregister_chrdev; 4871 } 4872 nvme_class->dev_uevent = nvme_class_uevent; 4873 4874 nvme_subsys_class = class_create(THIS_MODULE, "nvme-subsystem"); 4875 if (IS_ERR(nvme_subsys_class)) { 4876 result = PTR_ERR(nvme_subsys_class); 4877 goto destroy_class; 4878 } 4879 4880 result = alloc_chrdev_region(&nvme_ns_chr_devt, 0, NVME_MINORS, 4881 "nvme-generic"); 4882 if (result < 0) 4883 goto destroy_subsys_class; 4884 4885 nvme_ns_chr_class = class_create(THIS_MODULE, "nvme-generic"); 4886 if (IS_ERR(nvme_ns_chr_class)) { 4887 result = PTR_ERR(nvme_ns_chr_class); 4888 goto unregister_generic_ns; 4889 } 4890 4891 return 0; 4892 4893 unregister_generic_ns: 4894 unregister_chrdev_region(nvme_ns_chr_devt, NVME_MINORS); 4895 destroy_subsys_class: 4896 class_destroy(nvme_subsys_class); 4897 destroy_class: 4898 class_destroy(nvme_class); 4899 unregister_chrdev: 4900 unregister_chrdev_region(nvme_ctrl_base_chr_devt, NVME_MINORS); 4901 destroy_delete_wq: 4902 destroy_workqueue(nvme_delete_wq); 4903 destroy_reset_wq: 4904 destroy_workqueue(nvme_reset_wq); 4905 destroy_wq: 4906 destroy_workqueue(nvme_wq); 4907 out: 4908 return result; 4909 } 4910 4911 static void __exit nvme_core_exit(void) 4912 { 4913 class_destroy(nvme_ns_chr_class); 4914 class_destroy(nvme_subsys_class); 4915 class_destroy(nvme_class); 4916 unregister_chrdev_region(nvme_ns_chr_devt, NVME_MINORS); 4917 unregister_chrdev_region(nvme_ctrl_base_chr_devt, NVME_MINORS); 4918 destroy_workqueue(nvme_delete_wq); 4919 destroy_workqueue(nvme_reset_wq); 4920 destroy_workqueue(nvme_wq); 4921 ida_destroy(&nvme_ns_chr_minor_ida); 4922 ida_destroy(&nvme_instance_ida); 4923 } 4924 4925 MODULE_LICENSE("GPL"); 4926 MODULE_VERSION("1.0"); 4927 module_init(nvme_core_init); 4928 module_exit(nvme_core_exit); 4929