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