xref: /illumos-gate/usr/src/uts/sun4u/io/sbd_cpu.c (revision c3377ee9)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
262a1fd0ffSPeter Tribble /*
272a1fd0ffSPeter Tribble  * Copyright 2019 Peter Tribble.
28*c3377ee9SJohn Levon  * Copyright 2019 Joyent, Inc.
292a1fd0ffSPeter Tribble  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * CPU support routines for DR
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/debug.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/errno.h>
387c478bd9Sstevel@tonic-gate #include <sys/cred.h>
397c478bd9Sstevel@tonic-gate #include <sys/dditypes.h>
407c478bd9Sstevel@tonic-gate #include <sys/devops.h>
417c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
427c478bd9Sstevel@tonic-gate #include <sys/poll.h>
437c478bd9Sstevel@tonic-gate #include <sys/conf.h>
447c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
457c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
467c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
477c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
487c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
497c478bd9Sstevel@tonic-gate #include <sys/stat.h>
507c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
517c478bd9Sstevel@tonic-gate #include <sys/processor.h>
527c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
537c478bd9Sstevel@tonic-gate #include <sys/mem_config.h>
547c478bd9Sstevel@tonic-gate #include <sys/promif.h>
557c478bd9Sstevel@tonic-gate #include <sys/x_call.h>
567c478bd9Sstevel@tonic-gate #include <sys/cpu_sgnblk_defs.h>
577c478bd9Sstevel@tonic-gate #include <sys/membar.h>
587c478bd9Sstevel@tonic-gate #include <sys/stack.h>
597c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
607c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
617c478bd9Sstevel@tonic-gate #include <sys/spitregs.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
647c478bd9Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
657c478bd9Sstevel@tonic-gate #include <sys/pte.h>
667c478bd9Sstevel@tonic-gate #include <sys/mmu.h>
677c478bd9Sstevel@tonic-gate #include <sys/x_call.h>
687c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h>
697c478bd9Sstevel@tonic-gate #include <sys/cheetahregs.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
727c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #include <sys/sbdpriv.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate void
sbd_cpu_set_prop(sbd_cpu_unit_t * cp,dev_info_t * dip)777c478bd9Sstevel@tonic-gate sbd_cpu_set_prop(sbd_cpu_unit_t *cp, dev_info_t *dip)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	uint32_t	clock_freq;
807c478bd9Sstevel@tonic-gate 	int		ecache_size = 0;
817c478bd9Sstevel@tonic-gate 	char		*cache_str = NULL;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	/* read in the CPU speed */
847c478bd9Sstevel@tonic-gate 	clock_freq = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
857c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "clock-frequency", 0);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	ASSERT(clock_freq != 0);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	/*
907c478bd9Sstevel@tonic-gate 	 * The ecache property string is not the same
917c478bd9Sstevel@tonic-gate 	 * for all CPU implementations.
927c478bd9Sstevel@tonic-gate 	 */
937c478bd9Sstevel@tonic-gate 	switch (cp->sbc_cpu_impl) {
947c478bd9Sstevel@tonic-gate 	case CHEETAH_IMPL:
957c478bd9Sstevel@tonic-gate 	case CHEETAH_PLUS_IMPL:
967c478bd9Sstevel@tonic-gate 		cache_str = "ecache-size";
977c478bd9Sstevel@tonic-gate 		break;
987c478bd9Sstevel@tonic-gate 	case JAGUAR_IMPL:
997c478bd9Sstevel@tonic-gate 		cache_str = "l2-cache-size";
1007c478bd9Sstevel@tonic-gate 		break;
1017c478bd9Sstevel@tonic-gate 	case PANTHER_IMPL:
1027c478bd9Sstevel@tonic-gate 		cache_str = "l3-cache-size";
1037c478bd9Sstevel@tonic-gate 		break;
1047c478bd9Sstevel@tonic-gate 	default:
1057c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu implementation type "
1067c478bd9Sstevel@tonic-gate 		    "is an unknown %d value", cp->sbc_cpu_impl);
1077c478bd9Sstevel@tonic-gate 		ASSERT(0);
1087c478bd9Sstevel@tonic-gate 		break;
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (cache_str != NULL) {
1127c478bd9Sstevel@tonic-gate 		/* read in the ecache size */
1137c478bd9Sstevel@tonic-gate 		ecache_size = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1147c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, cache_str, 0);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/*
1187c478bd9Sstevel@tonic-gate 	 * In the case the size is still 0,
1197c478bd9Sstevel@tonic-gate 	 * a zero value will be displayed running non-debug.
1207c478bd9Sstevel@tonic-gate 	 */
1217c478bd9Sstevel@tonic-gate 	ASSERT(ecache_size != 0);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	/* convert to the proper units */
1247c478bd9Sstevel@tonic-gate 	cp->sbc_speed = (clock_freq + 500000) / 1000000;
1257c478bd9Sstevel@tonic-gate 	cp->sbc_ecache = ecache_size / (1024 * 1024);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static void
sbd_fill_cpu_stat(sbd_cpu_unit_t * cp,dev_info_t * dip,sbd_cpu_stat_t * csp)1297c478bd9Sstevel@tonic-gate sbd_fill_cpu_stat(sbd_cpu_unit_t *cp, dev_info_t *dip, sbd_cpu_stat_t *csp)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	int		namelen;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	bzero((caddr_t)csp, sizeof (*csp));
1347c478bd9Sstevel@tonic-gate 	csp->cs_type = cp->sbc_cm.sbdev_type;
1357c478bd9Sstevel@tonic-gate 	csp->cs_unit = cp->sbc_cm.sbdev_unum;
1367c478bd9Sstevel@tonic-gate 	namelen = sizeof (csp->cs_name);
1377c478bd9Sstevel@tonic-gate 	(void) ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1387c478bd9Sstevel@tonic-gate 	    OBP_DEVICETYPE, (caddr_t)csp->cs_name, &namelen);
1397c478bd9Sstevel@tonic-gate 	csp->cs_busy = cp->sbc_cm.sbdev_busy;
1407c478bd9Sstevel@tonic-gate 	csp->cs_time = cp->sbc_cm.sbdev_time;
1417c478bd9Sstevel@tonic-gate 	csp->cs_ostate = cp->sbc_cm.sbdev_ostate;
1427c478bd9Sstevel@tonic-gate 	csp->cs_cpuid = cp->sbc_cpu_id;
1437c478bd9Sstevel@tonic-gate 	csp->cs_suspend = 0;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	/*
1467c478bd9Sstevel@tonic-gate 	 * If we have marked the cpu's condition previously
1477c478bd9Sstevel@tonic-gate 	 * then don't rewrite it
1487c478bd9Sstevel@tonic-gate 	 */
1497c478bd9Sstevel@tonic-gate 	if (csp->cs_cond != SBD_COND_UNUSABLE)
1507c478bd9Sstevel@tonic-gate 		csp->cs_cond = sbd_get_comp_cond(dip);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/*
1537c478bd9Sstevel@tonic-gate 	 * If the speed and ecache properties have not been
1547c478bd9Sstevel@tonic-gate 	 * cached yet, read them in from the device tree.
1557c478bd9Sstevel@tonic-gate 	 */
1567c478bd9Sstevel@tonic-gate 	if ((cp->sbc_speed == 0) || (cp->sbc_ecache == 0))
1577c478bd9Sstevel@tonic-gate 		sbd_cpu_set_prop(cp, dip);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/* use the cached speed and ecache values */
1607c478bd9Sstevel@tonic-gate 	csp->cs_speed = cp->sbc_speed;
1617c478bd9Sstevel@tonic-gate 	csp->cs_ecache = cp->sbc_ecache;
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate static void
sbd_fill_cmp_stat(sbd_cpu_stat_t * csp,int ncores,int impl,sbd_cmp_stat_t * psp)1657c478bd9Sstevel@tonic-gate sbd_fill_cmp_stat(sbd_cpu_stat_t *csp, int ncores, int impl,
1667c478bd9Sstevel@tonic-gate     sbd_cmp_stat_t *psp)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	int	core;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	ASSERT(csp && psp && (ncores >= 1));
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	bzero((caddr_t)psp, sizeof (*psp));
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	/*
1757c478bd9Sstevel@tonic-gate 	 * Fill in the common status information based
1767c478bd9Sstevel@tonic-gate 	 * on the data for the first core.
1777c478bd9Sstevel@tonic-gate 	 */
1787c478bd9Sstevel@tonic-gate 	psp->ps_type = SBD_COMP_CMP;
1797c478bd9Sstevel@tonic-gate 	psp->ps_unit = SBD_CMP_NUM(csp->cs_unit);
1807c478bd9Sstevel@tonic-gate 	(void) strncpy(psp->ps_name, csp->cs_name, sizeof (psp->ps_name));
1817c478bd9Sstevel@tonic-gate 	psp->ps_cond = csp->cs_cond;
1827c478bd9Sstevel@tonic-gate 	psp->ps_busy = csp->cs_busy;
1837c478bd9Sstevel@tonic-gate 	psp->ps_time = csp->cs_time;
1847c478bd9Sstevel@tonic-gate 	psp->ps_ostate = csp->cs_ostate;
1857c478bd9Sstevel@tonic-gate 	psp->ps_suspend = csp->cs_suspend;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/* CMP specific status data */
1887c478bd9Sstevel@tonic-gate 	*psp->ps_cpuid = csp->cs_cpuid;
1897c478bd9Sstevel@tonic-gate 	psp->ps_ncores = 1;
1907c478bd9Sstevel@tonic-gate 	psp->ps_speed = csp->cs_speed;
1917c478bd9Sstevel@tonic-gate 	psp->ps_ecache = csp->cs_ecache;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	/*
1947c478bd9Sstevel@tonic-gate 	 * Walk through the data for the remaining cores.
1957c478bd9Sstevel@tonic-gate 	 * Make any adjustments to the common status data,
1967c478bd9Sstevel@tonic-gate 	 * or the shared CMP specific data if necessary.
1977c478bd9Sstevel@tonic-gate 	 */
1987c478bd9Sstevel@tonic-gate 	for (core = 1; core < ncores; core++) {
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		/*
2017c478bd9Sstevel@tonic-gate 		 * The following properties should be the same
2027c478bd9Sstevel@tonic-gate 		 * for all the cores of the CMP.
2037c478bd9Sstevel@tonic-gate 		 */
2047c478bd9Sstevel@tonic-gate 		ASSERT(psp->ps_unit == SBD_CMP_NUM(csp[core].cs_unit));
2057c478bd9Sstevel@tonic-gate 		ASSERT(psp->ps_speed == csp[core].cs_speed);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		psp->ps_cpuid[core] = csp[core].cs_cpuid;
2087c478bd9Sstevel@tonic-gate 		psp->ps_ncores++;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		/*
2117c478bd9Sstevel@tonic-gate 		 * Jaguar has a split ecache, so the ecache
2127c478bd9Sstevel@tonic-gate 		 * for each core must be added together to
2137c478bd9Sstevel@tonic-gate 		 * get the total ecache for the whole chip.
2147c478bd9Sstevel@tonic-gate 		 */
2157c478bd9Sstevel@tonic-gate 		if (IS_JAGUAR(impl)) {
2167c478bd9Sstevel@tonic-gate 			psp->ps_ecache += csp[core].cs_ecache;
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 		/* adjust time if necessary */
2207c478bd9Sstevel@tonic-gate 		if (csp[core].cs_time > psp->ps_time) {
2217c478bd9Sstevel@tonic-gate 			psp->ps_time = csp[core].cs_time;
2227c478bd9Sstevel@tonic-gate 		}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		psp->ps_busy |= csp[core].cs_busy;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		/*
2277c478bd9Sstevel@tonic-gate 		 * If any of the cores are configured, the
2287c478bd9Sstevel@tonic-gate 		 * entire CMP is marked as configured.
2297c478bd9Sstevel@tonic-gate 		 */
2307c478bd9Sstevel@tonic-gate 		if (csp[core].cs_ostate == SBD_STAT_CONFIGURED) {
2317c478bd9Sstevel@tonic-gate 			psp->ps_ostate = csp[core].cs_ostate;
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate int
sbd_cpu_flags(sbd_handle_t * hp,sbd_devset_t devset,sbd_dev_stat_t * dsp)2377c478bd9Sstevel@tonic-gate sbd_cpu_flags(sbd_handle_t *hp, sbd_devset_t devset, sbd_dev_stat_t *dsp)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	int		cmp;
2407c478bd9Sstevel@tonic-gate 	int		ncpu;
2417c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbp;
2427c478bd9Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
2437c478bd9Sstevel@tonic-gate 	sbd_cpu_stat_t	cstat[MAX_CORES_PER_CMP];
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
2467c478bd9Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/*
2497c478bd9Sstevel@tonic-gate 	 * Grab the status lock before accessing the dip as we allow
2507c478bd9Sstevel@tonic-gate 	 * concurrent status and branch unconfigure and disconnect.
2517c478bd9Sstevel@tonic-gate 	 *
2527c478bd9Sstevel@tonic-gate 	 * The disconnect thread clears the present devset first
2537c478bd9Sstevel@tonic-gate 	 * and then destroys dips. It is possible that the status
2547c478bd9Sstevel@tonic-gate 	 * thread checks the present devset before they are cleared
2557c478bd9Sstevel@tonic-gate 	 * but accesses the dip after they are destroyed causing a
2567c478bd9Sstevel@tonic-gate 	 * panic. To prevent this, the status thread should check
2577c478bd9Sstevel@tonic-gate 	 * the present devset and access dips with status lock held.
2587c478bd9Sstevel@tonic-gate 	 * Similarly disconnect thread should clear the present devset
2597c478bd9Sstevel@tonic-gate 	 * and destroy dips with status lock held.
2607c478bd9Sstevel@tonic-gate 	 */
2617c478bd9Sstevel@tonic-gate 	mutex_enter(&sbp->sb_slock);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/*
2647c478bd9Sstevel@tonic-gate 	 * Only look for requested devices that are actually present.
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	devset &= SBD_DEVS_PRESENT(sbp);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	/*
2697c478bd9Sstevel@tonic-gate 	 * Treat every CPU as a CMP.  In the case where the
2707c478bd9Sstevel@tonic-gate 	 * device is not a CMP, treat it as a CMP with only
2717c478bd9Sstevel@tonic-gate 	 * one core.
2727c478bd9Sstevel@tonic-gate 	 */
2737c478bd9Sstevel@tonic-gate 	for (cmp = ncpu = 0; cmp < MAX_CMP_UNITS_PER_BOARD; cmp++) {
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 		int		ncores;
2767c478bd9Sstevel@tonic-gate 		int		core;
2777c478bd9Sstevel@tonic-gate 		dev_info_t	*dip;
2787c478bd9Sstevel@tonic-gate 		sbd_cpu_unit_t	*cp;
2797c478bd9Sstevel@tonic-gate 		sbd_cmp_stat_t	*psp;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_CMP, cmp) == 0)
2827c478bd9Sstevel@tonic-gate 			continue;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		ncores = 0;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		for (core = 0; core < MAX_CORES_PER_CMP; core++) {
2877c478bd9Sstevel@tonic-gate 			int	unit;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 			unit = sbdp_portid_to_cpu_unit(cmp, core);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 			/*
2927c478bd9Sstevel@tonic-gate 			 * Check to make sure the cpu is in a state
2937c478bd9Sstevel@tonic-gate 			 * where its fully initialized.
2947c478bd9Sstevel@tonic-gate 			 */
2957c478bd9Sstevel@tonic-gate 			if (SBD_DEVICE_STATE(sbp, SBD_COMP_CPU, unit) ==
2967c478bd9Sstevel@tonic-gate 			    SBD_STATE_EMPTY)
2977c478bd9Sstevel@tonic-gate 				continue;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 			dip = sbp->sb_devlist[NIX(SBD_COMP_CMP)][unit];
3007c478bd9Sstevel@tonic-gate 			if (dip == NULL)
3017c478bd9Sstevel@tonic-gate 				continue;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 			cp = SBD_GET_BOARD_CPUUNIT(sbp, unit);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 			sbd_fill_cpu_stat(cp, dip, &cstat[ncores++]);
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 		if (ncores == 0)
3097c478bd9Sstevel@tonic-gate 			continue;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		/*
3127c478bd9Sstevel@tonic-gate 		 * Store the data to the outgoing array. If the
3137c478bd9Sstevel@tonic-gate 		 * device is a CMP, combine all the data for the
3147c478bd9Sstevel@tonic-gate 		 * cores into a single stat structure.
3157c478bd9Sstevel@tonic-gate 		 *
3167c478bd9Sstevel@tonic-gate 		 * The check for a CMP device uses the last core
3177c478bd9Sstevel@tonic-gate 		 * found, assuming that all cores will have the
3187c478bd9Sstevel@tonic-gate 		 * same implementation.
3197c478bd9Sstevel@tonic-gate 		 */
3207c478bd9Sstevel@tonic-gate 		if (CPU_IMPL_IS_CMP(cp->sbc_cpu_impl)) {
3217c478bd9Sstevel@tonic-gate 			psp = (sbd_cmp_stat_t *)dsp;
3227c478bd9Sstevel@tonic-gate 			sbd_fill_cmp_stat(cstat, ncores, cp->sbc_cpu_impl, psp);
3237c478bd9Sstevel@tonic-gate 		} else {
3247c478bd9Sstevel@tonic-gate 			ASSERT(ncores == 1);
3257c478bd9Sstevel@tonic-gate 			bcopy(cstat, dsp, sizeof (sbd_cpu_stat_t));
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		dsp++;
3297c478bd9Sstevel@tonic-gate 		ncpu++;
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	mutex_exit(&sbp->sb_slock);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	return (ncpu);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate int
sbd_pre_release_cpu(sbd_handle_t * hp,sbd_devlist_t * devlist,int devnum)3407c478bd9Sstevel@tonic-gate sbd_pre_release_cpu(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	int		i, rv = 0, unit;
3437c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
3447c478bd9Sstevel@tonic-gate 	processorid_t	cpuid;
3457c478bd9Sstevel@tonic-gate 	struct cpu	*cpup;
3467c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
3477c478bd9Sstevel@tonic-gate 	sbderror_t	*ep = SBD_HD2ERR(hp);
3487c478bd9Sstevel@tonic-gate 	sbd_cpu_unit_t	*cp;
3497c478bd9Sstevel@tonic-gate 	static fn_t	f = "sbd_pre_release_cpu";
3507c478bd9Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
3537c478bd9Sstevel@tonic-gate 	/*
3547c478bd9Sstevel@tonic-gate 	 * May have to juggle bootproc in release_component
3557c478bd9Sstevel@tonic-gate 	 */
3567c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	for (i = 0; i < devnum; i++, devlist++) {
3597c478bd9Sstevel@tonic-gate 		dip = devlist->dv_dip;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 		cpuid = sbdp_get_cpuid(hdp, dip);
3627c478bd9Sstevel@tonic-gate 		if (cpuid < 0) {
3637c478bd9Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE) {
3647c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
3657c478bd9Sstevel@tonic-gate 					"sbd:%s: failed to get cpuid for "
3667c478bd9Sstevel@tonic-gate 					"dip (0x%p)", f, (void *)dip);
3677c478bd9Sstevel@tonic-gate 				continue;
3687c478bd9Sstevel@tonic-gate 			} else {
3697c478bd9Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
3707c478bd9Sstevel@tonic-gate 				break;
3717c478bd9Sstevel@tonic-gate 			}
3727c478bd9Sstevel@tonic-gate 		}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 		unit = sbdp_get_unit_num(hdp, dip);
3767c478bd9Sstevel@tonic-gate 		if (unit < 0) {
3777c478bd9Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE) {
3787c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
3797c478bd9Sstevel@tonic-gate 				"sbd:%s: failed to get unit (cpu %d)",
3807c478bd9Sstevel@tonic-gate 				f, cpuid);
3817c478bd9Sstevel@tonic-gate 				continue;
3827c478bd9Sstevel@tonic-gate 			} else {
3837c478bd9Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
3847c478bd9Sstevel@tonic-gate 				break;
3857c478bd9Sstevel@tonic-gate 			}
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 		cp = SBD_GET_BOARD_CPUUNIT(sbp, unit);
3897c478bd9Sstevel@tonic-gate 		cp->sbc_cpu_flags = cpu[cpuid]->cpu_flags;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		if (cpu_flagged_active(cp->sbc_cpu_flags)) {
3927c478bd9Sstevel@tonic-gate 			int cpu_offline_flags = 0;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE)
3957c478bd9Sstevel@tonic-gate 				cpu_offline_flags = CPU_FORCED;
3967c478bd9Sstevel@tonic-gate 			PR_CPU("%s: offlining cpuid %d unit %d", f,
3977c478bd9Sstevel@tonic-gate 				cpuid, unit);
3987c478bd9Sstevel@tonic-gate 			if (cpu_offline(cpu[cpuid], cpu_offline_flags)) {
3997c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
4007c478bd9Sstevel@tonic-gate 					"%s: failed to offline cpu %d",
4017c478bd9Sstevel@tonic-gate 					f, cpuid);
4027c478bd9Sstevel@tonic-gate 				rv = -1;
4037c478bd9Sstevel@tonic-gate 				SBD_SET_ERR(ep, ESBD_OFFLINE);
4047c478bd9Sstevel@tonic-gate 				SBD_SET_ERRSTR(ep, sbp->sb_cpupath[i]);
4057c478bd9Sstevel@tonic-gate 				cpup = cpu_get(cpuid);
4067c478bd9Sstevel@tonic-gate 				if (cpup && disp_bound_threads(cpup, 0)) {
4077c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "sbd:%s: thread(s) "
4087c478bd9Sstevel@tonic-gate 						"bound to cpu %d",
4097c478bd9Sstevel@tonic-gate 						f, cpup->cpu_id);
4107c478bd9Sstevel@tonic-gate 				}
4117c478bd9Sstevel@tonic-gate 				break;
4127c478bd9Sstevel@tonic-gate 			}
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 		if (rv == 0) {
4167c478bd9Sstevel@tonic-gate 			if (sbdp_release_component(hdp, dip)) {
4177c478bd9Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, ep);
4187c478bd9Sstevel@tonic-gate 				break;
4197c478bd9Sstevel@tonic-gate 			}
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		if (rv)
4237c478bd9Sstevel@tonic-gate 			break;
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	if (rv) {
4297c478bd9Sstevel@tonic-gate 		/*
4307c478bd9Sstevel@tonic-gate 		 * Need to unwind others since at this level (pre-release)
4317c478bd9Sstevel@tonic-gate 		 * the device state has not yet transitioned and failures
4327c478bd9Sstevel@tonic-gate 		 * will prevent us from reaching the "post" release
4337c478bd9Sstevel@tonic-gate 		 * function where states are normally transitioned.
4347c478bd9Sstevel@tonic-gate 		 */
4357c478bd9Sstevel@tonic-gate 		for (; i >= 0; i--, devlist--) {
4367c478bd9Sstevel@tonic-gate 			dip = devlist->dv_dip;
4377c478bd9Sstevel@tonic-gate 			unit = sbdp_get_unit_num(hdp, dip);
4387c478bd9Sstevel@tonic-gate 			if (unit < 0) {
4397c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
4407c478bd9Sstevel@tonic-gate 					"sbd:%s: failed to get unit for "
4417c478bd9Sstevel@tonic-gate 					"dip (0x%p)", f, (void *)dip);
4427c478bd9Sstevel@tonic-gate 				break;
4437c478bd9Sstevel@tonic-gate 			}
4447c478bd9Sstevel@tonic-gate 			(void) sbd_cancel_cpu(hp, unit);
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	SBD_INJECT_ERR(SBD_OFFLINE_CPU_PSEUDO_ERR,
4497c478bd9Sstevel@tonic-gate 		hp->h_err, EIO,
4507c478bd9Sstevel@tonic-gate 		ESBD_OFFLINE,
4517c478bd9Sstevel@tonic-gate 		sbp->sb_cpupath[devnum - 1]);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	return (rv);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate int
sbd_pre_attach_cpu(sbd_handle_t * hp,sbd_devlist_t * devlist,int devnum)4597c478bd9Sstevel@tonic-gate sbd_pre_attach_cpu(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	int		i;
4627c478bd9Sstevel@tonic-gate 	int		unit;
4637c478bd9Sstevel@tonic-gate 	processorid_t	cpuid;
4647c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
4657c478bd9Sstevel@tonic-gate 	sbd_istate_t	dstate;
4667c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
4677c478bd9Sstevel@tonic-gate 	static fn_t	f = "sbd_pre_attach_cpu";
4687c478bd9Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	PR_CPU("%s...\n", f);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	for (i = 0; i < devnum; i++, devlist++) {
4757c478bd9Sstevel@tonic-gate 		dip = devlist->dv_dip;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 		ASSERT(sbd_is_cmp_child(dip) || e_ddi_branch_held(dip));
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 		cpuid = sbdp_get_cpuid(hdp, dip);
4807c478bd9Sstevel@tonic-gate 		if (cpuid < 0) {
4817c478bd9Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE) {
4827c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
4837c478bd9Sstevel@tonic-gate 					"sbd:%s: failed to get cpuid for "
4847c478bd9Sstevel@tonic-gate 					"dip (0x%p)", f, (void *)dip);
4857c478bd9Sstevel@tonic-gate 				continue;
4867c478bd9Sstevel@tonic-gate 			} else {
4877c478bd9Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
4887c478bd9Sstevel@tonic-gate 				break;
4897c478bd9Sstevel@tonic-gate 			}
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		unit = sbdp_get_unit_num(hdp, dip);
4937c478bd9Sstevel@tonic-gate 		if (unit < 0) {
4947c478bd9Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE) {
4957c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
4967c478bd9Sstevel@tonic-gate 				"sbd:%s: failed to get unit (cpu %d)",
4977c478bd9Sstevel@tonic-gate 				f, cpuid);
4987c478bd9Sstevel@tonic-gate 				continue;
4997c478bd9Sstevel@tonic-gate 			} else {
5007c478bd9Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
5017c478bd9Sstevel@tonic-gate 				break;
5027c478bd9Sstevel@tonic-gate 			}
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		PR_CPU("%s: attach cpu-unit (%d.%d)\n",
5067c478bd9Sstevel@tonic-gate 			f, sbp->sb_num, unit);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		dstate = SBD_DEVICE_STATE(sbp, SBD_COMP_CPU, unit);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 		if (dstate == SBD_STATE_UNCONFIGURED) {
5117c478bd9Sstevel@tonic-gate 			/*
5127c478bd9Sstevel@tonic-gate 			 * If we're coming from the UNCONFIGURED
5137c478bd9Sstevel@tonic-gate 			 * state then the cpu's sigblock will
5147c478bd9Sstevel@tonic-gate 			 * still be mapped in.  Need to unmap it
5157c478bd9Sstevel@tonic-gate 			 * before continuing with attachment.
5167c478bd9Sstevel@tonic-gate 			 */
5177c478bd9Sstevel@tonic-gate 			PR_CPU("%s: unmapping sigblk for cpu %d\n",
5187c478bd9Sstevel@tonic-gate 				f, cpuid);
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	return (0);
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate int
sbd_post_attach_cpu(sbd_handle_t * hp,sbd_devlist_t * devlist,int devnum)5317c478bd9Sstevel@tonic-gate sbd_post_attach_cpu(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	int		i;
5347c478bd9Sstevel@tonic-gate 	sbderror_t	*ep = SBD_HD2ERR(hp);
5357c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
5367c478bd9Sstevel@tonic-gate 	processorid_t	cpuid;
5377c478bd9Sstevel@tonic-gate 	struct cpu	*cp;
5387c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
5397c478bd9Sstevel@tonic-gate 	int		err = ESBD_NOERROR;
5407c478bd9Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
5417c478bd9Sstevel@tonic-gate 	static fn_t	f = "sbd_post_attach_cpu";
5427c478bd9Sstevel@tonic-gate 	sbd_cpu_unit_t	*cpup;
5437c478bd9Sstevel@tonic-gate 	int		unit;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/* Startup and online newly-attached CPUs */
5487c478bd9Sstevel@tonic-gate 	for (i = 0; i < devnum; i++, devlist++) {
5497c478bd9Sstevel@tonic-gate 		dip = devlist->dv_dip;
5507c478bd9Sstevel@tonic-gate 		cpuid = sbdp_get_cpuid(hdp, dip);
5517c478bd9Sstevel@tonic-gate 		if (cpuid < 0) {
5527c478bd9Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE) {
5537c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
5547c478bd9Sstevel@tonic-gate 				    "sbd:%s: failed to get cpuid for "
5557c478bd9Sstevel@tonic-gate 				    "dip (0x%p)", f, (void *)dip);
5567c478bd9Sstevel@tonic-gate 				continue;
5577c478bd9Sstevel@tonic-gate 			} else {
5587c478bd9Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, ep);
5597c478bd9Sstevel@tonic-gate 				break;
5607c478bd9Sstevel@tonic-gate 			}
5617c478bd9Sstevel@tonic-gate 		}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 		cp = cpu_get(cpuid);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		if (cp == NULL) {
5667c478bd9Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE) {
5677c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
5687c478bd9Sstevel@tonic-gate 				    "sbd:%s: cpu_get failed for cpu %d",
5697c478bd9Sstevel@tonic-gate 				    f, cpuid);
5707c478bd9Sstevel@tonic-gate 				continue;
5717c478bd9Sstevel@tonic-gate 			} else {
5727c478bd9Sstevel@tonic-gate 				SBD_SET_ERR(ep, ESBD_INTERNAL);
5737c478bd9Sstevel@tonic-gate 				SBD_SET_ERRSTR(ep, sbp->sb_cpupath[i]);
5747c478bd9Sstevel@tonic-gate 				break;
5757c478bd9Sstevel@tonic-gate 			}
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 		if (cpu_is_poweredoff(cp)) {
5797c478bd9Sstevel@tonic-gate 			if (cpu_poweron(cp) != 0) {
5807c478bd9Sstevel@tonic-gate 				SBD_SET_ERR(ep, ESBD_CPUSTART);
5817c478bd9Sstevel@tonic-gate 				SBD_SET_ERRSTR(ep, sbp->sb_cpupath[i]);
5827c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
5837c478bd9Sstevel@tonic-gate 				    "%s: failed to power-on cpu %d",
5847c478bd9Sstevel@tonic-gate 				    f, cpuid);
5857c478bd9Sstevel@tonic-gate 				break;
5867c478bd9Sstevel@tonic-gate 			}
5877c478bd9Sstevel@tonic-gate 			SBD_INJECT_ERR(SBD_POWERON_CPU_PSEUDO_ERR,
5887c478bd9Sstevel@tonic-gate 			    ep, EIO,
5897c478bd9Sstevel@tonic-gate 			    ESBD_CPUSTOP,
5907c478bd9Sstevel@tonic-gate 			    sbp->sb_cpupath[i]);
5917c478bd9Sstevel@tonic-gate 			PR_CPU("%s: cpu %d powered ON\n", f, cpuid);
5927c478bd9Sstevel@tonic-gate 		}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		if (cpu_is_offline(cp)) {
5957c478bd9Sstevel@tonic-gate 			PR_CPU("%s: onlining cpu %d...\n", f, cpuid);
5967c478bd9Sstevel@tonic-gate 
597*c3377ee9SJohn Levon 			if (cpu_online(cp, 0) != 0) {
5987c478bd9Sstevel@tonic-gate 				SBD_SET_ERR(ep, ESBD_ONLINE);
5997c478bd9Sstevel@tonic-gate 				SBD_SET_ERRSTR(ep, sbp->sb_cpupath[i]);
6007c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
6017c478bd9Sstevel@tonic-gate 				    "%s: failed to online cpu %d",
6027c478bd9Sstevel@tonic-gate 				    f, cp->cpu_id);
6037c478bd9Sstevel@tonic-gate 			}
6047c478bd9Sstevel@tonic-gate 			SBD_INJECT_ERR(SBD_ONLINE_CPU_PSEUDO_ERR,
6057c478bd9Sstevel@tonic-gate 			    ep, EIO,
6067c478bd9Sstevel@tonic-gate 			    ESBD_ONLINE,
6077c478bd9Sstevel@tonic-gate 			    sbp->sb_cpupath[i]);
6087c478bd9Sstevel@tonic-gate 		}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		/*
6117c478bd9Sstevel@tonic-gate 		 * if there is no error mark the cpu as OK to use
6127c478bd9Sstevel@tonic-gate 		 */
6137c478bd9Sstevel@tonic-gate 		if (SBD_GET_ERR(ep) == 0) {
6147c478bd9Sstevel@tonic-gate 			unit = sbdp_get_unit_num(hdp, dip);
6157c478bd9Sstevel@tonic-gate 			if (unit < 0) {
6167c478bd9Sstevel@tonic-gate 				if (hp->h_flags & SBD_IOCTL_FLAG_FORCE) {
6177c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN,
6187c478bd9Sstevel@tonic-gate 					    "sbd:%s: failed to get unit "
6197c478bd9Sstevel@tonic-gate 					    "(cpu %d)", f, cpuid);
6207c478bd9Sstevel@tonic-gate 					continue;
6217c478bd9Sstevel@tonic-gate 				} else {
6227c478bd9Sstevel@tonic-gate 					SBD_GET_PERR(hdp->h_err,
6237c478bd9Sstevel@tonic-gate 					    SBD_HD2ERR(hp));
6247c478bd9Sstevel@tonic-gate 					break;
6257c478bd9Sstevel@tonic-gate 				}
6267c478bd9Sstevel@tonic-gate 			}
6277c478bd9Sstevel@tonic-gate 			cpup = SBD_GET_BOARD_CPUUNIT(sbp, unit);
6287c478bd9Sstevel@tonic-gate 			cpup->sbc_cm.sbdev_cond = SBD_COND_OK;
6297c478bd9Sstevel@tonic-gate 		}
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	if (err != ESBD_NOERROR) {
6377c478bd9Sstevel@tonic-gate 		return (-1);
6387c478bd9Sstevel@tonic-gate 	} else {
6397c478bd9Sstevel@tonic-gate 		return (0);
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate int
sbd_pre_detach_cpu(sbd_handle_t * hp,sbd_devlist_t * devlist,int devnum)6447c478bd9Sstevel@tonic-gate sbd_pre_detach_cpu(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	int		i;
6477c478bd9Sstevel@tonic-gate 	int		unit;
6487c478bd9Sstevel@tonic-gate 	processorid_t	cpuid;
6497c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
6507c478bd9Sstevel@tonic-gate 	struct cpu	*cpu;
6517c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
6527c478bd9Sstevel@tonic-gate 	sbderror_t	*ep = SBD_HD2ERR(hp);
6537c478bd9Sstevel@tonic-gate 	static fn_t	f = "sbd_pre_detach_cpu";
6547c478bd9Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
6557c478bd9Sstevel@tonic-gate 	int		rv = 0;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	PR_CPU("%s...\n", f);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	for (i = 0; i < devnum; i++, devlist++) {
6647c478bd9Sstevel@tonic-gate 		dip = devlist->dv_dip;
6657c478bd9Sstevel@tonic-gate 		cpuid = sbdp_get_cpuid(hdp, dip);
6667c478bd9Sstevel@tonic-gate 		if (cpuid < 0) {
6677c478bd9Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE) {
6687c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
6697c478bd9Sstevel@tonic-gate 				    "sbd:%s: failed to get cpuid for "
6707c478bd9Sstevel@tonic-gate 				    "dip (0x%p)", f, (void *)dip);
6717c478bd9Sstevel@tonic-gate 				continue;
6727c478bd9Sstevel@tonic-gate 			} else {
6737c478bd9Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
6747c478bd9Sstevel@tonic-gate 				break;
6757c478bd9Sstevel@tonic-gate 			}
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		cpu = cpu_get(cpuid);
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 		if (cpu == NULL) {
6817c478bd9Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE) {
6827c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
6837c478bd9Sstevel@tonic-gate 				    "sbd:%s: failed to get cpu %d",
6847c478bd9Sstevel@tonic-gate 				    f, cpuid);
6857c478bd9Sstevel@tonic-gate 				continue;
6867c478bd9Sstevel@tonic-gate 			} else {
6877c478bd9Sstevel@tonic-gate 				SBD_SET_ERR(ep, ESBD_INTERNAL);
6887c478bd9Sstevel@tonic-gate 				SBD_SET_ERRSTR(ep, sbp->sb_cpupath[i]);
6897c478bd9Sstevel@tonic-gate 				break;
6907c478bd9Sstevel@tonic-gate 			}
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		unit = sbdp_get_unit_num(hdp, dip);
6947c478bd9Sstevel@tonic-gate 		if (unit < 0) {
6957c478bd9Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE) {
6967c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
6977c478bd9Sstevel@tonic-gate 				    "sbd:%s: failed to get unit (cpu %d)",
6987c478bd9Sstevel@tonic-gate 				    f, cpuid);
6997c478bd9Sstevel@tonic-gate 				continue;
7007c478bd9Sstevel@tonic-gate 			} else {
7017c478bd9Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
7027c478bd9Sstevel@tonic-gate 				break;
7037c478bd9Sstevel@tonic-gate 			}
7047c478bd9Sstevel@tonic-gate 		}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 		PR_CPU("%s: OS detach cpu-unit (%d.%d)\n",
7077c478bd9Sstevel@tonic-gate 		    f, sbp->sb_num, unit);
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		/*
7107c478bd9Sstevel@tonic-gate 		 * CPUs were offlined during Release.
7117c478bd9Sstevel@tonic-gate 		 */
7127c478bd9Sstevel@tonic-gate 		if (cpu_is_poweredoff(cpu)) {
7137c478bd9Sstevel@tonic-gate 			PR_CPU("%s: cpu %d already powered OFF\n", f, cpuid);
7147c478bd9Sstevel@tonic-gate 			continue;
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 		if (cpu_is_offline(cpu)) {
7187c478bd9Sstevel@tonic-gate 			int	e;
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 			if (e = cpu_poweroff(cpu)) {
7217c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
7227c478bd9Sstevel@tonic-gate 				    "%s: failed to power-off cpu %d "
7237c478bd9Sstevel@tonic-gate 				    "(errno %d)",
7247c478bd9Sstevel@tonic-gate 				    f, cpu->cpu_id, e);
7257c478bd9Sstevel@tonic-gate 				SBD_SET_ERR(ep, ESBD_CPUSTOP);
7267c478bd9Sstevel@tonic-gate 				SBD_SET_ERRSTR(ep, sbp->sb_cpupath[i]);
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 				rv = -1;
7297c478bd9Sstevel@tonic-gate 				break;
7307c478bd9Sstevel@tonic-gate 			} else {
7317c478bd9Sstevel@tonic-gate 				PR_CPU("%s: cpu %d powered OFF\n",
7327c478bd9Sstevel@tonic-gate 					f, cpuid);
7337c478bd9Sstevel@tonic-gate 			}
7347c478bd9Sstevel@tonic-gate 		} else {
7357c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s: cpu %d still active",
7367c478bd9Sstevel@tonic-gate 				f, cpu->cpu_id);
7377c478bd9Sstevel@tonic-gate 			SBD_SET_ERR(ep, ESBD_BUSY);
7387c478bd9Sstevel@tonic-gate 			SBD_SET_ERRSTR(ep, sbp->sb_cpupath[i]);
7397c478bd9Sstevel@tonic-gate 			rv = -1;
7407c478bd9Sstevel@tonic-gate 			break;
7417c478bd9Sstevel@tonic-gate 		}
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	return (rv);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate int
sbd_post_detach_cpu(sbd_handle_t * hp,sbd_devlist_t * devlist,int devnum)7507c478bd9Sstevel@tonic-gate sbd_post_detach_cpu(sbd_handle_t *hp, sbd_devlist_t *devlist, int devnum)
7517c478bd9Sstevel@tonic-gate {
7527c478bd9Sstevel@tonic-gate 	static fn_t	f = "sbd_post_detach_cpu";
7537c478bd9Sstevel@tonic-gate 	int		i;
7547c478bd9Sstevel@tonic-gate 	sbderror_t	*ep = SBD_HD2ERR(hp);
7557c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
7567c478bd9Sstevel@tonic-gate 	processorid_t	cpuid;
7577c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
7587c478bd9Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
7597c478bd9Sstevel@tonic-gate 	sbd_cpu_unit_t	*cpup;
7607c478bd9Sstevel@tonic-gate 	int		unit;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	PR_CPU("%s...\n", f);
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	/*
7657c478bd9Sstevel@tonic-gate 	 * We should be holding the cpu_lock at this point,
7667c478bd9Sstevel@tonic-gate 	 * and should have blocked device tree changes.
7677c478bd9Sstevel@tonic-gate 	 */
7687c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	for (i = 0; i < devnum; i++, devlist++) {
7717c478bd9Sstevel@tonic-gate 		dip = devlist->dv_dip;
7727c478bd9Sstevel@tonic-gate 		hdp = sbd_get_sbdp_handle(sbp, hp);
7737c478bd9Sstevel@tonic-gate 		cpuid = sbdp_get_cpuid(hdp, dip);
7747c478bd9Sstevel@tonic-gate 		if (cpuid < 0) {
7757c478bd9Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE) {
7767c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
7777c478bd9Sstevel@tonic-gate 					"sbd:%s: failed to get cpuid for "
7787c478bd9Sstevel@tonic-gate 					"dip (0x%p)", f, (void *)dip);
7797c478bd9Sstevel@tonic-gate 				continue;
7807c478bd9Sstevel@tonic-gate 			} else {
7817c478bd9Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, ep);
7827c478bd9Sstevel@tonic-gate 				break;
7837c478bd9Sstevel@tonic-gate 			}
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 		/*
7867c478bd9Sstevel@tonic-gate 		 * if there is no error mark the cpu as unusable
7877c478bd9Sstevel@tonic-gate 		 */
7887c478bd9Sstevel@tonic-gate 		if (SBD_GET_ERR(ep) == 0) {
7897c478bd9Sstevel@tonic-gate 			unit = sbdp_get_unit_num(hdp, dip);
7907c478bd9Sstevel@tonic-gate 			if (unit < 0) {
7917c478bd9Sstevel@tonic-gate 				if (hp->h_flags & SBD_IOCTL_FLAG_FORCE) {
7927c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN,
7937c478bd9Sstevel@tonic-gate 					    "sbd:%s: failed to get unit "
7947c478bd9Sstevel@tonic-gate 					    "(cpu %d)", f, cpuid);
7957c478bd9Sstevel@tonic-gate 					continue;
7967c478bd9Sstevel@tonic-gate 				} else {
7977c478bd9Sstevel@tonic-gate 					SBD_GET_PERR(hdp->h_err,
7987c478bd9Sstevel@tonic-gate 					    SBD_HD2ERR(hp));
7997c478bd9Sstevel@tonic-gate 					break;
8007c478bd9Sstevel@tonic-gate 				}
8017c478bd9Sstevel@tonic-gate 			}
8027c478bd9Sstevel@tonic-gate 			cpup = SBD_GET_BOARD_CPUUNIT(sbp, unit);
8037c478bd9Sstevel@tonic-gate 			cpup->sbc_cm.sbdev_cond = SBD_COND_UNUSABLE;
8047c478bd9Sstevel@tonic-gate 		}
8057c478bd9Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	return (0);
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate /*
8157c478bd9Sstevel@tonic-gate  * Cancel previous release operation for cpu.  For cpus this means simply
8167c478bd9Sstevel@tonic-gate  * bringing cpus that were offline back online.  Note that they had to have been
8177c478bd9Sstevel@tonic-gate  * online at the time they were released.  If attempting to power on or online
8187c478bd9Sstevel@tonic-gate  * a CPU fails, SBD_CPUERR_FATAL is returned to indicate that the CPU appears to
8197c478bd9Sstevel@tonic-gate  * be unsalvageable.  If a CPU reaches an online or nointr state but can't be
8207c478bd9Sstevel@tonic-gate  * taken to a "lesser" state, SBD_CPUERR_RECOVERABLE is returned to indicate
8217c478bd9Sstevel@tonic-gate  * that it was not returned to its original state but appears to be functional.
8227c478bd9Sstevel@tonic-gate  * Note that the latter case can occur due to unexpected but non-erroneous CPU
8237c478bd9Sstevel@tonic-gate  * manipulation (e.g. by the "psradm" command) during the DR operation.
8247c478bd9Sstevel@tonic-gate  */
8257c478bd9Sstevel@tonic-gate int
sbd_cancel_cpu(sbd_handle_t * hp,int unit)8267c478bd9Sstevel@tonic-gate sbd_cancel_cpu(sbd_handle_t *hp, int unit)
8277c478bd9Sstevel@tonic-gate {
8287c478bd9Sstevel@tonic-gate 	int		rv = SBD_CPUERR_NONE;
8297c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
8307c478bd9Sstevel@tonic-gate 	sbderror_t	*ep = SBD_HD2ERR(hp);
8317c478bd9Sstevel@tonic-gate 	sbd_cpu_unit_t	*cp;
8327c478bd9Sstevel@tonic-gate 	static fn_t	f = "sbd_cancel_cpu";
8337c478bd9Sstevel@tonic-gate 	struct cpu	*cpup;
8347c478bd9Sstevel@tonic-gate 	int		cpu_offline_flags = 0;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	PR_ALL("%s...\n", f);
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	cp = SBD_GET_BOARD_CPUUNIT(sbp, unit);
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	/*
8417c478bd9Sstevel@tonic-gate 	 * If CPU should remain off, nothing needs to be done.
8427c478bd9Sstevel@tonic-gate 	 */
8437c478bd9Sstevel@tonic-gate 	if (cpu_flagged_poweredoff(cp->sbc_cpu_flags))
8447c478bd9Sstevel@tonic-gate 		return (rv);
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	if (hp->h_flags & SBD_IOCTL_FLAG_FORCE)
8477c478bd9Sstevel@tonic-gate 		cpu_offline_flags = CPU_FORCED;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	/*
8507c478bd9Sstevel@tonic-gate 	 * CPU had been either offline, online, or set to no-intr.  We
8517c478bd9Sstevel@tonic-gate 	 * will return a component to its original state that it was
8527c478bd9Sstevel@tonic-gate 	 * prior to the failed DR operation.  There is a possible race
8537c478bd9Sstevel@tonic-gate 	 * condition between the calls to this function and re-obtaining
8547c478bd9Sstevel@tonic-gate 	 * the cpu_lock where a cpu state could change.  Because of this
8557c478bd9Sstevel@tonic-gate 	 * we can't externally document that we are trying to roll cpus
8567c478bd9Sstevel@tonic-gate 	 * back to their original state, but we believe a best effort
8577c478bd9Sstevel@tonic-gate 	 * should be made.
8587c478bd9Sstevel@tonic-gate 	 */
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
8617c478bd9Sstevel@tonic-gate 	cpup = cpu[cp->sbc_cpu_id];
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	/*
8647c478bd9Sstevel@tonic-gate 	 * The following will compare the cpu's current state with a
8657c478bd9Sstevel@tonic-gate 	 * snapshot of its state taken before the failed DR operation
8667c478bd9Sstevel@tonic-gate 	 * had started.
8677c478bd9Sstevel@tonic-gate 	 */
8687c478bd9Sstevel@tonic-gate 	/* POWEROFF */
8697c478bd9Sstevel@tonic-gate 	if (cpu_is_poweredoff(cpup)) {
8707c478bd9Sstevel@tonic-gate 		if (cpu_poweron(cpup)) {
8717c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
8727c478bd9Sstevel@tonic-gate 			    "sbd:%s: failed to power-on cpu %d",
8737c478bd9Sstevel@tonic-gate 			    f, cp->sbc_cpu_id);
8747c478bd9Sstevel@tonic-gate 			SBD_SET_ERR(ep, ESBD_CPUSTART);
8757c478bd9Sstevel@tonic-gate 			SBD_SET_ERRSTR(ep, sbp->sb_cpupath[unit]);
8767c478bd9Sstevel@tonic-gate 			rv = SBD_CPUERR_FATAL;
8777c478bd9Sstevel@tonic-gate 			goto out;
8787c478bd9Sstevel@tonic-gate 		}
8797c478bd9Sstevel@tonic-gate 		SBD_INJECT_ERR(SBD_POWERON_CPU_PSEUDO_ERR,
8807c478bd9Sstevel@tonic-gate 		    hp->h_err, EIO,
8817c478bd9Sstevel@tonic-gate 		    ESBD_CPUSTART,
8827c478bd9Sstevel@tonic-gate 		    sbp->sb_cpupath[unit]);
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	/* OFFLINE */
8867c478bd9Sstevel@tonic-gate 	if (cpu_is_offline(cpup)) {
8877c478bd9Sstevel@tonic-gate 		if (cpu_flagged_offline(cp->sbc_cpu_flags)) {
8887c478bd9Sstevel@tonic-gate 			PR_CPU("%s: leaving cpu %d OFFLINE\n",
8897c478bd9Sstevel@tonic-gate 			    f, cp->sbc_cpu_id);
890*c3377ee9SJohn Levon 		} else if (cpu_online(cpup, 0)) {
8917c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
8927c478bd9Sstevel@tonic-gate 			    "sbd:%s: failed to online cpu %d",
8937c478bd9Sstevel@tonic-gate 			    f, cp->sbc_cpu_id);
8947c478bd9Sstevel@tonic-gate 			SBD_SET_ERR(ep, ESBD_ONLINE);
8957c478bd9Sstevel@tonic-gate 			SBD_SET_ERRSTR(ep, sbp->sb_cpupath[unit]);
8967c478bd9Sstevel@tonic-gate 			rv = SBD_CPUERR_FATAL;
8977c478bd9Sstevel@tonic-gate 			goto out;
8987c478bd9Sstevel@tonic-gate 		} else {
8997c478bd9Sstevel@tonic-gate 			SBD_INJECT_ERR(SBD_ONLINE_CPU_PSEUDO_ERR,
9007c478bd9Sstevel@tonic-gate 			    hp->h_err, EIO,
9017c478bd9Sstevel@tonic-gate 			    ESBD_ONLINE,
9027c478bd9Sstevel@tonic-gate 			    sbp->sb_cpupath[unit]);
9037c478bd9Sstevel@tonic-gate 		}
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	/* ONLINE */
9077c478bd9Sstevel@tonic-gate 	if (cpu_is_online(cpup)) {
9087c478bd9Sstevel@tonic-gate 		if (cpu_flagged_online(cp->sbc_cpu_flags)) {
9097c478bd9Sstevel@tonic-gate 			PR_CPU("%s: setting cpu %d ONLINE\n",
9107c478bd9Sstevel@tonic-gate 			    f, cp->sbc_cpu_id);
9117c478bd9Sstevel@tonic-gate 		} else if (cpu_flagged_offline(cp->sbc_cpu_flags)) {
9127c478bd9Sstevel@tonic-gate 			if (cpu_offline(cpup, cpu_offline_flags)) {
9137c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
9147c478bd9Sstevel@tonic-gate 				    "sbd:%s: failed to offline"
9157c478bd9Sstevel@tonic-gate 				    " cpu %d", f, cp->sbc_cpu_id);
9167c478bd9Sstevel@tonic-gate 				rv = SBD_CPUERR_RECOVERABLE;
9177c478bd9Sstevel@tonic-gate 				goto out;
9187c478bd9Sstevel@tonic-gate 			}
9197c478bd9Sstevel@tonic-gate 		} else if (cpu_flagged_nointr(cp->sbc_cpu_flags)) {
9207c478bd9Sstevel@tonic-gate 			if (cpu_intr_disable(cpup)) {
9217c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s: failed to "
9227c478bd9Sstevel@tonic-gate 				    "disable interrupts on cpu %d",
9237c478bd9Sstevel@tonic-gate 				    f, cp->sbc_cpu_id);
9247c478bd9Sstevel@tonic-gate 				rv = SBD_CPUERR_RECOVERABLE;
9257c478bd9Sstevel@tonic-gate 			} else {
9267c478bd9Sstevel@tonic-gate 				PR_CPU("%s: setting cpu %d to NOINTR"
9277c478bd9Sstevel@tonic-gate 				    " (was online)\n",
9287c478bd9Sstevel@tonic-gate 				    f, cp->sbc_cpu_id);
9297c478bd9Sstevel@tonic-gate 			}
9307c478bd9Sstevel@tonic-gate 			goto out;
9317c478bd9Sstevel@tonic-gate 		}
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	/* NOINTR */
9357c478bd9Sstevel@tonic-gate 	if (cpu_is_nointr(cpup)) {
9367c478bd9Sstevel@tonic-gate 		if (cpu_flagged_online(cp->sbc_cpu_flags)) {
9377c478bd9Sstevel@tonic-gate 			cpu_intr_enable(cpup);
9387c478bd9Sstevel@tonic-gate 			PR_CPU("%s: setting cpu %d ONLINE"
9397c478bd9Sstevel@tonic-gate 			    "(was nointr)\n",
9407c478bd9Sstevel@tonic-gate 			    f, cp->sbc_cpu_id);
9417c478bd9Sstevel@tonic-gate 		}
9427c478bd9Sstevel@tonic-gate 		if (cpu_flagged_offline(cp->sbc_cpu_flags)) {
9437c478bd9Sstevel@tonic-gate 			if (cpu_offline(cpup, cpu_offline_flags)) {
9447c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
9457c478bd9Sstevel@tonic-gate 				    "sbd:%s: failed to offline"
9467c478bd9Sstevel@tonic-gate 				    " cpu %d", f, cp->sbc_cpu_id);
9477c478bd9Sstevel@tonic-gate 				rv = SBD_CPUERR_RECOVERABLE;
9487c478bd9Sstevel@tonic-gate 			}
9497c478bd9Sstevel@tonic-gate 		}
9507c478bd9Sstevel@tonic-gate 	}
9517c478bd9Sstevel@tonic-gate out:
9527c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	return (rv);
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate int
sbd_connect_cpu(sbd_board_t * sbp,int unit)9587c478bd9Sstevel@tonic-gate sbd_connect_cpu(sbd_board_t *sbp, int unit)
9597c478bd9Sstevel@tonic-gate {
9607c478bd9Sstevel@tonic-gate 	int		rv;
9617c478bd9Sstevel@tonic-gate 	processorid_t	cpuid;
9627c478bd9Sstevel@tonic-gate 	struct cpu	*cpu;
9637c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
9647c478bd9Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
9657c478bd9Sstevel@tonic-gate 	extern kmutex_t	cpu_lock;
9667c478bd9Sstevel@tonic-gate 	static fn_t	f = "sbd_connect_cpu";
9677c478bd9Sstevel@tonic-gate 	sbd_handle_t	*hp = MACHBD2HD(sbp);
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	/*
9707c478bd9Sstevel@tonic-gate 	 * get dip for cpu just located in tree walk
9717c478bd9Sstevel@tonic-gate 	 */
9727c478bd9Sstevel@tonic-gate 	if (SBD_DEV_IS_PRESENT(sbp, SBD_COMP_CPU, unit)) {
9737c478bd9Sstevel@tonic-gate 		dip = sbp->sb_devlist[NIX(SBD_COMP_CPU)][unit];
9747c478bd9Sstevel@tonic-gate 		if (dip == NULL) {
9757c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
9767c478bd9Sstevel@tonic-gate 			"sbd:%s: bad dip for cpu unit %d board %d",
9777c478bd9Sstevel@tonic-gate 			f, unit, sbp->sb_num);
9787c478bd9Sstevel@tonic-gate 			return (-1);
9797c478bd9Sstevel@tonic-gate 		}
9807c478bd9Sstevel@tonic-gate 		PR_CPU("%s...\n", f);
9817c478bd9Sstevel@tonic-gate 	} else {
9827c478bd9Sstevel@tonic-gate 		return (0);
9837c478bd9Sstevel@tonic-gate 	}
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	/*
9867c478bd9Sstevel@tonic-gate 	 * if sbd has attached this cpu, no need to bring
9877c478bd9Sstevel@tonic-gate 	 * it out of reset
9887c478bd9Sstevel@tonic-gate 	 */
9897c478bd9Sstevel@tonic-gate 	if (SBD_DEV_IS_ATTACHED(sbp, SBD_COMP_CPU, unit)) {
9907c478bd9Sstevel@tonic-gate 		return (0);
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	cpuid = sbdp_get_cpuid(hdp, dip);
9967c478bd9Sstevel@tonic-gate 	if (cpuid == -1) {
9977c478bd9Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
9987c478bd9Sstevel@tonic-gate 		return (-1);
9997c478bd9Sstevel@tonic-gate 	}
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	/*
10027c478bd9Sstevel@tonic-gate 	 * if the cpu is already under Solaris control,
10037c478bd9Sstevel@tonic-gate 	 * do not wake it up
10047c478bd9Sstevel@tonic-gate 	 */
10057c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
10067c478bd9Sstevel@tonic-gate 	cpu = cpu_get(cpuid);
10077c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
10087c478bd9Sstevel@tonic-gate 	if (cpu != NULL) {
10097c478bd9Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
10107c478bd9Sstevel@tonic-gate 		return (0);
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	rv = sbdp_connect_cpu(hdp, dip, cpuid);
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	if (rv != 0) {
10167c478bd9Sstevel@tonic-gate 		sbp->sb_memaccess_ok = 0;
10177c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
10187c478bd9Sstevel@tonic-gate 			"sbd:%s: failed to wake up cpu unit %d board %d",
10197c478bd9Sstevel@tonic-gate 			f, unit, sbp->sb_num);
10207c478bd9Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
10217c478bd9Sstevel@tonic-gate 		return (rv);
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	return (rv);
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate int
sbd_disconnect_cpu(sbd_handle_t * hp,int unit)10297c478bd9Sstevel@tonic-gate sbd_disconnect_cpu(sbd_handle_t *hp, int unit)
10307c478bd9Sstevel@tonic-gate {
10317c478bd9Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
10327c478bd9Sstevel@tonic-gate 	int		rv;
10337c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
10347c478bd9Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
10357c478bd9Sstevel@tonic-gate 	sbd_cpu_unit_t *cp;
10367c478bd9Sstevel@tonic-gate 	processorid_t   cpuid;
10377c478bd9Sstevel@tonic-gate 	static fn_t	f = "sbd_disconnect_cpu";
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	PR_CPU("%s...\n", f);
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	ASSERT((SBD_DEVICE_STATE(sbp, SBD_COMP_CPU, unit) ==
10427c478bd9Sstevel@tonic-gate 						SBD_STATE_CONNECTED) ||
10437c478bd9Sstevel@tonic-gate 		(SBD_DEVICE_STATE(sbp, SBD_COMP_CPU, unit) ==
10447c478bd9Sstevel@tonic-gate 						SBD_STATE_UNCONFIGURED));
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	cp = SBD_GET_BOARD_CPUUNIT(sbp, unit);
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	cpuid = cp->sbc_cpu_id;
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	dip = sbp->sb_devlist[NIX(SBD_COMP_CPU)][unit];
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	rv = sbdp_disconnect_cpu(hdp, dip, cpuid);
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	if (rv != 0) {
10577c478bd9Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, SBD_HD2ERR(hp));
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	return (rv);
10627c478bd9Sstevel@tonic-gate }
1063