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*12037SSean.McEnroe@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
241991Sheppo * Use is subject to license terms.
251991Sheppo */
261991Sheppo
271991Sheppo /*
281991Sheppo * sun4v DR Utility functions
291991Sheppo */
301991Sheppo
311991Sheppo #include <sys/types.h>
321991Sheppo #include <sys/cmn_err.h>
331991Sheppo #include <sys/sunddi.h>
341991Sheppo #include <sys/note.h>
351991Sheppo #include <sys/sysevent.h>
361991Sheppo #include <sys/sysevent/dr.h>
371991Sheppo #include <sys/sysevent/eventdefs.h>
381991Sheppo #include <sys/ldoms.h>
39*12037SSean.McEnroe@Sun.COM #include <sys/memlist.h>
401991Sheppo #include <sys/dr_util.h>
411991Sheppo
4210106SJason.Beloro@Sun.COM extern int ppvm_enable;
4310106SJason.Beloro@Sun.COM
441991Sheppo boolean_t
dr_is_disabled(dr_type_t type)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 */
614776Sjm22469 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
6710106SJason.Beloro@Sun.COM if (type == DR_TYPE_MEM && ppvm_enable == 0) {
6810106SJason.Beloro@Sun.COM cmn_err(CE_NOTE, "!Memory DR is disabled\n");
6910106SJason.Beloro@Sun.COM return (B_TRUE);
7010106SJason.Beloro@Sun.COM }
7110106SJason.Beloro@Sun.COM
721991Sheppo return (B_FALSE);
731991Sheppo }
741991Sheppo
751991Sheppo /*
761991Sheppo * Generate a DR sysevent based on the type of resource and
771991Sheppo * sysevent hint specified. The hint indicates whether the
781991Sheppo * resource was added or removed.
791991Sheppo */
801991Sheppo void
dr_generate_event(dr_type_t type,int se_hint)811991Sheppo dr_generate_event(dr_type_t type, int se_hint)
821991Sheppo {
831991Sheppo int rv;
841991Sheppo sysevent_id_t eid;
851991Sheppo sysevent_t *ev = NULL;
861991Sheppo sysevent_attr_list_t *evnt_attr_list = NULL;
871991Sheppo sysevent_value_t evnt_val;
881991Sheppo static char pubname[] = SUNW_KERN_PUB"dr";
891991Sheppo
901991Sheppo DR_DBG_ALL("generate_event: type=%s, hint=%s\n", DR_TYPE2STR(type),
911991Sheppo SE_HINT2STR(se_hint));
921991Sheppo
931991Sheppo /*
941991Sheppo * Add the attachment point attribute
951991Sheppo */
961991Sheppo ev = sysevent_alloc(EC_DR, ESC_DR_AP_STATE_CHANGE, pubname, KM_SLEEP);
971991Sheppo evnt_val.value_type = SE_DATA_TYPE_STRING;
981991Sheppo evnt_val.value.sv_string = DR_TYPE2STR(type);
991991Sheppo
1001991Sheppo rv = sysevent_add_attr(&evnt_attr_list, DR_AP_ID, &evnt_val, KM_SLEEP);
1011991Sheppo if (rv != 0) {
1021991Sheppo DR_DBG_ALL("generate_event: failed to add attr '%s' for "
1031991Sheppo "'%s' event\n", DR_AP_ID, EC_DR);
1041991Sheppo goto done;
1051991Sheppo }
1061991Sheppo
1071991Sheppo /*
1081991Sheppo * Add the DR hint attribute
1091991Sheppo */
1101991Sheppo evnt_val.value_type = SE_DATA_TYPE_STRING;
1111991Sheppo evnt_val.value.sv_string = SE_HINT2STR(se_hint);
1121991Sheppo
1131991Sheppo rv = sysevent_add_attr(&evnt_attr_list, DR_HINT, &evnt_val, KM_SLEEP);
1141991Sheppo if (rv != 0) {
1151991Sheppo DR_DBG_ALL("generate_event: failed to add attr '%s' for "
1161991Sheppo "'%s' event\n", DR_HINT, EC_DR);
1171991Sheppo sysevent_free_attr(evnt_attr_list);
1181991Sheppo goto done;
1191991Sheppo }
1201991Sheppo
1211991Sheppo /*
1221991Sheppo * Attach the attribute list to the event
1231991Sheppo */
1241991Sheppo rv = sysevent_attach_attributes(ev, evnt_attr_list);
1251991Sheppo if (rv != 0) {
1261991Sheppo DR_DBG_ALL("generate_event: failed to add attr list for "
1271991Sheppo "'%s' event\n", EC_DR);
1281991Sheppo sysevent_free_attr(evnt_attr_list);
1291991Sheppo goto done;
1301991Sheppo }
1311991Sheppo
1321991Sheppo /*
1331991Sheppo * Log the event
1341991Sheppo */
1351991Sheppo rv = log_sysevent(ev, KM_NOSLEEP, &eid);
1361991Sheppo if (rv != 0) {
1371991Sheppo DR_DBG_ALL("generate_event: failed to log event (%d)\n", rv);
1381991Sheppo }
1391991Sheppo
1401991Sheppo done:
1411991Sheppo if (ev != NULL)
1421991Sheppo sysevent_free(ev);
1431991Sheppo }
1441991Sheppo
145*12037SSean.McEnroe@Sun.COM struct memlist *
dr_memlist_dup(struct memlist * mlist)146*12037SSean.McEnroe@Sun.COM dr_memlist_dup(struct memlist *mlist)
147*12037SSean.McEnroe@Sun.COM {
148*12037SSean.McEnroe@Sun.COM struct memlist *hl = NULL, *tl, **mlp;
149*12037SSean.McEnroe@Sun.COM
150*12037SSean.McEnroe@Sun.COM if (mlist == NULL)
151*12037SSean.McEnroe@Sun.COM return (NULL);
152*12037SSean.McEnroe@Sun.COM
153*12037SSean.McEnroe@Sun.COM mlp = &hl;
154*12037SSean.McEnroe@Sun.COM tl = *mlp;
155*12037SSean.McEnroe@Sun.COM for (; mlist; mlist = mlist->ml_next) {
156*12037SSean.McEnroe@Sun.COM *mlp = (struct memlist *)kmem_zalloc(sizeof (struct memlist),\
157*12037SSean.McEnroe@Sun.COM KM_SLEEP);
158*12037SSean.McEnroe@Sun.COM (*mlp)->ml_address = mlist->ml_address;
159*12037SSean.McEnroe@Sun.COM (*mlp)->ml_size = mlist->ml_size;
160*12037SSean.McEnroe@Sun.COM (*mlp)->ml_prev = tl;
161*12037SSean.McEnroe@Sun.COM tl = *mlp;
162*12037SSean.McEnroe@Sun.COM mlp = &((*mlp)->ml_next);
163*12037SSean.McEnroe@Sun.COM }
164*12037SSean.McEnroe@Sun.COM *mlp = NULL;
165*12037SSean.McEnroe@Sun.COM
166*12037SSean.McEnroe@Sun.COM return (hl);
167*12037SSean.McEnroe@Sun.COM }
168*12037SSean.McEnroe@Sun.COM
169*12037SSean.McEnroe@Sun.COM /*
170*12037SSean.McEnroe@Sun.COM * Free a memlist and its elements
171*12037SSean.McEnroe@Sun.COM */
172*12037SSean.McEnroe@Sun.COM void
dr_memlist_delete(struct memlist * mlist)173*12037SSean.McEnroe@Sun.COM dr_memlist_delete(struct memlist *mlist)
174*12037SSean.McEnroe@Sun.COM {
175*12037SSean.McEnroe@Sun.COM register struct memlist *ml;
176*12037SSean.McEnroe@Sun.COM
177*12037SSean.McEnroe@Sun.COM for (ml = mlist; ml; ml = mlist) {
178*12037SSean.McEnroe@Sun.COM mlist = ml->ml_next;
179*12037SSean.McEnroe@Sun.COM kmem_free((void *)ml, sizeof (struct memlist));
180*12037SSean.McEnroe@Sun.COM }
181*12037SSean.McEnroe@Sun.COM }
182*12037SSean.McEnroe@Sun.COM
1831991Sheppo /*
1841991Sheppo * Debugging Features
1851991Sheppo */
1861991Sheppo #ifdef DEBUG
1871991Sheppo
1881991Sheppo uint_t dr_debug = 0x0;
1891991Sheppo
1901991Sheppo #define BYTESPERLINE 8
1911991Sheppo #define LINEWIDTH ((BYTESPERLINE * 3) + (BYTESPERLINE + 2) + 1)
1921991Sheppo #define ASCIIOFFSET ((BYTESPERLINE * 3) + 2)
1931991Sheppo #define ISPRINT(c) ((c >= ' ') && (c <= '~'))
1941991Sheppo
1951991Sheppo /*
1961991Sheppo * Output a buffer formatted with a set number of bytes on
1971991Sheppo * each line. Append each line with the ASCII equivalent of
1981991Sheppo * each byte if it falls within the printable ASCII range,
1991991Sheppo * and '.' otherwise.
2001991Sheppo */
2011991Sheppo void
dr_dbg_dump_msg(void * buf,size_t len)2021991Sheppo dr_dbg_dump_msg(void *buf, size_t len)
2031991Sheppo {
2041991Sheppo int i, j;
2051991Sheppo char *msg = buf;
2061991Sheppo char *curr;
2071991Sheppo char *aoff;
2081991Sheppo char line[LINEWIDTH];
2091991Sheppo
2101991Sheppo /* abort if not debugging transport */
2111991Sheppo if (!(dr_debug & DR_DBG_FLAG_TRANS)) {
2121991Sheppo return;
2131991Sheppo }
2141991Sheppo
2151991Sheppo /* walk the buffer one line at a time */
2161991Sheppo for (i = 0; i < len; i += BYTESPERLINE) {
2171991Sheppo
2181991Sheppo bzero(line, LINEWIDTH);
2191991Sheppo
2201991Sheppo curr = line;
2211991Sheppo aoff = line + ASCIIOFFSET;
2221991Sheppo
2231991Sheppo /*
2241991Sheppo * Walk the bytes in the current line, storing
2251991Sheppo * the hex value for the byte as well as the
2261991Sheppo * ASCII representation in a temporary buffer.
2271991Sheppo * All ASCII values are placed at the end of
2281991Sheppo * the line.
2291991Sheppo */
2301991Sheppo for (j = 0; (j < BYTESPERLINE) && ((i + j) < len); j++) {
2311991Sheppo (void) sprintf(curr, " %02x", msg[i + j]);
2321991Sheppo *aoff = (ISPRINT(msg[i + j])) ? msg[i + j] : '.';
2331991Sheppo curr += 3;
2341991Sheppo aoff++;
2351991Sheppo }
2361991Sheppo
2371991Sheppo /*
2381991Sheppo * Fill in to the start of the ASCII translation
2391991Sheppo * with spaces. This will only be necessary if
2401991Sheppo * this is the last line and there are not enough
2411991Sheppo * bytes to fill the whole line.
2421991Sheppo */
2431991Sheppo while (curr != (line + ASCIIOFFSET))
2441991Sheppo *curr++ = ' ';
2451991Sheppo
2461991Sheppo DR_DBG_TRANS("%s\n", line);
2471991Sheppo }
2481991Sheppo }
2491991Sheppo #endif /* DEBUG */
250