xref: /dragonfly/lib/libcam/camlib.c (revision 0720b42f)
1 /*
2  * Copyright (c) 1997, 1998, 1999, 2002 Kenneth D. Merry.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD: src/lib/libcam/camlib.c,v 1.8.2.2 2002/05/23 04:19:22 ken Exp $
26  */
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <ctype.h>
37 
38 #include <cam/cam.h>
39 #include <cam/scsi/scsi_all.h>
40 #include <cam/cam_ccb.h>
41 #include <cam/scsi/scsi_pass.h>
42 #include "camlib.h"
43 
44 struct cam_devequiv {
45 	char *given_dev;
46 	char *real_dev;
47 };
48 
49 struct cam_devequiv devmatchtable[] = {
50 	{"sd", "da"},
51 	{"st", "sa"}
52 };
53 
54 char cam_errbuf[CAM_ERRBUF_SIZE];
55 
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);
64 
65 /*
66  * Send a ccb to a passthrough device.
67  */
68 int
69 cam_send_ccb(struct cam_device *device, union ccb *ccb)
70 {
71 	return(ioctl(device->fd, CAMIOCOMMAND, ccb));
72 }
73 
74 /*
75  * Malloc a CCB, zero out the header and set its path, target and lun ids.
76  */
77 union ccb *
78 cam_getccb(struct cam_device *dev)
79 {
80 	union ccb *ccb;
81 
82 	ccb = (union ccb *)malloc(sizeof(union ccb));
83 	if (ccb != NULL) {
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;
88 	}
89 
90 	return(ccb);
91 }
92 
93 /*
94  * Free a CCB.
95  */
96 void
97 cam_freeccb(union ccb *ccb)
98 {
99 	free(ccb);
100 }
101 
102 /*
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:
105  * /dev/foo0a
106  * /dev/rfoo0a
107  * /dev/rfoos2c
108  * foo0
109  * foo0a
110  * rfoo0
111  * rfoo0a
112  * nrfoo0
113  *
114  * If the caller passes in an old style device name like 'sd' or 'st',
115  * it will be converted to the new style device name based upon devmatchtable
116  * above.
117  *
118  * Input parameters:  device name/path, length of devname string
119  * Output:            device name, unit number
120  * Return values:     returns 0 for success, -1 for failure
121  */
122 int
123 cam_get_device(const char *path, char *dev_name, int devnamelen, int *unit)
124 {
125 	char *func_name = "cam_get_device";
126 	char *tmpstr, *tmpstr2;
127 	char *newpath;
128 	int unit_offset;
129 	int i, found = 0;
130 
131 
132 	if (path == NULL) {
133 		sprintf(cam_errbuf, "%s: device pathname was NULL", func_name);
134 		return(-1);
135 	}
136 
137 	/*
138 	 * We can be rather destructive to the path string.  Make a copy of
139 	 * it so we don't hose the user's string.
140 	 */
141 	newpath = (char *)strdup(path);
142 	tmpstr = newpath;
143 
144 	/* Get rid of any leading white space */
145 	while (isspace(*tmpstr) && (*tmpstr != '\0'))
146 		tmpstr++;
147 
148 	/*
149 	 * Check to see whether we have an absolute pathname.
150 	 */
151 	if (*tmpstr == '/') {
152 		tmpstr2 = tmpstr;
153 		tmpstr = (char *)rindex(tmpstr2, '/');
154 		if ((tmpstr != NULL) && (*tmpstr != '\0'))
155 			tmpstr++;
156 	}
157 
158 	if (*tmpstr == '\0') {
159 		sprintf(cam_errbuf, "%s: no text after slash", func_name);
160 		free(newpath);
161 		return(-1);
162 	}
163 
164 	/*
165 	 * Check to see whether the user has given us a nonrewound tape
166 	 * device.
167 	 */
168 	if (*tmpstr == 'n')
169 		tmpstr++;
170 
171 	if (*tmpstr == '\0') {
172 		sprintf(cam_errbuf, "%s: no text after leading 'n'", func_name);
173 		free(newpath);
174 		return(-1);
175 	}
176 
177 	/*
178 	 * See if the user has given us a character device.
179 	 */
180 	if (*tmpstr == 'r')
181 		tmpstr++;
182 
183 	if (*tmpstr == '\0') {
184 		sprintf(cam_errbuf, "%s: no text after leading 'r'", func_name);
185 		free(newpath);
186 		return(-1);
187 	}
188 
189 	/*
190 	 * Try to get rid of any trailing white space or partition letters.
191 	 */
192 	tmpstr2 = &tmpstr[strlen(tmpstr) - 1];
193 
194 	while ((*tmpstr2 != '\0') && (tmpstr2 > tmpstr) &&(!isdigit(*tmpstr2))){
195 		*tmpstr2 = '\0';
196 		tmpstr2--;
197 	}
198 
199 	/*
200 	 * Check to see whether we have been given a partition with a slice
201 	 * name.  If so, get rid of the slice name/number.
202 	 */
203 	if (strlen(tmpstr) > 3) {
204 		/*
205 		 * Basically, we're looking for a string that ends in the
206 		 * following general manner:  1s1 -- a number, the letter
207 		 * s, and then another number.  This indicates that the
208 		 * user has given us a slice.  We substitute nulls for the
209 		 * s and the slice number.
210 		 */
211 		if ((isdigit(tmpstr[strlen(tmpstr) - 1]))
212 		 && (tmpstr[strlen(tmpstr) - 2] == 's')
213 		 && (isdigit(tmpstr[strlen(tmpstr) - 3]))) {
214 			tmpstr[strlen(tmpstr) - 1] = '\0';
215 			tmpstr[strlen(tmpstr) - 1] = '\0';
216 		}
217 	}
218 
219 	/*
220 	 * After we nuke off the slice, we should have just a device name
221 	 * and unit number.  That means there must be at least 2
222 	 * characters.  If we only have 1, we don't have a valid device name.
223 	 */
224 	if (strlen(tmpstr) < 2) {
225 		sprintf(cam_errbuf,
226 			"%s: must have both device name and unit number",
227 			func_name);
228 		free(newpath);
229 		return(-1);
230 	}
231 
232 	/*
233 	 * If the first character of the string is a digit, then the user
234 	 * has probably given us all numbers.  Point out the error.
235 	 */
236 	if (isdigit(*tmpstr)) {
237 		sprintf(cam_errbuf,
238 			"%s: device name cannot begin with a number",
239 			func_name);
240 		free(newpath);
241 		return(-1);
242 	}
243 
244 	/*
245 	 * At this point, if the last character of the string isn't a
246 	 * number, we know the user either didn't give us a device number,
247 	 * or he gave us a device name/number format we don't recognize.
248 	 */
249 	if (!isdigit(tmpstr[strlen(tmpstr) - 1])) {
250 		sprintf(cam_errbuf, "%s: unable to find device unit number",
251 			func_name);
252 		free(newpath);
253 		return(-1);
254 	}
255 
256 	/*
257 	 * Attempt to figure out where the device name ends and the unit
258 	 * number begins.  As long as unit_offset is at least 1 less than
259 	 * the length of the string, we can still potentially have a device
260 	 * name at the front of the string.  When we get to something that
261 	 * isn't a digit, we've hit the device name.  Because of the check
262 	 * above, we know that this cannot happen when unit_offset == 1.
263 	 * Therefore it is okay to decrement unit_offset -- it won't cause
264 	 * us to go past the end of the character array.
265 	 */
266 	for (unit_offset = 1;
267 	    (unit_offset < (strlen(tmpstr)))
268 	    && (isdigit(tmpstr[strlen(tmpstr) - unit_offset])); unit_offset++);
269 
270 	unit_offset--;
271 
272 	/*
273 	 * Grab the unit number.
274 	 */
275 	*unit = atoi(&tmpstr[strlen(tmpstr) - unit_offset]);
276 
277 	/*
278 	 * Put a null in place of the first number of the unit number so
279 	 * that all we have left is the device name.
280 	 */
281 	tmpstr[strlen(tmpstr) - unit_offset] = '\0';
282 
283 	/*
284 	 * Look through our equivalency table and see if the device name
285 	 * the user gave us is an old style device name.  If so, translate
286 	 * it to the new style device name.
287 	 */
288 	for (i = 0;i < (sizeof(devmatchtable)/sizeof(struct cam_devequiv));i++){
289 		if (strcmp(tmpstr, devmatchtable[i].given_dev) == 0) {
290 			strlcpy(dev_name,devmatchtable[i].real_dev, devnamelen);
291 			found = 1;
292 			break;
293 		}
294 	}
295 	if (found == 0)
296 		strlcpy(dev_name, tmpstr, devnamelen);
297 
298 	/* Clean up allocated memory */
299 	free(newpath);
300 
301 	return(0);
302 
303 }
304 
305 /*
306  * Backwards compatible wrapper for the real open routine.  This translates
307  * a pathname into a device name and unit number for use with the real open
308  * routine.
309  */
310 struct cam_device *
311 cam_open_device(const char *path, int flags)
312 {
313 	int unit;
314 	char dev_name[DEV_IDLEN + 1];
315 
316 	/*
317 	 * cam_get_device() has already put an error message in cam_errbuf,
318 	 * so we don't need to.
319 	 */
320 	if (cam_get_device(path, dev_name, sizeof(dev_name), &unit) == -1)
321 		return(NULL);
322 
323 	return(cam_lookup_pass(dev_name, unit, flags, path, NULL));
324 }
325 
326 /*
327  * Open the passthrough device for a given bus, target and lun, if the
328  * passthrough device exists.
329  */
330 struct cam_device *
331 cam_open_btl(path_id_t path_id, target_id_t target_id, lun_id_t target_lun,
332 	     int flags, struct cam_device *device)
333 {
334 	union ccb ccb;
335 	struct periph_match_pattern *match_pat;
336 	char *func_name = "cam_open_btl";
337 	int fd, bufsize;
338 
339 	if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
340 		snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
341 			 "%s: couldn't open %s\n%s: %s", func_name, XPT_DEVICE,
342 			 func_name, strerror(errno));
343 		return(NULL);
344 	}
345 
346 	bzero(&ccb, sizeof(union ccb));
347 	ccb.ccb_h.func_code = XPT_DEV_MATCH;
348 
349 	/* Setup the result buffer */
350 	bufsize = sizeof(struct dev_match_result);
351 	ccb.cdm.match_buf_len = bufsize;
352 	ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
353 	if (ccb.cdm.matches == NULL) {
354 		snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
355 			 "%s: couldn't malloc match buffer", func_name);
356 		close(fd);
357 		return(NULL);
358 	}
359 	ccb.cdm.num_matches = 0;
360 
361 	/* Setup the pattern buffer */
362 	ccb.cdm.num_patterns = 1;
363 	ccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
364 	ccb.cdm.patterns = (struct dev_match_pattern *)malloc(
365 		sizeof(struct dev_match_pattern));
366 	if (ccb.cdm.patterns == NULL) {
367 		snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
368 			 "%s: couldn't malloc pattern buffer", func_name);
369 		free(ccb.cdm.matches);
370 		close(fd);
371 		return(NULL);
372 	}
373 	ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH;
374 	match_pat = &ccb.cdm.patterns[0].pattern.periph_pattern;
375 
376 	/*
377 	 * We're looking for the passthrough device associated with this
378 	 * particular bus/target/lun.
379 	 */
380 	sprintf(match_pat->periph_name, "pass");
381 	match_pat->path_id = path_id;
382 	match_pat->target_id = target_id;
383 	match_pat->target_lun = target_lun;
384 	/* Now set the flags to indicate what we're looking for. */
385 	match_pat->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_TARGET |
386 			   PERIPH_MATCH_LUN | PERIPH_MATCH_NAME;
387 
388 	if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
389 		sprintf(cam_errbuf, "%s: CAMIOCOMMAND ioctl failed\n"
390 			"%s: %s", func_name, func_name, strerror(errno));
391 		goto btl_bailout;
392 	}
393 
394 	/*
395 	 * Check for an outright error.
396 	 */
397 	if ((ccb.ccb_h.status != CAM_REQ_CMP)
398 	 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
399 	   && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
400 		sprintf(cam_errbuf, "%s: CAM error %#x, CDM error %d "
401 			"returned from XPT_DEV_MATCH ccb", func_name,
402 			ccb.ccb_h.status, ccb.cdm.status);
403 		goto btl_bailout;
404 	}
405 
406 	if (ccb.cdm.status == CAM_DEV_MATCH_MORE) {
407 		sprintf(cam_errbuf, "%s: CDM reported more than one"
408 			" passthrough device at %d:%d:%d!!\n",
409 			func_name, path_id, target_id, target_lun);
410 		goto btl_bailout;
411 	}
412 
413 	if (ccb.cdm.num_matches == 0) {
414 		sprintf(cam_errbuf, "%s: no passthrough device found at"
415 			" %d:%d:%d", func_name, path_id, target_id,
416 			target_lun);
417 		goto btl_bailout;
418 	}
419 
420 	switch(ccb.cdm.matches[0].type) {
421 	case DEV_MATCH_PERIPH: {
422 		int pass_unit;
423 		char dev_path[256];
424 		struct periph_match_result *periph_result;
425 
426 		periph_result = &ccb.cdm.matches[0].result.periph_result;
427 		pass_unit = periph_result->unit_number;
428 		free(ccb.cdm.matches);
429 		free(ccb.cdm.patterns);
430 		close(fd);
431 		sprintf(dev_path, "/dev/pass%d", pass_unit);
432 		return(cam_real_open_device(dev_path, flags, device, NULL,
433 					    NULL, 0));
434 		break; /* NOTREACHED */
435 	}
436 	default:
437 		sprintf(cam_errbuf, "%s: asked for a peripheral match, but"
438 			" got a bus or device match", func_name);
439 		goto btl_bailout;
440 		break; /* NOTREACHED */
441 	}
442 
443 btl_bailout:
444 	free(ccb.cdm.matches);
445 	free(ccb.cdm.patterns);
446 	close(fd);
447 	return(NULL);
448 }
449 
450 struct cam_device *
451 cam_open_spec_device(const char *dev_name, int unit, int flags,
452 		     struct cam_device *device)
453 {
454 	return(cam_lookup_pass(dev_name, unit, flags, NULL, device));
455 }
456 
457 struct cam_device *
458 cam_open_pass(const char *path, int flags, struct cam_device *device)
459 {
460 	return(cam_real_open_device(path, flags, device, path, NULL, 0));
461 }
462 
463 static struct cam_device *
464 cam_lookup_pass(const char *dev_name, int unit, int flags,
465 		const char *given_path, struct cam_device *device)
466 {
467 	int fd;
468 	union ccb ccb;
469 	char dev_path[256];
470 	char *func_name = "cam_lookup_pass";
471 
472 	/*
473 	 * The flags argument above only applies to the actual passthrough
474 	 * device open, not our open of the given device to find the
475 	 * passthrough device.
476 	 */
477 	if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
478 		snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
479 			 "%s: couldn't open %s\n%s: %s", func_name, XPT_DEVICE,
480 			 func_name, strerror(errno));
481 		return(NULL);
482 	}
483 
484 	/* This isn't strictly necessary for the GETPASSTHRU ioctl. */
485 	ccb.ccb_h.func_code = XPT_GDEVLIST;
486 
487 	/* These two are necessary for the GETPASSTHRU ioctl to work. */
488 	strlcpy(ccb.cgdl.periph_name, dev_name, sizeof(ccb.cgdl.periph_name));
489 	ccb.cgdl.unit_number = unit;
490 
491 	/*
492 	 * Attempt to get the passthrough device.  This ioctl will fail if
493 	 * the device name is null, if the device doesn't exist, or if the
494 	 * passthrough driver isn't in the kernel.
495 	 */
496 	if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
497 		char tmpstr[256];
498 
499 		/*
500 		 * If we get ENOENT from the transport layer version of
501 		 * the CAMGETPASSTHRU ioctl, it means one of two things:
502 		 * either the device name/unit number passed in doesn't
503 		 * exist, or the passthrough driver isn't in the kernel.
504 		 */
505 		if (errno == ENOENT) {
506 			snprintf(tmpstr, sizeof(tmpstr),
507 				 "\n%s: either the pass driver isn't in "
508 				 "your kernel\n%s: or %s%d doesn't exist",
509 				 func_name, func_name, dev_name, unit);
510 		}
511 		snprintf(cam_errbuf, sizeof(cam_errbuf),
512 			 "%s: CAMGETPASSTHRU ioctl failed\n"
513 			 "%s: %s%s", func_name, func_name, strerror(errno),
514 			 (errno == ENOENT) ? tmpstr : "");
515 
516 		close(fd);
517 		return(NULL);
518 	}
519 
520 	close(fd);
521 
522 	/*
523 	 * If the ioctl returned the right status, but we got an error back
524 	 * in the ccb, that means that the kernel found the device the user
525 	 * passed in, but was unable to find the passthrough device for
526 	 * the device the user gave us.
527 	 */
528 	if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
529 		sprintf(cam_errbuf, "%s: device %s%d does not exist!",
530 			func_name, dev_name, unit);
531 		return(NULL);
532 	}
533 
534 	sprintf(dev_path, "/dev/%s%d", ccb.cgdl.periph_name,
535 		ccb.cgdl.unit_number);
536 
537 	return(cam_real_open_device(dev_path, flags, device, NULL,
538 				    dev_name, unit));
539 }
540 
541 /*
542  * Open a given device.  The path argument isn't strictly necessary, but it
543  * is copied into the cam_device structure as a convenience to the user.
544  */
545 static struct cam_device *
546 cam_real_open_device(const char *path, int flags, struct cam_device *device,
547 		     const char *given_path, const char *given_dev_name,
548 		     int given_unit_number)
549 {
550 	char *func_name = "cam_real_open_device";
551 	union ccb ccb;
552 	int fd = -1, malloced_device = 0;
553 
554 	/*
555 	 * See if the user wants us to malloc a device for him.
556 	 */
557 	if (device == NULL) {
558 		if ((device = (struct cam_device *)malloc(
559 		     sizeof(struct cam_device))) == NULL) {
560 			sprintf(cam_errbuf, "%s: device structure malloc"
561 				" failed\n%s: %s", func_name, func_name,
562 				strerror(errno));
563 			return(NULL);
564 		}
565 		device->fd = -1;
566 		malloced_device = 1;
567 	}
568 
569 	/*
570 	 * If the user passed in a path, save it for him.
571 	 */
572 	if (given_path != NULL)
573 		strlcpy(device->device_path, given_path,
574 			sizeof(device->device_path));
575 	else
576 		device->device_path[0] = '\0';
577 
578 	/*
579 	 * If the user passed in a device name and unit number pair, save
580 	 * those as well.
581 	 */
582 	if (given_dev_name != NULL)
583 		strlcpy(device->given_dev_name, given_dev_name,
584 			sizeof(device->given_dev_name));
585 	else
586 		device->given_dev_name[0] = '\0';
587 	device->given_unit_number = given_unit_number;
588 
589 	if ((fd = open(path, flags)) < 0) {
590 		snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
591 			 "%s: couldn't open passthrough device %s\n"
592 			 "%s: %s", func_name, path, func_name,
593 			 strerror(errno));
594 		goto crod_bailout;
595 	}
596 
597 	device->fd = fd;
598 
599 	bzero(&ccb, sizeof(union ccb));
600 
601 	/*
602 	 * Unlike the transport layer version of the GETPASSTHRU ioctl,
603 	 * we don't have to set any fields.
604 	 */
605 	ccb.ccb_h.func_code = XPT_GDEVLIST;
606 
607 	/*
608 	 * We're only doing this to get some information on the device in
609 	 * question.  Otherwise, we'd have to pass in yet another
610 	 * parameter: the passthrough driver unit number.
611 	 */
612 	if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
613 		/*
614 		 * At this point we know the passthrough device must exist
615 		 * because we just opened it above.  The only way this
616 		 * ioctl can fail is if the ccb size is wrong.
617 		 */
618 		sprintf(cam_errbuf, "%s: CAMGETPASSTHRU ioctl failed\n"
619 			"%s: %s", func_name, func_name, strerror(errno));
620 		goto crod_bailout;
621 	}
622 
623 	/*
624 	 * If the ioctl returned the right status, but we got an error back
625 	 * in the ccb, that means that the kernel found the device the user
626 	 * passed in, but was unable to find the passthrough device for
627 	 * the device the user gave us.
628 	 */
629 	if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
630 		sprintf(cam_errbuf, "%s: passthrough device does not exist!",
631 			func_name);
632 		goto crod_bailout;
633 	}
634 
635 	device->dev_unit_num = ccb.cgdl.unit_number;
636 	strlcpy(device->device_name, ccb.cgdl.periph_name,
637 		sizeof(device->device_name));
638 	device->path_id = ccb.ccb_h.path_id;
639 	device->target_id = ccb.ccb_h.target_id;
640 	device->target_lun = ccb.ccb_h.target_lun;
641 
642 	ccb.ccb_h.func_code = XPT_PATH_INQ;
643 	if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
644 		sprintf(cam_errbuf, "%s: Path Inquiry CCB failed\n"
645 			"%s: %s", func_name, func_name, strerror(errno));
646 		goto crod_bailout;
647 	}
648 	strlcpy(device->sim_name, ccb.cpi.dev_name, sizeof(device->sim_name));
649 	device->sim_unit_number = ccb.cpi.unit_number;
650 	device->bus_id = ccb.cpi.bus_id;
651 
652 	/*
653 	 * It doesn't really matter what is in the payload for a getdev
654 	 * CCB, the kernel doesn't look at it.
655 	 */
656 	ccb.ccb_h.func_code = XPT_GDEV_TYPE;
657 	if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
658 		sprintf(cam_errbuf, "%s: Get Device Type CCB failed\n"
659 			"%s: %s", func_name, func_name, strerror(errno));
660 		goto crod_bailout;
661 	}
662 	device->pd_type = SID_TYPE(&ccb.cgd.inq_data);
663 	bcopy(&ccb.cgd.inq_data, &device->inq_data,
664 	      sizeof(struct scsi_inquiry_data));
665 	device->serial_num_len = ccb.cgd.serial_num_len;
666 	bcopy(&ccb.cgd.serial_num, &device->serial_num, device->serial_num_len);
667 
668 	/*
669 	 * Zero the payload, the kernel does look at the flags.
670 	 */
671 	bzero(&(&ccb.ccb_h)[1], sizeof(struct ccb_trans_settings));
672 
673 	/*
674 	 * Get transfer settings for this device.
675 	 */
676 	ccb.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
677 
678 	ccb.cts.type = CTS_TYPE_CURRENT_SETTINGS;
679 
680 	if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
681 		sprintf(cam_errbuf, "%s: Get Transfer Settings CCB failed\n"
682 			"%s: %s", func_name, func_name, strerror(errno));
683 		goto crod_bailout;
684 	}
685 	if (ccb.cts.transport == XPORT_SPI) {
686 		struct ccb_trans_settings_spi *spi =
687 		    &ccb.cts.xport_specific.spi;
688 		device->sync_period = spi->sync_period;
689 		device->sync_offset = spi->sync_offset;
690 		device->bus_width = spi->bus_width;
691 	} else {
692 		device->sync_period = 0;
693 		device->sync_offset = 0;
694 		device->bus_width = 0;
695 	}
696 
697 	return(device);
698 
699 crod_bailout:
700 
701 	if (fd >= 0)
702 		close(fd);
703 
704 	if (malloced_device)
705 		free(device);
706 
707 	return(NULL);
708 }
709 
710 void
711 cam_close_device(struct cam_device *dev)
712 {
713 	if (dev == NULL)
714 		return;
715 
716 	cam_close_spec_device(dev);
717 
718 	free(dev);
719 }
720 
721 void
722 cam_close_spec_device(struct cam_device *dev)
723 {
724 	if (dev == NULL)
725 		return;
726 
727 	if (dev->fd >= 0)
728 		close(dev->fd);
729 }
730 
731 char *
732 cam_path_string(struct cam_device *dev, char *str, int len)
733 {
734 	if (dev == NULL) {
735 		snprintf(str, len, "No path");
736 		return(str);
737 	}
738 
739 	snprintf(str, len, "(%s%d:%s%d:%d:%d:%d): ",
740 		 (dev->device_name[0] != '\0') ? dev->device_name : "pass",
741 		 dev->dev_unit_num,
742 		 (dev->sim_name[0] != '\0') ? dev->sim_name : "unknown",
743 		 dev->sim_unit_number,
744 		 dev->bus_id,
745 		 dev->target_id,
746 		 dev->target_lun);
747 
748 	return(str);
749 }
750 
751 /*
752  * Malloc/duplicate a CAM device structure.
753  */
754 struct cam_device *
755 cam_device_dup(struct cam_device *device)
756 {
757 	char *func_name = "cam_device_dup";
758 	struct cam_device *newdev;
759 
760 	if (device == NULL) {
761 		sprintf(cam_errbuf, "%s: device is NULL", func_name);
762 		return(NULL);
763 	}
764 
765 	newdev = malloc(sizeof(struct cam_device));
766 	if (newdev == NULL) {
767 		snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
768 			 "%s: couldn't malloc CAM device structure", func_name);
769 		return(NULL);
770 	}
771 
772 	bcopy(device, newdev, sizeof(struct cam_device));
773 
774 	return(newdev);
775 }
776 
777 /*
778  * Copy a CAM device structure.
779  */
780 void
781 cam_device_copy(struct cam_device *src, struct cam_device *dst)
782 {
783 	char *func_name = "cam_device_copy";
784 
785 	if (src == NULL) {
786 		sprintf(cam_errbuf, "%s: source device struct was NULL",
787 			func_name);
788 		return;
789 	}
790 
791 	if (dst == NULL) {
792 		sprintf(cam_errbuf, "%s: destination device struct was NULL",
793 			func_name);
794 		return;
795 	}
796 
797 	bcopy(src, dst, sizeof(struct cam_device));
798 
799 }
800