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