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