xref: /freebsd-13.1/sys/dev/mpt/mpt_debug.c (revision a3116b5a)
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 
254 static char *
255 mpt_ioc_event(int code)
256 {
257 	const struct Error_Map *status = IOC_Event;
258 	static char buf[64];
259 	while (status->Error_Code >= 0) {
260 		if (status->Error_Code == code)
261 			return status->Error_String;
262 		status++;
263 	}
264 	snprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
265 	return buf;
266 }
267 
268 static char *
269 mpt_scsi_state(int code)
270 {
271 	const struct Error_Map *status = IOC_SCSIState;
272 	static char buf[128];
273 	char *ptr = buf;
274 	char *end = &buf[128];
275 	buf[0] = '\0';
276 	ptr += snprintf(buf, sizeof buf, "(0x%08x)", code);
277 	while (status->Error_Code >= 0) {
278 		if ((status->Error_Code & code) != 0)
279 			ptr += snprintf(ptr, (size_t)(end-ptr), "%s ",
280 				status->Error_String);
281 		status++;
282 	}
283 	return buf;
284 }
285 static char *
286 mpt_scsi_status(int code)
287 {
288 	const struct Error_Map *status = IOC_SCSIStatus;
289 	static char buf[64];
290 	while (status->Error_Code >= 0) {
291 		if (status->Error_Code == code)
292 			return status->Error_String;
293 		status++;
294 	}
295 	snprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
296 	return buf;
297 }
298 static char *
299 mpt_who(int who_init)
300 {
301 	char *who;
302 
303 	switch (who_init) {
304 	case MPT_DB_INIT_NOONE:       who = "No One";        break;
305 	case MPT_DB_INIT_BIOS:        who = "BIOS";          break;
306 	case MPT_DB_INIT_ROMBIOS:     who = "ROM BIOS";      break;
307 	case MPT_DB_INIT_PCIPEER:     who = "PCI Peer";      break;
308 	case MPT_DB_INIT_HOST:        who = "Host Driver";   break;
309 	case MPT_DB_INIT_MANUFACTURE: who = "Manufacturing"; break;
310 	default:                      who = "Unknown";       break;
311 	}
312 	return who;
313 }
314 
315 static char *
316 mpt_state(u_int32_t mb)
317 {
318 	char *text;
319 
320 	switch (MPT_STATE(mb)) {
321 		case MPT_DB_STATE_RESET:  text = "Reset";   break;
322 		case MPT_DB_STATE_READY:  text = "Ready";   break;
323 		case MPT_DB_STATE_RUNNING:text = "Running"; break;
324 		case MPT_DB_STATE_FAULT:  text = "Fault";   break;
325 		default: 		  text = "Unknown"; break;
326 	}
327 	return text;
328 }
329 
330 static char *
331 mpt_scsi_tm_type(int code)
332 {
333 	const struct Error_Map *status = IOC_SCSITMType;
334 	static char buf[64];
335 	while (status->Error_Code >= 0) {
336 		if (status->Error_Code == code)
337 			return status->Error_String;
338 		status++;
339 	}
340 	snprintf(buf, sizeof buf, "Unknown (0x%08x)", code);
341 	return buf;
342 }
343 
344 void
345 mpt_print_db(u_int32_t mb)
346 {
347 	printf("mpt mailbox: (0x%x) State %s  WhoInit %s\n",
348 	    mb, mpt_state(mb), mpt_who(MPT_WHO(mb)));
349 }
350 
351 /*****************************************************************************/
352 /*  Reply functions                                                          */
353 /*****************************************************************************/
354 static void
355 mpt_print_reply_hdr(MSG_DEFAULT_REPLY *msg)
356 {
357 	printf("%s Reply @ %p\n", mpt_ioc_function(msg->Function), msg);
358 	printf("\tIOC Status    %s\n", mpt_ioc_status(msg->IOCStatus));
359 	printf("\tIOCLogInfo    0x%08x\n", msg->IOCLogInfo);
360 	printf("\tMsgLength     0x%02x\n", msg->MsgLength);
361 	printf("\tMsgFlags      0x%02x\n", msg->MsgFlags);
362 	printf("\tMsgContext    0x%08x\n", msg->MsgContext);
363 }
364 
365 static void
366 mpt_print_init_reply(MSG_IOC_INIT_REPLY *msg)
367 {
368 	mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
369 	printf("\tWhoInit       %s\n", mpt_who(msg->WhoInit));
370 	printf("\tMaxDevices    0x%02x\n", msg->MaxDevices);
371 	printf("\tMaxBuses     0x%02x\n", msg->MaxBuses);
372 }
373 
374 static void
375 mpt_print_ioc_facts(MSG_IOC_FACTS_REPLY *msg)
376 {
377 	mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
378 	printf("\tIOCNumber     %d\n",		msg->IOCNumber);
379 	printf("\tMaxChainDepth %d\n",		msg->MaxChainDepth);
380 	printf("\tWhoInit       %s\n",		mpt_who(msg->WhoInit));
381 	printf("\tBlockSize     %d\n",		msg->BlockSize);
382 	printf("\tFlags         %d\n",		msg->Flags);
383 	printf("\tReplyQueueDepth %d\n",	msg->ReplyQueueDepth);
384 	printf("\tReqFrameSize  0x%04x\n",	msg->RequestFrameSize);
385 	printf("\tFW Version    0x%08x\n",	msg->FWVersion.Word);
386 	printf("\tProduct ID    0x%04x\n",	msg->ProductID);
387 	printf("\tCredits       0x%04x\n",	msg->GlobalCredits);
388 	printf("\tPorts         %d\n",		msg->NumberOfPorts);
389 	printf("\tEventState    0x%02x\n",	msg->EventState);
390 	printf("\tHostMFA_HA    0x%08x\n",	msg->CurrentHostMfaHighAddr);
391 	printf("\tSenseBuf_HA   0x%08x\n",
392 	    msg->CurrentSenseBufferHighAddr);
393 	printf("\tRepFrameSize  0x%04x\n",	msg->CurReplyFrameSize);
394 	printf("\tMaxDevices    0x%02x\n",	msg->MaxDevices);
395 	printf("\tMaxBuses      0x%02x\n",	msg->MaxBuses);
396 	printf("\tFWImageSize   0x%04x\n",	msg->FWImageSize);
397 }
398 
399 static void
400 mpt_print_enable_reply(MSG_PORT_ENABLE_REPLY *msg)
401 {
402 	mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
403 	printf("\tPort:         %d\n", msg->PortNumber);
404 }
405 
406 static void
407 mpt_print_scsi_io_reply(MSG_SCSI_IO_REPLY *msg)
408 {
409 	mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
410 	printf("\tBus:          %d\n", msg->Bus);
411 	printf("\tTargetID      %d\n", msg->TargetID);
412 	printf("\tCDBLength     %d\n", msg->CDBLength);
413 	printf("\tSCSI Status:  %s\n", mpt_scsi_status(msg->SCSIStatus));
414 	printf("\tSCSI State:   %s\n", mpt_scsi_state(msg->SCSIState));
415 	printf("\tTransferCnt   0x%04x\n", msg->TransferCount);
416 	printf("\tSenseCnt      0x%04x\n", msg->SenseCount);
417 	printf("\tResponseInfo  0x%08x\n", msg->ResponseInfo);
418 }
419 
420 
421 
422 static void
423 mpt_print_event_notice(MSG_EVENT_NOTIFY_REPLY *msg)
424 {
425 	mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
426 	printf("\tEvent:        %s\n", mpt_ioc_event(msg->Event));
427 	printf("\tEventContext  0x%04x\n", msg->EventContext);
428 	printf("\tAckRequired     %d\n", msg->AckRequired);
429 	printf("\tEventDataLength %d\n", msg->EventDataLength);
430 	printf("\tContinuation    %d\n", msg->MsgFlags & 0x80);
431 	switch(msg->Event) {
432 	case MPI_EVENT_LOG_DATA:
433 		printf("\tEvtLogData:   0x%04x\n", msg->Data[0]);
434 		break;
435 
436 	case MPI_EVENT_UNIT_ATTENTION:
437 		printf("\tTargetID:     0x%04x\n",
438 			msg->Data[0] & 0xff);
439 		printf("\tBus:          0x%04x\n",
440 			(msg->Data[0] >> 8) & 0xff);
441 		break;
442 
443 	case MPI_EVENT_IOC_BUS_RESET:
444 	case MPI_EVENT_EXT_BUS_RESET:
445 	case MPI_EVENT_RESCAN:
446 		printf("\tPort:           %d\n",
447 			(msg->Data[0] >> 8) & 0xff);
448 		break;
449 
450 	case MPI_EVENT_LINK_STATUS_CHANGE:
451 		printf("\tLinkState:    %d\n",
452 			msg->Data[0] & 0xff);
453 		printf("\tPort:         %d\n",
454 			(msg->Data[1] >> 8) & 0xff);
455 		break;
456 
457 	case MPI_EVENT_LOOP_STATE_CHANGE:
458 		printf("\tType:         %d\n",
459 			(msg->Data[0] >> 16) & 0xff);
460 		printf("\tChar3:      0x%02x\n",
461 			(msg->Data[0] >> 8) & 0xff);
462 		printf("\tChar4:      0x%02x\n",
463 			(msg->Data[0]     ) & 0xff);
464 		printf("\tPort:         %d\n",
465 			(msg->Data[1] >> 8) & 0xff);
466 		break;
467 
468 	case MPI_EVENT_LOGOUT:
469 		printf("\tN_PortId:   0x%04x\n", msg->Data[0]);
470 		printf("\tPort:         %d\n",
471 			(msg->Data[1] >> 8) & 0xff);
472 		break;
473 	}
474 
475 }
476 
477 void
478 mpt_print_reply(void *vmsg)
479 {
480 	MSG_DEFAULT_REPLY *msg = vmsg;
481 
482 	switch (msg->Function) {
483 	case MPI_FUNCTION_EVENT_NOTIFICATION:
484 		mpt_print_event_notice((MSG_EVENT_NOTIFY_REPLY *)msg);
485 		break;
486 	case MPI_FUNCTION_PORT_ENABLE:
487 		mpt_print_enable_reply((MSG_PORT_ENABLE_REPLY *)msg);
488 		break;
489 	case MPI_FUNCTION_IOC_FACTS:
490 		mpt_print_ioc_facts((MSG_IOC_FACTS_REPLY *)msg);
491 		break;
492 	case MPI_FUNCTION_IOC_INIT:
493 		mpt_print_init_reply((MSG_IOC_INIT_REPLY *)msg);
494 		break;
495 	case MPI_FUNCTION_SCSI_IO_REQUEST:
496 	case MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
497 		mpt_print_scsi_io_reply((MSG_SCSI_IO_REPLY *)msg);
498 		break;
499 	default:
500 		mpt_print_reply_hdr((MSG_DEFAULT_REPLY *)msg);
501 		break;
502 	}
503 }
504 
505 /*****************************************************************************/
506 /*  Request functions                                                        */
507 /*****************************************************************************/
508 static void
509 mpt_print_request_hdr(MSG_REQUEST_HEADER *req)
510 {
511 	printf("%s @ %p\n", mpt_ioc_function(req->Function), req);
512 	printf("\tChain Offset  0x%02x\n", req->ChainOffset);
513 	printf("\tMsgFlags      0x%02x\n", req->MsgFlags);
514 	printf("\tMsgContext    0x%08x\n", req->MsgContext);
515 }
516 
517 void
518 mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *orig_msg)
519 {
520 	MSG_SCSI_IO_REQUEST local, *msg = &local;
521 	int i;
522 
523 	bcopy(orig_msg, msg, sizeof (MSG_SCSI_IO_REQUEST));
524 	mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg);
525 	printf("\tBus:                %d\n", msg->Bus);
526 	printf("\tTargetID            %d\n", msg->TargetID);
527 	printf("\tSenseBufferLength   %d\n", msg->SenseBufferLength);
528 	printf("\tLUN:              0x%0x\n", msg->LUN[1]);
529 	printf("\tControl           0x%08x ", msg->Control);
530 #define MPI_PRINT_FIELD(x)						\
531 	case MPI_SCSIIO_CONTROL_ ## x :					\
532 		printf(" " #x " ");					\
533 		break
534 
535 	switch (msg->Control & MPI_SCSIIO_CONTROL_DATADIRECTION_MASK) {
536 	MPI_PRINT_FIELD(NODATATRANSFER);
537 	MPI_PRINT_FIELD(WRITE);
538 	MPI_PRINT_FIELD(READ);
539 	default:
540 		printf(" Invalid DIR! ");
541 		break;
542 	}
543 	switch (msg->Control & MPI_SCSIIO_CONTROL_TASKATTRIBUTE_MASK) {
544 	MPI_PRINT_FIELD(SIMPLEQ);
545 	MPI_PRINT_FIELD(HEADOFQ);
546 	MPI_PRINT_FIELD(ORDEREDQ);
547 	MPI_PRINT_FIELD(ACAQ);
548 	MPI_PRINT_FIELD(UNTAGGED);
549 	MPI_PRINT_FIELD(NO_DISCONNECT);
550 	default:
551 		printf(" Unknown attribute! ");
552 		break;
553 	}
554 
555 	printf("\n");
556 #undef MPI_PRINT_FIELD
557 
558 	printf("\tDataLength\t0x%08x\n", msg->DataLength);
559 	printf("\tSenseBufAddr\t0x%08x\n", msg->SenseBufferLowAddr);
560 	printf("\tCDB[0:%d]\t", msg->CDBLength);
561 	for (i = 0; i < msg->CDBLength; i++)
562 		printf("%02x ", msg->CDB[i]);
563 	printf("\n");
564 
565 	if ((msg->Control & MPI_SCSIIO_CONTROL_DATADIRECTION_MASK) !=
566 	   MPI_SCSIIO_CONTROL_NODATATRANSFER ) {
567 		mpt_dump_sgl(&orig_msg->SGL,
568 		    ((char *)&orig_msg->SGL)-(char *)orig_msg);
569 	}
570 }
571 
572 static void
573 mpt_print_scsi_tmf_request(MSG_SCSI_TASK_MGMT *msg)
574 {
575 	mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg);
576 	printf("\tLun             0x%02x\n", msg->LUN[1]);
577 	printf("\tTaskType        %s\n", mpt_scsi_tm_type(msg->TaskType));
578 	printf("\tTaskMsgContext  0x%08x\n", msg->TaskMsgContext);
579 }
580 
581 
582 static void
583 mpt_print_scsi_target_assist_request(PTR_MSG_TARGET_ASSIST_REQUEST msg)
584 {
585 	mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg);
586 	printf("\tStatusCode    0x%02x\n", msg->StatusCode);
587 	printf("\tTargetAssist  0x%02x\n", msg->TargetAssistFlags);
588 	printf("\tQueueTag      0x%04x\n", msg->QueueTag);
589 	printf("\tReplyWord     0x%08x\n", msg->ReplyWord);
590 	printf("\tLun           0x%02x\n", msg->LUN[1]);
591 	printf("\tRelativeOff   0x%08x\n", msg->RelativeOffset);
592 	printf("\tDataLength    0x%08x\n", msg->DataLength);
593 	mpt_dump_sgl(msg->SGL, 0);
594 }
595 
596 static void
597 mpt_print_scsi_target_status_send_request(MSG_TARGET_STATUS_SEND_REQUEST *msg)
598 {
599 	SGE_IO_UNION x;
600 	mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg);
601 	printf("\tStatusCode    0x%02x\n", msg->StatusCode);
602 	printf("\tStatusFlags   0x%02x\n", msg->StatusFlags);
603 	printf("\tQueueTag      0x%04x\n", msg->QueueTag);
604 	printf("\tReplyWord     0x%08x\n", msg->ReplyWord);
605 	printf("\tLun           0x%02x\n", msg->LUN[1]);
606 	x.u.Simple = msg->StatusDataSGE;
607 	mpt_dump_sgl(&x, 0);
608 }
609 
610 void
611 mpt_print_request(void *vreq)
612 {
613 	MSG_REQUEST_HEADER *req = vreq;
614 
615 	switch (req->Function) {
616 	case MPI_FUNCTION_SCSI_IO_REQUEST:
617 	case MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
618 		mpt_print_scsi_io_request((MSG_SCSI_IO_REQUEST *)req);
619 		break;
620 	case MPI_FUNCTION_SCSI_TASK_MGMT:
621 		mpt_print_scsi_tmf_request((MSG_SCSI_TASK_MGMT *)req);
622 		break;
623 	case MPI_FUNCTION_TARGET_ASSIST:
624 		mpt_print_scsi_target_assist_request(
625 		    (PTR_MSG_TARGET_ASSIST_REQUEST)req);
626 		break;
627 	case MPI_FUNCTION_TARGET_STATUS_SEND:
628 		mpt_print_scsi_target_status_send_request(
629 		    (MSG_TARGET_STATUS_SEND_REQUEST *)req);
630 		break;
631 	default:
632 		mpt_print_request_hdr(req);
633 		break;
634 	}
635 }
636 
637 int
638 mpt_decode_value(mpt_decode_entry_t *table, u_int num_entries,
639 		 const char *name, u_int value, u_int *cur_column,
640 		 u_int wrap_point)
641 {
642         int     printed;
643         u_int   printed_mask;
644 	u_int	dummy_column;
645 
646 	if (cur_column == NULL) {
647 		dummy_column = 0;
648 		cur_column = &dummy_column;
649 	}
650 
651 	if (*cur_column >= wrap_point) {
652 		printf("\n");
653 		*cur_column = 0;
654 	}
655 	printed = printf("%s[0x%x]", name, value);
656 	if (table == NULL) {
657 		printed += printf(" ");
658 		*cur_column += printed;
659 		return (printed);
660 	}
661 	printed_mask = 0;
662 	while (printed_mask != 0xFF) {
663 		int entry;
664 
665 		for (entry = 0; entry < num_entries; entry++) {
666 			if (((value & table[entry].mask)
667 			  != table[entry].value)
668 			 || ((printed_mask & table[entry].mask)
669 			  == table[entry].mask))
670 				continue;
671 
672 			printed += printf("%s%s",
673 					  printed_mask == 0 ? ":(" : "|",
674 					  table[entry].name);
675 			printed_mask |= table[entry].mask;
676 			break;
677                 }
678 		if (entry >= num_entries)
679 			break;
680         }
681         if (printed_mask != 0)
682 		printed += printf(") ");
683         else
684 		printed += printf(" ");
685 	*cur_column += printed;
686 	return (printed);
687 }
688 
689 static mpt_decode_entry_t req_state_parse_table[] = {
690 	{ "REQ_FREE",		0x00, 0xff },
691 	{ "REQ_ALLOCATED",	0x01, 0x01 },
692 	{ "REQ_QUEUED",		0x02, 0x02 },
693 	{ "REQ_DONE",		0x04, 0x04 },
694 	{ "REQ_TIMEDOUT",	0x08, 0x08 },
695 	{ "REQ_NEED_WAKEUP",	0x10, 0x10 }
696 };
697 
698 void
699 mpt_req_state(mpt_req_state_t state)
700 {
701 	mpt_decode_value(req_state_parse_table,
702 			 NUM_ELEMENTS(req_state_parse_table),
703 			 "REQ_STATE", state, NULL, 80);
704 }
705 
706 #define	LAST_SGE	(		\
707 	MPI_SGE_FLAGS_END_OF_LIST |	\
708 	MPI_SGE_FLAGS_END_OF_BUFFER|	\
709 	MPI_SGE_FLAGS_LAST_ELEMENT)
710 void
711 mpt_dump_sgl(SGE_IO_UNION *su, int offset)
712 {
713 	SGE_SIMPLE32 *se = (SGE_SIMPLE32 *) su;
714 	const char allfox[4] = { 0xff, 0xff, 0xff, 0xff };
715 	void *nxtaddr = se;
716 	void *lim;
717 	int flags;
718 
719 	/*
720 	 * Can't be any bigger than this.
721 	 */
722 	lim = &((char *)se)[MPT_REQUEST_AREA - offset];
723 
724 	do {
725 		int iprt;
726 
727 		printf("\t");
728 		if (memcmp(se, allfox, 4) == 0) {
729 			uint32_t *nxt = (uint32_t *)se;
730 			printf("PAD  %p\n", se);
731 			nxtaddr = nxt + 1;
732 			se = nxtaddr;
733 			flags = 0;
734 			continue;
735 		}
736 		nxtaddr = se + 1;
737 		flags = MPI_SGE_GET_FLAGS(se->FlagsLength);
738 		switch (flags & MPI_SGE_FLAGS_ELEMENT_MASK) {
739 		case MPI_SGE_FLAGS_SIMPLE_ELEMENT:
740 			if (flags & MPI_SGE_FLAGS_64_BIT_ADDRESSING) {
741 				SGE_SIMPLE64 *se64 = (SGE_SIMPLE64 *)se;
742 				printf("SE64 %p: Addr=0x%08x%08x FlagsLength"
743 				    "=0x%0x\n", se64, se64->Address.High,
744 				    se64->Address.Low, se64->FlagsLength);
745 				nxtaddr = se64 + 1;
746 			} else {
747 				printf("SE32 %p: Addr=0x%0x FlagsLength=0x%0x"
748 	                            "\n", se, se->Address, se->FlagsLength);
749 			}
750 			printf(" ");
751 			break;
752 		case MPI_SGE_FLAGS_CHAIN_ELEMENT:
753 			if (flags & MPI_SGE_FLAGS_64_BIT_ADDRESSING) {
754 				SGE_CHAIN64 *ce64 = (SGE_CHAIN64 *) se;
755 				printf("CE64 %p: Addr=0x%08x%08x NxtChnO=0x%x "
756 				    "Flgs=0x%x Len=0x%0x\n", ce64,
757 				    ce64->Address.High, ce64->Address.Low,
758 				    ce64->NextChainOffset,
759 				    ce64->Flags, ce64->Length);
760 				nxtaddr = ce64 + 1;
761 			} else {
762 				SGE_CHAIN32 *ce = (SGE_CHAIN32 *) se;
763 				printf("CE32 %p: Addr=0x%0x NxtChnO=0x%x "
764 				    " Flgs=0x%x Len=0x%0x\n", ce, ce->Address,
765 				    ce->NextChainOffset, ce->Flags, ce->Length);
766 			}
767 			flags = 0;
768 			break;
769 		case MPI_SGE_FLAGS_TRANSACTION_ELEMENT:
770 			printf("TE32 @ %p\n", se);
771 			flags = 0;
772 			break;
773 		}
774 		iprt = 0;
775 #define MPT_PRINT_FLAG(x)						\
776 		if (flags & MPI_SGE_FLAGS_ ## x ) { 			\
777 			if (iprt == 0) {				\
778 				printf("\t");				\
779 			}						\
780 			printf(" ");					\
781 			printf( #x );					\
782 			iprt++;						\
783 		}
784 		MPT_PRINT_FLAG(LOCAL_ADDRESS);
785 		MPT_PRINT_FLAG(HOST_TO_IOC);
786 		MPT_PRINT_FLAG(64_BIT_ADDRESSING);
787 		MPT_PRINT_FLAG(LAST_ELEMENT);
788 		MPT_PRINT_FLAG(END_OF_BUFFER);
789 		MPT_PRINT_FLAG(END_OF_LIST);
790 #undef MPT_PRINT_FLAG
791 		if (iprt)
792 			printf("\n");
793 		se = nxtaddr;
794 		if ((flags & LAST_SGE) == LAST_SGE) {
795 			break;
796 		}
797 	} while ((flags & MPI_SGE_FLAGS_END_OF_LIST) == 0 && nxtaddr < lim);
798 }
799 
800 void
801 mpt_dump_request(struct mpt_softc *mpt, request_t *req)
802 {
803         uint32_t *pReq = req->req_vbuf;
804 	int offset;
805 #if __FreeBSD_version >= 500000
806 	mpt_prt(mpt, "Send Request %d (%jx):",
807 	    req->index, (uintmax_t) req->req_pbuf);
808 #else
809 	mpt_prt(mpt, "Send Request %d (%llx):",
810 	    req->index, (unsigned long long) req->req_pbuf);
811 #endif
812 	for (offset = 0; offset < mpt->request_frame_size; offset++) {
813 		if ((offset & 0x7) == 0) {
814 			mpt_prtc(mpt, "\n");
815 			mpt_prt(mpt, " ");
816 		}
817 		mpt_prtc(mpt, " %08x", pReq[offset]);
818 	}
819 	mpt_prtc(mpt, "\n");
820 }
821 
822 #if __FreeBSD_version < 500000
823 void
824 mpt_lprt(struct mpt_softc *mpt, int level, const char *fmt, ...)
825 {
826 	va_list ap;
827         if (level <= mpt->verbose) {
828 		printf("%s: ", device_get_nameunit(mpt->dev));
829 		va_start(ap, fmt);
830 		vprintf(fmt, ap);
831 		va_end(ap);
832 	}
833 }
834 
835 void
836 mpt_lprtc(struct mpt_softc *mpt, int level, const char *fmt, ...)
837 {
838 	va_list ap;
839         if (level <= mpt->verbose) {
840 		va_start(ap, fmt);
841 		vprintf(fmt, ap);
842 		va_end(ap);
843 	}
844 }
845 #endif
846 
847 void
848 mpt_prt(struct mpt_softc *mpt, const char *fmt, ...)
849 {
850 	va_list ap;
851 
852 	printf("%s: ", device_get_nameunit(mpt->dev));
853 	va_start(ap, fmt);
854 	vprintf(fmt, ap);
855 	va_end(ap);
856 }
857 
858 void
859 mpt_prtc(struct mpt_softc *mpt, const char *fmt, ...)
860 {
861 	va_list ap;
862 
863 	va_start(ap, fmt);
864 	vprintf(fmt, ap);
865 	va_end(ap);
866 }
867