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