]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - lib/libcam/camlib.c
MFC r315798:
[FreeBSD/stable/10.git] / lib / libcam / camlib.c
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 <stdint.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <ctype.h>
39
40 #include <cam/cam.h>
41 #include <cam/scsi/scsi_all.h>
42 #include <cam/cam_ccb.h>
43 #include <cam/scsi/scsi_pass.h>
44 #include "camlib.h"
45
46
47 static const char *nonrewind_devs[] = {
48         "sa"
49 };
50
51 char cam_errbuf[CAM_ERRBUF_SIZE];
52
53 static struct cam_device *cam_real_open_device(const char *path, int flags,
54                                                struct cam_device *device,
55                                                const char *given_path,
56                                                const char *given_dev_name,
57                                                int given_unit_number);
58 static struct cam_device *cam_lookup_pass(const char *dev_name, int unit,
59                                           int flags, const char *given_path,
60                                           struct cam_device *device);
61
62 /*
63  * Send a ccb to a passthrough device.
64  */
65 int
66 cam_send_ccb(struct cam_device *device, union ccb *ccb)
67 {
68         return(ioctl(device->fd, CAMIOCOMMAND, ccb));
69 }
70
71 /*
72  * Malloc a CCB, zero out the header and set its path, target and lun ids.
73  */
74 union ccb *
75 cam_getccb(struct cam_device *dev)
76 {
77         union ccb *ccb;
78
79         ccb = (union ccb *)malloc(sizeof(union ccb));
80         if (ccb != NULL) {
81                 bzero(&ccb->ccb_h, sizeof(struct ccb_hdr));
82                 ccb->ccb_h.path_id = dev->path_id;
83                 ccb->ccb_h.target_id = dev->target_id;
84                 ccb->ccb_h.target_lun = dev->target_lun;
85         }
86
87         return(ccb);
88 }
89
90 /*
91  * Free a CCB.
92  */
93 void
94 cam_freeccb(union ccb *ccb)
95 {
96         free(ccb);
97 }
98
99 /*
100  * Take a device name or path passed in by the user, and attempt to figure
101  * out the device name and unit number.  Some possible device name formats are:
102  * /dev/foo0
103  * foo0
104  * nfoo0
105  *
106  * Some peripheral drivers create separate device nodes with 'n' prefix for
107  * non-rewind operations.  Currently only sa(4) tape driver has this feature.
108  * We extract pure peripheral name as device name for this special case.
109  *
110  * Input parameters:  device name/path, length of devname string
111  * Output:            device name, unit number
112  * Return values:     returns 0 for success, -1 for failure
113  */
114 int
115 cam_get_device(const char *path, char *dev_name, int devnamelen, int *unit)
116 {
117         char *func_name = "cam_get_device";
118         char *tmpstr, *tmpstr2;
119         char *newpath;
120         int unit_offset;
121         int i;
122
123
124         if (path == NULL) {
125                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
126                          "%s: device pathname was NULL", func_name);
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, CAM_ERRBUF_SIZE,
149                          "%s: no text after slash", func_name);
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, CAM_ERRBUF_SIZE,
177                          "%s: must have both device name and unit number",
178                          func_name);
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, CAM_ERRBUF_SIZE,
189                          "%s: device name cannot begin with a number",
190                          func_name);
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, CAM_ERRBUF_SIZE,
202                          "%s: unable to find device unit number", func_name);
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         char *func_name = "cam_open_btl";
275         int fd, bufsize;
276
277         if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
278                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
279                          "%s: couldn't open %s\n%s: %s", func_name, XPT_DEVICE,
280                          func_name, strerror(errno));
281                 return(NULL);
282         }
283
284         bzero(&ccb, sizeof(union ccb));
285         ccb.ccb_h.func_code = XPT_DEV_MATCH;
286         ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
287         ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
288         ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
289
290         /* Setup the result buffer */
291         bufsize = sizeof(struct dev_match_result);
292         ccb.cdm.match_buf_len = bufsize;
293         ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
294         if (ccb.cdm.matches == NULL) {
295                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
296                          "%s: couldn't malloc match buffer", func_name);
297                 close(fd);
298                 return(NULL);
299         }
300         ccb.cdm.num_matches = 0;
301
302         /* Setup the pattern buffer */
303         ccb.cdm.num_patterns = 1;
304         ccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
305         ccb.cdm.patterns = (struct dev_match_pattern *)malloc(
306                 sizeof(struct dev_match_pattern));
307         if (ccb.cdm.patterns == NULL) {
308                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
309                          "%s: couldn't malloc pattern buffer", func_name);
310                 free(ccb.cdm.matches);
311                 ccb.cdm.matches = NULL;
312                 close(fd);
313                 return(NULL);
314         }
315         ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH;
316         match_pat = &ccb.cdm.patterns[0].pattern.periph_pattern;
317
318         /*
319          * We're looking for the passthrough device associated with this
320          * particular bus/target/lun.
321          */
322         sprintf(match_pat->periph_name, "pass");
323         match_pat->path_id = path_id;
324         match_pat->target_id = target_id;
325         match_pat->target_lun = target_lun;
326         /* Now set the flags to indicate what we're looking for. */
327         match_pat->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_TARGET |
328                            PERIPH_MATCH_LUN | PERIPH_MATCH_NAME;
329
330         if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
331                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
332                          "%s: CAMIOCOMMAND ioctl failed\n"
333                          "%s: %s", func_name, func_name, strerror(errno));
334                 goto btl_bailout;
335         }
336
337         /*
338          * Check for an outright error.
339          */
340         if ((ccb.ccb_h.status != CAM_REQ_CMP)
341          || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
342            && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
343                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
344                          "%s: CAM error %#x, CDM error %d "
345                          "returned from XPT_DEV_MATCH ccb", func_name,
346                          ccb.ccb_h.status, ccb.cdm.status);
347                 goto btl_bailout;
348         }
349
350         if (ccb.cdm.status == CAM_DEV_MATCH_MORE) {
351                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
352                          "%s: CDM reported more than one"
353                          " passthrough device at %d:%d:%jx!!\n",
354                          func_name, path_id, target_id, (uintmax_t)target_lun);
355                 goto btl_bailout;
356         }
357
358         if (ccb.cdm.num_matches == 0) {
359                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
360                          "%s: no passthrough device found at"
361                          " %d:%d:%jx", func_name, path_id, target_id,
362                          (uintmax_t)target_lun);
363                 goto btl_bailout;
364         }
365
366         switch(ccb.cdm.matches[0].type) {
367         case DEV_MATCH_PERIPH: {
368                 int pass_unit;
369                 char dev_path[256];
370                 struct periph_match_result *periph_result;
371
372                 periph_result = &ccb.cdm.matches[0].result.periph_result;
373                 pass_unit = periph_result->unit_number;
374                 free(ccb.cdm.matches);
375                 ccb.cdm.matches = NULL;
376                 free(ccb.cdm.patterns);
377                 ccb.cdm.patterns = NULL;
378                 close(fd);
379                 sprintf(dev_path, "/dev/pass%d", pass_unit);
380                 return(cam_real_open_device(dev_path, flags, device, NULL,
381                                             NULL, 0));
382                 break; /* NOTREACHED */
383         }
384         default:
385                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
386                          "%s: asked for a peripheral match, but"
387                          " got a bus or device match", func_name);
388                 goto btl_bailout;
389                 break; /* NOTREACHED */
390         }
391
392 btl_bailout:
393         free(ccb.cdm.matches);
394         ccb.cdm.matches = NULL;
395         free(ccb.cdm.patterns);
396         ccb.cdm.patterns = NULL;
397         close(fd);
398         return(NULL);
399 }
400
401 struct cam_device *
402 cam_open_spec_device(const char *dev_name, int unit, int flags,
403                      struct cam_device *device)
404 {
405         return(cam_lookup_pass(dev_name, unit, flags, NULL, device));
406 }
407
408 struct cam_device *
409 cam_open_pass(const char *path, int flags, struct cam_device *device)
410 {
411         return(cam_real_open_device(path, flags, device, path, NULL, 0));
412 }
413
414 static struct cam_device *
415 cam_lookup_pass(const char *dev_name, int unit, int flags,
416                 const char *given_path, struct cam_device *device)
417 {
418         int fd;
419         union ccb ccb;
420         char dev_path[256];
421         char *func_name = "cam_lookup_pass";
422
423         /*
424          * The flags argument above only applies to the actual passthrough
425          * device open, not our open of the given device to find the
426          * passthrough device.
427          */
428         if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
429                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
430                          "%s: couldn't open %s\n%s: %s", func_name, XPT_DEVICE,
431                          func_name, strerror(errno));
432                 return(NULL);
433         }
434
435         /* This isn't strictly necessary for the GETPASSTHRU ioctl. */
436         ccb.ccb_h.func_code = XPT_GDEVLIST;
437
438         /* These two are necessary for the GETPASSTHRU ioctl to work. */
439         strlcpy(ccb.cgdl.periph_name, dev_name, sizeof(ccb.cgdl.periph_name));
440         ccb.cgdl.unit_number = unit;
441
442         /*
443          * Attempt to get the passthrough device.  This ioctl will fail if
444          * the device name is null, if the device doesn't exist, or if the
445          * passthrough driver isn't in the kernel.
446          */
447         if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
448                 char tmpstr[256];
449
450                 /*
451                  * If we get ENOENT from the transport layer version of
452                  * the CAMGETPASSTHRU ioctl, it means one of two things:
453                  * either the device name/unit number passed in doesn't
454                  * exist, or the passthrough driver isn't in the kernel.
455                  */
456                 if (errno == ENOENT) {
457                         snprintf(tmpstr, sizeof(tmpstr),
458                                  "\n%s: either the pass driver isn't in "
459                                  "your kernel\n%s: or %s%d doesn't exist",
460                                  func_name, func_name, dev_name, unit);
461                 }
462                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
463                          "%s: CAMGETPASSTHRU ioctl failed\n"
464                          "%s: %s%s", func_name, func_name, strerror(errno),
465                          (errno == ENOENT) ? tmpstr : "");
466
467                 close(fd);
468                 return(NULL);
469         }
470
471         close(fd);
472
473         /*
474          * If the ioctl returned the right status, but we got an error back
475          * in the ccb, that means that the kernel found the device the user
476          * passed in, but was unable to find the passthrough device for
477          * the device the user gave us.
478          */
479         if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
480                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
481                          "%s: device %s%d does not exist!",
482                          func_name, dev_name, unit);
483                 return(NULL);
484         }
485
486         sprintf(dev_path, "/dev/%s%d", ccb.cgdl.periph_name,
487                 ccb.cgdl.unit_number);
488
489         return(cam_real_open_device(dev_path, flags, device, NULL,
490                                     dev_name, unit));
491 }
492
493 /*
494  * Open a given device.  The path argument isn't strictly necessary, but it
495  * is copied into the cam_device structure as a convenience to the user.
496  */
497 static struct cam_device *
498 cam_real_open_device(const char *path, int flags, struct cam_device *device,
499                      const char *given_path, const char *given_dev_name,
500                      int given_unit_number)
501 {
502         char *func_name = "cam_real_open_device";
503         union ccb ccb;
504         int fd = -1, malloced_device = 0;
505
506         /*
507          * See if the user wants us to malloc a device for him.
508          */
509         if (device == NULL) {
510                 if ((device = (struct cam_device *)malloc(
511                      sizeof(struct cam_device))) == NULL) {
512                         snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
513                                  "%s: device structure malloc"
514                                  " failed\n%s: %s", func_name, func_name,
515                                  strerror(errno));
516                         return(NULL);
517                 }
518                 device->fd = -1;
519                 malloced_device = 1;
520         }
521
522         /*
523          * If the user passed in a path, save it for him.
524          */
525         if (given_path != NULL)
526                 strlcpy(device->device_path, given_path,
527                         sizeof(device->device_path));
528         else
529                 device->device_path[0] = '\0';
530
531         /*
532          * If the user passed in a device name and unit number pair, save
533          * those as well.
534          */
535         if (given_dev_name != NULL)
536                 strlcpy(device->given_dev_name, given_dev_name,
537                         sizeof(device->given_dev_name));
538         else
539                 device->given_dev_name[0] = '\0';
540         device->given_unit_number = given_unit_number;
541
542         if ((fd = open(path, flags)) < 0) {
543                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
544                          "%s: couldn't open passthrough device %s\n"
545                          "%s: %s", func_name, path, func_name,
546                          strerror(errno));
547                 goto crod_bailout;
548         }
549
550         device->fd = fd;
551
552         bzero(&ccb, sizeof(union ccb));
553
554         /*
555          * Unlike the transport layer version of the GETPASSTHRU ioctl,
556          * we don't have to set any fields.
557          */
558         ccb.ccb_h.func_code = XPT_GDEVLIST;
559
560         /*
561          * We're only doing this to get some information on the device in
562          * question.  Otherwise, we'd have to pass in yet another
563          * parameter: the passthrough driver unit number.
564          */
565         if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
566                 /*
567                  * At this point we know the passthrough device must exist
568                  * because we just opened it above.  The only way this
569                  * ioctl can fail is if the ccb size is wrong.
570                  */
571                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
572                          "%s: CAMGETPASSTHRU ioctl failed\n"
573                          "%s: %s", func_name, func_name, strerror(errno));
574                 goto crod_bailout;
575         }
576
577         /*
578          * If the ioctl returned the right status, but we got an error back
579          * in the ccb, that means that the kernel found the device the user
580          * passed in, but was unable to find the passthrough device for
581          * the device the user gave us.
582          */
583         if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
584                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
585                          "%s: passthrough device does not exist!", func_name);
586                 goto crod_bailout;
587         }
588
589         device->dev_unit_num = ccb.cgdl.unit_number;
590         strlcpy(device->device_name, ccb.cgdl.periph_name,
591                 sizeof(device->device_name));
592         device->path_id = ccb.ccb_h.path_id;
593         device->target_id = ccb.ccb_h.target_id;
594         device->target_lun = ccb.ccb_h.target_lun;
595
596         ccb.ccb_h.func_code = XPT_PATH_INQ;
597         if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
598                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
599                          "%s: Path Inquiry CCB failed\n"
600                          "%s: %s", func_name, func_name, strerror(errno));
601                 goto crod_bailout;
602         }
603         strlcpy(device->sim_name, ccb.cpi.dev_name, sizeof(device->sim_name));
604         device->sim_unit_number = ccb.cpi.unit_number;
605         device->bus_id = ccb.cpi.bus_id;
606
607         /*
608          * It doesn't really matter what is in the payload for a getdev
609          * CCB, the kernel doesn't look at it.
610          */
611         ccb.ccb_h.func_code = XPT_GDEV_TYPE;
612         if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
613                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
614                          "%s: Get Device Type CCB failed\n"
615                          "%s: %s", func_name, func_name, strerror(errno));
616                 goto crod_bailout;
617         }
618         device->pd_type = SID_TYPE(&ccb.cgd.inq_data);
619         bcopy(&ccb.cgd.inq_data, &device->inq_data,
620               sizeof(struct scsi_inquiry_data));
621         device->serial_num_len = ccb.cgd.serial_num_len;
622         bcopy(&ccb.cgd.serial_num, &device->serial_num, device->serial_num_len);
623
624         /*
625          * Zero the payload, the kernel does look at the flags.
626          */
627         CCB_CLEAR_ALL_EXCEPT_HDR(&ccb.cts);
628
629         /*
630          * Get transfer settings for this device.
631          */
632         ccb.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
633
634         ccb.cts.type = CTS_TYPE_CURRENT_SETTINGS;
635
636         if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
637                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
638                          "%s: Get Transfer Settings CCB failed\n"
639                          "%s: %s", func_name, func_name, strerror(errno));
640                 goto crod_bailout;
641         }
642         if (ccb.cts.transport == XPORT_SPI) {
643                 struct ccb_trans_settings_spi *spi =
644                     &ccb.cts.xport_specific.spi;
645                 device->sync_period = spi->sync_period;
646                 device->sync_offset = spi->sync_offset;
647                 device->bus_width = spi->bus_width;
648         } else {
649                 device->sync_period = 0;
650                 device->sync_offset = 0;
651                 device->bus_width = 0;
652         }
653
654         return(device);
655
656 crod_bailout:
657
658         if (fd >= 0)
659                 close(fd);
660
661         if (malloced_device)
662                 free(device);
663
664         return(NULL);
665 }
666
667 void
668 cam_close_device(struct cam_device *dev)
669 {
670         if (dev == NULL)
671                 return;
672
673         cam_close_spec_device(dev);
674
675         free(dev);
676 }
677
678 void
679 cam_close_spec_device(struct cam_device *dev)
680 {
681         if (dev == NULL)
682                 return;
683
684         if (dev->fd >= 0) {
685                 close(dev->fd);
686                 dev->fd = -1;
687         }
688 }
689
690 char *
691 cam_path_string(struct cam_device *dev, char *str, int len)
692 {
693         if (dev == NULL) {
694                 snprintf(str, len, "No path");
695                 return(str);
696         }
697
698         snprintf(str, len, "(%s%d:%s%d:%d:%d:%jx): ",
699                  (dev->device_name[0] != '\0') ? dev->device_name : "pass",
700                  dev->dev_unit_num,
701                  (dev->sim_name[0] != '\0') ? dev->sim_name : "unknown",
702                  dev->sim_unit_number,
703                  dev->bus_id,
704                  dev->target_id,
705                  (uintmax_t)dev->target_lun);
706
707         return(str);
708 }
709
710 /*
711  * Malloc/duplicate a CAM device structure.
712  */
713 struct cam_device *
714 cam_device_dup(struct cam_device *device)
715 {
716         char *func_name = "cam_device_dup";
717         struct cam_device *newdev;
718
719         if (device == NULL) {
720                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
721                          "%s: device is NULL", func_name);
722                 return(NULL);
723         }
724
725         newdev = malloc(sizeof(struct cam_device));
726         if (newdev == NULL) {
727                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
728                         "%s: couldn't malloc CAM device structure", func_name);
729                 return(NULL);
730         }
731
732         bcopy(device, newdev, sizeof(struct cam_device));
733
734         return(newdev);
735 }
736
737 /*
738  * Copy a CAM device structure.
739  */
740 void
741 cam_device_copy(struct cam_device *src, struct cam_device *dst)
742 {
743         char *func_name = "cam_device_copy";
744
745         if (src == NULL) {
746                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
747                          "%s: source device struct was NULL", func_name);
748                 return;
749         }
750
751         if (dst == NULL) {
752                 snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
753                          "%s: destination device struct was NULL", func_name);
754                 return;
755         }
756
757         bcopy(src, dst, sizeof(struct cam_device));
758
759 }