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