xref: /freebsd/usr.sbin/mptutil/mpt_evt.c (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
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/param.h>
34 #include <sys/errno.h>
35 #include <ctype.h>
36 #include <err.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include "mptutil.h"
41 
42 static CONFIG_PAGE_LOG_0 *
43 mpt_get_events(int fd, U16 *IOCStatus)
44 {
45 
46 	return (mpt_read_extended_config_page(fd, MPI_CONFIG_EXTPAGETYPE_LOG,
47 	    0, 0, 0, IOCStatus));
48 }
49 
50 /*
51  *          1         2         3         4         5         6         7
52  * 1234567890123456789012345678901234567890123456789012345678901234567890
53  * < ID> < time > <ty> <X XX XX XX XX XX XX XX XX XX XX XX XX XX |..............|
54  *  ID     Time   Type Log Data
55  */
56 static void
57 mpt_print_event(MPI_LOG_0_ENTRY *entry, int verbose)
58 {
59 	int i;
60 
61 	printf("%5d %7ds %4x ", entry->LogSequence, entry->TimeStamp,
62 	    entry->LogEntryQualifier);
63 	for (i = 0; i < 14; i++)
64 		printf("%02x ", entry->LogData[i]);
65 	printf("|");
66 	for (i = 0; i < 14; i++)
67 		printf("%c", isprint(entry->LogData[i]) ? entry->LogData[i] :
68 		    '.');
69 	printf("|\n");
70 	printf("                    ");
71 	for (i = 0; i < 14; i++)
72 		printf("%02x ", entry->LogData[i + 14]);
73 	printf("|");
74 	for (i = 0; i < 14; i++)
75 		printf("%c", isprint(entry->LogData[i + 14]) ?
76 		    entry->LogData[i + 14] : '.');
77 	printf("|\n");
78 }
79 
80 static int
81 event_compare(const void *first, const void *second)
82 {
83 	MPI_LOG_0_ENTRY * const *one;
84 	MPI_LOG_0_ENTRY * const *two;
85 
86 	one = first;
87 	two = second;
88 	return ((*one)->LogSequence - ((*two)->LogSequence));
89 }
90 
91 static int
92 show_events(int ac, char **av)
93 {
94 	CONFIG_PAGE_LOG_0 *log;
95 	MPI_LOG_0_ENTRY **entries;
96 	int ch, error, fd, i, num_events, verbose;
97 
98 	fd = mpt_open(mpt_unit);
99 	if (fd < 0) {
100 		error = errno;
101 		warn("mpt_open");
102 		return (error);
103 	}
104 
105 	log = mpt_get_events(fd, NULL);
106 	if (log == NULL) {
107 		error = errno;
108 		warn("Failed to get event log info");
109 		return (error);
110 	}
111 
112 	/* Default settings. */
113 	verbose = 0;
114 
115 	/* Parse any options. */
116 	optind = 1;
117 	while ((ch = getopt(ac, av, "v")) != -1) {
118 		switch (ch) {
119 		case 'v':
120 			verbose = 1;
121 			break;
122 		case '?':
123 		default:
124 			free(log);
125 			close(fd);
126 			return (EINVAL);
127 		}
128 	}
129 	ac -= optind;
130 	av += optind;
131 
132 	/* Build a list of valid entries and sort them by sequence. */
133 	entries = malloc(sizeof(MPI_LOG_0_ENTRY *) * log->NumLogEntries);
134 	if (entries == NULL) {
135 		free(log);
136 		close(fd);
137 		return (ENOMEM);
138 	}
139 	num_events = 0;
140 	for (i = 0; i < log->NumLogEntries; i++) {
141 		if (log->LogEntry[i].LogEntryQualifier ==
142 		    MPI_LOG_0_ENTRY_QUAL_ENTRY_UNUSED)
143 			continue;
144 		entries[num_events] = &log->LogEntry[i];
145 		num_events++;
146 	}
147 
148 	qsort(entries, num_events, sizeof(MPI_LOG_0_ENTRY *), event_compare);
149 
150 	if (num_events == 0)
151 		printf("Event log is empty\n");
152 	else {
153 		printf(" ID     Time   Type Log Data\n");
154 		for (i = 0; i < num_events; i++)
155 			mpt_print_event(entries[i], verbose);
156 	}
157 
158 	free(entries);
159 	free(log);
160 	close(fd);
161 
162 	return (0);
163 }
164 MPT_COMMAND(show, events, show_events);
165