xref: /onnv-gate/usr/src/uts/sun4v/io/dr_util.c (revision 4776:727b43e0e700)
11991Sheppo /*
21991Sheppo  * CDDL HEADER START
31991Sheppo  *
41991Sheppo  * The contents of this file are subject to the terms of the
51991Sheppo  * Common Development and Distribution License (the "License").
61991Sheppo  * You may not use this file except in compliance with the License.
71991Sheppo  *
81991Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91991Sheppo  * or http://www.opensolaris.org/os/licensing.
101991Sheppo  * See the License for the specific language governing permissions
111991Sheppo  * and limitations under the License.
121991Sheppo  *
131991Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141991Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151991Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161991Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171991Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181991Sheppo  *
191991Sheppo  * CDDL HEADER END
201991Sheppo  */
211991Sheppo 
221991Sheppo /*
23*4776Sjm22469  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
241991Sheppo  * Use is subject to license terms.
251991Sheppo  */
261991Sheppo 
271991Sheppo #pragma ident	"%Z%%M%	%I%	%E% SMI"
281991Sheppo 
291991Sheppo /*
301991Sheppo  * sun4v DR Utility functions
311991Sheppo  */
321991Sheppo 
331991Sheppo #include <sys/types.h>
341991Sheppo #include <sys/cmn_err.h>
351991Sheppo #include <sys/sunddi.h>
361991Sheppo #include <sys/note.h>
371991Sheppo #include <sys/sysevent.h>
381991Sheppo #include <sys/sysevent/dr.h>
391991Sheppo #include <sys/sysevent/eventdefs.h>
401991Sheppo #include <sys/ldoms.h>
411991Sheppo 
421991Sheppo #include <sys/dr_util.h>
431991Sheppo 
441991Sheppo boolean_t
451991Sheppo dr_is_disabled(dr_type_t type)
461991Sheppo {
471991Sheppo 	/*
481991Sheppo 	 * The type argument is currently unused. However, it
491991Sheppo 	 * keeps the interface flexible enough to allows for
501991Sheppo 	 * only disabling certain types of DR.
511991Sheppo 	 */
521991Sheppo 	_NOTE(ARGUNUSED(type))
531991Sheppo 
541991Sheppo 	/*
551991Sheppo 	 * DR requires that the kernel is using its own CIF
561991Sheppo 	 * handler. If that is not the case, either because
571991Sheppo 	 * domaining has been explicitly disabled, or because
581991Sheppo 	 * the firmware does not support it, the system must
591991Sheppo 	 * remain static and DR must be disabled.
601991Sheppo 	 */
61*4776Sjm22469 	if (!domaining_enabled()) {
621991Sheppo 		cmn_err(CE_NOTE, "!Kernel CIF handler is not enabled, DR "
631991Sheppo 		    "is not available\n");
641991Sheppo 		return (B_TRUE);
651991Sheppo 	}
661991Sheppo 
671991Sheppo 	return (B_FALSE);
681991Sheppo }
691991Sheppo 
701991Sheppo /*
711991Sheppo  * Generate a DR sysevent based on the type of resource and
721991Sheppo  * sysevent hint specified. The hint indicates whether the
731991Sheppo  * resource was added or removed.
741991Sheppo  */
751991Sheppo void
761991Sheppo dr_generate_event(dr_type_t type, int se_hint)
771991Sheppo {
781991Sheppo 	int			rv;
791991Sheppo 	sysevent_id_t		eid;
801991Sheppo 	sysevent_t		*ev = NULL;
811991Sheppo 	sysevent_attr_list_t	*evnt_attr_list = NULL;
821991Sheppo 	sysevent_value_t	evnt_val;
831991Sheppo 	static char		pubname[] = SUNW_KERN_PUB"dr";
841991Sheppo 
851991Sheppo 	DR_DBG_ALL("generate_event: type=%s, hint=%s\n", DR_TYPE2STR(type),
861991Sheppo 	    SE_HINT2STR(se_hint));
871991Sheppo 
881991Sheppo 	/*
891991Sheppo 	 * Add the attachment point attribute
901991Sheppo 	 */
911991Sheppo 	ev = sysevent_alloc(EC_DR, ESC_DR_AP_STATE_CHANGE, pubname, KM_SLEEP);
921991Sheppo 	evnt_val.value_type = SE_DATA_TYPE_STRING;
931991Sheppo 	evnt_val.value.sv_string = DR_TYPE2STR(type);
941991Sheppo 
951991Sheppo 	rv = sysevent_add_attr(&evnt_attr_list, DR_AP_ID, &evnt_val, KM_SLEEP);
961991Sheppo 	if (rv != 0) {
971991Sheppo 		DR_DBG_ALL("generate_event: failed to add attr '%s' for "
981991Sheppo 		    "'%s' event\n", DR_AP_ID, EC_DR);
991991Sheppo 		goto done;
1001991Sheppo 	}
1011991Sheppo 
1021991Sheppo 	/*
1031991Sheppo 	 * Add the DR hint attribute
1041991Sheppo 	 */
1051991Sheppo 	evnt_val.value_type = SE_DATA_TYPE_STRING;
1061991Sheppo 	evnt_val.value.sv_string = SE_HINT2STR(se_hint);
1071991Sheppo 
1081991Sheppo 	rv = sysevent_add_attr(&evnt_attr_list, DR_HINT, &evnt_val, KM_SLEEP);
1091991Sheppo 	if (rv != 0) {
1101991Sheppo 		DR_DBG_ALL("generate_event: failed to add attr '%s' for "
1111991Sheppo 		    "'%s' event\n", DR_HINT, EC_DR);
1121991Sheppo 		sysevent_free_attr(evnt_attr_list);
1131991Sheppo 		goto done;
1141991Sheppo 	}
1151991Sheppo 
1161991Sheppo 	/*
1171991Sheppo 	 * Attach the attribute list to the event
1181991Sheppo 	 */
1191991Sheppo 	rv = sysevent_attach_attributes(ev, evnt_attr_list);
1201991Sheppo 	if (rv != 0) {
1211991Sheppo 		DR_DBG_ALL("generate_event: failed to add attr list for "
1221991Sheppo 		    "'%s' event\n", EC_DR);
1231991Sheppo 		sysevent_free_attr(evnt_attr_list);
1241991Sheppo 		goto done;
1251991Sheppo 	}
1261991Sheppo 
1271991Sheppo 	/*
1281991Sheppo 	 * Log the event
1291991Sheppo 	 */
1301991Sheppo 	rv = log_sysevent(ev, KM_NOSLEEP, &eid);
1311991Sheppo 	if (rv != 0) {
1321991Sheppo 		DR_DBG_ALL("generate_event: failed to log event (%d)\n", rv);
1331991Sheppo 	}
1341991Sheppo 
1351991Sheppo done:
1361991Sheppo 	if (ev != NULL)
1371991Sheppo 		sysevent_free(ev);
1381991Sheppo }
1391991Sheppo 
1401991Sheppo /*
1411991Sheppo  * Debugging Features
1421991Sheppo  */
1431991Sheppo #ifdef DEBUG
1441991Sheppo 
1451991Sheppo uint_t dr_debug = 0x0;
1461991Sheppo 
1471991Sheppo #define	BYTESPERLINE    8
1481991Sheppo #define	LINEWIDTH	((BYTESPERLINE * 3) + (BYTESPERLINE + 2) + 1)
1491991Sheppo #define	ASCIIOFFSET	((BYTESPERLINE * 3) + 2)
1501991Sheppo #define	ISPRINT(c)	((c >= ' ') && (c <= '~'))
1511991Sheppo 
1521991Sheppo /*
1531991Sheppo  * Output a buffer formatted with a set number of bytes on
1541991Sheppo  * each line. Append each line with the ASCII equivalent of
1551991Sheppo  * each byte if it falls within the printable ASCII range,
1561991Sheppo  * and '.' otherwise.
1571991Sheppo  */
1581991Sheppo void
1591991Sheppo dr_dbg_dump_msg(void *buf, size_t len)
1601991Sheppo {
1611991Sheppo 	int	i, j;
1621991Sheppo 	char	*msg = buf;
1631991Sheppo 	char	*curr;
1641991Sheppo 	char	*aoff;
1651991Sheppo 	char	line[LINEWIDTH];
1661991Sheppo 
1671991Sheppo 	/* abort if not debugging transport */
1681991Sheppo 	if (!(dr_debug & DR_DBG_FLAG_TRANS)) {
1691991Sheppo 		return;
1701991Sheppo 	}
1711991Sheppo 
1721991Sheppo 	/* walk the buffer one line at a time */
1731991Sheppo 	for (i = 0; i < len; i += BYTESPERLINE) {
1741991Sheppo 
1751991Sheppo 		bzero(line, LINEWIDTH);
1761991Sheppo 
1771991Sheppo 		curr = line;
1781991Sheppo 		aoff = line + ASCIIOFFSET;
1791991Sheppo 
1801991Sheppo 		/*
1811991Sheppo 		 * Walk the bytes in the current line, storing
1821991Sheppo 		 * the hex value for the byte as well as the
1831991Sheppo 		 * ASCII representation in a temporary buffer.
1841991Sheppo 		 * All ASCII values are placed at the end of
1851991Sheppo 		 * the line.
1861991Sheppo 		 */
1871991Sheppo 		for (j = 0; (j < BYTESPERLINE) && ((i + j) < len); j++) {
1881991Sheppo 			(void) sprintf(curr, " %02x", msg[i + j]);
1891991Sheppo 			*aoff = (ISPRINT(msg[i + j])) ? msg[i + j] : '.';
1901991Sheppo 			curr += 3;
1911991Sheppo 			aoff++;
1921991Sheppo 		}
1931991Sheppo 
1941991Sheppo 		/*
1951991Sheppo 		 * Fill in to the start of the ASCII translation
1961991Sheppo 		 * with spaces. This will only be necessary if
1971991Sheppo 		 * this is the last line and there are not enough
1981991Sheppo 		 * bytes to fill the whole line.
1991991Sheppo 		 */
2001991Sheppo 		while (curr != (line + ASCIIOFFSET))
2011991Sheppo 			*curr++ = ' ';
2021991Sheppo 
2031991Sheppo 		DR_DBG_TRANS("%s\n", line);
2041991Sheppo 	}
2051991Sheppo }
2061991Sheppo #endif /* DEBUG */
207