02a00286dc3e650d54cef6fc8d68a4c521ccf1bb
[dragonfly.git] / usr.sbin / dpt / dpt_softc / dpt_softc.c
1 /*
2  *       Copyright (c) 1997 by Simon Shapiro
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  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/usr.sbin/dpt/dpt_softc/dpt_softc.c,v 1.3.2.1 2001/07/24 09:49:45 dd Exp $
30  * $DragonFly: src/usr.sbin/dpt/dpt_softc/dpt_softc.c,v 1.2 2003/06/17 04:29:53 dillon Exp $
31  */
32
33 /* dpt_softc.c:  Dunp a DPT control structure */
34
35 #include <stdlib.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <sys/time.h>
41 #include <sys/stat.h>
42 #include <sys/queue.h>
43 #include <sys/ioctl.h>
44 #include <scsi/scsi_all.h>
45 #include <scsi/scsi_message.h>
46 #include <scsi/scsiconf.h>
47
48 /*
49  * The following two defines alter the size and composition of dpt_softc_t.
50  * If useland does not match the kenel, disaster will ensue.
51  * Since we do not know how to pick up kernel options from here,
52  * and since we always use these options, we will enable them here.
53  *
54  * If you build a kernel without these options, edit here and recompile.
55  */
56 #define DPT_MEASURE_PERFORMANCE
57
58 #include <sys/dpt.h>
59
60 static char i2bin_bitmap[48];   /* Used for binary dump of registers */
61
62 char    *
63 i2bin(unsigned int no, int length)
64 {
65     int ndx, rind;
66     
67     for (ndx = 0, rind = 0; ndx < 32; ndx++, rind++) {
68         i2bin_bitmap[rind] = (((no << ndx) & 0x80000000) ? '1' : '0');
69         
70         if (((ndx % 4) == 3))
71             i2bin_bitmap[++rind] = ' ';
72     }
73         
74     if ((ndx % 4) == 3)
75         i2bin_bitmap[rind - 1] = '\0';
76     else
77         i2bin_bitmap[rind] = '\0';
78     
79     switch (length) {
80     case 8:
81         return (i2bin_bitmap + 30);
82         break;
83     case 16:
84         return (i2bin_bitmap + 20);
85         break;
86     case 24:
87         return (i2bin_bitmap + 10);
88         break;
89     case 32:
90         return (i2bin_bitmap);
91     default:
92         return ("i2bin: Invalid length Specs");
93         break;
94     }
95 }
96 int
97 main(int argc, char **argv, char **argp)
98 {
99     dpt_user_softc_t udpt;
100     int result;
101     int fd;
102
103     if ( (fd = open(argv[1], O_RDWR, S_IRUSR | S_IWUSR)) == -1 ) {
104         (void)fprintf(stderr, "%s ERROR:  Failed to open \"%s\" - %s\n",
105                       argv[0], argv[1], strerror(errno));
106         exit(1);
107     }
108
109     if ( (result = ioctl(fd, DPT_IOCTL_SOFTC, &udpt)) != 0 ) {
110                 (void)fprintf(stderr, "%s ERROR:  Failed to send IOCTL %lx - %s\n",
111                                           argv[0], DPT_IOCTL_SOFTC,
112                                           strerror(errno));
113                 exit(2);
114     }
115
116     (void)fprintf(stdout, "Counters:%d:%d:%d:%d:%d:%d:%d\n",
117                   udpt.total_ccbs_count,
118                   udpt.free_ccbs_count,
119                   udpt.waiting_ccbs_count,
120                   udpt.submitted_ccbs_count,
121                   udpt.completed_ccbs_count,
122                   udpt.commands_processed,
123                   udpt.lost_interrupts);
124
125     (void)fprintf(stdout, "Queue Status:%s\n",
126                   i2bin(udpt.queue_status, sizeof(udpt.queue_status) * 8));
127     
128     (void)fprintf(stdout, "Free lock:%s\n",
129                   i2bin(udpt.free_lock, sizeof(udpt.free_lock) * 8));
130     
131     (void)fprintf(stdout, "Waiting lock:%s\n",
132                   i2bin(udpt.waiting_lock, sizeof(udpt.waiting_lock) * 8));
133     
134     (void)fprintf(stdout, "Submitted lock:%s\n",
135                   i2bin(udpt.submitted_lock, sizeof(udpt.submitted_lock) * 8));
136     
137     (void)fprintf(stdout, "Completed lock:%s\n",
138                   i2bin(udpt.completed_lock, sizeof(udpt.completed_lock) * 8));
139     
140     (void)fprintf(stdout, "Configuration:%s:%d:%d:%d:%x:%d:%d\n",
141                   udpt.handle_interrupts ? "Yes" : "No",
142                   udpt.max_id,
143                   udpt.max_lun,
144                   udpt.channels,
145                   udpt.io_base,
146                   udpt.irq,
147                   udpt.dma_channel);
148     
149     (void)fprintf(stdout, "ID:%x:%x:%s:%s:%s:%s:%x\n",
150                   udpt.board_data.deviceType,
151                   udpt.board_data.rm_dtq,
152                   udpt.board_data.vendor,
153                   udpt.board_data.modelNum,
154                   udpt.board_data.firmware,
155                   udpt.board_data.protocol,
156                   udpt.EATA_revision);
157
158     (void)fprintf(stdout,"Capabilities:%x:%d:%s:%s:%s:%s:%s\n",
159                   udpt.bustype,
160                   udpt.channels,
161                   i2bin((u_int32_t)udpt.state, sizeof(udpt.state) * 8),
162                   udpt.primary ? "Yes" : "No",
163                   udpt.more_support ? "Yes" : "No",
164                   udpt.immediate_support ? "Yes" : "No",
165                   udpt.broken_INQUIRY ? "Yes" : "No");
166
167     (void)fprintf(stdout,"More Config:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d\n",
168                   udpt.resetlevel[0],
169                   udpt.resetlevel[1],
170                   udpt.resetlevel[2],
171                   udpt.cplen,           
172                   udpt.cppadlen,
173                   udpt.queuesize,
174                   udpt.sgsize,
175                   udpt.hostid[0],
176                   udpt.hostid[1],
177                   udpt.hostid[2]);
178
179     (void)fprintf(stdout,"Cache:%s:%d\n",
180                   (udpt.cache_type == DPT_NO_CACHE)
181                   ? "None"
182                   : (udpt.cache_type == DPT_CACHE_WRITETHROUGH)
183                   ? "Write-Through" : "Write-Back",
184                   udpt.cache_size);
185
186     return(0);
187 }