status as 'OK' if the device doesn't support the IE page. + * For now, just report the facts. + */ + if ((cam_send_ccb(fsmart->camdev, ccb) >= 0) && + ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)) { + if ((ie.hdr.param_len < 4) || ie.ie_asc || ie.ie_ascq) { + printf("Log Sense, Informational Exceptions failed " + "(length=%u asc=%#x ascq=%#x)\n", + ie.hdr.param_len, ie.ie_asc, ie.ie_ascq); + } else { + sinfo->supported = true; + } + } + +__device_info_scsi_out: + free(snum); + if (ccb) + cam_freeccb(ccb); + + return 0; +} + +static int32_t +__device_info_nvme(struct fbsd_smart *fsmart, struct ccb_getdev *cgd) +{ + union ccb *ccb; + smart_info_t *sinfo = NULL; + struct nvme_controller_data cd; + + if (!fsmart || !cgd) { + return -1; + } + + sinfo = &fsmart->common.info; + + sinfo->supported = true; + + ccb = cam_getccb(fsmart->camdev); + if (ccb != NULL) { + struct ccb_dev_advinfo *cdai = &ccb->cdai; + + CCB_CLEAR_ALL_EXCEPT_HDR(cdai); + + cdai->ccb_h.func_code = XPT_DEV_ADVINFO; + cdai->ccb_h.flags = CAM_DIR_IN; + cdai->flags = CDAI_FLAG_NONE; +#ifdef CDAI_TYPE_NVME_CNTRL + cdai->buftype = CDAI_TYPE_NVME_CNTRL; +#else + cdai->buftype = 6; +#endif + cdai->bufsiz = sizeof(struct nvme_controller_data); + cdai->buf = (uint8_t *)&cd; + + if (cam_send_ccb(fsmart->camdev, ccb) >= 0) { + if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { + cam_strvis((uint8_t *)sinfo->device, cd.mn, + sizeof(cd.mn), + sizeof(sinfo->device)); + cam_strvis((uint8_t *)sinfo->rev, cd.fr, + sizeof(cd.fr), + sizeof(sinfo->rev)); + cam_strvis((uint8_t *)sinfo->serial, cd.sn, + sizeof(cd.sn), + sizeof(sinfo->serial)); + } + } + + cam_freeccb(ccb); + } + + return 0; +} + +static int32_t +__device_info_tunneled_ata(struct fbsd_smart *fsmart) +{ + struct ata_params ident_data; + union ccb *ccb = NULL; + struct ata_pass_16 *ata_pass_16; + struct ata_cmd ata_cmd; + int32_t rc = -1; + + ccb = cam_getccb(fsmart->camdev); + if (ccb == NULL) { + goto __device_info_tunneled_ata_out; + } + + memset(&ident_data, 0, sizeof(struct ata_params)); + + CCB_CLEAR_ALL_EXCEPT_HDR(ccb); + + scsi_ata_pass_16(&ccb->csio, + /*retries*/ 1, + /*cbfcnp*/ NULL, + /*flags*/ CAM_DIR_IN, + /*tag_action*/ MSG_SIMPLE_Q_TAG, + /*protocol*/ AP_PROTO_PIO_IN, + /*ata_flags*/ AP_FLAG_TLEN_SECT_CNT | + AP_FLAG_BYT_BLOK_BLOCKS | + AP_FLAG_TDIR_FROM_DEV, + /*features*/ 0, + /*sector_count*/sizeof(struct ata_params), + /*lba*/ 0, + /*command*/ ATA_ATA_IDENTIFY, + /*control*/ 0, + /*data_ptr*/ (uint8_t *)&ident_data, + /*dxfer_len*/ sizeof(struct ata_params), + /*sense_len*/ SSD_FULL_SIZE, + /*timeout*/ 5000 + ); + + ata_pass_16 = (struct ata_pass_16 *)ccb->csio.cdb_io.cdb_bytes; + ata_cmd.command = ata_pass_16->command; + ata_cmd.control = ata_pass_16->control; + ata_cmd.features = ata_pass_16->features; + + rc = cam_send_ccb(fsmart->camdev, ccb); + if (rc != 0) { + warnx("%s: scsi_ata_pass_16() failed (programmer error?)", + __func__); + goto __device_info_tunneled_ata_out; + } + + fsmart->common.info.supported = ident_data.support.command1 & + ATA_SUPPORT_SMART; + + dprintf("ATA command1 = %#x\n", ident_data.support.command1); + +__device_info_tunneled_ata_out: + if (ccb) { + cam_freeccb(ccb); + } + + return rc; +} + +/** + * Retrieve the device information and use to populate the info structure + */ +static int32_t +__device_get_info(struct fbsd_smart *fsmart) +{ + union ccb *ccb; + int32_t rc = -1; + + if (!fsmart || !fsmart->camdev) { + warn("Bad handle %p", fsmart); + return -1; + } + + ccb = cam_getccb(fsmart->camdev); + if (ccb != NULL) { + struct ccb_getdev *cgd = &ccb->cgd; + + CCB_CLEAR_ALL_EXCEPT_HDR(cgd); + + /* + * GDEV_TYPE doesn't support NVMe. What we do get is: + * - device (ata/model, scsi/product) + * - revision (ata, scsi) + * - serial (ata) + * - vendor (scsi) + * - supported (ata) + * + * Serial # for all proto via ccb_dev_advinfo (buftype CDAI_TYPE_SERIAL_NUM) + */ + ccb->ccb_h.func_code = XPT_GDEV_TYPE; + + if (cam_send_ccb(fsmart->camdev, ccb) >= 0) { + if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { + switch (cgd->protocol) { + case PROTO_ATA: + rc = __device_info_ata(fsmart, cgd); + break; + case PROTO_SCSI: + rc = __device_info_scsi(fsmart, cgd); + if (!rc && fsmart->common.protocol == SMART_PROTO_ATA) { + rc = __device_info_tunneled_ata(fsmart); + } + break; + case PROTO_NVME: + rc = __device_info_nvme(fsmart, cgd); + break; + default: + printf("%s: unsupported protocol %d\n", + __func__, cgd->protocol); + } + } + } + + cam_freeccb(ccb); + } + + return rc; +} diff --git a/contrib/smart/libsmart.c b/contrib/smart/libsmart.c new file mode 100644 index 000000000000..a1732de09ed9 --- /dev/null +++ b/contrib/smart/libsmart.c @@ -0,0 +1,1359 @@ +/* + * Copyright (c) 2016-2026 Chuck Tuffli + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#include *** 2517 LINES SKIPPED ***