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