xref: /titanic_41/usr/src/uts/sun4/io/px/px_ib.c (revision 9113a79cf228b8f7bd509b1328adf88659dfe218)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * PX Interrupt Block implementation
30  */
31 
32 #include <sys/types.h>
33 #include <sys/kmem.h>
34 #include <sys/async.h>
35 #include <sys/systm.h>		/* panicstr */
36 #include <sys/spl.h>
37 #include <sys/sunddi.h>
38 #include <sys/machsystm.h>	/* intr_dist_add */
39 #include <sys/ddi_impldefs.h>
40 #include <sys/cpuvar.h>
41 #include <sys/time.h>
42 #include "px_obj.h"
43 
44 /*LINTLIBRARY*/
45 
46 static void px_ib_intr_redist(void *arg, int32_t weight_max, int32_t weight);
47 static void px_ib_cpu_ticks_to_ih_nsec(px_ib_t *ib_p, px_ih_t *ih_p,
48     uint32_t cpu_id);
49 static uint_t px_ib_intr_reset(void *arg);
50 static void px_fill_in_intr_devs(pcitool_intr_dev_t *dev, char *driver_name,
51     char *path_name, int instance);
52 
53 extern uint64_t xc_tick_jump_limit;
54 
55 int
56 px_ib_attach(px_t *px_p)
57 {
58 	dev_info_t	*dip = px_p->px_dip;
59 	px_ib_t		*ib_p;
60 	sysino_t	sysino;
61 	px_fault_t	*fault_p = &px_p->px_fault;
62 
63 	DBG(DBG_IB, dip, "px_ib_attach\n");
64 
65 	if (px_lib_intr_devino_to_sysino(px_p->px_dip,
66 	    px_p->px_inos[PX_INTR_PEC], &sysino) != DDI_SUCCESS)
67 		return (DDI_FAILURE);
68 
69 	/*
70 	 * Allocate interrupt block state structure and link it to
71 	 * the px state structure.
72 	 */
73 	ib_p = kmem_zalloc(sizeof (px_ib_t), KM_SLEEP);
74 	px_p->px_ib_p = ib_p;
75 	ib_p->ib_px_p = px_p;
76 	ib_p->ib_ino_lst = (px_ino_t *)NULL;
77 
78 	mutex_init(&ib_p->ib_intr_lock, NULL, MUTEX_DRIVER, NULL);
79 	mutex_init(&ib_p->ib_ino_lst_mutex, NULL, MUTEX_DRIVER, NULL);
80 
81 	bus_func_register(BF_TYPE_RESINTR, px_ib_intr_reset, ib_p);
82 
83 	intr_dist_add_weighted(px_ib_intr_redist, ib_p);
84 
85 	/*
86 	 * Initialize PEC fault data structure
87 	 */
88 	fault_p->px_fh_dip = dip;
89 	fault_p->px_fh_sysino = sysino;
90 	fault_p->px_err_func = px_err_dmc_pec_intr;
91 	fault_p->px_intr_ino = px_p->px_inos[PX_INTR_PEC];
92 
93 	return (DDI_SUCCESS);
94 }
95 
96 void
97 px_ib_detach(px_t *px_p)
98 {
99 	px_ib_t		*ib_p = px_p->px_ib_p;
100 	dev_info_t	*dip = px_p->px_dip;
101 
102 	DBG(DBG_IB, dip, "px_ib_detach\n");
103 
104 	bus_func_unregister(BF_TYPE_RESINTR, px_ib_intr_reset, ib_p);
105 	intr_dist_rem_weighted(px_ib_intr_redist, ib_p);
106 
107 	mutex_destroy(&ib_p->ib_ino_lst_mutex);
108 	mutex_destroy(&ib_p->ib_intr_lock);
109 
110 	px_ib_free_ino_all(ib_p);
111 
112 	px_p->px_ib_p = NULL;
113 	kmem_free(ib_p, sizeof (px_ib_t));
114 }
115 
116 void
117 px_ib_intr_enable(px_t *px_p, cpuid_t cpu_id, devino_t ino)
118 {
119 	px_ib_t		*ib_p = px_p->px_ib_p;
120 	sysino_t	sysino;
121 
122 	/*
123 	 * Determine the cpu for the interrupt
124 	 */
125 	mutex_enter(&ib_p->ib_intr_lock);
126 
127 	DBG(DBG_IB, px_p->px_dip,
128 	    "px_ib_intr_enable: ino=%x cpu_id=%x\n", ino, cpu_id);
129 
130 	if (px_lib_intr_devino_to_sysino(px_p->px_dip, ino,
131 	    &sysino) != DDI_SUCCESS) {
132 		DBG(DBG_IB, px_p->px_dip,
133 		    "px_ib_intr_enable: px_intr_devino_to_sysino() failed\n");
134 
135 		mutex_exit(&ib_p->ib_intr_lock);
136 		return;
137 	}
138 
139 	PX_INTR_ENABLE(px_p->px_dip, sysino, cpu_id);
140 	px_lib_intr_setstate(px_p->px_dip, sysino, INTR_IDLE_STATE);
141 
142 	mutex_exit(&ib_p->ib_intr_lock);
143 }
144 
145 /*ARGSUSED*/
146 void
147 px_ib_intr_disable(px_ib_t *ib_p, devino_t ino, int wait)
148 {
149 	sysino_t	sysino;
150 
151 	mutex_enter(&ib_p->ib_intr_lock);
152 
153 	DBG(DBG_IB, ib_p->ib_px_p->px_dip, "px_ib_intr_disable: ino=%x\n", ino);
154 
155 	/* Disable the interrupt */
156 	if (px_lib_intr_devino_to_sysino(ib_p->ib_px_p->px_dip, ino,
157 	    &sysino) != DDI_SUCCESS) {
158 		DBG(DBG_IB, ib_p->ib_px_p->px_dip,
159 		    "px_ib_intr_disable: px_intr_devino_to_sysino() failed\n");
160 
161 		mutex_exit(&ib_p->ib_intr_lock);
162 		return;
163 	}
164 
165 	PX_INTR_DISABLE(ib_p->ib_px_p->px_dip, sysino);
166 
167 	mutex_exit(&ib_p->ib_intr_lock);
168 }
169 
170 
171 void
172 px_ib_intr_dist_en(dev_info_t *dip, cpuid_t cpu_id, devino_t ino,
173     boolean_t wait_flag)
174 {
175 	uint32_t	old_cpu_id;
176 	sysino_t	sysino;
177 	intr_valid_state_t	enabled = 0;
178 	hrtime_t	start_time, prev, curr, interval, jump;
179 	hrtime_t	intr_timeout;
180 	intr_state_t	intr_state;
181 	int		e = DDI_SUCCESS;
182 
183 	DBG(DBG_IB, dip, "px_ib_intr_dist_en: ino=0x%x\n", ino);
184 
185 	if (px_lib_intr_devino_to_sysino(dip, ino, &sysino) != DDI_SUCCESS) {
186 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: "
187 		    "px_intr_devino_to_sysino() failed, ino 0x%x\n", ino);
188 		return;
189 	}
190 
191 	/* Skip enabling disabled interrupts */
192 	if (px_lib_intr_getvalid(dip, sysino, &enabled) != DDI_SUCCESS) {
193 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: px_intr_getvalid() "
194 		    "failed, sysino 0x%x\n", sysino);
195 		return;
196 	}
197 	if (!enabled)
198 		return;
199 
200 	/* Done if redistributed onto the same cpuid */
201 	if (px_lib_intr_gettarget(dip, sysino, &old_cpu_id) != DDI_SUCCESS) {
202 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: "
203 		    "px_intr_gettarget() failed\n");
204 		return;
205 	}
206 	if (cpu_id == old_cpu_id)
207 		return;
208 
209 	if (!wait_flag)
210 		goto done;
211 
212 	/* Busy wait on pending interrupts */
213 	PX_INTR_DISABLE(dip, sysino);
214 
215 	intr_timeout = px_intrpend_timeout;
216 	jump = TICK_TO_NSEC(xc_tick_jump_limit);
217 
218 	for (curr = start_time = gethrtime(); !panicstr &&
219 	    ((e = px_lib_intr_getstate(dip, sysino, &intr_state)) ==
220 		DDI_SUCCESS) &&
221 	    (intr_state == INTR_DELIVERED_STATE); /* */) {
222 		/*
223 		 * If we have a really large jump in hrtime, it is most
224 		 * probably because we entered the debugger (or OBP,
225 		 * in general). So, we adjust the timeout accordingly
226 		 * to prevent declaring an interrupt timeout. The
227 		 * master-interrupt mechanism in OBP should deliver
228 		 * the interrupts properly.
229 		 */
230 		prev = curr;
231 		curr = gethrtime();
232 		interval = curr - prev;
233 		if (interval > jump)
234 			intr_timeout += interval;
235 		if (curr - start_time > intr_timeout) {
236 			cmn_err(CE_WARN,
237 			    "%s%d: px_ib_intr_dist_en: sysino 0x%lx(ino 0x%x) "
238 			    "from cpu id 0x%x to 0x%x timeout",
239 			    ddi_driver_name(dip), ddi_get_instance(dip),
240 			    sysino, ino, old_cpu_id, cpu_id);
241 
242 			e = DDI_FAILURE;
243 			break;
244 		}
245 	}
246 
247 	if (e != DDI_SUCCESS)
248 		DBG(DBG_IB, dip, "px_ib_intr_dist_en: failed, "
249 		    "ino 0x%x sysino 0x%x\n", ino, sysino);
250 
251 done:
252 	PX_INTR_ENABLE(dip, sysino, cpu_id);
253 }
254 
255 static void
256 px_ib_cpu_ticks_to_ih_nsec(px_ib_t *ib_p, px_ih_t *ih_p, uint32_t cpu_id)
257 {
258 	extern kmutex_t pxintr_ks_template_lock;
259 	hrtime_t ticks;
260 
261 	/*
262 	 * Because we are updating two fields in ih_t we must lock
263 	 * pxintr_ks_template_lock to prevent someone from reading the
264 	 * kstats after we set ih_ticks to 0 and before we increment
265 	 * ih_nsec to compensate.
266 	 *
267 	 * We must also protect against the interrupt arriving and incrementing
268 	 * ih_ticks between the time we read it and when we reset it to 0.
269 	 * To do this we use atomic_swap.
270 	 */
271 
272 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
273 
274 	mutex_enter(&pxintr_ks_template_lock);
275 	ticks = atomic_swap_64(&ih_p->ih_ticks, 0);
276 	ih_p->ih_nsec += (uint64_t)tick2ns(ticks, cpu_id);
277 	mutex_exit(&pxintr_ks_template_lock);
278 }
279 
280 
281 /*
282  * Redistribute interrupts of the specified weight. The first call has a weight
283  * of weight_max, which can be used to trigger initialization for
284  * redistribution. The inos with weight [weight_max, inf.) should be processed
285  * on the "weight == weight_max" call.  This first call is followed by calls
286  * of decreasing weights, inos of that weight should be processed.  The final
287  * call specifies a weight of zero, this can be used to trigger processing of
288  * stragglers.
289  */
290 static void
291 px_ib_intr_redist(void *arg, int32_t weight_max, int32_t weight)
292 {
293 	px_ib_t		*ib_p = (px_ib_t *)arg;
294 	px_t		*px_p = ib_p->ib_px_p;
295 	dev_info_t	*dip = px_p->px_dip;
296 	px_ino_t	*ino_p;
297 	px_ino_pil_t	*ipil_p;
298 	px_ih_t		*ih_lst;
299 	int32_t		dweight = 0;
300 	int		i;
301 
302 	/* Redistribute internal interrupts */
303 	if (weight == 0) {
304 		mutex_enter(&ib_p->ib_intr_lock);
305 		px_ib_intr_dist_en(dip, intr_dist_cpuid(),
306 		    px_p->px_inos[PX_INTR_PEC], B_FALSE);
307 		mutex_exit(&ib_p->ib_intr_lock);
308 	}
309 
310 	/* Redistribute device interrupts */
311 	mutex_enter(&ib_p->ib_ino_lst_mutex);
312 
313 	for (ino_p = ib_p->ib_ino_lst; ino_p; ino_p = ino_p->ino_next_p) {
314 		uint32_t orig_cpuid;
315 
316 		/*
317 		 * Recomputes the sum of interrupt weights of devices that
318 		 * share the same ino upon first call marked by
319 		 * (weight == weight_max).
320 		 */
321 		if (weight == weight_max) {
322 			ino_p->ino_intr_weight = 0;
323 
324 			for (ipil_p = ino_p->ino_ipil_p; ipil_p;
325 			    ipil_p = ipil_p->ipil_next_p) {
326 				for (i = 0, ih_lst = ipil_p->ipil_ih_head;
327 				    i < ipil_p->ipil_ih_size; i++,
328 				    ih_lst = ih_lst->ih_next) {
329 					dweight = i_ddi_get_intr_weight(
330 					    ih_lst->ih_dip);
331 					if (dweight > 0)
332 						ino_p->ino_intr_weight +=
333 						    dweight;
334 				}
335 			}
336 		}
337 
338 		/*
339 		 * As part of redistributing weighted interrupts over cpus,
340 		 * nexus redistributes device interrupts and updates
341 		 * cpu weight. The purpose is for the most light weighted
342 		 * cpu to take the next interrupt and gain weight, therefore
343 		 * attention demanding device gains more cpu attention by
344 		 * making itself heavy.
345 		 */
346 		if ((weight == ino_p->ino_intr_weight) ||
347 		    ((weight >= weight_max) &&
348 		    (ino_p->ino_intr_weight >= weight_max))) {
349 			orig_cpuid = ino_p->ino_cpuid;
350 			if (cpu[orig_cpuid] == NULL)
351 				orig_cpuid = CPU->cpu_id;
352 
353 			/* select cpuid to target and mark ino established */
354 			ino_p->ino_cpuid = intr_dist_cpuid();
355 
356 			/* Add device weight to targeted cpu. */
357 			for (ipil_p = ino_p->ino_ipil_p; ipil_p;
358 			    ipil_p = ipil_p->ipil_next_p) {
359 				for (i = 0, ih_lst = ipil_p->ipil_ih_head;
360 				    i < ipil_p->ipil_ih_size; i++,
361 				    ih_lst = ih_lst->ih_next) {
362 
363 					dweight = i_ddi_get_intr_weight(
364 					    ih_lst->ih_dip);
365 					intr_dist_cpuid_add_device_weight(
366 					    ino_p->ino_cpuid, ih_lst->ih_dip,
367 					    dweight);
368 
369 					/*
370 					 * Different cpus may have different
371 					 * clock speeds. to account for this,
372 					 * whenever an interrupt is moved to a
373 					 * new CPU, we convert the accumulated
374 					 * ticks into nsec, based upon the clock
375 					 * rate of the prior CPU.
376 					 *
377 					 * It is possible that the prior CPU no
378 					 * longer exists. In this case, fall
379 					 * back to using this CPU's clock rate.
380 					 *
381 					 * Note that the value in ih_ticks has
382 					 * already been corrected for any power
383 					 * savings mode which might have been
384 					 * in effect.
385 					 */
386 					px_ib_cpu_ticks_to_ih_nsec(ib_p, ih_lst,
387 					    orig_cpuid);
388 				}
389 			}
390 
391 			/* enable interrupt on new targeted cpu */
392 			px_ib_intr_dist_en(dip, ino_p->ino_cpuid,
393 			    ino_p->ino_ino, B_TRUE);
394 		}
395 	}
396 	mutex_exit(&ib_p->ib_ino_lst_mutex);
397 }
398 
399 /*
400  * Reset interrupts to IDLE.  This function is called during
401  * panic handling after redistributing interrupts; it's needed to
402  * support dumping to network devices after 'sync' from OBP.
403  *
404  * N.B.  This routine runs in a context where all other threads
405  * are permanently suspended.
406  */
407 static uint_t
408 px_ib_intr_reset(void *arg)
409 {
410 	px_ib_t		*ib_p = (px_ib_t *)arg;
411 
412 	DBG(DBG_IB, ib_p->ib_px_p->px_dip, "px_ib_intr_reset\n");
413 
414 	if (px_lib_intr_reset(ib_p->ib_px_p->px_dip) != DDI_SUCCESS)
415 		return (BF_FATAL);
416 
417 	return (BF_NONE);
418 }
419 
420 /*
421  * Locate px_ino_t structure on ib_p->ib_ino_lst according to ino#
422  * returns NULL if not found.
423  */
424 px_ino_t *
425 px_ib_locate_ino(px_ib_t *ib_p, devino_t ino_num)
426 {
427 	px_ino_t	*ino_p = ib_p->ib_ino_lst;
428 
429 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
430 
431 	for (; ino_p && ino_p->ino_ino != ino_num; ino_p = ino_p->ino_next_p);
432 
433 	return (ino_p);
434 }
435 
436 px_ino_pil_t *
437 px_ib_new_ino_pil(px_ib_t *ib_p, devino_t ino_num, uint_t pil, px_ih_t *ih_p)
438 {
439 	px_ino_pil_t	*ipil_p = kmem_zalloc(sizeof (px_ino_pil_t), KM_SLEEP);
440 	px_ino_t	*ino_p;
441 
442 	if ((ino_p = px_ib_locate_ino(ib_p, ino_num)) == NULL) {
443 		sysino_t	sysino;
444 
445 		if (px_lib_intr_devino_to_sysino(ib_p->ib_px_p->px_dip,
446 		    ino_num, &sysino) != DDI_SUCCESS)
447 			return (NULL);
448 
449 		ino_p = kmem_zalloc(sizeof (px_ino_t), KM_SLEEP);
450 
451 		ino_p->ino_next_p = ib_p->ib_ino_lst;
452 		ib_p->ib_ino_lst = ino_p;
453 
454 		ino_p->ino_ino = ino_num;
455 		ino_p->ino_sysino = sysino;
456 		ino_p->ino_ib_p = ib_p;
457 		ino_p->ino_unclaimed_intrs = 0;
458 		ino_p->ino_lopil = pil;
459 	}
460 
461 	ih_p->ih_next = ih_p;
462 	ipil_p->ipil_pil = pil;
463 	ipil_p->ipil_ih_head = ih_p;
464 	ipil_p->ipil_ih_tail = ih_p;
465 	ipil_p->ipil_ih_start = ih_p;
466 	ipil_p->ipil_ih_size = 1;
467 	ipil_p->ipil_ino_p = ino_p;
468 
469 	ipil_p->ipil_next_p = ino_p->ino_ipil_p;
470 	ino_p->ino_ipil_p = ipil_p;
471 	ino_p->ino_ipil_size++;
472 
473 	if (ino_p->ino_lopil > pil)
474 		ino_p->ino_lopil = pil;
475 
476 	return (ipil_p);
477 }
478 
479 void
480 px_ib_delete_ino_pil(px_ib_t *ib_p, px_ino_pil_t *ipil_p)
481 {
482 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
483 	ushort_t	pil = ipil_p->ipil_pil;
484 	px_ino_pil_t	*prev, *next;
485 
486 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
487 
488 	if (ino_p->ino_ipil_p == ipil_p)
489 		ino_p->ino_ipil_p = ipil_p->ipil_next_p;
490 	else {
491 		for (prev = next = ino_p->ino_ipil_p; next != ipil_p;
492 		    prev = next, next = next->ipil_next_p);
493 
494 		if (prev)
495 			prev->ipil_next_p = ipil_p->ipil_next_p;
496 	}
497 
498 	kmem_free(ipil_p, sizeof (px_ino_pil_t));
499 
500 	if (ino_p->ino_lopil == pil) {
501 		for (pil = 0, next = ino_p->ino_ipil_p; next;
502 		    next = next->ipil_next_p) {
503 			if (pil > next->ipil_pil)
504 				pil = next->ipil_pil;
505 		}
506 
507 		ino_p->ino_lopil = pil;
508 	}
509 
510 	if (--ino_p->ino_ipil_size)
511 		return;
512 
513 	if (ib_p->ib_ino_lst == ino_p)
514 		ib_p->ib_ino_lst = ino_p->ino_next_p;
515 	else {
516 		px_ino_t	*list = ib_p->ib_ino_lst;
517 
518 		for (; list->ino_next_p != ino_p; list = list->ino_next_p);
519 		list->ino_next_p = ino_p->ino_next_p;
520 	}
521 }
522 
523 /*
524  * Free all ino when we are detaching.
525  */
526 void
527 px_ib_free_ino_all(px_ib_t *ib_p)
528 {
529 	px_ino_t	*ino_p = ib_p->ib_ino_lst;
530 	px_ino_t	*next = NULL;
531 
532 	while (ino_p) {
533 		next = ino_p->ino_next_p;
534 		kmem_free(ino_p, sizeof (px_ino_t));
535 		ino_p = next;
536 	}
537 }
538 
539 /*
540  * Locate px_ino_pil_t structure on ino_p->ino_ipil_p according to ino#
541  * returns NULL if not found.
542  */
543 px_ino_pil_t *
544 px_ib_ino_locate_ipil(px_ino_t *ino_p, uint_t pil)
545 {
546 	px_ino_pil_t	*ipil_p = ino_p->ino_ipil_p;
547 
548 	for (; ipil_p && ipil_p->ipil_pil != pil; ipil_p = ipil_p->ipil_next_p);
549 
550 	return (ipil_p);
551 }
552 
553 int
554 px_ib_ino_add_intr(px_t *px_p, px_ino_pil_t *ipil_p, px_ih_t *ih_p)
555 {
556 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
557 	px_ib_t		*ib_p = ino_p->ino_ib_p;
558 	devino_t	ino = ino_p->ino_ino;
559 	sysino_t	sysino = ino_p->ino_sysino;
560 	dev_info_t	*dip = px_p->px_dip;
561 	cpuid_t		curr_cpu;
562 	hrtime_t	start_time;
563 	intr_state_t	intr_state;
564 	int		ret = DDI_SUCCESS;
565 
566 	ASSERT(MUTEX_HELD(&ib_p->ib_ino_lst_mutex));
567 	ASSERT(ib_p == px_p->px_ib_p);
568 
569 	DBG(DBG_IB, dip, "px_ib_ino_add_intr ino=%x\n", ino_p->ino_ino);
570 
571 	/* Disable the interrupt */
572 	if ((ret = px_lib_intr_gettarget(dip, sysino,
573 	    &curr_cpu)) != DDI_SUCCESS) {
574 		DBG(DBG_IB, dip,
575 		    "px_ib_ino_add_intr px_intr_gettarget() failed\n");
576 
577 		return (ret);
578 	}
579 
580 	PX_INTR_DISABLE(dip, sysino);
581 
582 	/* Busy wait on pending interrupt */
583 	for (start_time = gethrtime(); !panicstr &&
584 	    ((ret = px_lib_intr_getstate(dip, sysino, &intr_state))
585 	    == DDI_SUCCESS) && (intr_state == INTR_DELIVERED_STATE); /* */) {
586 		if (gethrtime() - start_time > px_intrpend_timeout) {
587 			cmn_err(CE_WARN, "%s%d: px_ib_ino_add_intr: pending "
588 			    "sysino 0x%lx(ino 0x%x) timeout",
589 			    ddi_driver_name(dip), ddi_get_instance(dip),
590 			    sysino, ino);
591 
592 			ret = DDI_FAILURE;
593 			break;
594 		}
595 	}
596 
597 	/*
598 	 * If the interrupt was previously blocked (left in pending state)
599 	 * because of jabber we need to clear the pending state in case the
600 	 * jabber has gone away.
601 	 */
602 	if (ino_p->ino_unclaimed_intrs > px_unclaimed_intr_max) {
603 		cmn_err(CE_WARN,
604 		    "%s%d: px_ib_ino_add_intr: ino 0x%x has been unblocked",
605 		    ddi_driver_name(dip), ddi_get_instance(dip), ino);
606 
607 		ino_p->ino_unclaimed_intrs = 0;
608 		ret = px_lib_intr_setstate(dip, sysino, INTR_IDLE_STATE);
609 	}
610 
611 	if (ret != DDI_SUCCESS) {
612 		DBG(DBG_IB, dip, "px_ib_ino_add_intr: failed, "
613 		    "ino 0x%x sysino 0x%x\n", ino, sysino);
614 
615 		return (ret);
616 	}
617 
618 	/* Link up px_ih_t */
619 	ih_p->ih_next = ipil_p->ipil_ih_head;
620 	ipil_p->ipil_ih_tail->ih_next = ih_p;
621 	ipil_p->ipil_ih_tail = ih_p;
622 
623 	ipil_p->ipil_ih_start = ipil_p->ipil_ih_head;
624 	ipil_p->ipil_ih_size++;
625 
626 	/* Re-enable interrupt */
627 	PX_INTR_ENABLE(dip, sysino, curr_cpu);
628 
629 	return (ret);
630 }
631 
632 /*
633  * Removes px_ih_t from the ino's link list.
634  * uses hardware mutex to lock out interrupt threads.
635  * Side effects: interrupt belongs to that ino is turned off on return.
636  * if we are sharing PX slot with other inos, the caller needs
637  * to turn it back on.
638  */
639 int
640 px_ib_ino_rem_intr(px_t *px_p, px_ino_pil_t *ipil_p, px_ih_t *ih_p)
641 {
642 	px_ino_t	*ino_p = ipil_p->ipil_ino_p;
643 	devino_t	ino = ino_p->ino_ino;
644 	sysino_t	sysino = ino_p->ino_sysino;
645 	dev_info_t	*dip = px_p->px_dip;
646 	px_ih_t		*ih_lst = ipil_p->ipil_ih_head;
647 	hrtime_t	start_time;
648 	intr_state_t	intr_state;
649 	int		i, ret = DDI_SUCCESS;
650 
651 	ASSERT(MUTEX_HELD(&ino_p->ino_ib_p->ib_ino_lst_mutex));
652 
653 	DBG(DBG_IB, px_p->px_dip, "px_ib_ino_rem_intr ino=%x\n",
654 	    ino_p->ino_ino);
655 
656 	/* Disable the interrupt */
657 	PX_INTR_DISABLE(px_p->px_dip, sysino);
658 
659 	if (ipil_p->ipil_ih_size == 1) {
660 		if (ih_lst != ih_p)
661 			goto not_found;
662 
663 		/* No need to set head/tail as ino_p will be freed */
664 		goto reset;
665 	}
666 
667 	/* Busy wait on pending interrupt */
668 	for (start_time = gethrtime(); !panicstr &&
669 	    ((ret = px_lib_intr_getstate(dip, sysino, &intr_state))
670 	    == DDI_SUCCESS) && (intr_state == INTR_DELIVERED_STATE); /* */) {
671 		if (gethrtime() - start_time > px_intrpend_timeout) {
672 			cmn_err(CE_WARN, "%s%d: px_ib_ino_rem_intr: pending "
673 			    "sysino 0x%lx(ino 0x%x) timeout",
674 			    ddi_driver_name(dip), ddi_get_instance(dip),
675 			    sysino, ino);
676 
677 			ret = DDI_FAILURE;
678 			break;
679 		}
680 	}
681 
682 	/*
683 	 * If the interrupt was previously blocked (left in pending state)
684 	 * because of jabber we need to clear the pending state in case the
685 	 * jabber has gone away.
686 	 */
687 	if (ino_p->ino_unclaimed_intrs > px_unclaimed_intr_max) {
688 		cmn_err(CE_WARN, "%s%d: px_ib_ino_rem_intr: "
689 		    "ino 0x%x has been unblocked",
690 		    ddi_driver_name(dip), ddi_get_instance(dip), ino);
691 
692 		ino_p->ino_unclaimed_intrs = 0;
693 		ret = px_lib_intr_setstate(dip, sysino, INTR_IDLE_STATE);
694 	}
695 
696 	if (ret != DDI_SUCCESS) {
697 		DBG(DBG_IB, dip, "px_ib_ino_rem_intr: failed, "
698 		    "ino 0x%x sysino 0x%x\n", ino, sysino);
699 
700 		return (ret);
701 	}
702 
703 	/* Search the link list for ih_p */
704 	for (i = 0; (i < ipil_p->ipil_ih_size) &&
705 	    (ih_lst->ih_next != ih_p); i++, ih_lst = ih_lst->ih_next);
706 
707 	if (ih_lst->ih_next != ih_p)
708 		goto not_found;
709 
710 	/* Remove ih_p from the link list and maintain the head/tail */
711 	ih_lst->ih_next = ih_p->ih_next;
712 
713 	if (ipil_p->ipil_ih_head == ih_p)
714 		ipil_p->ipil_ih_head = ih_p->ih_next;
715 	if (ipil_p->ipil_ih_tail == ih_p)
716 		ipil_p->ipil_ih_tail = ih_lst;
717 
718 	ipil_p->ipil_ih_start = ipil_p->ipil_ih_head;
719 
720 reset:
721 	if (ih_p->ih_config_handle)
722 		pci_config_teardown(&ih_p->ih_config_handle);
723 	if (ih_p->ih_ksp != NULL)
724 		kstat_delete(ih_p->ih_ksp);
725 
726 	kmem_free(ih_p, sizeof (px_ih_t));
727 	ipil_p->ipil_ih_size--;
728 
729 	return (ret);
730 
731 not_found:
732 	DBG(DBG_R_INTX, ino_p->ino_ib_p->ib_px_p->px_dip,
733 		"ino_p=%x does not have ih_p=%x\n", ino_p, ih_p);
734 
735 	return (DDI_FAILURE);
736 }
737 
738 px_ih_t *
739 px_ib_intr_locate_ih(px_ino_pil_t *ipil_p, dev_info_t *rdip,
740     uint32_t inum, msiq_rec_type_t rec_type, msgcode_t msg_code)
741 {
742 	px_ih_t	*ih_p = ipil_p->ipil_ih_head;
743 	int	i;
744 
745 	for (i = 0; i < ipil_p->ipil_ih_size; i++, ih_p = ih_p->ih_next) {
746 		if ((ih_p->ih_dip == rdip) && (ih_p->ih_inum == inum) &&
747 		    (ih_p->ih_rec_type == rec_type) &&
748 		    (ih_p->ih_msg_code == msg_code))
749 			return (ih_p);
750 	}
751 
752 	return ((px_ih_t *)NULL);
753 }
754 
755 px_ih_t *
756 px_ib_alloc_ih(dev_info_t *rdip, uint32_t inum,
757     uint_t (*int_handler)(caddr_t int_handler_arg1, caddr_t int_handler_arg2),
758     caddr_t int_handler_arg1, caddr_t int_handler_arg2,
759     msiq_rec_type_t rec_type, msgcode_t msg_code)
760 {
761 	px_ih_t	*ih_p;
762 
763 	ih_p = kmem_alloc(sizeof (px_ih_t), KM_SLEEP);
764 	ih_p->ih_dip = rdip;
765 	ih_p->ih_inum = inum;
766 	ih_p->ih_intr_state = PX_INTR_STATE_DISABLE;
767 	ih_p->ih_handler = int_handler;
768 	ih_p->ih_handler_arg1 = int_handler_arg1;
769 	ih_p->ih_handler_arg2 = int_handler_arg2;
770 	ih_p->ih_config_handle = NULL;
771 	ih_p->ih_rec_type = rec_type;
772 	ih_p->ih_msg_code = msg_code;
773 	ih_p->ih_nsec = 0;
774 	ih_p->ih_ticks = 0;
775 	ih_p->ih_ksp = NULL;
776 
777 	return (ih_p);
778 }
779 
780 int
781 px_ib_update_intr_state(px_t *px_p, dev_info_t *rdip,
782     uint_t inum, devino_t ino, uint_t pil,
783     uint_t new_intr_state, msiq_rec_type_t rec_type,
784     msgcode_t msg_code)
785 {
786 	px_ib_t		*ib_p = px_p->px_ib_p;
787 	px_ino_t	*ino_p;
788 	px_ino_pil_t	*ipil_p;
789 	px_ih_t		*ih_p;
790 	int		ret = DDI_FAILURE;
791 
792 	DBG(DBG_IB, px_p->px_dip, "px_ib_update_intr_state: %s%d "
793 	    "inum %x devino %x pil %x state %x\n", ddi_driver_name(rdip),
794 	    ddi_get_instance(rdip), inum, ino, pil, new_intr_state);
795 
796 	mutex_enter(&ib_p->ib_ino_lst_mutex);
797 
798 	ino_p = px_ib_locate_ino(ib_p, ino);
799 	if (ino_p && (ipil_p = px_ib_ino_locate_ipil(ino_p, pil))) {
800 		if (ih_p = px_ib_intr_locate_ih(ipil_p, rdip, inum, rec_type,
801 		    msg_code)) {
802 			ih_p->ih_intr_state = new_intr_state;
803 			ret = DDI_SUCCESS;
804 		}
805 	}
806 
807 	mutex_exit(&ib_p->ib_ino_lst_mutex);
808 	return (ret);
809 }
810 
811 
812 static void
813 px_fill_in_intr_devs(pcitool_intr_dev_t *dev, char *driver_name,
814     char *path_name, int instance)
815 {
816 	(void) strncpy(dev->driver_name, driver_name, MAXMODCONFNAME-1);
817 	dev->driver_name[MAXMODCONFNAME] = '\0';
818 	(void) strncpy(dev->path, path_name, MAXPATHLEN-1);
819 	dev->dev_inst = instance;
820 }
821 
822 
823 /*
824  * Return the dips or number of dips associated with a given interrupt block.
825  * Size of dips array arg is passed in as dips_ret arg.
826  * Number of dips returned is returned in dips_ret arg.
827  * Array of dips gets returned in the dips argument.
828  * Function returns number of dips existing for the given interrupt block.
829  *
830  * Note: this function assumes an enabled/valid INO, which is why it returns
831  * the px node and (Internal) when it finds no other devices (and *devs_ret > 0)
832  */
833 uint8_t
834 pxtool_ib_get_ino_devs(
835     px_t *px_p, uint32_t ino, uint8_t *devs_ret, pcitool_intr_dev_t *devs)
836 {
837 	px_ib_t		*ib_p = px_p->px_ib_p;
838 	px_ino_t	*ino_p;
839 	px_ino_pil_t	*ipil_p;
840 	px_ih_t 	*ih_p;
841 	uint32_t 	num_devs = 0;
842 	char		pathname[MAXPATHLEN];
843 	int		i, j;
844 
845 	mutex_enter(&ib_p->ib_ino_lst_mutex);
846 	ino_p = px_ib_locate_ino(ib_p, ino);
847 	if (ino_p != NULL) {
848 		for (j = 0, ipil_p = ino_p->ino_ipil_p; ipil_p;
849 		    ipil_p = ipil_p->ipil_next_p) {
850 			num_devs += ipil_p->ipil_ih_size;
851 
852 			for (i = 0, ih_p = ipil_p->ipil_ih_head;
853 			    ((i < ipil_p->ipil_ih_size) && (i < *devs_ret));
854 			    i++, j++, ih_p = ih_p->ih_next) {
855 				(void) ddi_pathname(ih_p->ih_dip, pathname);
856 				px_fill_in_intr_devs(&devs[i],
857 				    (char *)ddi_driver_name(ih_p->ih_dip),
858 				    pathname, ddi_get_instance(ih_p->ih_dip));
859 			}
860 		}
861 
862 		*devs_ret = j;
863 	} else if (*devs_ret > 0) {
864 		(void) ddi_pathname(px_p->px_dip, pathname);
865 		strcat(pathname, " (Internal)");
866 		px_fill_in_intr_devs(&devs[0],
867 		    (char *)ddi_driver_name(px_p->px_dip),  pathname,
868 		    ddi_get_instance(px_p->px_dip));
869 		num_devs = *devs_ret = 1;
870 	}
871 
872 	mutex_exit(&ib_p->ib_ino_lst_mutex);
873 
874 	return (num_devs);
875 }
876 
877 
878 void
879 px_ib_log_new_cpu(px_ib_t *ib_p, uint32_t old_cpu_id, uint32_t new_cpu_id,
880     uint32_t ino)
881 {
882 	px_ino_t	*ino_p;
883 	px_ino_pil_t	*ipil_p;
884 	px_ih_t 	*ih_p;
885 	int		i;
886 
887 	mutex_enter(&ib_p->ib_ino_lst_mutex);
888 
889 	/* Log in OS data structures the new CPU. */
890 	if (ino_p = px_ib_locate_ino(ib_p, ino)) {
891 
892 		/* Log in OS data structures the new CPU. */
893 		ino_p->ino_cpuid = new_cpu_id;
894 
895 		for (ipil_p = ino_p->ino_ipil_p; ipil_p;
896 		    ipil_p = ipil_p->ipil_next_p) {
897 			for (i = 0, ih_p = ipil_p->ipil_ih_head;
898 			    (i < ipil_p->ipil_ih_size);
899 			    i++, ih_p = ih_p->ih_next) {
900 				/*
901 				 * Account for any residual time
902 				 * to be logged for old cpu.
903 				 */
904 				px_ib_cpu_ticks_to_ih_nsec(ib_p,
905 				    ih_p, old_cpu_id);
906 			}
907 		}
908 	}
909 
910 	mutex_exit(&ib_p->ib_ino_lst_mutex);
911 }
912