xref: /onnv-gate/usr/src/uts/sun4u/ngdr/io/dr_mem.c (revision 11311:639e7bc0b42f)
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
51772Sjl139090  * Common Development and Distribution License (the "License").
61772Sjl139090  * 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  */
21*11311SSurya.Prakki@Sun.COM 
220Sstevel@tonic-gate /*
2311066Srafael.vanoni@sun.com  * Copyright 2009 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  * DR memory support routines.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/note.h>
320Sstevel@tonic-gate #include <sys/debug.h>
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/errno.h>
350Sstevel@tonic-gate #include <sys/param.h>
360Sstevel@tonic-gate #include <sys/dditypes.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
380Sstevel@tonic-gate #include <sys/conf.h>
390Sstevel@tonic-gate #include <sys/ddi.h>
400Sstevel@tonic-gate #include <sys/sunddi.h>
410Sstevel@tonic-gate #include <sys/sunndi.h>
420Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
430Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
440Sstevel@tonic-gate #include <sys/sysmacros.h>
450Sstevel@tonic-gate #include <sys/machsystm.h>
460Sstevel@tonic-gate #include <sys/spitregs.h>
470Sstevel@tonic-gate #include <sys/cpuvar.h>
480Sstevel@tonic-gate #include <sys/promif.h>
490Sstevel@tonic-gate #include <vm/seg_kmem.h>
500Sstevel@tonic-gate #include <sys/lgrp.h>
510Sstevel@tonic-gate #include <sys/platform_module.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include <vm/page.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #include <sys/dr.h>
560Sstevel@tonic-gate #include <sys/dr_util.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate extern struct memlist	*phys_install;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /* TODO: push this reference below drmach line */
610Sstevel@tonic-gate extern int		kcage_on;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /* for the DR*INTERNAL_ERROR macros.  see sys/dr.h. */
647799SRichard.Bean@Sun.COM static char *dr_ie_fmt = "dr_mem.c %d";
650Sstevel@tonic-gate 
6611066Srafael.vanoni@sun.com static int	dr_post_detach_mem_unit(dr_mem_unit_t *mp);
6711066Srafael.vanoni@sun.com static int	dr_reserve_mem_spans(memhandle_t *mhp, struct memlist *mlist);
6811066Srafael.vanoni@sun.com static int	dr_select_mem_target(dr_handle_t *hp, dr_mem_unit_t *mp,
6911066Srafael.vanoni@sun.com     struct memlist *ml);
7011066Srafael.vanoni@sun.com static void	dr_init_mem_unit_data(dr_mem_unit_t *mp);
7111066Srafael.vanoni@sun.com 
7211066Srafael.vanoni@sun.com static int 	memlist_canfit(struct memlist *s_mlist,
7311066Srafael.vanoni@sun.com     struct memlist *t_mlist);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * dr_mem_unit_t.sbm_flags
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate #define	DR_MFLAG_RESERVED	0x01	/* mem unit reserved for delete */
790Sstevel@tonic-gate #define	DR_MFLAG_SOURCE		0x02	/* source brd of copy/rename op */
800Sstevel@tonic-gate #define	DR_MFLAG_TARGET		0x04	/* target brd of copy/rename op */
810Sstevel@tonic-gate #define	DR_MFLAG_MEMUPSIZE	0x08	/* move from big to small board */
820Sstevel@tonic-gate #define	DR_MFLAG_MEMDOWNSIZE	0x10	/* move from small to big board */
830Sstevel@tonic-gate #define	DR_MFLAG_MEMRESIZE	0x18	/* move to different size board */
840Sstevel@tonic-gate #define	DR_MFLAG_RELOWNER	0x20	/* memory release (delete) owner */
850Sstevel@tonic-gate #define	DR_MFLAG_RELDONE	0x40	/* memory release (delete) done */
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /* helper macros */
880Sstevel@tonic-gate #define	_ptob64(p) ((uint64_t)(p) << PAGESHIFT)
890Sstevel@tonic-gate #define	_b64top(b) ((pgcnt_t)((b) >> PAGESHIFT))
900Sstevel@tonic-gate 
910Sstevel@tonic-gate static struct memlist *
920Sstevel@tonic-gate dr_get_memlist(dr_mem_unit_t *mp)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	struct memlist	*mlist = NULL;
950Sstevel@tonic-gate 	sbd_error_t	*err;
960Sstevel@tonic-gate 	static fn_t	f = "dr_get_memlist";
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	PR_MEM("%s for %s...\n", f, mp->sbm_cm.sbdev_path);
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	/*
1010Sstevel@tonic-gate 	 * Return cached memlist, if present.
1020Sstevel@tonic-gate 	 * This memlist will be present following an
1030Sstevel@tonic-gate 	 * unconfigure (a.k.a: detach) of this memunit.
1040Sstevel@tonic-gate 	 * It should only be used in the case were a configure
1050Sstevel@tonic-gate 	 * is bringing this memunit back in without going
1060Sstevel@tonic-gate 	 * through the disconnect and connect states.
1070Sstevel@tonic-gate 	 */
1080Sstevel@tonic-gate 	if (mp->sbm_mlist) {
1090Sstevel@tonic-gate 		PR_MEM("%s: found cached memlist\n", f);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 		mlist = memlist_dup(mp->sbm_mlist);
1120Sstevel@tonic-gate 	} else {
1130Sstevel@tonic-gate 		uint64_t basepa = _ptob64(mp->sbm_basepfn);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 		/* attempt to construct a memlist using phys_install */
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 		/* round down to slice base address */
1180Sstevel@tonic-gate 		basepa &= ~(mp->sbm_slice_size - 1);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 		/* get a copy of phys_install to edit */
1210Sstevel@tonic-gate 		memlist_read_lock();
1220Sstevel@tonic-gate 		mlist = memlist_dup(phys_install);
1230Sstevel@tonic-gate 		memlist_read_unlock();
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 		/* trim lower irrelevant span */
1260Sstevel@tonic-gate 		if (mlist)
1270Sstevel@tonic-gate 			mlist = memlist_del_span(mlist, 0ull, basepa);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 		/* trim upper irrelevant span */
1300Sstevel@tonic-gate 		if (mlist) {
1310Sstevel@tonic-gate 			uint64_t endpa;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 			basepa += mp->sbm_slice_size;
1340Sstevel@tonic-gate 			endpa = _ptob64(physmax + 1);
1350Sstevel@tonic-gate 			if (endpa > basepa)
1360Sstevel@tonic-gate 				mlist = memlist_del_span(
13711066Srafael.vanoni@sun.com 				    mlist,
13811066Srafael.vanoni@sun.com 				    basepa,
13911066Srafael.vanoni@sun.com 				    endpa - basepa);
1400Sstevel@tonic-gate 		}
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 		if (mlist) {
1430Sstevel@tonic-gate 			/* successfully built a memlist */
1440Sstevel@tonic-gate 			PR_MEM("%s: derived memlist from phys_install\n", f);
1450Sstevel@tonic-gate 		}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 		/* if no mlist yet, try platform layer */
1480Sstevel@tonic-gate 		if (!mlist) {
1490Sstevel@tonic-gate 			err = drmach_mem_get_memlist(
15011066Srafael.vanoni@sun.com 			    mp->sbm_cm.sbdev_id, &mlist);
1510Sstevel@tonic-gate 			if (err) {
1520Sstevel@tonic-gate 				DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
1530Sstevel@tonic-gate 				mlist = NULL; /* paranoia */
1540Sstevel@tonic-gate 			}
1550Sstevel@tonic-gate 		}
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	PR_MEM("%s: memlist for %s\n", f, mp->sbm_cm.sbdev_path);
1590Sstevel@tonic-gate 	PR_MEMLIST_DUMP(mlist);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	return (mlist);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate typedef struct {
1650Sstevel@tonic-gate 	kcondvar_t cond;
1660Sstevel@tonic-gate 	kmutex_t lock;
1670Sstevel@tonic-gate 	int error;
1680Sstevel@tonic-gate 	int done;
1690Sstevel@tonic-gate } dr_release_mem_sync_t;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * Memory has been logically removed by the time this routine is called.
1730Sstevel@tonic-gate  */
1740Sstevel@tonic-gate static void
1750Sstevel@tonic-gate dr_mem_del_done(void *arg, int error)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 	dr_release_mem_sync_t *ds = arg;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	mutex_enter(&ds->lock);
1800Sstevel@tonic-gate 	ds->error = error;
1810Sstevel@tonic-gate 	ds->done = 1;
1820Sstevel@tonic-gate 	cv_signal(&ds->cond);
1830Sstevel@tonic-gate 	mutex_exit(&ds->lock);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate  * When we reach here the memory being drained should have
1880Sstevel@tonic-gate  * already been reserved in dr_pre_release_mem().
1890Sstevel@tonic-gate  * Our only task here is to kick off the "drain" and wait
1900Sstevel@tonic-gate  * for it to finish.
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate void
1930Sstevel@tonic-gate dr_release_mem(dr_common_unit_t *cp)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	dr_mem_unit_t	*mp = (dr_mem_unit_t *)cp;
1960Sstevel@tonic-gate 	int		err;
1970Sstevel@tonic-gate 	dr_release_mem_sync_t rms;
1980Sstevel@tonic-gate 	static fn_t	f = "dr_release_mem";
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	/* check that this memory unit has been reserved */
2010Sstevel@tonic-gate 	if (!(mp->sbm_flags & DR_MFLAG_RELOWNER)) {
2020Sstevel@tonic-gate 		DR_DEV_INTERNAL_ERROR(&mp->sbm_cm);
2030Sstevel@tonic-gate 		return;
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	bzero((void *) &rms, sizeof (rms));
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	mutex_init(&rms.lock, NULL, MUTEX_DRIVER, NULL);
2090Sstevel@tonic-gate 	cv_init(&rms.cond, NULL, CV_DRIVER, NULL);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	mutex_enter(&rms.lock);
21211066Srafael.vanoni@sun.com 	err = kphysm_del_start(mp->sbm_memhandle, dr_mem_del_done,
21311066Srafael.vanoni@sun.com 	    (void *) &rms);
2140Sstevel@tonic-gate 	if (err == KPHYSM_OK) {
2150Sstevel@tonic-gate 		/* wait for completion or interrupt */
2160Sstevel@tonic-gate 		while (!rms.done) {
2170Sstevel@tonic-gate 			if (cv_wait_sig(&rms.cond, &rms.lock) == 0) {
2180Sstevel@tonic-gate 				/* then there is a pending UNIX signal */
2190Sstevel@tonic-gate 				(void) kphysm_del_cancel(mp->sbm_memhandle);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 				/* wait for completion */
2220Sstevel@tonic-gate 				while (!rms.done)
2230Sstevel@tonic-gate 					cv_wait(&rms.cond, &rms.lock);
2240Sstevel@tonic-gate 			}
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 		/* get the result of the memory delete operation */
2270Sstevel@tonic-gate 		err = rms.error;
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 	mutex_exit(&rms.lock);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	cv_destroy(&rms.cond);
2320Sstevel@tonic-gate 	mutex_destroy(&rms.lock);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (err != KPHYSM_OK) {
2350Sstevel@tonic-gate 		int e_code;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 		switch (err) {
2380Sstevel@tonic-gate 			case KPHYSM_ENOWORK:
2390Sstevel@tonic-gate 				e_code = ESBD_NOERROR;
2400Sstevel@tonic-gate 				break;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 			case KPHYSM_EHANDLE:
2430Sstevel@tonic-gate 			case KPHYSM_ESEQUENCE:
2440Sstevel@tonic-gate 				e_code = ESBD_INTERNAL;
2450Sstevel@tonic-gate 				break;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 			case KPHYSM_ENOTVIABLE:
2480Sstevel@tonic-gate 				e_code = ESBD_MEM_NOTVIABLE;
2490Sstevel@tonic-gate 				break;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 			case KPHYSM_EREFUSED:
2520Sstevel@tonic-gate 				e_code = ESBD_MEM_REFUSED;
2530Sstevel@tonic-gate 				break;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 			case KPHYSM_ENONRELOC:
2560Sstevel@tonic-gate 				e_code = ESBD_MEM_NONRELOC;
2570Sstevel@tonic-gate 				break;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 			case KPHYSM_ECANCELLED:
2600Sstevel@tonic-gate 				e_code = ESBD_MEM_CANCELLED;
2610Sstevel@tonic-gate 				break;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 			case KPHYSM_ERESOURCE:
2640Sstevel@tonic-gate 				e_code = ESBD_MEMFAIL;
2650Sstevel@tonic-gate 				break;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 			default:
2680Sstevel@tonic-gate 				cmn_err(CE_WARN,
26911066Srafael.vanoni@sun.com 				    "%s: unexpected kphysm error code %d,"
27011066Srafael.vanoni@sun.com 				    " id 0x%p",
27111066Srafael.vanoni@sun.com 				    f, err, mp->sbm_cm.sbdev_id);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 				e_code = ESBD_IO;
2740Sstevel@tonic-gate 				break;
2750Sstevel@tonic-gate 		}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		if (e_code != ESBD_NOERROR) {
2780Sstevel@tonic-gate 			dr_dev_err(CE_IGNORE, &mp->sbm_cm, e_code);
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate void
2840Sstevel@tonic-gate dr_attach_mem(dr_handle_t *hp, dr_common_unit_t *cp)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	_NOTE(ARGUNUSED(hp))
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	dr_mem_unit_t	*mp = (dr_mem_unit_t *)cp;
2890Sstevel@tonic-gate 	struct memlist	*ml, *mc;
2900Sstevel@tonic-gate 	sbd_error_t	*err;
2910Sstevel@tonic-gate 	static fn_t	f = "dr_attach_mem";
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	dr_lock_status(hp->h_bd);
2960Sstevel@tonic-gate 	err = drmach_configure(cp->sbdev_id, 0);
2970Sstevel@tonic-gate 	dr_unlock_status(hp->h_bd);
2980Sstevel@tonic-gate 	if (err) {
2990Sstevel@tonic-gate 		DRERR_SET_C(&cp->sbdev_error, &err);
3000Sstevel@tonic-gate 		return;
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	ml = dr_get_memlist(mp);
3040Sstevel@tonic-gate 	for (mc = ml; mc; mc = mc->next) {
3050Sstevel@tonic-gate 		int		 rv;
3060Sstevel@tonic-gate 		sbd_error_t	*err;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 		rv = kphysm_add_memory_dynamic(
30911066Srafael.vanoni@sun.com 		    (pfn_t)(mc->address >> PAGESHIFT),
31011066Srafael.vanoni@sun.com 		    (pgcnt_t)(mc->size >> PAGESHIFT));
3110Sstevel@tonic-gate 		if (rv != KPHYSM_OK) {
3120Sstevel@tonic-gate 			/*
3130Sstevel@tonic-gate 			 * translate kphysm error and
3140Sstevel@tonic-gate 			 * store in devlist error
3150Sstevel@tonic-gate 			 */
3160Sstevel@tonic-gate 			switch (rv) {
3170Sstevel@tonic-gate 			case KPHYSM_ERESOURCE:
3180Sstevel@tonic-gate 				rv = ESBD_NOMEM;
3190Sstevel@tonic-gate 				break;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 			case KPHYSM_EFAULT:
3220Sstevel@tonic-gate 				rv = ESBD_FAULT;
3230Sstevel@tonic-gate 				break;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 			default:
3260Sstevel@tonic-gate 				rv = ESBD_INTERNAL;
3270Sstevel@tonic-gate 				break;
3280Sstevel@tonic-gate 			}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 			if (rv == ESBD_INTERNAL) {
3310Sstevel@tonic-gate 				DR_DEV_INTERNAL_ERROR(&mp->sbm_cm);
3320Sstevel@tonic-gate 			} else
3330Sstevel@tonic-gate 				dr_dev_err(CE_WARN, &mp->sbm_cm, rv);
3340Sstevel@tonic-gate 			break;
3350Sstevel@tonic-gate 		}
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 		err = drmach_mem_add_span(
33811066Srafael.vanoni@sun.com 		    mp->sbm_cm.sbdev_id, mc->address, mc->size);
3390Sstevel@tonic-gate 		if (err) {
3400Sstevel@tonic-gate 			DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
3410Sstevel@tonic-gate 			break;
3420Sstevel@tonic-gate 		}
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	memlist_delete(ml);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	/* back out if configure failed */
3480Sstevel@tonic-gate 	if (mp->sbm_cm.sbdev_error != NULL) {
3490Sstevel@tonic-gate 		dr_lock_status(hp->h_bd);
3501772Sjl139090 		err = drmach_unconfigure(cp->sbdev_id,
35111066Srafael.vanoni@sun.com 		    DEVI_BRANCH_DESTROY);
3520Sstevel@tonic-gate 		if (err)
3530Sstevel@tonic-gate 			sbd_err_clear(&err);
3540Sstevel@tonic-gate 		dr_unlock_status(hp->h_bd);
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate #define	DR_SCRUB_VALUE	0x0d0e0a0d0b0e0e0fULL
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate static void
3610Sstevel@tonic-gate dr_mem_ecache_scrub(dr_mem_unit_t *mp, struct memlist *mlist)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate #ifdef DEBUG
36411066Srafael.vanoni@sun.com 	clock_t		stime = ddi_get_lbolt();
3650Sstevel@tonic-gate #endif /* DEBUG */
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	struct memlist	*ml;
3680Sstevel@tonic-gate 	uint64_t	scrub_value = DR_SCRUB_VALUE;
3690Sstevel@tonic-gate 	processorid_t	cpuid;
3700Sstevel@tonic-gate 	static fn_t	f = "dr_mem_ecache_scrub";
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	cpuid = drmach_mem_cpu_affinity(mp->sbm_cm.sbdev_id);
3730Sstevel@tonic-gate 	affinity_set(cpuid);
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	PR_MEM("%s: using proc %d, memlist...\n", f,
3760Sstevel@tonic-gate 	    (cpuid == CPU_CURRENT) ? CPU->cpu_id : cpuid);
3770Sstevel@tonic-gate 	PR_MEMLIST_DUMP(mlist);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	for (ml = mlist; ml; ml = ml->next) {
3800Sstevel@tonic-gate 		uint64_t	dst_pa;
3810Sstevel@tonic-gate 		uint64_t	nbytes;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 		/* calculate the destination physical address */
3840Sstevel@tonic-gate 		dst_pa = ml->address;
3850Sstevel@tonic-gate 		if (ml->address & PAGEOFFSET)
3860Sstevel@tonic-gate 			cmn_err(CE_WARN,
38711066Srafael.vanoni@sun.com 			    "%s: address (0x%lx) not on "
38811066Srafael.vanoni@sun.com 			    "page boundary", f, ml->address);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 		nbytes = ml->size;
3910Sstevel@tonic-gate 		if (ml->size & PAGEOFFSET)
3920Sstevel@tonic-gate 			cmn_err(CE_WARN,
39311066Srafael.vanoni@sun.com 			    "%s: size (0x%lx) not on "
39411066Srafael.vanoni@sun.com 			    "page boundary", f, ml->size);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 		/*LINTED*/
3970Sstevel@tonic-gate 		while (nbytes > 0) {
3980Sstevel@tonic-gate 			/* write 64 bits to dst_pa */
3990Sstevel@tonic-gate 			stdphys(dst_pa, scrub_value);
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 			/* increment/decrement by cacheline sizes */
4020Sstevel@tonic-gate 			dst_pa += DRMACH_COHERENCY_UNIT;
4030Sstevel@tonic-gate 			nbytes -= DRMACH_COHERENCY_UNIT;
4040Sstevel@tonic-gate 		}
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	/*
4080Sstevel@tonic-gate 	 * flush this cpu's ecache and take care to ensure
4090Sstevel@tonic-gate 	 * that all of it's bus transactions have retired.
4100Sstevel@tonic-gate 	 */
4110Sstevel@tonic-gate 	drmach_cpu_flush_ecache_sync();
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	affinity_clear();
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate #ifdef DEBUG
41611066Srafael.vanoni@sun.com 	stime = ddi_get_lbolt() - stime;
4170Sstevel@tonic-gate 	PR_MEM("%s: scrub ticks = %ld (%ld secs)\n", f, stime, stime / hz);
4180Sstevel@tonic-gate #endif /* DEBUG */
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate static int
4220Sstevel@tonic-gate dr_move_memory(dr_handle_t *hp, dr_mem_unit_t *s_mp, dr_mem_unit_t *t_mp)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	time_t		 copytime;
4250Sstevel@tonic-gate 	drmachid_t	 cr_id;
4260Sstevel@tonic-gate 	dr_sr_handle_t	*srhp;
427917Selowe 	struct memlist	*c_ml, *d_ml;
4280Sstevel@tonic-gate 	sbd_error_t	*err;
4290Sstevel@tonic-gate 	static fn_t	 f = "dr_move_memory";
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	PR_MEM("%s: (INLINE) moving memory from %s to %s\n",
43211066Srafael.vanoni@sun.com 	    f,
43311066Srafael.vanoni@sun.com 	    s_mp->sbm_cm.sbdev_path,
43411066Srafael.vanoni@sun.com 	    t_mp->sbm_cm.sbdev_path);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	ASSERT(s_mp->sbm_flags & DR_MFLAG_SOURCE);
4370Sstevel@tonic-gate 	ASSERT(s_mp->sbm_peer == t_mp);
4380Sstevel@tonic-gate 	ASSERT(s_mp->sbm_mlist);
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	ASSERT(t_mp->sbm_flags & DR_MFLAG_TARGET);
4410Sstevel@tonic-gate 	ASSERT(t_mp->sbm_peer == s_mp);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/*
4440Sstevel@tonic-gate 	 * create a memlist of spans to copy by removing
4450Sstevel@tonic-gate 	 * the spans that have been deleted, if any, from
4460Sstevel@tonic-gate 	 * the full source board memlist.  s_mp->sbm_del_mlist
4470Sstevel@tonic-gate 	 * will be NULL if there were no spans deleted from
4480Sstevel@tonic-gate 	 * the source board.
4490Sstevel@tonic-gate 	 */
4500Sstevel@tonic-gate 	c_ml = memlist_dup(s_mp->sbm_mlist);
4510Sstevel@tonic-gate 	d_ml = s_mp->sbm_del_mlist;
4520Sstevel@tonic-gate 	while (d_ml != NULL) {
4530Sstevel@tonic-gate 		c_ml = memlist_del_span(c_ml, d_ml->address, d_ml->size);
4540Sstevel@tonic-gate 		d_ml = d_ml->next;
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	affinity_set(drmach_mem_cpu_affinity(t_mp->sbm_cm.sbdev_id));
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	err = drmach_copy_rename_init(
46011066Srafael.vanoni@sun.com 	    t_mp->sbm_cm.sbdev_id, _ptob64(t_mp->sbm_slice_offset),
46111066Srafael.vanoni@sun.com 	    s_mp->sbm_cm.sbdev_id, c_ml, &cr_id);
4620Sstevel@tonic-gate 	if (err) {
4630Sstevel@tonic-gate 		DRERR_SET_C(&s_mp->sbm_cm.sbdev_error, &err);
4640Sstevel@tonic-gate 		affinity_clear();
4650Sstevel@tonic-gate 		return (-1);
4660Sstevel@tonic-gate 	}
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	srhp = dr_get_sr_handle(hp);
4690Sstevel@tonic-gate 	ASSERT(srhp);
4700Sstevel@tonic-gate 
47111066Srafael.vanoni@sun.com 	copytime = ddi_get_lbolt();
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	/* Quiesce the OS.  */
4740Sstevel@tonic-gate 	if (dr_suspend(srhp)) {
4750Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: failed to quiesce OS"
47611066Srafael.vanoni@sun.com 		    " for copy-rename", f);
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 		dr_release_sr_handle(srhp);
4790Sstevel@tonic-gate 		err = drmach_copy_rename_fini(cr_id);
4800Sstevel@tonic-gate 		if (err) {
4810Sstevel@tonic-gate 			/*
4820Sstevel@tonic-gate 			 * no error is expected since the program has
4830Sstevel@tonic-gate 			 * not yet run.
4840Sstevel@tonic-gate 			 */
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 			/* catch this in debug kernels */
4870Sstevel@tonic-gate 			ASSERT(0);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 			sbd_err_clear(&err);
4900Sstevel@tonic-gate 		}
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 		/* suspend error reached via hp */
4930Sstevel@tonic-gate 		s_mp->sbm_cm.sbdev_error = hp->h_err;
4940Sstevel@tonic-gate 		hp->h_err = NULL;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 		affinity_clear();
4970Sstevel@tonic-gate 		return (-1);
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	/*
5010Sstevel@tonic-gate 	 * Rename memory for lgroup.
5020Sstevel@tonic-gate 	 * Source and target board numbers are packaged in arg.
5030Sstevel@tonic-gate 	 */
5040Sstevel@tonic-gate 	{
5050Sstevel@tonic-gate 		dr_board_t	*t_bp, *s_bp;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 		s_bp = s_mp->sbm_cm.sbdev_bp;
5080Sstevel@tonic-gate 		t_bp = t_mp->sbm_cm.sbdev_bp;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 		lgrp_plat_config(LGRP_CONFIG_MEM_RENAME,
51111066Srafael.vanoni@sun.com 		    (uintptr_t)(s_bp->b_num | (t_bp->b_num << 16)));
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	drmach_copy_rename(cr_id);
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	/* Resume the OS.  */
5170Sstevel@tonic-gate 	dr_resume(srhp);
5180Sstevel@tonic-gate 
51911066Srafael.vanoni@sun.com 	copytime = ddi_get_lbolt() - copytime;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	dr_release_sr_handle(srhp);
5220Sstevel@tonic-gate 	err = drmach_copy_rename_fini(cr_id);
5230Sstevel@tonic-gate 	if (err)
5240Sstevel@tonic-gate 		DRERR_SET_C(&s_mp->sbm_cm.sbdev_error, &err);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	affinity_clear();
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	PR_MEM("%s: copy-rename elapsed time = %ld ticks (%ld secs)\n",
52911066Srafael.vanoni@sun.com 	    f, copytime, copytime / hz);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	/* return -1 if dr_suspend or copy/rename recorded an error */
5320Sstevel@tonic-gate 	return (err == NULL ? 0 : -1);
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate /*
5360Sstevel@tonic-gate  * If detaching node contains memory that is "non-permanent"
5370Sstevel@tonic-gate  * then the memory adr's are simply cleared.  If the memory
5380Sstevel@tonic-gate  * is non-relocatable, then do a copy-rename.
5390Sstevel@tonic-gate  */
5400Sstevel@tonic-gate void
5410Sstevel@tonic-gate dr_detach_mem(dr_handle_t *hp, dr_common_unit_t *cp)
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate 	int			rv = 0;
5440Sstevel@tonic-gate 	dr_mem_unit_t		*s_mp = (dr_mem_unit_t *)cp;
5450Sstevel@tonic-gate 	dr_mem_unit_t		*t_mp;
5460Sstevel@tonic-gate 	dr_state_t		state;
5470Sstevel@tonic-gate 	static fn_t		f = "dr_detach_mem";
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	/* lookup target mem unit and target board structure, if any */
5520Sstevel@tonic-gate 	if (s_mp->sbm_flags & DR_MFLAG_SOURCE) {
5530Sstevel@tonic-gate 		t_mp = s_mp->sbm_peer;
5540Sstevel@tonic-gate 		ASSERT(t_mp != NULL);
5550Sstevel@tonic-gate 		ASSERT(t_mp->sbm_peer == s_mp);
5560Sstevel@tonic-gate 	} else {
5570Sstevel@tonic-gate 		t_mp = NULL;
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	/* verify mem unit's state is UNREFERENCED */
5610Sstevel@tonic-gate 	state = s_mp->sbm_cm.sbdev_state;
5620Sstevel@tonic-gate 	if (state != DR_STATE_UNREFERENCED) {
5630Sstevel@tonic-gate 		dr_dev_err(CE_IGNORE, &s_mp->sbm_cm, ESBD_STATE);
5640Sstevel@tonic-gate 		return;
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	/* verify target mem unit's state is UNREFERENCED, if any */
5680Sstevel@tonic-gate 	if (t_mp != NULL) {
5690Sstevel@tonic-gate 		state = t_mp->sbm_cm.sbdev_state;
5700Sstevel@tonic-gate 		if (state != DR_STATE_UNREFERENCED) {
5710Sstevel@tonic-gate 			dr_dev_err(CE_IGNORE, &t_mp->sbm_cm, ESBD_STATE);
5720Sstevel@tonic-gate 			return;
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	/*
5770Sstevel@tonic-gate 	 * Scrub deleted memory.  This will cause all cachelines
5780Sstevel@tonic-gate 	 * referencing the memory to only be in the local cpu's
5790Sstevel@tonic-gate 	 * ecache.
5800Sstevel@tonic-gate 	 */
5810Sstevel@tonic-gate 	if (s_mp->sbm_flags & DR_MFLAG_RELDONE) {
5820Sstevel@tonic-gate 		/* no del mlist for src<=dst mem size copy/rename */
5830Sstevel@tonic-gate 		if (s_mp->sbm_del_mlist)
5840Sstevel@tonic-gate 			dr_mem_ecache_scrub(s_mp, s_mp->sbm_del_mlist);
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 	if (t_mp != NULL && (t_mp->sbm_flags & DR_MFLAG_RELDONE)) {
5870Sstevel@tonic-gate 		ASSERT(t_mp->sbm_del_mlist);
5880Sstevel@tonic-gate 		dr_mem_ecache_scrub(t_mp, t_mp->sbm_del_mlist);
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	/*
5920Sstevel@tonic-gate 	 * If there is no target board (no copy/rename was needed), then
5930Sstevel@tonic-gate 	 * we're done!
5940Sstevel@tonic-gate 	 */
5950Sstevel@tonic-gate 	if (t_mp == NULL) {
5960Sstevel@tonic-gate 		sbd_error_t *err;
5970Sstevel@tonic-gate 		/*
5980Sstevel@tonic-gate 		 * Reprogram interconnect hardware and disable
5990Sstevel@tonic-gate 		 * memory controllers for memory node that's going away.
6000Sstevel@tonic-gate 		 */
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 		err = drmach_mem_disable(s_mp->sbm_cm.sbdev_id);
6030Sstevel@tonic-gate 		if (err) {
6040Sstevel@tonic-gate 			DRERR_SET_C(&s_mp->sbm_cm.sbdev_error, &err);
6050Sstevel@tonic-gate 			rv = -1;
6060Sstevel@tonic-gate 		}
6070Sstevel@tonic-gate 	} else {
6080Sstevel@tonic-gate 		rv = dr_move_memory(hp, s_mp, t_mp);
6090Sstevel@tonic-gate 		PR_MEM("%s: %s memory COPY-RENAME (board %d -> %d)\n",
61011066Srafael.vanoni@sun.com 		    f,
61111066Srafael.vanoni@sun.com 		    rv ? "FAILED" : "COMPLETED",
61211066Srafael.vanoni@sun.com 		    s_mp->sbm_cm.sbdev_bp->b_num,
61311066Srafael.vanoni@sun.com 		    t_mp->sbm_cm.sbdev_bp->b_num);
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 		if (rv != 0)
6160Sstevel@tonic-gate 			(void) dr_cancel_mem(s_mp);
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	if (rv == 0) {
6200Sstevel@tonic-gate 		sbd_error_t *err;
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 		dr_lock_status(hp->h_bd);
6230Sstevel@tonic-gate 		err = drmach_unconfigure(s_mp->sbm_cm.sbdev_id,
6241772Sjl139090 		    DEVI_BRANCH_DESTROY);
6250Sstevel@tonic-gate 		dr_unlock_status(hp->h_bd);
6260Sstevel@tonic-gate 		if (err)
6270Sstevel@tonic-gate 			sbd_err_clear(&err);
6280Sstevel@tonic-gate 	}
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate #ifndef _STARFIRE
6320Sstevel@tonic-gate /*
6330Sstevel@tonic-gate  * XXX workaround for certain lab configurations (see also starcat drmach.c)
6340Sstevel@tonic-gate  * Temporary code to get around observed incorrect results from
6350Sstevel@tonic-gate  * kphysm_del_span_query when the queried span contains address spans
6360Sstevel@tonic-gate  * not occupied by memory in between spans that do have memory.
6370Sstevel@tonic-gate  * This routine acts as a wrapper to kphysm_del_span_query.  It builds
6380Sstevel@tonic-gate  * a memlist from phys_install of spans that exist between base and
6390Sstevel@tonic-gate  * base + npages, inclusively.  Kphysm_del_span_query is called for each
6400Sstevel@tonic-gate  * node in the memlist with the results accumulated in *mp.
6410Sstevel@tonic-gate  */
6420Sstevel@tonic-gate static int
6430Sstevel@tonic-gate dr_del_span_query(pfn_t base, pgcnt_t npages, memquery_t *mp)
6440Sstevel@tonic-gate {
6450Sstevel@tonic-gate 	uint64_t	 pa = _ptob64(base);
6460Sstevel@tonic-gate 	uint64_t	 sm = ~ (137438953472ull - 1);
6470Sstevel@tonic-gate 	uint64_t	 sa = pa & sm;
6480Sstevel@tonic-gate 	struct memlist	*mlist, *ml;
6490Sstevel@tonic-gate 	int		 rv;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	npages = npages; /* silence lint */
6520Sstevel@tonic-gate 	memlist_read_lock();
6530Sstevel@tonic-gate 	mlist = memlist_dup(phys_install);
6540Sstevel@tonic-gate 	memlist_read_unlock();
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate again:
6570Sstevel@tonic-gate 	for (ml = mlist; ml; ml = ml->next) {
6580Sstevel@tonic-gate 		if ((ml->address & sm) != sa) {
65911066Srafael.vanoni@sun.com 			mlist = memlist_del_span(mlist, ml->address, ml->size);
6600Sstevel@tonic-gate 			goto again;
6610Sstevel@tonic-gate 		}
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	mp->phys_pages = 0;
6650Sstevel@tonic-gate 	mp->managed = 0;
6660Sstevel@tonic-gate 	mp->nonrelocatable = 0;
6670Sstevel@tonic-gate 	mp->first_nonrelocatable = (pfn_t)-1;	/* XXX */
6680Sstevel@tonic-gate 	mp->last_nonrelocatable = 0;
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	for (ml = mlist; ml; ml = ml->next) {
6710Sstevel@tonic-gate 		memquery_t mq;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 		rv = kphysm_del_span_query(
67411066Srafael.vanoni@sun.com 		    _b64top(ml->address), _b64top(ml->size), &mq);
6750Sstevel@tonic-gate 		if (rv)
6760Sstevel@tonic-gate 			break;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 		mp->phys_pages += mq.phys_pages;
6790Sstevel@tonic-gate 		mp->managed += mq.managed;
6800Sstevel@tonic-gate 		mp->nonrelocatable += mq.nonrelocatable;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 		if (mq.nonrelocatable != 0) {
6830Sstevel@tonic-gate 			if (mq.first_nonrelocatable < mp->first_nonrelocatable)
6840Sstevel@tonic-gate 				mp->first_nonrelocatable =
68511066Srafael.vanoni@sun.com 				    mq.first_nonrelocatable;
6860Sstevel@tonic-gate 			if (mq.last_nonrelocatable > mp->last_nonrelocatable)
6870Sstevel@tonic-gate 				mp->last_nonrelocatable =
68811066Srafael.vanoni@sun.com 				    mq.last_nonrelocatable;
6890Sstevel@tonic-gate 		}
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	if (mp->nonrelocatable == 0)
6930Sstevel@tonic-gate 		mp->first_nonrelocatable = 0;	/* XXX */
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	memlist_delete(mlist);
6960Sstevel@tonic-gate 	return (rv);
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate #define	kphysm_del_span_query dr_del_span_query
7000Sstevel@tonic-gate #endif /* _STARFIRE */
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate /*
7030Sstevel@tonic-gate  * NOTE: This routine is only partially smart about multiple
7040Sstevel@tonic-gate  *	 mem-units.  Need to make mem-status structure smart
7050Sstevel@tonic-gate  *	 about them also.
7060Sstevel@tonic-gate  */
7070Sstevel@tonic-gate int
7080Sstevel@tonic-gate dr_mem_status(dr_handle_t *hp, dr_devset_t devset, sbd_dev_stat_t *dsp)
7090Sstevel@tonic-gate {
7100Sstevel@tonic-gate 	int		m, mix;
7110Sstevel@tonic-gate 	memdelstat_t	mdst;
7120Sstevel@tonic-gate 	memquery_t	mq;
7130Sstevel@tonic-gate 	dr_board_t	*bp;
7140Sstevel@tonic-gate 	dr_mem_unit_t	*mp;
7150Sstevel@tonic-gate 	sbd_mem_stat_t	*msp;
7160Sstevel@tonic-gate 	static fn_t	f = "dr_mem_status";
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	bp = hp->h_bd;
7190Sstevel@tonic-gate 	devset &= DR_DEVS_PRESENT(bp);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	for (m = mix = 0; m < MAX_MEM_UNITS_PER_BOARD; m++) {
7220Sstevel@tonic-gate 		int		rv;
7230Sstevel@tonic-gate 		sbd_error_t	*err;
7240Sstevel@tonic-gate 		drmach_status_t	 pstat;
7250Sstevel@tonic-gate 		dr_mem_unit_t	*p_mp;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_MEM, m) == 0)
7280Sstevel@tonic-gate 			continue;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 		mp = dr_get_mem_unit(bp, m);
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 		if (mp->sbm_cm.sbdev_state == DR_STATE_EMPTY) {
7330Sstevel@tonic-gate 			/* present, but not fully initialized */
7340Sstevel@tonic-gate 			continue;
7350Sstevel@tonic-gate 		}
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 		if (mp->sbm_cm.sbdev_id == (drmachid_t)0)
7380Sstevel@tonic-gate 			continue;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 		/* fetch platform status */
7410Sstevel@tonic-gate 		err = drmach_status(mp->sbm_cm.sbdev_id, &pstat);
7420Sstevel@tonic-gate 		if (err) {
7430Sstevel@tonic-gate 			DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
7440Sstevel@tonic-gate 			continue;
7450Sstevel@tonic-gate 		}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 		msp = &dsp->d_mem;
7480Sstevel@tonic-gate 		bzero((caddr_t)msp, sizeof (*msp));
7490Sstevel@tonic-gate 
750*11311SSurya.Prakki@Sun.COM 		(void) strncpy(msp->ms_cm.c_id.c_name, pstat.type,
75111066Srafael.vanoni@sun.com 		    sizeof (msp->ms_cm.c_id.c_name));
7520Sstevel@tonic-gate 		msp->ms_cm.c_id.c_type = mp->sbm_cm.sbdev_type;
7530Sstevel@tonic-gate 		msp->ms_cm.c_id.c_unit = SBD_NULL_UNIT;
7540Sstevel@tonic-gate 		msp->ms_cm.c_cond = mp->sbm_cm.sbdev_cond;
7550Sstevel@tonic-gate 		msp->ms_cm.c_busy = mp->sbm_cm.sbdev_busy | pstat.busy;
7560Sstevel@tonic-gate 		msp->ms_cm.c_time = mp->sbm_cm.sbdev_time;
7570Sstevel@tonic-gate 		msp->ms_cm.c_ostate = mp->sbm_cm.sbdev_ostate;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 		msp->ms_totpages = mp->sbm_npages;
7600Sstevel@tonic-gate 		msp->ms_basepfn = mp->sbm_basepfn;
7610Sstevel@tonic-gate 		msp->ms_pageslost = mp->sbm_pageslost;
7620Sstevel@tonic-gate 		msp->ms_cage_enabled = kcage_on;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 		if (mp->sbm_flags & DR_MFLAG_RESERVED)
7650Sstevel@tonic-gate 			p_mp = mp->sbm_peer;
7660Sstevel@tonic-gate 		else
7670Sstevel@tonic-gate 			p_mp = NULL;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 		if (p_mp == NULL) {
7700Sstevel@tonic-gate 			msp->ms_peer_is_target = 0;
7710Sstevel@tonic-gate 			msp->ms_peer_ap_id[0] = '\0';
7720Sstevel@tonic-gate 		} else if (p_mp->sbm_flags & DR_MFLAG_RESERVED) {
7730Sstevel@tonic-gate 			char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7740Sstevel@tonic-gate 			char *minor;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 			/*
7770Sstevel@tonic-gate 			 * b_dip doesn't have to be held for ddi_pathname()
7780Sstevel@tonic-gate 			 * because the board struct (dr_board_t) will be
7790Sstevel@tonic-gate 			 * destroyed before b_dip detaches.
7800Sstevel@tonic-gate 			 */
7810Sstevel@tonic-gate 			(void) ddi_pathname(bp->b_dip, path);
7820Sstevel@tonic-gate 			minor = strchr(p_mp->sbm_cm.sbdev_path, ':');
7830Sstevel@tonic-gate 
784*11311SSurya.Prakki@Sun.COM 			(void) snprintf(msp->ms_peer_ap_id,
7850Sstevel@tonic-gate 			    sizeof (msp->ms_peer_ap_id), "%s%s",
7860Sstevel@tonic-gate 			    path, (minor == NULL) ? "" : minor);
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 			kmem_free(path, MAXPATHLEN);
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 			if (p_mp->sbm_flags & DR_MFLAG_TARGET)
7910Sstevel@tonic-gate 				msp->ms_peer_is_target = 1;
7920Sstevel@tonic-gate 		}
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 		if (mp->sbm_flags & DR_MFLAG_RELOWNER)
7950Sstevel@tonic-gate 			rv = kphysm_del_status(mp->sbm_memhandle, &mdst);
7960Sstevel@tonic-gate 		else
7970Sstevel@tonic-gate 			rv = KPHYSM_EHANDLE;	/* force 'if' to fail */
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 		if (rv == KPHYSM_OK) {
8000Sstevel@tonic-gate 			/*
8010Sstevel@tonic-gate 			 * Any pages above managed is "free",
8020Sstevel@tonic-gate 			 * i.e. it's collected.
8030Sstevel@tonic-gate 			 */
8040Sstevel@tonic-gate 			msp->ms_detpages += (uint_t)(mdst.collected +
8050Sstevel@tonic-gate 			    mdst.phys_pages - mdst.managed);
8060Sstevel@tonic-gate 		} else {
8070Sstevel@tonic-gate 			/*
8080Sstevel@tonic-gate 			 * If we're UNREFERENCED or UNCONFIGURED,
8090Sstevel@tonic-gate 			 * then the number of detached pages is
8100Sstevel@tonic-gate 			 * however many pages are on the board.
8110Sstevel@tonic-gate 			 * I.e. detached = not in use by OS.
8120Sstevel@tonic-gate 			 */
8130Sstevel@tonic-gate 			switch (msp->ms_cm.c_ostate) {
8140Sstevel@tonic-gate 			/*
8150Sstevel@tonic-gate 			 * changed to use cfgadm states
8160Sstevel@tonic-gate 			 *
8170Sstevel@tonic-gate 			 * was:
8180Sstevel@tonic-gate 			 *	case DR_STATE_UNREFERENCED:
8190Sstevel@tonic-gate 			 *	case DR_STATE_UNCONFIGURED:
8200Sstevel@tonic-gate 			 */
8210Sstevel@tonic-gate 			case SBD_STAT_UNCONFIGURED:
8220Sstevel@tonic-gate 				msp->ms_detpages = msp->ms_totpages;
8230Sstevel@tonic-gate 				break;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 			default:
8260Sstevel@tonic-gate 				break;
8270Sstevel@tonic-gate 			}
8280Sstevel@tonic-gate 		}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 		/*
8310Sstevel@tonic-gate 		 * kphysm_del_span_query can report non-reloc pages = total
8320Sstevel@tonic-gate 		 * pages for memory that is not yet configured
8330Sstevel@tonic-gate 		 */
8340Sstevel@tonic-gate 		if (mp->sbm_cm.sbdev_state != DR_STATE_UNCONFIGURED) {
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 			rv = kphysm_del_span_query(mp->sbm_basepfn,
8370Sstevel@tonic-gate 			    mp->sbm_npages, &mq);
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 			if (rv == KPHYSM_OK) {
8400Sstevel@tonic-gate 				msp->ms_managed_pages = mq.managed;
8410Sstevel@tonic-gate 				msp->ms_noreloc_pages = mq.nonrelocatable;
8420Sstevel@tonic-gate 				msp->ms_noreloc_first =
8430Sstevel@tonic-gate 				    mq.first_nonrelocatable;
8440Sstevel@tonic-gate 				msp->ms_noreloc_last =
8450Sstevel@tonic-gate 				    mq.last_nonrelocatable;
8460Sstevel@tonic-gate 				msp->ms_cm.c_sflags = 0;
8470Sstevel@tonic-gate 				if (mq.nonrelocatable) {
8480Sstevel@tonic-gate 					SBD_SET_SUSPEND(SBD_CMD_UNCONFIGURE,
8490Sstevel@tonic-gate 					    msp->ms_cm.c_sflags);
8500Sstevel@tonic-gate 				}
8510Sstevel@tonic-gate 			} else {
8520Sstevel@tonic-gate 				PR_MEM("%s: kphysm_del_span_query() = %d\n",
8530Sstevel@tonic-gate 				    f, rv);
8540Sstevel@tonic-gate 			}
8550Sstevel@tonic-gate 		}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 		/*
8580Sstevel@tonic-gate 		 * Check source unit state during copy-rename
8590Sstevel@tonic-gate 		 */
8600Sstevel@tonic-gate 		if ((mp->sbm_flags & DR_MFLAG_SOURCE) &&
8610Sstevel@tonic-gate 		    (mp->sbm_cm.sbdev_state == DR_STATE_UNREFERENCED ||
8620Sstevel@tonic-gate 		    mp->sbm_cm.sbdev_state == DR_STATE_RELEASE))
8630Sstevel@tonic-gate 			msp->ms_cm.c_ostate = SBD_STAT_CONFIGURED;
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 		mix++;
8660Sstevel@tonic-gate 		dsp++;
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	return (mix);
8700Sstevel@tonic-gate }
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate int
8730Sstevel@tonic-gate dr_pre_attach_mem(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
8740Sstevel@tonic-gate {
8750Sstevel@tonic-gate 	_NOTE(ARGUNUSED(hp))
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	int		err_flag = 0;
8780Sstevel@tonic-gate 	int		d;
8790Sstevel@tonic-gate 	sbd_error_t	*err;
8800Sstevel@tonic-gate 	static fn_t	f = "dr_pre_attach_mem";
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
8850Sstevel@tonic-gate 		dr_mem_unit_t	*mp = (dr_mem_unit_t *)devlist[d];
8860Sstevel@tonic-gate 		dr_state_t	state;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 		cmn_err(CE_CONT, "OS configure %s", mp->sbm_cm.sbdev_path);
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 		state = mp->sbm_cm.sbdev_state;
8910Sstevel@tonic-gate 		switch (state) {
8920Sstevel@tonic-gate 		case DR_STATE_UNCONFIGURED:
8930Sstevel@tonic-gate 			PR_MEM("%s: recovering from UNCONFIG for %s\n",
89411066Srafael.vanoni@sun.com 			    f,
89511066Srafael.vanoni@sun.com 			    mp->sbm_cm.sbdev_path);
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 			/* use memlist cached by dr_post_detach_mem_unit */
8980Sstevel@tonic-gate 			ASSERT(mp->sbm_mlist != NULL);
8990Sstevel@tonic-gate 			PR_MEM("%s: re-configuring cached memlist for %s:\n",
90011066Srafael.vanoni@sun.com 			    f, mp->sbm_cm.sbdev_path);
9010Sstevel@tonic-gate 			PR_MEMLIST_DUMP(mp->sbm_mlist);
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 			/* kphysm del handle should be have been freed */
9040Sstevel@tonic-gate 			ASSERT((mp->sbm_flags & DR_MFLAG_RELOWNER) == 0);
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 			/*FALLTHROUGH*/
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 		case DR_STATE_CONNECTED:
9090Sstevel@tonic-gate 			PR_MEM("%s: reprogramming mem hardware on %s\n",
91011066Srafael.vanoni@sun.com 			    f, mp->sbm_cm.sbdev_bp->b_path);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 			PR_MEM("%s: enabling %s\n",
91311066Srafael.vanoni@sun.com 			    f, mp->sbm_cm.sbdev_path);
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 			err = drmach_mem_enable(mp->sbm_cm.sbdev_id);
9160Sstevel@tonic-gate 			if (err) {
9170Sstevel@tonic-gate 				DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
9180Sstevel@tonic-gate 				err_flag = 1;
9190Sstevel@tonic-gate 			}
9200Sstevel@tonic-gate 			break;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 		default:
9230Sstevel@tonic-gate 			dr_dev_err(CE_WARN, &mp->sbm_cm, ESBD_STATE);
9240Sstevel@tonic-gate 			err_flag = 1;
9250Sstevel@tonic-gate 			break;
9260Sstevel@tonic-gate 		}
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 		/* exit for loop if error encountered */
9290Sstevel@tonic-gate 		if (err_flag)
9300Sstevel@tonic-gate 			break;
9310Sstevel@tonic-gate 	}
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	return (err_flag ? -1 : 0);
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate int
9370Sstevel@tonic-gate dr_post_attach_mem(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
9380Sstevel@tonic-gate {
9390Sstevel@tonic-gate 	_NOTE(ARGUNUSED(hp))
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	int		d;
9420Sstevel@tonic-gate 	static fn_t	f = "dr_post_attach_mem";
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
9470Sstevel@tonic-gate 		dr_mem_unit_t	*mp = (dr_mem_unit_t *)devlist[d];
9480Sstevel@tonic-gate 		struct memlist	*mlist, *ml;
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 		mlist = dr_get_memlist(mp);
9510Sstevel@tonic-gate 		if (mlist == NULL) {
9520Sstevel@tonic-gate 			dr_dev_err(CE_WARN, &mp->sbm_cm, ESBD_MEMFAIL);
9530Sstevel@tonic-gate 			continue;
9540Sstevel@tonic-gate 		}
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 		/*
9570Sstevel@tonic-gate 		 * Verify the memory really did successfully attach
9580Sstevel@tonic-gate 		 * by checking for its existence in phys_install.
9590Sstevel@tonic-gate 		 */
9600Sstevel@tonic-gate 		memlist_read_lock();
9610Sstevel@tonic-gate 		if (memlist_intersect(phys_install, mlist) == 0) {
9620Sstevel@tonic-gate 			memlist_read_unlock();
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 			DR_DEV_INTERNAL_ERROR(&mp->sbm_cm);
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 			PR_MEM("%s: %s memlist not in phys_install",
96711066Srafael.vanoni@sun.com 			    f, mp->sbm_cm.sbdev_path);
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 			memlist_delete(mlist);
9700Sstevel@tonic-gate 			continue;
9710Sstevel@tonic-gate 		}
9720Sstevel@tonic-gate 		memlist_read_unlock();
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 		for (ml = mlist; ml != NULL; ml = ml->next) {
9750Sstevel@tonic-gate 			sbd_error_t *err;
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 			err = drmach_mem_add_span(
97811066Srafael.vanoni@sun.com 			    mp->sbm_cm.sbdev_id,
97911066Srafael.vanoni@sun.com 			    ml->address,
98011066Srafael.vanoni@sun.com 			    ml->size);
9810Sstevel@tonic-gate 			if (err)
9820Sstevel@tonic-gate 				DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
9830Sstevel@tonic-gate 		}
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 		memlist_delete(mlist);
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 		/*
9880Sstevel@tonic-gate 		 * Destroy cached memlist, if any.
9890Sstevel@tonic-gate 		 * There will be a cached memlist in sbm_mlist if
9900Sstevel@tonic-gate 		 * this board is being configured directly after
9910Sstevel@tonic-gate 		 * an unconfigure.
9920Sstevel@tonic-gate 		 * To support this transition, dr_post_detach_mem
9930Sstevel@tonic-gate 		 * left a copy of the last known memlist in sbm_mlist.
9940Sstevel@tonic-gate 		 * This memlist could differ from any derived from
9950Sstevel@tonic-gate 		 * hardware if while this memunit was last configured
9960Sstevel@tonic-gate 		 * the system detected and deleted bad pages from
9970Sstevel@tonic-gate 		 * phys_install.  The location of those bad pages
9980Sstevel@tonic-gate 		 * will be reflected in the cached memlist.
9990Sstevel@tonic-gate 		 */
10000Sstevel@tonic-gate 		if (mp->sbm_mlist) {
10010Sstevel@tonic-gate 			memlist_delete(mp->sbm_mlist);
10020Sstevel@tonic-gate 			mp->sbm_mlist = NULL;
10030Sstevel@tonic-gate 		}
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate /*
10060Sstevel@tonic-gate  * TODO: why is this call to dr_init_mem_unit_data here?
10070Sstevel@tonic-gate  * this has been done at discovery or connect time, so this is
10080Sstevel@tonic-gate  * probably redundant and unnecessary.
10090Sstevel@tonic-gate  */
10100Sstevel@tonic-gate 		dr_init_mem_unit_data(mp);
10110Sstevel@tonic-gate 	}
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	return (0);
10140Sstevel@tonic-gate }
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate int
10170Sstevel@tonic-gate dr_pre_detach_mem(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
10180Sstevel@tonic-gate {
10190Sstevel@tonic-gate 	_NOTE(ARGUNUSED(hp))
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	int d;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
10240Sstevel@tonic-gate 		dr_mem_unit_t *mp = (dr_mem_unit_t *)devlist[d];
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 		cmn_err(CE_CONT, "OS unconfigure %s", mp->sbm_cm.sbdev_path);
10270Sstevel@tonic-gate 	}
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	return (0);
10300Sstevel@tonic-gate }
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate int
10340Sstevel@tonic-gate dr_post_detach_mem(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
10350Sstevel@tonic-gate {
10360Sstevel@tonic-gate 	_NOTE(ARGUNUSED(hp))
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	int		d, rv;
10390Sstevel@tonic-gate 	static fn_t	f = "dr_post_detach_mem";
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	rv = 0;
10440Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
10450Sstevel@tonic-gate 		dr_mem_unit_t	*mp = (dr_mem_unit_t *)devlist[d];
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 		ASSERT(mp->sbm_cm.sbdev_bp == hp->h_bd);
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 		if (dr_post_detach_mem_unit(mp))
10500Sstevel@tonic-gate 			rv = -1;
10510Sstevel@tonic-gate 	}
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	return (rv);
10540Sstevel@tonic-gate }
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate static void
10570Sstevel@tonic-gate dr_add_memory_spans(dr_mem_unit_t *mp, struct memlist *ml)
10580Sstevel@tonic-gate {
10590Sstevel@tonic-gate 	static fn_t	f = "dr_add_memory_spans";
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	PR_MEM("%s...", f);
10620Sstevel@tonic-gate 	PR_MEMLIST_DUMP(ml);
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate #ifdef DEBUG
10650Sstevel@tonic-gate 	memlist_read_lock();
10660Sstevel@tonic-gate 	if (memlist_intersect(phys_install, ml)) {
10670Sstevel@tonic-gate 		PR_MEM("%s:WARNING: memlist intersects with phys_install\n", f);
10680Sstevel@tonic-gate 	}
10690Sstevel@tonic-gate 	memlist_read_unlock();
10700Sstevel@tonic-gate #endif
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	for (; ml; ml = ml->next) {
10730Sstevel@tonic-gate 		pfn_t		 base;
10740Sstevel@tonic-gate 		pgcnt_t		 npgs;
10750Sstevel@tonic-gate 		int		 rv;
10760Sstevel@tonic-gate 		sbd_error_t	*err;
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 		base = _b64top(ml->address);
10790Sstevel@tonic-gate 		npgs = _b64top(ml->size);
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 		rv = kphysm_add_memory_dynamic(base, npgs);
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 		err = drmach_mem_add_span(
108411066Srafael.vanoni@sun.com 		    mp->sbm_cm.sbdev_id,
108511066Srafael.vanoni@sun.com 		    ml->address,
108611066Srafael.vanoni@sun.com 		    ml->size);
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 		if (err)
10890Sstevel@tonic-gate 			DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 		if (rv != KPHYSM_OK) {
10920Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s:"
109311066Srafael.vanoni@sun.com 			    " unexpected kphysm_add_memory_dynamic"
109411066Srafael.vanoni@sun.com 			    " return value %d;"
109511066Srafael.vanoni@sun.com 			    " basepfn=0x%lx, npages=%ld\n",
109611066Srafael.vanoni@sun.com 			    f, rv, base, npgs);
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 			continue;
10990Sstevel@tonic-gate 		}
11000Sstevel@tonic-gate 	}
11010Sstevel@tonic-gate }
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate static int
11040Sstevel@tonic-gate dr_post_detach_mem_unit(dr_mem_unit_t *s_mp)
11050Sstevel@tonic-gate {
11060Sstevel@tonic-gate 	uint64_t	sz = s_mp->sbm_slice_size;
11070Sstevel@tonic-gate 	uint64_t	sm = sz - 1;
11080Sstevel@tonic-gate 	/* old and new below refer to PAs before and after copy-rename */
11090Sstevel@tonic-gate 	uint64_t	s_old_basepa, s_new_basepa;
11100Sstevel@tonic-gate 	uint64_t	t_old_basepa, t_new_basepa;
11110Sstevel@tonic-gate 	uint64_t	t_new_smallsize = 0;
11120Sstevel@tonic-gate 	dr_mem_unit_t	*t_mp, *x_mp;
11130Sstevel@tonic-gate 	struct memlist	*ml;
11140Sstevel@tonic-gate 	int		rv;
11150Sstevel@tonic-gate 	sbd_error_t	*err;
11160Sstevel@tonic-gate 	static fn_t	f = "dr_post_detach_mem_unit";
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	/* s_mp->sbm_del_mlist could be NULL, meaning no deleted spans */
11210Sstevel@tonic-gate 	PR_MEM("%s: %s: deleted memlist (EMPTY maybe okay):\n",
112211066Srafael.vanoni@sun.com 	    f, s_mp->sbm_cm.sbdev_path);
11230Sstevel@tonic-gate 	PR_MEMLIST_DUMP(s_mp->sbm_del_mlist);
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	/* sanity check */
11260Sstevel@tonic-gate 	ASSERT(s_mp->sbm_del_mlist == NULL ||
112711066Srafael.vanoni@sun.com 	    (s_mp->sbm_flags & DR_MFLAG_RELDONE) != 0);
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	if (s_mp->sbm_flags & DR_MFLAG_SOURCE) {
11300Sstevel@tonic-gate 		t_mp = s_mp->sbm_peer;
11310Sstevel@tonic-gate 		ASSERT(t_mp != NULL);
11320Sstevel@tonic-gate 		ASSERT(t_mp->sbm_flags & DR_MFLAG_TARGET);
11330Sstevel@tonic-gate 		ASSERT(t_mp->sbm_peer == s_mp);
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 		ASSERT(t_mp->sbm_flags & DR_MFLAG_RELDONE);
11360Sstevel@tonic-gate 		ASSERT(t_mp->sbm_del_mlist);
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 		PR_MEM("%s: target %s: deleted memlist:\n",
113911066Srafael.vanoni@sun.com 		    f, t_mp->sbm_cm.sbdev_path);
11400Sstevel@tonic-gate 		PR_MEMLIST_DUMP(t_mp->sbm_del_mlist);
11410Sstevel@tonic-gate 	} else {
11420Sstevel@tonic-gate 		/* this is no target unit */
11430Sstevel@tonic-gate 		t_mp = NULL;
11440Sstevel@tonic-gate 	}
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	/*
11470Sstevel@tonic-gate 	 * Verify the memory really did successfully detach
11480Sstevel@tonic-gate 	 * by checking for its non-existence in phys_install.
11490Sstevel@tonic-gate 	 */
11500Sstevel@tonic-gate 	rv = 0;
11510Sstevel@tonic-gate 	memlist_read_lock();
11520Sstevel@tonic-gate 	if (s_mp->sbm_flags & DR_MFLAG_RELDONE) {
11530Sstevel@tonic-gate 		x_mp = s_mp;
11540Sstevel@tonic-gate 		rv = memlist_intersect(phys_install, x_mp->sbm_del_mlist);
11550Sstevel@tonic-gate 	}
11560Sstevel@tonic-gate 	if (rv == 0 && t_mp && (t_mp->sbm_flags & DR_MFLAG_RELDONE)) {
11570Sstevel@tonic-gate 		x_mp = t_mp;
11580Sstevel@tonic-gate 		rv = memlist_intersect(phys_install, x_mp->sbm_del_mlist);
11590Sstevel@tonic-gate 	}
11600Sstevel@tonic-gate 	memlist_read_unlock();
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	if (rv) {
11630Sstevel@tonic-gate 		/* error: memlist still in phys_install */
11640Sstevel@tonic-gate 		DR_DEV_INTERNAL_ERROR(&x_mp->sbm_cm);
11650Sstevel@tonic-gate 	}
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	/*
11680Sstevel@tonic-gate 	 * clean mem unit state and bail out if an error has been recorded.
11690Sstevel@tonic-gate 	 */
11700Sstevel@tonic-gate 	rv = 0;
11710Sstevel@tonic-gate 	if (s_mp->sbm_cm.sbdev_error) {
11720Sstevel@tonic-gate 		PR_MEM("%s: %s flags=%x", f,
117311066Srafael.vanoni@sun.com 		    s_mp->sbm_cm.sbdev_path, s_mp->sbm_flags);
11740Sstevel@tonic-gate 		DR_DEV_CLR_UNREFERENCED(&s_mp->sbm_cm);
11750Sstevel@tonic-gate 		DR_DEV_CLR_RELEASED(&s_mp->sbm_cm);
11760Sstevel@tonic-gate 		dr_device_transition(&s_mp->sbm_cm, DR_STATE_CONFIGURED);
11770Sstevel@tonic-gate 		rv = -1;
11780Sstevel@tonic-gate 	}
11790Sstevel@tonic-gate 	if (t_mp != NULL && t_mp->sbm_cm.sbdev_error != NULL) {
11800Sstevel@tonic-gate 		PR_MEM("%s: %s flags=%x", f,
118111066Srafael.vanoni@sun.com 		    s_mp->sbm_cm.sbdev_path, s_mp->sbm_flags);
11820Sstevel@tonic-gate 		DR_DEV_CLR_UNREFERENCED(&t_mp->sbm_cm);
11830Sstevel@tonic-gate 		DR_DEV_CLR_RELEASED(&t_mp->sbm_cm);
11840Sstevel@tonic-gate 		dr_device_transition(&t_mp->sbm_cm, DR_STATE_CONFIGURED);
11850Sstevel@tonic-gate 		rv = -1;
11860Sstevel@tonic-gate 	}
11870Sstevel@tonic-gate 	if (rv)
11880Sstevel@tonic-gate 		goto cleanup;
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	s_old_basepa = _ptob64(s_mp->sbm_basepfn);
11910Sstevel@tonic-gate 	err = drmach_mem_get_base_physaddr(s_mp->sbm_cm.sbdev_id,
11920Sstevel@tonic-gate 	    &s_new_basepa);
11930Sstevel@tonic-gate 	ASSERT(err == NULL);
11940Sstevel@tonic-gate 
1195930Smathue 	PR_MEM("%s:s_old_basepa: 0x%lx\n", f, s_old_basepa);
1196930Smathue 	PR_MEM("%s:s_new_basepa: 0x%lx\n", f, s_new_basepa);
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	if (t_mp != NULL) {
11990Sstevel@tonic-gate 		struct memlist *s_copy_mlist;
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 		t_old_basepa	= _ptob64(t_mp->sbm_basepfn);
12020Sstevel@tonic-gate 		err = drmach_mem_get_base_physaddr(t_mp->sbm_cm.sbdev_id,
12030Sstevel@tonic-gate 		    &t_new_basepa);
12040Sstevel@tonic-gate 		ASSERT(err == NULL);
12050Sstevel@tonic-gate 
1206930Smathue 		PR_MEM("%s:t_old_basepa: 0x%lx\n", f, t_old_basepa);
1207930Smathue 		PR_MEM("%s:t_new_basepa: 0x%lx\n", f, t_new_basepa);
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 		/*
12100Sstevel@tonic-gate 		 * Construct copy list with original source addresses.
12110Sstevel@tonic-gate 		 * Used to add back excess target mem.
12120Sstevel@tonic-gate 		 */
12130Sstevel@tonic-gate 		s_copy_mlist = memlist_dup(s_mp->sbm_mlist);
12140Sstevel@tonic-gate 		for (ml = s_mp->sbm_del_mlist; ml; ml = ml->next) {
12150Sstevel@tonic-gate 			s_copy_mlist = memlist_del_span(s_copy_mlist,
12160Sstevel@tonic-gate 			    ml->address, ml->size);
12170Sstevel@tonic-gate 		}
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 		PR_MEM("%s: source copy list:\n:", f);
12200Sstevel@tonic-gate 		PR_MEMLIST_DUMP(s_copy_mlist);
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 		/*
12230Sstevel@tonic-gate 		 * We had to swap mem-units, so update
12240Sstevel@tonic-gate 		 * memlists accordingly with new base
12250Sstevel@tonic-gate 		 * addresses.
12260Sstevel@tonic-gate 		 */
12270Sstevel@tonic-gate 		for (ml = t_mp->sbm_mlist; ml; ml = ml->next) {
12280Sstevel@tonic-gate 			ml->address -= t_old_basepa;
12290Sstevel@tonic-gate 			ml->address += t_new_basepa;
12300Sstevel@tonic-gate 		}
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 		/*
12330Sstevel@tonic-gate 		 * There is no need to explicitly rename the target delete
12340Sstevel@tonic-gate 		 * memlist, because sbm_del_mlist and sbm_mlist always
12350Sstevel@tonic-gate 		 * point to the same memlist for a copy/rename operation.
12360Sstevel@tonic-gate 		 */
12370Sstevel@tonic-gate 		ASSERT(t_mp->sbm_del_mlist == t_mp->sbm_mlist);
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 		PR_MEM("%s: renamed target memlist and delete memlist:\n", f);
12400Sstevel@tonic-gate 		PR_MEMLIST_DUMP(t_mp->sbm_mlist);
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 		for (ml = s_mp->sbm_mlist; ml; ml = ml->next) {
12430Sstevel@tonic-gate 			ml->address -= s_old_basepa;
12440Sstevel@tonic-gate 			ml->address += s_new_basepa;
12450Sstevel@tonic-gate 		}
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 		PR_MEM("%s: renamed source memlist:\n", f);
12480Sstevel@tonic-gate 		PR_MEMLIST_DUMP(s_mp->sbm_mlist);
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 		/*
12510Sstevel@tonic-gate 		 * Keep track of dynamically added segments
12520Sstevel@tonic-gate 		 * since they cannot be split if we need to delete
12530Sstevel@tonic-gate 		 * excess source memory later for this board.
12540Sstevel@tonic-gate 		 */
12550Sstevel@tonic-gate 		if (t_mp->sbm_dyn_segs)
12560Sstevel@tonic-gate 			memlist_delete(t_mp->sbm_dyn_segs);
12570Sstevel@tonic-gate 		t_mp->sbm_dyn_segs = s_mp->sbm_dyn_segs;
12580Sstevel@tonic-gate 		s_mp->sbm_dyn_segs = NULL;
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 		/*
12610Sstevel@tonic-gate 		 * If the target memory range with the new target base PA
12620Sstevel@tonic-gate 		 * extends beyond the usable slice, prevent any "target excess"
12630Sstevel@tonic-gate 		 * from being added back after this copy/rename and
12640Sstevel@tonic-gate 		 * calculate the new smaller size of the target board
12650Sstevel@tonic-gate 		 * to be set as part of target cleanup. The base + npages
12660Sstevel@tonic-gate 		 * must only include the range of memory up to the end of
12670Sstevel@tonic-gate 		 * this slice. This will only be used after a category 4
12680Sstevel@tonic-gate 		 * large-to-small target type copy/rename - see comments
12690Sstevel@tonic-gate 		 * in dr_select_mem_target.
12700Sstevel@tonic-gate 		 */
12710Sstevel@tonic-gate 		if (((t_new_basepa & sm) + _ptob64(t_mp->sbm_npages)) > sz) {
12720Sstevel@tonic-gate 			t_new_smallsize = sz - (t_new_basepa & sm);
12730Sstevel@tonic-gate 		}
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 		if (s_mp->sbm_flags & DR_MFLAG_MEMRESIZE &&
12760Sstevel@tonic-gate 		    t_new_smallsize == 0) {
12770Sstevel@tonic-gate 			struct memlist	*t_excess_mlist;
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 			/*
12800Sstevel@tonic-gate 			 * Add back excess target memory.
12810Sstevel@tonic-gate 			 * Subtract out the portion of the target memory
12820Sstevel@tonic-gate 			 * node that was taken over by the source memory
12830Sstevel@tonic-gate 			 * node.
12840Sstevel@tonic-gate 			 */
12850Sstevel@tonic-gate 			t_excess_mlist = memlist_dup(t_mp->sbm_mlist);
12860Sstevel@tonic-gate 			for (ml = s_copy_mlist; ml; ml = ml->next) {
12870Sstevel@tonic-gate 				t_excess_mlist =
12880Sstevel@tonic-gate 				    memlist_del_span(t_excess_mlist,
12890Sstevel@tonic-gate 				    ml->address, ml->size);
12900Sstevel@tonic-gate 			}
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 			/*
12930Sstevel@tonic-gate 			 * Update dynamically added segs
12940Sstevel@tonic-gate 			 */
12950Sstevel@tonic-gate 			for (ml = s_mp->sbm_del_mlist; ml; ml = ml->next) {
12960Sstevel@tonic-gate 				t_mp->sbm_dyn_segs =
12970Sstevel@tonic-gate 				    memlist_del_span(t_mp->sbm_dyn_segs,
12980Sstevel@tonic-gate 				    ml->address, ml->size);
12990Sstevel@tonic-gate 			}
13000Sstevel@tonic-gate 			for (ml = t_excess_mlist; ml; ml = ml->next) {
13010Sstevel@tonic-gate 				t_mp->sbm_dyn_segs =
13020Sstevel@tonic-gate 				    memlist_cat_span(t_mp->sbm_dyn_segs,
13030Sstevel@tonic-gate 				    ml->address, ml->size);
13040Sstevel@tonic-gate 			}
13050Sstevel@tonic-gate 			PR_MEM("%s: %s: updated dynamic seg list:\n",
13060Sstevel@tonic-gate 			    f, t_mp->sbm_cm.sbdev_path);
13070Sstevel@tonic-gate 			PR_MEMLIST_DUMP(t_mp->sbm_dyn_segs);
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 			PR_MEM("%s: adding back remaining portion"
131011066Srafael.vanoni@sun.com 			    " of %s, memlist:\n",
131111066Srafael.vanoni@sun.com 			    f, t_mp->sbm_cm.sbdev_path);
13120Sstevel@tonic-gate 			PR_MEMLIST_DUMP(t_excess_mlist);
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 			dr_add_memory_spans(s_mp, t_excess_mlist);
13150Sstevel@tonic-gate 			memlist_delete(t_excess_mlist);
13160Sstevel@tonic-gate 		}
13170Sstevel@tonic-gate 		memlist_delete(s_copy_mlist);
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate #ifdef DEBUG
13200Sstevel@tonic-gate 		/*
13210Sstevel@tonic-gate 		 * Renaming s_mp->sbm_del_mlist is not necessary.  This
13220Sstevel@tonic-gate 		 * list is not used beyond this point, and in fact, is
13230Sstevel@tonic-gate 		 * disposed of at the end of this function.
13240Sstevel@tonic-gate 		 */
13250Sstevel@tonic-gate 		for (ml = s_mp->sbm_del_mlist; ml; ml = ml->next) {
13260Sstevel@tonic-gate 			ml->address -= s_old_basepa;
13270Sstevel@tonic-gate 			ml->address += s_new_basepa;
13280Sstevel@tonic-gate 		}
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 		PR_MEM("%s: renamed source delete memlist", f);
13310Sstevel@tonic-gate 		PR_MEMLIST_DUMP(s_mp->sbm_del_mlist);
13320Sstevel@tonic-gate #endif
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	}
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	if (t_mp != NULL) {
13370Sstevel@tonic-gate 		/* delete target's entire address space */
133811066Srafael.vanoni@sun.com 		err = drmach_mem_del_span(t_mp->sbm_cm.sbdev_id,
133911066Srafael.vanoni@sun.com 		    t_old_basepa & ~ sm, sz);
13400Sstevel@tonic-gate 		if (err)
13410Sstevel@tonic-gate 			DRERR_SET_C(&t_mp->sbm_cm.sbdev_error, &err);
13420Sstevel@tonic-gate 		ASSERT(err == NULL);
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 		/*
13450Sstevel@tonic-gate 		 * After the copy/rename, the original address space
13460Sstevel@tonic-gate 		 * for the source board (which is now located on the
13470Sstevel@tonic-gate 		 * target board) may now have some excess to be deleted.
13480Sstevel@tonic-gate 		 * The amount is calculated by masking the slice
13490Sstevel@tonic-gate 		 * info and keeping the slice offset from t_new_basepa.
13500Sstevel@tonic-gate 		 */
13510Sstevel@tonic-gate 		err = drmach_mem_del_span(s_mp->sbm_cm.sbdev_id,
135211066Srafael.vanoni@sun.com 		    s_old_basepa & ~ sm, t_new_basepa & sm);
13530Sstevel@tonic-gate 		if (err)
13540Sstevel@tonic-gate 			DRERR_SET_C(&s_mp->sbm_cm.sbdev_error, &err);
13550Sstevel@tonic-gate 		ASSERT(err == NULL);
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	} else {
13580Sstevel@tonic-gate 		/* delete board's entire address space */
13590Sstevel@tonic-gate 		err = drmach_mem_del_span(s_mp->sbm_cm.sbdev_id,
136011066Srafael.vanoni@sun.com 		    s_old_basepa & ~ sm, sz);
13610Sstevel@tonic-gate 		if (err)
13620Sstevel@tonic-gate 			DRERR_SET_C(&s_mp->sbm_cm.sbdev_error, &err);
13630Sstevel@tonic-gate 		ASSERT(err == NULL);
13640Sstevel@tonic-gate 	}
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate cleanup:
13670Sstevel@tonic-gate 	/* clean up target mem unit */
13680Sstevel@tonic-gate 	if (t_mp != NULL) {
13690Sstevel@tonic-gate 		memlist_delete(t_mp->sbm_del_mlist);
13700Sstevel@tonic-gate 		/* no need to delete sbm_mlist, it shares sbm_del_mlist */
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 		t_mp->sbm_del_mlist = NULL;
13730Sstevel@tonic-gate 		t_mp->sbm_mlist = NULL;
13740Sstevel@tonic-gate 		t_mp->sbm_peer = NULL;
13750Sstevel@tonic-gate 		t_mp->sbm_flags = 0;
13760Sstevel@tonic-gate 		t_mp->sbm_cm.sbdev_busy = 0;
13770Sstevel@tonic-gate 		dr_init_mem_unit_data(t_mp);
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 		/* reduce target size if new PAs go past end of usable slice */
13800Sstevel@tonic-gate 		if (t_new_smallsize > 0) {
13810Sstevel@tonic-gate 			t_mp->sbm_npages = _b64top(t_new_smallsize);
1382930Smathue 			PR_MEM("%s: target new size 0x%lx bytes\n",
138311066Srafael.vanoni@sun.com 			    f, t_new_smallsize);
13840Sstevel@tonic-gate 		}
13850Sstevel@tonic-gate 	}
13860Sstevel@tonic-gate 	if (t_mp != NULL && t_mp->sbm_cm.sbdev_error == NULL) {
13870Sstevel@tonic-gate 		/*
13880Sstevel@tonic-gate 		 * now that copy/rename has completed, undo this
13890Sstevel@tonic-gate 		 * work that was done in dr_release_mem_done.
13900Sstevel@tonic-gate 		 */
13910Sstevel@tonic-gate 		DR_DEV_CLR_UNREFERENCED(&t_mp->sbm_cm);
13920Sstevel@tonic-gate 		DR_DEV_CLR_RELEASED(&t_mp->sbm_cm);
13930Sstevel@tonic-gate 		dr_device_transition(&t_mp->sbm_cm, DR_STATE_CONFIGURED);
13940Sstevel@tonic-gate 	}
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	/*
13970Sstevel@tonic-gate 	 * clean up (source) board's mem unit structure.
13980Sstevel@tonic-gate 	 * NOTE: sbm_mlist is retained if no error has been record (in other
13990Sstevel@tonic-gate 	 * words, when s_mp->sbm_cm.sbdev_error is NULL). This memlist is
14000Sstevel@tonic-gate 	 * referred to elsewhere as the cached memlist.  The cached memlist
14010Sstevel@tonic-gate 	 * is used to re-attach (configure back in) this memunit from the
14020Sstevel@tonic-gate 	 * unconfigured state.  The memlist is retained because it may
14030Sstevel@tonic-gate 	 * represent bad pages that were detected while the memory was
14040Sstevel@tonic-gate 	 * configured into the OS.  The OS deletes bad pages from phys_install.
14050Sstevel@tonic-gate 	 * Those deletes, if any, will be represented in the cached mlist.
14060Sstevel@tonic-gate 	 */
14070Sstevel@tonic-gate 	if (s_mp->sbm_del_mlist && s_mp->sbm_del_mlist != s_mp->sbm_mlist)
14080Sstevel@tonic-gate 		memlist_delete(s_mp->sbm_del_mlist);
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	if (s_mp->sbm_cm.sbdev_error && s_mp->sbm_mlist) {
14110Sstevel@tonic-gate 		memlist_delete(s_mp->sbm_mlist);
14120Sstevel@tonic-gate 		s_mp->sbm_mlist = NULL;
14130Sstevel@tonic-gate 	}
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 	if (s_mp->sbm_dyn_segs != NULL && s_mp->sbm_cm.sbdev_error == 0) {
14160Sstevel@tonic-gate 		memlist_delete(s_mp->sbm_dyn_segs);
14170Sstevel@tonic-gate 		s_mp->sbm_dyn_segs = NULL;
14180Sstevel@tonic-gate 	}
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	s_mp->sbm_del_mlist = NULL;
14210Sstevel@tonic-gate 	s_mp->sbm_peer = NULL;
14220Sstevel@tonic-gate 	s_mp->sbm_flags = 0;
14230Sstevel@tonic-gate 	s_mp->sbm_cm.sbdev_busy = 0;
14240Sstevel@tonic-gate 	dr_init_mem_unit_data(s_mp);
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	PR_MEM("%s: cached memlist for %s:", f, s_mp->sbm_cm.sbdev_path);
14270Sstevel@tonic-gate 	PR_MEMLIST_DUMP(s_mp->sbm_mlist);
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	return (0);
14300Sstevel@tonic-gate }
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate /*
14330Sstevel@tonic-gate  * Successful return from this function will have the memory
14340Sstevel@tonic-gate  * handle in bp->b_dev[..mem-unit...].sbm_memhandle allocated
14350Sstevel@tonic-gate  * and waiting.  This routine's job is to select the memory that
14360Sstevel@tonic-gate  * actually has to be released (detached) which may not necessarily
14370Sstevel@tonic-gate  * be the same memory node that came in in devlist[],
14380Sstevel@tonic-gate  * i.e. a copy-rename is needed.
14390Sstevel@tonic-gate  */
14400Sstevel@tonic-gate int
14410Sstevel@tonic-gate dr_pre_release_mem(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
14420Sstevel@tonic-gate {
14430Sstevel@tonic-gate 	int		d;
14440Sstevel@tonic-gate 	int		err_flag = 0;
14450Sstevel@tonic-gate 	static fn_t	f = "dr_pre_release_mem";
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
14500Sstevel@tonic-gate 		dr_mem_unit_t	*mp = (dr_mem_unit_t *)devlist[d];
14510Sstevel@tonic-gate 		int		rv;
14520Sstevel@tonic-gate 		memquery_t	mq;
14530Sstevel@tonic-gate 		struct memlist	*ml;
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 		if (mp->sbm_cm.sbdev_error) {
14560Sstevel@tonic-gate 			err_flag = 1;
14570Sstevel@tonic-gate 			continue;
14580Sstevel@tonic-gate 		} else if (!kcage_on) {
14590Sstevel@tonic-gate 			dr_dev_err(CE_WARN, &mp->sbm_cm, ESBD_KCAGE_OFF);
14600Sstevel@tonic-gate 			err_flag = 1;
14610Sstevel@tonic-gate 			continue;
14620Sstevel@tonic-gate 		}
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 		if (mp->sbm_flags & DR_MFLAG_RESERVED) {
14650Sstevel@tonic-gate 			/*
14660Sstevel@tonic-gate 			 * Board is currently involved in a delete
14670Sstevel@tonic-gate 			 * memory operation. Can't detach this guy until
14680Sstevel@tonic-gate 			 * that operation completes.
14690Sstevel@tonic-gate 			 */
14700Sstevel@tonic-gate 			dr_dev_err(CE_WARN, &mp->sbm_cm, ESBD_INVAL);
14710Sstevel@tonic-gate 			err_flag = 1;
14720Sstevel@tonic-gate 			break;
14730Sstevel@tonic-gate 		}
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 		/*
14760Sstevel@tonic-gate 		 * Check whether the detaching memory requires a
14770Sstevel@tonic-gate 		 * copy-rename.
14780Sstevel@tonic-gate 		 */
14790Sstevel@tonic-gate 		ASSERT(mp->sbm_npages != 0);
148011066Srafael.vanoni@sun.com 		rv = kphysm_del_span_query(mp->sbm_basepfn, mp->sbm_npages,
148111066Srafael.vanoni@sun.com 		    &mq);
14820Sstevel@tonic-gate 		if (rv != KPHYSM_OK) {
14830Sstevel@tonic-gate 			DR_DEV_INTERNAL_ERROR(&mp->sbm_cm);
14840Sstevel@tonic-gate 			err_flag = 1;
14850Sstevel@tonic-gate 			break;
14860Sstevel@tonic-gate 		}
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 		if (mq.nonrelocatable != 0) {
14890Sstevel@tonic-gate 			if (!(dr_cmd_flags(hp) &
149011066Srafael.vanoni@sun.com 			    (SBD_FLAG_FORCE | SBD_FLAG_QUIESCE_OKAY))) {
14910Sstevel@tonic-gate 				/* caller wasn't prompted for a suspend */
14920Sstevel@tonic-gate 				dr_dev_err(CE_WARN, &mp->sbm_cm,
149311066Srafael.vanoni@sun.com 				    ESBD_QUIESCE_REQD);
14940Sstevel@tonic-gate 				err_flag = 1;
14950Sstevel@tonic-gate 				break;
14960Sstevel@tonic-gate 			}
14970Sstevel@tonic-gate 		}
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 		/* flags should be clean at this time */
15000Sstevel@tonic-gate 		ASSERT(mp->sbm_flags == 0);
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 		ASSERT(mp->sbm_mlist == NULL);		/* should be null */
15030Sstevel@tonic-gate 		ASSERT(mp->sbm_del_mlist == NULL);	/* should be null */
15040Sstevel@tonic-gate 		if (mp->sbm_mlist != NULL) {
15050Sstevel@tonic-gate 			memlist_delete(mp->sbm_mlist);
15060Sstevel@tonic-gate 			mp->sbm_mlist = NULL;
15070Sstevel@tonic-gate 		}
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 		ml = dr_get_memlist(mp);
15100Sstevel@tonic-gate 		if (ml == NULL) {
15110Sstevel@tonic-gate 			err_flag = 1;
15120Sstevel@tonic-gate 			PR_MEM("%s: no memlist found for %s\n",
151311066Srafael.vanoni@sun.com 			    f, mp->sbm_cm.sbdev_path);
15140Sstevel@tonic-gate 			continue;
15150Sstevel@tonic-gate 		}
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 		/* allocate a kphysm handle */
15180Sstevel@tonic-gate 		rv = kphysm_del_gethandle(&mp->sbm_memhandle);
15190Sstevel@tonic-gate 		if (rv != KPHYSM_OK) {
15200Sstevel@tonic-gate 			memlist_delete(ml);
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 			DR_DEV_INTERNAL_ERROR(&mp->sbm_cm);
15230Sstevel@tonic-gate 			err_flag = 1;
15240Sstevel@tonic-gate 			break;
15250Sstevel@tonic-gate 		}
15260Sstevel@tonic-gate 		mp->sbm_flags |= DR_MFLAG_RELOWNER;
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate 		if ((mq.nonrelocatable != 0) ||
152911066Srafael.vanoni@sun.com 		    dr_reserve_mem_spans(&mp->sbm_memhandle, ml)) {
15300Sstevel@tonic-gate 			/*
15310Sstevel@tonic-gate 			 * Either the detaching memory node contains
15320Sstevel@tonic-gate 			 * non-reloc memory or we failed to reserve the
15330Sstevel@tonic-gate 			 * detaching memory node (which did _not_ have
15340Sstevel@tonic-gate 			 * any non-reloc memory, i.e. some non-reloc mem
15350Sstevel@tonic-gate 			 * got onboard).
15360Sstevel@tonic-gate 			 */
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 			if (dr_select_mem_target(hp, mp, ml)) {
15390Sstevel@tonic-gate 				int rv;
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 				/*
15420Sstevel@tonic-gate 				 * We had no luck locating a target
15430Sstevel@tonic-gate 				 * memory node to be the recipient of
15440Sstevel@tonic-gate 				 * the non-reloc memory on the node
15450Sstevel@tonic-gate 				 * we're trying to detach.
15460Sstevel@tonic-gate 				 * Clean up be disposing the mem handle
15470Sstevel@tonic-gate 				 * and the mem list.
15480Sstevel@tonic-gate 				 */
15490Sstevel@tonic-gate 				rv = kphysm_del_release(mp->sbm_memhandle);
15500Sstevel@tonic-gate 				if (rv != KPHYSM_OK) {
15510Sstevel@tonic-gate 					/*
15520Sstevel@tonic-gate 					 * can do nothing but complain
15530Sstevel@tonic-gate 					 * and hope helpful for debug
15540Sstevel@tonic-gate 					 */
15550Sstevel@tonic-gate 					cmn_err(CE_WARN, "%s: unexpected"
155611066Srafael.vanoni@sun.com 					    " kphysm_del_release return"
155711066Srafael.vanoni@sun.com 					    " value %d",
155811066Srafael.vanoni@sun.com 					    f, rv);
15590Sstevel@tonic-gate 				}
15600Sstevel@tonic-gate 				mp->sbm_flags &= ~DR_MFLAG_RELOWNER;
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 				memlist_delete(ml);
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 				/* make sure sbm_flags is clean */
15650Sstevel@tonic-gate 				ASSERT(mp->sbm_flags == 0);
15660Sstevel@tonic-gate 
156711066Srafael.vanoni@sun.com 				dr_dev_err(CE_WARN, &mp->sbm_cm,
156811066Srafael.vanoni@sun.com 				    ESBD_NO_TARGET);
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 				err_flag = 1;
15710Sstevel@tonic-gate 				break;
15720Sstevel@tonic-gate 			}
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 			/*
15750Sstevel@tonic-gate 			 * ml is not memlist_delete'd here because
15760Sstevel@tonic-gate 			 * it has been assigned to mp->sbm_mlist
15770Sstevel@tonic-gate 			 * by dr_select_mem_target.
15780Sstevel@tonic-gate 			 */
15790Sstevel@tonic-gate 		} else {
15800Sstevel@tonic-gate 			/* no target needed to detach this board */
15810Sstevel@tonic-gate 			mp->sbm_flags |= DR_MFLAG_RESERVED;
15820Sstevel@tonic-gate 			mp->sbm_peer = NULL;
15830Sstevel@tonic-gate 			mp->sbm_del_mlist = ml;
15840Sstevel@tonic-gate 			mp->sbm_mlist = ml;
15850Sstevel@tonic-gate 			mp->sbm_cm.sbdev_busy = 1;
15860Sstevel@tonic-gate 		}
15870Sstevel@tonic-gate #ifdef DEBUG
15880Sstevel@tonic-gate 		ASSERT(mp->sbm_mlist != NULL);
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate 		if (mp->sbm_flags & DR_MFLAG_SOURCE) {
15910Sstevel@tonic-gate 			PR_MEM("%s: release of %s requires copy/rename;"
159211066Srafael.vanoni@sun.com 			    " selected target board %s\n",
159311066Srafael.vanoni@sun.com 			    f,
159411066Srafael.vanoni@sun.com 			    mp->sbm_cm.sbdev_path,
159511066Srafael.vanoni@sun.com 			    mp->sbm_peer->sbm_cm.sbdev_path);
15960Sstevel@tonic-gate 		} else {
15970Sstevel@tonic-gate 			PR_MEM("%s: copy/rename not required to release %s\n",
159811066Srafael.vanoni@sun.com 			    f, mp->sbm_cm.sbdev_path);
15990Sstevel@tonic-gate 		}
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 		ASSERT(mp->sbm_flags & DR_MFLAG_RELOWNER);
16020Sstevel@tonic-gate 		ASSERT(mp->sbm_flags & DR_MFLAG_RESERVED);
16030Sstevel@tonic-gate #endif
16040Sstevel@tonic-gate 	}
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 	return (err_flag ? -1 : 0);
16070Sstevel@tonic-gate }
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate void
16100Sstevel@tonic-gate dr_release_mem_done(dr_common_unit_t *cp)
16110Sstevel@tonic-gate {
16120Sstevel@tonic-gate 	dr_mem_unit_t	*s_mp = (dr_mem_unit_t *)cp;
16130Sstevel@tonic-gate 	dr_mem_unit_t *t_mp, *mp;
16140Sstevel@tonic-gate 	int		rv;
16150Sstevel@tonic-gate 	static fn_t	f = "dr_release_mem_done";
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	/*
16180Sstevel@tonic-gate 	 * This unit will be flagged with DR_MFLAG_SOURCE, if it
16190Sstevel@tonic-gate 	 * has a target unit.
16200Sstevel@tonic-gate 	 */
16210Sstevel@tonic-gate 	if (s_mp->sbm_flags & DR_MFLAG_SOURCE) {
16220Sstevel@tonic-gate 		t_mp = s_mp->sbm_peer;
16230Sstevel@tonic-gate 		ASSERT(t_mp != NULL);
16240Sstevel@tonic-gate 		ASSERT(t_mp->sbm_peer == s_mp);
16250Sstevel@tonic-gate 		ASSERT(t_mp->sbm_flags & DR_MFLAG_TARGET);
16260Sstevel@tonic-gate 		ASSERT(t_mp->sbm_flags & DR_MFLAG_RESERVED);
16270Sstevel@tonic-gate 	} else {
16280Sstevel@tonic-gate 		/* this is no target unit */
16290Sstevel@tonic-gate 		t_mp = NULL;
16300Sstevel@tonic-gate 	}
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	/* free delete handle */
16330Sstevel@tonic-gate 	ASSERT(s_mp->sbm_flags & DR_MFLAG_RELOWNER);
16340Sstevel@tonic-gate 	ASSERT(s_mp->sbm_flags & DR_MFLAG_RESERVED);
16350Sstevel@tonic-gate 	rv = kphysm_del_release(s_mp->sbm_memhandle);
16360Sstevel@tonic-gate 	if (rv != KPHYSM_OK) {
16370Sstevel@tonic-gate 		/*
16380Sstevel@tonic-gate 		 * can do nothing but complain
16390Sstevel@tonic-gate 		 * and hope helpful for debug
16400Sstevel@tonic-gate 		 */
16410Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: unexpected kphysm_del_release"
164211066Srafael.vanoni@sun.com 		    " return value %d", f, rv);
16430Sstevel@tonic-gate 	}
16440Sstevel@tonic-gate 	s_mp->sbm_flags &= ~DR_MFLAG_RELOWNER;
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	/*
16470Sstevel@tonic-gate 	 * If an error was encountered during release, clean up
16480Sstevel@tonic-gate 	 * the source (and target, if present) unit data.
16490Sstevel@tonic-gate 	 */
16500Sstevel@tonic-gate /* XXX Can we know that sbdev_error was encountered during release? */
16510Sstevel@tonic-gate 	if (s_mp->sbm_cm.sbdev_error != NULL) {
16520Sstevel@tonic-gate 		PR_MEM("%s: %s: error %d noted\n",
165311066Srafael.vanoni@sun.com 		    f,
165411066Srafael.vanoni@sun.com 		    s_mp->sbm_cm.sbdev_path,
165511066Srafael.vanoni@sun.com 		    s_mp->sbm_cm.sbdev_error->e_code);
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 		if (t_mp != NULL) {
16580Sstevel@tonic-gate 			ASSERT(t_mp->sbm_del_mlist == t_mp->sbm_mlist);
16590Sstevel@tonic-gate 			t_mp->sbm_del_mlist = NULL;
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 			if (t_mp->sbm_mlist != NULL) {
16620Sstevel@tonic-gate 				memlist_delete(t_mp->sbm_mlist);
16630Sstevel@tonic-gate 				t_mp->sbm_mlist = NULL;
16640Sstevel@tonic-gate 			}
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 			t_mp->sbm_peer = NULL;
16670Sstevel@tonic-gate 			t_mp->sbm_flags = 0;
16680Sstevel@tonic-gate 			t_mp->sbm_cm.sbdev_busy = 0;
16690Sstevel@tonic-gate 		}
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 		if (s_mp->sbm_del_mlist != s_mp->sbm_mlist)
16720Sstevel@tonic-gate 			memlist_delete(s_mp->sbm_del_mlist);
16730Sstevel@tonic-gate 		s_mp->sbm_del_mlist = NULL;
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 		if (s_mp->sbm_mlist != NULL) {
16760Sstevel@tonic-gate 			memlist_delete(s_mp->sbm_mlist);
16770Sstevel@tonic-gate 			s_mp->sbm_mlist = NULL;
16780Sstevel@tonic-gate 		}
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 		s_mp->sbm_peer = NULL;
16810Sstevel@tonic-gate 		s_mp->sbm_flags = 0;
16820Sstevel@tonic-gate 		s_mp->sbm_cm.sbdev_busy = 0;
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 		/* bail out */
16850Sstevel@tonic-gate 		return;
16860Sstevel@tonic-gate 	}
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 	DR_DEV_SET_RELEASED(&s_mp->sbm_cm);
16890Sstevel@tonic-gate 	dr_device_transition(&s_mp->sbm_cm, DR_STATE_RELEASE);
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate 	if (t_mp != NULL) {
16920Sstevel@tonic-gate 		/*
16930Sstevel@tonic-gate 		 * the kphysm delete operation that drained the source
16940Sstevel@tonic-gate 		 * board also drained this target board.  Since the source
16950Sstevel@tonic-gate 		 * board drain is now known to have succeeded, we know this
16960Sstevel@tonic-gate 		 * target board is drained too.
16970Sstevel@tonic-gate 		 *
16980Sstevel@tonic-gate 		 * because DR_DEV_SET_RELEASED and dr_device_transition
16990Sstevel@tonic-gate 		 * is done here, the dr_release_dev_done should not
17000Sstevel@tonic-gate 		 * fail.
17010Sstevel@tonic-gate 		 */
17020Sstevel@tonic-gate 		DR_DEV_SET_RELEASED(&t_mp->sbm_cm);
17030Sstevel@tonic-gate 		dr_device_transition(&t_mp->sbm_cm, DR_STATE_RELEASE);
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 		/*
17060Sstevel@tonic-gate 		 * NOTE: do not transition target's board state,
17070Sstevel@tonic-gate 		 * even if the mem-unit was the last configure
17080Sstevel@tonic-gate 		 * unit of the board.  When copy/rename completes
17090Sstevel@tonic-gate 		 * this mem-unit will transitioned back to
17100Sstevel@tonic-gate 		 * the configured state.  In the meantime, the
17110Sstevel@tonic-gate 		 * board's must remain as is.
17120Sstevel@tonic-gate 		 */
17130Sstevel@tonic-gate 	}
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 	/* if board(s) had deleted memory, verify it is gone */
17160Sstevel@tonic-gate 	rv = 0;
17170Sstevel@tonic-gate 	memlist_read_lock();
17180Sstevel@tonic-gate 	if (s_mp->sbm_del_mlist != NULL) {
17190Sstevel@tonic-gate 		mp = s_mp;
17200Sstevel@tonic-gate 		rv = memlist_intersect(phys_install, mp->sbm_del_mlist);
17210Sstevel@tonic-gate 	}
17220Sstevel@tonic-gate 	if (rv == 0 && t_mp && t_mp->sbm_del_mlist != NULL) {
17230Sstevel@tonic-gate 		mp = t_mp;
17240Sstevel@tonic-gate 		rv = memlist_intersect(phys_install, mp->sbm_del_mlist);
17250Sstevel@tonic-gate 	}
17260Sstevel@tonic-gate 	memlist_read_unlock();
17270Sstevel@tonic-gate 	if (rv) {
17280Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: %smem-unit (%d.%d): "
172911066Srafael.vanoni@sun.com 		    "deleted memory still found in phys_install",
173011066Srafael.vanoni@sun.com 		    f,
173111066Srafael.vanoni@sun.com 		    (mp == t_mp ? "target " : ""),
173211066Srafael.vanoni@sun.com 		    mp->sbm_cm.sbdev_bp->b_num,
173311066Srafael.vanoni@sun.com 		    mp->sbm_cm.sbdev_unum);
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 		DR_DEV_INTERNAL_ERROR(&s_mp->sbm_cm);
17360Sstevel@tonic-gate 		return;
17370Sstevel@tonic-gate 	}
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate 	s_mp->sbm_flags |= DR_MFLAG_RELDONE;
17400Sstevel@tonic-gate 	if (t_mp != NULL)
17410Sstevel@tonic-gate 		t_mp->sbm_flags |= DR_MFLAG_RELDONE;
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 	/* this should not fail */
17440Sstevel@tonic-gate 	if (dr_release_dev_done(&s_mp->sbm_cm) != 0) {
17450Sstevel@tonic-gate 		/* catch this in debug kernels */
17460Sstevel@tonic-gate 		ASSERT(0);
17470Sstevel@tonic-gate 		return;
17480Sstevel@tonic-gate 	}
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	PR_MEM("%s: marking %s release DONE\n",
175111066Srafael.vanoni@sun.com 	    f, s_mp->sbm_cm.sbdev_path);
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 	s_mp->sbm_cm.sbdev_ostate = SBD_STAT_UNCONFIGURED;
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	if (t_mp != NULL) {
17560Sstevel@tonic-gate 		/* should not fail */
17570Sstevel@tonic-gate 		rv = dr_release_dev_done(&t_mp->sbm_cm);
17580Sstevel@tonic-gate 		if (rv != 0) {
17590Sstevel@tonic-gate 			/* catch this in debug kernels */
17600Sstevel@tonic-gate 			ASSERT(0);
17610Sstevel@tonic-gate 			return;
17620Sstevel@tonic-gate 		}
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate 		PR_MEM("%s: marking %s release DONE\n",
176511066Srafael.vanoni@sun.com 		    f, t_mp->sbm_cm.sbdev_path);
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 		t_mp->sbm_cm.sbdev_ostate = SBD_STAT_UNCONFIGURED;
17680Sstevel@tonic-gate 	}
17690Sstevel@tonic-gate }
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate /*ARGSUSED*/
17720Sstevel@tonic-gate int
17730Sstevel@tonic-gate dr_disconnect_mem(dr_mem_unit_t *mp)
17740Sstevel@tonic-gate {
17750Sstevel@tonic-gate 	static fn_t	f = "dr_disconnect_mem";
17760Sstevel@tonic-gate 	update_membounds_t umb;
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate #ifdef DEBUG
17790Sstevel@tonic-gate 	int state = mp->sbm_cm.sbdev_state;
178011066Srafael.vanoni@sun.com 	ASSERT(state == DR_STATE_CONNECTED || state == DR_STATE_UNCONFIGURED);
17810Sstevel@tonic-gate #endif
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 	if (mp->sbm_del_mlist && mp->sbm_del_mlist != mp->sbm_mlist)
17860Sstevel@tonic-gate 		memlist_delete(mp->sbm_del_mlist);
17870Sstevel@tonic-gate 	mp->sbm_del_mlist = NULL;
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	if (mp->sbm_mlist) {
17900Sstevel@tonic-gate 		memlist_delete(mp->sbm_mlist);
17910Sstevel@tonic-gate 		mp->sbm_mlist = NULL;
17920Sstevel@tonic-gate 	}
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	/*
17950Sstevel@tonic-gate 	 * Remove memory from lgroup
17960Sstevel@tonic-gate 	 * For now, only board info is required.
17970Sstevel@tonic-gate 	 */
17980Sstevel@tonic-gate 	umb.u_board = mp->sbm_cm.sbdev_bp->b_num;
17990Sstevel@tonic-gate 	umb.u_base = (uint64_t)-1;
18000Sstevel@tonic-gate 	umb.u_len = (uint64_t)-1;
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 	lgrp_plat_config(LGRP_CONFIG_MEM_DEL, (uintptr_t)&umb);
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 	return (0);
18050Sstevel@tonic-gate }
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate int
18080Sstevel@tonic-gate dr_cancel_mem(dr_mem_unit_t *s_mp)
18090Sstevel@tonic-gate {
18100Sstevel@tonic-gate 	dr_mem_unit_t	*t_mp;
18110Sstevel@tonic-gate 	dr_state_t	state;
18120Sstevel@tonic-gate 	static fn_t	f = "dr_cancel_mem";
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 	state = s_mp->sbm_cm.sbdev_state;
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	if (s_mp->sbm_flags & DR_MFLAG_TARGET) {
18170Sstevel@tonic-gate 		/* must cancel source board, not target board */
18180Sstevel@tonic-gate 		/* TODO: set error */
18190Sstevel@tonic-gate 		return (-1);
18200Sstevel@tonic-gate 	} else if (s_mp->sbm_flags & DR_MFLAG_SOURCE) {
18210Sstevel@tonic-gate 		t_mp = s_mp->sbm_peer;
18220Sstevel@tonic-gate 		ASSERT(t_mp != NULL);
18230Sstevel@tonic-gate 		ASSERT(t_mp->sbm_peer == s_mp);
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 		/* must always match the source board's state */
18260Sstevel@tonic-gate /* TODO: is this assertion correct? */
18270Sstevel@tonic-gate 		ASSERT(t_mp->sbm_cm.sbdev_state == state);
18280Sstevel@tonic-gate 	} else {
18290Sstevel@tonic-gate 		/* this is no target unit */
18300Sstevel@tonic-gate 		t_mp = NULL;
18310Sstevel@tonic-gate 	}
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate 	switch (state) {
18340Sstevel@tonic-gate 	case DR_STATE_UNREFERENCED:	/* state set by dr_release_dev_done */
18350Sstevel@tonic-gate 		ASSERT((s_mp->sbm_flags & DR_MFLAG_RELOWNER) == 0);
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 		if (t_mp != NULL && t_mp->sbm_del_mlist != NULL) {
18380Sstevel@tonic-gate 			PR_MEM("%s: undoing target %s memory delete\n",
183911066Srafael.vanoni@sun.com 			    f, t_mp->sbm_cm.sbdev_path);
18400Sstevel@tonic-gate 			dr_add_memory_spans(t_mp, t_mp->sbm_del_mlist);
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 			DR_DEV_CLR_UNREFERENCED(&t_mp->sbm_cm);
18430Sstevel@tonic-gate 		}
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 		if (s_mp->sbm_del_mlist != NULL) {
18460Sstevel@tonic-gate 			PR_MEM("%s: undoing %s memory delete\n",
184711066Srafael.vanoni@sun.com 			    f, s_mp->sbm_cm.sbdev_path);
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 			dr_add_memory_spans(s_mp, s_mp->sbm_del_mlist);
18500Sstevel@tonic-gate 		}
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate 		/*FALLTHROUGH*/
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate /* TODO: should no longer be possible to see the release state here */
18550Sstevel@tonic-gate 	case DR_STATE_RELEASE:	/* state set by dr_release_mem_done */
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 		ASSERT((s_mp->sbm_flags & DR_MFLAG_RELOWNER) == 0);
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 		if (t_mp != NULL) {
18600Sstevel@tonic-gate 			ASSERT(t_mp->sbm_del_mlist == t_mp->sbm_mlist);
18610Sstevel@tonic-gate 			t_mp->sbm_del_mlist = NULL;
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 			if (t_mp->sbm_mlist != NULL) {
18640Sstevel@tonic-gate 				memlist_delete(t_mp->sbm_mlist);
18650Sstevel@tonic-gate 				t_mp->sbm_mlist = NULL;
18660Sstevel@tonic-gate 			}
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 			t_mp->sbm_peer = NULL;
18690Sstevel@tonic-gate 			t_mp->sbm_flags = 0;
18700Sstevel@tonic-gate 			t_mp->sbm_cm.sbdev_busy = 0;
18710Sstevel@tonic-gate 			dr_init_mem_unit_data(t_mp);
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate 			DR_DEV_CLR_RELEASED(&t_mp->sbm_cm);
18740Sstevel@tonic-gate 
187511066Srafael.vanoni@sun.com 			dr_device_transition(&t_mp->sbm_cm,
187611066Srafael.vanoni@sun.com 			    DR_STATE_CONFIGURED);
18770Sstevel@tonic-gate 		}
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 		if (s_mp->sbm_del_mlist != s_mp->sbm_mlist)
18800Sstevel@tonic-gate 			memlist_delete(s_mp->sbm_del_mlist);
18810Sstevel@tonic-gate 		s_mp->sbm_del_mlist = NULL;
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 		if (s_mp->sbm_mlist != NULL) {
18840Sstevel@tonic-gate 			memlist_delete(s_mp->sbm_mlist);
18850Sstevel@tonic-gate 			s_mp->sbm_mlist = NULL;
18860Sstevel@tonic-gate 		}
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 		s_mp->sbm_peer = NULL;
18890Sstevel@tonic-gate 		s_mp->sbm_flags = 0;
18900Sstevel@tonic-gate 		s_mp->sbm_cm.sbdev_busy = 0;
18910Sstevel@tonic-gate 		dr_init_mem_unit_data(s_mp);
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate 		return (0);
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 	default:
18960Sstevel@tonic-gate 		PR_MEM("%s: WARNING unexpected state (%d) for %s\n",
189711066Srafael.vanoni@sun.com 		    f, (int)state, s_mp->sbm_cm.sbdev_path);
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 		return (-1);
19000Sstevel@tonic-gate 	}
19010Sstevel@tonic-gate 	/*NOTREACHED*/
19020Sstevel@tonic-gate }
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate void
19050Sstevel@tonic-gate dr_init_mem_unit(dr_mem_unit_t *mp)
19060Sstevel@tonic-gate {
19070Sstevel@tonic-gate 	dr_state_t	new_state;
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate 	if (DR_DEV_IS_ATTACHED(&mp->sbm_cm)) {
19110Sstevel@tonic-gate 		new_state = DR_STATE_CONFIGURED;
19120Sstevel@tonic-gate 		mp->sbm_cm.sbdev_cond = SBD_COND_OK;
19130Sstevel@tonic-gate 	} else if (DR_DEV_IS_PRESENT(&mp->sbm_cm)) {
19140Sstevel@tonic-gate 		new_state = DR_STATE_CONNECTED;
19150Sstevel@tonic-gate 		mp->sbm_cm.sbdev_cond = SBD_COND_OK;
19160Sstevel@tonic-gate 	} else if (mp->sbm_cm.sbdev_id != (drmachid_t)0) {
19170Sstevel@tonic-gate 		new_state = DR_STATE_OCCUPIED;
19180Sstevel@tonic-gate 	} else {
19190Sstevel@tonic-gate 		new_state = DR_STATE_EMPTY;
19200Sstevel@tonic-gate 	}
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate 	if (DR_DEV_IS_PRESENT(&mp->sbm_cm))
19230Sstevel@tonic-gate 		dr_init_mem_unit_data(mp);
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 	/* delay transition until fully initialized */
19260Sstevel@tonic-gate 	dr_device_transition(&mp->sbm_cm, new_state);
19270Sstevel@tonic-gate }
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate static void
19300Sstevel@tonic-gate dr_init_mem_unit_data(dr_mem_unit_t *mp)
19310Sstevel@tonic-gate {
19320Sstevel@tonic-gate 	drmachid_t	id = mp->sbm_cm.sbdev_id;
19330Sstevel@tonic-gate 	uint64_t	bytes;
19340Sstevel@tonic-gate 	sbd_error_t	*err;
19350Sstevel@tonic-gate 	static fn_t	f = "dr_init_mem_unit_data";
19360Sstevel@tonic-gate 	update_membounds_t umb;
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 	/* a little sanity checking */
19410Sstevel@tonic-gate 	ASSERT(mp->sbm_peer == NULL);
19420Sstevel@tonic-gate 	ASSERT(mp->sbm_flags == 0);
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate 	/* get basepfn of mem unit */
19450Sstevel@tonic-gate 	err = drmach_mem_get_base_physaddr(id, &bytes);
19460Sstevel@tonic-gate 	if (err) {
19470Sstevel@tonic-gate 		DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
19480Sstevel@tonic-gate 		mp->sbm_basepfn = (pfn_t)-1;
19490Sstevel@tonic-gate 	} else
19500Sstevel@tonic-gate 		mp->sbm_basepfn = _b64top(bytes);
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 	/* attempt to get number of pages from PDA */
19530Sstevel@tonic-gate 	err = drmach_mem_get_size(id, &bytes);
19540Sstevel@tonic-gate 	if (err) {
19550Sstevel@tonic-gate 		DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
19560Sstevel@tonic-gate 		mp->sbm_npages = 0;
19570Sstevel@tonic-gate 	} else
19580Sstevel@tonic-gate 		mp->sbm_npages = _b64top(bytes);
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate 	/* if didn't work, calculate using memlist */
19610Sstevel@tonic-gate 	if (mp->sbm_npages == 0) {
19620Sstevel@tonic-gate 		struct memlist	*ml, *mlist;
19630Sstevel@tonic-gate 		/*
19640Sstevel@tonic-gate 		 * Either we couldn't open the PDA or our
19650Sstevel@tonic-gate 		 * PDA has garbage in it.  We must have the
19660Sstevel@tonic-gate 		 * page count consistent and whatever the
19670Sstevel@tonic-gate 		 * OS states has precedence over the PDA
19680Sstevel@tonic-gate 		 * so let's check the kernel.
19690Sstevel@tonic-gate 		 */
19700Sstevel@tonic-gate /* TODO: curious comment. it suggests pda query should happen if this fails */
19710Sstevel@tonic-gate 		PR_MEM("%s: PDA query failed for npages."
197211066Srafael.vanoni@sun.com 		    " Checking memlist for %s\n",
197311066Srafael.vanoni@sun.com 		    f, mp->sbm_cm.sbdev_path);
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 		mlist = dr_get_memlist(mp);
19760Sstevel@tonic-gate 		for (ml = mlist; ml; ml = ml->next)
19770Sstevel@tonic-gate 			mp->sbm_npages += btop(ml->size);
19780Sstevel@tonic-gate 		memlist_delete(mlist);
19790Sstevel@tonic-gate 	}
19800Sstevel@tonic-gate 
19810Sstevel@tonic-gate 	err = drmach_mem_get_alignment(id, &bytes);
19820Sstevel@tonic-gate 	if (err) {
19830Sstevel@tonic-gate 		DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
19840Sstevel@tonic-gate 		mp->sbm_alignment_mask = 0;
19850Sstevel@tonic-gate 	} else
19860Sstevel@tonic-gate 		mp->sbm_alignment_mask = _b64top(bytes);
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 	err = drmach_mem_get_slice_size(id, &bytes);
19890Sstevel@tonic-gate 	if (err) {
19900Sstevel@tonic-gate 		DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
19910Sstevel@tonic-gate 		mp->sbm_slice_size = 0; /* paranoia */
19920Sstevel@tonic-gate 	} else
19930Sstevel@tonic-gate 		mp->sbm_slice_size = bytes;
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	/*
19960Sstevel@tonic-gate 	 * Add memory to lgroup
19970Sstevel@tonic-gate 	 */
19980Sstevel@tonic-gate 	umb.u_board = mp->sbm_cm.sbdev_bp->b_num;
19990Sstevel@tonic-gate 	umb.u_base = (uint64_t)mp->sbm_basepfn << MMU_PAGESHIFT;
20000Sstevel@tonic-gate 	umb.u_len = (uint64_t)mp->sbm_npages << MMU_PAGESHIFT;
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 	lgrp_plat_config(LGRP_CONFIG_MEM_ADD, (uintptr_t)&umb);
20030Sstevel@tonic-gate 
2004930Smathue 	PR_MEM("%s: %s (basepfn = 0x%lx, npgs = %ld)\n",
200511066Srafael.vanoni@sun.com 	    f, mp->sbm_cm.sbdev_path, mp->sbm_basepfn, mp->sbm_npages);
20060Sstevel@tonic-gate }
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate static int
20090Sstevel@tonic-gate dr_reserve_mem_spans(memhandle_t *mhp, struct memlist *ml)
20100Sstevel@tonic-gate {
20110Sstevel@tonic-gate 	int		err;
20120Sstevel@tonic-gate 	pfn_t		base;
20130Sstevel@tonic-gate 	pgcnt_t		npgs;
20140Sstevel@tonic-gate 	struct memlist	*mc;
20150Sstevel@tonic-gate 	static fn_t	f = "dr_reserve_mem_spans";
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 	/*
20200Sstevel@tonic-gate 	 * Walk the supplied memlist scheduling each span for removal
20210Sstevel@tonic-gate 	 * with kphysm_del_span.  It is possible that a span may intersect
20220Sstevel@tonic-gate 	 * an area occupied by the cage.
20230Sstevel@tonic-gate 	 */
20240Sstevel@tonic-gate 	for (mc = ml; mc != NULL; mc = mc->next) {
20250Sstevel@tonic-gate 		base = _b64top(mc->address);
20260Sstevel@tonic-gate 		npgs = _b64top(mc->size);
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 		err = kphysm_del_span(*mhp, base, npgs);
20290Sstevel@tonic-gate 		if (err != KPHYSM_OK) {
20300Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s memory reserve failed."
203111066Srafael.vanoni@sun.com 			    " unexpected kphysm_del_span return value %d;"
203211066Srafael.vanoni@sun.com 			    " basepfn=0x%lx npages=%ld",
203311066Srafael.vanoni@sun.com 			    f, err, base, npgs);
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 			return (-1);
20360Sstevel@tonic-gate 		}
20370Sstevel@tonic-gate 	}
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 	return (0);
20400Sstevel@tonic-gate }
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate /* debug counters */
20430Sstevel@tonic-gate int dr_smt_realigned;
20440Sstevel@tonic-gate int dr_smt_preference[4];
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate #ifdef DEBUG
20470Sstevel@tonic-gate uint_t dr_ignore_board; /* if bit[bnum-1] set, board won't be candidate */
20480Sstevel@tonic-gate #endif
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate /*
20510Sstevel@tonic-gate  * Find and reserve a copy/rename target board suitable for the
20520Sstevel@tonic-gate  * given source board.
20530Sstevel@tonic-gate  * All boards in the system are examined and categorized in relation to
20540Sstevel@tonic-gate  * their memory size versus the source board's memory size.  Order of
20550Sstevel@tonic-gate  * preference is:
20560Sstevel@tonic-gate  *	1st: board has same memory size
20570Sstevel@tonic-gate  * 	2nd: board has larger memory size
20580Sstevel@tonic-gate  *	3rd: board has smaller memory size
20590Sstevel@tonic-gate  *	4th: board has smaller memory size, available memory will be reduced.
20600Sstevel@tonic-gate  * Boards in category 3 and 4 will have their MC's reprogrammed to locate the
20610Sstevel@tonic-gate  * span to which the MC responds to address span that appropriately covers
20620Sstevel@tonic-gate  * the nonrelocatable span of the source board.
20630Sstevel@tonic-gate  */
20640Sstevel@tonic-gate static int
20650Sstevel@tonic-gate dr_select_mem_target(dr_handle_t *hp,
20660Sstevel@tonic-gate 	dr_mem_unit_t *s_mp, struct memlist *s_ml)
20670Sstevel@tonic-gate {
20680Sstevel@tonic-gate 	pgcnt_t		sz = _b64top(s_mp->sbm_slice_size);
20690Sstevel@tonic-gate 	pgcnt_t		sm = sz - 1; /* mem_slice_mask */
20700Sstevel@tonic-gate 	pfn_t		s_phi, t_phi;
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 	int		n_sets = 4; /* same, larger, smaller, clipped */
20730Sstevel@tonic-gate 	int		preference; /* lower value is higher preference */
20740Sstevel@tonic-gate 	int		n_units_per_set;
20750Sstevel@tonic-gate 	int		idx;
20760Sstevel@tonic-gate 	dr_mem_unit_t	**sets;
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 	int		t_bd;
20790Sstevel@tonic-gate 	int		t_unit;
20800Sstevel@tonic-gate 	int		rv;
20810Sstevel@tonic-gate 	int		allow_src_memrange_modify;
20820Sstevel@tonic-gate 	int		allow_targ_memrange_modify;
20830Sstevel@tonic-gate 	drmachid_t	t_id;
20840Sstevel@tonic-gate 	dr_board_t	*s_bp, *t_bp;
20850Sstevel@tonic-gate 	dr_mem_unit_t	*t_mp, *c_mp;
20860Sstevel@tonic-gate 	struct memlist	*d_ml, *t_ml, *x_ml;
20870Sstevel@tonic-gate 	memquery_t	s_mq = {0};
20880Sstevel@tonic-gate 	static fn_t	f = "dr_select_mem_target";
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 	ASSERT(s_ml != NULL);
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 	n_units_per_set = MAX_BOARDS * MAX_MEM_UNITS_PER_BOARD;
20950Sstevel@tonic-gate 	sets = GETSTRUCT(dr_mem_unit_t *, n_units_per_set * n_sets);
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate 	s_bp = hp->h_bd;
20980Sstevel@tonic-gate 	/* calculate the offset into the slice of the last source board pfn */
20990Sstevel@tonic-gate 	ASSERT(s_mp->sbm_npages != 0);
21000Sstevel@tonic-gate 	s_phi = (s_mp->sbm_basepfn + s_mp->sbm_npages - 1) & sm;
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 	allow_src_memrange_modify = drmach_allow_memrange_modify(s_bp->b_id);
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 	/*
21050Sstevel@tonic-gate 	 * Make one pass through all memory units on all boards
21060Sstevel@tonic-gate 	 * and categorize them with respect to the source board.
21070Sstevel@tonic-gate 	 */
21080Sstevel@tonic-gate 	for (t_bd = 0; t_bd < MAX_BOARDS; t_bd++) {
21090Sstevel@tonic-gate 		/*
21100Sstevel@tonic-gate 		 * The board structs are a contiguous array
21110Sstevel@tonic-gate 		 * so we take advantage of that to find the
21120Sstevel@tonic-gate 		 * correct board struct pointer for a given
21130Sstevel@tonic-gate 		 * board number.
21140Sstevel@tonic-gate 		 */
21150Sstevel@tonic-gate 		t_bp = dr_lookup_board(t_bd);
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 		/* source board can not be its own target */
21180Sstevel@tonic-gate 		if (s_bp->b_num == t_bp->b_num)
21190Sstevel@tonic-gate 			continue;
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 		for (t_unit = 0; t_unit < MAX_MEM_UNITS_PER_BOARD; t_unit++) {
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 			t_mp = dr_get_mem_unit(t_bp, t_unit);
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 			/* this memory node must be attached */
21260Sstevel@tonic-gate 			if (!DR_DEV_IS_ATTACHED(&t_mp->sbm_cm))
21270Sstevel@tonic-gate 				continue;
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate 			/* source unit can not be its own target */
21300Sstevel@tonic-gate 			if (s_mp == t_mp) {
21310Sstevel@tonic-gate 				/* catch this is debug kernels */
21320Sstevel@tonic-gate 				ASSERT(0);
21330Sstevel@tonic-gate 				continue;
21340Sstevel@tonic-gate 			}
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 			/*
21370Sstevel@tonic-gate 			 * this memory node must not already be reserved
21380Sstevel@tonic-gate 			 * by some other memory delete operation.
21390Sstevel@tonic-gate 			 */
21400Sstevel@tonic-gate 			if (t_mp->sbm_flags & DR_MFLAG_RESERVED)
21410Sstevel@tonic-gate 				continue;
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 			/*
21440Sstevel@tonic-gate 			 * categorize the memory node
21450Sstevel@tonic-gate 			 * If this is a smaller memory node, create a
21460Sstevel@tonic-gate 			 * temporary, edited copy of the source board's
21470Sstevel@tonic-gate 			 * memlist containing only the span of the non-
21480Sstevel@tonic-gate 			 * relocatable pages.
21490Sstevel@tonic-gate 			 */
21500Sstevel@tonic-gate 			t_phi = (t_mp->sbm_basepfn + t_mp->sbm_npages - 1) & sm;
21510Sstevel@tonic-gate 			t_id = t_mp->sbm_cm.sbdev_bp->b_id;
21520Sstevel@tonic-gate 			allow_targ_memrange_modify =
21530Sstevel@tonic-gate 			    drmach_allow_memrange_modify(t_id);
21540Sstevel@tonic-gate 			if (t_mp->sbm_npages == s_mp->sbm_npages &&
21550Sstevel@tonic-gate 			    t_phi == s_phi) {
21560Sstevel@tonic-gate 				preference = 0;
21570Sstevel@tonic-gate 				t_mp->sbm_slice_offset = 0;
21580Sstevel@tonic-gate 			} else if (t_mp->sbm_npages > s_mp->sbm_npages &&
21590Sstevel@tonic-gate 			    t_phi > s_phi) {
21600Sstevel@tonic-gate 				/*
21610Sstevel@tonic-gate 				 * Selecting this target will require modifying
21620Sstevel@tonic-gate 				 * the source and/or target physical address
21630Sstevel@tonic-gate 				 * ranges.  Skip if not supported by platform.
21640Sstevel@tonic-gate 				 */
21650Sstevel@tonic-gate 				if (!allow_src_memrange_modify ||
21660Sstevel@tonic-gate 				    !allow_targ_memrange_modify) {
21670Sstevel@tonic-gate 					PR_MEM("%s: skip target %s, memory "
21680Sstevel@tonic-gate 					    "range relocation not supported "
21690Sstevel@tonic-gate 					    "by platform\n", f,
21700Sstevel@tonic-gate 					    t_mp->sbm_cm.sbdev_path);
21710Sstevel@tonic-gate 					continue;
21720Sstevel@tonic-gate 				}
21730Sstevel@tonic-gate 				preference = 1;
21740Sstevel@tonic-gate 				t_mp->sbm_slice_offset = 0;
21750Sstevel@tonic-gate 			} else {
21760Sstevel@tonic-gate 				pfn_t		pfn = 0;
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate 				/*
21790Sstevel@tonic-gate 				 * Selecting this target will require modifying
21800Sstevel@tonic-gate 				 * the source and/or target physical address
21810Sstevel@tonic-gate 				 * ranges.  Skip if not supported by platform.
21820Sstevel@tonic-gate 				 */
21830Sstevel@tonic-gate 				if (!allow_src_memrange_modify ||
21840Sstevel@tonic-gate 				    !allow_targ_memrange_modify) {
21850Sstevel@tonic-gate 					PR_MEM("%s: skip target %s, memory "
21860Sstevel@tonic-gate 					    "range relocation not supported "
21870Sstevel@tonic-gate 					    "by platform\n", f,
21880Sstevel@tonic-gate 					    t_mp->sbm_cm.sbdev_path);
21890Sstevel@tonic-gate 					continue;
21900Sstevel@tonic-gate 				}
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate 				/*
21930Sstevel@tonic-gate 				 * Check if its mc can be programmed to relocate
21940Sstevel@tonic-gate 				 * the active address range to match the
21950Sstevel@tonic-gate 				 * nonrelocatable span of the source board.
21960Sstevel@tonic-gate 				 */
21970Sstevel@tonic-gate 				preference = 2;
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate 				if (s_mq.phys_pages == 0) {
22000Sstevel@tonic-gate 					/*
22010Sstevel@tonic-gate 					 * find non-relocatable span on
22020Sstevel@tonic-gate 					 * source board.
22030Sstevel@tonic-gate 					 */
22040Sstevel@tonic-gate 					rv = kphysm_del_span_query(
220511066Srafael.vanoni@sun.com 					    s_mp->sbm_basepfn,
220611066Srafael.vanoni@sun.com 					    s_mp->sbm_npages, &s_mq);
22070Sstevel@tonic-gate 					if (rv != KPHYSM_OK) {
22080Sstevel@tonic-gate 						PR_MEM("%s: %s: unexpected"
220911066Srafael.vanoni@sun.com 						    " kphysm_del_span_query"
221011066Srafael.vanoni@sun.com 						    " return value %d;"
221111066Srafael.vanoni@sun.com 						    " basepfn 0x%lx,"
221211066Srafael.vanoni@sun.com 						    " npages %ld\n",
221311066Srafael.vanoni@sun.com 						    f,
221411066Srafael.vanoni@sun.com 						    s_mp->sbm_cm.sbdev_path,
221511066Srafael.vanoni@sun.com 						    rv,
221611066Srafael.vanoni@sun.com 						    s_mp->sbm_basepfn,
221711066Srafael.vanoni@sun.com 						    s_mp->sbm_npages);
22180Sstevel@tonic-gate 
22190Sstevel@tonic-gate 						/* paranoia */
22200Sstevel@tonic-gate 						s_mq.phys_pages = 0;
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 						continue;
22230Sstevel@tonic-gate 					}
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 					/* more paranoia */
22260Sstevel@tonic-gate 					ASSERT(s_mq.phys_pages != 0);
22270Sstevel@tonic-gate 					ASSERT(s_mq.nonrelocatable != 0);
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate 					/*
22300Sstevel@tonic-gate 					 * this should not happen
22310Sstevel@tonic-gate 					 * if it does, it simply means that
22320Sstevel@tonic-gate 					 * we can not proceed with qualifying
22330Sstevel@tonic-gate 					 * this target candidate.
22340Sstevel@tonic-gate 					 */
22350Sstevel@tonic-gate 					if (s_mq.nonrelocatable == 0)
22360Sstevel@tonic-gate 						continue;
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate 					PR_MEM("%s: %s: nonrelocatable"
223911066Srafael.vanoni@sun.com 					    " span (0x%lx..0x%lx)\n",
224011066Srafael.vanoni@sun.com 					    f,
224111066Srafael.vanoni@sun.com 					    s_mp->sbm_cm.sbdev_path,
224211066Srafael.vanoni@sun.com 					    s_mq.first_nonrelocatable,
224311066Srafael.vanoni@sun.com 					    s_mq.last_nonrelocatable);
22440Sstevel@tonic-gate 				}
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate 				/*
22470Sstevel@tonic-gate 				 * Round down the starting pfn of the
22480Sstevel@tonic-gate 				 * nonrelocatable span on the source board
22490Sstevel@tonic-gate 				 * to nearest programmable boundary possible
22500Sstevel@tonic-gate 				 * with this target candidate.
22510Sstevel@tonic-gate 				 */
22520Sstevel@tonic-gate 				pfn = s_mq.first_nonrelocatable &
225311066Srafael.vanoni@sun.com 				    ~t_mp->sbm_alignment_mask;
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 				/* skip candidate if memory is too small */
22560Sstevel@tonic-gate 				if (pfn + t_mp->sbm_npages <
225711066Srafael.vanoni@sun.com 				    s_mq.last_nonrelocatable)
22580Sstevel@tonic-gate 					continue;
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 				/*
22610Sstevel@tonic-gate 				 * reprogramming an mc to relocate its
22620Sstevel@tonic-gate 				 * active address range means the beginning
22630Sstevel@tonic-gate 				 * address to which the DIMMS respond will
22640Sstevel@tonic-gate 				 * be somewhere above the slice boundary
22650Sstevel@tonic-gate 				 * address.  The larger the size of memory
22660Sstevel@tonic-gate 				 * on this unit, the more likely part of it
22670Sstevel@tonic-gate 				 * will exist beyond the end of the slice.
22680Sstevel@tonic-gate 				 * The portion of the memory that does is
22690Sstevel@tonic-gate 				 * unavailable to the system until the mc
22700Sstevel@tonic-gate 				 * reprogrammed to a more favorable base
22710Sstevel@tonic-gate 				 * address.
22720Sstevel@tonic-gate 				 * An attempt is made to avoid the loss by
22730Sstevel@tonic-gate 				 * recalculating the mc base address relative
22740Sstevel@tonic-gate 				 * to the end of the slice.  This may produce
22750Sstevel@tonic-gate 				 * a more favorable result.  If not, we lower
22760Sstevel@tonic-gate 				 * the board's preference rating so that it
22770Sstevel@tonic-gate 				 * is one the last candidate boards to be
22780Sstevel@tonic-gate 				 * considered.
22790Sstevel@tonic-gate 				 */
22800Sstevel@tonic-gate 				if ((pfn + t_mp->sbm_npages) & ~sm) {
22810Sstevel@tonic-gate 					pfn_t p;
22820Sstevel@tonic-gate 
22830Sstevel@tonic-gate 					ASSERT(sz >= t_mp->sbm_npages);
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 					/*
22860Sstevel@tonic-gate 					 * calculate an alternative starting
22870Sstevel@tonic-gate 					 * address relative to the end of the
22880Sstevel@tonic-gate 					 * slice's address space.
22890Sstevel@tonic-gate 					 */
22900Sstevel@tonic-gate 					p = pfn & ~sm;
22910Sstevel@tonic-gate 					p = p + (sz - t_mp->sbm_npages);
22920Sstevel@tonic-gate 					p = p & ~t_mp->sbm_alignment_mask;
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 					if ((p > s_mq.first_nonrelocatable) ||
229511066Srafael.vanoni@sun.com 					    (p + t_mp->sbm_npages <
229611066Srafael.vanoni@sun.com 					    s_mq.last_nonrelocatable)) {
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 						/*
22990Sstevel@tonic-gate 						 * alternative starting addr
23000Sstevel@tonic-gate 						 * won't work. Lower preference
23010Sstevel@tonic-gate 						 * rating of this board, since
23020Sstevel@tonic-gate 						 * some number of pages will
23030Sstevel@tonic-gate 						 * unavailable for use.
23040Sstevel@tonic-gate 						 */
23050Sstevel@tonic-gate 						preference = 3;
23060Sstevel@tonic-gate 					} else {
23070Sstevel@tonic-gate 						dr_smt_realigned++;
23080Sstevel@tonic-gate 						pfn = p;
23090Sstevel@tonic-gate 					}
23100Sstevel@tonic-gate 				}
23110Sstevel@tonic-gate 
23120Sstevel@tonic-gate 				/*
23130Sstevel@tonic-gate 				 * translate calculated pfn to an offset
23140Sstevel@tonic-gate 				 * relative to the slice boundary.  If the
23150Sstevel@tonic-gate 				 * candidate board is selected, this offset
23160Sstevel@tonic-gate 				 * will be used to calculate the values
23170Sstevel@tonic-gate 				 * programmed into the mc.
23180Sstevel@tonic-gate 				 */
23190Sstevel@tonic-gate 				t_mp->sbm_slice_offset = pfn & sm;
23200Sstevel@tonic-gate 				PR_MEM("%s: %s:"
232111066Srafael.vanoni@sun.com 				    "  proposed mc offset 0x%lx\n",
232211066Srafael.vanoni@sun.com 				    f,
232311066Srafael.vanoni@sun.com 				    t_mp->sbm_cm.sbdev_path,
232411066Srafael.vanoni@sun.com 				    t_mp->sbm_slice_offset);
23250Sstevel@tonic-gate 			}
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 			dr_smt_preference[preference]++;
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 			/* calculate index to start of preference set */
23300Sstevel@tonic-gate 			idx  = n_units_per_set * preference;
23310Sstevel@tonic-gate 			/* calculate offset to respective element */
23320Sstevel@tonic-gate 			idx += t_bd * MAX_MEM_UNITS_PER_BOARD + t_unit;
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 			ASSERT(idx < n_units_per_set * n_sets);
23350Sstevel@tonic-gate 			sets[idx] = t_mp;
23360Sstevel@tonic-gate 		}
23370Sstevel@tonic-gate 	}
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate 	/*
23400Sstevel@tonic-gate 	 * NOTE: this would be a good place to sort each candidate
23410Sstevel@tonic-gate 	 * set in to some desired order, e.g. memory size in ascending
23420Sstevel@tonic-gate 	 * order.  Without an additional sorting step here, the order
23430Sstevel@tonic-gate 	 * within a set is ascending board number order.
23440Sstevel@tonic-gate 	 */
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 	c_mp = NULL;
23470Sstevel@tonic-gate 	x_ml = NULL;
23480Sstevel@tonic-gate 	t_ml = NULL;
23490Sstevel@tonic-gate 	for (idx = 0; idx < n_units_per_set * n_sets; idx++) {
23500Sstevel@tonic-gate 		memquery_t mq;
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate 		/* cleanup t_ml after previous pass */
23530Sstevel@tonic-gate 		if (t_ml != NULL) {
23540Sstevel@tonic-gate 			memlist_delete(t_ml);
23550Sstevel@tonic-gate 			t_ml = NULL;
23560Sstevel@tonic-gate 		}
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate 		/* get candidate target board mem unit */
23590Sstevel@tonic-gate 		t_mp = sets[idx];
23600Sstevel@tonic-gate 		if (t_mp == NULL)
23610Sstevel@tonic-gate 			continue;
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate 		/* get target board memlist */
23640Sstevel@tonic-gate 		t_ml = dr_get_memlist(t_mp);
23650Sstevel@tonic-gate 		if (t_ml == NULL) {
23660Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s: no memlist for"
236711066Srafael.vanoni@sun.com 			    " mem-unit %d, board %d",
236811066Srafael.vanoni@sun.com 			    f,
236911066Srafael.vanoni@sun.com 			    t_mp->sbm_cm.sbdev_bp->b_num,
237011066Srafael.vanoni@sun.com 			    t_mp->sbm_cm.sbdev_unum);
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate 			continue;
23730Sstevel@tonic-gate 		}
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate 		/* get appropriate source board memlist */
23760Sstevel@tonic-gate 		t_phi = (t_mp->sbm_basepfn + t_mp->sbm_npages - 1) & sm;
23770Sstevel@tonic-gate 		if (t_mp->sbm_npages < s_mp->sbm_npages || t_phi < s_phi) {
23780Sstevel@tonic-gate 			spgcnt_t excess;
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 			/*
23810Sstevel@tonic-gate 			 * make a copy of the source board memlist
23820Sstevel@tonic-gate 			 * then edit it to remove the spans that
23830Sstevel@tonic-gate 			 * are outside the calculated span of
23840Sstevel@tonic-gate 			 * [pfn..s_mq.last_nonrelocatable].
23850Sstevel@tonic-gate 			 */
23860Sstevel@tonic-gate 			if (x_ml != NULL)
23870Sstevel@tonic-gate 				memlist_delete(x_ml);
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate 			x_ml = memlist_dup(s_ml);
23900Sstevel@tonic-gate 			if (x_ml == NULL) {
23910Sstevel@tonic-gate 				PR_MEM("%s: memlist_dup failed\n", f);
23920Sstevel@tonic-gate 				/* TODO: should abort */
23930Sstevel@tonic-gate 				continue;
23940Sstevel@tonic-gate 			}
23950Sstevel@tonic-gate 
23960Sstevel@tonic-gate 			/* trim off lower portion */
23970Sstevel@tonic-gate 			excess = t_mp->sbm_slice_offset -
23980Sstevel@tonic-gate 			    (s_mp->sbm_basepfn & sm);
23990Sstevel@tonic-gate 
24000Sstevel@tonic-gate 			if (excess > 0) {
24010Sstevel@tonic-gate 				x_ml = memlist_del_span(
240211066Srafael.vanoni@sun.com 				    x_ml,
240311066Srafael.vanoni@sun.com 				    _ptob64(s_mp->sbm_basepfn),
240411066Srafael.vanoni@sun.com 				    _ptob64(excess));
24050Sstevel@tonic-gate 			}
24060Sstevel@tonic-gate 			ASSERT(x_ml);
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 			/*
24090Sstevel@tonic-gate 			 * Since this candidate target board is smaller
24100Sstevel@tonic-gate 			 * than the source board, s_mq must have been
24110Sstevel@tonic-gate 			 * initialized in previous loop while processing
24120Sstevel@tonic-gate 			 * this or some other candidate board.
24130Sstevel@tonic-gate 			 * FIXME: this is weak.
24140Sstevel@tonic-gate 			 */
24150Sstevel@tonic-gate 			ASSERT(s_mq.phys_pages != 0);
24160Sstevel@tonic-gate 
24170Sstevel@tonic-gate 			/* trim off upper portion */
24180Sstevel@tonic-gate 			excess = (s_mp->sbm_basepfn + s_mp->sbm_npages)
241911066Srafael.vanoni@sun.com 			    - (s_mq.last_nonrelocatable + 1);
24200Sstevel@tonic-gate 			if (excess > 0) {
24210Sstevel@tonic-gate 				pfn_t p;
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 				p  = s_mq.last_nonrelocatable + 1;
24240Sstevel@tonic-gate 				x_ml = memlist_del_span(
242511066Srafael.vanoni@sun.com 				    x_ml,
242611066Srafael.vanoni@sun.com 				    _ptob64(p),
242711066Srafael.vanoni@sun.com 				    _ptob64(excess));
24280Sstevel@tonic-gate 			}
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate 			PR_MEM("%s: %s: edited source memlist:\n",
243111066Srafael.vanoni@sun.com 			    f, s_mp->sbm_cm.sbdev_path);
24320Sstevel@tonic-gate 			PR_MEMLIST_DUMP(x_ml);
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate #ifdef DEBUG
24350Sstevel@tonic-gate 			/* sanity check memlist */
24360Sstevel@tonic-gate 			d_ml = x_ml;
24370Sstevel@tonic-gate 			while (d_ml->next != NULL)
24380Sstevel@tonic-gate 				d_ml = d_ml->next;
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate 			ASSERT(d_ml->address + d_ml->size ==
244111066Srafael.vanoni@sun.com 			    _ptob64(s_mq.last_nonrelocatable + 1));
24420Sstevel@tonic-gate #endif
24430Sstevel@tonic-gate 
24440Sstevel@tonic-gate 			/*
24450Sstevel@tonic-gate 			 * x_ml now describes only the portion of the
24460Sstevel@tonic-gate 			 * source board that will be moved during the
24470Sstevel@tonic-gate 			 * copy/rename operation.
24480Sstevel@tonic-gate 			 */
24490Sstevel@tonic-gate 			d_ml = x_ml;
24500Sstevel@tonic-gate 		} else {
24510Sstevel@tonic-gate 			/* use original memlist; all spans will be moved */
24520Sstevel@tonic-gate 			d_ml = s_ml;
24530Sstevel@tonic-gate 		}
24540Sstevel@tonic-gate 
24550Sstevel@tonic-gate 		/* verify target can support source memory spans. */
24560Sstevel@tonic-gate 		if (memlist_canfit(d_ml, t_ml) == 0) {
24570Sstevel@tonic-gate 			PR_MEM("%s: source memlist won't"
245811066Srafael.vanoni@sun.com 			    " fit in target memlist\n", f);
24590Sstevel@tonic-gate 			PR_MEM("%s: source memlist:\n", f);
24600Sstevel@tonic-gate 			PR_MEMLIST_DUMP(d_ml);
24610Sstevel@tonic-gate 			PR_MEM("%s: target memlist:\n", f);
24620Sstevel@tonic-gate 			PR_MEMLIST_DUMP(t_ml);
24630Sstevel@tonic-gate 
24640Sstevel@tonic-gate 			continue;
24650Sstevel@tonic-gate 		}
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 		/* NOTE: the value of d_ml is not used beyond this point */
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 		PR_MEM("%s: checking for no-reloc in %s, "
247011066Srafael.vanoni@sun.com 		    " basepfn=0x%lx, npages=%ld\n",
247111066Srafael.vanoni@sun.com 		    f,
247211066Srafael.vanoni@sun.com 		    t_mp->sbm_cm.sbdev_path,
247311066Srafael.vanoni@sun.com 		    t_mp->sbm_basepfn,
247411066Srafael.vanoni@sun.com 		    t_mp->sbm_npages);
24750Sstevel@tonic-gate 
24760Sstevel@tonic-gate 		rv = kphysm_del_span_query(
247711066Srafael.vanoni@sun.com 		    t_mp->sbm_basepfn, t_mp->sbm_npages, &mq);
24780Sstevel@tonic-gate 		if (rv != KPHYSM_OK) {
24790Sstevel@tonic-gate 			PR_MEM("%s: kphysm_del_span_query:"
248011066Srafael.vanoni@sun.com 			    " unexpected return value %d\n", f, rv);
24810Sstevel@tonic-gate 
24820Sstevel@tonic-gate 			continue;
24830Sstevel@tonic-gate 		}
24840Sstevel@tonic-gate 
24850Sstevel@tonic-gate 		if (mq.nonrelocatable != 0) {
24860Sstevel@tonic-gate 			PR_MEM("%s: candidate %s has"
248711066Srafael.vanoni@sun.com 			    " nonrelocatable span [0x%lx..0x%lx]\n",
248811066Srafael.vanoni@sun.com 			    f,
248911066Srafael.vanoni@sun.com 			    t_mp->sbm_cm.sbdev_path,
249011066Srafael.vanoni@sun.com 			    mq.first_nonrelocatable,
249111066Srafael.vanoni@sun.com 			    mq.last_nonrelocatable);
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate 			continue;
24940Sstevel@tonic-gate 		}
24950Sstevel@tonic-gate 
24960Sstevel@tonic-gate #ifdef DEBUG
24970Sstevel@tonic-gate 		/*
24980Sstevel@tonic-gate 		 * This is a debug tool for excluding certain boards
24990Sstevel@tonic-gate 		 * from being selected as a target board candidate.
25000Sstevel@tonic-gate 		 * dr_ignore_board is only tested by this driver.
25010Sstevel@tonic-gate 		 * It must be set with adb, obp, /etc/system or your
25020Sstevel@tonic-gate 		 * favorite debugger.
25030Sstevel@tonic-gate 		 */
25040Sstevel@tonic-gate 		if (dr_ignore_board &
250511066Srafael.vanoni@sun.com 		    (1 << (t_mp->sbm_cm.sbdev_bp->b_num - 1))) {
25060Sstevel@tonic-gate 			PR_MEM("%s: dr_ignore_board flag set,"
250711066Srafael.vanoni@sun.com 			    " ignoring %s as candidate\n",
250811066Srafael.vanoni@sun.com 			    f, t_mp->sbm_cm.sbdev_path);
25090Sstevel@tonic-gate 			continue;
25100Sstevel@tonic-gate 		}
25110Sstevel@tonic-gate #endif
25120Sstevel@tonic-gate 
25130Sstevel@tonic-gate 		/*
25140Sstevel@tonic-gate 		 * Reserve excess source board memory, if any.
25150Sstevel@tonic-gate 		 *
25160Sstevel@tonic-gate 		 * When the number of pages on the candidate target
25170Sstevel@tonic-gate 		 * board is less than the number of pages on the source,
25180Sstevel@tonic-gate 		 * then some spans (clearly) of the source board's address
25190Sstevel@tonic-gate 		 * space will not be covered by physical memory after the
25200Sstevel@tonic-gate 		 * copy/rename completes.  The following code block
25210Sstevel@tonic-gate 		 * schedules those spans to be deleted.
25220Sstevel@tonic-gate 		 */
25230Sstevel@tonic-gate 		if (t_mp->sbm_npages < s_mp->sbm_npages || t_phi < s_phi) {
25240Sstevel@tonic-gate 			pfn_t pfn;
25250Sstevel@tonic-gate 			uint64_t s_del_pa;
25260Sstevel@tonic-gate 			struct memlist *ml;
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 			d_ml = memlist_dup(s_ml);
25290Sstevel@tonic-gate 			if (d_ml == NULL) {
25300Sstevel@tonic-gate 				PR_MEM("%s: cant dup src brd memlist\n", f);
25310Sstevel@tonic-gate 				/* TODO: should abort */
25320Sstevel@tonic-gate 				continue;
25330Sstevel@tonic-gate 			}
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 			/* calculate base pfn relative to target board */
25360Sstevel@tonic-gate 			pfn  = s_mp->sbm_basepfn & ~sm;
25370Sstevel@tonic-gate 			pfn += t_mp->sbm_slice_offset;
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 			/*
25400Sstevel@tonic-gate 			 * cannot split dynamically added segment
25410Sstevel@tonic-gate 			 */
25420Sstevel@tonic-gate 			s_del_pa = _ptob64(pfn + t_mp->sbm_npages);
25430Sstevel@tonic-gate 			PR_MEM("%s: proposed src delete pa=0x%lx\n", f,
25440Sstevel@tonic-gate 			    s_del_pa);
25450Sstevel@tonic-gate 			PR_MEM("%s: checking for split of dyn seg list:\n", f);
25460Sstevel@tonic-gate 			PR_MEMLIST_DUMP(s_mp->sbm_dyn_segs);
25470Sstevel@tonic-gate 			for (ml = s_mp->sbm_dyn_segs; ml; ml = ml->next) {
25480Sstevel@tonic-gate 				if (s_del_pa > ml->address &&
25490Sstevel@tonic-gate 				    s_del_pa < ml->address + ml->size) {
25500Sstevel@tonic-gate 					s_del_pa = ml->address;
25510Sstevel@tonic-gate 					break;
25520Sstevel@tonic-gate 				}
25530Sstevel@tonic-gate 			}
25540Sstevel@tonic-gate 
25550Sstevel@tonic-gate 			/* remove span that will reside on candidate board */
25560Sstevel@tonic-gate 			d_ml = memlist_del_span(d_ml, _ptob64(pfn),
25570Sstevel@tonic-gate 			    s_del_pa - _ptob64(pfn));
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate 			PR_MEM("%s: %s: reserving src brd memlist:\n",
256011066Srafael.vanoni@sun.com 			    f, s_mp->sbm_cm.sbdev_path);
25610Sstevel@tonic-gate 			PR_MEMLIST_DUMP(d_ml);
25620Sstevel@tonic-gate 
25630Sstevel@tonic-gate 			/* reserve excess spans */
256411066Srafael.vanoni@sun.com 			if (dr_reserve_mem_spans(&s_mp->sbm_memhandle, d_ml)
256511066Srafael.vanoni@sun.com 			    != 0) {
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate 				/* likely more non-reloc pages appeared */
25680Sstevel@tonic-gate 				/* TODO: restart from top? */
25690Sstevel@tonic-gate 				continue;
25700Sstevel@tonic-gate 			}
25710Sstevel@tonic-gate 		} else {
25720Sstevel@tonic-gate 			/* no excess source board memory */
25730Sstevel@tonic-gate 			d_ml = NULL;
25740Sstevel@tonic-gate 		}
25750Sstevel@tonic-gate 
25760Sstevel@tonic-gate 		s_mp->sbm_flags |= DR_MFLAG_RESERVED;
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate 		/*
25790Sstevel@tonic-gate 		 * reserve all memory on target board.
25800Sstevel@tonic-gate 		 * NOTE: source board's memhandle is used.
25810Sstevel@tonic-gate 		 *
25820Sstevel@tonic-gate 		 * If this succeeds (eq 0), then target selection is
25830Sstevel@tonic-gate 		 * complete and all unwanted memory spans, both source and
25840Sstevel@tonic-gate 		 * target, have been reserved.  Loop is terminated.
25850Sstevel@tonic-gate 		 */
25860Sstevel@tonic-gate 		if (dr_reserve_mem_spans(&s_mp->sbm_memhandle, t_ml) == 0) {
25870Sstevel@tonic-gate 			PR_MEM("%s: %s: target board memory reserved\n",
258811066Srafael.vanoni@sun.com 			    f, t_mp->sbm_cm.sbdev_path);
25890Sstevel@tonic-gate 
25900Sstevel@tonic-gate 			/* a candidate target board is now reserved */
25910Sstevel@tonic-gate 			t_mp->sbm_flags |= DR_MFLAG_RESERVED;
25920Sstevel@tonic-gate 			c_mp = t_mp;
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate 			/* *** EXITING LOOP *** */
25950Sstevel@tonic-gate 			break;
25960Sstevel@tonic-gate 		}
25970Sstevel@tonic-gate 
25980Sstevel@tonic-gate 		/* did not successfully reserve the target board. */
25990Sstevel@tonic-gate 		PR_MEM("%s: could not reserve target %s\n",
260011066Srafael.vanoni@sun.com 		    f, t_mp->sbm_cm.sbdev_path);
26010Sstevel@tonic-gate 
26020Sstevel@tonic-gate 		/*
26030Sstevel@tonic-gate 		 * NOTE: an undo of the dr_reserve_mem_span work
26040Sstevel@tonic-gate 		 * will happen automatically when the memhandle
26050Sstevel@tonic-gate 		 * (s_mp->sbm_memhandle) is kphysm_del_release'd.
26060Sstevel@tonic-gate 		 */
26070Sstevel@tonic-gate 
26080Sstevel@tonic-gate 		s_mp->sbm_flags &= ~DR_MFLAG_RESERVED;
26090Sstevel@tonic-gate 	}
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate 	/* clean up after memlist editing logic */
26120Sstevel@tonic-gate 	if (x_ml != NULL)
26130Sstevel@tonic-gate 		memlist_delete(x_ml);
26140Sstevel@tonic-gate 
26150Sstevel@tonic-gate 	FREESTRUCT(sets, dr_mem_unit_t *, n_units_per_set * n_sets);
26160Sstevel@tonic-gate 
26170Sstevel@tonic-gate 	/*
26180Sstevel@tonic-gate 	 * c_mp will be NULL when the entire sets[] array
26190Sstevel@tonic-gate 	 * has been searched without reserving a target board.
26200Sstevel@tonic-gate 	 */
26210Sstevel@tonic-gate 	if (c_mp == NULL) {
26220Sstevel@tonic-gate 		PR_MEM("%s: %s: target selection failed.\n",
262311066Srafael.vanoni@sun.com 		    f, s_mp->sbm_cm.sbdev_path);
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate 		if (t_ml != NULL)
26260Sstevel@tonic-gate 			memlist_delete(t_ml);
26270Sstevel@tonic-gate 
26280Sstevel@tonic-gate 		return (-1);
26290Sstevel@tonic-gate 	}
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate 	PR_MEM("%s: found target %s for source %s\n",
263211066Srafael.vanoni@sun.com 	    f,
263311066Srafael.vanoni@sun.com 	    c_mp->sbm_cm.sbdev_path,
263411066Srafael.vanoni@sun.com 	    s_mp->sbm_cm.sbdev_path);
26350Sstevel@tonic-gate 
26360Sstevel@tonic-gate 	s_mp->sbm_peer = c_mp;
26370Sstevel@tonic-gate 	s_mp->sbm_flags |= DR_MFLAG_SOURCE;
26380Sstevel@tonic-gate 	s_mp->sbm_del_mlist = d_ml;	/* spans to be deleted, if any */
26390Sstevel@tonic-gate 	s_mp->sbm_mlist = s_ml;
26400Sstevel@tonic-gate 	s_mp->sbm_cm.sbdev_busy = 1;
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	c_mp->sbm_peer = s_mp;
26430Sstevel@tonic-gate 	c_mp->sbm_flags |= DR_MFLAG_TARGET;
26440Sstevel@tonic-gate 	c_mp->sbm_del_mlist = t_ml;	/* spans to be deleted */
26450Sstevel@tonic-gate 	c_mp->sbm_mlist = t_ml;
26460Sstevel@tonic-gate 	c_mp->sbm_cm.sbdev_busy = 1;
26470Sstevel@tonic-gate 
26480Sstevel@tonic-gate 	s_mp->sbm_flags &= ~DR_MFLAG_MEMRESIZE;
26490Sstevel@tonic-gate 	if (c_mp->sbm_npages > s_mp->sbm_npages) {
26500Sstevel@tonic-gate 		s_mp->sbm_flags |= DR_MFLAG_MEMUPSIZE;
2651930Smathue 		PR_MEM("%s: upsize detected (source=%ld < target=%ld)\n",
265211066Srafael.vanoni@sun.com 		    f, s_mp->sbm_npages, c_mp->sbm_npages);
26530Sstevel@tonic-gate 	} else if (c_mp->sbm_npages < s_mp->sbm_npages) {
26540Sstevel@tonic-gate 		s_mp->sbm_flags |= DR_MFLAG_MEMDOWNSIZE;
2655930Smathue 		PR_MEM("%s: downsize detected (source=%ld > target=%ld)\n",
265611066Srafael.vanoni@sun.com 		    f, s_mp->sbm_npages, c_mp->sbm_npages);
26570Sstevel@tonic-gate 	}
26580Sstevel@tonic-gate 
26590Sstevel@tonic-gate 	return (0);
26600Sstevel@tonic-gate }
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate /*
26630Sstevel@tonic-gate  * Memlist support.
26640Sstevel@tonic-gate  */
26650Sstevel@tonic-gate 
26660Sstevel@tonic-gate /*
26670Sstevel@tonic-gate  * Determine whether the source memlist (s_mlist) will
26680Sstevel@tonic-gate  * fit into the target memlist (t_mlist) in terms of
26690Sstevel@tonic-gate  * size and holes (i.e. based on same relative base address).
26700Sstevel@tonic-gate  */
26710Sstevel@tonic-gate static int
26720Sstevel@tonic-gate memlist_canfit(struct memlist *s_mlist, struct memlist *t_mlist)
26730Sstevel@tonic-gate {
26740Sstevel@tonic-gate 	int		rv = 0;
26750Sstevel@tonic-gate 	uint64_t	s_basepa, t_basepa;
26760Sstevel@tonic-gate 	struct memlist	*s_ml, *t_ml;
26770Sstevel@tonic-gate 
26780Sstevel@tonic-gate 	if ((s_mlist == NULL) || (t_mlist == NULL))
26790Sstevel@tonic-gate 		return (0);
26800Sstevel@tonic-gate 
26810Sstevel@tonic-gate 	/*
26820Sstevel@tonic-gate 	 * Base both memlists on common base address (0).
26830Sstevel@tonic-gate 	 */
26840Sstevel@tonic-gate 	s_basepa = s_mlist->address;
26850Sstevel@tonic-gate 	t_basepa = t_mlist->address;
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 	for (s_ml = s_mlist; s_ml; s_ml = s_ml->next)
26880Sstevel@tonic-gate 		s_ml->address -= s_basepa;
26890Sstevel@tonic-gate 
26900Sstevel@tonic-gate 	for (t_ml = t_mlist; t_ml; t_ml = t_ml->next)
26910Sstevel@tonic-gate 		t_ml->address -= t_basepa;
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate 	s_ml = s_mlist;
26940Sstevel@tonic-gate 	for (t_ml = t_mlist; t_ml && s_ml; t_ml = t_ml->next) {
26950Sstevel@tonic-gate 		uint64_t	s_start, s_end;
26960Sstevel@tonic-gate 		uint64_t	t_start, t_end;
26970Sstevel@tonic-gate 
26980Sstevel@tonic-gate 		t_start = t_ml->address;
26990Sstevel@tonic-gate 		t_end = t_start + t_ml->size;
27000Sstevel@tonic-gate 
27010Sstevel@tonic-gate 		for (; s_ml; s_ml = s_ml->next) {
27020Sstevel@tonic-gate 			s_start = s_ml->address;
27030Sstevel@tonic-gate 			s_end = s_start + s_ml->size;
27040Sstevel@tonic-gate 
27050Sstevel@tonic-gate 			if ((s_start < t_start) || (s_end > t_end))
27060Sstevel@tonic-gate 				break;
27070Sstevel@tonic-gate 		}
27080Sstevel@tonic-gate 	}
27090Sstevel@tonic-gate 	/*
27100Sstevel@tonic-gate 	 * If we ran out of source memlist chunks that mean
27110Sstevel@tonic-gate 	 * we found a home for all of them.
27120Sstevel@tonic-gate 	 */
27130Sstevel@tonic-gate 	if (s_ml == NULL)
27140Sstevel@tonic-gate 		rv = 1;
27150Sstevel@tonic-gate 
27160Sstevel@tonic-gate 	/*
27170Sstevel@tonic-gate 	 * Need to add base addresses back since memlists
27180Sstevel@tonic-gate 	 * are probably in use by caller.
27190Sstevel@tonic-gate 	 */
27200Sstevel@tonic-gate 	for (s_ml = s_mlist; s_ml; s_ml = s_ml->next)
27210Sstevel@tonic-gate 		s_ml->address += s_basepa;
27220Sstevel@tonic-gate 
27230Sstevel@tonic-gate 	for (t_ml = t_mlist; t_ml; t_ml = t_ml->next)
27240Sstevel@tonic-gate 		t_ml->address += t_basepa;
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate 	return (rv);
27270Sstevel@tonic-gate }
2728