/*-1* Copyright (c) 2021 Netflix, Inc.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND13* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE15* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE16* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS18* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)19* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT20* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY21* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF22* SUCH DAMAGE.23*/2425/*26* Wrapper functions to make requests and get answers w/o managing the27* details.28*/2930#include <sys/types.h>3132#include <err.h>33#include <stdio.h>34#include <stdlib.h>35#include <strings.h>3637#include <cam/cam.h>38#include <cam/cam_ccb.h>39#include <cam/scsi/scsi_all.h>40#include <cam/scsi/scsi_pass.h>41#include <cam/scsi/scsi_message.h>42#include "camlib.h"43#include "scsi_wrap.h"4445void *46scsi_wrap_get_physical_element_status(struct cam_device *device, int task_attr, int retry_count,47int timeout, uint8_t report_type, uint32_t start_element)48{49uint32_t allocation_length;50union ccb *ccb = NULL;51struct scsi_get_physical_element_hdr *hdr = NULL;52uint32_t dtors;53uint32_t reported;5455ccb = cam_getccb(device);56if (ccb == NULL) {57warnx("Can't allocate ccb");58return (NULL);59}6061/*62* Do the request up to twice. Once to get the length and once to get63* the data. We'll guess that 4096 is enough to almost always long64* enough since that's 127 entries and most drives have < 20 heads. If65* by chance it's not, then we'll loop once as we'll then know the66* proper length.67*/68allocation_length = MAX(sizeof(*hdr), 4096);69again:70free(hdr);71hdr = calloc(allocation_length, 1);72if (hdr == NULL) {73warnx("Can't allocate memory for physical element list");74return (NULL);75}7677scsi_get_physical_element_status(&ccb->csio,78retry_count,79NULL,80task_attr,81(uint8_t *)hdr,82allocation_length,83report_type,84start_element,85SSD_FULL_SIZE,86timeout);8788/* Disable freezing the device queue */89ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;9091if (cam_send_ccb(device, ccb) < 0) {92warn("error sending GET PHYSICAL ELEMENT STATUS command");93goto errout;94}9596if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {97cam_error_print(device, ccb, CAM_ESF_ALL,98CAM_EPF_ALL, stderr);99goto errout;100}101102dtors = scsi_4btoul(hdr->num_descriptors);103reported = scsi_4btoul(hdr->num_returned);104if (dtors != 0 && dtors != reported) {105/*106* Get all the data... in the future we may need to step through107* a long list, but so far all drives I've found fit into one108* response. A 4k transfer can do 128 heads and current designs109* have 16.110*/111allocation_length = dtors * sizeof(struct scsi_get_physical_element_descriptor) +112sizeof(*hdr);113goto again;114}115cam_freeccb(ccb);116return (hdr);117errout:118cam_freeccb(ccb);119free(hdr);120return (NULL);121}122123void *124scsi_wrap_inquiry(struct cam_device *device, uint32_t page, uint32_t length)125{126union ccb *ccb;127uint8_t *buf;128129ccb = cam_getccb(device);130131if (ccb == NULL)132return (NULL);133134buf = malloc(length);135136if (buf == NULL) {137cam_freeccb(ccb);138return (NULL);139}140141scsi_inquiry(&ccb->csio,142/*retries*/ 0,143/*cbfcnp*/ NULL,144/* tag_action */ MSG_SIMPLE_Q_TAG,145/* inq_buf */ (uint8_t *)buf,146/* inq_len */ length,147/* evpd */ 1,148/* page_code */ page,149/* sense_len */ SSD_FULL_SIZE,150/* timeout */ 5000);151152/* Disable freezing the device queue */153ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;154// ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;155156if (cam_send_ccb(device, ccb) < 0) {157warn("error sending INQUIRY command");158cam_freeccb(ccb);159free(buf);160return (NULL);161}162163if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {164free(buf);165buf = NULL;166}167cam_freeccb(ccb);168return (buf);169}170171struct scsi_vpd_block_device_characteristics *172scsi_wrap_vpd_block_device_characteristics(struct cam_device *device)173{174175return ((struct scsi_vpd_block_device_characteristics *)scsi_wrap_inquiry(176device, SVPD_BDC, sizeof(struct scsi_vpd_block_device_characteristics)));177}178179180