xref: /titanic_50/usr/src/uts/common/os/ddi_intr.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/note.h>
30 #include <sys/sysmacros.h>
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kmem.h>
35 #include <sys/cmn_err.h>
36 #include <sys/debug.h>
37 #include <sys/avintr.h>
38 #include <sys/autoconf.h>
39 #include <sys/sunndi.h>
40 #include <sys/ndi_impldefs.h>	/* include prototypes */
41 
42 /*
43  * New DDI interrupt framework
44  */
45 
46 /*
47  * MSI/X allocation limit.
48  * This limit will change with Resource Management support.
49  */
50 uint_t		ddi_msix_alloc_limit = 2;
51 
52 /*
53  * ddi_intr_get_supported_types:
54  *	Return, as a bit mask, the hardware interrupt types supported by
55  *	both the device and by the host in the integer pointed
56  *	to be the 'typesp' argument.
57  */
58 int
59 ddi_intr_get_supported_types(dev_info_t *dip, int *typesp)
60 {
61 	int			ret;
62 	ddi_intr_handle_impl_t	hdl;
63 
64 	if (dip == NULL)
65 		return (DDI_EINVAL);
66 
67 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_supported_types: dip %p\n",
68 	    (void *)dip));
69 
70 	if (*typesp = i_ddi_intr_get_supported_types(dip))
71 		return (DDI_SUCCESS);
72 
73 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
74 	hdl.ih_dip = dip;
75 
76 	ret = i_ddi_handle_intr_ops(dip, dip, DDI_INTROP_SUPPORTED_TYPES, &hdl,
77 	    (void *)typesp);
78 
79 	if (ret != DDI_SUCCESS)
80 		return (DDI_INTR_NOTFOUND);
81 
82 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_supported_types: types %x\n",
83 	    *typesp));
84 
85 	return (ret);
86 }
87 
88 
89 /*
90  * ddi_intr_get_nintrs:
91  * 	Return as an integer in the integer pointed to by the argument
92  * 	*nintrsp*, the number of interrupts the device supports for the
93  *	given interrupt type.
94  */
95 int
96 ddi_intr_get_nintrs(dev_info_t *dip, int type, int *nintrsp)
97 {
98 	int			ret;
99 	ddi_intr_handle_impl_t	hdl;
100 
101 	if (dip == NULL)
102 		return (DDI_EINVAL);
103 
104 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_nintrs: dip %p\n",
105 	    (void *)dip));
106 
107 	if (!(i_ddi_intr_get_supported_types(dip) & type))
108 		return (DDI_EINVAL);
109 
110 	if (*nintrsp = i_ddi_intr_get_supported_nintrs(dip, type))
111 		return (DDI_SUCCESS);
112 
113 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
114 	hdl.ih_dip = dip;
115 	hdl.ih_type = type;
116 
117 	ret = i_ddi_handle_intr_ops(dip, dip, DDI_INTROP_NINTRS, &hdl,
118 	    (void *)nintrsp);
119 
120 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_nintrs:: nintrs %x\n",
121 	    *nintrsp));
122 
123 	return (ret);
124 }
125 
126 
127 /*
128  * ddi_intr_get_navail:
129  *	Bus nexus driver will return availble interrupt count value for
130  *	a given interrupt type.
131  *
132  * 	Return as an integer in the integer pointed to by the argument
133  * 	*navailp*, the number of interrupts currently available for the
134  *	given interrupt type.
135  */
136 int
137 ddi_intr_get_navail(dev_info_t *dip, int type, int *navailp)
138 {
139 	int			ret;
140 	ddi_intr_handle_impl_t	hdl;
141 
142 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_navail: dip %p\n",
143 	    (void *)dip));
144 
145 	if (dip == NULL)
146 		return (DDI_EINVAL);
147 
148 	if (!(i_ddi_intr_get_supported_types(dip) & type))
149 		return (DDI_EINVAL);
150 
151 	/*
152 	 * In future, this interface implementation will change
153 	 * with Resource Management support.
154 	 */
155 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
156 	hdl.ih_dip = dip;
157 	hdl.ih_type = type;
158 
159 	ret = i_ddi_handle_intr_ops(dip, dip,
160 	    DDI_INTROP_NAVAIL, &hdl, (void *)navailp);
161 
162 	return (ret == DDI_SUCCESS ? DDI_SUCCESS : DDI_INTR_NOTFOUND);
163 }
164 
165 
166 /*
167  * Interrupt allocate/free functions
168  */
169 int
170 ddi_intr_alloc(dev_info_t *dip, ddi_intr_handle_t *h_array, int type, int inum,
171     int count, int *actualp, int behavior)
172 {
173 	ddi_intr_handle_impl_t	*hdlp, tmp_hdl;
174 	int			i, ret, cap = 0, intr_type, nintrs = 0;
175 	uint_t			pri;
176 
177 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: name %s dip 0x%p "
178 	    "type %x inum %x count %x behavior %x\n", ddi_driver_name(dip),
179 	    (void *)dip, type, inum, count, behavior));
180 
181 	/* Validate parameters */
182 	if (dip == NULL || h_array == NULL || count < 1) {
183 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: Invalid args\n"));
184 		return (DDI_EINVAL);
185 	}
186 
187 	/* Validate interrupt type */
188 	if (!(i_ddi_intr_get_supported_types(dip) & type)) {
189 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: type %x not "
190 		    "supported\n", type));
191 		return (DDI_EINVAL);
192 	}
193 
194 	/* First, get how many interrupts the device supports */
195 	if (!(nintrs = i_ddi_intr_get_supported_nintrs(dip, type))) {
196 		if (ddi_intr_get_nintrs(dip, type, &nintrs) != DDI_SUCCESS) {
197 			DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: no "
198 			    "interrupts found of type %d\n", type));
199 			return (DDI_INTR_NOTFOUND);
200 		}
201 	}
202 
203 	/* Is this function invoked with more interrupt than device supports? */
204 	if (count > nintrs) {
205 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: no of interrupts "
206 		    "requested %d is more than supported %d\n", count, nintrs));
207 		return (DDI_EINVAL);
208 	}
209 
210 	/*
211 	 * Check if requested interrupt type is not same as interrupt
212 	 * type is in use if any.
213 	 */
214 	if (((intr_type = i_ddi_intr_get_current_type(dip)) != 0) &&
215 	    (intr_type != type)) {
216 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: Requested "
217 		    "interrupt type %x is different from interrupt type %x"
218 		    "already in use\n", type, intr_type));
219 
220 		return (DDI_EINVAL);
221 	}
222 
223 	/*
224 	 * Check if requested interrupt type is in use and requested number
225 	 * of interrupts and number of interrupts already in use exceeds the
226 	 * number of interrupts supported by this device.
227 	 */
228 	if (intr_type) {
229 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: type %x "
230 		    "is already being used\n", type));
231 
232 		if ((count + i_ddi_intr_get_current_nintrs(dip)) > nintrs) {
233 			DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: count %d "
234 			    "+ intrs in use %d exceeds supported %d intrs\n",
235 			    count, i_ddi_intr_get_current_nintrs(dip), nintrs));
236 			return (DDI_EINVAL);
237 		}
238 	}
239 
240 	/*
241 	 * For MSI, ensure that the requested interrupt count is a power of 2
242 	 */
243 	if (type == DDI_INTR_TYPE_MSI && !ISP2(count)) {
244 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: "
245 		    "MSI count %d is not a power of two\n", count));
246 		return (DDI_EINVAL);
247 	}
248 
249 	/*
250 	 * Limit max MSI/X allocation to ddi_msix_alloc_limit.
251 	 * This limit will change with Resource Management support.
252 	 */
253 	if (DDI_INTR_IS_MSI_OR_MSIX(type) && (count > ddi_msix_alloc_limit)) {
254 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: Requested MSI/Xs %d"
255 		    "Max MSI/Xs limit %d\n", count, ddi_msix_alloc_limit));
256 
257 		if (behavior == DDI_INTR_ALLOC_STRICT) {
258 			DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: "
259 			    "DDI_INTR_ALLOC_STRICT flag is passed, "
260 			    "return failure\n"));
261 
262 			return (DDI_EAGAIN);
263 		}
264 
265 		count = ddi_msix_alloc_limit;
266 	}
267 
268 	/* Now allocate required number of interrupts */
269 	bzero(&tmp_hdl, sizeof (ddi_intr_handle_impl_t));
270 	tmp_hdl.ih_type = type;
271 	tmp_hdl.ih_inum = inum;
272 	tmp_hdl.ih_scratch1 = count;
273 	tmp_hdl.ih_scratch2 = behavior;
274 	tmp_hdl.ih_dip = dip;
275 
276 	if (i_ddi_handle_intr_ops(dip, dip, DDI_INTROP_ALLOC,
277 	    &tmp_hdl, (void *)actualp) != DDI_SUCCESS) {
278 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: allocation "
279 		    "failed\n"));
280 		return (*actualp ? DDI_EAGAIN : DDI_INTR_NOTFOUND);
281 	}
282 
283 	if ((ret = i_ddi_handle_intr_ops(dip, dip, DDI_INTROP_GETPRI,
284 	    &tmp_hdl, (void *)&pri)) != DDI_SUCCESS) {
285 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: get priority "
286 		    "failed\n"));
287 		return (ret);
288 	}
289 
290 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: getting capability\n"));
291 
292 	if ((ret = i_ddi_handle_intr_ops(dip, dip, DDI_INTROP_GETCAP,
293 	    &tmp_hdl, (void *)&cap)) != DDI_SUCCESS) {
294 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: get capability "
295 		    "failed\n"));
296 		return (ret);
297 	}
298 
299 	/* Save current interrupt type, supported and current intr count */
300 	i_ddi_intr_devi_init(dip);
301 	i_ddi_intr_set_current_type(dip, type);
302 	i_ddi_intr_set_supported_nintrs(dip, nintrs);
303 	i_ddi_intr_set_current_nintrs(dip,
304 	    i_ddi_intr_get_current_nintrs(dip) + *actualp);
305 
306 	/* Now, go and handle each "handle" */
307 	for (i = 0; i < *actualp; i++) {
308 		hdlp = (ddi_intr_handle_impl_t *)kmem_zalloc(
309 		    (sizeof (ddi_intr_handle_impl_t)), KM_SLEEP);
310 		rw_init(&hdlp->ih_rwlock, NULL, RW_DRIVER, NULL);
311 		h_array[i] = (struct __ddi_intr_handle *)hdlp;
312 		hdlp->ih_type = type;
313 		hdlp->ih_pri = pri;
314 		hdlp->ih_cap = cap;
315 		hdlp->ih_ver = DDI_INTR_VERSION;
316 		hdlp->ih_state = DDI_IHDL_STATE_ALLOC;
317 		hdlp->ih_dip = dip;
318 		hdlp->ih_inum = inum + i;
319 		if (type & DDI_INTR_TYPE_FIXED)
320 			i_ddi_set_intr_handle(dip, hdlp->ih_inum, &h_array[i]);
321 
322 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: hdlp = 0x%p\n",
323 		    (void *)h_array[i]));
324 	}
325 
326 	return (DDI_SUCCESS);
327 }
328 
329 
330 int
331 ddi_intr_free(ddi_intr_handle_t h)
332 {
333 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
334 	int			ret;
335 
336 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_free: hdlp = %p\n", (void *)hdlp));
337 
338 	if (hdlp == NULL)
339 		return (DDI_EINVAL);
340 
341 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
342 	if (hdlp->ih_state != DDI_IHDL_STATE_ALLOC) {
343 		rw_exit(&hdlp->ih_rwlock);
344 		return (DDI_EINVAL);
345 	}
346 
347 	ret = i_ddi_handle_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
348 	    DDI_INTROP_FREE, hdlp, NULL);
349 
350 	rw_exit(&hdlp->ih_rwlock);
351 	if (ret == DDI_SUCCESS) {
352 		if ((i_ddi_intr_get_current_nintrs(hdlp->ih_dip) - 1) == 0)
353 			/* Reset current interrupt type and count used */
354 			i_ddi_intr_set_current_type(hdlp->ih_dip, 0);
355 
356 		i_ddi_intr_set_current_nintrs(hdlp->ih_dip,
357 		    i_ddi_intr_get_current_nintrs(hdlp->ih_dip) - 1);
358 
359 		if (hdlp->ih_type & DDI_INTR_TYPE_FIXED)
360 			i_ddi_set_intr_handle(hdlp->ih_dip,
361 			    hdlp->ih_inum, NULL);
362 
363 		rw_destroy(&hdlp->ih_rwlock);
364 		kmem_free(hdlp, sizeof (ddi_intr_handle_impl_t));
365 	}
366 
367 	return (ret);
368 }
369 
370 /*
371  * Interrupt get/set capacity functions
372  *
373  * The logic used to figure this out is shown here:
374  *
375  *			Device level		Platform level	    Intr source
376  * 1. Fixed interrupts
377  * (non-PCI)
378  * o Flags supported	N/A			Maskable/Pending/    rootnex
379  *						No Block Enable
380  * o navail					1
381  *
382  * 2. PCI Fixed interrupts
383  * o Flags supported	pending/Maskable	Maskable/pending/    pci
384  *						No Block enable
385  * o navail		N/A			1
386  *
387  * 3. PCI MSI
388  * o Flags supported	Maskable/Pending	Maskable/Pending    pci
389  *			Block Enable		(if drvr doesn't)   Block Enable
390  * o navail		N/A			#vectors - #used    N/A
391  *
392  * 4. PCI MSI-X
393  * o Flags supported	Maskable/Pending	Maskable/Pending    pci
394  *			Block Enable				    Block Enable
395  * o navail		N/A			#vectors - #used    N/A
396  *
397  * where:
398  *	#vectors	- Total numbers of vectors available
399  *	#used		- Total numbers of vectors currently being used
400  *
401  * For devices complying to PCI2.3 or greater, see bit10 of Command Register
402  * 0 - enables assertion of INTx
403  * 1 - disables assertion of INTx
404  *
405  * For non MSI/X interrupts; if the IRQ is shared then all ddi_intr_set_*()
406  * operations return failure.
407  */
408 int
409 ddi_intr_get_cap(ddi_intr_handle_t h, int *flagsp)
410 {
411 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
412 	int			ret;
413 
414 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_cap: hdlp = %p\n",
415 	    (void *)hdlp));
416 
417 	*flagsp = 0;
418 	if (hdlp == NULL)
419 		return (DDI_EINVAL);
420 
421 	rw_enter(&hdlp->ih_rwlock, RW_READER);
422 
423 	if (hdlp->ih_cap) {
424 		*flagsp = hdlp->ih_cap & ~DDI_INTR_FLAG_MSI64;
425 		rw_exit(&hdlp->ih_rwlock);
426 		return (DDI_SUCCESS);
427 	}
428 
429 	ret = i_ddi_handle_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
430 	    DDI_INTROP_GETCAP, hdlp, (void *)flagsp);
431 
432 	if (ret == DDI_SUCCESS) {
433 		hdlp->ih_cap = *flagsp;
434 
435 		/* Mask out MSI/X 64-bit support to the consumer */
436 		*flagsp &= ~DDI_INTR_FLAG_MSI64;
437 	}
438 
439 	rw_exit(&hdlp->ih_rwlock);
440 	return (ret);
441 }
442 
443 int
444 ddi_intr_set_cap(ddi_intr_handle_t h, int flags)
445 {
446 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
447 	int			ret;
448 
449 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_set_cap: hdlp = %p", (void *)hdlp));
450 
451 	if (hdlp == NULL)
452 		return (DDI_EINVAL);
453 
454 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
455 	if (hdlp->ih_state != DDI_IHDL_STATE_ALLOC) {
456 		rw_exit(&hdlp->ih_rwlock);
457 		return (DDI_EINVAL);
458 	}
459 
460 	/* Only DDI_INTR_FLAG_LEVEL or DDI_INTR_FLAG_EDGE are allowed */
461 	if (!(flags & (DDI_INTR_FLAG_EDGE | DDI_INTR_FLAG_LEVEL))) {
462 		DDI_INTR_APIDBG((CE_CONT, "%s%d: only LEVEL or EDGE capability "
463 		    "can be set\n", ddi_driver_name(hdlp->ih_dip),
464 		    ddi_get_instance(hdlp->ih_dip)));
465 		rw_exit(&hdlp->ih_rwlock);
466 		return (DDI_EINVAL);
467 	}
468 
469 	/* Both level/edge flags must be currently supported */
470 	if (!(hdlp->ih_cap & (DDI_INTR_FLAG_EDGE | DDI_INTR_FLAG_LEVEL))) {
471 		DDI_INTR_APIDBG((CE_CONT, "%s%d: Both LEVEL and EDGE capability"
472 		    " must be supported\n", ddi_driver_name(hdlp->ih_dip),
473 		    ddi_get_instance(hdlp->ih_dip)));
474 		rw_exit(&hdlp->ih_rwlock);
475 		return (DDI_ENOTSUP);
476 	}
477 
478 	ret = i_ddi_handle_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
479 	    DDI_INTROP_SETCAP, hdlp, &flags);
480 
481 	rw_exit(&hdlp->ih_rwlock);
482 	return (ret);
483 }
484 
485 /*
486  * Priority related functions
487  */
488 
489 /*
490  * ddi_intr_get_hilevel_pri:
491  *	Returns the minimum priority level for a
492  *	high-level interrupt on a platform.
493  */
494 uint_t
495 ddi_intr_get_hilevel_pri(void)
496 {
497 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_hilevel_pri:\n"));
498 	return (LOCK_LEVEL + 1);
499 }
500 
501 int
502 ddi_intr_get_pri(ddi_intr_handle_t h, uint_t *prip)
503 {
504 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
505 	int			ret;
506 
507 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_pri: hdlp = %p\n",
508 	    (void *)hdlp));
509 
510 	*prip = 0;
511 	if (hdlp == NULL)
512 		return (DDI_EINVAL);
513 
514 	rw_enter(&hdlp->ih_rwlock, RW_READER);
515 	/* Already initialized, just return that */
516 	if (hdlp->ih_pri) {
517 		*prip = hdlp->ih_pri;
518 		rw_exit(&hdlp->ih_rwlock);
519 		return (DDI_SUCCESS);
520 	}
521 
522 	ret = i_ddi_handle_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
523 	    DDI_INTROP_GETPRI, hdlp, (void *)prip);
524 
525 	if (ret == DDI_SUCCESS)
526 		hdlp->ih_pri = *prip;
527 
528 	rw_exit(&hdlp->ih_rwlock);
529 	return (ret);
530 }
531 
532 int
533 ddi_intr_set_pri(ddi_intr_handle_t h, uint_t pri)
534 {
535 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
536 	int			ret;
537 
538 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_set_pri: hdlp = %p", (void *)hdlp));
539 
540 	if (hdlp == NULL)
541 		return (DDI_EINVAL);
542 
543 	/* Validate priority argument */
544 	if (pri < DDI_INTR_PRI_MIN || pri > DDI_INTR_PRI_MAX) {
545 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_set_pri: invalid priority "
546 		    "specified  = %x\n", pri));
547 		return (DDI_EINVAL);
548 	}
549 
550 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
551 	if (hdlp->ih_state != DDI_IHDL_STATE_ALLOC) {
552 		rw_exit(&hdlp->ih_rwlock);
553 		return (DDI_EINVAL);
554 	}
555 
556 	/* If the passed priority is same as existing priority; do nothing */
557 	if (pri == hdlp->ih_pri) {
558 		rw_exit(&hdlp->ih_rwlock);
559 		return (DDI_SUCCESS);
560 	}
561 
562 	ret = i_ddi_handle_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
563 	    DDI_INTROP_SETPRI, hdlp, &pri);
564 
565 	if (ret == DDI_SUCCESS)
566 		hdlp->ih_pri = pri;
567 
568 	rw_exit(&hdlp->ih_rwlock);
569 	return (ret);
570 }
571 
572 /*
573  * Interrupt add/duplicate/remove handlers
574  */
575 int
576 ddi_intr_add_handler(ddi_intr_handle_t h, ddi_intr_handler_t inthandler,
577     void *arg1, void *arg2)
578 {
579 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
580 	int			ret;
581 
582 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_add_handler: hdlp = 0x%p\n",
583 	    (void *)hdlp));
584 
585 	if ((hdlp == NULL) || (inthandler == NULL))
586 		return (DDI_EINVAL);
587 
588 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
589 	if (hdlp->ih_state != DDI_IHDL_STATE_ALLOC) {
590 		rw_exit(&hdlp->ih_rwlock);
591 		return (DDI_EINVAL);
592 	}
593 
594 	hdlp->ih_cb_func = inthandler;
595 	hdlp->ih_cb_arg1 = arg1;
596 	hdlp->ih_cb_arg2 = arg2;
597 
598 	ret = i_ddi_handle_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
599 	    DDI_INTROP_ADDISR, hdlp, NULL);
600 
601 	if (ret != DDI_SUCCESS) {
602 		hdlp->ih_cb_func = NULL;
603 		hdlp->ih_cb_arg1 = NULL;
604 		hdlp->ih_cb_arg2 = NULL;
605 	} else
606 		hdlp->ih_state = DDI_IHDL_STATE_ADDED;
607 
608 	rw_exit(&hdlp->ih_rwlock);
609 	return (ret);
610 }
611 
612 int
613 ddi_intr_dup_handler(ddi_intr_handle_t org, int vector, ddi_intr_handle_t *dup)
614 {
615 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)org;
616 	ddi_intr_handle_impl_t	*dup_hdlp;
617 	int			ret;
618 
619 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_dup_handler: hdlp = 0x%p\n",
620 	    (void *)hdlp));
621 
622 	/* Do some input argument checking ("dup" is not allocated) */
623 	if ((hdlp == NULL) || (dup != NULL))
624 		return (DDI_EINVAL);
625 
626 	rw_enter(&hdlp->ih_rwlock, RW_READER);
627 
628 	/* Do some input argument checking */
629 	if ((hdlp->ih_state == DDI_IHDL_STATE_ALLOC) ||	/* intr handle alloc? */
630 	    (hdlp->ih_type != DDI_INTR_TYPE_MSIX)) {	/* only MSI-X allowed */
631 		rw_exit(&hdlp->ih_rwlock);
632 		return (DDI_EINVAL);
633 	}
634 
635 	ret = i_ddi_handle_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
636 	    DDI_INTROP_DUPVEC, hdlp, (void *)&vector);
637 
638 	if (ret == DDI_SUCCESS) {
639 		dup_hdlp = (ddi_intr_handle_impl_t *)
640 		    kmem_zalloc(sizeof (ddi_intr_handle_impl_t), KM_SLEEP);
641 
642 		dup = (ddi_intr_handle_t *)dup_hdlp;
643 		rw_init(&dup_hdlp->ih_rwlock, NULL, RW_DRIVER, NULL);
644 		rw_enter(&dup_hdlp->ih_rwlock, RW_WRITER);
645 		dup_hdlp->ih_ver = DDI_INTR_VERSION;
646 		dup_hdlp->ih_state = DDI_IHDL_STATE_ADDED;
647 		dup_hdlp->ih_dip = hdlp->ih_dip;
648 		dup_hdlp->ih_type = hdlp->ih_type;
649 		dup_hdlp->ih_pri = hdlp->ih_pri;
650 		dup_hdlp->ih_cap = hdlp->ih_cap;
651 		dup_hdlp->ih_inum = hdlp->ih_inum;
652 		/* What about MSI-X vector */
653 
654 		dup_hdlp->ih_cb_func = hdlp->ih_cb_func;
655 		dup_hdlp->ih_cb_arg1 = hdlp->ih_cb_arg1;
656 		dup_hdlp->ih_cb_arg2 = hdlp->ih_cb_arg2;
657 		rw_exit(&dup_hdlp->ih_rwlock);
658 	}
659 
660 	rw_exit(&hdlp->ih_rwlock);
661 	return (ret);
662 }
663 
664 int
665 ddi_intr_remove_handler(ddi_intr_handle_t h)
666 {
667 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
668 	int			ret;
669 
670 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_remove_handler: hdlp = %p\n",
671 	    (void *)hdlp));
672 
673 	if (hdlp == NULL)
674 		return (DDI_EINVAL);
675 
676 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
677 	if (hdlp->ih_state != DDI_IHDL_STATE_ADDED) {
678 		rw_exit(&hdlp->ih_rwlock);
679 		return (DDI_EINVAL);
680 	}
681 
682 	ret = i_ddi_handle_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
683 	    DDI_INTROP_REMISR, hdlp, NULL);
684 
685 	if (ret == DDI_SUCCESS) {
686 		hdlp->ih_state = DDI_IHDL_STATE_ALLOC;
687 		hdlp->ih_cb_func = NULL;
688 		hdlp->ih_cb_arg1 = NULL;
689 		hdlp->ih_cb_arg2 = NULL;
690 	}
691 
692 	rw_exit(&hdlp->ih_rwlock);
693 	return (ret);
694 }
695 
696 /*
697  * Interrupt enable/disable/block_enable/block_disable handlers
698  */
699 int
700 ddi_intr_enable(ddi_intr_handle_t h)
701 {
702 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
703 	int			ret;
704 
705 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_enable: hdlp = %p\n",
706 	    (void *)hdlp));
707 
708 	if (hdlp == NULL)
709 		return (DDI_EINVAL);
710 
711 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
712 	if ((hdlp->ih_state != DDI_IHDL_STATE_ADDED) ||
713 	    ((hdlp->ih_type == DDI_INTR_TYPE_MSI) &&
714 	    (hdlp->ih_cap & DDI_INTR_FLAG_BLOCK))) {
715 		rw_exit(&hdlp->ih_rwlock);
716 		return (DDI_EINVAL);
717 	}
718 
719 	ret = i_ddi_handle_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
720 	    DDI_INTROP_ENABLE, hdlp, NULL);
721 
722 	if (ret == DDI_SUCCESS)
723 		hdlp->ih_state = DDI_IHDL_STATE_ENABLE;
724 
725 	rw_exit(&hdlp->ih_rwlock);
726 	return (ret);
727 }
728 
729 int
730 ddi_intr_disable(ddi_intr_handle_t h)
731 {
732 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
733 	int			ret;
734 
735 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_disable: hdlp = %p\n",
736 	    (void *)hdlp));
737 
738 	if (hdlp == NULL)
739 		return (DDI_EINVAL);
740 
741 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
742 	if ((hdlp->ih_state != DDI_IHDL_STATE_ENABLE) ||
743 	    ((hdlp->ih_type == DDI_INTR_TYPE_MSI) &&
744 	    (hdlp->ih_cap & DDI_INTR_FLAG_BLOCK))) {
745 		rw_exit(&hdlp->ih_rwlock);
746 		return (DDI_EINVAL);
747 	}
748 
749 	ret = i_ddi_handle_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
750 	    DDI_INTROP_DISABLE, hdlp, NULL);
751 
752 	if (ret == DDI_SUCCESS)
753 		hdlp->ih_state = DDI_IHDL_STATE_ADDED;
754 
755 	rw_exit(&hdlp->ih_rwlock);
756 	return (ret);
757 }
758 
759 int
760 ddi_intr_block_enable(ddi_intr_handle_t *h_array, int count)
761 {
762 	ddi_intr_handle_impl_t	*hdlp;
763 	int			i, ret;
764 
765 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_block_enable: h_array = %p\n",
766 	    (void *)h_array));
767 
768 	if (h_array == NULL)
769 		return (DDI_EINVAL);
770 
771 	for (i = 0; i < count; i++) {
772 		hdlp = (ddi_intr_handle_impl_t *)h_array[i];
773 		rw_enter(&hdlp->ih_rwlock, RW_READER);
774 
775 		if (hdlp->ih_state != DDI_IHDL_STATE_ADDED ||
776 		    hdlp->ih_type != DDI_INTR_TYPE_MSI ||
777 		    !(hdlp->ih_cap & DDI_INTR_FLAG_BLOCK)) {
778 			rw_exit(&hdlp->ih_rwlock);
779 			return (DDI_EINVAL);
780 		}
781 		rw_exit(&hdlp->ih_rwlock);
782 	}
783 
784 	hdlp = (ddi_intr_handle_impl_t *)h_array[0];
785 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
786 	hdlp->ih_scratch1 = count;
787 
788 	ret = i_ddi_handle_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
789 	    DDI_INTROP_BLOCKENABLE, hdlp, NULL);
790 
791 	rw_exit(&hdlp->ih_rwlock);
792 
793 	if (ret == DDI_SUCCESS) {
794 		for (i = 0; i < count; i++) {
795 			hdlp = (ddi_intr_handle_impl_t *)h_array[i];
796 			rw_enter(&hdlp->ih_rwlock, RW_WRITER);
797 			hdlp->ih_state = DDI_IHDL_STATE_ENABLE;
798 			rw_exit(&hdlp->ih_rwlock);
799 		}
800 	}
801 
802 	return (ret);
803 }
804 
805 int
806 ddi_intr_block_disable(ddi_intr_handle_t *h_array, int count)
807 {
808 	ddi_intr_handle_impl_t	*hdlp;
809 	int			i, ret;
810 
811 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_block_disable: h_array = %p\n",
812 	    (void *)h_array));
813 
814 	if (h_array == NULL)
815 		return (DDI_EINVAL);
816 
817 	for (i = 0; i < count; i++) {
818 		hdlp = (ddi_intr_handle_impl_t *)h_array[i];
819 		rw_enter(&hdlp->ih_rwlock, RW_READER);
820 		if (hdlp->ih_state != DDI_IHDL_STATE_ENABLE ||
821 		    hdlp->ih_type != DDI_INTR_TYPE_MSI ||
822 		    !(hdlp->ih_cap & DDI_INTR_FLAG_BLOCK)) {
823 			rw_exit(&hdlp->ih_rwlock);
824 			return (DDI_EINVAL);
825 		}
826 		rw_exit(&hdlp->ih_rwlock);
827 	}
828 
829 	hdlp = (ddi_intr_handle_impl_t *)h_array[0];
830 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
831 	hdlp->ih_scratch1 = count;
832 
833 	ret = i_ddi_handle_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
834 	    DDI_INTROP_BLOCKDISABLE, hdlp, NULL);
835 
836 	rw_exit(&hdlp->ih_rwlock);
837 
838 	if (ret == DDI_SUCCESS) {
839 		for (i = 0; i < count; i++) {
840 			hdlp = (ddi_intr_handle_impl_t *)h_array[i];
841 			rw_enter(&hdlp->ih_rwlock, RW_WRITER);
842 			hdlp->ih_state = DDI_IHDL_STATE_ADDED;
843 			rw_exit(&hdlp->ih_rwlock);
844 		}
845 	}
846 
847 	return (ret);
848 }
849 
850 /*
851  * Interrupt set/clr mask handlers
852  */
853 int
854 ddi_intr_set_mask(ddi_intr_handle_t h)
855 {
856 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
857 	int			ret;
858 
859 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_set_mask: hdlp = %p\n",
860 	    (void *)hdlp));
861 
862 	if (hdlp == NULL)
863 		return (DDI_EINVAL);
864 
865 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
866 	if (!(hdlp->ih_cap & DDI_INTR_FLAG_MASKABLE)) {
867 		rw_exit(&hdlp->ih_rwlock);
868 		return (DDI_EINVAL);
869 	}
870 
871 	ret =  i_ddi_handle_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
872 	    DDI_INTROP_SETMASK, hdlp, NULL);
873 
874 	rw_exit(&hdlp->ih_rwlock);
875 	return (ret);
876 }
877 
878 int
879 ddi_intr_clr_mask(ddi_intr_handle_t h)
880 {
881 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
882 	int			ret;
883 
884 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_clr_mask: hdlp = %p\n",
885 	    (void *)hdlp));
886 
887 	if (hdlp == NULL)
888 		return (DDI_EINVAL);
889 
890 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
891 	if (!(hdlp->ih_cap & DDI_INTR_FLAG_MASKABLE)) {
892 		rw_exit(&hdlp->ih_rwlock);
893 		return (DDI_EINVAL);
894 	}
895 
896 	ret = i_ddi_handle_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
897 	    DDI_INTROP_CLRMASK, hdlp, NULL);
898 
899 	rw_exit(&hdlp->ih_rwlock);
900 	return (ret);
901 }
902 
903 /*
904  * Interrupt get_pending handler
905  */
906 int
907 ddi_intr_get_pending(ddi_intr_handle_t h, int *pendingp)
908 {
909 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
910 	int			ret;
911 
912 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_pending: hdlp = %p\n",
913 	    (void *)hdlp));
914 
915 	if (hdlp == NULL)
916 		return (DDI_EINVAL);
917 
918 	rw_enter(&hdlp->ih_rwlock, RW_READER);
919 	if (!(hdlp->ih_cap & DDI_INTR_FLAG_PENDING)) {
920 		rw_exit(&hdlp->ih_rwlock);
921 		return (DDI_EINVAL);
922 	}
923 
924 	ret = i_ddi_handle_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
925 	    DDI_INTROP_GETPENDING, hdlp, (void *)pendingp);
926 
927 	rw_exit(&hdlp->ih_rwlock);
928 	return (ret);
929 }
930 
931 /*
932  * Soft interrupt handlers
933  */
934 /*
935  * Add a soft interrupt and register its handler
936  */
937 /* ARGSUSED */
938 int
939 ddi_intr_add_softint(dev_info_t *dip, ddi_softint_handle_t *h_p, int soft_pri,
940     ddi_intr_handler_t handler, void *arg1)
941 {
942 	ddi_softint_hdl_impl_t	*hdlp;
943 	int			ret;
944 
945 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_add_softint: dip = %p, "
946 	    "softpri = 0x%x\n", (void *)dip, soft_pri));
947 
948 	if ((dip == NULL) || (h_p == NULL) || (handler == NULL)) {
949 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_add_softint: "
950 		    "invalid arguments"));
951 
952 		return (DDI_EINVAL);
953 	}
954 
955 	/* Validate input arguments */
956 	if (soft_pri < DDI_INTR_SOFTPRI_MIN ||
957 	    soft_pri > DDI_INTR_SOFTPRI_MAX) {
958 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_add_softint: invalid "
959 		    "soft_pri input given  = %x\n", soft_pri));
960 		return (DDI_EINVAL);
961 	}
962 
963 	hdlp = (ddi_softint_hdl_impl_t *)kmem_zalloc(
964 	    sizeof (ddi_softint_hdl_impl_t), KM_SLEEP);
965 
966 	/* fill up internally */
967 	rw_init(&hdlp->ih_rwlock, NULL, RW_DRIVER, NULL);
968 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
969 	hdlp->ih_pri = soft_pri;
970 	hdlp->ih_dip = dip;
971 	hdlp->ih_cb_func = handler;
972 	hdlp->ih_cb_arg1 = arg1;
973 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_add_softint: hdlp = %p\n",
974 	    (void *)hdlp));
975 
976 	/* do the platform specific calls */
977 	if ((ret = i_ddi_add_softint(hdlp)) != DDI_SUCCESS) {
978 		rw_exit(&hdlp->ih_rwlock);
979 		rw_destroy(&hdlp->ih_rwlock);
980 		kmem_free(hdlp, sizeof (ddi_softint_hdl_impl_t));
981 		return (ret);
982 	}
983 
984 	*h_p = (ddi_softint_handle_t)hdlp;
985 	rw_exit(&hdlp->ih_rwlock);
986 	return (ret);
987 }
988 
989 /*
990  * Remove the soft interrupt
991  */
992 int
993 ddi_intr_remove_softint(ddi_softint_handle_t h)
994 {
995 	ddi_softint_hdl_impl_t	*hdlp = (ddi_softint_hdl_impl_t *)h;
996 
997 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_remove_softint: hdlp = %p\n",
998 	    (void *)hdlp));
999 
1000 	if (hdlp == NULL)
1001 		return (DDI_EINVAL);
1002 
1003 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
1004 	i_ddi_remove_softint(hdlp);
1005 	rw_exit(&hdlp->ih_rwlock);
1006 	rw_destroy(&hdlp->ih_rwlock);
1007 
1008 	/* kmem_free the hdl impl_t structure allocated earlier */
1009 	kmem_free(hdlp, sizeof (ddi_softint_hdl_impl_t));
1010 	return (DDI_SUCCESS);
1011 }
1012 
1013 /*
1014  * Trigger a soft interrupt
1015  */
1016 int
1017 ddi_intr_trigger_softint(ddi_softint_handle_t h, void *arg2)
1018 {
1019 	ddi_softint_hdl_impl_t	*hdlp = (ddi_softint_hdl_impl_t *)h;
1020 	int			ret;
1021 
1022 	if (hdlp == NULL)
1023 		return (DDI_EINVAL);
1024 
1025 	if ((ret = i_ddi_trigger_softint(hdlp, arg2)) != DDI_SUCCESS) {
1026 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_trigger_softint: failed, "
1027 		    " ret 0%x\n", ret));
1028 
1029 		return (ret);
1030 	}
1031 
1032 	hdlp->ih_cb_arg2 = arg2;
1033 	return (DDI_SUCCESS);
1034 }
1035 
1036 /*
1037  * Get the soft interrupt priority
1038  */
1039 int
1040 ddi_intr_get_softint_pri(ddi_softint_handle_t h, uint_t *soft_prip)
1041 {
1042 	ddi_softint_hdl_impl_t	*hdlp = (ddi_softint_hdl_impl_t *)h;
1043 
1044 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_softint_pri: h = %p\n",
1045 	    (void *)h));
1046 
1047 	if (hdlp == NULL)
1048 		return (DDI_EINVAL);
1049 
1050 	rw_enter(&hdlp->ih_rwlock, RW_READER);
1051 	*soft_prip = hdlp->ih_pri;
1052 	rw_exit(&hdlp->ih_rwlock);
1053 	return (DDI_SUCCESS);
1054 }
1055 
1056 /*
1057  * Set the soft interrupt priority
1058  */
1059 int
1060 ddi_intr_set_softint_pri(ddi_softint_handle_t h, uint_t soft_pri)
1061 {
1062 	ddi_softint_hdl_impl_t	*hdlp = (ddi_softint_hdl_impl_t *)h;
1063 	int			ret;
1064 	uint_t			orig_soft_pri;
1065 
1066 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_set_softint_pri: h = %p\n",
1067 	    (void *)h));
1068 
1069 	if (hdlp == NULL)
1070 		return (DDI_EINVAL);
1071 
1072 	/* Validate priority argument */
1073 	if (soft_pri < DDI_INTR_SOFTPRI_MIN ||
1074 	    soft_pri > DDI_INTR_SOFTPRI_MAX) {
1075 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_set_softint_pri: invalid "
1076 		    "soft_pri input given  = %x\n", soft_pri));
1077 		return (DDI_EINVAL);
1078 	}
1079 
1080 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
1081 	orig_soft_pri = hdlp->ih_pri;
1082 	hdlp->ih_pri = soft_pri;
1083 
1084 	if ((ret = i_ddi_set_softint_pri(hdlp, orig_soft_pri)) != DDI_SUCCESS) {
1085 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_set_softint_pri: failed, "
1086 		    " ret 0%x\n", ret));
1087 		hdlp->ih_pri = orig_soft_pri;
1088 	}
1089 
1090 	rw_exit(&hdlp->ih_rwlock);
1091 	return (ret);
1092 }
1093 
1094 /*
1095  * Old DDI interrupt framework
1096  */
1097 
1098 int
1099 ddi_intr_hilevel(dev_info_t *dip, uint_t inumber)
1100 {
1101 	ddi_intr_handle_t	hdl, *existing_hdlp;
1102 	int			actual, ret;
1103 	uint_t			high_pri, pri;
1104 
1105 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_hilevel: name=%s%d dip=0x%p "
1106 	    "inum=0x%x\n", ddi_driver_name(dip), ddi_get_instance(dip),
1107 	    (void *)dip, inumber));
1108 
1109 	/*
1110 	 * The device driver may have already registed with the
1111 	 * framework. If so, first try to get the existing interrupt handle
1112 	 * for that given inumber and use that handle.
1113 	 */
1114 	existing_hdlp = i_ddi_get_intr_handle(dip, inumber);
1115 	if (existing_hdlp) {
1116 		hdl = existing_hdlp[0];	/* Use existing handle */
1117 	} else {
1118 		if ((ret = ddi_intr_alloc(dip, &hdl, DDI_INTR_TYPE_FIXED,
1119 		    inumber, 1, &actual, 0)) != DDI_SUCCESS) {
1120 			DDI_INTR_APIDBG((CE_CONT, "ddi_intr_hilevel: "
1121 			    "ddi_intr_alloc failed, ret 0x%x\n", ret));
1122 			return (0);
1123 		}
1124 	}
1125 
1126 	if ((ret = ddi_intr_get_pri(hdl, &pri)) != DDI_SUCCESS) {
1127 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_hilevel: "
1128 		    "ddi_intr_get_pri failed, ret 0x%x\n", ret));
1129 		(void) ddi_intr_free(hdl);
1130 		return (0);
1131 	}
1132 
1133 	high_pri = ddi_intr_get_hilevel_pri();
1134 
1135 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_hilevel: pri = %x, "
1136 	    "high_pri = %x\n", pri, high_pri));
1137 
1138 	/* Free the handle allocated here only if no existing handle exists */
1139 	if (existing_hdlp == NULL)
1140 		(void) ddi_intr_free(hdl);
1141 
1142 	return (pri >= high_pri);
1143 }
1144 
1145 int
1146 ddi_dev_nintrs(dev_info_t *dip, int *result)
1147 {
1148 	DDI_INTR_APIDBG((CE_CONT, "ddi_dev_nintrs: name=%s%d dip=0x%p\n",
1149 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
1150 
1151 	if (ddi_intr_get_nintrs(dip, DDI_INTR_TYPE_FIXED,
1152 	    result) != DDI_SUCCESS) {
1153 		DDI_INTR_APIDBG((CE_CONT, "ddi_dev_nintrs: "
1154 		    "ddi_intr_get_nintrs failed\n"));
1155 		*result = 0;
1156 	}
1157 
1158 	return (DDI_SUCCESS);
1159 }
1160 
1161 int
1162 ddi_get_iblock_cookie(dev_info_t *dip, uint_t inumber,
1163     ddi_iblock_cookie_t *iblock_cookiep)
1164 {
1165 	ddi_intr_handle_t	hdl, *existing_hdlp;
1166 	int			actual, ret;
1167 	uint_t			pri;
1168 
1169 	DDI_INTR_APIDBG((CE_CONT, "ddi_get_iblock_cookie: name=%s%d dip=0x%p "
1170 	    "inum=0x%x\n", ddi_driver_name(dip), ddi_get_instance(dip),
1171 	    (void *)dip, inumber));
1172 
1173 	ASSERT(iblock_cookiep != NULL);
1174 
1175 	/*
1176 	 * The device driver may have already registed with the
1177 	 * framework. If so, first try to get the existing interrupt handle
1178 	 * for that given inumber and use that handle.
1179 	 */
1180 	existing_hdlp = i_ddi_get_intr_handle(dip, inumber);
1181 	if (existing_hdlp) {
1182 		hdl = existing_hdlp[0];	/* Use existing handle */
1183 	} else {
1184 		if ((ret = ddi_intr_alloc(dip, &hdl, DDI_INTR_TYPE_FIXED,
1185 		    inumber, 1, &actual, 0)) != DDI_SUCCESS) {
1186 			DDI_INTR_APIDBG((CE_CONT, "ddi_get_iblock_cookie: "
1187 			    "ddi_intr_alloc failed, ret 0x%x\n", ret));
1188 			return (DDI_INTR_NOTFOUND);
1189 		}
1190 	}
1191 
1192 	if ((ret = ddi_intr_get_pri(hdl, &pri)) != DDI_SUCCESS) {
1193 		DDI_INTR_APIDBG((CE_CONT, "ddi_get_iblock_cookie: "
1194 		    "ddi_intr_get_pri failed, ret 0x%x\n", ret));
1195 
1196 		(void) ddi_intr_free(hdl);
1197 		return (DDI_FAILURE);
1198 	}
1199 
1200 	*iblock_cookiep = (ddi_iblock_cookie_t)(uintptr_t)pri;
1201 	/* Free the handle allocated here only if no existing handle exists */
1202 	if (existing_hdlp == NULL)
1203 		(void) ddi_intr_free(hdl);
1204 
1205 	return (DDI_SUCCESS);
1206 }
1207 
1208 int
1209 ddi_add_intr(dev_info_t *dip, uint_t inumber,
1210     ddi_iblock_cookie_t *iblock_cookiep,
1211     ddi_idevice_cookie_t *idevice_cookiep,
1212     uint_t (*int_handler)(caddr_t int_handler_arg),
1213     caddr_t int_handler_arg)
1214 {
1215 	ddi_intr_handle_t	*hdl_p;
1216 	int			actual, ret;
1217 	uint_t			pri;
1218 
1219 	DDI_INTR_APIDBG((CE_CONT, "ddi_add_intr: name=%s%d dip=0x%p "
1220 	    "inum=0x%x\n", ddi_driver_name(dip), ddi_get_instance(dip),
1221 	    (void *)dip, inumber));
1222 
1223 	hdl_p = kmem_zalloc(sizeof (ddi_intr_handle_t), KM_SLEEP);
1224 
1225 	if ((ret = ddi_intr_alloc(dip, hdl_p, DDI_INTR_TYPE_FIXED,
1226 	    inumber, 1, &actual, 0)) != DDI_SUCCESS) {
1227 		DDI_INTR_APIDBG((CE_CONT, "ddi_add_intr: "
1228 		    "ddi_intr_alloc failed, ret 0x%x\n", ret));
1229 		kmem_free(hdl_p, sizeof (ddi_intr_handle_t));
1230 		return (DDI_INTR_NOTFOUND);
1231 	}
1232 
1233 	if ((ret = ddi_intr_get_pri(hdl_p[0], &pri)) != DDI_SUCCESS)  {
1234 		DDI_INTR_APIDBG((CE_CONT, "ddi_add_intr: "
1235 		    "ddi_intr_get_pri failed, ret 0x%x\n", ret));
1236 		(void) ddi_intr_free(hdl_p[0]);
1237 		kmem_free(hdl_p, sizeof (ddi_intr_handle_t));
1238 		return (DDI_FAILURE);
1239 	}
1240 
1241 	if ((ret = ddi_intr_add_handler(hdl_p[0], (ddi_intr_handler_t *)
1242 	    int_handler, int_handler_arg, NULL)) != DDI_SUCCESS) {
1243 		DDI_INTR_APIDBG((CE_CONT, "ddi_add_intr: "
1244 		    "ddi_intr_add_handler failed, ret 0x%x\n", ret));
1245 		(void) ddi_intr_free(hdl_p[0]);
1246 		kmem_free(hdl_p, sizeof (ddi_intr_handle_t));
1247 		return (DDI_FAILURE);
1248 	}
1249 
1250 	if ((ret = ddi_intr_enable(hdl_p[0])) != DDI_SUCCESS) {
1251 		DDI_INTR_APIDBG((CE_CONT, "ddi_add_intr: "
1252 		    "ddi_intr_enable failed, ret 0x%x\n", ret));
1253 		(void) ddi_intr_remove_handler(hdl_p[0]);
1254 		(void) ddi_intr_free(hdl_p[0]);
1255 		kmem_free(hdl_p, sizeof (ddi_intr_handle_t));
1256 		return (DDI_FAILURE);
1257 	}
1258 
1259 	if (iblock_cookiep)
1260 		*iblock_cookiep = (ddi_iblock_cookie_t)(uintptr_t)pri;
1261 
1262 	if (idevice_cookiep) {
1263 		idevice_cookiep->idev_vector = 0;
1264 		idevice_cookiep->idev_priority = pri;
1265 	}
1266 
1267 	/* this line may be removed in near future */
1268 	i_ddi_set_intr_handle(dip, inumber, hdl_p);
1269 
1270 	return (DDI_SUCCESS);
1271 }
1272 
1273 /* ARGSUSED */
1274 int
1275 ddi_add_fastintr(dev_info_t *dip, uint_t inumber,
1276     ddi_iblock_cookie_t *iblock_cookiep,
1277     ddi_idevice_cookie_t *idevice_cookiep,
1278     uint_t (*hi_int_handler)(void))
1279 {
1280 	DDI_INTR_APIDBG((CE_CONT, "ddi_add_fastintr: name=%s%d dip=0x%p "
1281 	    "inum=0x%x: Not supported, return failure\n", ddi_driver_name(dip),
1282 	    ddi_get_instance(dip), (void *)dip, inumber));
1283 
1284 	return (DDI_FAILURE);
1285 }
1286 
1287 /* ARGSUSED */
1288 void
1289 ddi_remove_intr(dev_info_t *dip, uint_t inum, ddi_iblock_cookie_t iblock_cookie)
1290 {
1291 	ddi_intr_handle_t	*hdl_p;
1292 	int			ret;
1293 
1294 	DDI_INTR_APIDBG((CE_CONT, "ddi_remove_intr: name=%s%d dip=0x%p "
1295 	    "inum=0x%x\n", ddi_driver_name(dip), ddi_get_instance(dip),
1296 	    (void *)dip, inum));
1297 
1298 	if ((hdl_p = i_ddi_get_intr_handle(dip, inum)) == NULL) {
1299 		DDI_INTR_APIDBG((CE_CONT, "ddi_remove_intr: no handle "
1300 		    "found\n"));
1301 		return;
1302 	}
1303 
1304 	if ((ret = ddi_intr_disable(hdl_p[0])) != DDI_SUCCESS) {
1305 		DDI_INTR_APIDBG((CE_CONT, "ddi_remove_intr: "
1306 		    "ddi_intr_disable failed, ret 0x%x\n", ret));
1307 		return;
1308 	}
1309 
1310 	if ((ret = ddi_intr_remove_handler(hdl_p[0])) != DDI_SUCCESS) {
1311 		DDI_INTR_APIDBG((CE_CONT, "ddi_remove_intr: "
1312 		    "ddi_intr_remove_handler failed, ret 0x%x\n", ret));
1313 		return;
1314 	}
1315 
1316 	if ((ret = ddi_intr_free(hdl_p[0])) != DDI_SUCCESS) {
1317 		DDI_INTR_APIDBG((CE_CONT, "ddi_remove_intr: "
1318 		    "ddi_intr_free failed, ret 0x%x\n", ret));
1319 		return;
1320 	}
1321 
1322 	kmem_free(hdl_p, sizeof (ddi_intr_handle_t));
1323 
1324 	/* this line may be removed in near future */
1325 	i_ddi_set_intr_handle(dip, inum, NULL);
1326 }
1327 
1328 /* ARGSUSED */
1329 int
1330 ddi_get_soft_iblock_cookie(dev_info_t *dip, int preference,
1331     ddi_iblock_cookie_t *iblock_cookiep)
1332 {
1333 	DDI_INTR_APIDBG((CE_CONT, "ddi_get_soft_iblock_cookie: name=%s%d "
1334 	    "dip=0x%p pref=0x%x\n", ddi_driver_name(dip), ddi_get_instance(dip),
1335 	    (void *)dip, preference));
1336 
1337 	ASSERT(iblock_cookiep != NULL);
1338 
1339 	if (preference == DDI_SOFTINT_FIXED)
1340 		return (DDI_FAILURE);
1341 
1342 	*iblock_cookiep = (ddi_iblock_cookie_t)((uintptr_t)
1343 	    ((preference > DDI_SOFTINT_MED) ? DDI_SOFT_INTR_PRI_H :
1344 	    DDI_SOFT_INTR_PRI_M));
1345 
1346 	return (DDI_SUCCESS);
1347 }
1348 
1349 int
1350 ddi_add_softintr(dev_info_t *dip, int preference, ddi_softintr_t *idp,
1351     ddi_iblock_cookie_t *iblock_cookiep,
1352     ddi_idevice_cookie_t *idevice_cookiep,
1353     uint_t (*int_handler)(caddr_t int_handler_arg),
1354     caddr_t int_handler_arg)
1355 {
1356 	ddi_softint_handle_t	*hdl_p;
1357 	uint64_t		softpri;
1358 	int			ret;
1359 
1360 	DDI_INTR_APIDBG((CE_CONT, "ddi_add_softintr: name=%s%d dip=0x%p "
1361 	    "pref=0x%x\n", ddi_driver_name(dip), ddi_get_instance(dip),
1362 	    (void *)dip, preference));
1363 
1364 	if ((idp == NULL) || ((preference == DDI_SOFTINT_FIXED) &&
1365 	    (iblock_cookiep == NULL)))
1366 		return (DDI_FAILURE);
1367 
1368 	/* Translate the priority preference */
1369 	if (preference == DDI_SOFTINT_FIXED) {
1370 		softpri = (uint64_t)(uintptr_t)*iblock_cookiep;
1371 		softpri = MIN(softpri, DDI_SOFT_INTR_PRI_H);
1372 	} else {
1373 		softpri = (uint64_t)((preference > DDI_SOFTINT_MED) ?
1374 		    DDI_SOFT_INTR_PRI_H : DDI_SOFT_INTR_PRI_M);
1375 	}
1376 
1377 	DDI_INTR_APIDBG((CE_CONT, "ddi_add_softintr: preference 0x%x "
1378 	    "softpri 0x%lx\n", preference, (long)softpri));
1379 
1380 	hdl_p = kmem_zalloc(sizeof (ddi_softint_handle_t), KM_SLEEP);
1381 	if ((ret = ddi_intr_add_softint(dip, hdl_p, softpri,
1382 	    (ddi_intr_handler_t *)int_handler, int_handler_arg)) !=
1383 	    DDI_SUCCESS) {
1384 		DDI_INTR_APIDBG((CE_CONT, "ddi_add_softintr: "
1385 		    "ddi_intr_add_softint failed, ret 0x%x\n", ret));
1386 
1387 		kmem_free(hdl_p, sizeof (ddi_softint_handle_t));
1388 		return (DDI_FAILURE);
1389 	}
1390 
1391 	if (iblock_cookiep)
1392 		*iblock_cookiep =  (ddi_iblock_cookie_t)(uintptr_t)softpri;
1393 
1394 	if (idevice_cookiep) {
1395 		idevice_cookiep->idev_vector = 0;
1396 		idevice_cookiep->idev_priority = softpri;
1397 	}
1398 
1399 	*idp = (ddi_softintr_t)hdl_p;
1400 
1401 	DDI_INTR_APIDBG((CE_CONT, "ddi_add_softintr: dip = 0x%p, "
1402 	    "idp = 0x%p, ret = %x\n", (void *)dip, (void *)*idp, ret));
1403 
1404 	return (DDI_SUCCESS);
1405 }
1406 
1407 void
1408 ddi_remove_softintr(ddi_softintr_t id)
1409 {
1410 	ddi_softint_handle_t	*h_p = (ddi_softint_handle_t *)id;
1411 
1412 	DDI_INTR_APIDBG((CE_CONT, "ddi_remove_softintr: id=0x%p\n",
1413 	    (void *)id));
1414 
1415 	if (h_p == NULL)
1416 		return;
1417 
1418 	DDI_INTR_APIDBG((CE_CONT, "ddi_remove_softintr: handle 0x%p\n",
1419 	    (void *)h_p));
1420 
1421 	(void) ddi_intr_remove_softint(*h_p);
1422 	kmem_free(h_p, sizeof (ddi_softint_handle_t));
1423 }
1424 
1425 void
1426 ddi_trigger_softintr(ddi_softintr_t id)
1427 {
1428 	ddi_softint_handle_t	*h_p = (ddi_softint_handle_t *)id;
1429 	int			ret;
1430 
1431 	if (h_p == NULL)
1432 		return;
1433 
1434 	if ((ret = ddi_intr_trigger_softint(*h_p, NULL)) != DDI_SUCCESS) {
1435 		DDI_INTR_APIDBG((CE_CONT, "ddi_trigger_softintr: "
1436 		    "ddi_intr_trigger_softint failed, hdlp 0x%p "
1437 		    "ret 0x%x\n", (void *)h_p, ret));
1438 	}
1439 }
1440