2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 = (union ccb *)malloc(sizeof(union ccb));
84 bzero(&ccb->ccb_h, sizeof(struct ccb_hdr));
85 ccb->ccb_h.path_id = dev->path_id;
86 ccb->ccb_h.target_id = dev->target_id;
87 ccb->ccb_h.target_lun = dev->target_lun;
97 cam_freeccb(union ccb *ccb)
103 * Take a device name or path passed in by the user, and attempt to figure
104 * out the device name and unit number. Some possible device name formats are:
109 * Some peripheral drivers create separate device nodes with 'n' prefix for
110 * non-rewind operations. Currently only sa(4) tape driver has this feature.
111 * We extract pure peripheral name as device name for this special case.
113 * Input parameters: device name/path, length of devname string
114 * Output: device name, unit number
115 * Return values: returns 0 for success, -1 for failure
118 cam_get_device(const char *path, char *dev_name, int devnamelen, int *unit)
120 char *tmpstr, *tmpstr2;
126 snprintf(cam_errbuf, nitems(cam_errbuf),
127 "%s: device pathname was NULL", __func__);
132 * We can be rather destructive to the path string. Make a copy of
133 * it so we don't hose the user's string.
135 newpath = (char *)strdup(path);
142 * Check to see whether we have an absolute pathname.
144 if (*tmpstr == '/') {
146 tmpstr = strrchr(tmpstr2, '/');
147 /* We know that tmpstr2 contains a '/', so strrchr can't fail */
148 assert(tmpstr != NULL && *tmpstr != '\0');
152 if (*tmpstr == '\0') {
153 snprintf(cam_errbuf, nitems(cam_errbuf),
154 "%s: no text after slash", __func__);
160 * Check to see whether the user has given us a nonrewound tape
163 if (*tmpstr == 'n' || *tmpstr == 'e') {
164 for (i = 0; i < sizeof(nonrewind_devs)/sizeof(char *); i++) {
165 int len = strlen(nonrewind_devs[i]);
166 if (strncmp(tmpstr + 1, nonrewind_devs[i], len) == 0) {
167 if (isdigit(tmpstr[len + 1])) {
176 * We should now have just a device name and unit number.
177 * That means that there must be at least 2 characters.
178 * If we only have 1, we don't have a valid device name.
180 if (strlen(tmpstr) < 2) {
181 snprintf(cam_errbuf, nitems(cam_errbuf),
182 "%s: must have both device name and unit number",
189 * If the first character of the string is a digit, then the user
190 * has probably given us all numbers. Point out the error.
192 if (isdigit(*tmpstr)) {
193 snprintf(cam_errbuf, nitems(cam_errbuf),
194 "%s: device name cannot begin with a number",
201 * At this point, if the last character of the string isn't a
202 * number, we know the user either didn't give us a device number,
203 * or he gave us a device name/number format we don't recognize.
205 if (!isdigit(tmpstr[strlen(tmpstr) - 1])) {
206 snprintf(cam_errbuf, nitems(cam_errbuf),
207 "%s: unable to find device unit number", __func__);
213 * Attempt to figure out where the device name ends and the unit
214 * number begins. As long as unit_offset is at least 1 less than
215 * the length of the string, we can still potentially have a device
216 * name at the front of the string. When we get to something that
217 * isn't a digit, we've hit the device name. Because of the check
218 * above, we know that this cannot happen when unit_offset == 1.
219 * Therefore it is okay to decrement unit_offset -- it won't cause
220 * us to go past the end of the character array.
222 for (unit_offset = 1;
223 (unit_offset < (strlen(tmpstr)))
224 && (isdigit(tmpstr[strlen(tmpstr) - unit_offset])); unit_offset++);
229 * Grab the unit number.
231 *unit = atoi(&tmpstr[strlen(tmpstr) - unit_offset]);
234 * Put a null in place of the first number of the unit number so
235 * that all we have left is the device name.
237 tmpstr[strlen(tmpstr) - unit_offset] = '\0';
239 strlcpy(dev_name, tmpstr, devnamelen);
241 /* Clean up allocated memory */
249 * Backwards compatible wrapper for the real open routine. This translates
250 * a pathname into a device name and unit number for use with the real open
254 cam_open_device(const char *path, int flags)
257 char dev_name[DEV_IDLEN + 1];
260 * cam_get_device() has already put an error message in cam_errbuf,
261 * so we don't need to.
263 if (cam_get_device(path, dev_name, sizeof(dev_name), &unit) == -1)
266 return(cam_lookup_pass(dev_name, unit, flags, path, NULL));
270 * Open the passthrough device for a given bus, target and lun, if the
271 * passthrough device exists.
274 cam_open_btl(path_id_t path_id, target_id_t target_id, lun_id_t target_lun,
275 int flags, struct cam_device *device)
278 struct periph_match_pattern *match_pat;
281 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
282 snprintf(cam_errbuf, nitems(cam_errbuf),
283 "%s: couldn't open %s\n%s: %s", __func__, XPT_DEVICE,
284 __func__, strerror(errno));
288 bzero(&ccb, sizeof(union ccb));
289 ccb.ccb_h.func_code = XPT_DEV_MATCH;
290 ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
291 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
292 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
294 /* Setup the result buffer */
295 bufsize = sizeof(struct dev_match_result);
296 ccb.cdm.match_buf_len = bufsize;
297 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
298 if (ccb.cdm.matches == NULL) {
299 snprintf(cam_errbuf, nitems(cam_errbuf),
300 "%s: couldn't malloc match buffer", __func__);
304 ccb.cdm.num_matches = 0;
306 /* Setup the pattern buffer */
307 ccb.cdm.num_patterns = 1;
308 ccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
309 ccb.cdm.patterns = (struct dev_match_pattern *)malloc(
310 sizeof(struct dev_match_pattern));
311 if (ccb.cdm.patterns == NULL) {
312 snprintf(cam_errbuf, nitems(cam_errbuf),
313 "%s: couldn't malloc pattern buffer", __func__);
314 free(ccb.cdm.matches);
315 ccb.cdm.matches = NULL;
319 ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH;
320 match_pat = &ccb.cdm.patterns[0].pattern.periph_pattern;
323 * We're looking for the passthrough device associated with this
324 * particular bus/target/lun.
326 sprintf(match_pat->periph_name, "pass");
327 match_pat->path_id = path_id;
328 match_pat->target_id = target_id;
329 match_pat->target_lun = target_lun;
330 /* Now set the flags to indicate what we're looking for. */
331 match_pat->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_TARGET |
332 PERIPH_MATCH_LUN | PERIPH_MATCH_NAME;
334 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
335 snprintf(cam_errbuf, nitems(cam_errbuf),
336 "%s: CAMIOCOMMAND ioctl failed\n"
337 "%s: %s", __func__, __func__, strerror(errno));
342 * Check for an outright error.
344 if ((ccb.ccb_h.status != CAM_REQ_CMP)
345 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
346 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
347 snprintf(cam_errbuf, nitems(cam_errbuf),
348 "%s: CAM error %#x, CDM error %d "
349 "returned from XPT_DEV_MATCH ccb", __func__,
350 ccb.ccb_h.status, ccb.cdm.status);
354 if (ccb.cdm.status == CAM_DEV_MATCH_MORE) {
355 snprintf(cam_errbuf, nitems(cam_errbuf),
356 "%s: CDM reported more than one"
357 " passthrough device at %d:%d:%jx!!\n",
358 __func__, path_id, target_id, (uintmax_t)target_lun);
362 if (ccb.cdm.num_matches == 0) {
363 snprintf(cam_errbuf, nitems(cam_errbuf),
364 "%s: no passthrough device found at"
365 " %d:%d:%jx", __func__, path_id, target_id,
366 (uintmax_t)target_lun);
370 switch(ccb.cdm.matches[0].type) {
371 case DEV_MATCH_PERIPH: {
374 struct periph_match_result *periph_result;
376 periph_result = &ccb.cdm.matches[0].result.periph_result;
377 pass_unit = periph_result->unit_number;
378 free(ccb.cdm.matches);
379 ccb.cdm.matches = NULL;
380 free(ccb.cdm.patterns);
381 ccb.cdm.patterns = NULL;
383 sprintf(dev_path, "/dev/pass%d", pass_unit);
384 return(cam_real_open_device(dev_path, flags, device, NULL,
386 break; /* NOTREACHED */
389 snprintf(cam_errbuf, nitems(cam_errbuf),
390 "%s: asked for a peripheral match, but"
391 " got a bus or device match", __func__);
393 break; /* NOTREACHED */
397 free(ccb.cdm.matches);
398 ccb.cdm.matches = NULL;
399 free(ccb.cdm.patterns);
400 ccb.cdm.patterns = NULL;
406 cam_open_spec_device(const char *dev_name, int unit, int flags,
407 struct cam_device *device)
409 return(cam_lookup_pass(dev_name, unit, flags, NULL, device));
413 cam_open_pass(const char *path, int flags, struct cam_device *device)
415 return(cam_real_open_device(path, flags, device, path, NULL, 0));
418 static struct cam_device *
419 cam_lookup_pass(const char *dev_name, int unit, int flags,
420 const char *given_path, struct cam_device *device)
427 * The flags argument above only applies to the actual passthrough
428 * device open, not our open of the given device to find the
429 * passthrough device.
431 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
432 snprintf(cam_errbuf, nitems(cam_errbuf),
433 "%s: couldn't open %s\n%s: %s", __func__, XPT_DEVICE,
434 __func__, strerror(errno));
438 /* This isn't strictly necessary for the GETPASSTHRU ioctl. */
439 ccb.ccb_h.func_code = XPT_GDEVLIST;
441 /* These two are necessary for the GETPASSTHRU ioctl to work. */
442 strlcpy(ccb.cgdl.periph_name, dev_name, sizeof(ccb.cgdl.periph_name));
443 ccb.cgdl.unit_number = unit;
446 * Attempt to get the passthrough device. This ioctl will fail if
447 * the device name is null, if the device doesn't exist, or if the
448 * passthrough driver isn't in the kernel.
450 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
454 * If we get ENOENT from the transport layer version of
455 * the CAMGETPASSTHRU ioctl, it means one of two things:
456 * either the device name/unit number passed in doesn't
457 * exist, or the passthrough driver isn't in the kernel.
459 if (errno == ENOENT) {
460 snprintf(tmpstr, sizeof(tmpstr),
461 "\n%s: either the pass driver isn't in "
462 "your kernel\n%s: or %s%d doesn't exist",
463 __func__, __func__, dev_name, unit);
465 snprintf(cam_errbuf, nitems(cam_errbuf),
466 "%s: CAMGETPASSTHRU ioctl failed\n"
467 "%s: %s%s", __func__, __func__, strerror(errno),
468 (errno == ENOENT) ? tmpstr : "");
477 * If the ioctl returned the right status, but we got an error back
478 * in the ccb, that means that the kernel found the device the user
479 * passed in, but was unable to find the passthrough device for
480 * the device the user gave us.
482 if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
483 snprintf(cam_errbuf, nitems(cam_errbuf),
484 "%s: device %s%d does not exist!",
485 __func__, dev_name, unit);
489 sprintf(dev_path, "/dev/%s%d", ccb.cgdl.periph_name,
490 ccb.cgdl.unit_number);
492 return(cam_real_open_device(dev_path, flags, device, NULL,
497 * Open a given device. The path argument isn't strictly necessary, but it
498 * is copied into the cam_device structure as a convenience to the user.
500 static struct cam_device *
501 cam_real_open_device(const char *path, int flags, struct cam_device *device,
502 const char *given_path, const char *given_dev_name,
503 int given_unit_number)
506 int fd = -1, malloced_device = 0;
509 * See if the user wants us to malloc a device for him.
511 if (device == NULL) {
512 if ((device = (struct cam_device *)malloc(
513 sizeof(struct cam_device))) == NULL) {
514 snprintf(cam_errbuf, nitems(cam_errbuf),
515 "%s: device structure malloc"
516 " failed\n%s: %s", __func__, __func__,
525 * If the user passed in a path, save it for him.
527 if (given_path != NULL)
528 strlcpy(device->device_path, given_path,
529 sizeof(device->device_path));
531 device->device_path[0] = '\0';
534 * If the user passed in a device name and unit number pair, save
537 if (given_dev_name != NULL)
538 strlcpy(device->given_dev_name, given_dev_name,
539 sizeof(device->given_dev_name));
541 device->given_dev_name[0] = '\0';
542 device->given_unit_number = given_unit_number;
544 if ((fd = open(path, flags)) < 0) {
545 snprintf(cam_errbuf, nitems(cam_errbuf),
546 "%s: couldn't open passthrough device %s\n"
547 "%s: %s", __func__, path, __func__,
554 bzero(&ccb, sizeof(union ccb));
557 * Unlike the transport layer version of the GETPASSTHRU ioctl,
558 * we don't have to set any fields.
560 ccb.ccb_h.func_code = XPT_GDEVLIST;
563 * We're only doing this to get some information on the device in
564 * question. Otherwise, we'd have to pass in yet another
565 * parameter: the passthrough driver unit number.
567 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
569 * At this point we know the passthrough device must exist
570 * because we just opened it above. The only way this
571 * ioctl can fail is if the ccb size is wrong.
573 snprintf(cam_errbuf, nitems(cam_errbuf),
574 "%s: CAMGETPASSTHRU ioctl failed\n"
575 "%s: %s", __func__, __func__, strerror(errno));
580 * If the ioctl returned the right status, but we got an error back
581 * in the ccb, that means that the kernel found the device the user
582 * passed in, but was unable to find the passthrough device for
583 * the device the user gave us.
585 if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
586 snprintf(cam_errbuf, nitems(cam_errbuf),
587 "%s: passthrough device does not exist!", __func__);
591 device->dev_unit_num = ccb.cgdl.unit_number;
592 strlcpy(device->device_name, ccb.cgdl.periph_name,
593 sizeof(device->device_name));
594 device->path_id = ccb.ccb_h.path_id;
595 device->target_id = ccb.ccb_h.target_id;
596 device->target_lun = ccb.ccb_h.target_lun;
598 ccb.ccb_h.func_code = XPT_PATH_INQ;
599 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
600 snprintf(cam_errbuf, nitems(cam_errbuf),
601 "%s: Path Inquiry CCB failed\n"
602 "%s: %s", __func__, __func__, strerror(errno));
605 strlcpy(device->sim_name, ccb.cpi.dev_name, sizeof(device->sim_name));
606 device->sim_unit_number = ccb.cpi.unit_number;
607 device->bus_id = ccb.cpi.bus_id;
610 * It doesn't really matter what is in the payload for a getdev
611 * CCB, the kernel doesn't look at it.
613 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
614 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
615 snprintf(cam_errbuf, nitems(cam_errbuf),
616 "%s: Get Device Type CCB failed\n"
617 "%s: %s", __func__, __func__, strerror(errno));
620 device->pd_type = SID_TYPE(&ccb.cgd.inq_data);
621 bcopy(&ccb.cgd.inq_data, &device->inq_data,
622 sizeof(struct scsi_inquiry_data));
623 device->serial_num_len = ccb.cgd.serial_num_len;
624 bcopy(&ccb.cgd.serial_num, &device->serial_num, device->serial_num_len);
627 * Zero the payload, the kernel does look at the flags.
629 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb.cts);
632 * Get transfer settings for this device.
634 ccb.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
636 ccb.cts.type = CTS_TYPE_CURRENT_SETTINGS;
638 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
639 snprintf(cam_errbuf, nitems(cam_errbuf),
640 "%s: Get Transfer Settings CCB failed\n"
641 "%s: %s", __func__, __func__, strerror(errno));
644 if (ccb.cts.transport == XPORT_SPI) {
645 struct ccb_trans_settings_spi *spi =
646 &ccb.cts.xport_specific.spi;
647 device->sync_period = spi->sync_period;
648 device->sync_offset = spi->sync_offset;
649 device->bus_width = spi->bus_width;
651 device->sync_period = 0;
652 device->sync_offset = 0;
653 device->bus_width = 0;
670 cam_close_device(struct cam_device *dev)
675 cam_close_spec_device(dev);
681 cam_close_spec_device(struct cam_device *dev)
693 cam_path_string(struct cam_device *dev, char *str, int len)
696 snprintf(str, len, "No path");
700 snprintf(str, len, "(%s%d:%s%d:%d:%d:%jx): ",
701 (dev->device_name[0] != '\0') ? dev->device_name : "pass",
703 (dev->sim_name[0] != '\0') ? dev->sim_name : "unknown",
704 dev->sim_unit_number,
707 (uintmax_t)dev->target_lun);
713 * Malloc/duplicate a CAM device structure.
716 cam_device_dup(struct cam_device *device)
718 struct cam_device *newdev;
720 if (device == NULL) {
721 snprintf(cam_errbuf, nitems(cam_errbuf),
722 "%s: device is NULL", __func__);
726 newdev = malloc(sizeof(struct cam_device));
727 if (newdev == NULL) {
728 snprintf(cam_errbuf, nitems(cam_errbuf),
729 "%s: couldn't malloc CAM device structure", __func__);
733 bcopy(device, newdev, sizeof(struct cam_device));
739 * Copy a CAM device structure.
742 cam_device_copy(struct cam_device *src, struct cam_device *dst)
746 snprintf(cam_errbuf, nitems(cam_errbuf),
747 "%s: source device struct was NULL", __func__);
752 snprintf(cam_errbuf, nitems(cam_errbuf),
753 "%s: destination device struct was NULL", __func__);
757 bcopy(src, dst, sizeof(struct cam_device));