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