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