#include <sys/types.h>
#include <sys/param.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <cam/cam.h>
#include <cam/scsi/scsi_all.h>
#include <cam/cam_ccb.h>
#include <cam/scsi/scsi_pass.h>
#include "camlib.h"
static const char *nonrewind_devs[] = {
"sa"
};
char cam_errbuf[CAM_ERRBUF_SIZE];
static struct cam_device *cam_real_open_device(const char *path, int flags,
struct cam_device *device,
const char *given_path,
const char *given_dev_name,
int given_unit_number);
static struct cam_device *cam_lookup_pass(const char *dev_name, int unit,
int flags, const char *given_path,
struct cam_device *device);
int
cam_send_ccb(struct cam_device *device, union ccb *ccb)
{
return(ioctl(device->fd, CAMIOCOMMAND, ccb));
}
union ccb *
cam_getccb(struct cam_device *dev)
{
union ccb *ccb;
ccb = calloc(1, sizeof(*ccb));
if (ccb != NULL) {
ccb->ccb_h.path_id = dev->path_id;
ccb->ccb_h.target_id = dev->target_id;
ccb->ccb_h.target_lun = dev->target_lun;
}
return(ccb);
}
void
cam_freeccb(union ccb *ccb)
{
free(ccb);
}
int
cam_get_device(const char *path, char *dev_name, int devnamelen, int *unit)
{
char *tmpstr, *tmpstr2;
char *newpath;
int unit_offset;
int i;
if (path == NULL) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: device pathname was NULL", __func__);
return(-1);
}
newpath = realpath(path, NULL);
if (newpath == NULL)
newpath = strdup(path);
if (newpath == NULL)
return (-1);
tmpstr = newpath;
if (*tmpstr == '/') {
tmpstr2 = tmpstr;
tmpstr = strrchr(tmpstr2, '/');
assert(tmpstr != NULL && *tmpstr != '\0');
tmpstr++;
}
if (*tmpstr == '\0') {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: no text after slash", __func__);
free(newpath);
return(-1);
}
if (*tmpstr == 'n' || *tmpstr == 'e') {
for (i = 0; i < sizeof(nonrewind_devs)/sizeof(char *); i++) {
int len = strlen(nonrewind_devs[i]);
if (strncmp(tmpstr + 1, nonrewind_devs[i], len) == 0) {
if (isdigit(tmpstr[len + 1])) {
tmpstr++;
break;
}
}
}
}
if (strlen(tmpstr) < 2) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: must have both device name and unit number",
__func__);
free(newpath);
return(-1);
}
if (isdigit(*tmpstr)) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: device name cannot begin with a number",
__func__);
free(newpath);
return(-1);
}
if (!isdigit(tmpstr[strlen(tmpstr) - 1])) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: unable to find device unit number", __func__);
free(newpath);
return(-1);
}
for (unit_offset = 1;
(unit_offset < (strlen(tmpstr)))
&& (isdigit(tmpstr[strlen(tmpstr) - unit_offset])); unit_offset++);
unit_offset--;
*unit = atoi(&tmpstr[strlen(tmpstr) - unit_offset]);
tmpstr[strlen(tmpstr) - unit_offset] = '\0';
strlcpy(dev_name, tmpstr, devnamelen);
free(newpath);
return(0);
}
struct cam_device *
cam_open_device(const char *path, int flags)
{
int unit;
char dev_name[DEV_IDLEN + 1];
if (cam_get_device(path, dev_name, sizeof(dev_name), &unit) == -1)
return(NULL);
return(cam_lookup_pass(dev_name, unit, flags, path, NULL));
}
struct cam_device *
cam_open_btl(path_id_t path_id, target_id_t target_id, lun_id_t target_lun,
int flags, struct cam_device *device)
{
union ccb ccb;
struct periph_match_pattern *match_pat;
int fd, bufsize;
if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: couldn't open %s\n%s: %s", __func__, XPT_DEVICE,
__func__, strerror(errno));
return(NULL);
}
bzero(&ccb, sizeof(union ccb));
ccb.ccb_h.func_code = XPT_DEV_MATCH;
ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
bufsize = sizeof(struct dev_match_result);
ccb.cdm.match_buf_len = bufsize;
ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
if (ccb.cdm.matches == NULL) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: couldn't malloc match buffer", __func__);
close(fd);
return(NULL);
}
ccb.cdm.num_matches = 0;
ccb.cdm.num_patterns = 1;
ccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
ccb.cdm.patterns = (struct dev_match_pattern *)malloc(
sizeof(struct dev_match_pattern));
if (ccb.cdm.patterns == NULL) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: couldn't malloc pattern buffer", __func__);
free(ccb.cdm.matches);
ccb.cdm.matches = NULL;
close(fd);
return(NULL);
}
ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH;
match_pat = &ccb.cdm.patterns[0].pattern.periph_pattern;
sprintf(match_pat->periph_name, "pass");
match_pat->path_id = path_id;
match_pat->target_id = target_id;
match_pat->target_lun = target_lun;
match_pat->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_TARGET |
PERIPH_MATCH_LUN | PERIPH_MATCH_NAME;
if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: CAMIOCOMMAND ioctl failed\n"
"%s: %s", __func__, __func__, strerror(errno));
goto btl_bailout;
}
if ((ccb.ccb_h.status != CAM_REQ_CMP)
|| ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
&& (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: CAM error %#x, CDM error %d "
"returned from XPT_DEV_MATCH ccb", __func__,
ccb.ccb_h.status, ccb.cdm.status);
goto btl_bailout;
}
if (ccb.cdm.status == CAM_DEV_MATCH_MORE) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: CDM reported more than one"
" passthrough device at %d:%d:%jx!!\n",
__func__, path_id, target_id, (uintmax_t)target_lun);
goto btl_bailout;
}
if (ccb.cdm.num_matches == 0) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: no passthrough device found at"
" %d:%d:%jx", __func__, path_id, target_id,
(uintmax_t)target_lun);
goto btl_bailout;
}
switch(ccb.cdm.matches[0].type) {
case DEV_MATCH_PERIPH: {
int pass_unit;
char dev_path[256];
struct periph_match_result *periph_result;
periph_result = &ccb.cdm.matches[0].result.periph_result;
pass_unit = periph_result->unit_number;
free(ccb.cdm.matches);
ccb.cdm.matches = NULL;
free(ccb.cdm.patterns);
ccb.cdm.patterns = NULL;
close(fd);
sprintf(dev_path, "/dev/pass%d", pass_unit);
return(cam_real_open_device(dev_path, flags, device, NULL,
NULL, 0));
break;
}
default:
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: asked for a peripheral match, but"
" got a bus or device match", __func__);
goto btl_bailout;
break;
}
btl_bailout:
free(ccb.cdm.matches);
ccb.cdm.matches = NULL;
free(ccb.cdm.patterns);
ccb.cdm.patterns = NULL;
close(fd);
return(NULL);
}
struct cam_device *
cam_open_spec_device(const char *dev_name, int unit, int flags,
struct cam_device *device)
{
return(cam_lookup_pass(dev_name, unit, flags, NULL, device));
}
struct cam_device *
cam_open_pass(const char *path, int flags, struct cam_device *device)
{
return(cam_real_open_device(path, flags, device, path, NULL, 0));
}
static struct cam_device *
cam_lookup_pass(const char *dev_name, int unit, int flags,
const char *given_path, struct cam_device *device)
{
int fd;
union ccb ccb;
char dev_path[256];
if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: couldn't open %s\n%s: %s", __func__, XPT_DEVICE,
__func__, strerror(errno));
return(NULL);
}
ccb.ccb_h.func_code = XPT_GDEVLIST;
strlcpy(ccb.cgdl.periph_name, dev_name, sizeof(ccb.cgdl.periph_name));
ccb.cgdl.unit_number = unit;
if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
char tmpstr[256];
if (errno == ENOENT) {
snprintf(tmpstr, sizeof(tmpstr),
"\n%s: either the pass driver isn't in "
"your kernel\n%s: or %s%d doesn't exist",
__func__, __func__, dev_name, unit);
}
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: CAMGETPASSTHRU ioctl failed\n"
"%s: %s%s", __func__, __func__, strerror(errno),
(errno == ENOENT) ? tmpstr : "");
close(fd);
return(NULL);
}
close(fd);
if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: device %s%d does not exist!",
__func__, dev_name, unit);
return(NULL);
}
sprintf(dev_path, "/dev/%s%d", ccb.cgdl.periph_name,
ccb.cgdl.unit_number);
return(cam_real_open_device(dev_path, flags, device, NULL,
dev_name, unit));
}
static struct cam_device *
cam_real_open_device(const char *path, int flags, struct cam_device *device,
const char *given_path, const char *given_dev_name,
int given_unit_number)
{
union ccb ccb;
int fd = -1, malloced_device = 0;
if (device == NULL) {
if ((device = (struct cam_device *)malloc(
sizeof(struct cam_device))) == NULL) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: device structure malloc"
" failed\n%s: %s", __func__, __func__,
strerror(errno));
return(NULL);
}
device->fd = -1;
malloced_device = 1;
}
if (given_path != NULL)
strlcpy(device->device_path, given_path,
sizeof(device->device_path));
else
device->device_path[0] = '\0';
if (given_dev_name != NULL)
strlcpy(device->given_dev_name, given_dev_name,
sizeof(device->given_dev_name));
else
device->given_dev_name[0] = '\0';
device->given_unit_number = given_unit_number;
if ((fd = open(path, flags)) < 0) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: couldn't open passthrough device %s\n"
"%s: %s", __func__, path, __func__,
strerror(errno));
goto crod_bailout;
}
device->fd = fd;
bzero(&ccb, sizeof(union ccb));
ccb.ccb_h.func_code = XPT_GDEVLIST;
if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: CAMGETPASSTHRU ioctl failed\n"
"%s: %s", __func__, __func__, strerror(errno));
goto crod_bailout;
}
if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: passthrough device does not exist!", __func__);
goto crod_bailout;
}
device->dev_unit_num = ccb.cgdl.unit_number;
strlcpy(device->device_name, ccb.cgdl.periph_name,
sizeof(device->device_name));
device->path_id = ccb.ccb_h.path_id;
device->target_id = ccb.ccb_h.target_id;
device->target_lun = ccb.ccb_h.target_lun;
ccb.ccb_h.func_code = XPT_PATH_INQ;
if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: Path Inquiry CCB failed\n"
"%s: %s", __func__, __func__, strerror(errno));
goto crod_bailout;
}
strlcpy(device->sim_name, ccb.cpi.dev_name, sizeof(device->sim_name));
device->sim_unit_number = ccb.cpi.unit_number;
device->bus_id = ccb.cpi.bus_id;
ccb.ccb_h.func_code = XPT_GDEV_TYPE;
if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: Get Device Type CCB failed\n"
"%s: %s", __func__, __func__, strerror(errno));
goto crod_bailout;
}
device->pd_type = SID_TYPE(&ccb.cgd.inq_data);
bcopy(&ccb.cgd.inq_data, &device->inq_data,
sizeof(struct scsi_inquiry_data));
device->serial_num_len = ccb.cgd.serial_num_len;
bcopy(&ccb.cgd.serial_num, &device->serial_num, device->serial_num_len);
CCB_CLEAR_ALL_EXCEPT_HDR(&ccb.cts);
ccb.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
ccb.cts.type = CTS_TYPE_CURRENT_SETTINGS;
if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: Get Transfer Settings CCB failed\n"
"%s: %s", __func__, __func__, strerror(errno));
goto crod_bailout;
}
if (ccb.cts.transport == XPORT_SPI) {
struct ccb_trans_settings_spi *spi =
&ccb.cts.xport_specific.spi;
device->sync_period = spi->sync_period;
device->sync_offset = spi->sync_offset;
device->bus_width = spi->bus_width;
} else {
device->sync_period = 0;
device->sync_offset = 0;
device->bus_width = 0;
}
return(device);
crod_bailout:
if (fd >= 0)
close(fd);
if (malloced_device)
free(device);
return(NULL);
}
void
cam_close_device(struct cam_device *dev)
{
if (dev == NULL)
return;
cam_close_spec_device(dev);
free(dev);
}
void
cam_close_spec_device(struct cam_device *dev)
{
if (dev == NULL)
return;
if (dev->fd >= 0) {
close(dev->fd);
dev->fd = -1;
}
}
char *
cam_path_string(struct cam_device *dev, char *str, int len)
{
if (dev == NULL) {
snprintf(str, len, "No path");
return(str);
}
snprintf(str, len, "(%s%d:%s%d:%d:%d:%jx): ",
(dev->device_name[0] != '\0') ? dev->device_name : "pass",
dev->dev_unit_num,
(dev->sim_name[0] != '\0') ? dev->sim_name : "unknown",
dev->sim_unit_number,
dev->bus_id,
dev->target_id,
(uintmax_t)dev->target_lun);
return(str);
}
struct cam_device *
cam_device_dup(struct cam_device *device)
{
struct cam_device *newdev;
if (device == NULL) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: device is NULL", __func__);
return (NULL);
}
newdev = malloc(sizeof(struct cam_device));
if (newdev == NULL) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: couldn't malloc CAM device structure", __func__);
return (NULL);
}
bcopy(device, newdev, sizeof(struct cam_device));
return(newdev);
}
void
cam_device_copy(struct cam_device *src, struct cam_device *dst)
{
if (src == NULL) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: source device struct was NULL", __func__);
return;
}
if (dst == NULL) {
snprintf(cam_errbuf, nitems(cam_errbuf),
"%s: destination device struct was NULL", __func__);
return;
}
bcopy(src, dst, sizeof(struct cam_device));
}