2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2008, 2009 Yahoo!, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the authors may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/errno.h>
47 mfi_event_get_info(int fd, struct mfi_evt_log_state *info, uint8_t *statusp)
50 return (mfi_dcmd_command(fd, MFI_DCMD_CTRL_EVENT_GETINFO, info,
51 sizeof(struct mfi_evt_log_state), NULL, 0, statusp));
55 mfi_get_events(int fd, struct mfi_evt_list *list, int num_events,
56 union mfi_evt filter, uint32_t start_seq, uint8_t *statusp)
62 mbox[1] = filter.word;
63 size = sizeof(struct mfi_evt_list) + sizeof(struct mfi_evt_detail) *
65 return (mfi_dcmd_command(fd, MFI_DCMD_CTRL_EVENT_GET, list, size,
66 (uint8_t *)&mbox, sizeof(mbox), statusp));
70 show_logstate(int ac, char **av __unused)
72 struct mfi_evt_log_state info;
76 warnx("show logstate: extra arguments");
80 fd = mfi_open(mfi_unit, O_RDWR);
87 if (mfi_event_get_info(fd, &info, NULL) < 0) {
89 warn("Failed to get event log info");
94 printf("mfi%d Event Log Sequence Numbers:\n", mfi_unit);
95 printf(" Newest Seq #: %u\n", info.newest_seq_num);
96 printf(" Oldest Seq #: %u\n", info.oldest_seq_num);
97 printf(" Clear Seq #: %u\n", info.clear_seq_num);
98 printf("Shutdown Seq #: %u\n", info.shutdown_seq_num);
99 printf(" Boot Seq #: %u\n", info.boot_seq_num);
105 MFI_COMMAND(show, logstate, show_logstate);
108 parse_seq(struct mfi_evt_log_state *info, char *arg, uint32_t *seq)
113 if (strcasecmp(arg, "newest") == 0) {
114 *seq = info->newest_seq_num;
117 if (strcasecmp(arg, "oldest") == 0) {
118 *seq = info->oldest_seq_num;
121 if (strcasecmp(arg, "clear") == 0) {
122 *seq = info->clear_seq_num;
125 if (strcasecmp(arg, "shutdown") == 0) {
126 *seq = info->shutdown_seq_num;
129 if (strcasecmp(arg, "boot") == 0) {
130 *seq = info->boot_seq_num;
133 val = strtol(arg, &cp, 0);
134 if (*cp != '\0' || val < 0) {
143 parse_locale(char *arg, uint16_t *locale)
148 if (strncasecmp(arg, "vol", 3) == 0 || strcasecmp(arg, "ld") == 0) {
149 *locale = MFI_EVT_LOCALE_LD;
152 if (strncasecmp(arg, "drive", 5) == 0 || strcasecmp(arg, "pd") == 0) {
153 *locale = MFI_EVT_LOCALE_PD;
156 if (strncasecmp(arg, "encl", 4) == 0) {
157 *locale = MFI_EVT_LOCALE_ENCL;
160 if (strncasecmp(arg, "batt", 4) == 0 ||
161 strncasecmp(arg, "bbu", 3) == 0) {
162 *locale = MFI_EVT_LOCALE_BBU;
165 if (strcasecmp(arg, "sas") == 0) {
166 *locale = MFI_EVT_LOCALE_SAS;
169 if (strcasecmp(arg, "ctrl") == 0 || strncasecmp(arg, "cont", 4) == 0) {
170 *locale = MFI_EVT_LOCALE_CTRL;
173 if (strcasecmp(arg, "config") == 0) {
174 *locale = MFI_EVT_LOCALE_CONFIG;
177 if (strcasecmp(arg, "cluster") == 0) {
178 *locale = MFI_EVT_LOCALE_CLUSTER;
181 if (strcasecmp(arg, "all") == 0) {
182 *locale = MFI_EVT_LOCALE_ALL;
185 val = strtol(arg, &cp, 0);
186 if (*cp != '\0' || val < 0 || val > 0xffff) {
195 parse_class(char *arg, int8_t *class)
200 if (strcasecmp(arg, "debug") == 0) {
201 *class = MFI_EVT_CLASS_DEBUG;
204 if (strncasecmp(arg, "prog", 4) == 0) {
205 *class = MFI_EVT_CLASS_PROGRESS;
208 if (strncasecmp(arg, "info", 4) == 0) {
209 *class = MFI_EVT_CLASS_INFO;
212 if (strncasecmp(arg, "warn", 4) == 0) {
213 *class = MFI_EVT_CLASS_WARNING;
216 if (strncasecmp(arg, "crit", 4) == 0) {
217 *class = MFI_EVT_CLASS_CRITICAL;
220 if (strcasecmp(arg, "fatal") == 0) {
221 *class = MFI_EVT_CLASS_FATAL;
224 if (strcasecmp(arg, "dead") == 0) {
225 *class = MFI_EVT_CLASS_DEAD;
228 val = strtol(arg, &cp, 0);
229 if (*cp != '\0' || val < -128 || val > 127) {
238 * The timestamp is the number of seconds since 00:00 Jan 1, 2000. If
239 * the bits in 24-31 are all set, then it is the number of seconds since
243 format_timestamp(uint32_t timestamp)
245 static char buffer[32];
250 if ((timestamp & 0xff000000) == 0xff000000) {
251 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
257 /* Compute 00:00 Jan 1, 2000 offset. */
258 bzero(&tm, sizeof(tm));
260 tm.tm_year = (2000 - 1900);
264 snprintf(buffer, sizeof(buffer), "%us", timestamp);
267 t = base + timestamp;
268 strftime(buffer, sizeof(buffer), "%+", localtime(&t));
273 format_locale(uint16_t locale)
275 static char buffer[8];
278 case MFI_EVT_LOCALE_LD:
280 case MFI_EVT_LOCALE_PD:
282 case MFI_EVT_LOCALE_ENCL:
284 case MFI_EVT_LOCALE_BBU:
286 case MFI_EVT_LOCALE_SAS:
288 case MFI_EVT_LOCALE_CTRL:
290 case MFI_EVT_LOCALE_CONFIG:
292 case MFI_EVT_LOCALE_CLUSTER:
294 case MFI_EVT_LOCALE_ALL:
297 snprintf(buffer, sizeof(buffer), "0x%04x", locale);
303 format_class(int8_t class)
305 static char buffer[6];
308 case MFI_EVT_CLASS_DEBUG:
310 case MFI_EVT_CLASS_PROGRESS:
312 case MFI_EVT_CLASS_INFO:
314 case MFI_EVT_CLASS_WARNING:
316 case MFI_EVT_CLASS_CRITICAL:
318 case MFI_EVT_CLASS_FATAL:
320 case MFI_EVT_CLASS_DEAD:
323 snprintf(buffer, sizeof(buffer), "%d", class);
328 /* Simulates %D from kernel printf(9). */
330 simple_hex(void *ptr, size_t length, const char *separator)
338 printf("%02x", cp[0]);
339 for (i = 1; i < length; i++)
340 printf("%s%02x", separator, cp[i]);
344 pdrive_location(struct mfi_evt_pd *pd)
346 static char buffer[16];
348 if (pd->enclosure_index == 0)
349 snprintf(buffer, sizeof(buffer), "%02d(s%d)", pd->device_id,
352 snprintf(buffer, sizeof(buffer), "%02d(e%d/s%d)", pd->device_id,
353 pd->enclosure_index, pd->slot_number);
358 volume_name(int fd, struct mfi_evt_ld *ld)
361 return (mfi_volume_name(fd, ld->target_id));
364 /* Ripped from sys/dev/mfi/mfi.c. */
366 mfi_decode_evt(int fd, struct mfi_evt_detail *detail, int verbose)
369 printf("%5d (%s/%s/%s) - ", detail->seq, format_timestamp(detail->time),
370 format_locale(detail->evt_class.members.locale),
371 format_class(detail->evt_class.members.evt_class));
372 switch (detail->arg_type) {
373 case MR_EVT_ARGS_NONE:
375 case MR_EVT_ARGS_CDB_SENSE:
378 pdrive_location(&detail->args.cdb_sense.pd)
380 simple_hex(detail->args.cdb_sense.cdb,
381 detail->args.cdb_sense.cdb_len, ":");
383 simple_hex(detail->args.cdb_sense.sense,
384 detail->args.cdb_sense.sense_len, ":");
389 printf("VOL %s event: ", volume_name(fd, &detail->args.ld));
391 case MR_EVT_ARGS_LD_COUNT:
392 printf("VOL %s", volume_name(fd, &detail->args.ld_count.ld));
394 printf(" count %lld: ",
395 (long long)detail->args.ld_count.count);
399 case MR_EVT_ARGS_LD_LBA:
400 printf("VOL %s", volume_name(fd, &detail->args.ld_count.ld));
403 (long long)detail->args.ld_lba.lba);
407 case MR_EVT_ARGS_LD_OWNER:
408 printf("VOL %s", volume_name(fd, &detail->args.ld_count.ld));
410 printf(" owner changed: prior %d, new %d",
411 detail->args.ld_owner.pre_owner,
412 detail->args.ld_owner.new_owner);
416 case MR_EVT_ARGS_LD_LBA_PD_LBA:
417 printf("VOL %s", volume_name(fd, &detail->args.ld_count.ld));
419 printf(" lba %lld, physical drive PD %s lba %lld",
420 (long long)detail->args.ld_lba_pd_lba.ld_lba,
421 pdrive_location(&detail->args.ld_lba_pd_lba.pd),
422 (long long)detail->args.ld_lba_pd_lba.pd_lba);
426 case MR_EVT_ARGS_LD_PROG:
427 printf("VOL %s", volume_name(fd, &detail->args.ld_prog.ld));
429 printf(" progress %d%% in %ds",
430 detail->args.ld_prog.prog.progress/655,
431 detail->args.ld_prog.prog.elapsed_seconds);
435 case MR_EVT_ARGS_LD_STATE:
436 printf("VOL %s", volume_name(fd, &detail->args.ld_prog.ld));
438 printf(" state prior %s new %s",
439 mfi_ldstate(detail->args.ld_state.prev_state),
440 mfi_ldstate(detail->args.ld_state.new_state));
444 case MR_EVT_ARGS_LD_STRIP:
445 printf("VOL %s", volume_name(fd, &detail->args.ld_strip.ld));
447 printf(" strip %lld",
448 (long long)detail->args.ld_strip.strip);
454 printf("PD %s event: ",
455 pdrive_location(&detail->args.pd));
458 case MR_EVT_ARGS_PD_ERR:
460 printf("PD %s err %d: ",
461 pdrive_location(&detail->args.pd_err.pd),
462 detail->args.pd_err.err);
465 case MR_EVT_ARGS_PD_LBA:
467 printf("PD %s lba %lld: ",
468 pdrive_location(&detail->args.pd_lba.pd),
469 (long long)detail->args.pd_lba.lba);
472 case MR_EVT_ARGS_PD_LBA_LD:
474 printf("PD %s lba %lld VOL %s: ",
475 pdrive_location(&detail->args.pd_lba_ld.pd),
476 (long long)detail->args.pd_lba.lba,
477 volume_name(fd, &detail->args.pd_lba_ld.ld));
480 case MR_EVT_ARGS_PD_PROG:
482 printf("PD %s progress %d%% seconds %ds: ",
483 pdrive_location(&detail->args.pd_prog.pd),
484 detail->args.pd_prog.prog.progress/655,
485 detail->args.pd_prog.prog.elapsed_seconds);
488 case MR_EVT_ARGS_PD_STATE:
490 printf("PD %s state prior %s new %s: ",
491 pdrive_location(&detail->args.pd_prog.pd),
492 mfi_pdstate(detail->args.pd_state.prev_state),
493 mfi_pdstate(detail->args.pd_state.new_state));
496 case MR_EVT_ARGS_PCI:
498 printf("PCI 0x%04x 0x%04x 0x%04x 0x%04x: ",
499 detail->args.pci.venderId,
500 detail->args.pci.deviceId,
501 detail->args.pci.subVenderId,
502 detail->args.pci.subDeviceId);
505 case MR_EVT_ARGS_RATE:
507 printf("Rebuild rate %d: ", detail->args.rate);
510 case MR_EVT_ARGS_TIME:
512 printf("Adapter time %s; %d seconds since power on: ",
513 format_timestamp(detail->args.time.rtc),
514 detail->args.time.elapsedSeconds);
517 case MR_EVT_ARGS_ECC:
519 printf("Adapter ECC %x,%x: %s: ",
520 detail->args.ecc.ecar,
521 detail->args.ecc.elog,
522 detail->args.ecc.str);
527 printf("Type %d: ", detail->arg_type);
531 printf("%s\n", detail->description);
535 show_events(int ac, char **av)
537 struct mfi_evt_log_state info;
538 struct mfi_evt_list *list;
539 union mfi_evt filter;
544 uint32_t seq, start, stop;
547 int ch, error, fd, num_events, verbose;
550 fd = mfi_open(mfi_unit, O_RDWR);
557 if (mfi_event_get_info(fd, &info, NULL) < 0) {
559 warn("Failed to get event log info");
564 /* Default settings. */
566 filter.members.reserved = 0;
567 filter.members.locale = MFI_EVT_LOCALE_ALL;
568 filter.members.evt_class = MFI_EVT_CLASS_WARNING;
569 start = info.boot_seq_num;
570 stop = info.newest_seq_num;
573 /* Parse any options. */
575 while ((ch = getopt(ac, av, "c:l:n:v")) != -1) {
578 if (parse_class(optarg, &filter.members.evt_class) < 0) {
580 warn("Error parsing event class");
586 if (parse_locale(optarg, &locale) < 0) {
588 warn("Error parsing event locale");
592 filter.members.locale = locale;
595 val = strtol(optarg, &cp, 0);
596 if (*cp != '\0' || val <= 0) {
597 warnx("Invalid event count");
615 /* Determine buffer size and validate it. */
616 size = sizeof(struct mfi_evt_list) + sizeof(struct mfi_evt_detail) *
618 if (size > getpagesize()) {
619 warnx("Event count is too high");
624 /* Handle optional start and stop sequence numbers. */
626 warnx("show events: extra arguments");
630 if (ac > 0 && parse_seq(&info, av[0], &start) < 0) {
632 warn("Error parsing starting sequence number");
636 if (ac > 1 && parse_seq(&info, av[1], &stop) < 0) {
638 warn("Error parsing ending sequence number");
645 warnx("malloc failed");
652 if (mfi_get_events(fd, list, num_events, filter, seq,
655 warn("Failed to fetch events");
660 if (status == MFI_STAT_NOT_FOUND) {
663 if (status != MFI_STAT_OK) {
664 warnx("Error fetching events: %s", mfi_status(status));
670 for (i = 0; i < list->count; i++) {
672 * If this event is newer than 'stop_seq' then
673 * break out of the loop. Note that the log
674 * is a circular buffer so we have to handle
675 * the case that our stop point is earlier in
676 * the buffer than our start point.
678 if (list->event[i].seq > stop) {
681 else if (list->event[i].seq < start)
684 mfi_decode_evt(fd, &list->event[i], verbose);
689 * XXX: If the event's seq # is the end of the buffer
690 * then this probably won't do the right thing. We
691 * need to know the size of the buffer somehow.
693 seq = list->event[list->count - 1].seq + 1;
698 warnx("No matching events found");
705 MFI_COMMAND(show, events, show_events);