xref: /illumos-gate/usr/src/uts/sun4/io/px/px_intr.c (revision 5febcb4a)
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 /*
22614edcaeSEvan Yan  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * PX nexus interrupt handling:
287c478bd9Sstevel@tonic-gate  *	PX device interrupt handler wrapper
297c478bd9Sstevel@tonic-gate  *	PIL lookup routine
307c478bd9Sstevel@tonic-gate  *	PX device interrupt related initchild code
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/async.h>
367c478bd9Sstevel@tonic-gate #include <sys/spl.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
38f8d2de6bSjchu #include <sys/fm/protocol.h>
39f8d2de6bSjchu #include <sys/fm/util.h>
407c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>	/* e_ddi_nodeid_to_dip() */
417c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
427c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
437c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
447c478bd9Sstevel@tonic-gate #include "px_obj.h"
45f8d2de6bSjchu #include <sys/ontrap.h>
46f8d2de6bSjchu #include <sys/membar.h>
476d44af1bSesolom #include <sys/clock.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * interrupt jabber:
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * When an interrupt line is jabbering, every time the state machine for the
537c478bd9Sstevel@tonic-gate  * associated ino is idled, a new mondo will be sent and the ino will go into
547c478bd9Sstevel@tonic-gate  * the pending state again. The mondo will cause a new call to
557c478bd9Sstevel@tonic-gate  * px_intr_wrapper() which normally idles the ino's state machine which would
567c478bd9Sstevel@tonic-gate  * precipitate another trip round the loop.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * The loop can be broken by preventing the ino's state machine from being
597c478bd9Sstevel@tonic-gate  * idled when an interrupt line is jabbering. See the comment at the
607c478bd9Sstevel@tonic-gate  * beginning of px_intr_wrapper() explaining how the 'interrupt jabber
617c478bd9Sstevel@tonic-gate  * protection' code does this.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * If the unclaimed interrupt count has reached the limit set by
687c478bd9Sstevel@tonic-gate  * pci_unclaimed_intr_max within the time limit, then all interrupts
697c478bd9Sstevel@tonic-gate  * on this ino is blocked by not idling the interrupt state machine.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate static int
72b0fc0e77Sgovinda px_spurintr(px_ino_pil_t *ipil_p)
737c478bd9Sstevel@tonic-gate {
74b0fc0e77Sgovinda 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
75b0fc0e77Sgovinda 	px_ih_t		*ih_p = ipil_p->ipil_ih_start;
76b0fc0e77Sgovinda 	px_t		*px_p = ino_p->ino_ib_p->ib_px_p;
77b0fc0e77Sgovinda 	char		*err_fmt_str;
78b0fc0e77Sgovinda 	boolean_t	blocked = B_FALSE;
79b0fc0e77Sgovinda 	int		i;
807c478bd9Sstevel@tonic-gate 
81b0fc0e77Sgovinda 	if (ino_p->ino_unclaimed_intrs > px_unclaimed_intr_max)
827c478bd9Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
837c478bd9Sstevel@tonic-gate 
84b0fc0e77Sgovinda 	if (!ino_p->ino_unclaimed_intrs)
857c478bd9Sstevel@tonic-gate 		ino_p->ino_spurintr_begin = ddi_get_lbolt();
867c478bd9Sstevel@tonic-gate 
87b0fc0e77Sgovinda 	ino_p->ino_unclaimed_intrs++;
887c478bd9Sstevel@tonic-gate 
89b0fc0e77Sgovinda 	if (ino_p->ino_unclaimed_intrs <= 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) {
94b0fc0e77Sgovinda 		ino_p->ino_unclaimed_intrs = 0;
957c478bd9Sstevel@tonic-gate 		goto clear;
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 	err_fmt_str = "%s%d: ino 0x%x blocked";
98b0fc0e77Sgovinda 	blocked = B_TRUE;
997c478bd9Sstevel@tonic-gate 	goto warn;
1007c478bd9Sstevel@tonic-gate clear:
1017c478bd9Sstevel@tonic-gate 	err_fmt_str = "!%s%d: spurious interrupt from ino 0x%x";
1027c478bd9Sstevel@tonic-gate warn:
1037c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, err_fmt_str, NAMEINST(px_p->px_dip), ino_p->ino_ino);
104b0fc0e77Sgovinda 	for (i = 0; i < ipil_p->ipil_ih_size; i++, ih_p = ih_p->ih_next)
1057c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "!%s-%d#%x ", NAMEINST(ih_p->ih_dip),
1067c478bd9Sstevel@tonic-gate 		    ih_p->ih_inum);
1077c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "!\n");
108b0fc0e77Sgovinda 
109b0fc0e77Sgovinda 	/* Clear the pending state */
110b0fc0e77Sgovinda 	if (blocked == B_FALSE) {
111b0fc0e77Sgovinda 		if (px_lib_intr_setstate(px_p->px_dip, ino_p->ino_sysino,
112b0fc0e77Sgovinda 		    INTR_IDLE_STATE) != DDI_SUCCESS)
113b0fc0e77Sgovinda 			return (DDI_INTR_UNCLAIMED);
114b0fc0e77Sgovinda 	}
115b0fc0e77Sgovinda 
1167c478bd9Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate extern uint64_t intr_get_time(void);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
122a195726fSgovinda  * px_intx_intr (INTx or legacy interrupt handler)
1237c478bd9Sstevel@tonic-gate  *
1247c478bd9Sstevel@tonic-gate  * This routine is used as wrapper around interrupt handlers installed by child
1257c478bd9Sstevel@tonic-gate  * device drivers.  This routine invokes the driver interrupt handlers and
1267c478bd9Sstevel@tonic-gate  * examines the return codes.
1277c478bd9Sstevel@tonic-gate  *
1287c478bd9Sstevel@tonic-gate  * There is a count of unclaimed interrupts kept on a per-ino basis. If at
1297c478bd9Sstevel@tonic-gate  * least one handler claims the interrupt then the counter is halved and the
1307c478bd9Sstevel@tonic-gate  * interrupt state machine is idled. If no handler claims the interrupt then
1317c478bd9Sstevel@tonic-gate  * the counter is incremented by one and the state machine is idled.
1327c478bd9Sstevel@tonic-gate  * If the count ever reaches the limit value set by pci_unclaimed_intr_max
1337c478bd9Sstevel@tonic-gate  * then the interrupt state machine is not idled thus preventing any further
1347c478bd9Sstevel@tonic-gate  * interrupts on that ino. The state machine will only be idled again if a
1357c478bd9Sstevel@tonic-gate  * handler is subsequently added or removed.
1367c478bd9Sstevel@tonic-gate  *
1377c478bd9Sstevel@tonic-gate  * return value: DDI_INTR_CLAIMED if any handlers claimed the interrupt,
1387c478bd9Sstevel@tonic-gate  * DDI_INTR_UNCLAIMED otherwise.
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate uint_t
1417c478bd9Sstevel@tonic-gate px_intx_intr(caddr_t arg)
1427c478bd9Sstevel@tonic-gate {
143b0fc0e77Sgovinda 	px_ino_pil_t	*ipil_p = (px_ino_pil_t *)arg;
144b0fc0e77Sgovinda 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
1457c478bd9Sstevel@tonic-gate 	px_t		*px_p = ino_p->ino_ib_p->ib_px_p;
146b0fc0e77Sgovinda 	px_ih_t		*ih_p = ipil_p->ipil_ih_start;
147b0fc0e77Sgovinda 	ushort_t	pil = ipil_p->ipil_pil;
148b0fc0e77Sgovinda 	uint_t		result = 0, r = DDI_INTR_UNCLAIMED;
1497c478bd9Sstevel@tonic-gate 	int		i;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	DBG(DBG_INTX_INTR, px_p->px_dip, "px_intx_intr:"
1527c478bd9Sstevel@tonic-gate 	    "ino=%x sysino=%llx pil=%x ih_size=%x ih_lst=%x\n",
153b0fc0e77Sgovinda 	    ino_p->ino_ino, ino_p->ino_sysino, ipil_p->ipil_pil,
154b0fc0e77Sgovinda 	    ipil_p->ipil_ih_size, ipil_p->ipil_ih_head);
1557c478bd9Sstevel@tonic-gate 
156b0fc0e77Sgovinda 	for (i = 0; i < ipil_p->ipil_ih_size; i++, ih_p = ih_p->ih_next) {
1577c478bd9Sstevel@tonic-gate 		dev_info_t *dip = ih_p->ih_dip;
1587c478bd9Sstevel@tonic-gate 		uint_t (*handler)() = ih_p->ih_handler;
1597c478bd9Sstevel@tonic-gate 		caddr_t arg1 = ih_p->ih_handler_arg1;
1607c478bd9Sstevel@tonic-gate 		caddr_t arg2 = ih_p->ih_handler_arg2;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 		if (ih_p->ih_intr_state == PX_INTR_STATE_DISABLE) {
1637c478bd9Sstevel@tonic-gate 			DBG(DBG_INTX_INTR, px_p->px_dip,
1647c478bd9Sstevel@tonic-gate 			    "px_intx_intr: %s%d interrupt %d is disabled\n",
1657c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
1667c478bd9Sstevel@tonic-gate 			    ino_p->ino_ino);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 			continue;
1697c478bd9Sstevel@tonic-gate 		}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 		DBG(DBG_INTX_INTR, px_p->px_dip, "px_intx_intr:"
1727c478bd9Sstevel@tonic-gate 		    "ino=%x handler=%p arg1 =%p arg2 = %p\n",
1737c478bd9Sstevel@tonic-gate 		    ino_p->ino_ino, handler, arg1, arg2);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
1767c478bd9Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, caddr_t, arg2);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 		r = (*handler)(arg1, arg2);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		/*
1817c478bd9Sstevel@tonic-gate 		 * Account for time used by this interrupt. Protect against
1827c478bd9Sstevel@tonic-gate 		 * conflicting writes to ih_ticks from ib_intr_dist_all() by
1837c478bd9Sstevel@tonic-gate 		 * using atomic ops.
1847c478bd9Sstevel@tonic-gate 		 */
1857c478bd9Sstevel@tonic-gate 
186b0fc0e77Sgovinda 		if (pil <= LOCK_LEVEL)
1877c478bd9Sstevel@tonic-gate 			atomic_add_64(&ih_p->ih_ticks, intr_get_time());
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
1907c478bd9Sstevel@tonic-gate 		    void *, handler, caddr_t, arg1, int, r);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		result += r;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		if (px_check_all_handlers)
1957c478bd9Sstevel@tonic-gate 			continue;
1967c478bd9Sstevel@tonic-gate 		if (result)
1977c478bd9Sstevel@tonic-gate 			break;
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
200b0fc0e77Sgovinda 	if (result)
201b0fc0e77Sgovinda 		ino_p->ino_claimed |= (1 << pil);
202b0fc0e77Sgovinda 
203b0fc0e77Sgovinda 	/* Interrupt can only be cleared after all pil levels are handled */
204b0fc0e77Sgovinda 	if (pil != ino_p->ino_lopil)
205b0fc0e77Sgovinda 		return (DDI_INTR_CLAIMED);
206b0fc0e77Sgovinda 
207b0fc0e77Sgovinda 	if (!ino_p->ino_claimed) {
208b0fc0e77Sgovinda 		if (px_unclaimed_intr_block)
209b0fc0e77Sgovinda 			return (px_spurintr(ipil_p));
210b0fc0e77Sgovinda 	}
2117c478bd9Sstevel@tonic-gate 
212b0fc0e77Sgovinda 	ino_p->ino_unclaimed_intrs = 0;
213b0fc0e77Sgovinda 	ino_p->ino_claimed = 0;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/* Clear the pending state */
216b0fc0e77Sgovinda 	if (px_lib_intr_setstate(px_p->px_dip,
2177c478bd9Sstevel@tonic-gate 	    ino_p->ino_sysino, INTR_IDLE_STATE) != DDI_SUCCESS)
2187c478bd9Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
224a195726fSgovinda  * px_msiq_intr (MSI/X or PCIe MSG interrupt handler)
2257c478bd9Sstevel@tonic-gate  *
2267c478bd9Sstevel@tonic-gate  * This routine is used as wrapper around interrupt handlers installed by child
2277c478bd9Sstevel@tonic-gate  * device drivers.  This routine invokes the driver interrupt handlers and
2287c478bd9Sstevel@tonic-gate  * examines the return codes.
2297c478bd9Sstevel@tonic-gate  *
2307c478bd9Sstevel@tonic-gate  * There is a count of unclaimed interrupts kept on a per-ino basis. If at
2317c478bd9Sstevel@tonic-gate  * least one handler claims the interrupt then the counter is halved and the
2327c478bd9Sstevel@tonic-gate  * interrupt state machine is idled. If no handler claims the interrupt then
2337c478bd9Sstevel@tonic-gate  * the counter is incremented by one and the state machine is idled.
2347c478bd9Sstevel@tonic-gate  * If the count ever reaches the limit value set by pci_unclaimed_intr_max
2357c478bd9Sstevel@tonic-gate  * then the interrupt state machine is not idled thus preventing any further
2367c478bd9Sstevel@tonic-gate  * interrupts on that ino. The state machine will only be idled again if a
2377c478bd9Sstevel@tonic-gate  * handler is subsequently added or removed.
2387c478bd9Sstevel@tonic-gate  *
2397c478bd9Sstevel@tonic-gate  * return value: DDI_INTR_CLAIMED if any handlers claimed the interrupt,
2407c478bd9Sstevel@tonic-gate  * DDI_INTR_UNCLAIMED otherwise.
2417c478bd9Sstevel@tonic-gate  */
2427c478bd9Sstevel@tonic-gate uint_t
2437c478bd9Sstevel@tonic-gate px_msiq_intr(caddr_t arg)
2447c478bd9Sstevel@tonic-gate {
245b0fc0e77Sgovinda 	px_ino_pil_t	*ipil_p = (px_ino_pil_t *)arg;
246b0fc0e77Sgovinda 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
2477c478bd9Sstevel@tonic-gate 	px_t		*px_p = ino_p->ino_ib_p->ib_px_p;
2487c478bd9Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
2497c478bd9Sstevel@tonic-gate 	px_msiq_t	*msiq_p = ino_p->ino_msiq_p;
2507c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = px_p->px_dip;
251b0fc0e77Sgovinda 	ushort_t	pil = ipil_p->ipil_pil;
2527c478bd9Sstevel@tonic-gate 	msiq_rec_t	msiq_rec, *msiq_rec_p = &msiq_rec;
253023ccc1eSegillett 	msiqhead_t	*curr_head_p;
254023ccc1eSegillett 	msiqtail_t	curr_tail_index;
2557c478bd9Sstevel@tonic-gate 	msgcode_t	msg_code;
2567c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
257b0fc0e77Sgovinda 	uint_t		ret = DDI_INTR_UNCLAIMED;
258b0fc0e77Sgovinda 	int		i, j;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: msiq_id =%x ino=%x pil=%x "
2617c478bd9Sstevel@tonic-gate 	    "ih_size=%x ih_lst=%x\n", msiq_p->msiq_id, ino_p->ino_ino,
262b0fc0e77Sgovinda 	    ipil_p->ipil_pil, ipil_p->ipil_ih_size, ipil_p->ipil_ih_head);
263b0fc0e77Sgovinda 
264b0fc0e77Sgovinda 	/*
265b0fc0e77Sgovinda 	 * The px_msiq_intr() handles multiple interrupt priorities and it
266b0fc0e77Sgovinda 	 * will set msiq->msiq_rec2process to the number of MSIQ records to
267b0fc0e77Sgovinda 	 * process while handling the highest priority interrupt. Subsequent
268b0fc0e77Sgovinda 	 * lower priority interrupts will just process any unprocessed MSIQ
269b0fc0e77Sgovinda 	 * records or will just return immediately.
270b0fc0e77Sgovinda 	 */
271b0fc0e77Sgovinda 	if (msiq_p->msiq_recs2process == 0) {
272b0fc0e77Sgovinda 		/* Read current MSIQ tail index */
273b0fc0e77Sgovinda 		px_lib_msiq_gettail(dip, msiq_p->msiq_id, &curr_tail_index);
274b0fc0e77Sgovinda 		msiq_p->msiq_new_head_index = msiq_p->msiq_curr_head_index;
275b0fc0e77Sgovinda 
276b0fc0e77Sgovinda 		if (curr_tail_index < msiq_p->msiq_curr_head_index)
277b0fc0e77Sgovinda 			curr_tail_index += msiq_state_p->msiq_rec_cnt;
2787c478bd9Sstevel@tonic-gate 
279b0fc0e77Sgovinda 		msiq_p->msiq_recs2process = curr_tail_index -
280b0fc0e77Sgovinda 		    msiq_p->msiq_curr_head_index;
281b0fc0e77Sgovinda 	}
282b0fc0e77Sgovinda 
283b0fc0e77Sgovinda 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: curr_head %x new_head %x "
284b0fc0e77Sgovinda 	    "rec2process %x\n", msiq_p->msiq_curr_head_index,
285b0fc0e77Sgovinda 	    msiq_p->msiq_new_head_index, msiq_p->msiq_recs2process);
2867c478bd9Sstevel@tonic-gate 
287b0fc0e77Sgovinda 	/* If all MSIQ records are already processed, just return immediately */
288b0fc0e77Sgovinda 	if ((msiq_p->msiq_new_head_index - msiq_p->msiq_curr_head_index)
289b0fc0e77Sgovinda 	    == msiq_p->msiq_recs2process)
290b0fc0e77Sgovinda 		goto intr_done;
291b0fc0e77Sgovinda 
292b0fc0e77Sgovinda 	curr_head_p = (msiqhead_t *)((caddr_t)msiq_p->msiq_base_p +
293b0fc0e77Sgovinda 	    (msiq_p->msiq_curr_head_index * sizeof (msiq_rec_t)));
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	/*
296023ccc1eSegillett 	 * Calculate the number of recs to process by taking the difference
297023ccc1eSegillett 	 * between the head and tail pointers. For all records we always
298023ccc1eSegillett 	 * verify that we have a valid record type before we do any processing.
299b0fc0e77Sgovinda 	 * If triggered, we should always have at least one valid record.
3007c478bd9Sstevel@tonic-gate 	 */
301b0fc0e77Sgovinda 	for (i = 0; i < msiq_p->msiq_recs2process; i++) {
302b0fc0e77Sgovinda 		/* Read next MSIQ record */
303023ccc1eSegillett 		px_lib_get_msiq_rec(dip, curr_head_p, msiq_rec_p);
304023ccc1eSegillett 
3057c478bd9Sstevel@tonic-gate 		DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: MSIQ RECORD, "
3067c478bd9Sstevel@tonic-gate 		    "msiq_rec_type 0x%llx msiq_rec_rid 0x%llx\n",
3077c478bd9Sstevel@tonic-gate 		    msiq_rec_p->msiq_rec_type, msiq_rec_p->msiq_rec_rid);
3087c478bd9Sstevel@tonic-gate 
309023ccc1eSegillett 		if (!msiq_rec_p->msiq_rec_type)
310b0fc0e77Sgovinda 			goto next_rec;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 		/* Check MSIQ record type */
3137c478bd9Sstevel@tonic-gate 		switch (msiq_rec_p->msiq_rec_type) {
3147c478bd9Sstevel@tonic-gate 		case MSG_REC:
3157c478bd9Sstevel@tonic-gate 			msg_code = msiq_rec_p->msiq_rec_data.msg.msg_code;
3167c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: PCIE MSG "
3177c478bd9Sstevel@tonic-gate 			    "record, msg type 0x%x\n", msg_code);
3187c478bd9Sstevel@tonic-gate 			break;
3197c478bd9Sstevel@tonic-gate 		case MSI32_REC:
3207c478bd9Sstevel@tonic-gate 		case MSI64_REC:
3217c478bd9Sstevel@tonic-gate 			msg_code = msiq_rec_p->msiq_rec_data.msi.msi_data;
3227c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: MSI record, "
3237c478bd9Sstevel@tonic-gate 			    "msi 0x%x\n", msg_code);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 			/* Clear MSI state */
3267c478bd9Sstevel@tonic-gate 			px_lib_msi_setstate(dip, (msinum_t)msg_code,
3277c478bd9Sstevel@tonic-gate 			    PCI_MSI_STATE_IDLE);
3287c478bd9Sstevel@tonic-gate 			break;
3297c478bd9Sstevel@tonic-gate 		default:
3307c478bd9Sstevel@tonic-gate 			msg_code = 0;
3317c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: px_msiq_intr: 0x%x MSIQ "
3327c478bd9Sstevel@tonic-gate 			    "record type is not supported",
3337c478bd9Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip),
3347c478bd9Sstevel@tonic-gate 			    msiq_rec_p->msiq_rec_type);
335b0fc0e77Sgovinda 
3367c478bd9Sstevel@tonic-gate 			goto next_rec;
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		/*
3407c478bd9Sstevel@tonic-gate 		 * Scan through px_ih_t linked list, searching for the
3417c478bd9Sstevel@tonic-gate 		 * right px_ih_t, matching MSIQ record data.
3427c478bd9Sstevel@tonic-gate 		 */
343b0fc0e77Sgovinda 		for (j = 0, ih_p = ipil_p->ipil_ih_start;
344b0fc0e77Sgovinda 		    ih_p && (j < ipil_p->ipil_ih_size) &&
34507f14c08Sgovinda 		    ((ih_p->ih_msg_code != msg_code) ||
34607f14c08Sgovinda 		    (ih_p->ih_rec_type != msiq_rec_p->msiq_rec_type));
3472917a9c9Sschwartz 		    ih_p = ih_p->ih_next, j++)
3482917a9c9Sschwartz 			;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 		if ((ih_p->ih_msg_code == msg_code) &&
3517c478bd9Sstevel@tonic-gate 		    (ih_p->ih_rec_type == msiq_rec_p->msiq_rec_type)) {
3527c478bd9Sstevel@tonic-gate 			dev_info_t *dip = ih_p->ih_dip;
3537c478bd9Sstevel@tonic-gate 			uint_t (*handler)() = ih_p->ih_handler;
3547c478bd9Sstevel@tonic-gate 			caddr_t arg1 = ih_p->ih_handler_arg1;
3557c478bd9Sstevel@tonic-gate 			caddr_t arg2 = ih_p->ih_handler_arg2;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: ino=%x data=%x "
3587c478bd9Sstevel@tonic-gate 			    "handler=%p arg1 =%p arg2=%p\n", ino_p->ino_ino,
3597c478bd9Sstevel@tonic-gate 			    msg_code, handler, arg1, arg2);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 			DTRACE_PROBE4(interrupt__start, dev_info_t, dip,
3627c478bd9Sstevel@tonic-gate 			    void *, handler, caddr_t, arg1, caddr_t, arg2);
3637c478bd9Sstevel@tonic-gate 
364f8d2de6bSjchu 			/*
365f8d2de6bSjchu 			 * Special case for PCIE Error Messages.
366f8d2de6bSjchu 			 * The current frame work doesn't fit PCIE Err Msgs
367f8d2de6bSjchu 			 * This should be fixed when PCIE MESSAGES as a whole
368f8d2de6bSjchu 			 * is architected correctly.
369f8d2de6bSjchu 			 */
370f8d2de6bSjchu 			if ((msg_code == PCIE_MSG_CODE_ERR_COR) ||
371f8d2de6bSjchu 			    (msg_code == PCIE_MSG_CODE_ERR_NONFATAL) ||
372f8d2de6bSjchu 			    (msg_code == PCIE_MSG_CODE_ERR_FATAL)) {
373f8d2de6bSjchu 				ret = px_err_fabric_intr(px_p, msg_code,
374f8d2de6bSjchu 				    msiq_rec_p->msiq_rec_rid);
375f8d2de6bSjchu 			} else
376f8d2de6bSjchu 				ret = (*handler)(arg1, arg2);
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 			/*
3797c478bd9Sstevel@tonic-gate 			 * Account for time used by this interrupt. Protect
3807c478bd9Sstevel@tonic-gate 			 * against conflicting writes to ih_ticks from
3817c478bd9Sstevel@tonic-gate 			 * ib_intr_dist_all() by using atomic ops.
3827c478bd9Sstevel@tonic-gate 			 */
3837c478bd9Sstevel@tonic-gate 
384b0fc0e77Sgovinda 			if (pil <= LOCK_LEVEL)
3857c478bd9Sstevel@tonic-gate 				atomic_add_64(&ih_p->ih_ticks, intr_get_time());
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 			DTRACE_PROBE4(interrupt__complete, dev_info_t, dip,
3887c478bd9Sstevel@tonic-gate 			    void *, handler, caddr_t, arg1, int, ret);
389023ccc1eSegillett 
390b0fc0e77Sgovinda 			msiq_p->msiq_new_head_index++;
391b0fc0e77Sgovinda 			px_lib_clr_msiq_rec(dip, curr_head_p);
3927c478bd9Sstevel@tonic-gate 		} else {
3937c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr:"
394023ccc1eSegillett 			    "No matching MSIQ record found\n");
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate next_rec:
397023ccc1eSegillett 		/* Get the pointer next EQ record */
398023ccc1eSegillett 		curr_head_p = (msiqhead_t *)
399023ccc1eSegillett 		    ((caddr_t)curr_head_p + sizeof (msiq_rec_t));
400023ccc1eSegillett 
401023ccc1eSegillett 		/* Check for overflow condition */
402023ccc1eSegillett 		if (curr_head_p >= (msiqhead_t *)((caddr_t)msiq_p->msiq_base_p
403b0fc0e77Sgovinda 		    + (msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t))))
404023ccc1eSegillett 			curr_head_p = (msiqhead_t *)msiq_p->msiq_base_p;
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 
407b0fc0e77Sgovinda 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: No of MSIQ recs processed %x\n",
408b0fc0e77Sgovinda 	    (msiq_p->msiq_new_head_index - msiq_p->msiq_curr_head_index));
409023ccc1eSegillett 
410b0fc0e77Sgovinda 	DBG(DBG_MSIQ_INTR, dip, "px_msiq_intr: curr_head %x new_head %x "
411b0fc0e77Sgovinda 	    "rec2process %x\n", msiq_p->msiq_curr_head_index,
412b0fc0e77Sgovinda 	    msiq_p->msiq_new_head_index, msiq_p->msiq_recs2process);
413b0fc0e77Sgovinda 
414b0fc0e77Sgovinda 	/* ino_claimed used just for debugging purpose */
415b0fc0e77Sgovinda 	if (ret)
416b0fc0e77Sgovinda 		ino_p->ino_claimed |= (1 << pil);
417b0fc0e77Sgovinda 
418b0fc0e77Sgovinda intr_done:
419b0fc0e77Sgovinda 	/* Interrupt can only be cleared after all pil levels are handled */
420b0fc0e77Sgovinda 	if (pil != ino_p->ino_lopil)
421b0fc0e77Sgovinda 		return (DDI_INTR_CLAIMED);
422b0fc0e77Sgovinda 
423b0fc0e77Sgovinda 	if (msiq_p->msiq_new_head_index <= msiq_p->msiq_curr_head_index)  {
424b0fc0e77Sgovinda 		if (px_unclaimed_intr_block)
425b0fc0e77Sgovinda 			return (px_spurintr(ipil_p));
426023ccc1eSegillett 	}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/*  Update MSIQ head index with no of MSIQ records processed */
429b0fc0e77Sgovinda 	if (msiq_p->msiq_new_head_index >= msiq_state_p->msiq_rec_cnt)
430b0fc0e77Sgovinda 		msiq_p->msiq_new_head_index -= msiq_state_p->msiq_rec_cnt;
4317c478bd9Sstevel@tonic-gate 
432b0fc0e77Sgovinda 	msiq_p->msiq_curr_head_index = msiq_p->msiq_new_head_index;
433b0fc0e77Sgovinda 	px_lib_msiq_sethead(dip, msiq_p->msiq_id, msiq_p->msiq_new_head_index);
434b0fc0e77Sgovinda 
435b0fc0e77Sgovinda 	msiq_p->msiq_new_head_index = 0;
436b0fc0e77Sgovinda 	msiq_p->msiq_recs2process = 0;
437b0fc0e77Sgovinda 	ino_p->ino_claimed = 0;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	/* Clear the pending state */
4407c478bd9Sstevel@tonic-gate 	if (px_lib_intr_setstate(dip, ino_p->ino_sysino,
4417c478bd9Sstevel@tonic-gate 	    INTR_IDLE_STATE) != DDI_SUCCESS)
4427c478bd9Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate dev_info_t *
4487c478bd9Sstevel@tonic-gate px_get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate 	dev_info_t	*cdip = rdip;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
4537c478bd9Sstevel@tonic-gate 		;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	return (cdip);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate /* ARGSUSED */
4597c478bd9Sstevel@tonic-gate int
4607c478bd9Sstevel@tonic-gate px_intx_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
4617c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
4627c478bd9Sstevel@tonic-gate {
463a195726fSgovinda 	px_t	*px_p = DIP_TO_STATE(dip);
464a195726fSgovinda 	int	ret = DDI_SUCCESS;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	DBG(DBG_INTROPS, dip, "px_intx_ops: dip=%x rdip=%x intr_op=%x "
4677c478bd9Sstevel@tonic-gate 	    "handle=%p\n", dip, rdip, intr_op, hdlp);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	switch (intr_op) {
4707c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
4717c478bd9Sstevel@tonic-gate 		ret = pci_intx_get_cap(rdip, (int *)result);
4727c478bd9Sstevel@tonic-gate 		break;
4737c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETCAP:
4747c478bd9Sstevel@tonic-gate 		DBG(DBG_INTROPS, dip, "px_intx_ops: SetCap is not supported\n");
4757c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
4767c478bd9Sstevel@tonic-gate 		break;
4777c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ALLOC:
4787c478bd9Sstevel@tonic-gate 		*(int *)result = hdlp->ih_scratch1;
4797c478bd9Sstevel@tonic-gate 		break;
4807c478bd9Sstevel@tonic-gate 	case DDI_INTROP_FREE:
4817c478bd9Sstevel@tonic-gate 		break;
4827c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
483a195726fSgovinda 		*(int *)result = hdlp->ih_pri ?
484614edcaeSEvan Yan 		    hdlp->ih_pri : pci_class_to_pil(rdip);
4857c478bd9Sstevel@tonic-gate 		break;
4867c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETPRI:
4877c478bd9Sstevel@tonic-gate 		break;
4887c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
4897c478bd9Sstevel@tonic-gate 		ret = px_add_intx_intr(dip, rdip, hdlp);
4907c478bd9Sstevel@tonic-gate 		break;
4917c478bd9Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
4927c478bd9Sstevel@tonic-gate 		ret = px_rem_intx_intr(dip, rdip, hdlp);
4937c478bd9Sstevel@tonic-gate 		break;
4947c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ENABLE:
4957c478bd9Sstevel@tonic-gate 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
496b0fc0e77Sgovinda 		    hdlp->ih_vector, hdlp->ih_pri, PX_INTR_STATE_ENABLE, 0, 0);
4977c478bd9Sstevel@tonic-gate 		break;
4987c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
4997c478bd9Sstevel@tonic-gate 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
500b0fc0e77Sgovinda 		    hdlp->ih_vector, hdlp->ih_pri, PX_INTR_STATE_DISABLE, 0, 0);
5017c478bd9Sstevel@tonic-gate 		break;
5027c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETMASK:
5037c478bd9Sstevel@tonic-gate 		ret = pci_intx_set_mask(rdip);
5047c478bd9Sstevel@tonic-gate 		break;
5057c478bd9Sstevel@tonic-gate 	case DDI_INTROP_CLRMASK:
5067c478bd9Sstevel@tonic-gate 		ret = pci_intx_clr_mask(rdip);
5077c478bd9Sstevel@tonic-gate 		break;
5087c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPENDING:
5097c478bd9Sstevel@tonic-gate 		ret = pci_intx_get_pending(rdip, (int *)result);
5107c478bd9Sstevel@tonic-gate 		break;
5117c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
5127c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NAVAIL:
513a54f81fbSanish 		*(int *)result = i_ddi_get_intx_nintrs(rdip);
5147c478bd9Sstevel@tonic-gate 		break;
5157c478bd9Sstevel@tonic-gate 	default:
5167c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
5177c478bd9Sstevel@tonic-gate 		break;
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	return (ret);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate /* ARGSUSED */
5247c478bd9Sstevel@tonic-gate int
5257c478bd9Sstevel@tonic-gate px_msix_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
5267c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate 	px_t			*px_p = DIP_TO_STATE(dip);
5297c478bd9Sstevel@tonic-gate 	px_msi_state_t		*msi_state_p = &px_p->px_ib_p->ib_msi_state;
5309c75c6bfSgovinda 	msiq_rec_type_t		msiq_rec_type;
5319c75c6bfSgovinda 	msi_type_t		msi_type;
5329c75c6bfSgovinda 	uint64_t		msi_addr;
5337c478bd9Sstevel@tonic-gate 	msinum_t		msi_num;
5347c478bd9Sstevel@tonic-gate 	msiqid_t		msiq_id;
5357c478bd9Sstevel@tonic-gate 	uint_t			nintrs;
5367c478bd9Sstevel@tonic-gate 	int			i, ret = DDI_SUCCESS;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	DBG(DBG_INTROPS, dip, "px_msix_ops: dip=%x rdip=%x intr_op=%x "
5397c478bd9Sstevel@tonic-gate 	    "handle=%p\n", dip, rdip, intr_op, hdlp);
5407c478bd9Sstevel@tonic-gate 
5419c75c6bfSgovinda 	/* Check for MSI64 support */
54207f14c08Sgovinda 	if ((hdlp->ih_cap & DDI_INTR_FLAG_MSI64) && msi_state_p->msi_addr64) {
5439c75c6bfSgovinda 		msiq_rec_type = MSI64_REC;
5449c75c6bfSgovinda 		msi_type = MSI64_TYPE;
54507f14c08Sgovinda 		msi_addr = msi_state_p->msi_addr64;
5469c75c6bfSgovinda 	} else {
5479c75c6bfSgovinda 		msiq_rec_type = MSI32_REC;
5489c75c6bfSgovinda 		msi_type = MSI32_TYPE;
5499c75c6bfSgovinda 		msi_addr = msi_state_p->msi_addr32;
5509c75c6bfSgovinda 	}
5519c75c6bfSgovinda 
5527c478bd9Sstevel@tonic-gate 	switch (intr_op) {
5537c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
5547c478bd9Sstevel@tonic-gate 		ret = pci_msi_get_cap(rdip, hdlp->ih_type, (int *)result);
5557c478bd9Sstevel@tonic-gate 		break;
5567c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETCAP:
5577c478bd9Sstevel@tonic-gate 		DBG(DBG_INTROPS, dip, "px_msix_ops: SetCap is not supported\n");
5587c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
5597c478bd9Sstevel@tonic-gate 		break;
5607c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ALLOC:
5617c478bd9Sstevel@tonic-gate 		/*
5627c478bd9Sstevel@tonic-gate 		 * We need to restrict this allocation in future
5637c478bd9Sstevel@tonic-gate 		 * based on Resource Management policies.
5647c478bd9Sstevel@tonic-gate 		 */
565*5febcb4aSScott Carter, SD IOSW 		if ((ret = px_msi_alloc(px_p, rdip, hdlp->ih_type,
566*5febcb4aSScott Carter, SD IOSW 		    hdlp->ih_inum, hdlp->ih_scratch1,
567*5febcb4aSScott Carter, SD IOSW 		    (uintptr_t)hdlp->ih_scratch2,
56820036fe5Segillett 		    (int *)result)) != DDI_SUCCESS) {
56920036fe5Segillett 			DBG(DBG_INTROPS, dip, "px_msix_ops: allocation "
57020036fe5Segillett 			    "failed, rdip 0x%p type 0x%d inum 0x%x "
57120036fe5Segillett 			    "count 0x%x\n", rdip, hdlp->ih_type, hdlp->ih_inum,
57220036fe5Segillett 			    hdlp->ih_scratch1);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 			return (ret);
5757c478bd9Sstevel@tonic-gate 		}
5767c478bd9Sstevel@tonic-gate 
57720036fe5Segillett 		if ((hdlp->ih_type == DDI_INTR_TYPE_MSIX) &&
57820036fe5Segillett 		    (i_ddi_get_msix(rdip) == NULL)) {
57920036fe5Segillett 			ddi_intr_msix_t		*msix_p;
58020036fe5Segillett 
58120036fe5Segillett 			if (msix_p = pci_msix_init(rdip)) {
58220036fe5Segillett 				i_ddi_set_msix(rdip, msix_p);
58320036fe5Segillett 				break;
58420036fe5Segillett 			}
58520036fe5Segillett 
58620036fe5Segillett 			DBG(DBG_INTROPS, dip, "px_msix_ops: MSI-X allocation "
58720036fe5Segillett 			    "failed, rdip 0x%p inum 0x%x\n", rdip,
58820036fe5Segillett 			    hdlp->ih_inum);
58920036fe5Segillett 
59020036fe5Segillett 			(void) px_msi_free(px_p, rdip, hdlp->ih_inum,
59120036fe5Segillett 			    hdlp->ih_scratch1);
59220036fe5Segillett 
59320036fe5Segillett 			return (DDI_FAILURE);
59420036fe5Segillett 		}
59520036fe5Segillett 
5967c478bd9Sstevel@tonic-gate 		break;
5977c478bd9Sstevel@tonic-gate 	case DDI_INTROP_FREE:
59895003185Segillett 		(void) pci_msi_disable_mode(rdip, hdlp->ih_type, NULL);
5997c478bd9Sstevel@tonic-gate 		(void) pci_msi_unconfigure(rdip, hdlp->ih_type, hdlp->ih_inum);
60020036fe5Segillett 
60120036fe5Segillett 		if (hdlp->ih_type == DDI_INTR_TYPE_MSI)
60220036fe5Segillett 			goto msi_free;
60320036fe5Segillett 
60420036fe5Segillett 		if (hdlp->ih_flags & DDI_INTR_MSIX_DUP)
60520036fe5Segillett 			break;
60620036fe5Segillett 
60720036fe5Segillett 		if (((i_ddi_intr_get_current_nintrs(hdlp->ih_dip) - 1) == 0) &&
60820036fe5Segillett 		    (i_ddi_get_msix(rdip))) {
60920036fe5Segillett 			pci_msix_fini(i_ddi_get_msix(rdip));
61020036fe5Segillett 			i_ddi_set_msix(rdip, NULL);
61120036fe5Segillett 		}
61220036fe5Segillett msi_free:
6137c478bd9Sstevel@tonic-gate 		(void) px_msi_free(px_p, rdip, hdlp->ih_inum,
6147c478bd9Sstevel@tonic-gate 		    hdlp->ih_scratch1);
6157c478bd9Sstevel@tonic-gate 		break;
6167c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
6177c478bd9Sstevel@tonic-gate 		*(int *)result = hdlp->ih_pri ?
618614edcaeSEvan Yan 		    hdlp->ih_pri : pci_class_to_pil(rdip);
6197c478bd9Sstevel@tonic-gate 		break;
6207c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETPRI:
6217c478bd9Sstevel@tonic-gate 		break;
6227c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
6237c478bd9Sstevel@tonic-gate 		if ((ret = px_msi_get_msinum(px_p, hdlp->ih_dip,
6247c478bd9Sstevel@tonic-gate 		    hdlp->ih_inum, &msi_num)) != DDI_SUCCESS)
6257c478bd9Sstevel@tonic-gate 			return (ret);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 		if ((ret = px_add_msiq_intr(dip, rdip, hdlp,
6289c75c6bfSgovinda 		    msiq_rec_type, msi_num, &msiq_id)) != DDI_SUCCESS) {
6297c478bd9Sstevel@tonic-gate 			DBG(DBG_INTROPS, dip, "px_msix_ops: Add MSI handler "
6307c478bd9Sstevel@tonic-gate 			    "failed, rdip 0x%p msi 0x%x\n", rdip, msi_num);
6317c478bd9Sstevel@tonic-gate 			return (ret);
6327c478bd9Sstevel@tonic-gate 		}
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 		DBG(DBG_INTROPS, dip, "px_msix_ops: msiq used 0x%x\n", msiq_id);
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setmsiq(dip, msi_num,
6379c75c6bfSgovinda 		    msiq_id, msi_type)) != DDI_SUCCESS) {
6387c478bd9Sstevel@tonic-gate 			(void) px_rem_msiq_intr(dip, rdip,
6399c75c6bfSgovinda 			    hdlp, msiq_rec_type, msi_num, msiq_id);
6407c478bd9Sstevel@tonic-gate 			return (ret);
6417c478bd9Sstevel@tonic-gate 		}
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setstate(dip, msi_num,
6447c478bd9Sstevel@tonic-gate 		    PCI_MSI_STATE_IDLE)) != DDI_SUCCESS) {
6457c478bd9Sstevel@tonic-gate 			(void) px_rem_msiq_intr(dip, rdip,
6469c75c6bfSgovinda 			    hdlp, msiq_rec_type, msi_num, msiq_id);
6477c478bd9Sstevel@tonic-gate 			return (ret);
6487c478bd9Sstevel@tonic-gate 		}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = msi_num;
6517c478bd9Sstevel@tonic-gate 		break;
6527c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DUPVEC:
65320036fe5Segillett 		DBG(DBG_INTROPS, dip, "px_msix_ops: dupisr - inum: %x, "
65420036fe5Segillett 		    "new_vector: %x\n", hdlp->ih_inum, hdlp->ih_scratch1);
65520036fe5Segillett 
65620036fe5Segillett 		ret = pci_msix_dup(hdlp->ih_dip, hdlp->ih_inum,
65720036fe5Segillett 		    hdlp->ih_scratch1);
6587c478bd9Sstevel@tonic-gate 		break;
6597c478bd9Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
6607c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
6637c478bd9Sstevel@tonic-gate 		    &msiq_id)) != DDI_SUCCESS)
6647c478bd9Sstevel@tonic-gate 			return (ret);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setstate(dip, msi_num,
6679c75c6bfSgovinda 		    PCI_MSI_STATE_IDLE)) != DDI_SUCCESS)
6687c478bd9Sstevel@tonic-gate 			return (ret);
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		ret = px_rem_msiq_intr(dip, rdip,
6719c75c6bfSgovinda 		    hdlp, msiq_rec_type, msi_num, msiq_id);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = 0;
6747c478bd9Sstevel@tonic-gate 		break;
6757c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ENABLE:
6767c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		if ((ret = px_lib_msi_setvalid(dip, msi_num,
6797c478bd9Sstevel@tonic-gate 		    PCI_MSI_VALID)) != DDI_SUCCESS)
6807c478bd9Sstevel@tonic-gate 			return (ret);
6817c478bd9Sstevel@tonic-gate 
68295003185Segillett 		if ((pci_is_msi_enabled(rdip, hdlp->ih_type) != DDI_SUCCESS) ||
68395003185Segillett 		    (hdlp->ih_type == DDI_INTR_TYPE_MSIX)) {
6847c478bd9Sstevel@tonic-gate 			nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 			if ((ret = pci_msi_configure(rdip, hdlp->ih_type,
6879c75c6bfSgovinda 			    nintrs, hdlp->ih_inum, msi_addr,
68895003185Segillett 			    hdlp->ih_type == DDI_INTR_TYPE_MSIX ?
68995003185Segillett 			    msi_num : msi_num & ~(nintrs - 1))) != DDI_SUCCESS)
6907c478bd9Sstevel@tonic-gate 				return (ret);
6917c478bd9Sstevel@tonic-gate 
69295003185Segillett 			if ((ret = pci_msi_enable_mode(rdip, hdlp->ih_type))
69395003185Segillett 			    != DDI_SUCCESS)
6947c478bd9Sstevel@tonic-gate 				return (ret);
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 
69736fe4a92Segillett 		if ((ret = pci_msi_clr_mask(rdip, hdlp->ih_type,
69836fe4a92Segillett 		    hdlp->ih_inum)) != DDI_SUCCESS)
69936fe4a92Segillett 			return (ret);
70036fe4a92Segillett 
70120036fe5Segillett 		if (hdlp->ih_flags & DDI_INTR_MSIX_DUP)
70220036fe5Segillett 			break;
70320036fe5Segillett 
70436fe4a92Segillett 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
70536fe4a92Segillett 		    &msiq_id)) != DDI_SUCCESS)
70636fe4a92Segillett 			return (ret);
70736fe4a92Segillett 
70836fe4a92Segillett 		ret = px_ib_update_intr_state(px_p, rdip, hdlp->ih_inum,
709b0fc0e77Sgovinda 		    px_msiqid_to_devino(px_p, msiq_id), hdlp->ih_pri,
710b0fc0e77Sgovinda 		    PX_INTR_STATE_ENABLE, msiq_rec_type, msi_num);
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 		break;
7137c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
7147c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		if ((ret = pci_msi_set_mask(rdip, hdlp->ih_type,
7177c478bd9Sstevel@tonic-gate 		    hdlp->ih_inum)) != DDI_SUCCESS)
7187c478bd9Sstevel@tonic-gate 			return (ret);
7197c478bd9Sstevel@tonic-gate 
72036fe4a92Segillett 		if ((ret = px_lib_msi_setvalid(dip, msi_num,
72136fe4a92Segillett 		    PCI_MSI_INVALID)) != DDI_SUCCESS)
72236fe4a92Segillett 			return (ret);
72336fe4a92Segillett 
72420036fe5Segillett 		if (hdlp->ih_flags & DDI_INTR_MSIX_DUP)
72520036fe5Segillett 			break;
72620036fe5Segillett 
72736fe4a92Segillett 		if ((ret = px_lib_msi_getmsiq(dip, msi_num,
72836fe4a92Segillett 		    &msiq_id)) != DDI_SUCCESS)
72936fe4a92Segillett 			return (ret);
73036fe4a92Segillett 
73136fe4a92Segillett 		ret = px_ib_update_intr_state(px_p, rdip,
73236fe4a92Segillett 		    hdlp->ih_inum, px_msiqid_to_devino(px_p, msiq_id),
733b0fc0e77Sgovinda 		    hdlp->ih_pri, PX_INTR_STATE_DISABLE, msiq_rec_type,
734b0fc0e77Sgovinda 		    msi_num);
73536fe4a92Segillett 
7367c478bd9Sstevel@tonic-gate 		break;
7377c478bd9Sstevel@tonic-gate 	case DDI_INTROP_BLOCKENABLE:
7387c478bd9Sstevel@tonic-gate 		nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
7397c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 		if ((ret = pci_msi_configure(rdip, hdlp->ih_type,
7429c75c6bfSgovinda 		    nintrs, hdlp->ih_inum, msi_addr,
7437c478bd9Sstevel@tonic-gate 		    msi_num & ~(nintrs - 1))) != DDI_SUCCESS)
7447c478bd9Sstevel@tonic-gate 			return (ret);
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 		for (i = 0; i < nintrs; i++, msi_num++) {
7477c478bd9Sstevel@tonic-gate 			if ((ret = px_lib_msi_setvalid(dip, msi_num,
7487c478bd9Sstevel@tonic-gate 			    PCI_MSI_VALID)) != DDI_SUCCESS)
7497c478bd9Sstevel@tonic-gate 				return (ret);
75036fe4a92Segillett 
75136fe4a92Segillett 			if ((ret = px_lib_msi_getmsiq(dip, msi_num,
75236fe4a92Segillett 			    &msiq_id)) != DDI_SUCCESS)
75336fe4a92Segillett 				return (ret);
75436fe4a92Segillett 
75536fe4a92Segillett 			if ((ret = px_ib_update_intr_state(px_p, rdip,
75636fe4a92Segillett 			    hdlp->ih_inum + i, px_msiqid_to_devino(px_p,
757b0fc0e77Sgovinda 			    msiq_id), hdlp->ih_pri, PX_INTR_STATE_ENABLE,
758b0fc0e77Sgovinda 			    msiq_rec_type, msi_num)) != DDI_SUCCESS)
75936fe4a92Segillett 				return (ret);
7607c478bd9Sstevel@tonic-gate 		}
7617c478bd9Sstevel@tonic-gate 
76295003185Segillett 		ret = pci_msi_enable_mode(rdip, hdlp->ih_type);
7637c478bd9Sstevel@tonic-gate 		break;
7647c478bd9Sstevel@tonic-gate 	case DDI_INTROP_BLOCKDISABLE:
7657c478bd9Sstevel@tonic-gate 		nintrs = i_ddi_intr_get_current_nintrs(hdlp->ih_dip);
7667c478bd9Sstevel@tonic-gate 		msi_num = hdlp->ih_vector;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 		if ((ret = pci_msi_disable_mode(rdip, hdlp->ih_type,
76995003185Segillett 		    hdlp->ih_cap & DDI_INTR_FLAG_BLOCK)) != DDI_SUCCESS)
7707c478bd9Sstevel@tonic-gate 			return (ret);
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 		for (i = 0; i < nintrs; i++, msi_num++) {
7737c478bd9Sstevel@tonic-gate 			if ((ret = px_lib_msi_setvalid(dip, msi_num,
7747c478bd9Sstevel@tonic-gate 			    PCI_MSI_INVALID)) != DDI_SUCCESS)
7757c478bd9Sstevel@tonic-gate 				return (ret);
77636fe4a92Segillett 
77736fe4a92Segillett 			if ((ret = px_lib_msi_getmsiq(dip, msi_num,
77836fe4a92Segillett 			    &msiq_id)) != DDI_SUCCESS)
77936fe4a92Segillett 				return (ret);
78036fe4a92Segillett 
78136fe4a92Segillett 			if ((ret = px_ib_update_intr_state(px_p, rdip,
78236fe4a92Segillett 			    hdlp->ih_inum + i, px_msiqid_to_devino(px_p,
783b0fc0e77Sgovinda 			    msiq_id), hdlp->ih_pri, PX_INTR_STATE_DISABLE,
784b0fc0e77Sgovinda 			    msiq_rec_type, msi_num)) != DDI_SUCCESS)
78536fe4a92Segillett 				return (ret);
7867c478bd9Sstevel@tonic-gate 		}
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 		break;
7897c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETMASK:
7907c478bd9Sstevel@tonic-gate 		ret = pci_msi_set_mask(rdip, hdlp->ih_type, hdlp->ih_inum);
7917c478bd9Sstevel@tonic-gate 		break;
7927c478bd9Sstevel@tonic-gate 	case DDI_INTROP_CLRMASK:
7937c478bd9Sstevel@tonic-gate 		ret = pci_msi_clr_mask(rdip, hdlp->ih_type, hdlp->ih_inum);
7947c478bd9Sstevel@tonic-gate 		break;
7957c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPENDING:
7967c478bd9Sstevel@tonic-gate 		ret = pci_msi_get_pending(rdip, hdlp->ih_type,
7977c478bd9Sstevel@tonic-gate 		    hdlp->ih_inum, (int *)result);
7987c478bd9Sstevel@tonic-gate 		break;
7997c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
8007c478bd9Sstevel@tonic-gate 		ret = pci_msi_get_nintrs(rdip, hdlp->ih_type, (int *)result);
8017c478bd9Sstevel@tonic-gate 		break;
8027c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NAVAIL:
8037c478bd9Sstevel@tonic-gate 		/* XXX - a new interface may be needed */
8047c478bd9Sstevel@tonic-gate 		ret = pci_msi_get_nintrs(rdip, hdlp->ih_type, (int *)result);
8057c478bd9Sstevel@tonic-gate 		break;
806*5febcb4aSScott Carter, SD IOSW 	case DDI_INTROP_GETPOOL:
807*5febcb4aSScott Carter, SD IOSW 		if (msi_state_p->msi_pool_p == NULL) {
808*5febcb4aSScott Carter, SD IOSW 			*(ddi_irm_pool_t **)result = NULL;
809*5febcb4aSScott Carter, SD IOSW 			return (DDI_ENOTSUP);
810*5febcb4aSScott Carter, SD IOSW 		}
811*5febcb4aSScott Carter, SD IOSW 		*(ddi_irm_pool_t **)result = msi_state_p->msi_pool_p;
812*5febcb4aSScott Carter, SD IOSW 		ret = DDI_SUCCESS;
813*5febcb4aSScott Carter, SD IOSW 		break;
8147c478bd9Sstevel@tonic-gate 	default:
8157c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
8167c478bd9Sstevel@tonic-gate 		break;
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	return (ret);
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate 
8226d44af1bSesolom static struct {
8236d44af1bSesolom 	kstat_named_t pxintr_ks_name;
8246d44af1bSesolom 	kstat_named_t pxintr_ks_type;
8256d44af1bSesolom 	kstat_named_t pxintr_ks_cpu;
8266d44af1bSesolom 	kstat_named_t pxintr_ks_pil;
8276d44af1bSesolom 	kstat_named_t pxintr_ks_time;
8286d44af1bSesolom 	kstat_named_t pxintr_ks_ino;
8296d44af1bSesolom 	kstat_named_t pxintr_ks_cookie;
8306d44af1bSesolom 	kstat_named_t pxintr_ks_devpath;
8316d44af1bSesolom 	kstat_named_t pxintr_ks_buspath;
8326d44af1bSesolom } pxintr_ks_template = {
8336d44af1bSesolom 	{ "name",	KSTAT_DATA_CHAR },
8346d44af1bSesolom 	{ "type",	KSTAT_DATA_CHAR },
8356d44af1bSesolom 	{ "cpu",	KSTAT_DATA_UINT64 },
8366d44af1bSesolom 	{ "pil",	KSTAT_DATA_UINT64 },
8376d44af1bSesolom 	{ "time",	KSTAT_DATA_UINT64 },
8386d44af1bSesolom 	{ "ino",	KSTAT_DATA_UINT64 },
8396d44af1bSesolom 	{ "cookie",	KSTAT_DATA_UINT64 },
8406d44af1bSesolom 	{ "devpath",	KSTAT_DATA_STRING },
8416d44af1bSesolom 	{ "buspath",	KSTAT_DATA_STRING },
8426d44af1bSesolom };
8436d44af1bSesolom 
8446d44af1bSesolom static uint32_t pxintr_ks_instance;
845d48713b8Sesolom static char ih_devpath[MAXPATHLEN];
846d48713b8Sesolom static char ih_buspath[MAXPATHLEN];
8476d44af1bSesolom kmutex_t pxintr_ks_template_lock;
8486d44af1bSesolom 
8496d44af1bSesolom int
8506d44af1bSesolom px_ks_update(kstat_t *ksp, int rw)
8516d44af1bSesolom {
8526d44af1bSesolom 	px_ih_t *ih_p = ksp->ks_private;
8536d44af1bSesolom 	int maxlen = sizeof (pxintr_ks_template.pxintr_ks_name.value.c);
854b0fc0e77Sgovinda 	px_ino_pil_t *ipil_p = ih_p->ih_ipil_p;
855b0fc0e77Sgovinda 	px_ino_t *ino_p = ipil_p->ipil_ino_p;
856b0fc0e77Sgovinda 	px_t *px_p = ino_p->ino_ib_p->ib_px_p;
8576d44af1bSesolom 	devino_t ino;
8586d44af1bSesolom 	sysino_t sysino;
8596d44af1bSesolom 
860b0fc0e77Sgovinda 	ino = ino_p->ino_ino;
861d8d130aeSanbui 	if (px_lib_intr_devino_to_sysino(px_p->px_dip, ino, &sysino) !=
862d8d130aeSanbui 	    DDI_SUCCESS) {
863d8d130aeSanbui 		cmn_err(CE_WARN, "px_ks_update: px_lib_intr_devino_to_sysino "
864d8d130aeSanbui 		    "failed");
865d8d130aeSanbui 	}
8666d44af1bSesolom 
8676d44af1bSesolom 	(void) snprintf(pxintr_ks_template.pxintr_ks_name.value.c, maxlen,
8686d44af1bSesolom 	    "%s%d", ddi_driver_name(ih_p->ih_dip),
8696d44af1bSesolom 	    ddi_get_instance(ih_p->ih_dip));
8706d44af1bSesolom 
8716d44af1bSesolom 	(void) ddi_pathname(ih_p->ih_dip, ih_devpath);
8726d44af1bSesolom 	(void) ddi_pathname(px_p->px_dip, ih_buspath);
8736d44af1bSesolom 	kstat_named_setstr(&pxintr_ks_template.pxintr_ks_devpath, ih_devpath);
8746d44af1bSesolom 	kstat_named_setstr(&pxintr_ks_template.pxintr_ks_buspath, ih_buspath);
8756d44af1bSesolom 
876e1d9f4e6Sschwartz 	if (ih_p->ih_intr_state == PX_INTR_STATE_ENABLE) {
877e1d9f4e6Sschwartz 
8782917a9c9Sschwartz 		switch (i_ddi_intr_get_current_type(ih_p->ih_dip)) {
8792917a9c9Sschwartz 		case DDI_INTR_TYPE_MSI:
8802917a9c9Sschwartz 			(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
8812917a9c9Sschwartz 			    "msi");
8822917a9c9Sschwartz 			break;
8832917a9c9Sschwartz 		case DDI_INTR_TYPE_MSIX:
8842917a9c9Sschwartz 			(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
8852917a9c9Sschwartz 			    "msix");
8862917a9c9Sschwartz 			break;
8872917a9c9Sschwartz 		default:
8882917a9c9Sschwartz 			(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
8892917a9c9Sschwartz 			    "fixed");
8902917a9c9Sschwartz 			break;
8912917a9c9Sschwartz 		}
8922917a9c9Sschwartz 
893b0fc0e77Sgovinda 		pxintr_ks_template.pxintr_ks_cpu.value.ui64 = ino_p->ino_cpuid;
894b0fc0e77Sgovinda 		pxintr_ks_template.pxintr_ks_pil.value.ui64 = ipil_p->ipil_pil;
895e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_time.value.ui64 = ih_p->ih_nsec +
896e1d9f4e6Sschwartz 		    (uint64_t)tick2ns((hrtime_t)ih_p->ih_ticks,
897b0fc0e77Sgovinda 		    ino_p->ino_cpuid);
898e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_ino.value.ui64 = ino;
899e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_cookie.value.ui64 = sysino;
900e1d9f4e6Sschwartz 	} else {
901e1d9f4e6Sschwartz 		(void) strcpy(pxintr_ks_template.pxintr_ks_type.value.c,
902e1d9f4e6Sschwartz 		    "disabled");
903e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_cpu.value.ui64 = 0;
904e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_pil.value.ui64 = 0;
905e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_time.value.ui64 = 0;
906e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_ino.value.ui64 = 0;
907e1d9f4e6Sschwartz 		pxintr_ks_template.pxintr_ks_cookie.value.ui64 = 0;
908e1d9f4e6Sschwartz 	}
9096d44af1bSesolom 	return (0);
9106d44af1bSesolom }
9116d44af1bSesolom 
9126d44af1bSesolom void
9136d44af1bSesolom px_create_intr_kstats(px_ih_t *ih_p)
9146d44af1bSesolom {
9156d44af1bSesolom 	msiq_rec_type_t rec_type = ih_p->ih_rec_type;
9166d44af1bSesolom 
9176d44af1bSesolom 	ASSERT(ih_p->ih_ksp == NULL);
9186d44af1bSesolom 
9196d44af1bSesolom 	/*
9206d44af1bSesolom 	 * Create pci_intrs::: kstats for all ih types except messages,
9216d44af1bSesolom 	 * which represent unusual conditions and don't need to be tracked.
9226d44af1bSesolom 	 */
9236d44af1bSesolom 	if (rec_type == 0 || rec_type == MSI32_REC || rec_type == MSI64_REC) {
9246d44af1bSesolom 		ih_p->ih_ksp = kstat_create("pci_intrs",
9256d44af1bSesolom 		    atomic_inc_32_nv(&pxintr_ks_instance), "config",
9266d44af1bSesolom 		    "interrupts", KSTAT_TYPE_NAMED,
9276d44af1bSesolom 		    sizeof (pxintr_ks_template) / sizeof (kstat_named_t),
9286d44af1bSesolom 		    KSTAT_FLAG_VIRTUAL);
9296d44af1bSesolom 	}
9306d44af1bSesolom 	if (ih_p->ih_ksp != NULL) {
9316d44af1bSesolom 		ih_p->ih_ksp->ks_data_size += MAXPATHLEN * 2;
9326d44af1bSesolom 		ih_p->ih_ksp->ks_lock = &pxintr_ks_template_lock;
9336d44af1bSesolom 		ih_p->ih_ksp->ks_data = &pxintr_ks_template;
9346d44af1bSesolom 		ih_p->ih_ksp->ks_private = ih_p;
9356d44af1bSesolom 		ih_p->ih_ksp->ks_update = px_ks_update;
9366d44af1bSesolom 	}
9376d44af1bSesolom }
9386d44af1bSesolom 
939a195726fSgovinda /*
940a195726fSgovinda  * px_add_intx_intr:
941a195726fSgovinda  *
942a195726fSgovinda  * This function is called to register INTx and legacy hardware
943a195726fSgovinda  * interrupt pins interrupts.
944a195726fSgovinda  */
9457c478bd9Sstevel@tonic-gate int
9467c478bd9Sstevel@tonic-gate px_add_intx_intr(dev_info_t *dip, dev_info_t *rdip,
9477c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
9487c478bd9Sstevel@tonic-gate {
9497c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
9507c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
9517c478bd9Sstevel@tonic-gate 	devino_t	ino;
9527c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
953b0fc0e77Sgovinda 	px_ino_t	*ino_p;
954b0fc0e77Sgovinda 	px_ino_pil_t	*ipil_p, *ipil_list;
9557c478bd9Sstevel@tonic-gate 	int32_t		weight;
9567c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	ino = hdlp->ih_vector;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: rdip=%s%d ino=%x "
9617c478bd9Sstevel@tonic-gate 	    "handler=%x arg1=%x arg2=%x\n", ddi_driver_name(rdip),
9627c478bd9Sstevel@tonic-gate 	    ddi_get_instance(rdip), ino, hdlp->ih_cb_func,
9637c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	ih_p = px_ib_alloc_ih(rdip, hdlp->ih_inum,
9667c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, 0, 0);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
9697c478bd9Sstevel@tonic-gate 
970b0fc0e77Sgovinda 	ino_p = px_ib_locate_ino(ib_p, ino);
971b0fc0e77Sgovinda 	ipil_list = ino_p ? ino_p->ino_ipil_p : NULL;
972b0fc0e77Sgovinda 
973b0fc0e77Sgovinda 	/* Sharing ino */
974b0fc0e77Sgovinda 	if (ino_p && (ipil_p = px_ib_ino_locate_ipil(ino_p, hdlp->ih_pri))) {
975b0fc0e77Sgovinda 		if (px_ib_intr_locate_ih(ipil_p, rdip, hdlp->ih_inum, 0, 0)) {
9767c478bd9Sstevel@tonic-gate 			DBG(DBG_A_INTX, dip, "px_add_intx_intr: "
977b0fc0e77Sgovinda 			    "dup intr #%d\n", hdlp->ih_inum);
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 			ret = DDI_FAILURE;
9807c478bd9Sstevel@tonic-gate 			goto fail1;
9817c478bd9Sstevel@tonic-gate 		}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 		/* Save mondo value in hdlp */
9847c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
9857c478bd9Sstevel@tonic-gate 
986b0fc0e77Sgovinda 		if ((ret = px_ib_ino_add_intr(px_p, ipil_p,
987b0fc0e77Sgovinda 		    ih_p)) != DDI_SUCCESS)
9887c478bd9Sstevel@tonic-gate 			goto fail1;
9897c478bd9Sstevel@tonic-gate 
990b0fc0e77Sgovinda 		goto ino_done;
991b0fc0e77Sgovinda 	}
9927c478bd9Sstevel@tonic-gate 
993b0fc0e77Sgovinda 	if (hdlp->ih_pri == 0)
994614edcaeSEvan Yan 		hdlp->ih_pri = pci_class_to_pil(rdip);
9957c478bd9Sstevel@tonic-gate 
996b0fc0e77Sgovinda 	ipil_p = px_ib_new_ino_pil(ib_p, ino, hdlp->ih_pri, ih_p);
997b0fc0e77Sgovinda 	ino_p = ipil_p->ipil_ino_p;
9987c478bd9Sstevel@tonic-gate 
999b0fc0e77Sgovinda 	/* Save mondo value in hdlp */
1000b0fc0e77Sgovinda 	hdlp->ih_vector = ino_p->ino_sysino;
10017c478bd9Sstevel@tonic-gate 
1002b0fc0e77Sgovinda 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: pil=0x%x mondo=0x%x\n",
1003b0fc0e77Sgovinda 	    hdlp->ih_pri, hdlp->ih_vector);
10047c478bd9Sstevel@tonic-gate 
1005b0fc0e77Sgovinda 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
1006b0fc0e77Sgovinda 	    (ddi_intr_handler_t *)px_intx_intr, (caddr_t)ipil_p, NULL);
1007b0fc0e77Sgovinda 
1008b0fc0e77Sgovinda 	ret = i_ddi_add_ivintr(hdlp);
10097c478bd9Sstevel@tonic-gate 
1010b0fc0e77Sgovinda 	/*
1011b0fc0e77Sgovinda 	 * Restore original interrupt handler
1012b0fc0e77Sgovinda 	 * and arguments in interrupt handle.
1013b0fc0e77Sgovinda 	 */
1014b0fc0e77Sgovinda 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
1015b0fc0e77Sgovinda 	    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
10167c478bd9Sstevel@tonic-gate 
1017b0fc0e77Sgovinda 	if (ret != DDI_SUCCESS)
1018b0fc0e77Sgovinda 		goto fail2;
10197c478bd9Sstevel@tonic-gate 
1020b0fc0e77Sgovinda 	/* Save the pil for this ino */
1021b0fc0e77Sgovinda 	ipil_p->ipil_pil = hdlp->ih_pri;
1022b0fc0e77Sgovinda 
1023b0fc0e77Sgovinda 	/* Select cpu, saving it for sharing and removal */
1024b0fc0e77Sgovinda 	if (ipil_list == NULL) {
10257c478bd9Sstevel@tonic-gate 		ino_p->ino_cpuid = intr_dist_cpuid();
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 		/* Enable interrupt */
10287c478bd9Sstevel@tonic-gate 		px_ib_intr_enable(px_p, ino_p->ino_cpuid, ino);
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 
1031b0fc0e77Sgovinda ino_done:
1032b0fc0e77Sgovinda 	/* Add weight to the cpu that we are already targeting */
1033614edcaeSEvan Yan 	weight = pci_class_to_intr_weight(rdip);
10347c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_add_device_weight(ino_p->ino_cpuid, rdip, weight);
10357c478bd9Sstevel@tonic-gate 
1036b0fc0e77Sgovinda 	ih_p->ih_ipil_p = ipil_p;
10376d44af1bSesolom 	px_create_intr_kstats(ih_p);
10387c478bd9Sstevel@tonic-gate 	if (ih_p->ih_ksp)
10397c478bd9Sstevel@tonic-gate 		kstat_install(ih_p->ih_ksp);
10407c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: done! Interrupt 0x%x pil=%x\n",
10437c478bd9Sstevel@tonic-gate 	    ino_p->ino_sysino, hdlp->ih_pri);
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	return (ret);
10467c478bd9Sstevel@tonic-gate fail2:
1047b0fc0e77Sgovinda 	px_ib_delete_ino_pil(ib_p, ipil_p);
10487c478bd9Sstevel@tonic-gate fail1:
10497c478bd9Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
10507c478bd9Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
10537c478bd9Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (px_ih_t));
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	DBG(DBG_A_INTX, dip, "px_add_intx_intr: Failed! Interrupt 0x%x "
10567c478bd9Sstevel@tonic-gate 	    "pil=%x\n", ino_p->ino_sysino, hdlp->ih_pri);
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	return (ret);
10597c478bd9Sstevel@tonic-gate }
10607c478bd9Sstevel@tonic-gate 
1061a195726fSgovinda /*
1062a195726fSgovinda  * px_rem_intx_intr:
1063a195726fSgovinda  *
1064a195726fSgovinda  * This function is called to unregister INTx and legacy hardware
1065a195726fSgovinda  * interrupt pins interrupts.
1066a195726fSgovinda  */
10677c478bd9Sstevel@tonic-gate int
10687c478bd9Sstevel@tonic-gate px_rem_intx_intr(dev_info_t *dip, dev_info_t *rdip,
10697c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
10707c478bd9Sstevel@tonic-gate {
10717c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
10727c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
10737c478bd9Sstevel@tonic-gate 	devino_t	ino;
10747c478bd9Sstevel@tonic-gate 	cpuid_t		curr_cpu;
1075b0fc0e77Sgovinda 	px_ino_t	*ino_p;
1076b0fc0e77Sgovinda 	px_ino_pil_t	*ipil_p;
10777c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
10787c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	ino = hdlp->ih_vector;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	DBG(DBG_R_INTX, dip, "px_rem_intx_intr: rdip=%s%d ino=%x\n",
10837c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), ino);
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	ino_p = px_ib_locate_ino(ib_p, ino);
1088b0fc0e77Sgovinda 	ipil_p = px_ib_ino_locate_ipil(ino_p, hdlp->ih_pri);
1089b0fc0e77Sgovinda 	ih_p = px_ib_intr_locate_ih(ipil_p, rdip, hdlp->ih_inum, 0, 0);
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	/* Get the current cpu */
10927c478bd9Sstevel@tonic-gate 	if ((ret = px_lib_intr_gettarget(px_p->px_dip, ino_p->ino_sysino,
10937c478bd9Sstevel@tonic-gate 	    &curr_cpu)) != DDI_SUCCESS)
10947c478bd9Sstevel@tonic-gate 		goto fail;
10957c478bd9Sstevel@tonic-gate 
1096b0fc0e77Sgovinda 	if ((ret = px_ib_ino_rem_intr(px_p, ipil_p, ih_p)) != DDI_SUCCESS)
10977c478bd9Sstevel@tonic-gate 		goto fail;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
11007c478bd9Sstevel@tonic-gate 
1101b0fc0e77Sgovinda 	if (ipil_p->ipil_ih_size == 0) {
11027c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
11037c478bd9Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
11047c478bd9Sstevel@tonic-gate 
1105b0fc0e77Sgovinda 		px_ib_delete_ino_pil(ib_p, ipil_p);
1106b0fc0e77Sgovinda 	}
1107b0fc0e77Sgovinda 
1108b0fc0e77Sgovinda 	if (ino_p->ino_ipil_size == 0) {
1109b0fc0e77Sgovinda 		kmem_free(ino_p, sizeof (px_ino_t));
11107c478bd9Sstevel@tonic-gate 	} else {
11110d2a6fcfSegillett 		/* Re-enable interrupt only if mapping register still shared */
11120d2a6fcfSegillett 		PX_INTR_ENABLE(px_p->px_dip, ino_p->ino_sysino, curr_cpu);
11137c478bd9Sstevel@tonic-gate 	}
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate fail:
11167c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
11177c478bd9Sstevel@tonic-gate 	return (ret);
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate 
1120a195726fSgovinda /*
1121a195726fSgovinda  * px_add_msiq_intr:
1122a195726fSgovinda  *
1123a195726fSgovinda  * This function is called to register MSI/Xs and PCIe message interrupts.
1124a195726fSgovinda  */
11257c478bd9Sstevel@tonic-gate int
11267c478bd9Sstevel@tonic-gate px_add_msiq_intr(dev_info_t *dip, dev_info_t *rdip,
11277c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, msiq_rec_type_t rec_type,
11287c478bd9Sstevel@tonic-gate     msgcode_t msg_code, msiqid_t *msiq_id_p)
11297c478bd9Sstevel@tonic-gate {
11307c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
11317c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
11327c478bd9Sstevel@tonic-gate 	px_msiq_state_t	*msiq_state_p = &ib_p->ib_msiq_state;
11337c478bd9Sstevel@tonic-gate 	devino_t	ino;
11347c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
1135b0fc0e77Sgovinda 	px_ino_t	*ino_p;
1136b0fc0e77Sgovinda 	px_ino_pil_t	*ipil_p, *ipil_list;
11377c478bd9Sstevel@tonic-gate 	int32_t		weight;
11387c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: rdip=%s%d handler=%x "
11417c478bd9Sstevel@tonic-gate 	    "arg1=%x arg2=%x\n", ddi_driver_name(rdip), ddi_get_instance(rdip),
11427c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	if ((ret = px_msiq_alloc(px_p, rec_type, msiq_id_p)) != DDI_SUCCESS) {
11457c478bd9Sstevel@tonic-gate 		DBG(DBG_MSIQ, dip, "px_add_msiq_intr: "
11467c478bd9Sstevel@tonic-gate 		    "msiq allocation failed\n");
11477c478bd9Sstevel@tonic-gate 		return (ret);
11487c478bd9Sstevel@tonic-gate 	}
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	ino = px_msiqid_to_devino(px_p, *msiq_id_p);
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	ih_p = px_ib_alloc_ih(rdip, hdlp->ih_inum, hdlp->ih_cb_func,
11537c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2, rec_type, msg_code);
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
11567c478bd9Sstevel@tonic-gate 
1157b0fc0e77Sgovinda 	ino_p = px_ib_locate_ino(ib_p, ino);
1158b0fc0e77Sgovinda 	ipil_list = ino_p ? ino_p->ino_ipil_p : NULL;
1159b0fc0e77Sgovinda 
1160b0fc0e77Sgovinda 	/* Sharing ino */
1161b0fc0e77Sgovinda 	if (ino_p && (ipil_p = px_ib_ino_locate_ipil(ino_p, hdlp->ih_pri))) {
1162b0fc0e77Sgovinda 		if (px_ib_intr_locate_ih(ipil_p, rdip,
1163b0fc0e77Sgovinda 		    hdlp->ih_inum, rec_type, msg_code)) {
11647c478bd9Sstevel@tonic-gate 			DBG(DBG_MSIQ, dip, "px_add_msiq_intr: "
1165b0fc0e77Sgovinda 			    "dup intr #%d\n", hdlp->ih_inum);
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 			ret = DDI_FAILURE;
11687c478bd9Sstevel@tonic-gate 			goto fail1;
11697c478bd9Sstevel@tonic-gate 		}
11707c478bd9Sstevel@tonic-gate 
1171b0fc0e77Sgovinda 		/* Save mondo value in hdlp */
1172b0fc0e77Sgovinda 		hdlp->ih_vector = ino_p->ino_sysino;
1173b0fc0e77Sgovinda 
1174b0fc0e77Sgovinda 		if ((ret = px_ib_ino_add_intr(px_p, ipil_p,
1175b0fc0e77Sgovinda 		    ih_p)) != DDI_SUCCESS)
11767c478bd9Sstevel@tonic-gate 			goto fail1;
11777c478bd9Sstevel@tonic-gate 
1178b0fc0e77Sgovinda 		goto ino_done;
1179b0fc0e77Sgovinda 	}
11807c478bd9Sstevel@tonic-gate 
1181b0fc0e77Sgovinda 	if (hdlp->ih_pri == 0)
1182614edcaeSEvan Yan 		hdlp->ih_pri = pci_class_to_pil(rdip);
11837c478bd9Sstevel@tonic-gate 
1184b0fc0e77Sgovinda 	ipil_p = px_ib_new_ino_pil(ib_p, ino, hdlp->ih_pri, ih_p);
1185b0fc0e77Sgovinda 	ino_p = ipil_p->ipil_ino_p;
11867c478bd9Sstevel@tonic-gate 
1187b0fc0e77Sgovinda 	ino_p->ino_msiq_p = msiq_state_p->msiq_p +
1188b0fc0e77Sgovinda 	    (*msiq_id_p - msiq_state_p->msiq_1st_msiq_id);
11897c478bd9Sstevel@tonic-gate 
1190b0fc0e77Sgovinda 	/* Save mondo value in hdlp */
1191b0fc0e77Sgovinda 	hdlp->ih_vector = ino_p->ino_sysino;
11927c478bd9Sstevel@tonic-gate 
1193b0fc0e77Sgovinda 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: pil=0x%x mondo=0x%x\n",
1194b0fc0e77Sgovinda 	    hdlp->ih_pri, hdlp->ih_vector);
11957c478bd9Sstevel@tonic-gate 
1196b0fc0e77Sgovinda 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp,
1197b0fc0e77Sgovinda 	    (ddi_intr_handler_t *)px_msiq_intr, (caddr_t)ipil_p, NULL);
1198b0fc0e77Sgovinda 
1199b0fc0e77Sgovinda 	ret = i_ddi_add_ivintr(hdlp);
1200b0fc0e77Sgovinda 
1201b0fc0e77Sgovinda 	/*
1202b0fc0e77Sgovinda 	 * Restore original interrupt handler
1203b0fc0e77Sgovinda 	 * and arguments in interrupt handle.
1204b0fc0e77Sgovinda 	 */
1205b0fc0e77Sgovinda 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_handler,
1206b0fc0e77Sgovinda 	    ih_p->ih_handler_arg1, ih_p->ih_handler_arg2);
12077c478bd9Sstevel@tonic-gate 
1208b0fc0e77Sgovinda 	if (ret != DDI_SUCCESS)
1209b0fc0e77Sgovinda 		goto fail2;
12107c478bd9Sstevel@tonic-gate 
1211b0fc0e77Sgovinda 	/* Save the pil for this ino */
1212b0fc0e77Sgovinda 	ipil_p->ipil_pil = hdlp->ih_pri;
1213b0fc0e77Sgovinda 
1214b0fc0e77Sgovinda 	/* Select cpu, saving it for sharing and removal */
1215b0fc0e77Sgovinda 	if (ipil_list == NULL) {
1216b0fc0e77Sgovinda 		ino_p->ino_cpuid = intr_dist_cpuid();
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 		/* Enable MSIQ */
12197c478bd9Sstevel@tonic-gate 		px_lib_msiq_setstate(dip, *msiq_id_p, PCI_MSIQ_STATE_IDLE);
12207c478bd9Sstevel@tonic-gate 		px_lib_msiq_setvalid(dip, *msiq_id_p, PCI_MSIQ_VALID);
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 		/* Enable interrupt */
1223b0fc0e77Sgovinda 		px_ib_intr_enable(px_p, ino_p->ino_cpuid, ino);
12247c478bd9Sstevel@tonic-gate 	}
12257c478bd9Sstevel@tonic-gate 
1226b0fc0e77Sgovinda ino_done:
1227b0fc0e77Sgovinda 	/* Add weight to the cpu that we are already targeting */
1228614edcaeSEvan Yan 	weight = pci_class_to_intr_weight(rdip);
12297c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_add_device_weight(ino_p->ino_cpuid, rdip, weight);
12307c478bd9Sstevel@tonic-gate 
1231b0fc0e77Sgovinda 	ih_p->ih_ipil_p = ipil_p;
12326d44af1bSesolom 	px_create_intr_kstats(ih_p);
12337c478bd9Sstevel@tonic-gate 	if (ih_p->ih_ksp)
12347c478bd9Sstevel@tonic-gate 		kstat_install(ih_p->ih_ksp);
12357c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: done! Interrupt 0x%x pil=%x\n",
12387c478bd9Sstevel@tonic-gate 	    ino_p->ino_sysino, hdlp->ih_pri);
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	return (ret);
12417c478bd9Sstevel@tonic-gate fail2:
1242b0fc0e77Sgovinda 	px_ib_delete_ino_pil(ib_p, ipil_p);
12437c478bd9Sstevel@tonic-gate fail1:
12447c478bd9Sstevel@tonic-gate 	if (ih_p->ih_config_handle)
12457c478bd9Sstevel@tonic-gate 		pci_config_teardown(&ih_p->ih_config_handle);
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
12487c478bd9Sstevel@tonic-gate 	kmem_free(ih_p, sizeof (px_ih_t));
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_add_msiq_intr: Failed! Interrupt 0x%x pil=%x\n",
12517c478bd9Sstevel@tonic-gate 	    ino_p->ino_sysino, hdlp->ih_pri);
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	return (ret);
12547c478bd9Sstevel@tonic-gate }
12557c478bd9Sstevel@tonic-gate 
1256a195726fSgovinda /*
1257a195726fSgovinda  * px_rem_msiq_intr:
1258a195726fSgovinda  *
1259a195726fSgovinda  * This function is called to unregister MSI/Xs and PCIe message interrupts.
1260a195726fSgovinda  */
12617c478bd9Sstevel@tonic-gate int
12627c478bd9Sstevel@tonic-gate px_rem_msiq_intr(dev_info_t *dip, dev_info_t *rdip,
12637c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, msiq_rec_type_t rec_type,
12647c478bd9Sstevel@tonic-gate     msgcode_t msg_code, msiqid_t msiq_id)
12657c478bd9Sstevel@tonic-gate {
12667c478bd9Sstevel@tonic-gate 	px_t		*px_p = INST_TO_STATE(ddi_get_instance(dip));
12677c478bd9Sstevel@tonic-gate 	px_ib_t		*ib_p = px_p->px_ib_p;
12687c478bd9Sstevel@tonic-gate 	devino_t	ino = px_msiqid_to_devino(px_p, msiq_id);
12697c478bd9Sstevel@tonic-gate 	cpuid_t		curr_cpu;
1270b0fc0e77Sgovinda 	px_ino_t	*ino_p;
1271b0fc0e77Sgovinda 	px_ino_pil_t	*ipil_p;
12727c478bd9Sstevel@tonic-gate 	px_ih_t		*ih_p;
12737c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	DBG(DBG_MSIQ, dip, "px_rem_msiq_intr: rdip=%s%d msiq_id=%x ino=%x\n",
12767c478bd9Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip), msiq_id, ino);
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	mutex_enter(&ib_p->ib_ino_lst_mutex);
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	ino_p = px_ib_locate_ino(ib_p, ino);
1281b0fc0e77Sgovinda 	ipil_p = px_ib_ino_locate_ipil(ino_p, hdlp->ih_pri);
1282b0fc0e77Sgovinda 	ih_p = px_ib_intr_locate_ih(ipil_p, rdip, hdlp->ih_inum, rec_type,
1283b0fc0e77Sgovinda 	    msg_code);
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	/* Get the current cpu */
12867c478bd9Sstevel@tonic-gate 	if ((ret = px_lib_intr_gettarget(px_p->px_dip, ino_p->ino_sysino,
12877c478bd9Sstevel@tonic-gate 	    &curr_cpu)) != DDI_SUCCESS)
12887c478bd9Sstevel@tonic-gate 		goto fail;
12897c478bd9Sstevel@tonic-gate 
1290b0fc0e77Sgovinda 	if ((ret = px_ib_ino_rem_intr(px_p, ipil_p, ih_p)) != DDI_SUCCESS)
12917c478bd9Sstevel@tonic-gate 		goto fail;
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	intr_dist_cpuid_rem_device_weight(ino_p->ino_cpuid, rdip);
12947c478bd9Sstevel@tonic-gate 
1295b0fc0e77Sgovinda 	if (ipil_p->ipil_ih_size == 0) {
12967c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ino_p->ino_sysino;
12977c478bd9Sstevel@tonic-gate 		i_ddi_rem_ivintr(hdlp);
12987c478bd9Sstevel@tonic-gate 
1299b0fc0e77Sgovinda 		px_ib_delete_ino_pil(ib_p, ipil_p);
1300b0fc0e77Sgovinda 
1301b0fc0e77Sgovinda 		if (ino_p->ino_ipil_size == 0)
1302b0fc0e77Sgovinda 			px_lib_msiq_setvalid(dip,
1303b0fc0e77Sgovinda 			    px_devino_to_msiqid(px_p, ino), PCI_MSIQ_INVALID);
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 		(void) px_msiq_free(px_p, msiq_id);
1306b0fc0e77Sgovinda 	}
1307b0fc0e77Sgovinda 
1308b0fc0e77Sgovinda 	if (ino_p->ino_ipil_size == 0) {
1309b0fc0e77Sgovinda 		kmem_free(ino_p, sizeof (px_ino_t));
13107c478bd9Sstevel@tonic-gate 	} else {
13110d2a6fcfSegillett 		/* Re-enable interrupt only if mapping register still shared */
13120d2a6fcfSegillett 		PX_INTR_ENABLE(px_p->px_dip, ino_p->ino_sysino, curr_cpu);
13137c478bd9Sstevel@tonic-gate 	}
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate fail:
13167c478bd9Sstevel@tonic-gate 	mutex_exit(&ib_p->ib_ino_lst_mutex);
13177c478bd9Sstevel@tonic-gate 	return (ret);
13187c478bd9Sstevel@tonic-gate }
1319