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