xref: /freebsd/sys/dev/mpt/mpt_debug.c (revision 4232b8b1341f8d46987e09760eb36e2b052b29a9)
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 { -1, 0},
116 };
117 
118 static const struct Error_Map IOC_Event[] = {
119 { MPI_EVENT_NONE,   	                "None" },
120 { MPI_EVENT_LOG_DATA,                   "LogData" },
121 { MPI_EVENT_STATE_CHANGE,               "State Change" },
122 { MPI_EVENT_UNIT_ATTENTION,             "Unit Attention" },
123 { MPI_EVENT_IOC_BUS_RESET,              "IOC Bus Reset" },
124 { MPI_EVENT_EXT_BUS_RESET,              "External Bus Reset" },
125 { MPI_EVENT_RESCAN,        	        "Rescan" },
126 { MPI_EVENT_LINK_STATUS_CHANGE,	        "Link Status Change" },
127 { MPI_EVENT_LOOP_STATE_CHANGE, 	        "Loop State Change" },
128 { MPI_EVENT_LOGOUT,    	       		"Logout" },
129 { MPI_EVENT_EVENT_CHANGE,               "EventChange" },
130 { -1, 0},
131 };
132 
133 static const struct Error_Map IOC_SCSIState[] = {
134 { MPI_SCSI_STATE_AUTOSENSE_VALID,	"AutoSense_Valid" },
135 { MPI_SCSI_STATE_AUTOSENSE_FAILED,	"AutoSense_Failed" },
136 { MPI_SCSI_STATE_NO_SCSI_STATUS,	"No_SCSI_Status" },
137 { MPI_SCSI_STATE_TERMINATED,	   	"State_Terminated" },
138 { MPI_SCSI_STATE_RESPONSE_INFO_VALID,	"Repsonse_Info_Valid" },
139 { MPI_SCSI_STATE_QUEUE_TAG_REJECTED,	"Queue Tag Rejected" },
140 { -1, 0},
141 };
142 
143 static const struct Error_Map IOC_SCSIStatus[] = {
144 { SCSI_STATUS_OK,			"OK" },
145 { SCSI_STATUS_CHECK_COND,		"Check Condition" },
146 { SCSI_STATUS_COND_MET,			"Check Condition Met" },
147 { SCSI_STATUS_BUSY,			"Busy" },
148 { SCSI_STATUS_INTERMED,			"Intermidiate Condition" },
149 { SCSI_STATUS_INTERMED_COND_MET,	"Intermidiate Condition Met" },
150 { SCSI_STATUS_RESERV_CONFLICT,		"Reservation Conflict" },
151 { SCSI_STATUS_CMD_TERMINATED,		"Command Terminated" },
152 { SCSI_STATUS_QUEUE_FULL,		"Queue Full" },
153 { -1, 0},
154 };
155 
156 static const struct Error_Map IOC_Diag[] = {
157 { MPI_DIAG_DRWE,		"DWE" },
158 { MPI_DIAG_FLASH_BAD_SIG,	"FLASH_Bad" },
159 { MPI_DIAGNOSTIC_OFFSET,	"Offset" },
160 { MPI_DIAG_RESET_ADAPTER,	"Reset" },
161 { MPI_DIAG_DISABLE_ARM,		"DisARM" },
162 { MPI_DIAG_MEM_ENABLE,		"DME" },
163 { -1, 0 },
164 };
165 
166 static const struct Error_Map IOC_SCSITMType[] = {
167 { MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK,		"Abort Task" },
168 { MPI_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET,	"Abort Task Set" },
169 { MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,	"Target Reset" },
170 { MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,		"Reset Bus" },
171 { MPI_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET,	"Logical Unit Reset" },
172 { -1, 0 },
173 };
174 
175 static char *
176 mpt_ioc_status(int code)
177 {
178 	const struct Error_Map *status = IOC_Status;
179 	static char buf[64];
180 	while (status->Error_Code >= 0) {
181 		if (status->Error_Code == (code & MPI_IOCSTATUS_MASK))
182 			return status->Error_String;
183 		status++;
184 	}
185 	snprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
186 	return buf;
187 }
188 
189 char *
190 mpt_ioc_diag(u_int32_t code)
191 {
192 	const struct Error_Map *status = IOC_Diag;
193 	static char buf[128];
194 	char *ptr = buf;
195 	char *end = &buf[128];
196 	buf[0] = '\0';
197 	ptr += snprintf(buf, sizeof buf, "(0x%08x)", code);
198 	while (status->Error_Code >= 0) {
199 		if ((status->Error_Code & code) != 0)
200 			ptr += snprintf(ptr, (size_t)(end-ptr), "%s ",
201 				status->Error_String);
202 		status++;
203 	}
204 	return buf;
205 }
206 
207 static char *
208 mpt_ioc_function(int code)
209 {
210 	const struct Error_Map *status = IOC_Func;
211 	static char buf[64];
212 	while (status->Error_Code >= 0) {
213 		if (status->Error_Code == code)
214 			return status->Error_String;
215 		status++;
216 	}
217 	snprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
218 	return buf;
219 }
220 static char *
221 mpt_ioc_event(int code)
222 {
223 	const struct Error_Map *status = IOC_Event;
224 	static char buf[64];
225 	while (status->Error_Code >= 0) {
226 		if (status->Error_Code == code)
227 			return status->Error_String;
228 		status++;
229 	}
230 	snprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
231 	return buf;
232 }
233 static char *
234 mpt_scsi_state(int code)
235 {
236 	const struct Error_Map *status = IOC_SCSIState;
237 	static char buf[128];
238 	char *ptr = buf;
239 	char *end = &buf[128];
240 	buf[0] = '\0';
241 	ptr += snprintf(buf, sizeof buf, "(0x%08x)", code);
242 	while (status->Error_Code >= 0) {
243 		if ((status->Error_Code & code) != 0)
244 			ptr += snprintf(ptr, (size_t)(end-ptr), "%s ",
245 				status->Error_String);
246 		status++;
247 	}
248 	return buf;
249 }
250 static char *
251 mpt_scsi_status(int code)
252 {
253 	const struct Error_Map *status = IOC_SCSIStatus;
254 	static char buf[64];
255 	while (status->Error_Code >= 0) {
256 		if (status->Error_Code == code)
257 			return status->Error_String;
258 		status++;
259 	}
260 	snprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
261 	return buf;
262 }
263 static char *
264 mpt_who(int who_init)
265 {
266 	char *who;
267 
268 	switch (who_init) {
269 	case MPT_DB_INIT_NOONE:       who = "No One";        break;
270 	case MPT_DB_INIT_BIOS:        who = "BIOS";          break;
271 	case MPT_DB_INIT_ROMBIOS:     who = "ROM BIOS";      break;
272 	case MPT_DB_INIT_PCIPEER:     who = "PCI Peer";      break;
273 	case MPT_DB_INIT_HOST:        who = "Host Driver";   break;
274 	case MPT_DB_INIT_MANUFACTURE: who = "Manufacturing"; break;
275 	default:                      who = "Unknown";       break;
276 	}
277 	return who;
278 }
279 
280 static char *
281 mpt_state(u_int32_t mb)
282 {
283 	char *text;
284 
285 	switch (MPT_STATE(mb)) {
286 		case MPT_DB_STATE_RESET:  text = "Reset";   break;
287 		case MPT_DB_STATE_READY:  text = "Ready";   break;
288 		case MPT_DB_STATE_RUNNING:text = "Running"; break;
289 		case MPT_DB_STATE_FAULT:  text = "Fault";   break;
290 		default: 		  text = "Unknown"; break;
291 	}
292 	return text;
293 }
294 
295 static char *
296 mpt_scsi_tm_type(int code)
297 {
298 	const struct Error_Map *status = IOC_SCSITMType;
299 	static char buf[64];
300 	while (status->Error_Code >= 0) {
301 		if (status->Error_Code == code)
302 			return status->Error_String;
303 		status++;
304 	}
305 	snprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
306 	return buf;
307 }
308 
309 void
310 mpt_print_db(u_int32_t mb)
311 {
312 	printf("mpt mailbox: (0x%x) State %s  WhoInit %s\n",
313 	    mb, mpt_state(mb), mpt_who(MPT_WHO(mb)));
314 }
315 
316 /*****************************************************************************/
317 /*  Reply functions                                                          */
318 /*****************************************************************************/
319 static void
320 mpt_print_reply_hdr(MSG_DEFAULT_REPLY *msg)
321 {
322 	printf("%s Reply @ %p\n", mpt_ioc_function(msg->Function), msg);
323 	printf("\tIOC Status    %s\n", mpt_ioc_status(msg->IOCStatus));
324 	printf("\tIOCLogInfo    0x%08x\n", msg->IOCLogInfo);
325 	printf("\tMsgLength     0x%02x\n", msg->MsgLength);
326 	printf("\tMsgFlags      0x%02x\n", msg->MsgFlags);
327 	printf("\tMsgContext    0x%08x\n", msg->MsgContext);
328 }
329 
330 static void
331 mpt_print_init_reply(MSG_IOC_INIT_REPLY *msg)
332 {
333 	mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
334 	printf("\tWhoInit       %s\n", mpt_who(msg->WhoInit));
335 	printf("\tMaxDevices    0x%02x\n", msg->MaxDevices);
336 	printf("\tMaxBuses     0x%02x\n", msg->MaxBuses);
337 }
338 
339 static void
340 mpt_print_ioc_facts(MSG_IOC_FACTS_REPLY *msg)
341 {
342 	mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
343 	printf("\tIOCNumber     %d\n",		msg->IOCNumber);
344 	printf("\tMaxChainDepth %d\n",		msg->MaxChainDepth);
345 	printf("\tWhoInit       %s\n",		mpt_who(msg->WhoInit));
346 	printf("\tBlockSize     %d\n",		msg->BlockSize);
347 	printf("\tFlags         %d\n",		msg->Flags);
348 	printf("\tReplyQueueDepth %d\n",	msg->ReplyQueueDepth);
349 	printf("\tReqFrameSize  0x%04x\n",	msg->RequestFrameSize);
350 	printf("\tFW Version    0x%08x\n",	msg->FWVersion.Word);
351 	printf("\tProduct ID    0x%04x\n",	msg->ProductID);
352 	printf("\tCredits       0x%04x\n",	msg->GlobalCredits);
353 	printf("\tPorts         %d\n",		msg->NumberOfPorts);
354 	printf("\tEventState    0x%02x\n",	msg->EventState);
355 	printf("\tHostMFA_HA    0x%08x\n",	msg->CurrentHostMfaHighAddr);
356 	printf("\tSenseBuf_HA   0x%08x\n",
357 	    msg->CurrentSenseBufferHighAddr);
358 	printf("\tRepFrameSize  0x%04x\n",	msg->CurReplyFrameSize);
359 	printf("\tMaxDevices    0x%02x\n",	msg->MaxDevices);
360 	printf("\tMaxBuses      0x%02x\n",	msg->MaxBuses);
361 	printf("\tFWImageSize   0x%04x\n",	msg->FWImageSize);
362 }
363 
364 static void
365 mpt_print_enable_reply(MSG_PORT_ENABLE_REPLY *msg)
366 {
367 	mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
368 	printf("\tPort:         %d\n", msg->PortNumber);
369 }
370 
371 static void
372 mpt_print_scsi_io_reply(MSG_SCSI_IO_REPLY *msg)
373 {
374 	mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
375 	printf("\tBus:          %d\n", msg->Bus);
376 	printf("\tTargetID      %d\n", msg->TargetID);
377 	printf("\tCDBLength     %d\n", msg->CDBLength);
378 	printf("\tSCSI Status:  %s\n", mpt_scsi_status(msg->SCSIStatus));
379 	printf("\tSCSI State:   %s\n", mpt_scsi_state(msg->SCSIState));
380 	printf("\tTransferCnt   0x%04x\n", msg->TransferCount);
381 	printf("\tSenseCnt      0x%04x\n", msg->SenseCount);
382 	printf("\tResponseInfo  0x%08x\n", msg->ResponseInfo);
383 }
384 
385 
386 
387 static void
388 mpt_print_event_notice(MSG_EVENT_NOTIFY_REPLY *msg)
389 {
390 	mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
391 	printf("\tEvent:        %s\n", mpt_ioc_event(msg->Event));
392 	printf("\tEventContext  0x%04x\n", msg->EventContext);
393 	printf("\tAckRequired     %d\n", msg->AckRequired);
394 	printf("\tEventDataLength %d\n", msg->EventDataLength);
395 	printf("\tContinuation    %d\n", msg->MsgFlags & 0x80);
396 	switch(msg->Event) {
397 	case MPI_EVENT_LOG_DATA:
398 		printf("\tEvtLogData:   0x%04x\n", msg->Data[0]);
399 		break;
400 
401 	case MPI_EVENT_UNIT_ATTENTION:
402 		printf("\tTargetID:     0x%04x\n",
403 			msg->Data[0] & 0xff);
404 		printf("\tBus:          0x%04x\n",
405 			(msg->Data[0] >> 8) & 0xff);
406 		break;
407 
408 	case MPI_EVENT_IOC_BUS_RESET:
409 	case MPI_EVENT_EXT_BUS_RESET:
410 	case MPI_EVENT_RESCAN:
411 		printf("\tPort:           %d\n",
412 			(msg->Data[0] >> 8) & 0xff);
413 		break;
414 
415 	case MPI_EVENT_LINK_STATUS_CHANGE:
416 		printf("\tLinkState:    %d\n",
417 			msg->Data[0] & 0xff);
418 		printf("\tPort:         %d\n",
419 			(msg->Data[1] >> 8) & 0xff);
420 		break;
421 
422 	case MPI_EVENT_LOOP_STATE_CHANGE:
423 		printf("\tType:         %d\n",
424 			(msg->Data[0] >> 16) & 0xff);
425 		printf("\tChar3:      0x%02x\n",
426 			(msg->Data[0] >> 8) & 0xff);
427 		printf("\tChar4:      0x%02x\n",
428 			(msg->Data[0]     ) & 0xff);
429 		printf("\tPort:         %d\n",
430 			(msg->Data[1] >> 8) & 0xff);
431 		break;
432 
433 	case MPI_EVENT_LOGOUT:
434 		printf("\tN_PortId:   0x%04x\n", msg->Data[0]);
435 		printf("\tPort:         %d\n",
436 			(msg->Data[1] >> 8) & 0xff);
437 		break;
438 	}
439 
440 }
441 
442 void
443 mpt_print_reply(void *vmsg)
444 {
445 	MSG_DEFAULT_REPLY *msg = vmsg;
446 
447 	switch (msg->Function) {
448 	case MPI_FUNCTION_EVENT_NOTIFICATION:
449 		mpt_print_event_notice((MSG_EVENT_NOTIFY_REPLY *)msg);
450 		break;
451 	case MPI_FUNCTION_PORT_ENABLE:
452 		mpt_print_enable_reply((MSG_PORT_ENABLE_REPLY *)msg);
453 		break;
454 	case MPI_FUNCTION_IOC_FACTS:
455 		mpt_print_ioc_facts((MSG_IOC_FACTS_REPLY *)msg);
456 		break;
457 	case MPI_FUNCTION_IOC_INIT:
458 		mpt_print_init_reply((MSG_IOC_INIT_REPLY *)msg);
459 		break;
460 	case MPI_FUNCTION_SCSI_IO_REQUEST:
461 		mpt_print_scsi_io_reply((MSG_SCSI_IO_REPLY *)msg);
462 		break;
463 	default:
464 		mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
465 		break;
466 	}
467 }
468 
469 /*****************************************************************************/
470 /*  Request functions                                                        */
471 /*****************************************************************************/
472 static void
473 mpt_print_request_hdr(MSG_REQUEST_HEADER *req)
474 {
475 	printf("%s @ %p\n", mpt_ioc_function(req->Function), req);
476 	printf("\tChain Offset  0x%02x\n", req->ChainOffset);
477 	printf("\tMsgFlags      0x%02x\n", req->MsgFlags);
478 	printf("\tMsgContext    0x%08x\n", req->MsgContext);
479 }
480 
481 void
482 mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *orig_msg)
483 {
484 	MSG_SCSI_IO_REQUEST local, *msg = &local;
485 	int i;
486 
487 	bcopy(orig_msg, msg, sizeof (MSG_SCSI_IO_REQUEST));
488 	mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg);
489 	printf("\tBus:                %d\n", msg->Bus);
490 	printf("\tTargetID            %d\n", msg->TargetID);
491 	printf("\tSenseBufferLength   %d\n", msg->SenseBufferLength);
492 	printf("\tLUN:              0x%0x\n", msg->LUN[1]);
493 	printf("\tControl           0x%08x ", msg->Control);
494 #define MPI_PRINT_FIELD(x)						\
495 	case MPI_SCSIIO_CONTROL_ ## x :					\
496 		printf(" " #x " ");					\
497 		break
498 
499 	switch (msg->Control & MPI_SCSIIO_CONTROL_DATADIRECTION_MASK) {
500 	MPI_PRINT_FIELD(NODATATRANSFER);
501 	MPI_PRINT_FIELD(WRITE);
502 	MPI_PRINT_FIELD(READ);
503 	default:
504 		printf(" Invalid DIR! ");
505 		break;
506 	}
507 	switch (msg->Control & MPI_SCSIIO_CONTROL_TASKATTRIBUTE_MASK) {
508 	MPI_PRINT_FIELD(SIMPLEQ);
509 	MPI_PRINT_FIELD(HEADOFQ);
510 	MPI_PRINT_FIELD(ORDEREDQ);
511 	MPI_PRINT_FIELD(ACAQ);
512 	MPI_PRINT_FIELD(UNTAGGED);
513 	MPI_PRINT_FIELD(NO_DISCONNECT);
514 	default:
515 		printf(" Unknown attribute! ");
516 		break;
517 	}
518 
519 	printf("\n");
520 #undef MPI_PRINT_FIELD
521 
522 	printf("\tDataLength\t0x%08x\n", msg->DataLength);
523 	printf("\tSenseBufAddr\t0x%08x\n", msg->SenseBufferLowAddr);
524 	printf("\tCDB[0:%d]\t", msg->CDBLength);
525 	for (i = 0; i < msg->CDBLength; i++)
526 		printf("%02x ", msg->CDB[i]);
527 	printf("\n");
528 
529 	if ((msg->Control & MPI_SCSIIO_CONTROL_DATADIRECTION_MASK) !=
530 	   MPI_SCSIIO_CONTROL_NODATATRANSFER ) {
531 		mpt_dump_sgl(&orig_msg->SGL,
532 		    ((char *)&orig_msg->SGL)-(char *)orig_msg);
533 	}
534 }
535 
536 static void
537 mpt_print_scsi_tmf_request(MSG_SCSI_TASK_MGMT *msg)
538 {
539 	mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg);
540 	printf("\tLun             0x%02x\n", msg->LUN[1]);
541 	printf("\tTaskType        %s\n", mpt_scsi_tm_type(msg->TaskType));
542 	printf("\tTaskMsgContext  0x%08x\n", msg->TaskMsgContext);
543 }
544 
545 void
546 mpt_print_request(void *vreq)
547 {
548 	MSG_REQUEST_HEADER *req = vreq;
549 
550 	switch (req->Function) {
551 	case MPI_FUNCTION_SCSI_IO_REQUEST:
552 		mpt_print_scsi_io_request((MSG_SCSI_IO_REQUEST *)req);
553 		break;
554 	case MPI_FUNCTION_SCSI_TASK_MGMT:
555 		mpt_print_scsi_tmf_request((MSG_SCSI_TASK_MGMT *)req);
556 	default:
557 		mpt_print_request_hdr(req);
558 		break;
559 	}
560 }
561 
562 int
563 mpt_decode_value(mpt_decode_entry_t *table, u_int num_entries,
564 		 const char *name, u_int value, u_int *cur_column,
565 		 u_int wrap_point)
566 {
567         int     printed;
568         u_int   printed_mask;
569 	u_int	dummy_column;
570 
571 	if (cur_column == NULL) {
572 		dummy_column = 0;
573 		cur_column = &dummy_column;
574 	}
575 
576 	if (*cur_column >= wrap_point) {
577 		printf("\n");
578 		*cur_column = 0;
579 	}
580 	printed = printf("%s[0x%x]", name, value);
581 	if (table == NULL) {
582 		printed += printf(" ");
583 		*cur_column += printed;
584 		return (printed);
585 	}
586 	printed_mask = 0;
587 	while (printed_mask != 0xFF) {
588 		int entry;
589 
590 		for (entry = 0; entry < num_entries; entry++) {
591 			if (((value & table[entry].mask)
592 			  != table[entry].value)
593 			 || ((printed_mask & table[entry].mask)
594 			  == table[entry].mask))
595 				continue;
596 
597 			printed += printf("%s%s",
598 					  printed_mask == 0 ? ":(" : "|",
599 					  table[entry].name);
600 			printed_mask |= table[entry].mask;
601 			break;
602                 }
603 		if (entry >= num_entries)
604 			break;
605         }
606         if (printed_mask != 0)
607 		printed += printf(") ");
608         else
609 		printed += printf(" ");
610 	*cur_column += printed;
611 	return (printed);
612 }
613 
614 static mpt_decode_entry_t req_state_parse_table[] = {
615 	{ "REQ_FREE",		0x00, 0xff },
616 	{ "REQ_ALLOCATED",	0x01, 0x01 },
617 	{ "REQ_QUEUED",		0x02, 0x02 },
618 	{ "REQ_DONE",		0x04, 0x04 },
619 	{ "REQ_TIMEDOUT",	0x08, 0x08 },
620 	{ "REQ_NEED_WAKEUP",	0x10, 0x10 }
621 };
622 
623 void
624 mpt_req_state(mpt_req_state_t state)
625 {
626 	mpt_decode_value(req_state_parse_table,
627 			 NUM_ELEMENTS(req_state_parse_table),
628 			 "REQ_STATE", state, NULL, 80);
629 }
630 
631 #define	LAST_SGE	(		\
632 	MPI_SGE_FLAGS_END_OF_LIST |	\
633 	MPI_SGE_FLAGS_END_OF_BUFFER|	\
634 	MPI_SGE_FLAGS_LAST_ELEMENT)
635 void
636 mpt_dump_sgl(SGE_IO_UNION *su, int offset)
637 {
638 	SGE_SIMPLE32 *se = (SGE_SIMPLE32 *) su;
639 	const char allfox[4] = { 0xff, 0xff, 0xff, 0xff };
640 	void *nxtaddr = se;
641 	void *lim;
642 	int flags;
643 
644 	/*
645 	 * Can't be any bigger than this.
646 	 */
647 	lim = &((char *)se)[MPT_REQUEST_AREA - offset];
648 
649 	do {
650 		int iprt;
651 
652 		printf("\t");
653 		if (memcmp(se, allfox, 4) == 0) {
654 			uint32_t *nxt = (uint32_t *)se;
655 			printf("PAD  %p\n", se);
656 			nxtaddr = nxt + 1;
657 			se = nxtaddr;
658 			flags = 0;
659 			continue;
660 		}
661 		nxtaddr = se + 1;
662 		flags = MPI_SGE_GET_FLAGS(se->FlagsLength);
663 		switch (flags & MPI_SGE_FLAGS_ELEMENT_MASK) {
664 		case MPI_SGE_FLAGS_SIMPLE_ELEMENT:
665 			if (flags & MPI_SGE_FLAGS_64_BIT_ADDRESSING) {
666 				SGE_SIMPLE64 *se64 = (SGE_SIMPLE64 *)se;
667 				printf("SE64 %p: Addr=0x%08x%08x FlagsLength"
668 				    "=0x%0x\n", se64, se64->Address.High,
669 				    se64->Address.Low, se64->FlagsLength);
670 				nxtaddr = se64 + 1;
671 			} else {
672 				printf("SE32 %p: Addr=0x%0x FlagsLength=0x%0x"
673 	                            "\n", se, se->Address, se->FlagsLength);
674 			}
675 			printf(" ");
676 			break;
677 		case MPI_SGE_FLAGS_CHAIN_ELEMENT:
678 			if (flags & MPI_SGE_FLAGS_64_BIT_ADDRESSING) {
679 				SGE_CHAIN64 *ce64 = (SGE_CHAIN64 *) se;
680 				printf("CE64 %p: Addr=0x%08x%08x NxtChnO=0x%x "
681 				    "Flgs=0x%x Len=0x%0x\n", ce64,
682 				    ce64->Address.High, ce64->Address.Low,
683 				    ce64->NextChainOffset,
684 				    ce64->Flags, ce64->Length);
685 				nxtaddr = ce64 + 1;
686 			} else {
687 				SGE_CHAIN32 *ce = (SGE_CHAIN32 *) se;
688 				printf("CE32 %p: Addr=0x%0x NxtChnO=0x%x "
689 				    " Flgs=0x%x Len=0x%0x\n", ce, ce->Address,
690 				    ce->NextChainOffset, ce->Flags, ce->Length);
691 			}
692 			flags = 0;
693 			break;
694 		case MPI_SGE_FLAGS_TRANSACTION_ELEMENT:
695 			printf("TE32 @ %p\n", se);
696 			flags = 0;
697 			break;
698 		}
699 		iprt = 0;
700 #define MPT_PRINT_FLAG(x)						\
701 		if (flags & MPI_SGE_FLAGS_ ## x ) { 			\
702 			if (iprt == 0) {				\
703 				printf("\t");				\
704 			}						\
705 			printf(" ");					\
706 			printf( #x );					\
707 			iprt++;						\
708 		}
709 		MPT_PRINT_FLAG(LOCAL_ADDRESS);
710 		MPT_PRINT_FLAG(HOST_TO_IOC);
711 		MPT_PRINT_FLAG(64_BIT_ADDRESSING);
712 		MPT_PRINT_FLAG(LAST_ELEMENT);
713 		MPT_PRINT_FLAG(END_OF_BUFFER);
714 		MPT_PRINT_FLAG(END_OF_LIST);
715 #undef MPT_PRINT_FLAG
716 		if (iprt)
717 			printf("\n");
718 		se = nxtaddr;
719 		if ((flags & LAST_SGE) == LAST_SGE) {
720 			break;
721 		}
722 	} while ((flags & MPI_SGE_FLAGS_END_OF_LIST) == 0 && nxtaddr < lim);
723 }
724 
725 void
726 mpt_prt(struct mpt_softc *mpt, const char *fmt, ...)
727 {
728 	va_list ap;
729 
730 	printf("%s: ", device_get_nameunit(mpt->dev));
731 	va_start(ap, fmt);
732 	vprintf(fmt, ap);
733 	va_end(ap);
734 }
735 
736 void
737 mpt_prtc(struct mpt_softc *mpt, const char *fmt, ...)
738 {
739 	va_list ap;
740 
741 	va_start(ap, fmt);
742 	vprintf(fmt, ap);
743 	va_end(ap);
744 }
745