xref: /freebsd/sys/dev/mfi/mfi_debug.c (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 IronPort Systems
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_mfi.h"
33 
34 #ifdef MFI_DEBUG
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/conf.h>
40 #include <sys/bus.h>
41 #include <sys/bio.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/malloc.h>
45 #include <sys/selinfo.h>
46 #include <sys/sysctl.h>
47 #include <sys/taskqueue.h>
48 #include <sys/uio.h>
49 #include <machine/resource.h>
50 #include <machine/bus.h>
51 
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 
55 #include <dev/mfi/mfireg.h>
56 #include <dev/mfi/mfi_ioctl.h>
57 #include <dev/mfi/mfivar.h>
58 
59 static void
60 mfi_print_frame_flags(device_t dev, uint32_t flags)
61 {
62 	device_printf(dev, "flags=%b\n", flags, MFI_FRAME_FMT);
63 }
64 
65 static void
66 mfi_print_sgl(struct mfi_frame_header *hdr, union mfi_sgl *sgl, int count)
67 {
68 	int i, columns = 0;
69 
70 	printf("SG List:\n");
71 	for (i = 0; i < count; i++) {
72 		if (hdr->flags & MFI_FRAME_IEEE_SGL) {
73 			printf("0x%lx:%06d ", (u_long)sgl->sg_skinny[i].addr,
74 			    sgl->sg_skinny[i].len);
75 			columns += 26;
76 			if (columns > 77) {
77 				printf("\n");
78 				columns = 0;
79 			}
80 		} else if (hdr->flags & MFI_FRAME_SGL64) {
81 			printf("0x%lx:%06d ", (u_long)sgl->sg64[i].addr,
82 			    sgl->sg64[i].len);
83 			columns += 26;
84 			if (columns > 77) {
85 				printf("\n");
86 				columns = 0;
87 			}
88 		} else {
89 			printf("0x%x:%06d ", sgl->sg32[i].addr,
90 			    sgl->sg32[i].len);
91 			columns += 18;
92 			if (columns > 71) {
93 				printf("\n");
94 				columns = 0;
95 			}
96 		}
97 	}
98 	if (columns != 0)
99 		printf("\n");
100 
101 }
102 
103 static void
104 mfi_print_ldio(struct mfi_softc *sc, device_t dev, struct mfi_command *cm)
105 {
106 	struct mfi_io_frame *io;
107 	struct mfi_frame_header *hdr;
108 
109 	io = &cm->cm_frame->io;
110 	hdr = &io->header;
111 
112 	device_printf(dev, "cmd=%s target_id=%d sg_count=%d data_len=%d "
113 	    "lba=%d\n", (hdr->cmd == MFI_CMD_LD_READ) ? "LD_READ":"LD_WRITE",
114 	     hdr->target_id, hdr->sg_count, hdr->data_len, io->lba_lo);
115 	mfi_print_frame_flags(dev, hdr->flags);
116 	mfi_print_sgl(hdr, &io->sgl, hdr->sg_count);
117 
118 }
119 
120 static void
121 mfi_print_dcmd(struct mfi_softc *sc, device_t dev, struct mfi_command *cm)
122 {
123 	struct mfi_dcmd_frame *dcmd;
124 	struct mfi_frame_header *hdr;
125 	const char *opcode;
126 
127 	dcmd = &cm->cm_frame->dcmd;
128 	hdr = &dcmd->header;
129 
130 	switch (dcmd->opcode) {
131 	case MFI_DCMD_CTRL_GETINFO:
132 		opcode = "CTRL_GETINFO";
133 		break;
134 	case MFI_DCMD_CTRL_FLUSHCACHE:
135 		opcode = "CTRL_FLUSHCACHE";
136 		break;
137 	case MFI_DCMD_CTRL_SHUTDOWN:
138 		opcode = "CTRL_SHUTDOWN";
139 		break;
140 	case MFI_DCMD_CTRL_EVENT_GETINFO:
141 		opcode = "EVENT_GETINFO";
142 		break;
143 	case MFI_DCMD_CTRL_EVENT_GET:
144 		opcode = "EVENT_GET";
145 		break;
146 	case MFI_DCMD_CTRL_EVENT_WAIT:
147 		opcode = "EVENT_WAIT";
148 		break;
149 	case MFI_DCMD_LD_GET_LIST:
150 		opcode = "LD_GET_LIST";
151 		break;
152 	case MFI_DCMD_LD_GET_INFO:
153 		opcode = "LD_GET_INFO";
154 		break;
155 	case MFI_DCMD_LD_GET_PROP:
156 		opcode = "LD_GET_PROP";
157 		break;
158 	case MFI_DCMD_LD_SET_PROP:
159 		opcode = "LD_SET_PROP";
160 		break;
161 	case MFI_DCMD_CLUSTER:
162 		opcode = "CLUSTER";
163 		break;
164 	case MFI_DCMD_CLUSTER_RESET_ALL:
165 		opcode = "CLUSTER_RESET_ALL";
166 		break;
167 	case MFI_DCMD_CLUSTER_RESET_LD:
168 		opcode = "CLUSTER_RESET_LD";
169 		break;
170 	case MFI_DCMD_LD_MAP_GET_INFO:
171 		opcode = "LD_MAP_GET_INFO";
172 		break;
173 	case MFI_DCMD_BBU_START_LEARN:
174 		opcode = "BBU_START_LEARN";
175 		break;
176 	case MFI_DCMD_BBU_GET_PROP:
177 		opcode = "BBU_GET_PROP";
178 		break;
179 	case MFI_DCMD_BBU_SET_PROP:
180 		opcode = "BBU_SET_PROP";
181 		break;
182 	default:
183 		opcode = "UNKNOWN";
184 		break;
185 	}
186 
187 	device_printf(dev, "cmd=MFI_CMD_DCMD opcode=%s data_len=%d\n",
188 	    opcode, hdr->data_len);
189 	mfi_print_frame_flags(dev, hdr->flags);
190 	mfi_print_sgl(hdr, &dcmd->sgl, hdr->sg_count);
191 
192 }
193 
194 static void
195 mfi_print_generic_frame(struct mfi_softc *sc, struct mfi_command *cm)
196 {
197 	hexdump(cm->cm_frame, cm->cm_total_frame_size, NULL, HD_OMIT_CHARS);
198 }
199 
200 void
201 mfi_print_cmd(struct mfi_command *cm)
202 {
203 	device_t dev;
204 	struct mfi_softc *sc;
205 
206 	sc = cm->cm_sc;
207 	dev = sc->mfi_dev;
208 
209 	device_printf(dev, "cm=%p index=%d total_frame_size=%d "
210 	    "extra_frames=%d\n", cm, cm->cm_index, cm->cm_total_frame_size,
211 	    cm->cm_extra_frames);
212 	device_printf(dev, "flags=%b\n", cm->cm_flags, MFI_CMD_FLAGS_FMT);
213 
214 	switch (cm->cm_frame->header.cmd) {
215 	case MFI_CMD_DCMD:
216 		mfi_print_dcmd(sc, dev, cm);
217 		break;
218 	case MFI_CMD_LD_READ:
219 	case MFI_CMD_LD_WRITE:
220 		mfi_print_ldio(sc, dev, cm);
221 		break;
222 	default:
223 		mfi_print_generic_frame(sc, cm);
224 		break;
225 	}
226 
227 	return;
228 }
229 
230 void
231 mfi_dump_cmds(struct mfi_softc *sc)
232 {
233 	int i;
234 
235 	for (i = 0; i < sc->mfi_max_fw_cmds; i++)
236 		mfi_print_generic_frame(sc, &sc->mfi_commands[i]);
237 }
238 
239 void
240 mfi_validate_sg(struct mfi_softc *sc, struct mfi_command *cm,
241 	const char *function, int line)
242 {
243 	struct mfi_frame_header *hdr;
244 	int i;
245 	uint32_t count = 0, data_len;
246 
247 	hdr = &cm->cm_frame->header;
248 	count = 0;
249 	for (i = 0; i < hdr->sg_count; i++) {
250 		if (hdr->flags & MFI_FRAME_IEEE_SGL)
251 			count += cm->cm_sg->sg_skinny[i].len;
252 		else if (hdr->flags & MFI_FRAME_SGL64)
253 			count += cm->cm_sg->sg64[i].len;
254 		else
255 			count += cm->cm_sg->sg32[i].len;
256 	}
257 	/*
258 	count++;
259 	*/
260 	data_len = hdr->data_len;
261 	switch (hdr->cmd) {
262 	case MFI_CMD_LD_READ:
263 	case MFI_CMD_LD_WRITE:
264 		data_len = data_len * 512;
265 	case MFI_CMD_DCMD:
266 		if (count != data_len) {
267 			device_printf(sc->mfi_dev,
268 			    "%s %d COMMAND %p S/G count bad %d %d %d 0x%jx\n",
269 			    function, line, cm, count, data_len, cm->cm_len,
270 			    (intmax_t)pmap_kextract((vm_offset_t)cm->cm_data));
271 			MFI_PRINT_CMD(cm);
272 		}
273 	}
274 }
275 
276 #endif
277