2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015 Netflix, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * derived from ata_xpt.c: Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.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>
42 #include <sys/fcntl.h>
46 #include <sys/mutex.h>
47 #include <sys/sysctl.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>
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 */
66 struct nvme_quirk_entry {
68 #define CAM_QUIRK_MAXTAGS 1
73 /* Not even sure why we need this */
74 static periph_init_t nvme_probe_periph_init;
76 static struct periph_driver nvme_probe_driver =
78 nvme_probe_periph_init, "nvme_probe",
79 TAILQ_HEAD_INITIALIZER(nvme_probe_driver.units), /* generation */ 0,
83 PERIPHDRIVER_DECLARE(nvme_probe, nvme_probe_driver);
86 NVME_PROBE_IDENTIFY_CD,
87 NVME_PROBE_IDENTIFY_NS,
92 static char *nvme_probe_action_text[] = {
93 "NVME_PROBE_IDENTIFY_CD",
94 "NVME_PROBE_IDENTIFY_NS",
99 #define NVME_PROBE_SET_ACTION(softc, newaction) \
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); \
110 NVME_PROBE_NO_ANNOUNCE = 0x04
114 TAILQ_HEAD(, ccb_hdr) request_ccbs;
116 struct nvme_controller_data cd;
117 struct nvme_namespace_data ns;
119 nvme_probe_action action;
120 nvme_probe_flags flags;
122 struct cam_periph *periph;
125 static struct nvme_quirk_entry nvme_quirk_table[] =
129 // T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
130 // /*vendor*/"*", /*product*/"*", /*revision*/"*"
132 .quirks = 0, .mintags = 0, .maxtags = 0
136 static const int nvme_quirk_table_size =
137 sizeof(nvme_quirk_table) / sizeof(*nvme_quirk_table);
139 static cam_status nvme_probe_register(struct cam_periph *periph,
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,
149 static struct cam_ed *
150 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target,
152 static void nvme_device_transport(struct cam_path *path);
153 static void nvme_dev_async(u_int32_t async_code,
155 struct cam_et *target,
156 struct cam_ed *device,
158 static void nvme_action(union ccb *start_ccb);
159 static void nvme_announce_periph(struct cam_periph *periph);
160 static void nvme_proto_announce(struct cam_ed *device);
161 static void nvme_proto_denounce(struct cam_ed *device);
162 static void nvme_proto_debug_out(union ccb *ccb);
164 static struct xpt_xport_ops nvme_xport_ops = {
165 .alloc_device = nvme_alloc_device,
166 .action = nvme_action,
167 .async = nvme_dev_async,
168 .announce = nvme_announce_periph,
170 #define NVME_XPT_XPORT(x, X) \
171 static struct xpt_xport nvme_xport_ ## x = { \
172 .xport = XPORT_ ## X, \
174 .ops = &nvme_xport_ops, \
176 CAM_XPT_XPORT(nvme_xport_ ## x);
178 NVME_XPT_XPORT(nvme, NVME);
180 #undef NVME_XPT_XPORT
182 static struct xpt_proto_ops nvme_proto_ops = {
183 .announce = nvme_proto_announce,
184 .denounce = nvme_proto_denounce,
185 .debug_out = nvme_proto_debug_out,
187 static struct xpt_proto nvme_proto = {
190 .ops = &nvme_proto_ops,
192 CAM_XPT_PROTO(nvme_proto);
195 nvme_probe_periph_init(void)
200 nvme_probe_register(struct cam_periph *periph, void *arg)
202 union ccb *request_ccb; /* CCB representing the probe request */
203 nvme_probe_softc *softc;
205 request_ccb = (union ccb *)arg;
206 if (request_ccb == NULL) {
207 printf("nvme_probe_register: no probe CCB, "
208 "can't register device\n");
209 return(CAM_REQ_CMP_ERR);
212 softc = (nvme_probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
215 printf("nvme_probe_register: Unable to probe new device. "
216 "Unable to allocate softc\n");
217 return(CAM_REQ_CMP_ERR);
219 TAILQ_INIT(&softc->request_ccbs);
220 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
223 periph->softc = softc;
224 softc->periph = periph;
225 softc->action = NVME_PROBE_INVALID;
226 if (cam_periph_acquire(periph) != 0)
227 return (CAM_REQ_CMP_ERR);
229 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
231 // nvme_device_transport(periph->path);
232 nvme_probe_schedule(periph);
238 nvme_probe_schedule(struct cam_periph *periph)
241 nvme_probe_softc *softc;
243 softc = (nvme_probe_softc *)periph->softc;
244 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
246 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_CD);
248 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
249 softc->flags |= NVME_PROBE_NO_ANNOUNCE;
251 softc->flags &= ~NVME_PROBE_NO_ANNOUNCE;
253 xpt_schedule(periph, CAM_PRIORITY_XPT);
257 nvme_probe_start(struct cam_periph *periph, union ccb *start_ccb)
259 struct ccb_nvmeio *nvmeio;
260 nvme_probe_softc *softc;
261 struct cam_path *path;
264 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_start\n"));
266 softc = (nvme_probe_softc *)periph->softc;
267 path = start_ccb->ccb_h.path;
268 nvmeio = &start_ccb->nvmeio;
269 lun = xpt_path_lun_id(periph->path);
271 if (softc->restart) {
273 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_CD);
276 switch (softc->action) {
277 case NVME_PROBE_IDENTIFY_CD:
278 cam_fill_nvmeadmin(nvmeio,
280 nvme_probe_done, /* cbfcnp */
281 CAM_DIR_IN, /* flags */
282 (uint8_t *)&softc->cd, /* data_ptr */
283 sizeof(softc->cd), /* dxfer_len */
284 30 * 1000); /* timeout 30s */
285 nvme_ns_cmd(nvmeio, NVME_OPC_IDENTIFY, 0,
288 case NVME_PROBE_IDENTIFY_NS:
289 cam_fill_nvmeadmin(nvmeio,
291 nvme_probe_done, /* cbfcnp */
292 CAM_DIR_IN, /* flags */
293 (uint8_t *)&softc->ns, /* data_ptr */
294 sizeof(softc->ns), /* dxfer_len */
295 30 * 1000); /* timeout 30s */
296 nvme_ns_cmd(nvmeio, NVME_OPC_IDENTIFY, lun,
300 panic("nvme_probe_start: invalid action state 0x%x\n", softc->action);
302 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
303 xpt_action(start_ccb);
307 nvme_probe_done(struct cam_periph *periph, union ccb *done_ccb)
309 struct nvme_namespace_data *nvme_data;
310 struct nvme_controller_data *nvme_cdata;
311 nvme_probe_softc *softc;
312 struct cam_path *path;
313 struct scsi_vpd_device_id *did;
314 struct scsi_vpd_id_descriptor *idd;
317 int found = 1, e, g, len;
319 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("nvme_probe_done\n"));
321 softc = (nvme_probe_softc *)periph->softc;
322 path = done_ccb->ccb_h.path;
323 priority = done_ccb->ccb_h.pinfo.priority;
325 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
326 if (cam_periph_error(done_ccb,
327 0, softc->restart ? (SF_NO_RECOVERY | SF_NO_RETRY) : 0
330 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
331 cam_release_devq(path, 0, 0, 0, FALSE);
334 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
335 /* Don't wedge the queue */
336 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
338 status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
341 * If we get to this point, we got an error status back
342 * from the inquiry and the error status doesn't require
343 * automatically retrying the command. Therefore, the
344 * inquiry failed. If we had inquiry information before
345 * for this device, but this latest inquiry command failed,
346 * the device has probably gone away. If this device isn't
347 * already marked unconfigured, notify the peripheral
348 * drivers that this device is no more.
350 device_fail: if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
351 xpt_async(AC_LOST_DEVICE, path, NULL);
352 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_INVALID);
358 switch (softc->action) {
359 case NVME_PROBE_IDENTIFY_CD:
360 nvme_controller_data_swapbytes(&softc->cd);
362 nvme_cdata = path->device->nvme_cdata;
363 if (nvme_cdata == NULL) {
364 nvme_cdata = malloc(sizeof(*nvme_cdata), M_CAMXPT,
366 if (nvme_cdata == NULL) {
367 xpt_print(path, "Can't allocate memory");
371 bcopy(&softc->cd, nvme_cdata, sizeof(*nvme_cdata));
372 path->device->nvme_cdata = nvme_cdata;
374 /* Save/update serial number. */
375 if (path->device->serial_num != NULL) {
376 free(path->device->serial_num, M_CAMXPT);
377 path->device->serial_num = NULL;
378 path->device->serial_num_len = 0;
380 path->device->serial_num = (u_int8_t *)
381 malloc(NVME_SERIAL_NUMBER_LENGTH + 1, M_CAMXPT, M_NOWAIT);
382 if (path->device->serial_num != NULL) {
383 cam_strvis(path->device->serial_num, nvme_cdata->sn,
384 NVME_SERIAL_NUMBER_LENGTH, NVME_SERIAL_NUMBER_LENGTH + 1);
385 path->device->serial_num_len =
386 strlen(path->device->serial_num);
389 // nvme_find_quirk(path->device);
390 nvme_device_transport(path);
391 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_IDENTIFY_NS);
392 xpt_release_ccb(done_ccb);
393 xpt_schedule(periph, priority);
395 case NVME_PROBE_IDENTIFY_NS:
396 nvme_namespace_data_swapbytes(&softc->ns);
398 /* Check that the namespace exists. */
399 if (softc->ns.nsze == 0)
402 nvme_data = path->device->nvme_data;
403 if (nvme_data == NULL) {
404 nvme_data = malloc(sizeof(*nvme_data), M_CAMXPT,
406 if (nvme_data == NULL) {
407 xpt_print(path, "Can't allocate memory");
411 bcopy(&softc->ns, nvme_data, sizeof(*nvme_data));
412 path->device->nvme_data = nvme_data;
414 /* Save/update device_id based on NGUID and/or EUI64. */
415 if (path->device->device_id != NULL) {
416 free(path->device->device_id, M_CAMXPT);
417 path->device->device_id = NULL;
418 path->device->device_id_len = 0;
421 for (g = 0; g < sizeof(nvme_data->nguid); g++) {
422 if (nvme_data->nguid[g] != 0)
425 if (g < sizeof(nvme_data->nguid))
426 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
427 for (e = 0; e < sizeof(nvme_data->eui64); e++) {
428 if (nvme_data->eui64[e] != 0)
431 if (e < sizeof(nvme_data->eui64))
432 len += sizeof(struct scsi_vpd_id_descriptor) + 8;
434 path->device->device_id = (u_int8_t *)
435 malloc(SVPD_DEVICE_ID_HDR_LEN + len,
438 if (path->device->device_id != NULL) {
439 did = (struct scsi_vpd_device_id *)path->device->device_id;
440 did->device = SID_QUAL_LU_CONNECTED | T_DIRECT;
441 did->page_code = SVPD_DEVICE_ID;
442 scsi_ulto2b(len, did->length);
443 idd = (struct scsi_vpd_id_descriptor *)(did + 1);
444 if (g < sizeof(nvme_data->nguid)) {
445 idd->proto_codeset = SVPD_ID_CODESET_BINARY;
446 idd->id_type = SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_EUI64;
448 bcopy(nvme_data->nguid, idd->identifier, 16);
449 idd = (struct scsi_vpd_id_descriptor *)
450 &idd->identifier[16];
452 if (e < sizeof(nvme_data->eui64)) {
453 idd->proto_codeset = SVPD_ID_CODESET_BINARY;
454 idd->id_type = SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_EUI64;
456 bcopy(nvme_data->eui64, idd->identifier, 8);
458 path->device->device_id_len = SVPD_DEVICE_ID_HDR_LEN + len;
461 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
462 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
463 xpt_acquire_device(path->device);
464 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
465 xpt_action(done_ccb);
466 xpt_async(AC_FOUND_DEVICE, path, done_ccb);
468 NVME_PROBE_SET_ACTION(softc, NVME_PROBE_DONE);
471 panic("nvme_probe_done: invalid action state 0x%x\n", softc->action);
474 if (softc->restart) {
476 xpt_release_ccb(done_ccb);
477 nvme_probe_schedule(periph);
480 xpt_release_ccb(done_ccb);
481 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
482 while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
483 TAILQ_REMOVE(&softc->request_ccbs,
484 &done_ccb->ccb_h, periph_links.tqe);
485 done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
488 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
489 cam_release_devq(path, 0, 0, 0, FALSE);
490 cam_periph_invalidate(periph);
491 cam_periph_release_locked(periph);
495 nvme_probe_cleanup(struct cam_periph *periph)
498 free(periph->softc, M_CAMXPT);
502 /* XXX should be used, don't delete */
504 nvme_find_quirk(struct cam_ed *device)
506 struct nvme_quirk_entry *quirk;
509 match = cam_quirkmatch((caddr_t)&device->nvme_data,
510 (caddr_t)nvme_quirk_table,
511 nvme_quirk_table_size,
512 sizeof(*nvme_quirk_table), nvme_identify_match);
515 panic("xpt_find_quirk: device didn't match wildcard entry!!");
517 quirk = (struct nvme_quirk_entry *)match;
518 device->quirk = quirk;
519 if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
520 device->mintags = quirk->mintags;
521 device->maxtags = quirk->maxtags;
527 nvme_scan_lun(struct cam_periph *periph, struct cam_path *path,
528 cam_flags flags, union ccb *request_ccb)
530 struct ccb_pathinq cpi;
532 struct cam_periph *old_periph;
535 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun\n"));
537 xpt_path_inq(&cpi, path);
539 if (cpi.ccb_h.status != CAM_REQ_CMP) {
540 if (request_ccb != NULL) {
541 request_ccb->ccb_h.status = cpi.ccb_h.status;
542 xpt_done(request_ccb);
547 if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
548 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("nvme_scan_lun ignoring bus\n"));
549 request_ccb->ccb_h.status = CAM_REQ_CMP; /* XXX signal error ? */
550 xpt_done(request_ccb);
554 lock = (xpt_path_owned(path) == 0);
557 if ((old_periph = cam_periph_find(path, "nvme_probe")) != NULL) {
558 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
559 nvme_probe_softc *softc;
561 softc = (nvme_probe_softc *)old_periph->softc;
562 TAILQ_INSERT_TAIL(&softc->request_ccbs,
563 &request_ccb->ccb_h, periph_links.tqe);
565 CAM_DEBUG(path, CAM_DEBUG_TRACE,
566 ("restarting nvme_probe device\n"));
568 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
569 CAM_DEBUG(path, CAM_DEBUG_TRACE,
570 ("Failing to restart nvme_probe device\n"));
571 xpt_done(request_ccb);
574 CAM_DEBUG(path, CAM_DEBUG_TRACE,
575 ("Adding nvme_probe device\n"));
576 status = cam_periph_alloc(nvme_probe_register, NULL, nvme_probe_cleanup,
577 nvme_probe_start, "nvme_probe",
579 request_ccb->ccb_h.path, NULL, 0,
582 if (status != CAM_REQ_CMP) {
583 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
584 "returned an error, can't continue probe\n");
585 request_ccb->ccb_h.status = status;
586 xpt_done(request_ccb);
590 xpt_path_unlock(path);
593 static struct cam_ed *
594 nvme_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
596 struct nvme_quirk_entry *quirk;
597 struct cam_ed *device;
599 device = xpt_alloc_device(bus, target, lun_id);
604 * Take the default quirk entry until we have inquiry
605 * data from nvme and can determine a better quirk to use.
607 quirk = &nvme_quirk_table[nvme_quirk_table_size - 1];
608 device->quirk = (void *)quirk;
611 device->inq_flags = 0;
612 device->queue_flags = 0;
613 device->device_id = NULL;
614 device->device_id_len = 0;
615 device->serial_num = NULL;
616 device->serial_num_len = 0;
621 nvme_device_transport(struct cam_path *path)
623 struct ccb_pathinq cpi;
624 struct ccb_trans_settings cts;
625 /* XXX get data from nvme namespace and other info ??? */
627 /* Get transport information from the SIM */
628 xpt_path_inq(&cpi, path);
630 path->device->transport = cpi.transport;
631 path->device->transport_version = cpi.transport_version;
633 path->device->protocol = cpi.protocol;
634 path->device->protocol_version = cpi.protocol_version;
636 /* Tell the controller what we think */
637 memset(&cts, 0, sizeof(cts));
638 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
639 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
640 cts.type = CTS_TYPE_CURRENT_SETTINGS;
641 cts.transport = path->device->transport;
642 cts.transport_version = path->device->transport_version;
643 cts.protocol = path->device->protocol;
644 cts.protocol_version = path->device->protocol_version;
645 cts.proto_specific.valid = 0;
646 cts.xport_specific.valid = 0;
647 xpt_action((union ccb *)&cts);
651 nvme_dev_advinfo(union ccb *start_ccb)
653 struct cam_ed *device;
654 struct ccb_dev_advinfo *cdai;
657 xpt_path_assert(start_ccb->ccb_h.path, MA_OWNED);
658 start_ccb->ccb_h.status = CAM_REQ_INVALID;
659 device = start_ccb->ccb_h.path->device;
660 cdai = &start_ccb->cdai;
661 switch(cdai->buftype) {
662 case CDAI_TYPE_SCSI_DEVID:
663 if (cdai->flags & CDAI_FLAG_STORE)
665 cdai->provsiz = device->device_id_len;
666 if (device->device_id_len == 0)
668 amt = device->device_id_len;
669 if (cdai->provsiz > cdai->bufsiz)
671 memcpy(cdai->buf, device->device_id, amt);
673 case CDAI_TYPE_SERIAL_NUM:
674 if (cdai->flags & CDAI_FLAG_STORE)
676 cdai->provsiz = device->serial_num_len;
677 if (device->serial_num_len == 0)
679 amt = device->serial_num_len;
680 if (cdai->provsiz > cdai->bufsiz)
682 memcpy(cdai->buf, device->serial_num, amt);
684 case CDAI_TYPE_PHYS_PATH:
685 if (cdai->flags & CDAI_FLAG_STORE) {
686 if (device->physpath != NULL) {
687 free(device->physpath, M_CAMXPT);
688 device->physpath = NULL;
689 device->physpath_len = 0;
691 /* Clear existing buffer if zero length */
692 if (cdai->bufsiz == 0)
694 device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
695 if (device->physpath == NULL) {
696 start_ccb->ccb_h.status = CAM_REQ_ABORTED;
699 device->physpath_len = cdai->bufsiz;
700 memcpy(device->physpath, cdai->buf, cdai->bufsiz);
702 cdai->provsiz = device->physpath_len;
703 if (device->physpath_len == 0)
705 amt = device->physpath_len;
706 if (cdai->provsiz > cdai->bufsiz)
708 memcpy(cdai->buf, device->physpath, amt);
711 case CDAI_TYPE_NVME_CNTRL:
712 if (cdai->flags & CDAI_FLAG_STORE)
714 amt = sizeof(struct nvme_controller_data);
716 if (amt > cdai->bufsiz)
718 memcpy(cdai->buf, device->nvme_cdata, amt);
720 case CDAI_TYPE_NVME_NS:
721 if (cdai->flags & CDAI_FLAG_STORE)
723 amt = sizeof(struct nvme_namespace_data);
725 if (amt > cdai->bufsiz)
727 memcpy(cdai->buf, device->nvme_data, amt);
732 start_ccb->ccb_h.status = CAM_REQ_CMP;
734 if (cdai->flags & CDAI_FLAG_STORE) {
735 xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
736 (void *)(uintptr_t)cdai->buftype);
741 nvme_action(union ccb *start_ccb)
743 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
744 ("nvme_action: func= %#x\n", start_ccb->ccb_h.func_code));
746 switch (start_ccb->ccb_h.func_code) {
750 nvme_scan_lun(start_ccb->ccb_h.path->periph,
751 start_ccb->ccb_h.path, start_ccb->crcn.flags,
754 case XPT_DEV_ADVINFO:
755 nvme_dev_advinfo(start_ccb);
759 xpt_action_default(start_ccb);
765 * Handle any per-device event notifications that require action by the XPT.
768 nvme_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
769 struct cam_ed *device, void *async_arg)
773 * We only need to handle events for real devices.
775 if (target->target_id == CAM_TARGET_WILDCARD
776 || device->lun_id == CAM_LUN_WILDCARD)
779 if (async_code == AC_LOST_DEVICE &&
780 (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
781 device->flags |= CAM_DEV_UNCONFIGURED;
782 xpt_release_device(device);
787 nvme_announce_periph(struct cam_periph *periph)
789 struct ccb_pathinq cpi;
790 struct ccb_trans_settings cts;
791 struct cam_path *path = periph->path;
792 struct ccb_trans_settings_nvme *nvmex;
796 cam_periph_assert(periph, MA_OWNED);
798 /* Ask the SIM for connection details */
799 memset(&cts, 0, sizeof(cts));
800 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
801 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
802 cts.type = CTS_TYPE_CURRENT_SETTINGS;
803 xpt_action((union ccb*)&cts);
804 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
806 nvmex = &cts.xport_specific.nvme;
808 /* Ask the SIM for its base transfer speed */
809 xpt_path_inq(&cpi, periph->path);
810 sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
811 sbuf_printf(&sb, "%s%d: nvme version %d.%d",
812 periph->periph_name, periph->unit_number,
813 NVME_MAJOR(nvmex->spec),
814 NVME_MINOR(nvmex->spec));
815 if (nvmex->valid & CTS_NVME_VALID_LINK)
816 sbuf_printf(&sb, " x%d (max x%d) lanes PCIe Gen%d (max Gen%d) link",
817 nvmex->lanes, nvmex->max_lanes,
818 nvmex->speed, nvmex->max_speed);
819 sbuf_printf(&sb, "\n");
825 nvme_proto_announce(struct cam_ed *device)
830 sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
831 nvme_print_ident(device->nvme_cdata, device->nvme_data, &sb);
837 nvme_proto_denounce(struct cam_ed *device)
840 nvme_proto_announce(device);
844 nvme_proto_debug_out(union ccb *ccb)
846 char cdb_str[(sizeof(struct nvme_command) * 3) + 1];
848 if (ccb->ccb_h.func_code != XPT_NVME_IO &&
849 ccb->ccb_h.func_code != XPT_NVME_ADMIN)
852 CAM_DEBUG(ccb->ccb_h.path,
853 CAM_DEBUG_CDB,("%s. NCB: %s\n", nvme_op_string(&ccb->nvmeio.cmd,
854 ccb->ccb_h.func_code == XPT_NVME_ADMIN),
855 nvme_cmd_string(&ccb->nvmeio.cmd, cdb_str, sizeof(cdb_str))));