1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (C) 2012-2016 Intel Corporation 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include "opt_nvme.h" 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/buf.h> 34 #include <sys/bus.h> 35 #include <sys/conf.h> 36 #include <sys/ioccom.h> 37 #include <sys/proc.h> 38 #include <sys/smp.h> 39 #include <sys/uio.h> 40 #include <sys/sbuf.h> 41 #include <sys/endian.h> 42 #include <machine/stdarg.h> 43 #include <vm/vm.h> 44 45 #include "nvme_private.h" 46 47 #define B4_CHK_RDY_DELAY_MS 2300 /* work around controller bug */ 48 49 static void nvme_ctrlr_construct_and_submit_aer(struct nvme_controller *ctrlr, 50 struct nvme_async_event_request *aer); 51 52 static void 53 nvme_ctrlr_barrier(struct nvme_controller *ctrlr, int flags) 54 { 55 bus_barrier(ctrlr->resource, 0, rman_get_size(ctrlr->resource), flags); 56 } 57 58 static void 59 nvme_ctrlr_devctl_log(struct nvme_controller *ctrlr, const char *type, const char *msg, ...) 60 { 61 struct sbuf sb; 62 va_list ap; 63 int error; 64 65 if (sbuf_new(&sb, NULL, 0, SBUF_AUTOEXTEND | SBUF_NOWAIT) == NULL) 66 return; 67 sbuf_printf(&sb, "%s: ", device_get_nameunit(ctrlr->dev)); 68 va_start(ap, msg); 69 sbuf_vprintf(&sb, msg, ap); 70 va_end(ap); 71 error = sbuf_finish(&sb); 72 if (error == 0) 73 printf("%s\n", sbuf_data(&sb)); 74 75 sbuf_clear(&sb); 76 sbuf_printf(&sb, "name=\"%s\" reason=\"", device_get_nameunit(ctrlr->dev)); 77 va_start(ap, msg); 78 sbuf_vprintf(&sb, msg, ap); 79 va_end(ap); 80 sbuf_printf(&sb, "\""); 81 error = sbuf_finish(&sb); 82 if (error == 0) 83 devctl_notify("nvme", "controller", type, sbuf_data(&sb)); 84 sbuf_delete(&sb); 85 } 86 87 static int 88 nvme_ctrlr_construct_admin_qpair(struct nvme_controller *ctrlr) 89 { 90 struct nvme_qpair *qpair; 91 uint32_t num_entries; 92 int error; 93 94 qpair = &ctrlr->adminq; 95 qpair->id = 0; 96 qpair->cpu = CPU_FFS(&cpuset_domain[ctrlr->domain]) - 1; 97 qpair->domain = ctrlr->domain; 98 99 num_entries = NVME_ADMIN_ENTRIES; 100 TUNABLE_INT_FETCH("hw.nvme.admin_entries", &num_entries); 101 /* 102 * If admin_entries was overridden to an invalid value, revert it 103 * back to our default value. 104 */ 105 if (num_entries < NVME_MIN_ADMIN_ENTRIES || 106 num_entries > NVME_MAX_ADMIN_ENTRIES) { 107 nvme_printf(ctrlr, "invalid hw.nvme.admin_entries=%d " 108 "specified\n", num_entries); 109 num_entries = NVME_ADMIN_ENTRIES; 110 } 111 112 /* 113 * The admin queue's max xfer size is treated differently than the 114 * max I/O xfer size. 16KB is sufficient here - maybe even less? 115 */ 116 error = nvme_qpair_construct(qpair, num_entries, NVME_ADMIN_TRACKERS, 117 ctrlr); 118 return (error); 119 } 120 121 #define QP(ctrlr, c) ((c) * (ctrlr)->num_io_queues / mp_ncpus) 122 123 static int 124 nvme_ctrlr_construct_io_qpairs(struct nvme_controller *ctrlr) 125 { 126 struct nvme_qpair *qpair; 127 uint32_t cap_lo; 128 uint16_t mqes; 129 int c, error, i, n; 130 int num_entries, num_trackers, max_entries; 131 132 /* 133 * NVMe spec sets a hard limit of 64K max entries, but devices may 134 * specify a smaller limit, so we need to check the MQES field in the 135 * capabilities register. We have to cap the number of entries to the 136 * current stride allows for in BAR 0/1, otherwise the remainder entries 137 * are inaccessible. MQES should reflect this, and this is just a 138 * fail-safe. 139 */ 140 max_entries = 141 (rman_get_size(ctrlr->resource) - nvme_mmio_offsetof(doorbell[0])) / 142 (1 << (ctrlr->dstrd + 1)); 143 num_entries = NVME_IO_ENTRIES; 144 TUNABLE_INT_FETCH("hw.nvme.io_entries", &num_entries); 145 cap_lo = nvme_mmio_read_4(ctrlr, cap_lo); 146 mqes = NVME_CAP_LO_MQES(cap_lo); 147 num_entries = min(num_entries, mqes + 1); 148 num_entries = min(num_entries, max_entries); 149 150 num_trackers = NVME_IO_TRACKERS; 151 TUNABLE_INT_FETCH("hw.nvme.io_trackers", &num_trackers); 152 153 num_trackers = max(num_trackers, NVME_MIN_IO_TRACKERS); 154 num_trackers = min(num_trackers, NVME_MAX_IO_TRACKERS); 155 /* 156 * No need to have more trackers than entries in the submit queue. Note 157 * also that for a queue size of N, we can only have (N-1) commands 158 * outstanding, hence the "-1" here. 159 */ 160 num_trackers = min(num_trackers, (num_entries-1)); 161 162 /* 163 * Our best estimate for the maximum number of I/Os that we should 164 * normally have in flight at one time. This should be viewed as a hint, 165 * not a hard limit and will need to be revisited when the upper layers 166 * of the storage system grows multi-queue support. 167 */ 168 ctrlr->max_hw_pend_io = num_trackers * ctrlr->num_io_queues * 3 / 4; 169 170 ctrlr->ioq = malloc(ctrlr->num_io_queues * sizeof(struct nvme_qpair), 171 M_NVME, M_ZERO | M_WAITOK); 172 173 for (i = c = n = 0; i < ctrlr->num_io_queues; i++, c += n) { 174 qpair = &ctrlr->ioq[i]; 175 176 /* 177 * Admin queue has ID=0. IO queues start at ID=1 - 178 * hence the 'i+1' here. 179 */ 180 qpair->id = i + 1; 181 if (ctrlr->num_io_queues > 1) { 182 /* Find number of CPUs served by this queue. */ 183 for (n = 1; QP(ctrlr, c + n) == i; n++) 184 ; 185 /* Shuffle multiple NVMe devices between CPUs. */ 186 qpair->cpu = c + (device_get_unit(ctrlr->dev)+n/2) % n; 187 qpair->domain = pcpu_find(qpair->cpu)->pc_domain; 188 } else { 189 qpair->cpu = CPU_FFS(&cpuset_domain[ctrlr->domain]) - 1; 190 qpair->domain = ctrlr->domain; 191 } 192 193 /* 194 * For I/O queues, use the controller-wide max_xfer_size 195 * calculated in nvme_attach(). 196 */ 197 error = nvme_qpair_construct(qpair, num_entries, num_trackers, 198 ctrlr); 199 if (error) 200 return (error); 201 202 /* 203 * Do not bother binding interrupts if we only have one I/O 204 * interrupt thread for this controller. 205 */ 206 if (ctrlr->num_io_queues > 1) 207 bus_bind_intr(ctrlr->dev, qpair->res, qpair->cpu); 208 } 209 210 return (0); 211 } 212 213 static void 214 nvme_ctrlr_fail(struct nvme_controller *ctrlr) 215 { 216 int i; 217 218 /* 219 * No need to disable queues before failing them. Failing is a superet 220 * of disabling (though pedantically we'd abort the AERs silently with 221 * a different error, though when we fail, that hardly matters). 222 */ 223 ctrlr->is_failed = true; 224 nvme_qpair_fail(&ctrlr->adminq); 225 if (ctrlr->ioq != NULL) { 226 for (i = 0; i < ctrlr->num_io_queues; i++) { 227 nvme_qpair_fail(&ctrlr->ioq[i]); 228 } 229 } 230 nvme_notify_fail_consumers(ctrlr); 231 } 232 233 /* 234 * Wait for RDY to change. 235 * 236 * Starts sleeping for 1us and geometrically increases it the longer we wait, 237 * capped at 1ms. 238 */ 239 static int 240 nvme_ctrlr_wait_for_ready(struct nvme_controller *ctrlr, int desired_val) 241 { 242 int timeout = ticks + MSEC_2_TICKS(ctrlr->ready_timeout_in_ms); 243 sbintime_t delta_t = SBT_1US; 244 uint32_t csts; 245 246 while (1) { 247 csts = nvme_mmio_read_4(ctrlr, csts); 248 if (csts == NVME_GONE) /* Hot unplug. */ 249 return (ENXIO); 250 if (NVMEV(NVME_CSTS_REG_RDY, csts) == desired_val) 251 break; 252 if (timeout - ticks < 0) { 253 nvme_printf(ctrlr, "controller ready did not become %d " 254 "within %d ms\n", desired_val, ctrlr->ready_timeout_in_ms); 255 return (ENXIO); 256 } 257 258 pause_sbt("nvmerdy", delta_t, 0, C_PREL(1)); 259 delta_t = min(SBT_1MS, delta_t * 3 / 2); 260 } 261 262 return (0); 263 } 264 265 static int 266 nvme_ctrlr_disable(struct nvme_controller *ctrlr) 267 { 268 uint32_t cc; 269 uint32_t csts; 270 uint8_t en, rdy; 271 int err; 272 273 cc = nvme_mmio_read_4(ctrlr, cc); 274 csts = nvme_mmio_read_4(ctrlr, csts); 275 276 en = NVMEV(NVME_CC_REG_EN, cc); 277 rdy = NVMEV(NVME_CSTS_REG_RDY, csts); 278 279 /* 280 * Per 3.1.5 in NVME 1.3 spec, transitioning CC.EN from 0 to 1 281 * when CSTS.RDY is 1 or transitioning CC.EN from 1 to 0 when 282 * CSTS.RDY is 0 "has undefined results" So make sure that CSTS.RDY 283 * isn't the desired value. Short circuit if we're already disabled. 284 */ 285 if (en == 0) { 286 /* Wait for RDY == 0 or timeout & fail */ 287 if (rdy == 0) 288 return (0); 289 return (nvme_ctrlr_wait_for_ready(ctrlr, 0)); 290 } 291 if (rdy == 0) { 292 /* EN == 1, wait for RDY == 1 or timeout & fail */ 293 err = nvme_ctrlr_wait_for_ready(ctrlr, 1); 294 if (err != 0) 295 return (err); 296 } 297 298 cc &= ~NVMEM(NVME_CC_REG_EN); 299 nvme_mmio_write_4(ctrlr, cc, cc); 300 301 /* 302 * A few drives have firmware bugs that freeze the drive if we access 303 * the mmio too soon after we disable. 304 */ 305 if (ctrlr->quirks & QUIRK_DELAY_B4_CHK_RDY) 306 pause("nvmeR", MSEC_2_TICKS(B4_CHK_RDY_DELAY_MS)); 307 return (nvme_ctrlr_wait_for_ready(ctrlr, 0)); 308 } 309 310 static int 311 nvme_ctrlr_enable(struct nvme_controller *ctrlr) 312 { 313 uint32_t cc; 314 uint32_t csts; 315 uint32_t aqa; 316 uint32_t qsize; 317 uint8_t en, rdy; 318 int err; 319 320 cc = nvme_mmio_read_4(ctrlr, cc); 321 csts = nvme_mmio_read_4(ctrlr, csts); 322 323 en = NVMEV(NVME_CC_REG_EN, cc); 324 rdy = NVMEV(NVME_CSTS_REG_RDY, csts); 325 326 /* 327 * See note in nvme_ctrlr_disable. Short circuit if we're already enabled. 328 */ 329 if (en == 1) { 330 if (rdy == 1) 331 return (0); 332 return (nvme_ctrlr_wait_for_ready(ctrlr, 1)); 333 } 334 335 /* EN == 0 already wait for RDY == 0 or timeout & fail */ 336 err = nvme_ctrlr_wait_for_ready(ctrlr, 0); 337 if (err != 0) 338 return (err); 339 340 nvme_mmio_write_8(ctrlr, asq, ctrlr->adminq.cmd_bus_addr); 341 nvme_mmio_write_8(ctrlr, acq, ctrlr->adminq.cpl_bus_addr); 342 343 /* acqs and asqs are 0-based. */ 344 qsize = ctrlr->adminq.num_entries - 1; 345 346 aqa = 0; 347 aqa |= NVMEF(NVME_AQA_REG_ACQS, qsize); 348 aqa |= NVMEF(NVME_AQA_REG_ASQS, qsize); 349 nvme_mmio_write_4(ctrlr, aqa, aqa); 350 351 /* Initialization values for CC */ 352 cc = 0; 353 cc |= NVMEF(NVME_CC_REG_EN, 1); 354 cc |= NVMEF(NVME_CC_REG_CSS, 0); 355 cc |= NVMEF(NVME_CC_REG_AMS, 0); 356 cc |= NVMEF(NVME_CC_REG_SHN, 0); 357 cc |= NVMEF(NVME_CC_REG_IOSQES, 6); /* SQ entry size == 64 == 2^6 */ 358 cc |= NVMEF(NVME_CC_REG_IOCQES, 4); /* CQ entry size == 16 == 2^4 */ 359 360 /* 361 * Use the Memory Page Size selected during device initialization. Note 362 * that value stored in mps is suitable to use here without adjusting by 363 * NVME_MPS_SHIFT. 364 */ 365 cc |= NVMEF(NVME_CC_REG_MPS, ctrlr->mps); 366 367 nvme_ctrlr_barrier(ctrlr, BUS_SPACE_BARRIER_WRITE); 368 nvme_mmio_write_4(ctrlr, cc, cc); 369 370 return (nvme_ctrlr_wait_for_ready(ctrlr, 1)); 371 } 372 373 static void 374 nvme_ctrlr_disable_qpairs(struct nvme_controller *ctrlr) 375 { 376 int i; 377 378 nvme_admin_qpair_disable(&ctrlr->adminq); 379 /* 380 * I/O queues are not allocated before the initial HW 381 * reset, so do not try to disable them. Use is_initialized 382 * to determine if this is the initial HW reset. 383 */ 384 if (ctrlr->is_initialized) { 385 for (i = 0; i < ctrlr->num_io_queues; i++) 386 nvme_io_qpair_disable(&ctrlr->ioq[i]); 387 } 388 } 389 390 static int 391 nvme_ctrlr_hw_reset(struct nvme_controller *ctrlr) 392 { 393 int err; 394 395 TSENTER(); 396 397 nvme_ctrlr_disable_qpairs(ctrlr); 398 399 err = nvme_ctrlr_disable(ctrlr); 400 if (err != 0) 401 goto out; 402 403 err = nvme_ctrlr_enable(ctrlr); 404 out: 405 406 TSEXIT(); 407 return (err); 408 } 409 410 void 411 nvme_ctrlr_reset(struct nvme_controller *ctrlr) 412 { 413 int cmpset; 414 415 cmpset = atomic_cmpset_32(&ctrlr->is_resetting, 0, 1); 416 417 if (cmpset == 0 || ctrlr->is_failed) 418 /* 419 * Controller is already resetting or has failed. Return 420 * immediately since there is no need to kick off another 421 * reset in these cases. 422 */ 423 return; 424 425 if (!ctrlr->is_dying) 426 taskqueue_enqueue(ctrlr->taskqueue, &ctrlr->reset_task); 427 } 428 429 static int 430 nvme_ctrlr_identify(struct nvme_controller *ctrlr) 431 { 432 struct nvme_completion_poll_status status; 433 434 status.done = 0; 435 nvme_ctrlr_cmd_identify_controller(ctrlr, &ctrlr->cdata, 436 nvme_completion_poll_cb, &status); 437 nvme_completion_poll(&status); 438 if (nvme_completion_is_error(&status.cpl)) { 439 nvme_printf(ctrlr, "nvme_identify_controller failed!\n"); 440 return (ENXIO); 441 } 442 443 /* Convert data to host endian */ 444 nvme_controller_data_swapbytes(&ctrlr->cdata); 445 446 /* 447 * Use MDTS to ensure our default max_xfer_size doesn't exceed what the 448 * controller supports. 449 */ 450 if (ctrlr->cdata.mdts > 0) 451 ctrlr->max_xfer_size = min(ctrlr->max_xfer_size, 452 1 << (ctrlr->cdata.mdts + NVME_MPS_SHIFT + 453 NVME_CAP_HI_MPSMIN(ctrlr->cap_hi))); 454 455 return (0); 456 } 457 458 static int 459 nvme_ctrlr_set_num_qpairs(struct nvme_controller *ctrlr) 460 { 461 struct nvme_completion_poll_status status; 462 int cq_allocated, sq_allocated; 463 464 status.done = 0; 465 nvme_ctrlr_cmd_set_num_queues(ctrlr, ctrlr->num_io_queues, 466 nvme_completion_poll_cb, &status); 467 nvme_completion_poll(&status); 468 if (nvme_completion_is_error(&status.cpl)) { 469 nvme_printf(ctrlr, "nvme_ctrlr_set_num_qpairs failed!\n"); 470 return (ENXIO); 471 } 472 473 /* 474 * Data in cdw0 is 0-based. 475 * Lower 16-bits indicate number of submission queues allocated. 476 * Upper 16-bits indicate number of completion queues allocated. 477 */ 478 sq_allocated = (status.cpl.cdw0 & 0xFFFF) + 1; 479 cq_allocated = (status.cpl.cdw0 >> 16) + 1; 480 481 /* 482 * Controller may allocate more queues than we requested, 483 * so use the minimum of the number requested and what was 484 * actually allocated. 485 */ 486 ctrlr->num_io_queues = min(ctrlr->num_io_queues, sq_allocated); 487 ctrlr->num_io_queues = min(ctrlr->num_io_queues, cq_allocated); 488 if (ctrlr->num_io_queues > vm_ndomains) 489 ctrlr->num_io_queues -= ctrlr->num_io_queues % vm_ndomains; 490 491 return (0); 492 } 493 494 static int 495 nvme_ctrlr_create_qpairs(struct nvme_controller *ctrlr) 496 { 497 struct nvme_completion_poll_status status; 498 struct nvme_qpair *qpair; 499 int i; 500 501 for (i = 0; i < ctrlr->num_io_queues; i++) { 502 qpair = &ctrlr->ioq[i]; 503 504 status.done = 0; 505 nvme_ctrlr_cmd_create_io_cq(ctrlr, qpair, 506 nvme_completion_poll_cb, &status); 507 nvme_completion_poll(&status); 508 if (nvme_completion_is_error(&status.cpl)) { 509 nvme_printf(ctrlr, "nvme_create_io_cq failed!\n"); 510 return (ENXIO); 511 } 512 513 status.done = 0; 514 nvme_ctrlr_cmd_create_io_sq(ctrlr, qpair, 515 nvme_completion_poll_cb, &status); 516 nvme_completion_poll(&status); 517 if (nvme_completion_is_error(&status.cpl)) { 518 nvme_printf(ctrlr, "nvme_create_io_sq failed!\n"); 519 return (ENXIO); 520 } 521 } 522 523 return (0); 524 } 525 526 static int 527 nvme_ctrlr_delete_qpairs(struct nvme_controller *ctrlr) 528 { 529 struct nvme_completion_poll_status status; 530 struct nvme_qpair *qpair; 531 532 for (int i = 0; i < ctrlr->num_io_queues; i++) { 533 qpair = &ctrlr->ioq[i]; 534 535 status.done = 0; 536 nvme_ctrlr_cmd_delete_io_sq(ctrlr, qpair, 537 nvme_completion_poll_cb, &status); 538 nvme_completion_poll(&status); 539 if (nvme_completion_is_error(&status.cpl)) { 540 nvme_printf(ctrlr, "nvme_destroy_io_sq failed!\n"); 541 return (ENXIO); 542 } 543 544 status.done = 0; 545 nvme_ctrlr_cmd_delete_io_cq(ctrlr, qpair, 546 nvme_completion_poll_cb, &status); 547 nvme_completion_poll(&status); 548 if (nvme_completion_is_error(&status.cpl)) { 549 nvme_printf(ctrlr, "nvme_destroy_io_cq failed!\n"); 550 return (ENXIO); 551 } 552 } 553 554 return (0); 555 } 556 557 static int 558 nvme_ctrlr_construct_namespaces(struct nvme_controller *ctrlr) 559 { 560 struct nvme_namespace *ns; 561 uint32_t i; 562 563 for (i = 0; i < min(ctrlr->cdata.nn, NVME_MAX_NAMESPACES); i++) { 564 ns = &ctrlr->ns[i]; 565 nvme_ns_construct(ns, i+1, ctrlr); 566 } 567 568 return (0); 569 } 570 571 static bool 572 is_log_page_id_valid(uint8_t page_id) 573 { 574 575 switch (page_id) { 576 case NVME_LOG_ERROR: 577 case NVME_LOG_HEALTH_INFORMATION: 578 case NVME_LOG_FIRMWARE_SLOT: 579 case NVME_LOG_CHANGED_NAMESPACE: 580 case NVME_LOG_COMMAND_EFFECT: 581 case NVME_LOG_RES_NOTIFICATION: 582 case NVME_LOG_SANITIZE_STATUS: 583 return (true); 584 } 585 586 return (false); 587 } 588 589 static uint32_t 590 nvme_ctrlr_get_log_page_size(struct nvme_controller *ctrlr, uint8_t page_id) 591 { 592 uint32_t log_page_size; 593 594 switch (page_id) { 595 case NVME_LOG_ERROR: 596 log_page_size = min( 597 sizeof(struct nvme_error_information_entry) * 598 (ctrlr->cdata.elpe + 1), NVME_MAX_AER_LOG_SIZE); 599 break; 600 case NVME_LOG_HEALTH_INFORMATION: 601 log_page_size = sizeof(struct nvme_health_information_page); 602 break; 603 case NVME_LOG_FIRMWARE_SLOT: 604 log_page_size = sizeof(struct nvme_firmware_page); 605 break; 606 case NVME_LOG_CHANGED_NAMESPACE: 607 log_page_size = sizeof(struct nvme_ns_list); 608 break; 609 case NVME_LOG_COMMAND_EFFECT: 610 log_page_size = sizeof(struct nvme_command_effects_page); 611 break; 612 case NVME_LOG_RES_NOTIFICATION: 613 log_page_size = sizeof(struct nvme_res_notification_page); 614 break; 615 case NVME_LOG_SANITIZE_STATUS: 616 log_page_size = sizeof(struct nvme_sanitize_status_page); 617 break; 618 default: 619 log_page_size = 0; 620 break; 621 } 622 623 return (log_page_size); 624 } 625 626 static void 627 nvme_ctrlr_log_critical_warnings(struct nvme_controller *ctrlr, 628 uint8_t state) 629 { 630 631 if (state & NVME_CRIT_WARN_ST_AVAILABLE_SPARE) 632 nvme_ctrlr_devctl_log(ctrlr, "critical", 633 "available spare space below threshold"); 634 635 if (state & NVME_CRIT_WARN_ST_TEMPERATURE) 636 nvme_ctrlr_devctl_log(ctrlr, "critical", 637 "temperature above threshold"); 638 639 if (state & NVME_CRIT_WARN_ST_DEVICE_RELIABILITY) 640 nvme_ctrlr_devctl_log(ctrlr, "critical", 641 "device reliability degraded"); 642 643 if (state & NVME_CRIT_WARN_ST_READ_ONLY) 644 nvme_ctrlr_devctl_log(ctrlr, "critical", 645 "media placed in read only mode"); 646 647 if (state & NVME_CRIT_WARN_ST_VOLATILE_MEMORY_BACKUP) 648 nvme_ctrlr_devctl_log(ctrlr, "critical", 649 "volatile memory backup device failed"); 650 651 if (state & NVME_CRIT_WARN_ST_RESERVED_MASK) 652 nvme_ctrlr_devctl_log(ctrlr, "critical", 653 "unknown critical warning(s): state = 0x%02x", 654 state & NVME_CRIT_WARN_ST_RESERVED_MASK); 655 } 656 657 static void 658 nvme_ctrlr_async_event_log_page_cb(void *arg, const struct nvme_completion *cpl) 659 { 660 struct nvme_async_event_request *aer = arg; 661 struct nvme_health_information_page *health_info; 662 struct nvme_ns_list *nsl; 663 struct nvme_error_information_entry *err; 664 int i; 665 666 /* 667 * If the log page fetch for some reason completed with an error, 668 * don't pass log page data to the consumers. In practice, this case 669 * should never happen. 670 */ 671 if (nvme_completion_is_error(cpl)) 672 nvme_notify_async_consumers(aer->ctrlr, &aer->cpl, 673 aer->log_page_id, NULL, 0); 674 else { 675 /* Convert data to host endian */ 676 switch (aer->log_page_id) { 677 case NVME_LOG_ERROR: 678 err = (struct nvme_error_information_entry *)aer->log_page_buffer; 679 for (i = 0; i < (aer->ctrlr->cdata.elpe + 1); i++) 680 nvme_error_information_entry_swapbytes(err++); 681 break; 682 case NVME_LOG_HEALTH_INFORMATION: 683 nvme_health_information_page_swapbytes( 684 (struct nvme_health_information_page *)aer->log_page_buffer); 685 break; 686 case NVME_LOG_CHANGED_NAMESPACE: 687 nvme_ns_list_swapbytes( 688 (struct nvme_ns_list *)aer->log_page_buffer); 689 break; 690 case NVME_LOG_COMMAND_EFFECT: 691 nvme_command_effects_page_swapbytes( 692 (struct nvme_command_effects_page *)aer->log_page_buffer); 693 break; 694 case NVME_LOG_RES_NOTIFICATION: 695 nvme_res_notification_page_swapbytes( 696 (struct nvme_res_notification_page *)aer->log_page_buffer); 697 break; 698 case NVME_LOG_SANITIZE_STATUS: 699 nvme_sanitize_status_page_swapbytes( 700 (struct nvme_sanitize_status_page *)aer->log_page_buffer); 701 break; 702 case INTEL_LOG_TEMP_STATS: 703 intel_log_temp_stats_swapbytes( 704 (struct intel_log_temp_stats *)aer->log_page_buffer); 705 break; 706 default: 707 break; 708 } 709 710 if (aer->log_page_id == NVME_LOG_HEALTH_INFORMATION) { 711 health_info = (struct nvme_health_information_page *) 712 aer->log_page_buffer; 713 nvme_ctrlr_log_critical_warnings(aer->ctrlr, 714 health_info->critical_warning); 715 /* 716 * Critical warnings reported through the 717 * SMART/health log page are persistent, so 718 * clear the associated bits in the async event 719 * config so that we do not receive repeated 720 * notifications for the same event. 721 */ 722 aer->ctrlr->async_event_config &= 723 ~health_info->critical_warning; 724 nvme_ctrlr_cmd_set_async_event_config(aer->ctrlr, 725 aer->ctrlr->async_event_config, NULL, NULL); 726 } else if (aer->log_page_id == NVME_LOG_CHANGED_NAMESPACE && 727 !nvme_use_nvd) { 728 nsl = (struct nvme_ns_list *)aer->log_page_buffer; 729 for (i = 0; i < nitems(nsl->ns) && nsl->ns[i] != 0; i++) { 730 if (nsl->ns[i] > NVME_MAX_NAMESPACES) 731 break; 732 nvme_notify_ns(aer->ctrlr, nsl->ns[i]); 733 } 734 } 735 736 /* 737 * Pass the cpl data from the original async event completion, 738 * not the log page fetch. 739 */ 740 nvme_notify_async_consumers(aer->ctrlr, &aer->cpl, 741 aer->log_page_id, aer->log_page_buffer, aer->log_page_size); 742 } 743 744 /* 745 * Repost another asynchronous event request to replace the one 746 * that just completed. 747 */ 748 nvme_ctrlr_construct_and_submit_aer(aer->ctrlr, aer); 749 } 750 751 static void 752 nvme_ctrlr_async_event_cb(void *arg, const struct nvme_completion *cpl) 753 { 754 struct nvme_async_event_request *aer = arg; 755 756 if (nvme_completion_is_error(cpl)) { 757 /* 758 * Do not retry failed async event requests. This avoids 759 * infinite loops where a new async event request is submitted 760 * to replace the one just failed, only to fail again and 761 * perpetuate the loop. 762 */ 763 return; 764 } 765 766 /* Associated log page is in bits 23:16 of completion entry dw0. */ 767 aer->log_page_id = (cpl->cdw0 & 0xFF0000) >> 16; 768 769 nvme_printf(aer->ctrlr, "async event occurred (type 0x%x, info 0x%02x," 770 " page 0x%02x)\n", (cpl->cdw0 & 0x07), (cpl->cdw0 & 0xFF00) >> 8, 771 aer->log_page_id); 772 773 if (is_log_page_id_valid(aer->log_page_id)) { 774 aer->log_page_size = nvme_ctrlr_get_log_page_size(aer->ctrlr, 775 aer->log_page_id); 776 memcpy(&aer->cpl, cpl, sizeof(*cpl)); 777 nvme_ctrlr_cmd_get_log_page(aer->ctrlr, aer->log_page_id, 778 NVME_GLOBAL_NAMESPACE_TAG, aer->log_page_buffer, 779 aer->log_page_size, nvme_ctrlr_async_event_log_page_cb, 780 aer); 781 /* Wait to notify consumers until after log page is fetched. */ 782 } else { 783 nvme_notify_async_consumers(aer->ctrlr, cpl, aer->log_page_id, 784 NULL, 0); 785 786 /* 787 * Repost another asynchronous event request to replace the one 788 * that just completed. 789 */ 790 nvme_ctrlr_construct_and_submit_aer(aer->ctrlr, aer); 791 } 792 } 793 794 static void 795 nvme_ctrlr_construct_and_submit_aer(struct nvme_controller *ctrlr, 796 struct nvme_async_event_request *aer) 797 { 798 struct nvme_request *req; 799 800 aer->ctrlr = ctrlr; 801 req = nvme_allocate_request_null(nvme_ctrlr_async_event_cb, aer); 802 aer->req = req; 803 804 /* 805 * Disable timeout here, since asynchronous event requests should by 806 * nature never be timed out. 807 */ 808 req->timeout = false; 809 req->cmd.opc = NVME_OPC_ASYNC_EVENT_REQUEST; 810 nvme_ctrlr_submit_admin_request(ctrlr, req); 811 } 812 813 static void 814 nvme_ctrlr_configure_aer(struct nvme_controller *ctrlr) 815 { 816 struct nvme_completion_poll_status status; 817 struct nvme_async_event_request *aer; 818 uint32_t i; 819 820 ctrlr->async_event_config = NVME_CRIT_WARN_ST_AVAILABLE_SPARE | 821 NVME_CRIT_WARN_ST_DEVICE_RELIABILITY | 822 NVME_CRIT_WARN_ST_READ_ONLY | 823 NVME_CRIT_WARN_ST_VOLATILE_MEMORY_BACKUP; 824 if (ctrlr->cdata.ver >= NVME_REV(1, 2)) 825 ctrlr->async_event_config |= 826 ctrlr->cdata.oaes & (NVME_ASYNC_EVENT_NS_ATTRIBUTE | 827 NVME_ASYNC_EVENT_FW_ACTIVATE); 828 829 status.done = 0; 830 nvme_ctrlr_cmd_get_feature(ctrlr, NVME_FEAT_TEMPERATURE_THRESHOLD, 831 0, NULL, 0, nvme_completion_poll_cb, &status); 832 nvme_completion_poll(&status); 833 if (nvme_completion_is_error(&status.cpl) || 834 (status.cpl.cdw0 & 0xFFFF) == 0xFFFF || 835 (status.cpl.cdw0 & 0xFFFF) == 0x0000) { 836 nvme_printf(ctrlr, "temperature threshold not supported\n"); 837 } else 838 ctrlr->async_event_config |= NVME_CRIT_WARN_ST_TEMPERATURE; 839 840 nvme_ctrlr_cmd_set_async_event_config(ctrlr, 841 ctrlr->async_event_config, NULL, NULL); 842 843 /* aerl is a zero-based value, so we need to add 1 here. */ 844 ctrlr->num_aers = min(NVME_MAX_ASYNC_EVENTS, (ctrlr->cdata.aerl+1)); 845 846 for (i = 0; i < ctrlr->num_aers; i++) { 847 aer = &ctrlr->aer[i]; 848 nvme_ctrlr_construct_and_submit_aer(ctrlr, aer); 849 } 850 } 851 852 static void 853 nvme_ctrlr_configure_int_coalescing(struct nvme_controller *ctrlr) 854 { 855 856 ctrlr->int_coal_time = 0; 857 TUNABLE_INT_FETCH("hw.nvme.int_coal_time", 858 &ctrlr->int_coal_time); 859 860 ctrlr->int_coal_threshold = 0; 861 TUNABLE_INT_FETCH("hw.nvme.int_coal_threshold", 862 &ctrlr->int_coal_threshold); 863 864 nvme_ctrlr_cmd_set_interrupt_coalescing(ctrlr, ctrlr->int_coal_time, 865 ctrlr->int_coal_threshold, NULL, NULL); 866 } 867 868 static void 869 nvme_ctrlr_hmb_free(struct nvme_controller *ctrlr) 870 { 871 struct nvme_hmb_chunk *hmbc; 872 int i; 873 874 if (ctrlr->hmb_desc_paddr) { 875 bus_dmamap_unload(ctrlr->hmb_desc_tag, ctrlr->hmb_desc_map); 876 bus_dmamem_free(ctrlr->hmb_desc_tag, ctrlr->hmb_desc_vaddr, 877 ctrlr->hmb_desc_map); 878 ctrlr->hmb_desc_paddr = 0; 879 } 880 if (ctrlr->hmb_desc_tag) { 881 bus_dma_tag_destroy(ctrlr->hmb_desc_tag); 882 ctrlr->hmb_desc_tag = NULL; 883 } 884 for (i = 0; i < ctrlr->hmb_nchunks; i++) { 885 hmbc = &ctrlr->hmb_chunks[i]; 886 bus_dmamap_unload(ctrlr->hmb_tag, hmbc->hmbc_map); 887 bus_dmamem_free(ctrlr->hmb_tag, hmbc->hmbc_vaddr, 888 hmbc->hmbc_map); 889 } 890 ctrlr->hmb_nchunks = 0; 891 if (ctrlr->hmb_tag) { 892 bus_dma_tag_destroy(ctrlr->hmb_tag); 893 ctrlr->hmb_tag = NULL; 894 } 895 if (ctrlr->hmb_chunks) { 896 free(ctrlr->hmb_chunks, M_NVME); 897 ctrlr->hmb_chunks = NULL; 898 } 899 } 900 901 static void 902 nvme_ctrlr_hmb_alloc(struct nvme_controller *ctrlr) 903 { 904 struct nvme_hmb_chunk *hmbc; 905 size_t pref, min, minc, size; 906 int err, i; 907 uint64_t max; 908 909 /* Limit HMB to 5% of RAM size per device by default. */ 910 max = (uint64_t)physmem * PAGE_SIZE / 20; 911 TUNABLE_UINT64_FETCH("hw.nvme.hmb_max", &max); 912 913 /* 914 * Units of Host Memory Buffer in the Identify info are always in terms 915 * of 4k units. 916 */ 917 min = (long long unsigned)ctrlr->cdata.hmmin * NVME_HMB_UNITS; 918 if (max == 0 || max < min) 919 return; 920 pref = MIN((long long unsigned)ctrlr->cdata.hmpre * NVME_HMB_UNITS, max); 921 minc = MAX(ctrlr->cdata.hmminds * NVME_HMB_UNITS, ctrlr->page_size); 922 if (min > 0 && ctrlr->cdata.hmmaxd > 0) 923 minc = MAX(minc, min / ctrlr->cdata.hmmaxd); 924 ctrlr->hmb_chunk = pref; 925 926 again: 927 /* 928 * However, the chunk sizes, number of chunks, and alignment of chunks 929 * are all based on the current MPS (ctrlr->page_size). 930 */ 931 ctrlr->hmb_chunk = roundup2(ctrlr->hmb_chunk, ctrlr->page_size); 932 ctrlr->hmb_nchunks = howmany(pref, ctrlr->hmb_chunk); 933 if (ctrlr->cdata.hmmaxd > 0 && ctrlr->hmb_nchunks > ctrlr->cdata.hmmaxd) 934 ctrlr->hmb_nchunks = ctrlr->cdata.hmmaxd; 935 ctrlr->hmb_chunks = malloc(sizeof(struct nvme_hmb_chunk) * 936 ctrlr->hmb_nchunks, M_NVME, M_WAITOK); 937 err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev), 938 ctrlr->page_size, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 939 ctrlr->hmb_chunk, 1, ctrlr->hmb_chunk, 0, NULL, NULL, &ctrlr->hmb_tag); 940 if (err != 0) { 941 nvme_printf(ctrlr, "HMB tag create failed %d\n", err); 942 nvme_ctrlr_hmb_free(ctrlr); 943 return; 944 } 945 946 for (i = 0; i < ctrlr->hmb_nchunks; i++) { 947 hmbc = &ctrlr->hmb_chunks[i]; 948 if (bus_dmamem_alloc(ctrlr->hmb_tag, 949 (void **)&hmbc->hmbc_vaddr, BUS_DMA_NOWAIT, 950 &hmbc->hmbc_map)) { 951 nvme_printf(ctrlr, "failed to alloc HMB\n"); 952 break; 953 } 954 if (bus_dmamap_load(ctrlr->hmb_tag, hmbc->hmbc_map, 955 hmbc->hmbc_vaddr, ctrlr->hmb_chunk, nvme_single_map, 956 &hmbc->hmbc_paddr, BUS_DMA_NOWAIT) != 0) { 957 bus_dmamem_free(ctrlr->hmb_tag, hmbc->hmbc_vaddr, 958 hmbc->hmbc_map); 959 nvme_printf(ctrlr, "failed to load HMB\n"); 960 break; 961 } 962 bus_dmamap_sync(ctrlr->hmb_tag, hmbc->hmbc_map, 963 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 964 } 965 966 if (i < ctrlr->hmb_nchunks && i * ctrlr->hmb_chunk < min && 967 ctrlr->hmb_chunk / 2 >= minc) { 968 ctrlr->hmb_nchunks = i; 969 nvme_ctrlr_hmb_free(ctrlr); 970 ctrlr->hmb_chunk /= 2; 971 goto again; 972 } 973 ctrlr->hmb_nchunks = i; 974 if (ctrlr->hmb_nchunks * ctrlr->hmb_chunk < min) { 975 nvme_ctrlr_hmb_free(ctrlr); 976 return; 977 } 978 979 size = sizeof(struct nvme_hmb_desc) * ctrlr->hmb_nchunks; 980 err = bus_dma_tag_create(bus_get_dma_tag(ctrlr->dev), 981 16, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 982 size, 1, size, 0, NULL, NULL, &ctrlr->hmb_desc_tag); 983 if (err != 0) { 984 nvme_printf(ctrlr, "HMB desc tag create failed %d\n", err); 985 nvme_ctrlr_hmb_free(ctrlr); 986 return; 987 } 988 if (bus_dmamem_alloc(ctrlr->hmb_desc_tag, 989 (void **)&ctrlr->hmb_desc_vaddr, BUS_DMA_WAITOK, 990 &ctrlr->hmb_desc_map)) { 991 nvme_printf(ctrlr, "failed to alloc HMB desc\n"); 992 nvme_ctrlr_hmb_free(ctrlr); 993 return; 994 } 995 if (bus_dmamap_load(ctrlr->hmb_desc_tag, ctrlr->hmb_desc_map, 996 ctrlr->hmb_desc_vaddr, size, nvme_single_map, 997 &ctrlr->hmb_desc_paddr, BUS_DMA_NOWAIT) != 0) { 998 bus_dmamem_free(ctrlr->hmb_desc_tag, ctrlr->hmb_desc_vaddr, 999 ctrlr->hmb_desc_map); 1000 nvme_printf(ctrlr, "failed to load HMB desc\n"); 1001 nvme_ctrlr_hmb_free(ctrlr); 1002 return; 1003 } 1004 1005 for (i = 0; i < ctrlr->hmb_nchunks; i++) { 1006 memset(&ctrlr->hmb_desc_vaddr[i], 0, 1007 sizeof(struct nvme_hmb_desc)); 1008 ctrlr->hmb_desc_vaddr[i].addr = 1009 htole64(ctrlr->hmb_chunks[i].hmbc_paddr); 1010 ctrlr->hmb_desc_vaddr[i].size = htole32(ctrlr->hmb_chunk / ctrlr->page_size); 1011 } 1012 bus_dmamap_sync(ctrlr->hmb_desc_tag, ctrlr->hmb_desc_map, 1013 BUS_DMASYNC_PREWRITE); 1014 1015 nvme_printf(ctrlr, "Allocated %lluMB host memory buffer\n", 1016 (long long unsigned)ctrlr->hmb_nchunks * ctrlr->hmb_chunk 1017 / 1024 / 1024); 1018 } 1019 1020 static void 1021 nvme_ctrlr_hmb_enable(struct nvme_controller *ctrlr, bool enable, bool memret) 1022 { 1023 struct nvme_completion_poll_status status; 1024 uint32_t cdw11; 1025 1026 cdw11 = 0; 1027 if (enable) 1028 cdw11 |= 1; 1029 if (memret) 1030 cdw11 |= 2; 1031 status.done = 0; 1032 nvme_ctrlr_cmd_set_feature(ctrlr, NVME_FEAT_HOST_MEMORY_BUFFER, cdw11, 1033 ctrlr->hmb_nchunks * ctrlr->hmb_chunk / ctrlr->page_size, 1034 ctrlr->hmb_desc_paddr, ctrlr->hmb_desc_paddr >> 32, 1035 ctrlr->hmb_nchunks, NULL, 0, 1036 nvme_completion_poll_cb, &status); 1037 nvme_completion_poll(&status); 1038 if (nvme_completion_is_error(&status.cpl)) 1039 nvme_printf(ctrlr, "nvme_ctrlr_hmb_enable failed!\n"); 1040 } 1041 1042 static void 1043 nvme_ctrlr_start(void *ctrlr_arg, bool resetting) 1044 { 1045 struct nvme_controller *ctrlr = ctrlr_arg; 1046 uint32_t old_num_io_queues; 1047 int i; 1048 1049 TSENTER(); 1050 1051 /* 1052 * Only reset adminq here when we are restarting the 1053 * controller after a reset. During initialization, 1054 * we have already submitted admin commands to get 1055 * the number of I/O queues supported, so cannot reset 1056 * the adminq again here. 1057 */ 1058 if (resetting) { 1059 nvme_qpair_reset(&ctrlr->adminq); 1060 nvme_admin_qpair_enable(&ctrlr->adminq); 1061 } 1062 1063 if (ctrlr->ioq != NULL) { 1064 for (i = 0; i < ctrlr->num_io_queues; i++) 1065 nvme_qpair_reset(&ctrlr->ioq[i]); 1066 } 1067 1068 /* 1069 * If it was a reset on initialization command timeout, just 1070 * return here, letting initialization code fail gracefully. 1071 */ 1072 if (resetting && !ctrlr->is_initialized) 1073 return; 1074 1075 if (resetting && nvme_ctrlr_identify(ctrlr) != 0) { 1076 nvme_ctrlr_fail(ctrlr); 1077 return; 1078 } 1079 1080 /* 1081 * The number of qpairs are determined during controller initialization, 1082 * including using NVMe SET_FEATURES/NUMBER_OF_QUEUES to determine the 1083 * HW limit. We call SET_FEATURES again here so that it gets called 1084 * after any reset for controllers that depend on the driver to 1085 * explicit specify how many queues it will use. This value should 1086 * never change between resets, so panic if somehow that does happen. 1087 */ 1088 if (resetting) { 1089 old_num_io_queues = ctrlr->num_io_queues; 1090 if (nvme_ctrlr_set_num_qpairs(ctrlr) != 0) { 1091 nvme_ctrlr_fail(ctrlr); 1092 return; 1093 } 1094 1095 if (old_num_io_queues != ctrlr->num_io_queues) { 1096 panic("num_io_queues changed from %u to %u", 1097 old_num_io_queues, ctrlr->num_io_queues); 1098 } 1099 } 1100 1101 if (ctrlr->cdata.hmpre > 0 && ctrlr->hmb_nchunks == 0) { 1102 nvme_ctrlr_hmb_alloc(ctrlr); 1103 if (ctrlr->hmb_nchunks > 0) 1104 nvme_ctrlr_hmb_enable(ctrlr, true, false); 1105 } else if (ctrlr->hmb_nchunks > 0) 1106 nvme_ctrlr_hmb_enable(ctrlr, true, true); 1107 1108 if (nvme_ctrlr_create_qpairs(ctrlr) != 0) { 1109 nvme_ctrlr_fail(ctrlr); 1110 return; 1111 } 1112 1113 if (nvme_ctrlr_construct_namespaces(ctrlr) != 0) { 1114 nvme_ctrlr_fail(ctrlr); 1115 return; 1116 } 1117 1118 nvme_ctrlr_configure_aer(ctrlr); 1119 nvme_ctrlr_configure_int_coalescing(ctrlr); 1120 1121 for (i = 0; i < ctrlr->num_io_queues; i++) 1122 nvme_io_qpair_enable(&ctrlr->ioq[i]); 1123 TSEXIT(); 1124 } 1125 1126 void 1127 nvme_ctrlr_start_config_hook(void *arg) 1128 { 1129 struct nvme_controller *ctrlr = arg; 1130 1131 TSENTER(); 1132 1133 if (nvme_ctrlr_hw_reset(ctrlr) != 0) { 1134 fail: 1135 nvme_ctrlr_fail(ctrlr); 1136 config_intrhook_disestablish(&ctrlr->config_hook); 1137 return; 1138 } 1139 1140 nvme_qpair_reset(&ctrlr->adminq); 1141 nvme_admin_qpair_enable(&ctrlr->adminq); 1142 1143 if (nvme_ctrlr_identify(ctrlr) == 0 && 1144 nvme_ctrlr_set_num_qpairs(ctrlr) == 0 && 1145 nvme_ctrlr_construct_io_qpairs(ctrlr) == 0) 1146 nvme_ctrlr_start(ctrlr, false); 1147 else 1148 goto fail; 1149 1150 nvme_sysctl_initialize_ctrlr(ctrlr); 1151 config_intrhook_disestablish(&ctrlr->config_hook); 1152 1153 ctrlr->is_initialized = 1; 1154 nvme_notify_new_controller(ctrlr); 1155 TSEXIT(); 1156 } 1157 1158 static void 1159 nvme_ctrlr_reset_task(void *arg, int pending) 1160 { 1161 struct nvme_controller *ctrlr = arg; 1162 int status; 1163 1164 nvme_ctrlr_devctl_log(ctrlr, "RESET", "resetting controller"); 1165 status = nvme_ctrlr_hw_reset(ctrlr); 1166 if (status == 0) 1167 nvme_ctrlr_start(ctrlr, true); 1168 else 1169 nvme_ctrlr_fail(ctrlr); 1170 1171 atomic_cmpset_32(&ctrlr->is_resetting, 1, 0); 1172 } 1173 1174 /* 1175 * Poll all the queues enabled on the device for completion. 1176 */ 1177 void 1178 nvme_ctrlr_poll(struct nvme_controller *ctrlr) 1179 { 1180 int i; 1181 1182 nvme_qpair_process_completions(&ctrlr->adminq); 1183 1184 for (i = 0; i < ctrlr->num_io_queues; i++) 1185 if (ctrlr->ioq && ctrlr->ioq[i].cpl) 1186 nvme_qpair_process_completions(&ctrlr->ioq[i]); 1187 } 1188 1189 /* 1190 * Poll the single-vector interrupt case: num_io_queues will be 1 and 1191 * there's only a single vector. While we're polling, we mask further 1192 * interrupts in the controller. 1193 */ 1194 void 1195 nvme_ctrlr_shared_handler(void *arg) 1196 { 1197 struct nvme_controller *ctrlr = arg; 1198 1199 nvme_mmio_write_4(ctrlr, intms, 1); 1200 nvme_ctrlr_poll(ctrlr); 1201 nvme_mmio_write_4(ctrlr, intmc, 1); 1202 } 1203 1204 static void 1205 nvme_pt_done(void *arg, const struct nvme_completion *cpl) 1206 { 1207 struct nvme_pt_command *pt = arg; 1208 struct mtx *mtx = pt->driver_lock; 1209 uint16_t status; 1210 1211 bzero(&pt->cpl, sizeof(pt->cpl)); 1212 pt->cpl.cdw0 = cpl->cdw0; 1213 1214 status = cpl->status; 1215 status &= ~NVMEM(NVME_STATUS_P); 1216 pt->cpl.status = status; 1217 1218 mtx_lock(mtx); 1219 pt->driver_lock = NULL; 1220 wakeup(pt); 1221 mtx_unlock(mtx); 1222 } 1223 1224 int 1225 nvme_ctrlr_passthrough_cmd(struct nvme_controller *ctrlr, 1226 struct nvme_pt_command *pt, uint32_t nsid, int is_user_buffer, 1227 int is_admin_cmd) 1228 { 1229 struct nvme_request *req; 1230 struct mtx *mtx; 1231 struct buf *buf = NULL; 1232 int ret = 0; 1233 1234 if (pt->len > 0) { 1235 if (pt->len > ctrlr->max_xfer_size) { 1236 nvme_printf(ctrlr, "pt->len (%d) " 1237 "exceeds max_xfer_size (%d)\n", pt->len, 1238 ctrlr->max_xfer_size); 1239 return EIO; 1240 } 1241 if (is_user_buffer) { 1242 /* 1243 * Ensure the user buffer is wired for the duration of 1244 * this pass-through command. 1245 */ 1246 PHOLD(curproc); 1247 buf = uma_zalloc(pbuf_zone, M_WAITOK); 1248 buf->b_iocmd = pt->is_read ? BIO_READ : BIO_WRITE; 1249 if (vmapbuf(buf, pt->buf, pt->len, 1) < 0) { 1250 ret = EFAULT; 1251 goto err; 1252 } 1253 req = nvme_allocate_request_vaddr(buf->b_data, pt->len, 1254 nvme_pt_done, pt); 1255 } else 1256 req = nvme_allocate_request_vaddr(pt->buf, pt->len, 1257 nvme_pt_done, pt); 1258 } else 1259 req = nvme_allocate_request_null(nvme_pt_done, pt); 1260 1261 /* Assume user space already converted to little-endian */ 1262 req->cmd.opc = pt->cmd.opc; 1263 req->cmd.fuse = pt->cmd.fuse; 1264 req->cmd.rsvd2 = pt->cmd.rsvd2; 1265 req->cmd.rsvd3 = pt->cmd.rsvd3; 1266 req->cmd.cdw10 = pt->cmd.cdw10; 1267 req->cmd.cdw11 = pt->cmd.cdw11; 1268 req->cmd.cdw12 = pt->cmd.cdw12; 1269 req->cmd.cdw13 = pt->cmd.cdw13; 1270 req->cmd.cdw14 = pt->cmd.cdw14; 1271 req->cmd.cdw15 = pt->cmd.cdw15; 1272 1273 req->cmd.nsid = htole32(nsid); 1274 1275 mtx = mtx_pool_find(mtxpool_sleep, pt); 1276 pt->driver_lock = mtx; 1277 1278 if (is_admin_cmd) 1279 nvme_ctrlr_submit_admin_request(ctrlr, req); 1280 else 1281 nvme_ctrlr_submit_io_request(ctrlr, req); 1282 1283 mtx_lock(mtx); 1284 while (pt->driver_lock != NULL) 1285 mtx_sleep(pt, mtx, PRIBIO, "nvme_pt", 0); 1286 mtx_unlock(mtx); 1287 1288 if (buf != NULL) { 1289 vunmapbuf(buf); 1290 err: 1291 uma_zfree(pbuf_zone, buf); 1292 PRELE(curproc); 1293 } 1294 1295 return (ret); 1296 } 1297 1298 static int 1299 nvme_ctrlr_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag, 1300 struct thread *td) 1301 { 1302 struct nvme_controller *ctrlr; 1303 struct nvme_pt_command *pt; 1304 1305 ctrlr = cdev->si_drv1; 1306 1307 switch (cmd) { 1308 case NVME_RESET_CONTROLLER: 1309 nvme_ctrlr_reset(ctrlr); 1310 break; 1311 case NVME_PASSTHROUGH_CMD: 1312 pt = (struct nvme_pt_command *)arg; 1313 return (nvme_ctrlr_passthrough_cmd(ctrlr, pt, le32toh(pt->cmd.nsid), 1314 1 /* is_user_buffer */, 1 /* is_admin_cmd */)); 1315 case NVME_GET_NSID: 1316 { 1317 struct nvme_get_nsid *gnsid = (struct nvme_get_nsid *)arg; 1318 strncpy(gnsid->cdev, device_get_nameunit(ctrlr->dev), 1319 sizeof(gnsid->cdev)); 1320 gnsid->cdev[sizeof(gnsid->cdev) - 1] = '\0'; 1321 gnsid->nsid = 0; 1322 break; 1323 } 1324 case NVME_GET_MAX_XFER_SIZE: 1325 *(uint64_t *)arg = ctrlr->max_xfer_size; 1326 break; 1327 default: 1328 return (ENOTTY); 1329 } 1330 1331 return (0); 1332 } 1333 1334 static struct cdevsw nvme_ctrlr_cdevsw = { 1335 .d_version = D_VERSION, 1336 .d_flags = 0, 1337 .d_ioctl = nvme_ctrlr_ioctl 1338 }; 1339 1340 int 1341 nvme_ctrlr_construct(struct nvme_controller *ctrlr, device_t dev) 1342 { 1343 struct make_dev_args md_args; 1344 uint32_t cap_lo; 1345 uint32_t cap_hi; 1346 uint32_t to, vs, pmrcap; 1347 int status, timeout_period; 1348 1349 ctrlr->dev = dev; 1350 1351 mtx_init(&ctrlr->lock, "nvme ctrlr lock", NULL, MTX_DEF); 1352 if (bus_get_domain(dev, &ctrlr->domain) != 0) 1353 ctrlr->domain = 0; 1354 1355 ctrlr->cap_lo = cap_lo = nvme_mmio_read_4(ctrlr, cap_lo); 1356 if (bootverbose) { 1357 device_printf(dev, "CapLo: 0x%08x: MQES %u%s%s%s%s, TO %u\n", 1358 cap_lo, NVME_CAP_LO_MQES(cap_lo), 1359 NVME_CAP_LO_CQR(cap_lo) ? ", CQR" : "", 1360 NVME_CAP_LO_AMS(cap_lo) ? ", AMS" : "", 1361 (NVME_CAP_LO_AMS(cap_lo) & 0x1) ? " WRRwUPC" : "", 1362 (NVME_CAP_LO_AMS(cap_lo) & 0x2) ? " VS" : "", 1363 NVME_CAP_LO_TO(cap_lo)); 1364 } 1365 ctrlr->cap_hi = cap_hi = nvme_mmio_read_4(ctrlr, cap_hi); 1366 if (bootverbose) { 1367 device_printf(dev, "CapHi: 0x%08x: DSTRD %u%s, CSS %x%s, " 1368 "CPS %x, MPSMIN %u, MPSMAX %u%s%s%s%s%s\n", cap_hi, 1369 NVME_CAP_HI_DSTRD(cap_hi), 1370 NVME_CAP_HI_NSSRS(cap_hi) ? ", NSSRS" : "", 1371 NVME_CAP_HI_CSS(cap_hi), 1372 NVME_CAP_HI_BPS(cap_hi) ? ", BPS" : "", 1373 NVME_CAP_HI_CPS(cap_hi), 1374 NVME_CAP_HI_MPSMIN(cap_hi), 1375 NVME_CAP_HI_MPSMAX(cap_hi), 1376 NVME_CAP_HI_PMRS(cap_hi) ? ", PMRS" : "", 1377 NVME_CAP_HI_CMBS(cap_hi) ? ", CMBS" : "", 1378 NVME_CAP_HI_NSSS(cap_hi) ? ", NSSS" : "", 1379 NVME_CAP_HI_CRWMS(cap_hi) ? ", CRWMS" : "", 1380 NVME_CAP_HI_CRIMS(cap_hi) ? ", CRIMS" : ""); 1381 } 1382 if (bootverbose) { 1383 vs = nvme_mmio_read_4(ctrlr, vs); 1384 device_printf(dev, "Version: 0x%08x: %d.%d\n", vs, 1385 NVME_MAJOR(vs), NVME_MINOR(vs)); 1386 } 1387 if (bootverbose && NVME_CAP_HI_PMRS(cap_hi)) { 1388 pmrcap = nvme_mmio_read_4(ctrlr, pmrcap); 1389 device_printf(dev, "PMRCap: 0x%08x: BIR %u%s%s, PMRTU %u, " 1390 "PMRWBM %x, PMRTO %u%s\n", pmrcap, 1391 NVME_PMRCAP_BIR(pmrcap), 1392 NVME_PMRCAP_RDS(pmrcap) ? ", RDS" : "", 1393 NVME_PMRCAP_WDS(pmrcap) ? ", WDS" : "", 1394 NVME_PMRCAP_PMRTU(pmrcap), 1395 NVME_PMRCAP_PMRWBM(pmrcap), 1396 NVME_PMRCAP_PMRTO(pmrcap), 1397 NVME_PMRCAP_CMSS(pmrcap) ? ", CMSS" : ""); 1398 } 1399 1400 ctrlr->dstrd = NVME_CAP_HI_DSTRD(cap_hi) + 2; 1401 1402 ctrlr->mps = NVME_CAP_HI_MPSMIN(cap_hi); 1403 ctrlr->page_size = 1 << (NVME_MPS_SHIFT + ctrlr->mps); 1404 1405 /* Get ready timeout value from controller, in units of 500ms. */ 1406 to = NVME_CAP_LO_TO(cap_lo) + 1; 1407 ctrlr->ready_timeout_in_ms = to * 500; 1408 1409 timeout_period = NVME_ADMIN_TIMEOUT_PERIOD; 1410 TUNABLE_INT_FETCH("hw.nvme.admin_timeout_period", &timeout_period); 1411 timeout_period = min(timeout_period, NVME_MAX_TIMEOUT_PERIOD); 1412 timeout_period = max(timeout_period, NVME_MIN_TIMEOUT_PERIOD); 1413 ctrlr->admin_timeout_period = timeout_period; 1414 1415 timeout_period = NVME_DEFAULT_TIMEOUT_PERIOD; 1416 TUNABLE_INT_FETCH("hw.nvme.timeout_period", &timeout_period); 1417 timeout_period = min(timeout_period, NVME_MAX_TIMEOUT_PERIOD); 1418 timeout_period = max(timeout_period, NVME_MIN_TIMEOUT_PERIOD); 1419 ctrlr->timeout_period = timeout_period; 1420 1421 nvme_retry_count = NVME_DEFAULT_RETRY_COUNT; 1422 TUNABLE_INT_FETCH("hw.nvme.retry_count", &nvme_retry_count); 1423 1424 ctrlr->enable_aborts = 0; 1425 TUNABLE_INT_FETCH("hw.nvme.enable_aborts", &ctrlr->enable_aborts); 1426 1427 /* Cap transfers by the maximum addressable by page-sized PRP (4KB pages -> 2MB). */ 1428 ctrlr->max_xfer_size = MIN(maxphys, (ctrlr->page_size / 8 * ctrlr->page_size)); 1429 if (nvme_ctrlr_construct_admin_qpair(ctrlr) != 0) 1430 return (ENXIO); 1431 1432 /* 1433 * Create 2 threads for the taskqueue. The reset thread will block when 1434 * it detects that the controller has failed until all I/O has been 1435 * failed up the stack. The fail_req task needs to be able to run in 1436 * this case to finish the request failure for some cases. 1437 * 1438 * We could partially solve this race by draining the failed requeust 1439 * queue before proceding to free the sim, though nothing would stop 1440 * new I/O from coming in after we do that drain, but before we reach 1441 * cam_sim_free, so this big hammer is used instead. 1442 */ 1443 ctrlr->taskqueue = taskqueue_create("nvme_taskq", M_WAITOK, 1444 taskqueue_thread_enqueue, &ctrlr->taskqueue); 1445 taskqueue_start_threads(&ctrlr->taskqueue, 2, PI_DISK, "nvme taskq"); 1446 1447 ctrlr->is_resetting = 0; 1448 ctrlr->is_initialized = 0; 1449 ctrlr->notification_sent = 0; 1450 TASK_INIT(&ctrlr->reset_task, 0, nvme_ctrlr_reset_task, ctrlr); 1451 STAILQ_INIT(&ctrlr->fail_req); 1452 ctrlr->is_failed = false; 1453 1454 make_dev_args_init(&md_args); 1455 md_args.mda_devsw = &nvme_ctrlr_cdevsw; 1456 md_args.mda_uid = UID_ROOT; 1457 md_args.mda_gid = GID_WHEEL; 1458 md_args.mda_mode = 0600; 1459 md_args.mda_unit = device_get_unit(dev); 1460 md_args.mda_si_drv1 = (void *)ctrlr; 1461 status = make_dev_s(&md_args, &ctrlr->cdev, "nvme%d", 1462 device_get_unit(dev)); 1463 if (status != 0) 1464 return (ENXIO); 1465 1466 return (0); 1467 } 1468 1469 void 1470 nvme_ctrlr_destruct(struct nvme_controller *ctrlr, device_t dev) 1471 { 1472 int gone, i; 1473 1474 ctrlr->is_dying = true; 1475 1476 if (ctrlr->resource == NULL) 1477 goto nores; 1478 if (!mtx_initialized(&ctrlr->adminq.lock)) 1479 goto noadminq; 1480 1481 /* 1482 * Check whether it is a hot unplug or a clean driver detach. 1483 * If device is not there any more, skip any shutdown commands. 1484 */ 1485 gone = (nvme_mmio_read_4(ctrlr, csts) == NVME_GONE); 1486 if (gone) 1487 nvme_ctrlr_fail(ctrlr); 1488 else 1489 nvme_notify_fail_consumers(ctrlr); 1490 1491 for (i = 0; i < NVME_MAX_NAMESPACES; i++) 1492 nvme_ns_destruct(&ctrlr->ns[i]); 1493 1494 if (ctrlr->cdev) 1495 destroy_dev(ctrlr->cdev); 1496 1497 if (ctrlr->is_initialized) { 1498 if (!gone) { 1499 if (ctrlr->hmb_nchunks > 0) 1500 nvme_ctrlr_hmb_enable(ctrlr, false, false); 1501 nvme_ctrlr_delete_qpairs(ctrlr); 1502 } 1503 nvme_ctrlr_hmb_free(ctrlr); 1504 } 1505 if (ctrlr->ioq != NULL) { 1506 for (i = 0; i < ctrlr->num_io_queues; i++) 1507 nvme_io_qpair_destroy(&ctrlr->ioq[i]); 1508 free(ctrlr->ioq, M_NVME); 1509 } 1510 nvme_admin_qpair_destroy(&ctrlr->adminq); 1511 1512 /* 1513 * Notify the controller of a shutdown, even though this is due to 1514 * a driver unload, not a system shutdown (this path is not invoked 1515 * during shutdown). This ensures the controller receives a 1516 * shutdown notification in case the system is shutdown before 1517 * reloading the driver. 1518 */ 1519 if (!gone) 1520 nvme_ctrlr_shutdown(ctrlr); 1521 1522 if (!gone) 1523 nvme_ctrlr_disable(ctrlr); 1524 1525 noadminq: 1526 if (ctrlr->taskqueue) 1527 taskqueue_free(ctrlr->taskqueue); 1528 1529 if (ctrlr->tag) 1530 bus_teardown_intr(ctrlr->dev, ctrlr->res, ctrlr->tag); 1531 1532 if (ctrlr->res) 1533 bus_release_resource(ctrlr->dev, SYS_RES_IRQ, 1534 rman_get_rid(ctrlr->res), ctrlr->res); 1535 1536 if (ctrlr->bar4_resource != NULL) { 1537 bus_release_resource(dev, SYS_RES_MEMORY, 1538 ctrlr->bar4_resource_id, ctrlr->bar4_resource); 1539 } 1540 1541 bus_release_resource(dev, SYS_RES_MEMORY, 1542 ctrlr->resource_id, ctrlr->resource); 1543 1544 nores: 1545 mtx_destroy(&ctrlr->lock); 1546 } 1547 1548 void 1549 nvme_ctrlr_shutdown(struct nvme_controller *ctrlr) 1550 { 1551 uint32_t cc; 1552 uint32_t csts; 1553 int timeout; 1554 1555 cc = nvme_mmio_read_4(ctrlr, cc); 1556 cc &= ~NVMEM(NVME_CC_REG_SHN); 1557 cc |= NVMEF(NVME_CC_REG_SHN, NVME_SHN_NORMAL); 1558 nvme_mmio_write_4(ctrlr, cc, cc); 1559 1560 timeout = ticks + (ctrlr->cdata.rtd3e == 0 ? 5 * hz : 1561 ((uint64_t)ctrlr->cdata.rtd3e * hz + 999999) / 1000000); 1562 while (1) { 1563 csts = nvme_mmio_read_4(ctrlr, csts); 1564 if (csts == NVME_GONE) /* Hot unplug. */ 1565 break; 1566 if (NVME_CSTS_GET_SHST(csts) == NVME_SHST_COMPLETE) 1567 break; 1568 if (timeout - ticks < 0) { 1569 nvme_printf(ctrlr, "shutdown timeout\n"); 1570 break; 1571 } 1572 pause("nvmeshut", 1); 1573 } 1574 } 1575 1576 void 1577 nvme_ctrlr_submit_admin_request(struct nvme_controller *ctrlr, 1578 struct nvme_request *req) 1579 { 1580 1581 nvme_qpair_submit_request(&ctrlr->adminq, req); 1582 } 1583 1584 void 1585 nvme_ctrlr_submit_io_request(struct nvme_controller *ctrlr, 1586 struct nvme_request *req) 1587 { 1588 struct nvme_qpair *qpair; 1589 1590 qpair = &ctrlr->ioq[QP(ctrlr, curcpu)]; 1591 nvme_qpair_submit_request(qpair, req); 1592 } 1593 1594 device_t 1595 nvme_ctrlr_get_device(struct nvme_controller *ctrlr) 1596 { 1597 1598 return (ctrlr->dev); 1599 } 1600 1601 const struct nvme_controller_data * 1602 nvme_ctrlr_get_data(struct nvme_controller *ctrlr) 1603 { 1604 1605 return (&ctrlr->cdata); 1606 } 1607 1608 int 1609 nvme_ctrlr_suspend(struct nvme_controller *ctrlr) 1610 { 1611 int to = hz; 1612 1613 /* 1614 * Can't touch failed controllers, so it's already suspended. 1615 */ 1616 if (ctrlr->is_failed) 1617 return (0); 1618 1619 /* 1620 * We don't want the reset taskqueue running, since it does similar 1621 * things, so prevent it from running after we start. Wait for any reset 1622 * that may have been started to complete. The reset process we follow 1623 * will ensure that any new I/O will queue and be given to the hardware 1624 * after we resume (though there should be none). 1625 */ 1626 while (atomic_cmpset_32(&ctrlr->is_resetting, 0, 1) == 0 && to-- > 0) 1627 pause("nvmesusp", 1); 1628 if (to <= 0) { 1629 nvme_printf(ctrlr, 1630 "Competing reset task didn't finish. Try again later.\n"); 1631 return (EWOULDBLOCK); 1632 } 1633 1634 if (ctrlr->hmb_nchunks > 0) 1635 nvme_ctrlr_hmb_enable(ctrlr, false, false); 1636 1637 /* 1638 * Per Section 7.6.2 of NVMe spec 1.4, to properly suspend, we need to 1639 * delete the hardware I/O queues, and then shutdown. This properly 1640 * flushes any metadata the drive may have stored so it can survive 1641 * having its power removed and prevents the unsafe shutdown count from 1642 * incriminating. Once we delete the qpairs, we have to disable them 1643 * before shutting down. 1644 */ 1645 nvme_ctrlr_delete_qpairs(ctrlr); 1646 nvme_ctrlr_disable_qpairs(ctrlr); 1647 nvme_ctrlr_shutdown(ctrlr); 1648 1649 return (0); 1650 } 1651 1652 int 1653 nvme_ctrlr_resume(struct nvme_controller *ctrlr) 1654 { 1655 1656 /* 1657 * Can't touch failed controllers, so nothing to do to resume. 1658 */ 1659 if (ctrlr->is_failed) 1660 return (0); 1661 1662 if (nvme_ctrlr_hw_reset(ctrlr) != 0) 1663 goto fail; 1664 1665 /* 1666 * Now that we've reset the hardware, we can restart the controller. Any 1667 * I/O that was pending is requeued. Any admin commands are aborted with 1668 * an error. Once we've restarted, take the controller out of reset. 1669 */ 1670 nvme_ctrlr_start(ctrlr, true); 1671 (void)atomic_cmpset_32(&ctrlr->is_resetting, 1, 0); 1672 1673 return (0); 1674 fail: 1675 /* 1676 * Since we can't bring the controller out of reset, announce and fail 1677 * the controller. However, we have to return success for the resume 1678 * itself, due to questionable APIs. 1679 */ 1680 nvme_printf(ctrlr, "Failed to reset on resume, failing.\n"); 1681 nvme_ctrlr_fail(ctrlr); 1682 (void)atomic_cmpset_32(&ctrlr->is_resetting, 1, 0); 1683 return (0); 1684 } 1685