186d7f5d3SJohn Marino /*-
286d7f5d3SJohn Marino * Copyright (c) 2008 Yahoo!, Inc.
386d7f5d3SJohn Marino * All rights reserved.
486d7f5d3SJohn Marino * Written by: John Baldwin <jhb@FreeBSD.org>
586d7f5d3SJohn Marino *
686d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
786d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
886d7f5d3SJohn Marino * are met:
986d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
1086d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
1186d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
1286d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
1386d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
1486d7f5d3SJohn Marino * 3. Neither the name of the author nor the names of any co-contributors
1586d7f5d3SJohn Marino * may be used to endorse or promote products derived from this software
1686d7f5d3SJohn Marino * without specific prior written permission.
1786d7f5d3SJohn Marino *
1886d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1986d7f5d3SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2086d7f5d3SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2186d7f5d3SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2286d7f5d3SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2386d7f5d3SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2486d7f5d3SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2586d7f5d3SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2686d7f5d3SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2786d7f5d3SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2886d7f5d3SJohn Marino * SUCH DAMAGE.
2986d7f5d3SJohn Marino *
3086d7f5d3SJohn Marino * $FreeBSD: src/usr.sbin/mptutil/mpt_evt.c,v 1.2 2010/11/09 19:28:06 jhb Exp $
3186d7f5d3SJohn Marino */
3286d7f5d3SJohn Marino
3386d7f5d3SJohn Marino #include <sys/param.h>
3486d7f5d3SJohn Marino #include <sys/errno.h>
3586d7f5d3SJohn Marino #include <ctype.h>
3686d7f5d3SJohn Marino #include <err.h>
3786d7f5d3SJohn Marino #include <stdio.h>
3886d7f5d3SJohn Marino #include <stdlib.h>
3986d7f5d3SJohn Marino #include <unistd.h>
4086d7f5d3SJohn Marino #include "mptutil.h"
4186d7f5d3SJohn Marino
4286d7f5d3SJohn Marino static CONFIG_PAGE_LOG_0 *
mpt_get_events(int fd,U16 * IOCStatus)4386d7f5d3SJohn Marino mpt_get_events(int fd, U16 *IOCStatus)
4486d7f5d3SJohn Marino {
4586d7f5d3SJohn Marino
4686d7f5d3SJohn Marino return (mpt_read_extended_config_page(fd, MPI_CONFIG_EXTPAGETYPE_LOG,
4786d7f5d3SJohn Marino 0, 0, 0, IOCStatus));
4886d7f5d3SJohn Marino }
4986d7f5d3SJohn Marino
5086d7f5d3SJohn Marino /*
5186d7f5d3SJohn Marino * 1 2 3 4 5 6 7
5286d7f5d3SJohn Marino * 1234567890123456789012345678901234567890123456789012345678901234567890
5386d7f5d3SJohn Marino * < ID> < time > <ty> <X XX XX XX XX XX XX XX XX XX XX XX XX XX |..............|
5486d7f5d3SJohn Marino * ID Time Type Log Data
5586d7f5d3SJohn Marino */
5686d7f5d3SJohn Marino static void
mpt_print_event(MPI_LOG_0_ENTRY * entry,int verbose __unused)5786d7f5d3SJohn Marino mpt_print_event(MPI_LOG_0_ENTRY *entry, int verbose __unused)
5886d7f5d3SJohn Marino {
5986d7f5d3SJohn Marino int i;
6086d7f5d3SJohn Marino
6186d7f5d3SJohn Marino printf("%5d %7ds %4x ", entry->LogSequence, entry->TimeStamp,
6286d7f5d3SJohn Marino entry->LogEntryQualifier);
6386d7f5d3SJohn Marino for (i = 0; i < 14; i++)
6486d7f5d3SJohn Marino printf("%02x ", entry->LogData[i]);
6586d7f5d3SJohn Marino printf("|");
6686d7f5d3SJohn Marino for (i = 0; i < 14; i++)
6786d7f5d3SJohn Marino printf("%c", isprint(entry->LogData[i]) ? entry->LogData[i] :
6886d7f5d3SJohn Marino '.');
6986d7f5d3SJohn Marino printf("|\n");
7086d7f5d3SJohn Marino printf(" ");
7186d7f5d3SJohn Marino for (i = 0; i < 14; i++)
7286d7f5d3SJohn Marino printf("%02x ", entry->LogData[i + 14]);
7386d7f5d3SJohn Marino printf("|");
7486d7f5d3SJohn Marino for (i = 0; i < 14; i++)
7586d7f5d3SJohn Marino printf("%c", isprint(entry->LogData[i + 14]) ?
7686d7f5d3SJohn Marino entry->LogData[i + 14] : '.');
7786d7f5d3SJohn Marino printf("|\n");
7886d7f5d3SJohn Marino }
7986d7f5d3SJohn Marino
8086d7f5d3SJohn Marino static int
event_compare(const void * first,const void * second)8186d7f5d3SJohn Marino event_compare(const void *first, const void *second)
8286d7f5d3SJohn Marino {
8386d7f5d3SJohn Marino MPI_LOG_0_ENTRY * const *one;
8486d7f5d3SJohn Marino MPI_LOG_0_ENTRY * const *two;
8586d7f5d3SJohn Marino
8686d7f5d3SJohn Marino one = first;
8786d7f5d3SJohn Marino two = second;
8886d7f5d3SJohn Marino return ((*one)->LogSequence - ((*two)->LogSequence));
8986d7f5d3SJohn Marino }
9086d7f5d3SJohn Marino
9186d7f5d3SJohn Marino static int
show_events(int ac,char ** av)9286d7f5d3SJohn Marino show_events(int ac, char **av)
9386d7f5d3SJohn Marino {
9486d7f5d3SJohn Marino CONFIG_PAGE_LOG_0 *log;
9586d7f5d3SJohn Marino MPI_LOG_0_ENTRY **entries;
9686d7f5d3SJohn Marino int ch, error, fd, i, num_events, verbose;
9786d7f5d3SJohn Marino
9886d7f5d3SJohn Marino fd = mpt_open(mpt_unit);
9986d7f5d3SJohn Marino if (fd < 0) {
10086d7f5d3SJohn Marino error = errno;
10186d7f5d3SJohn Marino warn("mpt_open");
10286d7f5d3SJohn Marino return (error);
10386d7f5d3SJohn Marino }
10486d7f5d3SJohn Marino
10586d7f5d3SJohn Marino log = mpt_get_events(fd, NULL);
10686d7f5d3SJohn Marino if (log == NULL) {
10786d7f5d3SJohn Marino error = errno;
10886d7f5d3SJohn Marino warn("Failed to get event log info");
10986d7f5d3SJohn Marino return (error);
11086d7f5d3SJohn Marino }
11186d7f5d3SJohn Marino
11286d7f5d3SJohn Marino /* Default settings. */
11386d7f5d3SJohn Marino verbose = 0;
11486d7f5d3SJohn Marino
11586d7f5d3SJohn Marino /* Parse any options. */
11686d7f5d3SJohn Marino optind = 1;
11786d7f5d3SJohn Marino while ((ch = getopt(ac, av, "v")) != -1) {
11886d7f5d3SJohn Marino switch (ch) {
11986d7f5d3SJohn Marino case 'v':
12086d7f5d3SJohn Marino verbose = 1;
12186d7f5d3SJohn Marino break;
12286d7f5d3SJohn Marino case '?':
12386d7f5d3SJohn Marino default:
12486d7f5d3SJohn Marino return (EINVAL);
12586d7f5d3SJohn Marino }
12686d7f5d3SJohn Marino }
12786d7f5d3SJohn Marino ac -= optind;
12886d7f5d3SJohn Marino av += optind;
12986d7f5d3SJohn Marino
13086d7f5d3SJohn Marino /* Build a list of valid entries and sort them by sequence. */
13186d7f5d3SJohn Marino entries = malloc(sizeof(MPI_LOG_0_ENTRY *) * log->NumLogEntries);
13286d7f5d3SJohn Marino if (entries == NULL)
13386d7f5d3SJohn Marino return (ENOMEM);
13486d7f5d3SJohn Marino num_events = 0;
13586d7f5d3SJohn Marino for (i = 0; i < log->NumLogEntries; i++) {
13686d7f5d3SJohn Marino if (log->LogEntry[i].LogEntryQualifier ==
13786d7f5d3SJohn Marino MPI_LOG_0_ENTRY_QUAL_ENTRY_UNUSED)
13886d7f5d3SJohn Marino continue;
13986d7f5d3SJohn Marino entries[num_events] = &log->LogEntry[i];
14086d7f5d3SJohn Marino num_events++;
14186d7f5d3SJohn Marino }
14286d7f5d3SJohn Marino
14386d7f5d3SJohn Marino qsort(entries, num_events, sizeof(MPI_LOG_0_ENTRY *), event_compare);
14486d7f5d3SJohn Marino
14586d7f5d3SJohn Marino if (num_events == 0)
14686d7f5d3SJohn Marino printf("Event log is empty\n");
14786d7f5d3SJohn Marino else {
14886d7f5d3SJohn Marino printf(" ID Time Type Log Data\n");
14986d7f5d3SJohn Marino for (i = 0; i < num_events; i++)
15086d7f5d3SJohn Marino mpt_print_event(entries[i], verbose);
15186d7f5d3SJohn Marino }
15286d7f5d3SJohn Marino
15386d7f5d3SJohn Marino free(entries);
15486d7f5d3SJohn Marino close(fd);
15586d7f5d3SJohn Marino
15686d7f5d3SJohn Marino return (0);
15786d7f5d3SJohn Marino }
15886d7f5d3SJohn Marino MPT_COMMAND(show, events, show_events);
159