1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2015 Netflix, Inc. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * derived from ata_xpt.c: Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org> 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/bus.h> 35 #include <sys/endian.h> 36 #include <sys/systm.h> 37 #include <sys/types.h> 38 #include <sys/malloc.h> 39 #include <sys/kernel.h> 40 #include <sys/time.h> 41 #include <sys/conf.h> 42 #include <sys/fcntl.h> 43 #include <sys/sbuf.h> 44 45 #include <sys/lock.h> 46 #include <sys/mutex.h> 47 #include <sys/sysctl.h> 48 49 #include <cam/cam.h> 50 #include <cam/cam_ccb.h> 51 #include <cam/cam_queue.h> 52 #include <cam/cam_periph.h> 53 #include <cam/cam_sim.h> 54 #include <cam/cam_xpt.h> 55 #include <cam/cam_xpt_sim.h> 56 #include <cam/cam_xpt_periph.h> 57 #include <cam/cam_xpt_internal.h> 58 #include <cam/cam_debug.h> 59 60 #include <cam/scsi/scsi_all.h> 61 #include <cam/scsi/scsi_message.h> 62 #include <cam/nvme/nvme_all.h> 63 #include <machine/stdarg.h> /* for xpt_print below */ 64 #include "opt_cam.h" 65 66 struct nvme_quirk_entry { 67 u_int quirks; 68 #define CAM_QUIRK_MAXTAGS 1 69 u_int mintags; 70 u_int maxtags; 71 }; 72 73 /* Not even sure why we need this */ 74 static periph_init_t nvme_probe_periph_init; 75 76 static struct periph_driver nvme_probe_driver = 77 { 78 nvme_probe_periph_init, "nvme_probe", 79 TAILQ_HEAD_INITIALIZER(nvme_probe_driver.units), /* generation */ 0, 80 CAM_PERIPH_DRV_EARLY 81 }; 82 83 PERIPHDRIVER_DECLARE(nvme_probe, nvme_probe_driver); 84 85 typedef enum { 86 NVME_PROBE_IDENTIFY_CD, 87 NVME_PROBE_IDENTIFY_NS, 88 NVME_PROBE_DONE, 89 NVME_PROBE_INVALID 90 } nvme_probe_action; 91 92 static char *nvme_probe_action_text[] = { 93 "NVME_PROBE_IDENTIFY_CD", 94 "NVME_PROBE_IDENTIFY_NS", 95 "NVME_PROBE_DONE", 96 "NVME_PROBE_INVALID" 97 }; 98 99 #define NVME_PROBE_SET_ACTION(softc, newaction) \ 100 do { \ 101 char **text; \ 102 text = nvme_probe_action_text; \ 103 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE, \ 104 ("Probe %s to %s\n", text[(softc)->action], \ 105 text[(newaction)])); \ 106 (softc)->action = (newaction); \ 107 } while(0) 108 109 typedef enum { 110 NVME_PROBE_NO_ANNOUNCE = 0x04 111 } nvme_probe_flags; 112 113 typedef struct { 114 TAILQ_HEAD(, ccb_hdr) request_ccbs; 115 union { 116 struct nvme_controller_data cd; 117 struct nvme_namespace_data ns; 118 }; 119 nvme_probe_action action; 120 nvme_probe_flags flags; 121 int restart; 122 struct cam_periph *periph; 123 } nvme_probe_softc; 124 125 static struct nvme_quirk_entry nvme_quirk_table[] = 126 { 127 { 128 // { 129 // T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 130 // /*vendor*/"*", /*product*/"*", /*revision*/"*" 131 // }, 132 .quirks = 0, .mintags = 0, .maxtags = 0 133 }, 134 }; 135 136 static const int nvme_quirk_table_size = 137 sizeof(nvme_quirk_table) / sizeof(*nvme_quirk_table); 138 139 static cam_status nvme_probe_register(struct cam_periph *periph, 140 void *arg); 141 static void nvme_probe_schedule(struct cam_periph *nvme_probe_periph); 142 static void nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb); 143 static void nvme_probe_done(struct cam_periph *periph, union ccb *done_ccb); 144 static void nvme_probe_cleanup(struct cam_periph *periph); 145 //static void nvme_find_quirk(struct cam_ed *device); 146 static void nvme_scan_lun(struct cam_periph *periph, 147 struct cam_path *path, cam_flags flags, 148 union ccb *ccb); 149 static struct cam_ed * 150 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target, 151 lun_id_t lun_id); 152 static void nvme_device_transport(struct cam_path *path); 153 static void nvme_dev_async(uint32_t async_code, 154 struct cam_eb *bus, 155 struct cam_et *target, 156 struct cam_ed *device, 157 void *async_arg); 158 static void nvme_action(union ccb *start_ccb); 159 static void nvme_announce_periph(struct cam_periph *periph); 160 static void nvme_announce_periph_sbuf(struct cam_periph *periph, 161 struct sbuf *sb); 162 static void nvme_proto_announce(struct cam_ed *device); 163 static void nvme_proto_announce_sbuf(struct cam_ed *device, 164 struct sbuf *sb); 165 static void nvme_proto_denounce(struct cam_ed *device); 166 static void nvme_proto_denounce_sbuf(struct cam_ed *device, 167 struct sbuf *sb); 168 static void nvme_proto_debug_out(union ccb *ccb); 169 170 static struct xpt_xport_ops nvme_xport_ops = { 171 .alloc_device = nvme_alloc_device, 172 .action = nvme_action, 173 .async = nvme_dev_async, 174 .announce = nvme_announce_periph, 175 .announce_sbuf = nvme_announce_periph_sbuf, 176 }; 177 #define NVME_XPT_XPORT(x, X) \ 178 static struct xpt_xport nvme_xport_ ## x = { \ 179 .xport = XPORT_ ## X, \ 180 .name = #x, \ 181 .ops = &nvme_xport_ops, \ 182 }; \ 183 CAM_XPT_XPORT(nvme_xport_ ## x); 184 185 NVME_XPT_XPORT(nvme, NVME); 186 187 #undef NVME_XPT_XPORT 188 189 static struct xpt_proto_ops nvme_proto_ops = { 190 .announce = nvme_proto_announce, 191 .announce_sbuf = nvme_proto_announce_sbuf, 192 .denounce = nvme_proto_denounce, 193 .denounce_sbuf = nvme_proto_denounce_sbuf, 194 .debug_out = nvme_proto_debug_out, 195 }; 196 static struct xpt_proto nvme_proto = { 197 .proto = PROTO_NVME, 198 .name = "nvme", 199 .ops = &nvme_proto_ops, 200 }; 201 CAM_XPT_PROTO(nvme_proto); 202 203 static void 204 nvme_probe_periph_init(void) 205 { 206 } 207 208 static cam_status 209 nvme_probe_register(struct cam_periph *periph, void *arg) 210 { 211 union ccb *request_ccb; /* CCB representing the probe request */ 212 nvme_probe_softc *softc; 213 214 request_ccb = (union ccb *)arg; 215 if (request_ccb == NULL) { 216 printf("nvme_probe_register: no probe CCB, " 217 "can't register device\n"); 218 return(CAM_REQ_CMP_ERR); 219 } 220 221 softc = (nvme_probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT); 222 223 if (softc == NULL) { 224 printf("nvme_probe_register: Unable to probe new device. " 225 "Unable to allocate softc\n"); 226 return(CAM_REQ_CMP_ERR); 227 } 228 TAILQ_INIT(&softc->request_ccbs); 229 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h, 230 periph_links.tqe); 231 softc->flags = 0; 232 periph->softc = softc; 233 softc->periph = periph; 234 softc->action = NVME_PROBE_INVALID; 235 if (cam_periph_acquire(periph) != 0) 236 return (CAM_REQ_CMP_ERR); 237 238 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n")); 239 240 // nvme_device_transport(periph->path); 241 nvme_probe_schedule(periph); 242 243 return(CAM_REQ_CMP); 244 } 245 246 static void 247 nvme_probe_schedule(struct cam_periph *periph) 248 { 249 union ccb *ccb; 250 nvme_probe_softc *softc; 251 252 softc = (nvme_probe_softc *)periph->softc; 253 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs); 254 255 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_CD); 256 257 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE) 258 softc->flags |= NVME_PROBE_NO_ANNOUNCE; 259 else 260 softc->flags &= ~NVME_PROBE_NO_ANNOUNCE; 261 262 xpt_schedule(periph, CAM_PRIORITY_XPT); 263 } 264 265 static void 266 nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb) 267 { 268 struct ccb_nvmeio *nvmeio; 269 nvme_probe_softc *softc; 270 lun_id_t lun; 271 272 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_start\n")); 273 274 softc = (nvme_probe_softc *)periph->softc; 275 nvmeio = &start_ccb->nvmeio; 276 lun = xpt_path_lun_id(periph->path); 277 278 if (softc->restart) { 279 softc->restart = 0; 280 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_CD); 281 } 282 283 switch (softc->action) { 284 case NVME_PROBE_IDENTIFY_CD: 285 cam_fill_nvmeadmin(nvmeio, 286 0, /* retries */ 287 nvme_probe_done, /* cbfcnp */ 288 CAM_DIR_IN, /* flags */ 289 (uint8_t *)&softc->cd, /* data_ptr */ 290 sizeof(softc->cd), /* dxfer_len */ 291 30 * 1000); /* timeout 30s */ 292 nvme_ns_cmd(nvmeio, NVME_OPC_IDENTIFY, 0, 293 1, 0, 0, 0, 0, 0); 294 break; 295 case NVME_PROBE_IDENTIFY_NS: 296 cam_fill_nvmeadmin(nvmeio, 297 0, /* retries */ 298 nvme_probe_done, /* cbfcnp */ 299 CAM_DIR_IN, /* flags */ 300 (uint8_t *)&softc->ns, /* data_ptr */ 301 sizeof(softc->ns), /* dxfer_len */ 302 30 * 1000); /* timeout 30s */ 303 nvme_ns_cmd(nvmeio, NVME_OPC_IDENTIFY, lun, 304 0, 0, 0, 0, 0, 0); 305 break; 306 default: 307 panic("nvme_probe_start: invalid action state 0x%x\n", softc->action); 308 } 309 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 310 xpt_action(start_ccb); 311 } 312 313 static void 314 nvme_probe_done(struct cam_periph *periph, union ccb *done_ccb) 315 { 316 struct nvme_namespace_data *nvme_data; 317 struct nvme_controller_data *nvme_cdata; 318 nvme_probe_softc *softc; 319 struct cam_path *path; 320 struct scsi_vpd_device_id *did; 321 struct scsi_vpd_id_descriptor *idd; 322 uint32_t priority; 323 int found = 1, e, g, len; 324 325 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_done\n")); 326 327 softc = (nvme_probe_softc *)periph->softc; 328 path = done_ccb->ccb_h.path; 329 priority = done_ccb->ccb_h.pinfo.priority; 330 331 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 332 if (cam_periph_error(done_ccb, 333 0, softc->restart ? (SF_NO_RECOVERY | SF_NO_RETRY) : 0 334 ) == ERESTART) { 335 out: 336 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */ 337 cam_release_devq(path, 0, 0, 0, FALSE); 338 return; 339 } 340 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 341 /* Don't wedge the queue */ 342 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE); 343 } 344 345 /* 346 * If we get to this point, we got an error status back 347 * from the inquiry and the error status doesn't require 348 * automatically retrying the command. Therefore, the 349 * inquiry failed. If we had inquiry information before 350 * for this device, but this latest inquiry command failed, 351 * the device has probably gone away. If this device isn't 352 * already marked unconfigured, notify the peripheral 353 * drivers that this device is no more. 354 */ 355 device_fail: if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) 356 xpt_async(AC_LOST_DEVICE, path, NULL); 357 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_INVALID); 358 found = 0; 359 goto done; 360 } 361 if (softc->restart) 362 goto done; 363 switch (softc->action) { 364 case NVME_PROBE_IDENTIFY_CD: 365 nvme_controller_data_swapbytes(&softc->cd); 366 367 nvme_cdata = path->device->nvme_cdata; 368 if (nvme_cdata == NULL) { 369 nvme_cdata = malloc(sizeof(*nvme_cdata), M_CAMXPT, 370 M_NOWAIT); 371 if (nvme_cdata == NULL) { 372 xpt_print(path, "Can't allocate memory"); 373 goto device_fail; 374 } 375 } 376 bcopy(&softc->cd, nvme_cdata, sizeof(*nvme_cdata)); 377 path->device->nvme_cdata = nvme_cdata; 378 379 /* Save/update serial number. */ 380 if (path->device->serial_num != NULL) { 381 free(path->device->serial_num, M_CAMXPT); 382 path->device->serial_num = NULL; 383 path->device->serial_num_len = 0; 384 } 385 path->device->serial_num = (uint8_t *) 386 malloc(NVME_SERIAL_NUMBER_LENGTH + 1, M_CAMXPT, M_NOWAIT); 387 if (path->device->serial_num != NULL) { 388 cam_strvis_flag(path->device->serial_num, 389 nvme_cdata->sn, sizeof(nvme_cdata->sn), 390 NVME_SERIAL_NUMBER_LENGTH + 1, 391 CAM_STRVIS_FLAG_NONASCII_SPC); 392 393 path->device->serial_num_len = 394 strlen(path->device->serial_num); 395 } 396 397 // nvme_find_quirk(path->device); 398 nvme_device_transport(path); 399 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_NS); 400 xpt_release_ccb(done_ccb); 401 xpt_schedule(periph, priority); 402 goto out; 403 case NVME_PROBE_IDENTIFY_NS: 404 nvme_namespace_data_swapbytes(&softc->ns); 405 406 /* Check that the namespace exists. */ 407 if (softc->ns.nsze == 0) 408 goto device_fail; 409 410 nvme_data = path->device->nvme_data; 411 if (nvme_data == NULL) { 412 nvme_data = malloc(sizeof(*nvme_data), M_CAMXPT, 413 M_NOWAIT); 414 if (nvme_data == NULL) { 415 xpt_print(path, "Can't allocate memory"); 416 goto device_fail; 417 } 418 } 419 bcopy(&softc->ns, nvme_data, sizeof(*nvme_data)); 420 path->device->nvme_data = nvme_data; 421 422 /* Save/update device_id based on NGUID and/or EUI64. */ 423 if (path->device->device_id != NULL) { 424 free(path->device->device_id, M_CAMXPT); 425 path->device->device_id = NULL; 426 path->device->device_id_len = 0; 427 } 428 len = 0; 429 for (g = 0; g < sizeof(nvme_data->nguid); g++) { 430 if (nvme_data->nguid[g] != 0) 431 break; 432 } 433 if (g < sizeof(nvme_data->nguid)) 434 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 435 for (e = 0; e < sizeof(nvme_data->eui64); e++) { 436 if (nvme_data->eui64[e] != 0) 437 break; 438 } 439 if (e < sizeof(nvme_data->eui64)) 440 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 441 if (len > 0) { 442 path->device->device_id = (uint8_t *) 443 malloc(SVPD_DEVICE_ID_HDR_LEN + len, 444 M_CAMXPT, M_NOWAIT); 445 } 446 if (path->device->device_id != NULL) { 447 did = (struct scsi_vpd_device_id *)path->device->device_id; 448 did->device = SID_QUAL_LU_CONNECTED | T_DIRECT; 449 did->page_code = SVPD_DEVICE_ID; 450 scsi_ulto2b(len, did->length); 451 idd = (struct scsi_vpd_id_descriptor *)(did + 1); 452 if (g < sizeof(nvme_data->nguid)) { 453 idd->proto_codeset = SVPD_ID_CODESET_BINARY; 454 idd->id_type = SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_EUI64; 455 idd->length = 16; 456 bcopy(nvme_data->nguid, idd->identifier, 16); 457 idd = (struct scsi_vpd_id_descriptor *) 458 &idd->identifier[16]; 459 } 460 if (e < sizeof(nvme_data->eui64)) { 461 idd->proto_codeset = SVPD_ID_CODESET_BINARY; 462 idd->id_type = SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_EUI64; 463 idd->length = 8; 464 bcopy(nvme_data->eui64, idd->identifier, 8); 465 } 466 path->device->device_id_len = SVPD_DEVICE_ID_HDR_LEN + len; 467 } 468 469 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 470 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 471 xpt_acquire_device(path->device); 472 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 473 xpt_action(done_ccb); 474 xpt_async(AC_FOUND_DEVICE, path, done_ccb); 475 } 476 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_DONE); 477 break; 478 default: 479 panic("nvme_probe_done: invalid action state 0x%x\n", softc->action); 480 } 481 done: 482 if (softc->restart) { 483 softc->restart = 0; 484 xpt_release_ccb(done_ccb); 485 nvme_probe_schedule(periph); 486 goto out; 487 } 488 xpt_release_ccb(done_ccb); 489 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n")); 490 while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) { 491 TAILQ_REMOVE(&softc->request_ccbs, 492 &done_ccb->ccb_h, periph_links.tqe); 493 done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR; 494 xpt_done(done_ccb); 495 } 496 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */ 497 cam_release_devq(path, 0, 0, 0, FALSE); 498 cam_periph_invalidate(periph); 499 cam_periph_release_locked(periph); 500 } 501 502 static void 503 nvme_probe_cleanup(struct cam_periph *periph) 504 { 505 506 free(periph->softc, M_CAMXPT); 507 } 508 509 #if 0 510 /* XXX should be used, don't delete */ 511 static void 512 nvme_find_quirk(struct cam_ed *device) 513 { 514 struct nvme_quirk_entry *quirk; 515 caddr_t match; 516 517 match = cam_quirkmatch((caddr_t)&device->nvme_data, 518 (caddr_t)nvme_quirk_table, 519 nvme_quirk_table_size, 520 sizeof(*nvme_quirk_table), nvme_identify_match); 521 522 if (match == NULL) 523 panic("xpt_find_quirk: device didn't match wildcard entry!!"); 524 525 quirk = (struct nvme_quirk_entry *)match; 526 device->quirk = quirk; 527 if (quirk->quirks & CAM_QUIRK_MAXTAGS) { 528 device->mintags = quirk->mintags; 529 device->maxtags = quirk->maxtags; 530 } 531 } 532 #endif 533 534 static void 535 nvme_scan_lun(struct cam_periph *periph, struct cam_path *path, 536 cam_flags flags, union ccb *request_ccb) 537 { 538 struct ccb_pathinq cpi; 539 cam_status status; 540 struct cam_periph *old_periph; 541 int lock; 542 543 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun\n")); 544 545 xpt_path_inq(&cpi, path); 546 547 if (cpi.ccb_h.status != CAM_REQ_CMP) { 548 if (request_ccb != NULL) { 549 request_ccb->ccb_h.status = cpi.ccb_h.status; 550 xpt_done(request_ccb); 551 } 552 return; 553 } 554 555 if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) { 556 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun ignoring bus\n")); 557 request_ccb->ccb_h.status = CAM_REQ_CMP; /* XXX signal error ? */ 558 xpt_done(request_ccb); 559 return; 560 } 561 562 lock = (xpt_path_owned(path) == 0); 563 if (lock) 564 xpt_path_lock(path); 565 if ((old_periph = cam_periph_find(path, "nvme_probe")) != NULL) { 566 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) { 567 nvme_probe_softc *softc; 568 569 softc = (nvme_probe_softc *)old_periph->softc; 570 TAILQ_INSERT_TAIL(&softc->request_ccbs, 571 &request_ccb->ccb_h, periph_links.tqe); 572 softc->restart = 1; 573 CAM_DEBUG(path, CAM_DEBUG_TRACE, 574 ("restarting nvme_probe device\n")); 575 } else { 576 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR; 577 CAM_DEBUG(path, CAM_DEBUG_TRACE, 578 ("Failing to restart nvme_probe device\n")); 579 xpt_done(request_ccb); 580 } 581 } else { 582 CAM_DEBUG(path, CAM_DEBUG_TRACE, 583 ("Adding nvme_probe device\n")); 584 status = cam_periph_alloc(nvme_probe_register, NULL, nvme_probe_cleanup, 585 nvme_probe_start, "nvme_probe", 586 CAM_PERIPH_BIO, 587 request_ccb->ccb_h.path, NULL, 0, 588 request_ccb); 589 590 if (status != CAM_REQ_CMP) { 591 xpt_print(path, "xpt_scan_lun: cam_alloc_periph " 592 "returned an error, can't continue probe\n"); 593 request_ccb->ccb_h.status = status; 594 xpt_done(request_ccb); 595 } 596 } 597 if (lock) 598 xpt_path_unlock(path); 599 } 600 601 static struct cam_ed * 602 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id) 603 { 604 struct nvme_quirk_entry *quirk; 605 struct cam_ed *device; 606 607 device = xpt_alloc_device(bus, target, lun_id); 608 if (device == NULL) 609 return (NULL); 610 611 /* 612 * Take the default quirk entry until we have inquiry 613 * data from nvme and can determine a better quirk to use. 614 */ 615 quirk = &nvme_quirk_table[nvme_quirk_table_size - 1]; 616 device->quirk = (void *)quirk; 617 device->mintags = 0; 618 device->maxtags = 0; 619 device->inq_flags = 0; 620 device->queue_flags = 0; 621 device->device_id = NULL; 622 device->device_id_len = 0; 623 device->serial_num = NULL; 624 device->serial_num_len = 0; 625 return (device); 626 } 627 628 static void 629 nvme_device_transport(struct cam_path *path) 630 { 631 struct ccb_pathinq cpi; 632 struct ccb_trans_settings cts; 633 /* XXX get data from nvme namespace and other info ??? */ 634 635 /* Get transport information from the SIM */ 636 xpt_path_inq(&cpi, path); 637 638 path->device->transport = cpi.transport; 639 path->device->transport_version = cpi.transport_version; 640 641 path->device->protocol = cpi.protocol; 642 path->device->protocol_version = cpi.protocol_version; 643 644 /* Tell the controller what we think */ 645 memset(&cts, 0, sizeof(cts)); 646 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 647 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 648 cts.type = CTS_TYPE_CURRENT_SETTINGS; 649 cts.transport = path->device->transport; 650 cts.transport_version = path->device->transport_version; 651 cts.protocol = path->device->protocol; 652 cts.protocol_version = path->device->protocol_version; 653 cts.proto_specific.valid = 0; 654 cts.xport_specific.valid = 0; 655 xpt_action((union ccb *)&cts); 656 } 657 658 static void 659 nvme_dev_advinfo(union ccb *start_ccb) 660 { 661 struct cam_ed *device; 662 struct ccb_dev_advinfo *cdai; 663 off_t amt; 664 665 xpt_path_assert(start_ccb->ccb_h.path, MA_OWNED); 666 start_ccb->ccb_h.status = CAM_REQ_INVALID; 667 device = start_ccb->ccb_h.path->device; 668 cdai = &start_ccb->cdai; 669 switch(cdai->buftype) { 670 case CDAI_TYPE_SCSI_DEVID: 671 if (cdai->flags & CDAI_FLAG_STORE) 672 return; 673 cdai->provsiz = device->device_id_len; 674 if (device->device_id_len == 0) 675 break; 676 amt = device->device_id_len; 677 if (cdai->provsiz > cdai->bufsiz) 678 amt = cdai->bufsiz; 679 memcpy(cdai->buf, device->device_id, amt); 680 break; 681 case CDAI_TYPE_SERIAL_NUM: 682 if (cdai->flags & CDAI_FLAG_STORE) 683 return; 684 cdai->provsiz = device->serial_num_len; 685 if (device->serial_num_len == 0) 686 break; 687 amt = device->serial_num_len; 688 if (cdai->provsiz > cdai->bufsiz) 689 amt = cdai->bufsiz; 690 memcpy(cdai->buf, device->serial_num, amt); 691 break; 692 case CDAI_TYPE_PHYS_PATH: 693 if (cdai->flags & CDAI_FLAG_STORE) { 694 if (device->physpath != NULL) { 695 free(device->physpath, M_CAMXPT); 696 device->physpath = NULL; 697 device->physpath_len = 0; 698 } 699 /* Clear existing buffer if zero length */ 700 if (cdai->bufsiz == 0) 701 break; 702 device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT); 703 if (device->physpath == NULL) { 704 start_ccb->ccb_h.status = CAM_REQ_ABORTED; 705 return; 706 } 707 device->physpath_len = cdai->bufsiz; 708 memcpy(device->physpath, cdai->buf, cdai->bufsiz); 709 } else { 710 cdai->provsiz = device->physpath_len; 711 if (device->physpath_len == 0) 712 break; 713 amt = device->physpath_len; 714 if (cdai->provsiz > cdai->bufsiz) 715 amt = cdai->bufsiz; 716 memcpy(cdai->buf, device->physpath, amt); 717 } 718 break; 719 case CDAI_TYPE_NVME_CNTRL: 720 if (cdai->flags & CDAI_FLAG_STORE) 721 return; 722 amt = sizeof(struct nvme_controller_data); 723 cdai->provsiz = amt; 724 if (amt > cdai->bufsiz) 725 amt = cdai->bufsiz; 726 memcpy(cdai->buf, device->nvme_cdata, amt); 727 break; 728 case CDAI_TYPE_NVME_NS: 729 if (cdai->flags & CDAI_FLAG_STORE) 730 return; 731 amt = sizeof(struct nvme_namespace_data); 732 cdai->provsiz = amt; 733 if (amt > cdai->bufsiz) 734 amt = cdai->bufsiz; 735 memcpy(cdai->buf, device->nvme_data, amt); 736 break; 737 default: 738 return; 739 } 740 start_ccb->ccb_h.status = CAM_REQ_CMP; 741 742 if (cdai->flags & CDAI_FLAG_STORE) { 743 xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path, 744 (void *)(uintptr_t)cdai->buftype); 745 } 746 } 747 748 static void 749 nvme_action(union ccb *start_ccb) 750 { 751 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, 752 ("nvme_action: func= %#x\n", start_ccb->ccb_h.func_code)); 753 754 switch (start_ccb->ccb_h.func_code) { 755 case XPT_SCAN_BUS: 756 case XPT_SCAN_TGT: 757 case XPT_SCAN_LUN: 758 nvme_scan_lun(start_ccb->ccb_h.path->periph, 759 start_ccb->ccb_h.path, start_ccb->crcn.flags, 760 start_ccb); 761 break; 762 case XPT_DEV_ADVINFO: 763 nvme_dev_advinfo(start_ccb); 764 break; 765 766 default: 767 xpt_action_default(start_ccb); 768 break; 769 } 770 } 771 772 /* 773 * Handle any per-device event notifications that require action by the XPT. 774 */ 775 static void 776 nvme_dev_async(uint32_t async_code, struct cam_eb *bus, struct cam_et *target, 777 struct cam_ed *device, void *async_arg) 778 { 779 780 /* 781 * We only need to handle events for real devices. 782 */ 783 if (target->target_id == CAM_TARGET_WILDCARD 784 || device->lun_id == CAM_LUN_WILDCARD) 785 return; 786 787 if (async_code == AC_LOST_DEVICE && 788 (device->flags & CAM_DEV_UNCONFIGURED) == 0) { 789 device->flags |= CAM_DEV_UNCONFIGURED; 790 xpt_release_device(device); 791 } 792 } 793 794 static void 795 nvme_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb) 796 { 797 struct ccb_pathinq cpi; 798 struct ccb_trans_settings cts; 799 struct cam_path *path = periph->path; 800 struct ccb_trans_settings_nvme *nvmex; 801 802 cam_periph_assert(periph, MA_OWNED); 803 804 /* Ask the SIM for connection details */ 805 memset(&cts, 0, sizeof(cts)); 806 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL); 807 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 808 cts.type = CTS_TYPE_CURRENT_SETTINGS; 809 xpt_action((union ccb*)&cts); 810 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 811 return; 812 813 /* Ask the SIM for its base transfer speed */ 814 xpt_path_inq(&cpi, periph->path); 815 sbuf_printf(sb, "%s%d: nvme version %d.%d", 816 periph->periph_name, periph->unit_number, 817 NVME_MAJOR(cts.protocol_version), 818 NVME_MINOR(cts.protocol_version)); 819 if (cts.transport == XPORT_NVME) { 820 nvmex = &cts.proto_specific.nvme; 821 if (nvmex->valid & CTS_NVME_VALID_LINK) 822 sbuf_printf(sb, 823 " x%d (max x%d) lanes PCIe Gen%d (max Gen%d) link", 824 nvmex->lanes, nvmex->max_lanes, 825 nvmex->speed, nvmex->max_speed); 826 } 827 sbuf_printf(sb, "\n"); 828 } 829 830 static void 831 nvme_announce_periph(struct cam_periph *periph) 832 { 833 struct sbuf sb; 834 char buffer[120]; 835 836 sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN); 837 nvme_announce_periph_sbuf(periph, &sb); 838 sbuf_finish(&sb); 839 sbuf_putbuf(&sb); 840 } 841 842 static void 843 nvme_proto_announce(struct cam_ed *device) 844 { 845 struct sbuf sb; 846 char buffer[120]; 847 848 sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN); 849 nvme_print_ident(device->nvme_cdata, device->nvme_data, &sb); 850 sbuf_finish(&sb); 851 sbuf_putbuf(&sb); 852 } 853 854 static void 855 nvme_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb) 856 { 857 nvme_print_ident(device->nvme_cdata, device->nvme_data, sb); 858 } 859 860 static void 861 nvme_proto_denounce(struct cam_ed *device) 862 { 863 struct sbuf sb; 864 char buffer[120]; 865 866 sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN); 867 nvme_print_ident_short(device->nvme_cdata, device->nvme_data, &sb); 868 sbuf_finish(&sb); 869 sbuf_putbuf(&sb); 870 } 871 872 static void 873 nvme_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb) 874 { 875 nvme_print_ident_short(device->nvme_cdata, device->nvme_data, sb); 876 } 877 878 static void 879 nvme_proto_debug_out(union ccb *ccb) 880 { 881 char cdb_str[(sizeof(struct nvme_command) * 3) + 1]; 882 883 if (ccb->ccb_h.func_code != XPT_NVME_IO && 884 ccb->ccb_h.func_code != XPT_NVME_ADMIN) 885 return; 886 887 CAM_DEBUG(ccb->ccb_h.path, 888 CAM_DEBUG_CDB,("%s. NCB: %s\n", nvme_op_string(&ccb->nvmeio.cmd, 889 ccb->ccb_h.func_code == XPT_NVME_ADMIN), 890 nvme_cmd_string(&ccb->nvmeio.cmd, cdb_str, sizeof(cdb_str)))); 891 } 892