1763fae79SScott Long /*-
21de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
31de7b4b8SPedro F. Giffuni *
4763fae79SScott Long * Copyright (c) 2008, 2009 Yahoo!, Inc.
5763fae79SScott Long * All rights reserved.
6763fae79SScott Long *
7763fae79SScott Long * Redistribution and use in source and binary forms, with or without
8763fae79SScott Long * modification, are permitted provided that the following conditions
9763fae79SScott Long * are met:
10763fae79SScott Long * 1. Redistributions of source code must retain the above copyright
11763fae79SScott Long * notice, this list of conditions and the following disclaimer.
12763fae79SScott Long * 2. Redistributions in binary form must reproduce the above copyright
13763fae79SScott Long * notice, this list of conditions and the following disclaimer in the
14763fae79SScott Long * documentation and/or other materials provided with the distribution.
15763fae79SScott Long * 3. The names of the authors may not be used to endorse or promote
16763fae79SScott Long * products derived from this software without specific prior written
17763fae79SScott Long * permission.
18763fae79SScott Long *
19763fae79SScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20763fae79SScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21763fae79SScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22763fae79SScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23763fae79SScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24763fae79SScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25763fae79SScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26763fae79SScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27763fae79SScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28763fae79SScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29763fae79SScott Long * SUCH DAMAGE.
30763fae79SScott Long */
31763fae79SScott Long
32763fae79SScott Long #include <sys/types.h>
33763fae79SScott Long #include <sys/errno.h>
34763fae79SScott Long #include <err.h>
35bf4ec4dfSEitan Adler #include <fcntl.h>
36763fae79SScott Long #include <libutil.h>
37763fae79SScott Long #include <stdio.h>
38763fae79SScott Long #include <stdlib.h>
39763fae79SScott Long #include <string.h>
40763fae79SScott Long #include <unistd.h>
41763fae79SScott Long #include "mfiutil.h"
42763fae79SScott Long
4306f1884fSSean Bruno static const char* foreign_state = " (FOREIGN)";
4406f1884fSSean Bruno
45763fae79SScott Long MFI_TABLE(top, show);
46763fae79SScott Long
4706f1884fSSean Bruno void
format_stripe(char * buf,size_t buflen,uint8_t stripe)48763fae79SScott Long format_stripe(char *buf, size_t buflen, uint8_t stripe)
49763fae79SScott Long {
50763fae79SScott Long
51763fae79SScott Long humanize_number(buf, buflen, (1 << stripe) * 512, "", HN_AUTOSCALE,
52763fae79SScott Long HN_B | HN_NOSPACE);
53763fae79SScott Long }
54763fae79SScott Long
55763fae79SScott Long static int
show_adapter(int ac,char ** av __unused)5641b8cbdaSEitan Adler show_adapter(int ac, char **av __unused)
57763fae79SScott Long {
58763fae79SScott Long struct mfi_ctrl_info info;
59763fae79SScott Long char stripe[5];
60c02999d9SJohn Baldwin int error, fd, comma;
61763fae79SScott Long
62763fae79SScott Long if (ac != 1) {
63763fae79SScott Long warnx("show adapter: extra arguments");
64763fae79SScott Long return (EINVAL);
65763fae79SScott Long }
66763fae79SScott Long
677e0f8b79SDoug Ambrisko fd = mfi_open(mfi_device, O_RDONLY);
68763fae79SScott Long if (fd < 0) {
69c02999d9SJohn Baldwin error = errno;
70763fae79SScott Long warn("mfi_open");
71c02999d9SJohn Baldwin return (error);
72763fae79SScott Long }
73763fae79SScott Long
74763fae79SScott Long if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
75c02999d9SJohn Baldwin error = errno;
76763fae79SScott Long warn("Failed to get controller info");
77375c4656SBjoern A. Zeeb close(fd);
78c02999d9SJohn Baldwin return (error);
79763fae79SScott Long }
807e0f8b79SDoug Ambrisko printf("%s Adapter:\n", mfi_device);
81763fae79SScott Long printf(" Product Name: %.80s\n", info.product_name);
82763fae79SScott Long printf(" Serial Number: %.32s\n", info.serial_number);
83763fae79SScott Long if (info.package_version[0] != '\0')
84763fae79SScott Long printf(" Firmware: %s\n", info.package_version);
85763fae79SScott Long printf(" RAID Levels:");
86763fae79SScott Long #ifdef DEBUG
87763fae79SScott Long printf(" (%#x)", info.raid_levels);
88763fae79SScott Long #endif
89763fae79SScott Long comma = 0;
90763fae79SScott Long if (info.raid_levels & MFI_INFO_RAID_0) {
91763fae79SScott Long printf(" JBOD, RAID0");
92763fae79SScott Long comma = 1;
93763fae79SScott Long }
94763fae79SScott Long if (info.raid_levels & MFI_INFO_RAID_1) {
95763fae79SScott Long printf("%s RAID1", comma ? "," : "");
96763fae79SScott Long comma = 1;
97763fae79SScott Long }
98763fae79SScott Long if (info.raid_levels & MFI_INFO_RAID_5) {
99763fae79SScott Long printf("%s RAID5", comma ? "," : "");
100763fae79SScott Long comma = 1;
101763fae79SScott Long }
102763fae79SScott Long if (info.raid_levels & MFI_INFO_RAID_1E) {
103763fae79SScott Long printf("%s RAID1E", comma ? "," : "");
104763fae79SScott Long comma = 1;
105763fae79SScott Long }
106763fae79SScott Long if (info.raid_levels & MFI_INFO_RAID_6) {
107763fae79SScott Long printf("%s RAID6", comma ? "," : "");
108763fae79SScott Long comma = 1;
109763fae79SScott Long }
110763fae79SScott Long if ((info.raid_levels & (MFI_INFO_RAID_0 | MFI_INFO_RAID_1)) ==
111763fae79SScott Long (MFI_INFO_RAID_0 | MFI_INFO_RAID_1)) {
112763fae79SScott Long printf("%s RAID10", comma ? "," : "");
113763fae79SScott Long comma = 1;
114763fae79SScott Long }
115763fae79SScott Long if ((info.raid_levels & (MFI_INFO_RAID_0 | MFI_INFO_RAID_5)) ==
116763fae79SScott Long (MFI_INFO_RAID_0 | MFI_INFO_RAID_5)) {
117763fae79SScott Long printf("%s RAID50", comma ? "," : "");
118763fae79SScott Long comma = 1;
119763fae79SScott Long }
120763fae79SScott Long printf("\n");
121763fae79SScott Long printf(" Battery Backup: ");
122763fae79SScott Long if (info.hw_present & MFI_INFO_HW_BBU)
123763fae79SScott Long printf("present\n");
124763fae79SScott Long else
125763fae79SScott Long printf("not present\n");
126763fae79SScott Long if (info.hw_present & MFI_INFO_HW_NVRAM)
127763fae79SScott Long printf(" NVRAM: %uK\n", info.nvram_size);
128763fae79SScott Long printf(" Onboard Memory: %uM\n", info.memory_size);
129763fae79SScott Long format_stripe(stripe, sizeof(stripe), info.stripe_sz_ops.min);
130763fae79SScott Long printf(" Minimum Stripe: %s\n", stripe);
131763fae79SScott Long format_stripe(stripe, sizeof(stripe), info.stripe_sz_ops.max);
132763fae79SScott Long printf(" Maximum Stripe: %s\n", stripe);
133763fae79SScott Long
134763fae79SScott Long close(fd);
135763fae79SScott Long
136763fae79SScott Long return (0);
137763fae79SScott Long }
138763fae79SScott Long MFI_COMMAND(show, adapter, show_adapter);
139763fae79SScott Long
140763fae79SScott Long static int
show_battery(int ac,char ** av __unused)14141b8cbdaSEitan Adler show_battery(int ac, char **av __unused)
142763fae79SScott Long {
143763fae79SScott Long struct mfi_bbu_capacity_info cap;
144763fae79SScott Long struct mfi_bbu_design_info design;
145dee3e845SMark Johnston struct mfi_bbu_properties props;
146100ec05aSJohn Baldwin struct mfi_bbu_status stat;
147763fae79SScott Long uint8_t status;
148dee3e845SMark Johnston int comma, error, fd, show_capacity, show_props;
149dee3e845SMark Johnston char buf[32];
150763fae79SScott Long
151763fae79SScott Long if (ac != 1) {
152763fae79SScott Long warnx("show battery: extra arguments");
153763fae79SScott Long return (EINVAL);
154763fae79SScott Long }
155763fae79SScott Long
1567e0f8b79SDoug Ambrisko fd = mfi_open(mfi_device, O_RDONLY);
157763fae79SScott Long if (fd < 0) {
158c02999d9SJohn Baldwin error = errno;
159763fae79SScott Long warn("mfi_open");
160c02999d9SJohn Baldwin return (error);
161763fae79SScott Long }
162763fae79SScott Long
163763fae79SScott Long if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_CAPACITY_INFO, &cap,
164763fae79SScott Long sizeof(cap), NULL, 0, &status) < 0) {
165c02999d9SJohn Baldwin error = errno;
166763fae79SScott Long warn("Failed to get capacity info");
167375c4656SBjoern A. Zeeb close(fd);
168c02999d9SJohn Baldwin return (error);
169763fae79SScott Long }
170b08d3049SEd Maste if (status == MFI_STAT_NO_HW_PRESENT) {
1717e0f8b79SDoug Ambrisko printf("%s: No battery present\n", mfi_device);
172b08d3049SEd Maste close(fd);
173b08d3049SEd Maste return (0);
174b08d3049SEd Maste }
175b08d3049SEd Maste show_capacity = (status == MFI_STAT_OK);
176763fae79SScott Long
177763fae79SScott Long if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_DESIGN_INFO, &design,
178763fae79SScott Long sizeof(design), NULL, 0, NULL) < 0) {
179c02999d9SJohn Baldwin error = errno;
180763fae79SScott Long warn("Failed to get design info");
181375c4656SBjoern A. Zeeb close(fd);
182c02999d9SJohn Baldwin return (error);
183763fae79SScott Long }
184763fae79SScott Long
185100ec05aSJohn Baldwin if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_STATUS, &stat, sizeof(stat),
186100ec05aSJohn Baldwin NULL, 0, NULL) < 0) {
187b09e402aSJohn Baldwin error = errno;
188100ec05aSJohn Baldwin warn("Failed to get status");
189375c4656SBjoern A. Zeeb close(fd);
190b09e402aSJohn Baldwin return (error);
191100ec05aSJohn Baldwin }
192100ec05aSJohn Baldwin
193dee3e845SMark Johnston if (mfi_bbu_get_props(fd, &props, &status) < 0) {
194dee3e845SMark Johnston error = errno;
195dee3e845SMark Johnston warn("Failed to get properties");
196dee3e845SMark Johnston close(fd);
197dee3e845SMark Johnston return (error);
198dee3e845SMark Johnston }
199dee3e845SMark Johnston show_props = (status == MFI_STAT_OK);
200dee3e845SMark Johnston
2017e0f8b79SDoug Ambrisko printf("%s: Battery State:\n", mfi_device);
202763fae79SScott Long printf(" Manufacture Date: %d/%d/%d\n", design.mfg_date >> 5 & 0x0f,
203763fae79SScott Long design.mfg_date & 0x1f, design.mfg_date >> 9 & 0xffff);
204763fae79SScott Long printf(" Serial Number: %d\n", design.serial_number);
205763fae79SScott Long printf(" Manufacturer: %s\n", design.mfg_name);
206763fae79SScott Long printf(" Model: %s\n", design.device_name);
207763fae79SScott Long printf(" Chemistry: %s\n", design.device_chemistry);
208763fae79SScott Long printf(" Design Capacity: %d mAh\n", design.design_capacity);
209b08d3049SEd Maste if (show_capacity) {
210b08d3049SEd Maste printf(" Full Charge Capacity: %d mAh\n",
211b08d3049SEd Maste cap.full_charge_capacity);
212b08d3049SEd Maste printf(" Current Capacity: %d mAh\n",
213b08d3049SEd Maste cap.remaining_capacity);
214100ec05aSJohn Baldwin printf(" Charge Cycles: %d\n", cap.cycle_count);
215763fae79SScott Long printf(" Current Charge: %d%%\n", cap.relative_charge);
216b08d3049SEd Maste }
217100ec05aSJohn Baldwin printf(" Design Voltage: %d mV\n", design.design_voltage);
218100ec05aSJohn Baldwin printf(" Current Voltage: %d mV\n", stat.voltage);
219100ec05aSJohn Baldwin printf(" Temperature: %d C\n", stat.temperature);
220dee3e845SMark Johnston if (show_props) {
221dee3e845SMark Johnston mfi_autolearn_period(props.auto_learn_period, buf, sizeof(buf));
222dee3e845SMark Johnston printf(" Autolearn period: %s\n", buf);
223dee3e845SMark Johnston if (props.auto_learn_mode != 0)
224dee3e845SMark Johnston snprintf(buf, sizeof(buf), "never");
225dee3e845SMark Johnston else
226dee3e845SMark Johnston mfi_next_learn_time(props.next_learn_time, buf,
227dee3e845SMark Johnston sizeof(buf));
228dee3e845SMark Johnston printf(" Next learn time: %s\n", buf);
229dee3e845SMark Johnston printf(" Learn delay interval: %u hour%s\n",
230dee3e845SMark Johnston props.learn_delay_interval,
231dee3e845SMark Johnston props.learn_delay_interval != 1 ? "s" : "");
232dee3e845SMark Johnston mfi_autolearn_mode(props.auto_learn_mode, buf, sizeof(buf));
233dee3e845SMark Johnston printf(" Autolearn mode: %s\n", buf);
234dee3e845SMark Johnston if (props.bbu_mode != 0)
235dee3e845SMark Johnston printf(" BBU Mode: %d\n", props.bbu_mode);
236dee3e845SMark Johnston }
237100ec05aSJohn Baldwin printf(" Status:");
238100ec05aSJohn Baldwin comma = 0;
239100ec05aSJohn Baldwin if (stat.fw_status & MFI_BBU_STATE_PACK_MISSING) {
240100ec05aSJohn Baldwin printf(" PACK_MISSING");
241100ec05aSJohn Baldwin comma = 1;
242100ec05aSJohn Baldwin }
243100ec05aSJohn Baldwin if (stat.fw_status & MFI_BBU_STATE_VOLTAGE_LOW) {
244100ec05aSJohn Baldwin printf("%s VOLTAGE_LOW", comma ? "," : "");
245100ec05aSJohn Baldwin comma = 1;
246100ec05aSJohn Baldwin }
247100ec05aSJohn Baldwin if (stat.fw_status & MFI_BBU_STATE_TEMPERATURE_HIGH) {
248100ec05aSJohn Baldwin printf("%s TEMPERATURE_HIGH", comma ? "," : "");
249100ec05aSJohn Baldwin comma = 1;
250100ec05aSJohn Baldwin }
251100ec05aSJohn Baldwin if (stat.fw_status & MFI_BBU_STATE_CHARGE_ACTIVE) {
252100ec05aSJohn Baldwin printf("%s CHARGING", comma ? "," : "");
253100ec05aSJohn Baldwin comma = 1;
254100ec05aSJohn Baldwin }
255100ec05aSJohn Baldwin if (stat.fw_status & MFI_BBU_STATE_DISCHARGE_ACTIVE) {
256100ec05aSJohn Baldwin printf("%s DISCHARGING", comma ? "," : "");
257f8b329b0SSean Bruno comma = 1;
258100ec05aSJohn Baldwin }
259f8b329b0SSean Bruno if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_REQ) {
260f8b329b0SSean Bruno printf("%s LEARN_CYCLE_REQUESTED", comma ? "," : "");
261f8b329b0SSean Bruno comma = 1;
262f8b329b0SSean Bruno }
263f8b329b0SSean Bruno if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_ACTIVE) {
264f8b329b0SSean Bruno printf("%s LEARN_CYCLE_ACTIVE", comma ? "," : "");
265f8b329b0SSean Bruno comma = 1;
266f8b329b0SSean Bruno }
267f8b329b0SSean Bruno if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_FAIL) {
268f8b329b0SSean Bruno printf("%s LEARN_CYCLE_FAIL", comma ? "," : "");
269f8b329b0SSean Bruno comma = 1;
270f8b329b0SSean Bruno }
271f8b329b0SSean Bruno if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_TIMEOUT) {
272f8b329b0SSean Bruno printf("%s LEARN_CYCLE_TIMEOUT", comma ? "," : "");
273f8b329b0SSean Bruno comma = 1;
274f8b329b0SSean Bruno }
275f8b329b0SSean Bruno if (stat.fw_status & MFI_BBU_STATE_I2C_ERR_DETECT) {
276f8b329b0SSean Bruno printf("%s I2C_ERROR_DETECT", comma ? "," : "");
277f8b329b0SSean Bruno comma = 1;
278f8b329b0SSean Bruno }
279f8b329b0SSean Bruno
280100ec05aSJohn Baldwin if (!comma)
281100ec05aSJohn Baldwin printf(" normal");
282100ec05aSJohn Baldwin printf("\n");
283100ec05aSJohn Baldwin switch (stat.battery_type) {
284100ec05aSJohn Baldwin case MFI_BBU_TYPE_BBU:
285100ec05aSJohn Baldwin printf(" State of Health: %s\n",
286100ec05aSJohn Baldwin stat.detail.bbu.is_SOH_good ? "good" : "bad");
287100ec05aSJohn Baldwin break;
288100ec05aSJohn Baldwin }
289763fae79SScott Long
290763fae79SScott Long close(fd);
291763fae79SScott Long
292763fae79SScott Long return (0);
293763fae79SScott Long }
294763fae79SScott Long MFI_COMMAND(show, battery, show_battery);
295763fae79SScott Long
29606f1884fSSean Bruno void
print_ld(struct mfi_ld_info * info,int state_len)297763fae79SScott Long print_ld(struct mfi_ld_info *info, int state_len)
298763fae79SScott Long {
299763fae79SScott Long struct mfi_ld_params *params = &info->ld_config.params;
300763fae79SScott Long const char *level;
301763fae79SScott Long char size[6], stripe[5];
302763fae79SScott Long
303763fae79SScott Long humanize_number(size, sizeof(size), info->size * 512,
304763fae79SScott Long "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
305763fae79SScott Long format_stripe(stripe, sizeof(stripe),
306763fae79SScott Long info->ld_config.params.stripe_size);
307763fae79SScott Long level = mfi_raid_level(params->primary_raid_level,
308763fae79SScott Long params->secondary_raid_level);
309763fae79SScott Long if (state_len > 0)
310763fae79SScott Long printf("(%6s) %-8s %6s %-*s", size, level, stripe, state_len,
311763fae79SScott Long mfi_ldstate(params->state));
312763fae79SScott Long else
313763fae79SScott Long printf("(%s) %s %s %s", size, level, stripe,
314763fae79SScott Long mfi_ldstate(params->state));
315763fae79SScott Long }
316763fae79SScott Long
31706f1884fSSean Bruno void
print_pd(struct mfi_pd_info * info,int state_len)3187bbae305SBjoern A. Zeeb print_pd(struct mfi_pd_info *info, int state_len)
319763fae79SScott Long {
320763fae79SScott Long const char *s;
32106f1884fSSean Bruno char buf[256];
322763fae79SScott Long
3234a40e59fSSean Bruno humanize_number(buf, 6, info->raw_size * 512, "",
324763fae79SScott Long HN_AUTOSCALE, HN_B | HN_NOSPACE |HN_DECIMAL);
325763fae79SScott Long printf("(%6s) ", buf);
32606f1884fSSean Bruno if (info->state.ddf.v.pd_type.is_foreign) {
32706f1884fSSean Bruno sprintf(buf, "%s%s", mfi_pdstate(info->fw_state), foreign_state);
32806f1884fSSean Bruno s = buf;
32906f1884fSSean Bruno } else
33006f1884fSSean Bruno s = mfi_pdstate(info->fw_state);
331763fae79SScott Long if (state_len > 0)
33206f1884fSSean Bruno printf("%-*s", state_len, s);
333763fae79SScott Long else
33406f1884fSSean Bruno printf("%s",s);
335763fae79SScott Long s = mfi_pd_inq_string(info);
336763fae79SScott Long if (s != NULL)
337763fae79SScott Long printf(" %s", s);
338763fae79SScott Long }
339763fae79SScott Long
340763fae79SScott Long static int
show_config(int ac,char ** av __unused)34141b8cbdaSEitan Adler show_config(int ac, char **av __unused)
342763fae79SScott Long {
343763fae79SScott Long struct mfi_config_data *config;
344763fae79SScott Long struct mfi_array *ar;
345763fae79SScott Long struct mfi_ld_config *ld;
346763fae79SScott Long struct mfi_spare *sp;
347763fae79SScott Long struct mfi_ld_info linfo;
348763fae79SScott Long struct mfi_pd_info pinfo;
349763fae79SScott Long uint16_t device_id;
350763fae79SScott Long char *p;
351c02999d9SJohn Baldwin int error, fd, i, j;
352763fae79SScott Long
353763fae79SScott Long if (ac != 1) {
354763fae79SScott Long warnx("show config: extra arguments");
355763fae79SScott Long return (EINVAL);
356763fae79SScott Long }
357763fae79SScott Long
3587e0f8b79SDoug Ambrisko fd = mfi_open(mfi_device, O_RDONLY);
359763fae79SScott Long if (fd < 0) {
360c02999d9SJohn Baldwin error = errno;
361763fae79SScott Long warn("mfi_open");
362c02999d9SJohn Baldwin return (error);
363763fae79SScott Long }
364763fae79SScott Long
365763fae79SScott Long /* Get the config from the controller. */
366763fae79SScott Long if (mfi_config_read(fd, &config) < 0) {
367c02999d9SJohn Baldwin error = errno;
368763fae79SScott Long warn("Failed to get config");
369375c4656SBjoern A. Zeeb close(fd);
370c02999d9SJohn Baldwin return (error);
371763fae79SScott Long }
372763fae79SScott Long
373763fae79SScott Long /* Dump out the configuration. */
3747e0f8b79SDoug Ambrisko printf("%s Configuration: %d arrays, %d volumes, %d spares\n",
3757e0f8b79SDoug Ambrisko mfi_device, config->array_count, config->log_drv_count,
376763fae79SScott Long config->spares_count);
377763fae79SScott Long p = (char *)config->array;
378763fae79SScott Long
379763fae79SScott Long for (i = 0; i < config->array_count; i++) {
380763fae79SScott Long ar = (struct mfi_array *)p;
381763fae79SScott Long printf(" array %u of %u drives:\n", ar->array_ref,
382763fae79SScott Long ar->num_drives);
383763fae79SScott Long for (j = 0; j < ar->num_drives; j++) {
384763fae79SScott Long device_id = ar->pd[j].ref.v.device_id;
3857bbae305SBjoern A. Zeeb printf(" drive %s ", mfi_drive_name(NULL,
3867bbae305SBjoern A. Zeeb device_id,
3877bbae305SBjoern A. Zeeb MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
3887bbae305SBjoern A. Zeeb if (device_id != 0xffff) {
389763fae79SScott Long if (mfi_pd_get_info(fd, device_id, &pinfo,
390763fae79SScott Long NULL) < 0)
391763fae79SScott Long printf("%s",
392763fae79SScott Long mfi_pdstate(ar->pd[j].fw_state));
393763fae79SScott Long else
3947bbae305SBjoern A. Zeeb print_pd(&pinfo, -1);
395763fae79SScott Long }
39664371599SBjoern A. Zeeb printf("\n");
397763fae79SScott Long }
398763fae79SScott Long p += config->array_size;
399763fae79SScott Long }
400763fae79SScott Long
401763fae79SScott Long for (i = 0; i < config->log_drv_count; i++) {
402763fae79SScott Long ld = (struct mfi_ld_config *)p;
403763fae79SScott Long printf(" volume %s ",
404763fae79SScott Long mfi_volume_name(fd, ld->properties.ld.v.target_id));
405763fae79SScott Long if (mfi_ld_get_info(fd, ld->properties.ld.v.target_id, &linfo,
406763fae79SScott Long NULL) < 0) {
407763fae79SScott Long printf("%s %s",
408763fae79SScott Long mfi_raid_level(ld->params.primary_raid_level,
409763fae79SScott Long ld->params.secondary_raid_level),
410763fae79SScott Long mfi_ldstate(ld->params.state));
411763fae79SScott Long } else
412763fae79SScott Long print_ld(&linfo, -1);
413763fae79SScott Long if (ld->properties.name[0] != '\0')
414763fae79SScott Long printf(" <%s>", ld->properties.name);
415763fae79SScott Long printf(" spans:\n");
416763fae79SScott Long for (j = 0; j < ld->params.span_depth; j++)
417763fae79SScott Long printf(" array %u\n", ld->span[j].array_ref);
418763fae79SScott Long p += config->log_drv_size;
419763fae79SScott Long }
420763fae79SScott Long
421763fae79SScott Long for (i = 0; i < config->spares_count; i++) {
422763fae79SScott Long sp = (struct mfi_spare *)p;
4237bbae305SBjoern A. Zeeb printf(" %s spare %s ",
424763fae79SScott Long sp->spare_type & MFI_SPARE_DEDICATED ? "dedicated" :
4257bbae305SBjoern A. Zeeb "global", mfi_drive_name(NULL, sp->ref.v.device_id,
4267bbae305SBjoern A. Zeeb MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
427763fae79SScott Long if (mfi_pd_get_info(fd, sp->ref.v.device_id, &pinfo, NULL) < 0)
428763fae79SScott Long printf("%s", mfi_pdstate(MFI_PD_STATE_HOT_SPARE));
429763fae79SScott Long else
4307bbae305SBjoern A. Zeeb print_pd(&pinfo, -1);
431763fae79SScott Long if (sp->spare_type & MFI_SPARE_DEDICATED) {
432763fae79SScott Long printf(" backs:\n");
433763fae79SScott Long for (j = 0; j < sp->array_count; j++)
434763fae79SScott Long printf(" array %u\n", sp->array_ref[j]);
435763fae79SScott Long } else
436763fae79SScott Long printf("\n");
437763fae79SScott Long p += config->spares_size;
438763fae79SScott Long }
439375c4656SBjoern A. Zeeb free(config);
440763fae79SScott Long close(fd);
441763fae79SScott Long
442763fae79SScott Long return (0);
443763fae79SScott Long }
444763fae79SScott Long MFI_COMMAND(show, config, show_config);
445763fae79SScott Long
446763fae79SScott Long static int
show_volumes(int ac,char ** av __unused)44741b8cbdaSEitan Adler show_volumes(int ac, char **av __unused)
448763fae79SScott Long {
449763fae79SScott Long struct mfi_ld_list list;
450763fae79SScott Long struct mfi_ld_info info;
451c02999d9SJohn Baldwin int error, fd;
452763fae79SScott Long u_int i, len, state_len;
453763fae79SScott Long
454763fae79SScott Long if (ac != 1) {
455763fae79SScott Long warnx("show volumes: extra arguments");
456763fae79SScott Long return (EINVAL);
457763fae79SScott Long }
458763fae79SScott Long
4597e0f8b79SDoug Ambrisko fd = mfi_open(mfi_device, O_RDONLY);
460763fae79SScott Long if (fd < 0) {
461c02999d9SJohn Baldwin error = errno;
462763fae79SScott Long warn("mfi_open");
463c02999d9SJohn Baldwin return (error);
464763fae79SScott Long }
465763fae79SScott Long
466763fae79SScott Long /* Get the logical drive list from the controller. */
467763fae79SScott Long if (mfi_ld_get_list(fd, &list, NULL) < 0) {
468c02999d9SJohn Baldwin error = errno;
469763fae79SScott Long warn("Failed to get volume list");
470375c4656SBjoern A. Zeeb close(fd);
471c02999d9SJohn Baldwin return (error);
472763fae79SScott Long }
473763fae79SScott Long
474763fae79SScott Long /* List the volumes. */
4757e0f8b79SDoug Ambrisko printf("%s Volumes:\n", mfi_device);
476763fae79SScott Long state_len = strlen("State");
477763fae79SScott Long for (i = 0; i < list.ld_count; i++) {
478763fae79SScott Long len = strlen(mfi_ldstate(list.ld_list[i].state));
479763fae79SScott Long if (len > state_len)
480763fae79SScott Long state_len = len;
481763fae79SScott Long }
482763fae79SScott Long printf(" Id Size Level Stripe ");
483763fae79SScott Long len = state_len - strlen("State");
484763fae79SScott Long for (i = 0; i < (len + 1) / 2; i++)
485763fae79SScott Long printf(" ");
486763fae79SScott Long printf("State");
487763fae79SScott Long for (i = 0; i < len / 2; i++)
488763fae79SScott Long printf(" ");
489763fae79SScott Long printf(" Cache Name\n");
490763fae79SScott Long for (i = 0; i < list.ld_count; i++) {
491763fae79SScott Long if (mfi_ld_get_info(fd, list.ld_list[i].ld.v.target_id, &info,
492763fae79SScott Long NULL) < 0) {
493c02999d9SJohn Baldwin error = errno;
494763fae79SScott Long warn("Failed to get info for volume %d",
495763fae79SScott Long list.ld_list[i].ld.v.target_id);
496375c4656SBjoern A. Zeeb close(fd);
497c02999d9SJohn Baldwin return (error);
498763fae79SScott Long }
499763fae79SScott Long printf("%6s ",
500763fae79SScott Long mfi_volume_name(fd, list.ld_list[i].ld.v.target_id));
501763fae79SScott Long print_ld(&info, state_len);
502763fae79SScott Long switch (info.ld_config.properties.current_cache_policy &
503763fae79SScott Long (MR_LD_CACHE_ALLOW_WRITE_CACHE |
504763fae79SScott Long MR_LD_CACHE_ALLOW_READ_CACHE)) {
505763fae79SScott Long case 0:
506763fae79SScott Long printf(" Disabled");
507763fae79SScott Long break;
508763fae79SScott Long case MR_LD_CACHE_ALLOW_READ_CACHE:
509763fae79SScott Long printf(" Reads ");
510763fae79SScott Long break;
511763fae79SScott Long case MR_LD_CACHE_ALLOW_WRITE_CACHE:
512763fae79SScott Long printf(" Writes ");
513763fae79SScott Long break;
514763fae79SScott Long case MR_LD_CACHE_ALLOW_WRITE_CACHE |
515763fae79SScott Long MR_LD_CACHE_ALLOW_READ_CACHE:
516763fae79SScott Long printf(" Enabled ");
517763fae79SScott Long break;
518763fae79SScott Long }
519763fae79SScott Long if (info.ld_config.properties.name[0] != '\0')
520763fae79SScott Long printf(" <%s>", info.ld_config.properties.name);
521763fae79SScott Long printf("\n");
522763fae79SScott Long }
523763fae79SScott Long close(fd);
524763fae79SScott Long
525763fae79SScott Long return (0);
526763fae79SScott Long }
527763fae79SScott Long MFI_COMMAND(show, volumes, show_volumes);
528763fae79SScott Long
529763fae79SScott Long static int
show_drives(int ac,char ** av __unused)53041b8cbdaSEitan Adler show_drives(int ac, char **av __unused)
531763fae79SScott Long {
532763fae79SScott Long struct mfi_pd_list *list;
533763fae79SScott Long struct mfi_pd_info info;
534763fae79SScott Long u_int i, len, state_len;
535c02999d9SJohn Baldwin int error, fd;
536763fae79SScott Long
537763fae79SScott Long if (ac != 1) {
538763fae79SScott Long warnx("show drives: extra arguments");
539763fae79SScott Long return (EINVAL);
540763fae79SScott Long }
541763fae79SScott Long
5427e0f8b79SDoug Ambrisko fd = mfi_open(mfi_device, O_RDONLY);
543763fae79SScott Long if (fd < 0) {
544c02999d9SJohn Baldwin error = errno;
545763fae79SScott Long warn("mfi_open");
546c02999d9SJohn Baldwin return (error);
547763fae79SScott Long }
548763fae79SScott Long
549375c4656SBjoern A. Zeeb list = NULL;
550763fae79SScott Long if (mfi_pd_get_list(fd, &list, NULL) < 0) {
551c02999d9SJohn Baldwin error = errno;
552763fae79SScott Long warn("Failed to get drive list");
553375c4656SBjoern A. Zeeb goto error;
554763fae79SScott Long }
555763fae79SScott Long
556763fae79SScott Long /* Walk the list of drives to determine width of state column. */
557763fae79SScott Long state_len = 0;
558763fae79SScott Long for (i = 0; i < list->count; i++) {
559763fae79SScott Long if (list->addr[i].scsi_dev_type != 0)
560763fae79SScott Long continue;
561763fae79SScott Long
562763fae79SScott Long if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
563763fae79SScott Long NULL) < 0) {
564c02999d9SJohn Baldwin error = errno;
565763fae79SScott Long warn("Failed to fetch info for drive %u",
566763fae79SScott Long list->addr[i].device_id);
567375c4656SBjoern A. Zeeb goto error;
568763fae79SScott Long }
569763fae79SScott Long len = strlen(mfi_pdstate(info.fw_state));
57006f1884fSSean Bruno if (info.state.ddf.v.pd_type.is_foreign)
57106f1884fSSean Bruno len += strlen(foreign_state);
572763fae79SScott Long if (len > state_len)
573763fae79SScott Long state_len = len;
574763fae79SScott Long }
575763fae79SScott Long
576763fae79SScott Long /* List the drives. */
5777e0f8b79SDoug Ambrisko printf("%s Physical Drives:\n", mfi_device);
578763fae79SScott Long for (i = 0; i < list->count; i++) {
579763fae79SScott Long
580763fae79SScott Long /* Skip non-hard disks. */
581763fae79SScott Long if (list->addr[i].scsi_dev_type != 0)
582763fae79SScott Long continue;
583763fae79SScott Long
584763fae79SScott Long /* Fetch details for this drive. */
585763fae79SScott Long if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
586763fae79SScott Long NULL) < 0) {
587c02999d9SJohn Baldwin error = errno;
588763fae79SScott Long warn("Failed to fetch info for drive %u",
589763fae79SScott Long list->addr[i].device_id);
590375c4656SBjoern A. Zeeb goto error;
591763fae79SScott Long }
592763fae79SScott Long
5937bbae305SBjoern A. Zeeb printf("%s ", mfi_drive_name(&info, list->addr[i].device_id,
5947bbae305SBjoern A. Zeeb MFI_DNAME_DEVICE_ID));
5957bbae305SBjoern A. Zeeb print_pd(&info, state_len);
5967bbae305SBjoern A. Zeeb printf(" %s", mfi_drive_name(&info, list->addr[i].device_id,
5977bbae305SBjoern A. Zeeb MFI_DNAME_ES));
598763fae79SScott Long printf("\n");
599763fae79SScott Long }
600c0ca022aSJohn Baldwin error = 0;
601375c4656SBjoern A. Zeeb error:
602375c4656SBjoern A. Zeeb free(list);
603763fae79SScott Long close(fd);
604763fae79SScott Long
605375c4656SBjoern A. Zeeb return (error);
606763fae79SScott Long }
607763fae79SScott Long MFI_COMMAND(show, drives, show_drives);
608763fae79SScott Long
609763fae79SScott Long static int
show_firmware(int ac,char ** av __unused)61041b8cbdaSEitan Adler show_firmware(int ac, char **av __unused)
611763fae79SScott Long {
612763fae79SScott Long struct mfi_ctrl_info info;
613763fae79SScott Long struct mfi_info_component header;
614c02999d9SJohn Baldwin int error, fd;
615763fae79SScott Long u_int i;
616763fae79SScott Long
617763fae79SScott Long if (ac != 1) {
61898be0dfeSJohn Baldwin warnx("show firmware: extra arguments");
619763fae79SScott Long return (EINVAL);
620763fae79SScott Long }
621763fae79SScott Long
6227e0f8b79SDoug Ambrisko fd = mfi_open(mfi_device, O_RDONLY);
623763fae79SScott Long if (fd < 0) {
624c02999d9SJohn Baldwin error = errno;
625763fae79SScott Long warn("mfi_open");
626c02999d9SJohn Baldwin return (error);
627763fae79SScott Long }
628763fae79SScott Long
629763fae79SScott Long if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
630c02999d9SJohn Baldwin error = errno;
631763fae79SScott Long warn("Failed to get controller info");
632375c4656SBjoern A. Zeeb close(fd);
633c02999d9SJohn Baldwin return (error);
634763fae79SScott Long }
635763fae79SScott Long
636763fae79SScott Long if (info.package_version[0] != '\0')
6377e0f8b79SDoug Ambrisko printf("%s Firmware Package Version: %s\n", mfi_device,
638763fae79SScott Long info.package_version);
6397e0f8b79SDoug Ambrisko printf("%s Firmware Images:\n", mfi_device);
640763fae79SScott Long strcpy(header.name, "Name");
641763fae79SScott Long strcpy(header.version, "Version");
642763fae79SScott Long strcpy(header.build_date, "Date");
643763fae79SScott Long strcpy(header.build_time, "Time");
644763fae79SScott Long scan_firmware(&header);
645763fae79SScott Long if (info.image_component_count > 8)
646763fae79SScott Long info.image_component_count = 8;
647763fae79SScott Long for (i = 0; i < info.image_component_count; i++)
648763fae79SScott Long scan_firmware(&info.image_component[i]);
649763fae79SScott Long if (info.pending_image_component_count > 8)
650763fae79SScott Long info.pending_image_component_count = 8;
651763fae79SScott Long for (i = 0; i < info.pending_image_component_count; i++)
652763fae79SScott Long scan_firmware(&info.pending_image_component[i]);
653763fae79SScott Long display_firmware(&header, "Status");
654763fae79SScott Long for (i = 0; i < info.image_component_count; i++)
655763fae79SScott Long display_firmware(&info.image_component[i], "active");
656763fae79SScott Long for (i = 0; i < info.pending_image_component_count; i++)
657763fae79SScott Long display_firmware(&info.pending_image_component[i], "pending");
658763fae79SScott Long
659763fae79SScott Long close(fd);
660763fae79SScott Long
661763fae79SScott Long return (0);
662763fae79SScott Long }
663763fae79SScott Long MFI_COMMAND(show, firmware, show_firmware);
66498be0dfeSJohn Baldwin
66598be0dfeSJohn Baldwin static int
show_progress(int ac,char ** av __unused)66641b8cbdaSEitan Adler show_progress(int ac, char **av __unused)
66798be0dfeSJohn Baldwin {
66898be0dfeSJohn Baldwin struct mfi_ld_list llist;
66998be0dfeSJohn Baldwin struct mfi_pd_list *plist;
67098be0dfeSJohn Baldwin struct mfi_ld_info linfo;
67198be0dfeSJohn Baldwin struct mfi_pd_info pinfo;
67298be0dfeSJohn Baldwin int busy, error, fd;
67398be0dfeSJohn Baldwin u_int i;
67498be0dfeSJohn Baldwin uint16_t device_id;
67598be0dfeSJohn Baldwin uint8_t target_id;
67698be0dfeSJohn Baldwin
67798be0dfeSJohn Baldwin if (ac != 1) {
67898be0dfeSJohn Baldwin warnx("show progress: extra arguments");
67998be0dfeSJohn Baldwin return (EINVAL);
68098be0dfeSJohn Baldwin }
68198be0dfeSJohn Baldwin
6827e0f8b79SDoug Ambrisko fd = mfi_open(mfi_device, O_RDONLY);
68398be0dfeSJohn Baldwin if (fd < 0) {
68498be0dfeSJohn Baldwin error = errno;
68598be0dfeSJohn Baldwin warn("mfi_open");
68698be0dfeSJohn Baldwin return (error);
68798be0dfeSJohn Baldwin }
68898be0dfeSJohn Baldwin
68998be0dfeSJohn Baldwin if (mfi_ld_get_list(fd, &llist, NULL) < 0) {
69098be0dfeSJohn Baldwin error = errno;
69198be0dfeSJohn Baldwin warn("Failed to get volume list");
692375c4656SBjoern A. Zeeb close(fd);
69398be0dfeSJohn Baldwin return (error);
69498be0dfeSJohn Baldwin }
69598be0dfeSJohn Baldwin if (mfi_pd_get_list(fd, &plist, NULL) < 0) {
69698be0dfeSJohn Baldwin error = errno;
69798be0dfeSJohn Baldwin warn("Failed to get drive list");
698375c4656SBjoern A. Zeeb close(fd);
69998be0dfeSJohn Baldwin return (error);
70098be0dfeSJohn Baldwin }
70198be0dfeSJohn Baldwin
702375c4656SBjoern A. Zeeb busy = 0;
70398be0dfeSJohn Baldwin for (i = 0; i < llist.ld_count; i++) {
70498be0dfeSJohn Baldwin target_id = llist.ld_list[i].ld.v.target_id;
70598be0dfeSJohn Baldwin if (mfi_ld_get_info(fd, target_id, &linfo, NULL) < 0) {
70698be0dfeSJohn Baldwin error = errno;
70798be0dfeSJohn Baldwin warn("Failed to get info for volume %s",
70898be0dfeSJohn Baldwin mfi_volume_name(fd, target_id));
709375c4656SBjoern A. Zeeb free(plist);
710375c4656SBjoern A. Zeeb close(fd);
71198be0dfeSJohn Baldwin return (error);
71298be0dfeSJohn Baldwin }
71398be0dfeSJohn Baldwin if (linfo.progress.active & MFI_LD_PROGRESS_CC) {
71498be0dfeSJohn Baldwin printf("volume %s ", mfi_volume_name(fd, target_id));
71598be0dfeSJohn Baldwin mfi_display_progress("Consistency Check",
71698be0dfeSJohn Baldwin &linfo.progress.cc);
71798be0dfeSJohn Baldwin busy = 1;
71898be0dfeSJohn Baldwin }
71998be0dfeSJohn Baldwin if (linfo.progress.active & MFI_LD_PROGRESS_BGI) {
72098be0dfeSJohn Baldwin printf("volume %s ", mfi_volume_name(fd, target_id));
72198be0dfeSJohn Baldwin mfi_display_progress("Background Init",
72298be0dfeSJohn Baldwin &linfo.progress.bgi);
72398be0dfeSJohn Baldwin busy = 1;
72498be0dfeSJohn Baldwin }
72598be0dfeSJohn Baldwin if (linfo.progress.active & MFI_LD_PROGRESS_FGI) {
72698be0dfeSJohn Baldwin printf("volume %s ", mfi_volume_name(fd, target_id));
72798be0dfeSJohn Baldwin mfi_display_progress("Foreground Init",
72898be0dfeSJohn Baldwin &linfo.progress.fgi);
72998be0dfeSJohn Baldwin busy = 1;
73098be0dfeSJohn Baldwin }
73198be0dfeSJohn Baldwin if (linfo.progress.active & MFI_LD_PROGRESS_RECON) {
73298be0dfeSJohn Baldwin printf("volume %s ", mfi_volume_name(fd, target_id));
73398be0dfeSJohn Baldwin mfi_display_progress("Reconstruction",
73498be0dfeSJohn Baldwin &linfo.progress.recon);
73598be0dfeSJohn Baldwin busy = 1;
73698be0dfeSJohn Baldwin }
73798be0dfeSJohn Baldwin }
73898be0dfeSJohn Baldwin
73998be0dfeSJohn Baldwin for (i = 0; i < plist->count; i++) {
74098be0dfeSJohn Baldwin if (plist->addr[i].scsi_dev_type != 0)
74198be0dfeSJohn Baldwin continue;
74298be0dfeSJohn Baldwin
74398be0dfeSJohn Baldwin device_id = plist->addr[i].device_id;
74498be0dfeSJohn Baldwin if (mfi_pd_get_info(fd, device_id, &pinfo, NULL) < 0) {
74598be0dfeSJohn Baldwin error = errno;
74698be0dfeSJohn Baldwin warn("Failed to fetch info for drive %u", device_id);
747375c4656SBjoern A. Zeeb free(plist);
748375c4656SBjoern A. Zeeb close(fd);
74998be0dfeSJohn Baldwin return (error);
75098be0dfeSJohn Baldwin }
75198be0dfeSJohn Baldwin
75298be0dfeSJohn Baldwin if (pinfo.prog_info.active & MFI_PD_PROGRESS_REBUILD) {
7537bbae305SBjoern A. Zeeb printf("drive %s ", mfi_drive_name(NULL, device_id,
7547bbae305SBjoern A. Zeeb MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
75598be0dfeSJohn Baldwin mfi_display_progress("Rebuild", &pinfo.prog_info.rbld);
75698be0dfeSJohn Baldwin busy = 1;
75798be0dfeSJohn Baldwin }
75898be0dfeSJohn Baldwin if (pinfo.prog_info.active & MFI_PD_PROGRESS_PATROL) {
7597bbae305SBjoern A. Zeeb printf("drive %s ", mfi_drive_name(NULL, device_id,
7607bbae305SBjoern A. Zeeb MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
76198be0dfeSJohn Baldwin mfi_display_progress("Patrol Read",
76298be0dfeSJohn Baldwin &pinfo.prog_info.patrol);
76398be0dfeSJohn Baldwin busy = 1;
76498be0dfeSJohn Baldwin }
76598be0dfeSJohn Baldwin if (pinfo.prog_info.active & MFI_PD_PROGRESS_CLEAR) {
7667bbae305SBjoern A. Zeeb printf("drive %s ", mfi_drive_name(NULL, device_id,
7677bbae305SBjoern A. Zeeb MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
76898be0dfeSJohn Baldwin mfi_display_progress("Clear", &pinfo.prog_info.clear);
7694099f0ecSJohn Baldwin busy = 1;
77098be0dfeSJohn Baldwin }
77198be0dfeSJohn Baldwin }
77298be0dfeSJohn Baldwin
773375c4656SBjoern A. Zeeb free(plist);
77498be0dfeSJohn Baldwin close(fd);
77598be0dfeSJohn Baldwin
77698be0dfeSJohn Baldwin if (!busy)
7777e0f8b79SDoug Ambrisko printf("No activity in progress for adapter %s\n",
7787e0f8b79SDoug Ambrisko mfi_device);
77998be0dfeSJohn Baldwin
78098be0dfeSJohn Baldwin return (0);
78198be0dfeSJohn Baldwin }
78298be0dfeSJohn Baldwin MFI_COMMAND(show, progress, show_progress);
78306f1884fSSean Bruno
78406f1884fSSean Bruno static int
show_foreign(int ac,char ** av)78506f1884fSSean Bruno show_foreign(int ac, char **av)
78606f1884fSSean Bruno {
78706f1884fSSean Bruno return(display_format(ac, av, 0/*normal display*/, MFI_DCMD_CFG_FOREIGN_DISPLAY));
78806f1884fSSean Bruno }
78906f1884fSSean Bruno MFI_COMMAND(show, foreign, show_foreign);
790