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