xref: /freebsd/usr.sbin/mpsutil/mps_debug.c (revision 580744621f33383027108364dcadad718df46ffe)
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 __RCSID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/errno.h>
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
37 #include <err.h>
38 #include <libutil.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include "mpsutil.h"
44 
45 MPS_TABLE(top, debug);
46 
47 struct mps_dumpreq_hdr {
48 	uint32_t	smid;
49 	uint32_t	state;
50 	uint32_t	numframes;
51 	uint32_t	deschi;
52 	uint32_t	desclo;
53 };
54 
55 static int find_sgl(char *);
56 static void print_sgl(char *, int, int);
57 
58 #define MPS_FRAME_LEN 128
59 
60 static int
61 debug_dumpreqs(int ac, char **av)
62 {
63 	struct mps_dumpreq_hdr *hdr;
64 	char *buf, sysctlbuf[128];
65 	size_t len;
66 	int numframes, error, offset;
67 
68 	len = 0;
69 	buf = NULL;
70 	snprintf(sysctlbuf, sizeof(sysctlbuf), "dev.%s.%d.dump_reqs",
71 	    is_mps ? "mps" : "mpr", mps_unit);
72 
73 	error = sysctlbyname(sysctlbuf, NULL, &len, NULL, 0);
74 	if (error)
75 		return (error);
76 
77 	if (len == 0)
78 		return (0);
79 
80 	buf = malloc(len);
81 	if (buf == NULL)
82 		return (ENOMEM);
83 
84 	error = sysctlbyname(sysctlbuf, buf, &len, NULL, 0);
85 	if (error) {
86 		printf("len= %zd, error= %d errno= %d\n", len, error, errno);
87 		return (error);
88 	}
89 
90 	while (len >= MPS_FRAME_LEN) {
91 		hdr = (struct mps_dumpreq_hdr *)buf;
92 		numframes = hdr->numframes;
93 
94 		printf("SMID= %d state= %#x numframes= %d desc.hi= %#08x "
95 		    "desc.lo= %#08x\n", hdr->smid, hdr->state,
96 		    hdr->numframes, hdr->deschi, hdr->desclo);
97 
98 		buf += sizeof(struct mps_dumpreq_hdr);
99 		len -= sizeof(struct mps_dumpreq_hdr);
100 
101 		if ((offset = find_sgl(buf)) != -1)
102 			print_sgl(buf, offset, numframes);
103 
104 		buf += MPS_FRAME_LEN * numframes;
105 		len -= MPS_FRAME_LEN * numframes;
106 	}
107 
108 	return (error);
109 }
110 
111 static int
112 find_sgl(char *buf)
113 {
114 	MPI2_REQUEST_HEADER *req;
115 	MPI2_SCSI_IO_REQUEST *scsi;
116 	int offset = 0;
117 
118 	req = (MPI2_REQUEST_HEADER *)buf;
119 
120 	switch (req->Function) {
121 	case MPI2_FUNCTION_SCSI_IO_REQUEST:
122 		scsi = (MPI2_SCSI_IO_REQUEST *)buf;
123 		offset = scsi->SGLOffset0;
124 		break;
125 	default:
126 		offset = -1;
127 	}
128 
129 	return (offset);
130 }
131 
132 #define SGL_FLAGS "\10LastElement\7EndOfBuffer\4Local\3Host2IOC\2Addr64\1EndOfList"
133 
134 static void
135 print_sgl(char *buf, int offset, int numframes)
136 {
137 	MPI2_SGE_SIMPLE64 *sge;
138 	MPI2_SGE_CHAIN_UNION *sgc;
139 	MPI2_REQUEST_HEADER *req;
140 	u_int i = 0, flags;
141 	char *frame, tmpbuf[128];
142 
143 	req = (MPI2_REQUEST_HEADER *)buf;
144 	frame = (char *)buf;
145 	sge = (MPI2_SGE_SIMPLE64 *)&frame[offset * 4];
146 	printf("SGL for command\n");
147 
148 	hexdump(frame, MPS_FRAME_LEN, NULL, 0);
149 	while (frame != NULL) {
150 		flags = sge->FlagsLength >> MPI2_SGE_FLAGS_SHIFT;
151 		bzero(tmpbuf, sizeof(tmpbuf));
152 		mps_parse_flags(flags, SGL_FLAGS, tmpbuf, sizeof(tmpbuf));
153 		printf("seg%d flags=%x %s len= 0x%06x addr=0x%016jx\n", i,
154 		    flags, tmpbuf, sge->FlagsLength & 0xffffff,
155 		    mps_to_u64(&sge->Address));
156 		if (flags & (MPI2_SGE_FLAGS_END_OF_LIST |
157 		    MPI2_SGE_FLAGS_END_OF_BUFFER))
158 			break;
159 		sge++;
160 		i++;
161 		if (flags & MPI2_SGE_FLAGS_LAST_ELEMENT) {
162 			sgc = (MPI2_SGE_CHAIN_UNION *)sge;
163 			if ((sgc->Flags & MPI2_SGE_FLAGS_CHAIN_ELEMENT) == 0) {
164 				printf("Invalid chain element\n");
165 				break;
166 			}
167 			bzero(tmpbuf, sizeof(tmpbuf));
168 			mps_parse_flags(sgc->Flags, SGL_FLAGS, tmpbuf,
169 			    sizeof(tmpbuf));
170 			if (sgc->Flags & MPI2_SGE_FLAGS_64_BIT_ADDRESSING)
171 				printf("chain64 flags=0x%x %s len=0x%x "
172 				    "Offset=0x%x addr=0x%016jx\n", sgc->Flags,
173 				    tmpbuf, sgc->Length, sgc->NextChainOffset,
174 				    mps_to_u64(&sgc->u.Address64));
175 			else
176 				printf("chain32 flags=0x%x %s len=0x%x "
177 				    "Offset=0x%x addr=0x%08x\n", sgc->Flags,
178 				    tmpbuf, sgc->Length, sgc->NextChainOffset,
179 				    sgc->u.Address32);
180 			if (--numframes <= 0)
181 				break;
182 			frame += MPS_FRAME_LEN;
183 			sge = (MPI2_SGE_SIMPLE64 *)frame;
184 			hexdump(frame, MPS_FRAME_LEN, NULL, 0);
185 		}
186 	}
187 }
188 
189 MPS_COMMAND(debug, dumpreqs, debug_dumpreqs, "", "Dump the active request queue")
190