xref: /onnv-gate/usr/src/uts/sun4/io/px/px_intr.c (revision 10053:79ff8cfc9153)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51542Sjohnny  * Common Development and Distribution License (the "License").
61542Sjohnny  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
228535Sevan.yan@sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * PX nexus interrupt handling:
280Sstevel@tonic-gate  *	PX device interrupt handler wrapper
290Sstevel@tonic-gate  *	PIL lookup routine
300Sstevel@tonic-gate  *	PX device interrupt related initchild code
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/async.h>
360Sstevel@tonic-gate #include <sys/spl.h>
370Sstevel@tonic-gate #include <sys/sunddi.h>
3827Sjchu #include <sys/fm/protocol.h>
3927Sjchu #include <sys/fm/util.h>
400Sstevel@tonic-gate #include <sys/machsystm.h>	/* e_ddi_nodeid_to_dip() */
410Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
420Sstevel@tonic-gate #include <sys/sdt.h>
430Sstevel@tonic-gate #include <sys/atomic.h>
440Sstevel@tonic-gate #include "px_obj.h"
4527Sjchu #include <sys/ontrap.h>
4627Sjchu #include <sys/membar.h>
4766Sesolom #include <sys/clock.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * interrupt jabber:
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * When an interrupt line is jabbering, every time the state machine for the
530Sstevel@tonic-gate  * associated ino is idled, a new mondo will be sent and the ino will go into
540Sstevel@tonic-gate  * the pending state again. The mondo will cause a new call to
550Sstevel@tonic-gate  * px_intr_wrapper() which normally idles the ino's state machine which would
560Sstevel@tonic-gate  * precipitate another trip round the loop.
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  * The loop can be broken by preventing the ino's state machine from being
590Sstevel@tonic-gate  * idled when an interrupt line is jabbering. See the comment at the
600Sstevel@tonic-gate  * beginning of px_intr_wrapper() explaining how the 'interrupt jabber
610Sstevel@tonic-gate  * protection' code does this.
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*LINTLIBRARY*/
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * If the unclaimed interrupt count has reached the limit set by
680Sstevel@tonic-gate  * pci_unclaimed_intr_max within the time limit, then all interrupts
690Sstevel@tonic-gate  * on this ino is blocked by not idling the interrupt state machine.
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate static int
722973Sgovinda px_spurintr(px_ino_pil_t *ipil_p)
730Sstevel@tonic-gate {
742973Sgovinda 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
752973Sgovinda 	px_ih_t		*ih_p = ipil_p->ipil_ih_start;
762973Sgovinda 	px_t		*px_p = ino_p->ino_ib_p->ib_px_p;
772973Sgovinda 	char		*err_fmt_str;
782973Sgovinda 	boolean_t	blocked = B_FALSE;
792973Sgovinda 	int		i;
800Sstevel@tonic-gate 
812973Sgovinda 	if (ino_p->ino_unclaimed_intrs > px_unclaimed_intr_max)
820Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
830Sstevel@tonic-gate 
842973Sgovinda 	if (!ino_p->ino_unclaimed_intrs)
850Sstevel@tonic-gate 		ino_p->ino_spurintr_begin = ddi_get_lbolt();
860Sstevel@tonic-gate 
872973Sgovinda 	ino_p->ino_unclaimed_intrs++;
880Sstevel@tonic-gate 
892973Sgovinda 	if (ino_p->ino_unclaimed_intrs <= px_unclaimed_intr_max)
900Sstevel@tonic-gate 		goto clear;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	if (drv_hztousec(ddi_get_lbolt() - ino_p->ino_spurintr_begin)
930Sstevel@tonic-gate 	    > px_spurintr_duration) {
942973Sgovinda 		ino_p->ino_unclaimed_intrs = 0;
950Sstevel@tonic-gate 		goto clear;
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 	err_fmt_str = "%s%d: ino 0x%x blocked";
982973Sgovinda 	blocked = B_TRUE;
990Sstevel@tonic-gate 	goto warn;
1000Sstevel@tonic-gate clear:
1010Sstevel@tonic-gate 	err_fmt_str = "!%s%d: spurious interrupt from ino 0x%x";
1020Sstevel@tonic-gate warn:
1030Sstevel@tonic-gate 	cmn_err(CE_WARN, err_fmt_str, NAMEINST(px_p->px_dip), ino_p->ino_ino);
1042973Sgovinda 	for (i = 0; i < ipil_p->ipil_ih_size; i++, ih_p = ih_p->ih_next)
1050Sstevel@tonic-gate 		cmn_err(CE_CONT, "!%s-%d#%x ", NAMEINST(ih_p->ih_dip),
1060Sstevel@tonic-gate 		    ih_p->ih_inum);
1070Sstevel@tonic-gate 	cmn_err(CE_CONT, "!\n");
1082973Sgovinda 
1092973Sgovinda 	/* Clear the pending state */
1102973Sgovinda 	if (blocked == B_FALSE) {
1112973Sgovinda 		if (px_lib_intr_setstate(px_p->px_dip, ino_p->ino_sysino,
1122973Sgovinda 		    INTR_IDLE_STATE) != DDI_SUCCESS)
1132973Sgovinda 			return (DDI_INTR_UNCLAIMED);
1142973Sgovinda 	}
1152973Sgovinda 
1160Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate extern uint64_t intr_get_time(void);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /*
122693Sgovinda  * px_intx_intr (INTx or legacy interrupt handler)
1230Sstevel@tonic-gate  *
1240Sstevel@tonic-gate  * This routine is used as wrapper around interrupt handlers installed by child
1250Sstevel@tonic-gate  * device drivers.  This routine invokes the driver interrupt handlers and
1260Sstevel@tonic-gate  * examines the return codes.
1270Sstevel@tonic-gate  *
1280Sstevel@tonic-gate  * There is a count of unclaimed interrupts kept on a per-ino basis. If at
1290Sstevel@tonic-gate  * least one handler claims the interrupt then the counter is halved and the
1300Sstevel@tonic-gate  * interrupt state machine is idled. If no handler claims the interrupt then
1310Sstevel@tonic-gate  * the counter is incremented by one and the state machine is idled.
1320Sstevel@tonic-gate  * If the count ever reaches the limit value set by pci_unclaimed_intr_max
1330Sstevel@tonic-gate  * then the interrupt state machine is not idled thus preventing any further
1340Sstevel@tonic-gate  * interrupts on that ino. The state machine will only be idled again if a
1350Sstevel@tonic-gate  * handler is subsequently added or removed.
1360Sstevel@tonic-gate  *
1370Sstevel@tonic-gate  * return value: DDI_INTR_CLAIMED if any handlers claimed the interrupt,
1380Sstevel@tonic-gate  * DDI_INTR_UNCLAIMED otherwise.
1390Sstevel@tonic-gate  */
1400Sstevel@tonic-gate uint_t
1410Sstevel@tonic-gate px_intx_intr(caddr_t arg)
1420Sstevel@tonic-gate {
1432973Sgovinda 	px_ino_pil_t	*ipil_p = (px_ino_pil_t *)arg;
1442973Sgovinda 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
1450Sstevel@tonic-gate 	px_t		*px_p = ino_p->ino_ib_p->ib_px_p;
1462973Sgovinda 	px_ih_t		*ih_p = ipil_p->ipil_ih_start;
1472973Sgovinda 	ushort_t	pil = ipil_p->ipil_pil;
1482973Sgovinda 	uint_t		result = 0, r = DDI_INTR_UNCLAIMED;
1490Sstevel@tonic-gate 	int		i;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	DBG(DBG_INTX_INTR, px_p->px_dip, "px_intx_intr:"
1520Sstevel@tonic-gate 	    "ino=%x sysino=%llx pil=%x ih_size=%x ih_lst=%x\n",
1532973Sgovinda 	    ino_p->ino_ino, ino_p->ino_sysino, ipil_p->ipil_pil,
1542973Sgovinda 	    ipil_p->ipil_ih_size, ipil_p->ipil_ih_head);
1550Sstevel@tonic-gate 
1562973Sgovinda 	for (i = 0; i < ipil_p->ipil_ih_size; i++, ih_p = ih_p->ih_next) {
1570Sstevel@tonic-gate 		dev_info_t *dip = ih_p->ih_dip;
1580Sstevel@tonic-gate 		uint_t (*handler)() = ih_p->ih_handler;
1590Sstevel@tonic-gate 		caddr_t arg1 = ih_p->ih_handler_arg1;
1600Sstevel@tonic-gate 		caddr_t arg2 = ih_p->ih_handler_arg2;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 		if (ih_p->ih_intr_state == PX_INTR_STATE_DISABLE) {
1630Sstevel@tonic-gate 			DBG(DBG_INTX_INTR, px_p->px_dip,
1640Sstevel@tonic-gate 			    "px_intx_intr: %s%d interrupt %d is disabled\n",
1650Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
1660Sstevel@tonic-gate 			    ino_p->ino_ino);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 			continue;
1690Sstevel@tonic-gate 		}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 		DBG(DBG_INTX_INTR, px_p->px_dip, "px_intx_intr:"
1720Sstevel@tonic-gate 		    "ino=%x handler=%p arg1 =%p arg2 = %p\n",
1730Sstevel@tonic-gate 		    ino_p->ino_ino, handler, arg1, arg2);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
1760Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, caddr_t, arg2);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 		r = (*handler)(arg1, arg2);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 		/*
1810Sstevel@tonic-gate 		 * Account for time used by this interrupt. Protect against
1820Sstevel@tonic-gate 		 * conflicting writes to ih_ticks from ib_intr_dist_all() by
1830Sstevel@tonic-gate 		 * using atomic ops.
1840Sstevel@tonic-gate 		 */
1850Sstevel@tonic-gate 
1862973Sgovinda 		if (pil <= LOCK_LEVEL)
1870Sstevel@tonic-gate 			atomic_add_64(&ih_p->ih_ticks, intr_get_time());
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
1900Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, int, r);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		result += r;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 		if (px_check_all_handlers)
1950Sstevel@tonic-gate 			continue;
1960Sstevel@tonic-gate 		if (result)
1970Sstevel@tonic-gate 			break;
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
2002973Sgovinda 	if (result)
2012973Sgovinda 		ino_p->ino_claimed |= (1 << pil);
2022973Sgovinda 
2032973Sgovinda 	/* Interrupt can only be cleared after all pil levels are handled */
2042973Sgovinda 	if (pil != ino_p->ino_lopil)
2052973Sgovinda 		return (DDI_INTR_CLAIMED);
2060Sstevel@tonic-gate 
2072973Sgovinda 	if (!ino_p->ino_claimed) {
2082973Sgovinda 		if (px_unclaimed_intr_block)
2092973Sgovinda 			return (px_spurintr(ipil_p));
2102973Sgovinda 	}
2112973Sgovinda 
2122973Sgovinda 	ino_p->ino_unclaimed_intrs = 0;
2132973Sgovinda 	ino_p->ino_claimed = 0;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	/* Clear the pending state */
2162973Sgovinda 	if (px_lib_intr_setstate(px_p->px_dip,
2170Sstevel@tonic-gate 	    ino_p->ino_sysino, INTR_IDLE_STATE) != DDI_SUCCESS)
2180Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate /*
224693Sgovinda  * px_msiq_intr (MSI/X or PCIe MSG interrupt handler)
2250Sstevel@tonic-gate  *
2260Sstevel@tonic-gate  * This routine is used as wrapper around interrupt handlers installed by child
2270Sstevel@tonic-gate  * device drivers.  This routine invokes the driver interrupt handlers and
2280Sstevel@tonic-gate  * examines the return codes.
2290Sstevel@tonic-gate  *
2300Sstevel@tonic-gate  * There is a count of unclaimed interrupts kept on a per-ino basis. If at
2310Sstevel@tonic-gate  * least one handler claims the interrupt then the counter is halved and the
2320Sstevel@tonic-gate  * interrupt state machine is idled. If no handler claims the interrupt then
2330Sstevel@tonic-gate  * the counter is incremented by one and the state machine is idled.
2340Sstevel@tonic-gate  * If the count ever reaches the limit value set by pci_unclaimed_intr_max
2350Sstevel@tonic-gate  * then the interrupt state machine is not idled thus preventing any further
2360Sstevel@tonic-gate  * interrupts on that ino. The state machine will only be idled again if a
2370Sstevel@tonic-gate  * handler is subsequently added or removed.
2380Sstevel@tonic-gate  *
2390Sstevel@tonic-gate  * return value: DDI_INTR_CLAIMED if any handlers claimed the interrupt,
2400Sstevel@tonic-gate  * DDI_INTR_UNCLAIMED otherwise.
2410Sstevel@tonic-gate  */
2420Sstevel@tonic-gate uint_t
2430Sstevel@tonic-gate px_msiq_intr(caddr_t arg)
2440Sstevel@tonic-gate {
2452973Sgovinda 	px_ino_pil_t	*ipil_p = (px_ino_pil_t *)arg;
2462973Sgovinda 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
2470Sstevel@tonic-gate 	px_t		*px_p = ino_p->ino_ib_p->ib_px_p;
2480Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
2490Sstevel@tonic-gate 	px_msiq_t	*msiq_p = ino_p->ino_msiq_p;
2500Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
2512973Sgovinda 	ushort_t	pil = ipil_p->ipil_pil;
2520Sstevel@tonic-gate 	msiq_rec_t	msiq_rec, *msiq_rec_p = &msiq_rec;
2532588Segillett 	msiqhead_t	*curr_head_p;
2542588Segillett 	msiqtail_t	curr_tail_index;
2550Sstevel@tonic-gate 	msgcode_t	msg_code;
2560Sstevel@tonic-gate 	px_ih_t		*ih_p;
2572973Sgovinda 	uint_t		ret = DDI_INTR_UNCLAIMED;
2582973Sgovinda 	int		i, j;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: msiq_id =%x ino=%x pil=%x "
2610Sstevel@tonic-gate 	    "ih_size=%x ih_lst=%x\n", msiq_p->msiq_id, ino_p->ino_ino,
2622973Sgovinda 	    ipil_p->ipil_pil, ipil_p->ipil_ih_size, ipil_p->ipil_ih_head);
2632973Sgovinda 
2642973Sgovinda 	/*
2652973Sgovinda 	 * The px_msiq_intr() handles multiple interrupt priorities and it
2662973Sgovinda 	 * will set msiq->msiq_rec2process to the number of MSIQ records to
2672973Sgovinda 	 * process while handling the highest priority interrupt. Subsequent
2682973Sgovinda 	 * lower priority interrupts will just process any unprocessed MSIQ
2692973Sgovinda 	 * records or will just return immediately.
2702973Sgovinda 	 */
2712973Sgovinda 	if (msiq_p->msiq_recs2process == 0) {
2722973Sgovinda 		/* Read current MSIQ tail index */
2732973Sgovinda 		px_lib_msiq_gettail(dip, msiq_p->msiq_id, &curr_tail_index);
2742973Sgovinda 		msiq_p->msiq_new_head_index = msiq_p->msiq_curr_head_index;
2750Sstevel@tonic-gate 
2762973Sgovinda 		if (curr_tail_index < msiq_p->msiq_curr_head_index)
2772973Sgovinda 			curr_tail_index += msiq_state_p->msiq_rec_cnt;
2782973Sgovinda 
2792973Sgovinda 		msiq_p->msiq_recs2process = curr_tail_index -
2802973Sgovinda 		    msiq_p->msiq_curr_head_index;
2812973Sgovinda 	}
2820Sstevel@tonic-gate 
2832973Sgovinda 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: curr_head %x new_head %x "
2842973Sgovinda 	    "rec2process %x\n", msiq_p->msiq_curr_head_index,
2852973Sgovinda 	    msiq_p->msiq_new_head_index, msiq_p->msiq_recs2process);
2862973Sgovinda 
2872973Sgovinda 	/* If all MSIQ records are already processed, just return immediately */
2882973Sgovinda 	if ((msiq_p->msiq_new_head_index - msiq_p->msiq_curr_head_index)
2892973Sgovinda 	    == msiq_p->msiq_recs2process)
2902973Sgovinda 		goto intr_done;
2912973Sgovinda 
2922973Sgovinda 	curr_head_p = (msiqhead_t *)((caddr_t)msiq_p->msiq_base_p +
2932973Sgovinda 	    (msiq_p->msiq_curr_head_index * sizeof (msiq_rec_t)));
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	/*
2962588Segillett 	 * Calculate the number of recs to process by taking the difference
2972588Segillett 	 * between the head and tail pointers. For all records we always
2982588Segillett 	 * verify that we have a valid record type before we do any processing.
2992973Sgovinda 	 * If triggered, we should always have at least one valid record.
3000Sstevel@tonic-gate 	 */
3012973Sgovinda 	for (i = 0; i < msiq_p->msiq_recs2process; i++) {
3029686SAlan.Adamson@Sun.COM 		msiq_rec_type_t rec_type;
3039686SAlan.Adamson@Sun.COM 
3042973Sgovinda 		/* Read next MSIQ record */
3052588Segillett 		px_lib_get_msiq_rec(dip, curr_head_p, msiq_rec_p);
3062588Segillett 
3079686SAlan.Adamson@Sun.COM 		rec_type = msiq_rec_p->msiq_rec_type;
3089686SAlan.Adamson@Sun.COM 
3090Sstevel@tonic-gate 		DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: MSIQ RECORD, "
3100Sstevel@tonic-gate 		    "msiq_rec_type 0x%llx msiq_rec_rid 0x%llx\n",
3119686SAlan.Adamson@Sun.COM 		    rec_type, msiq_rec_p->msiq_rec_rid);
3120Sstevel@tonic-gate 
3139686SAlan.Adamson@Sun.COM 		if (!rec_type)
3142973Sgovinda 			goto next_rec;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 		/* Check MSIQ record type */
3179686SAlan.Adamson@Sun.COM 		switch (rec_type) {
3180Sstevel@tonic-gate 		case MSG_REC:
3190Sstevel@tonic-gate 			msg_code = msiq_rec_p->msiq_rec_data.msg.msg_code;
3200Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: PCIE MSG "
3210Sstevel@tonic-gate 			    "record, msg type 0x%x\n", msg_code);
3220Sstevel@tonic-gate 			break;
3230Sstevel@tonic-gate 		case MSI32_REC:
3240Sstevel@tonic-gate 		case MSI64_REC:
3250Sstevel@tonic-gate 			msg_code = msiq_rec_p->msiq_rec_data.msi.msi_data;
3260Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: MSI record, "
3270Sstevel@tonic-gate 			    "msi 0x%x\n", msg_code);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 			/* Clear MSI state */
3300Sstevel@tonic-gate 			px_lib_msi_setstate(dip, (msinum_t)msg_code,
3310Sstevel@tonic-gate 			    PCI_MSI_STATE_IDLE);
3320Sstevel@tonic-gate 			break;
3330Sstevel@tonic-gate 		default:
3340Sstevel@tonic-gate 			msg_code = 0;
3350Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: px_msiq_intr: 0x%x MSIQ "
3360Sstevel@tonic-gate 			    "record type is not supported",
3370Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
3389686SAlan.Adamson@Sun.COM 			    rec_type);
3392973Sgovinda 
3400Sstevel@tonic-gate 			goto next_rec;
3410Sstevel@tonic-gate 		}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 		/*
3440Sstevel@tonic-gate 		 * Scan through px_ih_t linked list, searching for the
3450Sstevel@tonic-gate 		 * right px_ih_t, matching MSIQ record data.
3460Sstevel@tonic-gate 		 */
3472973Sgovinda 		for (j = 0, ih_p = ipil_p->ipil_ih_start;
3482973Sgovinda 		    ih_p && (j < ipil_p->ipil_ih_size) &&
3491653Sgovinda 		    ((ih_p->ih_msg_code != msg_code) ||
3509686SAlan.Adamson@Sun.COM 		    (ih_p->ih_rec_type != rec_type));
3514397Sschwartz 		    ih_p = ih_p->ih_next, j++)
3524397Sschwartz 			;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 		if ((ih_p->ih_msg_code == msg_code) &&
3559686SAlan.Adamson@Sun.COM 		    (ih_p->ih_rec_type == rec_type)) {
3560Sstevel@tonic-gate 			dev_info_t *dip = ih_p->ih_dip;
3570Sstevel@tonic-gate 			uint_t (*handler)() = ih_p->ih_handler;
3580Sstevel@tonic-gate 			caddr_t arg1 = ih_p->ih_handler_arg1;
3590Sstevel@tonic-gate 			caddr_t arg2 = ih_p->ih_handler_arg2;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: ino=%x data=%x "
3620Sstevel@tonic-gate 			    "handler=%p arg1 =%p arg2=%p\n", ino_p->ino_ino,
3630Sstevel@tonic-gate 			    msg_code, handler, arg1, arg2);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 			DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
3660Sstevel@tonic-gate 			    void *, handler, caddr_t, arg1, caddr_t, arg2);
3670Sstevel@tonic-gate 
368*10053SEvan.Yan@Sun.COM 			ih_p->ih_retarget_flag = B_FALSE;
369*10053SEvan.Yan@Sun.COM 
37027Sjchu 			/*
37127Sjchu 			 * Special case for PCIE Error Messages.
37227Sjchu 			 * The current frame work doesn't fit PCIE Err Msgs
37327Sjchu 			 * This should be fixed when PCIE MESSAGES as a whole
37427Sjchu 			 * is architected correctly.
37527Sjchu 			 */
3769686SAlan.Adamson@Sun.COM 			if ((rec_type == MSG_REC) &&
3779686SAlan.Adamson@Sun.COM 			    ((msg_code == PCIE_MSG_CODE_ERR_COR) ||
37827Sjchu 			    (msg_code == PCIE_MSG_CODE_ERR_NONFATAL) ||
3799686SAlan.Adamson@Sun.COM 			    (msg_code == PCIE_MSG_CODE_ERR_FATAL))) {
38027Sjchu 				ret = px_err_fabric_intr(px_p, msg_code,
38127Sjchu 				    msiq_rec_p->msiq_rec_rid);
38227Sjchu 			} else
38327Sjchu 				ret = (*handler)(arg1, arg2);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 			/*
3860Sstevel@tonic-gate 			 * Account for time used by this interrupt. Protect
3870Sstevel@tonic-gate 			 * against conflicting writes to ih_ticks from
3880Sstevel@tonic-gate 			 * ib_intr_dist_all() by using atomic ops.
3890Sstevel@tonic-gate 			 */
3900Sstevel@tonic-gate 
3912973Sgovinda 			if (pil <= LOCK_LEVEL)
3920Sstevel@tonic-gate 				atomic_add_64(&ih_p->ih_ticks, intr_get_time());
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 			DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
3950Sstevel@tonic-gate 			    void *, handler, caddr_t, arg1, int, ret);
3962588Segillett 
3972973Sgovinda 			msiq_p->msiq_new_head_index++;
3982973Sgovinda 			px_lib_clr_msiq_rec(dip, curr_head_p);
3990Sstevel@tonic-gate 		} else {
4000Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr:"
4012588Segillett 			    "No matching MSIQ record found\n");
4022588Segillett 		}
4032588Segillett next_rec:
4042588Segillett 		/* Get the pointer next EQ record */
4052588Segillett 		curr_head_p = (msiqhead_t *)
4062588Segillett 		    ((caddr_t)curr_head_p + sizeof (msiq_rec_t));
4070Sstevel@tonic-gate 
4082588Segillett 		/* Check for overflow condition */
4092588Segillett 		if (curr_head_p >= (msiqhead_t *)((caddr_t)msiq_p->msiq_base_p
4102973Sgovinda 		    + (msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t))))
4112588Segillett 			curr_head_p = (msiqhead_t *)msiq_p->msiq_base_p;
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate 
4142973Sgovinda 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: No of MSIQ recs processed %x\n",
4152973Sgovinda 	    (msiq_p->msiq_new_head_index - msiq_p->msiq_curr_head_index));
4162973Sgovinda 
4172973Sgovinda 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: curr_head %x new_head %x "
4182973Sgovinda 	    "rec2process %x\n", msiq_p->msiq_curr_head_index,
4192973Sgovinda 	    msiq_p->msiq_new_head_index, msiq_p->msiq_recs2process);
4202588Segillett 
4212973Sgovinda 	/* ino_claimed used just for debugging purpose */
4222973Sgovinda 	if (ret)
4232973Sgovinda 		ino_p->ino_claimed |= (1 << pil);
4242973Sgovinda 
4252973Sgovinda intr_done:
4262973Sgovinda 	/* Interrupt can only be cleared after all pil levels are handled */
4272973Sgovinda 	if (pil != ino_p->ino_lopil)
4282973Sgovinda 		return (DDI_INTR_CLAIMED);
4292973Sgovinda 
4302973Sgovinda 	if (msiq_p->msiq_new_head_index <= msiq_p->msiq_curr_head_index)  {
4312973Sgovinda 		if (px_unclaimed_intr_block)
4322973Sgovinda 			return (px_spurintr(ipil_p));
4332588Segillett 	}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	/*  Update MSIQ head index with no of MSIQ records processed */
4362973Sgovinda 	if (msiq_p->msiq_new_head_index >= msiq_state_p->msiq_rec_cnt)
4372973Sgovinda 		msiq_p->msiq_new_head_index -= msiq_state_p->msiq_rec_cnt;
4380Sstevel@tonic-gate 
4392973Sgovinda 	msiq_p->msiq_curr_head_index = msiq_p->msiq_new_head_index;
4402973Sgovinda 	px_lib_msiq_sethead(dip, msiq_p->msiq_id, msiq_p->msiq_new_head_index);
4412973Sgovinda 
4422973Sgovinda 	msiq_p->msiq_new_head_index = 0;
4432973Sgovinda 	msiq_p->msiq_recs2process = 0;
4442973Sgovinda 	ino_p->ino_claimed = 0;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	/* Clear the pending state */
4470Sstevel@tonic-gate 	if (px_lib_intr_setstate(dip, ino_p->ino_sysino,
4480Sstevel@tonic-gate 	    INTR_IDLE_STATE) != DDI_SUCCESS)
4490Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate dev_info_t *
4550Sstevel@tonic-gate px_get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
4560Sstevel@tonic-gate {
4570Sstevel@tonic-gate 	dev_info_t	*cdip = rdip;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
4600Sstevel@tonic-gate 		;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	return (cdip);
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate /* ARGSUSED */
4660Sstevel@tonic-gate int
4670Sstevel@tonic-gate px_intx_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
4680Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
4690Sstevel@tonic-gate {
470693Sgovinda 	px_t	*px_p = DIP_TO_STATE(dip);
471693Sgovinda 	int	ret = DDI_SUCCESS;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	DBG(DBG_INTROPS, dip, "px_intx_ops: dip=%x rdip=%x intr_op=%x "
4740Sstevel@tonic-gate 	    "handle=%p\n", dip, rdip, intr_op, hdlp);
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	switch (intr_op) {
4770Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
4780Sstevel@tonic-gate 		ret = pci_intx_get_cap(rdip, (int *)result);
4790Sstevel@tonic-gate 		break;
4800Sstevel@tonic-gate 	case DDI_INTROP_SETCAP:
4810Sstevel@tonic-gate 		DBG(DBG_INTROPS, dip, "px_intx_ops: SetCap is not supported\n");
4820Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
4830Sstevel@tonic-gate 		break;
4840Sstevel@tonic-gate 	case DDI_INTROP_ALLOC:
4850Sstevel@tonic-gate 		*(int *)result = hdlp->ih_scratch1;
4860Sstevel@tonic-gate 		break;
4870Sstevel@tonic-gate 	case DDI_INTROP_FREE:
4880Sstevel@tonic-gate 		break;
4890Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
490693Sgovinda 		*(int *)result = hdlp->ih_pri ?
4918535Sevan.yan@sun.com 		    hdlp->ih_pri : pci_class_to_pil(rdip);
4920Sstevel@tonic-gate 		break;
4930Sstevel@tonic-gate 	case DDI_INTROP_SETPRI:
4940Sstevel@tonic-gate 		break;
4950Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
4960Sstevel@tonic-gate 		ret = px_add_intx_intr(dip, rdip, hdlp);
4970Sstevel@tonic-gate 		break;
4980Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
4990Sstevel@tonic-gate 		ret = px_rem_intx_intr(dip, rdip, hdlp);
5000Sstevel@tonic-gate 		break;
501*10053SEvan.Yan@Sun.COM 	case DDI_INTROP_GETTARGET:
502*10053SEvan.Yan@Sun.COM 		ret = px_ib_get_intr_target(px_p, hdlp->ih_vector,
503*10053SEvan.Yan@Sun.COM 		    (cpuid_t *)result);
504*10053SEvan.Yan@Sun.COM 		break;
505*10053SEvan.Yan@Sun.COM 	case DDI_INTROP_SETTARGET:
506*10053SEvan.Yan@Sun.COM 		ret = DDI_ENOTSUP;
507*10053SEvan.Yan@Sun.COM 		break;
5080Sstevel@tonic-gate 	case DDI_INTROP_ENABLE:
5090Sstevel@tonic-gate 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
5102973Sgovinda 		    hdlp->ih_vector, hdlp->ih_pri, PX_INTR_STATE_ENABLE, 0, 0);
5110Sstevel@tonic-gate 		break;
5120Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
5130Sstevel@tonic-gate 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
5142973Sgovinda 		    hdlp->ih_vector, hdlp->ih_pri, PX_INTR_STATE_DISABLE, 0, 0);
5150Sstevel@tonic-gate 		break;
5160Sstevel@tonic-gate 	case DDI_INTROP_SETMASK:
5170Sstevel@tonic-gate 		ret = pci_intx_set_mask(rdip);
5180Sstevel@tonic-gate 		break;
5190Sstevel@tonic-gate 	case DDI_INTROP_CLRMASK:
5200Sstevel@tonic-gate 		ret = pci_intx_clr_mask(rdip);
5210Sstevel@tonic-gate 		break;
5220Sstevel@tonic-gate 	case DDI_INTROP_GETPENDING:
5230Sstevel@tonic-gate 		ret = pci_intx_get_pending(rdip, (int *)result);
5240Sstevel@tonic-gate 		break;
5250Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
5260Sstevel@tonic-gate 	case DDI_INTROP_NAVAIL:
5272580Sanish 		*(int *)result = i_ddi_get_intx_nintrs(rdip);
5280Sstevel@tonic-gate 		break;
5290Sstevel@tonic-gate 	default:
5300Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
5310Sstevel@tonic-gate 		break;
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	return (ret);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate /* ARGSUSED */
5380Sstevel@tonic-gate int
5390Sstevel@tonic-gate px_msix_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
5400Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
5410Sstevel@tonic-gate {
5420Sstevel@tonic-gate 	px_t			*px_p = DIP_TO_STATE(dip);
5430Sstevel@tonic-gate 	px_msi_state_t		*msi_state_p = &px_p->px_ib_p->ib_msi_state;
544965Sgovinda 	msiq_rec_type_t		msiq_rec_type;
545965Sgovinda 	msi_type_t		msi_type;
546965Sgovinda 	uint64_t		msi_addr;
5470Sstevel@tonic-gate 	msinum_t		msi_num;
5480Sstevel@tonic-gate 	msiqid_t		msiq_id;
5490Sstevel@tonic-gate 	uint_t			nintrs;
550*10053SEvan.Yan@Sun.COM 	int			ret = DDI_SUCCESS;
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	DBG(DBG_INTROPS, dip, "px_msix_ops: dip=%x rdip=%x intr_op=%x "
5530Sstevel@tonic-gate 	    "handle=%p\n", dip, rdip, intr_op, hdlp);
5540Sstevel@tonic-gate 
555965Sgovinda 	/* Check for MSI64 support */
5561653Sgovinda 	if ((hdlp->ih_cap & DDI_INTR_FLAG_MSI64) && msi_state_p->msi_addr64) {
557965Sgovinda 		msiq_rec_type = MSI64_REC;
558965Sgovinda 		msi_type = MSI64_TYPE;
5591653Sgovinda 		msi_addr = msi_state_p->msi_addr64;
560965Sgovinda 	} else {
561965Sgovinda 		msiq_rec_type = MSI32_REC;
562965Sgovinda 		msi_type = MSI32_TYPE;
563965Sgovinda 		msi_addr = msi_state_p->msi_addr32;
564965Sgovinda 	}
565965Sgovinda 
566*10053SEvan.Yan@Sun.COM 	(void) px_msi_get_msinum(px_p, hdlp->ih_dip,
567*10053SEvan.Yan@Sun.COM 	    (hdlp->ih_flags & DDI_INTR_MSIX_DUP) ? hdlp->ih_main->ih_inum :
568*10053SEvan.Yan@Sun.COM 	    hdlp->ih_inum, &msi_num);
569*10053SEvan.Yan@Sun.COM 
5700Sstevel@tonic-gate 	switch (intr_op) {
5710Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
5720Sstevel@tonic-gate 		ret = pci_msi_get_cap(rdip, hdlp->ih_type, (int *)result);
573*10053SEvan.Yan@Sun.COM 		if (ret == DDI_SUCCESS)
574*10053SEvan.Yan@Sun.COM 			*(int *)result |= DDI_INTR_FLAG_RETARGETABLE;
5750Sstevel@tonic-gate 		break;
5760Sstevel@tonic-gate 	case DDI_INTROP_SETCAP:
5770Sstevel@tonic-gate 		DBG(DBG_INTROPS, dip, "px_msix_ops: SetCap is not supported\n");
5780Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
5790Sstevel@tonic-gate 		break;
5800Sstevel@tonic-gate 	case DDI_INTROP_ALLOC:
5810Sstevel@tonic-gate 		/*
5820Sstevel@tonic-gate 		 * We need to restrict this allocation in future
5830Sstevel@tonic-gate 		 * based on Resource Management policies.
5840Sstevel@tonic-gate 		 */
5858561SScott.Carter@Sun.COM 		if ((ret = px_msi_alloc(px_p, rdip, hdlp->ih_type,
5868561SScott.Carter@Sun.COM 		    hdlp->ih_inum, hdlp->ih_scratch1,
5878561SScott.Carter@Sun.COM 		    (uintptr_t)hdlp->ih_scratch2,
5881725Segillett 		    (int *)result)) != DDI_SUCCESS) {
5891725Segillett 			DBG(DBG_INTROPS, dip, "px_msix_ops: allocation "
5901725Segillett 			    "failed, rdip 0x%p type 0x%d inum 0x%x "
5911725Segillett 			    "count 0x%x\n", rdip, hdlp->ih_type, hdlp->ih_inum,
5921725Segillett 			    hdlp->ih_scratch1);
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 			return (ret);
5950Sstevel@tonic-gate 		}
5960Sstevel@tonic-gate 
5971725Segillett 		if ((hdlp->ih_type == DDI_INTR_TYPE_MSIX) &&
5981725Segillett 		    (i_ddi_get_msix(rdip) == NULL)) {
5991725Segillett 			ddi_intr_msix_t		*msix_p;
6001725Segillett 
6011725Segillett 			if (msix_p = pci_msix_init(rdip)) {
6021725Segillett 				i_ddi_set_msix(rdip, msix_p);
6031725Segillett 				break;
6041725Segillett 			}
6051725Segillett 
6061725Segillett 			DBG(DBG_INTROPS, dip, "px_msix_ops: MSI-X allocation "
6071725Segillett 			    "failed, rdip 0x%p inum 0x%x\n", rdip,
6081725Segillett 			    hdlp->ih_inum);
6091725Segillett 
6101725Segillett 			(void) px_msi_free(px_p, rdip, hdlp->ih_inum,
6111725Segillett 			    hdlp->ih_scratch1);
6121725Segillett 
6131725Segillett 			return (DDI_FAILURE);
6141725Segillett 		}
6151725Segillett 
6160Sstevel@tonic-gate 		break;
6170Sstevel@tonic-gate 	case DDI_INTROP_FREE:
6180Sstevel@tonic-gate 		(void) pci_msi_unconfigure(rdip, hdlp->ih_type, hdlp->ih_inum);
6191725Segillett 
6201725Segillett 		if (hdlp->ih_type == DDI_INTR_TYPE_MSI)
6211725Segillett 			goto msi_free;
6221725Segillett 
6231725Segillett 		if (hdlp->ih_flags & DDI_INTR_MSIX_DUP)
6241725Segillett 			break;
6251725Segillett 
6261725Segillett 		if (((i_ddi_intr_get_current_nintrs(hdlp->ih_dip) - 1) == 0) &&
6271725Segillett 		    (i_ddi_get_msix(rdip))) {
6281725Segillett 			pci_msix_fini(i_ddi_get_msix(rdip));
6291725Segillett 			i_ddi_set_msix(rdip, NULL);
6301725Segillett 		}
6311725Segillett msi_free:
6320Sstevel@tonic-gate 		(void) px_msi_free(px_p, rdip, hdlp->ih_inum,
6330Sstevel@tonic-gate 		    hdlp->ih_scratch1);
6340Sstevel@tonic-gate 		break;
6350Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
6360Sstevel@tonic-gate 		*(int *)result = hdlp->ih_pri ?
6378535Sevan.yan@sun.com 		    hdlp->ih_pri : pci_class_to_pil(rdip);
6380Sstevel@tonic-gate 		break;
6390Sstevel@tonic-gate 	case DDI_INTROP_SETPRI:
6400Sstevel@tonic-gate 		break;
6410Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
6420Sstevel@tonic-gate 		if ((ret = px_add_msiq_intr(dip, rdip, hdlp,
643*10053SEvan.Yan@Sun.COM 		    msiq_rec_type, msi_num, -1, &msiq_id)) != DDI_SUCCESS) {
6440Sstevel@tonic-gate 			DBG(DBG_INTROPS, dip, "px_msix_ops: Add MSI handler "
6450Sstevel@tonic-gate 			    "failed, rdip 0x%p msi 0x%x\n", rdip, msi_num);
6460Sstevel@tonic-gate 			return (ret);
6470Sstevel@tonic-gate 		}
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		DBG(DBG_INTROPS, dip, "px_msix_ops: msiq used 0x%x\n", msiq_id);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		if ((ret = px_lib_msi_setmsiq(dip, msi_num,
652965Sgovinda 		    msiq_id, msi_type)) != DDI_SUCCESS) {
6530Sstevel@tonic-gate 			(void) px_rem_msiq_intr(dip, rdip,
654965Sgovinda 			    hdlp, msiq_rec_type, msi_num, msiq_id);
6550Sstevel@tonic-gate 			return (ret);
6560Sstevel@tonic-gate 		}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 		if ((ret = px_lib_msi_setstate(dip, msi_num,
6590Sstevel@tonic-gate 		    PCI_MSI_STATE_IDLE)) != DDI_SUCCESS) {
6600Sstevel@tonic-gate 			(void) px_rem_msiq_intr(dip, rdip,
661965Sgovinda 			    hdlp, msiq_rec_type, msi_num, msiq_id);
6620Sstevel@tonic-gate 			return (ret);
6630Sstevel@tonic-gate 		}
6640Sstevel@tonic-gate 
665*10053SEvan.Yan@Sun.COM 		if ((ret = px_lib_msi_setvalid(dip, msi_num,
666*10053SEvan.Yan@Sun.COM 		    PCI_MSI_VALID)) != DDI_SUCCESS)
667*10053SEvan.Yan@Sun.COM 			return (ret);
668*10053SEvan.Yan@Sun.COM 
669*10053SEvan.Yan@Sun.COM 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
670*10053SEvan.Yan@Sun.COM 		    px_msiqid_to_devino(px_p, msiq_id), hdlp->ih_pri,
671*10053SEvan.Yan@Sun.COM 		    PX_INTR_STATE_ENABLE, msiq_rec_type, msi_num);
672*10053SEvan.Yan@Sun.COM 
6730Sstevel@tonic-gate 		break;
6740Sstevel@tonic-gate 	case DDI_INTROP_DUPVEC:
6751725Segillett 		DBG(DBG_INTROPS, dip, "px_msix_ops: dupisr - inum: %x, "
6761725Segillett 		    "new_vector: %x\n", hdlp->ih_inum, hdlp->ih_scratch1);
6771725Segillett 
6781725Segillett 		ret = pci_msix_dup(hdlp->ih_dip, hdlp->ih_inum,
6791725Segillett 		    hdlp->ih_scratch1);
6800Sstevel@tonic-gate 		break;
6810Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
6820Sstevel@tonic-gate 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
6830Sstevel@tonic-gate 		    &msiq_id)) != DDI_SUCCESS)
6840Sstevel@tonic-gate 			return (ret);
6850Sstevel@tonic-gate 
686*10053SEvan.Yan@Sun.COM 		if ((ret = px_ib_update_intr_state(px_p, rdip,
687*10053SEvan.Yan@Sun.COM 		    hdlp->ih_inum, px_msiqid_to_devino(px_p, msiq_id),
688*10053SEvan.Yan@Sun.COM 		    hdlp->ih_pri, PX_INTR_STATE_DISABLE, msiq_rec_type,
689*10053SEvan.Yan@Sun.COM 		    msi_num)) != DDI_SUCCESS)
690*10053SEvan.Yan@Sun.COM 			return (ret);
691*10053SEvan.Yan@Sun.COM 
692*10053SEvan.Yan@Sun.COM 		if ((ret = px_lib_msi_setvalid(dip, msi_num,
693*10053SEvan.Yan@Sun.COM 		    PCI_MSI_INVALID)) != DDI_SUCCESS)
694*10053SEvan.Yan@Sun.COM 			return (ret);
695*10053SEvan.Yan@Sun.COM 
6960Sstevel@tonic-gate 		if ((ret = px_lib_msi_setstate(dip, msi_num,
697965Sgovinda 		    PCI_MSI_STATE_IDLE)) != DDI_SUCCESS)
6980Sstevel@tonic-gate 			return (ret);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 		ret = px_rem_msiq_intr(dip, rdip,
701965Sgovinda 		    hdlp, msiq_rec_type, msi_num, msiq_id);
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 		break;
704*10053SEvan.Yan@Sun.COM 	case DDI_INTROP_GETTARGET:
705*10053SEvan.Yan@Sun.COM 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
706*10053SEvan.Yan@Sun.COM 		    &msiq_id)) != DDI_SUCCESS)
7070Sstevel@tonic-gate 			return (ret);
7080Sstevel@tonic-gate 
709*10053SEvan.Yan@Sun.COM 		ret = px_ib_get_intr_target(px_p,
710*10053SEvan.Yan@Sun.COM 		    px_msiqid_to_devino(px_p, msiq_id), (cpuid_t *)result);
711*10053SEvan.Yan@Sun.COM 		break;
712*10053SEvan.Yan@Sun.COM 	case DDI_INTROP_SETTARGET:
713*10053SEvan.Yan@Sun.COM 		ret = px_ib_set_msix_target(px_p, hdlp, msi_num,
714*10053SEvan.Yan@Sun.COM 		    *(cpuid_t *)result);
715*10053SEvan.Yan@Sun.COM 		break;
716*10053SEvan.Yan@Sun.COM 	case DDI_INTROP_ENABLE:
717*10053SEvan.Yan@Sun.COM 		/*
718*10053SEvan.Yan@Sun.COM 		 * curr_nenables will be greater than 0 if rdip is using
719*10053SEvan.Yan@Sun.COM 		 * MSI-X and also, if it is using DUP interface. If this
720*10053SEvan.Yan@Sun.COM 		 * curr_enables is > 1, return after clearing the mask bit.
721*10053SEvan.Yan@Sun.COM 		 */
722*10053SEvan.Yan@Sun.COM 		if ((pci_is_msi_enabled(rdip, hdlp->ih_type) == DDI_SUCCESS) &&
723*10053SEvan.Yan@Sun.COM 		    (i_ddi_intr_get_current_nenables(rdip) > 0)) {
724*10053SEvan.Yan@Sun.COM 			return (pci_msi_clr_mask(rdip, hdlp->ih_type,
725*10053SEvan.Yan@Sun.COM 			    hdlp->ih_inum));
726*10053SEvan.Yan@Sun.COM 		}
7270Sstevel@tonic-gate 
728*10053SEvan.Yan@Sun.COM 		nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
7290Sstevel@tonic-gate 
730*10053SEvan.Yan@Sun.COM 		if ((ret = pci_msi_configure(rdip, hdlp->ih_type,
731*10053SEvan.Yan@Sun.COM 		    nintrs, hdlp->ih_inum, msi_addr,
732*10053SEvan.Yan@Sun.COM 		    hdlp->ih_type == DDI_INTR_TYPE_MSIX ? msi_num :
733*10053SEvan.Yan@Sun.COM 		    msi_num & ~(nintrs - 1))) != DDI_SUCCESS)
734*10053SEvan.Yan@Sun.COM 			return (ret);
735*10053SEvan.Yan@Sun.COM 
736*10053SEvan.Yan@Sun.COM 		if ((ret = pci_msi_enable_mode(rdip,
737*10053SEvan.Yan@Sun.COM 		    hdlp->ih_type)) != DDI_SUCCESS)
738*10053SEvan.Yan@Sun.COM 			return (ret);
7390Sstevel@tonic-gate 
740909Segillett 		if ((ret = pci_msi_clr_mask(rdip, hdlp->ih_type,
741909Segillett 		    hdlp->ih_inum)) != DDI_SUCCESS)
742909Segillett 			return (ret);
743909Segillett 
7440Sstevel@tonic-gate 		break;
7450Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
7460Sstevel@tonic-gate 		if ((ret = pci_msi_set_mask(rdip, hdlp->ih_type,
7470Sstevel@tonic-gate 		    hdlp->ih_inum)) != DDI_SUCCESS)
7480Sstevel@tonic-gate 			return (ret);
7490Sstevel@tonic-gate 
750*10053SEvan.Yan@Sun.COM 		/*
751*10053SEvan.Yan@Sun.COM 		 * curr_nenables will be greater than 1 if rdip is using
752*10053SEvan.Yan@Sun.COM 		 * MSI-X and also, if it is using DUP interface. If this
753*10053SEvan.Yan@Sun.COM 		 * curr_enables is > 1, return after setting the mask bit.
754*10053SEvan.Yan@Sun.COM 		 */
755*10053SEvan.Yan@Sun.COM 		if (i_ddi_intr_get_current_nenables(rdip) > 1)
756*10053SEvan.Yan@Sun.COM 			return (DDI_SUCCESS);
7571725Segillett 
758*10053SEvan.Yan@Sun.COM 		if ((ret = pci_msi_disable_mode(rdip, hdlp->ih_type))
759*10053SEvan.Yan@Sun.COM 		    != DDI_SUCCESS)
760909Segillett 			return (ret);
761909Segillett 
7620Sstevel@tonic-gate 		break;
7630Sstevel@tonic-gate 	case DDI_INTROP_BLOCKENABLE:
7640Sstevel@tonic-gate 		nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 		if ((ret = pci_msi_configure(rdip, hdlp->ih_type,
767965Sgovinda 		    nintrs, hdlp->ih_inum, msi_addr,
7680Sstevel@tonic-gate 		    msi_num & ~(nintrs - 1))) != DDI_SUCCESS)
7690Sstevel@tonic-gate 			return (ret);
7700Sstevel@tonic-gate 
7712755Segillett 		ret = pci_msi_enable_mode(rdip, hdlp->ih_type);
7720Sstevel@tonic-gate 		break;
7730Sstevel@tonic-gate 	case DDI_INTROP_BLOCKDISABLE:
774*10053SEvan.Yan@Sun.COM 		ret = pci_msi_disable_mode(rdip, hdlp->ih_type);
7750Sstevel@tonic-gate 		break;
7760Sstevel@tonic-gate 	case DDI_INTROP_SETMASK:
7770Sstevel@tonic-gate 		ret = pci_msi_set_mask(rdip, hdlp->ih_type, hdlp->ih_inum);
7780Sstevel@tonic-gate 		break;
7790Sstevel@tonic-gate 	case DDI_INTROP_CLRMASK:
7800Sstevel@tonic-gate 		ret = pci_msi_clr_mask(rdip, hdlp->ih_type, hdlp->ih_inum);
7810Sstevel@tonic-gate 		break;
7820Sstevel@tonic-gate 	case DDI_INTROP_GETPENDING:
7830Sstevel@tonic-gate 		ret = pci_msi_get_pending(rdip, hdlp->ih_type,
7840Sstevel@tonic-gate 		    hdlp->ih_inum, (int *)result);
7850Sstevel@tonic-gate 		break;
7860Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
7870Sstevel@tonic-gate 		ret = pci_msi_get_nintrs(rdip, hdlp->ih_type, (int *)result);
7880Sstevel@tonic-gate 		break;
7890Sstevel@tonic-gate 	case DDI_INTROP_NAVAIL:
7900Sstevel@tonic-gate 		/* XXX - a new interface may be needed */
7910Sstevel@tonic-gate 		ret = pci_msi_get_nintrs(rdip, hdlp->ih_type, (int *)result);
7920Sstevel@tonic-gate 		break;
7938561SScott.Carter@Sun.COM 	case DDI_INTROP_GETPOOL:
7948561SScott.Carter@Sun.COM 		if (msi_state_p->msi_pool_p == NULL) {
7958561SScott.Carter@Sun.COM 			*(ddi_irm_pool_t **)result = NULL;
7968561SScott.Carter@Sun.COM 			return (DDI_ENOTSUP);
7978561SScott.Carter@Sun.COM 		}
7988561SScott.Carter@Sun.COM 		*(ddi_irm_pool_t **)result = msi_state_p->msi_pool_p;
7998561SScott.Carter@Sun.COM 		ret = DDI_SUCCESS;
8008561SScott.Carter@Sun.COM 		break;
8010Sstevel@tonic-gate 	default:
8020Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
8030Sstevel@tonic-gate 		break;
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	return (ret);
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate 
80966Sesolom static struct {
81066Sesolom 	kstat_named_t pxintr_ks_name;
81166Sesolom 	kstat_named_t pxintr_ks_type;
81266Sesolom 	kstat_named_t pxintr_ks_cpu;
81366Sesolom 	kstat_named_t pxintr_ks_pil;
81466Sesolom 	kstat_named_t pxintr_ks_time;
81566Sesolom 	kstat_named_t pxintr_ks_ino;
81666Sesolom 	kstat_named_t pxintr_ks_cookie;
81766Sesolom 	kstat_named_t pxintr_ks_devpath;
81866Sesolom 	kstat_named_t pxintr_ks_buspath;
81966Sesolom } pxintr_ks_template = {
82066Sesolom 	{ "name",	KSTAT_DATA_CHAR },
82166Sesolom 	{ "type",	KSTAT_DATA_CHAR },
82266Sesolom 	{ "cpu",	KSTAT_DATA_UINT64 },
82366Sesolom 	{ "pil",	KSTAT_DATA_UINT64 },
82466Sesolom 	{ "time",	KSTAT_DATA_UINT64 },
82566Sesolom 	{ "ino",	KSTAT_DATA_UINT64 },
82666Sesolom 	{ "cookie",	KSTAT_DATA_UINT64 },
82766Sesolom 	{ "devpath",	KSTAT_DATA_STRING },
82866Sesolom 	{ "buspath",	KSTAT_DATA_STRING },
82966Sesolom };
83066Sesolom 
83166Sesolom static uint32_t pxintr_ks_instance;
8321811Sesolom static char ih_devpath[MAXPATHLEN];
8331811Sesolom static char ih_buspath[MAXPATHLEN];
83466Sesolom kmutex_t pxintr_ks_template_lock;
83566Sesolom 
83666Sesolom int
83766Sesolom px_ks_update(kstat_t *ksp, int rw)
83866Sesolom {
83966Sesolom 	px_ih_t *ih_p = ksp->ks_private;
84066Sesolom 	int maxlen = sizeof (pxintr_ks_template.pxintr_ks_name.value.c);
8412973Sgovinda 	px_ino_pil_t *ipil_p = ih_p->ih_ipil_p;
8422973Sgovinda 	px_ino_t *ino_p = ipil_p->ipil_ino_p;
8432973Sgovinda 	px_t *px_p = ino_p->ino_ib_p->ib_px_p;
84466Sesolom 	devino_t ino;
84566Sesolom 	sysino_t sysino;
84666Sesolom 
8472973Sgovinda 	ino = ino_p->ino_ino;
8487124Sanbui 	if (px_lib_intr_devino_to_sysino(px_p->px_dip, ino, &sysino) !=
8497124Sanbui 	    DDI_SUCCESS) {
8507124Sanbui 		cmn_err(CE_WARN, "px_ks_update: px_lib_intr_devino_to_sysino "
8517124Sanbui 		    "failed");
8527124Sanbui 	}
85366Sesolom 
85466Sesolom 	(void) snprintf(pxintr_ks_template.pxintr_ks_name.value.c, maxlen,
85566Sesolom 	    "%s%d", ddi_driver_name(ih_p->ih_dip),
85666Sesolom 	    ddi_get_instance(ih_p->ih_dip));
85766Sesolom 
85866Sesolom 	(void) ddi_pathname(ih_p->ih_dip, ih_devpath);
85966Sesolom 	(void) ddi_pathname(px_p->px_dip, ih_buspath);
86066Sesolom 	kstat_named_setstr(&pxintr_ks_template.pxintr_ks_devpath, ih_devpath);
86166Sesolom 	kstat_named_setstr(&pxintr_ks_template.pxintr_ks_buspath, ih_buspath);
86266Sesolom 
8631087Sschwartz 	if (ih_p->ih_intr_state == PX_INTR_STATE_ENABLE) {
8641087Sschwartz 
8654397Sschwartz 		switch (i_ddi_intr_get_current_type(ih_p->ih_dip)) {
8664397Sschwartz 		case DDI_INTR_TYPE_MSI:
8674397Sschwartz 			(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
8684397Sschwartz 			    "msi");
8694397Sschwartz 			break;
8704397Sschwartz 		case DDI_INTR_TYPE_MSIX:
8714397Sschwartz 			(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
8724397Sschwartz 			    "msix");
8734397Sschwartz 			break;
8744397Sschwartz 		default:
8754397Sschwartz 			(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
8764397Sschwartz 			    "fixed");
8774397Sschwartz 			break;
8784397Sschwartz 		}
8794397Sschwartz 
8802973Sgovinda 		pxintr_ks_template.pxintr_ks_cpu.value.ui64 = ino_p->ino_cpuid;
8812973Sgovinda 		pxintr_ks_template.pxintr_ks_pil.value.ui64 = ipil_p->ipil_pil;
8821087Sschwartz 		pxintr_ks_template.pxintr_ks_time.value.ui64 = ih_p->ih_nsec +
8831087Sschwartz 		    (uint64_t)tick2ns((hrtime_t)ih_p->ih_ticks,
8842973Sgovinda 		    ino_p->ino_cpuid);
8851087Sschwartz 		pxintr_ks_template.pxintr_ks_ino.value.ui64 = ino;
8861087Sschwartz 		pxintr_ks_template.pxintr_ks_cookie.value.ui64 = sysino;
8871087Sschwartz 	} else {
8881087Sschwartz 		(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
8891087Sschwartz 		    "disabled");
8901087Sschwartz 		pxintr_ks_template.pxintr_ks_cpu.value.ui64 = 0;
8911087Sschwartz 		pxintr_ks_template.pxintr_ks_pil.value.ui64 = 0;
8921087Sschwartz 		pxintr_ks_template.pxintr_ks_time.value.ui64 = 0;
8931087Sschwartz 		pxintr_ks_template.pxintr_ks_ino.value.ui64 = 0;
8941087Sschwartz 		pxintr_ks_template.pxintr_ks_cookie.value.ui64 = 0;
8951087Sschwartz 	}
89666Sesolom 	return (0);
89766Sesolom }
89866Sesolom 
89966Sesolom void
90066Sesolom px_create_intr_kstats(px_ih_t *ih_p)
90166Sesolom {
90266Sesolom 	msiq_rec_type_t rec_type = ih_p->ih_rec_type;
90366Sesolom 
90466Sesolom 	ASSERT(ih_p->ih_ksp == NULL);
90566Sesolom 
90666Sesolom 	/*
90766Sesolom 	 * Create pci_intrs::: kstats for all ih types except messages,
90866Sesolom 	 * which represent unusual conditions and don't need to be tracked.
90966Sesolom 	 */
91066Sesolom 	if (rec_type == 0 || rec_type == MSI32_REC || rec_type == MSI64_REC) {
91166Sesolom 		ih_p->ih_ksp = kstat_create("pci_intrs",
91266Sesolom 		    atomic_inc_32_nv(&pxintr_ks_instance), "config",
91366Sesolom 		    "interrupts", KSTAT_TYPE_NAMED,
91466Sesolom 		    sizeof (pxintr_ks_template) / sizeof (kstat_named_t),
91566Sesolom 		    KSTAT_FLAG_VIRTUAL);
91666Sesolom 	}
91766Sesolom 	if (ih_p->ih_ksp != NULL) {
91866Sesolom 		ih_p->ih_ksp->ks_data_size += MAXPATHLEN * 2;
91966Sesolom 		ih_p->ih_ksp->ks_lock = &pxintr_ks_template_lock;
92066Sesolom 		ih_p->ih_ksp->ks_data = &pxintr_ks_template;
92166Sesolom 		ih_p->ih_ksp->ks_private = ih_p;
92266Sesolom 		ih_p->ih_ksp->ks_update = px_ks_update;
92366Sesolom 	}
92466Sesolom }
92566Sesolom 
926693Sgovinda /*
927693Sgovinda  * px_add_intx_intr:
928693Sgovinda  *
929693Sgovinda  * This function is called to register INTx and legacy hardware
930693Sgovinda  * interrupt pins interrupts.
931693Sgovinda  */
9320Sstevel@tonic-gate int
9330Sstevel@tonic-gate px_add_intx_intr(dev_info_t *dip, dev_info_t *rdip,
9340Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
9350Sstevel@tonic-gate {
9360Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
9370Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
9380Sstevel@tonic-gate 	devino_t	ino;
9390Sstevel@tonic-gate 	px_ih_t		*ih_p;
9402973Sgovinda 	px_ino_t	*ino_p;
9412973Sgovinda 	px_ino_pil_t	*ipil_p, *ipil_list;
9420Sstevel@tonic-gate 	int32_t		weight;
9430Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	ino = hdlp->ih_vector;
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: rdip=%s%d ino=%x "
9480Sstevel@tonic-gate 	    "handler=%x arg1=%x arg2=%x\n", ddi_driver_name(rdip),
9490Sstevel@tonic-gate 	    ddi_get_instance(rdip), ino, hdlp->ih_cb_func,
9500Sstevel@tonic-gate 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	ih_p = px_ib_alloc_ih(rdip, hdlp->ih_inum,
9530Sstevel@tonic-gate 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, 0, 0);
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
9560Sstevel@tonic-gate 
9572973Sgovinda 	ino_p = px_ib_locate_ino(ib_p, ino);
9582973Sgovinda 	ipil_list = ino_p ? ino_p->ino_ipil_p : NULL;
9592973Sgovinda 
9602973Sgovinda 	/* Sharing ino */
9612973Sgovinda 	if (ino_p && (ipil_p = px_ib_ino_locate_ipil(ino_p, hdlp->ih_pri))) {
9622973Sgovinda 		if (px_ib_intr_locate_ih(ipil_p, rdip, hdlp->ih_inum, 0, 0)) {
9630Sstevel@tonic-gate 			DBG(DBG_A_INTX, dip, "px_add_intx_intr: "
9642973Sgovinda 			    "dup intr #%d\n", hdlp->ih_inum);
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 			ret = DDI_FAILURE;
9670Sstevel@tonic-gate 			goto fail1;
9680Sstevel@tonic-gate 		}
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 		/* Save mondo value in hdlp */
9710Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
9720Sstevel@tonic-gate 
9732973Sgovinda 		if ((ret = px_ib_ino_add_intr(px_p, ipil_p,
9742973Sgovinda 		    ih_p)) != DDI_SUCCESS)
9750Sstevel@tonic-gate 			goto fail1;
9762973Sgovinda 
9772973Sgovinda 		goto ino_done;
9782973Sgovinda 	}
9790Sstevel@tonic-gate 
9802973Sgovinda 	if (hdlp->ih_pri == 0)
9818535Sevan.yan@sun.com 		hdlp->ih_pri = pci_class_to_pil(rdip);
9822973Sgovinda 
9832973Sgovinda 	ipil_p = px_ib_new_ino_pil(ib_p, ino, hdlp->ih_pri, ih_p);
9842973Sgovinda 	ino_p = ipil_p->ipil_ino_p;
9850Sstevel@tonic-gate 
9862973Sgovinda 	/* Save mondo value in hdlp */
9872973Sgovinda 	hdlp->ih_vector = ino_p->ino_sysino;
9880Sstevel@tonic-gate 
9892973Sgovinda 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: pil=0x%x mondo=0x%x\n",
9902973Sgovinda 	    hdlp->ih_pri, hdlp->ih_vector);
9910Sstevel@tonic-gate 
9922973Sgovinda 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
9932973Sgovinda 	    (ddi_intr_handler_t *)px_intx_intr, (caddr_t)ipil_p, NULL);
9940Sstevel@tonic-gate 
9952973Sgovinda 	ret = i_ddi_add_ivintr(hdlp);
9960Sstevel@tonic-gate 
9972973Sgovinda 	/*
9982973Sgovinda 	 * Restore original interrupt handler
9992973Sgovinda 	 * and arguments in interrupt handle.
10002973Sgovinda 	 */
10012973Sgovinda 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
10022973Sgovinda 	    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
10030Sstevel@tonic-gate 
10042973Sgovinda 	if (ret != DDI_SUCCESS)
10052973Sgovinda 		goto fail2;
10060Sstevel@tonic-gate 
10072973Sgovinda 	/* Save the pil for this ino */
10082973Sgovinda 	ipil_p->ipil_pil = hdlp->ih_pri;
10090Sstevel@tonic-gate 
10102973Sgovinda 	/* Select cpu, saving it for sharing and removal */
10112973Sgovinda 	if (ipil_list == NULL) {
1012*10053SEvan.Yan@Sun.COM 		if (ino_p->ino_cpuid == -1)
1013*10053SEvan.Yan@Sun.COM 			ino_p->ino_cpuid = intr_dist_cpuid();
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 		/* Enable interrupt */
10160Sstevel@tonic-gate 		px_ib_intr_enable(px_p, ino_p->ino_cpuid, ino);
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate 
10192973Sgovinda ino_done:
1020*10053SEvan.Yan@Sun.COM 	hdlp->ih_target = ino_p->ino_cpuid;
1021*10053SEvan.Yan@Sun.COM 
10222973Sgovinda 	/* Add weight to the cpu that we are already targeting */
10238535Sevan.yan@sun.com 	weight = pci_class_to_intr_weight(rdip);
10240Sstevel@tonic-gate 	intr_dist_cpuid_add_device_weight(ino_p->ino_cpuid, rdip, weight);
10250Sstevel@tonic-gate 
10262973Sgovinda 	ih_p->ih_ipil_p = ipil_p;
102766Sesolom 	px_create_intr_kstats(ih_p);
10280Sstevel@tonic-gate 	if (ih_p->ih_ksp)
10290Sstevel@tonic-gate 		kstat_install(ih_p->ih_ksp);
10300Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: done! Interrupt 0x%x pil=%x\n",
10330Sstevel@tonic-gate 	    ino_p->ino_sysino, hdlp->ih_pri);
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	return (ret);
10360Sstevel@tonic-gate fail2:
10372973Sgovinda 	px_ib_delete_ino_pil(ib_p, ipil_p);
10380Sstevel@tonic-gate fail1:
10390Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
10400Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
10430Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (px_ih_t));
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: Failed! Interrupt 0x%x "
10460Sstevel@tonic-gate 	    "pil=%x\n", ino_p->ino_sysino, hdlp->ih_pri);
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	return (ret);
10490Sstevel@tonic-gate }
10500Sstevel@tonic-gate 
1051693Sgovinda /*
1052693Sgovinda  * px_rem_intx_intr:
1053693Sgovinda  *
1054693Sgovinda  * This function is called to unregister INTx and legacy hardware
1055693Sgovinda  * interrupt pins interrupts.
1056693Sgovinda  */
10570Sstevel@tonic-gate int
10580Sstevel@tonic-gate px_rem_intx_intr(dev_info_t *dip, dev_info_t *rdip,
10590Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
10600Sstevel@tonic-gate {
10610Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
10620Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
10630Sstevel@tonic-gate 	devino_t	ino;
10640Sstevel@tonic-gate 	cpuid_t		curr_cpu;
10652973Sgovinda 	px_ino_t	*ino_p;
10662973Sgovinda 	px_ino_pil_t	*ipil_p;
10670Sstevel@tonic-gate 	px_ih_t		*ih_p;
10680Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	ino = hdlp->ih_vector;
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	DBG(DBG_R_INTX, dip, "px_rem_intx_intr: rdip=%s%d ino=%x\n",
10730Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	ino_p = px_ib_locate_ino(ib_p, ino);
10782973Sgovinda 	ipil_p = px_ib_ino_locate_ipil(ino_p, hdlp->ih_pri);
10792973Sgovinda 	ih_p = px_ib_intr_locate_ih(ipil_p, rdip, hdlp->ih_inum, 0, 0);
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	/* Get the current cpu */
10820Sstevel@tonic-gate 	if ((ret = px_lib_intr_gettarget(px_p->px_dip, ino_p->ino_sysino,
10830Sstevel@tonic-gate 	    &curr_cpu)) != DDI_SUCCESS)
10840Sstevel@tonic-gate 		goto fail;
10850Sstevel@tonic-gate 
10862973Sgovinda 	if ((ret = px_ib_ino_rem_intr(px_p, ipil_p, ih_p)) != DDI_SUCCESS)
10870Sstevel@tonic-gate 		goto fail;
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
10900Sstevel@tonic-gate 
10912973Sgovinda 	if (ipil_p->ipil_ih_size == 0) {
10920Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
10930Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
10940Sstevel@tonic-gate 
10952973Sgovinda 		px_ib_delete_ino_pil(ib_p, ipil_p);
10962973Sgovinda 	}
10972973Sgovinda 
10982973Sgovinda 	if (ino_p->ino_ipil_size == 0) {
10992973Sgovinda 		kmem_free(ino_p, sizeof (px_ino_t));
11000Sstevel@tonic-gate 	} else {
11013780Segillett 		/* Re-enable interrupt only if mapping register still shared */
11023780Segillett 		PX_INTR_ENABLE(px_p->px_dip, ino_p->ino_sysino, curr_cpu);
11030Sstevel@tonic-gate 	}
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate fail:
11060Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
11070Sstevel@tonic-gate 	return (ret);
11080Sstevel@tonic-gate }
11090Sstevel@tonic-gate 
1110693Sgovinda /*
1111693Sgovinda  * px_add_msiq_intr:
1112693Sgovinda  *
1113693Sgovinda  * This function is called to register MSI/Xs and PCIe message interrupts.
1114693Sgovinda  */
11150Sstevel@tonic-gate int
11160Sstevel@tonic-gate px_add_msiq_intr(dev_info_t *dip, dev_info_t *rdip,
11170Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, msiq_rec_type_t rec_type,
1118*10053SEvan.Yan@Sun.COM     msgcode_t msg_code, cpuid_t cpu_id, msiqid_t *msiq_id_p)
11190Sstevel@tonic-gate {
11200Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
11210Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
11220Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &ib_p->ib_msiq_state;
11230Sstevel@tonic-gate 	devino_t	ino;
11240Sstevel@tonic-gate 	px_ih_t		*ih_p;
11252973Sgovinda 	px_ino_t	*ino_p;
11262973Sgovinda 	px_ino_pil_t	*ipil_p, *ipil_list;
11270Sstevel@tonic-gate 	int32_t		weight;
11280Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
11290Sstevel@tonic-gate 
1130*10053SEvan.Yan@Sun.COM 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: rdip=%s%d handler=0x%x "
1131*10053SEvan.Yan@Sun.COM 	    "arg1=0x%x arg2=0x%x cpu=0x%x\n", ddi_driver_name(rdip),
1132*10053SEvan.Yan@Sun.COM 	    ddi_get_instance(rdip), hdlp->ih_cb_func, hdlp->ih_cb_arg1,
1133*10053SEvan.Yan@Sun.COM 	    hdlp->ih_cb_arg2, cpu_id);
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	ih_p = px_ib_alloc_ih(rdip, hdlp->ih_inum, hdlp->ih_cb_func,
11360Sstevel@tonic-gate 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, rec_type, msg_code);
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
11390Sstevel@tonic-gate 
1140*10053SEvan.Yan@Sun.COM 	ret = (cpu_id == -1) ? px_msiq_alloc(px_p, rec_type, msiq_id_p) :
1141*10053SEvan.Yan@Sun.COM 	    px_msiq_alloc_based_on_cpuid(px_p, rec_type, cpu_id, msiq_id_p);
1142*10053SEvan.Yan@Sun.COM 
1143*10053SEvan.Yan@Sun.COM 	if (ret != DDI_SUCCESS) {
1144*10053SEvan.Yan@Sun.COM 		DBG(DBG_MSIQ, dip, "px_add_msiq_intr: "
1145*10053SEvan.Yan@Sun.COM 		    "msiq allocation failed\n");
1146*10053SEvan.Yan@Sun.COM 		goto fail;
1147*10053SEvan.Yan@Sun.COM 	}
1148*10053SEvan.Yan@Sun.COM 
1149*10053SEvan.Yan@Sun.COM 	ino = px_msiqid_to_devino(px_p, *msiq_id_p);
1150*10053SEvan.Yan@Sun.COM 
11512973Sgovinda 	ino_p = px_ib_locate_ino(ib_p, ino);
11522973Sgovinda 	ipil_list = ino_p ? ino_p->ino_ipil_p : NULL;
11532973Sgovinda 
11542973Sgovinda 	/* Sharing ino */
11552973Sgovinda 	if (ino_p && (ipil_p = px_ib_ino_locate_ipil(ino_p, hdlp->ih_pri))) {
11562973Sgovinda 		if (px_ib_intr_locate_ih(ipil_p, rdip,
11572973Sgovinda 		    hdlp->ih_inum, rec_type, msg_code)) {
11580Sstevel@tonic-gate 			DBG(DBG_MSIQ, dip, "px_add_msiq_intr: "
11592973Sgovinda 			    "dup intr #%d\n", hdlp->ih_inum);
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 			ret = DDI_FAILURE;
11620Sstevel@tonic-gate 			goto fail1;
11630Sstevel@tonic-gate 		}
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 		/* Save mondo value in hdlp */
11660Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
11670Sstevel@tonic-gate 
11682973Sgovinda 		if ((ret = px_ib_ino_add_intr(px_p, ipil_p,
11692973Sgovinda 		    ih_p)) != DDI_SUCCESS)
11702973Sgovinda 			goto fail1;
11712973Sgovinda 
11722973Sgovinda 		goto ino_done;
11732973Sgovinda 	}
11742973Sgovinda 
11752973Sgovinda 	if (hdlp->ih_pri == 0)
11768535Sevan.yan@sun.com 		hdlp->ih_pri = pci_class_to_pil(rdip);
11770Sstevel@tonic-gate 
11782973Sgovinda 	ipil_p = px_ib_new_ino_pil(ib_p, ino, hdlp->ih_pri, ih_p);
11792973Sgovinda 	ino_p = ipil_p->ipil_ino_p;
11802973Sgovinda 
11812973Sgovinda 	ino_p->ino_msiq_p = msiq_state_p->msiq_p +
11822973Sgovinda 	    (*msiq_id_p - msiq_state_p->msiq_1st_msiq_id);
11830Sstevel@tonic-gate 
11842973Sgovinda 	/* Save mondo value in hdlp */
11852973Sgovinda 	hdlp->ih_vector = ino_p->ino_sysino;
11862973Sgovinda 
11872973Sgovinda 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: pil=0x%x mondo=0x%x\n",
11882973Sgovinda 	    hdlp->ih_pri, hdlp->ih_vector);
11890Sstevel@tonic-gate 
11902973Sgovinda 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
11912973Sgovinda 	    (ddi_intr_handler_t *)px_msiq_intr, (caddr_t)ipil_p, NULL);
11922973Sgovinda 
11932973Sgovinda 	ret = i_ddi_add_ivintr(hdlp);
11940Sstevel@tonic-gate 
11952973Sgovinda 	/*
11962973Sgovinda 	 * Restore original interrupt handler
11972973Sgovinda 	 * and arguments in interrupt handle.
11982973Sgovinda 	 */
11992973Sgovinda 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
12002973Sgovinda 	    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
12010Sstevel@tonic-gate 
12022973Sgovinda 	if (ret != DDI_SUCCESS)
12032973Sgovinda 		goto fail2;
12042973Sgovinda 
12052973Sgovinda 	/* Save the pil for this ino */
12062973Sgovinda 	ipil_p->ipil_pil = hdlp->ih_pri;
12072973Sgovinda 
12082973Sgovinda 	/* Select cpu, saving it for sharing and removal */
12092973Sgovinda 	if (ipil_list == NULL) {
12100Sstevel@tonic-gate 		/* Enable MSIQ */
12110Sstevel@tonic-gate 		px_lib_msiq_setstate(dip, *msiq_id_p, PCI_MSIQ_STATE_IDLE);
12120Sstevel@tonic-gate 		px_lib_msiq_setvalid(dip, *msiq_id_p, PCI_MSIQ_VALID);
12130Sstevel@tonic-gate 
1214*10053SEvan.Yan@Sun.COM 		if (ino_p->ino_cpuid == -1)
1215*10053SEvan.Yan@Sun.COM 			ino_p->ino_cpuid = intr_dist_cpuid();
1216*10053SEvan.Yan@Sun.COM 
12170Sstevel@tonic-gate 		/* Enable interrupt */
12182973Sgovinda 		px_ib_intr_enable(px_p, ino_p->ino_cpuid, ino);
12190Sstevel@tonic-gate 	}
12200Sstevel@tonic-gate 
12212973Sgovinda ino_done:
1222*10053SEvan.Yan@Sun.COM 	hdlp->ih_target = ino_p->ino_cpuid;
1223*10053SEvan.Yan@Sun.COM 
12242973Sgovinda 	/* Add weight to the cpu that we are already targeting */
12258535Sevan.yan@sun.com 	weight = pci_class_to_intr_weight(rdip);
12260Sstevel@tonic-gate 	intr_dist_cpuid_add_device_weight(ino_p->ino_cpuid, rdip, weight);
12270Sstevel@tonic-gate 
12282973Sgovinda 	ih_p->ih_ipil_p = ipil_p;
122966Sesolom 	px_create_intr_kstats(ih_p);
12300Sstevel@tonic-gate 	if (ih_p->ih_ksp)
12310Sstevel@tonic-gate 		kstat_install(ih_p->ih_ksp);
12320Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: done! Interrupt 0x%x pil=%x\n",
12350Sstevel@tonic-gate 	    ino_p->ino_sysino, hdlp->ih_pri);
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	return (ret);
12380Sstevel@tonic-gate fail2:
12392973Sgovinda 	px_ib_delete_ino_pil(ib_p, ipil_p);
12400Sstevel@tonic-gate fail1:
1241*10053SEvan.Yan@Sun.COM 	(void) px_msiq_free(px_p, *msiq_id_p);
1242*10053SEvan.Yan@Sun.COM fail:
12430Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
12440Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
12470Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (px_ih_t));
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: Failed! Interrupt 0x%x pil=%x\n",
12500Sstevel@tonic-gate 	    ino_p->ino_sysino, hdlp->ih_pri);
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	return (ret);
12530Sstevel@tonic-gate }
12540Sstevel@tonic-gate 
1255693Sgovinda /*
1256693Sgovinda  * px_rem_msiq_intr:
1257693Sgovinda  *
1258693Sgovinda  * This function is called to unregister MSI/Xs and PCIe message interrupts.
1259693Sgovinda  */
12600Sstevel@tonic-gate int
12610Sstevel@tonic-gate px_rem_msiq_intr(dev_info_t *dip, dev_info_t *rdip,
12620Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, msiq_rec_type_t rec_type,
12630Sstevel@tonic-gate     msgcode_t msg_code, msiqid_t msiq_id)
12640Sstevel@tonic-gate {
12650Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
12660Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
12670Sstevel@tonic-gate 	devino_t	ino = px_msiqid_to_devino(px_p, msiq_id);
12680Sstevel@tonic-gate 	cpuid_t		curr_cpu;
12692973Sgovinda 	px_ino_t	*ino_p;
12702973Sgovinda 	px_ino_pil_t	*ipil_p;
12710Sstevel@tonic-gate 	px_ih_t		*ih_p;
12720Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_rem_msiq_intr: rdip=%s%d msiq_id=%x ino=%x\n",
12750Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), msiq_id, ino);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	ino_p = px_ib_locate_ino(ib_p, ino);
12802973Sgovinda 	ipil_p = px_ib_ino_locate_ipil(ino_p, hdlp->ih_pri);
12812973Sgovinda 	ih_p = px_ib_intr_locate_ih(ipil_p, rdip, hdlp->ih_inum, rec_type,
12822973Sgovinda 	    msg_code);
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	/* Get the current cpu */
12850Sstevel@tonic-gate 	if ((ret = px_lib_intr_gettarget(px_p->px_dip, ino_p->ino_sysino,
12860Sstevel@tonic-gate 	    &curr_cpu)) != DDI_SUCCESS)
12870Sstevel@tonic-gate 		goto fail;
12880Sstevel@tonic-gate 
12892973Sgovinda 	if ((ret = px_ib_ino_rem_intr(px_p, ipil_p, ih_p)) != DDI_SUCCESS)
12900Sstevel@tonic-gate 		goto fail;
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
12930Sstevel@tonic-gate 
12942973Sgovinda 	if (ipil_p->ipil_ih_size == 0) {
12950Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
12960Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
12970Sstevel@tonic-gate 
12982973Sgovinda 		px_ib_delete_ino_pil(ib_p, ipil_p);
12992973Sgovinda 
13002973Sgovinda 		if (ino_p->ino_ipil_size == 0)
13012973Sgovinda 			px_lib_msiq_setvalid(dip,
13022973Sgovinda 			    px_devino_to_msiqid(px_p, ino), PCI_MSIQ_INVALID);
13032973Sgovinda 	}
13042973Sgovinda 
1305*10053SEvan.Yan@Sun.COM 	(void) px_msiq_free(px_p, msiq_id);
1306*10053SEvan.Yan@Sun.COM 
1307*10053SEvan.Yan@Sun.COM 	if (ino_p->ino_ipil_size) {
13083780Segillett 		/* Re-enable interrupt only if mapping register still shared */
13093780Segillett 		PX_INTR_ENABLE(px_p->px_dip, ino_p->ino_sysino, curr_cpu);
13100Sstevel@tonic-gate 	}
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate fail:
13130Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
13140Sstevel@tonic-gate 	return (ret);
13150Sstevel@tonic-gate }
1316