xref: /onnv-gate/usr/src/uts/sun4/io/px/px_ib.c (revision 624:8c5206bfd8f1)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * PX Interrupt Block implementation
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/async.h>
360Sstevel@tonic-gate #include <sys/systm.h>		/* panicstr */
370Sstevel@tonic-gate #include <sys/spl.h>
380Sstevel@tonic-gate #include <sys/sunddi.h>
390Sstevel@tonic-gate #include <sys/machsystm.h>	/* intr_dist_add */
400Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
410Sstevel@tonic-gate #include <sys/cpuvar.h>
420Sstevel@tonic-gate #include "px_obj.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*LINTLIBRARY*/
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static void px_ib_intr_redist(void *arg, int32_t weight_max, int32_t weight);
47*624Sschwartz static void px_ib_cpu_ticks_to_ih_nsec(px_ib_t *ib_p, px_ih_t *ih_p,
48*624Sschwartz     uint32_t cpu_id);
490Sstevel@tonic-gate static uint_t px_ib_intr_reset(void *arg);
50*624Sschwartz static void px_fill_in_intr_devs(pcitool_intr_dev_t *dev, char *driver_name,
51*624Sschwartz     char *path_name, int instance);
520Sstevel@tonic-gate 
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;
740Sstevel@tonic-gate 	ib_p->ib_ino_lst = (px_ib_ino_info_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);
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
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 
1670Sstevel@tonic-gate 
168*624Sschwartz void
1690Sstevel@tonic-gate px_ib_intr_dist_en(dev_info_t *dip, cpuid_t cpu_id, devino_t ino,
1700Sstevel@tonic-gate     boolean_t wait_flag)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate 	uint32_t	old_cpu_id;
1730Sstevel@tonic-gate 	sysino_t	sysino;
1740Sstevel@tonic-gate 	intr_valid_state_t	enabled = 0;
1750Sstevel@tonic-gate 	hrtime_t	start_time;
1760Sstevel@tonic-gate 	intr_state_t	intr_state;
17727Sjchu 	int		e = DDI_SUCCESS;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	DBG(DBG_IB, dip, "px_ib_intr_dist_en: ino=0x%x\n", ino);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (px_lib_intr_devino_to_sysino(dip, ino, &sysino) != DDI_SUCCESS) {
1820Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: "
1830Sstevel@tonic-gate 		    "px_intr_devino_to_sysino() failed, ino 0x%x\n", ino);
1840Sstevel@tonic-gate 		return;
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	/* Skip enabling disabled interrupts */
1880Sstevel@tonic-gate 	if (px_lib_intr_getvalid(dip, sysino, &enabled) != DDI_SUCCESS) {
1890Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: px_intr_getvalid() "
1900Sstevel@tonic-gate 		    "failed, sysino 0x%x\n", sysino);
1910Sstevel@tonic-gate 		return;
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 	if (!enabled)
1940Sstevel@tonic-gate 		return;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	/* Done if redistributed onto the same cpuid */
1970Sstevel@tonic-gate 	if (px_lib_intr_gettarget(dip, sysino, &old_cpu_id) != DDI_SUCCESS) {
1980Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: "
1990Sstevel@tonic-gate 		    "px_intr_gettarget() failed\n");
2000Sstevel@tonic-gate 		return;
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 	if (cpu_id == old_cpu_id)
2030Sstevel@tonic-gate 		return;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	if (!wait_flag)
2060Sstevel@tonic-gate 		goto done;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	/* Busy wait on pending interrupts */
2090Sstevel@tonic-gate 	PX_INTR_DISABLE(dip, sysino);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	for (start_time = gethrtime(); !panicstr &&
2120Sstevel@tonic-gate 	    ((e = px_lib_intr_getstate(dip, sysino, &intr_state)) ==
2130Sstevel@tonic-gate 		DDI_SUCCESS) &&
2140Sstevel@tonic-gate 	    (intr_state == INTR_DELIVERED_STATE); /* */) {
2150Sstevel@tonic-gate 		if (gethrtime() - start_time > px_intrpend_timeout) {
2160Sstevel@tonic-gate 			cmn_err(CE_WARN,
2170Sstevel@tonic-gate 			    "%s%d: px_ib_intr_dist_en: sysino 0x%x(ino 0x%x) "
2180Sstevel@tonic-gate 			    "from cpu id 0x%x to 0x%x timeout",
2190Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
2200Sstevel@tonic-gate 			    sysino, ino, old_cpu_id, cpu_id);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 			e = DDI_FAILURE;
2230Sstevel@tonic-gate 			break;
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	if (e != DDI_SUCCESS)
2280Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: failed, "
2290Sstevel@tonic-gate 		    "ino 0x%x sysino 0x%x\n", ino, sysino);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate done:
2320Sstevel@tonic-gate 	PX_INTR_ENABLE(dip, sysino, cpu_id);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
235*624Sschwartz static void
236*624Sschwartz px_ib_cpu_ticks_to_ih_nsec(px_ib_t *ib_p, px_ih_t *ih_p, uint32_t cpu_id)
237*624Sschwartz {
238*624Sschwartz 	extern kmutex_t pxintr_ks_template_lock;
239*624Sschwartz 	hrtime_t ticks;
240*624Sschwartz 
241*624Sschwartz 	/*
242*624Sschwartz 	 * Because we are updating two fields in ih_t we must lock
243*624Sschwartz 	 * pxintr_ks_template_lock to prevent someone from reading the
244*624Sschwartz 	 * kstats after we set ih_ticks to 0 and before we increment
245*624Sschwartz 	 * ih_nsec to compensate.
246*624Sschwartz 	 *
247*624Sschwartz 	 * We must also protect against the interrupt arriving and incrementing
248*624Sschwartz 	 * ih_ticks between the time we read it and when we reset it to 0.
249*624Sschwartz 	 * To do this we use atomic_swap.
250*624Sschwartz 	 */
251*624Sschwartz 
252*624Sschwartz 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
253*624Sschwartz 
254*624Sschwartz 	mutex_enter(&pxintr_ks_template_lock);
255*624Sschwartz 	ticks = atomic_swap_64(&ih_p->ih_ticks, 0);
256*624Sschwartz 	ih_p->ih_nsec += (uint64_t)tick2ns(ticks, cpu_id);
257*624Sschwartz 	mutex_exit(&pxintr_ks_template_lock);
258*624Sschwartz }
259*624Sschwartz 
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate  * Redistribute interrupts of the specified weight. The first call has a weight
2630Sstevel@tonic-gate  * of weight_max, which can be used to trigger initialization for
2640Sstevel@tonic-gate  * redistribution. The inos with weight [weight_max, inf.) should be processed
2650Sstevel@tonic-gate  * on the "weight == weight_max" call.  This first call is followed by calls
2660Sstevel@tonic-gate  * of decreasing weights, inos of that weight should be processed.  The final
2670Sstevel@tonic-gate  * call specifies a weight of zero, this can be used to trigger processing of
2680Sstevel@tonic-gate  * stragglers.
2690Sstevel@tonic-gate  */
2700Sstevel@tonic-gate static void
2710Sstevel@tonic-gate px_ib_intr_redist(void *arg, int32_t weight_max, int32_t weight)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	px_ib_t		*ib_p = (px_ib_t *)arg;
2740Sstevel@tonic-gate 	px_t		*px_p = ib_p->ib_px_p;
2750Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
2760Sstevel@tonic-gate 	px_ib_ino_info_t *ino_p;
2770Sstevel@tonic-gate 	px_ih_t		*ih_lst;
2780Sstevel@tonic-gate 	int32_t		dweight = 0;
2790Sstevel@tonic-gate 	int		i;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	/* Redistribute internal interrupts */
2820Sstevel@tonic-gate 	if (weight == 0) {
2830Sstevel@tonic-gate 		devino_t	ino_pec = px_p->px_inos[PX_INTR_PEC];
28427Sjchu 
2850Sstevel@tonic-gate 		mutex_enter(&ib_p->ib_intr_lock);
2860Sstevel@tonic-gate 		px_ib_intr_dist_en(dip, intr_dist_cpuid(), ino_pec, B_FALSE);
2870Sstevel@tonic-gate 		mutex_exit(&ib_p->ib_intr_lock);
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/* Redistribute device interrupts */
2910Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	for (ino_p = ib_p->ib_ino_lst; ino_p; ino_p = ino_p->ino_next) {
2940Sstevel@tonic-gate 		uint32_t orig_cpuid;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 		/*
2970Sstevel@tonic-gate 		 * Recomputes the sum of interrupt weights of devices that
2980Sstevel@tonic-gate 		 * share the same ino upon first call marked by
2990Sstevel@tonic-gate 		 * (weight == weight_max).
3000Sstevel@tonic-gate 		 */
3010Sstevel@tonic-gate 		if (weight == weight_max) {
3020Sstevel@tonic-gate 			ino_p->ino_intr_weight = 0;
3030Sstevel@tonic-gate 			for (i = 0, ih_lst = ino_p->ino_ih_head;
3040Sstevel@tonic-gate 			    i < ino_p->ino_ih_size;
3050Sstevel@tonic-gate 			    i++, ih_lst = ih_lst->ih_next) {
3060Sstevel@tonic-gate 				dweight = i_ddi_get_intr_weight(ih_lst->ih_dip);
3070Sstevel@tonic-gate 				if (dweight > 0)
3080Sstevel@tonic-gate 					ino_p->ino_intr_weight += dweight;
3090Sstevel@tonic-gate 			}
3100Sstevel@tonic-gate 		}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		/*
3130Sstevel@tonic-gate 		 * As part of redistributing weighted interrupts over cpus,
3140Sstevel@tonic-gate 		 * nexus redistributes device interrupts and updates
3150Sstevel@tonic-gate 		 * cpu weight. The purpose is for the most light weighted
3160Sstevel@tonic-gate 		 * cpu to take the next interrupt and gain weight, therefore
3170Sstevel@tonic-gate 		 * attention demanding device gains more cpu attention by
3180Sstevel@tonic-gate 		 * making itself heavy.
3190Sstevel@tonic-gate 		 */
3200Sstevel@tonic-gate 		if ((weight == ino_p->ino_intr_weight) ||
3210Sstevel@tonic-gate 		    ((weight >= weight_max) &&
3220Sstevel@tonic-gate 		    (ino_p->ino_intr_weight >= weight_max))) {
3230Sstevel@tonic-gate 			orig_cpuid = ino_p->ino_cpuid;
3240Sstevel@tonic-gate 			if (cpu[orig_cpuid] == NULL)
3250Sstevel@tonic-gate 				orig_cpuid = CPU->cpu_id;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 			/* select cpuid to target and mark ino established */
3280Sstevel@tonic-gate 			ino_p->ino_cpuid = intr_dist_cpuid();
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 			/* Add device weight to targeted cpu. */
3310Sstevel@tonic-gate 			for (i = 0, ih_lst = ino_p->ino_ih_head;
3320Sstevel@tonic-gate 			    i < ino_p->ino_ih_size;
3330Sstevel@tonic-gate 			    i++, ih_lst = ih_lst->ih_next) {
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 				dweight = i_ddi_get_intr_weight(ih_lst->ih_dip);
3360Sstevel@tonic-gate 				intr_dist_cpuid_add_device_weight(
3370Sstevel@tonic-gate 				    ino_p->ino_cpuid, ih_lst->ih_dip, dweight);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 				/*
340*624Sschwartz 				 * Different cpus may have different clock
3410Sstevel@tonic-gate 				 * speeds. to account for this, whenever an
3420Sstevel@tonic-gate 				 * interrupt is moved to a new CPU, we
3430Sstevel@tonic-gate 				 * convert the accumulated ticks into nsec,
3440Sstevel@tonic-gate 				 * based upon the clock rate of the prior
3450Sstevel@tonic-gate 				 * CPU.
3460Sstevel@tonic-gate 				 *
3470Sstevel@tonic-gate 				 * It is possible that the prior CPU no longer
3480Sstevel@tonic-gate 				 * exists. In this case, fall back to using
3490Sstevel@tonic-gate 				 * this CPU's clock rate.
3500Sstevel@tonic-gate 				 *
3510Sstevel@tonic-gate 				 * Note that the value in ih_ticks has already
3520Sstevel@tonic-gate 				 * been corrected for any power savings mode
3530Sstevel@tonic-gate 				 * which might have been in effect.
3540Sstevel@tonic-gate 				 */
355*624Sschwartz 				px_ib_cpu_ticks_to_ih_nsec(ib_p, ih_lst,
356*624Sschwartz 				    orig_cpuid);
3570Sstevel@tonic-gate 			}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 			/* enable interrupt on new targeted cpu */
3600Sstevel@tonic-gate 			px_ib_intr_dist_en(dip, ino_p->ino_cpuid,
3610Sstevel@tonic-gate 			    ino_p->ino_ino, B_TRUE);
3620Sstevel@tonic-gate 		}
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate /*
3680Sstevel@tonic-gate  * Reset interrupts to IDLE.  This function is called during
3690Sstevel@tonic-gate  * panic handling after redistributing interrupts; it's needed to
3700Sstevel@tonic-gate  * support dumping to network devices after 'sync' from OBP.
3710Sstevel@tonic-gate  *
3720Sstevel@tonic-gate  * N.B.  This routine runs in a context where all other threads
3730Sstevel@tonic-gate  * are permanently suspended.
3740Sstevel@tonic-gate  */
3750Sstevel@tonic-gate static uint_t
3760Sstevel@tonic-gate px_ib_intr_reset(void *arg)
3770Sstevel@tonic-gate {
3780Sstevel@tonic-gate 	px_ib_t		*ib_p = (px_ib_t *)arg;
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	DBG(DBG_IB, ib_p->ib_px_p->px_dip, "px_ib_intr_reset\n");
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if (px_lib_intr_reset(ib_p->ib_px_p->px_dip) != DDI_SUCCESS)
3830Sstevel@tonic-gate 		return (BF_FATAL);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	return (BF_NONE);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate /*
3890Sstevel@tonic-gate  * Locate ino_info structure on ib_p->ib_ino_lst according to ino#
3900Sstevel@tonic-gate  * returns NULL if not found.
3910Sstevel@tonic-gate  */
3920Sstevel@tonic-gate px_ib_ino_info_t *
3930Sstevel@tonic-gate px_ib_locate_ino(px_ib_t *ib_p, devino_t ino_num)
3940Sstevel@tonic-gate {
3950Sstevel@tonic-gate 	px_ib_ino_info_t	*ino_p = ib_p->ib_ino_lst;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	for (; ino_p && ino_p->ino_ino != ino_num; ino_p = ino_p->ino_next);
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	return (ino_p);
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate px_ib_ino_info_t *
4050Sstevel@tonic-gate px_ib_new_ino(px_ib_t *ib_p, devino_t ino_num, px_ih_t *ih_p)
4060Sstevel@tonic-gate {
4070Sstevel@tonic-gate 	px_ib_ino_info_t	*ino_p = kmem_alloc(sizeof (px_ib_ino_info_t),
4080Sstevel@tonic-gate 	    KM_SLEEP);
4090Sstevel@tonic-gate 	sysino_t	sysino;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	ino_p->ino_ino = ino_num;
4120Sstevel@tonic-gate 	ino_p->ino_ib_p = ib_p;
4130Sstevel@tonic-gate 	ino_p->ino_unclaimed = 0;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if (px_lib_intr_devino_to_sysino(ib_p->ib_px_p->px_dip, ino_p->ino_ino,
4160Sstevel@tonic-gate 	    &sysino) != DDI_SUCCESS)
4170Sstevel@tonic-gate 		return (NULL);
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	ino_p->ino_sysino = sysino;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	/*
4220Sstevel@tonic-gate 	 * Cannot disable interrupt since we might share slot
4230Sstevel@tonic-gate 	 */
4240Sstevel@tonic-gate 	ih_p->ih_next = ih_p;
4250Sstevel@tonic-gate 	ino_p->ino_ih_head = ih_p;
4260Sstevel@tonic-gate 	ino_p->ino_ih_tail = ih_p;
4270Sstevel@tonic-gate 	ino_p->ino_ih_start = ih_p;
4280Sstevel@tonic-gate 	ino_p->ino_ih_size = 1;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	ino_p->ino_next = ib_p->ib_ino_lst;
4310Sstevel@tonic-gate 	ib_p->ib_ino_lst = ino_p;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	return (ino_p);
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate /*
4370Sstevel@tonic-gate  * The ino_p is retrieved by previous call to px_ib_locate_ino().
4380Sstevel@tonic-gate  */
4390Sstevel@tonic-gate void
4400Sstevel@tonic-gate px_ib_delete_ino(px_ib_t *ib_p, px_ib_ino_info_t *ino_p)
4410Sstevel@tonic-gate {
4420Sstevel@tonic-gate 	px_ib_ino_info_t	*list = ib_p->ib_ino_lst;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	if (list == ino_p)
4470Sstevel@tonic-gate 		ib_p->ib_ino_lst = list->ino_next;
4480Sstevel@tonic-gate 	else {
4490Sstevel@tonic-gate 		for (; list->ino_next != ino_p; list = list->ino_next);
4500Sstevel@tonic-gate 		list->ino_next = ino_p->ino_next;
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate /*
4550Sstevel@tonic-gate  * Free all ino when we are detaching.
4560Sstevel@tonic-gate  */
4570Sstevel@tonic-gate void
4580Sstevel@tonic-gate px_ib_free_ino_all(px_ib_t *ib_p)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	px_ib_ino_info_t	*tmp = ib_p->ib_ino_lst;
4610Sstevel@tonic-gate 	px_ib_ino_info_t	*next = NULL;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	while (tmp) {
4640Sstevel@tonic-gate 		next = tmp->ino_next;
4650Sstevel@tonic-gate 		kmem_free(tmp, sizeof (px_ib_ino_info_t));
4660Sstevel@tonic-gate 		tmp = next;
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate int
4710Sstevel@tonic-gate px_ib_ino_add_intr(px_t *px_p, px_ib_ino_info_t *ino_p, px_ih_t *ih_p)
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate 	px_ib_t		*ib_p = ino_p->ino_ib_p;
4740Sstevel@tonic-gate 	devino_t	ino = ino_p->ino_ino;
4750Sstevel@tonic-gate 	sysino_t	sysino = ino_p->ino_sysino;
4760Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
4770Sstevel@tonic-gate 	cpuid_t		curr_cpu;
4780Sstevel@tonic-gate 	hrtime_t	start_time;
4790Sstevel@tonic-gate 	intr_state_t	intr_state;
4800Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
4830Sstevel@tonic-gate 	ASSERT(ib_p == px_p->px_ib_p);
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	DBG(DBG_IB, dip, "px_ib_ino_add_intr ino=%x\n", ino_p->ino_ino);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	/* Disable the interrupt */
4880Sstevel@tonic-gate 	if ((ret = px_lib_intr_gettarget(dip, sysino,
4890Sstevel@tonic-gate 	    &curr_cpu)) != DDI_SUCCESS) {
4900Sstevel@tonic-gate 		DBG(DBG_IB, dip,
4910Sstevel@tonic-gate 		    "px_ib_ino_add_intr px_intr_gettarget() failed\n");
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 		return (ret);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	PX_INTR_DISABLE(dip, sysino);
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	/* Busy wait on pending interrupt */
4990Sstevel@tonic-gate 	for (start_time = gethrtime(); !panicstr &&
5000Sstevel@tonic-gate 	    ((ret = px_lib_intr_getstate(dip, sysino, &intr_state))
5010Sstevel@tonic-gate 	    == DDI_SUCCESS) && (intr_state == INTR_DELIVERED_STATE); /* */) {
5020Sstevel@tonic-gate 		if (gethrtime() - start_time > px_intrpend_timeout) {
5030Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: px_ib_ino_add_intr: pending "
5040Sstevel@tonic-gate 			    "sysino 0x%x(ino 0x%x) timeout",
5050Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
5060Sstevel@tonic-gate 			    sysino, ino);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 			ret = DDI_FAILURE;
5090Sstevel@tonic-gate 			break;
5100Sstevel@tonic-gate 		}
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	if (ret != DDI_SUCCESS) {
5140Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_ino_add_intr: failed, "
5150Sstevel@tonic-gate 		    "ino 0x%x sysino 0x%x\n", ino, sysino);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 		return (ret);
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	/* Link up px_ispec_t portion of the ppd */
5210Sstevel@tonic-gate 	ih_p->ih_next = ino_p->ino_ih_head;
5220Sstevel@tonic-gate 	ino_p->ino_ih_tail->ih_next = ih_p;
5230Sstevel@tonic-gate 	ino_p->ino_ih_tail = ih_p;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	ino_p->ino_ih_start = ino_p->ino_ih_head;
5260Sstevel@tonic-gate 	ino_p->ino_ih_size++;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	/*
5290Sstevel@tonic-gate 	 * If the interrupt was previously blocked (left in pending state)
5300Sstevel@tonic-gate 	 * because of jabber we need to clear the pending state in case the
5310Sstevel@tonic-gate 	 * jabber has gone away.
5320Sstevel@tonic-gate 	 */
5330Sstevel@tonic-gate 	if (ino_p->ino_unclaimed > px_unclaimed_intr_max) {
5340Sstevel@tonic-gate 		cmn_err(CE_WARN,
5350Sstevel@tonic-gate 		    "%s%d: px_ib_ino_add_intr: ino 0x%x has been unblocked",
5360Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip), ino);
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 		ino_p->ino_unclaimed = 0;
5390Sstevel@tonic-gate 		if ((ret = px_lib_intr_setstate(dip, sysino,
5400Sstevel@tonic-gate 		    INTR_IDLE_STATE)) != DDI_SUCCESS) {
5410Sstevel@tonic-gate 			DBG(DBG_IB, px_p->px_dip,
5420Sstevel@tonic-gate 			    "px_ib_ino_add_intr px_intr_setstate failed\n");
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 			return (ret);
5450Sstevel@tonic-gate 		}
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	/* Re-enable interrupt */
5490Sstevel@tonic-gate 	PX_INTR_ENABLE(dip, sysino, curr_cpu);
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	return (ret);
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate /*
5550Sstevel@tonic-gate  * Removes px_ispec_t from the ino's link list.
5560Sstevel@tonic-gate  * uses hardware mutex to lock out interrupt threads.
5570Sstevel@tonic-gate  * Side effects: interrupt belongs to that ino is turned off on return.
5580Sstevel@tonic-gate  * if we are sharing PX slot with other inos, the caller needs
5590Sstevel@tonic-gate  * to turn it back on.
5600Sstevel@tonic-gate  */
5610Sstevel@tonic-gate int
5620Sstevel@tonic-gate px_ib_ino_rem_intr(px_t *px_p, px_ib_ino_info_t *ino_p, px_ih_t *ih_p)
5630Sstevel@tonic-gate {
5640Sstevel@tonic-gate 	devino_t	ino = ino_p->ino_ino;
5650Sstevel@tonic-gate 	sysino_t	sysino = ino_p->ino_sysino;
5660Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
5670Sstevel@tonic-gate 	px_ih_t		*ih_lst = ino_p->ino_ih_head;
5680Sstevel@tonic-gate 	hrtime_t	start_time;
5690Sstevel@tonic-gate 	intr_state_t	intr_state;
5700Sstevel@tonic-gate 	int		i, ret = DDI_SUCCESS;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ino_p->ino_ib_p->ib_ino_lst_mutex));
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	DBG(DBG_IB, px_p->px_dip, "px_ib_ino_rem_intr ino=%x\n",
5750Sstevel@tonic-gate 	    ino_p->ino_ino);
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	/* Disable the interrupt */
5780Sstevel@tonic-gate 	PX_INTR_DISABLE(px_p->px_dip, sysino);
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	if (ino_p->ino_ih_size == 1) {
5810Sstevel@tonic-gate 		if (ih_lst != ih_p)
5820Sstevel@tonic-gate 			goto not_found;
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 		/* No need to set head/tail as ino_p will be freed */
5850Sstevel@tonic-gate 		goto reset;
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	/* Busy wait on pending interrupt */
5890Sstevel@tonic-gate 	for (start_time = gethrtime(); !panicstr &&
5900Sstevel@tonic-gate 	    ((ret = px_lib_intr_getstate(dip, sysino, &intr_state))
5910Sstevel@tonic-gate 	    == DDI_SUCCESS) && (intr_state == INTR_DELIVERED_STATE); /* */) {
5920Sstevel@tonic-gate 		if (gethrtime() - start_time > px_intrpend_timeout) {
5930Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: px_ib_ino_rem_intr: pending "
5940Sstevel@tonic-gate 			    "sysino 0x%x(ino 0x%x) timeout",
5950Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
5960Sstevel@tonic-gate 			    sysino, ino);
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 			ret = DDI_FAILURE;
5990Sstevel@tonic-gate 			break;
6000Sstevel@tonic-gate 		}
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	if (ret != DDI_SUCCESS) {
6040Sstevel@tonic-gate 		DBG(DBG_IB, dip, "px_ib_ino_rem_intr: failed, "
6050Sstevel@tonic-gate 		    "ino 0x%x sysino 0x%x\n", ino, sysino);
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 		return (ret);
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	/*
6110Sstevel@tonic-gate 	 * If the interrupt was previously blocked (left in pending state)
6120Sstevel@tonic-gate 	 * because of jabber we need to clear the pending state in case the
6130Sstevel@tonic-gate 	 * jabber has gone away.
6140Sstevel@tonic-gate 	 */
6150Sstevel@tonic-gate 	if (ino_p->ino_unclaimed > px_unclaimed_intr_max) {
6160Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: px_ib_ino_rem_intr: "
6170Sstevel@tonic-gate 		    "ino 0x%x has been unblocked",
6180Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip), ino);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		ino_p->ino_unclaimed = 0;
6210Sstevel@tonic-gate 		if ((ret = px_lib_intr_setstate(dip, sysino,
6220Sstevel@tonic-gate 		    INTR_IDLE_STATE)) != DDI_SUCCESS) {
6230Sstevel@tonic-gate 			DBG(DBG_IB, px_p->px_dip,
6240Sstevel@tonic-gate 			    "px_ib_ino_rem_intr px_intr_setstate failed\n");
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 			return (ret);
6270Sstevel@tonic-gate 		}
6280Sstevel@tonic-gate 	}
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	/* Search the link list for ih_p */
6310Sstevel@tonic-gate 	for (i = 0; (i < ino_p->ino_ih_size) &&
6320Sstevel@tonic-gate 	    (ih_lst->ih_next != ih_p); i++, ih_lst = ih_lst->ih_next);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	if (ih_lst->ih_next != ih_p)
6350Sstevel@tonic-gate 		goto not_found;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	/* Remove ih_p from the link list and maintain the head/tail */
6380Sstevel@tonic-gate 	ih_lst->ih_next = ih_p->ih_next;
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	if (ino_p->ino_ih_head == ih_p)
6410Sstevel@tonic-gate 		ino_p->ino_ih_head = ih_p->ih_next;
6420Sstevel@tonic-gate 	if (ino_p->ino_ih_tail == ih_p)
6430Sstevel@tonic-gate 		ino_p->ino_ih_tail = ih_lst;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	ino_p->ino_ih_start = ino_p->ino_ih_head;
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate reset:
6480Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
6490Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
6500Sstevel@tonic-gate 	if (ih_p->ih_ksp != NULL)
6510Sstevel@tonic-gate 		kstat_delete(ih_p->ih_ksp);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (px_ih_t));
6540Sstevel@tonic-gate 	ino_p->ino_ih_size--;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	return (ret);
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate not_found:
6590Sstevel@tonic-gate 	DBG(DBG_R_INTX, ino_p->ino_ib_p->ib_px_p->px_dip,
6600Sstevel@tonic-gate 		"ino_p=%x does not have ih_p=%x\n", ino_p, ih_p);
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	return (DDI_FAILURE);
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate px_ih_t *
6660Sstevel@tonic-gate px_ib_ino_locate_intr(px_ib_ino_info_t *ino_p, dev_info_t *rdip,
6670Sstevel@tonic-gate     uint32_t inum, msiq_rec_type_t rec_type, msgcode_t msg_code)
6680Sstevel@tonic-gate {
6690Sstevel@tonic-gate 	px_ih_t	*ih_lst = ino_p->ino_ih_head;
6700Sstevel@tonic-gate 	int	i;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	for (i = 0; i < ino_p->ino_ih_size; i++, ih_lst = ih_lst->ih_next) {
6730Sstevel@tonic-gate 		if ((ih_lst->ih_dip == rdip) && (ih_lst->ih_inum == inum) &&
6740Sstevel@tonic-gate 		    (ih_lst->ih_rec_type == rec_type) &&
6750Sstevel@tonic-gate 		    (ih_lst->ih_msg_code == msg_code))
6760Sstevel@tonic-gate 			return (ih_lst);
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	return ((px_ih_t *)NULL);
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate px_ih_t *
6830Sstevel@tonic-gate px_ib_alloc_ih(dev_info_t *rdip, uint32_t inum,
6840Sstevel@tonic-gate     uint_t (*int_handler)(caddr_t int_handler_arg1, caddr_t int_handler_arg2),
6850Sstevel@tonic-gate     caddr_t int_handler_arg1, caddr_t int_handler_arg2,
6860Sstevel@tonic-gate     msiq_rec_type_t rec_type, msgcode_t msg_code)
6870Sstevel@tonic-gate {
6880Sstevel@tonic-gate 	px_ih_t	*ih_p;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	ih_p = kmem_alloc(sizeof (px_ih_t), KM_SLEEP);
6910Sstevel@tonic-gate 	ih_p->ih_dip = rdip;
6920Sstevel@tonic-gate 	ih_p->ih_inum = inum;
6930Sstevel@tonic-gate 	ih_p->ih_intr_state = PX_INTR_STATE_DISABLE;
6940Sstevel@tonic-gate 	ih_p->ih_handler = int_handler;
6950Sstevel@tonic-gate 	ih_p->ih_handler_arg1 = int_handler_arg1;
6960Sstevel@tonic-gate 	ih_p->ih_handler_arg2 = int_handler_arg2;
6970Sstevel@tonic-gate 	ih_p->ih_config_handle = NULL;
6980Sstevel@tonic-gate 	ih_p->ih_rec_type = rec_type;
6990Sstevel@tonic-gate 	ih_p->ih_msg_code = msg_code;
7000Sstevel@tonic-gate 	ih_p->ih_nsec = 0;
7010Sstevel@tonic-gate 	ih_p->ih_ticks = 0;
7020Sstevel@tonic-gate 	ih_p->ih_ksp = NULL;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	return (ih_p);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate /*
7080Sstevel@tonic-gate  * Only used for fixed or legacy interrupts.
7090Sstevel@tonic-gate  */
7100Sstevel@tonic-gate int
7110Sstevel@tonic-gate px_ib_update_intr_state(px_t *px_p, dev_info_t *rdip,
7120Sstevel@tonic-gate     uint_t inum, devino_t ino, uint_t new_intr_state)
7130Sstevel@tonic-gate {
7140Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
7150Sstevel@tonic-gate 	px_ib_ino_info_t *ino_p;
7160Sstevel@tonic-gate 	px_ih_t		*ih_p;
7170Sstevel@tonic-gate 	int		ret = DDI_FAILURE;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	DBG(DBG_IB, px_p->px_dip, "ib_update_intr_state: %s%d "
7200Sstevel@tonic-gate 	    "inum %x devino %x state %x\n", ddi_driver_name(rdip),
7210Sstevel@tonic-gate 	    ddi_get_instance(rdip), inum, ino, new_intr_state);
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	if (ino_p = px_ib_locate_ino(ib_p, ino)) {
7260Sstevel@tonic-gate 		if (ih_p = px_ib_ino_locate_intr(ino_p, rdip, inum, 0, 0)) {
7270Sstevel@tonic-gate 			ih_p->ih_intr_state = new_intr_state;
7280Sstevel@tonic-gate 			ret = DDI_SUCCESS;
7290Sstevel@tonic-gate 		}
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
7330Sstevel@tonic-gate 	return (ret);
7340Sstevel@tonic-gate }
735*624Sschwartz 
736*624Sschwartz 
737*624Sschwartz static void
738*624Sschwartz px_fill_in_intr_devs(pcitool_intr_dev_t *dev, char *driver_name,
739*624Sschwartz     char *path_name, int instance)
740*624Sschwartz {
741*624Sschwartz 	(void) strncpy(dev->driver_name, driver_name, MAXMODCONFNAME-1);
742*624Sschwartz 	dev->driver_name[MAXMODCONFNAME] = '\0';
743*624Sschwartz 	(void) strncpy(dev->path, path_name, MAXPATHLEN-1);
744*624Sschwartz 	dev->dev_inst = instance;
745*624Sschwartz }
746*624Sschwartz 
747*624Sschwartz 
748*624Sschwartz /*
749*624Sschwartz  * Return the dips or number of dips associated with a given interrupt block.
750*624Sschwartz  * Size of dips array arg is passed in as dips_ret arg.
751*624Sschwartz  * Number of dips returned is returned in dips_ret arg.
752*624Sschwartz  * Array of dips gets returned in the dips argument.
753*624Sschwartz  * Function returns number of dips existing for the given interrupt block.
754*624Sschwartz  *
755*624Sschwartz  * Note: this function assumes an enabled/valid INO, which is why it returns
756*624Sschwartz  * the px node and (Internal) when it finds no other devices (and *devs_ret > 0)
757*624Sschwartz  */
758*624Sschwartz uint8_t
759*624Sschwartz pxtool_ib_get_ino_devs(
760*624Sschwartz     px_t *px_p, uint32_t ino, uint8_t *devs_ret, pcitool_intr_dev_t *devs)
761*624Sschwartz {
762*624Sschwartz 	px_ib_t *ib_p = px_p->px_ib_p;
763*624Sschwartz 	px_ib_ino_info_t *ino_p;
764*624Sschwartz 	px_ih_t *ih_p;
765*624Sschwartz 	uint32_t num_devs = 0;
766*624Sschwartz 	char pathname[MAXPATHLEN];
767*624Sschwartz 	int i;
768*624Sschwartz 
769*624Sschwartz 	mutex_enter(&ib_p->ib_ino_lst_mutex);
770*624Sschwartz 	ino_p = px_ib_locate_ino(ib_p, ino);
771*624Sschwartz 	if (ino_p != NULL) {
772*624Sschwartz 		num_devs = ino_p->ino_ih_size;
773*624Sschwartz 		for (i = 0, ih_p = ino_p->ino_ih_head;
774*624Sschwartz 		    ((i < ino_p->ino_ih_size) && (i < *devs_ret));
775*624Sschwartz 		    i++, ih_p = ih_p->ih_next) {
776*624Sschwartz 			(void) ddi_pathname(ih_p->ih_dip, pathname);
777*624Sschwartz 			px_fill_in_intr_devs(&devs[i],
778*624Sschwartz 			    (char *)ddi_driver_name(ih_p->ih_dip),  pathname,
779*624Sschwartz 			    ddi_get_instance(ih_p->ih_dip));
780*624Sschwartz 		}
781*624Sschwartz 		*devs_ret = i;
782*624Sschwartz 
783*624Sschwartz 	} else if (*devs_ret > 0) {
784*624Sschwartz 		(void) ddi_pathname(px_p->px_dip, pathname);
785*624Sschwartz 		strcat(pathname, " (Internal)");
786*624Sschwartz 		px_fill_in_intr_devs(&devs[0],
787*624Sschwartz 		    (char *)ddi_driver_name(px_p->px_dip),  pathname,
788*624Sschwartz 		    ddi_get_instance(px_p->px_dip));
789*624Sschwartz 		num_devs = *devs_ret = 1;
790*624Sschwartz 	}
791*624Sschwartz 
792*624Sschwartz 	mutex_exit(&ib_p->ib_ino_lst_mutex);
793*624Sschwartz 
794*624Sschwartz 	return (num_devs);
795*624Sschwartz }
796*624Sschwartz 
797*624Sschwartz 
798*624Sschwartz void px_ib_log_new_cpu(px_ib_t *ib_p, uint32_t old_cpu_id, uint32_t new_cpu_id,
799*624Sschwartz     uint32_t ino)
800*624Sschwartz {
801*624Sschwartz 	px_ib_ino_info_t *ino_p;
802*624Sschwartz 
803*624Sschwartz 	mutex_enter(&ib_p->ib_ino_lst_mutex);
804*624Sschwartz 
805*624Sschwartz 	/* Log in OS data structures the new CPU. */
806*624Sschwartz 	ino_p = px_ib_locate_ino(ib_p, ino);
807*624Sschwartz 	if (ino_p != NULL) {
808*624Sschwartz 
809*624Sschwartz 		/* Log in OS data structures the new CPU. */
810*624Sschwartz 		ino_p->ino_cpuid = new_cpu_id;
811*624Sschwartz 
812*624Sschwartz 		/* Account for any residual time to be logged for old cpu. */
813*624Sschwartz 		px_ib_cpu_ticks_to_ih_nsec(ib_p, ino_p->ino_ih_head,
814*624Sschwartz 		    old_cpu_id);
815*624Sschwartz 	}
816*624Sschwartz 
817*624Sschwartz 	mutex_exit(&ib_p->ib_ino_lst_mutex);
818*624Sschwartz }
819