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