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