xref: /onnv-gate/usr/src/uts/sun4/io/px/px_ib.c (revision 11836:8c4dbbb43e4c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51617Sgovinda  * Common Development and Distribution License (the "License").
61617Sgovinda  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2211520SScott.Carter@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * PX Interrupt Block implementation
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/kmem.h>
320Sstevel@tonic-gate #include <sys/async.h>
330Sstevel@tonic-gate #include <sys/systm.h>		/* panicstr */
340Sstevel@tonic-gate #include <sys/spl.h>
350Sstevel@tonic-gate #include <sys/sunddi.h>
360Sstevel@tonic-gate #include <sys/machsystm.h>	/* intr_dist_add */
370Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
380Sstevel@tonic-gate #include <sys/cpuvar.h>
391772Sjl139090 #include <sys/time.h>
400Sstevel@tonic-gate #include "px_obj.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*LINTLIBRARY*/
430Sstevel@tonic-gate 
440Sstevel@tonic-gate static void px_ib_intr_redist(void *arg, int32_t weight_max, int32_t weight);
45624Sschwartz static void px_ib_cpu_ticks_to_ih_nsec(px_ib_t *ib_p, px_ih_t *ih_p,
46624Sschwartz     uint32_t cpu_id);
470Sstevel@tonic-gate static uint_t px_ib_intr_reset(void *arg);
48624Sschwartz static void px_fill_in_intr_devs(pcitool_intr_dev_t *dev, char *driver_name,
49624Sschwartz     char *path_name, int instance);
500Sstevel@tonic-gate 
511772Sjl139090 extern uint64_t xc_tick_jump_limit;
521772Sjl139090 
530Sstevel@tonic-gate int
540Sstevel@tonic-gate px_ib_attach(px_t *px_p)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
570Sstevel@tonic-gate 	px_ib_t		*ib_p;
580Sstevel@tonic-gate 	sysino_t	sysino;
590Sstevel@tonic-gate 	px_fault_t	*fault_p = &px_p->px_fault;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	DBG(DBG_IB, dip, "px_ib_attach\n");
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	if (px_lib_intr_devino_to_sysino(px_p->px_dip,
6427Sjchu 	    px_p->px_inos[PX_INTR_PEC], &sysino) != DDI_SUCCESS)
650Sstevel@tonic-gate 		return (DDI_FAILURE);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	/*
680Sstevel@tonic-gate 	 * Allocate interrupt block state structure and link it to
690Sstevel@tonic-gate 	 * the px state structure.
700Sstevel@tonic-gate 	 */
710Sstevel@tonic-gate 	ib_p = kmem_zalloc(sizeof (px_ib_t), KM_SLEEP);
720Sstevel@tonic-gate 	px_p->px_ib_p = ib_p;
730Sstevel@tonic-gate 	ib_p->ib_px_p = px_p;
742973Sgovinda 	ib_p->ib_ino_lst = (px_ino_t *)NULL;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	mutex_init(&ib_p->ib_intr_lock, NULL, MUTEX_DRIVER, NULL);
770Sstevel@tonic-gate 	mutex_init(&ib_p->ib_ino_lst_mutex, NULL, MUTEX_DRIVER, NULL);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	bus_func_register(BF_TYPE_RESINTR, px_ib_intr_reset, ib_p);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	intr_dist_add_weighted(px_ib_intr_redist, ib_p);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	/*
840Sstevel@tonic-gate 	 * Initialize PEC fault data structure
850Sstevel@tonic-gate 	 */
860Sstevel@tonic-gate 	fault_p->px_fh_dip = dip;
870Sstevel@tonic-gate 	fault_p->px_fh_sysino = sysino;
8827Sjchu 	fault_p->px_err_func = px_err_dmc_pec_intr;
8927Sjchu 	fault_p->px_intr_ino = px_p->px_inos[PX_INTR_PEC];
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	return (DDI_SUCCESS);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate void
950Sstevel@tonic-gate px_ib_detach(px_t *px_p)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
980Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	DBG(DBG_IB, dip, "px_ib_detach\n");
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	bus_func_unregister(BF_TYPE_RESINTR, px_ib_intr_reset, ib_p);
1030Sstevel@tonic-gate 	intr_dist_rem_weighted(px_ib_intr_redist, ib_p);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	mutex_destroy(&ib_p->ib_ino_lst_mutex);
1060Sstevel@tonic-gate 	mutex_destroy(&ib_p->ib_intr_lock);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	px_ib_free_ino_all(ib_p);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	px_p->px_ib_p = NULL;
1110Sstevel@tonic-gate 	kmem_free(ib_p, sizeof (px_ib_t));
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate void
1150Sstevel@tonic-gate px_ib_intr_enable(px_t *px_p, cpuid_t cpu_id, devino_t ino)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
1180Sstevel@tonic-gate 	sysino_t	sysino;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	/*
1210Sstevel@tonic-gate 	 * Determine the cpu for the interrupt
1220Sstevel@tonic-gate 	 */
1230Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_intr_lock);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	DBG(DBG_IB, px_p->px_dip,
1260Sstevel@tonic-gate 	    "px_ib_intr_enable: ino=%x cpu_id=%x\n", ino, cpu_id);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	if (px_lib_intr_devino_to_sysino(px_p->px_dip, ino,
1290Sstevel@tonic-gate 	    &sysino) != DDI_SUCCESS) {
1300Sstevel@tonic-gate 		DBG(DBG_IB, px_p->px_dip,
1310Sstevel@tonic-gate 		    "px_ib_intr_enable: px_intr_devino_to_sysino() failed\n");
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_intr_lock);
1340Sstevel@tonic-gate 		return;
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	PX_INTR_ENABLE(px_p->px_dip, sysino, cpu_id);
138693Sgovinda 	px_lib_intr_setstate(px_p->px_dip, sysino, INTR_IDLE_STATE);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_intr_lock);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /*ARGSUSED*/
1440Sstevel@tonic-gate void
1450Sstevel@tonic-gate px_ib_intr_disable(px_ib_t *ib_p, devino_t ino, int wait)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	sysino_t	sysino;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_intr_lock);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	DBG(DBG_IB, ib_p->ib_px_p->px_dip, "px_ib_intr_disable: ino=%x\n", ino);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/* Disable the interrupt */
1540Sstevel@tonic-gate 	if (px_lib_intr_devino_to_sysino(ib_p->ib_px_p->px_dip, ino,
1550Sstevel@tonic-gate 	    &sysino) != DDI_SUCCESS) {
1560Sstevel@tonic-gate 		DBG(DBG_IB, ib_p->ib_px_p->px_dip,
1570Sstevel@tonic-gate 		    "px_ib_intr_disable: px_intr_devino_to_sysino() failed\n");
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_intr_lock);
1600Sstevel@tonic-gate 		return;
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	PX_INTR_DISABLE(ib_p->ib_px_p->px_dip, sysino);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_intr_lock);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 
169624Sschwartz void
1700Sstevel@tonic-gate px_ib_intr_dist_en(dev_info_t *dip, cpuid_t cpu_id, devino_t ino,
1710Sstevel@tonic-gate     boolean_t wait_flag)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	uint32_t	old_cpu_id;
1740Sstevel@tonic-gate 	sysino_t	sysino;
1750Sstevel@tonic-gate 	intr_valid_state_t	enabled = 0;
1761772Sjl139090 	hrtime_t	start_time, prev, curr, interval, jump;
1771772Sjl139090 	hrtime_t	intr_timeout;
1780Sstevel@tonic-gate 	intr_state_t	intr_state;
17927Sjchu 	int		e = DDI_SUCCESS;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	DBG(DBG_IB, dip, "px_ib_intr_dist_en: ino=0x%x\n", ino);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if (px_lib_intr_devino_to_sysino(dip, ino, &sysino) != DDI_SUCCESS) {
1840Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: "
1850Sstevel@tonic-gate 		    "px_intr_devino_to_sysino() failed, ino 0x%x\n", ino);
1860Sstevel@tonic-gate 		return;
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/* Skip enabling disabled interrupts */
1900Sstevel@tonic-gate 	if (px_lib_intr_getvalid(dip, sysino, &enabled) != DDI_SUCCESS) {
1910Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: px_intr_getvalid() "
1920Sstevel@tonic-gate 		    "failed, sysino 0x%x\n", sysino);
1930Sstevel@tonic-gate 		return;
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 	if (!enabled)
1960Sstevel@tonic-gate 		return;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	/* Done if redistributed onto the same cpuid */
1990Sstevel@tonic-gate 	if (px_lib_intr_gettarget(dip, sysino, &old_cpu_id) != DDI_SUCCESS) {
2000Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: "
2010Sstevel@tonic-gate 		    "px_intr_gettarget() failed\n");
2020Sstevel@tonic-gate 		return;
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate 	if (cpu_id == old_cpu_id)
2050Sstevel@tonic-gate 		return;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	if (!wait_flag)
2080Sstevel@tonic-gate 		goto done;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/* Busy wait on pending interrupts */
2110Sstevel@tonic-gate 	PX_INTR_DISABLE(dip, sysino);
2120Sstevel@tonic-gate 
2131772Sjl139090 	intr_timeout = px_intrpend_timeout;
2141772Sjl139090 	jump = TICK_TO_NSEC(xc_tick_jump_limit);
2151772Sjl139090 
2161772Sjl139090 	for (curr = start_time = gethrtime(); !panicstr &&
2170Sstevel@tonic-gate 	    ((e = px_lib_intr_getstate(dip, sysino, &intr_state)) ==
2185635Srameshc 	    DDI_SUCCESS) &&
2190Sstevel@tonic-gate 	    (intr_state == INTR_DELIVERED_STATE); /* */) {
2201772Sjl139090 		/*
2211772Sjl139090 		 * If we have a really large jump in hrtime, it is most
2221772Sjl139090 		 * probably because we entered the debugger (or OBP,
2231772Sjl139090 		 * in general). So, we adjust the timeout accordingly
2241772Sjl139090 		 * to prevent declaring an interrupt timeout. The
2251772Sjl139090 		 * master-interrupt mechanism in OBP should deliver
2261772Sjl139090 		 * the interrupts properly.
2271772Sjl139090 		 */
2281772Sjl139090 		prev = curr;
2291772Sjl139090 		curr = gethrtime();
2301772Sjl139090 		interval = curr - prev;
2311772Sjl139090 		if (interval > jump)
2321772Sjl139090 			intr_timeout += interval;
2331772Sjl139090 		if (curr - start_time > intr_timeout) {
2340Sstevel@tonic-gate 			cmn_err(CE_WARN,
235671Skrishnae 			    "%s%d: px_ib_intr_dist_en: sysino 0x%lx(ino 0x%x) "
2360Sstevel@tonic-gate 			    "from cpu id 0x%x to 0x%x timeout",
2370Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
2380Sstevel@tonic-gate 			    sysino, ino, old_cpu_id, cpu_id);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 			e = DDI_FAILURE;
2410Sstevel@tonic-gate 			break;
2420Sstevel@tonic-gate 		}
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	if (e != DDI_SUCCESS)
2460Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: failed, "
2470Sstevel@tonic-gate 		    "ino 0x%x sysino 0x%x\n", ino, sysino);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate done:
2500Sstevel@tonic-gate 	PX_INTR_ENABLE(dip, sysino, cpu_id);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
253624Sschwartz static void
254624Sschwartz px_ib_cpu_ticks_to_ih_nsec(px_ib_t *ib_p, px_ih_t *ih_p, uint32_t cpu_id)
255624Sschwartz {
256624Sschwartz 	extern kmutex_t pxintr_ks_template_lock;
257624Sschwartz 	hrtime_t ticks;
258624Sschwartz 
259624Sschwartz 	/*
260624Sschwartz 	 * Because we are updating two fields in ih_t we must lock
261624Sschwartz 	 * pxintr_ks_template_lock to prevent someone from reading the
262624Sschwartz 	 * kstats after we set ih_ticks to 0 and before we increment
263624Sschwartz 	 * ih_nsec to compensate.
264624Sschwartz 	 *
265624Sschwartz 	 * We must also protect against the interrupt arriving and incrementing
266624Sschwartz 	 * ih_ticks between the time we read it and when we reset it to 0.
267624Sschwartz 	 * To do this we use atomic_swap.
268624Sschwartz 	 */
269624Sschwartz 
270624Sschwartz 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
271624Sschwartz 
272624Sschwartz 	mutex_enter(&pxintr_ks_template_lock);
273624Sschwartz 	ticks = atomic_swap_64(&ih_p->ih_ticks, 0);
274624Sschwartz 	ih_p->ih_nsec += (uint64_t)tick2ns(ticks, cpu_id);
275624Sschwartz 	mutex_exit(&pxintr_ks_template_lock);
276624Sschwartz }
277624Sschwartz 
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate  * Redistribute interrupts of the specified weight. The first call has a weight
2810Sstevel@tonic-gate  * of weight_max, which can be used to trigger initialization for
2820Sstevel@tonic-gate  * redistribution. The inos with weight [weight_max, inf.) should be processed
2830Sstevel@tonic-gate  * on the "weight == weight_max" call.  This first call is followed by calls
2840Sstevel@tonic-gate  * of decreasing weights, inos of that weight should be processed.  The final
2850Sstevel@tonic-gate  * call specifies a weight of zero, this can be used to trigger processing of
2860Sstevel@tonic-gate  * stragglers.
2870Sstevel@tonic-gate  */
2880Sstevel@tonic-gate static void
2890Sstevel@tonic-gate px_ib_intr_redist(void *arg, int32_t weight_max, int32_t weight)
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate 	px_ib_t		*ib_p = (px_ib_t *)arg;
2920Sstevel@tonic-gate 	px_t		*px_p = ib_p->ib_px_p;
2930Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
2942973Sgovinda 	px_ino_t	*ino_p;
2952973Sgovinda 	px_ino_pil_t	*ipil_p;
2960Sstevel@tonic-gate 	px_ih_t		*ih_lst;
2970Sstevel@tonic-gate 	int32_t		dweight = 0;
2980Sstevel@tonic-gate 	int		i;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	/* Redistribute internal interrupts */
3010Sstevel@tonic-gate 	if (weight == 0) {
3021648Sjchu 		mutex_enter(&ib_p->ib_intr_lock);
3031648Sjchu 		px_ib_intr_dist_en(dip, intr_dist_cpuid(),
3041648Sjchu 		    px_p->px_inos[PX_INTR_PEC], B_FALSE);
3051648Sjchu 		mutex_exit(&ib_p->ib_intr_lock);
3063953Sscarter 
3073953Sscarter 		px_hp_intr_redist(px_p);
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	/* Redistribute device interrupts */
3110Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
31210053SEvan.Yan@Sun.COM 	px_msiq_redist(px_p);
3130Sstevel@tonic-gate 
3142973Sgovinda 	for (ino_p = ib_p->ib_ino_lst; ino_p; ino_p = ino_p->ino_next_p) {
3150Sstevel@tonic-gate 		/*
3160Sstevel@tonic-gate 		 * Recomputes the sum of interrupt weights of devices that
3170Sstevel@tonic-gate 		 * share the same ino upon first call marked by
3180Sstevel@tonic-gate 		 * (weight == weight_max).
3190Sstevel@tonic-gate 		 */
3200Sstevel@tonic-gate 		if (weight == weight_max) {
3210Sstevel@tonic-gate 			ino_p->ino_intr_weight = 0;
3222973Sgovinda 
3232973Sgovinda 			for (ipil_p = ino_p->ino_ipil_p; ipil_p;
3242973Sgovinda 			    ipil_p = ipil_p->ipil_next_p) {
3252973Sgovinda 				for (i = 0, ih_lst = ipil_p->ipil_ih_head;
3262973Sgovinda 				    i < ipil_p->ipil_ih_size; i++,
3272973Sgovinda 				    ih_lst = ih_lst->ih_next) {
3282973Sgovinda 					dweight = i_ddi_get_intr_weight(
3292973Sgovinda 					    ih_lst->ih_dip);
3302973Sgovinda 					if (dweight > 0)
3312973Sgovinda 						ino_p->ino_intr_weight +=
3322973Sgovinda 						    dweight;
3332973Sgovinda 				}
3340Sstevel@tonic-gate 			}
3350Sstevel@tonic-gate 		}
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 		/*
3380Sstevel@tonic-gate 		 * As part of redistributing weighted interrupts over cpus,
3390Sstevel@tonic-gate 		 * nexus redistributes device interrupts and updates
3400Sstevel@tonic-gate 		 * cpu weight. The purpose is for the most light weighted
3410Sstevel@tonic-gate 		 * cpu to take the next interrupt and gain weight, therefore
3420Sstevel@tonic-gate 		 * attention demanding device gains more cpu attention by
3430Sstevel@tonic-gate 		 * making itself heavy.
3440Sstevel@tonic-gate 		 */
3450Sstevel@tonic-gate 		if ((weight == ino_p->ino_intr_weight) ||
3460Sstevel@tonic-gate 		    ((weight >= weight_max) &&
3470Sstevel@tonic-gate 		    (ino_p->ino_intr_weight >= weight_max))) {
34810053SEvan.Yan@Sun.COM 			uint32_t orig_cpuid = ino_p->ino_cpuid;
34910053SEvan.Yan@Sun.COM 
3500Sstevel@tonic-gate 			if (cpu[orig_cpuid] == NULL)
3510Sstevel@tonic-gate 				orig_cpuid = CPU->cpu_id;
3520Sstevel@tonic-gate 
35310053SEvan.Yan@Sun.COM 			DBG(DBG_IB, dip, "px_ib_intr_redist: sysino 0x%llx "
35410053SEvan.Yan@Sun.COM 			    "current cpuid 0x%x current default cpuid 0x%x\n",
35510053SEvan.Yan@Sun.COM 			    ino_p->ino_sysino, ino_p->ino_cpuid,
35610053SEvan.Yan@Sun.COM 			    ino_p->ino_default_cpuid);
35710053SEvan.Yan@Sun.COM 
35810053SEvan.Yan@Sun.COM 			/* select target cpuid and mark ino established */
35910053SEvan.Yan@Sun.COM 			if (ino_p->ino_default_cpuid == -1)
36010053SEvan.Yan@Sun.COM 				ino_p->ino_cpuid = ino_p->ino_default_cpuid =
36110053SEvan.Yan@Sun.COM 				    intr_dist_cpuid();
36210053SEvan.Yan@Sun.COM 			else if ((ino_p->ino_cpuid !=
36310053SEvan.Yan@Sun.COM 			    ino_p->ino_default_cpuid) &&
36410279SGovinda.Tatti@Sun.COM 			    cpu[ino_p->ino_default_cpuid] &&
36510279SGovinda.Tatti@Sun.COM 			    cpu_intr_on(cpu[ino_p->ino_default_cpuid]))
36610053SEvan.Yan@Sun.COM 				ino_p->ino_cpuid = ino_p->ino_default_cpuid;
36710053SEvan.Yan@Sun.COM 			else if (!cpu_intr_on(cpu[ino_p->ino_cpuid]))
36810053SEvan.Yan@Sun.COM 				ino_p->ino_cpuid = intr_dist_cpuid();
36910053SEvan.Yan@Sun.COM 
37010053SEvan.Yan@Sun.COM 			DBG(DBG_IB, dip, "px_ib_intr_redist: sysino 0x%llx "
37110053SEvan.Yan@Sun.COM 			    "new cpuid 0x%x new default cpuid 0x%x\n",
37210053SEvan.Yan@Sun.COM 			    ino_p->ino_sysino, ino_p->ino_cpuid,
37310053SEvan.Yan@Sun.COM 			    ino_p->ino_default_cpuid);
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 			/* Add device weight to targeted cpu. */
3762973Sgovinda 			for (ipil_p = ino_p->ino_ipil_p; ipil_p;
3772973Sgovinda 			    ipil_p = ipil_p->ipil_next_p) {
3782973Sgovinda 				for (i = 0, ih_lst = ipil_p->ipil_ih_head;
3792973Sgovinda 				    i < ipil_p->ipil_ih_size; i++,
3802973Sgovinda 				    ih_lst = ih_lst->ih_next) {
3810Sstevel@tonic-gate 
3822973Sgovinda 					dweight = i_ddi_get_intr_weight(
3832973Sgovinda 					    ih_lst->ih_dip);
3842973Sgovinda 					intr_dist_cpuid_add_device_weight(
3852973Sgovinda 					    ino_p->ino_cpuid, ih_lst->ih_dip,
3862973Sgovinda 					    dweight);
3870Sstevel@tonic-gate 
3882973Sgovinda 					/*
3892973Sgovinda 					 * Different cpus may have different
3902973Sgovinda 					 * clock speeds. to account for this,
3912973Sgovinda 					 * whenever an interrupt is moved to a
3922973Sgovinda 					 * new CPU, we convert the accumulated
3932973Sgovinda 					 * ticks into nsec, based upon the clock
3942973Sgovinda 					 * rate of the prior CPU.
3952973Sgovinda 					 *
3962973Sgovinda 					 * It is possible that the prior CPU no
3972973Sgovinda 					 * longer exists. In this case, fall
3982973Sgovinda 					 * back to using this CPU's clock rate.
3992973Sgovinda 					 *
4002973Sgovinda 					 * Note that the value in ih_ticks has
4012973Sgovinda 					 * already been corrected for any power
4022973Sgovinda 					 * savings mode which might have been
4032973Sgovinda 					 * in effect.
4042973Sgovinda 					 */
4052973Sgovinda 					px_ib_cpu_ticks_to_ih_nsec(ib_p, ih_lst,
4062973Sgovinda 					    orig_cpuid);
4072973Sgovinda 				}
4080Sstevel@tonic-gate 			}
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 			/* enable interrupt on new targeted cpu */
4110Sstevel@tonic-gate 			px_ib_intr_dist_en(dip, ino_p->ino_cpuid,
4120Sstevel@tonic-gate 			    ino_p->ino_ino, B_TRUE);
4130Sstevel@tonic-gate 		}
4140Sstevel@tonic-gate 	}
4150Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate /*
4190Sstevel@tonic-gate  * Reset interrupts to IDLE.  This function is called during
4200Sstevel@tonic-gate  * panic handling after redistributing interrupts; it's needed to
4210Sstevel@tonic-gate  * support dumping to network devices after 'sync' from OBP.
4220Sstevel@tonic-gate  *
4230Sstevel@tonic-gate  * N.B.  This routine runs in a context where all other threads
4240Sstevel@tonic-gate  * are permanently suspended.
4250Sstevel@tonic-gate  */
4260Sstevel@tonic-gate static uint_t
4270Sstevel@tonic-gate px_ib_intr_reset(void *arg)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	px_ib_t		*ib_p = (px_ib_t *)arg;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	DBG(DBG_IB, ib_p->ib_px_p->px_dip, "px_ib_intr_reset\n");
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	if (px_lib_intr_reset(ib_p->ib_px_p->px_dip) != DDI_SUCCESS)
4340Sstevel@tonic-gate 		return (BF_FATAL);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	return (BF_NONE);
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate /*
4402973Sgovinda  * Locate px_ino_t structure on ib_p->ib_ino_lst according to ino#
4410Sstevel@tonic-gate  * returns NULL if not found.
4420Sstevel@tonic-gate  */
4432973Sgovinda px_ino_t *
4440Sstevel@tonic-gate px_ib_locate_ino(px_ib_t *ib_p, devino_t ino_num)
4450Sstevel@tonic-gate {
4462973Sgovinda 	px_ino_t	*ino_p = ib_p->ib_ino_lst;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
4490Sstevel@tonic-gate 
4505635Srameshc 	for (; ino_p && ino_p->ino_ino != ino_num; ino_p = ino_p->ino_next_p)
4515635Srameshc 		;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	return (ino_p);
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate 
45610053SEvan.Yan@Sun.COM px_ino_t *
45710053SEvan.Yan@Sun.COM px_ib_alloc_ino(px_ib_t *ib_p, devino_t ino_num)
45810053SEvan.Yan@Sun.COM {
45910053SEvan.Yan@Sun.COM 	sysino_t	sysino;
46010053SEvan.Yan@Sun.COM 	px_ino_t	*ino_p;
46110053SEvan.Yan@Sun.COM 
46210053SEvan.Yan@Sun.COM 	if (px_lib_intr_devino_to_sysino(ib_p->ib_px_p->px_dip,
46310053SEvan.Yan@Sun.COM 	    ino_num, &sysino) != DDI_SUCCESS)
46410053SEvan.Yan@Sun.COM 		return (NULL);
46510053SEvan.Yan@Sun.COM 
46610053SEvan.Yan@Sun.COM 	ino_p = kmem_zalloc(sizeof (px_ino_t), KM_SLEEP);
46710053SEvan.Yan@Sun.COM 
46810053SEvan.Yan@Sun.COM 	ino_p->ino_next_p = ib_p->ib_ino_lst;
46910053SEvan.Yan@Sun.COM 	ib_p->ib_ino_lst = ino_p;
47010053SEvan.Yan@Sun.COM 
47110053SEvan.Yan@Sun.COM 	ino_p->ino_ino = ino_num;
47210053SEvan.Yan@Sun.COM 	ino_p->ino_sysino = sysino;
47310053SEvan.Yan@Sun.COM 	ino_p->ino_ib_p = ib_p;
47410053SEvan.Yan@Sun.COM 	ino_p->ino_unclaimed_intrs = 0;
47510053SEvan.Yan@Sun.COM 	ino_p->ino_lopil = 0;
47610053SEvan.Yan@Sun.COM 	ino_p->ino_cpuid = ino_p->ino_default_cpuid = (cpuid_t)-1;
47710053SEvan.Yan@Sun.COM 
47810053SEvan.Yan@Sun.COM 	return (ino_p);
47910053SEvan.Yan@Sun.COM }
48010053SEvan.Yan@Sun.COM 
4812973Sgovinda px_ino_pil_t *
4822973Sgovinda px_ib_new_ino_pil(px_ib_t *ib_p, devino_t ino_num, uint_t pil, px_ih_t *ih_p)
4830Sstevel@tonic-gate {
4842973Sgovinda 	px_ino_pil_t	*ipil_p = kmem_zalloc(sizeof (px_ino_pil_t), KM_SLEEP);
4852973Sgovinda 	px_ino_t	*ino_p;
4862973Sgovinda 
48710053SEvan.Yan@Sun.COM 	if ((ino_p = px_ib_locate_ino(ib_p, ino_num)) == NULL)
48810053SEvan.Yan@Sun.COM 		ino_p = px_ib_alloc_ino(ib_p, ino_num);
4892973Sgovinda 
49010053SEvan.Yan@Sun.COM 	ASSERT(ino_p != NULL);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	ih_p->ih_next = ih_p;
4932973Sgovinda 	ipil_p->ipil_pil = pil;
4942973Sgovinda 	ipil_p->ipil_ih_head = ih_p;
4952973Sgovinda 	ipil_p->ipil_ih_tail = ih_p;
4962973Sgovinda 	ipil_p->ipil_ih_start = ih_p;
4972973Sgovinda 	ipil_p->ipil_ih_size = 1;
4982973Sgovinda 	ipil_p->ipil_ino_p = ino_p;
4990Sstevel@tonic-gate 
5002973Sgovinda 	ipil_p->ipil_next_p = ino_p->ino_ipil_p;
5012973Sgovinda 	ino_p->ino_ipil_p = ipil_p;
5022973Sgovinda 	ino_p->ino_ipil_size++;
5030Sstevel@tonic-gate 
50410053SEvan.Yan@Sun.COM 	if ((ino_p->ino_lopil == 0) || (ino_p->ino_lopil > pil))
5052973Sgovinda 		ino_p->ino_lopil = pil;
5062973Sgovinda 
5072973Sgovinda 	return (ipil_p);
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate void
5112973Sgovinda px_ib_delete_ino_pil(px_ib_t *ib_p, px_ino_pil_t *ipil_p)
5120Sstevel@tonic-gate {
5132973Sgovinda 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
5142973Sgovinda 	ushort_t	pil = ipil_p->ipil_pil;
5152973Sgovinda 	px_ino_pil_t	*prev, *next;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
5180Sstevel@tonic-gate 
5192973Sgovinda 	if (ino_p->ino_ipil_p == ipil_p)
5202973Sgovinda 		ino_p->ino_ipil_p = ipil_p->ipil_next_p;
5210Sstevel@tonic-gate 	else {
5222973Sgovinda 		for (prev = next = ino_p->ino_ipil_p; next != ipil_p;
5235635Srameshc 		    prev = next, next = next->ipil_next_p)
5245635Srameshc 			;
5252973Sgovinda 
5262973Sgovinda 		if (prev)
5272973Sgovinda 			prev->ipil_next_p = ipil_p->ipil_next_p;
5282973Sgovinda 	}
5292973Sgovinda 
5302973Sgovinda 	kmem_free(ipil_p, sizeof (px_ino_pil_t));
5312973Sgovinda 
5325962Srameshc 	if ((--ino_p->ino_ipil_size) && (ino_p->ino_lopil == pil)) {
5335962Srameshc 		for (next = ino_p->ino_ipil_p, pil = next->ipil_pil;
5345962Srameshc 		    next; next = next->ipil_next_p) {
5355962Srameshc 
5362973Sgovinda 			if (pil > next->ipil_pil)
5372973Sgovinda 				pil = next->ipil_pil;
5382973Sgovinda 		}
53910053SEvan.Yan@Sun.COM 
5405962Srameshc 		/*
5415962Srameshc 		 * Value stored in pil should be the lowest pil.
5425962Srameshc 		 */
5432973Sgovinda 		ino_p->ino_lopil = pil;
5442973Sgovinda 	}
5452973Sgovinda 
5465962Srameshc 	if (ino_p->ino_ipil_size)
5472973Sgovinda 		return;
5482973Sgovinda 
54910053SEvan.Yan@Sun.COM 	ino_p->ino_lopil = 0;
55010053SEvan.Yan@Sun.COM 
55110053SEvan.Yan@Sun.COM 	if (ino_p->ino_msiq_p)
55210053SEvan.Yan@Sun.COM 		return;
55310053SEvan.Yan@Sun.COM 
5542973Sgovinda 	if (ib_p->ib_ino_lst == ino_p)
5552973Sgovinda 		ib_p->ib_ino_lst = ino_p->ino_next_p;
5562973Sgovinda 	else {
5572973Sgovinda 		px_ino_t	*list = ib_p->ib_ino_lst;
5582973Sgovinda 
5595635Srameshc 		for (; list->ino_next_p != ino_p; list = list->ino_next_p)
5605635Srameshc 			;
5612973Sgovinda 		list->ino_next_p = ino_p->ino_next_p;
5620Sstevel@tonic-gate 	}
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate /*
5660Sstevel@tonic-gate  * Free all ino when we are detaching.
5670Sstevel@tonic-gate  */
5680Sstevel@tonic-gate void
5690Sstevel@tonic-gate px_ib_free_ino_all(px_ib_t *ib_p)
5700Sstevel@tonic-gate {
5712973Sgovinda 	px_ino_t	*ino_p = ib_p->ib_ino_lst;
5722973Sgovinda 	px_ino_t	*next = NULL;
5730Sstevel@tonic-gate 
5742973Sgovinda 	while (ino_p) {
5752973Sgovinda 		next = ino_p->ino_next_p;
5762973Sgovinda 		kmem_free(ino_p, sizeof (px_ino_t));
5772973Sgovinda 		ino_p = next;
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate 
5812973Sgovinda /*
5822973Sgovinda  * Locate px_ino_pil_t structure on ino_p->ino_ipil_p according to ino#
5832973Sgovinda  * returns NULL if not found.
5842973Sgovinda  */
5852973Sgovinda px_ino_pil_t *
5862973Sgovinda px_ib_ino_locate_ipil(px_ino_t *ino_p, uint_t pil)
5872973Sgovinda {
5882973Sgovinda 	px_ino_pil_t	*ipil_p = ino_p->ino_ipil_p;
5892973Sgovinda 
5905635Srameshc 	for (; ipil_p && ipil_p->ipil_pil != pil; ipil_p = ipil_p->ipil_next_p)
5915635Srameshc 		;
5922973Sgovinda 
5932973Sgovinda 	return (ipil_p);
5942973Sgovinda }
5952973Sgovinda 
5960Sstevel@tonic-gate int
5972973Sgovinda px_ib_ino_add_intr(px_t *px_p, px_ino_pil_t *ipil_p, px_ih_t *ih_p)
5980Sstevel@tonic-gate {
5992973Sgovinda 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
6000Sstevel@tonic-gate 	px_ib_t		*ib_p = ino_p->ino_ib_p;
6010Sstevel@tonic-gate 	devino_t	ino = ino_p->ino_ino;
6020Sstevel@tonic-gate 	sysino_t	sysino = ino_p->ino_sysino;
6030Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
6040Sstevel@tonic-gate 	cpuid_t		curr_cpu;
6050Sstevel@tonic-gate 	hrtime_t	start_time;
6060Sstevel@tonic-gate 	intr_state_t	intr_state;
6070Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
6100Sstevel@tonic-gate 	ASSERT(ib_p == px_p->px_ib_p);
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	DBG(DBG_IB, dip, "px_ib_ino_add_intr ino=%x\n", ino_p->ino_ino);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	/* Disable the interrupt */
6150Sstevel@tonic-gate 	if ((ret = px_lib_intr_gettarget(dip, sysino,
6160Sstevel@tonic-gate 	    &curr_cpu)) != DDI_SUCCESS) {
6170Sstevel@tonic-gate 		DBG(DBG_IB, dip,
6180Sstevel@tonic-gate 		    "px_ib_ino_add_intr px_intr_gettarget() failed\n");
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		return (ret);
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	PX_INTR_DISABLE(dip, sysino);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	/* Busy wait on pending interrupt */
6260Sstevel@tonic-gate 	for (start_time = gethrtime(); !panicstr &&
6270Sstevel@tonic-gate 	    ((ret = px_lib_intr_getstate(dip, sysino, &intr_state))
6280Sstevel@tonic-gate 	    == DDI_SUCCESS) && (intr_state == INTR_DELIVERED_STATE); /* */) {
6290Sstevel@tonic-gate 		if (gethrtime() - start_time > px_intrpend_timeout) {
6300Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: px_ib_ino_add_intr: pending "
631671Skrishnae 			    "sysino 0x%lx(ino 0x%x) timeout",
6320Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
6330Sstevel@tonic-gate 			    sysino, ino);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 			ret = DDI_FAILURE;
6360Sstevel@tonic-gate 			break;
6370Sstevel@tonic-gate 		}
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 
6402973Sgovinda 	/*
6412973Sgovinda 	 * If the interrupt was previously blocked (left in pending state)
6422973Sgovinda 	 * because of jabber we need to clear the pending state in case the
6432973Sgovinda 	 * jabber has gone away.
6442973Sgovinda 	 */
6452973Sgovinda 	if (ino_p->ino_unclaimed_intrs > px_unclaimed_intr_max) {
6462973Sgovinda 		cmn_err(CE_WARN,
6472973Sgovinda 		    "%s%d: px_ib_ino_add_intr: ino 0x%x has been unblocked",
6482973Sgovinda 		    ddi_driver_name(dip), ddi_get_instance(dip), ino);
6492973Sgovinda 
6502973Sgovinda 		ino_p->ino_unclaimed_intrs = 0;
6512973Sgovinda 		ret = px_lib_intr_setstate(dip, sysino, INTR_IDLE_STATE);
6522973Sgovinda 	}
6532973Sgovinda 
6540Sstevel@tonic-gate 	if (ret != DDI_SUCCESS) {
6550Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_ino_add_intr: failed, "
6560Sstevel@tonic-gate 		    "ino 0x%x sysino 0x%x\n", ino, sysino);
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 		return (ret);
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 
6612973Sgovinda 	/* Link up px_ih_t */
6622973Sgovinda 	ih_p->ih_next = ipil_p->ipil_ih_head;
6632973Sgovinda 	ipil_p->ipil_ih_tail->ih_next = ih_p;
6642973Sgovinda 	ipil_p->ipil_ih_tail = ih_p;
6650Sstevel@tonic-gate 
6662973Sgovinda 	ipil_p->ipil_ih_start = ipil_p->ipil_ih_head;
6672973Sgovinda 	ipil_p->ipil_ih_size++;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	/* Re-enable interrupt */
6700Sstevel@tonic-gate 	PX_INTR_ENABLE(dip, sysino, curr_cpu);
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	return (ret);
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate /*
6762973Sgovinda  * Removes px_ih_t from the ino's link list.
6770Sstevel@tonic-gate  * uses hardware mutex to lock out interrupt threads.
6780Sstevel@tonic-gate  * Side effects: interrupt belongs to that ino is turned off on return.
6790Sstevel@tonic-gate  * if we are sharing PX slot with other inos, the caller needs
6800Sstevel@tonic-gate  * to turn it back on.
6810Sstevel@tonic-gate  */
6820Sstevel@tonic-gate int
6832973Sgovinda px_ib_ino_rem_intr(px_t *px_p, px_ino_pil_t *ipil_p, px_ih_t *ih_p)
6840Sstevel@tonic-gate {
6852973Sgovinda 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
6860Sstevel@tonic-gate 	devino_t	ino = ino_p->ino_ino;
6870Sstevel@tonic-gate 	sysino_t	sysino = ino_p->ino_sysino;
6880Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
6892973Sgovinda 	px_ih_t		*ih_lst = ipil_p->ipil_ih_head;
6900Sstevel@tonic-gate 	hrtime_t	start_time;
6910Sstevel@tonic-gate 	intr_state_t	intr_state;
6920Sstevel@tonic-gate 	int		i, ret = DDI_SUCCESS;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ino_p->ino_ib_p->ib_ino_lst_mutex));
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	DBG(DBG_IB, px_p->px_dip, "px_ib_ino_rem_intr ino=%x\n",
6970Sstevel@tonic-gate 	    ino_p->ino_ino);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	/* Disable the interrupt */
7000Sstevel@tonic-gate 	PX_INTR_DISABLE(px_p->px_dip, sysino);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	/* Busy wait on pending interrupt */
7030Sstevel@tonic-gate 	for (start_time = gethrtime(); !panicstr &&
7040Sstevel@tonic-gate 	    ((ret = px_lib_intr_getstate(dip, sysino, &intr_state))
7050Sstevel@tonic-gate 	    == DDI_SUCCESS) && (intr_state == INTR_DELIVERED_STATE); /* */) {
7060Sstevel@tonic-gate 		if (gethrtime() - start_time > px_intrpend_timeout) {
7070Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: px_ib_ino_rem_intr: pending "
708671Skrishnae 			    "sysino 0x%lx(ino 0x%x) timeout",
7090Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
7100Sstevel@tonic-gate 			    sysino, ino);
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 			ret = DDI_FAILURE;
7130Sstevel@tonic-gate 			break;
7140Sstevel@tonic-gate 		}
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 
7172973Sgovinda 	/*
7182973Sgovinda 	 * If the interrupt was previously blocked (left in pending state)
7192973Sgovinda 	 * because of jabber we need to clear the pending state in case the
7202973Sgovinda 	 * jabber has gone away.
7212973Sgovinda 	 */
722*11836SDaniel.Ice@Sun.COM 	if (ret == DDI_SUCCESS &&
723*11836SDaniel.Ice@Sun.COM 	    ino_p->ino_unclaimed_intrs > px_unclaimed_intr_max) {
7242973Sgovinda 		cmn_err(CE_WARN, "%s%d: px_ib_ino_rem_intr: "
7252973Sgovinda 		    "ino 0x%x has been unblocked",
7262973Sgovinda 		    ddi_driver_name(dip), ddi_get_instance(dip), ino);
7272973Sgovinda 
7282973Sgovinda 		ino_p->ino_unclaimed_intrs = 0;
7292973Sgovinda 		ret = px_lib_intr_setstate(dip, sysino, INTR_IDLE_STATE);
7302973Sgovinda 	}
7312973Sgovinda 
7320Sstevel@tonic-gate 	if (ret != DDI_SUCCESS) {
7330Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_ino_rem_intr: failed, "
7340Sstevel@tonic-gate 		    "ino 0x%x sysino 0x%x\n", ino, sysino);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 		return (ret);
7370Sstevel@tonic-gate 	}
7380Sstevel@tonic-gate 
739*11836SDaniel.Ice@Sun.COM 	if (ipil_p->ipil_ih_size == 1) {
740*11836SDaniel.Ice@Sun.COM 		if (ih_lst != ih_p)
741*11836SDaniel.Ice@Sun.COM 			goto not_found;
742*11836SDaniel.Ice@Sun.COM 
743*11836SDaniel.Ice@Sun.COM 		/* No need to set head/tail as ino_p will be freed */
744*11836SDaniel.Ice@Sun.COM 		goto reset;
745*11836SDaniel.Ice@Sun.COM 	}
746*11836SDaniel.Ice@Sun.COM 
7470Sstevel@tonic-gate 	/* Search the link list for ih_p */
7482973Sgovinda 	for (i = 0; (i < ipil_p->ipil_ih_size) &&
7495635Srameshc 	    (ih_lst->ih_next != ih_p); i++, ih_lst = ih_lst->ih_next)
7505635Srameshc 		;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	if (ih_lst->ih_next != ih_p)
7530Sstevel@tonic-gate 		goto not_found;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	/* Remove ih_p from the link list and maintain the head/tail */
7560Sstevel@tonic-gate 	ih_lst->ih_next = ih_p->ih_next;
7570Sstevel@tonic-gate 
7582973Sgovinda 	if (ipil_p->ipil_ih_head == ih_p)
7592973Sgovinda 		ipil_p->ipil_ih_head = ih_p->ih_next;
7602973Sgovinda 	if (ipil_p->ipil_ih_tail == ih_p)
7612973Sgovinda 		ipil_p->ipil_ih_tail = ih_lst;
7620Sstevel@tonic-gate 
7632973Sgovinda 	ipil_p->ipil_ih_start = ipil_p->ipil_ih_head;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate reset:
7660Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
7670Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
7680Sstevel@tonic-gate 	if (ih_p->ih_ksp != NULL)
7690Sstevel@tonic-gate 		kstat_delete(ih_p->ih_ksp);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (px_ih_t));
7722973Sgovinda 	ipil_p->ipil_ih_size--;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	return (ret);
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate not_found:
7770Sstevel@tonic-gate 	DBG(DBG_R_INTX, ino_p->ino_ib_p->ib_px_p->px_dip,
7785635Srameshc 	    "ino_p=%x does not have ih_p=%x\n", ino_p, ih_p);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	return (DDI_FAILURE);
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate px_ih_t *
7842973Sgovinda px_ib_intr_locate_ih(px_ino_pil_t *ipil_p, dev_info_t *rdip,
7850Sstevel@tonic-gate     uint32_t inum, msiq_rec_type_t rec_type, msgcode_t msg_code)
7860Sstevel@tonic-gate {
7872973Sgovinda 	px_ih_t	*ih_p = ipil_p->ipil_ih_head;
7880Sstevel@tonic-gate 	int	i;
7890Sstevel@tonic-gate 
7902973Sgovinda 	for (i = 0; i < ipil_p->ipil_ih_size; i++, ih_p = ih_p->ih_next) {
7912973Sgovinda 		if ((ih_p->ih_dip == rdip) && (ih_p->ih_inum == inum) &&
7922973Sgovinda 		    (ih_p->ih_rec_type == rec_type) &&
7932973Sgovinda 		    (ih_p->ih_msg_code == msg_code))
7942973Sgovinda 			return (ih_p);
7950Sstevel@tonic-gate 	}
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	return ((px_ih_t *)NULL);
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate px_ih_t *
8010Sstevel@tonic-gate px_ib_alloc_ih(dev_info_t *rdip, uint32_t inum,
8020Sstevel@tonic-gate     uint_t (*int_handler)(caddr_t int_handler_arg1, caddr_t int_handler_arg2),
8030Sstevel@tonic-gate     caddr_t int_handler_arg1, caddr_t int_handler_arg2,
8040Sstevel@tonic-gate     msiq_rec_type_t rec_type, msgcode_t msg_code)
8050Sstevel@tonic-gate {
8060Sstevel@tonic-gate 	px_ih_t	*ih_p;
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	ih_p = kmem_alloc(sizeof (px_ih_t), KM_SLEEP);
8090Sstevel@tonic-gate 	ih_p->ih_dip = rdip;
8100Sstevel@tonic-gate 	ih_p->ih_inum = inum;
8110Sstevel@tonic-gate 	ih_p->ih_intr_state = PX_INTR_STATE_DISABLE;
81211520SScott.Carter@Sun.COM 	ih_p->ih_intr_flags = PX_INTR_IDLE;
8130Sstevel@tonic-gate 	ih_p->ih_handler = int_handler;
8140Sstevel@tonic-gate 	ih_p->ih_handler_arg1 = int_handler_arg1;
8150Sstevel@tonic-gate 	ih_p->ih_handler_arg2 = int_handler_arg2;
8160Sstevel@tonic-gate 	ih_p->ih_config_handle = NULL;
8170Sstevel@tonic-gate 	ih_p->ih_rec_type = rec_type;
8180Sstevel@tonic-gate 	ih_p->ih_msg_code = msg_code;
8190Sstevel@tonic-gate 	ih_p->ih_nsec = 0;
8200Sstevel@tonic-gate 	ih_p->ih_ticks = 0;
8210Sstevel@tonic-gate 	ih_p->ih_ksp = NULL;
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	return (ih_p);
8240Sstevel@tonic-gate }
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate int
8270Sstevel@tonic-gate px_ib_update_intr_state(px_t *px_p, dev_info_t *rdip,
8282973Sgovinda     uint_t inum, devino_t ino, uint_t pil,
8292973Sgovinda     uint_t new_intr_state, msiq_rec_type_t rec_type,
8302973Sgovinda     msgcode_t msg_code)
8310Sstevel@tonic-gate {
8320Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
8332973Sgovinda 	px_ino_t	*ino_p;
8342973Sgovinda 	px_ino_pil_t	*ipil_p;
8350Sstevel@tonic-gate 	px_ih_t		*ih_p;
8360Sstevel@tonic-gate 	int		ret = DDI_FAILURE;
8370Sstevel@tonic-gate 
8382973Sgovinda 	DBG(DBG_IB, px_p->px_dip, "px_ib_update_intr_state: %s%d "
8392973Sgovinda 	    "inum %x devino %x pil %x state %x\n", ddi_driver_name(rdip),
8402973Sgovinda 	    ddi_get_instance(rdip), inum, ino, pil, new_intr_state);
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
8430Sstevel@tonic-gate 
8442973Sgovinda 	ino_p = px_ib_locate_ino(ib_p, ino);
8452973Sgovinda 	if (ino_p && (ipil_p = px_ib_ino_locate_ipil(ino_p, pil))) {
8462973Sgovinda 		if (ih_p = px_ib_intr_locate_ih(ipil_p, rdip, inum, rec_type,
847909Segillett 		    msg_code)) {
8480Sstevel@tonic-gate 			ih_p->ih_intr_state = new_intr_state;
8490Sstevel@tonic-gate 			ret = DDI_SUCCESS;
8500Sstevel@tonic-gate 		}
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
8540Sstevel@tonic-gate 	return (ret);
8550Sstevel@tonic-gate }
856624Sschwartz 
857624Sschwartz 
85810053SEvan.Yan@Sun.COM /*
85910053SEvan.Yan@Sun.COM  * Get interrupt CPU for a given ino.
86010053SEvan.Yan@Sun.COM  * Return info only for inos which are already mapped to devices.
86110053SEvan.Yan@Sun.COM  */
86210053SEvan.Yan@Sun.COM /*ARGSUSED*/
86310053SEvan.Yan@Sun.COM int
86410053SEvan.Yan@Sun.COM px_ib_get_intr_target(px_t *px_p, devino_t ino, cpuid_t *cpu_id_p)
86510053SEvan.Yan@Sun.COM {
86610053SEvan.Yan@Sun.COM 	dev_info_t	*dip = px_p->px_dip;
86710053SEvan.Yan@Sun.COM 	sysino_t	sysino;
86810053SEvan.Yan@Sun.COM 	int		ret;
86910053SEvan.Yan@Sun.COM 
87010053SEvan.Yan@Sun.COM 	DBG(DBG_IB, px_p->px_dip, "px_ib_get_intr_target: devino %x\n", ino);
87110053SEvan.Yan@Sun.COM 
87210053SEvan.Yan@Sun.COM 	/* Convert leaf-wide intr to system-wide intr */
87310053SEvan.Yan@Sun.COM 	if (px_lib_intr_devino_to_sysino(dip, ino, &sysino) != DDI_SUCCESS)
87410053SEvan.Yan@Sun.COM 		return (DDI_FAILURE);
87510053SEvan.Yan@Sun.COM 
87610053SEvan.Yan@Sun.COM 	ret = px_lib_intr_gettarget(dip, sysino, cpu_id_p);
87710053SEvan.Yan@Sun.COM 
87810053SEvan.Yan@Sun.COM 	DBG(DBG_IB, px_p->px_dip, "px_ib_get_intr_target: cpu_id %x\n",
87910053SEvan.Yan@Sun.COM 	    *cpu_id_p);
88010053SEvan.Yan@Sun.COM 
88110053SEvan.Yan@Sun.COM 	return (ret);
88210053SEvan.Yan@Sun.COM }
88310053SEvan.Yan@Sun.COM 
88410053SEvan.Yan@Sun.COM 
88510053SEvan.Yan@Sun.COM /*
88610053SEvan.Yan@Sun.COM  * Associate a new CPU with a given ino.
88710053SEvan.Yan@Sun.COM  * Operate only on INOs which are already mapped to devices.
88810053SEvan.Yan@Sun.COM  */
88910053SEvan.Yan@Sun.COM int
89010053SEvan.Yan@Sun.COM px_ib_set_intr_target(px_t *px_p, devino_t ino, cpuid_t cpu_id)
89110053SEvan.Yan@Sun.COM {
89210053SEvan.Yan@Sun.COM 	dev_info_t		*dip = px_p->px_dip;
89310053SEvan.Yan@Sun.COM 	cpuid_t			old_cpu_id;
89410053SEvan.Yan@Sun.COM 	sysino_t		sysino;
89510053SEvan.Yan@Sun.COM 	int			ret = DDI_SUCCESS;
89610053SEvan.Yan@Sun.COM 	extern const int	_ncpu;
89710053SEvan.Yan@Sun.COM 	extern cpu_t		*cpu[];
89810053SEvan.Yan@Sun.COM 
89910053SEvan.Yan@Sun.COM 	DBG(DBG_IB, px_p->px_dip, "px_ib_set_intr_target: devino %x "
90010053SEvan.Yan@Sun.COM 	    "cpu_id %x\n", ino, cpu_id);
90110053SEvan.Yan@Sun.COM 
90210053SEvan.Yan@Sun.COM 	mutex_enter(&cpu_lock);
90310053SEvan.Yan@Sun.COM 
90410053SEvan.Yan@Sun.COM 	/* Convert leaf-wide intr to system-wide intr */
90510053SEvan.Yan@Sun.COM 	if (px_lib_intr_devino_to_sysino(dip, ino, &sysino) != DDI_SUCCESS) {
90610053SEvan.Yan@Sun.COM 		ret = DDI_FAILURE;
90710053SEvan.Yan@Sun.COM 		goto done;
90810053SEvan.Yan@Sun.COM 	}
90910053SEvan.Yan@Sun.COM 
91010053SEvan.Yan@Sun.COM 	if (px_lib_intr_gettarget(dip, sysino, &old_cpu_id) != DDI_SUCCESS) {
91110053SEvan.Yan@Sun.COM 		ret = DDI_FAILURE;
91210053SEvan.Yan@Sun.COM 		goto done;
91310053SEvan.Yan@Sun.COM 	}
91410053SEvan.Yan@Sun.COM 
91510053SEvan.Yan@Sun.COM 	/*
91610053SEvan.Yan@Sun.COM 	 * Get lock, validate cpu and write it.
91710053SEvan.Yan@Sun.COM 	 */
91810053SEvan.Yan@Sun.COM 	if ((cpu_id < _ncpu) && (cpu[cpu_id] && cpu_is_online(cpu[cpu_id]))) {
91910053SEvan.Yan@Sun.COM 		DBG(DBG_IB, dip, "px_ib_set_intr_target: Enabling CPU %d\n",
92010053SEvan.Yan@Sun.COM 		    cpu_id);
92110053SEvan.Yan@Sun.COM 		px_ib_intr_dist_en(dip, cpu_id, ino, B_TRUE);
92210053SEvan.Yan@Sun.COM 		px_ib_log_new_cpu(px_p->px_ib_p, old_cpu_id, cpu_id, ino);
92310053SEvan.Yan@Sun.COM 	} else {	/* Invalid cpu */
92410053SEvan.Yan@Sun.COM 		DBG(DBG_IB, dip, "px_ib_set_intr_target: Invalid cpuid %x\n",
92510053SEvan.Yan@Sun.COM 		    cpu_id);
92610053SEvan.Yan@Sun.COM 		ret = DDI_EINVAL;
92710053SEvan.Yan@Sun.COM 	}
92810053SEvan.Yan@Sun.COM 
92910053SEvan.Yan@Sun.COM done:
93010053SEvan.Yan@Sun.COM 	mutex_exit(&cpu_lock);
93110053SEvan.Yan@Sun.COM 	return (ret);
93210053SEvan.Yan@Sun.COM }
93310053SEvan.Yan@Sun.COM 
93410053SEvan.Yan@Sun.COM hrtime_t px_ib_msix_retarget_timeout = 120ll * NANOSEC;	/* 120 seconds */
93510053SEvan.Yan@Sun.COM 
93610053SEvan.Yan@Sun.COM /*
93710053SEvan.Yan@Sun.COM  * Associate a new CPU with a given MSI/X.
93810053SEvan.Yan@Sun.COM  * Operate only on MSI/Xs which are already mapped to devices.
93910053SEvan.Yan@Sun.COM  */
94010053SEvan.Yan@Sun.COM int
94110053SEvan.Yan@Sun.COM px_ib_set_msix_target(px_t *px_p, ddi_intr_handle_impl_t *hdlp,
94210053SEvan.Yan@Sun.COM     msinum_t msi_num, cpuid_t cpu_id)
94310053SEvan.Yan@Sun.COM {
94410053SEvan.Yan@Sun.COM 	px_ib_t			*ib_p = px_p->px_ib_p;
94510053SEvan.Yan@Sun.COM 	px_msi_state_t		*msi_state_p = &px_p->px_ib_p->ib_msi_state;
94610053SEvan.Yan@Sun.COM 	dev_info_t		*dip = px_p->px_dip;
94710053SEvan.Yan@Sun.COM 	dev_info_t		*rdip = hdlp->ih_dip;
94810053SEvan.Yan@Sun.COM 	msiqid_t		msiq_id, old_msiq_id;
94910053SEvan.Yan@Sun.COM 	pci_msi_state_t		msi_state;
95010053SEvan.Yan@Sun.COM 	msiq_rec_type_t		msiq_rec_type;
95110053SEvan.Yan@Sun.COM 	msi_type_t		msi_type;
95210053SEvan.Yan@Sun.COM 	px_ino_t		*ino_p;
95310053SEvan.Yan@Sun.COM 	px_ih_t			*ih_p, *old_ih_p;
95410053SEvan.Yan@Sun.COM 	cpuid_t			old_cpu_id;
95510053SEvan.Yan@Sun.COM 	hrtime_t		start_time, end_time;
95610053SEvan.Yan@Sun.COM 	int			ret = DDI_SUCCESS;
95710053SEvan.Yan@Sun.COM 	extern const int	_ncpu;
95810053SEvan.Yan@Sun.COM 	extern cpu_t		*cpu[];
95910053SEvan.Yan@Sun.COM 
96010053SEvan.Yan@Sun.COM 	DBG(DBG_IB, dip, "px_ib_set_msix_target: msi_num %x new cpu_id %x\n",
96110053SEvan.Yan@Sun.COM 	    msi_num, cpu_id);
96210053SEvan.Yan@Sun.COM 
96310053SEvan.Yan@Sun.COM 	mutex_enter(&cpu_lock);
96410053SEvan.Yan@Sun.COM 
96510053SEvan.Yan@Sun.COM 	/* Check for MSI64 support */
96610053SEvan.Yan@Sun.COM 	if ((hdlp->ih_cap & DDI_INTR_FLAG_MSI64) && msi_state_p->msi_addr64) {
96710053SEvan.Yan@Sun.COM 		msiq_rec_type = MSI64_REC;
96810053SEvan.Yan@Sun.COM 		msi_type = MSI64_TYPE;
96910053SEvan.Yan@Sun.COM 	} else {
97010053SEvan.Yan@Sun.COM 		msiq_rec_type = MSI32_REC;
97110053SEvan.Yan@Sun.COM 		msi_type = MSI32_TYPE;
97210053SEvan.Yan@Sun.COM 	}
97310053SEvan.Yan@Sun.COM 
97410053SEvan.Yan@Sun.COM 	if ((ret = px_lib_msi_getmsiq(dip, msi_num,
97510053SEvan.Yan@Sun.COM 	    &old_msiq_id)) != DDI_SUCCESS) {
97610053SEvan.Yan@Sun.COM 
97710053SEvan.Yan@Sun.COM 		mutex_exit(&cpu_lock);
97810053SEvan.Yan@Sun.COM 		return (ret);
97910053SEvan.Yan@Sun.COM 	}
98010053SEvan.Yan@Sun.COM 
98110053SEvan.Yan@Sun.COM 	DBG(DBG_IB, dip, "px_ib_set_msix_target: current msiq 0x%x\n",
98210053SEvan.Yan@Sun.COM 	    old_msiq_id);
98310053SEvan.Yan@Sun.COM 
98410053SEvan.Yan@Sun.COM 	if ((ret = px_ib_get_intr_target(px_p,
98510053SEvan.Yan@Sun.COM 	    px_msiqid_to_devino(px_p, old_msiq_id),
98610053SEvan.Yan@Sun.COM 	    &old_cpu_id)) != DDI_SUCCESS) {
98710053SEvan.Yan@Sun.COM 
98810053SEvan.Yan@Sun.COM 		mutex_exit(&cpu_lock);
98910053SEvan.Yan@Sun.COM 		return (ret);
99010053SEvan.Yan@Sun.COM 	}
99110053SEvan.Yan@Sun.COM 
99210053SEvan.Yan@Sun.COM 	DBG(DBG_IB, dip, "px_ib_set_msix_target: current cpuid 0x%x\n",
99310053SEvan.Yan@Sun.COM 	    old_cpu_id);
99410053SEvan.Yan@Sun.COM 
99510053SEvan.Yan@Sun.COM 	if (cpu_id == old_cpu_id) {
99610053SEvan.Yan@Sun.COM 
99710053SEvan.Yan@Sun.COM 		mutex_exit(&cpu_lock);
99810053SEvan.Yan@Sun.COM 		return (DDI_SUCCESS);
99910053SEvan.Yan@Sun.COM 	}
100010053SEvan.Yan@Sun.COM 
100110053SEvan.Yan@Sun.COM 	/*
100210053SEvan.Yan@Sun.COM 	 * Get lock, validate cpu and write it.
100310053SEvan.Yan@Sun.COM 	 */
100410053SEvan.Yan@Sun.COM 	if (!((cpu_id < _ncpu) && (cpu[cpu_id] &&
100510053SEvan.Yan@Sun.COM 	    cpu_is_online(cpu[cpu_id])))) {
100610053SEvan.Yan@Sun.COM 		/* Invalid cpu */
100710053SEvan.Yan@Sun.COM 		DBG(DBG_IB, dip, "px_ib_set_msix_target: Invalid cpuid %x\n",
100810053SEvan.Yan@Sun.COM 		    cpu_id);
100910053SEvan.Yan@Sun.COM 
101010053SEvan.Yan@Sun.COM 		mutex_exit(&cpu_lock);
101110053SEvan.Yan@Sun.COM 		return (DDI_EINVAL);
101210053SEvan.Yan@Sun.COM 	}
101310053SEvan.Yan@Sun.COM 
101410053SEvan.Yan@Sun.COM 	DBG(DBG_IB, dip, "px_ib_set_msix_target: Enabling CPU %d\n", cpu_id);
101510053SEvan.Yan@Sun.COM 
101610053SEvan.Yan@Sun.COM 	if ((ret = px_add_msiq_intr(dip, rdip, hdlp,
101710053SEvan.Yan@Sun.COM 	    msiq_rec_type, msi_num, cpu_id, &msiq_id)) != DDI_SUCCESS) {
101810053SEvan.Yan@Sun.COM 		DBG(DBG_IB, dip, "px_ib_set_msix_target: Add MSI handler "
101910053SEvan.Yan@Sun.COM 		    "failed, rdip 0x%p msi 0x%x\n", rdip, msi_num);
102010053SEvan.Yan@Sun.COM 
102110053SEvan.Yan@Sun.COM 		mutex_exit(&cpu_lock);
102210053SEvan.Yan@Sun.COM 		return (ret);
102310053SEvan.Yan@Sun.COM 	}
102410053SEvan.Yan@Sun.COM 
102510053SEvan.Yan@Sun.COM 	if ((ret = px_lib_msi_setmsiq(dip, msi_num,
102610053SEvan.Yan@Sun.COM 	    msiq_id, msi_type)) != DDI_SUCCESS) {
102711520SScott.Carter@Sun.COM 		mutex_exit(&cpu_lock);
102811520SScott.Carter@Sun.COM 
102910053SEvan.Yan@Sun.COM 		(void) px_rem_msiq_intr(dip, rdip,
103010053SEvan.Yan@Sun.COM 		    hdlp, msiq_rec_type, msi_num, msiq_id);
103110053SEvan.Yan@Sun.COM 
103210053SEvan.Yan@Sun.COM 		return (ret);
103310053SEvan.Yan@Sun.COM 	}
103410053SEvan.Yan@Sun.COM 
103510053SEvan.Yan@Sun.COM 	if ((ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
103610053SEvan.Yan@Sun.COM 	    px_msiqid_to_devino(px_p, msiq_id), hdlp->ih_pri,
103710053SEvan.Yan@Sun.COM 	    PX_INTR_STATE_ENABLE, msiq_rec_type, msi_num)) != DDI_SUCCESS) {
103811520SScott.Carter@Sun.COM 		mutex_exit(&cpu_lock);
103911520SScott.Carter@Sun.COM 
104010053SEvan.Yan@Sun.COM 		(void) px_rem_msiq_intr(dip, rdip,
104110053SEvan.Yan@Sun.COM 		    hdlp, msiq_rec_type, msi_num, msiq_id);
104210053SEvan.Yan@Sun.COM 
104310053SEvan.Yan@Sun.COM 		return (ret);
104410053SEvan.Yan@Sun.COM 	}
104510053SEvan.Yan@Sun.COM 
104610053SEvan.Yan@Sun.COM 	mutex_exit(&cpu_lock);
104711520SScott.Carter@Sun.COM 
104811520SScott.Carter@Sun.COM 	/*
104911520SScott.Carter@Sun.COM 	 * Remove the old handler, but first ensure it is finished.
105011520SScott.Carter@Sun.COM 	 *
105111520SScott.Carter@Sun.COM 	 * Each handler sets its PENDING flag before it clears the MSI state.
105211520SScott.Carter@Sun.COM 	 * Then it clears that flag when finished.  If a re-target occurs while
105311520SScott.Carter@Sun.COM 	 * the MSI state is DELIVERED, then it is not yet known which of the
105411520SScott.Carter@Sun.COM 	 * two handlers will take the interrupt.  So the re-target operation
105511520SScott.Carter@Sun.COM 	 * sets a RETARGET flag on both handlers in that case.  Monitoring both
105611520SScott.Carter@Sun.COM 	 * flags on both handlers then determines when the old handler can be
105711520SScott.Carter@Sun.COM 	 * be safely removed.
105811520SScott.Carter@Sun.COM 	 */
105910053SEvan.Yan@Sun.COM 	mutex_enter(&ib_p->ib_ino_lst_mutex);
106010053SEvan.Yan@Sun.COM 
106110053SEvan.Yan@Sun.COM 	ino_p = px_ib_locate_ino(ib_p, px_msiqid_to_devino(px_p, old_msiq_id));
106210053SEvan.Yan@Sun.COM 	old_ih_p = px_ib_intr_locate_ih(px_ib_ino_locate_ipil(ino_p,
106310053SEvan.Yan@Sun.COM 	    hdlp->ih_pri), rdip, hdlp->ih_inum, msiq_rec_type, msi_num);
106410053SEvan.Yan@Sun.COM 
106510053SEvan.Yan@Sun.COM 	ino_p = px_ib_locate_ino(ib_p, px_msiqid_to_devino(px_p, msiq_id));
106610053SEvan.Yan@Sun.COM 	ih_p = px_ib_intr_locate_ih(px_ib_ino_locate_ipil(ino_p, hdlp->ih_pri),
106710053SEvan.Yan@Sun.COM 	    rdip, hdlp->ih_inum, msiq_rec_type, msi_num);
106810053SEvan.Yan@Sun.COM 
106910053SEvan.Yan@Sun.COM 	if ((ret = px_lib_msi_getstate(dip, msi_num,
107010053SEvan.Yan@Sun.COM 	    &msi_state)) != DDI_SUCCESS) {
107110053SEvan.Yan@Sun.COM 		(void) px_rem_msiq_intr(dip, rdip,
107210053SEvan.Yan@Sun.COM 		    hdlp, msiq_rec_type, msi_num, msiq_id);
107310053SEvan.Yan@Sun.COM 
107410053SEvan.Yan@Sun.COM 		mutex_exit(&ib_p->ib_ino_lst_mutex);
107510053SEvan.Yan@Sun.COM 		return (ret);
107610053SEvan.Yan@Sun.COM 	}
107710053SEvan.Yan@Sun.COM 
107811520SScott.Carter@Sun.COM 	if (msi_state == PCI_MSI_STATE_DELIVERED) {
107911520SScott.Carter@Sun.COM 		ih_p->ih_intr_flags |= PX_INTR_RETARGET;
108011520SScott.Carter@Sun.COM 		old_ih_p->ih_intr_flags |= PX_INTR_RETARGET;
108111520SScott.Carter@Sun.COM 	}
108210053SEvan.Yan@Sun.COM 
108310053SEvan.Yan@Sun.COM 	start_time = gethrtime();
108411520SScott.Carter@Sun.COM 	while (((ih_p->ih_intr_flags & PX_INTR_RETARGET) &&
108511520SScott.Carter@Sun.COM 	    (old_ih_p->ih_intr_flags & PX_INTR_RETARGET)) ||
108611520SScott.Carter@Sun.COM 	    (old_ih_p->ih_intr_flags & PX_INTR_PENDING)) {
108711520SScott.Carter@Sun.COM 
108811520SScott.Carter@Sun.COM 		/* Wait for one second */
108911520SScott.Carter@Sun.COM 		delay(drv_usectohz(1000000));
109011520SScott.Carter@Sun.COM 
109111520SScott.Carter@Sun.COM 		end_time = gethrtime() - start_time;
109211520SScott.Carter@Sun.COM 		if (end_time > px_ib_msix_retarget_timeout) {
109310053SEvan.Yan@Sun.COM 			cmn_err(CE_WARN, "MSIX retarget %x is not completed, "
109410053SEvan.Yan@Sun.COM 			    "even after waiting %llx ticks\n",
109510053SEvan.Yan@Sun.COM 			    msi_num, end_time);
109610053SEvan.Yan@Sun.COM 			break;
109710053SEvan.Yan@Sun.COM 		}
109811520SScott.Carter@Sun.COM 	}
109910053SEvan.Yan@Sun.COM 
110011520SScott.Carter@Sun.COM 	ih_p->ih_intr_flags &= ~(PX_INTR_RETARGET);
110110053SEvan.Yan@Sun.COM 
110210053SEvan.Yan@Sun.COM 	mutex_exit(&ib_p->ib_ino_lst_mutex);
110310053SEvan.Yan@Sun.COM 
110410053SEvan.Yan@Sun.COM 	ret = px_rem_msiq_intr(dip, rdip,
110510053SEvan.Yan@Sun.COM 	    hdlp, msiq_rec_type, msi_num, old_msiq_id);
110610053SEvan.Yan@Sun.COM 
110710053SEvan.Yan@Sun.COM 	return (ret);
110810053SEvan.Yan@Sun.COM }
110910053SEvan.Yan@Sun.COM 
111010053SEvan.Yan@Sun.COM 
1111624Sschwartz static void
1112624Sschwartz px_fill_in_intr_devs(pcitool_intr_dev_t *dev, char *driver_name,
1113624Sschwartz     char *path_name, int instance)
1114624Sschwartz {
1115624Sschwartz 	(void) strncpy(dev->driver_name, driver_name, MAXMODCONFNAME-1);
1116624Sschwartz 	dev->driver_name[MAXMODCONFNAME] = '\0';
1117624Sschwartz 	(void) strncpy(dev->path, path_name, MAXPATHLEN-1);
1118624Sschwartz 	dev->dev_inst = instance;
1119624Sschwartz }
1120624Sschwartz 
1121624Sschwartz 
1122624Sschwartz /*
1123624Sschwartz  * Return the dips or number of dips associated with a given interrupt block.
1124624Sschwartz  * Size of dips array arg is passed in as dips_ret arg.
1125624Sschwartz  * Number of dips returned is returned in dips_ret arg.
1126624Sschwartz  * Array of dips gets returned in the dips argument.
1127624Sschwartz  * Function returns number of dips existing for the given interrupt block.
1128624Sschwartz  *
1129624Sschwartz  * Note: this function assumes an enabled/valid INO, which is why it returns
1130624Sschwartz  * the px node and (Internal) when it finds no other devices (and *devs_ret > 0)
1131624Sschwartz  */
1132624Sschwartz uint8_t
113310053SEvan.Yan@Sun.COM pxtool_ib_get_ino_devs(px_t *px_p, uint32_t ino, uint32_t msi_num,
113410053SEvan.Yan@Sun.COM     uint8_t *devs_ret, pcitool_intr_dev_t *devs)
1135624Sschwartz {
11362973Sgovinda 	px_ib_t		*ib_p = px_p->px_ib_p;
11372973Sgovinda 	px_ino_t	*ino_p;
11382973Sgovinda 	px_ino_pil_t	*ipil_p;
11392973Sgovinda 	px_ih_t 	*ih_p;
11402973Sgovinda 	uint32_t 	num_devs = 0;
11412973Sgovinda 	char		pathname[MAXPATHLEN];
11422973Sgovinda 	int		i, j;
1143624Sschwartz 
1144624Sschwartz 	mutex_enter(&ib_p->ib_ino_lst_mutex);
1145624Sschwartz 	ino_p = px_ib_locate_ino(ib_p, ino);
1146624Sschwartz 	if (ino_p != NULL) {
11472973Sgovinda 		for (j = 0, ipil_p = ino_p->ino_ipil_p; ipil_p;
11482973Sgovinda 		    ipil_p = ipil_p->ipil_next_p) {
11492973Sgovinda 			num_devs += ipil_p->ipil_ih_size;
11502973Sgovinda 
11512973Sgovinda 			for (i = 0, ih_p = ipil_p->ipil_ih_head;
11522973Sgovinda 			    ((i < ipil_p->ipil_ih_size) && (i < *devs_ret));
11532973Sgovinda 			    i++, j++, ih_p = ih_p->ih_next) {
11542973Sgovinda 				(void) ddi_pathname(ih_p->ih_dip, pathname);
115510053SEvan.Yan@Sun.COM 
115610053SEvan.Yan@Sun.COM 				if (ih_p->ih_msg_code == msi_num) {
115710053SEvan.Yan@Sun.COM 					num_devs = *devs_ret = 1;
115810053SEvan.Yan@Sun.COM 					px_fill_in_intr_devs(&devs[0],
115910053SEvan.Yan@Sun.COM 					    (char *)ddi_driver_name(
116010053SEvan.Yan@Sun.COM 					    ih_p->ih_dip), pathname,
116110053SEvan.Yan@Sun.COM 					    ddi_get_instance(ih_p->ih_dip));
116210053SEvan.Yan@Sun.COM 					goto done;
116310053SEvan.Yan@Sun.COM 				}
116410053SEvan.Yan@Sun.COM 
116510053SEvan.Yan@Sun.COM 				px_fill_in_intr_devs(&devs[j],
11662973Sgovinda 				    (char *)ddi_driver_name(ih_p->ih_dip),
11672973Sgovinda 				    pathname, ddi_get_instance(ih_p->ih_dip));
11682973Sgovinda 			}
1169624Sschwartz 		}
1170624Sschwartz 
11712973Sgovinda 		*devs_ret = j;
1172624Sschwartz 	} else if (*devs_ret > 0) {
1173624Sschwartz 		(void) ddi_pathname(px_p->px_dip, pathname);
1174624Sschwartz 		strcat(pathname, " (Internal)");
1175624Sschwartz 		px_fill_in_intr_devs(&devs[0],
1176624Sschwartz 		    (char *)ddi_driver_name(px_p->px_dip),  pathname,
1177624Sschwartz 		    ddi_get_instance(px_p->px_dip));
1178624Sschwartz 		num_devs = *devs_ret = 1;
1179624Sschwartz 	}
1180624Sschwartz 
118110053SEvan.Yan@Sun.COM done:
1182624Sschwartz 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1183624Sschwartz 
1184624Sschwartz 	return (num_devs);
1185624Sschwartz }
1186624Sschwartz 
1187624Sschwartz 
118810053SEvan.Yan@Sun.COM int
118910053SEvan.Yan@Sun.COM pxtool_ib_get_msi_info(px_t *px_p, devino_t ino, msinum_t msi_num,
119010053SEvan.Yan@Sun.COM     ddi_intr_handle_impl_t *hdlp)
119110053SEvan.Yan@Sun.COM {
119210053SEvan.Yan@Sun.COM 	px_ib_t		*ib_p = px_p->px_ib_p;
119310053SEvan.Yan@Sun.COM 	px_ino_t	*ino_p;
119410053SEvan.Yan@Sun.COM 	px_ino_pil_t	*ipil_p;
119510053SEvan.Yan@Sun.COM 	px_ih_t 	*ih_p;
119610053SEvan.Yan@Sun.COM 	int		i;
119710053SEvan.Yan@Sun.COM 
119810053SEvan.Yan@Sun.COM 	mutex_enter(&ib_p->ib_ino_lst_mutex);
119910053SEvan.Yan@Sun.COM 
120010053SEvan.Yan@Sun.COM 	if ((ino_p = px_ib_locate_ino(ib_p, ino)) == NULL) {
120110053SEvan.Yan@Sun.COM 		mutex_exit(&ib_p->ib_ino_lst_mutex);
120210053SEvan.Yan@Sun.COM 		return (DDI_FAILURE);
120310053SEvan.Yan@Sun.COM 	}
120410053SEvan.Yan@Sun.COM 
120510053SEvan.Yan@Sun.COM 	for (ipil_p = ino_p->ino_ipil_p; ipil_p;
120610053SEvan.Yan@Sun.COM 	    ipil_p = ipil_p->ipil_next_p) {
120710053SEvan.Yan@Sun.COM 		for (i = 0, ih_p = ipil_p->ipil_ih_head;
120810053SEvan.Yan@Sun.COM 		    ((i < ipil_p->ipil_ih_size) && ih_p);
120910053SEvan.Yan@Sun.COM 		    i++, ih_p = ih_p->ih_next) {
121010053SEvan.Yan@Sun.COM 
121110053SEvan.Yan@Sun.COM 			if (ih_p->ih_msg_code != msi_num)
121210053SEvan.Yan@Sun.COM 				continue;
121310053SEvan.Yan@Sun.COM 
121410053SEvan.Yan@Sun.COM 			hdlp->ih_dip = ih_p->ih_dip;
121510053SEvan.Yan@Sun.COM 			hdlp->ih_inum = ih_p->ih_inum;
121610053SEvan.Yan@Sun.COM 			hdlp->ih_cb_func = ih_p->ih_handler;
121710053SEvan.Yan@Sun.COM 			hdlp->ih_cb_arg1 = ih_p->ih_handler_arg1;
121810053SEvan.Yan@Sun.COM 			hdlp->ih_cb_arg2 = ih_p->ih_handler_arg2;
121910053SEvan.Yan@Sun.COM 			if (ih_p->ih_rec_type == MSI64_REC)
122010053SEvan.Yan@Sun.COM 				hdlp->ih_cap = DDI_INTR_FLAG_MSI64;
122110053SEvan.Yan@Sun.COM 			hdlp->ih_pri = ipil_p->ipil_pil;
122210053SEvan.Yan@Sun.COM 			hdlp->ih_ver = DDI_INTR_VERSION;
122310053SEvan.Yan@Sun.COM 
122410053SEvan.Yan@Sun.COM 			mutex_exit(&ib_p->ib_ino_lst_mutex);
122510053SEvan.Yan@Sun.COM 			return (DDI_SUCCESS);
122610053SEvan.Yan@Sun.COM 		}
122710053SEvan.Yan@Sun.COM 	}
122810053SEvan.Yan@Sun.COM 
122910053SEvan.Yan@Sun.COM 	mutex_exit(&ib_p->ib_ino_lst_mutex);
123010053SEvan.Yan@Sun.COM 	return (DDI_FAILURE);
123110053SEvan.Yan@Sun.COM }
123210053SEvan.Yan@Sun.COM 
12331617Sgovinda void
123410053SEvan.Yan@Sun.COM px_ib_log_new_cpu(px_ib_t *ib_p, cpuid_t old_cpu_id, cpuid_t new_cpu_id,
1235624Sschwartz     uint32_t ino)
1236624Sschwartz {
12372973Sgovinda 	px_ino_t	*ino_p;
12382973Sgovinda 	px_ino_pil_t	*ipil_p;
12392973Sgovinda 	px_ih_t 	*ih_p;
12402973Sgovinda 	int		i;
1241624Sschwartz 
1242624Sschwartz 	mutex_enter(&ib_p->ib_ino_lst_mutex);
1243624Sschwartz 
1244624Sschwartz 	/* Log in OS data structures the new CPU. */
12452973Sgovinda 	if (ino_p = px_ib_locate_ino(ib_p, ino)) {
1246624Sschwartz 
1247624Sschwartz 		/* Log in OS data structures the new CPU. */
1248624Sschwartz 		ino_p->ino_cpuid = new_cpu_id;
1249624Sschwartz 
12502973Sgovinda 		for (ipil_p = ino_p->ino_ipil_p; ipil_p;
12512973Sgovinda 		    ipil_p = ipil_p->ipil_next_p) {
12522973Sgovinda 			for (i = 0, ih_p = ipil_p->ipil_ih_head;
12532973Sgovinda 			    (i < ipil_p->ipil_ih_size);
12542973Sgovinda 			    i++, ih_p = ih_p->ih_next) {
12552973Sgovinda 				/*
12562973Sgovinda 				 * Account for any residual time
12572973Sgovinda 				 * to be logged for old cpu.
12582973Sgovinda 				 */
12592973Sgovinda 				px_ib_cpu_ticks_to_ih_nsec(ib_p,
12602973Sgovinda 				    ih_p, old_cpu_id);
12612973Sgovinda 			}
12622973Sgovinda 		}
1263624Sschwartz 	}
1264624Sschwartz 
1265624Sschwartz 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1266624Sschwartz }
1267