xref: /freebsd/usr.sbin/sesutil/eltsub.c (revision 41059135ce931c0f1014a999ffabc6bc470ce856)
1 /* $FreeBSD$ */
2 /*
3  * Copyright (c) 2000 by Matthew Jacob
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer,
11  *    without modification, immediately at the beginning of the file.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * Alternatively, this software may be distributed under the terms of the
16  * the GNU Public License ("GPL").
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * Matthew Jacob
31  * Feral Software
32  * mjacob@feral.com
33  */
34 
35 #include <sys/endian.h>
36 #include <sys/types.h>
37 #include <sys/sbuf.h>
38 
39 #include <err.h>
40 #include <stddef.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <cam/scsi/scsi_enc.h>
44 
45 #include "eltsub.h"
46 
47 /*
48  * offset by +20 degrees.
49  * The range of the value expresses a temperature between -19 and +235 degrees
50  * Celsius. A value of 00h is reserved.
51  */
52 #define TEMPERATURE_OFFSET 20
53 
54 char *
55 geteltnm(int type)
56 {
57 	static char rbuf[132];
58 
59 	switch (type) {
60 	case ELMTYP_UNSPECIFIED:
61 		sprintf(rbuf, "Unspecified");
62 		break;
63 	case ELMTYP_DEVICE:
64 		sprintf(rbuf, "Device Slot");
65 		break;
66 	case ELMTYP_POWER:
67 		sprintf(rbuf, "Power Supply");
68 		break;
69 	case ELMTYP_FAN:
70 		sprintf(rbuf, "Cooling");
71 		break;
72 	case ELMTYP_THERM:
73 		sprintf(rbuf, "Temperature Sensors");
74 		break;
75 	case ELMTYP_DOORLOCK:
76 		sprintf(rbuf, "Door Lock");
77 		break;
78 	case ELMTYP_ALARM:
79 		sprintf(rbuf, "Audible alarm");
80 		break;
81 	case ELMTYP_ESCC:
82 		sprintf(rbuf, "Enclosure Services Controller Electronics");
83 		break;
84 	case ELMTYP_SCC:
85 		sprintf(rbuf, "SCC Controller Electronics");
86 		break;
87 	case ELMTYP_NVRAM:
88 		sprintf(rbuf, "Nonvolatile Cache");
89 		break;
90 	case ELMTYP_INV_OP_REASON:
91 		sprintf(rbuf, "Invalid Operation Reason");
92 		break;
93 	case ELMTYP_UPS:
94 		sprintf(rbuf, "Uninterruptible Power Supply");
95 		break;
96 	case ELMTYP_DISPLAY:
97 		sprintf(rbuf, "Display");
98 		break;
99 	case ELMTYP_KEYPAD:
100 		sprintf(rbuf, "Key Pad Entry");
101 		break;
102 	case ELMTYP_ENCLOSURE:
103 		sprintf(rbuf, "Enclosure");
104 		break;
105 	case ELMTYP_SCSIXVR:
106 		sprintf(rbuf, "SCSI Port/Transceiver");
107 		break;
108 	case ELMTYP_LANGUAGE:
109 		sprintf(rbuf, "Language");
110 		break;
111 	case ELMTYP_COMPORT:
112 		sprintf(rbuf, "Communication Port");
113 		break;
114 	case ELMTYP_VOM:
115 		sprintf(rbuf, "Voltage Sensor");
116 		break;
117 	case ELMTYP_AMMETER:
118 		sprintf(rbuf, "Current Sensor");
119 		break;
120 	case ELMTYP_SCSI_TGT:
121 		sprintf(rbuf, "SCSI Target Port");
122 		break;
123 	case ELMTYP_SCSI_INI:
124 		sprintf(rbuf, "SCSI Initiator Port");
125 		break;
126 	case ELMTYP_SUBENC:
127 		sprintf(rbuf, "Simple Subenclosure");
128 		break;
129 	case ELMTYP_ARRAY_DEV:
130 		sprintf(rbuf, "Array Device Slot");
131 		break;
132 	case ELMTYP_SAS_EXP:
133 		sprintf(rbuf, "SAS Expander");
134 		break;
135 	case ELMTYP_SAS_CONN:
136 		sprintf(rbuf, "SAS Connector");
137 		break;
138 	default:
139 		(void) sprintf(rbuf, "<Type 0x%x>", type);
140 		break;
141 	}
142 	return (rbuf);
143 }
144 
145 char *
146 scode2ascii(u_char code)
147 {
148 	static char rbuf[32];
149 	switch (code & 0xf) {
150 	case SES_OBJSTAT_UNSUPPORTED:
151 		sprintf(rbuf, "Unsupported");
152 		break;
153 	case SES_OBJSTAT_OK:
154 		sprintf(rbuf, "OK");
155 		break;
156 	case SES_OBJSTAT_CRIT:
157 		sprintf(rbuf, "Critical");
158 		break;
159 	case SES_OBJSTAT_NONCRIT:
160 		sprintf(rbuf, "Noncritical");
161 		break;
162 	case SES_OBJSTAT_UNRECOV:
163 		sprintf(rbuf, "Unrecoverable");
164 		break;
165 	case SES_OBJSTAT_NOTINSTALLED:
166 		sprintf(rbuf, "Not Installed");
167 		break;
168 	case SES_OBJSTAT_UNKNOWN:
169 		sprintf(rbuf, "Unknown");
170 		break;
171 	case SES_OBJSTAT_NOTAVAIL:
172 		sprintf(rbuf, "Not Available");
173 		break;
174 	case SES_OBJSTAT_NOACCESS:
175 		sprintf(rbuf, "No Access Allowed");
176 		break;
177 	default:
178 		sprintf(rbuf, "<Status 0x%x>", code & 0xf);
179 		break;
180 	}
181 	return (rbuf);
182 }
183 
184 struct sbuf *
185 stat2sbuf(int eletype, u_char *cstat)
186 {
187 	struct sbuf *buf;
188 
189 	buf = sbuf_new_auto();
190 	if (buf == NULL)
191 		err(EXIT_FAILURE, "sbuf_new_auto()");
192 
193 	if (cstat[0] & 0x40)
194 		sbuf_printf(buf, "\t\t- Predicted Failure\n");
195 	if (cstat[0] & 0x20)
196 		sbuf_printf(buf, "\t\t- Disabled\n");
197 	if (cstat[0] & 0x10)
198 		sbuf_printf(buf, "\t\t- Swapped\n");
199 	switch (eletype) {
200 	case ELMTYP_DEVICE:
201 		if (cstat[2] & 0x02)
202 			sbuf_printf(buf, "\t\t- LED=locate\n");
203 		if (cstat[2] & 0x20)
204 			sbuf_printf(buf, "\t\t- LED=fault\n");
205 		break;
206 	case ELMTYP_ARRAY_DEV:
207 		if (cstat[2] & 0x02)
208 			sbuf_printf(buf, "\t\t- LED=locate\n");
209 		if (cstat[2] & 0x20)
210 			sbuf_printf(buf, "\t\t- LED=fault\n");
211 		break;
212 	case ELMTYP_FAN:
213 		sbuf_printf(buf, "\t\t- Speed: %d rpm\n",
214 		    (((0x7 & cstat[1]) << 8) + cstat[2]) * 10);
215 		break;
216 	case ELMTYP_THERM:
217 		if (cstat[2]) {
218 			sbuf_printf(buf, "\t\t- Temperature: %d C\n",
219 			    cstat[2] - TEMPERATURE_OFFSET);
220 		} else {
221 			sbuf_printf(buf, "\t\t- Temperature: -reserved-\n");
222 		}
223 		break;
224 	case ELMTYP_VOM:
225 		sbuf_printf(buf, "\t\t- Voltage: %.2f V\n",
226 		    be16dec(cstat + 2) / 100.0);
227 		break;
228 	}
229 	sbuf_finish(buf);
230 	return (buf);
231 }
232