xref: /illumos-gate/usr/src/uts/sun4/io/px/px_intr.c (revision 07f14c08)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5102cb92eSjohnny  * Common Development and Distribution License (the "License").
6102cb92eSjohnny  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22102cb92eSjohnny  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * PX nexus interrupt handling:
307c478bd9Sstevel@tonic-gate  *	PX device interrupt handler wrapper
317c478bd9Sstevel@tonic-gate  *	PIL lookup routine
327c478bd9Sstevel@tonic-gate  *	PX device interrupt related initchild code
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
377c478bd9Sstevel@tonic-gate #include <sys/async.h>
387c478bd9Sstevel@tonic-gate #include <sys/spl.h>
397c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
40f8d2de6bSjchu #include <sys/fm/protocol.h>
41f8d2de6bSjchu #include <sys/fm/util.h>
427c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>	/* e_ddi_nodeid_to_dip() */
437c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
447c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
457c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
467c478bd9Sstevel@tonic-gate #include "px_obj.h"
47f8d2de6bSjchu #include <sys/ontrap.h>
48f8d2de6bSjchu #include <sys/membar.h>
496d44af1bSesolom #include <sys/clock.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * interrupt jabber:
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * When an interrupt line is jabbering, every time the state machine for the
557c478bd9Sstevel@tonic-gate  * associated ino is idled, a new mondo will be sent and the ino will go into
567c478bd9Sstevel@tonic-gate  * the pending state again. The mondo will cause a new call to
577c478bd9Sstevel@tonic-gate  * px_intr_wrapper() which normally idles the ino's state machine which would
587c478bd9Sstevel@tonic-gate  * precipitate another trip round the loop.
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  * The loop can be broken by preventing the ino's state machine from being
617c478bd9Sstevel@tonic-gate  * idled when an interrupt line is jabbering. See the comment at the
627c478bd9Sstevel@tonic-gate  * beginning of px_intr_wrapper() explaining how the 'interrupt jabber
637c478bd9Sstevel@tonic-gate  * protection' code does this.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * If the unclaimed interrupt count has reached the limit set by
707c478bd9Sstevel@tonic-gate  * pci_unclaimed_intr_max within the time limit, then all interrupts
717c478bd9Sstevel@tonic-gate  * on this ino is blocked by not idling the interrupt state machine.
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate static int
747c478bd9Sstevel@tonic-gate px_spurintr(px_ib_ino_info_t *ino_p)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	px_ih_t	*ih_p = ino_p->ino_ih_start;
777c478bd9Sstevel@tonic-gate 	px_t	*px_p = ino_p->ino_ib_p->ib_px_p;
787c478bd9Sstevel@tonic-gate 	char	*err_fmt_str;
797c478bd9Sstevel@tonic-gate 	int	i;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if (ino_p->ino_unclaimed > px_unclaimed_intr_max)
827c478bd9Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (!ino_p->ino_unclaimed)
857c478bd9Sstevel@tonic-gate 		ino_p->ino_spurintr_begin = ddi_get_lbolt();
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	ino_p->ino_unclaimed++;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (ino_p->ino_unclaimed <= px_unclaimed_intr_max)
907c478bd9Sstevel@tonic-gate 		goto clear;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if (drv_hztousec(ddi_get_lbolt() - ino_p->ino_spurintr_begin)
937c478bd9Sstevel@tonic-gate 	    > px_spurintr_duration) {
947c478bd9Sstevel@tonic-gate 		ino_p->ino_unclaimed = 0;
957c478bd9Sstevel@tonic-gate 		goto clear;
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 	err_fmt_str = "%s%d: ino 0x%x blocked";
987c478bd9Sstevel@tonic-gate 	goto warn;
997c478bd9Sstevel@tonic-gate clear:
1007c478bd9Sstevel@tonic-gate 	/* Clear the pending state */
1017c478bd9Sstevel@tonic-gate 	if (px_lib_intr_setstate(px_p->px_dip, ino_p->ino_sysino,
1027c478bd9Sstevel@tonic-gate 	    INTR_IDLE_STATE) != DDI_SUCCESS)
1037c478bd9Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	err_fmt_str = "!%s%d: spurious interrupt from ino 0x%x";
1067c478bd9Sstevel@tonic-gate warn:
1077c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, err_fmt_str, NAMEINST(px_p->px_dip), ino_p->ino_ino);
1087c478bd9Sstevel@tonic-gate 	for (i = 0; i < ino_p->ino_ih_size; i++, ih_p = ih_p->ih_next)
1097c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "!%s-%d#%x ", NAMEINST(ih_p->ih_dip),
1107c478bd9Sstevel@tonic-gate 		    ih_p->ih_inum);
1117c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "!\n");
1127c478bd9Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate extern uint64_t intr_get_time(void);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*
118a195726fSgovinda  * px_intx_intr (INTx or legacy interrupt handler)
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  * This routine is used as wrapper around interrupt handlers installed by child
1217c478bd9Sstevel@tonic-gate  * device drivers.  This routine invokes the driver interrupt handlers and
1227c478bd9Sstevel@tonic-gate  * examines the return codes.
1237c478bd9Sstevel@tonic-gate  *
1247c478bd9Sstevel@tonic-gate  * There is a count of unclaimed interrupts kept on a per-ino basis. If at
1257c478bd9Sstevel@tonic-gate  * least one handler claims the interrupt then the counter is halved and the
1267c478bd9Sstevel@tonic-gate  * interrupt state machine is idled. If no handler claims the interrupt then
1277c478bd9Sstevel@tonic-gate  * the counter is incremented by one and the state machine is idled.
1287c478bd9Sstevel@tonic-gate  * If the count ever reaches the limit value set by pci_unclaimed_intr_max
1297c478bd9Sstevel@tonic-gate  * then the interrupt state machine is not idled thus preventing any further
1307c478bd9Sstevel@tonic-gate  * interrupts on that ino. The state machine will only be idled again if a
1317c478bd9Sstevel@tonic-gate  * handler is subsequently added or removed.
1327c478bd9Sstevel@tonic-gate  *
1337c478bd9Sstevel@tonic-gate  * return value: DDI_INTR_CLAIMED if any handlers claimed the interrupt,
1347c478bd9Sstevel@tonic-gate  * DDI_INTR_UNCLAIMED otherwise.
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate uint_t
1377c478bd9Sstevel@tonic-gate px_intx_intr(caddr_t arg)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	px_ib_ino_info_t *ino_p = (px_ib_ino_info_t *)arg;
1407c478bd9Sstevel@tonic-gate 	px_t		*px_p = ino_p->ino_ib_p->ib_px_p;
1417c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p = ino_p->ino_ih_start;
1427c478bd9Sstevel@tonic-gate 	uint_t		result = 0, r;
1437c478bd9Sstevel@tonic-gate 	int		i;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	DBG(DBG_INTX_INTR, px_p->px_dip, "px_intx_intr:"
1467c478bd9Sstevel@tonic-gate 	    "ino=%x sysino=%llx pil=%x ih_size=%x ih_lst=%x\n",
1477c478bd9Sstevel@tonic-gate 	    ino_p->ino_ino, ino_p->ino_sysino, ino_p->ino_pil,
1487c478bd9Sstevel@tonic-gate 	    ino_p->ino_ih_size, ino_p->ino_ih_head);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	for (i = 0; i < ino_p->ino_ih_size; i++, ih_p = ih_p->ih_next) {
1517c478bd9Sstevel@tonic-gate 		dev_info_t *dip = ih_p->ih_dip;
1527c478bd9Sstevel@tonic-gate 		uint_t (*handler)() = ih_p->ih_handler;
1537c478bd9Sstevel@tonic-gate 		caddr_t arg1 = ih_p->ih_handler_arg1;
1547c478bd9Sstevel@tonic-gate 		caddr_t arg2 = ih_p->ih_handler_arg2;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		if (ih_p->ih_intr_state == PX_INTR_STATE_DISABLE) {
1577c478bd9Sstevel@tonic-gate 			DBG(DBG_INTX_INTR, px_p->px_dip,
1587c478bd9Sstevel@tonic-gate 			    "px_intx_intr: %s%d interrupt %d is disabled\n",
1597c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
1607c478bd9Sstevel@tonic-gate 			    ino_p->ino_ino);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 			continue;
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		DBG(DBG_INTX_INTR, px_p->px_dip, "px_intx_intr:"
1667c478bd9Sstevel@tonic-gate 		    "ino=%x handler=%p arg1 =%p arg2 = %p\n",
1677c478bd9Sstevel@tonic-gate 		    ino_p->ino_ino, handler, arg1, arg2);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
1707c478bd9Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, caddr_t, arg2);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		r = (*handler)(arg1, arg2);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		/*
1757c478bd9Sstevel@tonic-gate 		 * Account for time used by this interrupt. Protect against
1767c478bd9Sstevel@tonic-gate 		 * conflicting writes to ih_ticks from ib_intr_dist_all() by
1777c478bd9Sstevel@tonic-gate 		 * using atomic ops.
1787c478bd9Sstevel@tonic-gate 		 */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		if (ino_p->ino_pil <= LOCK_LEVEL)
1817c478bd9Sstevel@tonic-gate 			atomic_add_64(&ih_p->ih_ticks, intr_get_time());
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
1847c478bd9Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, int, r);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		result += r;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		if (px_check_all_handlers)
1897c478bd9Sstevel@tonic-gate 			continue;
1907c478bd9Sstevel@tonic-gate 		if (result)
1917c478bd9Sstevel@tonic-gate 			break;
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	if (!result && px_unclaimed_intr_block)
1957c478bd9Sstevel@tonic-gate 		return (px_spurintr(ino_p));
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	ino_p->ino_unclaimed = 0;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/* Clear the pending state */
2007c478bd9Sstevel@tonic-gate 	if (px_lib_intr_setstate(ino_p->ino_ib_p->ib_px_p->px_dip,
2017c478bd9Sstevel@tonic-gate 	    ino_p->ino_sysino, INTR_IDLE_STATE) != DDI_SUCCESS)
2027c478bd9Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
208a195726fSgovinda  * px_msiq_intr (MSI/X or PCIe MSG interrupt handler)
2097c478bd9Sstevel@tonic-gate  *
2107c478bd9Sstevel@tonic-gate  * This routine is used as wrapper around interrupt handlers installed by child
2117c478bd9Sstevel@tonic-gate  * device drivers.  This routine invokes the driver interrupt handlers and
2127c478bd9Sstevel@tonic-gate  * examines the return codes.
2137c478bd9Sstevel@tonic-gate  *
2147c478bd9Sstevel@tonic-gate  * There is a count of unclaimed interrupts kept on a per-ino basis. If at
2157c478bd9Sstevel@tonic-gate  * least one handler claims the interrupt then the counter is halved and the
2167c478bd9Sstevel@tonic-gate  * interrupt state machine is idled. If no handler claims the interrupt then
2177c478bd9Sstevel@tonic-gate  * the counter is incremented by one and the state machine is idled.
2187c478bd9Sstevel@tonic-gate  * If the count ever reaches the limit value set by pci_unclaimed_intr_max
2197c478bd9Sstevel@tonic-gate  * then the interrupt state machine is not idled thus preventing any further
2207c478bd9Sstevel@tonic-gate  * interrupts on that ino. The state machine will only be idled again if a
2217c478bd9Sstevel@tonic-gate  * handler is subsequently added or removed.
2227c478bd9Sstevel@tonic-gate  *
2237c478bd9Sstevel@tonic-gate  * return value: DDI_INTR_CLAIMED if any handlers claimed the interrupt,
2247c478bd9Sstevel@tonic-gate  * DDI_INTR_UNCLAIMED otherwise.
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate uint_t
2277c478bd9Sstevel@tonic-gate px_msiq_intr(caddr_t arg)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	px_ib_ino_info_t	*ino_p = (px_ib_ino_info_t *)arg;
2307c478bd9Sstevel@tonic-gate 	px_t		*px_p = ino_p->ino_ib_p->ib_px_p;
2317c478bd9Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
2327c478bd9Sstevel@tonic-gate 	px_msiq_t	*msiq_p = ino_p->ino_msiq_p;
2337c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
2347c478bd9Sstevel@tonic-gate 	msiq_rec_t	msiq_rec, *msiq_rec_p = &msiq_rec;
2357c478bd9Sstevel@tonic-gate 	msiqhead_t	curr_msiq_rec_cnt, new_msiq_rec_cnt;
2367c478bd9Sstevel@tonic-gate 	msgcode_t	msg_code;
2377c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
238*07f14c08Sgovinda 	int		i, ret;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: msiq_id =%x ino=%x pil=%x "
2417c478bd9Sstevel@tonic-gate 	    "ih_size=%x ih_lst=%x\n", msiq_p->msiq_id, ino_p->ino_ino,
2427c478bd9Sstevel@tonic-gate 	    ino_p->ino_pil, ino_p->ino_ih_size, ino_p->ino_ih_head);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/* Read current MSIQ head index */
2457c478bd9Sstevel@tonic-gate 	px_lib_msiq_gethead(dip, msiq_p->msiq_id, &curr_msiq_rec_cnt);
2467c478bd9Sstevel@tonic-gate 	msiq_p->msiq_curr = (uint64_t)((caddr_t)msiq_p->msiq_base +
2477c478bd9Sstevel@tonic-gate 	    curr_msiq_rec_cnt * sizeof (msiq_rec_t));
2487c478bd9Sstevel@tonic-gate 	new_msiq_rec_cnt = curr_msiq_rec_cnt;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/* Read next MSIQ record */
2517c478bd9Sstevel@tonic-gate 	px_lib_get_msiq_rec(dip, msiq_p, msiq_rec_p);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/*
2543ee8f295Smg 	 * Process current MSIQ record as long as record type
2557c478bd9Sstevel@tonic-gate 	 * field is non-zero.
2567c478bd9Sstevel@tonic-gate 	 */
2573ee8f295Smg 	while (msiq_rec_p->msiq_rec_type) {
2587c478bd9Sstevel@tonic-gate 		DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: MSIQ RECORD, "
2597c478bd9Sstevel@tonic-gate 		    "msiq_rec_type 0x%llx msiq_rec_rid 0x%llx\n",
2607c478bd9Sstevel@tonic-gate 		    msiq_rec_p->msiq_rec_type, msiq_rec_p->msiq_rec_rid);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		/* Get the pointer next EQ record */
2637c478bd9Sstevel@tonic-gate 		msiq_p->msiq_curr = (uint64_t)
2647c478bd9Sstevel@tonic-gate 		    ((caddr_t)msiq_p->msiq_curr + sizeof (msiq_rec_t));
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		/* Check for overflow condition */
2677c478bd9Sstevel@tonic-gate 		if (msiq_p->msiq_curr >= (uint64_t)((caddr_t)msiq_p->msiq_base +
2687c478bd9Sstevel@tonic-gate 		    msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t)))
2697c478bd9Sstevel@tonic-gate 			msiq_p->msiq_curr = msiq_p->msiq_base;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		/* Check MSIQ record type */
2727c478bd9Sstevel@tonic-gate 		switch (msiq_rec_p->msiq_rec_type) {
2737c478bd9Sstevel@tonic-gate 		case MSG_REC:
2747c478bd9Sstevel@tonic-gate 			msg_code = msiq_rec_p->msiq_rec_data.msg.msg_code;
2757c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: PCIE MSG "
2767c478bd9Sstevel@tonic-gate 			    "record, msg type 0x%x\n", msg_code);
2777c478bd9Sstevel@tonic-gate 			break;
2787c478bd9Sstevel@tonic-gate 		case MSI32_REC:
2797c478bd9Sstevel@tonic-gate 		case MSI64_REC:
2807c478bd9Sstevel@tonic-gate 			msg_code = msiq_rec_p->msiq_rec_data.msi.msi_data;
2817c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: MSI record, "
2827c478bd9Sstevel@tonic-gate 			    "msi 0x%x\n", msg_code);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 			/* Clear MSI state */
2857c478bd9Sstevel@tonic-gate 			px_lib_msi_setstate(dip, (msinum_t)msg_code,
2867c478bd9Sstevel@tonic-gate 			    PCI_MSI_STATE_IDLE);
2877c478bd9Sstevel@tonic-gate 			break;
2887c478bd9Sstevel@tonic-gate 		default:
2897c478bd9Sstevel@tonic-gate 			msg_code = 0;
2907c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: px_msiq_intr: 0x%x MSIQ "
2917c478bd9Sstevel@tonic-gate 			    "record type is not supported",
2927c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
2937c478bd9Sstevel@tonic-gate 			    msiq_rec_p->msiq_rec_type);
2947c478bd9Sstevel@tonic-gate 			goto next_rec;
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		/*
2987c478bd9Sstevel@tonic-gate 		 * Scan through px_ih_t linked list, searching for the
2997c478bd9Sstevel@tonic-gate 		 * right px_ih_t, matching MSIQ record data.
3007c478bd9Sstevel@tonic-gate 		 */
301*07f14c08Sgovinda 		for (i = 0, ih_p = ino_p->ino_ih_start;
302*07f14c08Sgovinda 		    ih_p && (i < ino_p->ino_ih_size) &&
303*07f14c08Sgovinda 		    ((ih_p->ih_msg_code != msg_code) ||
304*07f14c08Sgovinda 		    (ih_p->ih_rec_type != msiq_rec_p->msiq_rec_type));
305*07f14c08Sgovinda 		    ih_p = ih_p->ih_next, i++);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		if ((ih_p->ih_msg_code == msg_code) &&
3087c478bd9Sstevel@tonic-gate 		    (ih_p->ih_rec_type == msiq_rec_p->msiq_rec_type)) {
3097c478bd9Sstevel@tonic-gate 			dev_info_t *dip = ih_p->ih_dip;
3107c478bd9Sstevel@tonic-gate 			uint_t (*handler)() = ih_p->ih_handler;
3117c478bd9Sstevel@tonic-gate 			caddr_t arg1 = ih_p->ih_handler_arg1;
3127c478bd9Sstevel@tonic-gate 			caddr_t arg2 = ih_p->ih_handler_arg2;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: ino=%x data=%x "
3157c478bd9Sstevel@tonic-gate 			    "handler=%p arg1 =%p arg2=%p\n", ino_p->ino_ino,
3167c478bd9Sstevel@tonic-gate 			    msg_code, handler, arg1, arg2);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 			DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
3197c478bd9Sstevel@tonic-gate 			    void *, handler, caddr_t, arg1, caddr_t, arg2);
3207c478bd9Sstevel@tonic-gate 
321f8d2de6bSjchu 			/*
322f8d2de6bSjchu 			 * Special case for PCIE Error Messages.
323f8d2de6bSjchu 			 * The current frame work doesn't fit PCIE Err Msgs
324f8d2de6bSjchu 			 * This should be fixed when PCIE MESSAGES as a whole
325f8d2de6bSjchu 			 * is architected correctly.
326f8d2de6bSjchu 			 */
327f8d2de6bSjchu 			if ((msg_code == PCIE_MSG_CODE_ERR_COR) ||
328f8d2de6bSjchu 			    (msg_code == PCIE_MSG_CODE_ERR_NONFATAL) ||
329f8d2de6bSjchu 			    (msg_code == PCIE_MSG_CODE_ERR_FATAL)) {
330f8d2de6bSjchu 				ret = px_err_fabric_intr(px_p, msg_code,
331f8d2de6bSjchu 				    msiq_rec_p->msiq_rec_rid);
332f8d2de6bSjchu 			} else
333f8d2de6bSjchu 				ret = (*handler)(arg1, arg2);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 			/*
3367c478bd9Sstevel@tonic-gate 			 * Account for time used by this interrupt. Protect
3377c478bd9Sstevel@tonic-gate 			 * against conflicting writes to ih_ticks from
3387c478bd9Sstevel@tonic-gate 			 * ib_intr_dist_all() by using atomic ops.
3397c478bd9Sstevel@tonic-gate 			 */
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 			if (ino_p->ino_pil <= LOCK_LEVEL)
3427c478bd9Sstevel@tonic-gate 				atomic_add_64(&ih_p->ih_ticks, intr_get_time());
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 			DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
3457c478bd9Sstevel@tonic-gate 			    void *, handler, caddr_t, arg1, int, ret);
3467c478bd9Sstevel@tonic-gate 		} else {
3477c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr:"
3487c478bd9Sstevel@tonic-gate 			    "Not found matching MSIQ record\n");
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 			/* px_spurintr(ino_p); */
3517c478bd9Sstevel@tonic-gate 			ino_p->ino_unclaimed++;
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate next_rec:
3557c478bd9Sstevel@tonic-gate 		new_msiq_rec_cnt++;
3567c478bd9Sstevel@tonic-gate 
3573ee8f295Smg 		/* Zero out msiq_rec_type field */
3583ee8f295Smg 		msiq_rec_p->msiq_rec_type = 0;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 		/* Read next MSIQ record */
3617c478bd9Sstevel@tonic-gate 		px_lib_get_msiq_rec(dip, msiq_p, msiq_rec_p);
3627c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: No of MSIQ recs processed %x\n",
3657c478bd9Sstevel@tonic-gate 	    (new_msiq_rec_cnt - curr_msiq_rec_cnt));
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/*  Update MSIQ head index with no of MSIQ records processed */
3687c478bd9Sstevel@tonic-gate 	if (new_msiq_rec_cnt > curr_msiq_rec_cnt)  {
3697c478bd9Sstevel@tonic-gate 		if (new_msiq_rec_cnt >= msiq_state_p->msiq_rec_cnt)
3707c478bd9Sstevel@tonic-gate 			new_msiq_rec_cnt -= msiq_state_p->msiq_rec_cnt;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		px_lib_msiq_sethead(dip, msiq_p->msiq_id, new_msiq_rec_cnt);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	/* Clear the pending state */
3767c478bd9Sstevel@tonic-gate 	if (px_lib_intr_setstate(dip, ino_p->ino_sysino,
3777c478bd9Sstevel@tonic-gate 	    INTR_IDLE_STATE) != DDI_SUCCESS)
3787c478bd9Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate dev_info_t *
3847c478bd9Sstevel@tonic-gate px_get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip = rdip;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
3897c478bd9Sstevel@tonic-gate 		;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	return (cdip);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate /* Default class to pil value mapping */
3957c478bd9Sstevel@tonic-gate px_class_val_t px_default_pil [] = {
3967c478bd9Sstevel@tonic-gate 	{0x000000, 0xff0000, 0x1},	/* Class code for pre-2.0 devices */
3977c478bd9Sstevel@tonic-gate 	{0x010000, 0xff0000, 0x4},	/* Mass Storage Controller */
3987c478bd9Sstevel@tonic-gate 	{0x020000, 0xff0000, 0x6},	/* Network Controller */
3997c478bd9Sstevel@tonic-gate 	{0x030000, 0xff0000, 0x9},	/* Display Controller */
4007c478bd9Sstevel@tonic-gate 	{0x040000, 0xff0000, 0x9},	/* Multimedia Controller */
40144bb982bSgovinda 	{0x050000, 0xff0000, 0x9},	/* Memory Controller */
40244bb982bSgovinda 	{0x060000, 0xff0000, 0x9},	/* Bridge Controller */
4037c478bd9Sstevel@tonic-gate 	{0x0c0000, 0xffff00, 0x9},	/* Serial Bus, FireWire (IEEE 1394) */
4047c478bd9Sstevel@tonic-gate 	{0x0c0100, 0xffff00, 0x4},	/* Serial Bus, ACCESS.bus */
4057c478bd9Sstevel@tonic-gate 	{0x0c0200, 0xffff00, 0x4},	/* Serial Bus, SSA */
4067c478bd9Sstevel@tonic-gate 	{0x0c0300, 0xffff00, 0x9},	/* Serial Bus Universal Serial Bus */
4077c478bd9Sstevel@tonic-gate 	{0x0c0400, 0xffff00, 0x6},	/* Serial Bus, Fibre Channel */
4087c478bd9Sstevel@tonic-gate 	{0x0c0600, 0xffff00, 0x6}	/* Serial Bus, Infiniband */
4097c478bd9Sstevel@tonic-gate };
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate /*
4127c478bd9Sstevel@tonic-gate  * Default class to intr_weight value mapping (% of CPU).  A driver.conf
4137c478bd9Sstevel@tonic-gate  * entry on or above the pci node like
4147c478bd9Sstevel@tonic-gate  *
4157c478bd9Sstevel@tonic-gate  *	pci-class-intr-weights= 0x020000, 0xff0000, 30;
4167c478bd9Sstevel@tonic-gate  *
4177c478bd9Sstevel@tonic-gate  * can be used to augment or override entries in the default table below.
4187c478bd9Sstevel@tonic-gate  *
4197c478bd9Sstevel@tonic-gate  * NB: The values below give NICs preference on redistribution, and provide
4207c478bd9Sstevel@tonic-gate  * NICs some isolation from other interrupt sources. We need better interfaces
4217c478bd9Sstevel@tonic-gate  * that allow the NIC driver to identify a specific NIC instance as high
4227c478bd9Sstevel@tonic-gate  * bandwidth, and thus deserving of separation from other low bandwidth
4237c478bd9Sstevel@tonic-gate  * NICs additional isolation from other interrupt sources.
4247c478bd9Sstevel@tonic-gate  *
4257c478bd9Sstevel@tonic-gate  * NB: We treat Infiniband like a NIC.
4267c478bd9Sstevel@tonic-gate  */
4277c478bd9Sstevel@tonic-gate px_class_val_t px_default_intr_weight [] = {
4287c478bd9Sstevel@tonic-gate 	{0x020000, 0xff0000, 35},	/* Network Controller */
4297c478bd9Sstevel@tonic-gate 	{0x010000, 0xff0000, 10},	/* Mass Storage Controller */
4307c478bd9Sstevel@tonic-gate 	{0x0c0400, 0xffff00, 10},	/* Serial Bus, Fibre Channel */
4317c478bd9Sstevel@tonic-gate 	{0x0c0600, 0xffff00, 50}	/* Serial Bus, Infiniband */
4327c478bd9Sstevel@tonic-gate };
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate static uint32_t
4357c478bd9Sstevel@tonic-gate px_match_class_val(uint32_t key, px_class_val_t *rec_p, int nrec,
4367c478bd9Sstevel@tonic-gate     uint32_t default_val)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	int	i;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	for (i = 0; i < nrec; rec_p++, i++) {
4417c478bd9Sstevel@tonic-gate 		if ((rec_p->class_code & rec_p->class_mask) ==
4427c478bd9Sstevel@tonic-gate 		    (key & rec_p->class_mask))
4437c478bd9Sstevel@tonic-gate 			return (rec_p->class_val);
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	return (default_val);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate  * px_class_to_val
4517c478bd9Sstevel@tonic-gate  *
4527c478bd9Sstevel@tonic-gate  * Return the configuration value, based on class code and sub class code,
4537c478bd9Sstevel@tonic-gate  * from the specified property based or default px_class_val_t table.
4547c478bd9Sstevel@tonic-gate  */
4557c478bd9Sstevel@tonic-gate uint32_t
4567c478bd9Sstevel@tonic-gate px_class_to_val(dev_info_t *rdip, char *property_name, px_class_val_t *rec_p,
4577c478bd9Sstevel@tonic-gate     int nrec, uint32_t default_val)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	int property_len;
4607c478bd9Sstevel@tonic-gate 	uint32_t class_code;
4617c478bd9Sstevel@tonic-gate 	px_class_val_t *conf;
4627c478bd9Sstevel@tonic-gate 	uint32_t val = default_val;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	/*
4657c478bd9Sstevel@tonic-gate 	 * Use the "class-code" property to get the base and sub class
4667c478bd9Sstevel@tonic-gate 	 * codes for the requesting device.
4677c478bd9Sstevel@tonic-gate 	 */
4687c478bd9Sstevel@tonic-gate 	class_code = (uint32_t)ddi_prop_get_int(DDI_DEV_T_ANY, rdip,
4697c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "class-code", -1);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	if (class_code == -1)
4727c478bd9Sstevel@tonic-gate 		return (val);
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	/* look up the val from the default table */
4757c478bd9Sstevel@tonic-gate 	val = px_match_class_val(class_code, rec_p, nrec, val);
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/* see if there is a more specific property specified value */
4787c478bd9Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_NOTPROM,
4797c478bd9Sstevel@tonic-gate 	    property_name, (caddr_t)&conf, &property_len))
4807c478bd9Sstevel@tonic-gate 		return (val);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	if ((property_len % sizeof (px_class_val_t)) == 0)
4837c478bd9Sstevel@tonic-gate 		val = px_match_class_val(class_code, conf,
4847c478bd9Sstevel@tonic-gate 		    property_len / sizeof (px_class_val_t), val);
4857c478bd9Sstevel@tonic-gate 	kmem_free(conf, property_len);
4867c478bd9Sstevel@tonic-gate 	return (val);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /* px_class_to_pil: return the pil for a given device. */
4907c478bd9Sstevel@tonic-gate uint32_t
4917c478bd9Sstevel@tonic-gate px_class_to_pil(dev_info_t *rdip)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate 	uint32_t pil;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	/* default pil is 0 (uninitialized) */
4967c478bd9Sstevel@tonic-gate 	pil = px_class_to_val(rdip,
4977c478bd9Sstevel@tonic-gate 	    "pci-class-priorities", px_default_pil,
4987c478bd9Sstevel@tonic-gate 	    sizeof (px_default_pil) / sizeof (px_class_val_t), 0);
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	/* range check the result */
5017c478bd9Sstevel@tonic-gate 	if (pil >= 0xf)
5027c478bd9Sstevel@tonic-gate 		pil = 0;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	return (pil);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate /* px_class_to_intr_weight: return the intr_weight for a given device. */
5087c478bd9Sstevel@tonic-gate static int32_t
5097c478bd9Sstevel@tonic-gate px_class_to_intr_weight(dev_info_t *rdip)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 	int32_t intr_weight;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	/* default weight is 0% */
5147c478bd9Sstevel@tonic-gate 	intr_weight = px_class_to_val(rdip,
5157c478bd9Sstevel@tonic-gate 	    "pci-class-intr-weights", px_default_intr_weight,
5167c478bd9Sstevel@tonic-gate 	    sizeof (px_default_intr_weight) / sizeof (px_class_val_t), 0);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	/* range check the result */
5197c478bd9Sstevel@tonic-gate 	if (intr_weight < 0)
5207c478bd9Sstevel@tonic-gate 		intr_weight = 0;
5217c478bd9Sstevel@tonic-gate 	if (intr_weight > 1000)
5227c478bd9Sstevel@tonic-gate 		intr_weight = 1000;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	return (intr_weight);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /* ARGSUSED */
5287c478bd9Sstevel@tonic-gate int
5297c478bd9Sstevel@tonic-gate px_intx_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
5307c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
5317c478bd9Sstevel@tonic-gate {
532a195726fSgovinda 	px_t	*px_p = DIP_TO_STATE(dip);
533a195726fSgovinda 	int	ret = DDI_SUCCESS;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	DBG(DBG_INTROPS, dip, "px_intx_ops: dip=%x rdip=%x intr_op=%x "
5367c478bd9Sstevel@tonic-gate 	    "handle=%p\n", dip, rdip, intr_op, hdlp);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	switch (intr_op) {
5397c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
5407c478bd9Sstevel@tonic-gate 		ret = pci_intx_get_cap(rdip, (int *)result);
5417c478bd9Sstevel@tonic-gate 		break;
5427c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETCAP:
5437c478bd9Sstevel@tonic-gate 		DBG(DBG_INTROPS, dip, "px_intx_ops: SetCap is not supported\n");
5447c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
5457c478bd9Sstevel@tonic-gate 		break;
5467c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ALLOC:
5477c478bd9Sstevel@tonic-gate 		*(int *)result = hdlp->ih_scratch1;
5487c478bd9Sstevel@tonic-gate 		break;
5497c478bd9Sstevel@tonic-gate 	case DDI_INTROP_FREE:
5507c478bd9Sstevel@tonic-gate 		break;
5517c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
552a195726fSgovinda 		*(int *)result = hdlp->ih_pri ?
553a195726fSgovinda 		    hdlp->ih_pri : px_class_to_pil(rdip);
5547c478bd9Sstevel@tonic-gate 		break;
5557c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETPRI:
5567c478bd9Sstevel@tonic-gate 		break;
5577c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
5587c478bd9Sstevel@tonic-gate 		ret = px_add_intx_intr(dip, rdip, hdlp);
5597c478bd9Sstevel@tonic-gate 		break;
5607c478bd9Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
5617c478bd9Sstevel@tonic-gate 		ret = px_rem_intx_intr(dip, rdip, hdlp);
5627c478bd9Sstevel@tonic-gate 		break;
5637c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ENABLE:
5647c478bd9Sstevel@tonic-gate 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
56536fe4a92Segillett 		    hdlp->ih_vector, PX_INTR_STATE_ENABLE, 0, 0);
5667c478bd9Sstevel@tonic-gate 		break;
5677c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
5687c478bd9Sstevel@tonic-gate 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
56936fe4a92Segillett 		    hdlp->ih_vector, PX_INTR_STATE_DISABLE, 0, 0);
5707c478bd9Sstevel@tonic-gate 		break;
5717c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETMASK:
5727c478bd9Sstevel@tonic-gate 		ret = pci_intx_set_mask(rdip);
5737c478bd9Sstevel@tonic-gate 		break;
5747c478bd9Sstevel@tonic-gate 	case DDI_INTROP_CLRMASK:
5757c478bd9Sstevel@tonic-gate 		ret = pci_intx_clr_mask(rdip);
5767c478bd9Sstevel@tonic-gate 		break;
5777c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPENDING:
5787c478bd9Sstevel@tonic-gate 		ret = pci_intx_get_pending(rdip, (int *)result);
5797c478bd9Sstevel@tonic-gate 		break;
5807c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
5817c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NAVAIL:
5827c478bd9Sstevel@tonic-gate 		*(int *)result = i_ddi_get_nintrs(rdip);
5837c478bd9Sstevel@tonic-gate 		break;
5847c478bd9Sstevel@tonic-gate 	default:
5857c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
5867c478bd9Sstevel@tonic-gate 		break;
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	return (ret);
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate /* ARGSUSED */
5937c478bd9Sstevel@tonic-gate int
5947c478bd9Sstevel@tonic-gate px_msix_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
5957c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate 	px_t			*px_p = DIP_TO_STATE(dip);
5987c478bd9Sstevel@tonic-gate 	px_msi_state_t		*msi_state_p = &px_p->px_ib_p->ib_msi_state;
5999c75c6bfSgovinda 	msiq_rec_type_t		msiq_rec_type;
6009c75c6bfSgovinda 	msi_type_t		msi_type;
6019c75c6bfSgovinda 	uint64_t		msi_addr;
6027c478bd9Sstevel@tonic-gate 	msinum_t		msi_num;
6037c478bd9Sstevel@tonic-gate 	msiqid_t		msiq_id;
6047c478bd9Sstevel@tonic-gate 	uint_t			nintrs;
6057c478bd9Sstevel@tonic-gate 	int			i, ret = DDI_SUCCESS;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	DBG(DBG_INTROPS, dip, "px_msix_ops: dip=%x rdip=%x intr_op=%x "
6087c478bd9Sstevel@tonic-gate 	    "handle=%p\n", dip, rdip, intr_op, hdlp);
6097c478bd9Sstevel@tonic-gate 
6109c75c6bfSgovinda 	/* Check for MSI64 support */
611*07f14c08Sgovinda 	if ((hdlp->ih_cap & DDI_INTR_FLAG_MSI64) && msi_state_p->msi_addr64) {
6129c75c6bfSgovinda 		msiq_rec_type = MSI64_REC;
6139c75c6bfSgovinda 		msi_type = MSI64_TYPE;
614*07f14c08Sgovinda 		msi_addr = msi_state_p->msi_addr64;
6159c75c6bfSgovinda 	} else {
6169c75c6bfSgovinda 		msiq_rec_type = MSI32_REC;
6179c75c6bfSgovinda 		msi_type = MSI32_TYPE;
6189c75c6bfSgovinda 		msi_addr = msi_state_p->msi_addr32;
6199c75c6bfSgovinda 	}
6209c75c6bfSgovinda 
6217c478bd9Sstevel@tonic-gate 	switch (intr_op) {
6227c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
6237c478bd9Sstevel@tonic-gate 		ret = pci_msi_get_cap(rdip, hdlp->ih_type, (int *)result);
6247c478bd9Sstevel@tonic-gate 		break;
6257c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETCAP:
6267c478bd9Sstevel@tonic-gate 		DBG(DBG_INTROPS, dip, "px_msix_ops: SetCap is not supported\n");
6277c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
6287c478bd9Sstevel@tonic-gate 		break;
6297c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ALLOC:
6307c478bd9Sstevel@tonic-gate 		/*
6317c478bd9Sstevel@tonic-gate 		 * We need to restrict this allocation in future
6327c478bd9Sstevel@tonic-gate 		 * based on Resource Management policies.
6337c478bd9Sstevel@tonic-gate 		 */
6347c478bd9Sstevel@tonic-gate 		if ((ret = px_msi_alloc(px_p, rdip, hdlp->ih_inum,
635102cb92eSjohnny 		    hdlp->ih_scratch1, (int)hdlp->ih_scratch2, &msi_num,
6367c478bd9Sstevel@tonic-gate 		    (int *)result)) != DDI_SUCCESS) {
6377c478bd9Sstevel@tonic-gate 			DBG(DBG_INTROPS, dip, "px_msix_ops: MSI allocation "
6387c478bd9Sstevel@tonic-gate 			    "failed, rdip 0x%p inum 0x%x count 0x%x\n",
6397c478bd9Sstevel@tonic-gate 			    rdip, hdlp->ih_inum, hdlp->ih_scratch1);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 			return (ret);
6427c478bd9Sstevel@tonic-gate 		}
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		break;
6457c478bd9Sstevel@tonic-gate 	case DDI_INTROP_FREE:
6467c478bd9Sstevel@tonic-gate 		(void) pci_msi_disable_mode(rdip, hdlp->ih_type, hdlp->ih_inum);
6477c478bd9Sstevel@tonic-gate 		(void) pci_msi_unconfigure(rdip, hdlp->ih_type, hdlp->ih_inum);
6487c478bd9Sstevel@tonic-gate 		(void) px_msi_free(px_p, rdip, hdlp->ih_inum,
6497c478bd9Sstevel@tonic-gate 		    hdlp->ih_scratch1);
6507c478bd9Sstevel@tonic-gate 		break;
6517c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
6527c478bd9Sstevel@tonic-gate 		*(int *)result = hdlp->ih_pri ?
6537c478bd9Sstevel@tonic-gate 		    hdlp->ih_pri : px_class_to_pil(rdip);
6547c478bd9Sstevel@tonic-gate 		break;
6557c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETPRI:
6567c478bd9Sstevel@tonic-gate 		break;
6577c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
6587c478bd9Sstevel@tonic-gate 		if ((ret = px_msi_get_msinum(px_p, hdlp->ih_dip,
6597c478bd9Sstevel@tonic-gate 		    hdlp->ih_inum, &msi_num)) != DDI_SUCCESS)
6607c478bd9Sstevel@tonic-gate 			return (ret);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		if ((ret = px_add_msiq_intr(dip, rdip, hdlp,
6639c75c6bfSgovinda 		    msiq_rec_type, msi_num, &msiq_id)) != DDI_SUCCESS) {
6647c478bd9Sstevel@tonic-gate 			DBG(DBG_INTROPS, dip, "px_msix_ops: Add MSI handler "
6657c478bd9Sstevel@tonic-gate 			    "failed, rdip 0x%p msi 0x%x\n", rdip, msi_num);
6667c478bd9Sstevel@tonic-gate 			return (ret);
6677c478bd9Sstevel@tonic-gate 		}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 		DBG(DBG_INTROPS, dip, "px_msix_ops: msiq used 0x%x\n", msiq_id);
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setmsiq(dip, msi_num,
6729c75c6bfSgovinda 		    msiq_id, msi_type)) != DDI_SUCCESS) {
6737c478bd9Sstevel@tonic-gate 			(void) px_rem_msiq_intr(dip, rdip,
6749c75c6bfSgovinda 			    hdlp, msiq_rec_type, msi_num, msiq_id);
6757c478bd9Sstevel@tonic-gate 			return (ret);
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setstate(dip, msi_num,
6797c478bd9Sstevel@tonic-gate 		    PCI_MSI_STATE_IDLE)) != DDI_SUCCESS) {
6807c478bd9Sstevel@tonic-gate 			(void) px_rem_msiq_intr(dip, rdip,
6819c75c6bfSgovinda 			    hdlp, msiq_rec_type, msi_num, msiq_id);
6827c478bd9Sstevel@tonic-gate 			return (ret);
6837c478bd9Sstevel@tonic-gate 		}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = msi_num;
6867c478bd9Sstevel@tonic-gate 		break;
6877c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DUPVEC:
6887c478bd9Sstevel@tonic-gate 		DBG(DBG_INTROPS, dip, "px_msix_ops: DupIsr is not supported\n");
6897c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
6907c478bd9Sstevel@tonic-gate 		break;
6917c478bd9Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
6927c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
6957c478bd9Sstevel@tonic-gate 		    &msiq_id)) != DDI_SUCCESS)
6967c478bd9Sstevel@tonic-gate 			return (ret);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setstate(dip, msi_num,
6999c75c6bfSgovinda 		    PCI_MSI_STATE_IDLE)) != DDI_SUCCESS)
7007c478bd9Sstevel@tonic-gate 			return (ret);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 		ret = px_rem_msiq_intr(dip, rdip,
7039c75c6bfSgovinda 		    hdlp, msiq_rec_type, msi_num, msiq_id);
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = 0;
7067c478bd9Sstevel@tonic-gate 		break;
7077c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ENABLE:
7087c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setvalid(dip, msi_num,
7117c478bd9Sstevel@tonic-gate 		    PCI_MSI_VALID)) != DDI_SUCCESS)
7127c478bd9Sstevel@tonic-gate 			return (ret);
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 		if (pci_is_msi_enabled(rdip, hdlp->ih_type) != DDI_SUCCESS) {
7157c478bd9Sstevel@tonic-gate 			nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 			if ((ret = pci_msi_configure(rdip, hdlp->ih_type,
7189c75c6bfSgovinda 			    nintrs, hdlp->ih_inum, msi_addr,
7197c478bd9Sstevel@tonic-gate 			    msi_num & ~(nintrs - 1))) != DDI_SUCCESS)
7207c478bd9Sstevel@tonic-gate 				return (ret);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 			if ((ret = pci_msi_enable_mode(rdip, hdlp->ih_type,
7237c478bd9Sstevel@tonic-gate 			    hdlp->ih_inum)) != DDI_SUCCESS)
7247c478bd9Sstevel@tonic-gate 				return (ret);
7257c478bd9Sstevel@tonic-gate 		}
7267c478bd9Sstevel@tonic-gate 
72736fe4a92Segillett 		if ((ret = pci_msi_clr_mask(rdip, hdlp->ih_type,
72836fe4a92Segillett 		    hdlp->ih_inum)) != DDI_SUCCESS)
72936fe4a92Segillett 			return (ret);
73036fe4a92Segillett 
73136fe4a92Segillett 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
73236fe4a92Segillett 		    &msiq_id)) != DDI_SUCCESS)
73336fe4a92Segillett 			return (ret);
73436fe4a92Segillett 
73536fe4a92Segillett 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
73636fe4a92Segillett 		    px_msiqid_to_devino(px_p, msiq_id), PX_INTR_STATE_ENABLE,
7379c75c6bfSgovinda 		    msiq_rec_type, msi_num);
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		break;
7407c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
7417c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 		if ((ret = pci_msi_set_mask(rdip, hdlp->ih_type,
7447c478bd9Sstevel@tonic-gate 		    hdlp->ih_inum)) != DDI_SUCCESS)
7457c478bd9Sstevel@tonic-gate 			return (ret);
7467c478bd9Sstevel@tonic-gate 
74736fe4a92Segillett 		if ((ret = px_lib_msi_setvalid(dip, msi_num,
74836fe4a92Segillett 		    PCI_MSI_INVALID)) != DDI_SUCCESS)
74936fe4a92Segillett 			return (ret);
75036fe4a92Segillett 
75136fe4a92Segillett 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
75236fe4a92Segillett 		    &msiq_id)) != DDI_SUCCESS)
75336fe4a92Segillett 			return (ret);
75436fe4a92Segillett 
75536fe4a92Segillett 		ret = px_ib_update_intr_state(px_p, rdip,
75636fe4a92Segillett 		    hdlp->ih_inum, px_msiqid_to_devino(px_p, msiq_id),
7579c75c6bfSgovinda 		    PX_INTR_STATE_DISABLE, msiq_rec_type, msi_num);
75836fe4a92Segillett 
7597c478bd9Sstevel@tonic-gate 		break;
7607c478bd9Sstevel@tonic-gate 	case DDI_INTROP_BLOCKENABLE:
7617c478bd9Sstevel@tonic-gate 		nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
7627c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		if ((ret = pci_msi_configure(rdip, hdlp->ih_type,
7659c75c6bfSgovinda 		    nintrs, hdlp->ih_inum, msi_addr,
7667c478bd9Sstevel@tonic-gate 		    msi_num & ~(nintrs - 1))) != DDI_SUCCESS)
7677c478bd9Sstevel@tonic-gate 			return (ret);
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 		for (i = 0; i < nintrs; i++, msi_num++) {
7707c478bd9Sstevel@tonic-gate 			if ((ret = px_lib_msi_setvalid(dip, msi_num,
7717c478bd9Sstevel@tonic-gate 			    PCI_MSI_VALID)) != DDI_SUCCESS)
7727c478bd9Sstevel@tonic-gate 				return (ret);
77336fe4a92Segillett 
77436fe4a92Segillett 			if ((ret = px_lib_msi_getmsiq(dip, msi_num,
77536fe4a92Segillett 			    &msiq_id)) != DDI_SUCCESS)
77636fe4a92Segillett 				return (ret);
77736fe4a92Segillett 
77836fe4a92Segillett 			if ((ret = px_ib_update_intr_state(px_p, rdip,
77936fe4a92Segillett 			    hdlp->ih_inum + i, px_msiqid_to_devino(px_p,
7809c75c6bfSgovinda 			    msiq_id), PX_INTR_STATE_ENABLE, msiq_rec_type,
7819c75c6bfSgovinda 			    msi_num)) != DDI_SUCCESS)
78236fe4a92Segillett 				return (ret);
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 		ret = pci_msi_enable_mode(rdip, hdlp->ih_type, hdlp->ih_inum);
7867c478bd9Sstevel@tonic-gate 		break;
7877c478bd9Sstevel@tonic-gate 	case DDI_INTROP_BLOCKDISABLE:
7887c478bd9Sstevel@tonic-gate 		nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
7897c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 		if ((ret = pci_msi_disable_mode(rdip, hdlp->ih_type,
7927c478bd9Sstevel@tonic-gate 		    hdlp->ih_inum)) != DDI_SUCCESS)
7937c478bd9Sstevel@tonic-gate 			return (ret);
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 		for (i = 0; i < nintrs; i++, msi_num++) {
7967c478bd9Sstevel@tonic-gate 			if ((ret = px_lib_msi_setvalid(dip, msi_num,
7977c478bd9Sstevel@tonic-gate 			    PCI_MSI_INVALID)) != DDI_SUCCESS)
7987c478bd9Sstevel@tonic-gate 				return (ret);
79936fe4a92Segillett 
80036fe4a92Segillett 			if ((ret = px_lib_msi_getmsiq(dip, msi_num,
80136fe4a92Segillett 			    &msiq_id)) != DDI_SUCCESS)
80236fe4a92Segillett 				return (ret);
80336fe4a92Segillett 
80436fe4a92Segillett 			if ((ret = px_ib_update_intr_state(px_p, rdip,
80536fe4a92Segillett 			    hdlp->ih_inum + i, px_msiqid_to_devino(px_p,
8069c75c6bfSgovinda 			    msiq_id), PX_INTR_STATE_DISABLE, msiq_rec_type,
80736fe4a92Segillett 			    msi_num)) != DDI_SUCCESS)
80836fe4a92Segillett 				return (ret);
8097c478bd9Sstevel@tonic-gate 		}
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 		break;
8127c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETMASK:
8137c478bd9Sstevel@tonic-gate 		ret = pci_msi_set_mask(rdip, hdlp->ih_type, hdlp->ih_inum);
8147c478bd9Sstevel@tonic-gate 		break;
8157c478bd9Sstevel@tonic-gate 	case DDI_INTROP_CLRMASK:
8167c478bd9Sstevel@tonic-gate 		ret = pci_msi_clr_mask(rdip, hdlp->ih_type, hdlp->ih_inum);
8177c478bd9Sstevel@tonic-gate 		break;
8187c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPENDING:
8197c478bd9Sstevel@tonic-gate 		ret = pci_msi_get_pending(rdip, hdlp->ih_type,
8207c478bd9Sstevel@tonic-gate 		    hdlp->ih_inum, (int *)result);
8217c478bd9Sstevel@tonic-gate 		break;
8227c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
8237c478bd9Sstevel@tonic-gate 		ret = pci_msi_get_nintrs(rdip, hdlp->ih_type, (int *)result);
8247c478bd9Sstevel@tonic-gate 		break;
8257c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NAVAIL:
8267c478bd9Sstevel@tonic-gate 		/* XXX - a new interface may be needed */
8277c478bd9Sstevel@tonic-gate 		ret = pci_msi_get_nintrs(rdip, hdlp->ih_type, (int *)result);
8287c478bd9Sstevel@tonic-gate 		break;
8297c478bd9Sstevel@tonic-gate 	default:
8307c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
8317c478bd9Sstevel@tonic-gate 		break;
8327c478bd9Sstevel@tonic-gate 	}
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	return (ret);
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate 
8376d44af1bSesolom static struct {
8386d44af1bSesolom 	kstat_named_t pxintr_ks_name;
8396d44af1bSesolom 	kstat_named_t pxintr_ks_type;
8406d44af1bSesolom 	kstat_named_t pxintr_ks_cpu;
8416d44af1bSesolom 	kstat_named_t pxintr_ks_pil;
8426d44af1bSesolom 	kstat_named_t pxintr_ks_time;
8436d44af1bSesolom 	kstat_named_t pxintr_ks_ino;
8446d44af1bSesolom 	kstat_named_t pxintr_ks_cookie;
8456d44af1bSesolom 	kstat_named_t pxintr_ks_devpath;
8466d44af1bSesolom 	kstat_named_t pxintr_ks_buspath;
8476d44af1bSesolom } pxintr_ks_template = {
8486d44af1bSesolom 	{ "name",	KSTAT_DATA_CHAR },
8496d44af1bSesolom 	{ "type",	KSTAT_DATA_CHAR },
8506d44af1bSesolom 	{ "cpu",	KSTAT_DATA_UINT64 },
8516d44af1bSesolom 	{ "pil",	KSTAT_DATA_UINT64 },
8526d44af1bSesolom 	{ "time",	KSTAT_DATA_UINT64 },
8536d44af1bSesolom 	{ "ino",	KSTAT_DATA_UINT64 },
8546d44af1bSesolom 	{ "cookie",	KSTAT_DATA_UINT64 },
8556d44af1bSesolom 	{ "devpath",	KSTAT_DATA_STRING },
8566d44af1bSesolom 	{ "buspath",	KSTAT_DATA_STRING },
8576d44af1bSesolom };
8586d44af1bSesolom 
8596d44af1bSesolom static uint32_t pxintr_ks_instance;
8606d44af1bSesolom kmutex_t pxintr_ks_template_lock;
8616d44af1bSesolom 
8626d44af1bSesolom int
8636d44af1bSesolom px_ks_update(kstat_t *ksp, int rw)
8646d44af1bSesolom {
8656d44af1bSesolom 	px_ih_t *ih_p = ksp->ks_private;
8666d44af1bSesolom 	int maxlen = sizeof (pxintr_ks_template.pxintr_ks_name.value.c);
8676d44af1bSesolom 	px_ib_t *ib_p = ih_p->ih_ino_p->ino_ib_p;
8686d44af1bSesolom 	px_t *px_p = ib_p->ib_px_p;
8696d44af1bSesolom 	devino_t ino;
8706d44af1bSesolom 	sysino_t sysino;
8716d44af1bSesolom 	char ih_devpath[MAXPATHLEN];
8726d44af1bSesolom 	char ih_buspath[MAXPATHLEN];
8736d44af1bSesolom 
8746d44af1bSesolom 	ino = ih_p->ih_ino_p->ino_ino;
8756d44af1bSesolom 	(void) px_lib_intr_devino_to_sysino(px_p->px_dip, ino, &sysino);
8766d44af1bSesolom 
8776d44af1bSesolom 	(void) snprintf(pxintr_ks_template.pxintr_ks_name.value.c, maxlen,
8786d44af1bSesolom 	    "%s%d", ddi_driver_name(ih_p->ih_dip),
8796d44af1bSesolom 	    ddi_get_instance(ih_p->ih_dip));
8806d44af1bSesolom 
8816d44af1bSesolom 	(void) ddi_pathname(ih_p->ih_dip, ih_devpath);
8826d44af1bSesolom 	(void) ddi_pathname(px_p->px_dip, ih_buspath);
8836d44af1bSesolom 	kstat_named_setstr(&pxintr_ks_template.pxintr_ks_devpath, ih_devpath);
8846d44af1bSesolom 	kstat_named_setstr(&pxintr_ks_template.pxintr_ks_buspath, ih_buspath);
8856d44af1bSesolom 
886e1d9f4e6Sschwartz 	if (ih_p->ih_intr_state == PX_INTR_STATE_ENABLE) {
887e1d9f4e6Sschwartz 
888e1d9f4e6Sschwartz 		(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
889e1d9f4e6Sschwartz 		    (ih_p->ih_rec_type == 0) ? "fixed" : "msi");
890e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_cpu.value.ui64 =
891e1d9f4e6Sschwartz 		    ih_p->ih_ino_p->ino_cpuid;
892e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_pil.value.ui64 =
893e1d9f4e6Sschwartz 		    ih_p->ih_ino_p->ino_pil;
894e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_time.value.ui64 = ih_p->ih_nsec +
895e1d9f4e6Sschwartz 		    (uint64_t)tick2ns((hrtime_t)ih_p->ih_ticks,
896e1d9f4e6Sschwartz 			ih_p->ih_ino_p->ino_cpuid);
897e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_ino.value.ui64 = ino;
898e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_cookie.value.ui64 = sysino;
899e1d9f4e6Sschwartz 	} else {
900e1d9f4e6Sschwartz 		(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
901e1d9f4e6Sschwartz 		    "disabled");
902e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_cpu.value.ui64 = 0;
903e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_pil.value.ui64 = 0;
904e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_time.value.ui64 = 0;
905e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_ino.value.ui64 = 0;
906e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_cookie.value.ui64 = 0;
907e1d9f4e6Sschwartz 	}
9086d44af1bSesolom 	return (0);
9096d44af1bSesolom }
9106d44af1bSesolom 
9116d44af1bSesolom void
9126d44af1bSesolom px_create_intr_kstats(px_ih_t *ih_p)
9136d44af1bSesolom {
9146d44af1bSesolom 	msiq_rec_type_t rec_type = ih_p->ih_rec_type;
9156d44af1bSesolom 
9166d44af1bSesolom 	ASSERT(ih_p->ih_ksp == NULL);
9176d44af1bSesolom 
9186d44af1bSesolom 	/*
9196d44af1bSesolom 	 * Create pci_intrs::: kstats for all ih types except messages,
9206d44af1bSesolom 	 * which represent unusual conditions and don't need to be tracked.
9216d44af1bSesolom 	 */
9226d44af1bSesolom 	if (rec_type == 0 || rec_type == MSI32_REC || rec_type == MSI64_REC) {
9236d44af1bSesolom 		ih_p->ih_ksp = kstat_create("pci_intrs",
9246d44af1bSesolom 		    atomic_inc_32_nv(&pxintr_ks_instance), "config",
9256d44af1bSesolom 		    "interrupts", KSTAT_TYPE_NAMED,
9266d44af1bSesolom 		    sizeof (pxintr_ks_template) / sizeof (kstat_named_t),
9276d44af1bSesolom 		    KSTAT_FLAG_VIRTUAL);
9286d44af1bSesolom 	}
9296d44af1bSesolom 	if (ih_p->ih_ksp != NULL) {
9306d44af1bSesolom 		ih_p->ih_ksp->ks_data_size += MAXPATHLEN * 2;
9316d44af1bSesolom 		ih_p->ih_ksp->ks_lock = &pxintr_ks_template_lock;
9326d44af1bSesolom 		ih_p->ih_ksp->ks_data = &pxintr_ks_template;
9336d44af1bSesolom 		ih_p->ih_ksp->ks_private = ih_p;
9346d44af1bSesolom 		ih_p->ih_ksp->ks_update = px_ks_update;
9356d44af1bSesolom 	}
9366d44af1bSesolom }
9376d44af1bSesolom 
938a195726fSgovinda /*
939a195726fSgovinda  * px_add_intx_intr:
940a195726fSgovinda  *
941a195726fSgovinda  * This function is called to register INTx and legacy hardware
942a195726fSgovinda  * interrupt pins interrupts.
943a195726fSgovinda  */
9447c478bd9Sstevel@tonic-gate int
9457c478bd9Sstevel@tonic-gate px_add_intx_intr(dev_info_t *dip, dev_info_t *rdip,
9467c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
9477c478bd9Sstevel@tonic-gate {
9487c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
9497c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
9507c478bd9Sstevel@tonic-gate 	devino_t	ino;
9517c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
9527c478bd9Sstevel@tonic-gate 	px_ib_ino_info_t *ino_p;
9537c478bd9Sstevel@tonic-gate 	int32_t		weight;
9547c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	ino = hdlp->ih_vector;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: rdip=%s%d ino=%x "
9597c478bd9Sstevel@tonic-gate 	    "handler=%x arg1=%x arg2=%x\n", ddi_driver_name(rdip),
9607c478bd9Sstevel@tonic-gate 	    ddi_get_instance(rdip), ino, hdlp->ih_cb_func,
9617c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	ih_p = px_ib_alloc_ih(rdip, hdlp->ih_inum,
9647c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, 0, 0);
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	if (ino_p = px_ib_locate_ino(ib_p, ino)) {	/* sharing ino */
9697c478bd9Sstevel@tonic-gate 		uint32_t intr_index = hdlp->ih_inum;
9707c478bd9Sstevel@tonic-gate 		if (px_ib_ino_locate_intr(ino_p, rdip, intr_index, 0, 0)) {
9717c478bd9Sstevel@tonic-gate 			DBG(DBG_A_INTX, dip, "px_add_intx_intr: "
9727c478bd9Sstevel@tonic-gate 			    "dup intr #%d\n", intr_index);
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 			ret = DDI_FAILURE;
9757c478bd9Sstevel@tonic-gate 			goto fail1;
9767c478bd9Sstevel@tonic-gate 		}
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 		/* Save mondo value in hdlp */
9797c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 		if ((ret = px_ib_ino_add_intr(px_p, ino_p, ih_p))
9827c478bd9Sstevel@tonic-gate 		    != DDI_SUCCESS)
9837c478bd9Sstevel@tonic-gate 			goto fail1;
9847c478bd9Sstevel@tonic-gate 	} else {
9857c478bd9Sstevel@tonic-gate 		ino_p = px_ib_new_ino(ib_p, ino, ih_p);
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 		if (hdlp->ih_pri == 0)
9887c478bd9Sstevel@tonic-gate 			hdlp->ih_pri = px_class_to_pil(rdip);
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 		/* Save mondo value in hdlp */
9917c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 		DBG(DBG_A_INTX, dip, "px_add_intx_intr: pil=0x%x mondo=0x%x\n",
9947c478bd9Sstevel@tonic-gate 		    hdlp->ih_pri, hdlp->ih_vector);
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
9977c478bd9Sstevel@tonic-gate 		    (ddi_intr_handler_t *)px_intx_intr, (caddr_t)ino_p, NULL);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 		ret = i_ddi_add_ivintr(hdlp);
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 		/*
10027c478bd9Sstevel@tonic-gate 		 * Restore original interrupt handler
10037c478bd9Sstevel@tonic-gate 		 * and arguments in interrupt handle.
10047c478bd9Sstevel@tonic-gate 		 */
10057c478bd9Sstevel@tonic-gate 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
10067c478bd9Sstevel@tonic-gate 		    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 		if (ret != DDI_SUCCESS)
10097c478bd9Sstevel@tonic-gate 			goto fail2;
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 		/* Save the pil for this ino */
10127c478bd9Sstevel@tonic-gate 		ino_p->ino_pil = hdlp->ih_pri;
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 		/* select cpu, saving it for sharing and removal */
10157c478bd9Sstevel@tonic-gate 		ino_p->ino_cpuid = intr_dist_cpuid();
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 		/* Enable interrupt */
10187c478bd9Sstevel@tonic-gate 		px_ib_intr_enable(px_p, ino_p->ino_cpuid, ino);
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	/* add weight to the cpu that we are already targeting */
10227c478bd9Sstevel@tonic-gate 	weight = px_class_to_intr_weight(rdip);
10237c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_add_device_weight(ino_p->ino_cpuid, rdip, weight);
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	ih_p->ih_ino_p = ino_p;
10266d44af1bSesolom 	px_create_intr_kstats(ih_p);
10277c478bd9Sstevel@tonic-gate 	if (ih_p->ih_ksp)
10287c478bd9Sstevel@tonic-gate 		kstat_install(ih_p->ih_ksp);
10297c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: done! Interrupt 0x%x pil=%x\n",
10327c478bd9Sstevel@tonic-gate 	    ino_p->ino_sysino, hdlp->ih_pri);
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	return (ret);
10357c478bd9Sstevel@tonic-gate fail2:
10367c478bd9Sstevel@tonic-gate 	px_ib_delete_ino(ib_p, ino_p);
10377c478bd9Sstevel@tonic-gate fail1:
10387c478bd9Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
10397c478bd9Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
10427c478bd9Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (px_ih_t));
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: Failed! Interrupt 0x%x "
10457c478bd9Sstevel@tonic-gate 	    "pil=%x\n", ino_p->ino_sysino, hdlp->ih_pri);
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	return (ret);
10487c478bd9Sstevel@tonic-gate }
10497c478bd9Sstevel@tonic-gate 
1050a195726fSgovinda /*
1051a195726fSgovinda  * px_rem_intx_intr:
1052a195726fSgovinda  *
1053a195726fSgovinda  * This function is called to unregister INTx and legacy hardware
1054a195726fSgovinda  * interrupt pins interrupts.
1055a195726fSgovinda  */
10567c478bd9Sstevel@tonic-gate int
10577c478bd9Sstevel@tonic-gate px_rem_intx_intr(dev_info_t *dip, dev_info_t *rdip,
10587c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
10597c478bd9Sstevel@tonic-gate {
10607c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
10617c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
10627c478bd9Sstevel@tonic-gate 	devino_t	ino;
10637c478bd9Sstevel@tonic-gate 	cpuid_t		curr_cpu;
10647c478bd9Sstevel@tonic-gate 	px_ib_ino_info_t	*ino_p;
10657c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
10667c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	ino = hdlp->ih_vector;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	DBG(DBG_R_INTX, dip, "px_rem_intx_intr: rdip=%s%d ino=%x\n",
10717c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	ino_p = px_ib_locate_ino(ib_p, ino);
10767c478bd9Sstevel@tonic-gate 	ih_p = px_ib_ino_locate_intr(ino_p, rdip, hdlp->ih_inum, 0, 0);
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	/* Get the current cpu */
10797c478bd9Sstevel@tonic-gate 	if ((ret = px_lib_intr_gettarget(px_p->px_dip, ino_p->ino_sysino,
10807c478bd9Sstevel@tonic-gate 	    &curr_cpu)) != DDI_SUCCESS)
10817c478bd9Sstevel@tonic-gate 		goto fail;
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	if ((ret = px_ib_ino_rem_intr(px_p, ino_p, ih_p)) != DDI_SUCCESS)
10847c478bd9Sstevel@tonic-gate 		goto fail;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	if (ino_p->ino_ih_size == 0) {
10897c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_intr_setstate(px_p->px_dip, ino_p->ino_sysino,
10907c478bd9Sstevel@tonic-gate 		    INTR_DELIVERED_STATE)) != DDI_SUCCESS)
10917c478bd9Sstevel@tonic-gate 			goto fail;
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
10947c478bd9Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 		px_ib_delete_ino(ib_p, ino_p);
10977c478bd9Sstevel@tonic-gate 		kmem_free(ino_p, sizeof (px_ib_ino_info_t));
10987c478bd9Sstevel@tonic-gate 	} else {
10997c478bd9Sstevel@tonic-gate 		/* Re-enable interrupt only if mapping regsiter still shared */
1100a195726fSgovinda 		PX_INTR_ENABLE(px_p->px_dip, ino_p->ino_sysino, curr_cpu);
11017c478bd9Sstevel@tonic-gate 	}
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate fail:
11047c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
11057c478bd9Sstevel@tonic-gate 	return (ret);
11067c478bd9Sstevel@tonic-gate }
11077c478bd9Sstevel@tonic-gate 
1108a195726fSgovinda /*
1109a195726fSgovinda  * px_add_msiq_intr:
1110a195726fSgovinda  *
1111a195726fSgovinda  * This function is called to register MSI/Xs and PCIe message interrupts.
1112a195726fSgovinda  */
11137c478bd9Sstevel@tonic-gate int
11147c478bd9Sstevel@tonic-gate px_add_msiq_intr(dev_info_t *dip, dev_info_t *rdip,
11157c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, msiq_rec_type_t rec_type,
11167c478bd9Sstevel@tonic-gate     msgcode_t msg_code, msiqid_t *msiq_id_p)
11177c478bd9Sstevel@tonic-gate {
11187c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
11197c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
11207c478bd9Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &ib_p->ib_msiq_state;
11217c478bd9Sstevel@tonic-gate 	devino_t	ino;
11227c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
11237c478bd9Sstevel@tonic-gate 	px_ib_ino_info_t	*ino_p;
11247c478bd9Sstevel@tonic-gate 	int32_t		weight;
11257c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: rdip=%s%d handler=%x "
11287c478bd9Sstevel@tonic-gate 	    "arg1=%x arg2=%x\n", ddi_driver_name(rdip), ddi_get_instance(rdip),
11297c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	if ((ret = px_msiq_alloc(px_p, rec_type, msiq_id_p)) != DDI_SUCCESS) {
11327c478bd9Sstevel@tonic-gate 		DBG(DBG_MSIQ, dip, "px_add_msiq_intr: "
11337c478bd9Sstevel@tonic-gate 		    "msiq allocation failed\n");
11347c478bd9Sstevel@tonic-gate 		return (ret);
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	ino = px_msiqid_to_devino(px_p, *msiq_id_p);
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	ih_p = px_ib_alloc_ih(rdip, hdlp->ih_inum, hdlp->ih_cb_func,
11407c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, rec_type, msg_code);
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	if (ino_p = px_ib_locate_ino(ib_p, ino)) {	/* sharing ino */
11457c478bd9Sstevel@tonic-gate 		uint32_t intr_index = hdlp->ih_inum;
11467c478bd9Sstevel@tonic-gate 		if (px_ib_ino_locate_intr(ino_p, rdip,
11477c478bd9Sstevel@tonic-gate 		    intr_index, rec_type, msg_code)) {
11487c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ, dip, "px_add_msiq_intr: "
11497c478bd9Sstevel@tonic-gate 			    "dup intr #%d\n", intr_index);
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 			ret = DDI_FAILURE;
11527c478bd9Sstevel@tonic-gate 			goto fail1;
11537c478bd9Sstevel@tonic-gate 		}
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 		if ((ret = px_ib_ino_add_intr(px_p, ino_p, ih_p))
11567c478bd9Sstevel@tonic-gate 		    != DDI_SUCCESS)
11577c478bd9Sstevel@tonic-gate 			goto fail1;
11587c478bd9Sstevel@tonic-gate 	} else {
11597c478bd9Sstevel@tonic-gate 		ino_p = px_ib_new_ino(ib_p, ino, ih_p);
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 		ino_p->ino_msiq_p = msiq_state_p->msiq_p +
11627c478bd9Sstevel@tonic-gate 		    (*msiq_id_p - msiq_state_p->msiq_1st_msiq_id);
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 		if (hdlp->ih_pri == 0)
11657c478bd9Sstevel@tonic-gate 			hdlp->ih_pri = px_class_to_pil(rdip);
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 		/* Save mondo value in hdlp */
11687c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 		DBG(DBG_MSIQ, dip, "px_add_msiq_intr: pil=0x%x mondo=0x%x\n",
11717c478bd9Sstevel@tonic-gate 		    hdlp->ih_pri, hdlp->ih_vector);
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
11747c478bd9Sstevel@tonic-gate 		    (ddi_intr_handler_t *)px_msiq_intr, (caddr_t)ino_p, NULL);
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 		ret = i_ddi_add_ivintr(hdlp);
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 		/*
11797c478bd9Sstevel@tonic-gate 		 * Restore original interrupt handler
11807c478bd9Sstevel@tonic-gate 		 * and arguments in interrupt handle.
11817c478bd9Sstevel@tonic-gate 		 */
11827c478bd9Sstevel@tonic-gate 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
11837c478bd9Sstevel@tonic-gate 		    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 		if (ret != DDI_SUCCESS)
11867c478bd9Sstevel@tonic-gate 			goto fail2;
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 		/* Save the pil for this ino */
11897c478bd9Sstevel@tonic-gate 		ino_p->ino_pil = hdlp->ih_pri;
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 		/* Enable MSIQ */
11927c478bd9Sstevel@tonic-gate 		px_lib_msiq_setstate(dip, *msiq_id_p, PCI_MSIQ_STATE_IDLE);
11937c478bd9Sstevel@tonic-gate 		px_lib_msiq_setvalid(dip, *msiq_id_p, PCI_MSIQ_VALID);
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 		/* select cpu, saving it for sharing and removal */
11967c478bd9Sstevel@tonic-gate 		ino_p->ino_cpuid = intr_dist_cpuid();
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 		/* Enable interrupt */
11997c478bd9Sstevel@tonic-gate 		px_ib_intr_enable(px_p, ino_p->ino_cpuid, ino_p->ino_ino);
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	/* add weight to the cpu that we are already targeting */
12037c478bd9Sstevel@tonic-gate 	weight = px_class_to_intr_weight(rdip);
12047c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_add_device_weight(ino_p->ino_cpuid, rdip, weight);
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	ih_p->ih_ino_p = ino_p;
12076d44af1bSesolom 	px_create_intr_kstats(ih_p);
12087c478bd9Sstevel@tonic-gate 	if (ih_p->ih_ksp)
12097c478bd9Sstevel@tonic-gate 		kstat_install(ih_p->ih_ksp);
12107c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: done! Interrupt 0x%x pil=%x\n",
12137c478bd9Sstevel@tonic-gate 	    ino_p->ino_sysino, hdlp->ih_pri);
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	return (ret);
12167c478bd9Sstevel@tonic-gate fail2:
12177c478bd9Sstevel@tonic-gate 	px_ib_delete_ino(ib_p, ino_p);
12187c478bd9Sstevel@tonic-gate fail1:
12197c478bd9Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
12207c478bd9Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
12237c478bd9Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (px_ih_t));
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: Failed! Interrupt 0x%x pil=%x\n",
12267c478bd9Sstevel@tonic-gate 	    ino_p->ino_sysino, hdlp->ih_pri);
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	return (ret);
12297c478bd9Sstevel@tonic-gate }
12307c478bd9Sstevel@tonic-gate 
1231a195726fSgovinda /*
1232a195726fSgovinda  * px_rem_msiq_intr:
1233a195726fSgovinda  *
1234a195726fSgovinda  * This function is called to unregister MSI/Xs and PCIe message interrupts.
1235a195726fSgovinda  */
12367c478bd9Sstevel@tonic-gate int
12377c478bd9Sstevel@tonic-gate px_rem_msiq_intr(dev_info_t *dip, dev_info_t *rdip,
12387c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, msiq_rec_type_t rec_type,
12397c478bd9Sstevel@tonic-gate     msgcode_t msg_code, msiqid_t msiq_id)
12407c478bd9Sstevel@tonic-gate {
12417c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
12427c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
12437c478bd9Sstevel@tonic-gate 	devino_t	ino = px_msiqid_to_devino(px_p, msiq_id);
12447c478bd9Sstevel@tonic-gate 	cpuid_t		curr_cpu;
12457c478bd9Sstevel@tonic-gate 	px_ib_ino_info_t *ino_p;
12467c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
12477c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_rem_msiq_intr: rdip=%s%d msiq_id=%x ino=%x\n",
12507c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), msiq_id, ino);
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	ino_p = px_ib_locate_ino(ib_p, ino);
12557c478bd9Sstevel@tonic-gate 	ih_p = px_ib_ino_locate_intr(ino_p, rdip, hdlp->ih_inum,
12567c478bd9Sstevel@tonic-gate 	    rec_type, msg_code);
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	/* Get the current cpu */
12597c478bd9Sstevel@tonic-gate 	if ((ret = px_lib_intr_gettarget(px_p->px_dip, ino_p->ino_sysino,
12607c478bd9Sstevel@tonic-gate 	    &curr_cpu)) != DDI_SUCCESS)
12617c478bd9Sstevel@tonic-gate 		goto fail;
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	if ((ret = px_ib_ino_rem_intr(px_p, ino_p, ih_p)) != DDI_SUCCESS)
12647c478bd9Sstevel@tonic-gate 		goto fail;
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	if (ino_p->ino_ih_size == 0) {
12697c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_intr_setstate(px_p->px_dip, ino_p->ino_sysino,
12707c478bd9Sstevel@tonic-gate 		    INTR_DELIVERED_STATE)) != DDI_SUCCESS)
12717c478bd9Sstevel@tonic-gate 			goto fail;
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 		px_lib_msiq_setvalid(dip, px_devino_to_msiqid(px_p, ino),
12747c478bd9Sstevel@tonic-gate 		    PCI_MSIQ_INVALID);
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
12777c478bd9Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 		px_ib_delete_ino(ib_p, ino_p);
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 		(void) px_msiq_free(px_p, msiq_id);
12827c478bd9Sstevel@tonic-gate 		kmem_free(ino_p, sizeof (px_ib_ino_info_t));
12837c478bd9Sstevel@tonic-gate 	} else {
12847c478bd9Sstevel@tonic-gate 		/* Re-enable interrupt only if mapping regsiter still shared */
1285a195726fSgovinda 		PX_INTR_ENABLE(px_p->px_dip, ino_p->ino_sysino, curr_cpu);
12867c478bd9Sstevel@tonic-gate 	}
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate fail:
12897c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
12907c478bd9Sstevel@tonic-gate 	return (ret);
12917c478bd9Sstevel@tonic-gate }
1292