xref: /freebsd-src/usr.sbin/mptutil/mpt_evt.c (revision 4d65a7c6951cea0333f1a0c1b32c38489cdfa6c5)
1fc58801cSScott Long /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
4fc58801cSScott Long  * Copyright (c) 2008 Yahoo!, Inc.
5fc58801cSScott Long  * All rights reserved.
6fc58801cSScott Long  * Written by: John Baldwin <jhb@FreeBSD.org>
7fc58801cSScott Long  *
8fc58801cSScott Long  * Redistribution and use in source and binary forms, with or without
9fc58801cSScott Long  * modification, are permitted provided that the following conditions
10fc58801cSScott Long  * are met:
11fc58801cSScott Long  * 1. Redistributions of source code must retain the above copyright
12fc58801cSScott Long  *    notice, this list of conditions and the following disclaimer.
13fc58801cSScott Long  * 2. Redistributions in binary form must reproduce the above copyright
14fc58801cSScott Long  *    notice, this list of conditions and the following disclaimer in the
15fc58801cSScott Long  *    documentation and/or other materials provided with the distribution.
16fc58801cSScott Long  * 3. Neither the name of the author nor the names of any co-contributors
17fc58801cSScott Long  *    may be used to endorse or promote products derived from this software
18fc58801cSScott Long  *    without specific prior written permission.
19fc58801cSScott Long  *
20fc58801cSScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21fc58801cSScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22fc58801cSScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23fc58801cSScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24fc58801cSScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25fc58801cSScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26fc58801cSScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27fc58801cSScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28fc58801cSScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29fc58801cSScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30fc58801cSScott Long  * SUCH DAMAGE.
31fc58801cSScott Long  */
32fc58801cSScott Long 
33fc58801cSScott Long #include <sys/param.h>
34fc58801cSScott Long #include <sys/errno.h>
35fc58801cSScott Long #include <ctype.h>
36fc58801cSScott Long #include <err.h>
37fc58801cSScott Long #include <stdio.h>
38fc58801cSScott Long #include <stdlib.h>
39fc58801cSScott Long #include <unistd.h>
40fc58801cSScott Long #include "mptutil.h"
41fc58801cSScott Long 
42fc58801cSScott Long static CONFIG_PAGE_LOG_0 *
mpt_get_events(int fd,U16 * IOCStatus)43fc58801cSScott Long mpt_get_events(int fd, U16 *IOCStatus)
44fc58801cSScott Long {
45fc58801cSScott Long 
46fc58801cSScott Long 	return (mpt_read_extended_config_page(fd, MPI_CONFIG_EXTPAGETYPE_LOG,
47fc58801cSScott Long 	    0, 0, 0, IOCStatus));
48fc58801cSScott Long }
49fc58801cSScott Long 
50fc58801cSScott Long /*
51fc58801cSScott Long  *          1         2         3         4         5         6         7
52fc58801cSScott Long  * 1234567890123456789012345678901234567890123456789012345678901234567890
53fc58801cSScott Long  * < ID> < time > <ty> <X XX XX XX XX XX XX XX XX XX XX XX XX XX |..............|
54fc58801cSScott Long  *  ID     Time   Type Log Data
55fc58801cSScott Long  */
56fc58801cSScott Long static void
mpt_print_event(MPI_LOG_0_ENTRY * entry,int verbose)57fc58801cSScott Long mpt_print_event(MPI_LOG_0_ENTRY *entry, int verbose)
58fc58801cSScott Long {
59fc58801cSScott Long 	int i;
60fc58801cSScott Long 
61fc58801cSScott Long 	printf("%5d %7ds %4x ", entry->LogSequence, entry->TimeStamp,
62fc58801cSScott Long 	    entry->LogEntryQualifier);
63fc58801cSScott Long 	for (i = 0; i < 14; i++)
64fc58801cSScott Long 		printf("%02x ", entry->LogData[i]);
65fc58801cSScott Long 	printf("|");
66fc58801cSScott Long 	for (i = 0; i < 14; i++)
67fc58801cSScott Long 		printf("%c", isprint(entry->LogData[i]) ? entry->LogData[i] :
68fc58801cSScott Long 		    '.');
69fc58801cSScott Long 	printf("|\n");
70fc58801cSScott Long 	printf("                    ");
71fc58801cSScott Long 	for (i = 0; i < 14; i++)
72fc58801cSScott Long 		printf("%02x ", entry->LogData[i + 14]);
73fc58801cSScott Long 	printf("|");
74fc58801cSScott Long 	for (i = 0; i < 14; i++)
75fc58801cSScott Long 		printf("%c", isprint(entry->LogData[i + 14]) ?
76fc58801cSScott Long 		    entry->LogData[i + 14] : '.');
77fc58801cSScott Long 	printf("|\n");
78fc58801cSScott Long }
79fc58801cSScott Long 
80fc58801cSScott Long static int
event_compare(const void * first,const void * second)81fc58801cSScott Long event_compare(const void *first, const void *second)
82fc58801cSScott Long {
83fc58801cSScott Long 	MPI_LOG_0_ENTRY * const *one;
84fc58801cSScott Long 	MPI_LOG_0_ENTRY * const *two;
85fc58801cSScott Long 
86fc58801cSScott Long 	one = first;
87fc58801cSScott Long 	two = second;
88fc58801cSScott Long 	return ((*one)->LogSequence - ((*two)->LogSequence));
89fc58801cSScott Long }
90fc58801cSScott Long 
91fc58801cSScott Long static int
show_events(int ac,char ** av)92fc58801cSScott Long show_events(int ac, char **av)
93fc58801cSScott Long {
94fc58801cSScott Long 	CONFIG_PAGE_LOG_0 *log;
95fc58801cSScott Long 	MPI_LOG_0_ENTRY **entries;
96c5f2b79dSJohn Baldwin 	int ch, error, fd, i, num_events, verbose;
97fc58801cSScott Long 
98fc58801cSScott Long 	fd = mpt_open(mpt_unit);
99fc58801cSScott Long 	if (fd < 0) {
100c5f2b79dSJohn Baldwin 		error = errno;
101fc58801cSScott Long 		warn("mpt_open");
102c5f2b79dSJohn Baldwin 		return (error);
103fc58801cSScott Long 	}
104fc58801cSScott Long 
105fc58801cSScott Long 	log = mpt_get_events(fd, NULL);
106fc58801cSScott Long 	if (log == NULL) {
107c5f2b79dSJohn Baldwin 		error = errno;
108fc58801cSScott Long 		warn("Failed to get event log info");
109c5f2b79dSJohn Baldwin 		return (error);
110fc58801cSScott Long 	}
111fc58801cSScott Long 
112fc58801cSScott Long 	/* Default settings. */
113fc58801cSScott Long 	verbose = 0;
114fc58801cSScott Long 
115fc58801cSScott Long 	/* Parse any options. */
116fc58801cSScott Long 	optind = 1;
117fc58801cSScott Long 	while ((ch = getopt(ac, av, "v")) != -1) {
118fc58801cSScott Long 		switch (ch) {
119fc58801cSScott Long 		case 'v':
120fc58801cSScott Long 			verbose = 1;
121fc58801cSScott Long 			break;
122fc58801cSScott Long 		case '?':
123fc58801cSScott Long 		default:
124*d68b76a9SAlan Somers 			free(log);
125*d68b76a9SAlan Somers 			close(fd);
126fc58801cSScott Long 			return (EINVAL);
127fc58801cSScott Long 		}
128fc58801cSScott Long 	}
129fc58801cSScott Long 	ac -= optind;
130fc58801cSScott Long 	av += optind;
131fc58801cSScott Long 
132fc58801cSScott Long 	/* Build a list of valid entries and sort them by sequence. */
133fc58801cSScott Long 	entries = malloc(sizeof(MPI_LOG_0_ENTRY *) * log->NumLogEntries);
134*d68b76a9SAlan Somers 	if (entries == NULL) {
135*d68b76a9SAlan Somers 		free(log);
136*d68b76a9SAlan Somers 		close(fd);
137c5f2b79dSJohn Baldwin 		return (ENOMEM);
138*d68b76a9SAlan Somers 	}
139fc58801cSScott Long 	num_events = 0;
140fc58801cSScott Long 	for (i = 0; i < log->NumLogEntries; i++) {
141fc58801cSScott Long 		if (log->LogEntry[i].LogEntryQualifier ==
142fc58801cSScott Long 		    MPI_LOG_0_ENTRY_QUAL_ENTRY_UNUSED)
143fc58801cSScott Long 			continue;
144fc58801cSScott Long 		entries[num_events] = &log->LogEntry[i];
145fc58801cSScott Long 		num_events++;
146fc58801cSScott Long 	}
147fc58801cSScott Long 
148fc58801cSScott Long 	qsort(entries, num_events, sizeof(MPI_LOG_0_ENTRY *), event_compare);
149fc58801cSScott Long 
150fc58801cSScott Long 	if (num_events == 0)
151fc58801cSScott Long 		printf("Event log is empty\n");
152fc58801cSScott Long 	else {
153fc58801cSScott Long 		printf(" ID     Time   Type Log Data\n");
154fc58801cSScott Long 		for (i = 0; i < num_events; i++)
155fc58801cSScott Long 			mpt_print_event(entries[i], verbose);
156fc58801cSScott Long 	}
157fc58801cSScott Long 
158fc58801cSScott Long 	free(entries);
159*d68b76a9SAlan Somers 	free(log);
160fc58801cSScott Long 	close(fd);
161fc58801cSScott Long 
162fc58801cSScott Long 	return (0);
163fc58801cSScott Long }
164fc58801cSScott Long MPT_COMMAND(show, events, show_events);
165