17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5724365fsethg * Common Development and Distribution License (the "License").
6724365fsethg * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
222a417b2Robert Johnston * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bdstevel@tonic-gate */
247c478bdstevel@tonic-gate
257c478bdstevel@tonic-gate/*
267c478bdstevel@tonic-gate * FMD Dynamic Reconfiguration (DR) Event Handling
277c478bdstevel@tonic-gate *
287c478bdstevel@tonic-gate * Fault manager scheme plug-ins must track characteristics of individual
297c478bdstevel@tonic-gate * pieces of hardware.  As these components can be added or removed by a DR
307c478bdstevel@tonic-gate * operation, we need to provide a means by which plug-ins can determine when
317c478bdstevel@tonic-gate * they need to re-examine the current configuration.  We provide a simple
327c478bdstevel@tonic-gate * mechanism whereby this task can be implemented using lazy evaluation: a
337c478bdstevel@tonic-gate * simple 64-bit generation counter is maintained and incremented on *any* DR.
347c478bdstevel@tonic-gate * Schemes can store the generation number in scheme-specific data structures,
357c478bdstevel@tonic-gate * and then revalidate their contents if the current generation number has
367c478bdstevel@tonic-gate * changed since the resource information was cached.  This method saves time,
377c478bdstevel@tonic-gate * avoids the complexity of direct participation in DR, avoids the need for
387c478bdstevel@tonic-gate * resource-specific processing of DR events, and is relatively easy to port
397c478bdstevel@tonic-gate * to other systems that support dynamic reconfiguration.
4024db464eschrock *
4124db464eschrock * The dr generation is only incremented in response to hardware changes.  Since
4224db464eschrock * ASRUs can be in any scheme, including the device scheme, we must also be
4324db464eschrock * aware of software configuration changes which may affect the resource cache.
4424db464eschrock * In addition, we take a snapshot of the topology whenever a reconfiguration
4524db464eschrock * event occurs and notify any modules of the change.
467c478bdstevel@tonic-gate */
477c478bdstevel@tonic-gate
487c478bdstevel@tonic-gate#include <sys/types.h>
4924db464eschrock#include <sys/sunddi.h>
507c478bdstevel@tonic-gate#include <sys/sysevent/dr.h>
517c478bdstevel@tonic-gate#include <sys/sysevent/eventdefs.h>
527c478bdstevel@tonic-gate
537c478bdstevel@tonic-gate#include <stdio.h>
5424db464eschrock#include <string.h>
55d9638e5mws#include <unistd.h>
567c478bdstevel@tonic-gate#include <libsysevent.h>
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gate#undef MUTEX_HELD
597c478bdstevel@tonic-gate#undef RW_READ_HELD
607c478bdstevel@tonic-gate#undef RW_WRITE_HELD
617c478bdstevel@tonic-gate
62724365fsethg#include <fmd_asru.h>
637c478bdstevel@tonic-gate#include <fmd_error.h>
6424db464eschrock#include <fmd_event.h>
65724365fsethg#include <fmd_fmri.h>
6624db464eschrock#include <fmd_module.h>
677c478bdstevel@tonic-gate#include <fmd_subr.h>
6824db464eschrock#include <fmd_topo.h>
697c478bdstevel@tonic-gate#include <fmd.h>
707c478bdstevel@tonic-gate
719af3851eschrockvoid
7224db464eschrockfmd_dr_event(sysevent_t *sep)
73724365fsethg{
7424db464eschrock	uint64_t gen;
7524db464eschrock	fmd_event_t *e;
7624db464eschrock	const char *class = sysevent_get_class_name(sep);
779af3851eschrock	const char *subclass = sysevent_get_subclass_name(sep);
7824db464eschrock	hrtime_t evtime;
7924db464eschrock	fmd_topo_t *ftp, *prev;
80724365fsethg
8124db464eschrock	if (strcmp(class, EC_DR) == 0) {
829af3851eschrock		if (strcmp(subclass, ESC_DR_AP_STATE_CHANGE) != 0 &&
839af3851eschrock		    strcmp(subclass, ESC_DR_TARGET_STATE_CHANGE) != 0)
849af3851eschrock			return;
852a417b2Robert Johnston	/* LINTED: E_NOP_IF_STMT */
869af3851eschrock	} else if (strcmp(class, EC_DEVFS) == 0) {
879af3851eschrock		/*
889af3851eschrock		 * A devfs configuration event can change the topology,
899af3851eschrock		 * as disk nodes only exist when the device is configured.
909af3851eschrock		 */
91e5dcf7bRobert Johnston	} else if (strcmp(class, EC_PLATFORM) == 0) {
929af3851eschrock		/*
932a417b2Robert Johnston		 * Since we rely on the SP to enumerate fans,
942a417b2Robert Johnston		 * power-supplies and sensors/leds, it would be prudent
952a417b2Robert Johnston		 * to take a new snapshot if the SP resets.
969af3851eschrock		 */
972a417b2Robert Johnston		if (strcmp(subclass, ESC_PLATFORM_SP_RESET) != 0)
989af3851eschrock			return;
999af3851eschrock	} else if (strcmp(class, EC_DEV_ADD) == 0 ||
1009af3851eschrock	    strcmp(class, EC_DEV_REMOVE) == 0) {
1019af3851eschrock		if (strcmp(subclass, ESC_DISK) != 0)
1029af3851eschrock			return;
1032a417b2Robert Johnston	} else
1042a417b2Robert Johnston		return;
1057c478bdstevel@tonic-gate
106724365fsethg	/*
10724db464eschrock	 * Take a topo snapshot and notify modules of the change.  Picking an
10824db464eschrock	 * accurate time here is difficult.  On one hand, we have the timestamp
10924db464eschrock	 * of the underlying sysevent, indicating when the reconfiguration event
11024db464eschrock	 * occurred.  On the other hand, we are taking the topo snapshot
11124db464eschrock	 * asynchronously, and hence the timestamp of the snapshot is the
11224db464eschrock	 * current time.  Pretending this topo snapshot was valid at the time
11324db464eschrock	 * the sysevent was posted seems wrong, so we instead opt for the
11424db464eschrock	 * current time as an upper bound on the snapshot validity.
11524db464eschrock	 *
11624db464eschrock	 * Along these lines, we keep track of the last time we dispatched a
11724db464eschrock	 * topo snapshot.  If the sysevent occurred before the last topo
11824db464eschrock	 * snapshot, then don't bother dispatching another topo change event.
11924db464eschrock	 * We've already indicated (to the best of our ability) the change in
12024db464eschrock	 * topology.  This prevents endless topo snapshots in response to a
12124db464eschrock	 * flurry of sysevents.
122724365fsethg	 */
12324db464eschrock	sysevent_get_time(sep, &evtime);
12424db464eschrock	prev = fmd_topo_hold();
1250731288Eric Schrock	if (evtime <= prev->ft_time_begin &&
12624db464eschrock	    fmd.d_clockops == &fmd_timeops_native) {
12724db464eschrock		fmd_topo_rele(prev);
12824db464eschrock		return;
12924db464eschrock	}
13024db464eschrock	fmd_topo_rele(prev);
131724365fsethg
1322a417b2Robert Johnston	(void) pthread_mutex_lock(&fmd.d_stats_lock);
1332a417b2Robert Johnston	gen = fmd.d_stats->ds_dr_gen.fmds_value.ui64++;
1342a417b2Robert Johnston	(void) pthread_mutex_unlock(&fmd.d_stats_lock);
1352a417b2Robert Johnston
1362a417b2Robert Johnston	TRACE((FMD_DBG_XPRT, "dr event %p, gen=%llu", (void *)sep, gen));
1378393544Hyon Kim	fmd_topo_update();
1387c478bdstevel@tonic-gate
13924db464eschrock	ftp = fmd_topo_hold();
1400731288Eric Schrock	e = fmd_event_create(FMD_EVT_TOPO, ftp->ft_time_end, NULL, ftp);
14124db464eschrock	fmd_modhash_dispatch(fmd.d_mod_hash, e);
1427c478bdstevel@tonic-gate}
143