]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/camcontrol/camcontrol.c
This commit was generated by cvs2svn to compensate for changes in r169962,
[FreeBSD/FreeBSD.git] / sbin / camcontrol / camcontrol.c
1 /*
2  * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2005, 2006 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. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/ioctl.h>
33 #include <sys/stdint.h>
34 #include <sys/types.h>
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <ctype.h>
42 #include <err.h>
43
44 #include <cam/cam.h>
45 #include <cam/cam_debug.h>
46 #include <cam/cam_ccb.h>
47 #include <cam/scsi/scsi_all.h>
48 #include <cam/scsi/scsi_da.h>
49 #include <cam/scsi/scsi_pass.h>
50 #include <cam/scsi/scsi_message.h>
51 #include <camlib.h>
52 #include "camcontrol.h"
53
54 typedef enum {
55         CAM_CMD_NONE            = 0x00000000,
56         CAM_CMD_DEVLIST         = 0x00000001,
57         CAM_CMD_TUR             = 0x00000002,
58         CAM_CMD_INQUIRY         = 0x00000003,
59         CAM_CMD_STARTSTOP       = 0x00000004,
60         CAM_CMD_RESCAN          = 0x00000005,
61         CAM_CMD_READ_DEFECTS    = 0x00000006,
62         CAM_CMD_MODE_PAGE       = 0x00000007,
63         CAM_CMD_SCSI_CMD        = 0x00000008,
64         CAM_CMD_DEVTREE         = 0x00000009,
65         CAM_CMD_USAGE           = 0x0000000a,
66         CAM_CMD_DEBUG           = 0x0000000b,
67         CAM_CMD_RESET           = 0x0000000c,
68         CAM_CMD_FORMAT          = 0x0000000d,
69         CAM_CMD_TAG             = 0x0000000e,
70         CAM_CMD_RATE            = 0x0000000f,
71         CAM_CMD_DETACH          = 0x00000010,
72         CAM_CMD_REPORTLUNS      = 0x00000011
73 } cam_cmdmask;
74
75 typedef enum {
76         CAM_ARG_NONE            = 0x00000000,
77         CAM_ARG_VERBOSE         = 0x00000001,
78         CAM_ARG_DEVICE          = 0x00000002,
79         CAM_ARG_BUS             = 0x00000004,
80         CAM_ARG_TARGET          = 0x00000008,
81         CAM_ARG_LUN             = 0x00000010,
82         CAM_ARG_EJECT           = 0x00000020,
83         CAM_ARG_UNIT            = 0x00000040,
84         CAM_ARG_FORMAT_BLOCK    = 0x00000080,
85         CAM_ARG_FORMAT_BFI      = 0x00000100,
86         CAM_ARG_FORMAT_PHYS     = 0x00000200,
87         CAM_ARG_PLIST           = 0x00000400,
88         CAM_ARG_GLIST           = 0x00000800,
89         CAM_ARG_GET_SERIAL      = 0x00001000,
90         CAM_ARG_GET_STDINQ      = 0x00002000,
91         CAM_ARG_GET_XFERRATE    = 0x00004000,
92         CAM_ARG_INQ_MASK        = 0x00007000,
93         CAM_ARG_MODE_EDIT       = 0x00008000,
94         CAM_ARG_PAGE_CNTL       = 0x00010000,
95         CAM_ARG_TIMEOUT         = 0x00020000,
96         CAM_ARG_CMD_IN          = 0x00040000,
97         CAM_ARG_CMD_OUT         = 0x00080000,
98         CAM_ARG_DBD             = 0x00100000,
99         CAM_ARG_ERR_RECOVER     = 0x00200000,
100         CAM_ARG_RETRIES         = 0x00400000,
101         CAM_ARG_START_UNIT      = 0x00800000,
102         CAM_ARG_DEBUG_INFO      = 0x01000000,
103         CAM_ARG_DEBUG_TRACE     = 0x02000000,
104         CAM_ARG_DEBUG_SUBTRACE  = 0x04000000,
105         CAM_ARG_DEBUG_CDB       = 0x08000000,
106         CAM_ARG_DEBUG_XPT       = 0x10000000,
107         CAM_ARG_DEBUG_PERIPH    = 0x20000000,
108 } cam_argmask;
109
110 struct camcontrol_opts {
111         const char      *optname;       
112         cam_cmdmask     cmdnum;
113         cam_argmask     argnum;
114         const char      *subopt;
115 };
116
117 #ifndef MINIMALISTIC
118 static const char scsicmd_opts[] = "c:i:o:";
119 static const char readdefect_opts[] = "f:GP";
120 static const char negotiate_opts[] = "acD:O:qR:T:UW:";
121 #endif
122
123 struct camcontrol_opts option_table[] = {
124 #ifndef MINIMALISTIC
125         {"tur", CAM_CMD_TUR, CAM_ARG_NONE, NULL},
126         {"inquiry", CAM_CMD_INQUIRY, CAM_ARG_NONE, "DSR"},
127         {"start", CAM_CMD_STARTSTOP, CAM_ARG_START_UNIT, NULL},
128         {"stop", CAM_CMD_STARTSTOP, CAM_ARG_NONE, NULL},
129         {"load", CAM_CMD_STARTSTOP, CAM_ARG_START_UNIT | CAM_ARG_EJECT, NULL},
130         {"eject", CAM_CMD_STARTSTOP, CAM_ARG_EJECT, NULL},
131         {"reportluns", CAM_CMD_REPORTLUNS, CAM_ARG_NONE, "clr:"},
132 #endif /* MINIMALISTIC */
133         {"rescan", CAM_CMD_RESCAN, CAM_ARG_NONE, NULL},
134         {"reset", CAM_CMD_RESET, CAM_ARG_NONE, NULL},
135 #ifndef MINIMALISTIC
136         {"cmd", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts},
137         {"command", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts},
138         {"defects", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts},
139         {"defectlist", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts},
140 #endif /* MINIMALISTIC */
141         {"devlist", CAM_CMD_DEVTREE, CAM_ARG_NONE, NULL},
142 #ifndef MINIMALISTIC
143         {"periphlist", CAM_CMD_DEVLIST, CAM_ARG_NONE, NULL},
144         {"modepage", CAM_CMD_MODE_PAGE, CAM_ARG_NONE, "bdelm:P:"},
145         {"tags", CAM_CMD_TAG, CAM_ARG_NONE, "N:q"},
146         {"negotiate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts},
147         {"rate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts},
148         {"debug", CAM_CMD_DEBUG, CAM_ARG_NONE, "IPTSXc"},
149         {"format", CAM_CMD_FORMAT, CAM_ARG_NONE, "qrwy"},
150 #endif /* MINIMALISTIC */
151         {"help", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
152         {"-?", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
153         {"-h", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
154         {NULL, 0, 0, NULL}
155 };
156
157 typedef enum {
158         CC_OR_NOT_FOUND,
159         CC_OR_AMBIGUOUS,
160         CC_OR_FOUND
161 } camcontrol_optret;
162
163 cam_cmdmask cmdlist;
164 cam_argmask arglist;
165
166
167 camcontrol_optret getoption(char *arg, cam_cmdmask *cmdnum, cam_argmask *argnum,
168                             const char **subopt);
169 #ifndef MINIMALISTIC
170 static int getdevlist(struct cam_device *device);
171 #endif /* MINIMALISTIC */
172 static int getdevtree(void);
173 #ifndef MINIMALISTIC
174 static int testunitready(struct cam_device *device, int retry_count,
175                          int timeout, int quiet);
176 static int scsistart(struct cam_device *device, int startstop, int loadeject,
177                      int retry_count, int timeout);
178 static int scsidoinquiry(struct cam_device *device, int argc, char **argv,
179                          char *combinedopt, int retry_count, int timeout);
180 static int scsiinquiry(struct cam_device *device, int retry_count, int timeout);
181 static int scsiserial(struct cam_device *device, int retry_count, int timeout);
182 static int scsixferrate(struct cam_device *device);
183 #endif /* MINIMALISTIC */
184 static int parse_btl(char *tstr, int *bus, int *target, int *lun,
185                      cam_argmask *arglst);
186 static int dorescan_or_reset(int argc, char **argv, int rescan);
187 static int rescan_or_reset_bus(int bus, int rescan);
188 static int scanlun_or_reset_dev(int bus, int target, int lun, int scan);
189 #ifndef MINIMALISTIC
190 static int readdefects(struct cam_device *device, int argc, char **argv,
191                        char *combinedopt, int retry_count, int timeout);
192 static void modepage(struct cam_device *device, int argc, char **argv,
193                      char *combinedopt, int retry_count, int timeout);
194 static int scsicmd(struct cam_device *device, int argc, char **argv, 
195                    char *combinedopt, int retry_count, int timeout);
196 static int tagcontrol(struct cam_device *device, int argc, char **argv,
197                       char *combinedopt);
198 static void cts_print(struct cam_device *device,
199                       struct ccb_trans_settings *cts);
200 static void cpi_print(struct ccb_pathinq *cpi);
201 static int get_cpi(struct cam_device *device, struct ccb_pathinq *cpi);
202 static int get_print_cts(struct cam_device *device, int user_settings,
203                          int quiet, struct ccb_trans_settings *cts);
204 static int ratecontrol(struct cam_device *device, int retry_count,
205                        int timeout, int argc, char **argv, char *combinedopt);
206 static int scsiformat(struct cam_device *device, int argc, char **argv,
207                       char *combinedopt, int retry_count, int timeout);
208 static int scsireportluns(struct cam_device *device, int argc, char **argv,
209                           char *combinedopt, int retry_count, int timeout);
210 #endif /* MINIMALISTIC */
211
212 camcontrol_optret
213 getoption(char *arg, cam_cmdmask *cmdnum, cam_argmask *argnum, 
214           const char **subopt)
215 {
216         struct camcontrol_opts *opts;
217         int num_matches = 0;
218
219         for (opts = option_table; (opts != NULL) && (opts->optname != NULL);
220              opts++) {
221                 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
222                         *cmdnum = opts->cmdnum;
223                         *argnum = opts->argnum;
224                         *subopt = opts->subopt;
225                         if (++num_matches > 1)
226                                 return(CC_OR_AMBIGUOUS);
227                 }
228         }
229
230         if (num_matches > 0)
231                 return(CC_OR_FOUND);
232         else
233                 return(CC_OR_NOT_FOUND);
234 }
235
236 #ifndef MINIMALISTIC
237 static int
238 getdevlist(struct cam_device *device)
239 {
240         union ccb *ccb;
241         char status[32];
242         int error = 0;
243
244         ccb = cam_getccb(device);
245
246         ccb->ccb_h.func_code = XPT_GDEVLIST;
247         ccb->ccb_h.flags = CAM_DIR_NONE;
248         ccb->ccb_h.retry_count = 1;
249         ccb->cgdl.index = 0;
250         ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
251         while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
252                 if (cam_send_ccb(device, ccb) < 0) {
253                         perror("error getting device list");
254                         cam_freeccb(ccb);
255                         return(1);
256                 }
257
258                 status[0] = '\0';
259
260                 switch (ccb->cgdl.status) {
261                         case CAM_GDEVLIST_MORE_DEVS:
262                                 strcpy(status, "MORE");
263                                 break;
264                         case CAM_GDEVLIST_LAST_DEVICE:
265                                 strcpy(status, "LAST");
266                                 break;
267                         case CAM_GDEVLIST_LIST_CHANGED:
268                                 strcpy(status, "CHANGED");
269                                 break;
270                         case CAM_GDEVLIST_ERROR:
271                                 strcpy(status, "ERROR");
272                                 error = 1;
273                                 break;
274                 }
275
276                 fprintf(stdout, "%s%d:  generation: %d index: %d status: %s\n",
277                         ccb->cgdl.periph_name,
278                         ccb->cgdl.unit_number,
279                         ccb->cgdl.generation,
280                         ccb->cgdl.index,
281                         status);
282
283                 /*
284                  * If the list has changed, we need to start over from the
285                  * beginning.
286                  */
287                 if (ccb->cgdl.status == CAM_GDEVLIST_LIST_CHANGED)
288                         ccb->cgdl.index = 0;
289         }
290
291         cam_freeccb(ccb);
292
293         return(error);
294 }
295 #endif /* MINIMALISTIC */
296
297 static int
298 getdevtree(void)
299 {
300         union ccb ccb;
301         int bufsize, fd;
302         unsigned int i;
303         int need_close = 0;
304         int error = 0;
305         int skip_device = 0;
306
307         if ((fd = open(XPT_DEVICE, O_RDWR)) == -1) {
308                 warn("couldn't open %s", XPT_DEVICE);
309                 return(1);
310         }
311
312         bzero(&ccb, sizeof(union ccb));
313
314         ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
315         ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
316         ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
317
318         ccb.ccb_h.func_code = XPT_DEV_MATCH;
319         bufsize = sizeof(struct dev_match_result) * 100;
320         ccb.cdm.match_buf_len = bufsize;
321         ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
322         if (ccb.cdm.matches == NULL) {
323                 warnx("can't malloc memory for matches");
324                 close(fd);
325                 return(1);
326         }
327         ccb.cdm.num_matches = 0;
328
329         /*
330          * We fetch all nodes, since we display most of them in the default
331          * case, and all in the verbose case.
332          */
333         ccb.cdm.num_patterns = 0;
334         ccb.cdm.pattern_buf_len = 0;
335
336         /*
337          * We do the ioctl multiple times if necessary, in case there are
338          * more than 100 nodes in the EDT.
339          */
340         do {
341                 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
342                         warn("error sending CAMIOCOMMAND ioctl");
343                         error = 1;
344                         break;
345                 }
346
347                 if ((ccb.ccb_h.status != CAM_REQ_CMP)
348                  || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
349                     && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
350                         warnx("got CAM error %#x, CDM error %d\n",
351                               ccb.ccb_h.status, ccb.cdm.status);
352                         error = 1;
353                         break;
354                 }
355
356                 for (i = 0; i < ccb.cdm.num_matches; i++) {
357                         switch (ccb.cdm.matches[i].type) {
358                         case DEV_MATCH_BUS: {
359                                 struct bus_match_result *bus_result;
360
361                                 /*
362                                  * Only print the bus information if the
363                                  * user turns on the verbose flag.
364                                  */
365                                 if ((arglist & CAM_ARG_VERBOSE) == 0)
366                                         break;
367
368                                 bus_result =
369                                         &ccb.cdm.matches[i].result.bus_result;
370
371                                 if (need_close) {
372                                         fprintf(stdout, ")\n");
373                                         need_close = 0;
374                                 }
375
376                                 fprintf(stdout, "scbus%d on %s%d bus %d:\n",
377                                         bus_result->path_id,
378                                         bus_result->dev_name,
379                                         bus_result->unit_number,
380                                         bus_result->bus_id);
381                                 break;
382                         }
383                         case DEV_MATCH_DEVICE: {
384                                 struct device_match_result *dev_result;
385                                 char vendor[16], product[48], revision[16];
386                                 char tmpstr[256];
387
388                                 dev_result =
389                                      &ccb.cdm.matches[i].result.device_result;
390
391                                 if ((dev_result->flags
392                                      & DEV_RESULT_UNCONFIGURED)
393                                  && ((arglist & CAM_ARG_VERBOSE) == 0)) {
394                                         skip_device = 1;
395                                         break;
396                                 } else
397                                         skip_device = 0;
398
399                                 cam_strvis(vendor, dev_result->inq_data.vendor,
400                                            sizeof(dev_result->inq_data.vendor),
401                                            sizeof(vendor));
402                                 cam_strvis(product,
403                                            dev_result->inq_data.product,
404                                            sizeof(dev_result->inq_data.product),
405                                            sizeof(product));
406                                 cam_strvis(revision,
407                                            dev_result->inq_data.revision,
408                                           sizeof(dev_result->inq_data.revision),
409                                            sizeof(revision));
410                                 sprintf(tmpstr, "<%s %s %s>", vendor, product,
411                                         revision);
412                                 if (need_close) {
413                                         fprintf(stdout, ")\n");
414                                         need_close = 0;
415                                 }
416
417                                 fprintf(stdout, "%-33s  at scbus%d "
418                                         "target %d lun %d (",
419                                         tmpstr,
420                                         dev_result->path_id,
421                                         dev_result->target_id,
422                                         dev_result->target_lun);
423
424                                 need_close = 1;
425
426                                 break;
427                         }
428                         case DEV_MATCH_PERIPH: {
429                                 struct periph_match_result *periph_result;
430
431                                 periph_result =
432                                       &ccb.cdm.matches[i].result.periph_result;
433
434                                 if (skip_device != 0)
435                                         break;
436
437                                 if (need_close > 1)
438                                         fprintf(stdout, ",");
439
440                                 fprintf(stdout, "%s%d",
441                                         periph_result->periph_name,
442                                         periph_result->unit_number);
443
444                                 need_close++;
445                                 break;
446                         }
447                         default:
448                                 fprintf(stdout, "unknown match type\n");
449                                 break;
450                         }
451                 }
452
453         } while ((ccb.ccb_h.status == CAM_REQ_CMP)
454                 && (ccb.cdm.status == CAM_DEV_MATCH_MORE));
455
456         if (need_close)
457                 fprintf(stdout, ")\n");
458
459         close(fd);
460
461         return(error);
462 }
463
464 #ifndef MINIMALISTIC
465 static int
466 testunitready(struct cam_device *device, int retry_count, int timeout,
467               int quiet)
468 {
469         int error = 0;
470         union ccb *ccb;
471
472         ccb = cam_getccb(device);
473
474         scsi_test_unit_ready(&ccb->csio,
475                              /* retries */ retry_count,
476                              /* cbfcnp */ NULL,
477                              /* tag_action */ MSG_SIMPLE_Q_TAG,
478                              /* sense_len */ SSD_FULL_SIZE,
479                              /* timeout */ timeout ? timeout : 5000);
480
481         /* Disable freezing the device queue */
482         ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
483
484         if (arglist & CAM_ARG_ERR_RECOVER)
485                 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
486
487         if (cam_send_ccb(device, ccb) < 0) {
488                 if (quiet == 0)
489                         perror("error sending test unit ready");
490
491                 if (arglist & CAM_ARG_VERBOSE) {
492                         cam_error_print(device, ccb, CAM_ESF_ALL,
493                                         CAM_EPF_ALL, stderr);
494                 }
495
496                 cam_freeccb(ccb);
497                 return(1);
498         }
499
500         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
501                 if (quiet == 0)
502                         fprintf(stdout, "Unit is ready\n");
503         } else {
504                 if (quiet == 0)
505                         fprintf(stdout, "Unit is not ready\n");
506                 error = 1;
507
508                 if (arglist & CAM_ARG_VERBOSE) {
509                         cam_error_print(device, ccb, CAM_ESF_ALL,
510                                         CAM_EPF_ALL, stderr);
511                 }
512         }
513
514         cam_freeccb(ccb);
515
516         return(error);
517 }
518
519 static int
520 scsistart(struct cam_device *device, int startstop, int loadeject,
521           int retry_count, int timeout)
522 {
523         union ccb *ccb;
524         int error = 0;
525
526         ccb = cam_getccb(device);
527
528         /*
529          * If we're stopping, send an ordered tag so the drive in question
530          * will finish any previously queued writes before stopping.  If
531          * the device isn't capable of tagged queueing, or if tagged
532          * queueing is turned off, the tag action is a no-op.
533          */
534         scsi_start_stop(&ccb->csio,
535                         /* retries */ retry_count,
536                         /* cbfcnp */ NULL,
537                         /* tag_action */ startstop ? MSG_SIMPLE_Q_TAG :
538                                                      MSG_ORDERED_Q_TAG,
539                         /* start/stop */ startstop,
540                         /* load_eject */ loadeject,
541                         /* immediate */ 0,
542                         /* sense_len */ SSD_FULL_SIZE,
543                         /* timeout */ timeout ? timeout : 120000);
544
545         /* Disable freezing the device queue */
546         ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
547
548         if (arglist & CAM_ARG_ERR_RECOVER)
549                 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
550
551         if (cam_send_ccb(device, ccb) < 0) {
552                 perror("error sending start unit");
553
554                 if (arglist & CAM_ARG_VERBOSE) {
555                         cam_error_print(device, ccb, CAM_ESF_ALL,
556                                         CAM_EPF_ALL, stderr);
557                 }
558
559                 cam_freeccb(ccb);
560                 return(1);
561         }
562
563         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
564                 if (startstop) {
565                         fprintf(stdout, "Unit started successfully");
566                         if (loadeject)
567                                 fprintf(stdout,", Media loaded\n");
568                         else
569                                 fprintf(stdout,"\n");
570                 } else {
571                         fprintf(stdout, "Unit stopped successfully");
572                         if (loadeject)
573                                 fprintf(stdout, ", Media ejected\n");
574                         else
575                                 fprintf(stdout, "\n");
576                 }
577         else {
578                 error = 1;
579                 if (startstop)
580                         fprintf(stdout,
581                                 "Error received from start unit command\n");
582                 else
583                         fprintf(stdout,
584                                 "Error received from stop unit command\n");
585                         
586                 if (arglist & CAM_ARG_VERBOSE) {
587                         cam_error_print(device, ccb, CAM_ESF_ALL,
588                                         CAM_EPF_ALL, stderr);
589                 }
590         }
591
592         cam_freeccb(ccb);
593
594         return(error);
595 }
596
597 static int
598 scsidoinquiry(struct cam_device *device, int argc, char **argv,
599               char *combinedopt, int retry_count, int timeout)
600 {
601         int c;
602         int error = 0;
603
604         while ((c = getopt(argc, argv, combinedopt)) != -1) {
605                 switch(c) {
606                 case 'D':
607                         arglist |= CAM_ARG_GET_STDINQ;
608                         break;
609                 case 'R':
610                         arglist |= CAM_ARG_GET_XFERRATE;
611                         break;
612                 case 'S':
613                         arglist |= CAM_ARG_GET_SERIAL;
614                         break;
615                 default:
616                         break;
617                 }
618         }
619
620         /*
621          * If the user didn't specify any inquiry options, he wants all of
622          * them.
623          */
624         if ((arglist & CAM_ARG_INQ_MASK) == 0)
625                 arglist |= CAM_ARG_INQ_MASK;
626
627         if (arglist & CAM_ARG_GET_STDINQ)
628                 error = scsiinquiry(device, retry_count, timeout);
629
630         if (error != 0)
631                 return(error);
632
633         if (arglist & CAM_ARG_GET_SERIAL)
634                 scsiserial(device, retry_count, timeout);
635
636         if (error != 0)
637                 return(error);
638
639         if (arglist & CAM_ARG_GET_XFERRATE)
640                 error = scsixferrate(device);
641
642         return(error);
643 }
644
645 static int
646 scsiinquiry(struct cam_device *device, int retry_count, int timeout)
647 {
648         union ccb *ccb;
649         struct scsi_inquiry_data *inq_buf;
650         int error = 0;
651         
652         ccb = cam_getccb(device);
653
654         if (ccb == NULL) {
655                 warnx("couldn't allocate CCB");
656                 return(1);
657         }
658
659         /* cam_getccb cleans up the header, caller has to zero the payload */
660         bzero(&(&ccb->ccb_h)[1],
661               sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
662
663         inq_buf = (struct scsi_inquiry_data *)malloc(
664                 sizeof(struct scsi_inquiry_data));
665
666         if (inq_buf == NULL) {
667                 cam_freeccb(ccb);
668                 warnx("can't malloc memory for inquiry\n");
669                 return(1);
670         }
671         bzero(inq_buf, sizeof(*inq_buf));
672
673         /*
674          * Note that although the size of the inquiry buffer is the full
675          * 256 bytes specified in the SCSI spec, we only tell the device
676          * that we have allocated SHORT_INQUIRY_LENGTH bytes.  There are
677          * two reasons for this:
678          *
679          *  - The SCSI spec says that when a length field is only 1 byte,
680          *    a value of 0 will be interpreted as 256.  Therefore
681          *    scsi_inquiry() will convert an inq_len (which is passed in as
682          *    a u_int32_t, but the field in the CDB is only 1 byte) of 256
683          *    to 0.  Evidently, very few devices meet the spec in that
684          *    regard.  Some devices, like many Seagate disks, take the 0 as 
685          *    0, and don't return any data.  One Pioneer DVD-R drive
686          *    returns more data than the command asked for.
687          *
688          *    So, since there are numerous devices that just don't work
689          *    right with the full inquiry size, we don't send the full size.
690          * 
691          *  - The second reason not to use the full inquiry data length is
692          *    that we don't need it here.  The only reason we issue a
693          *    standard inquiry is to get the vendor name, device name,
694          *    and revision so scsi_print_inquiry() can print them.
695          *
696          * If, at some point in the future, more inquiry data is needed for
697          * some reason, this code should use a procedure similar to the
698          * probe code.  i.e., issue a short inquiry, and determine from
699          * the additional length passed back from the device how much
700          * inquiry data the device supports.  Once the amount the device
701          * supports is determined, issue an inquiry for that amount and no
702          * more.
703          *
704          * KDM, 2/18/2000
705          */
706         scsi_inquiry(&ccb->csio,
707                      /* retries */ retry_count,
708                      /* cbfcnp */ NULL,
709                      /* tag_action */ MSG_SIMPLE_Q_TAG,
710                      /* inq_buf */ (u_int8_t *)inq_buf,
711                      /* inq_len */ SHORT_INQUIRY_LENGTH,
712                      /* evpd */ 0,
713                      /* page_code */ 0,
714                      /* sense_len */ SSD_FULL_SIZE,
715                      /* timeout */ timeout ? timeout : 5000);
716
717         /* Disable freezing the device queue */
718         ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
719
720         if (arglist & CAM_ARG_ERR_RECOVER)
721                 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
722
723         if (cam_send_ccb(device, ccb) < 0) {
724                 perror("error sending SCSI inquiry");
725
726                 if (arglist & CAM_ARG_VERBOSE) {
727                         cam_error_print(device, ccb, CAM_ESF_ALL,
728                                         CAM_EPF_ALL, stderr);
729                 }
730
731                 cam_freeccb(ccb);
732                 return(1);
733         }
734
735         if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
736                 error = 1;
737
738                 if (arglist & CAM_ARG_VERBOSE) {
739                         cam_error_print(device, ccb, CAM_ESF_ALL,
740                                         CAM_EPF_ALL, stderr);
741                 }
742         }
743
744         cam_freeccb(ccb);
745
746         if (error != 0) {
747                 free(inq_buf);
748                 return(error);
749         }
750
751         fprintf(stdout, "%s%d: ", device->device_name,
752                 device->dev_unit_num);
753         scsi_print_inquiry(inq_buf);
754
755         free(inq_buf);
756
757         return(0);
758 }
759
760 static int
761 scsiserial(struct cam_device *device, int retry_count, int timeout)
762 {
763         union ccb *ccb;
764         struct scsi_vpd_unit_serial_number *serial_buf;
765         char serial_num[SVPD_SERIAL_NUM_SIZE + 1];
766         int error = 0;
767
768         ccb = cam_getccb(device);
769
770         if (ccb == NULL) {
771                 warnx("couldn't allocate CCB");
772                 return(1);
773         }
774
775         /* cam_getccb cleans up the header, caller has to zero the payload */
776         bzero(&(&ccb->ccb_h)[1],
777               sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
778
779         serial_buf = (struct scsi_vpd_unit_serial_number *)
780                 malloc(sizeof(*serial_buf));
781
782         if (serial_buf == NULL) {
783                 cam_freeccb(ccb);
784                 warnx("can't malloc memory for serial number");
785                 return(1);
786         }
787
788         scsi_inquiry(&ccb->csio,
789                      /*retries*/ retry_count,
790                      /*cbfcnp*/ NULL,
791                      /* tag_action */ MSG_SIMPLE_Q_TAG,
792                      /* inq_buf */ (u_int8_t *)serial_buf,
793                      /* inq_len */ sizeof(*serial_buf),
794                      /* evpd */ 1,
795                      /* page_code */ SVPD_UNIT_SERIAL_NUMBER,
796                      /* sense_len */ SSD_FULL_SIZE,
797                      /* timeout */ timeout ? timeout : 5000);
798
799         /* Disable freezing the device queue */
800         ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
801
802         if (arglist & CAM_ARG_ERR_RECOVER)
803                 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
804
805         if (cam_send_ccb(device, ccb) < 0) {
806                 warn("error getting serial number");
807
808                 if (arglist & CAM_ARG_VERBOSE) {
809                         cam_error_print(device, ccb, CAM_ESF_ALL,
810                                         CAM_EPF_ALL, stderr);
811                 }
812
813                 cam_freeccb(ccb);
814                 free(serial_buf);
815                 return(1);
816         }
817
818         if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
819                 error = 1;
820
821                 if (arglist & CAM_ARG_VERBOSE) {
822                         cam_error_print(device, ccb, CAM_ESF_ALL,
823                                         CAM_EPF_ALL, stderr);
824                 }
825         }
826
827         cam_freeccb(ccb);
828
829         if (error != 0) {
830                 free(serial_buf);
831                 return(error);
832         }
833
834         bcopy(serial_buf->serial_num, serial_num, serial_buf->length);
835         serial_num[serial_buf->length] = '\0';
836
837         if ((arglist & CAM_ARG_GET_STDINQ)
838          || (arglist & CAM_ARG_GET_XFERRATE))
839                 fprintf(stdout, "%s%d: Serial Number ",
840                         device->device_name, device->dev_unit_num);
841
842         fprintf(stdout, "%.60s\n", serial_num);
843
844         free(serial_buf);
845
846         return(0);
847 }
848
849 static int
850 scsixferrate(struct cam_device *device)
851 {
852         u_int32_t freq = 0;
853         u_int32_t speed = 0;
854         union ccb *ccb;
855         u_int mb;
856         int retval = 0;
857
858         ccb = cam_getccb(device);
859
860         if (ccb == NULL) {
861                 warnx("couldn't allocate CCB");
862                 return(1);
863         }
864
865         bzero(&(&ccb->ccb_h)[1],
866               sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
867
868         ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
869         ccb->cts.type = CTS_TYPE_CURRENT_SETTINGS;
870
871         if (((retval = cam_send_ccb(device, ccb)) < 0)
872          || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
873                 const char error_string[] = "error getting transfer settings";
874
875                 if (retval < 0)
876                         warn(error_string);
877                 else
878                         warnx(error_string);
879
880                 if (arglist & CAM_ARG_VERBOSE)
881                         cam_error_print(device, ccb, CAM_ESF_ALL,
882                                         CAM_EPF_ALL, stderr);
883
884                 retval = 1;
885
886                 goto xferrate_bailout;
887
888         }
889
890         if (ccb->cts.transport == XPORT_SPI) {
891                 struct ccb_trans_settings_spi *spi =
892                     &ccb->cts.xport_specific.spi;
893
894                 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
895                         freq = scsi_calc_syncsrate(spi->sync_period);
896                         speed = freq;
897                 }
898
899                 fprintf(stdout, "%s%d: ", device->device_name,
900                         device->dev_unit_num);
901
902                 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
903                         speed *= (0x01 << spi->bus_width);
904                 }
905
906                 mb = speed / 1000;
907
908                 if (mb > 0) 
909                         fprintf(stdout, "%d.%03dMB/s transfers ",
910                                 mb, speed % 1000);
911                 else
912                         fprintf(stdout, "%dKB/s transfers ",
913                                 speed);
914
915                 if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
916                  && (spi->sync_offset != 0))
917                         fprintf(stdout, "(%d.%03dMHz, offset %d", freq / 1000,
918                                 freq % 1000, spi->sync_offset);
919
920                 if (((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
921                  && (spi->bus_width > 0)) {
922                         if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
923                          && (spi->sync_offset != 0)) {
924                                 fprintf(stdout, ", ");
925                         } else {
926                                 fprintf(stdout, " (");
927                         }
928                         fprintf(stdout, "%dbit)", 8 * (0x01 << spi->bus_width));
929                 } else if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
930                  && (spi->sync_offset != 0)) {
931                         fprintf(stdout, ")");
932                 }
933         } else {
934                 struct ccb_pathinq cpi;
935
936                 retval = get_cpi(device, &cpi);
937
938                 if (retval != 0)
939                         goto xferrate_bailout;
940
941                 speed = cpi.base_transfer_speed;
942                 freq = 0;
943
944                 mb = speed / 1000;
945
946                 if (mb > 0) 
947                         fprintf(stdout, "%d.%03dMB/s transfers ",
948                                 mb, speed % 1000);
949                 else
950                         fprintf(stdout, "%dKB/s transfers ",
951                                 speed);
952         }
953
954         if (ccb->cts.protocol == PROTO_SCSI) {
955                 struct ccb_trans_settings_scsi *scsi =
956                     &ccb->cts.proto_specific.scsi;
957                 if (scsi->valid & CTS_SCSI_VALID_TQ) {
958                         if (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) {
959                                 fprintf(stdout, ", Command Queueing Enabled");
960                         }
961                 }
962         }
963
964         fprintf(stdout, "\n");
965
966 xferrate_bailout:
967
968         cam_freeccb(ccb);
969
970         return(retval);
971 }
972 #endif /* MINIMALISTIC */
973
974 /*
975  * Parse out a bus, or a bus, target and lun in the following
976  * format:
977  * bus
978  * bus:target
979  * bus:target:lun
980  *
981  * Returns the number of parsed components, or 0.
982  */
983 static int
984 parse_btl(char *tstr, int *bus, int *target, int *lun, cam_argmask *arglst)
985 {
986         char *tmpstr;
987         int convs = 0;
988
989         while (isspace(*tstr) && (*tstr != '\0'))
990                 tstr++;
991
992         tmpstr = (char *)strtok(tstr, ":");
993         if ((tmpstr != NULL) && (*tmpstr != '\0')) {
994                 *bus = strtol(tmpstr, NULL, 0);
995                 *arglst |= CAM_ARG_BUS;
996                 convs++;
997                 tmpstr = (char *)strtok(NULL, ":");
998                 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
999                         *target = strtol(tmpstr, NULL, 0);
1000                         *arglst |= CAM_ARG_TARGET;
1001                         convs++;
1002                         tmpstr = (char *)strtok(NULL, ":");
1003                         if ((tmpstr != NULL) && (*tmpstr != '\0')) {
1004                                 *lun = strtol(tmpstr, NULL, 0);
1005                                 *arglst |= CAM_ARG_LUN;
1006                                 convs++;
1007                         }
1008                 }
1009         }
1010
1011         return convs;
1012 }
1013
1014 static int
1015 dorescan_or_reset(int argc, char **argv, int rescan)
1016 {
1017         static const char must[] =
1018                 "you must specify \"all\", a bus, or a bus:target:lun to %s";
1019         int rv, error = 0;
1020         int bus = -1, target = -1, lun = -1;
1021         char *tstr;
1022
1023         if (argc < 3) {
1024                 warnx(must, rescan? "rescan" : "reset");
1025                 return(1);
1026         }
1027
1028         tstr = argv[optind];
1029         while (isspace(*tstr) && (*tstr != '\0'))
1030                 tstr++;
1031         if (strncasecmp(tstr, "all", strlen("all")) == 0)
1032                 arglist |= CAM_ARG_BUS;
1033         else {
1034                 rv = parse_btl(argv[optind], &bus, &target, &lun, &arglist);
1035                 if (rv != 1 && rv != 3) {
1036                         warnx(must, rescan? "rescan" : "reset");
1037                         return(1);
1038                 }
1039         }
1040
1041         if ((arglist & CAM_ARG_BUS)
1042             && (arglist & CAM_ARG_TARGET)
1043             && (arglist & CAM_ARG_LUN))
1044                 error = scanlun_or_reset_dev(bus, target, lun, rescan);
1045         else
1046                 error = rescan_or_reset_bus(bus, rescan);
1047
1048         return(error);
1049 }
1050
1051 static int
1052 rescan_or_reset_bus(int bus, int rescan)
1053 {
1054         union ccb ccb, matchccb;
1055         int fd, retval;
1056         int bufsize;
1057
1058         retval = 0;
1059
1060         if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1061                 warnx("error opening transport layer device %s", XPT_DEVICE);
1062                 warn("%s", XPT_DEVICE);
1063                 return(1);
1064         }
1065
1066         if (bus != -1) {
1067                 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS : XPT_RESET_BUS;
1068                 ccb.ccb_h.path_id = bus;
1069                 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1070                 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1071                 ccb.crcn.flags = CAM_FLAG_NONE;
1072
1073                 /* run this at a low priority */
1074                 ccb.ccb_h.pinfo.priority = 5;
1075
1076                 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1077                         warn("CAMIOCOMMAND ioctl failed");
1078                         close(fd);
1079                         return(1);
1080                 }
1081
1082                 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1083                         fprintf(stdout, "%s of bus %d was successful\n",
1084                             rescan ? "Re-scan" : "Reset", bus);
1085                 } else {
1086                         fprintf(stdout, "%s of bus %d returned error %#x\n",
1087                                 rescan ? "Re-scan" : "Reset", bus,
1088                                 ccb.ccb_h.status & CAM_STATUS_MASK);
1089                         retval = 1;
1090                 }
1091
1092                 close(fd);
1093                 return(retval);
1094
1095         }
1096
1097
1098         /*
1099          * The right way to handle this is to modify the xpt so that it can
1100          * handle a wildcarded bus in a rescan or reset CCB.  At the moment
1101          * that isn't implemented, so instead we enumerate the busses and
1102          * send the rescan or reset to those busses in the case where the
1103          * given bus is -1 (wildcard).  We don't send a rescan or reset
1104          * to the xpt bus; sending a rescan to the xpt bus is effectively a
1105          * no-op, sending a rescan to the xpt bus would result in a status of
1106          * CAM_REQ_INVALID.
1107          */
1108         bzero(&(&matchccb.ccb_h)[1],
1109               sizeof(struct ccb_dev_match) - sizeof(struct ccb_hdr));
1110         matchccb.ccb_h.func_code = XPT_DEV_MATCH;
1111         bufsize = sizeof(struct dev_match_result) * 20;
1112         matchccb.cdm.match_buf_len = bufsize;
1113         matchccb.cdm.matches=(struct dev_match_result *)malloc(bufsize);
1114         if (matchccb.cdm.matches == NULL) {
1115                 warnx("can't malloc memory for matches");
1116                 retval = 1;
1117                 goto bailout;
1118         }
1119         matchccb.cdm.num_matches = 0;
1120
1121         matchccb.cdm.num_patterns = 1;
1122         matchccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
1123
1124         matchccb.cdm.patterns = (struct dev_match_pattern *)malloc(
1125                 matchccb.cdm.pattern_buf_len);
1126         if (matchccb.cdm.patterns == NULL) {
1127                 warnx("can't malloc memory for patterns");
1128                 retval = 1;
1129                 goto bailout;
1130         }
1131         matchccb.cdm.patterns[0].type = DEV_MATCH_BUS;
1132         matchccb.cdm.patterns[0].pattern.bus_pattern.flags = BUS_MATCH_ANY;
1133
1134         do {
1135                 unsigned int i;
1136
1137                 if (ioctl(fd, CAMIOCOMMAND, &matchccb) == -1) {
1138                         warn("CAMIOCOMMAND ioctl failed");
1139                         retval = 1;
1140                         goto bailout;
1141                 }
1142
1143                 if ((matchccb.ccb_h.status != CAM_REQ_CMP)
1144                  || ((matchccb.cdm.status != CAM_DEV_MATCH_LAST)
1145                    && (matchccb.cdm.status != CAM_DEV_MATCH_MORE))) {
1146                         warnx("got CAM error %#x, CDM error %d\n",
1147                               matchccb.ccb_h.status, matchccb.cdm.status);
1148                         retval = 1;
1149                         goto bailout;
1150                 }
1151
1152                 for (i = 0; i < matchccb.cdm.num_matches; i++) {
1153                         struct bus_match_result *bus_result;
1154
1155                         /* This shouldn't happen. */
1156                         if (matchccb.cdm.matches[i].type != DEV_MATCH_BUS)
1157                                 continue;
1158
1159                         bus_result = &matchccb.cdm.matches[i].result.bus_result;
1160
1161                         /*
1162                          * We don't want to rescan or reset the xpt bus.
1163                          * See above.
1164                          */
1165                         if ((int)bus_result->path_id == -1)
1166                                 continue;
1167
1168                         ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS :
1169                                                        XPT_RESET_BUS;
1170                         ccb.ccb_h.path_id = bus_result->path_id;
1171                         ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1172                         ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1173                         ccb.crcn.flags = CAM_FLAG_NONE;
1174
1175                         /* run this at a low priority */
1176                         ccb.ccb_h.pinfo.priority = 5;
1177
1178                         if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1179                                 warn("CAMIOCOMMAND ioctl failed");
1180                                 retval = 1;
1181                                 goto bailout;
1182                         }
1183
1184                         if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==CAM_REQ_CMP){
1185                                 fprintf(stdout, "%s of bus %d was successful\n",
1186                                         rescan? "Re-scan" : "Reset",
1187                                         bus_result->path_id);
1188                         } else {
1189                                 /*
1190                                  * Don't bail out just yet, maybe the other
1191                                  * rescan or reset commands will complete
1192                                  * successfully.
1193                                  */
1194                                 fprintf(stderr, "%s of bus %d returned error "
1195                                         "%#x\n", rescan? "Re-scan" : "Reset",
1196                                         bus_result->path_id,
1197                                         ccb.ccb_h.status & CAM_STATUS_MASK);
1198                                 retval = 1;
1199                         }
1200                 }
1201         } while ((matchccb.ccb_h.status == CAM_REQ_CMP)
1202                  && (matchccb.cdm.status == CAM_DEV_MATCH_MORE));
1203
1204 bailout:
1205
1206         if (fd != -1)
1207                 close(fd);
1208
1209         if (matchccb.cdm.patterns != NULL)
1210                 free(matchccb.cdm.patterns);
1211         if (matchccb.cdm.matches != NULL)
1212                 free(matchccb.cdm.matches);
1213
1214         return(retval);
1215 }
1216
1217 static int
1218 scanlun_or_reset_dev(int bus, int target, int lun, int scan)
1219 {
1220         union ccb ccb;
1221         struct cam_device *device;
1222         int fd;
1223
1224         device = NULL;
1225
1226         if (bus < 0) {
1227                 warnx("invalid bus number %d", bus);
1228                 return(1);
1229         }
1230
1231         if (target < 0) {
1232                 warnx("invalid target number %d", target);
1233                 return(1);
1234         }
1235
1236         if (lun < 0) {
1237                 warnx("invalid lun number %d", lun);
1238                 return(1);
1239         }
1240
1241         fd = -1;
1242
1243         bzero(&ccb, sizeof(union ccb));
1244
1245         if (scan) {
1246                 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1247                         warnx("error opening transport layer device %s\n",
1248                             XPT_DEVICE);
1249                         warn("%s", XPT_DEVICE);
1250                         return(1);
1251                 }
1252         } else {
1253                 device = cam_open_btl(bus, target, lun, O_RDWR, NULL);
1254                 if (device == NULL) {
1255                         warnx("%s", cam_errbuf);
1256                         return(1);
1257                 }
1258         }
1259
1260         ccb.ccb_h.func_code = (scan)? XPT_SCAN_LUN : XPT_RESET_DEV;
1261         ccb.ccb_h.path_id = bus;
1262         ccb.ccb_h.target_id = target;
1263         ccb.ccb_h.target_lun = lun;
1264         ccb.ccb_h.timeout = 5000;
1265         ccb.crcn.flags = CAM_FLAG_NONE;
1266
1267         /* run this at a low priority */
1268         ccb.ccb_h.pinfo.priority = 5;
1269
1270         if (scan) {
1271                 if (ioctl(fd, CAMIOCOMMAND, &ccb) < 0) {
1272                         warn("CAMIOCOMMAND ioctl failed");
1273                         close(fd);
1274                         return(1);
1275                 }
1276         } else {
1277                 if (cam_send_ccb(device, &ccb) < 0) {
1278                         warn("error sending XPT_RESET_DEV CCB");
1279                         cam_close_device(device);
1280                         return(1);
1281                 }
1282         }
1283
1284         if (scan)
1285                 close(fd);
1286         else
1287                 cam_close_device(device);
1288
1289         /*
1290          * An error code of CAM_BDR_SENT is normal for a BDR request.
1291          */
1292         if (((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1293          || ((!scan)
1294           && ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_BDR_SENT))) {
1295                 fprintf(stdout, "%s of %d:%d:%d was successful\n",
1296                     scan? "Re-scan" : "Reset", bus, target, lun);
1297                 return(0);
1298         } else {
1299                 fprintf(stdout, "%s of %d:%d:%d returned error %#x\n",
1300                     scan? "Re-scan" : "Reset", bus, target, lun,
1301                     ccb.ccb_h.status & CAM_STATUS_MASK);
1302                 return(1);
1303         }
1304 }
1305
1306 #ifndef MINIMALISTIC
1307 static int
1308 readdefects(struct cam_device *device, int argc, char **argv,
1309             char *combinedopt, int retry_count, int timeout)
1310 {
1311         union ccb *ccb = NULL;
1312         struct scsi_read_defect_data_10 *rdd_cdb;
1313         u_int8_t *defect_list = NULL;
1314         u_int32_t dlist_length = 65000;
1315         u_int32_t returned_length = 0;
1316         u_int32_t num_returned = 0;
1317         u_int8_t returned_format;
1318         unsigned int i;
1319         int c, error = 0;
1320         int lists_specified = 0;
1321
1322         while ((c = getopt(argc, argv, combinedopt)) != -1) {
1323                 switch(c){
1324                 case 'f':
1325                 {
1326                         char *tstr;
1327                         tstr = optarg;
1328                         while (isspace(*tstr) && (*tstr != '\0'))
1329                                 tstr++;
1330                         if (strcmp(tstr, "block") == 0)
1331                                 arglist |= CAM_ARG_FORMAT_BLOCK;
1332                         else if (strcmp(tstr, "bfi") == 0)
1333                                 arglist |= CAM_ARG_FORMAT_BFI;
1334                         else if (strcmp(tstr, "phys") == 0)
1335                                 arglist |= CAM_ARG_FORMAT_PHYS;
1336                         else {
1337                                 error = 1;
1338                                 warnx("invalid defect format %s", tstr);
1339                                 goto defect_bailout;
1340                         }
1341                         break;
1342                 }
1343                 case 'G':
1344                         arglist |= CAM_ARG_GLIST;
1345                         break;
1346                 case 'P':
1347                         arglist |= CAM_ARG_PLIST;
1348                         break;
1349                 default:
1350                         break;
1351                 }
1352         }
1353
1354         ccb = cam_getccb(device);
1355
1356         /*
1357          * Hopefully 65000 bytes is enough to hold the defect list.  If it
1358          * isn't, the disk is probably dead already.  We'd have to go with
1359          * 12 byte command (i.e. alloc_length is 32 bits instead of 16)
1360          * to hold them all.
1361          */
1362         defect_list = malloc(dlist_length);
1363         if (defect_list == NULL) {
1364                 warnx("can't malloc memory for defect list");
1365                 error = 1;
1366                 goto defect_bailout;
1367         }
1368
1369         rdd_cdb =(struct scsi_read_defect_data_10 *)&ccb->csio.cdb_io.cdb_bytes;
1370
1371         /*
1372          * cam_getccb() zeros the CCB header only.  So we need to zero the
1373          * payload portion of the ccb.
1374          */
1375         bzero(&(&ccb->ccb_h)[1],
1376               sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1377
1378         cam_fill_csio(&ccb->csio,
1379                       /*retries*/ retry_count,
1380                       /*cbfcnp*/ NULL,
1381                       /*flags*/ CAM_DIR_IN | ((arglist & CAM_ARG_ERR_RECOVER) ?
1382                                               CAM_PASS_ERR_RECOVER : 0),
1383                       /*tag_action*/ MSG_SIMPLE_Q_TAG,
1384                       /*data_ptr*/ defect_list,
1385                       /*dxfer_len*/ dlist_length,
1386                       /*sense_len*/ SSD_FULL_SIZE,
1387                       /*cdb_len*/ sizeof(struct scsi_read_defect_data_10),
1388                       /*timeout*/ timeout ? timeout : 5000);
1389
1390         rdd_cdb->opcode = READ_DEFECT_DATA_10;
1391         if (arglist & CAM_ARG_FORMAT_BLOCK)
1392                 rdd_cdb->format = SRDD10_BLOCK_FORMAT;
1393         else if (arglist & CAM_ARG_FORMAT_BFI)
1394                 rdd_cdb->format = SRDD10_BYTES_FROM_INDEX_FORMAT;
1395         else if (arglist & CAM_ARG_FORMAT_PHYS)
1396                 rdd_cdb->format = SRDD10_PHYSICAL_SECTOR_FORMAT;
1397         else {
1398                 error = 1;
1399                 warnx("no defect list format specified");
1400                 goto defect_bailout;
1401         }
1402         if (arglist & CAM_ARG_PLIST) {
1403                 rdd_cdb->format |= SRDD10_PLIST;
1404                 lists_specified++;
1405         }
1406
1407         if (arglist & CAM_ARG_GLIST) {
1408                 rdd_cdb->format |= SRDD10_GLIST;
1409                 lists_specified++;
1410         }
1411
1412         scsi_ulto2b(dlist_length, rdd_cdb->alloc_length);
1413
1414         /* Disable freezing the device queue */
1415         ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1416
1417         if (cam_send_ccb(device, ccb) < 0) {
1418                 perror("error reading defect list");
1419
1420                 if (arglist & CAM_ARG_VERBOSE) {
1421                         cam_error_print(device, ccb, CAM_ESF_ALL,
1422                                         CAM_EPF_ALL, stderr);
1423                 }
1424
1425                 error = 1;
1426                 goto defect_bailout;
1427         }
1428
1429         returned_length = scsi_2btoul(((struct
1430                 scsi_read_defect_data_hdr_10 *)defect_list)->length);
1431
1432         returned_format = ((struct scsi_read_defect_data_hdr_10 *)
1433                         defect_list)->format;
1434
1435         if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR)
1436          && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1437          && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
1438                 struct scsi_sense_data *sense;
1439                 int error_code, sense_key, asc, ascq;
1440
1441                 sense = &ccb->csio.sense_data;
1442                 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
1443
1444                 /*
1445                  * According to the SCSI spec, if the disk doesn't support
1446                  * the requested format, it will generally return a sense
1447                  * key of RECOVERED ERROR, and an additional sense code
1448                  * of "DEFECT LIST NOT FOUND".  So, we check for that, and
1449                  * also check to make sure that the returned length is
1450                  * greater than 0, and then print out whatever format the
1451                  * disk gave us.
1452                  */
1453                 if ((sense_key == SSD_KEY_RECOVERED_ERROR)
1454                  && (asc == 0x1c) && (ascq == 0x00)
1455                  && (returned_length > 0)) {
1456                         warnx("requested defect format not available");
1457                         switch(returned_format & SRDDH10_DLIST_FORMAT_MASK) {
1458                         case SRDD10_BLOCK_FORMAT:
1459                                 warnx("Device returned block format");
1460                                 break;
1461                         case SRDD10_BYTES_FROM_INDEX_FORMAT:
1462                                 warnx("Device returned bytes from index"
1463                                       " format");
1464                                 break;
1465                         case SRDD10_PHYSICAL_SECTOR_FORMAT:
1466                                 warnx("Device returned physical sector format");
1467                                 break;
1468                         default:
1469                                 error = 1;
1470                                 warnx("Device returned unknown defect"
1471                                      " data format %#x", returned_format);
1472                                 goto defect_bailout;
1473                                 break; /* NOTREACHED */
1474                         }
1475                 } else {
1476                         error = 1;
1477                         warnx("Error returned from read defect data command");
1478                         if (arglist & CAM_ARG_VERBOSE)
1479                                 cam_error_print(device, ccb, CAM_ESF_ALL,
1480                                                 CAM_EPF_ALL, stderr);
1481                         goto defect_bailout;
1482                 }
1483         } else if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1484                 error = 1;
1485                 warnx("Error returned from read defect data command");
1486                 if (arglist & CAM_ARG_VERBOSE)
1487                         cam_error_print(device, ccb, CAM_ESF_ALL,
1488                                         CAM_EPF_ALL, stderr);
1489                 goto defect_bailout;
1490         }
1491
1492         /*
1493          * XXX KDM  I should probably clean up the printout format for the
1494          * disk defects. 
1495          */
1496         switch (returned_format & SRDDH10_DLIST_FORMAT_MASK){
1497                 case SRDDH10_PHYSICAL_SECTOR_FORMAT:
1498                 {
1499                         struct scsi_defect_desc_phys_sector *dlist;
1500
1501                         dlist = (struct scsi_defect_desc_phys_sector *)
1502                                 (defect_list +
1503                                 sizeof(struct scsi_read_defect_data_hdr_10));
1504
1505                         num_returned = returned_length /
1506                                 sizeof(struct scsi_defect_desc_phys_sector);
1507
1508                         fprintf(stderr, "Got %d defect", num_returned);
1509
1510                         if ((lists_specified == 0) || (num_returned == 0)) {
1511                                 fprintf(stderr, "s.\n");
1512                                 break;
1513                         } else if (num_returned == 1)
1514                                 fprintf(stderr, ":\n");
1515                         else
1516                                 fprintf(stderr, "s:\n");
1517
1518                         for (i = 0; i < num_returned; i++) {
1519                                 fprintf(stdout, "%d:%d:%d\n",
1520                                         scsi_3btoul(dlist[i].cylinder),
1521                                         dlist[i].head,
1522                                         scsi_4btoul(dlist[i].sector));
1523                         }
1524                         break;
1525                 }
1526                 case SRDDH10_BYTES_FROM_INDEX_FORMAT:
1527                 {
1528                         struct scsi_defect_desc_bytes_from_index *dlist;
1529
1530                         dlist = (struct scsi_defect_desc_bytes_from_index *)
1531                                 (defect_list +
1532                                 sizeof(struct scsi_read_defect_data_hdr_10));
1533
1534                         num_returned = returned_length /
1535                               sizeof(struct scsi_defect_desc_bytes_from_index);
1536
1537                         fprintf(stderr, "Got %d defect", num_returned);
1538
1539                         if ((lists_specified == 0) || (num_returned == 0)) {
1540                                 fprintf(stderr, "s.\n");
1541                                 break;
1542                         } else if (num_returned == 1)
1543                                 fprintf(stderr, ":\n");
1544                         else
1545                                 fprintf(stderr, "s:\n");
1546
1547                         for (i = 0; i < num_returned; i++) {
1548                                 fprintf(stdout, "%d:%d:%d\n",
1549                                         scsi_3btoul(dlist[i].cylinder),
1550                                         dlist[i].head,
1551                                         scsi_4btoul(dlist[i].bytes_from_index));
1552                         }
1553                         break;
1554                 }
1555                 case SRDDH10_BLOCK_FORMAT:
1556                 {
1557                         struct scsi_defect_desc_block *dlist;
1558
1559                         dlist = (struct scsi_defect_desc_block *)(defect_list +
1560                                 sizeof(struct scsi_read_defect_data_hdr_10));
1561
1562                         num_returned = returned_length /
1563                               sizeof(struct scsi_defect_desc_block);
1564
1565                         fprintf(stderr, "Got %d defect", num_returned);
1566
1567                         if ((lists_specified == 0) || (num_returned == 0)) {
1568                                 fprintf(stderr, "s.\n");
1569                                 break;
1570                         } else if (num_returned == 1)
1571                                 fprintf(stderr, ":\n");
1572                         else
1573                                 fprintf(stderr, "s:\n");
1574
1575                         for (i = 0; i < num_returned; i++)
1576                                 fprintf(stdout, "%u\n",
1577                                         scsi_4btoul(dlist[i].address));
1578                         break;
1579                 }
1580                 default:
1581                         fprintf(stderr, "Unknown defect format %d\n",
1582                                 returned_format & SRDDH10_DLIST_FORMAT_MASK);
1583                         error = 1;
1584                         break;
1585         }
1586 defect_bailout:
1587
1588         if (defect_list != NULL)
1589                 free(defect_list);
1590
1591         if (ccb != NULL)
1592                 cam_freeccb(ccb);
1593
1594         return(error);
1595 }
1596 #endif /* MINIMALISTIC */
1597
1598 #if 0
1599 void
1600 reassignblocks(struct cam_device *device, u_int32_t *blocks, int num_blocks)
1601 {
1602         union ccb *ccb;
1603         
1604         ccb = cam_getccb(device);
1605
1606         cam_freeccb(ccb);
1607 }
1608 #endif
1609
1610 #ifndef MINIMALISTIC
1611 void
1612 mode_sense(struct cam_device *device, int mode_page, int page_control,
1613            int dbd, int retry_count, int timeout, u_int8_t *data, int datalen)
1614 {
1615         union ccb *ccb;
1616         int retval;
1617
1618         ccb = cam_getccb(device);
1619
1620         if (ccb == NULL)
1621                 errx(1, "mode_sense: couldn't allocate CCB");
1622
1623         bzero(&(&ccb->ccb_h)[1],
1624               sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1625
1626         scsi_mode_sense(&ccb->csio,
1627                         /* retries */ retry_count,
1628                         /* cbfcnp */ NULL,
1629                         /* tag_action */ MSG_SIMPLE_Q_TAG,
1630                         /* dbd */ dbd,
1631                         /* page_code */ page_control << 6,
1632                         /* page */ mode_page,
1633                         /* param_buf */ data,
1634                         /* param_len */ datalen,
1635                         /* sense_len */ SSD_FULL_SIZE,
1636                         /* timeout */ timeout ? timeout : 5000);
1637
1638         if (arglist & CAM_ARG_ERR_RECOVER)
1639                 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1640
1641         /* Disable freezing the device queue */
1642         ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1643
1644         if (((retval = cam_send_ccb(device, ccb)) < 0)
1645          || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1646                 if (arglist & CAM_ARG_VERBOSE) {
1647                         cam_error_print(device, ccb, CAM_ESF_ALL,
1648                                         CAM_EPF_ALL, stderr);
1649                 }
1650                 cam_freeccb(ccb);
1651                 cam_close_device(device);
1652                 if (retval < 0)
1653                         err(1, "error sending mode sense command");
1654                 else
1655                         errx(1, "error sending mode sense command");
1656         }
1657
1658         cam_freeccb(ccb);
1659 }
1660
1661 void
1662 mode_select(struct cam_device *device, int save_pages, int retry_count,
1663            int timeout, u_int8_t *data, int datalen)
1664 {
1665         union ccb *ccb;
1666         int retval;
1667
1668         ccb = cam_getccb(device);
1669
1670         if (ccb == NULL)
1671                 errx(1, "mode_select: couldn't allocate CCB");
1672
1673         bzero(&(&ccb->ccb_h)[1],
1674               sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1675
1676         scsi_mode_select(&ccb->csio,
1677                          /* retries */ retry_count,
1678                          /* cbfcnp */ NULL,
1679                          /* tag_action */ MSG_SIMPLE_Q_TAG,
1680                          /* scsi_page_fmt */ 1,
1681                          /* save_pages */ save_pages,
1682                          /* param_buf */ data,
1683                          /* param_len */ datalen,
1684                          /* sense_len */ SSD_FULL_SIZE,
1685                          /* timeout */ timeout ? timeout : 5000);
1686
1687         if (arglist & CAM_ARG_ERR_RECOVER)
1688                 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1689
1690         /* Disable freezing the device queue */
1691         ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1692
1693         if (((retval = cam_send_ccb(device, ccb)) < 0)
1694          || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1695                 if (arglist & CAM_ARG_VERBOSE) {
1696                         cam_error_print(device, ccb, CAM_ESF_ALL,
1697                                         CAM_EPF_ALL, stderr);
1698                 }
1699                 cam_freeccb(ccb);
1700                 cam_close_device(device);
1701
1702                 if (retval < 0)
1703                         err(1, "error sending mode select command");
1704                 else
1705                         errx(1, "error sending mode select command");
1706                 
1707         }
1708
1709         cam_freeccb(ccb);
1710 }
1711
1712 void
1713 modepage(struct cam_device *device, int argc, char **argv, char *combinedopt,
1714          int retry_count, int timeout)
1715 {
1716         int c, mode_page = -1, page_control = 0;
1717         int binary = 0, list = 0;
1718
1719         while ((c = getopt(argc, argv, combinedopt)) != -1) {
1720                 switch(c) {
1721                 case 'b':
1722                         binary = 1;
1723                         break;
1724                 case 'd':
1725                         arglist |= CAM_ARG_DBD;
1726                         break;
1727                 case 'e':
1728                         arglist |= CAM_ARG_MODE_EDIT;
1729                         break;
1730                 case 'l':
1731                         list = 1;
1732                         break;
1733                 case 'm':
1734                         mode_page = strtol(optarg, NULL, 0);
1735                         if (mode_page < 0)
1736                                 errx(1, "invalid mode page %d", mode_page);
1737                         break;
1738                 case 'P':
1739                         page_control = strtol(optarg, NULL, 0);
1740                         if ((page_control < 0) || (page_control > 3))
1741                                 errx(1, "invalid page control field %d",
1742                                      page_control);
1743                         arglist |= CAM_ARG_PAGE_CNTL;
1744                         break;
1745                 default:
1746                         break;
1747                 }
1748         }
1749
1750         if (mode_page == -1 && list == 0)
1751                 errx(1, "you must specify a mode page!");
1752
1753         if (list) {
1754                 mode_list(device, page_control, arglist & CAM_ARG_DBD,
1755                     retry_count, timeout);
1756         } else {
1757                 mode_edit(device, mode_page, page_control,
1758                     arglist & CAM_ARG_DBD, arglist & CAM_ARG_MODE_EDIT, binary,
1759                     retry_count, timeout);
1760         }
1761 }
1762
1763 static int
1764 scsicmd(struct cam_device *device, int argc, char **argv, char *combinedopt,
1765         int retry_count, int timeout)
1766 {
1767         union ccb *ccb;
1768         u_int32_t flags = CAM_DIR_NONE;
1769         u_int8_t *data_ptr = NULL;
1770         u_int8_t cdb[20];
1771         struct get_hook hook;
1772         int c, data_bytes = 0;
1773         int cdb_len = 0;
1774         char *datastr = NULL, *tstr;
1775         int error = 0;
1776         int fd_data = 0;
1777         int retval;
1778
1779         ccb = cam_getccb(device);
1780
1781         if (ccb == NULL) {
1782                 warnx("scsicmd: error allocating ccb");
1783                 return(1);
1784         }
1785
1786         bzero(&(&ccb->ccb_h)[1],
1787               sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1788
1789         while ((c = getopt(argc, argv, combinedopt)) != -1) {
1790                 switch(c) {
1791                 case 'c':
1792                         tstr = optarg;
1793                         while (isspace(*tstr) && (*tstr != '\0'))
1794                                 tstr++;
1795                         hook.argc = argc - optind;
1796                         hook.argv = argv + optind;
1797                         hook.got = 0;
1798                         cdb_len = buff_encode_visit(cdb, sizeof(cdb), tstr,
1799                                                     iget, &hook);
1800                         /*
1801                          * Increment optind by the number of arguments the
1802                          * encoding routine processed.  After each call to
1803                          * getopt(3), optind points to the argument that
1804                          * getopt should process _next_.  In this case,
1805                          * that means it points to the first command string
1806                          * argument, if there is one.  Once we increment
1807                          * this, it should point to either the next command
1808                          * line argument, or it should be past the end of
1809                          * the list.
1810                          */
1811                         optind += hook.got;
1812                         break;
1813                 case 'i':
1814                         if (arglist & CAM_ARG_CMD_OUT) {
1815                                 warnx("command must either be "
1816                                       "read or write, not both");
1817                                 error = 1;
1818                                 goto scsicmd_bailout;
1819                         }
1820                         arglist |= CAM_ARG_CMD_IN;
1821                         flags = CAM_DIR_IN;
1822                         data_bytes = strtol(optarg, NULL, 0);
1823                         if (data_bytes <= 0) {
1824                                 warnx("invalid number of input bytes %d",
1825                                       data_bytes);
1826                                 error = 1;
1827                                 goto scsicmd_bailout;
1828                         }
1829                         hook.argc = argc - optind;
1830                         hook.argv = argv + optind;
1831                         hook.got = 0;
1832                         optind++;
1833                         datastr = cget(&hook, NULL);
1834                         /*
1835                          * If the user supplied "-" instead of a format, he
1836                          * wants the data to be written to stdout.
1837                          */
1838                         if ((datastr != NULL)
1839                          && (datastr[0] == '-'))
1840                                 fd_data = 1;
1841
1842                         data_ptr = (u_int8_t *)malloc(data_bytes);
1843                         if (data_ptr == NULL) {
1844                                 warnx("can't malloc memory for data_ptr");
1845                                 error = 1;
1846                                 goto scsicmd_bailout;
1847                         }
1848                         break;
1849                 case 'o':
1850                         if (arglist & CAM_ARG_CMD_IN) {
1851                                 warnx("command must either be "
1852                                       "read or write, not both");
1853                                 error = 1;      
1854                                 goto scsicmd_bailout;
1855                         }
1856                         arglist |= CAM_ARG_CMD_OUT;
1857                         flags = CAM_DIR_OUT;
1858                         data_bytes = strtol(optarg, NULL, 0);
1859                         if (data_bytes <= 0) {
1860                                 warnx("invalid number of output bytes %d",
1861                                       data_bytes);
1862                                 error = 1;
1863                                 goto scsicmd_bailout;
1864                         }
1865                         hook.argc = argc - optind;
1866                         hook.argv = argv + optind;
1867                         hook.got = 0;
1868                         datastr = cget(&hook, NULL);
1869                         data_ptr = (u_int8_t *)malloc(data_bytes);
1870                         if (data_ptr == NULL) {
1871                                 warnx("can't malloc memory for data_ptr");
1872                                 error = 1;
1873                                 goto scsicmd_bailout;
1874                         }
1875                         /*
1876                          * If the user supplied "-" instead of a format, he
1877                          * wants the data to be read from stdin.
1878                          */
1879                         if ((datastr != NULL)
1880                          && (datastr[0] == '-'))
1881                                 fd_data = 1;
1882                         else
1883                                 buff_encode_visit(data_ptr, data_bytes, datastr,
1884                                                   iget, &hook);
1885                         optind += hook.got;
1886                         break;
1887                 default:
1888                         break;
1889                 }
1890         }
1891
1892         /*
1893          * If fd_data is set, and we're writing to the device, we need to
1894          * read the data the user wants written from stdin.
1895          */
1896         if ((fd_data == 1) && (arglist & CAM_ARG_CMD_OUT)) {
1897                 ssize_t amt_read;
1898                 int amt_to_read = data_bytes;
1899                 u_int8_t *buf_ptr = data_ptr;
1900
1901                 for (amt_read = 0; amt_to_read > 0;
1902                      amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
1903                         if (amt_read == -1) {
1904                                 warn("error reading data from stdin");
1905                                 error = 1;
1906                                 goto scsicmd_bailout;
1907                         }
1908                         amt_to_read -= amt_read;
1909                         buf_ptr += amt_read;
1910                 }
1911         }
1912
1913         if (arglist & CAM_ARG_ERR_RECOVER)
1914                 flags |= CAM_PASS_ERR_RECOVER;
1915
1916         /* Disable freezing the device queue */
1917         flags |= CAM_DEV_QFRZDIS;
1918
1919         /*
1920          * This is taken from the SCSI-3 draft spec.
1921          * (T10/1157D revision 0.3)
1922          * The top 3 bits of an opcode are the group code.  The next 5 bits
1923          * are the command code.
1924          * Group 0:  six byte commands
1925          * Group 1:  ten byte commands
1926          * Group 2:  ten byte commands
1927          * Group 3:  reserved
1928          * Group 4:  sixteen byte commands
1929          * Group 5:  twelve byte commands
1930          * Group 6:  vendor specific
1931          * Group 7:  vendor specific
1932          */
1933         switch((cdb[0] >> 5) & 0x7) {
1934                 case 0:
1935                         cdb_len = 6;
1936                         break;
1937                 case 1:
1938                 case 2:
1939                         cdb_len = 10;
1940                         break;
1941                 case 3:
1942                 case 6:
1943                 case 7:
1944                         /* computed by buff_encode_visit */
1945                         break;
1946                 case 4:
1947                         cdb_len = 16;
1948                         break;
1949                 case 5:
1950                         cdb_len = 12;
1951                         break;
1952         }
1953
1954         /*
1955          * We should probably use csio_build_visit or something like that
1956          * here, but it's easier to encode arguments as you go.  The
1957          * alternative would be skipping the CDB argument and then encoding
1958          * it here, since we've got the data buffer argument by now.
1959          */
1960         bcopy(cdb, &ccb->csio.cdb_io.cdb_bytes, cdb_len);
1961
1962         cam_fill_csio(&ccb->csio,
1963                       /*retries*/ retry_count,
1964                       /*cbfcnp*/ NULL,
1965                       /*flags*/ flags,
1966                       /*tag_action*/ MSG_SIMPLE_Q_TAG,
1967                       /*data_ptr*/ data_ptr,
1968                       /*dxfer_len*/ data_bytes,
1969                       /*sense_len*/ SSD_FULL_SIZE,
1970                       /*cdb_len*/ cdb_len,
1971                       /*timeout*/ timeout ? timeout : 5000);
1972
1973         if (((retval = cam_send_ccb(device, ccb)) < 0)
1974          || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1975                 if (retval < 0)
1976                         warn("error sending command");
1977                 else
1978                         warnx("error sending command");
1979
1980                 if (arglist & CAM_ARG_VERBOSE) {
1981                         cam_error_print(device, ccb, CAM_ESF_ALL,
1982                                         CAM_EPF_ALL, stderr);
1983                 }
1984
1985                 error = 1;
1986                 goto scsicmd_bailout;
1987         }
1988
1989
1990         if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1991          && (arglist & CAM_ARG_CMD_IN)
1992          && (data_bytes > 0)) {
1993                 if (fd_data == 0) {
1994                         buff_decode_visit(data_ptr, data_bytes, datastr,
1995                                           arg_put, NULL);
1996                         fprintf(stdout, "\n");
1997                 } else {
1998                         ssize_t amt_written;
1999                         int amt_to_write = data_bytes;
2000                         u_int8_t *buf_ptr = data_ptr;
2001
2002                         for (amt_written = 0; (amt_to_write > 0) &&
2003                              (amt_written =write(1, buf_ptr,amt_to_write))> 0;){
2004                                 amt_to_write -= amt_written;
2005                                 buf_ptr += amt_written;
2006                         }
2007                         if (amt_written == -1) {
2008                                 warn("error writing data to stdout");
2009                                 error = 1;
2010                                 goto scsicmd_bailout;
2011                         } else if ((amt_written == 0)
2012                                 && (amt_to_write > 0)) {
2013                                 warnx("only wrote %u bytes out of %u",
2014                                       data_bytes - amt_to_write, data_bytes);
2015                         }
2016                 }
2017         }
2018
2019 scsicmd_bailout:
2020
2021         if ((data_bytes > 0) && (data_ptr != NULL))
2022                 free(data_ptr);
2023
2024         cam_freeccb(ccb);
2025
2026         return(error);
2027 }
2028
2029 static int
2030 camdebug(int argc, char **argv, char *combinedopt)
2031 {
2032         int c, fd;
2033         int bus = -1, target = -1, lun = -1;
2034         char *tstr, *tmpstr = NULL;
2035         union ccb ccb;
2036         int error = 0;
2037
2038         bzero(&ccb, sizeof(union ccb));
2039
2040         while ((c = getopt(argc, argv, combinedopt)) != -1) {
2041                 switch(c) {
2042                 case 'I':
2043                         arglist |= CAM_ARG_DEBUG_INFO;
2044                         ccb.cdbg.flags |= CAM_DEBUG_INFO;
2045                         break;
2046                 case 'P':
2047                         arglist |= CAM_ARG_DEBUG_PERIPH;
2048                         ccb.cdbg.flags |= CAM_DEBUG_PERIPH;
2049                         break;
2050                 case 'S':
2051                         arglist |= CAM_ARG_DEBUG_SUBTRACE;
2052                         ccb.cdbg.flags |= CAM_DEBUG_SUBTRACE;
2053                         break;
2054                 case 'T':
2055                         arglist |= CAM_ARG_DEBUG_TRACE;
2056                         ccb.cdbg.flags |= CAM_DEBUG_TRACE;
2057                         break;
2058                 case 'X':
2059                         arglist |= CAM_ARG_DEBUG_XPT;
2060                         ccb.cdbg.flags |= CAM_DEBUG_XPT;
2061                         break;
2062                 case 'c':
2063                         arglist |= CAM_ARG_DEBUG_CDB;
2064                         ccb.cdbg.flags |= CAM_DEBUG_CDB;
2065                         break;
2066                 default:
2067                         break;
2068                 }
2069         }
2070
2071         if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
2072                 warnx("error opening transport layer device %s", XPT_DEVICE);
2073                 warn("%s", XPT_DEVICE);
2074                 return(1);
2075         }
2076         argc -= optind;
2077         argv += optind;
2078
2079         if (argc <= 0) {
2080                 warnx("you must specify \"off\", \"all\" or a bus,");
2081                 warnx("bus:target, or bus:target:lun");
2082                 close(fd);
2083                 return(1);
2084         }
2085
2086         tstr = *argv;
2087
2088         while (isspace(*tstr) && (*tstr != '\0'))
2089                 tstr++;
2090
2091         if (strncmp(tstr, "off", 3) == 0) {
2092                 ccb.cdbg.flags = CAM_DEBUG_NONE;
2093                 arglist &= ~(CAM_ARG_DEBUG_INFO|CAM_ARG_DEBUG_PERIPH|
2094                              CAM_ARG_DEBUG_TRACE|CAM_ARG_DEBUG_SUBTRACE|
2095                              CAM_ARG_DEBUG_XPT);
2096         } else if (strncmp(tstr, "all", 3) != 0) {
2097                 tmpstr = (char *)strtok(tstr, ":");
2098                 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2099                         bus = strtol(tmpstr, NULL, 0);
2100                         arglist |= CAM_ARG_BUS;
2101                         tmpstr = (char *)strtok(NULL, ":");
2102                         if ((tmpstr != NULL) && (*tmpstr != '\0')){
2103                                 target = strtol(tmpstr, NULL, 0);
2104                                 arglist |= CAM_ARG_TARGET;
2105                                 tmpstr = (char *)strtok(NULL, ":");
2106                                 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2107                                         lun = strtol(tmpstr, NULL, 0);
2108                                         arglist |= CAM_ARG_LUN;
2109                                 }
2110                         }
2111                 } else {
2112                         error = 1;
2113                         warnx("you must specify \"all\", \"off\", or a bus,");
2114                         warnx("bus:target, or bus:target:lun to debug");
2115                 }
2116         }
2117         
2118         if (error == 0) {
2119
2120                 ccb.ccb_h.func_code = XPT_DEBUG;
2121                 ccb.ccb_h.path_id = bus;
2122                 ccb.ccb_h.target_id = target;
2123                 ccb.ccb_h.target_lun = lun;
2124
2125                 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
2126                         warn("CAMIOCOMMAND ioctl failed");
2127                         error = 1;
2128                 }
2129
2130                 if (error == 0) {
2131                         if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2132                              CAM_FUNC_NOTAVAIL) {
2133                                 warnx("CAM debugging not available");
2134                                 warnx("you need to put options CAMDEBUG in"
2135                                       " your kernel config file!");
2136                                 error = 1;
2137                         } else if ((ccb.ccb_h.status & CAM_STATUS_MASK) !=
2138                                     CAM_REQ_CMP) {
2139                                 warnx("XPT_DEBUG CCB failed with status %#x",
2140                                       ccb.ccb_h.status);
2141                                 error = 1;
2142                         } else {
2143                                 if (ccb.cdbg.flags == CAM_DEBUG_NONE) {
2144                                         fprintf(stderr,
2145                                                 "Debugging turned off\n");
2146                                 } else {
2147                                         fprintf(stderr,
2148                                                 "Debugging enabled for "
2149                                                 "%d:%d:%d\n",
2150                                                 bus, target, lun);
2151                                 }
2152                         }
2153                 }
2154                 close(fd);
2155         }
2156
2157         return(error);
2158 }
2159
2160 static int
2161 tagcontrol(struct cam_device *device, int argc, char **argv,
2162            char *combinedopt)
2163 {
2164         int c;
2165         union ccb *ccb;
2166         int numtags = -1;
2167         int retval = 0;
2168         int quiet = 0;
2169         char pathstr[1024];
2170
2171         ccb = cam_getccb(device);
2172
2173         if (ccb == NULL) {
2174                 warnx("tagcontrol: error allocating ccb");
2175                 return(1);
2176         }
2177
2178         while ((c = getopt(argc, argv, combinedopt)) != -1) {
2179                 switch(c) {
2180                 case 'N':
2181                         numtags = strtol(optarg, NULL, 0);
2182                         if (numtags < 0) {
2183                                 warnx("tag count %d is < 0", numtags);
2184                                 retval = 1;
2185                                 goto tagcontrol_bailout;
2186                         }
2187                         break;
2188                 case 'q':
2189                         quiet++;
2190                         break;
2191                 default:
2192                         break;
2193                 }
2194         }
2195
2196         cam_path_string(device, pathstr, sizeof(pathstr));
2197
2198         if (numtags >= 0) {
2199                 bzero(&(&ccb->ccb_h)[1],
2200                       sizeof(struct ccb_relsim) - sizeof(struct ccb_hdr));
2201                 ccb->ccb_h.func_code = XPT_REL_SIMQ;
2202                 ccb->crs.release_flags = RELSIM_ADJUST_OPENINGS;
2203                 ccb->crs.openings = numtags;
2204
2205
2206                 if (cam_send_ccb(device, ccb) < 0) {
2207                         perror("error sending XPT_REL_SIMQ CCB");
2208                         retval = 1;
2209                         goto tagcontrol_bailout;
2210                 }
2211
2212                 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2213                         warnx("XPT_REL_SIMQ CCB failed");
2214                         cam_error_print(device, ccb, CAM_ESF_ALL,
2215                                         CAM_EPF_ALL, stderr);
2216                         retval = 1;
2217                         goto tagcontrol_bailout;
2218                 }
2219
2220
2221                 if (quiet == 0)
2222                         fprintf(stdout, "%stagged openings now %d\n",
2223                                 pathstr, ccb->crs.openings);
2224         }
2225
2226         bzero(&(&ccb->ccb_h)[1],
2227               sizeof(struct ccb_getdevstats) - sizeof(struct ccb_hdr));
2228
2229         ccb->ccb_h.func_code = XPT_GDEV_STATS;
2230
2231         if (cam_send_ccb(device, ccb) < 0) {
2232                 perror("error sending XPT_GDEV_STATS CCB");
2233                 retval = 1;
2234                 goto tagcontrol_bailout;
2235         }
2236
2237         if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2238                 warnx("XPT_GDEV_STATS CCB failed");
2239                 cam_error_print(device, ccb, CAM_ESF_ALL,
2240                                 CAM_EPF_ALL, stderr);
2241                 retval = 1;
2242                 goto tagcontrol_bailout;
2243         }
2244
2245         if (arglist & CAM_ARG_VERBOSE) {
2246                 fprintf(stdout, "%s", pathstr);
2247                 fprintf(stdout, "dev_openings  %d\n", ccb->cgds.dev_openings);
2248                 fprintf(stdout, "%s", pathstr);
2249                 fprintf(stdout, "dev_active    %d\n", ccb->cgds.dev_active);
2250                 fprintf(stdout, "%s", pathstr);
2251                 fprintf(stdout, "devq_openings %d\n", ccb->cgds.devq_openings);
2252                 fprintf(stdout, "%s", pathstr);
2253                 fprintf(stdout, "devq_queued   %d\n", ccb->cgds.devq_queued);
2254                 fprintf(stdout, "%s", pathstr);
2255                 fprintf(stdout, "held          %d\n", ccb->cgds.held);
2256                 fprintf(stdout, "%s", pathstr);
2257                 fprintf(stdout, "mintags       %d\n", ccb->cgds.mintags);
2258                 fprintf(stdout, "%s", pathstr);
2259                 fprintf(stdout, "maxtags       %d\n", ccb->cgds.maxtags);
2260         } else {
2261                 if (quiet == 0) {
2262                         fprintf(stdout, "%s", pathstr);
2263                         fprintf(stdout, "device openings: ");
2264                 }
2265                 fprintf(stdout, "%d\n", ccb->cgds.dev_openings +
2266                         ccb->cgds.dev_active);
2267         }
2268
2269 tagcontrol_bailout:
2270
2271         cam_freeccb(ccb);
2272         return(retval);
2273 }
2274
2275 static void
2276 cts_print(struct cam_device *device, struct ccb_trans_settings *cts)
2277 {
2278         char pathstr[1024];
2279
2280         cam_path_string(device, pathstr, sizeof(pathstr));
2281
2282         if (cts->transport == XPORT_SPI) {
2283                 struct ccb_trans_settings_spi *spi =
2284                     &cts->xport_specific.spi;
2285
2286                 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
2287
2288                         fprintf(stdout, "%ssync parameter: %d\n", pathstr,
2289                                 spi->sync_period);
2290
2291                         if (spi->sync_offset != 0) {
2292                                 u_int freq;
2293
2294                                 freq = scsi_calc_syncsrate(spi->sync_period);
2295                                 fprintf(stdout, "%sfrequency: %d.%03dMHz\n",
2296                                         pathstr, freq / 1000, freq % 1000);
2297                         }
2298                 }
2299
2300                 if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) {
2301                         fprintf(stdout, "%soffset: %d\n", pathstr,
2302                             spi->sync_offset);
2303                 }
2304
2305                 if (spi->valid & CTS_SPI_VALID_BUS_WIDTH) {
2306                         fprintf(stdout, "%sbus width: %d bits\n", pathstr,
2307                                 (0x01 << spi->bus_width) * 8);
2308                 }
2309
2310                 if (spi->valid & CTS_SPI_VALID_DISC) {
2311                         fprintf(stdout, "%sdisconnection is %s\n", pathstr,
2312                                 (spi->flags & CTS_SPI_FLAGS_DISC_ENB) ?
2313                                 "enabled" : "disabled");
2314                 }
2315         }
2316
2317         if (cts->protocol == PROTO_SCSI) {
2318                 struct ccb_trans_settings_scsi *scsi=
2319                     &cts->proto_specific.scsi;
2320
2321                 if (scsi->valid & CTS_SCSI_VALID_TQ) {
2322                         fprintf(stdout, "%stagged queueing is %s\n", pathstr,
2323                                 (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) ?
2324                                 "enabled" : "disabled");
2325                 }
2326         }
2327
2328 }
2329
2330 /*
2331  * Get a path inquiry CCB for the specified device.  
2332  */
2333 static int
2334 get_cpi(struct cam_device *device, struct ccb_pathinq *cpi)
2335 {
2336         union ccb *ccb;
2337         int retval = 0;
2338
2339         ccb = cam_getccb(device);
2340
2341         if (ccb == NULL) {
2342                 warnx("get_cpi: couldn't allocate CCB");
2343                 return(1);
2344         }
2345
2346         bzero(&(&ccb->ccb_h)[1],
2347               sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2348
2349         ccb->ccb_h.func_code = XPT_PATH_INQ;
2350
2351         if (cam_send_ccb(device, ccb) < 0) {
2352                 warn("get_cpi: error sending Path Inquiry CCB");
2353
2354                 if (arglist & CAM_ARG_VERBOSE)
2355                         cam_error_print(device, ccb, CAM_ESF_ALL,
2356                                         CAM_EPF_ALL, stderr);
2357
2358                 retval = 1;
2359
2360                 goto get_cpi_bailout;
2361         }
2362
2363         if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2364
2365                 if (arglist & CAM_ARG_VERBOSE)
2366                         cam_error_print(device, ccb, CAM_ESF_ALL,
2367                                         CAM_EPF_ALL, stderr);
2368
2369                 retval = 1;
2370
2371                 goto get_cpi_bailout;
2372         }
2373
2374         bcopy(&ccb->cpi, cpi, sizeof(struct ccb_pathinq));
2375
2376 get_cpi_bailout:
2377
2378         cam_freeccb(ccb);
2379
2380         return(retval);
2381 }
2382
2383 static void
2384 cpi_print(struct ccb_pathinq *cpi)
2385 {
2386         char adapter_str[1024];
2387         int i;
2388
2389         snprintf(adapter_str, sizeof(adapter_str),
2390                  "%s%d:", cpi->dev_name, cpi->unit_number);
2391
2392         fprintf(stdout, "%s SIM/HBA version: %d\n", adapter_str,
2393                 cpi->version_num);
2394
2395         for (i = 1; i < 0xff; i = i << 1) {
2396                 const char *str;
2397
2398                 if ((i & cpi->hba_inquiry) == 0)
2399                         continue;
2400
2401                 fprintf(stdout, "%s supports ", adapter_str);
2402
2403                 switch(i) {
2404                 case PI_MDP_ABLE:
2405                         str = "MDP message";
2406                         break;
2407                 case PI_WIDE_32:
2408                         str = "32 bit wide SCSI";
2409                         break;
2410                 case PI_WIDE_16:
2411                         str = "16 bit wide SCSI";
2412                         break;
2413                 case PI_SDTR_ABLE:
2414                         str = "SDTR message";
2415                         break;
2416                 case PI_LINKED_CDB:
2417                         str = "linked CDBs";
2418                         break;
2419                 case PI_TAG_ABLE:
2420                         str = "tag queue messages";
2421                         break;
2422                 case PI_SOFT_RST:
2423                         str = "soft reset alternative";
2424                         break;
2425                 default:
2426                         str = "unknown PI bit set";
2427                         break;
2428                 }
2429                 fprintf(stdout, "%s\n", str);
2430         }
2431
2432         for (i = 1; i < 0xff; i = i << 1) {
2433                 const char *str;
2434
2435                 if ((i & cpi->hba_misc) == 0)
2436                         continue;
2437
2438                 fprintf(stdout, "%s ", adapter_str);
2439
2440                 switch(i) {
2441                 case PIM_SCANHILO:
2442                         str = "bus scans from high ID to low ID";
2443                         break;
2444                 case PIM_NOREMOVE:
2445                         str = "removable devices not included in scan";
2446                         break;
2447                 case PIM_NOINITIATOR:
2448                         str = "initiator role not supported";
2449                         break;
2450                 case PIM_NOBUSRESET:
2451                         str = "user has disabled initial BUS RESET or"
2452                               " controller is in target/mixed mode";
2453                         break;
2454                 default:
2455                         str = "unknown PIM bit set";
2456                         break;
2457                 }
2458                 fprintf(stdout, "%s\n", str);
2459         }
2460
2461         for (i = 1; i < 0xff; i = i << 1) {
2462                 const char *str;
2463
2464                 if ((i & cpi->target_sprt) == 0)
2465                         continue;
2466
2467                 fprintf(stdout, "%s supports ", adapter_str);
2468                 switch(i) {
2469                 case PIT_PROCESSOR:
2470                         str = "target mode processor mode";
2471                         break;
2472                 case PIT_PHASE:
2473                         str = "target mode phase cog. mode";
2474                         break;
2475                 case PIT_DISCONNECT:
2476                         str = "disconnects in target mode";
2477                         break;
2478                 case PIT_TERM_IO:
2479                         str = "terminate I/O message in target mode";
2480                         break;
2481                 case PIT_GRP_6:
2482                         str = "group 6 commands in target mode";
2483                         break;
2484                 case PIT_GRP_7:
2485                         str = "group 7 commands in target mode";
2486                         break;
2487                 default:
2488                         str = "unknown PIT bit set";
2489                         break;
2490                 }
2491
2492                 fprintf(stdout, "%s\n", str);
2493         }
2494         fprintf(stdout, "%s HBA engine count: %d\n", adapter_str,
2495                 cpi->hba_eng_cnt);
2496         fprintf(stdout, "%s maximum target: %d\n", adapter_str,
2497                 cpi->max_target);
2498         fprintf(stdout, "%s maximum LUN: %d\n", adapter_str,
2499                 cpi->max_lun);
2500         fprintf(stdout, "%s highest path ID in subsystem: %d\n",
2501                 adapter_str, cpi->hpath_id);
2502         fprintf(stdout, "%s initiator ID: %d\n", adapter_str,
2503                 cpi->initiator_id);
2504         fprintf(stdout, "%s SIM vendor: %s\n", adapter_str, cpi->sim_vid);
2505         fprintf(stdout, "%s HBA vendor: %s\n", adapter_str, cpi->hba_vid);
2506         fprintf(stdout, "%s bus ID: %d\n", adapter_str, cpi->bus_id);
2507         fprintf(stdout, "%s base transfer speed: ", adapter_str);
2508         if (cpi->base_transfer_speed > 1000)
2509                 fprintf(stdout, "%d.%03dMB/sec\n",
2510                         cpi->base_transfer_speed / 1000,
2511                         cpi->base_transfer_speed % 1000);
2512         else
2513                 fprintf(stdout, "%dKB/sec\n",
2514                         (cpi->base_transfer_speed % 1000) * 1000);
2515 }
2516
2517 static int
2518 get_print_cts(struct cam_device *device, int user_settings, int quiet,
2519               struct ccb_trans_settings *cts)
2520 {
2521         int retval;
2522         union ccb *ccb;
2523
2524         retval = 0;
2525         ccb = cam_getccb(device);
2526
2527         if (ccb == NULL) {
2528                 warnx("get_print_cts: error allocating ccb");
2529                 return(1);
2530         }
2531
2532         bzero(&(&ccb->ccb_h)[1],
2533               sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
2534
2535         ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2536
2537         if (user_settings == 0)
2538                 ccb->cts.type = CTS_TYPE_CURRENT_SETTINGS;
2539         else
2540                 ccb->cts.type = CTS_TYPE_USER_SETTINGS;
2541
2542         if (cam_send_ccb(device, ccb) < 0) {
2543                 perror("error sending XPT_GET_TRAN_SETTINGS CCB");
2544                 if (arglist & CAM_ARG_VERBOSE)
2545                         cam_error_print(device, ccb, CAM_ESF_ALL,
2546                                         CAM_EPF_ALL, stderr);
2547                 retval = 1;
2548                 goto get_print_cts_bailout;
2549         }
2550
2551         if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2552                 warnx("XPT_GET_TRANS_SETTINGS CCB failed");
2553                 if (arglist & CAM_ARG_VERBOSE)
2554                         cam_error_print(device, ccb, CAM_ESF_ALL,
2555                                         CAM_EPF_ALL, stderr);
2556                 retval = 1;
2557                 goto get_print_cts_bailout;
2558         }
2559
2560         if (quiet == 0)
2561                 cts_print(device, &ccb->cts);
2562
2563         if (cts != NULL)
2564                 bcopy(&ccb->cts, cts, sizeof(struct ccb_trans_settings));
2565
2566 get_print_cts_bailout:
2567
2568         cam_freeccb(ccb);
2569
2570         return(retval);
2571 }
2572
2573 static int
2574 ratecontrol(struct cam_device *device, int retry_count, int timeout,
2575             int argc, char **argv, char *combinedopt)
2576 {
2577         int c;
2578         union ccb *ccb;
2579         int user_settings = 0;
2580         int retval = 0;
2581         int disc_enable = -1, tag_enable = -1;
2582         int offset = -1;
2583         double syncrate = -1;
2584         int bus_width = -1;
2585         int quiet = 0;
2586         int change_settings = 0, send_tur = 0;
2587         struct ccb_pathinq cpi;
2588
2589         ccb = cam_getccb(device);
2590
2591         if (ccb == NULL) {
2592                 warnx("ratecontrol: error allocating ccb");
2593                 return(1);
2594         }
2595
2596         while ((c = getopt(argc, argv, combinedopt)) != -1) {
2597                 switch(c){
2598                 case 'a':
2599                         send_tur = 1;
2600                         break;
2601                 case 'c':
2602                         user_settings = 0;
2603                         break;
2604                 case 'D':
2605                         if (strncasecmp(optarg, "enable", 6) == 0)
2606                                 disc_enable = 1;
2607                         else if (strncasecmp(optarg, "disable", 7) == 0)
2608                                 disc_enable = 0;
2609                         else {
2610                                 warnx("-D argument \"%s\" is unknown", optarg);
2611                                 retval = 1;
2612                                 goto ratecontrol_bailout;
2613                         }
2614                         change_settings = 1;
2615                         break;
2616                 case 'O':
2617                         offset = strtol(optarg, NULL, 0);
2618                         if (offset < 0) {
2619                                 warnx("offset value %d is < 0", offset);
2620                                 retval = 1;
2621                                 goto ratecontrol_bailout;
2622                         }
2623                         change_settings = 1;
2624                         break;
2625                 case 'q':
2626                         quiet++;
2627                         break;
2628                 case 'R':
2629                         syncrate = atof(optarg);
2630
2631                         if (syncrate < 0) {
2632                                 warnx("sync rate %f is < 0", syncrate);
2633                                 retval = 1;
2634                                 goto ratecontrol_bailout;
2635                         }
2636                         change_settings = 1;
2637                         break;
2638                 case 'T':
2639                         if (strncasecmp(optarg, "enable", 6) == 0)
2640                                 tag_enable = 1;
2641                         else if (strncasecmp(optarg, "disable", 7) == 0)
2642                                 tag_enable = 0;
2643                         else {
2644                                 warnx("-T argument \"%s\" is unknown", optarg);
2645                                 retval = 1;
2646                                 goto ratecontrol_bailout;
2647                         }
2648                         change_settings = 1;
2649                         break;
2650                 case 'U':
2651                         user_settings = 1;
2652                         break;
2653                 case 'W':
2654                         bus_width = strtol(optarg, NULL, 0);
2655                         if (bus_width < 0) {
2656                                 warnx("bus width %d is < 0", bus_width);
2657                                 retval = 1;
2658                                 goto ratecontrol_bailout;
2659                         }
2660                         change_settings = 1;
2661                         break;
2662                 default:
2663                         break;
2664                 }
2665         }
2666
2667         bzero(&(&ccb->ccb_h)[1],
2668               sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2669
2670         /*
2671          * Grab path inquiry information, so we can determine whether
2672          * or not the initiator is capable of the things that the user
2673          * requests.
2674          */
2675         ccb->ccb_h.func_code = XPT_PATH_INQ;
2676
2677         if (cam_send_ccb(device, ccb) < 0) {
2678                 perror("error sending XPT_PATH_INQ CCB");
2679                 if (arglist & CAM_ARG_VERBOSE) {
2680                         cam_error_print(device, ccb, CAM_ESF_ALL,
2681                                         CAM_EPF_ALL, stderr);
2682                 }
2683                 retval = 1;
2684                 goto ratecontrol_bailout;
2685         }
2686
2687         if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2688                 warnx("XPT_PATH_INQ CCB failed");
2689                 if (arglist & CAM_ARG_VERBOSE) {
2690                         cam_error_print(device, ccb, CAM_ESF_ALL,
2691                                         CAM_EPF_ALL, stderr);
2692                 }
2693                 retval = 1;
2694                 goto ratecontrol_bailout;
2695         }
2696
2697         bcopy(&ccb->cpi, &cpi, sizeof(struct ccb_pathinq));
2698
2699         bzero(&(&ccb->ccb_h)[1],
2700               sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
2701
2702         if (quiet == 0)
2703                 fprintf(stdout, "Current Parameters:\n");
2704
2705         retval = get_print_cts(device, user_settings, quiet, &ccb->cts);
2706
2707         if (retval != 0)
2708                 goto ratecontrol_bailout;
2709
2710         if (arglist & CAM_ARG_VERBOSE)
2711                 cpi_print(&cpi);
2712
2713         if (change_settings) {
2714                 int didsettings = 0;
2715                 struct ccb_trans_settings_spi *spi = NULL;
2716                 struct ccb_trans_settings_scsi *scsi = NULL;
2717
2718                 if (ccb->cts.transport == XPORT_SPI) {
2719                         spi = &ccb->cts.xport_specific.spi;
2720                         spi->valid = 0;
2721                 }
2722                 if (ccb->cts.protocol == PROTO_SCSI) {
2723                         scsi = &ccb->cts.proto_specific.scsi;
2724                         scsi->valid = 0;
2725                 }
2726                 if (spi && disc_enable != -1) {
2727                         spi->valid |= CTS_SPI_VALID_DISC;
2728                         if (disc_enable == 0)
2729                                 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2730                         else
2731                                 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
2732                 }
2733
2734                 if (scsi && tag_enable != -1) {
2735                         if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0) {
2736                                 warnx("HBA does not support tagged queueing, "
2737                                       "so you cannot modify tag settings");
2738                                 retval = 1;
2739                                 goto ratecontrol_bailout;
2740                         }
2741
2742                         scsi->valid |= CTS_SCSI_VALID_TQ;
2743
2744                         if (tag_enable == 0)
2745                                 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2746                         else
2747                                 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
2748                         didsettings++;
2749                 }
2750
2751                 if (spi && offset != -1) {
2752                         if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
2753                                 warnx("HBA at %s%d is not cable of changing "
2754                                       "offset", cpi.dev_name,
2755                                       cpi.unit_number);
2756                                 retval = 1;
2757                                 goto ratecontrol_bailout;
2758                         }
2759                         spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
2760                         spi->sync_offset = offset;
2761                         didsettings++;
2762                 }
2763
2764                 if (spi && syncrate != -1) {
2765                         int prelim_sync_period;
2766                         u_int freq;
2767
2768                         if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
2769                                 warnx("HBA at %s%d is not cable of changing "
2770                                       "transfer rates", cpi.dev_name,
2771                                       cpi.unit_number);
2772                                 retval = 1;
2773                                 goto ratecontrol_bailout;
2774                         }
2775
2776                         spi->valid |= CTS_SPI_VALID_SYNC_RATE;
2777
2778                         /*
2779                          * The sync rate the user gives us is in MHz.
2780                          * We need to translate it into KHz for this
2781                          * calculation.
2782                          */
2783                         syncrate *= 1000;
2784
2785                         /*
2786                          * Next, we calculate a "preliminary" sync period
2787                          * in tenths of a nanosecond.
2788                          */
2789                         if (syncrate == 0)
2790                                 prelim_sync_period = 0;
2791                         else
2792                                 prelim_sync_period = 10000000 / syncrate;
2793
2794                         spi->sync_period =
2795                                 scsi_calc_syncparam(prelim_sync_period);
2796
2797                         freq = scsi_calc_syncsrate(spi->sync_period);
2798                         didsettings++;
2799                 }
2800
2801                 /*
2802                  * The bus_width argument goes like this:
2803                  * 0 == 8 bit
2804                  * 1 == 16 bit
2805                  * 2 == 32 bit
2806                  * Therefore, if you shift the number of bits given on the
2807                  * command line right by 4, you should get the correct
2808                  * number.
2809                  */
2810                 if (spi && bus_width != -1) {
2811
2812                         /*
2813                          * We might as well validate things here with a
2814                          * decipherable error message, rather than what
2815                          * will probably be an indecipherable error message
2816                          * by the time it gets back to us.
2817                          */
2818                         if ((bus_width == 16)
2819                          && ((cpi.hba_inquiry & PI_WIDE_16) == 0)) {
2820                                 warnx("HBA does not support 16 bit bus width");
2821                                 retval = 1;
2822                                 goto ratecontrol_bailout;
2823                         } else if ((bus_width == 32)
2824                                 && ((cpi.hba_inquiry & PI_WIDE_32) == 0)) {
2825                                 warnx("HBA does not support 32 bit bus width");
2826                                 retval = 1;
2827                                 goto ratecontrol_bailout;
2828                         } else if ((bus_width != 8)
2829                                 && (bus_width != 16)
2830                                 && (bus_width != 32)) {
2831                                 warnx("Invalid bus width %d", bus_width);
2832                                 retval = 1;
2833                                 goto ratecontrol_bailout;
2834                         }
2835
2836                         spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
2837                         spi->bus_width = bus_width >> 4;
2838                         didsettings++;
2839                 }
2840
2841                 if  (didsettings == 0) {
2842                         goto ratecontrol_bailout;
2843                 }
2844                 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2845
2846                 if (cam_send_ccb(device, ccb) < 0) {
2847                         perror("error sending XPT_SET_TRAN_SETTINGS CCB");
2848                         if (arglist & CAM_ARG_VERBOSE) {
2849                                 cam_error_print(device, ccb, CAM_ESF_ALL,
2850                                                 CAM_EPF_ALL, stderr);
2851                         }
2852                         retval = 1;
2853                         goto ratecontrol_bailout;
2854                 }
2855
2856                 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2857                         warnx("XPT_SET_TRANS_SETTINGS CCB failed");
2858                         if (arglist & CAM_ARG_VERBOSE) {
2859                                 cam_error_print(device, ccb, CAM_ESF_ALL,
2860                                                 CAM_EPF_ALL, stderr);
2861                         }
2862                         retval = 1;
2863                         goto ratecontrol_bailout;
2864                 }
2865         }
2866
2867         if (send_tur) {
2868                 retval = testunitready(device, retry_count, timeout,
2869                                        (arglist & CAM_ARG_VERBOSE) ? 0 : 1);
2870
2871                 /*
2872                  * If the TUR didn't succeed, just bail.
2873                  */
2874                 if (retval != 0) {
2875                         if (quiet == 0)
2876                                 fprintf(stderr, "Test Unit Ready failed\n");
2877                         goto ratecontrol_bailout;
2878                 }
2879
2880                 /*
2881                  * If the user wants things quiet, there's no sense in
2882                  * getting the transfer settings, if we're not going
2883                  * to print them.
2884                  */
2885                 if (quiet != 0)
2886                         goto ratecontrol_bailout;
2887
2888                 fprintf(stdout, "New Parameters:\n");
2889                 retval = get_print_cts(device, user_settings, 0, NULL);
2890         }
2891
2892 ratecontrol_bailout:
2893
2894         cam_freeccb(ccb);
2895         return(retval);
2896 }
2897
2898 static int
2899 scsiformat(struct cam_device *device, int argc, char **argv,
2900            char *combinedopt, int retry_count, int timeout)
2901 {
2902         union ccb *ccb;
2903         int c;
2904         int ycount = 0, quiet = 0;
2905         int error = 0, response = 0, retval = 0;
2906         int use_timeout = 10800 * 1000;
2907         int immediate = 1;
2908         struct format_defect_list_header fh;
2909         u_int8_t *data_ptr = NULL;
2910         u_int32_t dxfer_len = 0;
2911         u_int8_t byte2 = 0;
2912         int num_warnings = 0;
2913         int reportonly = 0;
2914
2915         ccb = cam_getccb(device);
2916
2917         if (ccb == NULL) {
2918                 warnx("scsiformat: error allocating ccb");
2919                 return(1);
2920         }
2921
2922         bzero(&(&ccb->ccb_h)[1],
2923               sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
2924
2925         while ((c = getopt(argc, argv, combinedopt)) != -1) {
2926                 switch(c) {
2927                 case 'q':
2928                         quiet++;
2929                         break;
2930                 case 'r':
2931                         reportonly = 1;
2932                         break;
2933                 case 'w':
2934                         immediate = 0;
2935                         break;
2936                 case 'y':
2937                         ycount++;
2938                         break;
2939                 }
2940         }
2941
2942         if (reportonly)
2943                 goto doreport;
2944
2945         if (quiet == 0) {
2946                 fprintf(stdout, "You are about to REMOVE ALL DATA from the "
2947                         "following device:\n");
2948
2949                 error = scsidoinquiry(device, argc, argv, combinedopt,
2950                                       retry_count, timeout);
2951
2952                 if (error != 0) {
2953                         warnx("scsiformat: error sending inquiry");
2954                         goto scsiformat_bailout;
2955                 }
2956         }
2957
2958         if (ycount == 0) {
2959
2960                 do {
2961                         char str[1024];
2962
2963                         fprintf(stdout, "Are you SURE you want to do "
2964                                 "this? (yes/no) ");
2965
2966                         if (fgets(str, sizeof(str), stdin) != NULL) {
2967
2968                                 if (strncasecmp(str, "yes", 3) == 0)
2969                                         response = 1;
2970                                 else if (strncasecmp(str, "no", 2) == 0)
2971                                         response = -1;
2972                                 else {
2973                                         fprintf(stdout, "Please answer"
2974                                                 " \"yes\" or \"no\"\n");
2975                                 }
2976                         }
2977                 } while (response == 0);
2978
2979                 if (response == -1) {
2980                         error = 1;
2981                         goto scsiformat_bailout;
2982                 }
2983         }
2984
2985         if (timeout != 0)
2986                 use_timeout = timeout;
2987
2988         if (quiet == 0) {
2989                 fprintf(stdout, "Current format timeout is %d seconds\n",
2990                         use_timeout / 1000);
2991         }
2992
2993         /*
2994          * If the user hasn't disabled questions and didn't specify a
2995          * timeout on the command line, ask them if they want the current
2996          * timeout.
2997          */
2998         if ((ycount == 0)
2999          && (timeout == 0)) {
3000                 char str[1024];
3001                 int new_timeout = 0;
3002
3003                 fprintf(stdout, "Enter new timeout in seconds or press\n"
3004                         "return to keep the current timeout [%d] ",
3005                         use_timeout / 1000);
3006
3007                 if (fgets(str, sizeof(str), stdin) != NULL) {
3008                         if (str[0] != '\0')
3009                                 new_timeout = atoi(str);
3010                 }
3011
3012                 if (new_timeout != 0) {
3013                         use_timeout = new_timeout * 1000;
3014                         fprintf(stdout, "Using new timeout value %d\n",
3015                                 use_timeout / 1000);
3016                 }
3017         }
3018
3019         /*
3020          * Keep this outside the if block below to silence any unused
3021          * variable warnings.
3022          */
3023         bzero(&fh, sizeof(fh));
3024
3025         /*
3026          * If we're in immediate mode, we've got to include the format
3027          * header
3028          */
3029         if (immediate != 0) {
3030                 fh.byte2 = FU_DLH_IMMED;
3031                 data_ptr = (u_int8_t *)&fh;
3032                 dxfer_len = sizeof(fh);
3033                 byte2 = FU_FMT_DATA;
3034         } else if (quiet == 0) {
3035                 fprintf(stdout, "Formatting...");
3036                 fflush(stdout);
3037         }
3038
3039         scsi_format_unit(&ccb->csio,
3040                          /* retries */ retry_count,
3041                          /* cbfcnp */ NULL,
3042                          /* tag_action */ MSG_SIMPLE_Q_TAG,
3043                          /* byte2 */ byte2,
3044                          /* ileave */ 0,
3045                          /* data_ptr */ data_ptr,
3046                          /* dxfer_len */ dxfer_len,
3047                          /* sense_len */ SSD_FULL_SIZE,
3048                          /* timeout */ use_timeout);
3049
3050         /* Disable freezing the device queue */
3051         ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3052
3053         if (arglist & CAM_ARG_ERR_RECOVER)
3054                 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
3055
3056         if (((retval = cam_send_ccb(device, ccb)) < 0)
3057          || ((immediate == 0)
3058            && ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP))) {
3059                 const char errstr[] = "error sending format command";
3060
3061                 if (retval < 0)
3062                         warn(errstr);
3063                 else
3064                         warnx(errstr);
3065
3066                 if (arglist & CAM_ARG_VERBOSE) {
3067                         cam_error_print(device, ccb, CAM_ESF_ALL,
3068                                         CAM_EPF_ALL, stderr);
3069                 }
3070                 error = 1;
3071                 goto scsiformat_bailout;
3072         }
3073
3074         /*
3075          * If we ran in non-immediate mode, we already checked for errors
3076          * above and printed out any necessary information.  If we're in
3077          * immediate mode, we need to loop through and get status
3078          * information periodically.
3079          */
3080         if (immediate == 0) {
3081                 if (quiet == 0) {
3082                         fprintf(stdout, "Format Complete\n");
3083                 }
3084                 goto scsiformat_bailout;
3085         }
3086
3087 doreport:
3088         do {
3089                 cam_status status;
3090
3091                 bzero(&(&ccb->ccb_h)[1],
3092                       sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3093
3094                 /*
3095                  * There's really no need to do error recovery or
3096                  * retries here, since we're just going to sit in a
3097                  * loop and wait for the device to finish formatting.
3098                  */
3099                 scsi_test_unit_ready(&ccb->csio,
3100                                      /* retries */ 0,
3101                                      /* cbfcnp */ NULL,
3102                                      /* tag_action */ MSG_SIMPLE_Q_TAG,
3103                                      /* sense_len */ SSD_FULL_SIZE,
3104                                      /* timeout */ 5000);
3105
3106                 /* Disable freezing the device queue */
3107                 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3108
3109                 retval = cam_send_ccb(device, ccb);
3110
3111                 /*
3112                  * If we get an error from the ioctl, bail out.  SCSI
3113                  * errors are expected.
3114                  */
3115                 if (retval < 0) {
3116                         warn("error sending CAMIOCOMMAND ioctl");
3117                         if (arglist & CAM_ARG_VERBOSE) {
3118                                 cam_error_print(device, ccb, CAM_ESF_ALL,
3119                                                 CAM_EPF_ALL, stderr);
3120                         }
3121                         error = 1;
3122                         goto scsiformat_bailout;
3123                 }
3124
3125                 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3126
3127                 if ((status != CAM_REQ_CMP)
3128                  && (status == CAM_SCSI_STATUS_ERROR)
3129                  && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
3130                         struct scsi_sense_data *sense;
3131                         int error_code, sense_key, asc, ascq;
3132
3133                         sense = &ccb->csio.sense_data;
3134                         scsi_extract_sense(sense, &error_code, &sense_key,
3135                                            &asc, &ascq);
3136
3137                         /*
3138                          * According to the SCSI-2 and SCSI-3 specs, a
3139                          * drive that is in the middle of a format should
3140                          * return NOT READY with an ASC of "logical unit
3141                          * not ready, format in progress".  The sense key
3142                          * specific bytes will then be a progress indicator.
3143                          */
3144                         if ((sense_key == SSD_KEY_NOT_READY)
3145                          && (asc == 0x04) && (ascq == 0x04)) {
3146                                 if ((sense->extra_len >= 10)
3147                                  && ((sense->sense_key_spec[0] &
3148                                       SSD_SCS_VALID) != 0)
3149                                  && (quiet == 0)) {
3150                                         int val;
3151                                         u_int64_t percentage;
3152
3153                                         val = scsi_2btoul(
3154                                                 &sense->sense_key_spec[1]);
3155                                         percentage = 10000 * val;
3156
3157                                         fprintf(stdout,
3158                                                 "\rFormatting:  %ju.%02u %% "
3159                                                 "(%d/%d) done",
3160                                                 (uintmax_t)(percentage / 
3161                                                 (0x10000 * 100)),
3162                                                 (unsigned)((percentage / 
3163                                                 0x10000) % 100),
3164                                                 val, 0x10000);
3165                                         fflush(stdout);
3166                                 } else if ((quiet == 0)
3167                                         && (++num_warnings <= 1)) {
3168                                         warnx("Unexpected SCSI Sense Key "
3169                                               "Specific value returned "
3170                                               "during format:");
3171                                         scsi_sense_print(device, &ccb->csio,
3172                                                          stderr);
3173                                         warnx("Unable to print status "
3174                                               "information, but format will "
3175                                               "proceed.");
3176                                         warnx("will exit when format is "
3177                                               "complete");
3178                                 }
3179                                 sleep(1);
3180                         } else {
3181                                 warnx("Unexpected SCSI error during format");
3182                                 cam_error_print(device, ccb, CAM_ESF_ALL,
3183                                                 CAM_EPF_ALL, stderr);
3184                                 error = 1;
3185                                 goto scsiformat_bailout;
3186                         }
3187
3188                 } else if (status != CAM_REQ_CMP) {
3189                         warnx("Unexpected CAM status %#x", status);
3190                         if (arglist & CAM_ARG_VERBOSE)
3191                                 cam_error_print(device, ccb, CAM_ESF_ALL,
3192                                                 CAM_EPF_ALL, stderr);
3193                         error = 1;
3194                         goto scsiformat_bailout;
3195                 }
3196
3197         } while((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP);
3198
3199         if (quiet == 0)
3200                 fprintf(stdout, "\nFormat Complete\n");
3201
3202 scsiformat_bailout:
3203
3204         cam_freeccb(ccb);
3205
3206         return(error);
3207 }
3208
3209 static int
3210 scsireportluns(struct cam_device *device, int argc, char **argv,
3211                char *combinedopt, int retry_count, int timeout)
3212 {
3213         union ccb *ccb;
3214         int c, countonly, lunsonly;
3215         struct scsi_report_luns_data *lundata;
3216         int alloc_len;
3217         uint8_t report_type;
3218         uint32_t list_len, i, j;
3219         int retval;
3220
3221         retval = 0;
3222         lundata = NULL;
3223         report_type = RPL_REPORT_DEFAULT;
3224         ccb = cam_getccb(device);
3225
3226         if (ccb == NULL) {
3227                 warnx("%s: error allocating ccb", __func__);
3228                 return (1);
3229         }
3230
3231         bzero(&(&ccb->ccb_h)[1],
3232               sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3233
3234         countonly = 0;
3235         lunsonly = 0;
3236
3237         while ((c = getopt(argc, argv, combinedopt)) != -1) {
3238                 switch (c) {
3239                 case 'c':
3240                         countonly++;
3241                         break;
3242                 case 'l':
3243                         lunsonly++;
3244                         break;
3245                 case 'r':
3246                         if (strcasecmp(optarg, "default") == 0)
3247                                 report_type = RPL_REPORT_DEFAULT;
3248                         else if (strcasecmp(optarg, "wellknown") == 0)
3249                                 report_type = RPL_REPORT_WELLKNOWN;
3250                         else if (strcasecmp(optarg, "all") == 0)
3251                                 report_type = RPL_REPORT_ALL;
3252                         else {
3253                                 warnx("%s: invalid report type \"%s\"",
3254                                       __func__, optarg);
3255                                 retval = 1;
3256                                 goto bailout;
3257                         }
3258                         break;
3259                 default:
3260                         break;
3261                 }
3262         }
3263
3264         if ((countonly != 0)
3265          && (lunsonly != 0)) {
3266                 warnx("%s: you can only specify one of -c or -l", __func__);
3267                 retval = 1;
3268                 goto bailout;
3269         }
3270         /*
3271          * According to SPC-4, the allocation length must be at least 16
3272          * bytes -- enough for the header and one LUN.
3273          */
3274         alloc_len = sizeof(*lundata) + 8;
3275
3276 retry:
3277
3278         lundata = malloc(alloc_len);
3279
3280         if (lundata == NULL) {
3281                 warn("%s: error mallocing %d bytes", __func__, alloc_len);
3282                 retval = 1;
3283                 goto bailout;
3284         }
3285
3286         scsi_report_luns(&ccb->csio,
3287                          /*retries*/ retry_count,
3288                          /*cbfcnp*/ NULL,
3289                          /*tag_action*/ MSG_SIMPLE_Q_TAG,
3290                          /*select_report*/ report_type,
3291                          /*rpl_buf*/ lundata,
3292                          /*alloc_len*/ alloc_len,
3293                          /*sense_len*/ SSD_FULL_SIZE,
3294                          /*timeout*/ timeout ? timeout : 5000);
3295
3296         /* Disable freezing the device queue */
3297         ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3298
3299         if (arglist & CAM_ARG_ERR_RECOVER)
3300                 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
3301
3302         if (cam_send_ccb(device, ccb) < 0) {
3303                 warn("error sending REPORT LUNS command");
3304
3305                 if (arglist & CAM_ARG_VERBOSE)
3306                         cam_error_print(device, ccb, CAM_ESF_ALL,
3307                                         CAM_EPF_ALL, stderr);
3308
3309                 retval = 1;
3310                 goto bailout;
3311         }
3312
3313         if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3314                 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
3315                 retval = 1;
3316                 goto bailout;
3317         }
3318
3319
3320         list_len = scsi_4btoul(lundata->length);
3321
3322         /*
3323          * If we need to list the LUNs, and our allocation
3324          * length was too short, reallocate and retry.
3325          */
3326         if ((countonly == 0)
3327          && (list_len > (alloc_len - sizeof(*lundata)))) {
3328                 alloc_len = list_len + sizeof(*lundata);
3329                 free(lundata);
3330                 goto retry;
3331         }
3332
3333         if (lunsonly == 0)
3334                 fprintf(stdout, "%u LUN%s found\n", list_len / 8,
3335                         ((list_len / 8) > 1) ? "s" : "");
3336
3337         if (countonly != 0)
3338                 goto bailout;
3339
3340         for (i = 0; i < (list_len / 8); i++) {
3341                 int no_more;
3342
3343                 no_more = 0;
3344                 for (j = 0; j < sizeof(lundata->luns[i].lundata); j += 2) {
3345                         if (j != 0)
3346                                 fprintf(stdout, ",");
3347                         switch (lundata->luns[i].lundata[j] &
3348                                 RPL_LUNDATA_ATYP_MASK) {
3349                         case RPL_LUNDATA_ATYP_PERIPH:
3350                                 if ((lundata->luns[i].lundata[j] &
3351                                     RPL_LUNDATA_PERIPH_BUS_MASK) != 0)
3352                                         fprintf(stdout, "%d:", 
3353                                                 lundata->luns[i].lundata[j] &
3354                                                 RPL_LUNDATA_PERIPH_BUS_MASK);
3355                                 else if ((j == 0)
3356                                       && ((lundata->luns[i].lundata[j+2] &
3357                                           RPL_LUNDATA_PERIPH_BUS_MASK) == 0))
3358                                         no_more = 1;
3359
3360                                 fprintf(stdout, "%d",
3361                                         lundata->luns[i].lundata[j+1]);
3362                                 break;
3363                         case RPL_LUNDATA_ATYP_FLAT: {
3364                                 uint8_t tmplun[2];
3365                                 tmplun[0] = lundata->luns[i].lundata[j] &
3366                                         RPL_LUNDATA_FLAT_LUN_MASK;
3367                                 tmplun[1] = lundata->luns[i].lundata[j+1];
3368
3369                                 fprintf(stdout, "%d", scsi_2btoul(tmplun));
3370                                 no_more = 1;
3371                                 break;
3372                         }
3373                         case RPL_LUNDATA_ATYP_LUN:
3374                                 fprintf(stdout, "%d:%d:%d",
3375                                         (lundata->luns[i].lundata[j+1] &
3376                                         RPL_LUNDATA_LUN_BUS_MASK) >> 5,
3377                                         lundata->luns[i].lundata[j] &
3378                                         RPL_LUNDATA_LUN_TARG_MASK,
3379                                         lundata->luns[i].lundata[j+1] &
3380                                         RPL_LUNDATA_LUN_LUN_MASK);
3381                                 break;
3382                         case RPL_LUNDATA_ATYP_EXTLUN: {
3383                                 int field_len, field_len_code, eam_code;
3384
3385                                 eam_code = lundata->luns[i].lundata[j] &
3386                                         RPL_LUNDATA_EXT_EAM_MASK;
3387                                 field_len_code = (lundata->luns[i].lundata[j] &
3388                                         RPL_LUNDATA_EXT_LEN_MASK) >> 4;
3389                                 field_len = field_len_code * 2;
3390                 
3391                                 if ((eam_code == RPL_LUNDATA_EXT_EAM_WK)
3392                                  && (field_len_code == 0x00)) {
3393                                         fprintf(stdout, "%d",
3394                                                 lundata->luns[i].lundata[j+1]);
3395                                 } else if ((eam_code ==
3396                                             RPL_LUNDATA_EXT_EAM_NOT_SPEC)
3397                                         && (field_len_code == 0x03)) {
3398                                         uint8_t tmp_lun[8];
3399
3400                                         /*
3401                                          * This format takes up all 8 bytes.
3402                                          * If we aren't starting at offset 0,
3403                                          * that's a bug.
3404                                          */
3405                                         if (j != 0) {
3406                                                 fprintf(stdout, "Invalid "
3407                                                         "offset %d for "
3408                                                         "Extended LUN not "
3409                                                         "specified format", j);
3410                                                 no_more = 1;
3411                                                 break;
3412                                         }
3413                                         bzero(tmp_lun, sizeof(tmp_lun));
3414                                         bcopy(&lundata->luns[i].lundata[j+1],
3415                                               &tmp_lun[1], sizeof(tmp_lun) - 1);
3416                                         fprintf(stdout, "%#jx",
3417                                                (intmax_t)scsi_8btou64(tmp_lun));
3418                                         no_more = 1;
3419                                 } else {
3420                                         fprintf(stderr, "Unknown Extended LUN"
3421                                                 "Address method %#x, length "
3422                                                 "code %#x", eam_code,
3423                                                 field_len_code);
3424                                         no_more = 1;
3425                                 }
3426                                 break;
3427                         }
3428                         default:
3429                                 fprintf(stderr, "Unknown LUN address method "
3430                                         "%#x\n", lundata->luns[i].lundata[0] &
3431                                         RPL_LUNDATA_ATYP_MASK);
3432                                 break;
3433                         }
3434                         /*
3435                          * For the flat addressing method, there are no
3436                          * other levels after it.
3437                          */
3438                         if (no_more != 0)
3439                                 break;
3440                 }
3441                 fprintf(stdout, "\n");
3442         }
3443
3444 bailout:
3445
3446         cam_freeccb(ccb);
3447
3448         free(lundata);
3449
3450         return (retval);
3451 }
3452
3453 #endif /* MINIMALISTIC */
3454
3455 void 
3456 usage(int verbose)
3457 {
3458         fprintf(verbose ? stdout : stderr,
3459 "usage:  camcontrol <command>  [device id][generic args][command args]\n"
3460 "        camcontrol devlist    [-v]\n"
3461 #ifndef MINIMALISTIC
3462 "        camcontrol periphlist [dev_id][-n dev_name] [-u unit]\n"
3463 "        camcontrol tur        [dev_id][generic args]\n"
3464 "        camcontrol inquiry    [dev_id][generic args] [-D] [-S] [-R]\n"
3465 "        camcontrol reportluns [dev_id][generic args] [-c] [-l] [-r report]\n"
3466 "        camcontrol start      [dev_id][generic args]\n"
3467 "        camcontrol stop       [dev_id][generic args]\n"
3468 "        camcontrol load       [dev_id][generic args]\n"
3469 "        camcontrol eject      [dev_id][generic args]\n"
3470 #endif /* MINIMALISTIC */
3471 "        camcontrol rescan     <all | bus[:target:lun]>\n"
3472 "        camcontrol reset      <all | bus[:target:lun]>\n"
3473 #ifndef MINIMALISTIC
3474 "        camcontrol defects    [dev_id][generic args] <-f format> [-P][-G]\n"
3475 "        camcontrol modepage   [dev_id][generic args] <-m page | -l>\n"
3476 "                              [-P pagectl][-e | -b][-d]\n"
3477 "        camcontrol cmd        [dev_id][generic args] <-c cmd [args]>\n"
3478 "                              [-i len fmt|-o len fmt [args]]\n"
3479 "        camcontrol debug      [-I][-P][-T][-S][-X][-c]\n"
3480 "                              <all|bus[:target[:lun]]|off>\n"
3481 "        camcontrol tags       [dev_id][generic args] [-N tags] [-q] [-v]\n"
3482 "        camcontrol negotiate  [dev_id][generic args] [-a][-c]\n"
3483 "                              [-D <enable|disable>][-O offset][-q]\n"
3484 "                              [-R syncrate][-v][-T <enable|disable>]\n"
3485 "                              [-U][-W bus_width]\n"
3486 "        camcontrol format     [dev_id][generic args][-q][-r][-w][-y]\n"
3487 #endif /* MINIMALISTIC */
3488 "        camcontrol help\n");
3489         if (!verbose)
3490                 return;
3491 #ifndef MINIMALISTIC
3492         fprintf(stdout,
3493 "Specify one of the following options:\n"
3494 "devlist     list all CAM devices\n"
3495 "periphlist  list all CAM peripheral drivers attached to a device\n"
3496 "tur         send a test unit ready to the named device\n"
3497 "inquiry     send a SCSI inquiry command to the named device\n"
3498 "reportluns  send a SCSI report luns command to the device\n"
3499 "start       send a Start Unit command to the device\n"
3500 "stop        send a Stop Unit command to the device\n"
3501 "load        send a Start Unit command to the device with the load bit set\n"
3502 "eject       send a Stop Unit command to the device with the eject bit set\n"
3503 "rescan      rescan all busses, the given bus, or bus:target:lun\n"
3504 "reset       reset all busses, the given bus, or bus:target:lun\n"
3505 "defects     read the defect list of the specified device\n"
3506 "modepage    display or edit (-e) the given mode page\n"
3507 "cmd         send the given scsi command, may need -i or -o as well\n"
3508 "debug       turn debugging on/off for a bus, target, or lun, or all devices\n"
3509 "tags        report or set the number of transaction slots for a device\n"
3510 "negotiate   report or set device negotiation parameters\n"
3511 "format      send the SCSI FORMAT UNIT command to the named device\n"
3512 "help        this message\n"
3513 "Device Identifiers:\n"
3514 "bus:target        specify the bus and target, lun defaults to 0\n"
3515 "bus:target:lun    specify the bus, target and lun\n"
3516 "deviceUNIT        specify the device name, like \"da4\" or \"cd2\"\n"
3517 "Generic arguments:\n"
3518 "-v                be verbose, print out sense information\n"
3519 "-t timeout        command timeout in seconds, overrides default timeout\n"
3520 "-n dev_name       specify device name, e.g. \"da\", \"cd\"\n"
3521 "-u unit           specify unit number, e.g. \"0\", \"5\"\n"
3522 "-E                have the kernel attempt to perform SCSI error recovery\n"
3523 "-C count          specify the SCSI command retry count (needs -E to work)\n"
3524 "modepage arguments:\n"
3525 "-l                list all available mode pages\n"
3526 "-m page           specify the mode page to view or edit\n"
3527 "-e                edit the specified mode page\n"
3528 "-b                force view to binary mode\n"
3529 "-d                disable block descriptors for mode sense\n"
3530 "-P pgctl          page control field 0-3\n"
3531 "defects arguments:\n"
3532 "-f format         specify defect list format (block, bfi or phys)\n"
3533 "-G                get the grown defect list\n"
3534 "-P                get the permanant defect list\n"
3535 "inquiry arguments:\n"
3536 "-D                get the standard inquiry data\n"
3537 "-S                get the serial number\n"
3538 "-R                get the transfer rate, etc.\n"
3539 "reportluns arguments:\n"
3540 "-c                only report a count of available LUNs\n"
3541 "-l                only print out luns, and not a count\n"
3542 "-r <reporttype>   specify \"default\", \"wellknown\" or \"all\"\n"
3543 "cmd arguments:\n"
3544 "-c cdb [args]     specify the SCSI CDB\n"
3545 "-i len fmt        specify input data and input data format\n"
3546 "-o len fmt [args] specify output data and output data fmt\n"
3547 "debug arguments:\n"
3548 "-I                CAM_DEBUG_INFO -- scsi commands, errors, data\n"
3549 "-T                CAM_DEBUG_TRACE -- routine flow tracking\n"
3550 "-S                CAM_DEBUG_SUBTRACE -- internal routine command flow\n"
3551 "-c                CAM_DEBUG_CDB -- print out SCSI CDBs only\n"
3552 "tags arguments:\n"
3553 "-N tags           specify the number of tags to use for this device\n"
3554 "-q                be quiet, don't report the number of tags\n"
3555 "-v                report a number of tag-related parameters\n"
3556 "negotiate arguments:\n"
3557 "-a                send a test unit ready after negotiation\n"
3558 "-c                report/set current negotiation settings\n"
3559 "-D <arg>          \"enable\" or \"disable\" disconnection\n"
3560 "-O offset         set command delay offset\n"
3561 "-q                be quiet, don't report anything\n"
3562 "-R syncrate       synchronization rate in MHz\n"
3563 "-T <arg>          \"enable\" or \"disable\" tagged queueing\n"
3564 "-U                report/set user negotiation settings\n"
3565 "-W bus_width      set the bus width in bits (8, 16 or 32)\n"
3566 "-v                also print a Path Inquiry CCB for the controller\n"
3567 "format arguments:\n"
3568 "-q                be quiet, don't print status messages\n"
3569 "-r                run in report only mode\n"
3570 "-w                don't send immediate format command\n"
3571 "-y                don't ask any questions\n");
3572 #endif /* MINIMALISTIC */
3573 }
3574
3575 int 
3576 main(int argc, char **argv)
3577 {
3578         int c;
3579         char *device = NULL;
3580         int unit = 0;
3581         struct cam_device *cam_dev = NULL;
3582         int timeout = 0, retry_count = 1;
3583         camcontrol_optret optreturn;
3584         char *tstr;
3585         const char *mainopt = "C:En:t:u:v";
3586         const char *subopt = NULL;
3587         char combinedopt[256];
3588         int error = 0, optstart = 2;
3589         int devopen = 1;
3590 #ifndef MINIMALISTIC
3591         int bus, target, lun;
3592 #endif /* MINIMALISTIC */
3593
3594         cmdlist = CAM_CMD_NONE;
3595         arglist = CAM_ARG_NONE;
3596
3597         if (argc < 2) {
3598                 usage(0);
3599                 exit(1);
3600         }
3601
3602         /*
3603          * Get the base option.
3604          */
3605         optreturn = getoption(argv[1], &cmdlist, &arglist, &subopt);
3606
3607         if (optreturn == CC_OR_AMBIGUOUS) {
3608                 warnx("ambiguous option %s", argv[1]);
3609                 usage(0);
3610                 exit(1);
3611         } else if (optreturn == CC_OR_NOT_FOUND) {
3612                 warnx("option %s not found", argv[1]);
3613                 usage(0);
3614                 exit(1);
3615         }
3616
3617         /*
3618          * Ahh, getopt(3) is a pain.
3619          *
3620          * This is a gross hack.  There really aren't many other good
3621          * options (excuse the pun) for parsing options in a situation like
3622          * this.  getopt is kinda braindead, so you end up having to run
3623          * through the options twice, and give each invocation of getopt
3624          * the option string for the other invocation.
3625          * 
3626          * You would think that you could just have two groups of options.
3627          * The first group would get parsed by the first invocation of
3628          * getopt, and the second group would get parsed by the second
3629          * invocation of getopt.  It doesn't quite work out that way.  When
3630          * the first invocation of getopt finishes, it leaves optind pointing
3631          * to the argument _after_ the first argument in the second group.
3632          * So when the second invocation of getopt comes around, it doesn't
3633          * recognize the first argument it gets and then bails out.
3634          * 
3635          * A nice alternative would be to have a flag for getopt that says
3636          * "just keep parsing arguments even when you encounter an unknown
3637          * argument", but there isn't one.  So there's no real clean way to
3638          * easily parse two sets of arguments without having one invocation
3639          * of getopt know about the other.
3640          * 
3641          * Without this hack, the first invocation of getopt would work as
3642          * long as the generic arguments are first, but the second invocation
3643          * (in the subfunction) would fail in one of two ways.  In the case
3644          * where you don't set optreset, it would fail because optind may be
3645          * pointing to the argument after the one it should be pointing at.
3646          * In the case where you do set optreset, and reset optind, it would
3647          * fail because getopt would run into the first set of options, which
3648          * it doesn't understand.
3649          *
3650          * All of this would "sort of" work if you could somehow figure out
3651          * whether optind had been incremented one option too far.  The
3652          * mechanics of that, however, are more daunting than just giving
3653          * both invocations all of the expect options for either invocation.
3654          * 
3655          * Needless to say, I wouldn't mind if someone invented a better
3656          * (non-GPL!) command line parsing interface than getopt.  I
3657          * wouldn't mind if someone added more knobs to getopt to make it
3658          * work better.  Who knows, I may talk myself into doing it someday,
3659          * if the standards weenies let me.  As it is, it just leads to
3660          * hackery like this and causes people to avoid it in some cases.
3661          * 
3662          * KDM, September 8th, 1998
3663          */
3664         if (subopt != NULL)
3665                 sprintf(combinedopt, "%s%s", mainopt, subopt);
3666         else
3667                 sprintf(combinedopt, "%s", mainopt);
3668
3669         /*
3670          * For these options we do not parse optional device arguments and
3671          * we do not open a passthrough device.
3672          */
3673         if ((cmdlist == CAM_CMD_RESCAN)
3674          || (cmdlist == CAM_CMD_RESET)
3675          || (cmdlist == CAM_CMD_DEVTREE)
3676          || (cmdlist == CAM_CMD_USAGE)
3677          || (cmdlist == CAM_CMD_DEBUG))
3678                 devopen = 0;
3679
3680 #ifndef MINIMALISTIC
3681         if ((devopen == 1)
3682          && (argc > 2 && argv[2][0] != '-')) {
3683                 char name[30];
3684                 int rv;
3685
3686                 /*
3687                  * First catch people who try to do things like:
3688                  * camcontrol tur /dev/da0 
3689                  * camcontrol doesn't take device nodes as arguments.
3690                  */
3691                 if (argv[2][0] == '/') {
3692                         warnx("%s is not a valid device identifier", argv[2]);
3693                         errx(1, "please read the camcontrol(8) man page");
3694                 } else if (isdigit(argv[2][0])) {
3695                         /* device specified as bus:target[:lun] */
3696                         rv = parse_btl(argv[2], &bus, &target, &lun, &arglist);
3697                         if (rv < 2)
3698                                 errx(1, "numeric device specification must "
3699                                      "be either bus:target, or "
3700                                      "bus:target:lun");
3701                         /* default to 0 if lun was not specified */
3702                         if ((arglist & CAM_ARG_LUN) == 0) {
3703                                 lun = 0;
3704                                 arglist |= CAM_ARG_LUN;
3705                         }
3706                         optstart++;
3707                 } else {
3708                         if (cam_get_device(argv[2], name, sizeof name, &unit)
3709                             == -1)
3710                                 errx(1, "%s", cam_errbuf);
3711                         device = strdup(name);
3712                         arglist |= CAM_ARG_DEVICE | CAM_ARG_UNIT;
3713                         optstart++;
3714                 }
3715         }
3716 #endif /* MINIMALISTIC */
3717         /*
3718          * Start getopt processing at argv[2/3], since we've already
3719          * accepted argv[1..2] as the command name, and as a possible
3720          * device name.
3721          */
3722         optind = optstart;
3723
3724         /*
3725          * Now we run through the argument list looking for generic
3726          * options, and ignoring options that possibly belong to
3727          * subfunctions.
3728          */
3729         while ((c = getopt(argc, argv, combinedopt))!= -1){
3730                 switch(c) {
3731                         case 'C':
3732                                 retry_count = strtol(optarg, NULL, 0);
3733                                 if (retry_count < 0)
3734                                         errx(1, "retry count %d is < 0",
3735                                              retry_count);
3736                                 arglist |= CAM_ARG_RETRIES;
3737                                 break;
3738                         case 'E':
3739                                 arglist |= CAM_ARG_ERR_RECOVER;
3740                                 break;
3741                         case 'n':
3742                                 arglist |= CAM_ARG_DEVICE;
3743                                 tstr = optarg;
3744                                 while (isspace(*tstr) && (*tstr != '\0'))
3745                                         tstr++;
3746                                 device = (char *)strdup(tstr);
3747                                 break;
3748                         case 't':
3749                                 timeout = strtol(optarg, NULL, 0);
3750                                 if (timeout < 0)
3751                                         errx(1, "invalid timeout %d", timeout);
3752                                 /* Convert the timeout from seconds to ms */
3753                                 timeout *= 1000;
3754                                 arglist |= CAM_ARG_TIMEOUT;
3755                                 break;
3756                         case 'u':
3757                                 arglist |= CAM_ARG_UNIT;
3758                                 unit = strtol(optarg, NULL, 0);
3759                                 break;
3760                         case 'v':
3761                                 arglist |= CAM_ARG_VERBOSE;
3762                                 break;
3763                         default:
3764                                 break;
3765                 }
3766         }
3767
3768 #ifndef MINIMALISTIC
3769         /*
3770          * For most commands we'll want to open the passthrough device
3771          * associated with the specified device.  In the case of the rescan
3772          * commands, we don't use a passthrough device at all, just the
3773          * transport layer device.
3774          */
3775         if (devopen == 1) {
3776                 if (((arglist & (CAM_ARG_BUS|CAM_ARG_TARGET)) == 0)
3777                  && (((arglist & CAM_ARG_DEVICE) == 0)
3778                   || ((arglist & CAM_ARG_UNIT) == 0))) {
3779                         errx(1, "subcommand \"%s\" requires a valid device "
3780                              "identifier", argv[1]);
3781                 }
3782
3783                 if ((cam_dev = ((arglist & (CAM_ARG_BUS | CAM_ARG_TARGET))?
3784                                 cam_open_btl(bus, target, lun, O_RDWR, NULL) :
3785                                 cam_open_spec_device(device,unit,O_RDWR,NULL)))
3786                      == NULL)
3787                         errx(1,"%s", cam_errbuf);
3788         }
3789 #endif /* MINIMALISTIC */
3790
3791         /*
3792          * Reset optind to 2, and reset getopt, so these routines can parse
3793          * the arguments again.
3794          */
3795         optind = optstart;
3796         optreset = 1;
3797
3798         switch(cmdlist) {
3799 #ifndef MINIMALISTIC
3800                 case CAM_CMD_DEVLIST:
3801                         error = getdevlist(cam_dev);
3802                         break;
3803 #endif /* MINIMALISTIC */
3804                 case CAM_CMD_DEVTREE:
3805                         error = getdevtree();
3806                         break;
3807 #ifndef MINIMALISTIC
3808                 case CAM_CMD_TUR:
3809                         error = testunitready(cam_dev, retry_count, timeout, 0);
3810                         break;
3811                 case CAM_CMD_INQUIRY:
3812                         error = scsidoinquiry(cam_dev, argc, argv, combinedopt,
3813                                               retry_count, timeout);
3814                         break;
3815                 case CAM_CMD_STARTSTOP:
3816                         error = scsistart(cam_dev, arglist & CAM_ARG_START_UNIT,
3817                                           arglist & CAM_ARG_EJECT, retry_count,
3818                                           timeout);
3819                         break;
3820 #endif /* MINIMALISTIC */
3821                 case CAM_CMD_RESCAN:
3822                         error = dorescan_or_reset(argc, argv, 1);
3823                         break;
3824                 case CAM_CMD_RESET:
3825                         error = dorescan_or_reset(argc, argv, 0);
3826                         break;
3827 #ifndef MINIMALISTIC
3828                 case CAM_CMD_READ_DEFECTS:
3829                         error = readdefects(cam_dev, argc, argv, combinedopt,
3830                                             retry_count, timeout);
3831                         break;
3832                 case CAM_CMD_MODE_PAGE:
3833                         modepage(cam_dev, argc, argv, combinedopt,
3834                                  retry_count, timeout);
3835                         break;
3836                 case CAM_CMD_SCSI_CMD:
3837                         error = scsicmd(cam_dev, argc, argv, combinedopt,
3838                                         retry_count, timeout);
3839                         break;
3840                 case CAM_CMD_DEBUG:
3841                         error = camdebug(argc, argv, combinedopt);
3842                         break;
3843                 case CAM_CMD_TAG:
3844                         error = tagcontrol(cam_dev, argc, argv, combinedopt);
3845                         break;
3846                 case CAM_CMD_RATE:
3847                         error = ratecontrol(cam_dev, retry_count, timeout,
3848                                             argc, argv, combinedopt);
3849                         break;
3850                 case CAM_CMD_FORMAT:
3851                         error = scsiformat(cam_dev, argc, argv,
3852                                            combinedopt, retry_count, timeout);
3853                         break;
3854                 case CAM_CMD_REPORTLUNS:
3855                         error = scsireportluns(cam_dev, argc, argv,
3856                                                combinedopt, retry_count,
3857                                                timeout);
3858                         break;
3859 #endif /* MINIMALISTIC */
3860                 case CAM_CMD_USAGE:
3861                         usage(1);
3862                         break;
3863                 default:
3864                         usage(0);
3865                         error = 1;
3866                         break;
3867         }
3868
3869         if (cam_dev != NULL)
3870                 cam_close_device(cam_dev);
3871
3872         exit(error);
3873 }