1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
|
/*-
* Copyright (c) 2008, 2009 Yahoo!, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <sys/types.h>
#include <sys/errno.h>
#include <err.h>
#include <libutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mfiutil.h"
MFI_TABLE(top, show);
static void
format_stripe(char *buf, size_t buflen, uint8_t stripe)
{
humanize_number(buf, buflen, (1 << stripe) * 512, "", HN_AUTOSCALE,
HN_B | HN_NOSPACE);
}
static int
show_adapter(int ac, char **av)
{
struct mfi_ctrl_info info;
char stripe[5];
int error, fd, comma;
if (ac != 1) {
warnx("show adapter: extra arguments");
return (EINVAL);
}
fd = mfi_open(mfi_unit);
if (fd < 0) {
error = errno;
warn("mfi_open");
return (error);
}
if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
error = errno;
warn("Failed to get controller info");
return (error);
}
printf("mfi%d Adapter:\n", mfi_unit);
printf(" Product Name: %.80s\n", info.product_name);
printf(" Serial Number: %.32s\n", info.serial_number);
if (info.package_version[0] != '\0')
printf(" Firmware: %s\n", info.package_version);
printf(" RAID Levels:");
#ifdef DEBUG
printf(" (%#x)", info.raid_levels);
#endif
comma = 0;
if (info.raid_levels & MFI_INFO_RAID_0) {
printf(" JBOD, RAID0");
comma = 1;
}
if (info.raid_levels & MFI_INFO_RAID_1) {
printf("%s RAID1", comma ? "," : "");
comma = 1;
}
if (info.raid_levels & MFI_INFO_RAID_5) {
printf("%s RAID5", comma ? "," : "");
comma = 1;
}
if (info.raid_levels & MFI_INFO_RAID_1E) {
printf("%s RAID1E", comma ? "," : "");
comma = 1;
}
if (info.raid_levels & MFI_INFO_RAID_6) {
printf("%s RAID6", comma ? "," : "");
comma = 1;
}
if ((info.raid_levels & (MFI_INFO_RAID_0 | MFI_INFO_RAID_1)) ==
(MFI_INFO_RAID_0 | MFI_INFO_RAID_1)) {
printf("%s RAID10", comma ? "," : "");
comma = 1;
}
if ((info.raid_levels & (MFI_INFO_RAID_0 | MFI_INFO_RAID_5)) ==
(MFI_INFO_RAID_0 | MFI_INFO_RAID_5)) {
printf("%s RAID50", comma ? "," : "");
comma = 1;
}
printf("\n");
printf(" Battery Backup: ");
if (info.hw_present & MFI_INFO_HW_BBU)
printf("present\n");
else
printf("not present\n");
if (info.hw_present & MFI_INFO_HW_NVRAM)
printf(" NVRAM: %uK\n", info.nvram_size);
printf(" Onboard Memory: %uM\n", info.memory_size);
format_stripe(stripe, sizeof(stripe), info.stripe_sz_ops.min);
printf(" Minimum Stripe: %s\n", stripe);
format_stripe(stripe, sizeof(stripe), info.stripe_sz_ops.max);
printf(" Maximum Stripe: %s\n", stripe);
close(fd);
return (0);
}
MFI_COMMAND(show, adapter, show_adapter);
static int
show_battery(int ac, char **av)
{
struct mfi_bbu_capacity_info cap;
struct mfi_bbu_design_info design;
struct mfi_bbu_status stat;
uint8_t status;
int comma, error, fd;
if (ac != 1) {
warnx("show battery: extra arguments");
return (EINVAL);
}
fd = mfi_open(mfi_unit);
if (fd < 0) {
error = errno;
warn("mfi_open");
return (error);
}
if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_CAPACITY_INFO, &cap,
sizeof(cap), NULL, 0, &status) < 0) {
if (status == MFI_STAT_NO_HW_PRESENT) {
printf("mfi%d: No battery present\n", mfi_unit);
return (0);
}
error = errno;
warn("Failed to get capacity info");
return (error);
}
if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_DESIGN_INFO, &design,
sizeof(design), NULL, 0, NULL) < 0) {
error = errno;
warn("Failed to get design info");
return (error);
}
if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_STATUS, &stat, sizeof(stat),
NULL, 0, NULL) < 0) {
warn("Failed to get status");
return (errno);
}
printf("mfi%d: Battery State:\n", mfi_unit);
printf(" Manufacture Date: %d/%d/%d\n", design.mfg_date >> 5 & 0x0f,
design.mfg_date & 0x1f, design.mfg_date >> 9 & 0xffff);
printf(" Serial Number: %d\n", design.serial_number);
printf(" Manufacturer: %s\n", design.mfg_name);
printf(" Model: %s\n", design.device_name);
printf(" Chemistry: %s\n", design.device_chemistry);
printf(" Design Capacity: %d mAh\n", design.design_capacity);
printf(" Full Charge Capacity: %d mAh\n", cap.full_charge_capacity);
printf(" Current Capacity: %d mAh\n", cap.remaining_capacity);
printf(" Charge Cycles: %d\n", cap.cycle_count);
printf(" Current Charge: %d%%\n", cap.relative_charge);
printf(" Design Voltage: %d mV\n", design.design_voltage);
printf(" Current Voltage: %d mV\n", stat.voltage);
printf(" Temperature: %d C\n", stat.temperature);
printf(" Status:");
comma = 0;
if (stat.fw_status & MFI_BBU_STATE_PACK_MISSING) {
printf(" PACK_MISSING");
comma = 1;
}
if (stat.fw_status & MFI_BBU_STATE_VOLTAGE_LOW) {
printf("%s VOLTAGE_LOW", comma ? "," : "");
comma = 1;
}
if (stat.fw_status & MFI_BBU_STATE_TEMPERATURE_HIGH) {
printf("%s TEMPERATURE_HIGH", comma ? "," : "");
comma = 1;
}
if (stat.fw_status & MFI_BBU_STATE_CHARGE_ACTIVE) {
printf("%s CHARGING", comma ? "," : "");
comma = 1;
}
if (stat.fw_status & MFI_BBU_STATE_DISCHARGE_ACTIVE) {
printf("%s DISCHARGING", comma ? "," : "");
}
if (!comma)
printf(" normal");
printf("\n");
switch (stat.battery_type) {
case MFI_BBU_TYPE_BBU:
printf(" State of Health: %s\n",
stat.detail.bbu.is_SOH_good ? "good" : "bad");
break;
}
close(fd);
return (0);
}
MFI_COMMAND(show, battery, show_battery);
static void
print_ld(struct mfi_ld_info *info, int state_len)
{
struct mfi_ld_params *params = &info->ld_config.params;
const char *level;
char size[6], stripe[5];
humanize_number(size, sizeof(size), info->size * 512,
"", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
format_stripe(stripe, sizeof(stripe),
info->ld_config.params.stripe_size);
level = mfi_raid_level(params->primary_raid_level,
params->secondary_raid_level);
if (state_len > 0)
printf("(%6s) %-8s %6s %-*s", size, level, stripe, state_len,
mfi_ldstate(params->state));
else
printf("(%s) %s %s %s", size, level, stripe,
mfi_ldstate(params->state));
}
static void
print_pd(struct mfi_pd_info *info, int state_len, int location)
{
const char *s;
char buf[6];
humanize_number(buf, sizeof(buf), info->raw_size * 512, "",
HN_AUTOSCALE, HN_B | HN_NOSPACE |HN_DECIMAL);
printf("(%6s) ", buf);
if (state_len > 0)
printf("%-*s", state_len, mfi_pdstate(info->fw_state));
else
printf("%s", mfi_pdstate(info->fw_state));
s = mfi_pd_inq_string(info);
if (s != NULL)
printf(" %s", s);
if (!location)
return;
if (info->encl_device_id == 0xffff)
printf(" slot %d", info->slot_number);
else if (info->encl_device_id == info->ref.v.device_id)
printf(" enclosure %d", info->encl_index);
else
printf(" enclosure %d, slot %d", info->encl_index,
info->slot_number);
}
static int
show_config(int ac, char **av)
{
struct mfi_config_data *config;
struct mfi_array *ar;
struct mfi_ld_config *ld;
struct mfi_spare *sp;
struct mfi_ld_info linfo;
struct mfi_pd_info pinfo;
uint16_t device_id;
char *p;
int error, fd, i, j;
if (ac != 1) {
warnx("show config: extra arguments");
return (EINVAL);
}
fd = mfi_open(mfi_unit);
if (fd < 0) {
error = errno;
warn("mfi_open");
return (error);
}
/* Get the config from the controller. */
if (mfi_config_read(fd, &config) < 0) {
error = errno;
warn("Failed to get config");
return (error);
}
/* Dump out the configuration. */
printf("mfi%d Configuration: %d arrays, %d volumes, %d spares\n",
mfi_unit, config->array_count, config->log_drv_count,
config->spares_count);
p = (char *)config->array;
for (i = 0; i < config->array_count; i++) {
ar = (struct mfi_array *)p;
printf(" array %u of %u drives:\n", ar->array_ref,
ar->num_drives);
for (j = 0; j < ar->num_drives; j++) {
device_id = ar->pd[j].ref.v.device_id;
if (device_id == 0xffff)
printf(" drive MISSING\n");
else {
printf(" drive %u ", device_id);
if (mfi_pd_get_info(fd, device_id, &pinfo,
NULL) < 0)
printf("%s",
mfi_pdstate(ar->pd[j].fw_state));
else
print_pd(&pinfo, -1, 1);
printf("\n");
}
}
p += config->array_size;
}
for (i = 0; i < config->log_drv_count; i++) {
ld = (struct mfi_ld_config *)p;
printf(" volume %s ",
mfi_volume_name(fd, ld->properties.ld.v.target_id));
if (mfi_ld_get_info(fd, ld->properties.ld.v.target_id, &linfo,
NULL) < 0) {
printf("%s %s",
mfi_raid_level(ld->params.primary_raid_level,
ld->params.secondary_raid_level),
mfi_ldstate(ld->params.state));
} else
print_ld(&linfo, -1);
if (ld->properties.name[0] != '\0')
printf(" <%s>", ld->properties.name);
printf(" spans:\n");
for (j = 0; j < ld->params.span_depth; j++)
printf(" array %u\n", ld->span[j].array_ref);
p += config->log_drv_size;
}
for (i = 0; i < config->spares_count; i++) {
sp = (struct mfi_spare *)p;
printf(" %s spare %u ",
sp->spare_type & MFI_SPARE_DEDICATED ? "dedicated" :
"global", sp->ref.v.device_id);
if (mfi_pd_get_info(fd, sp->ref.v.device_id, &pinfo, NULL) < 0)
printf("%s", mfi_pdstate(MFI_PD_STATE_HOT_SPARE));
else
print_pd(&pinfo, -1, 1);
if (sp->spare_type & MFI_SPARE_DEDICATED) {
printf(" backs:\n");
for (j = 0; j < sp->array_count; j++)
printf(" array %u\n", sp->array_ref[j]);
} else
printf("\n");
p += config->spares_size;
}
close(fd);
return (0);
}
MFI_COMMAND(show, config, show_config);
static int
show_volumes(int ac, char **av)
{
struct mfi_ld_list list;
struct mfi_ld_info info;
int error, fd;
u_int i, len, state_len;
if (ac != 1) {
warnx("show volumes: extra arguments");
return (EINVAL);
}
fd = mfi_open(mfi_unit);
if (fd < 0) {
error = errno;
warn("mfi_open");
return (error);
}
/* Get the logical drive list from the controller. */
if (mfi_ld_get_list(fd, &list, NULL) < 0) {
error = errno;
warn("Failed to get volume list");
return (error);
}
/* List the volumes. */
printf("mfi%d Volumes:\n", mfi_unit);
state_len = strlen("State");
for (i = 0; i < list.ld_count; i++) {
len = strlen(mfi_ldstate(list.ld_list[i].state));
if (len > state_len)
state_len = len;
}
printf(" Id Size Level Stripe ");
len = state_len - strlen("State");
for (i = 0; i < (len + 1) / 2; i++)
printf(" ");
printf("State");
for (i = 0; i < len / 2; i++)
printf(" ");
printf(" Cache Name\n");
for (i = 0; i < list.ld_count; i++) {
if (mfi_ld_get_info(fd, list.ld_list[i].ld.v.target_id, &info,
NULL) < 0) {
error = errno;
warn("Failed to get info for volume %d",
list.ld_list[i].ld.v.target_id);
return (error);
}
printf("%6s ",
mfi_volume_name(fd, list.ld_list[i].ld.v.target_id));
print_ld(&info, state_len);
switch (info.ld_config.properties.current_cache_policy &
(MR_LD_CACHE_ALLOW_WRITE_CACHE |
MR_LD_CACHE_ALLOW_READ_CACHE)) {
case 0:
printf(" Disabled");
break;
case MR_LD_CACHE_ALLOW_READ_CACHE:
printf(" Reads ");
break;
case MR_LD_CACHE_ALLOW_WRITE_CACHE:
printf(" Writes ");
break;
case MR_LD_CACHE_ALLOW_WRITE_CACHE |
MR_LD_CACHE_ALLOW_READ_CACHE:
printf(" Enabled ");
break;
}
if (info.ld_config.properties.name[0] != '\0')
printf(" <%s>", info.ld_config.properties.name);
printf("\n");
}
close(fd);
return (0);
}
MFI_COMMAND(show, volumes, show_volumes);
static int
show_drives(int ac, char **av)
{
struct mfi_pd_list *list;
struct mfi_pd_info info;
u_int i, len, state_len;
int error, fd;
if (ac != 1) {
warnx("show drives: extra arguments");
return (EINVAL);
}
fd = mfi_open(mfi_unit);
if (fd < 0) {
error = errno;
warn("mfi_open");
return (error);
}
if (mfi_pd_get_list(fd, &list, NULL) < 0) {
error = errno;
warn("Failed to get drive list");
return (error);
}
/* Walk the list of drives to determine width of state column. */
state_len = 0;
for (i = 0; i < list->count; i++) {
if (list->addr[i].scsi_dev_type != 0)
continue;
if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
NULL) < 0) {
error = errno;
warn("Failed to fetch info for drive %u",
list->addr[i].device_id);
return (error);
}
len = strlen(mfi_pdstate(info.fw_state));
if (len > state_len)
state_len = len;
}
/* List the drives. */
printf("mfi%d Physical Drives:\n", mfi_unit);
for (i = 0; i < list->count; i++) {
/* Skip non-hard disks. */
if (list->addr[i].scsi_dev_type != 0)
continue;
/* Fetch details for this drive. */
if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
NULL) < 0) {
error = errno;
warn("Failed to fetch info for drive %u",
list->addr[i].device_id);
return (error);
}
print_pd(&info, state_len, 1);
printf("\n");
}
close(fd);
return (0);
}
MFI_COMMAND(show, drives, show_drives);
int fw_name_width, fw_version_width, fw_date_width, fw_time_width;
static void
scan_firmware(struct mfi_info_component *comp)
{
int len;
len = strlen(comp->name);
if (fw_name_width < len)
fw_name_width = len;
len = strlen(comp->version);
if (fw_version_width < len)
fw_version_width = len;
len = strlen(comp->build_date);
if (fw_date_width < len)
fw_date_width = len;
len = strlen(comp->build_time);
if (fw_time_width < len)
fw_time_width = len;
}
static void
display_firmware(struct mfi_info_component *comp, const char *tag)
{
printf("%-*s %-*s %-*s %-*s %s\n", fw_name_width, comp->name,
fw_version_width, comp->version, fw_date_width, comp->build_date,
fw_time_width, comp->build_time, tag);
}
static int
show_firmware(int ac, char **av)
{
struct mfi_ctrl_info info;
struct mfi_info_component header;
int error, fd;
u_int i;
if (ac != 1) {
warnx("show drives: extra arguments");
return (EINVAL);
}
fd = mfi_open(mfi_unit);
if (fd < 0) {
error = errno;
warn("mfi_open");
return (error);
}
if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
error = errno;
warn("Failed to get controller info");
return (error);
}
if (info.package_version[0] != '\0')
printf("mfi%d Firmware Package Version: %s\n", mfi_unit,
info.package_version);
printf("mfi%d Firmware Images:\n", mfi_unit);
strcpy(header.name, "Name");
strcpy(header.version, "Version");
strcpy(header.build_date, "Date");
strcpy(header.build_time, "Time");
scan_firmware(&header);
if (info.image_component_count > 8)
info.image_component_count = 8;
for (i = 0; i < info.image_component_count; i++)
scan_firmware(&info.image_component[i]);
if (info.pending_image_component_count > 8)
info.pending_image_component_count = 8;
for (i = 0; i < info.pending_image_component_count; i++)
scan_firmware(&info.pending_image_component[i]);
display_firmware(&header, "Status");
for (i = 0; i < info.image_component_count; i++)
display_firmware(&info.image_component[i], "active");
for (i = 0; i < info.pending_image_component_count; i++)
display_firmware(&info.pending_image_component[i], "pending");
close(fd);
return (0);
}
MFI_COMMAND(show, firmware, show_firmware);
|