nvme_ctrlr.c revision 248759
1/*- 2 * Copyright (C) 2012 Intel Corporation 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/dev/nvme/nvme_ctrlr.c 248759 2013-03-26 21:05:15Z jimharris $"); 29 30#include <sys/param.h> 31#include <sys/bus.h> 32#include <sys/conf.h> 33#include <sys/ioccom.h> 34#include <sys/smp.h> 35 36#include <dev/pci/pcireg.h> 37#include <dev/pci/pcivar.h> 38 39#include "nvme_private.h" 40 41static void nvme_ctrlr_construct_and_submit_aer(struct nvme_controller *ctrlr, 42 struct nvme_async_event_request *aer); 43 44static void 45nvme_ctrlr_cb(void *arg, const struct nvme_completion *status) 46{ 47 struct nvme_completion *cpl = arg; 48 struct mtx *mtx; 49 50 /* 51 * Copy status into the argument passed by the caller, so that 52 * the caller can check the status to determine if the 53 * the request passed or failed. 54 */ 55 memcpy(cpl, status, sizeof(*cpl)); 56 mtx = mtx_pool_find(mtxpool_sleep, cpl); 57 mtx_lock(mtx); 58 wakeup(cpl); 59 mtx_unlock(mtx); 60} 61 62static int 63nvme_ctrlr_allocate_bar(struct nvme_controller *ctrlr) 64{ 65 66 /* Chatham puts the NVMe MMRs behind BAR 2/3, not BAR 0/1. */ 67 if (pci_get_devid(ctrlr->dev) == CHATHAM_PCI_ID) 68 ctrlr->resource_id = PCIR_BAR(2); 69 else 70 ctrlr->resource_id = PCIR_BAR(0); 71 72 ctrlr->resource = bus_alloc_resource(ctrlr->dev, SYS_RES_MEMORY, 73 &ctrlr->resource_id, 0, ~0, 1, RF_ACTIVE); 74 75 if(ctrlr->resource == NULL) { 76 device_printf(ctrlr->dev, "unable to allocate pci resource\n"); 77 return (ENOMEM); 78 } 79 80 ctrlr->bus_tag = rman_get_bustag(ctrlr->resource); 81 ctrlr->bus_handle = rman_get_bushandle(ctrlr->resource); 82 ctrlr->regs = (struct nvme_registers *)ctrlr->bus_handle; 83 84 /* 85 * The NVMe spec allows for the MSI-X table to be placed behind 86 * BAR 4/5, separate from the control/doorbell registers. Always 87 * try to map this bar, because it must be mapped prior to calling 88 * pci_alloc_msix(). If the table isn't behind BAR 4/5, 89 * bus_alloc_resource() will just return NULL which is OK. 90 */ 91 ctrlr->bar4_resource_id = PCIR_BAR(4); 92 ctrlr->bar4_resource = bus_alloc_resource(ctrlr->dev, SYS_RES_MEMORY, 93 &ctrlr->bar4_resource_id, 0, ~0, 1, RF_ACTIVE); 94 95 return (0); 96} 97 98#ifdef CHATHAM2 99static int 100nvme_ctrlr_allocate_chatham_bar(struct nvme_controller *ctrlr) 101{ 102 103 ctrlr->chatham_resource_id = PCIR_BAR(CHATHAM_CONTROL_BAR); 104 ctrlr->chatham_resource = bus_alloc_resource(ctrlr->dev, 105 SYS_RES_MEMORY, &ctrlr->chatham_resource_id, 0, ~0, 1, 106 RF_ACTIVE); 107 108 if(ctrlr->chatham_resource == NULL) { 109 device_printf(ctrlr->dev, "unable to alloc pci resource\n"); 110 return (ENOMEM); 111 } 112 113 ctrlr->chatham_bus_tag = rman_get_bustag(ctrlr->chatham_resource); 114 ctrlr->chatham_bus_handle = 115 rman_get_bushandle(ctrlr->chatham_resource); 116 117 return (0); 118} 119 120static void 121nvme_ctrlr_setup_chatham(struct nvme_controller *ctrlr) 122{ 123 uint64_t reg1, reg2, reg3; 124 uint64_t temp1, temp2; 125 uint32_t temp3; 126 uint32_t use_flash_timings = 0; 127 128 DELAY(10000); 129 130 temp3 = chatham_read_4(ctrlr, 0x8080); 131 132 device_printf(ctrlr->dev, "Chatham version: 0x%x\n", temp3); 133 134 ctrlr->chatham_lbas = chatham_read_4(ctrlr, 0x8068) - 0x110; 135 ctrlr->chatham_size = ctrlr->chatham_lbas * 512; 136 137 device_printf(ctrlr->dev, "Chatham size: %jd\n", 138 (intmax_t)ctrlr->chatham_size); 139 140 reg1 = reg2 = reg3 = ctrlr->chatham_size - 1; 141 142 TUNABLE_INT_FETCH("hw.nvme.use_flash_timings", &use_flash_timings); 143 if (use_flash_timings) { 144 device_printf(ctrlr->dev, "Chatham: using flash timings\n"); 145 temp1 = 0x00001b58000007d0LL; 146 temp2 = 0x000000cb00000131LL; 147 } else { 148 device_printf(ctrlr->dev, "Chatham: using DDR timings\n"); 149 temp1 = temp2 = 0x0LL; 150 } 151 152 chatham_write_8(ctrlr, 0x8000, reg1); 153 chatham_write_8(ctrlr, 0x8008, reg2); 154 chatham_write_8(ctrlr, 0x8010, reg3); 155 156 chatham_write_8(ctrlr, 0x8020, temp1); 157 temp3 = chatham_read_4(ctrlr, 0x8020); 158 159 chatham_write_8(ctrlr, 0x8028, temp2); 160 temp3 = chatham_read_4(ctrlr, 0x8028); 161 162 chatham_write_8(ctrlr, 0x8030, temp1); 163 chatham_write_8(ctrlr, 0x8038, temp2); 164 chatham_write_8(ctrlr, 0x8040, temp1); 165 chatham_write_8(ctrlr, 0x8048, temp2); 166 chatham_write_8(ctrlr, 0x8050, temp1); 167 chatham_write_8(ctrlr, 0x8058, temp2); 168 169 DELAY(10000); 170} 171 172static void 173nvme_chatham_populate_cdata(struct nvme_controller *ctrlr) 174{ 175 struct nvme_controller_data *cdata; 176 177 cdata = &ctrlr->cdata; 178 179 cdata->vid = 0x8086; 180 cdata->ssvid = 0x2011; 181 182 /* 183 * Chatham2 puts garbage data in these fields when we 184 * invoke IDENTIFY_CONTROLLER, so we need to re-zero 185 * the fields before calling bcopy(). 186 */ 187 memset(cdata->sn, 0, sizeof(cdata->sn)); 188 memcpy(cdata->sn, "2012", strlen("2012")); 189 memset(cdata->mn, 0, sizeof(cdata->mn)); 190 memcpy(cdata->mn, "CHATHAM2", strlen("CHATHAM2")); 191 memset(cdata->fr, 0, sizeof(cdata->fr)); 192 memcpy(cdata->fr, "0", strlen("0")); 193 cdata->rab = 8; 194 cdata->aerl = 3; 195 cdata->lpa.ns_smart = 1; 196 cdata->sqes.min = 6; 197 cdata->sqes.max = 6; 198 cdata->sqes.min = 4; 199 cdata->sqes.max = 4; 200 cdata->nn = 1; 201 202 /* Chatham2 doesn't support DSM command */ 203 cdata->oncs.dsm = 0; 204 205 cdata->vwc.present = 1; 206} 207#endif /* CHATHAM2 */ 208 209static void 210nvme_ctrlr_construct_admin_qpair(struct nvme_controller *ctrlr) 211{ 212 struct nvme_qpair *qpair; 213 uint32_t num_entries; 214 215 qpair = &ctrlr->adminq; 216 217 num_entries = NVME_ADMIN_ENTRIES; 218 TUNABLE_INT_FETCH("hw.nvme.admin_entries", &num_entries); 219 /* 220 * If admin_entries was overridden to an invalid value, revert it 221 * back to our default value. 222 */ 223 if (num_entries < NVME_MIN_ADMIN_ENTRIES || 224 num_entries > NVME_MAX_ADMIN_ENTRIES) { 225 printf("nvme: invalid hw.nvme.admin_entries=%d specified\n", 226 num_entries); 227 num_entries = NVME_ADMIN_ENTRIES; 228 } 229 230 /* 231 * The admin queue's max xfer size is treated differently than the 232 * max I/O xfer size. 16KB is sufficient here - maybe even less? 233 */ 234 nvme_qpair_construct(qpair, 235 0, /* qpair ID */ 236 0, /* vector */ 237 num_entries, 238 NVME_ADMIN_TRACKERS, 239 16*1024, /* max xfer size */ 240 ctrlr); 241} 242 243static int 244nvme_ctrlr_construct_io_qpairs(struct nvme_controller *ctrlr) 245{ 246 struct nvme_qpair *qpair; 247 union cap_lo_register cap_lo; 248 int i, num_entries, num_trackers; 249 250 num_entries = NVME_IO_ENTRIES; 251 TUNABLE_INT_FETCH("hw.nvme.io_entries", &num_entries); 252 253 /* 254 * NVMe spec sets a hard limit of 64K max entries, but 255 * devices may specify a smaller limit, so we need to check 256 * the MQES field in the capabilities register. 257 */ 258 cap_lo.raw = nvme_mmio_read_4(ctrlr, cap_lo); 259 num_entries = min(num_entries, cap_lo.bits.mqes+1); 260 261 num_trackers = NVME_IO_TRACKERS; 262 TUNABLE_INT_FETCH("hw.nvme.io_trackers", &num_trackers); 263 264 num_trackers = max(num_trackers, NVME_MIN_IO_TRACKERS); 265 num_trackers = min(num_trackers, NVME_MAX_IO_TRACKERS); 266 /* 267 * No need to have more trackers than entries in the submit queue. 268 * Note also that for a queue size of N, we can only have (N-1) 269 * commands outstanding, hence the "-1" here. 270 */ 271 num_trackers = min(num_trackers, (num_entries-1)); 272 273 ctrlr->max_xfer_size = NVME_MAX_XFER_SIZE; 274 TUNABLE_INT_FETCH("hw.nvme.max_xfer_size", &ctrlr->max_xfer_size); 275 /* 276 * Check that tunable doesn't specify a size greater than what our 277 * driver supports, and is an even PAGE_SIZE multiple. 278 */ 279 if (ctrlr->max_xfer_size > NVME_MAX_XFER_SIZE || 280 ctrlr->max_xfer_size % PAGE_SIZE) 281 ctrlr->max_xfer_size = NVME_MAX_XFER_SIZE; 282 283 ctrlr->ioq = malloc(ctrlr->num_io_queues * sizeof(struct nvme_qpair), 284 M_NVME, M_ZERO | M_NOWAIT); 285 286 if (ctrlr->ioq == NULL) 287 return (ENOMEM); 288 289 for (i = 0; i < ctrlr->num_io_queues; i++) { 290 qpair = &ctrlr->ioq[i]; 291 292 /* 293 * Admin queue has ID=0. IO queues start at ID=1 - 294 * hence the 'i+1' here. 295 * 296 * For I/O queues, use the controller-wide max_xfer_size 297 * calculated in nvme_attach(). 298 */ 299 nvme_qpair_construct(qpair, 300 i+1, /* qpair ID */ 301 ctrlr->msix_enabled ? i+1 : 0, /* vector */ 302 num_entries, 303 num_trackers, 304 ctrlr->max_xfer_size, 305 ctrlr); 306 307 if (ctrlr->per_cpu_io_queues) 308 bus_bind_intr(ctrlr->dev, qpair->res, i); 309 } 310 311 return (0); 312} 313 314static int 315nvme_ctrlr_wait_for_ready(struct nvme_controller *ctrlr) 316{ 317 int ms_waited; 318 union cc_register cc; 319 union csts_register csts; 320 321 cc.raw = nvme_mmio_read_4(ctrlr, cc); 322 csts.raw = nvme_mmio_read_4(ctrlr, csts); 323 324 if (!cc.bits.en) { 325 device_printf(ctrlr->dev, "%s called with cc.en = 0\n", 326 __func__); 327 return (ENXIO); 328 } 329 330 ms_waited = 0; 331 332 while (!csts.bits.rdy) { 333 DELAY(1000); 334 if (ms_waited++ > ctrlr->ready_timeout_in_ms) { 335 device_printf(ctrlr->dev, "controller did not become " 336 "ready within %d ms\n", ctrlr->ready_timeout_in_ms); 337 return (ENXIO); 338 } 339 csts.raw = nvme_mmio_read_4(ctrlr, csts); 340 } 341 342 return (0); 343} 344 345static void 346nvme_ctrlr_disable(struct nvme_controller *ctrlr) 347{ 348 union cc_register cc; 349 union csts_register csts; 350 351 cc.raw = nvme_mmio_read_4(ctrlr, cc); 352 csts.raw = nvme_mmio_read_4(ctrlr, csts); 353 354 if (cc.bits.en == 1 && csts.bits.rdy == 0) 355 nvme_ctrlr_wait_for_ready(ctrlr); 356 357 cc.bits.en = 0; 358 nvme_mmio_write_4(ctrlr, cc, cc.raw); 359 DELAY(5000); 360} 361 362static int 363nvme_ctrlr_enable(struct nvme_controller *ctrlr) 364{ 365 union cc_register cc; 366 union csts_register csts; 367 union aqa_register aqa; 368 369 cc.raw = nvme_mmio_read_4(ctrlr, cc); 370 csts.raw = nvme_mmio_read_4(ctrlr, csts); 371 372 if (cc.bits.en == 1) { 373 if (csts.bits.rdy == 1) 374 return (0); 375 else 376 return (nvme_ctrlr_wait_for_ready(ctrlr)); 377 } 378 379 nvme_mmio_write_8(ctrlr, asq, ctrlr->adminq.cmd_bus_addr); 380 DELAY(5000); 381 nvme_mmio_write_8(ctrlr, acq, ctrlr->adminq.cpl_bus_addr); 382 DELAY(5000); 383 384 aqa.raw = 0; 385 /* acqs and asqs are 0-based. */ 386 aqa.bits.acqs = ctrlr->adminq.num_entries-1; 387 aqa.bits.asqs = ctrlr->adminq.num_entries-1; 388 nvme_mmio_write_4(ctrlr, aqa, aqa.raw); 389 DELAY(5000); 390 391 cc.bits.en = 1; 392 cc.bits.css = 0; 393 cc.bits.ams = 0; 394 cc.bits.shn = 0; 395 cc.bits.iosqes = 6; /* SQ entry size == 64 == 2^6 */ 396 cc.bits.iocqes = 4; /* CQ entry size == 16 == 2^4 */ 397 398 /* This evaluates to 0, which is according to spec. */ 399 cc.bits.mps = (PAGE_SIZE >> 13); 400 401 nvme_mmio_write_4(ctrlr, cc, cc.raw); 402 DELAY(5000); 403 404 return (nvme_ctrlr_wait_for_ready(ctrlr)); 405} 406 407int 408nvme_ctrlr_hw_reset(struct nvme_controller *ctrlr) 409{ 410 int i; 411 412 nvme_admin_qpair_disable(&ctrlr->adminq); 413 for (i = 0; i < ctrlr->num_io_queues; i++) 414 nvme_io_qpair_disable(&ctrlr->ioq[i]); 415 416 DELAY(100*1000); 417 418 nvme_ctrlr_disable(ctrlr); 419 return (nvme_ctrlr_enable(ctrlr)); 420} 421 422void 423nvme_ctrlr_reset(struct nvme_controller *ctrlr) 424{ 425 int cmpset; 426 427 cmpset = atomic_cmpset_32(&ctrlr->is_resetting, 0, 1); 428 429 if (cmpset == 0) 430 /* Controller is already resetting. */ 431 return; 432 433 taskqueue_enqueue(ctrlr->taskqueue, &ctrlr->reset_task); 434} 435 436static int 437nvme_ctrlr_identify(struct nvme_controller *ctrlr) 438{ 439 struct mtx *mtx; 440 struct nvme_completion cpl; 441 int status; 442 443 mtx = mtx_pool_find(mtxpool_sleep, &cpl); 444 445 mtx_lock(mtx); 446 nvme_ctrlr_cmd_identify_controller(ctrlr, &ctrlr->cdata, 447 nvme_ctrlr_cb, &cpl); 448 status = msleep(&cpl, mtx, PRIBIO, "nvme_start", hz*5); 449 mtx_unlock(mtx); 450 if ((status != 0) || nvme_completion_is_error(&cpl)) { 451 printf("nvme_identify_controller failed!\n"); 452 return (ENXIO); 453 } 454 455#ifdef CHATHAM2 456 if (pci_get_devid(ctrlr->dev) == CHATHAM_PCI_ID) 457 nvme_chatham_populate_cdata(ctrlr); 458#endif 459 460 return (0); 461} 462 463static int 464nvme_ctrlr_set_num_qpairs(struct nvme_controller *ctrlr) 465{ 466 struct mtx *mtx; 467 struct nvme_completion cpl; 468 int cq_allocated, sq_allocated, status; 469 470 mtx = mtx_pool_find(mtxpool_sleep, &cpl); 471 472 mtx_lock(mtx); 473 nvme_ctrlr_cmd_set_num_queues(ctrlr, ctrlr->num_io_queues, 474 nvme_ctrlr_cb, &cpl); 475 status = msleep(&cpl, mtx, PRIBIO, "nvme_start", hz*5); 476 mtx_unlock(mtx); 477 if ((status != 0) || nvme_completion_is_error(&cpl)) { 478 printf("nvme_set_num_queues failed!\n"); 479 return (ENXIO); 480 } 481 482 /* 483 * Data in cdw0 is 0-based. 484 * Lower 16-bits indicate number of submission queues allocated. 485 * Upper 16-bits indicate number of completion queues allocated. 486 */ 487 sq_allocated = (cpl.cdw0 & 0xFFFF) + 1; 488 cq_allocated = (cpl.cdw0 >> 16) + 1; 489 490 /* 491 * Check that the controller was able to allocate the number of 492 * queues we requested. If not, revert to one IO queue. 493 */ 494 if (sq_allocated < ctrlr->num_io_queues || 495 cq_allocated < ctrlr->num_io_queues) { 496 ctrlr->num_io_queues = 1; 497 ctrlr->per_cpu_io_queues = 0; 498 499 /* TODO: destroy extra queues that were created 500 * previously but now found to be not needed. 501 */ 502 } 503 504 return (0); 505} 506 507static int 508nvme_ctrlr_create_qpairs(struct nvme_controller *ctrlr) 509{ 510 struct mtx *mtx; 511 struct nvme_qpair *qpair; 512 struct nvme_completion cpl; 513 int i, status; 514 515 mtx = mtx_pool_find(mtxpool_sleep, &cpl); 516 517 for (i = 0; i < ctrlr->num_io_queues; i++) { 518 qpair = &ctrlr->ioq[i]; 519 520 mtx_lock(mtx); 521 nvme_ctrlr_cmd_create_io_cq(ctrlr, qpair, qpair->vector, 522 nvme_ctrlr_cb, &cpl); 523 status = msleep(&cpl, mtx, PRIBIO, "nvme_start", hz*5); 524 mtx_unlock(mtx); 525 if ((status != 0) || nvme_completion_is_error(&cpl)) { 526 printf("nvme_create_io_cq failed!\n"); 527 return (ENXIO); 528 } 529 530 mtx_lock(mtx); 531 nvme_ctrlr_cmd_create_io_sq(qpair->ctrlr, qpair, 532 nvme_ctrlr_cb, &cpl); 533 status = msleep(&cpl, mtx, PRIBIO, "nvme_start", hz*5); 534 mtx_unlock(mtx); 535 if ((status != 0) || nvme_completion_is_error(&cpl)) { 536 printf("nvme_create_io_sq failed!\n"); 537 return (ENXIO); 538 } 539 } 540 541 return (0); 542} 543 544static int 545nvme_ctrlr_construct_namespaces(struct nvme_controller *ctrlr) 546{ 547 struct nvme_namespace *ns; 548 int i, status; 549 550 for (i = 0; i < ctrlr->cdata.nn; i++) { 551 ns = &ctrlr->ns[i]; 552 status = nvme_ns_construct(ns, i+1, ctrlr); 553 if (status != 0) 554 return (status); 555 } 556 557 return (0); 558} 559 560static boolean_t 561is_log_page_id_valid(uint8_t page_id) 562{ 563 564 switch (page_id) { 565 case NVME_LOG_ERROR: 566 case NVME_LOG_HEALTH_INFORMATION: 567 case NVME_LOG_FIRMWARE_SLOT: 568 return (TRUE); 569 } 570 571 return (FALSE); 572} 573 574static uint32_t 575nvme_ctrlr_get_log_page_size(struct nvme_controller *ctrlr, uint8_t page_id) 576{ 577 uint32_t log_page_size; 578 579 switch (page_id) { 580 case NVME_LOG_ERROR: 581 log_page_size = min( 582 sizeof(struct nvme_error_information_entry) * 583 ctrlr->cdata.elpe, 584 NVME_MAX_AER_LOG_SIZE); 585 break; 586 case NVME_LOG_HEALTH_INFORMATION: 587 log_page_size = sizeof(struct nvme_health_information_page); 588 break; 589 case NVME_LOG_FIRMWARE_SLOT: 590 log_page_size = sizeof(struct nvme_firmware_page); 591 break; 592 default: 593 log_page_size = 0; 594 break; 595 } 596 597 return (log_page_size); 598} 599 600static void 601nvme_ctrlr_async_event_log_page_cb(void *arg, const struct nvme_completion *cpl) 602{ 603 struct nvme_async_event_request *aer = arg; 604 605 nvme_notify_async_consumers(aer->ctrlr, &aer->cpl); 606 607 /* 608 * Repost another asynchronous event request to replace the one 609 * that just completed. 610 */ 611 nvme_ctrlr_construct_and_submit_aer(aer->ctrlr, aer); 612} 613 614static void 615nvme_ctrlr_async_event_cb(void *arg, const struct nvme_completion *cpl) 616{ 617 struct nvme_async_event_request *aer = arg; 618 uint8_t log_page_id; 619 620 if (cpl->status.sc == NVME_SC_ABORTED_SQ_DELETION) { 621 /* 622 * This is simulated when controller is being shut down, to 623 * effectively abort outstanding asynchronous event requests 624 * and make sure all memory is freed. Do not repost the 625 * request in this case. 626 */ 627 return; 628 } 629 630 printf("Asynchronous event occurred.\n"); 631 632 /* Associated log page is in bits 23:16 of completion entry dw0. */ 633 log_page_id = (cpl->cdw0 & 0xFF0000) >> 16; 634 635 if (is_log_page_id_valid(log_page_id)) { 636 aer->log_page_size = nvme_ctrlr_get_log_page_size(aer->ctrlr, 637 log_page_id); 638 memcpy(&aer->cpl, cpl, sizeof(*cpl)); 639 nvme_ctrlr_cmd_get_log_page(aer->ctrlr, log_page_id, 640 NVME_GLOBAL_NAMESPACE_TAG, aer->log_page_buffer, 641 aer->log_page_size, nvme_ctrlr_async_event_log_page_cb, 642 aer); 643 /* Wait to notify consumers until after log page is fetched. */ 644 } else { 645 nvme_notify_async_consumers(aer->ctrlr, cpl); 646 647 /* 648 * Repost another asynchronous event request to replace the one 649 * that just completed. 650 */ 651 nvme_ctrlr_construct_and_submit_aer(aer->ctrlr, aer); 652 } 653} 654 655static void 656nvme_ctrlr_construct_and_submit_aer(struct nvme_controller *ctrlr, 657 struct nvme_async_event_request *aer) 658{ 659 struct nvme_request *req; 660 661 aer->ctrlr = ctrlr; 662 req = nvme_allocate_request(NULL, 0, nvme_ctrlr_async_event_cb, aer); 663 aer->req = req; 664 665 /* 666 * Disable timeout here, since asynchronous event requests should by 667 * nature never be timed out. 668 */ 669 req->timeout = FALSE; 670 req->cmd.opc = NVME_OPC_ASYNC_EVENT_REQUEST; 671 nvme_ctrlr_submit_admin_request(ctrlr, req); 672} 673 674static void 675nvme_ctrlr_configure_aer(struct nvme_controller *ctrlr) 676{ 677 union nvme_critical_warning_state state; 678 struct nvme_async_event_request *aer; 679 uint32_t i; 680 681 state.raw = 0xFF; 682 state.bits.reserved = 0; 683 nvme_ctrlr_cmd_set_async_event_config(ctrlr, state, NULL, NULL); 684 685 /* aerl is a zero-based value, so we need to add 1 here. */ 686 ctrlr->num_aers = min(NVME_MAX_ASYNC_EVENTS, (ctrlr->cdata.aerl+1)); 687 688 /* Chatham doesn't support AERs. */ 689 if (pci_get_devid(ctrlr->dev) == CHATHAM_PCI_ID) 690 ctrlr->num_aers = 0; 691 692 for (i = 0; i < ctrlr->num_aers; i++) { 693 aer = &ctrlr->aer[i]; 694 nvme_ctrlr_construct_and_submit_aer(ctrlr, aer); 695 } 696} 697 698static void 699nvme_ctrlr_configure_int_coalescing(struct nvme_controller *ctrlr) 700{ 701 702 ctrlr->int_coal_time = 0; 703 TUNABLE_INT_FETCH("hw.nvme.int_coal_time", 704 &ctrlr->int_coal_time); 705 706 ctrlr->int_coal_threshold = 0; 707 TUNABLE_INT_FETCH("hw.nvme.int_coal_threshold", 708 &ctrlr->int_coal_threshold); 709 710 nvme_ctrlr_cmd_set_interrupt_coalescing(ctrlr, ctrlr->int_coal_time, 711 ctrlr->int_coal_threshold, NULL, NULL); 712} 713 714void 715nvme_ctrlr_start(void *ctrlr_arg) 716{ 717 struct nvme_controller *ctrlr = ctrlr_arg; 718 int i; 719 720 nvme_admin_qpair_enable(&ctrlr->adminq); 721 722 if (nvme_ctrlr_identify(ctrlr) != 0) 723 goto err; 724 725 if (nvme_ctrlr_set_num_qpairs(ctrlr) != 0) 726 goto err; 727 728 if (nvme_ctrlr_create_qpairs(ctrlr) != 0) 729 goto err; 730 731 if (nvme_ctrlr_construct_namespaces(ctrlr) != 0) 732 goto err; 733 734 nvme_ctrlr_configure_aer(ctrlr); 735 nvme_ctrlr_configure_int_coalescing(ctrlr); 736 737 for (i = 0; i < ctrlr->num_io_queues; i++) 738 nvme_io_qpair_enable(&ctrlr->ioq[i]); 739 740 ctrlr->is_started = TRUE; 741 742err: 743 744 if (ctrlr->num_start_attempts == 0) { 745 /* 746 * Initialize sysctls, even if controller failed to start, to 747 * assist with debugging admin queue pair. Only run this 748 * code on the initial start attempt though, and not 749 * subsequent start attempts due to controller-level resets. 750 * 751 */ 752 nvme_sysctl_initialize_ctrlr(ctrlr); 753 config_intrhook_disestablish(&ctrlr->config_hook); 754 } 755 756 ctrlr->num_start_attempts++; 757} 758 759static void 760nvme_ctrlr_reset_task(void *arg, int pending) 761{ 762 struct nvme_controller *ctrlr = arg; 763 int status; 764 765 device_printf(ctrlr->dev, "resetting controller"); 766 status = nvme_ctrlr_hw_reset(ctrlr); 767 /* 768 * Use pause instead of DELAY, so that we yield to any nvme interrupt 769 * handlers on this CPU that were blocked on a qpair lock. We want 770 * all nvme interrupts completed before proceeding with restarting the 771 * controller. 772 * 773 * XXX - any way to guarantee the interrupt handlers have quiesced? 774 */ 775 pause("nvmereset", hz / 10); 776 if (status == 0) 777 nvme_ctrlr_start(ctrlr); 778 779 atomic_cmpset_32(&ctrlr->is_resetting, 1, 0); 780} 781 782static void 783nvme_ctrlr_intx_handler(void *arg) 784{ 785 struct nvme_controller *ctrlr = arg; 786 787 nvme_mmio_write_4(ctrlr, intms, 1); 788 789 nvme_qpair_process_completions(&ctrlr->adminq); 790 791 if (ctrlr->ioq[0].cpl) 792 nvme_qpair_process_completions(&ctrlr->ioq[0]); 793 794 nvme_mmio_write_4(ctrlr, intmc, 1); 795} 796 797static int 798nvme_ctrlr_configure_intx(struct nvme_controller *ctrlr) 799{ 800 801 ctrlr->num_io_queues = 1; 802 ctrlr->per_cpu_io_queues = 0; 803 ctrlr->rid = 0; 804 ctrlr->res = bus_alloc_resource_any(ctrlr->dev, SYS_RES_IRQ, 805 &ctrlr->rid, RF_SHAREABLE | RF_ACTIVE); 806 807 if (ctrlr->res == NULL) { 808 device_printf(ctrlr->dev, "unable to allocate shared IRQ\n"); 809 return (ENOMEM); 810 } 811 812 bus_setup_intr(ctrlr->dev, ctrlr->res, 813 INTR_TYPE_MISC | INTR_MPSAFE, NULL, nvme_ctrlr_intx_handler, 814 ctrlr, &ctrlr->tag); 815 816 if (ctrlr->tag == NULL) { 817 device_printf(ctrlr->dev, 818 "unable to setup legacy interrupt handler\n"); 819 return (ENOMEM); 820 } 821 822 return (0); 823} 824 825static int 826nvme_ctrlr_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag, 827 struct thread *td) 828{ 829 struct nvme_controller *ctrlr; 830 struct nvme_completion cpl; 831 struct mtx *mtx; 832 833 ctrlr = cdev->si_drv1; 834 835 switch (cmd) { 836 case NVME_IDENTIFY_CONTROLLER: 837#ifdef CHATHAM2 838 /* 839 * Don't refresh data on Chatham, since Chatham returns 840 * garbage on IDENTIFY anyways. 841 */ 842 if (pci_get_devid(ctrlr->dev) == CHATHAM_PCI_ID) { 843 memcpy(arg, &ctrlr->cdata, sizeof(ctrlr->cdata)); 844 break; 845 } 846#endif 847 /* Refresh data before returning to user. */ 848 mtx = mtx_pool_find(mtxpool_sleep, &cpl); 849 mtx_lock(mtx); 850 nvme_ctrlr_cmd_identify_controller(ctrlr, &ctrlr->cdata, 851 nvme_ctrlr_cb, &cpl); 852 msleep(&cpl, mtx, PRIBIO, "nvme_ioctl", 0); 853 mtx_unlock(mtx); 854 if (nvme_completion_is_error(&cpl)) 855 return (ENXIO); 856 memcpy(arg, &ctrlr->cdata, sizeof(ctrlr->cdata)); 857 break; 858 case NVME_RESET_CONTROLLER: 859 nvme_ctrlr_reset(ctrlr); 860 break; 861 default: 862 return (ENOTTY); 863 } 864 865 return (0); 866} 867 868static struct cdevsw nvme_ctrlr_cdevsw = { 869 .d_version = D_VERSION, 870 .d_flags = 0, 871 .d_ioctl = nvme_ctrlr_ioctl 872}; 873 874int 875nvme_ctrlr_construct(struct nvme_controller *ctrlr, device_t dev) 876{ 877 union cap_lo_register cap_lo; 878 union cap_hi_register cap_hi; 879 int num_vectors, per_cpu_io_queues, status = 0; 880 int timeout_period; 881 882 ctrlr->dev = dev; 883 ctrlr->is_started = FALSE; 884 ctrlr->num_start_attempts = 0; 885 886 status = nvme_ctrlr_allocate_bar(ctrlr); 887 888 if (status != 0) 889 return (status); 890 891#ifdef CHATHAM2 892 if (pci_get_devid(dev) == CHATHAM_PCI_ID) { 893 status = nvme_ctrlr_allocate_chatham_bar(ctrlr); 894 if (status != 0) 895 return (status); 896 nvme_ctrlr_setup_chatham(ctrlr); 897 } 898#endif 899 900 /* 901 * Software emulators may set the doorbell stride to something 902 * other than zero, but this driver is not set up to handle that. 903 */ 904 cap_hi.raw = nvme_mmio_read_4(ctrlr, cap_hi); 905 if (cap_hi.bits.dstrd != 0) 906 return (ENXIO); 907 908 /* Get ready timeout value from controller, in units of 500ms. */ 909 cap_lo.raw = nvme_mmio_read_4(ctrlr, cap_lo); 910 ctrlr->ready_timeout_in_ms = cap_lo.bits.to * 500; 911 912 timeout_period = NVME_DEFAULT_TIMEOUT_PERIOD; 913 TUNABLE_INT_FETCH("hw.nvme.timeout_period", &timeout_period); 914 timeout_period = min(timeout_period, NVME_MAX_TIMEOUT_PERIOD); 915 timeout_period = max(timeout_period, NVME_MIN_TIMEOUT_PERIOD); 916 ctrlr->timeout_period = timeout_period; 917 918 per_cpu_io_queues = 1; 919 TUNABLE_INT_FETCH("hw.nvme.per_cpu_io_queues", &per_cpu_io_queues); 920 ctrlr->per_cpu_io_queues = per_cpu_io_queues ? TRUE : FALSE; 921 922 if (ctrlr->per_cpu_io_queues) 923 ctrlr->num_io_queues = mp_ncpus; 924 else 925 ctrlr->num_io_queues = 1; 926 927 ctrlr->force_intx = 0; 928 TUNABLE_INT_FETCH("hw.nvme.force_intx", &ctrlr->force_intx); 929 930 ctrlr->enable_aborts = 0; 931 TUNABLE_INT_FETCH("hw.nvme.enable_aborts", &ctrlr->enable_aborts); 932 933 ctrlr->msix_enabled = 1; 934 935 if (ctrlr->force_intx) { 936 ctrlr->msix_enabled = 0; 937 goto intx; 938 } 939 940 /* One vector per IO queue, plus one vector for admin queue. */ 941 num_vectors = ctrlr->num_io_queues + 1; 942 943 if (pci_msix_count(dev) < num_vectors) { 944 ctrlr->msix_enabled = 0; 945 goto intx; 946 } 947 948 if (pci_alloc_msix(dev, &num_vectors) != 0) 949 ctrlr->msix_enabled = 0; 950 951intx: 952 953 if (!ctrlr->msix_enabled) 954 nvme_ctrlr_configure_intx(ctrlr); 955 956 nvme_ctrlr_construct_admin_qpair(ctrlr); 957 958 status = nvme_ctrlr_construct_io_qpairs(ctrlr); 959 960 if (status != 0) 961 return (status); 962 963 ctrlr->cdev = make_dev(&nvme_ctrlr_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, 964 "nvme%d", device_get_unit(dev)); 965 966 if (ctrlr->cdev == NULL) 967 return (ENXIO); 968 969 ctrlr->cdev->si_drv1 = (void *)ctrlr; 970 971 TASK_INIT(&ctrlr->reset_task, 0, nvme_ctrlr_reset_task, ctrlr); 972 ctrlr->taskqueue = taskqueue_create("nvme_taskq", M_WAITOK, 973 taskqueue_thread_enqueue, &ctrlr->taskqueue); 974 taskqueue_start_threads(&ctrlr->taskqueue, 1, PI_DISK, "nvme taskq"); 975 976 ctrlr->is_resetting = 0; 977 978 return (0); 979} 980 981void 982nvme_ctrlr_destruct(struct nvme_controller *ctrlr, device_t dev) 983{ 984 int i; 985 986 taskqueue_free(ctrlr->taskqueue); 987 988 for (i = 0; i < NVME_MAX_NAMESPACES; i++) 989 nvme_ns_destruct(&ctrlr->ns[i]); 990 991 if (ctrlr->cdev) 992 destroy_dev(ctrlr->cdev); 993 994 for (i = 0; i < ctrlr->num_io_queues; i++) { 995 nvme_io_qpair_destroy(&ctrlr->ioq[i]); 996 } 997 998 free(ctrlr->ioq, M_NVME); 999 1000 nvme_admin_qpair_destroy(&ctrlr->adminq); 1001 1002 if (ctrlr->resource != NULL) { 1003 bus_release_resource(dev, SYS_RES_MEMORY, 1004 ctrlr->resource_id, ctrlr->resource); 1005 } 1006 1007 if (ctrlr->bar4_resource != NULL) { 1008 bus_release_resource(dev, SYS_RES_MEMORY, 1009 ctrlr->bar4_resource_id, ctrlr->bar4_resource); 1010 } 1011 1012#ifdef CHATHAM2 1013 if (ctrlr->chatham_resource != NULL) { 1014 bus_release_resource(dev, SYS_RES_MEMORY, 1015 ctrlr->chatham_resource_id, ctrlr->chatham_resource); 1016 } 1017#endif 1018 1019 if (ctrlr->tag) 1020 bus_teardown_intr(ctrlr->dev, ctrlr->res, ctrlr->tag); 1021 1022 if (ctrlr->res) 1023 bus_release_resource(ctrlr->dev, SYS_RES_IRQ, 1024 rman_get_rid(ctrlr->res), ctrlr->res); 1025 1026 if (ctrlr->msix_enabled) 1027 pci_release_msi(dev); 1028} 1029 1030void 1031nvme_ctrlr_submit_admin_request(struct nvme_controller *ctrlr, 1032 struct nvme_request *req) 1033{ 1034 1035 nvme_qpair_submit_request(&ctrlr->adminq, req); 1036} 1037 1038void 1039nvme_ctrlr_submit_io_request(struct nvme_controller *ctrlr, 1040 struct nvme_request *req) 1041{ 1042 struct nvme_qpair *qpair; 1043 1044 if (ctrlr->per_cpu_io_queues) 1045 qpair = &ctrlr->ioq[curcpu]; 1046 else 1047 qpair = &ctrlr->ioq[0]; 1048 1049 nvme_qpair_submit_request(qpair, req); 1050} 1051 1052device_t 1053nvme_ctrlr_get_device(struct nvme_controller *ctrlr) 1054{ 1055 1056 return (ctrlr->dev); 1057} 1058 1059const struct nvme_controller_data * 1060nvme_ctrlr_get_data(struct nvme_controller *ctrlr) 1061{ 1062 1063 return (&ctrlr->cdata); 1064} 1065