2 * Copyright (c) 1997, 1998, 1999, 2002 Kenneth D. Merry.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. The name of the author may not be used to endorse or promote products
11 * derived from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * $FreeBSD: src/lib/libcam/camlib.c,v 1.8.2.2 2002/05/23 04:19:22 ken Exp $
26 * $DragonFly: src/lib/libcam/camlib.c,v 1.7 2008/02/10 00:01:01 pavalos Exp $
29 #include <sys/types.h>
30 #include <sys/param.h>
40 #include <cam/scsi/scsi_all.h>
41 #include <cam/cam_ccb.h>
42 #include <cam/scsi/scsi_pass.h>
50 struct cam_devequiv devmatchtable[] = {
55 char cam_errbuf[CAM_ERRBUF_SIZE];
57 static struct cam_device *cam_real_open_device(const char *path, int flags,
58 struct cam_device *device,
59 const char *given_path,
60 const char *given_dev_name,
61 int given_unit_number);
62 static struct cam_device *cam_lookup_pass(const char *dev_name, int unit,
63 int flags, const char *given_path,
64 struct cam_device *device);
67 * Send a ccb to a passthrough device.
70 cam_send_ccb(struct cam_device *device, union ccb *ccb)
72 return(ioctl(device->fd, CAMIOCOMMAND, ccb));
76 * Malloc a CCB, zero out the header and set its path, target and lun ids.
79 cam_getccb(struct cam_device *dev)
83 ccb = (union ccb *)malloc(sizeof(union ccb));
85 bzero(&ccb->ccb_h, sizeof(struct ccb_hdr));
86 ccb->ccb_h.path_id = dev->path_id;
87 ccb->ccb_h.target_id = dev->target_id;
88 ccb->ccb_h.target_lun = dev->target_lun;
98 cam_freeccb(union ccb *ccb)
104 * Take a device name or path passed in by the user, and attempt to figure
105 * out the device name and unit number. Some possible device name formats are:
115 * If the caller passes in an old style device name like 'sd' or 'st',
116 * it will be converted to the new style device name based upon devmatchtable
119 * Input parameters: device name/path, length of devname string
120 * Output: device name, unit number
121 * Return values: returns 0 for success, -1 for failure
124 cam_get_device(const char *path, char *dev_name, int devnamelen, int *unit)
126 char *func_name = "cam_get_device";
127 char *tmpstr, *tmpstr2;
134 sprintf(cam_errbuf, "%s: device pathname was NULL", func_name);
139 * We can be rather destructive to the path string. Make a copy of
140 * it so we don't hose the user's string.
142 newpath = (char *)strdup(path);
145 /* Get rid of any leading white space */
146 while (isspace(*tmpstr) && (*tmpstr != '\0'))
150 * Check to see whether we have an absolute pathname.
152 if (*tmpstr == '/') {
154 tmpstr = (char *)rindex(tmpstr2, '/');
155 if ((tmpstr != NULL) && (*tmpstr != '\0'))
159 if (*tmpstr == '\0') {
160 sprintf(cam_errbuf, "%s: no text after slash", func_name);
166 * Check to see whether the user has given us a nonrewound tape
172 if (*tmpstr == '\0') {
173 sprintf(cam_errbuf, "%s: no text after leading 'n'", func_name);
179 * See if the user has given us a character device.
184 if (*tmpstr == '\0') {
185 sprintf(cam_errbuf, "%s: no text after leading 'r'", func_name);
191 * Try to get rid of any trailing white space or partition letters.
193 tmpstr2 = &tmpstr[strlen(tmpstr) - 1];
195 while ((*tmpstr2 != '\0') && (tmpstr2 > tmpstr) &&(!isdigit(*tmpstr2))){
201 * Check to see whether we have been given a partition with a slice
202 * name. If so, get rid of the slice name/number.
204 if (strlen(tmpstr) > 3) {
206 * Basically, we're looking for a string that ends in the
207 * following general manner: 1s1 -- a number, the letter
208 * s, and then another number. This indicates that the
209 * user has given us a slice. We substitute nulls for the
210 * s and the slice number.
212 if ((isdigit(tmpstr[strlen(tmpstr) - 1]))
213 && (tmpstr[strlen(tmpstr) - 2] == 's')
214 && (isdigit(tmpstr[strlen(tmpstr) - 3]))) {
215 tmpstr[strlen(tmpstr) - 1] = '\0';
216 tmpstr[strlen(tmpstr) - 1] = '\0';
221 * After we nuke off the slice, we should have just a device name
222 * and unit number. That means there must be at least 2
223 * characters. If we only have 1, we don't have a valid device name.
225 if (strlen(tmpstr) < 2) {
227 "%s: must have both device name and unit number",
234 * If the first character of the string is a digit, then the user
235 * has probably given us all numbers. Point out the error.
237 if (isdigit(*tmpstr)) {
239 "%s: device name cannot begin with a number",
246 * At this point, if the last character of the string isn't a
247 * number, we know the user either didn't give us a device number,
248 * or he gave us a device name/number format we don't recognize.
250 if (!isdigit(tmpstr[strlen(tmpstr) - 1])) {
251 sprintf(cam_errbuf, "%s: unable to find device unit number",
258 * Attempt to figure out where the device name ends and the unit
259 * number begins. As long as unit_offset is at least 1 less than
260 * the length of the string, we can still potentially have a device
261 * name at the front of the string. When we get to something that
262 * isn't a digit, we've hit the device name. Because of the check
263 * above, we know that this cannot happen when unit_offset == 1.
264 * Therefore it is okay to decrement unit_offset -- it won't cause
265 * us to go past the end of the character array.
267 for (unit_offset = 1;
268 (unit_offset < (strlen(tmpstr)))
269 && (isdigit(tmpstr[strlen(tmpstr) - unit_offset])); unit_offset++);
274 * Grab the unit number.
276 *unit = atoi(&tmpstr[strlen(tmpstr) - unit_offset]);
279 * Put a null in place of the first number of the unit number so
280 * that all we have left is the device name.
282 tmpstr[strlen(tmpstr) - unit_offset] = '\0';
285 * Look through our equivalency table and see if the device name
286 * the user gave us is an old style device name. If so, translate
287 * it to the new style device name.
289 for (i = 0;i < (sizeof(devmatchtable)/sizeof(struct cam_devequiv));i++){
290 if (strcmp(tmpstr, devmatchtable[i].given_dev) == 0) {
291 strlcpy(dev_name,devmatchtable[i].real_dev, devnamelen);
297 strlcpy(dev_name, tmpstr, devnamelen);
299 /* Clean up allocated memory */
307 * Backwards compatible wrapper for the real open routine. This translates
308 * a pathname into a device name and unit number for use with the real open
312 cam_open_device(const char *path, int flags)
315 char dev_name[DEV_IDLEN + 1];
318 * cam_get_device() has already put an error message in cam_errbuf,
319 * so we don't need to.
321 if (cam_get_device(path, dev_name, sizeof(dev_name), &unit) == -1)
324 return(cam_lookup_pass(dev_name, unit, flags, path, NULL));
328 * Open the passthrough device for a given bus, target and lun, if the
329 * passthrough device exists.
332 cam_open_btl(path_id_t path_id, target_id_t target_id, lun_id_t target_lun,
333 int flags, struct cam_device *device)
336 struct periph_match_pattern *match_pat;
337 char *func_name = "cam_open_btl";
340 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
341 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
342 "%s: couldn't open %s\n%s: %s", func_name, XPT_DEVICE,
343 func_name, strerror(errno));
347 bzero(&ccb, sizeof(union ccb));
348 ccb.ccb_h.func_code = XPT_DEV_MATCH;
350 /* Setup the result buffer */
351 bufsize = sizeof(struct dev_match_result);
352 ccb.cdm.match_buf_len = bufsize;
353 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
354 if (ccb.cdm.matches == NULL) {
355 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
356 "%s: couldn't malloc match buffer", func_name);
360 ccb.cdm.num_matches = 0;
362 /* Setup the pattern buffer */
363 ccb.cdm.num_patterns = 1;
364 ccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
365 ccb.cdm.patterns = (struct dev_match_pattern *)malloc(
366 sizeof(struct dev_match_pattern));
367 if (ccb.cdm.patterns == NULL) {
368 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
369 "%s: couldn't malloc pattern buffer", func_name);
370 free(ccb.cdm.matches);
374 ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH;
375 match_pat = &ccb.cdm.patterns[0].pattern.periph_pattern;
378 * We're looking for the passthrough device associated with this
379 * particular bus/target/lun.
381 sprintf(match_pat->periph_name, "pass");
382 match_pat->path_id = path_id;
383 match_pat->target_id = target_id;
384 match_pat->target_lun = target_lun;
385 /* Now set the flags to indicate what we're looking for. */
386 match_pat->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_TARGET |
387 PERIPH_MATCH_LUN | PERIPH_MATCH_NAME;
389 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
390 sprintf(cam_errbuf, "%s: CAMIOCOMMAND ioctl failed\n"
391 "%s: %s", func_name, func_name, strerror(errno));
396 * Check for an outright error.
398 if ((ccb.ccb_h.status != CAM_REQ_CMP)
399 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
400 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
401 sprintf(cam_errbuf, "%s: CAM error %#x, CDM error %d "
402 "returned from XPT_DEV_MATCH ccb", func_name,
403 ccb.ccb_h.status, ccb.cdm.status);
407 if (ccb.cdm.status == CAM_DEV_MATCH_MORE) {
408 sprintf(cam_errbuf, "%s: CDM reported more than one"
409 " passthrough device at %d:%d:%d!!\n",
410 func_name, path_id, target_id, target_lun);
414 if (ccb.cdm.num_matches == 0) {
415 sprintf(cam_errbuf, "%s: no passthrough device found at"
416 " %d:%d:%d", func_name, path_id, target_id,
421 switch(ccb.cdm.matches[0].type) {
422 case DEV_MATCH_PERIPH: {
425 struct periph_match_result *periph_result;
427 periph_result = &ccb.cdm.matches[0].result.periph_result;
428 pass_unit = periph_result->unit_number;
429 free(ccb.cdm.matches);
430 free(ccb.cdm.patterns);
432 sprintf(dev_path, "/dev/pass%d", pass_unit);
433 return(cam_real_open_device(dev_path, flags, device, NULL,
435 break; /* NOTREACHED */
438 sprintf(cam_errbuf, "%s: asked for a peripheral match, but"
439 " got a bus or device match", func_name);
441 break; /* NOTREACHED */
445 free(ccb.cdm.matches);
446 free(ccb.cdm.patterns);
452 cam_open_spec_device(const char *dev_name, int unit, int flags,
453 struct cam_device *device)
455 return(cam_lookup_pass(dev_name, unit, flags, NULL, device));
459 cam_open_pass(const char *path, int flags, struct cam_device *device)
461 return(cam_real_open_device(path, flags, device, path, NULL, 0));
464 static struct cam_device *
465 cam_lookup_pass(const char *dev_name, int unit, int flags,
466 const char *given_path, struct cam_device *device)
471 char *func_name = "cam_lookup_pass";
474 * The flags argument above only applies to the actual passthrough
475 * device open, not our open of the given device to find the
476 * passthrough device.
478 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
479 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
480 "%s: couldn't open %s\n%s: %s", func_name, XPT_DEVICE,
481 func_name, strerror(errno));
485 /* This isn't strictly necessary for the GETPASSTHRU ioctl. */
486 ccb.ccb_h.func_code = XPT_GDEVLIST;
488 /* These two are necessary for the GETPASSTHRU ioctl to work. */
489 strlcpy(ccb.cgdl.periph_name, dev_name, sizeof(ccb.cgdl.periph_name));
490 ccb.cgdl.unit_number = unit;
493 * Attempt to get the passthrough device. This ioctl will fail if
494 * the device name is null, if the device doesn't exist, or if the
495 * passthrough driver isn't in the kernel.
497 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
501 * If we get ENOENT from the transport layer version of
502 * the CAMGETPASSTHRU ioctl, it means one of two things:
503 * either the device name/unit number passed in doesn't
504 * exist, or the passthrough driver isn't in the kernel.
506 if (errno == ENOENT) {
507 snprintf(tmpstr, sizeof(tmpstr),
508 "\n%s: either the pass driver isn't in "
509 "your kernel\n%s: or %s%d doesn't exist",
510 func_name, func_name, dev_name, unit);
512 snprintf(cam_errbuf, sizeof(cam_errbuf),
513 "%s: CAMGETPASSTHRU ioctl failed\n"
514 "%s: %s%s", func_name, func_name, strerror(errno),
515 (errno == ENOENT) ? tmpstr : "");
524 * If the ioctl returned the right status, but we got an error back
525 * in the ccb, that means that the kernel found the device the user
526 * passed in, but was unable to find the passthrough device for
527 * the device the user gave us.
529 if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
530 sprintf(cam_errbuf, "%s: device %s%d does not exist!",
531 func_name, dev_name, unit);
535 sprintf(dev_path, "/dev/%s%d", ccb.cgdl.periph_name,
536 ccb.cgdl.unit_number);
538 return(cam_real_open_device(dev_path, flags, device, NULL,
543 * Open a given device. The path argument isn't strictly necessary, but it
544 * is copied into the cam_device structure as a convenience to the user.
546 static struct cam_device *
547 cam_real_open_device(const char *path, int flags, struct cam_device *device,
548 const char *given_path, const char *given_dev_name,
549 int given_unit_number)
551 char *func_name = "cam_real_open_device";
553 int fd = -1, malloced_device = 0;
556 * See if the user wants us to malloc a device for him.
558 if (device == NULL) {
559 if ((device = (struct cam_device *)malloc(
560 sizeof(struct cam_device))) == NULL) {
561 sprintf(cam_errbuf, "%s: device structure malloc"
562 " failed\n%s: %s", func_name, func_name,
571 * If the user passed in a path, save it for him.
573 if (given_path != NULL)
574 strlcpy(device->device_path, given_path,
575 sizeof(device->device_path));
577 device->device_path[0] = '\0';
580 * If the user passed in a device name and unit number pair, save
583 if (given_dev_name != NULL)
584 strlcpy(device->given_dev_name, given_dev_name,
585 sizeof(device->given_dev_name));
587 device->given_dev_name[0] = '\0';
588 device->given_unit_number = given_unit_number;
590 if ((fd = open(path, flags)) < 0) {
591 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
592 "%s: couldn't open passthrough device %s\n"
593 "%s: %s", func_name, path, func_name,
600 bzero(&ccb, sizeof(union ccb));
603 * Unlike the transport layer version of the GETPASSTHRU ioctl,
604 * we don't have to set any fields.
606 ccb.ccb_h.func_code = XPT_GDEVLIST;
609 * We're only doing this to get some information on the device in
610 * question. Otherwise, we'd have to pass in yet another
611 * parameter: the passthrough driver unit number.
613 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
615 * At this point we know the passthrough device must exist
616 * because we just opened it above. The only way this
617 * ioctl can fail is if the ccb size is wrong.
619 sprintf(cam_errbuf, "%s: CAMGETPASSTHRU ioctl failed\n"
620 "%s: %s", func_name, func_name, strerror(errno));
625 * If the ioctl returned the right status, but we got an error back
626 * in the ccb, that means that the kernel found the device the user
627 * passed in, but was unable to find the passthrough device for
628 * the device the user gave us.
630 if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
631 sprintf(cam_errbuf, "%s: passthrough device does not exist!",
636 device->dev_unit_num = ccb.cgdl.unit_number;
637 strlcpy(device->device_name, ccb.cgdl.periph_name,
638 sizeof(device->device_name));
639 device->path_id = ccb.ccb_h.path_id;
640 device->target_id = ccb.ccb_h.target_id;
641 device->target_lun = ccb.ccb_h.target_lun;
643 ccb.ccb_h.func_code = XPT_PATH_INQ;
644 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
645 sprintf(cam_errbuf, "%s: Path Inquiry CCB failed\n"
646 "%s: %s", func_name, func_name, strerror(errno));
649 strlcpy(device->sim_name, ccb.cpi.dev_name, sizeof(device->sim_name));
650 device->sim_unit_number = ccb.cpi.unit_number;
651 device->bus_id = ccb.cpi.bus_id;
654 * It doesn't really matter what is in the payload for a getdev
655 * CCB, the kernel doesn't look at it.
657 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
658 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
659 sprintf(cam_errbuf, "%s: Get Device Type CCB failed\n"
660 "%s: %s", func_name, func_name, strerror(errno));
663 device->pd_type = SID_TYPE(&ccb.cgd.inq_data);
664 bcopy(&ccb.cgd.inq_data, &device->inq_data,
665 sizeof(struct scsi_inquiry_data));
666 device->serial_num_len = ccb.cgd.serial_num_len;
667 bcopy(&ccb.cgd.serial_num, &device->serial_num, device->serial_num_len);
670 * Zero the payload, the kernel does look at the flags.
672 bzero(&(&ccb.ccb_h)[1], sizeof(struct ccb_trans_settings));
675 * Get transfer settings for this device.
677 ccb.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
679 ccb.cts.type = CTS_TYPE_CURRENT_SETTINGS;
681 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
682 sprintf(cam_errbuf, "%s: Get Transfer Settings CCB failed\n"
683 "%s: %s", func_name, func_name, strerror(errno));
686 if (ccb.cts.protocol == XPORT_SPI) {
687 struct ccb_trans_settings_spi *spi =
688 &ccb.cts.xport_specific.spi;
689 device->sync_period = spi->sync_period;
690 device->sync_offset = spi->sync_offset;
691 device->bus_width = spi->bus_width;
693 device->sync_period = 0;
694 device->sync_offset = 0;
695 device->bus_width = 0;
712 cam_close_device(struct cam_device *dev)
717 cam_close_spec_device(dev);
723 cam_close_spec_device(struct cam_device *dev)
733 cam_path_string(struct cam_device *dev, char *str, int len)
736 snprintf(str, len, "No path");
740 snprintf(str, len, "(%s%d:%s%d:%d:%d:%d): ",
741 (dev->device_name[0] != '\0') ? dev->device_name : "pass",
743 (dev->sim_name[0] != '\0') ? dev->sim_name : "unknown",
744 dev->sim_unit_number,
753 * Malloc/duplicate a CAM device structure.
756 cam_device_dup(struct cam_device *device)
758 char *func_name = "cam_device_dup";
759 struct cam_device *newdev;
761 if (device == NULL) {
762 sprintf(cam_errbuf, "%s: device is NULL", func_name);
766 newdev = malloc(sizeof(struct cam_device));
767 if (newdev == NULL) {
768 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
769 "%s: couldn't malloc CAM device structure", func_name);
773 bcopy(device, newdev, sizeof(struct cam_device));
779 * Copy a CAM device structure.
782 cam_device_copy(struct cam_device *src, struct cam_device *dst)
784 char *func_name = "cam_device_copy";
787 sprintf(cam_errbuf, "%s: source device struct was NULL",
793 sprintf(cam_errbuf, "%s: destination device struct was NULL",
798 bcopy(src, dst, sizeof(struct cam_device));