summaryrefslogtreecommitdiffstats
path: root/lib/libcam
diff options
context:
space:
mode:
authorgibbs <gibbs@FreeBSD.org>1998-09-15 06:16:46 +0000
committergibbs <gibbs@FreeBSD.org>1998-09-15 06:16:46 +0000
commit5987d73d1d29fb02fde99b7f92cad6f990cb97ce (patch)
tree6cbef733487fe8cc4dda5e08c5d58a0c0abff682 /lib/libcam
parentb12009dccc8fdf4722588d378418b455cc8b3673 (diff)
downloadFreeBSD-src-5987d73d1d29fb02fde99b7f92cad6f990cb97ce.zip
FreeBSD-src-5987d73d1d29fb02fde99b7f92cad6f990cb97ce.tar.gz
CAM userland utility library, a replacement for libscsi.
Submitted by: "Kenneth D. Merry" <ken@FreeBSD.org>
Diffstat (limited to 'lib/libcam')
-rw-r--r--lib/libcam/Makefile47
-rw-r--r--lib/libcam/Makefile.orig0
-rw-r--r--lib/libcam/camlib.c757
-rw-r--r--lib/libcam/camlib.c.orig0
-rw-r--r--lib/libcam/camlib.h178
-rw-r--r--lib/libcam/camlib.h.orig0
-rw-r--r--lib/libcam/scsi_cmdparse.c792
-rw-r--r--lib/libcam/scsi_cmdparse.c.orig0
8 files changed, 1774 insertions, 0 deletions
diff --git a/lib/libcam/Makefile b/lib/libcam/Makefile
new file mode 100644
index 0000000..b7e7189
--- /dev/null
+++ b/lib/libcam/Makefile
@@ -0,0 +1,47 @@
+
+LIB= cam
+SRCS= camlib.c scsi_cmdparse.c scsi_all.c scsi_da.c scsi_sa.c cam.c
+
+# MAN3= cam.3
+
+# XXX KDM Need to write the man pages.
+# MLINKS+= cam.3 cam_open_device.3 \
+# cam.3 cam_close_device.3 \
+# cam.3 cam_getccb.3 \
+# cam.3 cam_freeccb.3 \
+# cam.3 cam_send_ccb.3 \
+# cam.3 cam_device_dup.3 \
+# cam.3 cam_device_copy.3 \
+# cam.3 scsi_read_write.3 \
+# cam.3 scsi_start_stop.3 \
+# cam.3 scsi_sense_desc.3 \
+# cam.3 scsi_op_desc.3 \
+# cam.3 scsi_cdb_string.3 \
+# cam.3 scsi_print_inquriy.3 \
+# cam.3 scsi_calc_syncsrate.3 \ # XXX
+# cam.3 scsi_test_unit_ready.3 \
+# cam.3 scsi_inquiry.3 \
+# cam.3 scsi_read_capacity.3 \
+# cam.3 scsi_prevent.3 \
+# cam.3 scsi_synchronize_cache.3 \
+# cam.3 scsi_sense_string.3 \
+# cam.3 scsi_sense_print.3 \
+# cam.3 scsi_inqurity_match.3 \ # XXX
+# cam.3 scsi_extract_sense.3 \
+# cam.3 scsi_ulto2b.3 \
+# cam.3 scsi_ulto3b.3 \
+# cam.3 scsi_ulto4b.3 \
+# cam.3 scsi_2btoul.3 \
+# cam.3 scsi_3btoul.3 \
+# cam.3 scsi_3btol.3 \
+# cam.3 scsi_4btoul.3
+
+beforeinstall:
+ ${INSTALL} -C -o ${BINOWN} -g ${BINGRP} -m 444 ${.CURDIR}/camlib.h \
+ ${DESTDIR}/usr/include
+
+.PATH: ${.CURDIR}/../../sys/cam/scsi ${.CURDIR}/../../sys/cam
+
+CFLAGS+=-I${.CURDIR} -I${.CURDIR}/../../sys
+
+.include <bsd.lib.mk>
diff --git a/lib/libcam/Makefile.orig b/lib/libcam/Makefile.orig
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/libcam/Makefile.orig
diff --git a/lib/libcam/camlib.c b/lib/libcam/camlib.c
new file mode 100644
index 0000000..af62266
--- /dev/null
+++ b/lib/libcam/camlib.c
@@ -0,0 +1,757 @@
+/*
+ * Copyright (c) 1997, 1998 Kenneth D. Merry.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $Id$
+ */
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <stdio.h>
+#include <stdlib.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"
+
+struct cam_devequiv {
+ char *given_dev;
+ char *real_dev;
+};
+
+struct cam_devequiv devmatchtable[] = {
+ {"sd", "da"},
+ {"st", "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);
+
+/*
+ * Send a ccb to a passthrough device.
+ */
+int
+cam_send_ccb(struct cam_device *device, union ccb *ccb)
+{
+ return(ioctl(device->fd, CAMIOCOMMAND, ccb));
+}
+
+/*
+ * Malloc a CCB, zero out the header and set its path, target and lun ids.
+ */
+union ccb *
+cam_getccb(struct cam_device *dev)
+{
+ union ccb *ccb;
+
+ ccb = (union ccb *)malloc(sizeof(union ccb));
+ if (ccb != NULL) {
+ bzero(&ccb->ccb_h, sizeof(struct ccb_hdr));
+ 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);
+}
+
+/*
+ * Free a CCB.
+ */
+void
+cam_freeccb(union ccb *ccb)
+{
+ if (ccb != NULL)
+ free(ccb);
+}
+
+/*
+ * Take a device name or path passed in by the user, and attempt to figure
+ * out the device name and unit number. Some possible device name formats are:
+ * /dev/foo0a
+ * /dev/rfoo0a
+ * /dev/rfoos2c
+ * foo0
+ * foo0a
+ * rfoo0
+ * rfoo0a
+ * nrfoo0
+ *
+ * If the caller passes in an old style device name like 'sd' or 'st',
+ * it will be converted to the new style device name based upon devmatchtable
+ * above.
+ *
+ * Input parameters: device name/path, length of devname string
+ * Output: device name, unit number
+ * Return values: returns 0 for success, -1 for failure
+ */
+int
+cam_get_device(const char *path, char *dev_name, int devnamelen, int *unit)
+{
+ char *func_name = "cam_get_device";
+ char *tmpstr, *tmpstr2;
+ char *newpath;
+ int unit_offset;
+ int i, found = 0;
+
+
+ if (path == NULL) {
+ sprintf(cam_errbuf, "%s: device pathname was NULL", func_name);
+ return(0);
+ }
+
+ /*
+ * We can be rather destructive to the path string. Make a copy of
+ * it so we don't hose the user's string.
+ */
+ newpath = (char *)strdup(path);
+ tmpstr = newpath;
+
+ /* Get rid of any leading white space */
+ while (isspace(*tmpstr) && (*tmpstr != '\0'))
+ tmpstr++;
+
+ /*
+ * Check to see whether we have an absolute pathname.
+ */
+ if (*tmpstr == '/') {
+ tmpstr2 = tmpstr;
+ tmpstr = (char *)rindex(tmpstr2, '/');
+ if ((tmpstr != NULL) && (*tmpstr != '\0'))
+ tmpstr++;
+ }
+
+ if (*tmpstr == '\0') {
+ sprintf(cam_errbuf, "%s: no text after slash", func_name);
+ free(newpath);
+ return(0);
+ }
+
+ /*
+ * Check to see whether the user has given us a nonrewound tape
+ * device.
+ */
+ if (*tmpstr == 'n')
+ tmpstr++;
+
+ if (*tmpstr == '\0') {
+ sprintf(cam_errbuf, "%s: no text after leading 'n'", func_name);
+ free(newpath);
+ return(0);
+ }
+
+ /*
+ * See if the user has given us a character device.
+ */
+ if (*tmpstr == 'r')
+ tmpstr++;
+
+ if (*tmpstr == '\0') {
+ sprintf(cam_errbuf, "%s: no text after leading 'r'", func_name);
+ free(newpath);
+ return(0);
+ }
+
+ /*
+ * Try to get rid of any trailing white space or partition letters.
+ */
+ tmpstr2 = &tmpstr[strlen(tmpstr) - 1];
+
+ while ((*tmpstr2 != '\0') && (tmpstr2 > tmpstr) &&(!isdigit(*tmpstr2))){
+ *tmpstr2 = '\0';
+ tmpstr2--;
+ }
+
+ /*
+ * Check to see whether we have been given a partition with a slice
+ * name. If so, get rid of the slice name/number.
+ */
+ if (strlen(tmpstr) > 3) {
+ /*
+ * Basically, we're looking for a string that ends in the
+ * following general manner: 1s1 -- a number, the letter
+ * s, and then another number. This indicates that the
+ * user has given us a slice. We substitute nulls for the
+ * s and the slice number.
+ */
+ if ((isdigit(tmpstr[strlen(tmpstr) - 1]))
+ && (tmpstr[strlen(tmpstr) - 2] == 's')
+ && (isdigit(tmpstr[strlen(tmpstr) - 3]))) {
+ tmpstr[strlen(tmpstr) - 1] = '\0';
+ tmpstr[strlen(tmpstr) - 1] = '\0';
+ }
+ }
+
+ /*
+ * After we nuke off the slice, we should have just a device name
+ * and unit number. That means there must be at least 2
+ * characters. If we only have 1, we don't have a valid device name.
+ */
+ if (strlen(tmpstr) < 2) {
+ sprintf(cam_errbuf,
+ "%s: must have both device name and unit number",
+ func_name);
+ free(newpath);
+ return(0);
+ }
+
+ /*
+ * If the first character of the string is a digit, then the user
+ * has probably given us all numbers. Point out the error.
+ */
+ if (isdigit(*tmpstr)) {
+ sprintf(cam_errbuf,
+ "%s: device name cannot begin with a number",
+ func_name);
+ free(newpath);
+ return(0);
+ }
+
+ /*
+ * At this point, if the last character of the string isn't a
+ * number, we know the user either didn't give us a device number,
+ * or he gave us a device name/number format we don't recognize.
+ */
+ if (!isdigit(tmpstr[strlen(tmpstr) - 1])) {
+ sprintf(cam_errbuf, "%s: unable to find device unit number",
+ func_name);
+ free(newpath);
+ return(0);
+ }
+
+ /*
+ * Attempt to figure out where the device name ends and the unit
+ * number begins. As long as unit_offset is at least 1 less than
+ * the length of the string, we can still potentially have a device
+ * name at the front of the string. When we get to something that
+ * isn't a digit, we've hit the device name. Because of the check
+ * above, we know that this cannot happen when unit_offset == 1.
+ * Therefore it is okay to decrement unit_offset -- it won't cause
+ * us to go past the end of the character array.
+ */
+ for (unit_offset = 1;
+ (unit_offset < (strlen(tmpstr)))
+ && (isdigit(tmpstr[strlen(tmpstr) - unit_offset])); unit_offset++);
+
+ unit_offset--;
+
+ /*
+ * Grab the unit number.
+ */
+ *unit = atoi(&tmpstr[strlen(tmpstr) - unit_offset]);
+
+ /*
+ * Put a null in place of the first number of the unit number so
+ * that all we have left is the device name.
+ */
+ tmpstr[strlen(tmpstr) - unit_offset] = '\0';
+
+ /*
+ * Look through our equivalency table and see if the device name
+ * the user gave us is an old style device name. If so, translate
+ * it to the new style device name.
+ */
+ for (i = 0;i < (sizeof(devmatchtable)/sizeof(struct cam_devequiv));i++){
+ if (strcmp(tmpstr, devmatchtable[i].given_dev) == 0) {
+ strncpy(dev_name,devmatchtable[i].real_dev, devnamelen);
+ found = 1;
+ break;
+ }
+ }
+ if (found == 0)
+ strncpy(dev_name, tmpstr, devnamelen);
+
+ /* Make sure we pass back a null-terminated string */
+ dev_name[devnamelen - 1] = '\0';
+
+ /* Clean up allocated memory */
+ free(newpath);
+
+ return(1);
+
+}
+
+/*
+ * Backwards compatible wrapper for the real open routine. This translates
+ * a pathname into a device name and unit number for use with the real open
+ * routine.
+ */
+struct cam_device *
+cam_open_device(const char *path, int flags)
+{
+ int unit;
+ char dev_name[DEV_IDLEN + 1];
+
+ /*
+ * cam_get_device() has already put an error message in cam_errbuf,
+ * so we don't need to.
+ */
+ if (cam_get_device(path, dev_name, DEV_IDLEN + 1, &unit) == 0)
+ return(NULL);
+
+ return(cam_lookup_pass(dev_name, unit, flags, path, NULL));
+}
+
+/*
+ * Open the passthrough device for a given bus, target and lun, if the
+ * passthrough device exists.
+ */
+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;
+ char *func_name = "cam_open_btl";
+ int fd, bufsize;
+
+ if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
+ snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
+ "%s: couldn't open %s\n%s: %s", func_name, XPT_DEVICE,
+ func_name, strerror(errno));
+ return(NULL);
+ }
+
+ bzero(&ccb, sizeof(union ccb));
+ ccb.ccb_h.func_code = XPT_DEV_MATCH;
+
+ /* Setup the result buffer */
+ 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, CAM_ERRBUF_SIZE,
+ "%s: couldn't malloc match buffer", func_name);
+ return(NULL);
+ }
+ ccb.cdm.num_matches = 0;
+
+ /* Setup the pattern buffer */
+ 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, CAM_ERRBUF_SIZE,
+ "%s: couldn't malloc pattern buffer", func_name);
+ free(ccb.cdm.matches);
+ return(NULL);
+ }
+ ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH;
+ match_pat = &ccb.cdm.patterns[0].pattern.periph_pattern;
+
+ /*
+ * We're looking for the passthrough device associated with this
+ * particular bus/target/lun.
+ */
+ sprintf(match_pat->periph_name, "pass");
+ match_pat->path_id = path_id;
+ match_pat->target_id = target_id;
+ match_pat->target_lun = target_lun;
+ /* Now set the flags to indicate what we're looking for. */
+ match_pat->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_TARGET |
+ PERIPH_MATCH_LUN | PERIPH_MATCH_NAME;
+
+ if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
+ sprintf(cam_errbuf, "%s: CAMIOCOMMAND ioctl failed\n"
+ "%s: %s", func_name, func_name, strerror(errno));
+ goto btl_bailout;
+ }
+
+ /*
+ * Check for an outright error.
+ */
+ if ((ccb.ccb_h.status != CAM_REQ_CMP)
+ || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
+ && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
+ sprintf(cam_errbuf, "%s: CAM error %#x, CDM error %d "
+ "returned from XPT_DEV_MATCH ccb", func_name,
+ ccb.ccb_h.status, ccb.cdm.status);
+ goto btl_bailout;
+ }
+
+ if (ccb.cdm.status == CAM_DEV_MATCH_MORE) {
+ sprintf(cam_errbuf, "%s: CDM reported more than one"
+ " passthrough device at %d:%d:%d!!\n",
+ func_name, path_id, target_id, target_lun);
+ goto btl_bailout;
+ }
+
+ if (ccb.cdm.num_matches == 0) {
+ sprintf(cam_errbuf, "%s: no passthrough device found at"
+ " %d:%d:%d", func_name, path_id, target_id,
+ 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);
+ free(ccb.cdm.patterns);
+ sprintf(dev_path, "/dev/pass%d", pass_unit);
+ return(cam_real_open_device(dev_path, flags, device, NULL,
+ NULL, 0));
+ break; /* NOTREACHED */
+ }
+ default:
+ sprintf(cam_errbuf, "%s: asked for a peripheral match, but"
+ " got a bus or device match??!!", func_name);
+ goto btl_bailout;
+ break; /* NOTREACHED */
+ }
+
+btl_bailout:
+ free(ccb.cdm.matches);
+ free(ccb.cdm.patterns);
+ 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];
+ char *func_name = "cam_lookup_pass";
+
+ /*
+ * The flags argument above only applies to the actual passthrough
+ * device open, not our open of the given device to find the
+ * passthrough device.
+ */
+ if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
+ snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
+ "%s: couldn't open %s\n%s: %s", func_name, XPT_DEVICE,
+ func_name, strerror(errno));
+ return(NULL);
+ }
+
+ /* This isn't strictly necessary for the GETPASSTHRU ioctl. */
+ ccb.ccb_h.func_code = XPT_GDEVLIST;
+
+ /* These two are necessary for the GETPASSTHRU ioctl to work. */
+ strncpy(ccb.cgdl.periph_name, dev_name, DEV_IDLEN - 1);
+ ccb.cgdl.periph_name[DEV_IDLEN - 1] = '\0';
+ ccb.cgdl.unit_number = unit;
+
+ /*
+ * Attempt to get the passthrough device. This ioctl will fail if
+ * the device name is null, or if the device doesn't exist.
+ */
+ if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
+ sprintf(cam_errbuf, "%s: CAMGETPASSTHRU ioctl failed\n"
+ "%s: %s", func_name, func_name, strerror(errno));
+ return(NULL);
+ }
+
+ close(fd);
+
+ /*
+ * If the ioctl returned the right status, but we got an error back
+ * in the ccb, that means that the kernel found the device the user
+ * passed in, but was unable to find the passthrough device for
+ * the device the user gave us.
+ */
+ if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
+ sprintf(cam_errbuf, "%s: device %s%d does not exist",
+ func_name, 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));
+}
+
+/*
+ * Open a given device. The path argument isn't strictly necessary, but it
+ * is copied into the cam_device structure as a convenience to the user.
+ */
+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)
+{
+ char newpath[MAXPATHLEN+1];
+ char *func_name = "cam_real_open_device";
+ union ccb ccb;
+ int fd, malloced_device = 0;
+
+ /*
+ * See if the user wants us to malloc a device for him.
+ */
+ if (device == NULL) {
+ if ((device = (struct cam_device *)malloc(
+ sizeof(struct cam_device))) == NULL) {
+ sprintf(cam_errbuf, "%s: device structure malloc"
+ " failed\n%s: %s", func_name, func_name,
+ strerror(errno));
+ return(NULL);
+ }
+ malloced_device = 1;
+ }
+
+ /*
+ * If the user passed in a path, save it for him.
+ */
+ if (given_path != NULL)
+ strncpy(device->device_path, given_path, MAXPATHLEN + 1);
+ else
+ device->device_path[0] = '\0';
+
+ /*
+ * If the user passed in a device name and unit number pair, save
+ * those as well.
+ */
+ if (given_dev_name != NULL)
+ strncpy(device->given_dev_name, given_dev_name, DEV_IDLEN);
+ else
+ device->given_dev_name[0] = '\0';
+ device->given_unit_number = given_unit_number;
+
+ if ((fd = open(path, flags)) < 0) {
+ sprintf(cam_errbuf, "%s: couldn't open passthrough device %s\n"
+ "%s: %s", func_name, newpath, func_name,
+ strerror(errno));
+ goto crod_bailout;
+ }
+
+ device->fd = fd;
+
+ bzero(&ccb, sizeof(union ccb));
+
+ /*
+ * Unlike the transport layer version of the GETPASSTHRU ioctl,
+ * we don't have to set any fields.
+ */
+ ccb.ccb_h.func_code = XPT_GDEVLIST;
+
+ /*
+ * We're only doing this to get some information on the device in
+ * question. Otherwise, we'd have to pass in yet another
+ * parameter: the passthrough driver unit number.
+ */
+ if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
+ sprintf(cam_errbuf, "%s: CAMGETPASSTHRU ioctl failed\n"
+ "%s: %s", func_name, func_name, strerror(errno));
+ goto crod_bailout;
+ }
+
+ /*
+ * If the ioctl returned the right status, but we got an error back
+ * in the ccb, that means that the kernel found the device the user
+ * passed in, but was unable to find the passthrough device for
+ * the device the user gave us.
+ */
+ if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
+ sprintf(cam_errbuf, "%s: passthrough device does not exist??!!",
+ func_name);
+ goto crod_bailout;
+ }
+
+ device->dev_unit_num = ccb.cgdl.unit_number;
+ strcpy(device->device_name, ccb.cgdl.periph_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) {
+ sprintf(cam_errbuf, "%s: Path Inquiry CCB failed\n"
+ "%s: %s", func_name, func_name, strerror(errno));
+ goto crod_bailout;
+ }
+ strncpy(device->sim_name, ccb.cpi.dev_name, SIM_IDLEN);
+ device->sim_unit_number = ccb.cpi.unit_number;
+ device->bus_id = ccb.cpi.bus_id;
+
+ /*
+ * It doesn't really matter what is in the payload for a getdev
+ * CCB, the kernel doesn't look at it.
+ */
+ ccb.ccb_h.func_code = XPT_GDEV_TYPE;
+ if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
+ sprintf(cam_errbuf, "%s: Get Device Type CCB failed\n"
+ "%s: %s", func_name, func_name, strerror(errno));
+ goto crod_bailout;
+ }
+ device->pd_type = ccb.cgd.pd_type;
+ 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);
+
+ /*
+ * Zero the payload, the kernel does look at the flags.
+ */
+ bzero(&(&ccb.ccb_h)[1], sizeof(struct ccb_trans_settings));
+
+ /*
+ * Get transfer settings for this device.
+ */
+ ccb.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
+
+ ccb.cts.flags = CCB_TRANS_CURRENT_SETTINGS;
+
+ if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
+ sprintf(cam_errbuf, "%s: Get Transfer Settings CCB failed\n"
+ "%s: %s", func_name, func_name, strerror(errno));
+ goto crod_bailout;
+ }
+ device->sync_period = ccb.cts.sync_period;
+ device->sync_offset = ccb.cts.sync_offset;
+ device->bus_width = ccb.cts.bus_width;
+
+ return(device);
+
+crod_bailout:
+
+ if (malloced_device)
+ free(device);
+
+ return(NULL);
+}
+
+void
+cam_close_device(struct cam_device *dev)
+{
+ if (dev == NULL)
+ return;
+
+ cam_close_spec_device(dev);
+
+ if (dev != NULL)
+ free(dev);
+}
+
+void
+cam_close_spec_device(struct cam_device *dev)
+{
+ if (dev == NULL)
+ return;
+
+ if (dev->fd >= 0)
+ close(dev->fd);
+}
+
+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:%d): ",
+ (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,
+ dev->target_lun);
+
+ return(str);
+}
+
+/*
+ * Malloc/duplicate a CAM device structure.
+ */
+struct cam_device *
+cam_device_dup(struct cam_device *device)
+{
+ char *func_name = "cam_device_dup";
+ struct cam_device *newdev;
+
+ if (device == NULL) {
+ sprintf(cam_errbuf, "%s: device is NULL", func_name);
+ return(NULL);
+ }
+
+ newdev = malloc(sizeof(struct cam_device));
+
+ bcopy(device, newdev, sizeof(struct cam_device));
+
+ return(newdev);
+}
+
+/*
+ * Copy a CAM device structure.
+ */
+void
+cam_device_copy(struct cam_device *src, struct cam_device *dst)
+{
+ char *func_name = "cam_device_copy";
+
+ if (src == NULL) {
+ sprintf(cam_errbuf, "%s: source device struct was NULL",
+ func_name);
+ return;
+ }
+
+ if (dst == NULL) {
+ sprintf(cam_errbuf, "%s: destination device struct was NULL",
+ func_name);
+ return;
+ }
+
+ bcopy(src, dst, sizeof(struct cam_device));
+
+}
diff --git a/lib/libcam/camlib.c.orig b/lib/libcam/camlib.c.orig
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/libcam/camlib.c.orig
diff --git a/lib/libcam/camlib.h b/lib/libcam/camlib.h
new file mode 100644
index 0000000..9a7535c
--- /dev/null
+++ b/lib/libcam/camlib.h
@@ -0,0 +1,178 @@
+/*
+ * Copyright (c) 1997, 1998 Kenneth D. Merry.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $Id$
+ */
+/*
+ * Buffer encoding/decoding routines taken from the original FreeBSD SCSI
+ * library and slightly modified. The original header file had the following
+ * copyright:
+ */
+/* Copyright (c) 1994 HD Associates (hd@world.std.com)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by HD Associates
+ * 4. Neither the name of the HD Associaates nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+
+#ifndef _CAMLIB_H
+#define _CAMLIB_H
+
+#include <sys/cdefs.h>
+#include <sys/param.h>
+
+#include <cam/cam.h>
+#include <cam/cam_ccb.h>
+
+#define CAM_ERRBUF_SIZE 2048 /* sizeof the CAM libarary error string */
+
+/*
+ * Right now we hard code the transport layer device, but this will change
+ * if we ever get more than one transport layer.
+ */
+#define XPT_DEVICE "/dev/xpt0"
+
+
+extern char cam_errbuf[];
+
+struct cam_device {
+ char device_path[MAXPATHLEN+1];/*
+ * Pathname of the device
+ * given by the user. This
+ * may be null if the
+ * user states the device
+ * name and unit number
+ * separately.
+ */
+ char given_dev_name[DEV_IDLEN+1];/*
+ * Device name given by
+ * the user.
+ */
+ u_int32_t given_unit_number; /*
+ * Unit number given by
+ * the user.
+ */
+ char device_name[DEV_IDLEN+1];/*
+ * Name of the device,
+ * e.g. 'pass'
+ */
+ u_int32_t dev_unit_num; /* Unit number of the passthrough
+ * device associated with this
+ * particular device.
+ */
+
+ char sim_name[SIM_IDLEN+1]; /* Controller name, e.g. 'ahc' */
+ u_int32_t sim_unit_number; /* Controller unit number */
+ u_int32_t bus_id; /* Controller bus number */
+ lun_id_t target_lun; /* Logical Unit Number */
+ target_id_t target_id; /* Target ID */
+ path_id_t path_id; /* System SCSI bus number */
+ u_int16_t pd_type; /* type of peripheral device */
+ struct scsi_inquiry_data inq_data; /* SCSI Inquiry data */
+ u_int8_t serial_num[252]; /* device serial number */
+ u_int8_t serial_num_len; /* length of the serial number */
+ u_int8_t sync_period; /* Negotiated sync period */
+ u_int8_t sync_offset; /* Negotiated sync offset */
+ u_int8_t bus_width; /* Negotiated bus width */
+ int fd; /* file descriptor for device */
+};
+
+__BEGIN_DECLS
+/* Basic utility commands */
+struct cam_device * cam_open_device(const char *path, int flags);
+void cam_close_device(struct cam_device *dev);
+void cam_close_spec_device(struct cam_device *dev);
+struct cam_device * cam_open_spec_device(const char *dev_name,
+ int unit, int flags,
+ struct cam_device *device);
+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);
+struct cam_device * cam_open_pass(const char *path, int flags,
+ struct cam_device *device);
+union ccb * cam_getccb(struct cam_device *dev);
+void cam_freeccb(union ccb *ccb);
+int cam_send_ccb(struct cam_device *device, union ccb *ccb);
+char * cam_path_string(struct cam_device *dev, char *str,
+ int len);
+struct cam_device * cam_device_dup(struct cam_device *device);
+void cam_device_copy(struct cam_device *src,
+ struct cam_device *dst);
+int cam_get_device(const char *path, char *dev_name,
+ int devnamelen, int *unit);
+
+/*
+ * Buffer encoding/decoding routines, from the old SCSI library.
+ */
+int csio_decode(struct ccb_scsiio *csio, char *fmt, ...);
+int csio_decode_visit(struct ccb_scsiio *csio, char *fmt,
+ void (*arg_put)(void *, int, void *, int, char *),
+ void *puthook);
+int buff_decode(u_int8_t *buff, size_t len, char *fmt, ...);
+int buff_decode_visit(u_int8_t *buff, size_t len, char *fmt,
+ void (*arg_put)(void *, int, void *, int, char *),
+ void *puthook);
+int csio_build(struct ccb_scsiio *csio, u_int8_t *data_ptr,
+ u_int32_t dxfer_len, u_int32_t flags, int retry_count,
+ int timeout, char *cmd_spec, ...);
+int csio_build_visit(struct ccb_scsiio *csio, u_int8_t *data_ptr,
+ u_int32_t dxfer_len, u_int32_t flags, int retry_count,
+ int timeout, char *cmd_spec,
+ int (*arg_get)(void *hook, char *field_name),
+ void *gethook);
+int csio_encode(struct ccb_scsiio *csio, char *fmt, ...);
+int buff_encode_visit(u_int8_t *buff, size_t len, char *fmt,
+ int (*arg_get)(void *hook, char *field_name),
+ void *gethook);
+int csio_encode_visit(struct ccb_scsiio *csio, char *fmt,
+ int (*arg_get)(void *hook, char *field_name),
+ void *gethook);
+__END_DECLS
+
+#endif /* _CAMLIB_H */
diff --git a/lib/libcam/camlib.h.orig b/lib/libcam/camlib.h.orig
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/libcam/camlib.h.orig
diff --git a/lib/libcam/scsi_cmdparse.c b/lib/libcam/scsi_cmdparse.c
new file mode 100644
index 0000000..ce30659
--- /dev/null
+++ b/lib/libcam/scsi_cmdparse.c
@@ -0,0 +1,792 @@
+/*
+ * Taken from the original FreeBSD user SCSI library.
+ */
+/* Copyright (c) 1994 HD Associates
+ * (contact: dufault@hda.com)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by HD Associates
+ * 4. Neither the name of the HD Associaates nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * From: scsi.c,v 1.8 1997/02/22 15:07:54 peter Exp $
+ * $Id$
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <string.h>
+#include <sys/errno.h>
+#include <stdarg.h>
+#include <fcntl.h>
+
+#include <cam/cam.h>
+#include <cam/cam_ccb.h>
+#include <cam/scsi/scsi_message.h>
+#include "camlib.h"
+
+/*
+ * Decode: Decode the data section of a scsireq. This decodes
+ * trivial grammar:
+ *
+ * fields : field fields
+ * ;
+ *
+ * field : field_specifier
+ * | control
+ * ;
+ *
+ * control : 's' seek_value
+ * | 's' '+' seek_value
+ * ;
+ *
+ * seek_value : DECIMAL_NUMBER
+ * | 'v' // For indirect seek, i.e., value from the arg list
+ * ;
+ *
+ * field_specifier : type_specifier field_width
+ * | '{' NAME '}' type_specifier field_width
+ * ;
+ *
+ * field_width : DECIMAL_NUMBER
+ * ;
+ *
+ * type_specifier : 'i' // Integral types (i1, i2, i3, i4)
+ * | 'b' // Bits
+ * | 't' // Bits
+ * | 'c' // Character arrays
+ * | 'z' // Character arrays with zeroed trailing spaces
+ * ;
+ *
+ * Notes:
+ * 1. Integral types are swapped into host order.
+ * 2. Bit fields are allocated MSB to LSB to match the SCSI spec documentation.
+ * 3. 's' permits "seeking" in the string. "s+DECIMAL" seeks relative to
+ * DECIMAL; "sDECIMAL" seeks absolute to decimal.
+ * 4. 's' permits an indirect reference. "sv" or "s+v" will get the
+ * next integer value from the arg array.
+ * 5. Field names can be anything between the braces
+ *
+ * BUGS:
+ * i and b types are promoted to ints.
+ *
+ */
+
+static int
+do_buff_decode(u_int8_t *databuf, size_t len,
+ void (*arg_put)(void *, int , void *, int, char *),
+ void *puthook, char *fmt, va_list ap)
+{
+ int assigned = 0;
+ int width;
+ int suppress;
+ int plus;
+ int done = 0;
+ static u_char mask[] = {0, 0x01, 0x03, 0x07, 0x0f,
+ 0x1f, 0x3f, 0x7f, 0xff};
+ int value;
+ u_char *base = databuf;
+ char letter;
+ char field_name[80];
+
+# define ARG_PUT(ARG) \
+ do \
+ { \
+ if (!suppress) \
+ { \
+ if (arg_put) \
+ (*arg_put)(puthook, (letter == 't' ? \
+ 'b' : letter), \
+ (void *)((long)(ARG)), 1, field_name); \
+ else \
+ *(va_arg(ap, int *)) = (ARG); \
+ assigned++; \
+ } \
+ field_name[0] = 0; \
+ suppress = 0; \
+ } while (0)
+
+ u_char bits = 0; /* For bit fields */
+ int shift = 0; /* Bits already shifted out */
+ suppress = 0;
+ field_name[0] = 0;
+
+ while (!done) {
+ switch(letter = *fmt) {
+ case ' ': /* White space */
+ case '\t':
+ case '\r':
+ case '\n':
+ case '\f':
+ fmt++;
+ break;
+
+ case '#': /* Comment */
+ while (*fmt && (*fmt != '\n'))
+ fmt++;
+ if (fmt)
+ fmt++; /* Skip '\n' */
+ break;
+
+ case '*': /* Suppress assignment */
+ fmt++;
+ suppress = 1;
+ break;
+
+ case '{': /* Field Name */
+ {
+ int i = 0;
+ fmt++; /* Skip '{' */
+ while (*fmt && (*fmt != '}')) {
+ if (i < sizeof(field_name))
+ field_name[i++] = *fmt;
+
+ fmt++;
+ }
+ if (fmt)
+ fmt++; /* Skip '}' */
+ field_name[i] = 0;
+ break;
+ }
+
+ case 't': /* Bit (field) */
+ case 'b': /* Bits */
+ fmt++;
+ width = strtol(fmt, &fmt, 10);
+ if (width > 8)
+ done = 1;
+ else {
+ if (shift <= 0) {
+ bits = *databuf++;
+ shift = 8;
+ }
+ value = (bits >> (shift - width)) &
+ mask[width];
+
+#if 0
+ printf("shift %2d bits %02x value %02x width %2d mask %02x\n",
+ shift, bits, value, width, mask[width]);
+#endif
+
+ ARG_PUT(value);
+
+ shift -= width;
+ }
+ break;
+
+ case 'i': /* Integral values */
+ shift = 0;
+ fmt++;
+ width = strtol(fmt, &fmt, 10);
+ switch(width) {
+ case 1:
+ ARG_PUT(*databuf);
+ databuf++;
+ break;
+
+ case 2:
+ ARG_PUT((*databuf) << 8 | *(databuf + 1));
+ databuf += 2;
+ break;
+
+ case 3:
+ ARG_PUT((*databuf) << 16 |
+ (*(databuf + 1)) << 8 | *(databuf + 2));
+ databuf += 3;
+ break;
+
+ case 4:
+ ARG_PUT((*databuf) << 24 |
+ (*(databuf + 1)) << 16 |
+ (*(databuf + 2)) << 8 |
+ *(databuf + 3));
+ databuf += 4;
+ break;
+
+ default:
+ done = 1;
+ break;
+ }
+
+ break;
+
+ case 'c': /* Characters (i.e., not swapped) */
+ case 'z': /* Characters with zeroed trailing
+ spaces */
+ shift = 0;
+ fmt++;
+ width = strtol(fmt, &fmt, 10);
+ if (!suppress) {
+ if (arg_put)
+ (*arg_put)(puthook,
+ (letter == 't' ? 'b' : letter),
+ databuf, width, field_name);
+ else {
+ char *dest;
+ dest = va_arg(ap, char *);
+ bcopy(databuf, dest, width);
+ if (letter == 'z') {
+ char *p;
+ for (p = dest + width - 1;
+ (p >= (char *)dest)
+ && (*p == ' '); p--)
+ *p = 0;
+ }
+ }
+ assigned++;
+ }
+ databuf += width;
+ field_name[0] = 0;
+ suppress = 0;
+ break;
+
+ case 's': /* Seek */
+ shift = 0;
+ fmt++;
+ if (*fmt == '+') {
+ plus = 1;
+ fmt++;
+ } else
+ plus = 0;
+
+ if (tolower(*fmt) == 'v') {
+ /*
+ * You can't suppress a seek value. You also
+ * can't have a variable seek when you are using
+ * "arg_put".
+ */
+ width = (arg_put) ? 0 : va_arg(ap, int);
+ fmt++;
+ } else
+ width = strtol(fmt, &fmt, 10);
+
+ if (plus)
+ databuf += width; /* Relative seek */
+ else
+ databuf = base + width; /* Absolute seek */
+
+ break;
+
+ case 0:
+ done = 1;
+ break;
+
+ default:
+ fprintf(stderr, "Unknown letter in format: %c\n",
+ letter);
+ fmt++;
+ break;
+ }
+ }
+
+ return (assigned);
+}
+
+/* next_field: Return the next field in a command specifier. This
+ * builds up a SCSI command using this trivial grammar:
+ *
+ * fields : field fields
+ * ;
+ *
+ * field : value
+ * | value ':' field_width
+ * ;
+ *
+ * field_width : digit
+ * | 'i' digit // i2 = 2 byte integer, i3 = 3 byte integer etc.
+ * ;
+ *
+ * value : HEX_NUMBER
+ * | 'v' // For indirection.
+ * ;
+ *
+ * Notes:
+ * Bit fields are specified MSB first to match the SCSI spec.
+ *
+ * Examples:
+ * TUR: "0 0 0 0 0 0"
+ * WRITE BUFFER: "38 v:3 0:2 0:3 v v:i3 v:i3 0", mode, buffer_id, list_length
+ *
+ * The function returns the value:
+ * 0: For reached end, with error_p set if an error was found
+ * 1: For valid stuff setup
+ * 2: For "v" was entered as the value (implies use varargs)
+ *
+ */
+
+static int
+next_field(char **pp, char *fmt, int *width_p, int *value_p, char *name,
+ int n_name, int *error_p, int *suppress_p)
+{
+ char *p = *pp;
+
+ int something = 0;
+
+ enum {
+ BETWEEN_FIELDS,
+ START_FIELD,
+ GET_FIELD,
+ DONE,
+ } state;
+
+ int value = 0;
+ int field_size; /* Default to byte field type... */
+ int field_width; /* 1 byte wide */
+ int is_error = 0;
+ int suppress = 0;
+
+ field_size = 8; /* Default to byte field type... */
+ *fmt = 'i';
+ field_width = 1; /* 1 byte wide */
+ if (name)
+ *name = 0;
+
+ state = BETWEEN_FIELDS;
+
+ while (state != DONE) {
+ switch(state) {
+ case BETWEEN_FIELDS:
+ if (*p == 0)
+ state = DONE;
+ else if (isspace(*p))
+ p++;
+ else if (*p == '#') {
+ while (*p && *p != '\n')
+ p++;
+ if (p)
+ p++;
+ } else if (*p == '{') {
+ int i = 0;
+
+ p++;
+
+ while (*p && *p != '}') {
+ if(name && i < n_name) {
+ name[i] = *p;
+ i++;
+ }
+ p++;
+ }
+
+ if(name && i < n_name)
+ name[i] = 0;
+
+ if (*p == '}')
+ p++;
+ } else if (*p == '*') {
+ p++;
+ suppress = 1;
+ } else if (isxdigit(*p)) {
+ something = 1;
+ value = strtol(p, &p, 16);
+ state = START_FIELD;
+ } else if (tolower(*p) == 'v') {
+ p++;
+ something = 2;
+ value = *value_p;
+ state = START_FIELD;
+ } else if (tolower(*p) == 'i') {
+ /*
+ * Try to work without the "v".
+ */
+ something = 2;
+ value = *value_p;
+ p++;
+
+ *fmt = 'i';
+ field_size = 8;
+ field_width = strtol(p, &p, 10);
+ state = DONE;
+
+ } else if (tolower(*p) == 't') {
+ /*
+ * XXX: B can't work: Sees the 'b' as a
+ * hex digit in "isxdigit". try "t" for
+ * bit field.
+ */
+ something = 2;
+ value = *value_p;
+ p++;
+
+ *fmt = 'b';
+ field_size = 1;
+ field_width = strtol(p, &p, 10);
+ state = DONE;
+ } else if (tolower(*p) == 's') {
+ /* Seek */
+ *fmt = 's';
+ p++;
+ if (tolower(*p) == 'v') {
+ p++;
+ something = 2;
+ value = *value_p;
+ } else {
+ something = 1;
+ value = strtol(p, &p, 0);
+ }
+ state = DONE;
+ } else {
+ fprintf(stderr, "Invalid starting "
+ "character: %c\n", *p);
+ is_error = 1;
+ state = DONE;
+ }
+ break;
+
+ case START_FIELD:
+ if (*p == ':') {
+ p++;
+ field_size = 1; /* Default to bits
+ when specified */
+ state = GET_FIELD;
+ } else
+ state = DONE;
+ break;
+
+ case GET_FIELD:
+ if (isdigit(*p)) {
+ *fmt = 'b';
+ field_size = 1;
+ field_width = strtol(p, &p, 10);
+ state = DONE;
+ } else if (*p == 'i') {
+
+ /* Integral (bytes) */
+ p++;
+
+ *fmt = 'i';
+ field_size = 8;
+ field_width = strtol(p, &p, 10);
+ state = DONE;
+ } else if (*p == 'b') {
+
+ /* Bits */
+ p++;
+
+ *fmt = 'b';
+ field_size = 1;
+ field_width = strtol(p, &p, 10);
+ state = DONE;
+ } else {
+ fprintf(stderr, "Invalid startfield %c "
+ "(%02x)\n", *p, *p);
+ is_error = 1;
+ state = DONE;
+ }
+ break;
+
+ case DONE:
+ break;
+ }
+ }
+
+ if (is_error) {
+ *error_p = 1;
+ return 0;
+ }
+
+ *error_p = 0;
+ *pp = p;
+ *width_p = field_width * field_size;
+ *value_p = value;
+ *suppress_p = suppress;
+
+ return (something);
+}
+
+static int
+do_encode(u_char *buff, size_t vec_max, size_t *used,
+ int (*arg_get)(void *, char *), void *gethook, char *fmt, va_list ap)
+{
+ int ind;
+ int shift;
+ u_char val;
+ int ret;
+ int width, value, error, suppress;
+ char c;
+ int encoded = 0;
+ char field_name[80];
+
+ ind = 0;
+ shift = 0;
+ val = 0;
+
+ while ((ret = next_field(&fmt, &c, &width, &value, field_name,
+ sizeof(field_name), &error, &suppress))) {
+ encoded++;
+
+ if (ret == 2) {
+ if (suppress)
+ value = 0;
+ else
+ value = arg_get ?
+ (*arg_get)(gethook, field_name) :
+ va_arg(ap, int);
+ }
+
+#if 0
+ printf(
+"do_encode: ret %d fmt %c width %d value %d name \"%s\" error %d suppress %d\n",
+ ret, c, width, value, field_name, error, suppress);
+#endif
+ /* Absolute seek */
+ if (c == 's') {
+ ind = value;
+ continue;
+ }
+
+ /* A width of < 8 is a bit field. */
+ if (width < 8) {
+
+ /* This is a bit field. We start with the high bits
+ * so it reads the same as the SCSI spec.
+ */
+
+ shift += width;
+
+ val |= (value << (8 - shift));
+
+ if (shift == 8) {
+ if (ind < vec_max) {
+ buff[ind++] = val;
+ val = 0;
+ }
+ shift = 0;
+ }
+ } else {
+ if (shift) {
+ if (ind < vec_max) {
+ buff[ind++] = val;
+ val = 0;
+ }
+ shift = 0;
+ }
+ switch(width) {
+ case 8: /* 1 byte integer */
+ if (ind < vec_max)
+ buff[ind++] = value;
+ break;
+
+ case 16: /* 2 byte integer */
+ if (ind < vec_max - 2 + 1) {
+ buff[ind++] = value >> 8;
+ buff[ind++] = value;
+ }
+ break;
+
+ case 24: /* 3 byte integer */
+ if (ind < vec_max - 3 + 1) {
+ buff[ind++] = value >> 16;
+ buff[ind++] = value >> 8;
+ buff[ind++] = value;
+ }
+ break;
+
+ case 32: /* 4 byte integer */
+ if (ind < vec_max - 4 + 1) {
+ buff[ind++] = value >> 24;
+ buff[ind++] = value >> 16;
+ buff[ind++] = value >> 8;
+ buff[ind++] = value;
+ }
+ break;
+
+ default:
+ fprintf(stderr, "do_encode: Illegal width\n");
+ break;
+ }
+ }
+ }
+
+ /* Flush out any remaining bits
+ */
+ if (shift && ind < vec_max) {
+ buff[ind++] = val;
+ val = 0;
+ }
+
+
+ if (used)
+ *used = ind;
+
+ if (error)
+ return -1;
+
+ return encoded;
+}
+
+int
+csio_decode(struct ccb_scsiio *csio, char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+
+ return(do_buff_decode(csio->data_ptr, (size_t)csio->dxfer_len,
+ 0, 0, fmt, ap));
+}
+
+int
+csio_decode_visit(struct ccb_scsiio *csio, char *fmt,
+ void (*arg_put)(void *, int, void *, int, char *),
+ void *puthook)
+{
+ va_list ap;
+
+ ap = (va_list)0;
+
+ return(do_buff_decode(csio->data_ptr, (size_t)csio->dxfer_len,
+ arg_put, puthook, fmt, ap));
+}
+
+int
+buff_decode(u_int8_t *buff, size_t len, char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+
+ return(do_buff_decode(buff, len, 0, 0, fmt, ap));
+}
+
+int
+buff_decode_visit(u_int8_t *buff, size_t len, char *fmt,
+ void (*arg_put)(void *, int, void *, int, char *),
+ void *puthook)
+{
+ va_list ap;
+
+ ap = (va_list)0;
+
+ return(do_buff_decode(buff, len, arg_put, puthook, fmt, ap));
+}
+
+/*
+ * Build a SCSI CCB, given the command and data pointers and a format
+ * string describing the
+ */
+int
+csio_build(struct ccb_scsiio *csio, u_int8_t *data_ptr, u_int32_t dxfer_len,
+ u_int32_t flags, int retry_count, int timeout, char *cmd_spec, ...)
+{
+ int cmdlen;
+ int retval;
+ va_list ap;
+
+ if (csio == NULL)
+ return(0);
+
+ bzero(csio, sizeof(struct ccb_scsiio));
+
+ va_start(ap, cmd_spec);
+
+ if ((retval = do_encode(csio->cdb_io.cdb_bytes, SCSI_MAX_CDBLEN,
+ &cmdlen, NULL, NULL, cmd_spec, ap)) == -1)
+ return(retval);
+
+ cam_fill_csio(csio,
+ /* retries */ retry_count,
+ /* cbfcnp */ NULL,
+ /* flags */ flags,
+ /* tag_action */ MSG_SIMPLE_Q_TAG,
+ /* data_ptr */ data_ptr,
+ /* dxfer_len */ dxfer_len,
+ /* sense_len */ SSD_FULL_SIZE,
+ /* cdb_len */ cmdlen,
+ /* timeout */ timeout ? timeout : 5000);
+
+ return(retval);
+}
+
+int
+csio_build_visit(struct ccb_scsiio *csio, u_int8_t *data_ptr,
+ u_int32_t dxfer_len, u_int32_t flags, int retry_count,
+ int timeout, char *cmd_spec,
+ int (*arg_get)(void *hook, char *field_name), void *gethook)
+{
+ va_list ap;
+ int cmdlen, retval;
+
+ if (csio == NULL)
+ return(0);
+
+ ap = (va_list)0;
+
+ bzero(csio, sizeof(struct ccb_scsiio));
+
+ if ((retval = do_encode(csio->cdb_io.cdb_bytes, SCSI_MAX_CDBLEN,
+ &cmdlen, arg_get, gethook, cmd_spec, ap)) == -1)
+ return(retval);
+
+ cam_fill_csio(csio,
+ /* retries */ retry_count,
+ /* cbfcnp */ NULL,
+ /* flags */ flags,
+ /* tag_action */ MSG_SIMPLE_Q_TAG,
+ /* data_ptr */ data_ptr,
+ /* dxfer_len */ dxfer_len,
+ /* sense_len */ SSD_FULL_SIZE,
+ /* cdb_len */ cmdlen,
+ /* timeout */ timeout ? timeout : 5000);
+
+ return(retval);
+}
+
+int
+csio_encode(struct ccb_scsiio *csio, char *fmt, ...)
+{
+ va_list ap;
+
+ if (csio == NULL)
+ return(0);
+
+ va_start(ap, fmt);
+
+ return(do_encode(csio->data_ptr, csio->dxfer_len, 0, 0, 0, fmt, ap));
+}
+
+int
+buff_encode_visit(u_int8_t *buff, size_t len, char *fmt,
+ int (*arg_get)(void *hook, char *field_name), void *gethook)
+{
+ va_list ap;
+
+ ap = (va_list)0;
+
+ return(do_encode(buff, len, 0, arg_get, gethook, fmt, ap));
+}
+
+int
+csio_encode_visit(struct ccb_scsiio *csio, char *fmt,
+ int (*arg_get)(void *hook, char *field_name), void *gethook)
+{
+ va_list ap;
+
+ ap = (va_list) 0;
+
+ return(do_encode(csio->data_ptr, csio->dxfer_len, 0, arg_get,
+ gethook, fmt, ap));
+}
+
diff --git a/lib/libcam/scsi_cmdparse.c.orig b/lib/libcam/scsi_cmdparse.c.orig
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/libcam/scsi_cmdparse.c.orig
OpenPOWER on IntegriCloud