xref: /illumos-gate/usr/src/uts/sun4u/ngdr/io/dr_mem.c (revision 2a1fd0ff)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
525cf1a30Sjl  * Common Development and Distribution License (the "License").
625cf1a30Sjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2107d06da5SSurya Prakki 
227c478bd9Sstevel@tonic-gate /*
2356f33205SJonathan Adams  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*2a1fd0ffSPeter Tribble /*
28*2a1fd0ffSPeter Tribble  * Copyright 2019 Peter Tribble.
29*2a1fd0ffSPeter Tribble  */
30*2a1fd0ffSPeter Tribble 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * DR memory support routines.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/note.h>
367c478bd9Sstevel@tonic-gate #include <sys/debug.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/errno.h>
397c478bd9Sstevel@tonic-gate #include <sys/param.h>
407c478bd9Sstevel@tonic-gate #include <sys/dditypes.h>
417c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
427c478bd9Sstevel@tonic-gate #include <sys/conf.h>
437c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
447c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
457c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
467c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
477c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
487c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
497c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
507c478bd9Sstevel@tonic-gate #include <sys/spitregs.h>
517c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
527c478bd9Sstevel@tonic-gate #include <sys/promif.h>
537c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
547c478bd9Sstevel@tonic-gate #include <sys/lgrp.h>
557c478bd9Sstevel@tonic-gate #include <sys/platform_module.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <vm/page.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <sys/dr.h>
607c478bd9Sstevel@tonic-gate #include <sys/dr_util.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate extern struct memlist	*phys_install;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* TODO: push this reference below drmach line */
657c478bd9Sstevel@tonic-gate extern int		kcage_on;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /* for the DR*INTERNAL_ERROR macros.  see sys/dr.h. */
68f500b196SRichard Bean static char *dr_ie_fmt = "dr_mem.c %d";
697c478bd9Sstevel@tonic-gate 
70d3d50737SRafael Vanoni static int	dr_post_detach_mem_unit(dr_mem_unit_t *mp);
71d3d50737SRafael Vanoni static int	dr_reserve_mem_spans(memhandle_t *mhp, struct memlist *mlist);
72d3d50737SRafael Vanoni static int	dr_select_mem_target(dr_handle_t *hp, dr_mem_unit_t *mp,
73d3d50737SRafael Vanoni     struct memlist *ml);
74d3d50737SRafael Vanoni static void	dr_init_mem_unit_data(dr_mem_unit_t *mp);
757c478bd9Sstevel@tonic-gate 
76d3d50737SRafael Vanoni static int 	memlist_canfit(struct memlist *s_mlist,
77d3d50737SRafael Vanoni     struct memlist *t_mlist);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * dr_mem_unit_t.sbm_flags
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate #define	DR_MFLAG_RESERVED	0x01	/* mem unit reserved for delete */
837c478bd9Sstevel@tonic-gate #define	DR_MFLAG_SOURCE		0x02	/* source brd of copy/rename op */
847c478bd9Sstevel@tonic-gate #define	DR_MFLAG_TARGET		0x04	/* target brd of copy/rename op */
857c478bd9Sstevel@tonic-gate #define	DR_MFLAG_MEMUPSIZE	0x08	/* move from big to small board */
867c478bd9Sstevel@tonic-gate #define	DR_MFLAG_MEMDOWNSIZE	0x10	/* move from small to big board */
877c478bd9Sstevel@tonic-gate #define	DR_MFLAG_MEMRESIZE	0x18	/* move to different size board */
887c478bd9Sstevel@tonic-gate #define	DR_MFLAG_RELOWNER	0x20	/* memory release (delete) owner */
897c478bd9Sstevel@tonic-gate #define	DR_MFLAG_RELDONE	0x40	/* memory release (delete) done */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /* helper macros */
927c478bd9Sstevel@tonic-gate #define	_ptob64(p) ((uint64_t)(p) << PAGESHIFT)
937c478bd9Sstevel@tonic-gate #define	_b64top(b) ((pgcnt_t)((b) >> PAGESHIFT))
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static struct memlist *
dr_get_memlist(dr_mem_unit_t * mp)967c478bd9Sstevel@tonic-gate dr_get_memlist(dr_mem_unit_t *mp)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	struct memlist	*mlist = NULL;
997c478bd9Sstevel@tonic-gate 	sbd_error_t	*err;
1007c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_get_memlist";
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	PR_MEM("%s for %s...\n", f, mp->sbm_cm.sbdev_path);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	/*
1057c478bd9Sstevel@tonic-gate 	 * Return cached memlist, if present.
1067c478bd9Sstevel@tonic-gate 	 * This memlist will be present following an
1077c478bd9Sstevel@tonic-gate 	 * unconfigure (a.k.a: detach) of this memunit.
1087c478bd9Sstevel@tonic-gate 	 * It should only be used in the case were a configure
1097c478bd9Sstevel@tonic-gate 	 * is bringing this memunit back in without going
1107c478bd9Sstevel@tonic-gate 	 * through the disconnect and connect states.
1117c478bd9Sstevel@tonic-gate 	 */
1127c478bd9Sstevel@tonic-gate 	if (mp->sbm_mlist) {
1137c478bd9Sstevel@tonic-gate 		PR_MEM("%s: found cached memlist\n", f);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 		mlist = memlist_dup(mp->sbm_mlist);
1167c478bd9Sstevel@tonic-gate 	} else {
1177c478bd9Sstevel@tonic-gate 		uint64_t basepa = _ptob64(mp->sbm_basepfn);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 		/* attempt to construct a memlist using phys_install */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		/* round down to slice base address */
1227c478bd9Sstevel@tonic-gate 		basepa &= ~(mp->sbm_slice_size - 1);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 		/* get a copy of phys_install to edit */
1257c478bd9Sstevel@tonic-gate 		memlist_read_lock();
1267c478bd9Sstevel@tonic-gate 		mlist = memlist_dup(phys_install);
1277c478bd9Sstevel@tonic-gate 		memlist_read_unlock();
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 		/* trim lower irrelevant span */
1307c478bd9Sstevel@tonic-gate 		if (mlist)
1317c478bd9Sstevel@tonic-gate 			mlist = memlist_del_span(mlist, 0ull, basepa);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		/* trim upper irrelevant span */
1347c478bd9Sstevel@tonic-gate 		if (mlist) {
1357c478bd9Sstevel@tonic-gate 			uint64_t endpa;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 			basepa += mp->sbm_slice_size;
1387c478bd9Sstevel@tonic-gate 			endpa = _ptob64(physmax + 1);
1397c478bd9Sstevel@tonic-gate 			if (endpa > basepa)
1407c478bd9Sstevel@tonic-gate 				mlist = memlist_del_span(
141d3d50737SRafael Vanoni 				    mlist,
142d3d50737SRafael Vanoni 				    basepa,
143d3d50737SRafael Vanoni 				    endpa - basepa);
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 		if (mlist) {
1477c478bd9Sstevel@tonic-gate 			/* successfully built a memlist */
1487c478bd9Sstevel@tonic-gate 			PR_MEM("%s: derived memlist from phys_install\n", f);
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 		/* if no mlist yet, try platform layer */
1527c478bd9Sstevel@tonic-gate 		if (!mlist) {
1537c478bd9Sstevel@tonic-gate 			err = drmach_mem_get_memlist(
154d3d50737SRafael Vanoni 			    mp->sbm_cm.sbdev_id, &mlist);
1557c478bd9Sstevel@tonic-gate 			if (err) {
1567c478bd9Sstevel@tonic-gate 				DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
1577c478bd9Sstevel@tonic-gate 				mlist = NULL; /* paranoia */
1587c478bd9Sstevel@tonic-gate 			}
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	PR_MEM("%s: memlist for %s\n", f, mp->sbm_cm.sbdev_path);
1637c478bd9Sstevel@tonic-gate 	PR_MEMLIST_DUMP(mlist);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	return (mlist);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate typedef struct {
1697c478bd9Sstevel@tonic-gate 	kcondvar_t cond;
1707c478bd9Sstevel@tonic-gate 	kmutex_t lock;
1717c478bd9Sstevel@tonic-gate 	int error;
1727c478bd9Sstevel@tonic-gate 	int done;
1737c478bd9Sstevel@tonic-gate } dr_release_mem_sync_t;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * Memory has been logically removed by the time this routine is called.
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate static void
dr_mem_del_done(void * arg,int error)1797c478bd9Sstevel@tonic-gate dr_mem_del_done(void *arg, int error)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	dr_release_mem_sync_t *ds = arg;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	mutex_enter(&ds->lock);
1847c478bd9Sstevel@tonic-gate 	ds->error = error;
1857c478bd9Sstevel@tonic-gate 	ds->done = 1;
1867c478bd9Sstevel@tonic-gate 	cv_signal(&ds->cond);
1877c478bd9Sstevel@tonic-gate 	mutex_exit(&ds->lock);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * When we reach here the memory being drained should have
1927c478bd9Sstevel@tonic-gate  * already been reserved in dr_pre_release_mem().
1937c478bd9Sstevel@tonic-gate  * Our only task here is to kick off the "drain" and wait
1947c478bd9Sstevel@tonic-gate  * for it to finish.
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate void
dr_release_mem(dr_common_unit_t * cp)1977c478bd9Sstevel@tonic-gate dr_release_mem(dr_common_unit_t *cp)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	dr_mem_unit_t	*mp = (dr_mem_unit_t *)cp;
2007c478bd9Sstevel@tonic-gate 	int		err;
2017c478bd9Sstevel@tonic-gate 	dr_release_mem_sync_t rms;
2027c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_release_mem";
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/* check that this memory unit has been reserved */
2057c478bd9Sstevel@tonic-gate 	if (!(mp->sbm_flags & DR_MFLAG_RELOWNER)) {
2067c478bd9Sstevel@tonic-gate 		DR_DEV_INTERNAL_ERROR(&mp->sbm_cm);
2077c478bd9Sstevel@tonic-gate 		return;
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	bzero((void *) &rms, sizeof (rms));
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	mutex_init(&rms.lock, NULL, MUTEX_DRIVER, NULL);
2137c478bd9Sstevel@tonic-gate 	cv_init(&rms.cond, NULL, CV_DRIVER, NULL);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	mutex_enter(&rms.lock);
216d3d50737SRafael Vanoni 	err = kphysm_del_start(mp->sbm_memhandle, dr_mem_del_done,
217d3d50737SRafael Vanoni 	    (void *) &rms);
2187c478bd9Sstevel@tonic-gate 	if (err == KPHYSM_OK) {
2197c478bd9Sstevel@tonic-gate 		/* wait for completion or interrupt */
2207c478bd9Sstevel@tonic-gate 		while (!rms.done) {
2217c478bd9Sstevel@tonic-gate 			if (cv_wait_sig(&rms.cond, &rms.lock) == 0) {
2227c478bd9Sstevel@tonic-gate 				/* then there is a pending UNIX signal */
2237c478bd9Sstevel@tonic-gate 				(void) kphysm_del_cancel(mp->sbm_memhandle);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 				/* wait for completion */
2267c478bd9Sstevel@tonic-gate 				while (!rms.done)
2277c478bd9Sstevel@tonic-gate 					cv_wait(&rms.cond, &rms.lock);
2287c478bd9Sstevel@tonic-gate 			}
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 		/* get the result of the memory delete operation */
2317c478bd9Sstevel@tonic-gate 		err = rms.error;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 	mutex_exit(&rms.lock);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	cv_destroy(&rms.cond);
2367c478bd9Sstevel@tonic-gate 	mutex_destroy(&rms.lock);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (err != KPHYSM_OK) {
2397c478bd9Sstevel@tonic-gate 		int e_code;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		switch (err) {
2427c478bd9Sstevel@tonic-gate 			case KPHYSM_ENOWORK:
2437c478bd9Sstevel@tonic-gate 				e_code = ESBD_NOERROR;
2447c478bd9Sstevel@tonic-gate 				break;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 			case KPHYSM_EHANDLE:
2477c478bd9Sstevel@tonic-gate 			case KPHYSM_ESEQUENCE:
2487c478bd9Sstevel@tonic-gate 				e_code = ESBD_INTERNAL;
2497c478bd9Sstevel@tonic-gate 				break;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 			case KPHYSM_ENOTVIABLE:
2527c478bd9Sstevel@tonic-gate 				e_code = ESBD_MEM_NOTVIABLE;
2537c478bd9Sstevel@tonic-gate 				break;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 			case KPHYSM_EREFUSED:
2567c478bd9Sstevel@tonic-gate 				e_code = ESBD_MEM_REFUSED;
2577c478bd9Sstevel@tonic-gate 				break;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 			case KPHYSM_ENONRELOC:
2607c478bd9Sstevel@tonic-gate 				e_code = ESBD_MEM_NONRELOC;
2617c478bd9Sstevel@tonic-gate 				break;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 			case KPHYSM_ECANCELLED:
2647c478bd9Sstevel@tonic-gate 				e_code = ESBD_MEM_CANCELLED;
2657c478bd9Sstevel@tonic-gate 				break;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 			case KPHYSM_ERESOURCE:
2687c478bd9Sstevel@tonic-gate 				e_code = ESBD_MEMFAIL;
2697c478bd9Sstevel@tonic-gate 				break;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 			default:
2727c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
273d3d50737SRafael Vanoni 				    "%s: unexpected kphysm error code %d,"
274d3d50737SRafael Vanoni 				    " id 0x%p",
275d3d50737SRafael Vanoni 				    f, err, mp->sbm_cm.sbdev_id);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 				e_code = ESBD_IO;
2787c478bd9Sstevel@tonic-gate 				break;
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		if (e_code != ESBD_NOERROR) {
2827c478bd9Sstevel@tonic-gate 			dr_dev_err(CE_IGNORE, &mp->sbm_cm, e_code);
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate void
dr_attach_mem(dr_handle_t * hp,dr_common_unit_t * cp)2887c478bd9Sstevel@tonic-gate dr_attach_mem(dr_handle_t *hp, dr_common_unit_t *cp)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(hp))
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	dr_mem_unit_t	*mp = (dr_mem_unit_t *)cp;
2937c478bd9Sstevel@tonic-gate 	struct memlist	*ml, *mc;
2947c478bd9Sstevel@tonic-gate 	sbd_error_t	*err;
2957c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_attach_mem";
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	dr_lock_status(hp->h_bd);
3007c478bd9Sstevel@tonic-gate 	err = drmach_configure(cp->sbdev_id, 0);
3017c478bd9Sstevel@tonic-gate 	dr_unlock_status(hp->h_bd);
3027c478bd9Sstevel@tonic-gate 	if (err) {
3037c478bd9Sstevel@tonic-gate 		DRERR_SET_C(&cp->sbdev_error, &err);
3047c478bd9Sstevel@tonic-gate 		return;
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	ml = dr_get_memlist(mp);
30856f33205SJonathan Adams 	for (mc = ml; mc; mc = mc->ml_next) {
3097c478bd9Sstevel@tonic-gate 		int		 rv;
3107c478bd9Sstevel@tonic-gate 		sbd_error_t	*err;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 		rv = kphysm_add_memory_dynamic(
31356f33205SJonathan Adams 		    (pfn_t)(mc->ml_address >> PAGESHIFT),
31456f33205SJonathan Adams 		    (pgcnt_t)(mc->ml_size >> PAGESHIFT));
3157c478bd9Sstevel@tonic-gate 		if (rv != KPHYSM_OK) {
3167c478bd9Sstevel@tonic-gate 			/*
3177c478bd9Sstevel@tonic-gate 			 * translate kphysm error and
3187c478bd9Sstevel@tonic-gate 			 * store in devlist error
3197c478bd9Sstevel@tonic-gate 			 */
3207c478bd9Sstevel@tonic-gate 			switch (rv) {
3217c478bd9Sstevel@tonic-gate 			case KPHYSM_ERESOURCE:
3227c478bd9Sstevel@tonic-gate 				rv = ESBD_NOMEM;
3237c478bd9Sstevel@tonic-gate 				break;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 			case KPHYSM_EFAULT:
3267c478bd9Sstevel@tonic-gate 				rv = ESBD_FAULT;
3277c478bd9Sstevel@tonic-gate 				break;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 			default:
3307c478bd9Sstevel@tonic-gate 				rv = ESBD_INTERNAL;
3317c478bd9Sstevel@tonic-gate 				break;
3327c478bd9Sstevel@tonic-gate 			}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 			if (rv == ESBD_INTERNAL) {
3357c478bd9Sstevel@tonic-gate 				DR_DEV_INTERNAL_ERROR(&mp->sbm_cm);
3367c478bd9Sstevel@tonic-gate 			} else
3377c478bd9Sstevel@tonic-gate 				dr_dev_err(CE_WARN, &mp->sbm_cm, rv);
3387c478bd9Sstevel@tonic-gate 			break;
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		err = drmach_mem_add_span(
34256f33205SJonathan Adams 		    mp->sbm_cm.sbdev_id, mc->ml_address, mc->ml_size);
3437c478bd9Sstevel@tonic-gate 		if (err) {
3447c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
3457c478bd9Sstevel@tonic-gate 			break;
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	memlist_delete(ml);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	/* back out if configure failed */
3527c478bd9Sstevel@tonic-gate 	if (mp->sbm_cm.sbdev_error != NULL) {
3537c478bd9Sstevel@tonic-gate 		dr_lock_status(hp->h_bd);
35425cf1a30Sjl 		err = drmach_unconfigure(cp->sbdev_id,
355d3d50737SRafael Vanoni 		    DEVI_BRANCH_DESTROY);
3567c478bd9Sstevel@tonic-gate 		if (err)
3577c478bd9Sstevel@tonic-gate 			sbd_err_clear(&err);
3587c478bd9Sstevel@tonic-gate 		dr_unlock_status(hp->h_bd);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate #define	DR_SCRUB_VALUE	0x0d0e0a0d0b0e0e0fULL
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate static void
dr_mem_ecache_scrub(dr_mem_unit_t * mp,struct memlist * mlist)3657c478bd9Sstevel@tonic-gate dr_mem_ecache_scrub(dr_mem_unit_t *mp, struct memlist *mlist)
3667c478bd9Sstevel@tonic-gate {
3677c478bd9Sstevel@tonic-gate #ifdef DEBUG
368d3d50737SRafael Vanoni 	clock_t		stime = ddi_get_lbolt();
3697c478bd9Sstevel@tonic-gate #endif /* DEBUG */
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	struct memlist	*ml;
3727c478bd9Sstevel@tonic-gate 	uint64_t	scrub_value = DR_SCRUB_VALUE;
3737c478bd9Sstevel@tonic-gate 	processorid_t	cpuid;
3747c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_mem_ecache_scrub";
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	cpuid = drmach_mem_cpu_affinity(mp->sbm_cm.sbdev_id);
3777c478bd9Sstevel@tonic-gate 	affinity_set(cpuid);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	PR_MEM("%s: using proc %d, memlist...\n", f,
3807c478bd9Sstevel@tonic-gate 	    (cpuid == CPU_CURRENT) ? CPU->cpu_id : cpuid);
3817c478bd9Sstevel@tonic-gate 	PR_MEMLIST_DUMP(mlist);
3827c478bd9Sstevel@tonic-gate 
38356f33205SJonathan Adams 	for (ml = mlist; ml; ml = ml->ml_next) {
3847c478bd9Sstevel@tonic-gate 		uint64_t	dst_pa;
3857c478bd9Sstevel@tonic-gate 		uint64_t	nbytes;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 		/* calculate the destination physical address */
38856f33205SJonathan Adams 		dst_pa = ml->ml_address;
38956f33205SJonathan Adams 		if (ml->ml_address & PAGEOFFSET)
3907c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
391d3d50737SRafael Vanoni 			    "%s: address (0x%lx) not on "
39256f33205SJonathan Adams 			    "page boundary", f, ml->ml_address);
3937c478bd9Sstevel@tonic-gate 
39456f33205SJonathan Adams 		nbytes = ml->ml_size;
39556f33205SJonathan Adams 		if (ml->ml_size & PAGEOFFSET)
3967c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
397d3d50737SRafael Vanoni 			    "%s: size (0x%lx) not on "
39856f33205SJonathan Adams 			    "page boundary", f, ml->ml_size);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		/*LINTED*/
4017c478bd9Sstevel@tonic-gate 		while (nbytes > 0) {
4027c478bd9Sstevel@tonic-gate 			/* write 64 bits to dst_pa */
4037c478bd9Sstevel@tonic-gate 			stdphys(dst_pa, scrub_value);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 			/* increment/decrement by cacheline sizes */
4067c478bd9Sstevel@tonic-gate 			dst_pa += DRMACH_COHERENCY_UNIT;
4077c478bd9Sstevel@tonic-gate 			nbytes -= DRMACH_COHERENCY_UNIT;
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	/*
4127c478bd9Sstevel@tonic-gate 	 * flush this cpu's ecache and take care to ensure
4137c478bd9Sstevel@tonic-gate 	 * that all of it's bus transactions have retired.
4147c478bd9Sstevel@tonic-gate 	 */
4157c478bd9Sstevel@tonic-gate 	drmach_cpu_flush_ecache_sync();
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	affinity_clear();
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate #ifdef DEBUG
420d3d50737SRafael Vanoni 	stime = ddi_get_lbolt() - stime;
4217c478bd9Sstevel@tonic-gate 	PR_MEM("%s: scrub ticks = %ld (%ld secs)\n", f, stime, stime / hz);
4227c478bd9Sstevel@tonic-gate #endif /* DEBUG */
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate static int
dr_move_memory(dr_handle_t * hp,dr_mem_unit_t * s_mp,dr_mem_unit_t * t_mp)4267c478bd9Sstevel@tonic-gate dr_move_memory(dr_handle_t *hp, dr_mem_unit_t *s_mp, dr_mem_unit_t *t_mp)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate 	time_t		 copytime;
4297c478bd9Sstevel@tonic-gate 	drmachid_t	 cr_id;
4307c478bd9Sstevel@tonic-gate 	dr_sr_handle_t	*srhp;
431db874c57Selowe 	struct memlist	*c_ml, *d_ml;
4327c478bd9Sstevel@tonic-gate 	sbd_error_t	*err;
4337c478bd9Sstevel@tonic-gate 	static fn_t	 f = "dr_move_memory";
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	PR_MEM("%s: (INLINE) moving memory from %s to %s\n",
436d3d50737SRafael Vanoni 	    f,
437d3d50737SRafael Vanoni 	    s_mp->sbm_cm.sbdev_path,
438d3d50737SRafael Vanoni 	    t_mp->sbm_cm.sbdev_path);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	ASSERT(s_mp->sbm_flags & DR_MFLAG_SOURCE);
4417c478bd9Sstevel@tonic-gate 	ASSERT(s_mp->sbm_peer == t_mp);
4427c478bd9Sstevel@tonic-gate 	ASSERT(s_mp->sbm_mlist);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	ASSERT(t_mp->sbm_flags & DR_MFLAG_TARGET);
4457c478bd9Sstevel@tonic-gate 	ASSERT(t_mp->sbm_peer == s_mp);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	/*
4487c478bd9Sstevel@tonic-gate 	 * create a memlist of spans to copy by removing
4497c478bd9Sstevel@tonic-gate 	 * the spans that have been deleted, if any, from
4507c478bd9Sstevel@tonic-gate 	 * the full source board memlist.  s_mp->sbm_del_mlist
4517c478bd9Sstevel@tonic-gate 	 * will be NULL if there were no spans deleted from
4527c478bd9Sstevel@tonic-gate 	 * the source board.
4537c478bd9Sstevel@tonic-gate 	 */
4547c478bd9Sstevel@tonic-gate 	c_ml = memlist_dup(s_mp->sbm_mlist);
4557c478bd9Sstevel@tonic-gate 	d_ml = s_mp->sbm_del_mlist;
4567c478bd9Sstevel@tonic-gate 	while (d_ml != NULL) {
45756f33205SJonathan Adams 		c_ml = memlist_del_span(c_ml, d_ml->ml_address, d_ml->ml_size);
45856f33205SJonathan Adams 		d_ml = d_ml->ml_next;
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	affinity_set(drmach_mem_cpu_affinity(t_mp->sbm_cm.sbdev_id));
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	err = drmach_copy_rename_init(
464d3d50737SRafael Vanoni 	    t_mp->sbm_cm.sbdev_id, _ptob64(t_mp->sbm_slice_offset),
465d3d50737SRafael Vanoni 	    s_mp->sbm_cm.sbdev_id, c_ml, &cr_id);
4667c478bd9Sstevel@tonic-gate 	if (err) {
4677c478bd9Sstevel@tonic-gate 		DRERR_SET_C(&s_mp->sbm_cm.sbdev_error, &err);
4687c478bd9Sstevel@tonic-gate 		affinity_clear();
4697c478bd9Sstevel@tonic-gate 		return (-1);
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	srhp = dr_get_sr_handle(hp);
4737c478bd9Sstevel@tonic-gate 	ASSERT(srhp);
4747c478bd9Sstevel@tonic-gate 
475d3d50737SRafael Vanoni 	copytime = ddi_get_lbolt();
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/* Quiesce the OS.  */
4787c478bd9Sstevel@tonic-gate 	if (dr_suspend(srhp)) {
4797c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: failed to quiesce OS"
480d3d50737SRafael Vanoni 		    " for copy-rename", f);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 		dr_release_sr_handle(srhp);
4837c478bd9Sstevel@tonic-gate 		err = drmach_copy_rename_fini(cr_id);
4847c478bd9Sstevel@tonic-gate 		if (err) {
4857c478bd9Sstevel@tonic-gate 			/*
4867c478bd9Sstevel@tonic-gate 			 * no error is expected since the program has
4877c478bd9Sstevel@tonic-gate 			 * not yet run.
4887c478bd9Sstevel@tonic-gate 			 */
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 			/* catch this in debug kernels */
4917c478bd9Sstevel@tonic-gate 			ASSERT(0);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 			sbd_err_clear(&err);
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 		/* suspend error reached via hp */
4977c478bd9Sstevel@tonic-gate 		s_mp->sbm_cm.sbdev_error = hp->h_err;
4987c478bd9Sstevel@tonic-gate 		hp->h_err = NULL;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 		affinity_clear();
5017c478bd9Sstevel@tonic-gate 		return (-1);
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	/*
5057c478bd9Sstevel@tonic-gate 	 * Rename memory for lgroup.
5067c478bd9Sstevel@tonic-gate 	 * Source and target board numbers are packaged in arg.
5077c478bd9Sstevel@tonic-gate 	 */
5087c478bd9Sstevel@tonic-gate 	{
5097c478bd9Sstevel@tonic-gate 		dr_board_t	*t_bp, *s_bp;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 		s_bp = s_mp->sbm_cm.sbdev_bp;
5127c478bd9Sstevel@tonic-gate 		t_bp = t_mp->sbm_cm.sbdev_bp;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 		lgrp_plat_config(LGRP_CONFIG_MEM_RENAME,
515d3d50737SRafael Vanoni 		    (uintptr_t)(s_bp->b_num | (t_bp->b_num << 16)));
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	drmach_copy_rename(cr_id);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	/* Resume the OS.  */
5217c478bd9Sstevel@tonic-gate 	dr_resume(srhp);
5227c478bd9Sstevel@tonic-gate 
523d3d50737SRafael Vanoni 	copytime = ddi_get_lbolt() - copytime;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	dr_release_sr_handle(srhp);
5267c478bd9Sstevel@tonic-gate 	err = drmach_copy_rename_fini(cr_id);
5277c478bd9Sstevel@tonic-gate 	if (err)
5287c478bd9Sstevel@tonic-gate 		DRERR_SET_C(&s_mp->sbm_cm.sbdev_error, &err);
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	affinity_clear();
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	PR_MEM("%s: copy-rename elapsed time = %ld ticks (%ld secs)\n",
533d3d50737SRafael Vanoni 	    f, copytime, copytime / hz);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	/* return -1 if dr_suspend or copy/rename recorded an error */
5367c478bd9Sstevel@tonic-gate 	return (err == NULL ? 0 : -1);
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate  * If detaching node contains memory that is "non-permanent"
5417c478bd9Sstevel@tonic-gate  * then the memory adr's are simply cleared.  If the memory
5427c478bd9Sstevel@tonic-gate  * is non-relocatable, then do a copy-rename.
5437c478bd9Sstevel@tonic-gate  */
5447c478bd9Sstevel@tonic-gate void
dr_detach_mem(dr_handle_t * hp,dr_common_unit_t * cp)5457c478bd9Sstevel@tonic-gate dr_detach_mem(dr_handle_t *hp, dr_common_unit_t *cp)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	int			rv = 0;
5487c478bd9Sstevel@tonic-gate 	dr_mem_unit_t		*s_mp = (dr_mem_unit_t *)cp;
5497c478bd9Sstevel@tonic-gate 	dr_mem_unit_t		*t_mp;
5507c478bd9Sstevel@tonic-gate 	dr_state_t		state;
5517c478bd9Sstevel@tonic-gate 	static fn_t		f = "dr_detach_mem";
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	/* lookup target mem unit and target board structure, if any */
5567c478bd9Sstevel@tonic-gate 	if (s_mp->sbm_flags & DR_MFLAG_SOURCE) {
5577c478bd9Sstevel@tonic-gate 		t_mp = s_mp->sbm_peer;
5587c478bd9Sstevel@tonic-gate 		ASSERT(t_mp != NULL);
5597c478bd9Sstevel@tonic-gate 		ASSERT(t_mp->sbm_peer == s_mp);
5607c478bd9Sstevel@tonic-gate 	} else {
5617c478bd9Sstevel@tonic-gate 		t_mp = NULL;
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/* verify mem unit's state is UNREFERENCED */
5657c478bd9Sstevel@tonic-gate 	state = s_mp->sbm_cm.sbdev_state;
5667c478bd9Sstevel@tonic-gate 	if (state != DR_STATE_UNREFERENCED) {
5677c478bd9Sstevel@tonic-gate 		dr_dev_err(CE_IGNORE, &s_mp->sbm_cm, ESBD_STATE);
5687c478bd9Sstevel@tonic-gate 		return;
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	/* verify target mem unit's state is UNREFERENCED, if any */
5727c478bd9Sstevel@tonic-gate 	if (t_mp != NULL) {
5737c478bd9Sstevel@tonic-gate 		state = t_mp->sbm_cm.sbdev_state;
5747c478bd9Sstevel@tonic-gate 		if (state != DR_STATE_UNREFERENCED) {
5757c478bd9Sstevel@tonic-gate 			dr_dev_err(CE_IGNORE, &t_mp->sbm_cm, ESBD_STATE);
5767c478bd9Sstevel@tonic-gate 			return;
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	/*
5817c478bd9Sstevel@tonic-gate 	 * Scrub deleted memory.  This will cause all cachelines
5827c478bd9Sstevel@tonic-gate 	 * referencing the memory to only be in the local cpu's
5837c478bd9Sstevel@tonic-gate 	 * ecache.
5847c478bd9Sstevel@tonic-gate 	 */
5857c478bd9Sstevel@tonic-gate 	if (s_mp->sbm_flags & DR_MFLAG_RELDONE) {
5867c478bd9Sstevel@tonic-gate 		/* no del mlist for src<=dst mem size copy/rename */
5877c478bd9Sstevel@tonic-gate 		if (s_mp->sbm_del_mlist)
5887c478bd9Sstevel@tonic-gate 			dr_mem_ecache_scrub(s_mp, s_mp->sbm_del_mlist);
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 	if (t_mp != NULL && (t_mp->sbm_flags & DR_MFLAG_RELDONE)) {
5917c478bd9Sstevel@tonic-gate 		ASSERT(t_mp->sbm_del_mlist);
5927c478bd9Sstevel@tonic-gate 		dr_mem_ecache_scrub(t_mp, t_mp->sbm_del_mlist);
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	/*
5967c478bd9Sstevel@tonic-gate 	 * If there is no target board (no copy/rename was needed), then
5977c478bd9Sstevel@tonic-gate 	 * we're done!
5987c478bd9Sstevel@tonic-gate 	 */
5997c478bd9Sstevel@tonic-gate 	if (t_mp == NULL) {
6007c478bd9Sstevel@tonic-gate 		sbd_error_t *err;
6017c478bd9Sstevel@tonic-gate 		/*
6027c478bd9Sstevel@tonic-gate 		 * Reprogram interconnect hardware and disable
6037c478bd9Sstevel@tonic-gate 		 * memory controllers for memory node that's going away.
6047c478bd9Sstevel@tonic-gate 		 */
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		err = drmach_mem_disable(s_mp->sbm_cm.sbdev_id);
6077c478bd9Sstevel@tonic-gate 		if (err) {
6087c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&s_mp->sbm_cm.sbdev_error, &err);
6097c478bd9Sstevel@tonic-gate 			rv = -1;
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 	} else {
6127c478bd9Sstevel@tonic-gate 		rv = dr_move_memory(hp, s_mp, t_mp);
6137c478bd9Sstevel@tonic-gate 		PR_MEM("%s: %s memory COPY-RENAME (board %d -> %d)\n",
614d3d50737SRafael Vanoni 		    f,
615d3d50737SRafael Vanoni 		    rv ? "FAILED" : "COMPLETED",
616d3d50737SRafael Vanoni 		    s_mp->sbm_cm.sbdev_bp->b_num,
617d3d50737SRafael Vanoni 		    t_mp->sbm_cm.sbdev_bp->b_num);
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		if (rv != 0)
6207c478bd9Sstevel@tonic-gate 			(void) dr_cancel_mem(s_mp);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	if (rv == 0) {
6247c478bd9Sstevel@tonic-gate 		sbd_error_t *err;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 		dr_lock_status(hp->h_bd);
6277c478bd9Sstevel@tonic-gate 		err = drmach_unconfigure(s_mp->sbm_cm.sbdev_id,
62825cf1a30Sjl 		    DEVI_BRANCH_DESTROY);
6297c478bd9Sstevel@tonic-gate 		dr_unlock_status(hp->h_bd);
6307c478bd9Sstevel@tonic-gate 		if (err)
6317c478bd9Sstevel@tonic-gate 			sbd_err_clear(&err);
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate /*
6367c478bd9Sstevel@tonic-gate  * XXX workaround for certain lab configurations (see also starcat drmach.c)
6377c478bd9Sstevel@tonic-gate  * Temporary code to get around observed incorrect results from
6387c478bd9Sstevel@tonic-gate  * kphysm_del_span_query when the queried span contains address spans
6397c478bd9Sstevel@tonic-gate  * not occupied by memory in between spans that do have memory.
6407c478bd9Sstevel@tonic-gate  * This routine acts as a wrapper to kphysm_del_span_query.  It builds
6417c478bd9Sstevel@tonic-gate  * a memlist from phys_install of spans that exist between base and
6427c478bd9Sstevel@tonic-gate  * base + npages, inclusively.  Kphysm_del_span_query is called for each
6437c478bd9Sstevel@tonic-gate  * node in the memlist with the results accumulated in *mp.
6447c478bd9Sstevel@tonic-gate  */
6457c478bd9Sstevel@tonic-gate static int
dr_del_span_query(pfn_t base,pgcnt_t npages,memquery_t * mp)6467c478bd9Sstevel@tonic-gate dr_del_span_query(pfn_t base, pgcnt_t npages, memquery_t *mp)
6477c478bd9Sstevel@tonic-gate {
6487c478bd9Sstevel@tonic-gate 	uint64_t	 pa = _ptob64(base);
6497c478bd9Sstevel@tonic-gate 	uint64_t	 sm = ~ (137438953472ull - 1);
6507c478bd9Sstevel@tonic-gate 	uint64_t	 sa = pa & sm;
6517c478bd9Sstevel@tonic-gate 	struct memlist	*mlist, *ml;
6527c478bd9Sstevel@tonic-gate 	int		 rv;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	npages = npages; /* silence lint */
6557c478bd9Sstevel@tonic-gate 	memlist_read_lock();
6567c478bd9Sstevel@tonic-gate 	mlist = memlist_dup(phys_install);
6577c478bd9Sstevel@tonic-gate 	memlist_read_unlock();
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate again:
66056f33205SJonathan Adams 	for (ml = mlist; ml; ml = ml->ml_next) {
66156f33205SJonathan Adams 		if ((ml->ml_address & sm) != sa) {
66256f33205SJonathan Adams 			mlist = memlist_del_span(mlist,
66356f33205SJonathan Adams 			    ml->ml_address, ml->ml_size);
6647c478bd9Sstevel@tonic-gate 			goto again;
6657c478bd9Sstevel@tonic-gate 		}
6667c478bd9Sstevel@tonic-gate 	}
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	mp->phys_pages = 0;
6697c478bd9Sstevel@tonic-gate 	mp->managed = 0;
6707c478bd9Sstevel@tonic-gate 	mp->nonrelocatable = 0;
6717c478bd9Sstevel@tonic-gate 	mp->first_nonrelocatable = (pfn_t)-1;	/* XXX */
6727c478bd9Sstevel@tonic-gate 	mp->last_nonrelocatable = 0;
6737c478bd9Sstevel@tonic-gate 
67456f33205SJonathan Adams 	for (ml = mlist; ml; ml = ml->ml_next) {
6757c478bd9Sstevel@tonic-gate 		memquery_t mq;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 		rv = kphysm_del_span_query(
67856f33205SJonathan Adams 		    _b64top(ml->ml_address), _b64top(ml->ml_size), &mq);
6797c478bd9Sstevel@tonic-gate 		if (rv)
6807c478bd9Sstevel@tonic-gate 			break;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		mp->phys_pages += mq.phys_pages;
6837c478bd9Sstevel@tonic-gate 		mp->managed += mq.managed;
6847c478bd9Sstevel@tonic-gate 		mp->nonrelocatable += mq.nonrelocatable;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 		if (mq.nonrelocatable != 0) {
6877c478bd9Sstevel@tonic-gate 			if (mq.first_nonrelocatable < mp->first_nonrelocatable)
6887c478bd9Sstevel@tonic-gate 				mp->first_nonrelocatable =
689d3d50737SRafael Vanoni 				    mq.first_nonrelocatable;
6907c478bd9Sstevel@tonic-gate 			if (mq.last_nonrelocatable > mp->last_nonrelocatable)
6917c478bd9Sstevel@tonic-gate 				mp->last_nonrelocatable =
692d3d50737SRafael Vanoni 				    mq.last_nonrelocatable;
6937c478bd9Sstevel@tonic-gate 		}
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	if (mp->nonrelocatable == 0)
6977c478bd9Sstevel@tonic-gate 		mp->first_nonrelocatable = 0;	/* XXX */
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	memlist_delete(mlist);
7007c478bd9Sstevel@tonic-gate 	return (rv);
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate #define	kphysm_del_span_query dr_del_span_query
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate /*
7067c478bd9Sstevel@tonic-gate  * NOTE: This routine is only partially smart about multiple
7077c478bd9Sstevel@tonic-gate  *	 mem-units.  Need to make mem-status structure smart
7087c478bd9Sstevel@tonic-gate  *	 about them also.
7097c478bd9Sstevel@tonic-gate  */
7107c478bd9Sstevel@tonic-gate int
dr_mem_status(dr_handle_t * hp,dr_devset_t devset,sbd_dev_stat_t * dsp)7117c478bd9Sstevel@tonic-gate dr_mem_status(dr_handle_t *hp, dr_devset_t devset, sbd_dev_stat_t *dsp)
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate 	int		m, mix;
7147c478bd9Sstevel@tonic-gate 	memdelstat_t	mdst;
7157c478bd9Sstevel@tonic-gate 	memquery_t	mq;
7167c478bd9Sstevel@tonic-gate 	dr_board_t	*bp;
7177c478bd9Sstevel@tonic-gate 	dr_mem_unit_t	*mp;
7187c478bd9Sstevel@tonic-gate 	sbd_mem_stat_t	*msp;
7197c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_mem_status";
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	bp = hp->h_bd;
7227c478bd9Sstevel@tonic-gate 	devset &= DR_DEVS_PRESENT(bp);
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	for (m = mix = 0; m < MAX_MEM_UNITS_PER_BOARD; m++) {
7257c478bd9Sstevel@tonic-gate 		int		rv;
7267c478bd9Sstevel@tonic-gate 		sbd_error_t	*err;
7277c478bd9Sstevel@tonic-gate 		drmach_status_t	 pstat;
7287c478bd9Sstevel@tonic-gate 		dr_mem_unit_t	*p_mp;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		if (DEVSET_IN_SET(devset, SBD_COMP_MEM, m) == 0)
7317c478bd9Sstevel@tonic-gate 			continue;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		mp = dr_get_mem_unit(bp, m);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 		if (mp->sbm_cm.sbdev_state == DR_STATE_EMPTY) {
7367c478bd9Sstevel@tonic-gate 			/* present, but not fully initialized */
7377c478bd9Sstevel@tonic-gate 			continue;
7387c478bd9Sstevel@tonic-gate 		}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 		if (mp->sbm_cm.sbdev_id == (drmachid_t)0)
7417c478bd9Sstevel@tonic-gate 			continue;
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 		/* fetch platform status */
7447c478bd9Sstevel@tonic-gate 		err = drmach_status(mp->sbm_cm.sbdev_id, &pstat);
7457c478bd9Sstevel@tonic-gate 		if (err) {
7467c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
7477c478bd9Sstevel@tonic-gate 			continue;
7487c478bd9Sstevel@tonic-gate 		}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 		msp = &dsp->d_mem;
7517c478bd9Sstevel@tonic-gate 		bzero((caddr_t)msp, sizeof (*msp));
7527c478bd9Sstevel@tonic-gate 
75307d06da5SSurya Prakki 		(void) strncpy(msp->ms_cm.c_id.c_name, pstat.type,
754d3d50737SRafael Vanoni 		    sizeof (msp->ms_cm.c_id.c_name));
7557c478bd9Sstevel@tonic-gate 		msp->ms_cm.c_id.c_type = mp->sbm_cm.sbdev_type;
7567c478bd9Sstevel@tonic-gate 		msp->ms_cm.c_id.c_unit = SBD_NULL_UNIT;
7577c478bd9Sstevel@tonic-gate 		msp->ms_cm.c_cond = mp->sbm_cm.sbdev_cond;
7587c478bd9Sstevel@tonic-gate 		msp->ms_cm.c_busy = mp->sbm_cm.sbdev_busy | pstat.busy;
7597c478bd9Sstevel@tonic-gate 		msp->ms_cm.c_time = mp->sbm_cm.sbdev_time;
7607c478bd9Sstevel@tonic-gate 		msp->ms_cm.c_ostate = mp->sbm_cm.sbdev_ostate;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 		msp->ms_totpages = mp->sbm_npages;
7637c478bd9Sstevel@tonic-gate 		msp->ms_basepfn = mp->sbm_basepfn;
7647c478bd9Sstevel@tonic-gate 		msp->ms_pageslost = mp->sbm_pageslost;
7657c478bd9Sstevel@tonic-gate 		msp->ms_cage_enabled = kcage_on;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 		if (mp->sbm_flags & DR_MFLAG_RESERVED)
7687c478bd9Sstevel@tonic-gate 			p_mp = mp->sbm_peer;
7697c478bd9Sstevel@tonic-gate 		else
7707c478bd9Sstevel@tonic-gate 			p_mp = NULL;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 		if (p_mp == NULL) {
7737c478bd9Sstevel@tonic-gate 			msp->ms_peer_is_target = 0;
7747c478bd9Sstevel@tonic-gate 			msp->ms_peer_ap_id[0] = '\0';
7757c478bd9Sstevel@tonic-gate 		} else if (p_mp->sbm_flags & DR_MFLAG_RESERVED) {
7767c478bd9Sstevel@tonic-gate 			char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7777c478bd9Sstevel@tonic-gate 			char *minor;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 			/*
7807c478bd9Sstevel@tonic-gate 			 * b_dip doesn't have to be held for ddi_pathname()
7817c478bd9Sstevel@tonic-gate 			 * because the board struct (dr_board_t) will be
7827c478bd9Sstevel@tonic-gate 			 * destroyed before b_dip detaches.
7837c478bd9Sstevel@tonic-gate 			 */
7847c478bd9Sstevel@tonic-gate 			(void) ddi_pathname(bp->b_dip, path);
7857c478bd9Sstevel@tonic-gate 			minor = strchr(p_mp->sbm_cm.sbdev_path, ':');
7867c478bd9Sstevel@tonic-gate 
78707d06da5SSurya Prakki 			(void) snprintf(msp->ms_peer_ap_id,
7887c478bd9Sstevel@tonic-gate 			    sizeof (msp->ms_peer_ap_id), "%s%s",
7897c478bd9Sstevel@tonic-gate 			    path, (minor == NULL) ? "" : minor);
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 			kmem_free(path, MAXPATHLEN);
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 			if (p_mp->sbm_flags & DR_MFLAG_TARGET)
7947c478bd9Sstevel@tonic-gate 				msp->ms_peer_is_target = 1;
7957c478bd9Sstevel@tonic-gate 		}
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 		if (mp->sbm_flags & DR_MFLAG_RELOWNER)
7987c478bd9Sstevel@tonic-gate 			rv = kphysm_del_status(mp->sbm_memhandle, &mdst);
7997c478bd9Sstevel@tonic-gate 		else
8007c478bd9Sstevel@tonic-gate 			rv = KPHYSM_EHANDLE;	/* force 'if' to fail */
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 		if (rv == KPHYSM_OK) {
8037c478bd9Sstevel@tonic-gate 			/*
8047c478bd9Sstevel@tonic-gate 			 * Any pages above managed is "free",
8057c478bd9Sstevel@tonic-gate 			 * i.e. it's collected.
8067c478bd9Sstevel@tonic-gate 			 */
8077c478bd9Sstevel@tonic-gate 			msp->ms_detpages += (uint_t)(mdst.collected +
8087c478bd9Sstevel@tonic-gate 			    mdst.phys_pages - mdst.managed);
8097c478bd9Sstevel@tonic-gate 		} else {
8107c478bd9Sstevel@tonic-gate 			/*
8117c478bd9Sstevel@tonic-gate 			 * If we're UNREFERENCED or UNCONFIGURED,
8127c478bd9Sstevel@tonic-gate 			 * then the number of detached pages is
8137c478bd9Sstevel@tonic-gate 			 * however many pages are on the board.
8147c478bd9Sstevel@tonic-gate 			 * I.e. detached = not in use by OS.
8157c478bd9Sstevel@tonic-gate 			 */
8167c478bd9Sstevel@tonic-gate 			switch (msp->ms_cm.c_ostate) {
8177c478bd9Sstevel@tonic-gate 			/*
8187c478bd9Sstevel@tonic-gate 			 * changed to use cfgadm states
8197c478bd9Sstevel@tonic-gate 			 *
8207c478bd9Sstevel@tonic-gate 			 * was:
8217c478bd9Sstevel@tonic-gate 			 *	case DR_STATE_UNREFERENCED:
8227c478bd9Sstevel@tonic-gate 			 *	case DR_STATE_UNCONFIGURED:
8237c478bd9Sstevel@tonic-gate 			 */
8247c478bd9Sstevel@tonic-gate 			case SBD_STAT_UNCONFIGURED:
8257c478bd9Sstevel@tonic-gate 				msp->ms_detpages = msp->ms_totpages;
8267c478bd9Sstevel@tonic-gate 				break;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 			default:
8297c478bd9Sstevel@tonic-gate 				break;
8307c478bd9Sstevel@tonic-gate 			}
8317c478bd9Sstevel@tonic-gate 		}
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 		/*
8347c478bd9Sstevel@tonic-gate 		 * kphysm_del_span_query can report non-reloc pages = total
8357c478bd9Sstevel@tonic-gate 		 * pages for memory that is not yet configured
8367c478bd9Sstevel@tonic-gate 		 */
8377c478bd9Sstevel@tonic-gate 		if (mp->sbm_cm.sbdev_state != DR_STATE_UNCONFIGURED) {
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 			rv = kphysm_del_span_query(mp->sbm_basepfn,
8407c478bd9Sstevel@tonic-gate 			    mp->sbm_npages, &mq);
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 			if (rv == KPHYSM_OK) {
8437c478bd9Sstevel@tonic-gate 				msp->ms_managed_pages = mq.managed;
8447c478bd9Sstevel@tonic-gate 				msp->ms_noreloc_pages = mq.nonrelocatable;
8457c478bd9Sstevel@tonic-gate 				msp->ms_noreloc_first =
8467c478bd9Sstevel@tonic-gate 				    mq.first_nonrelocatable;
8477c478bd9Sstevel@tonic-gate 				msp->ms_noreloc_last =
8487c478bd9Sstevel@tonic-gate 				    mq.last_nonrelocatable;
8497c478bd9Sstevel@tonic-gate 				msp->ms_cm.c_sflags = 0;
8507c478bd9Sstevel@tonic-gate 				if (mq.nonrelocatable) {
8517c478bd9Sstevel@tonic-gate 					SBD_SET_SUSPEND(SBD_CMD_UNCONFIGURE,
8527c478bd9Sstevel@tonic-gate 					    msp->ms_cm.c_sflags);
8537c478bd9Sstevel@tonic-gate 				}
8547c478bd9Sstevel@tonic-gate 			} else {
8557c478bd9Sstevel@tonic-gate 				PR_MEM("%s: kphysm_del_span_query() = %d\n",
8567c478bd9Sstevel@tonic-gate 				    f, rv);
8577c478bd9Sstevel@tonic-gate 			}
8587c478bd9Sstevel@tonic-gate 		}
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 		/*
8617c478bd9Sstevel@tonic-gate 		 * Check source unit state during copy-rename
8627c478bd9Sstevel@tonic-gate 		 */
8637c478bd9Sstevel@tonic-gate 		if ((mp->sbm_flags & DR_MFLAG_SOURCE) &&
8647c478bd9Sstevel@tonic-gate 		    (mp->sbm_cm.sbdev_state == DR_STATE_UNREFERENCED ||
8657c478bd9Sstevel@tonic-gate 		    mp->sbm_cm.sbdev_state == DR_STATE_RELEASE))
8667c478bd9Sstevel@tonic-gate 			msp->ms_cm.c_ostate = SBD_STAT_CONFIGURED;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 		mix++;
8697c478bd9Sstevel@tonic-gate 		dsp++;
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	return (mix);
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate int
dr_pre_attach_mem(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)8767c478bd9Sstevel@tonic-gate dr_pre_attach_mem(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
8777c478bd9Sstevel@tonic-gate {
8787c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(hp))
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	int		err_flag = 0;
8817c478bd9Sstevel@tonic-gate 	int		d;
8827c478bd9Sstevel@tonic-gate 	sbd_error_t	*err;
8837c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_pre_attach_mem";
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
8887c478bd9Sstevel@tonic-gate 		dr_mem_unit_t	*mp = (dr_mem_unit_t *)devlist[d];
8897c478bd9Sstevel@tonic-gate 		dr_state_t	state;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "OS configure %s", mp->sbm_cm.sbdev_path);
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 		state = mp->sbm_cm.sbdev_state;
8947c478bd9Sstevel@tonic-gate 		switch (state) {
8957c478bd9Sstevel@tonic-gate 		case DR_STATE_UNCONFIGURED:
8967c478bd9Sstevel@tonic-gate 			PR_MEM("%s: recovering from UNCONFIG for %s\n",
897d3d50737SRafael Vanoni 			    f,
898d3d50737SRafael Vanoni 			    mp->sbm_cm.sbdev_path);
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 			/* use memlist cached by dr_post_detach_mem_unit */
9017c478bd9Sstevel@tonic-gate 			ASSERT(mp->sbm_mlist != NULL);
9027c478bd9Sstevel@tonic-gate 			PR_MEM("%s: re-configuring cached memlist for %s:\n",
903d3d50737SRafael Vanoni 			    f, mp->sbm_cm.sbdev_path);
9047c478bd9Sstevel@tonic-gate 			PR_MEMLIST_DUMP(mp->sbm_mlist);
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 			/* kphysm del handle should be have been freed */
9077c478bd9Sstevel@tonic-gate 			ASSERT((mp->sbm_flags & DR_MFLAG_RELOWNER) == 0);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 		case DR_STATE_CONNECTED:
9127c478bd9Sstevel@tonic-gate 			PR_MEM("%s: reprogramming mem hardware on %s\n",
913d3d50737SRafael Vanoni 			    f, mp->sbm_cm.sbdev_bp->b_path);
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 			PR_MEM("%s: enabling %s\n",
916d3d50737SRafael Vanoni 			    f, mp->sbm_cm.sbdev_path);
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 			err = drmach_mem_enable(mp->sbm_cm.sbdev_id);
9197c478bd9Sstevel@tonic-gate 			if (err) {
9207c478bd9Sstevel@tonic-gate 				DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
9217c478bd9Sstevel@tonic-gate 				err_flag = 1;
9227c478bd9Sstevel@tonic-gate 			}
9237c478bd9Sstevel@tonic-gate 			break;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 		default:
9267c478bd9Sstevel@tonic-gate 			dr_dev_err(CE_WARN, &mp->sbm_cm, ESBD_STATE);
9277c478bd9Sstevel@tonic-gate 			err_flag = 1;
9287c478bd9Sstevel@tonic-gate 			break;
9297c478bd9Sstevel@tonic-gate 		}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 		/* exit for loop if error encountered */
9327c478bd9Sstevel@tonic-gate 		if (err_flag)
9337c478bd9Sstevel@tonic-gate 			break;
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	return (err_flag ? -1 : 0);
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate int
dr_post_attach_mem(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)9407c478bd9Sstevel@tonic-gate dr_post_attach_mem(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
9417c478bd9Sstevel@tonic-gate {
9427c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(hp))
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	int		d;
9457c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_post_attach_mem";
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
9507c478bd9Sstevel@tonic-gate 		dr_mem_unit_t	*mp = (dr_mem_unit_t *)devlist[d];
9517c478bd9Sstevel@tonic-gate 		struct memlist	*mlist, *ml;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 		mlist = dr_get_memlist(mp);
9547c478bd9Sstevel@tonic-gate 		if (mlist == NULL) {
9557c478bd9Sstevel@tonic-gate 			dr_dev_err(CE_WARN, &mp->sbm_cm, ESBD_MEMFAIL);
9567c478bd9Sstevel@tonic-gate 			continue;
9577c478bd9Sstevel@tonic-gate 		}
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 		/*
9607c478bd9Sstevel@tonic-gate 		 * Verify the memory really did successfully attach
9617c478bd9Sstevel@tonic-gate 		 * by checking for its existence in phys_install.
9627c478bd9Sstevel@tonic-gate 		 */
9637c478bd9Sstevel@tonic-gate 		memlist_read_lock();
9647c478bd9Sstevel@tonic-gate 		if (memlist_intersect(phys_install, mlist) == 0) {
9657c478bd9Sstevel@tonic-gate 			memlist_read_unlock();
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 			DR_DEV_INTERNAL_ERROR(&mp->sbm_cm);
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 			PR_MEM("%s: %s memlist not in phys_install",
970d3d50737SRafael Vanoni 			    f, mp->sbm_cm.sbdev_path);
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 			memlist_delete(mlist);
9737c478bd9Sstevel@tonic-gate 			continue;
9747c478bd9Sstevel@tonic-gate 		}
9757c478bd9Sstevel@tonic-gate 		memlist_read_unlock();
9767c478bd9Sstevel@tonic-gate 
97756f33205SJonathan Adams 		for (ml = mlist; ml != NULL; ml = ml->ml_next) {
9787c478bd9Sstevel@tonic-gate 			sbd_error_t *err;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 			err = drmach_mem_add_span(
981d3d50737SRafael Vanoni 			    mp->sbm_cm.sbdev_id,
98256f33205SJonathan Adams 			    ml->ml_address,
98356f33205SJonathan Adams 			    ml->ml_size);
9847c478bd9Sstevel@tonic-gate 			if (err)
9857c478bd9Sstevel@tonic-gate 				DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
9867c478bd9Sstevel@tonic-gate 		}
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 		memlist_delete(mlist);
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 		/*
9917c478bd9Sstevel@tonic-gate 		 * Destroy cached memlist, if any.
9927c478bd9Sstevel@tonic-gate 		 * There will be a cached memlist in sbm_mlist if
9937c478bd9Sstevel@tonic-gate 		 * this board is being configured directly after
9947c478bd9Sstevel@tonic-gate 		 * an unconfigure.
9957c478bd9Sstevel@tonic-gate 		 * To support this transition, dr_post_detach_mem
9967c478bd9Sstevel@tonic-gate 		 * left a copy of the last known memlist in sbm_mlist.
9977c478bd9Sstevel@tonic-gate 		 * This memlist could differ from any derived from
9987c478bd9Sstevel@tonic-gate 		 * hardware if while this memunit was last configured
9997c478bd9Sstevel@tonic-gate 		 * the system detected and deleted bad pages from
10007c478bd9Sstevel@tonic-gate 		 * phys_install.  The location of those bad pages
10017c478bd9Sstevel@tonic-gate 		 * will be reflected in the cached memlist.
10027c478bd9Sstevel@tonic-gate 		 */
10037c478bd9Sstevel@tonic-gate 		if (mp->sbm_mlist) {
10047c478bd9Sstevel@tonic-gate 			memlist_delete(mp->sbm_mlist);
10057c478bd9Sstevel@tonic-gate 			mp->sbm_mlist = NULL;
10067c478bd9Sstevel@tonic-gate 		}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate /*
10097c478bd9Sstevel@tonic-gate  * TODO: why is this call to dr_init_mem_unit_data here?
10107c478bd9Sstevel@tonic-gate  * this has been done at discovery or connect time, so this is
10117c478bd9Sstevel@tonic-gate  * probably redundant and unnecessary.
10127c478bd9Sstevel@tonic-gate  */
10137c478bd9Sstevel@tonic-gate 		dr_init_mem_unit_data(mp);
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	return (0);
10177c478bd9Sstevel@tonic-gate }
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate int
dr_pre_detach_mem(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)10207c478bd9Sstevel@tonic-gate dr_pre_detach_mem(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
10217c478bd9Sstevel@tonic-gate {
10227c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(hp))
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	int d;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
10277c478bd9Sstevel@tonic-gate 		dr_mem_unit_t *mp = (dr_mem_unit_t *)devlist[d];
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "OS unconfigure %s", mp->sbm_cm.sbdev_path);
10307c478bd9Sstevel@tonic-gate 	}
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	return (0);
10337c478bd9Sstevel@tonic-gate }
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate int
dr_post_detach_mem(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)10377c478bd9Sstevel@tonic-gate dr_post_detach_mem(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
10387c478bd9Sstevel@tonic-gate {
10397c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(hp))
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	int		d, rv;
10427c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_post_detach_mem";
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	rv = 0;
10477c478bd9Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
10487c478bd9Sstevel@tonic-gate 		dr_mem_unit_t	*mp = (dr_mem_unit_t *)devlist[d];
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 		ASSERT(mp->sbm_cm.sbdev_bp == hp->h_bd);
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 		if (dr_post_detach_mem_unit(mp))
10537c478bd9Sstevel@tonic-gate 			rv = -1;
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	return (rv);
10577c478bd9Sstevel@tonic-gate }
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate static void
dr_add_memory_spans(dr_mem_unit_t * mp,struct memlist * ml)10607c478bd9Sstevel@tonic-gate dr_add_memory_spans(dr_mem_unit_t *mp, struct memlist *ml)
10617c478bd9Sstevel@tonic-gate {
10627c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_add_memory_spans";
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	PR_MEM("%s...", f);
10657c478bd9Sstevel@tonic-gate 	PR_MEMLIST_DUMP(ml);
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate #ifdef DEBUG
10687c478bd9Sstevel@tonic-gate 	memlist_read_lock();
10697c478bd9Sstevel@tonic-gate 	if (memlist_intersect(phys_install, ml)) {
10707c478bd9Sstevel@tonic-gate 		PR_MEM("%s:WARNING: memlist intersects with phys_install\n", f);
10717c478bd9Sstevel@tonic-gate 	}
10727c478bd9Sstevel@tonic-gate 	memlist_read_unlock();
10737c478bd9Sstevel@tonic-gate #endif
10747c478bd9Sstevel@tonic-gate 
107556f33205SJonathan Adams 	for (; ml; ml = ml->ml_next) {
10767c478bd9Sstevel@tonic-gate 		pfn_t		 base;
10777c478bd9Sstevel@tonic-gate 		pgcnt_t		 npgs;
10787c478bd9Sstevel@tonic-gate 		int		 rv;
10797c478bd9Sstevel@tonic-gate 		sbd_error_t	*err;
10807c478bd9Sstevel@tonic-gate 
108156f33205SJonathan Adams 		base = _b64top(ml->ml_address);
108256f33205SJonathan Adams 		npgs = _b64top(ml->ml_size);
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 		rv = kphysm_add_memory_dynamic(base, npgs);
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 		err = drmach_mem_add_span(
1087d3d50737SRafael Vanoni 		    mp->sbm_cm.sbdev_id,
108856f33205SJonathan Adams 		    ml->ml_address,
108956f33205SJonathan Adams 		    ml->ml_size);
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 		if (err)
10927c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 		if (rv != KPHYSM_OK) {
10957c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s:"
1096d3d50737SRafael Vanoni 			    " unexpected kphysm_add_memory_dynamic"
1097d3d50737SRafael Vanoni 			    " return value %d;"
1098d3d50737SRafael Vanoni 			    " basepfn=0x%lx, npages=%ld\n",
1099d3d50737SRafael Vanoni 			    f, rv, base, npgs);
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 			continue;
11027c478bd9Sstevel@tonic-gate 		}
11037c478bd9Sstevel@tonic-gate 	}
11047c478bd9Sstevel@tonic-gate }
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate static int
dr_post_detach_mem_unit(dr_mem_unit_t * s_mp)11077c478bd9Sstevel@tonic-gate dr_post_detach_mem_unit(dr_mem_unit_t *s_mp)
11087c478bd9Sstevel@tonic-gate {
11097c478bd9Sstevel@tonic-gate 	uint64_t	sz = s_mp->sbm_slice_size;
11107c478bd9Sstevel@tonic-gate 	uint64_t	sm = sz - 1;
11117c478bd9Sstevel@tonic-gate 	/* old and new below refer to PAs before and after copy-rename */
11127c478bd9Sstevel@tonic-gate 	uint64_t	s_old_basepa, s_new_basepa;
11137c478bd9Sstevel@tonic-gate 	uint64_t	t_old_basepa, t_new_basepa;
11147c478bd9Sstevel@tonic-gate 	uint64_t	t_new_smallsize = 0;
11157c478bd9Sstevel@tonic-gate 	dr_mem_unit_t	*t_mp, *x_mp;
11167c478bd9Sstevel@tonic-gate 	struct memlist	*ml;
11177c478bd9Sstevel@tonic-gate 	int		rv;
11187c478bd9Sstevel@tonic-gate 	sbd_error_t	*err;
11197c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_post_detach_mem_unit";
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	/* s_mp->sbm_del_mlist could be NULL, meaning no deleted spans */
11247c478bd9Sstevel@tonic-gate 	PR_MEM("%s: %s: deleted memlist (EMPTY maybe okay):\n",
1125d3d50737SRafael Vanoni 	    f, s_mp->sbm_cm.sbdev_path);
11267c478bd9Sstevel@tonic-gate 	PR_MEMLIST_DUMP(s_mp->sbm_del_mlist);
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	/* sanity check */
11297c478bd9Sstevel@tonic-gate 	ASSERT(s_mp->sbm_del_mlist == NULL ||
1130d3d50737SRafael Vanoni 	    (s_mp->sbm_flags & DR_MFLAG_RELDONE) != 0);
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	if (s_mp->sbm_flags & DR_MFLAG_SOURCE) {
11337c478bd9Sstevel@tonic-gate 		t_mp = s_mp->sbm_peer;
11347c478bd9Sstevel@tonic-gate 		ASSERT(t_mp != NULL);
11357c478bd9Sstevel@tonic-gate 		ASSERT(t_mp->sbm_flags & DR_MFLAG_TARGET);
11367c478bd9Sstevel@tonic-gate 		ASSERT(t_mp->sbm_peer == s_mp);
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 		ASSERT(t_mp->sbm_flags & DR_MFLAG_RELDONE);
11397c478bd9Sstevel@tonic-gate 		ASSERT(t_mp->sbm_del_mlist);
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 		PR_MEM("%s: target %s: deleted memlist:\n",
1142d3d50737SRafael Vanoni 		    f, t_mp->sbm_cm.sbdev_path);
11437c478bd9Sstevel@tonic-gate 		PR_MEMLIST_DUMP(t_mp->sbm_del_mlist);
11447c478bd9Sstevel@tonic-gate 	} else {
11457c478bd9Sstevel@tonic-gate 		/* this is no target unit */
11467c478bd9Sstevel@tonic-gate 		t_mp = NULL;
11477c478bd9Sstevel@tonic-gate 	}
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	/*
11507c478bd9Sstevel@tonic-gate 	 * Verify the memory really did successfully detach
11517c478bd9Sstevel@tonic-gate 	 * by checking for its non-existence in phys_install.
11527c478bd9Sstevel@tonic-gate 	 */
11537c478bd9Sstevel@tonic-gate 	rv = 0;
11547c478bd9Sstevel@tonic-gate 	memlist_read_lock();
11557c478bd9Sstevel@tonic-gate 	if (s_mp->sbm_flags & DR_MFLAG_RELDONE) {
11567c478bd9Sstevel@tonic-gate 		x_mp = s_mp;
11577c478bd9Sstevel@tonic-gate 		rv = memlist_intersect(phys_install, x_mp->sbm_del_mlist);
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate 	if (rv == 0 && t_mp && (t_mp->sbm_flags & DR_MFLAG_RELDONE)) {
11607c478bd9Sstevel@tonic-gate 		x_mp = t_mp;
11617c478bd9Sstevel@tonic-gate 		rv = memlist_intersect(phys_install, x_mp->sbm_del_mlist);
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate 	memlist_read_unlock();
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	if (rv) {
11667c478bd9Sstevel@tonic-gate 		/* error: memlist still in phys_install */
11677c478bd9Sstevel@tonic-gate 		DR_DEV_INTERNAL_ERROR(&x_mp->sbm_cm);
11687c478bd9Sstevel@tonic-gate 	}
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	/*
11717c478bd9Sstevel@tonic-gate 	 * clean mem unit state and bail out if an error has been recorded.
11727c478bd9Sstevel@tonic-gate 	 */
11737c478bd9Sstevel@tonic-gate 	rv = 0;
11747c478bd9Sstevel@tonic-gate 	if (s_mp->sbm_cm.sbdev_error) {
11757c478bd9Sstevel@tonic-gate 		PR_MEM("%s: %s flags=%x", f,
1176d3d50737SRafael Vanoni 		    s_mp->sbm_cm.sbdev_path, s_mp->sbm_flags);
11777c478bd9Sstevel@tonic-gate 		DR_DEV_CLR_UNREFERENCED(&s_mp->sbm_cm);
11787c478bd9Sstevel@tonic-gate 		DR_DEV_CLR_RELEASED(&s_mp->sbm_cm);
11797c478bd9Sstevel@tonic-gate 		dr_device_transition(&s_mp->sbm_cm, DR_STATE_CONFIGURED);
11807c478bd9Sstevel@tonic-gate 		rv = -1;
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 	if (t_mp != NULL && t_mp->sbm_cm.sbdev_error != NULL) {
11837c478bd9Sstevel@tonic-gate 		PR_MEM("%s: %s flags=%x", f,
1184d3d50737SRafael Vanoni 		    s_mp->sbm_cm.sbdev_path, s_mp->sbm_flags);
11857c478bd9Sstevel@tonic-gate 		DR_DEV_CLR_UNREFERENCED(&t_mp->sbm_cm);
11867c478bd9Sstevel@tonic-gate 		DR_DEV_CLR_RELEASED(&t_mp->sbm_cm);
11877c478bd9Sstevel@tonic-gate 		dr_device_transition(&t_mp->sbm_cm, DR_STATE_CONFIGURED);
11887c478bd9Sstevel@tonic-gate 		rv = -1;
11897c478bd9Sstevel@tonic-gate 	}
11907c478bd9Sstevel@tonic-gate 	if (rv)
11917c478bd9Sstevel@tonic-gate 		goto cleanup;
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	s_old_basepa = _ptob64(s_mp->sbm_basepfn);
11947c478bd9Sstevel@tonic-gate 	err = drmach_mem_get_base_physaddr(s_mp->sbm_cm.sbdev_id,
11957c478bd9Sstevel@tonic-gate 	    &s_new_basepa);
11967c478bd9Sstevel@tonic-gate 	ASSERT(err == NULL);
11977c478bd9Sstevel@tonic-gate 
119804580fdfSmathue 	PR_MEM("%s:s_old_basepa: 0x%lx\n", f, s_old_basepa);
119904580fdfSmathue 	PR_MEM("%s:s_new_basepa: 0x%lx\n", f, s_new_basepa);
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	if (t_mp != NULL) {
12027c478bd9Sstevel@tonic-gate 		struct memlist *s_copy_mlist;
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 		t_old_basepa	= _ptob64(t_mp->sbm_basepfn);
12057c478bd9Sstevel@tonic-gate 		err = drmach_mem_get_base_physaddr(t_mp->sbm_cm.sbdev_id,
12067c478bd9Sstevel@tonic-gate 		    &t_new_basepa);
12077c478bd9Sstevel@tonic-gate 		ASSERT(err == NULL);
12087c478bd9Sstevel@tonic-gate 
120904580fdfSmathue 		PR_MEM("%s:t_old_basepa: 0x%lx\n", f, t_old_basepa);
121004580fdfSmathue 		PR_MEM("%s:t_new_basepa: 0x%lx\n", f, t_new_basepa);
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 		/*
12137c478bd9Sstevel@tonic-gate 		 * Construct copy list with original source addresses.
12147c478bd9Sstevel@tonic-gate 		 * Used to add back excess target mem.
12157c478bd9Sstevel@tonic-gate 		 */
12167c478bd9Sstevel@tonic-gate 		s_copy_mlist = memlist_dup(s_mp->sbm_mlist);
121756f33205SJonathan Adams 		for (ml = s_mp->sbm_del_mlist; ml; ml = ml->ml_next) {
12187c478bd9Sstevel@tonic-gate 			s_copy_mlist = memlist_del_span(s_copy_mlist,
121956f33205SJonathan Adams 			    ml->ml_address, ml->ml_size);
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 		PR_MEM("%s: source copy list:\n:", f);
12237c478bd9Sstevel@tonic-gate 		PR_MEMLIST_DUMP(s_copy_mlist);
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 		/*
12267c478bd9Sstevel@tonic-gate 		 * We had to swap mem-units, so update
12277c478bd9Sstevel@tonic-gate 		 * memlists accordingly with new base
12287c478bd9Sstevel@tonic-gate 		 * addresses.
12297c478bd9Sstevel@tonic-gate 		 */
123056f33205SJonathan Adams 		for (ml = t_mp->sbm_mlist; ml; ml = ml->ml_next) {
123156f33205SJonathan Adams 			ml->ml_address -= t_old_basepa;
123256f33205SJonathan Adams 			ml->ml_address += t_new_basepa;
12337c478bd9Sstevel@tonic-gate 		}
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 		/*
12367c478bd9Sstevel@tonic-gate 		 * There is no need to explicitly rename the target delete
12377c478bd9Sstevel@tonic-gate 		 * memlist, because sbm_del_mlist and sbm_mlist always
12387c478bd9Sstevel@tonic-gate 		 * point to the same memlist for a copy/rename operation.
12397c478bd9Sstevel@tonic-gate 		 */
12407c478bd9Sstevel@tonic-gate 		ASSERT(t_mp->sbm_del_mlist == t_mp->sbm_mlist);
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 		PR_MEM("%s: renamed target memlist and delete memlist:\n", f);
12437c478bd9Sstevel@tonic-gate 		PR_MEMLIST_DUMP(t_mp->sbm_mlist);
12447c478bd9Sstevel@tonic-gate 
124556f33205SJonathan Adams 		for (ml = s_mp->sbm_mlist; ml; ml = ml->ml_next) {
124656f33205SJonathan Adams 			ml->ml_address -= s_old_basepa;
124756f33205SJonathan Adams 			ml->ml_address += s_new_basepa;
12487c478bd9Sstevel@tonic-gate 		}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 		PR_MEM("%s: renamed source memlist:\n", f);
12517c478bd9Sstevel@tonic-gate 		PR_MEMLIST_DUMP(s_mp->sbm_mlist);
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 		/*
12547c478bd9Sstevel@tonic-gate 		 * Keep track of dynamically added segments
12557c478bd9Sstevel@tonic-gate 		 * since they cannot be split if we need to delete
12567c478bd9Sstevel@tonic-gate 		 * excess source memory later for this board.
12577c478bd9Sstevel@tonic-gate 		 */
12587c478bd9Sstevel@tonic-gate 		if (t_mp->sbm_dyn_segs)
12597c478bd9Sstevel@tonic-gate 			memlist_delete(t_mp->sbm_dyn_segs);
12607c478bd9Sstevel@tonic-gate 		t_mp->sbm_dyn_segs = s_mp->sbm_dyn_segs;
12617c478bd9Sstevel@tonic-gate 		s_mp->sbm_dyn_segs = NULL;
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 		/*
12647c478bd9Sstevel@tonic-gate 		 * If the target memory range with the new target base PA
12657c478bd9Sstevel@tonic-gate 		 * extends beyond the usable slice, prevent any "target excess"
12667c478bd9Sstevel@tonic-gate 		 * from being added back after this copy/rename and
12677c478bd9Sstevel@tonic-gate 		 * calculate the new smaller size of the target board
12687c478bd9Sstevel@tonic-gate 		 * to be set as part of target cleanup. The base + npages
12697c478bd9Sstevel@tonic-gate 		 * must only include the range of memory up to the end of
12707c478bd9Sstevel@tonic-gate 		 * this slice. This will only be used after a category 4
12717c478bd9Sstevel@tonic-gate 		 * large-to-small target type copy/rename - see comments
12727c478bd9Sstevel@tonic-gate 		 * in dr_select_mem_target.
12737c478bd9Sstevel@tonic-gate 		 */
12747c478bd9Sstevel@tonic-gate 		if (((t_new_basepa & sm) + _ptob64(t_mp->sbm_npages)) > sz) {
12757c478bd9Sstevel@tonic-gate 			t_new_smallsize = sz - (t_new_basepa & sm);
12767c478bd9Sstevel@tonic-gate 		}
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 		if (s_mp->sbm_flags & DR_MFLAG_MEMRESIZE &&
12797c478bd9Sstevel@tonic-gate 		    t_new_smallsize == 0) {
12807c478bd9Sstevel@tonic-gate 			struct memlist	*t_excess_mlist;
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 			/*
12837c478bd9Sstevel@tonic-gate 			 * Add back excess target memory.
12847c478bd9Sstevel@tonic-gate 			 * Subtract out the portion of the target memory
12857c478bd9Sstevel@tonic-gate 			 * node that was taken over by the source memory
12867c478bd9Sstevel@tonic-gate 			 * node.
12877c478bd9Sstevel@tonic-gate 			 */
12887c478bd9Sstevel@tonic-gate 			t_excess_mlist = memlist_dup(t_mp->sbm_mlist);
128956f33205SJonathan Adams 			for (ml = s_copy_mlist; ml; ml = ml->ml_next) {
12907c478bd9Sstevel@tonic-gate 				t_excess_mlist =
12917c478bd9Sstevel@tonic-gate 				    memlist_del_span(t_excess_mlist,
129256f33205SJonathan Adams 				    ml->ml_address, ml->ml_size);
12937c478bd9Sstevel@tonic-gate 			}
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 			/*
12967c478bd9Sstevel@tonic-gate 			 * Update dynamically added segs
12977c478bd9Sstevel@tonic-gate 			 */
129856f33205SJonathan Adams 			for (ml = s_mp->sbm_del_mlist; ml; ml = ml->ml_next) {
12997c478bd9Sstevel@tonic-gate 				t_mp->sbm_dyn_segs =
13007c478bd9Sstevel@tonic-gate 				    memlist_del_span(t_mp->sbm_dyn_segs,
130156f33205SJonathan Adams 				    ml->ml_address, ml->ml_size);
13027c478bd9Sstevel@tonic-gate 			}
130356f33205SJonathan Adams 			for (ml = t_excess_mlist; ml; ml = ml->ml_next) {
13047c478bd9Sstevel@tonic-gate 				t_mp->sbm_dyn_segs =
13057c478bd9Sstevel@tonic-gate 				    memlist_cat_span(t_mp->sbm_dyn_segs,
130656f33205SJonathan Adams 				    ml->ml_address, ml->ml_size);
13077c478bd9Sstevel@tonic-gate 			}
13087c478bd9Sstevel@tonic-gate 			PR_MEM("%s: %s: updated dynamic seg list:\n",
13097c478bd9Sstevel@tonic-gate 			    f, t_mp->sbm_cm.sbdev_path);
13107c478bd9Sstevel@tonic-gate 			PR_MEMLIST_DUMP(t_mp->sbm_dyn_segs);
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 			PR_MEM("%s: adding back remaining portion"
1313d3d50737SRafael Vanoni 			    " of %s, memlist:\n",
1314d3d50737SRafael Vanoni 			    f, t_mp->sbm_cm.sbdev_path);
13157c478bd9Sstevel@tonic-gate 			PR_MEMLIST_DUMP(t_excess_mlist);
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 			dr_add_memory_spans(s_mp, t_excess_mlist);
13187c478bd9Sstevel@tonic-gate 			memlist_delete(t_excess_mlist);
13197c478bd9Sstevel@tonic-gate 		}
13207c478bd9Sstevel@tonic-gate 		memlist_delete(s_copy_mlist);
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate #ifdef DEBUG
13237c478bd9Sstevel@tonic-gate 		/*
13247c478bd9Sstevel@tonic-gate 		 * Renaming s_mp->sbm_del_mlist is not necessary.  This
13257c478bd9Sstevel@tonic-gate 		 * list is not used beyond this point, and in fact, is
13267c478bd9Sstevel@tonic-gate 		 * disposed of at the end of this function.
13277c478bd9Sstevel@tonic-gate 		 */
132856f33205SJonathan Adams 		for (ml = s_mp->sbm_del_mlist; ml; ml = ml->ml_next) {
132956f33205SJonathan Adams 			ml->ml_address -= s_old_basepa;
133056f33205SJonathan Adams 			ml->ml_address += s_new_basepa;
13317c478bd9Sstevel@tonic-gate 		}
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 		PR_MEM("%s: renamed source delete memlist", f);
13347c478bd9Sstevel@tonic-gate 		PR_MEMLIST_DUMP(s_mp->sbm_del_mlist);
13357c478bd9Sstevel@tonic-gate #endif
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	}
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	if (t_mp != NULL) {
13407c478bd9Sstevel@tonic-gate 		/* delete target's entire address space */
1341d3d50737SRafael Vanoni 		err = drmach_mem_del_span(t_mp->sbm_cm.sbdev_id,
1342d3d50737SRafael Vanoni 		    t_old_basepa & ~ sm, sz);
13437c478bd9Sstevel@tonic-gate 		if (err)
13447c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&t_mp->sbm_cm.sbdev_error, &err);
13457c478bd9Sstevel@tonic-gate 		ASSERT(err == NULL);
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 		/*
13487c478bd9Sstevel@tonic-gate 		 * After the copy/rename, the original address space
13497c478bd9Sstevel@tonic-gate 		 * for the source board (which is now located on the
13507c478bd9Sstevel@tonic-gate 		 * target board) may now have some excess to be deleted.
13517c478bd9Sstevel@tonic-gate 		 * The amount is calculated by masking the slice
13527c478bd9Sstevel@tonic-gate 		 * info and keeping the slice offset from t_new_basepa.
13537c478bd9Sstevel@tonic-gate 		 */
13547c478bd9Sstevel@tonic-gate 		err = drmach_mem_del_span(s_mp->sbm_cm.sbdev_id,
1355d3d50737SRafael Vanoni 		    s_old_basepa & ~ sm, t_new_basepa & sm);
13567c478bd9Sstevel@tonic-gate 		if (err)
13577c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&s_mp->sbm_cm.sbdev_error, &err);
13587c478bd9Sstevel@tonic-gate 		ASSERT(err == NULL);
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	} else {
13617c478bd9Sstevel@tonic-gate 		/* delete board's entire address space */
13627c478bd9Sstevel@tonic-gate 		err = drmach_mem_del_span(s_mp->sbm_cm.sbdev_id,
1363d3d50737SRafael Vanoni 		    s_old_basepa & ~ sm, sz);
13647c478bd9Sstevel@tonic-gate 		if (err)
13657c478bd9Sstevel@tonic-gate 			DRERR_SET_C(&s_mp->sbm_cm.sbdev_error, &err);
13667c478bd9Sstevel@tonic-gate 		ASSERT(err == NULL);
13677c478bd9Sstevel@tonic-gate 	}
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate cleanup:
13707c478bd9Sstevel@tonic-gate 	/* clean up target mem unit */
13717c478bd9Sstevel@tonic-gate 	if (t_mp != NULL) {
13727c478bd9Sstevel@tonic-gate 		memlist_delete(t_mp->sbm_del_mlist);
13737c478bd9Sstevel@tonic-gate 		/* no need to delete sbm_mlist, it shares sbm_del_mlist */
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 		t_mp->sbm_del_mlist = NULL;
13767c478bd9Sstevel@tonic-gate 		t_mp->sbm_mlist = NULL;
13777c478bd9Sstevel@tonic-gate 		t_mp->sbm_peer = NULL;
13787c478bd9Sstevel@tonic-gate 		t_mp->sbm_flags = 0;
13797c478bd9Sstevel@tonic-gate 		t_mp->sbm_cm.sbdev_busy = 0;
13807c478bd9Sstevel@tonic-gate 		dr_init_mem_unit_data(t_mp);
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 		/* reduce target size if new PAs go past end of usable slice */
13837c478bd9Sstevel@tonic-gate 		if (t_new_smallsize > 0) {
13847c478bd9Sstevel@tonic-gate 			t_mp->sbm_npages = _b64top(t_new_smallsize);
138504580fdfSmathue 			PR_MEM("%s: target new size 0x%lx bytes\n",
1386d3d50737SRafael Vanoni 			    f, t_new_smallsize);
13877c478bd9Sstevel@tonic-gate 		}
13887c478bd9Sstevel@tonic-gate 	}
13897c478bd9Sstevel@tonic-gate 	if (t_mp != NULL && t_mp->sbm_cm.sbdev_error == NULL) {
13907c478bd9Sstevel@tonic-gate 		/*
13917c478bd9Sstevel@tonic-gate 		 * now that copy/rename has completed, undo this
13927c478bd9Sstevel@tonic-gate 		 * work that was done in dr_release_mem_done.
13937c478bd9Sstevel@tonic-gate 		 */
13947c478bd9Sstevel@tonic-gate 		DR_DEV_CLR_UNREFERENCED(&t_mp->sbm_cm);
13957c478bd9Sstevel@tonic-gate 		DR_DEV_CLR_RELEASED(&t_mp->sbm_cm);
13967c478bd9Sstevel@tonic-gate 		dr_device_transition(&t_mp->sbm_cm, DR_STATE_CONFIGURED);
13977c478bd9Sstevel@tonic-gate 	}
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	/*
14007c478bd9Sstevel@tonic-gate 	 * clean up (source) board's mem unit structure.
14017c478bd9Sstevel@tonic-gate 	 * NOTE: sbm_mlist is retained if no error has been record (in other
14027c478bd9Sstevel@tonic-gate 	 * words, when s_mp->sbm_cm.sbdev_error is NULL). This memlist is
14037c478bd9Sstevel@tonic-gate 	 * referred to elsewhere as the cached memlist.  The cached memlist
14047c478bd9Sstevel@tonic-gate 	 * is used to re-attach (configure back in) this memunit from the
14057c478bd9Sstevel@tonic-gate 	 * unconfigured state.  The memlist is retained because it may
14067c478bd9Sstevel@tonic-gate 	 * represent bad pages that were detected while the memory was
14077c478bd9Sstevel@tonic-gate 	 * configured into the OS.  The OS deletes bad pages from phys_install.
14087c478bd9Sstevel@tonic-gate 	 * Those deletes, if any, will be represented in the cached mlist.
14097c478bd9Sstevel@tonic-gate 	 */
14107c478bd9Sstevel@tonic-gate 	if (s_mp->sbm_del_mlist && s_mp->sbm_del_mlist != s_mp->sbm_mlist)
14117c478bd9Sstevel@tonic-gate 		memlist_delete(s_mp->sbm_del_mlist);
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	if (s_mp->sbm_cm.sbdev_error && s_mp->sbm_mlist) {
14147c478bd9Sstevel@tonic-gate 		memlist_delete(s_mp->sbm_mlist);
14157c478bd9Sstevel@tonic-gate 		s_mp->sbm_mlist = NULL;
14167c478bd9Sstevel@tonic-gate 	}
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	if (s_mp->sbm_dyn_segs != NULL && s_mp->sbm_cm.sbdev_error == 0) {
14197c478bd9Sstevel@tonic-gate 		memlist_delete(s_mp->sbm_dyn_segs);
14207c478bd9Sstevel@tonic-gate 		s_mp->sbm_dyn_segs = NULL;
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	s_mp->sbm_del_mlist = NULL;
14247c478bd9Sstevel@tonic-gate 	s_mp->sbm_peer = NULL;
14257c478bd9Sstevel@tonic-gate 	s_mp->sbm_flags = 0;
14267c478bd9Sstevel@tonic-gate 	s_mp->sbm_cm.sbdev_busy = 0;
14277c478bd9Sstevel@tonic-gate 	dr_init_mem_unit_data(s_mp);
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	PR_MEM("%s: cached memlist for %s:", f, s_mp->sbm_cm.sbdev_path);
14307c478bd9Sstevel@tonic-gate 	PR_MEMLIST_DUMP(s_mp->sbm_mlist);
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	return (0);
14337c478bd9Sstevel@tonic-gate }
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate /*
14367c478bd9Sstevel@tonic-gate  * Successful return from this function will have the memory
14377c478bd9Sstevel@tonic-gate  * handle in bp->b_dev[..mem-unit...].sbm_memhandle allocated
14387c478bd9Sstevel@tonic-gate  * and waiting.  This routine's job is to select the memory that
14397c478bd9Sstevel@tonic-gate  * actually has to be released (detached) which may not necessarily
14407c478bd9Sstevel@tonic-gate  * be the same memory node that came in in devlist[],
14417c478bd9Sstevel@tonic-gate  * i.e. a copy-rename is needed.
14427c478bd9Sstevel@tonic-gate  */
14437c478bd9Sstevel@tonic-gate int
dr_pre_release_mem(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)14447c478bd9Sstevel@tonic-gate dr_pre_release_mem(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
14457c478bd9Sstevel@tonic-gate {
14467c478bd9Sstevel@tonic-gate 	int		d;
14477c478bd9Sstevel@tonic-gate 	int		err_flag = 0;
14487c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_pre_release_mem";
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	for (d = 0; d < devnum; d++) {
14537c478bd9Sstevel@tonic-gate 		dr_mem_unit_t	*mp = (dr_mem_unit_t *)devlist[d];
14547c478bd9Sstevel@tonic-gate 		int		rv;
14557c478bd9Sstevel@tonic-gate 		memquery_t	mq;
14567c478bd9Sstevel@tonic-gate 		struct memlist	*ml;
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 		if (mp->sbm_cm.sbdev_error) {
14597c478bd9Sstevel@tonic-gate 			err_flag = 1;
14607c478bd9Sstevel@tonic-gate 			continue;
14617c478bd9Sstevel@tonic-gate 		} else if (!kcage_on) {
14627c478bd9Sstevel@tonic-gate 			dr_dev_err(CE_WARN, &mp->sbm_cm, ESBD_KCAGE_OFF);
14637c478bd9Sstevel@tonic-gate 			err_flag = 1;
14647c478bd9Sstevel@tonic-gate 			continue;
14657c478bd9Sstevel@tonic-gate 		}
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 		if (mp->sbm_flags & DR_MFLAG_RESERVED) {
14687c478bd9Sstevel@tonic-gate 			/*
14697c478bd9Sstevel@tonic-gate 			 * Board is currently involved in a delete
14707c478bd9Sstevel@tonic-gate 			 * memory operation. Can't detach this guy until
14717c478bd9Sstevel@tonic-gate 			 * that operation completes.
14727c478bd9Sstevel@tonic-gate 			 */
14737c478bd9Sstevel@tonic-gate 			dr_dev_err(CE_WARN, &mp->sbm_cm, ESBD_INVAL);
14747c478bd9Sstevel@tonic-gate 			err_flag = 1;
14757c478bd9Sstevel@tonic-gate 			break;
14767c478bd9Sstevel@tonic-gate 		}
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 		/*
14797c478bd9Sstevel@tonic-gate 		 * Check whether the detaching memory requires a
14807c478bd9Sstevel@tonic-gate 		 * copy-rename.
14817c478bd9Sstevel@tonic-gate 		 */
14827c478bd9Sstevel@tonic-gate 		ASSERT(mp->sbm_npages != 0);
1483d3d50737SRafael Vanoni 		rv = kphysm_del_span_query(mp->sbm_basepfn, mp->sbm_npages,
1484d3d50737SRafael Vanoni 		    &mq);
14857c478bd9Sstevel@tonic-gate 		if (rv != KPHYSM_OK) {
14867c478bd9Sstevel@tonic-gate 			DR_DEV_INTERNAL_ERROR(&mp->sbm_cm);
14877c478bd9Sstevel@tonic-gate 			err_flag = 1;
14887c478bd9Sstevel@tonic-gate 			break;
14897c478bd9Sstevel@tonic-gate 		}
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 		if (mq.nonrelocatable != 0) {
14927c478bd9Sstevel@tonic-gate 			if (!(dr_cmd_flags(hp) &
1493d3d50737SRafael Vanoni 			    (SBD_FLAG_FORCE | SBD_FLAG_QUIESCE_OKAY))) {
14947c478bd9Sstevel@tonic-gate 				/* caller wasn't prompted for a suspend */
14957c478bd9Sstevel@tonic-gate 				dr_dev_err(CE_WARN, &mp->sbm_cm,
1496d3d50737SRafael Vanoni 				    ESBD_QUIESCE_REQD);
14977c478bd9Sstevel@tonic-gate 				err_flag = 1;
14987c478bd9Sstevel@tonic-gate 				break;
14997c478bd9Sstevel@tonic-gate 			}
15007c478bd9Sstevel@tonic-gate 		}
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 		/* flags should be clean at this time */
15037c478bd9Sstevel@tonic-gate 		ASSERT(mp->sbm_flags == 0);
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 		ASSERT(mp->sbm_mlist == NULL);		/* should be null */
15067c478bd9Sstevel@tonic-gate 		ASSERT(mp->sbm_del_mlist == NULL);	/* should be null */
15077c478bd9Sstevel@tonic-gate 		if (mp->sbm_mlist != NULL) {
15087c478bd9Sstevel@tonic-gate 			memlist_delete(mp->sbm_mlist);
15097c478bd9Sstevel@tonic-gate 			mp->sbm_mlist = NULL;
15107c478bd9Sstevel@tonic-gate 		}
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 		ml = dr_get_memlist(mp);
15137c478bd9Sstevel@tonic-gate 		if (ml == NULL) {
15147c478bd9Sstevel@tonic-gate 			err_flag = 1;
15157c478bd9Sstevel@tonic-gate 			PR_MEM("%s: no memlist found for %s\n",
1516d3d50737SRafael Vanoni 			    f, mp->sbm_cm.sbdev_path);
15177c478bd9Sstevel@tonic-gate 			continue;
15187c478bd9Sstevel@tonic-gate 		}
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 		/* allocate a kphysm handle */
15217c478bd9Sstevel@tonic-gate 		rv = kphysm_del_gethandle(&mp->sbm_memhandle);
15227c478bd9Sstevel@tonic-gate 		if (rv != KPHYSM_OK) {
15237c478bd9Sstevel@tonic-gate 			memlist_delete(ml);
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 			DR_DEV_INTERNAL_ERROR(&mp->sbm_cm);
15267c478bd9Sstevel@tonic-gate 			err_flag = 1;
15277c478bd9Sstevel@tonic-gate 			break;
15287c478bd9Sstevel@tonic-gate 		}
15297c478bd9Sstevel@tonic-gate 		mp->sbm_flags |= DR_MFLAG_RELOWNER;
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 		if ((mq.nonrelocatable != 0) ||
1532d3d50737SRafael Vanoni 		    dr_reserve_mem_spans(&mp->sbm_memhandle, ml)) {
15337c478bd9Sstevel@tonic-gate 			/*
15347c478bd9Sstevel@tonic-gate 			 * Either the detaching memory node contains
15357c478bd9Sstevel@tonic-gate 			 * non-reloc memory or we failed to reserve the
15367c478bd9Sstevel@tonic-gate 			 * detaching memory node (which did _not_ have
15377c478bd9Sstevel@tonic-gate 			 * any non-reloc memory, i.e. some non-reloc mem
15387c478bd9Sstevel@tonic-gate 			 * got onboard).
15397c478bd9Sstevel@tonic-gate 			 */
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 			if (dr_select_mem_target(hp, mp, ml)) {
15427c478bd9Sstevel@tonic-gate 				int rv;
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 				/*
15457c478bd9Sstevel@tonic-gate 				 * We had no luck locating a target
15467c478bd9Sstevel@tonic-gate 				 * memory node to be the recipient of
15477c478bd9Sstevel@tonic-gate 				 * the non-reloc memory on the node
15487c478bd9Sstevel@tonic-gate 				 * we're trying to detach.
15497c478bd9Sstevel@tonic-gate 				 * Clean up be disposing the mem handle
15507c478bd9Sstevel@tonic-gate 				 * and the mem list.
15517c478bd9Sstevel@tonic-gate 				 */
15527c478bd9Sstevel@tonic-gate 				rv = kphysm_del_release(mp->sbm_memhandle);
15537c478bd9Sstevel@tonic-gate 				if (rv != KPHYSM_OK) {
15547c478bd9Sstevel@tonic-gate 					/*
15557c478bd9Sstevel@tonic-gate 					 * can do nothing but complain
15567c478bd9Sstevel@tonic-gate 					 * and hope helpful for debug
15577c478bd9Sstevel@tonic-gate 					 */
15587c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "%s: unexpected"
1559d3d50737SRafael Vanoni 					    " kphysm_del_release return"
1560d3d50737SRafael Vanoni 					    " value %d",
1561d3d50737SRafael Vanoni 					    f, rv);
15627c478bd9Sstevel@tonic-gate 				}
15637c478bd9Sstevel@tonic-gate 				mp->sbm_flags &= ~DR_MFLAG_RELOWNER;
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 				memlist_delete(ml);
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 				/* make sure sbm_flags is clean */
15687c478bd9Sstevel@tonic-gate 				ASSERT(mp->sbm_flags == 0);
15697c478bd9Sstevel@tonic-gate 
1570d3d50737SRafael Vanoni 				dr_dev_err(CE_WARN, &mp->sbm_cm,
1571d3d50737SRafael Vanoni 				    ESBD_NO_TARGET);
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 				err_flag = 1;
15747c478bd9Sstevel@tonic-gate 				break;
15757c478bd9Sstevel@tonic-gate 			}
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 			/*
15787c478bd9Sstevel@tonic-gate 			 * ml is not memlist_delete'd here because
15797c478bd9Sstevel@tonic-gate 			 * it has been assigned to mp->sbm_mlist
15807c478bd9Sstevel@tonic-gate 			 * by dr_select_mem_target.
15817c478bd9Sstevel@tonic-gate 			 */
15827c478bd9Sstevel@tonic-gate 		} else {
15837c478bd9Sstevel@tonic-gate 			/* no target needed to detach this board */
15847c478bd9Sstevel@tonic-gate 			mp->sbm_flags |= DR_MFLAG_RESERVED;
15857c478bd9Sstevel@tonic-gate 			mp->sbm_peer = NULL;
15867c478bd9Sstevel@tonic-gate 			mp->sbm_del_mlist = ml;
15877c478bd9Sstevel@tonic-gate 			mp->sbm_mlist = ml;
15887c478bd9Sstevel@tonic-gate 			mp->sbm_cm.sbdev_busy = 1;
15897c478bd9Sstevel@tonic-gate 		}
15907c478bd9Sstevel@tonic-gate #ifdef DEBUG
15917c478bd9Sstevel@tonic-gate 		ASSERT(mp->sbm_mlist != NULL);
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 		if (mp->sbm_flags & DR_MFLAG_SOURCE) {
15947c478bd9Sstevel@tonic-gate 			PR_MEM("%s: release of %s requires copy/rename;"
1595d3d50737SRafael Vanoni 			    " selected target board %s\n",
1596d3d50737SRafael Vanoni 			    f,
1597d3d50737SRafael Vanoni 			    mp->sbm_cm.sbdev_path,
1598d3d50737SRafael Vanoni 			    mp->sbm_peer->sbm_cm.sbdev_path);
15997c478bd9Sstevel@tonic-gate 		} else {
16007c478bd9Sstevel@tonic-gate 			PR_MEM("%s: copy/rename not required to release %s\n",
1601d3d50737SRafael Vanoni 			    f, mp->sbm_cm.sbdev_path);
16027c478bd9Sstevel@tonic-gate 		}
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 		ASSERT(mp->sbm_flags & DR_MFLAG_RELOWNER);
16057c478bd9Sstevel@tonic-gate 		ASSERT(mp->sbm_flags & DR_MFLAG_RESERVED);
16067c478bd9Sstevel@tonic-gate #endif
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	return (err_flag ? -1 : 0);
16107c478bd9Sstevel@tonic-gate }
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate void
dr_release_mem_done(dr_common_unit_t * cp)16137c478bd9Sstevel@tonic-gate dr_release_mem_done(dr_common_unit_t *cp)
16147c478bd9Sstevel@tonic-gate {
16157c478bd9Sstevel@tonic-gate 	dr_mem_unit_t	*s_mp = (dr_mem_unit_t *)cp;
16167c478bd9Sstevel@tonic-gate 	dr_mem_unit_t *t_mp, *mp;
16177c478bd9Sstevel@tonic-gate 	int		rv;
16187c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_release_mem_done";
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	/*
16217c478bd9Sstevel@tonic-gate 	 * This unit will be flagged with DR_MFLAG_SOURCE, if it
16227c478bd9Sstevel@tonic-gate 	 * has a target unit.
16237c478bd9Sstevel@tonic-gate 	 */
16247c478bd9Sstevel@tonic-gate 	if (s_mp->sbm_flags & DR_MFLAG_SOURCE) {
16257c478bd9Sstevel@tonic-gate 		t_mp = s_mp->sbm_peer;
16267c478bd9Sstevel@tonic-gate 		ASSERT(t_mp != NULL);
16277c478bd9Sstevel@tonic-gate 		ASSERT(t_mp->sbm_peer == s_mp);
16287c478bd9Sstevel@tonic-gate 		ASSERT(t_mp->sbm_flags & DR_MFLAG_TARGET);
16297c478bd9Sstevel@tonic-gate 		ASSERT(t_mp->sbm_flags & DR_MFLAG_RESERVED);
16307c478bd9Sstevel@tonic-gate 	} else {
16317c478bd9Sstevel@tonic-gate 		/* this is no target unit */
16327c478bd9Sstevel@tonic-gate 		t_mp = NULL;
16337c478bd9Sstevel@tonic-gate 	}
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	/* free delete handle */
16367c478bd9Sstevel@tonic-gate 	ASSERT(s_mp->sbm_flags & DR_MFLAG_RELOWNER);
16377c478bd9Sstevel@tonic-gate 	ASSERT(s_mp->sbm_flags & DR_MFLAG_RESERVED);
16387c478bd9Sstevel@tonic-gate 	rv = kphysm_del_release(s_mp->sbm_memhandle);
16397c478bd9Sstevel@tonic-gate 	if (rv != KPHYSM_OK) {
16407c478bd9Sstevel@tonic-gate 		/*
16417c478bd9Sstevel@tonic-gate 		 * can do nothing but complain
16427c478bd9Sstevel@tonic-gate 		 * and hope helpful for debug
16437c478bd9Sstevel@tonic-gate 		 */
16447c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: unexpected kphysm_del_release"
1645d3d50737SRafael Vanoni 		    " return value %d", f, rv);
16467c478bd9Sstevel@tonic-gate 	}
16477c478bd9Sstevel@tonic-gate 	s_mp->sbm_flags &= ~DR_MFLAG_RELOWNER;
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	/*
16507c478bd9Sstevel@tonic-gate 	 * If an error was encountered during release, clean up
16517c478bd9Sstevel@tonic-gate 	 * the source (and target, if present) unit data.
16527c478bd9Sstevel@tonic-gate 	 */
16537c478bd9Sstevel@tonic-gate /* XXX Can we know that sbdev_error was encountered during release? */
16547c478bd9Sstevel@tonic-gate 	if (s_mp->sbm_cm.sbdev_error != NULL) {
16557c478bd9Sstevel@tonic-gate 		PR_MEM("%s: %s: error %d noted\n",
1656d3d50737SRafael Vanoni 		    f,
1657d3d50737SRafael Vanoni 		    s_mp->sbm_cm.sbdev_path,
1658d3d50737SRafael Vanoni 		    s_mp->sbm_cm.sbdev_error->e_code);
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 		if (t_mp != NULL) {
16617c478bd9Sstevel@tonic-gate 			ASSERT(t_mp->sbm_del_mlist == t_mp->sbm_mlist);
16627c478bd9Sstevel@tonic-gate 			t_mp->sbm_del_mlist = NULL;
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 			if (t_mp->sbm_mlist != NULL) {
16657c478bd9Sstevel@tonic-gate 				memlist_delete(t_mp->sbm_mlist);
16667c478bd9Sstevel@tonic-gate 				t_mp->sbm_mlist = NULL;
16677c478bd9Sstevel@tonic-gate 			}
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 			t_mp->sbm_peer = NULL;
16707c478bd9Sstevel@tonic-gate 			t_mp->sbm_flags = 0;
16717c478bd9Sstevel@tonic-gate 			t_mp->sbm_cm.sbdev_busy = 0;
16727c478bd9Sstevel@tonic-gate 		}
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 		if (s_mp->sbm_del_mlist != s_mp->sbm_mlist)
16757c478bd9Sstevel@tonic-gate 			memlist_delete(s_mp->sbm_del_mlist);
16767c478bd9Sstevel@tonic-gate 		s_mp->sbm_del_mlist = NULL;
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 		if (s_mp->sbm_mlist != NULL) {
16797c478bd9Sstevel@tonic-gate 			memlist_delete(s_mp->sbm_mlist);
16807c478bd9Sstevel@tonic-gate 			s_mp->sbm_mlist = NULL;
16817c478bd9Sstevel@tonic-gate 		}
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 		s_mp->sbm_peer = NULL;
16847c478bd9Sstevel@tonic-gate 		s_mp->sbm_flags = 0;
16857c478bd9Sstevel@tonic-gate 		s_mp->sbm_cm.sbdev_busy = 0;
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 		/* bail out */
16887c478bd9Sstevel@tonic-gate 		return;
16897c478bd9Sstevel@tonic-gate 	}
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 	DR_DEV_SET_RELEASED(&s_mp->sbm_cm);
16927c478bd9Sstevel@tonic-gate 	dr_device_transition(&s_mp->sbm_cm, DR_STATE_RELEASE);
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	if (t_mp != NULL) {
16957c478bd9Sstevel@tonic-gate 		/*
16967c478bd9Sstevel@tonic-gate 		 * the kphysm delete operation that drained the source
16977c478bd9Sstevel@tonic-gate 		 * board also drained this target board.  Since the source
16987c478bd9Sstevel@tonic-gate 		 * board drain is now known to have succeeded, we know this
16997c478bd9Sstevel@tonic-gate 		 * target board is drained too.
17007c478bd9Sstevel@tonic-gate 		 *
17017c478bd9Sstevel@tonic-gate 		 * because DR_DEV_SET_RELEASED and dr_device_transition
17027c478bd9Sstevel@tonic-gate 		 * is done here, the dr_release_dev_done should not
17037c478bd9Sstevel@tonic-gate 		 * fail.
17047c478bd9Sstevel@tonic-gate 		 */
17057c478bd9Sstevel@tonic-gate 		DR_DEV_SET_RELEASED(&t_mp->sbm_cm);
17067c478bd9Sstevel@tonic-gate 		dr_device_transition(&t_mp->sbm_cm, DR_STATE_RELEASE);
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 		/*
17097c478bd9Sstevel@tonic-gate 		 * NOTE: do not transition target's board state,
17107c478bd9Sstevel@tonic-gate 		 * even if the mem-unit was the last configure
17117c478bd9Sstevel@tonic-gate 		 * unit of the board.  When copy/rename completes
17127c478bd9Sstevel@tonic-gate 		 * this mem-unit will transitioned back to
17137c478bd9Sstevel@tonic-gate 		 * the configured state.  In the meantime, the
17147c478bd9Sstevel@tonic-gate 		 * board's must remain as is.
17157c478bd9Sstevel@tonic-gate 		 */
17167c478bd9Sstevel@tonic-gate 	}
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 	/* if board(s) had deleted memory, verify it is gone */
17197c478bd9Sstevel@tonic-gate 	rv = 0;
17207c478bd9Sstevel@tonic-gate 	memlist_read_lock();
17217c478bd9Sstevel@tonic-gate 	if (s_mp->sbm_del_mlist != NULL) {
17227c478bd9Sstevel@tonic-gate 		mp = s_mp;
17237c478bd9Sstevel@tonic-gate 		rv = memlist_intersect(phys_install, mp->sbm_del_mlist);
17247c478bd9Sstevel@tonic-gate 	}
17257c478bd9Sstevel@tonic-gate 	if (rv == 0 && t_mp && t_mp->sbm_del_mlist != NULL) {
17267c478bd9Sstevel@tonic-gate 		mp = t_mp;
17277c478bd9Sstevel@tonic-gate 		rv = memlist_intersect(phys_install, mp->sbm_del_mlist);
17287c478bd9Sstevel@tonic-gate 	}
17297c478bd9Sstevel@tonic-gate 	memlist_read_unlock();
17307c478bd9Sstevel@tonic-gate 	if (rv) {
17317c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: %smem-unit (%d.%d): "
1732d3d50737SRafael Vanoni 		    "deleted memory still found in phys_install",
1733d3d50737SRafael Vanoni 		    f,
1734d3d50737SRafael Vanoni 		    (mp == t_mp ? "target " : ""),
1735d3d50737SRafael Vanoni 		    mp->sbm_cm.sbdev_bp->b_num,
1736d3d50737SRafael Vanoni 		    mp->sbm_cm.sbdev_unum);
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 		DR_DEV_INTERNAL_ERROR(&s_mp->sbm_cm);
17397c478bd9Sstevel@tonic-gate 		return;
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	s_mp->sbm_flags |= DR_MFLAG_RELDONE;
17437c478bd9Sstevel@tonic-gate 	if (t_mp != NULL)
17447c478bd9Sstevel@tonic-gate 		t_mp->sbm_flags |= DR_MFLAG_RELDONE;
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	/* this should not fail */
17477c478bd9Sstevel@tonic-gate 	if (dr_release_dev_done(&s_mp->sbm_cm) != 0) {
17487c478bd9Sstevel@tonic-gate 		/* catch this in debug kernels */
17497c478bd9Sstevel@tonic-gate 		ASSERT(0);
17507c478bd9Sstevel@tonic-gate 		return;
17517c478bd9Sstevel@tonic-gate 	}
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	PR_MEM("%s: marking %s release DONE\n",
1754d3d50737SRafael Vanoni 	    f, s_mp->sbm_cm.sbdev_path);
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	s_mp->sbm_cm.sbdev_ostate = SBD_STAT_UNCONFIGURED;
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	if (t_mp != NULL) {
17597c478bd9Sstevel@tonic-gate 		/* should not fail */
17607c478bd9Sstevel@tonic-gate 		rv = dr_release_dev_done(&t_mp->sbm_cm);
17617c478bd9Sstevel@tonic-gate 		if (rv != 0) {
17627c478bd9Sstevel@tonic-gate 			/* catch this in debug kernels */
17637c478bd9Sstevel@tonic-gate 			ASSERT(0);
17647c478bd9Sstevel@tonic-gate 			return;
17657c478bd9Sstevel@tonic-gate 		}
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 		PR_MEM("%s: marking %s release DONE\n",
1768d3d50737SRafael Vanoni 		    f, t_mp->sbm_cm.sbdev_path);
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 		t_mp->sbm_cm.sbdev_ostate = SBD_STAT_UNCONFIGURED;
17717c478bd9Sstevel@tonic-gate 	}
17727c478bd9Sstevel@tonic-gate }
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17757c478bd9Sstevel@tonic-gate int
dr_disconnect_mem(dr_mem_unit_t * mp)17767c478bd9Sstevel@tonic-gate dr_disconnect_mem(dr_mem_unit_t *mp)
17777c478bd9Sstevel@tonic-gate {
17787c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_disconnect_mem";
17797c478bd9Sstevel@tonic-gate 	update_membounds_t umb;
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate #ifdef DEBUG
17827c478bd9Sstevel@tonic-gate 	int state = mp->sbm_cm.sbdev_state;
1783d3d50737SRafael Vanoni 	ASSERT(state == DR_STATE_CONNECTED || state == DR_STATE_UNCONFIGURED);
17847c478bd9Sstevel@tonic-gate #endif
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	if (mp->sbm_del_mlist && mp->sbm_del_mlist != mp->sbm_mlist)
17897c478bd9Sstevel@tonic-gate 		memlist_delete(mp->sbm_del_mlist);
17907c478bd9Sstevel@tonic-gate 	mp->sbm_del_mlist = NULL;
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	if (mp->sbm_mlist) {
17937c478bd9Sstevel@tonic-gate 		memlist_delete(mp->sbm_mlist);
17947c478bd9Sstevel@tonic-gate 		mp->sbm_mlist = NULL;
17957c478bd9Sstevel@tonic-gate 	}
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	/*
17987c478bd9Sstevel@tonic-gate 	 * Remove memory from lgroup
17997c478bd9Sstevel@tonic-gate 	 * For now, only board info is required.
18007c478bd9Sstevel@tonic-gate 	 */
18017c478bd9Sstevel@tonic-gate 	umb.u_board = mp->sbm_cm.sbdev_bp->b_num;
18027c478bd9Sstevel@tonic-gate 	umb.u_base = (uint64_t)-1;
18037c478bd9Sstevel@tonic-gate 	umb.u_len = (uint64_t)-1;
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	lgrp_plat_config(LGRP_CONFIG_MEM_DEL, (uintptr_t)&umb);
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	return (0);
18087c478bd9Sstevel@tonic-gate }
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate int
dr_cancel_mem(dr_mem_unit_t * s_mp)18117c478bd9Sstevel@tonic-gate dr_cancel_mem(dr_mem_unit_t *s_mp)
18127c478bd9Sstevel@tonic-gate {
18137c478bd9Sstevel@tonic-gate 	dr_mem_unit_t	*t_mp;
18147c478bd9Sstevel@tonic-gate 	dr_state_t	state;
18157c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_cancel_mem";
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	state = s_mp->sbm_cm.sbdev_state;
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	if (s_mp->sbm_flags & DR_MFLAG_TARGET) {
18207c478bd9Sstevel@tonic-gate 		/* must cancel source board, not target board */
18217c478bd9Sstevel@tonic-gate 		/* TODO: set error */
18227c478bd9Sstevel@tonic-gate 		return (-1);
18237c478bd9Sstevel@tonic-gate 	} else if (s_mp->sbm_flags & DR_MFLAG_SOURCE) {
18247c478bd9Sstevel@tonic-gate 		t_mp = s_mp->sbm_peer;
18257c478bd9Sstevel@tonic-gate 		ASSERT(t_mp != NULL);
18267c478bd9Sstevel@tonic-gate 		ASSERT(t_mp->sbm_peer == s_mp);
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 		/* must always match the source board's state */
18297c478bd9Sstevel@tonic-gate /* TODO: is this assertion correct? */
18307c478bd9Sstevel@tonic-gate 		ASSERT(t_mp->sbm_cm.sbdev_state == state);
18317c478bd9Sstevel@tonic-gate 	} else {
18327c478bd9Sstevel@tonic-gate 		/* this is no target unit */
18337c478bd9Sstevel@tonic-gate 		t_mp = NULL;
18347c478bd9Sstevel@tonic-gate 	}
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 	switch (state) {
18377c478bd9Sstevel@tonic-gate 	case DR_STATE_UNREFERENCED:	/* state set by dr_release_dev_done */
18387c478bd9Sstevel@tonic-gate 		ASSERT((s_mp->sbm_flags & DR_MFLAG_RELOWNER) == 0);
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 		if (t_mp != NULL && t_mp->sbm_del_mlist != NULL) {
18417c478bd9Sstevel@tonic-gate 			PR_MEM("%s: undoing target %s memory delete\n",
1842d3d50737SRafael Vanoni 			    f, t_mp->sbm_cm.sbdev_path);
18437c478bd9Sstevel@tonic-gate 			dr_add_memory_spans(t_mp, t_mp->sbm_del_mlist);
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 			DR_DEV_CLR_UNREFERENCED(&t_mp->sbm_cm);
18467c478bd9Sstevel@tonic-gate 		}
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 		if (s_mp->sbm_del_mlist != NULL) {
18497c478bd9Sstevel@tonic-gate 			PR_MEM("%s: undoing %s memory delete\n",
1850d3d50737SRafael Vanoni 			    f, s_mp->sbm_cm.sbdev_path);
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 			dr_add_memory_spans(s_mp, s_mp->sbm_del_mlist);
18537c478bd9Sstevel@tonic-gate 		}
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate /* TODO: should no longer be possible to see the release state here */
18587c478bd9Sstevel@tonic-gate 	case DR_STATE_RELEASE:	/* state set by dr_release_mem_done */
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 		ASSERT((s_mp->sbm_flags & DR_MFLAG_RELOWNER) == 0);
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 		if (t_mp != NULL) {
18637c478bd9Sstevel@tonic-gate 			ASSERT(t_mp->sbm_del_mlist == t_mp->sbm_mlist);
18647c478bd9Sstevel@tonic-gate 			t_mp->sbm_del_mlist = NULL;
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 			if (t_mp->sbm_mlist != NULL) {
18677c478bd9Sstevel@tonic-gate 				memlist_delete(t_mp->sbm_mlist);
18687c478bd9Sstevel@tonic-gate 				t_mp->sbm_mlist = NULL;
18697c478bd9Sstevel@tonic-gate 			}
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 			t_mp->sbm_peer = NULL;
18727c478bd9Sstevel@tonic-gate 			t_mp->sbm_flags = 0;
18737c478bd9Sstevel@tonic-gate 			t_mp->sbm_cm.sbdev_busy = 0;
18747c478bd9Sstevel@tonic-gate 			dr_init_mem_unit_data(t_mp);
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 			DR_DEV_CLR_RELEASED(&t_mp->sbm_cm);
18777c478bd9Sstevel@tonic-gate 
1878d3d50737SRafael Vanoni 			dr_device_transition(&t_mp->sbm_cm,
1879d3d50737SRafael Vanoni 			    DR_STATE_CONFIGURED);
18807c478bd9Sstevel@tonic-gate 		}
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 		if (s_mp->sbm_del_mlist != s_mp->sbm_mlist)
18837c478bd9Sstevel@tonic-gate 			memlist_delete(s_mp->sbm_del_mlist);
18847c478bd9Sstevel@tonic-gate 		s_mp->sbm_del_mlist = NULL;
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 		if (s_mp->sbm_mlist != NULL) {
18877c478bd9Sstevel@tonic-gate 			memlist_delete(s_mp->sbm_mlist);
18887c478bd9Sstevel@tonic-gate 			s_mp->sbm_mlist = NULL;
18897c478bd9Sstevel@tonic-gate 		}
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 		s_mp->sbm_peer = NULL;
18927c478bd9Sstevel@tonic-gate 		s_mp->sbm_flags = 0;
18937c478bd9Sstevel@tonic-gate 		s_mp->sbm_cm.sbdev_busy = 0;
18947c478bd9Sstevel@tonic-gate 		dr_init_mem_unit_data(s_mp);
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 		return (0);
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 	default:
18997c478bd9Sstevel@tonic-gate 		PR_MEM("%s: WARNING unexpected state (%d) for %s\n",
1900d3d50737SRafael Vanoni 		    f, (int)state, s_mp->sbm_cm.sbdev_path);
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate 		return (-1);
19037c478bd9Sstevel@tonic-gate 	}
19047c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
19057c478bd9Sstevel@tonic-gate }
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate void
dr_init_mem_unit(dr_mem_unit_t * mp)19087c478bd9Sstevel@tonic-gate dr_init_mem_unit(dr_mem_unit_t *mp)
19097c478bd9Sstevel@tonic-gate {
19107c478bd9Sstevel@tonic-gate 	dr_state_t	new_state;
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 	if (DR_DEV_IS_ATTACHED(&mp->sbm_cm)) {
19147c478bd9Sstevel@tonic-gate 		new_state = DR_STATE_CONFIGURED;
19157c478bd9Sstevel@tonic-gate 		mp->sbm_cm.sbdev_cond = SBD_COND_OK;
19167c478bd9Sstevel@tonic-gate 	} else if (DR_DEV_IS_PRESENT(&mp->sbm_cm)) {
19177c478bd9Sstevel@tonic-gate 		new_state = DR_STATE_CONNECTED;
19187c478bd9Sstevel@tonic-gate 		mp->sbm_cm.sbdev_cond = SBD_COND_OK;
19197c478bd9Sstevel@tonic-gate 	} else if (mp->sbm_cm.sbdev_id != (drmachid_t)0) {
19207c478bd9Sstevel@tonic-gate 		new_state = DR_STATE_OCCUPIED;
19217c478bd9Sstevel@tonic-gate 	} else {
19227c478bd9Sstevel@tonic-gate 		new_state = DR_STATE_EMPTY;
19237c478bd9Sstevel@tonic-gate 	}
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	if (DR_DEV_IS_PRESENT(&mp->sbm_cm))
19267c478bd9Sstevel@tonic-gate 		dr_init_mem_unit_data(mp);
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 	/* delay transition until fully initialized */
19297c478bd9Sstevel@tonic-gate 	dr_device_transition(&mp->sbm_cm, new_state);
19307c478bd9Sstevel@tonic-gate }
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate static void
dr_init_mem_unit_data(dr_mem_unit_t * mp)19337c478bd9Sstevel@tonic-gate dr_init_mem_unit_data(dr_mem_unit_t *mp)
19347c478bd9Sstevel@tonic-gate {
19357c478bd9Sstevel@tonic-gate 	drmachid_t	id = mp->sbm_cm.sbdev_id;
19367c478bd9Sstevel@tonic-gate 	uint64_t	bytes;
19377c478bd9Sstevel@tonic-gate 	sbd_error_t	*err;
19387c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_init_mem_unit_data";
19397c478bd9Sstevel@tonic-gate 	update_membounds_t umb;
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	/* a little sanity checking */
19447c478bd9Sstevel@tonic-gate 	ASSERT(mp->sbm_peer == NULL);
19457c478bd9Sstevel@tonic-gate 	ASSERT(mp->sbm_flags == 0);
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 	/* get basepfn of mem unit */
19487c478bd9Sstevel@tonic-gate 	err = drmach_mem_get_base_physaddr(id, &bytes);
19497c478bd9Sstevel@tonic-gate 	if (err) {
19507c478bd9Sstevel@tonic-gate 		DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
19517c478bd9Sstevel@tonic-gate 		mp->sbm_basepfn = (pfn_t)-1;
19527c478bd9Sstevel@tonic-gate 	} else
19537c478bd9Sstevel@tonic-gate 		mp->sbm_basepfn = _b64top(bytes);
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	/* attempt to get number of pages from PDA */
19567c478bd9Sstevel@tonic-gate 	err = drmach_mem_get_size(id, &bytes);
19577c478bd9Sstevel@tonic-gate 	if (err) {
19587c478bd9Sstevel@tonic-gate 		DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
19597c478bd9Sstevel@tonic-gate 		mp->sbm_npages = 0;
19607c478bd9Sstevel@tonic-gate 	} else
19617c478bd9Sstevel@tonic-gate 		mp->sbm_npages = _b64top(bytes);
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 	/* if didn't work, calculate using memlist */
19647c478bd9Sstevel@tonic-gate 	if (mp->sbm_npages == 0) {
19657c478bd9Sstevel@tonic-gate 		struct memlist	*ml, *mlist;
19667c478bd9Sstevel@tonic-gate 		/*
19677c478bd9Sstevel@tonic-gate 		 * Either we couldn't open the PDA or our
19687c478bd9Sstevel@tonic-gate 		 * PDA has garbage in it.  We must have the
19697c478bd9Sstevel@tonic-gate 		 * page count consistent and whatever the
19707c478bd9Sstevel@tonic-gate 		 * OS states has precedence over the PDA
19717c478bd9Sstevel@tonic-gate 		 * so let's check the kernel.
19727c478bd9Sstevel@tonic-gate 		 */
19737c478bd9Sstevel@tonic-gate /* TODO: curious comment. it suggests pda query should happen if this fails */
19747c478bd9Sstevel@tonic-gate 		PR_MEM("%s: PDA query failed for npages."
1975d3d50737SRafael Vanoni 		    " Checking memlist for %s\n",
1976d3d50737SRafael Vanoni 		    f, mp->sbm_cm.sbdev_path);
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 		mlist = dr_get_memlist(mp);
197956f33205SJonathan Adams 		for (ml = mlist; ml; ml = ml->ml_next)
198056f33205SJonathan Adams 			mp->sbm_npages += btop(ml->ml_size);
19817c478bd9Sstevel@tonic-gate 		memlist_delete(mlist);
19827c478bd9Sstevel@tonic-gate 	}
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	err = drmach_mem_get_alignment(id, &bytes);
19857c478bd9Sstevel@tonic-gate 	if (err) {
19867c478bd9Sstevel@tonic-gate 		DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
19877c478bd9Sstevel@tonic-gate 		mp->sbm_alignment_mask = 0;
19887c478bd9Sstevel@tonic-gate 	} else
19897c478bd9Sstevel@tonic-gate 		mp->sbm_alignment_mask = _b64top(bytes);
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 	err = drmach_mem_get_slice_size(id, &bytes);
19927c478bd9Sstevel@tonic-gate 	if (err) {
19937c478bd9Sstevel@tonic-gate 		DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err);
19947c478bd9Sstevel@tonic-gate 		mp->sbm_slice_size = 0; /* paranoia */
19957c478bd9Sstevel@tonic-gate 	} else
19967c478bd9Sstevel@tonic-gate 		mp->sbm_slice_size = bytes;
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 	/*
19997c478bd9Sstevel@tonic-gate 	 * Add memory to lgroup
20007c478bd9Sstevel@tonic-gate 	 */
20017c478bd9Sstevel@tonic-gate 	umb.u_board = mp->sbm_cm.sbdev_bp->b_num;
20027c478bd9Sstevel@tonic-gate 	umb.u_base = (uint64_t)mp->sbm_basepfn << MMU_PAGESHIFT;
20037c478bd9Sstevel@tonic-gate 	umb.u_len = (uint64_t)mp->sbm_npages << MMU_PAGESHIFT;
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	lgrp_plat_config(LGRP_CONFIG_MEM_ADD, (uintptr_t)&umb);
20067c478bd9Sstevel@tonic-gate 
200704580fdfSmathue 	PR_MEM("%s: %s (basepfn = 0x%lx, npgs = %ld)\n",
2008d3d50737SRafael Vanoni 	    f, mp->sbm_cm.sbdev_path, mp->sbm_basepfn, mp->sbm_npages);
20097c478bd9Sstevel@tonic-gate }
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate static int
dr_reserve_mem_spans(memhandle_t * mhp,struct memlist * ml)20127c478bd9Sstevel@tonic-gate dr_reserve_mem_spans(memhandle_t *mhp, struct memlist *ml)
20137c478bd9Sstevel@tonic-gate {
20147c478bd9Sstevel@tonic-gate 	int		err;
20157c478bd9Sstevel@tonic-gate 	pfn_t		base;
20167c478bd9Sstevel@tonic-gate 	pgcnt_t		npgs;
20177c478bd9Sstevel@tonic-gate 	struct memlist	*mc;
20187c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_reserve_mem_spans";
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	/*
20237c478bd9Sstevel@tonic-gate 	 * Walk the supplied memlist scheduling each span for removal
20247c478bd9Sstevel@tonic-gate 	 * with kphysm_del_span.  It is possible that a span may intersect
20257c478bd9Sstevel@tonic-gate 	 * an area occupied by the cage.
20267c478bd9Sstevel@tonic-gate 	 */
202756f33205SJonathan Adams 	for (mc = ml; mc != NULL; mc = mc->ml_next) {
202856f33205SJonathan Adams 		base = _b64top(mc->ml_address);
202956f33205SJonathan Adams 		npgs = _b64top(mc->ml_size);
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 		err = kphysm_del_span(*mhp, base, npgs);
20327c478bd9Sstevel@tonic-gate 		if (err != KPHYSM_OK) {
20337c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s memory reserve failed."
2034d3d50737SRafael Vanoni 			    " unexpected kphysm_del_span return value %d;"
2035d3d50737SRafael Vanoni 			    " basepfn=0x%lx npages=%ld",
2036d3d50737SRafael Vanoni 			    f, err, base, npgs);
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 			return (-1);
20397c478bd9Sstevel@tonic-gate 		}
20407c478bd9Sstevel@tonic-gate 	}
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	return (0);
20437c478bd9Sstevel@tonic-gate }
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate /* debug counters */
20467c478bd9Sstevel@tonic-gate int dr_smt_realigned;
20477c478bd9Sstevel@tonic-gate int dr_smt_preference[4];
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate #ifdef DEBUG
20507c478bd9Sstevel@tonic-gate uint_t dr_ignore_board; /* if bit[bnum-1] set, board won't be candidate */
20517c478bd9Sstevel@tonic-gate #endif
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate /*
20547c478bd9Sstevel@tonic-gate  * Find and reserve a copy/rename target board suitable for the
20557c478bd9Sstevel@tonic-gate  * given source board.
20567c478bd9Sstevel@tonic-gate  * All boards in the system are examined and categorized in relation to
20577c478bd9Sstevel@tonic-gate  * their memory size versus the source board's memory size.  Order of
20587c478bd9Sstevel@tonic-gate  * preference is:
20597c478bd9Sstevel@tonic-gate  *	1st: board has same memory size
20607c478bd9Sstevel@tonic-gate  * 	2nd: board has larger memory size
20617c478bd9Sstevel@tonic-gate  *	3rd: board has smaller memory size
20627c478bd9Sstevel@tonic-gate  *	4th: board has smaller memory size, available memory will be reduced.
20637c478bd9Sstevel@tonic-gate  * Boards in category 3 and 4 will have their MC's reprogrammed to locate the
20647c478bd9Sstevel@tonic-gate  * span to which the MC responds to address span that appropriately covers
20657c478bd9Sstevel@tonic-gate  * the nonrelocatable span of the source board.
20667c478bd9Sstevel@tonic-gate  */
20677c478bd9Sstevel@tonic-gate static int
dr_select_mem_target(dr_handle_t * hp,dr_mem_unit_t * s_mp,struct memlist * s_ml)20687c478bd9Sstevel@tonic-gate dr_select_mem_target(dr_handle_t *hp,
20697c478bd9Sstevel@tonic-gate 	dr_mem_unit_t *s_mp, struct memlist *s_ml)
20707c478bd9Sstevel@tonic-gate {
20717c478bd9Sstevel@tonic-gate 	pgcnt_t		sz = _b64top(s_mp->sbm_slice_size);
20727c478bd9Sstevel@tonic-gate 	pgcnt_t		sm = sz - 1; /* mem_slice_mask */
20737c478bd9Sstevel@tonic-gate 	pfn_t		s_phi, t_phi;
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	int		n_sets = 4; /* same, larger, smaller, clipped */
20767c478bd9Sstevel@tonic-gate 	int		preference; /* lower value is higher preference */
20777c478bd9Sstevel@tonic-gate 	int		n_units_per_set;
20787c478bd9Sstevel@tonic-gate 	int		idx;
20797c478bd9Sstevel@tonic-gate 	dr_mem_unit_t	**sets;
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	int		t_bd;
20827c478bd9Sstevel@tonic-gate 	int		t_unit;
20837c478bd9Sstevel@tonic-gate 	int		rv;
20847c478bd9Sstevel@tonic-gate 	int		allow_src_memrange_modify;
20857c478bd9Sstevel@tonic-gate 	int		allow_targ_memrange_modify;
20867c478bd9Sstevel@tonic-gate 	drmachid_t	t_id;
20877c478bd9Sstevel@tonic-gate 	dr_board_t	*s_bp, *t_bp;
20887c478bd9Sstevel@tonic-gate 	dr_mem_unit_t	*t_mp, *c_mp;
20897c478bd9Sstevel@tonic-gate 	struct memlist	*d_ml, *t_ml, *x_ml;
20907c478bd9Sstevel@tonic-gate 	memquery_t	s_mq = {0};
20917c478bd9Sstevel@tonic-gate 	static fn_t	f = "dr_select_mem_target";
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	PR_MEM("%s...\n", f);
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 	ASSERT(s_ml != NULL);
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	n_units_per_set = MAX_BOARDS * MAX_MEM_UNITS_PER_BOARD;
20987c478bd9Sstevel@tonic-gate 	sets = GETSTRUCT(dr_mem_unit_t *, n_units_per_set * n_sets);
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	s_bp = hp->h_bd;
21017c478bd9Sstevel@tonic-gate 	/* calculate the offset into the slice of the last source board pfn */
21027c478bd9Sstevel@tonic-gate 	ASSERT(s_mp->sbm_npages != 0);
21037c478bd9Sstevel@tonic-gate 	s_phi = (s_mp->sbm_basepfn + s_mp->sbm_npages - 1) & sm;
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	allow_src_memrange_modify = drmach_allow_memrange_modify(s_bp->b_id);
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 	/*
21087c478bd9Sstevel@tonic-gate 	 * Make one pass through all memory units on all boards
21097c478bd9Sstevel@tonic-gate 	 * and categorize them with respect to the source board.
21107c478bd9Sstevel@tonic-gate 	 */
21117c478bd9Sstevel@tonic-gate 	for (t_bd = 0; t_bd < MAX_BOARDS; t_bd++) {
21127c478bd9Sstevel@tonic-gate 		/*
21137c478bd9Sstevel@tonic-gate 		 * The board structs are a contiguous array
21147c478bd9Sstevel@tonic-gate 		 * so we take advantage of that to find the
21157c478bd9Sstevel@tonic-gate 		 * correct board struct pointer for a given
21167c478bd9Sstevel@tonic-gate 		 * board number.
21177c478bd9Sstevel@tonic-gate 		 */
21187c478bd9Sstevel@tonic-gate 		t_bp = dr_lookup_board(t_bd);
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 		/* source board can not be its own target */
21217c478bd9Sstevel@tonic-gate 		if (s_bp->b_num == t_bp->b_num)
21227c478bd9Sstevel@tonic-gate 			continue;
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 		for (t_unit = 0; t_unit < MAX_MEM_UNITS_PER_BOARD; t_unit++) {
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 			t_mp = dr_get_mem_unit(t_bp, t_unit);
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 			/* this memory node must be attached */
21297c478bd9Sstevel@tonic-gate 			if (!DR_DEV_IS_ATTACHED(&t_mp->sbm_cm))
21307c478bd9Sstevel@tonic-gate 				continue;
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 			/* source unit can not be its own target */
21337c478bd9Sstevel@tonic-gate 			if (s_mp == t_mp) {
21347c478bd9Sstevel@tonic-gate 				/* catch this is debug kernels */
21357c478bd9Sstevel@tonic-gate 				ASSERT(0);
21367c478bd9Sstevel@tonic-gate 				continue;
21377c478bd9Sstevel@tonic-gate 			}
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 			/*
21407c478bd9Sstevel@tonic-gate 			 * this memory node must not already be reserved
21417c478bd9Sstevel@tonic-gate 			 * by some other memory delete operation.
21427c478bd9Sstevel@tonic-gate 			 */
21437c478bd9Sstevel@tonic-gate 			if (t_mp->sbm_flags & DR_MFLAG_RESERVED)
21447c478bd9Sstevel@tonic-gate 				continue;
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 			/*
21477c478bd9Sstevel@tonic-gate 			 * categorize the memory node
21487c478bd9Sstevel@tonic-gate 			 * If this is a smaller memory node, create a
21497c478bd9Sstevel@tonic-gate 			 * temporary, edited copy of the source board's
21507c478bd9Sstevel@tonic-gate 			 * memlist containing only the span of the non-
21517c478bd9Sstevel@tonic-gate 			 * relocatable pages.
21527c478bd9Sstevel@tonic-gate 			 */
21537c478bd9Sstevel@tonic-gate 			t_phi = (t_mp->sbm_basepfn + t_mp->sbm_npages - 1) & sm;
21547c478bd9Sstevel@tonic-gate 			t_id = t_mp->sbm_cm.sbdev_bp->b_id;
21557c478bd9Sstevel@tonic-gate 			allow_targ_memrange_modify =
21567c478bd9Sstevel@tonic-gate 			    drmach_allow_memrange_modify(t_id);
21577c478bd9Sstevel@tonic-gate 			if (t_mp->sbm_npages == s_mp->sbm_npages &&
21587c478bd9Sstevel@tonic-gate 			    t_phi == s_phi) {
21597c478bd9Sstevel@tonic-gate 				preference = 0;
21607c478bd9Sstevel@tonic-gate 				t_mp->sbm_slice_offset = 0;
21617c478bd9Sstevel@tonic-gate 			} else if (t_mp->sbm_npages > s_mp->sbm_npages &&
21627c478bd9Sstevel@tonic-gate 			    t_phi > s_phi) {
21637c478bd9Sstevel@tonic-gate 				/*
21647c478bd9Sstevel@tonic-gate 				 * Selecting this target will require modifying
21657c478bd9Sstevel@tonic-gate 				 * the source and/or target physical address
21667c478bd9Sstevel@tonic-gate 				 * ranges.  Skip if not supported by platform.
21677c478bd9Sstevel@tonic-gate 				 */
21687c478bd9Sstevel@tonic-gate 				if (!allow_src_memrange_modify ||
21697c478bd9Sstevel@tonic-gate 				    !allow_targ_memrange_modify) {
21707c478bd9Sstevel@tonic-gate 					PR_MEM("%s: skip target %s, memory "
21717c478bd9Sstevel@tonic-gate 					    "range relocation not supported "
21727c478bd9Sstevel@tonic-gate 					    "by platform\n", f,
21737c478bd9Sstevel@tonic-gate 					    t_mp->sbm_cm.sbdev_path);
21747c478bd9Sstevel@tonic-gate 					continue;
21757c478bd9Sstevel@tonic-gate 				}
21767c478bd9Sstevel@tonic-gate 				preference = 1;
21777c478bd9Sstevel@tonic-gate 				t_mp->sbm_slice_offset = 0;
21787c478bd9Sstevel@tonic-gate 			} else {
21797c478bd9Sstevel@tonic-gate 				pfn_t		pfn = 0;
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 				/*
21827c478bd9Sstevel@tonic-gate 				 * Selecting this target will require modifying
21837c478bd9Sstevel@tonic-gate 				 * the source and/or target physical address
21847c478bd9Sstevel@tonic-gate 				 * ranges.  Skip if not supported by platform.
21857c478bd9Sstevel@tonic-gate 				 */
21867c478bd9Sstevel@tonic-gate 				if (!allow_src_memrange_modify ||
21877c478bd9Sstevel@tonic-gate 				    !allow_targ_memrange_modify) {
21887c478bd9Sstevel@tonic-gate 					PR_MEM("%s: skip target %s, memory "
21897c478bd9Sstevel@tonic-gate 					    "range relocation not supported "
21907c478bd9Sstevel@tonic-gate 					    "by platform\n", f,
21917c478bd9Sstevel@tonic-gate 					    t_mp->sbm_cm.sbdev_path);
21927c478bd9Sstevel@tonic-gate 					continue;
21937c478bd9Sstevel@tonic-gate 				}
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 				/*
21967c478bd9Sstevel@tonic-gate 				 * Check if its mc can be programmed to relocate
21977c478bd9Sstevel@tonic-gate 				 * the active address range to match the
21987c478bd9Sstevel@tonic-gate 				 * nonrelocatable span of the source board.
21997c478bd9Sstevel@tonic-gate 				 */
22007c478bd9Sstevel@tonic-gate 				preference = 2;
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 				if (s_mq.phys_pages == 0) {
22037c478bd9Sstevel@tonic-gate 					/*
22047c478bd9Sstevel@tonic-gate 					 * find non-relocatable span on
22057c478bd9Sstevel@tonic-gate 					 * source board.
22067c478bd9Sstevel@tonic-gate 					 */
22077c478bd9Sstevel@tonic-gate 					rv = kphysm_del_span_query(
2208d3d50737SRafael Vanoni 					    s_mp->sbm_basepfn,
2209d3d50737SRafael Vanoni 					    s_mp->sbm_npages, &s_mq);
22107c478bd9Sstevel@tonic-gate 					if (rv != KPHYSM_OK) {
22117c478bd9Sstevel@tonic-gate 						PR_MEM("%s: %s: unexpected"
2212d3d50737SRafael Vanoni 						    " kphysm_del_span_query"
2213d3d50737SRafael Vanoni 						    " return value %d;"
2214d3d50737SRafael Vanoni 						    " basepfn 0x%lx,"
2215d3d50737SRafael Vanoni 						    " npages %ld\n",
2216d3d50737SRafael Vanoni 						    f,
2217d3d50737SRafael Vanoni 						    s_mp->sbm_cm.sbdev_path,
2218d3d50737SRafael Vanoni 						    rv,
2219d3d50737SRafael Vanoni 						    s_mp->sbm_basepfn,
2220d3d50737SRafael Vanoni 						    s_mp->sbm_npages);
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 						/* paranoia */
22237c478bd9Sstevel@tonic-gate 						s_mq.phys_pages = 0;
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 						continue;
22267c478bd9Sstevel@tonic-gate 					}
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 					/* more paranoia */
22297c478bd9Sstevel@tonic-gate 					ASSERT(s_mq.phys_pages != 0);
22307c478bd9Sstevel@tonic-gate 					ASSERT(s_mq.nonrelocatable != 0);
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 					/*
22337c478bd9Sstevel@tonic-gate 					 * this should not happen
22347c478bd9Sstevel@tonic-gate 					 * if it does, it simply means that
22357c478bd9Sstevel@tonic-gate 					 * we can not proceed with qualifying
22367c478bd9Sstevel@tonic-gate 					 * this target candidate.
22377c478bd9Sstevel@tonic-gate 					 */
22387c478bd9Sstevel@tonic-gate 					if (s_mq.nonrelocatable == 0)
22397c478bd9Sstevel@tonic-gate 						continue;
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 					PR_MEM("%s: %s: nonrelocatable"
2242d3d50737SRafael Vanoni 					    " span (0x%lx..0x%lx)\n",
2243d3d50737SRafael Vanoni 					    f,
2244d3d50737SRafael Vanoni 					    s_mp->sbm_cm.sbdev_path,
2245d3d50737SRafael Vanoni 					    s_mq.first_nonrelocatable,
2246d3d50737SRafael Vanoni 					    s_mq.last_nonrelocatable);
22477c478bd9Sstevel@tonic-gate 				}
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 				/*
22507c478bd9Sstevel@tonic-gate 				 * Round down the starting pfn of the
22517c478bd9Sstevel@tonic-gate 				 * nonrelocatable span on the source board
22527c478bd9Sstevel@tonic-gate 				 * to nearest programmable boundary possible
22537c478bd9Sstevel@tonic-gate 				 * with this target candidate.
22547c478bd9Sstevel@tonic-gate 				 */
22557c478bd9Sstevel@tonic-gate 				pfn = s_mq.first_nonrelocatable &
2256d3d50737SRafael Vanoni 				    ~t_mp->sbm_alignment_mask;
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate 				/* skip candidate if memory is too small */
22597c478bd9Sstevel@tonic-gate 				if (pfn + t_mp->sbm_npages <
2260d3d50737SRafael Vanoni 				    s_mq.last_nonrelocatable)
22617c478bd9Sstevel@tonic-gate 					continue;
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 				/*
22647c478bd9Sstevel@tonic-gate 				 * reprogramming an mc to relocate its
22657c478bd9Sstevel@tonic-gate 				 * active address range means the beginning
22667c478bd9Sstevel@tonic-gate 				 * address to which the DIMMS respond will
22677c478bd9Sstevel@tonic-gate 				 * be somewhere above the slice boundary
22687c478bd9Sstevel@tonic-gate 				 * address.  The larger the size of memory
22697c478bd9Sstevel@tonic-gate 				 * on this unit, the more likely part of it
22707c478bd9Sstevel@tonic-gate 				 * will exist beyond the end of the slice.
22717c478bd9Sstevel@tonic-gate 				 * The portion of the memory that does is
22727c478bd9Sstevel@tonic-gate 				 * unavailable to the system until the mc
22737c478bd9Sstevel@tonic-gate 				 * reprogrammed to a more favorable base
22747c478bd9Sstevel@tonic-gate 				 * address.
22757c478bd9Sstevel@tonic-gate 				 * An attempt is made to avoid the loss by
22767c478bd9Sstevel@tonic-gate 				 * recalculating the mc base address relative
22777c478bd9Sstevel@tonic-gate 				 * to the end of the slice.  This may produce
22787c478bd9Sstevel@tonic-gate 				 * a more favorable result.  If not, we lower
22797c478bd9Sstevel@tonic-gate 				 * the board's preference rating so that it
22807c478bd9Sstevel@tonic-gate 				 * is one the last candidate boards to be
22817c478bd9Sstevel@tonic-gate 				 * considered.
22827c478bd9Sstevel@tonic-gate 				 */
22837c478bd9Sstevel@tonic-gate 				if ((pfn + t_mp->sbm_npages) & ~sm) {
22847c478bd9Sstevel@tonic-gate 					pfn_t p;
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 					ASSERT(sz >= t_mp->sbm_npages);
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate 					/*
22897c478bd9Sstevel@tonic-gate 					 * calculate an alternative starting
22907c478bd9Sstevel@tonic-gate 					 * address relative to the end of the
22917c478bd9Sstevel@tonic-gate 					 * slice's address space.
22927c478bd9Sstevel@tonic-gate 					 */
22937c478bd9Sstevel@tonic-gate 					p = pfn & ~sm;
22947c478bd9Sstevel@tonic-gate 					p = p + (sz - t_mp->sbm_npages);
22957c478bd9Sstevel@tonic-gate 					p = p & ~t_mp->sbm_alignment_mask;
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 					if ((p > s_mq.first_nonrelocatable) ||
2298d3d50737SRafael Vanoni 					    (p + t_mp->sbm_npages <
2299d3d50737SRafael Vanoni 					    s_mq.last_nonrelocatable)) {
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 						/*
23027c478bd9Sstevel@tonic-gate 						 * alternative starting addr
23037c478bd9Sstevel@tonic-gate 						 * won't work. Lower preference
23047c478bd9Sstevel@tonic-gate 						 * rating of this board, since
23057c478bd9Sstevel@tonic-gate 						 * some number of pages will
23067c478bd9Sstevel@tonic-gate 						 * unavailable for use.
23077c478bd9Sstevel@tonic-gate 						 */
23087c478bd9Sstevel@tonic-gate 						preference = 3;
23097c478bd9Sstevel@tonic-gate 					} else {
23107c478bd9Sstevel@tonic-gate 						dr_smt_realigned++;
23117c478bd9Sstevel@tonic-gate 						pfn = p;
23127c478bd9Sstevel@tonic-gate 					}
23137c478bd9Sstevel@tonic-gate 				}
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 				/*
23167c478bd9Sstevel@tonic-gate 				 * translate calculated pfn to an offset
23177c478bd9Sstevel@tonic-gate 				 * relative to the slice boundary.  If the
23187c478bd9Sstevel@tonic-gate 				 * candidate board is selected, this offset
23197c478bd9Sstevel@tonic-gate 				 * will be used to calculate the values
23207c478bd9Sstevel@tonic-gate 				 * programmed into the mc.
23217c478bd9Sstevel@tonic-gate 				 */
23227c478bd9Sstevel@tonic-gate 				t_mp->sbm_slice_offset = pfn & sm;
23237c478bd9Sstevel@tonic-gate 				PR_MEM("%s: %s:"
2324d3d50737SRafael Vanoni 				    "  proposed mc offset 0x%lx\n",
2325d3d50737SRafael Vanoni 				    f,
2326d3d50737SRafael Vanoni 				    t_mp->sbm_cm.sbdev_path,
2327d3d50737SRafael Vanoni 				    t_mp->sbm_slice_offset);
23287c478bd9Sstevel@tonic-gate 			}
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 			dr_smt_preference[preference]++;
23317c478bd9Sstevel@tonic-gate 
23327c478bd9Sstevel@tonic-gate 			/* calculate index to start of preference set */
23337c478bd9Sstevel@tonic-gate 			idx  = n_units_per_set * preference;
23347c478bd9Sstevel@tonic-gate 			/* calculate offset to respective element */
23357c478bd9Sstevel@tonic-gate 			idx += t_bd * MAX_MEM_UNITS_PER_BOARD + t_unit;
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 			ASSERT(idx < n_units_per_set * n_sets);
23387c478bd9Sstevel@tonic-gate 			sets[idx] = t_mp;
23397c478bd9Sstevel@tonic-gate 		}
23407c478bd9Sstevel@tonic-gate 	}
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 	/*
23437c478bd9Sstevel@tonic-gate 	 * NOTE: this would be a good place to sort each candidate
23447c478bd9Sstevel@tonic-gate 	 * set in to some desired order, e.g. memory size in ascending
23457c478bd9Sstevel@tonic-gate 	 * order.  Without an additional sorting step here, the order
23467c478bd9Sstevel@tonic-gate 	 * within a set is ascending board number order.
23477c478bd9Sstevel@tonic-gate 	 */
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 	c_mp = NULL;
23507c478bd9Sstevel@tonic-gate 	x_ml = NULL;
23517c478bd9Sstevel@tonic-gate 	t_ml = NULL;
23527c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < n_units_per_set * n_sets; idx++) {
23537c478bd9Sstevel@tonic-gate 		memquery_t mq;
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 		/* cleanup t_ml after previous pass */
23567c478bd9Sstevel@tonic-gate 		if (t_ml != NULL) {
23577c478bd9Sstevel@tonic-gate 			memlist_delete(t_ml);
23587c478bd9Sstevel@tonic-gate 			t_ml = NULL;
23597c478bd9Sstevel@tonic-gate 		}
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate 		/* get candidate target board mem unit */
23627c478bd9Sstevel@tonic-gate 		t_mp = sets[idx];
23637c478bd9Sstevel@tonic-gate 		if (t_mp == NULL)
23647c478bd9Sstevel@tonic-gate 			continue;
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate 		/* get target board memlist */
23677c478bd9Sstevel@tonic-gate 		t_ml = dr_get_memlist(t_mp);
23687c478bd9Sstevel@tonic-gate 		if (t_ml == NULL) {
23697c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s: no memlist for"
2370d3d50737SRafael Vanoni 			    " mem-unit %d, board %d",
2371d3d50737SRafael Vanoni 			    f,
2372d3d50737SRafael Vanoni 			    t_mp->sbm_cm.sbdev_bp->b_num,
2373d3d50737SRafael Vanoni 			    t_mp->sbm_cm.sbdev_unum);
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 			continue;
23767c478bd9Sstevel@tonic-gate 		}
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 		/* get appropriate source board memlist */
23797c478bd9Sstevel@tonic-gate 		t_phi = (t_mp->sbm_basepfn + t_mp->sbm_npages - 1) & sm;
23807c478bd9Sstevel@tonic-gate 		if (t_mp->sbm_npages < s_mp->sbm_npages || t_phi < s_phi) {
23817c478bd9Sstevel@tonic-gate 			spgcnt_t excess;
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 			/*
23847c478bd9Sstevel@tonic-gate 			 * make a copy of the source board memlist
23857c478bd9Sstevel@tonic-gate 			 * then edit it to remove the spans that
23867c478bd9Sstevel@tonic-gate 			 * are outside the calculated span of
23877c478bd9Sstevel@tonic-gate 			 * [pfn..s_mq.last_nonrelocatable].
23887c478bd9Sstevel@tonic-gate 			 */
23897c478bd9Sstevel@tonic-gate 			if (x_ml != NULL)
23907c478bd9Sstevel@tonic-gate 				memlist_delete(x_ml);
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 			x_ml = memlist_dup(s_ml);
23937c478bd9Sstevel@tonic-gate 			if (x_ml == NULL) {
23947c478bd9Sstevel@tonic-gate 				PR_MEM("%s: memlist_dup failed\n", f);
23957c478bd9Sstevel@tonic-gate 				/* TODO: should abort */
23967c478bd9Sstevel@tonic-gate 				continue;
23977c478bd9Sstevel@tonic-gate 			}
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 			/* trim off lower portion */
24007c478bd9Sstevel@tonic-gate 			excess = t_mp->sbm_slice_offset -
24017c478bd9Sstevel@tonic-gate 			    (s_mp->sbm_basepfn & sm);
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 			if (excess > 0) {
24047c478bd9Sstevel@tonic-gate 				x_ml = memlist_del_span(
2405d3d50737SRafael Vanoni 				    x_ml,
2406d3d50737SRafael Vanoni 				    _ptob64(s_mp->sbm_basepfn),
2407d3d50737SRafael Vanoni 				    _ptob64(excess));
24087c478bd9Sstevel@tonic-gate 			}
24097c478bd9Sstevel@tonic-gate 			ASSERT(x_ml);
24107c478bd9Sstevel@tonic-gate 
24117c478bd9Sstevel@tonic-gate 			/*
24127c478bd9Sstevel@tonic-gate 			 * Since this candidate target board is smaller
24137c478bd9Sstevel@tonic-gate 			 * than the source board, s_mq must have been
24147c478bd9Sstevel@tonic-gate 			 * initialized in previous loop while processing
24157c478bd9Sstevel@tonic-gate 			 * this or some other candidate board.
24167c478bd9Sstevel@tonic-gate 			 * FIXME: this is weak.
24177c478bd9Sstevel@tonic-gate 			 */
24187c478bd9Sstevel@tonic-gate 			ASSERT(s_mq.phys_pages != 0);
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate 			/* trim off upper portion */
24217c478bd9Sstevel@tonic-gate 			excess = (s_mp->sbm_basepfn + s_mp->sbm_npages)
2422d3d50737SRafael Vanoni 			    - (s_mq.last_nonrelocatable + 1);
24237c478bd9Sstevel@tonic-gate 			if (excess > 0) {
24247c478bd9Sstevel@tonic-gate 				pfn_t p;
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 				p  = s_mq.last_nonrelocatable + 1;
24277c478bd9Sstevel@tonic-gate 				x_ml = memlist_del_span(
2428d3d50737SRafael Vanoni 				    x_ml,
2429d3d50737SRafael Vanoni 				    _ptob64(p),
2430d3d50737SRafael Vanoni 				    _ptob64(excess));
24317c478bd9Sstevel@tonic-gate 			}
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 			PR_MEM("%s: %s: edited source memlist:\n",
2434d3d50737SRafael Vanoni 			    f, s_mp->sbm_cm.sbdev_path);
24357c478bd9Sstevel@tonic-gate 			PR_MEMLIST_DUMP(x_ml);
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate #ifdef DEBUG
24387c478bd9Sstevel@tonic-gate 			/* sanity check memlist */
24397c478bd9Sstevel@tonic-gate 			d_ml = x_ml;
244056f33205SJonathan Adams 			while (d_ml->ml_next != NULL)
244156f33205SJonathan Adams 				d_ml = d_ml->ml_next;
24427c478bd9Sstevel@tonic-gate 
244356f33205SJonathan Adams 			ASSERT(d_ml->ml_address + d_ml->ml_size ==
2444d3d50737SRafael Vanoni 			    _ptob64(s_mq.last_nonrelocatable + 1));
24457c478bd9Sstevel@tonic-gate #endif
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 			/*
24487c478bd9Sstevel@tonic-gate 			 * x_ml now describes only the portion of the
24497c478bd9Sstevel@tonic-gate 			 * source board that will be moved during the
24507c478bd9Sstevel@tonic-gate 			 * copy/rename operation.
24517c478bd9Sstevel@tonic-gate 			 */
24527c478bd9Sstevel@tonic-gate 			d_ml = x_ml;
24537c478bd9Sstevel@tonic-gate 		} else {
24547c478bd9Sstevel@tonic-gate 			/* use original memlist; all spans will be moved */
24557c478bd9Sstevel@tonic-gate 			d_ml = s_ml;
24567c478bd9Sstevel@tonic-gate 		}
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate 		/* verify target can support source memory spans. */
24597c478bd9Sstevel@tonic-gate 		if (memlist_canfit(d_ml, t_ml) == 0) {
24607c478bd9Sstevel@tonic-gate 			PR_MEM("%s: source memlist won't"
2461d3d50737SRafael Vanoni 			    " fit in target memlist\n", f);
24627c478bd9Sstevel@tonic-gate 			PR_MEM("%s: source memlist:\n", f);
24637c478bd9Sstevel@tonic-gate 			PR_MEMLIST_DUMP(d_ml);
24647c478bd9Sstevel@tonic-gate 			PR_MEM("%s: target memlist:\n", f);
24657c478bd9Sstevel@tonic-gate 			PR_MEMLIST_DUMP(t_ml);
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate 			continue;
24687c478bd9Sstevel@tonic-gate 		}
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate 		/* NOTE: the value of d_ml is not used beyond this point */
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate 		PR_MEM("%s: checking for no-reloc in %s, "
2473d3d50737SRafael Vanoni 		    " basepfn=0x%lx, npages=%ld\n",
2474d3d50737SRafael Vanoni 		    f,
2475d3d50737SRafael Vanoni 		    t_mp->sbm_cm.sbdev_path,
2476d3d50737SRafael Vanoni 		    t_mp->sbm_basepfn,
2477d3d50737SRafael Vanoni 		    t_mp->sbm_npages);
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 		rv = kphysm_del_span_query(
2480d3d50737SRafael Vanoni 		    t_mp->sbm_basepfn, t_mp->sbm_npages, &mq);
24817c478bd9Sstevel@tonic-gate 		if (rv != KPHYSM_OK) {
24827c478bd9Sstevel@tonic-gate 			PR_MEM("%s: kphysm_del_span_query:"
2483d3d50737SRafael Vanoni 			    " unexpected return value %d\n", f, rv);
24847c478bd9Sstevel@tonic-gate 
24857c478bd9Sstevel@tonic-gate 			continue;
24867c478bd9Sstevel@tonic-gate 		}
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate 		if (mq.nonrelocatable != 0) {
24897c478bd9Sstevel@tonic-gate 			PR_MEM("%s: candidate %s has"
2490d3d50737SRafael Vanoni 			    " nonrelocatable span [0x%lx..0x%lx]\n",
2491d3d50737SRafael Vanoni 			    f,
2492d3d50737SRafael Vanoni 			    t_mp->sbm_cm.sbdev_path,
2493d3d50737SRafael Vanoni 			    mq.first_nonrelocatable,
2494d3d50737SRafael Vanoni 			    mq.last_nonrelocatable);
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate 			continue;
24977c478bd9Sstevel@tonic-gate 		}
24987c478bd9Sstevel@tonic-gate 
24997c478bd9Sstevel@tonic-gate #ifdef DEBUG
25007c478bd9Sstevel@tonic-gate 		/*
25017c478bd9Sstevel@tonic-gate 		 * This is a debug tool for excluding certain boards
25027c478bd9Sstevel@tonic-gate 		 * from being selected as a target board candidate.
25037c478bd9Sstevel@tonic-gate 		 * dr_ignore_board is only tested by this driver.
25047c478bd9Sstevel@tonic-gate 		 * It must be set with adb, obp, /etc/system or your
25057c478bd9Sstevel@tonic-gate 		 * favorite debugger.
25067c478bd9Sstevel@tonic-gate 		 */
25077c478bd9Sstevel@tonic-gate 		if (dr_ignore_board &
2508d3d50737SRafael Vanoni 		    (1 << (t_mp->sbm_cm.sbdev_bp->b_num - 1))) {
25097c478bd9Sstevel@tonic-gate 			PR_MEM("%s: dr_ignore_board flag set,"
2510d3d50737SRafael Vanoni 			    " ignoring %s as candidate\n",
2511d3d50737SRafael Vanoni 			    f, t_mp->sbm_cm.sbdev_path);
25127c478bd9Sstevel@tonic-gate 			continue;
25137c478bd9Sstevel@tonic-gate 		}
25147c478bd9Sstevel@tonic-gate #endif
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate 		/*
25177c478bd9Sstevel@tonic-gate 		 * Reserve excess source board memory, if any.
25187c478bd9Sstevel@tonic-gate 		 *
25197c478bd9Sstevel@tonic-gate 		 * When the number of pages on the candidate target
25207c478bd9Sstevel@tonic-gate 		 * board is less than the number of pages on the source,
25217c478bd9Sstevel@tonic-gate 		 * then some spans (clearly) of the source board's address
25227c478bd9Sstevel@tonic-gate 		 * space will not be covered by physical memory after the
25237c478bd9Sstevel@tonic-gate 		 * copy/rename completes.  The following code block
25247c478bd9Sstevel@tonic-gate 		 * schedules those spans to be deleted.
25257c478bd9Sstevel@tonic-gate 		 */
25267c478bd9Sstevel@tonic-gate 		if (t_mp->sbm_npages < s_mp->sbm_npages || t_phi < s_phi) {
25277c478bd9Sstevel@tonic-gate 			pfn_t pfn;
25287c478bd9Sstevel@tonic-gate 			uint64_t s_del_pa;
25297c478bd9Sstevel@tonic-gate 			struct memlist *ml;
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate 			d_ml = memlist_dup(s_ml);
25327c478bd9Sstevel@tonic-gate 			if (d_ml == NULL) {
25337c478bd9Sstevel@tonic-gate 				PR_MEM("%s: cant dup src brd memlist\n", f);
25347c478bd9Sstevel@tonic-gate 				/* TODO: should abort */
25357c478bd9Sstevel@tonic-gate 				continue;
25367c478bd9Sstevel@tonic-gate 			}
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 			/* calculate base pfn relative to target board */
25397c478bd9Sstevel@tonic-gate 			pfn  = s_mp->sbm_basepfn & ~sm;
25407c478bd9Sstevel@tonic-gate 			pfn += t_mp->sbm_slice_offset;
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 			/*
25437c478bd9Sstevel@tonic-gate 			 * cannot split dynamically added segment
25447c478bd9Sstevel@tonic-gate 			 */
25457c478bd9Sstevel@tonic-gate 			s_del_pa = _ptob64(pfn + t_mp->sbm_npages);
25467c478bd9Sstevel@tonic-gate 			PR_MEM("%s: proposed src delete pa=0x%lx\n", f,
25477c478bd9Sstevel@tonic-gate 			    s_del_pa);
25487c478bd9Sstevel@tonic-gate 			PR_MEM("%s: checking for split of dyn seg list:\n", f);
25497c478bd9Sstevel@tonic-gate 			PR_MEMLIST_DUMP(s_mp->sbm_dyn_segs);
255056f33205SJonathan Adams 			for (ml = s_mp->sbm_dyn_segs; ml; ml = ml->ml_next) {
255156f33205SJonathan Adams 				if (s_del_pa > ml->ml_address &&
255256f33205SJonathan Adams 				    s_del_pa < ml->ml_address + ml->ml_size) {
255356f33205SJonathan Adams 					s_del_pa = ml->ml_address;
25547c478bd9Sstevel@tonic-gate 					break;
25557c478bd9Sstevel@tonic-gate 				}
25567c478bd9Sstevel@tonic-gate 			}
25577c478bd9Sstevel@tonic-gate 
25587c478bd9Sstevel@tonic-gate 			/* remove span that will reside on candidate board */
25597c478bd9Sstevel@tonic-gate 			d_ml = memlist_del_span(d_ml, _ptob64(pfn),
25607c478bd9Sstevel@tonic-gate 			    s_del_pa - _ptob64(pfn));
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 			PR_MEM("%s: %s: reserving src brd memlist:\n",
2563d3d50737SRafael Vanoni 			    f, s_mp->sbm_cm.sbdev_path);
25647c478bd9Sstevel@tonic-gate 			PR_MEMLIST_DUMP(d_ml);
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate 			/* reserve excess spans */
2567d3d50737SRafael Vanoni 			if (dr_reserve_mem_spans(&s_mp->sbm_memhandle, d_ml)
2568d3d50737SRafael Vanoni 			    != 0) {
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 				/* likely more non-reloc pages appeared */
25717c478bd9Sstevel@tonic-gate 				/* TODO: restart from top? */
25727c478bd9Sstevel@tonic-gate 				continue;
25737c478bd9Sstevel@tonic-gate 			}
25747c478bd9Sstevel@tonic-gate 		} else {
25757c478bd9Sstevel@tonic-gate 			/* no excess source board memory */
25767c478bd9Sstevel@tonic-gate 			d_ml = NULL;
25777c478bd9Sstevel@tonic-gate 		}
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 		s_mp->sbm_flags |= DR_MFLAG_RESERVED;
25807c478bd9Sstevel@tonic-gate 
25817c478bd9Sstevel@tonic-gate 		/*
25827c478bd9Sstevel@tonic-gate 		 * reserve all memory on target board.
25837c478bd9Sstevel@tonic-gate 		 * NOTE: source board's memhandle is used.
25847c478bd9Sstevel@tonic-gate 		 *
25857c478bd9Sstevel@tonic-gate 		 * If this succeeds (eq 0), then target selection is
25867c478bd9Sstevel@tonic-gate 		 * complete and all unwanted memory spans, both source and
25877c478bd9Sstevel@tonic-gate 		 * target, have been reserved.  Loop is terminated.
25887c478bd9Sstevel@tonic-gate 		 */
25897c478bd9Sstevel@tonic-gate 		if (dr_reserve_mem_spans(&s_mp->sbm_memhandle, t_ml) == 0) {
25907c478bd9Sstevel@tonic-gate 			PR_MEM("%s: %s: target board memory reserved\n",
2591d3d50737SRafael Vanoni 			    f, t_mp->sbm_cm.sbdev_path);
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate 			/* a candidate target board is now reserved */
25947c478bd9Sstevel@tonic-gate 			t_mp->sbm_flags |= DR_MFLAG_RESERVED;
25957c478bd9Sstevel@tonic-gate 			c_mp = t_mp;
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate 			/* *** EXITING LOOP *** */
25987c478bd9Sstevel@tonic-gate 			break;
25997c478bd9Sstevel@tonic-gate 		}
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate 		/* did not successfully reserve the target board. */
26027c478bd9Sstevel@tonic-gate 		PR_MEM("%s: could not reserve target %s\n",
2603d3d50737SRafael Vanoni 		    f, t_mp->sbm_cm.sbdev_path);
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 		/*
26067c478bd9Sstevel@tonic-gate 		 * NOTE: an undo of the dr_reserve_mem_span work
26077c478bd9Sstevel@tonic-gate 		 * will happen automatically when the memhandle
26087c478bd9Sstevel@tonic-gate 		 * (s_mp->sbm_memhandle) is kphysm_del_release'd.
26097c478bd9Sstevel@tonic-gate 		 */
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 		s_mp->sbm_flags &= ~DR_MFLAG_RESERVED;
26127c478bd9Sstevel@tonic-gate 	}
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate 	/* clean up after memlist editing logic */
26157c478bd9Sstevel@tonic-gate 	if (x_ml != NULL)
26167c478bd9Sstevel@tonic-gate 		memlist_delete(x_ml);
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 	FREESTRUCT(sets, dr_mem_unit_t *, n_units_per_set * n_sets);
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 	/*
26217c478bd9Sstevel@tonic-gate 	 * c_mp will be NULL when the entire sets[] array
26227c478bd9Sstevel@tonic-gate 	 * has been searched without reserving a target board.
26237c478bd9Sstevel@tonic-gate 	 */
26247c478bd9Sstevel@tonic-gate 	if (c_mp == NULL) {
26257c478bd9Sstevel@tonic-gate 		PR_MEM("%s: %s: target selection failed.\n",
2626d3d50737SRafael Vanoni 		    f, s_mp->sbm_cm.sbdev_path);
26277c478bd9Sstevel@tonic-gate 
26287c478bd9Sstevel@tonic-gate 		if (t_ml != NULL)
26297c478bd9Sstevel@tonic-gate 			memlist_delete(t_ml);
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 		return (-1);
26327c478bd9Sstevel@tonic-gate 	}
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate 	PR_MEM("%s: found target %s for source %s\n",
2635d3d50737SRafael Vanoni 	    f,
2636d3d50737SRafael Vanoni 	    c_mp->sbm_cm.sbdev_path,
2637d3d50737SRafael Vanoni 	    s_mp->sbm_cm.sbdev_path);
26387c478bd9Sstevel@tonic-gate 
26397c478bd9Sstevel@tonic-gate 	s_mp->sbm_peer = c_mp;
26407c478bd9Sstevel@tonic-gate 	s_mp->sbm_flags |= DR_MFLAG_SOURCE;
26417c478bd9Sstevel@tonic-gate 	s_mp->sbm_del_mlist = d_ml;	/* spans to be deleted, if any */
26427c478bd9Sstevel@tonic-gate 	s_mp->sbm_mlist = s_ml;
26437c478bd9Sstevel@tonic-gate 	s_mp->sbm_cm.sbdev_busy = 1;
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	c_mp->sbm_peer = s_mp;
26467c478bd9Sstevel@tonic-gate 	c_mp->sbm_flags |= DR_MFLAG_TARGET;
26477c478bd9Sstevel@tonic-gate 	c_mp->sbm_del_mlist = t_ml;	/* spans to be deleted */
26487c478bd9Sstevel@tonic-gate 	c_mp->sbm_mlist = t_ml;
26497c478bd9Sstevel@tonic-gate 	c_mp->sbm_cm.sbdev_busy = 1;
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 	s_mp->sbm_flags &= ~DR_MFLAG_MEMRESIZE;
26527c478bd9Sstevel@tonic-gate 	if (c_mp->sbm_npages > s_mp->sbm_npages) {
26537c478bd9Sstevel@tonic-gate 		s_mp->sbm_flags |= DR_MFLAG_MEMUPSIZE;
265404580fdfSmathue 		PR_MEM("%s: upsize detected (source=%ld < target=%ld)\n",
2655d3d50737SRafael Vanoni 		    f, s_mp->sbm_npages, c_mp->sbm_npages);
26567c478bd9Sstevel@tonic-gate 	} else if (c_mp->sbm_npages < s_mp->sbm_npages) {
26577c478bd9Sstevel@tonic-gate 		s_mp->sbm_flags |= DR_MFLAG_MEMDOWNSIZE;
265804580fdfSmathue 		PR_MEM("%s: downsize detected (source=%ld > target=%ld)\n",
2659d3d50737SRafael Vanoni 		    f, s_mp->sbm_npages, c_mp->sbm_npages);
26607c478bd9Sstevel@tonic-gate 	}
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate 	return (0);
26637c478bd9Sstevel@tonic-gate }
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate /*
26667c478bd9Sstevel@tonic-gate  * Memlist support.
26677c478bd9Sstevel@tonic-gate  */
26687c478bd9Sstevel@tonic-gate 
26697c478bd9Sstevel@tonic-gate /*
26707c478bd9Sstevel@tonic-gate  * Determine whether the source memlist (s_mlist) will
26717c478bd9Sstevel@tonic-gate  * fit into the target memlist (t_mlist) in terms of
26727c478bd9Sstevel@tonic-gate  * size and holes (i.e. based on same relative base address).
26737c478bd9Sstevel@tonic-gate  */
26747c478bd9Sstevel@tonic-gate static int
memlist_canfit(struct memlist * s_mlist,struct memlist * t_mlist)26757c478bd9Sstevel@tonic-gate memlist_canfit(struct memlist *s_mlist, struct memlist *t_mlist)
26767c478bd9Sstevel@tonic-gate {
26777c478bd9Sstevel@tonic-gate 	int		rv = 0;
26787c478bd9Sstevel@tonic-gate 	uint64_t	s_basepa, t_basepa;
26797c478bd9Sstevel@tonic-gate 	struct memlist	*s_ml, *t_ml;
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 	if ((s_mlist == NULL) || (t_mlist == NULL))
26827c478bd9Sstevel@tonic-gate 		return (0);
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	/*
26857c478bd9Sstevel@tonic-gate 	 * Base both memlists on common base address (0).
26867c478bd9Sstevel@tonic-gate 	 */
268756f33205SJonathan Adams 	s_basepa = s_mlist->ml_address;
268856f33205SJonathan Adams 	t_basepa = t_mlist->ml_address;
26897c478bd9Sstevel@tonic-gate 
269056f33205SJonathan Adams 	for (s_ml = s_mlist; s_ml; s_ml = s_ml->ml_next)
269156f33205SJonathan Adams 		s_ml->ml_address -= s_basepa;
26927c478bd9Sstevel@tonic-gate 
269356f33205SJonathan Adams 	for (t_ml = t_mlist; t_ml; t_ml = t_ml->ml_next)
269456f33205SJonathan Adams 		t_ml->ml_address -= t_basepa;
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate 	s_ml = s_mlist;
269756f33205SJonathan Adams 	for (t_ml = t_mlist; t_ml && s_ml; t_ml = t_ml->ml_next) {
26987c478bd9Sstevel@tonic-gate 		uint64_t	s_start, s_end;
26997c478bd9Sstevel@tonic-gate 		uint64_t	t_start, t_end;
27007c478bd9Sstevel@tonic-gate 
270156f33205SJonathan Adams 		t_start = t_ml->ml_address;
270256f33205SJonathan Adams 		t_end = t_start + t_ml->ml_size;
27037c478bd9Sstevel@tonic-gate 
270456f33205SJonathan Adams 		for (; s_ml; s_ml = s_ml->ml_next) {
270556f33205SJonathan Adams 			s_start = s_ml->ml_address;
270656f33205SJonathan Adams 			s_end = s_start + s_ml->ml_size;
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate 			if ((s_start < t_start) || (s_end > t_end))
27097c478bd9Sstevel@tonic-gate 				break;
27107c478bd9Sstevel@tonic-gate 		}
27117c478bd9Sstevel@tonic-gate 	}
27127c478bd9Sstevel@tonic-gate 	/*
27137c478bd9Sstevel@tonic-gate 	 * If we ran out of source memlist chunks that mean
27147c478bd9Sstevel@tonic-gate 	 * we found a home for all of them.
27157c478bd9Sstevel@tonic-gate 	 */
27167c478bd9Sstevel@tonic-gate 	if (s_ml == NULL)
27177c478bd9Sstevel@tonic-gate 		rv = 1;
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 	/*
27207c478bd9Sstevel@tonic-gate 	 * Need to add base addresses back since memlists
27217c478bd9Sstevel@tonic-gate 	 * are probably in use by caller.
27227c478bd9Sstevel@tonic-gate 	 */
272356f33205SJonathan Adams 	for (s_ml = s_mlist; s_ml; s_ml = s_ml->ml_next)
272456f33205SJonathan Adams 		s_ml->ml_address += s_basepa;
27257c478bd9Sstevel@tonic-gate 
272656f33205SJonathan Adams 	for (t_ml = t_mlist; t_ml; t_ml = t_ml->ml_next)
272756f33205SJonathan Adams 		t_ml->ml_address += t_basepa;
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate 	return (rv);
27307c478bd9Sstevel@tonic-gate }
2731