2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1997, 1998, 1999, 2002 Kenneth D. Merry.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
32 #include <sys/param.h>
44 #include <cam/scsi/scsi_all.h>
45 #include <cam/cam_ccb.h>
46 #include <cam/scsi/scsi_pass.h>
50 static const char *nonrewind_devs[] = {
54 char cam_errbuf[CAM_ERRBUF_SIZE];
56 static struct cam_device *cam_real_open_device(const char *path, int flags,
57 struct cam_device *device,
58 const char *given_path,
59 const char *given_dev_name,
60 int given_unit_number);
61 static struct cam_device *cam_lookup_pass(const char *dev_name, int unit,
62 int flags, const char *given_path,
63 struct cam_device *device);
66 * Send a ccb to a passthrough device.
69 cam_send_ccb(struct cam_device *device, union ccb *ccb)
71 return(ioctl(device->fd, CAMIOCOMMAND, ccb));
75 * Malloc a CCB, zero out the header and set its path, target and lun ids.
78 cam_getccb(struct cam_device *dev)
82 ccb = calloc(1, sizeof(*ccb));
84 ccb->ccb_h.path_id = dev->path_id;
85 ccb->ccb_h.target_id = dev->target_id;
86 ccb->ccb_h.target_lun = dev->target_lun;
96 cam_freeccb(union ccb *ccb)
102 * Take a device name or path passed in by the user, and attempt to figure
103 * out the device name and unit number. Some possible device name formats are:
108 * Some peripheral drivers create separate device nodes with 'n' prefix for
109 * non-rewind operations. Currently only sa(4) tape driver has this feature.
110 * We extract pure peripheral name as device name for this special case.
112 * Input parameters: device name/path, length of devname string
113 * Output: device name, unit number
114 * Return values: returns 0 for success, -1 for failure
117 cam_get_device(const char *path, char *dev_name, int devnamelen, int *unit)
119 char *tmpstr, *tmpstr2;
125 snprintf(cam_errbuf, nitems(cam_errbuf),
126 "%s: device pathname was NULL", __func__);
131 * Resolve the given path to a real device path in case we are given
132 * an alias or other symbolic link. If the path cannot be resolved
133 * then try to parse it as is.
135 newpath = realpath(path, NULL);
137 newpath = strdup(path);
144 * Check to see whether we have an absolute pathname.
146 if (*tmpstr == '/') {
148 tmpstr = strrchr(tmpstr2, '/');
149 /* We know that tmpstr2 contains a '/', so strrchr can't fail */
150 assert(tmpstr != NULL && *tmpstr != '\0');
154 if (*tmpstr == '\0') {
155 snprintf(cam_errbuf, nitems(cam_errbuf),
156 "%s: no text after slash", __func__);
162 * Check to see whether the user has given us a nonrewound tape
165 if (*tmpstr == 'n' || *tmpstr == 'e') {
166 for (i = 0; i < sizeof(nonrewind_devs)/sizeof(char *); i++) {
167 int len = strlen(nonrewind_devs[i]);
168 if (strncmp(tmpstr + 1, nonrewind_devs[i], len) == 0) {
169 if (isdigit(tmpstr[len + 1])) {
178 * We should now have just a device name and unit number.
179 * That means that there must be at least 2 characters.
180 * If we only have 1, we don't have a valid device name.
182 if (strlen(tmpstr) < 2) {
183 snprintf(cam_errbuf, nitems(cam_errbuf),
184 "%s: must have both device name and unit number",
191 * If the first character of the string is a digit, then the user
192 * has probably given us all numbers. Point out the error.
194 if (isdigit(*tmpstr)) {
195 snprintf(cam_errbuf, nitems(cam_errbuf),
196 "%s: device name cannot begin with a number",
203 * At this point, if the last character of the string isn't a
204 * number, we know the user either didn't give us a device number,
205 * or he gave us a device name/number format we don't recognize.
207 if (!isdigit(tmpstr[strlen(tmpstr) - 1])) {
208 snprintf(cam_errbuf, nitems(cam_errbuf),
209 "%s: unable to find device unit number", __func__);
215 * Attempt to figure out where the device name ends and the unit
216 * number begins. As long as unit_offset is at least 1 less than
217 * the length of the string, we can still potentially have a device
218 * name at the front of the string. When we get to something that
219 * isn't a digit, we've hit the device name. Because of the check
220 * above, we know that this cannot happen when unit_offset == 1.
221 * Therefore it is okay to decrement unit_offset -- it won't cause
222 * us to go past the end of the character array.
224 for (unit_offset = 1;
225 (unit_offset < (strlen(tmpstr)))
226 && (isdigit(tmpstr[strlen(tmpstr) - unit_offset])); unit_offset++);
231 * Grab the unit number.
233 *unit = atoi(&tmpstr[strlen(tmpstr) - unit_offset]);
236 * Put a null in place of the first number of the unit number so
237 * that all we have left is the device name.
239 tmpstr[strlen(tmpstr) - unit_offset] = '\0';
241 strlcpy(dev_name, tmpstr, devnamelen);
243 /* Clean up allocated memory */
251 * Backwards compatible wrapper for the real open routine. This translates
252 * a pathname into a device name and unit number for use with the real open
256 cam_open_device(const char *path, int flags)
259 char dev_name[DEV_IDLEN + 1];
262 * cam_get_device() has already put an error message in cam_errbuf,
263 * so we don't need to.
265 if (cam_get_device(path, dev_name, sizeof(dev_name), &unit) == -1)
268 return(cam_lookup_pass(dev_name, unit, flags, path, NULL));
272 * Open the passthrough device for a given bus, target and lun, if the
273 * passthrough device exists.
276 cam_open_btl(path_id_t path_id, target_id_t target_id, lun_id_t target_lun,
277 int flags, struct cam_device *device)
280 struct periph_match_pattern *match_pat;
283 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
284 snprintf(cam_errbuf, nitems(cam_errbuf),
285 "%s: couldn't open %s\n%s: %s", __func__, XPT_DEVICE,
286 __func__, strerror(errno));
290 bzero(&ccb, sizeof(union ccb));
291 ccb.ccb_h.func_code = XPT_DEV_MATCH;
292 ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
293 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
294 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
296 /* Setup the result buffer */
297 bufsize = sizeof(struct dev_match_result);
298 ccb.cdm.match_buf_len = bufsize;
299 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
300 if (ccb.cdm.matches == NULL) {
301 snprintf(cam_errbuf, nitems(cam_errbuf),
302 "%s: couldn't malloc match buffer", __func__);
306 ccb.cdm.num_matches = 0;
308 /* Setup the pattern buffer */
309 ccb.cdm.num_patterns = 1;
310 ccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
311 ccb.cdm.patterns = (struct dev_match_pattern *)malloc(
312 sizeof(struct dev_match_pattern));
313 if (ccb.cdm.patterns == NULL) {
314 snprintf(cam_errbuf, nitems(cam_errbuf),
315 "%s: couldn't malloc pattern buffer", __func__);
316 free(ccb.cdm.matches);
317 ccb.cdm.matches = NULL;
321 ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH;
322 match_pat = &ccb.cdm.patterns[0].pattern.periph_pattern;
325 * We're looking for the passthrough device associated with this
326 * particular bus/target/lun.
328 sprintf(match_pat->periph_name, "pass");
329 match_pat->path_id = path_id;
330 match_pat->target_id = target_id;
331 match_pat->target_lun = target_lun;
332 /* Now set the flags to indicate what we're looking for. */
333 match_pat->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_TARGET |
334 PERIPH_MATCH_LUN | PERIPH_MATCH_NAME;
336 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
337 snprintf(cam_errbuf, nitems(cam_errbuf),
338 "%s: CAMIOCOMMAND ioctl failed\n"
339 "%s: %s", __func__, __func__, strerror(errno));
344 * Check for an outright error.
346 if ((ccb.ccb_h.status != CAM_REQ_CMP)
347 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
348 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
349 snprintf(cam_errbuf, nitems(cam_errbuf),
350 "%s: CAM error %#x, CDM error %d "
351 "returned from XPT_DEV_MATCH ccb", __func__,
352 ccb.ccb_h.status, ccb.cdm.status);
356 if (ccb.cdm.status == CAM_DEV_MATCH_MORE) {
357 snprintf(cam_errbuf, nitems(cam_errbuf),
358 "%s: CDM reported more than one"
359 " passthrough device at %d:%d:%jx!!\n",
360 __func__, path_id, target_id, (uintmax_t)target_lun);
364 if (ccb.cdm.num_matches == 0) {
365 snprintf(cam_errbuf, nitems(cam_errbuf),
366 "%s: no passthrough device found at"
367 " %d:%d:%jx", __func__, path_id, target_id,
368 (uintmax_t)target_lun);
372 switch(ccb.cdm.matches[0].type) {
373 case DEV_MATCH_PERIPH: {
376 struct periph_match_result *periph_result;
378 periph_result = &ccb.cdm.matches[0].result.periph_result;
379 pass_unit = periph_result->unit_number;
380 free(ccb.cdm.matches);
381 ccb.cdm.matches = NULL;
382 free(ccb.cdm.patterns);
383 ccb.cdm.patterns = NULL;
385 sprintf(dev_path, "/dev/pass%d", pass_unit);
386 return(cam_real_open_device(dev_path, flags, device, NULL,
388 break; /* NOTREACHED */
391 snprintf(cam_errbuf, nitems(cam_errbuf),
392 "%s: asked for a peripheral match, but"
393 " got a bus or device match", __func__);
395 break; /* NOTREACHED */
399 free(ccb.cdm.matches);
400 ccb.cdm.matches = NULL;
401 free(ccb.cdm.patterns);
402 ccb.cdm.patterns = NULL;
408 cam_open_spec_device(const char *dev_name, int unit, int flags,
409 struct cam_device *device)
411 return(cam_lookup_pass(dev_name, unit, flags, NULL, device));
415 cam_open_pass(const char *path, int flags, struct cam_device *device)
417 return(cam_real_open_device(path, flags, device, path, NULL, 0));
420 static struct cam_device *
421 cam_lookup_pass(const char *dev_name, int unit, int flags,
422 const char *given_path, struct cam_device *device)
429 * The flags argument above only applies to the actual passthrough
430 * device open, not our open of the given device to find the
431 * passthrough device.
433 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
434 snprintf(cam_errbuf, nitems(cam_errbuf),
435 "%s: couldn't open %s\n%s: %s", __func__, XPT_DEVICE,
436 __func__, strerror(errno));
440 /* This isn't strictly necessary for the GETPASSTHRU ioctl. */
441 ccb.ccb_h.func_code = XPT_GDEVLIST;
443 /* These two are necessary for the GETPASSTHRU ioctl to work. */
444 strlcpy(ccb.cgdl.periph_name, dev_name, sizeof(ccb.cgdl.periph_name));
445 ccb.cgdl.unit_number = unit;
448 * Attempt to get the passthrough device. This ioctl will fail if
449 * the device name is null, if the device doesn't exist, or if the
450 * passthrough driver isn't in the kernel.
452 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
456 * If we get ENOENT from the transport layer version of
457 * the CAMGETPASSTHRU ioctl, it means one of two things:
458 * either the device name/unit number passed in doesn't
459 * exist, or the passthrough driver isn't in the kernel.
461 if (errno == ENOENT) {
462 snprintf(tmpstr, sizeof(tmpstr),
463 "\n%s: either the pass driver isn't in "
464 "your kernel\n%s: or %s%d doesn't exist",
465 __func__, __func__, dev_name, unit);
467 snprintf(cam_errbuf, nitems(cam_errbuf),
468 "%s: CAMGETPASSTHRU ioctl failed\n"
469 "%s: %s%s", __func__, __func__, strerror(errno),
470 (errno == ENOENT) ? tmpstr : "");
479 * If the ioctl returned the right status, but we got an error back
480 * in the ccb, that means that the kernel found the device the user
481 * passed in, but was unable to find the passthrough device for
482 * the device the user gave us.
484 if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
485 snprintf(cam_errbuf, nitems(cam_errbuf),
486 "%s: device %s%d does not exist!",
487 __func__, dev_name, unit);
491 sprintf(dev_path, "/dev/%s%d", ccb.cgdl.periph_name,
492 ccb.cgdl.unit_number);
494 return(cam_real_open_device(dev_path, flags, device, NULL,
499 * Open a given device. The path argument isn't strictly necessary, but it
500 * is copied into the cam_device structure as a convenience to the user.
502 static struct cam_device *
503 cam_real_open_device(const char *path, int flags, struct cam_device *device,
504 const char *given_path, const char *given_dev_name,
505 int given_unit_number)
508 int fd = -1, malloced_device = 0;
511 * See if the user wants us to malloc a device for him.
513 if (device == NULL) {
514 if ((device = (struct cam_device *)malloc(
515 sizeof(struct cam_device))) == NULL) {
516 snprintf(cam_errbuf, nitems(cam_errbuf),
517 "%s: device structure malloc"
518 " failed\n%s: %s", __func__, __func__,
527 * If the user passed in a path, save it for him.
529 if (given_path != NULL)
530 strlcpy(device->device_path, given_path,
531 sizeof(device->device_path));
533 device->device_path[0] = '\0';
536 * If the user passed in a device name and unit number pair, save
539 if (given_dev_name != NULL)
540 strlcpy(device->given_dev_name, given_dev_name,
541 sizeof(device->given_dev_name));
543 device->given_dev_name[0] = '\0';
544 device->given_unit_number = given_unit_number;
546 if ((fd = open(path, flags)) < 0) {
547 snprintf(cam_errbuf, nitems(cam_errbuf),
548 "%s: couldn't open passthrough device %s\n"
549 "%s: %s", __func__, path, __func__,
556 bzero(&ccb, sizeof(union ccb));
559 * Unlike the transport layer version of the GETPASSTHRU ioctl,
560 * we don't have to set any fields.
562 ccb.ccb_h.func_code = XPT_GDEVLIST;
565 * We're only doing this to get some information on the device in
566 * question. Otherwise, we'd have to pass in yet another
567 * parameter: the passthrough driver unit number.
569 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
571 * At this point we know the passthrough device must exist
572 * because we just opened it above. The only way this
573 * ioctl can fail is if the ccb size is wrong.
575 snprintf(cam_errbuf, nitems(cam_errbuf),
576 "%s: CAMGETPASSTHRU ioctl failed\n"
577 "%s: %s", __func__, __func__, strerror(errno));
582 * If the ioctl returned the right status, but we got an error back
583 * in the ccb, that means that the kernel found the device the user
584 * passed in, but was unable to find the passthrough device for
585 * the device the user gave us.
587 if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
588 snprintf(cam_errbuf, nitems(cam_errbuf),
589 "%s: passthrough device does not exist!", __func__);
593 device->dev_unit_num = ccb.cgdl.unit_number;
594 strlcpy(device->device_name, ccb.cgdl.periph_name,
595 sizeof(device->device_name));
596 device->path_id = ccb.ccb_h.path_id;
597 device->target_id = ccb.ccb_h.target_id;
598 device->target_lun = ccb.ccb_h.target_lun;
600 ccb.ccb_h.func_code = XPT_PATH_INQ;
601 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
602 snprintf(cam_errbuf, nitems(cam_errbuf),
603 "%s: Path Inquiry CCB failed\n"
604 "%s: %s", __func__, __func__, strerror(errno));
607 strlcpy(device->sim_name, ccb.cpi.dev_name, sizeof(device->sim_name));
608 device->sim_unit_number = ccb.cpi.unit_number;
609 device->bus_id = ccb.cpi.bus_id;
612 * It doesn't really matter what is in the payload for a getdev
613 * CCB, the kernel doesn't look at it.
615 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
616 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
617 snprintf(cam_errbuf, nitems(cam_errbuf),
618 "%s: Get Device Type CCB failed\n"
619 "%s: %s", __func__, __func__, strerror(errno));
622 device->pd_type = SID_TYPE(&ccb.cgd.inq_data);
623 bcopy(&ccb.cgd.inq_data, &device->inq_data,
624 sizeof(struct scsi_inquiry_data));
625 device->serial_num_len = ccb.cgd.serial_num_len;
626 bcopy(&ccb.cgd.serial_num, &device->serial_num, device->serial_num_len);
629 * Zero the payload, the kernel does look at the flags.
631 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb.cts);
634 * Get transfer settings for this device.
636 ccb.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
638 ccb.cts.type = CTS_TYPE_CURRENT_SETTINGS;
640 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
641 snprintf(cam_errbuf, nitems(cam_errbuf),
642 "%s: Get Transfer Settings CCB failed\n"
643 "%s: %s", __func__, __func__, strerror(errno));
646 if (ccb.cts.transport == XPORT_SPI) {
647 struct ccb_trans_settings_spi *spi =
648 &ccb.cts.xport_specific.spi;
649 device->sync_period = spi->sync_period;
650 device->sync_offset = spi->sync_offset;
651 device->bus_width = spi->bus_width;
653 device->sync_period = 0;
654 device->sync_offset = 0;
655 device->bus_width = 0;
672 cam_close_device(struct cam_device *dev)
677 cam_close_spec_device(dev);
683 cam_close_spec_device(struct cam_device *dev)
695 cam_path_string(struct cam_device *dev, char *str, int len)
698 snprintf(str, len, "No path");
702 snprintf(str, len, "(%s%d:%s%d:%d:%d:%jx): ",
703 (dev->device_name[0] != '\0') ? dev->device_name : "pass",
705 (dev->sim_name[0] != '\0') ? dev->sim_name : "unknown",
706 dev->sim_unit_number,
709 (uintmax_t)dev->target_lun);
715 * Malloc/duplicate a CAM device structure.
718 cam_device_dup(struct cam_device *device)
720 struct cam_device *newdev;
722 if (device == NULL) {
723 snprintf(cam_errbuf, nitems(cam_errbuf),
724 "%s: device is NULL", __func__);
728 newdev = malloc(sizeof(struct cam_device));
729 if (newdev == NULL) {
730 snprintf(cam_errbuf, nitems(cam_errbuf),
731 "%s: couldn't malloc CAM device structure", __func__);
735 bcopy(device, newdev, sizeof(struct cam_device));
741 * Copy a CAM device structure.
744 cam_device_copy(struct cam_device *src, struct cam_device *dst)
748 snprintf(cam_errbuf, nitems(cam_errbuf),
749 "%s: source device struct was NULL", __func__);
754 snprintf(cam_errbuf, nitems(cam_errbuf),
755 "%s: destination device struct was NULL", __func__);
759 bcopy(src, dst, sizeof(struct cam_device));