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