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