xref: /onnv-gate/usr/src/uts/sun4u/io/sbd_mem.c (revision 11474:857f9db4ef05)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54266Sdp78419  * Common Development and Distribution License (the "License").
64266Sdp78419  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
2111311SSurya.Prakki@Sun.COM 
220Sstevel@tonic-gate /*
23*11474SJonathan.Adams@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * memory support routines for sbd.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/debug.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/errno.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/dditypes.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/conf.h>
380Sstevel@tonic-gate #include <sys/ddi.h>
390Sstevel@tonic-gate #include <sys/sunddi.h>
400Sstevel@tonic-gate #include <sys/sunndi.h>
410Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
420Sstevel@tonic-gate #include <sys/sysmacros.h>
430Sstevel@tonic-gate #include <sys/machsystm.h>
440Sstevel@tonic-gate #include <sys/spitregs.h>
450Sstevel@tonic-gate #include <sys/cpuvar.h>
460Sstevel@tonic-gate #include <sys/cpu_module.h>
470Sstevel@tonic-gate #include <sys/promif.h>
480Sstevel@tonic-gate #include <sys/memlist_impl.h>
490Sstevel@tonic-gate #include <sys/mem_cage.h>
500Sstevel@tonic-gate #include <sys/lgrp.h>
510Sstevel@tonic-gate #include <sys/platform_module.h>
520Sstevel@tonic-gate #include <vm/seg_kmem.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include <sys/sbdpriv.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #define	_ptob64(p) ((uint64_t)(p) << PAGESHIFT)
570Sstevel@tonic-gate #define	_b64top(b) ((pgcnt_t)((b) >> PAGESHIFT))
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static int		sbd_post_detach_mem_unit(sbd_mem_unit_t *mp,
600Sstevel@tonic-gate 				sbderror_t *ep);
610Sstevel@tonic-gate static int		sbd_reserve_mem_spans(memhandle_t *mhp,
620Sstevel@tonic-gate 					struct memlist *mlist);
630Sstevel@tonic-gate static int		sbd_check_boundaries(struct memlist *orig_memlist,
640Sstevel@tonic-gate 					sbd_mem_unit_t *s_mp,
650Sstevel@tonic-gate 					sbd_mem_unit_t *t_mp);
660Sstevel@tonic-gate static int		sbd_select_mem_target(sbd_handle_t *hp,
670Sstevel@tonic-gate 				sbd_mem_unit_t *mp, struct memlist *ml);
680Sstevel@tonic-gate static void		sbd_init_mem_unit_data(sbd_mem_unit_t *mp, sbderror_t
690Sstevel@tonic-gate 					*ep);
700Sstevel@tonic-gate static int		memlist_canfit(struct memlist *s_mlist,
710Sstevel@tonic-gate 					struct memlist *t_mlist);
720Sstevel@tonic-gate static void		sbd_mem_cleanup(sbd_mem_unit_t *s_mp,
730Sstevel@tonic-gate 				sbd_mem_unit_t *t_mp, sbderror_t *ep);
740Sstevel@tonic-gate static void		sbd_flush_ecache(uint64_t a, uint64_t b);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate struct memlist *
sbd_get_memlist(sbd_mem_unit_t * mp,sbderror_t * ep)770Sstevel@tonic-gate sbd_get_memlist(sbd_mem_unit_t *mp, sbderror_t *ep)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	struct memlist	*mlist;
800Sstevel@tonic-gate 	static fn_t	f = "sbd_get_memlist";
810Sstevel@tonic-gate 	sbd_board_t 	*sbp = (sbd_board_t *)mp->sbm_cm.sbdev_sbp;
820Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
830Sstevel@tonic-gate 	sbd_handle_t	*hp = MACHBD2HD(sbp);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	/*
880Sstevel@tonic-gate 	 * Return cached memlist, if present.
890Sstevel@tonic-gate 	 * This memlist will be present following an
900Sstevel@tonic-gate 	 * unconfigure (a.k.a: detach) of this memunit.
910Sstevel@tonic-gate 	 * It should only be used in the case were a configure
920Sstevel@tonic-gate 	 * is bringing this memunit back in without going
930Sstevel@tonic-gate 	 * through the disconnect and connect states.
940Sstevel@tonic-gate 	 */
950Sstevel@tonic-gate 	if (mp->sbm_mlist) {
960Sstevel@tonic-gate 		PR_MEM("%s: found cached memlist\n", f);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 		mlist = memlist_dup(mp->sbm_mlist);
990Sstevel@tonic-gate 	} else {
1000Sstevel@tonic-gate 		/* attempt to construct a memlist using phys_install */
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 		/*
1030Sstevel@tonic-gate 		 * NOTE: this code block assumes only one memunit per
1040Sstevel@tonic-gate 		 * board.  This is currently safe because the function
1050Sstevel@tonic-gate 		 * sbd_init_mem_devlist() forces this assumption to be
1060Sstevel@tonic-gate 		 * valid.
1070Sstevel@tonic-gate 		 */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 		/* round down to slice base address */
1100Sstevel@tonic-gate 		/* build mlist from the lower layer */
1110Sstevel@tonic-gate 		hdp = sbd_get_sbdp_handle(sbp, hp);
1120Sstevel@tonic-gate 		mlist = sbdp_get_memlist(hdp, mp->sbm_cm.sbdev_dip);
1130Sstevel@tonic-gate 		if (mlist == NULL) {
1140Sstevel@tonic-gate 			SBD_GET_PERR(hdp->h_err, ep);
1150Sstevel@tonic-gate 			PR_MEM("sbd:%s: failed to get memlist for "
1160Sstevel@tonic-gate 				"dip (0x%p) ecode %d errno %d", f,
1170Sstevel@tonic-gate 				(void *)mp->sbm_cm.sbdev_dip,
1180Sstevel@tonic-gate 				ep->e_code, ep->e_errno);
1190Sstevel@tonic-gate 			sbd_release_sbdp_handle(hdp);
1200Sstevel@tonic-gate 			return (NULL);
1210Sstevel@tonic-gate 		}
1220Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	PR_MEM("%s: memlist for mem-unit (%d.%d), dip 0x%p:\n",
1260Sstevel@tonic-gate 		f, sbp->sb_num,
1270Sstevel@tonic-gate 		mp->sbm_cm.sbdev_unum,
1280Sstevel@tonic-gate 		(void *)mp->sbm_cm.sbdev_dip);
1290Sstevel@tonic-gate 	SBD_MEMLIST_DUMP(mlist);
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	return (mlist);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate int
sbd_pre_attach_mem(sbd_handle_t * hp,sbd_devlist_t devlist[],int devnum)1350Sstevel@tonic-gate sbd_pre_attach_mem(sbd_handle_t *hp, sbd_devlist_t devlist[], int devnum)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate 	int		err_flag = 0;
1380Sstevel@tonic-gate 	sbderror_t	*ep = SBD_HD2ERR(hp);
1390Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
1400Sstevel@tonic-gate 	int		d, i;
1410Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
1420Sstevel@tonic-gate 	static fn_t	f = "sbd_pre_attach_mem";
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	SBD_SET_ERR(ep, 0);
1470Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
1500Sstevel@tonic-gate 		sbd_mem_unit_t	*mp;
1510Sstevel@tonic-gate 		int		unit;
1520Sstevel@tonic-gate 		dev_info_t	*dip;
1530Sstevel@tonic-gate 		sbd_istate_t	state;
1540Sstevel@tonic-gate 		int		rv;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 		/* sbd_get_devlist will not devlist element w/ dip of 0 */
1570Sstevel@tonic-gate 		ASSERT(devlist[d].dv_dip != NULL);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 		dip = devlist[d].dv_dip;
1600Sstevel@tonic-gate 		unit = sbdp_get_unit_num(hdp, dip);
1610Sstevel@tonic-gate 		if (unit == -1) {
1620Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE)
1630Sstevel@tonic-gate 				continue;
1640Sstevel@tonic-gate 			else {
1650Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, ep);
1660Sstevel@tonic-gate 				err_flag = 1;
1670Sstevel@tonic-gate 				break;
1680Sstevel@tonic-gate 			}
1690Sstevel@tonic-gate 		}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 		mp = SBD_GET_BOARD_MEMUNIT(sbp, unit);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 		ASSERT(mp->sbm_cm.sbdev_sbp == sbp);
1740Sstevel@tonic-gate 		ASSERT(unit == mp->sbm_cm.sbdev_unum);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 		PR_MEM("sbd: OS attach mem-unit (%d.%d)\n",
1770Sstevel@tonic-gate 			sbp->sb_num,
1780Sstevel@tonic-gate 			mp->sbm_cm.sbdev_unum);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 		state = mp->sbm_cm.sbdev_state;
1810Sstevel@tonic-gate 		switch (state) {
1820Sstevel@tonic-gate 		case SBD_STATE_UNCONFIGURED:
1830Sstevel@tonic-gate 			/* use memlist cached by sbd_post_detach_mem_unit */
1840Sstevel@tonic-gate 			if (mp->sbm_mlist != NULL) {
1850Sstevel@tonic-gate 				PR_MEM("%s: recovering from UNCONFIG"
1860Sstevel@tonic-gate 					" mem-unit (%d.%d)\n",
1870Sstevel@tonic-gate 					f, sbp->sb_num,
1880Sstevel@tonic-gate 					mp->sbm_cm.sbdev_unum);
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 				PR_MEM("%s: re-configure cached memlist:\n", f);
1910Sstevel@tonic-gate 				SBD_MEMLIST_DUMP(mp->sbm_mlist);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 				/*
1940Sstevel@tonic-gate 				 * kphysm del handle should have been freed
1950Sstevel@tonic-gate 				 */
1960Sstevel@tonic-gate 				ASSERT((mp->sbm_flags & SBD_MFLAG_RELOWNER)
1970Sstevel@tonic-gate 					== 0);
1980Sstevel@tonic-gate 			} else {
1990Sstevel@tonic-gate 				if (hp->h_flags & SBD_IOCTL_FLAG_FORCE)
2000Sstevel@tonic-gate 					continue;
2010Sstevel@tonic-gate 				else {
2020Sstevel@tonic-gate 					SBD_GET_PERR(hdp->h_err, ep);
2030Sstevel@tonic-gate 					err_flag = 1;
2040Sstevel@tonic-gate 					PR_MEM("%s: mem-unit (%d.%d)"
2050Sstevel@tonic-gate 						" unusable\n",
2060Sstevel@tonic-gate 						f, sbp->sb_num,
2070Sstevel@tonic-gate 						mp->sbm_cm.sbdev_unum);
2080Sstevel@tonic-gate 					break;
2090Sstevel@tonic-gate 				}
2100Sstevel@tonic-gate 			}
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 			/*FALLTHROUGH*/
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 		case SBD_STATE_CONNECTED:
2150Sstevel@tonic-gate 			PR_MEM("%s: reprogramming mem hardware (board %d)\n",
2160Sstevel@tonic-gate 				f, sbp->sb_num);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 			for (i = 0; i < SBD_NUM_MC_PER_BOARD; i++) {
2190Sstevel@tonic-gate 				if (mp->sbm_dip[i] == NULL)
2200Sstevel@tonic-gate 					continue;
2210Sstevel@tonic-gate 				dip = mp->sbm_dip[i];
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 				PR_MEM("%s: enabling mc 0x%p on board %d\n",
2240Sstevel@tonic-gate 					f, (void *)dip, sbp->sb_num);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 				rv = sbdphw_enable_memctrl(hdp, dip);
2270Sstevel@tonic-gate 				if (rv < 0) {
2280Sstevel@tonic-gate 					SBD_GET_PERR(hdp->h_err, ep);
2290Sstevel@tonic-gate 					cmn_err(CE_WARN,
2300Sstevel@tonic-gate 					"%s: failed to program mem ctrlr %p on "
2310Sstevel@tonic-gate 					"board %d", f, (void *)mp->sbm_dip[i],
2320Sstevel@tonic-gate 					sbp->sb_num);
2330Sstevel@tonic-gate 					err_flag = 1;
2340Sstevel@tonic-gate 				}
2350Sstevel@tonic-gate 			}
2360Sstevel@tonic-gate 			break;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 		default:
2390Sstevel@tonic-gate 			cmn_err(CE_WARN,
2400Sstevel@tonic-gate 				"%s: unexpected state (%d) for mem-unit "
2410Sstevel@tonic-gate 				"(%d.%d)", f, state, sbp->sb_num,
2420Sstevel@tonic-gate 				mp->sbm_cm.sbdev_unum);
2430Sstevel@tonic-gate 			if (SBD_GET_ERR(ep) == 0) {
2440Sstevel@tonic-gate 				SBD_SET_ERR(ep, ESBD_STATE);
2450Sstevel@tonic-gate 				err_flag = 1;
2460Sstevel@tonic-gate 			}
2470Sstevel@tonic-gate 			break;
2480Sstevel@tonic-gate 		}
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 		/* exit for loop if error encountered */
2510Sstevel@tonic-gate 		if (err_flag) {
2520Sstevel@tonic-gate 			SBD_SET_ERRSTR(ep,
2530Sstevel@tonic-gate 			    sbp->sb_mempath[mp->sbm_cm.sbdev_unum]);
2540Sstevel@tonic-gate 			break;
2550Sstevel@tonic-gate 		}
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	return (err_flag ? -1 : 0);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate int
sbd_post_attach_mem(sbd_handle_t * hp,sbd_devlist_t devlist[],int devnum)2630Sstevel@tonic-gate sbd_post_attach_mem(sbd_handle_t *hp, sbd_devlist_t devlist[], int devnum)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	int		d;
2660Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
2670Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
2680Sstevel@tonic-gate 	sbderror_t	*ep = SBD_HD2ERR(hp);
2690Sstevel@tonic-gate 	static fn_t	f = "sbd_post_attach_mem";
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
2720Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
2750Sstevel@tonic-gate 		sbd_mem_unit_t	*mp;
2760Sstevel@tonic-gate 		dev_info_t	*dip;
2770Sstevel@tonic-gate 		int		unit;
2780Sstevel@tonic-gate 		struct memlist	*mlist, *ml;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 		/* sbd_get_devlist will not devlist element w/ dip of 0 */
2810Sstevel@tonic-gate 		ASSERT(devlist[d].dv_dip != NULL);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 		dip = devlist[d].dv_dip;
2840Sstevel@tonic-gate 		unit = sbdp_get_unit_num(hdp, dip);
2850Sstevel@tonic-gate 		if (unit == -1) {
2860Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE)
2870Sstevel@tonic-gate 				continue;
2880Sstevel@tonic-gate 			else {
2890Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, ep);
2900Sstevel@tonic-gate 				break;
2910Sstevel@tonic-gate 			}
2920Sstevel@tonic-gate 		}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 		mp = SBD_GET_BOARD_MEMUNIT(sbp, unit);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 		mlist = sbd_get_memlist(mp, ep);
2970Sstevel@tonic-gate 		if (mlist == NULL) {
2980Sstevel@tonic-gate 			cmn_err(CE_WARN,
2990Sstevel@tonic-gate 				"%s: no memlist for mem-unit (%d.%d)",
3000Sstevel@tonic-gate 				f,
3010Sstevel@tonic-gate 				sbp->sb_num,
3020Sstevel@tonic-gate 				mp->sbm_cm.sbdev_unum);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 			if (SBD_GET_ERR(ep) == 0) {
3050Sstevel@tonic-gate 				SBD_SET_ERR(ep, ESBD_MEMFAIL);
3060Sstevel@tonic-gate 				SBD_SET_ERRSTR(ep, sbp->sb_mempath[unit]);
3070Sstevel@tonic-gate 			}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 			continue;
3100Sstevel@tonic-gate 		}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		/*
3130Sstevel@tonic-gate 		 * Verify the memory really did successfully attach
3140Sstevel@tonic-gate 		 * by checking for its existence in phys_install.
3150Sstevel@tonic-gate 		 */
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 		memlist_read_lock();
3180Sstevel@tonic-gate 		if (memlist_intersect(phys_install, mlist) == 0) {
3190Sstevel@tonic-gate 			memlist_read_unlock();
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 			cmn_err(CE_WARN,
3220Sstevel@tonic-gate 				"%s: mem-unit (%d.%d) memlist not in"
3230Sstevel@tonic-gate 				" phys_install", f, sbp->sb_num,
3240Sstevel@tonic-gate 				mp->sbm_cm.sbdev_unum);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 			if (SBD_GET_ERR(ep) == 0) {
3270Sstevel@tonic-gate 				SBD_SET_ERR(ep, ESBD_INTERNAL);
3280Sstevel@tonic-gate 				SBD_SET_ERRSTR(ep, sbp->sb_mempath[unit]);
3290Sstevel@tonic-gate 			}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 			memlist_delete(mlist);
3320Sstevel@tonic-gate 			continue;
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 		memlist_read_unlock();
3350Sstevel@tonic-gate 
336*11474SJonathan.Adams@Sun.COM 		for (ml = mlist; ml != NULL; ml = ml->ml_next) {
337*11474SJonathan.Adams@Sun.COM 			(void) sbdp_mem_add_span(hdp, ml->ml_address,
338*11474SJonathan.Adams@Sun.COM 			    ml->ml_size);
3390Sstevel@tonic-gate 		}
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 		memlist_delete(mlist);
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 		/*
3440Sstevel@tonic-gate 		 * Destroy cached memlist, if any.
3450Sstevel@tonic-gate 		 * There will be a cached memlist in sbm_mlist if
3460Sstevel@tonic-gate 		 * this board is being configured directly after
3470Sstevel@tonic-gate 		 * an unconfigure.
3480Sstevel@tonic-gate 		 * To support this transition, sbd_post_detach_mem
3490Sstevel@tonic-gate 		 * left a copy of the last known memlist in sbm_mlist.
3500Sstevel@tonic-gate 		 * This memlist could differ from any derived from
3510Sstevel@tonic-gate 		 * hardware if while this memunit was last configured
3520Sstevel@tonic-gate 		 * the system detected and deleted bad pages from
3530Sstevel@tonic-gate 		 * phys_install.  The location of those bad pages
3540Sstevel@tonic-gate 		 * will be reflected in the cached memlist.
3550Sstevel@tonic-gate 		 */
3560Sstevel@tonic-gate 		if (mp->sbm_mlist) {
3570Sstevel@tonic-gate 			memlist_delete(mp->sbm_mlist);
3580Sstevel@tonic-gate 			mp->sbm_mlist = NULL;
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 		sbd_init_mem_unit_data(mp, ep);
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
3640Sstevel@tonic-gate 	return (0);
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate int
sbd_pre_detach_mem(sbd_handle_t * hp,sbd_devlist_t devlist[],int devnum)3680Sstevel@tonic-gate sbd_pre_detach_mem(sbd_handle_t *hp, sbd_devlist_t devlist[], int devnum)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	int		d;
3710Sstevel@tonic-gate 	int		unit;
3720Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
3730Sstevel@tonic-gate 	sbderror_t	*ep = SBD_HD2ERR(hp);
3740Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
3750Sstevel@tonic-gate 	dev_info_t	*dip;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
3800Sstevel@tonic-gate 		sbd_mem_unit_t *mp;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 		/* sbd_get_devlist will not devlist element w/ dip of 0 */
3830Sstevel@tonic-gate 		ASSERT(devlist[d].dv_dip != NULL);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 		dip = devlist[d].dv_dip;
3860Sstevel@tonic-gate 		unit = sbdp_get_unit_num(hdp, dip);
3870Sstevel@tonic-gate 		if (unit == -1) {
3880Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE)
3890Sstevel@tonic-gate 				continue;
3900Sstevel@tonic-gate 			else {
3910Sstevel@tonic-gate 				SBD_GET_PERR(hdp->h_err, ep);
3920Sstevel@tonic-gate 				sbd_release_sbdp_handle(hdp);
3930Sstevel@tonic-gate 				return (-1);
3940Sstevel@tonic-gate 			}
3950Sstevel@tonic-gate 		}
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 		mp = SBD_GET_BOARD_MEMUNIT(sbp, unit);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 		/* sanity check */
4000Sstevel@tonic-gate 		ASSERT(mp->sbm_cm.sbdev_sbp == sbp);
4010Sstevel@tonic-gate 		ASSERT(unit == mp->sbm_cm.sbdev_unum);
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 		PR_MEM("sbd: OS detach mem-unit (%d.%d)\n",
4040Sstevel@tonic-gate 			sbp->sb_num, mp->sbm_cm.sbdev_unum);
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
4080Sstevel@tonic-gate 	return (0);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate int
sbd_post_detach_mem(sbd_handle_t * hp,sbd_devlist_t devlist[],int devnum)4120Sstevel@tonic-gate sbd_post_detach_mem(sbd_handle_t *hp, sbd_devlist_t devlist[], int devnum)
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate 	int		d, rv;
4150Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
4160Sstevel@tonic-gate 	sbd_board_t	*sbp;
4170Sstevel@tonic-gate 	sbd_mem_unit_t	*s_mp, *t_mp;
4180Sstevel@tonic-gate 	static fn_t	f = "sbd_post_detach_mem";
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	sbp = SBDH2BD(hp->h_sbd);
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	rv = 0;
4280Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
4290Sstevel@tonic-gate 		sbderror_t	*ep;
4300Sstevel@tonic-gate 		dev_info_t	*dip;
4310Sstevel@tonic-gate 		int		unit;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 		/* sbd_get_devlist will not devlist element w/ dip of 0 */
4340Sstevel@tonic-gate 		ASSERT(devlist[d].dv_dip != NULL);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 		ep = &devlist[d].dv_error;
4370Sstevel@tonic-gate 		if ((SBD_GET_ERR(SBD_HD2ERR(hp)) != 0) ||
4380Sstevel@tonic-gate 		    (sbd_set_err_in_hdl(hp, ep) == 0)) {
4390Sstevel@tonic-gate 			rv = -1;
4400Sstevel@tonic-gate 		}
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 		dip = devlist[d].dv_dip;
4430Sstevel@tonic-gate 		unit = sbdp_get_unit_num(hdp, dip);
4440Sstevel@tonic-gate 		if (unit == -1) {
4450Sstevel@tonic-gate 			if (hp->h_flags & SBD_IOCTL_FLAG_FORCE)
4460Sstevel@tonic-gate 				continue;
4470Sstevel@tonic-gate 			else {
4480Sstevel@tonic-gate 				if (rv != -1)
4490Sstevel@tonic-gate 					SBD_GET_PERR(hdp->h_err, ep);
4500Sstevel@tonic-gate 				break;
4510Sstevel@tonic-gate 			}
4520Sstevel@tonic-gate 		}
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		s_mp = SBD_GET_BOARD_MEMUNIT(sbp, unit);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 		ASSERT(s_mp->sbm_cm.sbdev_sbp == sbp);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 		if (rv == -1) {
4590Sstevel@tonic-gate 			if (s_mp->sbm_flags & SBD_MFLAG_SOURCE) {
4600Sstevel@tonic-gate 				t_mp = s_mp->sbm_peer;
4610Sstevel@tonic-gate 			} else {
4620Sstevel@tonic-gate 				/* this is no target unit */
4630Sstevel@tonic-gate 				t_mp = NULL;
4640Sstevel@tonic-gate 			}
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 			sbd_mem_cleanup(s_mp, t_mp, ep);
4670Sstevel@tonic-gate 		} else if (sbd_post_detach_mem_unit(s_mp, ep))
4680Sstevel@tonic-gate 			rv = -1;
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
4720Sstevel@tonic-gate 	return (rv);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate static void
sbd_add_memory_spans(sbd_board_t * sbp,struct memlist * ml)4760Sstevel@tonic-gate sbd_add_memory_spans(sbd_board_t *sbp, struct memlist *ml)
4770Sstevel@tonic-gate {
4780Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
4790Sstevel@tonic-gate 	static fn_t	f = "sbd_add_memory_spans";
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	PR_MEM("%s...", f);
4820Sstevel@tonic-gate 	SBD_MEMLIST_DUMP(ml);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate #ifdef DEBUG
4850Sstevel@tonic-gate 	memlist_read_lock();
4860Sstevel@tonic-gate 	if (memlist_intersect(phys_install, ml)) {
4870Sstevel@tonic-gate 		PR_MEM("%s:WARNING: memlist intersects with phys_install\n", f);
4880Sstevel@tonic-gate 	}
4890Sstevel@tonic-gate 	memlist_read_unlock();
4900Sstevel@tonic-gate #endif
4910Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(NULL, NULL);
4920Sstevel@tonic-gate 
493*11474SJonathan.Adams@Sun.COM 	for (; ml; ml = ml->ml_next) {
4940Sstevel@tonic-gate 		update_membounds_t umb;
4950Sstevel@tonic-gate 		pfn_t	base;
4960Sstevel@tonic-gate 		pgcnt_t	npgs;
4970Sstevel@tonic-gate 		int	rv;
4980Sstevel@tonic-gate 
499*11474SJonathan.Adams@Sun.COM 		base = _b64top(ml->ml_address);
500*11474SJonathan.Adams@Sun.COM 		npgs = _b64top(ml->ml_size);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 		umb.u_board = sbp->sb_num;
5030Sstevel@tonic-gate 		umb.u_base = (uint64_t)base << MMU_PAGESHIFT;
5040Sstevel@tonic-gate 		umb.u_len = (uint64_t)npgs << MMU_PAGESHIFT;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 		lgrp_plat_config(LGRP_CONFIG_MEM_ADD, (uintptr_t)&umb);
5070Sstevel@tonic-gate 		rv = kphysm_add_memory_dynamic(base, npgs);
5080Sstevel@tonic-gate 
509*11474SJonathan.Adams@Sun.COM 		(void) sbdp_mem_add_span(hdp, ml->ml_address, ml->ml_size);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 		if (rv != KPHYSM_OK) {
5120Sstevel@tonic-gate 			cmn_err(CE_WARN, "sbd:%s:"
5130Sstevel@tonic-gate 				" unexpected kphysm_add_memory_dynamic"
5140Sstevel@tonic-gate 				" return value %d;"
5150Sstevel@tonic-gate 				" basepfn=0x%lx, npages=%ld\n",
5160Sstevel@tonic-gate 				f, rv, base, npgs);
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 			continue;
5190Sstevel@tonic-gate 		}
5204266Sdp78419 		rv = kcage_range_add(base, npgs, KCAGE_DOWN);
5210Sstevel@tonic-gate 		if (rv != 0)
5220Sstevel@tonic-gate 			continue;
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate /* hack for test scripts.  *** remove before code finalized *** */
5280Sstevel@tonic-gate int sbd_last_target;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate static int
sbd_post_detach_mem_unit(sbd_mem_unit_t * s_mp,sbderror_t * ep)5310Sstevel@tonic-gate sbd_post_detach_mem_unit(sbd_mem_unit_t *s_mp, sbderror_t *ep)
5320Sstevel@tonic-gate {
5330Sstevel@tonic-gate 	uint64_t	sz;
5340Sstevel@tonic-gate 	uint64_t	sm;
5350Sstevel@tonic-gate 	uint64_t	t_basepa;
5360Sstevel@tonic-gate 	uint64_t	tmp_basepa;
5370Sstevel@tonic-gate 	uint64_t	s_basepa;
5380Sstevel@tonic-gate 	sbd_board_t 	*sbp;
5390Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
5400Sstevel@tonic-gate 	uint64_t	s_nbytes;
5410Sstevel@tonic-gate 	uint64_t	s_new_basepa;
5420Sstevel@tonic-gate 	sbd_mem_unit_t	*t_mp, *x_mp;
5430Sstevel@tonic-gate 	struct memlist	*ml;
5440Sstevel@tonic-gate 	int		rv;
5450Sstevel@tonic-gate 	static fn_t	f = "sbd_post_detach_mem_unit";
5460Sstevel@tonic-gate 	sbd_handle_t	*hp;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	sbp = (sbd_board_t *)s_mp->sbm_cm.sbdev_sbp;
5510Sstevel@tonic-gate 	hp = MACHBD2HD(sbp);
5520Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	if (sbdp_get_mem_alignment(hdp, s_mp->sbm_cm.sbdev_dip, &sz)) {
5550Sstevel@tonic-gate 		cmn_err(CE_WARN,
5560Sstevel@tonic-gate 			"sbd:%s: no alignment for mem-unit (%d.%d)",
5570Sstevel@tonic-gate 			f, sbp->sb_num, s_mp->sbm_cm.sbdev_unum);
5580Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
5590Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
5600Sstevel@tonic-gate 		return (-1);
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate 	sm = sz - 1;
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	/* s_mp->sbm_del_mlist could be NULL, meaning no deleted spans */
5650Sstevel@tonic-gate 	PR_MEM("%s: brd %d: deleted memlist (EMPTY maybe okay):\n",
5660Sstevel@tonic-gate 		f, sbp->sb_num);
5670Sstevel@tonic-gate 	SBD_MEMLIST_DUMP(s_mp->sbm_del_mlist);
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	/* sanity check */
5700Sstevel@tonic-gate 	ASSERT(s_mp->sbm_del_mlist == NULL ||
5710Sstevel@tonic-gate 		(s_mp->sbm_flags & SBD_MFLAG_RELDONE) != 0);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	if (s_mp->sbm_flags & SBD_MFLAG_SOURCE) {
5740Sstevel@tonic-gate 		t_mp = s_mp->sbm_peer;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 		ASSERT(t_mp != NULL);
5770Sstevel@tonic-gate 		ASSERT(t_mp->sbm_flags & SBD_MFLAG_TARGET);
5780Sstevel@tonic-gate 		ASSERT(t_mp->sbm_peer == s_mp);
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 		ASSERT(t_mp->sbm_flags & SBD_MFLAG_RELDONE);
5810Sstevel@tonic-gate 		ASSERT(t_mp->sbm_del_mlist);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 		sbp = (sbd_board_t *)t_mp->sbm_cm.sbdev_sbp;
5840Sstevel@tonic-gate 		PR_MEM("%s: target brd %d: deleted memlist:\n",
5850Sstevel@tonic-gate 			f, sbp->sb_num);
5860Sstevel@tonic-gate 		SBD_MEMLIST_DUMP(t_mp->sbm_del_mlist);
5870Sstevel@tonic-gate 	} else {
5880Sstevel@tonic-gate 		/* this is no target unit */
5890Sstevel@tonic-gate 		t_mp = NULL;
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	/*
5930Sstevel@tonic-gate 	 * Verify the memory really did successfully detach
5940Sstevel@tonic-gate 	 * by checking for its non-existence in phys_install.
5950Sstevel@tonic-gate 	 */
5960Sstevel@tonic-gate 	rv = 0;
5970Sstevel@tonic-gate 	memlist_read_lock();
5980Sstevel@tonic-gate 	if (s_mp->sbm_flags & SBD_MFLAG_RELDONE) {
5990Sstevel@tonic-gate 		x_mp = s_mp;
6000Sstevel@tonic-gate 		rv = memlist_intersect(phys_install, x_mp->sbm_del_mlist);
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 	if (rv == 0 && t_mp && (t_mp->sbm_flags & SBD_MFLAG_RELDONE)) {
6030Sstevel@tonic-gate 		x_mp = t_mp;
6040Sstevel@tonic-gate 		rv = memlist_intersect(phys_install, x_mp->sbm_del_mlist);
6050Sstevel@tonic-gate 	}
6060Sstevel@tonic-gate 	memlist_read_unlock();
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	if (rv) {
6090Sstevel@tonic-gate 		sbp = (sbd_board_t *)x_mp->sbm_cm.sbdev_sbp;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 		cmn_err(CE_WARN,
6120Sstevel@tonic-gate 			"%s: %smem-unit (%d.%d) memlist still in phys_install",
6130Sstevel@tonic-gate 			f,
6140Sstevel@tonic-gate 			x_mp == t_mp ? "target " : "",
6150Sstevel@tonic-gate 			sbp->sb_num,
6160Sstevel@tonic-gate 			x_mp->sbm_cm.sbdev_unum);
6170Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_INTERNAL);
6180Sstevel@tonic-gate 		SBD_SET_ERRSTR(ep, sbp->sb_mempath[x_mp->sbm_cm.sbdev_unum]);
6190Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
6200Sstevel@tonic-gate 		return (-1);
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	s_basepa	= _ptob64(s_mp->sbm_basepfn);
6240Sstevel@tonic-gate 	s_nbytes	= _ptob64(s_mp->sbm_npages);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	if (t_mp != NULL) {
6270Sstevel@tonic-gate 		t_basepa	= _ptob64(t_mp->sbm_basepfn);
6280Sstevel@tonic-gate 		s_new_basepa	= (s_basepa & ~ sm) +
6290Sstevel@tonic-gate 					_ptob64(t_mp->sbm_slice_offset);
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 		/*
6320Sstevel@tonic-gate 		 * We had to swap mem-units, so update
6330Sstevel@tonic-gate 		 * memlists accordingly with new base
6340Sstevel@tonic-gate 		 * addresses.
6350Sstevel@tonic-gate 		 */
636*11474SJonathan.Adams@Sun.COM 		for (ml = t_mp->sbm_mlist; ml; ml = ml->ml_next) {
637*11474SJonathan.Adams@Sun.COM 			ml->ml_address -= t_basepa;
638*11474SJonathan.Adams@Sun.COM 			ml->ml_address += s_new_basepa;
6390Sstevel@tonic-gate 		}
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 		/*
6420Sstevel@tonic-gate 		 * There is no need to explicitly rename the target delete
6430Sstevel@tonic-gate 		 * memlist, because sbm_del_mlist and sbm_mlist always
6440Sstevel@tonic-gate 		 * point to the same memlist for a copy/rename operation.
6450Sstevel@tonic-gate 		 */
6460Sstevel@tonic-gate 		ASSERT(t_mp->sbm_del_mlist == t_mp->sbm_mlist);
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		PR_MEM("%s: renamed target memlist and delete memlist", f);
6490Sstevel@tonic-gate 		SBD_MEMLIST_DUMP(t_mp->sbm_mlist);
6500Sstevel@tonic-gate 
651*11474SJonathan.Adams@Sun.COM 		for (ml = s_mp->sbm_mlist; ml; ml = ml->ml_next) {
652*11474SJonathan.Adams@Sun.COM 			ml->ml_address -= s_basepa;
653*11474SJonathan.Adams@Sun.COM 			ml->ml_address += t_basepa;
6540Sstevel@tonic-gate 		}
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 		PR_MEM("%s: renamed source memlist", f);
6570Sstevel@tonic-gate 		SBD_MEMLIST_DUMP(s_mp->sbm_mlist);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate #ifdef DEBUG
6600Sstevel@tonic-gate 		ASSERT(s_mp->sbm_mlist != s_mp->sbm_del_mlist);
6610Sstevel@tonic-gate 		/*
6620Sstevel@tonic-gate 		 * Renaming s_mp->sbm_del_mlist is not necessary.  This
6630Sstevel@tonic-gate 		 * list is not used beyond this point, and in fact, is
6640Sstevel@tonic-gate 		 *  disposed of at the end of this function.
6650Sstevel@tonic-gate 		 */
666*11474SJonathan.Adams@Sun.COM 		for (ml = s_mp->sbm_del_mlist; ml; ml = ml->ml_next) {
667*11474SJonathan.Adams@Sun.COM 			ml->ml_address -= s_basepa;
668*11474SJonathan.Adams@Sun.COM 			ml->ml_address += t_basepa;
6690Sstevel@tonic-gate 		}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 		PR_MEM("%s: renamed source delete memlist", f);
6720Sstevel@tonic-gate 		SBD_MEMLIST_DUMP(s_mp->sbm_del_mlist);
6730Sstevel@tonic-gate #endif
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 		if (s_mp->sbm_flags & SBD_MFLAG_MEMUPSIZE) {
6760Sstevel@tonic-gate 			struct memlist	*nl;
6770Sstevel@tonic-gate 			int mlret;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 			/*
6800Sstevel@tonic-gate 			 * We had to perform a copy-rename from a
6810Sstevel@tonic-gate 			 * small memory node to a big memory node.
6820Sstevel@tonic-gate 			 * Need to add back the remaining memory on
6830Sstevel@tonic-gate 			 * the big board that wasn't used by that
6840Sstevel@tonic-gate 			 * from the small board during the copy.
6850Sstevel@tonic-gate 			 * Subtract out the portion of the target memory
6860Sstevel@tonic-gate 			 * node that was taken over by the source memory
6870Sstevel@tonic-gate 			 * node.
6880Sstevel@tonic-gate 			 */
6890Sstevel@tonic-gate 			nl = memlist_dup(t_mp->sbm_mlist);
6900Sstevel@tonic-gate 			mlret = memlist_delete_span(s_basepa, s_nbytes, &nl);
6910Sstevel@tonic-gate 			PR_MEM("%s: mlret = %d\n", f, mlret);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 			sbp = (sbd_board_t *)t_mp->sbm_cm.sbdev_sbp;
6940Sstevel@tonic-gate 			PR_MEM("%s: adding back remaining portion"
6950Sstevel@tonic-gate 				" of mem-unit (%d.%d), memlist:\n",
6960Sstevel@tonic-gate 				f, sbp->sb_num,
6970Sstevel@tonic-gate 				t_mp->sbm_cm.sbdev_unum);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 			SBD_MEMLIST_DUMP(nl);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 			sbd_add_memory_spans(sbp, nl);
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 			memlist_delete(nl);
7040Sstevel@tonic-gate 		}
7050Sstevel@tonic-gate 	}
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	if (t_mp != NULL) {
7090Sstevel@tonic-gate 		sbp = (sbd_board_t *)t_mp->sbm_cm.sbdev_sbp;
7100Sstevel@tonic-gate 		hdp->h_board = sbp->sb_num;
7110Sstevel@tonic-gate 		/* delete target's entire address space */
7120Sstevel@tonic-gate 		tmp_basepa = t_basepa & ~ sm;
7130Sstevel@tonic-gate 		rv = sbdp_mem_del_span(hdp, tmp_basepa, sz);
7140Sstevel@tonic-gate 		ASSERT(rv == 0);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 		sbp = (sbd_board_t *)s_mp->sbm_cm.sbdev_sbp;
7170Sstevel@tonic-gate 		hdp->h_board = sbp->sb_num;
7180Sstevel@tonic-gate 		tmp_basepa = s_basepa & ~ sm;
7190Sstevel@tonic-gate 		sz = s_new_basepa & sm;
7200Sstevel@tonic-gate 		/* delete source board's vacant address space */
7210Sstevel@tonic-gate 		rv = sbdp_mem_del_span(hdp, tmp_basepa, sz);
7220Sstevel@tonic-gate 		ASSERT(rv == 0);
7230Sstevel@tonic-gate 	} else {
7240Sstevel@tonic-gate 		sbp = (sbd_board_t *)s_mp->sbm_cm.sbdev_sbp;
7250Sstevel@tonic-gate 		hdp->h_board = sbp->sb_num;
7260Sstevel@tonic-gate 		tmp_basepa = s_basepa & ~ sm;
7270Sstevel@tonic-gate 		/* delete board's entire address space */
7280Sstevel@tonic-gate 		rv = sbdp_mem_del_span(hdp, tmp_basepa, sz);
7290Sstevel@tonic-gate 		ASSERT(rv == 0);
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate #ifdef LINT
7330Sstevel@tonic-gate 	rv = rv;
7340Sstevel@tonic-gate #endif
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	sbd_mem_cleanup(s_mp, t_mp, ep);
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	sbp = (sbd_board_t *)s_mp->sbm_cm.sbdev_sbp;
7390Sstevel@tonic-gate 	PR_MEM("%s: board %d's memlist:", f, sbp->sb_num);
7400Sstevel@tonic-gate 	SBD_MEMLIST_DUMP(s_mp->sbm_mlist);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
7430Sstevel@tonic-gate 	return (0);
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate static void
sbd_mem_cleanup(sbd_mem_unit_t * s_mp,sbd_mem_unit_t * t_mp,sbderror_t * ep)7470Sstevel@tonic-gate sbd_mem_cleanup(sbd_mem_unit_t *s_mp, sbd_mem_unit_t *t_mp, sbderror_t *ep)
7480Sstevel@tonic-gate {
7490Sstevel@tonic-gate 	sbd_board_t *sbp;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/* clean up target mem unit */
7520Sstevel@tonic-gate 	if (t_mp != NULL) {
7530Sstevel@tonic-gate 		sbp = (sbd_board_t *)t_mp->sbm_cm.sbdev_sbp;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 		ASSERT(t_mp->sbm_del_mlist == t_mp->sbm_mlist);
7560Sstevel@tonic-gate 		/*
7570Sstevel@tonic-gate 		 * sbm_del_mlist and sbm_mlist point at the same list
7580Sstevel@tonic-gate 		 * We only need to delete one and then set both pointers
7590Sstevel@tonic-gate 		 * to NULL
7600Sstevel@tonic-gate 		 */
7610Sstevel@tonic-gate 		memlist_delete(t_mp->sbm_del_mlist);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		t_mp->sbm_del_mlist = NULL;
7640Sstevel@tonic-gate 		t_mp->sbm_mlist = NULL;
7650Sstevel@tonic-gate 		t_mp->sbm_peer = NULL;
7660Sstevel@tonic-gate 		t_mp->sbm_flags = 0;
7670Sstevel@tonic-gate 		t_mp->sbm_cm.sbdev_busy = 0;
7680Sstevel@tonic-gate 		sbd_init_mem_unit_data(t_mp, ep);
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 		/*
7710Sstevel@tonic-gate 		 * now that copy/rename has completed, undo this
7720Sstevel@tonic-gate 		 * work that was done in sbd_release_mem_done.
7730Sstevel@tonic-gate 		 */
7740Sstevel@tonic-gate 		/*
7750Sstevel@tonic-gate 		 * If error don't set the target to configured
7760Sstevel@tonic-gate 		 */
7770Sstevel@tonic-gate 		if (SBD_GET_ERR(ep) == 0) {
7780Sstevel@tonic-gate 			SBD_DEV_CLR_UNREFERENCED(sbp, SBD_COMP_MEM, 0);
7790Sstevel@tonic-gate 			SBD_DEV_CLR_RELEASED(sbp, SBD_COMP_MEM, 0);
7800Sstevel@tonic-gate 			SBD_DEVICE_TRANSITION(sbp, SBD_COMP_MEM, 0,
7810Sstevel@tonic-gate 				SBD_STATE_CONFIGURED);
7820Sstevel@tonic-gate 		}
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate /* hack for test scripts.  *** remove before code finalized *** */
7850Sstevel@tonic-gate sbd_last_target = sbp->sb_num;
7860Sstevel@tonic-gate 	}
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	/*
7890Sstevel@tonic-gate 	 * clean up (source) board's mem unit structure.
7900Sstevel@tonic-gate 	 * NOTE: sbm_mlist is retained.  It is referred to as the
7910Sstevel@tonic-gate 	 * cached memlist.  The cached memlist is used to re-attach
7920Sstevel@tonic-gate 	 * (configure back in) this memunit from the unconfigured
7930Sstevel@tonic-gate 	 * state.
7940Sstevel@tonic-gate 	 */
7950Sstevel@tonic-gate 	if (s_mp != NULL) {
7960Sstevel@tonic-gate 		sbp = (sbd_board_t *)s_mp->sbm_cm.sbdev_sbp;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 		/*
7990Sstevel@tonic-gate 		 * Don't want to call memlist_delete for sbm_del_mlist,
8000Sstevel@tonic-gate 		 * since that list points to the sbm_list
8010Sstevel@tonic-gate 		 */
8020Sstevel@tonic-gate 		s_mp->sbm_del_mlist = NULL;
8030Sstevel@tonic-gate 		s_mp->sbm_peer = NULL;
8040Sstevel@tonic-gate 		s_mp->sbm_flags = 0;
8050Sstevel@tonic-gate 		s_mp->sbm_cm.sbdev_busy = 0;
8060Sstevel@tonic-gate 		sbd_init_mem_unit_data(s_mp, ep);
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate }
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate /*
8110Sstevel@tonic-gate  * Successful return from this function will have the memory
8120Sstevel@tonic-gate  * handle in sbp->sb_dev[..mem-unit...].sbm_memhandle allocated
8130Sstevel@tonic-gate  * and waiting.  This routine's job is to select the memory that
8140Sstevel@tonic-gate  * actually has to be released (detached) which may not necessarily
8150Sstevel@tonic-gate  * be the same memory node that came in in devlist[],
8160Sstevel@tonic-gate  * i.e. a copy-rename is needed.
8170Sstevel@tonic-gate  */
8180Sstevel@tonic-gate int
sbd_pre_release_mem(sbd_handle_t * hp,sbd_devlist_t devlist[],int devnum)8190Sstevel@tonic-gate sbd_pre_release_mem(sbd_handle_t *hp, sbd_devlist_t devlist[], int devnum)
8200Sstevel@tonic-gate {
8210Sstevel@tonic-gate 	extern int	kcage_on;
8220Sstevel@tonic-gate 	int		d;
8230Sstevel@tonic-gate 	int		err_flag = 0;
8240Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
8250Sstevel@tonic-gate 	sbderror_t	*ep = SBD_HD2ERR(hp);
8260Sstevel@tonic-gate 	sbderror_t	*lep;
8270Sstevel@tonic-gate 	static fn_t	f = "sbd_pre_release_mem";
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	if (kcage_on == 0) {
8320Sstevel@tonic-gate 		/*
8330Sstevel@tonic-gate 		 * Can't Detach memory if Cage is OFF.
8340Sstevel@tonic-gate 		 */
8350Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: kernel cage is disabled", f);
8360Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_KCAGE_OFF);
8370Sstevel@tonic-gate 		return (-1);
8380Sstevel@tonic-gate 	}
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
8410Sstevel@tonic-gate 		int		rv;
8420Sstevel@tonic-gate 		memquery_t	mq;
8430Sstevel@tonic-gate 		sbd_mem_unit_t	*mp;
8440Sstevel@tonic-gate 		struct memlist	*ml;
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 		/* sbd_get_devlist will not devlist element w/ dip of 0 */
8470Sstevel@tonic-gate 		ASSERT(devlist[d].dv_dip != NULL);
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 		mp = SBD_GET_BOARD_MEMUNIT(sbp, d);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 		/*
8520Sstevel@tonic-gate 		 * If all the mem unit is marked as failed then don't allow the
8530Sstevel@tonic-gate 		 * operation
8540Sstevel@tonic-gate 		 */
8550Sstevel@tonic-gate 		if (mp->sbm_cm.sbdev_cond == SBD_COND_FAILED) {
8560Sstevel@tonic-gate 			SBD_SET_ERR(ep, ESBD_STATE);
8570Sstevel@tonic-gate 			SBD_SET_ERRSTR(ep, sbp->sb_mempath[d]);
8580Sstevel@tonic-gate 			err_flag = -1;
8590Sstevel@tonic-gate 			break;
8600Sstevel@tonic-gate 		}
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 		ASSERT(d == mp->sbm_cm.sbdev_unum);
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 		/*
8650Sstevel@tonic-gate 		 * if interleave is set to across boards fail the op
8660Sstevel@tonic-gate 		 */
8670Sstevel@tonic-gate 		if (mp->sbm_interleave) {
8680Sstevel@tonic-gate 			SBD_SET_ERR(ep, ESBD_MEMINTLV);
8690Sstevel@tonic-gate 			SBD_SET_ERRSTR(ep, sbp->sb_mempath[d]);
8700Sstevel@tonic-gate 			err_flag = -1;
8710Sstevel@tonic-gate 			break;
8720Sstevel@tonic-gate 		}
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 		lep = &devlist[d].dv_error;
8750Sstevel@tonic-gate 		if (SBD_GET_ERR(lep) != 0) {
8760Sstevel@tonic-gate 			err_flag = -1;
8770Sstevel@tonic-gate 			(void) sbd_set_err_in_hdl(hp, lep);
8780Sstevel@tonic-gate 			break;
8790Sstevel@tonic-gate 		}
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 		if (mp->sbm_flags & SBD_MFLAG_RESERVED) {
8820Sstevel@tonic-gate 			/*
8830Sstevel@tonic-gate 			 * Board is currently involved in a delete
8840Sstevel@tonic-gate 			 * memory operation. Can't detach this guy until
8850Sstevel@tonic-gate 			 * that operation completes.
8860Sstevel@tonic-gate 			 */
8870Sstevel@tonic-gate 			cmn_err(CE_WARN,
8880Sstevel@tonic-gate 				"%s: ineligible mem-unit (%d.%d) for detach",
8890Sstevel@tonic-gate 				f, sbp->sb_num,
8900Sstevel@tonic-gate 				mp->sbm_cm.sbdev_unum);
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 			SBD_SET_ERR(lep, ESBD_INVAL);
8930Sstevel@tonic-gate 			SBD_SET_ERRSTR(lep, sbp->sb_mempath[d]);
8940Sstevel@tonic-gate 			(void) sbd_set_err_in_hdl(hp, lep);
8950Sstevel@tonic-gate 			err_flag = -1;
8960Sstevel@tonic-gate 			break;
8970Sstevel@tonic-gate 		}
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 		/*
9000Sstevel@tonic-gate 		 * Check whether the detaching memory requires a
9010Sstevel@tonic-gate 		 * copy-rename.
9020Sstevel@tonic-gate 		 */
9030Sstevel@tonic-gate 		ASSERT(mp->sbm_npages != 0);
9040Sstevel@tonic-gate 		rv = kphysm_del_span_query(
9050Sstevel@tonic-gate 			mp->sbm_basepfn, mp->sbm_npages, &mq);
9060Sstevel@tonic-gate 		if (rv != KPHYSM_OK) {
9070Sstevel@tonic-gate 			cmn_err(CE_WARN,
9080Sstevel@tonic-gate 				"%s: unexpected kphysm_del_span_query"
9090Sstevel@tonic-gate 				" return value %d;"
9100Sstevel@tonic-gate 				" basepfn 0x%lx, npages 0x%lx,"
9110Sstevel@tonic-gate 				" mem-unit (%d.%d), dip 0x%p",
9120Sstevel@tonic-gate 				f,
9130Sstevel@tonic-gate 				rv,
9140Sstevel@tonic-gate 				mp->sbm_basepfn,
9150Sstevel@tonic-gate 				mp->sbm_npages,
9160Sstevel@tonic-gate 				sbp->sb_num,
9170Sstevel@tonic-gate 				mp->sbm_cm.sbdev_unum,
9180Sstevel@tonic-gate 				(void *)mp->sbm_cm.sbdev_dip);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 			SBD_SET_ERR(lep, ESBD_INTERNAL);
9210Sstevel@tonic-gate 			SBD_SET_ERRSTR(lep, sbp->sb_mempath[d]);
9220Sstevel@tonic-gate 			(void) sbd_set_err_in_hdl(hp, lep);
9230Sstevel@tonic-gate 			err_flag = -1;
9240Sstevel@tonic-gate 			break;
9250Sstevel@tonic-gate 		}
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 		if (mq.nonrelocatable != 0) {
9280Sstevel@tonic-gate 			if (!(hp->h_iap->i_flags & SBD_FLAG_QUIESCE_OKAY)) {
9290Sstevel@tonic-gate 				/* caller wasn't prompted for a suspend */
9300Sstevel@tonic-gate 					SBD_SET_ERR(lep, ESBD_QUIESCE_REQD);
9310Sstevel@tonic-gate 					SBD_SET_ERRSTR(lep, sbp->sb_mempath[d]);
9320Sstevel@tonic-gate 					(void) sbd_set_err_in_hdl(hp, lep);
9330Sstevel@tonic-gate 					err_flag = 1;
9340Sstevel@tonic-gate 					break;
9350Sstevel@tonic-gate 			}
9360Sstevel@tonic-gate 		}
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 		/* flags should be clean at this time */
9390Sstevel@tonic-gate 		ASSERT(mp->sbm_flags == 0);
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 		ASSERT(mp->sbm_del_mlist == NULL);	/* should be null */
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 		if (mp->sbm_mlist != NULL) {
9440Sstevel@tonic-gate 			memlist_delete(mp->sbm_mlist);
9450Sstevel@tonic-gate 			mp->sbm_mlist = NULL;
9460Sstevel@tonic-gate 		}
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 		ml = sbd_get_memlist(mp, lep);
9490Sstevel@tonic-gate 		(void) sbd_set_err_in_hdl(hp, lep);
9500Sstevel@tonic-gate 		if (ml == NULL) {
9510Sstevel@tonic-gate 			PR_MEM("%s: no memlist found for board %d\n",
9520Sstevel@tonic-gate 				f, sbp->sb_num);
9530Sstevel@tonic-gate 			err_flag = -1;
9540Sstevel@tonic-gate 			break;
9550Sstevel@tonic-gate 		}
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 		/* allocate a kphysm handle */
9580Sstevel@tonic-gate 		rv = kphysm_del_gethandle(&mp->sbm_memhandle);
9590Sstevel@tonic-gate 		if (rv != KPHYSM_OK) {
9600Sstevel@tonic-gate 			memlist_delete(ml);
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 			cmn_err(CE_WARN,
9630Sstevel@tonic-gate 				"%s: unexpected kphysm_del_gethandle"
9640Sstevel@tonic-gate 				" return value %d", f, rv);
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 			SBD_SET_ERR(lep, ESBD_INTERNAL);
9670Sstevel@tonic-gate 			SBD_SET_ERRSTR(lep, sbp->sb_mempath[d]);
9680Sstevel@tonic-gate 			(void) sbd_set_err_in_hdl(hp, lep);
9690Sstevel@tonic-gate 			err_flag = -1;
9700Sstevel@tonic-gate 			break;
9710Sstevel@tonic-gate 		}
9720Sstevel@tonic-gate 		mp->sbm_flags |= SBD_MFLAG_RELOWNER;
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 		if ((mq.nonrelocatable != 0) ||
9750Sstevel@tonic-gate 			sbd_reserve_mem_spans(&mp->sbm_memhandle, ml)) {
9760Sstevel@tonic-gate 			/*
9770Sstevel@tonic-gate 			 * Either the detaching memory node contains
9780Sstevel@tonic-gate 			 * non-reloc memory or we failed to reserve the
9790Sstevel@tonic-gate 			 * detaching memory node (which did _not_ have
9800Sstevel@tonic-gate 			 * any non-reloc memory, i.e. some non-reloc mem
9810Sstevel@tonic-gate 			 * got onboard).
9820Sstevel@tonic-gate 			 */
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 			if (sbd_select_mem_target(hp, mp, ml)) {
9850Sstevel@tonic-gate 				int rv;
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 				/*
9880Sstevel@tonic-gate 				 * We had no luck locating a target
9890Sstevel@tonic-gate 				 * memory node to be the recipient of
9900Sstevel@tonic-gate 				 * the non-reloc memory on the node
9910Sstevel@tonic-gate 				 * we're trying to detach.
9920Sstevel@tonic-gate 				 * Clean up be disposing the mem handle
9930Sstevel@tonic-gate 				 * and the mem list.
9940Sstevel@tonic-gate 				 */
9950Sstevel@tonic-gate 				rv = kphysm_del_release(mp->sbm_memhandle);
9960Sstevel@tonic-gate 				if (rv != KPHYSM_OK) {
9970Sstevel@tonic-gate 					/*
9980Sstevel@tonic-gate 					 * can do nothing but complain
9990Sstevel@tonic-gate 					 * and hope helpful for debug
10000Sstevel@tonic-gate 					 */
10010Sstevel@tonic-gate 					cmn_err(CE_WARN, "sbd:%s: unexpected"
10020Sstevel@tonic-gate 						" kphysm_del_release return"
10030Sstevel@tonic-gate 						" value %d",
10040Sstevel@tonic-gate 						f, rv);
10050Sstevel@tonic-gate 				}
10060Sstevel@tonic-gate 				mp->sbm_flags &= ~SBD_MFLAG_RELOWNER;
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 				memlist_delete(ml);
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 				/* make sure sbm_flags is clean */
10110Sstevel@tonic-gate 				ASSERT(mp->sbm_flags == 0);
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 				cmn_err(CE_WARN,
10140Sstevel@tonic-gate 					"%s: no available target for "
10150Sstevel@tonic-gate 					"mem-unit (%d.%d)",
10160Sstevel@tonic-gate 					f, sbp->sb_num,
10170Sstevel@tonic-gate 					mp->sbm_cm.sbdev_unum);
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 				SBD_SET_ERR(lep, ESBD_NO_TARGET);
10200Sstevel@tonic-gate 				SBD_SET_ERRSTR(lep,
10210Sstevel@tonic-gate 					sbp->sb_mempath[mp->sbm_cm.sbdev_unum]);
10220Sstevel@tonic-gate 				(void) sbd_set_err_in_hdl(hp, lep);
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 				err_flag = -1;
10250Sstevel@tonic-gate 				break;
10260Sstevel@tonic-gate 			}
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 			/*
10290Sstevel@tonic-gate 			 * ml is not memlist_deleted here because
10300Sstevel@tonic-gate 			 * it has been assigned to mp->sbm_mlist
10310Sstevel@tonic-gate 			 * by sbd_select_mem_target.
10320Sstevel@tonic-gate 			 */
10330Sstevel@tonic-gate 		} else {
10340Sstevel@tonic-gate 			/* no target needed to detach this board */
10350Sstevel@tonic-gate 			mp->sbm_flags |= SBD_MFLAG_RESERVED;
10360Sstevel@tonic-gate 			mp->sbm_peer = NULL;
10370Sstevel@tonic-gate 			mp->sbm_del_mlist = ml;
10380Sstevel@tonic-gate 			mp->sbm_mlist = ml;
10390Sstevel@tonic-gate 			mp->sbm_cm.sbdev_busy = 1;
10400Sstevel@tonic-gate 		}
10410Sstevel@tonic-gate #ifdef DEBUG
10420Sstevel@tonic-gate 		ASSERT(mp->sbm_mlist != NULL);
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 		if (mp->sbm_flags & SBD_MFLAG_SOURCE) {
10450Sstevel@tonic-gate 			int src, targ;
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 			sbp = (sbd_board_t *)
10480Sstevel@tonic-gate 				mp->sbm_peer->sbm_cm.sbdev_sbp;
10490Sstevel@tonic-gate 			targ = sbp->sb_num;
10500Sstevel@tonic-gate 			sbp = (sbd_board_t *)mp->sbm_cm.sbdev_sbp;
10510Sstevel@tonic-gate 			src = sbp->sb_num;
10520Sstevel@tonic-gate 			PR_MEM("%s: release of board %d requires copy/rename;"
10530Sstevel@tonic-gate 				" selected target board %d\n",
10540Sstevel@tonic-gate 				f, src, targ);
10550Sstevel@tonic-gate 		} else {
10560Sstevel@tonic-gate 			sbp = (sbd_board_t *)mp->sbm_cm.sbdev_sbp;
10570Sstevel@tonic-gate 			PR_MEM("%s: copy/rename not required to release"
10580Sstevel@tonic-gate 				" board %d\n", f, sbp->sb_num);
10590Sstevel@tonic-gate 		}
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 		ASSERT(mp->sbm_flags & SBD_MFLAG_RELOWNER);
10620Sstevel@tonic-gate 		ASSERT(mp->sbm_flags & SBD_MFLAG_RESERVED);
10630Sstevel@tonic-gate #endif
10640Sstevel@tonic-gate 	}
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	return (err_flag);
10670Sstevel@tonic-gate }
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate void
sbd_release_mem_done(sbd_handle_t * hp,int unit)10700Sstevel@tonic-gate sbd_release_mem_done(sbd_handle_t *hp, int unit)
10710Sstevel@tonic-gate {
10720Sstevel@tonic-gate 	sbd_mem_unit_t	*s_mp, *t_mp, *mp;
10730Sstevel@tonic-gate 	sbderror_t	*ep = SBD_HD2ERR(hp);
10740Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
10750Sstevel@tonic-gate 	int		rv;
10760Sstevel@tonic-gate 	static fn_t	f = "sbd_release_mem_done";
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	s_mp = SBD_GET_BOARD_MEMUNIT(sbp, unit);
10790Sstevel@tonic-gate 	sbp = (sbd_board_t *)s_mp->sbm_cm.sbdev_sbp;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	/*
10820Sstevel@tonic-gate 	 * This unit will be flagged with SBD_MFLAG_SOURCE, if it
10830Sstevel@tonic-gate 	 * has a target unit.
10840Sstevel@tonic-gate 	 */
10850Sstevel@tonic-gate 	if (s_mp->sbm_flags & SBD_MFLAG_SOURCE) {
10860Sstevel@tonic-gate 		t_mp = s_mp->sbm_peer;
10870Sstevel@tonic-gate 		ASSERT(t_mp != NULL);
10880Sstevel@tonic-gate 		ASSERT(t_mp->sbm_peer == s_mp);
10890Sstevel@tonic-gate 		ASSERT(t_mp->sbm_flags & SBD_MFLAG_TARGET);
10900Sstevel@tonic-gate 		ASSERT(t_mp->sbm_flags & SBD_MFLAG_RESERVED);
10910Sstevel@tonic-gate 	} else {
10920Sstevel@tonic-gate 		/* this is no target unit */
10930Sstevel@tonic-gate 		t_mp = NULL;
10940Sstevel@tonic-gate 	}
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	/* free delete handle */
10970Sstevel@tonic-gate 	ASSERT(s_mp->sbm_flags & SBD_MFLAG_RELOWNER);
10980Sstevel@tonic-gate 	ASSERT(s_mp->sbm_flags & SBD_MFLAG_RESERVED);
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	rv = kphysm_del_release(s_mp->sbm_memhandle);
11010Sstevel@tonic-gate 	if (rv != KPHYSM_OK) {
11020Sstevel@tonic-gate 		/*
11030Sstevel@tonic-gate 		 * can do nothing but complain
11040Sstevel@tonic-gate 		 * and hope helpful for debug
11050Sstevel@tonic-gate 		 */
11060Sstevel@tonic-gate 		cmn_err(CE_WARN, "sbd:%s: unexpected kphysm_del_release"
11070Sstevel@tonic-gate 			" return value %d", f, rv);
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate 	s_mp->sbm_flags &= ~SBD_MFLAG_RELOWNER;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	/*
11120Sstevel@tonic-gate 	 * If an error was encountered during release, clean up
11130Sstevel@tonic-gate 	 * the source (and target, if present) unit data.
11140Sstevel@tonic-gate 	 */
11150Sstevel@tonic-gate 	if (SBD_GET_ERR(ep) != 0) {
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 		PR_MEM("%s: unit %d.%d: error %d noted\n",
11180Sstevel@tonic-gate 			f, sbp->sb_num,
11190Sstevel@tonic-gate 			s_mp->sbm_cm.sbdev_unum,
11200Sstevel@tonic-gate 			SBD_GET_ERR(ep));
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 		sbd_mem_cleanup(s_mp, t_mp, ep);
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 		/* bail out */
11250Sstevel@tonic-gate 		return;
11260Sstevel@tonic-gate 	}
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	SBD_DEV_SET_RELEASED(sbp, SBD_COMP_MEM, unit);
11290Sstevel@tonic-gate 	SBD_DEVICE_TRANSITION(sbp, SBD_COMP_MEM, unit, SBD_STATE_RELEASE);
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	if (t_mp != NULL) {
11320Sstevel@tonic-gate 		sbp = (sbd_board_t *)t_mp->sbm_cm.sbdev_sbp;
11330Sstevel@tonic-gate 		/*
11340Sstevel@tonic-gate 		 * the kphysm delete operation that drained the source
11350Sstevel@tonic-gate 		 * board also drained this target board.  Since the source
11360Sstevel@tonic-gate 		 * board drain is now known to have succeeded, we know this
11370Sstevel@tonic-gate 		 * target board is drained too.
11380Sstevel@tonic-gate 		 */
11390Sstevel@tonic-gate 		SBD_DEV_SET_RELEASED(sbp, SBD_COMP_MEM,
11400Sstevel@tonic-gate 			t_mp->sbm_cm.sbdev_unum);
11410Sstevel@tonic-gate 		SBD_DEVICE_TRANSITION(sbp, SBD_COMP_MEM,
11420Sstevel@tonic-gate 			t_mp->sbm_cm.sbdev_unum,
11430Sstevel@tonic-gate 			SBD_STATE_RELEASE);
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 		/*
11460Sstevel@tonic-gate 		 * NOTE: do not transition target's board state,
11470Sstevel@tonic-gate 		 * even if the mem-unit was the last configure
11480Sstevel@tonic-gate 		 * unit of the board.  When copy/rename completes
11490Sstevel@tonic-gate 		 * this mem-unit will transitioned back to
11500Sstevel@tonic-gate 		 * the configured state.  In the meantime, the
11510Sstevel@tonic-gate 		 * board's must remain as is.
11520Sstevel@tonic-gate 		 */
11530Sstevel@tonic-gate 	}
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	/* if board(s) had deleted memory, verify it is gone */
11560Sstevel@tonic-gate 	rv = 0;
11570Sstevel@tonic-gate 	memlist_read_lock();
11580Sstevel@tonic-gate 	if (s_mp->sbm_del_mlist != NULL) {
11590Sstevel@tonic-gate 		sbp = (sbd_board_t *)s_mp->sbm_cm.sbdev_sbp;
11600Sstevel@tonic-gate 		mp = s_mp;
11610Sstevel@tonic-gate 		rv = memlist_intersect(phys_install, mp->sbm_del_mlist);
11620Sstevel@tonic-gate 	}
11630Sstevel@tonic-gate 	if (rv == 0 && t_mp && t_mp->sbm_del_mlist != NULL) {
11640Sstevel@tonic-gate 		sbp = (sbd_board_t *)t_mp->sbm_cm.sbdev_sbp;
11650Sstevel@tonic-gate 		mp = t_mp;
11660Sstevel@tonic-gate 		rv = memlist_intersect(phys_install, mp->sbm_del_mlist);
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 	memlist_read_unlock();
11690Sstevel@tonic-gate 	if (rv) {
11700Sstevel@tonic-gate 		cmn_err(CE_WARN, "sbd:%s: %smem-unit (%d.%d): "
11710Sstevel@tonic-gate 			"deleted memory still found in phys_install",
11720Sstevel@tonic-gate 			f,
11730Sstevel@tonic-gate 			(mp == t_mp ? "target " : ""),
11740Sstevel@tonic-gate 			sbp->sb_num,
11750Sstevel@tonic-gate 			mp->sbm_cm.sbdev_unum);
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_INTERNAL);
11780Sstevel@tonic-gate 		SBD_SET_ERRSTR(ep, sbp->sb_mempath[mp->sbm_cm.sbdev_unum]);
11790Sstevel@tonic-gate 		return;
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	s_mp->sbm_flags |= SBD_MFLAG_RELDONE;
11830Sstevel@tonic-gate 	if (t_mp != NULL) {
11840Sstevel@tonic-gate 		t_mp->sbm_flags &= ~SBD_MFLAG_RESERVED;
11850Sstevel@tonic-gate 		t_mp->sbm_flags |= SBD_MFLAG_RELDONE;
11860Sstevel@tonic-gate 	}
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	sbp = (sbd_board_t *)s_mp->sbm_cm.sbdev_sbp;
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	SBD_DEV_SET_UNREFERENCED(sbp, SBD_COMP_MEM, unit);
11910Sstevel@tonic-gate 	SBD_DEVICE_TRANSITION(sbp, SBD_COMP_MEM, unit, SBD_STATE_UNREFERENCED);
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	PR_MEM("%s: marking mem-unit (%d.%d) release DONE\n",
11940Sstevel@tonic-gate 		f, sbp->sb_num,
11950Sstevel@tonic-gate 		s_mp->sbm_cm.sbdev_unum);
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	s_mp->sbm_cm.sbdev_ostate = SBD_STAT_UNCONFIGURED;
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	if (t_mp != NULL) {
12000Sstevel@tonic-gate 		sbp = (sbd_board_t *)t_mp->sbm_cm.sbdev_sbp;
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 		SBD_DEV_SET_UNREFERENCED(sbp, SBD_COMP_MEM,
12030Sstevel@tonic-gate 			t_mp->sbm_cm.sbdev_unum);
12040Sstevel@tonic-gate 		SBD_DEVICE_TRANSITION(sbp, SBD_COMP_MEM,
12050Sstevel@tonic-gate 			t_mp->sbm_cm.sbdev_unum,
12060Sstevel@tonic-gate 			SBD_STATE_UNREFERENCED);
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 		sbp = (sbd_board_t *)s_mp->sbm_cm.sbdev_sbp;
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 		PR_MEM("%s: marking mem-unit (%d.%d) release DONE\n",
12110Sstevel@tonic-gate 			f, sbp->sb_num,
12120Sstevel@tonic-gate 			t_mp->sbm_cm.sbdev_unum);
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 		t_mp->sbm_cm.sbdev_ostate = SBD_STAT_UNCONFIGURED;
12150Sstevel@tonic-gate 	}
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate int
sbd_disconnect_mem(sbd_handle_t * hp,int unit)12190Sstevel@tonic-gate sbd_disconnect_mem(sbd_handle_t *hp, int unit)
12200Sstevel@tonic-gate {
12210Sstevel@tonic-gate 	static fn_t	f = "sbd_disconnect_mem";
12220Sstevel@tonic-gate 	sbd_mem_unit_t	*mp;
12230Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	mp = SBD_GET_BOARD_MEMUNIT(sbp, unit);
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	ASSERT(mp->sbm_cm.sbdev_state == SBD_STATE_CONNECTED ||
12280Sstevel@tonic-gate 	    mp->sbm_cm.sbdev_state == SBD_STATE_UNCONFIGURED);
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	if (mp->sbm_del_mlist && mp->sbm_del_mlist != mp->sbm_mlist)
12330Sstevel@tonic-gate 		memlist_delete(mp->sbm_del_mlist);
12340Sstevel@tonic-gate 	mp->sbm_del_mlist = NULL;
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	if (mp->sbm_mlist) {
12370Sstevel@tonic-gate 		memlist_delete(mp->sbm_mlist);
12380Sstevel@tonic-gate 		mp->sbm_mlist = NULL;
12390Sstevel@tonic-gate 	}
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	return (0);
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate int
sbd_cancel_mem(sbd_handle_t * hp,int unit)12450Sstevel@tonic-gate sbd_cancel_mem(sbd_handle_t *hp, int unit)
12460Sstevel@tonic-gate {
12470Sstevel@tonic-gate 	sbd_mem_unit_t	*s_mp, *t_mp;
12480Sstevel@tonic-gate 	sbd_istate_t	state;
12490Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
12500Sstevel@tonic-gate 	sbd_board_t	*tsbp;
12510Sstevel@tonic-gate 	static fn_t	f = "sbd_cancel_mem";
12520Sstevel@tonic-gate 	sbderror_t	*ep = SBD_HD2ERR(hp);
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	s_mp = SBD_GET_BOARD_MEMUNIT(sbp, unit);
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	state = s_mp->sbm_cm.sbdev_state;
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	if (s_mp->sbm_flags & SBD_MFLAG_TARGET) {
12590Sstevel@tonic-gate 		/* must cancel source board, not target board */
12600Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_INTERNAL);
12610Sstevel@tonic-gate 		SBD_SET_ERRSTR(ep, sbp->sb_mempath[unit]);
12620Sstevel@tonic-gate 		return (-1);
12630Sstevel@tonic-gate 	} else if (s_mp->sbm_flags & SBD_MFLAG_SOURCE) {
12640Sstevel@tonic-gate 		t_mp = s_mp->sbm_peer;
12650Sstevel@tonic-gate 		tsbp = t_mp->sbm_cm.sbdev_sbp;
12660Sstevel@tonic-gate 		ASSERT(t_mp != NULL);
12670Sstevel@tonic-gate 		ASSERT(t_mp->sbm_peer == s_mp);
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 		/* must always match the source board's state */
12700Sstevel@tonic-gate 		ASSERT(t_mp->sbm_cm.sbdev_state == state);
12710Sstevel@tonic-gate 	} else {
12720Sstevel@tonic-gate 		/* this is no target unit */
12730Sstevel@tonic-gate 		t_mp = NULL;
12740Sstevel@tonic-gate 	}
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	switch (state) {
12770Sstevel@tonic-gate 	case SBD_STATE_UNREFERENCED:	/* state set by sbd_release_mem_done */
12780Sstevel@tonic-gate 		ASSERT((s_mp->sbm_flags & SBD_MFLAG_RELOWNER) == 0);
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 		if (t_mp != NULL && t_mp->sbm_del_mlist != NULL) {
12810Sstevel@tonic-gate 			PR_MEM("%s: undoing target board %d memory delete\n",
12820Sstevel@tonic-gate 				f, tsbp->sb_num);
12830Sstevel@tonic-gate 			sbd_add_memory_spans(tsbp, t_mp->sbm_del_mlist);
12840Sstevel@tonic-gate 			SBD_DEV_CLR_UNREFERENCED(tsbp, SBD_COMP_MEM,
12850Sstevel@tonic-gate 				t_mp->sbm_cm.sbdev_unum);
12860Sstevel@tonic-gate 		}
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 		if (s_mp->sbm_del_mlist != NULL) {
12890Sstevel@tonic-gate 			PR_MEM("%s: undoing board %d memory delete\n",
12900Sstevel@tonic-gate 				f, sbp->sb_num);
12910Sstevel@tonic-gate 			sbd_add_memory_spans(sbp, s_mp->sbm_del_mlist);
12920Sstevel@tonic-gate 		}
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 		/*FALLTHROUGH*/
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	case SBD_STATE_CONFIGURED:
12970Sstevel@tonic-gate 		/*
12980Sstevel@tonic-gate 		 * we got here because of an error early in the release process
12990Sstevel@tonic-gate 		 * Just leave the memory as is and report the error
13000Sstevel@tonic-gate 		 */
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 		ASSERT((s_mp->sbm_flags & SBD_MFLAG_RELOWNER) == 0);
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 		if (t_mp != NULL) {
13050Sstevel@tonic-gate 			ASSERT(t_mp->sbm_del_mlist == t_mp->sbm_mlist);
13060Sstevel@tonic-gate 			t_mp->sbm_del_mlist = NULL;
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 			if (t_mp->sbm_mlist != NULL) {
13090Sstevel@tonic-gate 				memlist_delete(t_mp->sbm_mlist);
13100Sstevel@tonic-gate 				t_mp->sbm_mlist = NULL;
13110Sstevel@tonic-gate 			}
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 			t_mp->sbm_peer = NULL;
13140Sstevel@tonic-gate 			t_mp->sbm_flags = 0;
13150Sstevel@tonic-gate 			t_mp->sbm_cm.sbdev_busy = 0;
13160Sstevel@tonic-gate 			sbd_init_mem_unit_data(t_mp, ep);
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 			SBD_DEV_CLR_RELEASED(tsbp, SBD_COMP_MEM,
13190Sstevel@tonic-gate 				t_mp->sbm_cm.sbdev_unum);
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 			SBD_DEVICE_TRANSITION(tsbp, SBD_COMP_MEM,
13220Sstevel@tonic-gate 				t_mp->sbm_cm.sbdev_unum,
13230Sstevel@tonic-gate 				SBD_STATE_CONFIGURED);
13240Sstevel@tonic-gate 		}
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 		if (s_mp->sbm_del_mlist != s_mp->sbm_mlist)
13270Sstevel@tonic-gate 			memlist_delete(s_mp->sbm_del_mlist);
13280Sstevel@tonic-gate 		s_mp->sbm_del_mlist = NULL;
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 		if (s_mp->sbm_mlist != NULL) {
13310Sstevel@tonic-gate 			memlist_delete(s_mp->sbm_mlist);
13320Sstevel@tonic-gate 			s_mp->sbm_mlist = NULL;
13330Sstevel@tonic-gate 		}
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 		s_mp->sbm_peer = NULL;
13360Sstevel@tonic-gate 		s_mp->sbm_flags = 0;
13370Sstevel@tonic-gate 		s_mp->sbm_cm.sbdev_busy = 0;
13380Sstevel@tonic-gate 		sbd_init_mem_unit_data(s_mp, ep);
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 		return (0);
13410Sstevel@tonic-gate 	default:
13420Sstevel@tonic-gate 		PR_MEM("%s: WARNING unexpected state (%d) for "
13430Sstevel@tonic-gate 			"mem-unit %d.%d\n",
13440Sstevel@tonic-gate 			f,
13450Sstevel@tonic-gate 			(int)state,
13460Sstevel@tonic-gate 			sbp->sb_num,
13470Sstevel@tonic-gate 			s_mp->sbm_cm.sbdev_unum);
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 		return (-1);
13500Sstevel@tonic-gate 	}
13510Sstevel@tonic-gate 	/*NOTREACHED*/
13520Sstevel@tonic-gate }
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate void
sbd_init_mem_unit(sbd_board_t * sbp,int unit,sbderror_t * ep)13550Sstevel@tonic-gate sbd_init_mem_unit(sbd_board_t *sbp, int unit, sbderror_t *ep)
13560Sstevel@tonic-gate {
13570Sstevel@tonic-gate 	sbd_istate_t	new_state;
13580Sstevel@tonic-gate 	sbd_mem_unit_t	*mp;
13590Sstevel@tonic-gate 	dev_info_t	*cur_mc_dip;
13600Sstevel@tonic-gate 	int		failed_mcs = 0, present_mcs = 0;
13610Sstevel@tonic-gate 	sbd_cond_t	mc_cond;
13620Sstevel@tonic-gate 	int		i;
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	mp = SBD_GET_BOARD_MEMUNIT(sbp, unit);
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	if (SBD_DEV_IS_ATTACHED(sbp, SBD_COMP_MEM, unit)) {
13670Sstevel@tonic-gate 		new_state = SBD_STATE_CONFIGURED;
13680Sstevel@tonic-gate 	} else if (SBD_DEV_IS_PRESENT(sbp, SBD_COMP_MEM, unit)) {
13690Sstevel@tonic-gate 		new_state = SBD_STATE_CONNECTED;
13700Sstevel@tonic-gate 	} else if (mp->sbm_cm.sbdev_dip != NULL) {
13710Sstevel@tonic-gate 		new_state = SBD_STATE_OCCUPIED;
13720Sstevel@tonic-gate 	} else {
13730Sstevel@tonic-gate 		new_state = SBD_STATE_EMPTY;
13740Sstevel@tonic-gate 	}
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 	/*
13770Sstevel@tonic-gate 	 * Check all the possible memory nodes on the board.  If all of them
13780Sstevel@tonic-gate 	 * have a failed status mark memory as failed. Otherwise mem is ok
13790Sstevel@tonic-gate 	 */
13800Sstevel@tonic-gate 	if (!sbp->sb_memaccess_ok) {
13810Sstevel@tonic-gate 		mp->sbm_cm.sbdev_cond = SBD_COND_UNKNOWN;
13820Sstevel@tonic-gate 		return;
13830Sstevel@tonic-gate 	}
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	for (i = 0; i < SBD_NUM_MC_PER_BOARD; i++) {
13860Sstevel@tonic-gate 		cur_mc_dip = mp->sbm_dip[i];
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 		if (cur_mc_dip == NULL)
13890Sstevel@tonic-gate 			continue;
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 		present_mcs |= (1 << i);
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 		mc_cond = sbd_get_comp_cond(cur_mc_dip);
13940Sstevel@tonic-gate 		if (mc_cond == SBD_COND_FAILED) {
13950Sstevel@tonic-gate 			failed_mcs |= (1 << i);
13960Sstevel@tonic-gate 		}
13970Sstevel@tonic-gate 	}
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	if (failed_mcs == present_mcs) {
14000Sstevel@tonic-gate 		/*
14010Sstevel@tonic-gate 		 * All mem nodes failed, therefore mark all mem
14020Sstevel@tonic-gate 		 * as failed
14030Sstevel@tonic-gate 		 */
14040Sstevel@tonic-gate 		mp->sbm_cm.sbdev_cond = SBD_COND_FAILED;
14050Sstevel@tonic-gate 	} else {
14060Sstevel@tonic-gate 		mp->sbm_cm.sbdev_cond = SBD_COND_OK;
14070Sstevel@tonic-gate 	}
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	sbd_init_mem_unit_data(mp, ep);
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	/*
14120Sstevel@tonic-gate 	 * Any changes to this memory unit should be performed above
14130Sstevel@tonic-gate 	 * this call to ensure the unit is fully initialized
14140Sstevel@tonic-gate 	 * before transitioning to the new state.
14150Sstevel@tonic-gate 	 */
14160Sstevel@tonic-gate 	SBD_DEVICE_TRANSITION(sbp, SBD_COMP_MEM, unit, new_state);
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate }
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate static void
sbd_init_mem_unit_data(sbd_mem_unit_t * mp,sbderror_t * ep)14210Sstevel@tonic-gate sbd_init_mem_unit_data(sbd_mem_unit_t *mp, sbderror_t *ep)
14220Sstevel@tonic-gate {
14230Sstevel@tonic-gate 	uint64_t	basepa;
14240Sstevel@tonic-gate 	uint64_t	sz;
14250Sstevel@tonic-gate 	sbd_board_t	*sbp = mp->sbm_cm.sbdev_sbp;
14260Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
14270Sstevel@tonic-gate 	static fn_t	f = "sbd_init_mem_unit_data";
14280Sstevel@tonic-gate 	sbd_handle_t	*hp = MACHBD2HD(sbp);
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	/* a little sanity checking */
14330Sstevel@tonic-gate 	ASSERT(mp->sbm_peer == NULL);
14340Sstevel@tonic-gate 	ASSERT(mp->sbm_flags == 0);
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	/* get basepfn of mem unit */
14390Sstevel@tonic-gate 	if (sbdphw_get_base_physaddr(hdp, mp->sbm_cm.sbdev_dip, &basepa)) {
14400Sstevel@tonic-gate 		cmn_err(CE_WARN, "sbd:%s: failed to get physaddr"
14410Sstevel@tonic-gate 			" for mem-unit (%d.%d)",
14420Sstevel@tonic-gate 			f,
14430Sstevel@tonic-gate 			sbp->sb_num,
14440Sstevel@tonic-gate 			mp->sbm_cm.sbdev_unum);
14450Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
14460Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
14470Sstevel@tonic-gate 		return;
14480Sstevel@tonic-gate 	}
14490Sstevel@tonic-gate 	mp->sbm_basepfn = _b64top(basepa);
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	/* attempt to get number of pages from PDA */
14520Sstevel@tonic-gate 	mp->sbm_npages = sbdp_get_mem_size(hdp);
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	/* if didn't work, calculate using memlist */
14550Sstevel@tonic-gate 	if (mp->sbm_npages == 0) {
14560Sstevel@tonic-gate 		struct memlist	*ml, *mlist;
14570Sstevel@tonic-gate 		mlist = sbd_get_memlist(mp, ep);
1458*11474SJonathan.Adams@Sun.COM 		for (ml = mlist; ml; ml = ml->ml_next)
1459*11474SJonathan.Adams@Sun.COM 			mp->sbm_npages += btop(ml->ml_size);
14600Sstevel@tonic-gate 		memlist_delete(mlist);
14610Sstevel@tonic-gate 	}
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 	if (sbdp_get_mem_alignment(hdp, mp->sbm_cm.sbdev_dip, &sz)) {
14650Sstevel@tonic-gate 		cmn_err(CE_WARN,
14660Sstevel@tonic-gate 			"sbd:%s: no alignment for mem-unit (%d.%d)",
14670Sstevel@tonic-gate 			f, sbp->sb_num, mp->sbm_cm.sbdev_unum);
14680Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
14690Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
14700Sstevel@tonic-gate 		return;
14710Sstevel@tonic-gate 	}
14720Sstevel@tonic-gate 	mp->sbm_alignment_mask = _b64top(sz);
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	mp->sbm_interleave = sbdp_isinterleaved(hdp,
14760Sstevel@tonic-gate 	    mp->sbm_cm.sbdev_dip);
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	PR_MEM("%s: board %d (basepfn = 0x%lx, npgs = 0x%lx interleave %d)\n",
14790Sstevel@tonic-gate 		f, sbp->sb_num,
14800Sstevel@tonic-gate 		mp->sbm_basepfn,
14810Sstevel@tonic-gate 		mp->sbm_npages,
14820Sstevel@tonic-gate 		mp->sbm_interleave);
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
14850Sstevel@tonic-gate }
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate static int
sbd_reserve_mem_spans(memhandle_t * mhp,struct memlist * ml)14880Sstevel@tonic-gate sbd_reserve_mem_spans(memhandle_t *mhp, struct memlist *ml)
14890Sstevel@tonic-gate {
14900Sstevel@tonic-gate 	int		err;
14910Sstevel@tonic-gate 	pfn_t		base;
14920Sstevel@tonic-gate 	pgcnt_t		npgs;
14930Sstevel@tonic-gate 	struct memlist	*mc;
14940Sstevel@tonic-gate 	static fn_t	f = "sbd_reserve_mem_spans";
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	/*
14990Sstevel@tonic-gate 	 * Walk the supplied memlist scheduling each span for removal
15000Sstevel@tonic-gate 	 * with kphysm_del_span.  It is possible that a span may intersect
15010Sstevel@tonic-gate 	 * an area occupied by the cage.
15020Sstevel@tonic-gate 	 */
1503*11474SJonathan.Adams@Sun.COM 	for (mc = ml; mc != NULL; mc = mc->ml_next) {
1504*11474SJonathan.Adams@Sun.COM 		base = _b64top(mc->ml_address);
1505*11474SJonathan.Adams@Sun.COM 		npgs = _b64top(mc->ml_size);
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 		err = kphysm_del_span(*mhp, base, npgs);
15080Sstevel@tonic-gate 		if (err != KPHYSM_OK) {
15090Sstevel@tonic-gate 			cmn_err(CE_WARN, "sbd:%s memory reserve failed."
15100Sstevel@tonic-gate 				" unexpected kphysm_del_span return value %d;"
15110Sstevel@tonic-gate 				" basepfn=0x%lx npages=%ld",
15120Sstevel@tonic-gate 				f, err, base, npgs);
15130Sstevel@tonic-gate 			return (-1);
15140Sstevel@tonic-gate 		}
15150Sstevel@tonic-gate 	}
15160Sstevel@tonic-gate 	return (0);
15170Sstevel@tonic-gate }
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate /* debug counters */
15200Sstevel@tonic-gate int sbd_smt_realigned;
15210Sstevel@tonic-gate int sbd_smt_preference[4];
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate #ifdef DEBUG
15240Sstevel@tonic-gate uint_t sbd_ignore_board; /* if bit[bnum-1] set, board won't be candidate */
15250Sstevel@tonic-gate #endif
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate /*
15280Sstevel@tonic-gate  * Verify that there is no memory overlapping if copy-rename is
15290Sstevel@tonic-gate  * done with the selected target board.
15300Sstevel@tonic-gate  *
15310Sstevel@tonic-gate  * Returns 0 if OK, -1 otherwise.
15320Sstevel@tonic-gate  */
15330Sstevel@tonic-gate static int
sbd_check_boundaries(struct memlist * orig_memlist,sbd_mem_unit_t * s_mp,sbd_mem_unit_t * t_mp)15340Sstevel@tonic-gate sbd_check_boundaries(struct memlist *orig_memlist, sbd_mem_unit_t *s_mp,
15350Sstevel@tonic-gate 	sbd_mem_unit_t *t_mp)
15360Sstevel@tonic-gate {
15370Sstevel@tonic-gate 	struct memlist	*new_memlist;
15380Sstevel@tonic-gate 	int mlret;
15390Sstevel@tonic-gate 	static fn_t	f = "sbd_check_boundaries";
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	new_memlist = memlist_dup(orig_memlist);
15420Sstevel@tonic-gate 	if (new_memlist == NULL) {
15430Sstevel@tonic-gate 		PR_MEM("%s: can't dup original memlist\n", f);
15440Sstevel@tonic-gate 		return (-1);
15450Sstevel@tonic-gate 	}
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	mlret = memlist_delete_span(
15480Sstevel@tonic-gate 		_ptob64(s_mp->sbm_basepfn),
15490Sstevel@tonic-gate 		_ptob64(s_mp->sbm_npages),
15500Sstevel@tonic-gate 		&new_memlist);
15510Sstevel@tonic-gate 	if (mlret != MEML_SPANOP_OK) {
15520Sstevel@tonic-gate 		PR_MEM("%s: del s/s mlret = %d\n", f, mlret);
15530Sstevel@tonic-gate 		goto check_done;
15540Sstevel@tonic-gate 	}
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	mlret = memlist_delete_span(
15570Sstevel@tonic-gate 		_ptob64(t_mp->sbm_basepfn),
15580Sstevel@tonic-gate 		_ptob64(t_mp->sbm_npages),
15590Sstevel@tonic-gate 		&new_memlist);
15600Sstevel@tonic-gate 	if (mlret != MEML_SPANOP_OK) {
15610Sstevel@tonic-gate 		PR_MEM("%s: del t/t mlret = %d\n", f, mlret);
15620Sstevel@tonic-gate 		goto check_done;
15630Sstevel@tonic-gate 	}
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	mlret = memlist_add_span(
15660Sstevel@tonic-gate 		_ptob64(t_mp->sbm_basepfn),
15670Sstevel@tonic-gate 		_ptob64(s_mp->sbm_npages),
15680Sstevel@tonic-gate 		&new_memlist);
15690Sstevel@tonic-gate 	if (mlret != MEML_SPANOP_OK) {
15700Sstevel@tonic-gate 		PR_MEM("%s: add t/s mlret = %d\n", f, mlret);
15710Sstevel@tonic-gate 		goto check_done;
15720Sstevel@tonic-gate 	}
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	mlret = memlist_add_span(
15750Sstevel@tonic-gate 		_ptob64(s_mp->sbm_basepfn),
15760Sstevel@tonic-gate 		_ptob64(t_mp->sbm_npages),
15770Sstevel@tonic-gate 		&new_memlist);
15780Sstevel@tonic-gate 	if (mlret != MEML_SPANOP_OK) {
15790Sstevel@tonic-gate 		PR_MEM("%s: add s/t mlret = %d\n", f, mlret);
15800Sstevel@tonic-gate 	}
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate check_done:
15830Sstevel@tonic-gate 	memlist_delete(new_memlist);
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate 	if (mlret == MEML_SPANOP_OK)
15860Sstevel@tonic-gate 		return (0);
15870Sstevel@tonic-gate 	else
15880Sstevel@tonic-gate 		return (-1);
15890Sstevel@tonic-gate }
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate /*
15920Sstevel@tonic-gate  * Find and reserve a copy/rename target board suitable for the
15930Sstevel@tonic-gate  * given source board.
15940Sstevel@tonic-gate  * All boards in the system are examined and categorized in relation to
15950Sstevel@tonic-gate  * their memory size versus the source board's memory size.  Order of
15960Sstevel@tonic-gate  * preference is:
15970Sstevel@tonic-gate  *	1st: board has same memory size
15980Sstevel@tonic-gate  * 	2nd: board has larger memory size
15990Sstevel@tonic-gate  *	3rd: board has smaller memory size
16000Sstevel@tonic-gate  *	4th: board has smaller memory size, available memory will be reduced.
16010Sstevel@tonic-gate  * Boards in category 3 and 4 will have their MC's reprogrammed to locate the
16020Sstevel@tonic-gate  * span to which the MC responds to address span that appropriately covers
16030Sstevel@tonic-gate  * the nonrelocatable span of the source board.
16040Sstevel@tonic-gate  */
16050Sstevel@tonic-gate static int
sbd_select_mem_target(sbd_handle_t * hp,sbd_mem_unit_t * s_mp,struct memlist * s_ml)16060Sstevel@tonic-gate sbd_select_mem_target(sbd_handle_t *hp,
16070Sstevel@tonic-gate 	sbd_mem_unit_t *s_mp, struct memlist *s_ml)
16080Sstevel@tonic-gate {
16090Sstevel@tonic-gate 	uint64_t	sz;
16100Sstevel@tonic-gate 	pgcnt_t		sm;
16110Sstevel@tonic-gate 	int		n_sets = 4; /* same, larger, smaller, clipped */
16120Sstevel@tonic-gate 	int		preference; /* lower value is higher preference */
16130Sstevel@tonic-gate 	int		n_units_per_set;
16140Sstevel@tonic-gate 	int		idx;
16150Sstevel@tonic-gate 	sbd_mem_unit_t	**sets;
16160Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
16170Sstevel@tonic-gate 	int		t_bd;
16180Sstevel@tonic-gate 	sbd_softstate_t	*softsp;
16190Sstevel@tonic-gate 	int		t_unit;
16200Sstevel@tonic-gate 	int		max_boards;
16210Sstevel@tonic-gate 	int		rv;
16220Sstevel@tonic-gate 	sbd_board_t	*s_sbp, *t_sbp;
16230Sstevel@tonic-gate 	sbd_mem_unit_t	*t_mp, *c_mp;
16240Sstevel@tonic-gate 	struct memlist	*d_ml, *t_ml, *x_ml;
16250Sstevel@tonic-gate 	memquery_t	s_mq = {0};
16260Sstevel@tonic-gate 	static fn_t	f = "sbd_select_mem_target";
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	ASSERT(s_ml != NULL);
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	s_sbp = s_mp->sbm_cm.sbdev_sbp;
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(s_sbp, hp);
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	if (sbdp_get_mem_alignment(hdp, s_mp->sbm_cm.sbdev_dip, &sz)) {
16370Sstevel@tonic-gate 		sbderror_t	*ep = SBD_HD2ERR(hp);
16380Sstevel@tonic-gate 		cmn_err(CE_WARN,
16390Sstevel@tonic-gate 			"sbd:%s: no alignment for mem-unit (%d.%d)",
16400Sstevel@tonic-gate 			f, s_sbp->sb_num, s_mp->sbm_cm.sbdev_unum);
16410Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
16420Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
16430Sstevel@tonic-gate 		return (-1);
16440Sstevel@tonic-gate 	}
16450Sstevel@tonic-gate 	sm = sz - 1;
16460Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	softsp = (sbd_softstate_t *)s_sbp->sb_softsp;
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	max_boards = softsp->max_boards;
16510Sstevel@tonic-gate 	n_units_per_set = max_boards * MAX_MEM_UNITS_PER_BOARD;
16520Sstevel@tonic-gate 	sets = GETSTRUCT(sbd_mem_unit_t *, n_units_per_set * n_sets);
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 	/*
16550Sstevel@tonic-gate 	 * Make one pass through all memory units on all boards
16560Sstevel@tonic-gate 	 * and categorize them with respect to the source board.
16570Sstevel@tonic-gate 	 */
16580Sstevel@tonic-gate 	for (t_bd = 0; t_bd < max_boards; t_bd++) {
16590Sstevel@tonic-gate 		/*
16600Sstevel@tonic-gate 		 * The board structs are a contiguous array
16610Sstevel@tonic-gate 		 * so we take advantage of that to find the
16620Sstevel@tonic-gate 		 * correct board struct pointer for a given
16630Sstevel@tonic-gate 		 * board number.
16640Sstevel@tonic-gate 		 */
16650Sstevel@tonic-gate 		t_sbp = (sbd_board_t *)softsp->sbd_boardlist;
16660Sstevel@tonic-gate 		t_sbp += t_bd;
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 		/* source board can not be its own target */
16690Sstevel@tonic-gate 		if (s_sbp->sb_num == t_sbp->sb_num)
16700Sstevel@tonic-gate 			continue;
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 		for (t_unit = 0; t_unit < MAX_MEM_UNITS_PER_BOARD; t_unit++) {
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 			t_mp = SBD_GET_BOARD_MEMUNIT(t_sbp, t_unit);
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 			/* this memory node must be attached */
16770Sstevel@tonic-gate 			if (!SBD_DEV_IS_ATTACHED(t_sbp, SBD_COMP_MEM, t_unit))
16780Sstevel@tonic-gate 				continue;
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 			/* source unit can not be its own target */
16810Sstevel@tonic-gate 			if (s_mp == t_mp) {
16820Sstevel@tonic-gate 				/* catch this in debug kernels */
16830Sstevel@tonic-gate 				ASSERT(0);
16840Sstevel@tonic-gate 				continue;
16850Sstevel@tonic-gate 			}
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 			/*
16880Sstevel@tonic-gate 			 * this memory node must not already be reserved
16890Sstevel@tonic-gate 			 * by some other memory delete operation.
16900Sstevel@tonic-gate 			 */
16910Sstevel@tonic-gate 			if (t_mp->sbm_flags & SBD_MFLAG_RESERVED)
16920Sstevel@tonic-gate 				continue;
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 			/*
16950Sstevel@tonic-gate 			 * categorize the memory node
16960Sstevel@tonic-gate 			 * If this is a smaller memory node, create a
16970Sstevel@tonic-gate 			 * temporary, edited copy of the source board's
16980Sstevel@tonic-gate 			 * memlist containing only the span of the non-
16990Sstevel@tonic-gate 			 * relocatable pages.
17000Sstevel@tonic-gate 			 */
17010Sstevel@tonic-gate 			if (t_mp->sbm_npages == s_mp->sbm_npages) {
17020Sstevel@tonic-gate 				preference = 0;
17030Sstevel@tonic-gate 				t_mp->sbm_slice_offset = 0;
17040Sstevel@tonic-gate 			} else if (t_mp->sbm_npages > s_mp->sbm_npages) {
17050Sstevel@tonic-gate 				preference = 1;
17060Sstevel@tonic-gate 				t_mp->sbm_slice_offset = 0;
17070Sstevel@tonic-gate 			} else {
17080Sstevel@tonic-gate 				/*
17090Sstevel@tonic-gate 				 * We do not allow other options right now
17100Sstevel@tonic-gate 				 */
17110Sstevel@tonic-gate 				continue;
17120Sstevel@tonic-gate 			}
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 			sbd_smt_preference[preference]++;
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 			/* calculate index to start of preference set */
17170Sstevel@tonic-gate 			idx  = n_units_per_set * preference;
17180Sstevel@tonic-gate 			/* calculate offset to respective element */
17190Sstevel@tonic-gate 			idx += t_bd * MAX_MEM_UNITS_PER_BOARD + t_unit;
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 			ASSERT(idx < n_units_per_set * n_sets);
17220Sstevel@tonic-gate 			sets[idx] = t_mp;
17230Sstevel@tonic-gate 		}
17240Sstevel@tonic-gate 	}
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	/*
17270Sstevel@tonic-gate 	 * NOTE: this would be a good place to sort each candidate
17280Sstevel@tonic-gate 	 * set in to some desired order, e.g. memory size in ascending
17290Sstevel@tonic-gate 	 * order.  Without an additional sorting step here, the order
17300Sstevel@tonic-gate 	 * within a set is ascending board number order.
17310Sstevel@tonic-gate 	 */
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 	c_mp = NULL;
17340Sstevel@tonic-gate 	x_ml = NULL;
17350Sstevel@tonic-gate 	t_ml = NULL;
17360Sstevel@tonic-gate 	for (idx = 0; idx < n_units_per_set * n_sets; idx++) {
17370Sstevel@tonic-gate 		memquery_t mq;
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate 		/* cleanup t_ml after previous pass */
17400Sstevel@tonic-gate 		if (t_ml != NULL) {
17410Sstevel@tonic-gate 			memlist_delete(t_ml);
17420Sstevel@tonic-gate 			t_ml = NULL;
17430Sstevel@tonic-gate 		}
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 		/* get candidate target board mem unit */
17460Sstevel@tonic-gate 		t_mp = sets[idx];
17470Sstevel@tonic-gate 		if (t_mp == NULL)
17480Sstevel@tonic-gate 			continue;
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 		t_sbp = t_mp->sbm_cm.sbdev_sbp;
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 		/* get target board memlist */
17530Sstevel@tonic-gate 		t_ml = sbd_get_memlist(t_mp, SBD_HD2ERR(hp));
17540Sstevel@tonic-gate 		if (t_ml == NULL) {
17550Sstevel@tonic-gate 			cmn_err(CE_WARN, "sbd:%s: no memlist for"
17560Sstevel@tonic-gate 				" mem-unit %d, board %d",
17570Sstevel@tonic-gate 				f,
17580Sstevel@tonic-gate 				t_sbp->sb_num,
17590Sstevel@tonic-gate 				t_mp->sbm_cm.sbdev_unum);
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 			continue;
17620Sstevel@tonic-gate 		}
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate 		/* get appropriate source board memlist */
17650Sstevel@tonic-gate 		if (t_mp->sbm_npages < s_mp->sbm_npages) {
17660Sstevel@tonic-gate 			spgcnt_t excess;
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 			/*
17690Sstevel@tonic-gate 			 * make a copy of the source board memlist
17700Sstevel@tonic-gate 			 * then edit it to remove the spans that
17710Sstevel@tonic-gate 			 * are outside the calculated span of
17720Sstevel@tonic-gate 			 * [pfn..s_mq.last_nonrelocatable].
17730Sstevel@tonic-gate 			 */
17740Sstevel@tonic-gate 			if (x_ml != NULL)
17750Sstevel@tonic-gate 				memlist_delete(x_ml);
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 			x_ml = memlist_dup(s_ml);
17780Sstevel@tonic-gate 			if (x_ml == NULL) {
17790Sstevel@tonic-gate 				PR_MEM("%s: memlist_dup failed\n", f);
17800Sstevel@tonic-gate 				/* TODO: should abort */
17810Sstevel@tonic-gate 				continue;
17820Sstevel@tonic-gate 			}
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 			/* trim off lower portion */
17850Sstevel@tonic-gate 			excess = t_mp->sbm_slice_offset;
17860Sstevel@tonic-gate 			if (excess > 0) {
17870Sstevel@tonic-gate 				int mlret;
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 				mlret = memlist_delete_span(
17900Sstevel@tonic-gate 					_ptob64(s_mp->sbm_basepfn),
17910Sstevel@tonic-gate 					_ptob64(excess),
17920Sstevel@tonic-gate 					&x_ml);
17930Sstevel@tonic-gate 				PR_MEM("%s: mlret = %d\n", f, mlret);
17940Sstevel@tonic-gate 			}
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate 			/*
17970Sstevel@tonic-gate 			 * Since this candidate target board is smaller
17980Sstevel@tonic-gate 			 * than the source board, s_mq must have been
17990Sstevel@tonic-gate 			 * initialized in previous loop while processing
18000Sstevel@tonic-gate 			 * this or some other candidate board.
18010Sstevel@tonic-gate 			 * FIXME: this is weak.
18020Sstevel@tonic-gate 			 */
18030Sstevel@tonic-gate 			ASSERT(s_mq.phys_pages != 0);
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 			/* trim off upper portion */
18060Sstevel@tonic-gate 			excess = (s_mp->sbm_basepfn + s_mp->sbm_npages)
18070Sstevel@tonic-gate 				- (s_mq.last_nonrelocatable + 1);
18080Sstevel@tonic-gate 			if (excess > 0) {
18090Sstevel@tonic-gate 				pfn_t p;
18100Sstevel@tonic-gate 				int mlret;
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate 				p  = s_mq.last_nonrelocatable + 1;
18130Sstevel@tonic-gate 				p -= excess;
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate 				mlret = memlist_delete_span(
18160Sstevel@tonic-gate 					_ptob64(p),
18170Sstevel@tonic-gate 					_ptob64(excess),
18180Sstevel@tonic-gate 					&x_ml);
18190Sstevel@tonic-gate 				PR_MEM("%s: mlret = %d\n", f, mlret);
18200Sstevel@tonic-gate 			}
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 			PR_MEM("%s: brd %d: edited source memlist:\n",
18230Sstevel@tonic-gate 				f, s_sbp->sb_num);
18240Sstevel@tonic-gate 			SBD_MEMLIST_DUMP(x_ml);
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate #ifdef DEBUG
18270Sstevel@tonic-gate 			/* sanity check memlist */
18280Sstevel@tonic-gate 			d_ml = x_ml;
1829*11474SJonathan.Adams@Sun.COM 			while (d_ml->ml_next != NULL)
1830*11474SJonathan.Adams@Sun.COM 				d_ml = d_ml->ml_next;
1831*11474SJonathan.Adams@Sun.COM 			ASSERT(x_ml->ml_address == _ptob64(s_mp->sbm_basepfn) +
18320Sstevel@tonic-gate 				_ptob64(t_mp->sbm_slice_offset));
1833*11474SJonathan.Adams@Sun.COM 			ASSERT(d_ml->ml_address + d_ml->ml_size ==
18340Sstevel@tonic-gate 				_ptob64(s_mq.last_nonrelocatable + 1));
18350Sstevel@tonic-gate #endif
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 			/*
18380Sstevel@tonic-gate 			 * x_ml now describes only the portion of the
18390Sstevel@tonic-gate 			 * source board that will be moved during the
18400Sstevel@tonic-gate 			 * copy/rename operation.
18410Sstevel@tonic-gate 			 */
18420Sstevel@tonic-gate 			d_ml = x_ml;
18430Sstevel@tonic-gate 		} else {
18440Sstevel@tonic-gate 			/* use original memlist; all spans will be moved */
18450Sstevel@tonic-gate 			d_ml = s_ml;
18460Sstevel@tonic-gate 		}
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 		/* verify target can support source memory spans. */
18490Sstevel@tonic-gate 		if (memlist_canfit(d_ml, t_ml) == 0) {
18500Sstevel@tonic-gate 			PR_MEM("%s: source memlist won't"
18510Sstevel@tonic-gate 				" fit in target memlist\n", f);
18520Sstevel@tonic-gate 			PR_MEM("%s: source memlist:\n", f);
18530Sstevel@tonic-gate 			SBD_MEMLIST_DUMP(d_ml);
18540Sstevel@tonic-gate 			PR_MEM("%s: target memlist:\n", f);
18550Sstevel@tonic-gate 			SBD_MEMLIST_DUMP(t_ml);
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 			continue;
18580Sstevel@tonic-gate 		}
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 		/* NOTE: the value of d_ml is not used beyond this point */
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate 		PR_MEM("%s: checking for no-reloc on board %d, "
18630Sstevel@tonic-gate 			" basepfn=0x%lx, npages=%ld\n",
18640Sstevel@tonic-gate 			f,
18650Sstevel@tonic-gate 			t_sbp->sb_num,
18660Sstevel@tonic-gate 			t_mp->sbm_basepfn,
18670Sstevel@tonic-gate 			t_mp->sbm_npages);
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 		rv = kphysm_del_span_query(
18700Sstevel@tonic-gate 			t_mp->sbm_basepfn, t_mp->sbm_npages, &mq);
18710Sstevel@tonic-gate 		if (rv != KPHYSM_OK) {
18720Sstevel@tonic-gate 			PR_MEM("%s: kphysm_del_span_query:"
18730Sstevel@tonic-gate 				" unexpected return value %d\n", f, rv);
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate 			continue;
18760Sstevel@tonic-gate 		}
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 		if (mq.nonrelocatable != 0) {
18790Sstevel@tonic-gate 			PR_MEM("%s: candidate board %d has"
18800Sstevel@tonic-gate 				" nonrelocatable span [0x%lx..0x%lx]\n",
18810Sstevel@tonic-gate 				f,
18820Sstevel@tonic-gate 				t_sbp->sb_num,
18830Sstevel@tonic-gate 				mq.first_nonrelocatable,
18840Sstevel@tonic-gate 				mq.last_nonrelocatable);
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 			continue;
18870Sstevel@tonic-gate 		}
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate #ifdef DEBUG
18900Sstevel@tonic-gate 		/*
18910Sstevel@tonic-gate 		 * This is a debug tool for excluding certain boards
18920Sstevel@tonic-gate 		 * from being selected as a target board candidate.
18930Sstevel@tonic-gate 		 * sbd_ignore_board is only tested by this driver.
18940Sstevel@tonic-gate 		 * It must be set with adb, obp, /etc/system or your
18950Sstevel@tonic-gate 		 * favorite debugger.
18960Sstevel@tonic-gate 		 */
18970Sstevel@tonic-gate 		if (sbd_ignore_board &
18980Sstevel@tonic-gate 			(1 << (t_sbp->sb_num - 1))) {
18990Sstevel@tonic-gate 			PR_MEM("%s: sbd_ignore_board flag set,"
19000Sstevel@tonic-gate 				" ignoring board %d as candidate\n",
19010Sstevel@tonic-gate 				f, t_sbp->sb_num);
19020Sstevel@tonic-gate 			continue;
19030Sstevel@tonic-gate 		}
19040Sstevel@tonic-gate #endif
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 		/*
19070Sstevel@tonic-gate 		 * Make sure there is no memory overlap if this
19080Sstevel@tonic-gate 		 * target board is used for copy-rename.
19090Sstevel@tonic-gate 		 */
19100Sstevel@tonic-gate 		if (sbd_check_boundaries(phys_install, s_mp, t_mp) != 0)
19110Sstevel@tonic-gate 			continue;
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 		/*
19140Sstevel@tonic-gate 		 * Reserve excess source board memory, if any.
19150Sstevel@tonic-gate 		 *
19160Sstevel@tonic-gate 		 * When the number of pages on the candidate target
19170Sstevel@tonic-gate 		 * board is less than the number of pages on the source,
19180Sstevel@tonic-gate 		 * then some spans (clearly) of the source board's address
19190Sstevel@tonic-gate 		 * space will not be covered by physical memory after the
19200Sstevel@tonic-gate 		 * copy/rename completes.  The following code block
19210Sstevel@tonic-gate 		 * schedules those spans to be deleted.
19220Sstevel@tonic-gate 		 */
19230Sstevel@tonic-gate 		if (t_mp->sbm_npages < s_mp->sbm_npages) {
19240Sstevel@tonic-gate 			pfn_t pfn;
19250Sstevel@tonic-gate 			int mlret;
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 			d_ml = memlist_dup(s_ml);
19280Sstevel@tonic-gate 			if (d_ml == NULL) {
19290Sstevel@tonic-gate 				PR_MEM("%s: cant dup src brd memlist\n", f);
19300Sstevel@tonic-gate 				/* TODO: should abort */
19310Sstevel@tonic-gate 				continue;
19320Sstevel@tonic-gate 			}
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 			/* calculate base pfn relative to target board */
19350Sstevel@tonic-gate 			pfn  = s_mp->sbm_basepfn & ~sm;
19360Sstevel@tonic-gate 			pfn += t_mp->sbm_slice_offset;
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 			/* remove span that will reside on candidate board */
19390Sstevel@tonic-gate 			mlret = memlist_delete_span(
19400Sstevel@tonic-gate 				_ptob64(pfn),
19410Sstevel@tonic-gate 				_ptob64(t_mp->sbm_npages),
19420Sstevel@tonic-gate 				&d_ml);
19430Sstevel@tonic-gate 			PR_MEM("%s: mlret = %d\n", f, mlret);
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 			PR_MEM("%s: brd %d: reserving src brd memlist:\n",
19460Sstevel@tonic-gate 				f, s_sbp->sb_num);
19470Sstevel@tonic-gate 			SBD_MEMLIST_DUMP(d_ml);
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 			/* reserve excess spans */
19500Sstevel@tonic-gate 			if (sbd_reserve_mem_spans(
19510Sstevel@tonic-gate 				&s_mp->sbm_memhandle, d_ml) != 0) {
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 				/* likely more non-reloc pages appeared */
19540Sstevel@tonic-gate 				/* TODO: restart from top? */
19550Sstevel@tonic-gate 				continue;
19560Sstevel@tonic-gate 			}
19570Sstevel@tonic-gate 		} else {
19580Sstevel@tonic-gate 			/* no excess source board memory */
19590Sstevel@tonic-gate 			d_ml = NULL;
19600Sstevel@tonic-gate 		}
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate 		s_mp->sbm_flags |= SBD_MFLAG_RESERVED;
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate 		/*
19650Sstevel@tonic-gate 		 * reserve all memory on target board.
19660Sstevel@tonic-gate 		 * NOTE: source board's memhandle is used.
19670Sstevel@tonic-gate 		 *
19680Sstevel@tonic-gate 		 * If this succeeds (eq 0), then target selection is
19690Sstevel@tonic-gate 		 * complete and all unwanted memory spans, both source and
19700Sstevel@tonic-gate 		 * target, have been reserved.  Loop is terminated.
19710Sstevel@tonic-gate 		 */
19720Sstevel@tonic-gate 		if (sbd_reserve_mem_spans(&s_mp->sbm_memhandle, t_ml) == 0) {
19730Sstevel@tonic-gate 			PR_MEM("%s: brd %d: target board memory reserved\n",
19740Sstevel@tonic-gate 				f, t_sbp->sb_num);
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 			/* a candidate target board is now reserved */
19770Sstevel@tonic-gate 			t_mp->sbm_flags |= SBD_MFLAG_RESERVED;
19780Sstevel@tonic-gate 			c_mp = t_mp;
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate 			/* *** EXITING LOOP *** */
19810Sstevel@tonic-gate 			break;
19820Sstevel@tonic-gate 		}
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate 		/* did not successfully reserve the target board. */
19850Sstevel@tonic-gate 		PR_MEM("%s: could not reserve target board %d\n",
19860Sstevel@tonic-gate 			f, t_sbp->sb_num);
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 		/*
19890Sstevel@tonic-gate 		 * NOTE: an undo of the sbd_reserve_mem_span work
19900Sstevel@tonic-gate 		 * will happen automatically when the memhandle
19910Sstevel@tonic-gate 		 * (s_mp->sbm_memhandle) is kphysm_del_release'd.
19920Sstevel@tonic-gate 		 */
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 		s_mp->sbm_flags &= ~SBD_MFLAG_RESERVED;
19950Sstevel@tonic-gate 	}
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 	/* clean up after memlist editing logic */
19980Sstevel@tonic-gate 	if (x_ml != NULL)
19990Sstevel@tonic-gate 		memlist_delete(x_ml);
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 	FREESTRUCT(sets, sbd_mem_unit_t *, n_units_per_set * n_sets);
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	/*
20040Sstevel@tonic-gate 	 * c_mp will be NULL when the entire sets[] array
20050Sstevel@tonic-gate 	 * has been searched without reserving a target board.
20060Sstevel@tonic-gate 	 */
20070Sstevel@tonic-gate 	if (c_mp == NULL) {
20080Sstevel@tonic-gate 		PR_MEM("%s: brd %d: target selection failed.\n",
20090Sstevel@tonic-gate 			f, s_sbp->sb_num);
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 		if (t_ml != NULL)
20120Sstevel@tonic-gate 			memlist_delete(t_ml);
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate 		return (-1);
20150Sstevel@tonic-gate 	}
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	PR_MEM("%s: found target board %d for source board %d\n",
20180Sstevel@tonic-gate 		f,
20190Sstevel@tonic-gate 		t_sbp->sb_num,
20200Sstevel@tonic-gate 		s_sbp->sb_num);
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 	s_mp->sbm_peer = c_mp;
20230Sstevel@tonic-gate 	s_mp->sbm_flags |= SBD_MFLAG_SOURCE;
20240Sstevel@tonic-gate 	s_mp->sbm_del_mlist = d_ml;	/* spans to be deleted, if any */
20250Sstevel@tonic-gate 	s_mp->sbm_mlist = s_ml;
20260Sstevel@tonic-gate 	s_mp->sbm_cm.sbdev_busy = 1;
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 	c_mp->sbm_peer = s_mp;
20290Sstevel@tonic-gate 	c_mp->sbm_flags |= SBD_MFLAG_TARGET;
20300Sstevel@tonic-gate 	c_mp->sbm_del_mlist = t_ml;	/* spans to be deleted */
20310Sstevel@tonic-gate 	c_mp->sbm_mlist = t_ml;
20320Sstevel@tonic-gate 	c_mp->sbm_cm.sbdev_busy = 1;
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 	s_mp->sbm_flags &= ~SBD_MFLAG_MEMRESIZE;
20350Sstevel@tonic-gate 	if (c_mp->sbm_npages > s_mp->sbm_npages) {
20360Sstevel@tonic-gate 		s_mp->sbm_flags |= SBD_MFLAG_MEMUPSIZE;
20370Sstevel@tonic-gate 		PR_MEM("%s: upsize (source pgs 0x%lx < target pgs 0x%lx)\n",
20380Sstevel@tonic-gate 			f, s_mp->sbm_npages, c_mp->sbm_npages);
20390Sstevel@tonic-gate 	} else if (c_mp->sbm_npages < s_mp->sbm_npages) {
20400Sstevel@tonic-gate 		s_mp->sbm_flags |= SBD_MFLAG_MEMDOWNSIZE;
20410Sstevel@tonic-gate 		PR_MEM("%s: downsize (source pgs 0x%lx > target pgs 0x%lx)\n",
20420Sstevel@tonic-gate 			f, s_mp->sbm_npages, c_mp->sbm_npages);
20430Sstevel@tonic-gate 	}
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 	return (0);
20460Sstevel@tonic-gate }
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate int
sbd_move_memory(sbd_handle_t * hp,sbd_board_t * s_bp,sbd_board_t * t_bp)20490Sstevel@tonic-gate sbd_move_memory(sbd_handle_t *hp, sbd_board_t *s_bp, sbd_board_t *t_bp)
20500Sstevel@tonic-gate {
20510Sstevel@tonic-gate 	int	ret;
20520Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
20530Sstevel@tonic-gate 	sbderror_t	*ep = SBD_HD2ERR(hp);
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(s_bp, hp);
20560Sstevel@tonic-gate 
20570Sstevel@tonic-gate 	ret = sbdp_move_memory(hdp, t_bp->sb_num);
20580Sstevel@tonic-gate 	if (ret != 0)
20590Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 	return (ret);
20640Sstevel@tonic-gate }
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate /*
20670Sstevel@tonic-gate  * Memlist support.
20680Sstevel@tonic-gate  */
20690Sstevel@tonic-gate void
memlist_delete(struct memlist * mlist)20700Sstevel@tonic-gate memlist_delete(struct memlist *mlist)
20710Sstevel@tonic-gate {
20720Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(NULL, NULL);
20750Sstevel@tonic-gate 
20760Sstevel@tonic-gate 	(void) sbdp_del_memlist(hdp, mlist);
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
20790Sstevel@tonic-gate }
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate struct memlist *
memlist_dup(struct memlist * mlist)20820Sstevel@tonic-gate memlist_dup(struct memlist *mlist)
20830Sstevel@tonic-gate {
20840Sstevel@tonic-gate 	struct memlist *hl, *prev;
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	if (mlist == NULL)
20870Sstevel@tonic-gate 		return (NULL);
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 	prev = NULL;
20900Sstevel@tonic-gate 	hl = NULL;
2091*11474SJonathan.Adams@Sun.COM 	for (; mlist; mlist = mlist->ml_next) {
20920Sstevel@tonic-gate 		struct memlist *mp;
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 		mp = memlist_get_one();
20950Sstevel@tonic-gate 		if (mp == NULL) {
20960Sstevel@tonic-gate 			if (hl != NULL)
20970Sstevel@tonic-gate 				memlist_free_list(hl);
20980Sstevel@tonic-gate 			hl = NULL;
20990Sstevel@tonic-gate 			break;
21000Sstevel@tonic-gate 		}
2101*11474SJonathan.Adams@Sun.COM 		mp->ml_address = mlist->ml_address;
2102*11474SJonathan.Adams@Sun.COM 		mp->ml_size = mlist->ml_size;
2103*11474SJonathan.Adams@Sun.COM 		mp->ml_next = NULL;
2104*11474SJonathan.Adams@Sun.COM 		mp->ml_prev = prev;
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 		if (prev == NULL)
21070Sstevel@tonic-gate 			hl = mp;
21080Sstevel@tonic-gate 		else
2109*11474SJonathan.Adams@Sun.COM 			prev->ml_next = mp;
21100Sstevel@tonic-gate 		prev = mp;
21110Sstevel@tonic-gate 	}
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 	return (hl);
21140Sstevel@tonic-gate }
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate void
memlist_dump(struct memlist * mlist)21170Sstevel@tonic-gate memlist_dump(struct memlist *mlist)
21180Sstevel@tonic-gate {
21190Sstevel@tonic-gate 	register struct memlist *ml;
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 	if (mlist == NULL) {
21220Sstevel@tonic-gate 		PR_MEM("memlist> EMPTY\n");
21230Sstevel@tonic-gate 	} else {
2124*11474SJonathan.Adams@Sun.COM 		for (ml = mlist; ml; ml = ml->ml_next)
21250Sstevel@tonic-gate 			PR_MEM("memlist> 0x%" PRIx64 " "
21260Sstevel@tonic-gate 				"0x%" PRIx64 " \n",
2127*11474SJonathan.Adams@Sun.COM 				ml->ml_address, ml->ml_size);
21280Sstevel@tonic-gate 	}
21290Sstevel@tonic-gate }
21300Sstevel@tonic-gate 
21310Sstevel@tonic-gate int
memlist_intersect(struct memlist * al,struct memlist * bl)21320Sstevel@tonic-gate memlist_intersect(struct memlist *al, struct memlist *bl)
21330Sstevel@tonic-gate {
21340Sstevel@tonic-gate 	uint64_t	astart, aend, bstart, bend;
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 	if ((al == NULL) || (bl == NULL))
21370Sstevel@tonic-gate 		return (0);
21380Sstevel@tonic-gate 
2139*11474SJonathan.Adams@Sun.COM 	aend = al->ml_address + al->ml_size;
2140*11474SJonathan.Adams@Sun.COM 	bstart = bl->ml_address;
2141*11474SJonathan.Adams@Sun.COM 	bend = bl->ml_address + bl->ml_size;
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 	while (al && bl) {
21440Sstevel@tonic-gate 		while (al && (aend <= bstart))
2145*11474SJonathan.Adams@Sun.COM 			if ((al = al->ml_next) != NULL)
2146*11474SJonathan.Adams@Sun.COM 				aend = al->ml_address + al->ml_size;
21470Sstevel@tonic-gate 		if (al == NULL)
21480Sstevel@tonic-gate 			return (0);
21490Sstevel@tonic-gate 
2150*11474SJonathan.Adams@Sun.COM 		if ((astart = al->ml_address) <= bstart)
21510Sstevel@tonic-gate 			return (1);
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 		while (bl && (bend <= astart))
2154*11474SJonathan.Adams@Sun.COM 			if ((bl = bl->ml_next) != NULL)
2155*11474SJonathan.Adams@Sun.COM 				bend = bl->ml_address + bl->ml_size;
21560Sstevel@tonic-gate 		if (bl == NULL)
21570Sstevel@tonic-gate 			return (0);
21580Sstevel@tonic-gate 
2159*11474SJonathan.Adams@Sun.COM 		if ((bstart = bl->ml_address) <= astart)
21600Sstevel@tonic-gate 			return (1);
21610Sstevel@tonic-gate 	}
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 	return (0);
21640Sstevel@tonic-gate }
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate /*
21670Sstevel@tonic-gate  * Determine whether the source memlist (s_mlist) will
21680Sstevel@tonic-gate  * fit into the target memlist (t_mlist) in terms of
21690Sstevel@tonic-gate  * size and holes (i.e. based on same relative base address).
21700Sstevel@tonic-gate  */
21710Sstevel@tonic-gate static int
memlist_canfit(struct memlist * s_mlist,struct memlist * t_mlist)21720Sstevel@tonic-gate memlist_canfit(struct memlist *s_mlist, struct memlist *t_mlist)
21730Sstevel@tonic-gate {
21740Sstevel@tonic-gate 	int		rv = 0;
21750Sstevel@tonic-gate 	uint64_t	s_basepa, t_basepa;
21760Sstevel@tonic-gate 	struct memlist	*s_ml, *t_ml;
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate 	if ((s_mlist == NULL) || (t_mlist == NULL))
21790Sstevel@tonic-gate 		return (0);
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 	/*
21820Sstevel@tonic-gate 	 * Base both memlists on common base address (0).
21830Sstevel@tonic-gate 	 */
2184*11474SJonathan.Adams@Sun.COM 	s_basepa = s_mlist->ml_address;
2185*11474SJonathan.Adams@Sun.COM 	t_basepa = t_mlist->ml_address;
2186*11474SJonathan.Adams@Sun.COM 
2187*11474SJonathan.Adams@Sun.COM 	for (s_ml = s_mlist; s_ml; s_ml = s_ml->ml_next)
2188*11474SJonathan.Adams@Sun.COM 		s_ml->ml_address -= s_basepa;
2189*11474SJonathan.Adams@Sun.COM 
2190*11474SJonathan.Adams@Sun.COM 	for (t_ml = t_mlist; t_ml; t_ml = t_ml->ml_next)
2191*11474SJonathan.Adams@Sun.COM 		t_ml->ml_address -= t_basepa;
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate 	s_ml = s_mlist;
2194*11474SJonathan.Adams@Sun.COM 	for (t_ml = t_mlist; t_ml && s_ml; t_ml = t_ml->ml_next) {
21950Sstevel@tonic-gate 		uint64_t	s_start, s_end;
21960Sstevel@tonic-gate 		uint64_t	t_start, t_end;
21970Sstevel@tonic-gate 
2198*11474SJonathan.Adams@Sun.COM 		t_start = t_ml->ml_address;
2199*11474SJonathan.Adams@Sun.COM 		t_end = t_start + t_ml->ml_size;
2200*11474SJonathan.Adams@Sun.COM 
2201*11474SJonathan.Adams@Sun.COM 		for (; s_ml; s_ml = s_ml->ml_next) {
2202*11474SJonathan.Adams@Sun.COM 			s_start = s_ml->ml_address;
2203*11474SJonathan.Adams@Sun.COM 			s_end = s_start + s_ml->ml_size;
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 			if ((s_start < t_start) || (s_end > t_end))
22060Sstevel@tonic-gate 				break;
22070Sstevel@tonic-gate 		}
22080Sstevel@tonic-gate 	}
22090Sstevel@tonic-gate 	/*
22100Sstevel@tonic-gate 	 * If we ran out of source memlist chunks that mean
22110Sstevel@tonic-gate 	 * we found a home for all of them.
22120Sstevel@tonic-gate 	 */
22130Sstevel@tonic-gate 	if (s_ml == NULL)
22140Sstevel@tonic-gate 		rv = 1;
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 	/*
22170Sstevel@tonic-gate 	 * Need to add base addresses back since memlists
22180Sstevel@tonic-gate 	 * are probably in use by caller.
22190Sstevel@tonic-gate 	 */
2220*11474SJonathan.Adams@Sun.COM 	for (s_ml = s_mlist; s_ml; s_ml = s_ml->ml_next)
2221*11474SJonathan.Adams@Sun.COM 		s_ml->ml_address += s_basepa;
2222*11474SJonathan.Adams@Sun.COM 
2223*11474SJonathan.Adams@Sun.COM 	for (t_ml = t_mlist; t_ml; t_ml = t_ml->ml_next)
2224*11474SJonathan.Adams@Sun.COM 		t_ml->ml_address += t_basepa;
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate 	return (rv);
22270Sstevel@tonic-gate }
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate void
sbd_attach_mem(sbd_handle_t * hp,sbderror_t * ep)22300Sstevel@tonic-gate sbd_attach_mem(sbd_handle_t *hp, sbderror_t *ep)
22310Sstevel@tonic-gate {
22320Sstevel@tonic-gate 	sbd_mem_unit_t	*mp;
22330Sstevel@tonic-gate 	dev_info_t	*dip;
22340Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
22350Sstevel@tonic-gate 	sbdp_handle_t	*hdp;
22360Sstevel@tonic-gate 	int		err, unit;
22370Sstevel@tonic-gate 	struct memlist	*ml, *mc;
22380Sstevel@tonic-gate 	static fn_t	f = "sbd_attach_mem";
22390Sstevel@tonic-gate 	int		i;
22400Sstevel@tonic-gate 
22410Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 	/*
22440Sstevel@tonic-gate 	 * all four cpus have to be attached before
22450Sstevel@tonic-gate 	 * configuring mem
22460Sstevel@tonic-gate 	 */
22470Sstevel@tonic-gate 	for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
22480Sstevel@tonic-gate 		sbd_cpu_unit_t	*cpup;
22490Sstevel@tonic-gate 		struct cpu	*cp;
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 		if (!SBD_DEV_IS_PRESENT(sbp, SBD_COMP_CPU, i))
22520Sstevel@tonic-gate 			continue;
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate 		if (!SBD_DEV_IS_ATTACHED(sbp, SBD_COMP_CPU, i))
22550Sstevel@tonic-gate 			goto error;
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 		cpup = SBD_GET_BOARD_CPUUNIT(sbp, i);
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 		if (cpup == NULL)
22600Sstevel@tonic-gate 			goto error;
22610Sstevel@tonic-gate 
22620Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
22630Sstevel@tonic-gate 		cp = cpu_get(cpup->sbc_cpu_id);
22640Sstevel@tonic-gate 		if (cp == NULL) {
22650Sstevel@tonic-gate 			mutex_exit(&cpu_lock);
22660Sstevel@tonic-gate 			cmn_err(CE_WARN,
22670Sstevel@tonic-gate 			    "sbd:%s: cpu_get failed for cpu %d",
22680Sstevel@tonic-gate 			    f, cpup->sbc_cpu_id);
22690Sstevel@tonic-gate 			goto error;
22700Sstevel@tonic-gate 		}
22710Sstevel@tonic-gate 		if (cpu_is_poweredoff(cp)) {
22720Sstevel@tonic-gate 			mutex_exit(&cpu_lock);
22730Sstevel@tonic-gate 			goto error;
22740Sstevel@tonic-gate 		}
22750Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
22760Sstevel@tonic-gate 		continue;
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate error:
22790Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_CPUONLINE);
22800Sstevel@tonic-gate 		SBD_SET_ERRSTR(ep, sbp->sb_mempath[i]);
22810Sstevel@tonic-gate 		(void) sbd_set_err_in_hdl(hp, ep);
22820Sstevel@tonic-gate 		return;
22830Sstevel@tonic-gate 	}
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 	dip = *(sbp->sb_devlist[NIX(SBD_COMP_MEM)]);
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
22880Sstevel@tonic-gate 	unit = sbdp_get_unit_num(hdp, dip);
22890Sstevel@tonic-gate 	if (unit < 0) {
22900Sstevel@tonic-gate 		SBD_GET_PERR(hdp->h_err, ep);
22910Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
22920Sstevel@tonic-gate 		return;
22930Sstevel@tonic-gate 	}
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate 	ASSERT(sbp->sb_mempath[unit] != NULL);
22960Sstevel@tonic-gate 	ASSERT(e_ddi_branch_held(dip));
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 	(void) ddi_pathname(dip, sbp->sb_mempath[unit]);
22990Sstevel@tonic-gate 
23000Sstevel@tonic-gate 	mp = SBD_GET_BOARD_MEMUNIT(sbp, unit);
23010Sstevel@tonic-gate 
23020Sstevel@tonic-gate 	ml = sbd_get_memlist(mp, ep);
23030Sstevel@tonic-gate 	if (ml == NULL) {
23040Sstevel@tonic-gate 		cmn_err(CE_WARN,
23050Sstevel@tonic-gate 			"sbd:%s: failed to get memlist for "
23060Sstevel@tonic-gate 			"board %d", f, sbp->sb_num);
23070Sstevel@tonic-gate 		/*
23080Sstevel@tonic-gate 		 * Need to record an error and return.
23090Sstevel@tonic-gate 		 */
23100Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_MEMFAIL);
23110Sstevel@tonic-gate 		SBD_SET_ERRSTR(ep, sbp->sb_mempath[unit]);
23120Sstevel@tonic-gate 		sbd_release_sbdp_handle(hdp);
23130Sstevel@tonic-gate 		return;
23140Sstevel@tonic-gate 	}
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 	SBD_MEMLIST_DUMP(ml);
23170Sstevel@tonic-gate 	err = 0;
2318*11474SJonathan.Adams@Sun.COM 	for (mc = ml; mc; mc = mc->ml_next) {
23190Sstevel@tonic-gate 		update_membounds_t umb;
23200Sstevel@tonic-gate 		pfn_t	base;
23210Sstevel@tonic-gate 		pgcnt_t npgs;
23220Sstevel@tonic-gate 
2323*11474SJonathan.Adams@Sun.COM 		base = (pfn_t)(mc->ml_address >> PAGESHIFT);
2324*11474SJonathan.Adams@Sun.COM 		npgs = (pgcnt_t)(mc->ml_size >> PAGESHIFT);
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate 		umb.u_board = sbp->sb_num;
23270Sstevel@tonic-gate 		umb.u_base = (uint64_t)base << MMU_PAGESHIFT;
23280Sstevel@tonic-gate 		umb.u_len = (uint64_t)npgs << MMU_PAGESHIFT;
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 		lgrp_plat_config(LGRP_CONFIG_MEM_ADD, (uintptr_t)&umb);
23310Sstevel@tonic-gate 		err = kphysm_add_memory_dynamic(base, npgs);
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 		if (err != KPHYSM_OK) {
23340Sstevel@tonic-gate 			cmn_err(CE_WARN,
23350Sstevel@tonic-gate 			    "%s: kphysm_add_memory_dynamic fail %d", f, err);
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate 			/* translate kphysm error */
23380Sstevel@tonic-gate 			switch (err) {
23390Sstevel@tonic-gate 			case KPHYSM_ERESOURCE:
23400Sstevel@tonic-gate 				err = ESBD_NOMEM;
23410Sstevel@tonic-gate 				break;
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 			case KPHYSM_EFAULT:
23440Sstevel@tonic-gate 				err = ESBD_FAULT;
23450Sstevel@tonic-gate 				break;
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 			default:
23480Sstevel@tonic-gate 				err = ESBD_INVAL;
23490Sstevel@tonic-gate 				break;
23500Sstevel@tonic-gate 			}
23510Sstevel@tonic-gate 			break;
23520Sstevel@tonic-gate 		}
23530Sstevel@tonic-gate 
23544266Sdp78419 		err = kcage_range_add(base, npgs, KCAGE_DOWN);
23550Sstevel@tonic-gate 		if (err != 0) {
23560Sstevel@tonic-gate 			cmn_err(CE_WARN,
23570Sstevel@tonic-gate 			    "%s: kcage_range_add fail %d", f, err);
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 			/* Translate kcage error. */
23600Sstevel@tonic-gate 			switch (err) {
23610Sstevel@tonic-gate 			case ENOMEM:
23620Sstevel@tonic-gate 				err = ESBD_NOMEM;
23630Sstevel@tonic-gate 				break;
23640Sstevel@tonic-gate 			default:
23650Sstevel@tonic-gate 				err = ESBD_INVAL;
23660Sstevel@tonic-gate 				break;
23670Sstevel@tonic-gate 			}
23680Sstevel@tonic-gate 			break;
23690Sstevel@tonic-gate 		}
2370*11474SJonathan.Adams@Sun.COM 		(void) sbdp_mem_add_span(hdp, mc->ml_address, mc->ml_size);
23710Sstevel@tonic-gate 	}
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate 	if (err != 0) {
23740Sstevel@tonic-gate 		SBD_SET_ERR(ep, err);
23750Sstevel@tonic-gate 		SBD_SET_ERRSTR(ep, sbp->sb_mempath[unit]);
23760Sstevel@tonic-gate 	}
23770Sstevel@tonic-gate 
23780Sstevel@tonic-gate 	memlist_delete(ml);
23790Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 	/*
23820Sstevel@tonic-gate 	 * Now attach all mem devinfo nodes to the device tree.
23830Sstevel@tonic-gate 	 */
23840Sstevel@tonic-gate 	for (i = 0; i < SBD_NUM_MC_PER_BOARD; i++) {
23850Sstevel@tonic-gate 		if (mp->sbm_dip[i] == NULL)
23860Sstevel@tonic-gate 			continue;
23870Sstevel@tonic-gate 		ASSERT(e_ddi_branch_held(mp->sbm_dip[i]));
23880Sstevel@tonic-gate 		if (e_ddi_branch_configure(mp->sbm_dip[i], NULL, 0) &&
23890Sstevel@tonic-gate 		    SBD_GET_ERR(ep) == 0) {
23900Sstevel@tonic-gate 			SBD_SET_ERR(ep, ESBD_INVAL);
23910Sstevel@tonic-gate 			SBD_SET_ERRSTR(ep, sbp->sb_mempath[unit]);
23920Sstevel@tonic-gate 		}
23930Sstevel@tonic-gate 	}
23940Sstevel@tonic-gate }
23950Sstevel@tonic-gate 
23960Sstevel@tonic-gate typedef struct {
23970Sstevel@tonic-gate 	kcondvar_t cond;
23980Sstevel@tonic-gate 	kmutex_t lock;
23990Sstevel@tonic-gate 	int error;
24000Sstevel@tonic-gate 	int done;
24010Sstevel@tonic-gate } sbd_release_mem_sync_t;
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate /*
24040Sstevel@tonic-gate  * When we reach here the memory being drained should have
24050Sstevel@tonic-gate  * already been reserved in sbd_pre_release_mem().
24060Sstevel@tonic-gate  * Our only task here is to kick off the "drain".
24070Sstevel@tonic-gate  * Returns -1 when error encountered or zero for success.
24080Sstevel@tonic-gate  */
24090Sstevel@tonic-gate int
sbd_release_mem(sbd_handle_t * hp,dev_info_t * dip,int unit)24100Sstevel@tonic-gate sbd_release_mem(sbd_handle_t *hp, dev_info_t *dip, int unit)
24110Sstevel@tonic-gate {
24120Sstevel@tonic-gate 	memhandle_t	mh;
24130Sstevel@tonic-gate 	int		err;
24140Sstevel@tonic-gate 	int		cancel_flag = 0;
24150Sstevel@tonic-gate 	int		e_code = 0;
24160Sstevel@tonic-gate 	sbd_board_t	*sbp = SBDH2BD(hp->h_sbd);
24170Sstevel@tonic-gate 	sbd_release_mem_sync_t rms;
24180Sstevel@tonic-gate 	static fn_t	f = "sbd_release_mem";
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate 	/*
24210Sstevel@tonic-gate 	 * If this node has a scheduled memory delete operation,
24220Sstevel@tonic-gate 	 * it will have a memhandle.  If it does have a memhandle (the
24230Sstevel@tonic-gate 	 * return value of sbd_get_memhandle is zero when true),
24240Sstevel@tonic-gate 	 * then perform the delete.
24250Sstevel@tonic-gate 	 */
24260Sstevel@tonic-gate 
24270Sstevel@tonic-gate 	if ((cancel_flag = sbd_get_memhandle(hp, dip, &mh)) != 0) {
24280Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: couldn't get the memhandle\n", f);
24290Sstevel@tonic-gate 		return (cancel_flag);
24300Sstevel@tonic-gate 	}
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 	bzero((void *) &rms, sizeof (rms));
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate 	mutex_init(&rms.lock, NULL, MUTEX_DRIVER, NULL);
24350Sstevel@tonic-gate 	cv_init(&rms.cond, NULL, CV_DRIVER, NULL);
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate 	mutex_enter(&rms.lock);
24380Sstevel@tonic-gate 	err = kphysm_del_start(mh, sbd_release_memory_done, (void *) &rms);
24390Sstevel@tonic-gate 	if (err == KPHYSM_OK) {
24400Sstevel@tonic-gate 		/* wait for completion */
24410Sstevel@tonic-gate 		while (!rms.done) {
24420Sstevel@tonic-gate 			if (cancel_flag) {
24430Sstevel@tonic-gate 				/* previously canceled */
24440Sstevel@tonic-gate 				cv_wait(&rms.cond, &rms.lock);
24450Sstevel@tonic-gate 			} else if (cv_wait_sig(&rms.cond, &rms.lock) == 0) {
24460Sstevel@tonic-gate 				/* interrupted: cancel and wait */
24470Sstevel@tonic-gate 				cancel_flag = -1;
24480Sstevel@tonic-gate 				(void) kphysm_del_cancel(mh);
24490Sstevel@tonic-gate 			}
24500Sstevel@tonic-gate 		}
24510Sstevel@tonic-gate 		/* get the result of the memory delete operation */
24520Sstevel@tonic-gate 		err = rms.error;
24530Sstevel@tonic-gate 	} else {
24540Sstevel@tonic-gate 		(void) kphysm_del_release(mh);
24550Sstevel@tonic-gate 	}
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate 	mutex_exit(&rms.lock);
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate 	cv_destroy(&rms.cond);
24600Sstevel@tonic-gate 	mutex_destroy(&rms.lock);
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 	if (err != KPHYSM_OK) {
24630Sstevel@tonic-gate 		switch (err) {
24640Sstevel@tonic-gate 			case KPHYSM_ENOWORK:
24650Sstevel@tonic-gate 				e_code = ESBD_NOERROR;
24660Sstevel@tonic-gate 				break;
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 			case KPHYSM_EHANDLE:
24690Sstevel@tonic-gate 			case KPHYSM_ESEQUENCE:
24700Sstevel@tonic-gate 				e_code = ESBD_INTERNAL;
24710Sstevel@tonic-gate 				break;
24720Sstevel@tonic-gate 
24730Sstevel@tonic-gate 			case KPHYSM_ENOTVIABLE:
24740Sstevel@tonic-gate 				e_code = ESBD_MEM_NOTVIABLE;
24750Sstevel@tonic-gate 				break;
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate 			case KPHYSM_EREFUSED:
24780Sstevel@tonic-gate 				e_code = ESBD_MEM_REFUSED;
24790Sstevel@tonic-gate 				break;
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 			case KPHYSM_ENONRELOC:
24820Sstevel@tonic-gate 				e_code = ESBD_MEM_NONRELOC;
24830Sstevel@tonic-gate 				break;
24840Sstevel@tonic-gate 
24850Sstevel@tonic-gate 			case KPHYSM_ECANCELLED:
24860Sstevel@tonic-gate 				e_code = ESBD_MEM_CANCELLED;
24870Sstevel@tonic-gate 				break;
24880Sstevel@tonic-gate 
24890Sstevel@tonic-gate 			case KPHYSM_ERESOURCE:
24900Sstevel@tonic-gate 				e_code = ESBD_MEMFAIL;
24910Sstevel@tonic-gate 				break;
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate 			default:
24940Sstevel@tonic-gate 				cmn_err(CE_WARN, "sbd:%s:"
24950Sstevel@tonic-gate 					" unexpected kphysm error code %d,"
24960Sstevel@tonic-gate 					" dip 0x%p",
24970Sstevel@tonic-gate 					f, err, (void *)dip);
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate 				e_code = ESBD_IO;
25000Sstevel@tonic-gate 				break;
25010Sstevel@tonic-gate 		}
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 		if (e_code != 0) {
25040Sstevel@tonic-gate 			cancel_flag = -1;
25050Sstevel@tonic-gate 			SBD_SET_ERR(SBD_HD2ERR(hp), e_code);
25060Sstevel@tonic-gate 			SBD_SET_ERRSTR(SBD_HD2ERR(hp), sbp->sb_mempath[unit]);
25070Sstevel@tonic-gate 		}
25080Sstevel@tonic-gate 	}
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate 	return (cancel_flag);
25110Sstevel@tonic-gate }
25120Sstevel@tonic-gate 
25130Sstevel@tonic-gate /*
25140Sstevel@tonic-gate  * Memory has been logically removed by the time this routine is called.
25150Sstevel@tonic-gate  */
25160Sstevel@tonic-gate void
sbd_release_memory_done(void * arg,int error)25170Sstevel@tonic-gate sbd_release_memory_done(void *arg, int error)
25180Sstevel@tonic-gate {
25190Sstevel@tonic-gate 	sbd_release_mem_sync_t *ds = arg;
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate 	mutex_enter(&ds->lock);
25220Sstevel@tonic-gate 	ds->error = error;
25230Sstevel@tonic-gate 	ds->done = 1;
25240Sstevel@tonic-gate 	cv_signal(&ds->cond);
25250Sstevel@tonic-gate 	mutex_exit(&ds->lock);
25260Sstevel@tonic-gate }
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate /*
25290Sstevel@tonic-gate  * If detaching node contains memory that is "non-permanent"
25300Sstevel@tonic-gate  * then the memory adr's are simply cleared.  If the memory
25310Sstevel@tonic-gate  * is non-relocatable, then do a copy-rename.
25320Sstevel@tonic-gate  */
25330Sstevel@tonic-gate int
sbd_detach_memory(sbd_handle_t * hp,sbderror_t * ep,sbd_mem_unit_t * s_mp,int unit)25340Sstevel@tonic-gate sbd_detach_memory(sbd_handle_t *hp, sbderror_t *ep, sbd_mem_unit_t *s_mp,
25350Sstevel@tonic-gate 	int unit)
25360Sstevel@tonic-gate {
25370Sstevel@tonic-gate 	int			rv;
25380Sstevel@tonic-gate 	sbd_mem_unit_t		*t_mp;
25390Sstevel@tonic-gate 	sbd_istate_t		state;
25400Sstevel@tonic-gate 	sbdp_handle_t		*hdp;
25410Sstevel@tonic-gate 	sbd_board_t 		*sbp = (sbd_board_t *)s_mp->sbm_cm.sbdev_sbp;
25420Sstevel@tonic-gate 	sbd_board_t		*tbp;
25430Sstevel@tonic-gate 	static fn_t		f = "sbd_detach_memory";
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate 	/* lookup target mem unit and target board structure, if any */
25480Sstevel@tonic-gate 	if (s_mp->sbm_flags & SBD_MFLAG_SOURCE) {
25490Sstevel@tonic-gate 		t_mp = s_mp->sbm_peer;
25500Sstevel@tonic-gate 		ASSERT(t_mp != NULL);
25510Sstevel@tonic-gate 		ASSERT(t_mp->sbm_peer == s_mp);
25520Sstevel@tonic-gate 		tbp = (sbd_board_t *)t_mp->sbm_cm.sbdev_sbp;
25530Sstevel@tonic-gate 	} else {
25540Sstevel@tonic-gate 		t_mp = NULL;
25550Sstevel@tonic-gate 	}
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 	/* verify mem unit's state is UNREFERENCED */
25580Sstevel@tonic-gate 	state = s_mp->sbm_cm.sbdev_state;
25590Sstevel@tonic-gate 	if (state != SBD_STATE_UNREFERENCED) {
25600Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: invalid state transition for"
25610Sstevel@tonic-gate 			" mem-unit (%d.%d)",
25620Sstevel@tonic-gate 			f,
25630Sstevel@tonic-gate 			sbp->sb_num,
25640Sstevel@tonic-gate 			s_mp->sbm_cm.sbdev_unum);
25650Sstevel@tonic-gate 		SBD_SET_ERR(ep, ESBD_STATE);
25660Sstevel@tonic-gate 		SBD_SET_ERRSTR(ep, sbp->sb_mempath[unit]);
25670Sstevel@tonic-gate 		return (-1);
25680Sstevel@tonic-gate 	}
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate 	/* verify target mem unit's state is UNREFERENCED, if any */
25710Sstevel@tonic-gate 	if (t_mp != NULL) {
25720Sstevel@tonic-gate 		state = t_mp->sbm_cm.sbdev_state;
25730Sstevel@tonic-gate 		if (state != SBD_STATE_UNREFERENCED) {
25740Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s: invalid state transition for"
25750Sstevel@tonic-gate 				" target mem-unit (%d.%d)",
25760Sstevel@tonic-gate 				f,
25770Sstevel@tonic-gate 				tbp->sb_num,
25780Sstevel@tonic-gate 				t_mp->sbm_cm.sbdev_unum);
25790Sstevel@tonic-gate 			SBD_SET_ERR(ep, ESBD_STATE);
25800Sstevel@tonic-gate 			SBD_SET_ERRSTR(ep, sbp->sb_mempath[unit]);
25810Sstevel@tonic-gate 			return (-1);
25820Sstevel@tonic-gate 		}
25830Sstevel@tonic-gate 	}
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 	/*
25860Sstevel@tonic-gate 	 * Displacement flush all ecaches in the system.
25870Sstevel@tonic-gate 	 * That's the fastest way to remove all cache references
25880Sstevel@tonic-gate 	 * to the detaching memory.
25890Sstevel@tonic-gate 	 */
25900Sstevel@tonic-gate 	xc_all(sbd_flush_ecache, 0, 0);
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate 	hdp = sbd_get_sbdp_handle(sbp, hp);
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate 	/*
25950Sstevel@tonic-gate 	 * If there is no target board (no copy/rename was needed), then
25960Sstevel@tonic-gate 	 * we're done!
25970Sstevel@tonic-gate 	 */
25980Sstevel@tonic-gate 	if (t_mp == NULL) {
25990Sstevel@tonic-gate 		/*
26000Sstevel@tonic-gate 		 * Reprogram interconnect hardware and disable
26010Sstevel@tonic-gate 		 * memory controllers for memory node that's going away.
26020Sstevel@tonic-gate 		 */
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 		rv = sbdphw_disable_memctrl(hdp, s_mp->sbm_cm.sbdev_dip);
26050Sstevel@tonic-gate 		if (rv) {
26060Sstevel@tonic-gate 			cmn_err(CE_WARN,
26070Sstevel@tonic-gate 				"%s: failed to deprogram mem-unit (%d.%d),"
26080Sstevel@tonic-gate 				" dip 0x%p",
26090Sstevel@tonic-gate 				f,
26100Sstevel@tonic-gate 				sbp->sb_num,
26110Sstevel@tonic-gate 				s_mp->sbm_cm.sbdev_unum,
26120Sstevel@tonic-gate 				(void *)s_mp->sbm_cm.sbdev_dip);
26130Sstevel@tonic-gate 			/*
26140Sstevel@tonic-gate 			 * Make sure we don't rewrite an sbdp error
26150Sstevel@tonic-gate 			 */
26160Sstevel@tonic-gate 			if (SBD_GET_ERR(ep) != 0) {
26170Sstevel@tonic-gate 				SBD_SET_ERR(ep, ESBD_HW_PROGRAM);
26180Sstevel@tonic-gate 				SBD_SET_ERRSTR(ep, sbp->sb_mempath[unit]);
26190Sstevel@tonic-gate 			}
26200Sstevel@tonic-gate 		}
26210Sstevel@tonic-gate 	} else {
26220Sstevel@tonic-gate 		rv = sbd_move_memory(hp, sbp, tbp);
26230Sstevel@tonic-gate 		if (rv) {
26240Sstevel@tonic-gate 			int i;
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s: failed to move memory"
26270Sstevel@tonic-gate 				" from board %d to board %d",
26280Sstevel@tonic-gate 				f,
26290Sstevel@tonic-gate 				sbp->sb_num,
26300Sstevel@tonic-gate 				tbp->sb_num);
26310Sstevel@tonic-gate 			/*
26320Sstevel@tonic-gate 			 * Make sure we don't rewrite an sbdp error
26330Sstevel@tonic-gate 			 */
26340Sstevel@tonic-gate 			if (SBD_GET_ERR(ep) != 0) {
26350Sstevel@tonic-gate 				SBD_SET_ERR(ep, ESBD_INTERNAL);
26360Sstevel@tonic-gate 				SBD_SET_ERRSTR(ep, sbp->sb_mempath[unit]);
26370Sstevel@tonic-gate 			}
26380Sstevel@tonic-gate 			/*
26390Sstevel@tonic-gate 			 * If we failed here, it means that the target board's
26400Sstevel@tonic-gate 			 * memory has been unconfigured.  We need to configure
26410Sstevel@tonic-gate 			 * it back
26420Sstevel@tonic-gate 			 */
26430Sstevel@tonic-gate 			for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
26440Sstevel@tonic-gate 				int		unit;
26450Sstevel@tonic-gate 				dev_info_t	*dip;
26460Sstevel@tonic-gate 				dev_info_t	**devlist;
26470Sstevel@tonic-gate 
26480Sstevel@tonic-gate 
26490Sstevel@tonic-gate 				devlist = tbp->sb_devlist[NIX(SBD_COMP_MEM)];
26500Sstevel@tonic-gate 				dip = devlist[i];
26510Sstevel@tonic-gate 				sbd_reset_error_sbdph(hdp);
26520Sstevel@tonic-gate 				unit = sbdp_get_unit_num(hdp, dip);
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate 				/*
26550Sstevel@tonic-gate 				 * We already saved the error that created
26560Sstevel@tonic-gate 				 * this mess.  If we fail, make sure not
26570Sstevel@tonic-gate 				 * to overwrite the original error
26580Sstevel@tonic-gate 				 */
26590Sstevel@tonic-gate 				if (unit == -1) {
26600Sstevel@tonic-gate 					continue;
26610Sstevel@tonic-gate 				}
26620Sstevel@tonic-gate 				if (sbd_cancel_mem(hp, unit) != 0)
26630Sstevel@tonic-gate 					continue;
26640Sstevel@tonic-gate 
26650Sstevel@tonic-gate 				t_mp->sbm_flags = 0;
26660Sstevel@tonic-gate 				/*
26670Sstevel@tonic-gate 				 * clean up
26680Sstevel@tonic-gate 				 */
26690Sstevel@tonic-gate 				sbd_mem_cleanup(s_mp, t_mp, ep);
26700Sstevel@tonic-gate 				if (s_mp->sbm_mlist) {
26710Sstevel@tonic-gate 					memlist_delete(s_mp->sbm_mlist);
26720Sstevel@tonic-gate 					s_mp->sbm_mlist = NULL;
26730Sstevel@tonic-gate 				}
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 				SBD_DEVICE_TRANSITION(tbp, SBD_COMP_MEM,
26760Sstevel@tonic-gate 				    unit, SBD_STATE_CONFIGURED);
26770Sstevel@tonic-gate 			}
26780Sstevel@tonic-gate 		}
26790Sstevel@tonic-gate 
26800Sstevel@tonic-gate 		PR_MEM("%s: %s memory COPY-RENAME (board %d -> %d)\n",
26810Sstevel@tonic-gate 			f,
26820Sstevel@tonic-gate 			rv ? "FAILED" : "COMPLETED",
26830Sstevel@tonic-gate 			sbp->sb_num,
26840Sstevel@tonic-gate 			tbp->sb_num);
26850Sstevel@tonic-gate 	}
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 	if (rv == 0) {
26880Sstevel@tonic-gate 		update_membounds_t umb;
26890Sstevel@tonic-gate 
26900Sstevel@tonic-gate 		umb.u_board = sbp->sb_num;
26910Sstevel@tonic-gate 		umb.u_base = (uint64_t)-1;
26920Sstevel@tonic-gate 		umb.u_len = (uint64_t)-1;
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate 		lgrp_plat_config(LGRP_CONFIG_MEM_DEL, (uintptr_t)&umb);
26950Sstevel@tonic-gate 	}
26960Sstevel@tonic-gate 
26970Sstevel@tonic-gate 	sbd_release_sbdp_handle(hdp);
26980Sstevel@tonic-gate 	return (rv);
26990Sstevel@tonic-gate }
27000Sstevel@tonic-gate 
27010Sstevel@tonic-gate /*ARGSUSED*/
27020Sstevel@tonic-gate static void
sbd_flush_ecache(uint64_t a,uint64_t b)27030Sstevel@tonic-gate sbd_flush_ecache(uint64_t a, uint64_t b)
27040Sstevel@tonic-gate {
27050Sstevel@tonic-gate 	cpu_flush_ecache();
27060Sstevel@tonic-gate }
2707