Merge branch 'vendor/GCC44' into gcc442
[dragonfly.git] / usr.sbin / mlxcontrol / util.c
1 /*-
2  * Copyright (c) 1999 Michael Smith
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/usr.sbin/mlxcontrol/util.c,v 1.2.2.1 2000/04/24 19:44:47 msmith Exp $
27  *      $DragonFly: src/usr.sbin/mlxcontrol/util.c,v 1.3 2003/08/08 04:18:46 dillon Exp $
28  */
29
30 #include <sys/types.h>
31 #include <stdio.h>
32 #include <paths.h>
33 #include <string.h>
34
35 #include <dev/raid/mlx/mlxio.h>
36 #include <dev/raid/mlx/mlxreg.h>
37
38 #include "mlxcontrol.h"
39
40 /********************************************************************************
41  * Various name-producing and -parsing functions
42  */
43
44 /* return path of controller (unit) */
45 char *
46 ctrlrpath(int unit)
47 {
48     static char buf[32];
49     
50     sprintf(buf, "%s%s", _PATH_DEV, ctrlrname(unit));
51     return(buf);
52 }
53
54 /* return name of controller (unit) */
55 char *
56 ctrlrname(int unit)
57 {
58     static char buf[32];
59     
60     sprintf(buf, "mlx%d", unit);
61     return(buf);
62 }
63
64 /* return path of drive (unit) */
65 char *
66 drivepath(int unit)
67 {
68     static char buf[32];
69     
70     sprintf(buf, "%s%s", _PATH_DEV, drivename(unit));
71     return(buf);
72 }
73
74 /* return name of drive (unit) */
75 char *
76 drivename(int unit)
77 {
78     static char buf[32];
79     
80     sprintf(buf, "mlxd%d", unit);
81     return(buf);
82 }
83
84 /* get controller unit number from name in (str) */
85 int
86 ctrlrunit(char *str)
87 {
88     int         unit;
89     
90     if (sscanf(str, "mlx%d", &unit) == 1)
91         return(unit);
92     return(-1);
93 }
94
95 /* get drive unit number from name in (str) */
96 int
97 driveunit(char *str)
98 {
99     int         unit;
100     
101     if (sscanf(str, "mlxd%d", &unit) == 1)
102         return(unit);
103     return(-1);
104 }
105
106 /********************************************************************************
107  * Standardised output of various data structures.
108  */
109
110 void
111 mlx_print_phys_drv(struct mlx_phys_drv *drv, int chn, int targ, char *prefix, int verbose)
112 {
113     char        *type, *device, *vendor, *revision;
114
115     switch(drv->pd_flags2 & 0x03) {
116     case MLX_PHYS_DRV_DISK:
117         type = "disk";
118         break;
119     case MLX_PHYS_DRV_SEQUENTIAL:
120         type = "tape";
121         break;
122     case MLX_PHYS_DRV_CDROM:
123         type= "cdrom";
124         break;
125     case MLX_PHYS_DRV_OTHER:
126     default:
127         type = "unknown";
128         break;
129     }
130     printf("%s%s%02d%02d ", prefix, type, chn, targ);
131     switch(drv->pd_status) {
132     case MLX_PHYS_DRV_DEAD:
133         printf(" (dead)       ");
134         break;
135     case MLX_PHYS_DRV_WRONLY:
136         printf(" (write-only) ");
137         break;
138     case MLX_PHYS_DRV_ONLINE:
139         printf(" (online)     ");
140         break;
141     case MLX_PHYS_DRV_STANDBY:
142         printf(" (standby)    ");
143         break;
144     default:
145         printf(" (0x%02x)   ", drv->pd_status);
146     }
147     printf("\n");
148     
149     if (verbose) {
150         
151         printf("%s   ", prefix);
152         if (!mlx_scsi_inquiry(0, chn, targ, &vendor, &device, &revision)) {
153             printf("'%8.8s' '%16.16s' '%4.4s'", vendor, device, revision);
154         } else {
155             printf("<IDENTIFY FAILED>");
156         }
157     
158         printf(" %dMB ", drv->pd_config_size / 2048);
159     
160         if (drv->pd_flags2 & MLX_PHYS_DRV_FAST20) {
161             printf(" fast20");
162         } else if (drv->pd_flags2 & MLX_PHYS_DRV_FAST) {
163             printf(" fast");
164         }
165         if (drv->pd_flags2 & MLX_PHYS_DRV_WIDE)
166             printf(" wide");
167         if (drv->pd_flags2 & MLX_PHYS_DRV_SYNC)
168             printf(" sync");
169         if (drv->pd_flags2 & MLX_PHYS_DRV_TAG)
170             printf(" tag-enabled");
171         printf("\n");
172     }
173 }