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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/types.h>
30 #include <sys/param.h>
42 #include <cam/scsi/scsi_all.h>
43 #include <cam/cam_ccb.h>
44 #include <cam/scsi/scsi_pass.h>
48 static const char *nonrewind_devs[] = {
52 char cam_errbuf[CAM_ERRBUF_SIZE];
54 static struct cam_device *cam_real_open_device(const char *path, int flags,
55 struct cam_device *device,
56 const char *given_path,
57 const char *given_dev_name,
58 int given_unit_number);
59 static struct cam_device *cam_lookup_pass(const char *dev_name, int unit,
60 int flags, const char *given_path,
61 struct cam_device *device);
64 * Send a ccb to a passthrough device.
67 cam_send_ccb(struct cam_device *device, union ccb *ccb)
69 return(ioctl(device->fd, CAMIOCOMMAND, ccb));
73 * Malloc a CCB, zero out the header and set its path, target and lun ids.
76 cam_getccb(struct cam_device *dev)
80 ccb = (union ccb *)malloc(sizeof(union ccb));
82 bzero(&ccb->ccb_h, sizeof(struct ccb_hdr));
83 ccb->ccb_h.path_id = dev->path_id;
84 ccb->ccb_h.target_id = dev->target_id;
85 ccb->ccb_h.target_lun = dev->target_lun;
95 cam_freeccb(union ccb *ccb)
101 * Take a device name or path passed in by the user, and attempt to figure
102 * out the device name and unit number. Some possible device name formats are:
107 * Some peripheral drivers create separate device nodes with 'n' prefix for
108 * non-rewind operations. Currently only sa(4) tape driver has this feature.
109 * We extract pure peripheral name as device name for this special case.
111 * Input parameters: device name/path, length of devname string
112 * Output: device name, unit number
113 * Return values: returns 0 for success, -1 for failure
116 cam_get_device(const char *path, char *dev_name, int devnamelen, int *unit)
118 char *tmpstr, *tmpstr2;
124 snprintf(cam_errbuf, nitems(cam_errbuf),
125 "%s: device pathname was NULL", __func__);
130 * We can be rather destructive to the path string. Make a copy of
131 * it so we don't hose the user's string.
133 newpath = (char *)strdup(path);
140 * Check to see whether we have an absolute pathname.
142 if (*tmpstr == '/') {
144 tmpstr = strrchr(tmpstr2, '/');
145 /* We know that tmpstr2 contains a '/', so strrchr can't fail */
146 assert(tmpstr != NULL && *tmpstr != '\0');
150 if (*tmpstr == '\0') {
151 snprintf(cam_errbuf, nitems(cam_errbuf),
152 "%s: no text after slash", __func__);
158 * Check to see whether the user has given us a nonrewound tape
161 if (*tmpstr == 'n' || *tmpstr == 'e') {
162 for (i = 0; i < sizeof(nonrewind_devs)/sizeof(char *); i++) {
163 int len = strlen(nonrewind_devs[i]);
164 if (strncmp(tmpstr + 1, nonrewind_devs[i], len) == 0) {
165 if (isdigit(tmpstr[len + 1])) {
174 * We should now have just a device name and unit number.
175 * That means that there must be at least 2 characters.
176 * If we only have 1, we don't have a valid device name.
178 if (strlen(tmpstr) < 2) {
179 snprintf(cam_errbuf, nitems(cam_errbuf),
180 "%s: must have both device name and unit number",
187 * If the first character of the string is a digit, then the user
188 * has probably given us all numbers. Point out the error.
190 if (isdigit(*tmpstr)) {
191 snprintf(cam_errbuf, nitems(cam_errbuf),
192 "%s: device name cannot begin with a number",
199 * At this point, if the last character of the string isn't a
200 * number, we know the user either didn't give us a device number,
201 * or he gave us a device name/number format we don't recognize.
203 if (!isdigit(tmpstr[strlen(tmpstr) - 1])) {
204 snprintf(cam_errbuf, nitems(cam_errbuf),
205 "%s: unable to find device unit number", __func__);
211 * Attempt to figure out where the device name ends and the unit
212 * number begins. As long as unit_offset is at least 1 less than
213 * the length of the string, we can still potentially have a device
214 * name at the front of the string. When we get to something that
215 * isn't a digit, we've hit the device name. Because of the check
216 * above, we know that this cannot happen when unit_offset == 1.
217 * Therefore it is okay to decrement unit_offset -- it won't cause
218 * us to go past the end of the character array.
220 for (unit_offset = 1;
221 (unit_offset < (strlen(tmpstr)))
222 && (isdigit(tmpstr[strlen(tmpstr) - unit_offset])); unit_offset++);
227 * Grab the unit number.
229 *unit = atoi(&tmpstr[strlen(tmpstr) - unit_offset]);
232 * Put a null in place of the first number of the unit number so
233 * that all we have left is the device name.
235 tmpstr[strlen(tmpstr) - unit_offset] = '\0';
237 strlcpy(dev_name, tmpstr, devnamelen);
239 /* Clean up allocated memory */
247 * Backwards compatible wrapper for the real open routine. This translates
248 * a pathname into a device name and unit number for use with the real open
252 cam_open_device(const char *path, int flags)
255 char dev_name[DEV_IDLEN + 1];
258 * cam_get_device() has already put an error message in cam_errbuf,
259 * so we don't need to.
261 if (cam_get_device(path, dev_name, sizeof(dev_name), &unit) == -1)
264 return(cam_lookup_pass(dev_name, unit, flags, path, NULL));
268 * Open the passthrough device for a given bus, target and lun, if the
269 * passthrough device exists.
272 cam_open_btl(path_id_t path_id, target_id_t target_id, lun_id_t target_lun,
273 int flags, struct cam_device *device)
276 struct periph_match_pattern *match_pat;
279 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
280 snprintf(cam_errbuf, nitems(cam_errbuf),
281 "%s: couldn't open %s\n%s: %s", __func__, XPT_DEVICE,
282 __func__, strerror(errno));
286 bzero(&ccb, sizeof(union ccb));
287 ccb.ccb_h.func_code = XPT_DEV_MATCH;
288 ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
289 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
290 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
292 /* Setup the result buffer */
293 bufsize = sizeof(struct dev_match_result);
294 ccb.cdm.match_buf_len = bufsize;
295 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
296 if (ccb.cdm.matches == NULL) {
297 snprintf(cam_errbuf, nitems(cam_errbuf),
298 "%s: couldn't malloc match buffer", __func__);
302 ccb.cdm.num_matches = 0;
304 /* Setup the pattern buffer */
305 ccb.cdm.num_patterns = 1;
306 ccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
307 ccb.cdm.patterns = (struct dev_match_pattern *)malloc(
308 sizeof(struct dev_match_pattern));
309 if (ccb.cdm.patterns == NULL) {
310 snprintf(cam_errbuf, nitems(cam_errbuf),
311 "%s: couldn't malloc pattern buffer", __func__);
312 free(ccb.cdm.matches);
313 ccb.cdm.matches = NULL;
317 ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH;
318 match_pat = &ccb.cdm.patterns[0].pattern.periph_pattern;
321 * We're looking for the passthrough device associated with this
322 * particular bus/target/lun.
324 sprintf(match_pat->periph_name, "pass");
325 match_pat->path_id = path_id;
326 match_pat->target_id = target_id;
327 match_pat->target_lun = target_lun;
328 /* Now set the flags to indicate what we're looking for. */
329 match_pat->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_TARGET |
330 PERIPH_MATCH_LUN | PERIPH_MATCH_NAME;
332 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
333 snprintf(cam_errbuf, nitems(cam_errbuf),
334 "%s: CAMIOCOMMAND ioctl failed\n"
335 "%s: %s", __func__, __func__, strerror(errno));
340 * Check for an outright error.
342 if ((ccb.ccb_h.status != CAM_REQ_CMP)
343 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
344 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
345 snprintf(cam_errbuf, nitems(cam_errbuf),
346 "%s: CAM error %#x, CDM error %d "
347 "returned from XPT_DEV_MATCH ccb", __func__,
348 ccb.ccb_h.status, ccb.cdm.status);
352 if (ccb.cdm.status == CAM_DEV_MATCH_MORE) {
353 snprintf(cam_errbuf, nitems(cam_errbuf),
354 "%s: CDM reported more than one"
355 " passthrough device at %d:%d:%jx!!\n",
356 __func__, path_id, target_id, (uintmax_t)target_lun);
360 if (ccb.cdm.num_matches == 0) {
361 snprintf(cam_errbuf, nitems(cam_errbuf),
362 "%s: no passthrough device found at"
363 " %d:%d:%jx", __func__, path_id, target_id,
364 (uintmax_t)target_lun);
368 switch(ccb.cdm.matches[0].type) {
369 case DEV_MATCH_PERIPH: {
372 struct periph_match_result *periph_result;
374 periph_result = &ccb.cdm.matches[0].result.periph_result;
375 pass_unit = periph_result->unit_number;
376 free(ccb.cdm.matches);
377 ccb.cdm.matches = NULL;
378 free(ccb.cdm.patterns);
379 ccb.cdm.patterns = NULL;
381 sprintf(dev_path, "/dev/pass%d", pass_unit);
382 return(cam_real_open_device(dev_path, flags, device, NULL,
384 break; /* NOTREACHED */
387 snprintf(cam_errbuf, nitems(cam_errbuf),
388 "%s: asked for a peripheral match, but"
389 " got a bus or device match", __func__);
391 break; /* NOTREACHED */
395 free(ccb.cdm.matches);
396 ccb.cdm.matches = NULL;
397 free(ccb.cdm.patterns);
398 ccb.cdm.patterns = NULL;
404 cam_open_spec_device(const char *dev_name, int unit, int flags,
405 struct cam_device *device)
407 return(cam_lookup_pass(dev_name, unit, flags, NULL, device));
411 cam_open_pass(const char *path, int flags, struct cam_device *device)
413 return(cam_real_open_device(path, flags, device, path, NULL, 0));
416 static struct cam_device *
417 cam_lookup_pass(const char *dev_name, int unit, int flags,
418 const char *given_path, struct cam_device *device)
425 * The flags argument above only applies to the actual passthrough
426 * device open, not our open of the given device to find the
427 * passthrough device.
429 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
430 snprintf(cam_errbuf, nitems(cam_errbuf),
431 "%s: couldn't open %s\n%s: %s", __func__, XPT_DEVICE,
432 __func__, strerror(errno));
436 /* This isn't strictly necessary for the GETPASSTHRU ioctl. */
437 ccb.ccb_h.func_code = XPT_GDEVLIST;
439 /* These two are necessary for the GETPASSTHRU ioctl to work. */
440 strlcpy(ccb.cgdl.periph_name, dev_name, sizeof(ccb.cgdl.periph_name));
441 ccb.cgdl.unit_number = unit;
444 * Attempt to get the passthrough device. This ioctl will fail if
445 * the device name is null, if the device doesn't exist, or if the
446 * passthrough driver isn't in the kernel.
448 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
452 * If we get ENOENT from the transport layer version of
453 * the CAMGETPASSTHRU ioctl, it means one of two things:
454 * either the device name/unit number passed in doesn't
455 * exist, or the passthrough driver isn't in the kernel.
457 if (errno == ENOENT) {
458 snprintf(tmpstr, sizeof(tmpstr),
459 "\n%s: either the pass driver isn't in "
460 "your kernel\n%s: or %s%d doesn't exist",
461 __func__, __func__, dev_name, unit);
463 snprintf(cam_errbuf, nitems(cam_errbuf),
464 "%s: CAMGETPASSTHRU ioctl failed\n"
465 "%s: %s%s", __func__, __func__, strerror(errno),
466 (errno == ENOENT) ? tmpstr : "");
475 * If the ioctl returned the right status, but we got an error back
476 * in the ccb, that means that the kernel found the device the user
477 * passed in, but was unable to find the passthrough device for
478 * the device the user gave us.
480 if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
481 snprintf(cam_errbuf, nitems(cam_errbuf),
482 "%s: device %s%d does not exist!",
483 __func__, dev_name, unit);
487 sprintf(dev_path, "/dev/%s%d", ccb.cgdl.periph_name,
488 ccb.cgdl.unit_number);
490 return(cam_real_open_device(dev_path, flags, device, NULL,
495 * Open a given device. The path argument isn't strictly necessary, but it
496 * is copied into the cam_device structure as a convenience to the user.
498 static struct cam_device *
499 cam_real_open_device(const char *path, int flags, struct cam_device *device,
500 const char *given_path, const char *given_dev_name,
501 int given_unit_number)
504 int fd = -1, malloced_device = 0;
507 * See if the user wants us to malloc a device for him.
509 if (device == NULL) {
510 if ((device = (struct cam_device *)malloc(
511 sizeof(struct cam_device))) == NULL) {
512 snprintf(cam_errbuf, nitems(cam_errbuf),
513 "%s: device structure malloc"
514 " failed\n%s: %s", __func__, __func__,
523 * If the user passed in a path, save it for him.
525 if (given_path != NULL)
526 strlcpy(device->device_path, given_path,
527 sizeof(device->device_path));
529 device->device_path[0] = '\0';
532 * If the user passed in a device name and unit number pair, save
535 if (given_dev_name != NULL)
536 strlcpy(device->given_dev_name, given_dev_name,
537 sizeof(device->given_dev_name));
539 device->given_dev_name[0] = '\0';
540 device->given_unit_number = given_unit_number;
542 if ((fd = open(path, flags)) < 0) {
543 snprintf(cam_errbuf, nitems(cam_errbuf),
544 "%s: couldn't open passthrough device %s\n"
545 "%s: %s", __func__, path, __func__,
552 bzero(&ccb, sizeof(union ccb));
555 * Unlike the transport layer version of the GETPASSTHRU ioctl,
556 * we don't have to set any fields.
558 ccb.ccb_h.func_code = XPT_GDEVLIST;
561 * We're only doing this to get some information on the device in
562 * question. Otherwise, we'd have to pass in yet another
563 * parameter: the passthrough driver unit number.
565 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
567 * At this point we know the passthrough device must exist
568 * because we just opened it above. The only way this
569 * ioctl can fail is if the ccb size is wrong.
571 snprintf(cam_errbuf, nitems(cam_errbuf),
572 "%s: CAMGETPASSTHRU ioctl failed\n"
573 "%s: %s", __func__, __func__, strerror(errno));
578 * If the ioctl returned the right status, but we got an error back
579 * in the ccb, that means that the kernel found the device the user
580 * passed in, but was unable to find the passthrough device for
581 * the device the user gave us.
583 if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
584 snprintf(cam_errbuf, nitems(cam_errbuf),
585 "%s: passthrough device does not exist!", __func__);
589 device->dev_unit_num = ccb.cgdl.unit_number;
590 strlcpy(device->device_name, ccb.cgdl.periph_name,
591 sizeof(device->device_name));
592 device->path_id = ccb.ccb_h.path_id;
593 device->target_id = ccb.ccb_h.target_id;
594 device->target_lun = ccb.ccb_h.target_lun;
596 ccb.ccb_h.func_code = XPT_PATH_INQ;
597 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
598 snprintf(cam_errbuf, nitems(cam_errbuf),
599 "%s: Path Inquiry CCB failed\n"
600 "%s: %s", __func__, __func__, strerror(errno));
603 strlcpy(device->sim_name, ccb.cpi.dev_name, sizeof(device->sim_name));
604 device->sim_unit_number = ccb.cpi.unit_number;
605 device->bus_id = ccb.cpi.bus_id;
608 * It doesn't really matter what is in the payload for a getdev
609 * CCB, the kernel doesn't look at it.
611 ccb.ccb_h.func_code = XPT_GDEV_TYPE;
612 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
613 snprintf(cam_errbuf, nitems(cam_errbuf),
614 "%s: Get Device Type CCB failed\n"
615 "%s: %s", __func__, __func__, strerror(errno));
618 device->pd_type = SID_TYPE(&ccb.cgd.inq_data);
619 bcopy(&ccb.cgd.inq_data, &device->inq_data,
620 sizeof(struct scsi_inquiry_data));
621 device->serial_num_len = ccb.cgd.serial_num_len;
622 bcopy(&ccb.cgd.serial_num, &device->serial_num, device->serial_num_len);
625 * Zero the payload, the kernel does look at the flags.
627 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb.cts);
630 * Get transfer settings for this device.
632 ccb.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
634 ccb.cts.type = CTS_TYPE_CURRENT_SETTINGS;
636 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
637 snprintf(cam_errbuf, nitems(cam_errbuf),
638 "%s: Get Transfer Settings CCB failed\n"
639 "%s: %s", __func__, __func__, strerror(errno));
642 if (ccb.cts.transport == XPORT_SPI) {
643 struct ccb_trans_settings_spi *spi =
644 &ccb.cts.xport_specific.spi;
645 device->sync_period = spi->sync_period;
646 device->sync_offset = spi->sync_offset;
647 device->bus_width = spi->bus_width;
649 device->sync_period = 0;
650 device->sync_offset = 0;
651 device->bus_width = 0;
668 cam_close_device(struct cam_device *dev)
673 cam_close_spec_device(dev);
679 cam_close_spec_device(struct cam_device *dev)
691 cam_path_string(struct cam_device *dev, char *str, int len)
694 snprintf(str, len, "No path");
698 snprintf(str, len, "(%s%d:%s%d:%d:%d:%jx): ",
699 (dev->device_name[0] != '\0') ? dev->device_name : "pass",
701 (dev->sim_name[0] != '\0') ? dev->sim_name : "unknown",
702 dev->sim_unit_number,
705 (uintmax_t)dev->target_lun);
711 * Malloc/duplicate a CAM device structure.
714 cam_device_dup(struct cam_device *device)
716 struct cam_device *newdev;
718 if (device == NULL) {
719 snprintf(cam_errbuf, nitems(cam_errbuf),
720 "%s: device is NULL", __func__);
724 newdev = malloc(sizeof(struct cam_device));
725 if (newdev == NULL) {
726 snprintf(cam_errbuf, nitems(cam_errbuf),
727 "%s: couldn't malloc CAM device structure", __func__);
731 bcopy(device, newdev, sizeof(struct cam_device));
737 * Copy a CAM device structure.
740 cam_device_copy(struct cam_device *src, struct cam_device *dst)
744 snprintf(cam_errbuf, nitems(cam_errbuf),
745 "%s: source device struct was NULL", __func__);
750 snprintf(cam_errbuf, nitems(cam_errbuf),
751 "%s: destination device struct was NULL", __func__);
755 bcopy(src, dst, sizeof(struct cam_device));