MPT - fix all compiler warnings
[dragonfly.git] / sys / dev / disk / mpt / mpt_debug.c
1 /*-
2  * Debug routines for LSI '909 FC  adapters.
3  * FreeBSD Version.
4  *
5  * Copyright (c)  2000, 2001 by Greg Ansley
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 immediately at the beginning of the file, without modification,
12  *    this list of conditions, and the following disclaimer.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
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 FOR
20  * 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 /*-
30  * Copyright (c) 2002, 2006 by Matthew Jacob
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions are
35  * met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
39  *    substantially similar to the "NO WARRANTY" disclaimer below
40  *    ("Disclaimer") and any redistribution must be conditioned upon including
41  *    a substantially similar Disclaimer requirement for further binary
42  *    redistribution.
43  * 3. Neither the names of the above listed copyright holders nor the names
44  *    of any contributors may be used to endorse or promote products derived
45  *    from this software without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
57  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58  *
59  * Support from Chris Ellsworth in order to make SAS adapters work
60  * is gratefully acknowledged.
61  *
62  * Support from LSI-Logic has also gone a great deal toward making this a
63  * workable subsystem and is gratefully acknowledged.
64  * $FreeBSD: src/sys/dev/mpt/mpt_debug.c,v 1.18 2006/12/07 22:02:28 mjacob Exp $
65  */
66
67 #include <sys/cdefs.h>
68
69 #include <dev/disk/mpt/mpt.h>
70
71 #include <dev/disk/mpt/mpilib/mpi_ioc.h>
72 #include <dev/disk/mpt/mpilib/mpi_init.h>
73 #include <dev/disk/mpt/mpilib/mpi_fc.h>
74 #include <dev/disk/mpt/mpilib/mpi_targ.h>
75
76 #include <bus/cam/scsi/scsi_all.h>
77
78 #include <machine/stdarg.h>     /* for use by mpt_prt below */
79
80 struct Error_Map {
81         int      Error_Code;
82         char    *Error_String;
83 };
84
85 static const struct Error_Map IOC_Status[] = {
86 { MPI_IOCSTATUS_SUCCESS,                  "Success" },
87 { MPI_IOCSTATUS_INVALID_FUNCTION,         "IOC: Invalid Function" },
88 { MPI_IOCSTATUS_BUSY,                     "IOC: Busy" },
89 { MPI_IOCSTATUS_INVALID_SGL,              "IOC: Invalid SGL" },
90 { MPI_IOCSTATUS_INTERNAL_ERROR,           "IOC: Internal Error" },
91 { MPI_IOCSTATUS_RESERVED,                 "IOC: Reserved" },
92 { MPI_IOCSTATUS_INSUFFICIENT_RESOURCES,   "IOC: Insufficient Resources" },
93 { MPI_IOCSTATUS_INVALID_FIELD,            "IOC: Invalid Field" },
94 { MPI_IOCSTATUS_INVALID_STATE,            "IOC: Invalid State" },
95 { MPI_IOCSTATUS_CONFIG_INVALID_ACTION,    "Invalid Action" },
96 { MPI_IOCSTATUS_CONFIG_INVALID_TYPE,      "Invalid Type" },
97 { MPI_IOCSTATUS_CONFIG_INVALID_PAGE,      "Invalid Page" },
98 { MPI_IOCSTATUS_CONFIG_INVALID_DATA,      "Invalid Data" },
99 { MPI_IOCSTATUS_CONFIG_NO_DEFAULTS,       "No Defaults" },
100 { MPI_IOCSTATUS_CONFIG_CANT_COMMIT,       "Can't Commit" },
101 { MPI_IOCSTATUS_SCSI_RECOVERED_ERROR,     "SCSI: Recoverd Error" },
102 { MPI_IOCSTATUS_SCSI_INVALID_BUS,         "SCSI: Invalid Bus" },
103 { MPI_IOCSTATUS_SCSI_INVALID_TARGETID,    "SCSI: Invalid Target ID" },
104 { MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE,    "SCSI: Device Not There" },
105 { MPI_IOCSTATUS_SCSI_DATA_OVERRUN,        "SCSI: Data Overrun" },
106 { MPI_IOCSTATUS_SCSI_DATA_UNDERRUN,       "SCSI: Data Underrun" },
107 { MPI_IOCSTATUS_SCSI_IO_DATA_ERROR,       "SCSI: Data Error" },
108 { MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR,      "SCSI: Protocol Error" },
109 { MPI_IOCSTATUS_SCSI_TASK_TERMINATED,     "SCSI: Task Terminated" },
110 { MPI_IOCSTATUS_SCSI_RESIDUAL_MISMATCH,   "SCSI: Residual Mismatch" },
111 { MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED,    "SCSI: Task Management Failed" },
112 { MPI_IOCSTATUS_SCSI_IOC_TERMINATED,      "SCSI: IOC Bus Reset" },
113 { MPI_IOCSTATUS_SCSI_EXT_TERMINATED,      "SCSI: External Bus Reset" },
114 { MPI_IOCSTATUS_TARGET_PRIORITY_IO,       "SCSI Target: Priority I/O" },
115 { MPI_IOCSTATUS_TARGET_INVALID_PORT,      "SCSI Target: Invalid Port" },
116 { MPI_IOCSTATUS_TARGET_INVALID_IOCINDEX,  "SCSI Target: Invalid IOC Index" },
117 { MPI_IOCSTATUS_TARGET_ABORTED,           "SCSI Target: Aborted" },
118 { MPI_IOCSTATUS_TARGET_NO_CONN_RETRYABLE, "SCSI Target: No Connection (Retryable)" },
119 { MPI_IOCSTATUS_TARGET_NO_CONNECTION,     "SCSI Target: No Connection" },
120 { MPI_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH,"SCSI Target: Transfer Count Mismatch" },
121 { MPI_IOCSTATUS_TARGET_FC_ABORTED,        "FC: Aborted" },
122 { MPI_IOCSTATUS_TARGET_FC_RX_ID_INVALID,  "FC: Recieve ID Invalid" },
123 { MPI_IOCSTATUS_TARGET_FC_DID_INVALID,    "FC: Recieve DID Invalid" },
124 { MPI_IOCSTATUS_TARGET_FC_NODE_LOGGED_OUT,"FC: Node Logged Out" },
125 { MPI_IOCSTATUS_LAN_DEVICE_NOT_FOUND,     "LAN: Device Not Found" },
126 { MPI_IOCSTATUS_LAN_DEVICE_FAILURE,       "LAN: Device Not Failure" },
127 { MPI_IOCSTATUS_LAN_TRANSMIT_ERROR,       "LAN: Transmit Error" },
128 { MPI_IOCSTATUS_LAN_TRANSMIT_ABORTED,     "LAN: Transmit Aborted" },
129 { MPI_IOCSTATUS_LAN_RECEIVE_ERROR,        "LAN: Recieve Error" },
130 { MPI_IOCSTATUS_LAN_RECEIVE_ABORTED,      "LAN: Recieve Aborted" },
131 { MPI_IOCSTATUS_LAN_PARTIAL_PACKET,       "LAN: Partial Packet" },
132 { MPI_IOCSTATUS_LAN_CANCELED,             "LAN: Canceled" },
133 { -1, 0},
134 };
135
136 static const struct Error_Map IOC_Func[] = {
137 { MPI_FUNCTION_SCSI_IO_REQUEST,              "SCSI IO Request" },
138 { MPI_FUNCTION_SCSI_TASK_MGMT,               "SCSI Task Management" },
139 { MPI_FUNCTION_IOC_INIT,                     "IOC Init" },
140 { MPI_FUNCTION_IOC_FACTS,                    "IOC Facts" },
141 { MPI_FUNCTION_CONFIG,                       "Config" },
142 { MPI_FUNCTION_PORT_FACTS,                   "Port Facts" },
143 { MPI_FUNCTION_PORT_ENABLE,                  "Port Enable" },
144 { MPI_FUNCTION_EVENT_NOTIFICATION,           "Event Notification" },
145 { MPI_FUNCTION_EVENT_ACK,                    "Event Ack" },
146 { MPI_FUNCTION_FW_DOWNLOAD,                  "FW Download" },
147 { MPI_FUNCTION_TARGET_CMD_BUFFER_POST,       "SCSI Target Command Buffer" },
148 { MPI_FUNCTION_TARGET_ASSIST,                "Target Assist" },
149 { MPI_FUNCTION_TARGET_STATUS_SEND,           "Target Status Send" },
150 { MPI_FUNCTION_TARGET_MODE_ABORT,            "Target Mode Abort" },
151 { -1, 0},
152 };
153
154 static const struct Error_Map IOC_Event[] = {
155 { MPI_EVENT_NONE,                       "None" },
156 { MPI_EVENT_LOG_DATA,                   "LogData" },
157 { MPI_EVENT_STATE_CHANGE,               "State Change" },
158 { MPI_EVENT_UNIT_ATTENTION,             "Unit Attention" },
159 { MPI_EVENT_IOC_BUS_RESET,              "IOC Bus Reset" },
160 { MPI_EVENT_EXT_BUS_RESET,              "External Bus Reset" },
161 { MPI_EVENT_RESCAN,                     "Rescan" },
162 { MPI_EVENT_LINK_STATUS_CHANGE,         "Link Status Change" },
163 { MPI_EVENT_LOOP_STATE_CHANGE,          "Loop State Change" },
164 { MPI_EVENT_LOGOUT,                     "Logout" },
165 { MPI_EVENT_EVENT_CHANGE,               "EventChange" },
166 { -1, 0},
167 };
168
169 static const struct Error_Map IOC_SCSIState[] = {
170 { MPI_SCSI_STATE_AUTOSENSE_VALID,       "AutoSense_Valid" },
171 { MPI_SCSI_STATE_AUTOSENSE_FAILED,      "AutoSense_Failed" },
172 { MPI_SCSI_STATE_NO_SCSI_STATUS,        "No_SCSI_Status" },
173 { MPI_SCSI_STATE_TERMINATED,            "State_Terminated" },
174 { MPI_SCSI_STATE_RESPONSE_INFO_VALID,   "Repsonse_Info_Valid" },
175 { MPI_SCSI_STATE_QUEUE_TAG_REJECTED,    "Queue Tag Rejected" },
176 { -1, 0},
177 };
178
179 static const struct Error_Map IOC_SCSIStatus[] = {
180 { SCSI_STATUS_OK,                       "OK" },
181 { SCSI_STATUS_CHECK_COND,               "Check Condition" },
182 { SCSI_STATUS_COND_MET,                 "Check Condition Met" },
183 { SCSI_STATUS_BUSY,                     "Busy" },
184 { SCSI_STATUS_INTERMED,                 "Intermidiate Condition" },
185 { SCSI_STATUS_INTERMED_COND_MET,        "Intermidiate Condition Met" },
186 { SCSI_STATUS_RESERV_CONFLICT,          "Reservation Conflict" },
187 { SCSI_STATUS_CMD_TERMINATED,           "Command Terminated" },
188 { SCSI_STATUS_QUEUE_FULL,               "Queue Full" },
189 { -1, 0},
190 };
191
192 static const struct Error_Map IOC_Diag[] = {
193 { MPI_DIAG_DRWE,                "DWE" },
194 { MPI_DIAG_FLASH_BAD_SIG,       "FLASH_Bad" },
195 { MPI_DIAGNOSTIC_OFFSET,        "Offset" },
196 { MPI_DIAG_RESET_ADAPTER,       "Reset" },
197 { MPI_DIAG_DISABLE_ARM,         "DisARM" },
198 { MPI_DIAG_MEM_ENABLE,          "DME" },
199 { -1, 0 },
200 };
201
202 static const struct Error_Map IOC_SCSITMType[] = {
203 { MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK,         "Abort Task" },
204 { MPI_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET,      "Abort Task Set" },
205 { MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,       "Target Reset" },
206 { MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,          "Reset Bus" },
207 { MPI_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, "Logical Unit Reset" },
208 { -1, 0 },
209 };
210
211 static char *
212 mpt_ioc_status(int code)
213 {
214         const struct Error_Map *status = IOC_Status;
215         static char buf[64];
216         while (status->Error_Code >= 0) {
217                 if (status->Error_Code == (code & MPI_IOCSTATUS_MASK))
218                         return status->Error_String;
219                 status++;
220         }
221         ksnprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
222         return buf;
223 }
224
225 char *
226 mpt_ioc_diag(u_int32_t code)
227 {
228         const struct Error_Map *status = IOC_Diag;
229         static char buf[128];
230         char *ptr = buf;
231         char *end = &buf[128];
232         buf[0] = '\0';
233         ptr += ksnprintf(buf, sizeof buf, "(0x%08x)", code);
234         while (status->Error_Code >= 0) {
235                 if ((status->Error_Code & code) != 0)
236                         ptr += ksnprintf(ptr, (size_t)(end-ptr), "%s ",
237                                 status->Error_String);
238                 status++;
239         }
240         return buf;
241 }
242
243 static char *
244 mpt_ioc_function(int code)
245 {
246         const struct Error_Map *status = IOC_Func;
247         static char buf[64];
248         while (status->Error_Code >= 0) {
249                 if (status->Error_Code == code)
250                         return status->Error_String;
251                 status++;
252         }
253         ksnprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
254         return buf;
255 }
256
257 static char *
258 mpt_ioc_event(int code)
259 {
260         const struct Error_Map *status = IOC_Event;
261         static char buf[64];
262         while (status->Error_Code >= 0) {
263                 if (status->Error_Code == code)
264                         return status->Error_String;
265                 status++;
266         }
267         ksnprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
268         return buf;
269 }
270
271 static char *
272 mpt_scsi_state(int code)
273 {
274         const struct Error_Map *status = IOC_SCSIState;
275         static char buf[128];
276         char *ptr = buf;
277         char *end = &buf[128];
278         buf[0] = '\0';
279         ptr += ksnprintf(buf, sizeof buf, "(0x%08x)", code);
280         while (status->Error_Code >= 0) {
281                 if ((status->Error_Code & code) != 0)
282                         ptr += ksnprintf(ptr, (size_t)(end-ptr), "%s ",
283                                 status->Error_String);
284                 status++;
285         }
286         return buf;
287 }
288 static char *
289 mpt_scsi_status(int code)
290 {
291         const struct Error_Map *status = IOC_SCSIStatus;
292         static char buf[64];
293         while (status->Error_Code >= 0) {
294                 if (status->Error_Code == code)
295                         return status->Error_String;
296                 status++;
297         }
298         ksnprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
299         return buf;
300 }
301 static char *
302 mpt_who(int who_init)
303 {
304         char *who;
305
306         switch (who_init) {
307         case MPT_DB_INIT_NOONE:       who = "No One";        break;
308         case MPT_DB_INIT_BIOS:        who = "BIOS";          break;
309         case MPT_DB_INIT_ROMBIOS:     who = "ROM BIOS";      break;
310         case MPT_DB_INIT_PCIPEER:     who = "PCI Peer";      break;
311         case MPT_DB_INIT_HOST:        who = "Host Driver";   break;
312         case MPT_DB_INIT_MANUFACTURE: who = "Manufacturing"; break;
313         default:                      who = "Unknown";       break;
314         }
315         return who;
316 }
317
318 static char *
319 mpt_state(u_int32_t mb)
320 {
321         char *text;
322
323         switch (MPT_STATE(mb)) {
324                 case MPT_DB_STATE_RESET:  text = "Reset";   break;
325                 case MPT_DB_STATE_READY:  text = "Ready";   break;
326                 case MPT_DB_STATE_RUNNING:text = "Running"; break;
327                 case MPT_DB_STATE_FAULT:  text = "Fault";   break;
328                 default:                  text = "Unknown"; break;
329         }
330         return text;
331 }
332
333 static char *
334 mpt_scsi_tm_type(int code)
335 {
336         const struct Error_Map *status = IOC_SCSITMType;
337         static char buf[64];
338         while (status->Error_Code >= 0) {
339                 if (status->Error_Code == code)
340                         return status->Error_String;
341                 status++;
342         }
343         ksnprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
344         return buf;
345 }
346
347 void
348 mpt_print_db(u_int32_t mb)
349 {
350         kprintf("mpt mailbox: (0x%x) State %s  WhoInit %s\n",
351             mb, mpt_state(mb), mpt_who(MPT_WHO(mb)));
352 }
353
354 /*****************************************************************************/
355 /*  Reply functions                                                          */
356 /*****************************************************************************/
357 static void
358 mpt_print_reply_hdr(MSG_DEFAULT_REPLY *msg)
359 {
360         kprintf("%s Reply @ %p\n", mpt_ioc_function(msg->Function), msg);
361         kprintf("\tIOC Status    %s\n", mpt_ioc_status(msg->IOCStatus));
362         kprintf("\tIOCLogInfo    0x%08x\n", (unsigned)msg->IOCLogInfo);
363         kprintf("\tMsgLength     0x%02x\n", msg->MsgLength);
364         kprintf("\tMsgFlags      0x%02x\n", msg->MsgFlags);
365         kprintf("\tMsgContext    0x%08x\n", (unsigned)msg->MsgContext);
366 }
367
368 static void
369 mpt_print_init_reply(MSG_IOC_INIT_REPLY *msg)
370 {
371         mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
372         kprintf("\tWhoInit       %s\n", mpt_who(msg->WhoInit));
373         kprintf("\tMaxDevices    0x%02x\n", msg->MaxDevices);
374         kprintf("\tMaxBuses     0x%02x\n", msg->MaxBuses);
375 }
376
377 static void
378 mpt_print_ioc_facts(MSG_IOC_FACTS_REPLY *msg)
379 {
380         mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
381         kprintf("\tIOCNumber     %d\n",         msg->IOCNumber);
382         kprintf("\tMaxChainDepth %d\n",         msg->MaxChainDepth);
383         kprintf("\tWhoInit       %s\n",         mpt_who(msg->WhoInit));
384         kprintf("\tBlockSize     %d\n",         msg->BlockSize);
385         kprintf("\tFlags         %d\n",         msg->Flags);
386         kprintf("\tReplyQueueDepth %d\n",       msg->ReplyQueueDepth);
387         kprintf("\tReqFrameSize  0x%04x\n",     msg->RequestFrameSize);
388         kprintf("\tFW Version    0x%08x\n",     (unsigned)msg->FWVersion.Word);
389         kprintf("\tProduct ID    0x%04x\n",     msg->ProductID);
390         kprintf("\tCredits       0x%04x\n",     msg->GlobalCredits);
391         kprintf("\tPorts         %d\n",         msg->NumberOfPorts);
392         kprintf("\tEventState    0x%02x\n",     msg->EventState);
393         kprintf("\tHostMFA_HA    0x%08x\n",
394                 (unsigned)msg->CurrentHostMfaHighAddr);
395         kprintf("\tSenseBuf_HA   0x%08x\n",
396                 (unsigned)msg->CurrentSenseBufferHighAddr);
397         kprintf("\tRepFrameSize  0x%04x\n",     msg->CurReplyFrameSize);
398         kprintf("\tMaxDevices    0x%02x\n",     msg->MaxDevices);
399         kprintf("\tMaxBuses      0x%02x\n",     msg->MaxBuses);
400         kprintf("\tFWImageSize   0x%04x\n",     (unsigned)msg->FWImageSize);
401 }
402
403 static void
404 mpt_print_enable_reply(MSG_PORT_ENABLE_REPLY *msg)
405 {
406         mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
407         kprintf("\tPort:         %d\n", msg->PortNumber);
408 }
409
410 static void
411 mpt_print_scsi_io_reply(MSG_SCSI_IO_REPLY *msg)
412 {
413         mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
414         kprintf("\tBus:          %d\n", msg->Bus);
415         kprintf("\tTargetID      %d\n", msg->TargetID);
416         kprintf("\tCDBLength     %d\n", msg->CDBLength);
417         kprintf("\tSCSI Status:  %s\n", mpt_scsi_status(msg->SCSIStatus));
418         kprintf("\tSCSI State:   %s\n", mpt_scsi_state(msg->SCSIState));
419         kprintf("\tTransferCnt   0x%04x\n", (unsigned)msg->TransferCount);
420         kprintf("\tSenseCnt      0x%04x\n", (unsigned)msg->SenseCount);
421         kprintf("\tResponseInfo  0x%08x\n", (unsigned)msg->ResponseInfo);
422 }
423
424
425
426 static void
427 mpt_print_event_notice(MSG_EVENT_NOTIFY_REPLY *msg)
428 {
429         mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
430         kprintf("\tEvent:        %s\n", mpt_ioc_event(msg->Event));
431         kprintf("\tEventContext  0x%04x\n", (unsigned)msg->EventContext);
432         kprintf("\tAckRequired     %d\n", msg->AckRequired);
433         kprintf("\tEventDataLength %d\n", msg->EventDataLength);
434         kprintf("\tContinuation    %d\n", msg->MsgFlags & 0x80);
435         switch(msg->Event) {
436         case MPI_EVENT_LOG_DATA:
437                 kprintf("\tEvtLogData:   0x%04x\n",
438                         (unsigned)msg->Data[0]);
439                 break;
440
441         case MPI_EVENT_UNIT_ATTENTION:
442                 kprintf("\tTargetID:     0x%04x\n",
443                         (unsigned)msg->Data[0] & 0xff);
444                 kprintf("\tBus:          0x%04x\n",
445                         (unsigned)(msg->Data[0] >> 8) & 0xff);
446                 break;
447
448         case MPI_EVENT_IOC_BUS_RESET:
449         case MPI_EVENT_EXT_BUS_RESET:
450         case MPI_EVENT_RESCAN:
451                 kprintf("\tPort:           %lu\n",
452                         (msg->Data[0] >> 8) & 0xff);
453                 break;
454
455         case MPI_EVENT_LINK_STATUS_CHANGE:
456                 kprintf("\tLinkState:    %lu\n",
457                         msg->Data[0] & 0xff);
458                 kprintf("\tPort:         %lu\n",
459                         (msg->Data[1] >> 8) & 0xff);
460                 break;
461
462         case MPI_EVENT_LOOP_STATE_CHANGE:
463                 kprintf("\tType:         %lu\n",
464                         (msg->Data[0] >> 16) & 0xff);
465                 kprintf("\tChar3:      0x%02lx\n",
466                         (msg->Data[0] >> 8) & 0xff);
467                 kprintf("\tChar4:      0x%02lx\n",
468                         (msg->Data[0]     ) & 0xff);
469                 kprintf("\tPort:         %lu\n",
470                         (msg->Data[1] >> 8) & 0xff);
471                 break;
472
473         case MPI_EVENT_LOGOUT:
474                 kprintf("\tN_PortId:   0x%04lx\n", msg->Data[0]);
475                 kprintf("\tPort:         %lu\n",
476                         (msg->Data[1] >> 8) & 0xff);
477                 break;
478         }
479
480 }
481
482 void
483 mpt_print_reply(void *vmsg)
484 {
485         MSG_DEFAULT_REPLY *msg = vmsg;
486
487         switch (msg->Function) {
488         case MPI_FUNCTION_EVENT_NOTIFICATION:
489                 mpt_print_event_notice((MSG_EVENT_NOTIFY_REPLY *)msg);
490                 break;
491         case MPI_FUNCTION_PORT_ENABLE:
492                 mpt_print_enable_reply((MSG_PORT_ENABLE_REPLY *)msg);
493                 break;
494         case MPI_FUNCTION_IOC_FACTS:
495                 mpt_print_ioc_facts((MSG_IOC_FACTS_REPLY *)msg);
496                 break;
497         case MPI_FUNCTION_IOC_INIT:
498                 mpt_print_init_reply((MSG_IOC_INIT_REPLY *)msg);
499                 break;
500         case MPI_FUNCTION_SCSI_IO_REQUEST:
501         case MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
502                 mpt_print_scsi_io_reply((MSG_SCSI_IO_REPLY *)msg);
503                 break;
504         default:
505                 mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
506                 break;
507         }
508 }
509
510 /*****************************************************************************/
511 /*  Request functions                                                        */
512 /*****************************************************************************/
513 static void
514 mpt_print_request_hdr(MSG_REQUEST_HEADER *req)
515 {
516         kprintf("%s @ %p\n", mpt_ioc_function(req->Function), req);
517         kprintf("\tChain Offset  0x%02x\n", req->ChainOffset);
518         kprintf("\tMsgFlags      0x%02x\n", (unsigned)req->MsgFlags);
519         kprintf("\tMsgContext    0x%08x\n", (unsigned)req->MsgContext);
520 }
521
522 void
523 mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *orig_msg)
524 {
525         MSG_SCSI_IO_REQUEST local, *msg = &local;
526         int i;
527
528         bcopy(orig_msg, msg, sizeof (MSG_SCSI_IO_REQUEST));
529         mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg);
530         kprintf("\tBus:                %d\n", msg->Bus);
531         kprintf("\tTargetID            %d\n", msg->TargetID);
532         kprintf("\tSenseBufferLength   %d\n", msg->SenseBufferLength);
533         kprintf("\tLUN:              0x%0x\n", msg->LUN[1]);
534         kprintf("\tControl           0x%08x ", (unsigned)msg->Control);
535 #define MPI_PRINT_FIELD(x)                                              \
536         case MPI_SCSIIO_CONTROL_ ## x :                                 \
537                 kprintf(" " #x " ");                                    \
538                 break
539
540         switch (msg->Control & MPI_SCSIIO_CONTROL_DATADIRECTION_MASK) {
541         MPI_PRINT_FIELD(NODATATRANSFER);
542         MPI_PRINT_FIELD(WRITE);
543         MPI_PRINT_FIELD(READ);
544         default:
545                 kprintf(" Invalid DIR! ");
546                 break;
547         }
548         switch (msg->Control & MPI_SCSIIO_CONTROL_TASKATTRIBUTE_MASK) {
549         MPI_PRINT_FIELD(SIMPLEQ);
550         MPI_PRINT_FIELD(HEADOFQ);
551         MPI_PRINT_FIELD(ORDEREDQ);
552         MPI_PRINT_FIELD(ACAQ);
553         MPI_PRINT_FIELD(UNTAGGED);
554         MPI_PRINT_FIELD(NO_DISCONNECT);
555         default:
556                 kprintf(" Unknown attribute! ");
557                 break;
558         }
559
560         kprintf("\n");
561 #undef MPI_PRINT_FIELD
562
563         kprintf("\tDataLength\t0x%08x\n", (unsigned)msg->DataLength);
564         kprintf("\tSenseBufAddr\t0x%08x\n", (unsigned)msg->SenseBufferLowAddr);
565         kprintf("\tCDB[0:%d]\t", msg->CDBLength);
566         for (i = 0; i < msg->CDBLength; i++)
567                 kprintf("%02x ", msg->CDB[i]);
568         kprintf("\n");
569
570         if ((msg->Control & MPI_SCSIIO_CONTROL_DATADIRECTION_MASK) !=
571            MPI_SCSIIO_CONTROL_NODATATRANSFER ) {
572                 mpt_dump_sgl(&orig_msg->SGL,
573                     ((char *)&orig_msg->SGL)-(char *)orig_msg);
574         }
575 }
576
577 static void
578 mpt_print_scsi_tmf_request(MSG_SCSI_TASK_MGMT *msg)
579 {
580         mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg);
581         kprintf("\tLun             0x%02x\n", msg->LUN[1]);
582         kprintf("\tTaskType        %s\n", mpt_scsi_tm_type(msg->TaskType));
583         kprintf("\tTaskMsgContext  0x%08x\n", (unsigned)msg->TaskMsgContext);
584 }
585
586
587 static void
588 mpt_print_scsi_target_assist_request(PTR_MSG_TARGET_ASSIST_REQUEST msg)
589 {
590         mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg);
591         kprintf("\tStatusCode    0x%02x\n", msg->StatusCode);
592         kprintf("\tTargetAssist  0x%02x\n", msg->TargetAssistFlags);
593         kprintf("\tQueueTag      0x%04x\n", msg->QueueTag);
594         kprintf("\tReplyWord     0x%08x\n", (unsigned)msg->ReplyWord);
595         kprintf("\tLun           0x%02x\n", msg->LUN[1]);
596         kprintf("\tRelativeOff   0x%08x\n", (unsigned)msg->RelativeOffset);
597         kprintf("\tDataLength    0x%08x\n", (unsigned)msg->DataLength);
598         mpt_dump_sgl(msg->SGL, 0);
599 }
600
601 static void
602 mpt_print_scsi_target_status_send_request(MSG_TARGET_STATUS_SEND_REQUEST *msg)
603 {
604         SGE_IO_UNION x;
605         mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg);
606         kprintf("\tStatusCode    0x%02x\n", msg->StatusCode);
607         kprintf("\tStatusFlags   0x%02x\n", msg->StatusFlags);
608         kprintf("\tQueueTag      0x%04x\n", msg->QueueTag);
609         kprintf("\tReplyWord     0x%08x\n", (unsigned)msg->ReplyWord);
610         kprintf("\tLun           0x%02x\n", msg->LUN[1]);
611         x.u.Simple = msg->StatusDataSGE;
612         mpt_dump_sgl(&x, 0);
613 }
614
615 void
616 mpt_print_request(void *vreq)
617 {
618         MSG_REQUEST_HEADER *req = vreq;
619
620         switch (req->Function) {
621         case MPI_FUNCTION_SCSI_IO_REQUEST:
622         case MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
623                 mpt_print_scsi_io_request((MSG_SCSI_IO_REQUEST *)req);
624                 break;
625         case MPI_FUNCTION_SCSI_TASK_MGMT:
626                 mpt_print_scsi_tmf_request((MSG_SCSI_TASK_MGMT *)req);
627                 break;
628         case MPI_FUNCTION_TARGET_ASSIST:
629                 mpt_print_scsi_target_assist_request(
630                     (PTR_MSG_TARGET_ASSIST_REQUEST)req);
631                 break;
632         case MPI_FUNCTION_TARGET_STATUS_SEND:
633                 mpt_print_scsi_target_status_send_request(
634                     (MSG_TARGET_STATUS_SEND_REQUEST *)req);
635                 break;
636         default:
637                 mpt_print_request_hdr(req);
638                 break;
639         }
640 }
641
642 int
643 mpt_decode_value(mpt_decode_entry_t *table, u_int num_entries,
644                  const char *name, u_int value, u_int *cur_column,
645                  u_int wrap_point)
646 {
647         int     printed;
648         u_int   printed_mask;
649         u_int   dummy_column;
650
651         if (cur_column == NULL) {
652                 dummy_column = 0;
653                 cur_column = &dummy_column;
654         }
655
656         if (*cur_column >= wrap_point) {
657                 kprintf("\n");
658                 *cur_column = 0;
659         }
660         printed = kprintf("%s[0x%x]", name, value);
661         if (table == NULL) {
662                 printed += kprintf(" ");
663                 *cur_column += printed;
664                 return (printed);
665         }
666         printed_mask = 0;
667         while (printed_mask != 0xFF) {
668                 int entry;
669
670                 for (entry = 0; entry < num_entries; entry++) {
671                         if (((value & table[entry].mask)
672                           != table[entry].value)
673                          || ((printed_mask & table[entry].mask)
674                           == table[entry].mask))
675                                 continue;
676
677                         printed += kprintf("%s%s",
678                                           printed_mask == 0 ? ":(" : "|",
679                                           table[entry].name);
680                         printed_mask |= table[entry].mask;
681                         break;
682                 }
683                 if (entry >= num_entries)
684                         break;
685         }
686         if (printed_mask != 0)
687                 printed += kprintf(") ");
688         else
689                 printed += kprintf(" ");
690         *cur_column += printed;
691         return (printed);
692 }
693
694 static mpt_decode_entry_t req_state_parse_table[] = {
695         { "REQ_FREE",           0x00, 0xff },
696         { "REQ_ALLOCATED",      0x01, 0x01 },
697         { "REQ_QUEUED",         0x02, 0x02 },
698         { "REQ_DONE",           0x04, 0x04 },
699         { "REQ_TIMEDOUT",       0x08, 0x08 },
700         { "REQ_NEED_WAKEUP",    0x10, 0x10 }
701 };
702
703 void
704 mpt_req_state(mpt_req_state_t state)
705 {
706         mpt_decode_value(req_state_parse_table,
707                          NUM_ELEMENTS(req_state_parse_table),
708                          "REQ_STATE", state, NULL, 80);
709 }
710
711 #define LAST_SGE        (               \
712         MPI_SGE_FLAGS_END_OF_LIST |     \
713         MPI_SGE_FLAGS_END_OF_BUFFER|    \
714         MPI_SGE_FLAGS_LAST_ELEMENT)
715 void
716 mpt_dump_sgl(SGE_IO_UNION *su, int offset)
717 {
718         SGE_SIMPLE32 *se = (SGE_SIMPLE32 *) su;
719         const char allfox[4] = { 0xff, 0xff, 0xff, 0xff };
720         void *nxtaddr = se;
721         void *lim;
722         int flags;
723
724         /*
725          * Can't be any bigger than this.
726          */
727         lim = &((char *)se)[MPT_REQUEST_AREA - offset];
728
729         do {
730                 int iprt;
731
732                 kprintf("\t");
733                 if (memcmp(se, allfox, 4) == 0) {
734                         uint32_t *nxt = (uint32_t *)se;
735                         kprintf("PAD  %p\n", se);
736                         nxtaddr = nxt + 1;
737                         se = nxtaddr;
738                         flags = 0;
739                         continue;
740                 }
741                 nxtaddr = se + 1;
742                 flags = MPI_SGE_GET_FLAGS(se->FlagsLength);
743                 switch (flags & MPI_SGE_FLAGS_ELEMENT_MASK) {
744                 case MPI_SGE_FLAGS_SIMPLE_ELEMENT:
745                         if (flags & MPI_SGE_FLAGS_64_BIT_ADDRESSING) {
746                                 SGE_SIMPLE64 *se64 = (SGE_SIMPLE64 *)se;
747                                 kprintf("SE64 %p: Addr=0x%08x%08x FlagsLength"
748                                     "=0x%0x\n", se64,
749                                     (unsigned)se64->Address.High,
750                                     (unsigned)se64->Address.Low,
751                                     (unsigned)se64->FlagsLength);
752                                 nxtaddr = se64 + 1;
753                         } else {
754                                 kprintf("SE32 %p: Addr=0x%0x FlagsLength=0x%0x"
755                                     "\n", se,
756                                     (unsigned)se->Address,
757                                     (unsigned)se->FlagsLength);
758                         }
759                         kprintf(" ");
760                         break;
761                 case MPI_SGE_FLAGS_CHAIN_ELEMENT:
762                         if (flags & MPI_SGE_FLAGS_64_BIT_ADDRESSING) {
763                                 SGE_CHAIN64 *ce64 = (SGE_CHAIN64 *) se;
764                                 kprintf("CE64 %p: Addr=0x%08x%08x NxtChnO=0x%x "
765                                     "Flgs=0x%x Len=0x%0x\n", ce64,
766                                     (unsigned)ce64->Address.High,
767                                     (unsigned)ce64->Address.Low,
768                                     ce64->NextChainOffset,
769                                     (unsigned)ce64->Flags,
770                                     (unsigned)ce64->Length);
771                                 nxtaddr = ce64 + 1;
772                         } else {
773                                 SGE_CHAIN32 *ce = (SGE_CHAIN32 *) se;
774                                 kprintf("CE32 %p: Addr=0x%0x NxtChnO=0x%x "
775                                     " Flgs=0x%x Len=0x%0x\n", ce,
776                                     (unsigned)ce->Address,
777                                     ce->NextChainOffset,
778                                     ce->Flags,
779                                     ce->Length);
780                         }
781                         flags = 0;
782                         break;
783                 case MPI_SGE_FLAGS_TRANSACTION_ELEMENT:
784                         kprintf("TE32 @ %p\n", se);
785                         flags = 0;
786                         break;
787                 }
788                 iprt = 0;
789 #define MPT_PRINT_FLAG(x)                                               \
790                 if (flags & MPI_SGE_FLAGS_ ## x ) {                     \
791                         if (iprt == 0) {                                \
792                                 kprintf("\t");                          \
793                         }                                               \
794                         kprintf(" ");                                   \
795                         kprintf( #x );                                  \
796                         iprt++;                                         \
797                 }
798                 MPT_PRINT_FLAG(LOCAL_ADDRESS);
799                 MPT_PRINT_FLAG(HOST_TO_IOC);
800                 MPT_PRINT_FLAG(64_BIT_ADDRESSING);
801                 MPT_PRINT_FLAG(LAST_ELEMENT);
802                 MPT_PRINT_FLAG(END_OF_BUFFER);
803                 MPT_PRINT_FLAG(END_OF_LIST);
804 #undef MPT_PRINT_FLAG
805                 if (iprt)
806                         kprintf("\n");
807                 se = nxtaddr;
808                 if ((flags & LAST_SGE) == LAST_SGE) {
809                         break;
810                 }
811         } while ((flags & MPI_SGE_FLAGS_END_OF_LIST) == 0 && nxtaddr < lim);
812 }
813
814 void
815 mpt_dump_data(struct mpt_softc *mpt, const char *msg, void *addr, int len)
816 {
817         int offset;
818         uint8_t *cp = addr;
819         mpt_prt(mpt, "%s:", msg);
820         for (offset = 0; offset < len; offset++) {
821                 if ((offset & 0xf) == 0) {
822                         mpt_prtc(mpt, "\n");
823                 }
824                 mpt_prtc(mpt, " %02x", cp[offset]);
825         }
826         mpt_prtc(mpt, "\n");
827 }
828
829 void
830 mpt_dump_request(struct mpt_softc *mpt, request_t *req)
831 {
832         uint32_t *pReq = req->req_vbuf;
833         int o;
834 #if __FreeBSD_version >= 500000
835         mpt_prt(mpt, "Send Request %d (%jx):",
836             req->index, (uintmax_t) req->req_pbuf);
837 #else
838         mpt_prt(mpt, "Send Request %d (%llx):",
839             req->index, (unsigned long long) req->req_pbuf);
840 #endif
841         for (o = 0; o < mpt->ioc_facts.RequestFrameSize; o++) {
842                 if ((o & 0x7) == 0) {
843                         mpt_prtc(mpt, "\n");
844                         mpt_prt(mpt, " ");
845                 }
846                 mpt_prtc(mpt, " %08x", pReq[o]);
847         }
848         mpt_prtc(mpt, "\n");
849 }
850
851 #if __FreeBSD_version < 500000
852 void
853 mpt_lprt(struct mpt_softc *mpt, int level, const char *fmt, ...)
854 {
855         __va_list ap;
856         if (level <= mpt->verbose) {
857                 kprintf("%s: ", device_get_nameunit(mpt->dev));
858                 __va_start(ap, fmt);
859                 kvprintf(fmt, ap);
860                 __va_end(ap);
861         }
862 }
863
864 void
865 mpt_lprtc(struct mpt_softc *mpt, int level, const char *fmt, ...)
866 {
867         __va_list ap;
868         if (level <= mpt->verbose) {
869                 __va_start(ap, fmt);
870                 kvprintf(fmt, ap);
871                 __va_end(ap);
872         }
873 }
874 #endif
875
876 void
877 mpt_prt(struct mpt_softc *mpt, const char *fmt, ...)
878 {
879         __va_list ap;
880
881         kprintf("%s: ", device_get_nameunit(mpt->dev));
882         __va_start(ap, fmt);
883         kvprintf(fmt, ap);
884         __va_end(ap);
885 }
886
887 void
888 mpt_prtc(struct mpt_softc *mpt, const char *fmt, ...)
889 {
890         __va_list ap;
891
892         __va_start(ap, fmt);
893         kvprintf(fmt, ap);
894         __va_end(ap);
895 }