amrctl.c revision 1.7 1 1.1 bouyer /*-
2 1.1 bouyer * Copyright (c) 2002, Pierre David <Pierre.David (at) crc.u-strasbg.fr>
3 1.1 bouyer * Copyright (c) 2006, Jung-uk Kim <jkim (at) FreeBSD.org>
4 1.1 bouyer * All rights reserved.
5 1.1 bouyer *
6 1.1 bouyer * Redistribution and use in source and binary forms, with or without
7 1.1 bouyer * modification, are permitted provided that the following conditions
8 1.1 bouyer * are met:
9 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
10 1.1 bouyer * notice unmodified, this list of conditions, and the following
11 1.1 bouyer * disclaimer.
12 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.1 bouyer * documentation and/or other materials provided with the distribution.
15 1.1 bouyer *
16 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 bouyer */
27 1.1 bouyer
28 1.1 bouyer #include <sys/cdefs.h>
29 1.1 bouyer
30 1.1 bouyer #include <stdio.h>
31 1.1 bouyer #include <stdlib.h>
32 1.1 bouyer #include <string.h>
33 1.1 bouyer #include <fcntl.h>
34 1.6 jakllsch #include <err.h>
35 1.1 bouyer #include <errno.h>
36 1.1 bouyer #include <unistd.h>
37 1.1 bouyer
38 1.1 bouyer #include <sys/ioctl.h>
39 1.1 bouyer
40 1.1 bouyer #include <machine/param.h>
41 1.1 bouyer
42 1.1 bouyer #include <dev/pci/amrio.h>
43 1.1 bouyer #include <dev/pci/amrreg.h>
44 1.1 bouyer
45 1.1 bouyer #define NATTEMPTS 5
46 1.1 bouyer #define SLEEPTIME 100000 /* microseconds */
47 1.1 bouyer
48 1.3 joerg static int nattempts = NATTEMPTS; /* # of attempts before giving up */
49 1.3 joerg static int sleeptime = SLEEPTIME; /* between attempts, in ms */
50 1.1 bouyer
51 1.1 bouyer #define AMR_BUFSIZE 1024
52 1.1 bouyer
53 1.3 joerg static int enq_result = AMR_STATUS_FAILED;
54 1.3 joerg static char enq_buffer[AMR_BUFSIZE];
55 1.1 bouyer
56 1.1 bouyer #define AMR_MAX_NCTRLS 16
57 1.1 bouyer #define AMR_MAX_NSDEVS 16
58 1.1 bouyer
59 1.7 jakllsch static uint8_t nschan = 0;
60 1.1 bouyer
61 1.1 bouyer /*
62 1.1 bouyer * Include lookup tables, and a function to match a code to a string.
63 1.1 bouyer *
64 1.1 bouyer * XXX Lookup tables cannot be included, since they require symbols from
65 1.1 bouyer * amrreg.h which need in turn the _KERNEL define.
66 1.1 bouyer */
67 1.1 bouyer
68 1.1 bouyer /* #define AMR_DEFINE_TABLES */
69 1.1 bouyer /* #include "amr_tables.h" */
70 1.1 bouyer
71 1.7 jakllsch static int amr_ioctl_enquiry(int, uint8_t, uint8_t, uint8_t);
72 1.3 joerg __dead static void usage(const char *);
73 1.3 joerg static int describe_card(int, int, int);
74 1.7 jakllsch static char * describe_property(uint8_t, char *);
75 1.7 jakllsch static const char * describe_state(int, uint8_t);
76 1.3 joerg static void describe_battery(int, int, int, int, int);
77 1.7 jakllsch static void describe_one_volume(int, int, uint32_t, uint8_t, uint8_t);
78 1.7 jakllsch static void describe_one_drive(int, int, uint8_t);
79 1.3 joerg static void describe_drive(int, int, int, int, int);
80 1.1 bouyer
81 1.1 bouyer /*
82 1.1 bouyer * Offsets in an amr_user_ioctl.au_cmd [] array See amrio.h
83 1.1 bouyer */
84 1.1 bouyer
85 1.1 bouyer #define MB_COMMAND 0
86 1.1 bouyer #define MB_CHANNEL 1
87 1.1 bouyer #define MB_PARAM 2
88 1.1 bouyer #define MB_PAD 3
89 1.1 bouyer #define MB_DRIVE 4
90 1.1 bouyer
91 1.1 bouyer #define FIRMWARE_40LD 1
92 1.1 bouyer #define FIRMWARE_8LD 2
93 1.1 bouyer
94 1.4 jakllsch static const struct {
95 1.1 bouyer const char *product;
96 1.2 lukem const uint32_t signature;
97 1.1 bouyer } prodtable[] = {
98 1.1 bouyer { "Series 431", AMR_SIG_431 },
99 1.1 bouyer { "Series 438", AMR_SIG_438 },
100 1.1 bouyer { "Series 762", AMR_SIG_762 },
101 1.1 bouyer { "Integrated HP NetRAID (T5)", AMR_SIG_T5 },
102 1.1 bouyer { "Series 466", AMR_SIG_466 },
103 1.1 bouyer { "Series 467", AMR_SIG_467 },
104 1.1 bouyer { "Integrated HP NetRAID (T7)", AMR_SIG_T7 },
105 1.1 bouyer { "Series 490", AMR_SIG_490 }
106 1.1 bouyer };
107 1.1 bouyer
108 1.4 jakllsch static const struct {
109 1.1 bouyer const int code;
110 1.1 bouyer const char *ifyes, *ifno;
111 1.1 bouyer } proptable[] = {
112 1.1 bouyer { AMR_DRV_WRITEBACK,
113 1.1 bouyer "writeback", "write-through" },
114 1.1 bouyer { AMR_DRV_READHEAD,
115 1.1 bouyer "read-ahead", "no-read-ahead" },
116 1.1 bouyer { AMR_DRV_ADAPTIVE,
117 1.1 bouyer "adaptative-io", "no-adaptative-io" }
118 1.1 bouyer };
119 1.1 bouyer
120 1.4 jakllsch static const struct {
121 1.1 bouyer const int code;
122 1.1 bouyer const char *status;
123 1.1 bouyer } statetable[] = {
124 1.1 bouyer { AMR_DRV_OFFLINE, "offline" },
125 1.1 bouyer { AMR_DRV_DEGRADED, "degraded" },
126 1.1 bouyer { AMR_DRV_OPTIMAL, "optimal" },
127 1.1 bouyer { AMR_DRV_ONLINE, "online" },
128 1.1 bouyer { AMR_DRV_FAILED, "failed" },
129 1.1 bouyer { AMR_DRV_REBUILD, "rebuild" },
130 1.1 bouyer { AMR_DRV_HOTSPARE, "hotspare" }
131 1.1 bouyer };
132 1.1 bouyer
133 1.4 jakllsch static const struct {
134 1.7 jakllsch const uint8_t code;
135 1.1 bouyer const char *status;
136 1.1 bouyer } battable[] = {
137 1.1 bouyer { AMR_BATT_MODULE_MISSING, "not present" },
138 1.1 bouyer { AMR_BATT_LOW_VOLTAGE, "low voltage" },
139 1.1 bouyer { AMR_BATT_TEMP_HIGH, "high temperature" },
140 1.1 bouyer { AMR_BATT_PACK_MISSING, "pack missing" },
141 1.1 bouyer { AMR_BATT_CYCLES_EXCEEDED, "cycle exceeded" }
142 1.1 bouyer };
143 1.1 bouyer
144 1.4 jakllsch static const struct {
145 1.7 jakllsch const uint8_t code;
146 1.1 bouyer const char *status;
147 1.1 bouyer } bcstatble[] = {
148 1.1 bouyer { AMR_BATT_CHARGE_DONE, "charge done" },
149 1.1 bouyer { AMR_BATT_CHARGE_INPROG, "charge in progress" },
150 1.1 bouyer { AMR_BATT_CHARGE_FAIL, "charge failed" }
151 1.1 bouyer };
152 1.1 bouyer
153 1.3 joerg static int
154 1.7 jakllsch amr_ioctl_enquiry(int fd, uint8_t cmd, uint8_t cmdsub, uint8_t cmdqual)
155 1.1 bouyer {
156 1.1 bouyer struct amr_user_ioctl am;
157 1.1 bouyer int r, i;
158 1.1 bouyer
159 1.1 bouyer am.au_cmd[MB_COMMAND] = cmd;
160 1.1 bouyer am.au_cmd[MB_CHANNEL] = cmdsub;
161 1.1 bouyer am.au_cmd[MB_PARAM] = cmdqual;
162 1.1 bouyer am.au_cmd[MB_PAD] = 0;
163 1.1 bouyer am.au_cmd[MB_DRIVE] = 0;
164 1.1 bouyer
165 1.1 bouyer am.au_buffer = enq_buffer;
166 1.1 bouyer am.au_length = AMR_BUFSIZE;
167 1.1 bouyer am.au_direction = AMR_IO_READ;
168 1.1 bouyer am.au_status = 0;
169 1.1 bouyer
170 1.1 bouyer i = 0;
171 1.1 bouyer r = -1;
172 1.1 bouyer while (i < nattempts && r == -1) {
173 1.1 bouyer r = ioctl(fd, AMR_IO_COMMAND, &am);
174 1.1 bouyer if (r == -1) {
175 1.1 bouyer if (errno != EBUSY) {
176 1.6 jakllsch err(EXIT_FAILURE, "ioctl enquiry");
177 1.1 bouyer } else
178 1.1 bouyer usleep(sleeptime);
179 1.1 bouyer }
180 1.1 bouyer i++;
181 1.1 bouyer }
182 1.1 bouyer return am.au_status;
183 1.1 bouyer }
184 1.1 bouyer
185 1.3 joerg static void
186 1.3 joerg usage(const char *prog)
187 1.1 bouyer {
188 1.1 bouyer fprintf(stderr, "usage: %s stat [-a num] [-b] "
189 1.1 bouyer "[-c ctlr|-f dev] [-g] [-l vol]\n\t\t"
190 1.1 bouyer "[-p drive|-s bus[:target]] [-t usec] [-v]\n\n\t"
191 1.1 bouyer "-a num\t\tnumber of retries\n\t"
192 1.1 bouyer "-b\t\tbattery status\n\t"
193 1.1 bouyer "-c ctrl\t\tcontroller ID\n\t"
194 1.1 bouyer "-f dev\t\tdevice path\n\t"
195 1.1 bouyer "-g\t\tprint global parameters\n\t"
196 1.1 bouyer "-l vol\t\tlogical volume ID\n\t"
197 1.1 bouyer "-p drive\tphysical drive ID\n\t"
198 1.1 bouyer "-s bus[:target]\tSCSI bus (and optinal target)\n\t"
199 1.1 bouyer "-t usec\t\tsleep time between retries\n\t"
200 1.1 bouyer "-v\t\tverbose output\n",
201 1.1 bouyer prog);
202 1.1 bouyer exit(1);
203 1.1 bouyer }
204 1.1 bouyer
205 1.1 bouyer /******************************************************************************
206 1.1 bouyer * Card description
207 1.1 bouyer */
208 1.1 bouyer
209 1.3 joerg static int
210 1.1 bouyer describe_card(int fd, int verbosity, int globalparam)
211 1.1 bouyer {
212 1.1 bouyer struct amr_enquiry *ae;
213 1.2 lukem uint32_t cardtype;
214 1.1 bouyer
215 1.1 bouyer /*
216 1.1 bouyer * Try the 40LD firmware interface
217 1.1 bouyer */
218 1.1 bouyer
219 1.1 bouyer enq_result = amr_ioctl_enquiry(fd, AMR_CMD_CONFIG,
220 1.1 bouyer AMR_CONFIG_PRODUCT_INFO, 0);
221 1.1 bouyer if (enq_result == AMR_STATUS_SUCCESS) {
222 1.1 bouyer struct amr_prodinfo *ap;
223 1.1 bouyer
224 1.1 bouyer ap = (struct amr_prodinfo *)enq_buffer;
225 1.1 bouyer nschan = ap->ap_nschan;
226 1.1 bouyer if (globalparam) {
227 1.1 bouyer printf("Product\t\t\t<%.80s>\n", ap->ap_product);
228 1.1 bouyer printf("Firmware\t\t%.16s\n", ap->ap_firmware);
229 1.1 bouyer printf("BIOS\t\t\t%.16s\n", ap->ap_bios);
230 1.1 bouyer printf("SCSI channels\t\t%d\n", ap->ap_nschan);
231 1.1 bouyer printf("Fibre loops\t\t%d\n", ap->ap_fcloops);
232 1.1 bouyer printf("Memory size\t\t%d MB\n", ap->ap_memsize);
233 1.1 bouyer if (verbosity >= 1) {
234 1.1 bouyer printf("Ioctl\t\t\t%d (%s)\n", FIRMWARE_40LD,
235 1.1 bouyer "40LD");
236 1.1 bouyer printf("Signature\t\t0x%08x\n",
237 1.1 bouyer ap->ap_signature);
238 1.1 bouyer printf("Configsig\t\t0x%08x\n",
239 1.1 bouyer ap->ap_configsig);
240 1.1 bouyer printf("Subsystem\t\t0x%04x\n",
241 1.1 bouyer ap->ap_subsystem);
242 1.1 bouyer printf("Subvendor\t\t0x%04x\n",
243 1.1 bouyer ap->ap_subvendor);
244 1.1 bouyer printf("Notify counters\t\t%d\n",
245 1.1 bouyer ap->ap_numnotifyctr);
246 1.1 bouyer }
247 1.1 bouyer }
248 1.1 bouyer return FIRMWARE_40LD;
249 1.1 bouyer }
250 1.1 bouyer /*
251 1.1 bouyer * Try the 8LD firmware interface
252 1.1 bouyer */
253 1.1 bouyer
254 1.1 bouyer enq_result = amr_ioctl_enquiry(fd, AMR_CMD_EXT_ENQUIRY2, 0, 0);
255 1.1 bouyer ae = (struct amr_enquiry *)enq_buffer;
256 1.1 bouyer if (enq_result == AMR_STATUS_SUCCESS) {
257 1.1 bouyer cardtype = ae->ae_signature;
258 1.1 bouyer } else {
259 1.1 bouyer enq_result = amr_ioctl_enquiry(fd, AMR_CMD_ENQUIRY, 0, 0);
260 1.1 bouyer cardtype = 0;
261 1.1 bouyer }
262 1.1 bouyer
263 1.1 bouyer if (enq_result == AMR_STATUS_SUCCESS) {
264 1.1 bouyer
265 1.1 bouyer if (globalparam) {
266 1.1 bouyer const char *product = NULL;
267 1.1 bouyer char bios[100], firmware[100];
268 1.2 lukem size_t i;
269 1.1 bouyer
270 1.5 jakllsch for (i = 0; i < __arraycount(prodtable); i++) {
271 1.1 bouyer if (cardtype == prodtable[i].signature) {
272 1.1 bouyer product = prodtable[i].product;
273 1.1 bouyer break;
274 1.1 bouyer }
275 1.1 bouyer }
276 1.1 bouyer if (product == NULL)
277 1.1 bouyer product = "unknown card signature";
278 1.1 bouyer
279 1.1 bouyer /*
280 1.1 bouyer * HP NetRaid controllers have a special encoding of
281 1.1 bouyer * the firmware and BIOS versions. The AMI version
282 1.1 bouyer * seems to have it as strings whereas the HP version
283 1.1 bouyer * does it with a leading uppercase character and two
284 1.1 bouyer * binary numbers.
285 1.1 bouyer */
286 1.1 bouyer
287 1.1 bouyer if (ae->ae_adapter.aa_firmware[2] >= 'A' &&
288 1.1 bouyer ae->ae_adapter.aa_firmware[2] <= 'Z' &&
289 1.1 bouyer ae->ae_adapter.aa_firmware[1] < ' ' &&
290 1.1 bouyer ae->ae_adapter.aa_firmware[0] < ' ' &&
291 1.1 bouyer ae->ae_adapter.aa_bios[2] >= 'A' &&
292 1.1 bouyer ae->ae_adapter.aa_bios[2] <= 'Z' &&
293 1.1 bouyer ae->ae_adapter.aa_bios[1] < ' ' &&
294 1.1 bouyer ae->ae_adapter.aa_bios[0] < ' ') {
295 1.1 bouyer
296 1.1 bouyer /*
297 1.1 bouyer * looks like we have an HP NetRaid version
298 1.1 bouyer * of the MegaRaid
299 1.1 bouyer */
300 1.1 bouyer
301 1.1 bouyer if (cardtype == AMR_SIG_438) {
302 1.1 bouyer /*
303 1.1 bouyer * the AMI 438 is a NetRaid 3si in
304 1.1 bouyer * HP-land
305 1.1 bouyer */
306 1.1 bouyer product = "HP NetRaid 3si";
307 1.1 bouyer }
308 1.1 bouyer sprintf(firmware, "%c.%02d.%02d",
309 1.1 bouyer ae->ae_adapter.aa_firmware[2],
310 1.1 bouyer ae->ae_adapter.aa_firmware[1],
311 1.1 bouyer ae->ae_adapter.aa_firmware[0]);
312 1.1 bouyer sprintf(bios, "%c.%02d.%02d",
313 1.1 bouyer ae->ae_adapter.aa_bios[2],
314 1.1 bouyer ae->ae_adapter.aa_bios[1],
315 1.1 bouyer ae->ae_adapter.aa_bios[0]);
316 1.1 bouyer } else {
317 1.1 bouyer sprintf(firmware, "%.4s",
318 1.1 bouyer ae->ae_adapter.aa_firmware);
319 1.1 bouyer sprintf(bios, "%.4s", ae->ae_adapter.aa_bios);
320 1.1 bouyer }
321 1.1 bouyer
322 1.1 bouyer printf("Ioctl = %d (%s)\n", FIRMWARE_8LD, "8LD");
323 1.1 bouyer printf("Product =\t<%s>\n", product);
324 1.1 bouyer printf("Firmware =\t%s\n", firmware);
325 1.1 bouyer printf("BIOS =\t%s\n", bios);
326 1.1 bouyer /* printf ("SCSI Channels =\t%d\n", ae->ae_nschan); */
327 1.1 bouyer /* printf ("Fibre Loops =\t%d\n", ae->ae_fcloops); */
328 1.1 bouyer printf("Memory size =\t%d MB\n",
329 1.1 bouyer ae->ae_adapter.aa_memorysize);
330 1.1 bouyer /*
331 1.1 bouyer * printf ("Notify counters =\t%d\n",
332 1.1 bouyer * ae->ae_numnotifyctr) ;
333 1.1 bouyer */
334 1.1 bouyer }
335 1.1 bouyer return FIRMWARE_8LD;
336 1.1 bouyer }
337 1.1 bouyer /*
338 1.1 bouyer * Neither firmware interface succeeded. Abort.
339 1.1 bouyer */
340 1.1 bouyer
341 1.1 bouyer fprintf(stderr, "Firmware interface not supported\n");
342 1.1 bouyer exit(1);
343 1.1 bouyer
344 1.1 bouyer }
345 1.1 bouyer
346 1.3 joerg static char *
347 1.7 jakllsch describe_property(uint8_t prop, char *buffer)
348 1.1 bouyer {
349 1.2 lukem size_t i;
350 1.1 bouyer
351 1.1 bouyer strcpy(buffer, "<");
352 1.5 jakllsch for (i = 0; i < __arraycount(proptable); i++) {
353 1.1 bouyer if (i > 0)
354 1.1 bouyer strcat(buffer, ",");
355 1.1 bouyer if (prop & proptable[i].code)
356 1.1 bouyer strcat(buffer, proptable[i].ifyes);
357 1.1 bouyer else
358 1.1 bouyer strcat(buffer, proptable[i].ifno);
359 1.1 bouyer }
360 1.1 bouyer strcat(buffer, ">");
361 1.1 bouyer
362 1.1 bouyer return buffer;
363 1.1 bouyer }
364 1.1 bouyer
365 1.3 joerg static const char *
366 1.7 jakllsch describe_state(int verbosity, uint8_t state)
367 1.1 bouyer {
368 1.2 lukem size_t i;
369 1.1 bouyer
370 1.1 bouyer if ((AMR_DRV_PREVSTATE(state) == AMR_DRV_CURSTATE(state)) &&
371 1.1 bouyer (AMR_DRV_CURSTATE(state) == AMR_DRV_OFFLINE) && verbosity == 0)
372 1.1 bouyer return NULL;
373 1.1 bouyer
374 1.5 jakllsch for (i = 0; i < __arraycount(statetable); i++)
375 1.1 bouyer if (AMR_DRV_CURSTATE(state) == statetable[i].code)
376 1.1 bouyer return (statetable[i].status);
377 1.1 bouyer
378 1.1 bouyer return NULL;
379 1.1 bouyer }
380 1.1 bouyer
381 1.1 bouyer /******************************************************************************
382 1.1 bouyer * Battery status
383 1.1 bouyer */
384 1.3 joerg static void
385 1.1 bouyer describe_battery(int fd, int verbosity, int fwint, int bflags, int globalparam)
386 1.1 bouyer {
387 1.7 jakllsch uint8_t batt_status;
388 1.2 lukem size_t i;
389 1.1 bouyer
390 1.1 bouyer if (fwint == FIRMWARE_40LD) {
391 1.1 bouyer enq_result = amr_ioctl_enquiry(fd, AMR_CMD_CONFIG,
392 1.1 bouyer AMR_CONFIG_ENQ3, AMR_CONFIG_ENQ3_SOLICITED_FULL);
393 1.1 bouyer if (enq_result == AMR_STATUS_SUCCESS) {
394 1.1 bouyer struct amr_enquiry3 *ae3;
395 1.1 bouyer
396 1.1 bouyer ae3 = (struct amr_enquiry3 *)enq_buffer;
397 1.1 bouyer if (bflags || globalparam) {
398 1.1 bouyer batt_status = ae3->ae_batterystatus;
399 1.1 bouyer printf("Battery status\t\t");
400 1.5 jakllsch for (i = 0; i < __arraycount(battable); i++) {
401 1.1 bouyer if (batt_status & battable[i].code)
402 1.1 bouyer printf("%s, ", battable[i].status);
403 1.1 bouyer }
404 1.1 bouyer if (!(batt_status &
405 1.1 bouyer (AMR_BATT_MODULE_MISSING|AMR_BATT_PACK_MISSING))) {
406 1.5 jakllsch for (i = 0;
407 1.5 jakllsch i < __arraycount(bcstatble); i++)
408 1.1 bouyer if (bcstatble[i].code ==
409 1.1 bouyer (batt_status & AMR_BATT_CHARGE_MASK))
410 1.1 bouyer printf("%s", bcstatble[i].status);
411 1.1 bouyer } else
412 1.1 bouyer printf("charge unknown");
413 1.1 bouyer if (verbosity)
414 1.1 bouyer printf(" (0x%02x)", batt_status);
415 1.1 bouyer printf("\n");
416 1.1 bouyer }
417 1.1 bouyer }
418 1.1 bouyer } else if (fwint == FIRMWARE_8LD) {
419 1.1 bouyer /* Nothing to do here. */
420 1.1 bouyer return;
421 1.1 bouyer } else {
422 1.1 bouyer fprintf(stderr, "Firmware interface not supported.\n");
423 1.1 bouyer exit(1);
424 1.1 bouyer }
425 1.1 bouyer
426 1.1 bouyer return;
427 1.1 bouyer }
428 1.1 bouyer
429 1.1 bouyer /******************************************************************************
430 1.1 bouyer * Logical volumes
431 1.1 bouyer */
432 1.1 bouyer
433 1.3 joerg static void
434 1.1 bouyer describe_one_volume(int ldrv, int verbosity,
435 1.7 jakllsch uint32_t size, uint8_t state, uint8_t prop)
436 1.1 bouyer {
437 1.1 bouyer float szgb;
438 1.1 bouyer int raid_level;
439 1.1 bouyer char propstr[MAXPATHLEN];
440 1.1 bouyer const char *statestr;
441 1.1 bouyer
442 1.1 bouyer szgb = ((float)size) / (1024 * 1024 * 2); /* size in GB */
443 1.1 bouyer
444 1.1 bouyer raid_level = prop & AMR_DRV_RAID_MASK;
445 1.1 bouyer
446 1.1 bouyer printf("Logical volume %d\t", ldrv);
447 1.1 bouyer statestr = describe_state(verbosity, state);
448 1.1 bouyer printf("%s ", statestr);
449 1.1 bouyer printf("(%.2f GB, RAID%d", szgb, raid_level);
450 1.1 bouyer if (verbosity >= 1) {
451 1.1 bouyer describe_property(prop, propstr);
452 1.1 bouyer printf(" %s", propstr);
453 1.1 bouyer }
454 1.1 bouyer printf(")\n");
455 1.1 bouyer }
456 1.1 bouyer
457 1.1 bouyer /******************************************************************************
458 1.1 bouyer * Physical drives
459 1.1 bouyer */
460 1.1 bouyer
461 1.3 joerg static void
462 1.7 jakllsch describe_one_drive(int pdrv, int verbosity, uint8_t state)
463 1.1 bouyer {
464 1.1 bouyer const char *statestr;
465 1.1 bouyer
466 1.1 bouyer statestr = describe_state(verbosity, state);
467 1.1 bouyer if (statestr) {
468 1.1 bouyer if (nschan > 0)
469 1.1 bouyer printf("Physical drive %d:%d\t%s\n",
470 1.1 bouyer pdrv / AMR_MAX_NSDEVS, pdrv % AMR_MAX_NSDEVS,
471 1.1 bouyer statestr);
472 1.1 bouyer else
473 1.1 bouyer printf("Physical drive %d:\t%s\n", pdrv, statestr);
474 1.1 bouyer }
475 1.1 bouyer }
476 1.1 bouyer
477 1.3 joerg static void
478 1.1 bouyer describe_drive(int verbosity, int fwint, int ldrv, int sbus, int sdev)
479 1.1 bouyer {
480 1.1 bouyer int drv, pdrv = -1;
481 1.1 bouyer
482 1.1 bouyer if (sbus > -1 && sdev > -1)
483 1.1 bouyer pdrv = (sbus * AMR_MAX_NSDEVS) + sdev;
484 1.1 bouyer if (nschan != 0) {
485 1.1 bouyer if (sbus > -1 && sbus >= nschan) {
486 1.1 bouyer fprintf(stderr, "SCSI channel %d does not exist.\n", sbus);
487 1.1 bouyer exit(1);
488 1.1 bouyer } else if (sdev > -1 && sdev >= AMR_MAX_NSDEVS) {
489 1.1 bouyer fprintf(stderr, "SCSI device %d:%d does not exist.\n",
490 1.1 bouyer sbus, sdev);
491 1.1 bouyer exit(1);
492 1.1 bouyer }
493 1.1 bouyer }
494 1.1 bouyer if (fwint == FIRMWARE_40LD) {
495 1.1 bouyer if (enq_result == AMR_STATUS_SUCCESS) {
496 1.1 bouyer struct amr_enquiry3 *ae3;
497 1.1 bouyer
498 1.1 bouyer ae3 = (struct amr_enquiry3 *)enq_buffer;
499 1.1 bouyer if ((ldrv < 0 && sbus < 0) || ldrv >= 0) {
500 1.1 bouyer if (ldrv >= ae3->ae_numldrives) {
501 1.1 bouyer fprintf(stderr, "Logical volume %d "
502 1.1 bouyer "does not exist.\n", ldrv);
503 1.1 bouyer exit(1);
504 1.1 bouyer }
505 1.1 bouyer if (ldrv < 0) {
506 1.1 bouyer for (drv = 0;
507 1.1 bouyer drv < ae3->ae_numldrives;
508 1.1 bouyer drv++)
509 1.1 bouyer describe_one_volume(drv,
510 1.1 bouyer verbosity,
511 1.1 bouyer ae3->ae_drivesize[drv],
512 1.1 bouyer ae3->ae_drivestate[drv],
513 1.1 bouyer ae3->ae_driveprop[drv]);
514 1.1 bouyer } else {
515 1.1 bouyer describe_one_volume(ldrv,
516 1.1 bouyer verbosity,
517 1.1 bouyer ae3->ae_drivesize[ldrv],
518 1.1 bouyer ae3->ae_drivestate[ldrv],
519 1.1 bouyer ae3->ae_driveprop[ldrv]);
520 1.1 bouyer }
521 1.1 bouyer }
522 1.1 bouyer if ((ldrv < 0 && sbus < 0) || sbus >= 0) {
523 1.1 bouyer if (pdrv >= AMR_40LD_MAXPHYSDRIVES ||
524 1.1 bouyer (nschan != 0 && pdrv >= (nschan * AMR_MAX_NSDEVS))) {
525 1.1 bouyer fprintf(stderr, "Physical drive %d "
526 1.1 bouyer "is out of range.\n", pdrv);
527 1.1 bouyer exit(1);
528 1.1 bouyer }
529 1.1 bouyer if (sbus < 0) {
530 1.1 bouyer for (drv = 0;
531 1.1 bouyer drv < AMR_40LD_MAXPHYSDRIVES;
532 1.1 bouyer drv++) {
533 1.1 bouyer if (nschan != 0 &&
534 1.1 bouyer drv >= (nschan * AMR_MAX_NSDEVS))
535 1.1 bouyer break;
536 1.1 bouyer describe_one_drive(drv,
537 1.1 bouyer verbosity,
538 1.1 bouyer ae3->ae_pdrivestate[drv]);
539 1.1 bouyer }
540 1.1 bouyer } else if (sdev < 0) {
541 1.1 bouyer for (drv = sbus * AMR_MAX_NSDEVS;
542 1.1 bouyer drv < ((sbus + 1) * AMR_MAX_NSDEVS);
543 1.1 bouyer drv++) {
544 1.1 bouyer if (nschan != 0 &&
545 1.1 bouyer drv >= (nschan * AMR_MAX_NSDEVS))
546 1.1 bouyer break;
547 1.1 bouyer describe_one_drive(drv,
548 1.1 bouyer verbosity,
549 1.1 bouyer ae3->ae_pdrivestate[drv]);
550 1.1 bouyer }
551 1.1 bouyer } else {
552 1.1 bouyer if (nschan != 0 &&
553 1.1 bouyer pdrv < (nschan * AMR_MAX_NSDEVS))
554 1.1 bouyer describe_one_drive(pdrv, 1,
555 1.1 bouyer ae3->ae_pdrivestate[pdrv]);
556 1.1 bouyer }
557 1.1 bouyer }
558 1.1 bouyer }
559 1.1 bouyer } else if (fwint == FIRMWARE_8LD) {
560 1.1 bouyer /* Nothing to do here. */
561 1.1 bouyer return;
562 1.1 bouyer } else {
563 1.1 bouyer fprintf(stderr, "Firmware interface not supported.\n");
564 1.1 bouyer exit(1);
565 1.1 bouyer }
566 1.1 bouyer }
567 1.1 bouyer
568 1.1 bouyer /******************************************************************************
569 1.1 bouyer * Main function
570 1.1 bouyer */
571 1.1 bouyer
572 1.1 bouyer int
573 1.1 bouyer main(int argc, char *argv[])
574 1.1 bouyer {
575 1.1 bouyer int i;
576 1.1 bouyer int fd = -1;
577 1.1 bouyer int globalparam = 0, verbosity = 0;
578 1.1 bouyer int bflags = 0, fflags = 0, sflags = 0;
579 1.1 bouyer int lvolno = -1, physno = -1;
580 1.1 bouyer int sbusno = -1, targetno = -1;
581 1.1 bouyer char filename[MAXPATHLEN];
582 1.1 bouyer char sdev[MAXPATHLEN];
583 1.1 bouyer char *pdev;
584 1.1 bouyer
585 1.1 bouyer extern char *optarg;
586 1.1 bouyer extern int optind;
587 1.1 bouyer
588 1.1 bouyer /*
589 1.1 bouyer * Parse arguments
590 1.1 bouyer */
591 1.1 bouyer if (argc < 2)
592 1.1 bouyer usage(argv[0]);
593 1.1 bouyer if (strcmp(argv[1], "stat") != 0) /* only stat implemented for now */
594 1.1 bouyer usage(argv[0]);
595 1.1 bouyer
596 1.1 bouyer optind = 2;
597 1.1 bouyer while ((i = getopt(argc, argv, "a:bc:f:gl:p:s:t:v")) != -1)
598 1.1 bouyer switch (i) {
599 1.1 bouyer case 'a':
600 1.1 bouyer nattempts = atoi(optarg);
601 1.1 bouyer break;
602 1.1 bouyer case 'b':
603 1.1 bouyer bflags++;
604 1.1 bouyer break;
605 1.1 bouyer case 'f':
606 1.1 bouyer snprintf(filename, MAXPATHLEN, "%s", optarg);
607 1.1 bouyer filename[MAXPATHLEN - 1] = '\0';
608 1.1 bouyer fflags++;
609 1.1 bouyer break;
610 1.1 bouyer case 'g':
611 1.1 bouyer globalparam = 1;
612 1.1 bouyer break;
613 1.1 bouyer case 'l':
614 1.1 bouyer lvolno = atoi(optarg);
615 1.1 bouyer break;
616 1.1 bouyer case 'p':
617 1.1 bouyer physno = atoi(optarg);
618 1.1 bouyer break;
619 1.1 bouyer case 's':
620 1.1 bouyer snprintf(sdev, MAXPATHLEN, "%s", optarg);
621 1.1 bouyer sdev[MAXPATHLEN - 1] = '\0';
622 1.1 bouyer sflags++;
623 1.1 bouyer break;
624 1.1 bouyer case 't':
625 1.1 bouyer sleeptime = atoi(optarg);
626 1.1 bouyer break;
627 1.1 bouyer case 'v':
628 1.1 bouyer verbosity++;
629 1.1 bouyer break;
630 1.1 bouyer case '?':
631 1.1 bouyer default:
632 1.1 bouyer usage(argv[0]);
633 1.1 bouyer }
634 1.1 bouyer argc -= optind;
635 1.1 bouyer argv += optind;
636 1.1 bouyer
637 1.1 bouyer if (argc != 0)
638 1.1 bouyer usage(argv[0]);
639 1.1 bouyer
640 1.1 bouyer if (!fflags) {
641 1.1 bouyer snprintf(filename, MAXPATHLEN, "/dev/amr0");
642 1.1 bouyer }
643 1.1 bouyer
644 1.1 bouyer fd = open(filename, O_RDONLY);
645 1.1 bouyer if (fd == -1) {
646 1.6 jakllsch err(EXIT_FAILURE, "open");
647 1.1 bouyer }
648 1.1 bouyer if (ioctl(fd, AMR_IO_VERSION, &i) == -1) {
649 1.6 jakllsch err(EXIT_FAILURE, "ioctl version");
650 1.1 bouyer }
651 1.1 bouyer
652 1.1 bouyer if (sflags) {
653 1.1 bouyer if(physno > -1)
654 1.1 bouyer usage(argv[0]);
655 1.1 bouyer else {
656 1.1 bouyer sbusno = atoi(sdev);
657 1.1 bouyer if ((pdev = index(sdev, ':')))
658 1.1 bouyer targetno = atoi(++pdev);
659 1.1 bouyer }
660 1.1 bouyer } else if (physno > -1) {
661 1.1 bouyer sbusno = physno / AMR_MAX_NSDEVS;
662 1.1 bouyer targetno = physno % AMR_MAX_NSDEVS;
663 1.1 bouyer }
664 1.1 bouyer
665 1.1 bouyer if (globalparam && verbosity >= 1)
666 1.1 bouyer printf("Version\t\t\t%d\n", i);
667 1.1 bouyer #if 0
668 1.1 bouyer if (i != 1) {
669 1.1 bouyer fprintf(stderr, "Driver version (%d) not supported\n", i);
670 1.1 bouyer exit(1);
671 1.1 bouyer }
672 1.1 bouyer #endif
673 1.1 bouyer
674 1.1 bouyer i = describe_card(fd, verbosity, globalparam);
675 1.1 bouyer describe_battery(fd, verbosity, i, bflags, globalparam);
676 1.1 bouyer if (!bflags || lvolno > -1 || physno > -1 || sbusno > -1 || targetno > -1)
677 1.1 bouyer describe_drive(verbosity, i, lvolno, sbusno, targetno);
678 1.1 bouyer
679 1.1 bouyer return 0;
680 1.1 bouyer }
681