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