xref: /freebsd/usr.sbin/mpsutil/mps_debug.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*-
2  * Copyright (c) 2018 Netflix, Inc.
3  * Written by: Scott Long <scottl@freebsd.org>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/errno.h>
33 #include <sys/types.h>
34 #include <sys/sysctl.h>
35 #include <err.h>
36 #include <libutil.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include "mpsutil.h"
42 
43 MPS_TABLE(top, debug);
44 
45 struct mps_dumpreq_hdr {
46 	uint32_t	smid;
47 	uint32_t	state;
48 	uint32_t	numframes;
49 	uint32_t	deschi;
50 	uint32_t	desclo;
51 };
52 
53 static int find_sgl(char *);
54 static void print_sgl(char *, int, int);
55 
56 #define MPS_FRAME_LEN 128
57 
58 static int
59 debug_dumpreqs(int ac, char **av)
60 {
61 	struct mps_dumpreq_hdr *hdr;
62 	char *buf, sysctlbuf[128];
63 	size_t len;
64 	int numframes, error, offset;
65 
66 	len = 0;
67 	buf = NULL;
68 	snprintf(sysctlbuf, sizeof(sysctlbuf), "dev.%s.%d.dump_reqs",
69 	    is_mps ? "mps" : "mpr", mps_unit);
70 
71 	error = sysctlbyname(sysctlbuf, NULL, &len, NULL, 0);
72 	if (error)
73 		return (error);
74 
75 	if (len == 0)
76 		return (0);
77 
78 	buf = malloc(len);
79 	if (buf == NULL)
80 		return (ENOMEM);
81 
82 	error = sysctlbyname(sysctlbuf, buf, &len, NULL, 0);
83 	if (error) {
84 		printf("len= %zd, error= %d errno= %d\n", len, error, errno);
85 		return (error);
86 	}
87 
88 	while (len >= MPS_FRAME_LEN) {
89 		hdr = (struct mps_dumpreq_hdr *)buf;
90 		numframes = hdr->numframes;
91 
92 		printf("SMID= %d state= %#x numframes= %d desc.hi= %#08x "
93 		    "desc.lo= %#08x\n", hdr->smid, hdr->state,
94 		    hdr->numframes, hdr->deschi, hdr->desclo);
95 
96 		buf += sizeof(struct mps_dumpreq_hdr);
97 		len -= sizeof(struct mps_dumpreq_hdr);
98 
99 		if ((offset = find_sgl(buf)) != -1)
100 			print_sgl(buf, offset, numframes);
101 
102 		buf += MPS_FRAME_LEN * numframes;
103 		len -= MPS_FRAME_LEN * numframes;
104 	}
105 
106 	return (error);
107 }
108 
109 static int
110 find_sgl(char *buf)
111 {
112 	MPI2_REQUEST_HEADER *req;
113 	MPI2_SCSI_IO_REQUEST *scsi;
114 	int offset = 0;
115 
116 	req = (MPI2_REQUEST_HEADER *)buf;
117 
118 	switch (req->Function) {
119 	case MPI2_FUNCTION_SCSI_IO_REQUEST:
120 		scsi = (MPI2_SCSI_IO_REQUEST *)buf;
121 		offset = scsi->SGLOffset0;
122 		break;
123 	default:
124 		offset = -1;
125 	}
126 
127 	return (offset);
128 }
129 
130 #define SGL_FLAGS "\10LastElement\7EndOfBuffer\4Local\3Host2IOC\2Addr64\1EndOfList"
131 
132 static void
133 print_sgl(char *buf, int offset, int numframes)
134 {
135 	MPI2_SGE_SIMPLE64 *sge;
136 	MPI2_SGE_CHAIN_UNION *sgc;
137 	u_int i = 0, flags;
138 	char *frame, tmpbuf[128];
139 
140 	frame = (char *)buf;
141 	sge = (MPI2_SGE_SIMPLE64 *)&frame[offset * 4];
142 	printf("SGL for command\n");
143 
144 	hexdump(frame, MPS_FRAME_LEN, NULL, 0);
145 	while (frame != NULL) {
146 		flags = sge->FlagsLength >> MPI2_SGE_FLAGS_SHIFT;
147 		bzero(tmpbuf, sizeof(tmpbuf));
148 		mps_parse_flags(flags, SGL_FLAGS, tmpbuf, sizeof(tmpbuf));
149 		printf("seg%d flags=%x %s len= 0x%06x addr=0x%016jx\n", i,
150 		    flags, tmpbuf, sge->FlagsLength & 0xffffff,
151 		    mps_to_u64(&sge->Address));
152 		if (flags & (MPI2_SGE_FLAGS_END_OF_LIST |
153 		    MPI2_SGE_FLAGS_END_OF_BUFFER))
154 			break;
155 		sge++;
156 		i++;
157 		if (flags & MPI2_SGE_FLAGS_LAST_ELEMENT) {
158 			sgc = (MPI2_SGE_CHAIN_UNION *)sge;
159 			if ((sgc->Flags & MPI2_SGE_FLAGS_CHAIN_ELEMENT) == 0) {
160 				printf("Invalid chain element\n");
161 				break;
162 			}
163 			bzero(tmpbuf, sizeof(tmpbuf));
164 			mps_parse_flags(sgc->Flags, SGL_FLAGS, tmpbuf,
165 			    sizeof(tmpbuf));
166 			if (sgc->Flags & MPI2_SGE_FLAGS_64_BIT_ADDRESSING)
167 				printf("chain64 flags=0x%x %s len=0x%x "
168 				    "Offset=0x%x addr=0x%016jx\n", sgc->Flags,
169 				    tmpbuf, sgc->Length, sgc->NextChainOffset,
170 				    mps_to_u64(&sgc->u.Address64));
171 			else
172 				printf("chain32 flags=0x%x %s len=0x%x "
173 				    "Offset=0x%x addr=0x%08x\n", sgc->Flags,
174 				    tmpbuf, sgc->Length, sgc->NextChainOffset,
175 				    sgc->u.Address32);
176 			if (--numframes <= 0)
177 				break;
178 			frame += MPS_FRAME_LEN;
179 			sge = (MPI2_SGE_SIMPLE64 *)frame;
180 			hexdump(frame, MPS_FRAME_LEN, NULL, 0);
181 		}
182 	}
183 }
184 
185 MPS_COMMAND(debug, dumpreqs, debug_dumpreqs, "", "Dump the active request queue")
186