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