xref: /illumos-gate/usr/src/uts/sun4/io/px/px_intr.c (revision a54f81fb)
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;
23807f14c08Sgovinda 	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 		 */
30107f14c08Sgovinda 		for (i = 0, ih_p = ino_p->ino_ih_start;
30207f14c08Sgovinda 		    ih_p && (i < ino_p->ino_ih_size) &&
30307f14c08Sgovinda 		    ((ih_p->ih_msg_code != msg_code) ||
30407f14c08Sgovinda 		    (ih_p->ih_rec_type != msiq_rec_p->msiq_rec_type));
30507f14c08Sgovinda 		    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:
582*a54f81fbSanish 		*(int *)result = i_ddi_get_intx_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 */
61107f14c08Sgovinda 	if ((hdlp->ih_cap & DDI_INTR_FLAG_MSI64) && msi_state_p->msi_addr64) {
6129c75c6bfSgovinda 		msiq_rec_type = MSI64_REC;
6139c75c6bfSgovinda 		msi_type = MSI64_TYPE;
61407f14c08Sgovinda 		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,
63520036fe5Segillett 		    hdlp->ih_scratch1, (uintptr_t)hdlp->ih_scratch2, &msi_num,
63620036fe5Segillett 		    (int *)result)) != DDI_SUCCESS) {
63720036fe5Segillett 			DBG(DBG_INTROPS, dip, "px_msix_ops: allocation "
63820036fe5Segillett 			    "failed, rdip 0x%p type 0x%d inum 0x%x "
63920036fe5Segillett 			    "count 0x%x\n", rdip, hdlp->ih_type, hdlp->ih_inum,
64020036fe5Segillett 			    hdlp->ih_scratch1);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 			return (ret);
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 
64520036fe5Segillett 		if ((hdlp->ih_type == DDI_INTR_TYPE_MSIX) &&
64620036fe5Segillett 		    (i_ddi_get_msix(rdip) == NULL)) {
64720036fe5Segillett 			ddi_intr_msix_t		*msix_p;
64820036fe5Segillett 
64920036fe5Segillett 			if (msix_p = pci_msix_init(rdip)) {
65020036fe5Segillett 				i_ddi_set_msix(rdip, msix_p);
65120036fe5Segillett 				break;
65220036fe5Segillett 			}
65320036fe5Segillett 
65420036fe5Segillett 			DBG(DBG_INTROPS, dip, "px_msix_ops: MSI-X allocation "
65520036fe5Segillett 			    "failed, rdip 0x%p inum 0x%x\n", rdip,
65620036fe5Segillett 			    hdlp->ih_inum);
65720036fe5Segillett 
65820036fe5Segillett 			(void) px_msi_free(px_p, rdip, hdlp->ih_inum,
65920036fe5Segillett 			    hdlp->ih_scratch1);
66020036fe5Segillett 
66120036fe5Segillett 			return (DDI_FAILURE);
66220036fe5Segillett 		}
66320036fe5Segillett 
6647c478bd9Sstevel@tonic-gate 		break;
6657c478bd9Sstevel@tonic-gate 	case DDI_INTROP_FREE:
6667c478bd9Sstevel@tonic-gate 		(void) pci_msi_disable_mode(rdip, hdlp->ih_type, hdlp->ih_inum);
6677c478bd9Sstevel@tonic-gate 		(void) pci_msi_unconfigure(rdip, hdlp->ih_type, hdlp->ih_inum);
66820036fe5Segillett 
66920036fe5Segillett 		if (hdlp->ih_type == DDI_INTR_TYPE_MSI)
67020036fe5Segillett 			goto msi_free;
67120036fe5Segillett 
67220036fe5Segillett 		if (hdlp->ih_flags & DDI_INTR_MSIX_DUP)
67320036fe5Segillett 			break;
67420036fe5Segillett 
67520036fe5Segillett 		if (((i_ddi_intr_get_current_nintrs(hdlp->ih_dip) - 1) == 0) &&
67620036fe5Segillett 		    (i_ddi_get_msix(rdip))) {
67720036fe5Segillett 			pci_msix_fini(i_ddi_get_msix(rdip));
67820036fe5Segillett 			i_ddi_set_msix(rdip, NULL);
67920036fe5Segillett 		}
68020036fe5Segillett msi_free:
6817c478bd9Sstevel@tonic-gate 		(void) px_msi_free(px_p, rdip, hdlp->ih_inum,
6827c478bd9Sstevel@tonic-gate 		    hdlp->ih_scratch1);
6837c478bd9Sstevel@tonic-gate 		break;
6847c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
6857c478bd9Sstevel@tonic-gate 		*(int *)result = hdlp->ih_pri ?
6867c478bd9Sstevel@tonic-gate 		    hdlp->ih_pri : px_class_to_pil(rdip);
6877c478bd9Sstevel@tonic-gate 		break;
6887c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETPRI:
6897c478bd9Sstevel@tonic-gate 		break;
6907c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
6917c478bd9Sstevel@tonic-gate 		if ((ret = px_msi_get_msinum(px_p, hdlp->ih_dip,
6927c478bd9Sstevel@tonic-gate 		    hdlp->ih_inum, &msi_num)) != DDI_SUCCESS)
6937c478bd9Sstevel@tonic-gate 			return (ret);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 		if ((ret = px_add_msiq_intr(dip, rdip, hdlp,
6969c75c6bfSgovinda 		    msiq_rec_type, msi_num, &msiq_id)) != DDI_SUCCESS) {
6977c478bd9Sstevel@tonic-gate 			DBG(DBG_INTROPS, dip, "px_msix_ops: Add MSI handler "
6987c478bd9Sstevel@tonic-gate 			    "failed, rdip 0x%p msi 0x%x\n", rdip, msi_num);
6997c478bd9Sstevel@tonic-gate 			return (ret);
7007c478bd9Sstevel@tonic-gate 		}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 		DBG(DBG_INTROPS, dip, "px_msix_ops: msiq used 0x%x\n", msiq_id);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setmsiq(dip, msi_num,
7059c75c6bfSgovinda 		    msiq_id, msi_type)) != DDI_SUCCESS) {
7067c478bd9Sstevel@tonic-gate 			(void) px_rem_msiq_intr(dip, rdip,
7079c75c6bfSgovinda 			    hdlp, msiq_rec_type, msi_num, msiq_id);
7087c478bd9Sstevel@tonic-gate 			return (ret);
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setstate(dip, msi_num,
7127c478bd9Sstevel@tonic-gate 		    PCI_MSI_STATE_IDLE)) != DDI_SUCCESS) {
7137c478bd9Sstevel@tonic-gate 			(void) px_rem_msiq_intr(dip, rdip,
7149c75c6bfSgovinda 			    hdlp, msiq_rec_type, msi_num, msiq_id);
7157c478bd9Sstevel@tonic-gate 			return (ret);
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = msi_num;
7197c478bd9Sstevel@tonic-gate 		break;
7207c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DUPVEC:
72120036fe5Segillett 		DBG(DBG_INTROPS, dip, "px_msix_ops: dupisr - inum: %x, "
72220036fe5Segillett 		    "new_vector: %x\n", hdlp->ih_inum, hdlp->ih_scratch1);
72320036fe5Segillett 
72420036fe5Segillett 		ret = pci_msix_dup(hdlp->ih_dip, hdlp->ih_inum,
72520036fe5Segillett 		    hdlp->ih_scratch1);
7267c478bd9Sstevel@tonic-gate 		break;
7277c478bd9Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
7287c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
7317c478bd9Sstevel@tonic-gate 		    &msiq_id)) != DDI_SUCCESS)
7327c478bd9Sstevel@tonic-gate 			return (ret);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setstate(dip, msi_num,
7359c75c6bfSgovinda 		    PCI_MSI_STATE_IDLE)) != DDI_SUCCESS)
7367c478bd9Sstevel@tonic-gate 			return (ret);
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 		ret = px_rem_msiq_intr(dip, rdip,
7399c75c6bfSgovinda 		    hdlp, msiq_rec_type, msi_num, msiq_id);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = 0;
7427c478bd9Sstevel@tonic-gate 		break;
7437c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ENABLE:
7447c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setvalid(dip, msi_num,
7477c478bd9Sstevel@tonic-gate 		    PCI_MSI_VALID)) != DDI_SUCCESS)
7487c478bd9Sstevel@tonic-gate 			return (ret);
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 		if (pci_is_msi_enabled(rdip, hdlp->ih_type) != DDI_SUCCESS) {
7517c478bd9Sstevel@tonic-gate 			nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 			if ((ret = pci_msi_configure(rdip, hdlp->ih_type,
7549c75c6bfSgovinda 			    nintrs, hdlp->ih_inum, msi_addr,
7557c478bd9Sstevel@tonic-gate 			    msi_num & ~(nintrs - 1))) != DDI_SUCCESS)
7567c478bd9Sstevel@tonic-gate 				return (ret);
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 			if ((ret = pci_msi_enable_mode(rdip, hdlp->ih_type,
7597c478bd9Sstevel@tonic-gate 			    hdlp->ih_inum)) != DDI_SUCCESS)
7607c478bd9Sstevel@tonic-gate 				return (ret);
7617c478bd9Sstevel@tonic-gate 		}
7627c478bd9Sstevel@tonic-gate 
76336fe4a92Segillett 		if ((ret = pci_msi_clr_mask(rdip, hdlp->ih_type,
76436fe4a92Segillett 		    hdlp->ih_inum)) != DDI_SUCCESS)
76536fe4a92Segillett 			return (ret);
76636fe4a92Segillett 
76720036fe5Segillett 		if (hdlp->ih_flags & DDI_INTR_MSIX_DUP)
76820036fe5Segillett 			break;
76920036fe5Segillett 
77036fe4a92Segillett 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
77136fe4a92Segillett 		    &msiq_id)) != DDI_SUCCESS)
77236fe4a92Segillett 			return (ret);
77336fe4a92Segillett 
77436fe4a92Segillett 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
77536fe4a92Segillett 		    px_msiqid_to_devino(px_p, msiq_id), PX_INTR_STATE_ENABLE,
7769c75c6bfSgovinda 		    msiq_rec_type, msi_num);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		break;
7797c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
7807c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 		if ((ret = pci_msi_set_mask(rdip, hdlp->ih_type,
7837c478bd9Sstevel@tonic-gate 		    hdlp->ih_inum)) != DDI_SUCCESS)
7847c478bd9Sstevel@tonic-gate 			return (ret);
7857c478bd9Sstevel@tonic-gate 
78636fe4a92Segillett 		if ((ret = px_lib_msi_setvalid(dip, msi_num,
78736fe4a92Segillett 		    PCI_MSI_INVALID)) != DDI_SUCCESS)
78836fe4a92Segillett 			return (ret);
78936fe4a92Segillett 
79020036fe5Segillett 		if (hdlp->ih_flags & DDI_INTR_MSIX_DUP)
79120036fe5Segillett 			break;
79220036fe5Segillett 
79336fe4a92Segillett 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
79436fe4a92Segillett 		    &msiq_id)) != DDI_SUCCESS)
79536fe4a92Segillett 			return (ret);
79636fe4a92Segillett 
79736fe4a92Segillett 		ret = px_ib_update_intr_state(px_p, rdip,
79836fe4a92Segillett 		    hdlp->ih_inum, px_msiqid_to_devino(px_p, msiq_id),
7999c75c6bfSgovinda 		    PX_INTR_STATE_DISABLE, msiq_rec_type, msi_num);
80036fe4a92Segillett 
8017c478bd9Sstevel@tonic-gate 		break;
8027c478bd9Sstevel@tonic-gate 	case DDI_INTROP_BLOCKENABLE:
8037c478bd9Sstevel@tonic-gate 		nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
8047c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 		if ((ret = pci_msi_configure(rdip, hdlp->ih_type,
8079c75c6bfSgovinda 		    nintrs, hdlp->ih_inum, msi_addr,
8087c478bd9Sstevel@tonic-gate 		    msi_num & ~(nintrs - 1))) != DDI_SUCCESS)
8097c478bd9Sstevel@tonic-gate 			return (ret);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 		for (i = 0; i < nintrs; i++, msi_num++) {
8127c478bd9Sstevel@tonic-gate 			if ((ret = px_lib_msi_setvalid(dip, msi_num,
8137c478bd9Sstevel@tonic-gate 			    PCI_MSI_VALID)) != DDI_SUCCESS)
8147c478bd9Sstevel@tonic-gate 				return (ret);
81536fe4a92Segillett 
81636fe4a92Segillett 			if ((ret = px_lib_msi_getmsiq(dip, msi_num,
81736fe4a92Segillett 			    &msiq_id)) != DDI_SUCCESS)
81836fe4a92Segillett 				return (ret);
81936fe4a92Segillett 
82036fe4a92Segillett 			if ((ret = px_ib_update_intr_state(px_p, rdip,
82136fe4a92Segillett 			    hdlp->ih_inum + i, px_msiqid_to_devino(px_p,
8229c75c6bfSgovinda 			    msiq_id), PX_INTR_STATE_ENABLE, msiq_rec_type,
8239c75c6bfSgovinda 			    msi_num)) != DDI_SUCCESS)
82436fe4a92Segillett 				return (ret);
8257c478bd9Sstevel@tonic-gate 		}
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 		ret = pci_msi_enable_mode(rdip, hdlp->ih_type, hdlp->ih_inum);
8287c478bd9Sstevel@tonic-gate 		break;
8297c478bd9Sstevel@tonic-gate 	case DDI_INTROP_BLOCKDISABLE:
8307c478bd9Sstevel@tonic-gate 		nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
8317c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 		if ((ret = pci_msi_disable_mode(rdip, hdlp->ih_type,
8347c478bd9Sstevel@tonic-gate 		    hdlp->ih_inum)) != DDI_SUCCESS)
8357c478bd9Sstevel@tonic-gate 			return (ret);
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 		for (i = 0; i < nintrs; i++, msi_num++) {
8387c478bd9Sstevel@tonic-gate 			if ((ret = px_lib_msi_setvalid(dip, msi_num,
8397c478bd9Sstevel@tonic-gate 			    PCI_MSI_INVALID)) != DDI_SUCCESS)
8407c478bd9Sstevel@tonic-gate 				return (ret);
84136fe4a92Segillett 
84236fe4a92Segillett 			if ((ret = px_lib_msi_getmsiq(dip, msi_num,
84336fe4a92Segillett 			    &msiq_id)) != DDI_SUCCESS)
84436fe4a92Segillett 				return (ret);
84536fe4a92Segillett 
84636fe4a92Segillett 			if ((ret = px_ib_update_intr_state(px_p, rdip,
84736fe4a92Segillett 			    hdlp->ih_inum + i, px_msiqid_to_devino(px_p,
8489c75c6bfSgovinda 			    msiq_id), PX_INTR_STATE_DISABLE, msiq_rec_type,
84936fe4a92Segillett 			    msi_num)) != DDI_SUCCESS)
85036fe4a92Segillett 				return (ret);
8517c478bd9Sstevel@tonic-gate 		}
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 		break;
8547c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETMASK:
8557c478bd9Sstevel@tonic-gate 		ret = pci_msi_set_mask(rdip, hdlp->ih_type, hdlp->ih_inum);
8567c478bd9Sstevel@tonic-gate 		break;
8577c478bd9Sstevel@tonic-gate 	case DDI_INTROP_CLRMASK:
8587c478bd9Sstevel@tonic-gate 		ret = pci_msi_clr_mask(rdip, hdlp->ih_type, hdlp->ih_inum);
8597c478bd9Sstevel@tonic-gate 		break;
8607c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPENDING:
8617c478bd9Sstevel@tonic-gate 		ret = pci_msi_get_pending(rdip, hdlp->ih_type,
8627c478bd9Sstevel@tonic-gate 		    hdlp->ih_inum, (int *)result);
8637c478bd9Sstevel@tonic-gate 		break;
8647c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
8657c478bd9Sstevel@tonic-gate 		ret = pci_msi_get_nintrs(rdip, hdlp->ih_type, (int *)result);
8667c478bd9Sstevel@tonic-gate 		break;
8677c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NAVAIL:
8687c478bd9Sstevel@tonic-gate 		/* XXX - a new interface may be needed */
8697c478bd9Sstevel@tonic-gate 		ret = pci_msi_get_nintrs(rdip, hdlp->ih_type, (int *)result);
8707c478bd9Sstevel@tonic-gate 		break;
8717c478bd9Sstevel@tonic-gate 	default:
8727c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
8737c478bd9Sstevel@tonic-gate 		break;
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	return (ret);
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate 
8796d44af1bSesolom static struct {
8806d44af1bSesolom 	kstat_named_t pxintr_ks_name;
8816d44af1bSesolom 	kstat_named_t pxintr_ks_type;
8826d44af1bSesolom 	kstat_named_t pxintr_ks_cpu;
8836d44af1bSesolom 	kstat_named_t pxintr_ks_pil;
8846d44af1bSesolom 	kstat_named_t pxintr_ks_time;
8856d44af1bSesolom 	kstat_named_t pxintr_ks_ino;
8866d44af1bSesolom 	kstat_named_t pxintr_ks_cookie;
8876d44af1bSesolom 	kstat_named_t pxintr_ks_devpath;
8886d44af1bSesolom 	kstat_named_t pxintr_ks_buspath;
8896d44af1bSesolom } pxintr_ks_template = {
8906d44af1bSesolom 	{ "name",	KSTAT_DATA_CHAR },
8916d44af1bSesolom 	{ "type",	KSTAT_DATA_CHAR },
8926d44af1bSesolom 	{ "cpu",	KSTAT_DATA_UINT64 },
8936d44af1bSesolom 	{ "pil",	KSTAT_DATA_UINT64 },
8946d44af1bSesolom 	{ "time",	KSTAT_DATA_UINT64 },
8956d44af1bSesolom 	{ "ino",	KSTAT_DATA_UINT64 },
8966d44af1bSesolom 	{ "cookie",	KSTAT_DATA_UINT64 },
8976d44af1bSesolom 	{ "devpath",	KSTAT_DATA_STRING },
8986d44af1bSesolom 	{ "buspath",	KSTAT_DATA_STRING },
8996d44af1bSesolom };
9006d44af1bSesolom 
9016d44af1bSesolom static uint32_t pxintr_ks_instance;
902d48713b8Sesolom static char ih_devpath[MAXPATHLEN];
903d48713b8Sesolom static char ih_buspath[MAXPATHLEN];
9046d44af1bSesolom kmutex_t pxintr_ks_template_lock;
9056d44af1bSesolom 
9066d44af1bSesolom int
9076d44af1bSesolom px_ks_update(kstat_t *ksp, int rw)
9086d44af1bSesolom {
9096d44af1bSesolom 	px_ih_t *ih_p = ksp->ks_private;
9106d44af1bSesolom 	int maxlen = sizeof (pxintr_ks_template.pxintr_ks_name.value.c);
9116d44af1bSesolom 	px_ib_t *ib_p = ih_p->ih_ino_p->ino_ib_p;
9126d44af1bSesolom 	px_t *px_p = ib_p->ib_px_p;
9136d44af1bSesolom 	devino_t ino;
9146d44af1bSesolom 	sysino_t sysino;
9156d44af1bSesolom 
9166d44af1bSesolom 	ino = ih_p->ih_ino_p->ino_ino;
9176d44af1bSesolom 	(void) px_lib_intr_devino_to_sysino(px_p->px_dip, ino, &sysino);
9186d44af1bSesolom 
9196d44af1bSesolom 	(void) snprintf(pxintr_ks_template.pxintr_ks_name.value.c, maxlen,
9206d44af1bSesolom 	    "%s%d", ddi_driver_name(ih_p->ih_dip),
9216d44af1bSesolom 	    ddi_get_instance(ih_p->ih_dip));
9226d44af1bSesolom 
9236d44af1bSesolom 	(void) ddi_pathname(ih_p->ih_dip, ih_devpath);
9246d44af1bSesolom 	(void) ddi_pathname(px_p->px_dip, ih_buspath);
9256d44af1bSesolom 	kstat_named_setstr(&pxintr_ks_template.pxintr_ks_devpath, ih_devpath);
9266d44af1bSesolom 	kstat_named_setstr(&pxintr_ks_template.pxintr_ks_buspath, ih_buspath);
9276d44af1bSesolom 
928e1d9f4e6Sschwartz 	if (ih_p->ih_intr_state == PX_INTR_STATE_ENABLE) {
929e1d9f4e6Sschwartz 
930e1d9f4e6Sschwartz 		(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
931e1d9f4e6Sschwartz 		    (ih_p->ih_rec_type == 0) ? "fixed" : "msi");
932e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_cpu.value.ui64 =
933e1d9f4e6Sschwartz 		    ih_p->ih_ino_p->ino_cpuid;
934e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_pil.value.ui64 =
935e1d9f4e6Sschwartz 		    ih_p->ih_ino_p->ino_pil;
936e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_time.value.ui64 = ih_p->ih_nsec +
937e1d9f4e6Sschwartz 		    (uint64_t)tick2ns((hrtime_t)ih_p->ih_ticks,
938e1d9f4e6Sschwartz 			ih_p->ih_ino_p->ino_cpuid);
939e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_ino.value.ui64 = ino;
940e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_cookie.value.ui64 = sysino;
941e1d9f4e6Sschwartz 	} else {
942e1d9f4e6Sschwartz 		(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
943e1d9f4e6Sschwartz 		    "disabled");
944e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_cpu.value.ui64 = 0;
945e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_pil.value.ui64 = 0;
946e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_time.value.ui64 = 0;
947e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_ino.value.ui64 = 0;
948e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_cookie.value.ui64 = 0;
949e1d9f4e6Sschwartz 	}
9506d44af1bSesolom 	return (0);
9516d44af1bSesolom }
9526d44af1bSesolom 
9536d44af1bSesolom void
9546d44af1bSesolom px_create_intr_kstats(px_ih_t *ih_p)
9556d44af1bSesolom {
9566d44af1bSesolom 	msiq_rec_type_t rec_type = ih_p->ih_rec_type;
9576d44af1bSesolom 
9586d44af1bSesolom 	ASSERT(ih_p->ih_ksp == NULL);
9596d44af1bSesolom 
9606d44af1bSesolom 	/*
9616d44af1bSesolom 	 * Create pci_intrs::: kstats for all ih types except messages,
9626d44af1bSesolom 	 * which represent unusual conditions and don't need to be tracked.
9636d44af1bSesolom 	 */
9646d44af1bSesolom 	if (rec_type == 0 || rec_type == MSI32_REC || rec_type == MSI64_REC) {
9656d44af1bSesolom 		ih_p->ih_ksp = kstat_create("pci_intrs",
9666d44af1bSesolom 		    atomic_inc_32_nv(&pxintr_ks_instance), "config",
9676d44af1bSesolom 		    "interrupts", KSTAT_TYPE_NAMED,
9686d44af1bSesolom 		    sizeof (pxintr_ks_template) / sizeof (kstat_named_t),
9696d44af1bSesolom 		    KSTAT_FLAG_VIRTUAL);
9706d44af1bSesolom 	}
9716d44af1bSesolom 	if (ih_p->ih_ksp != NULL) {
9726d44af1bSesolom 		ih_p->ih_ksp->ks_data_size += MAXPATHLEN * 2;
9736d44af1bSesolom 		ih_p->ih_ksp->ks_lock = &pxintr_ks_template_lock;
9746d44af1bSesolom 		ih_p->ih_ksp->ks_data = &pxintr_ks_template;
9756d44af1bSesolom 		ih_p->ih_ksp->ks_private = ih_p;
9766d44af1bSesolom 		ih_p->ih_ksp->ks_update = px_ks_update;
9776d44af1bSesolom 	}
9786d44af1bSesolom }
9796d44af1bSesolom 
980a195726fSgovinda /*
981a195726fSgovinda  * px_add_intx_intr:
982a195726fSgovinda  *
983a195726fSgovinda  * This function is called to register INTx and legacy hardware
984a195726fSgovinda  * interrupt pins interrupts.
985a195726fSgovinda  */
9867c478bd9Sstevel@tonic-gate int
9877c478bd9Sstevel@tonic-gate px_add_intx_intr(dev_info_t *dip, dev_info_t *rdip,
9887c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
9897c478bd9Sstevel@tonic-gate {
9907c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
9917c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
9927c478bd9Sstevel@tonic-gate 	devino_t	ino;
9937c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
9947c478bd9Sstevel@tonic-gate 	px_ib_ino_info_t *ino_p;
9957c478bd9Sstevel@tonic-gate 	int32_t		weight;
9967c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	ino = hdlp->ih_vector;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: rdip=%s%d ino=%x "
10017c478bd9Sstevel@tonic-gate 	    "handler=%x arg1=%x arg2=%x\n", ddi_driver_name(rdip),
10027c478bd9Sstevel@tonic-gate 	    ddi_get_instance(rdip), ino, hdlp->ih_cb_func,
10037c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	ih_p = px_ib_alloc_ih(rdip, hdlp->ih_inum,
10067c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, 0, 0);
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	if (ino_p = px_ib_locate_ino(ib_p, ino)) {	/* sharing ino */
10117c478bd9Sstevel@tonic-gate 		uint32_t intr_index = hdlp->ih_inum;
10127c478bd9Sstevel@tonic-gate 		if (px_ib_ino_locate_intr(ino_p, rdip, intr_index, 0, 0)) {
10137c478bd9Sstevel@tonic-gate 			DBG(DBG_A_INTX, dip, "px_add_intx_intr: "
10147c478bd9Sstevel@tonic-gate 			    "dup intr #%d\n", intr_index);
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 			ret = DDI_FAILURE;
10177c478bd9Sstevel@tonic-gate 			goto fail1;
10187c478bd9Sstevel@tonic-gate 		}
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 		/* Save mondo value in hdlp */
10217c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 		if ((ret = px_ib_ino_add_intr(px_p, ino_p, ih_p))
10247c478bd9Sstevel@tonic-gate 		    != DDI_SUCCESS)
10257c478bd9Sstevel@tonic-gate 			goto fail1;
10267c478bd9Sstevel@tonic-gate 	} else {
10277c478bd9Sstevel@tonic-gate 		ino_p = px_ib_new_ino(ib_p, ino, ih_p);
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 		if (hdlp->ih_pri == 0)
10307c478bd9Sstevel@tonic-gate 			hdlp->ih_pri = px_class_to_pil(rdip);
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 		/* Save mondo value in hdlp */
10337c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 		DBG(DBG_A_INTX, dip, "px_add_intx_intr: pil=0x%x mondo=0x%x\n",
10367c478bd9Sstevel@tonic-gate 		    hdlp->ih_pri, hdlp->ih_vector);
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
10397c478bd9Sstevel@tonic-gate 		    (ddi_intr_handler_t *)px_intx_intr, (caddr_t)ino_p, NULL);
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 		ret = i_ddi_add_ivintr(hdlp);
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 		/*
10447c478bd9Sstevel@tonic-gate 		 * Restore original interrupt handler
10457c478bd9Sstevel@tonic-gate 		 * and arguments in interrupt handle.
10467c478bd9Sstevel@tonic-gate 		 */
10477c478bd9Sstevel@tonic-gate 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
10487c478bd9Sstevel@tonic-gate 		    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 		if (ret != DDI_SUCCESS)
10517c478bd9Sstevel@tonic-gate 			goto fail2;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 		/* Save the pil for this ino */
10547c478bd9Sstevel@tonic-gate 		ino_p->ino_pil = hdlp->ih_pri;
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 		/* select cpu, saving it for sharing and removal */
10577c478bd9Sstevel@tonic-gate 		ino_p->ino_cpuid = intr_dist_cpuid();
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 		/* Enable interrupt */
10607c478bd9Sstevel@tonic-gate 		px_ib_intr_enable(px_p, ino_p->ino_cpuid, ino);
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	/* add weight to the cpu that we are already targeting */
10647c478bd9Sstevel@tonic-gate 	weight = px_class_to_intr_weight(rdip);
10657c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_add_device_weight(ino_p->ino_cpuid, rdip, weight);
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	ih_p->ih_ino_p = ino_p;
10686d44af1bSesolom 	px_create_intr_kstats(ih_p);
10697c478bd9Sstevel@tonic-gate 	if (ih_p->ih_ksp)
10707c478bd9Sstevel@tonic-gate 		kstat_install(ih_p->ih_ksp);
10717c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: done! Interrupt 0x%x pil=%x\n",
10747c478bd9Sstevel@tonic-gate 	    ino_p->ino_sysino, hdlp->ih_pri);
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	return (ret);
10777c478bd9Sstevel@tonic-gate fail2:
10787c478bd9Sstevel@tonic-gate 	px_ib_delete_ino(ib_p, ino_p);
10797c478bd9Sstevel@tonic-gate fail1:
10807c478bd9Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
10817c478bd9Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
10847c478bd9Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (px_ih_t));
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: Failed! Interrupt 0x%x "
10877c478bd9Sstevel@tonic-gate 	    "pil=%x\n", ino_p->ino_sysino, hdlp->ih_pri);
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	return (ret);
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate 
1092a195726fSgovinda /*
1093a195726fSgovinda  * px_rem_intx_intr:
1094a195726fSgovinda  *
1095a195726fSgovinda  * This function is called to unregister INTx and legacy hardware
1096a195726fSgovinda  * interrupt pins interrupts.
1097a195726fSgovinda  */
10987c478bd9Sstevel@tonic-gate int
10997c478bd9Sstevel@tonic-gate px_rem_intx_intr(dev_info_t *dip, dev_info_t *rdip,
11007c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
11017c478bd9Sstevel@tonic-gate {
11027c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
11037c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
11047c478bd9Sstevel@tonic-gate 	devino_t	ino;
11057c478bd9Sstevel@tonic-gate 	cpuid_t		curr_cpu;
11067c478bd9Sstevel@tonic-gate 	px_ib_ino_info_t	*ino_p;
11077c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
11087c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	ino = hdlp->ih_vector;
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	DBG(DBG_R_INTX, dip, "px_rem_intx_intr: rdip=%s%d ino=%x\n",
11137c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	ino_p = px_ib_locate_ino(ib_p, ino);
11187c478bd9Sstevel@tonic-gate 	ih_p = px_ib_ino_locate_intr(ino_p, rdip, hdlp->ih_inum, 0, 0);
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	/* Get the current cpu */
11217c478bd9Sstevel@tonic-gate 	if ((ret = px_lib_intr_gettarget(px_p->px_dip, ino_p->ino_sysino,
11227c478bd9Sstevel@tonic-gate 	    &curr_cpu)) != DDI_SUCCESS)
11237c478bd9Sstevel@tonic-gate 		goto fail;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	if ((ret = px_ib_ino_rem_intr(px_p, ino_p, ih_p)) != DDI_SUCCESS)
11267c478bd9Sstevel@tonic-gate 		goto fail;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	if (ino_p->ino_ih_size == 0) {
11317c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_intr_setstate(px_p->px_dip, ino_p->ino_sysino,
11327c478bd9Sstevel@tonic-gate 		    INTR_DELIVERED_STATE)) != DDI_SUCCESS)
11337c478bd9Sstevel@tonic-gate 			goto fail;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
11367c478bd9Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 		px_ib_delete_ino(ib_p, ino_p);
11397c478bd9Sstevel@tonic-gate 		kmem_free(ino_p, sizeof (px_ib_ino_info_t));
11407c478bd9Sstevel@tonic-gate 	} else {
11417c478bd9Sstevel@tonic-gate 		/* Re-enable interrupt only if mapping regsiter still shared */
1142a195726fSgovinda 		PX_INTR_ENABLE(px_p->px_dip, ino_p->ino_sysino, curr_cpu);
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate fail:
11467c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
11477c478bd9Sstevel@tonic-gate 	return (ret);
11487c478bd9Sstevel@tonic-gate }
11497c478bd9Sstevel@tonic-gate 
1150a195726fSgovinda /*
1151a195726fSgovinda  * px_add_msiq_intr:
1152a195726fSgovinda  *
1153a195726fSgovinda  * This function is called to register MSI/Xs and PCIe message interrupts.
1154a195726fSgovinda  */
11557c478bd9Sstevel@tonic-gate int
11567c478bd9Sstevel@tonic-gate px_add_msiq_intr(dev_info_t *dip, dev_info_t *rdip,
11577c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, msiq_rec_type_t rec_type,
11587c478bd9Sstevel@tonic-gate     msgcode_t msg_code, msiqid_t *msiq_id_p)
11597c478bd9Sstevel@tonic-gate {
11607c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
11617c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
11627c478bd9Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &ib_p->ib_msiq_state;
11637c478bd9Sstevel@tonic-gate 	devino_t	ino;
11647c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
11657c478bd9Sstevel@tonic-gate 	px_ib_ino_info_t	*ino_p;
11667c478bd9Sstevel@tonic-gate 	int32_t		weight;
11677c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: rdip=%s%d handler=%x "
11707c478bd9Sstevel@tonic-gate 	    "arg1=%x arg2=%x\n", ddi_driver_name(rdip), ddi_get_instance(rdip),
11717c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	if ((ret = px_msiq_alloc(px_p, rec_type, msiq_id_p)) != DDI_SUCCESS) {
11747c478bd9Sstevel@tonic-gate 		DBG(DBG_MSIQ, dip, "px_add_msiq_intr: "
11757c478bd9Sstevel@tonic-gate 		    "msiq allocation failed\n");
11767c478bd9Sstevel@tonic-gate 		return (ret);
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	ino = px_msiqid_to_devino(px_p, *msiq_id_p);
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	ih_p = px_ib_alloc_ih(rdip, hdlp->ih_inum, hdlp->ih_cb_func,
11827c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, rec_type, msg_code);
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	if (ino_p = px_ib_locate_ino(ib_p, ino)) {	/* sharing ino */
11877c478bd9Sstevel@tonic-gate 		uint32_t intr_index = hdlp->ih_inum;
11887c478bd9Sstevel@tonic-gate 		if (px_ib_ino_locate_intr(ino_p, rdip,
11897c478bd9Sstevel@tonic-gate 		    intr_index, rec_type, msg_code)) {
11907c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ, dip, "px_add_msiq_intr: "
11917c478bd9Sstevel@tonic-gate 			    "dup intr #%d\n", intr_index);
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 			ret = DDI_FAILURE;
11947c478bd9Sstevel@tonic-gate 			goto fail1;
11957c478bd9Sstevel@tonic-gate 		}
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 		if ((ret = px_ib_ino_add_intr(px_p, ino_p, ih_p))
11987c478bd9Sstevel@tonic-gate 		    != DDI_SUCCESS)
11997c478bd9Sstevel@tonic-gate 			goto fail1;
12007c478bd9Sstevel@tonic-gate 	} else {
12017c478bd9Sstevel@tonic-gate 		ino_p = px_ib_new_ino(ib_p, ino, ih_p);
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 		ino_p->ino_msiq_p = msiq_state_p->msiq_p +
12047c478bd9Sstevel@tonic-gate 		    (*msiq_id_p - msiq_state_p->msiq_1st_msiq_id);
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 		if (hdlp->ih_pri == 0)
12077c478bd9Sstevel@tonic-gate 			hdlp->ih_pri = px_class_to_pil(rdip);
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 		/* Save mondo value in hdlp */
12107c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 		DBG(DBG_MSIQ, dip, "px_add_msiq_intr: pil=0x%x mondo=0x%x\n",
12137c478bd9Sstevel@tonic-gate 		    hdlp->ih_pri, hdlp->ih_vector);
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
12167c478bd9Sstevel@tonic-gate 		    (ddi_intr_handler_t *)px_msiq_intr, (caddr_t)ino_p, NULL);
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 		ret = i_ddi_add_ivintr(hdlp);
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 		/*
12217c478bd9Sstevel@tonic-gate 		 * Restore original interrupt handler
12227c478bd9Sstevel@tonic-gate 		 * and arguments in interrupt handle.
12237c478bd9Sstevel@tonic-gate 		 */
12247c478bd9Sstevel@tonic-gate 		DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
12257c478bd9Sstevel@tonic-gate 		    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 		if (ret != DDI_SUCCESS)
12287c478bd9Sstevel@tonic-gate 			goto fail2;
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 		/* Save the pil for this ino */
12317c478bd9Sstevel@tonic-gate 		ino_p->ino_pil = hdlp->ih_pri;
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 		/* Enable MSIQ */
12347c478bd9Sstevel@tonic-gate 		px_lib_msiq_setstate(dip, *msiq_id_p, PCI_MSIQ_STATE_IDLE);
12357c478bd9Sstevel@tonic-gate 		px_lib_msiq_setvalid(dip, *msiq_id_p, PCI_MSIQ_VALID);
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 		/* select cpu, saving it for sharing and removal */
12387c478bd9Sstevel@tonic-gate 		ino_p->ino_cpuid = intr_dist_cpuid();
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 		/* Enable interrupt */
12417c478bd9Sstevel@tonic-gate 		px_ib_intr_enable(px_p, ino_p->ino_cpuid, ino_p->ino_ino);
12427c478bd9Sstevel@tonic-gate 	}
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	/* add weight to the cpu that we are already targeting */
12457c478bd9Sstevel@tonic-gate 	weight = px_class_to_intr_weight(rdip);
12467c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_add_device_weight(ino_p->ino_cpuid, rdip, weight);
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	ih_p->ih_ino_p = ino_p;
12496d44af1bSesolom 	px_create_intr_kstats(ih_p);
12507c478bd9Sstevel@tonic-gate 	if (ih_p->ih_ksp)
12517c478bd9Sstevel@tonic-gate 		kstat_install(ih_p->ih_ksp);
12527c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: done! Interrupt 0x%x pil=%x\n",
12557c478bd9Sstevel@tonic-gate 	    ino_p->ino_sysino, hdlp->ih_pri);
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	return (ret);
12587c478bd9Sstevel@tonic-gate fail2:
12597c478bd9Sstevel@tonic-gate 	px_ib_delete_ino(ib_p, ino_p);
12607c478bd9Sstevel@tonic-gate fail1:
12617c478bd9Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
12627c478bd9Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
12657c478bd9Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (px_ih_t));
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: Failed! Interrupt 0x%x pil=%x\n",
12687c478bd9Sstevel@tonic-gate 	    ino_p->ino_sysino, hdlp->ih_pri);
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	return (ret);
12717c478bd9Sstevel@tonic-gate }
12727c478bd9Sstevel@tonic-gate 
1273a195726fSgovinda /*
1274a195726fSgovinda  * px_rem_msiq_intr:
1275a195726fSgovinda  *
1276a195726fSgovinda  * This function is called to unregister MSI/Xs and PCIe message interrupts.
1277a195726fSgovinda  */
12787c478bd9Sstevel@tonic-gate int
12797c478bd9Sstevel@tonic-gate px_rem_msiq_intr(dev_info_t *dip, dev_info_t *rdip,
12807c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, msiq_rec_type_t rec_type,
12817c478bd9Sstevel@tonic-gate     msgcode_t msg_code, msiqid_t msiq_id)
12827c478bd9Sstevel@tonic-gate {
12837c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
12847c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
12857c478bd9Sstevel@tonic-gate 	devino_t	ino = px_msiqid_to_devino(px_p, msiq_id);
12867c478bd9Sstevel@tonic-gate 	cpuid_t		curr_cpu;
12877c478bd9Sstevel@tonic-gate 	px_ib_ino_info_t *ino_p;
12887c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
12897c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_rem_msiq_intr: rdip=%s%d msiq_id=%x ino=%x\n",
12927c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), msiq_id, ino);
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	ino_p = px_ib_locate_ino(ib_p, ino);
12977c478bd9Sstevel@tonic-gate 	ih_p = px_ib_ino_locate_intr(ino_p, rdip, hdlp->ih_inum,
12987c478bd9Sstevel@tonic-gate 	    rec_type, msg_code);
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	/* Get the current cpu */
13017c478bd9Sstevel@tonic-gate 	if ((ret = px_lib_intr_gettarget(px_p->px_dip, ino_p->ino_sysino,
13027c478bd9Sstevel@tonic-gate 	    &curr_cpu)) != DDI_SUCCESS)
13037c478bd9Sstevel@tonic-gate 		goto fail;
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	if ((ret = px_ib_ino_rem_intr(px_p, ino_p, ih_p)) != DDI_SUCCESS)
13067c478bd9Sstevel@tonic-gate 		goto fail;
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	if (ino_p->ino_ih_size == 0) {
13117c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_intr_setstate(px_p->px_dip, ino_p->ino_sysino,
13127c478bd9Sstevel@tonic-gate 		    INTR_DELIVERED_STATE)) != DDI_SUCCESS)
13137c478bd9Sstevel@tonic-gate 			goto fail;
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 		px_lib_msiq_setvalid(dip, px_devino_to_msiqid(px_p, ino),
13167c478bd9Sstevel@tonic-gate 		    PCI_MSIQ_INVALID);
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
13197c478bd9Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 		px_ib_delete_ino(ib_p, ino_p);
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 		(void) px_msiq_free(px_p, msiq_id);
13247c478bd9Sstevel@tonic-gate 		kmem_free(ino_p, sizeof (px_ib_ino_info_t));
13257c478bd9Sstevel@tonic-gate 	} else {
13267c478bd9Sstevel@tonic-gate 		/* Re-enable interrupt only if mapping regsiter still shared */
1327a195726fSgovinda 		PX_INTR_ENABLE(px_p->px_dip, ino_p->ino_sysino, curr_cpu);
13287c478bd9Sstevel@tonic-gate 	}
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate fail:
13317c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
13327c478bd9Sstevel@tonic-gate 	return (ret);
13337c478bd9Sstevel@tonic-gate }
1334