xref: /freebsd/sys/dev/mpt/mpt_debug.c (revision 262e143bd46171a6415a5b28af260a5efa2a3db8)
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  * Additional Copyright (c) 2002 by Matthew Jacob under same license.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <dev/mpt/mpt.h>
35 
36 #include <dev/mpt/mpilib/mpi_ioc.h>
37 #include <dev/mpt/mpilib/mpi_init.h>
38 #include <dev/mpt/mpilib/mpi_fc.h>
39 
40 #include <cam/scsi/scsi_all.h>
41 
42 #include <machine/stdarg.h>	/* for use by mpt_prt below */
43 
44 struct Error_Map {
45 	int 	 Error_Code;
46 	char    *Error_String;
47 };
48 
49 static const struct Error_Map IOC_Status[] = {
50 { MPI_IOCSTATUS_SUCCESS,                  "Success" },
51 { MPI_IOCSTATUS_INVALID_FUNCTION,         "IOC: Invalid Function" },
52 { MPI_IOCSTATUS_BUSY,                     "IOC: Busy" },
53 { MPI_IOCSTATUS_INVALID_SGL,              "IOC: Invalid SGL" },
54 { MPI_IOCSTATUS_INTERNAL_ERROR,           "IOC: Internal Error" },
55 { MPI_IOCSTATUS_RESERVED,                 "IOC: Reserved" },
56 { MPI_IOCSTATUS_INSUFFICIENT_RESOURCES,   "IOC: Insufficient Resources" },
57 { MPI_IOCSTATUS_INVALID_FIELD,            "IOC: Invalid Field" },
58 { MPI_IOCSTATUS_INVALID_STATE,            "IOC: Invalid State" },
59 { MPI_IOCSTATUS_CONFIG_INVALID_ACTION,    "Invalid Action" },
60 { MPI_IOCSTATUS_CONFIG_INVALID_TYPE,      "Invalid Type" },
61 { MPI_IOCSTATUS_CONFIG_INVALID_PAGE,      "Invalid Page" },
62 { MPI_IOCSTATUS_CONFIG_INVALID_DATA,      "Invalid Data" },
63 { MPI_IOCSTATUS_CONFIG_NO_DEFAULTS,       "No Defaults" },
64 { MPI_IOCSTATUS_CONFIG_CANT_COMMIT,       "Can't Commit" },
65 { MPI_IOCSTATUS_SCSI_RECOVERED_ERROR,     "SCSI: Recoverd Error" },
66 { MPI_IOCSTATUS_SCSI_INVALID_BUS,         "SCSI: Invalid Bus" },
67 { MPI_IOCSTATUS_SCSI_INVALID_TARGETID,    "SCSI: Invalid Target ID" },
68 { MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE,    "SCSI: Device Not There" },
69 { MPI_IOCSTATUS_SCSI_DATA_OVERRUN,        "SCSI: Data Overrun" },
70 { MPI_IOCSTATUS_SCSI_DATA_UNDERRUN,       "SCSI: Data Underrun" },
71 { MPI_IOCSTATUS_SCSI_IO_DATA_ERROR,       "SCSI: Data Error" },
72 { MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR,      "SCSI: Protocol Error" },
73 { MPI_IOCSTATUS_SCSI_TASK_TERMINATED,     "SCSI: Task Terminated" },
74 { MPI_IOCSTATUS_SCSI_RESIDUAL_MISMATCH,   "SCSI: Residual Mismatch" },
75 { MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED,    "SCSI: Task Management Failed" },
76 { MPI_IOCSTATUS_SCSI_IOC_TERMINATED,      "SCSI: IOC Bus Reset" },
77 { MPI_IOCSTATUS_SCSI_EXT_TERMINATED,      "SCSI: External Bus Reset" },
78 { MPI_IOCSTATUS_TARGET_PRIORITY_IO,       "SCSI Target: Priority I/O" },
79 { MPI_IOCSTATUS_TARGET_INVALID_PORT,      "SCSI Target: Invalid Port" },
80 { MPI_IOCSTATUS_TARGET_INVALID_IOCINDEX,  "SCSI Target: Invalid IOC Index" },
81 { MPI_IOCSTATUS_TARGET_ABORTED,           "SCSI Target: Aborted" },
82 { MPI_IOCSTATUS_TARGET_NO_CONN_RETRYABLE, "SCSI Target: No Connection (Retryable)" },
83 { MPI_IOCSTATUS_TARGET_NO_CONNECTION,     "SCSI Target: No Connection" },
84 { MPI_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH,"SCSI Target: Transfer Count Mismatch" },
85 { MPI_IOCSTATUS_TARGET_FC_ABORTED,        "FC: Aborted" },
86 { MPI_IOCSTATUS_TARGET_FC_RX_ID_INVALID,  "FC: Recieve ID Invalid" },
87 { MPI_IOCSTATUS_TARGET_FC_DID_INVALID,    "FC: Recieve DID Invalid" },
88 { MPI_IOCSTATUS_TARGET_FC_NODE_LOGGED_OUT,"FC: Node Logged Out" },
89 { MPI_IOCSTATUS_LAN_DEVICE_NOT_FOUND,     "LAN: Device Not Found" },
90 { MPI_IOCSTATUS_LAN_DEVICE_FAILURE,       "LAN: Device Not Failure" },
91 { MPI_IOCSTATUS_LAN_TRANSMIT_ERROR,       "LAN: Transmit Error" },
92 { MPI_IOCSTATUS_LAN_TRANSMIT_ABORTED,     "LAN: Transmit Aborted" },
93 { MPI_IOCSTATUS_LAN_RECEIVE_ERROR,        "LAN: Recieve Error" },
94 { MPI_IOCSTATUS_LAN_RECEIVE_ABORTED,      "LAN: Recieve Aborted" },
95 { MPI_IOCSTATUS_LAN_PARTIAL_PACKET,       "LAN: Partial Packet" },
96 { MPI_IOCSTATUS_LAN_CANCELED,             "LAN: Canceled" },
97 { -1, 0},
98 };
99 
100 static const struct Error_Map IOC_Func[] = {
101 { MPI_FUNCTION_SCSI_IO_REQUEST,              "SCSI IO Request" },
102 { MPI_FUNCTION_SCSI_TASK_MGMT,               "SCSI Task Management" },
103 { MPI_FUNCTION_IOC_INIT,                     "IOC Init" },
104 { MPI_FUNCTION_IOC_FACTS,                    "IOC Facts" },
105 { MPI_FUNCTION_CONFIG,                       "Config" },
106 { MPI_FUNCTION_PORT_FACTS,                   "Port Facts" },
107 { MPI_FUNCTION_PORT_ENABLE,                  "Port Enable" },
108 { MPI_FUNCTION_EVENT_NOTIFICATION,           "Event Notification" },
109 { MPI_FUNCTION_EVENT_ACK,                    "Event Ack" },
110 { MPI_FUNCTION_FW_DOWNLOAD,                  "FW Download" },
111 { MPI_FUNCTION_TARGET_CMD_BUFFER_POST,       "SCSI Target Command Buffer" },
112 { MPI_FUNCTION_TARGET_ASSIST,                "Target Assist" },
113 { MPI_FUNCTION_TARGET_STATUS_SEND,           "Target Status Send" },
114 { MPI_FUNCTION_TARGET_MODE_ABORT,            "Target Mode Abort" },
115 { MPI_FUNCTION_TARGET_FC_BUF_POST_LINK_SRVC, "FC: Link Service Buffers" },
116 { MPI_FUNCTION_TARGET_FC_RSP_LINK_SRVC,      "FC: Link Service Response" },
117 { MPI_FUNCTION_TARGET_FC_EX_SEND_LINK_SRVC,  "FC: Send Extended Link Service" },
118 { MPI_FUNCTION_TARGET_FC_ABORT,              "FC: Abort" },
119 { MPI_FUNCTION_FC_LINK_SRVC_BUF_POST,        "FC: Link Service Buffers" },
120 { MPI_FUNCTION_FC_LINK_SRVC_RSP,             "FC: Link Server Response" },
121 { MPI_FUNCTION_FC_EX_LINK_SRVC_SEND,         "FC: Send Extended Link Service" },
122 { MPI_FUNCTION_FC_ABORT,                     "FC: Abort" },
123 { MPI_FUNCTION_FW_UPLOAD,                    "FW Upload" },
124 { MPI_FUNCTION_FC_COMMON_TRANSPORT_SEND,     "FC: Send Common Transport" },
125 { MPI_FUNCTION_FC_PRIMITIVE_SEND,            "FC: Send Primitive" },
126 { MPI_FUNCTION_RAID_ACTION,                  "RAID Action" },
127 { MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH,     "RAID SCSI Pass-Through" },
128 { MPI_FUNCTION_TOOLBOX,                      "Toolbox Command" },
129 { MPI_FUNCTION_SCSI_ENCLOSURE_PROCESSOR,     "SCSI Enclosure Proc. Command" },
130 { MPI_FUNCTION_MAILBOX,                      "Mailbox Command" },
131 { MPI_FUNCTION_LAN_SEND,                     "LAN Send" },
132 { MPI_FUNCTION_LAN_RECEIVE,                  "LAN Recieve" },
133 { MPI_FUNCTION_LAN_RESET,                    "LAN Reset" },
134 { MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET,       "IOC Message Unit Reset" },
135 { MPI_FUNCTION_IO_UNIT_RESET,                "IO Unit Reset" },
136 { MPI_FUNCTION_HANDSHAKE,                    "Handshake" },
137 { MPI_FUNCTION_REPLY_FRAME_REMOVAL,          "Reply Frame Removal" },
138 { -1, 0},
139 };
140 
141 static const struct Error_Map IOC_Event[] = {
142 { MPI_EVENT_NONE,   	                "None" },
143 { MPI_EVENT_LOG_DATA,                   "LogData" },
144 { MPI_EVENT_STATE_CHANGE,               "State Change" },
145 { MPI_EVENT_UNIT_ATTENTION,             "Unit Attention" },
146 { MPI_EVENT_IOC_BUS_RESET,              "IOC Bus Reset" },
147 { MPI_EVENT_EXT_BUS_RESET,              "External Bus Reset" },
148 { MPI_EVENT_RESCAN,        	        "Rescan" },
149 { MPI_EVENT_LINK_STATUS_CHANGE,	        "Link Status Change" },
150 { MPI_EVENT_LOOP_STATE_CHANGE, 	        "Loop State Change" },
151 { MPI_EVENT_LOGOUT,    	       		"Logout" },
152 { MPI_EVENT_EVENT_CHANGE,               "EventChange" },
153 { -1, 0},
154 };
155 
156 static const struct Error_Map IOC_SCSIState[] = {
157 { MPI_SCSI_STATE_AUTOSENSE_VALID,	"AutoSense_Valid" },
158 { MPI_SCSI_STATE_AUTOSENSE_FAILED,	"AutoSense_Failed" },
159 { MPI_SCSI_STATE_NO_SCSI_STATUS,	"No_SCSI_Status" },
160 { MPI_SCSI_STATE_TERMINATED,	   	"State_Terminated" },
161 { MPI_SCSI_STATE_RESPONSE_INFO_VALID,	"Repsonse_Info_Valid" },
162 { MPI_SCSI_STATE_QUEUE_TAG_REJECTED,	"Queue Tag Rejected" },
163 { -1, 0},
164 };
165 
166 static const struct Error_Map IOC_SCSIStatus[] = {
167 { SCSI_STATUS_OK,			"OK" },
168 { SCSI_STATUS_CHECK_COND,		"Check Condition" },
169 { SCSI_STATUS_COND_MET,			"Check Condition Met" },
170 { SCSI_STATUS_BUSY,			"Busy" },
171 { SCSI_STATUS_INTERMED,			"Intermidiate Condition" },
172 { SCSI_STATUS_INTERMED_COND_MET,	"Intermidiate Condition Met" },
173 { SCSI_STATUS_RESERV_CONFLICT,		"Reservation Conflict" },
174 { SCSI_STATUS_CMD_TERMINATED,		"Command Terminated" },
175 { SCSI_STATUS_QUEUE_FULL,		"Queue Full" },
176 { -1, 0},
177 };
178 
179 static const struct Error_Map IOC_Diag[] = {
180 { MPI_DIAG_DRWE,		"DWE" },
181 { MPI_DIAG_FLASH_BAD_SIG,	"FLASH_Bad" },
182 { MPI_DIAGNOSTIC_OFFSET,	"Offset" },
183 { MPI_DIAG_RESET_ADAPTER,	"Reset" },
184 { MPI_DIAG_DISABLE_ARM,		"DisARM" },
185 { MPI_DIAG_MEM_ENABLE,		"DME" },
186 { -1, 0 },
187 };
188 
189 static const struct Error_Map IOC_SCSITMType[] = {
190 { MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK,		"Abort Task" },
191 { MPI_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET,	"Abort Task Set" },
192 { MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,	"Target Reset" },
193 { MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,		"Reset Bus" },
194 { MPI_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET,	"Logical Unit Reset" },
195 { -1, 0 },
196 };
197 
198 static void mpt_dump_sgl(SGE_IO_UNION *sgl);
199 
200 static char *
201 mpt_ioc_status(int code)
202 {
203 	const struct Error_Map *status = IOC_Status;
204 	static char buf[64];
205 	while (status->Error_Code >= 0) {
206 		if (status->Error_Code == (code & MPI_IOCSTATUS_MASK))
207 			return status->Error_String;
208 		status++;
209 	}
210 	snprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
211 	return buf;
212 }
213 
214 char *
215 mpt_ioc_diag(u_int32_t code)
216 {
217 	const struct Error_Map *status = IOC_Diag;
218 	static char buf[128];
219 	char *ptr = buf;
220 	char *end = &buf[128];
221 	buf[0] = '\0';
222 	ptr += snprintf(buf, sizeof buf, "(0x%08x)", code);
223 	while (status->Error_Code >= 0) {
224 		if ((status->Error_Code & code) != 0)
225 			ptr += snprintf(ptr, (size_t)(end-ptr), "%s ",
226 				status->Error_String);
227 		status++;
228 	}
229 	return buf;
230 }
231 
232 static char *
233 mpt_ioc_function(int code)
234 {
235 	const struct Error_Map *status = IOC_Func;
236 	static char buf[64];
237 	while (status->Error_Code >= 0) {
238 		if (status->Error_Code == code)
239 			return status->Error_String;
240 		status++;
241 	}
242 	snprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
243 	return buf;
244 }
245 static char *
246 mpt_ioc_event(int code)
247 {
248 	const struct Error_Map *status = IOC_Event;
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 static char *
259 mpt_scsi_state(int code)
260 {
261 	const struct Error_Map *status = IOC_SCSIState;
262 	static char buf[128];
263 	char *ptr = buf;
264 	char *end = &buf[128];
265 	buf[0] = '\0';
266 	ptr += snprintf(buf, sizeof buf, "(0x%08x)", code);
267 	while (status->Error_Code >= 0) {
268 		if ((status->Error_Code & code) != 0)
269 			ptr += snprintf(ptr, (size_t)(end-ptr), "%s ",
270 				status->Error_String);
271 		status++;
272 	}
273 	return buf;
274 }
275 static char *
276 mpt_scsi_status(int code)
277 {
278 	const struct Error_Map *status = IOC_SCSIStatus;
279 	static char buf[64];
280 	while (status->Error_Code >= 0) {
281 		if (status->Error_Code == code)
282 			return status->Error_String;
283 		status++;
284 	}
285 	snprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
286 	return buf;
287 }
288 static char *
289 mpt_who(int who_init)
290 {
291 	char *who;
292 
293 	switch (who_init) {
294 	case MPT_DB_INIT_NOONE:       who = "No One";        break;
295 	case MPT_DB_INIT_BIOS:        who = "BIOS";          break;
296 	case MPT_DB_INIT_ROMBIOS:     who = "ROM BIOS";      break;
297 	case MPT_DB_INIT_PCIPEER:     who = "PCI Peer";      break;
298 	case MPT_DB_INIT_HOST:        who = "Host Driver";   break;
299 	case MPT_DB_INIT_MANUFACTURE: who = "Manufacturing"; break;
300 	default:                      who = "Unknown";       break;
301 	}
302 	return who;
303 }
304 
305 static char *
306 mpt_state(u_int32_t mb)
307 {
308 	char *text;
309 
310 	switch (MPT_STATE(mb)) {
311 		case MPT_DB_STATE_RESET:  text = "Reset";   break;
312 		case MPT_DB_STATE_READY:  text = "Ready";   break;
313 		case MPT_DB_STATE_RUNNING:text = "Running"; break;
314 		case MPT_DB_STATE_FAULT:  text = "Fault";   break;
315 		default: 		  text = "Unknown"; break;
316 	}
317 	return text;
318 }
319 
320 static char *
321 mpt_scsi_tm_type(int code)
322 {
323 	const struct Error_Map *status = IOC_SCSITMType;
324 	static char buf[64];
325 	while (status->Error_Code >= 0) {
326 		if (status->Error_Code == code)
327 			return status->Error_String;
328 		status++;
329 	}
330 	snprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
331 	return buf;
332 }
333 
334 void
335 mpt_print_db(u_int32_t mb)
336 {
337 	printf("mpt mailbox: (0x%x) State %s  WhoInit %s\n",
338 	    mb, mpt_state(mb), mpt_who(MPT_WHO(mb)));
339 }
340 
341 /*****************************************************************************/
342 /*  Reply functions                                                          */
343 /*****************************************************************************/
344 static void
345 mpt_print_reply_hdr(MSG_DEFAULT_REPLY *msg)
346 {
347 	printf("%s Reply @ %p\n", mpt_ioc_function(msg->Function), msg);
348 	printf("\tIOC Status    %s\n", mpt_ioc_status(msg->IOCStatus));
349 	printf("\tIOCLogInfo    0x%08x\n", msg->IOCLogInfo);
350 	printf("\tMsgLength     0x%02x\n", msg->MsgLength);
351 	printf("\tMsgFlags      0x%02x\n", msg->MsgFlags);
352 	printf("\tMsgContext    0x%08x\n", msg->MsgContext);
353 }
354 
355 static void
356 mpt_print_init_reply(MSG_IOC_INIT_REPLY *msg)
357 {
358 	mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
359 	printf("\tWhoInit       %s\n", mpt_who(msg->WhoInit));
360 	printf("\tMaxDevices    0x%02x\n", msg->MaxDevices);
361 	printf("\tMaxBuses     0x%02x\n", msg->MaxBuses);
362 }
363 
364 static void
365 mpt_print_ioc_facts(MSG_IOC_FACTS_REPLY *msg)
366 {
367 	mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
368 	printf("\tIOCNumber     %d\n",		msg->IOCNumber);
369 	printf("\tMaxChainDepth %d\n",		msg->MaxChainDepth);
370 	printf("\tWhoInit       %s\n",		mpt_who(msg->WhoInit));
371 	printf("\tBlockSize     %d\n",		msg->BlockSize);
372 	printf("\tFlags         %d\n",		msg->Flags);
373 	printf("\tReplyQueueDepth %d\n",	msg->ReplyQueueDepth);
374 	printf("\tReqFrameSize  0x%04x\n",	msg->RequestFrameSize);
375 	printf("\tFW Version    0x%08x\n",	msg->FWVersion.Word);
376 	printf("\tProduct ID    0x%04x\n",	msg->ProductID);
377 	printf("\tCredits       0x%04x\n",	msg->GlobalCredits);
378 	printf("\tPorts         %d\n",		msg->NumberOfPorts);
379 	printf("\tEventState    0x%02x\n",	msg->EventState);
380 	printf("\tHostMFA_HA    0x%08x\n",	msg->CurrentHostMfaHighAddr);
381 	printf("\tSenseBuf_HA   0x%08x\n",
382 	    msg->CurrentSenseBufferHighAddr);
383 	printf("\tRepFrameSize  0x%04x\n",	msg->CurReplyFrameSize);
384 	printf("\tMaxDevices    0x%02x\n",	msg->MaxDevices);
385 	printf("\tMaxBuses      0x%02x\n",	msg->MaxBuses);
386 	printf("\tFWImageSize   0x%04x\n",	msg->FWImageSize);
387 }
388 
389 static void
390 mpt_print_enable_reply(MSG_PORT_ENABLE_REPLY *msg)
391 {
392 	mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
393 	printf("\tPort:         %d\n", msg->PortNumber);
394 }
395 
396 static void
397 mpt_print_scsi_io_reply(MSG_SCSI_IO_REPLY *msg)
398 {
399 	mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
400 	printf("\tBus:          %d\n", msg->Bus);
401 	printf("\tTargetID      %d\n", msg->TargetID);
402 	printf("\tCDBLength     %d\n", msg->CDBLength);
403 	printf("\tSCSI Status:  %s\n", mpt_scsi_status(msg->SCSIStatus));
404 	printf("\tSCSI State:   %s\n", mpt_scsi_state(msg->SCSIState));
405 	printf("\tTransferCnt   0x%04x\n", msg->TransferCount);
406 	printf("\tSenseCnt      0x%04x\n", msg->SenseCount);
407 	printf("\tResponseInfo  0x%08x\n", msg->ResponseInfo);
408 }
409 
410 
411 
412 static void
413 mpt_print_event_notice(MSG_EVENT_NOTIFY_REPLY *msg)
414 {
415 	mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
416 	printf("\tEvent:        %s\n", mpt_ioc_event(msg->Event));
417 	printf("\tEventContext  0x%04x\n", msg->EventContext);
418 	printf("\tAckRequired     %d\n", msg->AckRequired);
419 	printf("\tEventDataLength %d\n", msg->EventDataLength);
420 	printf("\tContinuation    %d\n", msg->MsgFlags & 0x80);
421 	switch(msg->Event) {
422 	case MPI_EVENT_LOG_DATA:
423 		printf("\tEvtLogData:   0x%04x\n", msg->Data[0]);
424 		break;
425 
426 	case MPI_EVENT_UNIT_ATTENTION:
427 		printf("\tTargetID:     0x%04x\n",
428 			msg->Data[0] & 0xff);
429 		printf("\tBus:          0x%04x\n",
430 			(msg->Data[0] >> 8) & 0xff);
431 		break;
432 
433 	case MPI_EVENT_IOC_BUS_RESET:
434 	case MPI_EVENT_EXT_BUS_RESET:
435 	case MPI_EVENT_RESCAN:
436 		printf("\tPort:           %d\n",
437 			(msg->Data[0] >> 8) & 0xff);
438 		break;
439 
440 	case MPI_EVENT_LINK_STATUS_CHANGE:
441 		printf("\tLinkState:    %d\n",
442 			msg->Data[0] & 0xff);
443 		printf("\tPort:         %d\n",
444 			(msg->Data[1] >> 8) & 0xff);
445 		break;
446 
447 	case MPI_EVENT_LOOP_STATE_CHANGE:
448 		printf("\tType:         %d\n",
449 			(msg->Data[0] >> 16) & 0xff);
450 		printf("\tChar3:      0x%02x\n",
451 			(msg->Data[0] >> 8) & 0xff);
452 		printf("\tChar4:      0x%02x\n",
453 			(msg->Data[0]     ) & 0xff);
454 		printf("\tPort:         %d\n",
455 			(msg->Data[1] >> 8) & 0xff);
456 		break;
457 
458 	case MPI_EVENT_LOGOUT:
459 		printf("\tN_PortId:   0x%04x\n", msg->Data[0]);
460 		printf("\tPort:         %d\n",
461 			(msg->Data[1] >> 8) & 0xff);
462 		break;
463 	}
464 
465 }
466 
467 void
468 mpt_print_reply(void *vmsg)
469 {
470 	MSG_DEFAULT_REPLY *msg = vmsg;
471 
472 	switch (msg->Function) {
473 	case MPI_FUNCTION_EVENT_NOTIFICATION:
474 		mpt_print_event_notice((MSG_EVENT_NOTIFY_REPLY *)msg);
475 		break;
476 	case MPI_FUNCTION_PORT_ENABLE:
477 		mpt_print_enable_reply((MSG_PORT_ENABLE_REPLY *)msg);
478 		break;
479 	case MPI_FUNCTION_IOC_FACTS:
480 		mpt_print_ioc_facts((MSG_IOC_FACTS_REPLY *)msg);
481 		break;
482 	case MPI_FUNCTION_IOC_INIT:
483 		mpt_print_init_reply((MSG_IOC_INIT_REPLY *)msg);
484 		break;
485 	case MPI_FUNCTION_SCSI_IO_REQUEST:
486 		mpt_print_scsi_io_reply((MSG_SCSI_IO_REPLY *)msg);
487 		break;
488 	default:
489 		mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
490 		break;
491 	}
492 }
493 
494 /*****************************************************************************/
495 /*  Request functions                                                        */
496 /*****************************************************************************/
497 static void
498 mpt_print_request_hdr(MSG_REQUEST_HEADER *req)
499 {
500 	printf("%s @ %p\n", mpt_ioc_function(req->Function), req);
501 	printf("\tChain Offset  0x%02x\n", req->ChainOffset);
502 	printf("\tMsgFlags      0x%02x\n", req->MsgFlags);
503 	printf("\tMsgContext    0x%08x\n", req->MsgContext);
504 }
505 
506 void
507 mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *orig_msg)
508 {
509 	MSG_SCSI_IO_REQUEST local, *msg = &local;
510 	int i;
511 
512 	bcopy(orig_msg, msg, sizeof (MSG_SCSI_IO_REQUEST));
513 	mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg);
514 	printf("\tBus:                %d\n", msg->Bus);
515 	printf("\tTargetID            %d\n", msg->TargetID);
516 	printf("\tSenseBufferLength   %d\n", msg->SenseBufferLength);
517 	printf("\tLUN:              0x%0x\n", msg->LUN[1]);
518 	printf("\tControl           0x%08x ", msg->Control);
519 #define MPI_PRINT_FIELD(x)						\
520 	case MPI_SCSIIO_CONTROL_ ## x :					\
521 		printf(" " #x " ");					\
522 		break
523 
524 	switch (msg->Control & MPI_SCSIIO_CONTROL_DATADIRECTION_MASK) {
525 	MPI_PRINT_FIELD(NODATATRANSFER);
526 	MPI_PRINT_FIELD(WRITE);
527 	MPI_PRINT_FIELD(READ);
528 	default:
529 		printf(" Invalid DIR! ");
530 		break;
531 	}
532 	switch (msg->Control & MPI_SCSIIO_CONTROL_TASKATTRIBUTE_MASK) {
533 	MPI_PRINT_FIELD(SIMPLEQ);
534 	MPI_PRINT_FIELD(HEADOFQ);
535 	MPI_PRINT_FIELD(ORDEREDQ);
536 	MPI_PRINT_FIELD(ACAQ);
537 	MPI_PRINT_FIELD(UNTAGGED);
538 	MPI_PRINT_FIELD(NO_DISCONNECT);
539 	default:
540 		printf(" Unknown attribute! ");
541 		break;
542 	}
543 
544 	printf("\n");
545 #undef MPI_PRINT_FIELD
546 
547 	printf("\tDataLength\t0x%08x\n", msg->DataLength);
548 	printf("\tSenseBufAddr\t0x%08x\n", msg->SenseBufferLowAddr);
549 	printf("\tCDB[0:%d]\t", msg->CDBLength);
550 	for (i = 0; i < msg->CDBLength; i++)
551 		printf("%02x ", msg->CDB[i]);
552 	printf("\n");
553 	mpt_dump_sgl(&orig_msg->SGL);
554 }
555 
556 static void
557 mpt_print_scsi_tmf_request(MSG_SCSI_TASK_MGMT *msg)
558 {
559 	mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg);
560 	printf("\tLun             0x%02x\n", msg->LUN[1]);
561 	printf("\tTaskType        %s\n", mpt_scsi_tm_type(msg->TaskType));
562 	printf("\tTaskMsgContext  0x%08x\n", msg->TaskMsgContext);
563 }
564 
565 void
566 mpt_print_request(void *vreq)
567 {
568 	MSG_REQUEST_HEADER *req = vreq;
569 
570 	switch (req->Function) {
571 	case MPI_FUNCTION_SCSI_IO_REQUEST:
572 		mpt_print_scsi_io_request((MSG_SCSI_IO_REQUEST *)req);
573 		break;
574 	case MPI_FUNCTION_SCSI_TASK_MGMT:
575 		mpt_print_scsi_tmf_request((MSG_SCSI_TASK_MGMT *)req);
576 	default:
577 		mpt_print_request_hdr(req);
578 		break;
579 	}
580 }
581 
582 int
583 mpt_decode_value(mpt_decode_entry_t *table, u_int num_entries,
584 		 const char *name, u_int value, u_int *cur_column,
585 		 u_int wrap_point)
586 {
587         int     printed;
588         u_int   printed_mask;
589 	u_int	dummy_column;
590 
591 	if (cur_column == NULL) {
592 		dummy_column = 0;
593 		cur_column = &dummy_column;
594 	}
595 
596 	if (*cur_column >= wrap_point) {
597 		printf("\n");
598 		*cur_column = 0;
599 	}
600 	printed = printf("%s[0x%x]", name, value);
601 	if (table == NULL) {
602 		printed += printf(" ");
603 		*cur_column += printed;
604 		return (printed);
605 	}
606 	printed_mask = 0;
607 	while (printed_mask != 0xFF) {
608 		int entry;
609 
610 		for (entry = 0; entry < num_entries; entry++) {
611 			if (((value & table[entry].mask)
612 			  != table[entry].value)
613 			 || ((printed_mask & table[entry].mask)
614 			  == table[entry].mask))
615 				continue;
616 
617 			printed += printf("%s%s",
618 					  printed_mask == 0 ? ":(" : "|",
619 					  table[entry].name);
620 			printed_mask |= table[entry].mask;
621 			break;
622                 }
623 		if (entry >= num_entries)
624 			break;
625         }
626         if (printed_mask != 0)
627 		printed += printf(") ");
628         else
629 		printed += printf(" ");
630 	*cur_column += printed;
631 	return (printed);
632 }
633 
634 static mpt_decode_entry_t req_state_parse_table[] = {
635 	{ "REQ_FREE",		0x00, 0xff },
636 	{ "REQ_ALLOCATED",	0x01, 0x01 },
637 	{ "REQ_QUEUED",		0x02, 0x02 },
638 	{ "REQ_DONE",		0x04, 0x04 },
639 	{ "REQ_TIMEDOUT",	0x08, 0x08 },
640 	{ "REQ_NEED_WAKEUP",	0x10, 0x10 }
641 };
642 
643 void
644 mpt_req_state(mpt_req_state_t state)
645 {
646 	mpt_decode_value(req_state_parse_table,
647 			 NUM_ELEMENTS(req_state_parse_table),
648 			 "REQ_STATE", state, NULL, 80);
649 }
650 
651 static void
652 mpt_dump_sgl(SGE_IO_UNION *su)
653 {
654 	SGE_SIMPLE32 *se = (SGE_SIMPLE32 *) su;
655 	int iCount, flags;
656 
657 	iCount = MPT_SGL_MAX;
658 	do {
659 		int iprt;
660 
661 		printf("\t");
662 		flags = MPI_SGE_GET_FLAGS(se->FlagsLength);
663 		switch (flags & MPI_SGE_FLAGS_ELEMENT_MASK) {
664 		case MPI_SGE_FLAGS_SIMPLE_ELEMENT:
665 		{
666 			printf("SE32 %p: Addr=0x%0x FlagsLength=0x%0x\n",
667 			    se, se->Address, se->FlagsLength);
668 			printf(" ");
669 			break;
670 		}
671 		case MPI_SGE_FLAGS_CHAIN_ELEMENT:
672 		{
673 			SGE_CHAIN32 *ce = (SGE_CHAIN32 *) se;
674 			printf("CE32 %p: Addr=0x%0x NxtChnO=0x%x Flgs=0x%x "
675 			    "Len=0x%0x\n", ce, ce->Address, ce->NextChainOffset,
676 			    ce->Flags, ce->Length);
677 			flags = 0;
678 			break;
679 		}
680 		case MPI_SGE_FLAGS_TRANSACTION_ELEMENT:
681 			printf("TE32 @ %p\n", se);
682 			flags = 0;
683 			break;
684 		}
685 		iprt = 0;
686 #define MPT_PRINT_FLAG(x)						\
687 		if (flags & MPI_SGE_FLAGS_ ## x ) { 			\
688 			if (iprt == 0) {				\
689 				printf("\t");				\
690 			}						\
691 			printf(" ");					\
692 			printf( #x );					\
693 			iprt++;						\
694 		}
695 		MPT_PRINT_FLAG(LOCAL_ADDRESS);
696 		MPT_PRINT_FLAG(HOST_TO_IOC);
697 		MPT_PRINT_FLAG(64_BIT_ADDRESSING);
698 		MPT_PRINT_FLAG(LAST_ELEMENT);
699 		MPT_PRINT_FLAG(END_OF_BUFFER);
700 		MPT_PRINT_FLAG(END_OF_LIST);
701 #undef MPT_PRINT_FLAG
702 		if (iprt)
703 			printf("\n");
704 		se++;
705 		iCount -= 1;
706 	} while ((flags & MPI_SGE_FLAGS_END_OF_LIST) == 0 && iCount != 0);
707 }
708 
709 void
710 mpt_prt(struct mpt_softc *mpt, const char *fmt, ...)
711 {
712 	va_list ap;
713 
714 	printf("%s: ", device_get_nameunit(mpt->dev));
715 	va_start(ap, fmt);
716 	vprintf(fmt, ap);
717 	va_end(ap);
718 }
719 
720 void
721 mpt_prtc(struct mpt_softc *mpt, const char *fmt, ...)
722 {
723 	va_list ap;
724 
725 	va_start(ap, fmt);
726 	vprintf(fmt, ap);
727 	va_end(ap);
728 }
729