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