xref: /onnv-gate/usr/src/uts/sun4/io/px/px_ib.c (revision 12589:08d831116e35)
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 /*
22*12589SDavid.Major@Oracle.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  * PX Interrupt Block implementation
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/kmem.h>
310Sstevel@tonic-gate #include <sys/async.h>
320Sstevel@tonic-gate #include <sys/systm.h>		/* panicstr */
330Sstevel@tonic-gate #include <sys/spl.h>
340Sstevel@tonic-gate #include <sys/sunddi.h>
350Sstevel@tonic-gate #include <sys/machsystm.h>	/* intr_dist_add */
360Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
370Sstevel@tonic-gate #include <sys/cpuvar.h>
381772Sjl139090 #include <sys/time.h>
390Sstevel@tonic-gate #include "px_obj.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*LINTLIBRARY*/
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static void px_ib_intr_redist(void *arg, int32_t weight_max, int32_t weight);
44624Sschwartz static void px_ib_cpu_ticks_to_ih_nsec(px_ib_t *ib_p, px_ih_t *ih_p,
45624Sschwartz     uint32_t cpu_id);
460Sstevel@tonic-gate static uint_t px_ib_intr_reset(void *arg);
47624Sschwartz static void px_fill_in_intr_devs(pcitool_intr_dev_t *dev, char *driver_name,
48624Sschwartz     char *path_name, int instance);
490Sstevel@tonic-gate 
501772Sjl139090 extern uint64_t xc_tick_jump_limit;
511772Sjl139090 
520Sstevel@tonic-gate int
px_ib_attach(px_t * px_p)530Sstevel@tonic-gate px_ib_attach(px_t *px_p)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
560Sstevel@tonic-gate 	px_ib_t		*ib_p;
570Sstevel@tonic-gate 	sysino_t	sysino;
580Sstevel@tonic-gate 	px_fault_t	*fault_p = &px_p->px_fault;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	DBG(DBG_IB, dip, "px_ib_attach\n");
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	if (px_lib_intr_devino_to_sysino(px_p->px_dip,
6327Sjchu 	    px_p->px_inos[PX_INTR_PEC], &sysino) != DDI_SUCCESS)
640Sstevel@tonic-gate 		return (DDI_FAILURE);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	/*
670Sstevel@tonic-gate 	 * Allocate interrupt block state structure and link it to
680Sstevel@tonic-gate 	 * the px state structure.
690Sstevel@tonic-gate 	 */
700Sstevel@tonic-gate 	ib_p = kmem_zalloc(sizeof (px_ib_t), KM_SLEEP);
710Sstevel@tonic-gate 	px_p->px_ib_p = ib_p;
720Sstevel@tonic-gate 	ib_p->ib_px_p = px_p;
732973Sgovinda 	ib_p->ib_ino_lst = (px_ino_t *)NULL;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	mutex_init(&ib_p->ib_intr_lock, NULL, MUTEX_DRIVER, NULL);
760Sstevel@tonic-gate 	mutex_init(&ib_p->ib_ino_lst_mutex, NULL, MUTEX_DRIVER, NULL);
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	bus_func_register(BF_TYPE_RESINTR, px_ib_intr_reset, ib_p);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	intr_dist_add_weighted(px_ib_intr_redist, ib_p);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	/*
830Sstevel@tonic-gate 	 * Initialize PEC fault data structure
840Sstevel@tonic-gate 	 */
850Sstevel@tonic-gate 	fault_p->px_fh_dip = dip;
860Sstevel@tonic-gate 	fault_p->px_fh_sysino = sysino;
8727Sjchu 	fault_p->px_err_func = px_err_dmc_pec_intr;
8827Sjchu 	fault_p->px_intr_ino = px_p->px_inos[PX_INTR_PEC];
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	return (DDI_SUCCESS);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate 
930Sstevel@tonic-gate void
px_ib_detach(px_t * px_p)940Sstevel@tonic-gate px_ib_detach(px_t *px_p)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
970Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	DBG(DBG_IB, dip, "px_ib_detach\n");
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	bus_func_unregister(BF_TYPE_RESINTR, px_ib_intr_reset, ib_p);
1020Sstevel@tonic-gate 	intr_dist_rem_weighted(px_ib_intr_redist, ib_p);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	mutex_destroy(&ib_p->ib_ino_lst_mutex);
1050Sstevel@tonic-gate 	mutex_destroy(&ib_p->ib_intr_lock);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	px_ib_free_ino_all(ib_p);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	px_p->px_ib_p = NULL;
1100Sstevel@tonic-gate 	kmem_free(ib_p, sizeof (px_ib_t));
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate void
px_ib_intr_enable(px_t * px_p,cpuid_t cpu_id,devino_t ino)1140Sstevel@tonic-gate px_ib_intr_enable(px_t *px_p, cpuid_t cpu_id, devino_t ino)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
1170Sstevel@tonic-gate 	sysino_t	sysino;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	/*
1200Sstevel@tonic-gate 	 * Determine the cpu for the interrupt
1210Sstevel@tonic-gate 	 */
1220Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_intr_lock);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	DBG(DBG_IB, px_p->px_dip,
1250Sstevel@tonic-gate 	    "px_ib_intr_enable: ino=%x cpu_id=%x\n", ino, cpu_id);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if (px_lib_intr_devino_to_sysino(px_p->px_dip, ino,
1280Sstevel@tonic-gate 	    &sysino) != DDI_SUCCESS) {
1290Sstevel@tonic-gate 		DBG(DBG_IB, px_p->px_dip,
1300Sstevel@tonic-gate 		    "px_ib_intr_enable: px_intr_devino_to_sysino() failed\n");
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_intr_lock);
1330Sstevel@tonic-gate 		return;
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	PX_INTR_ENABLE(px_p->px_dip, sysino, cpu_id);
137693Sgovinda 	px_lib_intr_setstate(px_p->px_dip, sysino, INTR_IDLE_STATE);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_intr_lock);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*ARGSUSED*/
1430Sstevel@tonic-gate void
px_ib_intr_disable(px_ib_t * ib_p,devino_t ino,int wait)1440Sstevel@tonic-gate px_ib_intr_disable(px_ib_t *ib_p, devino_t ino, int wait)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	sysino_t	sysino;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_intr_lock);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	DBG(DBG_IB, ib_p->ib_px_p->px_dip, "px_ib_intr_disable: ino=%x\n", ino);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/* Disable the interrupt */
1530Sstevel@tonic-gate 	if (px_lib_intr_devino_to_sysino(ib_p->ib_px_p->px_dip, ino,
1540Sstevel@tonic-gate 	    &sysino) != DDI_SUCCESS) {
1550Sstevel@tonic-gate 		DBG(DBG_IB, ib_p->ib_px_p->px_dip,
1560Sstevel@tonic-gate 		    "px_ib_intr_disable: px_intr_devino_to_sysino() failed\n");
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_intr_lock);
1590Sstevel@tonic-gate 		return;
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	PX_INTR_DISABLE(ib_p->ib_px_p->px_dip, sysino);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_intr_lock);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
167*12589SDavid.Major@Oracle.COM int
px_ib_intr_pend(dev_info_t * dip,sysino_t sysino)168*12589SDavid.Major@Oracle.COM px_ib_intr_pend(dev_info_t *dip, sysino_t sysino)
169*12589SDavid.Major@Oracle.COM {
170*12589SDavid.Major@Oracle.COM 	int		ret = DDI_SUCCESS;
171*12589SDavid.Major@Oracle.COM 	hrtime_t	start_time, prev, curr, interval, jump;
172*12589SDavid.Major@Oracle.COM 	hrtime_t	intr_timeout;
173*12589SDavid.Major@Oracle.COM 	intr_state_t	intr_state;
174*12589SDavid.Major@Oracle.COM 
175*12589SDavid.Major@Oracle.COM 	/* Disable the interrupt */
176*12589SDavid.Major@Oracle.COM 	PX_INTR_DISABLE(dip, sysino);
177*12589SDavid.Major@Oracle.COM 
178*12589SDavid.Major@Oracle.COM 	intr_timeout = px_intrpend_timeout;
179*12589SDavid.Major@Oracle.COM 	jump = TICK_TO_NSEC(xc_tick_jump_limit);
180*12589SDavid.Major@Oracle.COM 
181*12589SDavid.Major@Oracle.COM 	/* Busy wait on pending interrupt */
182*12589SDavid.Major@Oracle.COM 	for (curr = start_time = gethrtime(); !panicstr &&
183*12589SDavid.Major@Oracle.COM 	    ((ret = px_lib_intr_getstate(dip, sysino,
184*12589SDavid.Major@Oracle.COM 	    &intr_state)) == DDI_SUCCESS) &&
185*12589SDavid.Major@Oracle.COM 	    (intr_state == INTR_DELIVERED_STATE); /* */) {
186*12589SDavid.Major@Oracle.COM 		/*
187*12589SDavid.Major@Oracle.COM 		 * If we have a really large jump in hrtime, it is most
188*12589SDavid.Major@Oracle.COM 		 * probably because we entered the debugger (or OBP,
189*12589SDavid.Major@Oracle.COM 		 * in general). So, we adjust the timeout accordingly
190*12589SDavid.Major@Oracle.COM 		 * to prevent declaring an interrupt timeout. The
191*12589SDavid.Major@Oracle.COM 		 * master-interrupt mechanism in OBP should deliver
192*12589SDavid.Major@Oracle.COM 		 * the interrupts properly.
193*12589SDavid.Major@Oracle.COM 		 */
194*12589SDavid.Major@Oracle.COM 		prev = curr;
195*12589SDavid.Major@Oracle.COM 		curr = gethrtime();
196*12589SDavid.Major@Oracle.COM 		interval = curr - prev;
197*12589SDavid.Major@Oracle.COM 		if (interval > jump)
198*12589SDavid.Major@Oracle.COM 			intr_timeout += interval;
199*12589SDavid.Major@Oracle.COM 		if (curr - start_time > intr_timeout) {
200*12589SDavid.Major@Oracle.COM 			ret = DDI_FAILURE;
201*12589SDavid.Major@Oracle.COM 			break;
202*12589SDavid.Major@Oracle.COM 		}
203*12589SDavid.Major@Oracle.COM 	}
204*12589SDavid.Major@Oracle.COM 	return (ret);
205*12589SDavid.Major@Oracle.COM }
2060Sstevel@tonic-gate 
207624Sschwartz void
px_ib_intr_dist_en(dev_info_t * dip,cpuid_t cpu_id,devino_t ino,boolean_t wait_flag)2080Sstevel@tonic-gate px_ib_intr_dist_en(dev_info_t *dip, cpuid_t cpu_id, devino_t ino,
2090Sstevel@tonic-gate     boolean_t wait_flag)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	uint32_t	old_cpu_id;
2120Sstevel@tonic-gate 	sysino_t	sysino;
2130Sstevel@tonic-gate 	intr_valid_state_t	enabled = 0;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	DBG(DBG_IB, dip, "px_ib_intr_dist_en: ino=0x%x\n", ino);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	if (px_lib_intr_devino_to_sysino(dip, ino, &sysino) != DDI_SUCCESS) {
2180Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: "
2190Sstevel@tonic-gate 		    "px_intr_devino_to_sysino() failed, ino 0x%x\n", ino);
2200Sstevel@tonic-gate 		return;
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	/* Skip enabling disabled interrupts */
2240Sstevel@tonic-gate 	if (px_lib_intr_getvalid(dip, sysino, &enabled) != DDI_SUCCESS) {
2250Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: px_intr_getvalid() "
2260Sstevel@tonic-gate 		    "failed, sysino 0x%x\n", sysino);
2270Sstevel@tonic-gate 		return;
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 	if (!enabled)
2300Sstevel@tonic-gate 		return;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	/* Done if redistributed onto the same cpuid */
2330Sstevel@tonic-gate 	if (px_lib_intr_gettarget(dip, sysino, &old_cpu_id) != DDI_SUCCESS) {
2340Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: "
2350Sstevel@tonic-gate 		    "px_intr_gettarget() failed\n");
2360Sstevel@tonic-gate 		return;
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 	if (cpu_id == old_cpu_id)
2390Sstevel@tonic-gate 		return;
2400Sstevel@tonic-gate 
241*12589SDavid.Major@Oracle.COM 	/* Wait on pending interrupts */
242*12589SDavid.Major@Oracle.COM 	if (wait_flag != 0 && px_ib_intr_pend(dip, sysino) != DDI_SUCCESS) {
243*12589SDavid.Major@Oracle.COM 		cmn_err(CE_WARN,
244*12589SDavid.Major@Oracle.COM 		    "%s%d: px_ib_intr_dist_en: sysino 0x%lx(ino 0x%x) "
245*12589SDavid.Major@Oracle.COM 		    "from cpu id 0x%x to 0x%x timeout",
246*12589SDavid.Major@Oracle.COM 		    ddi_driver_name(dip), ddi_get_instance(dip),
247*12589SDavid.Major@Oracle.COM 		    sysino, ino, old_cpu_id, cpu_id);
2481772Sjl139090 
249*12589SDavid.Major@Oracle.COM 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: failed, "
250*12589SDavid.Major@Oracle.COM 		    "ino 0x%x sysino 0x%x\n", ino, sysino);
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	PX_INTR_ENABLE(dip, sysino, cpu_id);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate 
256624Sschwartz static void
px_ib_cpu_ticks_to_ih_nsec(px_ib_t * ib_p,px_ih_t * ih_p,uint32_t cpu_id)257624Sschwartz px_ib_cpu_ticks_to_ih_nsec(px_ib_t *ib_p, px_ih_t *ih_p, uint32_t cpu_id)
258624Sschwartz {
259624Sschwartz 	extern kmutex_t pxintr_ks_template_lock;
260624Sschwartz 	hrtime_t ticks;
261624Sschwartz 
262624Sschwartz 	/*
263624Sschwartz 	 * Because we are updating two fields in ih_t we must lock
264624Sschwartz 	 * pxintr_ks_template_lock to prevent someone from reading the
265624Sschwartz 	 * kstats after we set ih_ticks to 0 and before we increment
266624Sschwartz 	 * ih_nsec to compensate.
267624Sschwartz 	 *
268624Sschwartz 	 * We must also protect against the interrupt arriving and incrementing
269624Sschwartz 	 * ih_ticks between the time we read it and when we reset it to 0.
270624Sschwartz 	 * To do this we use atomic_swap.
271624Sschwartz 	 */
272624Sschwartz 
273624Sschwartz 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
274624Sschwartz 
275624Sschwartz 	mutex_enter(&pxintr_ks_template_lock);
276624Sschwartz 	ticks = atomic_swap_64(&ih_p->ih_ticks, 0);
277624Sschwartz 	ih_p->ih_nsec += (uint64_t)tick2ns(ticks, cpu_id);
278624Sschwartz 	mutex_exit(&pxintr_ks_template_lock);
279624Sschwartz }
280624Sschwartz 
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate /*
2830Sstevel@tonic-gate  * Redistribute interrupts of the specified weight. The first call has a weight
2840Sstevel@tonic-gate  * of weight_max, which can be used to trigger initialization for
2850Sstevel@tonic-gate  * redistribution. The inos with weight [weight_max, inf.) should be processed
2860Sstevel@tonic-gate  * on the "weight == weight_max" call.  This first call is followed by calls
2870Sstevel@tonic-gate  * of decreasing weights, inos of that weight should be processed.  The final
2880Sstevel@tonic-gate  * call specifies a weight of zero, this can be used to trigger processing of
2890Sstevel@tonic-gate  * stragglers.
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate static void
px_ib_intr_redist(void * arg,int32_t weight_max,int32_t weight)2920Sstevel@tonic-gate px_ib_intr_redist(void *arg, int32_t weight_max, int32_t weight)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate 	px_ib_t		*ib_p = (px_ib_t *)arg;
2950Sstevel@tonic-gate 	px_t		*px_p = ib_p->ib_px_p;
2960Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
2972973Sgovinda 	px_ino_t	*ino_p;
2982973Sgovinda 	px_ino_pil_t	*ipil_p;
2990Sstevel@tonic-gate 	px_ih_t		*ih_lst;
3000Sstevel@tonic-gate 	int32_t		dweight = 0;
3010Sstevel@tonic-gate 	int		i;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	/* Redistribute internal interrupts */
3040Sstevel@tonic-gate 	if (weight == 0) {
3051648Sjchu 		mutex_enter(&ib_p->ib_intr_lock);
3061648Sjchu 		px_ib_intr_dist_en(dip, intr_dist_cpuid(),
3071648Sjchu 		    px_p->px_inos[PX_INTR_PEC], B_FALSE);
3081648Sjchu 		mutex_exit(&ib_p->ib_intr_lock);
3093953Sscarter 
3103953Sscarter 		px_hp_intr_redist(px_p);
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	/* Redistribute device interrupts */
3140Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
31510053SEvan.Yan@Sun.COM 	px_msiq_redist(px_p);
3160Sstevel@tonic-gate 
3172973Sgovinda 	for (ino_p = ib_p->ib_ino_lst; ino_p; ino_p = ino_p->ino_next_p) {
3180Sstevel@tonic-gate 		/*
3190Sstevel@tonic-gate 		 * Recomputes the sum of interrupt weights of devices that
3200Sstevel@tonic-gate 		 * share the same ino upon first call marked by
3210Sstevel@tonic-gate 		 * (weight == weight_max).
3220Sstevel@tonic-gate 		 */
3230Sstevel@tonic-gate 		if (weight == weight_max) {
3240Sstevel@tonic-gate 			ino_p->ino_intr_weight = 0;
3252973Sgovinda 
3262973Sgovinda 			for (ipil_p = ino_p->ino_ipil_p; ipil_p;
3272973Sgovinda 			    ipil_p = ipil_p->ipil_next_p) {
3282973Sgovinda 				for (i = 0, ih_lst = ipil_p->ipil_ih_head;
3292973Sgovinda 				    i < ipil_p->ipil_ih_size; i++,
3302973Sgovinda 				    ih_lst = ih_lst->ih_next) {
3312973Sgovinda 					dweight = i_ddi_get_intr_weight(
3322973Sgovinda 					    ih_lst->ih_dip);
3332973Sgovinda 					if (dweight > 0)
3342973Sgovinda 						ino_p->ino_intr_weight +=
3352973Sgovinda 						    dweight;
3362973Sgovinda 				}
3370Sstevel@tonic-gate 			}
3380Sstevel@tonic-gate 		}
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 		/*
3410Sstevel@tonic-gate 		 * As part of redistributing weighted interrupts over cpus,
3420Sstevel@tonic-gate 		 * nexus redistributes device interrupts and updates
3430Sstevel@tonic-gate 		 * cpu weight. The purpose is for the most light weighted
3440Sstevel@tonic-gate 		 * cpu to take the next interrupt and gain weight, therefore
3450Sstevel@tonic-gate 		 * attention demanding device gains more cpu attention by
3460Sstevel@tonic-gate 		 * making itself heavy.
3470Sstevel@tonic-gate 		 */
3480Sstevel@tonic-gate 		if ((weight == ino_p->ino_intr_weight) ||
3490Sstevel@tonic-gate 		    ((weight >= weight_max) &&
3500Sstevel@tonic-gate 		    (ino_p->ino_intr_weight >= weight_max))) {
35110053SEvan.Yan@Sun.COM 			uint32_t orig_cpuid = ino_p->ino_cpuid;
35210053SEvan.Yan@Sun.COM 
3530Sstevel@tonic-gate 			if (cpu[orig_cpuid] == NULL)
3540Sstevel@tonic-gate 				orig_cpuid = CPU->cpu_id;
3550Sstevel@tonic-gate 
35610053SEvan.Yan@Sun.COM 			DBG(DBG_IB, dip, "px_ib_intr_redist: sysino 0x%llx "
35710053SEvan.Yan@Sun.COM 			    "current cpuid 0x%x current default cpuid 0x%x\n",
35810053SEvan.Yan@Sun.COM 			    ino_p->ino_sysino, ino_p->ino_cpuid,
35910053SEvan.Yan@Sun.COM 			    ino_p->ino_default_cpuid);
36010053SEvan.Yan@Sun.COM 
36110053SEvan.Yan@Sun.COM 			/* select target cpuid and mark ino established */
36210053SEvan.Yan@Sun.COM 			if (ino_p->ino_default_cpuid == -1)
36310053SEvan.Yan@Sun.COM 				ino_p->ino_cpuid = ino_p->ino_default_cpuid =
36410053SEvan.Yan@Sun.COM 				    intr_dist_cpuid();
36510053SEvan.Yan@Sun.COM 			else if ((ino_p->ino_cpuid !=
36610053SEvan.Yan@Sun.COM 			    ino_p->ino_default_cpuid) &&
36710279SGovinda.Tatti@Sun.COM 			    cpu[ino_p->ino_default_cpuid] &&
36810279SGovinda.Tatti@Sun.COM 			    cpu_intr_on(cpu[ino_p->ino_default_cpuid]))
36910053SEvan.Yan@Sun.COM 				ino_p->ino_cpuid = ino_p->ino_default_cpuid;
37010053SEvan.Yan@Sun.COM 			else if (!cpu_intr_on(cpu[ino_p->ino_cpuid]))
37110053SEvan.Yan@Sun.COM 				ino_p->ino_cpuid = intr_dist_cpuid();
37210053SEvan.Yan@Sun.COM 
37310053SEvan.Yan@Sun.COM 			DBG(DBG_IB, dip, "px_ib_intr_redist: sysino 0x%llx "
37410053SEvan.Yan@Sun.COM 			    "new cpuid 0x%x new default cpuid 0x%x\n",
37510053SEvan.Yan@Sun.COM 			    ino_p->ino_sysino, ino_p->ino_cpuid,
37610053SEvan.Yan@Sun.COM 			    ino_p->ino_default_cpuid);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 			/* Add device weight to targeted cpu. */
3792973Sgovinda 			for (ipil_p = ino_p->ino_ipil_p; ipil_p;
3802973Sgovinda 			    ipil_p = ipil_p->ipil_next_p) {
3812973Sgovinda 				for (i = 0, ih_lst = ipil_p->ipil_ih_head;
3822973Sgovinda 				    i < ipil_p->ipil_ih_size; i++,
3832973Sgovinda 				    ih_lst = ih_lst->ih_next) {
3840Sstevel@tonic-gate 
3852973Sgovinda 					dweight = i_ddi_get_intr_weight(
3862973Sgovinda 					    ih_lst->ih_dip);
3872973Sgovinda 					intr_dist_cpuid_add_device_weight(
3882973Sgovinda 					    ino_p->ino_cpuid, ih_lst->ih_dip,
3892973Sgovinda 					    dweight);
3900Sstevel@tonic-gate 
3912973Sgovinda 					/*
3922973Sgovinda 					 * Different cpus may have different
3932973Sgovinda 					 * clock speeds. to account for this,
3942973Sgovinda 					 * whenever an interrupt is moved to a
3952973Sgovinda 					 * new CPU, we convert the accumulated
3962973Sgovinda 					 * ticks into nsec, based upon the clock
3972973Sgovinda 					 * rate of the prior CPU.
3982973Sgovinda 					 *
3992973Sgovinda 					 * It is possible that the prior CPU no
4002973Sgovinda 					 * longer exists. In this case, fall
4012973Sgovinda 					 * back to using this CPU's clock rate.
4022973Sgovinda 					 *
4032973Sgovinda 					 * Note that the value in ih_ticks has
4042973Sgovinda 					 * already been corrected for any power
4052973Sgovinda 					 * savings mode which might have been
4062973Sgovinda 					 * in effect.
4072973Sgovinda 					 */
4082973Sgovinda 					px_ib_cpu_ticks_to_ih_nsec(ib_p, ih_lst,
4092973Sgovinda 					    orig_cpuid);
4102973Sgovinda 				}
4110Sstevel@tonic-gate 			}
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 			/* enable interrupt on new targeted cpu */
4140Sstevel@tonic-gate 			px_ib_intr_dist_en(dip, ino_p->ino_cpuid,
4150Sstevel@tonic-gate 			    ino_p->ino_ino, B_TRUE);
4160Sstevel@tonic-gate 		}
4170Sstevel@tonic-gate 	}
4180Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate /*
4220Sstevel@tonic-gate  * Reset interrupts to IDLE.  This function is called during
4230Sstevel@tonic-gate  * panic handling after redistributing interrupts; it's needed to
4240Sstevel@tonic-gate  * support dumping to network devices after 'sync' from OBP.
4250Sstevel@tonic-gate  *
4260Sstevel@tonic-gate  * N.B.  This routine runs in a context where all other threads
4270Sstevel@tonic-gate  * are permanently suspended.
4280Sstevel@tonic-gate  */
4290Sstevel@tonic-gate static uint_t
px_ib_intr_reset(void * arg)4300Sstevel@tonic-gate px_ib_intr_reset(void *arg)
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate 	px_ib_t		*ib_p = (px_ib_t *)arg;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	DBG(DBG_IB, ib_p->ib_px_p->px_dip, "px_ib_intr_reset\n");
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (px_lib_intr_reset(ib_p->ib_px_p->px_dip) != DDI_SUCCESS)
4370Sstevel@tonic-gate 		return (BF_FATAL);
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	return (BF_NONE);
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate /*
4432973Sgovinda  * Locate px_ino_t structure on ib_p->ib_ino_lst according to ino#
4440Sstevel@tonic-gate  * returns NULL if not found.
4450Sstevel@tonic-gate  */
4462973Sgovinda px_ino_t *
px_ib_locate_ino(px_ib_t * ib_p,devino_t ino_num)4470Sstevel@tonic-gate px_ib_locate_ino(px_ib_t *ib_p, devino_t ino_num)
4480Sstevel@tonic-gate {
4492973Sgovinda 	px_ino_t	*ino_p = ib_p->ib_ino_lst;
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
4520Sstevel@tonic-gate 
4535635Srameshc 	for (; ino_p && ino_p->ino_ino != ino_num; ino_p = ino_p->ino_next_p)
4545635Srameshc 		;
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	return (ino_p);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
45910053SEvan.Yan@Sun.COM px_ino_t *
px_ib_alloc_ino(px_ib_t * ib_p,devino_t ino_num)46010053SEvan.Yan@Sun.COM px_ib_alloc_ino(px_ib_t *ib_p, devino_t ino_num)
46110053SEvan.Yan@Sun.COM {
46210053SEvan.Yan@Sun.COM 	sysino_t	sysino;
46310053SEvan.Yan@Sun.COM 	px_ino_t	*ino_p;
46410053SEvan.Yan@Sun.COM 
46510053SEvan.Yan@Sun.COM 	if (px_lib_intr_devino_to_sysino(ib_p->ib_px_p->px_dip,
46610053SEvan.Yan@Sun.COM 	    ino_num, &sysino) != DDI_SUCCESS)
46710053SEvan.Yan@Sun.COM 		return (NULL);
46810053SEvan.Yan@Sun.COM 
46910053SEvan.Yan@Sun.COM 	ino_p = kmem_zalloc(sizeof (px_ino_t), KM_SLEEP);
47010053SEvan.Yan@Sun.COM 
47110053SEvan.Yan@Sun.COM 	ino_p->ino_next_p = ib_p->ib_ino_lst;
47210053SEvan.Yan@Sun.COM 	ib_p->ib_ino_lst = ino_p;
47310053SEvan.Yan@Sun.COM 
47410053SEvan.Yan@Sun.COM 	ino_p->ino_ino = ino_num;
47510053SEvan.Yan@Sun.COM 	ino_p->ino_sysino = sysino;
47610053SEvan.Yan@Sun.COM 	ino_p->ino_ib_p = ib_p;
47710053SEvan.Yan@Sun.COM 	ino_p->ino_unclaimed_intrs = 0;
47810053SEvan.Yan@Sun.COM 	ino_p->ino_lopil = 0;
47910053SEvan.Yan@Sun.COM 	ino_p->ino_cpuid = ino_p->ino_default_cpuid = (cpuid_t)-1;
48010053SEvan.Yan@Sun.COM 
48110053SEvan.Yan@Sun.COM 	return (ino_p);
48210053SEvan.Yan@Sun.COM }
48310053SEvan.Yan@Sun.COM 
4842973Sgovinda px_ino_pil_t *
px_ib_new_ino_pil(px_ib_t * ib_p,devino_t ino_num,uint_t pil,px_ih_t * ih_p)4852973Sgovinda px_ib_new_ino_pil(px_ib_t *ib_p, devino_t ino_num, uint_t pil, px_ih_t *ih_p)
4860Sstevel@tonic-gate {
4872973Sgovinda 	px_ino_pil_t	*ipil_p = kmem_zalloc(sizeof (px_ino_pil_t), KM_SLEEP);
4882973Sgovinda 	px_ino_t	*ino_p;
4892973Sgovinda 
49010053SEvan.Yan@Sun.COM 	if ((ino_p = px_ib_locate_ino(ib_p, ino_num)) == NULL)
49110053SEvan.Yan@Sun.COM 		ino_p = px_ib_alloc_ino(ib_p, ino_num);
4922973Sgovinda 
49310053SEvan.Yan@Sun.COM 	ASSERT(ino_p != NULL);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	ih_p->ih_next = ih_p;
4962973Sgovinda 	ipil_p->ipil_pil = pil;
4972973Sgovinda 	ipil_p->ipil_ih_head = ih_p;
4982973Sgovinda 	ipil_p->ipil_ih_tail = ih_p;
4992973Sgovinda 	ipil_p->ipil_ih_start = ih_p;
5002973Sgovinda 	ipil_p->ipil_ih_size = 1;
5012973Sgovinda 	ipil_p->ipil_ino_p = ino_p;
5020Sstevel@tonic-gate 
5032973Sgovinda 	ipil_p->ipil_next_p = ino_p->ino_ipil_p;
5042973Sgovinda 	ino_p->ino_ipil_p = ipil_p;
5052973Sgovinda 	ino_p->ino_ipil_size++;
5060Sstevel@tonic-gate 
50710053SEvan.Yan@Sun.COM 	if ((ino_p->ino_lopil == 0) || (ino_p->ino_lopil > pil))
5082973Sgovinda 		ino_p->ino_lopil = pil;
5092973Sgovinda 
5102973Sgovinda 	return (ipil_p);
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate void
px_ib_delete_ino_pil(px_ib_t * ib_p,px_ino_pil_t * ipil_p)5142973Sgovinda px_ib_delete_ino_pil(px_ib_t *ib_p, px_ino_pil_t *ipil_p)
5150Sstevel@tonic-gate {
5162973Sgovinda 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
5172973Sgovinda 	ushort_t	pil = ipil_p->ipil_pil;
5182973Sgovinda 	px_ino_pil_t	*prev, *next;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
5210Sstevel@tonic-gate 
5222973Sgovinda 	if (ino_p->ino_ipil_p == ipil_p)
5232973Sgovinda 		ino_p->ino_ipil_p = ipil_p->ipil_next_p;
5240Sstevel@tonic-gate 	else {
5252973Sgovinda 		for (prev = next = ino_p->ino_ipil_p; next != ipil_p;
5265635Srameshc 		    prev = next, next = next->ipil_next_p)
5275635Srameshc 			;
5282973Sgovinda 
5292973Sgovinda 		if (prev)
5302973Sgovinda 			prev->ipil_next_p = ipil_p->ipil_next_p;
5312973Sgovinda 	}
5322973Sgovinda 
5332973Sgovinda 	kmem_free(ipil_p, sizeof (px_ino_pil_t));
5342973Sgovinda 
5355962Srameshc 	if ((--ino_p->ino_ipil_size) && (ino_p->ino_lopil == pil)) {
5365962Srameshc 		for (next = ino_p->ino_ipil_p, pil = next->ipil_pil;
5375962Srameshc 		    next; next = next->ipil_next_p) {
5385962Srameshc 
5392973Sgovinda 			if (pil > next->ipil_pil)
5402973Sgovinda 				pil = next->ipil_pil;
5412973Sgovinda 		}
54210053SEvan.Yan@Sun.COM 
5435962Srameshc 		/*
5445962Srameshc 		 * Value stored in pil should be the lowest pil.
5455962Srameshc 		 */
5462973Sgovinda 		ino_p->ino_lopil = pil;
5472973Sgovinda 	}
5482973Sgovinda 
5495962Srameshc 	if (ino_p->ino_ipil_size)
5502973Sgovinda 		return;
5512973Sgovinda 
55210053SEvan.Yan@Sun.COM 	ino_p->ino_lopil = 0;
55310053SEvan.Yan@Sun.COM 
55410053SEvan.Yan@Sun.COM 	if (ino_p->ino_msiq_p)
55510053SEvan.Yan@Sun.COM 		return;
55610053SEvan.Yan@Sun.COM 
5572973Sgovinda 	if (ib_p->ib_ino_lst == ino_p)
5582973Sgovinda 		ib_p->ib_ino_lst = ino_p->ino_next_p;
5592973Sgovinda 	else {
5602973Sgovinda 		px_ino_t	*list = ib_p->ib_ino_lst;
5612973Sgovinda 
5625635Srameshc 		for (; list->ino_next_p != ino_p; list = list->ino_next_p)
5635635Srameshc 			;
5642973Sgovinda 		list->ino_next_p = ino_p->ino_next_p;
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate /*
5690Sstevel@tonic-gate  * Free all ino when we are detaching.
5700Sstevel@tonic-gate  */
5710Sstevel@tonic-gate void
px_ib_free_ino_all(px_ib_t * ib_p)5720Sstevel@tonic-gate px_ib_free_ino_all(px_ib_t *ib_p)
5730Sstevel@tonic-gate {
5742973Sgovinda 	px_ino_t	*ino_p = ib_p->ib_ino_lst;
5752973Sgovinda 	px_ino_t	*next = NULL;
5760Sstevel@tonic-gate 
5772973Sgovinda 	while (ino_p) {
5782973Sgovinda 		next = ino_p->ino_next_p;
5792973Sgovinda 		kmem_free(ino_p, sizeof (px_ino_t));
5802973Sgovinda 		ino_p = next;
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate 
5842973Sgovinda /*
5852973Sgovinda  * Locate px_ino_pil_t structure on ino_p->ino_ipil_p according to ino#
5862973Sgovinda  * returns NULL if not found.
5872973Sgovinda  */
5882973Sgovinda px_ino_pil_t *
px_ib_ino_locate_ipil(px_ino_t * ino_p,uint_t pil)5892973Sgovinda px_ib_ino_locate_ipil(px_ino_t *ino_p, uint_t pil)
5902973Sgovinda {
5912973Sgovinda 	px_ino_pil_t	*ipil_p = ino_p->ino_ipil_p;
5922973Sgovinda 
5935635Srameshc 	for (; ipil_p && ipil_p->ipil_pil != pil; ipil_p = ipil_p->ipil_next_p)
5945635Srameshc 		;
5952973Sgovinda 
5962973Sgovinda 	return (ipil_p);
5972973Sgovinda }
5982973Sgovinda 
5990Sstevel@tonic-gate int
px_ib_ino_add_intr(px_t * px_p,px_ino_pil_t * ipil_p,px_ih_t * ih_p)6002973Sgovinda px_ib_ino_add_intr(px_t *px_p, px_ino_pil_t *ipil_p, px_ih_t *ih_p)
6010Sstevel@tonic-gate {
6022973Sgovinda 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
6030Sstevel@tonic-gate 	px_ib_t		*ib_p = ino_p->ino_ib_p;
6040Sstevel@tonic-gate 	devino_t	ino = ino_p->ino_ino;
6050Sstevel@tonic-gate 	sysino_t	sysino = ino_p->ino_sysino;
6060Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
6070Sstevel@tonic-gate 	cpuid_t		curr_cpu;
6080Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
6110Sstevel@tonic-gate 	ASSERT(ib_p == px_p->px_ib_p);
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	DBG(DBG_IB, dip, "px_ib_ino_add_intr ino=%x\n", ino_p->ino_ino);
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	/* Disable the interrupt */
6160Sstevel@tonic-gate 	if ((ret = px_lib_intr_gettarget(dip, sysino,
6170Sstevel@tonic-gate 	    &curr_cpu)) != DDI_SUCCESS) {
6180Sstevel@tonic-gate 		DBG(DBG_IB, dip,
6190Sstevel@tonic-gate 		    "px_ib_ino_add_intr px_intr_gettarget() failed\n");
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 		return (ret);
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
624*12589SDavid.Major@Oracle.COM 	/* Wait on pending interrupt */
625*12589SDavid.Major@Oracle.COM 	if ((ret = px_ib_intr_pend(dip, sysino)) != DDI_SUCCESS) {
626*12589SDavid.Major@Oracle.COM 		cmn_err(CE_WARN, "%s%d: px_ib_ino_add_intr: pending "
627*12589SDavid.Major@Oracle.COM 		    "sysino 0x%lx(ino 0x%x) timeout",
628*12589SDavid.Major@Oracle.COM 		    ddi_driver_name(dip), ddi_get_instance(dip),
629*12589SDavid.Major@Oracle.COM 		    sysino, ino);
6300Sstevel@tonic-gate 	}
6310Sstevel@tonic-gate 
6322973Sgovinda 	/*
6332973Sgovinda 	 * If the interrupt was previously blocked (left in pending state)
6342973Sgovinda 	 * because of jabber we need to clear the pending state in case the
6352973Sgovinda 	 * jabber has gone away.
6362973Sgovinda 	 */
6372973Sgovinda 	if (ino_p->ino_unclaimed_intrs > px_unclaimed_intr_max) {
6382973Sgovinda 		cmn_err(CE_WARN,
6392973Sgovinda 		    "%s%d: px_ib_ino_add_intr: ino 0x%x has been unblocked",
6402973Sgovinda 		    ddi_driver_name(dip), ddi_get_instance(dip), ino);
6412973Sgovinda 
6422973Sgovinda 		ino_p->ino_unclaimed_intrs = 0;
6432973Sgovinda 		ret = px_lib_intr_setstate(dip, sysino, INTR_IDLE_STATE);
6442973Sgovinda 	}
6452973Sgovinda 
6460Sstevel@tonic-gate 	if (ret != DDI_SUCCESS) {
6470Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_ino_add_intr: failed, "
6480Sstevel@tonic-gate 		    "ino 0x%x sysino 0x%x\n", ino, sysino);
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 		return (ret);
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 
6532973Sgovinda 	/* Link up px_ih_t */
6542973Sgovinda 	ih_p->ih_next = ipil_p->ipil_ih_head;
6552973Sgovinda 	ipil_p->ipil_ih_tail->ih_next = ih_p;
6562973Sgovinda 	ipil_p->ipil_ih_tail = ih_p;
6570Sstevel@tonic-gate 
6582973Sgovinda 	ipil_p->ipil_ih_start = ipil_p->ipil_ih_head;
6592973Sgovinda 	ipil_p->ipil_ih_size++;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	/* Re-enable interrupt */
6620Sstevel@tonic-gate 	PX_INTR_ENABLE(dip, sysino, curr_cpu);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	return (ret);
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate /*
6682973Sgovinda  * Removes px_ih_t from the ino's link list.
6690Sstevel@tonic-gate  * uses hardware mutex to lock out interrupt threads.
6700Sstevel@tonic-gate  * Side effects: interrupt belongs to that ino is turned off on return.
6710Sstevel@tonic-gate  * if we are sharing PX slot with other inos, the caller needs
6720Sstevel@tonic-gate  * to turn it back on.
6730Sstevel@tonic-gate  */
6740Sstevel@tonic-gate int
px_ib_ino_rem_intr(px_t * px_p,px_ino_pil_t * ipil_p,px_ih_t * ih_p)6752973Sgovinda px_ib_ino_rem_intr(px_t *px_p, px_ino_pil_t *ipil_p, px_ih_t *ih_p)
6760Sstevel@tonic-gate {
6772973Sgovinda 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
6780Sstevel@tonic-gate 	devino_t	ino = ino_p->ino_ino;
6790Sstevel@tonic-gate 	sysino_t	sysino = ino_p->ino_sysino;
6800Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
6812973Sgovinda 	px_ih_t		*ih_lst = ipil_p->ipil_ih_head;
6820Sstevel@tonic-gate 	int		i, ret = DDI_SUCCESS;
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ino_p->ino_ib_p->ib_ino_lst_mutex));
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	DBG(DBG_IB, px_p->px_dip, "px_ib_ino_rem_intr ino=%x\n",
6870Sstevel@tonic-gate 	    ino_p->ino_ino);
6880Sstevel@tonic-gate 
689*12589SDavid.Major@Oracle.COM 	/* Wait on pending interrupt */
690*12589SDavid.Major@Oracle.COM 	if ((ret = px_ib_intr_pend(dip, sysino)) != DDI_SUCCESS) {
691*12589SDavid.Major@Oracle.COM 		cmn_err(CE_WARN, "%s%d: px_ib_ino_rem_intr: pending "
692*12589SDavid.Major@Oracle.COM 		    "sysino 0x%lx(ino 0x%x) timeout",
693*12589SDavid.Major@Oracle.COM 		    ddi_driver_name(dip), ddi_get_instance(dip),
694*12589SDavid.Major@Oracle.COM 		    sysino, ino);
6950Sstevel@tonic-gate 	}
6960Sstevel@tonic-gate 
6972973Sgovinda 	/*
6982973Sgovinda 	 * If the interrupt was previously blocked (left in pending state)
6992973Sgovinda 	 * because of jabber we need to clear the pending state in case the
7002973Sgovinda 	 * jabber has gone away.
7012973Sgovinda 	 */
70211836SDaniel.Ice@Sun.COM 	if (ret == DDI_SUCCESS &&
70311836SDaniel.Ice@Sun.COM 	    ino_p->ino_unclaimed_intrs > px_unclaimed_intr_max) {
7042973Sgovinda 		cmn_err(CE_WARN, "%s%d: px_ib_ino_rem_intr: "
7052973Sgovinda 		    "ino 0x%x has been unblocked",
7062973Sgovinda 		    ddi_driver_name(dip), ddi_get_instance(dip), ino);
7072973Sgovinda 
7082973Sgovinda 		ino_p->ino_unclaimed_intrs = 0;
7092973Sgovinda 		ret = px_lib_intr_setstate(dip, sysino, INTR_IDLE_STATE);
7102973Sgovinda 	}
7112973Sgovinda 
7120Sstevel@tonic-gate 	if (ret != DDI_SUCCESS) {
7130Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_ino_rem_intr: failed, "
7140Sstevel@tonic-gate 		    "ino 0x%x sysino 0x%x\n", ino, sysino);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 		return (ret);
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
71911836SDaniel.Ice@Sun.COM 	if (ipil_p->ipil_ih_size == 1) {
72011836SDaniel.Ice@Sun.COM 		if (ih_lst != ih_p)
72111836SDaniel.Ice@Sun.COM 			goto not_found;
72211836SDaniel.Ice@Sun.COM 
72311836SDaniel.Ice@Sun.COM 		/* No need to set head/tail as ino_p will be freed */
72411836SDaniel.Ice@Sun.COM 		goto reset;
72511836SDaniel.Ice@Sun.COM 	}
72611836SDaniel.Ice@Sun.COM 
7270Sstevel@tonic-gate 	/* Search the link list for ih_p */
7282973Sgovinda 	for (i = 0; (i < ipil_p->ipil_ih_size) &&
7295635Srameshc 	    (ih_lst->ih_next != ih_p); i++, ih_lst = ih_lst->ih_next)
7305635Srameshc 		;
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	if (ih_lst->ih_next != ih_p)
7330Sstevel@tonic-gate 		goto not_found;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	/* Remove ih_p from the link list and maintain the head/tail */
7360Sstevel@tonic-gate 	ih_lst->ih_next = ih_p->ih_next;
7370Sstevel@tonic-gate 
7382973Sgovinda 	if (ipil_p->ipil_ih_head == ih_p)
7392973Sgovinda 		ipil_p->ipil_ih_head = ih_p->ih_next;
7402973Sgovinda 	if (ipil_p->ipil_ih_tail == ih_p)
7412973Sgovinda 		ipil_p->ipil_ih_tail = ih_lst;
7420Sstevel@tonic-gate 
7432973Sgovinda 	ipil_p->ipil_ih_start = ipil_p->ipil_ih_head;
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate reset:
7460Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
7470Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
7480Sstevel@tonic-gate 	if (ih_p->ih_ksp != NULL)
7490Sstevel@tonic-gate 		kstat_delete(ih_p->ih_ksp);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (px_ih_t));
7522973Sgovinda 	ipil_p->ipil_ih_size--;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	return (ret);
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate not_found:
7570Sstevel@tonic-gate 	DBG(DBG_R_INTX, ino_p->ino_ib_p->ib_px_p->px_dip,
7585635Srameshc 	    "ino_p=%x does not have ih_p=%x\n", ino_p, ih_p);
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	return (DDI_FAILURE);
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate px_ih_t *
px_ib_intr_locate_ih(px_ino_pil_t * ipil_p,dev_info_t * rdip,uint32_t inum,msiq_rec_type_t rec_type,msgcode_t msg_code)7642973Sgovinda px_ib_intr_locate_ih(px_ino_pil_t *ipil_p, dev_info_t *rdip,
7650Sstevel@tonic-gate     uint32_t inum, msiq_rec_type_t rec_type, msgcode_t msg_code)
7660Sstevel@tonic-gate {
7672973Sgovinda 	px_ih_t	*ih_p = ipil_p->ipil_ih_head;
7680Sstevel@tonic-gate 	int	i;
7690Sstevel@tonic-gate 
7702973Sgovinda 	for (i = 0; i < ipil_p->ipil_ih_size; i++, ih_p = ih_p->ih_next) {
7712973Sgovinda 		if ((ih_p->ih_dip == rdip) && (ih_p->ih_inum == inum) &&
7722973Sgovinda 		    (ih_p->ih_rec_type == rec_type) &&
7732973Sgovinda 		    (ih_p->ih_msg_code == msg_code))
7742973Sgovinda 			return (ih_p);
7750Sstevel@tonic-gate 	}
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	return ((px_ih_t *)NULL);
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate px_ih_t *
px_ib_alloc_ih(dev_info_t * rdip,uint32_t inum,uint_t (* int_handler)(caddr_t int_handler_arg1,caddr_t int_handler_arg2),caddr_t int_handler_arg1,caddr_t int_handler_arg2,msiq_rec_type_t rec_type,msgcode_t msg_code)7810Sstevel@tonic-gate px_ib_alloc_ih(dev_info_t *rdip, uint32_t inum,
7820Sstevel@tonic-gate     uint_t (*int_handler)(caddr_t int_handler_arg1, caddr_t int_handler_arg2),
7830Sstevel@tonic-gate     caddr_t int_handler_arg1, caddr_t int_handler_arg2,
7840Sstevel@tonic-gate     msiq_rec_type_t rec_type, msgcode_t msg_code)
7850Sstevel@tonic-gate {
7860Sstevel@tonic-gate 	px_ih_t	*ih_p;
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	ih_p = kmem_alloc(sizeof (px_ih_t), KM_SLEEP);
7890Sstevel@tonic-gate 	ih_p->ih_dip = rdip;
7900Sstevel@tonic-gate 	ih_p->ih_inum = inum;
7910Sstevel@tonic-gate 	ih_p->ih_intr_state = PX_INTR_STATE_DISABLE;
79211520SScott.Carter@Sun.COM 	ih_p->ih_intr_flags = PX_INTR_IDLE;
7930Sstevel@tonic-gate 	ih_p->ih_handler = int_handler;
7940Sstevel@tonic-gate 	ih_p->ih_handler_arg1 = int_handler_arg1;
7950Sstevel@tonic-gate 	ih_p->ih_handler_arg2 = int_handler_arg2;
7960Sstevel@tonic-gate 	ih_p->ih_config_handle = NULL;
7970Sstevel@tonic-gate 	ih_p->ih_rec_type = rec_type;
7980Sstevel@tonic-gate 	ih_p->ih_msg_code = msg_code;
7990Sstevel@tonic-gate 	ih_p->ih_nsec = 0;
8000Sstevel@tonic-gate 	ih_p->ih_ticks = 0;
8010Sstevel@tonic-gate 	ih_p->ih_ksp = NULL;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	return (ih_p);
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate int
px_ib_update_intr_state(px_t * px_p,dev_info_t * rdip,uint_t inum,devino_t ino,uint_t pil,uint_t new_intr_state,msiq_rec_type_t rec_type,msgcode_t msg_code)8070Sstevel@tonic-gate px_ib_update_intr_state(px_t *px_p, dev_info_t *rdip,
8082973Sgovinda     uint_t inum, devino_t ino, uint_t pil,
8092973Sgovinda     uint_t new_intr_state, msiq_rec_type_t rec_type,
8102973Sgovinda     msgcode_t msg_code)
8110Sstevel@tonic-gate {
8120Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
8132973Sgovinda 	px_ino_t	*ino_p;
8142973Sgovinda 	px_ino_pil_t	*ipil_p;
8150Sstevel@tonic-gate 	px_ih_t		*ih_p;
8160Sstevel@tonic-gate 	int		ret = DDI_FAILURE;
8170Sstevel@tonic-gate 
8182973Sgovinda 	DBG(DBG_IB, px_p->px_dip, "px_ib_update_intr_state: %s%d "
8192973Sgovinda 	    "inum %x devino %x pil %x state %x\n", ddi_driver_name(rdip),
8202973Sgovinda 	    ddi_get_instance(rdip), inum, ino, pil, new_intr_state);
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
8230Sstevel@tonic-gate 
8242973Sgovinda 	ino_p = px_ib_locate_ino(ib_p, ino);
8252973Sgovinda 	if (ino_p && (ipil_p = px_ib_ino_locate_ipil(ino_p, pil))) {
8262973Sgovinda 		if (ih_p = px_ib_intr_locate_ih(ipil_p, rdip, inum, rec_type,
827909Segillett 		    msg_code)) {
8280Sstevel@tonic-gate 			ih_p->ih_intr_state = new_intr_state;
8290Sstevel@tonic-gate 			ret = DDI_SUCCESS;
8300Sstevel@tonic-gate 		}
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
8340Sstevel@tonic-gate 	return (ret);
8350Sstevel@tonic-gate }
836624Sschwartz 
837624Sschwartz 
83810053SEvan.Yan@Sun.COM /*
83910053SEvan.Yan@Sun.COM  * Get interrupt CPU for a given ino.
84010053SEvan.Yan@Sun.COM  * Return info only for inos which are already mapped to devices.
84110053SEvan.Yan@Sun.COM  */
84210053SEvan.Yan@Sun.COM /*ARGSUSED*/
84310053SEvan.Yan@Sun.COM int
px_ib_get_intr_target(px_t * px_p,devino_t ino,cpuid_t * cpu_id_p)84410053SEvan.Yan@Sun.COM px_ib_get_intr_target(px_t *px_p, devino_t ino, cpuid_t *cpu_id_p)
84510053SEvan.Yan@Sun.COM {
84610053SEvan.Yan@Sun.COM 	dev_info_t	*dip = px_p->px_dip;
84710053SEvan.Yan@Sun.COM 	sysino_t	sysino;
84810053SEvan.Yan@Sun.COM 	int		ret;
84910053SEvan.Yan@Sun.COM 
85010053SEvan.Yan@Sun.COM 	DBG(DBG_IB, px_p->px_dip, "px_ib_get_intr_target: devino %x\n", ino);
85110053SEvan.Yan@Sun.COM 
85210053SEvan.Yan@Sun.COM 	/* Convert leaf-wide intr to system-wide intr */
85310053SEvan.Yan@Sun.COM 	if (px_lib_intr_devino_to_sysino(dip, ino, &sysino) != DDI_SUCCESS)
85410053SEvan.Yan@Sun.COM 		return (DDI_FAILURE);
85510053SEvan.Yan@Sun.COM 
85610053SEvan.Yan@Sun.COM 	ret = px_lib_intr_gettarget(dip, sysino, cpu_id_p);
85710053SEvan.Yan@Sun.COM 
85810053SEvan.Yan@Sun.COM 	DBG(DBG_IB, px_p->px_dip, "px_ib_get_intr_target: cpu_id %x\n",
85910053SEvan.Yan@Sun.COM 	    *cpu_id_p);
86010053SEvan.Yan@Sun.COM 
86110053SEvan.Yan@Sun.COM 	return (ret);
86210053SEvan.Yan@Sun.COM }
86310053SEvan.Yan@Sun.COM 
86410053SEvan.Yan@Sun.COM 
86510053SEvan.Yan@Sun.COM /*
86610053SEvan.Yan@Sun.COM  * Associate a new CPU with a given ino.
86710053SEvan.Yan@Sun.COM  * Operate only on INOs which are already mapped to devices.
86810053SEvan.Yan@Sun.COM  */
86910053SEvan.Yan@Sun.COM int
px_ib_set_intr_target(px_t * px_p,devino_t ino,cpuid_t cpu_id)87010053SEvan.Yan@Sun.COM px_ib_set_intr_target(px_t *px_p, devino_t ino, cpuid_t cpu_id)
87110053SEvan.Yan@Sun.COM {
87210053SEvan.Yan@Sun.COM 	dev_info_t		*dip = px_p->px_dip;
87310053SEvan.Yan@Sun.COM 	cpuid_t			old_cpu_id;
87410053SEvan.Yan@Sun.COM 	sysino_t		sysino;
87510053SEvan.Yan@Sun.COM 	int			ret = DDI_SUCCESS;
87610053SEvan.Yan@Sun.COM 	extern const int	_ncpu;
87710053SEvan.Yan@Sun.COM 	extern cpu_t		*cpu[];
87810053SEvan.Yan@Sun.COM 
87910053SEvan.Yan@Sun.COM 	DBG(DBG_IB, px_p->px_dip, "px_ib_set_intr_target: devino %x "
88010053SEvan.Yan@Sun.COM 	    "cpu_id %x\n", ino, cpu_id);
88110053SEvan.Yan@Sun.COM 
88210053SEvan.Yan@Sun.COM 	mutex_enter(&cpu_lock);
88310053SEvan.Yan@Sun.COM 
88410053SEvan.Yan@Sun.COM 	/* Convert leaf-wide intr to system-wide intr */
88510053SEvan.Yan@Sun.COM 	if (px_lib_intr_devino_to_sysino(dip, ino, &sysino) != DDI_SUCCESS) {
88610053SEvan.Yan@Sun.COM 		ret = DDI_FAILURE;
88710053SEvan.Yan@Sun.COM 		goto done;
88810053SEvan.Yan@Sun.COM 	}
88910053SEvan.Yan@Sun.COM 
89010053SEvan.Yan@Sun.COM 	if (px_lib_intr_gettarget(dip, sysino, &old_cpu_id) != DDI_SUCCESS) {
89110053SEvan.Yan@Sun.COM 		ret = DDI_FAILURE;
89210053SEvan.Yan@Sun.COM 		goto done;
89310053SEvan.Yan@Sun.COM 	}
89410053SEvan.Yan@Sun.COM 
89510053SEvan.Yan@Sun.COM 	/*
89610053SEvan.Yan@Sun.COM 	 * Get lock, validate cpu and write it.
89710053SEvan.Yan@Sun.COM 	 */
89810053SEvan.Yan@Sun.COM 	if ((cpu_id < _ncpu) && (cpu[cpu_id] && cpu_is_online(cpu[cpu_id]))) {
89910053SEvan.Yan@Sun.COM 		DBG(DBG_IB, dip, "px_ib_set_intr_target: Enabling CPU %d\n",
90010053SEvan.Yan@Sun.COM 		    cpu_id);
90110053SEvan.Yan@Sun.COM 		px_ib_intr_dist_en(dip, cpu_id, ino, B_TRUE);
90210053SEvan.Yan@Sun.COM 		px_ib_log_new_cpu(px_p->px_ib_p, old_cpu_id, cpu_id, ino);
90310053SEvan.Yan@Sun.COM 	} else {	/* Invalid cpu */
90410053SEvan.Yan@Sun.COM 		DBG(DBG_IB, dip, "px_ib_set_intr_target: Invalid cpuid %x\n",
90510053SEvan.Yan@Sun.COM 		    cpu_id);
90610053SEvan.Yan@Sun.COM 		ret = DDI_EINVAL;
90710053SEvan.Yan@Sun.COM 	}
90810053SEvan.Yan@Sun.COM 
90910053SEvan.Yan@Sun.COM done:
91010053SEvan.Yan@Sun.COM 	mutex_exit(&cpu_lock);
91110053SEvan.Yan@Sun.COM 	return (ret);
91210053SEvan.Yan@Sun.COM }
91310053SEvan.Yan@Sun.COM 
91410053SEvan.Yan@Sun.COM hrtime_t px_ib_msix_retarget_timeout = 120ll * NANOSEC;	/* 120 seconds */
91510053SEvan.Yan@Sun.COM 
91610053SEvan.Yan@Sun.COM /*
91710053SEvan.Yan@Sun.COM  * Associate a new CPU with a given MSI/X.
91810053SEvan.Yan@Sun.COM  * Operate only on MSI/Xs which are already mapped to devices.
91910053SEvan.Yan@Sun.COM  */
92010053SEvan.Yan@Sun.COM int
px_ib_set_msix_target(px_t * px_p,ddi_intr_handle_impl_t * hdlp,msinum_t msi_num,cpuid_t cpu_id)92110053SEvan.Yan@Sun.COM px_ib_set_msix_target(px_t *px_p, ddi_intr_handle_impl_t *hdlp,
92210053SEvan.Yan@Sun.COM     msinum_t msi_num, cpuid_t cpu_id)
92310053SEvan.Yan@Sun.COM {
92410053SEvan.Yan@Sun.COM 	px_ib_t			*ib_p = px_p->px_ib_p;
92510053SEvan.Yan@Sun.COM 	px_msi_state_t		*msi_state_p = &px_p->px_ib_p->ib_msi_state;
92610053SEvan.Yan@Sun.COM 	dev_info_t		*dip = px_p->px_dip;
92710053SEvan.Yan@Sun.COM 	dev_info_t		*rdip = hdlp->ih_dip;
92810053SEvan.Yan@Sun.COM 	msiqid_t		msiq_id, old_msiq_id;
92910053SEvan.Yan@Sun.COM 	pci_msi_state_t		msi_state;
93010053SEvan.Yan@Sun.COM 	msiq_rec_type_t		msiq_rec_type;
93110053SEvan.Yan@Sun.COM 	msi_type_t		msi_type;
93210053SEvan.Yan@Sun.COM 	px_ino_t		*ino_p;
93310053SEvan.Yan@Sun.COM 	px_ih_t			*ih_p, *old_ih_p;
93410053SEvan.Yan@Sun.COM 	cpuid_t			old_cpu_id;
93510053SEvan.Yan@Sun.COM 	hrtime_t		start_time, end_time;
93610053SEvan.Yan@Sun.COM 	int			ret = DDI_SUCCESS;
93710053SEvan.Yan@Sun.COM 	extern const int	_ncpu;
93810053SEvan.Yan@Sun.COM 	extern cpu_t		*cpu[];
93910053SEvan.Yan@Sun.COM 
94010053SEvan.Yan@Sun.COM 	DBG(DBG_IB, dip, "px_ib_set_msix_target: msi_num %x new cpu_id %x\n",
94110053SEvan.Yan@Sun.COM 	    msi_num, cpu_id);
94210053SEvan.Yan@Sun.COM 
94310053SEvan.Yan@Sun.COM 	mutex_enter(&cpu_lock);
94410053SEvan.Yan@Sun.COM 
94510053SEvan.Yan@Sun.COM 	/* Check for MSI64 support */
94610053SEvan.Yan@Sun.COM 	if ((hdlp->ih_cap & DDI_INTR_FLAG_MSI64) && msi_state_p->msi_addr64) {
94710053SEvan.Yan@Sun.COM 		msiq_rec_type = MSI64_REC;
94810053SEvan.Yan@Sun.COM 		msi_type = MSI64_TYPE;
94910053SEvan.Yan@Sun.COM 	} else {
95010053SEvan.Yan@Sun.COM 		msiq_rec_type = MSI32_REC;
95110053SEvan.Yan@Sun.COM 		msi_type = MSI32_TYPE;
95210053SEvan.Yan@Sun.COM 	}
95310053SEvan.Yan@Sun.COM 
95410053SEvan.Yan@Sun.COM 	if ((ret = px_lib_msi_getmsiq(dip, msi_num,
95510053SEvan.Yan@Sun.COM 	    &old_msiq_id)) != DDI_SUCCESS) {
95610053SEvan.Yan@Sun.COM 
95710053SEvan.Yan@Sun.COM 		mutex_exit(&cpu_lock);
95810053SEvan.Yan@Sun.COM 		return (ret);
95910053SEvan.Yan@Sun.COM 	}
96010053SEvan.Yan@Sun.COM 
96110053SEvan.Yan@Sun.COM 	DBG(DBG_IB, dip, "px_ib_set_msix_target: current msiq 0x%x\n",
96210053SEvan.Yan@Sun.COM 	    old_msiq_id);
96310053SEvan.Yan@Sun.COM 
96410053SEvan.Yan@Sun.COM 	if ((ret = px_ib_get_intr_target(px_p,
96510053SEvan.Yan@Sun.COM 	    px_msiqid_to_devino(px_p, old_msiq_id),
96610053SEvan.Yan@Sun.COM 	    &old_cpu_id)) != DDI_SUCCESS) {
96710053SEvan.Yan@Sun.COM 
96810053SEvan.Yan@Sun.COM 		mutex_exit(&cpu_lock);
96910053SEvan.Yan@Sun.COM 		return (ret);
97010053SEvan.Yan@Sun.COM 	}
97110053SEvan.Yan@Sun.COM 
97210053SEvan.Yan@Sun.COM 	DBG(DBG_IB, dip, "px_ib_set_msix_target: current cpuid 0x%x\n",
97310053SEvan.Yan@Sun.COM 	    old_cpu_id);
97410053SEvan.Yan@Sun.COM 
97510053SEvan.Yan@Sun.COM 	if (cpu_id == old_cpu_id) {
97610053SEvan.Yan@Sun.COM 
97710053SEvan.Yan@Sun.COM 		mutex_exit(&cpu_lock);
97810053SEvan.Yan@Sun.COM 		return (DDI_SUCCESS);
97910053SEvan.Yan@Sun.COM 	}
98010053SEvan.Yan@Sun.COM 
98110053SEvan.Yan@Sun.COM 	/*
98210053SEvan.Yan@Sun.COM 	 * Get lock, validate cpu and write it.
98310053SEvan.Yan@Sun.COM 	 */
98410053SEvan.Yan@Sun.COM 	if (!((cpu_id < _ncpu) && (cpu[cpu_id] &&
98510053SEvan.Yan@Sun.COM 	    cpu_is_online(cpu[cpu_id])))) {
98610053SEvan.Yan@Sun.COM 		/* Invalid cpu */
98710053SEvan.Yan@Sun.COM 		DBG(DBG_IB, dip, "px_ib_set_msix_target: Invalid cpuid %x\n",
98810053SEvan.Yan@Sun.COM 		    cpu_id);
98910053SEvan.Yan@Sun.COM 
99010053SEvan.Yan@Sun.COM 		mutex_exit(&cpu_lock);
99110053SEvan.Yan@Sun.COM 		return (DDI_EINVAL);
99210053SEvan.Yan@Sun.COM 	}
99310053SEvan.Yan@Sun.COM 
99410053SEvan.Yan@Sun.COM 	DBG(DBG_IB, dip, "px_ib_set_msix_target: Enabling CPU %d\n", cpu_id);
99510053SEvan.Yan@Sun.COM 
99610053SEvan.Yan@Sun.COM 	if ((ret = px_add_msiq_intr(dip, rdip, hdlp,
99710053SEvan.Yan@Sun.COM 	    msiq_rec_type, msi_num, cpu_id, &msiq_id)) != DDI_SUCCESS) {
99810053SEvan.Yan@Sun.COM 		DBG(DBG_IB, dip, "px_ib_set_msix_target: Add MSI handler "
99910053SEvan.Yan@Sun.COM 		    "failed, rdip 0x%p msi 0x%x\n", rdip, msi_num);
100010053SEvan.Yan@Sun.COM 
100110053SEvan.Yan@Sun.COM 		mutex_exit(&cpu_lock);
100210053SEvan.Yan@Sun.COM 		return (ret);
100310053SEvan.Yan@Sun.COM 	}
100410053SEvan.Yan@Sun.COM 
100510053SEvan.Yan@Sun.COM 	if ((ret = px_lib_msi_setmsiq(dip, msi_num,
100610053SEvan.Yan@Sun.COM 	    msiq_id, msi_type)) != DDI_SUCCESS) {
100711520SScott.Carter@Sun.COM 		mutex_exit(&cpu_lock);
100811520SScott.Carter@Sun.COM 
100910053SEvan.Yan@Sun.COM 		(void) px_rem_msiq_intr(dip, rdip,
101010053SEvan.Yan@Sun.COM 		    hdlp, msiq_rec_type, msi_num, msiq_id);
101110053SEvan.Yan@Sun.COM 
101210053SEvan.Yan@Sun.COM 		return (ret);
101310053SEvan.Yan@Sun.COM 	}
101410053SEvan.Yan@Sun.COM 
101510053SEvan.Yan@Sun.COM 	if ((ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
101610053SEvan.Yan@Sun.COM 	    px_msiqid_to_devino(px_p, msiq_id), hdlp->ih_pri,
101710053SEvan.Yan@Sun.COM 	    PX_INTR_STATE_ENABLE, msiq_rec_type, msi_num)) != DDI_SUCCESS) {
101811520SScott.Carter@Sun.COM 		mutex_exit(&cpu_lock);
101911520SScott.Carter@Sun.COM 
102010053SEvan.Yan@Sun.COM 		(void) px_rem_msiq_intr(dip, rdip,
102110053SEvan.Yan@Sun.COM 		    hdlp, msiq_rec_type, msi_num, msiq_id);
102210053SEvan.Yan@Sun.COM 
102310053SEvan.Yan@Sun.COM 		return (ret);
102410053SEvan.Yan@Sun.COM 	}
102510053SEvan.Yan@Sun.COM 
102610053SEvan.Yan@Sun.COM 	mutex_exit(&cpu_lock);
102711520SScott.Carter@Sun.COM 
102811520SScott.Carter@Sun.COM 	/*
102911520SScott.Carter@Sun.COM 	 * Remove the old handler, but first ensure it is finished.
103011520SScott.Carter@Sun.COM 	 *
103111520SScott.Carter@Sun.COM 	 * Each handler sets its PENDING flag before it clears the MSI state.
103211520SScott.Carter@Sun.COM 	 * Then it clears that flag when finished.  If a re-target occurs while
103311520SScott.Carter@Sun.COM 	 * the MSI state is DELIVERED, then it is not yet known which of the
103411520SScott.Carter@Sun.COM 	 * two handlers will take the interrupt.  So the re-target operation
103511520SScott.Carter@Sun.COM 	 * sets a RETARGET flag on both handlers in that case.  Monitoring both
103611520SScott.Carter@Sun.COM 	 * flags on both handlers then determines when the old handler can be
103711520SScott.Carter@Sun.COM 	 * be safely removed.
103811520SScott.Carter@Sun.COM 	 */
103910053SEvan.Yan@Sun.COM 	mutex_enter(&ib_p->ib_ino_lst_mutex);
104010053SEvan.Yan@Sun.COM 
104110053SEvan.Yan@Sun.COM 	ino_p = px_ib_locate_ino(ib_p, px_msiqid_to_devino(px_p, old_msiq_id));
104210053SEvan.Yan@Sun.COM 	old_ih_p = px_ib_intr_locate_ih(px_ib_ino_locate_ipil(ino_p,
104310053SEvan.Yan@Sun.COM 	    hdlp->ih_pri), rdip, hdlp->ih_inum, msiq_rec_type, msi_num);
104410053SEvan.Yan@Sun.COM 
104510053SEvan.Yan@Sun.COM 	ino_p = px_ib_locate_ino(ib_p, px_msiqid_to_devino(px_p, msiq_id));
104610053SEvan.Yan@Sun.COM 	ih_p = px_ib_intr_locate_ih(px_ib_ino_locate_ipil(ino_p, hdlp->ih_pri),
104710053SEvan.Yan@Sun.COM 	    rdip, hdlp->ih_inum, msiq_rec_type, msi_num);
104810053SEvan.Yan@Sun.COM 
104910053SEvan.Yan@Sun.COM 	if ((ret = px_lib_msi_getstate(dip, msi_num,
105010053SEvan.Yan@Sun.COM 	    &msi_state)) != DDI_SUCCESS) {
105110053SEvan.Yan@Sun.COM 		(void) px_rem_msiq_intr(dip, rdip,
105210053SEvan.Yan@Sun.COM 		    hdlp, msiq_rec_type, msi_num, msiq_id);
105310053SEvan.Yan@Sun.COM 
105410053SEvan.Yan@Sun.COM 		mutex_exit(&ib_p->ib_ino_lst_mutex);
105510053SEvan.Yan@Sun.COM 		return (ret);
105610053SEvan.Yan@Sun.COM 	}
105710053SEvan.Yan@Sun.COM 
105811520SScott.Carter@Sun.COM 	if (msi_state == PCI_MSI_STATE_DELIVERED) {
105911520SScott.Carter@Sun.COM 		ih_p->ih_intr_flags |= PX_INTR_RETARGET;
106011520SScott.Carter@Sun.COM 		old_ih_p->ih_intr_flags |= PX_INTR_RETARGET;
106111520SScott.Carter@Sun.COM 	}
106210053SEvan.Yan@Sun.COM 
106310053SEvan.Yan@Sun.COM 	start_time = gethrtime();
106411520SScott.Carter@Sun.COM 	while (((ih_p->ih_intr_flags & PX_INTR_RETARGET) &&
106511520SScott.Carter@Sun.COM 	    (old_ih_p->ih_intr_flags & PX_INTR_RETARGET)) ||
106611520SScott.Carter@Sun.COM 	    (old_ih_p->ih_intr_flags & PX_INTR_PENDING)) {
106711520SScott.Carter@Sun.COM 
106811520SScott.Carter@Sun.COM 		/* Wait for one second */
106911520SScott.Carter@Sun.COM 		delay(drv_usectohz(1000000));
107011520SScott.Carter@Sun.COM 
107111520SScott.Carter@Sun.COM 		end_time = gethrtime() - start_time;
107211520SScott.Carter@Sun.COM 		if (end_time > px_ib_msix_retarget_timeout) {
107310053SEvan.Yan@Sun.COM 			cmn_err(CE_WARN, "MSIX retarget %x is not completed, "
107410053SEvan.Yan@Sun.COM 			    "even after waiting %llx ticks\n",
107510053SEvan.Yan@Sun.COM 			    msi_num, end_time);
107610053SEvan.Yan@Sun.COM 			break;
107710053SEvan.Yan@Sun.COM 		}
107811520SScott.Carter@Sun.COM 	}
107910053SEvan.Yan@Sun.COM 
108011520SScott.Carter@Sun.COM 	ih_p->ih_intr_flags &= ~(PX_INTR_RETARGET);
108110053SEvan.Yan@Sun.COM 
108210053SEvan.Yan@Sun.COM 	mutex_exit(&ib_p->ib_ino_lst_mutex);
108310053SEvan.Yan@Sun.COM 
108410053SEvan.Yan@Sun.COM 	ret = px_rem_msiq_intr(dip, rdip,
108510053SEvan.Yan@Sun.COM 	    hdlp, msiq_rec_type, msi_num, old_msiq_id);
108610053SEvan.Yan@Sun.COM 
108710053SEvan.Yan@Sun.COM 	return (ret);
108810053SEvan.Yan@Sun.COM }
108910053SEvan.Yan@Sun.COM 
109010053SEvan.Yan@Sun.COM 
1091624Sschwartz static void
px_fill_in_intr_devs(pcitool_intr_dev_t * dev,char * driver_name,char * path_name,int instance)1092624Sschwartz px_fill_in_intr_devs(pcitool_intr_dev_t *dev, char *driver_name,
1093624Sschwartz     char *path_name, int instance)
1094624Sschwartz {
1095624Sschwartz 	(void) strncpy(dev->driver_name, driver_name, MAXMODCONFNAME-1);
1096624Sschwartz 	dev->driver_name[MAXMODCONFNAME] = '\0';
1097624Sschwartz 	(void) strncpy(dev->path, path_name, MAXPATHLEN-1);
1098624Sschwartz 	dev->dev_inst = instance;
1099624Sschwartz }
1100624Sschwartz 
1101624Sschwartz 
1102624Sschwartz /*
1103624Sschwartz  * Return the dips or number of dips associated with a given interrupt block.
1104624Sschwartz  * Size of dips array arg is passed in as dips_ret arg.
1105624Sschwartz  * Number of dips returned is returned in dips_ret arg.
1106624Sschwartz  * Array of dips gets returned in the dips argument.
1107624Sschwartz  * Function returns number of dips existing for the given interrupt block.
1108624Sschwartz  *
1109624Sschwartz  * Note: this function assumes an enabled/valid INO, which is why it returns
1110624Sschwartz  * the px node and (Internal) when it finds no other devices (and *devs_ret > 0)
1111624Sschwartz  */
1112624Sschwartz uint8_t
pxtool_ib_get_ino_devs(px_t * px_p,uint32_t ino,uint32_t msi_num,uint8_t * devs_ret,pcitool_intr_dev_t * devs)111310053SEvan.Yan@Sun.COM pxtool_ib_get_ino_devs(px_t *px_p, uint32_t ino, uint32_t msi_num,
111410053SEvan.Yan@Sun.COM     uint8_t *devs_ret, pcitool_intr_dev_t *devs)
1115624Sschwartz {
11162973Sgovinda 	px_ib_t		*ib_p = px_p->px_ib_p;
11172973Sgovinda 	px_ino_t	*ino_p;
11182973Sgovinda 	px_ino_pil_t	*ipil_p;
11192973Sgovinda 	px_ih_t 	*ih_p;
11202973Sgovinda 	uint32_t 	num_devs = 0;
11212973Sgovinda 	char		pathname[MAXPATHLEN];
11222973Sgovinda 	int		i, j;
1123624Sschwartz 
1124624Sschwartz 	mutex_enter(&ib_p->ib_ino_lst_mutex);
1125624Sschwartz 	ino_p = px_ib_locate_ino(ib_p, ino);
1126624Sschwartz 	if (ino_p != NULL) {
11272973Sgovinda 		for (j = 0, ipil_p = ino_p->ino_ipil_p; ipil_p;
11282973Sgovinda 		    ipil_p = ipil_p->ipil_next_p) {
11292973Sgovinda 			num_devs += ipil_p->ipil_ih_size;
11302973Sgovinda 
11312973Sgovinda 			for (i = 0, ih_p = ipil_p->ipil_ih_head;
11322973Sgovinda 			    ((i < ipil_p->ipil_ih_size) && (i < *devs_ret));
11332973Sgovinda 			    i++, j++, ih_p = ih_p->ih_next) {
11342973Sgovinda 				(void) ddi_pathname(ih_p->ih_dip, pathname);
113510053SEvan.Yan@Sun.COM 
113610053SEvan.Yan@Sun.COM 				if (ih_p->ih_msg_code == msi_num) {
113710053SEvan.Yan@Sun.COM 					num_devs = *devs_ret = 1;
113810053SEvan.Yan@Sun.COM 					px_fill_in_intr_devs(&devs[0],
113910053SEvan.Yan@Sun.COM 					    (char *)ddi_driver_name(
114010053SEvan.Yan@Sun.COM 					    ih_p->ih_dip), pathname,
114110053SEvan.Yan@Sun.COM 					    ddi_get_instance(ih_p->ih_dip));
114210053SEvan.Yan@Sun.COM 					goto done;
114310053SEvan.Yan@Sun.COM 				}
114410053SEvan.Yan@Sun.COM 
114510053SEvan.Yan@Sun.COM 				px_fill_in_intr_devs(&devs[j],
11462973Sgovinda 				    (char *)ddi_driver_name(ih_p->ih_dip),
11472973Sgovinda 				    pathname, ddi_get_instance(ih_p->ih_dip));
11482973Sgovinda 			}
1149624Sschwartz 		}
1150624Sschwartz 
11512973Sgovinda 		*devs_ret = j;
1152624Sschwartz 	} else if (*devs_ret > 0) {
1153624Sschwartz 		(void) ddi_pathname(px_p->px_dip, pathname);
1154624Sschwartz 		strcat(pathname, " (Internal)");
1155624Sschwartz 		px_fill_in_intr_devs(&devs[0],
1156624Sschwartz 		    (char *)ddi_driver_name(px_p->px_dip),  pathname,
1157624Sschwartz 		    ddi_get_instance(px_p->px_dip));
1158624Sschwartz 		num_devs = *devs_ret = 1;
1159624Sschwartz 	}
1160624Sschwartz 
116110053SEvan.Yan@Sun.COM done:
1162624Sschwartz 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1163624Sschwartz 
1164624Sschwartz 	return (num_devs);
1165624Sschwartz }
1166624Sschwartz 
1167624Sschwartz 
116810053SEvan.Yan@Sun.COM int
pxtool_ib_get_msi_info(px_t * px_p,devino_t ino,msinum_t msi_num,ddi_intr_handle_impl_t * hdlp)116910053SEvan.Yan@Sun.COM pxtool_ib_get_msi_info(px_t *px_p, devino_t ino, msinum_t msi_num,
117010053SEvan.Yan@Sun.COM     ddi_intr_handle_impl_t *hdlp)
117110053SEvan.Yan@Sun.COM {
117210053SEvan.Yan@Sun.COM 	px_ib_t		*ib_p = px_p->px_ib_p;
117310053SEvan.Yan@Sun.COM 	px_ino_t	*ino_p;
117410053SEvan.Yan@Sun.COM 	px_ino_pil_t	*ipil_p;
117510053SEvan.Yan@Sun.COM 	px_ih_t 	*ih_p;
117610053SEvan.Yan@Sun.COM 	int		i;
117710053SEvan.Yan@Sun.COM 
117810053SEvan.Yan@Sun.COM 	mutex_enter(&ib_p->ib_ino_lst_mutex);
117910053SEvan.Yan@Sun.COM 
118010053SEvan.Yan@Sun.COM 	if ((ino_p = px_ib_locate_ino(ib_p, ino)) == NULL) {
118110053SEvan.Yan@Sun.COM 		mutex_exit(&ib_p->ib_ino_lst_mutex);
118210053SEvan.Yan@Sun.COM 		return (DDI_FAILURE);
118310053SEvan.Yan@Sun.COM 	}
118410053SEvan.Yan@Sun.COM 
118510053SEvan.Yan@Sun.COM 	for (ipil_p = ino_p->ino_ipil_p; ipil_p;
118610053SEvan.Yan@Sun.COM 	    ipil_p = ipil_p->ipil_next_p) {
118710053SEvan.Yan@Sun.COM 		for (i = 0, ih_p = ipil_p->ipil_ih_head;
118810053SEvan.Yan@Sun.COM 		    ((i < ipil_p->ipil_ih_size) && ih_p);
118910053SEvan.Yan@Sun.COM 		    i++, ih_p = ih_p->ih_next) {
119010053SEvan.Yan@Sun.COM 
119110053SEvan.Yan@Sun.COM 			if (ih_p->ih_msg_code != msi_num)
119210053SEvan.Yan@Sun.COM 				continue;
119310053SEvan.Yan@Sun.COM 
119410053SEvan.Yan@Sun.COM 			hdlp->ih_dip = ih_p->ih_dip;
119510053SEvan.Yan@Sun.COM 			hdlp->ih_inum = ih_p->ih_inum;
119610053SEvan.Yan@Sun.COM 			hdlp->ih_cb_func = ih_p->ih_handler;
119710053SEvan.Yan@Sun.COM 			hdlp->ih_cb_arg1 = ih_p->ih_handler_arg1;
119810053SEvan.Yan@Sun.COM 			hdlp->ih_cb_arg2 = ih_p->ih_handler_arg2;
119910053SEvan.Yan@Sun.COM 			if (ih_p->ih_rec_type == MSI64_REC)
120010053SEvan.Yan@Sun.COM 				hdlp->ih_cap = DDI_INTR_FLAG_MSI64;
120110053SEvan.Yan@Sun.COM 			hdlp->ih_pri = ipil_p->ipil_pil;
120210053SEvan.Yan@Sun.COM 			hdlp->ih_ver = DDI_INTR_VERSION;
120310053SEvan.Yan@Sun.COM 
120410053SEvan.Yan@Sun.COM 			mutex_exit(&ib_p->ib_ino_lst_mutex);
120510053SEvan.Yan@Sun.COM 			return (DDI_SUCCESS);
120610053SEvan.Yan@Sun.COM 		}
120710053SEvan.Yan@Sun.COM 	}
120810053SEvan.Yan@Sun.COM 
120910053SEvan.Yan@Sun.COM 	mutex_exit(&ib_p->ib_ino_lst_mutex);
121010053SEvan.Yan@Sun.COM 	return (DDI_FAILURE);
121110053SEvan.Yan@Sun.COM }
121210053SEvan.Yan@Sun.COM 
12131617Sgovinda void
px_ib_log_new_cpu(px_ib_t * ib_p,cpuid_t old_cpu_id,cpuid_t new_cpu_id,uint32_t ino)121410053SEvan.Yan@Sun.COM px_ib_log_new_cpu(px_ib_t *ib_p, cpuid_t old_cpu_id, cpuid_t new_cpu_id,
1215624Sschwartz     uint32_t ino)
1216624Sschwartz {
12172973Sgovinda 	px_ino_t	*ino_p;
12182973Sgovinda 	px_ino_pil_t	*ipil_p;
12192973Sgovinda 	px_ih_t 	*ih_p;
12202973Sgovinda 	int		i;
1221624Sschwartz 
1222624Sschwartz 	mutex_enter(&ib_p->ib_ino_lst_mutex);
1223624Sschwartz 
1224624Sschwartz 	/* Log in OS data structures the new CPU. */
12252973Sgovinda 	if (ino_p = px_ib_locate_ino(ib_p, ino)) {
1226624Sschwartz 
1227624Sschwartz 		/* Log in OS data structures the new CPU. */
1228624Sschwartz 		ino_p->ino_cpuid = new_cpu_id;
1229624Sschwartz 
12302973Sgovinda 		for (ipil_p = ino_p->ino_ipil_p; ipil_p;
12312973Sgovinda 		    ipil_p = ipil_p->ipil_next_p) {
12322973Sgovinda 			for (i = 0, ih_p = ipil_p->ipil_ih_head;
12332973Sgovinda 			    (i < ipil_p->ipil_ih_size);
12342973Sgovinda 			    i++, ih_p = ih_p->ih_next) {
12352973Sgovinda 				/*
12362973Sgovinda 				 * Account for any residual time
12372973Sgovinda 				 * to be logged for old cpu.
12382973Sgovinda 				 */
12392973Sgovinda 				px_ib_cpu_ticks_to_ih_nsec(ib_p,
12402973Sgovinda 				    ih_p, old_cpu_id);
12412973Sgovinda 			}
12422973Sgovinda 		}
1243624Sschwartz 	}
1244624Sschwartz 
1245624Sschwartz 	mutex_exit(&ib_p->ib_ino_lst_mutex);
1246624Sschwartz }
1247