#include <sys/types.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_pass.h>
#include <cam/scsi/scsi_message.h>
#include "camlib.h"
#include "scsi_wrap.h"
void *
scsi_wrap_get_physical_element_status(struct cam_device *device, int task_attr, int retry_count,
int timeout, uint8_t report_type, uint32_t start_element)
{
uint32_t allocation_length;
union ccb *ccb = NULL;
struct scsi_get_physical_element_hdr *hdr = NULL;
uint32_t dtors;
uint32_t reported;
ccb = cam_getccb(device);
if (ccb == NULL) {
warnx("Can't allocate ccb");
return (NULL);
}
allocation_length = MAX(sizeof(*hdr), 4096);
again:
free(hdr);
hdr = calloc(allocation_length, 1);
if (hdr == NULL) {
warnx("Can't allocate memory for physical element list");
return (NULL);
}
scsi_get_physical_element_status(&ccb->csio,
retry_count,
NULL,
task_attr,
(uint8_t *)hdr,
allocation_length,
report_type,
start_element,
SSD_FULL_SIZE,
timeout);
ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
if (cam_send_ccb(device, ccb) < 0) {
warn("error sending GET PHYSICAL ELEMENT STATUS command");
goto errout;
}
if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
cam_error_print(device, ccb, CAM_ESF_ALL,
CAM_EPF_ALL, stderr);
goto errout;
}
dtors = scsi_4btoul(hdr->num_descriptors);
reported = scsi_4btoul(hdr->num_returned);
if (dtors != 0 && dtors != reported) {
allocation_length = dtors * sizeof(struct scsi_get_physical_element_descriptor) +
sizeof(*hdr);
goto again;
}
cam_freeccb(ccb);
return (hdr);
errout:
cam_freeccb(ccb);
free(hdr);
return (NULL);
}
void *
scsi_wrap_inquiry(struct cam_device *device, uint32_t page, uint32_t length)
{
union ccb *ccb;
uint8_t *buf;
ccb = cam_getccb(device);
if (ccb == NULL)
return (NULL);
buf = malloc(length);
if (buf == NULL) {
cam_freeccb(ccb);
return (NULL);
}
scsi_inquiry(&ccb->csio,
0,
NULL,
MSG_SIMPLE_Q_TAG,
(uint8_t *)buf,
length,
1,
page,
SSD_FULL_SIZE,
5000);
ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
if (cam_send_ccb(device, ccb) < 0) {
warn("error sending INQUIRY command");
cam_freeccb(ccb);
free(buf);
return (NULL);
}
if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
free(buf);
buf = NULL;
}
cam_freeccb(ccb);
return (buf);
}
struct scsi_vpd_block_device_characteristics *
scsi_wrap_vpd_block_device_characteristics(struct cam_device *device)
{
return ((struct scsi_vpd_block_device_characteristics *)scsi_wrap_inquiry(
device, SVPD_BDC, sizeof(struct scsi_vpd_block_device_characteristics)));
}